Skip to content

Commit 82583da

Browse files
liu-song-6Alexei Starovoitov
authored andcommitted
bpf: Add helpers for trampoline image management
As BPF trampoline of different archs moves from bpf_jit_[alloc|free]_exec() to bpf_prog_pack_[alloc|free](), we need to use different _alloc, _free for different archs during the transition. Add the following helpers for this transition: void *arch_alloc_bpf_trampoline(unsigned int size); void arch_free_bpf_trampoline(void *image, unsigned int size); void arch_protect_bpf_trampoline(void *image, unsigned int size); void arch_unprotect_bpf_trampoline(void *image, unsigned int size); The fallback version of these helpers require size <= PAGE_SIZE, but they are only called with size == PAGE_SIZE. They will be called with size < PAGE_SIZE when arch_bpf_trampoline_size() helper is introduced later. Signed-off-by: Song Liu <[email protected]> Acked-by: Ilya Leoshkevich <[email protected]> Tested-by: Ilya Leoshkevich <[email protected]> # on s390x Acked-by: Jiri Olsa <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 7a3d9a1 commit 82583da

File tree

4 files changed

+52
-18
lines changed

4 files changed

+52
-18
lines changed

include/linux/bpf.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,11 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i
11021102
const struct btf_func_model *m, u32 flags,
11031103
struct bpf_tramp_links *tlinks,
11041104
void *func_addr);
1105+
void *arch_alloc_bpf_trampoline(unsigned int size);
1106+
void arch_free_bpf_trampoline(void *image, unsigned int size);
1107+
void arch_protect_bpf_trampoline(void *image, unsigned int size);
1108+
void arch_unprotect_bpf_trampoline(void *image, unsigned int size);
1109+
11051110
u64 notrace __bpf_prog_enter_sleepable_recur(struct bpf_prog *prog,
11061111
struct bpf_tramp_run_ctx *run_ctx);
11071112
void notrace __bpf_prog_exit_sleepable_recur(struct bpf_prog *prog, u64 start,

kernel/bpf/bpf_struct_ops.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
515515
if (err)
516516
goto reset_unlock;
517517
}
518-
set_memory_rox((long)st_map->image, 1);
518+
arch_protect_bpf_trampoline(st_map->image, PAGE_SIZE);
519519
/* Let bpf_link handle registration & unregistration.
520520
*
521521
* Pair with smp_load_acquire() during lookup_elem().
@@ -524,7 +524,7 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
524524
goto unlock;
525525
}
526526

527-
set_memory_rox((long)st_map->image, 1);
527+
arch_protect_bpf_trampoline(st_map->image, PAGE_SIZE);
528528
err = st_ops->reg(kdata);
529529
if (likely(!err)) {
530530
/* This refcnt increment on the map here after
@@ -547,8 +547,7 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
547547
* there was a race in registering the struct_ops (under the same name) to
548548
* a sub-system through different struct_ops's maps.
549549
*/
550-
set_memory_nx((long)st_map->image, 1);
551-
set_memory_rw((long)st_map->image, 1);
550+
arch_unprotect_bpf_trampoline(st_map->image, PAGE_SIZE);
552551

553552
reset_unlock:
554553
bpf_struct_ops_map_put_progs(st_map);
@@ -616,7 +615,7 @@ static void __bpf_struct_ops_map_free(struct bpf_map *map)
616615
bpf_struct_ops_map_put_progs(st_map);
617616
bpf_map_area_free(st_map->links);
618617
if (st_map->image) {
619-
bpf_jit_free_exec(st_map->image);
618+
arch_free_bpf_trampoline(st_map->image, PAGE_SIZE);
620619
bpf_jit_uncharge_modmem(PAGE_SIZE);
621620
}
622621
bpf_map_area_free(st_map->uvalue);
@@ -691,7 +690,7 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
691690
return ERR_PTR(ret);
692691
}
693692

