| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/08/27 06:43 | flow | patch-triage |
2mArgs:null Results: {
"EnableConfigs": null,
"FocusSymbols": [
"btf_struct_is_composed_of",
"btf_type_is_arena_ptr",
"btf_prepare_func_args"
],
"KMSANReasoning": "The patch modifies the BPF verifier to allow kfuncs and subprograms to return structs/unions containing arena pointers by value. It updates the verifier's type checking logic and register state tracking. It does not introduce any new memory allocations, kernel-to-user data copies, or complex data structures that could expose uninitialized memory. The changes are confined to the static analysis phase of BPF programs. Standard KASAN and other bug detectors are sufficient to catch any potential memory safety issues in the verifier.",
"NeedsKMSAN": false,
"Reasoning": "The patch modifies the BPF verifier to allow returning structs or unions by value from kfuncs and global subprograms if they are composed of scalars and arena pointers (pointers with the `btf_type_tag(\"arena\")` attribute). Previously, only scalars were allowed. This is a functional change in the verifier's type checking logic and register state tracking (handling multiple return registers and arena pointers). It is reachable from userspace via the `bpf()` syscall when loading BPF programs that call such kfuncs or subprograms.",
"WorthFuzzing": true
} |
| 1/1 | 2026/08/27 06:43 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit af7e8a8a9501e5ea3bca9b8e122fdc4ecc545e63\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Thu Aug 27 06:43:34 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/Documentation/bpf/kfuncs.rst b/Documentation/bpf/kfuncs.rst\nindex 89dea6b0b0241..96bf63d344322 100644\n--- a/Documentation/bpf/kfuncs.rst\n+++ b/Documentation/bpf/kfuncs.rst\n@@ -581,20 +581,27 @@ against the arena. Larger accesses must verify the range explicitly.\n A kfunc may return a scalar, a pointer, or a small struct or union by\n value. A scalar or pointer of up to 8 bytes is returned in R0, as usual.\n \n-A struct or union returned by value must be composed only of scalars\n-(recursively), where a scalar is an integer or an enum; arrays of scalars are\n-allowed as members. Its bytes are handed back to the program as the raw\n-contents of R0 (and R2), so a pointer field would be laundered into a scalar\n-and escape the verifier's pointer provenance and reference tracking. A struct\n-or union with a pointer member is therefore rejected at load time, and so is\n-one with a floating-point member, which the ABI may not return in R0:R2 at\n-all.\n+A struct or union returned by value must be composed only of scalars and arena\n+pointers (recursively), where a scalar is an integer or an enum and an arena\n+pointer is one carrying the ``btf_type_tag(\"arena\")`` attribute; arrays of\n+either are allowed as members. Its bytes are handed back to the program as the\n+raw contents of R0 (and R2), so a member of any other pointer type would be\n+laundered into a scalar and escape the verifier's pointer provenance and\n+reference tracking. A struct or union with such a member is therefore rejected\n+at load time, and so is one with a floating-point member, which the ABI may not\n+return in R0:R2 at all.\n+\n+An arena pointer member is handed back as a scalar too, but nothing is lost by\n+that. The program must ``cast_kern()`` the value before it can be used, and the\n+verifier allows that cast on any scalar, so a laundered arena address gives the\n+program no reach it did not already have. The result is confined to the\n+program's arena in either case.\n \n A kfunc may also return a value larger than 8 bytes and up to 16 bytes -- a\n-scalar-only struct or union, or an ``__int128``. Such a value is returned\n-in the register pair R0:R2, matching the convention LLVM uses for the BPF\n-target: the first 8 bytes in R0 and the second 8 bytes in R2. A struct or\n-union of 8 bytes or less is returned in R0 alone.\n+struct or union of scalars and arena pointers, or an ``__int128``. Such a\n+value is returned in the register pair R0:R2, matching the convention LLVM\n+uses for the BPF target: the first 8 bytes in R0 and the second 8 bytes in R2.\n+A struct or union of 8 bytes or less is returned in R0 alone.\n \n ::\n \n@@ -620,10 +627,10 @@ used when the program is JITed, since the interpreter propagates only R0 out of\n a subprogram: without a JIT the return value stays in R0 alone, and a caller\n reading R2 is rejected for reading an uninitialized register. A global\n subprogram is verified in isolation, so its by-value struct or union return is\n-restricted to scalars just like a kfunc's; a static subprogram is verified\n-inline and has no such restriction. The main program is not covered: its return\n-value is the program's exit code, read out of R0 alone, so a declared upper\n-half is never looked at.\n+restricted to scalars and arena pointers just like a kfunc's; a static\n+subprogram is verified inline and has no such restriction. The main program is\n+not covered: its return value is the program's exit code, read out of R0\n+alone, so a declared upper half is never looked at.\n \n A global subprogram must leave a scalar in *every* register of the pair, so\n both halves of the returned value have to be assigned. Leaving the upper half\ndiff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h\nindex 004b06785521c..ae9f606539f41 100644\n--- a/include/linux/bpf_verifier.h\n+++ b/include/linux/bpf_verifier.h\n@@ -1488,8 +1488,15 @@ int bpf_jmp_offset(struct bpf_insn *insn);\n struct bpf_iarray *bpf_insn_successors(struct bpf_verifier_env *env, u32 idx);\n void bpf_fmt_stack_mask(char *buf, ssize_t buf_sz, u64 stack_mask);\n bool bpf_subprog_is_global(const struct bpf_verifier_env *env, int subprog);\n-bool btf_type_is_scalar_struct(struct bpf_verifier_env *env, const struct btf *btf,\n-\t\t\t const struct btf_type *t, int rec);\n+\n+/* Kinds of member a by-value struct or union may be composed of. */\n+enum btf_member_kind {\n+\tBTF_MEMBER_SCALAR\t= BIT(0), /* an int or an enum */\n+\tBTF_MEMBER_ARENA_PTR\t= BIT(1), /* a pointer carrying the \"arena\" type tag */\n+};\n+\n+bool btf_struct_is_composed_of(struct bpf_verifier_env *env, const struct btf *btf,\n+\t\t\t const struct btf_type *t, u32 member_kinds);\n \n int bpf_find_subprog(struct bpf_verifier_env *env, int off);\n bool bpf_is_throw_kfunc(struct bpf_insn *insn);\ndiff --git a/include/linux/btf.h b/include/linux/btf.h\nindex 89d5a5c4f1171..ddd0f4f32d24b 100644\n--- a/include/linux/btf.h\n+++ b/include/linux/btf.h\n@@ -235,6 +235,7 @@ struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type\n \t\t\t\t u32 field_mask, u32 value_size);\n int btf_check_and_fixup_fields(const struct btf *btf, struct btf_record *rec);\n bool btf_type_is_void(const struct btf_type *t);\n+bool btf_type_is_arena_ptr(const struct btf *btf, const struct btf_type *t);\n s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);\n s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p);\n struct btf *btf_get_module_btf(const struct module *module);\ndiff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c\nindex 91b8ce77f699a..f6d82a8fd617b 100644\n--- a/kernel/bpf/btf.c\n+++ b/kernel/bpf/btf.c\n@@ -3523,6 +3523,22 @@ static int btf_type_tag_walk(const struct btf *btf,\n \treturn 0;\n }\n \n+bool btf_type_is_arena_ptr(const struct btf *btf, const struct btf_type *t)\n+{\n+\tif (!btf_type_is_ptr(t))\n+\t\treturn false;\n+\n+\tfor (t = btf_type_by_id(btf, t-\u003etype); btf_type_is_modifier(t);\n+\t t = btf_type_by_id(btf, t-\u003etype)) {\n+\t\tif (!btf_type_is_type_tag(t) || btf_type_kflag(t))\n+\t\t\tcontinue;\n+\t\tif (!strcmp(__btf_name_by_offset(btf, t-\u003ename_off), \"arena\"))\n+\t\t\treturn true;\n+\t}\n+\n+\treturn false;\n+}\n+\n static int btf_find_kptr(const struct btf *btf, const struct btf_type *t,\n \t\t\t u32 off, int sz, struct btf_field_info *info, u32 field_mask)\n {\n@@ -7927,51 +7943,19 @@ static int btf_scan_decl_tags(struct bpf_verifier_env *env,\n \treturn 0;\n }\n \n-static int btf_scan_type_tags(struct bpf_verifier_env *env,\n-\t\t\t const struct btf *btf, u32 type_id,\n-\t\t\t u32 *tags)\n+static void btf_scan_type_tags(const struct btf *btf, u32 type_id, u32 *tags)\n {\n-\tstatic const struct btf_type_tag_match func_type_tags[] = {\n-\t\t{ \"arena\", ARG_TAG_ARENA },\n-\t};\n-\tstruct btf_type_tag_walk_ctx ctx;\n-\tconst struct btf_type *t;\n-\tint err;\n-\n \t/* Find the first pointer type in the chain. */\n-\tt = btf_type_skip_modifiers(btf, type_id, NULL);\n+\tconst struct btf_type *t = btf_type_skip_modifiers(btf, type_id, NULL);\n \n-\t/*\n-\t * We currently reject type tags on non-pointer types,\n-\t * which neither LLVM nor GCC support anyway.\n-\t */\n-\tif (!t || !btf_type_is_ptr(t))\n-\t\treturn 0;\n-\n-\tctx.t = t;\n-\terr = btf_type_tag_walk(btf, \u0026ctx, func_type_tags,\n-\t\t\t\tARRAY_SIZE(func_type_tags));\n-\tif (err) {\n-\t\tbpf_log(\u0026env-\u003elog,\n-\t\t\t\"function signature member has multiple type tags\\n\");\n-\t\treturn err;\n-\t}\n-\t*tags |= ctx.res;\n-\n-\treturn 0;\n+\tif (btf_type_is_arena_ptr(btf, t))\n+\t\t*tags |= ARG_TAG_ARENA;\n }\n \n /* Check whether the type is a valid return type. */\n static int btf_validate_return_type(struct bpf_verifier_env *env, struct btf *btf,\n \t\tconst struct btf_type *t, int subprog, bool is_global)\n {\n-\tu32 tags = 0;\n-\tint err;\n-\n-\terr = btf_scan_type_tags(env, btf, t-\u003etype, \u0026tags);\n-\tif (err)\n-\t\treturn err;\n-\n \tt = btf_type_skip_modifiers(btf, t-\u003etype, NULL);\n \n \t/*\n@@ -7979,7 +7963,7 @@ static int btf_validate_return_type(struct bpf_verifier_env *env, struct btf *bt\n \t * General arena variables are not allowed, since it makes no sense to return by value\n \t * a variable that's on the heap in the first place.\n \t */\n-\tif (subprog \u0026\u0026 (tags \u0026 ARG_TAG_ARENA) \u0026\u0026 btf_type_is_ptr(t))\n+\tif (subprog \u0026\u0026 btf_type_is_arena_ptr(btf, t))\n \t\treturn 0;\n \n \t/* We always accept void or scalars. */\n@@ -7989,13 +7973,22 @@ static int btf_validate_return_type(struct bpf_verifier_env *env, struct btf *bt\n \tif (btf_type_is_struct(t) \u0026\u0026 t-\u003esize \u003c= 16) {\n \t\t/*\n \t\t * A global function's caller models the return as an opaque\n-\t\t * scalar pair, so it may only return scalars by value. A local\n-\t\t * function is verified inline, so a pointer field stays tracked\n-\t\t * and needs no such restriction.\n+\t\t * scalar pair, so a pointer member would be laundered into a\n+\t\t * scalar and escape provenance and reference tracking. Only\n+\t\t * scalars and arena pointers are allowed: an arena pointer has\n+\t\t * no provenance to lose, since a program may already derive one\n+\t\t * from any scalar with addr_space_cast(), which confines the\n+\t\t * result to the arena. A local function is verified inline, so\n+\t\t * its caller receives the real register state and any member is\n+\t\t * fine.\n \t\t */\n \t\tbool local_func = subprog \u0026\u0026 !is_global;\n+\t\tu32 member_kinds = BTF_MEMBER_SCALAR;\n+\n+\t\tif (subprog)\n+\t\t\tmember_kinds |= BTF_MEMBER_ARENA_PTR;\n \n-\t\tif (local_func || btf_type_is_scalar_struct(env, btf, t, 0))\n+\t\tif (local_func || btf_struct_is_composed_of(env, btf, t, member_kinds))\n \t\t\treturn 0;\n \t}\n \n@@ -8091,7 +8084,8 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog)\n \t\tif (is_global) {\n \t\t\tbpf_log(log,\n \t\t\t\t\"Global function %s() has unsupported return type. \"\n-\t\t\t\t\"Only void, scalar, or a scalar-only struct/union up to 16 bytes is supported.\\n\",\n+\t\t\t\t\"Only void, a scalar, an arena pointer, or a struct/union of \"\n+\t\t\t\t\"scalars and arena pointers up to 16 bytes is supported.\\n\",\n \t\t\t\ttname);\n \t\t}\n \t\treturn err;\n@@ -8106,9 +8100,7 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog)\n \t\tif (err)\n \t\t\treturn err;\n \n-\t\terr = btf_scan_type_tags(env, btf, args[i].type, \u0026tags);\n-\t\tif (err)\n-\t\t\treturn err;\n+\t\tbtf_scan_type_tags(btf, args[i].type, \u0026tags);\n \n \t\tt = btf_type_by_id(btf, args[i].type);\n \t\twhile (btf_type_is_modifier(t))\ndiff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c\nindex e036ae20bf6b9..9a36808136c9a 100644\n--- a/kernel/bpf/verifier.c\n+++ b/kernel/bpf/verifier.c\n@@ -9995,9 +9995,14 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \t\t */\n \t\tif (!returns_void) {\n \t\t\tnregs = bpf_ret_reg_pair(env, subprog) ? 2 : 1;\n-\t\t\tfor (i = 0; i \u003c nregs; i++)\n-\t\t\t\tmark_reg_unknown(env, caller-\u003eregs, ret_regs[i]);\n+\t\t\tmark_reg_unknown(env, caller-\u003eregs, ret_regs[0]);\n \t\t\tbpf_diag_mod_end(env);\n+\t\t\tfor (i = 1; i \u003c nregs; i++) {\n+\t\t\t\tbpf_diag_mod_begin(env, \u0026caller-\u003eregs[ret_regs[i]], NULL,\n+\t\t\t\t\t\t BPF_DIAG_MOD_WRITE);\n+\t\t\t\tmark_reg_unknown(env, caller-\u003eregs, ret_regs[i]);\n+\t\t\t\tbpf_diag_mod_end(env);\n+\t\t\t}\n \t\t}\n \n \t\tif (env-\u003esubprog_info[subprog].might_throw) {\n@@ -10403,10 +10408,14 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)\n \t\t * return to the caller whatever the callee had in the\n \t\t * return register(s)\n \t\t */\n-\t\tbpf_diag_mod_begin(env, \u0026caller-\u003eregs[BPF_REG_0], r0, BPF_DIAG_MOD_WRITE);\n-\t\tfor (i = 0; i \u003c nregs; i++)\n-\t\t\tcaller-\u003eregs[ret_regs[i]] = callee-\u003eregs[ret_regs[i]];\n-\t\tbpf_diag_mod_end(env);\n+\t\tfor (i = 0; i \u003c nregs; i++) {\n+\t\t\tu32 regno = ret_regs[i];\n+\n+\t\t\tbpf_diag_mod_begin(env, \u0026caller-\u003eregs[regno], \u0026callee-\u003eregs[regno],\n+\t\t\t\t\t BPF_DIAG_MOD_WRITE);\n+\t\t\tcaller-\u003eregs[regno] = callee-\u003eregs[regno];\n+\t\t\tbpf_diag_mod_end(env);\n+\t\t}\n \t}\n \n \t/* for callbacks like bpf_loop or bpf_for_each_map_elem go back to callsite,\n@@ -11342,15 +11351,33 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn\n \n /*\n * Mark the register(s) holding a @size byte kfunc return value as unknown\n- * scalars. Both halves of a register pair are treated the same way.\n+ * scalars and return how many registers that took. Both halves of a register\n+ * pair are treated the same way.\n+ *\n+ * R0 is written in the diagnostic modification scope the caller opened before\n+ * the caller-saved scrub, since R0 gets no scrub record of its own and that\n+ * scope is what carries its pre-call value into the history. A scope tracks a\n+ * single register and does not nest, so close it before recording the rest of\n+ * the return value. A value that fits in R0 leaves the caller's scope open for\n+ * the caller to close, so a later write to R0 still lands in it.\n */\n-static void mark_kfunc_ret_regs(struct bpf_verifier_env *env,\n-\t\t\t\tstruct bpf_reg_state *regs, u32 size)\n+static u32 mark_kfunc_ret_regs(struct bpf_verifier_env *env,\n+\t\t\t struct bpf_reg_state *regs, u32 size)\n {\n \tu32 i, nregs = ret_regs_cnt(size);\n \n-\tfor (i = 0; i \u003c nregs; i++)\n+\tmark_reg_unknown(env, regs, ret_regs[0]);\n+\tif (nregs == 1)\n+\t\treturn nregs;\n+\n+\tbpf_diag_mod_end(env);\n+\tfor (i = 1; i \u003c nregs; i++) {\n+\t\tbpf_diag_mod_begin(env, \u0026regs[ret_regs[i]], NULL, BPF_DIAG_MOD_WRITE);\n \t\tmark_reg_unknown(env, regs, ret_regs[i]);\n+\t\tbpf_diag_mod_end(env);\n+\t}\n+\n+\treturn nregs;\n }\n \n static bool is_kfunc_acquire(struct bpf_call_arg_meta *meta)\n@@ -11619,10 +11646,33 @@ static bool is_kfunc_arg_implicit(const struct bpf_call_arg_meta *meta, u32 arg_\n \treturn argn \u003c= arg_idx;\n }\n \n-/* Returns true if struct is composed of scalars, 4 levels of nesting allowed */\n-bool btf_type_is_scalar_struct(struct bpf_verifier_env *env,\n-\t\t\t const struct btf *btf,\n-\t\t\t const struct btf_type *t, int rec)\n+#define BTF_MEMBER_MAX_DEPTH\t4\n+#define BTF_MEMBER_PATH_LEN\t64\n+\n+struct btf_member_path {\n+\tconst struct btf_member *member[BTF_MEMBER_MAX_DEPTH];\n+\tint depth;\n+\tbool too_deep;\n+};\n+\n+static bool btf_member_kind_allowed(const struct btf *btf, const struct btf_type *t,\n+\t\t\t\t u32 member_kinds)\n+{\n+\tif ((member_kinds \u0026 BTF_MEMBER_SCALAR) \u0026\u0026 btf_type_is_scalar(t))\n+\t\treturn true;\n+\tif ((member_kinds \u0026 BTF_MEMBER_ARENA_PTR) \u0026\u0026 btf_type_is_arena_ptr(btf, t))\n+\t\treturn true;\n+\treturn false;\n+}\n+\n+/*\n+ * Returns true if every member of struct @t is of a kind listed in\n+ * @member_kinds, BTF_MEMBER_MAX_DEPTH levels of nesting allowed. An array\n+ * member counts as its element type.\n+ */\n+static bool btf_struct_member_walk(struct bpf_verifier_env *env, const struct btf *btf,\n+\t\t\t\t const struct btf_type *t, u32 member_kinds, int rec,\n+\t\t\t\t struct btf_member_path *path)\n {\n \tconst struct btf_type *member_type;\n \tconst struct btf_member *member;\n@@ -11636,27 +11686,65 @@ bool btf_type_is_scalar_struct(struct bpf_verifier_env *env,\n \n \t\tmember_type = btf_type_skip_modifiers(btf, member-\u003etype, NULL);\n \t\tif (btf_type_is_struct(member_type)) {\n-\t\t\tif (rec \u003e= 3) {\n+\t\t\tif (rec \u003e= BTF_MEMBER_MAX_DEPTH - 1) {\n \t\t\t\tverbose(env, \"max struct nesting depth exceeded\\n\");\n+\t\t\t\tif (path)\n+\t\t\t\t\tpath-\u003etoo_deep = true;\n \t\t\t\treturn false;\n \t\t\t}\n-\t\t\tif (!btf_type_is_scalar_struct(env, btf, member_type, rec + 1))\n-\t\t\t\treturn false;\n+\t\t\tif (!btf_struct_member_walk(env, btf, member_type, member_kinds,\n+\t\t\t\t\t\t rec + 1, path))\n+\t\t\t\tgoto bad_path;\n \t\t\tcontinue;\n \t\t}\n \t\tif (btf_type_is_array(member_type)) {\n \t\t\tarray = btf_array(member_type);\n \t\t\tif (!array-\u003enelems)\n-\t\t\t\treturn false;\n+\t\t\t\tgoto bad_member;\n \t\t\tmember_type = btf_type_skip_modifiers(btf, array-\u003etype, NULL);\n-\t\t\tif (!btf_type_is_scalar(member_type))\n-\t\t\t\treturn false;\n-\t\t\tcontinue;\n \t\t}\n-\t\tif (!btf_type_is_scalar(member_type))\n-\t\t\treturn false;\n+\t\tif (!btf_member_kind_allowed(btf, member_type, member_kinds))\n+\t\t\tgoto bad_member;\n \t}\n \treturn true;\n+\n+bad_member:\n+\tif (path)\n+\t\tpath-\u003edepth = rec + 1;\n+bad_path:\n+\tif (path \u0026\u0026 path-\u003edepth)\n+\t\tpath-\u003emember[rec] = member;\n+\treturn false;\n+}\n+\n+bool btf_struct_is_composed_of(struct bpf_verifier_env *env,\n+\t\t\t const struct btf *btf,\n+\t\t\t const struct btf_type *t, u32 member_kinds)\n+{\n+\treturn btf_struct_member_walk(env, btf, t, member_kinds, 0, NULL);\n+}\n+\n+static bool btf_type_is_scalar_struct(struct bpf_verifier_env *env,\n+\t\t\t\t const struct btf *btf,\n+\t\t\t\t const struct btf_type *t)\n+{\n+\treturn btf_struct_is_composed_of(env, btf, t, BTF_MEMBER_SCALAR);\n+}\n+\n+static void btf_member_path_str(const struct btf *btf, const struct btf_member_path *path,\n+\t\t\t\tchar *buf, size_t buf_sz)\n+{\n+\tsize_t len = 0;\n+\tint i;\n+\n+\tbuf[0] = '\\0';\n+\tfor (i = 0; i \u003c path-\u003edepth; i++) {\n+\t\tconst char *name = btf_name_by_offset(btf, path-\u003emember[i]-\u003ename_off);\n+\n+\t\tif (!name || !name[0])\n+\t\t\tcontinue;\n+\t\tlen += scnprintf(buf + len, buf_sz - len, \"%s%s\", len ? \".\" : \"\", name);\n+\t}\n }\n \n enum kfunc_ptr_arg_type {\n@@ -12039,7 +12127,7 @@ get_kfunc_arg_type(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,\n \t\t (is_kfunc_arg_mem_size(meta-\u003ebtf, \u0026args[arg + 1]) ||\n \t\t is_kfunc_arg_const_mem_size(meta-\u003ebtf, \u0026args[arg + 1]))) {\n \t\tif (!btf_type_is_void(ref_t) \u0026\u0026 !btf_type_is_scalar(ref_t) \u0026\u0026\n-\t\t !btf_type_is_scalar_struct(env, meta-\u003ebtf, ref_t, 0)) {\n+\t\t !btf_type_is_scalar_struct(env, meta-\u003ebtf, ref_t)) {\n \t\t\tverbose(env, \"%s pointer type %s %s must point to void, scalar, or struct with scalar\\n\",\n \t\t\t\treg_arg_name(env, argno), btf_type_str(ref_t), ref_tname);\n \t\t\treturn -EINVAL;\n@@ -12055,7 +12143,7 @@ get_kfunc_arg_type(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,\n \t\t * scalars. The access size is derived from the pointed-to BTF type.\n \t\t */\n \t\tif (!btf_type_is_scalar(ref_t) \u0026\u0026\n-\t\t !btf_type_is_scalar_struct(env, meta-\u003ebtf, ref_t, 0)) {\n+\t\t !btf_type_is_scalar_struct(env, meta-\u003ebtf, ref_t)) {\n \t\t\tverbose(env, \"%s pointer type %s %s must point to scalar, or struct with scalar\\n\",\n \t\t\t\treg_arg_name(env, argno), btf_type_str(ref_t), ref_tname);\n \t\t\treturn -EINVAL;\n@@ -13111,7 +13199,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me\n \t\t\t\tbreak;\n \t\t\t}\n \n-\t\t\tif (!btf_type_is_scalar_struct(env, meta-\u003ebtf, ref_t, 0)) {\n+\t\t\tif (!btf_type_is_scalar_struct(env, meta-\u003ebtf, ref_t)) {\n \t\t\t\tenum bpf_reg_type reg2btf_type = lookup_reg2btf_ids(ref_id);\n \t\t\t\tconst char *expected_type;\n \n@@ -13653,7 +13741,7 @@ static int check_special_kfunc(struct bpf_verifier_env *env, struct bpf_call_arg\n \n \t\tstruct_meta = btf_find_struct_meta(ret_btf, ret_btf_id);\n \t\tif (is_bpf_percpu_obj_new_kfunc(meta-\u003efunc_id)) {\n-\t\t\tif (!btf_type_is_scalar_struct(env, ret_btf, ret_t, 0)) {\n+\t\t\tif (!btf_type_is_scalar_struct(env, ret_btf, ret_t)) {\n \t\t\t\tverbose(env, \"bpf_percpu_obj_new type ID argument must be of a struct of scalars\\n\");\n \t\t\t\treturn -EINVAL;\n \t\t\t}\n@@ -13767,7 +13855,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \tstruct bpf_insn_aux_data *insn_aux;\n \tconst char *operation;\n \tint err, insn_idx = *insn_idx_p;\n-\tu32 i, nargs, ptr_type_id;\n+\tu32 i, nargs, ptr_type_id, ret_nregs = 1;\n \tstruct bpf_kfunc_desc *desc;\n \tstruct btf *desc_btf;\n \tint id;\n@@ -14021,25 +14109,59 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \t}\n \n \tif (btf_type_is_scalar(t)) {\n-\t\tmark_kfunc_ret_regs(env, regs, t-\u003esize);\n+\t\tret_nregs = mark_kfunc_ret_regs(env, regs, t-\u003esize);\n \t\tif (meta.btf == btf_vmlinux \u0026\u0026 (meta.func_id == special_kfunc_list[KF_bpf_res_spin_lock] ||\n \t\t meta.func_id == special_kfunc_list[KF_bpf_res_spin_lock_irqsave]))\n \t\t\t__mark_reg_const_zero(env, \u0026regs[BPF_REG_0]);\n \t} else if (btf_type_is_struct(t)) {\n+\t\tstruct btf_member_path path = {};\n+\t\tconst char *member_note = \"\";\n+\n \t\t/*\n \t\t * The returned struct comes back as raw register bits modeled\n-\t\t * as an unknown scalar, so it must contain only scalars:\n-\t\t * otherwise a pointer field would be laundered into a scalar\n-\t\t * and escape provenance and reference tracking.\n+\t\t * as an unknown scalar, so a pointer member would be laundered\n+\t\t * into a scalar and escape provenance and reference tracking.\n+\t\t * Only scalars and arena pointers are allowed: an arena pointer\n+\t\t * has no provenance to lose, since a program may already derive\n+\t\t * one from any scalar with addr_space_cast(), which confines the\n+\t\t * result to the arena.\n \t\t */\n-\t\tif (!btf_type_is_scalar_struct(env, desc_btf, t, 0)) {\n+\t\tif (!btf_struct_member_walk(env, desc_btf, t,\n+\t\t\t\t\t BTF_MEMBER_SCALAR | BTF_MEMBER_ARENA_PTR, 0, \u0026path)) {\n \t\t\tverbose(env,\n-\t\t\t\t\"kernel function %s returns %s %s that is not composed of scalars\\n\",\n+\t\t\t\t\"kernel function %s returns %s %s that is not composed of scalars or arena pointers\\n\",\n \t\t\t\tfunc_name, btf_type_str(t),\n \t\t\t\tbtf_name_by_offset(desc_btf, t-\u003ename_off));\n+\t\t\tif (path.too_deep) {\n+\t\t\t\tmember_note = bpf_diag_fmt(\n+\t\t\t\t\tenv, \" It nests structs more than %d levels deep.\",\n+\t\t\t\t\tBTF_MEMBER_MAX_DEPTH);\n+\t\t\t} else if (path.depth) {\n+\t\t\t\tconst struct btf_member *bad = path.member[path.depth - 1];\n+\t\t\t\tchar bad_name[BTF_MEMBER_PATH_LEN];\n+\t\t\t\tconst struct btf_type *bad_type;\n+\n+\t\t\t\tbtf_member_path_str(desc_btf, \u0026path, bad_name, sizeof(bad_name));\n+\t\t\t\tbad_type = btf_type_skip_modifiers(desc_btf, bad-\u003etype, NULL);\n+\t\t\t\tverbose(env, \"member '%s' has type %s\\n\", bad_name,\n+\t\t\t\t\tbtf_type_str(bad_type));\n+\t\t\t\tmember_note = bpf_diag_fmt(\n+\t\t\t\t\tenv,\n+\t\t\t\t\t\" Its member '%s' is %s, not a scalar or an arena pointer.\",\n+\t\t\t\t\tbad_name, btf_type_str(bad_type));\n+\t\t\t}\n+\t\t\tbpf_diag_program_structure(\n+\t\t\t\tenv, insn_idx, \"unsupported kernel function return type\",\n+\t\t\t\t\"Call a kernel function that returns only scalars or arena pointers by value.\",\n+\t\t\t\t\"%s() returns %s %s by value.%s \"\n+\t\t\t\t\"Only kfuncs returning scalar values or arena pointers, or \"\n+\t\t\t\t\"structures composed of scalar values or arena pointers are \"\n+\t\t\t\t\"supported.\",\n+\t\t\t\tfunc_name, btf_type_str(t),\n+\t\t\t\tbtf_name_by_offset(desc_btf, t-\u003ename_off), member_note);\n \t\t\treturn -EINVAL;\n \t\t}\n-\t\tmark_kfunc_ret_regs(env, regs, t-\u003esize);\n+\t\tret_nregs = mark_kfunc_ret_regs(env, regs, t-\u003esize);\n \t} else if (btf_type_is_ptr(t)) {\n \t\tptr_type = btf_type_skip_modifiers(desc_btf, t-\u003etype, \u0026ptr_type_id);\n \t\terr = check_special_kfunc(env, \u0026meta, regs, insn_aux, ptr_type, desc_btf);\n@@ -14170,7 +14292,8 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \t * Record R0 before process_iter_next_call() snapshots the alternate\n \t * iterator path's diagnostic position.\n \t */\n-\tbpf_diag_mod_end(env);\n+\tif (ret_nregs == 1)\n+\t\tbpf_diag_mod_end(env);\n \n \tif (bpf_is_iter_next_kfunc(\u0026meta)) {\n \t\terr = process_iter_next_call(env, insn_idx, \u0026meta);\ndiff --git a/tools/testing/selftests/bpf/prog_tests/aggregate_ret.c b/tools/testing/selftests/bpf/prog_tests/aggregate_ret.c\nindex e0b94ed10f941..07d9d6e1d6b88 100644\n--- a/tools/testing/selftests/bpf/prog_tests/aggregate_ret.c\n+++ b/tools/testing/selftests/bpf/prog_tests/aggregate_ret.c\n@@ -1,11 +1,53 @@\n // SPDX-License-Identifier: GPL-2.0\n /* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n #include \u003ctest_progs.h\u003e\n+#include \u003cbpf/btf.h\u003e\n #include \"aggregate_ret_func.skel.h\"\n #include \"aggregate_ret_kfunc.skel.h\"\n+#include \"aggregate_ret_kfunc_arena.skel.h\"\n+\n+static bool testmod_has_arena_tagged_member(void)\n+{\n+\tstruct btf *vmlinux_btf, *module_btf = NULL;\n+\tconst struct btf_type *t;\n+\tbool tagged = false;\n+\t__s32 id;\n+\n+\tvmlinux_btf = btf__load_vmlinux_btf();\n+\tif (!vmlinux_btf)\n+\t\treturn false;\n+\n+\tmodule_btf = btf__load_module_btf(\"bpf_testmod\", vmlinux_btf);\n+\tif (!module_btf)\n+\t\tgoto out;\n+\n+\t/* prog_test_ret_arena::a is 'void __arena_tag *': PTR -\u003e TYPE_TAG -\u003e void */\n+\tid = btf__find_by_name_kind(module_btf, \"prog_test_ret_arena\", BTF_KIND_STRUCT);\n+\tif (id \u003c= 0)\n+\t\tgoto out;\n+\n+\tt = btf__type_by_id(module_btf, btf_members(btf__type_by_id(module_btf, id))[0].type);\n+\tif (!t || !btf_is_ptr(t))\n+\t\tgoto out;\n+\n+\tt = btf__type_by_id(module_btf, t-\u003etype);\n+\ttagged = t \u0026\u0026 btf_is_type_tag(t) \u0026\u0026\n+\t\t !strcmp(btf__name_by_offset(module_btf, t-\u003ename_off), \"arena\");\n+\n+out:\n+\tbtf__free(module_btf);\n+\tbtf__free(vmlinux_btf);\n+\n+\treturn tagged;\n+}\n \n void test_aggregate_ret(void)\n {\n \tRUN_TESTS(aggregate_ret_func);\n \tRUN_TESTS(aggregate_ret_kfunc);\n+\n+\tif (testmod_has_arena_tagged_member())\n+\t\tRUN_TESTS(aggregate_ret_kfunc_arena);\n+\telse\n+\t\ttest__skip();\n }\ndiff --git a/tools/testing/selftests/bpf/progs/aggregate_ret_func.c b/tools/testing/selftests/bpf/progs/aggregate_ret_func.c\nindex 6f66fc822cede..f0077d951a25d 100644\n--- a/tools/testing/selftests/bpf/progs/aggregate_ret_func.c\n+++ b/tools/testing/selftests/bpf/progs/aggregate_ret_func.c\n@@ -2,6 +2,7 @@\n /* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n #include \u003clinux/bpf.h\u003e\n #include \u003cbpf/bpf_helpers.h\u003e\n+#include \u003cbpf_arena_common.h\u003e\n #include \"bpf_misc.h\"\n \n typedef unsigned __int128 u128;\n@@ -234,4 +235,149 @@ __naked int aggregate_ret_global_union_ptr_fail(void)\n \n #endif\n \n+/*\n+ * gcc returns a by-value struct through a hidden pointer, and emits the\n+ * 'r0 = r1' returning it after the __naked body's exit, leaving the\n+ * subprogram falling through. Build these with clang only.\n+ */\n+#if defined(__clang__)\n+\n+struct arena_pair {\n+\tvoid __arena *lo;\n+\tvoid __arena *hi;\n+};\n+\n+struct arena_and_scalar {\n+\tvoid __arena *p;\n+\t__u64 x;\n+};\n+\n+struct arena_array {\n+\tvoid __arena *p[2];\n+};\n+\n+struct arena_single {\n+\tvoid __arena *p;\n+};\n+\n+union arena_upair {\n+\tvoid __arena *p;\n+\t__u64 halves[2];\n+};\n+\n+__naked struct arena_pair global_ret_arena_pair(void)\n+{\n+\tasm volatile (\n+\t\"r0 = 0;\"\n+\t\"r2 = 0;\"\n+\t\"exit;\"\n+\t);\n+}\n+\n+SEC(\"tc\")\n+__load_if_JITed()\n+__success __retval(0)\n+__naked int aggregate_ret_global_arena_pair(void)\n+{\n+\tasm volatile (\n+\t\"call %[global_ret_arena_pair];\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\t:\n+\t: __imm(global_ret_arena_pair)\n+\t: __clobber_all);\n+}\n+\n+__naked struct arena_and_scalar global_ret_arena_and_scalar(void)\n+{\n+\tasm volatile (\n+\t\"r0 = 0;\"\n+\t\"r2 = 0;\"\n+\t\"exit;\"\n+\t);\n+}\n+\n+SEC(\"tc\")\n+__load_if_JITed()\n+__success __retval(0)\n+__naked int aggregate_ret_global_arena_and_scalar(void)\n+{\n+\tasm volatile (\n+\t\"call %[global_ret_arena_and_scalar];\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\t:\n+\t: __imm(global_ret_arena_and_scalar)\n+\t: __clobber_all);\n+}\n+\n+__naked struct arena_array global_ret_arena_array(void)\n+{\n+\tasm volatile (\n+\t\"r0 = 0;\"\n+\t\"r2 = 0;\"\n+\t\"exit;\"\n+\t);\n+}\n+\n+SEC(\"tc\")\n+__load_if_JITed()\n+__success __retval(0)\n+__naked int aggregate_ret_global_arena_array(void)\n+{\n+\tasm volatile (\n+\t\"call %[global_ret_arena_array];\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\t:\n+\t: __imm(global_ret_arena_array)\n+\t: __clobber_all);\n+}\n+\n+__naked struct arena_single global_ret_arena_single(void)\n+{\n+\tasm volatile (\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\t);\n+}\n+\n+SEC(\"tc\")\n+__success __retval(0)\n+__naked int aggregate_ret_global_arena_single(void)\n+{\n+\tasm volatile (\n+\t\"call %[global_ret_arena_single];\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\t:\n+\t: __imm(global_ret_arena_single)\n+\t: __clobber_all);\n+}\n+\n+__naked union arena_upair global_ret_arena_union(void)\n+{\n+\tasm volatile (\n+\t\"r0 = 0;\"\n+\t\"r2 = 0;\"\n+\t\"exit;\"\n+\t);\n+}\n+\n+SEC(\"tc\")\n+__load_if_JITed()\n+__success __retval(0)\n+__naked int aggregate_ret_global_arena_union(void)\n+{\n+\tasm volatile (\n+\t\"call %[global_ret_arena_union];\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\t:\n+\t: __imm(global_ret_arena_union)\n+\t: __clobber_all);\n+}\n+\n+#endif\n+\n char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/progs/aggregate_ret_kfunc.c b/tools/testing/selftests/bpf/progs/aggregate_ret_kfunc.c\nindex d6b422ae9784b..e9c82df8efb26 100644\n--- a/tools/testing/selftests/bpf/progs/aggregate_ret_kfunc.c\n+++ b/tools/testing/selftests/bpf/progs/aggregate_ret_kfunc.c\n@@ -18,6 +18,8 @@ void __kfunc_btf_root(void)\n \t: \"r\"(\u0026bpf_kfunc_call_test_i128),\n \t \"r\"(\u0026bpf_kfunc_call_test_ret_fastcall),\n \t \"r\"(\u0026bpf_kfunc_call_test_ret_ptr),\n+\t \"r\"(\u0026bpf_kfunc_call_test_ret_nested),\n+\t \"r\"(\u0026bpf_kfunc_call_test_ret_deep),\n \t \"r\"(\u0026bpf_kfunc_call_test_ret_ii),\n \t \"r\"(\u0026bpf_kfunc_call_test_ret_big));\n }\n@@ -71,7 +73,8 @@ __naked int aggregate_ret_kfunc_fastcall_fail(void)\n \n SEC(\"tc\")\n __arch_x86_64 __arch_arm64\n-__failure __msg(\"is not composed of scalars\")\n+__failure __msg(\"is not composed of scalars or arena pointers\")\n+__msg(\"member 'p' has type PTR\")\n __naked int aggregate_ret_kfunc_ptr_fail(void)\n {\n \tasm volatile (\n@@ -84,6 +87,37 @@ __naked int aggregate_ret_kfunc_ptr_fail(void)\n \t: __clobber_all);\n }\n \n+SEC(\"tc\")\n+__arch_x86_64 __arch_arm64\n+__failure __msg(\"is not composed of scalars or arena pointers\")\n+__msg(\"member 'in.p' has type PTR\")\n+__naked int aggregate_ret_kfunc_nested_ptr_fail(void)\n+{\n+\tasm volatile (\n+\t\"r1 = 0;\"\n+\t\"call %[bpf_kfunc_call_test_ret_nested];\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\t:\n+\t: __imm(bpf_kfunc_call_test_ret_nested)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"tc\")\n+__arch_x86_64 __arch_arm64\n+__failure __msg(\"max struct nesting depth exceeded\")\n+__naked int aggregate_ret_kfunc_too_deep_fail(void)\n+{\n+\tasm volatile (\n+\t\"r1 = 0;\"\n+\t\"call %[bpf_kfunc_call_test_ret_deep];\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\t:\n+\t: __imm(bpf_kfunc_call_test_ret_deep)\n+\t: __clobber_all);\n+}\n+\n SEC(\"tc\")\n __arch_x86_64 __arch_arm64\n __failure __msg(\"R2 !read_ok\")\ndiff --git a/tools/testing/selftests/bpf/progs/aggregate_ret_kfunc_arena.c b/tools/testing/selftests/bpf/progs/aggregate_ret_kfunc_arena.c\nnew file mode 100644\nindex 0000000000000..94c35e1b547cd\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/aggregate_ret_kfunc_arena.c\n@@ -0,0 +1,129 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+#include \u003cvmlinux.h\u003e\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \u003cbpf_arena_common.h\u003e\n+#include \"bpf_misc.h\"\n+#include \"../test_kmods/bpf_testmod_kfunc.h\"\n+\n+#if defined(__clang_major__) \u0026\u0026 __clang_major__ \u003e= 23\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_ARENA);\n+\t__uint(map_flags, BPF_F_MMAPABLE);\n+\t__uint(max_entries, 2);\n+} arena SEC(\".maps\");\n+\n+/*\n+ * A returned member carries the arena type tag but not the address space\n+ * qualifier, so the program casts it into the arena address space itself\n+ * rather than the compiler doing it.\n+ */\n+#define arena_ptr(p) ((u32 volatile __arena *)(p))\n+\n+SEC(\"syscall\")\n+__arch_x86_64 __arch_arm64\n+__load_if_JITed()\n+__success __retval(0)\n+int aggregate_ret_kfunc_arena(void *ctx)\n+{\n+\tu32 volatile __arena *page = bpf_arena_alloc_pages(\u0026arena, NULL, 1, NUMA_NO_NODE, 0);\n+\tu32 volatile __arena *a, *b;\n+\tstruct prog_test_ret_arena r;\n+\n+\tif (!page)\n+\t\treturn 1;\n+\n+\t/* Both halves come back in R0:R2, pointing at page and page + 4. */\n+\tr = bpf_kfunc_call_test_ret_arena((u64)page);\n+\tif (!r.a || !r.b)\n+\t\treturn 2;\n+\n+\ta = arena_ptr(r.a);\n+\tb = arena_ptr(r.b);\n+\t*a = 1;\n+\t*b = 2;\n+\tif (*a != 1)\n+\t\treturn 3;\n+\tif (*b != 2)\n+\t\treturn 4;\n+\n+\t/* The halves are the first two slots of the page the program allocated. */\n+\tpage[0] = 7;\n+\tif (*a != 7)\n+\t\treturn 5;\n+\tpage[1] = 9;\n+\tif (*b != 9)\n+\t\treturn 6;\n+\n+\treturn 0;\n+}\n+\n+SEC(\"syscall\")\n+__arch_x86_64 __arch_arm64\n+__load_if_JITed()\n+__success __retval(0)\n+int aggregate_ret_kfunc_arena_mixed(void *ctx)\n+{\n+\tu32 __arena *page = bpf_arena_alloc_pages(\u0026arena, NULL, 1, NUMA_NO_NODE, 0);\n+\tstruct prog_test_ret_arena_mixed r;\n+\tu32 volatile __arena *p;\n+\n+\tif (!page)\n+\t\treturn 1;\n+\n+\t/* An arena pointer in R0 beside a scalar in R2. */\n+\tr = bpf_kfunc_call_test_ret_arena_mixed((u64)page);\n+\tif (!r.p)\n+\t\treturn 2;\n+\tif (r.tag != 0xbeef)\n+\t\treturn 3;\n+\n+\tp = arena_ptr(r.p);\n+\t*p = 3;\n+\tif (*p != 3)\n+\t\treturn 4;\n+\n+\treturn 0;\n+}\n+\n+SEC(\"syscall\")\n+__arch_x86_64 __arch_arm64\n+__failure __msg(\"is not composed of scalars or arena pointers\")\n+__msg(\"member 'b' has type PTR\")\n+int aggregate_ret_kfunc_arena_untagged_fail(void *ctx)\n+{\n+\tstruct prog_test_ret_arena_untagged r;\n+\n+\tr = bpf_kfunc_call_test_ret_arena_untagged(0);\n+\n+\treturn r.a == r.b;\n+}\n+\n+SEC(\"syscall\")\n+__arch_x86_64 __arch_arm64\n+__failure __msg(\"is not composed of scalars or arena pointers\")\n+__msg(\"member 'b' has type PTR\")\n+int aggregate_ret_kfunc_arena_union_fail(void *ctx)\n+{\n+\tunion prog_test_ret_arena_union r;\n+\n+\tr = bpf_kfunc_call_test_ret_arena_union(0);\n+\n+\treturn r.a == r.b;\n+}\n+\n+#else\n+\n+SEC(\"socket\")\n+__description(\"aggregate_ret_kfunc_arena: needs LLVM 23, dummy test\")\n+__skip(\"needs LLVM 23\")\n+__success\n+int dummy_test(void)\n+{\n+\treturn 0;\n+}\n+\n+#endif\n+\n+char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/progs/exceptions_fail.c b/tools/testing/selftests/bpf/progs/exceptions_fail.c\nindex 9708efb93683b..cde4982e99896 100644\n--- a/tools/testing/selftests/bpf/progs/exceptions_fail.c\n+++ b/tools/testing/selftests/bpf/progs/exceptions_fail.c\n@@ -60,7 +60,7 @@ __noinline int exception_cb_ok_arg_small(int a)\n \n SEC(\"?tc\")\n __exception_cb(exception_cb_bad_ret_type1)\n-__failure __msg(\"Only void, scalar, or a scalar-only struct/union up to 16 bytes is supported.\")\n+__failure __msg(\"Only void, a scalar, an arena pointer, or a struct/union of scalars\")\n int reject_exception_cb_type_1(struct __sk_buff *ctx)\n {\n \tbpf_throw(0);\ndiff --git a/tools/testing/selftests/bpf/progs/verifier_arena.c b/tools/testing/selftests/bpf/progs/verifier_arena.c\nindex 815f342eb4b05..672a3aadd9cb2 100644\n--- a/tools/testing/selftests/bpf/progs/verifier_arena.c\n+++ b/tools/testing/selftests/bpf/progs/verifier_arena.c\n@@ -734,4 +734,52 @@ int check_arena_arg_ret(void *ctx)\n \treturn 0;\n }\n \n+#if defined(__clang_major__) \u0026\u0026 __clang_major__ \u003e= 23\n+\n+struct arena_page_pair {\n+\tu32 __arena *first;\n+\tu32 __arena *second;\n+};\n+\n+__weak struct arena_page_pair split_arena_page(u32 __arena *page)\n+{\n+\tstruct arena_page_pair pair;\n+\n+\tpair.first = page;\n+\tpair.second = page + 1;\n+\n+\treturn pair;\n+}\n+\n+SEC(\"syscall\")\n+__load_if_JITed()\n+__success __retval(0)\n+int check_arena_struct_ret(void *ctx)\n+{\n+\tu32 __arena *page = bpf_arena_alloc_pages(\u0026arena, NULL, 1, NUMA_NO_NODE, 0);\n+\tu32 volatile __arena *first, *second;\n+\tstruct arena_page_pair pair;\n+\n+\tif (!page)\n+\t\treturn 1;\n+\n+\tpair = split_arena_page(page);\n+\tif (!pair.first || !pair.second)\n+\t\treturn 2;\n+\n+\t/* Both halves of the pair must still be usable as arena pointers. */\n+\tfirst = pair.first;\n+\tsecond = pair.second;\n+\t*first = 1;\n+\t*second = 2;\n+\tif (*first != 1)\n+\t\treturn 3;\n+\tif (*second != 2)\n+\t\treturn 4;\n+\n+\treturn 0;\n+}\n+\n+#endif\n+\n char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c\nindex 850cf4f830c48..0ef2ce875d71d 100644\n--- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c\n+++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c\n@@ -981,6 +981,48 @@ __bpf_kfunc struct prog_test_ret_ptr bpf_kfunc_call_test_ret_ptr(u64 tag)\n \treturn r;\n }\n \n+__bpf_kfunc struct prog_test_ret_arena bpf_kfunc_call_test_ret_arena(u64 addr)\n+{\n+\tstruct prog_test_ret_arena r = { .a = (void *)addr, .b = (void *)(addr + 4) };\n+\n+\treturn r;\n+}\n+\n+__bpf_kfunc struct prog_test_ret_arena_mixed bpf_kfunc_call_test_ret_arena_mixed(u64 addr)\n+{\n+\tstruct prog_test_ret_arena_mixed r = { .p = (void *)addr, .tag = 0xbeef };\n+\n+\treturn r;\n+}\n+\n+__bpf_kfunc struct prog_test_ret_arena_untagged bpf_kfunc_call_test_ret_arena_untagged(u64 addr)\n+{\n+\tstruct prog_test_ret_arena_untagged r = { .a = (void *)addr, .b = NULL };\n+\n+\treturn r;\n+}\n+\n+__bpf_kfunc union prog_test_ret_arena_union bpf_kfunc_call_test_ret_arena_union(u64 addr)\n+{\n+\tunion prog_test_ret_arena_union r = { .a = (void *)addr };\n+\n+\treturn r;\n+}\n+\n+__bpf_kfunc struct prog_test_ret_nested bpf_kfunc_call_test_ret_nested(u64 tag)\n+{\n+\tstruct prog_test_ret_nested r = { .in = { .p = NULL }, .tag = tag };\n+\n+\treturn r;\n+}\n+\n+__bpf_kfunc struct prog_test_ret_deep bpf_kfunc_call_test_ret_deep(u64 v)\n+{\n+\tstruct prog_test_ret_deep r = { .l1 = { .l2 = { .l3 = { .l4 = { .v = v } } } } };\n+\n+\treturn r;\n+}\n+\n __bpf_kfunc struct prog_test_ret_ii bpf_kfunc_call_test_ret_ii(int a, int b)\n {\n \tstruct prog_test_ret_ii r = { .a = a, .b = b };\n@@ -1539,6 +1581,12 @@ BTF_ID_FLAGS(func, bpf_kfunc_call_test_i128)\n BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_pair)\n BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_fastcall, KF_FASTCALL)\n BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_ptr)\n+BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_arena)\n+BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_arena_mixed)\n+BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_arena_untagged)\n+BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_arena_union)\n+BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_nested)\n+BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_deep)\n BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_ii)\n #endif\n BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_big)\ndiff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h b/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h\nindex 65e693ada7364..a2e9e9f3184e2 100644\n--- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h\n+++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h\n@@ -26,6 +26,12 @@ struct prog_test_ref_kfunc {\n };\n #endif\n \n+#if __has_attribute(btf_type_tag)\n+#define __arena_tag __attribute__((btf_type_tag(\"arena\")))\n+#else\n+#define __arena_tag\n+#endif\n+\n struct bpf_iter_testmod_seq;\n \n struct prog_test_pass1 {\n@@ -70,6 +76,45 @@ struct prog_test_ret_ptr {\t/* 16 bytes: contains a pointer */\n \t__u64 tag;\n };\n \n+struct prog_test_ret_arena {\t/* 16 bytes: two arena pointers */\n+\tvoid __arena_tag *a;\n+\tvoid __arena_tag *b;\n+};\n+\n+struct prog_test_ret_arena_mixed {\t/* 16 bytes: an arena pointer and a scalar */\n+\tvoid __arena_tag *p;\n+\t__u64 tag;\n+};\n+\n+struct prog_test_ret_arena_untagged {\t/* 16 bytes: 'b' lacks the arena tag */\n+\tvoid __arena_tag *a;\n+\tvoid *b;\n+};\n+\n+union prog_test_ret_arena_union {\t/* 8 bytes: 'b' lacks the arena tag */\n+\tvoid __arena_tag *a;\n+\tvoid *b;\n+};\n+\n+struct prog_test_ret_nested {\t/* 16 bytes: the pointer hides one level down */\n+\tstruct {\n+\t\tvoid *p;\n+\t} in;\n+\t__u64 tag;\n+};\n+\n+struct prog_test_ret_deep {\t/* 8 bytes, but nested past the 4-level walk limit */\n+\tstruct {\n+\t\tstruct {\n+\t\t\tstruct {\n+\t\t\t\tstruct {\n+\t\t\t\t\t__u64 v;\n+\t\t\t\t} l4;\n+\t\t\t} l3;\n+\t\t} l2;\n+\t} l1;\n+};\n+\n struct prog_test_ret_big {\t/* 24 bytes: too large for R0:R2 */\n \t__u64 a;\n \t__u64 b;\n@@ -159,6 +204,12 @@ struct prog_test_ret_pair bpf_kfunc_call_test_ret_pair(__u64 a, __u64 b) __ksym;\n struct prog_test_ret_pair bpf_kfunc_call_test_ret_fastcall(__u64 a, __u64 b) __ksym;\n struct prog_test_ret_ii bpf_kfunc_call_test_ret_ii(int a, int b) __ksym;\n struct prog_test_ret_ptr bpf_kfunc_call_test_ret_ptr(__u64 tag) __ksym;\n+struct prog_test_ret_nested bpf_kfunc_call_test_ret_nested(__u64 tag) __ksym;\n+struct prog_test_ret_arena bpf_kfunc_call_test_ret_arena(__u64 addr) __ksym;\n+struct prog_test_ret_arena_mixed bpf_kfunc_call_test_ret_arena_mixed(__u64 addr) __ksym;\n+struct prog_test_ret_arena_untagged bpf_kfunc_call_test_ret_arena_untagged(__u64 addr) __ksym;\n+union prog_test_ret_arena_union bpf_kfunc_call_test_ret_arena_union(__u64 addr) __ksym;\n+struct prog_test_ret_deep bpf_kfunc_call_test_ret_deep(__u64 v) __ksym;\n struct prog_test_ret_big bpf_kfunc_call_test_ret_big(void) __ksym;\n __u64 bpf_kfunc_call_stack_arg(__u64 a, __u64 b, __u64 c, __u64 d,\n \t\t\t __u64 e, __u64 f, __u64 g, __u64 h,\n"
} |
| 2/1 | 2026/08/27 06:43 | agent | patch-evaluator |
1mModel:best-expensive Args: null Results: {
"EnableConfigs": null,
"FocusSymbols": [
"btf_struct_is_composed_of",
"btf_type_is_arena_ptr",
"btf_prepare_func_args"
],
"Reasoning": "The patch modifies the BPF verifier to allow returning structs or unions by value from kfuncs and global subprograms if they are composed of scalars and arena pointers (pointers with the `btf_type_tag(\"arena\")` attribute). Previously, only scalars were allowed. This is a functional change in the verifier's type checking logic and register state tracking (handling multiple return registers and arena pointers). It is reachable from userspace via the `bpf()` syscall when loading BPF programs that call such kfuncs or subprograms.",
"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 af7e8a8a9501e5ea3bca9b8e122fdc4ecc545e63
Author: syz-cluster <triage@syzkaller.com>
Date: Thu Aug 27 06:43:34 2026 +0000
syz-cluster: applied patch under review
diff --git a/Documentation/bpf/kfuncs.rst b/Documentation/bpf/kfuncs.rst
index 89dea6b0b0241..96bf63d344322 100644
--- a/Documentation/bpf/kfuncs.rst
+++ b/Documentation/bpf/kfuncs.rst
@@ -581,20 +581,27 @@ against the arena. Larger accesses must verify the range explicitly.
A kfunc may return a scalar, a pointer, or a small struct or union by
value. A scalar or pointer of up to 8 bytes is returned in R0, as usual.
-A struct or union returned by value must be composed only of scalars
-(recursively), where a scalar is an integer or an enum; arrays of scalars are
-allowed as members. Its bytes are handed back to the program as the raw
-contents of R0 (and R2), so a pointer field would be laundered into a scalar
-and escape the verifier's pointer provenance and reference tracking. A struct
-or union with a pointer member is therefore rejected at load time, and so is
-one with a floating-point member, which the ABI may not return in R0:R2 at
-all.
+A struct or union returned by value must be composed only of scalars and arena
+pointers (recursively), where a scalar is an integer or an enum and an arena
+pointer is one carrying the ``btf_type_tag("arena")`` attribute; arrays of
+either are allowed as members. Its bytes are handed back to the program as the
+raw contents of R0 (and R2), so a member of any other pointer type would be
+laundered into a scalar and escape the verifier's pointer provenance and
+reference tracking. A struct or union with such a member is therefore rejected
+at load time, and so is one with a floating-point member, which the ABI may not
+return in R0:R2 at all.
+
+An arena pointer member is handed back as a scalar too, but nothing is lost by
+that. The program must ``cast_kern()`` the value before it can be used, and the
+verifier allows that cast on any scalar, so a laundered arena address gives the
+program no reach it did not already have. The result is confined to the
+program's arena in either case.
A kfunc may also return a value larger than 8 bytes and up to 16 bytes -- a
-scalar-only struct or union, or an ``__int128``. Such a value is returned
-in the register pair R0:R2, matching the convention LLVM uses for the BPF
-target: the first 8 bytes in R0 and the second 8 bytes in R2. A struct or
-union of 8 bytes or less is returned in R0 alone.
+struct or union of scalars and arena pointers, or an ``__int128``. Such a
+value is returned in the register pair R0:R2, matching the convention LLVM
+uses for the BPF target: the first 8 bytes in R0 and the second 8 bytes in R2.
+A struct or union of 8 bytes or less is returned in R0 alone.
::
@@ -620,10 +627,10 @@ used when the program is JITed, since the interpreter propagates only R0 out of
a subprogram: without a JIT the return value stays in R0 alone, and a caller
reading R2 is rejected for reading an uninitialized register. A global
subprogram is verified in isolation, so its by-value struct or union return is
-restricted to scalars just like a kfunc's; a static subprogram is verified
-inline and has no such restriction. The main program is not covered: its return
-value is the program's exit code, read out of R0 alone, so a declared upper
-half is never looked at.
+restricted to scalars and arena pointers just like a kfunc's; a static
+subprogram is verified inline and has no such restriction. The main program is
+not covered: its return value is the program's exit code, read out of R0
+alone, so a declared upper half is never looked at.
A global subprogram must leave a scalar in *every* register of the pair, so
both halves of the returned value have to be assigned. Leaving the upper half
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 004b06785521c..ae9f606539f41 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -1488,8 +1488,15 @@ int bpf_jmp_offset(struct bpf_insn *insn);
struct bpf_iarray *bpf_insn_successors(struct bpf_verifier_env *env, u32 idx);
void bpf_fmt_stack_mask(char *buf, ssize_t buf_sz, u64 stack_mask);
bool bpf_subprog_is_global(const struct bpf_verifier_env *env, int subprog);
-bool btf_type_is_scalar_struct(struct bpf_verifier_env *env, const struct btf *btf,
- const struct btf_type *t, int rec);
+
+/* Kinds of member a by-value struct or union may be composed of. */
+enum btf_member_kind {
+ BTF_MEMBER_SCALAR = BIT(0), /* an int or an enum */
+ BTF_MEMBER_ARENA_PTR = BIT(1), /* a pointer carrying the "arena" type tag */
+};
+
+bool btf_struct_is_composed_of(struct bpf_verifier_env *env, const struct btf *btf,
+ const struct btf_type *t, u32 member_kinds);
int bpf_find_subprog(struct bpf_verifier_env *env, int off);
bool bpf_is_throw_kfunc(struct bpf_insn *insn);
diff --git a/include/linux/btf.h b/include/linux/btf.h
index 89d5a5c4f1171..ddd0f4f32d24b 100644
--- a/include/linux/btf.h
+++ b/include/linux/btf.h
@@ -235,6 +235,7 @@ struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type
u32 field_mask, u32 value_size);
int btf_check_and_fixup_fields(const struct btf *btf, struct btf_record *rec);
bool btf_type_is_void(const struct btf_type *t);
+bool btf_type_is_arena_ptr(const struct btf *btf, const struct btf_type *t);
s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p);
struct btf *btf_get_module_btf(const struct module *module);
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 91b8ce77f699a..f6d82a8fd617b 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -3523,6 +3523,22 @@ static int btf_type_tag_walk(const struct btf *btf,
return 0;
}
+bool btf_type_is_arena_ptr(const struct btf *btf, const struct btf_type *t)
+{
+ if (!btf_type_is_ptr(t))
+ return false;
+
+ for (t = btf_type_by_id(btf, t->type); btf_type_is_modifier(t);
+ t = btf_type_by_id(btf, t->type)) {
+ if (!btf_type_is_type_tag(t) || btf_type_kflag(t))
+ continue;
+ if (!strcmp(__btf_name_by_offset(btf, t->name_off), "arena"))
+ return true;
+ }
+
+ return false;
+}
+
static int btf_find_kptr(const struct btf *btf, const struct btf_type *t,
u32 off, int sz, struct btf_field_info *info, u32 field_mask)
{
@@ -7927,51 +7943,19 @@ static int btf_scan_decl_tags(struct bpf_verifier_env *env,
return 0;
}
-static int btf_scan_type_tags(struct bpf_verifier_env *env,
- const struct btf *btf, u32 type_id,
- u32 *tags)
+static void btf_scan_type_tags(const struct btf *btf, u32 type_id, u32 *tags)
{
- static const struct btf_type_tag_match func_type_tags[] = {
- { "arena", ARG_TAG_ARENA },
- };
- struct btf_type_tag_walk_ctx ctx;
- const struct btf_type *t;
- int err;
-
/* Find the first pointer type in the chain. */
- t = btf_type_skip_modifiers(btf, type_id, NULL);
+ const struct btf_type *t = btf_type_skip_modifiers(btf, type_id, NULL);
- /*
- * We currently reject type tags on non-pointer types,
- * which neither LLVM nor GCC support anyway.
- */
- if (!t || !btf_type_is_ptr(t))
- return 0;
-
- ctx.t = t;
- err = btf_type_tag_walk(btf, &ctx, func_type_tags,
- ARRAY_SIZE(func_type_tags));
- if (err) {
- bpf_log(&env->log,
- "function signature member has multiple type tags\n");
- return err;
- }
- *tags |= ctx.res;
-
- return 0;
+ if (btf_type_is_arena_ptr(btf, t))
+ *tags |= ARG_TAG_ARENA;
}
/* Check whether the type is a valid return type. */
static int btf_validate_return_type(struct bpf_verifier_env *env, struct btf *btf,
const struct btf_type *t, int subprog, bool is_global)
{
- u32 tags = 0;
- int err;
-
- err = btf_scan_type_tags(env, btf, t->type, &tags);
- if (err)
- return err;
-
t = btf_type_skip_modifiers(btf, t->type, NULL);
/*
@@ -7979,7 +7963,7 @@ static int btf_validate_return_type(struct bpf_verifier_env *env, struct btf *bt
* General arena variables are not allowed, since it makes no sense to return by value
* a variable that's on the heap in the first place.
*/
- if (subprog && (tags & ARG_TAG_ARENA) && btf_type_is_ptr(t))
+ if (subprog && btf_type_is_arena_ptr(btf, t))
return 0;
/* We always accept void or scalars. */
@@ -7989,13 +7973,22 @@ static int btf_validate_return_type(struct bpf_verifier_env *env, struct btf *bt
if (btf_type_is_struct(t) && t->size <= 16) {
/*
* A global function's caller models the return as an opaque
- * scalar pair, so it may only return scalars by value. A local
- * function is verified inline, so a pointer field stays tracked
- * and needs no such restriction.
+ * scalar pair, so a pointer member would be laundered into a
+ * scalar and escape provenance and reference tracking. Only
+ * scalars and arena pointers are allowed: an arena pointer has
+ * no provenance to lose, since a program may already derive one
+ * from any scalar with addr_space_cast(), which confines the
+ * result to the arena. A local function is verified inline, so
+ * its caller receives the real register state and any member is
+ * fine.
*/
bool local_func = subprog && !is_global;
+ u32 member_kinds = BTF_MEMBER_SCALAR;
+
+ if (subprog)
+ member_kinds |= BTF_MEMBER_ARENA_PTR;
- if (local_func || btf_type_is_scalar_struct(env, btf, t, 0))
+ if (local_func || btf_struct_is_composed_of(env, btf, t, member_kinds))
return 0;
}
@@ -8091,7 +8084,8 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog)
if (is_global) {
bpf_log(log,
"Global function %s() has unsupported return type. "
- "Only void, scalar, or a scalar-only struct/union up to 16 bytes is supported.\n",
+ "Only void, a scalar, an arena pointer, or a struct/union of "
+ "scalars and arena pointers up to 16 bytes is supported.\n",
tname);
}
return err;
@@ -8106,9 +8100,7 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog)
if (err)
return err;
- err = btf_scan_type_tags(env, btf, args[i].type, &tags);
- if (err)
- return err;
+ btf_scan_type_tags(btf, args[i].type, &tags);
t = btf_type_by_id(btf, args[i].type);
while (btf_type_is_modifier(t))
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index e036ae20bf6b9..9a36808136c9a 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -9995,9 +9995,14 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
*/
if (!returns_void) {
nregs = bpf_ret_reg_pair(env, subprog) ? 2 : 1;
- for (i = 0; i < nregs; i++)
- mark_reg_unknown(env, caller->regs, ret_regs[i]);
+ mark_reg_unknown(env, caller->regs, ret_regs[0]);
bpf_diag_mod_end(env);
+ for (i = 1; i < nregs; i++) {
+ bpf_diag_mod_begin(env, &caller->regs[ret_regs[i]], NULL,
+ BPF_DIAG_MOD_WRITE);
+ mark_reg_unknown(env, caller->regs, ret_regs[i]);
+ bpf_diag_mod_end(env);
+ }
}
if (env->subprog_info[subprog].might_throw) {
@@ -10403,10 +10408,14 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
* return to the caller whatever the callee had in the
* return register(s)
*/
- bpf_diag_mod_begin(env, &caller->regs[BPF_REG_0], r0, BPF_DIAG_MOD_WRITE);
- for (i = 0; i < nregs; i++)
- caller->regs[ret_regs[i]] = callee->regs[ret_regs[i]];
- bpf_diag_mod_end(env);
+ for (i = 0; i < nregs; i++) {
+ u32 regno = ret_regs[i];
+
+ bpf_diag_mod_begin(env, &caller->regs[regno], &callee->regs[regno],
+ BPF_DIAG_MOD_WRITE);
+ caller->regs[regno] = callee->regs[regno];
+ bpf_diag_mod_end(env);
+ }
}
/* for callbacks like bpf_loop or bpf_for_each_map_elem go back to callsite,
@@ -11342,15 +11351,33 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
/*
* Mark the register(s) holding a @size byte kfunc return value as unknown
- * scalars. Both halves of a register pair are treated the same way.
+ * scalars and return how many registers that took. Both halves of a register
+ * pair are treated the same way.
+ *
+ * R0 is written in the diagnostic modification scope the caller opened before
+ * the caller-saved scrub, since R0 gets no scrub record of its own and that
+ * scope is what carries its pre-call value into the history. A scope tracks a
+ * single register and does not nest, so close it before recording the rest of
+ * the return value. A value that fits in R0 leaves the caller's scope open for
+ * the caller to close, so a later write to R0 still lands in it.
*/
-static void mark_kfunc_ret_regs(struct bpf_verifier_env *env,
- struct bpf_reg_state *regs, u32 size)
+static u32 mark_kfunc_ret_regs(struct bpf_verifier_env *env,
+ struct bpf_reg_state *regs, u32 size)
{
u32 i, nregs = ret_regs_cnt(size);
- for (i = 0; i < nregs; i++)
+ mark_reg_unknown(env, regs, ret_regs[0]);
+ if (nregs == 1)
+ return nregs;
+
+ bpf_diag_mod_end(env);
+ for (i = 1; i < nregs; i++) {
+ bpf_diag_mod_begin(env, ®s[ret_regs[i]], NULL, BPF_DIAG_MOD_WRITE);
mark_reg_unknown(env, regs, ret_regs[i]);
+ bpf_diag_mod_end(env);
+ }
+
+ return nregs;
}
static bool is_kfunc_acquire(struct bpf_call_arg_meta *meta)
@@ -11619,10 +11646,33 @@ static bool is_kfunc_arg_implicit(const struct bpf_call_arg_meta *meta, u32 arg_
return argn <= arg_idx;
}
-/* Returns true if struct is composed of scalars, 4 levels of nesting allowed */
-bool btf_type_is_scalar_struct(struct bpf_verifier_env *env,
- const struct btf *btf,
- const struct btf_type *t, int rec)
+#define BTF_MEMBER_MAX_DEPTH 4
+#define BTF_MEMBER_PATH_LEN 64
+
+struct btf_member_path {
+ const struct btf_member *member[BTF_MEMBER_MAX_DEPTH];
+ int depth;
+ bool too_deep;
+};
+
+static bool btf_member_kind_allowed(const struct btf *btf, const struct btf_type *t,
+ u32 member_kinds)
+{
+ if ((member_kinds & BTF_MEMBER_SCALAR) && btf_type_is_scalar(t))
+ return true;
+ if ((member_kinds & BTF_MEMBER_ARENA_PTR) && btf_type_is_arena_ptr(btf, t))
+ return true;
+ return false;
+}
+
+/*
+ * Returns true if every member of struct @t is of a kind listed in
+ * @member_kinds, BTF_MEMBER_MAX_DEPTH levels of nesting allowed. An array
+ * member counts as its element type.
+ */
+static bool btf_struct_member_walk(struct bpf_verifier_env *env, const struct btf *btf,
+ const struct btf_type *t, u32 member_kinds, int rec,
+ struct btf_member_path *path)
{
const struct btf_type *member_type;
const struct btf_member *member;
@@ -11636,27 +11686,65 @@ bool btf_type_is_scalar_struct(struct bpf_verifier_env *env,
member_type = btf_type_skip_modifiers(btf, member->type, NULL);
if (btf_type_is_struct(member_type)) {
- if (rec >= 3) {
+ if (rec >= BTF_MEMBER_MAX_DEPTH - 1) {
verbose(env, "max struct nesting depth exceeded\n");
+ if (path)
+ path->too_deep = true;
return false;
}
- if (!btf_type_is_scalar_struct(env, btf, member_type, rec + 1))
- return false;
+ if (!btf_struct_member_walk(env, btf, member_type, member_kinds,
+ rec + 1, path))
+ goto bad_path;
continue;
}
if (btf_type_is_array(member_type)) {
array = btf_array(member_type);
if (!array->nelems)
- return false;
+ goto bad_member;
member_type = btf_type_skip_modifiers(btf, array->type, NULL);
- if (!btf_type_is_scalar(member_type))
- return false;
- continue;
}
- if (!btf_type_is_scalar(member_type))
- return false;
+ if (!btf_member_kind_allowed(btf, member_type, member_kinds))
+ goto bad_member;
}
return true;
+
+bad_member:
+ if (path)
+ path->depth = rec + 1;
+bad_path:
+ if (path && path->depth)
+ path->member[rec] = member;
+ return false;
+}
+
+bool btf_struct_is_composed_of(struct bpf_verifier_env *env,
+ const struct btf *btf,
+ const struct btf_type *t, u32 member_kinds)
+{
+ return btf_struct_member_walk(env, btf, t, member_kinds, 0, NULL);
+}
+
+static bool btf_type_is_scalar_struct(struct bpf_verifier_env *env,
+ const struct btf *btf,
+ const struct btf_type *t)
+{
+ return btf_struct_is_composed_of(env, btf, t, BTF_MEMBER_SCALAR);
+}
+
+static void btf_member_path_str(const struct btf *btf, const struct btf_member_path *path,
+ char *buf, size_t buf_sz)
+{
+ size_t len = 0;
+ int i;
+
+ buf[0] = '\0';
+ for (i = 0; i < path->depth; i++) {
+ const char *name = btf_name_by_offset(btf, path->member[i]->name_off);
+
+ if (!name || !name[0])
+ continue;
+ len += scnprintf(buf + len, buf_sz - len, "%s%s", len ? "." : "", name);
+ }
}
enum kfunc_ptr_arg_type {
@@ -12039,7 +12127,7 @@ get_kfunc_arg_type(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,
(is_kfunc_arg_mem_size(meta->btf, &args[arg + 1]) ||
is_kfunc_arg_const_mem_size(meta->btf, &args[arg + 1]))) {
if (!btf_type_is_void(ref_t) && !btf_type_is_scalar(ref_t) &&
- !btf_type_is_scalar_struct(env, meta->btf, ref_t, 0)) {
+ !btf_type_is_scalar_struct(env, meta->btf, ref_t)) {
verbose(env, "%s pointer type %s %s must point to void, scalar, or struct with scalar\n",
reg_arg_name(env, argno), btf_type_str(ref_t), ref_tname);
return -EINVAL;
@@ -12055,7 +12143,7 @@ get_kfunc_arg_type(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,
* scalars. The access size is derived from the pointed-to BTF type.
*/
if (!btf_type_is_scalar(ref_t) &&
- !btf_type_is_scalar_struct(env, meta->btf, ref_t, 0)) {
+ !btf_type_is_scalar_struct(env, meta->btf, ref_t)) {
verbose(env, "%s pointer type %s %s must point to scalar, or struct with scalar\n",
reg_arg_name(env, argno), btf_type_str(ref_t), ref_tname);
return -EINVAL;
@@ -13111,7 +13199,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me
break;
}
- if (!btf_type_is_scalar_struct(env, meta->btf, ref_t, 0)) {
+ if (!btf_type_is_scalar_struct(env, meta->btf, ref_t)) {
enum bpf_reg_type reg2btf_type = lookup_reg2btf_ids(ref_id);
const char *expected_type;
@@ -13653,7 +13741,7 @@ static int check_special_kfunc(struct bpf_verifier_env *env, struct bpf_call_arg
struct_meta = btf_find_struct_meta(ret_btf, ret_btf_id);
if (is_bpf_percpu_obj_new_kfunc(meta->func_id)) {
- if (!btf_type_is_scalar_struct(env, ret_btf, ret_t, 0)) {
+ if (!btf_type_is_scalar_struct(env, ret_btf, ret_t)) {
verbose(env, "bpf_percpu_obj_new type ID argument must be of a struct of scalars\n");
return -EINVAL;
}
@@ -13767,7 +13855,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
struct bpf_insn_aux_data *insn_aux;
const char *operation;
int err, insn_idx = *insn_idx_p;
- u32 i, nargs, ptr_type_id;
+ u32 i, nargs, ptr_type_id, ret_nregs = 1;
struct bpf_kfunc_desc *desc;
struct btf *desc_btf;
int id;
@@ -14021,25 +14109,59 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
}
if (btf_type_is_scalar(t)) {
- mark_kfunc_ret_regs(env, regs, t->size);
+ ret_nregs = mark_kfunc_ret_regs(env, regs, t->size);
if (meta.btf == btf_vmlinux && (meta.func_id == special_kfunc_list[KF_bpf_res_spin_lock] ||
meta.func_id == special_kfunc_list[KF_bpf_res_spin_lock_irqsave]))
__mark_reg_const_zero(env, ®s[BPF_REG_0]);
} else if (btf_type_is_struct(t)) {
+ struct btf_member_path path = {};
+ const char *member_note = "";
+
/*
* The returned struct comes back as raw register bits modeled
- * as an unknown scalar, so it must contain only scalars:
- * otherwise a pointer field would be laundered into a scalar
- * and escape provenance and reference tracking.
+ * as an unknown scalar, so a pointer member would be laundered
+ * into a scalar and escape provenance and reference tracking.
+ * Only scalars and arena pointers are allowed: an arena pointer
+ * has no provenance to lose, since a program may already derive
+ * one from any scalar with addr_space_cast(), which confines the
+ * result to the arena.
*/
- if (!btf_type_is_scalar_struct(env, desc_btf, t, 0)) {
+ if (!btf_struct_member_walk(env, desc_btf, t,
+ BTF_MEMBER_SCALAR | BTF_MEMBER_ARENA_PTR, 0, &path)) {
verbose(env,
- "kernel function %s returns %s %s that is not composed of scalars\n",
+ "kernel function %s returns %s %s that is not composed of scalars or arena pointers\n",
func_name, btf_type_str(t),
btf_name_by_offset(desc_btf, t->name_off));
+ if (path.too_deep) {
+ member_note = bpf_diag_fmt(
+ env, " It nests structs more than %d levels deep.",
+ BTF_MEMBER_MAX_DEPTH);
+ } else if (path.depth) {
+ const struct btf_member *bad = path.member[path.depth - 1];
+ char bad_name[BTF_MEMBER_PATH_LEN];
+ const struct btf_type *bad_type;
+
+ btf_member_path_str(desc_btf, &path, bad_name, sizeof(bad_name));
+ bad_type = btf_type_skip_modifiers(desc_btf, bad->type, NULL);
+ verbose(env, "member '%s' has type %s\n", bad_name,
+ btf_type_str(bad_type));
+ member_note = bpf_diag_fmt(
+ env,
+ " Its member '%s' is %s, not a scalar or an arena pointer.",
+ bad_name, btf_type_str(bad_type));
+ }
+ bpf_diag_program_structure(
+ env, insn_idx, "unsupported kernel function return type",
+ "Call a kernel function that returns only scalars or arena pointers by value.",
+ "%s() returns %s %s by value.%s "
+ "Only kfuncs returning scalar values or arena pointers, or "
+ "structures composed of scalar values or arena pointers are "
+ "supported.",
+ func_name, btf_type_str(t),
+ btf_name_by_offset(desc_btf, t->name_off), member_note);
return -EINVAL;
}
- mark_kfunc_ret_regs(env, regs, t->size);
+ ret_nregs = mark_kfunc_ret_regs(env, regs, t->size);
} else if (btf_type_is_ptr(t)) {
ptr_type = btf_type_skip_modifiers(desc_btf, t->type, &ptr_type_id);
err = check_special_kfunc(env, &meta, regs, insn_aux, ptr_type, desc_btf);
@@ -14170,7 +14292,8 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
* Record R0 before process_iter_next_call() snapshots the alternate
* iterator path's diagnostic position.
*/
- bpf_diag_mod_end(env);
+ if (ret_nregs == 1)
+ bpf_diag_mod_end(env);
if (bpf_is_iter_next_kfunc(&meta)) {
err = process_iter_next_call(env, insn_idx, &meta);
diff --git a/tools/testing/selftests/bpf/prog_tests/aggregate_ret.c b/tools/testing/selftests/bpf/prog_tests/aggregate_ret.c
index e0b94ed10f941..07d9d6e1d6b88 100644
--- a/tools/testing/selftests/bpf/prog_tests/aggregate_ret.c
+++ b/tools/testing/selftests/bpf/prog_tests/aggregate_ret.c
@@ -1,11 +1,53 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
#include <test_progs.h>
+#include <bpf/btf.h>
#include "aggregate_ret_func.skel.h"
#include "aggregate_ret_kfunc.skel.h"
+#include "aggregate_ret_kfunc_arena.skel.h"
+
+static bool testmod_has_arena_tagged_member(void)
+{
+ struct btf *vmlinux_btf, *module_btf = NULL;
+ const struct btf_type *t;
+ bool tagged = false;
+ __s32 id;
+
+ vmlinux_btf = btf__load_vmlinux_btf();
+ if (!vmlinux_btf)
+ return false;
+
+ module_btf = btf__load_module_btf("bpf_testmod", vmlinux_btf);
+ if (!module_btf)
+ goto out;
+
+ /* prog_test_ret_arena::a is 'void __arena_tag *': PTR -> TYPE_TAG -> void */
+ id = btf__find_by_name_kind(module_btf, "prog_test_ret_arena", BTF_KIND_STRUCT);
+ if (id <= 0)
+ goto out;
+
+ t = btf__type_by_id(module_btf, btf_members(btf__type_by_id(module_btf, id))[0].type);
+ if (!t || !btf_is_ptr(t))
+ goto out;
+
+ t = btf__type_by_id(module_btf, t->type);
+ tagged = t && btf_is_type_tag(t) &&
+ !strcmp(btf__name_by_offset(module_btf, t->name_off), "arena");
+
+out:
+ btf__free(module_btf);
+ btf__free(vmlinux_btf);
+
+ return tagged;
+}
void test_aggregate_ret(void)
{
RUN_TESTS(aggregate_ret_func);
RUN_TESTS(aggregate_ret_kfunc);
+
+ if (testmod_has_arena_tagged_member())
+ RUN_TESTS(aggregate_ret_kfunc_arena);
+ else
+ test__skip();
}
diff --git a/tools/testing/selftests/bpf/progs/aggregate_ret_func.c b/tools/testing/selftests/bpf/progs/aggregate_ret_func.c
index 6f66fc822cede..f0077d951a25d 100644
--- a/tools/testing/selftests/bpf/progs/aggregate_ret_func.c
+++ b/tools/testing/selftests/bpf/progs/aggregate_ret_func.c
@@ -2,6 +2,7 @@
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
+#include <bpf_arena_common.h>
#include "bpf_misc.h"
typedef unsigned __int128 u128;
@@ -234,4 +235,149 @@ __naked int aggregate_ret_global_union_ptr_fail(void)
#endif
+/*
+ * gcc returns a by-value struct through a hidden pointer, and emits the
+ * 'r0 = r1' returning it after the __naked body's exit, leaving the
+ * subprogram falling through. Build these with clang only.
+ */
+#if defined(__clang__)
+
+struct arena_pair {
+ void __arena *lo;
+ void __arena *hi;
+};
+
+struct arena_and_scalar {
+ void __arena *p;
+ __u64 x;
+};
+
+struct arena_array {
+ void __arena *p[2];
+};
+
+struct arena_single {
+ void __arena *p;
+};
+
+union arena_upair {
+ void __arena *p;
+ __u64 halves[2];
+};
+
+__naked struct arena_pair global_ret_arena_pair(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "r2 = 0;"
+ "exit;"
+ );
+}
+
+SEC("tc")
+__load_if_JITed()
+__success __retval(0)
+__naked int aggregate_ret_global_arena_pair(void)
+{
+ asm volatile (
+ "call %[global_ret_arena_pair];"
+ "r0 = 0;"
+ "exit;"
+ :
+ : __imm(global_ret_arena_pair)
+ : __clobber_all);
+}
+
+__naked struct arena_and_scalar global_ret_arena_and_scalar(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "r2 = 0;"
+ "exit;"
+ );
+}
+
+SEC("tc")
+__load_if_JITed()
+__success __retval(0)
+__naked int aggregate_ret_global_arena_and_scalar(void)
+{
+ asm volatile (
+ "call %[global_ret_arena_and_scalar];"
+ "r0 = 0;"
+ "exit;"
+ :
+ : __imm(global_ret_arena_and_scalar)
+ : __clobber_all);
+}
+
+__naked struct arena_array global_ret_arena_array(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "r2 = 0;"
+ "exit;"
+ );
+}
+
+SEC("tc")
+__load_if_JITed()
+__success __retval(0)
+__naked int aggregate_ret_global_arena_array(void)
+{
+ asm volatile (
+ "call %[global_ret_arena_array];"
+ "r0 = 0;"
+ "exit;"
+ :
+ : __imm(global_ret_arena_array)
+ : __clobber_all);
+}
+
+__naked struct arena_single global_ret_arena_single(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "exit;"
+ );
+}
+
+SEC("tc")
+__success __retval(0)
+__naked int aggregate_ret_global_arena_single(void)
+{
+ asm volatile (
+ "call %[global_ret_arena_single];"
+ "r0 = 0;"
+ "exit;"
+ :
+ : __imm(global_ret_arena_single)
+ : __clobber_all);
+}
+
+__naked union arena_upair global_ret_arena_union(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "r2 = 0;"
+ "exit;"
+ );
+}
+
+SEC("tc")
+__load_if_JITed()
+__success __retval(0)
+__naked int aggregate_ret_global_arena_union(void)
+{
+ asm volatile (
+ "call %[global_ret_arena_union];"
+ "r0 = 0;"
+ "exit;"
+ :
+ : __imm(global_ret_arena_union)
+ : __clobber_all);
+}
+
+#endif
+
char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/aggregate_ret_kfunc.c b/tools/testing/selftests/bpf/progs/aggregate_ret_kfunc.c
index d6b422ae9784b..e9c82df8efb26 100644
--- a/tools/testing/selftests/bpf/progs/aggregate_ret_kfunc.c
+++ b/tools/testing/selftests/bpf/progs/aggregate_ret_kfunc.c
@@ -18,6 +18,8 @@ void __kfunc_btf_root(void)
: "r"(&bpf_kfunc_call_test_i128),
"r"(&bpf_kfunc_call_test_ret_fastcall),
"r"(&bpf_kfunc_call_test_ret_ptr),
+ "r"(&bpf_kfunc_call_test_ret_nested),
+ "r"(&bpf_kfunc_call_test_ret_deep),
"r"(&bpf_kfunc_call_test_ret_ii),
"r"(&bpf_kfunc_call_test_ret_big));
}
@@ -71,7 +73,8 @@ __naked int aggregate_ret_kfunc_fastcall_fail(void)
SEC("tc")
__arch_x86_64 __arch_arm64
-__failure __msg("is not composed of scalars")
+__failure __msg("is not composed of scalars or arena pointers")
+__msg("member 'p' has type PTR")
__naked int aggregate_ret_kfunc_ptr_fail(void)
{
asm volatile (
@@ -84,6 +87,37 @@ __naked int aggregate_ret_kfunc_ptr_fail(void)
: __clobber_all);
}
+SEC("tc")
+__arch_x86_64 __arch_arm64
+__failure __msg("is not composed of scalars or arena pointers")
+__msg("member 'in.p' has type PTR")
+__naked int aggregate_ret_kfunc_nested_ptr_fail(void)
+{
+ asm volatile (
+ "r1 = 0;"
+ "call %[bpf_kfunc_call_test_ret_nested];"
+ "r0 = 0;"
+ "exit;"
+ :
+ : __imm(bpf_kfunc_call_test_ret_nested)
+ : __clobber_all);
+}
+
+SEC("tc")
+__arch_x86_64 __arch_arm64
+__failure __msg("max struct nesting depth exceeded")
+__naked int aggregate_ret_kfunc_too_deep_fail(void)
+{
+ asm volatile (
+ "r1 = 0;"
+ "call %[bpf_kfunc_call_test_ret_deep];"
+ "r0 = 0;"
+ "exit;"
+ :
+ : __imm(bpf_kfunc_call_test_ret_deep)
+ : __clobber_all);
+}
+
SEC("tc")
__arch_x86_64 __arch_arm64
__failure __msg("R2 !read_ok")
diff --git a/tools/testing/selftests/bpf/progs/aggregate_ret_kfunc_arena.c b/tools/testing/selftests/bpf/progs/aggregate_ret_kfunc_arena.c
new file mode 100644
index 0000000000000..94c35e1b547cd
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/aggregate_ret_kfunc_arena.c
@@ -0,0 +1,129 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf_arena_common.h>
+#include "bpf_misc.h"
+#include "../test_kmods/bpf_testmod_kfunc.h"
+
+#if defined(__clang_major__) && __clang_major__ >= 23
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARENA);
+ __uint(map_flags, BPF_F_MMAPABLE);
+ __uint(max_entries, 2);
+} arena SEC(".maps");
+
+/*
+ * A returned member carries the arena type tag but not the address space
+ * qualifier, so the program casts it into the arena address space itself
+ * rather than the compiler doing it.
+ */
+#define arena_ptr(p) ((u32 volatile __arena *)(p))
+
+SEC("syscall")
+__arch_x86_64 __arch_arm64
+__load_if_JITed()
+__success __retval(0)
+int aggregate_ret_kfunc_arena(void *ctx)
+{
+ u32 volatile __arena *page = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
+ u32 volatile __arena *a, *b;
+ struct prog_test_ret_arena r;
+
+ if (!page)
+ return 1;
+
+ /* Both halves come back in R0:R2, pointing at page and page + 4. */
+ r = bpf_kfunc_call_test_ret_arena((u64)page);
+ if (!r.a || !r.b)
+ return 2;
+
+ a = arena_ptr(r.a);
+ b = arena_ptr(r.b);
+ *a = 1;
+ *b = 2;
+ if (*a != 1)
+ return 3;
+ if (*b != 2)
+ return 4;
+
+ /* The halves are the first two slots of the page the program allocated. */
+ page[0] = 7;
+ if (*a != 7)
+ return 5;
+ page[1] = 9;
+ if (*b != 9)
+ return 6;
+
+ return 0;
+}
+
+SEC("syscall")
+__arch_x86_64 __arch_arm64
+__load_if_JITed()
+__success __retval(0)
+int aggregate_ret_kfunc_arena_mixed(void *ctx)
+{
+ u32 __arena *page = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
+ struct prog_test_ret_arena_mixed r;
+ u32 volatile __arena *p;
+
+ if (!page)
+ return 1;
+
+ /* An arena pointer in R0 beside a scalar in R2. */
+ r = bpf_kfunc_call_test_ret_arena_mixed((u64)page);
+ if (!r.p)
+ return 2;
+ if (r.tag != 0xbeef)
+ return 3;
+
+ p = arena_ptr(r.p);
+ *p = 3;
+ if (*p != 3)
+ return 4;
+
+ return 0;
+}
+
+SEC("syscall")
+__arch_x86_64 __arch_arm64
+__failure __msg("is not composed of scalars or arena pointers")
+__msg("member 'b' has type PTR")
+int aggregate_ret_kfunc_arena_untagged_fail(void *ctx)
+{
+ struct prog_test_ret_arena_untagged r;
+
+ r = bpf_kfunc_call_test_ret_arena_untagged(0);
+
+ return r.a == r.b;
+}
+
+SEC("syscall")
+__arch_x86_64 __arch_arm64
+__failure __msg("is not composed of scalars or arena pointers")
+__msg("member 'b' has type PTR")
+int aggregate_ret_kfunc_arena_union_fail(void *ctx)
+{
+ union prog_test_ret_arena_union r;
+
+ r = bpf_kfunc_call_test_ret_arena_union(0);
+
+ return r.a == r.b;
+}
+
+#else
+
+SEC("socket")
+__description("aggregate_ret_kfunc_arena: needs LLVM 23, dummy test")
+__skip("needs LLVM 23")
+__success
+int dummy_test(void)
+{
+ return 0;
+}
+
+#endif
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/exceptions_fail.c b/tools/testing/selftests/bpf/progs/exceptions_fail.c
index 9708efb93683b..cde4982e99896 100644
--- a/tools/testing/selftests/bpf/progs/exceptions_fail.c
+++ b/tools/testing/selftests/bpf/progs/exceptions_fail.c
@@ -60,7 +60,7 @@ __noinline int exception_cb_ok_arg_small(int a)
SEC("?tc")
__exception_cb(exception_cb_bad_ret_type1)
-__failure __msg("Only void, scalar, or a scalar-only struct/union up to 16 bytes is supported.")
+__failure __msg("Only void, a scalar, an arena pointer, or a struct/union of scalars")
int reject_exception_cb_type_1(struct __sk_buff *ctx)
{
bpf_throw(0);
diff --git a/tools/testing/selftests/bpf/progs/verifier_arena.c b/tools/testing/selftests/bpf/progs/verifier_arena.c
index 815f342eb4b05..672a3aadd9cb2 100644
--- a/tools/testing/selftests/bpf/progs/verifier_arena.c
+++ b/tools/testing/selftests/bpf/progs/verifier_arena.c
@@ -734,4 +734,52 @@ int check_arena_arg_ret(void *ctx)
return 0;
}
+#if defined(__clang_major__) && __clang_major__ >= 23
+
+struct arena_page_pair {
+ u32 __arena *first;
+ u32 __arena *second;
+};
+
+__weak struct arena_page_pair split_arena_page(u32 __arena *page)
+{
+ struct arena_page_pair pair;
+
+ pair.first = page;
+ pair.second = page + 1;
+
+ return pair;
+}
+
+SEC("syscall")
+__load_if_JITed()
+__success __retval(0)
+int check_arena_struct_ret(void *ctx)
+{
+ u32 __arena *page = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
+ u32 volatile __arena *first, *second;
+ struct arena_page_pair pair;
+
+ if (!page)
+ return 1;
+
+ pair = split_arena_page(page);
+ if (!pair.first || !pair.second)
+ return 2;
+
+ /* Both halves of the pair must still be usable as arena pointers. */
+ first = pair.first;
+ second = pair.second;
+ *first = 1;
+ *second = 2;
+ if (*first != 1)
+ return 3;
+ if (*second != 2)
+ return 4;
+
+ return 0;
+}
+
+#endif
+
char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
index 850cf4f830c48..0ef2ce875d71d 100644
--- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
+++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
@@ -981,6 +981,48 @@ __bpf_kfunc struct prog_test_ret_ptr bpf_kfunc_call_test_ret_ptr(u64 tag)
return r;
}
+__bpf_kfunc struct prog_test_ret_arena bpf_kfunc_call_test_ret_arena(u64 addr)
+{
+ struct prog_test_ret_arena r = { .a = (void *)addr, .b = (void *)(addr + 4) };
+
+ return r;
+}
+
+__bpf_kfunc struct prog_test_ret_arena_mixed bpf_kfunc_call_test_ret_arena_mixed(u64 addr)
+{
+ struct prog_test_ret_arena_mixed r = { .p = (void *)addr, .tag = 0xbeef };
+
+ return r;
+}
+
+__bpf_kfunc struct prog_test_ret_arena_untagged bpf_kfunc_call_test_ret_arena_untagged(u64 addr)
+{
+ struct prog_test_ret_arena_untagged r = { .a = (void *)addr, .b = NULL };
+
+ return r;
+}
+
+__bpf_kfunc union prog_test_ret_arena_union bpf_kfunc_call_test_ret_arena_union(u64 addr)
+{
+ union prog_test_ret_arena_union r = { .a = (void *)addr };
+
+ return r;
+}
+
+__bpf_kfunc struct prog_test_ret_nested bpf_kfunc_call_test_ret_nested(u64 tag)
+{
+ struct prog_test_ret_nested r = { .in = { .p = NULL }, .tag = tag };
+
+ return r;
+}
+
+__bpf_kfunc struct prog_test_ret_deep bpf_kfunc_call_test_ret_deep(u64 v)
+{
+ struct prog_test_ret_deep r = { .l1 = { .l2 = { .l3 = { .l4 = { .v = v } } } } };
+
+ return r;
+}
+
__bpf_kfunc struct prog_test_ret_ii bpf_kfunc_call_test_ret_ii(int a, int b)
{
struct prog_test_ret_ii r = { .a = a, .b = b };
@@ -1539,6 +1581,12 @@ BTF_ID_FLAGS(func, bpf_kfunc_call_test_i128)
BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_pair)
BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_fastcall, KF_FASTCALL)
BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_ptr)
+BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_arena)
+BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_arena_mixed)
+BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_arena_untagged)
+BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_arena_union)
+BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_nested)
+BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_deep)
BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_ii)
#endif
BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_big)
diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h b/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h
index 65e693ada7364..a2e9e9f3184e2 100644
--- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h
+++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h
@@ -26,6 +26,12 @@ struct prog_test_ref_kfunc {
};
#endif
+#if __has_attribute(btf_type_tag)
+#define __arena_tag __attribute__((btf_type_tag("arena")))
+#else
+#define __arena_tag
+#endif
+
struct bpf_iter_testmod_seq;
struct prog_test_pass1 {
@@ -70,6 +76,45 @@ struct prog_test_ret_ptr { /* 16 bytes: contains a pointer */
__u64 tag;
};
+struct prog_test_ret_arena { /* 16 bytes: two arena pointers */
+ void __arena_tag *a;
+ void __arena_tag *b;
+};
+
+struct prog_test_ret_arena_mixed { /* 16 bytes: an arena pointer and a scalar */
+ void __arena_tag *p;
+ __u64 tag;
+};
+
+struct prog_test_ret_arena_untagged { /* 16 bytes: 'b' lacks the arena tag */
+ void __arena_tag *a;
+ void *b;
+};
+
+union prog_test_ret_arena_union { /* 8 bytes: 'b' lacks the arena tag */
+ void __arena_tag *a;
+ void *b;
+};
+
+struct prog_test_ret_nested { /* 16 bytes: the pointer hides one level down */
+ struct {
+ void *p;
+ } in;
+ __u64 tag;
+};
+
+struct prog_test_ret_deep { /* 8 bytes, but nested past the 4-level walk limit */
+ struct {
+ struct {
+ struct {
+ struct {
+ __u64 v;
+ } l4;
+ } l3;
+ } l2;
+ } l1;
+};
+
struct prog_test_ret_big { /* 24 bytes: too large for R0:R2 */
__u64 a;
__u64 b;
@@ -159,6 +204,12 @@ struct prog_test_ret_pair bpf_kfunc_call_test_ret_pair(__u64 a, __u64 b) __ksym;
struct prog_test_ret_pair bpf_kfunc_call_test_ret_fastcall(__u64 a, __u64 b) __ksym;
struct prog_test_ret_ii bpf_kfunc_call_test_ret_ii(int a, int b) __ksym;
struct prog_test_ret_ptr bpf_kfunc_call_test_ret_ptr(__u64 tag) __ksym;
+struct prog_test_ret_nested bpf_kfunc_call_test_ret_nested(__u64 tag) __ksym;
+struct prog_test_ret_arena bpf_kfunc_call_test_ret_arena(__u64 addr) __ksym;
+struct prog_test_ret_arena_mixed bpf_kfunc_call_test_ret_arena_mixed(__u64 addr) __ksym;
+struct prog_test_ret_arena_untagged bpf_kfunc_call_test_ret_arena_untagged(__u64 addr) __ksym;
+union prog_test_ret_arena_union bpf_kfunc_call_test_ret_arena_union(__u64 addr) __ksym;
+struct prog_test_ret_deep bpf_kfunc_call_test_ret_deep(__u64 v) __ksym;
struct prog_test_ret_big bpf_kfunc_call_test_ret_big(void) __ksym;
__u64 bpf_kfunc_call_stack_arg(__u64 a, __u64 b, __u64 c, __u64 d,
__u64 e, __u64 f, __u64 g, __u64 h,
|
| 3/2 | 2026/08/27 06:43 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 17935 output: 21 thoughts: 361 Thoughts: **My Analysis of the BPF Verifier Patch**
Okay, so this patch is a functional change, a pretty interesting one actually. The BPF verifier is being updated to permit kfuncs and global subprograms to return structs or unions by value, *provided* those aggregates are made up entirely of scalars and arena pointers (defined by that `btf_type_tag("arena")` attribute). Previously, only scalars were allowed. This is a significant expansion of what's permissible, and it's going to require careful review.
My immediate focus is going to be on the implementation details. I need to dig into the code, specifically the modifications in `kernel/bpf/btf.c` and `kernel/bpf/verifier.c`. I'm seeing new functions like `btf_type_is_arena_ptr` which is going to be crucial for verifying these arena pointer types, and `btf_struct_is_composed_of`, that looks like it's taking the place of `btf_type_is_scalar_struct` in a few spots. Then there are changes to the core call-checking and return-handling functions: `check_func_call`, `prepare_func_exit`, `mark_kfunc_ret_regs`, and `check_kfunc_call`. These are the heart of the verifier logic, so handling multiple return registers and those arena pointers correctly is going to be key.
This has fuzzing written all over it. This is a clear expansion of functionality and, as with any change to the verifier, I'd want to be sure it's robust against misconfigurations or malicious input. Time to put the fuzzer on these.
First I need to identify the key functions to focus my attention on. I'm thinking:
* `btf_type_is_arena_ptr`
* `btf_struct_is_composed_of`
* `btf_struct_member_walk`
* `mark_kfunc_ret_regs`
* `check_kfunc_call`
* `check_func_call`
* `prepare_func_exit`
I want to make sure I understand the full scope of what's changing.
One more thing. I need to make sure I'm testing with the correct kernel configurations. BPF and BPF\_SYSCALL should be standard, and BPF\_JIT too. But I need to double-check if any special configs related to BPF arenas are needed to enable this functionality. Let's do a quick grep for `BPF_ARENA` or related terms, just to make sure I don't miss anything that's needed for the full picture.
|
| 4/2 | 2026/08/27 06:43 | tool | grepper |
0mError:no matches Args: {
"Expression": "CONFIG_BPF_ARENA"
}Results: {
"error": "no matches"
} |
| 5/2 | 2026/08/27 06:43 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 18922 output: 28 thoughts: 18 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Kernel BPF Arena: Hunting for Configuration Options** Alright, so I need to understand how the kernel's BPF subsystem handles memory allocation and, specifically, how an "arena" might be involved. My first instinct is always to check the configuration options. If there's an "arena," it's likely controlled by some `Kconfig` setting, determining its size, enabling/disabling it, or tweaking its behavior. Therefore, I'm going to jump into the kernel source and immediately `grep` for "arena" within `kernel/bpf/Kconfig`. That's the quickest way to get a bird's-eye view of any existing configuration knobs related to this concept. It's a fundamental initial step to check which options are in the source tree, and from those, quickly look for relationships to other BPF subsystem parameters and related source code. I expect I'll quickly find what I'm looking for there. Once I have the configuration options, then I can see what source files and code paths are influenced by those settings. |
| 6/2 | 2026/08/27 06:43 | tool | grepper |
0mError:no matches Args: {
"Expression": "arena",
"PathPrefix": "kernel/bpf/Kconfig"
}Results: {
"error": "no matches"
} |
| 7/2 | 2026/08/27 06:43 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 19222 output: 22 thoughts: 17 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience: **Investigating a Potential BPF Arena Issue** Okay, so I'm thinking, based on what I've seen, this feels like it could be a memory management issue specifically related to the BPF arena. My initial hunch is to dive into the kernel code, and my first instinct is to zero in on `kernel/bpf/arena.c`, or something closely related within the BPF subsystem. I want to see how the arena memory is being allocated, deallocated, and how it’s being tracked. I'm looking for potential race conditions, fragmentation problems, or possibly memory leaks within that section of the kernel. A quick scan of related files might also be warranted, to see how that arena is used and interacted with across other parts of the BPF system. I'm expecting this to provide the key to figuring out this issue, so this is where I'll begin. |
| 8/2 | 2026/08/27 06:44 | tool | codesearch-dir-index |
0mArgs:{
"Dir": "kernel/bpf"
}Results: {
"Files": [
"arena.c",
"arraymap.c",
"backtrack.c",
"bloom_filter.c",
"bpf_cgrp_storage.c",
"bpf_inode_storage.c",
"bpf_insn_array.c",
"bpf_iter.c",
"bpf_local_storage.c",
"bpf_lru_list.c",
"bpf_lru_list.h",
"bpf_lsm.c",
"bpf_lsm_proto.c",
"bpf_struct_ops.c",
"bpf_task_storage.c",
"btf.c",
"btf_iter.c",
"btf_relocate.c",
"cfg.c",
"cgroup.c",
"cgroup_iter.c",
"check_btf.c",
"cnum.c",
"cnum_defs.h",
"const_fold.c",
"core.c",
"cpumap.c",
"cpumask.c",
"crypto.c",
"devmap.c",
"diagnostics.c",
"diagnostics.h",
"disasm.c",
"disasm.h",
"dispatcher.c",
"dmabuf_iter.c",
"fixups.c",
"hashtab.c",
"helpers.c",
"inode.c",
"kmem_cache_iter.c",
"link_iter.c",
"liveness.c",
"local_storage.c",
"log.c",
"lpm_trie.c",
"map_in_map.c",
"map_in_map.h",
"map_iter.c",
"memalloc.c",
"mmap_unlock_work.h",
"mprog.c",
"net_namespace.c",
"offload.c",
"percpu_freelist.c",
"percpu_freelist.h",
"prog_iter.c",
"queue_stack_maps.c",
"range_tree.c",
"range_tree.h",
"relo_core.c",
"reuseport_array.c",
"ringbuf.c",
"rqspinlock.c",
"rqspinlock.h",
"stackmap.c",
"states.c",
"stream.c",
"syscall.c",
"sysfs_btf.c",
"task_iter.c",
"tcx.c",
"tnum.c",
"token.c",
"trampoline.c",
"verifier.c"
],
"Subdirs": [
"preload"
]
} |
| 9/2 | 2026/08/27 06:44 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 20108 output: 29 thoughts: 14 Thoughts: Here's a summary of my thought process: **Analyzing Conditional Compilation in `arena.c`** Okay, let's dive in. My immediate goal is to determine if the `arena.c` file has any conditional compilation directives baked into it. I need to understand if the code within that file behaves differently depending on certain build configurations, feature flags, or platform specifics. I'm going to look for preprocessor directives like `#ifdef`, `#ifndef`, `#if`, `#else`, and `#endif` that could affect which sections of code are included during the compilation process. Identifying these directives is key to understanding the file's flexibility and potential variability across different deployments or use cases. This information will inform my overall understanding of the build system and how this particular module integrates with the broader project. I'm looking for clues about the dependencies and how this file is used. |
| 10/2 | 2026/08/27 06:44 | tool | grepper |
0mArgs:{
"Expression": "arena.o",
"PathPrefix": "kernel/bpf/Makefile"
}Results: {
"Output": "kernel/bpf/Makefile=19=ifeq ($(CONFIG_MMU)$(CONFIG_64BIT),yy)\nkernel/bpf/Makefile:20:obj-$(CONFIG_BPF_SYSCALL) += arena.o range_tree.o\nkernel/bpf/Makefile-21-endif\n"
} |
| 11/2 | 2026/08/27 06:44 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 20416 output: 35 thoughts: 645 Thoughts: **My Analysis of Key BPF Functions** Okay, so I'm diving into this BPF code. First, the usual suspects are at play: `CONFIG_MMU`, `CONFIG_64BIT`, and `CONFIG_BPF_SYSCALL` – nothing unexpected there. Now, let's get down to the core functions. My initial scan yielded a list of functions that seem particularly relevant, clustered around `btf.c` and `verifier.c`. In `btf.c`, I saw `btf_type_is_arena_ptr`, `btf_scan_type_tags` (which was modified), `btf_validate_return_type` (also modified), and `btf_prepare_func_args` (modified again). Then, over in `verifier.c`, the list includes `check_func_call` (modified), `prepare_func_exit` (modified), `mark_kfunc_ret_regs` (modified), `btf_member_kind_allowed` (added), `btf_struct_member_walk` (added), `btf_struct_is_composed_of` (added), `btf_type_is_scalar_struct` (modified), `btf_member_path_str` (added), and `check_kfunc_call` (modified). After a first pass, I narrowed it down to: `btf_type_is_arena_ptr`, `btf_struct_member_walk`, `btf_struct_is_composed_of`, `mark_kfunc_ret_regs`, `check_kfunc_call`, `check_func_call`, and `prepare_func_exit`. I checked which functions are static. `btf_struct_member_walk`, `mark_kfunc_ret_regs`, `check_kfunc_call`, `check_func_call`, and `prepare_func_exit` are all static. That's a key piece of information, as I need to focus on non-static functions for the API entry points. Given that constraint, I refined the list to focus on `btf_struct_is_composed_of`, `btf_type_is_arena_ptr`, `btf_prepare_func_args`, and `bpf_check`. I'll need to double check `btf_struct_is_composed_of` and `btf_type_is_arena_ptr`. It looks like these functions are the key ones that I should look at right now. |
| 12/2 | 2026/08/27 06:44 | tool | grepper |
0mArgs:{
"Expression": "btf_struct_is_composed_of",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/btf.c=7956=static int btf_validate_return_type(struct bpf_verifier_env *env, struct btf *btf,\n--\nkernel/bpf/btf.c-7990-\nkernel/bpf/btf.c:7991:\t\tif (local_func || btf_struct_is_composed_of(env, btf, t, member_kinds))\nkernel/bpf/btf.c-7992-\t\t\treturn 0;\n--\nkernel/bpf/verifier.c=11673=static bool btf_struct_member_walk(struct bpf_verifier_env *env, const struct btf *btf,\n--\nkernel/bpf/verifier.c-11719-\nkernel/bpf/verifier.c:11720:bool btf_struct_is_composed_of(struct bpf_verifier_env *env,\nkernel/bpf/verifier.c-11721-\t\t\t const struct btf *btf,\n--\nkernel/bpf/verifier.c=11727=static bool btf_type_is_scalar_struct(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-11730-{\nkernel/bpf/verifier.c:11731:\treturn btf_struct_is_composed_of(env, btf, t, BTF_MEMBER_SCALAR);\nkernel/bpf/verifier.c-11732-}\n"
} |
| 13/2 | 2026/08/27 06:44 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 22038 output: 35 thoughts: 16 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience and written in the first person: **Checking `btf_type_is_arena_ptr`** Okay, let's dive into this `btf_type_is_arena_ptr` function. Given the context, I'm immediately thinking about how it fits into the broader picture of BTF type analysis, and specifically, memory management in the kernel. I'm assuming, based on the naming, that this is responsible for determining if a given BTF type descriptor represents a pointer that resides within some managed arena or pool. The immediate implications are important. Is this check performed at compile time, or runtime? What are the calling contexts of this function and are there other checks being performed to qualify a pointer? And does it relate to garbage collection, reference counting, or some other memory management strategy? My intuition tells me this is part of ensuring safety and preventing memory leaks or use-after-free scenarios, likely within a specific kernel subsystem or driver. Are there implications for concurrency here? Are we dealing with atomically accessed memory locations or are there potentially race conditions to worry about? I need to think about the various type qualifiers too; const, volatile, etc. This function might have to check more than just the plain BTF type kind. This could have some real implications for how the verifier functions as well. I need to quickly assess the possible cases where a pointer *wouldn't* be considered arena-managed. And by definition, pointers returned from malloc, or even something returned from an eBPF map are not. I have to make sure my logic is as clear as possible. I'm starting to formulate a mental model, and I'll need to double check the implementation to verify my hypotheses. This is a critical building block for BTF-based analysis, and any errors here can lead to subtle bugs down the line. Let's see how it works... |
| 14/2 | 2026/08/27 06:45 | tool | grepper |
0mArgs:{
"Expression": "btf_type_is_arena_ptr",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/btf.c=3486=static int btf_type_tag_walk(const struct btf *btf,\n--\nkernel/bpf/btf.c-3525-\nkernel/bpf/btf.c:3526:bool btf_type_is_arena_ptr(const struct btf *btf, const struct btf_type *t)\nkernel/bpf/btf.c-3527-{\n--\nkernel/bpf/btf.c=7946=static void btf_scan_type_tags(const struct btf *btf, u32 type_id, u32 *tags)\n--\nkernel/bpf/btf.c-7950-\nkernel/bpf/btf.c:7951:\tif (btf_type_is_arena_ptr(btf, t))\nkernel/bpf/btf.c-7952-\t\t*tags |= ARG_TAG_ARENA;\n--\nkernel/bpf/btf.c=7956=static int btf_validate_return_type(struct bpf_verifier_env *env, struct btf *btf,\n--\nkernel/bpf/btf.c-7965-\t */\nkernel/bpf/btf.c:7966:\tif (subprog \u0026\u0026 btf_type_is_arena_ptr(btf, t))\nkernel/bpf/btf.c-7967-\t\treturn 0;\n--\nkernel/bpf/verifier.c=11658=static bool btf_member_kind_allowed(const struct btf *btf, const struct btf_type *t,\n--\nkernel/bpf/verifier.c-11662-\t\treturn true;\nkernel/bpf/verifier.c:11663:\tif ((member_kinds \u0026 BTF_MEMBER_ARENA_PTR) \u0026\u0026 btf_type_is_arena_ptr(btf, t))\nkernel/bpf/verifier.c-11664-\t\treturn true;\n"
} |
| 15/2 | 2026/08/27 06:45 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 23018 output: 177 thoughts: 465 Thoughts: **My Analysis of the BPF Verifier Changes**
Okay, so the core of this patch seems to be about how the BPF verifier handles return types from kfuncs and subprograms. Specifically, it's about expanding the allowed return types beyond just simple scalars to include structs or unions, as long as they're composed of scalars and arena pointers. I know the verifier is a critical component, and any changes here warrant careful attention.
The entry points for the checks are `btf_prepare_func_args`, `check_kfunc_call`, and `check_func_call`. Since `check_kfunc_call` and `check_func_call` are static, I need to focus on the non-static functions directly related to this new logic: `btf_struct_is_composed_of` and `btf_type_is_arena_ptr`, and also `btf_prepare_func_args`. I should double-check to be sure I haven't missed any other non-static functions involved in this. I've already confirmed those three, and I'll keep an eye out for any others.
As for configuration, `CONFIG_BPF_SYSCALL` is standard, which is good. The patch mentions `btf_type_tag("arena")`, and I initially looked for a specific `CONFIG_BPF_ARENA` but confirmed that isn't present, but `arena.c` is compiled when `CONFIG_MMU`, `CONFIG_64BIT`, and `CONFIG_BPF_SYSCALL` are enabled. So, the implication is that no special config flags are needed beyond the standard BPF ones.
So, let's nail down the reasoning. This patch enables returning structs or unions by value from kfuncs and global subprograms within the BPF verifier if they're composed of scalars and arena pointers. This is a clear functional change in how the verifier handles type checking and register state tracking, particularly regarding multiple return registers and the arena pointers. It's a fundamental shift in the permitted return types. From userspace, this is triggered via the `bpf()` syscall when loading BPF programs that call the kfuncs or subprograms that now return structs/unions.
I'll be paying close attention to these symbols:
* `btf_struct_is_composed_of`
* `btf_type_is_arena_ptr`
* `btf_prepare_func_args`
Okay, time to call `set-results`.
|
| 16/2 | 2026/08/27 06:45 | tool | set-results |
0mArgs:{
"EnableConfigs": [],
"FocusSymbols": [
"btf_struct_is_composed_of",
"btf_type_is_arena_ptr",
"btf_prepare_func_args"
],
"Reasoning": "The patch modifies the BPF verifier to allow returning structs or unions by value from kfuncs and global subprograms if they are composed of scalars and arena pointers (pointers with the `btf_type_tag(\"arena\")` attribute). Previously, only scalars were allowed. This is a functional change in the verifier's type checking logic and register state tracking (handling multiple return registers and arena pointers). It is reachable from userspace via the `bpf()` syscall when loading BPF programs that call such kfuncs or subprograms.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": null,
"FocusSymbols": [
"btf_struct_is_composed_of",
"btf_type_is_arena_ptr",
"btf_prepare_func_args"
],
"Reasoning": "The patch modifies the BPF verifier to allow returning structs or unions by value from kfuncs and global subprograms if they are composed of scalars and arena pointers (pointers with the `btf_type_tag(\"arena\")` attribute). Previously, only scalars were allowed. This is a functional change in the verifier's type checking logic and register state tracking (handling multiple return registers and arena pointers). It is reachable from userspace via the `bpf()` syscall when loading BPF programs that call such kfuncs or subprograms.",
"WorthFuzzing": true
} |
| 17/1 | 2026/08/27 06:45 | action | If |
0mArgs:{
"WorthFuzzing": true
}Results: null |
| 18/2 | 2026/08/27 06:45 | agent | kmsan-evaluator |
0mModel:best-expensive Args: null Results: {
"KMSANReasoning": "The patch modifies the BPF verifier to allow kfuncs and subprograms to return structs/unions containing arena pointers by value. It updates the verifier's type checking logic and register state tracking. It does not introduce any new memory allocations, kernel-to-user data copies, or complex data structures that could expose uninitialized memory. The changes are confined to the static analysis phase of BPF programs. Standard KASAN and other bug detectors are sufficient to catch any potential memory safety issues in the verifier.",
"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 af7e8a8a9501e5ea3bca9b8e122fdc4ecc545e63
Author: syz-cluster <triage@syzkaller.com>
Date: Thu Aug 27 06:43:34 2026 +0000
syz-cluster: applied patch under review
diff --git a/Documentation/bpf/kfuncs.rst b/Documentation/bpf/kfuncs.rst
index 89dea6b0b0241..96bf63d344322 100644
--- a/Documentation/bpf/kfuncs.rst
+++ b/Documentation/bpf/kfuncs.rst
@@ -581,20 +581,27 @@ against the arena. Larger accesses must verify the range explicitly.
A kfunc may return a scalar, a pointer, or a small struct or union by
value. A scalar or pointer of up to 8 bytes is returned in R0, as usual.
-A struct or union returned by value must be composed only of scalars
-(recursively), where a scalar is an integer or an enum; arrays of scalars are
-allowed as members. Its bytes are handed back to the program as the raw
-contents of R0 (and R2), so a pointer field would be laundered into a scalar
-and escape the verifier's pointer provenance and reference tracking. A struct
-or union with a pointer member is therefore rejected at load time, and so is
-one with a floating-point member, which the ABI may not return in R0:R2 at
-all.
+A struct or union returned by value must be composed only of scalars and arena
+pointers (recursively), where a scalar is an integer or an enum and an arena
+pointer is one carrying the ``btf_type_tag("arena")`` attribute; arrays of
+either are allowed as members. Its bytes are handed back to the program as the
+raw contents of R0 (and R2), so a member of any other pointer type would be
+laundered into a scalar and escape the verifier's pointer provenance and
+reference tracking. A struct or union with such a member is therefore rejected
+at load time, and so is one with a floating-point member, which the ABI may not
+return in R0:R2 at all.
+
+An arena pointer member is handed back as a scalar too, but nothing is lost by
+that. The program must ``cast_kern()`` the value before it can be used, and the
+verifier allows that cast on any scalar, so a laundered arena address gives the
+program no reach it did not already have. The result is confined to the
+program's arena in either case.
A kfunc may also return a value larger than 8 bytes and up to 16 bytes -- a
-scalar-only struct or union, or an ``__int128``. Such a value is returned
-in the register pair R0:R2, matching the convention LLVM uses for the BPF
-target: the first 8 bytes in R0 and the second 8 bytes in R2. A struct or
-union of 8 bytes or less is returned in R0 alone.
+struct or union of scalars and arena pointers, or an ``__int128``. Such a
+value is returned in the register pair R0:R2, matching the convention LLVM
+uses for the BPF target: the first 8 bytes in R0 and the second 8 bytes in R2.
+A struct or union of 8 bytes or less is returned in R0 alone.
::
@@ -620,10 +627,10 @@ used when the program is JITed, since the interpreter propagates only R0 out of
a subprogram: without a JIT the return value stays in R0 alone, and a caller
reading R2 is rejected for reading an uninitialized register. A global
subprogram is verified in isolation, so its by-value struct or union return is
-restricted to scalars just like a kfunc's; a static subprogram is verified
-inline and has no such restriction. The main program is not covered: its return
-value is the program's exit code, read out of R0 alone, so a declared upper
-half is never looked at.
+restricted to scalars and arena pointers just like a kfunc's; a static
+subprogram is verified inline and has no such restriction. The main program is
+not covered: its return value is the program's exit code, read out of R0
+alone, so a declared upper half is never looked at.
A global subprogram must leave a scalar in *every* register of the pair, so
both halves of the returned value have to be assigned. Leaving the upper half
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 004b06785521c..ae9f606539f41 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -1488,8 +1488,15 @@ int bpf_jmp_offset(struct bpf_insn *insn);
struct bpf_iarray *bpf_insn_successors(struct bpf_verifier_env *env, u32 idx);
void bpf_fmt_stack_mask(char *buf, ssize_t buf_sz, u64 stack_mask);
bool bpf_subprog_is_global(const struct bpf_verifier_env *env, int subprog);
-bool btf_type_is_scalar_struct(struct bpf_verifier_env *env, const struct btf *btf,
- const struct btf_type *t, int rec);
+
+/* Kinds of member a by-value struct or union may be composed of. */
+enum btf_member_kind {
+ BTF_MEMBER_SCALAR = BIT(0), /* an int or an enum */
+ BTF_MEMBER_ARENA_PTR = BIT(1), /* a pointer carrying the "arena" type tag */
+};
+
+bool btf_struct_is_composed_of(struct bpf_verifier_env *env, const struct btf *btf,
+ const struct btf_type *t, u32 member_kinds);
int bpf_find_subprog(struct bpf_verifier_env *env, int off);
bool bpf_is_throw_kfunc(struct bpf_insn *insn);
diff --git a/include/linux/btf.h b/include/linux/btf.h
index 89d5a5c4f1171..ddd0f4f32d24b 100644
--- a/include/linux/btf.h
+++ b/include/linux/btf.h
@@ -235,6 +235,7 @@ struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type
u32 field_mask, u32 value_size);
int btf_check_and_fixup_fields(const struct btf *btf, struct btf_record *rec);
bool btf_type_is_void(const struct btf_type *t);
+bool btf_type_is_arena_ptr(const struct btf *btf, const struct btf_type *t);
s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p);
struct btf *btf_get_module_btf(const struct module *module);
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 91b8ce77f699a..f6d82a8fd617b 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -3523,6 +3523,22 @@ static int btf_type_tag_walk(const struct btf *btf,
return 0;
}
+bool btf_type_is_arena_ptr(const struct btf *btf, const struct btf_type *t)
+{
+ if (!btf_type_is_ptr(t))
+ return false;
+
+ for (t = btf_type_by_id(btf, t->type); btf_type_is_modifier(t);
+ t = btf_type_by_id(btf, t->type)) {
+ if (!btf_type_is_type_tag(t) || btf_type_kflag(t))
+ continue;
+ if (!strcmp(__btf_name_by_offset(btf, t->name_off), "arena"))
+ return true;
+ }
+
+ return false;
+}
+
static int btf_find_kptr(const struct btf *btf, const struct btf_type *t,
u32 off, int sz, struct btf_field_info *info, u32 field_mask)
{
@@ -7927,51 +7943,19 @@ static int btf_scan_decl_tags(struct bpf_verifier_env *env,
return 0;
}
-static int btf_scan_type_tags(struct bpf_verifier_env *env,
- const struct btf *btf, u32 type_id,
- u32 *tags)
+static void btf_scan_type_tags(const struct btf *btf, u32 type_id, u32 *tags)
{
- static const struct btf_type_tag_match func_type_tags[] = {
- { "arena", ARG_TAG_ARENA },
- };
- struct btf_type_tag_walk_ctx ctx;
- const struct btf_type *t;
- int err;
-
/* Find the first pointer type in the chain. */
- t = btf_type_skip_modifiers(btf, type_id, NULL);
+ const struct btf_type *t = btf_type_skip_modifiers(btf, type_id, NULL);
- /*
- * We currently reject type tags on non-pointer types,
- * which neither LLVM nor GCC support anyway.
- */
- if (!t || !btf_type_is_ptr(t))
- return 0;
-
- ctx.t = t;
- err = btf_type_tag_walk(btf, &ctx, func_type_tags,
- ARRAY_SIZE(func_type_tags));
- if (err) {
- bpf_log(&env->log,
- "function signature member has multiple type tags\n");
- return err;
- }
- *tags |= ctx.res;
-
- return 0;
+ if (btf_type_is_arena_ptr(btf, t))
+ *tags |= ARG_TAG_ARENA;
}
/* Check whether the type is a valid return type. */
static int btf_validate_return_type(struct bpf_verifier_env *env, struct btf *btf,
const struct btf_type *t, int subprog, bool is_global)
{
- u32 tags = 0;
- int err;
-
- err = btf_scan_type_tags(env, btf, t->type, &tags);
- if (err)
- return err;
-
t = btf_type_skip_modifiers(btf, t->type, NULL);
/*
@@ -7979,7 +7963,7 @@ static int btf_validate_return_type(struct bpf_verifier_env *env, struct btf *bt
* General arena variables are not allowed, since it makes no sense to return by value
* a variable that's on the heap in the first place.
*/
- if (subprog && (tags & ARG_TAG_ARENA) && btf_type_is_ptr(t))
+ if (subprog && btf_type_is_arena_ptr(btf, t))
return 0;
/* We always accept void or scalars. */
@@ -7989,13 +7973,22 @@ static int btf_validate_return_type(struct bpf_verifier_env *env, struct btf *bt
if (btf_type_is_struct(t) && t->size <= 16) {
/*
* A global function's caller models the return as an opaque
- * scalar pair, so it may only return scalars by value. A local
- * function is verified inline, so a pointer field stays tracked
- * and needs no such restriction.
+ * scalar pair, so a pointer member would be laundered into a
+ * scalar and escape provenance and reference tracking. Only
+ * scalars and arena pointers are allowed: an arena pointer has
+ * no provenance to lose, since a program may already derive one
+ * from any scalar with addr_space_cast(), which confines the
+ * result to the arena. A local function is verified inline, so
+ * its caller receives the real register state and any member is
+ * fine.
*/
bool local_func = subprog && !is_global;
+ u32 member_kinds = BTF_MEMBER_SCALAR;
+
+ if (subprog)
+ member_kinds |= BTF_MEMBER_ARENA_PTR;
- if (local_func || btf_type_is_scalar_struct(env, btf, t, 0))
+ if (local_func || btf_struct_is_composed_of(env, btf, t, member_kinds))
return 0;
}
@@ -8091,7 +8084,8 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog)
if (is_global) {
bpf_log(log,
"Global function %s() has unsupported return type. "
- "Only void, scalar, or a scalar-only struct/union up to 16 bytes is supported.\n",
+ "Only void, a scalar, an arena pointer, or a struct/union of "
+ "scalars and arena pointers up to 16 bytes is supported.\n",
tname);
}
return err;
@@ -8106,9 +8100,7 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog)
if (err)
return err;
- err = btf_scan_type_tags(env, btf, args[i].type, &tags);
- if (err)
- return err;
+ btf_scan_type_tags(btf, args[i].type, &tags);
t = btf_type_by_id(btf, args[i].type);
while (btf_type_is_modifier(t))
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index e036ae20bf6b9..9a36808136c9a 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -9995,9 +9995,14 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
*/
if (!returns_void) {
nregs = bpf_ret_reg_pair(env, subprog) ? 2 : 1;
- for (i = 0; i < nregs; i++)
- mark_reg_unknown(env, caller->regs, ret_regs[i]);
+ mark_reg_unknown(env, caller->regs, ret_regs[0]);
bpf_diag_mod_end(env);
+ for (i = 1; i < nregs; i++) {
+ bpf_diag_mod_begin(env, &caller->regs[ret_regs[i]], NULL,
+ BPF_DIAG_MOD_WRITE);
+ mark_reg_unknown(env, caller->regs, ret_regs[i]);
+ bpf_diag_mod_end(env);
+ }
}
if (env->subprog_info[subprog].might_throw) {
@@ -10403,10 +10408,14 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
* return to the caller whatever the callee had in the
* return register(s)
*/
- bpf_diag_mod_begin(env, &caller->regs[BPF_REG_0], r0, BPF_DIAG_MOD_WRITE);
- for (i = 0; i < nregs; i++)
- caller->regs[ret_regs[i]] = callee->regs[ret_regs[i]];
- bpf_diag_mod_end(env);
+ for (i = 0; i < nregs; i++) {
+ u32 regno = ret_regs[i];
+
+ bpf_diag_mod_begin(env, &caller->regs[regno], &callee->regs[regno],
+ BPF_DIAG_MOD_WRITE);
+ caller->regs[regno] = callee->regs[regno];
+ bpf_diag_mod_end(env);
+ }
}
/* for callbacks like bpf_loop or bpf_for_each_map_elem go back to callsite,
@@ -11342,15 +11351,33 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
/*
* Mark the register(s) holding a @size byte kfunc return value as unknown
- * scalars. Both halves of a register pair are treated the same way.
+ * scalars and return how many registers that took. Both halves of a register
+ * pair are treated the same way.
+ *
+ * R0 is written in the diagnostic modification scope the caller opened before
+ * the caller-saved scrub, since R0 gets no scrub record of its own and that
+ * scope is what carries its pre-call value into the history. A scope tracks a
+ * single register and does not nest, so close it before recording the rest of
+ * the return value. A value that fits in R0 leaves the caller's scope open for
+ * the caller to close, so a later write to R0 still lands in it.
*/
-static void mark_kfunc_ret_regs(struct bpf_verifier_env *env,
- struct bpf_reg_state *regs, u32 size)
+static u32 mark_kfunc_ret_regs(struct bpf_verifier_env *env,
+ struct bpf_reg_state *regs, u32 size)
{
u32 i, nregs = ret_regs_cnt(size);
- for (i = 0; i < nregs; i++)
+ mark_reg_unknown(env, regs, ret_regs[0]);
+ if (nregs == 1)
+ return nregs;
+
+ bpf_diag_mod_end(env);
+ for (i = 1; i < nregs; i++) {
+ bpf_diag_mod_begin(env, ®s[ret_regs[i]], NULL, BPF_DIAG_MOD_WRITE);
mark_reg_unknown(env, regs, ret_regs[i]);
+ bpf_diag_mod_end(env);
+ }
+
+ return nregs;
}
static bool is_kfunc_acquire(struct bpf_call_arg_meta *meta)
@@ -11619,10 +11646,33 @@ static bool is_kfunc_arg_implicit(const struct bpf_call_arg_meta *meta, u32 arg_
return argn <= arg_idx;
}
-/* Returns true if struct is composed of scalars, 4 levels of nesting allowed */
-bool btf_type_is_scalar_struct(struct bpf_verifier_env *env,
- const struct btf *btf,
- const struct btf_type *t, int rec)
+#define BTF_MEMBER_MAX_DEPTH 4
+#define BTF_MEMBER_PATH_LEN 64
+
+struct btf_member_path {
+ const struct btf_member *member[BTF_MEMBER_MAX_DEPTH];
+ int depth;
+ bool too_deep;
+};
+
+static bool btf_member_kind_allowed(const struct btf *btf, const struct btf_type *t,
+ u32 member_kinds)
+{
+ if ((member_kinds & BTF_MEMBER_SCALAR) && btf_type_is_scalar(t))
+ return true;
+ if ((member_kinds & BTF_MEMBER_ARENA_PTR) && btf_type_is_arena_ptr(btf, t))
+ return true;
+ return false;
+}
+
+/*
+ * Returns true if every member of struct @t is of a kind listed in
+ * @member_kinds, BTF_MEMBER_MAX_DEPTH levels of nesting allowed. An array
+ * member counts as its element type.
+ */
+static bool btf_struct_member_walk(struct bpf_verifier_env *env, const struct btf *btf,
+ const struct btf_type *t, u32 member_kinds, int rec,
+ struct btf_member_path *path)
{
const struct btf_type *member_type;
const struct btf_member *member;
@@ -11636,27 +11686,65 @@ bool btf_type_is_scalar_struct(struct bpf_verifier_env *env,
member_type = btf_type_skip_modifiers(btf, member->type, NULL);
if (btf_type_is_struct(member_type)) {
- if (rec >= 3) {
+ if (rec >= BTF_MEMBER_MAX_DEPTH - 1) {
verbose(env, "max struct nesting depth exceeded\n");
+ if (path)
+ path->too_deep = true;
return false;
}
- if (!btf_type_is_scalar_struct(env, btf, member_type, rec + 1))
- return false;
+ if (!btf_struct_member_walk(env, btf, member_type, member_kinds,
+ rec + 1, path))
+ goto bad_path;
continue;
}
if (btf_type_is_array(member_type)) {
array = btf_array(member_type);
if (!array->nelems)
- return false;
+ goto bad_member;
member_type = btf_type_skip_modifiers(btf, array->type, NULL);
- if (!btf_type_is_scalar(member_type))
- return false;
- continue;
}
- if (!btf_type_is_scalar(member_type))
- return false;
+ if (!btf_member_kind_allowed(btf, member_type, member_kinds))
+ goto bad_member;
}
return true;
+
+bad_member:
+ if (path)
+ path->depth = rec + 1;
+bad_path:
+ if (path && path->depth)
+ path->member[rec] = member;
+ return false;
+}
+
+bool btf_struct_is_composed_of(struct bpf_verifier_env *env,
+ const struct btf *btf,
+ const struct btf_type *t, u32 member_kinds)
+{
+ return btf_struct_member_walk(env, btf, t, member_kinds, 0, NULL);
+}
+
+static bool btf_type_is_scalar_struct(struct bpf_verifier_env *env,
+ const struct btf *btf,
+ const struct btf_type *t)
+{
+ return btf_struct_is_composed_of(env, btf, t, BTF_MEMBER_SCALAR);
+}
+
+static void btf_member_path_str(const struct btf *btf, const struct btf_member_path *path,
+ char *buf, size_t buf_sz)
+{
+ size_t len = 0;
+ int i;
+
+ buf[0] = '\0';
+ for (i = 0; i < path->depth; i++) {
+ const char *name = btf_name_by_offset(btf, path->member[i]->name_off);
+
+ if (!name || !name[0])
+ continue;
+ len += scnprintf(buf + len, buf_sz - len, "%s%s", len ? "." : "", name);
+ }
}
enum kfunc_ptr_arg_type {
@@ -12039,7 +12127,7 @@ get_kfunc_arg_type(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,
(is_kfunc_arg_mem_size(meta->btf, &args[arg + 1]) ||
is_kfunc_arg_const_mem_size(meta->btf, &args[arg + 1]))) {
if (!btf_type_is_void(ref_t) && !btf_type_is_scalar(ref_t) &&
- !btf_type_is_scalar_struct(env, meta->btf, ref_t, 0)) {
+ !btf_type_is_scalar_struct(env, meta->btf, ref_t)) {
verbose(env, "%s pointer type %s %s must point to void, scalar, or struct with scalar\n",
reg_arg_name(env, argno), btf_type_str(ref_t), ref_tname);
return -EINVAL;
@@ -12055,7 +12143,7 @@ get_kfunc_arg_type(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,
* scalars. The access size is derived from the pointed-to BTF type.
*/
if (!btf_type_is_scalar(ref_t) &&
- !btf_type_is_scalar_struct(env, meta->btf, ref_t, 0)) {
+ !btf_type_is_scalar_struct(env, meta->btf, ref_t)) {
verbose(env, "%s pointer type %s %s must point to scalar, or struct with scalar\n",
reg_arg_name(env, argno), btf_type_str(ref_t), ref_tname);
return -EINVAL;
@@ -13111,7 +13199,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me
break;
}
- if (!btf_type_is_scalar_struct(env, meta->btf, ref_t, 0)) {
+ if (!btf_type_is_scalar_struct(env, meta->btf, ref_t)) {
enum bpf_reg_type reg2btf_type = lookup_reg2btf_ids(ref_id);
const char *expected_type;
@@ -13653,7 +13741,7 @@ static int check_special_kfunc(struct bpf_verifier_env *env, struct bpf_call_arg
struct_meta = btf_find_struct_meta(ret_btf, ret_btf_id);
if (is_bpf_percpu_obj_new_kfunc(meta->func_id)) {
- if (!btf_type_is_scalar_struct(env, ret_btf, ret_t, 0)) {
+ if (!btf_type_is_scalar_struct(env, ret_btf, ret_t)) {
verbose(env, "bpf_percpu_obj_new type ID argument must be of a struct of scalars\n");
return -EINVAL;
}
@@ -13767,7 +13855,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
struct bpf_insn_aux_data *insn_aux;
const char *operation;
int err, insn_idx = *insn_idx_p;
- u32 i, nargs, ptr_type_id;
+ u32 i, nargs, ptr_type_id, ret_nregs = 1;
struct bpf_kfunc_desc *desc;
struct btf *desc_btf;
int id;
@@ -14021,25 +14109,59 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
}
if (btf_type_is_scalar(t)) {
- mark_kfunc_ret_regs(env, regs, t->size);
+ ret_nregs = mark_kfunc_ret_regs(env, regs, t->size);
if (meta.btf == btf_vmlinux && (meta.func_id == special_kfunc_list[KF_bpf_res_spin_lock] ||
meta.func_id == special_kfunc_list[KF_bpf_res_spin_lock_irqsave]))
__mark_reg_const_zero(env, ®s[BPF_REG_0]);
} else if (btf_type_is_struct(t)) {
+ struct btf_member_path path = {};
+ const char *member_note = "";
+
/*
* The returned struct comes back as raw register bits modeled
- * as an unknown scalar, so it must contain only scalars:
- * otherwise a pointer field would be laundered into a scalar
- * and escape provenance and reference tracking.
+ * as an unknown scalar, so a pointer member would be laundered
+ * into a scalar and escape provenance and reference tracking.
+ * Only scalars and arena pointers are allowed: an arena pointer
+ * has no provenance to lose, since a program may already derive
+ * one from any scalar with addr_space_cast(), which confines the
+ * result to the arena.
*/
- if (!btf_type_is_scalar_struct(env, desc_btf, t, 0)) {
+ if (!btf_struct_member_walk(env, desc_btf, t,
+ BTF_MEMBER_SCALAR | BTF_MEMBER_ARENA_PTR, 0, &path)) {
verbose(env,
- "kernel function %s returns %s %s that is not composed of scalars\n",
+ "kernel function %s returns %s %s that is not composed of scalars or arena pointers\n",
func_name, btf_type_str(t),
btf_name_by_offset(desc_btf, t->name_off));
+ if (path.too_deep) {
+ member_note = bpf_diag_fmt(
+ env, " It nests structs more than %d levels deep.",
+ BTF_MEMBER_MAX_DEPTH);
+ } else if (path.depth) {
+ const struct btf_member *bad = path.member[path.depth - 1];
+ char bad_name[BTF_MEMBER_PATH_LEN];
+ const struct btf_type *bad_type;
+
+ btf_member_path_str(desc_btf, &path, bad_name, sizeof(bad_name));
+ bad_type = btf_type_skip_modifiers(desc_btf, bad->type, NULL);
+ verbose(env, "member '%s' has type %s\n", bad_name,
+ btf_type_str(bad_type));
+ member_note = bpf_diag_fmt(
+ env,
+ " Its member '%s' is %s, not a scalar or an arena pointer.",
+ bad_name, btf_type_str(bad_type));
+ }
+ bpf_diag_program_structure(
+ env, insn_idx, "unsupported kernel function return type",
+ "Call a kernel function that returns only scalars or arena pointers by value.",
+ "%s() returns %s %s by value.%s "
+ "Only kfuncs returning scalar values or arena pointers, or "
+ "structures composed of scalar values or arena pointers are "
+ "supported.",
+ func_name, btf_type_str(t),
+ btf_name_by_offset(desc_btf, t->name_off), member_note);
return -EINVAL;
}
- mark_kfunc_ret_regs(env, regs, t->size);
+ ret_nregs = mark_kfunc_ret_regs(env, regs, t->size);
} else if (btf_type_is_ptr(t)) {
ptr_type = btf_type_skip_modifiers(desc_btf, t->type, &ptr_type_id);
err = check_special_kfunc(env, &meta, regs, insn_aux, ptr_type, desc_btf);
@@ -14170,7 +14292,8 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
* Record R0 before process_iter_next_call() snapshots the alternate
* iterator path's diagnostic position.
*/
- bpf_diag_mod_end(env);
+ if (ret_nregs == 1)
+ bpf_diag_mod_end(env);
if (bpf_is_iter_next_kfunc(&meta)) {
err = process_iter_next_call(env, insn_idx, &meta);
diff --git a/tools/testing/selftests/bpf/prog_tests/aggregate_ret.c b/tools/testing/selftests/bpf/prog_tests/aggregate_ret.c
index e0b94ed10f941..07d9d6e1d6b88 100644
--- a/tools/testing/selftests/bpf/prog_tests/aggregate_ret.c
+++ b/tools/testing/selftests/bpf/prog_tests/aggregate_ret.c
@@ -1,11 +1,53 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
#include <test_progs.h>
+#include <bpf/btf.h>
#include "aggregate_ret_func.skel.h"
#include "aggregate_ret_kfunc.skel.h"
+#include "aggregate_ret_kfunc_arena.skel.h"
+
+static bool testmod_has_arena_tagged_member(void)
+{
+ struct btf *vmlinux_btf, *module_btf = NULL;
+ const struct btf_type *t;
+ bool tagged = false;
+ __s32 id;
+
+ vmlinux_btf = btf__load_vmlinux_btf();
+ if (!vmlinux_btf)
+ return false;
+
+ module_btf = btf__load_module_btf("bpf_testmod", vmlinux_btf);
+ if (!module_btf)
+ goto out;
+
+ /* prog_test_ret_arena::a is 'void __arena_tag *': PTR -> TYPE_TAG -> void */
+ id = btf__find_by_name_kind(module_btf, "prog_test_ret_arena", BTF_KIND_STRUCT);
+ if (id <= 0)
+ goto out;
+
+ t = btf__type_by_id(module_btf, btf_members(btf__type_by_id(module_btf, id))[0].type);
+ if (!t || !btf_is_ptr(t))
+ goto out;
+
+ t = btf__type_by_id(module_btf, t->type);
+ tagged = t && btf_is_type_tag(t) &&
+ !strcmp(btf__name_by_offset(module_btf, t->name_off), "arena");
+
+out:
+ btf__free(module_btf);
+ btf__free(vmlinux_btf);
+
+ return tagged;
+}
void test_aggregate_ret(void)
{
RUN_TESTS(aggregate_ret_func);
RUN_TESTS(aggregate_ret_kfunc);
+
+ if (testmod_has_arena_tagged_member())
+ RUN_TESTS(aggregate_ret_kfunc_arena);
+ else
+ test__skip();
}
diff --git a/tools/testing/selftests/bpf/progs/aggregate_ret_func.c b/tools/testing/selftests/bpf/progs/aggregate_ret_func.c
index 6f66fc822cede..f0077d951a25d 100644
--- a/tools/testing/selftests/bpf/progs/aggregate_ret_func.c
+++ b/tools/testing/selftests/bpf/progs/aggregate_ret_func.c
@@ -2,6 +2,7 @@
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
+#include <bpf_arena_common.h>
#include "bpf_misc.h"
typedef unsigned __int128 u128;
@@ -234,4 +235,149 @@ __naked int aggregate_ret_global_union_ptr_fail(void)
#endif
+/*
+ * gcc returns a by-value struct through a hidden pointer, and emits the
+ * 'r0 = r1' returning it after the __naked body's exit, leaving the
+ * subprogram falling through. Build these with clang only.
+ */
+#if defined(__clang__)
+
+struct arena_pair {
+ void __arena *lo;
+ void __arena *hi;
+};
+
+struct arena_and_scalar {
+ void __arena *p;
+ __u64 x;
+};
+
+struct arena_array {
+ void __arena *p[2];
+};
+
+struct arena_single {
+ void __arena *p;
+};
+
+union arena_upair {
+ void __arena *p;
+ __u64 halves[2];
+};
+
+__naked struct arena_pair global_ret_arena_pair(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "r2 = 0;"
+ "exit;"
+ );
+}
+
+SEC("tc")
+__load_if_JITed()
+__success __retval(0)
+__naked int aggregate_ret_global_arena_pair(void)
+{
+ asm volatile (
+ "call %[global_ret_arena_pair];"
+ "r0 = 0;"
+ "exit;"
+ :
+ : __imm(global_ret_arena_pair)
+ : __clobber_all);
+}
+
+__naked struct arena_and_scalar global_ret_arena_and_scalar(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "r2 = 0;"
+ "exit;"
+ );
+}
+
+SEC("tc")
+__load_if_JITed()
+__success __retval(0)
+__naked int aggregate_ret_global_arena_and_scalar(void)
+{
+ asm volatile (
+ "call %[global_ret_arena_and_scalar];"
+ "r0 = 0;"
+ "exit;"
+ :
+ : __imm(global_ret_arena_and_scalar)
+ : __clobber_all);
+}
+
+__naked struct arena_array global_ret_arena_array(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "r2 = 0;"
+ "exit;"
+ );
+}
+
+SEC("tc")
+__load_if_JITed()
+__success __retval(0)
+__naked int aggregate_ret_global_arena_array(void)
+{
+ asm volatile (
+ "call %[global_ret_arena_array];"
+ "r0 = 0;"
+ "exit;"
+ :
+ : __imm(global_ret_arena_array)
+ : __clobber_all);
+}
+
+__naked struct arena_single global_ret_arena_single(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "exit;"
+ );
+}
+
+SEC("tc")
+__success __retval(0)
+__naked int aggregate_ret_global_arena_single(void)
+{
+ asm volatile (
+ "call %[global_ret_arena_single];"
+ "r0 = 0;"
+ "exit;"
+ :
+ : __imm(global_ret_arena_single)
+ : __clobber_all);
+}
+
+__naked union arena_upair global_ret_arena_union(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "r2 = 0;"
+ "exit;"
+ );
+}
+
+SEC("tc")
+__load_if_JITed()
+__success __retval(0)
+__naked int aggregate_ret_global_arena_union(void)
+{
+ asm volatile (
+ "call %[global_ret_arena_union];"
+ "r0 = 0;"
+ "exit;"
+ :
+ : __imm(global_ret_arena_union)
+ : __clobber_all);
+}
+
+#endif
+
char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/aggregate_ret_kfunc.c b/tools/testing/selftests/bpf/progs/aggregate_ret_kfunc.c
index d6b422ae9784b..e9c82df8efb26 100644
--- a/tools/testing/selftests/bpf/progs/aggregate_ret_kfunc.c
+++ b/tools/testing/selftests/bpf/progs/aggregate_ret_kfunc.c
@@ -18,6 +18,8 @@ void __kfunc_btf_root(void)
: "r"(&bpf_kfunc_call_test_i128),
"r"(&bpf_kfunc_call_test_ret_fastcall),
"r"(&bpf_kfunc_call_test_ret_ptr),
+ "r"(&bpf_kfunc_call_test_ret_nested),
+ "r"(&bpf_kfunc_call_test_ret_deep),
"r"(&bpf_kfunc_call_test_ret_ii),
"r"(&bpf_kfunc_call_test_ret_big));
}
@@ -71,7 +73,8 @@ __naked int aggregate_ret_kfunc_fastcall_fail(void)
SEC("tc")
__arch_x86_64 __arch_arm64
-__failure __msg("is not composed of scalars")
+__failure __msg("is not composed of scalars or arena pointers")
+__msg("member 'p' has type PTR")
__naked int aggregate_ret_kfunc_ptr_fail(void)
{
asm volatile (
@@ -84,6 +87,37 @@ __naked int aggregate_ret_kfunc_ptr_fail(void)
: __clobber_all);
}
+SEC("tc")
+__arch_x86_64 __arch_arm64
+__failure __msg("is not composed of scalars or arena pointers")
+__msg("member 'in.p' has type PTR")
+__naked int aggregate_ret_kfunc_nested_ptr_fail(void)
+{
+ asm volatile (
+ "r1 = 0;"
+ "call %[bpf_kfunc_call_test_ret_nested];"
+ "r0 = 0;"
+ "exit;"
+ :
+ : __imm(bpf_kfunc_call_test_ret_nested)
+ : __clobber_all);
+}
+
+SEC("tc")
+__arch_x86_64 __arch_arm64
+__failure __msg("max struct nesting depth exceeded")
+__naked int aggregate_ret_kfunc_too_deep_fail(void)
+{
+ asm volatile (
+ "r1 = 0;"
+ "call %[bpf_kfunc_call_test_ret_deep];"
+ "r0 = 0;"
+ "exit;"
+ :
+ : __imm(bpf_kfunc_call_test_ret_deep)
+ : __clobber_all);
+}
+
SEC("tc")
__arch_x86_64 __arch_arm64
__failure __msg("R2 !read_ok")
diff --git a/tools/testing/selftests/bpf/progs/aggregate_ret_kfunc_arena.c b/tools/testing/selftests/bpf/progs/aggregate_ret_kfunc_arena.c
new file mode 100644
index 0000000000000..94c35e1b547cd
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/aggregate_ret_kfunc_arena.c
@@ -0,0 +1,129 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf_arena_common.h>
+#include "bpf_misc.h"
+#include "../test_kmods/bpf_testmod_kfunc.h"
+
+#if defined(__clang_major__) && __clang_major__ >= 23
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARENA);
+ __uint(map_flags, BPF_F_MMAPABLE);
+ __uint(max_entries, 2);
+} arena SEC(".maps");
+
+/*
+ * A returned member carries the arena type tag but not the address space
+ * qualifier, so the program casts it into the arena address space itself
+ * rather than the compiler doing it.
+ */
+#define arena_ptr(p) ((u32 volatile __arena *)(p))
+
+SEC("syscall")
+__arch_x86_64 __arch_arm64
+__load_if_JITed()
+__success __retval(0)
+int aggregate_ret_kfunc_arena(void *ctx)
+{
+ u32 volatile __arena *page = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
+ u32 volatile __arena *a, *b;
+ struct prog_test_ret_arena r;
+
+ if (!page)
+ return 1;
+
+ /* Both halves come back in R0:R2, pointing at page and page + 4. */
+ r = bpf_kfunc_call_test_ret_arena((u64)page);
+ if (!r.a || !r.b)
+ return 2;
+
+ a = arena_ptr(r.a);
+ b = arena_ptr(r.b);
+ *a = 1;
+ *b = 2;
+ if (*a != 1)
+ return 3;
+ if (*b != 2)
+ return 4;
+
+ /* The halves are the first two slots of the page the program allocated. */
+ page[0] = 7;
+ if (*a != 7)
+ return 5;
+ page[1] = 9;
+ if (*b != 9)
+ return 6;
+
+ return 0;
+}
+
+SEC("syscall")
+__arch_x86_64 __arch_arm64
+__load_if_JITed()
+__success __retval(0)
+int aggregate_ret_kfunc_arena_mixed(void *ctx)
+{
+ u32 __arena *page = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
+ struct prog_test_ret_arena_mixed r;
+ u32 volatile __arena *p;
+
+ if (!page)
+ return 1;
+
+ /* An arena pointer in R0 beside a scalar in R2. */
+ r = bpf_kfunc_call_test_ret_arena_mixed((u64)page);
+ if (!r.p)
+ return 2;
+ if (r.tag != 0xbeef)
+ return 3;
+
+ p = arena_ptr(r.p);
+ *p = 3;
+ if (*p != 3)
+ return 4;
+
+ return 0;
+}
+
+SEC("syscall")
+__arch_x86_64 __arch_arm64
+__failure __msg("is not composed of scalars or arena pointers")
+__msg("member 'b' has type PTR")
+int aggregate_ret_kfunc_arena_untagged_fail(void *ctx)
+{
+ struct prog_test_ret_arena_untagged r;
+
+ r = bpf_kfunc_call_test_ret_arena_untagged(0);
+
+ return r.a == r.b;
+}
+
+SEC("syscall")
+__arch_x86_64 __arch_arm64
+__failure __msg("is not composed of scalars or arena pointers")
+__msg("member 'b' has type PTR")
+int aggregate_ret_kfunc_arena_union_fail(void *ctx)
+{
+ union prog_test_ret_arena_union r;
+
+ r = bpf_kfunc_call_test_ret_arena_union(0);
+
+ return r.a == r.b;
+}
+
+#else
+
+SEC("socket")
+__description("aggregate_ret_kfunc_arena: needs LLVM 23, dummy test")
+__skip("needs LLVM 23")
+__success
+int dummy_test(void)
+{
+ return 0;
+}
+
+#endif
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/exceptions_fail.c b/tools/testing/selftests/bpf/progs/exceptions_fail.c
index 9708efb93683b..cde4982e99896 100644
--- a/tools/testing/selftests/bpf/progs/exceptions_fail.c
+++ b/tools/testing/selftests/bpf/progs/exceptions_fail.c
@@ -60,7 +60,7 @@ __noinline int exception_cb_ok_arg_small(int a)
SEC("?tc")
__exception_cb(exception_cb_bad_ret_type1)
-__failure __msg("Only void, scalar, or a scalar-only struct/union up to 16 bytes is supported.")
+__failure __msg("Only void, a scalar, an arena pointer, or a struct/union of scalars")
int reject_exception_cb_type_1(struct __sk_buff *ctx)
{
bpf_throw(0);
diff --git a/tools/testing/selftests/bpf/progs/verifier_arena.c b/tools/testing/selftests/bpf/progs/verifier_arena.c
index 815f342eb4b05..672a3aadd9cb2 100644
--- a/tools/testing/selftests/bpf/progs/verifier_arena.c
+++ b/tools/testing/selftests/bpf/progs/verifier_arena.c
@@ -734,4 +734,52 @@ int check_arena_arg_ret(void *ctx)
return 0;
}
+#if defined(__clang_major__) && __clang_major__ >= 23
+
+struct arena_page_pair {
+ u32 __arena *first;
+ u32 __arena *second;
+};
+
+__weak struct arena_page_pair split_arena_page(u32 __arena *page)
+{
+ struct arena_page_pair pair;
+
+ pair.first = page;
+ pair.second = page + 1;
+
+ return pair;
+}
+
+SEC("syscall")
+__load_if_JITed()
+__success __retval(0)
+int check_arena_struct_ret(void *ctx)
+{
+ u32 __arena *page = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
+ u32 volatile __arena *first, *second;
+ struct arena_page_pair pair;
+
+ if (!page)
+ return 1;
+
+ pair = split_arena_page(page);
+ if (!pair.first || !pair.second)
+ return 2;
+
+ /* Both halves of the pair must still be usable as arena pointers. */
+ first = pair.first;
+ second = pair.second;
+ *first = 1;
+ *second = 2;
+ if (*first != 1)
+ return 3;
+ if (*second != 2)
+ return 4;
+
+ return 0;
+}
+
+#endif
+
char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
index 850cf4f830c48..0ef2ce875d71d 100644
--- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
+++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
@@ -981,6 +981,48 @@ __bpf_kfunc struct prog_test_ret_ptr bpf_kfunc_call_test_ret_ptr(u64 tag)
return r;
}
+__bpf_kfunc struct prog_test_ret_arena bpf_kfunc_call_test_ret_arena(u64 addr)
+{
+ struct prog_test_ret_arena r = { .a = (void *)addr, .b = (void *)(addr + 4) };
+
+ return r;
+}
+
+__bpf_kfunc struct prog_test_ret_arena_mixed bpf_kfunc_call_test_ret_arena_mixed(u64 addr)
+{
+ struct prog_test_ret_arena_mixed r = { .p = (void *)addr, .tag = 0xbeef };
+
+ return r;
+}
+
+__bpf_kfunc struct prog_test_ret_arena_untagged bpf_kfunc_call_test_ret_arena_untagged(u64 addr)
+{
+ struct prog_test_ret_arena_untagged r = { .a = (void *)addr, .b = NULL };
+
+ return r;
+}
+
+__bpf_kfunc union prog_test_ret_arena_union bpf_kfunc_call_test_ret_arena_union(u64 addr)
+{
+ union prog_test_ret_arena_union r = { .a = (void *)addr };
+
+ return r;
+}
+
+__bpf_kfunc struct prog_test_ret_nested bpf_kfunc_call_test_ret_nested(u64 tag)
+{
+ struct prog_test_ret_nested r = { .in = { .p = NULL }, .tag = tag };
+
+ return r;
+}
+
+__bpf_kfunc struct prog_test_ret_deep bpf_kfunc_call_test_ret_deep(u64 v)
+{
+ struct prog_test_ret_deep r = { .l1 = { .l2 = { .l3 = { .l4 = { .v = v } } } } };
+
+ return r;
+}
+
__bpf_kfunc struct prog_test_ret_ii bpf_kfunc_call_test_ret_ii(int a, int b)
{
struct prog_test_ret_ii r = { .a = a, .b = b };
@@ -1539,6 +1581,12 @@ BTF_ID_FLAGS(func, bpf_kfunc_call_test_i128)
BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_pair)
BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_fastcall, KF_FASTCALL)
BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_ptr)
+BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_arena)
+BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_arena_mixed)
+BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_arena_untagged)
+BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_arena_union)
+BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_nested)
+BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_deep)
BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_ii)
#endif
BTF_ID_FLAGS(func, bpf_kfunc_call_test_ret_big)
diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h b/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h
index 65e693ada7364..a2e9e9f3184e2 100644
--- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h
+++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h
@@ -26,6 +26,12 @@ struct prog_test_ref_kfunc {
};
#endif
+#if __has_attribute(btf_type_tag)
+#define __arena_tag __attribute__((btf_type_tag("arena")))
+#else
+#define __arena_tag
+#endif
+
struct bpf_iter_testmod_seq;
struct prog_test_pass1 {
@@ -70,6 +76,45 @@ struct prog_test_ret_ptr { /* 16 bytes: contains a pointer */
__u64 tag;
};
+struct prog_test_ret_arena { /* 16 bytes: two arena pointers */
+ void __arena_tag *a;
+ void __arena_tag *b;
+};
+
+struct prog_test_ret_arena_mixed { /* 16 bytes: an arena pointer and a scalar */
+ void __arena_tag *p;
+ __u64 tag;
+};
+
+struct prog_test_ret_arena_untagged { /* 16 bytes: 'b' lacks the arena tag */
+ void __arena_tag *a;
+ void *b;
+};
+
+union prog_test_ret_arena_union { /* 8 bytes: 'b' lacks the arena tag */
+ void __arena_tag *a;
+ void *b;
+};
+
+struct prog_test_ret_nested { /* 16 bytes: the pointer hides one level down */
+ struct {
+ void *p;
+ } in;
+ __u64 tag;
+};
+
+struct prog_test_ret_deep { /* 8 bytes, but nested past the 4-level walk limit */
+ struct {
+ struct {
+ struct {
+ struct {
+ __u64 v;
+ } l4;
+ } l3;
+ } l2;
+ } l1;
+};
+
struct prog_test_ret_big { /* 24 bytes: too large for R0:R2 */
__u64 a;
__u64 b;
@@ -159,6 +204,12 @@ struct prog_test_ret_pair bpf_kfunc_call_test_ret_pair(__u64 a, __u64 b) __ksym;
struct prog_test_ret_pair bpf_kfunc_call_test_ret_fastcall(__u64 a, __u64 b) __ksym;
struct prog_test_ret_ii bpf_kfunc_call_test_ret_ii(int a, int b) __ksym;
struct prog_test_ret_ptr bpf_kfunc_call_test_ret_ptr(__u64 tag) __ksym;
+struct prog_test_ret_nested bpf_kfunc_call_test_ret_nested(__u64 tag) __ksym;
+struct prog_test_ret_arena bpf_kfunc_call_test_ret_arena(__u64 addr) __ksym;
+struct prog_test_ret_arena_mixed bpf_kfunc_call_test_ret_arena_mixed(__u64 addr) __ksym;
+struct prog_test_ret_arena_untagged bpf_kfunc_call_test_ret_arena_untagged(__u64 addr) __ksym;
+union prog_test_ret_arena_union bpf_kfunc_call_test_ret_arena_union(__u64 addr) __ksym;
+struct prog_test_ret_deep bpf_kfunc_call_test_ret_deep(__u64 v) __ksym;
struct prog_test_ret_big bpf_kfunc_call_test_ret_big(void) __ksym;
__u64 bpf_kfunc_call_stack_arg(__u64 a, __u64 b, __u64 c, __u64 d,
__u64 e, __u64 f, __u64 g, __u64 h,
|
| 19/3 | 2026/08/27 06:45 | llm | kmsan-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 17932 output: 132 thoughts: 1197 Thoughts: **BPF Verifier Patch Analysis: KMSAN Considerations** Okay, let's break this down from a memory safety perspective. This patch fundamentally alters the BPF verifier to permit kfuncs and subprograms to return structs and unions by value, even if those structs contain "arena" pointers, which is a departure from previous restrictions. It seems the main changes revolve around `btf_member_kind`, `btf_struct_is_composed_of`, and `btf_type_is_arena_ptr`, and adjustments to `mark_kfunc_ret_regs`. My initial thought is to assess potential uninitialized memory risks. Essentially, the core logic is still about the *verifier* - it's a type checker, not a mechanism for memory allocation or data transfer itself. The verifier flags the return registers as unknown, and the kernel function supplies the data. These values reside within the BPF registers themselves. Thus, it's about checking return value types, not new memory exposure to user space directly from the verifier change. So, could uninitialized memory *from a kfunc* be exposed? Yes, potentially, if a kfunc returns uninitialized data. However, the existing KMSAN should already catch this when the BPF program attempts to read such values, or when the kfunc itself returns it. Crucially, the patch *doesn't* introduce new kfuncs. The added test cases are initialized correctly. Let's quickly check `mark_kfunc_ret_regs`. Ah, it just handles verifier state tracking! It marks those registers as containing "unknown" values, but nothing more, confirming that the values are being generated by the kfunc itself. This is critical: the verifier isn't creating or initializing memory here. The `prepare_func_exit` part is simply copying registers, nothing new. From my understanding, this is purely verifier logic. It's about static analysis, not memory management. The kfuncs are responsible for returning initialized data. Thus, if a kfunc *did* return uninitialized data, that's a bug *within the kfunc*, not a consequence of this patch, and existing KMSAN/KASAN would address this. It's confined to the static analysis phase of BPF programs. Reviewing KMSAN's critical scenarios – kernel structures passed to userspace where fields may be uninitialized, branching on uninitialized data, allocation of complex structures, or buffer access with incorrect bounds checks – none of these apply to the core verifier changes. The test cases themselves are properly initialized. Therefore, the patch doesn't warrant a dedicated KMSAN fuzzing session. The reasoning is clear: the changes are solely in the type-checking and register state-tracking logic of the verifier. No new memory allocations, no kernel-to-user data copies, nothing that introduces uninitialized memory risks beyond what's already covered by existing memory safety tools. **NeedsKMSAN = false.** |
| 20/3 | 2026/08/27 06:45 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch modifies the BPF verifier to allow kfuncs and subprograms to return structs/unions containing arena pointers by value. It updates the verifier's type checking logic and register state tracking. It does not introduce any new memory allocations, kernel-to-user data copies, or complex data structures that could expose uninitialized memory. The changes are confined to the static analysis phase of BPF programs. Standard KASAN and other bug detectors are sufficient to catch any potential memory safety issues in the verifier.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch modifies the BPF verifier to allow kfuncs and subprograms to return structs/unions containing arena pointers by value. It updates the verifier's type checking logic and register state tracking. It does not introduce any new memory allocations, kernel-to-user data copies, or complex data structures that could expose uninitialized memory. The changes are confined to the static analysis phase of BPF programs. Standard KASAN and other bug detectors are sufficient to catch any potential memory safety issues in the verifier.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|