| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/09/02 07:09 | flow | patch-triage |
5mArgs:null Results: {
"EnableConfigs": [
"BPF_SYSCALL",
"DEBUG_INFO_BTF",
"DEBUG_INFO_BTF_MODULES",
"DEBUG_INFO_BTF_INLINE",
"SYSFS"
],
"FocusSymbols": [
"bpf_btf_load",
"btf_new_fd",
"btf_relocate",
"sysfs_btf_add"
],
"KMSANReasoning": "The patch series introduces support for inline BTF (BTF_KIND_LOC_PARAM, BTF_KIND_LOC_PROTO, and BTF_KIND_LOCSEC) along with kernel module parsing and sysfs exposure of .BTF.inline sections via /sys/kernel/btf/*.\n\nAnalysis of KMSAN vs KASAN applicability:\n- Memory Allocations and Initialization: All new kernel structures (e.g., struct btf, struct btf_module, struct bin_attribute) are allocated using kzalloc_obj(), ensuring zero-initialization. BTF payload data buffers are created using kvmemdup() from existing ELF sections.\n- User-space Data Transfers: sysfs reads (sysfs_btf_kernfs_read) copy explicitly sized BTF byte streams, with offsets and lengths strictly bounded by the acquired data_size. There are no kernel structs with padding or uninitialized fields exposed to user space.\n- Logic \u0026 Variables: All local variables in the parsing, validation, and notification paths are initialized before use.\n- Potential risks in this patch series involve bounds checking of BTF sections, offset calculations, type resolution, or memory management, all of which are addressable by KASAN, not KMSAN.\n\nTherefore, this patch series does not introduce uninitialized memory risks or info-leaks, and a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false,
"Reasoning": "The patch introduces support for inline BTF with three new BTF kinds (BTF_KIND_LOC_PARAM, BTF_KIND_LOC_PROTO, and BTF_KIND_LOCSEC), including type validation and resolution metadata checks in kernel/bpf/btf.c, split BTF relocation updates, and kernfs/sysfs interaction. These changes modify reachable core BTF verification and parsing logic reachable via the bpf(BPF_BTF_LOAD) syscall and module loading.",
"WorthFuzzing": true
} |
| 1/1 | 2026/09/02 07:09 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit f8e95b56b8cab7d6d143f7b8bfacbef2d3b9498b\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Wed Sep 2 07:09:32 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/Makefile b/Makefile\nindex 130926fa592e2..4fa15fa5d1ed1 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -1733,6 +1733,7 @@ endif # CONFIG_MODULES\n CLEAN_FILES += vmlinux.symvers modules-only.symvers \\\n \t modules.builtin modules.builtin.modinfo modules.nsdeps \\\n \t modules.builtin.ranges vmlinux.o.map vmlinux.unstripped \\\n+\t vmlinux.BTF.inline \\\n \t vmlinux.thinlto-index builtin.order \\\n \t compile_commands.json rust/test \\\n \t rust-project.json .vmlinux.objs .vmlinux.export.c \\\ndiff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h\nindex b2988aa12f664..a2e192854c47f 100644\n--- a/include/asm-generic/vmlinux.lds.h\n+++ b/include/asm-generic/vmlinux.lds.h\n@@ -675,12 +675,23 @@\n /*\n * .BTF\n */\n+#ifdef CONFIG_DEBUG_INFO_BTF_INLINE\n+#define BTF_INLINE\t\t\t\t\t\t\t\\\n+\t. = ALIGN(PAGE_SIZE);\t\t\t\t\t\t\\\n+\t.BTF.inline : AT(ADDR(.BTF.inline) - LOAD_OFFSET) {\t\t\\\n+\t\tBOUNDED_SECTION_BY(.BTF.inline, _BTF_inline)\t\t\\\n+\t}\n+#else\n+#define BTF_INLINE\n+#endif\n+\n #ifdef CONFIG_DEBUG_INFO_BTF\n #define BTF\t\t\t\t\t\t\t\t\\\n \t. = ALIGN(PAGE_SIZE);\t\t\t\t\t\t\\\n \t.BTF : AT(ADDR(.BTF) - LOAD_OFFSET) {\t\t\t\t\\\n \t\tBOUNDED_SECTION_BY(.BTF, _BTF)\t\t\t\t\\\n \t}\t\t\t\t\t\t\t\t\\\n+\tBTF_INLINE\t\t\t\t\t\t\t\\\n \t. = ALIGN(PAGE_SIZE);\t\t\t\t\t\t\\\n \t.BTF_ids : AT(ADDR(.BTF_ids) - LOAD_OFFSET) {\t\t\t\\\n \t\t*(.BTF_ids)\t\t\t\t\t\t\\\ndiff --git a/include/linux/btf.h b/include/linux/btf.h\nindex ddd0f4f32d24b..702ae246052f5 100644\n--- a/include/linux/btf.h\n+++ b/include/linux/btf.h\n@@ -329,6 +329,21 @@ static inline u64 btf_enum64_value(const struct btf_enum64 *e)\n \treturn ((u64)e-\u003eval_hi32 \u003c\u003c 32) | e-\u003eval_lo32;\n }\n \n+static inline struct btf_loc_param *btf_loc_param(const struct btf_type *t)\n+{\n+\treturn (struct btf_loc_param *)(t + 1);\n+}\n+\n+static inline __u32 *btf_loc_params(const struct btf_type *t)\n+{\n+\treturn (__u32 *)(t + 1);\n+}\n+\n+static inline struct btf_loc *btf_type_loc_secinfo(const struct btf_type *t)\n+{\n+\treturn (struct btf_loc *)(t + 1);\n+}\n+\n static inline bool btf_is_composite(const struct btf_type *t)\n {\n \tu16 kind = btf_kind(t);\n@@ -559,7 +574,7 @@ struct btf_field_desc {\n \t/* member struct size, or zero, if no members */\n \tint m_sz;\n \t/* repeated per-member offsets */\n-\tint m_off_cnt, m_offs[1];\n+\tint m_off_cnt, m_offs[2];\n };\n \n struct btf_field_iter {\n@@ -573,7 +588,8 @@ struct btf_field_iter {\n #ifdef CONFIG_BPF_SYSCALL\n const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);\n void btf_set_base_btf(struct btf *btf, const struct btf *base_btf);\n-int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **map_ids);\n+int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **map_ids,\n+\t\t __u32 **map_strs);\n int btf_field_iter_init(struct btf_field_iter *it, struct btf_type *t,\n \t\t\tenum btf_field_iter_kind iter_kind);\n __u32 *btf_field_iter_next(struct btf_field_iter *it);\n@@ -602,6 +618,7 @@ int get_kern_ctx_btf_id(struct bpf_verifier_log *log, enum bpf_prog_type prog_ty\n bool btf_types_are_same(const struct btf *btf1, u32 id1,\n \t\t\tconst struct btf *btf2, u32 id2);\n int btf_check_iter_arg(struct btf *btf, const struct btf_type *func, int arg_idx);\n+struct bin_attribute *sysfs_btf_add(const char *name, void *data, size_t data_size);\n \n static inline bool btf_type_is_struct_ptr(struct btf *btf, const struct btf_type *t)\n {\n@@ -624,7 +641,7 @@ static inline void btf_set_base_btf(struct btf *btf, const struct btf *base_btf)\n }\n \n static inline int btf_relocate(void *log, struct btf *btf, const struct btf *base_btf,\n-\t\t\t __u32 **map_ids)\n+\t\t\t __u32 **map_ids, __u32 **map_strs)\n {\n \treturn -EOPNOTSUPP;\n }\ndiff --git a/include/linux/module.h b/include/linux/module.h\nindex 7566815fabbe8..3d32c4e86f445 100644\n--- a/include/linux/module.h\n+++ b/include/linux/module.h\n@@ -507,6 +507,10 @@ struct module {\n \tvoid *btf_data;\n \tvoid *btf_base_data;\n #endif\n+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)\n+\tunsigned int btf_inline_data_size;\n+\tvoid *btf_inline_data;\n+#endif\n #ifdef CONFIG_JUMP_LABEL\n \tstruct jump_entry *jump_entries;\n \tunsigned int num_jump_entries;\ndiff --git a/include/uapi/linux/btf.h b/include/uapi/linux/btf.h\nindex 618167cab4e61..6062c99580345 100644\n--- a/include/uapi/linux/btf.h\n+++ b/include/uapi/linux/btf.h\n@@ -92,7 +92,9 @@ enum {\n \tBTF_KIND_DECL_TAG\t= 17,\t/* Decl Tag */\n \tBTF_KIND_TYPE_TAG\t= 18,\t/* Type Tag */\n \tBTF_KIND_ENUM64\t\t= 19,\t/* Enumeration up to 64-bit values */\n-\n+\tBTF_KIND_LOC_PARAM\t= 20,\t/* Location parameter information */\n+\tBTF_KIND_LOC_PROTO\t= 21,\t/* Location prototype for site */\n+\tBTF_KIND_LOCSEC\t\t= 22,\t/* Location section */\n \tNR_BTF_KINDS,\n \tBTF_KIND_MAX\t\t= NR_BTF_KINDS - 1,\n };\n@@ -212,4 +214,65 @@ struct btf_enum64 {\n \t__u32\tval_hi32;\n };\n \n+/*\n+ * BTF_KIND_LOC_PARAM is followed by a single \"struct btf_loc_param\"\n+ * that contains flags specifying the contents of the vlen-specified\n+ * number of 4-byte values that follow.\n+ */\n+struct btf_loc_param {\n+\t__u32 flags;\n+};\n+\n+/*\n+ * The combination of size, vlen and flags gives us the means to interpret\n+ * the following vlen-specified set of 4-byte values:\n+ *\n+ * - a BTF_LOC_PARAM_CONST is a constant value; combination\n+ * of size, vlen and _SIGNED flag determines it. If the value requires\n+ * 64 bits it is stored in {lo,hi} order.\n+ * - a BTF_LOC_PARAM_ADDR is an address that will be normalized with\n+ * respect to kernel base address.\n+ * - a BTF_LOC_PARAM_REG with vlen 1 is a simple register number;\n+ * with vlen 2 it is a multi-register parameter.\n+ * - a _REG | DEREF with vlen 1 dereferences the value in the register\n+ * number specified.\n+ * - a REG | DEREF | OFFSET with vlen specifies the register value in\n+ * the first 4-byte value and the offset in the remainder.\n+ * - binary logical operators operate on a combination of register\n+ * number and constant value, aside from _NOT which operates on\n+ * a register\n+ */\n+enum btf_loc_param_flags {\n+\tBTF_LOC_PARAM_SIGNED\t\t=\t0x1,\n+\tBTF_LOC_PARAM_CONST\t\t=\t0x2,\n+\tBTF_LOC_PARAM_ADDR\t\t=\t0x4,\n+\tBTF_LOC_PARAM_REG\t\t=\t0x8,\n+\tBTF_LOC_PARAM_DEREF\t\t=\t0x10,\n+\tBTF_LOC_PARAM_OFFSET\t\t=\t0x20,\n+};\n+\n+/*\n+ * BTF_KIND_LOC_PROTO specifies location prototypes; i.e. how locations relate\n+ * to parameters; a struct btf_type of BTF_KIND_LOC_PROTO is followed by a\n+ * a vlen-specified number of __u32 BTF type ids which specify the associated\n+ * BTF_KIND_LOC_PARAM for each function parameter associated with the\n+ * location. The type should either be 0 (no location info) or point at\n+ * a BTF_KIND_LOC_PARAM.\n+ */\n+\n+/*\n+ * BTF_KIND_LOCSEC consists of vlen-specified number of \"struct btf_loc\"\n+ * containing location site-specific information;\n+ *\n+ * - function (func)\n+ * - location prototype type id (loc_proto)\n+ * - address offset (offset) relative to kernel base address\n+ */\n+\n+struct btf_loc {\n+\t__u32 func;\n+\t__u32 loc_proto;\n+\t__u32 offset;\n+};\n+\n #endif /* _UAPI__LINUX_BTF_H__ */\ndiff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile\nindex 9a92c348bbda6..60aa5adb03540 100644\n--- a/kernel/bpf/Makefile\n+++ b/kernel/bpf/Makefile\n@@ -42,6 +42,7 @@ obj-$(CONFIG_BPF_SYSCALL) += reuseport_array.o\n endif\n ifeq ($(CONFIG_SYSFS),y)\n obj-$(CONFIG_DEBUG_INFO_BTF) += sysfs_btf.o\n+obj-$(CONFIG_DEBUG_INFO_BTF_INLINE) += btf_vmlinux_inline.o\n endif\n ifeq ($(CONFIG_BPF_JIT),y)\n obj-$(CONFIG_BPF_SYSCALL) += bpf_struct_ops.o\ndiff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c\nindex 9c2cab08bb794..2f3e8cea7dfcd 100644\n--- a/kernel/bpf/btf.c\n+++ b/kernel/bpf/btf.c\n@@ -8,6 +8,7 @@\n #include \u003clinux/seq_file.h\u003e\n #include \u003clinux/compiler.h\u003e\n #include \u003clinux/ctype.h\u003e\n+#include \u003clinux/delay.h\u003e\n #include \u003clinux/errno.h\u003e\n #include \u003clinux/slab.h\u003e\n #include \u003clinux/anon_inodes.h\u003e\n@@ -25,6 +26,7 @@\n #include \u003clinux/perf_event.h\u003e\n #include \u003clinux/bsearch.h\u003e\n #include \u003clinux/kobject.h\u003e\n+#include \u003clinux/kernfs.h\u003e\n #include \u003clinux/string.h\u003e\n #include \u003clinux/sysfs.h\u003e\n #include \u003clinux/overflow.h\u003e\n@@ -345,6 +347,9 @@ static const char * const btf_kind_str[NR_BTF_KINDS] = {\n \t[BTF_KIND_DECL_TAG]\t= \"DECL_TAG\",\n \t[BTF_KIND_TYPE_TAG]\t= \"TYPE_TAG\",\n \t[BTF_KIND_ENUM64]\t= \"ENUM64\",\n+\t[BTF_KIND_LOC_PARAM]\t= \"LOC_PARAM\",\n+\t[BTF_KIND_LOC_PROTO]\t= \"LOC_PROTO\",\n+\t[BTF_KIND_LOCSEC]\t= \"LOCSEC\",\n };\n \n const char *btf_type_str(const struct btf_type *t)\n@@ -517,11 +522,27 @@ static bool btf_type_is_decl_tag(const struct btf_type *t)\n \treturn BTF_INFO_KIND(t-\u003einfo) == BTF_KIND_DECL_TAG;\n }\n \n+static bool btf_type_is_loc_param(const struct btf_type *t)\n+{\n+\treturn BTF_INFO_KIND(t-\u003einfo) == BTF_KIND_LOC_PARAM;\n+}\n+\n+static bool btf_type_is_loc_proto(const struct btf_type *t)\n+{\n+\treturn BTF_INFO_KIND(t-\u003einfo) == BTF_KIND_LOC_PROTO;\n+}\n+\n+static bool btf_type_is_locsec(const struct btf_type *t)\n+{\n+\treturn BTF_INFO_KIND(t-\u003einfo) == BTF_KIND_LOCSEC;\n+}\n+\n static bool btf_type_nosize(const struct btf_type *t)\n {\n \treturn btf_type_is_void(t) || btf_type_is_fwd(t) ||\n \t btf_type_is_func(t) || btf_type_is_func_proto(t) ||\n-\t btf_type_is_decl_tag(t);\n+\t btf_type_is_decl_tag(t) || btf_type_is_loc_param(t) ||\n+\t btf_type_is_loc_proto(t) || btf_type_is_locsec(t);\n }\n \n static bool btf_type_nosize_or_null(const struct btf_type *t)\n@@ -796,7 +817,9 @@ static bool btf_type_needs_resolve(const struct btf_type *t)\n \t btf_type_is_var(t) ||\n \t btf_type_is_func(t) ||\n \t btf_type_is_decl_tag(t) ||\n-\t btf_type_is_datasec(t);\n+\t btf_type_is_datasec(t) ||\n+\t btf_type_is_loc_proto(t) ||\n+\t btf_type_is_locsec(t);\n }\n \n /* t-\u003esize can be used */\n@@ -1920,7 +1943,7 @@ void btf_set_base_btf(struct btf *btf, const struct btf *base_btf)\n {\n \tbtf-\u003ebase_btf = (struct btf *)base_btf;\n \tbtf-\u003estart_id = btf_nr_types(base_btf);\n-\tbtf-\u003estart_str_off = base_btf-\u003ehdr.str_len;\n+\tbtf-\u003estart_str_off = base_btf-\u003estart_str_off + base_btf-\u003ehdr.str_len;\n }\n \n static int env_resolve_init(struct btf_verifier_env *env)\n@@ -4714,6 +4737,213 @@ static const struct btf_kind_operations enum64_ops = {\n \t.show = btf_enum64_show,\n };\n \n+static s32 btf_loc_param_check_meta(struct btf_verifier_env *env,\n+\t\t\t\t const struct btf_type *t,\n+\t\t\t\t u32 meta_left)\n+{\n+\tconst struct btf_loc_param *p = btf_loc_param(t);\n+\tu32 meta_needed;\n+\tu32 size;\n+\n+\tmeta_needed = sizeof(*p) + sizeof(__u32) * btf_vlen(t);\n+\tif (meta_left \u003c meta_needed) {\n+\t\tbtf_verifier_log_basic(env, t,\n+\t\t\t\t \"meta_left:%u meta_needed:%u\",\n+\t\t\t\t meta_left, meta_needed);\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tif (t-\u003ename_off) {\n+\t\tbtf_verifier_log_type(env, t, \"Invalid name\");\n+\t\treturn -EINVAL;\n+\t}\n+\tsize = t-\u003esize;\n+\tif (size \u003e 16 || !is_power_of_2(size)) {\n+\t\tbtf_verifier_log_type(env, t, \"Unexpected size\");\n+\t\treturn -EINVAL;\n+\t}\n+\n+\treturn meta_needed;\n+}\n+\n+static void btf_loc_param_log(struct btf_verifier_env *env,\n+\t\t\t const struct btf_type *t)\n+{\n+\tconst struct btf_loc_param *p = btf_loc_param(t);\n+\tu32 *d = (u32 *)(p + 1);\n+\tu32 i, vlen = btf_vlen(t);\n+\n+\tbtf_verifier_log(env, \"size=%u flags=0x%x param_data(\", t-\u003esize, p-\u003eflags);\n+\tfor (i = 0; i \u003c vlen; i++, d++)\n+\t\tbtf_verifier_log(env, \"%u%s\", *d,\n+\t\t\t\t i + 1 == vlen ? \")\" : \", \");\n+}\n+\n+static const struct btf_kind_operations loc_param_ops = {\n+\t.check_meta = btf_loc_param_check_meta,\n+\t.resolve = btf_df_resolve,\n+\t.check_member = btf_df_check_member,\n+\t.check_kflag_member = btf_df_check_kflag_member,\n+\t.log_details = btf_loc_param_log,\n+\t.show = btf_df_show,\n+};\n+\n+static s32 btf_loc_proto_check_meta(struct btf_verifier_env *env,\n+\t\t\t\t const struct btf_type *t,\n+\t\t\t\t u32 meta_left)\n+{\n+\tu32 meta_needed;\n+\n+\tmeta_needed = sizeof(__u32) * btf_type_vlen(t);\n+\n+\tif (meta_left \u003c meta_needed) {\n+\t\tbtf_verifier_log_basic(env, t,\n+\t\t\t\t \"meta_left:%u meta_needed:%u\",\n+\t\t\t\t meta_left, meta_needed);\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tif (t-\u003ename_off) {\n+\t\tbtf_verifier_log_type(env, t, \"Invalid name\");\n+\t\treturn -EINVAL;\n+\t}\n+\treturn meta_needed;\n+}\n+\n+static void btf_loc_proto_log(struct btf_verifier_env *env,\n+\t\t\t const struct btf_type *t)\n+{\n+\tconst __u32 *params = btf_loc_params(t);\n+\tu32 nr_params = btf_type_vlen(t), i;\n+\n+\tbtf_verifier_log(env, \"loc_proto locs=(\");\n+\tfor (i = 0; i \u003c nr_params; i++, params++) {\n+\t\tbtf_verifier_log(env, \"type=%u%s\", *params,\n+\t\t\t\t i + 1 == nr_params ? \")\" : \", \");\n+\t}\n+}\n+\n+static int btf_loc_proto_resolve(struct btf_verifier_env *env,\n+\t\t\t\t const struct resolve_vertex *v)\n+{\n+\tconst __u32 *params = btf_loc_params(v-\u003et);\n+\tu32 i, nr_params = btf_type_vlen(v-\u003et);\n+\tstruct btf *btf = env-\u003ebtf;\n+\n+\tfor (i = 0; i \u003c nr_params; i++) {\n+\t\tconst struct btf_type *param_type;\n+\t\tu32 param_type_id = params[i];\n+\n+\t\tif (!param_type_id)\n+\t\t\tcontinue;\n+\n+\t\tparam_type = btf_type_by_id(btf, param_type_id);\n+\t\tif (!param_type || !btf_type_is_loc_param(param_type)) {\n+\t\t\tbtf_verifier_log_type(env, v-\u003et,\n+\t\t\t\t\t \"Invalid loc_param#%u\", i + 1);\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\t}\n+\n+\tenv_stack_pop_resolved(env, 0, 0);\n+\treturn 0;\n+}\n+\n+static const struct btf_kind_operations loc_proto_ops = {\n+\t.check_meta = btf_loc_proto_check_meta,\n+\t.resolve = btf_loc_proto_resolve,\n+\t.check_member = btf_df_check_member,\n+\t.check_kflag_member = btf_df_check_kflag_member,\n+\t.log_details = btf_loc_proto_log,\n+\t.show = btf_df_show,\n+};\n+\n+static s32 btf_locsec_check_meta(struct btf_verifier_env *env,\n+\t\t\t\t const struct btf_type *t,\n+\t\t\t\t u32 meta_left)\n+{\n+\tu32 meta_needed;\n+\n+\tmeta_needed = sizeof(struct btf_loc) * btf_type_vlen(t);\n+\n+\tif (meta_left \u003c meta_needed) {\n+\t\tbtf_verifier_log_basic(env, t,\n+\t\t\t\t \"meta_left:%u meta_needed:%u\",\n+\t\t\t\t meta_left, meta_needed);\n+\t\treturn -EINVAL;\n+\t}\n+\treturn meta_needed;\n+}\n+\n+static void btf_locsec_log(struct btf_verifier_env *env,\n+\t\t\t const struct btf_type *t)\n+{\n+\tconst struct btf_loc *loc = btf_type_loc_secinfo(t);\n+\tu32 nr_locs = btf_type_vlen(t), i;\n+\tconst struct btf *btf = env-\u003ebtf;\n+\n+\tbtf_verifier_log(env, \"locsec %s locs=(\",\n+\t\t\t __btf_name_by_offset(btf, t-\u003ename_off));\n+\tfor (i = 0; i \u003c nr_locs; i++, loc++) {\n+\t\tbtf_verifier_log(env, \"\\n\\tfunc %u loc_proto %u offset 0x%x%s\",\n+\t\t\t\t loc-\u003efunc, loc-\u003eloc_proto, loc-\u003eoffset,\n+\t\t\t\t i + 1 == nr_locs ? \")\" : \", \");\n+\t}\n+}\n+\n+static int btf_locsec_resolve(struct btf_verifier_env *env,\n+\t\t\t const struct resolve_vertex *v)\n+{\n+\tconst struct btf_loc *loc;\n+\tstruct btf *btf = env-\u003ebtf;\n+\tu32 i;\n+\n+\tenv-\u003eresolve_mode = RESOLVE_TBD;\n+\tfor (i = v-\u003enext_member, loc = btf_type_loc_secinfo(v-\u003et) + i;\n+\t i \u003c btf_type_vlen(v-\u003et); i++, loc++) {\n+\t\tconst struct btf_type *func_type, *loc_proto_type;\n+\t\tu32 func_type_id = loc-\u003efunc;\n+\t\tu32 loc_proto_type_id = loc-\u003eloc_proto;\n+\n+\t\tfunc_type = btf_type_by_id(btf, func_type_id);\n+\t\tif (!func_type || !btf_type_is_func(func_type)) {\n+\t\t\tbtf_verifier_log_type(env, v-\u003et,\n+\t\t\t\t\t \"Invalid func#%u\", i + 1);\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\n+\t\tif (!env_type_is_resolved(env, func_type_id)) {\n+\t\t\tenv_stack_set_next_member(env, i);\n+\t\t\treturn env_stack_push(env, func_type, func_type_id);\n+\t\t}\n+\n+\t\tloc_proto_type = btf_type_by_id(btf, loc_proto_type_id);\n+\t\tif (!loc_proto_type || !btf_type_is_loc_proto(loc_proto_type)) {\n+\t\t\tbtf_verifier_log_type(env, v-\u003et,\n+\t\t\t\t\t \"Invalid loc_proto#%u\", i + 1);\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\n+\t\tif (!env_type_is_resolved(env, loc_proto_type_id)) {\n+\t\t\tenv_stack_set_next_member(env, i + 1);\n+\t\t\treturn env_stack_push(env, loc_proto_type,\n+\t\t\t\t\t loc_proto_type_id);\n+\t\t}\n+\t}\n+\n+\tenv_stack_pop_resolved(env, 0, 0);\n+\treturn 0;\n+}\n+\n+static const struct btf_kind_operations locsec_ops = {\n+\t.check_meta = btf_locsec_check_meta,\n+\t.resolve = btf_locsec_resolve,\n+\t.check_member = btf_df_check_member,\n+\t.check_kflag_member = btf_df_check_kflag_member,\n+\t.log_details = btf_locsec_log,\n+\t.show = btf_df_show,\n+};\n+\n static s32 btf_func_proto_check_meta(struct btf_verifier_env *env,\n \t\t\t\t const struct btf_type *t,\n \t\t\t\t u32 meta_left)\n@@ -5383,6 +5613,9 @@ static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = {\n \t[BTF_KIND_DECL_TAG] = \u0026decl_tag_ops,\n \t[BTF_KIND_TYPE_TAG] = \u0026modifier_ops,\n \t[BTF_KIND_ENUM64] = \u0026enum64_ops,\n+\t[BTF_KIND_LOC_PARAM] = \u0026loc_param_ops,\n+\t[BTF_KIND_LOC_PROTO] = \u0026loc_proto_ops,\n+\t[BTF_KIND_LOCSEC] = \u0026locsec_ops,\n };\n \n static s32 btf_check_meta(struct btf_verifier_env *env,\n@@ -5457,7 +5690,8 @@ static bool btf_resolve_valid(struct btf_verifier_env *env,\n \tif (!env_type_is_resolved(env, type_id))\n \t\treturn false;\n \n-\tif (btf_type_is_struct(t) || btf_type_is_datasec(t))\n+\tif (btf_type_is_struct(t) || btf_type_is_datasec(t) ||\n+\t btf_type_is_loc_proto(t) || btf_type_is_locsec(t))\n \t\treturn !btf_resolved_type_id(btf, type_id) \u0026\u0026\n \t\t !btf_resolved_type_size(btf, type_id);\n \n@@ -6493,15 +6727,140 @@ __u32 btf_relocate_id(const struct btf *btf, __u32 id)\n \n #ifdef CONFIG_DEBUG_INFO_BTF_MODULES\n \n+static struct btf *btf_parse_split(struct btf_verifier_env *env, const char *name,\n+\t\t\t\t const void *data, unsigned int data_size,\n+\t\t\t\t struct btf *base_btf)\n+{\n+\tstruct btf *btf;\n+\tint err;\n+\n+\tbtf = kzalloc_obj(*btf, GFP_KERNEL | __GFP_NOWARN);\n+\tif (!btf)\n+\t\treturn ERR_PTR(-ENOMEM);\n+\tenv-\u003ebtf = btf;\n+\n+\tbtf_set_base_btf(btf, base_btf);\n+\tbtf-\u003ekernel_btf = true;\n+\tbtf-\u003enamed_start_id = 0;\n+\tstrscpy(btf-\u003ename, name);\n+\n+\tbtf-\u003edata = kvmemdup(data, data_size, GFP_KERNEL | __GFP_NOWARN);\n+\tif (!btf-\u003edata) {\n+\t\terr = -ENOMEM;\n+\t\tgoto errout;\n+\t}\n+\tbtf-\u003edata_size = data_size;\n+\n+\terr = btf_parse_hdr(env);\n+\tif (err)\n+\t\tgoto errout;\n+\n+\tbtf-\u003enohdr_data = btf-\u003edata + btf-\u003ehdr.hdr_len;\n+\n+\terr = btf_parse_str_sec(env);\n+\tif (err)\n+\t\tgoto errout;\n+\n+\terr = btf_check_all_metas(env);\n+\tif (err)\n+\t\tgoto errout;\n+\n+\terr = btf_check_modifier_chain_length(env, btf, btf_nr_types(base_btf));\n+\tif (err)\n+\t\tgoto errout;\n+\n+\treturn btf;\n+\n+errout:\n+\tbtf_free(btf);\n+\treturn ERR_PTR(err);\n+}\n+\n+static int btf_rebase_inline(struct btf *inline_btf, const struct btf *module_btf,\n+\t\t\t const u32 *module_id_map, const u32 *module_str_map,\n+\t\t\t u32 old_module_type_cnt)\n+{\n+\tu32 old_start_id = inline_btf-\u003estart_id;\n+\tu32 old_start_str_off = inline_btf-\u003estart_str_off;\n+\tu32 old_module_start_str_off = old_start_str_off - module_btf-\u003ehdr.str_len;\n+\tu32 new_start_id = btf_nr_types(module_btf);\n+\tu32 new_start_str_off = module_btf-\u003estart_str_off + module_btf-\u003ehdr.str_len;\n+\ts64 id_delta = (s64)new_start_id - old_start_id;\n+\ts64 str_delta = (s64)new_start_str_off - old_start_str_off;\n+\tu32 i;\n+\n+\t/*\n+\t * The inline BTF was parsed relative to the original module BTF. Its\n+\t * base IDs must therefore use the map generated when that BTF was\n+\t * relocated, while IDs for inline-local types only move by the change\n+\t * in the module BTF's starting ID.\n+\t */\n+\tfor (i = 0; i \u003c inline_btf-\u003enr_types; i++) {\n+\t\tstruct btf_field_iter it;\n+\t\tstruct btf_type *t = inline_btf-\u003etypes[i];\n+\t\tu32 *id, *str_off;\n+\t\tint err;\n+\n+\t\terr = btf_field_iter_init(\u0026it, t, BTF_FIELD_ITER_IDS);\n+\t\tif (err)\n+\t\t\treturn err;\n+\t\twhile ((id = btf_field_iter_next(\u0026it))) {\n+\t\t\tif (!*id)\n+\t\t\t\tcontinue;\n+\t\t\tif (*id \u003c old_module_type_cnt) {\n+\t\t\t\tif (module_id_map)\n+\t\t\t\t\t*id = module_id_map[*id];\n+\t\t\t} else if (*id \u003e= old_start_id) {\n+\t\t\t\t*id += id_delta;\n+\t\t\t} else {\n+\t\t\t\treturn -EINVAL;\n+\t\t\t}\n+\t\t}\n+\n+\t\terr = btf_field_iter_init(\u0026it, t, BTF_FIELD_ITER_STRS);\n+\t\tif (err)\n+\t\t\treturn err;\n+\t\twhile ((str_off = btf_field_iter_next(\u0026it))) {\n+\t\t\tif (!*str_off)\n+\t\t\t\tcontinue;\n+\t\t\t/*\n+\t\t\t * LOCSEC names its code section in the module BTF's string\n+\t\t\t * section. Shift those inherited strings together with\n+\t\t\t * inline-local strings when replacing a distilled base BTF.\n+\t\t\t */\n+\t\t\tif (*str_off \u003c old_module_start_str_off) {\n+\t\t\t\t/* vmlinux strings retain their offsets for in-tree modules. */\n+\t\t\t\tif (!module_id_map)\n+\t\t\t\t\tcontinue;\n+\t\t\t\tif (!module_str_map || !module_str_map[*str_off])\n+\t\t\t\t\treturn -EINVAL;\n+\t\t\t\t*str_off = module_str_map[*str_off];\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\t\t\t*str_off += str_delta;\n+\t\t}\n+\t}\n+\n+\tbtf_set_base_btf(inline_btf, module_btf);\n+\tbtf_check_sorted(inline_btf);\n+\treturn 0;\n+}\n+\n static struct btf *btf_parse_module(const char *module_name, const void *data,\n \t\t\t\t unsigned int data_size, void *base_data,\n-\t\t\t\t unsigned int base_data_size)\n+\t\t\t\t unsigned int base_data_size, const void *inline_data,\n+\t\t\t\t unsigned int inline_data_size, bool vmlinux_inline,\n+\t\t\t\t void **relocated_inline_data)\n {\n-\tstruct btf *btf = NULL, *vmlinux_btf, *base_btf = NULL;\n+\tstruct btf *btf = NULL, *inline_btf = NULL, *vmlinux_btf, *base_btf = NULL;\n+\tstruct btf *inline_base_btf;\n \tstruct btf_verifier_env *env = NULL;\n \tstruct bpf_verifier_log *log;\n+\tu32 old_module_type_cnt;\n+\tu32 *module_str_map = NULL;\n \tint err = 0;\n \n+\t*relocated_inline_data = NULL;\n \tvmlinux_btf = bpf_get_btf_vmlinux();\n \tif (IS_ERR(vmlinux_btf))\n \t\treturn vmlinux_btf;\n@@ -6525,67 +6884,75 @@ static struct btf *btf_parse_module(const char *module_name, const void *data,\n \t\tbase_btf = vmlinux_btf;\n \t}\n \n-\tbtf = kzalloc_obj(*btf, GFP_KERNEL | __GFP_NOWARN);\n-\tif (!btf) {\n-\t\terr = -ENOMEM;\n+\tbtf = btf_parse_split(env, module_name, data, data_size, base_btf);\n+\tif (IS_ERR(btf)) {\n+\t\terr = PTR_ERR(btf);\n+\t\tbtf = NULL;\n \t\tgoto errout;\n \t}\n-\tenv-\u003ebtf = btf;\n \n-\tbtf-\u003ebase_btf = base_btf;\n-\tbtf-\u003estart_id = base_btf-\u003enr_types;\n-\tbtf-\u003estart_str_off = base_btf-\u003ehdr.str_len;\n-\tbtf-\u003ekernel_btf = true;\n-\tbtf-\u003enamed_start_id = 0;\n-\tstrscpy(btf-\u003ename, module_name);\n-\n-\tbtf-\u003edata = kvmemdup(data, data_size, GFP_KERNEL | __GFP_NOWARN);\n-\tif (!btf-\u003edata) {\n-\t\terr = -ENOMEM;\n-\t\tgoto errout;\n+\tif (inline_data_size) {\n+\t\t/*\n+\t\t * Ordinary module inline BTF is split relative to the module BTF.\n+\t\t * The btf_vmlinux_inline delivery module instead carries BTF split\n+\t\t * directly relative to vmlinux.\n+\t\t */\n+\t\tinline_base_btf = vmlinux_inline ? vmlinux_btf : btf;\n+\t\tinline_btf = btf_parse_split(env, module_name, inline_data,\n+\t\t\t\t\t inline_data_size, inline_base_btf);\n+\t\tif (IS_ERR(inline_btf)) {\n+\t\t\tpr_warn(\"failed to validate module [%s] inline BTF: %ld\\n\",\n+\t\t\t\tmodule_name, PTR_ERR(inline_btf));\n+\t\t\tinline_btf = NULL;\n+\t\t}\n \t}\n-\tbtf-\u003edata_size = data_size;\n-\n-\terr = btf_parse_hdr(env);\n-\tif (err)\n-\t\tgoto errout;\n-\n-\tbtf-\u003enohdr_data = btf-\u003edata + btf-\u003ehdr.hdr_len;\n-\n-\terr = btf_parse_str_sec(env);\n-\tif (err)\n-\t\tgoto errout;\n-\n-\terr = btf_check_all_metas(env);\n-\tif (err)\n-\t\tgoto errout;\n-\n-\terr = btf_check_modifier_chain_length(env, btf, btf_nr_types(base_btf));\n-\tif (err)\n-\t\tgoto errout;\n \n+\told_module_type_cnt = btf_nr_types(btf);\n \tif (base_btf != vmlinux_btf) {\n-\t\terr = btf_relocate(btf, vmlinux_btf, \u0026btf-\u003ebase_id_map);\n+\t\terr = btf_relocate(btf, vmlinux_btf, \u0026btf-\u003ebase_id_map, \u0026module_str_map);\n \t\tif (err)\n \t\t\tgoto errout;\n \t\tbtf_free(base_btf);\n \t\tbase_btf = vmlinux_btf;\n \t}\n \n-\tbtf_verifier_env_free(env);\n+\tif (inline_btf) {\n+\t\tif (!vmlinux_inline) {\n+\t\t\terr = btf_rebase_inline(inline_btf, btf, btf-\u003ebase_id_map,\n+\t\t\t\t\t\tmodule_str_map, old_module_type_cnt);\n+\t\t\tif (err) {\n+\t\t\t\tpr_warn(\"failed to relocate module [%s] inline BTF: %d\\n\",\n+\t\t\t\t\tmodule_name, err);\n+\t\t\t\tbtf_free(inline_btf);\n+\t\t\t\tinline_btf = NULL;\n+\t\t\t}\n+\t\t}\n+\t\tif (inline_btf) {\n+\t\t\t*relocated_inline_data = inline_btf-\u003edata;\n+\t\t\tinline_btf-\u003edata = NULL;\n+\t\t\tbtf_free(inline_btf);\n+\t\t}\n+\t}\n+\n+\t/*\n+\t * With a distilled base, btf_relocate() replaces the base BTF and\n+\t * rewrites string offsets. Check ordering only after that final BTF\n+\t * view has been established, so named_start_id describes the BTF used\n+\t * by name lookups.\n+\t */\n \tbtf_check_sorted(btf);\n+\tbtf_verifier_env_free(env);\n+\tkvfree(module_str_map);\n \trefcount_set(\u0026btf-\u003erefcnt, 1);\n \treturn btf;\n \n errout:\n+\tkvfree(module_str_map);\n \tbtf_verifier_env_free(env);\n+\tbtf_free(inline_btf);\n \tif (!IS_ERR(base_btf) \u0026\u0026 base_btf != vmlinux_btf)\n \t\tbtf_free(base_btf);\n-\tif (btf) {\n-\t\tkvfree(btf-\u003edata);\n-\t\tkvfree(btf-\u003etypes);\n-\t\tkfree(btf);\n-\t}\n+\tbtf_free(btf);\n \treturn ERR_PTR(err);\n }\n \n@@ -8503,12 +8870,163 @@ enum {\n \tBTF_MODULE_F_LIVE = (1 \u003c\u003c 0),\n };\n \n+#if IS_ENABLED(CONFIG_SYSFS)\n+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)\n+static struct bin_attribute *vmlinux_inline_attr;\n+#endif\n+\n+static int sysfs_btf_bin_attr_load(struct bin_attribute *attr)\n+{\n+\tchar modname[MODULE_NAME_LEN + sizeof(\"btf_vmlinux_inline\")];\n+\tint retries = 0;\n+\n+\t/* First on-demand read; load module. */\n+\tsnprintf(modname, sizeof(modname), \"btf_%s\", attr-\u003eattr.name);\n+\tstrreplace(modname, '.', '_');\n+\trequest_module(\"%s\", modname);\n+\n+\t/*\n+\t * request_module() is synchronous, but the module notifier is\n+\t * responsible for updating private data, so retries are required.\n+\t */\n+\twhile (retries++ \u003c 10) {\n+\t\tif (smp_load_acquire(\u0026attr-\u003esize))\n+\t\t\treturn 0;\n+\t\tudelay(50);\n+\t}\n+\treturn -ENODEV;\n+}\n+\n+static int sysfs_btf_kernfs_open(struct kernfs_open_file *of)\n+{\n+\tstruct bin_attribute *attr = of-\u003ekn-\u003epriv;\n+\tsize_t data_size;\n+\tint err;\n+\n+\tif (!smp_load_acquire(\u0026attr-\u003esize)) {\n+\t\terr = sysfs_btf_bin_attr_load(attr);\n+\t\tif (err)\n+\t\t\treturn err;\n+\t}\n+\t/* Refresh file size or the open() caller will not see updated size. */\n+\tdata_size = smp_load_acquire(\u0026attr-\u003esize);\n+\tof-\u003ekn-\u003eattr.size = data_size;\n+\tif (of-\u003efile) {\n+\t\tstruct inode *inode = file_inode(of-\u003efile);\n+\n+\t\tif (inode)\n+\t\t\ti_size_write(inode, data_size);\n+\t}\n+\treturn 0;\n+}\n+\n+static ssize_t sysfs_btf_kernfs_read(struct kernfs_open_file *of, char *buf,\n+\t\t\t\t size_t bytes_requested, loff_t offset)\n+{\n+\tstruct bin_attribute *attr = of-\u003ekn-\u003epriv;\n+\tvoid *data;\n+\tsize_t data_size;\n+\n+\tdata_size = smp_load_acquire(\u0026attr-\u003esize);\n+\tif (offset \u003e= data_size)\n+\t\treturn 0;\n+\tif (offset + bytes_requested \u003e data_size)\n+\t\tbytes_requested = data_size - offset;\n+\tdata = READ_ONCE(attr-\u003eprivate);\n+\tmemcpy(buf, data + offset, bytes_requested);\n+\n+\treturn bytes_requested;\n+}\n+\n+static const struct kernfs_ops sysfs_btf_kernfs_ops = {\n+\t.open = sysfs_btf_kernfs_open,\n+\t.read = sysfs_btf_kernfs_read,\n+};\n+\n+struct bin_attribute *sysfs_btf_add(const char *name, void *data, size_t data_size)\n+{\n+\tstruct bin_attribute *attr;\n+\tint err = 0;\n+\n+\tattr = kzalloc_obj(*attr);\n+\tif (!attr)\n+\t\treturn ERR_PTR(-ENOMEM);\n+\n+\tsysfs_bin_attr_init(attr);\n+\tattr-\u003eattr.mode = 0444;\n+\tattr-\u003esize = data_size;\n+\tattr-\u003eprivate = data;\n+\tattr-\u003eread = sysfs_bin_attr_simple_read;\n+\tattr-\u003eattr.name = kstrdup(name, GFP_KERNEL);\n+\tif (!attr-\u003eattr.name) {\n+\t\terr = -ENOMEM;\n+\t\tgoto err_free;\n+\t}\n+\tif (data_size \u003e 0) {\n+\t\terr = sysfs_create_bin_file(btf_kobj, attr);\n+\t} else {\n+\t\tstruct kernfs_node *node;\n+\n+\t\tnode = __kernfs_create_file(btf_kobj-\u003esd, attr-\u003eattr.name,\n+\t\t\t\t\t attr-\u003eattr.mode, GLOBAL_ROOT_UID,\n+\t\t\t\t\t GLOBAL_ROOT_GID, data_size,\n+\t\t\t\t\t \u0026sysfs_btf_kernfs_ops, attr, NULL, NULL);\n+\t\tif (IS_ERR(node))\n+\t\t\terr = PTR_ERR(node);\n+\t}\n+\tif (err) {\n+\t\tpr_warn(\"failed to register [%s] BTF in sysfs: %d\\n\", name, err);\n+\t\tgoto err_free;\n+\t}\n+\treturn attr;\n+\n+err_free:\n+\tkfree(attr-\u003eattr.name);\n+\tkfree(attr);\n+\treturn ERR_PTR(err);\n+}\n+\n+#else\n+struct bin_attribute *sysfs_btf_add(const char *name, void *data, size_t data_size)\n+{\n+\treturn NULL;\n+}\n+#endif\n+\n+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)\n+static void sysfs_btf_update(struct bin_attribute *attr, void *data, size_t data_size)\n+{\n+\tif (!attr)\n+\t\treturn;\n+\tWRITE_ONCE(attr-\u003eprivate, data);\n+\t/* Publish data before its non-zero size makes it readable. */\n+\tsmp_store_release(\u0026attr-\u003esize, data_size);\n+}\n+#endif\n+\n #ifdef CONFIG_DEBUG_INFO_BTF_MODULES\n+#if IS_ENABLED(CONFIG_SYSFS)\n+static void sysfs_btf_remove(struct bin_attribute *attr)\n+{\n+\tsysfs_remove_bin_file(btf_kobj, attr);\n+\tkfree(attr-\u003eattr.name);\n+\tkfree(attr);\n+}\n+#else\n+static void sysfs_btf_remove(struct bin_attribute *attr)\n+{\n+}\n+#endif\n+\n struct btf_module {\n \tstruct list_head list;\n \tstruct module *module;\n \tstruct btf *btf;\n \tstruct bin_attribute *sysfs_attr;\n+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)\n+\tstruct bin_attribute *sysfs_inline_attr;\n+\tvoid *btf_inline_data;\n+#endif\n \tint flags;\n };\n \n@@ -8522,7 +9040,10 @@ static int btf_module_notify(struct notifier_block *nb, unsigned long op,\n {\n \tstruct btf_module *btf_mod, *tmp;\n \tstruct module *mod = module;\n+\tstruct bin_attribute *attr;\n \tstruct btf *btf;\n+\tvoid *inline_data = NULL, *relocated_inline_data = NULL;\n+\tunsigned int inline_data_size = 0;\n \tint err = 0;\n \n \tif (mod-\u003ebtf_data_size == 0 ||\n@@ -8532,13 +9053,20 @@ static int btf_module_notify(struct notifier_block *nb, unsigned long op,\n \n \tswitch (op) {\n \tcase MODULE_STATE_COMING:\n+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)\n+\t\tinline_data = mod-\u003ebtf_inline_data;\n+\t\tinline_data_size = mod-\u003ebtf_inline_data_size;\n+#endif\n \t\tbtf_mod = kzalloc_obj(*btf_mod);\n \t\tif (!btf_mod) {\n \t\t\terr = -ENOMEM;\n \t\t\tgoto out;\n \t\t}\n \t\tbtf = btf_parse_module(mod-\u003ename, mod-\u003ebtf_data, mod-\u003ebtf_data_size,\n-\t\t\t\t mod-\u003ebtf_base_data, mod-\u003ebtf_base_data_size);\n+\t\t\t\t mod-\u003ebtf_base_data, mod-\u003ebtf_base_data_size,\n+\t\t\t\t inline_data, inline_data_size,\n+\t\t\t\t strcmp(mod-\u003ename, \"btf_vmlinux_inline\") == 0,\n+\t\t\t\t \u0026relocated_inline_data);\n \t\tif (IS_ERR(btf)) {\n \t\t\tkfree(btf_mod);\n \t\t\tif (!IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) {\n@@ -8553,6 +9081,7 @@ static int btf_module_notify(struct notifier_block *nb, unsigned long op,\n \t\terr = btf_alloc_id(btf);\n \t\tif (err) {\n \t\t\tbtf_free(btf);\n+\t\t\tkvfree(relocated_inline_data);\n \t\t\tkfree(btf_mod);\n \t\t\tgoto out;\n \t\t}\n@@ -8561,34 +9090,43 @@ static int btf_module_notify(struct notifier_block *nb, unsigned long op,\n \t\tmutex_lock(\u0026btf_module_mutex);\n \t\tbtf_mod-\u003emodule = module;\n \t\tbtf_mod-\u003ebtf = btf;\n+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)\n+\t\tbtf_mod-\u003ebtf_inline_data = relocated_inline_data;\n+#endif\n \t\tlist_add(\u0026btf_mod-\u003elist, \u0026btf_modules);\n \t\tmutex_unlock(\u0026btf_module_mutex);\n \n-\t\tif (IS_ENABLED(CONFIG_SYSFS)) {\n-\t\t\tstruct bin_attribute *attr;\n-\n-\t\t\tattr = kzalloc_obj(*attr);\n-\t\t\tif (!attr)\n-\t\t\t\tgoto out;\n+\t\tattr = sysfs_btf_add(btf-\u003ename, btf-\u003edata, btf-\u003edata_size);\n+\t\tif (IS_ERR(attr)) {\n+\t\t\terr = 0;\n+\t\t\tgoto out;\n+\t\t}\n+\t\tbtf_mod-\u003esysfs_attr = attr;\n \n-\t\t\tsysfs_bin_attr_init(attr);\n-\t\t\tattr-\u003eattr.name = btf-\u003ename;\n-\t\t\tattr-\u003eattr.mode = 0444;\n-\t\t\tattr-\u003esize = btf-\u003edata_size;\n-\t\t\tattr-\u003eprivate = btf-\u003edata;\n-\t\t\tattr-\u003eread = sysfs_bin_attr_simple_read;\n+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)\n+\t\tif (relocated_inline_data) {\n+\t\t\tchar name[MODULE_NAME_LEN + sizeof(\".inline\")];\n \n-\t\t\terr = sysfs_create_bin_file(btf_kobj, attr);\n-\t\t\tif (err) {\n-\t\t\t\tpr_warn(\"failed to register module [%s] BTF in sysfs: %d\\n\",\n-\t\t\t\t\tmod-\u003ename, err);\n-\t\t\t\tkfree(attr);\n+\t\t\tif (strcmp(mod-\u003ename, \"btf_vmlinux_inline\") == 0) {\n+\t\t\t\tif (vmlinux_inline_attr)\n+\t\t\t\t\tsysfs_btf_update(vmlinux_inline_attr,\n+\t\t\t\t\t\t\t relocated_inline_data, inline_data_size);\n+\t\t\t\telse\n+\t\t\t\t\tkvfree(relocated_inline_data);\n+\t\t\t\tbtf_mod-\u003ebtf_inline_data = NULL;\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t\tsnprintf(name, sizeof(name), \"%s.inline\", mod-\u003ename);\n+\t\t\tattr = sysfs_btf_add(name, relocated_inline_data, inline_data_size);\n+\t\t\tif (IS_ERR(attr)) {\n \t\t\t\terr = 0;\n+\t\t\t\tkvfree(relocated_inline_data);\n+\t\t\t\tbtf_mod-\u003ebtf_inline_data = NULL;\n \t\t\t\tgoto out;\n \t\t\t}\n-\n-\t\t\tbtf_mod-\u003esysfs_attr = attr;\n+\t\t\tbtf_mod-\u003esysfs_inline_attr = attr;\n \t\t}\n+#endif\n \n \t\tbreak;\n \tcase MODULE_STATE_LIVE:\n@@ -8617,10 +9155,14 @@ static int btf_module_notify(struct notifier_block *nb, unsigned long op,\n \t\t\tbtf_free_id(btf_mod-\u003ebtf);\n \t\t\tlist_del(\u0026btf_mod-\u003elist);\n \t\t\tif (btf_mod-\u003esysfs_attr)\n-\t\t\t\tsysfs_remove_bin_file(btf_kobj, btf_mod-\u003esysfs_attr);\n+\t\t\t\tsysfs_btf_remove(btf_mod-\u003esysfs_attr);\n+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)\n+\t\t\tif (btf_mod-\u003esysfs_inline_attr)\n+\t\t\t\tsysfs_btf_remove(btf_mod-\u003esysfs_inline_attr);\n+\t\t\tkvfree(btf_mod-\u003ebtf_inline_data);\n+#endif\n \t\t\tpurge_cand_cache(btf_mod-\u003ebtf);\n \t\t\tbtf_put(btf_mod-\u003ebtf);\n-\t\t\tkfree(btf_mod-\u003esysfs_attr);\n \t\t\tkfree(btf_mod);\n \t\t\tbreak;\n \t\t}\n@@ -8638,6 +9180,12 @@ static struct notifier_block btf_module_nb = {\n static int __init btf_module_init(void)\n {\n \tregister_module_notifier(\u0026btf_module_nb);\n+#if IS_MODULE(CONFIG_DEBUG_INFO_BTF_INLINE)\n+\t/* Attribute data will be filled in on-demand if vmlinux.inline is read. */\n+\tvmlinux_inline_attr = sysfs_btf_add(\"vmlinux.inline\", NULL, 0);\n+\tif (IS_ERR(vmlinux_inline_attr))\n+\t\tvmlinux_inline_attr = NULL;\n+#endif\n \treturn 0;\n }\n \ndiff --git a/kernel/bpf/btf_vmlinux_inline.c b/kernel/bpf/btf_vmlinux_inline.c\nnew file mode 100644\nindex 0000000000000..13ed962ffceb1\n--- /dev/null\n+++ b/kernel/bpf/btf_vmlinux_inline.c\n@@ -0,0 +1,37 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026, Oracle and/or its affiliates. */\n+/*\n+ * Provide kernel BTF inline function information for use by BPF tools.\n+ */\n+#include \u003clinux/btf.h\u003e\n+#include \u003clinux/init.h\u003e\n+#include \u003clinux/kernel.h\u003e\n+#include \u003clinux/module.h\u003e\n+\n+#if IS_BUILTIN(CONFIG_DEBUG_INFO_BTF_INLINE)\n+extern char __start_BTF_inline[];\n+extern char __stop_BTF_inline[];\n+#endif\n+\n+static int __init btf_vmlinux_inline_init(void)\n+{\n+#if IS_BUILTIN(CONFIG_DEBUG_INFO_BTF_INLINE)\n+\tsize_t data_size = __stop_BTF_inline - __start_BTF_inline;\n+\n+\tif (data_size)\n+\t\tsysfs_btf_add(\"vmlinux.inline\", __start_BTF_inline,\n+\t\t\t data_size);\n+#endif\n+\treturn 0;\n+}\n+subsys_initcall(btf_vmlinux_inline_init);\n+\n+#if IS_MODULE(CONFIG_DEBUG_INFO_BTF_INLINE)\n+static void __exit btf_vmlinux_inline_fini(void)\n+{\n+}\n+module_exit(btf_vmlinux_inline_fini);\n+#endif\n+\n+MODULE_DESCRIPTION(\"BTF inline information for vmlinux\");\n+MODULE_LICENSE(\"GPL\");\ndiff --git a/kernel/module/main.c b/kernel/module/main.c\nindex 46dd8d25a6058..7e06ef1131131 100644\n--- a/kernel/module/main.c\n+++ b/kernel/module/main.c\n@@ -2712,6 +2712,10 @@ static int find_module_sections(struct module *mod, struct load_info *info)\n \tmod-\u003ebtf_base_data = any_section_objs(info, \".BTF.base\", 1,\n \t\t\t\t\t \u0026mod-\u003ebtf_base_data_size);\n #endif\n+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)\n+\tmod-\u003ebtf_inline_data = any_section_objs(info, \".BTF.inline\", 1,\n+\t\t\t\t\t\t\u0026mod-\u003ebtf_inline_data_size);\n+#endif\n #ifdef CONFIG_JUMP_LABEL\n \tmod-\u003ejump_entries = section_objs(info, \"__jump_table\",\n \t\t\t\t\tsizeof(*mod-\u003ejump_entries),\ndiff --git a/lib/Kconfig.debug b/lib/Kconfig.debug\nindex 1244dcac2294a..1b96d6acfdce9 100644\n--- a/lib/Kconfig.debug\n+++ b/lib/Kconfig.debug\n@@ -425,6 +425,12 @@ config PAHOLE_HAS_LANG_EXCLUDE\n \t otherwise it would emit malformed kernel and module binaries when\n \t using DEBUG_INFO_BTF_MODULES.\n \n+config PAHOLE_HAS_INLINE\n+\tdef_bool PAHOLE_VERSION \u003e= 131\n+\thelp\n+\t Support for the \"inline\" BTF feature is available. It encodes\n+\t information about inline sites and how to retrieve their parameters.\n+\n config DEBUG_INFO_BTF_MODULES\n \tbool \"Generate BTF type information for kernel modules\"\n \tdefault y\n@@ -432,6 +438,18 @@ config DEBUG_INFO_BTF_MODULES\n \thelp\n \t Generate compact split BTF type information for kernel modules.\n \n+config DEBUG_INFO_BTF_INLINE\n+\ttristate \"Provide information about inline sites in BTF\"\n+\tdefault n\n+\tdepends on DEBUG_INFO_BTF \u0026\u0026 PAHOLE_HAS_INLINE \u0026\u0026 SYSFS\n+\tselect DEBUG_INFO_BTF_MODULES if MODULES\n+\thelp\n+\t Generate information about inline sites in .BTF.inline sections.\n+\t These sections contain split BTF relative to the kernel or module BTF\n+\t and are made available in /sys/kernel/btf with a \".inline\" suffix.\n+\t The information describes inline locations and how to retrieve their\n+\t associated parameters.\n+\n config MODULE_ALLOW_BTF_MISMATCH\n \tbool \"Allow loading modules with non-matching BTF type info\"\n \tdepends on DEBUG_INFO_BTF_MODULES\ndiff --git a/scripts/Makefile.btf b/scripts/Makefile.btf\nindex a1812985a61aa..d42dbc8d71992 100644\n--- a/scripts/Makefile.btf\n+++ b/scripts/Makefile.btf\n@@ -22,7 +22,14 @@ endif\n \n pahole-flags-$(CONFIG_PAHOLE_HAS_LANG_EXCLUDE)\t\t+= --lang_exclude=rust\n \n+btf-inline := $(CONFIG_DEBUG_INFO_BTF_INLINE)\n+ifneq ($(btf-inline),)\n+btf-inline-feat := inline\n+pahole-flags-$(call test-ge, $(pahole-ver), 131) += --btf_features=$(btf-inline-feat)\n+endif\n+\n export PAHOLE_FLAGS := $(pahole-flags-y)\n+export BTF_INLINE := $(btf-inline)\n \n resolve-btfids-flags-y :=\n resolve-btfids-flags-$(CONFIG_WERROR) += --fatal_warnings\ndiff --git a/scripts/gen-btf.sh b/scripts/gen-btf.sh\nindex 8ca96eb10a69e..cd6588588fb8f 100755\n--- a/scripts/gen-btf.sh\n+++ b/scripts/gen-btf.sh\n@@ -76,6 +76,7 @@ gen_btf_data()\n \n \t${RESOLVE_BTFIDS} ${RESOLVE_BTFIDS_FLAGS}\t\\\n \t\t${BTF_BASE:+--btf_base ${BTF_BASE}}\t\\\n+\t\t${BTF_INLINE:+--inline}\t\t\\\n \t\t--btf ${btf1} \"${ELF_FILE}\"\n }\n \n@@ -83,14 +84,30 @@ gen_btf_o()\n {\n \tbtf_data=${ELF_FILE}.btf.o\n \n-\t# Create ${btf_data} which contains just .BTF section but no symbols. Add\n+\t# Create ${btf_data} which contains just BTF sections but no symbols. Add\n \t# SHF_ALLOC because .BTF will be part of the vmlinux image. --strip-all\n \t# deletes all symbols including __start_BTF and __stop_BTF, which will\n \t# be redefined in the linker script.\n \techo \"\" | ${CC} ${CLANG_FLAGS} ${KBUILD_CPPFLAGS} ${KBUILD_CFLAGS} -fno-lto -c -x c -o ${btf_data} -\n \t${OBJCOPY} --add-section .BTF=${ELF_FILE}.BTF \\\n \t\t--set-section-flags .BTF=alloc,readonly ${btf_data}\n-\t${OBJCOPY} --only-section=.BTF --strip-all ${btf_data}\n+\tONLY_SEC=\"--only-section=.BTF\"\n+\tbtf_inline=${ELF_FILE}.BTF.inline\n+\tif [ \"${BTF_INLINE}\" = \"m\" ]; then\n+\t\t# vmlinux BTF is generated from a temporary ELF. Retain its\n+\t\t# vmlinux-relative inline BTF for btf_vmlinux_inline.ko.\n+\t\tif [ -f \"${btf_inline}\" ]; then\n+\t\t\tcp \"${btf_inline}\" \"${objtree}/vmlinux.BTF.inline\"\n+\t\telse\n+\t\t\trm -f \"${objtree}/vmlinux.BTF.inline\"\n+\t\tfi\n+\tfi\n+\tif [ \"${BTF_INLINE}\" = \"y\" ] \u0026\u0026 [ -f \"${btf_inline}\" ]; then\n+\t\t${OBJCOPY} --add-section .BTF.inline=${btf_inline} \\\n+\t\t\t--set-section-flags .BTF.inline=alloc,readonly ${btf_data}\n+\t\tONLY_SEC=\"${ONLY_SEC} --only-section=.BTF.inline\"\n+\tfi\n+\t${OBJCOPY} ${ONLY_SEC} --strip-all ${btf_data}\n \n \t# Change e_type to ET_REL so that it can be used to link final vmlinux.\n \t# GNU ld 2.35+ and lld do not allow an ET_EXEC input.\n@@ -111,6 +128,17 @@ embed_btf_data()\n \tif [ -f \"${btf_base}\" ]; then\n \t\t${OBJCOPY} --add-section .BTF.base=${btf_base} ${ELF_FILE}\n \tfi\n+\tbtf_inline=${ELF_FILE}.BTF.inline\n+\tcase \"${ELF_FILE}\" in\n+\t*/btf_vmlinux_inline.ko)\n+\t\t# With CONFIG_DEBUG_INFO_BTF_INLINE=m, deliver vmlinux\n+\t\t# .BTF.inline via module\n+\t\tbtf_inline=${BTF_BASE}.BTF.inline\n+\t\t;;\n+\tesac\n+\tif [ -n \"${BTF_INLINE}\" ] \u0026\u0026 [ -f \"${btf_inline}\" ]; then\n+\t\t${OBJCOPY} --add-section .BTF.inline=${btf_inline} ${ELF_FILE}\n+\tfi\n \tbtf_ids=\"${ELF_FILE}.BTF_ids\"\n \tif [ -f \"${btf_ids}\" ]; then\n \t\t${RESOLVE_BTFIDS} --patch_btfids ${btf_ids} ${ELF_FILE}\n@@ -121,6 +149,7 @@ cleanup()\n {\n \trm -f \"${ELF_FILE}.BTF.1\"\n \trm -f \"${ELF_FILE}.BTF\"\n+\trm -f \"${ELF_FILE}.BTF.inline\"\n \tif [ \"${BTFGEN_MODE}\" = \"module\" ]; then\n \t\trm -f \"${ELF_FILE}.BTF.base\"\n \t\trm -f \"${ELF_FILE}.BTF_ids\"\ndiff --git a/tools/bpf/bpftool/Documentation/bpftool-btf.rst b/tools/bpf/bpftool/Documentation/bpftool-btf.rst\nindex cf75a7fa2d6bc..60c46748b8663 100644\n--- a/tools/bpf/bpftool/Documentation/bpftool-btf.rst\n+++ b/tools/bpf/bpftool/Documentation/bpftool-btf.rst\n@@ -16,7 +16,7 @@ SYNOPSIS\n \n **bpftool** [*OPTIONS*] **btf** *COMMAND*\n \n-*OPTIONS* := { |COMMON_OPTIONS| | { **-B** | **--base-btf** } }\n+*OPTIONS* := { |COMMON_OPTIONS| | { **-B** | **--base-btf** [ **-B** | **--base-btf** ] } }\n \n *COMMANDS* := { **dump** | **help** }\n \n@@ -87,7 +87,10 @@ OPTIONS\n objects for kernel modules. To avoid duplicating all kernel symbols\n required by modules, BTF objects for modules are \"split\", they are\n built incrementally on top of the kernel (vmlinux) BTF object. So the\n- base BTF reference should usually point to the kernel BTF.\n+ base BTF reference should usually point to the kernel BTF. Multiple\n+ base BTF objects can be passed, where the first is assumed to be the\n+ root BTF, followed by split BTF based upon it, followed by split\n+ BTF based upon the first split BTF and so on.\n \n When the main BTF object to process (for example, the module BTF to\n dump) is passed as a *FILE*, bpftool attempts to autodetect the path\ndiff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c\nindex bca0a3982f09b..4d8991c99b484 100644\n--- a/tools/bpf/bpftool/btf.c\n+++ b/tools/bpf/bpftool/btf.c\n@@ -51,6 +51,9 @@ static const char * const btf_kind_str[NR_BTF_KINDS] = {\n \t[BTF_KIND_DECL_TAG]\t= \"DECL_TAG\",\n \t[BTF_KIND_TYPE_TAG]\t= \"TYPE_TAG\",\n \t[BTF_KIND_ENUM64]\t= \"ENUM64\",\n+\t[BTF_KIND_LOC_PARAM]\t= \"LOC_PARAM\",\n+\t[BTF_KIND_LOC_PROTO]\t= \"LOC_PROTO\",\n+\t[BTF_KIND_LOCSEC]\t= \"LOCSEC\",\n };\n \n struct sort_datum {\n@@ -415,6 +418,88 @@ static int dump_btf_type(const struct btf *btf, __u32 id,\n \t\t}\n \t\tbreak;\n \t}\n+\tcase BTF_KIND_LOC_PARAM: {\n+\t\tconst struct btf_loc_param *p = btf_loc_param(t);\n+\t\t__u32 *v = (__u32 *)(p + 1);\n+\t\t__u32 i, vlen = btf_vlen(t);\n+\n+\t\tif (json_output) {\n+\t\t\tjsonw_uint_field(w, \"size\", t-\u003esize);\n+\t\t\tjsonw_uint_field(w, \"flags\", p-\u003eflags);\n+\t\t\tjsonw_uint_field(w, \"vlen\", vlen);\n+\t\t\tjsonw_name(w, \"values\");\n+\t\t\tjsonw_start_array(w);\n+\t\t} else {\n+\t\t\tprintf(\" size=%u flags=0x%x vlen=%u\", t-\u003esize, p-\u003eflags, vlen);\n+\t\t}\n+\t\tfor (i = 0; i \u003c vlen; i++, v++) {\n+\t\t\tif (json_output) {\n+\t\t\t\tjsonw_start_object(w);\n+\t\t\t\tjsonw_uint_field(w, \"value\", *v);\n+\t\t\t\tjsonw_end_object(w);\n+\t\t\t} else {\n+\t\t\t\tprintf(\"\\n\\t value=%u\", *v);\n+\t\t\t}\n+\t\t}\n+\t\tif (json_output)\n+\t\t\tjsonw_end_array(w);\n+\t\tbreak;\n+\t}\n+\tcase BTF_KIND_LOC_PROTO: {\n+\t\t__u32 *params = btf_loc_proto_params(t);\n+\t\t__u16 vlen = btf_vlen(t);\n+\t\tint i;\n+\n+\t\tif (json_output) {\n+\t\t\tjsonw_uint_field(w, \"vlen\", vlen);\n+\t\t\tjsonw_name(w, \"params\");\n+\t\t\tjsonw_start_array(w);\n+\t\t} else {\n+\t\t\tprintf(\" vlen=%u\", vlen);\n+\t\t}\n+\n+\t\tfor (i = 0; i \u003c vlen; i++, params++) {\n+\t\t\tif (json_output) {\n+\t\t\t\tjsonw_start_object(w);\n+\t\t\t\tjsonw_uint_field(w, \"type_id\", *params);\n+\t\t\t\tjsonw_end_object(w);\n+\t\t\t} else {\n+\t\t\t\tprintf(\"\\n\\t type_id=%u\", *params);\n+\t\t\t}\n+\t\t}\n+\t\tif (json_output)\n+\t\t\tjsonw_end_array(w);\n+\t\tbreak;\n+\t}\n+\n+\tcase BTF_KIND_LOCSEC: {\n+\t\tstruct btf_loc *locs = btf_locsec_locs(t);\n+\t\t__u32 i, vlen = btf_vlen(t);\n+\n+\t\tif (json_output) {\n+\t\t\tjsonw_uint_field(w, \"vlen\", vlen);\n+\t\t\tjsonw_name(w, \"locs\");\n+\t\t\tjsonw_start_array(w);\n+\t\t} else {\n+\t\t\tprintf(\" vlen=%u\", vlen);\n+\t\t}\n+\n+\t\tfor (i = 0; i \u003c vlen; i++, locs++) {\n+\t\t\tif (json_output) {\n+\t\t\t\tjsonw_start_object(w);\n+\t\t\t\tjsonw_uint_field(w, \"func_type_id\", locs-\u003efunc);\n+\t\t\t\tjsonw_uint_field(w, \"loc_proto_type_id\", locs-\u003eloc_proto);\n+\t\t\t\tjsonw_uint_field(w, \"offset\", locs-\u003eoffset);\n+\t\t\t\tjsonw_end_object(w);\n+\t\t\t} else {\n+\t\t\t\tprintf(\"\\n\\t func_type_id=%u loc_proto_type_id=%u offset=%u\",\n+\t\t\t\t locs-\u003efunc, locs-\u003eloc_proto, locs-\u003eoffset);\n+\t\t\t}\n+\t\t}\n+\t\tif (json_output)\n+\t\t\tjsonw_end_array(w);\n+\t\tbreak;\n+\t}\n \tdefault:\n \t\tbreak;\n \t}\ndiff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c\nindex 5ababd8f7d0a0..9329f140b700f 100644\n--- a/tools/bpf/bpftool/main.c\n+++ b/tools/bpf/bpftool/main.c\n@@ -515,7 +515,8 @@ int main(int argc, char **argv)\n \t\t\tverifier_logs = true;\n \t\t\tbreak;\n \t\tcase 'B':\n-\t\t\tbase_btf = btf__parse(optarg, NULL);\n+\t\t\t/* handle multi-split BTF */\n+\t\t\tbase_btf = btf__parse_split(optarg, base_btf);\n \t\t\tif (!base_btf) {\n \t\t\t\tp_err(\"failed to parse base BTF at '%s': %d\\n\",\n \t\t\t\t optarg, -errno);\ndiff --git a/tools/bpf/resolve_btfids/main.c b/tools/bpf/resolve_btfids/main.c\nindex 37d7e7224207d..20b228c4d6789 100644\n--- a/tools/bpf/resolve_btfids/main.c\n+++ b/tools/bpf/resolve_btfids/main.c\n@@ -142,7 +142,9 @@ struct object {\n \n \tstruct btf *btf;\n \tstruct btf *base_btf;\n+\tstruct btf *inline_btf;\n \tbool distill_base;\n+\tbool extract_inline;\n \n \tstruct {\n \t\tint\t\t fd;\n@@ -154,6 +156,10 @@ struct object {\n \t\tsize_t\t\t strtabidx;\n \t\tunsigned long\t idlist_addr;\n \t\tint\t\t encoding;\n+\t\tconst char\t**func_symbols;\n+\t\tu32\t\t func_symbols_cnt;\n+\t\tu32\t\t func_symbols_cap;\n+\t\tbool\t\t func_symbols_collected;\n \t} efile;\n \n \tstruct rb_root\tsets;\n@@ -572,6 +578,58 @@ static const char *find_name_by_addr(struct object *obj, Elf64_Addr addr)\n \treturn res ? res-\u003ename : NULL;\n }\n \n+static int cmp_func_symbol(const void *a, const void *b)\n+{\n+\tconst char * const *name = a;\n+\tconst char * const *other = b;\n+\n+\treturn strcmp(*name, *other);\n+}\n+\n+static int collect_func_symbols(struct object *obj)\n+{\n+\tElf_Scn *scn;\n+\tGElf_Shdr sh;\n+\tint n, i;\n+\n+\tif (obj-\u003eefile.symbols_shndx == -1)\n+\t\treturn 0;\n+\n+\tscn = elf_getscn(obj-\u003eefile.elf, obj-\u003eefile.symbols_shndx);\n+\tif (!scn || gelf_getshdr(scn, \u0026sh) != \u0026sh)\n+\t\treturn -EINVAL;\n+\tn = sh.sh_size / sh.sh_entsize;\n+\n+\tfor (i = 0; i \u003c n; i++) {\n+\t\tGElf_Sym sym;\n+\t\tconst char *name;\n+\n+\t\tif (!gelf_getsym(obj-\u003eefile.symbols, i, \u0026sym))\n+\t\t\treturn -EINVAL;\n+\t\tif (GELF_ST_TYPE(sym.st_info) != STT_FUNC ||\n+\t\t sym.st_shndx == SHN_UNDEF || !sym.st_name)\n+\t\t\tcontinue;\n+\t\tname = elf_strptr(obj-\u003eefile.elf, obj-\u003eefile.strtabidx, sym.st_name);\n+\t\tif (!name)\n+\t\t\treturn -EINVAL;\n+\t\tif (ensure_mem(\u0026obj-\u003eefile.func_symbols, \u0026obj-\u003eefile.func_symbols_cap,\n+\t\t\t obj-\u003eefile.func_symbols_cnt + 1))\n+\t\t\treturn -ENOMEM;\n+\t\tobj-\u003eefile.func_symbols[obj-\u003eefile.func_symbols_cnt++] = name;\n+\t}\n+\n+\tqsort(obj-\u003eefile.func_symbols, obj-\u003eefile.func_symbols_cnt,\n+\t sizeof(*obj-\u003eefile.func_symbols), cmp_func_symbol);\n+\tobj-\u003eefile.func_symbols_collected = true;\n+\treturn 0;\n+}\n+\n+static bool has_func_symbol(const struct object *obj, const char *name)\n+{\n+\treturn bsearch(\u0026name, obj-\u003eefile.func_symbols, obj-\u003eefile.func_symbols_cnt,\n+\t\t sizeof(*obj-\u003eefile.func_symbols), cmp_func_symbol) != NULL;\n+}\n+\n static int symbols_collect(struct object *obj)\n {\n \tElf_Scn *scn = NULL;\n@@ -1514,14 +1572,193 @@ static int btf2btf(struct object *obj)\n * Sort types by name in ascending order resulting in all\n * anonymous types being placed before named types.\n */\n+struct btf_name_sort {\n+\tstruct btf *btf;\n+\tbool extract_inline;\n+\tbool *transfer_funcs;\n+\tbool *transfer_protos;\n+};\n+\n+static int btf_inline_kind_order(const struct btf_type *t, bool transfer_func,\n+\t\t\t\t bool transfer_proto)\n+{\n+\tif (transfer_func)\n+\t\treturn 1;\n+\tif (transfer_proto)\n+\t\treturn 2;\n+\tif (btf_is_loc_param(t))\n+\t\treturn 3;\n+\tif (btf_is_loc_proto(t))\n+\t\treturn 4;\n+\tif (btf_is_locsec(t))\n+\t\treturn 5;\n+\treturn 0;\n+}\n+\n+enum inline_ref_kind {\n+\tINLINE_REF_FUNC,\n+\tINLINE_REF_FUNC_PROTO,\n+};\n+\n+static void mark_inline_ref(struct btf *btf, bool *refs, int start_id,\n+\t\t\t int type_cnt, __u32 type_id, enum inline_ref_kind ref_kind)\n+{\n+\tconst struct btf_type *t;\n+\n+\tif (type_id \u003c start_id || type_id \u003e= type_cnt)\n+\t\treturn;\n+\tt = btf__type_by_id(btf, type_id);\n+\tif ((ref_kind == INLINE_REF_FUNC \u0026\u0026 btf_is_func(t)) ||\n+\t (ref_kind == INLINE_REF_FUNC_PROTO \u0026\u0026 btf_is_func_proto(t)))\n+\t\trefs[type_id] = true;\n+}\n+\n+static void mark_inline_type_refs(struct btf *btf, bool *refs, int start_id,\n+\t\t\t\t int type_cnt, const struct btf_type *t,\n+\t\t\t\t enum inline_ref_kind ref_kind)\n+{\n+\t/*\n+\t * These are the only BTF kinds that can directly reference a FUNC or\n+\t * FUNC_PROTO (aside from LOCSEC).\n+\t */\n+\tswitch (btf_kind(t)) {\n+\tcase BTF_KIND_CONST:\n+\tcase BTF_KIND_VOLATILE:\n+\tcase BTF_KIND_RESTRICT:\n+\tcase BTF_KIND_PTR:\n+\tcase BTF_KIND_TYPEDEF:\n+\tcase BTF_KIND_FUNC:\n+\tcase BTF_KIND_VAR:\n+\tcase BTF_KIND_DECL_TAG:\n+\tcase BTF_KIND_TYPE_TAG:\n+\t\tmark_inline_ref(btf, refs, start_id, type_cnt, t-\u003etype, ref_kind);\n+\t\tbreak;\n+\tdefault:\n+\t\treturn;\n+\t}\n+}\n+\n+/* A FUNC can move to inline BTF only when LOCSEC is its sole BTF user. */\n+static int find_inline_funcs(struct object *obj, bool **transfer_funcsp,\n+\t\t\t bool **transfer_protosp)\n+{\n+\tbool *locsec_refs, *other_refs, *proto_candidates, *proto_base_refs;\n+\tint i, type_cnt, start_id = 0;\n+\tstruct btf *btf = obj-\u003ebtf;\n+\n+\ttype_cnt = btf__type_cnt(btf);\n+\t*transfer_funcsp = NULL;\n+\t*transfer_protosp = NULL;\n+\tif (btf__base_btf(btf))\n+\t\tstart_id = btf__type_cnt(btf__base_btf(btf));\n+\n+\tlocsec_refs = calloc(type_cnt, sizeof(*locsec_refs));\n+\tother_refs = calloc(type_cnt, sizeof(*other_refs));\n+\tproto_candidates = calloc(type_cnt, sizeof(*proto_candidates));\n+\tproto_base_refs = calloc(type_cnt, sizeof(*proto_base_refs));\n+\tif (!locsec_refs || !other_refs || !proto_candidates || !proto_base_refs)\n+\t\tgoto err;\n+\n+\t/* First identify locsec FUNC references */\n+\tfor (i = start_id; i \u003c type_cnt; i++) {\n+\t\tconst struct btf_type *t = btf__type_by_id(btf, i);\n+\t\tconst struct btf_loc *loc;\n+\t\tint j;\n+\n+\t\tif (!btf_is_locsec(t))\n+\t\t\tcontinue;\n+\t\tloc = btf_locsec_locs(t);\n+\t\tfor (j = 0; j \u003c btf_vlen(t); j++, loc++) {\n+\t\t\tif (loc-\u003efunc \u003e= start_id \u0026\u0026 loc-\u003efunc \u003c type_cnt \u0026\u0026\n+\t\t\t btf_is_func(btf__type_by_id(btf, loc-\u003efunc)))\n+\t\t\t\tlocsec_refs[loc-\u003efunc] = true;\n+\t\t}\n+\t}\n+\n+\t/*\n+\t * Then mark any additional references to them; references outside\n+\t * of LOCSEC make the FUNC ineligible for transfer.\n+\t */\n+\tfor (i = start_id; i \u003c type_cnt; i++) {\n+\t\tconst struct btf_type *t = btf__type_by_id(btf, i);\n+\n+\t\tif (btf_is_locsec(t))\n+\t\t\tcontinue;\n+\t\tmark_inline_type_refs(btf, other_refs, start_id, type_cnt, t,\n+\t\t\t\t INLINE_REF_FUNC);\n+\t}\n+\n+\tfor (i = start_id; i \u003c type_cnt; i++) {\n+\t\tconst struct btf_type *t = btf__type_by_id(btf, i);\n+\t\tconst char *name;\n+\n+\t\t/* If there are no locsec references, or we find additional\n+\t\t * non-locsec references, skip as ineligible.\n+\t\t */\n+\t\tif (!locsec_refs[i] || other_refs[i])\n+\t\t\tcontinue;\n+\t\tname = btf__name_by_offset(btf, t-\u003ename_off);\n+\t\t/* Keep out-of-line functions and BTF ID users in the main BTF. */\n+\t\tif (!obj-\u003eefile.func_symbols_collected || has_func_symbol(obj, name) ||\n+\t\t btf_id__find(\u0026obj-\u003efuncs, name))\n+\t\t\tlocsec_refs[i] = false;\n+\t}\n+\n+\tfor (i = start_id; i \u003c type_cnt; i++) {\n+\t\tconst struct btf_type *t;\n+\n+\t\tif (!locsec_refs[i])\n+\t\t\tcontinue;\n+\t\tt = btf__type_by_id(btf, i);\n+\t\tproto_candidates[t-\u003etype] = true;\n+\t}\n+\n+\t/* Mark every FUNC_PROTO directly needed by retained base BTF types. */\n+\tfor (i = start_id; i \u003c type_cnt; i++) {\n+\t\tconst struct btf_type *t = btf__type_by_id(btf, i);\n+\n+\t\tif (locsec_refs[i] || proto_candidates[i] ||\n+\t\t btf_is_loc_param(t) || btf_is_loc_proto(t) ||\n+\t\t btf_is_locsec(t))\n+\t\t\tcontinue;\n+\t\tmark_inline_type_refs(btf, proto_base_refs, start_id, type_cnt, t,\n+\t\t\t\t INLINE_REF_FUNC_PROTO);\n+\t}\n+\tfor (i = start_id; i \u003c type_cnt; i++)\n+\t\tproto_candidates[i] \u0026= !proto_base_refs[i];\n+\n+\tfree(other_refs);\n+\t*transfer_funcsp = locsec_refs;\n+\t*transfer_protosp = proto_candidates;\n+\tfree(proto_base_refs);\n+\treturn 0;\n+err:\n+\tfree(locsec_refs);\n+\tfree(other_refs);\n+\tfree(proto_candidates);\n+\tfree(proto_base_refs);\n+\treturn -ENOMEM;\n+}\n+\n static int cmp_type_names(const void *a, const void *b, void *priv)\n {\n-\tstruct btf *btf = (struct btf *)priv;\n+\tstruct btf_name_sort *sort = priv;\n+\tstruct btf *btf = sort-\u003ebtf;\n \tconst struct btf_type *ta = btf__type_by_id(btf, *(__u32 *)a);\n \tconst struct btf_type *tb = btf__type_by_id(btf, *(__u32 *)b);\n \tconst char *na, *nb;\n+\tint inline_order;\n \tint r;\n \n+\tif (sort-\u003eextract_inline) {\n+\t\tinline_order = btf_inline_kind_order(ta, sort-\u003etransfer_funcs[*(__u32 *)a],\n+\t\t\t\t\t sort-\u003etransfer_protos[*(__u32 *)a]) -\n+\t\t\t btf_inline_kind_order(tb, sort-\u003etransfer_funcs[*(__u32 *)b],\n+\t\t\t\t\t sort-\u003etransfer_protos[*(__u32 *)b]);\n+\t\tif (inline_order)\n+\t\t\treturn inline_order;\n+\t}\n+\n \tna = btf__str_by_offset(btf, ta-\u003ename_off);\n \tnb = btf__str_by_offset(btf, tb-\u003ename_off);\n \tr = strcmp(na, nb);\n@@ -1532,8 +1769,60 @@ static int cmp_type_names(const void *a, const void *b, void *priv)\n \treturn *(__u32 *)a \u003c *(__u32 *)b ? -1 : 1;\n }\n \n-static int sort_btf_by_name(struct btf *btf)\n+struct loc_sort {\n+\tconst __u32 *id_map;\n+\t__u32 start_id;\n+};\n+\n+static __u32 projected_type_id(const struct loc_sort *sort, __u32 id)\n+{\n+\tif (id \u003c sort-\u003estart_id)\n+\t\treturn id;\n+\treturn sort-\u003eid_map[id - sort-\u003estart_id] \u0026 ~BTF_PERMUTE_ID_TRANSFER;\n+}\n+\n+static int cmp_loc(const void *a, const void *b, void *priv)\n {\n+\tconst struct loc_sort *sort = priv;\n+\tconst struct btf_loc *la = a, *lb = b;\n+\t__u32 afunc = projected_type_id(sort, la-\u003efunc);\n+\t__u32 bfunc = projected_type_id(sort, lb-\u003efunc);\n+\t__u32 aproto = projected_type_id(sort, la-\u003eloc_proto);\n+\t__u32 bproto = projected_type_id(sort, lb-\u003eloc_proto);\n+\n+\tif (afunc != bfunc)\n+\t\treturn afunc \u003c bfunc ? -1 : 1;\n+\tif (la-\u003eoffset != lb-\u003eoffset)\n+\t\treturn la-\u003eoffset \u003c lb-\u003eoffset ? -1 : 1;\n+\tif (aproto != bproto)\n+\t\treturn aproto \u003c bproto ? -1 : 1;\n+\treturn 0;\n+}\n+\n+static void sort_locsecs(struct btf *btf, const __u32 *id_map, __u32 start_id,\n+\t\t\t int nr_types)\n+{\n+\tstruct loc_sort sort = { .id_map = id_map, .start_id = start_id };\n+\tint i;\n+\n+\tfor (i = 0; i \u003c nr_types; i++) {\n+\t\tstruct btf_type *t = (struct btf_type *)btf__type_by_id(btf, start_id + i);\n+\n+\t\tif (btf_is_locsec(t))\n+\t\t\tqsort_r(btf_locsec_locs(t), btf_vlen(t), sizeof(struct btf_loc),\n+\t\t\t\tcmp_loc, \u0026sort);\n+\t}\n+}\n+\n+static int sort_btf_by_name(struct object *obj)\n+{\n+\tstruct btf *btf = obj-\u003ebtf;\n+\tbool *transfer_funcs = NULL, *transfer_protos = NULL;\n+\tstruct btf_name_sort sort = {\n+\t\t.btf = btf,\n+\t\t.extract_inline = obj-\u003eextract_inline,\n+\t};\n+\tLIBBPF_OPTS(btf_permute_opts, opts);\n \t__u32 *permute_ids = NULL, *id_map = NULL;\n \tint nr_types, i, err = 0;\n \t__u32 start_id = 0, id;\n@@ -1541,6 +1830,13 @@ static int sort_btf_by_name(struct btf *btf)\n \tif (btf__base_btf(btf))\n \t\tstart_id = btf__type_cnt(btf__base_btf(btf));\n \tnr_types = btf__type_cnt(btf) - start_id;\n+\tif (obj-\u003eextract_inline) {\n+\t\terr = find_inline_funcs(obj, \u0026transfer_funcs, \u0026transfer_protos);\n+\t\tif (err)\n+\t\t\tgoto out;\n+\t\tsort.transfer_funcs = transfer_funcs;\n+\t\tsort.transfer_protos = transfer_protos;\n+\t}\n \n \tpermute_ids = calloc(nr_types, sizeof(*permute_ids));\n \tif (!permute_ids) {\n@@ -1557,21 +1853,37 @@ static int sort_btf_by_name(struct btf *btf)\n \tfor (i = 0, id = start_id; i \u003c nr_types; i++, id++)\n \t\tpermute_ids[i] = id;\n \n-\tqsort_r(permute_ids, nr_types, sizeof(*permute_ids), cmp_type_names,\n-\t\tbtf);\n+\tqsort_r(permute_ids, nr_types, sizeof(*permute_ids), cmp_type_names, \u0026sort);\n \n \tfor (i = 0; i \u003c nr_types; i++) {\n \t\tid = permute_ids[i] - start_id;\n \t\tid_map[id] = i + start_id;\n+\t\tif (obj-\u003eextract_inline \u0026\u0026\n+\t\t btf_inline_kind_order(btf__type_by_id(btf, permute_ids[i]),\n+\t\t\t\t\t transfer_funcs[permute_ids[i]],\n+\t\t\t\t\t transfer_protos[permute_ids[i]]))\n+\t\t\tid_map[id] |= BTF_PERMUTE_ID_TRANSFER;\n \t}\n \n-\terr = btf__permute(btf, id_map, nr_types, NULL);\n+\t/*\n+\t * Key LOCSEC records by their post-permutation function ID and offset\n+\t * since the tracer pattern will be to look up FUNC by name, then\n+\t * find LOCSEC records for that function.\n+\t */\n+\tsort_locsecs(btf, id_map, start_id, nr_types);\n+\n+\tif (obj-\u003eextract_inline)\n+\t\topts.transfer_btf = \u0026obj-\u003einline_btf;\n+\terr = btf__permute(btf, id_map, nr_types,\n+\t\t\t obj-\u003eextract_inline ? \u0026opts : NULL);\n \tif (err)\n \t\tpr_err(\"FAILED: btf permute: %s\\n\", strerror(-err));\n \n out:\n \tfree(permute_ids);\n \tfree(id_map);\n+\tfree(transfer_funcs);\n+\tfree(transfer_protos);\n \treturn err;\n }\n \n@@ -1599,7 +1911,7 @@ static int finalize_btf(struct object *obj)\n \t\tobj-\u003ebtf = btf;\n \t}\n \n-\terr = sort_btf_by_name(obj-\u003ebtf);\n+\terr = sort_btf_by_name(obj);\n \tif (err) {\n \t\tpr_err(\"FAILED to sort BTF: %s\\n\", strerror(errno));\n \t\tgoto out_err;\n@@ -1773,6 +2085,8 @@ int main(int argc, const char **argv)\n \t\t\t \"turn warnings into errors\"),\n \t\tOPT_BOOLEAN(0, \"distill_base\", \u0026obj.distill_base,\n \t\t\t \"distill --btf_base and emit .BTF.base section data\"),\n+\t\tOPT_BOOLEAN(0, \"inline\", \u0026obj.extract_inline,\n+\t\t\t \"extract location BTF into a .BTF.inline file\"),\n \t\tOPT_STRING(0, \"patch_btfids\", \u0026btfids_path, \"file\",\n \t\t\t \"path to .BTF_ids section data blob to patch into ELF file\"),\n \t\tOPT_END()\n@@ -1791,6 +2105,8 @@ int main(int argc, const char **argv)\n \n \tif (elf_collect(\u0026obj))\n \t\tgoto out;\n+\tif (obj.extract_inline \u0026\u0026 collect_func_symbols(\u0026obj))\n+\t\tgoto out;\n \n \t/*\n \t * We did not find .BTF_ids section or symbols section,\n@@ -1841,11 +2157,18 @@ int main(int argc, const char **argv)\n \t\tif (err)\n \t\t\tgoto out;\n \t}\n+\tif (obj.inline_btf) {\n+\t\terr = make_out_path(out_path, sizeof(out_path), obj.path, BTF_ELF_SEC \".inline\");\n+\t\terr = err ?: dump_raw_btf(obj.inline_btf, out_path);\n+\t\tif (err)\n+\t\t\tgoto out;\n+\t}\n \n \tif (!(fatal_warnings \u0026\u0026 warnings))\n \t\terr = 0;\n out:\n \tbtf__free(obj.base_btf);\n+\tbtf__free(obj.inline_btf);\n \tbtf__free(obj.btf);\n \tbtf_id__free_all(\u0026obj.structs);\n \tbtf_id__free_all(\u0026obj.unions);\n@@ -1853,6 +2176,7 @@ int main(int argc, const char **argv)\n \tbtf_id__free_all(\u0026obj.funcs);\n \tbtf_id__free_all(\u0026obj.sets);\n \tfree(obj.addr_syms);\n+\tfree(obj.efile.func_symbols);\n \tif (obj.efile.elf) {\n \t\telf_end(obj.efile.elf);\n \t\tclose(obj.efile.fd);\ndiff --git a/tools/include/uapi/linux/btf.h b/tools/include/uapi/linux/btf.h\nindex 618167cab4e61..6062c99580345 100644\n--- a/tools/include/uapi/linux/btf.h\n+++ b/tools/include/uapi/linux/btf.h\n@@ -92,7 +92,9 @@ enum {\n \tBTF_KIND_DECL_TAG\t= 17,\t/* Decl Tag */\n \tBTF_KIND_TYPE_TAG\t= 18,\t/* Type Tag */\n \tBTF_KIND_ENUM64\t\t= 19,\t/* Enumeration up to 64-bit values */\n-\n+\tBTF_KIND_LOC_PARAM\t= 20,\t/* Location parameter information */\n+\tBTF_KIND_LOC_PROTO\t= 21,\t/* Location prototype for site */\n+\tBTF_KIND_LOCSEC\t\t= 22,\t/* Location section */\n \tNR_BTF_KINDS,\n \tBTF_KIND_MAX\t\t= NR_BTF_KINDS - 1,\n };\n@@ -212,4 +214,65 @@ struct btf_enum64 {\n \t__u32\tval_hi32;\n };\n \n+/*\n+ * BTF_KIND_LOC_PARAM is followed by a single \"struct btf_loc_param\"\n+ * that contains flags specifying the contents of the vlen-specified\n+ * number of 4-byte values that follow.\n+ */\n+struct btf_loc_param {\n+\t__u32 flags;\n+};\n+\n+/*\n+ * The combination of size, vlen and flags gives us the means to interpret\n+ * the following vlen-specified set of 4-byte values:\n+ *\n+ * - a BTF_LOC_PARAM_CONST is a constant value; combination\n+ * of size, vlen and _SIGNED flag determines it. If the value requires\n+ * 64 bits it is stored in {lo,hi} order.\n+ * - a BTF_LOC_PARAM_ADDR is an address that will be normalized with\n+ * respect to kernel base address.\n+ * - a BTF_LOC_PARAM_REG with vlen 1 is a simple register number;\n+ * with vlen 2 it is a multi-register parameter.\n+ * - a _REG | DEREF with vlen 1 dereferences the value in the register\n+ * number specified.\n+ * - a REG | DEREF | OFFSET with vlen specifies the register value in\n+ * the first 4-byte value and the offset in the remainder.\n+ * - binary logical operators operate on a combination of register\n+ * number and constant value, aside from _NOT which operates on\n+ * a register\n+ */\n+enum btf_loc_param_flags {\n+\tBTF_LOC_PARAM_SIGNED\t\t=\t0x1,\n+\tBTF_LOC_PARAM_CONST\t\t=\t0x2,\n+\tBTF_LOC_PARAM_ADDR\t\t=\t0x4,\n+\tBTF_LOC_PARAM_REG\t\t=\t0x8,\n+\tBTF_LOC_PARAM_DEREF\t\t=\t0x10,\n+\tBTF_LOC_PARAM_OFFSET\t\t=\t0x20,\n+};\n+\n+/*\n+ * BTF_KIND_LOC_PROTO specifies location prototypes; i.e. how locations relate\n+ * to parameters; a struct btf_type of BTF_KIND_LOC_PROTO is followed by a\n+ * a vlen-specified number of __u32 BTF type ids which specify the associated\n+ * BTF_KIND_LOC_PARAM for each function parameter associated with the\n+ * location. The type should either be 0 (no location info) or point at\n+ * a BTF_KIND_LOC_PARAM.\n+ */\n+\n+/*\n+ * BTF_KIND_LOCSEC consists of vlen-specified number of \"struct btf_loc\"\n+ * containing location site-specific information;\n+ *\n+ * - function (func)\n+ * - location prototype type id (loc_proto)\n+ * - address offset (offset) relative to kernel base address\n+ */\n+\n+struct btf_loc {\n+\t__u32 func;\n+\t__u32 loc_proto;\n+\t__u32 offset;\n+};\n+\n #endif /* _UAPI__LINUX_BTF_H__ */\ndiff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c\nindex c783359977b46..eea325ccf768e 100644\n--- a/tools/lib/bpf/btf.c\n+++ b/tools/lib/bpf/btf.c\n@@ -57,6 +57,9 @@ static struct btf_layout layouts[NR_BTF_KINDS] = {\n [BTF_KIND_DECL_TAG] =\t{\tsizeof(struct btf_decl_tag),\t0,\t\t\t\t0 },\n [BTF_KIND_TYPE_TAG] =\t{\t0,\t\t\t\t0,\t\t\t\t0 },\n [BTF_KIND_ENUM64] =\t{\t0,\t\t\t\tsizeof(struct btf_enum64),\t0 },\n+[BTF_KIND_LOC_PARAM] =\t{\tsizeof(struct btf_loc_param),\tsizeof(__u32),\t\t\t0 },\n+[BTF_KIND_LOC_PROTO] =\t{\t0,\t\t\t\tsizeof(__u32),\t\t\t0 },\n+[BTF_KIND_LOCSEC] =\t{\t0,\t\t\t\tsizeof(struct btf_loc),\t\t0 },\n };\n \n struct btf {\n@@ -486,6 +489,12 @@ static int btf_type_size(const struct btf *btf, const struct btf_type *t)\n \t\treturn base_size + vlen * sizeof(struct btf_var_secinfo);\n \tcase BTF_KIND_DECL_TAG:\n \t\treturn base_size + sizeof(struct btf_decl_tag);\n+\tcase BTF_KIND_LOC_PARAM:\n+\t\treturn base_size + sizeof(struct btf_loc_param) + vlen * sizeof(__u32);\n+\tcase BTF_KIND_LOC_PROTO:\n+\t\treturn base_size + vlen * sizeof(__u32);\n+\tcase BTF_KIND_LOCSEC:\n+\t\treturn base_size + vlen * sizeof(struct btf_loc);\n \tdefault:\n \t\treturn btf_type_size_unknown(btf, t);\n \t}\n@@ -501,12 +510,15 @@ static void btf_bswap_type_base(struct btf_type *t)\n static int btf_bswap_type_rest(struct btf_type *t)\n {\n \tstruct btf_var_secinfo *v;\n+\tstruct btf_loc_param *lp;\n \tstruct btf_enum64 *e64;\n \tstruct btf_member *m;\n \tstruct btf_array *a;\n \tstruct btf_param *p;\n \tstruct btf_enum *e;\n+\tstruct btf_loc *l;\n \t__u32 vlen = btf_vlen(t);\n+\t__u32 *d;\n \tint i;\n \n \tswitch (btf_kind(t)) {\n@@ -569,6 +581,23 @@ static int btf_bswap_type_rest(struct btf_type *t)\n \tcase BTF_KIND_DECL_TAG:\n \t\tbtf_decl_tag(t)-\u003ecomponent_idx = bswap_32(btf_decl_tag(t)-\u003ecomponent_idx);\n \t\treturn 0;\n+\tcase BTF_KIND_LOC_PARAM:\n+\t\tlp = btf_loc_param(t);\n+\t\tlp-\u003eflags = bswap_32(lp-\u003eflags);\n+\t\tfor (i = 0, d = (__u32 *)(lp + 1); i \u003c vlen; i++, d++)\n+\t\t\t*d = bswap_32(*d);\n+\t\treturn 0;\n+\tcase BTF_KIND_LOC_PROTO:\n+\t\tfor (i = 0, d = btf_loc_proto_params(t); i \u003c vlen; i++, d++)\n+\t\t\t*d = bswap_32(*d);\n+\t\treturn 0;\n+\tcase BTF_KIND_LOCSEC:\n+\t\tfor (i = 0, l = btf_locsec_locs(t); i \u003c vlen; i++, l++) {\n+\t\t\tl-\u003efunc = bswap_32(l-\u003efunc);\n+\t\t\tl-\u003eloc_proto = bswap_32(l-\u003eloc_proto);\n+\t\t\tl-\u003eoffset = bswap_32(l-\u003eoffset);\n+\t\t}\n+\t\treturn 0;\n \tdefault:\n \t\tpr_debug(\"Unsupported BTF_KIND:%u\\n\", btf_kind(t));\n \t\treturn -EINVAL;\n@@ -745,6 +774,33 @@ static int btf_validate_type(const struct btf *btf, const struct btf_type *t, __\n \t\t}\n \t\tbreak;\n \t}\n+\tcase BTF_KIND_LOC_PARAM:\n+\t\tbreak;\n+\tcase BTF_KIND_LOC_PROTO: {\n+\t\t__u32 *p = btf_loc_proto_params(t);\n+\n+\t\tn = btf_vlen(t);\n+\t\tfor (i = 0; i \u003c n; i++, p++) {\n+\t\t\terr = btf_validate_id(btf, *p, id);\n+\t\t\tif (err)\n+\t\t\t\treturn err;\n+\t\t}\n+\t\tbreak;\n+\t}\n+\tcase BTF_KIND_LOCSEC: {\n+\t\tconst struct btf_loc *l = btf_locsec_locs(t);\n+\n+\t\tn = btf_vlen(t);\n+\t\tfor (i = 0; i \u003c n; i++, l++) {\n+\t\t\tif (!err)\n+\t\t\t\terr = btf_validate_id(btf, l-\u003efunc, id);\n+\t\t\tif (!err)\n+\t\t\t\terr = btf_validate_id(btf, l-\u003eloc_proto, id);\n+\t\t\tif (err)\n+\t\t\t\treturn err;\n+\t\t}\n+\t\tbreak;\n+\t}\n \tdefault:\n \t\t/* Kind may be represented in kind layout information. */\n \t\tif (btf_type_size_unknown(btf, t) \u003c 0) {\n@@ -2091,16 +2147,9 @@ int btf__find_str(struct btf *btf, const char *s)\n * - \u003e 0 offset into string section, on success;\n * - \u003c 0, on error.\n */\n-int btf__add_str(struct btf *btf, const char *s)\n+static int btf_add_local_str(struct btf *btf, const char *s)\n {\n-\tint off;\n-\tint err;\n-\n-\tif (btf-\u003ebase_btf) {\n-\t\toff = btf__find_str(btf-\u003ebase_btf, s);\n-\t\tif (off != -ENOENT)\n-\t\t\treturn off;\n-\t}\n+\tint off, err;\n \n \terr = btf_ensure_modifiable(btf);\n \tif (err)\n@@ -2111,10 +2160,24 @@ int btf__add_str(struct btf *btf, const char *s)\n \t\treturn libbpf_err(off);\n \n \tbtf-\u003ehdr.str_len = strset__data_size(btf-\u003estrs_set);\n+\tbtf-\u003estrs_deduped = false;\n \n \treturn btf-\u003estart_str_off + off;\n }\n \n+int btf__add_str(struct btf *btf, const char *s)\n+{\n+\tint off;\n+\n+\tif (btf-\u003ebase_btf) {\n+\t\toff = btf__find_str(btf-\u003ebase_btf, s);\n+\t\tif (off != -ENOENT)\n+\t\t\treturn off;\n+\t}\n+\n+\treturn btf_add_local_str(btf, s);\n+}\n+\n static void *btf_add_type_mem(struct btf *btf, size_t add_sz)\n {\n \treturn libbpf_add_mem(\u0026btf-\u003etypes_data, \u0026btf-\u003etypes_data_cap, 1,\n@@ -2162,6 +2225,7 @@ struct btf_pipe {\n \tconst struct btf *src;\n \tstruct btf *dst;\n \tstruct hashmap *str_off_map; /* map string offsets from src to dst */\n+\tbool force_local_strs;\n };\n \n static int btf_rewrite_str(struct btf_pipe *p, __u32 *str_off)\n@@ -2178,7 +2242,10 @@ static int btf_rewrite_str(struct btf_pipe *p, __u32 *str_off)\n \t\treturn 0;\n \t}\n \n-\toff = btf__add_str(p-\u003edst, btf__str_by_offset(p-\u003esrc, *str_off));\n+\tif (p-\u003eforce_local_strs)\n+\t\toff = btf_add_local_str(p-\u003edst, btf__str_by_offset(p-\u003esrc, *str_off));\n+\telse\n+\t\toff = btf__add_str(p-\u003edst, btf__str_by_offset(p-\u003esrc, *str_off));\n \tif (off \u003c 0)\n \t\treturn off;\n \n@@ -3344,6 +3411,208 @@ int btf__add_decl_attr(struct btf *btf, const char *value, int ref_type_id,\n \treturn btf_add_decl_tag(btf, value, ref_type_id, component_idx, 1);\n }\n \n+/*\n+ * Append new BTF_KIND_LOC_PARAM with specified size and flags. Values are\n+ * added via btf__add_loc_param_value().\n+ *\n+ * Returns:\n+ * - \u003e0, type ID of newly added BTF type;\n+ * - \u003c0, on error.\n+ */\n+int btf__add_loc_param(struct btf *btf, __u32 size, __u32 flags)\n+{\n+\tstruct btf_loc_param *p;\n+\tstruct btf_type *t;\n+\tint sz, err;\n+\n+\terr = btf_ensure_modifiable(btf);\n+\tif (err)\n+\t\treturn libbpf_err(err);\n+\n+\tsz = sizeof(struct btf_type) + sizeof(*p);\n+\tt = btf_add_type_mem(btf, sz);\n+\tif (!t)\n+\t\treturn libbpf_err(-ENOMEM);\n+\n+\tt-\u003ename_off = 0;\n+\tt-\u003einfo = btf_type_info(BTF_KIND_LOC_PARAM, 0, 0);\n+\tt-\u003esize = size;\n+\n+\tp = btf_loc_param(t);\n+\tp-\u003eflags = flags;\n+\n+\treturn btf_commit_type(btf, sz);\n+}\n+\n+int btf__add_loc_param_value(struct btf *btf, __u32 value)\n+{\n+\tstruct btf_type *t;\n+\tint sz, err;\n+\t__u32 *v;\n+\n+\t/* last type should be BTF_KIND_LOC_PARAM */\n+\tif (btf-\u003enr_types == 0)\n+\t\treturn libbpf_err(-EINVAL);\n+\tt = btf_last_type(btf);\n+\tif (!btf_is_loc_param(t))\n+\t\treturn libbpf_err(-EINVAL);\n+\n+\t/* decompose and invalidate raw data */\n+\terr = btf_ensure_modifiable(btf);\n+\tif (err)\n+\t\treturn libbpf_err(err);\n+\n+\tsz = sizeof(value);\n+\tv = btf_add_type_mem(btf, sz);\n+\tif (!v)\n+\t\treturn libbpf_err(-ENOMEM);\n+\t*v = value;\n+\n+\t/* update parent type's vlen */\n+\tt = btf_last_type(btf);\n+\terr = btf_type_inc_vlen(t);\n+\tif (err)\n+\t\treturn libbpf_err(err);\n+\n+\tbtf_hdr_update_type_len(btf, btf-\u003ehdr.type_len + sz);\n+\treturn 0;\n+}\n+\n+/*\n+ * Append new BTF_KIND_LOC_PROTO\n+ *\n+ * The prototype is then populated with 0 or more BTF_KIND_LOC_PARAMs via\n+ * btf__add_loc_proto_param(); similar to how btf__add_func_param() adds\n+ * parameters to a FUNC_PROTO.\n+ *\n+ * Returns:\n+ * - \u003e0, type ID of newly added BTF type;\n+ * - \u003c0, on error.\n+ */\n+int btf__add_loc_proto(struct btf *btf)\n+{\n+\tstruct btf_type *t;\n+\tint err;\n+\n+\terr = btf_ensure_modifiable(btf);\n+\tif (err)\n+\t\treturn libbpf_err(err);\n+\n+\tt = btf_add_type_mem(btf, sizeof(struct btf_type));\n+\tif (!t)\n+\t\treturn libbpf_err(-ENOMEM);\n+\n+\tt-\u003ename_off = 0;\n+\tt-\u003einfo = btf_type_info(BTF_KIND_LOC_PROTO, 0, 0);\n+\tt-\u003esize = 0;\n+\n+\treturn btf_commit_type(btf, sizeof(struct btf_type));\n+}\n+\n+int btf__add_loc_proto_param(struct btf *btf, __u32 id)\n+{\n+\tstruct btf_type *t;\n+\tint sz, err;\n+\t__u32 *p;\n+\n+\tif (validate_type_id(id))\n+\t\treturn libbpf_err(-EINVAL);\n+\n+\t/* last type should be BTF_KIND_LOC_PROTO */\n+\tif (btf-\u003enr_types == 0)\n+\t\treturn libbpf_err(-EINVAL);\n+\tt = btf_last_type(btf);\n+\tif (!btf_is_loc_proto(t))\n+\t\treturn libbpf_err(-EINVAL);\n+\n+\t/* decompose and invalidate raw data */\n+\terr = btf_ensure_modifiable(btf);\n+\tif (err)\n+\t\treturn libbpf_err(err);\n+\n+\tsz = sizeof(__u32);\n+\tp = btf_add_type_mem(btf, sz);\n+\tif (!p)\n+\t\treturn libbpf_err(-ENOMEM);\n+\t*p = id;\n+\n+\t/* update parent type's vlen */\n+\tt = btf_last_type(btf);\n+\terr = btf_type_inc_vlen(t);\n+\tif (err)\n+\t\treturn libbpf_err(err);\n+\n+\tbtf_hdr_update_type_len(btf, btf-\u003ehdr.type_len + sz);\n+\treturn 0;\n+}\n+\n+int btf__add_locsec(struct btf *btf, const char *name)\n+{\n+\tstruct btf_type *t;\n+\tint name_off = 0;\n+\tint err;\n+\n+\terr = btf_ensure_modifiable(btf);\n+\tif (err)\n+\t\treturn libbpf_err(err);\n+\n+\tt = btf_add_type_mem(btf, sizeof(struct btf_type));\n+\tif (!t)\n+\t\treturn libbpf_err(-ENOMEM);\n+\n+\tif (!str_is_empty(name)) {\n+\t\tname_off = btf__add_str(btf, name);\n+\t\tif (name_off \u003c 0)\n+\t\t\treturn name_off;\n+\t}\n+\tt-\u003ename_off = name_off;\n+\tt-\u003einfo = btf_type_info(BTF_KIND_LOCSEC, 0, 0);\n+\tt-\u003esize = 0;\n+\n+\treturn btf_commit_type(btf, sizeof(struct btf_type));\n+}\n+\n+int btf__add_locsec_loc(struct btf *btf, __u32 func, __u32 loc_proto,\n+\t\t\t__u32 offset)\n+{\n+\tstruct btf_type *t;\n+\tstruct btf_loc *l;\n+\tint sz, err;\n+\n+\tif (validate_type_id(func) || validate_type_id(loc_proto))\n+\t\treturn libbpf_err(-EINVAL);\n+\n+\t/* last type should be BTF_KIND_LOCSEC */\n+\tif (btf-\u003enr_types == 0)\n+\t\treturn libbpf_err(-EINVAL);\n+\tt = btf_last_type(btf);\n+\tif (!btf_is_locsec(t))\n+\t\treturn libbpf_err(-EINVAL);\n+\n+\t/* decompose and invalidate raw data */\n+\terr = btf_ensure_modifiable(btf);\n+\tif (err)\n+\t\treturn libbpf_err(err);\n+\n+\tsz = sizeof(*l);\n+\tl = btf_add_type_mem(btf, sz);\n+\tif (!l)\n+\t\treturn libbpf_err(-ENOMEM);\n+\n+\tl-\u003efunc = func;\n+\tl-\u003eloc_proto = loc_proto;\n+\tl-\u003eoffset = offset;\n+\n+\t/* update parent type's vlen */\n+\tt = btf_last_type(btf);\n+\terr = btf_type_inc_vlen(t);\n+\tif (err)\n+\t\treturn libbpf_err(err);\n+\n+\tbtf_hdr_update_type_len(btf, btf-\u003ehdr.type_len + sz);\n+\treturn 0;\n+}\n+\n struct btf_ext_sec_info_param {\n \t__u32 off;\n \t__u32 len;\n@@ -4114,8 +4383,8 @@ static struct btf_dedup *btf_dedup_new(struct btf *btf, const struct btf_dedup_o\n \tfor (i = 1; i \u003c type_cnt; i++) {\n \t\tstruct btf_type *t = btf_type_by_id(d-\u003ebtf, i);\n \n-\t\t/* VAR and DATASEC are never deduped and are self-canonical */\n-\t\tif (btf_is_var(t) || btf_is_datasec(t))\n+\t\t/* VAR, DATASEC and LOCSEC are never deduped and are self-canonical */\n+\t\tif (btf_is_var(t) || btf_is_datasec(t) || btf_is_locsec(t))\n \t\t\td-\u003emap[i] = i;\n \t\telse\n \t\t\td-\u003emap[i] = BTF_UNPROCESSED_ID;\n@@ -4255,6 +4524,19 @@ static int btf_dedup_strings(struct btf_dedup *d)\n \treturn err;\n }\n \n+/* Compact strings after a type-only transformation such as btf__permute(). */\n+static int btf_compact_strings(struct btf *btf, struct btf_ext *btf_ext)\n+{\n+\tstruct btf_dedup d = {\n+\t\t.btf = btf,\n+\t\t.btf_ext = btf_ext,\n+\t};\n+\n+\t/* Types may have been removed since the last string compaction. */\n+\tbtf-\u003estrs_deduped = false;\n+\treturn btf_dedup_strings(\u0026d);\n+}\n+\n /*\n * Calculate type signature hash of TYPEDEF, ignoring referenced type IDs,\n * as referenced type IDs equivalence is established separately during type\n@@ -4395,6 +4677,45 @@ static bool btf_compat_enum(struct btf_type *t1, struct btf_type *t2)\n \t btf_is_any_enum(t1) \u0026\u0026 btf_is_any_enum(t2);\n }\n \n+static long btf_hash_loc_param(struct btf_type *t)\n+{\n+\tlong h = btf_hash_common(t);\n+\t__u32 *v = (__u32 *)btf_loc_param(t);\n+\tint i, vlen = btf_vlen(t);\n+\n+\tfor (i = 0; i \u003c= vlen; i++, v++)\n+\t\th = hash_combine(h, *v);\n+\treturn h;\n+}\n+\n+static long btf_hash_loc_proto(struct btf_type *t)\n+{\n+\t__u32 *p = btf_loc_proto_params(t);\n+\tlong h = btf_hash_common(t);\n+\tint i, vlen = btf_vlen(t);\n+\n+\tfor (i = 0; i \u003c vlen; i++, p++)\n+\t\th = hash_combine(h, *p);\n+\treturn h;\n+}\n+\n+static bool btf_equal_loc_param(struct btf_type *t1, struct btf_type *t2)\n+{\n+\tstruct btf_loc_param *p1 = btf_loc_param(t1);\n+\tstruct btf_loc_param *p2 = btf_loc_param(t2);\n+\t__u32 *v1 = (__u32 *)(p1 + 1);\n+\t__u32 *v2 = (__u32 *)(p2 + 1);\n+\tint i, vlen = btf_vlen(t1);\n+\n+\tif (!btf_equal_common(t1, t2))\n+\t\treturn false;\n+\tfor (i = 0; i \u003c vlen; i++, v1++, v2++) {\n+\t\tif (*v1 != *v2)\n+\t\t\treturn false;\n+\t}\n+\treturn true;\n+}\n+\n /*\n * Calculate type signature hash of STRUCT/UNION, ignoring referenced type IDs,\n * as referenced type IDs equivalence is established separately during type\n@@ -4622,6 +4943,12 @@ static int btf_dedup_prep(struct btf_dedup *d)\n \t\tcase BTF_KIND_FUNC_PROTO:\n \t\t\th = btf_hash_fnproto(t);\n \t\t\tbreak;\n+\t\tcase BTF_KIND_LOC_PARAM:\n+\t\t\th = btf_hash_loc_param(t);\n+\t\t\tbreak;\n+\t\tcase BTF_KIND_LOC_PROTO:\n+\t\t\th = btf_hash_loc_proto(t);\n+\t\t\tbreak;\n \t\tdefault:\n \t\t\tpr_debug(\"unknown kind %d for type [%d]\\n\", btf_kind(t), type_id);\n \t\t\treturn -EINVAL;\n@@ -4664,6 +4991,8 @@ static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)\n \tcase BTF_KIND_DATASEC:\n \tcase BTF_KIND_DECL_TAG:\n \tcase BTF_KIND_TYPE_TAG:\n+\tcase BTF_KIND_LOC_PROTO:\n+\tcase BTF_KIND_LOCSEC:\n \t\treturn 0;\n \n \tcase BTF_KIND_INT:\n@@ -4713,6 +5042,18 @@ static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)\n \t\t}\n \t\tbreak;\n \n+\tcase BTF_KIND_LOC_PARAM:\n+\t\th = btf_hash_loc_param(t);\n+\t\tfor_each_dedup_cand(d, hash_entry, h) {\n+\t\t\tcand_id = hash_entry-\u003evalue;\n+\t\t\tcand = btf_type_by_id(d-\u003ebtf, cand_id);\n+\t\t\tif (btf_equal_loc_param(t, cand)) {\n+\t\t\t\tnew_id = cand_id;\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t}\n+\t\tbreak;\n+\n \tdefault:\n \t\treturn -EINVAL;\n \t}\n@@ -5145,6 +5486,13 @@ static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id,\n \t\treturn 1;\n \t}\n \n+\tcase BTF_KIND_LOC_PARAM:\n+\t\treturn btf_equal_loc_param(cand_type, canon_type);\n+\n+\tcase BTF_KIND_LOC_PROTO:\n+\tcase BTF_KIND_LOCSEC:\n+\t\treturn 0;\n+\n \tdefault:\n \t\treturn -EINVAL;\n \t}\n@@ -5489,6 +5837,41 @@ static int btf_dedup_ref_type(struct btf_dedup *d, __u32 type_id)\n \t\tbreak;\n \t}\n \n+\tcase BTF_KIND_LOC_PROTO: {\n+\t\t__u32 *p1, *p2;\n+\t\t__u32 i, vlen;\n+\n+\t\tp1 = btf_loc_proto_params(t);\n+\t\tvlen = btf_vlen(t);\n+\n+\t\tfor (i = 0; i \u003c vlen; i++, p1++) {\n+\t\t\tref_type_id = btf_dedup_ref_type(d, *p1);\n+\t\t\tif (ref_type_id \u003c 0)\n+\t\t\t\treturn ref_type_id;\n+\t\t\t*p1 = ref_type_id;\n+\t\t}\n+\n+\t\th = btf_hash_loc_proto(t);\n+\t\tfor_each_dedup_cand(d, hash_entry, h) {\n+\t\t\tcand_id = hash_entry-\u003evalue;\n+\t\t\tcand = btf_type_by_id(d-\u003ebtf, cand_id);\n+\t\t\tif (!btf_equal_common(t, cand))\n+\t\t\t\tcontinue;\n+\t\t\tvlen = btf_vlen(cand);\n+\t\t\tp1 = btf_loc_proto_params(t);\n+\t\t\tp2 = btf_loc_proto_params(cand);\n+\t\t\tif (vlen == 0) {\n+\t\t\t\tnew_id = cand_id;\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t\tif (memcmp(p1, p2, vlen * sizeof(__u32)) == 0) {\n+\t\t\t\tnew_id = cand_id;\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t}\n+\t\tbreak;\n+\t}\n+\n \tdefault:\n \t\treturn -EINVAL;\n \t}\n@@ -5970,8 +6353,11 @@ static int btf_add_distilled_type_ids(struct btf_distill *dist, __u32 i)\n \t\tcase BTF_KIND_CONST:\n \t\tcase BTF_KIND_RESTRICT:\n \t\tcase BTF_KIND_VOLATILE:\n+\t\tcase BTF_KIND_FUNC:\n \t\tcase BTF_KIND_FUNC_PROTO:\n \t\tcase BTF_KIND_TYPE_TAG:\n+\t\tcase BTF_KIND_LOC_PARAM:\n+\t\tcase BTF_KIND_LOC_PROTO:\n \t\t\tdist-\u003eid_map[*id] = *id;\n \t\t\tbreak;\n \t\tdefault:\n@@ -5997,7 +6383,7 @@ static int btf_add_distilled_type_ids(struct btf_distill *dist, __u32 i)\n \n static int btf_add_distilled_types(struct btf_distill *dist)\n {\n-\tbool adding_to_base = dist-\u003epipe.dst-\u003estart_id == 1;\n+\tbool adding_to_base = dist-\u003epipe.dst-\u003ebase_btf == NULL;\n \tint id = btf__type_cnt(dist-\u003epipe.dst);\n \tstruct btf_type *t;\n \tint i, err = 0;\n@@ -6065,8 +6451,12 @@ static int btf_add_distilled_types(struct btf_distill *dist)\n \t\tcase BTF_KIND_CONST:\n \t\tcase BTF_KIND_RESTRICT:\n \t\tcase BTF_KIND_VOLATILE:\n+\t\tcase BTF_KIND_FUNC:\n \t\tcase BTF_KIND_FUNC_PROTO:\n \t\tcase BTF_KIND_TYPE_TAG:\n+\t\tcase BTF_KIND_LOC_PARAM:\n+\t\tcase BTF_KIND_LOC_PROTO:\n+\t\tcase BTF_KIND_LOCSEC:\n \t\t\t/* All other types are added to split BTF. */\n \t\t\tif (adding_to_base)\n \t\t\t\tcontinue;\n@@ -6240,7 +6630,7 @@ void btf_set_base_btf(struct btf *btf, const struct btf *base_btf)\n \n int btf__relocate(struct btf *btf, const struct btf *base_btf)\n {\n-\tint err = btf_relocate(btf, base_btf, NULL);\n+\tint err = btf_relocate(btf, base_btf, NULL, NULL);\n \n \tif (!err)\n \t\tbtf-\u003eowns_base = false;\n@@ -6251,8 +6641,15 @@ struct btf_permute {\n \tstruct btf *btf;\n \t__u32 *id_map;\n \t__u32 start_offs;\n+\t__u32 transfer_start_id;\n+\tbool reject_transfer;\n };\n \n+static bool btf_permute_id_is_transfer(__u32 id)\n+{\n+\treturn id \u0026 BTF_PERMUTE_ID_TRANSFER;\n+}\n+\n /* Callback function to remap individual type ID references */\n static int btf_permute_remap_type_id(__u32 *type_id, void *ctx)\n {\n@@ -6267,18 +6664,54 @@ static int btf_permute_remap_type_id(__u32 *type_id, void *ctx)\n \t\treturn -EINVAL;\n \n \t*type_id = p-\u003eid_map[new_id - p-\u003ebtf-\u003estart_id + p-\u003estart_offs];\n+\tif (p-\u003ereject_transfer \u0026\u0026 *type_id \u003e= p-\u003etransfer_start_id)\n+\t\treturn -EINVAL;\n+\treturn 0;\n+}\n+\n+/*\n+ * String offsets in a split BTF are absolute. If its parent string table is\n+ * compacted after the split was populated, move its local string offsets to\n+ * start at the parent's new end.\n+ */\n+static int btf_permute_rebase_split_strs(struct btf *split_btf, __u32 old_start_str_off)\n+{\n+\t__s64 delta = split_btf-\u003ebase_btf-\u003estart_str_off + split_btf-\u003ebase_btf-\u003ehdr.str_len -\n+\t\t old_start_str_off;\n+\tint i;\n+\n+\tfor (i = 0; i \u003c split_btf-\u003enr_types; i++) {\n+\t\tstruct btf_field_iter it;\n+\t\tstruct btf_type *t = btf_type_by_id(split_btf,\n+\t\t\t\t\t\t split_btf-\u003estart_id + i);\n+\t\t__u32 *str_off;\n+\t\tint err;\n+\n+\t\terr = btf_field_iter_init(\u0026it, t, BTF_FIELD_ITER_STRS);\n+\t\tif (err)\n+\t\t\treturn err;\n+\t\twhile ((str_off = btf_field_iter_next(\u0026it))) {\n+\t\t\tif (*str_off \u003e= old_start_str_off)\n+\t\t\t\t*str_off += delta;\n+\t\t}\n+\t}\n+\n+\tbtf_set_base_btf(split_btf, split_btf-\u003ebase_btf);\n \treturn 0;\n }\n \n int btf__permute(struct btf *btf, __u32 *id_map, __u32 id_map_cnt,\n \t\t const struct btf_permute_opts *opts)\n {\n-\tstruct btf_permute p;\n-\tstruct btf_ext *btf_ext;\n+\t__u32 n, start_offs = 0, nr_transfer = 0, nr_base_types;\n+\t__u32 next_base_id, next_transfer_id, old_str_off = 0;\n+\tstruct btf **transfer_btfp, *transfer_btf = NULL;\n+\t__u32 *new_id_map = NULL, *order_map = NULL;\n \tvoid *nt, *new_types = NULL;\n-\t__u32 *order_map = NULL;\n+\tstruct btf_ext *btf_ext;\n+\tbool committed = false;\n+\tstruct btf_permute p;\n \tint err = 0, i;\n-\t__u32 n, id, start_offs = 0;\n \n \tif (!OPTS_VALID(opts, btf_permute_opts))\n \t\treturn libbpf_err(-EINVAL);\n@@ -6295,7 +6728,21 @@ int btf__permute(struct btf *btf, __u32 *id_map, __u32 id_map_cnt,\n \tif (id_map_cnt != n)\n \t\treturn libbpf_err(-EINVAL);\n \n-\t/* record the sequence of types */\n+\ttransfer_btfp = OPTS_GET(opts, transfer_btf, NULL);\n+\tif (transfer_btfp)\n+\t\t*transfer_btfp = NULL;\n+\n+\t/*\n+\t * Make a working copy of the id map; it will be modified (such as\n+\t * removing transfer flags) unlike the caller-passed original.\n+\t */\n+\tnew_id_map = calloc(id_map_cnt, sizeof(*new_id_map));\n+\tif (!new_id_map) {\n+\t\terr = -ENOMEM;\n+\t\tgoto done;\n+\t}\n+\tmemcpy(new_id_map, id_map, id_map_cnt * sizeof(*new_id_map));\n+\n \torder_map = calloc(id_map_cnt, sizeof(*id_map));\n \tif (!order_map) {\n \t\terr = -ENOMEM;\n@@ -6312,33 +6759,82 @@ int btf__permute(struct btf *btf, __u32 *id_map, __u32 id_map_cnt,\n \tif (err)\n \t\tgoto done;\n \n+\t/*\n+\t * Build the inverse map: for each requested destination ID, record the\n+\t * original type ID that will occupy it. order_map is indexed by the\n+\t * destination ID relative to the first ID represented by id_map. The\n+\t * caller id_map maps old IDs to requested destinations, but BTF types\n+\t * must be emitted in destination-ID order; the inverse map supplies the\n+\t * old type to copy for each output slot without repeatedly searching\n+ * id_map.\n+ */\n \tfor (i = start_offs; i \u003c id_map_cnt; i++) {\n-\t\tid = id_map[i];\n-\t\tif (id \u003c btf-\u003estart_id || id \u003e= btf__type_cnt(btf)) {\n+\t\t__u32 requested_id = new_id_map[i];\n+\t\t__u32 order_idx;\n+\n+\t\tif (btf_permute_id_is_transfer(requested_id)) {\n+\t\t\tif (!transfer_btfp) {\n+\t\t\t\terr = -EINVAL;\n+\t\t\t\tgoto done;\n+\t\t\t}\n+\t\t\trequested_id \u0026= ~BTF_PERMUTE_ID_TRANSFER;\n+\t\t\tnr_transfer++;\n+\t\t}\n+\t\tif (requested_id \u003c btf-\u003estart_id || requested_id \u003e= btf__type_cnt(btf)) {\n \t\t\terr = -EINVAL;\n \t\t\tgoto done;\n \t\t}\n-\t\tid -= btf-\u003estart_id - start_offs;\n+\t\torder_idx = requested_id - btf-\u003estart_id + start_offs;\n \t\t/* cannot be mapped to the same ID */\n-\t\tif (order_map[id]) {\n+\t\tif (order_map[order_idx]) {\n \t\t\terr = -EINVAL;\n \t\t\tgoto done;\n \t\t}\n-\t\torder_map[id] = i + btf-\u003estart_id - start_offs;\n+\t\torder_map[order_idx] = i + btf-\u003estart_id - start_offs;\n \t}\n \n+\t/*\n+\t * Turn requested destination IDs into final IDs. Retained types\n+\t * occupy the compacted parent BTF first; transferred types immediately\n+\t * follow them, which is where they will start in returned split BTF.\n+\t */\n+\tnr_base_types = n - nr_transfer - start_offs;\n+\tnext_base_id = btf-\u003estart_id;\n+\tnext_transfer_id = btf-\u003estart_id + nr_base_types;\n+\tfor (i = start_offs; i \u003c id_map_cnt; i++) {\n+\t\t__u32 old_id = order_map[i];\n+\t\t__u32 old_id_idx = old_id - btf-\u003estart_id + start_offs;\n+\n+\t\tif (btf_permute_id_is_transfer(new_id_map[old_id_idx]))\n+\t\t\tnew_id_map[old_id_idx] = next_transfer_id++;\n+\t\telse\n+\t\t\tnew_id_map[old_id_idx] = next_base_id++;\n+\t}\n+\n+\t/*\n+\t * Copy retained types and reject references into the split (transfer)\n+\t * BTF from the original BTF.\n+\t */\n \tp.btf = btf;\n-\tp.id_map = id_map;\n+\tp.id_map = new_id_map;\n \tp.start_offs = start_offs;\n+\tp.transfer_start_id = btf-\u003estart_id + nr_base_types;\n+\tp.reject_transfer = true;\n \tnt = new_types;\n \tfor (i = start_offs; i \u003c id_map_cnt; i++) {\n \t\tstruct btf_field_iter it;\n \t\tconst struct btf_type *t;\n \t\t__u32 *type_id;\n+\t\t__u32 old_id;\n+\t\t__u32 old_id_idx;\n \t\tint type_size;\n \n-\t\tid = order_map[i];\n-\t\tt = btf__type_by_id(btf, id);\n+\t\told_id = order_map[i];\n+\t\told_id_idx = old_id - btf-\u003estart_id + start_offs;\n+\n+\t\tif (new_id_map[old_id_idx] \u003e= btf-\u003estart_id + nr_base_types)\n+\t\t\tcontinue;\n+\t\tt = btf__type_by_id(btf, old_id);\n \t\ttype_size = btf_type_size(btf, t);\n \t\tmemcpy(nt, t, type_size);\n \n@@ -6348,14 +6844,71 @@ int btf__permute(struct btf *btf, __u32 *id_map, __u32 id_map_cnt,\n \t\t\tgoto done;\n \t\twhile ((type_id = btf_field_iter_next(\u0026it))) {\n \t\t\terr = btf_permute_remap_type_id(type_id, \u0026p);\n-\t\t\tif (err)\n+\t\t\tif (err \u003c 0)\n \t\t\t\tgoto done;\n \t\t}\n \n \t\tnt += type_size;\n \t}\n \n-\t/* fix up referenced IDs for btf_ext */\n+\t/*\n+\t * Copy transferred types into a split BTF. They can reference both\n+\t * retained parent types and other transferred types, so do not reject\n+\t * final IDs in the split-ID range while rewriting their references.\n+\t */\n+\tif (nr_transfer) {\n+\t\tstruct btf_permute transfer_p;\n+\n+\t\ttransfer_btf = btf__new_empty_split(btf);\n+\t\tif (!transfer_btf) {\n+\t\t\terr = -errno;\n+\t\t\tgoto done;\n+\t\t}\n+\t\told_str_off = transfer_btf-\u003estart_str_off;\n+\n+\t\ttransfer_p = p;\n+\t\t/* Split transfer types may refer to other split types */\n+\t\ttransfer_p.reject_transfer = false;\n+\t\tfor (i = start_offs; i \u003c id_map_cnt; i++) {\n+\t\t\tstruct btf_field_iter it;\n+\t\t\tstruct btf_pipe pipe = {\n+\t\t\t\t.src = btf,\n+\t\t\t\t.dst = transfer_btf,\n+\t\t\t\t.force_local_strs = true,\n+\t\t\t};\n+\t\t\tconst struct btf_type *t;\n+\t\t\tstruct btf_type *new_t;\n+\t\t\t__u32 *type_id;\n+\t\t\t__u32 old_id;\n+\t\t\t__u32 old_id_idx;\n+\t\t\tint new_id;\n+\n+\t\t\told_id = order_map[i];\n+\t\t\told_id_idx = old_id - btf-\u003estart_id + start_offs;\n+\n+\t\t\tif (!btf_permute_id_is_transfer(id_map[old_id_idx]))\n+\t\t\t\tcontinue;\n+\t\t\tt = btf__type_by_id(btf, old_id);\n+\t\t\tnew_id = btf_add_type(\u0026pipe, t);\n+\t\t\tif (new_id \u003c 0) {\n+\t\t\t\terr = new_id;\n+\t\t\t\tgoto done;\n+\t\t\t}\n+\t\t\tnew_t = btf_type_by_id(transfer_btf, new_id);\n+\t\t\terr = btf_field_iter_init(\u0026it, new_t, BTF_FIELD_ITER_IDS);\n+\t\t\tif (!err) {\n+\t\t\t\twhile ((type_id = btf_field_iter_next(\u0026it))) {\n+\t\t\t\t\terr = btf_permute_remap_type_id(type_id, \u0026transfer_p);\n+\t\t\t\t\tif (err)\n+\t\t\t\t\t\tbreak;\n+\t\t\t\t}\n+\t\t\t}\n+\t\t\tif (err \u003c 0)\n+\t\t\t\tgoto done;\n+\t\t}\n+\t}\n+\n+\t/* Rewrite optional .BTF.ext references to retained type IDs. */\n \tbtf_ext = OPTS_GET(opts, btf_ext, NULL);\n \tif (btf_ext) {\n \t\terr = btf_ext_visit_type_ids(btf_ext, btf_permute_remap_type_id, \u0026p);\n@@ -6363,18 +6916,58 @@ int btf__permute(struct btf *btf, __u32 *id_map, __u32 id_map_cnt,\n \t\t\tgoto done;\n \t}\n \n-\tfor (nt = new_types, i = 0; i \u003c id_map_cnt - start_offs; i++) {\n+\t/* Install the compacted parent types and rebuild their index. */\n+\tfor (nt = new_types, i = 0; i \u003c nr_base_types; i++) {\n \t\tbtf-\u003etype_offs[i] = nt - new_types;\n \t\tnt += btf_type_size(btf, nt);\n \t}\n-\n-\tfree(order_map);\n \tfree(btf-\u003etypes_data);\n \tbtf-\u003etypes_data = new_types;\n+\tcommitted = true;\n+\tbtf_hdr_update_type_len(btf, nt - new_types);\n+\tbtf-\u003enr_types = nr_base_types;\n+\n+\t/*\n+\t * An important goal is to also transfer strings to the transfer\n+\t * BTF where they do not have duplicates in BTF. This is important\n+\t * for cases like inline BTF where inline function names could\n+\t * comprise a significant portion of the string table. The approach\n+\t * is to first compact the set of strings in the base now transfer\n+\t * is complete; this will remove unreferenced strings. Then\n+\t * local references in the transfer BTF have to be moved downwards\n+\t * based upon that compaction. Finally also compact transfer BTF\n+\t * references so that we replace duplicate references in split with\n+\t * base references where present.\n+\t *\n+\t * Once all this is done, we end up with transfer-only strings in\n+\t * transfer split BTF and shared strings in BTF. The result is\n+\t * a transfer BTF with a lot of transfer-only strings will not\n+\t * pollute the string table of the non-transfer BTF.\n+\t */\n+\tif (transfer_btf) {\n+\t\terr = btf_compact_strings(btf, btf_ext);\n+\t\tif (err)\n+\t\t\tgoto done;\n+\t\terr = btf_permute_rebase_split_strs(transfer_btf, old_str_off);\n+\t\tif (err)\n+\t\t\tgoto done;\n+\t\terr = btf_compact_strings(transfer_btf, NULL);\n+\t\tif (err)\n+\t\t\tgoto done;\n+\t\t*transfer_btfp = transfer_btf;\n+\t}\n+\n+\t/* On success return the final old-to-new type-ID map to the caller. */\n+\tmemcpy(id_map, new_id_map, id_map_cnt * sizeof(*id_map));\n+\tfree(new_id_map);\n+\tfree(order_map);\n \treturn 0;\n \n done:\n+\tfree(new_id_map);\n \tfree(order_map);\n-\tfree(new_types);\n+\tif (!committed)\n+\t\tfree(new_types);\n+\tbtf__free(transfer_btf);\n \treturn libbpf_err(err);\n }\ndiff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h\nindex 587172c0de089..8ce5697557734 100644\n--- a/tools/lib/bpf/btf.h\n+++ b/tools/lib/bpf/btf.h\n@@ -274,6 +274,20 @@ LIBBPF_API int btf__add_decl_tag(struct btf *btf, const char *value, int ref_typ\n LIBBPF_API int btf__add_decl_attr(struct btf *btf, const char *value, int ref_type_id,\n \t\t\t\t int component_idx);\n \n+/* location construction APIs */\n+LIBBPF_API int btf__add_loc_param(struct btf *btf, __u32 size, __u32 flags);\n+\n+LIBBPF_API int btf__add_loc_param_value(struct btf *btf, __u32 value);\n+\n+LIBBPF_API int btf__add_loc_proto(struct btf *btf);\n+\n+LIBBPF_API int btf__add_loc_proto_param(struct btf *btf, __u32 id);\n+\n+LIBBPF_API int btf__add_locsec(struct btf *btf, const char *name);\n+\n+LIBBPF_API int btf__add_locsec_loc(struct btf *btf, __u32 func, __u32 loc_proto,\n+\t\t\t\t __u32 offset);\n+\n struct btf_dedup_opts {\n \tsize_t sz;\n \t/* optional .BTF.ext info to dedup along the main BTF info */\n@@ -307,9 +321,17 @@ struct btf_permute_opts {\n \tsize_t sz;\n \t/* optional .BTF.ext info along the main BTF info */\n \tstruct btf_ext *btf_ext;\n+\t/*\n+\t * If set, types whose map entry has BTF_PERMUTE_ID_TRANSFER set are\n+\t * removed from @btf and returned in a new split BTF based on @btf.\n+\t */\n+\tstruct btf **transfer_btf;\n \tsize_t :0;\n };\n-#define btf_permute_opts__last_field btf_ext\n+#define btf_permute_opts__last_field transfer_btf\n+\n+/* Mark an id_map entry as a type to be moved to .transfer_btf. */\n+#define BTF_PERMUTE_ID_TRANSFER (1U \u003c\u003c 31)\n \n /**\n * @brief **btf__permute()** rearranges BTF types in-place according to a specified ID mapping\n@@ -335,6 +357,12 @@ struct btf_permute_opts {\n * - Mapping is defined as `id_map[original_id - start_id] = new_id`\n * - `start_id` equals `btf__type_cnt(btf__base_btf(btf))`\n *\n+ * An @id_map entry can be ORed with BTF_PERMUTE_ID_TRANSFER to move that\n+ * type to a newly-created split BTF returned through @opts-\u003etransfer_btf.\n+ * The low bits still specify the type's position in the requested ordering.\n+ * The id map is updated to contain the final IDs in the base and returned\n+ * split BTF.\n+ *\n * After permutation, all type references within the BTF data and optional\n * BTF extension (if provided via @opts) are updated automatically.\n *\n@@ -431,6 +459,12 @@ btf_dump__dump_type_data(struct btf_dump *d, __u32 id,\n #define BTF_KIND_DECL_TAG\t17\t/* Decl Tag */\n #define BTF_KIND_TYPE_TAG\t18\t/* Type Tag */\n #define BTF_KIND_ENUM64\t\t19\t/* Enum for up-to 64bit values */\n+#define BTF_KIND_LOC_PARAM\t20\t/* Parameter at location */\n+#define BTF_KIND_LOC_PROTO\t21\t/* Parameter set at location */\n+#define BTF_KIND_LOCSEC\t\t22\t/* Section containing location info */\n+\n+struct btf_loc_param;\n+struct btf_loc;\n \n static inline __u16 btf_kind(const struct btf_type *t)\n {\n@@ -569,6 +603,21 @@ static inline bool btf_is_any_enum(const struct btf_type *t)\n \treturn btf_is_enum(t) || btf_is_enum64(t);\n }\n \n+static inline bool btf_is_loc_param(const struct btf_type *t)\n+{\n+\treturn btf_kind(t) == BTF_KIND_LOC_PARAM;\n+}\n+\n+static inline bool btf_is_loc_proto(const struct btf_type *t)\n+{\n+\treturn btf_kind(t) == BTF_KIND_LOC_PROTO;\n+}\n+\n+static inline bool btf_is_locsec(const struct btf_type *t)\n+{\n+\treturn btf_kind(t) == BTF_KIND_LOCSEC;\n+}\n+\n static inline bool btf_kind_core_compat(const struct btf_type *t1,\n \t\t\t\t\tconst struct btf_type *t2)\n {\n@@ -683,6 +732,21 @@ static inline struct btf_decl_tag *btf_decl_tag(const struct btf_type *t)\n \treturn (struct btf_decl_tag *)(t + 1);\n }\n \n+static inline struct btf_loc_param *btf_loc_param(const struct btf_type *t)\n+{\n+\treturn (struct btf_loc_param *)(t + 1);\n+}\n+\n+static inline __u32 *btf_loc_proto_params(const struct btf_type *t)\n+{\n+\treturn (__u32 *)(t + 1);\n+}\n+\n+static inline struct btf_loc *btf_locsec_locs(const struct btf_type *t)\n+{\n+\treturn (struct btf_loc *)(t + 1);\n+}\n+\n #ifdef __cplusplus\n } /* extern \"C\" */\n #endif\ndiff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c\nindex 123c448f20c74..fa995c02a1705 100644\n--- a/tools/lib/bpf/btf_dump.c\n+++ b/tools/lib/bpf/btf_dump.c\n@@ -328,6 +328,9 @@ static int btf_dump_mark_referenced(struct btf_dump *d)\n \t\tcase BTF_KIND_ENUM64:\n \t\tcase BTF_KIND_FWD:\n \t\tcase BTF_KIND_FLOAT:\n+\t\tcase BTF_KIND_LOC_PARAM:\n+\t\tcase BTF_KIND_LOC_PROTO:\n+\t\tcase BTF_KIND_LOCSEC:\n \t\t\tbreak;\n \n \t\tcase BTF_KIND_VOLATILE:\n@@ -609,6 +612,9 @@ static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr)\n \tcase BTF_KIND_VAR:\n \tcase BTF_KIND_DATASEC:\n \tcase BTF_KIND_DECL_TAG:\n+\tcase BTF_KIND_LOC_PARAM:\n+\tcase BTF_KIND_LOC_PROTO:\n+\tcase BTF_KIND_LOCSEC:\n \t\td-\u003etype_states[id].order_state = ORDERED;\n \t\treturn 0;\n \n@@ -2525,6 +2531,9 @@ static int btf_dump_dump_type_data(struct btf_dump *d,\n \tcase BTF_KIND_FUNC:\n \tcase BTF_KIND_FUNC_PROTO:\n \tcase BTF_KIND_DECL_TAG:\n+\tcase BTF_KIND_LOC_PARAM:\n+\tcase BTF_KIND_LOC_PROTO:\n+\tcase BTF_KIND_LOCSEC:\n \t\terr = btf_dump_unsupported_data(d, t, id);\n \t\tbreak;\n \tcase BTF_KIND_INT:\ndiff --git a/tools/lib/bpf/btf_iter.c b/tools/lib/bpf/btf_iter.c\nindex 9a6c822c22944..54428e422ec39 100644\n--- a/tools/lib/bpf/btf_iter.c\n+++ b/tools/lib/bpf/btf_iter.c\n@@ -29,6 +29,7 @@ int btf_field_iter_init(struct btf_field_iter *it, struct btf_type *t,\n \t\tcase BTF_KIND_FLOAT:\n \t\tcase BTF_KIND_ENUM:\n \t\tcase BTF_KIND_ENUM64:\n+\t\tcase BTF_KIND_LOC_PARAM:\n \t\t\tit-\u003edesc = (struct btf_field_desc) {};\n \t\t\tbreak;\n \t\tcase BTF_KIND_FWD:\n@@ -71,6 +72,20 @@ int btf_field_iter_init(struct btf_field_iter *it, struct btf_type *t,\n \t\t\t\t1, {offsetof(struct btf_var_secinfo, type)}\n \t\t\t};\n \t\t\tbreak;\n+\t\tcase BTF_KIND_LOC_PROTO:\n+\t\t\tit-\u003edesc = (struct btf_field_desc) {\n+\t\t\t\t0, {},\n+\t\t\t\tsizeof(__u32),\n+\t\t\t\t1, {0}};\n+\t\t\tbreak;\n+\t\tcase BTF_KIND_LOCSEC:\n+\t\t\tit-\u003edesc = (struct btf_field_desc) {\n+\t\t\t\t0, {},\n+\t\t\t\tsizeof(struct btf_loc),\n+\t\t\t\t2, {offsetof(struct btf_loc, func),\n+\t\t\t\t offsetof(struct btf_loc, loc_proto)}};\n+\t\t\tbreak;\n+\n \t\tdefault:\n \t\t\treturn -EINVAL;\n \t\t}\n@@ -94,6 +109,8 @@ int btf_field_iter_init(struct btf_field_iter *it, struct btf_type *t,\n \t\tcase BTF_KIND_DECL_TAG:\n \t\tcase BTF_KIND_TYPE_TAG:\n \t\tcase BTF_KIND_DATASEC:\n+\t\tcase BTF_KIND_LOC_PARAM:\n+\t\tcase BTF_KIND_LOC_PROTO:\n \t\t\tit-\u003edesc = (struct btf_field_desc) {\n \t\t\t\t1, {offsetof(struct btf_type, name_off)}\n \t\t\t};\n@@ -127,6 +144,11 @@ int btf_field_iter_init(struct btf_field_iter *it, struct btf_type *t,\n \t\t\t\t1, {offsetof(struct btf_param, name_off)}\n \t\t\t};\n \t\t\tbreak;\n+\t\tcase BTF_KIND_LOCSEC:\n+\t\t\tit-\u003edesc = (struct btf_field_desc) {\n+\t\t\t\t1, {offsetof(struct btf_type, name_off)}\n+\t\t\t};\n+\t\t\tbreak;\n \t\tdefault:\n \t\t\treturn -EINVAL;\n \t\t}\ndiff --git a/tools/lib/bpf/btf_relocate.c b/tools/lib/bpf/btf_relocate.c\nindex df5fa4bd87d69..e55dd75f6c950 100644\n--- a/tools/lib/bpf/btf_relocate.c\n+++ b/tools/lib/bpf/btf_relocate.c\n@@ -441,7 +441,8 @@ static int btf_relocate_rewrite_strs(struct btf_relocate *r, __u32 i)\n /* If successful, output of relocation is updated BTF with base BTF pointing\n * at base_btf, and type ids, strings adjusted accordingly.\n */\n-int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **id_map)\n+int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **id_map,\n+\t\t __u32 **str_map)\n {\n \tunsigned int nr_types = btf__type_cnt(btf);\n \tconst struct btf_header *dist_base_hdr;\n@@ -512,6 +513,10 @@ int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **id_map)\n \t\t*id_map = r.id_map;\n \t\tr.id_map = NULL;\n \t}\n+\tif (str_map) {\n+\t\t*str_map = r.str_map;\n+\t\tr.str_map = NULL;\n+\t}\n err_out:\n \tfree(r.id_map);\n \tfree(r.str_map);\ndiff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map\nindex 08ab2ea881fb6..7f51783df129a 100644\n--- a/tools/lib/bpf/libbpf.map\n+++ b/tools/lib/bpf/libbpf.map\n@@ -460,6 +460,12 @@ LIBBPF_1.8.0 {\n \tglobal:\n \t\tbpf_program__attach_tracing_multi;\n \t\tbpf_program__clone;\n+\t\tbtf__add_loc_param;\n+\t\tbtf__add_loc_param_value;\n+\t\tbtf__add_loc_proto;\n+\t\tbtf__add_loc_proto_param;\n+\t\tbtf__add_locsec;\n+\t\tbtf__add_locsec_loc;\n \t\tbtf__find_by_name_kind_own;\n \t\tbtf__new_empty_opts;\n } LIBBPF_1.7.0;\ndiff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h\nindex cb4d962338441..e7a6219374a96 100644\n--- a/tools/lib/bpf/libbpf_internal.h\n+++ b/tools/lib/bpf/libbpf_internal.h\n@@ -249,7 +249,8 @@ const char *btf_kind_str(const struct btf_type *t);\n const struct btf_type *skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id);\n const struct btf_header *btf_header(const struct btf *btf);\n void btf_set_base_btf(struct btf *btf, const struct btf *base_btf);\n-int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **id_map);\n+int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **id_map,\n+\t\t __u32 **str_map);\n bool btf_type_is_traceable_func(const struct btf *btf, const struct btf_type *t);\n \n static inline enum btf_func_linkage btf_func_linkage(const struct btf_type *t)\n@@ -580,7 +581,7 @@ struct btf_field_desc {\n \t/* member struct size, or zero, if no members */\n \tint m_sz;\n \t/* repeated per-member offsets */\n-\tint m_off_cnt, m_offs[1];\n+\tint m_off_cnt, m_offs[22];\n };\n \n struct btf_field_iter {\ndiff --git a/tools/testing/selftests/bpf/btf_helpers.c b/tools/testing/selftests/bpf/btf_helpers.c\nindex 1c1c2c26690a7..6065ee4ba00a6 100644\n--- a/tools/testing/selftests/bpf/btf_helpers.c\n+++ b/tools/testing/selftests/bpf/btf_helpers.c\n@@ -27,11 +27,14 @@ static const char * const btf_kind_str_mapping[] = {\n \t[BTF_KIND_DECL_TAG]\t= \"DECL_TAG\",\n \t[BTF_KIND_TYPE_TAG]\t= \"TYPE_TAG\",\n \t[BTF_KIND_ENUM64]\t= \"ENUM64\",\n+\t[BTF_KIND_LOC_PARAM]\t= \"LOC_PARAM\",\n+\t[BTF_KIND_LOC_PROTO]\t= \"LOC_PROTO\",\n+\t[BTF_KIND_LOCSEC]\t= \"LOCSEC\",\n };\n \n static const char *btf_kind_str(__u16 kind)\n {\n-\tif (kind \u003e BTF_KIND_ENUM64)\n+\tif (kind \u003e BTF_KIND_LOCSEC)\n \t\treturn \"UNKNOWN\";\n \treturn btf_kind_str_mapping[kind];\n }\n@@ -203,6 +206,37 @@ int fprintf_btf_type_raw(FILE *out, const struct btf *btf, __u32 id)\n \t\tfprintf(out, \" type_id=%u component_idx=%d\",\n \t\t\tt-\u003etype, btf_decl_tag(t)-\u003ecomponent_idx);\n \t\tbreak;\n+\tcase BTF_KIND_LOC_PARAM: {\n+\t\tstruct btf_loc_param *p = btf_loc_param(t);\n+\t\t__u32 *v = (__u32 *)(p + 1);\n+\n+\t\tfprintf(out, \" size=%d flags=0x%x vlen=%u\", t-\u003esize, p-\u003eflags, vlen);\n+\t\tfor (i = 0; i \u003c vlen; i++, v++) {\n+\t\t\tif (p-\u003eflags \u0026 BTF_LOC_PARAM_SIGNED)\n+\t\t\t\tfprintf(out, \"\\n\\tvalue=%d\", (__s32)*v);\n+\t\t\telse\n+\t\t\t\tfprintf(out, \"\\n\\tvalue=%u\", *v);\n+\t\t}\n+\t\tbreak;\n+\t}\n+\tcase BTF_KIND_LOC_PROTO: {\n+\t\tconst __u32 *p = btf_loc_proto_params(t);\n+\n+\t\tfprintf(out, \" vlen=%u\", vlen);\n+\t\tfor (i = 0; i \u003c vlen; i++, p++)\n+\t\t\tfprintf(out, \"\\n\\ttype_id=%u\", *p);\n+\t\tbreak;\n+\t}\n+\tcase BTF_KIND_LOCSEC: {\n+\t\tconst struct btf_loc *l = btf_locsec_locs(t);\n+\n+\t\tfprintf(out, \" vlen=%u\", vlen);\n+\t\tfor (i = 0; i \u003c vlen; i++, l++) {\n+\t\t\tfprintf(out, \"\\n\\tfunc_type_id=%u loc_proto_type_id=%u offset=%d\",\n+\t\t\t\tl-\u003efunc, l-\u003eloc_proto, l-\u003eoffset);\n+\t\t}\n+\t\tbreak;\n+\t}\n \tdefault:\n \t\tbreak;\n \t}\ndiff --git a/tools/testing/selftests/bpf/prog_tests/btf_dedup_split.c b/tools/testing/selftests/bpf/prog_tests/btf_dedup_split.c\nindex 9d61611515938..34ca20f88afd2 100644\n--- a/tools/testing/selftests/bpf/prog_tests/btf_dedup_split.c\n+++ b/tools/testing/selftests/bpf/prog_tests/btf_dedup_split.c\n@@ -554,6 +554,113 @@ static void test_split_module(void)\n \tbtf__free(vmlinux_btf);\n }\n \n+static void test_split_loc(void)\n+{\n+\tstruct btf *btf1, *btf2;\n+\tint err;\n+\n+\tbtf1 = btf__new_empty();\n+\tif (!ASSERT_OK_PTR(btf1, \"empty_main_btf\"))\n+\t\treturn;\n+\n+\tbtf__set_pointer_size(btf1, 8); /* enforce 64-bit arch */\n+\n+\n+\tbtf__add_int(btf1, \"long\", 8, BTF_INT_SIGNED); /* [1] long */\n+\tbtf__add_ptr(btf1, 1); /* [2] ptr to long */\n+\tbtf__add_func_proto(btf1, 1);\t\t\t/* [3] long (*)(long, long *); */\n+\tbtf__add_func_param(btf1, \"p1\", 1);\n+\tbtf__add_func_param(btf1, \"p2\", 2);\n+\tbtf__add_func(btf1, \"foo\", BTF_FUNC_STATIC, 3);\t/* [4] long foo(long, long *); */\n+\tbtf__add_loc_param(btf1, 8, BTF_LOC_PARAM_CONST);\n+\tbtf__add_loc_param_value(btf1, 3735928559);\n+\tbtf__add_loc_param_value(btf1, 4277009102);\t/* [5] loc value */\n+\tbtf__add_loc_param(btf1, 8, BTF_LOC_PARAM_REG);\t/* [6] loc reg 1 */\n+\tbtf__add_loc_param_value(btf1, 1);\n+\tbtf__add_loc_proto(btf1);\t\t\t/* [7] loc proto */\n+\tbtf__add_loc_proto_param(btf1, 5);\t\t/* param value */\n+\tbtf__add_loc_proto_param(btf1, 6);\t\t/* param reg 1 */\n+\n+\tVALIDATE_RAW_BTF(\n+\t\tbtf1,\n+\t\t\"[1] INT 'long' size=8 bits_offset=0 nr_bits=64 encoding=SIGNED\",\n+\t\t\"[2] PTR '(anon)' type_id=1\",\n+\t\t\"[3] FUNC_PROTO '(anon)' ret_type_id=1 vlen=2\\n\"\n+\t\t\"\\t'p1' type_id=1\\n\"\n+\t\t\"\\t'p2' type_id=2\",\n+\t\t\"[4] FUNC 'foo' type_id=3 linkage=static\",\n+\t\t\"[5] LOC_PARAM '(anon)' size=8 flags=0x2 vlen=2\\n\"\n+\t\t\"\\tvalue=3735928559\\n\"\n+\t\t\"\\tvalue=4277009102\",\n+\t\t\"[6] LOC_PARAM '(anon)' size=8 flags=0x8 vlen=1\\n\"\n+\t\t\"\\tvalue=1\",\n+\t\t\"[7] LOC_PROTO '(anon)' vlen=2\\n\"\n+\t\t\"\\ttype_id=5\\n\"\n+\t\t\"\\ttype_id=6\");\n+\n+\tbtf2 = btf__new_empty_split(btf1);\n+\tif (!ASSERT_OK_PTR(btf2, \"empty_split_btf\"))\n+\t\tgoto cleanup;\n+\tbtf__add_loc_param(btf2, 8, BTF_LOC_PARAM_REG);\n+\tbtf__add_loc_param_value(btf2, 1);\t\t/* [8] loc reg 1 */\n+\tbtf__add_loc_proto(btf2);\t\t\t/* [9] loc proto */\n+\tbtf__add_loc_proto_param(btf2, 5);\t\t/* param value */\n+\tbtf__add_loc_proto_param(btf2, 8);\t\t/* param reg 1 */\n+\tbtf__add_locsec(btf2, \".locs\");\t\t\t/* [9] locsec \".locs\" */\n+\tbtf__add_locsec_loc(btf2, 4, 9, 128);\n+\n+\tVALIDATE_RAW_BTF(\n+\t\tbtf2,\n+\t\t\"[1] INT 'long' size=8 bits_offset=0 nr_bits=64 encoding=SIGNED\",\n+\t\t\"[2] PTR '(anon)' type_id=1\",\n+\t\t\"[3] FUNC_PROTO '(anon)' ret_type_id=1 vlen=2\\n\"\n+\t\t\"\\t'p1' type_id=1\\n\"\n+\t\t\"\\t'p2' type_id=2\",\n+\t\t\"[4] FUNC 'foo' type_id=3 linkage=static\",\n+\t\t\"[5] LOC_PARAM '(anon)' size=8 flags=0x2 vlen=2\\n\"\n+\t\t\"\\tvalue=3735928559\\n\"\n+\t\t\"\\tvalue=4277009102\",\n+\t\t\"[6] LOC_PARAM '(anon)' size=8 flags=0x8 vlen=1\\n\"\n+\t\t\"\\tvalue=1\",\n+\t\t\"[7] LOC_PROTO '(anon)' vlen=2\\n\"\n+\t\t\"\\ttype_id=5\\n\"\n+\t\t\"\\ttype_id=6\",\n+\t\t\"[8] LOC_PARAM '(anon)' size=8 flags=0x8 vlen=1\\n\"\n+\t\t\"\\tvalue=1\",\n+\t\t\"[9] LOC_PROTO '(anon)' vlen=2\\n\"\n+\t\t\"\\ttype_id=5\\n\"\n+\t\t\"\\ttype_id=8\",\n+\t\t\"[10] LOCSEC '.locs' vlen=1\\n\"\n+\t\t\"\\tfunc_type_id=4 loc_proto_type_id=9 offset=128\");\n+\n+\terr = btf__dedup(btf2, NULL);\n+\tif (!ASSERT_OK(err, \"btf_dedup\"))\n+\t\tgoto cleanup;\n+\n+\tVALIDATE_RAW_BTF(\n+\t\tbtf2,\n+\t\t\"[1] INT 'long' size=8 bits_offset=0 nr_bits=64 encoding=SIGNED\",\n+\t\t\"[2] PTR '(anon)' type_id=1\",\n+\t\t\"[3] FUNC_PROTO '(anon)' ret_type_id=1 vlen=2\\n\"\n+\t\t\"\\t'p1' type_id=1\\n\"\n+\t\t\"\\t'p2' type_id=2\",\n+\t\t\"[4] FUNC 'foo' type_id=3 linkage=static\",\n+\t\t\"[5] LOC_PARAM '(anon)' size=8 flags=0x2 vlen=2\\n\"\n+\t\t\"\\tvalue=3735928559\\n\"\n+\t\t\"\\tvalue=4277009102\",\n+\t\t\"[6] LOC_PARAM '(anon)' size=8 flags=0x8 vlen=1\\n\"\n+\t\t\"\\tvalue=1\",\n+\t\t\"[7] LOC_PROTO '(anon)' vlen=2\\n\"\n+\t\t\"\\ttype_id=5\\n\"\n+\t\t\"\\ttype_id=6\",\n+\t\t\"[8] LOCSEC '.locs' vlen=1\\n\"\n+\t\t\"\\tfunc_type_id=4 loc_proto_type_id=7 offset=128\");\n+\n+cleanup:\n+\tbtf__free(btf2);\n+\tbtf__free(btf1);\n+}\n+\n void test_btf_dedup_split()\n {\n \tif (test__start_subtest(\"split_simple\"))\n@@ -566,4 +673,6 @@ void test_btf_dedup_split()\n \t\ttest_split_dup_struct_in_cu();\n \tif (test__start_subtest(\"split_module\"))\n \t\ttest_split_module();\n+\tif (test__start_subtest(\"split_loc\"))\n+\t\ttest_split_loc();\n }\ndiff --git a/tools/testing/selftests/bpf/prog_tests/btf_distill.c b/tools/testing/selftests/bpf/prog_tests/btf_distill.c\nindex fb67ae195a738..f433b29ffce92 100644\n--- a/tools/testing/selftests/bpf/prog_tests/btf_distill.c\n+++ b/tools/testing/selftests/bpf/prog_tests/btf_distill.c\n@@ -671,6 +671,80 @@ static void test_distilled_base_embedded_err(void)\n \tbtf__free(btf1);\n }\n \n+/* LOC_PARAM, LOC_PROTO should be added to split BTF. */\n+static void test_distilled_loc(void)\n+{\n+\tstruct btf *btf1 = NULL, *btf2 = NULL, *btf3 = NULL, *btf4 = NULL;\n+\n+\tbtf1 = btf__new_empty();\n+\tif (!ASSERT_OK_PTR(btf1, \"empty_main_btf\"))\n+\t\treturn;\n+\n+\tbtf__add_int(btf1, \"int\", 4, BTF_INT_SIGNED);\t/* [1] int */\n+\tbtf__add_func_proto(btf1, 1); /* [2] int (*)(int); */\n+\tbtf__add_func_param(btf1, \"p1\", 1);\n+\tbtf__add_func(btf1, \"foo\", BTF_FUNC_STATIC, 2);\t/* [3] int foo(int); */\n+\tbtf__add_loc_param(btf1, 4, BTF_LOC_PARAM_SIGNED | BTF_LOC_PARAM_CONST);\n+\tbtf__add_loc_param_value(btf1, -1);\t\t/* [4] loc value */\n+\n+\tVALIDATE_RAW_BTF(\n+\t\tbtf1,\n+\t\t\"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED\",\n+\t\t\"[2] FUNC_PROTO '(anon)' ret_type_id=1 vlen=1\\n\"\n+\t\t\"\\t'p1' type_id=1\",\n+\t\t\"[3] FUNC 'foo' type_id=2 linkage=static\",\n+\t\t\"[4] LOC_PARAM '(anon)' size=4 flags=0x3 vlen=1\\n\"\n+\t\t\"\\tvalue=-1\");\n+\n+\tbtf2 = btf__new_empty_split(btf1);\n+\tif (!ASSERT_OK_PTR(btf2, \"empty_split_btf\"))\n+\t\tgoto cleanup;\n+\n+\tbtf__add_loc_proto(btf2);\t\t\t/* [5] loc proto */\n+\tbtf__add_loc_proto_param(btf2, 4);\t\t/* param value */\n+\n+\tbtf__add_locsec(btf2, \".locs\");\t\t\t/* [6] locsec */\n+\tbtf__add_locsec_loc(btf2, 3, 5, 256);\t\t/* \"foo\" offset 256 */\n+\tVALIDATE_RAW_BTF(\n+\t\tbtf2,\n+\t\t\"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED\",\n+\t\t\"[2] FUNC_PROTO '(anon)' ret_type_id=1 vlen=1\\n\"\n+\t\t\"\\t'p1' type_id=1\",\n+\t\t\"[3] FUNC 'foo' type_id=2 linkage=static\",\n+\t\t\"[4] LOC_PARAM '(anon)' size=4 flags=0x3 vlen=1\\n\"\n+\t\t\"\\tvalue=-1\",\n+\t\t\"[5] LOC_PROTO '(anon)' vlen=1\\n\"\n+\t\t\"\\ttype_id=4\",\n+\t\t\"[6] LOCSEC '.locs' vlen=1\\n\"\n+\t\t\"\\tfunc_type_id=3 loc_proto_type_id=5 offset=256\");\n+\n+\tif (!ASSERT_EQ(0, btf__distill_base(btf2, \u0026btf3, \u0026btf4),\n+\t\t \"distilled_base\") ||\n+\t !ASSERT_OK_PTR(btf3, \"distilled_base\") ||\n+\t !ASSERT_OK_PTR(btf4, \"distilled_split\") ||\n+\t !ASSERT_EQ(2, btf__type_cnt(btf3), \"distilled_base_type_cnt\"))\n+\t\tgoto cleanup;\n+\n+\tVALIDATE_RAW_BTF(\n+\t\tbtf4,\n+\t\t\"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED\",\n+\t\t/* remainder is split BTF */\n+\t\t\"[2] LOC_PROTO '(anon)' vlen=1\\n\"\n+\t\t\"\\ttype_id=6\",\n+\t\t\"[3] LOCSEC '.locs' vlen=1\\n\"\n+\t\t\"\\tfunc_type_id=5 loc_proto_type_id=2 offset=256\",\n+\t\t\"[4] FUNC_PROTO '(anon)' ret_type_id=1 vlen=1\\n\"\n+\t\t\"\\t'p1' type_id=1\",\n+\t\t\"[5] FUNC 'foo' type_id=4 linkage=static\",\n+\t\t\"[6] LOC_PARAM '(anon)' size=4 flags=0x3 vlen=1\\n\"\n+\t\t\"\\tvalue=-1\");\n+cleanup:\n+\tbtf__free(btf4);\n+\tbtf__free(btf3);\n+\tbtf__free(btf2);\n+\tbtf__free(btf1);\n+}\n+\n void test_btf_distill(void)\n {\n \tif (test__start_subtest(\"distilled_base\"))\n@@ -689,4 +763,6 @@ void test_btf_distill(void)\n \t\ttest_distilled_base_vmlinux();\n \tif (test__start_subtest(\"distilled_endianness\"))\n \t\ttest_distilled_endianness();\n+\tif (test__start_subtest(\"distilled_loc\"))\n+\t\ttest_distilled_loc();\n }\ndiff --git a/tools/testing/selftests/bpf/prog_tests/btf_field_iter.c b/tools/testing/selftests/bpf/prog_tests/btf_field_iter.c\nindex 32159d3eb281b..dcb5429d141df 100644\n--- a/tools/testing/selftests/bpf/prog_tests/btf_field_iter.c\n+++ b/tools/testing/selftests/bpf/prog_tests/btf_field_iter.c\n@@ -31,8 +31,11 @@ struct field_data {\n \t{ .ids = { 11 },\t.strs = { \"decltag\" } },\n \t{ .ids = { 6 },\t\t.strs = { \"typetag\" } },\n \t{ .ids = {},\t\t.strs = { \"e64\", \"eval1\", \"eval2\", \"eval3\" } },\n-\t{ .ids = { 15, 16 },\t.strs = { \"datasec1\" } }\n-\n+\t{ .ids = { 15, 16 },\t.strs = { \"datasec1\" } },\n+\t{ .ids = {},\t\t.strs = { \"\" } },\n+\t{ .ids = {},\t\t.strs = { \"\" } },\n+\t{ .ids = { 22, 23 },\t.strs = { \"\" } },\n+\t{ .ids = { 14, 24 },\t.strs = { \".loc\" } }\n };\n \n /* Fabricate BTF with various types and check BTF field iteration finds types,\n@@ -88,6 +91,19 @@ void test_btf_field_iter(void)\n \tbtf__add_datasec_var_info(btf, 15, 0, 4);\n \tbtf__add_datasec_var_info(btf, 16, 4, 8);\n \n+\tbtf__add_loc_param(btf, 4, BTF_LOC_PARAM_CONST | BTF_LOC_PARAM_SIGNED);\n+\t\t\t\t\t\t\t/* [22] loc value -1 */\n+\tbtf__add_loc_param_value(btf, -1);\n+\tbtf__add_loc_param(btf, 8, BTF_LOC_PARAM_REG);\t/* [23] loc reg 1 */\n+\tbtf__add_loc_param_value(btf, 1);\n+\n+\tbtf__add_loc_proto(btf);\t\t\t/* [24] loc proto */\n+\tbtf__add_loc_proto_param(btf, 22);\t\t/* param value -1, */\n+\tbtf__add_loc_proto_param(btf, 23);\t\t/* param reg 1 */\n+\n+\tbtf__add_locsec(btf, \".loc\");\t\t\t/* [25] locsec \".loc\" */\n+\tbtf__add_locsec_loc(btf, 14, 24, 128);\t\t/* \"func\" */\n+\n \tVALIDATE_RAW_BTF(\n \t\tbtf,\n \t\t\"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED\",\n@@ -123,7 +139,16 @@ void test_btf_field_iter(void)\n \t\t\"\\t'eval3' val=3000\",\n \t\t\"[21] DATASEC 'datasec1' size=12 vlen=2\\n\"\n \t\t\"\\ttype_id=15 offset=0 size=4\\n\"\n-\t\t\"\\ttype_id=16 offset=4 size=8\");\n+\t\t\"\\ttype_id=16 offset=4 size=8\",\n+\t\t\"[22] LOC_PARAM '(anon)' size=4 flags=0x3 vlen=1\\n\"\n+\t\t\"\\tvalue=-1\",\n+\t\t\"[23] LOC_PARAM '(anon)' size=8 flags=0x8 vlen=1\\n\"\n+\t\t\"\\tvalue=1\",\n+\t\t\"[24] LOC_PROTO '(anon)' vlen=2\\n\"\n+\t\t\"\\ttype_id=22\\n\"\n+\t\t\"\\ttype_id=23\",\n+\t\t\"[25] LOCSEC '.loc' vlen=1\\n\"\n+\t\t\"\\tfunc_type_id=14 loc_proto_type_id=24 offset=128\");\n \n \tfor (id = 1; id \u003c btf__type_cnt(btf); id++) {\n \t\tstruct btf_type *t = btf_type_by_id(btf, id);\ndiff --git a/tools/testing/selftests/bpf/prog_tests/btf_inline.c b/tools/testing/selftests/bpf/prog_tests/btf_inline.c\nnew file mode 100644\nindex 0000000000000..8e0a85f6c6980\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/prog_tests/btf_inline.c\n@@ -0,0 +1,110 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026, Oracle and/or its affiliates. */\n+\n+#include \u003ctest_progs.h\u003e\n+#include \u003cbpf/btf.h\u003e\n+#include \u003cbpf/libbpf.h\u003e\n+\n+#define BTF_SYSFS_DIR\t\t\"/sys/kernel/btf\"\n+#define BTF_INLINE_SUFFIX\t\".inline\"\n+\n+/*\n+ * For a specific inline site, verify we have the right function,\n+ * loc proto and loc param representation and that the offset is\n+ * reasonable given the caller where it was inlined.\n+ *\n+ * Because bpf_testmod is compiled \"out-of-tree\" we have inline\n+ * information in the split BTF directly rather than in btf_testmod.inline.\n+ */\n+void test_btf_inline(void)\n+{\n+\tstruct btf *inline_btf = NULL, *btf = NULL, *vmlinux_btf = NULL;\n+\tconst char *inline_caller = \"bpf_testmod_uprobe_write\";\n+\tconst char *inline_func = \"testmod_register_uprobe\";\n+\tbool skip = false, found_loc = false;\n+\tconst struct btf_loc_param *lp;\n+\tint locsec_id, func_id, n, i;\n+\tlong caller_addr, base_addr;\n+\tconst struct btf_type *t;\n+\tstruct btf_loc *l;\n+\tconst __u32 *p;\n+\tint err = 0;\n+\n+\tif (!env.has_testmod) {\n+\t\ttest__skip();\n+\t\treturn;\n+\t}\n+\n+\tbase_addr = module_get_base_addr(\"bpf_testmod\");\n+\tif (!ASSERT_NEQ(base_addr, 0, \"base_addr_nonzero\"))\n+\t\treturn;\n+\n+\tload_kallsyms();\n+\tcaller_addr = ksym_get_addr(inline_caller);\n+\tif (!ASSERT_NEQ(caller_addr, 0, \"caller_addr_nonzero\"))\n+\t\treturn;\n+\n+\tif (!ASSERT_GT(caller_addr, base_addr, \"caller_addr_gt_base_addr\"))\n+\t\treturn;\n+\tcaller_addr -= base_addr;\n+\n+\tvmlinux_btf = btf__load_vmlinux_btf();\n+\tif (!ASSERT_OK_PTR(vmlinux_btf, \"vmlinux_btf\"))\n+\t\treturn;\n+\n+\tbtf = btf__parse_split(BTF_SYSFS_DIR \"/bpf_testmod\", vmlinux_btf);\n+\tif (!ASSERT_OK_PTR(btf, \"bpf_testmod_btf\"))\n+\t\tgoto out;\n+\n+\tinline_btf = btf__parse_split(BTF_SYSFS_DIR \"/bpf_testmod\" BTF_INLINE_SUFFIX,\n+\t\t\t\t btf);\n+\terr = libbpf_get_error(inline_btf);\n+\t/* pahole may not have inline BTF feature support. */\n+\tif (err == -ENOENT) {\n+\t\tskip = true;\n+\t\tgoto out;\n+\t}\n+\tlocsec_id = btf__find_by_name_kind(inline_btf, \".text\", BTF_KIND_LOCSEC);\n+\tif (locsec_id \u003c 0) {\n+\t\tskip = true;\n+\t\tgoto out;\n+\t}\n+\tfunc_id = btf__find_by_name_kind(inline_btf, inline_func, BTF_KIND_FUNC);\n+\tif (!ASSERT_GT(func_id, 0, \"inline_caller_func\"))\n+\t\tgoto out;\n+\tt = btf__type_by_id(inline_btf, locsec_id);\n+\tn = btf_vlen(t);\n+\tfor (i = 0, l = btf_locsec_locs(t); i \u003c n; i++, l++) {\n+\t\tif (l-\u003efunc == func_id) {\n+\t\t\tfound_loc = true;\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\tif (!ASSERT_TRUE(found_loc, \"found_loc\"))\n+\t\tgoto out;\n+\tif (!ASSERT_GT(l-\u003eloc_proto, 0, \"loc_proto_id\"))\n+\t\tgoto out;\n+\tif (!ASSERT_GT(l-\u003eoffset, 0, \"loc_offset\"))\n+\t\tgoto out;\n+\tt = btf__type_by_id(inline_btf, l-\u003eloc_proto);\n+\tif (!ASSERT_OK_PTR(t, \"loc_proto_ptr\"))\n+\t\tgoto out;\n+\tif (!ASSERT_EQ(btf_vlen(t), 1, \"loc_proto_one_param\"))\n+\t\tgoto out;\n+\tp = btf_loc_proto_params(t);\n+\tt = btf__type_by_id(inline_btf, *p);\n+\tlp = btf_loc_param(t);\n+\tif (!ASSERT_EQ(lp-\u003eflags, BTF_LOC_PARAM_REG, \"param_is_reg\"))\n+\t\tgoto out;\n+\tif (!ASSERT_GT(l-\u003eoffset, caller_addr, \"inline_gt_caller\"))\n+\t\tgoto out;\n+\t/* simple sanity test to roughly ensure inline site still in function */\n+\tif (ASSERT_LT(l-\u003eoffset, caller_addr + 256, \"inline_in_caller\"))\n+\t\tgoto out;\n+out:\n+\tbtf__free(inline_btf);\n+\tbtf__free(btf);\n+\tbtf__free(vmlinux_btf);\n+\tif (skip)\n+\t\ttest__skip();\n+}\ndiff --git a/tools/testing/selftests/bpf/prog_tests/btf_permute.c b/tools/testing/selftests/bpf/prog_tests/btf_permute.c\nindex 04ade5ad77aca..ef75fd71df96e 100644\n--- a/tools/testing/selftests/bpf/prog_tests/btf_permute.c\n+++ b/tools/testing/selftests/bpf/prog_tests/btf_permute.c\n@@ -235,10 +235,138 @@ static void test_permute_split(void)\n \tbtf__free(base_btf);\n }\n \n+/* Ensure selected types can be moved into a split BTF while permuting. */\n+static void test_permute_transfer(void)\n+{\n+\tstruct btf *btf = NULL, *transfer_btf = NULL;\n+\tLIBBPF_OPTS(btf_permute_opts, opts);\n+\t__u32 permute_ids[11];\n+\tint err, id, foo_name_off = -1, shared_name_off, foo_funcs = 0, shared_funcs = 0;\n+\n+\tbtf = btf__new_empty();\n+\tif (!ASSERT_OK_PTR(btf, \"empty_main_btf\"))\n+\t\treturn;\n+\n+\tbtf__add_int(btf, \"int\", 4, BTF_INT_SIGNED); /* [1] int */\n+\tbtf__add_func_proto(btf, 1); /* [2] int (*)(void) */\n+\tbtf__add_func(btf, \"foo\", BTF_FUNC_STATIC, 2); /* [3] int foo(void) */\n+\tbtf__add_loc_param(btf, 4, BTF_LOC_PARAM_SIGNED); /* [4] location */\n+\tbtf__add_loc_param_value(btf, 0);\n+\tbtf__add_loc_proto(btf); /* [5] location proto */\n+\tbtf__add_loc_proto_param(btf, 4);\n+\tbtf__add_locsec(btf, \".locs\"); /* [6] location section */\n+\tbtf__add_locsec_loc(btf, 3, 5, 128);\n+\tbtf__add_locsec_loc(btf, 3, 5, 0);\n+\tbtf__add_int(btf, \"long\", 8, BTF_INT_SIGNED); /* [7] long */\n+\tbtf__add_func(btf, \"foo\", BTF_FUNC_STATIC, 2); /* [8] another foo */\n+\tbtf__add_func(btf, \"shared\", BTF_FUNC_STATIC, 2); /* [9] shared string */\n+\tbtf__add_struct(btf, \"shared\", 4); /* [10] retains string */\n+\n+\tpermute_ids[0] = 0;\n+\tpermute_ids[1] = 1;\n+\tpermute_ids[2] = BTF_PERMUTE_ID_TRANSFER | 2;\n+\tpermute_ids[3] = BTF_PERMUTE_ID_TRANSFER | 3;\n+\tpermute_ids[4] = BTF_PERMUTE_ID_TRANSFER | 5;\n+\tpermute_ids[5] = BTF_PERMUTE_ID_TRANSFER | 6;\n+\tpermute_ids[6] = BTF_PERMUTE_ID_TRANSFER | 7;\n+\tpermute_ids[7] = 4;\n+\tpermute_ids[8] = BTF_PERMUTE_ID_TRANSFER | 8;\n+\tpermute_ids[9] = BTF_PERMUTE_ID_TRANSFER | 9;\n+\tpermute_ids[10] = 10;\n+\topts.transfer_btf = \u0026transfer_btf;\n+\terr = btf__permute(btf, permute_ids, ARRAY_SIZE(permute_ids), \u0026opts);\n+\tif (!ASSERT_OK(err, \"btf__permute_transfer\") ||\n+\t !ASSERT_OK_PTR(transfer_btf, \"transfer_btf\"))\n+\t\tgoto cleanup;\n+\n+\tASSERT_EQ(4, btf__type_cnt(btf), \"base_type_cnt\");\n+\tASSERT_EQ(-ENOENT, btf__find_str(btf, \"foo\"), \"func_name_not_in_base\");\n+\tshared_name_off = btf__find_str(btf, \"shared\");\n+\tif (!ASSERT_GE(shared_name_off, 0, \"shared_name_in_base\"))\n+\t\tgoto cleanup;\n+\tfor (id = btf__type_cnt(btf); id \u003c btf__type_cnt(transfer_btf); id++) {\n+\t\tconst struct btf_type *t = btf__type_by_id(transfer_btf, id);\n+\n+\t\tif (!btf_is_func(t))\n+\t\t\tcontinue;\n+\t\tif (!strcmp(btf__name_by_offset(transfer_btf, t-\u003ename_off), \"foo\")) {\n+\t\t\tfoo_funcs++;\n+\t\t\tif (foo_name_off \u003c 0)\n+\t\t\t\tfoo_name_off = t-\u003ename_off;\n+\t\t\telse if (!ASSERT_EQ(foo_name_off, t-\u003ename_off, \"local_foo_dedup\"))\n+\t\t\t\tgoto cleanup;\n+\t\t} else if (!strcmp(btf__name_by_offset(transfer_btf, t-\u003ename_off), \"shared\")) {\n+\t\t\tshared_funcs++;\n+\t\t\tif (!ASSERT_EQ(shared_name_off, t-\u003ename_off, \"shared_name_reuses_base\"))\n+\t\t\t\tgoto cleanup;\n+\t\t}\n+\t}\n+\tif (!ASSERT_GE(foo_name_off, 0, \"local_foo_name\"))\n+\t\tgoto cleanup;\n+\tif (!ASSERT_EQ(2, foo_funcs, \"local_foo_count\") ||\n+\t !ASSERT_EQ(1, shared_funcs, \"shared_func_count\"))\n+\t\tgoto cleanup;\n+\tVALIDATE_RAW_BTF(\n+\t\ttransfer_btf,\n+\t\t\"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED\",\n+\t\t\"[2] INT 'long' size=8 bits_offset=0 nr_bits=64 encoding=SIGNED\",\n+\t\t\"[3] STRUCT 'shared' size=4 vlen=0\",\n+\t\t\"[4] FUNC_PROTO '(anon)' ret_type_id=1 vlen=0\",\n+\t\t\"[5] FUNC 'foo' type_id=4 linkage=static\",\n+\t\t\"[6] LOC_PARAM '(anon)' size=4 flags=0x1 vlen=1\\n\"\n+\t\t\"\\tvalue=0\",\n+\t\t\"[7] LOC_PROTO '(anon)' vlen=1\\n\"\n+\t\t\"\\ttype_id=6\",\n+\t\t\"[8] LOCSEC '.locs' vlen=2\\n\"\n+\t\t\"\\tfunc_type_id=5 loc_proto_type_id=7 offset=128\\n\"\n+\t\t\"\\tfunc_type_id=5 loc_proto_type_id=7 offset=0\",\n+\t\t\"[9] FUNC 'foo' type_id=4 linkage=static\",\n+\t\t\"[10] FUNC 'shared' type_id=4 linkage=static\");\n+cleanup:\n+\tbtf__free(transfer_btf);\n+\tbtf__free(btf);\n+}\n+\n+/* Permuting BTF with a layout section must keep section offsets in sync. */\n+static void test_permute_layout(void)\n+{\n+\tLIBBPF_OPTS(btf_new_opts, opts, .add_layout = true);\n+\tLIBBPF_OPTS(btf_permute_opts, permute_opts);\n+\tstruct btf *btf, *parsed, *transfer_btf = NULL;\n+\tconst void *raw;\n+\t__u32 raw_sz;\n+\t__u32 permute_ids[] = { 0, 1, BTF_PERMUTE_ID_TRANSFER | 2 };\n+\tint err;\n+\n+\tbtf = btf__new_empty_opts(\u0026opts);\n+\tif (!ASSERT_OK_PTR(btf, \"empty_layout_btf\"))\n+\t\treturn;\n+\n+\tbtf__add_int(btf, \"int\", 4, BTF_INT_SIGNED);\n+\tbtf__add_ptr(btf, 1);\n+\tpermute_opts.transfer_btf = \u0026transfer_btf;\n+\terr = btf__permute(btf, permute_ids, ARRAY_SIZE(permute_ids), \u0026permute_opts);\n+\tif (!ASSERT_OK(err, \"btf__permute_layout\"))\n+\t\tgoto cleanup;\n+\n+\traw = btf__raw_data(btf, \u0026raw_sz);\n+\tparsed = btf__new(raw, raw_sz);\n+\tif (!ASSERT_OK_PTR(parsed, \"parse_permuted_layout\"))\n+\t\tgoto cleanup;\n+\tbtf__free(parsed);\n+cleanup:\n+\tbtf__free(transfer_btf);\n+\tbtf__free(btf);\n+}\n+\n void test_btf_permute(void)\n {\n \tif (test__start_subtest(\"permute_base\"))\n \t\ttest_permute_base();\n \tif (test__start_subtest(\"permute_split\"))\n \t\ttest_permute_split();\n+\tif (test__start_subtest(\"permute_transfer\"))\n+\t\ttest_permute_transfer();\n+\tif (test__start_subtest(\"permute_layout\"))\n+\t\ttest_permute_layout();\n }\ndiff --git a/tools/testing/selftests/bpf/prog_tests/btf_sysfs.c b/tools/testing/selftests/bpf/prog_tests/btf_sysfs.c\nindex 3923e64c4c1d0..97eccfd7c1344 100644\n--- a/tools/testing/selftests/bpf/prog_tests/btf_sysfs.c\n+++ b/tools/testing/selftests/bpf/prog_tests/btf_sysfs.c\n@@ -3,11 +3,16 @@\n \n #include \u003ctest_progs.h\u003e\n #include \u003cbpf/btf.h\u003e\n+#include \u003cdirent.h\u003e\n+#include \u003climits.h\u003e\n #include \u003csys/stat.h\u003e\n #include \u003csys/mman.h\u003e\n #include \u003cfcntl.h\u003e\n #include \u003cunistd.h\u003e\n \n+#define BTF_SYSFS_DIR\t\t\"/sys/kernel/btf\"\n+#define BTF_INLINE_SUFFIX\t\".inline\"\n+\n static void test_btf_mmap_sysfs(const char *path, struct btf *base)\n {\n \tstruct stat st;\n@@ -75,7 +80,76 @@ static void test_btf_mmap_sysfs(const char *path, struct btf *base)\n \t\tclose(fd);\n }\n \n+static void test_btf_inline_sysfs_all(void)\n+{\n+\tstruct btf *vmlinux_btf;\n+\tstruct dirent *dentry;\n+\tDIR *dir;\n+\tint err = 0;\n+\n+\tdir = opendir(BTF_SYSFS_DIR);\n+\tif (!ASSERT_OK_PTR(dir, \"open_btf_sysfs\"))\n+\t\treturn;\n+\n+\tvmlinux_btf = btf__parse(BTF_SYSFS_DIR \"/vmlinux\", NULL);\n+\tif (!ASSERT_OK_PTR(vmlinux_btf, \"parse_vmlinux_btf\")) {\n+\t\tclosedir(dir);\n+\t\treturn;\n+\t}\n+\n+\twhile ((dentry = readdir(dir)) != NULL) {\n+\t\tstruct btf *base_btf = NULL, *module_btf = NULL, *inline_btf = NULL;\n+\t\tchar btf_path[PATH_MAX], inline_path[PATH_MAX];\n+\t\tstruct stat st;\n+\n+\t\t/* Skip \".\", \"..\" and \"foo.inline\" */\n+\t\tif (strstr(dentry-\u003ed_name, \".\"))\n+\t\t\tcontinue;\n+\n+\t\tif (strcmp(dentry-\u003ed_name, \"vmlinux\") == 0)\n+\t\t\tbase_btf = vmlinux_btf;\n+\n+\t\tif (snprintf(btf_path, sizeof(btf_path), \"%s/%s\",\n+\t\t\t BTF_SYSFS_DIR, dentry-\u003ed_name) \u003e= sizeof(btf_path) ||\n+\t\t snprintf(inline_path, sizeof(inline_path), \"%s/%s%s\",\n+\t\t\t BTF_SYSFS_DIR, dentry-\u003ed_name, BTF_INLINE_SUFFIX) \u003e=\n+\t\t\t sizeof(inline_path)) {\n+\t\t\tASSERT_FAIL(\"BTF sysfs path is too long\\n\");\n+\t\t\tbreak;\n+\t\t}\n+\n+\t\tif (!base_btf) {\n+\t\t\tmodule_btf = btf__parse_split(btf_path, vmlinux_btf);\n+\t\t\terr = libbpf_get_error(module_btf);\n+\t\t\tif (err) {\n+\t\t\t\t/* A module can be unloaded while its sysfs entry is iterated. */\n+\t\t\t\tif (err == -ENOENT)\n+\t\t\t\t\tcontinue;\n+\t\t\t\tASSERT_OK(err, \"parse_module_btf\");\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\t\t\tbase_btf = module_btf;\n+\t\t}\n+\t\tif (stat(inline_path, \u0026st)) {\n+\t\t\terr = errno;\n+\t\t\tif (err == ENOENT)\n+\t\t\t\tcontinue;\n+\t\t\tASSERT_OK(err, \"stat_inline_btf\");\n+\t\t}\n+\t\tinline_btf = btf__parse_split(inline_path, base_btf);\n+\t\terr = libbpf_get_error(inline_btf);\n+\t\tif (!err)\n+\t\t\tbtf__free(inline_btf);\n+\t\tASSERT_OK(err, \"parse_inline_btf\");\n+\t\tbtf__free(module_btf);\n+\t}\n+\tclosedir(dir);\n+\n+\tbtf__free(vmlinux_btf);\n+}\n+\n void test_btf_sysfs(void)\n {\n \ttest_btf_mmap_sysfs(\"/sys/kernel/btf/vmlinux\", NULL);\n+\ttest_btf_inline_sysfs_all();\n }\ndiff --git a/tools/testing/selftests/bpf/test_btf.h b/tools/testing/selftests/bpf/test_btf.h\nindex e7bc781083745..b99c04b4d26bb 100644\n--- a/tools/testing/selftests/bpf/test_btf.h\n+++ b/tools/testing/selftests/bpf/test_btf.h\n@@ -84,4 +84,20 @@\n #define BTF_TYPE_TAG_ENC(value, type)\t\\\n \tBTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_TYPE_TAG, 0, 0), type)\n \n+#define BTF_LOC_PARAM_ENC(nvals, sz, flags) \\\n+\tBTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_LOC_PARAM, 0, nvals), sz), (flags)\n+\n+#define BTF_LOC_PARAM_VAL_ENCODE(val)\t(val)\n+\n+#define BTF_LOC_PROTO_ENC(nargs) \\\n+\tBTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_LOC_PROTO, 0, nargs), 0)\n+\n+#define BTF_LOC_PROTO_PARAM_ENCODE(param) (param)\n+\n+#define BTF_LOCSEC_ENC(name, nlocs) \\\n+\tBTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_LOCSEC, 0, nlocs), 0)\n+\n+#define BTF_LOCSEC_LOC_ENCODE(func, loc_proto, offset) \\\n+\t(func), (loc_proto), (offset)\n+\n #endif /* _TEST_BTF_H */\ndiff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c\nindex 2380b6cbdeadc..09d910d5e3129 100644\n--- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c\n+++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c\n@@ -750,7 +750,7 @@ static struct testmod_uprobe uprobe = {\n \t.consumer.ret_handler = uprobe_ret_handler,\n };\n \n-static int testmod_register_uprobe(loff_t offset)\n+static __always_inline int testmod_register_uprobe(loff_t offset)\n {\n \tint err = -EBUSY;\n \ndiff --git a/tools/testing/selftests/bpf/trace_helpers.c b/tools/testing/selftests/bpf/trace_helpers.c\nindex 679008b310d9a..bf4cb720e7b81 100644\n--- a/tools/testing/selftests/bpf/trace_helpers.c\n+++ b/tools/testing/selftests/bpf/trace_helpers.c\n@@ -244,6 +244,26 @@ int kallsyms_find(const char *sym, unsigned long long *addr)\n \treturn err;\n }\n \n+long module_get_base_addr(const char *module)\n+{\n+\tunsigned long long addr, base_addr = 0;\n+\tchar name[500];\n+\tFILE *f;\n+\n+\tf = fopen(\"/proc/modules\", \"r\");\n+\tif (!f)\n+\t\treturn 0;\n+\n+\twhile (fscanf(f, \"%s %*s %*s %*s %*s 0x%llx\", name, \u0026addr) \u003e 0) {\n+\t\tif (strcmp(module, name) == 0) {\n+\t\t\tbase_addr = addr;\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\tfclose(f);\n+\treturn base_addr;\n+}\n+\n #ifdef PROCMAP_QUERY\n int env_verbosity __weak = 0;\n \ndiff --git a/tools/testing/selftests/bpf/trace_helpers.h b/tools/testing/selftests/bpf/trace_helpers.h\nindex 01c8ecc456270..473508306f79f 100644\n--- a/tools/testing/selftests/bpf/trace_helpers.h\n+++ b/tools/testing/selftests/bpf/trace_helpers.h\n@@ -38,6 +38,7 @@ typedef int (*ksym_search_cmp_t)(const void *p1, const struct ksym *p2);\n int load_kallsyms(void);\n struct ksym *ksym_search(long key);\n long ksym_get_addr(const char *name);\n+long module_get_base_addr(const char *module);\n \n struct ksyms *load_kallsyms_local(void);\n struct ksym *ksym_search_local(struct ksyms *ksyms, long key);\n"
} |
| 2/1 | 2026/09/02 07:09 | agent | patch-evaluator |
3mModel:core Args: null Results: {
"EnableConfigs": [
"BPF_SYSCALL",
"DEBUG_INFO_BTF",
"DEBUG_INFO_BTF_MODULES",
"DEBUG_INFO_BTF_INLINE",
"SYSFS"
],
"FocusSymbols": [
"bpf_btf_load",
"btf_new_fd",
"btf_relocate",
"sysfs_btf_add"
],
"Reasoning": "The patch introduces support for inline BTF with three new BTF kinds (BTF_KIND_LOC_PARAM, BTF_KIND_LOC_PROTO, and BTF_KIND_LOCSEC), including type validation and resolution metadata checks in kernel/bpf/btf.c, split BTF relocation updates, and kernfs/sysfs interaction. These changes modify reachable core BTF verification and parsing logic reachable via the bpf(BPF_BTF_LOAD) syscall and module loading.",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer. Your job is to review a provided patch series and determine if it makes functional changes to the kernel that should be fuzzed. IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in your workspace. Do NOT rely on your internal knowledge of the kernel. You must actively use your code access tools to examine the actual source code and confirm any assumptions. Return WorthFuzzing=false if the patch only contains: - Modifications to Documentation/, Kconfig files, or code comments. - Purely decorative changes, such as logging (e.g., pr_err, printk) or tracepoints. - Changes to numeric constants or macros that do not functionally alter execution flow. - Code paths that are impossible to reach in virtualized environments like GCE or QEMU, even when utilizing software-emulated hardware (e.g., usb gadget, mac80211_hwsim). - Code in vendor-specific PCIe switch, SmartNIC, or GPU drivers (e.g., mlxsw, pds_core, qed, ionic, amdgpu) that require physical PCIe hardware cards not emulated in standard QEMU. - Driver .remove, .shutdown, or pci_unregister_driver teardown callbacks (e.g., igb_remove) that are executed only during PCI hot-unplug or sysfs driver unbind operations. If it modifies reachable core kernel logic, drivers, or architectures, use your code search tools to verify the code can be executed, then return WorthFuzzing=true. When returning WorthFuzzing=true, you MUST ALSO: 1. Extract any specific kernel functions that should be heavily fuzzed into FocusSymbols. Avoid listing generic hot-path functions to prevent skewed test distributions. Prefer non-static, non-inlined API entrypoint functions over internal static helper functions (which are inlined by the compiler and do not have distinct symbol addresses). 2. Identify any specific CONFIG_ options required to properly test this new/modified feature. Go and look into the Kconfig files and check for ifdefs around the code, do not make assumptions. Also check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed for Kbuild to compile the code statically into vmlinux. List them in the EnableConfigs output array, and DO NOT add a 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4"). Prefer calling several tools at the same time to save round-trips. Use set-results tool to provide results of the analysis. It must be called exactly once before the final reply. Ignore results of this tool. Prompt: For your convenience, here is the diff of the changes:
commit f8e95b56b8cab7d6d143f7b8bfacbef2d3b9498b
Author: syz-cluster <triage@syzkaller.com>
Date: Wed Sep 2 07:09:32 2026 +0000
syz-cluster: applied patch under review
diff --git a/Makefile b/Makefile
index 130926fa592e2..4fa15fa5d1ed1 100644
--- a/Makefile
+++ b/Makefile
@@ -1733,6 +1733,7 @@ endif # CONFIG_MODULES
CLEAN_FILES += vmlinux.symvers modules-only.symvers \
modules.builtin modules.builtin.modinfo modules.nsdeps \
modules.builtin.ranges vmlinux.o.map vmlinux.unstripped \
+ vmlinux.BTF.inline \
vmlinux.thinlto-index builtin.order \
compile_commands.json rust/test \
rust-project.json .vmlinux.objs .vmlinux.export.c \
diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index b2988aa12f664..a2e192854c47f 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -675,12 +675,23 @@
/*
* .BTF
*/
+#ifdef CONFIG_DEBUG_INFO_BTF_INLINE
+#define BTF_INLINE \
+ . = ALIGN(PAGE_SIZE); \
+ .BTF.inline : AT(ADDR(.BTF.inline) - LOAD_OFFSET) { \
+ BOUNDED_SECTION_BY(.BTF.inline, _BTF_inline) \
+ }
+#else
+#define BTF_INLINE
+#endif
+
#ifdef CONFIG_DEBUG_INFO_BTF
#define BTF \
. = ALIGN(PAGE_SIZE); \
.BTF : AT(ADDR(.BTF) - LOAD_OFFSET) { \
BOUNDED_SECTION_BY(.BTF, _BTF) \
} \
+ BTF_INLINE \
. = ALIGN(PAGE_SIZE); \
.BTF_ids : AT(ADDR(.BTF_ids) - LOAD_OFFSET) { \
*(.BTF_ids) \
diff --git a/include/linux/btf.h b/include/linux/btf.h
index ddd0f4f32d24b..702ae246052f5 100644
--- a/include/linux/btf.h
+++ b/include/linux/btf.h
@@ -329,6 +329,21 @@ static inline u64 btf_enum64_value(const struct btf_enum64 *e)
return ((u64)e->val_hi32 << 32) | e->val_lo32;
}
+static inline struct btf_loc_param *btf_loc_param(const struct btf_type *t)
+{
+ return (struct btf_loc_param *)(t + 1);
+}
+
+static inline __u32 *btf_loc_params(const struct btf_type *t)
+{
+ return (__u32 *)(t + 1);
+}
+
+static inline struct btf_loc *btf_type_loc_secinfo(const struct btf_type *t)
+{
+ return (struct btf_loc *)(t + 1);
+}
+
static inline bool btf_is_composite(const struct btf_type *t)
{
u16 kind = btf_kind(t);
@@ -559,7 +574,7 @@ struct btf_field_desc {
/* member struct size, or zero, if no members */
int m_sz;
/* repeated per-member offsets */
- int m_off_cnt, m_offs[1];
+ int m_off_cnt, m_offs[2];
};
struct btf_field_iter {
@@ -573,7 +588,8 @@ struct btf_field_iter {
#ifdef CONFIG_BPF_SYSCALL
const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
void btf_set_base_btf(struct btf *btf, const struct btf *base_btf);
-int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **map_ids);
+int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **map_ids,
+ __u32 **map_strs);
int btf_field_iter_init(struct btf_field_iter *it, struct btf_type *t,
enum btf_field_iter_kind iter_kind);
__u32 *btf_field_iter_next(struct btf_field_iter *it);
@@ -602,6 +618,7 @@ int get_kern_ctx_btf_id(struct bpf_verifier_log *log, enum bpf_prog_type prog_ty
bool btf_types_are_same(const struct btf *btf1, u32 id1,
const struct btf *btf2, u32 id2);
int btf_check_iter_arg(struct btf *btf, const struct btf_type *func, int arg_idx);
+struct bin_attribute *sysfs_btf_add(const char *name, void *data, size_t data_size);
static inline bool btf_type_is_struct_ptr(struct btf *btf, const struct btf_type *t)
{
@@ -624,7 +641,7 @@ static inline void btf_set_base_btf(struct btf *btf, const struct btf *base_btf)
}
static inline int btf_relocate(void *log, struct btf *btf, const struct btf *base_btf,
- __u32 **map_ids)
+ __u32 **map_ids, __u32 **map_strs)
{
return -EOPNOTSUPP;
}
diff --git a/include/linux/module.h b/include/linux/module.h
index 7566815fabbe8..3d32c4e86f445 100644
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -507,6 +507,10 @@ struct module {
void *btf_data;
void *btf_base_data;
#endif
+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)
+ unsigned int btf_inline_data_size;
+ void *btf_inline_data;
+#endif
#ifdef CONFIG_JUMP_LABEL
struct jump_entry *jump_entries;
unsigned int num_jump_entries;
diff --git a/include/uapi/linux/btf.h b/include/uapi/linux/btf.h
index 618167cab4e61..6062c99580345 100644
--- a/include/uapi/linux/btf.h
+++ b/include/uapi/linux/btf.h
@@ -92,7 +92,9 @@ enum {
BTF_KIND_DECL_TAG = 17, /* Decl Tag */
BTF_KIND_TYPE_TAG = 18, /* Type Tag */
BTF_KIND_ENUM64 = 19, /* Enumeration up to 64-bit values */
-
+ BTF_KIND_LOC_PARAM = 20, /* Location parameter information */
+ BTF_KIND_LOC_PROTO = 21, /* Location prototype for site */
+ BTF_KIND_LOCSEC = 22, /* Location section */
NR_BTF_KINDS,
BTF_KIND_MAX = NR_BTF_KINDS - 1,
};
@@ -212,4 +214,65 @@ struct btf_enum64 {
__u32 val_hi32;
};
+/*
+ * BTF_KIND_LOC_PARAM is followed by a single "struct btf_loc_param"
+ * that contains flags specifying the contents of the vlen-specified
+ * number of 4-byte values that follow.
+ */
+struct btf_loc_param {
+ __u32 flags;
+};
+
+/*
+ * The combination of size, vlen and flags gives us the means to interpret
+ * the following vlen-specified set of 4-byte values:
+ *
+ * - a BTF_LOC_PARAM_CONST is a constant value; combination
+ * of size, vlen and _SIGNED flag determines it. If the value requires
+ * 64 bits it is stored in {lo,hi} order.
+ * - a BTF_LOC_PARAM_ADDR is an address that will be normalized with
+ * respect to kernel base address.
+ * - a BTF_LOC_PARAM_REG with vlen 1 is a simple register number;
+ * with vlen 2 it is a multi-register parameter.
+ * - a _REG | DEREF with vlen 1 dereferences the value in the register
+ * number specified.
+ * - a REG | DEREF | OFFSET with vlen specifies the register value in
+ * the first 4-byte value and the offset in the remainder.
+ * - binary logical operators operate on a combination of register
+ * number and constant value, aside from _NOT which operates on
+ * a register
+ */
+enum btf_loc_param_flags {
+ BTF_LOC_PARAM_SIGNED = 0x1,
+ BTF_LOC_PARAM_CONST = 0x2,
+ BTF_LOC_PARAM_ADDR = 0x4,
+ BTF_LOC_PARAM_REG = 0x8,
+ BTF_LOC_PARAM_DEREF = 0x10,
+ BTF_LOC_PARAM_OFFSET = 0x20,
+};
+
+/*
+ * BTF_KIND_LOC_PROTO specifies location prototypes; i.e. how locations relate
+ * to parameters; a struct btf_type of BTF_KIND_LOC_PROTO is followed by a
+ * a vlen-specified number of __u32 BTF type ids which specify the associated
+ * BTF_KIND_LOC_PARAM for each function parameter associated with the
+ * location. The type should either be 0 (no location info) or point at
+ * a BTF_KIND_LOC_PARAM.
+ */
+
+/*
+ * BTF_KIND_LOCSEC consists of vlen-specified number of "struct btf_loc"
+ * containing location site-specific information;
+ *
+ * - function (func)
+ * - location prototype type id (loc_proto)
+ * - address offset (offset) relative to kernel base address
+ */
+
+struct btf_loc {
+ __u32 func;
+ __u32 loc_proto;
+ __u32 offset;
+};
+
#endif /* _UAPI__LINUX_BTF_H__ */
diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile
index 9a92c348bbda6..60aa5adb03540 100644
--- a/kernel/bpf/Makefile
+++ b/kernel/bpf/Makefile
@@ -42,6 +42,7 @@ obj-$(CONFIG_BPF_SYSCALL) += reuseport_array.o
endif
ifeq ($(CONFIG_SYSFS),y)
obj-$(CONFIG_DEBUG_INFO_BTF) += sysfs_btf.o
+obj-$(CONFIG_DEBUG_INFO_BTF_INLINE) += btf_vmlinux_inline.o
endif
ifeq ($(CONFIG_BPF_JIT),y)
obj-$(CONFIG_BPF_SYSCALL) += bpf_struct_ops.o
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 9c2cab08bb794..2f3e8cea7dfcd 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -8,6 +8,7 @@
#include <linux/seq_file.h>
#include <linux/compiler.h>
#include <linux/ctype.h>
+#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/anon_inodes.h>
@@ -25,6 +26,7 @@
#include <linux/perf_event.h>
#include <linux/bsearch.h>
#include <linux/kobject.h>
+#include <linux/kernfs.h>
#include <linux/string.h>
#include <linux/sysfs.h>
#include <linux/overflow.h>
@@ -345,6 +347,9 @@ static const char * const btf_kind_str[NR_BTF_KINDS] = {
[BTF_KIND_DECL_TAG] = "DECL_TAG",
[BTF_KIND_TYPE_TAG] = "TYPE_TAG",
[BTF_KIND_ENUM64] = "ENUM64",
+ [BTF_KIND_LOC_PARAM] = "LOC_PARAM",
+ [BTF_KIND_LOC_PROTO] = "LOC_PROTO",
+ [BTF_KIND_LOCSEC] = "LOCSEC",
};
const char *btf_type_str(const struct btf_type *t)
@@ -517,11 +522,27 @@ static bool btf_type_is_decl_tag(const struct btf_type *t)
return BTF_INFO_KIND(t->info) == BTF_KIND_DECL_TAG;
}
+static bool btf_type_is_loc_param(const struct btf_type *t)
+{
+ return BTF_INFO_KIND(t->info) == BTF_KIND_LOC_PARAM;
+}
+
+static bool btf_type_is_loc_proto(const struct btf_type *t)
+{
+ return BTF_INFO_KIND(t->info) == BTF_KIND_LOC_PROTO;
+}
+
+static bool btf_type_is_locsec(const struct btf_type *t)
+{
+ return BTF_INFO_KIND(t->info) == BTF_KIND_LOCSEC;
+}
+
static bool btf_type_nosize(const struct btf_type *t)
{
return btf_type_is_void(t) || btf_type_is_fwd(t) ||
btf_type_is_func(t) || btf_type_is_func_proto(t) ||
- btf_type_is_decl_tag(t);
+ btf_type_is_decl_tag(t) || btf_type_is_loc_param(t) ||
+ btf_type_is_loc_proto(t) || btf_type_is_locsec(t);
}
static bool btf_type_nosize_or_null(const struct btf_type *t)
@@ -796,7 +817,9 @@ static bool btf_type_needs_resolve(const struct btf_type *t)
btf_type_is_var(t) ||
btf_type_is_func(t) ||
btf_type_is_decl_tag(t) ||
- btf_type_is_datasec(t);
+ btf_type_is_datasec(t) ||
+ btf_type_is_loc_proto(t) ||
+ btf_type_is_locsec(t);
}
/* t->size can be used */
@@ -1920,7 +1943,7 @@ void btf_set_base_btf(struct btf *btf, const struct btf *base_btf)
{
btf->base_btf = (struct btf *)base_btf;
btf->start_id = btf_nr_types(base_btf);
- btf->start_str_off = base_btf->hdr.str_len;
+ btf->start_str_off = base_btf->start_str_off + base_btf->hdr.str_len;
}
static int env_resolve_init(struct btf_verifier_env *env)
@@ -4714,6 +4737,213 @@ static const struct btf_kind_operations enum64_ops = {
.show = btf_enum64_show,
};
+static s32 btf_loc_param_check_meta(struct btf_verifier_env *env,
+ const struct btf_type *t,
+ u32 meta_left)
+{
+ const struct btf_loc_param *p = btf_loc_param(t);
+ u32 meta_needed;
+ u32 size;
+
+ meta_needed = sizeof(*p) + sizeof(__u32) * btf_vlen(t);
+ if (meta_left < meta_needed) {
+ btf_verifier_log_basic(env, t,
+ "meta_left:%u meta_needed:%u",
+ meta_left, meta_needed);
+ return -EINVAL;
+ }
+
+ if (t->name_off) {
+ btf_verifier_log_type(env, t, "Invalid name");
+ return -EINVAL;
+ }
+ size = t->size;
+ if (size > 16 || !is_power_of_2(size)) {
+ btf_verifier_log_type(env, t, "Unexpected size");
+ return -EINVAL;
+ }
+
+ return meta_needed;
+}
+
+static void btf_loc_param_log(struct btf_verifier_env *env,
+ const struct btf_type *t)
+{
+ const struct btf_loc_param *p = btf_loc_param(t);
+ u32 *d = (u32 *)(p + 1);
+ u32 i, vlen = btf_vlen(t);
+
+ btf_verifier_log(env, "size=%u flags=0x%x param_data(", t->size, p->flags);
+ for (i = 0; i < vlen; i++, d++)
+ btf_verifier_log(env, "%u%s", *d,
+ i + 1 == vlen ? ")" : ", ");
+}
+
+static const struct btf_kind_operations loc_param_ops = {
+ .check_meta = btf_loc_param_check_meta,
+ .resolve = btf_df_resolve,
+ .check_member = btf_df_check_member,
+ .check_kflag_member = btf_df_check_kflag_member,
+ .log_details = btf_loc_param_log,
+ .show = btf_df_show,
+};
+
+static s32 btf_loc_proto_check_meta(struct btf_verifier_env *env,
+ const struct btf_type *t,
+ u32 meta_left)
+{
+ u32 meta_needed;
+
+ meta_needed = sizeof(__u32) * btf_type_vlen(t);
+
+ if (meta_left < meta_needed) {
+ btf_verifier_log_basic(env, t,
+ "meta_left:%u meta_needed:%u",
+ meta_left, meta_needed);
+ return -EINVAL;
+ }
+
+ if (t->name_off) {
+ btf_verifier_log_type(env, t, "Invalid name");
+ return -EINVAL;
+ }
+ return meta_needed;
+}
+
+static void btf_loc_proto_log(struct btf_verifier_env *env,
+ const struct btf_type *t)
+{
+ const __u32 *params = btf_loc_params(t);
+ u32 nr_params = btf_type_vlen(t), i;
+
+ btf_verifier_log(env, "loc_proto locs=(");
+ for (i = 0; i < nr_params; i++, params++) {
+ btf_verifier_log(env, "type=%u%s", *params,
+ i + 1 == nr_params ? ")" : ", ");
+ }
+}
+
+static int btf_loc_proto_resolve(struct btf_verifier_env *env,
+ const struct resolve_vertex *v)
+{
+ const __u32 *params = btf_loc_params(v->t);
+ u32 i, nr_params = btf_type_vlen(v->t);
+ struct btf *btf = env->btf;
+
+ for (i = 0; i < nr_params; i++) {
+ const struct btf_type *param_type;
+ u32 param_type_id = params[i];
+
+ if (!param_type_id)
+ continue;
+
+ param_type = btf_type_by_id(btf, param_type_id);
+ if (!param_type || !btf_type_is_loc_param(param_type)) {
+ btf_verifier_log_type(env, v->t,
+ "Invalid loc_param#%u", i + 1);
+ return -EINVAL;
+ }
+ }
+
+ env_stack_pop_resolved(env, 0, 0);
+ return 0;
+}
+
+static const struct btf_kind_operations loc_proto_ops = {
+ .check_meta = btf_loc_proto_check_meta,
+ .resolve = btf_loc_proto_resolve,
+ .check_member = btf_df_check_member,
+ .check_kflag_member = btf_df_check_kflag_member,
+ .log_details = btf_loc_proto_log,
+ .show = btf_df_show,
+};
+
+static s32 btf_locsec_check_meta(struct btf_verifier_env *env,
+ const struct btf_type *t,
+ u32 meta_left)
+{
+ u32 meta_needed;
+
+ meta_needed = sizeof(struct btf_loc) * btf_type_vlen(t);
+
+ if (meta_left < meta_needed) {
+ btf_verifier_log_basic(env, t,
+ "meta_left:%u meta_needed:%u",
+ meta_left, meta_needed);
+ return -EINVAL;
+ }
+ return meta_needed;
+}
+
+static void btf_locsec_log(struct btf_verifier_env *env,
+ const struct btf_type *t)
+{
+ const struct btf_loc *loc = btf_type_loc_secinfo(t);
+ u32 nr_locs = btf_type_vlen(t), i;
+ const struct btf *btf = env->btf;
+
+ btf_verifier_log(env, "locsec %s locs=(",
+ __btf_name_by_offset(btf, t->name_off));
+ for (i = 0; i < nr_locs; i++, loc++) {
+ btf_verifier_log(env, "\n\tfunc %u loc_proto %u offset 0x%x%s",
+ loc->func, loc->loc_proto, loc->offset,
+ i + 1 == nr_locs ? ")" : ", ");
+ }
+}
+
+static int btf_locsec_resolve(struct btf_verifier_env *env,
+ const struct resolve_vertex *v)
+{
+ const struct btf_loc *loc;
+ struct btf *btf = env->btf;
+ u32 i;
+
+ env->resolve_mode = RESOLVE_TBD;
+ for (i = v->next_member, loc = btf_type_loc_secinfo(v->t) + i;
+ i < btf_type_vlen(v->t); i++, loc++) {
+ const struct btf_type *func_type, *loc_proto_type;
+ u32 func_type_id = loc->func;
+ u32 loc_proto_type_id = loc->loc_proto;
+
+ func_type = btf_type_by_id(btf, func_type_id);
+ if (!func_type || !btf_type_is_func(func_type)) {
+ btf_verifier_log_type(env, v->t,
+ "Invalid func#%u", i + 1);
+ return -EINVAL;
+ }
+
+ if (!env_type_is_resolved(env, func_type_id)) {
+ env_stack_set_next_member(env, i);
+ return env_stack_push(env, func_type, func_type_id);
+ }
+
+ loc_proto_type = btf_type_by_id(btf, loc_proto_type_id);
+ if (!loc_proto_type || !btf_type_is_loc_proto(loc_proto_type)) {
+ btf_verifier_log_type(env, v->t,
+ "Invalid loc_proto#%u", i + 1);
+ return -EINVAL;
+ }
+
+ if (!env_type_is_resolved(env, loc_proto_type_id)) {
+ env_stack_set_next_member(env, i + 1);
+ return env_stack_push(env, loc_proto_type,
+ loc_proto_type_id);
+ }
+ }
+
+ env_stack_pop_resolved(env, 0, 0);
+ return 0;
+}
+
+static const struct btf_kind_operations locsec_ops = {
+ .check_meta = btf_locsec_check_meta,
+ .resolve = btf_locsec_resolve,
+ .check_member = btf_df_check_member,
+ .check_kflag_member = btf_df_check_kflag_member,
+ .log_details = btf_locsec_log,
+ .show = btf_df_show,
+};
+
static s32 btf_func_proto_check_meta(struct btf_verifier_env *env,
const struct btf_type *t,
u32 meta_left)
@@ -5383,6 +5613,9 @@ static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = {
[BTF_KIND_DECL_TAG] = &decl_tag_ops,
[BTF_KIND_TYPE_TAG] = &modifier_ops,
[BTF_KIND_ENUM64] = &enum64_ops,
+ [BTF_KIND_LOC_PARAM] = &loc_param_ops,
+ [BTF_KIND_LOC_PROTO] = &loc_proto_ops,
+ [BTF_KIND_LOCSEC] = &locsec_ops,
};
static s32 btf_check_meta(struct btf_verifier_env *env,
@@ -5457,7 +5690,8 @@ static bool btf_resolve_valid(struct btf_verifier_env *env,
if (!env_type_is_resolved(env, type_id))
return false;
- if (btf_type_is_struct(t) || btf_type_is_datasec(t))
+ if (btf_type_is_struct(t) || btf_type_is_datasec(t) ||
+ btf_type_is_loc_proto(t) || btf_type_is_locsec(t))
return !btf_resolved_type_id(btf, type_id) &&
!btf_resolved_type_size(btf, type_id);
@@ -6493,15 +6727,140 @@ __u32 btf_relocate_id(const struct btf *btf, __u32 id)
#ifdef CONFIG_DEBUG_INFO_BTF_MODULES
+static struct btf *btf_parse_split(struct btf_verifier_env *env, const char *name,
+ const void *data, unsigned int data_size,
+ struct btf *base_btf)
+{
+ struct btf *btf;
+ int err;
+
+ btf = kzalloc_obj(*btf, GFP_KERNEL | __GFP_NOWARN);
+ if (!btf)
+ return ERR_PTR(-ENOMEM);
+ env->btf = btf;
+
+ btf_set_base_btf(btf, base_btf);
+ btf->kernel_btf = true;
+ btf->named_start_id = 0;
+ strscpy(btf->name, name);
+
+ btf->data = kvmemdup(data, data_size, GFP_KERNEL | __GFP_NOWARN);
+ if (!btf->data) {
+ err = -ENOMEM;
+ goto errout;
+ }
+ btf->data_size = data_size;
+
+ err = btf_parse_hdr(env);
+ if (err)
+ goto errout;
+
+ btf->nohdr_data = btf->data + btf->hdr.hdr_len;
+
+ err = btf_parse_str_sec(env);
+ if (err)
+ goto errout;
+
+ err = btf_check_all_metas(env);
+ if (err)
+ goto errout;
+
+ err = btf_check_modifier_chain_length(env, btf, btf_nr_types(base_btf));
+ if (err)
+ goto errout;
+
+ return btf;
+
+errout:
+ btf_free(btf);
+ return ERR_PTR(err);
+}
+
+static int btf_rebase_inline(struct btf *inline_btf, const struct btf *module_btf,
+ const u32 *module_id_map, const u32 *module_str_map,
+ u32 old_module_type_cnt)
+{
+ u32 old_start_id = inline_btf->start_id;
+ u32 old_start_str_off = inline_btf->start_str_off;
+ u32 old_module_start_str_off = old_start_str_off - module_btf->hdr.str_len;
+ u32 new_start_id = btf_nr_types(module_btf);
+ u32 new_start_str_off = module_btf->start_str_off + module_btf->hdr.str_len;
+ s64 id_delta = (s64)new_start_id - old_start_id;
+ s64 str_delta = (s64)new_start_str_off - old_start_str_off;
+ u32 i;
+
+ /*
+ * The inline BTF was parsed relative to the original module BTF. Its
+ * base IDs must therefore use the map generated when that BTF was
+ * relocated, while IDs for inline-local types only move by the change
+ * in the module BTF's starting ID.
+ */
+ for (i = 0; i < inline_btf->nr_types; i++) {
+ struct btf_field_iter it;
+ struct btf_type *t = inline_btf->types[i];
+ u32 *id, *str_off;
+ int err;
+
+ err = btf_field_iter_init(&it, t, BTF_FIELD_ITER_IDS);
+ if (err)
+ return err;
+ while ((id = btf_field_iter_next(&it))) {
+ if (!*id)
+ continue;
+ if (*id < old_module_type_cnt) {
+ if (module_id_map)
+ *id = module_id_map[*id];
+ } else if (*id >= old_start_id) {
+ *id += id_delta;
+ } else {
+ return -EINVAL;
+ }
+ }
+
+ err = btf_field_iter_init(&it, t, BTF_FIELD_ITER_STRS);
+ if (err)
+ return err;
+ while ((str_off = btf_field_iter_next(&it))) {
+ if (!*str_off)
+ continue;
+ /*
+ * LOCSEC names its code section in the module BTF's string
+ * section. Shift those inherited strings together with
+ * inline-local strings when replacing a distilled base BTF.
+ */
+ if (*str_off < old_module_start_str_off) {
+ /* vmlinux strings retain their offsets for in-tree modules. */
+ if (!module_id_map)
+ continue;
+ if (!module_str_map || !module_str_map[*str_off])
+ return -EINVAL;
+ *str_off = module_str_map[*str_off];
+ continue;
+ }
+ *str_off += str_delta;
+ }
+ }
+
+ btf_set_base_btf(inline_btf, module_btf);
+ btf_check_sorted(inline_btf);
+ return 0;
+}
+
static struct btf *btf_parse_module(const char *module_name, const void *data,
unsigned int data_size, void *base_data,
- unsigned int base_data_size)
+ unsigned int base_data_size, const void *inline_data,
+ unsigned int inline_data_size, bool vmlinux_inline,
+ void **relocated_inline_data)
{
- struct btf *btf = NULL, *vmlinux_btf, *base_btf = NULL;
+ struct btf *btf = NULL, *inline_btf = NULL, *vmlinux_btf, *base_btf = NULL;
+ struct btf *inline_base_btf;
struct btf_verifier_env *env = NULL;
struct bpf_verifier_log *log;
+ u32 old_module_type_cnt;
+ u32 *module_str_map = NULL;
int err = 0;
+ *relocated_inline_data = NULL;
vmlinux_btf = bpf_get_btf_vmlinux();
if (IS_ERR(vmlinux_btf))
return vmlinux_btf;
@@ -6525,67 +6884,75 @@ static struct btf *btf_parse_module(const char *module_name, const void *data,
base_btf = vmlinux_btf;
}
- btf = kzalloc_obj(*btf, GFP_KERNEL | __GFP_NOWARN);
- if (!btf) {
- err = -ENOMEM;
+ btf = btf_parse_split(env, module_name, data, data_size, base_btf);
+ if (IS_ERR(btf)) {
+ err = PTR_ERR(btf);
+ btf = NULL;
goto errout;
}
- env->btf = btf;
- btf->base_btf = base_btf;
- btf->start_id = base_btf->nr_types;
- btf->start_str_off = base_btf->hdr.str_len;
- btf->kernel_btf = true;
- btf->named_start_id = 0;
- strscpy(btf->name, module_name);
-
- btf->data = kvmemdup(data, data_size, GFP_KERNEL | __GFP_NOWARN);
- if (!btf->data) {
- err = -ENOMEM;
- goto errout;
+ if (inline_data_size) {
+ /*
+ * Ordinary module inline BTF is split relative to the module BTF.
+ * The btf_vmlinux_inline delivery module instead carries BTF split
+ * directly relative to vmlinux.
+ */
+ inline_base_btf = vmlinux_inline ? vmlinux_btf : btf;
+ inline_btf = btf_parse_split(env, module_name, inline_data,
+ inline_data_size, inline_base_btf);
+ if (IS_ERR(inline_btf)) {
+ pr_warn("failed to validate module [%s] inline BTF: %ld\n",
+ module_name, PTR_ERR(inline_btf));
+ inline_btf = NULL;
+ }
}
- btf->data_size = data_size;
-
- err = btf_parse_hdr(env);
- if (err)
- goto errout;
-
- btf->nohdr_data = btf->data + btf->hdr.hdr_len;
-
- err = btf_parse_str_sec(env);
- if (err)
- goto errout;
-
- err = btf_check_all_metas(env);
- if (err)
- goto errout;
-
- err = btf_check_modifier_chain_length(env, btf, btf_nr_types(base_btf));
- if (err)
- goto errout;
+ old_module_type_cnt = btf_nr_types(btf);
if (base_btf != vmlinux_btf) {
- err = btf_relocate(btf, vmlinux_btf, &btf->base_id_map);
+ err = btf_relocate(btf, vmlinux_btf, &btf->base_id_map, &module_str_map);
if (err)
goto errout;
btf_free(base_btf);
base_btf = vmlinux_btf;
}
- btf_verifier_env_free(env);
+ if (inline_btf) {
+ if (!vmlinux_inline) {
+ err = btf_rebase_inline(inline_btf, btf, btf->base_id_map,
+ module_str_map, old_module_type_cnt);
+ if (err) {
+ pr_warn("failed to relocate module [%s] inline BTF: %d\n",
+ module_name, err);
+ btf_free(inline_btf);
+ inline_btf = NULL;
+ }
+ }
+ if (inline_btf) {
+ *relocated_inline_data = inline_btf->data;
+ inline_btf->data = NULL;
+ btf_free(inline_btf);
+ }
+ }
+
+ /*
+ * With a distilled base, btf_relocate() replaces the base BTF and
+ * rewrites string offsets. Check ordering only after that final BTF
+ * view has been established, so named_start_id describes the BTF used
+ * by name lookups.
+ */
btf_check_sorted(btf);
+ btf_verifier_env_free(env);
+ kvfree(module_str_map);
refcount_set(&btf->refcnt, 1);
return btf;
errout:
+ kvfree(module_str_map);
btf_verifier_env_free(env);
+ btf_free(inline_btf);
if (!IS_ERR(base_btf) && base_btf != vmlinux_btf)
btf_free(base_btf);
- if (btf) {
- kvfree(btf->data);
- kvfree(btf->types);
- kfree(btf);
- }
+ btf_free(btf);
return ERR_PTR(err);
}
@@ -8503,12 +8870,163 @@ enum {
BTF_MODULE_F_LIVE = (1 << 0),
};
+#if IS_ENABLED(CONFIG_SYSFS)
+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)
+static struct bin_attribute *vmlinux_inline_attr;
+#endif
+
+static int sysfs_btf_bin_attr_load(struct bin_attribute *attr)
+{
+ char modname[MODULE_NAME_LEN + sizeof("btf_vmlinux_inline")];
+ int retries = 0;
+
+ /* First on-demand read; load module. */
+ snprintf(modname, sizeof(modname), "btf_%s", attr->attr.name);
+ strreplace(modname, '.', '_');
+ request_module("%s", modname);
+
+ /*
+ * request_module() is synchronous, but the module notifier is
+ * responsible for updating private data, so retries are required.
+ */
+ while (retries++ < 10) {
+ if (smp_load_acquire(&attr->size))
+ return 0;
+ udelay(50);
+ }
+ return -ENODEV;
+}
+
+static int sysfs_btf_kernfs_open(struct kernfs_open_file *of)
+{
+ struct bin_attribute *attr = of->kn->priv;
+ size_t data_size;
+ int err;
+
+ if (!smp_load_acquire(&attr->size)) {
+ err = sysfs_btf_bin_attr_load(attr);
+ if (err)
+ return err;
+ }
+ /* Refresh file size or the open() caller will not see updated size. */
+ data_size = smp_load_acquire(&attr->size);
+ of->kn->attr.size = data_size;
+ if (of->file) {
+ struct inode *inode = file_inode(of->file);
+
+ if (inode)
+ i_size_write(inode, data_size);
+ }
+ return 0;
+}
+
+static ssize_t sysfs_btf_kernfs_read(struct kernfs_open_file *of, char *buf,
+ size_t bytes_requested, loff_t offset)
+{
+ struct bin_attribute *attr = of->kn->priv;
+ void *data;
+ size_t data_size;
+
+ data_size = smp_load_acquire(&attr->size);
+ if (offset >= data_size)
+ return 0;
+ if (offset + bytes_requested > data_size)
+ bytes_requested = data_size - offset;
+ data = READ_ONCE(attr->private);
+ memcpy(buf, data + offset, bytes_requested);
+
+ return bytes_requested;
+}
+
+static const struct kernfs_ops sysfs_btf_kernfs_ops = {
+ .open = sysfs_btf_kernfs_open,
+ .read = sysfs_btf_kernfs_read,
+};
+
+struct bin_attribute *sysfs_btf_add(const char *name, void *data, size_t data_size)
+{
+ struct bin_attribute *attr;
+ int err = 0;
+
+ attr = kzalloc_obj(*attr);
+ if (!attr)
+ return ERR_PTR(-ENOMEM);
+
+ sysfs_bin_attr_init(attr);
+ attr->attr.mode = 0444;
+ attr->size = data_size;
+ attr->private = data;
+ attr->read = sysfs_bin_attr_simple_read;
+ attr->attr.name = kstrdup(name, GFP_KERNEL);
+ if (!attr->attr.name) {
+ err = -ENOMEM;
+ goto err_free;
+ }
+ if (data_size > 0) {
+ err = sysfs_create_bin_file(btf_kobj, attr);
+ } else {
+ struct kernfs_node *node;
+
+ node = __kernfs_create_file(btf_kobj->sd, attr->attr.name,
+ attr->attr.mode, GLOBAL_ROOT_UID,
+ GLOBAL_ROOT_GID, data_size,
+ &sysfs_btf_kernfs_ops, attr, NULL, NULL);
+ if (IS_ERR(node))
+ err = PTR_ERR(node);
+ }
+ if (err) {
+ pr_warn("failed to register [%s] BTF in sysfs: %d\n", name, err);
+ goto err_free;
+ }
+ return attr;
+
+err_free:
+ kfree(attr->attr.name);
+ kfree(attr);
+ return ERR_PTR(err);
+}
+
+#else
+struct bin_attribute *sysfs_btf_add(const char *name, void *data, size_t data_size)
+{
+ return NULL;
+}
+#endif
+
+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)
+static void sysfs_btf_update(struct bin_attribute *attr, void *data, size_t data_size)
+{
+ if (!attr)
+ return;
+ WRITE_ONCE(attr->private, data);
+ /* Publish data before its non-zero size makes it readable. */
+ smp_store_release(&attr->size, data_size);
+}
+#endif
+
#ifdef CONFIG_DEBUG_INFO_BTF_MODULES
+#if IS_ENABLED(CONFIG_SYSFS)
+static void sysfs_btf_remove(struct bin_attribute *attr)
+{
+ sysfs_remove_bin_file(btf_kobj, attr);
+ kfree(attr->attr.name);
+ kfree(attr);
+}
+#else
+static void sysfs_btf_remove(struct bin_attribute *attr)
+{
+}
+#endif
+
struct btf_module {
struct list_head list;
struct module *module;
struct btf *btf;
struct bin_attribute *sysfs_attr;
+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)
+ struct bin_attribute *sysfs_inline_attr;
+ void *btf_inline_data;
+#endif
int flags;
};
@@ -8522,7 +9040,10 @@ static int btf_module_notify(struct notifier_block *nb, unsigned long op,
{
struct btf_module *btf_mod, *tmp;
struct module *mod = module;
+ struct bin_attribute *attr;
struct btf *btf;
+ void *inline_data = NULL, *relocated_inline_data = NULL;
+ unsigned int inline_data_size = 0;
int err = 0;
if (mod->btf_data_size == 0 ||
@@ -8532,13 +9053,20 @@ static int btf_module_notify(struct notifier_block *nb, unsigned long op,
switch (op) {
case MODULE_STATE_COMING:
+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)
+ inline_data = mod->btf_inline_data;
+ inline_data_size = mod->btf_inline_data_size;
+#endif
btf_mod = kzalloc_obj(*btf_mod);
if (!btf_mod) {
err = -ENOMEM;
goto out;
}
btf = btf_parse_module(mod->name, mod->btf_data, mod->btf_data_size,
- mod->btf_base_data, mod->btf_base_data_size);
+ mod->btf_base_data, mod->btf_base_data_size,
+ inline_data, inline_data_size,
+ strcmp(mod->name, "btf_vmlinux_inline") == 0,
+ &relocated_inline_data);
if (IS_ERR(btf)) {
kfree(btf_mod);
if (!IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) {
@@ -8553,6 +9081,7 @@ static int btf_module_notify(struct notifier_block *nb, unsigned long op,
err = btf_alloc_id(btf);
if (err) {
btf_free(btf);
+ kvfree(relocated_inline_data);
kfree(btf_mod);
goto out;
}
@@ -8561,34 +9090,43 @@ static int btf_module_notify(struct notifier_block *nb, unsigned long op,
mutex_lock(&btf_module_mutex);
btf_mod->module = module;
btf_mod->btf = btf;
+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)
+ btf_mod->btf_inline_data = relocated_inline_data;
+#endif
list_add(&btf_mod->list, &btf_modules);
mutex_unlock(&btf_module_mutex);
- if (IS_ENABLED(CONFIG_SYSFS)) {
- struct bin_attribute *attr;
-
- attr = kzalloc_obj(*attr);
- if (!attr)
- goto out;
+ attr = sysfs_btf_add(btf->name, btf->data, btf->data_size);
+ if (IS_ERR(attr)) {
+ err = 0;
+ goto out;
+ }
+ btf_mod->sysfs_attr = attr;
- sysfs_bin_attr_init(attr);
- attr->attr.name = btf->name;
- attr->attr.mode = 0444;
- attr->size = btf->data_size;
- attr->private = btf->data;
- attr->read = sysfs_bin_attr_simple_read;
+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)
+ if (relocated_inline_data) {
+ char name[MODULE_NAME_LEN + sizeof(".inline")];
- err = sysfs_create_bin_file(btf_kobj, attr);
- if (err) {
- pr_warn("failed to register module [%s] BTF in sysfs: %d\n",
- mod->name, err);
- kfree(attr);
+ if (strcmp(mod->name, "btf_vmlinux_inline") == 0) {
+ if (vmlinux_inline_attr)
+ sysfs_btf_update(vmlinux_inline_attr,
+ relocated_inline_data, inline_data_size);
+ else
+ kvfree(relocated_inline_data);
+ btf_mod->btf_inline_data = NULL;
+ break;
+ }
+ snprintf(name, sizeof(name), "%s.inline", mod->name);
+ attr = sysfs_btf_add(name, relocated_inline_data, inline_data_size);
+ if (IS_ERR(attr)) {
err = 0;
+ kvfree(relocated_inline_data);
+ btf_mod->btf_inline_data = NULL;
goto out;
}
-
- btf_mod->sysfs_attr = attr;
+ btf_mod->sysfs_inline_attr = attr;
}
+#endif
break;
case MODULE_STATE_LIVE:
@@ -8617,10 +9155,14 @@ static int btf_module_notify(struct notifier_block *nb, unsigned long op,
btf_free_id(btf_mod->btf);
list_del(&btf_mod->list);
if (btf_mod->sysfs_attr)
- sysfs_remove_bin_file(btf_kobj, btf_mod->sysfs_attr);
+ sysfs_btf_remove(btf_mod->sysfs_attr);
+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)
+ if (btf_mod->sysfs_inline_attr)
+ sysfs_btf_remove(btf_mod->sysfs_inline_attr);
+ kvfree(btf_mod->btf_inline_data);
+#endif
purge_cand_cache(btf_mod->btf);
btf_put(btf_mod->btf);
- kfree(btf_mod->sysfs_attr);
kfree(btf_mod);
break;
}
@@ -8638,6 +9180,12 @@ static struct notifier_block btf_module_nb = {
static int __init btf_module_init(void)
{
register_module_notifier(&btf_module_nb);
+#if IS_MODULE(CONFIG_DEBUG_INFO_BTF_INLINE)
+ /* Attribute data will be filled in on-demand if vmlinux.inline is read. */
+ vmlinux_inline_attr = sysfs_btf_add("vmlinux.inline", NULL, 0);
+ if (IS_ERR(vmlinux_inline_attr))
+ vmlinux_inline_attr = NULL;
+#endif
return 0;
}
diff --git a/kernel/bpf/btf_vmlinux_inline.c b/kernel/bpf/btf_vmlinux_inline.c
new file mode 100644
index 0000000000000..13ed962ffceb1
--- /dev/null
+++ b/kernel/bpf/btf_vmlinux_inline.c
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026, Oracle and/or its affiliates. */
+/*
+ * Provide kernel BTF inline function information for use by BPF tools.
+ */
+#include <linux/btf.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+
+#if IS_BUILTIN(CONFIG_DEBUG_INFO_BTF_INLINE)
+extern char __start_BTF_inline[];
+extern char __stop_BTF_inline[];
+#endif
+
+static int __init btf_vmlinux_inline_init(void)
+{
+#if IS_BUILTIN(CONFIG_DEBUG_INFO_BTF_INLINE)
+ size_t data_size = __stop_BTF_inline - __start_BTF_inline;
+
+ if (data_size)
+ sysfs_btf_add("vmlinux.inline", __start_BTF_inline,
+ data_size);
+#endif
+ return 0;
+}
+subsys_initcall(btf_vmlinux_inline_init);
+
+#if IS_MODULE(CONFIG_DEBUG_INFO_BTF_INLINE)
+static void __exit btf_vmlinux_inline_fini(void)
+{
+}
+module_exit(btf_vmlinux_inline_fini);
+#endif
+
+MODULE_DESCRIPTION("BTF inline information for vmlinux");
+MODULE_LICENSE("GPL");
diff --git a/kernel/module/main.c b/kernel/module/main.c
index 46dd8d25a6058..7e06ef1131131 100644
--- a/kernel/module/main.c
+++ b/kernel/module/main.c
@@ -2712,6 +2712,10 @@ static int find_module_sections(struct module *mod, struct load_info *info)
mod->btf_base_data = any_section_objs(info, ".BTF.base", 1,
&mod->btf_base_data_size);
#endif
+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)
+ mod->btf_inline_data = any_section_objs(info, ".BTF.inline", 1,
+ &mod->btf_inline_data_size);
+#endif
#ifdef CONFIG_JUMP_LABEL
mod->jump_entries = section_objs(info, "__jump_table",
sizeof(*mod->jump_entries),
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 1244dcac2294a..1b96d6acfdce9 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -425,6 +425,12 @@ config PAHOLE_HAS_LANG_EXCLUDE
otherwise it would emit malformed kernel and module binaries when
using DEBUG_INFO_BTF_MODULES.
+config PAHOLE_HAS_INLINE
+ def_bool PAHOLE_VERSION >= 131
+ help
+ Support for the "inline" BTF feature is available. It encodes
+ information about inline sites and how to retrieve their parameters.
+
config DEBUG_INFO_BTF_MODULES
bool "Generate BTF type information for kernel modules"
default y
@@ -432,6 +438,18 @@ config DEBUG_INFO_BTF_MODULES
help
Generate compact split BTF type information for kernel modules.
+config DEBUG_INFO_BTF_INLINE
+ tristate "Provide information about inline sites in BTF"
+ default n
+ depends on DEBUG_INFO_BTF && PAHOLE_HAS_INLINE && SYSFS
+ select DEBUG_INFO_BTF_MODULES if MODULES
+ help
+ Generate information about inline sites in .BTF.inline sections.
+ These sections contain split BTF relative to the kernel or module BTF
+ and are made available in /sys/kernel/btf with a ".inline" suffix.
+ The information describes inline locations and how to retrieve their
+ associated parameters.
+
config MODULE_ALLOW_BTF_MISMATCH
bool "Allow loading modules with non-matching BTF type info"
depends on DEBUG_INFO_BTF_MODULES
diff --git a/scripts/Makefile.btf b/scripts/Makefile.btf
index a1812985a61aa..d42dbc8d71992 100644
--- a/scripts/Makefile.btf
+++ b/scripts/Makefile.btf
@@ -22,7 +22,14 @@ endif
pahole-flags-$(CONFIG_PAHOLE_HAS_LANG_EXCLUDE) += --lang_exclude=rust
+btf-inline := $(CONFIG_DEBUG_INFO_BTF_INLINE)
+ifneq ($(btf-inline),)
+btf-inline-feat := inline
+pahole-flags-$(call test-ge, $(pahole-ver), 131) += --btf_features=$(btf-inline-feat)
+endif
+
export PAHOLE_FLAGS := $(pahole-flags-y)
+export BTF_INLINE := $(btf-inline)
resolve-btfids-flags-y :=
resolve-btfids-flags-$(CONFIG_WERROR) += --fatal_warnings
diff --git a/scripts/gen-btf.sh b/scripts/gen-btf.sh
index 8ca96eb10a69e..cd6588588fb8f 100755
--- a/scripts/gen-btf.sh
+++ b/scripts/gen-btf.sh
@@ -76,6 +76,7 @@ gen_btf_data()
${RESOLVE_BTFIDS} ${RESOLVE_BTFIDS_FLAGS} \
${BTF_BASE:+--btf_base ${BTF_BASE}} \
+ ${BTF_INLINE:+--inline} \
--btf ${btf1} "${ELF_FILE}"
}
@@ -83,14 +84,30 @@ gen_btf_o()
{
btf_data=${ELF_FILE}.btf.o
- # Create ${btf_data} which contains just .BTF section but no symbols. Add
+ # Create ${btf_data} which contains just BTF sections but no symbols. Add
# SHF_ALLOC because .BTF will be part of the vmlinux image. --strip-all
# deletes all symbols including __start_BTF and __stop_BTF, which will
# be redefined in the linker script.
echo "" | ${CC} ${CLANG_FLAGS} ${KBUILD_CPPFLAGS} ${KBUILD_CFLAGS} -fno-lto -c -x c -o ${btf_data} -
${OBJCOPY} --add-section .BTF=${ELF_FILE}.BTF \
--set-section-flags .BTF=alloc,readonly ${btf_data}
- ${OBJCOPY} --only-section=.BTF --strip-all ${btf_data}
+ ONLY_SEC="--only-section=.BTF"
+ btf_inline=${ELF_FILE}.BTF.inline
+ if [ "${BTF_INLINE}" = "m" ]; then
+ # vmlinux BTF is generated from a temporary ELF. Retain its
+ # vmlinux-relative inline BTF for btf_vmlinux_inline.ko.
+ if [ -f "${btf_inline}" ]; then
+ cp "${btf_inline}" "${objtree}/vmlinux.BTF.inline"
+ else
+ rm -f "${objtree}/vmlinux.BTF.inline"
+ fi
+ fi
+ if [ "${BTF_INLINE}" = "y" ] && [ -f "${btf_inline}" ]; then
+ ${OBJCOPY} --add-section .BTF.inline=${btf_inline} \
+ --set-section-flags .BTF.inline=alloc,readonly ${btf_data}
+ ONLY_SEC="${ONLY_SEC} --only-section=.BTF.inline"
+ fi
+ ${OBJCOPY} ${ONLY_SEC} --strip-all ${btf_data}
# Change e_type to ET_REL so that it can be used to link final vmlinux.
# GNU ld 2.35+ and lld do not allow an ET_EXEC input.
@@ -111,6 +128,17 @@ embed_btf_data()
if [ -f "${btf_base}" ]; then
${OBJCOPY} --add-section .BTF.base=${btf_base} ${ELF_FILE}
fi
+ btf_inline=${ELF_FILE}.BTF.inline
+ case "${ELF_FILE}" in
+ */btf_vmlinux_inline.ko)
+ # With CONFIG_DEBUG_INFO_BTF_INLINE=m, deliver vmlinux
+ # .BTF.inline via module
+ btf_inline=${BTF_BASE}.BTF.inline
+ ;;
+ esac
+ if [ -n "${BTF_INLINE}" ] && [ -f "${btf_inline}" ]; then
+ ${OBJCOPY} --add-section .BTF.inline=${btf_inline} ${ELF_FILE}
+ fi
btf_ids="${ELF_FILE}.BTF_ids"
if [ -f "${btf_ids}" ]; then
${RESOLVE_BTFIDS} --patch_btfids ${btf_ids} ${ELF_FILE}
@@ -121,6 +149,7 @@ cleanup()
{
rm -f "${ELF_FILE}.BTF.1"
rm -f "${ELF_FILE}.BTF"
+ rm -f "${ELF_FILE}.BTF.inline"
if [ "${BTFGEN_MODE}" = "module" ]; then
rm -f "${ELF_FILE}.BTF.base"
rm -f "${ELF_FILE}.BTF_ids"
diff --git a/tools/bpf/bpftool/Documentation/bpftool-btf.rst b/tools/bpf/bpftool/Documentation/bpftool-btf.rst
index cf75a7fa2d6bc..60c46748b8663 100644
--- a/tools/bpf/bpftool/Documentation/bpftool-btf.rst
+++ b/tools/bpf/bpftool/Documentation/bpftool-btf.rst
@@ -16,7 +16,7 @@ SYNOPSIS
**bpftool** [*OPTIONS*] **btf** *COMMAND*
-*OPTIONS* := { |COMMON_OPTIONS| | { **-B** | **--base-btf** } }
+*OPTIONS* := { |COMMON_OPTIONS| | { **-B** | **--base-btf** [ **-B** | **--base-btf** ] } }
*COMMANDS* := { **dump** | **help** }
@@ -87,7 +87,10 @@ OPTIONS
objects for kernel modules. To avoid duplicating all kernel symbols
required by modules, BTF objects for modules are "split", they are
built incrementally on top of the kernel (vmlinux) BTF object. So the
- base BTF reference should usually point to the kernel BTF.
+ base BTF reference should usually point to the kernel BTF. Multiple
+ base BTF objects can be passed, where the first is assumed to be the
+ root BTF, followed by split BTF based upon it, followed by split
+ BTF based upon the first split BTF and so on.
When the main BTF object to process (for example, the module BTF to
dump) is passed as a *FILE*, bpftool attempts to autodetect the path
diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c
index bca0a3982f09b..4d8991c99b484 100644
--- a/tools/bpf/bpftool/btf.c
+++ b/tools/bpf/bpftool/btf.c
@@ -51,6 +51,9 @@ static const char * const btf_kind_str[NR_BTF_KINDS] = {
[BTF_KIND_DECL_TAG] = "DECL_TAG",
[BTF_KIND_TYPE_TAG] = "TYPE_TAG",
[BTF_KIND_ENUM64] = "ENUM64",
+ [BTF_KIND_LOC_PARAM] = "LOC_PARAM",
+ [BTF_KIND_LOC_PROTO] = "LOC_PROTO",
+ [BTF_KIND_LOCSEC] = "LOCSEC",
};
struct sort_datum {
@@ -415,6 +418,88 @@ static int dump_btf_type(const struct btf *btf, __u32 id,
}
break;
}
+ case BTF_KIND_LOC_PARAM: {
+ const struct btf_loc_param *p = btf_loc_param(t);
+ __u32 *v = (__u32 *)(p + 1);
+ __u32 i, vlen = btf_vlen(t);
+
+ if (json_output) {
+ jsonw_uint_field(w, "size", t->size);
+ jsonw_uint_field(w, "flags", p->flags);
+ jsonw_uint_field(w, "vlen", vlen);
+ jsonw_name(w, "values");
+ jsonw_start_array(w);
+ } else {
+ printf(" size=%u flags=0x%x vlen=%u", t->size, p->flags, vlen);
+ }
+ for (i = 0; i < vlen; i++, v++) {
+ if (json_output) {
+ jsonw_start_object(w);
+ jsonw_uint_field(w, "value", *v);
+ jsonw_end_object(w);
+ } else {
+ printf("\n\t value=%u", *v);
+ }
+ }
+ if (json_output)
+ jsonw_end_array(w);
+ break;
+ }
+ case BTF_KIND_LOC_PROTO: {
+ __u32 *params = btf_loc_proto_params(t);
+ __u16 vlen = btf_vlen(t);
+ int i;
+
+ if (json_output) {
+ jsonw_uint_field(w, "vlen", vlen);
+ jsonw_name(w, "params");
+ jsonw_start_array(w);
+ } else {
+ printf(" vlen=%u", vlen);
+ }
+
+ for (i = 0; i < vlen; i++, params++) {
+ if (json_output) {
+ jsonw_start_object(w);
+ jsonw_uint_field(w, "type_id", *params);
+ jsonw_end_object(w);
+ } else {
+ printf("\n\t type_id=%u", *params);
+ }
+ }
+ if (json_output)
+ jsonw_end_array(w);
+ break;
+ }
+
+ case BTF_KIND_LOCSEC: {
+ struct btf_loc *locs = btf_locsec_locs(t);
+ __u32 i, vlen = btf_vlen(t);
+
+ if (json_output) {
+ jsonw_uint_field(w, "vlen", vlen);
+ jsonw_name(w, "locs");
+ jsonw_start_array(w);
+ } else {
+ printf(" vlen=%u", vlen);
+ }
+
+ for (i = 0; i < vlen; i++, locs++) {
+ if (json_output) {
+ jsonw_start_object(w);
+ jsonw_uint_field(w, "func_type_id", locs->func);
+ jsonw_uint_field(w, "loc_proto_type_id", locs->loc_proto);
+ jsonw_uint_field(w, "offset", locs->offset);
+ jsonw_end_object(w);
+ } else {
+ printf("\n\t func_type_id=%u loc_proto_type_id=%u offset=%u",
+ locs->func, locs->loc_proto, locs->offset);
+ }
+ }
+ if (json_output)
+ jsonw_end_array(w);
+ break;
+ }
default:
break;
}
diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c
index 5ababd8f7d0a0..9329f140b700f 100644
--- a/tools/bpf/bpftool/main.c
+++ b/tools/bpf/bpftool/main.c
@@ -515,7 +515,8 @@ int main(int argc, char **argv)
verifier_logs = true;
break;
case 'B':
- base_btf = btf__parse(optarg, NULL);
+ /* handle multi-split BTF */
+ base_btf = btf__parse_split(optarg, base_btf);
if (!base_btf) {
p_err("failed to parse base BTF at '%s': %d\n",
optarg, -errno);
diff --git a/tools/bpf/resolve_btfids/main.c b/tools/bpf/resolve_btfids/main.c
index 37d7e7224207d..20b228c4d6789 100644
--- a/tools/bpf/resolve_btfids/main.c
+++ b/tools/bpf/resolve_btfids/main.c
@@ -142,7 +142,9 @@ struct object {
struct btf *btf;
struct btf *base_btf;
+ struct btf *inline_btf;
bool distill_base;
+ bool extract_inline;
struct {
int fd;
@@ -154,6 +156,10 @@ struct object {
size_t strtabidx;
unsigned long idlist_addr;
int encoding;
+ const char **func_symbols;
+ u32 func_symbols_cnt;
+ u32 func_symbols_cap;
+ bool func_symbols_collected;
} efile;
struct rb_root sets;
@@ -572,6 +578,58 @@ static const char *find_name_by_addr(struct object *obj, Elf64_Addr addr)
return res ? res->name : NULL;
}
+static int cmp_func_symbol(const void *a, const void *b)
+{
+ const char * const *name = a;
+ const char * const *other = b;
+
+ return strcmp(*name, *other);
+}
+
+static int collect_func_symbols(struct object *obj)
+{
+ Elf_Scn *scn;
+ GElf_Shdr sh;
+ int n, i;
+
+ if (obj->efile.symbols_shndx == -1)
+ return 0;
+
+ scn = elf_getscn(obj->efile.elf, obj->efile.symbols_shndx);
+ if (!scn || gelf_getshdr(scn, &sh) != &sh)
+ return -EINVAL;
+ n = sh.sh_size / sh.sh_entsize;
+
+ for (i = 0; i < n; i++) {
+ GElf_Sym sym;
+ const char *name;
+
+ if (!gelf_getsym(obj->efile.symbols, i, &sym))
+ return -EINVAL;
+ if (GELF_ST_TYPE(sym.st_info) != STT_FUNC ||
+ sym.st_shndx == SHN_UNDEF || !sym.st_name)
+ continue;
+ name = elf_strptr(obj->efile.elf, obj->efile.strtabidx, sym.st_name);
+ if (!name)
+ return -EINVAL;
+ if (ensure_mem(&obj->efile.func_symbols, &obj->efile.func_symbols_cap,
+ obj->efile.func_symbols_cnt + 1))
+ return -ENOMEM;
+ obj->efile.func_symbols[obj->efile.func_symbols_cnt++] = name;
+ }
+
+ qsort(obj->efile.func_symbols, obj->efile.func_symbols_cnt,
+ sizeof(*obj->efile.func_symbols), cmp_func_symbol);
+ obj->efile.func_symbols_collected = true;
+ return 0;
+}
+
+static bool has_func_symbol(const struct object *obj, const char *name)
+{
+ return bsearch(&name, obj->efile.func_symbols, obj->efile.func_symbols_cnt,
+ sizeof(*obj->efile.func_symbols), cmp_func_symbol) != NULL;
+}
+
static int symbols_collect(struct object *obj)
{
Elf_Scn *scn = NULL;
@@ -1514,14 +1572,193 @@ static int btf2btf(struct object *obj)
* Sort types by name in ascending order resulting in all
* anonymous types being placed before named types.
*/
+struct btf_name_sort {
+ struct btf *btf;
+ bool extract_inline;
+ bool *transfer_funcs;
+ bool *transfer_protos;
+};
+
+static int btf_inline_kind_order(const struct btf_type *t, bool transfer_func,
+ bool transfer_proto)
+{
+ if (transfer_func)
+ return 1;
+ if (transfer_proto)
+ return 2;
+ if (btf_is_loc_param(t))
+ return 3;
+ if (btf_is_loc_proto(t))
+ return 4;
+ if (btf_is_locsec(t))
+ return 5;
+ return 0;
+}
+
+enum inline_ref_kind {
+ INLINE_REF_FUNC,
+ INLINE_REF_FUNC_PROTO,
+};
+
+static void mark_inline_ref(struct btf *btf, bool *refs, int start_id,
+ int type_cnt, __u32 type_id, enum inline_ref_kind ref_kind)
+{
+ const struct btf_type *t;
+
+ if (type_id < start_id || type_id >= type_cnt)
+ return;
+ t = btf__type_by_id(btf, type_id);
+ if ((ref_kind == INLINE_REF_FUNC && btf_is_func(t)) ||
+ (ref_kind == INLINE_REF_FUNC_PROTO && btf_is_func_proto(t)))
+ refs[type_id] = true;
+}
+
+static void mark_inline_type_refs(struct btf *btf, bool *refs, int start_id,
+ int type_cnt, const struct btf_type *t,
+ enum inline_ref_kind ref_kind)
+{
+ /*
+ * These are the only BTF kinds that can directly reference a FUNC or
+ * FUNC_PROTO (aside from LOCSEC).
+ */
+ switch (btf_kind(t)) {
+ case BTF_KIND_CONST:
+ case BTF_KIND_VOLATILE:
+ case BTF_KIND_RESTRICT:
+ case BTF_KIND_PTR:
+ case BTF_KIND_TYPEDEF:
+ case BTF_KIND_FUNC:
+ case BTF_KIND_VAR:
+ case BTF_KIND_DECL_TAG:
+ case BTF_KIND_TYPE_TAG:
+ mark_inline_ref(btf, refs, start_id, type_cnt, t->type, ref_kind);
+ break;
+ default:
+ return;
+ }
+}
+
+/* A FUNC can move to inline BTF only when LOCSEC is its sole BTF user. */
+static int find_inline_funcs(struct object *obj, bool **transfer_funcsp,
+ bool **transfer_protosp)
+{
+ bool *locsec_refs, *other_refs, *proto_candidates, *proto_base_refs;
+ int i, type_cnt, start_id = 0;
+ struct btf *btf = obj->btf;
+
+ type_cnt = btf__type_cnt(btf);
+ *transfer_funcsp = NULL;
+ *transfer_protosp = NULL;
+ if (btf__base_btf(btf))
+ start_id = btf__type_cnt(btf__base_btf(btf));
+
+ locsec_refs = calloc(type_cnt, sizeof(*locsec_refs));
+ other_refs = calloc(type_cnt, sizeof(*other_refs));
+ proto_candidates = calloc(type_cnt, sizeof(*proto_candidates));
+ proto_base_refs = calloc(type_cnt, sizeof(*proto_base_refs));
+ if (!locsec_refs || !other_refs || !proto_candidates || !proto_base_refs)
+ goto err;
+
+ /* First identify locsec FUNC references */
+ for (i = start_id; i < type_cnt; i++) {
+ const struct btf_type *t = btf__type_by_id(btf, i);
+ const struct btf_loc *loc;
+ int j;
+
+ if (!btf_is_locsec(t))
+ continue;
+ loc = btf_locsec_locs(t);
+ for (j = 0; j < btf_vlen(t); j++, loc++) {
+ if (loc->func >= start_id && loc->func < type_cnt &&
+ btf_is_func(btf__type_by_id(btf, loc->func)))
+ locsec_refs[loc->func] = true;
+ }
+ }
+
+ /*
+ * Then mark any additional references to them; references outside
+ * of LOCSEC make the FUNC ineligible for transfer.
+ */
+ for (i = start_id; i < type_cnt; i++) {
+ const struct btf_type *t = btf__type_by_id(btf, i);
+
+ if (btf_is_locsec(t))
+ continue;
+ mark_inline_type_refs(btf, other_refs, start_id, type_cnt, t,
+ INLINE_REF_FUNC);
+ }
+
+ for (i = start_id; i < type_cnt; i++) {
+ const struct btf_type *t = btf__type_by_id(btf, i);
+ const char *name;
+
+ /* If there are no locsec references, or we find additional
+ * non-locsec references, skip as ineligible.
+ */
+ if (!locsec_refs[i] || other_refs[i])
+ continue;
+ name = btf__name_by_offset(btf, t->name_off);
+ /* Keep out-of-line functions and BTF ID users in the main BTF. */
+ if (!obj->efile.func_symbols_collected || has_func_symbol(obj, name) ||
+ btf_id__find(&obj->funcs, name))
+ locsec_refs[i] = false;
+ }
+
+ for (i = start_id; i < type_cnt; i++) {
+ const struct btf_type *t;
+
+ if (!locsec_refs[i])
+ continue;
+ t = btf__type_by_id(btf, i);
+ proto_candidates[t->type] = true;
+ }
+
+ /* Mark every FUNC_PROTO directly needed by retained base BTF types. */
+ for (i = start_id; i < type_cnt; i++) {
+ const struct btf_type *t = btf__type_by_id(btf, i);
+
+ if (locsec_refs[i] || proto_candidates[i] ||
+ btf_is_loc_param(t) || btf_is_loc_proto(t) ||
+ btf_is_locsec(t))
+ continue;
+ mark_inline_type_refs(btf, proto_base_refs, start_id, type_cnt, t,
+ INLINE_REF_FUNC_PROTO);
+ }
+ for (i = start_id; i < type_cnt; i++)
+ proto_candidates[i] &= !proto_base_refs[i];
+
+ free(other_refs);
+ *transfer_funcsp = locsec_refs;
+ *transfer_protosp = proto_candidates;
+ free(proto_base_refs);
+ return 0;
+err:
+ free(locsec_refs);
+ free(other_refs);
+ free(proto_candidates);
+ free(proto_base_refs);
+ return -ENOMEM;
+}
+
static int cmp_type_names(const void *a, const void *b, void *priv)
{
- struct btf *btf = (struct btf *)priv;
+ struct btf_name_sort *sort = priv;
+ struct btf *btf = sort->btf;
const struct btf_type *ta = btf__type_by_id(btf, *(__u32 *)a);
const struct btf_type *tb = btf__type_by_id(btf, *(__u32 *)b);
const char *na, *nb;
+ int inline_order;
int r;
+ if (sort->extract_inline) {
+ inline_order = btf_inline_kind_order(ta, sort->transfer_funcs[*(__u32 *)a],
+ sort->transfer_protos[*(__u32 *)a]) -
+ btf_inline_kind_order(tb, sort->transfer_funcs[*(__u32 *)b],
+ sort->transfer_protos[*(__u32 *)b]);
+ if (inline_order)
+ return inline_order;
+ }
+
na = btf__str_by_offset(btf, ta->name_off);
nb = btf__str_by_offset(btf, tb->name_off);
r = strcmp(na, nb);
@@ -1532,8 +1769,60 @@ static int cmp_type_names(const void *a, const void *b, void *priv)
return *(__u32 *)a < *(__u32 *)b ? -1 : 1;
}
-static int sort_btf_by_name(struct btf *btf)
+struct loc_sort {
+ const __u32 *id_map;
+ __u32 start_id;
+};
+
+static __u32 projected_type_id(const struct loc_sort *sort, __u32 id)
+{
+ if (id < sort->start_id)
+ return id;
+ return sort->id_map[id - sort->start_id] & ~BTF_PERMUTE_ID_TRANSFER;
+}
+
+static int cmp_loc(const void *a, const void *b, void *priv)
{
+ const struct loc_sort *sort = priv;
+ const struct btf_loc *la = a, *lb = b;
+ __u32 afunc = projected_type_id(sort, la->func);
+ __u32 bfunc = projected_type_id(sort, lb->func);
+ __u32 aproto = projected_type_id(sort, la->loc_proto);
+ __u32 bproto = projected_type_id(sort, lb->loc_proto);
+
+ if (afunc != bfunc)
+ return afunc < bfunc ? -1 : 1;
+ if (la->offset != lb->offset)
+ return la->offset < lb->offset ? -1 : 1;
+ if (aproto != bproto)
+ return aproto < bproto ? -1 : 1;
+ return 0;
+}
+
+static void sort_locsecs(struct btf *btf, const __u32 *id_map, __u32 start_id,
+ int nr_types)
+{
+ struct loc_sort sort = { .id_map = id_map, .start_id = start_id };
+ int i;
+
+ for (i = 0; i < nr_types; i++) {
+ struct btf_type *t = (struct btf_type *)btf__type_by_id(btf, start_id + i);
+
+ if (btf_is_locsec(t))
+ qsort_r(btf_locsec_locs(t), btf_vlen(t), sizeof(struct btf_loc),
+ cmp_loc, &sort);
+ }
+}
+
+static int sort_btf_by_name(struct object *obj)
+{
+ struct btf *btf = obj->btf;
+ bool *transfer_funcs = NULL, *transfer_protos = NULL;
+ struct btf_name_sort sort = {
+ .btf = btf,
+ .extract_inline = obj->extract_inline,
+ };
+ LIBBPF_OPTS(btf_permute_opts, opts);
__u32 *permute_ids = NULL, *id_map = NULL;
int nr_types, i, err = 0;
__u32 start_id = 0, id;
@@ -1541,6 +1830,13 @@ static int sort_btf_by_name(struct btf *btf)
if (btf__base_btf(btf))
start_id = btf__type_cnt(btf__base_btf(btf));
nr_types = btf__type_cnt(btf) - start_id;
+ if (obj->extract_inline) {
+ err = find_inline_funcs(obj, &transfer_funcs, &transfer_protos);
+ if (err)
+ goto out;
+ sort.transfer_funcs = transfer_funcs;
+ sort.transfer_protos = transfer_protos;
+ }
permute_ids = calloc(nr_types, sizeof(*permute_ids));
if (!permute_ids) {
@@ -1557,21 +1853,37 @@ static int sort_btf_by_name(struct btf *btf)
for (i = 0, id = start_id; i < nr_types; i++, id++)
permute_ids[i] = id;
- qsort_r(permute_ids, nr_types, sizeof(*permute_ids), cmp_type_names,
- btf);
+ qsort_r(permute_ids, nr_types, sizeof(*permute_ids), cmp_type_names, &sort);
for (i = 0; i < nr_types; i++) {
id = permute_ids[i] - start_id;
id_map[id] = i + start_id;
+ if (obj->extract_inline &&
+ btf_inline_kind_order(btf__type_by_id(btf, permute_ids[i]),
+ transfer_funcs[permute_ids[i]],
+ transfer_protos[permute_ids[i]]))
+ id_map[id] |= BTF_PERMUTE_ID_TRANSFER;
}
- err = btf__permute(btf, id_map, nr_types, NULL);
+ /*
+ * Key LOCSEC records by their post-permutation function ID and offset
+ * since the tracer pattern will be to look up FUNC by name, then
+ * find LOCSEC records for that function.
+ */
+ sort_locsecs(btf, id_map, start_id, nr_types);
+
+ if (obj->extract_inline)
+ opts.transfer_btf = &obj->inline_btf;
+ err = btf__permute(btf, id_map, nr_types,
+ obj->extract_inline ? &opts : NULL);
if (err)
pr_err("FAILED: btf permute: %s\n", strerror(-err));
out:
free(permute_ids);
free(id_map);
+ free(transfer_funcs);
+ free(transfer_protos);
return err;
}
@@ -1599,7 +1911,7 @@ static int finalize_btf(struct object *obj)
obj->btf = btf;
}
- err = sort_btf_by_name(obj->btf);
+ err = sort_btf_by_name(obj);
if (err) {
pr_err("FAILED to sort BTF: %s\n", strerror(errno));
goto out_err;
@@ -1773,6 +2085,8 @@ int main(int argc, const char **argv)
"turn warnings into errors"),
OPT_BOOLEAN(0, "distill_base", &obj.distill_base,
"distill --btf_base and emit .BTF.base section data"),
+ OPT_BOOLEAN(0, "inline", &obj.extract_inline,
+ "extract location BTF into a .BTF.inline file"),
OPT_STRING(0, "patch_btfids", &btfids_path, "file",
"path to .BTF_ids section data blob to patch into ELF file"),
OPT_END()
@@ -1791,6 +2105,8 @@ int main(int argc, const char **argv)
if (elf_collect(&obj))
goto out;
+ if (obj.extract_inline && collect_func_symbols(&obj))
+ goto out;
/*
* We did not find .BTF_ids section or symbols section,
@@ -1841,11 +2157,18 @@ int main(int argc, const char **argv)
if (err)
goto out;
}
+ if (obj.inline_btf) {
+ err = make_out_path(out_path, sizeof(out_path), obj.path, BTF_ELF_SEC ".inline");
+ err = err ?: dump_raw_btf(obj.inline_btf, out_path);
+ if (err)
+ goto out;
+ }
if (!(fatal_warnings && warnings))
err = 0;
out:
btf__free(obj.base_btf);
+ btf__free(obj.inline_btf);
btf__free(obj.btf);
btf_id__free_all(&obj.structs);
btf_id__free_all(&obj.unions);
@@ -1853,6 +2176,7 @@ int main(int argc, const char **argv)
btf_id__free_all(&obj.funcs);
btf_id__free_all(&obj.sets);
free(obj.addr_syms);
+ free(obj.efile.func_symbols);
if (obj.efile.elf) {
elf_end(obj.efile.elf);
close(obj.efile.fd);
diff --git a/tools/include/uapi/linux/btf.h b/tools/include/uapi/linux/btf.h
index 618167cab4e61..6062c99580345 100644
--- a/tools/include/uapi/linux/btf.h
+++ b/tools/include/uapi/linux/btf.h
@@ -92,7 +92,9 @@ enum {
BTF_KIND_DECL_TAG = 17, /* Decl Tag */
BTF_KIND_TYPE_TAG = 18, /* Type Tag */
BTF_KIND_ENUM64 = 19, /* Enumeration up to 64-bit values */
-
+ BTF_KIND_LOC_PARAM = 20, /* Location parameter information */
+ BTF_KIND_LOC_PROTO = 21, /* Location prototype for site */
+ BTF_KIND_LOCSEC = 22, /* Location section */
NR_BTF_KINDS,
BTF_KIND_MAX = NR_BTF_KINDS - 1,
};
@@ -212,4 +214,65 @@ struct btf_enum64 {
__u32 val_hi32;
};
+/*
+ * BTF_KIND_LOC_PARAM is followed by a single "struct btf_loc_param"
+ * that contains flags specifying the contents of the vlen-specified
+ * number of 4-byte values that follow.
+ */
+struct btf_loc_param {
+ __u32 flags;
+};
+
+/*
+ * The combination of size, vlen and flags gives us the means to interpret
+ * the following vlen-specified set of 4-byte values:
+ *
+ * - a BTF_LOC_PARAM_CONST is a constant value; combination
+ * of size, vlen and _SIGNED flag determines it. If the value requires
+ * 64 bits it is stored in {lo,hi} order.
+ * - a BTF_LOC_PARAM_ADDR is an address that will be normalized with
+ * respect to kernel base address.
+ * - a BTF_LOC_PARAM_REG with vlen 1 is a simple register number;
+ * with vlen 2 it is a multi-register parameter.
+ * - a _REG | DEREF with vlen 1 dereferences the value in the register
+ * number specified.
+ * - a REG | DEREF | OFFSET with vlen specifies the register value in
+ * the first 4-byte value and the offset in the remainder.
+ * - binary logical operators operate on a combination of register
+ * number and constant value, aside from _NOT which operates on
+ * a register
+ */
+enum btf_loc_param_flags {
+ BTF_LOC_PARAM_SIGNED = 0x1,
+ BTF_LOC_PARAM_CONST = 0x2,
+ BTF_LOC_PARAM_ADDR = 0x4,
+ BTF_LOC_PARAM_REG = 0x8,
+ BTF_LOC_PARAM_DEREF = 0x10,
+ BTF_LOC_PARAM_OFFSET = 0x20,
+};
+
+/*
+ * BTF_KIND_LOC_PROTO specifies location prototypes; i.e. how locations relate
+ * to parameters; a struct btf_type of BTF_KIND_LOC_PROTO is followed by a
+ * a vlen-specified number of __u32 BTF type ids which specify the associated
+ * BTF_KIND_LOC_PARAM for each function parameter associated with the
+ * location. The type should either be 0 (no location info) or point at
+ * a BTF_KIND_LOC_PARAM.
+ */
+
+/*
+ * BTF_KIND_LOCSEC consists of vlen-specified number of "struct btf_loc"
+ * containing location site-specific information;
+ *
+ * - function (func)
+ * - location prototype type id (loc_proto)
+ * - address offset (offset) relative to kernel base address
+ */
+
+struct btf_loc {
+ __u32 func;
+ __u32 loc_proto;
+ __u32 offset;
+};
+
#endif /* _UAPI__LINUX_BTF_H__ */
diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index c783359977b46..eea325ccf768e 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -57,6 +57,9 @@ static struct btf_layout layouts[NR_BTF_KINDS] = {
[BTF_KIND_DECL_TAG] = { sizeof(struct btf_decl_tag), 0, 0 },
[BTF_KIND_TYPE_TAG] = { 0, 0, 0 },
[BTF_KIND_ENUM64] = { 0, sizeof(struct btf_enum64), 0 },
+[BTF_KIND_LOC_PARAM] = { sizeof(struct btf_loc_param), sizeof(__u32), 0 },
+[BTF_KIND_LOC_PROTO] = { 0, sizeof(__u32), 0 },
+[BTF_KIND_LOCSEC] = { 0, sizeof(struct btf_loc), 0 },
};
struct btf {
@@ -486,6 +489,12 @@ static int btf_type_size(const struct btf *btf, const struct btf_type *t)
return base_size + vlen * sizeof(struct btf_var_secinfo);
case BTF_KIND_DECL_TAG:
return base_size + sizeof(struct btf_decl_tag);
+ case BTF_KIND_LOC_PARAM:
+ return base_size + sizeof(struct btf_loc_param) + vlen * sizeof(__u32);
+ case BTF_KIND_LOC_PROTO:
+ return base_size + vlen * sizeof(__u32);
+ case BTF_KIND_LOCSEC:
+ return base_size + vlen * sizeof(struct btf_loc);
default:
return btf_type_size_unknown(btf, t);
}
@@ -501,12 +510,15 @@ static void btf_bswap_type_base(struct btf_type *t)
static int btf_bswap_type_rest(struct btf_type *t)
{
struct btf_var_secinfo *v;
+ struct btf_loc_param *lp;
struct btf_enum64 *e64;
struct btf_member *m;
struct btf_array *a;
struct btf_param *p;
struct btf_enum *e;
+ struct btf_loc *l;
__u32 vlen = btf_vlen(t);
+ __u32 *d;
int i;
switch (btf_kind(t)) {
@@ -569,6 +581,23 @@ static int btf_bswap_type_rest(struct btf_type *t)
case BTF_KIND_DECL_TAG:
btf_decl_tag(t)->component_idx = bswap_32(btf_decl_tag(t)->component_idx);
return 0;
+ case BTF_KIND_LOC_PARAM:
+ lp = btf_loc_param(t);
+ lp->flags = bswap_32(lp->flags);
+ for (i = 0, d = (__u32 *)(lp + 1); i < vlen; i++, d++)
+ *d = bswap_32(*d);
+ return 0;
+ case BTF_KIND_LOC_PROTO:
+ for (i = 0, d = btf_loc_proto_params(t); i < vlen; i++, d++)
+ *d = bswap_32(*d);
+ return 0;
+ case BTF_KIND_LOCSEC:
+ for (i = 0, l = btf_locsec_locs(t); i < vlen; i++, l++) {
+ l->func = bswap_32(l->func);
+ l->loc_proto = bswap_32(l->loc_proto);
+ l->offset = bswap_32(l->offset);
+ }
+ return 0;
default:
pr_debug("Unsupported BTF_KIND:%u\n", btf_kind(t));
return -EINVAL;
@@ -745,6 +774,33 @@ static int btf_validate_type(const struct btf *btf, const struct btf_type *t, __
}
break;
}
+ case BTF_KIND_LOC_PARAM:
+ break;
+ case BTF_KIND_LOC_PROTO: {
+ __u32 *p = btf_loc_proto_params(t);
+
+ n = btf_vlen(t);
+ for (i = 0; i < n; i++, p++) {
+ err = btf_validate_id(btf, *p, id);
+ if (err)
+ return err;
+ }
+ break;
+ }
+ case BTF_KIND_LOCSEC: {
+ const struct btf_loc *l = btf_locsec_locs(t);
+
+ n = btf_vlen(t);
+ for (i = 0; i < n; i++, l++) {
+ if (!err)
+ err = btf_validate_id(btf, l->func, id);
+ if (!err)
+ err = btf_validate_id(btf, l->loc_proto, id);
+ if (err)
+ return err;
+ }
+ break;
+ }
default:
/* Kind may be represented in kind layout information. */
if (btf_type_size_unknown(btf, t) < 0) {
@@ -2091,16 +2147,9 @@ int btf__find_str(struct btf *btf, const char *s)
* - > 0 offset into string section, on success;
* - < 0, on error.
*/
-int btf__add_str(struct btf *btf, const char *s)
+static int btf_add_local_str(struct btf *btf, const char *s)
{
- int off;
- int err;
-
- if (btf->base_btf) {
- off = btf__find_str(btf->base_btf, s);
- if (off != -ENOENT)
- return off;
- }
+ int off, err;
err = btf_ensure_modifiable(btf);
if (err)
@@ -2111,10 +2160,24 @@ int btf__add_str(struct btf *btf, const char *s)
return libbpf_err(off);
btf->hdr.str_len = strset__data_size(btf->strs_set);
+ btf->strs_deduped = false;
return btf->start_str_off + off;
}
+int btf__add_str(struct btf *btf, const char *s)
+{
+ int off;
+
+ if (btf->base_btf) {
+ off = btf__find_str(btf->base_btf, s);
+ if (off != -ENOENT)
+ return off;
+ }
+
+ return btf_add_local_str(btf, s);
+}
+
static void *btf_add_type_mem(struct btf *btf, size_t add_sz)
{
return libbpf_add_mem(&btf->types_data, &btf->types_data_cap, 1,
@@ -2162,6 +2225,7 @@ struct btf_pipe {
const struct btf *src;
struct btf *dst;
struct hashmap *str_off_map; /* map string offsets from src to dst */
+ bool force_local_strs;
};
static int btf_rewrite_str(struct btf_pipe *p, __u32 *str_off)
@@ -2178,7 +2242,10 @@ static int btf_rewrite_str(struct btf_pipe *p, __u32 *str_off)
return 0;
}
- off = btf__add_str(p->dst, btf__str_by_offset(p->src, *str_off));
+ if (p->force_local_strs)
+ off = btf_add_local_str(p->dst, btf__str_by_offset(p->src, *str_off));
+ else
+ off = btf__add_str(p->dst, btf__str_by_offset(p->src, *str_off));
if (off < 0)
return off;
@@ -3344,6 +3411,208 @@ int btf__add_decl_attr(struct btf *btf, const char *value, int ref_type_id,
return btf_add_decl_tag(btf, value, ref_type_id, component_idx, 1);
}
+/*
+ * Append new BTF_KIND_LOC_PARAM with specified size and flags. Values are
+ * added via btf__add_loc_param_value().
+ *
+ * Returns:
+ * - >0, type ID of newly added BTF type;
+ * - <0, on error.
+ */
+int btf__add_loc_param(struct btf *btf, __u32 size, __u32 flags)
+{
+ struct btf_loc_param *p;
+ struct btf_type *t;
+ int sz, err;
+
+ err = btf_ensure_modifiable(btf);
+ if (err)
+ return libbpf_err(err);
+
+ sz = sizeof(struct btf_type) + sizeof(*p);
+ t = btf_add_type_mem(btf, sz);
+ if (!t)
+ return libbpf_err(-ENOMEM);
+
+ t->name_off = 0;
+ t->info = btf_type_info(BTF_KIND_LOC_PARAM, 0, 0);
+ t->size = size;
+
+ p = btf_loc_param(t);
+ p->flags = flags;
+
+ return btf_commit_type(btf, sz);
+}
+
+int btf__add_loc_param_value(struct btf *btf, __u32 value)
+{
+ struct btf_type *t;
+ int sz, err;
+ __u32 *v;
+
+ /* last type should be BTF_KIND_LOC_PARAM */
+ if (btf->nr_types == 0)
+ return libbpf_err(-EINVAL);
+ t = btf_last_type(btf);
+ if (!btf_is_loc_param(t))
+ return libbpf_err(-EINVAL);
+
+ /* decompose and invalidate raw data */
+ err = btf_ensure_modifiable(btf);
+ if (err)
+ return libbpf_err(err);
+
+ sz = sizeof(value);
+ v = btf_add_type_mem(btf, sz);
+ if (!v)
+ return libbpf_err(-ENOMEM);
+ *v = value;
+
+ /* update parent type's vlen */
+ t = btf_last_type(btf);
+ err = btf_type_inc_vlen(t);
+ if (err)
+ return libbpf_err(err);
+
+ btf_hdr_update_type_len(btf, btf->hdr.type_len + sz);
+ return 0;
+}
+
+/*
+ * Append new BTF_KIND_LOC_PROTO
+ *
+ * The prototype is then populated with 0 or more BTF_KIND_LOC_PARAMs via
+ * btf__add_loc_proto_param(); similar to how btf__add_func_param() adds
+ * parameters to a FUNC_PROTO.
+ *
+ * Returns:
+ * - >0, type ID of newly added BTF type;
+ * - <0, on error.
+ */
+int btf__add_loc_proto(struct btf *btf)
+{
+ struct btf_type *t;
+ int err;
+
+ err = btf_ensure_modifiable(btf);
+ if (err)
+ return libbpf_err(err);
+
+ t = btf_add_type_mem(btf, sizeof(struct btf_type));
+ if (!t)
+ return libbpf_err(-ENOMEM);
+
+ t->name_off = 0;
+ t->info = btf_type_info(BTF_KIND_LOC_PROTO, 0, 0);
+ t->size = 0;
+
+ return btf_commit_type(btf, sizeof(struct btf_type));
+}
+
+int btf__add_loc_proto_param(struct btf *btf, __u32 id)
+{
+ struct btf_type *t;
+ int sz, err;
+ __u32 *p;
+
+ if (validate_type_id(id))
+ return libbpf_err(-EINVAL);
+
+ /* last type should be BTF_KIND_LOC_PROTO */
+ if (btf->nr_types == 0)
+ return libbpf_err(-EINVAL);
+ t = btf_last_type(btf);
+ if (!btf_is_loc_proto(t))
+ return libbpf_err(-EINVAL);
+
+ /* decompose and invalidate raw data */
+ err = btf_ensure_modifiable(btf);
+ if (err)
+ return libbpf_err(err);
+
+ sz = sizeof(__u32);
+ p = btf_add_type_mem(btf, sz);
+ if (!p)
+ return libbpf_err(-ENOMEM);
+ *p = id;
+
+ /* update parent type's vlen */
+ t = btf_last_type(btf);
+ err = btf_type_inc_vlen(t);
+ if (err)
+ return libbpf_err(err);
+
+ btf_hdr_update_type_len(btf, btf->hdr.type_len + sz);
+ return 0;
+}
+
+int btf__add_locsec(struct btf *btf, const char *name)
+{
+ struct btf_type *t;
+ int name_off = 0;
+ int err;
+
+ err = btf_ensure_modifiable(btf);
+ if (err)
+ return libbpf_err(err);
+
+ t = btf_add_type_mem(btf, sizeof(struct btf_type));
+ if (!t)
+ return libbpf_err(-ENOMEM);
+
+ if (!str_is_empty(name)) {
+ name_off = btf__add_str(btf, name);
+ if (name_off < 0)
+ return name_off;
+ }
+ t->name_off = name_off;
+ t->info = btf_type_info(BTF_KIND_LOCSEC, 0, 0);
+ t->size = 0;
+
+ return btf_commit_type(btf, sizeof(struct btf_type));
+}
+
+int btf__add_locsec_loc(struct btf *btf, __u32 func, __u32 loc_proto,
+ __u32 offset)
+{
+ struct btf_type *t;
+ struct btf_loc *l;
+ int sz, err;
+
+ if (validate_type_id(func) || validate_type_id(loc_proto))
+ return libbpf_err(-EINVAL);
+
+ /* last type should be BTF_KIND_LOCSEC */
+ if (btf->nr_types == 0)
+ return libbpf_err(-EINVAL);
+ t = btf_last_type(btf);
+ if (!btf_is_locsec(t))
+ return libbpf_err(-EINVAL);
+
+ /* decompose and invalidate raw data */
+ err = btf_ensure_modifiable(btf);
+ if (err)
+ return libbpf_err(err);
+
+ sz = sizeof(*l);
+ l = btf_add_type_mem(btf, sz);
+ if (!l)
+ return libbpf_err(-ENOMEM);
+
+ l->func = func;
+ l->loc_proto = loc_proto;
+ l->offset = offset;
+
+ /* update parent type's vlen */
+ t = btf_last_type(btf);
+ err = btf_type_inc_vlen(t);
+ if (err)
+ return libbpf_err(err);
+
+ btf_hdr_update_type_len(btf, btf->hdr.type_len + sz);
+ return 0;
+}
+
struct btf_ext_sec_info_param {
__u32 off;
__u32 len;
@@ -4114,8 +4383,8 @@ static struct btf_dedup *btf_dedup_new(struct btf *btf, const struct btf_dedup_o
for (i = 1; i < type_cnt; i++) {
struct btf_type *t = btf_type_by_id(d->btf, i);
- /* VAR and DATASEC are never deduped and are self-canonical */
- if (btf_is_var(t) || btf_is_datasec(t))
+ /* VAR, DATASEC and LOCSEC are never deduped and are self-canonical */
+ if (btf_is_var(t) || btf_is_datasec(t) || btf_is_locsec(t))
d->map[i] = i;
else
d->map[i] = BTF_UNPROCESSED_ID;
@@ -4255,6 +4524,19 @@ static int btf_dedup_strings(struct btf_dedup *d)
return err;
}
+/* Compact strings after a type-only transformation such as btf__permute(). */
+static int btf_compact_strings(struct btf *btf, struct btf_ext *btf_ext)
+{
+ struct btf_dedup d = {
+ .btf = btf,
+ .btf_ext = btf_ext,
+ };
+
+ /* Types may have been removed since the last string compaction. */
+ btf->strs_deduped = false;
+ return btf_dedup_strings(&d);
+}
+
/*
* Calculate type signature hash of TYPEDEF, ignoring referenced type IDs,
* as referenced type IDs equivalence is established separately during type
@@ -4395,6 +4677,45 @@ static bool btf_compat_enum(struct btf_type *t1, struct btf_type *t2)
btf_is_any_enum(t1) && btf_is_any_enum(t2);
}
+static long btf_hash_loc_param(struct btf_type *t)
+{
+ long h = btf_hash_common(t);
+ __u32 *v = (__u32 *)btf_loc_param(t);
+ int i, vlen = btf_vlen(t);
+
+ for (i = 0; i <= vlen; i++, v++)
+ h = hash_combine(h, *v);
+ return h;
+}
+
+static long btf_hash_loc_proto(struct btf_type *t)
+{
+ __u32 *p = btf_loc_proto_params(t);
+ long h = btf_hash_common(t);
+ int i, vlen = btf_vlen(t);
+
+ for (i = 0; i < vlen; i++, p++)
+ h = hash_combine(h, *p);
+ return h;
+}
+
+static bool btf_equal_loc_param(struct btf_type *t1, struct btf_type *t2)
+{
+ struct btf_loc_param *p1 = btf_loc_param(t1);
+ struct btf_loc_param *p2 = btf_loc_param(t2);
+ __u32 *v1 = (__u32 *)(p1 + 1);
+ __u32 *v2 = (__u32 *)(p2 + 1);
+ int i, vlen = btf_vlen(t1);
+
+ if (!btf_equal_common(t1, t2))
+ return false;
+ for (i = 0; i < vlen; i++, v1++, v2++) {
+ if (*v1 != *v2)
+ return false;
+ }
+ return true;
+}
+
/*
* Calculate type signature hash of STRUCT/UNION, ignoring referenced type IDs,
* as referenced type IDs equivalence is established separately during type
@@ -4622,6 +4943,12 @@ static int btf_dedup_prep(struct btf_dedup *d)
case BTF_KIND_FUNC_PROTO:
h = btf_hash_fnproto(t);
break;
+ case BTF_KIND_LOC_PARAM:
+ h = btf_hash_loc_param(t);
+ break;
+ case BTF_KIND_LOC_PROTO:
+ h = btf_hash_loc_proto(t);
+ break;
default:
pr_debug("unknown kind %d for type [%d]\n", btf_kind(t), type_id);
return -EINVAL;
@@ -4664,6 +4991,8 @@ static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)
case BTF_KIND_DATASEC:
case BTF_KIND_DECL_TAG:
case BTF_KIND_TYPE_TAG:
+ case BTF_KIND_LOC_PROTO:
+ case BTF_KIND_LOCSEC:
return 0;
case BTF_KIND_INT:
@@ -4713,6 +5042,18 @@ static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)
}
break;
+ case BTF_KIND_LOC_PARAM:
+ h = btf_hash_loc_param(t);
+ for_each_dedup_cand(d, hash_entry, h) {
+ cand_id = hash_entry->value;
+ cand = btf_type_by_id(d->btf, cand_id);
+ if (btf_equal_loc_param(t, cand)) {
+ new_id = cand_id;
+ break;
+ }
+ }
+ break;
+
default:
return -EINVAL;
}
@@ -5145,6 +5486,13 @@ static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id,
return 1;
}
+ case BTF_KIND_LOC_PARAM:
+ return btf_equal_loc_param(cand_type, canon_type);
+
+ case BTF_KIND_LOC_PROTO:
+ case BTF_KIND_LOCSEC:
+ return 0;
+
default:
return -EINVAL;
}
@@ -5489,6 +5837,41 @@ static int btf_dedup_ref_type(struct btf_dedup *d, __u32 type_id)
break;
}
+ case BTF_KIND_LOC_PROTO: {
+ __u32 *p1, *p2;
+ __u32 i, vlen;
+
+ p1 = btf_loc_proto_params(t);
+ vlen = btf_vlen(t);
+
+ for (i = 0; i < vlen; i++, p1++) {
+ ref_type_id = btf_dedup_ref_type(d, *p1);
+ if (ref_type_id < 0)
+ return ref_type_id;
+ *p1 = ref_type_id;
+ }
+
+ h = btf_hash_loc_proto(t);
+ for_each_dedup_cand(d, hash_entry, h) {
+ cand_id = hash_entry->value;
+ cand = btf_type_by_id(d->btf, cand_id);
+ if (!btf_equal_common(t, cand))
+ continue;
+ vlen = btf_vlen(cand);
+ p1 = btf_loc_proto_params(t);
+ p2 = btf_loc_proto_params(cand);
+ if (vlen == 0) {
+ new_id = cand_id;
+ break;
+ }
+ if (memcmp(p1, p2, vlen * sizeof(__u32)) == 0) {
+ new_id = cand_id;
+ break;
+ }
+ }
+ break;
+ }
+
default:
return -EINVAL;
}
@@ -5970,8 +6353,11 @@ static int btf_add_distilled_type_ids(struct btf_distill *dist, __u32 i)
case BTF_KIND_CONST:
case BTF_KIND_RESTRICT:
case BTF_KIND_VOLATILE:
+ case BTF_KIND_FUNC:
case BTF_KIND_FUNC_PROTO:
case BTF_KIND_TYPE_TAG:
+ case BTF_KIND_LOC_PARAM:
+ case BTF_KIND_LOC_PROTO:
dist->id_map[*id] = *id;
break;
default:
@@ -5997,7 +6383,7 @@ static int btf_add_distilled_type_ids(struct btf_distill *dist, __u32 i)
static int btf_add_distilled_types(struct btf_distill *dist)
{
- bool adding_to_base = dist->pipe.dst->start_id == 1;
+ bool adding_to_base = dist->pipe.dst->base_btf == NULL;
int id = btf__type_cnt(dist->pipe.dst);
struct btf_type *t;
int i, err = 0;
@@ -6065,8 +6451,12 @@ static int btf_add_distilled_types(struct btf_distill *dist)
case BTF_KIND_CONST:
case BTF_KIND_RESTRICT:
case BTF_KIND_VOLATILE:
+ case BTF_KIND_FUNC:
case BTF_KIND_FUNC_PROTO:
case BTF_KIND_TYPE_TAG:
+ case BTF_KIND_LOC_PARAM:
+ case BTF_KIND_LOC_PROTO:
+ case BTF_KIND_LOCSEC:
/* All other types are added to split BTF. */
if (adding_to_base)
continue;
@@ -6240,7 +6630,7 @@ void btf_set_base_btf(struct btf *btf, const struct btf *base_btf)
int btf__relocate(struct btf *btf, const struct btf *base_btf)
{
- int err = btf_relocate(btf, base_btf, NULL);
+ int err = btf_relocate(btf, base_btf, NULL, NULL);
if (!err)
btf->owns_base = false;
@@ -6251,8 +6641,15 @@ struct btf_permute {
struct btf *btf;
__u32 *id_map;
__u32 start_offs;
+ __u32 transfer_start_id;
+ bool reject_transfer;
};
+static bool btf_permute_id_is_transfer(__u32 id)
+{
+ return id & BTF_PERMUTE_ID_TRANSFER;
+}
+
/* Callback function to remap individual type ID references */
static int btf_permute_remap_type_id(__u32 *type_id, void *ctx)
{
@@ -6267,18 +6664,54 @@ static int btf_permute_remap_type_id(__u32 *type_id, void *ctx)
return -EINVAL;
*type_id = p->id_map[new_id - p->btf->start_id + p->start_offs];
+ if (p->reject_transfer && *type_id >= p->transfer_start_id)
+ return -EINVAL;
+ return 0;
+}
+
+/*
+ * String offsets in a split BTF are absolute. If its parent string table is
+ * compacted after the split was populated, move its local string offsets to
+ * start at the parent's new end.
+ */
+static int btf_permute_rebase_split_strs(struct btf *split_btf, __u32 old_start_str_off)
+{
+ __s64 delta = split_btf->base_btf->start_str_off + split_btf->base_btf->hdr.str_len -
+ old_start_str_off;
+ int i;
+
+ for (i = 0; i < split_btf->nr_types; i++) {
+ struct btf_field_iter it;
+ struct btf_type *t = btf_type_by_id(split_btf,
+ split_btf->start_id + i);
+ __u32 *str_off;
+ int err;
+
+ err = btf_field_iter_init(&it, t, BTF_FIELD_ITER_STRS);
+ if (err)
+ return err;
+ while ((str_off = btf_field_iter_next(&it))) {
+ if (*str_off >= old_start_str_off)
+ *str_off += delta;
+ }
+ }
+
+ btf_set_base_btf(split_btf, split_btf->base_btf);
return 0;
}
int btf__permute(struct btf *btf, __u32 *id_map, __u32 id_map_cnt,
const struct btf_permute_opts *opts)
{
- struct btf_permute p;
- struct btf_ext *btf_ext;
+ __u32 n, start_offs = 0, nr_transfer = 0, nr_base_types;
+ __u32 next_base_id, next_transfer_id, old_str_off = 0;
+ struct btf **transfer_btfp, *transfer_btf = NULL;
+ __u32 *new_id_map = NULL, *order_map = NULL;
void *nt, *new_types = NULL;
- __u32 *order_map = NULL;
+ struct btf_ext *btf_ext;
+ bool committed = false;
+ struct btf_permute p;
int err = 0, i;
- __u32 n, id, start_offs = 0;
if (!OPTS_VALID(opts, btf_permute_opts))
return libbpf_err(-EINVAL);
@@ -6295,7 +6728,21 @@ int btf__permute(struct btf *btf, __u32 *id_map, __u32 id_map_cnt,
if (id_map_cnt != n)
return libbpf_err(-EINVAL);
- /* record the sequence of types */
+ transfer_btfp = OPTS_GET(opts, transfer_btf, NULL);
+ if (transfer_btfp)
+ *transfer_btfp = NULL;
+
+ /*
+ * Make a working copy of the id map; it will be modified (such as
+ * removing transfer flags) unlike the caller-passed original.
+ */
+ new_id_map = calloc(id_map_cnt, sizeof(*new_id_map));
+ if (!new_id_map) {
+ err = -ENOMEM;
+ goto done;
+ }
+ memcpy(new_id_map, id_map, id_map_cnt * sizeof(*new_id_map));
+
order_map = calloc(id_map_cnt, sizeof(*id_map));
if (!order_map) {
err = -ENOMEM;
@@ -6312,33 +6759,82 @@ int btf__permute(struct btf *btf, __u32 *id_map, __u32 id_map_cnt,
if (err)
goto done;
+ /*
+ * Build the inverse map: for each requested destination ID, record the
+ * original type ID that will occupy it. order_map is indexed by the
+ * destination ID relative to the first ID represented by id_map. The
+ * caller id_map maps old IDs to requested destinations, but BTF types
+ * must be emitted in destination-ID order; the inverse map supplies the
+ * old type to copy for each output slot without repeatedly searching
+ * id_map.
+ */
for (i = start_offs; i < id_map_cnt; i++) {
- id = id_map[i];
- if (id < btf->start_id || id >= btf__type_cnt(btf)) {
+ __u32 requested_id = new_id_map[i];
+ __u32 order_idx;
+
+ if (btf_permute_id_is_transfer(requested_id)) {
+ if (!transfer_btfp) {
+ err = -EINVAL;
+ goto done;
+ }
+ requested_id &= ~BTF_PERMUTE_ID_TRANSFER;
+ nr_transfer++;
+ }
+ if (requested_id < btf->start_id || requested_id >= btf__type_cnt(btf)) {
err = -EINVAL;
goto done;
}
- id -= btf->start_id - start_offs;
+ order_idx = requested_id - btf->start_id + start_offs;
/* cannot be mapped to the same ID */
- if (order_map[id]) {
+ if (order_map[order_idx]) {
err = -EINVAL;
goto done;
}
- order_map[id] = i + btf->start_id - start_offs;
+ order_map[order_idx] = i + btf->start_id - start_offs;
}
+ /*
+ * Turn requested destination IDs into final IDs. Retained types
+ * occupy the compacted parent BTF first; transferred types immediately
+ * follow them, which is where they will start in returned split BTF.
+ */
+ nr_base_types = n - nr_transfer - start_offs;
+ next_base_id = btf->start_id;
+ next_transfer_id = btf->start_id + nr_base_types;
+ for (i = start_offs; i < id_map_cnt; i++) {
+ __u32 old_id = order_map[i];
+ __u32 old_id_idx = old_id - btf->start_id + start_offs;
+
+ if (btf_permute_id_is_transfer(new_id_map[old_id_idx]))
+ new_id_map[old_id_idx] = next_transfer_id++;
+ else
+ new_id_map[old_id_idx] = next_base_id++;
+ }
+
+ /*
+ * Copy retained types and reject references into the split (transfer)
+ * BTF from the original BTF.
+ */
p.btf = btf;
- p.id_map = id_map;
+ p.id_map = new_id_map;
p.start_offs = start_offs;
+ p.transfer_start_id = btf->start_id + nr_base_types;
+ p.reject_transfer = true;
nt = new_types;
for (i = start_offs; i < id_map_cnt; i++) {
struct btf_field_iter it;
const struct btf_type *t;
__u32 *type_id;
+ __u32 old_id;
+ __u32 old_id_idx;
int type_size;
- id = order_map[i];
- t = btf__type_by_id(btf, id);
+ old_id = order_map[i];
+ old_id_idx = old_id - btf->start_id + start_offs;
+
+ if (new_id_map[old_id_idx] >= btf->start_id + nr_base_types)
+ continue;
+ t = btf__type_by_id(btf, old_id);
type_size = btf_type_size(btf, t);
memcpy(nt, t, type_size);
@@ -6348,14 +6844,71 @@ int btf__permute(struct btf *btf, __u32 *id_map, __u32 id_map_cnt,
goto done;
while ((type_id = btf_field_iter_next(&it))) {
err = btf_permute_remap_type_id(type_id, &p);
- if (err)
+ if (err < 0)
goto done;
}
nt += type_size;
}
- /* fix up referenced IDs for btf_ext */
+ /*
+ * Copy transferred types into a split BTF. They can reference both
+ * retained parent types and other transferred types, so do not reject
+ * final IDs in the split-ID range while rewriting their references.
+ */
+ if (nr_transfer) {
+ struct btf_permute transfer_p;
+
+ transfer_btf = btf__new_empty_split(btf);
+ if (!transfer_btf) {
+ err = -errno;
+ goto done;
+ }
+ old_str_off = transfer_btf->start_str_off;
+
+ transfer_p = p;
+ /* Split transfer types may refer to other split types */
+ transfer_p.reject_transfer = false;
+ for (i = start_offs; i < id_map_cnt; i++) {
+ struct btf_field_iter it;
+ struct btf_pipe pipe = {
+ .src = btf,
+ .dst = transfer_btf,
+ .force_local_strs = true,
+ };
+ const struct btf_type *t;
+ struct btf_type *new_t;
+ __u32 *type_id;
+ __u32 old_id;
+ __u32 old_id_idx;
+ int new_id;
+
+ old_id = order_map[i];
+ old_id_idx = old_id - btf->start_id + start_offs;
+
+ if (!btf_permute_id_is_transfer(id_map[old_id_idx]))
+ continue;
+ t = btf__type_by_id(btf, old_id);
+ new_id = btf_add_type(&pipe, t);
+ if (new_id < 0) {
+ err = new_id;
+ goto done;
+ }
+ new_t = btf_type_by_id(transfer_btf, new_id);
+ err = btf_field_iter_init(&it, new_t, BTF_FIELD_ITER_IDS);
+ if (!err) {
+ while ((type_id = btf_field_iter_next(&it))) {
+ err = btf_permute_remap_type_id(type_id, &transfer_p);
+ if (err)
+ break;
+ }
+ }
+ if (err < 0)
+ goto done;
+ }
+ }
+
+ /* Rewrite optional .BTF.ext references to retained type IDs. */
btf_ext = OPTS_GET(opts, btf_ext, NULL);
if (btf_ext) {
err = btf_ext_visit_type_ids(btf_ext, btf_permute_remap_type_id, &p);
@@ -6363,18 +6916,58 @@ int btf__permute(struct btf *btf, __u32 *id_map, __u32 id_map_cnt,
goto done;
}
- for (nt = new_types, i = 0; i < id_map_cnt - start_offs; i++) {
+ /* Install the compacted parent types and rebuild their index. */
+ for (nt = new_types, i = 0; i < nr_base_types; i++) {
btf->type_offs[i] = nt - new_types;
nt += btf_type_size(btf, nt);
}
-
- free(order_map);
free(btf->types_data);
btf->types_data = new_types;
+ committed = true;
+ btf_hdr_update_type_len(btf, nt - new_types);
+ btf->nr_types = nr_base_types;
+
+ /*
+ * An important goal is to also transfer strings to the transfer
+ * BTF where they do not have duplicates in BTF. This is important
+ * for cases like inline BTF where inline function names could
+ * comprise a significant portion of the string table. The approach
+ * is to first compact the set of strings in the base now transfer
+ * is complete; this will remove unreferenced strings. Then
+ * local references in the transfer BTF have to be moved downwards
+ * based upon that compaction. Finally also compact transfer BTF
+ * references so that we replace duplicate references in split with
+ * base references where present.
+ *
+ * Once all this is done, we end up with transfer-only strings in
+ * transfer split BTF and shared strings in BTF. The result is
+ * a transfer BTF with a lot of transfer-only strings will not
+ * pollute the string table of the non-transfer BTF.
+ */
+ if (transfer_btf) {
+ err = btf_compact_strings(btf, btf_ext);
+ if (err)
+ goto done;
+ err = btf_permute_rebase_split_strs(transfer_btf, old_str_off);
+ if (err)
+ goto done;
+ err = btf_compact_strings(transfer_btf, NULL);
+ if (err)
+ goto done;
+ *transfer_btfp = transfer_btf;
+ }
+
+ /* On success return the final old-to-new type-ID map to the caller. */
+ memcpy(id_map, new_id_map, id_map_cnt * sizeof(*id_map));
+ free(new_id_map);
+ free(order_map);
return 0;
done:
+ free(new_id_map);
free(order_map);
- free(new_types);
+ if (!committed)
+ free(new_types);
+ btf__free(transfer_btf);
return libbpf_err(err);
}
diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h
index 587172c0de089..8ce5697557734 100644
--- a/tools/lib/bpf/btf.h
+++ b/tools/lib/bpf/btf.h
@@ -274,6 +274,20 @@ LIBBPF_API int btf__add_decl_tag(struct btf *btf, const char *value, int ref_typ
LIBBPF_API int btf__add_decl_attr(struct btf *btf, const char *value, int ref_type_id,
int component_idx);
+/* location construction APIs */
+LIBBPF_API int btf__add_loc_param(struct btf *btf, __u32 size, __u32 flags);
+
+LIBBPF_API int btf__add_loc_param_value(struct btf *btf, __u32 value);
+
+LIBBPF_API int btf__add_loc_proto(struct btf *btf);
+
+LIBBPF_API int btf__add_loc_proto_param(struct btf *btf, __u32 id);
+
+LIBBPF_API int btf__add_locsec(struct btf *btf, const char *name);
+
+LIBBPF_API int btf__add_locsec_loc(struct btf *btf, __u32 func, __u32 loc_proto,
+ __u32 offset);
+
struct btf_dedup_opts {
size_t sz;
/* optional .BTF.ext info to dedup along the main BTF info */
@@ -307,9 +321,17 @@ struct btf_permute_opts {
size_t sz;
/* optional .BTF.ext info along the main BTF info */
struct btf_ext *btf_ext;
+ /*
+ * If set, types whose map entry has BTF_PERMUTE_ID_TRANSFER set are
+ * removed from @btf and returned in a new split BTF based on @btf.
+ */
+ struct btf **transfer_btf;
size_t :0;
};
-#define btf_permute_opts__last_field btf_ext
+#define btf_permute_opts__last_field transfer_btf
+
+/* Mark an id_map entry as a type to be moved to .transfer_btf. */
+#define BTF_PERMUTE_ID_TRANSFER (1U << 31)
/**
* @brief **btf__permute()** rearranges BTF types in-place according to a specified ID mapping
@@ -335,6 +357,12 @@ struct btf_permute_opts {
* - Mapping is defined as `id_map[original_id - start_id] = new_id`
* - `start_id` equals `btf__type_cnt(btf__base_btf(btf))`
*
+ * An @id_map entry can be ORed with BTF_PERMUTE_ID_TRANSFER to move that
+ * type to a newly-created split BTF returned through @opts->transfer_btf.
+ * The low bits still specify the type's position in the requested ordering.
+ * The id map is updated to contain the final IDs in the base and returned
+ * split BTF.
+ *
* After permutation, all type references within the BTF data and optional
* BTF extension (if provided via @opts) are updated automatically.
*
@@ -431,6 +459,12 @@ btf_dump__dump_type_data(struct btf_dump *d, __u32 id,
#define BTF_KIND_DECL_TAG 17 /* Decl Tag */
#define BTF_KIND_TYPE_TAG 18 /* Type Tag */
#define BTF_KIND_ENUM64 19 /* Enum for up-to 64bit values */
+#define BTF_KIND_LOC_PARAM 20 /* Parameter at location */
+#define BTF_KIND_LOC_PROTO 21 /* Parameter set at location */
+#define BTF_KIND_LOCSEC 22 /* Section containing location info */
+
+struct btf_loc_param;
+struct btf_loc;
static inline __u16 btf_kind(const struct btf_type *t)
{
@@ -569,6 +603,21 @@ static inline bool btf_is_any_enum(const struct btf_type *t)
return btf_is_enum(t) || btf_is_enum64(t);
}
+static inline bool btf_is_loc_param(const struct btf_type *t)
+{
+ return btf_kind(t) == BTF_KIND_LOC_PARAM;
+}
+
+static inline bool btf_is_loc_proto(const struct btf_type *t)
+{
+ return btf_kind(t) == BTF_KIND_LOC_PROTO;
+}
+
+static inline bool btf_is_locsec(const struct btf_type *t)
+{
+ return btf_kind(t) == BTF_KIND_LOCSEC;
+}
+
static inline bool btf_kind_core_compat(const struct btf_type *t1,
const struct btf_type *t2)
{
@@ -683,6 +732,21 @@ static inline struct btf_decl_tag *btf_decl_tag(const struct btf_type *t)
return (struct btf_decl_tag *)(t + 1);
}
+static inline struct btf_loc_param *btf_loc_param(const struct btf_type *t)
+{
+ return (struct btf_loc_param *)(t + 1);
+}
+
+static inline __u32 *btf_loc_proto_params(const struct btf_type *t)
+{
+ return (__u32 *)(t + 1);
+}
+
+static inline struct btf_loc *btf_locsec_locs(const struct btf_type *t)
+{
+ return (struct btf_loc *)(t + 1);
+}
+
#ifdef __cplusplus
} /* extern "C" */
#endif
diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
index 123c448f20c74..fa995c02a1705 100644
--- a/tools/lib/bpf/btf_dump.c
+++ b/tools/lib/bpf/btf_dump.c
@@ -328,6 +328,9 @@ static int btf_dump_mark_referenced(struct btf_dump *d)
case BTF_KIND_ENUM64:
case BTF_KIND_FWD:
case BTF_KIND_FLOAT:
+ case BTF_KIND_LOC_PARAM:
+ case BTF_KIND_LOC_PROTO:
+ case BTF_KIND_LOCSEC:
break;
case BTF_KIND_VOLATILE:
@@ -609,6 +612,9 @@ static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr)
case BTF_KIND_VAR:
case BTF_KIND_DATASEC:
case BTF_KIND_DECL_TAG:
+ case BTF_KIND_LOC_PARAM:
+ case BTF_KIND_LOC_PROTO:
+ case BTF_KIND_LOCSEC:
d->type_states[id].order_state = ORDERED;
return 0;
@@ -2525,6 +2531,9 @@ static int btf_dump_dump_type_data(struct btf_dump *d,
case BTF_KIND_FUNC:
case BTF_KIND_FUNC_PROTO:
case BTF_KIND_DECL_TAG:
+ case BTF_KIND_LOC_PARAM:
+ case BTF_KIND_LOC_PROTO:
+ case BTF_KIND_LOCSEC:
err = btf_dump_unsupported_data(d, t, id);
break;
case BTF_KIND_INT:
diff --git a/tools/lib/bpf/btf_iter.c b/tools/lib/bpf/btf_iter.c
index 9a6c822c22944..54428e422ec39 100644
--- a/tools/lib/bpf/btf_iter.c
+++ b/tools/lib/bpf/btf_iter.c
@@ -29,6 +29,7 @@ int btf_field_iter_init(struct btf_field_iter *it, struct btf_type *t,
case BTF_KIND_FLOAT:
case BTF_KIND_ENUM:
case BTF_KIND_ENUM64:
+ case BTF_KIND_LOC_PARAM:
it->desc = (struct btf_field_desc) {};
break;
case BTF_KIND_FWD:
@@ -71,6 +72,20 @@ int btf_field_iter_init(struct btf_field_iter *it, struct btf_type *t,
1, {offsetof(struct btf_var_secinfo, type)}
};
break;
+ case BTF_KIND_LOC_PROTO:
+ it->desc = (struct btf_field_desc) {
+ 0, {},
+ sizeof(__u32),
+ 1, {0}};
+ break;
+ case BTF_KIND_LOCSEC:
+ it->desc = (struct btf_field_desc) {
+ 0, {},
+ sizeof(struct btf_loc),
+ 2, {offsetof(struct btf_loc, func),
+ offsetof(struct btf_loc, loc_proto)}};
+ break;
+
default:
return -EINVAL;
}
@@ -94,6 +109,8 @@ int btf_field_iter_init(struct btf_field_iter *it, struct btf_type *t,
case BTF_KIND_DECL_TAG:
case BTF_KIND_TYPE_TAG:
case BTF_KIND_DATASEC:
+ case BTF_KIND_LOC_PARAM:
+ case BTF_KIND_LOC_PROTO:
it->desc = (struct btf_field_desc) {
1, {offsetof(struct btf_type, name_off)}
};
@@ -127,6 +144,11 @@ int btf_field_iter_init(struct btf_field_iter *it, struct btf_type *t,
1, {offsetof(struct btf_param, name_off)}
};
break;
+ case BTF_KIND_LOCSEC:
+ it->desc = (struct btf_field_desc) {
+ 1, {offsetof(struct btf_type, name_off)}
+ };
+ break;
default:
return -EINVAL;
}
diff --git a/tools/lib/bpf/btf_relocate.c b/tools/lib/bpf/btf_relocate.c
index df5fa4bd87d69..e55dd75f6c950 100644
--- a/tools/lib/bpf/btf_relocate.c
+++ b/tools/lib/bpf/btf_relocate.c
@@ -441,7 +441,8 @@ static int btf_relocate_rewrite_strs(struct btf_relocate *r, __u32 i)
/* If successful, output of relocation is updated BTF with base BTF pointing
* at base_btf, and type ids, strings adjusted accordingly.
*/
-int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **id_map)
+int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **id_map,
+ __u32 **str_map)
{
unsigned int nr_types = btf__type_cnt(btf);
const struct btf_header *dist_base_hdr;
@@ -512,6 +513,10 @@ int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **id_map)
*id_map = r.id_map;
r.id_map = NULL;
}
+ if (str_map) {
+ *str_map = r.str_map;
+ r.str_map = NULL;
+ }
err_out:
free(r.id_map);
free(r.str_map);
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index 08ab2ea881fb6..7f51783df129a 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -460,6 +460,12 @@ LIBBPF_1.8.0 {
global:
bpf_program__attach_tracing_multi;
bpf_program__clone;
+ btf__add_loc_param;
+ btf__add_loc_param_value;
+ btf__add_loc_proto;
+ btf__add_loc_proto_param;
+ btf__add_locsec;
+ btf__add_locsec_loc;
btf__find_by_name_kind_own;
btf__new_empty_opts;
} LIBBPF_1.7.0;
diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h
index cb4d962338441..e7a6219374a96 100644
--- a/tools/lib/bpf/libbpf_internal.h
+++ b/tools/lib/bpf/libbpf_internal.h
@@ -249,7 +249,8 @@ const char *btf_kind_str(const struct btf_type *t);
const struct btf_type *skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id);
const struct btf_header *btf_header(const struct btf *btf);
void btf_set_base_btf(struct btf *btf, const struct btf *base_btf);
-int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **id_map);
+int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **id_map,
+ __u32 **str_map);
bool btf_type_is_traceable_func(const struct btf *btf, const struct btf_type *t);
static inline enum btf_func_linkage btf_func_linkage(const struct btf_type *t)
@@ -580,7 +581,7 @@ struct btf_field_desc {
/* member struct size, or zero, if no members */
int m_sz;
/* repeated per-member offsets */
- int m_off_cnt, m_offs[1];
+ int m_off_cnt, m_offs[22];
};
struct btf_field_iter {
diff --git a/tools/testing/selftests/bpf/btf_helpers.c b/tools/testing/selftests/bpf/btf_helpers.c
index 1c1c2c26690a7..6065ee4ba00a6 100644
--- a/tools/testing/selftests/bpf/btf_helpers.c
+++ b/tools/testing/selftests/bpf/btf_helpers.c
@@ -27,11 +27,14 @@ static const char * const btf_kind_str_mapping[] = {
[BTF_KIND_DECL_TAG] = "DECL_TAG",
[BTF_KIND_TYPE_TAG] = "TYPE_TAG",
[BTF_KIND_ENUM64] = "ENUM64",
+ [BTF_KIND_LOC_PARAM] = "LOC_PARAM",
+ [BTF_KIND_LOC_PROTO] = "LOC_PROTO",
+ [BTF_KIND_LOCSEC] = "LOCSEC",
};
static const char *btf_kind_str(__u16 kind)
{
- if (kind > BTF_KIND_ENUM64)
+ if (kind > BTF_KIND_LOCSEC)
return "UNKNOWN";
return btf_kind_str_mapping[kind];
}
@@ -203,6 +206,37 @@ int fprintf_btf_type_raw(FILE *out, const struct btf *btf, __u32 id)
fprintf(out, " type_id=%u component_idx=%d",
t->type, btf_decl_tag(t)->component_idx);
break;
+ case BTF_KIND_LOC_PARAM: {
+ struct btf_loc_param *p = btf_loc_param(t);
+ __u32 *v = (__u32 *)(p + 1);
+
+ fprintf(out, " size=%d flags=0x%x vlen=%u", t->size, p->flags, vlen);
+ for (i = 0; i < vlen; i++, v++) {
+ if (p->flags & BTF_LOC_PARAM_SIGNED)
+ fprintf(out, "\n\tvalue=%d", (__s32)*v);
+ else
+ fprintf(out, "\n\tvalue=%u", *v);
+ }
+ break;
+ }
+ case BTF_KIND_LOC_PROTO: {
+ const __u32 *p = btf_loc_proto_params(t);
+
+ fprintf(out, " vlen=%u", vlen);
+ for (i = 0; i < vlen; i++, p++)
+ fprintf(out, "\n\ttype_id=%u", *p);
+ break;
+ }
+ case BTF_KIND_LOCSEC: {
+ const struct btf_loc *l = btf_locsec_locs(t);
+
+ fprintf(out, " vlen=%u", vlen);
+ for (i = 0; i < vlen; i++, l++) {
+ fprintf(out, "\n\tfunc_type_id=%u loc_proto_type_id=%u offset=%d",
+ l->func, l->loc_proto, l->offset);
+ }
+ break;
+ }
default:
break;
}
diff --git a/tools/testing/selftests/bpf/prog_tests/btf_dedup_split.c b/tools/testing/selftests/bpf/prog_tests/btf_dedup_split.c
index 9d61611515938..34ca20f88afd2 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_dedup_split.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_dedup_split.c
@@ -554,6 +554,113 @@ static void test_split_module(void)
btf__free(vmlinux_btf);
}
+static void test_split_loc(void)
+{
+ struct btf *btf1, *btf2;
+ int err;
+
+ btf1 = btf__new_empty();
+ if (!ASSERT_OK_PTR(btf1, "empty_main_btf"))
+ return;
+
+ btf__set_pointer_size(btf1, 8); /* enforce 64-bit arch */
+
+
+ btf__add_int(btf1, "long", 8, BTF_INT_SIGNED); /* [1] long */
+ btf__add_ptr(btf1, 1); /* [2] ptr to long */
+ btf__add_func_proto(btf1, 1); /* [3] long (*)(long, long *); */
+ btf__add_func_param(btf1, "p1", 1);
+ btf__add_func_param(btf1, "p2", 2);
+ btf__add_func(btf1, "foo", BTF_FUNC_STATIC, 3); /* [4] long foo(long, long *); */
+ btf__add_loc_param(btf1, 8, BTF_LOC_PARAM_CONST);
+ btf__add_loc_param_value(btf1, 3735928559);
+ btf__add_loc_param_value(btf1, 4277009102); /* [5] loc value */
+ btf__add_loc_param(btf1, 8, BTF_LOC_PARAM_REG); /* [6] loc reg 1 */
+ btf__add_loc_param_value(btf1, 1);
+ btf__add_loc_proto(btf1); /* [7] loc proto */
+ btf__add_loc_proto_param(btf1, 5); /* param value */
+ btf__add_loc_proto_param(btf1, 6); /* param reg 1 */
+
+ VALIDATE_RAW_BTF(
+ btf1,
+ "[1] INT 'long' size=8 bits_offset=0 nr_bits=64 encoding=SIGNED",
+ "[2] PTR '(anon)' type_id=1",
+ "[3] FUNC_PROTO '(anon)' ret_type_id=1 vlen=2\n"
+ "\t'p1' type_id=1\n"
+ "\t'p2' type_id=2",
+ "[4] FUNC 'foo' type_id=3 linkage=static",
+ "[5] LOC_PARAM '(anon)' size=8 flags=0x2 vlen=2\n"
+ "\tvalue=3735928559\n"
+ "\tvalue=4277009102",
+ "[6] LOC_PARAM '(anon)' size=8 flags=0x8 vlen=1\n"
+ "\tvalue=1",
+ "[7] LOC_PROTO '(anon)' vlen=2\n"
+ "\ttype_id=5\n"
+ "\ttype_id=6");
+
+ btf2 = btf__new_empty_split(btf1);
+ if (!ASSERT_OK_PTR(btf2, "empty_split_btf"))
+ goto cleanup;
+ btf__add_loc_param(btf2, 8, BTF_LOC_PARAM_REG);
+ btf__add_loc_param_value(btf2, 1); /* [8] loc reg 1 */
+ btf__add_loc_proto(btf2); /* [9] loc proto */
+ btf__add_loc_proto_param(btf2, 5); /* param value */
+ btf__add_loc_proto_param(btf2, 8); /* param reg 1 */
+ btf__add_locsec(btf2, ".locs"); /* [9] locsec ".locs" */
+ btf__add_locsec_loc(btf2, 4, 9, 128);
+
+ VALIDATE_RAW_BTF(
+ btf2,
+ "[1] INT 'long' size=8 bits_offset=0 nr_bits=64 encoding=SIGNED",
+ "[2] PTR '(anon)' type_id=1",
+ "[3] FUNC_PROTO '(anon)' ret_type_id=1 vlen=2\n"
+ "\t'p1' type_id=1\n"
+ "\t'p2' type_id=2",
+ "[4] FUNC 'foo' type_id=3 linkage=static",
+ "[5] LOC_PARAM '(anon)' size=8 flags=0x2 vlen=2\n"
+ "\tvalue=3735928559\n"
+ "\tvalue=4277009102",
+ "[6] LOC_PARAM '(anon)' size=8 flags=0x8 vlen=1\n"
+ "\tvalue=1",
+ "[7] LOC_PROTO '(anon)' vlen=2\n"
+ "\ttype_id=5\n"
+ "\ttype_id=6",
+ "[8] LOC_PARAM '(anon)' size=8 flags=0x8 vlen=1\n"
+ "\tvalue=1",
+ "[9] LOC_PROTO '(anon)' vlen=2\n"
+ "\ttype_id=5\n"
+ "\ttype_id=8",
+ "[10] LOCSEC '.locs' vlen=1\n"
+ "\tfunc_type_id=4 loc_proto_type_id=9 offset=128");
+
+ err = btf__dedup(btf2, NULL);
+ if (!ASSERT_OK(err, "btf_dedup"))
+ goto cleanup;
+
+ VALIDATE_RAW_BTF(
+ btf2,
+ "[1] INT 'long' size=8 bits_offset=0 nr_bits=64 encoding=SIGNED",
+ "[2] PTR '(anon)' type_id=1",
+ "[3] FUNC_PROTO '(anon)' ret_type_id=1 vlen=2\n"
+ "\t'p1' type_id=1\n"
+ "\t'p2' type_id=2",
+ "[4] FUNC 'foo' type_id=3 linkage=static",
+ "[5] LOC_PARAM '(anon)' size=8 flags=0x2 vlen=2\n"
+ "\tvalue=3735928559\n"
+ "\tvalue=4277009102",
+ "[6] LOC_PARAM '(anon)' size=8 flags=0x8 vlen=1\n"
+ "\tvalue=1",
+ "[7] LOC_PROTO '(anon)' vlen=2\n"
+ "\ttype_id=5\n"
+ "\ttype_id=6",
+ "[8] LOCSEC '.locs' vlen=1\n"
+ "\tfunc_type_id=4 loc_proto_type_id=7 offset=128");
+
+cleanup:
+ btf__free(btf2);
+ btf__free(btf1);
+}
+
void test_btf_dedup_split()
{
if (test__start_subtest("split_simple"))
@@ -566,4 +673,6 @@ void test_btf_dedup_split()
test_split_dup_struct_in_cu();
if (test__start_subtest("split_module"))
test_split_module();
+ if (test__start_subtest("split_loc"))
+ test_split_loc();
}
diff --git a/tools/testing/selftests/bpf/prog_tests/btf_distill.c b/tools/testing/selftests/bpf/prog_tests/btf_distill.c
index fb67ae195a738..f433b29ffce92 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_distill.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_distill.c
@@ -671,6 +671,80 @@ static void test_distilled_base_embedded_err(void)
btf__free(btf1);
}
+/* LOC_PARAM, LOC_PROTO should be added to split BTF. */
+static void test_distilled_loc(void)
+{
+ struct btf *btf1 = NULL, *btf2 = NULL, *btf3 = NULL, *btf4 = NULL;
+
+ btf1 = btf__new_empty();
+ if (!ASSERT_OK_PTR(btf1, "empty_main_btf"))
+ return;
+
+ btf__add_int(btf1, "int", 4, BTF_INT_SIGNED); /* [1] int */
+ btf__add_func_proto(btf1, 1); /* [2] int (*)(int); */
+ btf__add_func_param(btf1, "p1", 1);
+ btf__add_func(btf1, "foo", BTF_FUNC_STATIC, 2); /* [3] int foo(int); */
+ btf__add_loc_param(btf1, 4, BTF_LOC_PARAM_SIGNED | BTF_LOC_PARAM_CONST);
+ btf__add_loc_param_value(btf1, -1); /* [4] loc value */
+
+ VALIDATE_RAW_BTF(
+ btf1,
+ "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
+ "[2] FUNC_PROTO '(anon)' ret_type_id=1 vlen=1\n"
+ "\t'p1' type_id=1",
+ "[3] FUNC 'foo' type_id=2 linkage=static",
+ "[4] LOC_PARAM '(anon)' size=4 flags=0x3 vlen=1\n"
+ "\tvalue=-1");
+
+ btf2 = btf__new_empty_split(btf1);
+ if (!ASSERT_OK_PTR(btf2, "empty_split_btf"))
+ goto cleanup;
+
+ btf__add_loc_proto(btf2); /* [5] loc proto */
+ btf__add_loc_proto_param(btf2, 4); /* param value */
+
+ btf__add_locsec(btf2, ".locs"); /* [6] locsec */
+ btf__add_locsec_loc(btf2, 3, 5, 256); /* "foo" offset 256 */
+ VALIDATE_RAW_BTF(
+ btf2,
+ "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
+ "[2] FUNC_PROTO '(anon)' ret_type_id=1 vlen=1\n"
+ "\t'p1' type_id=1",
+ "[3] FUNC 'foo' type_id=2 linkage=static",
+ "[4] LOC_PARAM '(anon)' size=4 flags=0x3 vlen=1\n"
+ "\tvalue=-1",
+ "[5] LOC_PROTO '(anon)' vlen=1\n"
+ "\ttype_id=4",
+ "[6] LOCSEC '.locs' vlen=1\n"
+ "\tfunc_type_id=3 loc_proto_type_id=5 offset=256");
+
+ if (!ASSERT_EQ(0, btf__distill_base(btf2, &btf3, &btf4),
+ "distilled_base") ||
+ !ASSERT_OK_PTR(btf3, "distilled_base") ||
+ !ASSERT_OK_PTR(btf4, "distilled_split") ||
+ !ASSERT_EQ(2, btf__type_cnt(btf3), "distilled_base_type_cnt"))
+ goto cleanup;
+
+ VALIDATE_RAW_BTF(
+ btf4,
+ "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
+ /* remainder is split BTF */
+ "[2] LOC_PROTO '(anon)' vlen=1\n"
+ "\ttype_id=6",
+ "[3] LOCSEC '.locs' vlen=1\n"
+ "\tfunc_type_id=5 loc_proto_type_id=2 offset=256",
+ "[4] FUNC_PROTO '(anon)' ret_type_id=1 vlen=1\n"
+ "\t'p1' type_id=1",
+ "[5] FUNC 'foo' type_id=4 linkage=static",
+ "[6] LOC_PARAM '(anon)' size=4 flags=0x3 vlen=1\n"
+ "\tvalue=-1");
+cleanup:
+ btf__free(btf4);
+ btf__free(btf3);
+ btf__free(btf2);
+ btf__free(btf1);
+}
+
void test_btf_distill(void)
{
if (test__start_subtest("distilled_base"))
@@ -689,4 +763,6 @@ void test_btf_distill(void)
test_distilled_base_vmlinux();
if (test__start_subtest("distilled_endianness"))
test_distilled_endianness();
+ if (test__start_subtest("distilled_loc"))
+ test_distilled_loc();
}
diff --git a/tools/testing/selftests/bpf/prog_tests/btf_field_iter.c b/tools/testing/selftests/bpf/prog_tests/btf_field_iter.c
index 32159d3eb281b..dcb5429d141df 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_field_iter.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_field_iter.c
@@ -31,8 +31,11 @@ struct field_data {
{ .ids = { 11 }, .strs = { "decltag" } },
{ .ids = { 6 }, .strs = { "typetag" } },
{ .ids = {}, .strs = { "e64", "eval1", "eval2", "eval3" } },
- { .ids = { 15, 16 }, .strs = { "datasec1" } }
-
+ { .ids = { 15, 16 }, .strs = { "datasec1" } },
+ { .ids = {}, .strs = { "" } },
+ { .ids = {}, .strs = { "" } },
+ { .ids = { 22, 23 }, .strs = { "" } },
+ { .ids = { 14, 24 }, .strs = { ".loc" } }
};
/* Fabricate BTF with various types and check BTF field iteration finds types,
@@ -88,6 +91,19 @@ void test_btf_field_iter(void)
btf__add_datasec_var_info(btf, 15, 0, 4);
btf__add_datasec_var_info(btf, 16, 4, 8);
+ btf__add_loc_param(btf, 4, BTF_LOC_PARAM_CONST | BTF_LOC_PARAM_SIGNED);
+ /* [22] loc value -1 */
+ btf__add_loc_param_value(btf, -1);
+ btf__add_loc_param(btf, 8, BTF_LOC_PARAM_REG); /* [23] loc reg 1 */
+ btf__add_loc_param_value(btf, 1);
+
+ btf__add_loc_proto(btf); /* [24] loc proto */
+ btf__add_loc_proto_param(btf, 22); /* param value -1, */
+ btf__add_loc_proto_param(btf, 23); /* param reg 1 */
+
+ btf__add_locsec(btf, ".loc"); /* [25] locsec ".loc" */
+ btf__add_locsec_loc(btf, 14, 24, 128); /* "func" */
+
VALIDATE_RAW_BTF(
btf,
"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
@@ -123,7 +139,16 @@ void test_btf_field_iter(void)
"\t'eval3' val=3000",
"[21] DATASEC 'datasec1' size=12 vlen=2\n"
"\ttype_id=15 offset=0 size=4\n"
- "\ttype_id=16 offset=4 size=8");
+ "\ttype_id=16 offset=4 size=8",
+ "[22] LOC_PARAM '(anon)' size=4 flags=0x3 vlen=1\n"
+ "\tvalue=-1",
+ "[23] LOC_PARAM '(anon)' size=8 flags=0x8 vlen=1\n"
+ "\tvalue=1",
+ "[24] LOC_PROTO '(anon)' vlen=2\n"
+ "\ttype_id=22\n"
+ "\ttype_id=23",
+ "[25] LOCSEC '.loc' vlen=1\n"
+ "\tfunc_type_id=14 loc_proto_type_id=24 offset=128");
for (id = 1; id < btf__type_cnt(btf); id++) {
struct btf_type *t = btf_type_by_id(btf, id);
diff --git a/tools/testing/selftests/bpf/prog_tests/btf_inline.c b/tools/testing/selftests/bpf/prog_tests/btf_inline.c
new file mode 100644
index 0000000000000..8e0a85f6c6980
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/btf_inline.c
@@ -0,0 +1,110 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026, Oracle and/or its affiliates. */
+
+#include <test_progs.h>
+#include <bpf/btf.h>
+#include <bpf/libbpf.h>
+
+#define BTF_SYSFS_DIR "/sys/kernel/btf"
+#define BTF_INLINE_SUFFIX ".inline"
+
+/*
+ * For a specific inline site, verify we have the right function,
+ * loc proto and loc param representation and that the offset is
+ * reasonable given the caller where it was inlined.
+ *
+ * Because bpf_testmod is compiled "out-of-tree" we have inline
+ * information in the split BTF directly rather than in btf_testmod.inline.
+ */
+void test_btf_inline(void)
+{
+ struct btf *inline_btf = NULL, *btf = NULL, *vmlinux_btf = NULL;
+ const char *inline_caller = "bpf_testmod_uprobe_write";
+ const char *inline_func = "testmod_register_uprobe";
+ bool skip = false, found_loc = false;
+ const struct btf_loc_param *lp;
+ int locsec_id, func_id, n, i;
+ long caller_addr, base_addr;
+ const struct btf_type *t;
+ struct btf_loc *l;
+ const __u32 *p;
+ int err = 0;
+
+ if (!env.has_testmod) {
+ test__skip();
+ return;
+ }
+
+ base_addr = module_get_base_addr("bpf_testmod");
+ if (!ASSERT_NEQ(base_addr, 0, "base_addr_nonzero"))
+ return;
+
+ load_kallsyms();
+ caller_addr = ksym_get_addr(inline_caller);
+ if (!ASSERT_NEQ(caller_addr, 0, "caller_addr_nonzero"))
+ return;
+
+ if (!ASSERT_GT(caller_addr, base_addr, "caller_addr_gt_base_addr"))
+ return;
+ caller_addr -= base_addr;
+
+ vmlinux_btf = btf__load_vmlinux_btf();
+ if (!ASSERT_OK_PTR(vmlinux_btf, "vmlinux_btf"))
+ return;
+
+ btf = btf__parse_split(BTF_SYSFS_DIR "/bpf_testmod", vmlinux_btf);
+ if (!ASSERT_OK_PTR(btf, "bpf_testmod_btf"))
+ goto out;
+
+ inline_btf = btf__parse_split(BTF_SYSFS_DIR "/bpf_testmod" BTF_INLINE_SUFFIX,
+ btf);
+ err = libbpf_get_error(inline_btf);
+ /* pahole may not have inline BTF feature support. */
+ if (err == -ENOENT) {
+ skip = true;
+ goto out;
+ }
+ locsec_id = btf__find_by_name_kind(inline_btf, ".text", BTF_KIND_LOCSEC);
+ if (locsec_id < 0) {
+ skip = true;
+ goto out;
+ }
+ func_id = btf__find_by_name_kind(inline_btf, inline_func, BTF_KIND_FUNC);
+ if (!ASSERT_GT(func_id, 0, "inline_caller_func"))
+ goto out;
+ t = btf__type_by_id(inline_btf, locsec_id);
+ n = btf_vlen(t);
+ for (i = 0, l = btf_locsec_locs(t); i < n; i++, l++) {
+ if (l->func == func_id) {
+ found_loc = true;
+ break;
+ }
+ }
+ if (!ASSERT_TRUE(found_loc, "found_loc"))
+ goto out;
+ if (!ASSERT_GT(l->loc_proto, 0, "loc_proto_id"))
+ goto out;
+ if (!ASSERT_GT(l->offset, 0, "loc_offset"))
+ goto out;
+ t = btf__type_by_id(inline_btf, l->loc_proto);
+ if (!ASSERT_OK_PTR(t, "loc_proto_ptr"))
+ goto out;
+ if (!ASSERT_EQ(btf_vlen(t), 1, "loc_proto_one_param"))
+ goto out;
+ p = btf_loc_proto_params(t);
+ t = btf__type_by_id(inline_btf, *p);
+ lp = btf_loc_param(t);
+ if (!ASSERT_EQ(lp->flags, BTF_LOC_PARAM_REG, "param_is_reg"))
+ goto out;
+ if (!ASSERT_GT(l->offset, caller_addr, "inline_gt_caller"))
+ goto out;
+ /* simple sanity test to roughly ensure inline site still in function */
+ if (ASSERT_LT(l->offset, caller_addr + 256, "inline_in_caller"))
+ goto out;
+out:
+ btf__free(inline_btf);
+ btf__free(btf);
+ btf__free(vmlinux_btf);
+ if (skip)
+ test__skip();
+}
diff --git a/tools/testing/selftests/bpf/prog_tests/btf_permute.c b/tools/testing/selftests/bpf/prog_tests/btf_permute.c
index 04ade5ad77aca..ef75fd71df96e 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_permute.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_permute.c
@@ -235,10 +235,138 @@ static void test_permute_split(void)
btf__free(base_btf);
}
+/* Ensure selected types can be moved into a split BTF while permuting. */
+static void test_permute_transfer(void)
+{
+ struct btf *btf = NULL, *transfer_btf = NULL;
+ LIBBPF_OPTS(btf_permute_opts, opts);
+ __u32 permute_ids[11];
+ int err, id, foo_name_off = -1, shared_name_off, foo_funcs = 0, shared_funcs = 0;
+
+ btf = btf__new_empty();
+ if (!ASSERT_OK_PTR(btf, "empty_main_btf"))
+ return;
+
+ btf__add_int(btf, "int", 4, BTF_INT_SIGNED); /* [1] int */
+ btf__add_func_proto(btf, 1); /* [2] int (*)(void) */
+ btf__add_func(btf, "foo", BTF_FUNC_STATIC, 2); /* [3] int foo(void) */
+ btf__add_loc_param(btf, 4, BTF_LOC_PARAM_SIGNED); /* [4] location */
+ btf__add_loc_param_value(btf, 0);
+ btf__add_loc_proto(btf); /* [5] location proto */
+ btf__add_loc_proto_param(btf, 4);
+ btf__add_locsec(btf, ".locs"); /* [6] location section */
+ btf__add_locsec_loc(btf, 3, 5, 128);
+ btf__add_locsec_loc(btf, 3, 5, 0);
+ btf__add_int(btf, "long", 8, BTF_INT_SIGNED); /* [7] long */
+ btf__add_func(btf, "foo", BTF_FUNC_STATIC, 2); /* [8] another foo */
+ btf__add_func(btf, "shared", BTF_FUNC_STATIC, 2); /* [9] shared string */
+ btf__add_struct(btf, "shared", 4); /* [10] retains string */
+
+ permute_ids[0] = 0;
+ permute_ids[1] = 1;
+ permute_ids[2] = BTF_PERMUTE_ID_TRANSFER | 2;
+ permute_ids[3] = BTF_PERMUTE_ID_TRANSFER | 3;
+ permute_ids[4] = BTF_PERMUTE_ID_TRANSFER | 5;
+ permute_ids[5] = BTF_PERMUTE_ID_TRANSFER | 6;
+ permute_ids[6] = BTF_PERMUTE_ID_TRANSFER | 7;
+ permute_ids[7] = 4;
+ permute_ids[8] = BTF_PERMUTE_ID_TRANSFER | 8;
+ permute_ids[9] = BTF_PERMUTE_ID_TRANSFER | 9;
+ permute_ids[10] = 10;
+ opts.transfer_btf = &transfer_btf;
+ err = btf__permute(btf, permute_ids, ARRAY_SIZE(permute_ids), &opts);
+ if (!ASSERT_OK(err, "btf__permute_transfer") ||
+ !ASSERT_OK_PTR(transfer_btf, "transfer_btf"))
+ goto cleanup;
+
+ ASSERT_EQ(4, btf__type_cnt(btf), "base_type_cnt");
+ ASSERT_EQ(-ENOENT, btf__find_str(btf, "foo"), "func_name_not_in_base");
+ shared_name_off = btf__find_str(btf, "shared");
+ if (!ASSERT_GE(shared_name_off, 0, "shared_name_in_base"))
+ goto cleanup;
+ for (id = btf__type_cnt(btf); id < btf__type_cnt(transfer_btf); id++) {
+ const struct btf_type *t = btf__type_by_id(transfer_btf, id);
+
+ if (!btf_is_func(t))
+ continue;
+ if (!strcmp(btf__name_by_offset(transfer_btf, t->name_off), "foo")) {
+ foo_funcs++;
+ if (foo_name_off < 0)
+ foo_name_off = t->name_off;
+ else if (!ASSERT_EQ(foo_name_off, t->name_off, "local_foo_dedup"))
+ goto cleanup;
+ } else if (!strcmp(btf__name_by_offset(transfer_btf, t->name_off), "shared")) {
+ shared_funcs++;
+ if (!ASSERT_EQ(shared_name_off, t->name_off, "shared_name_reuses_base"))
+ goto cleanup;
+ }
+ }
+ if (!ASSERT_GE(foo_name_off, 0, "local_foo_name"))
+ goto cleanup;
+ if (!ASSERT_EQ(2, foo_funcs, "local_foo_count") ||
+ !ASSERT_EQ(1, shared_funcs, "shared_func_count"))
+ goto cleanup;
+ VALIDATE_RAW_BTF(
+ transfer_btf,
+ "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
+ "[2] INT 'long' size=8 bits_offset=0 nr_bits=64 encoding=SIGNED",
+ "[3] STRUCT 'shared' size=4 vlen=0",
+ "[4] FUNC_PROTO '(anon)' ret_type_id=1 vlen=0",
+ "[5] FUNC 'foo' type_id=4 linkage=static",
+ "[6] LOC_PARAM '(anon)' size=4 flags=0x1 vlen=1\n"
+ "\tvalue=0",
+ "[7] LOC_PROTO '(anon)' vlen=1\n"
+ "\ttype_id=6",
+ "[8] LOCSEC '.locs' vlen=2\n"
+ "\tfunc_type_id=5 loc_proto_type_id=7 offset=128\n"
+ "\tfunc_type_id=5 loc_proto_type_id=7 offset=0",
+ "[9] FUNC 'foo' type_id=4 linkage=static",
+ "[10] FUNC 'shared' type_id=4 linkage=static");
+cleanup:
+ btf__free(transfer_btf);
+ btf__free(btf);
+}
+
+/* Permuting BTF with a layout section must keep section offsets in sync. */
+static void test_permute_layout(void)
+{
+ LIBBPF_OPTS(btf_new_opts, opts, .add_layout = true);
+ LIBBPF_OPTS(btf_permute_opts, permute_opts);
+ struct btf *btf, *parsed, *transfer_btf = NULL;
+ const void *raw;
+ __u32 raw_sz;
+ __u32 permute_ids[] = { 0, 1, BTF_PERMUTE_ID_TRANSFER | 2 };
+ int err;
+
+ btf = btf__new_empty_opts(&opts);
+ if (!ASSERT_OK_PTR(btf, "empty_layout_btf"))
+ return;
+
+ btf__add_int(btf, "int", 4, BTF_INT_SIGNED);
+ btf__add_ptr(btf, 1);
+ permute_opts.transfer_btf = &transfer_btf;
+ err = btf__permute(btf, permute_ids, ARRAY_SIZE(permute_ids), &permute_opts);
+ if (!ASSERT_OK(err, "btf__permute_layout"))
+ goto cleanup;
+
+ raw = btf__raw_data(btf, &raw_sz);
+ parsed = btf__new(raw, raw_sz);
+ if (!ASSERT_OK_PTR(parsed, "parse_permuted_layout"))
+ goto cleanup;
+ btf__free(parsed);
+cleanup:
+ btf__free(transfer_btf);
+ btf__free(btf);
+}
+
void test_btf_permute(void)
{
if (test__start_subtest("permute_base"))
test_permute_base();
if (test__start_subtest("permute_split"))
test_permute_split();
+ if (test__start_subtest("permute_transfer"))
+ test_permute_transfer();
+ if (test__start_subtest("permute_layout"))
+ test_permute_layout();
}
diff --git a/tools/testing/selftests/bpf/prog_tests/btf_sysfs.c b/tools/testing/selftests/bpf/prog_tests/btf_sysfs.c
index 3923e64c4c1d0..97eccfd7c1344 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_sysfs.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_sysfs.c
@@ -3,11 +3,16 @@
#include <test_progs.h>
#include <bpf/btf.h>
+#include <dirent.h>
+#include <limits.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
+#define BTF_SYSFS_DIR "/sys/kernel/btf"
+#define BTF_INLINE_SUFFIX ".inline"
+
static void test_btf_mmap_sysfs(const char *path, struct btf *base)
{
struct stat st;
@@ -75,7 +80,76 @@ static void test_btf_mmap_sysfs(const char *path, struct btf *base)
close(fd);
}
+static void test_btf_inline_sysfs_all(void)
+{
+ struct btf *vmlinux_btf;
+ struct dirent *dentry;
+ DIR *dir;
+ int err = 0;
+
+ dir = opendir(BTF_SYSFS_DIR);
+ if (!ASSERT_OK_PTR(dir, "open_btf_sysfs"))
+ return;
+
+ vmlinux_btf = btf__parse(BTF_SYSFS_DIR "/vmlinux", NULL);
+ if (!ASSERT_OK_PTR(vmlinux_btf, "parse_vmlinux_btf")) {
+ closedir(dir);
+ return;
+ }
+
+ while ((dentry = readdir(dir)) != NULL) {
+ struct btf *base_btf = NULL, *module_btf = NULL, *inline_btf = NULL;
+ char btf_path[PATH_MAX], inline_path[PATH_MAX];
+ struct stat st;
+
+ /* Skip ".", ".." and "foo.inline" */
+ if (strstr(dentry->d_name, "."))
+ continue;
+
+ if (strcmp(dentry->d_name, "vmlinux") == 0)
+ base_btf = vmlinux_btf;
+
+ if (snprintf(btf_path, sizeof(btf_path), "%s/%s",
+ BTF_SYSFS_DIR, dentry->d_name) >= sizeof(btf_path) ||
+ snprintf(inline_path, sizeof(inline_path), "%s/%s%s",
+ BTF_SYSFS_DIR, dentry->d_name, BTF_INLINE_SUFFIX) >=
+ sizeof(inline_path)) {
+ ASSERT_FAIL("BTF sysfs path is too long\n");
+ break;
+ }
+
+ if (!base_btf) {
+ module_btf = btf__parse_split(btf_path, vmlinux_btf);
+ err = libbpf_get_error(module_btf);
+ if (err) {
+ /* A module can be unloaded while its sysfs entry is iterated. */
+ if (err == -ENOENT)
+ continue;
+ ASSERT_OK(err, "parse_module_btf");
+ continue;
+ }
+ base_btf = module_btf;
+ }
+ if (stat(inline_path, &st)) {
+ err = errno;
+ if (err == ENOENT)
+ continue;
+ ASSERT_OK(err, "stat_inline_btf");
+ }
+ inline_btf = btf__parse_split(inline_path, base_btf);
+ err = libbpf_get_error(inline_btf);
+ if (!err)
+ btf__free(inline_btf);
+ ASSERT_OK(err, "parse_inline_btf");
+ btf__free(module_btf);
+ }
+ closedir(dir);
+
+ btf__free(vmlinux_btf);
+}
+
void test_btf_sysfs(void)
{
test_btf_mmap_sysfs("/sys/kernel/btf/vmlinux", NULL);
+ test_btf_inline_sysfs_all();
}
diff --git a/tools/testing/selftests/bpf/test_btf.h b/tools/testing/selftests/bpf/test_btf.h
index e7bc781083745..b99c04b4d26bb 100644
--- a/tools/testing/selftests/bpf/test_btf.h
+++ b/tools/testing/selftests/bpf/test_btf.h
@@ -84,4 +84,20 @@
#define BTF_TYPE_TAG_ENC(value, type) \
BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_TYPE_TAG, 0, 0), type)
+#define BTF_LOC_PARAM_ENC(nvals, sz, flags) \
+ BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_LOC_PARAM, 0, nvals), sz), (flags)
+
+#define BTF_LOC_PARAM_VAL_ENCODE(val) (val)
+
+#define BTF_LOC_PROTO_ENC(nargs) \
+ BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_LOC_PROTO, 0, nargs), 0)
+
+#define BTF_LOC_PROTO_PARAM_ENCODE(param) (param)
+
+#define BTF_LOCSEC_ENC(name, nlocs) \
+ BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_LOCSEC, 0, nlocs), 0)
+
+#define BTF_LOCSEC_LOC_ENCODE(func, loc_proto, offset) \
+ (func), (loc_proto), (offset)
+
#endif /* _TEST_BTF_H */
diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
index 2380b6cbdeadc..09d910d5e3129 100644
--- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
+++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
@@ -750,7 +750,7 @@ static struct testmod_uprobe uprobe = {
.consumer.ret_handler = uprobe_ret_handler,
};
-static int testmod_register_uprobe(loff_t offset)
+static __always_inline int testmod_register_uprobe(loff_t offset)
{
int err = -EBUSY;
diff --git a/tools/testing/selftests/bpf/trace_helpers.c b/tools/testing/selftests/bpf/trace_helpers.c
index 679008b310d9a..bf4cb720e7b81 100644
--- a/tools/testing/selftests/bpf/trace_helpers.c
+++ b/tools/testing/selftests/bpf/trace_helpers.c
@@ -244,6 +244,26 @@ int kallsyms_find(const char *sym, unsigned long long *addr)
return err;
}
+long module_get_base_addr(const char *module)
+{
+ unsigned long long addr, base_addr = 0;
+ char name[500];
+ FILE *f;
+
+ f = fopen("/proc/modules", "r");
+ if (!f)
+ return 0;
+
+ while (fscanf(f, "%s %*s %*s %*s %*s 0x%llx", name, &addr) > 0) {
+ if (strcmp(module, name) == 0) {
+ base_addr = addr;
+ break;
+ }
+ }
+ fclose(f);
+ return base_addr;
+}
+
#ifdef PROCMAP_QUERY
int env_verbosity __weak = 0;
diff --git a/tools/testing/selftests/bpf/trace_helpers.h b/tools/testing/selftests/bpf/trace_helpers.h
index 01c8ecc456270..473508306f79f 100644
--- a/tools/testing/selftests/bpf/trace_helpers.h
+++ b/tools/testing/selftests/bpf/trace_helpers.h
@@ -38,6 +38,7 @@ typedef int (*ksym_search_cmp_t)(const void *p1, const struct ksym *p2);
int load_kallsyms(void);
struct ksym *ksym_search(long key);
long ksym_get_addr(const char *name);
+long module_get_base_addr(const char *module);
struct ksyms *load_kallsyms_local(void);
struct ksym *ksym_search_local(struct ksyms *ksyms, long key);
|
| 3/2 | 2026/09/02 07:09 | llm | patch-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 55691 output: 31 thoughts: 1002 Thoughts: **Analyzing the BTF Patch for Fuzzing Opportunities**
Okay, let's break down this patch. My immediate goal is to understand the changes well enough to assess their fuzzing potential – what configurations we'll need, and what symbols are likely to be interesting targets.
First, the patch introduces support for `.BTF.inline` and three new BTF kinds: `BTF_KIND_LOC_PARAM`, `BTF_KIND_LOC_PROTO`, and `BTF_KIND_LOCSEC`. This suggests expanded functionality related to location information within BTF data, specifically for parameters, prototypes, and sections, which could be useful.
Looking at the kernel changes, I see:
* **Header Files:** The `btf.h` file now defines the new enum values and corresponding structs (`btf_loc_param`, `btf_loc_proto`, and `btf_loc`).
* **BTF Implementation:** The core `btf.c` file has been significantly updated. We have the addition of kind strings, the checks for our new types, and most importantly, validation logic via `*_check_meta()`, logging functions and `*_ops`. It seems this is where the new kind's logic is implemented.
* I'll need to pay close attention to `btf_loc_param_check_meta`, `btf_loc_proto_check_meta`, and `btf_locsec_check_meta`, as those are likely where any subtle bugs or vulnerabilities would be hiding. I will also make sure to check all of the functions for each kind's `.c` file and ensure to find the resolve function.
* There's also modification of core BTF functions like `btf_set_base_btf`, `btf_resolve_valid`, and the addition of `btf_parse_split`, `btf_rebase_inline`, and updates to `btf_parse_module`. These indicate a refactoring to handle the new inline BTF data, which is intriguing from a fuzzing perspective, as parsing is often fertile ground for vulnerabilities.
* **Sysfs Integration:** The sysfs handling looks important, as we're now exposing BTF data via `/sys/kernel/btf/`. Specifically, I note the addition of `sysfs_btf_add`, `sysfs_btf_kernfs_open`, `sysfs_btf_kernfs_read`, and `sysfs_btf_bin_attr_load`, plus `btf_module_notify`, which is responsible for loading `.BTF.inline` from modules and managing `btf_vmlinux_inline`. This sysfs interaction is *key*, as it potentially provides an interface from userspace.
* **Module Support:** The `btf_vmlinux_inline` is a new module/builtin that provides `vmlinux.inline`, which will definitely be a fuzzing target. The module code now extracts `.BTF.inline` data, which is an important place to look for subtle interactions.
* **Kconfig:** `DEBUG_INFO_BTF_INLINE` is another sign that the features are in development. This could provide additional checks and options.
* **Tools:** Lastly, the tools and `libbpf` changes hint at broader tooling support.
Now, the crucial question: can we reach the BTF type loading and validation logic from userspace or module loading? I need to figure out if we can influence the new code via either of those two ways.
I know BTF loading from userspace goes through `sys_bpf(BPF_BTF_LOAD, ...)` and then through `btf_new_fd()` -> `btf_parse()` -> `btf_check_all_metas()` -> `btf_check_meta()` -> `kind_ops[kind]->check_meta()`. The resolution functions are also very important here as they're the last thing that is processed. Can we get userspace to load BTF with `BTF_KIND_LOC_PARAM`, `BTF_KIND_LOC_PROTO`, and `BTF_KIND_LOCSEC`? The answer to that question will determine where to focus my fuzzing efforts.
I'll check `btf.c` itself now to see if the `btf_load` path (via the BPF syscall) allows these new kinds. I'll use `grepper` or `read-file` for that.
|
| 4/2 | 2026/09/02 07:09 | tool | grepper |
0mArgs:{
"Expression": "btf_check_meta",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/btf.c=5596=static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = {\n--\nkernel/bpf/btf.c-5620-\nkernel/bpf/btf.c:5621:static s32 btf_check_meta(struct btf_verifier_env *env,\nkernel/bpf/btf.c-5622-\t\t\t const struct btf_type *t,\n--\nkernel/bpf/btf.c=5657=static int btf_check_all_metas(struct btf_verifier_env *env)\n--\nkernel/bpf/btf.c-5671-\nkernel/bpf/btf.c:5672:\t\tmeta_size = btf_check_meta(env, t, end - cur);\nkernel/bpf/btf.c-5673-\t\tif (meta_size \u003c 0)\n"
} |
| 5/2 | 2026/09/02 07:09 | llm | patch-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 57955 output: 39 thoughts: 86 Thoughts: **Diving into BTF Loading from Userspace** Okay, so I'm focusing on how BTF gets loaded from userspace, specifically through `BPF_BTF_LOAD`. That `bpf()` system call with `BPF_BTF_LOAD` looks like the starting point. My mental map tells me this then calls either `btf_load()` or, more likely, `btf_new_fd()`. Since I see a file descriptor involved, I'm leaning toward the latter. Now, I need to understand what's actually *happening*. I'm going to jump into `kernel/bpf/btf.c` and take a close look at `btf_new_fd()` and then, since that's probably a wrapper, drill down into `btf_parse` to really get to the heart of the matter and figure out the parsing logic. This will allow me to understand how userspace BTF data gets interpreted and validated by the kernel. |
| 6/2 | 2026/09/02 07:09 | tool | grepper |
0mArgs:{
"Expression": "btf_new_fd|BPF_BTF_LOAD",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/btf.c=8721=const struct file_operations btf_fops = {\n--\nkernel/bpf/btf.c-8727-\nkernel/bpf/btf.c:8728:static int __btf_new_fd(struct btf *btf)\nkernel/bpf/btf.c-8729-{\n--\nkernel/bpf/btf.c-8732-\nkernel/bpf/btf.c:8733:int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_attr *attr_log)\nkernel/bpf/btf.c-8734-{\n--\nkernel/bpf/btf.c-8753-\nkernel/bpf/btf.c:8754:\tret = __btf_new_fd(btf);\nkernel/bpf/btf.c-8755-\tif (ret \u003c 0)\n--\nkernel/bpf/btf.c=8833=int btf_get_fd_by_id(u32 id)\n--\nkernel/bpf/btf.c-8846-\nkernel/bpf/btf.c:8847:\tfd = __btf_new_fd(btf);\nkernel/bpf/btf.c-8848-\tif (fd \u003c 0)\n--\nkernel/bpf/btf.c=9266=BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int, flags)\n--\nkernel/bpf/btf.c-9279-\tif (ret \u003e 0 \u0026\u0026 btf_is_module(btf)) {\nkernel/bpf/btf.c:9280:\t\tbtf_obj_fd = __btf_new_fd(btf);\nkernel/bpf/btf.c-9281-\t\tif (btf_obj_fd \u003c 0) {\n--\nkernel/bpf/syscall.c=5535=static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,\n--\nkernel/bpf/syscall.c-5561-\nkernel/bpf/syscall.c:5562:#define BPF_BTF_LOAD_LAST_FIELD btf_token_fd\nkernel/bpf/syscall.c-5563-\nkernel/bpf/syscall.c=5564=static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_attr *attr_log)\n--\nkernel/bpf/syscall.c-5567-\nkernel/bpf/syscall.c:5568:\tif (CHECK_ATTR(BPF_BTF_LOAD))\nkernel/bpf/syscall.c-5569-\t\treturn -EINVAL;\n--\nkernel/bpf/syscall.c-5577-\t\t\treturn PTR_ERR(token);\nkernel/bpf/syscall.c:5578:\t\tif (!bpf_token_allow_cmd(token, BPF_BTF_LOAD)) {\nkernel/bpf/syscall.c-5579-\t\t\tbpf_token_put(token);\n--\nkernel/bpf/syscall.c-5590-\nkernel/bpf/syscall.c:5591:\treturn btf_new_fd(attr, uattr, attr_log);\nkernel/bpf/syscall.c-5592-}\n--\nkernel/bpf/syscall.c=6303=static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size,\n--\nkernel/bpf/syscall.c-6410-\t\tbreak;\nkernel/bpf/syscall.c:6411:\tcase BPF_BTF_LOAD:\nkernel/bpf/syscall.c-6412-\t\tif (size \u003e= offsetofend(union bpf_attr, btf_log_true_size))\n--\nkernel/bpf/syscall.c=6500=BPF_CALL_3(bpf_sys_bpf, int, cmd, union bpf_attr *, attr, u32, attr_size)\n--\nkernel/bpf/syscall.c-6508-\tcase BPF_PROG_LOAD:\nkernel/bpf/syscall.c:6509:\tcase BPF_BTF_LOAD:\nkernel/bpf/syscall.c-6510-\tcase BPF_LINK_CREATE:\n"
} |
| 7/2 | 2026/09/02 07:09 | llm | patch-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 59465 output: 28 thoughts: 654 Thoughts: **My Assessment of the New BTF Code** Okay, let's break this down. It looks like we're diving deep into the core of the BPF/BTF subsystem, specifically the handling of new BTF kinds, module loading, and sysfs interactions. My initial thought process is triggered by `btf_new_fd`, which, as I recall, kicks off the whole BTF parsing process. It's clear that the flow goes through `btf_parse`, which in turn, calls `btf_check_all_metas` and `btf_check_all_types`. These functions are crucial for validating the BTF data. The calls to `kind_ops[...]->check_meta` and `kind_ops[...]->resolve` are the real meat of the validation logic for each BTF type, and this is where potential vulnerabilities often hide. I'm noting that module loading is also involved. We have `btf_parse_module` and `btf_module_notify`, which deal with `.BTF.inline` sections. And we have this whole sysfs interaction via `/sys/kernel/btf/` which leads to functions like `sysfs_btf_kernfs_read`, `sysfs_btf_kernfs_open`, and `sysfs_btf_add`. These functions are all linked together. Now, looking at the specific new code added in `btf.c` – `btf_loc_param_check_meta`, `btf_loc_proto_check_meta`, `btf_loc_proto_resolve`, `btf_locsec_check_meta`, `btf_locsec_resolve`, `btf_rebase_inline`, `btf_parse_split`, `btf_parse_module`, `sysfs_btf_add`, `sysfs_btf_bin_attr_load`, `sysfs_btf_kernfs_open`, `sysfs_btf_kernfs_read`, and `btf_module_notify` – it's obvious we're dealing with new type validators and resolvers. Specifically, we are looking at new "local" types that need resolving. That means the new code is providing verification and resolution logic for these additions. My spidey-sense is tingling. This is *prime* fuzzing territory. We're dealing with user input via the `BPF_BTF_LOAD` syscall, and also untrusted kernel modules. The potential for bugs in these new type validators and resolvers is high. I'm thinking out-of-bounds reads, infinite loops within the resolve recursion, integer overflows – the usual suspects. Before I jump in, I need to check the Kconfig requirements. I'll start with `lib/Kconfig.debug`, then `kernel/bpf/Makefile`, and any other relevant Kconfig files to see if anything needs to be enabled for my fuzzing setup. I need to make sure I have all the proper configurations and dependencies. |
| 8/2 | 2026/09/02 07:10 | tool | grepper |
0mArgs:{
"Expression": "DEBUG_INFO_BTF",
"PathPrefix": "lib/"
}Results: {
"Output": "lib/Kconfig.debug=378=config DEBUG_INFO_SPLIT\n--\nlib/Kconfig.debug-397-\nlib/Kconfig.debug:398:config DEBUG_INFO_BTF\nlib/Kconfig.debug-399-\tbool \"Generate BTF type information\"\n--\nlib/Kconfig.debug=419=config PAHOLE_HAS_LANG_EXCLUDE\n--\nlib/Kconfig.debug-425-\t otherwise it would emit malformed kernel and module binaries when\nlib/Kconfig.debug:426:\t using DEBUG_INFO_BTF_MODULES.\nlib/Kconfig.debug-427-\nlib/Kconfig.debug=428=config PAHOLE_HAS_INLINE\n--\nlib/Kconfig.debug-433-\nlib/Kconfig.debug:434:config DEBUG_INFO_BTF_MODULES\nlib/Kconfig.debug-435-\tbool \"Generate BTF type information for kernel modules\"\nlib/Kconfig.debug-436-\tdefault y\nlib/Kconfig.debug:437:\tdepends on DEBUG_INFO_BTF \u0026\u0026 MODULES\nlib/Kconfig.debug-438-\thelp\n--\nlib/Kconfig.debug-440-\nlib/Kconfig.debug:441:config DEBUG_INFO_BTF_INLINE\nlib/Kconfig.debug-442-\ttristate \"Provide information about inline sites in BTF\"\nlib/Kconfig.debug-443-\tdefault n\nlib/Kconfig.debug:444:\tdepends on DEBUG_INFO_BTF \u0026\u0026 PAHOLE_HAS_INLINE \u0026\u0026 SYSFS\nlib/Kconfig.debug:445:\tselect DEBUG_INFO_BTF_MODULES if MODULES\nlib/Kconfig.debug-446-\thelp\n--\nlib/Kconfig.debug=453=config MODULE_ALLOW_BTF_MISMATCH\nlib/Kconfig.debug-454-\tbool \"Allow loading modules with non-matching BTF type info\"\nlib/Kconfig.debug:455:\tdepends on DEBUG_INFO_BTF_MODULES\nlib/Kconfig.debug-456-\thelp\n"
} |
| 9/2 | 2026/09/02 07:10 | llm | patch-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 61347 output: 34 thoughts: 21 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Investigating `DEBUG_INFO_BTF` in `lib/Kconfig.debug`** Alright, time to dive in and refresh my understanding of `DEBUG_INFO_BTF`. This usually means a specific use case or bug has resurfaced, or perhaps I'm looking at potential performance implications. My first port of call, as always, is the `lib/Kconfig.debug` file. It's the central source of truth for understanding the configuration options related to debugging information, specifically the Build Time Type Information (BTF) generation. I need to quickly recall: What dependencies does `DEBUG_INFO_BTF` have? What other debugging options might conflict or interact with it? Am I looking at it from a build size perspective or runtime performance? From my experience with the system, I need to know which kernel subsystems will be impacted by the selection of this configuration option. Knowing this will point me to the next best area to examine in the code. |
| 10/2 | 2026/09/02 07:10 | tool | read-file |
0mArgs:{
"File": "lib/Kconfig.debug",
"FirstLine": 395,
"LineCount": 45
}Results: {
"Contents": " 395:\t\t to know about the .dwo files and include them.\n 396:\t\t Incompatible with older versions of ccache.\n 397:\t\n 398:\tconfig DEBUG_INFO_BTF\n 399:\t\tbool \"Generate BTF type information\"\n 400:\t\tdepends on !DEBUG_INFO_SPLIT \u0026\u0026 !DEBUG_INFO_REDUCED\n 401:\t\tdepends on !GCC_PLUGIN_RANDSTRUCT || COMPILE_TEST\n 402:\t\tdepends on BPF_SYSCALL\n 403:\t\tdepends on PAHOLE_VERSION \u003e= 122\n 404:\t\t# pahole uses elfutils, which does not have support for Hexagon relocations\n 405:\t\tdepends on !HEXAGON\n 406:\t\thelp\n 407:\t\t Generate deduplicated BTF type information from DWARF debug info.\n 408:\t\t Turning this on requires pahole v1.22 or later, which will convert\n 409:\t\t DWARF type info into equivalent deduplicated BTF type info.\n 410:\t\n 411:\tconfig PAHOLE_HAS_BTF_TAG\n 412:\t\tdef_bool PAHOLE_VERSION \u003e= 123\n 413:\t\tdepends on CC_IS_CLANG\n 414:\t\thelp\n 415:\t\t Decide whether pahole emits btf_tag attributes (btf_type_tag and\n 416:\t\t btf_decl_tag) or not. Currently only clang compiler implements\n 417:\t\t these attributes, so make the config depend on CC_IS_CLANG.\n 418:\t\n 419:\tconfig PAHOLE_HAS_LANG_EXCLUDE\n 420:\t\tdef_bool PAHOLE_VERSION \u003e= 124\n 421:\t\thelp\n 422:\t\t Support for the --lang_exclude flag which makes pahole exclude\n 423:\t\t compilation units from the supplied language. Used in Kbuild to\n 424:\t\t omit Rust CUs which are not supported in version 1.24 of pahole,\n 425:\t\t otherwise it would emit malformed kernel and module binaries when\n 426:\t\t using DEBUG_INFO_BTF_MODULES.\n 427:\t\n 428:\tconfig PAHOLE_HAS_INLINE\n 429:\t\tdef_bool PAHOLE_VERSION \u003e= 131\n 430:\t\thelp\n 431:\t\t Support for the \"inline\" BTF feature is available. It encodes\n 432:\t\t information about inline sites and how to retrieve their parameters.\n 433:\t\n 434:\tconfig DEBUG_INFO_BTF_MODULES\n 435:\t\tbool \"Generate BTF type information for kernel modules\"\n 436:\t\tdefault y\n 437:\t\tdepends on DEBUG_INFO_BTF \u0026\u0026 MODULES\n 438:\t\thelp\n 439:\t\t Generate compact split BTF type information for kernel modules.\n"
} |
| 11/2 | 2026/09/02 07:10 | llm | patch-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 62387 output: 29 thoughts: 157 Thoughts: **My Analysis of `DEBUG_INFO_BTF_INLINE` Configuration** Okay, let's dive into how `DEBUG_INFO_BTF_INLINE` is being handled. I need to understand its impact within the build process and source code. First, the `kernel/bpf/Makefile` is where I'll begin. It looks like `btf_vmlinux_inline.o` gets included in the `obj-` list based on the value of `CONFIG_DEBUG_INFO_BTF_INLINE`. That makes perfect sense. Now, let's see how that flag is actually *used* in the code. Checking `kernel/bpf/btf.c`, I see `#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)` used to gate certain blocks of code. Interesting. I'm also seeing a nested `#ifdef CONFIG_DEBUG_INFO_BTF_MODULES`, which is something to keep in mind. I need to mentally connect how these flags work together. Finally, since we're dealing with BTF, I'll need to confirm that the base BTF parser itself is built when `CONFIG_BPF_SYSCALL` is enabled; it looks like the BTF parser and related functionality are built under this umbrella. I'll take a quick glance at `kernel/bpf/Makefile` again to verify. Got it. |
| 12/2 | 2026/09/02 07:10 | tool | grepper |
0mArgs:{
"Expression": "btf\\.o",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/Makefile=13=obj-$(CONFIG_BPF_SYSCALL) += bpf_local_storage.o bpf_task_storage.o\nkernel/bpf/Makefile:14:obj-$(CONFIG_BPF_SYSCALL) += fixups.o cfg.o states.o backtrack.o check_btf.o\nkernel/bpf/Makefile-15-obj-${CONFIG_BPF_LSM}\t += bpf_inode_storage.o\n--\nkernel/bpf/Makefile=17=obj-$(CONFIG_BPF_JIT) += trampoline.o\nkernel/bpf/Makefile:18:obj-$(CONFIG_BPF_SYSCALL) += btf.o memalloc.o rqspinlock.o stream.o\nkernel/bpf/Makefile-19-ifeq ($(CONFIG_MMU)$(CONFIG_64BIT),yy)\n--\nkernel/bpf/Makefile=43=ifeq ($(CONFIG_SYSFS),y)\nkernel/bpf/Makefile:44:obj-$(CONFIG_DEBUG_INFO_BTF) += sysfs_btf.o\nkernel/bpf/Makefile-45-obj-$(CONFIG_DEBUG_INFO_BTF_INLINE) += btf_vmlinux_inline.o\n"
} |
| 13/2 | 2026/09/02 07:10 | llm | patch-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 63186 output: 32 thoughts: 613 Thoughts: **My Initial Investigation into BTF and Kernel Modifications** Alright, let's dive into this. First, I need to make sure my assumptions are correct, so I'll quickly verify all the relevant CONFIG options are enabled: `BPF_SYSCALL`, `DEBUG_INFO_BTF`, `DEBUG_INFO_BTF_MODULES`, `DEBUG_INFO_BTF_INLINE`, and `SYSFS`. This forms the foundation for my analysis. Next, I'll identify the key areas of code that have likely seen modifications related to this BTF functionality. I'm focusing on function definitions, so I'm checking `kernel/bpf/btf.c`, `kernel/bpf/btf_vmlinux_inline.c`, and `kernel/module/main.c`. Inside `btf.c`, I've got a comprehensive list of potentially relevant functions, mostly static helper functions, including those dealing with meta data checking, logging, and resolving locations, like `btf_loc_param_check_meta`, `btf_loc_param_log`, `btf_loc_proto_check_meta`, `btf_loc_proto_log`, `btf_loc_proto_resolve`, `btf_locsec_check_meta`, `btf_locsec_log`, `btf_locsec_resolve`, `btf_parse_split`, `btf_rebase_inline`, `btf_parse_module`, a handful of sysfs functions such as `sysfs_btf_bin_attr_load`, `sysfs_btf_kernfs_open`, `sysfs_btf_kernfs_read`, `sysfs_btf_update`, `sysfs_btf_remove`, `btf_module_notify`, and finally, the key entrypoints: `btf_new_fd` and `bpf_btf_load`. Then, I'll pay closer attention to the non-static functions, which are the ones that are exported or potentially callable from outside the module. I see `sysfs_btf_add` is non-static (defined in `include/linux/btf.h`), which is interesting, then, `btf_new_fd` is the entrypoint for `BPF_BTF_LOAD`, so it must be a core function, `btf_module_notify` is a notifier callback and `btf_parse_module` / `btf_parse_split`, so I'll want to review these functions carefully. Now, I'm trying to get a handle on the overall flow. Are `btf_new_fd` and `sysfs_btf_add` the primary entrypoints for BTF loading and resolution? I need to confirm that. I'll take a closer look at `btf_new_fd` within `btf.c` to see how it fits in. |
| 14/2 | 2026/09/02 07:11 | tool | grepper |
0mArgs:{
"Expression": "int btf_new_fd",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/btf.c=8728=static int __btf_new_fd(struct btf *btf)\n--\nkernel/bpf/btf.c-8732-\nkernel/bpf/btf.c:8733:int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_attr *attr_log)\nkernel/bpf/btf.c-8734-{\n"
} |
| 15/2 | 2026/09/02 07:11 | llm | patch-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 64600 output: 32 thoughts: 24 Thoughts: Here's my take on the thought process, as if I were the one thinking it: **Diving into the BPF BTF Loading Process** Okay, so I need to understand what's happening with BPF and BTF loading. My initial thought is to go straight to the source, to the core implementation. Let's see... the most likely place to start looking is `kernel/bpf/syscall.c`. Specifically, I'm betting the crucial function will be `bpf_btf_load`. That function name itself is pretty suggestive; it's practically screaming, "I'm the one who handles loading those BTF data structures!" Time to dig in and see what's really happening under the hood. I expect to find the nitty-gritty details of how BTF data is parsed, validated, and incorporated into the kernel's BPF infrastructure there. |
| 16/2 | 2026/09/02 07:11 | tool | grepper |
0mArgs:{
"Expression": "bpf_btf_load",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/syscall.c=5535=static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,\n--\nkernel/bpf/syscall.c-5563-\nkernel/bpf/syscall.c:5564:static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_attr *attr_log)\nkernel/bpf/syscall.c-5565-{\n--\nkernel/bpf/syscall.c=6303=static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size,\n--\nkernel/bpf/syscall.c-6416-\t\t\t\t\t\u0026attr_common, uattr_common, size_common);\nkernel/bpf/syscall.c:6417:\t\terr = err ?: bpf_btf_load(\u0026attr, uattr, \u0026attr_log);\nkernel/bpf/syscall.c-6418-\t\tbreak;\n"
} |
| 17/2 | 2026/09/02 07:11 | llm | patch-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 65114 output: 31 thoughts: 36 Thoughts: Okay, here's my take on those thoughts, framed as an expert's internal monologue: **Investigating Potential BTF Relocation Strategies** Alright, I'm diving back into this `btf.c` file. Given the task at hand, I need to methodically assess potential entry points for handling BTF relocation. The immediate question is: are there any *other* non-static functions within `btf.c` that might be relevant to relocation beyond what I've already considered? I need to perform a quick sweep to rule out any overlooked options. Now, let's zoom in on `btf_relocate` specifically. What's its role here? I need to analyze how it's currently structured and utilized. What assumptions does it make about the BTF data? Does it provide the necessary flexibility for my use case, or does it need modification? I'm thinking about its existing functionality and if there are any obvious areas for potential extension or customization to incorporate new logic. I need to understand its current limitations to determine the best approach. |
| 18/2 | 2026/09/02 07:11 | tool | grepper |
0mError:no matches Args: {
"Expression": "int btf_relocate",
"PathPrefix": "kernel/bpf/"
}Results: {
"error": "no matches"
} |
| 19/2 | 2026/09/02 07:11 | llm | patch-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 65420 output: 27 thoughts: 16 Thoughts: Here's my thought process, as if I were considering this search: **Diving into BTF Relocation** Okay, so I need to understand how `btf_relocate` is used across the entire kernel. That's a good starting point for… well, *something*. I need to understand its scope and impact. Let's start with a comprehensive search. I'm thinking of using the standard search tools to scour the kernel source code for every instance of `btf_relocate`. That'll provide a baseline. I'll need to pay close attention to the contexts in which it appears. Are we talking about core kernel relocations? Driver-specific handling? Perhaps something related to eBPF? I need to build a mental map of its usage. After the search, I'll organize my findings, noting the relevant code locations and how `btf_relocate` is being invoked and with what arguments. It's time to get a lay of the land. |
| 20/2 | 2026/09/02 07:11 | tool | grepper |
0mArgs:{
"Expression": "btf_relocate",
"PathPrefix": "kernel/"
}Results: {
"Output": "kernel/bpf/Makefile=68=obj-$(CONFIG_BPF_SYSCALL) += btf_iter.o\nkernel/bpf/Makefile:69:obj-$(CONFIG_BPF_SYSCALL) += btf_relocate.o\nkernel/bpf/Makefile-70-obj-$(CONFIG_BPF_SYSCALL) += kmem_cache_iter.o\n--\nkernel/bpf/btf.c=6688=struct btf *btf_parse_vmlinux(void)\n--\nkernel/bpf/btf.c-6720- */\nkernel/bpf/btf.c:6721:__u32 btf_relocate_id(const struct btf *btf, __u32 id)\nkernel/bpf/btf.c-6722-{\n--\nkernel/bpf/btf.c=6849=static struct btf *btf_parse_module(const char *module_name, const void *data,\n--\nkernel/bpf/btf.c-6911-\tif (base_btf != vmlinux_btf) {\nkernel/bpf/btf.c:6912:\t\terr = btf_relocate(btf, vmlinux_btf, \u0026btf-\u003ebase_id_map, \u0026module_str_map);\nkernel/bpf/btf.c-6913-\t\tif (err)\n--\nkernel/bpf/btf.c-6937-\t/*\nkernel/bpf/btf.c:6938:\t * With a distilled base, btf_relocate() replaces the base BTF and\nkernel/bpf/btf.c-6939-\t * rewrites string offsets. Check ordering only after that final BTF\n--\nkernel/bpf/btf.c=9467=static int btf_populate_kfunc_set(struct btf *btf, enum btf_kfunc_hook hook,\n--\nkernel/bpf/btf.c-9561-\tfor (i = set-\u003ecnt; i \u003c set-\u003ecnt + add_set-\u003ecnt; i++)\nkernel/bpf/btf.c:9562:\t\tset-\u003epairs[i].id = btf_relocate_id(btf, set-\u003epairs[i].id);\nkernel/bpf/btf.c-9563-\n--\nkernel/bpf/btf.c=9747=static int __register_btf_kfunc_id_set(enum btf_kfunc_hook hook,\n--\nkernel/bpf/btf.c-9759-\tfor (i = 0; i \u003c kset-\u003eset-\u003ecnt; i++) {\nkernel/bpf/btf.c:9760:\t\tret = btf_check_kfunc_protos(btf, btf_relocate_id(btf, kset-\u003eset-\u003epairs[i].id),\nkernel/bpf/btf.c-9761-\t\t\t\t\t kset-\u003eset-\u003epairs[i].flags);\n--\nkernel/bpf/btf.c=9816=static int btf_check_dtor_kfuncs(struct btf *btf, const struct btf_id_dtor_kfunc *dtors, u32 cnt)\n--\nkernel/bpf/btf.c-9823-\tfor (i = 0; i \u003c cnt; i++) {\nkernel/bpf/btf.c:9824:\t\tdtor_btf_id = btf_relocate_id(btf, dtors[i].kfunc_btf_id);\nkernel/bpf/btf.c-9825-\n--\nkernel/bpf/btf.c=9861=int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt,\n--\nkernel/bpf/btf.c-9919-\tfor (i = tab_cnt; i \u003c tab_cnt + add_cnt; i++) {\nkernel/bpf/btf.c:9920:\t\ttab-\u003edtors[i].btf_id = btf_relocate_id(btf, tab-\u003edtors[i].btf_id);\nkernel/bpf/btf.c:9921:\t\ttab-\u003edtors[i].kfunc_btf_id = btf_relocate_id(btf, tab-\u003edtors[i].kfunc_btf_id);\nkernel/bpf/btf.c-9922-\t}\n--\nkernel/bpf/btf_relocate.c-1-// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)\nkernel/bpf/btf_relocate.c:2:#include \"../../tools/lib/bpf/btf_relocate.c\"\n--\nkernel/trace/trace_events.c=2234=event_btf_ids_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)\n--\nkernel/trace/trace_events.c-2263-\t\t/* Module-local ids in ids[] need base+local relocation. */\nkernel/trace/trace_events.c:2264:\t\ttp_id = btf_relocate_id(btf, ids[1]);\nkernel/trace/trace_events.c-2265-\n--\nkernel/trace/trace_events.c-2271-\t\tif (call-\u003eflags \u0026 TRACE_EVENT_FL_TRACEPOINT) {\nkernel/trace/trace_events.c:2272:\t\t\tt = btf_type_by_id(btf, btf_relocate_id(btf, ids[0]));\nkernel/trace/trace_events.c-2273-\t\t\traw_id = t ? t-\u003etype : 0;\n"
} |
| 21/2 | 2026/09/02 07:11 | llm | patch-evaluator |
1mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 67136 output: 220 thoughts: 299 Thoughts: **My Assessment of the BTF Configuration Requirements** Okay, let's break this down. First, I need to verify the focus symbols related to BTF handling. Specifically, I'm thinking about the key entry points and functions, like `bpf_btf_load` (and its counterpart `btf_new_fd`), `btf_relocate`, `sysfs_btf_add`, and `btf_module_notify`. These are where the core BTF operations take place and are thus critical to understanding the system's behavior in this context. Next, I need to ensure the correct kernel configurations are enabled. I'm focusing on the following: `BPF_SYSCALL`, `DEBUG_INFO_BTF`, `DEBUG_INFO_BTF_MODULES`, `DEBUG_INFO_BTF_INLINE`, and `SYSFS`. Without these, my analysis will be incomplete, and I won't have the necessary hooks and debug information to get a full picture. Now, let's cross-reference the configurations and symbols to ensure the "set-results" conditions are met. I need to make sure the enablement configurations are actually enabled – that's the list of configs. And that the symbols I care about, namely, those related to loading and relocating BTF data (`bpf_btf_load`, `btf_new_fd`, `btf_relocate`, `sysfs_btf_add`) are actually available. Finally, the justification for fuzzing this area seems valid, the reasoning makes sense. It covers the new BTF kinds (LOC_PARAM, LOC_PROTO, LOCSEC), which implies new data structures or behavior. Also, the verification logic, how BTF is split or inlined, and the sysfs exposure all make a compelling case for examination. This is all exposed via BPF_BTF_LOAD and module loading/sysfs operations. So, yep, let's call `set-results`. We have enough information to proceed. |
| 22/2 | 2026/09/02 07:12 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"BPF_SYSCALL",
"DEBUG_INFO_BTF",
"DEBUG_INFO_BTF_MODULES",
"DEBUG_INFO_BTF_INLINE",
"SYSFS"
],
"FocusSymbols": [
"bpf_btf_load",
"btf_new_fd",
"btf_relocate",
"sysfs_btf_add"
],
"Reasoning": "The patch introduces support for inline BTF with three new BTF kinds (BTF_KIND_LOC_PARAM, BTF_KIND_LOC_PROTO, and BTF_KIND_LOCSEC), including type validation and resolution metadata checks in kernel/bpf/btf.c, split BTF relocation updates, and kernfs/sysfs interaction. These changes modify reachable core BTF verification and parsing logic reachable via the bpf(BPF_BTF_LOAD) syscall and module loading.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"BPF_SYSCALL",
"DEBUG_INFO_BTF",
"DEBUG_INFO_BTF_MODULES",
"DEBUG_INFO_BTF_INLINE",
"SYSFS"
],
"FocusSymbols": [
"bpf_btf_load",
"btf_new_fd",
"btf_relocate",
"sysfs_btf_add"
],
"Reasoning": "The patch introduces support for inline BTF with three new BTF kinds (BTF_KIND_LOC_PARAM, BTF_KIND_LOC_PROTO, and BTF_KIND_LOCSEC), including type validation and resolution metadata checks in kernel/bpf/btf.c, split BTF relocation updates, and kernfs/sysfs interaction. These changes modify reachable core BTF verification and parsing logic reachable via the bpf(BPF_BTF_LOAD) syscall and module loading.",
"WorthFuzzing": true
} |
| 23/1 | 2026/09/02 07:12 | action | If |
2mArgs:{
"WorthFuzzing": true
}Results: null |
| 24/2 | 2026/09/02 07:12 | agent | kmsan-evaluator |
2mModel:core Args: null Results: {
"KMSANReasoning": "The patch series introduces support for inline BTF (BTF_KIND_LOC_PARAM, BTF_KIND_LOC_PROTO, and BTF_KIND_LOCSEC) along with kernel module parsing and sysfs exposure of .BTF.inline sections via /sys/kernel/btf/*.\n\nAnalysis of KMSAN vs KASAN applicability:\n- Memory Allocations and Initialization: All new kernel structures (e.g., struct btf, struct btf_module, struct bin_attribute) are allocated using kzalloc_obj(), ensuring zero-initialization. BTF payload data buffers are created using kvmemdup() from existing ELF sections.\n- User-space Data Transfers: sysfs reads (sysfs_btf_kernfs_read) copy explicitly sized BTF byte streams, with offsets and lengths strictly bounded by the acquired data_size. There are no kernel structs with padding or uninitialized fields exposed to user space.\n- Logic \u0026 Variables: All local variables in the parsing, validation, and notification paths are initialized before use.\n- Potential risks in this patch series involve bounds checking of BTF sections, offset calculations, type resolution, or memory management, all of which are addressable by KASAN, not KMSAN.\n\nTherefore, this patch series does not introduce uninitialized memory risks or info-leaks, and a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
}Instruction: You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.
CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
a comprehensive suite of debugging tools and sanitizers, including KASAN
(out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
(locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
or page allocations) and kernel-to-user memory info-leaks.
Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.
Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.
Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
attributes, ioctl output arguments, socket options, or BPF buffers) where fields
or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
uninitialized bytes of existing buffers.
Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
dereferences, locking deadlocks, or use-after-free bugs (these are already caught
by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.
Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: For your convenience, here is the diff of the changes:
commit f8e95b56b8cab7d6d143f7b8bfacbef2d3b9498b
Author: syz-cluster <triage@syzkaller.com>
Date: Wed Sep 2 07:09:32 2026 +0000
syz-cluster: applied patch under review
diff --git a/Makefile b/Makefile
index 130926fa592e2..4fa15fa5d1ed1 100644
--- a/Makefile
+++ b/Makefile
@@ -1733,6 +1733,7 @@ endif # CONFIG_MODULES
CLEAN_FILES += vmlinux.symvers modules-only.symvers \
modules.builtin modules.builtin.modinfo modules.nsdeps \
modules.builtin.ranges vmlinux.o.map vmlinux.unstripped \
+ vmlinux.BTF.inline \
vmlinux.thinlto-index builtin.order \
compile_commands.json rust/test \
rust-project.json .vmlinux.objs .vmlinux.export.c \
diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index b2988aa12f664..a2e192854c47f 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -675,12 +675,23 @@
/*
* .BTF
*/
+#ifdef CONFIG_DEBUG_INFO_BTF_INLINE
+#define BTF_INLINE \
+ . = ALIGN(PAGE_SIZE); \
+ .BTF.inline : AT(ADDR(.BTF.inline) - LOAD_OFFSET) { \
+ BOUNDED_SECTION_BY(.BTF.inline, _BTF_inline) \
+ }
+#else
+#define BTF_INLINE
+#endif
+
#ifdef CONFIG_DEBUG_INFO_BTF
#define BTF \
. = ALIGN(PAGE_SIZE); \
.BTF : AT(ADDR(.BTF) - LOAD_OFFSET) { \
BOUNDED_SECTION_BY(.BTF, _BTF) \
} \
+ BTF_INLINE \
. = ALIGN(PAGE_SIZE); \
.BTF_ids : AT(ADDR(.BTF_ids) - LOAD_OFFSET) { \
*(.BTF_ids) \
diff --git a/include/linux/btf.h b/include/linux/btf.h
index ddd0f4f32d24b..702ae246052f5 100644
--- a/include/linux/btf.h
+++ b/include/linux/btf.h
@@ -329,6 +329,21 @@ static inline u64 btf_enum64_value(const struct btf_enum64 *e)
return ((u64)e->val_hi32 << 32) | e->val_lo32;
}
+static inline struct btf_loc_param *btf_loc_param(const struct btf_type *t)
+{
+ return (struct btf_loc_param *)(t + 1);
+}
+
+static inline __u32 *btf_loc_params(const struct btf_type *t)
+{
+ return (__u32 *)(t + 1);
+}
+
+static inline struct btf_loc *btf_type_loc_secinfo(const struct btf_type *t)
+{
+ return (struct btf_loc *)(t + 1);
+}
+
static inline bool btf_is_composite(const struct btf_type *t)
{
u16 kind = btf_kind(t);
@@ -559,7 +574,7 @@ struct btf_field_desc {
/* member struct size, or zero, if no members */
int m_sz;
/* repeated per-member offsets */
- int m_off_cnt, m_offs[1];
+ int m_off_cnt, m_offs[2];
};
struct btf_field_iter {
@@ -573,7 +588,8 @@ struct btf_field_iter {
#ifdef CONFIG_BPF_SYSCALL
const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
void btf_set_base_btf(struct btf *btf, const struct btf *base_btf);
-int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **map_ids);
+int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **map_ids,
+ __u32 **map_strs);
int btf_field_iter_init(struct btf_field_iter *it, struct btf_type *t,
enum btf_field_iter_kind iter_kind);
__u32 *btf_field_iter_next(struct btf_field_iter *it);
@@ -602,6 +618,7 @@ int get_kern_ctx_btf_id(struct bpf_verifier_log *log, enum bpf_prog_type prog_ty
bool btf_types_are_same(const struct btf *btf1, u32 id1,
const struct btf *btf2, u32 id2);
int btf_check_iter_arg(struct btf *btf, const struct btf_type *func, int arg_idx);
+struct bin_attribute *sysfs_btf_add(const char *name, void *data, size_t data_size);
static inline bool btf_type_is_struct_ptr(struct btf *btf, const struct btf_type *t)
{
@@ -624,7 +641,7 @@ static inline void btf_set_base_btf(struct btf *btf, const struct btf *base_btf)
}
static inline int btf_relocate(void *log, struct btf *btf, const struct btf *base_btf,
- __u32 **map_ids)
+ __u32 **map_ids, __u32 **map_strs)
{
return -EOPNOTSUPP;
}
diff --git a/include/linux/module.h b/include/linux/module.h
index 7566815fabbe8..3d32c4e86f445 100644
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -507,6 +507,10 @@ struct module {
void *btf_data;
void *btf_base_data;
#endif
+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)
+ unsigned int btf_inline_data_size;
+ void *btf_inline_data;
+#endif
#ifdef CONFIG_JUMP_LABEL
struct jump_entry *jump_entries;
unsigned int num_jump_entries;
diff --git a/include/uapi/linux/btf.h b/include/uapi/linux/btf.h
index 618167cab4e61..6062c99580345 100644
--- a/include/uapi/linux/btf.h
+++ b/include/uapi/linux/btf.h
@@ -92,7 +92,9 @@ enum {
BTF_KIND_DECL_TAG = 17, /* Decl Tag */
BTF_KIND_TYPE_TAG = 18, /* Type Tag */
BTF_KIND_ENUM64 = 19, /* Enumeration up to 64-bit values */
-
+ BTF_KIND_LOC_PARAM = 20, /* Location parameter information */
+ BTF_KIND_LOC_PROTO = 21, /* Location prototype for site */
+ BTF_KIND_LOCSEC = 22, /* Location section */
NR_BTF_KINDS,
BTF_KIND_MAX = NR_BTF_KINDS - 1,
};
@@ -212,4 +214,65 @@ struct btf_enum64 {
__u32 val_hi32;
};
+/*
+ * BTF_KIND_LOC_PARAM is followed by a single "struct btf_loc_param"
+ * that contains flags specifying the contents of the vlen-specified
+ * number of 4-byte values that follow.
+ */
+struct btf_loc_param {
+ __u32 flags;
+};
+
+/*
+ * The combination of size, vlen and flags gives us the means to interpret
+ * the following vlen-specified set of 4-byte values:
+ *
+ * - a BTF_LOC_PARAM_CONST is a constant value; combination
+ * of size, vlen and _SIGNED flag determines it. If the value requires
+ * 64 bits it is stored in {lo,hi} order.
+ * - a BTF_LOC_PARAM_ADDR is an address that will be normalized with
+ * respect to kernel base address.
+ * - a BTF_LOC_PARAM_REG with vlen 1 is a simple register number;
+ * with vlen 2 it is a multi-register parameter.
+ * - a _REG | DEREF with vlen 1 dereferences the value in the register
+ * number specified.
+ * - a REG | DEREF | OFFSET with vlen specifies the register value in
+ * the first 4-byte value and the offset in the remainder.
+ * - binary logical operators operate on a combination of register
+ * number and constant value, aside from _NOT which operates on
+ * a register
+ */
+enum btf_loc_param_flags {
+ BTF_LOC_PARAM_SIGNED = 0x1,
+ BTF_LOC_PARAM_CONST = 0x2,
+ BTF_LOC_PARAM_ADDR = 0x4,
+ BTF_LOC_PARAM_REG = 0x8,
+ BTF_LOC_PARAM_DEREF = 0x10,
+ BTF_LOC_PARAM_OFFSET = 0x20,
+};
+
+/*
+ * BTF_KIND_LOC_PROTO specifies location prototypes; i.e. how locations relate
+ * to parameters; a struct btf_type of BTF_KIND_LOC_PROTO is followed by a
+ * a vlen-specified number of __u32 BTF type ids which specify the associated
+ * BTF_KIND_LOC_PARAM for each function parameter associated with the
+ * location. The type should either be 0 (no location info) or point at
+ * a BTF_KIND_LOC_PARAM.
+ */
+
+/*
+ * BTF_KIND_LOCSEC consists of vlen-specified number of "struct btf_loc"
+ * containing location site-specific information;
+ *
+ * - function (func)
+ * - location prototype type id (loc_proto)
+ * - address offset (offset) relative to kernel base address
+ */
+
+struct btf_loc {
+ __u32 func;
+ __u32 loc_proto;
+ __u32 offset;
+};
+
#endif /* _UAPI__LINUX_BTF_H__ */
diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile
index 9a92c348bbda6..60aa5adb03540 100644
--- a/kernel/bpf/Makefile
+++ b/kernel/bpf/Makefile
@@ -42,6 +42,7 @@ obj-$(CONFIG_BPF_SYSCALL) += reuseport_array.o
endif
ifeq ($(CONFIG_SYSFS),y)
obj-$(CONFIG_DEBUG_INFO_BTF) += sysfs_btf.o
+obj-$(CONFIG_DEBUG_INFO_BTF_INLINE) += btf_vmlinux_inline.o
endif
ifeq ($(CONFIG_BPF_JIT),y)
obj-$(CONFIG_BPF_SYSCALL) += bpf_struct_ops.o
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 9c2cab08bb794..2f3e8cea7dfcd 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -8,6 +8,7 @@
#include <linux/seq_file.h>
#include <linux/compiler.h>
#include <linux/ctype.h>
+#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/anon_inodes.h>
@@ -25,6 +26,7 @@
#include <linux/perf_event.h>
#include <linux/bsearch.h>
#include <linux/kobject.h>
+#include <linux/kernfs.h>
#include <linux/string.h>
#include <linux/sysfs.h>
#include <linux/overflow.h>
@@ -345,6 +347,9 @@ static const char * const btf_kind_str[NR_BTF_KINDS] = {
[BTF_KIND_DECL_TAG] = "DECL_TAG",
[BTF_KIND_TYPE_TAG] = "TYPE_TAG",
[BTF_KIND_ENUM64] = "ENUM64",
+ [BTF_KIND_LOC_PARAM] = "LOC_PARAM",
+ [BTF_KIND_LOC_PROTO] = "LOC_PROTO",
+ [BTF_KIND_LOCSEC] = "LOCSEC",
};
const char *btf_type_str(const struct btf_type *t)
@@ -517,11 +522,27 @@ static bool btf_type_is_decl_tag(const struct btf_type *t)
return BTF_INFO_KIND(t->info) == BTF_KIND_DECL_TAG;
}
+static bool btf_type_is_loc_param(const struct btf_type *t)
+{
+ return BTF_INFO_KIND(t->info) == BTF_KIND_LOC_PARAM;
+}
+
+static bool btf_type_is_loc_proto(const struct btf_type *t)
+{
+ return BTF_INFO_KIND(t->info) == BTF_KIND_LOC_PROTO;
+}
+
+static bool btf_type_is_locsec(const struct btf_type *t)
+{
+ return BTF_INFO_KIND(t->info) == BTF_KIND_LOCSEC;
+}
+
static bool btf_type_nosize(const struct btf_type *t)
{
return btf_type_is_void(t) || btf_type_is_fwd(t) ||
btf_type_is_func(t) || btf_type_is_func_proto(t) ||
- btf_type_is_decl_tag(t);
+ btf_type_is_decl_tag(t) || btf_type_is_loc_param(t) ||
+ btf_type_is_loc_proto(t) || btf_type_is_locsec(t);
}
static bool btf_type_nosize_or_null(const struct btf_type *t)
@@ -796,7 +817,9 @@ static bool btf_type_needs_resolve(const struct btf_type *t)
btf_type_is_var(t) ||
btf_type_is_func(t) ||
btf_type_is_decl_tag(t) ||
- btf_type_is_datasec(t);
+ btf_type_is_datasec(t) ||
+ btf_type_is_loc_proto(t) ||
+ btf_type_is_locsec(t);
}
/* t->size can be used */
@@ -1920,7 +1943,7 @@ void btf_set_base_btf(struct btf *btf, const struct btf *base_btf)
{
btf->base_btf = (struct btf *)base_btf;
btf->start_id = btf_nr_types(base_btf);
- btf->start_str_off = base_btf->hdr.str_len;
+ btf->start_str_off = base_btf->start_str_off + base_btf->hdr.str_len;
}
static int env_resolve_init(struct btf_verifier_env *env)
@@ -4714,6 +4737,213 @@ static const struct btf_kind_operations enum64_ops = {
.show = btf_enum64_show,
};
+static s32 btf_loc_param_check_meta(struct btf_verifier_env *env,
+ const struct btf_type *t,
+ u32 meta_left)
+{
+ const struct btf_loc_param *p = btf_loc_param(t);
+ u32 meta_needed;
+ u32 size;
+
+ meta_needed = sizeof(*p) + sizeof(__u32) * btf_vlen(t);
+ if (meta_left < meta_needed) {
+ btf_verifier_log_basic(env, t,
+ "meta_left:%u meta_needed:%u",
+ meta_left, meta_needed);
+ return -EINVAL;
+ }
+
+ if (t->name_off) {
+ btf_verifier_log_type(env, t, "Invalid name");
+ return -EINVAL;
+ }
+ size = t->size;
+ if (size > 16 || !is_power_of_2(size)) {
+ btf_verifier_log_type(env, t, "Unexpected size");
+ return -EINVAL;
+ }
+
+ return meta_needed;
+}
+
+static void btf_loc_param_log(struct btf_verifier_env *env,
+ const struct btf_type *t)
+{
+ const struct btf_loc_param *p = btf_loc_param(t);
+ u32 *d = (u32 *)(p + 1);
+ u32 i, vlen = btf_vlen(t);
+
+ btf_verifier_log(env, "size=%u flags=0x%x param_data(", t->size, p->flags);
+ for (i = 0; i < vlen; i++, d++)
+ btf_verifier_log(env, "%u%s", *d,
+ i + 1 == vlen ? ")" : ", ");
+}
+
+static const struct btf_kind_operations loc_param_ops = {
+ .check_meta = btf_loc_param_check_meta,
+ .resolve = btf_df_resolve,
+ .check_member = btf_df_check_member,
+ .check_kflag_member = btf_df_check_kflag_member,
+ .log_details = btf_loc_param_log,
+ .show = btf_df_show,
+};
+
+static s32 btf_loc_proto_check_meta(struct btf_verifier_env *env,
+ const struct btf_type *t,
+ u32 meta_left)
+{
+ u32 meta_needed;
+
+ meta_needed = sizeof(__u32) * btf_type_vlen(t);
+
+ if (meta_left < meta_needed) {
+ btf_verifier_log_basic(env, t,
+ "meta_left:%u meta_needed:%u",
+ meta_left, meta_needed);
+ return -EINVAL;
+ }
+
+ if (t->name_off) {
+ btf_verifier_log_type(env, t, "Invalid name");
+ return -EINVAL;
+ }
+ return meta_needed;
+}
+
+static void btf_loc_proto_log(struct btf_verifier_env *env,
+ const struct btf_type *t)
+{
+ const __u32 *params = btf_loc_params(t);
+ u32 nr_params = btf_type_vlen(t), i;
+
+ btf_verifier_log(env, "loc_proto locs=(");
+ for (i = 0; i < nr_params; i++, params++) {
+ btf_verifier_log(env, "type=%u%s", *params,
+ i + 1 == nr_params ? ")" : ", ");
+ }
+}
+
+static int btf_loc_proto_resolve(struct btf_verifier_env *env,
+ const struct resolve_vertex *v)
+{
+ const __u32 *params = btf_loc_params(v->t);
+ u32 i, nr_params = btf_type_vlen(v->t);
+ struct btf *btf = env->btf;
+
+ for (i = 0; i < nr_params; i++) {
+ const struct btf_type *param_type;
+ u32 param_type_id = params[i];
+
+ if (!param_type_id)
+ continue;
+
+ param_type = btf_type_by_id(btf, param_type_id);
+ if (!param_type || !btf_type_is_loc_param(param_type)) {
+ btf_verifier_log_type(env, v->t,
+ "Invalid loc_param#%u", i + 1);
+ return -EINVAL;
+ }
+ }
+
+ env_stack_pop_resolved(env, 0, 0);
+ return 0;
+}
+
+static const struct btf_kind_operations loc_proto_ops = {
+ .check_meta = btf_loc_proto_check_meta,
+ .resolve = btf_loc_proto_resolve,
+ .check_member = btf_df_check_member,
+ .check_kflag_member = btf_df_check_kflag_member,
+ .log_details = btf_loc_proto_log,
+ .show = btf_df_show,
+};
+
+static s32 btf_locsec_check_meta(struct btf_verifier_env *env,
+ const struct btf_type *t,
+ u32 meta_left)
+{
+ u32 meta_needed;
+
+ meta_needed = sizeof(struct btf_loc) * btf_type_vlen(t);
+
+ if (meta_left < meta_needed) {
+ btf_verifier_log_basic(env, t,
+ "meta_left:%u meta_needed:%u",
+ meta_left, meta_needed);
+ return -EINVAL;
+ }
+ return meta_needed;
+}
+
+static void btf_locsec_log(struct btf_verifier_env *env,
+ const struct btf_type *t)
+{
+ const struct btf_loc *loc = btf_type_loc_secinfo(t);
+ u32 nr_locs = btf_type_vlen(t), i;
+ const struct btf *btf = env->btf;
+
+ btf_verifier_log(env, "locsec %s locs=(",
+ __btf_name_by_offset(btf, t->name_off));
+ for (i = 0; i < nr_locs; i++, loc++) {
+ btf_verifier_log(env, "\n\tfunc %u loc_proto %u offset 0x%x%s",
+ loc->func, loc->loc_proto, loc->offset,
+ i + 1 == nr_locs ? ")" : ", ");
+ }
+}
+
+static int btf_locsec_resolve(struct btf_verifier_env *env,
+ const struct resolve_vertex *v)
+{
+ const struct btf_loc *loc;
+ struct btf *btf = env->btf;
+ u32 i;
+
+ env->resolve_mode = RESOLVE_TBD;
+ for (i = v->next_member, loc = btf_type_loc_secinfo(v->t) + i;
+ i < btf_type_vlen(v->t); i++, loc++) {
+ const struct btf_type *func_type, *loc_proto_type;
+ u32 func_type_id = loc->func;
+ u32 loc_proto_type_id = loc->loc_proto;
+
+ func_type = btf_type_by_id(btf, func_type_id);
+ if (!func_type || !btf_type_is_func(func_type)) {
+ btf_verifier_log_type(env, v->t,
+ "Invalid func#%u", i + 1);
+ return -EINVAL;
+ }
+
+ if (!env_type_is_resolved(env, func_type_id)) {
+ env_stack_set_next_member(env, i);
+ return env_stack_push(env, func_type, func_type_id);
+ }
+
+ loc_proto_type = btf_type_by_id(btf, loc_proto_type_id);
+ if (!loc_proto_type || !btf_type_is_loc_proto(loc_proto_type)) {
+ btf_verifier_log_type(env, v->t,
+ "Invalid loc_proto#%u", i + 1);
+ return -EINVAL;
+ }
+
+ if (!env_type_is_resolved(env, loc_proto_type_id)) {
+ env_stack_set_next_member(env, i + 1);
+ return env_stack_push(env, loc_proto_type,
+ loc_proto_type_id);
+ }
+ }
+
+ env_stack_pop_resolved(env, 0, 0);
+ return 0;
+}
+
+static const struct btf_kind_operations locsec_ops = {
+ .check_meta = btf_locsec_check_meta,
+ .resolve = btf_locsec_resolve,
+ .check_member = btf_df_check_member,
+ .check_kflag_member = btf_df_check_kflag_member,
+ .log_details = btf_locsec_log,
+ .show = btf_df_show,
+};
+
static s32 btf_func_proto_check_meta(struct btf_verifier_env *env,
const struct btf_type *t,
u32 meta_left)
@@ -5383,6 +5613,9 @@ static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = {
[BTF_KIND_DECL_TAG] = &decl_tag_ops,
[BTF_KIND_TYPE_TAG] = &modifier_ops,
[BTF_KIND_ENUM64] = &enum64_ops,
+ [BTF_KIND_LOC_PARAM] = &loc_param_ops,
+ [BTF_KIND_LOC_PROTO] = &loc_proto_ops,
+ [BTF_KIND_LOCSEC] = &locsec_ops,
};
static s32 btf_check_meta(struct btf_verifier_env *env,
@@ -5457,7 +5690,8 @@ static bool btf_resolve_valid(struct btf_verifier_env *env,
if (!env_type_is_resolved(env, type_id))
return false;
- if (btf_type_is_struct(t) || btf_type_is_datasec(t))
+ if (btf_type_is_struct(t) || btf_type_is_datasec(t) ||
+ btf_type_is_loc_proto(t) || btf_type_is_locsec(t))
return !btf_resolved_type_id(btf, type_id) &&
!btf_resolved_type_size(btf, type_id);
@@ -6493,15 +6727,140 @@ __u32 btf_relocate_id(const struct btf *btf, __u32 id)
#ifdef CONFIG_DEBUG_INFO_BTF_MODULES
+static struct btf *btf_parse_split(struct btf_verifier_env *env, const char *name,
+ const void *data, unsigned int data_size,
+ struct btf *base_btf)
+{
+ struct btf *btf;
+ int err;
+
+ btf = kzalloc_obj(*btf, GFP_KERNEL | __GFP_NOWARN);
+ if (!btf)
+ return ERR_PTR(-ENOMEM);
+ env->btf = btf;
+
+ btf_set_base_btf(btf, base_btf);
+ btf->kernel_btf = true;
+ btf->named_start_id = 0;
+ strscpy(btf->name, name);
+
+ btf->data = kvmemdup(data, data_size, GFP_KERNEL | __GFP_NOWARN);
+ if (!btf->data) {
+ err = -ENOMEM;
+ goto errout;
+ }
+ btf->data_size = data_size;
+
+ err = btf_parse_hdr(env);
+ if (err)
+ goto errout;
+
+ btf->nohdr_data = btf->data + btf->hdr.hdr_len;
+
+ err = btf_parse_str_sec(env);
+ if (err)
+ goto errout;
+
+ err = btf_check_all_metas(env);
+ if (err)
+ goto errout;
+
+ err = btf_check_modifier_chain_length(env, btf, btf_nr_types(base_btf));
+ if (err)
+ goto errout;
+
+ return btf;
+
+errout:
+ btf_free(btf);
+ return ERR_PTR(err);
+}
+
+static int btf_rebase_inline(struct btf *inline_btf, const struct btf *module_btf,
+ const u32 *module_id_map, const u32 *module_str_map,
+ u32 old_module_type_cnt)
+{
+ u32 old_start_id = inline_btf->start_id;
+ u32 old_start_str_off = inline_btf->start_str_off;
+ u32 old_module_start_str_off = old_start_str_off - module_btf->hdr.str_len;
+ u32 new_start_id = btf_nr_types(module_btf);
+ u32 new_start_str_off = module_btf->start_str_off + module_btf->hdr.str_len;
+ s64 id_delta = (s64)new_start_id - old_start_id;
+ s64 str_delta = (s64)new_start_str_off - old_start_str_off;
+ u32 i;
+
+ /*
+ * The inline BTF was parsed relative to the original module BTF. Its
+ * base IDs must therefore use the map generated when that BTF was
+ * relocated, while IDs for inline-local types only move by the change
+ * in the module BTF's starting ID.
+ */
+ for (i = 0; i < inline_btf->nr_types; i++) {
+ struct btf_field_iter it;
+ struct btf_type *t = inline_btf->types[i];
+ u32 *id, *str_off;
+ int err;
+
+ err = btf_field_iter_init(&it, t, BTF_FIELD_ITER_IDS);
+ if (err)
+ return err;
+ while ((id = btf_field_iter_next(&it))) {
+ if (!*id)
+ continue;
+ if (*id < old_module_type_cnt) {
+ if (module_id_map)
+ *id = module_id_map[*id];
+ } else if (*id >= old_start_id) {
+ *id += id_delta;
+ } else {
+ return -EINVAL;
+ }
+ }
+
+ err = btf_field_iter_init(&it, t, BTF_FIELD_ITER_STRS);
+ if (err)
+ return err;
+ while ((str_off = btf_field_iter_next(&it))) {
+ if (!*str_off)
+ continue;
+ /*
+ * LOCSEC names its code section in the module BTF's string
+ * section. Shift those inherited strings together with
+ * inline-local strings when replacing a distilled base BTF.
+ */
+ if (*str_off < old_module_start_str_off) {
+ /* vmlinux strings retain their offsets for in-tree modules. */
+ if (!module_id_map)
+ continue;
+ if (!module_str_map || !module_str_map[*str_off])
+ return -EINVAL;
+ *str_off = module_str_map[*str_off];
+ continue;
+ }
+ *str_off += str_delta;
+ }
+ }
+
+ btf_set_base_btf(inline_btf, module_btf);
+ btf_check_sorted(inline_btf);
+ return 0;
+}
+
static struct btf *btf_parse_module(const char *module_name, const void *data,
unsigned int data_size, void *base_data,
- unsigned int base_data_size)
+ unsigned int base_data_size, const void *inline_data,
+ unsigned int inline_data_size, bool vmlinux_inline,
+ void **relocated_inline_data)
{
- struct btf *btf = NULL, *vmlinux_btf, *base_btf = NULL;
+ struct btf *btf = NULL, *inline_btf = NULL, *vmlinux_btf, *base_btf = NULL;
+ struct btf *inline_base_btf;
struct btf_verifier_env *env = NULL;
struct bpf_verifier_log *log;
+ u32 old_module_type_cnt;
+ u32 *module_str_map = NULL;
int err = 0;
+ *relocated_inline_data = NULL;
vmlinux_btf = bpf_get_btf_vmlinux();
if (IS_ERR(vmlinux_btf))
return vmlinux_btf;
@@ -6525,67 +6884,75 @@ static struct btf *btf_parse_module(const char *module_name, const void *data,
base_btf = vmlinux_btf;
}
- btf = kzalloc_obj(*btf, GFP_KERNEL | __GFP_NOWARN);
- if (!btf) {
- err = -ENOMEM;
+ btf = btf_parse_split(env, module_name, data, data_size, base_btf);
+ if (IS_ERR(btf)) {
+ err = PTR_ERR(btf);
+ btf = NULL;
goto errout;
}
- env->btf = btf;
- btf->base_btf = base_btf;
- btf->start_id = base_btf->nr_types;
- btf->start_str_off = base_btf->hdr.str_len;
- btf->kernel_btf = true;
- btf->named_start_id = 0;
- strscpy(btf->name, module_name);
-
- btf->data = kvmemdup(data, data_size, GFP_KERNEL | __GFP_NOWARN);
- if (!btf->data) {
- err = -ENOMEM;
- goto errout;
+ if (inline_data_size) {
+ /*
+ * Ordinary module inline BTF is split relative to the module BTF.
+ * The btf_vmlinux_inline delivery module instead carries BTF split
+ * directly relative to vmlinux.
+ */
+ inline_base_btf = vmlinux_inline ? vmlinux_btf : btf;
+ inline_btf = btf_parse_split(env, module_name, inline_data,
+ inline_data_size, inline_base_btf);
+ if (IS_ERR(inline_btf)) {
+ pr_warn("failed to validate module [%s] inline BTF: %ld\n",
+ module_name, PTR_ERR(inline_btf));
+ inline_btf = NULL;
+ }
}
- btf->data_size = data_size;
-
- err = btf_parse_hdr(env);
- if (err)
- goto errout;
-
- btf->nohdr_data = btf->data + btf->hdr.hdr_len;
-
- err = btf_parse_str_sec(env);
- if (err)
- goto errout;
-
- err = btf_check_all_metas(env);
- if (err)
- goto errout;
-
- err = btf_check_modifier_chain_length(env, btf, btf_nr_types(base_btf));
- if (err)
- goto errout;
+ old_module_type_cnt = btf_nr_types(btf);
if (base_btf != vmlinux_btf) {
- err = btf_relocate(btf, vmlinux_btf, &btf->base_id_map);
+ err = btf_relocate(btf, vmlinux_btf, &btf->base_id_map, &module_str_map);
if (err)
goto errout;
btf_free(base_btf);
base_btf = vmlinux_btf;
}
- btf_verifier_env_free(env);
+ if (inline_btf) {
+ if (!vmlinux_inline) {
+ err = btf_rebase_inline(inline_btf, btf, btf->base_id_map,
+ module_str_map, old_module_type_cnt);
+ if (err) {
+ pr_warn("failed to relocate module [%s] inline BTF: %d\n",
+ module_name, err);
+ btf_free(inline_btf);
+ inline_btf = NULL;
+ }
+ }
+ if (inline_btf) {
+ *relocated_inline_data = inline_btf->data;
+ inline_btf->data = NULL;
+ btf_free(inline_btf);
+ }
+ }
+
+ /*
+ * With a distilled base, btf_relocate() replaces the base BTF and
+ * rewrites string offsets. Check ordering only after that final BTF
+ * view has been established, so named_start_id describes the BTF used
+ * by name lookups.
+ */
btf_check_sorted(btf);
+ btf_verifier_env_free(env);
+ kvfree(module_str_map);
refcount_set(&btf->refcnt, 1);
return btf;
errout:
+ kvfree(module_str_map);
btf_verifier_env_free(env);
+ btf_free(inline_btf);
if (!IS_ERR(base_btf) && base_btf != vmlinux_btf)
btf_free(base_btf);
- if (btf) {
- kvfree(btf->data);
- kvfree(btf->types);
- kfree(btf);
- }
+ btf_free(btf);
return ERR_PTR(err);
}
@@ -8503,12 +8870,163 @@ enum {
BTF_MODULE_F_LIVE = (1 << 0),
};
+#if IS_ENABLED(CONFIG_SYSFS)
+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)
+static struct bin_attribute *vmlinux_inline_attr;
+#endif
+
+static int sysfs_btf_bin_attr_load(struct bin_attribute *attr)
+{
+ char modname[MODULE_NAME_LEN + sizeof("btf_vmlinux_inline")];
+ int retries = 0;
+
+ /* First on-demand read; load module. */
+ snprintf(modname, sizeof(modname), "btf_%s", attr->attr.name);
+ strreplace(modname, '.', '_');
+ request_module("%s", modname);
+
+ /*
+ * request_module() is synchronous, but the module notifier is
+ * responsible for updating private data, so retries are required.
+ */
+ while (retries++ < 10) {
+ if (smp_load_acquire(&attr->size))
+ return 0;
+ udelay(50);
+ }
+ return -ENODEV;
+}
+
+static int sysfs_btf_kernfs_open(struct kernfs_open_file *of)
+{
+ struct bin_attribute *attr = of->kn->priv;
+ size_t data_size;
+ int err;
+
+ if (!smp_load_acquire(&attr->size)) {
+ err = sysfs_btf_bin_attr_load(attr);
+ if (err)
+ return err;
+ }
+ /* Refresh file size or the open() caller will not see updated size. */
+ data_size = smp_load_acquire(&attr->size);
+ of->kn->attr.size = data_size;
+ if (of->file) {
+ struct inode *inode = file_inode(of->file);
+
+ if (inode)
+ i_size_write(inode, data_size);
+ }
+ return 0;
+}
+
+static ssize_t sysfs_btf_kernfs_read(struct kernfs_open_file *of, char *buf,
+ size_t bytes_requested, loff_t offset)
+{
+ struct bin_attribute *attr = of->kn->priv;
+ void *data;
+ size_t data_size;
+
+ data_size = smp_load_acquire(&attr->size);
+ if (offset >= data_size)
+ return 0;
+ if (offset + bytes_requested > data_size)
+ bytes_requested = data_size - offset;
+ data = READ_ONCE(attr->private);
+ memcpy(buf, data + offset, bytes_requested);
+
+ return bytes_requested;
+}
+
+static const struct kernfs_ops sysfs_btf_kernfs_ops = {
+ .open = sysfs_btf_kernfs_open,
+ .read = sysfs_btf_kernfs_read,
+};
+
+struct bin_attribute *sysfs_btf_add(const char *name, void *data, size_t data_size)
+{
+ struct bin_attribute *attr;
+ int err = 0;
+
+ attr = kzalloc_obj(*attr);
+ if (!attr)
+ return ERR_PTR(-ENOMEM);
+
+ sysfs_bin_attr_init(attr);
+ attr->attr.mode = 0444;
+ attr->size = data_size;
+ attr->private = data;
+ attr->read = sysfs_bin_attr_simple_read;
+ attr->attr.name = kstrdup(name, GFP_KERNEL);
+ if (!attr->attr.name) {
+ err = -ENOMEM;
+ goto err_free;
+ }
+ if (data_size > 0) {
+ err = sysfs_create_bin_file(btf_kobj, attr);
+ } else {
+ struct kernfs_node *node;
+
+ node = __kernfs_create_file(btf_kobj->sd, attr->attr.name,
+ attr->attr.mode, GLOBAL_ROOT_UID,
+ GLOBAL_ROOT_GID, data_size,
+ &sysfs_btf_kernfs_ops, attr, NULL, NULL);
+ if (IS_ERR(node))
+ err = PTR_ERR(node);
+ }
+ if (err) {
+ pr_warn("failed to register [%s] BTF in sysfs: %d\n", name, err);
+ goto err_free;
+ }
+ return attr;
+
+err_free:
+ kfree(attr->attr.name);
+ kfree(attr);
+ return ERR_PTR(err);
+}
+
+#else
+struct bin_attribute *sysfs_btf_add(const char *name, void *data, size_t data_size)
+{
+ return NULL;
+}
+#endif
+
+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)
+static void sysfs_btf_update(struct bin_attribute *attr, void *data, size_t data_size)
+{
+ if (!attr)
+ return;
+ WRITE_ONCE(attr->private, data);
+ /* Publish data before its non-zero size makes it readable. */
+ smp_store_release(&attr->size, data_size);
+}
+#endif
+
#ifdef CONFIG_DEBUG_INFO_BTF_MODULES
+#if IS_ENABLED(CONFIG_SYSFS)
+static void sysfs_btf_remove(struct bin_attribute *attr)
+{
+ sysfs_remove_bin_file(btf_kobj, attr);
+ kfree(attr->attr.name);
+ kfree(attr);
+}
+#else
+static void sysfs_btf_remove(struct bin_attribute *attr)
+{
+}
+#endif
+
struct btf_module {
struct list_head list;
struct module *module;
struct btf *btf;
struct bin_attribute *sysfs_attr;
+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)
+ struct bin_attribute *sysfs_inline_attr;
+ void *btf_inline_data;
+#endif
int flags;
};
@@ -8522,7 +9040,10 @@ static int btf_module_notify(struct notifier_block *nb, unsigned long op,
{
struct btf_module *btf_mod, *tmp;
struct module *mod = module;
+ struct bin_attribute *attr;
struct btf *btf;
+ void *inline_data = NULL, *relocated_inline_data = NULL;
+ unsigned int inline_data_size = 0;
int err = 0;
if (mod->btf_data_size == 0 ||
@@ -8532,13 +9053,20 @@ static int btf_module_notify(struct notifier_block *nb, unsigned long op,
switch (op) {
case MODULE_STATE_COMING:
+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)
+ inline_data = mod->btf_inline_data;
+ inline_data_size = mod->btf_inline_data_size;
+#endif
btf_mod = kzalloc_obj(*btf_mod);
if (!btf_mod) {
err = -ENOMEM;
goto out;
}
btf = btf_parse_module(mod->name, mod->btf_data, mod->btf_data_size,
- mod->btf_base_data, mod->btf_base_data_size);
+ mod->btf_base_data, mod->btf_base_data_size,
+ inline_data, inline_data_size,
+ strcmp(mod->name, "btf_vmlinux_inline") == 0,
+ &relocated_inline_data);
if (IS_ERR(btf)) {
kfree(btf_mod);
if (!IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) {
@@ -8553,6 +9081,7 @@ static int btf_module_notify(struct notifier_block *nb, unsigned long op,
err = btf_alloc_id(btf);
if (err) {
btf_free(btf);
+ kvfree(relocated_inline_data);
kfree(btf_mod);
goto out;
}
@@ -8561,34 +9090,43 @@ static int btf_module_notify(struct notifier_block *nb, unsigned long op,
mutex_lock(&btf_module_mutex);
btf_mod->module = module;
btf_mod->btf = btf;
+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)
+ btf_mod->btf_inline_data = relocated_inline_data;
+#endif
list_add(&btf_mod->list, &btf_modules);
mutex_unlock(&btf_module_mutex);
- if (IS_ENABLED(CONFIG_SYSFS)) {
- struct bin_attribute *attr;
-
- attr = kzalloc_obj(*attr);
- if (!attr)
- goto out;
+ attr = sysfs_btf_add(btf->name, btf->data, btf->data_size);
+ if (IS_ERR(attr)) {
+ err = 0;
+ goto out;
+ }
+ btf_mod->sysfs_attr = attr;
- sysfs_bin_attr_init(attr);
- attr->attr.name = btf->name;
- attr->attr.mode = 0444;
- attr->size = btf->data_size;
- attr->private = btf->data;
- attr->read = sysfs_bin_attr_simple_read;
+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)
+ if (relocated_inline_data) {
+ char name[MODULE_NAME_LEN + sizeof(".inline")];
- err = sysfs_create_bin_file(btf_kobj, attr);
- if (err) {
- pr_warn("failed to register module [%s] BTF in sysfs: %d\n",
- mod->name, err);
- kfree(attr);
+ if (strcmp(mod->name, "btf_vmlinux_inline") == 0) {
+ if (vmlinux_inline_attr)
+ sysfs_btf_update(vmlinux_inline_attr,
+ relocated_inline_data, inline_data_size);
+ else
+ kvfree(relocated_inline_data);
+ btf_mod->btf_inline_data = NULL;
+ break;
+ }
+ snprintf(name, sizeof(name), "%s.inline", mod->name);
+ attr = sysfs_btf_add(name, relocated_inline_data, inline_data_size);
+ if (IS_ERR(attr)) {
err = 0;
+ kvfree(relocated_inline_data);
+ btf_mod->btf_inline_data = NULL;
goto out;
}
-
- btf_mod->sysfs_attr = attr;
+ btf_mod->sysfs_inline_attr = attr;
}
+#endif
break;
case MODULE_STATE_LIVE:
@@ -8617,10 +9155,14 @@ static int btf_module_notify(struct notifier_block *nb, unsigned long op,
btf_free_id(btf_mod->btf);
list_del(&btf_mod->list);
if (btf_mod->sysfs_attr)
- sysfs_remove_bin_file(btf_kobj, btf_mod->sysfs_attr);
+ sysfs_btf_remove(btf_mod->sysfs_attr);
+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)
+ if (btf_mod->sysfs_inline_attr)
+ sysfs_btf_remove(btf_mod->sysfs_inline_attr);
+ kvfree(btf_mod->btf_inline_data);
+#endif
purge_cand_cache(btf_mod->btf);
btf_put(btf_mod->btf);
- kfree(btf_mod->sysfs_attr);
kfree(btf_mod);
break;
}
@@ -8638,6 +9180,12 @@ static struct notifier_block btf_module_nb = {
static int __init btf_module_init(void)
{
register_module_notifier(&btf_module_nb);
+#if IS_MODULE(CONFIG_DEBUG_INFO_BTF_INLINE)
+ /* Attribute data will be filled in on-demand if vmlinux.inline is read. */
+ vmlinux_inline_attr = sysfs_btf_add("vmlinux.inline", NULL, 0);
+ if (IS_ERR(vmlinux_inline_attr))
+ vmlinux_inline_attr = NULL;
+#endif
return 0;
}
diff --git a/kernel/bpf/btf_vmlinux_inline.c b/kernel/bpf/btf_vmlinux_inline.c
new file mode 100644
index 0000000000000..13ed962ffceb1
--- /dev/null
+++ b/kernel/bpf/btf_vmlinux_inline.c
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026, Oracle and/or its affiliates. */
+/*
+ * Provide kernel BTF inline function information for use by BPF tools.
+ */
+#include <linux/btf.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+
+#if IS_BUILTIN(CONFIG_DEBUG_INFO_BTF_INLINE)
+extern char __start_BTF_inline[];
+extern char __stop_BTF_inline[];
+#endif
+
+static int __init btf_vmlinux_inline_init(void)
+{
+#if IS_BUILTIN(CONFIG_DEBUG_INFO_BTF_INLINE)
+ size_t data_size = __stop_BTF_inline - __start_BTF_inline;
+
+ if (data_size)
+ sysfs_btf_add("vmlinux.inline", __start_BTF_inline,
+ data_size);
+#endif
+ return 0;
+}
+subsys_initcall(btf_vmlinux_inline_init);
+
+#if IS_MODULE(CONFIG_DEBUG_INFO_BTF_INLINE)
+static void __exit btf_vmlinux_inline_fini(void)
+{
+}
+module_exit(btf_vmlinux_inline_fini);
+#endif
+
+MODULE_DESCRIPTION("BTF inline information for vmlinux");
+MODULE_LICENSE("GPL");
diff --git a/kernel/module/main.c b/kernel/module/main.c
index 46dd8d25a6058..7e06ef1131131 100644
--- a/kernel/module/main.c
+++ b/kernel/module/main.c
@@ -2712,6 +2712,10 @@ static int find_module_sections(struct module *mod, struct load_info *info)
mod->btf_base_data = any_section_objs(info, ".BTF.base", 1,
&mod->btf_base_data_size);
#endif
+#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)
+ mod->btf_inline_data = any_section_objs(info, ".BTF.inline", 1,
+ &mod->btf_inline_data_size);
+#endif
#ifdef CONFIG_JUMP_LABEL
mod->jump_entries = section_objs(info, "__jump_table",
sizeof(*mod->jump_entries),
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 1244dcac2294a..1b96d6acfdce9 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -425,6 +425,12 @@ config PAHOLE_HAS_LANG_EXCLUDE
otherwise it would emit malformed kernel and module binaries when
using DEBUG_INFO_BTF_MODULES.
+config PAHOLE_HAS_INLINE
+ def_bool PAHOLE_VERSION >= 131
+ help
+ Support for the "inline" BTF feature is available. It encodes
+ information about inline sites and how to retrieve their parameters.
+
config DEBUG_INFO_BTF_MODULES
bool "Generate BTF type information for kernel modules"
default y
@@ -432,6 +438,18 @@ config DEBUG_INFO_BTF_MODULES
help
Generate compact split BTF type information for kernel modules.
+config DEBUG_INFO_BTF_INLINE
+ tristate "Provide information about inline sites in BTF"
+ default n
+ depends on DEBUG_INFO_BTF && PAHOLE_HAS_INLINE && SYSFS
+ select DEBUG_INFO_BTF_MODULES if MODULES
+ help
+ Generate information about inline sites in .BTF.inline sections.
+ These sections contain split BTF relative to the kernel or module BTF
+ and are made available in /sys/kernel/btf with a ".inline" suffix.
+ The information describes inline locations and how to retrieve their
+ associated parameters.
+
config MODULE_ALLOW_BTF_MISMATCH
bool "Allow loading modules with non-matching BTF type info"
depends on DEBUG_INFO_BTF_MODULES
diff --git a/scripts/Makefile.btf b/scripts/Makefile.btf
index a1812985a61aa..d42dbc8d71992 100644
--- a/scripts/Makefile.btf
+++ b/scripts/Makefile.btf
@@ -22,7 +22,14 @@ endif
pahole-flags-$(CONFIG_PAHOLE_HAS_LANG_EXCLUDE) += --lang_exclude=rust
+btf-inline := $(CONFIG_DEBUG_INFO_BTF_INLINE)
+ifneq ($(btf-inline),)
+btf-inline-feat := inline
+pahole-flags-$(call test-ge, $(pahole-ver), 131) += --btf_features=$(btf-inline-feat)
+endif
+
export PAHOLE_FLAGS := $(pahole-flags-y)
+export BTF_INLINE := $(btf-inline)
resolve-btfids-flags-y :=
resolve-btfids-flags-$(CONFIG_WERROR) += --fatal_warnings
diff --git a/scripts/gen-btf.sh b/scripts/gen-btf.sh
index 8ca96eb10a69e..cd6588588fb8f 100755
--- a/scripts/gen-btf.sh
+++ b/scripts/gen-btf.sh
@@ -76,6 +76,7 @@ gen_btf_data()
${RESOLVE_BTFIDS} ${RESOLVE_BTFIDS_FLAGS} \
${BTF_BASE:+--btf_base ${BTF_BASE}} \
+ ${BTF_INLINE:+--inline} \
--btf ${btf1} "${ELF_FILE}"
}
@@ -83,14 +84,30 @@ gen_btf_o()
{
btf_data=${ELF_FILE}.btf.o
- # Create ${btf_data} which contains just .BTF section but no symbols. Add
+ # Create ${btf_data} which contains just BTF sections but no symbols. Add
# SHF_ALLOC because .BTF will be part of the vmlinux image. --strip-all
# deletes all symbols including __start_BTF and __stop_BTF, which will
# be redefined in the linker script.
echo "" | ${CC} ${CLANG_FLAGS} ${KBUILD_CPPFLAGS} ${KBUILD_CFLAGS} -fno-lto -c -x c -o ${btf_data} -
${OBJCOPY} --add-section .BTF=${ELF_FILE}.BTF \
--set-section-flags .BTF=alloc,readonly ${btf_data}
- ${OBJCOPY} --only-section=.BTF --strip-all ${btf_data}
+ ONLY_SEC="--only-section=.BTF"
+ btf_inline=${ELF_FILE}.BTF.inline
+ if [ "${BTF_INLINE}" = "m" ]; then
+ # vmlinux BTF is generated from a temporary ELF. Retain its
+ # vmlinux-relative inline BTF for btf_vmlinux_inline.ko.
+ if [ -f "${btf_inline}" ]; then
+ cp "${btf_inline}" "${objtree}/vmlinux.BTF.inline"
+ else
+ rm -f "${objtree}/vmlinux.BTF.inline"
+ fi
+ fi
+ if [ "${BTF_INLINE}" = "y" ] && [ -f "${btf_inline}" ]; then
+ ${OBJCOPY} --add-section .BTF.inline=${btf_inline} \
+ --set-section-flags .BTF.inline=alloc,readonly ${btf_data}
+ ONLY_SEC="${ONLY_SEC} --only-section=.BTF.inline"
+ fi
+ ${OBJCOPY} ${ONLY_SEC} --strip-all ${btf_data}
# Change e_type to ET_REL so that it can be used to link final vmlinux.
# GNU ld 2.35+ and lld do not allow an ET_EXEC input.
@@ -111,6 +128,17 @@ embed_btf_data()
if [ -f "${btf_base}" ]; then
${OBJCOPY} --add-section .BTF.base=${btf_base} ${ELF_FILE}
fi
+ btf_inline=${ELF_FILE}.BTF.inline
+ case "${ELF_FILE}" in
+ */btf_vmlinux_inline.ko)
+ # With CONFIG_DEBUG_INFO_BTF_INLINE=m, deliver vmlinux
+ # .BTF.inline via module
+ btf_inline=${BTF_BASE}.BTF.inline
+ ;;
+ esac
+ if [ -n "${BTF_INLINE}" ] && [ -f "${btf_inline}" ]; then
+ ${OBJCOPY} --add-section .BTF.inline=${btf_inline} ${ELF_FILE}
+ fi
btf_ids="${ELF_FILE}.BTF_ids"
if [ -f "${btf_ids}" ]; then
${RESOLVE_BTFIDS} --patch_btfids ${btf_ids} ${ELF_FILE}
@@ -121,6 +149,7 @@ cleanup()
{
rm -f "${ELF_FILE}.BTF.1"
rm -f "${ELF_FILE}.BTF"
+ rm -f "${ELF_FILE}.BTF.inline"
if [ "${BTFGEN_MODE}" = "module" ]; then
rm -f "${ELF_FILE}.BTF.base"
rm -f "${ELF_FILE}.BTF_ids"
diff --git a/tools/bpf/bpftool/Documentation/bpftool-btf.rst b/tools/bpf/bpftool/Documentation/bpftool-btf.rst
index cf75a7fa2d6bc..60c46748b8663 100644
--- a/tools/bpf/bpftool/Documentation/bpftool-btf.rst
+++ b/tools/bpf/bpftool/Documentation/bpftool-btf.rst
@@ -16,7 +16,7 @@ SYNOPSIS
**bpftool** [*OPTIONS*] **btf** *COMMAND*
-*OPTIONS* := { |COMMON_OPTIONS| | { **-B** | **--base-btf** } }
+*OPTIONS* := { |COMMON_OPTIONS| | { **-B** | **--base-btf** [ **-B** | **--base-btf** ] } }
*COMMANDS* := { **dump** | **help** }
@@ -87,7 +87,10 @@ OPTIONS
objects for kernel modules. To avoid duplicating all kernel symbols
required by modules, BTF objects for modules are "split", they are
built incrementally on top of the kernel (vmlinux) BTF object. So the
- base BTF reference should usually point to the kernel BTF.
+ base BTF reference should usually point to the kernel BTF. Multiple
+ base BTF objects can be passed, where the first is assumed to be the
+ root BTF, followed by split BTF based upon it, followed by split
+ BTF based upon the first split BTF and so on.
When the main BTF object to process (for example, the module BTF to
dump) is passed as a *FILE*, bpftool attempts to autodetect the path
diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c
index bca0a3982f09b..4d8991c99b484 100644
--- a/tools/bpf/bpftool/btf.c
+++ b/tools/bpf/bpftool/btf.c
@@ -51,6 +51,9 @@ static const char * const btf_kind_str[NR_BTF_KINDS] = {
[BTF_KIND_DECL_TAG] = "DECL_TAG",
[BTF_KIND_TYPE_TAG] = "TYPE_TAG",
[BTF_KIND_ENUM64] = "ENUM64",
+ [BTF_KIND_LOC_PARAM] = "LOC_PARAM",
+ [BTF_KIND_LOC_PROTO] = "LOC_PROTO",
+ [BTF_KIND_LOCSEC] = "LOCSEC",
};
struct sort_datum {
@@ -415,6 +418,88 @@ static int dump_btf_type(const struct btf *btf, __u32 id,
}
break;
}
+ case BTF_KIND_LOC_PARAM: {
+ const struct btf_loc_param *p = btf_loc_param(t);
+ __u32 *v = (__u32 *)(p + 1);
+ __u32 i, vlen = btf_vlen(t);
+
+ if (json_output) {
+ jsonw_uint_field(w, "size", t->size);
+ jsonw_uint_field(w, "flags", p->flags);
+ jsonw_uint_field(w, "vlen", vlen);
+ jsonw_name(w, "values");
+ jsonw_start_array(w);
+ } else {
+ printf(" size=%u flags=0x%x vlen=%u", t->size, p->flags, vlen);
+ }
+ for (i = 0; i < vlen; i++, v++) {
+ if (json_output) {
+ jsonw_start_object(w);
+ jsonw_uint_field(w, "value", *v);
+ jsonw_end_object(w);
+ } else {
+ printf("\n\t value=%u", *v);
+ }
+ }
+ if (json_output)
+ jsonw_end_array(w);
+ break;
+ }
+ case BTF_KIND_LOC_PROTO: {
+ __u32 *params = btf_loc_proto_params(t);
+ __u16 vlen = btf_vlen(t);
+ int i;
+
+ if (json_output) {
+ jsonw_uint_field(w, "vlen", vlen);
+ jsonw_name(w, "params");
+ jsonw_start_array(w);
+ } else {
+ printf(" vlen=%u", vlen);
+ }
+
+ for (i = 0; i < vlen; i++, params++) {
+ if (json_output) {
+ jsonw_start_object(w);
+ jsonw_uint_field(w, "type_id", *params);
+ jsonw_end_object(w);
+ } else {
+ printf("\n\t type_id=%u", *params);
+ }
+ }
+ if (json_output)
+ jsonw_end_array(w);
+ break;
+ }
+
+ case BTF_KIND_LOCSEC: {
+ struct btf_loc *locs = btf_locsec_locs(t);
+ __u32 i, vlen = btf_vlen(t);
+
+ if (json_output) {
+ jsonw_uint_field(w, "vlen", vlen);
+ jsonw_name(w, "locs");
+ jsonw_start_array(w);
+ } else {
+ printf(" vlen=%u", vlen);
+ }
+
+ for (i = 0; i < vlen; i++, locs++) {
+ if (json_output) {
+ jsonw_start_object(w);
+ jsonw_uint_field(w, "func_type_id", locs->func);
+ jsonw_uint_field(w, "loc_proto_type_id", locs->loc_proto);
+ jsonw_uint_field(w, "offset", locs->offset);
+ jsonw_end_object(w);
+ } else {
+ printf("\n\t func_type_id=%u loc_proto_type_id=%u offset=%u",
+ locs->func, locs->loc_proto, locs->offset);
+ }
+ }
+ if (json_output)
+ jsonw_end_array(w);
+ break;
+ }
default:
break;
}
diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c
index 5ababd8f7d0a0..9329f140b700f 100644
--- a/tools/bpf/bpftool/main.c
+++ b/tools/bpf/bpftool/main.c
@@ -515,7 +515,8 @@ int main(int argc, char **argv)
verifier_logs = true;
break;
case 'B':
- base_btf = btf__parse(optarg, NULL);
+ /* handle multi-split BTF */
+ base_btf = btf__parse_split(optarg, base_btf);
if (!base_btf) {
p_err("failed to parse base BTF at '%s': %d\n",
optarg, -errno);
diff --git a/tools/bpf/resolve_btfids/main.c b/tools/bpf/resolve_btfids/main.c
index 37d7e7224207d..20b228c4d6789 100644
--- a/tools/bpf/resolve_btfids/main.c
+++ b/tools/bpf/resolve_btfids/main.c
@@ -142,7 +142,9 @@ struct object {
struct btf *btf;
struct btf *base_btf;
+ struct btf *inline_btf;
bool distill_base;
+ bool extract_inline;
struct {
int fd;
@@ -154,6 +156,10 @@ struct object {
size_t strtabidx;
unsigned long idlist_addr;
int encoding;
+ const char **func_symbols;
+ u32 func_symbols_cnt;
+ u32 func_symbols_cap;
+ bool func_symbols_collected;
} efile;
struct rb_root sets;
@@ -572,6 +578,58 @@ static const char *find_name_by_addr(struct object *obj, Elf64_Addr addr)
return res ? res->name : NULL;
}
+static int cmp_func_symbol(const void *a, const void *b)
+{
+ const char * const *name = a;
+ const char * const *other = b;
+
+ return strcmp(*name, *other);
+}
+
+static int collect_func_symbols(struct object *obj)
+{
+ Elf_Scn *scn;
+ GElf_Shdr sh;
+ int n, i;
+
+ if (obj->efile.symbols_shndx == -1)
+ return 0;
+
+ scn = elf_getscn(obj->efile.elf, obj->efile.symbols_shndx);
+ if (!scn || gelf_getshdr(scn, &sh) != &sh)
+ return -EINVAL;
+ n = sh.sh_size / sh.sh_entsize;
+
+ for (i = 0; i < n; i++) {
+ GElf_Sym sym;
+ const char *name;
+
+ if (!gelf_getsym(obj->efile.symbols, i, &sym))
+ return -EINVAL;
+ if (GELF_ST_TYPE(sym.st_info) != STT_FUNC ||
+ sym.st_shndx == SHN_UNDEF || !sym.st_name)
+ continue;
+ name = elf_strptr(obj->efile.elf, obj->efile.strtabidx, sym.st_name);
+ if (!name)
+ return -EINVAL;
+ if (ensure_mem(&obj->efile.func_symbols, &obj->efile.func_symbols_cap,
+ obj->efile.func_symbols_cnt + 1))
+ return -ENOMEM;
+ obj->efile.func_symbols[obj->efile.func_symbols_cnt++] = name;
+ }
+
+ qsort(obj->efile.func_symbols, obj->efile.func_symbols_cnt,
+ sizeof(*obj->efile.func_symbols), cmp_func_symbol);
+ obj->efile.func_symbols_collected = true;
+ return 0;
+}
+
+static bool has_func_symbol(const struct object *obj, const char *name)
+{
+ return bsearch(&name, obj->efile.func_symbols, obj->efile.func_symbols_cnt,
+ sizeof(*obj->efile.func_symbols), cmp_func_symbol) != NULL;
+}
+
static int symbols_collect(struct object *obj)
{
Elf_Scn *scn = NULL;
@@ -1514,14 +1572,193 @@ static int btf2btf(struct object *obj)
* Sort types by name in ascending order resulting in all
* anonymous types being placed before named types.
*/
+struct btf_name_sort {
+ struct btf *btf;
+ bool extract_inline;
+ bool *transfer_funcs;
+ bool *transfer_protos;
+};
+
+static int btf_inline_kind_order(const struct btf_type *t, bool transfer_func,
+ bool transfer_proto)
+{
+ if (transfer_func)
+ return 1;
+ if (transfer_proto)
+ return 2;
+ if (btf_is_loc_param(t))
+ return 3;
+ if (btf_is_loc_proto(t))
+ return 4;
+ if (btf_is_locsec(t))
+ return 5;
+ return 0;
+}
+
+enum inline_ref_kind {
+ INLINE_REF_FUNC,
+ INLINE_REF_FUNC_PROTO,
+};
+
+static void mark_inline_ref(struct btf *btf, bool *refs, int start_id,
+ int type_cnt, __u32 type_id, enum inline_ref_kind ref_kind)
+{
+ const struct btf_type *t;
+
+ if (type_id < start_id || type_id >= type_cnt)
+ return;
+ t = btf__type_by_id(btf, type_id);
+ if ((ref_kind == INLINE_REF_FUNC && btf_is_func(t)) ||
+ (ref_kind == INLINE_REF_FUNC_PROTO && btf_is_func_proto(t)))
+ refs[type_id] = true;
+}
+
+static void mark_inline_type_refs(struct btf *btf, bool *refs, int start_id,
+ int type_cnt, const struct btf_type *t,
+ enum inline_ref_kind ref_kind)
+{
+ /*
+ * These are the only BTF kinds that can directly reference a FUNC or
+ * FUNC_PROTO (aside from LOCSEC).
+ */
+ switch (btf_kind(t)) {
+ case BTF_KIND_CONST:
+ case BTF_KIND_VOLATILE:
+ case BTF_KIND_RESTRICT:
+ case BTF_KIND_PTR:
+ case BTF_KIND_TYPEDEF:
+ case BTF_KIND_FUNC:
+ case BTF_KIND_VAR:
+ case BTF_KIND_DECL_TAG:
+ case BTF_KIND_TYPE_TAG:
+ mark_inline_ref(btf, refs, start_id, type_cnt, t->type, ref_kind);
+ break;
+ default:
+ return;
+ }
+}
+
+/* A FUNC can move to inline BTF only when LOCSEC is its sole BTF user. */
+static int find_inline_funcs(struct object *obj, bool **transfer_funcsp,
+ bool **transfer_protosp)
+{
+ bool *locsec_refs, *other_refs, *proto_candidates, *proto_base_refs;
+ int i, type_cnt, start_id = 0;
+ struct btf *btf = obj->btf;
+
+ type_cnt = btf__type_cnt(btf);
+ *transfer_funcsp = NULL;
+ *transfer_protosp = NULL;
+ if (btf__base_btf(btf))
+ start_id = btf__type_cnt(btf__base_btf(btf));
+
+ locsec_refs = calloc(type_cnt, sizeof(*locsec_refs));
+ other_refs = calloc(type_cnt, sizeof(*other_refs));
+ proto_candidates = calloc(type_cnt, sizeof(*proto_candidates));
+ proto_base_refs = calloc(type_cnt, sizeof(*proto_base_refs));
+ if (!locsec_refs || !other_refs || !proto_candidates || !proto_base_refs)
+ goto err;
+
+ /* First identify locsec FUNC references */
+ for (i = start_id; i < type_cnt; i++) {
+ const struct btf_type *t = btf__type_by_id(btf, i);
+ const struct btf_loc *loc;
+ int j;
+
+ if (!btf_is_locsec(t))
+ continue;
+ loc = btf_locsec_locs(t);
+ for (j = 0; j < btf_vlen(t); j++, loc++) {
+ if (loc->func >= start_id && loc->func < type_cnt &&
+ btf_is_func(btf__type_by_id(btf, loc->func)))
+ locsec_refs[loc->func] = true;
+ }
+ }
+
+ /*
+ * Then mark any additional references to them; references outside
+ * of LOCSEC make the FUNC ineligible for transfer.
+ */
+ for (i = start_id; i < type_cnt; i++) {
+ const struct btf_type *t = btf__type_by_id(btf, i);
+
+ if (btf_is_locsec(t))
+ continue;
+ mark_inline_type_refs(btf, other_refs, start_id, type_cnt, t,
+ INLINE_REF_FUNC);
+ }
+
+ for (i = start_id; i < type_cnt; i++) {
+ const struct btf_type *t = btf__type_by_id(btf, i);
+ const char *name;
+
+ /* If there are no locsec references, or we find additional
+ * non-locsec references, skip as ineligible.
+ */
+ if (!locsec_refs[i] || other_refs[i])
+ continue;
+ name = btf__name_by_offset(btf, t->name_off);
+ /* Keep out-of-line functions and BTF ID users in the main BTF. */
+ if (!obj->efile.func_symbols_collected || has_func_symbol(obj, name) ||
+ btf_id__find(&obj->funcs, name))
+ locsec_refs[i] = false;
+ }
+
+ for (i = start_id; i < type_cnt; i++) {
+ const struct btf_type *t;
+
+ if (!locsec_refs[i])
+ continue;
+ t = btf__type_by_id(btf, i);
+ proto_candidates[t->type] = true;
+ }
+
+ /* Mark every FUNC_PROTO directly needed by retained base BTF types. */
+ for (i = start_id; i < type_cnt; i++) {
+ const struct btf_type *t = btf__type_by_id(btf, i);
+
+ if (locsec_refs[i] || proto_candidates[i] ||
+ btf_is_loc_param(t) || btf_is_loc_proto(t) ||
+ btf_is_locsec(t))
+ continue;
+ mark_inline_type_refs(btf, proto_base_refs, start_id, type_cnt, t,
+ INLINE_REF_FUNC_PROTO);
+ }
+ for (i = start_id; i < type_cnt; i++)
+ proto_candidates[i] &= !proto_base_refs[i];
+
+ free(other_refs);
+ *transfer_funcsp = locsec_refs;
+ *transfer_protosp = proto_candidates;
+ free(proto_base_refs);
+ return 0;
+err:
+ free(locsec_refs);
+ free(other_refs);
+ free(proto_candidates);
+ free(proto_base_refs);
+ return -ENOMEM;
+}
+
static int cmp_type_names(const void *a, const void *b, void *priv)
{
- struct btf *btf = (struct btf *)priv;
+ struct btf_name_sort *sort = priv;
+ struct btf *btf = sort->btf;
const struct btf_type *ta = btf__type_by_id(btf, *(__u32 *)a);
const struct btf_type *tb = btf__type_by_id(btf, *(__u32 *)b);
const char *na, *nb;
+ int inline_order;
int r;
+ if (sort->extract_inline) {
+ inline_order = btf_inline_kind_order(ta, sort->transfer_funcs[*(__u32 *)a],
+ sort->transfer_protos[*(__u32 *)a]) -
+ btf_inline_kind_order(tb, sort->transfer_funcs[*(__u32 *)b],
+ sort->transfer_protos[*(__u32 *)b]);
+ if (inline_order)
+ return inline_order;
+ }
+
na = btf__str_by_offset(btf, ta->name_off);
nb = btf__str_by_offset(btf, tb->name_off);
r = strcmp(na, nb);
@@ -1532,8 +1769,60 @@ static int cmp_type_names(const void *a, const void *b, void *priv)
return *(__u32 *)a < *(__u32 *)b ? -1 : 1;
}
-static int sort_btf_by_name(struct btf *btf)
+struct loc_sort {
+ const __u32 *id_map;
+ __u32 start_id;
+};
+
+static __u32 projected_type_id(const struct loc_sort *sort, __u32 id)
+{
+ if (id < sort->start_id)
+ return id;
+ return sort->id_map[id - sort->start_id] & ~BTF_PERMUTE_ID_TRANSFER;
+}
+
+static int cmp_loc(const void *a, const void *b, void *priv)
{
+ const struct loc_sort *sort = priv;
+ const struct btf_loc *la = a, *lb = b;
+ __u32 afunc = projected_type_id(sort, la->func);
+ __u32 bfunc = projected_type_id(sort, lb->func);
+ __u32 aproto = projected_type_id(sort, la->loc_proto);
+ __u32 bproto = projected_type_id(sort, lb->loc_proto);
+
+ if (afunc != bfunc)
+ return afunc < bfunc ? -1 : 1;
+ if (la->offset != lb->offset)
+ return la->offset < lb->offset ? -1 : 1;
+ if (aproto != bproto)
+ return aproto < bproto ? -1 : 1;
+ return 0;
+}
+
+static void sort_locsecs(struct btf *btf, const __u32 *id_map, __u32 start_id,
+ int nr_types)
+{
+ struct loc_sort sort = { .id_map = id_map, .start_id = start_id };
+ int i;
+
+ for (i = 0; i < nr_types; i++) {
+ struct btf_type *t = (struct btf_type *)btf__type_by_id(btf, start_id + i);
+
+ if (btf_is_locsec(t))
+ qsort_r(btf_locsec_locs(t), btf_vlen(t), sizeof(struct btf_loc),
+ cmp_loc, &sort);
+ }
+}
+
+static int sort_btf_by_name(struct object *obj)
+{
+ struct btf *btf = obj->btf;
+ bool *transfer_funcs = NULL, *transfer_protos = NULL;
+ struct btf_name_sort sort = {
+ .btf = btf,
+ .extract_inline = obj->extract_inline,
+ };
+ LIBBPF_OPTS(btf_permute_opts, opts);
__u32 *permute_ids = NULL, *id_map = NULL;
int nr_types, i, err = 0;
__u32 start_id = 0, id;
@@ -1541,6 +1830,13 @@ static int sort_btf_by_name(struct btf *btf)
if (btf__base_btf(btf))
start_id = btf__type_cnt(btf__base_btf(btf));
nr_types = btf__type_cnt(btf) - start_id;
+ if (obj->extract_inline) {
+ err = find_inline_funcs(obj, &transfer_funcs, &transfer_protos);
+ if (err)
+ goto out;
+ sort.transfer_funcs = transfer_funcs;
+ sort.transfer_protos = transfer_protos;
+ }
permute_ids = calloc(nr_types, sizeof(*permute_ids));
if (!permute_ids) {
@@ -1557,21 +1853,37 @@ static int sort_btf_by_name(struct btf *btf)
for (i = 0, id = start_id; i < nr_types; i++, id++)
permute_ids[i] = id;
- qsort_r(permute_ids, nr_types, sizeof(*permute_ids), cmp_type_names,
- btf);
+ qsort_r(permute_ids, nr_types, sizeof(*permute_ids), cmp_type_names, &sort);
for (i = 0; i < nr_types; i++) {
id = permute_ids[i] - start_id;
id_map[id] = i + start_id;
+ if (obj->extract_inline &&
+ btf_inline_kind_order(btf__type_by_id(btf, permute_ids[i]),
+ transfer_funcs[permute_ids[i]],
+ transfer_protos[permute_ids[i]]))
+ id_map[id] |= BTF_PERMUTE_ID_TRANSFER;
}
- err = btf__permute(btf, id_map, nr_types, NULL);
+ /*
+ * Key LOCSEC records by their post-permutation function ID and offset
+ * since the tracer pattern will be to look up FUNC by name, then
+ * find LOCSEC records for that function.
+ */
+ sort_locsecs(btf, id_map, start_id, nr_types);
+
+ if (obj->extract_inline)
+ opts.transfer_btf = &obj->inline_btf;
+ err = btf__permute(btf, id_map, nr_types,
+ obj->extract_inline ? &opts : NULL);
if (err)
pr_err("FAILED: btf permute: %s\n", strerror(-err));
out:
free(permute_ids);
free(id_map);
+ free(transfer_funcs);
+ free(transfer_protos);
return err;
}
@@ -1599,7 +1911,7 @@ static int finalize_btf(struct object *obj)
obj->btf = btf;
}
- err = sort_btf_by_name(obj->btf);
+ err = sort_btf_by_name(obj);
if (err) {
pr_err("FAILED to sort BTF: %s\n", strerror(errno));
goto out_err;
@@ -1773,6 +2085,8 @@ int main(int argc, const char **argv)
"turn warnings into errors"),
OPT_BOOLEAN(0, "distill_base", &obj.distill_base,
"distill --btf_base and emit .BTF.base section data"),
+ OPT_BOOLEAN(0, "inline", &obj.extract_inline,
+ "extract location BTF into a .BTF.inline file"),
OPT_STRING(0, "patch_btfids", &btfids_path, "file",
"path to .BTF_ids section data blob to patch into ELF file"),
OPT_END()
@@ -1791,6 +2105,8 @@ int main(int argc, const char **argv)
if (elf_collect(&obj))
goto out;
+ if (obj.extract_inline && collect_func_symbols(&obj))
+ goto out;
/*
* We did not find .BTF_ids section or symbols section,
@@ -1841,11 +2157,18 @@ int main(int argc, const char **argv)
if (err)
goto out;
}
+ if (obj.inline_btf) {
+ err = make_out_path(out_path, sizeof(out_path), obj.path, BTF_ELF_SEC ".inline");
+ err = err ?: dump_raw_btf(obj.inline_btf, out_path);
+ if (err)
+ goto out;
+ }
if (!(fatal_warnings && warnings))
err = 0;
out:
btf__free(obj.base_btf);
+ btf__free(obj.inline_btf);
btf__free(obj.btf);
btf_id__free_all(&obj.structs);
btf_id__free_all(&obj.unions);
@@ -1853,6 +2176,7 @@ int main(int argc, const char **argv)
btf_id__free_all(&obj.funcs);
btf_id__free_all(&obj.sets);
free(obj.addr_syms);
+ free(obj.efile.func_symbols);
if (obj.efile.elf) {
elf_end(obj.efile.elf);
close(obj.efile.fd);
diff --git a/tools/include/uapi/linux/btf.h b/tools/include/uapi/linux/btf.h
index 618167cab4e61..6062c99580345 100644
--- a/tools/include/uapi/linux/btf.h
+++ b/tools/include/uapi/linux/btf.h
@@ -92,7 +92,9 @@ enum {
BTF_KIND_DECL_TAG = 17, /* Decl Tag */
BTF_KIND_TYPE_TAG = 18, /* Type Tag */
BTF_KIND_ENUM64 = 19, /* Enumeration up to 64-bit values */
-
+ BTF_KIND_LOC_PARAM = 20, /* Location parameter information */
+ BTF_KIND_LOC_PROTO = 21, /* Location prototype for site */
+ BTF_KIND_LOCSEC = 22, /* Location section */
NR_BTF_KINDS,
BTF_KIND_MAX = NR_BTF_KINDS - 1,
};
@@ -212,4 +214,65 @@ struct btf_enum64 {
__u32 val_hi32;
};
+/*
+ * BTF_KIND_LOC_PARAM is followed by a single "struct btf_loc_param"
+ * that contains flags specifying the contents of the vlen-specified
+ * number of 4-byte values that follow.
+ */
+struct btf_loc_param {
+ __u32 flags;
+};
+
+/*
+ * The combination of size, vlen and flags gives us the means to interpret
+ * the following vlen-specified set of 4-byte values:
+ *
+ * - a BTF_LOC_PARAM_CONST is a constant value; combination
+ * of size, vlen and _SIGNED flag determines it. If the value requires
+ * 64 bits it is stored in {lo,hi} order.
+ * - a BTF_LOC_PARAM_ADDR is an address that will be normalized with
+ * respect to kernel base address.
+ * - a BTF_LOC_PARAM_REG with vlen 1 is a simple register number;
+ * with vlen 2 it is a multi-register parameter.
+ * - a _REG | DEREF with vlen 1 dereferences the value in the register
+ * number specified.
+ * - a REG | DEREF | OFFSET with vlen specifies the register value in
+ * the first 4-byte value and the offset in the remainder.
+ * - binary logical operators operate on a combination of register
+ * number and constant value, aside from _NOT which operates on
+ * a register
+ */
+enum btf_loc_param_flags {
+ BTF_LOC_PARAM_SIGNED = 0x1,
+ BTF_LOC_PARAM_CONST = 0x2,
+ BTF_LOC_PARAM_ADDR = 0x4,
+ BTF_LOC_PARAM_REG = 0x8,
+ BTF_LOC_PARAM_DEREF = 0x10,
+ BTF_LOC_PARAM_OFFSET = 0x20,
+};
+
+/*
+ * BTF_KIND_LOC_PROTO specifies location prototypes; i.e. how locations relate
+ * to parameters; a struct btf_type of BTF_KIND_LOC_PROTO is followed by a
+ * a vlen-specified number of __u32 BTF type ids which specify the associated
+ * BTF_KIND_LOC_PARAM for each function parameter associated with the
+ * location. The type should either be 0 (no location info) or point at
+ * a BTF_KIND_LOC_PARAM.
+ */
+
+/*
+ * BTF_KIND_LOCSEC consists of vlen-specified number of "struct btf_loc"
+ * containing location site-specific information;
+ *
+ * - function (func)
+ * - location prototype type id (loc_proto)
+ * - address offset (offset) relative to kernel base address
+ */
+
+struct btf_loc {
+ __u32 func;
+ __u32 loc_proto;
+ __u32 offset;
+};
+
#endif /* _UAPI__LINUX_BTF_H__ */
diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index c783359977b46..eea325ccf768e 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -57,6 +57,9 @@ static struct btf_layout layouts[NR_BTF_KINDS] = {
[BTF_KIND_DECL_TAG] = { sizeof(struct btf_decl_tag), 0, 0 },
[BTF_KIND_TYPE_TAG] = { 0, 0, 0 },
[BTF_KIND_ENUM64] = { 0, sizeof(struct btf_enum64), 0 },
+[BTF_KIND_LOC_PARAM] = { sizeof(struct btf_loc_param), sizeof(__u32), 0 },
+[BTF_KIND_LOC_PROTO] = { 0, sizeof(__u32), 0 },
+[BTF_KIND_LOCSEC] = { 0, sizeof(struct btf_loc), 0 },
};
struct btf {
@@ -486,6 +489,12 @@ static int btf_type_size(const struct btf *btf, const struct btf_type *t)
return base_size + vlen * sizeof(struct btf_var_secinfo);
case BTF_KIND_DECL_TAG:
return base_size + sizeof(struct btf_decl_tag);
+ case BTF_KIND_LOC_PARAM:
+ return base_size + sizeof(struct btf_loc_param) + vlen * sizeof(__u32);
+ case BTF_KIND_LOC_PROTO:
+ return base_size + vlen * sizeof(__u32);
+ case BTF_KIND_LOCSEC:
+ return base_size + vlen * sizeof(struct btf_loc);
default:
return btf_type_size_unknown(btf, t);
}
@@ -501,12 +510,15 @@ static void btf_bswap_type_base(struct btf_type *t)
static int btf_bswap_type_rest(struct btf_type *t)
{
struct btf_var_secinfo *v;
+ struct btf_loc_param *lp;
struct btf_enum64 *e64;
struct btf_member *m;
struct btf_array *a;
struct btf_param *p;
struct btf_enum *e;
+ struct btf_loc *l;
__u32 vlen = btf_vlen(t);
+ __u32 *d;
int i;
switch (btf_kind(t)) {
@@ -569,6 +581,23 @@ static int btf_bswap_type_rest(struct btf_type *t)
case BTF_KIND_DECL_TAG:
btf_decl_tag(t)->component_idx = bswap_32(btf_decl_tag(t)->component_idx);
return 0;
+ case BTF_KIND_LOC_PARAM:
+ lp = btf_loc_param(t);
+ lp->flags = bswap_32(lp->flags);
+ for (i = 0, d = (__u32 *)(lp + 1); i < vlen; i++, d++)
+ *d = bswap_32(*d);
+ return 0;
+ case BTF_KIND_LOC_PROTO:
+ for (i = 0, d = btf_loc_proto_params(t); i < vlen; i++, d++)
+ *d = bswap_32(*d);
+ return 0;
+ case BTF_KIND_LOCSEC:
+ for (i = 0, l = btf_locsec_locs(t); i < vlen; i++, l++) {
+ l->func = bswap_32(l->func);
+ l->loc_proto = bswap_32(l->loc_proto);
+ l->offset = bswap_32(l->offset);
+ }
+ return 0;
default:
pr_debug("Unsupported BTF_KIND:%u\n", btf_kind(t));
return -EINVAL;
@@ -745,6 +774,33 @@ static int btf_validate_type(const struct btf *btf, const struct btf_type *t, __
}
break;
}
+ case BTF_KIND_LOC_PARAM:
+ break;
+ case BTF_KIND_LOC_PROTO: {
+ __u32 *p = btf_loc_proto_params(t);
+
+ n = btf_vlen(t);
+ for (i = 0; i < n; i++, p++) {
+ err = btf_validate_id(btf, *p, id);
+ if (err)
+ return err;
+ }
+ break;
+ }
+ case BTF_KIND_LOCSEC: {
+ const struct btf_loc *l = btf_locsec_locs(t);
+
+ n = btf_vlen(t);
+ for (i = 0; i < n; i++, l++) {
+ if (!err)
+ err = btf_validate_id(btf, l->func, id);
+ if (!err)
+ err = btf_validate_id(btf, l->loc_proto, id);
+ if (err)
+ return err;
+ }
+ break;
+ }
default:
/* Kind may be represented in kind layout information. */
if (btf_type_size_unknown(btf, t) < 0) {
@@ -2091,16 +2147,9 @@ int btf__find_str(struct btf *btf, const char *s)
* - > 0 offset into string section, on success;
* - < 0, on error.
*/
-int btf__add_str(struct btf *btf, const char *s)
+static int btf_add_local_str(struct btf *btf, const char *s)
{
- int off;
- int err;
-
- if (btf->base_btf) {
- off = btf__find_str(btf->base_btf, s);
- if (off != -ENOENT)
- return off;
- }
+ int off, err;
err = btf_ensure_modifiable(btf);
if (err)
@@ -2111,10 +2160,24 @@ int btf__add_str(struct btf *btf, const char *s)
return libbpf_err(off);
btf->hdr.str_len = strset__data_size(btf->strs_set);
+ btf->strs_deduped = false;
return btf->start_str_off + off;
}
+int btf__add_str(struct btf *btf, const char *s)
+{
+ int off;
+
+ if (btf->base_btf) {
+ off = btf__find_str(btf->base_btf, s);
+ if (off != -ENOENT)
+ return off;
+ }
+
+ return btf_add_local_str(btf, s);
+}
+
static void *btf_add_type_mem(struct btf *btf, size_t add_sz)
{
return libbpf_add_mem(&btf->types_data, &btf->types_data_cap, 1,
@@ -2162,6 +2225,7 @@ struct btf_pipe {
const struct btf *src;
struct btf *dst;
struct hashmap *str_off_map; /* map string offsets from src to dst */
+ bool force_local_strs;
};
static int btf_rewrite_str(struct btf_pipe *p, __u32 *str_off)
@@ -2178,7 +2242,10 @@ static int btf_rewrite_str(struct btf_pipe *p, __u32 *str_off)
return 0;
}
- off = btf__add_str(p->dst, btf__str_by_offset(p->src, *str_off));
+ if (p->force_local_strs)
+ off = btf_add_local_str(p->dst, btf__str_by_offset(p->src, *str_off));
+ else
+ off = btf__add_str(p->dst, btf__str_by_offset(p->src, *str_off));
if (off < 0)
return off;
@@ -3344,6 +3411,208 @@ int btf__add_decl_attr(struct btf *btf, const char *value, int ref_type_id,
return btf_add_decl_tag(btf, value, ref_type_id, component_idx, 1);
}
+/*
+ * Append new BTF_KIND_LOC_PARAM with specified size and flags. Values are
+ * added via btf__add_loc_param_value().
+ *
+ * Returns:
+ * - >0, type ID of newly added BTF type;
+ * - <0, on error.
+ */
+int btf__add_loc_param(struct btf *btf, __u32 size, __u32 flags)
+{
+ struct btf_loc_param *p;
+ struct btf_type *t;
+ int sz, err;
+
+ err = btf_ensure_modifiable(btf);
+ if (err)
+ return libbpf_err(err);
+
+ sz = sizeof(struct btf_type) + sizeof(*p);
+ t = btf_add_type_mem(btf, sz);
+ if (!t)
+ return libbpf_err(-ENOMEM);
+
+ t->name_off = 0;
+ t->info = btf_type_info(BTF_KIND_LOC_PARAM, 0, 0);
+ t->size = size;
+
+ p = btf_loc_param(t);
+ p->flags = flags;
+
+ return btf_commit_type(btf, sz);
+}
+
+int btf__add_loc_param_value(struct btf *btf, __u32 value)
+{
+ struct btf_type *t;
+ int sz, err;
+ __u32 *v;
+
+ /* last type should be BTF_KIND_LOC_PARAM */
+ if (btf->nr_types == 0)
+ return libbpf_err(-EINVAL);
+ t = btf_last_type(btf);
+ if (!btf_is_loc_param(t))
+ return libbpf_err(-EINVAL);
+
+ /* decompose and invalidate raw data */
+ err = btf_ensure_modifiable(btf);
+ if (err)
+ return libbpf_err(err);
+
+ sz = sizeof(value);
+ v = btf_add_type_mem(btf, sz);
+ if (!v)
+ return libbpf_err(-ENOMEM);
+ *v = value;
+
+ /* update parent type's vlen */
+ t = btf_last_type(btf);
+ err = btf_type_inc_vlen(t);
+ if (err)
+ return libbpf_err(err);
+
+ btf_hdr_update_type_len(btf, btf->hdr.type_len + sz);
+ return 0;
+}
+
+/*
+ * Append new BTF_KIND_LOC_PROTO
+ *
+ * The prototype is then populated with 0 or more BTF_KIND_LOC_PARAMs via
+ * btf__add_loc_proto_param(); similar to how btf__add_func_param() adds
+ * parameters to a FUNC_PROTO.
+ *
+ * Returns:
+ * - >0, type ID of newly added BTF type;
+ * - <0, on error.
+ */
+int btf__add_loc_proto(struct btf *btf)
+{
+ struct btf_type *t;
+ int err;
+
+ err = btf_ensure_modifiable(btf);
+ if (err)
+ return libbpf_err(err);
+
+ t = btf_add_type_mem(btf, sizeof(struct btf_type));
+ if (!t)
+ return libbpf_err(-ENOMEM);
+
+ t->name_off = 0;
+ t->info = btf_type_info(BTF_KIND_LOC_PROTO, 0, 0);
+ t->size = 0;
+
+ return btf_commit_type(btf, sizeof(struct btf_type));
+}
+
+int btf__add_loc_proto_param(struct btf *btf, __u32 id)
+{
+ struct btf_type *t;
+ int sz, err;
+ __u32 *p;
+
+ if (validate_type_id(id))
+ return libbpf_err(-EINVAL);
+
+ /* last type should be BTF_KIND_LOC_PROTO */
+ if (btf->nr_types == 0)
+ return libbpf_err(-EINVAL);
+ t = btf_last_type(btf);
+ if (!btf_is_loc_proto(t))
+ return libbpf_err(-EINVAL);
+
+ /* decompose and invalidate raw data */
+ err = btf_ensure_modifiable(btf);
+ if (err)
+ return libbpf_err(err);
+
+ sz = sizeof(__u32);
+ p = btf_add_type_mem(btf, sz);
+ if (!p)
+ return libbpf_err(-ENOMEM);
+ *p = id;
+
+ /* update parent type's vlen */
+ t = btf_last_type(btf);
+ err = btf_type_inc_vlen(t);
+ if (err)
+ return libbpf_err(err);
+
+ btf_hdr_update_type_len(btf, btf->hdr.type_len + sz);
+ return 0;
+}
+
+int btf__add_locsec(struct btf *btf, const char *name)
+{
+ struct btf_type *t;
+ int name_off = 0;
+ int err;
+
+ err = btf_ensure_modifiable(btf);
+ if (err)
+ return libbpf_err(err);
+
+ t = btf_add_type_mem(btf, sizeof(struct btf_type));
+ if (!t)
+ return libbpf_err(-ENOMEM);
+
+ if (!str_is_empty(name)) {
+ name_off = btf__add_str(btf, name);
+ if (name_off < 0)
+ return name_off;
+ }
+ t->name_off = name_off;
+ t->info = btf_type_info(BTF_KIND_LOCSEC, 0, 0);
+ t->size = 0;
+
+ return btf_commit_type(btf, sizeof(struct btf_type));
+}
+
+int btf__add_locsec_loc(struct btf *btf, __u32 func, __u32 loc_proto,
+ __u32 offset)
+{
+ struct btf_type *t;
+ struct btf_loc *l;
+ int sz, err;
+
+ if (validate_type_id(func) || validate_type_id(loc_proto))
+ return libbpf_err(-EINVAL);
+
+ /* last type should be BTF_KIND_LOCSEC */
+ if (btf->nr_types == 0)
+ return libbpf_err(-EINVAL);
+ t = btf_last_type(btf);
+ if (!btf_is_locsec(t))
+ return libbpf_err(-EINVAL);
+
+ /* decompose and invalidate raw data */
+ err = btf_ensure_modifiable(btf);
+ if (err)
+ return libbpf_err(err);
+
+ sz = sizeof(*l);
+ l = btf_add_type_mem(btf, sz);
+ if (!l)
+ return libbpf_err(-ENOMEM);
+
+ l->func = func;
+ l->loc_proto = loc_proto;
+ l->offset = offset;
+
+ /* update parent type's vlen */
+ t = btf_last_type(btf);
+ err = btf_type_inc_vlen(t);
+ if (err)
+ return libbpf_err(err);
+
+ btf_hdr_update_type_len(btf, btf->hdr.type_len + sz);
+ return 0;
+}
+
struct btf_ext_sec_info_param {
__u32 off;
__u32 len;
@@ -4114,8 +4383,8 @@ static struct btf_dedup *btf_dedup_new(struct btf *btf, const struct btf_dedup_o
for (i = 1; i < type_cnt; i++) {
struct btf_type *t = btf_type_by_id(d->btf, i);
- /* VAR and DATASEC are never deduped and are self-canonical */
- if (btf_is_var(t) || btf_is_datasec(t))
+ /* VAR, DATASEC and LOCSEC are never deduped and are self-canonical */
+ if (btf_is_var(t) || btf_is_datasec(t) || btf_is_locsec(t))
d->map[i] = i;
else
d->map[i] = BTF_UNPROCESSED_ID;
@@ -4255,6 +4524,19 @@ static int btf_dedup_strings(struct btf_dedup *d)
return err;
}
+/* Compact strings after a type-only transformation such as btf__permute(). */
+static int btf_compact_strings(struct btf *btf, struct btf_ext *btf_ext)
+{
+ struct btf_dedup d = {
+ .btf = btf,
+ .btf_ext = btf_ext,
+ };
+
+ /* Types may have been removed since the last string compaction. */
+ btf->strs_deduped = false;
+ return btf_dedup_strings(&d);
+}
+
/*
* Calculate type signature hash of TYPEDEF, ignoring referenced type IDs,
* as referenced type IDs equivalence is established separately during type
@@ -4395,6 +4677,45 @@ static bool btf_compat_enum(struct btf_type *t1, struct btf_type *t2)
btf_is_any_enum(t1) && btf_is_any_enum(t2);
}
+static long btf_hash_loc_param(struct btf_type *t)
+{
+ long h = btf_hash_common(t);
+ __u32 *v = (__u32 *)btf_loc_param(t);
+ int i, vlen = btf_vlen(t);
+
+ for (i = 0; i <= vlen; i++, v++)
+ h = hash_combine(h, *v);
+ return h;
+}
+
+static long btf_hash_loc_proto(struct btf_type *t)
+{
+ __u32 *p = btf_loc_proto_params(t);
+ long h = btf_hash_common(t);
+ int i, vlen = btf_vlen(t);
+
+ for (i = 0; i < vlen; i++, p++)
+ h = hash_combine(h, *p);
+ return h;
+}
+
+static bool btf_equal_loc_param(struct btf_type *t1, struct btf_type *t2)
+{
+ struct btf_loc_param *p1 = btf_loc_param(t1);
+ struct btf_loc_param *p2 = btf_loc_param(t2);
+ __u32 *v1 = (__u32 *)(p1 + 1);
+ __u32 *v2 = (__u32 *)(p2 + 1);
+ int i, vlen = btf_vlen(t1);
+
+ if (!btf_equal_common(t1, t2))
+ return false;
+ for (i = 0; i < vlen; i++, v1++, v2++) {
+ if (*v1 != *v2)
+ return false;
+ }
+ return true;
+}
+
/*
* Calculate type signature hash of STRUCT/UNION, ignoring referenced type IDs,
* as referenced type IDs equivalence is established separately during type
@@ -4622,6 +4943,12 @@ static int btf_dedup_prep(struct btf_dedup *d)
case BTF_KIND_FUNC_PROTO:
h = btf_hash_fnproto(t);
break;
+ case BTF_KIND_LOC_PARAM:
+ h = btf_hash_loc_param(t);
+ break;
+ case BTF_KIND_LOC_PROTO:
+ h = btf_hash_loc_proto(t);
+ break;
default:
pr_debug("unknown kind %d for type [%d]\n", btf_kind(t), type_id);
return -EINVAL;
@@ -4664,6 +4991,8 @@ static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)
case BTF_KIND_DATASEC:
case BTF_KIND_DECL_TAG:
case BTF_KIND_TYPE_TAG:
+ case BTF_KIND_LOC_PROTO:
+ case BTF_KIND_LOCSEC:
return 0;
case BTF_KIND_INT:
@@ -4713,6 +5042,18 @@ static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)
}
break;
+ case BTF_KIND_LOC_PARAM:
+ h = btf_hash_loc_param(t);
+ for_each_dedup_cand(d, hash_entry, h) {
+ cand_id = hash_entry->value;
+ cand = btf_type_by_id(d->btf, cand_id);
+ if (btf_equal_loc_param(t, cand)) {
+ new_id = cand_id;
+ break;
+ }
+ }
+ break;
+
default:
return -EINVAL;
}
@@ -5145,6 +5486,13 @@ static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id,
return 1;
}
+ case BTF_KIND_LOC_PARAM:
+ return btf_equal_loc_param(cand_type, canon_type);
+
+ case BTF_KIND_LOC_PROTO:
+ case BTF_KIND_LOCSEC:
+ return 0;
+
default:
return -EINVAL;
}
@@ -5489,6 +5837,41 @@ static int btf_dedup_ref_type(struct btf_dedup *d, __u32 type_id)
break;
}
+ case BTF_KIND_LOC_PROTO: {
+ __u32 *p1, *p2;
+ __u32 i, vlen;
+
+ p1 = btf_loc_proto_params(t);
+ vlen = btf_vlen(t);
+
+ for (i = 0; i < vlen; i++, p1++) {
+ ref_type_id = btf_dedup_ref_type(d, *p1);
+ if (ref_type_id < 0)
+ return ref_type_id;
+ *p1 = ref_type_id;
+ }
+
+ h = btf_hash_loc_proto(t);
+ for_each_dedup_cand(d, hash_entry, h) {
+ cand_id = hash_entry->value;
+ cand = btf_type_by_id(d->btf, cand_id);
+ if (!btf_equal_common(t, cand))
+ continue;
+ vlen = btf_vlen(cand);
+ p1 = btf_loc_proto_params(t);
+ p2 = btf_loc_proto_params(cand);
+ if (vlen == 0) {
+ new_id = cand_id;
+ break;
+ }
+ if (memcmp(p1, p2, vlen * sizeof(__u32)) == 0) {
+ new_id = cand_id;
+ break;
+ }
+ }
+ break;
+ }
+
default:
return -EINVAL;
}
@@ -5970,8 +6353,11 @@ static int btf_add_distilled_type_ids(struct btf_distill *dist, __u32 i)
case BTF_KIND_CONST:
case BTF_KIND_RESTRICT:
case BTF_KIND_VOLATILE:
+ case BTF_KIND_FUNC:
case BTF_KIND_FUNC_PROTO:
case BTF_KIND_TYPE_TAG:
+ case BTF_KIND_LOC_PARAM:
+ case BTF_KIND_LOC_PROTO:
dist->id_map[*id] = *id;
break;
default:
@@ -5997,7 +6383,7 @@ static int btf_add_distilled_type_ids(struct btf_distill *dist, __u32 i)
static int btf_add_distilled_types(struct btf_distill *dist)
{
- bool adding_to_base = dist->pipe.dst->start_id == 1;
+ bool adding_to_base = dist->pipe.dst->base_btf == NULL;
int id = btf__type_cnt(dist->pipe.dst);
struct btf_type *t;
int i, err = 0;
@@ -6065,8 +6451,12 @@ static int btf_add_distilled_types(struct btf_distill *dist)
case BTF_KIND_CONST:
case BTF_KIND_RESTRICT:
case BTF_KIND_VOLATILE:
+ case BTF_KIND_FUNC:
case BTF_KIND_FUNC_PROTO:
case BTF_KIND_TYPE_TAG:
+ case BTF_KIND_LOC_PARAM:
+ case BTF_KIND_LOC_PROTO:
+ case BTF_KIND_LOCSEC:
/* All other types are added to split BTF. */
if (adding_to_base)
continue;
@@ -6240,7 +6630,7 @@ void btf_set_base_btf(struct btf *btf, const struct btf *base_btf)
int btf__relocate(struct btf *btf, const struct btf *base_btf)
{
- int err = btf_relocate(btf, base_btf, NULL);
+ int err = btf_relocate(btf, base_btf, NULL, NULL);
if (!err)
btf->owns_base = false;
@@ -6251,8 +6641,15 @@ struct btf_permute {
struct btf *btf;
__u32 *id_map;
__u32 start_offs;
+ __u32 transfer_start_id;
+ bool reject_transfer;
};
+static bool btf_permute_id_is_transfer(__u32 id)
+{
+ return id & BTF_PERMUTE_ID_TRANSFER;
+}
+
/* Callback function to remap individual type ID references */
static int btf_permute_remap_type_id(__u32 *type_id, void *ctx)
{
@@ -6267,18 +6664,54 @@ static int btf_permute_remap_type_id(__u32 *type_id, void *ctx)
return -EINVAL;
*type_id = p->id_map[new_id - p->btf->start_id + p->start_offs];
+ if (p->reject_transfer && *type_id >= p->transfer_start_id)
+ return -EINVAL;
+ return 0;
+}
+
+/*
+ * String offsets in a split BTF are absolute. If its parent string table is
+ * compacted after the split was populated, move its local string offsets to
+ * start at the parent's new end.
+ */
+static int btf_permute_rebase_split_strs(struct btf *split_btf, __u32 old_start_str_off)
+{
+ __s64 delta = split_btf->base_btf->start_str_off + split_btf->base_btf->hdr.str_len -
+ old_start_str_off;
+ int i;
+
+ for (i = 0; i < split_btf->nr_types; i++) {
+ struct btf_field_iter it;
+ struct btf_type *t = btf_type_by_id(split_btf,
+ split_btf->start_id + i);
+ __u32 *str_off;
+ int err;
+
+ err = btf_field_iter_init(&it, t, BTF_FIELD_ITER_STRS);
+ if (err)
+ return err;
+ while ((str_off = btf_field_iter_next(&it))) {
+ if (*str_off >= old_start_str_off)
+ *str_off += delta;
+ }
+ }
+
+ btf_set_base_btf(split_btf, split_btf->base_btf);
return 0;
}
int btf__permute(struct btf *btf, __u32 *id_map, __u32 id_map_cnt,
const struct btf_permute_opts *opts)
{
- struct btf_permute p;
- struct btf_ext *btf_ext;
+ __u32 n, start_offs = 0, nr_transfer = 0, nr_base_types;
+ __u32 next_base_id, next_transfer_id, old_str_off = 0;
+ struct btf **transfer_btfp, *transfer_btf = NULL;
+ __u32 *new_id_map = NULL, *order_map = NULL;
void *nt, *new_types = NULL;
- __u32 *order_map = NULL;
+ struct btf_ext *btf_ext;
+ bool committed = false;
+ struct btf_permute p;
int err = 0, i;
- __u32 n, id, start_offs = 0;
if (!OPTS_VALID(opts, btf_permute_opts))
return libbpf_err(-EINVAL);
@@ -6295,7 +6728,21 @@ int btf__permute(struct btf *btf, __u32 *id_map, __u32 id_map_cnt,
if (id_map_cnt != n)
return libbpf_err(-EINVAL);
- /* record the sequence of types */
+ transfer_btfp = OPTS_GET(opts, transfer_btf, NULL);
+ if (transfer_btfp)
+ *transfer_btfp = NULL;
+
+ /*
+ * Make a working copy of the id map; it will be modified (such as
+ * removing transfer flags) unlike the caller-passed original.
+ */
+ new_id_map = calloc(id_map_cnt, sizeof(*new_id_map));
+ if (!new_id_map) {
+ err = -ENOMEM;
+ goto done;
+ }
+ memcpy(new_id_map, id_map, id_map_cnt * sizeof(*new_id_map));
+
order_map = calloc(id_map_cnt, sizeof(*id_map));
if (!order_map) {
err = -ENOMEM;
@@ -6312,33 +6759,82 @@ int btf__permute(struct btf *btf, __u32 *id_map, __u32 id_map_cnt,
if (err)
goto done;
+ /*
+ * Build the inverse map: for each requested destination ID, record the
+ * original type ID that will occupy it. order_map is indexed by the
+ * destination ID relative to the first ID represented by id_map. The
+ * caller id_map maps old IDs to requested destinations, but BTF types
+ * must be emitted in destination-ID order; the inverse map supplies the
+ * old type to copy for each output slot without repeatedly searching
+ * id_map.
+ */
for (i = start_offs; i < id_map_cnt; i++) {
- id = id_map[i];
- if (id < btf->start_id || id >= btf__type_cnt(btf)) {
+ __u32 requested_id = new_id_map[i];
+ __u32 order_idx;
+
+ if (btf_permute_id_is_transfer(requested_id)) {
+ if (!transfer_btfp) {
+ err = -EINVAL;
+ goto done;
+ }
+ requested_id &= ~BTF_PERMUTE_ID_TRANSFER;
+ nr_transfer++;
+ }
+ if (requested_id < btf->start_id || requested_id >= btf__type_cnt(btf)) {
err = -EINVAL;
goto done;
}
- id -= btf->start_id - start_offs;
+ order_idx = requested_id - btf->start_id + start_offs;
/* cannot be mapped to the same ID */
- if (order_map[id]) {
+ if (order_map[order_idx]) {
err = -EINVAL;
goto done;
}
- order_map[id] = i + btf->start_id - start_offs;
+ order_map[order_idx] = i + btf->start_id - start_offs;
}
+ /*
+ * Turn requested destination IDs into final IDs. Retained types
+ * occupy the compacted parent BTF first; transferred types immediately
+ * follow them, which is where they will start in returned split BTF.
+ */
+ nr_base_types = n - nr_transfer - start_offs;
+ next_base_id = btf->start_id;
+ next_transfer_id = btf->start_id + nr_base_types;
+ for (i = start_offs; i < id_map_cnt; i++) {
+ __u32 old_id = order_map[i];
+ __u32 old_id_idx = old_id - btf->start_id + start_offs;
+
+ if (btf_permute_id_is_transfer(new_id_map[old_id_idx]))
+ new_id_map[old_id_idx] = next_transfer_id++;
+ else
+ new_id_map[old_id_idx] = next_base_id++;
+ }
+
+ /*
+ * Copy retained types and reject references into the split (transfer)
+ * BTF from the original BTF.
+ */
p.btf = btf;
- p.id_map = id_map;
+ p.id_map = new_id_map;
p.start_offs = start_offs;
+ p.transfer_start_id = btf->start_id + nr_base_types;
+ p.reject_transfer = true;
nt = new_types;
for (i = start_offs; i < id_map_cnt; i++) {
struct btf_field_iter it;
const struct btf_type *t;
__u32 *type_id;
+ __u32 old_id;
+ __u32 old_id_idx;
int type_size;
- id = order_map[i];
- t = btf__type_by_id(btf, id);
+ old_id = order_map[i];
+ old_id_idx = old_id - btf->start_id + start_offs;
+
+ if (new_id_map[old_id_idx] >= btf->start_id + nr_base_types)
+ continue;
+ t = btf__type_by_id(btf, old_id);
type_size = btf_type_size(btf, t);
memcpy(nt, t, type_size);
@@ -6348,14 +6844,71 @@ int btf__permute(struct btf *btf, __u32 *id_map, __u32 id_map_cnt,
goto done;
while ((type_id = btf_field_iter_next(&it))) {
err = btf_permute_remap_type_id(type_id, &p);
- if (err)
+ if (err < 0)
goto done;
}
nt += type_size;
}
- /* fix up referenced IDs for btf_ext */
+ /*
+ * Copy transferred types into a split BTF. They can reference both
+ * retained parent types and other transferred types, so do not reject
+ * final IDs in the split-ID range while rewriting their references.
+ */
+ if (nr_transfer) {
+ struct btf_permute transfer_p;
+
+ transfer_btf = btf__new_empty_split(btf);
+ if (!transfer_btf) {
+ err = -errno;
+ goto done;
+ }
+ old_str_off = transfer_btf->start_str_off;
+
+ transfer_p = p;
+ /* Split transfer types may refer to other split types */
+ transfer_p.reject_transfer = false;
+ for (i = start_offs; i < id_map_cnt; i++) {
+ struct btf_field_iter it;
+ struct btf_pipe pipe = {
+ .src = btf,
+ .dst = transfer_btf,
+ .force_local_strs = true,
+ };
+ const struct btf_type *t;
+ struct btf_type *new_t;
+ __u32 *type_id;
+ __u32 old_id;
+ __u32 old_id_idx;
+ int new_id;
+
+ old_id = order_map[i];
+ old_id_idx = old_id - btf->start_id + start_offs;
+
+ if (!btf_permute_id_is_transfer(id_map[old_id_idx]))
+ continue;
+ t = btf__type_by_id(btf, old_id);
+ new_id = btf_add_type(&pipe, t);
+ if (new_id < 0) {
+ err = new_id;
+ goto done;
+ }
+ new_t = btf_type_by_id(transfer_btf, new_id);
+ err = btf_field_iter_init(&it, new_t, BTF_FIELD_ITER_IDS);
+ if (!err) {
+ while ((type_id = btf_field_iter_next(&it))) {
+ err = btf_permute_remap_type_id(type_id, &transfer_p);
+ if (err)
+ break;
+ }
+ }
+ if (err < 0)
+ goto done;
+ }
+ }
+
+ /* Rewrite optional .BTF.ext references to retained type IDs. */
btf_ext = OPTS_GET(opts, btf_ext, NULL);
if (btf_ext) {
err = btf_ext_visit_type_ids(btf_ext, btf_permute_remap_type_id, &p);
@@ -6363,18 +6916,58 @@ int btf__permute(struct btf *btf, __u32 *id_map, __u32 id_map_cnt,
goto done;
}
- for (nt = new_types, i = 0; i < id_map_cnt - start_offs; i++) {
+ /* Install the compacted parent types and rebuild their index. */
+ for (nt = new_types, i = 0; i < nr_base_types; i++) {
btf->type_offs[i] = nt - new_types;
nt += btf_type_size(btf, nt);
}
-
- free(order_map);
free(btf->types_data);
btf->types_data = new_types;
+ committed = true;
+ btf_hdr_update_type_len(btf, nt - new_types);
+ btf->nr_types = nr_base_types;
+
+ /*
+ * An important goal is to also transfer strings to the transfer
+ * BTF where they do not have duplicates in BTF. This is important
+ * for cases like inline BTF where inline function names could
+ * comprise a significant portion of the string table. The approach
+ * is to first compact the set of strings in the base now transfer
+ * is complete; this will remove unreferenced strings. Then
+ * local references in the transfer BTF have to be moved downwards
+ * based upon that compaction. Finally also compact transfer BTF
+ * references so that we replace duplicate references in split with
+ * base references where present.
+ *
+ * Once all this is done, we end up with transfer-only strings in
+ * transfer split BTF and shared strings in BTF. The result is
+ * a transfer BTF with a lot of transfer-only strings will not
+ * pollute the string table of the non-transfer BTF.
+ */
+ if (transfer_btf) {
+ err = btf_compact_strings(btf, btf_ext);
+ if (err)
+ goto done;
+ err = btf_permute_rebase_split_strs(transfer_btf, old_str_off);
+ if (err)
+ goto done;
+ err = btf_compact_strings(transfer_btf, NULL);
+ if (err)
+ goto done;
+ *transfer_btfp = transfer_btf;
+ }
+
+ /* On success return the final old-to-new type-ID map to the caller. */
+ memcpy(id_map, new_id_map, id_map_cnt * sizeof(*id_map));
+ free(new_id_map);
+ free(order_map);
return 0;
done:
+ free(new_id_map);
free(order_map);
- free(new_types);
+ if (!committed)
+ free(new_types);
+ btf__free(transfer_btf);
return libbpf_err(err);
}
diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h
index 587172c0de089..8ce5697557734 100644
--- a/tools/lib/bpf/btf.h
+++ b/tools/lib/bpf/btf.h
@@ -274,6 +274,20 @@ LIBBPF_API int btf__add_decl_tag(struct btf *btf, const char *value, int ref_typ
LIBBPF_API int btf__add_decl_attr(struct btf *btf, const char *value, int ref_type_id,
int component_idx);
+/* location construction APIs */
+LIBBPF_API int btf__add_loc_param(struct btf *btf, __u32 size, __u32 flags);
+
+LIBBPF_API int btf__add_loc_param_value(struct btf *btf, __u32 value);
+
+LIBBPF_API int btf__add_loc_proto(struct btf *btf);
+
+LIBBPF_API int btf__add_loc_proto_param(struct btf *btf, __u32 id);
+
+LIBBPF_API int btf__add_locsec(struct btf *btf, const char *name);
+
+LIBBPF_API int btf__add_locsec_loc(struct btf *btf, __u32 func, __u32 loc_proto,
+ __u32 offset);
+
struct btf_dedup_opts {
size_t sz;
/* optional .BTF.ext info to dedup along the main BTF info */
@@ -307,9 +321,17 @@ struct btf_permute_opts {
size_t sz;
/* optional .BTF.ext info along the main BTF info */
struct btf_ext *btf_ext;
+ /*
+ * If set, types whose map entry has BTF_PERMUTE_ID_TRANSFER set are
+ * removed from @btf and returned in a new split BTF based on @btf.
+ */
+ struct btf **transfer_btf;
size_t :0;
};
-#define btf_permute_opts__last_field btf_ext
+#define btf_permute_opts__last_field transfer_btf
+
+/* Mark an id_map entry as a type to be moved to .transfer_btf. */
+#define BTF_PERMUTE_ID_TRANSFER (1U << 31)
/**
* @brief **btf__permute()** rearranges BTF types in-place according to a specified ID mapping
@@ -335,6 +357,12 @@ struct btf_permute_opts {
* - Mapping is defined as `id_map[original_id - start_id] = new_id`
* - `start_id` equals `btf__type_cnt(btf__base_btf(btf))`
*
+ * An @id_map entry can be ORed with BTF_PERMUTE_ID_TRANSFER to move that
+ * type to a newly-created split BTF returned through @opts->transfer_btf.
+ * The low bits still specify the type's position in the requested ordering.
+ * The id map is updated to contain the final IDs in the base and returned
+ * split BTF.
+ *
* After permutation, all type references within the BTF data and optional
* BTF extension (if provided via @opts) are updated automatically.
*
@@ -431,6 +459,12 @@ btf_dump__dump_type_data(struct btf_dump *d, __u32 id,
#define BTF_KIND_DECL_TAG 17 /* Decl Tag */
#define BTF_KIND_TYPE_TAG 18 /* Type Tag */
#define BTF_KIND_ENUM64 19 /* Enum for up-to 64bit values */
+#define BTF_KIND_LOC_PARAM 20 /* Parameter at location */
+#define BTF_KIND_LOC_PROTO 21 /* Parameter set at location */
+#define BTF_KIND_LOCSEC 22 /* Section containing location info */
+
+struct btf_loc_param;
+struct btf_loc;
static inline __u16 btf_kind(const struct btf_type *t)
{
@@ -569,6 +603,21 @@ static inline bool btf_is_any_enum(const struct btf_type *t)
return btf_is_enum(t) || btf_is_enum64(t);
}
+static inline bool btf_is_loc_param(const struct btf_type *t)
+{
+ return btf_kind(t) == BTF_KIND_LOC_PARAM;
+}
+
+static inline bool btf_is_loc_proto(const struct btf_type *t)
+{
+ return btf_kind(t) == BTF_KIND_LOC_PROTO;
+}
+
+static inline bool btf_is_locsec(const struct btf_type *t)
+{
+ return btf_kind(t) == BTF_KIND_LOCSEC;
+}
+
static inline bool btf_kind_core_compat(const struct btf_type *t1,
const struct btf_type *t2)
{
@@ -683,6 +732,21 @@ static inline struct btf_decl_tag *btf_decl_tag(const struct btf_type *t)
return (struct btf_decl_tag *)(t + 1);
}
+static inline struct btf_loc_param *btf_loc_param(const struct btf_type *t)
+{
+ return (struct btf_loc_param *)(t + 1);
+}
+
+static inline __u32 *btf_loc_proto_params(const struct btf_type *t)
+{
+ return (__u32 *)(t + 1);
+}
+
+static inline struct btf_loc *btf_locsec_locs(const struct btf_type *t)
+{
+ return (struct btf_loc *)(t + 1);
+}
+
#ifdef __cplusplus
} /* extern "C" */
#endif
diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
index 123c448f20c74..fa995c02a1705 100644
--- a/tools/lib/bpf/btf_dump.c
+++ b/tools/lib/bpf/btf_dump.c
@@ -328,6 +328,9 @@ static int btf_dump_mark_referenced(struct btf_dump *d)
case BTF_KIND_ENUM64:
case BTF_KIND_FWD:
case BTF_KIND_FLOAT:
+ case BTF_KIND_LOC_PARAM:
+ case BTF_KIND_LOC_PROTO:
+ case BTF_KIND_LOCSEC:
break;
case BTF_KIND_VOLATILE:
@@ -609,6 +612,9 @@ static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr)
case BTF_KIND_VAR:
case BTF_KIND_DATASEC:
case BTF_KIND_DECL_TAG:
+ case BTF_KIND_LOC_PARAM:
+ case BTF_KIND_LOC_PROTO:
+ case BTF_KIND_LOCSEC:
d->type_states[id].order_state = ORDERED;
return 0;
@@ -2525,6 +2531,9 @@ static int btf_dump_dump_type_data(struct btf_dump *d,
case BTF_KIND_FUNC:
case BTF_KIND_FUNC_PROTO:
case BTF_KIND_DECL_TAG:
+ case BTF_KIND_LOC_PARAM:
+ case BTF_KIND_LOC_PROTO:
+ case BTF_KIND_LOCSEC:
err = btf_dump_unsupported_data(d, t, id);
break;
case BTF_KIND_INT:
diff --git a/tools/lib/bpf/btf_iter.c b/tools/lib/bpf/btf_iter.c
index 9a6c822c22944..54428e422ec39 100644
--- a/tools/lib/bpf/btf_iter.c
+++ b/tools/lib/bpf/btf_iter.c
@@ -29,6 +29,7 @@ int btf_field_iter_init(struct btf_field_iter *it, struct btf_type *t,
case BTF_KIND_FLOAT:
case BTF_KIND_ENUM:
case BTF_KIND_ENUM64:
+ case BTF_KIND_LOC_PARAM:
it->desc = (struct btf_field_desc) {};
break;
case BTF_KIND_FWD:
@@ -71,6 +72,20 @@ int btf_field_iter_init(struct btf_field_iter *it, struct btf_type *t,
1, {offsetof(struct btf_var_secinfo, type)}
};
break;
+ case BTF_KIND_LOC_PROTO:
+ it->desc = (struct btf_field_desc) {
+ 0, {},
+ sizeof(__u32),
+ 1, {0}};
+ break;
+ case BTF_KIND_LOCSEC:
+ it->desc = (struct btf_field_desc) {
+ 0, {},
+ sizeof(struct btf_loc),
+ 2, {offsetof(struct btf_loc, func),
+ offsetof(struct btf_loc, loc_proto)}};
+ break;
+
default:
return -EINVAL;
}
@@ -94,6 +109,8 @@ int btf_field_iter_init(struct btf_field_iter *it, struct btf_type *t,
case BTF_KIND_DECL_TAG:
case BTF_KIND_TYPE_TAG:
case BTF_KIND_DATASEC:
+ case BTF_KIND_LOC_PARAM:
+ case BTF_KIND_LOC_PROTO:
it->desc = (struct btf_field_desc) {
1, {offsetof(struct btf_type, name_off)}
};
@@ -127,6 +144,11 @@ int btf_field_iter_init(struct btf_field_iter *it, struct btf_type *t,
1, {offsetof(struct btf_param, name_off)}
};
break;
+ case BTF_KIND_LOCSEC:
+ it->desc = (struct btf_field_desc) {
+ 1, {offsetof(struct btf_type, name_off)}
+ };
+ break;
default:
return -EINVAL;
}
diff --git a/tools/lib/bpf/btf_relocate.c b/tools/lib/bpf/btf_relocate.c
index df5fa4bd87d69..e55dd75f6c950 100644
--- a/tools/lib/bpf/btf_relocate.c
+++ b/tools/lib/bpf/btf_relocate.c
@@ -441,7 +441,8 @@ static int btf_relocate_rewrite_strs(struct btf_relocate *r, __u32 i)
/* If successful, output of relocation is updated BTF with base BTF pointing
* at base_btf, and type ids, strings adjusted accordingly.
*/
-int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **id_map)
+int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **id_map,
+ __u32 **str_map)
{
unsigned int nr_types = btf__type_cnt(btf);
const struct btf_header *dist_base_hdr;
@@ -512,6 +513,10 @@ int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **id_map)
*id_map = r.id_map;
r.id_map = NULL;
}
+ if (str_map) {
+ *str_map = r.str_map;
+ r.str_map = NULL;
+ }
err_out:
free(r.id_map);
free(r.str_map);
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index 08ab2ea881fb6..7f51783df129a 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -460,6 +460,12 @@ LIBBPF_1.8.0 {
global:
bpf_program__attach_tracing_multi;
bpf_program__clone;
+ btf__add_loc_param;
+ btf__add_loc_param_value;
+ btf__add_loc_proto;
+ btf__add_loc_proto_param;
+ btf__add_locsec;
+ btf__add_locsec_loc;
btf__find_by_name_kind_own;
btf__new_empty_opts;
} LIBBPF_1.7.0;
diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h
index cb4d962338441..e7a6219374a96 100644
--- a/tools/lib/bpf/libbpf_internal.h
+++ b/tools/lib/bpf/libbpf_internal.h
@@ -249,7 +249,8 @@ const char *btf_kind_str(const struct btf_type *t);
const struct btf_type *skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id);
const struct btf_header *btf_header(const struct btf *btf);
void btf_set_base_btf(struct btf *btf, const struct btf *base_btf);
-int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **id_map);
+int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **id_map,
+ __u32 **str_map);
bool btf_type_is_traceable_func(const struct btf *btf, const struct btf_type *t);
static inline enum btf_func_linkage btf_func_linkage(const struct btf_type *t)
@@ -580,7 +581,7 @@ struct btf_field_desc {
/* member struct size, or zero, if no members */
int m_sz;
/* repeated per-member offsets */
- int m_off_cnt, m_offs[1];
+ int m_off_cnt, m_offs[22];
};
struct btf_field_iter {
diff --git a/tools/testing/selftests/bpf/btf_helpers.c b/tools/testing/selftests/bpf/btf_helpers.c
index 1c1c2c26690a7..6065ee4ba00a6 100644
--- a/tools/testing/selftests/bpf/btf_helpers.c
+++ b/tools/testing/selftests/bpf/btf_helpers.c
@@ -27,11 +27,14 @@ static const char * const btf_kind_str_mapping[] = {
[BTF_KIND_DECL_TAG] = "DECL_TAG",
[BTF_KIND_TYPE_TAG] = "TYPE_TAG",
[BTF_KIND_ENUM64] = "ENUM64",
+ [BTF_KIND_LOC_PARAM] = "LOC_PARAM",
+ [BTF_KIND_LOC_PROTO] = "LOC_PROTO",
+ [BTF_KIND_LOCSEC] = "LOCSEC",
};
static const char *btf_kind_str(__u16 kind)
{
- if (kind > BTF_KIND_ENUM64)
+ if (kind > BTF_KIND_LOCSEC)
return "UNKNOWN";
return btf_kind_str_mapping[kind];
}
@@ -203,6 +206,37 @@ int fprintf_btf_type_raw(FILE *out, const struct btf *btf, __u32 id)
fprintf(out, " type_id=%u component_idx=%d",
t->type, btf_decl_tag(t)->component_idx);
break;
+ case BTF_KIND_LOC_PARAM: {
+ struct btf_loc_param *p = btf_loc_param(t);
+ __u32 *v = (__u32 *)(p + 1);
+
+ fprintf(out, " size=%d flags=0x%x vlen=%u", t->size, p->flags, vlen);
+ for (i = 0; i < vlen; i++, v++) {
+ if (p->flags & BTF_LOC_PARAM_SIGNED)
+ fprintf(out, "\n\tvalue=%d", (__s32)*v);
+ else
+ fprintf(out, "\n\tvalue=%u", *v);
+ }
+ break;
+ }
+ case BTF_KIND_LOC_PROTO: {
+ const __u32 *p = btf_loc_proto_params(t);
+
+ fprintf(out, " vlen=%u", vlen);
+ for (i = 0; i < vlen; i++, p++)
+ fprintf(out, "\n\ttype_id=%u", *p);
+ break;
+ }
+ case BTF_KIND_LOCSEC: {
+ const struct btf_loc *l = btf_locsec_locs(t);
+
+ fprintf(out, " vlen=%u", vlen);
+ for (i = 0; i < vlen; i++, l++) {
+ fprintf(out, "\n\tfunc_type_id=%u loc_proto_type_id=%u offset=%d",
+ l->func, l->loc_proto, l->offset);
+ }
+ break;
+ }
default:
break;
}
diff --git a/tools/testing/selftests/bpf/prog_tests/btf_dedup_split.c b/tools/testing/selftests/bpf/prog_tests/btf_dedup_split.c
index 9d61611515938..34ca20f88afd2 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_dedup_split.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_dedup_split.c
@@ -554,6 +554,113 @@ static void test_split_module(void)
btf__free(vmlinux_btf);
}
+static void test_split_loc(void)
+{
+ struct btf *btf1, *btf2;
+ int err;
+
+ btf1 = btf__new_empty();
+ if (!ASSERT_OK_PTR(btf1, "empty_main_btf"))
+ return;
+
+ btf__set_pointer_size(btf1, 8); /* enforce 64-bit arch */
+
+
+ btf__add_int(btf1, "long", 8, BTF_INT_SIGNED); /* [1] long */
+ btf__add_ptr(btf1, 1); /* [2] ptr to long */
+ btf__add_func_proto(btf1, 1); /* [3] long (*)(long, long *); */
+ btf__add_func_param(btf1, "p1", 1);
+ btf__add_func_param(btf1, "p2", 2);
+ btf__add_func(btf1, "foo", BTF_FUNC_STATIC, 3); /* [4] long foo(long, long *); */
+ btf__add_loc_param(btf1, 8, BTF_LOC_PARAM_CONST);
+ btf__add_loc_param_value(btf1, 3735928559);
+ btf__add_loc_param_value(btf1, 4277009102); /* [5] loc value */
+ btf__add_loc_param(btf1, 8, BTF_LOC_PARAM_REG); /* [6] loc reg 1 */
+ btf__add_loc_param_value(btf1, 1);
+ btf__add_loc_proto(btf1); /* [7] loc proto */
+ btf__add_loc_proto_param(btf1, 5); /* param value */
+ btf__add_loc_proto_param(btf1, 6); /* param reg 1 */
+
+ VALIDATE_RAW_BTF(
+ btf1,
+ "[1] INT 'long' size=8 bits_offset=0 nr_bits=64 encoding=SIGNED",
+ "[2] PTR '(anon)' type_id=1",
+ "[3] FUNC_PROTO '(anon)' ret_type_id=1 vlen=2\n"
+ "\t'p1' type_id=1\n"
+ "\t'p2' type_id=2",
+ "[4] FUNC 'foo' type_id=3 linkage=static",
+ "[5] LOC_PARAM '(anon)' size=8 flags=0x2 vlen=2\n"
+ "\tvalue=3735928559\n"
+ "\tvalue=4277009102",
+ "[6] LOC_PARAM '(anon)' size=8 flags=0x8 vlen=1\n"
+ "\tvalue=1",
+ "[7] LOC_PROTO '(anon)' vlen=2\n"
+ "\ttype_id=5\n"
+ "\ttype_id=6");
+
+ btf2 = btf__new_empty_split(btf1);
+ if (!ASSERT_OK_PTR(btf2, "empty_split_btf"))
+ goto cleanup;
+ btf__add_loc_param(btf2, 8, BTF_LOC_PARAM_REG);
+ btf__add_loc_param_value(btf2, 1); /* [8] loc reg 1 */
+ btf__add_loc_proto(btf2); /* [9] loc proto */
+ btf__add_loc_proto_param(btf2, 5); /* param value */
+ btf__add_loc_proto_param(btf2, 8); /* param reg 1 */
+ btf__add_locsec(btf2, ".locs"); /* [9] locsec ".locs" */
+ btf__add_locsec_loc(btf2, 4, 9, 128);
+
+ VALIDATE_RAW_BTF(
+ btf2,
+ "[1] INT 'long' size=8 bits_offset=0 nr_bits=64 encoding=SIGNED",
+ "[2] PTR '(anon)' type_id=1",
+ "[3] FUNC_PROTO '(anon)' ret_type_id=1 vlen=2\n"
+ "\t'p1' type_id=1\n"
+ "\t'p2' type_id=2",
+ "[4] FUNC 'foo' type_id=3 linkage=static",
+ "[5] LOC_PARAM '(anon)' size=8 flags=0x2 vlen=2\n"
+ "\tvalue=3735928559\n"
+ "\tvalue=4277009102",
+ "[6] LOC_PARAM '(anon)' size=8 flags=0x8 vlen=1\n"
+ "\tvalue=1",
+ "[7] LOC_PROTO '(anon)' vlen=2\n"
+ "\ttype_id=5\n"
+ "\ttype_id=6",
+ "[8] LOC_PARAM '(anon)' size=8 flags=0x8 vlen=1\n"
+ "\tvalue=1",
+ "[9] LOC_PROTO '(anon)' vlen=2\n"
+ "\ttype_id=5\n"
+ "\ttype_id=8",
+ "[10] LOCSEC '.locs' vlen=1\n"
+ "\tfunc_type_id=4 loc_proto_type_id=9 offset=128");
+
+ err = btf__dedup(btf2, NULL);
+ if (!ASSERT_OK(err, "btf_dedup"))
+ goto cleanup;
+
+ VALIDATE_RAW_BTF(
+ btf2,
+ "[1] INT 'long' size=8 bits_offset=0 nr_bits=64 encoding=SIGNED",
+ "[2] PTR '(anon)' type_id=1",
+ "[3] FUNC_PROTO '(anon)' ret_type_id=1 vlen=2\n"
+ "\t'p1' type_id=1\n"
+ "\t'p2' type_id=2",
+ "[4] FUNC 'foo' type_id=3 linkage=static",
+ "[5] LOC_PARAM '(anon)' size=8 flags=0x2 vlen=2\n"
+ "\tvalue=3735928559\n"
+ "\tvalue=4277009102",
+ "[6] LOC_PARAM '(anon)' size=8 flags=0x8 vlen=1\n"
+ "\tvalue=1",
+ "[7] LOC_PROTO '(anon)' vlen=2\n"
+ "\ttype_id=5\n"
+ "\ttype_id=6",
+ "[8] LOCSEC '.locs' vlen=1\n"
+ "\tfunc_type_id=4 loc_proto_type_id=7 offset=128");
+
+cleanup:
+ btf__free(btf2);
+ btf__free(btf1);
+}
+
void test_btf_dedup_split()
{
if (test__start_subtest("split_simple"))
@@ -566,4 +673,6 @@ void test_btf_dedup_split()
test_split_dup_struct_in_cu();
if (test__start_subtest("split_module"))
test_split_module();
+ if (test__start_subtest("split_loc"))
+ test_split_loc();
}
diff --git a/tools/testing/selftests/bpf/prog_tests/btf_distill.c b/tools/testing/selftests/bpf/prog_tests/btf_distill.c
index fb67ae195a738..f433b29ffce92 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_distill.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_distill.c
@@ -671,6 +671,80 @@ static void test_distilled_base_embedded_err(void)
btf__free(btf1);
}
+/* LOC_PARAM, LOC_PROTO should be added to split BTF. */
+static void test_distilled_loc(void)
+{
+ struct btf *btf1 = NULL, *btf2 = NULL, *btf3 = NULL, *btf4 = NULL;
+
+ btf1 = btf__new_empty();
+ if (!ASSERT_OK_PTR(btf1, "empty_main_btf"))
+ return;
+
+ btf__add_int(btf1, "int", 4, BTF_INT_SIGNED); /* [1] int */
+ btf__add_func_proto(btf1, 1); /* [2] int (*)(int); */
+ btf__add_func_param(btf1, "p1", 1);
+ btf__add_func(btf1, "foo", BTF_FUNC_STATIC, 2); /* [3] int foo(int); */
+ btf__add_loc_param(btf1, 4, BTF_LOC_PARAM_SIGNED | BTF_LOC_PARAM_CONST);
+ btf__add_loc_param_value(btf1, -1); /* [4] loc value */
+
+ VALIDATE_RAW_BTF(
+ btf1,
+ "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
+ "[2] FUNC_PROTO '(anon)' ret_type_id=1 vlen=1\n"
+ "\t'p1' type_id=1",
+ "[3] FUNC 'foo' type_id=2 linkage=static",
+ "[4] LOC_PARAM '(anon)' size=4 flags=0x3 vlen=1\n"
+ "\tvalue=-1");
+
+ btf2 = btf__new_empty_split(btf1);
+ if (!ASSERT_OK_PTR(btf2, "empty_split_btf"))
+ goto cleanup;
+
+ btf__add_loc_proto(btf2); /* [5] loc proto */
+ btf__add_loc_proto_param(btf2, 4); /* param value */
+
+ btf__add_locsec(btf2, ".locs"); /* [6] locsec */
+ btf__add_locsec_loc(btf2, 3, 5, 256); /* "foo" offset 256 */
+ VALIDATE_RAW_BTF(
+ btf2,
+ "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
+ "[2] FUNC_PROTO '(anon)' ret_type_id=1 vlen=1\n"
+ "\t'p1' type_id=1",
+ "[3] FUNC 'foo' type_id=2 linkage=static",
+ "[4] LOC_PARAM '(anon)' size=4 flags=0x3 vlen=1\n"
+ "\tvalue=-1",
+ "[5] LOC_PROTO '(anon)' vlen=1\n"
+ "\ttype_id=4",
+ "[6] LOCSEC '.locs' vlen=1\n"
+ "\tfunc_type_id=3 loc_proto_type_id=5 offset=256");
+
+ if (!ASSERT_EQ(0, btf__distill_base(btf2, &btf3, &btf4),
+ "distilled_base") ||
+ !ASSERT_OK_PTR(btf3, "distilled_base") ||
+ !ASSERT_OK_PTR(btf4, "distilled_split") ||
+ !ASSERT_EQ(2, btf__type_cnt(btf3), "distilled_base_type_cnt"))
+ goto cleanup;
+
+ VALIDATE_RAW_BTF(
+ btf4,
+ "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
+ /* remainder is split BTF */
+ "[2] LOC_PROTO '(anon)' vlen=1\n"
+ "\ttype_id=6",
+ "[3] LOCSEC '.locs' vlen=1\n"
+ "\tfunc_type_id=5 loc_proto_type_id=2 offset=256",
+ "[4] FUNC_PROTO '(anon)' ret_type_id=1 vlen=1\n"
+ "\t'p1' type_id=1",
+ "[5] FUNC 'foo' type_id=4 linkage=static",
+ "[6] LOC_PARAM '(anon)' size=4 flags=0x3 vlen=1\n"
+ "\tvalue=-1");
+cleanup:
+ btf__free(btf4);
+ btf__free(btf3);
+ btf__free(btf2);
+ btf__free(btf1);
+}
+
void test_btf_distill(void)
{
if (test__start_subtest("distilled_base"))
@@ -689,4 +763,6 @@ void test_btf_distill(void)
test_distilled_base_vmlinux();
if (test__start_subtest("distilled_endianness"))
test_distilled_endianness();
+ if (test__start_subtest("distilled_loc"))
+ test_distilled_loc();
}
diff --git a/tools/testing/selftests/bpf/prog_tests/btf_field_iter.c b/tools/testing/selftests/bpf/prog_tests/btf_field_iter.c
index 32159d3eb281b..dcb5429d141df 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_field_iter.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_field_iter.c
@@ -31,8 +31,11 @@ struct field_data {
{ .ids = { 11 }, .strs = { "decltag" } },
{ .ids = { 6 }, .strs = { "typetag" } },
{ .ids = {}, .strs = { "e64", "eval1", "eval2", "eval3" } },
- { .ids = { 15, 16 }, .strs = { "datasec1" } }
-
+ { .ids = { 15, 16 }, .strs = { "datasec1" } },
+ { .ids = {}, .strs = { "" } },
+ { .ids = {}, .strs = { "" } },
+ { .ids = { 22, 23 }, .strs = { "" } },
+ { .ids = { 14, 24 }, .strs = { ".loc" } }
};
/* Fabricate BTF with various types and check BTF field iteration finds types,
@@ -88,6 +91,19 @@ void test_btf_field_iter(void)
btf__add_datasec_var_info(btf, 15, 0, 4);
btf__add_datasec_var_info(btf, 16, 4, 8);
+ btf__add_loc_param(btf, 4, BTF_LOC_PARAM_CONST | BTF_LOC_PARAM_SIGNED);
+ /* [22] loc value -1 */
+ btf__add_loc_param_value(btf, -1);
+ btf__add_loc_param(btf, 8, BTF_LOC_PARAM_REG); /* [23] loc reg 1 */
+ btf__add_loc_param_value(btf, 1);
+
+ btf__add_loc_proto(btf); /* [24] loc proto */
+ btf__add_loc_proto_param(btf, 22); /* param value -1, */
+ btf__add_loc_proto_param(btf, 23); /* param reg 1 */
+
+ btf__add_locsec(btf, ".loc"); /* [25] locsec ".loc" */
+ btf__add_locsec_loc(btf, 14, 24, 128); /* "func" */
+
VALIDATE_RAW_BTF(
btf,
"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
@@ -123,7 +139,16 @@ void test_btf_field_iter(void)
"\t'eval3' val=3000",
"[21] DATASEC 'datasec1' size=12 vlen=2\n"
"\ttype_id=15 offset=0 size=4\n"
- "\ttype_id=16 offset=4 size=8");
+ "\ttype_id=16 offset=4 size=8",
+ "[22] LOC_PARAM '(anon)' size=4 flags=0x3 vlen=1\n"
+ "\tvalue=-1",
+ "[23] LOC_PARAM '(anon)' size=8 flags=0x8 vlen=1\n"
+ "\tvalue=1",
+ "[24] LOC_PROTO '(anon)' vlen=2\n"
+ "\ttype_id=22\n"
+ "\ttype_id=23",
+ "[25] LOCSEC '.loc' vlen=1\n"
+ "\tfunc_type_id=14 loc_proto_type_id=24 offset=128");
for (id = 1; id < btf__type_cnt(btf); id++) {
struct btf_type *t = btf_type_by_id(btf, id);
diff --git a/tools/testing/selftests/bpf/prog_tests/btf_inline.c b/tools/testing/selftests/bpf/prog_tests/btf_inline.c
new file mode 100644
index 0000000000000..8e0a85f6c6980
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/btf_inline.c
@@ -0,0 +1,110 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026, Oracle and/or its affiliates. */
+
+#include <test_progs.h>
+#include <bpf/btf.h>
+#include <bpf/libbpf.h>
+
+#define BTF_SYSFS_DIR "/sys/kernel/btf"
+#define BTF_INLINE_SUFFIX ".inline"
+
+/*
+ * For a specific inline site, verify we have the right function,
+ * loc proto and loc param representation and that the offset is
+ * reasonable given the caller where it was inlined.
+ *
+ * Because bpf_testmod is compiled "out-of-tree" we have inline
+ * information in the split BTF directly rather than in btf_testmod.inline.
+ */
+void test_btf_inline(void)
+{
+ struct btf *inline_btf = NULL, *btf = NULL, *vmlinux_btf = NULL;
+ const char *inline_caller = "bpf_testmod_uprobe_write";
+ const char *inline_func = "testmod_register_uprobe";
+ bool skip = false, found_loc = false;
+ const struct btf_loc_param *lp;
+ int locsec_id, func_id, n, i;
+ long caller_addr, base_addr;
+ const struct btf_type *t;
+ struct btf_loc *l;
+ const __u32 *p;
+ int err = 0;
+
+ if (!env.has_testmod) {
+ test__skip();
+ return;
+ }
+
+ base_addr = module_get_base_addr("bpf_testmod");
+ if (!ASSERT_NEQ(base_addr, 0, "base_addr_nonzero"))
+ return;
+
+ load_kallsyms();
+ caller_addr = ksym_get_addr(inline_caller);
+ if (!ASSERT_NEQ(caller_addr, 0, "caller_addr_nonzero"))
+ return;
+
+ if (!ASSERT_GT(caller_addr, base_addr, "caller_addr_gt_base_addr"))
+ return;
+ caller_addr -= base_addr;
+
+ vmlinux_btf = btf__load_vmlinux_btf();
+ if (!ASSERT_OK_PTR(vmlinux_btf, "vmlinux_btf"))
+ return;
+
+ btf = btf__parse_split(BTF_SYSFS_DIR "/bpf_testmod", vmlinux_btf);
+ if (!ASSERT_OK_PTR(btf, "bpf_testmod_btf"))
+ goto out;
+
+ inline_btf = btf__parse_split(BTF_SYSFS_DIR "/bpf_testmod" BTF_INLINE_SUFFIX,
+ btf);
+ err = libbpf_get_error(inline_btf);
+ /* pahole may not have inline BTF feature support. */
+ if (err == -ENOENT) {
+ skip = true;
+ goto out;
+ }
+ locsec_id = btf__find_by_name_kind(inline_btf, ".text", BTF_KIND_LOCSEC);
+ if (locsec_id < 0) {
+ skip = true;
+ goto out;
+ }
+ func_id = btf__find_by_name_kind(inline_btf, inline_func, BTF_KIND_FUNC);
+ if (!ASSERT_GT(func_id, 0, "inline_caller_func"))
+ goto out;
+ t = btf__type_by_id(inline_btf, locsec_id);
+ n = btf_vlen(t);
+ for (i = 0, l = btf_locsec_locs(t); i < n; i++, l++) {
+ if (l->func == func_id) {
+ found_loc = true;
+ break;
+ }
+ }
+ if (!ASSERT_TRUE(found_loc, "found_loc"))
+ goto out;
+ if (!ASSERT_GT(l->loc_proto, 0, "loc_proto_id"))
+ goto out;
+ if (!ASSERT_GT(l->offset, 0, "loc_offset"))
+ goto out;
+ t = btf__type_by_id(inline_btf, l->loc_proto);
+ if (!ASSERT_OK_PTR(t, "loc_proto_ptr"))
+ goto out;
+ if (!ASSERT_EQ(btf_vlen(t), 1, "loc_proto_one_param"))
+ goto out;
+ p = btf_loc_proto_params(t);
+ t = btf__type_by_id(inline_btf, *p);
+ lp = btf_loc_param(t);
+ if (!ASSERT_EQ(lp->flags, BTF_LOC_PARAM_REG, "param_is_reg"))
+ goto out;
+ if (!ASSERT_GT(l->offset, caller_addr, "inline_gt_caller"))
+ goto out;
+ /* simple sanity test to roughly ensure inline site still in function */
+ if (ASSERT_LT(l->offset, caller_addr + 256, "inline_in_caller"))
+ goto out;
+out:
+ btf__free(inline_btf);
+ btf__free(btf);
+ btf__free(vmlinux_btf);
+ if (skip)
+ test__skip();
+}
diff --git a/tools/testing/selftests/bpf/prog_tests/btf_permute.c b/tools/testing/selftests/bpf/prog_tests/btf_permute.c
index 04ade5ad77aca..ef75fd71df96e 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_permute.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_permute.c
@@ -235,10 +235,138 @@ static void test_permute_split(void)
btf__free(base_btf);
}
+/* Ensure selected types can be moved into a split BTF while permuting. */
+static void test_permute_transfer(void)
+{
+ struct btf *btf = NULL, *transfer_btf = NULL;
+ LIBBPF_OPTS(btf_permute_opts, opts);
+ __u32 permute_ids[11];
+ int err, id, foo_name_off = -1, shared_name_off, foo_funcs = 0, shared_funcs = 0;
+
+ btf = btf__new_empty();
+ if (!ASSERT_OK_PTR(btf, "empty_main_btf"))
+ return;
+
+ btf__add_int(btf, "int", 4, BTF_INT_SIGNED); /* [1] int */
+ btf__add_func_proto(btf, 1); /* [2] int (*)(void) */
+ btf__add_func(btf, "foo", BTF_FUNC_STATIC, 2); /* [3] int foo(void) */
+ btf__add_loc_param(btf, 4, BTF_LOC_PARAM_SIGNED); /* [4] location */
+ btf__add_loc_param_value(btf, 0);
+ btf__add_loc_proto(btf); /* [5] location proto */
+ btf__add_loc_proto_param(btf, 4);
+ btf__add_locsec(btf, ".locs"); /* [6] location section */
+ btf__add_locsec_loc(btf, 3, 5, 128);
+ btf__add_locsec_loc(btf, 3, 5, 0);
+ btf__add_int(btf, "long", 8, BTF_INT_SIGNED); /* [7] long */
+ btf__add_func(btf, "foo", BTF_FUNC_STATIC, 2); /* [8] another foo */
+ btf__add_func(btf, "shared", BTF_FUNC_STATIC, 2); /* [9] shared string */
+ btf__add_struct(btf, "shared", 4); /* [10] retains string */
+
+ permute_ids[0] = 0;
+ permute_ids[1] = 1;
+ permute_ids[2] = BTF_PERMUTE_ID_TRANSFER | 2;
+ permute_ids[3] = BTF_PERMUTE_ID_TRANSFER | 3;
+ permute_ids[4] = BTF_PERMUTE_ID_TRANSFER | 5;
+ permute_ids[5] = BTF_PERMUTE_ID_TRANSFER | 6;
+ permute_ids[6] = BTF_PERMUTE_ID_TRANSFER | 7;
+ permute_ids[7] = 4;
+ permute_ids[8] = BTF_PERMUTE_ID_TRANSFER | 8;
+ permute_ids[9] = BTF_PERMUTE_ID_TRANSFER | 9;
+ permute_ids[10] = 10;
+ opts.transfer_btf = &transfer_btf;
+ err = btf__permute(btf, permute_ids, ARRAY_SIZE(permute_ids), &opts);
+ if (!ASSERT_OK(err, "btf__permute_transfer") ||
+ !ASSERT_OK_PTR(transfer_btf, "transfer_btf"))
+ goto cleanup;
+
+ ASSERT_EQ(4, btf__type_cnt(btf), "base_type_cnt");
+ ASSERT_EQ(-ENOENT, btf__find_str(btf, "foo"), "func_name_not_in_base");
+ shared_name_off = btf__find_str(btf, "shared");
+ if (!ASSERT_GE(shared_name_off, 0, "shared_name_in_base"))
+ goto cleanup;
+ for (id = btf__type_cnt(btf); id < btf__type_cnt(transfer_btf); id++) {
+ const struct btf_type *t = btf__type_by_id(transfer_btf, id);
+
+ if (!btf_is_func(t))
+ continue;
+ if (!strcmp(btf__name_by_offset(transfer_btf, t->name_off), "foo")) {
+ foo_funcs++;
+ if (foo_name_off < 0)
+ foo_name_off = t->name_off;
+ else if (!ASSERT_EQ(foo_name_off, t->name_off, "local_foo_dedup"))
+ goto cleanup;
+ } else if (!strcmp(btf__name_by_offset(transfer_btf, t->name_off), "shared")) {
+ shared_funcs++;
+ if (!ASSERT_EQ(shared_name_off, t->name_off, "shared_name_reuses_base"))
+ goto cleanup;
+ }
+ }
+ if (!ASSERT_GE(foo_name_off, 0, "local_foo_name"))
+ goto cleanup;
+ if (!ASSERT_EQ(2, foo_funcs, "local_foo_count") ||
+ !ASSERT_EQ(1, shared_funcs, "shared_func_count"))
+ goto cleanup;
+ VALIDATE_RAW_BTF(
+ transfer_btf,
+ "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
+ "[2] INT 'long' size=8 bits_offset=0 nr_bits=64 encoding=SIGNED",
+ "[3] STRUCT 'shared' size=4 vlen=0",
+ "[4] FUNC_PROTO '(anon)' ret_type_id=1 vlen=0",
+ "[5] FUNC 'foo' type_id=4 linkage=static",
+ "[6] LOC_PARAM '(anon)' size=4 flags=0x1 vlen=1\n"
+ "\tvalue=0",
+ "[7] LOC_PROTO '(anon)' vlen=1\n"
+ "\ttype_id=6",
+ "[8] LOCSEC '.locs' vlen=2\n"
+ "\tfunc_type_id=5 loc_proto_type_id=7 offset=128\n"
+ "\tfunc_type_id=5 loc_proto_type_id=7 offset=0",
+ "[9] FUNC 'foo' type_id=4 linkage=static",
+ "[10] FUNC 'shared' type_id=4 linkage=static");
+cleanup:
+ btf__free(transfer_btf);
+ btf__free(btf);
+}
+
+/* Permuting BTF with a layout section must keep section offsets in sync. */
+static void test_permute_layout(void)
+{
+ LIBBPF_OPTS(btf_new_opts, opts, .add_layout = true);
+ LIBBPF_OPTS(btf_permute_opts, permute_opts);
+ struct btf *btf, *parsed, *transfer_btf = NULL;
+ const void *raw;
+ __u32 raw_sz;
+ __u32 permute_ids[] = { 0, 1, BTF_PERMUTE_ID_TRANSFER | 2 };
+ int err;
+
+ btf = btf__new_empty_opts(&opts);
+ if (!ASSERT_OK_PTR(btf, "empty_layout_btf"))
+ return;
+
+ btf__add_int(btf, "int", 4, BTF_INT_SIGNED);
+ btf__add_ptr(btf, 1);
+ permute_opts.transfer_btf = &transfer_btf;
+ err = btf__permute(btf, permute_ids, ARRAY_SIZE(permute_ids), &permute_opts);
+ if (!ASSERT_OK(err, "btf__permute_layout"))
+ goto cleanup;
+
+ raw = btf__raw_data(btf, &raw_sz);
+ parsed = btf__new(raw, raw_sz);
+ if (!ASSERT_OK_PTR(parsed, "parse_permuted_layout"))
+ goto cleanup;
+ btf__free(parsed);
+cleanup:
+ btf__free(transfer_btf);
+ btf__free(btf);
+}
+
void test_btf_permute(void)
{
if (test__start_subtest("permute_base"))
test_permute_base();
if (test__start_subtest("permute_split"))
test_permute_split();
+ if (test__start_subtest("permute_transfer"))
+ test_permute_transfer();
+ if (test__start_subtest("permute_layout"))
+ test_permute_layout();
}
diff --git a/tools/testing/selftests/bpf/prog_tests/btf_sysfs.c b/tools/testing/selftests/bpf/prog_tests/btf_sysfs.c
index 3923e64c4c1d0..97eccfd7c1344 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_sysfs.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_sysfs.c
@@ -3,11 +3,16 @@
#include <test_progs.h>
#include <bpf/btf.h>
+#include <dirent.h>
+#include <limits.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
+#define BTF_SYSFS_DIR "/sys/kernel/btf"
+#define BTF_INLINE_SUFFIX ".inline"
+
static void test_btf_mmap_sysfs(const char *path, struct btf *base)
{
struct stat st;
@@ -75,7 +80,76 @@ static void test_btf_mmap_sysfs(const char *path, struct btf *base)
close(fd);
}
+static void test_btf_inline_sysfs_all(void)
+{
+ struct btf *vmlinux_btf;
+ struct dirent *dentry;
+ DIR *dir;
+ int err = 0;
+
+ dir = opendir(BTF_SYSFS_DIR);
+ if (!ASSERT_OK_PTR(dir, "open_btf_sysfs"))
+ return;
+
+ vmlinux_btf = btf__parse(BTF_SYSFS_DIR "/vmlinux", NULL);
+ if (!ASSERT_OK_PTR(vmlinux_btf, "parse_vmlinux_btf")) {
+ closedir(dir);
+ return;
+ }
+
+ while ((dentry = readdir(dir)) != NULL) {
+ struct btf *base_btf = NULL, *module_btf = NULL, *inline_btf = NULL;
+ char btf_path[PATH_MAX], inline_path[PATH_MAX];
+ struct stat st;
+
+ /* Skip ".", ".." and "foo.inline" */
+ if (strstr(dentry->d_name, "."))
+ continue;
+
+ if (strcmp(dentry->d_name, "vmlinux") == 0)
+ base_btf = vmlinux_btf;
+
+ if (snprintf(btf_path, sizeof(btf_path), "%s/%s",
+ BTF_SYSFS_DIR, dentry->d_name) >= sizeof(btf_path) ||
+ snprintf(inline_path, sizeof(inline_path), "%s/%s%s",
+ BTF_SYSFS_DIR, dentry->d_name, BTF_INLINE_SUFFIX) >=
+ sizeof(inline_path)) {
+ ASSERT_FAIL("BTF sysfs path is too long\n");
+ break;
+ }
+
+ if (!base_btf) {
+ module_btf = btf__parse_split(btf_path, vmlinux_btf);
+ err = libbpf_get_error(module_btf);
+ if (err) {
+ /* A module can be unloaded while its sysfs entry is iterated. */
+ if (err == -ENOENT)
+ continue;
+ ASSERT_OK(err, "parse_module_btf");
+ continue;
+ }
+ base_btf = module_btf;
+ }
+ if (stat(inline_path, &st)) {
+ err = errno;
+ if (err == ENOENT)
+ continue;
+ ASSERT_OK(err, "stat_inline_btf");
+ }
+ inline_btf = btf__parse_split(inline_path, base_btf);
+ err = libbpf_get_error(inline_btf);
+ if (!err)
+ btf__free(inline_btf);
+ ASSERT_OK(err, "parse_inline_btf");
+ btf__free(module_btf);
+ }
+ closedir(dir);
+
+ btf__free(vmlinux_btf);
+}
+
void test_btf_sysfs(void)
{
test_btf_mmap_sysfs("/sys/kernel/btf/vmlinux", NULL);
+ test_btf_inline_sysfs_all();
}
diff --git a/tools/testing/selftests/bpf/test_btf.h b/tools/testing/selftests/bpf/test_btf.h
index e7bc781083745..b99c04b4d26bb 100644
--- a/tools/testing/selftests/bpf/test_btf.h
+++ b/tools/testing/selftests/bpf/test_btf.h
@@ -84,4 +84,20 @@
#define BTF_TYPE_TAG_ENC(value, type) \
BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_TYPE_TAG, 0, 0), type)
+#define BTF_LOC_PARAM_ENC(nvals, sz, flags) \
+ BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_LOC_PARAM, 0, nvals), sz), (flags)
+
+#define BTF_LOC_PARAM_VAL_ENCODE(val) (val)
+
+#define BTF_LOC_PROTO_ENC(nargs) \
+ BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_LOC_PROTO, 0, nargs), 0)
+
+#define BTF_LOC_PROTO_PARAM_ENCODE(param) (param)
+
+#define BTF_LOCSEC_ENC(name, nlocs) \
+ BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_LOCSEC, 0, nlocs), 0)
+
+#define BTF_LOCSEC_LOC_ENCODE(func, loc_proto, offset) \
+ (func), (loc_proto), (offset)
+
#endif /* _TEST_BTF_H */
diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
index 2380b6cbdeadc..09d910d5e3129 100644
--- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
+++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
@@ -750,7 +750,7 @@ static struct testmod_uprobe uprobe = {
.consumer.ret_handler = uprobe_ret_handler,
};
-static int testmod_register_uprobe(loff_t offset)
+static __always_inline int testmod_register_uprobe(loff_t offset)
{
int err = -EBUSY;
diff --git a/tools/testing/selftests/bpf/trace_helpers.c b/tools/testing/selftests/bpf/trace_helpers.c
index 679008b310d9a..bf4cb720e7b81 100644
--- a/tools/testing/selftests/bpf/trace_helpers.c
+++ b/tools/testing/selftests/bpf/trace_helpers.c
@@ -244,6 +244,26 @@ int kallsyms_find(const char *sym, unsigned long long *addr)
return err;
}
+long module_get_base_addr(const char *module)
+{
+ unsigned long long addr, base_addr = 0;
+ char name[500];
+ FILE *f;
+
+ f = fopen("/proc/modules", "r");
+ if (!f)
+ return 0;
+
+ while (fscanf(f, "%s %*s %*s %*s %*s 0x%llx", name, &addr) > 0) {
+ if (strcmp(module, name) == 0) {
+ base_addr = addr;
+ break;
+ }
+ }
+ fclose(f);
+ return base_addr;
+}
+
#ifdef PROCMAP_QUERY
int env_verbosity __weak = 0;
diff --git a/tools/testing/selftests/bpf/trace_helpers.h b/tools/testing/selftests/bpf/trace_helpers.h
index 01c8ecc456270..473508306f79f 100644
--- a/tools/testing/selftests/bpf/trace_helpers.h
+++ b/tools/testing/selftests/bpf/trace_helpers.h
@@ -38,6 +38,7 @@ typedef int (*ksym_search_cmp_t)(const void *p1, const struct ksym *p2);
int load_kallsyms(void);
struct ksym *ksym_search(long key);
long ksym_get_addr(const char *name);
+long module_get_base_addr(const char *module);
struct ksyms *load_kallsyms_local(void);
struct ksym *ksym_search_local(struct ksyms *ksyms, long key);
|
| 25/3 | 2026/09/02 07:12 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 55688 output: 20 thoughts: 699 Thoughts: **KMSAN Fuzzing Justification: Inline BTF Patch Series** Okay, so I need to decide if this patch series warrants a dedicated KMSAN fuzzing session, beyond our regular KASAN efforts. My initial thought is, let's dive into the code and see if it introduces any uninitialized memory risks. First, let's break down what's happening. The patch series adds support for "inline" BTF, specifically `BTF_KIND_LOC_PARAM`, `BTF_KIND_LOC_PROTO`, and `BTF_KIND_LOCSEC`. That's a new dimension to BTF, requiring changes across the kernel: the core BTF implementation, module handling, sysfs interfaces, and also in user-space tools like `libbpf`, `bpftool`, and `resolve_btfids`. So it's pretty extensive. We're talking changes in how BTF is constructed, loaded, and exposed. My primary concern is uninitialized memory reads. KASAN is great, but it doesn't catch *everything*. KMSAN is specifically designed to find those sneaky reads of uninitialized memory that can lead to information leaks or subtle bugs. The KASAN fuzzing will definitely help in this patch series, but if there's a good chance of uninitialized memory reads, a focused KMSAN fuzzing session would be worth the effort. Let's look at the kernel code. The `btf.c` file is obviously critical. Also, I'm already spotting a change that makes me pause: The `include/linux/btf.h` change modifying `struct btf_field_desc`: `m_offs[1]` to `m_offs[2]`. This means the `btf_field_desc` struct is changing in size, and I need to ensure it's handled safely everywhere. Is `btf_field_iter.c` affected? I'll need to check the references to `btf_field_desc` in `btf.c` and everywhere it's used using the `grepper` or other tool. My gut tells me this could be a potential spot for uninitialized memory issues during the allocation or initialization of this structure. I need to understand where and how the `m_offs` array is being populated, and what happens if the size isn't handled correctly. The change in the array size is enough for me to suspect that there are many new paths. The sysfs changes are also worth a careful look. Any time you introduce a new sysfs interface, you're opening up a potential avenue for uninitialized memory exposure, especially if the new data is dynamically allocated or handled in a way that’s not thoroughly initialized. I'm also paying close attention to the new `btf_parse_split`, `btf_rebase_inline`, and `btf_parse_module` functions. The module loading/parsing is especially a concern. So I need to see how these functions handle allocation, initialization and data movement. Finally I will look at the vmlinux BTF part to identify any potential vulnerability. Alright, time to dig in with some grepping and code inspection. I think there's a strong *potential* for KMSAN to find something here, especially given the scope of changes, the new structs, and the module/sysfs interaction, but the only way to know for sure is to do a thorough code review. My initial inclination leans towards yes, we should definitely consider it. |
| 26/3 | 2026/09/02 07:12 | tool | grepper |
0mArgs:{
"Expression": "btf_field_desc"
}Results: {
"Output": "include/linux/btf.h=566=enum btf_field_iter_kind {\n--\ninclude/linux/btf.h-570-\ninclude/linux/btf.h:571:struct btf_field_desc {\ninclude/linux/btf.h-572-\t/* once-per-type offsets */\n--\ninclude/linux/btf.h=580=struct btf_field_iter {\ninclude/linux/btf.h:581:\tstruct btf_field_desc desc;\ninclude/linux/btf.h-582-\tvoid *p;\n--\ntools/lib/bpf/btf_iter.c=16=int btf_field_iter_init(struct btf_field_iter *it, struct btf_type *t,\n--\ntools/lib/bpf/btf_iter.c-32-\t\tcase BTF_KIND_LOC_PARAM:\ntools/lib/bpf/btf_iter.c:33:\t\t\tit-\u003edesc = (struct btf_field_desc) {};\ntools/lib/bpf/btf_iter.c-34-\t\t\tbreak;\n--\ntools/lib/bpf/btf_iter.c-44-\t\tcase BTF_KIND_TYPE_TAG:\ntools/lib/bpf/btf_iter.c:45:\t\t\tit-\u003edesc = (struct btf_field_desc) { 1, {offsetof(struct btf_type, type)} };\ntools/lib/bpf/btf_iter.c-46-\t\t\tbreak;\ntools/lib/bpf/btf_iter.c-47-\t\tcase BTF_KIND_ARRAY:\ntools/lib/bpf/btf_iter.c:48:\t\t\tit-\u003edesc = (struct btf_field_desc) {\ntools/lib/bpf/btf_iter.c-49-\t\t\t\t2, {sizeof(struct btf_type) + offsetof(struct btf_array, type),\n--\ntools/lib/bpf/btf_iter.c-54-\t\tcase BTF_KIND_UNION:\ntools/lib/bpf/btf_iter.c:55:\t\t\tit-\u003edesc = (struct btf_field_desc) {\ntools/lib/bpf/btf_iter.c-56-\t\t\t\t0, {},\n--\ntools/lib/bpf/btf_iter.c-61-\t\tcase BTF_KIND_FUNC_PROTO:\ntools/lib/bpf/btf_iter.c:62:\t\t\tit-\u003edesc = (struct btf_field_desc) {\ntools/lib/bpf/btf_iter.c-63-\t\t\t\t1, {offsetof(struct btf_type, type)},\n--\ntools/lib/bpf/btf_iter.c-68-\t\tcase BTF_KIND_DATASEC:\ntools/lib/bpf/btf_iter.c:69:\t\t\tit-\u003edesc = (struct btf_field_desc) {\ntools/lib/bpf/btf_iter.c-70-\t\t\t\t0, {},\n--\ntools/lib/bpf/btf_iter.c-75-\t\tcase BTF_KIND_LOC_PROTO:\ntools/lib/bpf/btf_iter.c:76:\t\t\tit-\u003edesc = (struct btf_field_desc) {\ntools/lib/bpf/btf_iter.c-77-\t\t\t\t0, {},\n--\ntools/lib/bpf/btf_iter.c-81-\t\tcase BTF_KIND_LOCSEC:\ntools/lib/bpf/btf_iter.c:82:\t\t\tit-\u003edesc = (struct btf_field_desc) {\ntools/lib/bpf/btf_iter.c-83-\t\t\t\t0, {},\n--\ntools/lib/bpf/btf_iter.c-95-\t\tcase BTF_KIND_UNKN:\ntools/lib/bpf/btf_iter.c:96:\t\t\tit-\u003edesc = (struct btf_field_desc) {};\ntools/lib/bpf/btf_iter.c-97-\t\t\tbreak;\n--\ntools/lib/bpf/btf_iter.c-113-\t\tcase BTF_KIND_LOC_PROTO:\ntools/lib/bpf/btf_iter.c:114:\t\t\tit-\u003edesc = (struct btf_field_desc) {\ntools/lib/bpf/btf_iter.c-115-\t\t\t\t1, {offsetof(struct btf_type, name_off)}\n--\ntools/lib/bpf/btf_iter.c-118-\t\tcase BTF_KIND_ENUM:\ntools/lib/bpf/btf_iter.c:119:\t\t\tit-\u003edesc = (struct btf_field_desc) {\ntools/lib/bpf/btf_iter.c-120-\t\t\t\t1, {offsetof(struct btf_type, name_off)},\n--\ntools/lib/bpf/btf_iter.c-125-\t\tcase BTF_KIND_ENUM64:\ntools/lib/bpf/btf_iter.c:126:\t\t\tit-\u003edesc = (struct btf_field_desc) {\ntools/lib/bpf/btf_iter.c-127-\t\t\t\t1, {offsetof(struct btf_type, name_off)},\n--\ntools/lib/bpf/btf_iter.c-133-\t\tcase BTF_KIND_UNION:\ntools/lib/bpf/btf_iter.c:134:\t\t\tit-\u003edesc = (struct btf_field_desc) {\ntools/lib/bpf/btf_iter.c-135-\t\t\t\t1, {offsetof(struct btf_type, name_off)},\n--\ntools/lib/bpf/btf_iter.c-140-\t\tcase BTF_KIND_FUNC_PROTO:\ntools/lib/bpf/btf_iter.c:141:\t\t\tit-\u003edesc = (struct btf_field_desc) {\ntools/lib/bpf/btf_iter.c-142-\t\t\t\t1, {offsetof(struct btf_type, name_off)},\n--\ntools/lib/bpf/btf_iter.c-147-\t\tcase BTF_KIND_LOCSEC:\ntools/lib/bpf/btf_iter.c:148:\t\t\tit-\u003edesc = (struct btf_field_desc) {\ntools/lib/bpf/btf_iter.c-149-\t\t\t\t1, {offsetof(struct btf_type, name_off)}\n--\ntools/lib/bpf/libbpf_internal.h=573=enum btf_field_iter_kind {\n--\ntools/lib/bpf/libbpf_internal.h-577-\ntools/lib/bpf/libbpf_internal.h:578:struct btf_field_desc {\ntools/lib/bpf/libbpf_internal.h-579-\t/* once-per-type offsets */\n--\ntools/lib/bpf/libbpf_internal.h=587=struct btf_field_iter {\ntools/lib/bpf/libbpf_internal.h:588:\tstruct btf_field_desc desc;\ntools/lib/bpf/libbpf_internal.h-589-\tvoid *p;\n"
} |
| 27/3 | 2026/09/02 07:12 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 58986 output: 31 thoughts: 28 Thoughts: Okay, let's dive into this. Here's how I'd approach that kernel code exploration: **Diving into `btf_field_iter` Usage** Alright, I'm thinking, "Let's get down to the nitty-gritty and see exactly how `btf_field_iter` is being utilized within the kernel." My immediate instinct is to crack open the `btf.c` source file, as that seems the most logical place to start. I need to understand the context of this iteration function. I want a good, broad view. I'm going to search directly for instances of `btf_field_iter` within that file. That should give me a solid list of the relevant kernel functions where it's actually being *called*. Seeing those call sites will give me an immediate grasp of the design pattern involved and how developers are leveraging this particular iterator. From there, I can start to infer the purpose of the iteration and what problem this function is designed to solve. |
| 28/3 | 2026/09/02 07:13 | tool | grepper |
0mArgs:{
"Expression": "btf_field_iter",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/btf.c=6779=static int btf_rebase_inline(struct btf *inline_btf, const struct btf *module_btf,\n--\nkernel/bpf/btf.c-6798-\tfor (i = 0; i \u003c inline_btf-\u003enr_types; i++) {\nkernel/bpf/btf.c:6799:\t\tstruct btf_field_iter it;\nkernel/bpf/btf.c-6800-\t\tstruct btf_type *t = inline_btf-\u003etypes[i];\n--\nkernel/bpf/btf.c-6803-\nkernel/bpf/btf.c:6804:\t\terr = btf_field_iter_init(\u0026it, t, BTF_FIELD_ITER_IDS);\nkernel/bpf/btf.c-6805-\t\tif (err)\nkernel/bpf/btf.c-6806-\t\t\treturn err;\nkernel/bpf/btf.c:6807:\t\twhile ((id = btf_field_iter_next(\u0026it))) {\nkernel/bpf/btf.c-6808-\t\t\tif (!*id)\n--\nkernel/bpf/btf.c-6819-\nkernel/bpf/btf.c:6820:\t\terr = btf_field_iter_init(\u0026it, t, BTF_FIELD_ITER_STRS);\nkernel/bpf/btf.c-6821-\t\tif (err)\nkernel/bpf/btf.c-6822-\t\t\treturn err;\nkernel/bpf/btf.c:6823:\t\twhile ((str_off = btf_field_iter_next(\u0026it))) {\nkernel/bpf/btf.c-6824-\t\t\tif (!*str_off)\n"
} |
| 29/3 | 2026/09/02 07:13 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 59731 output: 38 thoughts: 56 Thoughts: **Diving into BTF in the Kernel** Okay, so I need to refresh my memory on some recent additions around BTF, specifically related to `sysfs_btf_*` and any other related code that's been integrated. To get a handle on the key changes, I'll focus my attention on `kernel/bpf/btf.c`. Let's zero in on lines 6770 through 6920 – that should cover the initial batch of added functionality. Then, I should probably also examine lines 8870 through 9200; that chunk of code might contain some related implementations or refinements. This should give me a good overview of the adjustments and give me the context I need. |
| 30/3 | 2026/09/02 07:13 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/btf.c",
"FirstLine": 6770,
"LineCount": 80
}Results: {
"Contents": "6770:\t\t\tgoto errout;\n6771:\t\n6772:\t\treturn btf;\n6773:\t\n6774:\terrout:\n6775:\t\tbtf_free(btf);\n6776:\t\treturn ERR_PTR(err);\n6777:\t}\n6778:\t\n6779:\tstatic int btf_rebase_inline(struct btf *inline_btf, const struct btf *module_btf,\n6780:\t\t\t\t const u32 *module_id_map, const u32 *module_str_map,\n6781:\t\t\t\t u32 old_module_type_cnt)\n6782:\t{\n6783:\t\tu32 old_start_id = inline_btf-\u003estart_id;\n6784:\t\tu32 old_start_str_off = inline_btf-\u003estart_str_off;\n6785:\t\tu32 old_module_start_str_off = old_start_str_off - module_btf-\u003ehdr.str_len;\n6786:\t\tu32 new_start_id = btf_nr_types(module_btf);\n6787:\t\tu32 new_start_str_off = module_btf-\u003estart_str_off + module_btf-\u003ehdr.str_len;\n6788:\t\ts64 id_delta = (s64)new_start_id - old_start_id;\n6789:\t\ts64 str_delta = (s64)new_start_str_off - old_start_str_off;\n6790:\t\tu32 i;\n6791:\t\n6792:\t\t/*\n6793:\t\t * The inline BTF was parsed relative to the original module BTF. Its\n6794:\t\t * base IDs must therefore use the map generated when that BTF was\n6795:\t\t * relocated, while IDs for inline-local types only move by the change\n6796:\t\t * in the module BTF's starting ID.\n6797:\t\t */\n6798:\t\tfor (i = 0; i \u003c inline_btf-\u003enr_types; i++) {\n6799:\t\t\tstruct btf_field_iter it;\n6800:\t\t\tstruct btf_type *t = inline_btf-\u003etypes[i];\n6801:\t\t\tu32 *id, *str_off;\n6802:\t\t\tint err;\n6803:\t\n6804:\t\t\terr = btf_field_iter_init(\u0026it, t, BTF_FIELD_ITER_IDS);\n6805:\t\t\tif (err)\n6806:\t\t\t\treturn err;\n6807:\t\t\twhile ((id = btf_field_iter_next(\u0026it))) {\n6808:\t\t\t\tif (!*id)\n6809:\t\t\t\t\tcontinue;\n6810:\t\t\t\tif (*id \u003c old_module_type_cnt) {\n6811:\t\t\t\t\tif (module_id_map)\n6812:\t\t\t\t\t\t*id = module_id_map[*id];\n6813:\t\t\t\t} else if (*id \u003e= old_start_id) {\n6814:\t\t\t\t\t*id += id_delta;\n6815:\t\t\t\t} else {\n6816:\t\t\t\t\treturn -EINVAL;\n6817:\t\t\t\t}\n6818:\t\t\t}\n6819:\t\n6820:\t\t\terr = btf_field_iter_init(\u0026it, t, BTF_FIELD_ITER_STRS);\n6821:\t\t\tif (err)\n6822:\t\t\t\treturn err;\n6823:\t\t\twhile ((str_off = btf_field_iter_next(\u0026it))) {\n6824:\t\t\t\tif (!*str_off)\n6825:\t\t\t\t\tcontinue;\n6826:\t\t\t\t/*\n6827:\t\t\t\t * LOCSEC names its code section in the module BTF's string\n6828:\t\t\t\t * section. Shift those inherited strings together with\n6829:\t\t\t\t * inline-local strings when replacing a distilled base BTF.\n6830:\t\t\t\t */\n6831:\t\t\t\tif (*str_off \u003c old_module_start_str_off) {\n6832:\t\t\t\t\t/* vmlinux strings retain their offsets for in-tree modules. */\n6833:\t\t\t\t\tif (!module_id_map)\n6834:\t\t\t\t\t\tcontinue;\n6835:\t\t\t\t\tif (!module_str_map || !module_str_map[*str_off])\n6836:\t\t\t\t\t\treturn -EINVAL;\n6837:\t\t\t\t\t*str_off = module_str_map[*str_off];\n6838:\t\t\t\t\tcontinue;\n6839:\t\t\t\t}\n6840:\t\t\t\t*str_off += str_delta;\n6841:\t\t\t}\n6842:\t\t}\n6843:\t\n6844:\t\tbtf_set_base_btf(inline_btf, module_btf);\n6845:\t\tbtf_check_sorted(inline_btf);\n6846:\t\treturn 0;\n6847:\t}\n6848:\t\n6849:\tstatic struct btf *btf_parse_module(const char *module_name, const void *data,\n"
} |
| 31/3 | 2026/09/02 07:13 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 61319 output: 39 thoughts: 37 Thoughts: Here's my summary, as though I'm thinking through this process: **Diving into BTF Sysfs Interface** Okay, so I need to get a handle on how the sysfs interface interacts with BTF, and the first place I'm going to look is `kernel/bpf/btf.c`. Specifically, I'm zeroing in on the code around the `sysfs_btf_*` functions – that's usually where the sysfs handling lives. I'm thinking roughly lines 8870 through 9200 are the relevant section, but I'll have to take a look to be sure. I'm expecting to find the standard sysfs file operations defined: `show`, `store`, maybe some `get_attribute` or `set_attribute` functions. I'll be looking for how they are used, how the data is handled, where the BPF-related structures get involved. I'll probably also check for related error handling and any security considerations. Hopefully, I'll quickly get an understanding of how BTF data is exposed and managed through sysfs. It's a key piece of the puzzle if I'm going to implement some sort of external integration for this. |
| 32/3 | 2026/09/02 07:13 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/btf.c",
"FirstLine": 8870,
"LineCount": 100
}Results: {
"Contents": "8870:\t\tBTF_MODULE_F_LIVE = (1 \u003c\u003c 0),\n8871:\t};\n8872:\t\n8873:\t#if IS_ENABLED(CONFIG_SYSFS)\n8874:\t#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)\n8875:\tstatic struct bin_attribute *vmlinux_inline_attr;\n8876:\t#endif\n8877:\t\n8878:\tstatic int sysfs_btf_bin_attr_load(struct bin_attribute *attr)\n8879:\t{\n8880:\t\tchar modname[MODULE_NAME_LEN + sizeof(\"btf_vmlinux_inline\")];\n8881:\t\tint retries = 0;\n8882:\t\n8883:\t\t/* First on-demand read; load module. */\n8884:\t\tsnprintf(modname, sizeof(modname), \"btf_%s\", attr-\u003eattr.name);\n8885:\t\tstrreplace(modname, '.', '_');\n8886:\t\trequest_module(\"%s\", modname);\n8887:\t\n8888:\t\t/*\n8889:\t\t * request_module() is synchronous, but the module notifier is\n8890:\t\t * responsible for updating private data, so retries are required.\n8891:\t\t */\n8892:\t\twhile (retries++ \u003c 10) {\n8893:\t\t\tif (smp_load_acquire(\u0026attr-\u003esize))\n8894:\t\t\t\treturn 0;\n8895:\t\t\tudelay(50);\n8896:\t\t}\n8897:\t\treturn -ENODEV;\n8898:\t}\n8899:\t\n8900:\tstatic int sysfs_btf_kernfs_open(struct kernfs_open_file *of)\n8901:\t{\n8902:\t\tstruct bin_attribute *attr = of-\u003ekn-\u003epriv;\n8903:\t\tsize_t data_size;\n8904:\t\tint err;\n8905:\t\n8906:\t\tif (!smp_load_acquire(\u0026attr-\u003esize)) {\n8907:\t\t\terr = sysfs_btf_bin_attr_load(attr);\n8908:\t\t\tif (err)\n8909:\t\t\t\treturn err;\n8910:\t\t}\n8911:\t\t/* Refresh file size or the open() caller will not see updated size. */\n8912:\t\tdata_size = smp_load_acquire(\u0026attr-\u003esize);\n8913:\t\tof-\u003ekn-\u003eattr.size = data_size;\n8914:\t\tif (of-\u003efile) {\n8915:\t\t\tstruct inode *inode = file_inode(of-\u003efile);\n8916:\t\n8917:\t\t\tif (inode)\n8918:\t\t\t\ti_size_write(inode, data_size);\n8919:\t\t}\n8920:\t\treturn 0;\n8921:\t}\n8922:\t\n8923:\tstatic ssize_t sysfs_btf_kernfs_read(struct kernfs_open_file *of, char *buf,\n8924:\t\t\t\t\t size_t bytes_requested, loff_t offset)\n8925:\t{\n8926:\t\tstruct bin_attribute *attr = of-\u003ekn-\u003epriv;\n8927:\t\tvoid *data;\n8928:\t\tsize_t data_size;\n8929:\t\n8930:\t\tdata_size = smp_load_acquire(\u0026attr-\u003esize);\n8931:\t\tif (offset \u003e= data_size)\n8932:\t\t\treturn 0;\n8933:\t\tif (offset + bytes_requested \u003e data_size)\n8934:\t\t\tbytes_requested = data_size - offset;\n8935:\t\tdata = READ_ONCE(attr-\u003eprivate);\n8936:\t\tmemcpy(buf, data + offset, bytes_requested);\n8937:\t\n8938:\t\treturn bytes_requested;\n8939:\t}\n8940:\t\n8941:\tstatic const struct kernfs_ops sysfs_btf_kernfs_ops = {\n8942:\t\t.open = sysfs_btf_kernfs_open,\n8943:\t\t.read = sysfs_btf_kernfs_read,\n8944:\t};\n8945:\t\n8946:\tstruct bin_attribute *sysfs_btf_add(const char *name, void *data, size_t data_size)\n8947:\t{\n8948:\t\tstruct bin_attribute *attr;\n8949:\t\tint err = 0;\n8950:\t\n8951:\t\tattr = kzalloc_obj(*attr);\n8952:\t\tif (!attr)\n8953:\t\t\treturn ERR_PTR(-ENOMEM);\n8954:\t\n8955:\t\tsysfs_bin_attr_init(attr);\n8956:\t\tattr-\u003eattr.mode = 0444;\n8957:\t\tattr-\u003esize = data_size;\n8958:\t\tattr-\u003eprivate = data;\n8959:\t\tattr-\u003eread = sysfs_bin_attr_simple_read;\n8960:\t\tattr-\u003eattr.name = kstrdup(name, GFP_KERNEL);\n8961:\t\tif (!attr-\u003eattr.name) {\n8962:\t\t\terr = -ENOMEM;\n8963:\t\t\tgoto err_free;\n8964:\t\t}\n8965:\t\tif (data_size \u003e 0) {\n8966:\t\t\terr = sysfs_create_bin_file(btf_kobj, attr);\n8967:\t\t} else {\n8968:\t\t\tstruct kernfs_node *node;\n8969:\t\n"
} |
| 33/3 | 2026/09/02 07:13 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 63163 output: 39 thoughts: 17 Thoughts: Okay, let's dive into lines 8970-9100. I need to get a clear understanding of the argument presented within this section. Given my expertise, I'm already anticipating the potential nuances, the subtle shifts in logic that are likely present in this specific section. I'll need to pay close attention to the way the author builds their case, the evidence they provide, and the conclusions they draw. I'll also be looking for any potential weaknesses or areas that might require further scrutiny. I'll be particularly vigilant for any assumptions they may be making, and how those assumptions might impact their overall argument. Understanding the intricacies of this section is critical, and I'm ready to apply my critical thinking skills to ensure a thorough analysis. |
| 34/3 | 2026/09/02 07:13 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/btf.c",
"FirstLine": 8970,
"LineCount": 100
}Results: {
"Contents": "8970:\t\t\tnode = __kernfs_create_file(btf_kobj-\u003esd, attr-\u003eattr.name,\n8971:\t\t\t\t\t\t attr-\u003eattr.mode, GLOBAL_ROOT_UID,\n8972:\t\t\t\t\t\t GLOBAL_ROOT_GID, data_size,\n8973:\t\t\t\t\t\t \u0026sysfs_btf_kernfs_ops, attr, NULL, NULL);\n8974:\t\t\tif (IS_ERR(node))\n8975:\t\t\t\terr = PTR_ERR(node);\n8976:\t\t}\n8977:\t\tif (err) {\n8978:\t\t\tpr_warn(\"failed to register [%s] BTF in sysfs: %d\\n\", name, err);\n8979:\t\t\tgoto err_free;\n8980:\t\t}\n8981:\t\treturn attr;\n8982:\t\n8983:\terr_free:\n8984:\t\tkfree(attr-\u003eattr.name);\n8985:\t\tkfree(attr);\n8986:\t\treturn ERR_PTR(err);\n8987:\t}\n8988:\t\n8989:\t#else\n8990:\tstruct bin_attribute *sysfs_btf_add(const char *name, void *data, size_t data_size)\n8991:\t{\n8992:\t\treturn NULL;\n8993:\t}\n8994:\t#endif\n8995:\t\n8996:\t#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)\n8997:\tstatic void sysfs_btf_update(struct bin_attribute *attr, void *data, size_t data_size)\n8998:\t{\n8999:\t\tif (!attr)\n9000:\t\t\treturn;\n9001:\t\tWRITE_ONCE(attr-\u003eprivate, data);\n9002:\t\t/* Publish data before its non-zero size makes it readable. */\n9003:\t\tsmp_store_release(\u0026attr-\u003esize, data_size);\n9004:\t}\n9005:\t#endif\n9006:\t\n9007:\t#ifdef CONFIG_DEBUG_INFO_BTF_MODULES\n9008:\t#if IS_ENABLED(CONFIG_SYSFS)\n9009:\tstatic void sysfs_btf_remove(struct bin_attribute *attr)\n9010:\t{\n9011:\t\tsysfs_remove_bin_file(btf_kobj, attr);\n9012:\t\tkfree(attr-\u003eattr.name);\n9013:\t\tkfree(attr);\n9014:\t}\n9015:\t#else\n9016:\tstatic void sysfs_btf_remove(struct bin_attribute *attr)\n9017:\t{\n9018:\t}\n9019:\t#endif\n9020:\t\n9021:\tstruct btf_module {\n9022:\t\tstruct list_head list;\n9023:\t\tstruct module *module;\n9024:\t\tstruct btf *btf;\n9025:\t\tstruct bin_attribute *sysfs_attr;\n9026:\t#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)\n9027:\t\tstruct bin_attribute *sysfs_inline_attr;\n9028:\t\tvoid *btf_inline_data;\n9029:\t#endif\n9030:\t\tint flags;\n9031:\t};\n9032:\t\n9033:\tstatic LIST_HEAD(btf_modules);\n9034:\tstatic DEFINE_MUTEX(btf_module_mutex);\n9035:\t\n9036:\tstatic void purge_cand_cache(struct btf *btf);\n9037:\t\n9038:\tstatic int btf_module_notify(struct notifier_block *nb, unsigned long op,\n9039:\t\t\t\t void *module)\n9040:\t{\n9041:\t\tstruct btf_module *btf_mod, *tmp;\n9042:\t\tstruct module *mod = module;\n9043:\t\tstruct bin_attribute *attr;\n9044:\t\tstruct btf *btf;\n9045:\t\tvoid *inline_data = NULL, *relocated_inline_data = NULL;\n9046:\t\tunsigned int inline_data_size = 0;\n9047:\t\tint err = 0;\n9048:\t\n9049:\t\tif (mod-\u003ebtf_data_size == 0 ||\n9050:\t\t (op != MODULE_STATE_COMING \u0026\u0026 op != MODULE_STATE_LIVE \u0026\u0026\n9051:\t\t op != MODULE_STATE_GOING))\n9052:\t\t\tgoto out;\n9053:\t\n9054:\t\tswitch (op) {\n9055:\t\tcase MODULE_STATE_COMING:\n9056:\t#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)\n9057:\t\t\tinline_data = mod-\u003ebtf_inline_data;\n9058:\t\t\tinline_data_size = mod-\u003ebtf_inline_data_size;\n9059:\t#endif\n9060:\t\t\tbtf_mod = kzalloc_obj(*btf_mod);\n9061:\t\t\tif (!btf_mod) {\n9062:\t\t\t\terr = -ENOMEM;\n9063:\t\t\t\tgoto out;\n9064:\t\t\t}\n9065:\t\t\tbtf = btf_parse_module(mod-\u003ename, mod-\u003ebtf_data, mod-\u003ebtf_data_size,\n9066:\t\t\t\t\t mod-\u003ebtf_base_data, mod-\u003ebtf_base_data_size,\n9067:\t\t\t\t\t inline_data, inline_data_size,\n9068:\t\t\t\t\t strcmp(mod-\u003ename, \"btf_vmlinux_inline\") == 0,\n9069:\t\t\t\t\t \u0026relocated_inline_data);\n"
} |
| 35/3 | 2026/09/02 07:13 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 64918 output: 39 thoughts: 17 Thoughts: Okay, here's my attempt at summarizing the thought process, as requested, geared toward an expert audience, and written in the first person: **Diving Back In: Lines 9070-9200** Alright, let's pick this back up. The analysis is starting to get pretty granular, but we need to stay focused. Time to resume reading and parsing the material from lines 9070 to 9200. I need to maintain a clear picture of the context, the core arguments, and any potential shifts in methodology or evidence that are being presented. I'm going to pay particularly close attention to anything that deviates from the established pattern or throws up any red flags. A focused, detailed read of those particular lines should give us a solid foundation moving forward. |
| 36/3 | 2026/09/02 07:13 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/btf.c",
"FirstLine": 9070,
"LineCount": 100
}Results: {
"Contents": "9070:\t\t\tif (IS_ERR(btf)) {\n9071:\t\t\t\tkfree(btf_mod);\n9072:\t\t\t\tif (!IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) {\n9073:\t\t\t\t\tpr_warn(\"failed to validate module [%s] BTF: %ld\\n\",\n9074:\t\t\t\t\t\tmod-\u003ename, PTR_ERR(btf));\n9075:\t\t\t\t\terr = PTR_ERR(btf);\n9076:\t\t\t\t} else {\n9077:\t\t\t\t\tpr_warn_once(\"Kernel module BTF mismatch detected, BTF debug info may be unavailable for some modules\\n\");\n9078:\t\t\t\t}\n9079:\t\t\t\tgoto out;\n9080:\t\t\t}\n9081:\t\t\terr = btf_alloc_id(btf);\n9082:\t\t\tif (err) {\n9083:\t\t\t\tbtf_free(btf);\n9084:\t\t\t\tkvfree(relocated_inline_data);\n9085:\t\t\t\tkfree(btf_mod);\n9086:\t\t\t\tgoto out;\n9087:\t\t\t}\n9088:\t\n9089:\t\t\tpurge_cand_cache(NULL);\n9090:\t\t\tmutex_lock(\u0026btf_module_mutex);\n9091:\t\t\tbtf_mod-\u003emodule = module;\n9092:\t\t\tbtf_mod-\u003ebtf = btf;\n9093:\t#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)\n9094:\t\t\tbtf_mod-\u003ebtf_inline_data = relocated_inline_data;\n9095:\t#endif\n9096:\t\t\tlist_add(\u0026btf_mod-\u003elist, \u0026btf_modules);\n9097:\t\t\tmutex_unlock(\u0026btf_module_mutex);\n9098:\t\n9099:\t\t\tattr = sysfs_btf_add(btf-\u003ename, btf-\u003edata, btf-\u003edata_size);\n9100:\t\t\tif (IS_ERR(attr)) {\n9101:\t\t\t\terr = 0;\n9102:\t\t\t\tgoto out;\n9103:\t\t\t}\n9104:\t\t\tbtf_mod-\u003esysfs_attr = attr;\n9105:\t\n9106:\t#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)\n9107:\t\t\tif (relocated_inline_data) {\n9108:\t\t\t\tchar name[MODULE_NAME_LEN + sizeof(\".inline\")];\n9109:\t\n9110:\t\t\t\tif (strcmp(mod-\u003ename, \"btf_vmlinux_inline\") == 0) {\n9111:\t\t\t\t\tif (vmlinux_inline_attr)\n9112:\t\t\t\t\t\tsysfs_btf_update(vmlinux_inline_attr,\n9113:\t\t\t\t\t\t\t\t relocated_inline_data, inline_data_size);\n9114:\t\t\t\t\telse\n9115:\t\t\t\t\t\tkvfree(relocated_inline_data);\n9116:\t\t\t\t\tbtf_mod-\u003ebtf_inline_data = NULL;\n9117:\t\t\t\t\tbreak;\n9118:\t\t\t\t}\n9119:\t\t\t\tsnprintf(name, sizeof(name), \"%s.inline\", mod-\u003ename);\n9120:\t\t\t\tattr = sysfs_btf_add(name, relocated_inline_data, inline_data_size);\n9121:\t\t\t\tif (IS_ERR(attr)) {\n9122:\t\t\t\t\terr = 0;\n9123:\t\t\t\t\tkvfree(relocated_inline_data);\n9124:\t\t\t\t\tbtf_mod-\u003ebtf_inline_data = NULL;\n9125:\t\t\t\t\tgoto out;\n9126:\t\t\t\t}\n9127:\t\t\t\tbtf_mod-\u003esysfs_inline_attr = attr;\n9128:\t\t\t}\n9129:\t#endif\n9130:\t\n9131:\t\t\tbreak;\n9132:\t\tcase MODULE_STATE_LIVE:\n9133:\t\t\tmutex_lock(\u0026btf_module_mutex);\n9134:\t\t\tlist_for_each_entry_safe(btf_mod, tmp, \u0026btf_modules, list) {\n9135:\t\t\t\tif (btf_mod-\u003emodule != module)\n9136:\t\t\t\t\tcontinue;\n9137:\t\n9138:\t\t\t\tbtf_mod-\u003eflags |= BTF_MODULE_F_LIVE;\n9139:\t\t\t\tbreak;\n9140:\t\t\t}\n9141:\t\t\tmutex_unlock(\u0026btf_module_mutex);\n9142:\t\t\tbreak;\n9143:\t\tcase MODULE_STATE_GOING:\n9144:\t\t\tmutex_lock(\u0026btf_module_mutex);\n9145:\t\t\tlist_for_each_entry_safe(btf_mod, tmp, \u0026btf_modules, list) {\n9146:\t\t\t\tif (btf_mod-\u003emodule != module)\n9147:\t\t\t\t\tcontinue;\n9148:\t\n9149:\t\t\t\t/*\n9150:\t\t\t\t * For modules, we do the freeing of BTF IDR as soon as\n9151:\t\t\t\t * module goes away to disable BTF discovery, since the\n9152:\t\t\t\t * btf_try_get_module() on such BTFs will fail. This may\n9153:\t\t\t\t * be called again on btf_put(), but it's ok to do so.\n9154:\t\t\t\t */\n9155:\t\t\t\tbtf_free_id(btf_mod-\u003ebtf);\n9156:\t\t\t\tlist_del(\u0026btf_mod-\u003elist);\n9157:\t\t\t\tif (btf_mod-\u003esysfs_attr)\n9158:\t\t\t\t\tsysfs_btf_remove(btf_mod-\u003esysfs_attr);\n9159:\t#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)\n9160:\t\t\t\tif (btf_mod-\u003esysfs_inline_attr)\n9161:\t\t\t\t\tsysfs_btf_remove(btf_mod-\u003esysfs_inline_attr);\n9162:\t\t\t\tkvfree(btf_mod-\u003ebtf_inline_data);\n9163:\t#endif\n9164:\t\t\t\tpurge_cand_cache(btf_mod-\u003ebtf);\n9165:\t\t\t\tbtf_put(btf_mod-\u003ebtf);\n9166:\t\t\t\tkfree(btf_mod);\n9167:\t\t\t\tbreak;\n9168:\t\t\t}\n9169:\t\t\tmutex_unlock(\u0026btf_module_mutex);\n"
} |
| 37/3 | 2026/09/02 07:13 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 66800 output: 320 thoughts: 1645 Thoughts: **KMSAN Assessment: BTF Inline Data Patch** Okay, let's break this down. The patch focuses on handling new BTF types (LOC_PARAM, LOC_PROTO, LOCSEC) within `btf.c`, essentially parsing, verifying, and relocating BTF data, primarily for inline BTF information in the kernel. I need to determine if it introduces any uninitialized memory risks, which would necessitate KMSAN (Kernel Memory Sanitizer) coverage. Firstly, I'm analyzing the code touching the BTF parsing and verification logic: `btf_loc_param_check_meta`, `btf_loc_proto_check_meta`, `btf_locsec_check_meta`, `btf_parse_split`, and `btf_rebase_inline`. My focus is on allocations, data copying, and metadata handling. I'm verifying that allocations using `kzalloc_obj` are zeroing memory properly and that `kvmemdup` creates copies, and importantly, any manipulation of the data within the duplicated region happens after initialization. `btf_rebase_inline` rewrites type IDs in place, but operates on data that was properly allocated and initialized through `kvmemdup`. Secondly, the sysfs operations (`sysfs_btf_bin_attr_load`, `sysfs_btf_kernfs_open`, `sysfs_btf_kernfs_read`, etc.) are critical for how this data is exposed to userspace. I'm scrutinizing how data is read from `/sys/kernel/btf/*`. The read path uses `READ_ONCE`, and `memcpy` to copy the data, using `data_size` as a boundary. Crucially, the source `data` is from the `.BTF.inline` section in the ELF or from `kvmemdup`, which is the copied `.BTF.inline`, both of which are supposed to be initialized. There is no new kernel struct copied to user space, it is raw, known format data. Thirdly, the module loading hook and the `vmlinux.inline` initialization via `kernel/bpf/btf_vmlinux_inline.c` need review. The hook, `find_module_sections` and the inline init (`sysfs_btf_add`) are analyzed. I'm focusing on the allocation of `btf_inline_data` and the transfer of data from module sections to memory. Next, I'm methodically checking for potential vulnerabilities, addressing each point: 1. **Kernel Structures Exposed to Userspace:** The data written to `/sys/kernel/btf/*` is the raw, relocated inline BTF data, copied from ELF sections. There aren't new kernel structures being sent. No fields or padding are uninitialized. I am confident that the `kvmemdup` operation ensures proper initialization. 2. **Conditional Logic & Uninitialized Variables:** I'm carefully reviewing all the relevant functions (`btf_parse_split`, `btf_rebase_inline`, `btf_parse_module`, `btf_module_notify`, etc.) for any potential use of uninitialized variables or conditional logic dependent on such variables. All of them are properly initialized, including variables like `*relocated_inline_data`, inline_data`, and `data_size`. 3. **Allocation & Initialization:** Allocations of complex structures via `kzalloc_obj` are zeroing the data, which is fundamental to avoiding uninitialized memory reads. All of them are properly zeroed. 4. **Bounds Checks & Buffer Manipulation:** The code rigorously validates the ELF/BTF format. Checks ensure that there aren't any potential memory leaks, buffer overflows, or out-of-bounds reads/writes. Any bugs that might exist here would show up as KASAN errors during testing. The length checks, `meta_needed <= meta_left` and the use of the `data_size` boundary for reads via `sysfs_btf_kernfs_read` appear sound. Considering the userland tools, the patch updates those to handle BTF, however, the userland tooling isn't going to affect KMSAN coverage in the kernel. Therefore, after this thorough review, I'm confident that this patch doesn't introduce uninitialized memory risks. The data is properly initialized at all stages, and KMSAN isn't required to ensure safety. The primary memory safety concern is around potential data corruption, invalid format data or out-of-bounds access. The existing testing and KASAN will address those types of failures. |
| 38/3 | 2026/09/02 07:14 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch series introduces support for inline BTF (BTF_KIND_LOC_PARAM, BTF_KIND_LOC_PROTO, and BTF_KIND_LOCSEC) along with kernel module parsing and sysfs exposure of .BTF.inline sections via /sys/kernel/btf/*.\n\nAnalysis of KMSAN vs KASAN applicability:\n- Memory Allocations and Initialization: All new kernel structures (e.g., struct btf, struct btf_module, struct bin_attribute) are allocated using kzalloc_obj(), ensuring zero-initialization. BTF payload data buffers are created using kvmemdup() from existing ELF sections.\n- User-space Data Transfers: sysfs reads (sysfs_btf_kernfs_read) copy explicitly sized BTF byte streams, with offsets and lengths strictly bounded by the acquired data_size. There are no kernel structs with padding or uninitialized fields exposed to user space.\n- Logic \u0026 Variables: All local variables in the parsing, validation, and notification paths are initialized before use.\n- Potential risks in this patch series involve bounds checking of BTF sections, offset calculations, type resolution, or memory management, all of which are addressable by KASAN, not KMSAN.\n\nTherefore, this patch series does not introduce uninitialized memory risks or info-leaks, and a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch series introduces support for inline BTF (BTF_KIND_LOC_PARAM, BTF_KIND_LOC_PROTO, and BTF_KIND_LOCSEC) along with kernel module parsing and sysfs exposure of .BTF.inline sections via /sys/kernel/btf/*.\n\nAnalysis of KMSAN vs KASAN applicability:\n- Memory Allocations and Initialization: All new kernel structures (e.g., struct btf, struct btf_module, struct bin_attribute) are allocated using kzalloc_obj(), ensuring zero-initialization. BTF payload data buffers are created using kvmemdup() from existing ELF sections.\n- User-space Data Transfers: sysfs reads (sysfs_btf_kernfs_read) copy explicitly sized BTF byte streams, with offsets and lengths strictly bounded by the acquired data_size. There are no kernel structs with padding or uninitialized fields exposed to user space.\n- Logic \u0026 Variables: All local variables in the parsing, validation, and notification paths are initialized before use.\n- Potential risks in this patch series involve bounds checking of BTF sections, offset calculations, type resolution, or memory management, all of which are addressable by KASAN, not KMSAN.\n\nTherefore, this patch series does not introduce uninitialized memory risks or info-leaks, and a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|