694-
st_map->image = bpf_jit_alloc_exec(PAGE_SIZE);
693+
st_map->image = arch_alloc_bpf_trampoline(PAGE_SIZE);
695694
if (!st_map->image) {
696695
/* __bpf_struct_ops_map_free() uses st_map->image as flag
697696
* for "charged or not". In this case, we need to unchange
@@ -711,7 +710,6 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
711710
}
712711

713712
mutex_init(&st_map->lock);
714-
set_vm_flush_reset_perms(st_map->image);
715713
bpf_map_init_from_attr(map, attr);
716714

717715
return map;

kernel/bpf/trampoline.c

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total, bool *ip_a
254254
static void bpf_tramp_image_free(struct bpf_tramp_image *im)
255255
{
256256
bpf_image_ksym_del(&im->ksym);
257-
bpf_jit_free_exec(im->image);
257+
arch_free_bpf_trampoline(im->image, PAGE_SIZE);
258258
bpf_jit_uncharge_modmem(PAGE_SIZE);
259259
percpu_ref_exit(&im->pcref);
260260
kfree_rcu(im, rcu);
@@ -365,10 +365,9 @@ static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key)
365365
goto out_free_im;
366366

367367
err = -ENOMEM;
368-
im->image = image = bpf_jit_alloc_exec(PAGE_SIZE);
368+
im->image = image = arch_alloc_bpf_trampoline(PAGE_SIZE);
369369
if (!image)
370370
goto out_uncharge;
371-
set_vm_flush_reset_perms(image);
372371

373372
err = percpu_ref_init(&im->pcref, __bpf_tramp_image_release, 0, GFP_KERNEL);
374373
if (err)
@@ -381,7 +380,7 @@ static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key)
381380
return im;
382381

383382
out_free_image:
384-
bpf_jit_free_exec(im->image);
383+
arch_free_bpf_trampoline(im->image, PAGE_SIZE);
385384
out_uncharge:
386385
bpf_jit_uncharge_modmem(PAGE_SIZE);
387386
out_free_im:
@@ -444,7 +443,7 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut
444443
if (err < 0)
445444
goto out_free;
446445

447-
set_memory_rox((long)im->image, 1);
446+
arch_protect_bpf_trampoline(im->image, PAGE_SIZE);
448447

449448
WARN_ON(tr->cur_image && total == 0);
450449
if (tr->cur_image)
@@ -465,8 +464,7 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut
465464
tr->fops->trampoline = 0;
466465

467466
/* reset im->image memory attr for arch_prepare_bpf_trampoline */
468-
set_memory_nx((long)im->image, 1);
469-
set_memory_rw((long)im->image, 1);
467+
arch_unprotect_bpf_trampoline(im->image, PAGE_SIZE);
470468
goto again;
471469
}
472470
#endif
@@ -1040,6 +1038,40 @@ arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image
10401038
return -ENOTSUPP;
10411039
}
10421040

1041+
void * __weak arch_alloc_bpf_trampoline(unsigned int size)
1042+
{
1043+
void *image;
1044+
1045+
if (WARN_ON_ONCE(size > PAGE_SIZE))
1046+
return NULL;
1047+
image = bpf_jit_alloc_exec(PAGE_SIZE);
1048+
if (image)
1049+
set_vm_flush_reset_perms(image);
1050+
return image;
1051+
}
1052+
1053+
void __weak arch_free_bpf_trampoline(void *image, unsigned int size)
1054+
{
1055+
WARN_ON_ONCE(size > PAGE_SIZE);
1056+
/* bpf_jit_free_exec doesn't need "size", but
1057+
* bpf_prog_pack_free() needs it.
1058+
*/
1059+
bpf_jit_free_exec(image);
1060+
}
1061+
1062+
void __weak arch_protect_bpf_trampoline(void *image, unsigned int size)
1063+
{
1064+
WARN_ON_ONCE(size > PAGE_SIZE);
1065+
set_memory_rox((long)image, 1);
1066+
}
1067+
1068+
void __weak arch_unprotect_bpf_trampoline(void *image, unsigned int size)
1069+
{
1070+
WARN_ON_ONCE(size > PAGE_SIZE);
1071+
set_memory_nx((long)image, 1);
1072+
set_memory_rw((long)image, 1);
1073+
}
1074+
10431075
static int __init init_trampolines(void)
10441076
{
10451077
int i;

net/bpf/bpf_dummy_struct_ops.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,11 @@ int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
101101
goto out;
102102
}
103103

104-
image = bpf_jit_alloc_exec(PAGE_SIZE);
104+
image = arch_alloc_bpf_trampoline(PAGE_SIZE);
105105
if (!image) {
106106
err = -ENOMEM;
107107
goto out;
108108
}
109-
set_vm_flush_reset_perms(image);
110109

111110
link = kzalloc(sizeof(*link), GFP_USER);
112111
if (!link) {
@@ -124,7 +123,7 @@ int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
124123
if (err < 0)
125124
goto out;
126125

127-
set_memory_rox((long)image, 1);
126+
arch_protect_bpf_trampoline(image, PAGE_SIZE);
128127
prog_ret = dummy_ops_call_op(image, args);
129128

130129
err = dummy_ops_copy_args(args);
@@ -134,7 +133,7 @@ int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
134133
err = -EFAULT;
135134
out:
136135
kfree(args);
137-
bpf_jit_free_exec(image);
136+
arch_free_bpf_trampoline(image, PAGE_SIZE);
138137
if (link)
139138
bpf_link_put(&link->link);
140139
kfree(tlinks);

0 commit comments

Comments
 (0)