Skip to content

Commit b1d18a7

Browse files
Alexei Starovoitovborkmann
authored andcommitted
bpf: Extend sys_bpf commands for bpf_syscall programs.
bpf_sycall programs can be used directly by the kernel modules to load programs and create maps via kernel skeleton. . Export bpf_sys_bpf syscall wrapper to be used in kernel skeleton. . Export bpf_map_get to be used in kernel skeleton. . Allow prog_run cmd for bpf_syscall programs with recursion check. . Enable link_create and raw_tp_open cmds. Signed-off-by: Alexei Starovoitov <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Yonghong Song <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 4f5e483 commit b1d18a7

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

kernel/bpf/syscall.c

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,7 @@ struct bpf_map *bpf_map_get(u32 ufd)
985985

986986
return map;
987987
}
988+
EXPORT_SYMBOL(bpf_map_get);
988989

989990
struct bpf_map *bpf_map_get_with_uref(u32 ufd)
990991
{
@@ -4756,23 +4757,52 @@ static bool syscall_prog_is_valid_access(int off, int size,
47564757
return true;
47574758
}
47584759

4759-
BPF_CALL_3(bpf_sys_bpf, int, cmd, void *, attr, u32, attr_size)
4760+
BPF_CALL_3(bpf_sys_bpf, int, cmd, union bpf_attr *, attr, u32, attr_size)
47604761
{
4762+
struct bpf_prog * __maybe_unused prog;
4763+
47614764
switch (cmd) {
47624765
case BPF_MAP_CREATE:
47634766
case BPF_MAP_UPDATE_ELEM:
47644767
case BPF_MAP_FREEZE:
47654768
case BPF_PROG_LOAD:
47664769
case BPF_BTF_LOAD:
4770+
case BPF_LINK_CREATE:
4771+
case BPF_RAW_TRACEPOINT_OPEN:
47674772
break;
4768-
/* case BPF_PROG_TEST_RUN:
4769-
* is not part of this list to prevent recursive test_run
4770-
*/
4773+
#ifdef CONFIG_BPF_JIT /* __bpf_prog_enter_sleepable used by trampoline and JIT */
4774+
case BPF_PROG_TEST_RUN:
4775+
if (attr->test.data_in || attr->test.data_out ||
4776+
attr->test.ctx_out || attr->test.duration ||
4777+
attr->test.repeat || attr->test.flags)
4778+
return -EINVAL;
4779+
4780+
prog = bpf_prog_get_type(attr->test.prog_fd, BPF_PROG_TYPE_SYSCALL);
4781+
if (IS_ERR(prog))
4782+
return PTR_ERR(prog);
4783+
4784+
if (attr->test.ctx_size_in < prog->aux->max_ctx_offset ||
4785+
attr->test.ctx_size_in > U16_MAX) {
4786+
bpf_prog_put(prog);
4787+
return -EINVAL;
4788+
}
4789+
4790+
if (!__bpf_prog_enter_sleepable(prog)) {
4791+
/* recursion detected */
4792+
bpf_prog_put(prog);
4793+
return -EBUSY;
4794+
}
4795+
attr->test.retval = bpf_prog_run(prog, (void *) (long) attr->test.ctx_in);
4796+
__bpf_prog_exit_sleepable(prog, 0 /* bpf_prog_run does runtime stats */);
4797+
bpf_prog_put(prog);
4798+
return 0;
4799+
#endif
47714800
default:
47724801
return -EINVAL;
47734802
}
47744803
return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size);
47754804
}
4805+
EXPORT_SYMBOL(bpf_sys_bpf);
47764806

47774807
static const struct bpf_func_proto bpf_sys_bpf_proto = {
47784808
.func = bpf_sys_bpf,

0 commit comments

Comments
 (0)