bpf: Introduce btf_tracing_ids
authorSong Liu <songliubraving@fb.com>
Fri, 12 Nov 2021 15:02:43 +0000 (07:02 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 12 Nov 2021 18:19:09 +0000 (10:19 -0800)
Similar to btf_sock_ids, btf_tracing_ids provides btf ID for task_struct,
file, and vm_area_struct via easy to understand format like
btf_tracing_ids[BTF_TRACING_TYPE_[TASK|file|VMA]].

Suggested-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20211112150243.1270987-3-songliubraving@fb.com
include/linux/btf_ids.h
kernel/bpf/bpf_task_storage.c
kernel/bpf/btf.c
kernel/bpf/stackmap.c
kernel/bpf/task_iter.c
kernel/bpf/verifier.c
kernel/trace/bpf_trace.c

index 6bb42b785293a6c0e972b7c480cafa289585df01..919c0fde1c515f1699aef52eeb5b2e170e965cf7 100644 (file)
@@ -189,6 +189,18 @@ MAX_BTF_SOCK_TYPE,
 extern u32 btf_sock_ids[];
 #endif
 
-extern u32 btf_task_struct_ids[];
+#define BTF_TRACING_TYPE_xxx   \
+       BTF_TRACING_TYPE(BTF_TRACING_TYPE_TASK, task_struct)    \
+       BTF_TRACING_TYPE(BTF_TRACING_TYPE_FILE, file)           \
+       BTF_TRACING_TYPE(BTF_TRACING_TYPE_VMA, vm_area_struct)
+
+enum {
+#define BTF_TRACING_TYPE(name, type) name,
+BTF_TRACING_TYPE_xxx
+#undef BTF_TRACING_TYPE
+MAX_BTF_TRACING_TYPE,
+};
+
+extern u32 btf_tracing_ids[];
 
 #endif
index ebfa8bc90892352354d5eec895aa3ed8440906f8..bb69aea1a7776fa7e5c1a310ff09d2145199e948 100644 (file)
@@ -323,7 +323,7 @@ const struct bpf_func_proto bpf_task_storage_get_proto = {
        .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
        .arg1_type = ARG_CONST_MAP_PTR,
        .arg2_type = ARG_PTR_TO_BTF_ID,
-       .arg2_btf_id = &btf_task_struct_ids[0],
+       .arg2_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
        .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
        .arg4_type = ARG_ANYTHING,
 };
@@ -334,5 +334,5 @@ const struct bpf_func_proto bpf_task_storage_delete_proto = {
        .ret_type = RET_INTEGER,
        .arg1_type = ARG_CONST_MAP_PTR,
        .arg2_type = ARG_PTR_TO_BTF_ID,
-       .arg2_btf_id = &btf_task_struct_ids[0],
+       .arg2_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
 };
index 2a9d8a1fee1de97a0b0bc03304565968454e7c45..6b9d23be1e992f3b48e8787bc61e1912f3de59c7 100644 (file)
@@ -6354,10 +6354,10 @@ const struct bpf_func_proto bpf_btf_find_by_name_kind_proto = {
        .arg4_type      = ARG_ANYTHING,
 };
 
-BTF_ID_LIST_GLOBAL(btf_task_struct_ids, 3)
-BTF_ID(struct, task_struct)
-BTF_ID(struct, file)
-BTF_ID(struct, vm_area_struct)
+BTF_ID_LIST_GLOBAL(btf_tracing_ids, MAX_BTF_TRACING_TYPE)
+#define BTF_TRACING_TYPE(name, type) BTF_ID(struct, type)
+BTF_TRACING_TYPE_xxx
+#undef BTF_TRACING_TYPE
 
 /* BTF ID set registration API for modules */
 
index 1de0a1b03636ec09e8ee02a74dc29f3767d4a62d..49e567209c6b6f961e6d10a27db90440213b090f 100644 (file)
@@ -489,7 +489,7 @@ const struct bpf_func_proto bpf_get_task_stack_proto = {
        .gpl_only       = false,
        .ret_type       = RET_INTEGER,
        .arg1_type      = ARG_PTR_TO_BTF_ID,
-       .arg1_btf_id    = &btf_task_struct_ids[0],
+       .arg1_btf_id    = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
        .arg2_type      = ARG_PTR_TO_UNINIT_MEM,
        .arg3_type      = ARG_CONST_SIZE_OR_ZERO,
        .arg4_type      = ARG_ANYTHING,
index f171479f7dd6b4fcd4ed34a4ad48238fd5e01402..d94696198ef89b54ec41231ae2dc2254898c9c2f 100644 (file)
@@ -622,7 +622,7 @@ const struct bpf_func_proto bpf_find_vma_proto = {
        .func           = bpf_find_vma,
        .ret_type       = RET_INTEGER,
        .arg1_type      = ARG_PTR_TO_BTF_ID,
-       .arg1_btf_id    = &btf_task_struct_ids[0],
+       .arg1_btf_id    = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
        .arg2_type      = ARG_ANYTHING,
        .arg3_type      = ARG_PTR_TO_FUNC,
        .arg4_type      = ARG_PTR_TO_STACK_OR_NULL,
@@ -652,19 +652,19 @@ static int __init task_iter_init(void)
                init_irq_work(&work->irq_work, do_mmap_read_unlock);
        }
 
-       task_reg_info.ctx_arg_info[0].btf_id = btf_task_struct_ids[0];
+       task_reg_info.ctx_arg_info[0].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_TASK];
        ret = bpf_iter_reg_target(&task_reg_info);
        if (ret)
                return ret;
 
-       task_file_reg_info.ctx_arg_info[0].btf_id = btf_task_struct_ids[0];
-       task_file_reg_info.ctx_arg_info[1].btf_id = btf_task_struct_ids[1];
+       task_file_reg_info.ctx_arg_info[0].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_TASK];
+       task_file_reg_info.ctx_arg_info[1].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_FILE];
        ret =  bpf_iter_reg_target(&task_file_reg_info);
        if (ret)
                return ret;
 
