Skip to content

Commit 15cf098

Browse files
Xu KuohaiKernel Patches Daemon
Xu Kuohai
authored and
Kernel Patches Daemon
committed
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 <[email protected]> Acked-by: Song Liu <[email protected]>
1 parent b224035 commit 15cf098

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
@@ -1922,23 +1922,6 @@ static int invoke_bpf_mod_ret(const struct btf_func_model *m, u8 **pprog,
19221922
return 0;
19231923
}
19241924

1925-
static bool is_valid_bpf_tramp_flags(unsigned int flags)
1926-
{
1927-
if ((flags & BPF_TRAMP_F_RESTORE_REGS) &&
1928-
(flags & BPF_TRAMP_F_SKIP_FRAME))
1929-
return false;
1930-
1931-
/*
1932-
* BPF_TRAMP_F_RET_FENTRY_RET is only used by bpf_struct_ops,
1933-
* and it must be used alone.
1934-
*/
1935-
if ((flags & BPF_TRAMP_F_RET_FENTRY_RET) &&
1936-
(flags & ~BPF_TRAMP_F_RET_FENTRY_RET))
1937-
return false;
1938-
1939-
return true;
1940-
}
1941-
19421925
/* Example:
19431926
* __be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev);
19441927
* its 'struct btf_func_model' will be nr_args=2
@@ -2017,9 +2000,6 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i
20172000
if (nr_args > 6)
20182001
return -ENOTSUPP;
20192002

2020-
if (!is_valid_bpf_tramp_flags(flags))
2021-
return -EINVAL;
2022-
20232003
/* Generated trampoline stack layout:
20242004
*
20252005
* RBP + 8 [ return address ]

include/linux/bpf.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,12 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *tr, void *image, void *i
760760
const struct btf_func_model *m, u32 flags,
761761
struct bpf_tramp_links *tlinks,
762762
void *orig_call);
763+
int bpf_prepare_trampoline(struct bpf_tramp_image *tr, void *image, void *image_end,
764+
const struct btf_func_model *m, u32 flags,
765+
struct bpf_tramp_links *tlinks,
766+
void *orig_call);
767+
768+
763769
/* these two functions are called from generated trampoline */
764770
u64 notrace __bpf_prog_enter(struct bpf_prog *prog, struct bpf_tramp_run_ctx *run_ctx);
765771
void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start, struct bpf_tramp_run_ctx *run_ctx);

kernel/bpf/bpf_struct_ops.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_links *tlinks,
342342
tlinks[BPF_TRAMP_FENTRY].links[0] = link;
343343
tlinks[BPF_TRAMP_FENTRY].nr_links = 1;
344344
flags = model->ret_size > 0 ? BPF_TRAMP_F_RET_FENTRY_RET : 0;
345-
return arch_prepare_bpf_trampoline(NULL, image, image_end,
346-
model, flags, tlinks, NULL);
345+
return bpf_prepare_trampoline(NULL, image, image_end, model, flags,
346+
tlinks, NULL);
347347
}
348348

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

kernel/bpf/trampoline.c

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,9 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr)
363363
if (ip_arg)
364364
flags |= BPF_TRAMP_F_IP_ARG;
365365

366-
err = arch_prepare_bpf_trampoline(im, im->image, im->image + PAGE_SIZE,
367-
&tr->func.model, flags, tlinks,
368-
tr->func.addr);
366+
err = bpf_prepare_trampoline(im, im->image, im->image + PAGE_SIZE,
367+
&tr->func.model, flags, tlinks,
368+
tr->func.addr);
369369
if (err < 0)
370370
goto out;
371371

@@ -671,6 +671,34 @@ arch_prepare_bpf_trampoline(struct bpf_tramp_image *tr, void *image, void *image
671671
return -ENOTSUPP;
672672
}
673673

674+
static bool is_valid_bpf_tramp_flags(unsigned int flags)
675+
{
676+
if ((flags & BPF_TRAMP_F_RESTORE_REGS) &&
677+
(flags & BPF_TRAMP_F_SKIP_FRAME))
678+
return false;
679+
680+
/* BPF_TRAMP_F_RET_FENTRY_RET is only used by bpf_struct_ops,
681+
* and it must be used alone.
682+
*/
683+
if ((flags & BPF_TRAMP_F_RET_FENTRY_RET) &&
684+
(flags & ~BPF_TRAMP_F_RET_FENTRY_RET))
685+
return false;
686+
687+
return true;
688+
}
689+
690+
int bpf_prepare_trampoline(struct bpf_tramp_image *tr, void *image,
691+
void *image_end, const struct btf_func_model *m,
692+
u32 flags, struct bpf_tramp_links *tlinks,
693+
void *orig_call)
694+
{
695+
if (!is_valid_bpf_tramp_flags(flags))
696+
return -EINVAL;
697+
698+
return arch_prepare_bpf_trampoline(tr, image, image_end, m, flags,
699+
tlinks, orig_call);
700+
}
701+
674702
static int __init init_trampolines(void)
675703
{
676704
int i;

0 commit comments

Comments
 (0)