Skip to content

Commit 26ef208

Browse files
liu-song-6Alexei Starovoitov
authored andcommitted
bpf: Use arch_bpf_trampoline_size
Instead of blindly allocating PAGE_SIZE for each trampoline, check the size of the trampoline with arch_bpf_trampoline_size(). This size is saved in bpf_tramp_image->size, and used for modmem charge/uncharge. The fallback arch_alloc_bpf_trampoline() still allocates a whole page because we need to use set_memory_* to protect the memory. struct_ops trampoline still uses a whole page for multiple trampolines. With this size check at caller (regular trampoline and struct_ops trampoline), remove arch_bpf_trampoline_size() from arch_prepare_bpf_trampoline() in archs. Also, update bpf_image_ksym_add() to handle symbol of different sizes. 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]> Acked-by: Björn Töpel <[email protected]> Tested-by: Björn Töpel <[email protected]> # on riscv Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 96d1b7c commit 26ef208

File tree

6 files changed

+44
-37
lines changed

6 files changed

+44
-37
lines changed

arch/arm64/net/bpf_jit_comp.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,13 +2079,6 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image,
20792079
if (nregs > 8)
20802080
return -ENOTSUPP;
20812081

2082-
ret = arch_bpf_trampoline_size(m, flags, tlinks, func_addr);
2083-
if (ret < 0)
2084-
return ret;
2085-
2086-
if (ret > ((long)image_end - (long)image))
2087-
return -EFBIG;
2088-
20892082
jit_fill_hole(image, (unsigned int)(image_end - image));
20902083
ret = prepare_trampoline(&ctx, im, tlinks, func_addr, nregs, flags);
20912084

arch/riscv/net/bpf_jit_comp64.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,13 +1052,6 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image,
10521052
int ret;
10531053
struct rv_jit_context ctx;
10541054

1055-
ret = arch_bpf_trampoline_size(im, m, flags, tlinks, func_addr);
1056-
if (ret < 0)
1057-
return ret;
1058-
1059-
if (ret > (long)image_end - (long)image)
1060-
return -EFBIG;
1061-
10621055
ctx.ninsns = 0;
10631056
/*
10641057
* The bpf_int_jit_compile() uses a RW buffer (ctx.insns) to write the

include/linux/bpf.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,7 @@ enum bpf_tramp_prog_type {
11411141

11421142
struct bpf_tramp_image {
11431143
void *image;
1144+
int size;
11441145
struct bpf_ksym ksym;
11451146
struct percpu_ref pcref;
11461147
void *ip_after_call;
@@ -1325,7 +1326,7 @@ int arch_prepare_bpf_dispatcher(void *image, void *buf, s64 *funcs, int num_func
13251326
void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, struct bpf_prog *from,
13261327
struct bpf_prog *to);
13271328
/* Called only from JIT-enabled code, so there's no need for stubs. */
1328-
void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym);
1329+
void bpf_image_ksym_add(void *data, unsigned int size, struct bpf_ksym *ksym);
13291330
void bpf_image_ksym_del(struct bpf_ksym *ksym);
13301331
void bpf_ksym_add(struct bpf_ksym *ksym);
13311332
void bpf_ksym_del(struct bpf_ksym *ksym);

kernel/bpf/bpf_struct_ops.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,20 @@ int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_links *tlinks,
355355
void *image, void *image_end)
356356
{
357357
u32 flags;
358+
int size;
358359

359360
tlinks[BPF_TRAMP_FENTRY].links[0] = link;
360361
tlinks[BPF_TRAMP_FENTRY].nr_links = 1;
361362
/* BPF_TRAMP_F_RET_FENTRY_RET is only used by bpf_struct_ops,
362363
* and it must be used alone.
363364
*/
364365
flags = model->ret_size > 0 ? BPF_TRAMP_F_RET_FENTRY_RET : 0;
366+
367+
size = arch_bpf_trampoline_size(model, flags, tlinks, NULL);
368+
if (size < 0)
369+
return size;
370+
if (size > (unsigned long)image_end - (unsigned long)image)
371+
return -E2BIG;
365372
return arch_prepare_bpf_trampoline(NULL, image, image_end,
366373
model, flags, tlinks, NULL);
367374
}

kernel/bpf/dispatcher.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, struct bpf_prog *from,
154154
d->image = NULL;
155155
goto out;
156156
}
157-
bpf_image_ksym_add(d->image, &d->ksym);
157+
bpf_image_ksym_add(d->image, PAGE_SIZE, &d->ksym);
158158
}
159159

160160
prev_num_progs = d->num_progs;

kernel/bpf/trampoline.c

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ bool bpf_prog_has_trampoline(const struct bpf_prog *prog)
115115
(ptype == BPF_PROG_TYPE_LSM && eatype == BPF_LSM_MAC);
116116
}
117117

