History

bpf: Refactor trampoline update code

As we need to introduce a third type of attachment for trampolines, the flattened signature of arch_prepare_bpf_trampoline gets even more complicated. Refactor the prog and count argument to arch_prepare_bpf_trampoline to use bpf_tramp_progs to simplify the addition and accounting for new attachment types.

KP Singh committed 6 years ago

Commit 88fd9e51 parent cc6fa774 files · +71 −45Browse files

Showing the changes to kernel (2 of 4 files). Show all

Unified · Split

2 files
  1. kernel/bpf/bpf_struct_ops.c +9 −1
  2. kernel/bpf/trampoline.c +35 −27
kernel/bpf/bpf_struct_ops.c+9−1
@@ -320,6 +320,7 @@ static int bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, · 319 unmodified lines
320320 struct bpf_struct_ops_value *uvalue, *kvalue;
321321 const struct btf_member *member;
322322 const struct btf_type *t = st_ops->type;
323+ struct bpf_tramp_progs *tprogs = NULL;
323324 void *udata, *kdata;
324325 int prog_fd, err = 0;
325326 void *image;
@@ -343,6 +344,10 @@ static int bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, · 17 unmodified lines
343344 if (uvalue->state || refcount_read(&uvalue->refcnt))
344345 return -EINVAL;
345346
347+ tprogs = kcalloc(BPF_TRAMP_MAX, sizeof(*tprogs), GFP_KERNEL);
348+ if (!tprogs)
349+ return -ENOMEM;
350+
346351 uvalue = (struct bpf_struct_ops_value *)st_map->uvalue;
347352 kvalue = (struct bpf_struct_ops_value *)&st_map->kvalue;
348353
@@ -425,10 +430,12 @@ static int bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, · 76 unmodified lines
425430 goto reset_unlock;
426431 }
427432
433+ tprogs[BPF_TRAMP_FENTRY].progs[0] = prog;
434+ tprogs[BPF_TRAMP_FENTRY].nr_progs = 1;
428435 err = arch_prepare_bpf_trampoline(image,
429436 st_map->image + PAGE_SIZE,
430437 &st_ops->func_models[i], 0,
431− &prog, 1, NULL, 0, NULL);
438+ tprogs, NULL);
432439 if (err < 0)
433440 goto reset_unlock;
434441
@@ -469,6 +476,7 @@ reset_unlock: · 34 unmodified lines
469476 memset(uvalue, 0, map->value_size);
470477 memset(kvalue, 0, map->value_size);
471478 unlock:
479+ kfree(tprogs);
472480 mutex_unlock(&st_map->lock);
473481 return err;
474482 }
kernel/bpf/trampoline.c+35−27
@@ -190,40 +190,49 @@ static int register_fentry(struct bpf_trampoline *tr, void *new_addr) · 189 unmodified lines
190190 return ret;
191191 }
192192
193−/* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50
194− * bytes on x86. Pick a number to fit into BPF_IMAGE_SIZE / 2
195− */
196−#define BPF_MAX_TRAMP_PROGS 40
193+static struct bpf_tramp_progs *
194+bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total)
195+{
196+ const struct bpf_prog_aux *aux;
197+ struct bpf_tramp_progs *tprogs;
198+ struct bpf_prog **progs;
199+ int kind;
200+
201+ *total = 0;
202+ tprogs = kcalloc(BPF_TRAMP_MAX, sizeof(*tprogs), GFP_KERNEL);
203+ if (!tprogs)
204+ return ERR_PTR(-ENOMEM);
205+
206+ for (kind = 0; kind < BPF_TRAMP_MAX; kind++) {
207+ tprogs[kind].nr_progs = tr->progs_cnt[kind];
208+ *total += tr->progs_cnt[kind];
209+ progs = tprogs[kind].progs;
210+
211+ hlist_for_each_entry(aux, &tr->progs_hlist[kind], tramp_hlist)
212+ *progs++ = aux->prog;
213+ }
214+ return tprogs;
215+}
197216
198217 static int bpf_trampoline_update(struct bpf_trampoline *tr)
199218 {
200219 void *old_image = tr->image + ((tr->selector + 1) & 1) * BPF_IMAGE_SIZE/2;
201220 void *new_image = tr->image + (tr->selector & 1) * BPF_IMAGE_SIZE/2;
202− struct bpf_prog *progs_to_run[BPF_MAX_TRAMP_PROGS];
203− int fentry_cnt = tr->progs_cnt[BPF_TRAMP_FENTRY];
204− int fexit_cnt = tr->progs_cnt[BPF_TRAMP_FEXIT];
205− struct bpf_prog **progs, **fentry, **fexit;
221+ struct bpf_tramp_progs *tprogs;
206222 u32 flags = BPF_TRAMP_F_RESTORE_REGS;
207− struct bpf_prog_aux *aux;
208− int err;
223+ int err, total;
209224
210− if (fentry_cnt + fexit_cnt == 0) {
225+ tprogs = bpf_trampoline_get_progs(tr, &total);
226+ if (IS_ERR(tprogs))
227+ return PTR_ERR(tprogs);
228+
229+ if (total == 0) {
211230 err = unregister_fentry(tr, old_image);
212231 tr->selector = 0;
213232 goto out;
214233 }
215234
216− /* populate fentry progs */
217− fentry = progs = progs_to_run;
218− hlist_for_each_entry(aux, &tr->progs_hlist[BPF_TRAMP_FENTRY], tramp_hlist)
219− *progs++ = aux->prog;
220−
221− /* populate fexit progs */
222− fexit = progs;
223− hlist_for_each_entry(aux, &tr->progs_hlist[BPF_TRAMP_FEXIT], tramp_hlist)
224− *progs++ = aux->prog;
225−
226− if (fexit_cnt)
235+ if (tprogs[BPF_TRAMP_FEXIT].nr_progs)
227236 flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME;
228237
229238 /* Though the second half of trampoline page is unused a task could be
@@ -232,12 +241,11 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr) · 2 unmodified lines
232241 * preempted task. Hence wait for tasks to voluntarily schedule or go
233242 * to userspace.
234243 */
244+
235245 synchronize_rcu_tasks();
236246
237247 err = arch_prepare_bpf_trampoline(new_image, new_image + BPF_IMAGE_SIZE / 2,
238− &tr->func.model, flags,
239− fentry, fentry_cnt,
240− fexit, fexit_cnt,
248+ &tr->func.model, flags, tprogs,
241249 tr->func.addr);
242250 if (err < 0)
243251 goto out;
@@ -252,6 +260,7 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr) · 8 unmodified lines
252260 goto out;
253261 tr->selector++;
254262 out:
263+ kfree(tprogs);
255264 return err;
256265 }
257266
@@ -409,8 +418,7 @@ void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start) · 151 unmodified lines
409418 int __weak
410419 arch_prepare_bpf_trampoline(void *image, void *image_end,
411420 const struct btf_func_model *m, u32 flags,
412− struct bpf_prog **fentry_progs, int fentry_cnt,
413− struct bpf_prog **fexit_progs, int fexit_cnt,
421+ struct bpf_tramp_progs *tprogs,
414422 void *orig_call)
415423 {
416424 return -ENOTSUPP;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t