Skip to content

Commit 31a645a

Browse files
Hou TaoAlexei Starovoitov
authored andcommitted
bpf: Factor out a helper to prepare trampoline for struct_ops prog
Factor out a helper bpf_struct_ops_prepare_trampoline() to prepare trampoline for BPF_PROG_TYPE_STRUCT_OPS prog. It will be used by .test_run callback in following patch. Signed-off-by: Hou Tao <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 36e70b9 commit 31a645a

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

include/linux/bpf.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,10 @@ bool bpf_struct_ops_get(const void *kdata);
10001000
void bpf_struct_ops_put(const void *kdata);
10011001
int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
10021002
void *value);
1003+
int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_progs *tprogs,
1004+
struct bpf_prog *prog,
1005+
const struct btf_func_model *model,
1006+
void *image, void *image_end);
10031007
static inline bool bpf_try_module_get(const void *data, struct module *owner)
10041008
{
10051009
if (owner == BPF_MODULE_OWNER)

kernel/bpf/bpf_struct_ops.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,20 @@ static int check_zero_holes(const struct btf_type *t, void *data)
312312
return 0;
313313
}
314314

315+
int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_progs *tprogs,
316+
struct bpf_prog *prog,
317+
const struct btf_func_model *model,
318+
void *image, void *image_end)
319+
{
320+
u32 flags;
321+
322+
tprogs[BPF_TRAMP_FENTRY].progs[0] = prog;
323+
tprogs[BPF_TRAMP_FENTRY].nr_progs = 1;
324+
flags = model->ret_size > 0 ? BPF_TRAMP_F_RET_FENTRY_RET : 0;
325+
return arch_prepare_bpf_trampoline(NULL, image, image_end,
326+
model, flags, tprogs, NULL);
327+
}
328+
315329
static int bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
316330
void *value, u64 flags)
317331
{
@@ -323,7 +337,7 @@ static int bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
323337
struct bpf_tramp_progs *tprogs = NULL;
324338
void *udata, *kdata;
325339
int prog_fd, err = 0;
326-
void *image;
340+
void *image, *image_end;
327341
u32 i;
328342

329343
if (flags)
@@ -363,12 +377,12 @@ static int bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
363377
udata = &uvalue->data;
364378
kdata = &kvalue->data;
365379
image = st_map->image;
380+
image_end = st_map->image + PAGE_SIZE;
366381

367382
for_each_member(i, t, member) {
368383
const struct btf_type *mtype, *ptype;
369384
struct bpf_prog *prog;
370385
u32 moff;
371-
u32 flags;
372386

373387
moff = btf_member_bit_offset(t, member) / 8;
374388
ptype = btf_type_resolve_ptr(btf_vmlinux, member->type, NULL);
@@ -430,14 +444,9 @@ static int bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
430444
goto reset_unlock;
431445
}
432446

433-
tprogs[BPF_TRAMP_FENTRY].progs[0] = prog;
434-
tprogs[BPF_TRAMP_FENTRY].nr_progs = 1;
435-
flags = st_ops->func_models[i].ret_size > 0 ?
436-
BPF_TRAMP_F_RET_FENTRY_RET : 0;
437-
err = arch_prepare_bpf_trampoline(NULL, image,
438-
st_map->image + PAGE_SIZE,
439-
&st_ops->func_models[i],
440-
flags, tprogs, NULL);
447+
err = bpf_struct_ops_prepare_trampoline(tprogs, prog,
448+
&st_ops->func_models[i],
449+
image, image_end);
441450
if (err < 0)
442451
goto reset_unlock;
443452

0 commit comments

Comments
 (0)