-       task_vma_reg_info.ctx_arg_info[0].btf_id = btf_task_struct_ids[0];
-       task_vma_reg_info.ctx_arg_info[1].btf_id = btf_task_struct_ids[2];
+       task_vma_reg_info.ctx_arg_info[0].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_TASK];
+       task_vma_reg_info.ctx_arg_info[1].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_VMA];
        return bpf_iter_reg_target(&task_vma_reg_info);
 }
 late_initcall(task_iter_init);
index 1aafb43f61d1c2d2b125abfc8b623127b92d89ef..d31a031ab377514e7adf49e6b34447e2537cd050 100644 (file)
@@ -6147,7 +6147,7 @@ static int set_find_vma_callback_state(struct bpf_verifier_env *env,
        callee->regs[BPF_REG_2].type = PTR_TO_BTF_ID;
        __mark_reg_known_zero(&callee->regs[BPF_REG_2]);
        callee->regs[BPF_REG_2].btf =  btf_vmlinux;
-       callee->regs[BPF_REG_2].btf_id = btf_task_struct_ids[2];
+       callee->regs[BPF_REG_2].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_VMA],
 
        /* pointer to stack or null */
        callee->regs[BPF_REG_3] = caller->regs[BPF_REG_4];
index 390176a3031abaa3034b50cd4b833b88f087da4d..25ea521fb8f1444a0050629b730386fabe8eb4cc 100644 (file)
@@ -764,7 +764,7 @@ const struct bpf_func_proto bpf_get_current_task_btf_proto = {
        .func           = bpf_get_current_task_btf,
        .gpl_only       = true,
        .ret_type       = RET_PTR_TO_BTF_ID,
-       .ret_btf_id     = &btf_task_struct_ids[0],
+       .ret_btf_id     = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
 };
 
 BPF_CALL_1(bpf_task_pt_regs, struct task_struct *, task)
@@ -779,7 +779,7 @@ const struct bpf_func_proto bpf_task_pt_regs_proto = {
        .func           = bpf_task_pt_regs,
        .gpl_only       = true,
        .arg1_type      = ARG_PTR_TO_BTF_ID,
-       .arg1_btf_id    = &btf_task_struct_ids[0],
+       .arg1_btf_id    = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
        .ret_type       = RET_PTR_TO_BTF_ID,
        .ret_btf_id     = &bpf_task_pt_regs_ids[0],
 };