118-
void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym)
118+
void bpf_image_ksym_add(void *data, unsigned int size, struct bpf_ksym *ksym)
119119
{
120120
ksym->start = (unsigned long) data;
121-
ksym->end = ksym->start + PAGE_SIZE;
121+
ksym->end = ksym->start + size;
122122
bpf_ksym_add(ksym);
123123
perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start,
124124
PAGE_SIZE, false, ksym->name);
@@ -254,8 +254,8 @@ 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-
arch_free_bpf_trampoline(im->image, PAGE_SIZE);
258-
bpf_jit_uncharge_modmem(PAGE_SIZE);
257+
arch_free_bpf_trampoline(im->image, im->size);
258+
bpf_jit_uncharge_modmem(im->size);
259259
percpu_ref_exit(&im->pcref);
260260
kfree_rcu(im, rcu);
261261
}
@@ -349,7 +349,7 @@ static void bpf_tramp_image_put(struct bpf_tramp_image *im)
349349
call_rcu_tasks_trace(&im->rcu, __bpf_tramp_image_put_rcu_tasks);
350350
}
351351

352-
static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key)
352+
static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, int size)
353353
{
354354
struct bpf_tramp_image *im;
355355
struct bpf_ksym *ksym;
@@ -360,12 +360,13 @@ static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key)
360360
if (!im)
361361
goto out;
362362

363-
err = bpf_jit_charge_modmem(PAGE_SIZE);
363+
err = bpf_jit_charge_modmem(size);
364364
if (err)
365365
goto out_free_im;
366+
im->size = size;
366367

367368
err = -ENOMEM;
368-
im->image = image = arch_alloc_bpf_trampoline(PAGE_SIZE);
369+
im->image = image = arch_alloc_bpf_trampoline(size);
369370
if (!image)
370371
goto out_uncharge;
371372

@@ -376,13 +377,13 @@ static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key)
376377
ksym = &im->ksym;
377378
INIT_LIST_HEAD_RCU(&ksym->lnode);
378379
snprintf(ksym->name, KSYM_NAME_LEN, "bpf_trampoline_%llu", key);
379-
bpf_image_ksym_add(image, ksym);
380+
bpf_image_ksym_add(image, size, ksym);
380381
return im;
381382

382383
out_free_image:
383-
arch_free_bpf_trampoline(im->image, PAGE_SIZE);
384+
arch_free_bpf_trampoline(im->image, im->size);
384385
out_uncharge:
385-
bpf_jit_uncharge_modmem(PAGE_SIZE);
386+
bpf_jit_uncharge_modmem(size);
386387
out_free_im:
387388
kfree(im);
388389
out:
@@ -395,7 +396,7 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut
395396
struct bpf_tramp_links *tlinks;
396397
u32 orig_flags = tr->flags;
397398
bool ip_arg = false;
398-
int err, total;
399+
int err, total, size;
399400

400401
tlinks = bpf_trampoline_get_progs(tr, &total, &ip_arg);
401402
if (IS_ERR(tlinks))
@@ -408,12 +409,6 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut
408409
goto out;
409410
}
410411

411-
im = bpf_tramp_image_alloc(tr->key);
412-
if (IS_ERR(im)) {
413-
err = PTR_ERR(im);
414-
goto out;
415-
}
416-
417412
/* clear all bits except SHARE_IPMODIFY and TAIL_CALL_CTX */
418413
tr->flags &= (BPF_TRAMP_F_SHARE_IPMODIFY | BPF_TRAMP_F_TAIL_CALL_CTX);
419414

@@ -437,13 +432,31 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut
437432
tr->flags |= BPF_TRAMP_F_ORIG_STACK;
438433
#endif
439434

440-
err = arch_prepare_bpf_trampoline(im, im->image, im->image + PAGE_SIZE,
435+
size = arch_bpf_trampoline_size(&tr->func.model, tr->flags,
436+
tlinks, tr->func.addr);
437+
if (size < 0) {
438+
err = size;
439+
goto out;
440+
}
441+
442+
if (size > PAGE_SIZE) {
443+
err = -E2BIG;
444+
goto out;
445+
}
446+
447+
im = bpf_tramp_image_alloc(tr->key, size);
448+
if (IS_ERR(im)) {
449+
err = PTR_ERR(im);
450+
goto out;
451+
}
452+
453+
err = arch_prepare_bpf_trampoline(im, im->image, im->image + size,
441454
&tr->func.model, tr->flags, tlinks,
442455
tr->func.addr);
443456
if (err < 0)
444457
goto out_free;
445458

446-
arch_protect_bpf_trampoline(im->image, PAGE_SIZE);
459+
arch_protect_bpf_trampoline(im->image, im->size);
447460

448461
WARN_ON(tr->cur_image && total == 0);
449462
if (tr->cur_image)
@@ -463,8 +476,8 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut
463476
tr->fops->func = NULL;
464477
tr->fops->trampoline = 0;
465478

466-
/* reset im->image memory attr for arch_prepare_bpf_trampoline */
467-
arch_unprotect_bpf_trampoline(im->image, PAGE_SIZE);
479+
/* free im memory and reallocate later */
480+
bpf_tramp_image_free(im);
468481
goto again;
469482
}
470483
#endif

0 commit comments

Comments
 (0)