Skip to content

Commit 06cedb5

Browse files
Xu KuohaiKernel Patches Daemon
authored andcommitted
bpf: Move is_valid_bpf_tramp_flags() to the public trampoline code
is_valid_bpf_tramp_flags() is not relevant to architecture, so move it to the public trampoline code. Signed-off-by: Xu Kuohai <xukuohai@huawei.com> Acked-by: Song Liu <songliubraving@fb.com>
1 parent cf59764 commit 06cedb5

File tree

4 files changed

+39
-25
lines changed

4 files changed

+39
-25
lines changed

arch/x86/net/bpf_jit_comp.c

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1900,23 +1900,6 @@ static int invoke_bpf_mod_ret(const struct btf_func_model *m, u8 **pprog,
19001900
return 0;
19011901
}
19021902

1903-
static bool is_valid_bpf_tramp_flags(unsigned int flags)
1904-
{
1905-
if ((flags & BPF_TRAMP_F_RESTORE_REGS) &&
1906-
(flags & BPF_TRAMP_F_SKIP_FRAME))
1907-
return false;
1908-
1909-
/*
1910-
* BPF_TRAMP_F_RET_FENTRY_RET is only used by bpf_struct_ops,
1911-
* and it must be used alone.
1912-
*/
1913-
if ((flags & BPF_TRAMP_F_RET_FENTRY_RET) &&
1914-
(flags & ~BPF_TRAMP_F_RET_FENTRY_RET))
1915-
return false;
1916-
1917-
return true;
1918-
}
1919-
19201903
/* Example:
19211904
* __be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev);
19221905
* its 'struct btf_func_model' will be nr_args=2
@@ -1995,9 +1978,6 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i
19951978
if (nr_args > 6)
19961979
return -ENOTSUPP;
19971980

1998-
if (!is_valid_bpf_tramp_flags(flags))
1999-
return -EINVAL;
2000-
20011981
/* Generated trampoline stack layout:
20021982
*
20031983
* RBP + 8 [ return address ]

include/linux/bpf.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,11 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *tr, void *image, void *i
706706
const struct btf_func_model *m, u32 flags,
707707
struct bpf_tramp_progs *tprogs,
708708
void *orig_call);
709+
int bpf_prepare_trampoline(struct bpf_tramp_image *tr, void *image, void *image_end,
710+
const struct btf_func_model *m, u32 flags,
711+
struct bpf_tramp_progs *tprogs,
712+
void *orig_call);
713+
709714
/* these two functions are called from generated trampoline */
710715
u64 notrace __bpf_prog_enter(struct bpf_prog *prog);
711716
void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start);

kernel/bpf/bpf_struct_ops.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,8 @@ int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_progs *tprogs,
325325
tprogs[BPF_TRAMP_FENTRY].progs[0] = prog;
326326
tprogs[BPF_TRAMP_FENTRY].nr_progs = 1;
327327
flags = model->ret_size > 0 ? BPF_TRAMP_F_RET_FENTRY_RET : 0;
328-
return arch_prepare_bpf_trampoline(NULL, image, image_end,
329-
model, flags, tprogs, NULL);
328+
return bpf_prepare_trampoline(NULL, image, image_end, model, flags,
329+
tprogs, NULL);
330330
}
331331

332332
static int bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,

kernel/bpf/trampoline.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,9 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr)
360360
if (ip_arg)
361361
flags |= BPF_TRAMP_F_IP_ARG;
362362

363-
err = arch_prepare_bpf_trampoline(im, im->image, im->image + PAGE_SIZE,
364-
&tr->func.model, flags, tprogs,
365-
tr->func.addr);
363+
err = bpf_prepare_trampoline(im, im->image, im->image + PAGE_SIZE,
364+
&tr->func.model, flags, tprogs,
365+
tr->func.addr);
366366
if (err < 0)
367367
goto out;
368368

@@ -641,6 +641,35 @@ arch_prepare_bpf_trampoline(struct bpf_tramp_image *tr, void *image, void *image
641641
return -ENOTSUPP;
642642
}
643643

644+
static bool is_valid_bpf_tramp_flags(unsigned int flags)
645+
{
646+
if ((flags & BPF_TRAMP_F_RESTORE_REGS) &&
647+
(flags & BPF_TRAMP_F_SKIP_FRAME))
648+
return false;
649+
650+
/* BPF_TRAMP_F_RET_FENTRY_RET is only used by bpf_struct_ops,
651+
* and it must be used alone.
652+
*/
653+
if ((flags & BPF_TRAMP_F_RET_FENTRY_RET) &&
654+
(flags & ~BPF_TRAMP_F_RET_FENTRY_RET))
655+
return false;
656+
657+
return true;
658+
}
659+
660+
int
661+
bpf_prepare_trampoline(struct bpf_tramp_image *tr, void *image, void *image_end,
662+
const struct btf_func_model *m, u32 flags,
663+
struct bpf_tramp_progs *tprogs,
664+
void *orig_call)
665+
{
666+
if (!is_valid_bpf_tramp_flags(flags))
667+
return -EINVAL;
668+
669+
return arch_prepare_bpf_trampoline(tr, image, image_end, m, flags,
670+
tprogs, orig_call);
671+
}
672+
644673
static int __init init_trampolines(void)
645674
{
646675
int i;

0 commit comments

Comments
 (0)