bpf: add new BPF_BTF_GET_NEXT_ID syscall command
authorQuentin Monnet <quentin.monnet@netronome.com>
Tue, 20 Aug 2019 09:31:50 +0000 (10:31 +0100)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 20 Aug 2019 16:51:06 +0000 (09:51 -0700)
Add a new command for the bpf() system call: BPF_BTF_GET_NEXT_ID is used
to cycle through all BTF objects loaded on the system.

The motivation is to be able to inspect (list) all BTF objects presents
on the system.

Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
include/linux/bpf.h
include/uapi/linux/bpf.h
kernel/bpf/btf.c
kernel/bpf/syscall.c

index 15ae498..5b9d223 100644 (file)
@@ -24,6 +24,9 @@ struct seq_file;
 struct btf;
 struct btf_type;
 
+extern struct idr btf_idr;
+extern spinlock_t btf_idr_lock;
+
 /* map is generic key/value storage optionally accesible by eBPF programs */
 struct bpf_map_ops {
        /* funcs callable from userspace (via syscall) */
index 0ef594a..8aa6126 100644 (file)
@@ -106,6 +106,7 @@ enum bpf_cmd {
        BPF_TASK_FD_QUERY,
        BPF_MAP_LOOKUP_AND_DELETE_ELEM,
        BPF_MAP_FREEZE,
+       BPF_BTF_GET_NEXT_ID,
 };
 
 enum bpf_map_type {
index 6b403dc..adb3adc 100644 (file)
             i < btf_type_vlen(struct_type);                                    \
             i++, member++)
 
-static DEFINE_IDR(btf_idr);
-static DEFINE_SPINLOCK(btf_idr_lock);
+DEFINE_IDR(btf_idr);
+DEFINE_SPINLOCK(btf_idr_lock);
 
 struct btf {
        void *data;
index cf8052b..c0f62fd 100644 (file)
@@ -2884,6 +2884,10 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, siz
                err = bpf_obj_get_next_id(&attr, uattr,
                                          &map_idr, &map_idr_lock);
                break;
+       case BPF_BTF_GET_NEXT_ID:
+               err = bpf_obj_get_next_id(&attr, uattr,
+                                         &btf_idr, &btf_idr_lock);
+               break;
        case BPF_PROG_GET_FD_BY_ID:
                err = bpf_prog_get_fd_by_id(&attr);
                break;