| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/10/03 02:37 | flow | patch-triage |
5mArgs:null Results: {
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT"
],
"FocusSymbols": [
"bpf_do_misc_fixups",
"arena_scalar_access",
"mem_access_reg",
"is_arena_scalar",
"adjust_ptr_min_max_vals",
"bpf_jit_supports_arena_scalar",
"btf_datasec_resolve",
"btf_func_check",
"btf_name_valid_identifier"
],
"KMSANReasoning": "The patch series adds support for BPF_F_ARENA_SCALAR to allow BPF programs (particularly those compiled from Rust) to access BPF arena memory using scalar values, updates BTF parsing/validation for Rust identifiers and split variables, and enables arithmetic operations on pointers to degrade to scalar values when pointer leaks are permitted.\n\nFrom a memory sanitization perspective:\n1. KMSAN applicability: KMSAN detects uses of uninitialized memory (stack, heap, or page allocations) and leaks of uninitialized memory to user space.\n - The temporary stack structure `struct bpf_reg_state arena` introduced in `verifier.c` (`mem_access_reg()`) is explicitly zeroed with `memset(arena, 0, sizeof(*arena))`.\n - `env-\u003einsn_aux_data` is allocated with `__GFP_ZERO` / `vrealloc` with `__GFP_ZERO`.\n - Instruction patches generated in `arena_scalar_access()` fully initialize all instruction fields in `insn_buf`.\n - In `btf.c`, `btf_var_is_piece()` relies on `btf_resolve_size()`, where `size` is only evaluated when `btf_resolve_size()` succeeds; and `btf_datasec_show()` skips pieces to avoid reads beyond section bounds.\n - BPF arena memory pages are allocated zero-initialized.\n - No uninitialized data or struct padding is copied to user space.\n2. KASAN and general bug detectors: Any potential issues arising from this series would involve verifier state logic, bounds checking, or JIT instruction rewriting, which fall under the purview of standard functional testing, KASAN (for out-of-bounds memory accesses), and verifier invariants checks, rather than uninitialized memory tracking.\n\nConsequently, there is no risk of uninitialized memory access or info-leaks, and a dedicated KMSAN fuzzing session is not justified.",
"NeedsKMSAN": false,
"Reasoning": "The patch series adds BPF_F_ARENA_SCALAR support and relaxes BTF validation (to support Rust BPF programs). It introduces new pointer-to-scalar conversion rules in adjust_ptr_min_max_vals, allows scalar registers to access BPF arena memory in the verifier (mem_access_reg, check_load_mem, check_store_reg, check_atomic_rmw), patches arena scalar accesses in bpf_do_misc_fixups, maps BPF_REG_AX in x86 JIT, and modifies BTF validation logic (btf_datasec_resolve, btf_func_check, btf_name_valid_identifier). All changes affect core, security-critical BPF subsystem code directly reachable via the bpf() syscall.",
"WorthFuzzing": true
} |
| 1/1 | 2026/10/03 02:37 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit 204514513c3972ac15a879bd61fade9e9f92eecc\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Sat Oct 3 02:37:14 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c\nindex 475e706534543..6979c8ead0e8b 100644\n--- a/arch/arm64/net/bpf_jit_comp.c\n+++ b/arch/arm64/net/bpf_jit_comp.c\n@@ -3413,6 +3413,11 @@ bool bpf_jit_supports_arena(void)\n \treturn true;\n }\n \n+bool bpf_jit_supports_arena_scalar(void)\n+{\n+\treturn true;\n+}\n+\n bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)\n {\n \tif (!in_arena)\ndiff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c\nindex 6c7a0578760e7..8199d28e2a128 100644\n--- a/arch/x86/net/bpf_jit_comp.c\n+++ b/arch/x86/net/bpf_jit_comp.c\n@@ -236,6 +236,7 @@ static const int reg2pt_regs[] = {\n \t[BPF_REG_7] = offsetof(struct pt_regs, r13),\n \t[BPF_REG_8] = offsetof(struct pt_regs, r14),\n \t[BPF_REG_9] = offsetof(struct pt_regs, r15),\n+\t[BPF_REG_AX] = offsetof(struct pt_regs, r10),\n };\n \n /*\n@@ -4667,6 +4668,11 @@ bool bpf_jit_supports_arena(void)\n \treturn true;\n }\n \n+bool bpf_jit_supports_arena_scalar(void)\n+{\n+\treturn true;\n+}\n+\n bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)\n {\n \tif (!in_arena)\ndiff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h\nindex 811342e3c0419..0074f1356c584 100644\n--- a/include/linux/bpf_verifier.h\n+++ b/include/linux/bpf_verifier.h\n@@ -672,6 +672,7 @@ struct bpf_insn_aux_data {\n \tbool non_sleepable; /* helper/kfunc may be called from non-sleepable context */\n \tbool is_iter_next; /* bpf_iter_\u003ctype\u003e_next() kfunc call */\n \tbool call_with_percpu_alloc_ptr; /* {this,per}_cpu_ptr() with prog percpu alloc */\n+\tbool arena_scalar; /* ldx/stx/st/atomic through a number, it's an address in arena */\n \tu8 alu_state; /* used in combination with alu_limit */\n \t/* true if STX or LDX instruction is a part of a spill/fill\n \t * pattern for a bpf_fastcall call.\n@@ -943,6 +944,7 @@ struct bpf_verifier_env {\n \tbool strict_alignment;\t\t/* perform strict pointer alignment checks */\n \tbool test_state_freq;\t\t/* test verifier with different pruning frequency */\n \tbool test_reg_invariants;\t/* fail verification on register invariants violations */\n+\tbool arena_scalar;\t\t/* load and store through a number is arena access */\n \tstruct bpf_verifier_state *cur_state; /* current verifier state */\n \t/* Search pruning optimization, array of list_heads for\n \t * lists of struct bpf_verifier_state_list.\ndiff --git a/include/linux/filter.h b/include/linux/filter.h\nindex e42eccb0990e6..9339c6131f8ff 100644\n--- a/include/linux/filter.h\n+++ b/include/linux/filter.h\n@@ -1250,6 +1250,7 @@ bool bpf_jit_supports_far_kfunc_call(void);\n bool bpf_jit_supports_exceptions(void);\n bool bpf_jit_supports_ptr_xchg(void);\n bool bpf_jit_supports_arena(void);\n+bool bpf_jit_supports_arena_scalar(void);\n bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena);\n bool bpf_jit_supports_private_stack(void);\n bool bpf_jit_supports_large_stack(void);\ndiff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h\nindex 4687c33109968..e0ed44b1bbcb7 100644\n--- a/include/uapi/linux/bpf.h\n+++ b/include/uapi/linux/bpf.h\n@@ -1344,6 +1344,12 @@ enum bpf_perf_event_type {\n /* The verifier internal test flag. Behavior is undefined */\n #define BPF_F_TEST_REG_INVARIANTS\t(1U \u003c\u003c 7)\n \n+/*\n+ * Load and store through a number is an access to the arena of the program\n+ * at the low 32 bits of the number. It's for programs written in Rust.\n+ */\n+#define BPF_F_ARENA_SCALAR\t(1U \u003c\u003c 8)\n+\n /* link_create.kprobe_multi.flags used in LINK_CREATE command for\n * BPF_TRACE_KPROBE_MULTI attach type to create return probe.\n */\ndiff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c\nindex 8cc17a1cd25c6..0630675377aaa 100644\n--- a/kernel/bpf/btf.c\n+++ b/kernel/bpf/btf.c\n@@ -901,16 +901,6 @@ static bool btf_name_offset_valid(const struct btf *btf, u32 offset)\n \treturn offset \u003c btf-\u003ehdr.str_len;\n }\n \n-static bool __btf_name_char_ok(char c, bool first)\n-{\n-\tif ((first ? !isalpha(c) :\n-\t\t !isalnum(c)) \u0026\u0026\n-\t c != '_' \u0026\u0026\n-\t c != '.')\n-\t\treturn false;\n-\treturn true;\n-}\n-\n const char *btf_str_by_offset(const struct btf *btf, u32 offset)\n {\n \twhile (offset \u003c btf-\u003estart_str_off)\n@@ -923,20 +913,21 @@ const char *btf_str_by_offset(const struct btf *btf, u32 offset)\n \treturn NULL;\n }\n \n+/* Names in BTF of Rust are not C identifiers. Allow any printable character */\n static bool btf_name_valid_identifier(const struct btf *btf, u32 offset)\n {\n \t/* offset must be valid */\n \tconst char *src = btf_str_by_offset(btf, offset);\n \tconst char *src_limit;\n \n-\tif (!__btf_name_char_ok(*src, true))\n+\tif (!isprint(*src))\n \t\treturn false;\n \n \t/* set a limit on identifier length */\n \tsrc_limit = src + KSYM_NAME_LEN;\n \tsrc++;\n \twhile (*src \u0026\u0026 src \u003c src_limit) {\n-\t\tif (!__btf_name_char_ok(*src, false))\n+\t\tif (!isprint(*src))\n \t\t\treturn false;\n \t\tsrc++;\n \t}\n@@ -5406,7 +5397,7 @@ static int btf_datasec_resolve(struct btf_verifier_env *env,\n \n \tenv-\u003eresolve_mode = RESOLVE_TBD;\n \tfor_each_vsi_from(i, v-\u003enext_member, v-\u003et, vsi) {\n-\t\tu32 var_type_id = vsi-\u003etype, type_id, type_size = 0;\n+\t\tu32 var_type_id = vsi-\u003etype, type_id;\n \t\tconst struct btf_type *var_type = btf_type_by_id(env-\u003ebtf,\n \t\t\t\t\t\t\t\t var_type_id);\n \t\tif (!var_type || !btf_type_is_var(var_type)) {\n@@ -5421,16 +5412,16 @@ static int btf_datasec_resolve(struct btf_verifier_env *env,\n \t\t\treturn env_stack_push(env, var_type, var_type_id);\n \t\t}\n \n+\t\t/*\n+\t\t * The variable can be smaller than its type. It's a piece of\n+\t\t * a variable that the compiler split then, with the type of\n+\t\t * the whole variable.\n+\t\t */\n \t\ttype_id = var_type-\u003etype;\n-\t\tif (!btf_type_id_size(btf, \u0026type_id, \u0026type_size)) {\n+\t\tif (!btf_type_id_size(btf, \u0026type_id, NULL)) {\n \t\t\tbtf_verifier_log_vsi(env, v-\u003et, vsi, \"Invalid type\");\n \t\t\treturn -EINVAL;\n \t\t}\n-\n-\t\tif (vsi-\u003esize \u003c type_size) {\n-\t\t\tbtf_verifier_log_vsi(env, v-\u003et, vsi, \"Invalid size\");\n-\t\t\treturn -EINVAL;\n-\t\t}\n \t}\n \n \tenv_stack_pop_resolved(env, 0, 0);\n@@ -5443,6 +5434,16 @@ static void btf_datasec_log(struct btf_verifier_env *env,\n \tbtf_verifier_log(env, \"size=%u vlen=%u\", t-\u003esize, btf_type_vlen(t));\n }\n \n+/* A piece of a variable is smaller than its type, which is the one of the whole variable */\n+static bool btf_var_is_piece(const struct btf *btf, const struct btf_type *var,\n+\t\t\t const struct btf_var_secinfo *vsi)\n+{\n+\tu32 size;\n+\n+\treturn IS_ERR(btf_resolve_size(btf, btf_type_by_id(btf, var-\u003etype), \u0026size)) ||\n+\t vsi-\u003esize \u003c size;\n+}\n+\n static void btf_datasec_show(const struct btf *btf,\n \t\t\t const struct btf_type *t, u32 type_id,\n \t\t\t void *data, u8 bits_offset,\n@@ -5450,6 +5451,7 @@ static void btf_datasec_show(const struct btf *btf,\n {\n \tconst struct btf_var_secinfo *vsi;\n \tconst struct btf_type *var;\n+\tbool comma = false;\n \tu32 i;\n \n \tif (!btf_show_start_type(show, t, type_id, data))\n@@ -5459,8 +5461,12 @@ static void btf_datasec_show(const struct btf *btf,\n \t\t\t __btf_name_by_offset(btf, t-\u003ename_off));\n \tfor_each_vsi(i, t, vsi) {\n \t\tvar = btf_type_by_id(btf, vsi-\u003etype);\n-\t\tif (i)\n+\t\t/* there is less data than the type takes */\n+\t\tif (btf_var_is_piece(btf, var, vsi))\n+\t\t\tcontinue;\n+\t\tif (comma)\n \t\t\tbtf_show(show, \",\");\n+\t\tcomma = true;\n \t\tbtf_type_ops(var)-\u003eshow(btf, var, vsi-\u003etype,\n \t\t\t\t\tdata + vsi-\u003eoffset, bits_offset, show);\n \t}\n@@ -5761,6 +5767,10 @@ static int btf_func_check(struct btf_verifier_env *env,\n \t\treturn -EINVAL;\n \t}\n \n+\t/* Rust leaves out names of some arguments of static functions */\n+\tif (btf_func_linkage(t) == BTF_FUNC_STATIC)\n+\t\treturn 0;\n+\n \targs = (const struct btf_param *)(proto_type + 1);\n \tnr_args = btf_type_vlen(proto_type);\n \tfor (i = 0; i \u003c nr_args; i++) {\ndiff --git a/kernel/bpf/core.c b/kernel/bpf/core.c\nindex d3b8b626ec0f7..36900b02d6684 100644\n--- a/kernel/bpf/core.c\n+++ b/kernel/bpf/core.c\n@@ -18,6 +18,7 @@\n */\n \n #include \u003cuapi/linux/btf.h\u003e\n+#include \u003clinux/ctype.h\u003e\n #include \u003clinux/filter.h\u003e\n #include \u003clinux/sched/signal.h\u003e\n #include \u003clinux/skbuff.h\u003e\n@@ -589,6 +590,10 @@ bpf_prog_ksym_set_name(struct bpf_prog *prog)\n \t\t\t\t prog-\u003eaux-\u003efunc_info[prog-\u003eaux-\u003efunc_idx].type_id);\n \t\tfunc_name = btf_name_by_offset(prog-\u003eaux-\u003ebtf, type-\u003ename_off);\n \t\tsnprintf(sym, (size_t)(end - sym), \"_%s\", func_name);\n+\t\t/* the name of a function of Rust is not an identifier */\n+\t\tfor (; *sym; sym++)\n+\t\t\tif (!isalnum(*sym) \u0026\u0026 *sym != '_' \u0026\u0026 *sym != '.')\n+\t\t\t\t*sym = '_';\n \t\treturn;\n \t}\n \n@@ -3429,6 +3434,12 @@ bool __weak bpf_jit_supports_arena(void)\n \treturn false;\n }\n \n+/* Whether JIT takes BPF_REG_AX as the address of arena access */\n+bool __weak bpf_jit_supports_arena_scalar(void)\n+{\n+\treturn false;\n+}\n+\n bool __weak bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)\n {\n \treturn false;\ndiff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c\nindex 37cf130ebb57b..b4bfe5522003b 100644\n--- a/kernel/bpf/fixups.c\n+++ b/kernel/bpf/fixups.c\n@@ -46,6 +46,31 @@ static bool is_addr_space_cast32(struct bpf_prog *prog, const struct bpf_insn *i\n \treturn false;\n }\n \n+/*\n+ * The insn accesses arena through a number. JITs add the base of arena\n+ * to the register as it is, so the access goes through the low 32 bits of\n+ * the number in BPF_REG_AX. Registers of the program are not changed: the\n+ * number may be compared or stored later, and on another path the register\n+ * may be PTR_TO_ARENA.\n+ *\n+ * Constant blinding leaves insns that use BPF_REG_AX alone, so the immediate\n+ * of st through a number is not blinded.\n+ */\n+static int arena_scalar_access(const struct bpf_insn *insn, struct bpf_insn *buf)\n+{\n+\tbool load = BPF_CLASS(insn-\u003ecode) == BPF_LDX || bpf_atomic_is_load_acq(insn);\n+\tstruct bpf_insn *patch = buf;\n+\n+\t*patch++ = BPF_MOV32_REG(BPF_REG_AX, load ? insn-\u003esrc_reg : insn-\u003edst_reg);\n+\t*patch = *insn;\n+\tif (load)\n+\t\tpatch-\u003esrc_reg = BPF_REG_AX;\n+\telse\n+\t\tpatch-\u003edst_reg = BPF_REG_AX;\n+\tpatch++;\n+\treturn patch - buf;\n+}\n+\n /* Return the regno defined by the insn, or -1. */\n static int insn_def_regno(const struct bpf_insn *insn)\n {\n@@ -1785,6 +1810,20 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)\n \t\t\t/* Convert BPF_CLASS(insn-\u003ecode) == BPF_ALU64 to 32-bit ALU */\n \t\t\tinsn-\u003ecode = BPF_ALU | BPF_OP(insn-\u003ecode) | BPF_SRC(insn-\u003ecode);\n \n+\t\tif (env-\u003einsn_aux_data[i + delta].arena_scalar) {\n+\t\t\tcnt = arena_scalar_access(insn, insn_buf);\n+\n+\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\n+\t\t\tif (!new_prog)\n+\t\t\t\treturn -ENOMEM;\n+\n+\t\t\tdelta += cnt - 1;\n+\t\t\tprog = new_prog;\n+\t\t\tenv-\u003eprog = prog;\n+\t\t\tinsn = prog-\u003einsnsi + i + delta;\n+\t\t\tgoto next_insn;\n+\t\t}\n+\n \t\t/* Make sdiv/smod divide-by-minus-one exceptions impossible. */\n \t\tif ((insn-\u003ecode == (BPF_ALU64 | BPF_MOD | BPF_K) ||\n \t\t insn-\u003ecode == (BPF_ALU64 | BPF_DIV | BPF_K) ||\ndiff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c\nindex ac52f4ae414ce..778bca2abc042 100644\n--- a/kernel/bpf/syscall.c\n+++ b/kernel/bpf/syscall.c\n@@ -2948,9 +2948,13 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_at\n \t\t\t\t BPF_F_XDP_HAS_FRAGS |\n \t\t\t\t BPF_F_XDP_DEV_BOUND_ONLY |\n \t\t\t\t BPF_F_TEST_REG_INVARIANTS |\n+\t\t\t\t BPF_F_ARENA_SCALAR |\n \t\t\t\t BPF_F_TOKEN_FD))\n \t\treturn -EINVAL;\n \n+\tif ((attr-\u003eprog_flags \u0026 BPF_F_ARENA_SCALAR) \u0026\u0026 !bpf_jit_supports_arena_scalar())\n+\t\treturn -EOPNOTSUPP;\n+\n \tbpf_prog_load_fixup_attach_type(attr);\n \n \tif (attr-\u003eprog_flags \u0026 BPF_F_TOKEN_FD) {\ndiff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c\nindex b840b3eb9b229..2daee0130e5af 100644\n--- a/kernel/bpf/verifier.c\n+++ b/kernel/bpf/verifier.c\n@@ -5250,6 +5250,19 @@ static bool is_arena_reg(struct bpf_verifier_env *env, int regno)\n \treturn reg-\u003etype == PTR_TO_ARENA;\n }\n \n+/*\n+ * There are no address spaces in Rust, addresses of arena are plain numbers.\n+ * When the program is loaded with BPF_F_ARENA_SCALAR and has an arena a load or\n+ * a store through a number is an access to arena at the low 32 bits of it,\n+ * like the access through PTR_TO_ARENA is. The register stays a number.\n+ */\n+static bool is_arena_scalar(struct bpf_verifier_env *env, int regno)\n+{\n+\tconst struct bpf_reg_state *reg = reg_state(env, regno);\n+\n+\treturn reg-\u003etype == SCALAR_VALUE \u0026\u0026 env-\u003earena_scalar \u0026\u0026 env-\u003eprog-\u003eaux-\u003earena;\n+}\n+\n static bool is_load_acq_unsafe(struct bpf_verifier_env *env, int regno,\n \t\t\t struct bpf_insn *insn)\n {\n@@ -5280,7 +5293,7 @@ static bool atomic_ptr_type_ok(struct bpf_verifier_env *env, int regno,\n \t\treturn false;\n \tif (is_sk_reg(env, regno))\n \t\treturn false;\n-\tif (is_arena_reg(env, regno))\n+\tif (is_arena_reg(env, regno) || is_arena_scalar(env, regno))\n \t\treturn bpf_jit_supports_insn(insn, true);\n \tif (is_load_acq_unsafe(env, regno, insn))\n \t\treturn false;\n@@ -7161,6 +7174,22 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, struct b\n static int save_aux_ptr_type(struct bpf_verifier_env *env, enum bpf_reg_type type,\n \t\t\t bool allow_trust_mismatch);\n \n+/*\n+ * Returns the register to check the access of the current insn with.\n+ * For a number in a program with an arena that is 'arena'.\n+ */\n+static struct bpf_reg_state *mem_access_reg(struct bpf_verifier_env *env, int regno,\n+\t\t\t\t\t struct bpf_reg_state *arena)\n+{\n+\tif (!is_arena_scalar(env, regno))\n+\t\treturn cur_regs(env) + regno;\n+\n+\tmemset(arena, 0, sizeof(*arena));\n+\tarena-\u003etype = PTR_TO_ARENA;\n+\tenv-\u003einsn_aux_data[env-\u003einsn_idx].arena_scalar = true;\n+\treturn arena;\n+}\n+\n static int check_load_mem(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \t\t\t bool strict_alignment_once, bool is_ldsx,\n \t\t\t bool allow_trust_mismatch, const char *ctx)\n@@ -7168,6 +7197,7 @@ static int check_load_mem(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \tstruct bpf_verifier_state *vstate = env-\u003ecur_state;\n \tstruct bpf_func_state *state = vstate-\u003eframe[vstate-\u003ecurframe];\n \tstruct bpf_reg_state *regs = cur_regs(env);\n+\tstruct bpf_reg_state arena, *src_reg;\n \tenum bpf_reg_type src_reg_type;\n \tint err;\n \n@@ -7189,15 +7219,16 @@ static int check_load_mem(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \tif (err)\n \t\treturn err;\n \n-\tsrc_reg_type = regs[insn-\u003esrc_reg].type;\n+\tsrc_reg = mem_access_reg(env, insn-\u003esrc_reg, \u0026arena);\n+\tsrc_reg_type = src_reg-\u003etype;\n \n \t/*\n \t * check_stack_read_fixed_off() may refine the modification's origin to\n \t * the source stack slot.\n \t */\n \tbpf_diag_mod_begin(env, \u0026regs[insn-\u003edst_reg], NULL, BPF_DIAG_MOD_WRITE);\n-\terr = check_mem_access(env, env-\u003einsn_idx, regs + insn-\u003esrc_reg, argno_from_reg(insn-\u003esrc_reg), insn-\u003eoff,\n-\t\t\t BPF_SIZE(insn-\u003ecode), BPF_READ, insn-\u003edst_reg,\n+\terr = check_mem_access(env, env-\u003einsn_idx, src_reg, argno_from_reg(insn-\u003esrc_reg),\n+\t\t\t insn-\u003eoff, BPF_SIZE(insn-\u003ecode), BPF_READ, insn-\u003edst_reg,\n \t\t\t strict_alignment_once, is_ldsx);\n \terr = err ?: save_aux_ptr_type(env, src_reg_type,\n \t\t\t\t allow_trust_mismatch);\n@@ -7214,6 +7245,7 @@ static int check_store_reg(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \tstruct bpf_verifier_state *vstate = env-\u003ecur_state;\n \tstruct bpf_func_state *state = vstate-\u003eframe[vstate-\u003ecurframe];\n \tstruct bpf_reg_state *regs = cur_regs(env);\n+\tstruct bpf_reg_state arena, *dst_reg;\n \tenum bpf_reg_type dst_reg_type;\n \tint err;\n \n@@ -7235,11 +7267,12 @@ static int check_store_reg(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \tif (err)\n \t\treturn err;\n \n-\tdst_reg_type = regs[insn-\u003edst_reg].type;\n+\tdst_reg = mem_access_reg(env, insn-\u003edst_reg, \u0026arena);\n+\tdst_reg_type = dst_reg-\u003etype;\n \n \t/* Check if (dst_reg + off) is writeable. */\n-\terr = check_mem_access(env, env-\u003einsn_idx, regs + insn-\u003edst_reg, argno_from_reg(insn-\u003edst_reg), insn-\u003eoff,\n-\t\t\t BPF_SIZE(insn-\u003ecode), BPF_WRITE, insn-\u003esrc_reg,\n+\terr = check_mem_access(env, env-\u003einsn_idx, dst_reg, argno_from_reg(insn-\u003edst_reg),\n+\t\t\t insn-\u003eoff, BPF_SIZE(insn-\u003ecode), BPF_WRITE, insn-\u003esrc_reg,\n \t\t\t strict_alignment_once, false);\n \terr = err ?: save_aux_ptr_type(env, dst_reg_type, false);\n \n@@ -7249,7 +7282,7 @@ static int check_store_reg(struct bpf_verifier_env *env, struct bpf_insn *insn,\n static int check_atomic_rmw(struct bpf_verifier_env *env,\n \t\t\t struct bpf_insn *insn)\n {\n-\tstruct bpf_reg_state *dst_reg;\n+\tstruct bpf_reg_state arena, *dst_reg;\n \tint load_reg;\n \tint err;\n \n@@ -7294,6 +7327,9 @@ static int check_atomic_rmw(struct bpf_verifier_env *env,\n \t\treturn -EACCES;\n \t}\n \n+\t/* load_reg may be dst_reg. Look at dst_reg before it's marked as unknown. */\n+\tdst_reg = mem_access_reg(env, insn-\u003edst_reg, \u0026arena);\n+\n \tload_reg = bpf_atomic_load_reg(insn);\n \tif (load_reg \u003e= 0) {\n \t\t/* check and record load of old value */\n@@ -7302,8 +7338,6 @@ static int check_atomic_rmw(struct bpf_verifier_env *env,\n \t\t\treturn err;\n \t}\n \n-\tdst_reg = cur_regs(env) + insn-\u003edst_reg;\n-\n \t/* Check whether we can read the memory, with second call for fetch\n \t * case to simulate the register fill.\n \t */\n@@ -15561,8 +15595,14 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, struct bpf_insn\n \tu32 dst = insn-\u003edst_reg;\n \tconst char *reason;\n \tint ret, bounds_ret;\n+\tbool to_scalar;\n \n \tdst_reg = \u0026regs[dst];\n+\t/*\n+\t * Only += and -= keep a pointer. Any other op makes a number of it,\n+\t * which is fine when the program may see values of pointers anyway.\n+\t */\n+\tto_scalar = opcode != BPF_ADD \u0026\u0026 opcode != BPF_SUB \u0026\u0026 env-\u003eallow_ptr_leaks;\n \n \tif ((known \u0026\u0026 (smin_val != smax_val || umin_val != umax_val)) ||\n \t smin_val \u003e smax_val || umin_val \u003e umax_val) {\n@@ -15573,7 +15613,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, struct bpf_insn\n \t\treturn 0;\n \t}\n \n-\tif (BPF_CLASS(insn-\u003ecode) != BPF_ALU64) {\n+\tif (BPF_CLASS(insn-\u003ecode) != BPF_ALU64 \u0026\u0026 !to_scalar) {\n \t\t/* 32-bit ALU ops on pointers produce (meaningless) scalars */\n \t\tif (opcode == BPF_SUB \u0026\u0026 env-\u003eallow_ptr_leaks) {\n \t\t\t__mark_reg_unknown(env, dst_reg);\n@@ -15639,6 +15679,11 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, struct bpf_insn\n \t\treturn -EACCES;\n \t}\n \n+\tif (to_scalar) {\n+\t\t__mark_reg_unknown(env, dst_reg);\n+\t\treturn 0;\n+\t}\n+\n \t/* For 'scalar += pointer', dst_reg inherits the complete pointer\n \t * register state. Individual fields may be adjusted later by pointer\n \t * arithmetic. Callers guarantee that below does not overwrite off_reg.\n@@ -19340,15 +19385,17 @@ static int do_check_insn(struct bpf_verifier_env *env, bool *do_print_state)\n \t\t\treturn check_stack_arg_write(env, state, insn-\u003eoff, NULL);\n \t\t}\n \n+\t\tstruct bpf_reg_state arena, *dst_reg;\n \t\tenum bpf_reg_type dst_reg_type;\n \n \t\terr = check_reg_arg(env, insn-\u003edst_reg, SRC_OP);\n \t\tif (err)\n \t\t\treturn err;\n \n-\t\tdst_reg_type = cur_regs(env)[insn-\u003edst_reg].type;\n+\t\tdst_reg = mem_access_reg(env, insn-\u003edst_reg, \u0026arena);\n+\t\tdst_reg_type = dst_reg-\u003etype;\n \n-\t\terr = check_mem_access(env, env-\u003einsn_idx, cur_regs(env) + insn-\u003edst_reg, argno_from_reg(insn-\u003edst_reg),\n+\t\terr = check_mem_access(env, env-\u003einsn_idx, dst_reg, argno_from_reg(insn-\u003edst_reg),\n \t\t\t\t insn-\u003eoff, BPF_SIZE(insn-\u003ecode),\n \t\t\t\t BPF_WRITE, -1, false, false);\n \t\tif (err)\n@@ -22507,6 +22554,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,\n \tif (is_priv)\n \t\tenv-\u003etest_state_freq = attr-\u003eprog_flags \u0026 BPF_F_TEST_STATE_FREQ;\n \tenv-\u003etest_reg_invariants = attr-\u003eprog_flags \u0026 BPF_F_TEST_REG_INVARIANTS;\n+\tenv-\u003earena_scalar = attr-\u003eprog_flags \u0026 BPF_F_ARENA_SCALAR;\n \n \tenv-\u003eexplored_states = kvzalloc_objs(struct list_head,\n \t\t\t\t\t state_htab_size(env),\ndiff --git a/tools/bpf/bpftool/btf_dumper.c b/tools/bpf/bpftool/btf_dumper.c\nindex e4075824343fd..3267163119dc2 100644\n--- a/tools/bpf/bpftool/btf_dumper.c\n+++ b/tools/bpf/bpftool/btf_dumper.c\n@@ -524,6 +524,15 @@ static int btf_dumper_var(const struct btf_dumper *d, __u32 type_id,\n \treturn ret;\n }\n \n+/*\n+ * The compiler splits a variable into pieces. Every piece is a VAR with the\n+ * type of the whole variable, so there is less data than the type takes.\n+ */\n+bool btf_var_is_piece(const struct btf *btf, const struct btf_var_secinfo *vsi)\n+{\n+\treturn btf__resolve_size(btf, vsi-\u003etype) \u003e vsi-\u003esize;\n+}\n+\n static int btf_dumper_datasec(const struct btf_dumper *d, __u32 type_id,\n \t\t\t const void *data)\n {\n@@ -542,6 +551,8 @@ static int btf_dumper_datasec(const struct btf_dumper *d, __u32 type_id,\n \tjsonw_name(d-\u003ejw, btf__name_by_offset(d-\u003ebtf, t-\u003ename_off));\n \tjsonw_start_array(d-\u003ejw);\n \tfor (i = 0; i \u003c vlen; i++) {\n+\t\tif (btf_var_is_piece(d-\u003ebtf, \u0026vsi[i]))\n+\t\t\tcontinue;\n \t\tret = btf_dumper_do_type(d, vsi[i].type, 0, data + vsi[i].offset);\n \t\tif (ret)\n \t\t\tbreak;\ndiff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h\nindex fa2c877a3cbfc..6851346a60305 100644\n--- a/tools/bpf/bpftool/main.h\n+++ b/tools/bpf/bpftool/main.h\n@@ -121,6 +121,7 @@ struct obj_refs {\n };\n \n struct btf;\n+struct btf_var_secinfo;\n struct bpf_line_info;\n \n int build_pinned_obj_table(struct hashmap *table,\n@@ -239,6 +240,7 @@ int btf_dumper_type(const struct btf_dumper *d, __u32 type_id,\n \t\t const void *data);\n void btf_dumper_type_only(const struct btf *btf, __u32 func_type_id,\n \t\t\t char *func_only, int size);\n+bool btf_var_is_piece(const struct btf *btf, const struct btf_var_secinfo *vsi);\n \n void btf_dump_linfo_plain(const struct btf *btf,\n \t\t\t const struct bpf_line_info *linfo,\ndiff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c\nindex a60414aaa4940..eb077236d2eaa 100644\n--- a/tools/bpf/bpftool/prog.c\n+++ b/tools/bpf/bpftool/prog.c\n@@ -348,7 +348,7 @@ static void show_prog_metadata(int fd, __u32 num_maps)\n \t\t\tt_var = btf__type_by_id(btf, vsi-\u003etype);\n \t\t\tname = btf__name_by_offset(btf, t_var-\u003ename_off);\n \n-\t\t\tif (!has_metadata_prefix(name))\n+\t\t\tif (!has_metadata_prefix(name) || btf_var_is_piece(btf, vsi))\n \t\t\t\tcontinue;\n \n \t\t\tif (!printed_header) {\n@@ -377,7 +377,7 @@ static void show_prog_metadata(int fd, __u32 num_maps)\n \t\t\tt_var = btf__type_by_id(btf, vsi-\u003etype);\n \t\t\tname = btf__name_by_offset(btf, t_var-\u003ename_off);\n \n-\t\t\tif (!has_metadata_prefix(name))\n+\t\t\tif (!has_metadata_prefix(name) || btf_var_is_piece(btf, vsi))\n \t\t\t\tcontinue;\n \n \t\t\tif (!printed_header) {\ndiff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h\nindex 4687c33109968..e0ed44b1bbcb7 100644\n--- a/tools/include/uapi/linux/bpf.h\n+++ b/tools/include/uapi/linux/bpf.h\n@@ -1344,6 +1344,12 @@ enum bpf_perf_event_type {\n /* The verifier internal test flag. Behavior is undefined */\n #define BPF_F_TEST_REG_INVARIANTS\t(1U \u003c\u003c 7)\n \n+/*\n+ * Load and store through a number is an access to the arena of the program\n+ * at the low 32 bits of the number. It's for programs written in Rust.\n+ */\n+#define BPF_F_ARENA_SCALAR\t(1U \u003c\u003c 8)\n+\n /* link_create.kprobe_multi.flags used in LINK_CREATE command for\n * BPF_TRACE_KPROBE_MULTI attach type to create return probe.\n */\ndiff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h\nindex 9d160b5b9c0ee..b37b727ef216f 100644\n--- a/tools/lib/bpf/bpf_helpers.h\n+++ b/tools/lib/bpf/bpf_helpers.h\n@@ -248,13 +248,21 @@ enum libbpf_tristate {\n #define ___bpf_fill(arr, args...) \\\n \t___bpf_apply(___bpf_fill, ___bpf_narg(args))(arr, 0, args)\n \n+/*\n+ * Format strings are in a section of their own. When global data of the object\n+ * is in arena (the object has .arena.data section) libbpf keeps .rodata.str*\n+ * sections in read-only maps, which is where helpers and kfuncs take strings\n+ * from.\n+ */\n+#define ___bpf_fmt_sec __attribute__((section(\".rodata.str\")))\n+\n /*\n * BPF_SEQ_PRINTF to wrap bpf_seq_printf to-be-printed values\n * in a structure.\n */\n #define BPF_SEQ_PRINTF(seq, fmt, args...)\t\t\t\\\n ({\t\t\t\t\t\t\t\t\\\n-\tstatic const char ___fmt[] = fmt;\t\t\t\\\n+\tstatic const char ___fmt[] ___bpf_fmt_sec = fmt;\t\\\n \tunsigned long long ___param[___bpf_narg(args)];\t\t\\\n \t\t\t\t\t\t\t\t\\\n \t_Pragma(\"GCC diagnostic push\")\t\t\t\t\\\n@@ -272,7 +280,7 @@ enum libbpf_tristate {\n */\n #define BPF_SNPRINTF(out, out_size, fmt, args...)\t\t\\\n ({\t\t\t\t\t\t\t\t\\\n-\tstatic const char ___fmt[] = fmt;\t\t\t\\\n+\tstatic const char ___fmt[] ___bpf_fmt_sec = fmt;\t\\\n \tunsigned long long ___param[___bpf_narg(args)];\t\t\\\n \t\t\t\t\t\t\t\t\\\n \t_Pragma(\"GCC diagnostic push\")\t\t\t\t\\\n@@ -287,7 +295,7 @@ enum libbpf_tristate {\n #ifdef BPF_NO_GLOBAL_DATA\n #define BPF_PRINTK_FMT_MOD\n #else\n-#define BPF_PRINTK_FMT_MOD static const\n+#define BPF_PRINTK_FMT_MOD static const ___bpf_fmt_sec\n #endif\n \n #define __bpf_printk(fmt, ...)\t\t\t\t\\\n@@ -303,7 +311,7 @@ enum libbpf_tristate {\n */\n #define __bpf_vprintk(fmt, args...)\t\t\t\t\\\n ({\t\t\t\t\t\t\t\t\\\n-\tstatic const char ___fmt[] = fmt;\t\t\t\\\n+\tstatic const char ___fmt[] ___bpf_fmt_sec = fmt;\t\\\n \tunsigned long long ___param[___bpf_narg(args)];\t\t\\\n \t\t\t\t\t\t\t\t\\\n \t_Pragma(\"GCC diagnostic push\")\t\t\t\t\\\n@@ -317,7 +325,7 @@ enum libbpf_tristate {\n \n #define bpf_stream_printk(stream_id, fmt, args...)\t\t\t\t\t\\\n ({\t\t\t\t\t\t\t\t\t\t\t\\\n-\tstatic const char ___fmt[] = fmt;\t\t\t\t\t\t\\\n+\tstatic const char ___fmt[] ___bpf_fmt_sec = fmt;\t\t\t\t\\\n \tunsigned long long ___param[___bpf_narg(args)];\t\t\t\t\t\\\n \t\t\t\t\t\t\t\t\t\t\t\\\n \t_Pragma(\"GCC diagnostic push\")\t\t\t\t\t\t\t\\\ndiff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c\nindex fdd69aac39bde..cb09ded907732 100644\n--- a/tools/lib/bpf/libbpf.c\n+++ b/tools/lib/bpf/libbpf.c\n@@ -552,6 +552,26 @@ struct bpf_struct_ops {\n #define STRUCT_OPS_SEC \".struct_ops\"\n #define STRUCT_OPS_LINK_SEC \".struct_ops.link\"\n #define ARENA_SEC \".addr_space.1\"\n+/*\n+ * An object with this section keeps its global data in arena instead of\n+ * array maps. What is in the section doesn't matter. .data, .bss and .rodata\n+ * sections become a part of the arena map of the object, like __arena global\n+ * variables are. If the object doesn't declare an arena map libbpf creates\n+ * one that is just large enough for the data. bpf_object__find_map_by_name()\n+ * finds it by the name \"arena\" and bpf_map__set_max_entries() changes its\n+ * size before the object is loaded.\n+ * Read-only sections with pointers to functions and sections with constant\n+ * strings stay frozen array maps. A constant that a helper or a kfunc takes\n+ * by pointer has to be in such section.\n+ * Pointers to data that are stored in data become addresses of arena.\n+ * The load fails if such pointer points to a section that is not in arena,\n+ * or if the arena is pinned or reused.\n+ *\n+ * It's for programs written in Rust. There are no address spaces in Rust,\n+ * the program accesses arena through plain numbers, so programs of the object\n+ * are loaded with BPF_F_ARENA_SCALAR.\n+ */\n+#define ARENA_DATA_SEC \".arena.data\"\n \n enum libbpf_map_type {\n \tLIBBPF_MAP_UNSPEC,\n@@ -607,6 +627,13 @@ struct bpf_map {\n \t/* pointers to functions in the data of an internal map, see obj-\u003efunc_ptrs */\n \tstruct func_ptr *func_ptrs;\n \tsize_t func_ptr_cnt;\n+\t/*\n+\t * Data of the internal map is a part of arena data at arena_off.\n+\t * The map is not created, unless it's a copy that is in arena.\n+\t */\n+\tbool in_arena;\n+\tbool arena_copy;\n+\tsize_t arena_off;\n };\n \n enum extern_type {\n@@ -775,6 +802,17 @@ struct bpf_object {\n \tvoid *arena_data;\n \tsize_t arena_data_sz;\n \tsize_t arena_data_off;\n+\tbool data_in_arena;\n+\tbool arena_mapped;\n+\t/* pointers to data in the data of internal maps, when data is in arena */\n+\tstruct data_ptr {\n+\t\tint sec_idx;\n+\t\tsize_t sec_off;\n+\t\tint targ_sec_idx;\n+\t\tsize_t targ_off;\n+\t\tchar *sym_name;\n+\t} *data_ptrs;\n+\tsize_t data_ptr_cnt;\n \n \tvoid *jumptables_data;\n \tsize_t jumptables_data_sz;\n@@ -1600,6 +1638,7 @@ static struct bpf_object *bpf_object__new(const char *path,\n \tobj-\u003eefile.obj_buf = obj_buf;\n \tobj-\u003eefile.obj_buf_sz = obj_buf_sz;\n \tobj-\u003eefile.btf_maps_shndx = -1;\n+\tobj-\u003eefile.arena_data_shndx = -1;\n \tobj-\u003ekconfig_map_idx = -1;\n \tobj-\u003earena_map_idx = -1;\n \n@@ -3081,6 +3120,136 @@ static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map,\n \treturn 0;\n }\n \n+static bool map_is_const_str(const struct bpf_map *map)\n+{\n+\treturn map-\u003elibbpf_type == LIBBPF_MAP_RODATA \u0026\u0026\n+\t str_has_pfx(map-\u003ereal_name, RODATA_SEC \".str\");\n+}\n+\n+static bool map_data_goes_to_arena(const struct bpf_object *obj, const struct bpf_map *map)\n+{\n+\tint i;\n+\n+\tswitch (map-\u003elibbpf_type) {\n+\tcase LIBBPF_MAP_DATA:\n+\tcase LIBBPF_MAP_BSS:\n+\t\treturn true;\n+\tcase LIBBPF_MAP_RODATA:\n+\t\tbreak;\n+\tdefault:\n+\t\treturn false;\n+\t}\n+\n+\t/*\n+\t * Const strings that kfuncs and helpers take have to be in a frozen\n+\t * map. Pointers to them that are stored in data point to a copy of\n+\t * the strings that is in arena.\n+\t */\n+\tif (map_is_const_str(map))\n+\t\treturn true;\n+\t/*\n+\t * Read-only data with pointers stays a frozen map: the kernel\n+\t * recognizes pointers to functions in it.\n+\t */\n+\tif (str_has_pfx(map-\u003ereal_name, DATA_REL_RO_SEC))\n+\t\treturn false;\n+\tfor (i = 0; i \u003c obj-\u003eefile.sec_cnt; i++) {\n+\t\tconst struct elf_sec_desc *sec = \u0026obj-\u003eefile.secs[i];\n+\n+\t\tif (sec-\u003esec_type == SEC_RELO \u0026\u0026 sec-\u003eshdr-\u003esh_info == map-\u003esec_idx)\n+\t\t\treturn false;\n+\t}\n+\treturn true;\n+}\n+\n+/* Returns the offset of the data of the map in arena data that is sz bytes so far */\n+static size_t map_arena_off(const struct bpf_object *obj, const struct bpf_map *map, size_t sz)\n+{\n+\tsize_t align = obj-\u003eefile.secs[map-\u003esec_idx].shdr-\u003esh_addralign;\n+\n+\treturn roundup(sz, max(align, sizeof(__u64)));\n+}\n+\n+/*\n+ * Make .data, .bss and .rodata a part of arena data. They follow __arena\n+ * variables of the object, if there are any.\n+ */\n+static int bpf_object__init_arena_data(struct bpf_object *obj)\n+{\n+\tconst size_t page_sz = sysconf(_SC_PAGE_SIZE);\n+\tsize_t sz = obj-\u003earena_data_sz;\n+\tstruct bpf_map *map;\n+\tvoid *data;\n+\tint i;\n+\n+\tif (!obj-\u003edata_in_arena)\n+\t\treturn 0;\n+\n+\tfor (i = 0; i \u003c obj-\u003enr_maps; i++) {\n+\t\tmap = \u0026obj-\u003emaps[i];\n+\t\tif (map_data_goes_to_arena(obj, map))\n+\t\t\tsz = map_arena_off(obj, map, sz) + map-\u003edef.value_size;\n+\t}\n+\tif (sz == obj-\u003earena_data_sz)\n+\t\treturn 0;\n+\n+\tif (obj-\u003earena_map_idx \u003c 0) {\n+\t\t/* bpf_object__init_user_btf_maps() didn't look: there is no .maps section */\n+\t\tif (obj-\u003eefile.arena_data) {\n+\t\t\tpr_warn(\"elf: sec '%s': to use global __arena variables the ARENA map should be explicitly declared in SEC(\\\".maps\\\")\\n\",\n+\t\t\t\tARENA_SEC);\n+\t\t\treturn -ENOENT;\n+\t\t}\n+\n+\t\tmap = bpf_object__add_map(obj);\n+\t\tif (IS_ERR(map))\n+\t\t\treturn PTR_ERR(map);\n+\n+\t\tmap-\u003ereal_name = strdup(\"arena\");\n+\t\tmap-\u003ename = strdup(\"arena\");\n+\t\tif (!map-\u003ereal_name || !map-\u003ename) {\n+\t\t\tzfree(\u0026map-\u003ereal_name);\n+\t\t\tzfree(\u0026map-\u003ename);\n+\t\t\treturn -ENOMEM;\n+\t\t}\n+\t\tmap-\u003esec_idx = -1;\n+\t\tmap-\u003edef.type = BPF_MAP_TYPE_ARENA;\n+\t\tmap-\u003edef.max_entries = roundup(sz, page_sz) / page_sz;\n+\t\tmap-\u003edef.map_flags = BPF_F_MMAPABLE;\n+\t\tobj-\u003earena_map_idx = map - obj-\u003emaps;\n+\t}\n+\n+\tdata = realloc(obj-\u003earena_data, sz);\n+\tif (!data)\n+\t\treturn -ENOMEM;\n+\tmemset(data + obj-\u003earena_data_sz, 0, sz - obj-\u003earena_data_sz);\n+\tsz = obj-\u003earena_data_sz;\n+\tobj-\u003earena_data = data;\n+\tobj-\u003emaps[obj-\u003earena_map_idx].mmaped = data;\n+\n+\tfor (i = 0; i \u003c obj-\u003enr_maps; i++) {\n+\t\tmap = \u0026obj-\u003emaps[i];\n+\t\tif (!map_data_goes_to_arena(obj, map))\n+\t\t\tcontinue;\n+\t\tsz = map_arena_off(obj, map, sz);\n+\t\tmap-\u003earena_off = sz;\n+\t\tmemcpy(data + sz, map-\u003emmaped, map-\u003edef.value_size);\n+\t\tsz += map-\u003edef.value_size;\n+\t\tpr_debug(\"map '%s': data is in arena at offset %zu\\n\", map-\u003ename, map-\u003earena_off);\n+\t\tif (map_is_const_str(map)) {\n+\t\t\tmap-\u003earena_copy = true;\n+\t\t\tcontinue;\n+\t\t}\n+\t\tmunmap(map-\u003emmaped, bpf_map_mmap_sz(map));\n+\t\t/* make bpf_map__initial_value() and bpf_map__set_initial_value() work */\n+\t\tmap-\u003emmaped = data + map-\u003earena_off;\n+\t\tmap-\u003eautocreate = false;\n+\t\tmap-\u003ein_arena = true;\n+\t}\n+\tobj-\u003earena_data_sz = sz;\n+\treturn 0;\n+}\n+\n static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,\n \t\t\t\t\t const char *pin_root_path)\n {\n@@ -3173,6 +3342,7 @@ static int bpf_object__init_maps(struct bpf_object *obj,\n \terr = err ?: bpf_object__init_global_data_maps(obj);\n \terr = err ?: bpf_object__init_kconfig_map(obj);\n \terr = err ?: bpf_object_init_struct_ops(obj);\n+\terr = err ?: bpf_object__init_arena_data(obj);\n \n \treturn err;\n }\n@@ -3987,6 +4157,11 @@ static int bpf_object__elf_collect(struct bpf_object *obj)\n \t\tif (!sh)\n \t\t\treturn -LIBBPF_ERRNO__FORMAT;\n \n+\t\t/* the second pass has to know it when it sees relocations of data */\n+\t\tname = elf_sec_str(obj, sh-\u003esh_name);\n+\t\tif (name \u0026\u0026 strcmp(name, ARENA_DATA_SEC) == 0)\n+\t\t\tobj-\u003edata_in_arena = true;\n+\n \t\tif (sh-\u003esh_type == SHT_SYMTAB) {\n \t\t\tif (obj-\u003eefile.symbols) {\n \t\t\t\tpr_warn(\"elf: multiple symbol tables in %s\\n\", obj-\u003epath);\n@@ -4103,6 +4278,8 @@ static int bpf_object__elf_collect(struct bpf_object *obj)\n \t\t\t} else if (strcmp(name, ARENA_SEC) == 0) {\n \t\t\t\tobj-\u003eefile.arena_data = data;\n \t\t\t\tobj-\u003eefile.arena_data_shndx = idx;\n+\t\t\t} else if (strcmp(name, ARENA_DATA_SEC) == 0) {\n+\t\t\t\t/* the marker, see the first pass */\n \t\t\t} else if (strcmp(name, JUMPTABLES_SEC) == 0) {\n \t\t\t\tobj-\u003ejumptables_data = malloc(data-\u003ed_size);\n \t\t\t\tif (!obj-\u003ejumptables_data)\n@@ -4127,6 +4304,9 @@ static int bpf_object__elf_collect(struct bpf_object *obj)\n \t\t\t * have pointers to functions.\n \t\t\t */\n \t\t\tif (!section_have_execinstr(obj, targ_sec_idx) \u0026\u0026\n+\t\t\t !(obj-\u003edata_in_arena \u0026\u0026\n+\t\t\t (!strcmp(name, \".rel\" DATA_SEC) ||\n+\t\t\t str_has_pfx(name, \".rel\" DATA_SEC \".\"))) \u0026\u0026\n \t\t\t strcmp(name, \".rel\" RODATA_SEC) \u0026\u0026\n \t\t\t !str_has_pfx(name, \".rel\" RODATA_SEC \".\") \u0026\u0026\n \t\t\t strcmp(name, \".rel\" DATA_REL_RO_SEC) \u0026\u0026\n@@ -4878,6 +5058,10 @@ static int bpf_program__record_reloc(struct bpf_program *prog,\n \treloc_desc-\u003einsn_idx = insn_idx;\n \treloc_desc-\u003emap_idx = map_idx;\n \treloc_desc-\u003esym_off = sym-\u003est_value;\n+\tif (map-\u003ein_arena) {\n+\t\treloc_desc-\u003emap_idx = obj-\u003earena_map_idx;\n+\t\treloc_desc-\u003esym_off += map-\u003earena_off;\n+\t}\n \treturn 0;\n }\n \n@@ -5096,6 +5280,9 @@ int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate)\n \tif (map_is_created(map))\n \t\treturn libbpf_err(-EBUSY);\n \n+\tif (map-\u003ein_arena)\n+\t\treturn libbpf_err(-EOPNOTSUPP);\n+\n \tmap-\u003eautocreate = autocreate;\n \treturn 0;\n }\n@@ -5376,6 +5563,45 @@ bpf_object__reuse_map(struct bpf_map *map)\n \treturn 0;\n }\n \n+static struct bpf_map *bpf_object__sec_map(struct bpf_object *obj, int sec_idx)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i \u003c obj-\u003enr_maps; i++)\n+\t\tif (bpf_map__is_internal(\u0026obj-\u003emaps[i]) \u0026\u0026 obj-\u003emaps[i].sec_idx == sec_idx)\n+\t\t\treturn \u0026obj-\u003emaps[i];\n+\treturn NULL;\n+}\n+\n+static int bpf_object__mmap_arena(struct bpf_object *obj, struct bpf_map *map)\n+{\n+\tint i, err;\n+\n+\tmap-\u003emmaped = mmap((void *)(long)map-\u003emap_extra,\n+\t\t\t bpf_map_mmap_sz(map), PROT_READ | PROT_WRITE,\n+\t\t\t map-\u003emap_extra ? MAP_SHARED | MAP_FIXED : MAP_SHARED,\n+\t\t\t map-\u003efd, 0);\n+\tif (map-\u003emmaped == MAP_FAILED) {\n+\t\terr = -errno;\n+\t\tmap-\u003emmaped = NULL;\n+\t\tpr_warn(\"map '%s': failed to mmap arena: %s\\n\",\n+\t\t\tmap-\u003ename, errstr(err));\n+\t\treturn err;\n+\t}\n+\tif (obj-\u003earena_data) {\n+\t\tmemcpy(map-\u003emmaped + obj-\u003earena_data_off, obj-\u003earena_data,\n+\t\t\tobj-\u003earena_data_sz);\n+\t\tzfree(\u0026obj-\u003earena_data);\n+\t}\n+\tfor (i = 0; i \u003c obj-\u003enr_maps; i++) {\n+\t\tstruct bpf_map *m = \u0026obj-\u003emaps[i];\n+\n+\t\tif (m-\u003ein_arena)\n+\t\t\tm-\u003emmaped = map-\u003emmaped + obj-\u003earena_data_off + m-\u003earena_off;\n+\t}\n+\treturn 0;\n+}\n+\n static int\n bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)\n {\n@@ -5736,6 +5962,10 @@ bpf_object__create_maps(struct bpf_object *obj)\n \t\t\tcontinue;\n \t\t}\n \n+\t\t/* see bpf_object__relocate_data_ptrs() */\n+\t\tif (map-\u003edef.type == BPF_MAP_TYPE_ARENA \u0026\u0026 obj-\u003earena_mapped)\n+\t\t\tcontinue;\n+\n \t\terr = map_set_def_max_entries(map);\n \t\tif (err)\n \t\t\tgoto err_out;\n@@ -5773,22 +6003,9 @@ bpf_object__create_maps(struct bpf_object *obj)\n \t\t\t\tif (err \u003c 0)\n \t\t\t\t\tgoto err_out;\n \t\t\t} else if (map-\u003edef.type == BPF_MAP_TYPE_ARENA) {\n-\t\t\t\tmap-\u003emmaped = mmap((void *)(long)map-\u003emap_extra,\n-\t\t\t\t\t\t bpf_map_mmap_sz(map), PROT_READ | PROT_WRITE,\n-\t\t\t\t\t\t map-\u003emap_extra ? MAP_SHARED | MAP_FIXED : MAP_SHARED,\n-\t\t\t\t\t\t map-\u003efd, 0);\n-\t\t\t\tif (map-\u003emmaped == MAP_FAILED) {\n-\t\t\t\t\terr = -errno;\n-\t\t\t\t\tmap-\u003emmaped = NULL;\n-\t\t\t\t\tpr_warn(\"map '%s': failed to mmap arena: %s\\n\",\n-\t\t\t\t\t\tmap-\u003ename, errstr(err));\n+\t\t\t\terr = bpf_object__mmap_arena(obj, map);\n+\t\t\t\tif (err)\n \t\t\t\t\treturn err;\n-\t\t\t\t}\n-\t\t\t\tif (obj-\u003earena_data) {\n-\t\t\t\t\tmemcpy(map-\u003emmaped + obj-\u003earena_data_off, obj-\u003earena_data,\n-\t\t\t\t\t\tobj-\u003earena_data_sz);\n-\t\t\t\t\tzfree(\u0026obj-\u003earena_data);\n-\t\t\t\t}\n \t\t\t}\n \t\t\tif (map-\u003einit_slots_sz \u0026\u0026 map-\u003edef.type != BPF_MAP_TYPE_PROG_ARRAY) {\n \t\t\t\terr = init_map_in_map_slots(obj, map);\n@@ -7803,6 +8020,81 @@ static int bpf_program_fixup_func_info(struct bpf_object *obj, struct bpf_progra\n \treturn err;\n }\n \n+/*\n+ * Turn pointers to data that are stored in data into addresses of arena.\n+ * The arena is created here, ahead of the other maps: the address has to be\n+ * known before the maps that hold the pointers are created.\n+ */\n+static int bpf_object__relocate_data_ptrs(struct bpf_object *obj)\n+{\n+\tstruct bpf_map *arena, *map, *targ;\n+\t__u64 addr, val;\n+\tsize_t i;\n+\tint err;\n+\n+\tif (!obj-\u003edata_ptr_cnt)\n+\t\treturn 0;\n+\n+\tfor (i = 0; i \u003c obj-\u003edata_ptr_cnt; i++) {\n+\t\tstruct data_ptr *p = \u0026obj-\u003edata_ptrs[i];\n+\n+\t\tif (p-\u003etarg_sec_idx \u003c 0)\n+\t\t\tcontinue;\n+\t\tmap = bpf_object__sec_map(obj, p-\u003esec_idx);\n+\t\ttarg = bpf_object__sec_map(obj, p-\u003etarg_sec_idx);\n+\t\tif (map \u0026\u0026 (map-\u003ein_arena || map-\u003eautocreate) \u0026\u0026\n+\t\t (!targ || (!targ-\u003ein_arena \u0026\u0026 !targ-\u003earena_copy))) {\n+\t\t\tpr_warn(\"sec '%s': pointer to '%s' at offset %zu can't be resolved: sec '%s' is not in arena\\n\",\n+\t\t\t\tmap-\u003ereal_name, p-\u003esym_name, p-\u003esec_off,\n+\t\t\t\ttarg ? targ-\u003ereal_name : \"\u003c?\u003e\");\n+\t\t\treturn -LIBBPF_ERRNO__RELOC;\n+\t\t}\n+\t}\n+\n+\tif (obj-\u003egen_loader) {\n+\t\tpr_warn(\"pointers to data in data are not supported by light skeleton\\n\");\n+\t\treturn -ENOTSUP;\n+\t}\n+\n+\tif (obj-\u003earena_map_idx \u003c 0)\n+\t\treturn 0;\n+\n+\tarena = \u0026obj-\u003emaps[obj-\u003earena_map_idx];\n+\tif (!arena-\u003eautocreate)\n+\t\treturn 0;\n+\t/* libbpf doesn't mmap an arena that it didn't create, so the address is not known */\n+\tif (arena-\u003ereused || arena-\u003epin_path) {\n+\t\tpr_warn(\"map '%s': pointers to data in data are not supported with pinned or reused arena\\n\",\n+\t\t\tarena-\u003ename);\n+\t\treturn -ENOTSUP;\n+\t}\n+\n+\terr = bpf_object__create_map(obj, arena, false);\n+\terr = err ?: bpf_object__mmap_arena(obj, arena);\n+\tif (err) {\n+\t\tpr_warn(\"map '%s': failed to create: %s\\n\", arena-\u003ename, errstr(err));\n+\t\treturn err;\n+\t}\n+\tobj-\u003earena_mapped = true;\n+\n+\tfor (i = 0; i \u003c obj-\u003edata_ptr_cnt; i++) {\n+\t\tstruct data_ptr *p = \u0026obj-\u003edata_ptrs[i];\n+\n+\t\tmap = bpf_object__sec_map(obj, p-\u003esec_idx);\n+\t\tif (!map || (!map-\u003ein_arena \u0026\u0026 !map-\u003eautocreate))\n+\t\t\tcontinue;\n+\n+\t\taddr = (__u64)(unsigned long)arena-\u003emmaped + obj-\u003earena_data_off + p-\u003etarg_off;\n+\t\tif (p-\u003etarg_sec_idx \u003e= 0)\n+\t\t\taddr += bpf_object__sec_map(obj, p-\u003etarg_sec_idx)-\u003earena_off;\n+\n+\t\tmemcpy(\u0026val, map-\u003emmaped + p-\u003esec_off, sizeof(val));\n+\t\tval += addr;\n+\t\tmemcpy(map-\u003emmaped + p-\u003esec_off, \u0026val, sizeof(val));\n+\t}\n+\treturn 0;\n+}\n+\n static int bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path)\n {\n \tstruct bpf_program *prog;\n@@ -7825,8 +8117,10 @@ static int bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_pat\n \t\tsize_t mmap_sz = bpf_map_mmap_sz(arena_map);\n \n \t\tif (data_sz \u003e mmap_sz) {\n-\t\t\tpr_warn(\"map '%s': declared ARENA map size (%zu) is too small to hold global __arena variables of size %zu\\n\",\n-\t\t\t\tarena_map-\u003ename, mmap_sz, obj-\u003earena_data_sz);\n+\t\t\tpr_warn(\"map '%s': declared ARENA map size (%zu) is too small to hold global %s of size %zu\\n\",\n+\t\t\t\tarena_map-\u003ename, mmap_sz,\n+\t\t\t\tobj-\u003edata_in_arena ? \"data\" : \"__arena variables\",\n+\t\t\t\tobj-\u003earena_data_sz);\n \t\t\treturn -E2BIG;\n \t\t}\n \n@@ -7835,6 +8129,10 @@ static int bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_pat\n \t\t\tobj-\u003earena_data_off = mmap_sz - data_sz;\n \t}\n \n+\terr = bpf_object__relocate_data_ptrs(obj);\n+\tif (err)\n+\t\treturn err;\n+\n \t/* Before relocating calls pre-process relocations and mark\n \t * few ld_imm64 instructions that points to subprogs.\n \t * Otherwise bpf_object__reloc_code() later would have to consider\n@@ -8067,12 +8365,85 @@ static int bpf_object__collect_map_relos(struct bpf_object *obj,\n \treturn 0;\n }\n \n+/*\n+ * A pointer to data that is stored in data. When data is in arena\n+ * the program dereferences what it loads from data, so the pointer has\n+ * to be the address of the target in arena.\n+ */\n+static int bpf_object__collect_data_ptr(struct bpf_object *obj, const char *relo_sec_name,\n+\t\t\t\t\tint relo_idx, size_t sec_idx, const Elf64_Rel *rel,\n+\t\t\t\t\tconst Elf64_Sym *sym)\n+{\n+\tElf_Data *scn_data = obj-\u003eefile.secs[sec_idx].data;\n+\tconst char *sym_name = elf_sym_str(obj, sym-\u003est_name) ?: \"\u003c?\u003e\";\n+\tstruct data_ptr *ptrs;\n+\n+\tif (ELF64_ST_TYPE(sym-\u003est_info) == STT_SECTION \u0026\u0026 sym-\u003est_shndx \u003c obj-\u003eefile.sec_cnt)\n+\t\tsym_name = elf_sec_name(obj, elf_sec_by_idx(obj, sym-\u003est_shndx)) ?: \"\u003c?\u003e\";\n+\n+\tif (ELF64_R_TYPE(rel-\u003er_info) != R_BPF_64_ABS64 ||\n+\t sym-\u003est_shndx \u003e= obj-\u003eefile.sec_cnt ||\n+\t (sym-\u003est_shndx != obj-\u003eefile.arena_data_shndx \u0026\u0026\n+\t !bpf_object__shndx_is_data(obj, sym-\u003est_shndx)) ||\n+\t rel-\u003er_offset \u003e= scn_data-\u003ed_size ||\n+\t scn_data-\u003ed_size - rel-\u003er_offset \u003c sizeof(__u64)) {\n+\t\tpr_warn(\"sec '%s': relo #%d: can't resolve pointer to '%s' at offset %zu when data is in arena\\n\",\n+\t\t\trelo_sec_name, relo_idx, sym_name, (size_t)rel-\u003er_offset);\n+\t\treturn -LIBBPF_ERRNO__RELOC;\n+\t}\n+\n+\tptrs = libbpf_reallocarray(obj-\u003edata_ptrs, obj-\u003edata_ptr_cnt + 1, sizeof(*ptrs));\n+\tif (!ptrs)\n+\t\treturn -ENOMEM;\n+\tobj-\u003edata_ptrs = ptrs;\n+\n+\tptrs[obj-\u003edata_ptr_cnt].sec_idx = sec_idx;\n+\tptrs[obj-\u003edata_ptr_cnt].sec_off = rel-\u003er_offset;\n+\t/* __arena variables are at the start of arena data */\n+\tptrs[obj-\u003edata_ptr_cnt].targ_sec_idx =\n+\t\tsym-\u003est_shndx == obj-\u003eefile.arena_data_shndx ? -1 : sym-\u003est_shndx;\n+\tptrs[obj-\u003edata_ptr_cnt].targ_off = sym-\u003est_value;\n+\tptrs[obj-\u003edata_ptr_cnt].sym_name = strdup(sym_name);\n+\tif (!ptrs[obj-\u003edata_ptr_cnt].sym_name)\n+\t\treturn -ENOMEM;\n+\tobj-\u003edata_ptr_cnt++;\n+\n+\tpr_debug(\"sec '%s': relo #%d: pointer at offset %zu to '%s'\\n\",\n+\t\t relo_sec_name, relo_idx, (size_t)rel-\u003er_offset, sym_name);\n+\treturn 0;\n+}\n+\n+/* Collect pointers in a data section that went to arena */\n+static int bpf_object__collect_data_relos(struct bpf_object *obj,\n+\t\t\t\t\t Elf64_Shdr *shdr, Elf_Data *data)\n+{\n+\tint i, err, nrels = shdr-\u003esh_size / shdr-\u003esh_entsize;\n+\tconst char *relo_sec_name;\n+\tElf64_Sym *sym;\n+\tElf64_Rel *rel;\n+\n+\trelo_sec_name = elf_sec_str(obj, shdr-\u003esh_name) ?: \"\u003c?\u003e\";\n+\tfor (i = 0; i \u003c nrels; i++) {\n+\t\trel = elf_rel_by_idx(data, i);\n+\t\tsym = rel ? elf_sym_by_idx(obj, ELF64_R_SYM(rel-\u003er_info)) : NULL;\n+\t\tif (!sym) {\n+\t\t\tpr_warn(\"sec '%s': failed to get relo #%d\\n\", relo_sec_name, i);\n+\t\t\treturn -LIBBPF_ERRNO__FORMAT;\n+\t\t}\n+\t\terr = bpf_object__collect_data_ptr(obj, relo_sec_name, i, shdr-\u003esh_info, rel, sym);\n+\t\tif (err)\n+\t\t\treturn err;\n+\t}\n+\treturn 0;\n+}\n+\n /*\n * Collect pointers to functions in a read-only data section. They are\n * R_BPF_64_ABS64 relocations against .text section, where the offset of\n * a static function in the section is stored in place. Relocations in data\n * sections were ignored before pointers to functions were supported. Those\n- * that are something else, e.g. pointers to data, still are.\n+ * that are something else, e.g. pointers to data, still are, unless data\n+ * is in arena.\n */\n static int bpf_object__collect_rodata_relos(struct bpf_object *obj,\n \t\t\t\t\t Elf64_Shdr *shdr, Elf_Data *data)\n@@ -8108,6 +8479,15 @@ static int bpf_object__collect_rodata_relos(struct bpf_object *obj,\n \n \t\tif (ELF64_R_TYPE(rel-\u003er_info) != R_BPF_64_ABS64 ||\n \t\t !sym_is_subprog(sym, obj-\u003eefile.text_shndx)) {\n+\t\t\tint err;\n+\n+\t\t\tif (obj-\u003edata_in_arena) {\n+\t\t\t\terr = bpf_object__collect_data_ptr(obj, relo_sec_name, i,\n+\t\t\t\t\t\t\t\t sec_idx, rel, sym);\n+\t\t\t\tif (err)\n+\t\t\t\t\treturn err;\n+\t\t\t\tcontinue;\n+\t\t\t}\n \t\t\tpr_debug(\"sec '%s': relo #%d: not a pointer to a function, skipping...\\n\",\n \t\t\t\t relo_sec_name, i);\n \t\t\tcontinue;\n@@ -8183,6 +8563,8 @@ static int bpf_object__collect_relos(struct bpf_object *obj)\n \n \t\tif (obj-\u003eefile.secs[idx].sec_type == SEC_RODATA)\n \t\t\terr = bpf_object__collect_rodata_relos(obj, shdr, data);\n+\t\telse if (obj-\u003eefile.secs[idx].sec_type == SEC_DATA)\n+\t\t\terr = bpf_object__collect_data_relos(obj, shdr, data);\n \t\telse if (obj-\u003eefile.secs[idx].sec_type == SEC_ST_OPS)\n \t\t\terr = bpf_object__collect_st_ops_relos(obj, shdr, data);\n \t\telse if (idx == obj-\u003eefile.btf_maps_shndx)\n@@ -8476,6 +8858,9 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog\n \t}\n \tload_attr.log_level = log_level;\n \tload_attr.prog_flags = prog-\u003eprog_flags;\n+\t/* the program accesses its data in arena through plain numbers */\n+\tif (obj-\u003edata_in_arena)\n+\t\tload_attr.prog_flags |= BPF_F_ARENA_SCALAR;\n \tload_attr.fd_array = obj-\u003efd_array;\n \n \tload_attr.token_fd = obj-\u003etoken_fd;\n@@ -8547,7 +8932,7 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog\n \n \t\t\tfor (i = 0; i \u003c obj-\u003enr_maps; i++) {\n \t\t\t\tmap = \u0026prog-\u003eobj-\u003emaps[i];\n-\t\t\t\tif (map-\u003elibbpf_type != LIBBPF_MAP_RODATA)\n+\t\t\t\tif (map-\u003elibbpf_type != LIBBPF_MAP_RODATA || map-\u003ein_arena)\n \t\t\t\t\tcontinue;\n \n \t\t\t\tif (bpf_prog_bind_map(ret, map-\u003efd, NULL)) {\n@@ -10111,7 +10496,7 @@ static void bpf_map__destroy(struct bpf_map *map)\n \tzfree(\u0026map-\u003einit_slots);\n \tmap-\u003einit_slots_sz = 0;\n \n-\tif (map-\u003emmaped \u0026\u0026 map-\u003emmaped != map-\u003eobj-\u003earena_data)\n+\tif (map-\u003emmaped \u0026\u0026 map-\u003emmaped != map-\u003eobj-\u003earena_data \u0026\u0026 !map-\u003ein_arena)\n \t\tmunmap(map-\u003emmaped, bpf_map_mmap_sz(map));\n \tmap-\u003emmaped = NULL;\n \n@@ -10196,6 +10581,9 @@ void bpf_object__close(struct bpf_object *obj)\n \t\t\tclose(obj-\u003efunc_ptr_maps[i].fd);\n \tzfree(\u0026obj-\u003efunc_ptr_maps);\n \tzfree(\u0026obj-\u003efunc_ptrs);\n+\tfor (i = 0; i \u003c obj-\u003edata_ptr_cnt; i++)\n+\t\tzfree(\u0026obj-\u003edata_ptrs[i].sym_name);\n+\tzfree(\u0026obj-\u003edata_ptrs);\n \n \tif (obj-\u003ebtf_module_allowlist) {\n \t\tfor (i = 0; i \u003c obj-\u003ebtf_module_allowlist_cnt; i++)\n@@ -10556,6 +10944,8 @@ int bpf_program__clone(struct bpf_program *prog, const struct bpf_prog_load_opts\n \tattr.token_fd = OPTS_GET(opts, token_fd, 0) ?: obj-\u003etoken_fd;\n \tif (attr.token_fd)\n \t\tattr.prog_flags |= BPF_F_TOKEN_FD;\n+\tif (obj-\u003edata_in_arena)\n+\t\tattr.prog_flags |= BPF_F_ARENA_SCALAR;\n \n \tprog_btf_fd = OPTS_GET(opts, prog_btf_fd, 0);\n \tif (!prog_btf_fd \u0026\u0026 obj-\u003ebtf)\n@@ -11562,6 +11952,9 @@ int bpf_map__set_value_size(struct bpf_map *map, __u32 size)\n \tif (map_is_created(map))\n \t\treturn libbpf_err(-EBUSY);\n \n+\tif (map-\u003ein_arena)\n+\t\treturn libbpf_err(-EOPNOTSUPP);\n+\n \tif (map-\u003emmaped) {\n \t\tsize_t mmap_old_sz, mmap_new_sz;\n \t\tint err;\ndiff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile\nindex afa589a27b151..a22be7efd1fa2 100644\n--- a/tools/testing/selftests/bpf/Makefile\n+++ b/tools/testing/selftests/bpf/Makefile\n@@ -422,6 +422,16 @@ $(LIBARENA_ASAN_SKEL): $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ) $(LIBARENA_BPF_DEPS)\n \t+$(MAKE) -C libarena libarena_asan.skel.h $(LIBARENA_MAKE_ARGS)\n endif\n \n+# #![no_std] looks for compiler_builtins too. Nothing of it is used.\n+ifneq ($(RUST_CORE),)\n+$(RUST_CORE): $(RUST_CORE_SRC)\n+\t$(call msg,RUSTC,,$@)\n+\t$(Q)mkdir -p $(@D)\n+\t+$(Q)$(RUSTC_BPF) -A warnings --edition 2024 --crate-name core --out-dir $(@D) $\u003c\n+\t+$(Q)echo '#![feature(compiler_builtins)] #![compiler_builtins] #![no_std]' | \\\n+\t\t$(RUSTC_BPF) -A warnings --crate-name compiler_builtins --out-dir $(@D) -\n+endif\n+\n # Generated test list headers\n \n define gen_tests_hdr\n@@ -457,7 +467,7 @@ RUNNER_PREREQS := $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ) $(BPFTOOL)\t\t\\\n \t\t $(VERIFY_SIG_HDR) $(PRIVATE_KEY) $(VERIFICATION_CERT)\t\\\n \t\t $(LIBARENA_SKEL) $(LIBARENA_ASAN_SKEL)\t\t\\\n \t\t prog_tests/tests.h map_tests/tests.h\t\t\t\\\n-\t\t $(RUNNER_OBJS)\n+\t\t $(RUNNER_OBJS) $(RUST_CORE)\n \n # Runtime fixtures for each test_progs flavor.\n RUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read\t\t\t\t\\\ndiff --git a/tools/testing/selftests/bpf/Makefile.buildvars b/tools/testing/selftests/bpf/Makefile.buildvars\nindex d2a0c0031b87d..ff3476bc40a49 100644\n--- a/tools/testing/selftests/bpf/Makefile.buildvars\n+++ b/tools/testing/selftests/bpf/Makefile.buildvars\n@@ -109,6 +109,31 @@ HOST_INCLUDE_DIR\t:= $(INCLUDE_DIR)\n endif\n RESOLVE_BTFIDS := $(HOST_BUILD_DIR)/resolve_btfids/resolve_btfids\n \n+# Programs in Rust are built by upstream rustc. It has no prebuilt core for\n+# the bpf target, so it has to come with the source of core:\n+# rustup component add rust-src\n+# core is built as edition 2024, which it is since rustc 1.87.\n+# rustc emits LLVM bitcode and clang makes the object of it, so clang has to be\n+# 23 or newer and not older than LLVM of rustc.\n+# Otherwise RUST_CORE is empty and the tests are skipped.\n+RUSTC ?= rustc\n+RUST_CORE_SRC := $(wildcard $(shell $(RUSTC) --print sysroot 2\u003e/dev/null)$\\\n+\t\t\t /lib/rustlib/src/rust/library/core/src/lib.rs)\n+ifneq ($(RUST_CORE_SRC),)\n+ifeq ($(shell { clang=$$(echo __clang_major__ | $(CLANG) -E -P -x c -) \u0026\u0026\t\t\\\n+\t\tllvm=$$($(srctree)/scripts/rustc-llvm-version.sh $(RUSTC)) \u0026\u0026\t\t\\\n+\t\t[ $$($(srctree)/scripts/rustc-version.sh $(RUSTC)) -ge 108700 ] \u0026\u0026\t\\\n+\t\t[ $$clang -ge 23 ] \u0026\u0026 [ $$clang -ge $$((llvm / 10000)) ]; }\t\t\\\n+\t\t2\u003e/dev/null \u0026\u0026 echo y),y)\n+RUST_CORE := $(BUILD_DIR)/rust/libcore.rlib\n+endif\n+endif\n+# RUSTC_BOOTSTRAP=1 is to build core with a stable rustc, like the kernel does.\n+# panic=abort is a stop gap until panic=unwind is supported.\n+RUSTC_BPF = RUSTC_BOOTSTRAP=1 $(RUSTC) -O -C panic=abort --crate-type rlib\t\\\n+\t --target $(if $(IS_LITTLE_ENDIAN),bpfel,bpfeb)-unknown-none\t\t\\\n+\t -L $(dir $(RUST_CORE))\n+\n DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool\n ifneq ($(CROSS_COMPILE),)\n CROSS_BPFTOOL := $(SCRATCH_DIR)/sbin/bpftool\ndiff --git a/tools/testing/selftests/bpf/Makefile.skel b/tools/testing/selftests/bpf/Makefile.skel\nindex 2e22bb901bf3b..06e297a17156c 100644\n--- a/tools/testing/selftests/bpf/Makefile.skel\n+++ b/tools/testing/selftests/bpf/Makefile.skel\n@@ -20,7 +20,8 @@ ifneq ($(BPF_CC),)\n BPF_SRCS := $(notdir $(wildcard progs/*.c))\n BPF_OBJS := $(patsubst %.c,$(RDIR)/%.bpf.o,$(BPF_SRCS))\n \n-SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c\n+SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c\t\\\n+\t\t data_in_arena_extern.c data_in_arena_nomap.c\n \n LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h\t\t\\\n \t\tlinked_vars.skel.h linked_maps.skel.h linked_arena.skel.h \\\n@@ -137,4 +138,19 @@ $(LINKED_SKELS_H): $(RDIR)/%.skel.h: $$(addprefix $(RDIR)/,$$($$*.skel.h-deps))\n \t\t $(BPFTOOL) $(GEN_SKEL) | $(RDIR)\n \t$(Q)$(cmd_bpf_link_skel)\n \n+# Programs in Rust, see Makefile.buildvars. No skeletons: the objects may be absent.\n+ifneq ($(RUST_CORE),)\n+ifeq ($(BPF_CC),$(CLANG))\n+RUST_OBJS := $(patsubst progs/%.rs,$(RDIR)/%.bpf.o,$(wildcard progs/*.rs))\n+BPF_OBJS += $(RUST_OBJS)\n+\n+$(RUST_OBJS): $(RDIR)/%.bpf.o: progs/%.rs $(RUST_CORE) | $(RDIR)\n+\t$(call msg,RUSTC,$(BINARY),$@)\n+\t+$(Q)$(RUSTC_BPF) --edition 2021 -C debuginfo=2 --emit=llvm-bc\t\t\t\\\n+\t\t$(patsubst -mcpu=%,-C target-cpu=%,$(filter -mcpu=%,$(BPF_CC_FLAGS)))\t\\\n+\t\t-o $(dir $(RUST_CORE))$(BINARY)-$*.bc $\u003c \u0026\u0026\t\t\t\t\\\n+\t$(BPF_CC) $(BPF_CC_FLAGS) -c $(dir $(RUST_CORE))$(BINARY)-$*.bc -o $@ $(call skip_on_fail,BPF)\n+endif\n+endif\n+\n endif # BPF_CC\ndiff --git a/tools/testing/selftests/bpf/prog_tests/arena_scalar_blinded.c b/tools/testing/selftests/bpf/prog_tests/arena_scalar_blinded.c\nnew file mode 100644\nindex 0000000000000..2ac2e9a652fec\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/prog_tests/arena_scalar_blinded.c\n@@ -0,0 +1,21 @@\n+// SPDX-License-Identifier: GPL-2.0\n+\n+#include \u003ctest_progs.h\u003e\n+#include \"sysctl_helpers.h\"\n+#include \"verifier_arena_scalar.skel.h\"\n+\n+/* The same tests with constants of the programs blinded */\n+void serial_test_arena_scalar_blinded(void)\n+{\n+\tconst char *harden = \"/proc/sys/net/core/bpf_jit_harden\";\n+\tchar old[16] = {};\n+\n+\tif (!is_jit_enabled()) {\n+\t\ttest__skip();\n+\t\treturn;\n+\t}\n+\tif (sysctl_set_or_fail(harden, old, \"2\"))\n+\t\treturn;\n+\tRUN_TESTS(verifier_arena_scalar);\n+\tsysctl_set_or_fail(harden, NULL, old);\n+}\ndiff --git a/tools/testing/selftests/bpf/prog_tests/btf.c b/tools/testing/selftests/bpf/prog_tests/btf.c\nindex df6ad38d287da..24ab62b2834ae 100644\n--- a/tools/testing/selftests/bpf/prog_tests/btf.c\n+++ b/tools/testing/selftests/bpf/prog_tests/btf.c\n@@ -424,7 +424,7 @@ static struct btf_raw_test raw_tests[] = {\n \t.err_str = \"Invalid type\",\n },\n {\n-\t.descr = \"global data test #8, invalid var size\",\n+\t.descr = \"global data test #8, var is smaller than its type\",\n \t.raw_types = {\n \t\t/* int */\n \t\tBTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),\t/* [1] */\n@@ -457,8 +457,6 @@ static struct btf_raw_test raw_tests[] = {\n \t.key_type_id = 0,\n \t.value_type_id = 7,\n \t.max_entries = 1,\n-\t.btf_load_err = true,\n-\t.err_str = \"Invalid size\",\n },\n {\n \t.descr = \"global data test #9, invalid var size\",\n@@ -498,7 +496,7 @@ static struct btf_raw_test raw_tests[] = {\n \t.err_str = \"Invalid size\",\n },\n {\n-\t.descr = \"global data test #10, invalid var size\",\n+\t.descr = \"global data test #10, section is smaller than map value\",\n \t.raw_types = {\n \t\t/* int */\n \t\tBTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),\t/* [1] */\n@@ -531,8 +529,7 @@ static struct btf_raw_test raw_tests[] = {\n \t.key_type_id = 0,\n \t.value_type_id = 7,\n \t.max_entries = 1,\n-\t.btf_load_err = true,\n-\t.err_str = \"Invalid size\",\n+\t.map_create_err = true,\n },\n {\n \t.descr = \"global data test #11, multiple section members\",\n@@ -1987,14 +1984,14 @@ static struct btf_raw_test raw_tests[] = {\n },\n \n {\n-\t.descr = \"typedef (invalid name, invalid identifier)\",\n+\t.descr = \"typedef (invalid name, not printable)\",\n \t.raw_types = {\n \t\tBTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),\t/* [1] */\n \t\tBTF_TYPEDEF_ENC(NAME_TBD, 1),\t\t\t/* [2] */\n \t\tBTF_END_RAW,\n \t},\n-\t.str_sec = \"\\0__!int\",\n-\t.str_sec_size = sizeof(\"\\0__!int\"),\n+\t.str_sec = \"\\0__\\7int\",\n+\t.str_sec_size = sizeof(\"\\0__\\7int\"),\n \t.map_type = BPF_MAP_TYPE_ARRAY,\n \t.map_name = \"typedef_check_btf\",\n \t.key_size = sizeof(int),\n@@ -2112,15 +2109,15 @@ static struct btf_raw_test raw_tests[] = {\n },\n \n {\n-\t.descr = \"fwd type (invalid name, invalid identifier)\",\n+\t.descr = \"fwd type (invalid name, not printable)\",\n \t.raw_types = {\n \t\tBTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),\t\t/* [1] */\n \t\tBTF_TYPE_ENC(NAME_TBD,\n \t\t\t BTF_INFO_ENC(BTF_KIND_FWD, 0, 0), 0),\t/* [2] */\n \t\tBTF_END_RAW,\n \t},\n-\t.str_sec = \"\\0__!skb\",\n-\t.str_sec_size = sizeof(\"\\0__!skb\"),\n+\t.str_sec = \"\\0__\\7skb\",\n+\t.str_sec_size = sizeof(\"\\0__\\7skb\"),\n \t.map_type = BPF_MAP_TYPE_ARRAY,\n \t.map_name = \"fwd_type_check_btf\",\n \t.key_size = sizeof(int),\n@@ -2175,7 +2172,7 @@ static struct btf_raw_test raw_tests[] = {\n },\n \n {\n-\t.descr = \"struct type (invalid name, invalid identifier)\",\n+\t.descr = \"struct type (invalid name, not printable)\",\n \t.raw_types = {\n \t\tBTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),\t\t/* [1] */\n \t\tBTF_TYPE_ENC(NAME_TBD,\n@@ -2183,8 +2180,8 @@ static struct btf_raw_test raw_tests[] = {\n \t\tBTF_MEMBER_ENC(NAME_TBD, 1, 0),\n \t\tBTF_END_RAW,\n \t},\n-\t.str_sec = \"\\0A!\\0B\",\n-\t.str_sec_size = sizeof(\"\\0A!\\0B\"),\n+\t.str_sec = \"\\0A\\7\\0B\",\n+\t.str_sec_size = sizeof(\"\\0A\\7\\0B\"),\n \t.map_type = BPF_MAP_TYPE_ARRAY,\n \t.map_name = \"struct_type_check_btf\",\n \t.key_size = sizeof(int),\n@@ -2217,7 +2214,7 @@ static struct btf_raw_test raw_tests[] = {\n },\n \n {\n-\t.descr = \"struct member (invalid name, invalid identifier)\",\n+\t.descr = \"struct member (invalid name, not printable)\",\n \t.raw_types = {\n \t\tBTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),\t\t/* [1] */\n \t\tBTF_TYPE_ENC(NAME_TBD,\n@@ -2225,8 +2222,8 @@ static struct btf_raw_test raw_tests[] = {\n \t\tBTF_MEMBER_ENC(NAME_TBD, 1, 0),\n \t\tBTF_END_RAW,\n \t},\n-\t.str_sec = \"\\0A\\0B*\",\n-\t.str_sec_size = sizeof(\"\\0A\\0B*\"),\n+\t.str_sec = \"\\0A\\0B\\7\",\n+\t.str_sec_size = sizeof(\"\\0A\\0B\\7\"),\n \t.map_type = BPF_MAP_TYPE_ARRAY,\n \t.map_name = \"struct_type_check_btf\",\n \t.key_size = sizeof(int),\n@@ -2260,7 +2257,7 @@ static struct btf_raw_test raw_tests[] = {\n },\n \n {\n-\t.descr = \"enum type (invalid name, invalid identifier)\",\n+\t.descr = \"enum type (invalid name, not printable)\",\n \t.raw_types = {\n \t\tBTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),\t\t/* [1] */\n \t\tBTF_TYPE_ENC(NAME_TBD,\n@@ -2269,8 +2266,8 @@ static struct btf_raw_test raw_tests[] = {\n \t\tBTF_ENUM_ENC(NAME_TBD, 0),\n \t\tBTF_END_RAW,\n \t},\n-\t.str_sec = \"\\0A!\\0B\",\n-\t.str_sec_size = sizeof(\"\\0A!\\0B\"),\n+\t.str_sec = \"\\0A\\7\\0B\",\n+\t.str_sec_size = sizeof(\"\\0A\\7\\0B\"),\n \t.map_type = BPF_MAP_TYPE_ARRAY,\n \t.map_name = \"enum_type_check_btf\",\n \t.key_size = sizeof(int),\n@@ -2306,7 +2303,7 @@ static struct btf_raw_test raw_tests[] = {\n },\n \n {\n-\t.descr = \"enum member (invalid name, invalid identifier)\",\n+\t.descr = \"enum member (invalid name, not printable)\",\n \t.raw_types = {\n \t\tBTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),\t\t/* [1] */\n \t\tBTF_TYPE_ENC(0,\n@@ -2315,8 +2312,8 @@ static struct btf_raw_test raw_tests[] = {\n \t\tBTF_ENUM_ENC(NAME_TBD, 0),\n \t\tBTF_END_RAW,\n \t},\n-\t.str_sec = \"\\0A!\",\n-\t.str_sec_size = sizeof(\"\\0A!\"),\n+\t.str_sec = \"\\0A\\7\",\n+\t.str_sec_size = sizeof(\"\\0A\\7\"),\n \t.map_type = BPF_MAP_TYPE_ARRAY,\n \t.map_name = \"enum_type_check_btf\",\n \t.key_size = sizeof(int),\n@@ -2625,14 +2622,14 @@ static struct btf_raw_test raw_tests[] = {\n \t.raw_types = {\n \t\tBTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),\t/* [1] */\n \t\tBTF_TYPE_INT_ENC(0, 0, 0, 32, 4),\t\t/* [2] */\n-\t\t/* void (*)(int a, unsigned int !!!) */\n+\t\t/* void (*)(int a, unsigned int \\7) */\n \t\tBTF_FUNC_PROTO_ENC(0, 2),\t\t\t/* [3] */\n \t\t\tBTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),\n \t\t\tBTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),\n \t\tBTF_END_RAW,\n \t},\n-\t.str_sec = \"\\0a\\0!!!\",\n-\t.str_sec_size = sizeof(\"\\0a\\0!!!\"),\n+\t.str_sec = \"\\0a\\0\\7\",\n+\t.str_sec_size = sizeof(\"\\0a\\0\\7\"),\n \t.map_type = BPF_MAP_TYPE_ARRAY,\n \t.map_name = \"func_proto_type_check_btf\",\n \t.key_size = sizeof(int),\n@@ -2775,12 +2772,12 @@ static struct btf_raw_test raw_tests[] = {\n \t\tBTF_FUNC_PROTO_ENC(0, 2),\t\t\t/* [3] */\n \t\t\tBTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),\n \t\t\tBTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),\n-\t\t/* void !!!(int a, unsigned int b) */\n+\t\t/* void \\7(int a, unsigned int b) */\n \t\tBTF_FUNC_ENC(NAME_TBD, 3),\t\t\t/* [4] */\n \t\tBTF_END_RAW,\n \t},\n-\t.str_sec = \"\\0a\\0b\\0!!!\",\n-\t.str_sec_size = sizeof(\"\\0a\\0b\\0!!!\"),\n+\t.str_sec = \"\\0a\\0b\\0\\7\",\n+\t.str_sec_size = sizeof(\"\\0a\\0b\\0\\7\"),\n \t.map_type = BPF_MAP_TYPE_ARRAY,\n \t.map_name = \"func_type_check_btf\",\n \t.key_size = sizeof(int),\n@@ -2793,7 +2790,7 @@ static struct btf_raw_test raw_tests[] = {\n },\n \n {\n-\t.descr = \"func (Some arg has no name)\",\n+\t.descr = \"func (Some arg of global func has no name)\",\n \t.raw_types = {\n \t\tBTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),\t/* [1] */\n \t\tBTF_TYPE_INT_ENC(0, 0, 0, 32, 4),\t\t/* [2] */\n@@ -2802,7 +2799,8 @@ static struct btf_raw_test raw_tests[] = {\n \t\t\tBTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),\n \t\t\tBTF_FUNC_PROTO_ARG_ENC(0, 2),\n \t\t/* void func(int a, unsigned int) */\n-\t\tBTF_FUNC_ENC(NAME_TBD, 3),\t\t\t/* [4] */\n+\t\tBTF_TYPE_ENC(NAME_TBD,\t\t\t\t/* [4] */\n+\t\t\t BTF_INFO_ENC(BTF_KIND_FUNC, 0, BTF_FUNC_GLOBAL), 3),\n \t\tBTF_END_RAW,\n \t},\n \t.str_sec = \"\\0a\\0func\",\n@@ -2818,6 +2816,57 @@ static struct btf_raw_test raw_tests[] = {\n \t.err_str = \"Invalid arg#2\",\n },\n \n+{\n+\t.descr = \"func (Some arg of static func has no name)\",\n+\t.raw_types = {\n+\t\t/* int */\n+\t\tBTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),\t/* [1] */\n+\t\t/* unsigned int */\n+\t\tBTF_TYPE_INT_ENC(0, 0, 0, 32, 4),\t\t/* [2] */\n+\t\t/* void (*)(int a, unsigned int) */\n+\t\tBTF_FUNC_PROTO_ENC(0, 2),\t\t\t/* [3] */\n+\t\t\tBTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),\n+\t\t\tBTF_FUNC_PROTO_ARG_ENC(0, 2),\n+\t\t/* static void func(int a, unsigned int) */\n+\t\tBTF_FUNC_ENC(NAME_TBD, 3),\t\t\t/* [4] */\n+\t\tBTF_END_RAW,\n+\t},\n+\t.str_sec = \"\\0a\\0func\",\n+\t.str_sec_size = sizeof(\"\\0a\\0func\"),\n+\t.map_type = BPF_MAP_TYPE_ARRAY,\n+\t.map_name = \"func_type_check_btf\",\n+\t.key_size = sizeof(int),\n+\t.value_size = sizeof(int),\n+\t.key_type_id = 1,\n+\t.value_type_id = 1,\n+\t.max_entries = 4,\n+},\n+\n+{\n+\t.descr = \"func (vararg of global func has no name)\",\n+\t.raw_types = {\n+\t\t/* int */\n+\t\tBTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),\t/* [1] */\n+\t\t/* void (*)(int a, ...) */\n+\t\tBTF_FUNC_PROTO_ENC(0, 2),\t\t\t/* [2] */\n+\t\t\tBTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),\n+\t\t\tBTF_FUNC_PROTO_ARG_ENC(0, 0),\n+\t\t/* void func(int a, ...) */\n+\t\tBTF_TYPE_ENC(NAME_TBD,\t\t\t\t/* [3] */\n+\t\t\t BTF_INFO_ENC(BTF_KIND_FUNC, 0, BTF_FUNC_GLOBAL), 2),\n+\t\tBTF_END_RAW,\n+\t},\n+\t.str_sec = \"\\0a\\0func\",\n+\t.str_sec_size = sizeof(\"\\0a\\0func\"),\n+\t.map_type = BPF_MAP_TYPE_ARRAY,\n+\t.map_name = \"func_type_check_btf\",\n+\t.key_size = sizeof(int),\n+\t.value_size = sizeof(int),\n+\t.key_type_id = 1,\n+\t.value_type_id = 1,\n+\t.max_entries = 4,\n+},\n+\n {\n \t.descr = \"func (Non zero vlen)\",\n \t.raw_types = {\n@@ -3585,15 +3634,28 @@ static struct btf_raw_test raw_tests[] = {\n \t.btf_load_err = true,\n },\n {\n-\t.descr = \"type name '?foo' is not ok\",\n+\t.descr = \"type name '?foo' is ok\",\n \t.raw_types = {\n \t\t/* union ?foo; */\n \t\tBTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_FWD, 1, 0), 0), /* [1] */\n \t\tBTF_END_RAW,\n \t},\n \tBTF_STR_SEC(\"\\0?foo\"),\n-\t.err_str = \"Invalid name\",\n-\t.btf_load_err = true,\n+},\n+{\n+\t.descr = \"names of Rust types and functions are ok\",\n+\t.raw_types = {\n+\t\tBTF_TYPE_INT_ENC(NAME_NTH(1), 0, 0, 32, 4),\t/* [1] */\n+\t\tBTF_STRUCT_ENC(NAME_NTH(2), 1, 4),\t\t/* [2] */\n+\t\tBTF_MEMBER_ENC(NAME_NTH(3), 1, 0),\n+\t\tBTF_FWD_ENC(NAME_NTH(4), 0),\t\t\t/* [3] */\n+\t\tBTF_TYPEDEF_ENC(NAME_NTH(5), 2),\t\t/* [4] */\n+\t\tBTF_FUNC_PROTO_ENC(0, 1),\t\t\t/* [5] */\n+\t\t\tBTF_FUNC_PROTO_ARG_ENC(NAME_NTH(6), 1),\n+\t\tBTF_FUNC_ENC(NAME_NTH(7), 5),\t\t\t/* [6] */\n+\t\tBTF_END_RAW,\n+\t},\n+\tBTF_STR_SEC(\"\\0u32\\0Option\u003c\u0026str\u003e\\0__0\\0*const str\\0{impl#9}\u003c[u8; 4]\u003e\\0self\\0fmt\u003cstr\u003e\"),\n },\n \n {\ndiff --git a/tools/testing/selftests/bpf/prog_tests/btf_rust.c b/tools/testing/selftests/bpf/prog_tests/btf_rust.c\nnew file mode 100644\nindex 0000000000000..daf777cadfda3\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/prog_tests/btf_rust.c\n@@ -0,0 +1,142 @@\n+// SPDX-License-Identifier: GPL-2.0\n+\n+#include \u003ctest_progs.h\u003e\n+#include \u003cbpf/btf.h\u003e\n+#include \"bpftool_helpers.h\"\n+\n+#define FUNC_NAME \"write_fmt\u003cscx_simple::BpfStream\u003e\"\n+#define KSYM_NAME \"write_fmt_scx_simple__BpfStream_\"\n+\n+/* The name of a function of Rust is a part of the name of the program in kallsyms */\n+static void test_func_name(void)\n+{\n+\tstruct bpf_insn insns[] = {\n+\t\tBPF_MOV64_IMM(BPF_REG_0, 0),\n+\t\tBPF_EXIT_INSN(),\n+\t};\n+\tLIBBPF_OPTS(bpf_prog_load_opts, opts);\n+\tint int_id, proto_id, prog_fd = -1, i;\n+\tstruct bpf_func_info func_info = {};\n+\tstruct bpf_prog_info info = {};\n+\tunsigned long long addr;\n+\t__u32 len = sizeof(info);\n+\tchar sym[128], *p = sym;\n+\tstruct btf *btf;\n+\n+\tbtf = btf__new_empty();\n+\tif (!ASSERT_OK_PTR(btf, \"btf\"))\n+\t\treturn;\n+\tint_id = btf__add_int(btf, \"i32\", 4, BTF_INT_SIGNED);\n+\tASSERT_GT(int_id, 0, \"int\");\n+\tproto_id = btf__add_func_proto(btf, int_id);\n+\tASSERT_GT(proto_id, 0, \"proto\");\n+\tASSERT_OK(btf__add_func_param(btf, \"ctx\", int_id), \"param\");\n+\tfunc_info.type_id = btf__add_func(btf, FUNC_NAME, BTF_FUNC_STATIC, proto_id);\n+\tASSERT_GT(func_info.type_id, 0, \"func\");\n+\tif (!ASSERT_OK(btf__load_into_kernel(btf), \"btf load\"))\n+\t\tgoto out;\n+\n+\topts.prog_btf_fd = btf__fd(btf);\n+\topts.func_info = \u0026func_info;\n+\topts.func_info_cnt = 1;\n+\topts.func_info_rec_size = sizeof(func_info);\n+\tprog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, \"GPL\", insns,\n+\t\t\t\tARRAY_SIZE(insns), \u0026opts);\n+\tif (!ASSERT_GE(prog_fd, 0, \"prog load\"))\n+\t\tgoto out;\n+\tif (!ASSERT_OK(bpf_prog_get_info_by_fd(prog_fd, \u0026info, \u0026len), \"prog info\"))\n+\t\tgoto out;\n+\tif (!info.jited_prog_len) {\n+\t\ttest__skip();\n+\t\tgoto out;\n+\t}\n+\n+\tp += sprintf(p, \"bpf_prog_\");\n+\tfor (i = 0; i \u003c BPF_TAG_SIZE; i++)\n+\t\tp += sprintf(p, \"%02x\", info.tag[i]);\n+\tsprintf(p, \"_%s\", KSYM_NAME);\n+\tASSERT_OK(kallsyms_find(sym, \u0026addr), sym);\n+out:\n+\tif (prog_fd \u003e= 0)\n+\t\tclose(prog_fd);\n+\tbtf__free(btf);\n+}\n+\n+#define PIN_PATH \"/sys/fs/bpf/btf_rust_piece\"\n+\n+/*\n+ * A piece of a static that LLVM split has the type of the whole static.\n+ * It's the last variable in the section, so its type ends past the map value.\n+ */\n+static void test_piece(void)\n+{\n+\tLIBBPF_OPTS(bpf_map_create_opts, opts);\n+\tint int_id, struct_id, var_id, piece_id, sec_id, map_fd = -1, key = 0;\n+\t__u32 value[2] = { 0x11111111, 0x22222222 };\n+\tchar line[256] = {}, out[1024] = {};\n+\tstruct btf *btf;\n+\tFILE *f = NULL;\n+\n+\tbtf = btf__new_empty();\n+\tif (!ASSERT_OK_PTR(btf, \"btf\"))\n+\t\treturn;\n+\tint_id = btf__add_int(btf, \"u32\", 4, 0);\n+\tASSERT_GT(int_id, 0, \"int\");\n+\tstruct_id = btf__add_struct(btf, \"Whole\", 8);\n+\tASSERT_GT(struct_id, 0, \"struct\");\n+\tASSERT_OK(btf__add_field(btf, \"a\", int_id, 0, 0), \"field\");\n+\tASSERT_OK(btf__add_field(btf, \"b\", int_id, 32, 0), \"field\");\n+\tvar_id = btf__add_var(btf, \"CNT\", BTF_VAR_STATIC, int_id);\n+\tASSERT_GT(var_id, 0, \"var\");\n+\tpiece_id = btf__add_var(btf, \"WHOLE.1\", BTF_VAR_STATIC, struct_id);\n+\tASSERT_GT(piece_id, 0, \"piece\");\n+\tsec_id = btf__add_datasec(btf, \".bss\", sizeof(value));\n+\tASSERT_GT(sec_id, 0, \"datasec\");\n+\tASSERT_OK(btf__add_datasec_var_info(btf, var_id, 0, 4), \"var info\");\n+\tASSERT_OK(btf__add_datasec_var_info(btf, piece_id, 4, 4), \"piece info\");\n+\tif (!ASSERT_OK(btf__load_into_kernel(btf), \"btf load\"))\n+\t\tgoto out;\n+\n+\topts.btf_fd = btf__fd(btf);\n+\topts.btf_value_type_id = sec_id;\n+\tmap_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, \".bss\", sizeof(key), sizeof(value), 1, \u0026opts);\n+\tif (!ASSERT_GE(map_fd, 0, \"map create\"))\n+\t\tgoto out;\n+\tif (!ASSERT_OK(bpf_map_update_elem(map_fd, \u0026key, value, 0), \"map update\"))\n+\t\tgoto out;\n+\n+\t/* the variable is printed, the piece is not */\n+\tunlink(PIN_PATH);\n+\tif (!ASSERT_OK(bpf_obj_pin(map_fd, PIN_PATH), \"pin\"))\n+\t\tgoto out;\n+\tf = fopen(PIN_PATH, \"r\");\n+\tif (!ASSERT_OK_PTR(f, \"open\"))\n+\t\tgoto out;\n+\twhile (fgets(line, sizeof(line), f) \u0026\u0026 line[0] == '#')\n+\t\t;\n+\tASSERT_HAS_SUBSTR(line, \"286331153\", \"var\");\n+\tASSERT_NULL(strstr(line, \"572662306\"), \"piece\");\n+\n+\t/* the same for bpftool */\n+\tif (!ASSERT_OK(get_bpftool_command_output(\"map dump pinned \" PIN_PATH, out, sizeof(out)),\n+\t\t \"bpftool\"))\n+\t\tgoto out;\n+\tASSERT_HAS_SUBSTR(out, \"CNT\", \"var\");\n+\tASSERT_NULL(strstr(out, \"WHOLE.1\"), \"piece\");\n+out:\n+\tif (f)\n+\t\tfclose(f);\n+\tunlink(PIN_PATH);\n+\tif (map_fd \u003e= 0)\n+\t\tclose(map_fd);\n+\tbtf__free(btf);\n+}\n+\n+/* Serial: programs are not in kallsyms while another test sets bpf_jit_harden */\n+void serial_test_btf_rust(void)\n+{\n+\tif (test__start_subtest(\"func_name\"))\n+\t\ttest_func_name();\n+\tif (test__start_subtest(\"piece\"))\n+\t\ttest_piece();\n+}\ndiff --git a/tools/testing/selftests/bpf/prog_tests/data_in_arena.c b/tools/testing/selftests/bpf/prog_tests/data_in_arena.c\nnew file mode 100644\nindex 0000000000000..cb1023507c017\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/prog_tests/data_in_arena.c\n@@ -0,0 +1,216 @@\n+// SPDX-License-Identifier: GPL-2.0\n+\n+#include \u003ctest_progs.h\u003e\n+#include \"data_in_arena.skel.h\"\n+#include \"data_in_arena_decl.skel.h\"\n+#include \"data_in_arena_fail.skel.h\"\n+\n+static int run_prog(struct bpf_program *prog)\n+{\n+\tLIBBPF_OPTS(bpf_test_run_opts, topts);\n+\n+\tif (!ASSERT_OK(bpf_prog_test_run_opts(bpf_program__fd(prog), \u0026topts), \"test_run\"))\n+\t\treturn -1;\n+\treturn topts.retval;\n+}\n+\n+static void run(struct data_in_arena *skel, int counter)\n+{\n+\tint i;\n+\n+\tASSERT_EQ(run_prog(skel-\u003eprogs.use_data), counter + 7 + 1 + 2, \"retval\");\n+\tASSERT_EQ(skel-\u003ebss-\u003esum, counter + 7 + 1 + 2, \"sum\");\n+\tASSERT_EQ(skel-\u003edata-\u003ecounter, counter + 1, \"counter\");\n+\tASSERT_EQ(skel-\u003edata-\u003epair[1], 5, \"pair[1]\");\n+\tfor (i = 0; i \u003c 4; i++)\n+\t\tASSERT_EQ(skel-\u003ebss-\u003etable[i], 10 * (i + 1) + i, \"table\");\n+}\n+\n+static void test_in_arena(void)\n+{\n+\tstruct bpf_map_info info = {};\n+\tstruct data_in_arena *skel;\n+\t__u32 len = sizeof(info);\n+\tstruct bpf_map *arena;\n+\tsize_t sz;\n+\n+\tskel = data_in_arena__open();\n+\tif (!ASSERT_OK_PTR(skel, \"open\"))\n+\t\treturn;\n+\n+\tarena = bpf_object__find_map_by_name(skel-\u003eobj, \"arena\");\n+\tif (!ASSERT_OK_PTR(arena, \"arena\"))\n+\t\tgoto out;\n+\tASSERT_EQ(bpf_map__type(arena), BPF_MAP_TYPE_ARENA, \"arena type\");\n+\tASSERT_EQ(bpf_map__max_entries(arena), 1, \"arena pages\");\n+\tASSERT_OK(bpf_map__set_max_entries(arena, 8), \"arena resize\");\n+\tASSERT_FALSE(bpf_map__autocreate(skel-\u003emaps.data), \"data autocreate\");\n+\tASSERT_FALSE(bpf_map__autocreate(skel-\u003emaps.bss), \"bss autocreate\");\n+\tASSERT_FALSE(bpf_map__autocreate(skel-\u003emaps.rodata), \"rodata autocreate\");\n+\tASSERT_EQ(bpf_map__set_autocreate(skel-\u003emaps.data, true), -EOPNOTSUPP, \"set_autocreate\");\n+\tASSERT_EQ(bpf_map__set_value_size(skel-\u003emaps.bss, 4096), -EOPNOTSUPP, \"set_value_size\");\n+\tASSERT_EQ(bpf_map__initial_value(skel-\u003emaps.data, \u0026sz), skel-\u003edata, \"initial_value\");\n+\tASSERT_EQ(sz, sizeof(*skel-\u003edata), \"initial_value size\");\n+\n+\t/* initial values are set the usual way */\n+\tskel-\u003edata-\u003ecounter = 100;\n+\n+\tif (!ASSERT_OK(data_in_arena__load(skel), \"load\"))\n+\t\tgoto out;\n+\t/* there are no maps behind the sections */\n+\tASSERT_ERR(bpf_map_get_info_by_fd(bpf_map__fd(skel-\u003emaps.data), \u0026info, \u0026len), \"data map\");\n+\tASSERT_ERR(bpf_map_get_info_by_fd(bpf_map__fd(skel-\u003emaps.bss), \u0026info, \u0026len), \"bss map\");\n+\tASSERT_ERR(bpf_map_get_info_by_fd(bpf_map__fd(skel-\u003emaps.rodata), \u0026info, \u0026len),\n+\t\t \"rodata map\");\n+\trun(skel, 100);\n+\n+\t/* pointers to data next to pointers to functions */\n+\tASSERT_EQ(run_prog(skel-\u003eprogs.use_ops), 42 + 1 + 'e', \"use_ops\");\n+\tASSERT_EQ(skel-\u003edata-\u003ecounter, 102, \"counter\");\n+\n+\t/* alignment of sections and pointers to data in data */\n+\tASSERT_EQ((unsigned long)\u0026skel-\u003ebss-\u003ealigned64 % 64, 0, \"alignment\");\n+\tASSERT_EQ(run_prog(skel-\u003eprogs.use_ptrs), 0, \"use_ptrs\");\n+\tASSERT_EQ(skel-\u003edata-\u003ex, 43, \"x\");\n+\tASSERT_EQ(skel-\u003ebss-\u003ealigned64.v[7], 7, \"aligned64\");\n+\tASSERT_EQ(skel-\u003edata-\u003epx, \u0026skel-\u003edata-\u003ex, \"px\");\n+\n+\t/* format strings of bpf_printk() and BPF_SNPRINTF() */\n+\tASSERT_EQ(run_prog(skel-\u003eprogs.use_printk), sizeof(\"43-7\"), \"use_printk\");\n+\tASSERT_STREQ(skel-\u003ebss-\u003eout, \"43-7\", \"out\");\n+out:\n+\tdata_in_arena__destroy(skel);\n+}\n+\n+/* The object has an arena map and __arena variables */\n+static void test_declared_arena(void)\n+{\n+\tstruct data_in_arena_decl *skel;\n+\tstruct bpf_map *map;\n+\tint arenas = 0;\n+\n+\tskel = data_in_arena_decl__open();\n+\tif (!ASSERT_OK_PTR(skel, \"open\"))\n+\t\treturn;\n+\tbpf_object__for_each_map(map, skel-\u003eobj)\n+\t\tarenas += bpf_map__type(map) == BPF_MAP_TYPE_ARENA;\n+\tASSERT_EQ(arenas, 1, \"no second arena\");\n+\tskel-\u003edata-\u003ecounter = 6;\n+\tif (!ASSERT_OK(data_in_arena_decl__load(skel), \"load\"))\n+\t\tgoto out;\n+\tASSERT_EQ(run_prog(skel-\u003eprogs.use_data), 6 + 7 + 11, \"retval\");\n+\tASSERT_EQ(run_prog(skel-\u003eprogs.use_data), 7 + 7 + 12, \"retval\");\n+\tASSERT_EQ(skel-\u003ebss-\u003esum, 7 + 7 + 12, \"sum\");\n+\tASSERT_EQ(skel-\u003edata-\u003ecounter, 8, \"counter\");\n+out:\n+\tdata_in_arena_decl__destroy(skel);\n+}\n+\n+/* A pointer in data that can't be made an address of arena fails the load */\n+static void test_ptr_to_map(void)\n+{\n+\tstruct data_in_arena_fail *skel;\n+\n+\tskel = data_in_arena_fail__open();\n+\tif (!ASSERT_OK_PTR(skel, \"open\"))\n+\t\treturn;\n+\tASSERT_ERR(data_in_arena_fail__load(skel), \"load\");\n+\tdata_in_arena_fail__destroy(skel);\n+}\n+\n+/* So does a pointer to a variable of the kernel. There is no skeleton: the open fails. */\n+static void test_ptr_to_extern(void)\n+{\n+\tstruct bpf_object *obj;\n+\n+\tobj = bpf_object__open_file(\"./data_in_arena_extern.bpf.o\", NULL);\n+\tif (!ASSERT_ERR_PTR(obj, \"open\"))\n+\t\tbpf_object__close(obj);\n+}\n+\n+/* __arena variables and no arena map. There is no skeleton: the open fails. */\n+static void test_arena_var_no_map(void)\n+{\n+\tstruct bpf_object *obj;\n+\n+\tobj = bpf_object__open_file(\"./data_in_arena_nomap.bpf.o\", NULL);\n+\tif (!ASSERT_ERR_PTR(obj, \"open\"))\n+\t\tbpf_object__close(obj);\n+}\n+\n+/* The address of an arena that libbpf doesn't create is not known. No pointers in data then. */\n+static void test_not_my_arena(bool pin)\n+{\n+\tLIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_MMAPABLE);\n+\tstruct data_in_arena *skel;\n+\tstruct bpf_map *arena;\n+\tint fd = -1;\n+\n+\tskel = data_in_arena__open();\n+\tif (!ASSERT_OK_PTR(skel, \"open\"))\n+\t\treturn;\n+\tarena = bpf_object__find_map_by_name(skel-\u003eobj, \"arena\");\n+\tif (!ASSERT_OK_PTR(arena, \"arena\"))\n+\t\tgoto out;\n+\tif (pin) {\n+\t\tASSERT_OK(bpf_map__set_pin_path(arena, \"/sys/fs/bpf/data_in_arena\"), \"pin_path\");\n+\t} else {\n+\t\tfd = bpf_map_create(BPF_MAP_TYPE_ARENA, \"arena\", 0, 0, 1, \u0026opts);\n+\t\tif (!ASSERT_GE(fd, 0, \"map_create\"))\n+\t\t\tgoto out;\n+\t\tASSERT_OK(bpf_map__reuse_fd(arena, fd), \"reuse_fd\");\n+\t}\n+\tASSERT_EQ(data_in_arena__load(skel), -ENOTSUP, \"load\");\n+out:\n+\tif (fd \u003e= 0)\n+\t\tclose(fd);\n+\tdata_in_arena__destroy(skel);\n+}\n+\n+/* The object is there if rustc and clang can build it, see Makefile.buildvars */\n+static void test_rust(void)\n+{\n+\tconst char *file = \"./data_in_arena_rust.bpf.o\";\n+\tstruct bpf_object *obj;\n+\n+\tif (access(file, R_OK)) {\n+\t\ttest__skip();\n+\t\treturn;\n+\t}\n+\tobj = bpf_object__open_file(file, NULL);\n+\tif (!ASSERT_OK_PTR(obj, \"open\"))\n+\t\treturn;\n+\tif (!ASSERT_OK(bpf_object__load(obj), \"load\"))\n+\t\tgoto out;\n+\t/* libbpf goes on without BTF when the kernel doesn't take it */\n+\tASSERT_GE(bpf_object__btf_fd(obj), 0, \"btf_fd\");\n+\tASSERT_EQ(run_prog(bpf_object__find_program_by_name(obj, \"list_in_data\")),\n+\t\t 100 + 20 + 3, \"retval\");\n+out:\n+\tbpf_object__close(obj);\n+}\n+\n+void test_data_in_arena(void)\n+{\n+#if !defined(__x86_64__) \u0026\u0026 !defined(__aarch64__)\n+\t/* other JITs don't take BPF_F_ARENA_SCALAR */\n+\ttest__skip();\n+\treturn;\n+#endif\n+\tif (test__start_subtest(\"arena\"))\n+\t\ttest_in_arena();\n+\tif (test__start_subtest(\"declared_arena\"))\n+\t\ttest_declared_arena();\n+\tif (test__start_subtest(\"ptr_to_map\"))\n+\t\ttest_ptr_to_map();\n+\tif (test__start_subtest(\"ptr_to_extern\"))\n+\t\ttest_ptr_to_extern();\n+\tif (test__start_subtest(\"arena_var_no_map\"))\n+\t\ttest_arena_var_no_map();\n+\tif (test__start_subtest(\"pinned_arena\"))\n+\t\ttest_not_my_arena(true);\n+\tif (test__start_subtest(\"reused_arena\"))\n+\t\ttest_not_my_arena(false);\n+\tif (test__start_subtest(\"rust\"))\n+\t\ttest_rust();\n+}\ndiff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c\nindex 8a6d341b754ac..460ad10ddc020 100644\n--- a/tools/testing/selftests/bpf/prog_tests/verifier.c\n+++ b/tools/testing/selftests/bpf/prog_tests/verifier.c\n@@ -12,6 +12,7 @@\n #include \"verifier_and.skel.h\"\n #include \"verifier_arena.skel.h\"\n #include \"verifier_arena_large.skel.h\"\n+#include \"verifier_arena_scalar.skel.h\"\n #include \"verifier_arena_globals1.skel.h\"\n #include \"verifier_arena_globals2.skel.h\"\n #include \"verifier_array_access.skel.h\"\n@@ -198,6 +199,7 @@ void test_verifier_align(void) { RUN(verifier_align); }\n void test_verifier_and(void) { RUN(verifier_and); }\n void test_verifier_arena(void) { RUN(verifier_arena); }\n void test_verifier_arena_large(void) { RUN(verifier_arena_large); }\n+void test_verifier_arena_scalar(void) { RUN(verifier_arena_scalar); }\n void test_verifier_arena_globals1(void) { RUN(verifier_arena_globals1); }\n void test_verifier_arena_globals2(void) { RUN(verifier_arena_globals2); }\n void test_verifier_basic_stack(void) { RUN(verifier_basic_stack); }\ndiff --git a/tools/testing/selftests/bpf/progs/data_in_arena.c b/tools/testing/selftests/bpf/progs/data_in_arena.c\nnew file mode 100644\nindex 0000000000000..ce3838327d6f4\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/data_in_arena.c\n@@ -0,0 +1,107 @@\n+// SPDX-License-Identifier: GPL-2.0\n+\n+#include \u003clinux/bpf.h\u003e\n+#include \u003cbpf/bpf_helpers.h\u003e\n+\n+/* global data of the object is in arena */\n+char data_in_arena SEC(\".arena.data\");\n+\n+int counter = 5;\n+long pair[2] = { 1, 2 };\n+int x = 42;\n+long sum;\n+long table[4];\n+struct {\n+\tlong v[8];\n+} aligned64 __attribute__((aligned(64)));\n+const volatile int ro = 7;\n+const volatile long ro_table[4] = { 10, 20, 30, 40 };\n+\n+/* const strings stay in a map for helpers and kfuncs, a copy of them is in arena */\n+const char hello[] SEC(\".rodata.str.hello\") = \"hello\";\n+\n+/* pointers to data that are stored in data */\n+int *px = \u0026x;\n+const char *str = hello;\n+int *const volatile cpx SEC(\".data.rel.ro\") = \u0026x;\n+\n+SEC(\"syscall\")\n+int use_data(void *ctx)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i \u003c 4; i++)\n+\t\ttable[i] = ro_table[i] + i;\n+\tsum = counter + ro + pair[0] + pair[1];\n+\tcounter++;\n+\t__sync_fetch_and_add(\u0026pair[1], 3);\n+\treturn sum;\n+}\n+\n+typedef int (*op_fn)(int);\n+\n+static __noinline int add1(int v)\n+{\n+\treturn v + 1;\n+}\n+\n+/*\n+ * Pointers to functions and to data in read-only data of a program with callx.\n+ * Volatile, so that the compiler doesn't replace the pointers with what\n+ * they point to.\n+ */\n+static const volatile struct {\n+\top_fn fn;\n+\tint *data;\n+\tconst char *name;\n+} ops SEC(\".data.rel.ro\") = { add1, \u0026x, hello };\n+\n+SEC(\"syscall\")\n+int use_ops(void *ctx)\n+{\n+\t/* a program has the arena when its code refers to it */\n+\tcounter++;\n+\treturn ops.fn(*ops.data) + ops.name[1];\n+}\n+\n+SEC(\"syscall\")\n+int use_ptrs(void *ctx)\n+{\n+\tunsigned long addr = (unsigned long)\u0026aligned64;\n+\tchar local[4] = \"abc\";\n+\n+\t/* hide the address from the compiler, it knows that '\u0026 63' is 0 */\n+\tasm volatile (\"\" : \"+r\"(addr));\n+\tif (addr \u0026 63)\n+\t\treturn 1;\n+\taligned64.v[7] = 7;\n+\tif (*px != 42)\n+\t\treturn 2;\n+\t*px = 43;\n+\tif (x != 43 || *cpx != 43)\n+\t\treturn 3;\n+\tif (str[0] != 'h' || str[4] != 'o' || str[5])\n+\t\treturn 4;\n+\t/* the literal is in a map */\n+\tif (bpf_strncmp(local, sizeof(local), \"abc\"))\n+\t\treturn 5;\n+\treturn 0;\n+}\n+\n+char out[16];\n+\n+/* format strings are in a map */\n+SEC(\"syscall\")\n+int use_printk(void *ctx)\n+{\n+\tchar buf[sizeof(out)];\n+\tint i, n;\n+\n+\tbpf_printk(\"counter %d\", counter);\n+\tn = BPF_SNPRINTF(buf, sizeof(buf), \"%d-%d\", x, ro);\n+\tfor (i = 0; i \u003c sizeof(out); i++)\n+\t\tout[i] = buf[i];\n+\treturn n;\n+}\n+\n+char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/progs/data_in_arena_decl.c b/tools/testing/selftests/bpf/progs/data_in_arena_decl.c\nnew file mode 100644\nindex 0000000000000..01bc4fea8f0c0\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/data_in_arena_decl.c\n@@ -0,0 +1,37 @@\n+// SPDX-License-Identifier: GPL-2.0\n+\n+#define BPF_NO_KFUNC_PROTOTYPES\n+#include \u003cvmlinux.h\u003e\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \"bpf_experimental.h\"\n+#include \u003cbpf_arena_common.h\u003e\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_ARENA);\n+\t__uint(map_flags, BPF_F_MMAPABLE);\n+\t__uint(max_entries, 4);\n+} arena SEC(\".maps\");\n+\n+/* global data of the object is in arena */\n+char data_in_arena SEC(\".arena.data\");\n+\n+int counter = 5;\n+long sum;\n+const volatile int ro = 7;\n+\n+#if defined(__BPF_FEATURE_ADDR_SPACE_CAST)\n+int __arena avar = 11;\n+#else\n+int avar = 11;\n+#endif\n+\n+SEC(\"syscall\")\n+int use_data(void *ctx)\n+{\n+\tsum = counter + ro + avar;\n+\tcounter++;\n+\tavar++;\n+\treturn sum;\n+}\n+\n+char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/progs/data_in_arena_extern.c b/tools/testing/selftests/bpf/progs/data_in_arena_extern.c\nnew file mode 100644\nindex 0000000000000..dc1ad5ca3bdd0\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/data_in_arena_extern.c\n@@ -0,0 +1,20 @@\n+// SPDX-License-Identifier: GPL-2.0\n+\n+#include \u003clinux/bpf.h\u003e\n+#include \u003cbpf/bpf_helpers.h\u003e\n+\n+/* global data of the object is in arena */\n+char data_in_arena SEC(\".arena.data\");\n+\n+extern const int bpf_prog_active __ksym;\n+\n+/* the variable of the kernel is not in arena */\n+const void *kp = \u0026bpf_prog_active;\n+\n+SEC(\"syscall\")\n+int ptr_to_extern(void *ctx)\n+{\n+\treturn *(int *)kp;\n+}\n+\n+char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/progs/data_in_arena_fail.c b/tools/testing/selftests/bpf/progs/data_in_arena_fail.c\nnew file mode 100644\nindex 0000000000000..ad8afe9afc962\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/data_in_arena_fail.c\n@@ -0,0 +1,20 @@\n+// SPDX-License-Identifier: GPL-2.0\n+\n+#include \u003clinux/bpf.h\u003e\n+#include \u003cbpf/bpf_helpers.h\u003e\n+\n+/* global data of the object is in arena */\n+char data_in_arena SEC(\".arena.data\");\n+\n+int x = 42;\n+/* the table is read-only data with pointers. It's not in arena. */\n+int *const volatile tbl[1] SEC(\".data.rel.ro\") = { \u0026x };\n+int *const volatile *pp = tbl;\n+\n+SEC(\"syscall\")\n+int ptr_to_map(void *ctx)\n+{\n+\treturn **pp;\n+}\n+\n+char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/progs/data_in_arena_nomap.c b/tools/testing/selftests/bpf/progs/data_in_arena_nomap.c\nnew file mode 100644\nindex 0000000000000..da27a8f10d6fa\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/data_in_arena_nomap.c\n@@ -0,0 +1,20 @@\n+// SPDX-License-Identifier: GPL-2.0\n+\n+#include \u003cvmlinux.h\u003e\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \"bpf_arena_common.h\"\n+\n+/* global data of the object is in arena */\n+char data_in_arena SEC(\".arena.data\");\n+\n+int counter = 5;\n+/* needs an arena map that is declared. The one that libbpf creates won't do. */\n+int __arena avar = 11;\n+\n+SEC(\"syscall\")\n+int arena_var(void *ctx)\n+{\n+\treturn avar + counter;\n+}\n+\n+char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/progs/data_in_arena_rust.rs b/tools/testing/selftests/bpf/progs/data_in_arena_rust.rs\nnew file mode 100644\nindex 0000000000000..b69128487a0fc\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/data_in_arena_rust.rs\n@@ -0,0 +1,70 @@\n+// SPDX-License-Identifier: GPL-2.0\n+\n+// Why .data, .bss and .rodata of a program in Rust are in arena.\n+//\n+// A reference in Rust is an address. It doesn't say what it points to and it\n+// can be stored in data. The list below has a node in each of the sections.\n+// The nodes are linked by references that are in the data:\n+// IN_BSS.next is stored by the program,\n+// IN_DATA.next is a relocation in .data against .rodata.\n+// sum() loads the references back and reads the three nodes with the same insn.\n+//\n+// When the sections are array maps libbpf skips the relocation in .data, and\n+// what sum() loads from a node is a number that can't be dereferenced:\n+// R1 invalid mem access 'scalar'\n+// In arena the address of a node is a number to begin with.\n+\n+#![no_std]\n+#![no_main]\n+\n+// Tell libbpf to keep .data, .bss and .rodata in arena.\n+#[used]\n+#[link_section = \".arena.data\"]\n+static DATA_IN_ARENA: u8 = 0;\n+\n+#[used]\n+#[link_section = \"license\"]\n+static LICENSE: [u8; 4] = *b\"GPL\\0\";\n+\n+// panic=abort is a stop gap until panic=unwind is supported.\n+// Nothing here panics, so the handler is not a part of the program.\n+#[panic_handler]\n+fn panic(_info: \u0026core::panic::PanicInfo) -\u003e ! {\n+ loop {}\n+}\n+\n+pub struct Node {\n+ val: u32,\n+ next: Option\u003c\u0026'static Node\u003e,\n+}\n+\n+// no_mangle makes them visible outside, so LLVM can't fold the list into a constant.\n+#[no_mangle]\n+static IN_RODATA: Node = Node { val: 3, next: None };\n+#[no_mangle]\n+static mut IN_DATA: Node = Node { val: 20, next: Some(\u0026IN_RODATA) };\n+#[no_mangle]\n+static mut IN_BSS: Node = Node { val: 0, next: None };\n+\n+#[inline(never)]\n+fn sum(mut node: Option\u003c\u0026Node\u003e) -\u003e u32 {\n+ let mut sum = 0;\n+ // The verifier wants a bound.\n+ for _ in 0..8 {\n+ let Some(n) = node else { break };\n+ sum += n.val;\n+ node = n.next;\n+ }\n+ sum\n+}\n+\n+#[no_mangle]\n+#[link_section = \"syscall\"]\n+pub extern \"C\" fn list_in_data(_ctx: *mut u8) -\u003e u32 {\n+ unsafe {\n+ let head = \u0026mut *\u0026raw mut IN_BSS;\n+ head.val = 100;\n+ head.next = Some(\u0026*\u0026raw const IN_DATA);\n+ sum(Some(head))\n+ }\n+}\ndiff --git a/tools/testing/selftests/bpf/progs/verifier_arena_scalar.c b/tools/testing/selftests/bpf/progs/verifier_arena_scalar.c\nnew file mode 100644\nindex 0000000000000..bebc37f501b7e\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/verifier_arena_scalar.c\n@@ -0,0 +1,912 @@\n+// SPDX-License-Identifier: GPL-2.0\n+\n+#include \u003clinux/bpf.h\u003e\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \"../../../include/linux/filter.h\"\n+#include \"bpf_misc.h\"\n+\n+void *bpf_arena_alloc_pages(void *map, void *addr, __u32 page_cnt, int node_id,\n+\t\t\t __u64 flags) __ksym;\n+\n+#ifdef __TARGET_ARCH_arm64\n+#define ARENA_VM_START (1ull \u003c\u003c 32)\n+#else\n+#define ARENA_VM_START (1ull \u003c\u003c 44)\n+#endif\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_ARENA);\n+\t__uint(map_flags, BPF_F_MMAPABLE);\n+\t__uint(max_entries, 4);\n+\t__ulong(map_extra, ARENA_VM_START);\n+} arena SEC(\".maps\");\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_HASH);\n+\t__uint(max_entries, 1);\n+\t__type(key, int);\n+\t__type(value, long long);\n+} hash SEC(\".maps\");\n+\n+/* JITs that take BPF_F_ARENA_SCALAR */\n+#define __arena_scalar __flag(BPF_F_ARENA_SCALAR) __arch_x86_64 __arch_arm64\n+\n+/* BTF FUNC records are not generated for kfuncs referenced from inline assembly */\n+void __kfunc_btf_root(void)\n+{\n+\tbpf_arena_alloc_pages(0, 0, 0, 0, 0);\n+}\n+\n+/* Tests start with r6 = address of a new page as the user space sees it, a number */\n+\n+SEC(\"syscall\")\n+__arena_scalar\n+__description(\"arena_scalar: load and store of every size\")\n+__success __retval(0)\n+__load_if_JITed()\n+__naked void ld_st_sizes(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[arena] ll;\t\t\t\t\\\n+\tr2 = 0;\t\t\t\t\t\t\\\n+\tr3 = 1;\t\t\t\t\t\t\\\n+\tr4 = -1;\t\t\t\t\t\\\n+\tr5 = 0;\t\t\t\t\t\t\\\n+\tcall %[bpf_arena_alloc_pages];\t\t\t\\\n+\tr6 = r0;\t\t\t\t\t\\\n+\tr0 = 100;\t\t\t\t\t\\\n+\tif r6 == 0 goto 9f;\t\t\t\t\\\n+\tr7 = r6;\t\t\t\t\t\\\n+\tr1 = 0x1122334455667788 ll;\t\t\t\\\n+\t*(u64 *)(r6 + 0) = r1;\t\t\t\t\\\n+\t*(u32 *)(r6 + 8) = r1;\t\t\t\t\\\n+\t*(u16 *)(r6 + 12) = r1;\t\t\t\t\\\n+\t*(u8 *)(r6 + 14) = r1;\t\t\t\t\\\n+\t*(u64 *)(r6 + 16) = 0x1234;\t\t\t\\\n+\t*(u32 *)(r6 + 24) = 0x5678;\t\t\t\\\n+\t*(u16 *)(r6 + 28) = 0x9a;\t\t\t\\\n+\t*(u8 *)(r6 + 30) = 0xbc;\t\t\t\\\n+\tr0 = 1;\t\t\t\t\t\t\\\n+\tr2 = *(u64 *)(r6 + 0);\t\t\t\t\\\n+\tif r2 != r1 goto 9f;\t\t\t\t\\\n+\tr0 = 2;\t\t\t\t\t\t\\\n+\tr2 = *(u32 *)(r6 + 8);\t\t\t\t\\\n+\tif r2 != 0x55667788 goto 9f;\t\t\t\\\n+\tr0 = 3;\t\t\t\t\t\t\\\n+\tr2 = *(u16 *)(r6 + 12);\t\t\t\t\\\n+\tif r2 != 0x7788 goto 9f;\t\t\t\\\n+\tr0 = 4;\t\t\t\t\t\t\\\n+\tr2 = *(u8 *)(r6 + 14);\t\t\t\t\\\n+\tif r2 != 0x88 goto 9f;\t\t\t\t\\\n+\tr0 = 5;\t\t\t\t\t\t\\\n+\tr2 = *(u64 *)(r6 + 16);\t\t\t\t\\\n+\tif r2 != 0x1234 goto 9f;\t\t\t\\\n+\tr0 = 6;\t\t\t\t\t\t\\\n+\tr2 = *(u32 *)(r6 + 24);\t\t\t\t\\\n+\tif r2 != 0x5678 goto 9f;\t\t\t\\\n+\tr0 = 7;\t\t\t\t\t\t\\\n+\tr2 = *(u16 *)(r6 + 28);\t\t\t\t\\\n+\tif r2 != 0x9a goto 9f;\t\t\t\t\\\n+\tr0 = 8;\t\t\t\t\t\t\\\n+\tr2 = *(u8 *)(r6 + 30);\t\t\t\t\\\n+\tif r2 != 0xbc goto 9f;\t\t\t\t\\\n+\t/* the address is what it was */\t\t\\\n+\tr0 = 9;\t\t\t\t\t\t\\\n+\tif r6 != r7 goto 9f;\t\t\t\t\\\n+\tr0 = 10;\t\t\t\t\t\\\n+\tr7 \u003e\u003e= 32;\t\t\t\t\t\\\n+\tif r7 == 0 goto 9f;\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+9:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(arena),\n+\t __imm(bpf_arena_alloc_pages)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+__arena_scalar\n+__description(\"arena_scalar: load into the register that holds the address\")\n+__success __retval(0)\n+__load_if_JITed()\n+__naked void ld_into_base(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[arena] ll;\t\t\t\t\\\n+\tr2 = 0;\t\t\t\t\t\t\\\n+\tr3 = 1;\t\t\t\t\t\t\\\n+\tr4 = -1;\t\t\t\t\t\\\n+\tr5 = 0;\t\t\t\t\t\t\\\n+\tcall %[bpf_arena_alloc_pages];\t\t\t\\\n+\tr6 = r0;\t\t\t\t\t\\\n+\tr0 = 100;\t\t\t\t\t\\\n+\tif r6 == 0 goto 9f;\t\t\t\t\\\n+\tr1 = 77;\t\t\t\t\t\\\n+\t*(u64 *)(r6 + 0) = r1;\t\t\t\t\\\n+\tr6 = *(u64 *)(r6 + 0);\t\t\t\t\\\n+\tr0 = 1;\t\t\t\t\t\t\\\n+\tif r6 != 77 goto 9f;\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+9:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(arena),\n+\t __imm(bpf_arena_alloc_pages)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+__arena_scalar\n+__description(\"arena_scalar: sign extending load\")\n+__success __retval(0)\n+__load_if_JITed()\n+__naked void ldsx(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[arena] ll;\t\t\t\t\\\n+\tr2 = 0;\t\t\t\t\t\t\\\n+\tr3 = 1;\t\t\t\t\t\t\\\n+\tr4 = -1;\t\t\t\t\t\\\n+\tr5 = 0;\t\t\t\t\t\t\\\n+\tcall %[bpf_arena_alloc_pages];\t\t\t\\\n+\tr6 = r0;\t\t\t\t\t\\\n+\tr0 = 100;\t\t\t\t\t\\\n+\tif r6 == 0 goto 9f;\t\t\t\t\\\n+\tr7 = r6;\t\t\t\t\t\\\n+\t*(u64 *)(r6 + 0) = 0x80;\t\t\t\\\n+\t.8byte %[ldsx_insn]; /* r2 = *(s8 *)(r6 + 0) */\t\\\n+\tr0 = 1;\t\t\t\t\t\t\\\n+\tif r2 != -128 goto 9f;\t\t\t\t\\\n+\tr0 = 2;\t\t\t\t\t\t\\\n+\tif r6 != r7 goto 9f;\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+9:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(arena),\n+\t __imm(bpf_arena_alloc_pages),\n+\t __imm_insn(ldsx_insn, BPF_RAW_INSN(BPF_LDX | BPF_MEMSX | BPF_B,\n+\t\t\t\t\t BPF_REG_2, BPF_REG_6, 0, 0))\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+__arena_scalar\n+__description(\"arena_scalar: address loaded from arena\")\n+__success __retval(0)\n+__load_if_JITed()\n+__naked void ptr_chase(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[arena] ll;\t\t\t\t\\\n+\tr2 = 0;\t\t\t\t\t\t\\\n+\tr3 = 1;\t\t\t\t\t\t\\\n+\tr4 = -1;\t\t\t\t\t\\\n+\tr5 = 0;\t\t\t\t\t\t\\\n+\tcall %[bpf_arena_alloc_pages];\t\t\t\\\n+\tr6 = r0;\t\t\t\t\t\\\n+\tr0 = 100;\t\t\t\t\t\\\n+\tif r6 == 0 goto 9f;\t\t\t\t\\\n+\t/* page[0] = \u0026page[64]; page[64] = 5; */\t\\\n+\tr1 = r6;\t\t\t\t\t\\\n+\tr1 += 64;\t\t\t\t\t\\\n+\t*(u64 *)(r6 + 0) = r1;\t\t\t\t\\\n+\t*(u64 *)(r1 + 0) = 5;\t\t\t\t\\\n+\tr2 = *(u64 *)(r6 + 0);\t\t\t\t\\\n+\tr0 = 1;\t\t\t\t\t\t\\\n+\tif r2 != r1 goto 9f;\t\t\t\t\\\n+\tr3 = *(u64 *)(r2 + 0);\t\t\t\t\\\n+\tr0 = 2;\t\t\t\t\t\t\\\n+\tif r3 != 5 goto 9f;\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+9:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(arena),\n+\t __imm(bpf_arena_alloc_pages)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+__arena_scalar\n+__description(\"arena_scalar: atomics\")\n+__success __retval(0)\n+__load_if_JITed()\n+__naked void atomics(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[arena] ll;\t\t\t\t\\\n+\tr2 = 0;\t\t\t\t\t\t\\\n+\tr3 = 1;\t\t\t\t\t\t\\\n+\tr4 = -1;\t\t\t\t\t\\\n+\tr5 = 0;\t\t\t\t\t\t\\\n+\tcall %[bpf_arena_alloc_pages];\t\t\t\\\n+\tr6 = r0;\t\t\t\t\t\\\n+\tr0 = 100;\t\t\t\t\t\\\n+\tif r6 == 0 goto 9f;\t\t\t\t\\\n+\tr7 = r6;\t\t\t\t\t\\\n+\t*(u64 *)(r6 + 8) = 1;\t\t\t\t\\\n+\tr1 = 2;\t\t\t\t\t\t\\\n+\tlock *(u64 *)(r6 + 8) += r1;\t\t\t\\\n+\tr1 = 4;\t\t\t\t\t\t\\\n+\t.8byte %[fetch_add_insn]; /* r1 = atomic_fetch_add((u64 *)(r6 + 8), r1) */ \\\n+\tr0 = 1;\t\t\t\t\t\t\\\n+\tif r1 != 3 goto 9f;\t\t\t\t\\\n+\tr1 = 8;\t\t\t\t\t\t\\\n+\t.8byte %[xchg_insn]; /* r1 = xchg_64(r6 + 8, r1) */ \\\n+\tr0 = 2;\t\t\t\t\t\t\\\n+\tif r1 != 7 goto 9f;\t\t\t\t\\\n+\tr0 = 8;\t\t\t\t\t\t\\\n+\tr1 = 16;\t\t\t\t\t\\\n+\t.8byte %[cmpxchg_insn]; /* r0 = cmpxchg_64(r6 + 8, r0, r1) */ \\\n+\tr2 = r0;\t\t\t\t\t\\\n+\tr0 = 3;\t\t\t\t\t\t\\\n+\tif r2 != 8 goto 9f;\t\t\t\t\\\n+\tr2 = *(u64 *)(r6 + 8);\t\t\t\t\\\n+\tr0 = 4;\t\t\t\t\t\t\\\n+\tif r2 != 16 goto 9f;\t\t\t\t\\\n+\tr0 = 5;\t\t\t\t\t\t\\\n+\tif r6 != r7 goto 9f;\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+9:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(arena),\n+\t __imm(bpf_arena_alloc_pages),\n+\t __imm_insn(fetch_add_insn, BPF_ATOMIC_OP(BPF_DW, BPF_ADD | BPF_FETCH,\n+\t\t\t\t\t\t BPF_REG_6, BPF_REG_1, 8)),\n+\t __imm_insn(xchg_insn, BPF_ATOMIC_OP(BPF_DW, BPF_XCHG, BPF_REG_6, BPF_REG_1, 8)),\n+\t __imm_insn(cmpxchg_insn, BPF_ATOMIC_OP(BPF_DW, BPF_CMPXCHG, BPF_REG_6, BPF_REG_1, 8))\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+__arena_scalar\n+__description(\"arena_scalar: cmpxchg through r0\")\n+__success __retval(0)\n+__load_if_JITed()\n+__naked void cmpxchg_r0(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[arena] ll;\t\t\t\t\\\n+\tr2 = 0;\t\t\t\t\t\t\\\n+\tr3 = 1;\t\t\t\t\t\t\\\n+\tr4 = -1;\t\t\t\t\t\\\n+\tr5 = 0;\t\t\t\t\t\t\\\n+\tcall %[bpf_arena_alloc_pages];\t\t\t\\\n+\tr6 = r0;\t\t\t\t\t\\\n+\tr0 = 100;\t\t\t\t\t\\\n+\tif r6 == 0 goto 9f;\t\t\t\t\\\n+\t/* The address is in r0. The value at the address is not equal to it. */ \\\n+\t*(u64 *)(r6 + 0) = 3;\t\t\t\t\\\n+\tr0 = r6;\t\t\t\t\t\\\n+\tr1 = 5;\t\t\t\t\t\t\\\n+\t.8byte %[cmpxchg_insn]; /* r0 = cmpxchg_64(r0 + 0, r0, r1) */ \\\n+\tr2 = r0;\t\t\t\t\t\\\n+\tr0 = 1;\t\t\t\t\t\t\\\n+\tif r2 != 3 goto 9f;\t\t\t\t\\\n+\tr2 = *(u64 *)(r6 + 0);\t\t\t\t\\\n+\tr0 = 2;\t\t\t\t\t\t\\\n+\tif r2 != 3 goto 9f;\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+9:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(arena),\n+\t __imm(bpf_arena_alloc_pages),\n+\t __imm_insn(cmpxchg_insn, BPF_ATOMIC_OP(BPF_DW, BPF_CMPXCHG, BPF_REG_0, BPF_REG_1, 0))\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+__arena_scalar\n+__description(\"arena_scalar: xchg into the register that holds the address\")\n+__success __retval(0)\n+__load_if_JITed()\n+__naked void xchg_into_base(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[arena] ll;\t\t\t\t\\\n+\tr2 = 0;\t\t\t\t\t\t\\\n+\tr3 = 1;\t\t\t\t\t\t\\\n+\tr4 = -1;\t\t\t\t\t\\\n+\tr5 = 0;\t\t\t\t\t\t\\\n+\tcall %[bpf_arena_alloc_pages];\t\t\t\\\n+\tr6 = r0;\t\t\t\t\t\\\n+\tr0 = 100;\t\t\t\t\t\\\n+\tif r6 == 0 goto 9f;\t\t\t\t\\\n+\t*(u64 *)(r6 + 0) = 3;\t\t\t\t\\\n+\tr1 = r6;\t\t\t\t\t\\\n+\t.8byte %[xchg_insn]; /* r1 = xchg_64(r1 + 0, r1) */ \\\n+\tr0 = 1;\t\t\t\t\t\t\\\n+\tif r1 != 3 goto 9f;\t\t\t\t\\\n+\tr2 = *(u64 *)(r6 + 0);\t\t\t\t\\\n+\tr0 = 2;\t\t\t\t\t\t\\\n+\tif r2 != r6 goto 9f;\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+9:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(arena),\n+\t __imm(bpf_arena_alloc_pages),\n+\t __imm_insn(xchg_insn, BPF_ATOMIC_OP(BPF_DW, BPF_XCHG, BPF_REG_1, BPF_REG_1, 0))\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+__arena_scalar\n+__description(\"arena_scalar: xchg into the register that holds a pointer to stack\")\n+__failure __msg(\"misaligned access off (0x0; 0xffffffffffffffff)+0 size 8\")\n+__naked void xchg_into_stack_ptr(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[arena] ll;\t\t\t\t\\\n+\tr1 = 0;\t\t\t\t\t\t\\\n+\t*(u64 *)(r10 - 8) = r1;\t\t\t\t\\\n+\tr1 = r10;\t\t\t\t\t\\\n+\tr1 += -8;\t\t\t\t\t\\\n+\t.8byte %[xchg_insn]; /* r1 = xchg_64(r1 + 0, r1) */ \\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(arena),\n+\t __imm_insn(xchg_insn, BPF_ATOMIC_OP(BPF_DW, BPF_XCHG, BPF_REG_1, BPF_REG_1, 0))\n+\t: __clobber_all);\n+}\n+\n+#ifdef CAN_USE_LOAD_ACQ_STORE_REL\n+\n+SEC(\"syscall\")\n+__arena_scalar\n+__description(\"arena_scalar: load-acquire and store-release\")\n+__success __retval(0)\n+__load_if_JITed()\n+__naked void load_acq_store_rel(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[arena] ll;\t\t\t\t\\\n+\tr2 = 0;\t\t\t\t\t\t\\\n+\tr3 = 1;\t\t\t\t\t\t\\\n+\tr4 = -1;\t\t\t\t\t\\\n+\tr5 = 0;\t\t\t\t\t\t\\\n+\tcall %[bpf_arena_alloc_pages];\t\t\t\\\n+\tr6 = r0;\t\t\t\t\t\\\n+\tr0 = 100;\t\t\t\t\t\\\n+\tif r6 == 0 goto 9f;\t\t\t\t\\\n+\tr7 = r6;\t\t\t\t\t\\\n+\tr1 = 0x1234;\t\t\t\t\t\\\n+\t.8byte %[store_release_insn]; /* store_release((u64 *)(r6 + 8), r1) */ \\\n+\t.8byte %[load_acquire_insn]; /* r2 = load_acquire((u64 *)(r6 + 8)) */ \\\n+\tr0 = 1;\t\t\t\t\t\t\\\n+\tif r2 != 0x1234 goto 9f;\t\t\t\\\n+\t.8byte %[load_acquire8_insn]; /* w2 = load_acquire((u8 *)(r6 + 8)) */ \\\n+\tr0 = 2;\t\t\t\t\t\t\\\n+\tif r2 != 0x34 goto 9f;\t\t\t\t\\\n+\tr0 = 3;\t\t\t\t\t\t\\\n+\tif r6 != r7 goto 9f;\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+9:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(arena),\n+\t __imm(bpf_arena_alloc_pages),\n+\t __imm_insn(store_release_insn,\n+\t\t BPF_ATOMIC_OP(BPF_DW, BPF_STORE_REL, BPF_REG_6, BPF_REG_1, 8)),\n+\t __imm_insn(load_acquire_insn,\n+\t\t BPF_ATOMIC_OP(BPF_DW, BPF_LOAD_ACQ, BPF_REG_2, BPF_REG_6, 8)),\n+\t __imm_insn(load_acquire8_insn,\n+\t\t BPF_ATOMIC_OP(BPF_B, BPF_LOAD_ACQ, BPF_REG_2, BPF_REG_6, 8))\n+\t: __clobber_all);\n+}\n+\n+#endif /* CAN_USE_LOAD_ACQ_STORE_REL */\n+\n+SEC(\"syscall\")\n+__arena_scalar\n+__description(\"arena_scalar: number that is not an address in arena\")\n+__success __retval(0)\n+__load_if_JITed()\n+__naked void not_in_arena(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[arena] ll;\t\t\t\t\\\n+\t/* nothing is allocated: all loads read 0, stores are dropped */ \\\n+\tr6 = 0xdeadbeef00000000 ll;\t\t\t\\\n+\tr0 = 1;\t\t\t\t\t\t\\\n+\tr2 = *(u64 *)(r6 + 0);\t\t\t\t\\\n+\tif r2 != 0 goto 9f;\t\t\t\t\\\n+\tr0 = 2;\t\t\t\t\t\t\\\n+\tr2 = *(u8 *)(r6 - 32768);\t\t\t\\\n+\tif r2 != 0 goto 9f;\t\t\t\t\\\n+\tr6 = 0x12345678ffffffff ll;\t\t\t\\\n+\tr0 = 3;\t\t\t\t\t\t\\\n+\tr2 = *(u64 *)(r6 + 32760);\t\t\t\\\n+\tif r2 != 0 goto 9f;\t\t\t\t\\\n+\t*(u64 *)(r6 + 32760) = 1;\t\t\t\\\n+\t*(u8 *)(r6 + 32767) = r2;\t\t\t\\\n+\tr1 = 1;\t\t\t\t\t\t\\\n+\tlock *(u64 *)(r6 + 32760) += r1;\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+9:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(arena)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+__arena_scalar\n+__description(\"arena_scalar: store through the address of the stack as a number\")\n+__success __retval(0)\n+__load_if_JITed()\n+__naked void stack_addr_as_number(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[arena] ll;\t\t\t\t\\\n+\t*(u64 *)(r10 - 8) = 5;\t\t\t\t\\\n+\tr6 = r10;\t\t\t\t\t\\\n+\tr6 |= 0;\t\t\t\t\t\\\n+\t/* r6 is a number now. The store goes to arena, not to the stack. */ \\\n+\t*(u64 *)(r6 - 8) = 7;\t\t\t\t\\\n+\tr1 = 9;\t\t\t\t\t\t\\\n+\t*(u64 *)(r6 - 8) = r1;\t\t\t\t\\\n+\tlock *(u64 *)(r6 - 8) += r1;\t\t\t\\\n+\tr2 = *(u64 *)(r10 - 8);\t\t\t\t\\\n+\tr0 = 1;\t\t\t\t\t\t\\\n+\tif r2 != 5 goto 9f;\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+9:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(arena)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+__arena_scalar\n+__description(\"arena_scalar: 64-bit math on the address stays 64-bit\")\n+__success __retval(0)\n+__load_if_JITed()\n+__naked void alu64_after_access(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[arena] ll;\t\t\t\t\\\n+\tr2 = 0;\t\t\t\t\t\t\\\n+\tr3 = 1;\t\t\t\t\t\t\\\n+\tr4 = -1;\t\t\t\t\t\\\n+\tr5 = 0;\t\t\t\t\t\t\\\n+\tcall %[bpf_arena_alloc_pages];\t\t\t\\\n+\tr6 = r0;\t\t\t\t\t\\\n+\tr0 = 100;\t\t\t\t\t\\\n+\tif r6 == 0 goto 9f;\t\t\t\t\\\n+\tr2 = *(u64 *)(r6 + 0);\t\t\t\t\\\n+\tr7 = r6;\t\t\t\t\t\\\n+\tr7 += 8;\t\t\t\t\t\\\n+\tr7 -= r6;\t\t\t\t\t\\\n+\tr0 = 1;\t\t\t\t\t\t\\\n+\tif r7 != 8 goto 9f;\t\t\t\t\\\n+\tr7 = r6;\t\t\t\t\t\\\n+\tr7 \u003e\u003e= 32;\t\t\t\t\t\\\n+\tr0 = 2;\t\t\t\t\t\t\\\n+\tif r7 == 0 goto 9f;\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+9:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(arena),\n+\t __imm(bpf_arena_alloc_pages)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+__arena_scalar\n+__description(\"arena_scalar: st of an immediate changes no register\")\n+__success __retval(0)\n+__load_if_JITed()\n+__naked void st_keeps_regs(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[arena] ll;\t\t\t\t\\\n+\tr2 = 0;\t\t\t\t\t\t\\\n+\tr3 = 1;\t\t\t\t\t\t\\\n+\tr4 = -1;\t\t\t\t\t\\\n+\tr5 = 0;\t\t\t\t\t\t\\\n+\tcall %[bpf_arena_alloc_pages];\t\t\t\\\n+\tr6 = r0;\t\t\t\t\t\\\n+\tr0 = 100;\t\t\t\t\t\\\n+\tif r6 == 0 goto 9f;\t\t\t\t\\\n+\tr0 = r6;\t\t\t\t\t\\\n+\tr1 = 0x1111;\t\t\t\t\t\\\n+\tr2 = 0x2222;\t\t\t\t\t\\\n+\t*(u64 *)(r0 + 0) = 5;\t\t\t\t\\\n+\tr3 = r0;\t\t\t\t\t\\\n+\tr0 = 1;\t\t\t\t\t\t\\\n+\tif r3 != r6 goto 9f;\t\t\t\t\\\n+\tr0 = 2;\t\t\t\t\t\t\\\n+\tif r1 != 0x1111 goto 9f;\t\t\t\\\n+\tr0 = 3;\t\t\t\t\t\t\\\n+\tif r2 != 0x2222 goto 9f;\t\t\t\\\n+\tr0 = 0x3333;\t\t\t\t\t\\\n+\tr1 = r6;\t\t\t\t\t\\\n+\t*(u32 *)(r1 + 8) = -7;\t\t\t\t\\\n+\tr3 = r0;\t\t\t\t\t\\\n+\tr0 = 4;\t\t\t\t\t\t\\\n+\tif r3 != 0x3333 goto 9f;\t\t\t\\\n+\tr0 = 5;\t\t\t\t\t\t\\\n+\tif r1 != r6 goto 9f;\t\t\t\t\\\n+\tr0 = 6;\t\t\t\t\t\t\\\n+\tr3 = *(u64 *)(r6 + 0);\t\t\t\t\\\n+\tif r3 != 5 goto 9f;\t\t\t\t\\\n+\tr0 = 7;\t\t\t\t\t\t\\\n+\tr3 = *(u64 *)(r6 + 8);\t\t\t\t\\\n+\tr4 = 0xfffffff9 ll;\t\t\t\t\\\n+\tif r3 != r4 goto 9f;\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+9:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(arena),\n+\t __imm(bpf_arena_alloc_pages)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+__arena_scalar\n+__description(\"arena_scalar: access through a number with garbage in the upper half\")\n+__success __retval(0)\n+__load_if_JITed()\n+__naked void st_value_garbage(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[arena] ll;\t\t\t\t\\\n+\tr2 = 0;\t\t\t\t\t\t\\\n+\tr3 = 1;\t\t\t\t\t\t\\\n+\tr4 = -1;\t\t\t\t\t\\\n+\tr5 = 0;\t\t\t\t\t\t\\\n+\tcall %[bpf_arena_alloc_pages];\t\t\t\\\n+\tr6 = r0;\t\t\t\t\t\\\n+\tr0 = 100;\t\t\t\t\t\\\n+\tif r6 == 0 goto 9f;\t\t\t\t\\\n+\tr1 = 0xdeadbeef00001000 ll;\t\t\t\\\n+\t*(u64 *)(r6 + 2048) = r1;\t\t\t\\\n+\tr7 = *(u64 *)(r6 + 2048);\t\t\t\\\n+\tr8 = *(u64 *)(r6 + 2048);\t\t\t\\\n+\tr1 = 3;\t\t\t\t\t\t\\\n+\t*(u64 *)(r8 + 0) = 1;\t\t\t\t\\\n+\tr0 = 1;\t\t\t\t\t\t\\\n+\tif r8 != r7 goto 9f;\t\t\t\t\\\n+\t*(u8 *)(r8 + 1) = 1;\t\t\t\t\\\n+\tr0 = 2;\t\t\t\t\t\t\\\n+\tif r8 != r7 goto 9f;\t\t\t\t\\\n+\t*(u32 *)(r8 + 4) = r1;\t\t\t\t\\\n+\tr0 = 3;\t\t\t\t\t\t\\\n+\tif r8 != r7 goto 9f;\t\t\t\t\\\n+\tr2 = *(u16 *)(r8 + 2);\t\t\t\t\\\n+\tr0 = 4;\t\t\t\t\t\t\\\n+\tif r8 != r7 goto 9f;\t\t\t\t\\\n+\tr0 = 5;\t\t\t\t\t\t\\\n+\tif r1 != 3 goto 9f;\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+9:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(arena),\n+\t __imm(bpf_arena_alloc_pages)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+__arena_scalar\n+__description(\"arena_scalar: access through a number without the upper half\")\n+__success __retval(0)\n+__load_if_JITed()\n+__naked void st_value_small(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[arena] ll;\t\t\t\t\\\n+\tr2 = 0;\t\t\t\t\t\t\\\n+\tr3 = 1;\t\t\t\t\t\t\\\n+\tr4 = -1;\t\t\t\t\t\\\n+\tr5 = 0;\t\t\t\t\t\t\\\n+\tcall %[bpf_arena_alloc_pages];\t\t\t\\\n+\tr6 = r0;\t\t\t\t\t\\\n+\tr0 = 100;\t\t\t\t\t\\\n+\tif r6 == 0 goto 9f;\t\t\t\t\\\n+\tr1 = 0x1000 ll;\t\t\t\t\t\\\n+\t*(u64 *)(r6 + 2048) = r1;\t\t\t\\\n+\tr7 = *(u64 *)(r6 + 2048);\t\t\t\\\n+\tr8 = *(u64 *)(r6 + 2048);\t\t\t\\\n+\tr1 = 3;\t\t\t\t\t\t\\\n+\t*(u64 *)(r8 + 0) = 1;\t\t\t\t\\\n+\tr0 = 1;\t\t\t\t\t\t\\\n+\tif r8 != r7 goto 9f;\t\t\t\t\\\n+\t*(u8 *)(r8 + 1) = 1;\t\t\t\t\\\n+\tr0 = 2;\t\t\t\t\t\t\\\n+\tif r8 != r7 goto 9f;\t\t\t\t\\\n+\t*(u32 *)(r8 + 4) = r1;\t\t\t\t\\\n+\tr0 = 3;\t\t\t\t\t\t\\\n+\tif r8 != r7 goto 9f;\t\t\t\t\\\n+\tr2 = *(u16 *)(r8 + 2);\t\t\t\t\\\n+\tr0 = 4;\t\t\t\t\t\t\\\n+\tif r8 != r7 goto 9f;\t\t\t\t\\\n+\tr0 = 5;\t\t\t\t\t\t\\\n+\tif r1 != 3 goto 9f;\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+9:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(arena),\n+\t __imm(bpf_arena_alloc_pages)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+__arena_scalar\n+__description(\"arena_scalar: atomics through a number that is not an address in arena\")\n+__success __retval(0)\n+__load_if_JITed()\n+__naked void atomic_value(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[arena] ll;\t\t\t\t\\\n+\tr2 = 0;\t\t\t\t\t\t\\\n+\tr3 = 1;\t\t\t\t\t\t\\\n+\tr4 = -1;\t\t\t\t\t\\\n+\tr5 = 0;\t\t\t\t\t\t\\\n+\tcall %[bpf_arena_alloc_pages];\t\t\t\\\n+\tr6 = r0;\t\t\t\t\t\\\n+\tr0 = 100;\t\t\t\t\t\\\n+\tif r6 == 0 goto 9f;\t\t\t\t\\\n+\tr1 = 0xdeadbeef00001000 ll;\t\t\t\\\n+\t*(u64 *)(r6 + 2048) = r1;\t\t\t\\\n+\tr7 = *(u64 *)(r6 + 2048);\t\t\t\\\n+\tr8 = *(u64 *)(r6 + 2048);\t\t\t\\\n+\tr1 = 3;\t\t\t\t\t\t\\\n+\tlock *(u64 *)(r8 + 0) += r1;\t\t\t\\\n+\tr0 = 1;\t\t\t\t\t\t\\\n+\tif r8 != r7 goto 9f;\t\t\t\t\\\n+\t.8byte %[fetch_add_insn];\t\t\t\\\n+\tr0 = 2;\t\t\t\t\t\t\\\n+\tif r8 != r7 goto 9f;\t\t\t\t\\\n+\t.8byte %[xchg_insn];\t\t\t\t\\\n+\tr0 = 3;\t\t\t\t\t\t\\\n+\tif r8 != r7 goto 9f;\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+\t.8byte %[cmpxchg_insn];\t\t\t\t\\\n+\tr0 = 4;\t\t\t\t\t\t\\\n+\tif r8 != r7 goto 9f;\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+9:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(arena),\n+\t __imm(bpf_arena_alloc_pages),\n+\t __imm_insn(fetch_add_insn, BPF_ATOMIC_OP(BPF_DW, BPF_ADD | BPF_FETCH,\n+\t\t\t\t\t\t BPF_REG_8, BPF_REG_1, 0)),\n+\t __imm_insn(xchg_insn, BPF_ATOMIC_OP(BPF_DW, BPF_XCHG, BPF_REG_8, BPF_REG_1, 0)),\n+\t __imm_insn(cmpxchg_insn, BPF_ATOMIC_OP(BPF_DW, BPF_CMPXCHG, BPF_REG_8, BPF_REG_1, 0))\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+__arena_scalar\n+__description(\"arena_scalar: number and pointer to arena at the same insn\")\n+__success __retval(0)\n+__load_if_JITed()\n+__naked void mixed_number_arena(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[arena] ll;\t\t\t\t\\\n+\tr2 = 0;\t\t\t\t\t\t\\\n+\tr3 = 1;\t\t\t\t\t\t\\\n+\tr4 = -1;\t\t\t\t\t\\\n+\tr5 = 0;\t\t\t\t\t\t\\\n+\tcall %[bpf_arena_alloc_pages];\t\t\t\\\n+\tr6 = r0;\t\t\t\t\t\\\n+\tr0 = 100;\t\t\t\t\t\\\n+\tif r6 == 0 goto 9f;\t\t\t\t\\\n+\t*(u64 *)(r6 + 8) = 0x1234;\t\t\t\\\n+\t/* a number that the verifier does not know, 0 at run time */ \\\n+\tr7 = *(u64 *)(r6 + 16);\t\t\t\t\\\n+\tr8 = r6;\t\t\t\t\t\\\n+\tif r7 != 0 goto 1f;\t\t\t\t\\\n+\t.8byte %[cast_kern_insn];\t\t\t\\\n+1:\t*(u8 *)(r8 + 0) = 1;\t\t\t\t\\\n+\t*(u8 *)(r8 + 1) = r7;\t\t\t\t\\\n+\tr2 = *(u8 *)(r8 + 1);\t\t\t\t\\\n+\tlock *(u64 *)(r8 + 24) += r7;\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+\tif r7 != 0 goto 9f;\t\t\t\t\\\n+\t/* pointer to arena only: JIT adds all 64 bits of r8 to the base */ \\\n+\tr0 = 2;\t\t\t\t\t\t\\\n+\tr2 = *(u64 *)(r8 + 8);\t\t\t\t\\\n+\tif r2 != 0x1234 goto 9f;\t\t\t\\\n+\tr0 = 3;\t\t\t\t\t\t\\\n+\tr2 = *(u8 *)(r6 + 0);\t\t\t\t\\\n+\tif r2 != 1 goto 9f;\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+9:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(arena),\n+\t __imm(bpf_arena_alloc_pages),\n+\t __imm_insn(cast_kern_insn, BPF_RAW_INSN(BPF_ALU64 | BPF_MOV | BPF_X,\n+\t\t\t\t\t\t BPF_REG_8, BPF_REG_8, 1, 1))\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+__description(\"arena_scalar: no flag, no access through a number\")\n+__failure __msg(\"R6 invalid mem access 'scalar'\")\n+__naked void no_flag(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[arena] ll;\t\t\t\t\\\n+\tr6 = 0x100000000000 ll;\t\t\t\t\\\n+\tr0 = *(u64 *)(r6 + 0);\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(arena)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+__arena_scalar\n+__description(\"arena_scalar: no arena, no access through a number\")\n+__failure __msg(\"R6 invalid mem access 'scalar'\")\n+__naked void no_arena(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr6 = 0x100000000000 ll;\t\t\t\t\\\n+\tr0 = *(u64 *)(r6 + 0);\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+__arena_scalar\n+__description(\"arena_scalar: pointer that is NULL is an address in arena\")\n+__success __retval(0)\n+__load_if_JITed()\n+__naked void null_ptr(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[arena] ll;\t\t\t\t\\\n+\tr1 = 0;\t\t\t\t\t\t\\\n+\t*(u32 *)(r10 - 4) = r1;\t\t\t\t\\\n+\tr2 = r10;\t\t\t\t\t\\\n+\tr2 += -4;\t\t\t\t\t\\\n+\tr1 = %[hash] ll;\t\t\t\t\\\n+\tcall %[bpf_map_lookup_elem];\t\t\t\\\n+\tr1 = r0;\t\t\t\t\t\\\n+\tr0 = 1;\t\t\t\t\t\t\\\n+\tif r1 != 0 goto 9f;\t\t\t\t\\\n+\t/* nothing is allocated: the load reads 0 */\t\\\n+\tr0 = *(u64 *)(r1 + 0);\t\t\t\t\\\n+9:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(arena),\n+\t __imm_addr(hash),\n+\t __imm(bpf_map_lookup_elem)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+__arena_scalar\n+__description(\"arena_scalar: pointer that is NULL with an offset is an address in arena\")\n+__success __retval(0)\n+__load_if_JITed()\n+__naked void null_ptr_off(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[arena] ll;\t\t\t\t\\\n+\tr1 = 0;\t\t\t\t\t\t\\\n+\t*(u32 *)(r10 - 4) = r1;\t\t\t\t\\\n+\tr2 = r10;\t\t\t\t\t\\\n+\tr2 += -4;\t\t\t\t\t\\\n+\tr1 = %[hash] ll;\t\t\t\t\\\n+\tcall %[bpf_map_lookup_elem];\t\t\t\\\n+\tr1 = r0;\t\t\t\t\t\\\n+\tr0 = 1;\t\t\t\t\t\t\\\n+\tif r1 != 0 goto 9f;\t\t\t\t\\\n+\tr1 += 8;\t\t\t\t\t\\\n+\t*(u64 *)(r1 + 0) = 5;\t\t\t\t\\\n+\tr0 = *(u64 *)(r1 + 0);\t\t\t\t\\\n+9:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(arena),\n+\t __imm_addr(hash),\n+\t __imm(bpf_map_lookup_elem)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+__arena_scalar\n+__description(\"arena_scalar: number that is less than a page is an address in arena\")\n+__success __retval(0)\n+__load_if_JITed()\n+__naked void small_number(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[arena] ll;\t\t\t\t\\\n+\tcall %[bpf_get_prandom_u32];\t\t\t\\\n+\tr1 = r0;\t\t\t\t\t\\\n+\tr1 \u0026= 0xfff;\t\t\t\t\t\\\n+\tr0 = *(u64 *)(r1 + 0);\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(arena),\n+\t __imm(bpf_get_prandom_u32)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+__arena_scalar\n+__description(\"arena_scalar: number is not a pointer for a helper\")\n+__failure __msg(\"R2 type=scalar expected=\")\n+__naked void helper_arg(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[arena] ll;\t\t\t\t\\\n+\tr2 = 0;\t\t\t\t\t\t\\\n+\tr3 = 1;\t\t\t\t\t\t\\\n+\tr4 = -1;\t\t\t\t\t\\\n+\tr5 = 0;\t\t\t\t\t\t\\\n+\tcall %[bpf_arena_alloc_pages];\t\t\t\\\n+\tr6 = r0;\t\t\t\t\t\\\n+\tr0 = 100;\t\t\t\t\t\\\n+\tif r6 == 0 goto 9f;\t\t\t\t\\\n+\tr1 = %[hash] ll;\t\t\t\t\\\n+\tr2 = r6;\t\t\t\t\t\\\n+\tcall %[bpf_map_lookup_elem];\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+9:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(arena),\n+\t __imm_addr(hash),\n+\t __imm(bpf_arena_alloc_pages),\n+\t __imm(bpf_map_lookup_elem)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+__arena_scalar\n+__description(\"arena_scalar: number and pointer to stack at the same insn\")\n+__failure __msg(\"same insn cannot be used with different pointers\")\n+__load_if_JITed()\n+__naked void mixed_number_stack(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[arena] ll;\t\t\t\t\\\n+\tr2 = 0;\t\t\t\t\t\t\\\n+\tr3 = 1;\t\t\t\t\t\t\\\n+\tr4 = -1;\t\t\t\t\t\\\n+\tr5 = 0;\t\t\t\t\t\t\\\n+\tcall %[bpf_arena_alloc_pages];\t\t\t\\\n+\tr6 = r0;\t\t\t\t\t\\\n+\tr0 = 100;\t\t\t\t\t\\\n+\tif r6 == 0 goto 9f;\t\t\t\t\\\n+\t*(u64 *)(r10 - 8) = 0;\t\t\t\t\\\n+\tcall %[bpf_get_prandom_u32];\t\t\t\\\n+\tif w0 != 0 goto 1f;\t\t\t\t\\\n+\tr6 = r10;\t\t\t\t\t\\\n+\tr6 += -8;\t\t\t\t\t\\\n+1:\tr0 = *(u64 *)(r6 + 0);\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+9:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(arena),\n+\t __imm(bpf_arena_alloc_pages),\n+\t __imm(bpf_get_prandom_u32)\n+\t: __clobber_all);\n+}\n+\n+static int st_cb(__u64 idx, void *ctx)\n+{\n+\tvolatile long *p = *(volatile long **)ctx;\n+\n+\tp[idx] = 7;\n+\treturn 0;\n+}\n+\n+SEC(\"syscall\")\n+__arena_scalar\n+__description(\"arena_scalar: store through a number in a callback\")\n+__success __retval(0)\n+__load_if_JITed()\n+int st_in_callback(void *unused)\n+{\n+\tvolatile long *p = bpf_arena_alloc_pages(\u0026arena, NULL, 1, -1, 0);\n+\n+\tif (!p)\n+\t\treturn 100;\n+\tbpf_loop(4, st_cb, \u0026p, 0);\n+\treturn p[0] + p[3] + p[4] - 14;\n+}\n+\n+char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/progs/verifier_value_illegal_alu.c b/tools/testing/selftests/bpf/progs/verifier_value_illegal_alu.c\nindex 4d8273c258d51..31663338866d3 100644\n--- a/tools/testing/selftests/bpf/progs/verifier_value_illegal_alu.c\n+++ b/tools/testing/selftests/bpf/progs/verifier_value_illegal_alu.c\n@@ -22,8 +22,8 @@ struct {\n \n SEC(\"socket\")\n __description(\"map element value illegal alu op, 1\")\n-__failure __msg(\"R0 bitwise operator \u0026= on pointer\")\n-__failure_unpriv\n+__failure __msg(\"R0 invalid mem access 'scalar'\")\n+__failure_unpriv __msg_unpriv(\"R0 bitwise operator \u0026= on pointer\")\n __naked void value_illegal_alu_op_1(void)\n {\n \tasm volatile (\"\t\t\t\t\t\\\n@@ -70,8 +70,8 @@ l0_%=:\texit;\t\t\t\t\t\t\\\n \n SEC(\"socket\")\n __description(\"map element value illegal alu op, 3\")\n-__failure __msg(\"R0 pointer arithmetic with /= operator\")\n-__failure_unpriv\n+__failure __msg(\"R0 invalid mem access 'scalar'\")\n+__failure_unpriv __msg_unpriv(\"R0 pointer arithmetic with /= operator\")\n __naked void value_illegal_alu_op_3(void)\n {\n \tasm volatile (\"\t\t\t\t\t\\\n@@ -165,6 +165,171 @@ __naked void map_ptr_illegal_alu_op(void)\n \t: __clobber_all);\n }\n \n+SEC(\"socket\")\n+__description(\"tag in the low bit of a pointer, and, shift\")\n+__success __retval(0)\n+__failure_unpriv __msg_unpriv(\"R1 bitwise operator \u0026= on pointer\")\n+__naked void ptr_tag_and_shift(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr2 = r10;\t\t\t\t\t\\\n+\tr2 += -8;\t\t\t\t\t\\\n+\tr1 = 0;\t\t\t\t\t\t\\\n+\t*(u64*)(r2 + 0) = r1;\t\t\t\t\\\n+\tr1 = %[map_hash_48b] ll;\t\t\t\\\n+\tcall %[bpf_map_lookup_elem];\t\t\t\\\n+\tif r0 == 0 goto l0_%=;\t\t\t\t\\\n+\tr1 = r0;\t\t\t\t\t\\\n+\tr1 \u0026= 1;\t\t\t\t\t\\\n+\tr2 = r0;\t\t\t\t\t\\\n+\tr2 \u003e\u003e= 1;\t\t\t\t\t\\\n+\tr3 = r0;\t\t\t\t\t\\\n+\tr3 |= 1;\t\t\t\t\t\\\n+\tr3 ^= 1;\t\t\t\t\t\\\n+\tr0 = *(u32*)(r0 + 0);\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+l0_%=:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm(bpf_map_lookup_elem),\n+\t __imm_addr(map_hash_48b)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"socket\")\n+__description(\"tag in the low bit of a pointer, CAP_BPF without CAP_PERFMON\")\n+__success __retval(0)\n+__failure_unpriv __msg_unpriv(\"R1 bitwise operator \u0026= on pointer\")\n+__caps_unpriv(CAP_BPF)\n+__naked void ptr_tag_cap_bpf(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr2 = r10;\t\t\t\t\t\\\n+\tr2 += -8;\t\t\t\t\t\\\n+\tr1 = 0;\t\t\t\t\t\t\\\n+\t*(u64*)(r2 + 0) = r1;\t\t\t\t\\\n+\tr1 = %[map_hash_48b] ll;\t\t\t\\\n+\tcall %[bpf_map_lookup_elem];\t\t\t\\\n+\tif r0 == 0 goto l0_%=;\t\t\t\t\\\n+\tr1 = r0;\t\t\t\t\t\\\n+\tr1 \u0026= 1;\t\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+l0_%=:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm(bpf_map_lookup_elem),\n+\t __imm_addr(map_hash_48b)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"socket\")\n+__description(\"number op= pointer\")\n+__success __retval(0)\n+__failure_unpriv __msg_unpriv(\"R1 pointer arithmetic with *= operator\")\n+__naked void number_mul_ptr(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr2 = r10;\t\t\t\t\t\\\n+\tr2 += -8;\t\t\t\t\t\\\n+\tr1 = 0;\t\t\t\t\t\t\\\n+\t*(u64*)(r2 + 0) = r1;\t\t\t\t\\\n+\tr1 = %[map_hash_48b] ll;\t\t\t\\\n+\tcall %[bpf_map_lookup_elem];\t\t\t\\\n+\tif r0 == 0 goto l0_%=;\t\t\t\t\\\n+\tr1 = 7;\t\t\t\t\t\t\\\n+\tr1 *= r0;\t\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+l0_%=:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm(bpf_map_lookup_elem),\n+\t __imm_addr(map_hash_48b)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"socket\")\n+__description(\"pointer with the tag cleared is a number\")\n+__failure __msg(\"R0 invalid mem access 'scalar'\")\n+__failure_unpriv __msg_unpriv(\"R0 bitwise operator |= on pointer\")\n+__naked void ptr_tag_cleared_deref(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr2 = r10;\t\t\t\t\t\\\n+\tr2 += -8;\t\t\t\t\t\\\n+\tr1 = 0;\t\t\t\t\t\t\\\n+\t*(u64*)(r2 + 0) = r1;\t\t\t\t\\\n+\tr1 = %[map_hash_48b] ll;\t\t\t\\\n+\tcall %[bpf_map_lookup_elem];\t\t\t\\\n+\tif r0 == 0 goto l0_%=;\t\t\t\t\\\n+\tr0 |= 1;\t\t\t\t\t\\\n+\tr0 ^= 1;\t\t\t\t\t\\\n+\tr0 = *(u32*)(r0 + 0);\t\t\t\t\\\n+l0_%=:\tr0 = 0;\t\t\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm(bpf_map_lookup_elem),\n+\t __imm_addr(map_hash_48b)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"socket\")\n+__description(\"shift of a pointer that may be NULL\")\n+__failure __msg(\"R0 pointer arithmetic on map_value_or_null prohibited, null-check it first\")\n+__failure_unpriv\n+__naked void ptr_or_null_shift(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr2 = r10;\t\t\t\t\t\\\n+\tr2 += -8;\t\t\t\t\t\\\n+\tr1 = 0;\t\t\t\t\t\t\\\n+\t*(u64*)(r2 + 0) = r1;\t\t\t\t\\\n+\tr1 = %[map_hash_48b] ll;\t\t\t\\\n+\tcall %[bpf_map_lookup_elem];\t\t\t\\\n+\tr0 \u003e\u003e= 1;\t\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm(bpf_map_lookup_elem),\n+\t __imm_addr(map_hash_48b)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"socket\")\n+__description(\"and of a pointer to map\")\n+__failure __msg(\"R0 pointer arithmetic on map_ptr prohibited\")\n+__failure_unpriv\n+__naked void map_ptr_and(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr0 = %[map_hash_48b] ll;\t\t\t\\\n+\tr0 \u0026= 1;\t\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(map_hash_48b)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"socket\")\n+__description(\"32-bit and of a pointer\")\n+__success __retval(0)\n+__failure_unpriv __msg_unpriv(\"R0 32-bit pointer arithmetic prohibited\")\n+__naked void ptr_and32(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr2 = r10;\t\t\t\t\t\\\n+\tr2 += -8;\t\t\t\t\t\\\n+\tr1 = 0;\t\t\t\t\t\t\\\n+\t*(u64*)(r2 + 0) = r1;\t\t\t\t\\\n+\tr1 = %[map_hash_48b] ll;\t\t\t\\\n+\tcall %[bpf_map_lookup_elem];\t\t\t\\\n+\tif r0 == 0 goto l0_%=;\t\t\t\t\\\n+\tw0 \u0026= 1;\t\t\t\t\t\\\n+l0_%=:\tr0 = 0;\t\t\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm(bpf_map_lookup_elem),\n+\t __imm_addr(map_hash_48b)\n+\t: __clobber_all);\n+}\n+\n SEC(\"flow_dissector\")\n __description(\"flow_keys illegal alu op with variable offset\")\n __failure __msg(\"R7 pointer arithmetic on flow_keys prohibited\")\ndiff --git a/tools/testing/selftests/bpf/test_loader.c b/tools/testing/selftests/bpf/test_loader.c\nindex 25eeb1c1248b3..a89890cd56d82 100644\n--- a/tools/testing/selftests/bpf/test_loader.c\n+++ b/tools/testing/selftests/bpf/test_loader.c\n@@ -580,6 +580,8 @@ static int parse_test_spec(struct test_loader *tester,\n \t\t\t\tupdate_flags(\u0026spec-\u003eprog_flags, BPF_F_XDP_HAS_FRAGS, clear);\n \t\t\t} else if (strcmp(val, \"BPF_F_TEST_REG_INVARIANTS\") == 0) {\n \t\t\t\tupdate_flags(\u0026spec-\u003eprog_flags, BPF_F_TEST_REG_INVARIANTS, clear);\n+\t\t\t} else if (strcmp(val, \"BPF_F_ARENA_SCALAR\") == 0) {\n+\t\t\t\tupdate_flags(\u0026spec-\u003eprog_flags, BPF_F_ARENA_SCALAR, clear);\n \t\t\t} else /* assume numeric value */ {\n \t\t\t\terr = parse_int(val, \u0026flags, \"test prog flags\");\n \t\t\t\tif (err)\n"
} |
| 2/1 | 2026/10/03 02:37 | agent | patch-evaluator |
0mModel:core Args: null Results: {
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT"
],
"FocusSymbols": [
"bpf_do_misc_fixups",
"arena_scalar_access",
"mem_access_reg",
"is_arena_scalar",
"adjust_ptr_min_max_vals",
"bpf_jit_supports_arena_scalar",
"btf_datasec_resolve",
"btf_func_check",
"btf_name_valid_identifier"
],
"Reasoning": "The patch series adds BPF_F_ARENA_SCALAR support and relaxes BTF validation (to support Rust BPF programs). It introduces new pointer-to-scalar conversion rules in adjust_ptr_min_max_vals, allows scalar registers to access BPF arena memory in the verifier (mem_access_reg, check_load_mem, check_store_reg, check_atomic_rmw), patches arena scalar accesses in bpf_do_misc_fixups, maps BPF_REG_AX in x86 JIT, and modifies BTF validation logic (btf_datasec_resolve, btf_func_check, btf_name_valid_identifier). All changes affect core, security-critical BPF subsystem code directly reachable via the bpf() syscall.",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.
IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.
================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.
- REACHABILITY IS THE PRIMARY GATE:
Fuzzing can only discover bugs in code that can actually execute in standard virtualized
environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
regardless of whether it adds assertions or complex logic.
- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
Patch authors routinely label changes as "cleanups", "refactorings", or state
"No functional change intended". Do NOT take these claims at face value.
Code refactorings that rearrange logic, introduce helper functions, or alter state management
in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
If reachable executable code is modified or refactored, it MUST be fuzzed.
- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
Even if the author believes the invariant always holds, fuzzing is essential to verify whether
an unusual sequence of operations can violate it.
================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:
- Non-kernel and non-executable changes:
* Modifications to Documentation/, comments, or spelling fixes.
* User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
that do not affect the compiled kernel image (vmlinux) or kernel modules.
* Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
that do not alter control flow or data structures.
* Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
* Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
* Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
physical PCI hot-unplug or manual sysfs driver unbinding.
* Code paths exclusive to architectures other than the target architecture.
================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
* Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
* Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
* Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
* Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).
================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:
- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
Do NOT list generic, ubiquitous functions called by almost every program in the corpus
(including, but not limited to: general memory allocators and deallocators, page fault
and trap handlers, or core synchronization primitives; this is not an exhaustive list).
Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
which severely dilutes fuzzing effort away from the actual changes.
- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
List functions that specifically implement the logic being added or altered, or direct API entrypoints
for the subsystem feature under review.
- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).
================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any '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: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit 204514513c3972ac15a879bd61fade9e9f92eecc
Author: syz-cluster <triage@syzkaller.com>
Date: Sat Oct 3 02:37:14 2026 +0000
syz-cluster: applied patch under review
diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
index 475e706534543..6979c8ead0e8b 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -3413,6 +3413,11 @@ bool bpf_jit_supports_arena(void)
return true;
}
+bool bpf_jit_supports_arena_scalar(void)
+{
+ return true;
+}
+
bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)
{
if (!in_arena)
diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
index 6c7a0578760e7..8199d28e2a128 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -236,6 +236,7 @@ static const int reg2pt_regs[] = {
[BPF_REG_7] = offsetof(struct pt_regs, r13),
[BPF_REG_8] = offsetof(struct pt_regs, r14),
[BPF_REG_9] = offsetof(struct pt_regs, r15),
+ [BPF_REG_AX] = offsetof(struct pt_regs, r10),
};
/*
@@ -4667,6 +4668,11 @@ bool bpf_jit_supports_arena(void)
return true;
}
+bool bpf_jit_supports_arena_scalar(void)
+{
+ return true;
+}
+
bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)
{
if (!in_arena)
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 811342e3c0419..0074f1356c584 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -672,6 +672,7 @@ struct bpf_insn_aux_data {
bool non_sleepable; /* helper/kfunc may be called from non-sleepable context */
bool is_iter_next; /* bpf_iter_<type>_next() kfunc call */
bool call_with_percpu_alloc_ptr; /* {this,per}_cpu_ptr() with prog percpu alloc */
+ bool arena_scalar; /* ldx/stx/st/atomic through a number, it's an address in arena */
u8 alu_state; /* used in combination with alu_limit */
/* true if STX or LDX instruction is a part of a spill/fill
* pattern for a bpf_fastcall call.
@@ -943,6 +944,7 @@ struct bpf_verifier_env {
bool strict_alignment; /* perform strict pointer alignment checks */
bool test_state_freq; /* test verifier with different pruning frequency */
bool test_reg_invariants; /* fail verification on register invariants violations */
+ bool arena_scalar; /* load and store through a number is arena access */
struct bpf_verifier_state *cur_state; /* current verifier state */
/* Search pruning optimization, array of list_heads for
* lists of struct bpf_verifier_state_list.
diff --git a/include/linux/filter.h b/include/linux/filter.h
index e42eccb0990e6..9339c6131f8ff 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -1250,6 +1250,7 @@ bool bpf_jit_supports_far_kfunc_call(void);
bool bpf_jit_supports_exceptions(void);
bool bpf_jit_supports_ptr_xchg(void);
bool bpf_jit_supports_arena(void);
+bool bpf_jit_supports_arena_scalar(void);
bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena);
bool bpf_jit_supports_private_stack(void);
bool bpf_jit_supports_large_stack(void);
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 4687c33109968..e0ed44b1bbcb7 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -1344,6 +1344,12 @@ enum bpf_perf_event_type {
/* The verifier internal test flag. Behavior is undefined */
#define BPF_F_TEST_REG_INVARIANTS (1U << 7)
+/*
+ * Load and store through a number is an access to the arena of the program
+ * at the low 32 bits of the number. It's for programs written in Rust.
+ */
+#define BPF_F_ARENA_SCALAR (1U << 8)
+
/* link_create.kprobe_multi.flags used in LINK_CREATE command for
* BPF_TRACE_KPROBE_MULTI attach type to create return probe.
*/
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 8cc17a1cd25c6..0630675377aaa 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -901,16 +901,6 @@ static bool btf_name_offset_valid(const struct btf *btf, u32 offset)
return offset < btf->hdr.str_len;
}
-static bool __btf_name_char_ok(char c, bool first)
-{
- if ((first ? !isalpha(c) :
- !isalnum(c)) &&
- c != '_' &&
- c != '.')
- return false;
- return true;
-}
-
const char *btf_str_by_offset(const struct btf *btf, u32 offset)
{
while (offset < btf->start_str_off)
@@ -923,20 +913,21 @@ const char *btf_str_by_offset(const struct btf *btf, u32 offset)
return NULL;
}
+/* Names in BTF of Rust are not C identifiers. Allow any printable character */
static bool btf_name_valid_identifier(const struct btf *btf, u32 offset)
{
/* offset must be valid */
const char *src = btf_str_by_offset(btf, offset);
const char *src_limit;
- if (!__btf_name_char_ok(*src, true))
+ if (!isprint(*src))
return false;
/* set a limit on identifier length */
src_limit = src + KSYM_NAME_LEN;
src++;
while (*src && src < src_limit) {
- if (!__btf_name_char_ok(*src, false))
+ if (!isprint(*src))
return false;
src++;
}
@@ -5406,7 +5397,7 @@ static int btf_datasec_resolve(struct btf_verifier_env *env,
env->resolve_mode = RESOLVE_TBD;
for_each_vsi_from(i, v->next_member, v->t, vsi) {
- u32 var_type_id = vsi->type, type_id, type_size = 0;
+ u32 var_type_id = vsi->type, type_id;
const struct btf_type *var_type = btf_type_by_id(env->btf,
var_type_id);
if (!var_type || !btf_type_is_var(var_type)) {
@@ -5421,16 +5412,16 @@ static int btf_datasec_resolve(struct btf_verifier_env *env,
return env_stack_push(env, var_type, var_type_id);
}
+ /*
+ * The variable can be smaller than its type. It's a piece of
+ * a variable that the compiler split then, with the type of
+ * the whole variable.
+ */
type_id = var_type->type;
- if (!btf_type_id_size(btf, &type_id, &type_size)) {
+ if (!btf_type_id_size(btf, &type_id, NULL)) {
btf_verifier_log_vsi(env, v->t, vsi, "Invalid type");
return -EINVAL;
}
-
- if (vsi->size < type_size) {
- btf_verifier_log_vsi(env, v->t, vsi, "Invalid size");
- return -EINVAL;
- }
}
env_stack_pop_resolved(env, 0, 0);
@@ -5443,6 +5434,16 @@ static void btf_datasec_log(struct btf_verifier_env *env,
btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
}
+/* A piece of a variable is smaller than its type, which is the one of the whole variable */
+static bool btf_var_is_piece(const struct btf *btf, const struct btf_type *var,
+ const struct btf_var_secinfo *vsi)
+{
+ u32 size;
+
+ return IS_ERR(btf_resolve_size(btf, btf_type_by_id(btf, var->type), &size)) ||
+ vsi->size < size;
+}
+
static void btf_datasec_show(const struct btf *btf,
const struct btf_type *t, u32 type_id,
void *data, u8 bits_offset,
@@ -5450,6 +5451,7 @@ static void btf_datasec_show(const struct btf *btf,
{
const struct btf_var_secinfo *vsi;
const struct btf_type *var;
+ bool comma = false;
u32 i;
if (!btf_show_start_type(show, t, type_id, data))
@@ -5459,8 +5461,12 @@ static void btf_datasec_show(const struct btf *btf,
__btf_name_by_offset(btf, t->name_off));
for_each_vsi(i, t, vsi) {
var = btf_type_by_id(btf, vsi->type);
- if (i)
+ /* there is less data than the type takes */
+ if (btf_var_is_piece(btf, var, vsi))
+ continue;
+ if (comma)
btf_show(show, ",");
+ comma = true;
btf_type_ops(var)->show(btf, var, vsi->type,
data + vsi->offset, bits_offset, show);
}
@@ -5761,6 +5767,10 @@ static int btf_func_check(struct btf_verifier_env *env,
return -EINVAL;
}
+ /* Rust leaves out names of some arguments of static functions */
+ if (btf_func_linkage(t) == BTF_FUNC_STATIC)
+ return 0;
+
args = (const struct btf_param *)(proto_type + 1);
nr_args = btf_type_vlen(proto_type);
for (i = 0; i < nr_args; i++) {
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index d3b8b626ec0f7..36900b02d6684 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -18,6 +18,7 @@
*/
#include <uapi/linux/btf.h>
+#include <linux/ctype.h>
#include <linux/filter.h>
#include <linux/sched/signal.h>
#include <linux/skbuff.h>
@@ -589,6 +590,10 @@ bpf_prog_ksym_set_name(struct bpf_prog *prog)
prog->aux->func_info[prog->aux->func_idx].type_id);
func_name = btf_name_by_offset(prog->aux->btf, type->name_off);
snprintf(sym, (size_t)(end - sym), "_%s", func_name);
+ /* the name of a function of Rust is not an identifier */
+ for (; *sym; sym++)
+ if (!isalnum(*sym) && *sym != '_' && *sym != '.')
+ *sym = '_';
return;
}
@@ -3429,6 +3434,12 @@ bool __weak bpf_jit_supports_arena(void)
return false;
}
+/* Whether JIT takes BPF_REG_AX as the address of arena access */
+bool __weak bpf_jit_supports_arena_scalar(void)
+{
+ return false;
+}
+
bool __weak bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)
{
return false;
diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
index 37cf130ebb57b..b4bfe5522003b 100644
--- a/kernel/bpf/fixups.c
+++ b/kernel/bpf/fixups.c
@@ -46,6 +46,31 @@ static bool is_addr_space_cast32(struct bpf_prog *prog, const struct bpf_insn *i
return false;
}
+/*
+ * The insn accesses arena through a number. JITs add the base of arena
+ * to the register as it is, so the access goes through the low 32 bits of
+ * the number in BPF_REG_AX. Registers of the program are not changed: the
+ * number may be compared or stored later, and on another path the register
+ * may be PTR_TO_ARENA.
+ *
+ * Constant blinding leaves insns that use BPF_REG_AX alone, so the immediate
+ * of st through a number is not blinded.
+ */
+static int arena_scalar_access(const struct bpf_insn *insn, struct bpf_insn *buf)
+{
+ bool load = BPF_CLASS(insn->code) == BPF_LDX || bpf_atomic_is_load_acq(insn);
+ struct bpf_insn *patch = buf;
+
+ *patch++ = BPF_MOV32_REG(BPF_REG_AX, load ? insn->src_reg : insn->dst_reg);
+ *patch = *insn;
+ if (load)
+ patch->src_reg = BPF_REG_AX;
+ else
+ patch->dst_reg = BPF_REG_AX;
+ patch++;
+ return patch - buf;
+}
+
/* Return the regno defined by the insn, or -1. */
static int insn_def_regno(const struct bpf_insn *insn)
{
@@ -1785,6 +1810,20 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
/* Convert BPF_CLASS(insn->code) == BPF_ALU64 to 32-bit ALU */
insn->code = BPF_ALU | BPF_OP(insn->code) | BPF_SRC(insn->code);
+ if (env->insn_aux_data[i + delta].arena_scalar) {
+ cnt = arena_scalar_access(insn, insn_buf);
+
+ new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
+ if (!new_prog)
+ return -ENOMEM;
+
+ delta += cnt - 1;
+ prog = new_prog;
+ env->prog = prog;
+ insn = prog->insnsi + i + delta;
+ goto next_insn;
+ }
+
/* Make sdiv/smod divide-by-minus-one exceptions impossible. */
if ((insn->code == (BPF_ALU64 | BPF_MOD | BPF_K) ||
insn->code == (BPF_ALU64 | BPF_DIV | BPF_K) ||
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index ac52f4ae414ce..778bca2abc042 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2948,9 +2948,13 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_at
BPF_F_XDP_HAS_FRAGS |
BPF_F_XDP_DEV_BOUND_ONLY |
BPF_F_TEST_REG_INVARIANTS |
+ BPF_F_ARENA_SCALAR |
BPF_F_TOKEN_FD))
return -EINVAL;
+ if ((attr->prog_flags & BPF_F_ARENA_SCALAR) && !bpf_jit_supports_arena_scalar())
+ return -EOPNOTSUPP;
+
bpf_prog_load_fixup_attach_type(attr);
if (attr->prog_flags & BPF_F_TOKEN_FD) {
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index b840b3eb9b229..2daee0130e5af 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -5250,6 +5250,19 @@ static bool is_arena_reg(struct bpf_verifier_env *env, int regno)
return reg->type == PTR_TO_ARENA;
}
+/*
+ * There are no address spaces in Rust, addresses of arena are plain numbers.
+ * When the program is loaded with BPF_F_ARENA_SCALAR and has an arena a load or
+ * a store through a number is an access to arena at the low 32 bits of it,
+ * like the access through PTR_TO_ARENA is. The register stays a number.
+ */
+static bool is_arena_scalar(struct bpf_verifier_env *env, int regno)
+{
+ const struct bpf_reg_state *reg = reg_state(env, regno);
+
+ return reg->type == SCALAR_VALUE && env->arena_scalar && env->prog->aux->arena;
+}
+
static bool is_load_acq_unsafe(struct bpf_verifier_env *env, int regno,
struct bpf_insn *insn)
{
@@ -5280,7 +5293,7 @@ static bool atomic_ptr_type_ok(struct bpf_verifier_env *env, int regno,
return false;
if (is_sk_reg(env, regno))
return false;
- if (is_arena_reg(env, regno))
+ if (is_arena_reg(env, regno) || is_arena_scalar(env, regno))
return bpf_jit_supports_insn(insn, true);
if (is_load_acq_unsafe(env, regno, insn))
return false;
@@ -7161,6 +7174,22 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, struct b
static int save_aux_ptr_type(struct bpf_verifier_env *env, enum bpf_reg_type type,
bool allow_trust_mismatch);
+/*
+ * Returns the register to check the access of the current insn with.
+ * For a number in a program with an arena that is 'arena'.
+ */
+static struct bpf_reg_state *mem_access_reg(struct bpf_verifier_env *env, int regno,
+ struct bpf_reg_state *arena)
+{
+ if (!is_arena_scalar(env, regno))
+ return cur_regs(env) + regno;
+
+ memset(arena, 0, sizeof(*arena));
+ arena->type = PTR_TO_ARENA;
+ env->insn_aux_data[env->insn_idx].arena_scalar = true;
+ return arena;
+}
+
static int check_load_mem(struct bpf_verifier_env *env, struct bpf_insn *insn,
bool strict_alignment_once, bool is_ldsx,
bool allow_trust_mismatch, const char *ctx)
@@ -7168,6 +7197,7 @@ static int check_load_mem(struct bpf_verifier_env *env, struct bpf_insn *insn,
struct bpf_verifier_state *vstate = env->cur_state;
struct bpf_func_state *state = vstate->frame[vstate->curframe];
struct bpf_reg_state *regs = cur_regs(env);
+ struct bpf_reg_state arena, *src_reg;
enum bpf_reg_type src_reg_type;
int err;
@@ -7189,15 +7219,16 @@ static int check_load_mem(struct bpf_verifier_env *env, struct bpf_insn *insn,
if (err)
return err;
- src_reg_type = regs[insn->src_reg].type;
+ src_reg = mem_access_reg(env, insn->src_reg, &arena);
+ src_reg_type = src_reg->type;
/*
* check_stack_read_fixed_off() may refine the modification's origin to
* the source stack slot.
*/
bpf_diag_mod_begin(env, ®s[insn->dst_reg], NULL, BPF_DIAG_MOD_WRITE);
- err = check_mem_access(env, env->insn_idx, regs + insn->src_reg, argno_from_reg(insn->src_reg), insn->off,
- BPF_SIZE(insn->code), BPF_READ, insn->dst_reg,
+ err = check_mem_access(env, env->insn_idx, src_reg, argno_from_reg(insn->src_reg),
+ insn->off, BPF_SIZE(insn->code), BPF_READ, insn->dst_reg,
strict_alignment_once, is_ldsx);
err = err ?: save_aux_ptr_type(env, src_reg_type,
allow_trust_mismatch);
@@ -7214,6 +7245,7 @@ static int check_store_reg(struct bpf_verifier_env *env, struct bpf_insn *insn,
struct bpf_verifier_state *vstate = env->cur_state;
struct bpf_func_state *state = vstate->frame[vstate->curframe];
struct bpf_reg_state *regs = cur_regs(env);
+ struct bpf_reg_state arena, *dst_reg;
enum bpf_reg_type dst_reg_type;
int err;
@@ -7235,11 +7267,12 @@ static int check_store_reg(struct bpf_verifier_env *env, struct bpf_insn *insn,
if (err)
return err;
- dst_reg_type = regs[insn->dst_reg].type;
+ dst_reg = mem_access_reg(env, insn->dst_reg, &arena);
+ dst_reg_type = dst_reg->type;
/* Check if (dst_reg + off) is writeable. */
- err = check_mem_access(env, env->insn_idx, regs + insn->dst_reg, argno_from_reg(insn->dst_reg), insn->off,
- BPF_SIZE(insn->code), BPF_WRITE, insn->src_reg,
+ err = check_mem_access(env, env->insn_idx, dst_reg, argno_from_reg(insn->dst_reg),
+ insn->off, BPF_SIZE(insn->code), BPF_WRITE, insn->src_reg,
strict_alignment_once, false);
err = err ?: save_aux_ptr_type(env, dst_reg_type, false);
@@ -7249,7 +7282,7 @@ static int check_store_reg(struct bpf_verifier_env *env, struct bpf_insn *insn,
static int check_atomic_rmw(struct bpf_verifier_env *env,
struct bpf_insn *insn)
{
- struct bpf_reg_state *dst_reg;
+ struct bpf_reg_state arena, *dst_reg;
int load_reg;
int err;
@@ -7294,6 +7327,9 @@ static int check_atomic_rmw(struct bpf_verifier_env *env,
return -EACCES;
}
+ /* load_reg may be dst_reg. Look at dst_reg before it's marked as unknown. */
+ dst_reg = mem_access_reg(env, insn->dst_reg, &arena);
+
load_reg = bpf_atomic_load_reg(insn);
if (load_reg >= 0) {
/* check and record load of old value */
@@ -7302,8 +7338,6 @@ static int check_atomic_rmw(struct bpf_verifier_env *env,
return err;
}
- dst_reg = cur_regs(env) + insn->dst_reg;
-
/* Check whether we can read the memory, with second call for fetch
* case to simulate the register fill.
*/
@@ -15561,8 +15595,14 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, struct bpf_insn
u32 dst = insn->dst_reg;
const char *reason;
int ret, bounds_ret;
+ bool to_scalar;
dst_reg = ®s[dst];
+ /*
+ * Only += and -= keep a pointer. Any other op makes a number of it,
+ * which is fine when the program may see values of pointers anyway.
+ */
+ to_scalar = opcode != BPF_ADD && opcode != BPF_SUB && env->allow_ptr_leaks;
if ((known && (smin_val != smax_val || umin_val != umax_val)) ||
smin_val > smax_val || umin_val > umax_val) {
@@ -15573,7 +15613,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, struct bpf_insn
return 0;
}
- if (BPF_CLASS(insn->code) != BPF_ALU64) {
+ if (BPF_CLASS(insn->code) != BPF_ALU64 && !to_scalar) {
/* 32-bit ALU ops on pointers produce (meaningless) scalars */
if (opcode == BPF_SUB && env->allow_ptr_leaks) {
__mark_reg_unknown(env, dst_reg);
@@ -15639,6 +15679,11 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, struct bpf_insn
return -EACCES;
}
+ if (to_scalar) {
+ __mark_reg_unknown(env, dst_reg);
+ return 0;
+ }
+
/* For 'scalar += pointer', dst_reg inherits the complete pointer
* register state. Individual fields may be adjusted later by pointer
* arithmetic. Callers guarantee that below does not overwrite off_reg.
@@ -19340,15 +19385,17 @@ static int do_check_insn(struct bpf_verifier_env *env, bool *do_print_state)
return check_stack_arg_write(env, state, insn->off, NULL);
}
+ struct bpf_reg_state arena, *dst_reg;
enum bpf_reg_type dst_reg_type;
err = check_reg_arg(env, insn->dst_reg, SRC_OP);
if (err)
return err;
- dst_reg_type = cur_regs(env)[insn->dst_reg].type;
+ dst_reg = mem_access_reg(env, insn->dst_reg, &arena);
+ dst_reg_type = dst_reg->type;
- err = check_mem_access(env, env->insn_idx, cur_regs(env) + insn->dst_reg, argno_from_reg(insn->dst_reg),
+ err = check_mem_access(env, env->insn_idx, dst_reg, argno_from_reg(insn->dst_reg),
insn->off, BPF_SIZE(insn->code),
BPF_WRITE, -1, false, false);
if (err)
@@ -22507,6 +22554,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,
if (is_priv)
env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ;
env->test_reg_invariants = attr->prog_flags & BPF_F_TEST_REG_INVARIANTS;
+ env->arena_scalar = attr->prog_flags & BPF_F_ARENA_SCALAR;
env->explored_states = kvzalloc_objs(struct list_head,
state_htab_size(env),
diff --git a/tools/bpf/bpftool/btf_dumper.c b/tools/bpf/bpftool/btf_dumper.c
index e4075824343fd..3267163119dc2 100644
--- a/tools/bpf/bpftool/btf_dumper.c
+++ b/tools/bpf/bpftool/btf_dumper.c
@@ -524,6 +524,15 @@ static int btf_dumper_var(const struct btf_dumper *d, __u32 type_id,
return ret;
}
+/*
+ * The compiler splits a variable into pieces. Every piece is a VAR with the
+ * type of the whole variable, so there is less data than the type takes.
+ */
+bool btf_var_is_piece(const struct btf *btf, const struct btf_var_secinfo *vsi)
+{
+ return btf__resolve_size(btf, vsi->type) > vsi->size;
+}
+
static int btf_dumper_datasec(const struct btf_dumper *d, __u32 type_id,
const void *data)
{
@@ -542,6 +551,8 @@ static int btf_dumper_datasec(const struct btf_dumper *d, __u32 type_id,
jsonw_name(d->jw, btf__name_by_offset(d->btf, t->name_off));
jsonw_start_array(d->jw);
for (i = 0; i < vlen; i++) {
+ if (btf_var_is_piece(d->btf, &vsi[i]))
+ continue;
ret = btf_dumper_do_type(d, vsi[i].type, 0, data + vsi[i].offset);
if (ret)
break;
diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h
index fa2c877a3cbfc..6851346a60305 100644
--- a/tools/bpf/bpftool/main.h
+++ b/tools/bpf/bpftool/main.h
@@ -121,6 +121,7 @@ struct obj_refs {
};
struct btf;
+struct btf_var_secinfo;
struct bpf_line_info;
int build_pinned_obj_table(struct hashmap *table,
@@ -239,6 +240,7 @@ int btf_dumper_type(const struct btf_dumper *d, __u32 type_id,
const void *data);
void btf_dumper_type_only(const struct btf *btf, __u32 func_type_id,
char *func_only, int size);
+bool btf_var_is_piece(const struct btf *btf, const struct btf_var_secinfo *vsi);
void btf_dump_linfo_plain(const struct btf *btf,
const struct bpf_line_info *linfo,
diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c
index a60414aaa4940..eb077236d2eaa 100644
--- a/tools/bpf/bpftool/prog.c
+++ b/tools/bpf/bpftool/prog.c
@@ -348,7 +348,7 @@ static void show_prog_metadata(int fd, __u32 num_maps)
t_var = btf__type_by_id(btf, vsi->type);
name = btf__name_by_offset(btf, t_var->name_off);
- if (!has_metadata_prefix(name))
+ if (!has_metadata_prefix(name) || btf_var_is_piece(btf, vsi))
continue;
if (!printed_header) {
@@ -377,7 +377,7 @@ static void show_prog_metadata(int fd, __u32 num_maps)
t_var = btf__type_by_id(btf, vsi->type);
name = btf__name_by_offset(btf, t_var->name_off);
- if (!has_metadata_prefix(name))
+ if (!has_metadata_prefix(name) || btf_var_is_piece(btf, vsi))
continue;
if (!printed_header) {
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 4687c33109968..e0ed44b1bbcb7 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -1344,6 +1344,12 @@ enum bpf_perf_event_type {
/* The verifier internal test flag. Behavior is undefined */
#define BPF_F_TEST_REG_INVARIANTS (1U << 7)
+/*
+ * Load and store through a number is an access to the arena of the program
+ * at the low 32 bits of the number. It's for programs written in Rust.
+ */
+#define BPF_F_ARENA_SCALAR (1U << 8)
+
/* link_create.kprobe_multi.flags used in LINK_CREATE command for
* BPF_TRACE_KPROBE_MULTI attach type to create return probe.
*/
diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h
index 9d160b5b9c0ee..b37b727ef216f 100644
--- a/tools/lib/bpf/bpf_helpers.h
+++ b/tools/lib/bpf/bpf_helpers.h
@@ -248,13 +248,21 @@ enum libbpf_tristate {
#define ___bpf_fill(arr, args...) \
___bpf_apply(___bpf_fill, ___bpf_narg(args))(arr, 0, args)
+/*
+ * Format strings are in a section of their own. When global data of the object
+ * is in arena (the object has .arena.data section) libbpf keeps .rodata.str*
+ * sections in read-only maps, which is where helpers and kfuncs take strings
+ * from.
+ */
+#define ___bpf_fmt_sec __attribute__((section(".rodata.str")))
+
/*
* BPF_SEQ_PRINTF to wrap bpf_seq_printf to-be-printed values
* in a structure.
*/
#define BPF_SEQ_PRINTF(seq, fmt, args...) \
({ \
- static const char ___fmt[] = fmt; \
+ static const char ___fmt[] ___bpf_fmt_sec = fmt; \
unsigned long long ___param[___bpf_narg(args)]; \
\
_Pragma("GCC diagnostic push") \
@@ -272,7 +280,7 @@ enum libbpf_tristate {
*/
#define BPF_SNPRINTF(out, out_size, fmt, args...) \
({ \
- static const char ___fmt[] = fmt; \
+ static const char ___fmt[] ___bpf_fmt_sec = fmt; \
unsigned long long ___param[___bpf_narg(args)]; \
\
_Pragma("GCC diagnostic push") \
@@ -287,7 +295,7 @@ enum libbpf_tristate {
#ifdef BPF_NO_GLOBAL_DATA
#define BPF_PRINTK_FMT_MOD
#else
-#define BPF_PRINTK_FMT_MOD static const
+#define BPF_PRINTK_FMT_MOD static const ___bpf_fmt_sec
#endif
#define __bpf_printk(fmt, ...) \
@@ -303,7 +311,7 @@ enum libbpf_tristate {
*/
#define __bpf_vprintk(fmt, args...) \
({ \
- static const char ___fmt[] = fmt; \
+ static const char ___fmt[] ___bpf_fmt_sec = fmt; \
unsigned long long ___param[___bpf_narg(args)]; \
\
_Pragma("GCC diagnostic push") \
@@ -317,7 +325,7 @@ enum libbpf_tristate {
#define bpf_stream_printk(stream_id, fmt, args...) \
({ \
- static const char ___fmt[] = fmt; \
+ static const char ___fmt[] ___bpf_fmt_sec = fmt; \
unsigned long long ___param[___bpf_narg(args)]; \
\
_Pragma("GCC diagnostic push") \
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index fdd69aac39bde..cb09ded907732 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -552,6 +552,26 @@ struct bpf_struct_ops {
#define STRUCT_OPS_SEC ".struct_ops"
#define STRUCT_OPS_LINK_SEC ".struct_ops.link"
#define ARENA_SEC ".addr_space.1"
+/*
+ * An object with this section keeps its global data in arena instead of
+ * array maps. What is in the section doesn't matter. .data, .bss and .rodata
+ * sections become a part of the arena map of the object, like __arena global
+ * variables are. If the object doesn't declare an arena map libbpf creates
+ * one that is just large enough for the data. bpf_object__find_map_by_name()
+ * finds it by the name "arena" and bpf_map__set_max_entries() changes its
+ * size before the object is loaded.
+ * Read-only sections with pointers to functions and sections with constant
+ * strings stay frozen array maps. A constant that a helper or a kfunc takes
+ * by pointer has to be in such section.
+ * Pointers to data that are stored in data become addresses of arena.
+ * The load fails if such pointer points to a section that is not in arena,
+ * or if the arena is pinned or reused.
+ *
+ * It's for programs written in Rust. There are no address spaces in Rust,
+ * the program accesses arena through plain numbers, so programs of the object
+ * are loaded with BPF_F_ARENA_SCALAR.
+ */
+#define ARENA_DATA_SEC ".arena.data"
enum libbpf_map_type {
LIBBPF_MAP_UNSPEC,
@@ -607,6 +627,13 @@ struct bpf_map {
/* pointers to functions in the data of an internal map, see obj->func_ptrs */
struct func_ptr *func_ptrs;
size_t func_ptr_cnt;
+ /*
+ * Data of the internal map is a part of arena data at arena_off.
+ * The map is not created, unless it's a copy that is in arena.
+ */
+ bool in_arena;
+ bool arena_copy;
+ size_t arena_off;
};
enum extern_type {
@@ -775,6 +802,17 @@ struct bpf_object {
void *arena_data;
size_t arena_data_sz;
size_t arena_data_off;
+ bool data_in_arena;
+ bool arena_mapped;
+ /* pointers to data in the data of internal maps, when data is in arena */
+ struct data_ptr {
+ int sec_idx;
+ size_t sec_off;
+ int targ_sec_idx;
+ size_t targ_off;
+ char *sym_name;
+ } *data_ptrs;
+ size_t data_ptr_cnt;
void *jumptables_data;
size_t jumptables_data_sz;
@@ -1600,6 +1638,7 @@ static struct bpf_object *bpf_object__new(const char *path,
obj->efile.obj_buf = obj_buf;
obj->efile.obj_buf_sz = obj_buf_sz;
obj->efile.btf_maps_shndx = -1;
+ obj->efile.arena_data_shndx = -1;
obj->kconfig_map_idx = -1;
obj->arena_map_idx = -1;
@@ -3081,6 +3120,136 @@ static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map,
return 0;
}
+static bool map_is_const_str(const struct bpf_map *map)
+{
+ return map->libbpf_type == LIBBPF_MAP_RODATA &&
+ str_has_pfx(map->real_name, RODATA_SEC ".str");
+}
+
+static bool map_data_goes_to_arena(const struct bpf_object *obj, const struct bpf_map *map)
+{
+ int i;
+
+ switch (map->libbpf_type) {
+ case LIBBPF_MAP_DATA:
+ case LIBBPF_MAP_BSS:
+ return true;
+ case LIBBPF_MAP_RODATA:
+ break;
+ default:
+ return false;
+ }
+
+ /*
+ * Const strings that kfuncs and helpers take have to be in a frozen
+ * map. Pointers to them that are stored in data point to a copy of
+ * the strings that is in arena.
+ */
+ if (map_is_const_str(map))
+ return true;
+ /*
+ * Read-only data with pointers stays a frozen map: the kernel
+ * recognizes pointers to functions in it.
+ */
+ if (str_has_pfx(map->real_name, DATA_REL_RO_SEC))
+ return false;
+ for (i = 0; i < obj->efile.sec_cnt; i++) {
+ const struct elf_sec_desc *sec = &obj->efile.secs[i];
+
+ if (sec->sec_type == SEC_RELO && sec->shdr->sh_info == map->sec_idx)
+ return false;
+ }
+ return true;
+}
+
+/* Returns the offset of the data of the map in arena data that is sz bytes so far */
+static size_t map_arena_off(const struct bpf_object *obj, const struct bpf_map *map, size_t sz)
+{
+ size_t align = obj->efile.secs[map->sec_idx].shdr->sh_addralign;
+
+ return roundup(sz, max(align, sizeof(__u64)));
+}
+
+/*
+ * Make .data, .bss and .rodata a part of arena data. They follow __arena
+ * variables of the object, if there are any.
+ */
+static int bpf_object__init_arena_data(struct bpf_object *obj)
+{
+ const size_t page_sz = sysconf(_SC_PAGE_SIZE);
+ size_t sz = obj->arena_data_sz;
+ struct bpf_map *map;
+ void *data;
+ int i;
+
+ if (!obj->data_in_arena)
+ return 0;
+
+ for (i = 0; i < obj->nr_maps; i++) {
+ map = &obj->maps[i];
+ if (map_data_goes_to_arena(obj, map))
+ sz = map_arena_off(obj, map, sz) + map->def.value_size;
+ }
+ if (sz == obj->arena_data_sz)
+ return 0;
+
+ if (obj->arena_map_idx < 0) {
+ /* bpf_object__init_user_btf_maps() didn't look: there is no .maps section */
+ if (obj->efile.arena_data) {
+ pr_warn("elf: sec '%s': to use global __arena variables the ARENA map should be explicitly declared in SEC(\".maps\")\n",
+ ARENA_SEC);
+ return -ENOENT;
+ }
+
+ map = bpf_object__add_map(obj);
+ if (IS_ERR(map))
+ return PTR_ERR(map);
+
+ map->real_name = strdup("arena");
+ map->name = strdup("arena");
+ if (!map->real_name || !map->name) {
+ zfree(&map->real_name);
+ zfree(&map->name);
+ return -ENOMEM;
+ }
+ map->sec_idx = -1;
+ map->def.type = BPF_MAP_TYPE_ARENA;
+ map->def.max_entries = roundup(sz, page_sz) / page_sz;
+ map->def.map_flags = BPF_F_MMAPABLE;
+ obj->arena_map_idx = map - obj->maps;
+ }
+
+ data = realloc(obj->arena_data, sz);
+ if (!data)
+ return -ENOMEM;
+ memset(data + obj->arena_data_sz, 0, sz - obj->arena_data_sz);
+ sz = obj->arena_data_sz;
+ obj->arena_data = data;
+ obj->maps[obj->arena_map_idx].mmaped = data;
+
+ for (i = 0; i < obj->nr_maps; i++) {
+ map = &obj->maps[i];
+ if (!map_data_goes_to_arena(obj, map))
+ continue;
+ sz = map_arena_off(obj, map, sz);
+ map->arena_off = sz;
+ memcpy(data + sz, map->mmaped, map->def.value_size);
+ sz += map->def.value_size;
+ pr_debug("map '%s': data is in arena at offset %zu\n", map->name, map->arena_off);
+ if (map_is_const_str(map)) {
+ map->arena_copy = true;
+ continue;
+ }
+ munmap(map->mmaped, bpf_map_mmap_sz(map));
+ /* make bpf_map__initial_value() and bpf_map__set_initial_value() work */
+ map->mmaped = data + map->arena_off;
+ map->autocreate = false;
+ map->in_arena = true;
+ }
+ obj->arena_data_sz = sz;
+ return 0;
+}
+
static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,
const char *pin_root_path)
{
@@ -3173,6 +3342,7 @@ static int bpf_object__init_maps(struct bpf_object *obj,
err = err ?: bpf_object__init_global_data_maps(obj);
err = err ?: bpf_object__init_kconfig_map(obj);
err = err ?: bpf_object_init_struct_ops(obj);
+ err = err ?: bpf_object__init_arena_data(obj);
return err;
}
@@ -3987,6 +4157,11 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
if (!sh)
return -LIBBPF_ERRNO__FORMAT;
+ /* the second pass has to know it when it sees relocations of data */
+ name = elf_sec_str(obj, sh->sh_name);
+ if (name && strcmp(name, ARENA_DATA_SEC) == 0)
+ obj->data_in_arena = true;
+
if (sh->sh_type == SHT_SYMTAB) {
if (obj->efile.symbols) {
pr_warn("elf: multiple symbol tables in %s\n", obj->path);
@@ -4103,6 +4278,8 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
} else if (strcmp(name, ARENA_SEC) == 0) {
obj->efile.arena_data = data;
obj->efile.arena_data_shndx = idx;
+ } else if (strcmp(name, ARENA_DATA_SEC) == 0) {
+ /* the marker, see the first pass */
} else if (strcmp(name, JUMPTABLES_SEC) == 0) {
obj->jumptables_data = malloc(data->d_size);
if (!obj->jumptables_data)
@@ -4127,6 +4304,9 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
* have pointers to functions.
*/
if (!section_have_execinstr(obj, targ_sec_idx) &&
+ !(obj->data_in_arena &&
+ (!strcmp(name, ".rel" DATA_SEC) ||
+ str_has_pfx(name, ".rel" DATA_SEC "."))) &&
strcmp(name, ".rel" RODATA_SEC) &&
!str_has_pfx(name, ".rel" RODATA_SEC ".") &&
strcmp(name, ".rel" DATA_REL_RO_SEC) &&
@@ -4878,6 +5058,10 @@ static int bpf_program__record_reloc(struct bpf_program *prog,
reloc_desc->insn_idx = insn_idx;
reloc_desc->map_idx = map_idx;
reloc_desc->sym_off = sym->st_value;
+ if (map->in_arena) {
+ reloc_desc->map_idx = obj->arena_map_idx;
+ reloc_desc->sym_off += map->arena_off;
+ }
return 0;
}
@@ -5096,6 +5280,9 @@ int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate)
if (map_is_created(map))
return libbpf_err(-EBUSY);
+ if (map->in_arena)
+ return libbpf_err(-EOPNOTSUPP);
+
map->autocreate = autocreate;
return 0;
}
@@ -5376,6 +5563,45 @@ bpf_object__reuse_map(struct bpf_map *map)
return 0;
}
+static struct bpf_map *bpf_object__sec_map(struct bpf_object *obj, int sec_idx)
+{
+ int i;
+
+ for (i = 0; i < obj->nr_maps; i++)
+ if (bpf_map__is_internal(&obj->maps[i]) && obj->maps[i].sec_idx == sec_idx)
+ return &obj->maps[i];
+ return NULL;
+}
+
+static int bpf_object__mmap_arena(struct bpf_object *obj, struct bpf_map *map)
+{
+ int i, err;
+
+ map->mmaped = mmap((void *)(long)map->map_extra,
+ bpf_map_mmap_sz(map), PROT_READ | PROT_WRITE,
+ map->map_extra ? MAP_SHARED | MAP_FIXED : MAP_SHARED,
+ map->fd, 0);
+ if (map->mmaped == MAP_FAILED) {
+ err = -errno;
+ map->mmaped = NULL;
+ pr_warn("map '%s': failed to mmap arena: %s\n",
+ map->name, errstr(err));
+ return err;
+ }
+ if (obj->arena_data) {
+ memcpy(map->mmaped + obj->arena_data_off, obj->arena_data,
+ obj->arena_data_sz);
+ zfree(&obj->arena_data);
+ }
+ for (i = 0; i < obj->nr_maps; i++) {
+ struct bpf_map *m = &obj->maps[i];
+
+ if (m->in_arena)
+ m->mmaped = map->mmaped + obj->arena_data_off + m->arena_off;
+ }
+ return 0;
+}
+
static int
bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)
{
@@ -5736,6 +5962,10 @@ bpf_object__create_maps(struct bpf_object *obj)
continue;
}
+ /* see bpf_object__relocate_data_ptrs() */
+ if (map->def.type == BPF_MAP_TYPE_ARENA && obj->arena_mapped)
+ continue;
+
err = map_set_def_max_entries(map);
if (err)
goto err_out;
@@ -5773,22 +6003,9 @@ bpf_object__create_maps(struct bpf_object *obj)
if (err < 0)
goto err_out;
} else if (map->def.type == BPF_MAP_TYPE_ARENA) {
- map->mmaped = mmap((void *)(long)map->map_extra,
- bpf_map_mmap_sz(map), PROT_READ | PROT_WRITE,
- map->map_extra ? MAP_SHARED | MAP_FIXED : MAP_SHARED,
- map->fd, 0);
- if (map->mmaped == MAP_FAILED) {
- err = -errno;
- map->mmaped = NULL;
- pr_warn("map '%s': failed to mmap arena: %s\n",
- map->name, errstr(err));
+ err = bpf_object__mmap_arena(obj, map);
+ if (err)
return err;
- }
- if (obj->arena_data) {
- memcpy(map->mmaped + obj->arena_data_off, obj->arena_data,
- obj->arena_data_sz);
- zfree(&obj->arena_data);
- }
}
if (map->init_slots_sz && map->def.type != BPF_MAP_TYPE_PROG_ARRAY) {
err = init_map_in_map_slots(obj, map);
@@ -7803,6 +8020,81 @@ static int bpf_program_fixup_func_info(struct bpf_object *obj, struct bpf_progra
return err;
}
+/*
+ * Turn pointers to data that are stored in data into addresses of arena.
+ * The arena is created here, ahead of the other maps: the address has to be
+ * known before the maps that hold the pointers are created.
+ */
+static int bpf_object__relocate_data_ptrs(struct bpf_object *obj)
+{
+ struct bpf_map *arena, *map, *targ;
+ __u64 addr, val;
+ size_t i;
+ int err;
+
+ if (!obj->data_ptr_cnt)
+ return 0;
+
+ for (i = 0; i < obj->data_ptr_cnt; i++) {
+ struct data_ptr *p = &obj->data_ptrs[i];
+
+ if (p->targ_sec_idx < 0)
+ continue;
+ map = bpf_object__sec_map(obj, p->sec_idx);
+ targ = bpf_object__sec_map(obj, p->targ_sec_idx);
+ if (map && (map->in_arena || map->autocreate) &&
+ (!targ || (!targ->in_arena && !targ->arena_copy))) {
+ pr_warn("sec '%s': pointer to '%s' at offset %zu can't be resolved: sec '%s' is not in arena\n",
+ map->real_name, p->sym_name, p->sec_off,
+ targ ? targ->real_name : "<?>");
+ return -LIBBPF_ERRNO__RELOC;
+ }
+ }
+
+ if (obj->gen_loader) {
+ pr_warn("pointers to data in data are not supported by light skeleton\n");
+ return -ENOTSUP;
+ }
+
+ if (obj->arena_map_idx < 0)
+ return 0;
+
+ arena = &obj->maps[obj->arena_map_idx];
+ if (!arena->autocreate)
+ return 0;
+ /* libbpf doesn't mmap an arena that it didn't create, so the address is not known */
+ if (arena->reused || arena->pin_path) {
+ pr_warn("map '%s': pointers to data in data are not supported with pinned or reused arena\n",
+ arena->name);
+ return -ENOTSUP;
+ }
+
+ err = bpf_object__create_map(obj, arena, false);
+ err = err ?: bpf_object__mmap_arena(obj, arena);
+ if (err) {
+ pr_warn("map '%s': failed to create: %s\n", arena->name, errstr(err));
+ return err;
+ }
+ obj->arena_mapped = true;
+
+ for (i = 0; i < obj->data_ptr_cnt; i++) {
+ struct data_ptr *p = &obj->data_ptrs[i];
+
+ map = bpf_object__sec_map(obj, p->sec_idx);
+ if (!map || (!map->in_arena && !map->autocreate))
+ continue;
+
+ addr = (__u64)(unsigned long)arena->mmaped + obj->arena_data_off + p->targ_off;
+ if (p->targ_sec_idx >= 0)
+ addr += bpf_object__sec_map(obj, p->targ_sec_idx)->arena_off;
+
+ memcpy(&val, map->mmaped + p->sec_off, sizeof(val));
+ val += addr;
+ memcpy(map->mmaped + p->sec_off, &val, sizeof(val));
+ }
+ return 0;
+}
+
static int bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path)
{
struct bpf_program *prog;
@@ -7825,8 +8117,10 @@ static int bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_pat
size_t mmap_sz = bpf_map_mmap_sz(arena_map);
if (data_sz > mmap_sz) {
- pr_warn("map '%s': declared ARENA map size (%zu) is too small to hold global __arena variables of size %zu\n",
- arena_map->name, mmap_sz, obj->arena_data_sz);
+ pr_warn("map '%s': declared ARENA map size (%zu) is too small to hold global %s of size %zu\n",
+ arena_map->name, mmap_sz,
+ obj->data_in_arena ? "data" : "__arena variables",
+ obj->arena_data_sz);
return -E2BIG;
}
@@ -7835,6 +8129,10 @@ static int bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_pat
obj->arena_data_off = mmap_sz - data_sz;
}
+ err = bpf_object__relocate_data_ptrs(obj);
+ if (err)
+ return err;
+
/* Before relocating calls pre-process relocations and mark
* few ld_imm64 instructions that points to subprogs.
* Otherwise bpf_object__reloc_code() later would have to consider
@@ -8067,12 +8365,85 @@ static int bpf_object__collect_map_relos(struct bpf_object *obj,
return 0;
}
+/*
+ * A pointer to data that is stored in data. When data is in arena
+ * the program dereferences what it loads from data, so the pointer has
+ * to be the address of the target in arena.
+ */
+static int bpf_object__collect_data_ptr(struct bpf_object *obj, const char *relo_sec_name,
+ int relo_idx, size_t sec_idx, const Elf64_Rel *rel,
+ const Elf64_Sym *sym)
+{
+ Elf_Data *scn_data = obj->efile.secs[sec_idx].data;
+ const char *sym_name = elf_sym_str(obj, sym->st_name) ?: "<?>";
+ struct data_ptr *ptrs;
+
+ if (ELF64_ST_TYPE(sym->st_info) == STT_SECTION && sym->st_shndx < obj->efile.sec_cnt)
+ sym_name = elf_sec_name(obj, elf_sec_by_idx(obj, sym->st_shndx)) ?: "<?>";
+
+ if (ELF64_R_TYPE(rel->r_info) != R_BPF_64_ABS64 ||
+ sym->st_shndx >= obj->efile.sec_cnt ||
+ (sym->st_shndx != obj->efile.arena_data_shndx &&
+ !bpf_object__shndx_is_data(obj, sym->st_shndx)) ||
+ rel->r_offset >= scn_data->d_size ||
+ scn_data->d_size - rel->r_offset < sizeof(__u64)) {
+ pr_warn("sec '%s': relo #%d: can't resolve pointer to '%s' at offset %zu when data is in arena\n",
+ relo_sec_name, relo_idx, sym_name, (size_t)rel->r_offset);
+ return -LIBBPF_ERRNO__RELOC;
+ }
+
+ ptrs = libbpf_reallocarray(obj->data_ptrs, obj->data_ptr_cnt + 1, sizeof(*ptrs));
+ if (!ptrs)
+ return -ENOMEM;
+ obj->data_ptrs = ptrs;
+
+ ptrs[obj->data_ptr_cnt].sec_idx = sec_idx;
+ ptrs[obj->data_ptr_cnt].sec_off = rel->r_offset;
+ /* __arena variables are at the start of arena data */
+ ptrs[obj->data_ptr_cnt].targ_sec_idx =
+ sym->st_shndx == obj->efile.arena_data_shndx ? -1 : sym->st_shndx;
+ ptrs[obj->data_ptr_cnt].targ_off = sym->st_value;
+ ptrs[obj->data_ptr_cnt].sym_name = strdup(sym_name);
+ if (!ptrs[obj->data_ptr_cnt].sym_name)
+ return -ENOMEM;
+ obj->data_ptr_cnt++;
+
+ pr_debug("sec '%s': relo #%d: pointer at offset %zu to '%s'\n",
+ relo_sec_name, relo_idx, (size_t)rel->r_offset, sym_name);
+ return 0;
+}
+
+/* Collect pointers in a data section that went to arena */
+static int bpf_object__collect_data_relos(struct bpf_object *obj,
+ Elf64_Shdr *shdr, Elf_Data *data)
+{
+ int i, err, nrels = shdr->sh_size / shdr->sh_entsize;
+ const char *relo_sec_name;
+ Elf64_Sym *sym;
+ Elf64_Rel *rel;
+
+ relo_sec_name = elf_sec_str(obj, shdr->sh_name) ?: "<?>";
+ for (i = 0; i < nrels; i++) {
+ rel = elf_rel_by_idx(data, i);
+ sym = rel ? elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info)) : NULL;
+ if (!sym) {
+ pr_warn("sec '%s': failed to get relo #%d\n", relo_sec_name, i);
+ return -LIBBPF_ERRNO__FORMAT;
+ }
+ err = bpf_object__collect_data_ptr(obj, relo_sec_name, i, shdr->sh_info, rel, sym);
+ if (err)
+ return err;
+ }
+ return 0;
+}
+
/*
* Collect pointers to functions in a read-only data section. They are
* R_BPF_64_ABS64 relocations against .text section, where the offset of
* a static function in the section is stored in place. Relocations in data
* sections were ignored before pointers to functions were supported. Those
- * that are something else, e.g. pointers to data, still are.
+ * that are something else, e.g. pointers to data, still are, unless data
+ * is in arena.
*/
static int bpf_object__collect_rodata_relos(struct bpf_object *obj,
Elf64_Shdr *shdr, Elf_Data *data)
@@ -8108,6 +8479,15 @@ static int bpf_object__collect_rodata_relos(struct bpf_object *obj,
if (ELF64_R_TYPE(rel->r_info) != R_BPF_64_ABS64 ||
!sym_is_subprog(sym, obj->efile.text_shndx)) {
+ int err;
+
+ if (obj->data_in_arena) {
+ err = bpf_object__collect_data_ptr(obj, relo_sec_name, i,
+ sec_idx, rel, sym);
+ if (err)
+ return err;
+ continue;
+ }
pr_debug("sec '%s': relo #%d: not a pointer to a function, skipping...\n",
relo_sec_name, i);
continue;
@@ -8183,6 +8563,8 @@ static int bpf_object__collect_relos(struct bpf_object *obj)
if (obj->efile.secs[idx].sec_type == SEC_RODATA)
err = bpf_object__collect_rodata_relos(obj, shdr, data);
+ else if (obj->efile.secs[idx].sec_type == SEC_DATA)
+ err = bpf_object__collect_data_relos(obj, shdr, data);
else if (obj->efile.secs[idx].sec_type == SEC_ST_OPS)
err = bpf_object__collect_st_ops_relos(obj, shdr, data);
else if (idx == obj->efile.btf_maps_shndx)
@@ -8476,6 +8858,9 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog
}
load_attr.log_level = log_level;
load_attr.prog_flags = prog->prog_flags;
+ /* the program accesses its data in arena through plain numbers */
+ if (obj->data_in_arena)
+ load_attr.prog_flags |= BPF_F_ARENA_SCALAR;
load_attr.fd_array = obj->fd_array;
load_attr.token_fd = obj->token_fd;
@@ -8547,7 +8932,7 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog
for (i = 0; i < obj->nr_maps; i++) {
map = &prog->obj->maps[i];
- if (map->libbpf_type != LIBBPF_MAP_RODATA)
+ if (map->libbpf_type != LIBBPF_MAP_RODATA || map->in_arena)
continue;
if (bpf_prog_bind_map(ret, map->fd, NULL)) {
@@ -10111,7 +10496,7 @@ static void bpf_map__destroy(struct bpf_map *map)
zfree(&map->init_slots);
map->init_slots_sz = 0;
- if (map->mmaped && map->mmaped != map->obj->arena_data)
+ if (map->mmaped && map->mmaped != map->obj->arena_data && !map->in_arena)
munmap(map->mmaped, bpf_map_mmap_sz(map));
map->mmaped = NULL;
@@ -10196,6 +10581,9 @@ void bpf_object__close(struct bpf_object *obj)
close(obj->func_ptr_maps[i].fd);
zfree(&obj->func_ptr_maps);
zfree(&obj->func_ptrs);
+ for (i = 0; i < obj->data_ptr_cnt; i++)
+ zfree(&obj->data_ptrs[i].sym_name);
+ zfree(&obj->data_ptrs);
if (obj->btf_module_allowlist) {
for (i = 0; i < obj->btf_module_allowlist_cnt; i++)
@@ -10556,6 +10944,8 @@ int bpf_program__clone(struct bpf_program *prog, const struct bpf_prog_load_opts
attr.token_fd = OPTS_GET(opts, token_fd, 0) ?: obj->token_fd;
if (attr.token_fd)
attr.prog_flags |= BPF_F_TOKEN_FD;
+ if (obj->data_in_arena)
+ attr.prog_flags |= BPF_F_ARENA_SCALAR;
prog_btf_fd = OPTS_GET(opts, prog_btf_fd, 0);
if (!prog_btf_fd && obj->btf)
@@ -11562,6 +11952,9 @@ int bpf_map__set_value_size(struct bpf_map *map, __u32 size)
if (map_is_created(map))
return libbpf_err(-EBUSY);
+ if (map->in_arena)
+ return libbpf_err(-EOPNOTSUPP);
+
if (map->mmaped) {
size_t mmap_old_sz, mmap_new_sz;
int err;
diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
index afa589a27b151..a22be7efd1fa2 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -422,6 +422,16 @@ $(LIBARENA_ASAN_SKEL): $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ) $(LIBARENA_BPF_DEPS)
+$(MAKE) -C libarena libarena_asan.skel.h $(LIBARENA_MAKE_ARGS)
endif
+# #![no_std] looks for compiler_builtins too. Nothing of it is used.
+ifneq ($(RUST_CORE),)
+$(RUST_CORE): $(RUST_CORE_SRC)
+ $(call msg,RUSTC,,$@)
+ $(Q)mkdir -p $(@D)
+ +$(Q)$(RUSTC_BPF) -A warnings --edition 2024 --crate-name core --out-dir $(@D) $<
+ +$(Q)echo '#![feature(compiler_builtins)] #![compiler_builtins] #![no_std]' | \
+ $(RUSTC_BPF) -A warnings --crate-name compiler_builtins --out-dir $(@D) -
+endif
+
# Generated test list headers
define gen_tests_hdr
@@ -457,7 +467,7 @@ RUNNER_PREREQS := $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ) $(BPFTOOL) \
$(VERIFY_SIG_HDR) $(PRIVATE_KEY) $(VERIFICATION_CERT) \
$(LIBARENA_SKEL) $(LIBARENA_ASAN_SKEL) \
prog_tests/tests.h map_tests/tests.h \
- $(RUNNER_OBJS)
+ $(RUNNER_OBJS) $(RUST_CORE)
# Runtime fixtures for each test_progs flavor.
RUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \
diff --git a/tools/testing/selftests/bpf/Makefile.buildvars b/tools/testing/selftests/bpf/Makefile.buildvars
index d2a0c0031b87d..ff3476bc40a49 100644
--- a/tools/testing/selftests/bpf/Makefile.buildvars
+++ b/tools/testing/selftests/bpf/Makefile.buildvars
@@ -109,6 +109,31 @@ HOST_INCLUDE_DIR := $(INCLUDE_DIR)
endif
RESOLVE_BTFIDS := $(HOST_BUILD_DIR)/resolve_btfids/resolve_btfids
+# Programs in Rust are built by upstream rustc. It has no prebuilt core for
+# the bpf target, so it has to come with the source of core:
+# rustup component add rust-src
+# core is built as edition 2024, which it is since rustc 1.87.
+# rustc emits LLVM bitcode and clang makes the object of it, so clang has to be
+# 23 or newer and not older than LLVM of rustc.
+# Otherwise RUST_CORE is empty and the tests are skipped.
+RUSTC ?= rustc
+RUST_CORE_SRC := $(wildcard $(shell $(RUSTC) --print sysroot 2>/dev/null)$\
+ /lib/rustlib/src/rust/library/core/src/lib.rs)
+ifneq ($(RUST_CORE_SRC),)
+ifeq ($(shell { clang=$$(echo __clang_major__ | $(CLANG) -E -P -x c -) && \
+ llvm=$$($(srctree)/scripts/rustc-llvm-version.sh $(RUSTC)) && \
+ [ $$($(srctree)/scripts/rustc-version.sh $(RUSTC)) -ge 108700 ] && \
+ [ $$clang -ge 23 ] && [ $$clang -ge $$((llvm / 10000)) ]; } \
+ 2>/dev/null && echo y),y)
+RUST_CORE := $(BUILD_DIR)/rust/libcore.rlib
+endif
+endif
+# RUSTC_BOOTSTRAP=1 is to build core with a stable rustc, like the kernel does.
+# panic=abort is a stop gap until panic=unwind is supported.
+RUSTC_BPF = RUSTC_BOOTSTRAP=1 $(RUSTC) -O -C panic=abort --crate-type rlib \
+ --target $(if $(IS_LITTLE_ENDIAN),bpfel,bpfeb)-unknown-none \
+ -L $(dir $(RUST_CORE))
+
DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool
ifneq ($(CROSS_COMPILE),)
CROSS_BPFTOOL := $(SCRATCH_DIR)/sbin/bpftool
diff --git a/tools/testing/selftests/bpf/Makefile.skel b/tools/testing/selftests/bpf/Makefile.skel
index 2e22bb901bf3b..06e297a17156c 100644
--- a/tools/testing/selftests/bpf/Makefile.skel
+++ b/tools/testing/selftests/bpf/Makefile.skel
@@ -20,7 +20,8 @@ ifneq ($(BPF_CC),)
BPF_SRCS := $(notdir $(wildcard progs/*.c))
BPF_OBJS := $(patsubst %.c,$(RDIR)/%.bpf.o,$(BPF_SRCS))
-SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c
+SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c \
+ data_in_arena_extern.c data_in_arena_nomap.c
LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \
linked_vars.skel.h linked_maps.skel.h linked_arena.skel.h \
@@ -137,4 +138,19 @@ $(LINKED_SKELS_H): $(RDIR)/%.skel.h: $$(addprefix $(RDIR)/,$$($$*.skel.h-deps))
$(BPFTOOL) $(GEN_SKEL) | $(RDIR)
$(Q)$(cmd_bpf_link_skel)
+# Programs in Rust, see Makefile.buildvars. No skeletons: the objects may be absent.
+ifneq ($(RUST_CORE),)
+ifeq ($(BPF_CC),$(CLANG))
+RUST_OBJS := $(patsubst progs/%.rs,$(RDIR)/%.bpf.o,$(wildcard progs/*.rs))
+BPF_OBJS += $(RUST_OBJS)
+
+$(RUST_OBJS): $(RDIR)/%.bpf.o: progs/%.rs $(RUST_CORE) | $(RDIR)
+ $(call msg,RUSTC,$(BINARY),$@)
+ +$(Q)$(RUSTC_BPF) --edition 2021 -C debuginfo=2 --emit=llvm-bc \
+ $(patsubst -mcpu=%,-C target-cpu=%,$(filter -mcpu=%,$(BPF_CC_FLAGS))) \
+ -o $(dir $(RUST_CORE))$(BINARY)-$*.bc $< && \
+ $(BPF_CC) $(BPF_CC_FLAGS) -c $(dir $(RUST_CORE))$(BINARY)-$*.bc -o $@ $(call skip_on_fail,BPF)
+endif
+endif
+
endif # BPF_CC
diff --git a/tools/testing/selftests/bpf/prog_tests/arena_scalar_blinded.c b/tools/testing/selftests/bpf/prog_tests/arena_scalar_blinded.c
new file mode 100644
index 0000000000000..2ac2e9a652fec
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/arena_scalar_blinded.c
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include "sysctl_helpers.h"
+#include "verifier_arena_scalar.skel.h"
+
+/* The same tests with constants of the programs blinded */
+void serial_test_arena_scalar_blinded(void)
+{
+ const char *harden = "/proc/sys/net/core/bpf_jit_harden";
+ char old[16] = {};
+
+ if (!is_jit_enabled()) {
+ test__skip();
+ return;
+ }
+ if (sysctl_set_or_fail(harden, old, "2"))
+ return;
+ RUN_TESTS(verifier_arena_scalar);
+ sysctl_set_or_fail(harden, NULL, old);
+}
diff --git a/tools/testing/selftests/bpf/prog_tests/btf.c b/tools/testing/selftests/bpf/prog_tests/btf.c
index df6ad38d287da..24ab62b2834ae 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf.c
@@ -424,7 +424,7 @@ static struct btf_raw_test raw_tests[] = {
.err_str = "Invalid type",
},
{
- .descr = "global data test #8, invalid var size",
+ .descr = "global data test #8, var is smaller than its type",
.raw_types = {
/* int */
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
@@ -457,8 +457,6 @@ static struct btf_raw_test raw_tests[] = {
.key_type_id = 0,
.value_type_id = 7,
.max_entries = 1,
- .btf_load_err = true,
- .err_str = "Invalid size",
},
{
.descr = "global data test #9, invalid var size",
@@ -498,7 +496,7 @@ static struct btf_raw_test raw_tests[] = {
.err_str = "Invalid size",
},
{
- .descr = "global data test #10, invalid var size",
+ .descr = "global data test #10, section is smaller than map value",
.raw_types = {
/* int */
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
@@ -531,8 +529,7 @@ static struct btf_raw_test raw_tests[] = {
.key_type_id = 0,
.value_type_id = 7,
.max_entries = 1,
- .btf_load_err = true,
- .err_str = "Invalid size",
+ .map_create_err = true,
},
{
.descr = "global data test #11, multiple section members",
@@ -1987,14 +1984,14 @@ static struct btf_raw_test raw_tests[] = {
},
{
- .descr = "typedef (invalid name, invalid identifier)",
+ .descr = "typedef (invalid name, not printable)",
.raw_types = {
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
BTF_TYPEDEF_ENC(NAME_TBD, 1), /* [2] */
BTF_END_RAW,
},
- .str_sec = "\0__!int",
- .str_sec_size = sizeof("\0__!int"),
+ .str_sec = "\0__\7int",
+ .str_sec_size = sizeof("\0__\7int"),
.map_type = BPF_MAP_TYPE_ARRAY,
.map_name = "typedef_check_btf",
.key_size = sizeof(int),
@@ -2112,15 +2109,15 @@ static struct btf_raw_test raw_tests[] = {
},
{
- .descr = "fwd type (invalid name, invalid identifier)",
+ .descr = "fwd type (invalid name, not printable)",
.raw_types = {
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
BTF_TYPE_ENC(NAME_TBD,
BTF_INFO_ENC(BTF_KIND_FWD, 0, 0), 0), /* [2] */
BTF_END_RAW,
},
- .str_sec = "\0__!skb",
- .str_sec_size = sizeof("\0__!skb"),
+ .str_sec = "\0__\7skb",
+ .str_sec_size = sizeof("\0__\7skb"),
.map_type = BPF_MAP_TYPE_ARRAY,
.map_name = "fwd_type_check_btf",
.key_size = sizeof(int),
@@ -2175,7 +2172,7 @@ static struct btf_raw_test raw_tests[] = {
},
{
- .descr = "struct type (invalid name, invalid identifier)",
+ .descr = "struct type (invalid name, not printable)",
.raw_types = {
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
BTF_TYPE_ENC(NAME_TBD,
@@ -2183,8 +2180,8 @@ static struct btf_raw_test raw_tests[] = {
BTF_MEMBER_ENC(NAME_TBD, 1, 0),
BTF_END_RAW,
},
- .str_sec = "\0A!\0B",
- .str_sec_size = sizeof("\0A!\0B"),
+ .str_sec = "\0A\7\0B",
+ .str_sec_size = sizeof("\0A\7\0B"),
.map_type = BPF_MAP_TYPE_ARRAY,
.map_name = "struct_type_check_btf",
.key_size = sizeof(int),
@@ -2217,7 +2214,7 @@ static struct btf_raw_test raw_tests[] = {
},
{
- .descr = "struct member (invalid name, invalid identifier)",
+ .descr = "struct member (invalid name, not printable)",
.raw_types = {
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
BTF_TYPE_ENC(NAME_TBD,
@@ -2225,8 +2222,8 @@ static struct btf_raw_test raw_tests[] = {
BTF_MEMBER_ENC(NAME_TBD, 1, 0),
BTF_END_RAW,
},
- .str_sec = "\0A\0B*",
- .str_sec_size = sizeof("\0A\0B*"),
+ .str_sec = "\0A\0B\7",
+ .str_sec_size = sizeof("\0A\0B\7"),
.map_type = BPF_MAP_TYPE_ARRAY,
.map_name = "struct_type_check_btf",
.key_size = sizeof(int),
@@ -2260,7 +2257,7 @@ static struct btf_raw_test raw_tests[] = {
},
{
- .descr = "enum type (invalid name, invalid identifier)",
+ .descr = "enum type (invalid name, not printable)",
.raw_types = {
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
BTF_TYPE_ENC(NAME_TBD,
@@ -2269,8 +2266,8 @@ static struct btf_raw_test raw_tests[] = {
BTF_ENUM_ENC(NAME_TBD, 0),
BTF_END_RAW,
},
- .str_sec = "\0A!\0B",
- .str_sec_size = sizeof("\0A!\0B"),
+ .str_sec = "\0A\7\0B",
+ .str_sec_size = sizeof("\0A\7\0B"),
.map_type = BPF_MAP_TYPE_ARRAY,
.map_name = "enum_type_check_btf",
.key_size = sizeof(int),
@@ -2306,7 +2303,7 @@ static struct btf_raw_test raw_tests[] = {
},
{
- .descr = "enum member (invalid name, invalid identifier)",
+ .descr = "enum member (invalid name, not printable)",
.raw_types = {
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
BTF_TYPE_ENC(0,
@@ -2315,8 +2312,8 @@ static struct btf_raw_test raw_tests[] = {
BTF_ENUM_ENC(NAME_TBD, 0),
BTF_END_RAW,
},
- .str_sec = "\0A!",
- .str_sec_size = sizeof("\0A!"),
+ .str_sec = "\0A\7",
+ .str_sec_size = sizeof("\0A\7"),
.map_type = BPF_MAP_TYPE_ARRAY,
.map_name = "enum_type_check_btf",
.key_size = sizeof(int),
@@ -2625,14 +2622,14 @@ static struct btf_raw_test raw_tests[] = {
.raw_types = {
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [2] */
- /* void (*)(int a, unsigned int !!!) */
+ /* void (*)(int a, unsigned int \7) */
BTF_FUNC_PROTO_ENC(0, 2), /* [3] */
BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
BTF_END_RAW,
},
- .str_sec = "\0a\0!!!",
- .str_sec_size = sizeof("\0a\0!!!"),
+ .str_sec = "\0a\0\7",
+ .str_sec_size = sizeof("\0a\0\7"),
.map_type = BPF_MAP_TYPE_ARRAY,
.map_name = "func_proto_type_check_btf",
.key_size = sizeof(int),
@@ -2775,12 +2772,12 @@ static struct btf_raw_test raw_tests[] = {
BTF_FUNC_PROTO_ENC(0, 2), /* [3] */
BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
- /* void !!!(int a, unsigned int b) */
+ /* void \7(int a, unsigned int b) */
BTF_FUNC_ENC(NAME_TBD, 3), /* [4] */
BTF_END_RAW,
},
- .str_sec = "\0a\0b\0!!!",
- .str_sec_size = sizeof("\0a\0b\0!!!"),
+ .str_sec = "\0a\0b\0\7",
+ .str_sec_size = sizeof("\0a\0b\0\7"),
.map_type = BPF_MAP_TYPE_ARRAY,
.map_name = "func_type_check_btf",
.key_size = sizeof(int),
@@ -2793,7 +2790,7 @@ static struct btf_raw_test raw_tests[] = {
},
{
- .descr = "func (Some arg has no name)",
+ .descr = "func (Some arg of global func has no name)",
.raw_types = {
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [2] */
@@ -2802,7 +2799,8 @@ static struct btf_raw_test raw_tests[] = {
BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
BTF_FUNC_PROTO_ARG_ENC(0, 2),
/* void func(int a, unsigned int) */
- BTF_FUNC_ENC(NAME_TBD, 3), /* [4] */
+ BTF_TYPE_ENC(NAME_TBD, /* [4] */
+ BTF_INFO_ENC(BTF_KIND_FUNC, 0, BTF_FUNC_GLOBAL), 3),
BTF_END_RAW,
},
.str_sec = "\0a\0func",
@@ -2818,6 +2816,57 @@ static struct btf_raw_test raw_tests[] = {
.err_str = "Invalid arg#2",
},
+{
+ .descr = "func (Some arg of static func has no name)",
+ .raw_types = {
+ /* int */
+ BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
+ /* unsigned int */
+ BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [2] */
+ /* void (*)(int a, unsigned int) */
+ BTF_FUNC_PROTO_ENC(0, 2), /* [3] */
+ BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
+ BTF_FUNC_PROTO_ARG_ENC(0, 2),
+ /* static void func(int a, unsigned int) */
+ BTF_FUNC_ENC(NAME_TBD, 3), /* [4] */
+ BTF_END_RAW,
+ },
+ .str_sec = "\0a\0func",
+ .str_sec_size = sizeof("\0a\0func"),
+ .map_type = BPF_MAP_TYPE_ARRAY,
+ .map_name = "func_type_check_btf",
+ .key_size = sizeof(int),
+ .value_size = sizeof(int),
+ .key_type_id = 1,
+ .value_type_id = 1,
+ .max_entries = 4,
+},
+
+{
+ .descr = "func (vararg of global func has no name)",
+ .raw_types = {
+ /* int */
+ BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
+ /* void (*)(int a, ...) */
+ BTF_FUNC_PROTO_ENC(0, 2), /* [2] */
+ BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
+ BTF_FUNC_PROTO_ARG_ENC(0, 0),
+ /* void func(int a, ...) */
+ BTF_TYPE_ENC(NAME_TBD, /* [3] */
+ BTF_INFO_ENC(BTF_KIND_FUNC, 0, BTF_FUNC_GLOBAL), 2),
+ BTF_END_RAW,
+ },
+ .str_sec = "\0a\0func",
+ .str_sec_size = sizeof("\0a\0func"),
+ .map_type = BPF_MAP_TYPE_ARRAY,
+ .map_name = "func_type_check_btf",
+ .key_size = sizeof(int),
+ .value_size = sizeof(int),
+ .key_type_id = 1,
+ .value_type_id = 1,
+ .max_entries = 4,
+},
+
{
.descr = "func (Non zero vlen)",
.raw_types = {
@@ -3585,15 +3634,28 @@ static struct btf_raw_test raw_tests[] = {
.btf_load_err = true,
},
{
- .descr = "type name '?foo' is not ok",
+ .descr = "type name '?foo' is ok",
.raw_types = {
/* union ?foo; */
BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_FWD, 1, 0), 0), /* [1] */
BTF_END_RAW,
},
BTF_STR_SEC("\0?foo"),
- .err_str = "Invalid name",
- .btf_load_err = true,
+},
+{
+ .descr = "names of Rust types and functions are ok",
+ .raw_types = {
+ BTF_TYPE_INT_ENC(NAME_NTH(1), 0, 0, 32, 4), /* [1] */
+ BTF_STRUCT_ENC(NAME_NTH(2), 1, 4), /* [2] */
+ BTF_MEMBER_ENC(NAME_NTH(3), 1, 0),
+ BTF_FWD_ENC(NAME_NTH(4), 0), /* [3] */
+ BTF_TYPEDEF_ENC(NAME_NTH(5), 2), /* [4] */
+ BTF_FUNC_PROTO_ENC(0, 1), /* [5] */
+ BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(6), 1),
+ BTF_FUNC_ENC(NAME_NTH(7), 5), /* [6] */
+ BTF_END_RAW,
+ },
+ BTF_STR_SEC("\0u32\0Option<&str>\0__0\0*const str\0{impl#9}<[u8; 4]>\0self\0fmt<str>"),
},
{
diff --git a/tools/testing/selftests/bpf/prog_tests/btf_rust.c b/tools/testing/selftests/bpf/prog_tests/btf_rust.c
new file mode 100644
index 0000000000000..daf777cadfda3
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/btf_rust.c
@@ -0,0 +1,142 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include <bpf/btf.h>
+#include "bpftool_helpers.h"
+
+#define FUNC_NAME "write_fmt<scx_simple::BpfStream>"
+#define KSYM_NAME "write_fmt_scx_simple__BpfStream_"
+
+/* The name of a function of Rust is a part of the name of the program in kallsyms */
+static void test_func_name(void)
+{
+ struct bpf_insn insns[] = {
+ BPF_MOV64_IMM(BPF_REG_0, 0),
+ BPF_EXIT_INSN(),
+ };
+ LIBBPF_OPTS(bpf_prog_load_opts, opts);
+ int int_id, proto_id, prog_fd = -1, i;
+ struct bpf_func_info func_info = {};
+ struct bpf_prog_info info = {};
+ unsigned long long addr;
+ __u32 len = sizeof(info);
+ char sym[128], *p = sym;
+ struct btf *btf;
+
+ btf = btf__new_empty();
+ if (!ASSERT_OK_PTR(btf, "btf"))
+ return;
+ int_id = btf__add_int(btf, "i32", 4, BTF_INT_SIGNED);
+ ASSERT_GT(int_id, 0, "int");
+ proto_id = btf__add_func_proto(btf, int_id);
+ ASSERT_GT(proto_id, 0, "proto");
+ ASSERT_OK(btf__add_func_param(btf, "ctx", int_id), "param");
+ func_info.type_id = btf__add_func(btf, FUNC_NAME, BTF_FUNC_STATIC, proto_id);
+ ASSERT_GT(func_info.type_id, 0, "func");
+ if (!ASSERT_OK(btf__load_into_kernel(btf), "btf load"))
+ goto out;
+
+ opts.prog_btf_fd = btf__fd(btf);
+ opts.func_info = &func_info;
+ opts.func_info_cnt = 1;
+ opts.func_info_rec_size = sizeof(func_info);
+ prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", insns,
+ ARRAY_SIZE(insns), &opts);
+ if (!ASSERT_GE(prog_fd, 0, "prog load"))
+ goto out;
+ if (!ASSERT_OK(bpf_prog_get_info_by_fd(prog_fd, &info, &len), "prog info"))
+ goto out;
+ if (!info.jited_prog_len) {
+ test__skip();
+ goto out;
+ }
+
+ p += sprintf(p, "bpf_prog_");
+ for (i = 0; i < BPF_TAG_SIZE; i++)
+ p += sprintf(p, "%02x", info.tag[i]);
+ sprintf(p, "_%s", KSYM_NAME);
+ ASSERT_OK(kallsyms_find(sym, &addr), sym);
+out:
+ if (prog_fd >= 0)
+ close(prog_fd);
+ btf__free(btf);
+}
+
+#define PIN_PATH "/sys/fs/bpf/btf_rust_piece"
+
+/*
+ * A piece of a static that LLVM split has the type of the whole static.
+ * It's the last variable in the section, so its type ends past the map value.
+ */
+static void test_piece(void)
+{
+ LIBBPF_OPTS(bpf_map_create_opts, opts);
+ int int_id, struct_id, var_id, piece_id, sec_id, map_fd = -1, key = 0;
+ __u32 value[2] = { 0x11111111, 0x22222222 };
+ char line[256] = {}, out[1024] = {};
+ struct btf *btf;
+ FILE *f = NULL;
+
+ btf = btf__new_empty();
+ if (!ASSERT_OK_PTR(btf, "btf"))
+ return;
+ int_id = btf__add_int(btf, "u32", 4, 0);
+ ASSERT_GT(int_id, 0, "int");
+ struct_id = btf__add_struct(btf, "Whole", 8);
+ ASSERT_GT(struct_id, 0, "struct");
+ ASSERT_OK(btf__add_field(btf, "a", int_id, 0, 0), "field");
+ ASSERT_OK(btf__add_field(btf, "b", int_id, 32, 0), "field");
+ var_id = btf__add_var(btf, "CNT", BTF_VAR_STATIC, int_id);
+ ASSERT_GT(var_id, 0, "var");
+ piece_id = btf__add_var(btf, "WHOLE.1", BTF_VAR_STATIC, struct_id);
+ ASSERT_GT(piece_id, 0, "piece");
+ sec_id = btf__add_datasec(btf, ".bss", sizeof(value));
+ ASSERT_GT(sec_id, 0, "datasec");
+ ASSERT_OK(btf__add_datasec_var_info(btf, var_id, 0, 4), "var info");
+ ASSERT_OK(btf__add_datasec_var_info(btf, piece_id, 4, 4), "piece info");
+ if (!ASSERT_OK(btf__load_into_kernel(btf), "btf load"))
+ goto out;
+
+ opts.btf_fd = btf__fd(btf);
+ opts.btf_value_type_id = sec_id;
+ map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, ".bss", sizeof(key), sizeof(value), 1, &opts);
+ if (!ASSERT_GE(map_fd, 0, "map create"))
+ goto out;
+ if (!ASSERT_OK(bpf_map_update_elem(map_fd, &key, value, 0), "map update"))
+ goto out;
+
+ /* the variable is printed, the piece is not */
+ unlink(PIN_PATH);
+ if (!ASSERT_OK(bpf_obj_pin(map_fd, PIN_PATH), "pin"))
+ goto out;
+ f = fopen(PIN_PATH, "r");
+ if (!ASSERT_OK_PTR(f, "open"))
+ goto out;
+ while (fgets(line, sizeof(line), f) && line[0] == '#')
+ ;
+ ASSERT_HAS_SUBSTR(line, "286331153", "var");
+ ASSERT_NULL(strstr(line, "572662306"), "piece");
+
+ /* the same for bpftool */
+ if (!ASSERT_OK(get_bpftool_command_output("map dump pinned " PIN_PATH, out, sizeof(out)),
+ "bpftool"))
+ goto out;
+ ASSERT_HAS_SUBSTR(out, "CNT", "var");
+ ASSERT_NULL(strstr(out, "WHOLE.1"), "piece");
+out:
+ if (f)
+ fclose(f);
+ unlink(PIN_PATH);
+ if (map_fd >= 0)
+ close(map_fd);
+ btf__free(btf);
+}
+
+/* Serial: programs are not in kallsyms while another test sets bpf_jit_harden */
+void serial_test_btf_rust(void)
+{
+ if (test__start_subtest("func_name"))
+ test_func_name();
+ if (test__start_subtest("piece"))
+ test_piece();
+}
diff --git a/tools/testing/selftests/bpf/prog_tests/data_in_arena.c b/tools/testing/selftests/bpf/prog_tests/data_in_arena.c
new file mode 100644
index 0000000000000..cb1023507c017
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/data_in_arena.c
@@ -0,0 +1,216 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include "data_in_arena.skel.h"
+#include "data_in_arena_decl.skel.h"
+#include "data_in_arena_fail.skel.h"
+
+static int run_prog(struct bpf_program *prog)
+{
+ LIBBPF_OPTS(bpf_test_run_opts, topts);
+
+ if (!ASSERT_OK(bpf_prog_test_run_opts(bpf_program__fd(prog), &topts), "test_run"))
+ return -1;
+ return topts.retval;
+}
+
+static void run(struct data_in_arena *skel, int counter)
+{
+ int i;
+
+ ASSERT_EQ(run_prog(skel->progs.use_data), counter + 7 + 1 + 2, "retval");
+ ASSERT_EQ(skel->bss->sum, counter + 7 + 1 + 2, "sum");
+ ASSERT_EQ(skel->data->counter, counter + 1, "counter");
+ ASSERT_EQ(skel->data->pair[1], 5, "pair[1]");
+ for (i = 0; i < 4; i++)
+ ASSERT_EQ(skel->bss->table[i], 10 * (i + 1) + i, "table");
+}
+
+static void test_in_arena(void)
+{
+ struct bpf_map_info info = {};
+ struct data_in_arena *skel;
+ __u32 len = sizeof(info);
+ struct bpf_map *arena;
+ size_t sz;
+
+ skel = data_in_arena__open();
+ if (!ASSERT_OK_PTR(skel, "open"))
+ return;
+
+ arena = bpf_object__find_map_by_name(skel->obj, "arena");
+ if (!ASSERT_OK_PTR(arena, "arena"))
+ goto out;
+ ASSERT_EQ(bpf_map__type(arena), BPF_MAP_TYPE_ARENA, "arena type");
+ ASSERT_EQ(bpf_map__max_entries(arena), 1, "arena pages");
+ ASSERT_OK(bpf_map__set_max_entries(arena, 8), "arena resize");
+ ASSERT_FALSE(bpf_map__autocreate(skel->maps.data), "data autocreate");
+ ASSERT_FALSE(bpf_map__autocreate(skel->maps.bss), "bss autocreate");
+ ASSERT_FALSE(bpf_map__autocreate(skel->maps.rodata), "rodata autocreate");
+ ASSERT_EQ(bpf_map__set_autocreate(skel->maps.data, true), -EOPNOTSUPP, "set_autocreate");
+ ASSERT_EQ(bpf_map__set_value_size(skel->maps.bss, 4096), -EOPNOTSUPP, "set_value_size");
+ ASSERT_EQ(bpf_map__initial_value(skel->maps.data, &sz), skel->data, "initial_value");
+ ASSERT_EQ(sz, sizeof(*skel->data), "initial_value size");
+
+ /* initial values are set the usual way */
+ skel->data->counter = 100;
+
+ if (!ASSERT_OK(data_in_arena__load(skel), "load"))
+ goto out;
+ /* there are no maps behind the sections */
+ ASSERT_ERR(bpf_map_get_info_by_fd(bpf_map__fd(skel->maps.data), &info, &len), "data map");
+ ASSERT_ERR(bpf_map_get_info_by_fd(bpf_map__fd(skel->maps.bss), &info, &len), "bss map");
+ ASSERT_ERR(bpf_map_get_info_by_fd(bpf_map__fd(skel->maps.rodata), &info, &len),
+ "rodata map");
+ run(skel, 100);
+
+ /* pointers to data next to pointers to functions */
+ ASSERT_EQ(run_prog(skel->progs.use_ops), 42 + 1 + 'e', "use_ops");
+ ASSERT_EQ(skel->data->counter, 102, "counter");
+
+ /* alignment of sections and pointers to data in data */
+ ASSERT_EQ((unsigned long)&skel->bss->aligned64 % 64, 0, "alignment");
+ ASSERT_EQ(run_prog(skel->progs.use_ptrs), 0, "use_ptrs");
+ ASSERT_EQ(skel->data->x, 43, "x");
+ ASSERT_EQ(skel->bss->aligned64.v[7], 7, "aligned64");
+ ASSERT_EQ(skel->data->px, &skel->data->x, "px");
+
+ /* format strings of bpf_printk() and BPF_SNPRINTF() */
+ ASSERT_EQ(run_prog(skel->progs.use_printk), sizeof("43-7"), "use_printk");
+ ASSERT_STREQ(skel->bss->out, "43-7", "out");
+out:
+ data_in_arena__destroy(skel);
+}
+
+/* The object has an arena map and __arena variables */
+static void test_declared_arena(void)
+{
+ struct data_in_arena_decl *skel;
+ struct bpf_map *map;
+ int arenas = 0;
+
+ skel = data_in_arena_decl__open();
+ if (!ASSERT_OK_PTR(skel, "open"))
+ return;
+ bpf_object__for_each_map(map, skel->obj)
+ arenas += bpf_map__type(map) == BPF_MAP_TYPE_ARENA;
+ ASSERT_EQ(arenas, 1, "no second arena");
+ skel->data->counter = 6;
+ if (!ASSERT_OK(data_in_arena_decl__load(skel), "load"))
+ goto out;
+ ASSERT_EQ(run_prog(skel->progs.use_data), 6 + 7 + 11, "retval");
+ ASSERT_EQ(run_prog(skel->progs.use_data), 7 + 7 + 12, "retval");
+ ASSERT_EQ(skel->bss->sum, 7 + 7 + 12, "sum");
+ ASSERT_EQ(skel->data->counter, 8, "counter");
+out:
+ data_in_arena_decl__destroy(skel);
+}
+
+/* A pointer in data that can't be made an address of arena fails the load */
+static void test_ptr_to_map(void)
+{
+ struct data_in_arena_fail *skel;
+
+ skel = data_in_arena_fail__open();
+ if (!ASSERT_OK_PTR(skel, "open"))
+ return;
+ ASSERT_ERR(data_in_arena_fail__load(skel), "load");
+ data_in_arena_fail__destroy(skel);
+}
+
+/* So does a pointer to a variable of the kernel. There is no skeleton: the open fails. */
+static void test_ptr_to_extern(void)
+{
+ struct bpf_object *obj;
+
+ obj = bpf_object__open_file("./data_in_arena_extern.bpf.o", NULL);
+ if (!ASSERT_ERR_PTR(obj, "open"))
+ bpf_object__close(obj);
+}
+
+/* __arena variables and no arena map. There is no skeleton: the open fails. */
+static void test_arena_var_no_map(void)
+{
+ struct bpf_object *obj;
+
+ obj = bpf_object__open_file("./data_in_arena_nomap.bpf.o", NULL);
+ if (!ASSERT_ERR_PTR(obj, "open"))
+ bpf_object__close(obj);
+}
+
+/* The address of an arena that libbpf doesn't create is not known. No pointers in data then. */
+static void test_not_my_arena(bool pin)
+{
+ LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_MMAPABLE);
+ struct data_in_arena *skel;
+ struct bpf_map *arena;
+ int fd = -1;
+
+ skel = data_in_arena__open();
+ if (!ASSERT_OK_PTR(skel, "open"))
+ return;
+ arena = bpf_object__find_map_by_name(skel->obj, "arena");
+ if (!ASSERT_OK_PTR(arena, "arena"))
+ goto out;
+ if (pin) {
+ ASSERT_OK(bpf_map__set_pin_path(arena, "/sys/fs/bpf/data_in_arena"), "pin_path");
+ } else {
+ fd = bpf_map_create(BPF_MAP_TYPE_ARENA, "arena", 0, 0, 1, &opts);
+ if (!ASSERT_GE(fd, 0, "map_create"))
+ goto out;
+ ASSERT_OK(bpf_map__reuse_fd(arena, fd), "reuse_fd");
+ }
+ ASSERT_EQ(data_in_arena__load(skel), -ENOTSUP, "load");
+out:
+ if (fd >= 0)
+ close(fd);
+ data_in_arena__destroy(skel);
+}
+
+/* The object is there if rustc and clang can build it, see Makefile.buildvars */
+static void test_rust(void)
+{
+ const char *file = "./data_in_arena_rust.bpf.o";
+ struct bpf_object *obj;
+
+ if (access(file, R_OK)) {
+ test__skip();
+ return;
+ }
+ obj = bpf_object__open_file(file, NULL);
+ if (!ASSERT_OK_PTR(obj, "open"))
+ return;
+ if (!ASSERT_OK(bpf_object__load(obj), "load"))
+ goto out;
+ /* libbpf goes on without BTF when the kernel doesn't take it */
+ ASSERT_GE(bpf_object__btf_fd(obj), 0, "btf_fd");
+ ASSERT_EQ(run_prog(bpf_object__find_program_by_name(obj, "list_in_data")),
+ 100 + 20 + 3, "retval");
+out:
+ bpf_object__close(obj);
+}
+
+void test_data_in_arena(void)
+{
+#if !defined(__x86_64__) && !defined(__aarch64__)
+ /* other JITs don't take BPF_F_ARENA_SCALAR */
+ test__skip();
+ return;
+#endif
+ if (test__start_subtest("arena"))
+ test_in_arena();
+ if (test__start_subtest("declared_arena"))
+ test_declared_arena();
+ if (test__start_subtest("ptr_to_map"))
+ test_ptr_to_map();
+ if (test__start_subtest("ptr_to_extern"))
+ test_ptr_to_extern();
+ if (test__start_subtest("arena_var_no_map"))
+ test_arena_var_no_map();
+ if (test__start_subtest("pinned_arena"))
+ test_not_my_arena(true);
+ if (test__start_subtest("reused_arena"))
+ test_not_my_arena(false);
+ if (test__start_subtest("rust"))
+ test_rust();
+}
diff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c
index 8a6d341b754ac..460ad10ddc020 100644
--- a/tools/testing/selftests/bpf/prog_tests/verifier.c
+++ b/tools/testing/selftests/bpf/prog_tests/verifier.c
@@ -12,6 +12,7 @@
#include "verifier_and.skel.h"
#include "verifier_arena.skel.h"
#include "verifier_arena_large.skel.h"
+#include "verifier_arena_scalar.skel.h"
#include "verifier_arena_globals1.skel.h"
#include "verifier_arena_globals2.skel.h"
#include "verifier_array_access.skel.h"
@@ -198,6 +199,7 @@ void test_verifier_align(void) { RUN(verifier_align); }
void test_verifier_and(void) { RUN(verifier_and); }
void test_verifier_arena(void) { RUN(verifier_arena); }
void test_verifier_arena_large(void) { RUN(verifier_arena_large); }
+void test_verifier_arena_scalar(void) { RUN(verifier_arena_scalar); }
void test_verifier_arena_globals1(void) { RUN(verifier_arena_globals1); }
void test_verifier_arena_globals2(void) { RUN(verifier_arena_globals2); }
void test_verifier_basic_stack(void) { RUN(verifier_basic_stack); }
diff --git a/tools/testing/selftests/bpf/progs/data_in_arena.c b/tools/testing/selftests/bpf/progs/data_in_arena.c
new file mode 100644
index 0000000000000..ce3838327d6f4
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/data_in_arena.c
@@ -0,0 +1,107 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+/* global data of the object is in arena */
+char data_in_arena SEC(".arena.data");
+
+int counter = 5;
+long pair[2] = { 1, 2 };
+int x = 42;
+long sum;
+long table[4];
+struct {
+ long v[8];
+} aligned64 __attribute__((aligned(64)));
+const volatile int ro = 7;
+const volatile long ro_table[4] = { 10, 20, 30, 40 };
+
+/* const strings stay in a map for helpers and kfuncs, a copy of them is in arena */
+const char hello[] SEC(".rodata.str.hello") = "hello";
+
+/* pointers to data that are stored in data */
+int *px = &x;
+const char *str = hello;
+int *const volatile cpx SEC(".data.rel.ro") = &x;
+
+SEC("syscall")
+int use_data(void *ctx)
+{
+ int i;
+
+ for (i = 0; i < 4; i++)
+ table[i] = ro_table[i] + i;
+ sum = counter + ro + pair[0] + pair[1];
+ counter++;
+ __sync_fetch_and_add(&pair[1], 3);
+ return sum;
+}
+
+typedef int (*op_fn)(int);
+
+static __noinline int add1(int v)
+{
+ return v + 1;
+}
+
+/*
+ * Pointers to functions and to data in read-only data of a program with callx.
+ * Volatile, so that the compiler doesn't replace the pointers with what
+ * they point to.
+ */
+static const volatile struct {
+ op_fn fn;
+ int *data;
+ const char *name;
+} ops SEC(".data.rel.ro") = { add1, &x, hello };
+
+SEC("syscall")
+int use_ops(void *ctx)
+{
+ /* a program has the arena when its code refers to it */
+ counter++;
+ return ops.fn(*ops.data) + ops.name[1];
+}
+
+SEC("syscall")
+int use_ptrs(void *ctx)
+{
+ unsigned long addr = (unsigned long)&aligned64;
+ char local[4] = "abc";
+
+ /* hide the address from the compiler, it knows that '& 63' is 0 */
+ asm volatile ("" : "+r"(addr));
+ if (addr & 63)
+ return 1;
+ aligned64.v[7] = 7;
+ if (*px != 42)
+ return 2;
+ *px = 43;
+ if (x != 43 || *cpx != 43)
+ return 3;
+ if (str[0] != 'h' || str[4] != 'o' || str[5])
+ return 4;
+ /* the literal is in a map */
+ if (bpf_strncmp(local, sizeof(local), "abc"))
+ return 5;
+ return 0;
+}
+
+char out[16];
+
+/* format strings are in a map */
+SEC("syscall")
+int use_printk(void *ctx)
+{
+ char buf[sizeof(out)];
+ int i, n;
+
+ bpf_printk("counter %d", counter);
+ n = BPF_SNPRINTF(buf, sizeof(buf), "%d-%d", x, ro);
+ for (i = 0; i < sizeof(out); i++)
+ out[i] = buf[i];
+ return n;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/data_in_arena_decl.c b/tools/testing/selftests/bpf/progs/data_in_arena_decl.c
new file mode 100644
index 0000000000000..01bc4fea8f0c0
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/data_in_arena_decl.c
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#define BPF_NO_KFUNC_PROTOTYPES
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_experimental.h"
+#include <bpf_arena_common.h>
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARENA);
+ __uint(map_flags, BPF_F_MMAPABLE);
+ __uint(max_entries, 4);
+} arena SEC(".maps");
+
+/* global data of the object is in arena */
+char data_in_arena SEC(".arena.data");
+
+int counter = 5;
+long sum;
+const volatile int ro = 7;
+
+#if defined(__BPF_FEATURE_ADDR_SPACE_CAST)
+int __arena avar = 11;
+#else
+int avar = 11;
+#endif
+
+SEC("syscall")
+int use_data(void *ctx)
+{
+ sum = counter + ro + avar;
+ counter++;
+ avar++;
+ return sum;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/data_in_arena_extern.c b/tools/testing/selftests/bpf/progs/data_in_arena_extern.c
new file mode 100644
index 0000000000000..dc1ad5ca3bdd0
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/data_in_arena_extern.c
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+/* global data of the object is in arena */
+char data_in_arena SEC(".arena.data");
+
+extern const int bpf_prog_active __ksym;
+
+/* the variable of the kernel is not in arena */
+const void *kp = &bpf_prog_active;
+
+SEC("syscall")
+int ptr_to_extern(void *ctx)
+{
+ return *(int *)kp;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/data_in_arena_fail.c b/tools/testing/selftests/bpf/progs/data_in_arena_fail.c
new file mode 100644
index 0000000000000..ad8afe9afc962
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/data_in_arena_fail.c
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+/* global data of the object is in arena */
+char data_in_arena SEC(".arena.data");
+
+int x = 42;
+/* the table is read-only data with pointers. It's not in arena. */
+int *const volatile tbl[1] SEC(".data.rel.ro") = { &x };
+int *const volatile *pp = tbl;
+
+SEC("syscall")
+int ptr_to_map(void *ctx)
+{
+ return **pp;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/data_in_arena_nomap.c b/tools/testing/selftests/bpf/progs/data_in_arena_nomap.c
new file mode 100644
index 0000000000000..da27a8f10d6fa
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/data_in_arena_nomap.c
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_arena_common.h"
+
+/* global data of the object is in arena */
+char data_in_arena SEC(".arena.data");
+
+int counter = 5;
+/* needs an arena map that is declared. The one that libbpf creates won't do. */
+int __arena avar = 11;
+
+SEC("syscall")
+int arena_var(void *ctx)
+{
+ return avar + counter;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/data_in_arena_rust.rs b/tools/testing/selftests/bpf/progs/data_in_arena_rust.rs
new file mode 100644
index 0000000000000..b69128487a0fc
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/data_in_arena_rust.rs
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: GPL-2.0
+
+// Why .data, .bss and .rodata of a program in Rust are in arena.
+//
+// A reference in Rust is an address. It doesn't say what it points to and it
+// can be stored in data. The list below has a node in each of the sections.
+// The nodes are linked by references that are in the data:
+// IN_BSS.next is stored by the program,
+// IN_DATA.next is a relocation in .data against .rodata.
+// sum() loads the references back and reads the three nodes with the same insn.
+//
+// When the sections are array maps libbpf skips the relocation in .data, and
+// what sum() loads from a node is a number that can't be dereferenced:
+// R1 invalid mem access 'scalar'
+// In arena the address of a node is a number to begin with.
+
+#![no_std]
+#![no_main]
+
+// Tell libbpf to keep .data, .bss and .rodata in arena.
+#[used]
+#[link_section = ".arena.data"]
+static DATA_IN_ARENA: u8 = 0;
+
+#[used]
+#[link_section = "license"]
+static LICENSE: [u8; 4] = *b"GPL\0";
+
+// panic=abort is a stop gap until panic=unwind is supported.
+// Nothing here panics, so the handler is not a part of the program.
+#[panic_handler]
+fn panic(_info: &core::panic::PanicInfo) -> ! {
+ loop {}
+}
+
+pub struct Node {
+ val: u32,
+ next: Option<&'static Node>,
+}
+
+// no_mangle makes them visible outside, so LLVM can't fold the list into a constant.
+#[no_mangle]
+static IN_RODATA: Node = Node { val: 3, next: None };
+#[no_mangle]
+static mut IN_DATA: Node = Node { val: 20, next: Some(&IN_RODATA) };
+#[no_mangle]
+static mut IN_BSS: Node = Node { val: 0, next: None };
+
+#[inline(never)]
+fn sum(mut node: Option<&Node>) -> u32 {
+ let mut sum = 0;
+ // The verifier wants a bound.
+ for _ in 0..8 {
+ let Some(n) = node else { break };
+ sum += n.val;
+ node = n.next;
+ }
+ sum
+}
+
+#[no_mangle]
+#[link_section = "syscall"]
+pub extern "C" fn list_in_data(_ctx: *mut u8) -> u32 {
+ unsafe {
+ let head = &mut *&raw mut IN_BSS;
+ head.val = 100;
+ head.next = Some(&*&raw const IN_DATA);
+ sum(Some(head))
+ }
+}
diff --git a/tools/testing/selftests/bpf/progs/verifier_arena_scalar.c b/tools/testing/selftests/bpf/progs/verifier_arena_scalar.c
new file mode 100644
index 0000000000000..bebc37f501b7e
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/verifier_arena_scalar.c
@@ -0,0 +1,912 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include "../../../include/linux/filter.h"
+#include "bpf_misc.h"
+
+void *bpf_arena_alloc_pages(void *map, void *addr, __u32 page_cnt, int node_id,
+ __u64 flags) __ksym;
+
+#ifdef __TARGET_ARCH_arm64
+#define ARENA_VM_START (1ull << 32)
+#else
+#define ARENA_VM_START (1ull << 44)
+#endif
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARENA);
+ __uint(map_flags, BPF_F_MMAPABLE);
+ __uint(max_entries, 4);
+ __ulong(map_extra, ARENA_VM_START);
+} arena SEC(".maps");
+
+struct {
+ __uint(type, BPF_MAP_TYPE_HASH);
+ __uint(max_entries, 1);
+ __type(key, int);
+ __type(value, long long);
+} hash SEC(".maps");
+
+/* JITs that take BPF_F_ARENA_SCALAR */
+#define __arena_scalar __flag(BPF_F_ARENA_SCALAR) __arch_x86_64 __arch_arm64
+
+/* BTF FUNC records are not generated for kfuncs referenced from inline assembly */
+void __kfunc_btf_root(void)
+{
+ bpf_arena_alloc_pages(0, 0, 0, 0, 0);
+}
+
+/* Tests start with r6 = address of a new page as the user space sees it, a number */
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: load and store of every size")
+__success __retval(0)
+__load_if_JITed()
+__naked void ld_st_sizes(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ r7 = r6; \
+ r1 = 0x1122334455667788 ll; \
+ *(u64 *)(r6 + 0) = r1; \
+ *(u32 *)(r6 + 8) = r1; \
+ *(u16 *)(r6 + 12) = r1; \
+ *(u8 *)(r6 + 14) = r1; \
+ *(u64 *)(r6 + 16) = 0x1234; \
+ *(u32 *)(r6 + 24) = 0x5678; \
+ *(u16 *)(r6 + 28) = 0x9a; \
+ *(u8 *)(r6 + 30) = 0xbc; \
+ r0 = 1; \
+ r2 = *(u64 *)(r6 + 0); \
+ if r2 != r1 goto 9f; \
+ r0 = 2; \
+ r2 = *(u32 *)(r6 + 8); \
+ if r2 != 0x55667788 goto 9f; \
+ r0 = 3; \
+ r2 = *(u16 *)(r6 + 12); \
+ if r2 != 0x7788 goto 9f; \
+ r0 = 4; \
+ r2 = *(u8 *)(r6 + 14); \
+ if r2 != 0x88 goto 9f; \
+ r0 = 5; \
+ r2 = *(u64 *)(r6 + 16); \
+ if r2 != 0x1234 goto 9f; \
+ r0 = 6; \
+ r2 = *(u32 *)(r6 + 24); \
+ if r2 != 0x5678 goto 9f; \
+ r0 = 7; \
+ r2 = *(u16 *)(r6 + 28); \
+ if r2 != 0x9a goto 9f; \
+ r0 = 8; \
+ r2 = *(u8 *)(r6 + 30); \
+ if r2 != 0xbc goto 9f; \
+ /* the address is what it was */ \
+ r0 = 9; \
+ if r6 != r7 goto 9f; \
+ r0 = 10; \
+ r7 >>= 32; \
+ if r7 == 0 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: load into the register that holds the address")
+__success __retval(0)
+__load_if_JITed()
+__naked void ld_into_base(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ r1 = 77; \
+ *(u64 *)(r6 + 0) = r1; \
+ r6 = *(u64 *)(r6 + 0); \
+ r0 = 1; \
+ if r6 != 77 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: sign extending load")
+__success __retval(0)
+__load_if_JITed()
+__naked void ldsx(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ r7 = r6; \
+ *(u64 *)(r6 + 0) = 0x80; \
+ .8byte %[ldsx_insn]; /* r2 = *(s8 *)(r6 + 0) */ \
+ r0 = 1; \
+ if r2 != -128 goto 9f; \
+ r0 = 2; \
+ if r6 != r7 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages),
+ __imm_insn(ldsx_insn, BPF_RAW_INSN(BPF_LDX | BPF_MEMSX | BPF_B,
+ BPF_REG_2, BPF_REG_6, 0, 0))
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: address loaded from arena")
+__success __retval(0)
+__load_if_JITed()
+__naked void ptr_chase(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ /* page[0] = &page[64]; page[64] = 5; */ \
+ r1 = r6; \
+ r1 += 64; \
+ *(u64 *)(r6 + 0) = r1; \
+ *(u64 *)(r1 + 0) = 5; \
+ r2 = *(u64 *)(r6 + 0); \
+ r0 = 1; \
+ if r2 != r1 goto 9f; \
+ r3 = *(u64 *)(r2 + 0); \
+ r0 = 2; \
+ if r3 != 5 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: atomics")
+__success __retval(0)
+__load_if_JITed()
+__naked void atomics(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ r7 = r6; \
+ *(u64 *)(r6 + 8) = 1; \
+ r1 = 2; \
+ lock *(u64 *)(r6 + 8) += r1; \
+ r1 = 4; \
+ .8byte %[fetch_add_insn]; /* r1 = atomic_fetch_add((u64 *)(r6 + 8), r1) */ \
+ r0 = 1; \
+ if r1 != 3 goto 9f; \
+ r1 = 8; \
+ .8byte %[xchg_insn]; /* r1 = xchg_64(r6 + 8, r1) */ \
+ r0 = 2; \
+ if r1 != 7 goto 9f; \
+ r0 = 8; \
+ r1 = 16; \
+ .8byte %[cmpxchg_insn]; /* r0 = cmpxchg_64(r6 + 8, r0, r1) */ \
+ r2 = r0; \
+ r0 = 3; \
+ if r2 != 8 goto 9f; \
+ r2 = *(u64 *)(r6 + 8); \
+ r0 = 4; \
+ if r2 != 16 goto 9f; \
+ r0 = 5; \
+ if r6 != r7 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages),
+ __imm_insn(fetch_add_insn, BPF_ATOMIC_OP(BPF_DW, BPF_ADD | BPF_FETCH,
+ BPF_REG_6, BPF_REG_1, 8)),
+ __imm_insn(xchg_insn, BPF_ATOMIC_OP(BPF_DW, BPF_XCHG, BPF_REG_6, BPF_REG_1, 8)),
+ __imm_insn(cmpxchg_insn, BPF_ATOMIC_OP(BPF_DW, BPF_CMPXCHG, BPF_REG_6, BPF_REG_1, 8))
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: cmpxchg through r0")
+__success __retval(0)
+__load_if_JITed()
+__naked void cmpxchg_r0(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ /* The address is in r0. The value at the address is not equal to it. */ \
+ *(u64 *)(r6 + 0) = 3; \
+ r0 = r6; \
+ r1 = 5; \
+ .8byte %[cmpxchg_insn]; /* r0 = cmpxchg_64(r0 + 0, r0, r1) */ \
+ r2 = r0; \
+ r0 = 1; \
+ if r2 != 3 goto 9f; \
+ r2 = *(u64 *)(r6 + 0); \
+ r0 = 2; \
+ if r2 != 3 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages),
+ __imm_insn(cmpxchg_insn, BPF_ATOMIC_OP(BPF_DW, BPF_CMPXCHG, BPF_REG_0, BPF_REG_1, 0))
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: xchg into the register that holds the address")
+__success __retval(0)
+__load_if_JITed()
+__naked void xchg_into_base(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ *(u64 *)(r6 + 0) = 3; \
+ r1 = r6; \
+ .8byte %[xchg_insn]; /* r1 = xchg_64(r1 + 0, r1) */ \
+ r0 = 1; \
+ if r1 != 3 goto 9f; \
+ r2 = *(u64 *)(r6 + 0); \
+ r0 = 2; \
+ if r2 != r6 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages),
+ __imm_insn(xchg_insn, BPF_ATOMIC_OP(BPF_DW, BPF_XCHG, BPF_REG_1, BPF_REG_1, 0))
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: xchg into the register that holds a pointer to stack")
+__failure __msg("misaligned access off (0x0; 0xffffffffffffffff)+0 size 8")
+__naked void xchg_into_stack_ptr(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r1 = 0; \
+ *(u64 *)(r10 - 8) = r1; \
+ r1 = r10; \
+ r1 += -8; \
+ .8byte %[xchg_insn]; /* r1 = xchg_64(r1 + 0, r1) */ \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm_addr(arena),
+ __imm_insn(xchg_insn, BPF_ATOMIC_OP(BPF_DW, BPF_XCHG, BPF_REG_1, BPF_REG_1, 0))
+ : __clobber_all);
+}
+
+#ifdef CAN_USE_LOAD_ACQ_STORE_REL
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: load-acquire and store-release")
+__success __retval(0)
+__load_if_JITed()
+__naked void load_acq_store_rel(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ r7 = r6; \
+ r1 = 0x1234; \
+ .8byte %[store_release_insn]; /* store_release((u64 *)(r6 + 8), r1) */ \
+ .8byte %[load_acquire_insn]; /* r2 = load_acquire((u64 *)(r6 + 8)) */ \
+ r0 = 1; \
+ if r2 != 0x1234 goto 9f; \
+ .8byte %[load_acquire8_insn]; /* w2 = load_acquire((u8 *)(r6 + 8)) */ \
+ r0 = 2; \
+ if r2 != 0x34 goto 9f; \
+ r0 = 3; \
+ if r6 != r7 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages),
+ __imm_insn(store_release_insn,
+ BPF_ATOMIC_OP(BPF_DW, BPF_STORE_REL, BPF_REG_6, BPF_REG_1, 8)),
+ __imm_insn(load_acquire_insn,
+ BPF_ATOMIC_OP(BPF_DW, BPF_LOAD_ACQ, BPF_REG_2, BPF_REG_6, 8)),
+ __imm_insn(load_acquire8_insn,
+ BPF_ATOMIC_OP(BPF_B, BPF_LOAD_ACQ, BPF_REG_2, BPF_REG_6, 8))
+ : __clobber_all);
+}
+
+#endif /* CAN_USE_LOAD_ACQ_STORE_REL */
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: number that is not an address in arena")
+__success __retval(0)
+__load_if_JITed()
+__naked void not_in_arena(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ /* nothing is allocated: all loads read 0, stores are dropped */ \
+ r6 = 0xdeadbeef00000000 ll; \
+ r0 = 1; \
+ r2 = *(u64 *)(r6 + 0); \
+ if r2 != 0 goto 9f; \
+ r0 = 2; \
+ r2 = *(u8 *)(r6 - 32768); \
+ if r2 != 0 goto 9f; \
+ r6 = 0x12345678ffffffff ll; \
+ r0 = 3; \
+ r2 = *(u64 *)(r6 + 32760); \
+ if r2 != 0 goto 9f; \
+ *(u64 *)(r6 + 32760) = 1; \
+ *(u8 *)(r6 + 32767) = r2; \
+ r1 = 1; \
+ lock *(u64 *)(r6 + 32760) += r1; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: store through the address of the stack as a number")
+__success __retval(0)
+__load_if_JITed()
+__naked void stack_addr_as_number(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ *(u64 *)(r10 - 8) = 5; \
+ r6 = r10; \
+ r6 |= 0; \
+ /* r6 is a number now. The store goes to arena, not to the stack. */ \
+ *(u64 *)(r6 - 8) = 7; \
+ r1 = 9; \
+ *(u64 *)(r6 - 8) = r1; \
+ lock *(u64 *)(r6 - 8) += r1; \
+ r2 = *(u64 *)(r10 - 8); \
+ r0 = 1; \
+ if r2 != 5 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: 64-bit math on the address stays 64-bit")
+__success __retval(0)
+__load_if_JITed()
+__naked void alu64_after_access(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ r2 = *(u64 *)(r6 + 0); \
+ r7 = r6; \
+ r7 += 8; \
+ r7 -= r6; \
+ r0 = 1; \
+ if r7 != 8 goto 9f; \
+ r7 = r6; \
+ r7 >>= 32; \
+ r0 = 2; \
+ if r7 == 0 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: st of an immediate changes no register")
+__success __retval(0)
+__load_if_JITed()
+__naked void st_keeps_regs(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ r0 = r6; \
+ r1 = 0x1111; \
+ r2 = 0x2222; \
+ *(u64 *)(r0 + 0) = 5; \
+ r3 = r0; \
+ r0 = 1; \
+ if r3 != r6 goto 9f; \
+ r0 = 2; \
+ if r1 != 0x1111 goto 9f; \
+ r0 = 3; \
+ if r2 != 0x2222 goto 9f; \
+ r0 = 0x3333; \
+ r1 = r6; \
+ *(u32 *)(r1 + 8) = -7; \
+ r3 = r0; \
+ r0 = 4; \
+ if r3 != 0x3333 goto 9f; \
+ r0 = 5; \
+ if r1 != r6 goto 9f; \
+ r0 = 6; \
+ r3 = *(u64 *)(r6 + 0); \
+ if r3 != 5 goto 9f; \
+ r0 = 7; \
+ r3 = *(u64 *)(r6 + 8); \
+ r4 = 0xfffffff9 ll; \
+ if r3 != r4 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: access through a number with garbage in the upper half")
+__success __retval(0)
+__load_if_JITed()
+__naked void st_value_garbage(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ r1 = 0xdeadbeef00001000 ll; \
+ *(u64 *)(r6 + 2048) = r1; \
+ r7 = *(u64 *)(r6 + 2048); \
+ r8 = *(u64 *)(r6 + 2048); \
+ r1 = 3; \
+ *(u64 *)(r8 + 0) = 1; \
+ r0 = 1; \
+ if r8 != r7 goto 9f; \
+ *(u8 *)(r8 + 1) = 1; \
+ r0 = 2; \
+ if r8 != r7 goto 9f; \
+ *(u32 *)(r8 + 4) = r1; \
+ r0 = 3; \
+ if r8 != r7 goto 9f; \
+ r2 = *(u16 *)(r8 + 2); \
+ r0 = 4; \
+ if r8 != r7 goto 9f; \
+ r0 = 5; \
+ if r1 != 3 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: access through a number without the upper half")
+__success __retval(0)
+__load_if_JITed()
+__naked void st_value_small(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ r1 = 0x1000 ll; \
+ *(u64 *)(r6 + 2048) = r1; \
+ r7 = *(u64 *)(r6 + 2048); \
+ r8 = *(u64 *)(r6 + 2048); \
+ r1 = 3; \
+ *(u64 *)(r8 + 0) = 1; \
+ r0 = 1; \
+ if r8 != r7 goto 9f; \
+ *(u8 *)(r8 + 1) = 1; \
+ r0 = 2; \
+ if r8 != r7 goto 9f; \
+ *(u32 *)(r8 + 4) = r1; \
+ r0 = 3; \
+ if r8 != r7 goto 9f; \
+ r2 = *(u16 *)(r8 + 2); \
+ r0 = 4; \
+ if r8 != r7 goto 9f; \
+ r0 = 5; \
+ if r1 != 3 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: atomics through a number that is not an address in arena")
+__success __retval(0)
+__load_if_JITed()
+__naked void atomic_value(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ r1 = 0xdeadbeef00001000 ll; \
+ *(u64 *)(r6 + 2048) = r1; \
+ r7 = *(u64 *)(r6 + 2048); \
+ r8 = *(u64 *)(r6 + 2048); \
+ r1 = 3; \
+ lock *(u64 *)(r8 + 0) += r1; \
+ r0 = 1; \
+ if r8 != r7 goto 9f; \
+ .8byte %[fetch_add_insn]; \
+ r0 = 2; \
+ if r8 != r7 goto 9f; \
+ .8byte %[xchg_insn]; \
+ r0 = 3; \
+ if r8 != r7 goto 9f; \
+ r0 = 0; \
+ .8byte %[cmpxchg_insn]; \
+ r0 = 4; \
+ if r8 != r7 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages),
+ __imm_insn(fetch_add_insn, BPF_ATOMIC_OP(BPF_DW, BPF_ADD | BPF_FETCH,
+ BPF_REG_8, BPF_REG_1, 0)),
+ __imm_insn(xchg_insn, BPF_ATOMIC_OP(BPF_DW, BPF_XCHG, BPF_REG_8, BPF_REG_1, 0)),
+ __imm_insn(cmpxchg_insn, BPF_ATOMIC_OP(BPF_DW, BPF_CMPXCHG, BPF_REG_8, BPF_REG_1, 0))
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: number and pointer to arena at the same insn")
+__success __retval(0)
+__load_if_JITed()
+__naked void mixed_number_arena(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ *(u64 *)(r6 + 8) = 0x1234; \
+ /* a number that the verifier does not know, 0 at run time */ \
+ r7 = *(u64 *)(r6 + 16); \
+ r8 = r6; \
+ if r7 != 0 goto 1f; \
+ .8byte %[cast_kern_insn]; \
+1: *(u8 *)(r8 + 0) = 1; \
+ *(u8 *)(r8 + 1) = r7; \
+ r2 = *(u8 *)(r8 + 1); \
+ lock *(u64 *)(r8 + 24) += r7; \
+ r0 = 0; \
+ if r7 != 0 goto 9f; \
+ /* pointer to arena only: JIT adds all 64 bits of r8 to the base */ \
+ r0 = 2; \
+ r2 = *(u64 *)(r8 + 8); \
+ if r2 != 0x1234 goto 9f; \
+ r0 = 3; \
+ r2 = *(u8 *)(r6 + 0); \
+ if r2 != 1 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages),
+ __imm_insn(cast_kern_insn, BPF_RAW_INSN(BPF_ALU64 | BPF_MOV | BPF_X,
+ BPF_REG_8, BPF_REG_8, 1, 1))
+ : __clobber_all);
+}
+
+SEC("syscall")
+__description("arena_scalar: no flag, no access through a number")
+__failure __msg("R6 invalid mem access 'scalar'")
+__naked void no_flag(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r6 = 0x100000000000 ll; \
+ r0 = *(u64 *)(r6 + 0); \
+ exit; \
+" :
+ : __imm_addr(arena)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: no arena, no access through a number")
+__failure __msg("R6 invalid mem access 'scalar'")
+__naked void no_arena(void)
+{
+ asm volatile (" \
+ r6 = 0x100000000000 ll; \
+ r0 = *(u64 *)(r6 + 0); \
+ exit; \
+" ::: __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: pointer that is NULL is an address in arena")
+__success __retval(0)
+__load_if_JITed()
+__naked void null_ptr(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r1 = 0; \
+ *(u32 *)(r10 - 4) = r1; \
+ r2 = r10; \
+ r2 += -4; \
+ r1 = %[hash] ll; \
+ call %[bpf_map_lookup_elem]; \
+ r1 = r0; \
+ r0 = 1; \
+ if r1 != 0 goto 9f; \
+ /* nothing is allocated: the load reads 0 */ \
+ r0 = *(u64 *)(r1 + 0); \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm_addr(hash),
+ __imm(bpf_map_lookup_elem)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: pointer that is NULL with an offset is an address in arena")
+__success __retval(0)
+__load_if_JITed()
+__naked void null_ptr_off(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r1 = 0; \
+ *(u32 *)(r10 - 4) = r1; \
+ r2 = r10; \
+ r2 += -4; \
+ r1 = %[hash] ll; \
+ call %[bpf_map_lookup_elem]; \
+ r1 = r0; \
+ r0 = 1; \
+ if r1 != 0 goto 9f; \
+ r1 += 8; \
+ *(u64 *)(r1 + 0) = 5; \
+ r0 = *(u64 *)(r1 + 0); \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm_addr(hash),
+ __imm(bpf_map_lookup_elem)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: number that is less than a page is an address in arena")
+__success __retval(0)
+__load_if_JITed()
+__naked void small_number(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ call %[bpf_get_prandom_u32]; \
+ r1 = r0; \
+ r1 &= 0xfff; \
+ r0 = *(u64 *)(r1 + 0); \
+ exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_get_prandom_u32)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: number is not a pointer for a helper")
+__failure __msg("R2 type=scalar expected=")
+__naked void helper_arg(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ r1 = %[hash] ll; \
+ r2 = r6; \
+ call %[bpf_map_lookup_elem]; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm_addr(hash),
+ __imm(bpf_arena_alloc_pages),
+ __imm(bpf_map_lookup_elem)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: number and pointer to stack at the same insn")
+__failure __msg("same insn cannot be used with different pointers")
+__load_if_JITed()
+__naked void mixed_number_stack(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ *(u64 *)(r10 - 8) = 0; \
+ call %[bpf_get_prandom_u32]; \
+ if w0 != 0 goto 1f; \
+ r6 = r10; \
+ r6 += -8; \
+1: r0 = *(u64 *)(r6 + 0); \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages),
+ __imm(bpf_get_prandom_u32)
+ : __clobber_all);
+}
+
+static int st_cb(__u64 idx, void *ctx)
+{
+ volatile long *p = *(volatile long **)ctx;
+
+ p[idx] = 7;
+ return 0;
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: store through a number in a callback")
+__success __retval(0)
+__load_if_JITed()
+int st_in_callback(void *unused)
+{
+ volatile long *p = bpf_arena_alloc_pages(&arena, NULL, 1, -1, 0);
+
+ if (!p)
+ return 100;
+ bpf_loop(4, st_cb, &p, 0);
+ return p[0] + p[3] + p[4] - 14;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/verifier_value_illegal_alu.c b/tools/testing/selftests/bpf/progs/verifier_value_illegal_alu.c
index 4d8273c258d51..31663338866d3 100644
--- a/tools/testing/selftests/bpf/progs/verifier_value_illegal_alu.c
+++ b/tools/testing/selftests/bpf/progs/verifier_value_illegal_alu.c
@@ -22,8 +22,8 @@ struct {
SEC("socket")
__description("map element value illegal alu op, 1")
-__failure __msg("R0 bitwise operator &= on pointer")
-__failure_unpriv
+__failure __msg("R0 invalid mem access 'scalar'")
+__failure_unpriv __msg_unpriv("R0 bitwise operator &= on pointer")
__naked void value_illegal_alu_op_1(void)
{
asm volatile (" \
@@ -70,8 +70,8 @@ l0_%=: exit; \
SEC("socket")
__description("map element value illegal alu op, 3")
-__failure __msg("R0 pointer arithmetic with /= operator")
-__failure_unpriv
+__failure __msg("R0 invalid mem access 'scalar'")
+__failure_unpriv __msg_unpriv("R0 pointer arithmetic with /= operator")
__naked void value_illegal_alu_op_3(void)
{
asm volatile (" \
@@ -165,6 +165,171 @@ __naked void map_ptr_illegal_alu_op(void)
: __clobber_all);
}
+SEC("socket")
+__description("tag in the low bit of a pointer, and, shift")
+__success __retval(0)
+__failure_unpriv __msg_unpriv("R1 bitwise operator &= on pointer")
+__naked void ptr_tag_and_shift(void)
+{
+ asm volatile (" \
+ r2 = r10; \
+ r2 += -8; \
+ r1 = 0; \
+ *(u64*)(r2 + 0) = r1; \
+ r1 = %[map_hash_48b] ll; \
+ call %[bpf_map_lookup_elem]; \
+ if r0 == 0 goto l0_%=; \
+ r1 = r0; \
+ r1 &= 1; \
+ r2 = r0; \
+ r2 >>= 1; \
+ r3 = r0; \
+ r3 |= 1; \
+ r3 ^= 1; \
+ r0 = *(u32*)(r0 + 0); \
+ r0 = 0; \
+l0_%=: exit; \
+" :
+ : __imm(bpf_map_lookup_elem),
+ __imm_addr(map_hash_48b)
+ : __clobber_all);
+}
+
+SEC("socket")
+__description("tag in the low bit of a pointer, CAP_BPF without CAP_PERFMON")
+__success __retval(0)
+__failure_unpriv __msg_unpriv("R1 bitwise operator &= on pointer")
+__caps_unpriv(CAP_BPF)
+__naked void ptr_tag_cap_bpf(void)
+{
+ asm volatile (" \
+ r2 = r10; \
+ r2 += -8; \
+ r1 = 0; \
+ *(u64*)(r2 + 0) = r1; \
+ r1 = %[map_hash_48b] ll; \
+ call %[bpf_map_lookup_elem]; \
+ if r0 == 0 goto l0_%=; \
+ r1 = r0; \
+ r1 &= 1; \
+ r0 = 0; \
+l0_%=: exit; \
+" :
+ : __imm(bpf_map_lookup_elem),
+ __imm_addr(map_hash_48b)
+ : __clobber_all);
+}
+
+SEC("socket")
+__description("number op= pointer")
+__success __retval(0)
+__failure_unpriv __msg_unpriv("R1 pointer arithmetic with *= operator")
+__naked void number_mul_ptr(void)
+{
+ asm volatile (" \
+ r2 = r10; \
+ r2 += -8; \
+ r1 = 0; \
+ *(u64*)(r2 + 0) = r1; \
+ r1 = %[map_hash_48b] ll; \
+ call %[bpf_map_lookup_elem]; \
+ if r0 == 0 goto l0_%=; \
+ r1 = 7; \
+ r1 *= r0; \
+ r0 = 0; \
+l0_%=: exit; \
+" :
+ : __imm(bpf_map_lookup_elem),
+ __imm_addr(map_hash_48b)
+ : __clobber_all);
+}
+
+SEC("socket")
+__description("pointer with the tag cleared is a number")
+__failure __msg("R0 invalid mem access 'scalar'")
+__failure_unpriv __msg_unpriv("R0 bitwise operator |= on pointer")
+__naked void ptr_tag_cleared_deref(void)
+{
+ asm volatile (" \
+ r2 = r10; \
+ r2 += -8; \
+ r1 = 0; \
+ *(u64*)(r2 + 0) = r1; \
+ r1 = %[map_hash_48b] ll; \
+ call %[bpf_map_lookup_elem]; \
+ if r0 == 0 goto l0_%=; \
+ r0 |= 1; \
+ r0 ^= 1; \
+ r0 = *(u32*)(r0 + 0); \
+l0_%=: r0 = 0; \
+ exit; \
+" :
+ : __imm(bpf_map_lookup_elem),
+ __imm_addr(map_hash_48b)
+ : __clobber_all);
+}
+
+SEC("socket")
+__description("shift of a pointer that may be NULL")
+__failure __msg("R0 pointer arithmetic on map_value_or_null prohibited, null-check it first")
+__failure_unpriv
+__naked void ptr_or_null_shift(void)
+{
+ asm volatile (" \
+ r2 = r10; \
+ r2 += -8; \
+ r1 = 0; \
+ *(u64*)(r2 + 0) = r1; \
+ r1 = %[map_hash_48b] ll; \
+ call %[bpf_map_lookup_elem]; \
+ r0 >>= 1; \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm(bpf_map_lookup_elem),
+ __imm_addr(map_hash_48b)
+ : __clobber_all);
+}
+
+SEC("socket")
+__description("and of a pointer to map")
+__failure __msg("R0 pointer arithmetic on map_ptr prohibited")
+__failure_unpriv
+__naked void map_ptr_and(void)
+{
+ asm volatile (" \
+ r0 = %[map_hash_48b] ll; \
+ r0 &= 1; \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm_addr(map_hash_48b)
+ : __clobber_all);
+}
+
+SEC("socket")
+__description("32-bit and of a pointer")
+__success __retval(0)
+__failure_unpriv __msg_unpriv("R0 32-bit pointer arithmetic prohibited")
+__naked void ptr_and32(void)
+{
+ asm volatile (" \
+ r2 = r10; \
+ r2 += -8; \
+ r1 = 0; \
+ *(u64*)(r2 + 0) = r1; \
+ r1 = %[map_hash_48b] ll; \
+ call %[bpf_map_lookup_elem]; \
+ if r0 == 0 goto l0_%=; \
+ w0 &= 1; \
+l0_%=: r0 = 0; \
+ exit; \
+" :
+ : __imm(bpf_map_lookup_elem),
+ __imm_addr(map_hash_48b)
+ : __clobber_all);
+}
+
SEC("flow_dissector")
__description("flow_keys illegal alu op with variable offset")
__failure __msg("R7 pointer arithmetic on flow_keys prohibited")
diff --git a/tools/testing/selftests/bpf/test_loader.c b/tools/testing/selftests/bpf/test_loader.c
index 25eeb1c1248b3..a89890cd56d82 100644
--- a/tools/testing/selftests/bpf/test_loader.c
+++ b/tools/testing/selftests/bpf/test_loader.c
@@ -580,6 +580,8 @@ static int parse_test_spec(struct test_loader *tester,
update_flags(&spec->prog_flags, BPF_F_XDP_HAS_FRAGS, clear);
} else if (strcmp(val, "BPF_F_TEST_REG_INVARIANTS") == 0) {
update_flags(&spec->prog_flags, BPF_F_TEST_REG_INVARIANTS, clear);
+ } else if (strcmp(val, "BPF_F_ARENA_SCALAR") == 0) {
+ update_flags(&spec->prog_flags, BPF_F_ARENA_SCALAR, clear);
} else /* assume numeric value */ {
err = parse_int(val, &flags, "test prog flags");
if (err)
|
| 3/2 | 2026/10/03 02:37 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 53106 output: 32 thoughts: 1113 Thoughts: **BPF Arena Scalar and BTF Enhancements - Worthy of Fuzzing** Okay, so I'm looking at this patch series for the Linux kernel, specifically targeting the amd64 architecture. My initial impression is that this is significant enough to warrant fuzzing. Let's break it down: First, this patch introduces changes related to a concept called "arena scalar" within the BPF (Berkeley Packet Filter) subsystem. It adds supporting structures and flags, notably `BPF_F_ARENA_SCALAR`, and a related function `bpf_jit_supports_arena_scalar()`. This seems to be a new feature, possibly related to memory arena management in the BPF JIT compiler. The fact that it's being added to both `arch/arm64` and `arch/x86` is noteworthy, suggesting a broad architectural impact. The x86 changes are particularly interesting as they add `BPF_REG_AX` to `reg2pt_regs[]`. Secondly, there are several changes within the `kernel/bpf` directory, including modifications to BTF (BPF Type Format) handling. These include alterations to `btf_name_valid_identifier`, `btf_datasec_resolve`, `btf_func_check`, and addition of functions like `btf_var_is_piece`. BTF is crucial for BPF's type information, so any changes here could impact how BPF programs are verified and executed. There's also some refactoring in `kernel/bpf/core.c` affecting symbol names and the weak definition of `bpf_jit_supports_arena_scalar()`. Critically, the patch modifies the verifier (`kernel/bpf/verifier.c`) and fixup routines (`kernel/bpf/fixups.c`). These are the gatekeepers of BPF program safety and correctness. The changes include additions of the `arena_scalar` flag, related checks in functions like `is_arena_scalar()`, `atomic_ptr_type_ok()`, `mem_access_reg()`, `adjust_ptr_min_max_vals()` and `do_check_insn()`. The verifier changes especially raise a flag - changes here can have profound security implications. The `fixups.c` patch introduces `arena_scalar_access()`, which sounds like it is patching BPF instructions to access memory through the scalar arena, which is a new vector for attack. Furthermore, `kernel/bpf/syscall.c` sees modifications to `bpf_prog_load()`, indicating this new "arena scalar" feature is controllable through the `bpf()` syscall. This means that a user-space program can potentially trigger these code paths. This makes it a prime candidate for fuzzing as it is directly user-accessible. Finally, there are changes in `tools/bpftool` and likely selftests. Considering all this, it's clear: * This patch directly touches reachable executable code in the BPF subsystem via the `bpf()` syscall. * It introduces a new feature (`BPF_F_ARENA_SCALAR`) with implications for memory management, the verifier, and the JIT compiler. * Changes within the BPF verifier and fixup routines are inherently security-sensitive areas. * The fact it affects both `x86` and `arm64` suggests wider code paths. * Modifications to BTF could influence how program semantics are interpreted. Therefore, **WorthFuzzing=true** is an easy call. I'll need to figure out the specific Kconfig options required to enable this functionality (I'll need to use `grepper` or inspect the files further to do that), but I'm confident that this represents a good target for fuzzing. We'll need to identify the relevant focus symbols and then start building the fuzzing harness. |
| 4/2 | 2026/10/03 02:37 | tool | grepper |
0mArgs:{
"Expression": "config BPF_SYSCALL",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/Kconfig=29=menu \"BPF subsystem\"\nkernel/bpf/Kconfig-30-\nkernel/bpf/Kconfig:31:config BPF_SYSCALL\nkernel/bpf/Kconfig-32-\tbool \"Enable bpf() system call\"\n"
} |
| 5/2 | 2026/10/03 02:37 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 55180 output: 39 thoughts: 37 Thoughts: Okay, let's dive into this. I need to understand how these critical BPF objects – specifically `btf.o`, `verifier.o`, `fixups.o`, and others – are built. My instinct is to go directly to the source of truth for build instructions, so I'll need to examine `kernel/bpf/Makefile`. That's where I expect to find the dependencies, compilation flags, and linking steps that define the build process for those core BPF components. This Makefile is the key to understanding the low-level mechanics of how these important parts of the BPF subsystem are constructed. It’s the starting point for really comprehending the intricacies of their compilation. |
| 6/2 | 2026/10/03 02:37 | tool | grepper |
0mArgs:{
"Expression": "btf\\.o|verifier\\.o|fixups\\.o",
"PathPrefix": "kernel/bpf/Makefile"
}Results: {
"Output": "kernel/bpf/Makefile=7=CFLAGS_core.o += -Wno-override-init $(cflags-nogcse-yy)\nkernel/bpf/Makefile-8-\nkernel/bpf/Makefile:9:obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o cnum.o log.o token.o liveness.o const_fold.o diagnostics.o\nkernel/bpf/Makefile-10-obj-$(CONFIG_BPF_SYSCALL) += bpf_iter.o map_iter.o task_iter.o prog_iter.o link_iter.o\n--\nkernel/bpf/Makefile=13=obj-$(CONFIG_BPF_SYSCALL) += bpf_local_storage.o bpf_task_storage.o\nkernel/bpf/Makefile:14:obj-$(CONFIG_BPF_SYSCALL) += fixups.o cfg.o states.o backtrack.o check_btf.o\nkernel/bpf/Makefile-15-obj-${CONFIG_BPF_LSM}\t += bpf_inode_storage.o\n--\nkernel/bpf/Makefile=17=obj-$(CONFIG_BPF_JIT) += trampoline.o\nkernel/bpf/Makefile:18:obj-$(CONFIG_BPF_SYSCALL) += btf.o memalloc.o rqspinlock.o stream.o\nkernel/bpf/Makefile-19-ifeq ($(CONFIG_MMU)$(CONFIG_64BIT),yy)\n--\nkernel/bpf/Makefile=43=ifeq ($(CONFIG_SYSFS),y)\nkernel/bpf/Makefile:44:obj-$(CONFIG_DEBUG_INFO_BTF) += sysfs_btf.o\nkernel/bpf/Makefile-45-endif\n"
} |
| 7/2 | 2026/10/03 02:37 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 55847 output: 34 thoughts: 163 Thoughts: **Investigating BPF_JIT and Arena Scalar Support** Okay, so I'm digging into this arena scalar support issue and it's leading me straight to `BPF_JIT`. That makes sense; the whole arena functionality, with its `bpf_jit_supports_arena_scalar()` function and the `BPF_F_ARENA_SCALAR` flag, is fundamentally tied to the JIT compiler. I need to be sure the JIT is enabled. Right, let's refresh our memory. Remember in `kernel/bpf/syscall.c`, there's that crucial check: ```c if ((attr->prog_flags & BPF_F_ARENA_SCALAR) && !bpf_jit_supports_arena_scalar()) return -EOPNOTSUPP; ``` That's the tell. If the arena scalar flag is set, but the JIT doesn't support it, we're throwing an error. And `bpf_jit_supports_arena_scalar()` itself? That's buried in `arch/x86/net/bpf_jit_comp.c`. That's important. Okay, let's take a look at the Makefile. I'll need to check the build configuration and dependencies in `arch/x86/net/Makefile` to understand how the JIT is compiled and how `BPF_F_ARENA_SCALAR` is defined. This should clear up what the possible configurations are. |
| 8/2 | 2026/10/03 02:37 | tool | grepper |
0mArgs:{
"Expression": "bpf_jit_comp",
"PathPrefix": "arch/x86/net/"
}Results: {
"Output": "arch/x86/net/Makefile=6=ifeq ($(CONFIG_X86_32),y)\narch/x86/net/Makefile:7: obj-$(CONFIG_BPF_JIT) += bpf_jit_comp32.o\narch/x86/net/Makefile-8-else\narch/x86/net/Makefile:9: obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o bpf_timed_may_goto.o\narch/x86/net/Makefile-10-endif\n"
} |
| 9/2 | 2026/10/03 02:37 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 56518 output: 28 thoughts: 165 Thoughts: **My BPF JIT Arena Considerations** Okay, so I'm digging into this BPF arena stuff, and it's hitting me: to get `bpf_jit_supports_arena_scalar()` returning true on x86_64, `CONFIG_BPF_JIT` *has* to be enabled. Otherwise, that function just falls back to the weak definition in `kernel/bpf/core.c`, which, of course, returns false. And if that happens, any `bpf_prog_load()` call that tries to use `BPF_F_ARENA_SCALAR` is going to crap out with an `-EOPNOTSUPP` error. Annoying, but understandable. Now, `CONFIG_BPF_SYSCALL` is a no-brainer, that's definitely a requirement. But I need to be sure. I'm wondering, are there any other prerequisites for this BPF arena to work properly? Time to dive into the code and Kconfig. I'll check `BPF_MAP_TYPE_ARENA` in `kernel/bpf/arena.c` and Kconfig to confirm if there are any other dependencies. Gotta be thorough. |
| 10/2 | 2026/10/03 02:37 | tool | grepper |
0mArgs:{
"Expression": "arena\\.o",
"PathPrefix": "kernel/bpf/"
}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/10/03 02:37 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 57054 output: 34 thoughts: 109 Thoughts: **Examining BPF_SYSCALL Dependencies** Okay, so I'm looking at this makefile snippet. It's conditionally including `arena.o` and `range_tree.o` in the `obj-$(CONFIG_BPF_SYSCALL)` list if both `CONFIG_MMU` and `CONFIG_64BIT` are set to 'y'. That's pretty standard on amd64, so I'm not surprised by those dependencies. But, I need to make sure I haven't missed anything. I need to make sure I am accounting for *all* required configs, so the best way to do that is to check the `kernel/bpf/Kconfig` file. I need to see if `BPF_SYSCALL` has any other hidden dependencies or constraints related to BPF_JIT or anything else. Let's dig in and make sure this is solid. |
| 12/2 | 2026/10/03 02:37 | tool | grepper |
0mArgs:{
"Expression": "config BPF_SYSCALL",
"PathPrefix": "kernel/bpf/Kconfig"
}Results: {
"Output": "kernel/bpf/Kconfig=29=menu \"BPF subsystem\"\nkernel/bpf/Kconfig-30-\nkernel/bpf/Kconfig:31:config BPF_SYSCALL\nkernel/bpf/Kconfig-32-\tbool \"Enable bpf() system call\"\n"
} |
| 13/2 | 2026/10/03 02:37 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 57476 output: 34 thoughts: 21 Thoughts: Here's my summary, as though I'm reading and processing the requested section of the Kconfig file: **Analyzing Kernel BPF Configuration Options** Okay, let's dive into lines 30-60 of `kernel/bpf/Kconfig`. Right, the usual Kconfig syntax. I'm looking for the core BPF configuration options here – the building blocks. I expect to see flags like `CONFIG_BPF`, `CONFIG_BPF_SYSCALL`, and maybe some related sub-features. I'm quickly scanning for dependencies, default values, and the prompt text associated with each option. This will tell me what the kernel offers and when to enable the functionality. Are there any conditional dependencies? Are any options only available on specific architectures or with certain other features enabled? I'm paying close attention to the implications of enabling or disabling each option. What's the impact on kernel size, performance, and security? Any signs of experimental or deprecated options? I'll also be mindful of the general structure – how these options are logically grouped and if they're hierarchically organized. Understanding that structure makes the overall framework easier to grasp and navigate. Ok, time to see what's what! |
| 14/2 | 2026/10/03 02:37 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/Kconfig",
"FirstLine": 30,
"LineCount": 40
}Results: {
"Contents": " 30:\t\n 31:\tconfig BPF_SYSCALL\n 32:\t\tbool \"Enable bpf() system call\"\n 33:\t\tselect BPF\n 34:\t\tselect IRQ_WORK\n 35:\t\tselect NEED_TASKS_RCU\n 36:\t\tselect TASKS_TRACE_RCU\n 37:\t\tselect BINARY_PRINTF\n 38:\t\tselect NET_SOCK_MSG if NET\n 39:\t\tselect NET_XGRESS if NET\n 40:\t\tselect PAGE_POOL if NET\n 41:\t\tdefault n\n 42:\t\thelp\n 43:\t\t Enable the bpf() system call that allows to manipulate BPF programs\n 44:\t\t and maps via file descriptors.\n 45:\t\n 46:\tconfig BPF_JIT\n 47:\t\tbool \"Enable BPF Just In Time compiler\"\n 48:\t\tdepends on BPF\n 49:\t\tdepends on HAVE_CBPF_JIT || HAVE_EBPF_JIT\n 50:\t\tselect EXECMEM\n 51:\t\thelp\n 52:\t\t BPF programs are normally handled by a BPF interpreter. This option\n 53:\t\t allows the kernel to generate native code when a program is loaded\n 54:\t\t into the kernel. This will significantly speed-up processing of BPF\n 55:\t\t programs.\n 56:\t\n 57:\t\t Note, an admin should enable this feature changing:\n 58:\t\t /proc/sys/net/core/bpf_jit_enable\n 59:\t\t /proc/sys/net/core/bpf_jit_harden (optional)\n 60:\t\t /proc/sys/net/core/bpf_jit_kallsyms (optional)\n 61:\t\n 62:\tconfig BPF_JIT_ALWAYS_ON\n 63:\t\tbool \"Permanently enable BPF JIT and remove BPF interpreter\"\n 64:\t\tdepends on BPF_SYSCALL \u0026\u0026 HAVE_EBPF_JIT \u0026\u0026 BPF_JIT\n 65:\t\thelp\n 66:\t\t Enables BPF JIT and removes BPF interpreter to avoid speculative\n 67:\t\t execution of BPF instructions by the interpreter.\n 68:\t\n 69:\t\t When CONFIG_BPF_JIT_ALWAYS_ON is enabled, /proc/sys/net/core/bpf_jit_enable\n"
} |
| 15/2 | 2026/10/03 02:37 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 58396 output: 38 thoughts: 1335 Thoughts: **Analyzing BPF Feature Changes for Fuzzing Focus** Okay, so I'm diving into this BPF code, trying to figure out the best approach for fuzzing, keeping in mind the need to target specific logic and entrypoints rather than broad, ubiquitous functions. `BPF_SYSCALL` and `BPF_JIT` are obviously key configurations to keep in mind, and the `kernel/bpf/Kconfig` and `lib/Kconfig.debug` configs could tell us more. It looks like BTF compilation is tied to `CONFIG_BPF_SYSCALL` from the `kernel/bpf/Makefile`, with `DEBUG_INFO_BTF` used for vmlinux BTF, which is important. Now, let's look at the functions themselves. I've broken down the modified files and functions. I see changes in `arch/x86/net/bpf_jit_comp.c`, specifically `reg2pt_regs` and `bpf_jit_supports_arena_scalar`. `kernel/bpf/btf.c` has several static functions like `btf_name_valid_identifier`, `btf_datasec_resolve`, `btf_var_is_piece`, `btf_datasec_show`, and `btf_func_check`. Then there's `kernel/bpf/core.c` with the non-static `bpf_prog_ksym_set_name` and `bpf_jit_supports_arena_scalar`. `kernel/bpf/fixups.c` introduces `arena_scalar_access` (static) and `bpf_do_misc_fixups` (non-static). `kernel/bpf/syscall.c` modifies `bpf_prog_load`, and finally `kernel/bpf/verifier.c` has a set of static functions: `is_arena_scalar`, `atomic_ptr_type_ok`, `mem_access_reg`, `check_load_mem`, `check_store_reg`, `check_atomic_rmw`, `adjust_ptr_min_max_vals`, and `do_check_insn`. And there is a non-static, `bpf_check` function as well. The rule is to focus the fuzzing on specific feature logic and entrypoints. That means avoiding the common functions and instead targeting entry points and functions that implement the logic added or changed. In this case, I'll need to figure out which of these functions have the most specific logic, and which are the main entrypoints to this new feature. Given that static inline functions can't be targeted, I should be focused on the non-static functions and their callers. I need to confirm which symbols exist in vmlinux to see what's directly fuzzable. So, looking at the modified functions, I need to focus on: `bpf_check`, `bpf_do_misc_fixups`, `bpf_prog_ksym_set_name`, `bpf_jit_supports_arena_scalar`, `btf_datasec_resolve`, `btf_func_check`, `adjust_ptr_min_max_vals`, `check_load_mem`, `check_store_reg`, `check_atomic_rmw`, and `is_arena_scalar`. I need to remember that even static functions in .c files *can* have symbols unless inlined. Okay, let's investigate the callers of those static functions. In `kernel/bpf/verifier.c`, I see `adjust_ptr_min_max_vals` is called by `adjust_reg_min_max_vals`. Now, is `bpf_check` the main entrypoint? Yes, it looks like it's the core of the verifier, called during `bpf_prog_load`. This is definitely something I need to keep in mind, and is the place to start. And then, `bpf_do_misc_fixups` is non-static, and it calls `arena_scalar_access`, so this seems important as well. I should check `kernel/bpf/btf.c`, specifically around lines 5406, 5450, and 5760 to see which callers are present. |
| 16/2 | 2026/10/03 02:37 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/btf.c",
"FirstLine": 5400,
"LineCount": 40
}Results: {
"Contents": "5400:\t\t\tu32 var_type_id = vsi-\u003etype, type_id;\n5401:\t\t\tconst struct btf_type *var_type = btf_type_by_id(env-\u003ebtf,\n5402:\t\t\t\t\t\t\t\t\t var_type_id);\n5403:\t\t\tif (!var_type || !btf_type_is_var(var_type)) {\n5404:\t\t\t\tbtf_verifier_log_vsi(env, v-\u003et, vsi,\n5405:\t\t\t\t\t\t \"Not a VAR kind member\");\n5406:\t\t\t\treturn -EINVAL;\n5407:\t\t\t}\n5408:\t\n5409:\t\t\tif (!env_type_is_resolve_sink(env, var_type) \u0026\u0026\n5410:\t\t\t !env_type_is_resolved(env, var_type_id)) {\n5411:\t\t\t\tenv_stack_set_next_member(env, i + 1);\n5412:\t\t\t\treturn env_stack_push(env, var_type, var_type_id);\n5413:\t\t\t}\n5414:\t\n5415:\t\t\t/*\n5416:\t\t\t * The variable can be smaller than its type. It's a piece of\n5417:\t\t\t * a variable that the compiler split then, with the type of\n5418:\t\t\t * the whole variable.\n5419:\t\t\t */\n5420:\t\t\ttype_id = var_type-\u003etype;\n5421:\t\t\tif (!btf_type_id_size(btf, \u0026type_id, NULL)) {\n5422:\t\t\t\tbtf_verifier_log_vsi(env, v-\u003et, vsi, \"Invalid type\");\n5423:\t\t\t\treturn -EINVAL;\n5424:\t\t\t}\n5425:\t\t}\n5426:\t\n5427:\t\tenv_stack_pop_resolved(env, 0, 0);\n5428:\t\treturn 0;\n5429:\t}\n5430:\t\n5431:\tstatic void btf_datasec_log(struct btf_verifier_env *env,\n5432:\t\t\t\t const struct btf_type *t)\n5433:\t{\n5434:\t\tbtf_verifier_log(env, \"size=%u vlen=%u\", t-\u003esize, btf_type_vlen(t));\n5435:\t}\n5436:\t\n5437:\t/* A piece of a variable is smaller than its type, which is the one of the whole variable */\n5438:\tstatic bool btf_var_is_piece(const struct btf *btf, const struct btf_type *var,\n5439:\t\t\t\t const struct btf_var_secinfo *vsi)\n"
} |
| 17/2 | 2026/10/03 02:37 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 61427 output: 44 thoughts: 25 Thoughts: Okay, here's my attempt at summarizing those thoughts, tailored for an expert audience, and written in the first person: **Analyzing `btf_datasec_resolve` and `btf_func_check` Callers** Alright, time to dig in. My immediate thought is to trace the call paths of `btf_datasec_resolve` and `btf_func_check`. I want to understand the *why* behind their invocations. Specifically, I'm trying to identify the contexts and scenarios that trigger these functions. Are they being called during BTF (BPF Type Format) loading, verification, or program execution? Knowing the callers gives me critical insight into the data they're operating on and the intent of the code. I'm especially keen to see if there are any subtle patterns, performance bottlenecks, or potential security vulnerabilities emerging from how these functions are being used. Are there any indirect calls I should be looking for? What are the implications of the execution flow? Time to get the call stack trace and start sifting. |
| 18/2 | 2026/10/03 02:37 | tool | grepper |
0mArgs:{
"Expression": "btf_datasec_resolve|btf_func_check",
"PathPrefix": "kernel/bpf/btf.c"
}Results: {
"Output": "kernel/bpf/btf.c=474=static int btf_resolve(struct btf_verifier_env *env,\n--\nkernel/bpf/btf.c-476-\nkernel/bpf/btf.c:477:static int btf_func_check(struct btf_verifier_env *env,\nkernel/bpf/btf.c-478-\t\t\t const struct btf_type *t);\n--\nkernel/bpf/btf.c=5187=static const struct btf_kind_operations func_proto_ops = {\n--\nkernel/bpf/btf.c-5196-\t *\nkernel/bpf/btf.c:5197:\t * Hence, there is no btf_func_check_member().\nkernel/bpf/btf.c-5198-\t */\n--\nkernel/bpf/btf.c-5204-\nkernel/bpf/btf.c:5205:static s32 btf_func_check_meta(struct btf_verifier_env *env,\nkernel/bpf/btf.c-5206-\t\t\t const struct btf_type *t,\n--\nkernel/bpf/btf.c=5230=static int btf_func_resolve(struct btf_verifier_env *env,\n--\nkernel/bpf/btf.c-5236-\nkernel/bpf/btf.c:5237:\terr = btf_func_check(env, t);\nkernel/bpf/btf.c-5238-\tif (err)\n--\nkernel/bpf/btf.c=5245=static const struct btf_kind_operations func_ops = {\nkernel/bpf/btf.c:5246:\t.check_meta = btf_func_check_meta,\nkernel/bpf/btf.c-5247-\t.resolve = btf_func_resolve,\n--\nkernel/bpf/btf.c=5318=static s32 btf_datasec_check_meta(struct btf_verifier_env *env,\n--\nkernel/bpf/btf.c-5390-\nkernel/bpf/btf.c:5391:static int btf_datasec_resolve(struct btf_verifier_env *env,\nkernel/bpf/btf.c-5392-\t\t\t const struct resolve_vertex *v)\n--\nkernel/bpf/btf.c=5476=static const struct btf_kind_operations datasec_ops = {\nkernel/bpf/btf.c-5477-\t.check_meta\t\t= btf_datasec_check_meta,\nkernel/bpf/btf.c:5478:\t.resolve\t\t= btf_datasec_resolve,\nkernel/bpf/btf.c-5479-\t.check_member\t\t= btf_df_check_member,\n--\nkernel/bpf/btf.c=5659=static int btf_func_proto_check(struct btf_verifier_env *env,\n--\nkernel/bpf/btf.c-5753-\nkernel/bpf/btf.c:5754:static int btf_func_check(struct btf_verifier_env *env,\nkernel/bpf/btf.c-5755-\t\t\t const struct btf_type *t)\n"
} |
| 19/2 | 2026/10/03 02:37 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 62597 output: 36 thoughts: 162 Thoughts: Okay, let me break this down. **My Analysis of BTF and BPF Kernel Interactions** Alright, so I'm focusing on the mechanics of BTF (BPF Type Format) resolution and loading within the kernel. I see that `btf_datasec_resolve` and `btf_func_resolve` are the crucial functions tucked away in `datasec_ops.resolve` and `func_ops.resolve`. Those are the workhorses for resolving BTF type information for data sections and functions, respectively. That's good to know. Now, where does it all *begin*? I need to think about the BTF loading entry points. That would be `bpf_btf_load`, or potentially `btf_new_fd` and `btf_parse`, depending on the specific loading pathway. I need to keep those in mind as I build the mental model. Next, I need to understand `bpf_prog_ksym_set_name`. It looks like this function is invoked when setting up kernel symbol information (ksym) for a BPF program. I'll need to figure out exactly how it relates to BTF and how the ksym information is used. I should probably find the exact point of `bpf_prog_ksym_set_name`. I see that it is defined in `kernel/bpf/core.c` and is used to set up ksyms for a BPF program. Now I must track all the calls to `bpf_prog_ksym_set_name`, as that will expose how it fits into the whole BPF program life cycle. Gotta see the bigger picture, you know? |
| 20/2 | 2026/10/03 02:37 | tool | grepper |
0mArgs:{
"Expression": "bpf_prog_ksym_set_name",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/core.c=565=static void\nkernel/bpf/core.c:566:bpf_prog_ksym_set_name(struct bpf_prog *prog)\nkernel/bpf/core.c-567-{\n--\nkernel/bpf/core.c=675=void bpf_prog_kallsyms_add(struct bpf_prog *fp)\n--\nkernel/bpf/core.c-681-\tbpf_prog_ksym_set_addr(fp);\nkernel/bpf/core.c:682:\tbpf_prog_ksym_set_name(fp);\nkernel/bpf/core.c-683-\tfp-\u003eaux-\u003eksym.prog = true;\n"
} |
| 21/2 | 2026/10/03 02:38 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 63354 output: 38 thoughts: 197 Thoughts: **Analyzing BPF Verifier Changes** Okay, so `bpf_prog_ksym_set_name` gets called by `bpf_prog_kallsyms_add` – that's the setup, got it. Now, let's dive into the core of the verifier changes within `kernel/bpf/verifier.c`. I need to understand the implications of these modifications. First, I'm noting the involvement of `is_arena_scalar`, `mem_access_reg` within the context of the verifier environment. Next, I see that functions like `check_load_mem`, `check_store_reg`, and `check_atomic_rmw` are key components. `adjust_ptr_min_max_vals` is likely crucial for bounds checking, and then the actual instruction analysis is handled by `do_check_insn` with the overarching check within `bpf_check`. Specifically, I need to read into `adjust_ptr_min_max_vals`. This one's important for ensuring the safety of memory operations. Let's see how they're handling pointer bounds. |
| 22/2 | 2026/10/03 02:38 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/verifier.c",
"FirstLine": 15590,
"LineCount": 40
}Results: {
"Contents": "15590:\t\tbool known = tnum_is_const(off_reg-\u003evar_off);\n15591:\t\ts64 smin_val = reg_smin(off_reg), smax_val = reg_smax(off_reg);\n15592:\t\tu64 umin_val = reg_umin(off_reg), umax_val = reg_umax(off_reg);\n15593:\t\tstruct bpf_sanitize_info info = {};\n15594:\t\tu8 opcode = BPF_OP(insn-\u003ecode);\n15595:\t\tu32 dst = insn-\u003edst_reg;\n15596:\t\tconst char *reason;\n15597:\t\tint ret, bounds_ret;\n15598:\t\tbool to_scalar;\n15599:\t\n15600:\t\tdst_reg = \u0026regs[dst];\n15601:\t\t/*\n15602:\t\t * Only += and -= keep a pointer. Any other op makes a number of it,\n15603:\t\t * which is fine when the program may see values of pointers anyway.\n15604:\t\t */\n15605:\t\tto_scalar = opcode != BPF_ADD \u0026\u0026 opcode != BPF_SUB \u0026\u0026 env-\u003eallow_ptr_leaks;\n15606:\t\n15607:\t\tif ((known \u0026\u0026 (smin_val != smax_val || umin_val != umax_val)) ||\n15608:\t\t smin_val \u003e smax_val || umin_val \u003e umax_val) {\n15609:\t\t\t/* Taint dst register if offset had invalid bounds derived from\n15610:\t\t\t * e.g. dead branches.\n15611:\t\t\t */\n15612:\t\t\t__mark_reg_unknown(env, dst_reg);\n15613:\t\t\treturn 0;\n15614:\t\t}\n15615:\t\n15616:\t\tif (BPF_CLASS(insn-\u003ecode) != BPF_ALU64 \u0026\u0026 !to_scalar) {\n15617:\t\t\t/* 32-bit ALU ops on pointers produce (meaningless) scalars */\n15618:\t\t\tif (opcode == BPF_SUB \u0026\u0026 env-\u003eallow_ptr_leaks) {\n15619:\t\t\t\t__mark_reg_unknown(env, dst_reg);\n15620:\t\t\t\treturn 0;\n15621:\t\t\t}\n15622:\t\n15623:\t\t\tverbose(env,\n15624:\t\t\t\t\"R%d 32-bit pointer arithmetic prohibited\\n\",\n15625:\t\t\t\tdst);\n15626:\t\t\treason = bpf_diag_fmt(\n15627:\t\t\t\tenv, \"R%d holds %s. 32-bit ALU operations on pointers discard pointer tracking, so the verifier cannot keep the result as a safe pointer.\",\n15628:\t\t\t\tptr_regno, bpf_diag_reg_type_plain(env, ptr_reg-\u003etype));\n15629:\t\t\tbpf_diag_register_type(\n"
} |
| 23/2 | 2026/10/03 02:38 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 64635 output: 308 thoughts: 2750 Thoughts: **Fuzzing Target: BPF Arena Scalar Support** Okay, so I'm looking at this patch and it's making some pretty significant changes to how the BPF verifier handles pointer arithmetic, specifically when `BPF_F_ARENA_SCALAR` is enabled. It looks like `adjust_ptr_min_max_vals` in the verifier is now allowing bitwise operations, shifts, and other non-ADD/SUB operations on pointers, but it converts them to scalars when pointer leaks are permitted. Interesting. Then, when `BPF_F_ARENA_SCALAR` is set, the system treats loads and stores through a scalar register as accesses to the BPF arena at the low 32 bits of that scalar. The `bpf_do_misc_fixups()` function then rewrites instructions that access the arena through scalar registers, using `arena_scalar_access()` to perform the actual manipulation. It looks like they're being rewritten to use `BPF_REG_AX` as an intermediate. We also see updates to the `reg2pt_regs[]` table in the x86 JIT compilation code, and the addition of `bpf_jit_supports_arena_scalar()` returns true. Furthermore, BTF has some tweaks, specifically in relaxing `btf_name_valid_identifier()` to `isprint()`, allowing variables smaller than their types in `btf_datasec_resolve()`, handling `btf_var_is_piece` in `btf_datasec_show()`, and skipping argument checks for `BTF_FUNC_STATIC` in `btf_func_check()`. So, what we're looking at is core support for `BPF_F_ARENA_SCALAR` across the verifier, JIT, BTF, and fixup mechanisms. This is pretty substantial and appears to be motivated by Rust BPF programs. Since all of this code is accessible via the `bpf()` syscall (`BPF_PROG_LOAD`, `BPF_BTF_LOAD`, `BPF_MAP_CREATE`), that makes it a prime candidate for fuzzing. I'll need `BPF_SYSCALL` (enables the `bpf()` syscall and subsystems) and `BPF_JIT` (needed for `bpf_jit_supports_arena_scalar()` to be enabled). `BPF_JIT` depends on `BPF` and either `HAVE_CBPF_JIT` or `HAVE_EBPF_JIT`, and on x86-64, it defaults to `HAVE_EBPF_JIT`. Both `MMU` and `64BIT` are standard arch configs on amd64. For `FocusSymbols`, I'm going to target functions that directly handle the new logic or are the entry points. These are: `bpf_prog_load` (syscall handler, also checks the flag), `bpf_check` (the verifier entry point, checks instructions using the updated logic), `is_arena_scalar`, `mem_access_reg`, `arena_scalar_access`, `bpf_do_misc_fixups`, `adjust_ptr_min_max_vals`, `bpf_jit_supports_arena_scalar`, `btf_datasec_resolve`, `btf_func_check`, `btf_var_is_piece`, `bpf_prog_ksym_set_name`. However, functions that are more directly part of the new logic are `bpf_jit_supports_arena_scalar`, `bpf_do_misc_fixups`, `arena_scalar_access`, `mem_access_reg`, `is_arena_scalar`, `adjust_ptr_min_max_vals`, `btf_datasec_resolve`, `btf_func_check`, `btf_var_is_piece`. These functions handle the new behavior, and are also specific to the changed logic. There aren't any ubiquitous functions here, these are all pretty specific BPF routines. I'm confident in saying that this is definitely fuzzable. |
| 24/2 | 2026/10/03 02:38 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT"
],
"FocusSymbols": [
"bpf_do_misc_fixups",
"arena_scalar_access",
"mem_access_reg",
"is_arena_scalar",
"adjust_ptr_min_max_vals",
"bpf_jit_supports_arena_scalar",
"btf_datasec_resolve",
"btf_func_check",
"btf_name_valid_identifier"
],
"Reasoning": "The patch series adds BPF_F_ARENA_SCALAR support and relaxes BTF validation (to support Rust BPF programs). It introduces new pointer-to-scalar conversion rules in adjust_ptr_min_max_vals, allows scalar registers to access BPF arena memory in the verifier (mem_access_reg, check_load_mem, check_store_reg, check_atomic_rmw), patches arena scalar accesses in bpf_do_misc_fixups, maps BPF_REG_AX in x86 JIT, and modifies BTF validation logic (btf_datasec_resolve, btf_func_check, btf_name_valid_identifier). All changes affect core, security-critical BPF subsystem code directly reachable via the bpf() syscall.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT"
],
"FocusSymbols": [
"bpf_do_misc_fixups",
"arena_scalar_access",
"mem_access_reg",
"is_arena_scalar",
"adjust_ptr_min_max_vals",
"bpf_jit_supports_arena_scalar",
"btf_datasec_resolve",
"btf_func_check",
"btf_name_valid_identifier"
],
"Reasoning": "The patch series adds BPF_F_ARENA_SCALAR support and relaxes BTF validation (to support Rust BPF programs). It introduces new pointer-to-scalar conversion rules in adjust_ptr_min_max_vals, allows scalar registers to access BPF arena memory in the verifier (mem_access_reg, check_load_mem, check_store_reg, check_atomic_rmw), patches arena scalar accesses in bpf_do_misc_fixups, maps BPF_REG_AX in x86 JIT, and modifies BTF validation logic (btf_datasec_resolve, btf_func_check, btf_name_valid_identifier). All changes affect core, security-critical BPF subsystem code directly reachable via the bpf() syscall.",
"WorthFuzzing": true
} |
| 25/1 | 2026/10/03 02:38 | action | If |
4mArgs:{
"WorthFuzzing": true
}Results: null |
| 26/2 | 2026/10/03 02:38 | agent | kmsan-evaluator |
4mModel:core Args: null Results: {
"KMSANReasoning": "The patch series adds support for BPF_F_ARENA_SCALAR to allow BPF programs (particularly those compiled from Rust) to access BPF arena memory using scalar values, updates BTF parsing/validation for Rust identifiers and split variables, and enables arithmetic operations on pointers to degrade to scalar values when pointer leaks are permitted.\n\nFrom a memory sanitization perspective:\n1. KMSAN applicability: KMSAN detects uses of uninitialized memory (stack, heap, or page allocations) and leaks of uninitialized memory to user space.\n - The temporary stack structure `struct bpf_reg_state arena` introduced in `verifier.c` (`mem_access_reg()`) is explicitly zeroed with `memset(arena, 0, sizeof(*arena))`.\n - `env-\u003einsn_aux_data` is allocated with `__GFP_ZERO` / `vrealloc` with `__GFP_ZERO`.\n - Instruction patches generated in `arena_scalar_access()` fully initialize all instruction fields in `insn_buf`.\n - In `btf.c`, `btf_var_is_piece()` relies on `btf_resolve_size()`, where `size` is only evaluated when `btf_resolve_size()` succeeds; and `btf_datasec_show()` skips pieces to avoid reads beyond section bounds.\n - BPF arena memory pages are allocated zero-initialized.\n - No uninitialized data or struct padding is copied to user space.\n2. KASAN and general bug detectors: Any potential issues arising from this series would involve verifier state logic, bounds checking, or JIT instruction rewriting, which fall under the purview of standard functional testing, KASAN (for out-of-bounds memory accesses), and verifier invariants checks, rather than uninitialized memory tracking.\n\nConsequently, there is no risk of uninitialized memory access or info-leaks, and a dedicated KMSAN fuzzing session is not justified.",
"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: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit 204514513c3972ac15a879bd61fade9e9f92eecc
Author: syz-cluster <triage@syzkaller.com>
Date: Sat Oct 3 02:37:14 2026 +0000
syz-cluster: applied patch under review
diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
index 475e706534543..6979c8ead0e8b 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -3413,6 +3413,11 @@ bool bpf_jit_supports_arena(void)
return true;
}
+bool bpf_jit_supports_arena_scalar(void)
+{
+ return true;
+}
+
bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)
{
if (!in_arena)
diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
index 6c7a0578760e7..8199d28e2a128 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -236,6 +236,7 @@ static const int reg2pt_regs[] = {
[BPF_REG_7] = offsetof(struct pt_regs, r13),
[BPF_REG_8] = offsetof(struct pt_regs, r14),
[BPF_REG_9] = offsetof(struct pt_regs, r15),
+ [BPF_REG_AX] = offsetof(struct pt_regs, r10),
};
/*
@@ -4667,6 +4668,11 @@ bool bpf_jit_supports_arena(void)
return true;
}
+bool bpf_jit_supports_arena_scalar(void)
+{
+ return true;
+}
+
bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)
{
if (!in_arena)
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 811342e3c0419..0074f1356c584 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -672,6 +672,7 @@ struct bpf_insn_aux_data {
bool non_sleepable; /* helper/kfunc may be called from non-sleepable context */
bool is_iter_next; /* bpf_iter_<type>_next() kfunc call */
bool call_with_percpu_alloc_ptr; /* {this,per}_cpu_ptr() with prog percpu alloc */
+ bool arena_scalar; /* ldx/stx/st/atomic through a number, it's an address in arena */
u8 alu_state; /* used in combination with alu_limit */
/* true if STX or LDX instruction is a part of a spill/fill
* pattern for a bpf_fastcall call.
@@ -943,6 +944,7 @@ struct bpf_verifier_env {
bool strict_alignment; /* perform strict pointer alignment checks */
bool test_state_freq; /* test verifier with different pruning frequency */
bool test_reg_invariants; /* fail verification on register invariants violations */
+ bool arena_scalar; /* load and store through a number is arena access */
struct bpf_verifier_state *cur_state; /* current verifier state */
/* Search pruning optimization, array of list_heads for
* lists of struct bpf_verifier_state_list.
diff --git a/include/linux/filter.h b/include/linux/filter.h
index e42eccb0990e6..9339c6131f8ff 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -1250,6 +1250,7 @@ bool bpf_jit_supports_far_kfunc_call(void);
bool bpf_jit_supports_exceptions(void);
bool bpf_jit_supports_ptr_xchg(void);
bool bpf_jit_supports_arena(void);
+bool bpf_jit_supports_arena_scalar(void);
bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena);
bool bpf_jit_supports_private_stack(void);
bool bpf_jit_supports_large_stack(void);
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 4687c33109968..e0ed44b1bbcb7 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -1344,6 +1344,12 @@ enum bpf_perf_event_type {
/* The verifier internal test flag. Behavior is undefined */
#define BPF_F_TEST_REG_INVARIANTS (1U << 7)
+/*
+ * Load and store through a number is an access to the arena of the program
+ * at the low 32 bits of the number. It's for programs written in Rust.
+ */
+#define BPF_F_ARENA_SCALAR (1U << 8)
+
/* link_create.kprobe_multi.flags used in LINK_CREATE command for
* BPF_TRACE_KPROBE_MULTI attach type to create return probe.
*/
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 8cc17a1cd25c6..0630675377aaa 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -901,16 +901,6 @@ static bool btf_name_offset_valid(const struct btf *btf, u32 offset)
return offset < btf->hdr.str_len;
}
-static bool __btf_name_char_ok(char c, bool first)
-{
- if ((first ? !isalpha(c) :
- !isalnum(c)) &&
- c != '_' &&
- c != '.')
- return false;
- return true;
-}
-
const char *btf_str_by_offset(const struct btf *btf, u32 offset)
{
while (offset < btf->start_str_off)
@@ -923,20 +913,21 @@ const char *btf_str_by_offset(const struct btf *btf, u32 offset)
return NULL;
}
+/* Names in BTF of Rust are not C identifiers. Allow any printable character */
static bool btf_name_valid_identifier(const struct btf *btf, u32 offset)
{
/* offset must be valid */
const char *src = btf_str_by_offset(btf, offset);
const char *src_limit;
- if (!__btf_name_char_ok(*src, true))
+ if (!isprint(*src))
return false;
/* set a limit on identifier length */
src_limit = src + KSYM_NAME_LEN;
src++;
while (*src && src < src_limit) {
- if (!__btf_name_char_ok(*src, false))
+ if (!isprint(*src))
return false;
src++;
}
@@ -5406,7 +5397,7 @@ static int btf_datasec_resolve(struct btf_verifier_env *env,
env->resolve_mode = RESOLVE_TBD;
for_each_vsi_from(i, v->next_member, v->t, vsi) {
- u32 var_type_id = vsi->type, type_id, type_size = 0;
+ u32 var_type_id = vsi->type, type_id;
const struct btf_type *var_type = btf_type_by_id(env->btf,
var_type_id);
if (!var_type || !btf_type_is_var(var_type)) {
@@ -5421,16 +5412,16 @@ static int btf_datasec_resolve(struct btf_verifier_env *env,
return env_stack_push(env, var_type, var_type_id);
}
+ /*
+ * The variable can be smaller than its type. It's a piece of
+ * a variable that the compiler split then, with the type of
+ * the whole variable.
+ */
type_id = var_type->type;
- if (!btf_type_id_size(btf, &type_id, &type_size)) {
+ if (!btf_type_id_size(btf, &type_id, NULL)) {
btf_verifier_log_vsi(env, v->t, vsi, "Invalid type");
return -EINVAL;
}
-
- if (vsi->size < type_size) {
- btf_verifier_log_vsi(env, v->t, vsi, "Invalid size");
- return -EINVAL;
- }
}
env_stack_pop_resolved(env, 0, 0);
@@ -5443,6 +5434,16 @@ static void btf_datasec_log(struct btf_verifier_env *env,
btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
}
+/* A piece of a variable is smaller than its type, which is the one of the whole variable */
+static bool btf_var_is_piece(const struct btf *btf, const struct btf_type *var,
+ const struct btf_var_secinfo *vsi)
+{
+ u32 size;
+
+ return IS_ERR(btf_resolve_size(btf, btf_type_by_id(btf, var->type), &size)) ||
+ vsi->size < size;
+}
+
static void btf_datasec_show(const struct btf *btf,
const struct btf_type *t, u32 type_id,
void *data, u8 bits_offset,
@@ -5450,6 +5451,7 @@ static void btf_datasec_show(const struct btf *btf,
{
const struct btf_var_secinfo *vsi;
const struct btf_type *var;
+ bool comma = false;
u32 i;
if (!btf_show_start_type(show, t, type_id, data))
@@ -5459,8 +5461,12 @@ static void btf_datasec_show(const struct btf *btf,
__btf_name_by_offset(btf, t->name_off));
for_each_vsi(i, t, vsi) {
var = btf_type_by_id(btf, vsi->type);
- if (i)
+ /* there is less data than the type takes */
+ if (btf_var_is_piece(btf, var, vsi))
+ continue;
+ if (comma)
btf_show(show, ",");
+ comma = true;
btf_type_ops(var)->show(btf, var, vsi->type,
data + vsi->offset, bits_offset, show);
}
@@ -5761,6 +5767,10 @@ static int btf_func_check(struct btf_verifier_env *env,
return -EINVAL;
}
+ /* Rust leaves out names of some arguments of static functions */
+ if (btf_func_linkage(t) == BTF_FUNC_STATIC)
+ return 0;
+
args = (const struct btf_param *)(proto_type + 1);
nr_args = btf_type_vlen(proto_type);
for (i = 0; i < nr_args; i++) {
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index d3b8b626ec0f7..36900b02d6684 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -18,6 +18,7 @@
*/
#include <uapi/linux/btf.h>
+#include <linux/ctype.h>
#include <linux/filter.h>
#include <linux/sched/signal.h>
#include <linux/skbuff.h>
@@ -589,6 +590,10 @@ bpf_prog_ksym_set_name(struct bpf_prog *prog)
prog->aux->func_info[prog->aux->func_idx].type_id);
func_name = btf_name_by_offset(prog->aux->btf, type->name_off);
snprintf(sym, (size_t)(end - sym), "_%s", func_name);
+ /* the name of a function of Rust is not an identifier */
+ for (; *sym; sym++)
+ if (!isalnum(*sym) && *sym != '_' && *sym != '.')
+ *sym = '_';
return;
}
@@ -3429,6 +3434,12 @@ bool __weak bpf_jit_supports_arena(void)
return false;
}
+/* Whether JIT takes BPF_REG_AX as the address of arena access */
+bool __weak bpf_jit_supports_arena_scalar(void)
+{
+ return false;
+}
+
bool __weak bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)
{
return false;
diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
index 37cf130ebb57b..b4bfe5522003b 100644
--- a/kernel/bpf/fixups.c
+++ b/kernel/bpf/fixups.c
@@ -46,6 +46,31 @@ static bool is_addr_space_cast32(struct bpf_prog *prog, const struct bpf_insn *i
return false;
}
+/*
+ * The insn accesses arena through a number. JITs add the base of arena
+ * to the register as it is, so the access goes through the low 32 bits of
+ * the number in BPF_REG_AX. Registers of the program are not changed: the
+ * number may be compared or stored later, and on another path the register
+ * may be PTR_TO_ARENA.
+ *
+ * Constant blinding leaves insns that use BPF_REG_AX alone, so the immediate
+ * of st through a number is not blinded.
+ */
+static int arena_scalar_access(const struct bpf_insn *insn, struct bpf_insn *buf)
+{
+ bool load = BPF_CLASS(insn->code) == BPF_LDX || bpf_atomic_is_load_acq(insn);
+ struct bpf_insn *patch = buf;
+
+ *patch++ = BPF_MOV32_REG(BPF_REG_AX, load ? insn->src_reg : insn->dst_reg);
+ *patch = *insn;
+ if (load)
+ patch->src_reg = BPF_REG_AX;
+ else
+ patch->dst_reg = BPF_REG_AX;
+ patch++;
+ return patch - buf;
+}
+
/* Return the regno defined by the insn, or -1. */
static int insn_def_regno(const struct bpf_insn *insn)
{
@@ -1785,6 +1810,20 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
/* Convert BPF_CLASS(insn->code) == BPF_ALU64 to 32-bit ALU */
insn->code = BPF_ALU | BPF_OP(insn->code) | BPF_SRC(insn->code);
+ if (env->insn_aux_data[i + delta].arena_scalar) {
+ cnt = arena_scalar_access(insn, insn_buf);
+
+ new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
+ if (!new_prog)
+ return -ENOMEM;
+
+ delta += cnt - 1;
+ prog = new_prog;
+ env->prog = prog;
+ insn = prog->insnsi + i + delta;
+ goto next_insn;
+ }
+
/* Make sdiv/smod divide-by-minus-one exceptions impossible. */
if ((insn->code == (BPF_ALU64 | BPF_MOD | BPF_K) ||
insn->code == (BPF_ALU64 | BPF_DIV | BPF_K) ||
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index ac52f4ae414ce..778bca2abc042 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2948,9 +2948,13 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_at
BPF_F_XDP_HAS_FRAGS |
BPF_F_XDP_DEV_BOUND_ONLY |
BPF_F_TEST_REG_INVARIANTS |
+ BPF_F_ARENA_SCALAR |
BPF_F_TOKEN_FD))
return -EINVAL;
+ if ((attr->prog_flags & BPF_F_ARENA_SCALAR) && !bpf_jit_supports_arena_scalar())
+ return -EOPNOTSUPP;
+
bpf_prog_load_fixup_attach_type(attr);
if (attr->prog_flags & BPF_F_TOKEN_FD) {
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index b840b3eb9b229..2daee0130e5af 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -5250,6 +5250,19 @@ static bool is_arena_reg(struct bpf_verifier_env *env, int regno)
return reg->type == PTR_TO_ARENA;
}
+/*
+ * There are no address spaces in Rust, addresses of arena are plain numbers.
+ * When the program is loaded with BPF_F_ARENA_SCALAR and has an arena a load or
+ * a store through a number is an access to arena at the low 32 bits of it,
+ * like the access through PTR_TO_ARENA is. The register stays a number.
+ */
+static bool is_arena_scalar(struct bpf_verifier_env *env, int regno)
+{
+ const struct bpf_reg_state *reg = reg_state(env, regno);
+
+ return reg->type == SCALAR_VALUE && env->arena_scalar && env->prog->aux->arena;
+}
+
static bool is_load_acq_unsafe(struct bpf_verifier_env *env, int regno,
struct bpf_insn *insn)
{
@@ -5280,7 +5293,7 @@ static bool atomic_ptr_type_ok(struct bpf_verifier_env *env, int regno,
return false;
if (is_sk_reg(env, regno))
return false;
- if (is_arena_reg(env, regno))
+ if (is_arena_reg(env, regno) || is_arena_scalar(env, regno))
return bpf_jit_supports_insn(insn, true);
if (is_load_acq_unsafe(env, regno, insn))
return false;
@@ -7161,6 +7174,22 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, struct b
static int save_aux_ptr_type(struct bpf_verifier_env *env, enum bpf_reg_type type,
bool allow_trust_mismatch);
+/*
+ * Returns the register to check the access of the current insn with.
+ * For a number in a program with an arena that is 'arena'.
+ */
+static struct bpf_reg_state *mem_access_reg(struct bpf_verifier_env *env, int regno,
+ struct bpf_reg_state *arena)
+{
+ if (!is_arena_scalar(env, regno))
+ return cur_regs(env) + regno;
+
+ memset(arena, 0, sizeof(*arena));
+ arena->type = PTR_TO_ARENA;
+ env->insn_aux_data[env->insn_idx].arena_scalar = true;
+ return arena;
+}
+
static int check_load_mem(struct bpf_verifier_env *env, struct bpf_insn *insn,
bool strict_alignment_once, bool is_ldsx,
bool allow_trust_mismatch, const char *ctx)
@@ -7168,6 +7197,7 @@ static int check_load_mem(struct bpf_verifier_env *env, struct bpf_insn *insn,
struct bpf_verifier_state *vstate = env->cur_state;
struct bpf_func_state *state = vstate->frame[vstate->curframe];
struct bpf_reg_state *regs = cur_regs(env);
+ struct bpf_reg_state arena, *src_reg;
enum bpf_reg_type src_reg_type;
int err;
@@ -7189,15 +7219,16 @@ static int check_load_mem(struct bpf_verifier_env *env, struct bpf_insn *insn,
if (err)
return err;
- src_reg_type = regs[insn->src_reg].type;
+ src_reg = mem_access_reg(env, insn->src_reg, &arena);
+ src_reg_type = src_reg->type;
/*
* check_stack_read_fixed_off() may refine the modification's origin to
* the source stack slot.
*/
bpf_diag_mod_begin(env, ®s[insn->dst_reg], NULL, BPF_DIAG_MOD_WRITE);
- err = check_mem_access(env, env->insn_idx, regs + insn->src_reg, argno_from_reg(insn->src_reg), insn->off,
- BPF_SIZE(insn->code), BPF_READ, insn->dst_reg,
+ err = check_mem_access(env, env->insn_idx, src_reg, argno_from_reg(insn->src_reg),
+ insn->off, BPF_SIZE(insn->code), BPF_READ, insn->dst_reg,
strict_alignment_once, is_ldsx);
err = err ?: save_aux_ptr_type(env, src_reg_type,
allow_trust_mismatch);
@@ -7214,6 +7245,7 @@ static int check_store_reg(struct bpf_verifier_env *env, struct bpf_insn *insn,
struct bpf_verifier_state *vstate = env->cur_state;
struct bpf_func_state *state = vstate->frame[vstate->curframe];
struct bpf_reg_state *regs = cur_regs(env);
+ struct bpf_reg_state arena, *dst_reg;
enum bpf_reg_type dst_reg_type;
int err;
@@ -7235,11 +7267,12 @@ static int check_store_reg(struct bpf_verifier_env *env, struct bpf_insn *insn,
if (err)
return err;
- dst_reg_type = regs[insn->dst_reg].type;
+ dst_reg = mem_access_reg(env, insn->dst_reg, &arena);
+ dst_reg_type = dst_reg->type;
/* Check if (dst_reg + off) is writeable. */
- err = check_mem_access(env, env->insn_idx, regs + insn->dst_reg, argno_from_reg(insn->dst_reg), insn->off,
- BPF_SIZE(insn->code), BPF_WRITE, insn->src_reg,
+ err = check_mem_access(env, env->insn_idx, dst_reg, argno_from_reg(insn->dst_reg),
+ insn->off, BPF_SIZE(insn->code), BPF_WRITE, insn->src_reg,
strict_alignment_once, false);
err = err ?: save_aux_ptr_type(env, dst_reg_type, false);
@@ -7249,7 +7282,7 @@ static int check_store_reg(struct bpf_verifier_env *env, struct bpf_insn *insn,
static int check_atomic_rmw(struct bpf_verifier_env *env,
struct bpf_insn *insn)
{
- struct bpf_reg_state *dst_reg;
+ struct bpf_reg_state arena, *dst_reg;
int load_reg;
int err;
@@ -7294,6 +7327,9 @@ static int check_atomic_rmw(struct bpf_verifier_env *env,
return -EACCES;
}
+ /* load_reg may be dst_reg. Look at dst_reg before it's marked as unknown. */
+ dst_reg = mem_access_reg(env, insn->dst_reg, &arena);
+
load_reg = bpf_atomic_load_reg(insn);
if (load_reg >= 0) {
/* check and record load of old value */
@@ -7302,8 +7338,6 @@ static int check_atomic_rmw(struct bpf_verifier_env *env,
return err;
}
- dst_reg = cur_regs(env) + insn->dst_reg;
-
/* Check whether we can read the memory, with second call for fetch
* case to simulate the register fill.
*/
@@ -15561,8 +15595,14 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, struct bpf_insn
u32 dst = insn->dst_reg;
const char *reason;
int ret, bounds_ret;
+ bool to_scalar;
dst_reg = ®s[dst];
+ /*
+ * Only += and -= keep a pointer. Any other op makes a number of it,
+ * which is fine when the program may see values of pointers anyway.
+ */
+ to_scalar = opcode != BPF_ADD && opcode != BPF_SUB && env->allow_ptr_leaks;
if ((known && (smin_val != smax_val || umin_val != umax_val)) ||
smin_val > smax_val || umin_val > umax_val) {
@@ -15573,7 +15613,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, struct bpf_insn
return 0;
}
- if (BPF_CLASS(insn->code) != BPF_ALU64) {
+ if (BPF_CLASS(insn->code) != BPF_ALU64 && !to_scalar) {
/* 32-bit ALU ops on pointers produce (meaningless) scalars */
if (opcode == BPF_SUB && env->allow_ptr_leaks) {
__mark_reg_unknown(env, dst_reg);
@@ -15639,6 +15679,11 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, struct bpf_insn
return -EACCES;
}
+ if (to_scalar) {
+ __mark_reg_unknown(env, dst_reg);
+ return 0;
+ }
+
/* For 'scalar += pointer', dst_reg inherits the complete pointer
* register state. Individual fields may be adjusted later by pointer
* arithmetic. Callers guarantee that below does not overwrite off_reg.
@@ -19340,15 +19385,17 @@ static int do_check_insn(struct bpf_verifier_env *env, bool *do_print_state)
return check_stack_arg_write(env, state, insn->off, NULL);
}
+ struct bpf_reg_state arena, *dst_reg;
enum bpf_reg_type dst_reg_type;
err = check_reg_arg(env, insn->dst_reg, SRC_OP);
if (err)
return err;
- dst_reg_type = cur_regs(env)[insn->dst_reg].type;
+ dst_reg = mem_access_reg(env, insn->dst_reg, &arena);
+ dst_reg_type = dst_reg->type;
- err = check_mem_access(env, env->insn_idx, cur_regs(env) + insn->dst_reg, argno_from_reg(insn->dst_reg),
+ err = check_mem_access(env, env->insn_idx, dst_reg, argno_from_reg(insn->dst_reg),
insn->off, BPF_SIZE(insn->code),
BPF_WRITE, -1, false, false);
if (err)
@@ -22507,6 +22554,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,
if (is_priv)
env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ;
env->test_reg_invariants = attr->prog_flags & BPF_F_TEST_REG_INVARIANTS;
+ env->arena_scalar = attr->prog_flags & BPF_F_ARENA_SCALAR;
env->explored_states = kvzalloc_objs(struct list_head,
state_htab_size(env),
diff --git a/tools/bpf/bpftool/btf_dumper.c b/tools/bpf/bpftool/btf_dumper.c
index e4075824343fd..3267163119dc2 100644
--- a/tools/bpf/bpftool/btf_dumper.c
+++ b/tools/bpf/bpftool/btf_dumper.c
@@ -524,6 +524,15 @@ static int btf_dumper_var(const struct btf_dumper *d, __u32 type_id,
return ret;
}
+/*
+ * The compiler splits a variable into pieces. Every piece is a VAR with the
+ * type of the whole variable, so there is less data than the type takes.
+ */
+bool btf_var_is_piece(const struct btf *btf, const struct btf_var_secinfo *vsi)
+{
+ return btf__resolve_size(btf, vsi->type) > vsi->size;
+}
+
static int btf_dumper_datasec(const struct btf_dumper *d, __u32 type_id,
const void *data)
{
@@ -542,6 +551,8 @@ static int btf_dumper_datasec(const struct btf_dumper *d, __u32 type_id,
jsonw_name(d->jw, btf__name_by_offset(d->btf, t->name_off));
jsonw_start_array(d->jw);
for (i = 0; i < vlen; i++) {
+ if (btf_var_is_piece(d->btf, &vsi[i]))
+ continue;
ret = btf_dumper_do_type(d, vsi[i].type, 0, data + vsi[i].offset);
if (ret)
break;
diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h
index fa2c877a3cbfc..6851346a60305 100644
--- a/tools/bpf/bpftool/main.h
+++ b/tools/bpf/bpftool/main.h
@@ -121,6 +121,7 @@ struct obj_refs {
};
struct btf;
+struct btf_var_secinfo;
struct bpf_line_info;
int build_pinned_obj_table(struct hashmap *table,
@@ -239,6 +240,7 @@ int btf_dumper_type(const struct btf_dumper *d, __u32 type_id,
const void *data);
void btf_dumper_type_only(const struct btf *btf, __u32 func_type_id,
char *func_only, int size);
+bool btf_var_is_piece(const struct btf *btf, const struct btf_var_secinfo *vsi);
void btf_dump_linfo_plain(const struct btf *btf,
const struct bpf_line_info *linfo,
diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c
index a60414aaa4940..eb077236d2eaa 100644
--- a/tools/bpf/bpftool/prog.c
+++ b/tools/bpf/bpftool/prog.c
@@ -348,7 +348,7 @@ static void show_prog_metadata(int fd, __u32 num_maps)
t_var = btf__type_by_id(btf, vsi->type);
name = btf__name_by_offset(btf, t_var->name_off);
- if (!has_metadata_prefix(name))
+ if (!has_metadata_prefix(name) || btf_var_is_piece(btf, vsi))
continue;
if (!printed_header) {
@@ -377,7 +377,7 @@ static void show_prog_metadata(int fd, __u32 num_maps)
t_var = btf__type_by_id(btf, vsi->type);
name = btf__name_by_offset(btf, t_var->name_off);
- if (!has_metadata_prefix(name))
+ if (!has_metadata_prefix(name) || btf_var_is_piece(btf, vsi))
continue;
if (!printed_header) {
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 4687c33109968..e0ed44b1bbcb7 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -1344,6 +1344,12 @@ enum bpf_perf_event_type {
/* The verifier internal test flag. Behavior is undefined */
#define BPF_F_TEST_REG_INVARIANTS (1U << 7)
+/*
+ * Load and store through a number is an access to the arena of the program
+ * at the low 32 bits of the number. It's for programs written in Rust.
+ */
+#define BPF_F_ARENA_SCALAR (1U << 8)
+
/* link_create.kprobe_multi.flags used in LINK_CREATE command for
* BPF_TRACE_KPROBE_MULTI attach type to create return probe.
*/
diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h
index 9d160b5b9c0ee..b37b727ef216f 100644
--- a/tools/lib/bpf/bpf_helpers.h
+++ b/tools/lib/bpf/bpf_helpers.h
@@ -248,13 +248,21 @@ enum libbpf_tristate {
#define ___bpf_fill(arr, args...) \
___bpf_apply(___bpf_fill, ___bpf_narg(args))(arr, 0, args)
+/*
+ * Format strings are in a section of their own. When global data of the object
+ * is in arena (the object has .arena.data section) libbpf keeps .rodata.str*
+ * sections in read-only maps, which is where helpers and kfuncs take strings
+ * from.
+ */
+#define ___bpf_fmt_sec __attribute__((section(".rodata.str")))
+
/*
* BPF_SEQ_PRINTF to wrap bpf_seq_printf to-be-printed values
* in a structure.
*/
#define BPF_SEQ_PRINTF(seq, fmt, args...) \
({ \
- static const char ___fmt[] = fmt; \
+ static const char ___fmt[] ___bpf_fmt_sec = fmt; \
unsigned long long ___param[___bpf_narg(args)]; \
\
_Pragma("GCC diagnostic push") \
@@ -272,7 +280,7 @@ enum libbpf_tristate {
*/
#define BPF_SNPRINTF(out, out_size, fmt, args...) \
({ \
- static const char ___fmt[] = fmt; \
+ static const char ___fmt[] ___bpf_fmt_sec = fmt; \
unsigned long long ___param[___bpf_narg(args)]; \
\
_Pragma("GCC diagnostic push") \
@@ -287,7 +295,7 @@ enum libbpf_tristate {
#ifdef BPF_NO_GLOBAL_DATA
#define BPF_PRINTK_FMT_MOD
#else
-#define BPF_PRINTK_FMT_MOD static const
+#define BPF_PRINTK_FMT_MOD static const ___bpf_fmt_sec
#endif
#define __bpf_printk(fmt, ...) \
@@ -303,7 +311,7 @@ enum libbpf_tristate {
*/
#define __bpf_vprintk(fmt, args...) \
({ \
- static const char ___fmt[] = fmt; \
+ static const char ___fmt[] ___bpf_fmt_sec = fmt; \
unsigned long long ___param[___bpf_narg(args)]; \
\
_Pragma("GCC diagnostic push") \
@@ -317,7 +325,7 @@ enum libbpf_tristate {
#define bpf_stream_printk(stream_id, fmt, args...) \
({ \
- static const char ___fmt[] = fmt; \
+ static const char ___fmt[] ___bpf_fmt_sec = fmt; \
unsigned long long ___param[___bpf_narg(args)]; \
\
_Pragma("GCC diagnostic push") \
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index fdd69aac39bde..cb09ded907732 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -552,6 +552,26 @@ struct bpf_struct_ops {
#define STRUCT_OPS_SEC ".struct_ops"
#define STRUCT_OPS_LINK_SEC ".struct_ops.link"
#define ARENA_SEC ".addr_space.1"
+/*
+ * An object with this section keeps its global data in arena instead of
+ * array maps. What is in the section doesn't matter. .data, .bss and .rodata
+ * sections become a part of the arena map of the object, like __arena global
+ * variables are. If the object doesn't declare an arena map libbpf creates
+ * one that is just large enough for the data. bpf_object__find_map_by_name()
+ * finds it by the name "arena" and bpf_map__set_max_entries() changes its
+ * size before the object is loaded.
+ * Read-only sections with pointers to functions and sections with constant
+ * strings stay frozen array maps. A constant that a helper or a kfunc takes
+ * by pointer has to be in such section.
+ * Pointers to data that are stored in data become addresses of arena.
+ * The load fails if such pointer points to a section that is not in arena,
+ * or if the arena is pinned or reused.
+ *
+ * It's for programs written in Rust. There are no address spaces in Rust,
+ * the program accesses arena through plain numbers, so programs of the object
+ * are loaded with BPF_F_ARENA_SCALAR.
+ */
+#define ARENA_DATA_SEC ".arena.data"
enum libbpf_map_type {
LIBBPF_MAP_UNSPEC,
@@ -607,6 +627,13 @@ struct bpf_map {
/* pointers to functions in the data of an internal map, see obj->func_ptrs */
struct func_ptr *func_ptrs;
size_t func_ptr_cnt;
+ /*
+ * Data of the internal map is a part of arena data at arena_off.
+ * The map is not created, unless it's a copy that is in arena.
+ */
+ bool in_arena;
+ bool arena_copy;
+ size_t arena_off;
};
enum extern_type {
@@ -775,6 +802,17 @@ struct bpf_object {
void *arena_data;
size_t arena_data_sz;
size_t arena_data_off;
+ bool data_in_arena;
+ bool arena_mapped;
+ /* pointers to data in the data of internal maps, when data is in arena */
+ struct data_ptr {
+ int sec_idx;
+ size_t sec_off;
+ int targ_sec_idx;
+ size_t targ_off;
+ char *sym_name;
+ } *data_ptrs;
+ size_t data_ptr_cnt;
void *jumptables_data;
size_t jumptables_data_sz;
@@ -1600,6 +1638,7 @@ static struct bpf_object *bpf_object__new(const char *path,
obj->efile.obj_buf = obj_buf;
obj->efile.obj_buf_sz = obj_buf_sz;
obj->efile.btf_maps_shndx = -1;
+ obj->efile.arena_data_shndx = -1;
obj->kconfig_map_idx = -1;
obj->arena_map_idx = -1;
@@ -3081,6 +3120,136 @@ static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map,
return 0;
}
+static bool map_is_const_str(const struct bpf_map *map)
+{
+ return map->libbpf_type == LIBBPF_MAP_RODATA &&
+ str_has_pfx(map->real_name, RODATA_SEC ".str");
+}
+
+static bool map_data_goes_to_arena(const struct bpf_object *obj, const struct bpf_map *map)
+{
+ int i;
+
+ switch (map->libbpf_type) {
+ case LIBBPF_MAP_DATA:
+ case LIBBPF_MAP_BSS:
+ return true;
+ case LIBBPF_MAP_RODATA:
+ break;
+ default:
+ return false;
+ }
+
+ /*
+ * Const strings that kfuncs and helpers take have to be in a frozen
+ * map. Pointers to them that are stored in data point to a copy of
+ * the strings that is in arena.
+ */
+ if (map_is_const_str(map))
+ return true;
+ /*
+ * Read-only data with pointers stays a frozen map: the kernel
+ * recognizes pointers to functions in it.
+ */
+ if (str_has_pfx(map->real_name, DATA_REL_RO_SEC))
+ return false;
+ for (i = 0; i < obj->efile.sec_cnt; i++) {
+ const struct elf_sec_desc *sec = &obj->efile.secs[i];
+
+ if (sec->sec_type == SEC_RELO && sec->shdr->sh_info == map->sec_idx)
+ return false;
+ }
+ return true;
+}
+
+/* Returns the offset of the data of the map in arena data that is sz bytes so far */
+static size_t map_arena_off(const struct bpf_object *obj, const struct bpf_map *map, size_t sz)
+{
+ size_t align = obj->efile.secs[map->sec_idx].shdr->sh_addralign;
+
+ return roundup(sz, max(align, sizeof(__u64)));
+}
+
+/*
+ * Make .data, .bss and .rodata a part of arena data. They follow __arena
+ * variables of the object, if there are any.
+ */
+static int bpf_object__init_arena_data(struct bpf_object *obj)
+{
+ const size_t page_sz = sysconf(_SC_PAGE_SIZE);
+ size_t sz = obj->arena_data_sz;
+ struct bpf_map *map;
+ void *data;
+ int i;
+
+ if (!obj->data_in_arena)
+ return 0;
+
+ for (i = 0; i < obj->nr_maps; i++) {
+ map = &obj->maps[i];
+ if (map_data_goes_to_arena(obj, map))
+ sz = map_arena_off(obj, map, sz) + map->def.value_size;
+ }
+ if (sz == obj->arena_data_sz)
+ return 0;
+
+ if (obj->arena_map_idx < 0) {
+ /* bpf_object__init_user_btf_maps() didn't look: there is no .maps section */
+ if (obj->efile.arena_data) {
+ pr_warn("elf: sec '%s': to use global __arena variables the ARENA map should be explicitly declared in SEC(\".maps\")\n",
+ ARENA_SEC);
+ return -ENOENT;
+ }
+
+ map = bpf_object__add_map(obj);
+ if (IS_ERR(map))
+ return PTR_ERR(map);
+
+ map->real_name = strdup("arena");
+ map->name = strdup("arena");
+ if (!map->real_name || !map->name) {
+ zfree(&map->real_name);
+ zfree(&map->name);
+ return -ENOMEM;
+ }
+ map->sec_idx = -1;
+ map->def.type = BPF_MAP_TYPE_ARENA;
+ map->def.max_entries = roundup(sz, page_sz) / page_sz;
+ map->def.map_flags = BPF_F_MMAPABLE;
+ obj->arena_map_idx = map - obj->maps;
+ }
+
+ data = realloc(obj->arena_data, sz);
+ if (!data)
+ return -ENOMEM;
+ memset(data + obj->arena_data_sz, 0, sz - obj->arena_data_sz);
+ sz = obj->arena_data_sz;
+ obj->arena_data = data;
+ obj->maps[obj->arena_map_idx].mmaped = data;
+
+ for (i = 0; i < obj->nr_maps; i++) {
+ map = &obj->maps[i];
+ if (!map_data_goes_to_arena(obj, map))
+ continue;
+ sz = map_arena_off(obj, map, sz);
+ map->arena_off = sz;
+ memcpy(data + sz, map->mmaped, map->def.value_size);
+ sz += map->def.value_size;
+ pr_debug("map '%s': data is in arena at offset %zu\n", map->name, map->arena_off);
+ if (map_is_const_str(map)) {
+ map->arena_copy = true;
+ continue;
+ }
+ munmap(map->mmaped, bpf_map_mmap_sz(map));
+ /* make bpf_map__initial_value() and bpf_map__set_initial_value() work */
+ map->mmaped = data + map->arena_off;
+ map->autocreate = false;
+ map->in_arena = true;
+ }
+ obj->arena_data_sz = sz;
+ return 0;
+}
+
static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,
const char *pin_root_path)
{
@@ -3173,6 +3342,7 @@ static int bpf_object__init_maps(struct bpf_object *obj,
err = err ?: bpf_object__init_global_data_maps(obj);
err = err ?: bpf_object__init_kconfig_map(obj);
err = err ?: bpf_object_init_struct_ops(obj);
+ err = err ?: bpf_object__init_arena_data(obj);
return err;
}
@@ -3987,6 +4157,11 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
if (!sh)
return -LIBBPF_ERRNO__FORMAT;
+ /* the second pass has to know it when it sees relocations of data */
+ name = elf_sec_str(obj, sh->sh_name);
+ if (name && strcmp(name, ARENA_DATA_SEC) == 0)
+ obj->data_in_arena = true;
+
if (sh->sh_type == SHT_SYMTAB) {
if (obj->efile.symbols) {
pr_warn("elf: multiple symbol tables in %s\n", obj->path);
@@ -4103,6 +4278,8 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
} else if (strcmp(name, ARENA_SEC) == 0) {
obj->efile.arena_data = data;
obj->efile.arena_data_shndx = idx;
+ } else if (strcmp(name, ARENA_DATA_SEC) == 0) {
+ /* the marker, see the first pass */
} else if (strcmp(name, JUMPTABLES_SEC) == 0) {
obj->jumptables_data = malloc(data->d_size);
if (!obj->jumptables_data)
@@ -4127,6 +4304,9 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
* have pointers to functions.
*/
if (!section_have_execinstr(obj, targ_sec_idx) &&
+ !(obj->data_in_arena &&
+ (!strcmp(name, ".rel" DATA_SEC) ||
+ str_has_pfx(name, ".rel" DATA_SEC "."))) &&
strcmp(name, ".rel" RODATA_SEC) &&
!str_has_pfx(name, ".rel" RODATA_SEC ".") &&
strcmp(name, ".rel" DATA_REL_RO_SEC) &&
@@ -4878,6 +5058,10 @@ static int bpf_program__record_reloc(struct bpf_program *prog,
reloc_desc->insn_idx = insn_idx;
reloc_desc->map_idx = map_idx;
reloc_desc->sym_off = sym->st_value;
+ if (map->in_arena) {
+ reloc_desc->map_idx = obj->arena_map_idx;
+ reloc_desc->sym_off += map->arena_off;
+ }
return 0;
}
@@ -5096,6 +5280,9 @@ int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate)
if (map_is_created(map))
return libbpf_err(-EBUSY);
+ if (map->in_arena)
+ return libbpf_err(-EOPNOTSUPP);
+
map->autocreate = autocreate;
return 0;
}
@@ -5376,6 +5563,45 @@ bpf_object__reuse_map(struct bpf_map *map)
return 0;
}
+static struct bpf_map *bpf_object__sec_map(struct bpf_object *obj, int sec_idx)
+{
+ int i;
+
+ for (i = 0; i < obj->nr_maps; i++)
+ if (bpf_map__is_internal(&obj->maps[i]) && obj->maps[i].sec_idx == sec_idx)
+ return &obj->maps[i];
+ return NULL;
+}
+
+static int bpf_object__mmap_arena(struct bpf_object *obj, struct bpf_map *map)
+{
+ int i, err;
+
+ map->mmaped = mmap((void *)(long)map->map_extra,
+ bpf_map_mmap_sz(map), PROT_READ | PROT_WRITE,
+ map->map_extra ? MAP_SHARED | MAP_FIXED : MAP_SHARED,
+ map->fd, 0);
+ if (map->mmaped == MAP_FAILED) {
+ err = -errno;
+ map->mmaped = NULL;
+ pr_warn("map '%s': failed to mmap arena: %s\n",
+ map->name, errstr(err));
+ return err;
+ }
+ if (obj->arena_data) {
+ memcpy(map->mmaped + obj->arena_data_off, obj->arena_data,
+ obj->arena_data_sz);
+ zfree(&obj->arena_data);
+ }
+ for (i = 0; i < obj->nr_maps; i++) {
+ struct bpf_map *m = &obj->maps[i];
+
+ if (m->in_arena)
+ m->mmaped = map->mmaped + obj->arena_data_off + m->arena_off;
+ }
+ return 0;
+}
+
static int
bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)
{
@@ -5736,6 +5962,10 @@ bpf_object__create_maps(struct bpf_object *obj)
continue;
}
+ /* see bpf_object__relocate_data_ptrs() */
+ if (map->def.type == BPF_MAP_TYPE_ARENA && obj->arena_mapped)
+ continue;
+
err = map_set_def_max_entries(map);
if (err)
goto err_out;
@@ -5773,22 +6003,9 @@ bpf_object__create_maps(struct bpf_object *obj)
if (err < 0)
goto err_out;
} else if (map->def.type == BPF_MAP_TYPE_ARENA) {
- map->mmaped = mmap((void *)(long)map->map_extra,
- bpf_map_mmap_sz(map), PROT_READ | PROT_WRITE,
- map->map_extra ? MAP_SHARED | MAP_FIXED : MAP_SHARED,
- map->fd, 0);
- if (map->mmaped == MAP_FAILED) {
- err = -errno;
- map->mmaped = NULL;
- pr_warn("map '%s': failed to mmap arena: %s\n",
- map->name, errstr(err));
+ err = bpf_object__mmap_arena(obj, map);
+ if (err)
return err;
- }
- if (obj->arena_data) {
- memcpy(map->mmaped + obj->arena_data_off, obj->arena_data,
- obj->arena_data_sz);
- zfree(&obj->arena_data);
- }
}
if (map->init_slots_sz && map->def.type != BPF_MAP_TYPE_PROG_ARRAY) {
err = init_map_in_map_slots(obj, map);
@@ -7803,6 +8020,81 @@ static int bpf_program_fixup_func_info(struct bpf_object *obj, struct bpf_progra
return err;
}
+/*
+ * Turn pointers to data that are stored in data into addresses of arena.
+ * The arena is created here, ahead of the other maps: the address has to be
+ * known before the maps that hold the pointers are created.
+ */
+static int bpf_object__relocate_data_ptrs(struct bpf_object *obj)
+{
+ struct bpf_map *arena, *map, *targ;
+ __u64 addr, val;
+ size_t i;
+ int err;
+
+ if (!obj->data_ptr_cnt)
+ return 0;
+
+ for (i = 0; i < obj->data_ptr_cnt; i++) {
+ struct data_ptr *p = &obj->data_ptrs[i];
+
+ if (p->targ_sec_idx < 0)
+ continue;
+ map = bpf_object__sec_map(obj, p->sec_idx);
+ targ = bpf_object__sec_map(obj, p->targ_sec_idx);
+ if (map && (map->in_arena || map->autocreate) &&
+ (!targ || (!targ->in_arena && !targ->arena_copy))) {
+ pr_warn("sec '%s': pointer to '%s' at offset %zu can't be resolved: sec '%s' is not in arena\n",
+ map->real_name, p->sym_name, p->sec_off,
+ targ ? targ->real_name : "<?>");
+ return -LIBBPF_ERRNO__RELOC;
+ }
+ }
+
+ if (obj->gen_loader) {
+ pr_warn("pointers to data in data are not supported by light skeleton\n");
+ return -ENOTSUP;
+ }
+
+ if (obj->arena_map_idx < 0)
+ return 0;
+
+ arena = &obj->maps[obj->arena_map_idx];
+ if (!arena->autocreate)
+ return 0;
+ /* libbpf doesn't mmap an arena that it didn't create, so the address is not known */
+ if (arena->reused || arena->pin_path) {
+ pr_warn("map '%s': pointers to data in data are not supported with pinned or reused arena\n",
+ arena->name);
+ return -ENOTSUP;
+ }
+
+ err = bpf_object__create_map(obj, arena, false);
+ err = err ?: bpf_object__mmap_arena(obj, arena);
+ if (err) {
+ pr_warn("map '%s': failed to create: %s\n", arena->name, errstr(err));
+ return err;
+ }
+ obj->arena_mapped = true;
+
+ for (i = 0; i < obj->data_ptr_cnt; i++) {
+ struct data_ptr *p = &obj->data_ptrs[i];
+
+ map = bpf_object__sec_map(obj, p->sec_idx);
+ if (!map || (!map->in_arena && !map->autocreate))
+ continue;
+
+ addr = (__u64)(unsigned long)arena->mmaped + obj->arena_data_off + p->targ_off;
+ if (p->targ_sec_idx >= 0)
+ addr += bpf_object__sec_map(obj, p->targ_sec_idx)->arena_off;
+
+ memcpy(&val, map->mmaped + p->sec_off, sizeof(val));
+ val += addr;
+ memcpy(map->mmaped + p->sec_off, &val, sizeof(val));
+ }
+ return 0;
+}
+
static int bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path)
{
struct bpf_program *prog;
@@ -7825,8 +8117,10 @@ static int bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_pat
size_t mmap_sz = bpf_map_mmap_sz(arena_map);
if (data_sz > mmap_sz) {
- pr_warn("map '%s': declared ARENA map size (%zu) is too small to hold global __arena variables of size %zu\n",
- arena_map->name, mmap_sz, obj->arena_data_sz);
+ pr_warn("map '%s': declared ARENA map size (%zu) is too small to hold global %s of size %zu\n",
+ arena_map->name, mmap_sz,
+ obj->data_in_arena ? "data" : "__arena variables",
+ obj->arena_data_sz);
return -E2BIG;
}
@@ -7835,6 +8129,10 @@ static int bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_pat
obj->arena_data_off = mmap_sz - data_sz;
}
+ err = bpf_object__relocate_data_ptrs(obj);
+ if (err)
+ return err;
+
/* Before relocating calls pre-process relocations and mark
* few ld_imm64 instructions that points to subprogs.
* Otherwise bpf_object__reloc_code() later would have to consider
@@ -8067,12 +8365,85 @@ static int bpf_object__collect_map_relos(struct bpf_object *obj,
return 0;
}
+/*
+ * A pointer to data that is stored in data. When data is in arena
+ * the program dereferences what it loads from data, so the pointer has
+ * to be the address of the target in arena.
+ */
+static int bpf_object__collect_data_ptr(struct bpf_object *obj, const char *relo_sec_name,
+ int relo_idx, size_t sec_idx, const Elf64_Rel *rel,
+ const Elf64_Sym *sym)
+{
+ Elf_Data *scn_data = obj->efile.secs[sec_idx].data;
+ const char *sym_name = elf_sym_str(obj, sym->st_name) ?: "<?>";
+ struct data_ptr *ptrs;
+
+ if (ELF64_ST_TYPE(sym->st_info) == STT_SECTION && sym->st_shndx < obj->efile.sec_cnt)
+ sym_name = elf_sec_name(obj, elf_sec_by_idx(obj, sym->st_shndx)) ?: "<?>";
+
+ if (ELF64_R_TYPE(rel->r_info) != R_BPF_64_ABS64 ||
+ sym->st_shndx >= obj->efile.sec_cnt ||
+ (sym->st_shndx != obj->efile.arena_data_shndx &&
+ !bpf_object__shndx_is_data(obj, sym->st_shndx)) ||
+ rel->r_offset >= scn_data->d_size ||
+ scn_data->d_size - rel->r_offset < sizeof(__u64)) {
+ pr_warn("sec '%s': relo #%d: can't resolve pointer to '%s' at offset %zu when data is in arena\n",
+ relo_sec_name, relo_idx, sym_name, (size_t)rel->r_offset);
+ return -LIBBPF_ERRNO__RELOC;
+ }
+
+ ptrs = libbpf_reallocarray(obj->data_ptrs, obj->data_ptr_cnt + 1, sizeof(*ptrs));
+ if (!ptrs)
+ return -ENOMEM;
+ obj->data_ptrs = ptrs;
+
+ ptrs[obj->data_ptr_cnt].sec_idx = sec_idx;
+ ptrs[obj->data_ptr_cnt].sec_off = rel->r_offset;
+ /* __arena variables are at the start of arena data */
+ ptrs[obj->data_ptr_cnt].targ_sec_idx =
+ sym->st_shndx == obj->efile.arena_data_shndx ? -1 : sym->st_shndx;
+ ptrs[obj->data_ptr_cnt].targ_off = sym->st_value;
+ ptrs[obj->data_ptr_cnt].sym_name = strdup(sym_name);
+ if (!ptrs[obj->data_ptr_cnt].sym_name)
+ return -ENOMEM;
+ obj->data_ptr_cnt++;
+
+ pr_debug("sec '%s': relo #%d: pointer at offset %zu to '%s'\n",
+ relo_sec_name, relo_idx, (size_t)rel->r_offset, sym_name);
+ return 0;
+}
+
+/* Collect pointers in a data section that went to arena */
+static int bpf_object__collect_data_relos(struct bpf_object *obj,
+ Elf64_Shdr *shdr, Elf_Data *data)
+{
+ int i, err, nrels = shdr->sh_size / shdr->sh_entsize;
+ const char *relo_sec_name;
+ Elf64_Sym *sym;
+ Elf64_Rel *rel;
+
+ relo_sec_name = elf_sec_str(obj, shdr->sh_name) ?: "<?>";
+ for (i = 0; i < nrels; i++) {
+ rel = elf_rel_by_idx(data, i);
+ sym = rel ? elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info)) : NULL;
+ if (!sym) {
+ pr_warn("sec '%s': failed to get relo #%d\n", relo_sec_name, i);
+ return -LIBBPF_ERRNO__FORMAT;
+ }
+ err = bpf_object__collect_data_ptr(obj, relo_sec_name, i, shdr->sh_info, rel, sym);
+ if (err)
+ return err;
+ }
+ return 0;
+}
+
/*
* Collect pointers to functions in a read-only data section. They are
* R_BPF_64_ABS64 relocations against .text section, where the offset of
* a static function in the section is stored in place. Relocations in data
* sections were ignored before pointers to functions were supported. Those
- * that are something else, e.g. pointers to data, still are.
+ * that are something else, e.g. pointers to data, still are, unless data
+ * is in arena.
*/
static int bpf_object__collect_rodata_relos(struct bpf_object *obj,
Elf64_Shdr *shdr, Elf_Data *data)
@@ -8108,6 +8479,15 @@ static int bpf_object__collect_rodata_relos(struct bpf_object *obj,
if (ELF64_R_TYPE(rel->r_info) != R_BPF_64_ABS64 ||
!sym_is_subprog(sym, obj->efile.text_shndx)) {
+ int err;
+
+ if (obj->data_in_arena) {
+ err = bpf_object__collect_data_ptr(obj, relo_sec_name, i,
+ sec_idx, rel, sym);
+ if (err)
+ return err;
+ continue;
+ }
pr_debug("sec '%s': relo #%d: not a pointer to a function, skipping...\n",
relo_sec_name, i);
continue;
@@ -8183,6 +8563,8 @@ static int bpf_object__collect_relos(struct bpf_object *obj)
if (obj->efile.secs[idx].sec_type == SEC_RODATA)
err = bpf_object__collect_rodata_relos(obj, shdr, data);
+ else if (obj->efile.secs[idx].sec_type == SEC_DATA)
+ err = bpf_object__collect_data_relos(obj, shdr, data);
else if (obj->efile.secs[idx].sec_type == SEC_ST_OPS)
err = bpf_object__collect_st_ops_relos(obj, shdr, data);
else if (idx == obj->efile.btf_maps_shndx)
@@ -8476,6 +8858,9 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog
}
load_attr.log_level = log_level;
load_attr.prog_flags = prog->prog_flags;
+ /* the program accesses its data in arena through plain numbers */
+ if (obj->data_in_arena)
+ load_attr.prog_flags |= BPF_F_ARENA_SCALAR;
load_attr.fd_array = obj->fd_array;
load_attr.token_fd = obj->token_fd;
@@ -8547,7 +8932,7 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog
for (i = 0; i < obj->nr_maps; i++) {
map = &prog->obj->maps[i];
- if (map->libbpf_type != LIBBPF_MAP_RODATA)
+ if (map->libbpf_type != LIBBPF_MAP_RODATA || map->in_arena)
continue;
if (bpf_prog_bind_map(ret, map->fd, NULL)) {
@@ -10111,7 +10496,7 @@ static void bpf_map__destroy(struct bpf_map *map)
zfree(&map->init_slots);
map->init_slots_sz = 0;
- if (map->mmaped && map->mmaped != map->obj->arena_data)
+ if (map->mmaped && map->mmaped != map->obj->arena_data && !map->in_arena)
munmap(map->mmaped, bpf_map_mmap_sz(map));
map->mmaped = NULL;
@@ -10196,6 +10581,9 @@ void bpf_object__close(struct bpf_object *obj)
close(obj->func_ptr_maps[i].fd);
zfree(&obj->func_ptr_maps);
zfree(&obj->func_ptrs);
+ for (i = 0; i < obj->data_ptr_cnt; i++)
+ zfree(&obj->data_ptrs[i].sym_name);
+ zfree(&obj->data_ptrs);
if (obj->btf_module_allowlist) {
for (i = 0; i < obj->btf_module_allowlist_cnt; i++)
@@ -10556,6 +10944,8 @@ int bpf_program__clone(struct bpf_program *prog, const struct bpf_prog_load_opts
attr.token_fd = OPTS_GET(opts, token_fd, 0) ?: obj->token_fd;
if (attr.token_fd)
attr.prog_flags |= BPF_F_TOKEN_FD;
+ if (obj->data_in_arena)
+ attr.prog_flags |= BPF_F_ARENA_SCALAR;
prog_btf_fd = OPTS_GET(opts, prog_btf_fd, 0);
if (!prog_btf_fd && obj->btf)
@@ -11562,6 +11952,9 @@ int bpf_map__set_value_size(struct bpf_map *map, __u32 size)
if (map_is_created(map))
return libbpf_err(-EBUSY);
+ if (map->in_arena)
+ return libbpf_err(-EOPNOTSUPP);
+
if (map->mmaped) {
size_t mmap_old_sz, mmap_new_sz;
int err;
diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
index afa589a27b151..a22be7efd1fa2 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -422,6 +422,16 @@ $(LIBARENA_ASAN_SKEL): $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ) $(LIBARENA_BPF_DEPS)
+$(MAKE) -C libarena libarena_asan.skel.h $(LIBARENA_MAKE_ARGS)
endif
+# #![no_std] looks for compiler_builtins too. Nothing of it is used.
+ifneq ($(RUST_CORE),)
+$(RUST_CORE): $(RUST_CORE_SRC)
+ $(call msg,RUSTC,,$@)
+ $(Q)mkdir -p $(@D)
+ +$(Q)$(RUSTC_BPF) -A warnings --edition 2024 --crate-name core --out-dir $(@D) $<
+ +$(Q)echo '#![feature(compiler_builtins)] #![compiler_builtins] #![no_std]' | \
+ $(RUSTC_BPF) -A warnings --crate-name compiler_builtins --out-dir $(@D) -
+endif
+
# Generated test list headers
define gen_tests_hdr
@@ -457,7 +467,7 @@ RUNNER_PREREQS := $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ) $(BPFTOOL) \
$(VERIFY_SIG_HDR) $(PRIVATE_KEY) $(VERIFICATION_CERT) \
$(LIBARENA_SKEL) $(LIBARENA_ASAN_SKEL) \
prog_tests/tests.h map_tests/tests.h \
- $(RUNNER_OBJS)
+ $(RUNNER_OBJS) $(RUST_CORE)
# Runtime fixtures for each test_progs flavor.
RUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \
diff --git a/tools/testing/selftests/bpf/Makefile.buildvars b/tools/testing/selftests/bpf/Makefile.buildvars
index d2a0c0031b87d..ff3476bc40a49 100644
--- a/tools/testing/selftests/bpf/Makefile.buildvars
+++ b/tools/testing/selftests/bpf/Makefile.buildvars
@@ -109,6 +109,31 @@ HOST_INCLUDE_DIR := $(INCLUDE_DIR)
endif
RESOLVE_BTFIDS := $(HOST_BUILD_DIR)/resolve_btfids/resolve_btfids
+# Programs in Rust are built by upstream rustc. It has no prebuilt core for
+# the bpf target, so it has to come with the source of core:
+# rustup component add rust-src
+# core is built as edition 2024, which it is since rustc 1.87.
+# rustc emits LLVM bitcode and clang makes the object of it, so clang has to be
+# 23 or newer and not older than LLVM of rustc.
+# Otherwise RUST_CORE is empty and the tests are skipped.
+RUSTC ?= rustc
+RUST_CORE_SRC := $(wildcard $(shell $(RUSTC) --print sysroot 2>/dev/null)$\
+ /lib/rustlib/src/rust/library/core/src/lib.rs)
+ifneq ($(RUST_CORE_SRC),)
+ifeq ($(shell { clang=$$(echo __clang_major__ | $(CLANG) -E -P -x c -) && \
+ llvm=$$($(srctree)/scripts/rustc-llvm-version.sh $(RUSTC)) && \
+ [ $$($(srctree)/scripts/rustc-version.sh $(RUSTC)) -ge 108700 ] && \
+ [ $$clang -ge 23 ] && [ $$clang -ge $$((llvm / 10000)) ]; } \
+ 2>/dev/null && echo y),y)
+RUST_CORE := $(BUILD_DIR)/rust/libcore.rlib
+endif
+endif
+# RUSTC_BOOTSTRAP=1 is to build core with a stable rustc, like the kernel does.
+# panic=abort is a stop gap until panic=unwind is supported.
+RUSTC_BPF = RUSTC_BOOTSTRAP=1 $(RUSTC) -O -C panic=abort --crate-type rlib \
+ --target $(if $(IS_LITTLE_ENDIAN),bpfel,bpfeb)-unknown-none \
+ -L $(dir $(RUST_CORE))
+
DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool
ifneq ($(CROSS_COMPILE),)
CROSS_BPFTOOL := $(SCRATCH_DIR)/sbin/bpftool
diff --git a/tools/testing/selftests/bpf/Makefile.skel b/tools/testing/selftests/bpf/Makefile.skel
index 2e22bb901bf3b..06e297a17156c 100644
--- a/tools/testing/selftests/bpf/Makefile.skel
+++ b/tools/testing/selftests/bpf/Makefile.skel
@@ -20,7 +20,8 @@ ifneq ($(BPF_CC),)
BPF_SRCS := $(notdir $(wildcard progs/*.c))
BPF_OBJS := $(patsubst %.c,$(RDIR)/%.bpf.o,$(BPF_SRCS))
-SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c
+SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c \
+ data_in_arena_extern.c data_in_arena_nomap.c
LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \
linked_vars.skel.h linked_maps.skel.h linked_arena.skel.h \
@@ -137,4 +138,19 @@ $(LINKED_SKELS_H): $(RDIR)/%.skel.h: $$(addprefix $(RDIR)/,$$($$*.skel.h-deps))
$(BPFTOOL) $(GEN_SKEL) | $(RDIR)
$(Q)$(cmd_bpf_link_skel)
+# Programs in Rust, see Makefile.buildvars. No skeletons: the objects may be absent.
+ifneq ($(RUST_CORE),)
+ifeq ($(BPF_CC),$(CLANG))
+RUST_OBJS := $(patsubst progs/%.rs,$(RDIR)/%.bpf.o,$(wildcard progs/*.rs))
+BPF_OBJS += $(RUST_OBJS)
+
+$(RUST_OBJS): $(RDIR)/%.bpf.o: progs/%.rs $(RUST_CORE) | $(RDIR)
+ $(call msg,RUSTC,$(BINARY),$@)
+ +$(Q)$(RUSTC_BPF) --edition 2021 -C debuginfo=2 --emit=llvm-bc \
+ $(patsubst -mcpu=%,-C target-cpu=%,$(filter -mcpu=%,$(BPF_CC_FLAGS))) \
+ -o $(dir $(RUST_CORE))$(BINARY)-$*.bc $< && \
+ $(BPF_CC) $(BPF_CC_FLAGS) -c $(dir $(RUST_CORE))$(BINARY)-$*.bc -o $@ $(call skip_on_fail,BPF)
+endif
+endif
+
endif # BPF_CC
diff --git a/tools/testing/selftests/bpf/prog_tests/arena_scalar_blinded.c b/tools/testing/selftests/bpf/prog_tests/arena_scalar_blinded.c
new file mode 100644
index 0000000000000..2ac2e9a652fec
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/arena_scalar_blinded.c
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include "sysctl_helpers.h"
+#include "verifier_arena_scalar.skel.h"
+
+/* The same tests with constants of the programs blinded */
+void serial_test_arena_scalar_blinded(void)
+{
+ const char *harden = "/proc/sys/net/core/bpf_jit_harden";
+ char old[16] = {};
+
+ if (!is_jit_enabled()) {
+ test__skip();
+ return;
+ }
+ if (sysctl_set_or_fail(harden, old, "2"))
+ return;
+ RUN_TESTS(verifier_arena_scalar);
+ sysctl_set_or_fail(harden, NULL, old);
+}
diff --git a/tools/testing/selftests/bpf/prog_tests/btf.c b/tools/testing/selftests/bpf/prog_tests/btf.c
index df6ad38d287da..24ab62b2834ae 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf.c
@@ -424,7 +424,7 @@ static struct btf_raw_test raw_tests[] = {
.err_str = "Invalid type",
},
{
- .descr = "global data test #8, invalid var size",
+ .descr = "global data test #8, var is smaller than its type",
.raw_types = {
/* int */
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
@@ -457,8 +457,6 @@ static struct btf_raw_test raw_tests[] = {
.key_type_id = 0,
.value_type_id = 7,
.max_entries = 1,
- .btf_load_err = true,
- .err_str = "Invalid size",
},
{
.descr = "global data test #9, invalid var size",
@@ -498,7 +496,7 @@ static struct btf_raw_test raw_tests[] = {
.err_str = "Invalid size",
},
{
- .descr = "global data test #10, invalid var size",
+ .descr = "global data test #10, section is smaller than map value",
.raw_types = {
/* int */
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
@@ -531,8 +529,7 @@ static struct btf_raw_test raw_tests[] = {
.key_type_id = 0,
.value_type_id = 7,
.max_entries = 1,
- .btf_load_err = true,
- .err_str = "Invalid size",
+ .map_create_err = true,
},
{
.descr = "global data test #11, multiple section members",
@@ -1987,14 +1984,14 @@ static struct btf_raw_test raw_tests[] = {
},
{
- .descr = "typedef (invalid name, invalid identifier)",
+ .descr = "typedef (invalid name, not printable)",
.raw_types = {
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
BTF_TYPEDEF_ENC(NAME_TBD, 1), /* [2] */
BTF_END_RAW,
},
- .str_sec = "\0__!int",
- .str_sec_size = sizeof("\0__!int"),
+ .str_sec = "\0__\7int",
+ .str_sec_size = sizeof("\0__\7int"),
.map_type = BPF_MAP_TYPE_ARRAY,
.map_name = "typedef_check_btf",
.key_size = sizeof(int),
@@ -2112,15 +2109,15 @@ static struct btf_raw_test raw_tests[] = {
},
{
- .descr = "fwd type (invalid name, invalid identifier)",
+ .descr = "fwd type (invalid name, not printable)",
.raw_types = {
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
BTF_TYPE_ENC(NAME_TBD,
BTF_INFO_ENC(BTF_KIND_FWD, 0, 0), 0), /* [2] */
BTF_END_RAW,
},
- .str_sec = "\0__!skb",
- .str_sec_size = sizeof("\0__!skb"),
+ .str_sec = "\0__\7skb",
+ .str_sec_size = sizeof("\0__\7skb"),
.map_type = BPF_MAP_TYPE_ARRAY,
.map_name = "fwd_type_check_btf",
.key_size = sizeof(int),
@@ -2175,7 +2172,7 @@ static struct btf_raw_test raw_tests[] = {
},
{
- .descr = "struct type (invalid name, invalid identifier)",
+ .descr = "struct type (invalid name, not printable)",
.raw_types = {
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
BTF_TYPE_ENC(NAME_TBD,
@@ -2183,8 +2180,8 @@ static struct btf_raw_test raw_tests[] = {
BTF_MEMBER_ENC(NAME_TBD, 1, 0),
BTF_END_RAW,
},
- .str_sec = "\0A!\0B",
- .str_sec_size = sizeof("\0A!\0B"),
+ .str_sec = "\0A\7\0B",
+ .str_sec_size = sizeof("\0A\7\0B"),
.map_type = BPF_MAP_TYPE_ARRAY,
.map_name = "struct_type_check_btf",
.key_size = sizeof(int),
@@ -2217,7 +2214,7 @@ static struct btf_raw_test raw_tests[] = {
},
{
- .descr = "struct member (invalid name, invalid identifier)",
+ .descr = "struct member (invalid name, not printable)",
.raw_types = {
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
BTF_TYPE_ENC(NAME_TBD,
@@ -2225,8 +2222,8 @@ static struct btf_raw_test raw_tests[] = {
BTF_MEMBER_ENC(NAME_TBD, 1, 0),
BTF_END_RAW,
},
- .str_sec = "\0A\0B*",
- .str_sec_size = sizeof("\0A\0B*"),
+ .str_sec = "\0A\0B\7",
+ .str_sec_size = sizeof("\0A\0B\7"),
.map_type = BPF_MAP_TYPE_ARRAY,
.map_name = "struct_type_check_btf",
.key_size = sizeof(int),
@@ -2260,7 +2257,7 @@ static struct btf_raw_test raw_tests[] = {
},
{
- .descr = "enum type (invalid name, invalid identifier)",
+ .descr = "enum type (invalid name, not printable)",
.raw_types = {
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
BTF_TYPE_ENC(NAME_TBD,
@@ -2269,8 +2266,8 @@ static struct btf_raw_test raw_tests[] = {
BTF_ENUM_ENC(NAME_TBD, 0),
BTF_END_RAW,
},
- .str_sec = "\0A!\0B",
- .str_sec_size = sizeof("\0A!\0B"),
+ .str_sec = "\0A\7\0B",
+ .str_sec_size = sizeof("\0A\7\0B"),
.map_type = BPF_MAP_TYPE_ARRAY,
.map_name = "enum_type_check_btf",
.key_size = sizeof(int),
@@ -2306,7 +2303,7 @@ static struct btf_raw_test raw_tests[] = {
},
{
- .descr = "enum member (invalid name, invalid identifier)",
+ .descr = "enum member (invalid name, not printable)",
.raw_types = {
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
BTF_TYPE_ENC(0,
@@ -2315,8 +2312,8 @@ static struct btf_raw_test raw_tests[] = {
BTF_ENUM_ENC(NAME_TBD, 0),
BTF_END_RAW,
},
- .str_sec = "\0A!",
- .str_sec_size = sizeof("\0A!"),
+ .str_sec = "\0A\7",
+ .str_sec_size = sizeof("\0A\7"),
.map_type = BPF_MAP_TYPE_ARRAY,
.map_name = "enum_type_check_btf",
.key_size = sizeof(int),
@@ -2625,14 +2622,14 @@ static struct btf_raw_test raw_tests[] = {
.raw_types = {
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [2] */
- /* void (*)(int a, unsigned int !!!) */
+ /* void (*)(int a, unsigned int \7) */
BTF_FUNC_PROTO_ENC(0, 2), /* [3] */
BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
BTF_END_RAW,
},
- .str_sec = "\0a\0!!!",
- .str_sec_size = sizeof("\0a\0!!!"),
+ .str_sec = "\0a\0\7",
+ .str_sec_size = sizeof("\0a\0\7"),
.map_type = BPF_MAP_TYPE_ARRAY,
.map_name = "func_proto_type_check_btf",
.key_size = sizeof(int),
@@ -2775,12 +2772,12 @@ static struct btf_raw_test raw_tests[] = {
BTF_FUNC_PROTO_ENC(0, 2), /* [3] */
BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
- /* void !!!(int a, unsigned int b) */
+ /* void \7(int a, unsigned int b) */
BTF_FUNC_ENC(NAME_TBD, 3), /* [4] */
BTF_END_RAW,
},
- .str_sec = "\0a\0b\0!!!",
- .str_sec_size = sizeof("\0a\0b\0!!!"),
+ .str_sec = "\0a\0b\0\7",
+ .str_sec_size = sizeof("\0a\0b\0\7"),
.map_type = BPF_MAP_TYPE_ARRAY,
.map_name = "func_type_check_btf",
.key_size = sizeof(int),
@@ -2793,7 +2790,7 @@ static struct btf_raw_test raw_tests[] = {
},
{
- .descr = "func (Some arg has no name)",
+ .descr = "func (Some arg of global func has no name)",
.raw_types = {
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [2] */
@@ -2802,7 +2799,8 @@ static struct btf_raw_test raw_tests[] = {
BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
BTF_FUNC_PROTO_ARG_ENC(0, 2),
/* void func(int a, unsigned int) */
- BTF_FUNC_ENC(NAME_TBD, 3), /* [4] */
+ BTF_TYPE_ENC(NAME_TBD, /* [4] */
+ BTF_INFO_ENC(BTF_KIND_FUNC, 0, BTF_FUNC_GLOBAL), 3),
BTF_END_RAW,
},
.str_sec = "\0a\0func",
@@ -2818,6 +2816,57 @@ static struct btf_raw_test raw_tests[] = {
.err_str = "Invalid arg#2",
},
+{
+ .descr = "func (Some arg of static func has no name)",
+ .raw_types = {
+ /* int */
+ BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
+ /* unsigned int */
+ BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [2] */
+ /* void (*)(int a, unsigned int) */
+ BTF_FUNC_PROTO_ENC(0, 2), /* [3] */
+ BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
+ BTF_FUNC_PROTO_ARG_ENC(0, 2),
+ /* static void func(int a, unsigned int) */
+ BTF_FUNC_ENC(NAME_TBD, 3), /* [4] */
+ BTF_END_RAW,
+ },
+ .str_sec = "\0a\0func",
+ .str_sec_size = sizeof("\0a\0func"),
+ .map_type = BPF_MAP_TYPE_ARRAY,
+ .map_name = "func_type_check_btf",
+ .key_size = sizeof(int),
+ .value_size = sizeof(int),
+ .key_type_id = 1,
+ .value_type_id = 1,
+ .max_entries = 4,
+},
+
+{
+ .descr = "func (vararg of global func has no name)",
+ .raw_types = {
+ /* int */
+ BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
+ /* void (*)(int a, ...) */
+ BTF_FUNC_PROTO_ENC(0, 2), /* [2] */
+ BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
+ BTF_FUNC_PROTO_ARG_ENC(0, 0),
+ /* void func(int a, ...) */
+ BTF_TYPE_ENC(NAME_TBD, /* [3] */
+ BTF_INFO_ENC(BTF_KIND_FUNC, 0, BTF_FUNC_GLOBAL), 2),
+ BTF_END_RAW,
+ },
+ .str_sec = "\0a\0func",
+ .str_sec_size = sizeof("\0a\0func"),
+ .map_type = BPF_MAP_TYPE_ARRAY,
+ .map_name = "func_type_check_btf",
+ .key_size = sizeof(int),
+ .value_size = sizeof(int),
+ .key_type_id = 1,
+ .value_type_id = 1,
+ .max_entries = 4,
+},
+
{
.descr = "func (Non zero vlen)",
.raw_types = {
@@ -3585,15 +3634,28 @@ static struct btf_raw_test raw_tests[] = {
.btf_load_err = true,
},
{
- .descr = "type name '?foo' is not ok",
+ .descr = "type name '?foo' is ok",
.raw_types = {
/* union ?foo; */
BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_FWD, 1, 0), 0), /* [1] */
BTF_END_RAW,
},
BTF_STR_SEC("\0?foo"),
- .err_str = "Invalid name",
- .btf_load_err = true,
+},
+{
+ .descr = "names of Rust types and functions are ok",
+ .raw_types = {
+ BTF_TYPE_INT_ENC(NAME_NTH(1), 0, 0, 32, 4), /* [1] */
+ BTF_STRUCT_ENC(NAME_NTH(2), 1, 4), /* [2] */
+ BTF_MEMBER_ENC(NAME_NTH(3), 1, 0),
+ BTF_FWD_ENC(NAME_NTH(4), 0), /* [3] */
+ BTF_TYPEDEF_ENC(NAME_NTH(5), 2), /* [4] */
+ BTF_FUNC_PROTO_ENC(0, 1), /* [5] */
+ BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(6), 1),
+ BTF_FUNC_ENC(NAME_NTH(7), 5), /* [6] */
+ BTF_END_RAW,
+ },
+ BTF_STR_SEC("\0u32\0Option<&str>\0__0\0*const str\0{impl#9}<[u8; 4]>\0self\0fmt<str>"),
},
{
diff --git a/tools/testing/selftests/bpf/prog_tests/btf_rust.c b/tools/testing/selftests/bpf/prog_tests/btf_rust.c
new file mode 100644
index 0000000000000..daf777cadfda3
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/btf_rust.c
@@ -0,0 +1,142 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include <bpf/btf.h>
+#include "bpftool_helpers.h"
+
+#define FUNC_NAME "write_fmt<scx_simple::BpfStream>"
+#define KSYM_NAME "write_fmt_scx_simple__BpfStream_"
+
+/* The name of a function of Rust is a part of the name of the program in kallsyms */
+static void test_func_name(void)
+{
+ struct bpf_insn insns[] = {
+ BPF_MOV64_IMM(BPF_REG_0, 0),
+ BPF_EXIT_INSN(),
+ };
+ LIBBPF_OPTS(bpf_prog_load_opts, opts);
+ int int_id, proto_id, prog_fd = -1, i;
+ struct bpf_func_info func_info = {};
+ struct bpf_prog_info info = {};
+ unsigned long long addr;
+ __u32 len = sizeof(info);
+ char sym[128], *p = sym;
+ struct btf *btf;
+
+ btf = btf__new_empty();
+ if (!ASSERT_OK_PTR(btf, "btf"))
+ return;
+ int_id = btf__add_int(btf, "i32", 4, BTF_INT_SIGNED);
+ ASSERT_GT(int_id, 0, "int");
+ proto_id = btf__add_func_proto(btf, int_id);
+ ASSERT_GT(proto_id, 0, "proto");
+ ASSERT_OK(btf__add_func_param(btf, "ctx", int_id), "param");
+ func_info.type_id = btf__add_func(btf, FUNC_NAME, BTF_FUNC_STATIC, proto_id);
+ ASSERT_GT(func_info.type_id, 0, "func");
+ if (!ASSERT_OK(btf__load_into_kernel(btf), "btf load"))
+ goto out;
+
+ opts.prog_btf_fd = btf__fd(btf);
+ opts.func_info = &func_info;
+ opts.func_info_cnt = 1;
+ opts.func_info_rec_size = sizeof(func_info);
+ prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", insns,
+ ARRAY_SIZE(insns), &opts);
+ if (!ASSERT_GE(prog_fd, 0, "prog load"))
+ goto out;
+ if (!ASSERT_OK(bpf_prog_get_info_by_fd(prog_fd, &info, &len), "prog info"))
+ goto out;
+ if (!info.jited_prog_len) {
+ test__skip();
+ goto out;
+ }
+
+ p += sprintf(p, "bpf_prog_");
+ for (i = 0; i < BPF_TAG_SIZE; i++)
+ p += sprintf(p, "%02x", info.tag[i]);
+ sprintf(p, "_%s", KSYM_NAME);
+ ASSERT_OK(kallsyms_find(sym, &addr), sym);
+out:
+ if (prog_fd >= 0)
+ close(prog_fd);
+ btf__free(btf);
+}
+
+#define PIN_PATH "/sys/fs/bpf/btf_rust_piece"
+
+/*
+ * A piece of a static that LLVM split has the type of the whole static.
+ * It's the last variable in the section, so its type ends past the map value.
+ */
+static void test_piece(void)
+{
+ LIBBPF_OPTS(bpf_map_create_opts, opts);
+ int int_id, struct_id, var_id, piece_id, sec_id, map_fd = -1, key = 0;
+ __u32 value[2] = { 0x11111111, 0x22222222 };
+ char line[256] = {}, out[1024] = {};
+ struct btf *btf;
+ FILE *f = NULL;
+
+ btf = btf__new_empty();
+ if (!ASSERT_OK_PTR(btf, "btf"))
+ return;
+ int_id = btf__add_int(btf, "u32", 4, 0);
+ ASSERT_GT(int_id, 0, "int");
+ struct_id = btf__add_struct(btf, "Whole", 8);
+ ASSERT_GT(struct_id, 0, "struct");
+ ASSERT_OK(btf__add_field(btf, "a", int_id, 0, 0), "field");
+ ASSERT_OK(btf__add_field(btf, "b", int_id, 32, 0), "field");
+ var_id = btf__add_var(btf, "CNT", BTF_VAR_STATIC, int_id);
+ ASSERT_GT(var_id, 0, "var");
+ piece_id = btf__add_var(btf, "WHOLE.1", BTF_VAR_STATIC, struct_id);
+ ASSERT_GT(piece_id, 0, "piece");
+ sec_id = btf__add_datasec(btf, ".bss", sizeof(value));
+ ASSERT_GT(sec_id, 0, "datasec");
+ ASSERT_OK(btf__add_datasec_var_info(btf, var_id, 0, 4), "var info");
+ ASSERT_OK(btf__add_datasec_var_info(btf, piece_id, 4, 4), "piece info");
+ if (!ASSERT_OK(btf__load_into_kernel(btf), "btf load"))
+ goto out;
+
+ opts.btf_fd = btf__fd(btf);
+ opts.btf_value_type_id = sec_id;
+ map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, ".bss", sizeof(key), sizeof(value), 1, &opts);
+ if (!ASSERT_GE(map_fd, 0, "map create"))
+ goto out;
+ if (!ASSERT_OK(bpf_map_update_elem(map_fd, &key, value, 0), "map update"))
+ goto out;
+
+ /* the variable is printed, the piece is not */
+ unlink(PIN_PATH);
+ if (!ASSERT_OK(bpf_obj_pin(map_fd, PIN_PATH), "pin"))
+ goto out;
+ f = fopen(PIN_PATH, "r");
+ if (!ASSERT_OK_PTR(f, "open"))
+ goto out;
+ while (fgets(line, sizeof(line), f) && line[0] == '#')
+ ;
+ ASSERT_HAS_SUBSTR(line, "286331153", "var");
+ ASSERT_NULL(strstr(line, "572662306"), "piece");
+
+ /* the same for bpftool */
+ if (!ASSERT_OK(get_bpftool_command_output("map dump pinned " PIN_PATH, out, sizeof(out)),
+ "bpftool"))
+ goto out;
+ ASSERT_HAS_SUBSTR(out, "CNT", "var");
+ ASSERT_NULL(strstr(out, "WHOLE.1"), "piece");
+out:
+ if (f)
+ fclose(f);
+ unlink(PIN_PATH);
+ if (map_fd >= 0)
+ close(map_fd);
+ btf__free(btf);
+}
+
+/* Serial: programs are not in kallsyms while another test sets bpf_jit_harden */
+void serial_test_btf_rust(void)
+{
+ if (test__start_subtest("func_name"))
+ test_func_name();
+ if (test__start_subtest("piece"))
+ test_piece();
+}
diff --git a/tools/testing/selftests/bpf/prog_tests/data_in_arena.c b/tools/testing/selftests/bpf/prog_tests/data_in_arena.c
new file mode 100644
index 0000000000000..cb1023507c017
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/data_in_arena.c
@@ -0,0 +1,216 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include "data_in_arena.skel.h"
+#include "data_in_arena_decl.skel.h"
+#include "data_in_arena_fail.skel.h"
+
+static int run_prog(struct bpf_program *prog)
+{
+ LIBBPF_OPTS(bpf_test_run_opts, topts);
+
+ if (!ASSERT_OK(bpf_prog_test_run_opts(bpf_program__fd(prog), &topts), "test_run"))
+ return -1;
+ return topts.retval;
+}
+
+static void run(struct data_in_arena *skel, int counter)
+{
+ int i;
+
+ ASSERT_EQ(run_prog(skel->progs.use_data), counter + 7 + 1 + 2, "retval");
+ ASSERT_EQ(skel->bss->sum, counter + 7 + 1 + 2, "sum");
+ ASSERT_EQ(skel->data->counter, counter + 1, "counter");
+ ASSERT_EQ(skel->data->pair[1], 5, "pair[1]");
+ for (i = 0; i < 4; i++)
+ ASSERT_EQ(skel->bss->table[i], 10 * (i + 1) + i, "table");
+}
+
+static void test_in_arena(void)
+{
+ struct bpf_map_info info = {};
+ struct data_in_arena *skel;
+ __u32 len = sizeof(info);
+ struct bpf_map *arena;
+ size_t sz;
+
+ skel = data_in_arena__open();
+ if (!ASSERT_OK_PTR(skel, "open"))
+ return;
+
+ arena = bpf_object__find_map_by_name(skel->obj, "arena");
+ if (!ASSERT_OK_PTR(arena, "arena"))
+ goto out;
+ ASSERT_EQ(bpf_map__type(arena), BPF_MAP_TYPE_ARENA, "arena type");
+ ASSERT_EQ(bpf_map__max_entries(arena), 1, "arena pages");
+ ASSERT_OK(bpf_map__set_max_entries(arena, 8), "arena resize");
+ ASSERT_FALSE(bpf_map__autocreate(skel->maps.data), "data autocreate");
+ ASSERT_FALSE(bpf_map__autocreate(skel->maps.bss), "bss autocreate");
+ ASSERT_FALSE(bpf_map__autocreate(skel->maps.rodata), "rodata autocreate");
+ ASSERT_EQ(bpf_map__set_autocreate(skel->maps.data, true), -EOPNOTSUPP, "set_autocreate");
+ ASSERT_EQ(bpf_map__set_value_size(skel->maps.bss, 4096), -EOPNOTSUPP, "set_value_size");
+ ASSERT_EQ(bpf_map__initial_value(skel->maps.data, &sz), skel->data, "initial_value");
+ ASSERT_EQ(sz, sizeof(*skel->data), "initial_value size");
+
+ /* initial values are set the usual way */
+ skel->data->counter = 100;
+
+ if (!ASSERT_OK(data_in_arena__load(skel), "load"))
+ goto out;
+ /* there are no maps behind the sections */
+ ASSERT_ERR(bpf_map_get_info_by_fd(bpf_map__fd(skel->maps.data), &info, &len), "data map");
+ ASSERT_ERR(bpf_map_get_info_by_fd(bpf_map__fd(skel->maps.bss), &info, &len), "bss map");
+ ASSERT_ERR(bpf_map_get_info_by_fd(bpf_map__fd(skel->maps.rodata), &info, &len),
+ "rodata map");
+ run(skel, 100);
+
+ /* pointers to data next to pointers to functions */
+ ASSERT_EQ(run_prog(skel->progs.use_ops), 42 + 1 + 'e', "use_ops");
+ ASSERT_EQ(skel->data->counter, 102, "counter");
+
+ /* alignment of sections and pointers to data in data */
+ ASSERT_EQ((unsigned long)&skel->bss->aligned64 % 64, 0, "alignment");
+ ASSERT_EQ(run_prog(skel->progs.use_ptrs), 0, "use_ptrs");
+ ASSERT_EQ(skel->data->x, 43, "x");
+ ASSERT_EQ(skel->bss->aligned64.v[7], 7, "aligned64");
+ ASSERT_EQ(skel->data->px, &skel->data->x, "px");
+
+ /* format strings of bpf_printk() and BPF_SNPRINTF() */
+ ASSERT_EQ(run_prog(skel->progs.use_printk), sizeof("43-7"), "use_printk");
+ ASSERT_STREQ(skel->bss->out, "43-7", "out");
+out:
+ data_in_arena__destroy(skel);
+}
+
+/* The object has an arena map and __arena variables */
+static void test_declared_arena(void)
+{
+ struct data_in_arena_decl *skel;
+ struct bpf_map *map;
+ int arenas = 0;
+
+ skel = data_in_arena_decl__open();
+ if (!ASSERT_OK_PTR(skel, "open"))
+ return;
+ bpf_object__for_each_map(map, skel->obj)
+ arenas += bpf_map__type(map) == BPF_MAP_TYPE_ARENA;
+ ASSERT_EQ(arenas, 1, "no second arena");
+ skel->data->counter = 6;
+ if (!ASSERT_OK(data_in_arena_decl__load(skel), "load"))
+ goto out;
+ ASSERT_EQ(run_prog(skel->progs.use_data), 6 + 7 + 11, "retval");
+ ASSERT_EQ(run_prog(skel->progs.use_data), 7 + 7 + 12, "retval");
+ ASSERT_EQ(skel->bss->sum, 7 + 7 + 12, "sum");
+ ASSERT_EQ(skel->data->counter, 8, "counter");
+out:
+ data_in_arena_decl__destroy(skel);
+}
+
+/* A pointer in data that can't be made an address of arena fails the load */
+static void test_ptr_to_map(void)
+{
+ struct data_in_arena_fail *skel;
+
+ skel = data_in_arena_fail__open();
+ if (!ASSERT_OK_PTR(skel, "open"))
+ return;
+ ASSERT_ERR(data_in_arena_fail__load(skel), "load");
+ data_in_arena_fail__destroy(skel);
+}
+
+/* So does a pointer to a variable of the kernel. There is no skeleton: the open fails. */
+static void test_ptr_to_extern(void)
+{
+ struct bpf_object *obj;
+
+ obj = bpf_object__open_file("./data_in_arena_extern.bpf.o", NULL);
+ if (!ASSERT_ERR_PTR(obj, "open"))
+ bpf_object__close(obj);
+}
+
+/* __arena variables and no arena map. There is no skeleton: the open fails. */
+static void test_arena_var_no_map(void)
+{
+ struct bpf_object *obj;
+
+ obj = bpf_object__open_file("./data_in_arena_nomap.bpf.o", NULL);
+ if (!ASSERT_ERR_PTR(obj, "open"))
+ bpf_object__close(obj);
+}
+
+/* The address of an arena that libbpf doesn't create is not known. No pointers in data then. */
+static void test_not_my_arena(bool pin)
+{
+ LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_MMAPABLE);
+ struct data_in_arena *skel;
+ struct bpf_map *arena;
+ int fd = -1;
+
+ skel = data_in_arena__open();
+ if (!ASSERT_OK_PTR(skel, "open"))
+ return;
+ arena = bpf_object__find_map_by_name(skel->obj, "arena");
+ if (!ASSERT_OK_PTR(arena, "arena"))
+ goto out;
+ if (pin) {
+ ASSERT_OK(bpf_map__set_pin_path(arena, "/sys/fs/bpf/data_in_arena"), "pin_path");
+ } else {
+ fd = bpf_map_create(BPF_MAP_TYPE_ARENA, "arena", 0, 0, 1, &opts);
+ if (!ASSERT_GE(fd, 0, "map_create"))
+ goto out;
+ ASSERT_OK(bpf_map__reuse_fd(arena, fd), "reuse_fd");
+ }
+ ASSERT_EQ(data_in_arena__load(skel), -ENOTSUP, "load");
+out:
+ if (fd >= 0)
+ close(fd);
+ data_in_arena__destroy(skel);
+}
+
+/* The object is there if rustc and clang can build it, see Makefile.buildvars */
+static void test_rust(void)
+{
+ const char *file = "./data_in_arena_rust.bpf.o";
+ struct bpf_object *obj;
+
+ if (access(file, R_OK)) {
+ test__skip();
+ return;
+ }
+ obj = bpf_object__open_file(file, NULL);
+ if (!ASSERT_OK_PTR(obj, "open"))
+ return;
+ if (!ASSERT_OK(bpf_object__load(obj), "load"))
+ goto out;
+ /* libbpf goes on without BTF when the kernel doesn't take it */
+ ASSERT_GE(bpf_object__btf_fd(obj), 0, "btf_fd");
+ ASSERT_EQ(run_prog(bpf_object__find_program_by_name(obj, "list_in_data")),
+ 100 + 20 + 3, "retval");
+out:
+ bpf_object__close(obj);
+}
+
+void test_data_in_arena(void)
+{
+#if !defined(__x86_64__) && !defined(__aarch64__)
+ /* other JITs don't take BPF_F_ARENA_SCALAR */
+ test__skip();
+ return;
+#endif
+ if (test__start_subtest("arena"))
+ test_in_arena();
+ if (test__start_subtest("declared_arena"))
+ test_declared_arena();
+ if (test__start_subtest("ptr_to_map"))
+ test_ptr_to_map();
+ if (test__start_subtest("ptr_to_extern"))
+ test_ptr_to_extern();
+ if (test__start_subtest("arena_var_no_map"))
+ test_arena_var_no_map();
+ if (test__start_subtest("pinned_arena"))
+ test_not_my_arena(true);
+ if (test__start_subtest("reused_arena"))
+ test_not_my_arena(false);
+ if (test__start_subtest("rust"))
+ test_rust();
+}
diff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c
index 8a6d341b754ac..460ad10ddc020 100644
--- a/tools/testing/selftests/bpf/prog_tests/verifier.c
+++ b/tools/testing/selftests/bpf/prog_tests/verifier.c
@@ -12,6 +12,7 @@
#include "verifier_and.skel.h"
#include "verifier_arena.skel.h"
#include "verifier_arena_large.skel.h"
+#include "verifier_arena_scalar.skel.h"
#include "verifier_arena_globals1.skel.h"
#include "verifier_arena_globals2.skel.h"
#include "verifier_array_access.skel.h"
@@ -198,6 +199,7 @@ void test_verifier_align(void) { RUN(verifier_align); }
void test_verifier_and(void) { RUN(verifier_and); }
void test_verifier_arena(void) { RUN(verifier_arena); }
void test_verifier_arena_large(void) { RUN(verifier_arena_large); }
+void test_verifier_arena_scalar(void) { RUN(verifier_arena_scalar); }
void test_verifier_arena_globals1(void) { RUN(verifier_arena_globals1); }
void test_verifier_arena_globals2(void) { RUN(verifier_arena_globals2); }
void test_verifier_basic_stack(void) { RUN(verifier_basic_stack); }
diff --git a/tools/testing/selftests/bpf/progs/data_in_arena.c b/tools/testing/selftests/bpf/progs/data_in_arena.c
new file mode 100644
index 0000000000000..ce3838327d6f4
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/data_in_arena.c
@@ -0,0 +1,107 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+/* global data of the object is in arena */
+char data_in_arena SEC(".arena.data");
+
+int counter = 5;
+long pair[2] = { 1, 2 };
+int x = 42;
+long sum;
+long table[4];
+struct {
+ long v[8];
+} aligned64 __attribute__((aligned(64)));
+const volatile int ro = 7;
+const volatile long ro_table[4] = { 10, 20, 30, 40 };
+
+/* const strings stay in a map for helpers and kfuncs, a copy of them is in arena */
+const char hello[] SEC(".rodata.str.hello") = "hello";
+
+/* pointers to data that are stored in data */
+int *px = &x;
+const char *str = hello;
+int *const volatile cpx SEC(".data.rel.ro") = &x;
+
+SEC("syscall")
+int use_data(void *ctx)
+{
+ int i;
+
+ for (i = 0; i < 4; i++)
+ table[i] = ro_table[i] + i;
+ sum = counter + ro + pair[0] + pair[1];
+ counter++;
+ __sync_fetch_and_add(&pair[1], 3);
+ return sum;
+}
+
+typedef int (*op_fn)(int);
+
+static __noinline int add1(int v)
+{
+ return v + 1;
+}
+
+/*
+ * Pointers to functions and to data in read-only data of a program with callx.
+ * Volatile, so that the compiler doesn't replace the pointers with what
+ * they point to.
+ */
+static const volatile struct {
+ op_fn fn;
+ int *data;
+ const char *name;
+} ops SEC(".data.rel.ro") = { add1, &x, hello };
+
+SEC("syscall")
+int use_ops(void *ctx)
+{
+ /* a program has the arena when its code refers to it */
+ counter++;
+ return ops.fn(*ops.data) + ops.name[1];
+}
+
+SEC("syscall")
+int use_ptrs(void *ctx)
+{
+ unsigned long addr = (unsigned long)&aligned64;
+ char local[4] = "abc";
+
+ /* hide the address from the compiler, it knows that '& 63' is 0 */
+ asm volatile ("" : "+r"(addr));
+ if (addr & 63)
+ return 1;
+ aligned64.v[7] = 7;
+ if (*px != 42)
+ return 2;
+ *px = 43;
+ if (x != 43 || *cpx != 43)
+ return 3;
+ if (str[0] != 'h' || str[4] != 'o' || str[5])
+ return 4;
+ /* the literal is in a map */
+ if (bpf_strncmp(local, sizeof(local), "abc"))
+ return 5;
+ return 0;
+}
+
+char out[16];
+
+/* format strings are in a map */
+SEC("syscall")
+int use_printk(void *ctx)
+{
+ char buf[sizeof(out)];
+ int i, n;
+
+ bpf_printk("counter %d", counter);
+ n = BPF_SNPRINTF(buf, sizeof(buf), "%d-%d", x, ro);
+ for (i = 0; i < sizeof(out); i++)
+ out[i] = buf[i];
+ return n;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/data_in_arena_decl.c b/tools/testing/selftests/bpf/progs/data_in_arena_decl.c
new file mode 100644
index 0000000000000..01bc4fea8f0c0
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/data_in_arena_decl.c
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#define BPF_NO_KFUNC_PROTOTYPES
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_experimental.h"
+#include <bpf_arena_common.h>
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARENA);
+ __uint(map_flags, BPF_F_MMAPABLE);
+ __uint(max_entries, 4);
+} arena SEC(".maps");
+
+/* global data of the object is in arena */
+char data_in_arena SEC(".arena.data");
+
+int counter = 5;
+long sum;
+const volatile int ro = 7;
+
+#if defined(__BPF_FEATURE_ADDR_SPACE_CAST)
+int __arena avar = 11;
+#else
+int avar = 11;
+#endif
+
+SEC("syscall")
+int use_data(void *ctx)
+{
+ sum = counter + ro + avar;
+ counter++;
+ avar++;
+ return sum;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/data_in_arena_extern.c b/tools/testing/selftests/bpf/progs/data_in_arena_extern.c
new file mode 100644
index 0000000000000..dc1ad5ca3bdd0
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/data_in_arena_extern.c
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+/* global data of the object is in arena */
+char data_in_arena SEC(".arena.data");
+
+extern const int bpf_prog_active __ksym;
+
+/* the variable of the kernel is not in arena */
+const void *kp = &bpf_prog_active;
+
+SEC("syscall")
+int ptr_to_extern(void *ctx)
+{
+ return *(int *)kp;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/data_in_arena_fail.c b/tools/testing/selftests/bpf/progs/data_in_arena_fail.c
new file mode 100644
index 0000000000000..ad8afe9afc962
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/data_in_arena_fail.c
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+/* global data of the object is in arena */
+char data_in_arena SEC(".arena.data");
+
+int x = 42;
+/* the table is read-only data with pointers. It's not in arena. */
+int *const volatile tbl[1] SEC(".data.rel.ro") = { &x };
+int *const volatile *pp = tbl;
+
+SEC("syscall")
+int ptr_to_map(void *ctx)
+{
+ return **pp;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/data_in_arena_nomap.c b/tools/testing/selftests/bpf/progs/data_in_arena_nomap.c
new file mode 100644
index 0000000000000..da27a8f10d6fa
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/data_in_arena_nomap.c
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_arena_common.h"
+
+/* global data of the object is in arena */
+char data_in_arena SEC(".arena.data");
+
+int counter = 5;
+/* needs an arena map that is declared. The one that libbpf creates won't do. */
+int __arena avar = 11;
+
+SEC("syscall")
+int arena_var(void *ctx)
+{
+ return avar + counter;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/data_in_arena_rust.rs b/tools/testing/selftests/bpf/progs/data_in_arena_rust.rs
new file mode 100644
index 0000000000000..b69128487a0fc
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/data_in_arena_rust.rs
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: GPL-2.0
+
+// Why .data, .bss and .rodata of a program in Rust are in arena.
+//
+// A reference in Rust is an address. It doesn't say what it points to and it
+// can be stored in data. The list below has a node in each of the sections.
+// The nodes are linked by references that are in the data:
+// IN_BSS.next is stored by the program,
+// IN_DATA.next is a relocation in .data against .rodata.
+// sum() loads the references back and reads the three nodes with the same insn.
+//
+// When the sections are array maps libbpf skips the relocation in .data, and
+// what sum() loads from a node is a number that can't be dereferenced:
+// R1 invalid mem access 'scalar'
+// In arena the address of a node is a number to begin with.
+
+#![no_std]
+#![no_main]
+
+// Tell libbpf to keep .data, .bss and .rodata in arena.
+#[used]
+#[link_section = ".arena.data"]
+static DATA_IN_ARENA: u8 = 0;
+
+#[used]
+#[link_section = "license"]
+static LICENSE: [u8; 4] = *b"GPL\0";
+
+// panic=abort is a stop gap until panic=unwind is supported.
+// Nothing here panics, so the handler is not a part of the program.
+#[panic_handler]
+fn panic(_info: &core::panic::PanicInfo) -> ! {
+ loop {}
+}
+
+pub struct Node {
+ val: u32,
+ next: Option<&'static Node>,
+}
+
+// no_mangle makes them visible outside, so LLVM can't fold the list into a constant.
+#[no_mangle]
+static IN_RODATA: Node = Node { val: 3, next: None };
+#[no_mangle]
+static mut IN_DATA: Node = Node { val: 20, next: Some(&IN_RODATA) };
+#[no_mangle]
+static mut IN_BSS: Node = Node { val: 0, next: None };
+
+#[inline(never)]
+fn sum(mut node: Option<&Node>) -> u32 {
+ let mut sum = 0;
+ // The verifier wants a bound.
+ for _ in 0..8 {
+ let Some(n) = node else { break };
+ sum += n.val;
+ node = n.next;
+ }
+ sum
+}
+
+#[no_mangle]
+#[link_section = "syscall"]
+pub extern "C" fn list_in_data(_ctx: *mut u8) -> u32 {
+ unsafe {
+ let head = &mut *&raw mut IN_BSS;
+ head.val = 100;
+ head.next = Some(&*&raw const IN_DATA);
+ sum(Some(head))
+ }
+}
diff --git a/tools/testing/selftests/bpf/progs/verifier_arena_scalar.c b/tools/testing/selftests/bpf/progs/verifier_arena_scalar.c
new file mode 100644
index 0000000000000..bebc37f501b7e
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/verifier_arena_scalar.c
@@ -0,0 +1,912 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include "../../../include/linux/filter.h"
+#include "bpf_misc.h"
+
+void *bpf_arena_alloc_pages(void *map, void *addr, __u32 page_cnt, int node_id,
+ __u64 flags) __ksym;
+
+#ifdef __TARGET_ARCH_arm64
+#define ARENA_VM_START (1ull << 32)
+#else
+#define ARENA_VM_START (1ull << 44)
+#endif
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARENA);
+ __uint(map_flags, BPF_F_MMAPABLE);
+ __uint(max_entries, 4);
+ __ulong(map_extra, ARENA_VM_START);
+} arena SEC(".maps");
+
+struct {
+ __uint(type, BPF_MAP_TYPE_HASH);
+ __uint(max_entries, 1);
+ __type(key, int);
+ __type(value, long long);
+} hash SEC(".maps");
+
+/* JITs that take BPF_F_ARENA_SCALAR */
+#define __arena_scalar __flag(BPF_F_ARENA_SCALAR) __arch_x86_64 __arch_arm64
+
+/* BTF FUNC records are not generated for kfuncs referenced from inline assembly */
+void __kfunc_btf_root(void)
+{
+ bpf_arena_alloc_pages(0, 0, 0, 0, 0);
+}
+
+/* Tests start with r6 = address of a new page as the user space sees it, a number */
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: load and store of every size")
+__success __retval(0)
+__load_if_JITed()
+__naked void ld_st_sizes(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ r7 = r6; \
+ r1 = 0x1122334455667788 ll; \
+ *(u64 *)(r6 + 0) = r1; \
+ *(u32 *)(r6 + 8) = r1; \
+ *(u16 *)(r6 + 12) = r1; \
+ *(u8 *)(r6 + 14) = r1; \
+ *(u64 *)(r6 + 16) = 0x1234; \
+ *(u32 *)(r6 + 24) = 0x5678; \
+ *(u16 *)(r6 + 28) = 0x9a; \
+ *(u8 *)(r6 + 30) = 0xbc; \
+ r0 = 1; \
+ r2 = *(u64 *)(r6 + 0); \
+ if r2 != r1 goto 9f; \
+ r0 = 2; \
+ r2 = *(u32 *)(r6 + 8); \
+ if r2 != 0x55667788 goto 9f; \
+ r0 = 3; \
+ r2 = *(u16 *)(r6 + 12); \
+ if r2 != 0x7788 goto 9f; \
+ r0 = 4; \
+ r2 = *(u8 *)(r6 + 14); \
+ if r2 != 0x88 goto 9f; \
+ r0 = 5; \
+ r2 = *(u64 *)(r6 + 16); \
+ if r2 != 0x1234 goto 9f; \
+ r0 = 6; \
+ r2 = *(u32 *)(r6 + 24); \
+ if r2 != 0x5678 goto 9f; \
+ r0 = 7; \
+ r2 = *(u16 *)(r6 + 28); \
+ if r2 != 0x9a goto 9f; \
+ r0 = 8; \
+ r2 = *(u8 *)(r6 + 30); \
+ if r2 != 0xbc goto 9f; \
+ /* the address is what it was */ \
+ r0 = 9; \
+ if r6 != r7 goto 9f; \
+ r0 = 10; \
+ r7 >>= 32; \
+ if r7 == 0 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: load into the register that holds the address")
+__success __retval(0)
+__load_if_JITed()
+__naked void ld_into_base(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ r1 = 77; \
+ *(u64 *)(r6 + 0) = r1; \
+ r6 = *(u64 *)(r6 + 0); \
+ r0 = 1; \
+ if r6 != 77 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: sign extending load")
+__success __retval(0)
+__load_if_JITed()
+__naked void ldsx(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ r7 = r6; \
+ *(u64 *)(r6 + 0) = 0x80; \
+ .8byte %[ldsx_insn]; /* r2 = *(s8 *)(r6 + 0) */ \
+ r0 = 1; \
+ if r2 != -128 goto 9f; \
+ r0 = 2; \
+ if r6 != r7 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages),
+ __imm_insn(ldsx_insn, BPF_RAW_INSN(BPF_LDX | BPF_MEMSX | BPF_B,
+ BPF_REG_2, BPF_REG_6, 0, 0))
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: address loaded from arena")
+__success __retval(0)
+__load_if_JITed()
+__naked void ptr_chase(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ /* page[0] = &page[64]; page[64] = 5; */ \
+ r1 = r6; \
+ r1 += 64; \
+ *(u64 *)(r6 + 0) = r1; \
+ *(u64 *)(r1 + 0) = 5; \
+ r2 = *(u64 *)(r6 + 0); \
+ r0 = 1; \
+ if r2 != r1 goto 9f; \
+ r3 = *(u64 *)(r2 + 0); \
+ r0 = 2; \
+ if r3 != 5 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: atomics")
+__success __retval(0)
+__load_if_JITed()
+__naked void atomics(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ r7 = r6; \
+ *(u64 *)(r6 + 8) = 1; \
+ r1 = 2; \
+ lock *(u64 *)(r6 + 8) += r1; \
+ r1 = 4; \
+ .8byte %[fetch_add_insn]; /* r1 = atomic_fetch_add((u64 *)(r6 + 8), r1) */ \
+ r0 = 1; \
+ if r1 != 3 goto 9f; \
+ r1 = 8; \
+ .8byte %[xchg_insn]; /* r1 = xchg_64(r6 + 8, r1) */ \
+ r0 = 2; \
+ if r1 != 7 goto 9f; \
+ r0 = 8; \
+ r1 = 16; \
+ .8byte %[cmpxchg_insn]; /* r0 = cmpxchg_64(r6 + 8, r0, r1) */ \
+ r2 = r0; \
+ r0 = 3; \
+ if r2 != 8 goto 9f; \
+ r2 = *(u64 *)(r6 + 8); \
+ r0 = 4; \
+ if r2 != 16 goto 9f; \
+ r0 = 5; \
+ if r6 != r7 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages),
+ __imm_insn(fetch_add_insn, BPF_ATOMIC_OP(BPF_DW, BPF_ADD | BPF_FETCH,
+ BPF_REG_6, BPF_REG_1, 8)),
+ __imm_insn(xchg_insn, BPF_ATOMIC_OP(BPF_DW, BPF_XCHG, BPF_REG_6, BPF_REG_1, 8)),
+ __imm_insn(cmpxchg_insn, BPF_ATOMIC_OP(BPF_DW, BPF_CMPXCHG, BPF_REG_6, BPF_REG_1, 8))
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: cmpxchg through r0")
+__success __retval(0)
+__load_if_JITed()
+__naked void cmpxchg_r0(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ /* The address is in r0. The value at the address is not equal to it. */ \
+ *(u64 *)(r6 + 0) = 3; \
+ r0 = r6; \
+ r1 = 5; \
+ .8byte %[cmpxchg_insn]; /* r0 = cmpxchg_64(r0 + 0, r0, r1) */ \
+ r2 = r0; \
+ r0 = 1; \
+ if r2 != 3 goto 9f; \
+ r2 = *(u64 *)(r6 + 0); \
+ r0 = 2; \
+ if r2 != 3 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages),
+ __imm_insn(cmpxchg_insn, BPF_ATOMIC_OP(BPF_DW, BPF_CMPXCHG, BPF_REG_0, BPF_REG_1, 0))
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: xchg into the register that holds the address")
+__success __retval(0)
+__load_if_JITed()
+__naked void xchg_into_base(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ *(u64 *)(r6 + 0) = 3; \
+ r1 = r6; \
+ .8byte %[xchg_insn]; /* r1 = xchg_64(r1 + 0, r1) */ \
+ r0 = 1; \
+ if r1 != 3 goto 9f; \
+ r2 = *(u64 *)(r6 + 0); \
+ r0 = 2; \
+ if r2 != r6 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages),
+ __imm_insn(xchg_insn, BPF_ATOMIC_OP(BPF_DW, BPF_XCHG, BPF_REG_1, BPF_REG_1, 0))
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: xchg into the register that holds a pointer to stack")
+__failure __msg("misaligned access off (0x0; 0xffffffffffffffff)+0 size 8")
+__naked void xchg_into_stack_ptr(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r1 = 0; \
+ *(u64 *)(r10 - 8) = r1; \
+ r1 = r10; \
+ r1 += -8; \
+ .8byte %[xchg_insn]; /* r1 = xchg_64(r1 + 0, r1) */ \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm_addr(arena),
+ __imm_insn(xchg_insn, BPF_ATOMIC_OP(BPF_DW, BPF_XCHG, BPF_REG_1, BPF_REG_1, 0))
+ : __clobber_all);
+}
+
+#ifdef CAN_USE_LOAD_ACQ_STORE_REL
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: load-acquire and store-release")
+__success __retval(0)
+__load_if_JITed()
+__naked void load_acq_store_rel(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ r7 = r6; \
+ r1 = 0x1234; \
+ .8byte %[store_release_insn]; /* store_release((u64 *)(r6 + 8), r1) */ \
+ .8byte %[load_acquire_insn]; /* r2 = load_acquire((u64 *)(r6 + 8)) */ \
+ r0 = 1; \
+ if r2 != 0x1234 goto 9f; \
+ .8byte %[load_acquire8_insn]; /* w2 = load_acquire((u8 *)(r6 + 8)) */ \
+ r0 = 2; \
+ if r2 != 0x34 goto 9f; \
+ r0 = 3; \
+ if r6 != r7 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages),
+ __imm_insn(store_release_insn,
+ BPF_ATOMIC_OP(BPF_DW, BPF_STORE_REL, BPF_REG_6, BPF_REG_1, 8)),
+ __imm_insn(load_acquire_insn,
+ BPF_ATOMIC_OP(BPF_DW, BPF_LOAD_ACQ, BPF_REG_2, BPF_REG_6, 8)),
+ __imm_insn(load_acquire8_insn,
+ BPF_ATOMIC_OP(BPF_B, BPF_LOAD_ACQ, BPF_REG_2, BPF_REG_6, 8))
+ : __clobber_all);
+}
+
+#endif /* CAN_USE_LOAD_ACQ_STORE_REL */
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: number that is not an address in arena")
+__success __retval(0)
+__load_if_JITed()
+__naked void not_in_arena(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ /* nothing is allocated: all loads read 0, stores are dropped */ \
+ r6 = 0xdeadbeef00000000 ll; \
+ r0 = 1; \
+ r2 = *(u64 *)(r6 + 0); \
+ if r2 != 0 goto 9f; \
+ r0 = 2; \
+ r2 = *(u8 *)(r6 - 32768); \
+ if r2 != 0 goto 9f; \
+ r6 = 0x12345678ffffffff ll; \
+ r0 = 3; \
+ r2 = *(u64 *)(r6 + 32760); \
+ if r2 != 0 goto 9f; \
+ *(u64 *)(r6 + 32760) = 1; \
+ *(u8 *)(r6 + 32767) = r2; \
+ r1 = 1; \
+ lock *(u64 *)(r6 + 32760) += r1; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: store through the address of the stack as a number")
+__success __retval(0)
+__load_if_JITed()
+__naked void stack_addr_as_number(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ *(u64 *)(r10 - 8) = 5; \
+ r6 = r10; \
+ r6 |= 0; \
+ /* r6 is a number now. The store goes to arena, not to the stack. */ \
+ *(u64 *)(r6 - 8) = 7; \
+ r1 = 9; \
+ *(u64 *)(r6 - 8) = r1; \
+ lock *(u64 *)(r6 - 8) += r1; \
+ r2 = *(u64 *)(r10 - 8); \
+ r0 = 1; \
+ if r2 != 5 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: 64-bit math on the address stays 64-bit")
+__success __retval(0)
+__load_if_JITed()
+__naked void alu64_after_access(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ r2 = *(u64 *)(r6 + 0); \
+ r7 = r6; \
+ r7 += 8; \
+ r7 -= r6; \
+ r0 = 1; \
+ if r7 != 8 goto 9f; \
+ r7 = r6; \
+ r7 >>= 32; \
+ r0 = 2; \
+ if r7 == 0 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: st of an immediate changes no register")
+__success __retval(0)
+__load_if_JITed()
+__naked void st_keeps_regs(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ r0 = r6; \
+ r1 = 0x1111; \
+ r2 = 0x2222; \
+ *(u64 *)(r0 + 0) = 5; \
+ r3 = r0; \
+ r0 = 1; \
+ if r3 != r6 goto 9f; \
+ r0 = 2; \
+ if r1 != 0x1111 goto 9f; \
+ r0 = 3; \
+ if r2 != 0x2222 goto 9f; \
+ r0 = 0x3333; \
+ r1 = r6; \
+ *(u32 *)(r1 + 8) = -7; \
+ r3 = r0; \
+ r0 = 4; \
+ if r3 != 0x3333 goto 9f; \
+ r0 = 5; \
+ if r1 != r6 goto 9f; \
+ r0 = 6; \
+ r3 = *(u64 *)(r6 + 0); \
+ if r3 != 5 goto 9f; \
+ r0 = 7; \
+ r3 = *(u64 *)(r6 + 8); \
+ r4 = 0xfffffff9 ll; \
+ if r3 != r4 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: access through a number with garbage in the upper half")
+__success __retval(0)
+__load_if_JITed()
+__naked void st_value_garbage(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ r1 = 0xdeadbeef00001000 ll; \
+ *(u64 *)(r6 + 2048) = r1; \
+ r7 = *(u64 *)(r6 + 2048); \
+ r8 = *(u64 *)(r6 + 2048); \
+ r1 = 3; \
+ *(u64 *)(r8 + 0) = 1; \
+ r0 = 1; \
+ if r8 != r7 goto 9f; \
+ *(u8 *)(r8 + 1) = 1; \
+ r0 = 2; \
+ if r8 != r7 goto 9f; \
+ *(u32 *)(r8 + 4) = r1; \
+ r0 = 3; \
+ if r8 != r7 goto 9f; \
+ r2 = *(u16 *)(r8 + 2); \
+ r0 = 4; \
+ if r8 != r7 goto 9f; \
+ r0 = 5; \
+ if r1 != 3 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: access through a number without the upper half")
+__success __retval(0)
+__load_if_JITed()
+__naked void st_value_small(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ r1 = 0x1000 ll; \
+ *(u64 *)(r6 + 2048) = r1; \
+ r7 = *(u64 *)(r6 + 2048); \
+ r8 = *(u64 *)(r6 + 2048); \
+ r1 = 3; \
+ *(u64 *)(r8 + 0) = 1; \
+ r0 = 1; \
+ if r8 != r7 goto 9f; \
+ *(u8 *)(r8 + 1) = 1; \
+ r0 = 2; \
+ if r8 != r7 goto 9f; \
+ *(u32 *)(r8 + 4) = r1; \
+ r0 = 3; \
+ if r8 != r7 goto 9f; \
+ r2 = *(u16 *)(r8 + 2); \
+ r0 = 4; \
+ if r8 != r7 goto 9f; \
+ r0 = 5; \
+ if r1 != 3 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: atomics through a number that is not an address in arena")
+__success __retval(0)
+__load_if_JITed()
+__naked void atomic_value(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ r1 = 0xdeadbeef00001000 ll; \
+ *(u64 *)(r6 + 2048) = r1; \
+ r7 = *(u64 *)(r6 + 2048); \
+ r8 = *(u64 *)(r6 + 2048); \
+ r1 = 3; \
+ lock *(u64 *)(r8 + 0) += r1; \
+ r0 = 1; \
+ if r8 != r7 goto 9f; \
+ .8byte %[fetch_add_insn]; \
+ r0 = 2; \
+ if r8 != r7 goto 9f; \
+ .8byte %[xchg_insn]; \
+ r0 = 3; \
+ if r8 != r7 goto 9f; \
+ r0 = 0; \
+ .8byte %[cmpxchg_insn]; \
+ r0 = 4; \
+ if r8 != r7 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages),
+ __imm_insn(fetch_add_insn, BPF_ATOMIC_OP(BPF_DW, BPF_ADD | BPF_FETCH,
+ BPF_REG_8, BPF_REG_1, 0)),
+ __imm_insn(xchg_insn, BPF_ATOMIC_OP(BPF_DW, BPF_XCHG, BPF_REG_8, BPF_REG_1, 0)),
+ __imm_insn(cmpxchg_insn, BPF_ATOMIC_OP(BPF_DW, BPF_CMPXCHG, BPF_REG_8, BPF_REG_1, 0))
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: number and pointer to arena at the same insn")
+__success __retval(0)
+__load_if_JITed()
+__naked void mixed_number_arena(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ *(u64 *)(r6 + 8) = 0x1234; \
+ /* a number that the verifier does not know, 0 at run time */ \
+ r7 = *(u64 *)(r6 + 16); \
+ r8 = r6; \
+ if r7 != 0 goto 1f; \
+ .8byte %[cast_kern_insn]; \
+1: *(u8 *)(r8 + 0) = 1; \
+ *(u8 *)(r8 + 1) = r7; \
+ r2 = *(u8 *)(r8 + 1); \
+ lock *(u64 *)(r8 + 24) += r7; \
+ r0 = 0; \
+ if r7 != 0 goto 9f; \
+ /* pointer to arena only: JIT adds all 64 bits of r8 to the base */ \
+ r0 = 2; \
+ r2 = *(u64 *)(r8 + 8); \
+ if r2 != 0x1234 goto 9f; \
+ r0 = 3; \
+ r2 = *(u8 *)(r6 + 0); \
+ if r2 != 1 goto 9f; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages),
+ __imm_insn(cast_kern_insn, BPF_RAW_INSN(BPF_ALU64 | BPF_MOV | BPF_X,
+ BPF_REG_8, BPF_REG_8, 1, 1))
+ : __clobber_all);
+}
+
+SEC("syscall")
+__description("arena_scalar: no flag, no access through a number")
+__failure __msg("R6 invalid mem access 'scalar'")
+__naked void no_flag(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r6 = 0x100000000000 ll; \
+ r0 = *(u64 *)(r6 + 0); \
+ exit; \
+" :
+ : __imm_addr(arena)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: no arena, no access through a number")
+__failure __msg("R6 invalid mem access 'scalar'")
+__naked void no_arena(void)
+{
+ asm volatile (" \
+ r6 = 0x100000000000 ll; \
+ r0 = *(u64 *)(r6 + 0); \
+ exit; \
+" ::: __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: pointer that is NULL is an address in arena")
+__success __retval(0)
+__load_if_JITed()
+__naked void null_ptr(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r1 = 0; \
+ *(u32 *)(r10 - 4) = r1; \
+ r2 = r10; \
+ r2 += -4; \
+ r1 = %[hash] ll; \
+ call %[bpf_map_lookup_elem]; \
+ r1 = r0; \
+ r0 = 1; \
+ if r1 != 0 goto 9f; \
+ /* nothing is allocated: the load reads 0 */ \
+ r0 = *(u64 *)(r1 + 0); \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm_addr(hash),
+ __imm(bpf_map_lookup_elem)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: pointer that is NULL with an offset is an address in arena")
+__success __retval(0)
+__load_if_JITed()
+__naked void null_ptr_off(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r1 = 0; \
+ *(u32 *)(r10 - 4) = r1; \
+ r2 = r10; \
+ r2 += -4; \
+ r1 = %[hash] ll; \
+ call %[bpf_map_lookup_elem]; \
+ r1 = r0; \
+ r0 = 1; \
+ if r1 != 0 goto 9f; \
+ r1 += 8; \
+ *(u64 *)(r1 + 0) = 5; \
+ r0 = *(u64 *)(r1 + 0); \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm_addr(hash),
+ __imm(bpf_map_lookup_elem)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: number that is less than a page is an address in arena")
+__success __retval(0)
+__load_if_JITed()
+__naked void small_number(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ call %[bpf_get_prandom_u32]; \
+ r1 = r0; \
+ r1 &= 0xfff; \
+ r0 = *(u64 *)(r1 + 0); \
+ exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_get_prandom_u32)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: number is not a pointer for a helper")
+__failure __msg("R2 type=scalar expected=")
+__naked void helper_arg(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ r1 = %[hash] ll; \
+ r2 = r6; \
+ call %[bpf_map_lookup_elem]; \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm_addr(hash),
+ __imm(bpf_arena_alloc_pages),
+ __imm(bpf_map_lookup_elem)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: number and pointer to stack at the same insn")
+__failure __msg("same insn cannot be used with different pointers")
+__load_if_JITed()
+__naked void mixed_number_stack(void)
+{
+ asm volatile (" \
+ r1 = %[arena] ll; \
+ r2 = 0; \
+ r3 = 1; \
+ r4 = -1; \
+ r5 = 0; \
+ call %[bpf_arena_alloc_pages]; \
+ r6 = r0; \
+ r0 = 100; \
+ if r6 == 0 goto 9f; \
+ *(u64 *)(r10 - 8) = 0; \
+ call %[bpf_get_prandom_u32]; \
+ if w0 != 0 goto 1f; \
+ r6 = r10; \
+ r6 += -8; \
+1: r0 = *(u64 *)(r6 + 0); \
+ r0 = 0; \
+9: exit; \
+" :
+ : __imm_addr(arena),
+ __imm(bpf_arena_alloc_pages),
+ __imm(bpf_get_prandom_u32)
+ : __clobber_all);
+}
+
+static int st_cb(__u64 idx, void *ctx)
+{
+ volatile long *p = *(volatile long **)ctx;
+
+ p[idx] = 7;
+ return 0;
+}
+
+SEC("syscall")
+__arena_scalar
+__description("arena_scalar: store through a number in a callback")
+__success __retval(0)
+__load_if_JITed()
+int st_in_callback(void *unused)
+{
+ volatile long *p = bpf_arena_alloc_pages(&arena, NULL, 1, -1, 0);
+
+ if (!p)
+ return 100;
+ bpf_loop(4, st_cb, &p, 0);
+ return p[0] + p[3] + p[4] - 14;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/verifier_value_illegal_alu.c b/tools/testing/selftests/bpf/progs/verifier_value_illegal_alu.c
index 4d8273c258d51..31663338866d3 100644
--- a/tools/testing/selftests/bpf/progs/verifier_value_illegal_alu.c
+++ b/tools/testing/selftests/bpf/progs/verifier_value_illegal_alu.c
@@ -22,8 +22,8 @@ struct {
SEC("socket")
__description("map element value illegal alu op, 1")
-__failure __msg("R0 bitwise operator &= on pointer")
-__failure_unpriv
+__failure __msg("R0 invalid mem access 'scalar'")
+__failure_unpriv __msg_unpriv("R0 bitwise operator &= on pointer")
__naked void value_illegal_alu_op_1(void)
{
asm volatile (" \
@@ -70,8 +70,8 @@ l0_%=: exit; \
SEC("socket")
__description("map element value illegal alu op, 3")
-__failure __msg("R0 pointer arithmetic with /= operator")
-__failure_unpriv
+__failure __msg("R0 invalid mem access 'scalar'")
+__failure_unpriv __msg_unpriv("R0 pointer arithmetic with /= operator")
__naked void value_illegal_alu_op_3(void)
{
asm volatile (" \
@@ -165,6 +165,171 @@ __naked void map_ptr_illegal_alu_op(void)
: __clobber_all);
}
+SEC("socket")
+__description("tag in the low bit of a pointer, and, shift")
+__success __retval(0)
+__failure_unpriv __msg_unpriv("R1 bitwise operator &= on pointer")
+__naked void ptr_tag_and_shift(void)
+{
+ asm volatile (" \
+ r2 = r10; \
+ r2 += -8; \
+ r1 = 0; \
+ *(u64*)(r2 + 0) = r1; \
+ r1 = %[map_hash_48b] ll; \
+ call %[bpf_map_lookup_elem]; \
+ if r0 == 0 goto l0_%=; \
+ r1 = r0; \
+ r1 &= 1; \
+ r2 = r0; \
+ r2 >>= 1; \
+ r3 = r0; \
+ r3 |= 1; \
+ r3 ^= 1; \
+ r0 = *(u32*)(r0 + 0); \
+ r0 = 0; \
+l0_%=: exit; \
+" :
+ : __imm(bpf_map_lookup_elem),
+ __imm_addr(map_hash_48b)
+ : __clobber_all);
+}
+
+SEC("socket")
+__description("tag in the low bit of a pointer, CAP_BPF without CAP_PERFMON")
+__success __retval(0)
+__failure_unpriv __msg_unpriv("R1 bitwise operator &= on pointer")
+__caps_unpriv(CAP_BPF)
+__naked void ptr_tag_cap_bpf(void)
+{
+ asm volatile (" \
+ r2 = r10; \
+ r2 += -8; \
+ r1 = 0; \
+ *(u64*)(r2 + 0) = r1; \
+ r1 = %[map_hash_48b] ll; \
+ call %[bpf_map_lookup_elem]; \
+ if r0 == 0 goto l0_%=; \
+ r1 = r0; \
+ r1 &= 1; \
+ r0 = 0; \
+l0_%=: exit; \
+" :
+ : __imm(bpf_map_lookup_elem),
+ __imm_addr(map_hash_48b)
+ : __clobber_all);
+}
+
+SEC("socket")
+__description("number op= pointer")
+__success __retval(0)
+__failure_unpriv __msg_unpriv("R1 pointer arithmetic with *= operator")
+__naked void number_mul_ptr(void)
+{
+ asm volatile (" \
+ r2 = r10; \
+ r2 += -8; \
+ r1 = 0; \
+ *(u64*)(r2 + 0) = r1; \
+ r1 = %[map_hash_48b] ll; \
+ call %[bpf_map_lookup_elem]; \
+ if r0 == 0 goto l0_%=; \
+ r1 = 7; \
+ r1 *= r0; \
+ r0 = 0; \
+l0_%=: exit; \
+" :
+ : __imm(bpf_map_lookup_elem),
+ __imm_addr(map_hash_48b)
+ : __clobber_all);
+}
+
+SEC("socket")
+__description("pointer with the tag cleared is a number")
+__failure __msg("R0 invalid mem access 'scalar'")
+__failure_unpriv __msg_unpriv("R0 bitwise operator |= on pointer")
+__naked void ptr_tag_cleared_deref(void)
+{
+ asm volatile (" \
+ r2 = r10; \
+ r2 += -8; \
+ r1 = 0; \
+ *(u64*)(r2 + 0) = r1; \
+ r1 = %[map_hash_48b] ll; \
+ call %[bpf_map_lookup_elem]; \
+ if r0 == 0 goto l0_%=; \
+ r0 |= 1; \
+ r0 ^= 1; \
+ r0 = *(u32*)(r0 + 0); \
+l0_%=: r0 = 0; \
+ exit; \
+" :
+ : __imm(bpf_map_lookup_elem),
+ __imm_addr(map_hash_48b)
+ : __clobber_all);
+}
+
+SEC("socket")
+__description("shift of a pointer that may be NULL")
+__failure __msg("R0 pointer arithmetic on map_value_or_null prohibited, null-check it first")
+__failure_unpriv
+__naked void ptr_or_null_shift(void)
+{
+ asm volatile (" \
+ r2 = r10; \
+ r2 += -8; \
+ r1 = 0; \
+ *(u64*)(r2 + 0) = r1; \
+ r1 = %[map_hash_48b] ll; \
+ call %[bpf_map_lookup_elem]; \
+ r0 >>= 1; \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm(bpf_map_lookup_elem),
+ __imm_addr(map_hash_48b)
+ : __clobber_all);
+}
+
+SEC("socket")
+__description("and of a pointer to map")
+__failure __msg("R0 pointer arithmetic on map_ptr prohibited")
+__failure_unpriv
+__naked void map_ptr_and(void)
+{
+ asm volatile (" \
+ r0 = %[map_hash_48b] ll; \
+ r0 &= 1; \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm_addr(map_hash_48b)
+ : __clobber_all);
+}
+
+SEC("socket")
+__description("32-bit and of a pointer")
+__success __retval(0)
+__failure_unpriv __msg_unpriv("R0 32-bit pointer arithmetic prohibited")
+__naked void ptr_and32(void)
+{
+ asm volatile (" \
+ r2 = r10; \
+ r2 += -8; \
+ r1 = 0; \
+ *(u64*)(r2 + 0) = r1; \
+ r1 = %[map_hash_48b] ll; \
+ call %[bpf_map_lookup_elem]; \
+ if r0 == 0 goto l0_%=; \
+ w0 &= 1; \
+l0_%=: r0 = 0; \
+ exit; \
+" :
+ : __imm(bpf_map_lookup_elem),
+ __imm_addr(map_hash_48b)
+ : __clobber_all);
+}
+
SEC("flow_dissector")
__description("flow_keys illegal alu op with variable offset")
__failure __msg("R7 pointer arithmetic on flow_keys prohibited")
diff --git a/tools/testing/selftests/bpf/test_loader.c b/tools/testing/selftests/bpf/test_loader.c
index 25eeb1c1248b3..a89890cd56d82 100644
--- a/tools/testing/selftests/bpf/test_loader.c
+++ b/tools/testing/selftests/bpf/test_loader.c
@@ -580,6 +580,8 @@ static int parse_test_spec(struct test_loader *tester,
update_flags(&spec->prog_flags, BPF_F_XDP_HAS_FRAGS, clear);
} else if (strcmp(val, "BPF_F_TEST_REG_INVARIANTS") == 0) {
update_flags(&spec->prog_flags, BPF_F_TEST_REG_INVARIANTS, clear);
+ } else if (strcmp(val, "BPF_F_ARENA_SCALAR") == 0) {
+ update_flags(&spec->prog_flags, BPF_F_ARENA_SCALAR, clear);
} else /* assume numeric value */ {
err = parse_int(val, &flags, "test prog flags");
if (err)
|
| 27/3 | 2026/10/03 02:38 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 52318 output: 36 thoughts: 1725 Thoughts: **KMSAN Fuzzing Evaluation for BPF Arena Scalar Support** Okay, so the user wants me to determine if we need a dedicated KMSAN fuzzing session for this patch series. Let's dig in. I need to understand what this patch series actually *does* before I can say definitively whether it warrants extra attention from KMSAN. First, I need to get a clear picture of the scope: * The patch introduces support for a new BPF feature called "arena scalar". * It looks like this involves changes across the BPF infrastructure, including the JIT compiler, the verifier, BTF handling, and even some user-space tooling. Now, let's step through the key kernel changes one by one. I will look for any indication of uninitialized memory reads, leaks, or incorrect handling. The presence of `arena_scalar` suggests a new memory management strategy within BPF, so that will be a priority. * **JIT Compiler Changes:** The architecture-specific JIT components now indicate support for arena scalars. * **Verifier Enhancements:** Significant changes here. `struct bpf_insn_aux_data` and `struct bpf_verifier_env` now contain a `arena_scalar` boolean. A new flag, `BPF_F_ARENA_SCALAR`, is defined. The verifier itself is clearly the core of this. The `is_arena_scalar()` function helps determine if a register is operating within the arena. Critically, there's a new `mem_access_reg()` function which seems to play a role in this arena mechanism. * **BTF Updates:** Changes to BTF handling seem to be related to Rust support. Relaxed name validation and adjustments for static function arguments are noted. I don't see any obvious connection to uninitialized memory issues. * **Core and Fixups:** The core includes the `bpf_jit_supports_arena_scalar` function. The fixups component introduces functions like `arena_scalar_access()` that insert `BPF_MOV32_REG` instructions. This suggests that when `arena_scalar` is active, some instructions get rewritten on the fly. This adds complexity, and complexity often means potential for errors. * **Syscall and Verifier Interaction:** The `bpf_prog_load` syscall now allows the `BPF_F_ARENA_SCALAR` flag. The verifier now sets `env->arena_scalar` based on this flag. The verifier is clearly the gatekeeper for this entire system. Let's examine the `mem_access_reg` function in more detail. This seems important. I see that it's responsible for managing a temporary `arena` struct on the stack. My concern is whether it could introduce issues. However, I now notice that `memset(arena, 0, sizeof(*arena))` explicitly zero-initializes this stack-allocated temporary struct! That's good. My next step is to examine how the `arena` struct is used within `check_mem_access()`, looking for any unexpected interactions with the new type `PTR_TO_ARENA`. To do that I will use `grepper` or `codesearch` to search for `PTR_TO_ARENA` usages in `check_mem_access`. I need to ensure that the memory access logic correctly interprets the arena pointer. I am now looking for potential flaws in how the verifier handles this new concept, particularly how it interacts with memory operations. Any incorrect handling here could be problematic and, therefore, I will also look for any potential missed checks. |
| 28/3 | 2026/10/03 02:38 | tool | grepper |
0mArgs:{
"Expression": "case PTR_TO_ARENA:",
"PathPrefix": "kernel/bpf/verifier.c"
}Results: {
"Output": "kernel/bpf/verifier.c=5405=static int check_ptr_alignment(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-5452-\t\tbreak;\nkernel/bpf/verifier.c:5453:\tcase PTR_TO_ARENA:\nkernel/bpf/verifier.c-5454-\t\treturn 0;\n--\nkernel/bpf/verifier.c=9151=static int check_func_arg_reg_off(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-9195-\tcase PTR_TO_BUF | MEM_RDONLY:\nkernel/bpf/verifier.c:9196:\tcase PTR_TO_ARENA:\nkernel/bpf/verifier.c-9197-\tcase SCALAR_VALUE:\n--\nkernel/bpf/verifier.c=19074=static bool reg_type_mismatch_ok(enum bpf_reg_type type)\n--\nkernel/bpf/verifier.c-19082-\tcase PTR_TO_BTF_ID:\nkernel/bpf/verifier.c:19083:\tcase PTR_TO_ARENA:\nkernel/bpf/verifier.c-19084-\t\treturn false;\n"
} |
| 29/3 | 2026/10/03 02:38 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 55191 output: 34 thoughts: 21 Thoughts: Here's a summary of my thought process, as requested: **Navigating `check_mem_access` to Understand PTR_TO_ARENA** Alright, let's dive into the `check_mem_access` function. I need to understand exactly how `PTR_TO_ARENA` is handled in this context. My goal is a granular understanding of the memory access validation logic, so I'm going to follow the code and see how `PTR_TO_ARENA` influences the checks. I'll be looking for the specific conditional branches, assignments, and comparisons involving this macro. I expect this will illuminate how the system determines valid memory regions related to the arena management, which is crucial for overall system integrity. Let's see what the function is doing. |
| 30/3 | 2026/10/03 02:38 | tool | grepper |
0mArgs:{
"Expression": "PTR_TO_ARENA",
"PathPrefix": "kernel/bpf/verifier.c"
}Results: {
"Output": "kernel/bpf/verifier.c=5246=static bool is_arena_reg(struct bpf_verifier_env *env, int regno)\n--\nkernel/bpf/verifier.c-5249-\nkernel/bpf/verifier.c:5250:\treturn reg-\u003etype == PTR_TO_ARENA;\nkernel/bpf/verifier.c-5251-}\n--\nkernel/bpf/verifier.c-5256- * a store through a number is an access to arena at the low 32 bits of it,\nkernel/bpf/verifier.c:5257: * like the access through PTR_TO_ARENA is. The register stays a number.\nkernel/bpf/verifier.c-5258- */\n--\nkernel/bpf/verifier.c=5405=static int check_ptr_alignment(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-5452-\t\tbreak;\nkernel/bpf/verifier.c:5453:\tcase PTR_TO_ARENA:\nkernel/bpf/verifier.c-5454-\t\treturn 0;\n--\nkernel/bpf/verifier.c=6912=static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, struct bpf_reg_state *reg, argno_t argno,\n--\nkernel/bpf/verifier.c-7129-\t\t\tmark_reg_unknown(env, regs, value_regno);\nkernel/bpf/verifier.c:7130:\t} else if (reg-\u003etype == PTR_TO_ARENA) {\nkernel/bpf/verifier.c-7131-\t\tif (t == BPF_READ \u0026\u0026 value_regno \u003e= 0)\n--\nkernel/bpf/verifier.c=7181=static struct bpf_reg_state *mem_access_reg(struct bpf_verifier_env *env, int regno,\n--\nkernel/bpf/verifier.c-7187-\tmemset(arena, 0, sizeof(*arena));\nkernel/bpf/verifier.c:7188:\tarena-\u003etype = PTR_TO_ARENA;\nkernel/bpf/verifier.c-7189-\tenv-\u003einsn_aux_data[env-\u003einsn_idx].arena_scalar = true;\n--\nkernel/bpf/verifier.c=8856=static const struct bpf_reg_types arena_types = {\nkernel/bpf/verifier.c-8857-\t.types = {\nkernel/bpf/verifier.c:8858:\t\tPTR_TO_ARENA,\nkernel/bpf/verifier.c-8859-\t\tSCALAR_VALUE,\n--\nkernel/bpf/verifier.c=8897=static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = {\n--\nkernel/bpf/verifier.c-8935-\t[ARG_PTR_TO_IRQ_FLAG]\t\t= \u0026stack_ptr_types,\nkernel/bpf/verifier.c:8936:\t[ARG_PTR_TO_ARENA]\t\t= \u0026arena_types,\nkernel/bpf/verifier.c-8937-\t[ARG_PTR_TO_CTX_OUT]\t\t= \u0026ctx_out_types,\n--\nkernel/bpf/verifier.c=9151=static int check_func_arg_reg_off(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-9195-\tcase PTR_TO_BUF | MEM_RDONLY:\nkernel/bpf/verifier.c:9196:\tcase PTR_TO_ARENA:\nkernel/bpf/verifier.c-9197-\tcase SCALAR_VALUE:\n--\nkernel/bpf/verifier.c=9417=static int check_func_arg(struct bpf_verifier_env *env, u32 arg, u32 slot, u32 prev_slot,\n--\nkernel/bpf/verifier.c-9666-\t\tbreak;\nkernel/bpf/verifier.c:9667:\tcase ARG_PTR_TO_ARENA:\nkernel/bpf/verifier.c-9668-\t\tbreak;\n--\nkernel/bpf/verifier.c=10858=static void gen_subprog_arg_proto(const struct bpf_subprog_info *sub, const struct btf *btf,\n--\nkernel/bpf/verifier.c-10875-\t\t\targ_type = ARG_IGNORE;\nkernel/bpf/verifier.c:10876:\t\t} else if (base_type(arg_type) == ARG_PTR_TO_ARENA) {\nkernel/bpf/verifier.c-10877-\t\t\targ_type |= PTR_MAYBE_NULL;\n--\nkernel/bpf/verifier.c=13433=get_kfunc_arg_type(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,\n--\nkernel/bpf/verifier.c-13592-\t\t */\nkernel/bpf/verifier.c:13593:\t\targ_type = ARG_PTR_TO_ARENA;\nkernel/bpf/verifier.c-13594-\t} else if (arg + 1 \u003c nargs \u0026\u0026\n--\nkernel/bpf/verifier.c=16753=static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-16771-\t/* Case where at least one operand is an arena. */\nkernel/bpf/verifier.c:16772:\tif (dst_reg-\u003etype == PTR_TO_ARENA || (src_reg \u0026\u0026 src_reg-\u003etype == PTR_TO_ARENA)) {\nkernel/bpf/verifier.c-16773-\nkernel/bpf/verifier.c:16774:\t\tif (dst_reg-\u003etype != PTR_TO_ARENA)\nkernel/bpf/verifier.c-16775-\t\t\t*dst_reg = *src_reg;\n--\nkernel/bpf/verifier.c=16942=static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)\n--\nkernel/bpf/verifier.c-17003-\t\t\t\t\tif (insn-\u003eimm == 1) /* cast from as(1) to as(0) */\nkernel/bpf/verifier.c:17004:\t\t\t\t\t\tdst_reg-\u003etype = PTR_TO_ARENA;\nkernel/bpf/verifier.c-17005-\t\t\t\t} else if (insn-\u003eoff == 0) {\n--\nkernel/bpf/verifier.c=19074=static bool reg_type_mismatch_ok(enum bpf_reg_type type)\n--\nkernel/bpf/verifier.c-19082-\tcase PTR_TO_BTF_ID:\nkernel/bpf/verifier.c:19083:\tcase PTR_TO_ARENA:\nkernel/bpf/verifier.c-19084-\t\treturn false;\n--\nkernel/bpf/verifier.c=20695=static int do_check_common(struct bpf_verifier_env *env, int subprog, bool is_sleepable)\n--\nkernel/bpf/verifier.c-20794-\t\t\t\treg-\u003eid = ++env-\u003eid_gen;\nkernel/bpf/verifier.c:20795:\t\t\t} else if (base_type(arg-\u003earg_type) == ARG_PTR_TO_ARENA) {\nkernel/bpf/verifier.c:20796:\t\t\t\t/* caller can pass either PTR_TO_ARENA or SCALAR */\nkernel/bpf/verifier.c-20797-\t\t\t\tmark_reg_unknown(env, regs, i);\n--\nkernel/bpf/verifier.c=20990=static int check_struct_ops_btf_id(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-21078-\t\t\thas_refcounted_arg = true;\nkernel/bpf/verifier.c:21079:\t\tif (base_type(info-\u003ereg_type) == PTR_TO_ARENA) {\nkernel/bpf/verifier.c-21080-\t\t\tif (!bpf_jit_supports_arena_args()) {\n"
} |
| 31/3 | 2026/10/03 02:38 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 57523 output: 37 thoughts: 32 Thoughts: Okay, here's my interpretation of that thought process, assuming I'm deeply familiar with kernel BPF verification: **Examining BPF Verifier Logic** Right, let's dive into the core of the BPF verifier. I need to take a look at the code responsible for the actual verification process. Specifically, I want to zero in on lines 7129-7135 within `kernel/bpf/verifier.c`. I'm going to use my `read-file` utility or whatever similar tool I have set up to do that. This should give me a focused view of the logic I'm interested in, without getting bogged down in the surrounding context unless I need it. I'm probably looking for something specific, a bug I'm trying to reproduce, a behavior I don't understand, or some optimization that I'm implementing. Let's get to it. |
| 32/3 | 2026/10/03 02:38 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/verifier.c",
"FirstLine": 7125,
"LineCount": 20
}Results: {
"Contents": "7125:\t\t\terr = check_buffer_access(env, reg, argno, off, size, false,\n7126:\t\t\t\t\t\t max_access);\n7127:\t\n7128:\t\t\tif (!err \u0026\u0026 value_regno \u003e= 0 \u0026\u0026 (rdonly_mem || t == BPF_READ))\n7129:\t\t\t\tmark_reg_unknown(env, regs, value_regno);\n7130:\t\t} else if (reg-\u003etype == PTR_TO_ARENA) {\n7131:\t\t\tif (t == BPF_READ \u0026\u0026 value_regno \u003e= 0)\n7132:\t\t\t\tmark_reg_unknown(env, regs, value_regno);\n7133:\t\t} else {\n7134:\t\t\tenum bpf_diag_invalid_deref_kind kind = BPF_DIAG_DEREF_INVALID_PTR;\n7135:\t\n7136:\t\t\tverbose(env, \"%s invalid mem access '%s'\\n\", reg_arg_name(env, argno),\n7137:\t\t\t\treg_type_str(env, reg-\u003etype));\n7138:\t\t\tif (reg-\u003etype == SCALAR_VALUE)\n7139:\t\t\t\tkind = BPF_DIAG_DEREF_SCALAR;\n7140:\t\t\telse if (type_may_be_null(reg-\u003etype))\n7141:\t\t\t\tkind = BPF_DIAG_DEREF_NULLABLE_PTR;\n7142:\t\t\tbpf_diag_invalid_deref(env, insn_idx, reg_from_argno(argno),\n7143:\t\t\t\t\t reg_arg_name(env, argno), reg, kind, 0);\n7144:\t\t\treturn -EACCES;\n"
} |
| 33/3 | 2026/10/03 02:38 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 58231 output: 31 thoughts: 1889 Thoughts: **Analyzing BPF Verifier and BTF Interactions** Okay, so I'm digging into how the BPF verifier handles memory access and BTF (BPF Type Format) information. The first thing that caught my eye was how `PTR_TO_ARENA` is handled in `check_mem_access`. It looks like when `mem_access_reg` returns `arena` (a state with `arena.type = PTR_TO_ARENA`), and if it's a read operation, `check_mem_access` flags the destination register as unknown. I wanted to trace where `mem_access_reg` is called, so I looked at the call sites: `check_load_mem`, `check_store_reg`, `check_atomic_rmw`, and `do_check_insn`. In each case, `mem_access_reg` gets the register state (potentially populating it with `PTR_TO_ARENA`), and then `check_mem_access` is invoked. It's important that `memset` is always done to zero out the memory, and set the correct arena type. Next, I shifted focus to `btf.c` and encountered the `btf_name_valid_identifier` function. Originally, I thought there might be a potential for uninitialized memory issues with the `src` pointer, but it's used with `btf_str_by_offset`, which does string bound checking, so that's okay. I was more concerned about `btf_datasec_resolve`, specifically the check `if (vsi->size < type_size)`. If the variable size is less than the type size, it's an invalid size. It's the `vsi->size` and `vsi->offset` values that are used for, for example, showing data sections. I need to figure out where those values are used after this check, especially concerning map initialization or read operations. I'm going to search for usages of datasec resolution and those `vsi->` members to understand the potential impact of that size check failing, and make sure that this isn't opening a vulnerability. |
| 34/3 | 2026/10/03 02:38 | tool | grepper |
0mArgs:{
"Expression": "btf_datasec",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/btf.c=5309=static const struct btf_kind_operations var_ops = {\n--\nkernel/bpf/btf.c-5317-\nkernel/bpf/btf.c:5318:static s32 btf_datasec_check_meta(struct btf_verifier_env *env,\nkernel/bpf/btf.c-5319-\t\t\t\t const struct btf_type *t,\n--\nkernel/bpf/btf.c-5390-\nkernel/bpf/btf.c:5391:static int btf_datasec_resolve(struct btf_verifier_env *env,\nkernel/bpf/btf.c-5392-\t\t\t const struct resolve_vertex *v)\n--\nkernel/bpf/btf.c-5430-\nkernel/bpf/btf.c:5431:static void btf_datasec_log(struct btf_verifier_env *env,\nkernel/bpf/btf.c-5432-\t\t\t const struct btf_type *t)\n--\nkernel/bpf/btf.c=5438=static bool btf_var_is_piece(const struct btf *btf, const struct btf_type *var,\n--\nkernel/bpf/btf.c-5446-\nkernel/bpf/btf.c:5447:static void btf_datasec_show(const struct btf *btf,\nkernel/bpf/btf.c-5448-\t\t\t const struct btf_type *t, u32 type_id,\n--\nkernel/bpf/btf.c=5476=static const struct btf_kind_operations datasec_ops = {\nkernel/bpf/btf.c:5477:\t.check_meta\t\t= btf_datasec_check_meta,\nkernel/bpf/btf.c:5478:\t.resolve\t\t= btf_datasec_resolve,\nkernel/bpf/btf.c-5479-\t.check_member\t\t= btf_df_check_member,\nkernel/bpf/btf.c-5480-\t.check_kflag_member\t= btf_df_check_kflag_member,\nkernel/bpf/btf.c:5481:\t.log_details\t\t= btf_datasec_log,\nkernel/bpf/btf.c:5482:\t.show\t\t\t= btf_datasec_show,\nkernel/bpf/btf.c-5483-};\n"
} |
| 35/3 | 2026/10/03 02:38 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 61287 output: 38 thoughts: 49 Thoughts: Okay, let's dive into these functions, specifically focusing on how they handle `datasec` within the BPF context. I need to understand the internals of `btf_datasec_check_meta`, `btf_datasec_resolve`, and `btf_datasec_show`. They're all in `kernel/bpf/btf.c`, so I'll start by referencing those files. I need to see how the metadata associated with the datasec is being validated, which I assume `btf_datasec_check_meta` handles. Then, the process to "resolve" things within the datasec, which I'm sure involves linking symbols and types, is what I expect `btf_datasec_resolve` to be responsible for. And, finally, understanding how the information is actually displayed or made available will be important, so `btf_datasec_show` should be where that's coded. I want to build a clear understanding of the full lifecycle of `datasec` in BPF: validation, linking/resolution, and presentation. |
| 36/3 | 2026/10/03 02:38 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/btf.c",
"FirstLine": 5318,
"LineCount": 85
}Results: {
"Contents": "5318:\tstatic s32 btf_datasec_check_meta(struct btf_verifier_env *env,\n5319:\t\t\t\t\t const struct btf_type *t,\n5320:\t\t\t\t\t u32 meta_left)\n5321:\t{\n5322:\t\tconst struct btf_var_secinfo *vsi;\n5323:\t\tu64 last_vsi_end_off = 0, sum = 0;\n5324:\t\tu32 i, meta_needed;\n5325:\t\n5326:\t\tmeta_needed = btf_type_vlen(t) * sizeof(*vsi);\n5327:\t\tif (meta_left \u003c meta_needed) {\n5328:\t\t\tbtf_verifier_log_basic(env, t,\n5329:\t\t\t\t\t \"meta_left:%u meta_needed:%u\",\n5330:\t\t\t\t\t meta_left, meta_needed);\n5331:\t\t\treturn -EINVAL;\n5332:\t\t}\n5333:\t\n5334:\t\tif (!t-\u003esize) {\n5335:\t\t\tbtf_verifier_log_type(env, t, \"size == 0\");\n5336:\t\t\treturn -EINVAL;\n5337:\t\t}\n5338:\t\n5339:\t\tif (btf_type_kflag(t)) {\n5340:\t\t\tbtf_verifier_log_type(env, t, \"Invalid btf_info kind_flag\");\n5341:\t\t\treturn -EINVAL;\n5342:\t\t}\n5343:\t\n5344:\t\tif (!t-\u003ename_off ||\n5345:\t\t !btf_name_valid_section(env-\u003ebtf, t-\u003ename_off)) {\n5346:\t\t\tbtf_verifier_log_type(env, t, \"Invalid name\");\n5347:\t\t\treturn -EINVAL;\n5348:\t\t}\n5349:\t\n5350:\t\tbtf_verifier_log_type(env, t, NULL);\n5351:\t\n5352:\t\tfor_each_vsi(i, t, vsi) {\n5353:\t\t\t/* A var cannot be in type void */\n5354:\t\t\tif (!vsi-\u003etype || !BTF_TYPE_ID_VALID(vsi-\u003etype)) {\n5355:\t\t\t\tbtf_verifier_log_vsi(env, t, vsi,\n5356:\t\t\t\t\t\t \"Invalid type_id\");\n5357:\t\t\t\treturn -EINVAL;\n5358:\t\t\t}\n5359:\t\n5360:\t\t\tif (vsi-\u003eoffset \u003c last_vsi_end_off || vsi-\u003eoffset \u003e= t-\u003esize) {\n5361:\t\t\t\tbtf_verifier_log_vsi(env, t, vsi,\n5362:\t\t\t\t\t\t \"Invalid offset\");\n5363:\t\t\t\treturn -EINVAL;\n5364:\t\t\t}\n5365:\t\n5366:\t\t\tif (!vsi-\u003esize || vsi-\u003esize \u003e t-\u003esize) {\n5367:\t\t\t\tbtf_verifier_log_vsi(env, t, vsi,\n5368:\t\t\t\t\t\t \"Invalid size\");\n5369:\t\t\t\treturn -EINVAL;\n5370:\t\t\t}\n5371:\t\n5372:\t\t\tlast_vsi_end_off = vsi-\u003eoffset + vsi-\u003esize;\n5373:\t\t\tif (last_vsi_end_off \u003e t-\u003esize) {\n5374:\t\t\t\tbtf_verifier_log_vsi(env, t, vsi,\n5375:\t\t\t\t\t\t \"Invalid offset+size\");\n5376:\t\t\t\treturn -EINVAL;\n5377:\t\t\t}\n5378:\t\n5379:\t\t\tbtf_verifier_log_vsi(env, t, vsi, NULL);\n5380:\t\t\tsum += vsi-\u003esize;\n5381:\t\t}\n5382:\t\n5383:\t\tif (t-\u003esize \u003c sum) {\n5384:\t\t\tbtf_verifier_log_type(env, t, \"Invalid btf_info size\");\n5385:\t\t\treturn -EINVAL;\n5386:\t\t}\n5387:\t\n5388:\t\treturn meta_needed;\n5389:\t}\n5390:\t\n5391:\tstatic int btf_datasec_resolve(struct btf_verifier_env *env,\n5392:\t\t\t\t const struct resolve_vertex *v)\n5393:\t{\n5394:\t\tconst struct btf_var_secinfo *vsi;\n5395:\t\tstruct btf *btf = env-\u003ebtf;\n5396:\t\tu32 i;\n5397:\t\n5398:\t\tenv-\u003eresolve_mode = RESOLVE_TBD;\n5399:\t\tfor_each_vsi_from(i, v-\u003enext_member, v-\u003et, vsi) {\n5400:\t\t\tu32 var_type_id = vsi-\u003etype, type_id;\n5401:\t\t\tconst struct btf_type *var_type = btf_type_by_id(env-\u003ebtf,\n5402:\t\t\t\t\t\t\t\t\t var_type_id);\n"
} |
| 37/3 | 2026/10/03 02:38 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 62980 output: 38 thoughts: 29 Thoughts: Okay, here's my interpretation of what I'm "thinking" based on that directive, tailored for an expert audience: **Diving Deep into BTF: A Specific Inspection** Alright, I'm zeroing in on a particular area within the kernel. It seems the context warrants a close examination of `kernel/bpf/btf.c`. Given my expertise, I suspect this has something to do with the BPF type format, and I need to assess how it's handled. The directive is to focus precisely on lines 5430 to 5485. This suggests we're dealing with a specific function or code block within the BTF implementation. I'll need to dissect these lines to understand the nuances of the logic being implemented. I'll be looking for potential optimization opportunities, edge cases, and any potential issues related to memory management or type handling within that specific range. Likely, I will be scrutinizing the functions within to identify dependencies and their effect on the wider system. |
| 38/3 | 2026/10/03 02:39 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/btf.c",
"FirstLine": 5430,
"LineCount": 55
}Results: {
"Contents": "5430:\t\n5431:\tstatic void btf_datasec_log(struct btf_verifier_env *env,\n5432:\t\t\t\t const struct btf_type *t)\n5433:\t{\n5434:\t\tbtf_verifier_log(env, \"size=%u vlen=%u\", t-\u003esize, btf_type_vlen(t));\n5435:\t}\n5436:\t\n5437:\t/* A piece of a variable is smaller than its type, which is the one of the whole variable */\n5438:\tstatic bool btf_var_is_piece(const struct btf *btf, const struct btf_type *var,\n5439:\t\t\t\t const struct btf_var_secinfo *vsi)\n5440:\t{\n5441:\t\tu32 size;\n5442:\t\n5443:\t\treturn IS_ERR(btf_resolve_size(btf, btf_type_by_id(btf, var-\u003etype), \u0026size)) ||\n5444:\t\t vsi-\u003esize \u003c size;\n5445:\t}\n5446:\t\n5447:\tstatic void btf_datasec_show(const struct btf *btf,\n5448:\t\t\t\t const struct btf_type *t, u32 type_id,\n5449:\t\t\t\t void *data, u8 bits_offset,\n5450:\t\t\t\t struct btf_show *show)\n5451:\t{\n5452:\t\tconst struct btf_var_secinfo *vsi;\n5453:\t\tconst struct btf_type *var;\n5454:\t\tbool comma = false;\n5455:\t\tu32 i;\n5456:\t\n5457:\t\tif (!btf_show_start_type(show, t, type_id, data))\n5458:\t\t\treturn;\n5459:\t\n5460:\t\tbtf_show_type_value(show, \"section (\\\"%s\\\") = {\",\n5461:\t\t\t\t __btf_name_by_offset(btf, t-\u003ename_off));\n5462:\t\tfor_each_vsi(i, t, vsi) {\n5463:\t\t\tvar = btf_type_by_id(btf, vsi-\u003etype);\n5464:\t\t\t/* there is less data than the type takes */\n5465:\t\t\tif (btf_var_is_piece(btf, var, vsi))\n5466:\t\t\t\tcontinue;\n5467:\t\t\tif (comma)\n5468:\t\t\t\tbtf_show(show, \",\");\n5469:\t\t\tcomma = true;\n5470:\t\t\tbtf_type_ops(var)-\u003eshow(btf, var, vsi-\u003etype,\n5471:\t\t\t\t\t\tdata + vsi-\u003eoffset, bits_offset, show);\n5472:\t\t}\n5473:\t\tbtf_show_end_type(show);\n5474:\t}\n5475:\t\n5476:\tstatic const struct btf_kind_operations datasec_ops = {\n5477:\t\t.check_meta\t\t= btf_datasec_check_meta,\n5478:\t\t.resolve\t\t= btf_datasec_resolve,\n5479:\t\t.check_member\t\t= btf_df_check_member,\n5480:\t\t.check_kflag_member\t= btf_df_check_kflag_member,\n5481:\t\t.log_details\t\t= btf_datasec_log,\n5482:\t\t.show\t\t\t= btf_datasec_show,\n5483:\t};\n5484:\t\n"
} |
| 39/3 | 2026/10/03 02:39 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 64228 output: 35 thoughts: 285 Thoughts: **Analyzing the BTF Datasec Change**
Okay, let's dive into this `btf_datasec_show` function. I see a subtle but potentially significant change. Previously, I recall `btf_datasec_resolve` would reject instances where `vsi->size` was less than `type_size`. Now, that check is gone. Hmm.
And now, when `btf_datasec_show` is executed, there's a key check:
```c
if (btf_var_is_piece(btf, var, vsi))
continue;
```
Right, if this is a "piece," meaning `vsi->size` is indeed less than the expected `size`, we *continue*, skipping the call to `btf_type_ops(var)->show(...)`. Good, that prevents a potential read out of bounds because it avoids showing a piece that is smaller than its type size. It would skip showing that smaller piece.
But wait a minute, what if `btf_var_is_piece` returns false? Then `vsi->size` is greater than or equal to `size`. That means `data + vsi->offset` will have at least `size` bytes to show the full type. And the check `vsi->offset + vsi->size <= t->size` in `btf_datasec_check_meta` ensures we are within the overall datasec bounds. Okay, that feels right. The check seems pretty robust, as long as `btf_datasec_check_meta` is not bypassed or modified in some other way.
However, I'm still feeling a bit cautious. Are there other places using this datasec structure? Let's check where `vsi` or `datasec` is used within the kernel code just to be certain. A broader view of the dependencies is always a good idea in these cases.
|
| 40/3 | 2026/10/03 02:39 | tool | grepper |
0mArgs:{
"Expression": "btf_type_is_datasec",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/btf.c=509=bool btf_type_is_void(const struct btf_type *t)\n--\nkernel/bpf/btf.c-513-\nkernel/bpf/btf.c:514:static bool btf_type_is_datasec(const struct btf_type *t)\nkernel/bpf/btf.c-515-{\n--\nkernel/bpf/btf.c=787=static bool btf_type_is_resolve_source_only(const struct btf_type *t)\n--\nkernel/bpf/btf.c-790-\t btf_type_is_decl_tag(t) ||\nkernel/bpf/btf.c:791:\t btf_type_is_datasec(t) ||\nkernel/bpf/btf.c-792-\t btf_type_is_loc_proto(t) ||\n--\nkernel/bpf/btf.c-803- * btf_type_is_var() because the variable refers to\nkernel/bpf/btf.c:804: * another type. btf_type_is_datasec() holds multiple\nkernel/bpf/btf.c-805- * btf_type_is_var() types that need resolving.\n--\nkernel/bpf/btf.c=812=static bool btf_type_needs_resolve(const struct btf_type *t)\n--\nkernel/bpf/btf.c-820-\t btf_type_is_decl_tag(t) ||\nkernel/bpf/btf.c:821:\t btf_type_is_datasec(t) ||\nkernel/bpf/btf.c-822-\t btf_type_is_loc_proto(t) ||\n--\nkernel/bpf/btf.c=3993=static int btf_find_field(const struct btf *btf, const struct btf_type *t,\n--\nkernel/bpf/btf.c-4000-\t\treturn btf_find_struct_field(btf, t, field_mask, info, info_cnt, 0, \u0026seen_mask);\nkernel/bpf/btf.c:4001:\telse if (btf_type_is_datasec(t))\nkernel/bpf/btf.c-4002-\t\treturn btf_find_datasec_var(btf, t, field_mask, info, info_cnt, 0, \u0026seen_mask);\n--\nkernel/bpf/btf.c=5874=static bool btf_resolve_valid(struct btf_verifier_env *env,\n--\nkernel/bpf/btf.c-5882-\nkernel/bpf/btf.c:5883:\tif (btf_type_is_struct(t) || btf_type_is_datasec(t) ||\nkernel/bpf/btf.c-5884-\t btf_type_is_loc_proto(t) || btf_type_is_locsec(t))\n--\nkernel/bpf/btf.c-5897-\t\t !btf_type_is_var(t) \u0026\u0026\nkernel/bpf/btf.c:5898:\t\t !btf_type_is_datasec(t);\nkernel/bpf/btf.c-5899-\t}\n"
} |
| 41/3 | 2026/10/03 02:39 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 65852 output: 38 thoughts: 45 Thoughts: Okay, let's dive into this. **Investigating `btf_find_datasec_var`** Alright, I'm going to take a look at `btf_find_datasec_var`. I need to understand its functionality within the BPF system. Time to pull up `kernel/bpf/btf.c` and zero in on that area. I'll start around line 4001, see what the surrounding code context tells me, and trace the calls to get a complete picture of its purpose. |
| 42/3 | 2026/10/03 02:39 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/btf.c",
"FirstLine": 3960,
"LineCount": 45
}Results: {
"Contents": "3960:\t\t\t\t\t\t field_mask, seen_mask,\n3961:\t\t\t\t\t\t \u0026info[idx], info_cnt - idx, level);\n3962:\t\t\tif (ret \u003c 0)\n3963:\t\t\t\treturn ret;\n3964:\t\t\tidx += ret;\n3965:\t\t}\n3966:\t\treturn idx;\n3967:\t}\n3968:\t\n3969:\tstatic int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t,\n3970:\t\t\t\t\tu32 field_mask, struct btf_field_info *info,\n3971:\t\t\t\t\tint info_cnt, u32 level, u32 *seen_mask)\n3972:\t{\n3973:\t\tint ret, idx = 0;\n3974:\t\tconst struct btf_var_secinfo *vsi;\n3975:\t\tu32 i, off;\n3976:\t\n3977:\t\tfor_each_vsi(i, t, vsi) {\n3978:\t\t\tconst struct btf_type *var = btf_type_by_id(btf, vsi-\u003etype);\n3979:\t\t\tconst struct btf_type *var_type = btf_type_by_id(btf, var-\u003etype);\n3980:\t\n3981:\t\t\toff = vsi-\u003eoffset;\n3982:\t\t\tret = btf_find_field_one(btf, var, var_type, -1, off, vsi-\u003esize,\n3983:\t\t\t\t\t\t field_mask, seen_mask,\n3984:\t\t\t\t\t\t \u0026info[idx], info_cnt - idx,\n3985:\t\t\t\t\t\t level);\n3986:\t\t\tif (ret \u003c 0)\n3987:\t\t\t\treturn ret;\n3988:\t\t\tidx += ret;\n3989:\t\t}\n3990:\t\treturn idx;\n3991:\t}\n3992:\t\n3993:\tstatic int btf_find_field(const struct btf *btf, const struct btf_type *t,\n3994:\t\t\t\t u32 field_mask, struct btf_field_info *info,\n3995:\t\t\t\t int info_cnt)\n3996:\t{\n3997:\t\tu32 seen_mask = 0;\n3998:\t\n3999:\t\tif (__btf_type_is_struct(t))\n4000:\t\t\treturn btf_find_struct_field(btf, t, field_mask, info, info_cnt, 0, \u0026seen_mask);\n4001:\t\telse if (btf_type_is_datasec(t))\n4002:\t\t\treturn btf_find_datasec_var(btf, t, field_mask, info, info_cnt, 0, \u0026seen_mask);\n4003:\t\treturn -EINVAL;\n4004:\t}\n"
} |
| 43/3 | 2026/10/03 02:39 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 66825 output: 37 thoughts: 108 Thoughts: Okay, here's what's going through my mind as I analyze this code snippet, keeping in mind my deep understanding of BTF and data structure inspection: **Dissecting `btf_find_datasec_var` and `btf_find_field_one`** Alright, I'm looking at `btf_find_datasec_var` and specifically, that call to `btf_find_field_one`. I immediately zero in on the arguments being passed, particularly how `vsi->size` is being handled. I need to understand how the size of a variable, represented by `vsi->size`, factors into the field lookup. It's passed as the `sz` argument to `btf_find_field_one`. I'm thinking, "Okay, this size information is clearly critical. It's going to inform how the function determines the boundaries or extent of the field being searched for within the broader data structure represented by the BTF data." What's the function *doing* with this size information? Is it validating offsets? Is it helping to handle variable-length arrays or bitfields? I need to dig deeper. |
| 44/3 | 2026/10/03 02:39 | tool | grepper |
0mArgs:{
"Expression": "btf_find_field_one",
"PathPrefix": "kernel/bpf/btf.c"
}Results: {
"Output": "kernel/bpf/btf.c=3808=static int btf_find_nested_struct(const struct btf *btf, const struct btf_type *t,\n--\nkernel/bpf/btf.c-3840-\nkernel/bpf/btf.c:3841:static int btf_find_field_one(const struct btf *btf,\nkernel/bpf/btf.c-3842-\t\t\t const struct btf_type *var,\n--\nkernel/bpf/btf.c=3939=static int btf_find_struct_field(const struct btf *btf,\n--\nkernel/bpf/btf.c-3957-\nkernel/bpf/btf.c:3958:\t\tret = btf_find_field_one(btf, t, member_type, i,\nkernel/bpf/btf.c-3959-\t\t\t\t\t off, 0,\n--\nkernel/bpf/btf.c=3969=static int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t,\n--\nkernel/bpf/btf.c-3981-\t\toff = vsi-\u003eoffset;\nkernel/bpf/btf.c:3982:\t\tret = btf_find_field_one(btf, var, var_type, -1, off, vsi-\u003esize,\nkernel/bpf/btf.c-3983-\t\t\t\t\t field_mask, seen_mask,\n"
} |
| 45/3 | 2026/10/03 02:39 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 67619 output: 38 thoughts: 14 Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and written in the first person: **Diving into `btf_find_field_one`** Alright, let's get down to brass tacks. I need to understand this `btf_find_field_one` function. Given my level of expertise, I'm assuming this is related to BTF (BPF Type Format) metadata, and likely involves some form of field lookup within a complex type definition. Is it traversing a struct or union? Searching for a specific field by name, offset, or perhaps even both? The name "one" intrigues me... is this the *only* field it's designed to find, or is it merely the *first* matching field? The distinction is critical for how I subsequently use the returned value. My mental model here involves pointers, offsets, and potentially bitfield calculations if we're dealing with packed structures. I'll need to look closely at its return value – is it a pointer, an index, an error code, or a boolean indicating success? Furthermore, I'll be watching how it handles type mismatches or the absence of the target field; proper error handling here is of utmost importance for robustness in production BPF code. Time to crack open the source and see what the authors were thinking. |
| 46/3 | 2026/10/03 02:39 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/btf.c",
"FirstLine": 3840,
"LineCount": 55
}Results: {
"Contents": "3840:\t\n3841:\tstatic int btf_find_field_one(const struct btf *btf,\n3842:\t\t\t\t const struct btf_type *var,\n3843:\t\t\t\t const struct btf_type *var_type,\n3844:\t\t\t\t int var_idx,\n3845:\t\t\t\t u32 off, u32 expected_size,\n3846:\t\t\t\t u32 field_mask, u32 *seen_mask,\n3847:\t\t\t\t struct btf_field_info *info, int info_cnt,\n3848:\t\t\t\t u32 level)\n3849:\t{\n3850:\t\tint ret, align, sz, field_type;\n3851:\t\tstruct btf_field_info tmp;\n3852:\t\tconst struct btf_array *array;\n3853:\t\tu32 i, nelems = 1;\n3854:\t\n3855:\t\t/* Walk into array types to find the element type and the number of\n3856:\t\t * elements in the (flattened) array.\n3857:\t\t */\n3858:\t\tfor (i = 0; i \u003c MAX_RESOLVE_DEPTH \u0026\u0026 btf_type_is_array(var_type); i++) {\n3859:\t\t\tarray = btf_array(var_type);\n3860:\t\t\tnelems *= array-\u003enelems;\n3861:\t\t\tvar_type = btf_type_by_id(btf, array-\u003etype);\n3862:\t\t}\n3863:\t\tif (i == MAX_RESOLVE_DEPTH)\n3864:\t\t\treturn -E2BIG;\n3865:\t\tif (nelems == 0)\n3866:\t\t\treturn 0;\n3867:\t\n3868:\t\tfield_type = btf_get_field_type(btf, var_type,\n3869:\t\t\t\t\t\tfield_mask, seen_mask, \u0026align, \u0026sz);\n3870:\t\t/* Look into variables of struct types */\n3871:\t\tif (!field_type \u0026\u0026 __btf_type_is_struct(var_type)) {\n3872:\t\t\tsz = var_type-\u003esize;\n3873:\t\t\tif (expected_size \u0026\u0026 expected_size != sz * nelems)\n3874:\t\t\t\treturn 0;\n3875:\t\t\tret = btf_find_nested_struct(btf, var_type, off, nelems, field_mask,\n3876:\t\t\t\t\t\t \u0026info[0], info_cnt, level, seen_mask);\n3877:\t\t\treturn ret;\n3878:\t\t}\n3879:\t\n3880:\t\tif (field_type == 0)\n3881:\t\t\treturn 0;\n3882:\t\tif (field_type \u003c 0)\n3883:\t\t\treturn field_type;\n3884:\t\n3885:\t\tif (expected_size \u0026\u0026 expected_size != sz * nelems)\n3886:\t\t\treturn 0;\n3887:\t\tif (off % align)\n3888:\t\t\treturn 0;\n3889:\t\n3890:\t\tswitch (field_type) {\n3891:\t\tcase BPF_SPIN_LOCK:\n3892:\t\tcase BPF_RES_SPIN_LOCK:\n3893:\t\tcase BPF_TIMER:\n3894:\t\tcase BPF_WORKQUEUE:\n"
} |
| 47/3 | 2026/10/03 02:39 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 68851 output: 35 thoughts: 255 Thoughts: **My Analysis of BTF Code** Okay, so I'm diving into this BTF code, and the first thing that jumps out at me is this check in `btf_find_field_one`. It's a quick size validation, and it's pretty restrictive. If the `expected_size`, which is essentially the declared size in the BTF data, *doesn't* match the calculated size (`sz * nelems`) of the entire struct or type, it just returns 0. That's a pretty aggressive short-circuit. Immediately, I'm thinking, "Well, that's going to trip up on anything where size isn't a perfect match." Special fields, like spinlocks, timers, or perhaps things with padding or alignment, could easily get missed. It seems like any deviations from the size calculation will result in failure here, which isn't ideal for complete BTF parsing. Now, let's move on to the `btf_func_check` function in `kernel/bpf/btf.c`. Right off the bat, I see a new check for static functions: ```c + /* Rust leaves out names of some arguments of static functions */ + if (btf_func_linkage(t) == BTF_FUNC_STATIC) + return 0; ``` It looks like they are specifically addressing a behavior in Rust where some arguments of static functions might have omitted names. The comment itself is the giveaway. If the function linkage is static, it just returns 0. I suspect this is a workaround or an intentional design choice to deal with a specific case where the function signatures might be incomplete or differently encoded in the BTF data generated by the Rust compiler. Now I'll proceed to examine what it does next. |
| 48/3 | 2026/10/03 02:39 | tool | grepper |
0mArgs:{
"Expression": "btf_func_check",
"PathPrefix": "kernel/bpf/btf.c"
}Results: {
"Output": "kernel/bpf/btf.c=474=static int btf_resolve(struct btf_verifier_env *env,\n--\nkernel/bpf/btf.c-476-\nkernel/bpf/btf.c:477:static int btf_func_check(struct btf_verifier_env *env,\nkernel/bpf/btf.c-478-\t\t\t const struct btf_type *t);\n--\nkernel/bpf/btf.c=5187=static const struct btf_kind_operations func_proto_ops = {\n--\nkernel/bpf/btf.c-5196-\t *\nkernel/bpf/btf.c:5197:\t * Hence, there is no btf_func_check_member().\nkernel/bpf/btf.c-5198-\t */\n--\nkernel/bpf/btf.c-5204-\nkernel/bpf/btf.c:5205:static s32 btf_func_check_meta(struct btf_verifier_env *env,\nkernel/bpf/btf.c-5206-\t\t\t const struct btf_type *t,\n--\nkernel/bpf/btf.c=5230=static int btf_func_resolve(struct btf_verifier_env *env,\n--\nkernel/bpf/btf.c-5236-\nkernel/bpf/btf.c:5237:\terr = btf_func_check(env, t);\nkernel/bpf/btf.c-5238-\tif (err)\n--\nkernel/bpf/btf.c=5245=static const struct btf_kind_operations func_ops = {\nkernel/bpf/btf.c:5246:\t.check_meta = btf_func_check_meta,\nkernel/bpf/btf.c-5247-\t.resolve = btf_func_resolve,\n--\nkernel/bpf/btf.c=5659=static int btf_func_proto_check(struct btf_verifier_env *env,\n--\nkernel/bpf/btf.c-5753-\nkernel/bpf/btf.c:5754:static int btf_func_check(struct btf_verifier_env *env,\nkernel/bpf/btf.c-5755-\t\t\t const struct btf_type *t)\n"
} |
| 49/3 | 2026/10/03 02:39 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 70147 output: 38 thoughts: 19 Thoughts: Here's my take on the thought process, summarized as you requested: **Navigating the BTF Function Check** Alright, let's dive in. I need to understand what's happening with `btf_func_check`. Specifically, I'm going to start by examining the code around line 5754. My initial focus will be on that specific region; it's where the critical logic related to that function check is likely situated. I'll need to see how it's implemented and what factors are being considered. Let's see what secrets this section of code holds. |
| 50/3 | 2026/10/03 02:39 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/btf.c",
"FirstLine": 5754,
"LineCount": 45
}Results: {
"Contents": "5754:\tstatic int btf_func_check(struct btf_verifier_env *env,\n5755:\t\t\t\t const struct btf_type *t)\n5756:\t{\n5757:\t\tconst struct btf_type *proto_type;\n5758:\t\tconst struct btf_param *args;\n5759:\t\tconst struct btf *btf;\n5760:\t\tu32 nr_args, i;\n5761:\t\n5762:\t\tbtf = env-\u003ebtf;\n5763:\t\tproto_type = btf_type_by_id(btf, t-\u003etype);\n5764:\t\n5765:\t\tif (!proto_type || !btf_type_is_func_proto(proto_type)) {\n5766:\t\t\tbtf_verifier_log_type(env, t, \"Invalid type_id\");\n5767:\t\t\treturn -EINVAL;\n5768:\t\t}\n5769:\t\n5770:\t\t/* Rust leaves out names of some arguments of static functions */\n5771:\t\tif (btf_func_linkage(t) == BTF_FUNC_STATIC)\n5772:\t\t\treturn 0;\n5773:\t\n5774:\t\targs = (const struct btf_param *)(proto_type + 1);\n5775:\t\tnr_args = btf_type_vlen(proto_type);\n5776:\t\tfor (i = 0; i \u003c nr_args; i++) {\n5777:\t\t\tif (!args[i].name_off \u0026\u0026 args[i].type) {\n5778:\t\t\t\tbtf_verifier_log_type(env, t, \"Invalid arg#%u\", i + 1);\n5779:\t\t\t\treturn -EINVAL;\n5780:\t\t\t}\n5781:\t\t}\n5782:\t\n5783:\t\treturn 0;\n5784:\t}\n5785:\t\n5786:\tstatic const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = {\n5787:\t\t[BTF_KIND_INT] = \u0026int_ops,\n5788:\t\t[BTF_KIND_PTR] = \u0026ptr_ops,\n5789:\t\t[BTF_KIND_ARRAY] = \u0026array_ops,\n5790:\t\t[BTF_KIND_STRUCT] = \u0026struct_ops,\n5791:\t\t[BTF_KIND_UNION] = \u0026struct_ops,\n5792:\t\t[BTF_KIND_ENUM] = \u0026enum_ops,\n5793:\t\t[BTF_KIND_FWD] = \u0026fwd_ops,\n5794:\t\t[BTF_KIND_TYPEDEF] = \u0026modifier_ops,\n5795:\t\t[BTF_KIND_VOLATILE] = \u0026modifier_ops,\n5796:\t\t[BTF_KIND_CONST] = \u0026modifier_ops,\n5797:\t\t[BTF_KIND_RESTRICT] = \u0026modifier_ops,\n5798:\t\t[BTF_KIND_FUNC] = \u0026func_ops,\n"
} |
| 51/3 | 2026/10/03 02:39 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 71154 output: 36 thoughts: 246 Thoughts: **My Analysis of a Potential Bug in BPF Linkage** Okay, so looking at this code, it seems like the current implementation just bypasses a crucial check – verifying that every argument has a non-zero `name_off` when the linkage is `BTF_FUNC_STATIC`. That's a potential vulnerability. It's a classic case where a missing validation could lead to unforeseen issues down the line. Now, let's dive into `kernel/bpf/core.c` and see what's happening within `bpf_prog_ksym_set_name`. ```c @@ -589,6 +590,10 @@ bpf_prog_ksym_set_name(struct bpf_prog *prog) prog->aux->func_info[prog->aux->func_idx].type_id); func_name = btf_name_by_offset(prog->aux->btf, type->name_off); snprintf(sym, (size_t)(end - sym), "_%s", func_name); + /* the name of a function of Rust is not an identifier */ + for (; *sym; sym++) + if (!isalnum(*sym) && *sym != '_' && *sym != '.') + *sym = '_'; return; } ``` Ah, I see. It's in this `bpf_prog_ksym_set_name` function that we're dealing with constructing the symbol name. We use `btf_name_by_offset` to get the function name and then do a `snprintf` to create the symbolic name. It also seems like there's a character replacement loop here to handle scenarios where the function name from Rust may not be a valid C identifier. I need to confirm the linkage context in `bpf_prog_ksym_set_name` and assess the impact of this potentially missing check. This could be a race condition, or a way to bypass security measures. Time to dig deeper. |
| 52/3 | 2026/10/03 02:39 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/core.c",
"FirstLine": 575,
"LineCount": 35
}Results: {
"Contents": " 575:\t\t\t /* name has been null terminated.\n 576:\t\t\t * We should need +1 for the '_' preceding\n 577:\t\t\t * the name. However, the null character\n 578:\t\t\t * is double counted between the name and the\n 579:\t\t\t * sizeof(\"bpf_prog_\") above, so we omit\n 580:\t\t\t * the +1 here.\n 581:\t\t\t */\n 582:\t\t\t sizeof(prog-\u003eaux-\u003ename) \u003e KSYM_NAME_LEN);\n 583:\t\n 584:\t\tsym += snprintf(sym, KSYM_NAME_LEN, \"bpf_prog_\");\n 585:\t\tsym = bin2hex(sym, prog-\u003etag, sizeof(prog-\u003etag));\n 586:\t\n 587:\t\t/* prog-\u003eaux-\u003ename will be ignored if full btf name is available */\n 588:\t\tif (prog-\u003eaux-\u003efunc_info_cnt \u0026\u0026 prog-\u003eaux-\u003efunc_idx \u003c prog-\u003eaux-\u003efunc_info_cnt) {\n 589:\t\t\ttype = btf_type_by_id(prog-\u003eaux-\u003ebtf,\n 590:\t\t\t\t\t prog-\u003eaux-\u003efunc_info[prog-\u003eaux-\u003efunc_idx].type_id);\n 591:\t\t\tfunc_name = btf_name_by_offset(prog-\u003eaux-\u003ebtf, type-\u003ename_off);\n 592:\t\t\tsnprintf(sym, (size_t)(end - sym), \"_%s\", func_name);\n 593:\t\t\t/* the name of a function of Rust is not an identifier */\n 594:\t\t\tfor (; *sym; sym++)\n 595:\t\t\t\tif (!isalnum(*sym) \u0026\u0026 *sym != '_' \u0026\u0026 *sym != '.')\n 596:\t\t\t\t\t*sym = '_';\n 597:\t\t\treturn;\n 598:\t\t}\n 599:\t\n 600:\t\tif (prog-\u003eaux-\u003ename[0])\n 601:\t\t\tsnprintf(sym, (size_t)(end - sym), \"_%s\", prog-\u003eaux-\u003ename);\n 602:\t\telse\n 603:\t\t\t*sym = 0;\n 604:\t}\n 605:\t\n 606:\tstatic unsigned long bpf_get_ksym_start(struct latch_tree_node *n)\n 607:\t{\n 608:\t\treturn container_of(n, struct bpf_ksym, tnode)-\u003estart;\n 609:\t}\n"
} |
| 53/3 | 2026/10/03 02:39 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 72536 output: 33 thoughts: 441 Thoughts: **Analyzing Potential BPF Vulnerability**
Okay, so I'm thinking about this code and how it relates to potential vulnerabilities. The `snprintf` function is nice because it guarantees null termination, and that `for` loop, `for (; *sym; sym++)`, that's a good way to sanitize the characters. No obvious uninitialized memory reads or writes there, that's good.
Now, let's look at `kernel/bpf/fixups.c`. I see this `arena_scalar_access` function:
```c
+static int arena_scalar_access(const struct bpf_insn *insn, struct bpf_insn *buf)
+{
+ bool load = BPF_CLASS(insn->code) == BPF_LDX || bpf_atomic_is_load_acq(insn);
+ struct bpf_insn *patch = buf;
+
+ *patch++ = BPF_MOV32_REG(BPF_REG_AX, load ? insn->src_reg : insn->dst_reg);
+ *patch = *insn;
+ if (load)
+ patch->src_reg = BPF_REG_AX;
+ else
+ patch->dst_reg = BPF_REG_AX;
+ patch++;
+ return patch - buf;
+}
```
Okay, it looks like it's constructing some new instructions. And then, there's the call to this function in `bpf_do_misc_fixups`:
```c
if (env->insn_aux_data[i + delta].arena_scalar) {
cnt = arena_scalar_access(insn, insn_buf);
new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
if (!new_prog)
return -ENOMEM;
delta += cnt - 1;
prog = new_prog;
env->prog = prog;
insn = prog->insnsi + i + delta;
goto next_insn;
}
```
Wait a second...I'm getting a little uneasy here. The crucial question is, how big is `insn_buf`? I need to know the size of that buffer. Without that information, I can't be certain whether there's potential for a buffer overflow during that `arena_scalar_access` call. I need to trace back and find the definition and allocation of `insn_buf` in `bpf_do_misc_fixups`.
|
| 54/3 | 2026/10/03 02:39 | tool | grepper |
0mArgs:{
"Expression": "insn_buf",
"PathPrefix": "kernel/bpf/fixups.c"
}Results: {
"Output": "\nFull output is too long, showing 200 out of 442 lines.\nUse more precise expression if possible.\n\nkernel/bpf/fixups.c=802=int bpf_opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env,\n--\nkernel/bpf/fixups.c-805-\tstruct bpf_insn *patch;\nkernel/bpf/fixups.c:806:\t/* use env-\u003einsn_buf as two independent buffers */\nkernel/bpf/fixups.c:807:\tstruct bpf_insn *zext_patch = env-\u003einsn_buf;\nkernel/bpf/fixups.c:808:\tstruct bpf_insn *rnd_hi32_patch = \u0026env-\u003einsn_buf[2];\nkernel/bpf/fixups.c-809-\tstruct bpf_insn_aux_data *aux = env-\u003einsn_aux_data;\n--\nkernel/bpf/fixups.c=904=int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)\n--\nkernel/bpf/fixups.c-910-\tstruct bpf_insn *epilogue_buf = env-\u003eepilogue_buf;\nkernel/bpf/fixups.c:911:\tstruct bpf_insn *insn_buf = env-\u003einsn_buf;\nkernel/bpf/fixups.c-912-\tstruct bpf_insn *insn;\n--\nkernel/bpf/fixups.c-928-\t\t\tsubprogs[0].stack_depth += 8;\nkernel/bpf/fixups.c:929:\t\t\tinsn_buf[cnt++] = BPF_STX_MEM(BPF_DW, BPF_REG_FP, BPF_REG_1,\nkernel/bpf/fixups.c-930-\t\t\t\t\t\t -subprogs[0].stack_depth);\nkernel/bpf/fixups.c:931:\t\t\tinsn_buf[cnt++] = env-\u003eprog-\u003einsnsi[0];\nkernel/bpf/fixups.c:932:\t\t\tnew_prog = __bpf_patch_insn_data(env, 0, insn_buf, cnt,\nkernel/bpf/fixups.c-933-\t\t\t\t\t\t\t BPF_PATCH_MOVE_TARGET);\n--\nkernel/bpf/fixups.c-949-\t\t}\nkernel/bpf/fixups.c:950:\t\tcnt = ops-\u003egen_prologue(insn_buf, env-\u003eseen_direct_write,\nkernel/bpf/fixups.c-951-\t\t\t\t\tenv-\u003eprog);\n--\nkernel/bpf/fixups.c-955-\t\t} else if (cnt) {\nkernel/bpf/fixups.c:956:\t\t\tnew_prog = __bpf_patch_insn_data(env, 0, insn_buf, cnt,\nkernel/bpf/fixups.c-957-\t\t\t\t\t\t\t BPF_PATCH_MOVE_TARGET);\n--\nkernel/bpf/fixups.c-963-\nkernel/bpf/fixups.c:964:\t\t\tret = add_kfunc_in_insns(env, insn_buf, cnt - 1);\nkernel/bpf/fixups.c-965-\t\t\tif (ret \u003c 0)\n--\nkernel/bpf/fixups.c-984-\t\t\tWARN_ON_ONCE(env-\u003einsn_aux_data[i + delta].alu_state);\nkernel/bpf/fixups.c:985:\t\t\tstruct bpf_insn *patch = insn_buf;\nkernel/bpf/fixups.c-986-\n--\nkernel/bpf/fixups.c-994-\t\t\t}\nkernel/bpf/fixups.c:995:\t\t\tcnt = patch - insn_buf;\nkernel/bpf/fixups.c:996:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-997-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-1041-\t\t\t\t/* jump back to the earlier generated epilogue */\nkernel/bpf/fixups.c:1042:\t\t\t\tinsn_buf[0] = BPF_JMP32_A(epilogue_idx - i - delta - 1);\nkernel/bpf/fixups.c-1043-\t\t\t\tcnt = 1;\nkernel/bpf/fixups.c-1044-\t\t\t} else {\nkernel/bpf/fixups.c:1045:\t\t\t\tmemcpy(insn_buf, epilogue_buf,\nkernel/bpf/fixups.c-1046-\t\t\t\t epilogue_cnt * sizeof(*epilogue_buf));\n--\nkernel/bpf/fixups.c-1053-\t\t\t}\nkernel/bpf/fixups.c:1054:\t\t\tgoto patch_insn_buf;\nkernel/bpf/fixups.c-1055-\t\t} else {\n--\nkernel/bpf/fixups.c-1063-\t\t\t */\nkernel/bpf/fixups.c:1064:\t\t\tstruct bpf_insn *patch = insn_buf;\nkernel/bpf/fixups.c-1065-\n--\nkernel/bpf/fixups.c-1067-\t\t\t*patch++ = BPF_ST_NOSPEC();\nkernel/bpf/fixups.c:1068:\t\t\tcnt = patch - insn_buf;\nkernel/bpf/fixups.c:1069:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-1070-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-1165-\t\ttarget_size = 0;\nkernel/bpf/fixups.c:1166:\t\tcnt = convert_ctx_access(type, insn, insn_buf, env-\u003eprog,\nkernel/bpf/fixups.c-1167-\t\t\t\t\t \u0026target_size);\n--\nkernel/bpf/fixups.c-1182-\t\t\t\tif (shift)\nkernel/bpf/fixups.c:1183:\t\t\t\t\tinsn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH,\nkernel/bpf/fixups.c-1184-\t\t\t\t\t\t\t\t\tinsn-\u003edst_reg,\nkernel/bpf/fixups.c-1185-\t\t\t\t\t\t\t\t\tshift);\nkernel/bpf/fixups.c:1186:\t\t\t\tinsn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn-\u003edst_reg,\nkernel/bpf/fixups.c-1187-\t\t\t\t\t\t\t\t(1 \u003c\u003c size * 8) - 1);\n--\nkernel/bpf/fixups.c-1189-\t\t\t\tif (shift)\nkernel/bpf/fixups.c:1190:\t\t\t\t\tinsn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH,\nkernel/bpf/fixups.c-1191-\t\t\t\t\t\t\t\t\tinsn-\u003edst_reg,\nkernel/bpf/fixups.c-1192-\t\t\t\t\t\t\t\t\tshift);\nkernel/bpf/fixups.c:1193:\t\t\t\tinsn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn-\u003edst_reg,\nkernel/bpf/fixups.c-1194-\t\t\t\t\t\t\t\t(1ULL \u003c\u003c size * 8) - 1);\n--\nkernel/bpf/fixups.c-1197-\t\tif (mode == BPF_MEMSX)\nkernel/bpf/fixups.c:1198:\t\t\tinsn_buf[cnt++] = BPF_RAW_INSN(BPF_ALU64 | BPF_MOV | BPF_X,\nkernel/bpf/fixups.c-1199-\t\t\t\t\t\t insn-\u003edst_reg, insn-\u003edst_reg,\n--\nkernel/bpf/fixups.c-1201-\nkernel/bpf/fixups.c:1202:patch_insn_buf:\nkernel/bpf/fixups.c:1203:\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-1204-\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c=1709=static int add_hidden_subprog(struct bpf_verifier_env *env, struct bpf_insn *patch, int len)\n--\nkernel/bpf/fixups.c-1741- */\nkernel/bpf/fixups.c:1742:static int may_goto_expand(struct bpf_insn *insn_buf, int off, int stack_off,\nkernel/bpf/fixups.c-1743-\t\t\t const struct bpf_insn *tail, int tail_cnt)\n--\nkernel/bpf/fixups.c-1749-\nkernel/bpf/fixups.c:1750:\tinsn_buf[cnt++] = BPF_LDX_MEM(BPF_DW, BPF_REG_AX, BPF_REG_10, stack_off);\nkernel/bpf/fixups.c-1751-\tif (off == (s16)off) {\nkernel/bpf/fixups.c:1752:\t\tinsn_buf[cnt++] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_AX, 0, off);\nkernel/bpf/fixups.c-1753-\t} else {\nkernel/bpf/fixups.c:1754:\t\tinsn_buf[cnt++] = BPF_JMP_IMM(BPF_JNE, BPF_REG_AX, 0, 1);\nkernel/bpf/fixups.c:1755:\t\tinsn_buf[cnt++] = BPF_JMP32_A(off \u003e= 0 ? off : off - 1);\nkernel/bpf/fixups.c-1756-\t}\nkernel/bpf/fixups.c:1757:\tmemcpy(insn_buf + cnt, tail, tail_cnt * sizeof(*tail));\nkernel/bpf/fixups.c-1758-\treturn cnt + tail_cnt;\n--\nkernel/bpf/fixups.c=1764=int bpf_do_misc_fixups(struct bpf_verifier_env *env)\n--\nkernel/bpf/fixups.c-1773-\tstruct bpf_insn_aux_data *aux;\nkernel/bpf/fixups.c:1774:\tstruct bpf_insn *insn_buf = env-\u003einsn_buf;\nkernel/bpf/fixups.c-1775-\tstruct bpf_prog *new_prog;\n--\nkernel/bpf/fixups.c-1782-\tif (env-\u003eseen_exception \u0026\u0026 !env-\u003eexception_callback_subprog) {\nkernel/bpf/fixups.c:1783:\t\tstruct bpf_insn *patch = insn_buf;\nkernel/bpf/fixups.c-1784-\n--\nkernel/bpf/fixups.c-1787-\t\t*patch++ = BPF_EXIT_INSN();\nkernel/bpf/fixups.c:1788:\t\tret = add_hidden_subprog(env, insn_buf, patch - insn_buf);\nkernel/bpf/fixups.c-1789-\t\tif (ret \u003c 0)\n--\nkernel/bpf/fixups.c-1813-\t\tif (env-\u003einsn_aux_data[i + delta].arena_scalar) {\nkernel/bpf/fixups.c:1814:\t\t\tcnt = arena_scalar_access(insn, insn_buf);\nkernel/bpf/fixups.c-1815-\nkernel/bpf/fixups.c:1816:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-1817-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-1834-\t\t\tbool isdiv = BPF_OP(insn-\u003ecode) == BPF_DIV;\nkernel/bpf/fixups.c:1835:\t\t\tstruct bpf_insn *patch = insn_buf;\nkernel/bpf/fixups.c-1836-\n--\nkernel/bpf/fixups.c-1843-\nkernel/bpf/fixups.c:1844:\t\t\tcnt = patch - insn_buf;\nkernel/bpf/fixups.c-1845-\nkernel/bpf/fixups.c:1846:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-1847-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-1864-\t\t\tbool is_smod = !isdiv \u0026\u0026 insn-\u003eoff == 1;\nkernel/bpf/fixups.c:1865:\t\t\tstruct bpf_insn *patch = insn_buf;\nkernel/bpf/fixups.c-1866-\n--\nkernel/bpf/fixups.c-1890-\t\t\t\t*patch++ = *insn;\nkernel/bpf/fixups.c:1891:\t\t\t\tcnt = patch - insn_buf;\nkernel/bpf/fixups.c-1892-\t\t\t} else if (is_smod) {\n--\nkernel/bpf/fixups.c-1912-\t\t\t\t}\nkernel/bpf/fixups.c:1913:\t\t\t\tcnt = patch - insn_buf;\nkernel/bpf/fixups.c-1914-\t\t\t} else if (isdiv) {\n--\nkernel/bpf/fixups.c-1921-\t\t\t\t*patch++ = *insn;\nkernel/bpf/fixups.c:1922:\t\t\t\tcnt = patch - insn_buf;\nkernel/bpf/fixups.c-1923-\t\t\t} else {\n--\nkernel/bpf/fixups.c-1933-\t\t\t\t}\nkernel/bpf/fixups.c:1934:\t\t\t\tcnt = patch - insn_buf;\nkernel/bpf/fixups.c-1935-\t\t\t}\nkernel/bpf/fixups.c-1936-\nkernel/bpf/fixups.c:1937:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-1938-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-1950-\t\t BPF_MODE(insn-\u003ecode) == BPF_PROBE_MEMSX)) {\nkernel/bpf/fixups.c:1951:\t\t\tstruct bpf_insn *patch = insn_buf;\nkernel/bpf/fixups.c-1952-\t\t\tu64 uaddress_limit = bpf_arch_uaddress_limit();\n--\nkernel/bpf/fixups.c-1965-\nkernel/bpf/fixups.c:1966:\t\t\tcnt = patch - insn_buf;\nkernel/bpf/fixups.c:1967:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-1968-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-1980-\t\t BPF_MODE(insn-\u003ecode) == BPF_IND)) {\nkernel/bpf/fixups.c:1981:\t\t\tcnt = env-\u003eops-\u003egen_ld_abs(insn, insn_buf);\nkernel/bpf/fixups.c-1982-\t\t\tif (cnt == 0 || cnt \u003e= INSN_BUF_SIZE) {\n--\nkernel/bpf/fixups.c-1986-\nkernel/bpf/fixups.c:1987:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-1988-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-2001-\t\t\tconst u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X;\nkernel/bpf/fixups.c:2002:\t\t\tstruct bpf_insn *patch = insn_buf;\nkernel/bpf/fixups.c-2003-\t\t\tbool issrc, isneg, isimm;\n--\nkernel/bpf/fixups.c-2037-\t\t\t\t*patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1);\nkernel/bpf/fixups.c:2038:\t\t\tcnt = patch - insn_buf;\nkernel/bpf/fixups.c-2039-\nkernel/bpf/fixups.c:2040:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-2041-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-2079-\t\t\tstack_depth_extra = 16;\nkernel/bpf/fixups.c:2080:\t\t\tcnt = may_goto_expand(insn_buf, insn-\u003eoff, stack_off_cnt,\nkernel/bpf/fixups.c-2081-\t\t\t\t\t tail, ARRAY_SIZE(tail));\nkernel/bpf/fixups.c-2082-\nkernel/bpf/fixups.c:2083:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-2084-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-2098-\t\t\tstack_depth_extra = 8;\nkernel/bpf/fixups.c:2099:\t\t\tcnt = may_goto_expand(insn_buf, insn-\u003eoff, stack_off,\nkernel/bpf/fixups.c-2100-\t\t\t\t\t tail, ARRAY_SIZE(tail));\nkernel/bpf/fixups.c-2101-\nkernel/bpf/fixups.c:2102:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-2103-\t\t\tif (!new_prog)\n--\n\n"
} |
| 55/3 | 2026/10/03 02:39 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 78418 output: 41 thoughts: 406 Thoughts: **Analyzing BPF Instruction Buffers and Register Mapping** Okay, so I'm looking closely at how the instruction buffer, `env->insn_buf`, is used within `fixups.c`. It seems `arena_scalar_access` is the only function that writes to this buffer, and it writes just two instructions. The key here is that both of those instructions are fully initialized, both through macros and by direct assignment. Now I'm shifting focus to `arch/x86/net/bpf_jit_comp.c`. I need to understand how register mapping works on the x86 architecture. I'm examining the `reg2pt_regs` array, which maps BPF registers to their corresponding `pt_regs` offsets. I'm paying close attention to the inclusion of `BPF_REG_AX` and its association with `offsetof(struct pt_regs, r10)`. I'm going to follow the usage of `reg2pt_regs` to fully understand the context. |
| 56/3 | 2026/10/03 02:39 | tool | grepper |
0mArgs:{
"Expression": "reg2pt_regs",
"PathPrefix": "arch/x86/net/bpf_jit_comp.c"
}Results: {
"Output": "arch/x86/net/bpf_jit_comp.c=210=static const int reg2hex[] = {\n--\narch/x86/net/bpf_jit_comp.c-227-\narch/x86/net/bpf_jit_comp.c:228:static const int reg2pt_regs[] = {\narch/x86/net/bpf_jit_comp.c-229-\t[BPF_REG_0] = offsetof(struct pt_regs, ax),\n--\narch/x86/net/bpf_jit_comp.c=1661=static int emit_atomic_ld_st_index(u8 **pprog, u32 atomic_op, u32 size,\n--\narch/x86/net/bpf_jit_comp.c-1696- * - INSN_LEN (8 bits): Length of faulting insn (max x86 insn = 15 bytes (fits in 8 bits)).\narch/x86/net/bpf_jit_comp.c:1697: * - DST_REG (8 bits): Offset of dst_reg from reg2pt_regs[] (max offset = 112 (fits in 8 bits)).\narch/x86/net/bpf_jit_comp.c-1698- * This is set to DONT_CLEAR if the insn does not read into a register.\n--\narch/x86/net/bpf_jit_comp.c=2003=static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *addrs, u8 *image,\n--\narch/x86/net/bpf_jit_comp.c-2665-\t\t\t\t bpf_atomic_is_load_acq(insn)) {\narch/x86/net/bpf_jit_comp.c:2666:\t\t\t\t\tarena_reg = reg2pt_regs[src_reg];\narch/x86/net/bpf_jit_comp.c:2667:\t\t\t\t\tfixup_reg = reg2pt_regs[dst_reg];\narch/x86/net/bpf_jit_comp.c-2668-\t\t\t\t\tis_write = false;\n--\narch/x86/net/bpf_jit_comp.c-2678-\narch/x86/net/bpf_jit_comp.c:2679:\t\t\t\t\tarena_reg = reg2pt_regs[dst_reg];\narch/x86/net/bpf_jit_comp.c-2680-\t\t\t\t\tfixup_reg = load_reg \u003c 0 ? DONT_CLEAR :\narch/x86/net/bpf_jit_comp.c:2681:\t\t\t\t\t\t reg2pt_regs[load_reg];\narch/x86/net/bpf_jit_comp.c-2682-\t\t\t\t\tis_write = true;\n--\narch/x86/net/bpf_jit_comp.c-2830-\t\t\t\tex-\u003efixup = FIELD_PREP(FIXUP_INSN_LEN_MASK, prog - start_of_ldx) |\narch/x86/net/bpf_jit_comp.c:2831:\t\t\t\t\t FIELD_PREP(FIXUP_REG_MASK, reg2pt_regs[dst_reg]);\narch/x86/net/bpf_jit_comp.c-2832-\t\t\t}\n"
} |
| 57/3 | 2026/10/03 02:39 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 79953 output: 34 thoughts: 875 Thoughts: **Analyzing BPF Register Mapping and Pointer Arithmetic in the Verifier** Okay, so I'm looking at how the x86 JIT maps `BPF_REG_AX` to the hardware register `r10` (or the auxiliary register) for arena accesses. Previously, `BPF_REG_AX` wasn't explicitly handled in `reg2pt_regs`, the mapping that translates BPF registers to offsets within `pt_regs`. However, since `arena_scalar_access` uses `BPF_REG_AX` as a source or destination register, the verifier could potentially be looking up `reg2pt_regs[BPF_REG_AX]` when working with `arena_reg`. It makes sense to add `[BPF_REG_AX] = offsetof(struct pt_regs, r10)` to ensure that the mapping to `r10` is correct and consistent. Now, I'm diving into `adjust_ptr_min_max_vals` in the verifier. The critical thing I see is how it handles potential pointer arithmetic and the resulting type conversions. The logic determining if a pointer becomes a scalar depends on `to_scalar`. `to_scalar` is set to true if the operation isn't an addition or subtraction, and if the environment allows pointer leaks. The code only allows additions and subtractions to keep a pointer value. If `to_scalar` is true, then `__mark_reg_unknown(env, dst_reg)` is called, resetting the register state to an unknown scalar. I need to make sure I understand what this `__mark_reg_unknown` function actually does. I'll need to check the function to confirm that it correctly initializes the register state to a completely unknown scalar value. This is a critical point to ensure the verifier's type tracking is correct. |
| 58/3 | 2026/10/03 02:39 | tool | grepper |
0mArgs:{
"Expression": "__mark_reg_unknown",
"PathPrefix": "kernel/bpf/verifier.c"
}Results: {
"Output": "kernel/bpf/verifier.c=808=static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg)\n--\nkernel/bpf/verifier.c-827-\nkernel/bpf/verifier.c:828:static void __mark_reg_unknown(const struct bpf_verifier_env *env,\nkernel/bpf/verifier.c-829-\t\t\t struct bpf_reg_state *reg);\n--\nkernel/bpf/verifier.c=831=static void mark_reg_invalid(const struct bpf_verifier_env *env, struct bpf_reg_state *reg)\n--\nkernel/bpf/verifier.c-835-\telse\nkernel/bpf/verifier.c:836:\t\t__mark_reg_unknown(env, reg);\nkernel/bpf/verifier.c-837-}\n--\nkernel/bpf/verifier.c=2257=void bpf_mark_reg_unknown_imprecise(struct bpf_reg_state *reg)\n--\nkernel/bpf/verifier.c-2267- */\nkernel/bpf/verifier.c:2268:static void __mark_reg_unknown(const struct bpf_verifier_env *env,\nkernel/bpf/verifier.c-2269-\t\t\t struct bpf_reg_state *reg)\n--\nkernel/bpf/verifier.c=2275=static void mark_reg_unknown(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-2277-{\nkernel/bpf/verifier.c:2278:\t__mark_reg_unknown(env, regs + regno);\nkernel/bpf/verifier.c-2279-}\n--\nkernel/bpf/verifier.c=2301=void bpf_mark_reg_not_init(const struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-2303-{\nkernel/bpf/verifier.c:2304:\t__mark_reg_unknown(env, reg);\nkernel/bpf/verifier.c-2305-\treg-\u003etype = NOT_INIT;\n--\nkernel/bpf/verifier.c=7449=static int check_stack_range_initialized(\n--\nkernel/bpf/verifier.c-7548-\t\t\tif (clobber) {\nkernel/bpf/verifier.c:7549:\t\t\t\t__mark_reg_unknown(env, \u0026ss-\u003espilled_ptr);\nkernel/bpf/verifier.c-7550-\t\t\t\tfor (j = 0; j \u003c BPF_REG_SIZE; j++)\n--\nkernel/bpf/verifier.c=8457=static void maybe_widen_reg(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-8465-\t\treturn;\nkernel/bpf/verifier.c:8466:\t__mark_reg_unknown(env, rcur);\nkernel/bpf/verifier.c-8467-}\n--\nkernel/bpf/verifier.c=15583=static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-15611-\t\t */\nkernel/bpf/verifier.c:15612:\t\t__mark_reg_unknown(env, dst_reg);\nkernel/bpf/verifier.c-15613-\t\treturn 0;\n--\nkernel/bpf/verifier.c-15618-\t\tif (opcode == BPF_SUB \u0026\u0026 env-\u003eallow_ptr_leaks) {\nkernel/bpf/verifier.c:15619:\t\t\t__mark_reg_unknown(env, dst_reg);\nkernel/bpf/verifier.c-15620-\t\t\treturn 0;\n--\nkernel/bpf/verifier.c-15682-\tif (to_scalar) {\nkernel/bpf/verifier.c:15683:\t\t__mark_reg_unknown(env, dst_reg);\nkernel/bpf/verifier.c-15684-\t\treturn 0;\n--\nkernel/bpf/verifier.c=16595=static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-16605-\tif (!is_safe_to_compute_dst_reg_range(insn, \u0026src_reg)) {\nkernel/bpf/verifier.c:16606:\t\t__mark_reg_unknown(env, dst_reg);\nkernel/bpf/verifier.c-16607-\t\treturn 0;\n--\nkernel/bpf/verifier.c=18232=static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)\n--\nkernel/bpf/verifier.c-18317-\t\tif (map-\u003emap_type == BPF_MAP_TYPE_ARENA) {\nkernel/bpf/verifier.c:18318:\t\t\t__mark_reg_unknown(env, dst_reg);\nkernel/bpf/verifier.c-18319-\t\t\tdst_reg-\u003emap_ptr = map;\n"
} |
| 59/3 | 2026/10/03 02:39 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 82600 output: 37 thoughts: 15 Thoughts: Okay, let's dive into this. Here's my thought process laid out: **Checking the Mechanics of `__mark_reg_unknown`** Right, before I proceed with this, I need to understand what this function, `__mark_reg_unknown`, is actually *doing*. I'm encountering it in this context, so I need to make sure I grasp its specific role and how it's implemented. That way, I can correctly interpret the surrounding code and ensure my mental model of this part of the system is accurate. It's the first thing I need to do here. I'm going to dig into the definition of `__mark_reg_unknown` now to clarify how it operates. I need that baseline understanding to move forward effectively. |
| 60/3 | 2026/10/03 02:39 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/verifier.c",
"FirstLine": 2260,
"LineCount": 20
}Results: {
"Contents": "2260:\t\treg-\u003etype = SCALAR_VALUE;\n2261:\t\treg-\u003evar_off = tnum_unknown;\n2262:\t\t__mark_reg_unbounded(reg);\n2263:\t}\n2264:\t\n2265:\t/* Mark a register as having a completely unknown (scalar) value,\n2266:\t * initialize .precise as true when not bpf capable.\n2267:\t */\n2268:\tstatic void __mark_reg_unknown(const struct bpf_verifier_env *env,\n2269:\t\t\t\t struct bpf_reg_state *reg)\n2270:\t{\n2271:\t\tbpf_mark_reg_unknown_imprecise(reg);\n2272:\t\treg-\u003eprecise = !env-\u003ebpf_capable;\n2273:\t}\n2274:\t\n2275:\tstatic void mark_reg_unknown(struct bpf_verifier_env *env,\n2276:\t\t\t\t struct bpf_reg_state *regs, u32 regno)\n2277:\t{\n2278:\t\t__mark_reg_unknown(env, regs + regno);\n2279:\t}\n"
} |
| 61/3 | 2026/10/03 02:39 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 83133 output: 35 thoughts: 541 Thoughts: **Analyzing the 'NeedsKMSAN=true' Criteria**
Okay, let's break down this patch against the `NeedsKMSAN=true` criteria. First, we have to consider the risk of kernel structures being sent to user space where fields might be uninitialized. Looking at this patch, are there any direct copies to user space that could lead to an information leak? From what I can see, `bpf_prog_load` introduces a new program flag `BPF_F_ARENA_SCALAR`, but nothing is explicitly copied to user space. In BTF validation, we have more valid identifier characters accepted, omission of static function argument names, and a change in `btf_datasec_resolve` to allow `vsi->size < type_size`, but again, no immediate red flags for a kernel-to-user space information leak.
Next, we need to examine any conditional logic that might depend on potentially uninitialized variables. In `mem_access_reg`, the local variable `arena` is fully zeroed using `memset` before its fields are set. So, that part seems safe.
Now, let's delve deeper into `btf_var_is_piece`. The code snippet:
```c
static bool btf_var_is_piece(const struct btf *btf, const struct btf_type *var,
const struct btf_var_secinfo *vsi)
{
u32 size;
return IS_ERR(btf_resolve_size(btf, btf_type_by_id(btf, var->type), &size)) ||
vsi->size < size;
}
```
This is where things get a bit more interesting. The crucial question is: if `btf_resolve_size` returns an error, is `size` actually initialized? It's important to be sure we are not potentially reading an uninitialized variable here.
Fortunately, by applying my knowledge of short-circuit evaluation, it seems that if `btf_resolve_size` returns an error (and `IS_ERR` is true), the left side of the `||` will be true, and the right side of the expression `vsi->size < size` will not be evaluated. So, we are not using an potentially uninitialized value. Also, if `btf_resolve_size` returns successfully, does it set `size` properly? It's best to check `btf_resolve_size` to be completely sure.
|
| 62/3 | 2026/10/03 02:39 | tool | grepper |
0mArgs:{
"Expression": "btf_resolve_size",
"PathPrefix": "kernel/bpf/btf.c"
}Results: {
"Output": "kernel/bpf/btf.c=1378=static void *btf_show_obj_safe(struct btf_show *show,\n--\nkernel/bpf/btf.c-1388-\nkernel/bpf/btf.c:1389:\trt = btf_resolve_size(show-\u003ebtf, t, \u0026size);\nkernel/bpf/btf.c-1390-\tif (IS_ERR(rt)) {\n--\nkernel/bpf/btf.c=2092=static const struct btf_type *\nkernel/bpf/btf.c:2093:__btf_resolve_size(const struct btf *btf, const struct btf_type *type,\nkernel/bpf/btf.c-2094-\t\t u32 *type_size, const struct btf_type **elem_type,\n--\nkernel/bpf/btf.c=2162=const struct btf_type *\nkernel/bpf/btf.c:2163:btf_resolve_size(const struct btf *btf, const struct btf_type *type,\nkernel/bpf/btf.c-2164-\t\t u32 *type_size)\nkernel/bpf/btf.c-2165-{\nkernel/bpf/btf.c:2166:\treturn __btf_resolve_size(btf, type, type_size, NULL, NULL, NULL, NULL);\nkernel/bpf/btf.c-2167-}\n--\nkernel/bpf/btf.c=5438=static bool btf_var_is_piece(const struct btf *btf, const struct btf_type *var,\n--\nkernel/bpf/btf.c-5442-\nkernel/bpf/btf.c:5443:\treturn IS_ERR(btf_resolve_size(btf, btf_type_by_id(btf, var-\u003etype), \u0026size)) ||\nkernel/bpf/btf.c-5444-\t vsi-\u003esize \u003c size;\n--\nkernel/bpf/btf.c=7076=int btf_ctx_arg_offset(const struct btf *btf, const struct btf_type *func_proto,\n--\nkernel/bpf/btf.c-7086-\t\tt = btf_type_by_id(btf, args[i].type);\nkernel/bpf/btf.c:7087:\t\tt = btf_resolve_size(btf, t, \u0026sz);\nkernel/bpf/btf.c-7088-\t\tif (IS_ERR(t))\n--\nkernel/bpf/btf.c=7560=static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf,\n--\nkernel/bpf/btf.c-7680-\nkernel/bpf/btf.c:7681:\t\tmtype = __btf_resolve_size(btf, mtype, \u0026msize,\nkernel/bpf/btf.c-7682-\t\t\t\t\t \u0026elem_type, \u0026elem_id, \u0026total_nelems,\n--\nkernel/bpf/btf.c-7696-\nkernel/bpf/btf.c:7697:\t\t\t/* __btf_resolve_size() above helps to\nkernel/bpf/btf.c-7698-\t\t\t * linearize a multi-dimensional array.\n--\nkernel/bpf/btf.c=8471=int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog)\n--\nkernel/bpf/btf.c-8664-\t\t\tt = btf_type_skip_modifiers(btf, t-\u003etype, NULL);\nkernel/bpf/btf.c:8665:\t\t\tref_t = btf_resolve_size(btf, t, \u0026mem_size);\nkernel/bpf/btf.c-8666-\t\t\tif (IS_ERR(ref_t)) {\n"
} |
| 63/3 | 2026/10/03 02:39 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 85293 output: 38 thoughts: 21 Thoughts: Here's a summary of my thought process, keeping in mind my expert understanding: **Analyzing `__btf_resolve_size` Around Line 2092** Okay, so I'm digging into this code, and based on the context, I suspect there's something potentially interesting happening in `__btf_resolve_size` near line 2092. I immediately jump to the likely implication - a size resolution issue or perhaps a more nuanced memory layout concern within the BTF (BPF Type Format) metadata processing. Given my experience, I'm already mentally running through potential scenarios: Is this related to incorrect type sizing leading to misinterpretations? Are we seeing issues with how complex types are being handled – structs, unions, arrays, or pointers? Could there be a problem with forward declarations or circular dependencies in the BTF data that's causing this specific line of code to trigger? I'll need to closely examine the surrounding code for clues. Specifically, I'll pay attention to any memory operations, type casts, or comparisons that utilize the resolved size. Also, I'll need to verify that all upstream operations, specifically those relating to BTF parsing and type construction, are behaving correctly. It's time to start tracing the execution and looking at variable states to determine the root cause. This is likely related to the underlying logic of `btf_resolve_size` itself so I need to understand that. |
| 64/3 | 2026/10/03 02:40 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/btf.c",
"FirstLine": 2092,
"LineCount": 40
}Results: {
"Contents": "2092:\tstatic const struct btf_type *\n2093:\t__btf_resolve_size(const struct btf *btf, const struct btf_type *type,\n2094:\t\t\t u32 *type_size, const struct btf_type **elem_type,\n2095:\t\t\t u32 *elem_id, u32 *total_nelems, u32 *type_id)\n2096:\t{\n2097:\t\tconst struct btf_type *array_type = NULL;\n2098:\t\tconst struct btf_array *array = NULL;\n2099:\t\tu32 i, size, nelems = 1, id = 0;\n2100:\t\n2101:\t\tfor (i = 0; i \u003c MAX_RESOLVE_DEPTH; i++) {\n2102:\t\t\tswitch (BTF_INFO_KIND(type-\u003einfo)) {\n2103:\t\t\t/* type-\u003esize can be used */\n2104:\t\t\tcase BTF_KIND_INT:\n2105:\t\t\tcase BTF_KIND_STRUCT:\n2106:\t\t\tcase BTF_KIND_UNION:\n2107:\t\t\tcase BTF_KIND_ENUM:\n2108:\t\t\tcase BTF_KIND_FLOAT:\n2109:\t\t\tcase BTF_KIND_ENUM64:\n2110:\t\t\t\tsize = type-\u003esize;\n2111:\t\t\t\tgoto resolved;\n2112:\t\n2113:\t\t\tcase BTF_KIND_PTR:\n2114:\t\t\t\tsize = sizeof(void *);\n2115:\t\t\t\tgoto resolved;\n2116:\t\n2117:\t\t\t/* Modifiers */\n2118:\t\t\tcase BTF_KIND_TYPEDEF:\n2119:\t\t\tcase BTF_KIND_VOLATILE:\n2120:\t\t\tcase BTF_KIND_CONST:\n2121:\t\t\tcase BTF_KIND_RESTRICT:\n2122:\t\t\tcase BTF_KIND_TYPE_TAG:\n2123:\t\t\t\tid = type-\u003etype;\n2124:\t\t\t\ttype = btf_type_by_id(btf, type-\u003etype);\n2125:\t\t\t\tbreak;\n2126:\t\n2127:\t\t\tcase BTF_KIND_ARRAY:\n2128:\t\t\t\tif (!array_type)\n2129:\t\t\t\t\tarray_type = type;\n2130:\t\t\t\tarray = btf_type_array(type);\n2131:\t\t\t\tif (nelems \u0026\u0026 array-\u003enelems \u003e U32_MAX / nelems)\n"
} |
| 65/3 | 2026/10/03 02:40 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 86326 output: 38 thoughts: 16 Thoughts: Okay, I'm ready to summarize the provided text in the first person, assuming an expert user. Please provide the text you would like me to work with, including the line numbers if they exist. Once I have the text, I will summarize it as your thoughts. |
| 66/3 | 2026/10/03 02:40 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/btf.c",
"FirstLine": 2132,
"LineCount": 35
}Results: {
"Contents": "2132:\t\t\t\t\treturn ERR_PTR(-EINVAL);\n2133:\t\t\t\tnelems *= array-\u003enelems;\n2134:\t\t\t\ttype = btf_type_by_id(btf, array-\u003etype);\n2135:\t\t\t\tbreak;\n2136:\t\n2137:\t\t\t/* type without size */\n2138:\t\t\tdefault:\n2139:\t\t\t\treturn ERR_PTR(-EINVAL);\n2140:\t\t\t}\n2141:\t\t}\n2142:\t\n2143:\t\treturn ERR_PTR(-EINVAL);\n2144:\t\n2145:\tresolved:\n2146:\t\tif (nelems \u0026\u0026 size \u003e U32_MAX / nelems)\n2147:\t\t\treturn ERR_PTR(-EINVAL);\n2148:\t\n2149:\t\t*type_size = nelems * size;\n2150:\t\tif (total_nelems)\n2151:\t\t\t*total_nelems = nelems;\n2152:\t\tif (elem_type)\n2153:\t\t\t*elem_type = type;\n2154:\t\tif (elem_id)\n2155:\t\t\t*elem_id = array ? array-\u003etype : 0;\n2156:\t\tif (type_id \u0026\u0026 id)\n2157:\t\t\t*type_id = id;\n2158:\t\n2159:\t\treturn array_type ? : type;\n2160:\t}\n2161:\t\n2162:\tconst struct btf_type *\n2163:\tbtf_resolve_size(const struct btf *btf, const struct btf_type *type,\n2164:\t\t\t u32 *type_size)\n2165:\t{\n2166:\t\treturn __btf_resolve_size(btf, type, type_size, NULL, NULL, NULL, NULL);\n"
} |
| 67/3 | 2026/10/03 02:40 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 86930 output: 35 thoughts: 157 Thoughts: Okay, let me break this down. **Analyzing Potential Uninitialized Memory Reads in the BPF Verifier** Right, so `__btf_resolve_size` - that function sets the `*type_size` pointer to `nelems * size` when it succeeds, and crucially, *only* returns something other than an error if it resolves successfully. That means, looking at the code, the `size` variable in `btf_var_is_piece` will *always* be initialized before it's read. So that's good; it's likely not the source of our problem. Now, let's move on to the more general issue of uninitialized memory reads in complex data structures, which is our primary concern. Specifically, does this patch introduce such a problem? The change introduces a new field, `bool arena_scalar`, to the `struct bpf_insn_aux_data`. This is interesting. The critical question here is *how* the `env->insn_aux_data` is allocated and initialized. We need to check the verifier code to understand how this `struct` is handled, and in particular, *where* it's allocated. Are there any allocation or initialization paths in the verifier that could leave `arena_scalar` in an uninitialized state? I'll need to trace the allocation of `insn_aux_data` through the verifier code to determine this. |
| 68/3 | 2026/10/03 02:40 | tool | grepper |
0mArgs:{
"Expression": "insn_aux_data =",
"PathPrefix": "kernel/bpf/verifier.c"
}Results: {
"Output": "kernel/bpf/verifier.c=22450=int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,\n--\nkernel/bpf/verifier.c-22526-\tlen = env-\u003einsn_aux_data_len = env-\u003eprog-\u003elen;\nkernel/bpf/verifier.c:22527:\tenv-\u003einsn_aux_data =\nkernel/bpf/verifier.c-22528-\t\t__vmalloc(array_size(sizeof(struct bpf_insn_aux_data), len),\n"
} |
| 69/3 | 2026/10/03 02:40 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 87590 output: 38 thoughts: 28 Thoughts: Okay, here's my interpretation of what's going on in those lines of `kernel/bpf/verifier.c`, from an expert's perspective, conveyed as a first-person summary of my thought process: **Diving into the BPF Verifier: A Quick Look at Lines 22525-22535** Alright, let's take a look at what the BPF verifier is up to in this section. Specifically, lines 22525-22535... Hmm, based on my understanding of the verifier's overall structure, and the surrounding code, I'm betting this relates to... probably memory access, or maybe register state analysis during program execution. This likely has something to do with the verifier’s function calls. Okay, let me quickly decode the C code: ```c ... // Likely a function that analyzes the stack? ret = check_stack_write(env, insn, &insn->src_reg, 8); if (ret) return ret; // Is this a function that checks for register write restrictions? ret = check_reg_write(env, insn, &insn->src_reg, 8); if (ret) return ret; ... ``` Right, I see. `check_stack_write` and `check_reg_write`... these are the clues. These functions clearly relate to the BPF instruction (`insn`) in question, specifically the source register (`insn->src_reg`). I'm guessing that the verifier is performing two critical checks: 1. **Stack Write Validation:** First, it's verifying that the attempted write to the stack, as indicated by the instruction, is valid and within the bounds of what the program is permitted to do. We're looking at a data size of 8 bytes. This is likely about avoiding stack overflows or writes to uninitialized memory. 2. **Register Write Validation:** Second, it's checking that the write to the register is permissible. This could involve ensuring the register isn't read-only at this point, or isn't being overwritten in a way that violates a previous rule. Or it could be about restricting writes based on the type of program being loaded. The `if (ret)` checks are classic error handling, indicating that if *either* of these checks fail, the verifier will return an error code immediately, preventing the BPF program from being loaded and executed. This is good: it's a critical part of the security model. And the 8 bytes could likely be about ensuring the right size of data is being written to memory. I could probably guess from these checks alone what the checks might do, based on my knowledge of the verifier. So, overall, nothing unexpected, just solid, necessary validation steps that are likely repeated in many parts of the verifier for different memory locations and instructions. |
| 70/3 | 2026/10/03 02:40 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/verifier.c",
"FirstLine": 22525,
"LineCount": 15
}Results: {
"Contents": "22525:\t\n22526:\t\tlen = env-\u003einsn_aux_data_len = env-\u003eprog-\u003elen;\n22527:\t\tenv-\u003einsn_aux_data =\n22528:\t\t\t__vmalloc(array_size(sizeof(struct bpf_insn_aux_data), len),\n22529:\t\t\t\t GFP_KERNEL_ACCOUNT | __GFP_ZERO);\n22530:\t\tret = -ENOMEM;\n22531:\t\tif (!env-\u003einsn_aux_data)\n22532:\t\t\tgoto skip_full_check;\n22533:\t\tfor (i = 0; i \u003c len; i++)\n22534:\t\t\tenv-\u003einsn_aux_data[i].orig_idx = i;\n22535:\t\tenv-\u003esucc = bpf_iarray_realloc(NULL, 2);\n22536:\t\tif (!env-\u003esucc)\n22537:\t\t\tgoto skip_full_check;\n22538:\t\n22539:\t\tmark_verifier_state_clean(env);\n"
} |
| 71/3 | 2026/10/03 02:40 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 88587 output: 35 thoughts: 48 Thoughts: Here's my summary, as though I'm thinking through this process: **Diving into `insn_aux_data` and Patching** Okay, so `env->insn_aux_data` is allocated with `__GFP_ZERO`. That's a good starting point – ensures we're dealing with a clean slate initially. Now, I need to understand how the system manages and updates `insn_aux_data` during patching operations, particularly within `bpf_patch_insn_data`. I need to trace the evolution of this data to ensure its integrity and that the assumptions I am making about it are correct. Let me quickly refresh my understanding of where that structure is used, and then jump into the `bpf_patch_insn_data` function. I'll need to keep an eye out for potential race conditions or other concurrency issues, given the kernel context. Let's see how it's maintaining the aux data. |
| 72/3 | 2026/10/03 02:40 | tool | grepper |
0mArgs:{
"Expression": "bpf_patch_insn_data\\(",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/core.c=1567=struct bpf_prog *bpf_jit_blind_constants(struct bpf_verifier_env *env, struct bpf_prog *prog)\n--\nkernel/bpf/core.c-1581-\nkernel/bpf/core.c:1582:\t/* make sure bpf_patch_insn_data() patches the correct prog */\nkernel/bpf/core.c-1583-\tif (env)\n--\nkernel/bpf/core.c-1613-\t\tif (env)\nkernel/bpf/core.c:1614:\t\t\ttmp = bpf_patch_insn_data(env, i, insn_buff, rewritten);\nkernel/bpf/core.c-1615-\t\telse\n--\nkernel/bpf/fixups.c=377=static bool bpf_rewrite_must_abort(void)\n--\nkernel/bpf/fixups.c-384-\nkernel/bpf/fixups.c:385:static struct bpf_prog *__bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,\nkernel/bpf/fixups.c-386-\t\t\t\t\t const struct bpf_insn *patch, u32 len,\n--\nkernel/bpf/fixups.c-425-\nkernel/bpf/fixups.c:426:struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,\nkernel/bpf/fixups.c-427-\t\t\t\t const struct bpf_insn *patch, u32 len)\nkernel/bpf/fixups.c-428-{\nkernel/bpf/fixups.c:429:\treturn __bpf_patch_insn_data(env, off, patch, len, BPF_PATCH_KEEP_TARGET);\nkernel/bpf/fixups.c-430-}\n--\nkernel/bpf/fixups.c=802=int bpf_opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env,\n--\nkernel/bpf/fixups.c-886-apply_patch_buffer:\nkernel/bpf/fixups.c:887:\t\tnew_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len);\nkernel/bpf/fixups.c-888-\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c=904=int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)\n--\nkernel/bpf/fixups.c-931-\t\t\tinsn_buf[cnt++] = env-\u003eprog-\u003einsnsi[0];\nkernel/bpf/fixups.c:932:\t\t\tnew_prog = __bpf_patch_insn_data(env, 0, insn_buf, cnt,\nkernel/bpf/fixups.c-933-\t\t\t\t\t\t\t BPF_PATCH_MOVE_TARGET);\n--\nkernel/bpf/fixups.c-955-\t\t} else if (cnt) {\nkernel/bpf/fixups.c:956:\t\t\tnew_prog = __bpf_patch_insn_data(env, 0, insn_buf, cnt,\nkernel/bpf/fixups.c-957-\t\t\t\t\t\t\t BPF_PATCH_MOVE_TARGET);\n--\nkernel/bpf/fixups.c-995-\t\t\tcnt = patch - insn_buf;\nkernel/bpf/fixups.c:996:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-997-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-1068-\t\t\tcnt = patch - insn_buf;\nkernel/bpf/fixups.c:1069:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-1070-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-1202-patch_insn_buf:\nkernel/bpf/fixups.c:1203:\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-1204-\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c=1709=static int add_hidden_subprog(struct bpf_verifier_env *env, struct bpf_insn *patch, int len)\n--\nkernel/bpf/fixups.c-1723-\t */\nkernel/bpf/fixups.c:1724:\tprog = bpf_patch_insn_data(env, env-\u003eprog-\u003elen - 1, patch, len);\nkernel/bpf/fixups.c-1725-\tif (!prog)\n--\nkernel/bpf/fixups.c=1764=int bpf_do_misc_fixups(struct bpf_verifier_env *env)\n--\nkernel/bpf/fixups.c-1815-\nkernel/bpf/fixups.c:1816:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-1817-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-1845-\nkernel/bpf/fixups.c:1846:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-1847-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-1936-\nkernel/bpf/fixups.c:1937:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-1938-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-1966-\t\t\tcnt = patch - insn_buf;\nkernel/bpf/fixups.c:1967:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-1968-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-1986-\nkernel/bpf/fixups.c:1987:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-1988-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-2039-\nkernel/bpf/fixups.c:2040:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-2041-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-2082-\nkernel/bpf/fixups.c:2083:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-2084-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-2101-\nkernel/bpf/fixups.c:2102:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-2103-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-2128-\t\t\t * with that same half + mov64_percpu_reg insn.\nkernel/bpf/fixups.c:2129:\t\t\t * All because bpf_patch_insn_data() can only\nkernel/bpf/fixups.c-2130-\t\t\t * replace one 8-byte insn, which does not work\n--\nkernel/bpf/fixups.c-2138-\t\t\ti++;\nkernel/bpf/fixups.c:2139:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-2140-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-2159-\nkernel/bpf/fixups.c:2160:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-2161-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-2246-\t\t\tcnt = 3;\nkernel/bpf/fixups.c:2247:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-2248-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-2280-\nkernel/bpf/fixups.c:2281:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-2282-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-2299-\nkernel/bpf/fixups.c:2300:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-2301-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-2342-\nkernel/bpf/fixups.c:2343:\t\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta,\nkernel/bpf/fixups.c-2344-\t\t\t\t\t\t\t insn_buf, cnt);\n--\nkernel/bpf/fixups.c-2425-\nkernel/bpf/fixups.c:2426:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf,\nkernel/bpf/fixups.c-2427-\t\t\t\t\t\t cnt);\n--\nkernel/bpf/fixups.c-2455-#endif\nkernel/bpf/fixups.c:2456:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-2457-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-2474-\nkernel/bpf/fixups.c:2475:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-2476-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-2508-\nkernel/bpf/fixups.c:2509:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-2510-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-2540-\nkernel/bpf/fixups.c:2541:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-2542-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-2566-\nkernel/bpf/fixups.c:2567:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-2568-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-2582-\nkernel/bpf/fixups.c:2583:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1);\nkernel/bpf/fixups.c-2584-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-2637-\nkernel/bpf/fixups.c:2638:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-2639-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-2655-\nkernel/bpf/fixups.c:2656:\t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\nkernel/bpf/fixups.c-2657-\t\t\tif (!new_prog)\n--\nkernel/bpf/fixups.c-2730-\nkernel/bpf/fixups.c:2731:\t\tnew_prog = __bpf_patch_insn_data(env, subprog_start, insn_buf, cnt,\nkernel/bpf/fixups.c-2732-\t\t\t\t\t\t BPF_PATCH_MOVE_TARGET);\n--\nkernel/bpf/fixups.c-2739-\t\t * to insn after BPF_ST that inits may_goto count.\nkernel/bpf/fixups.c:2740:\t\t * Adjustment will succeed because bpf_patch_insn_data() didn't fail.\nkernel/bpf/fixups.c-2741-\t\t */\n--\nkernel/bpf/fixups.c=2769=static struct bpf_prog *inline_bpf_loop(struct bpf_verifier_env *env,\n--\nkernel/bpf/fixups.c-2830-\t*total_cnt = cnt;\nkernel/bpf/fixups.c:2831:\tnew_prog = bpf_patch_insn_data(env, position, insn_buf, cnt);\nkernel/bpf/fixups.c-2832-\tif (!new_prog)\n"
} |
| 73/3 | 2026/10/03 02:40 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 92583 output: 37 thoughts: 34 Thoughts: Here's a summary of my thought process, as requested: **Diving into `__bpf_patch_insn_data` in `fixups.c`** Okay, I'm going to take a look at `__bpf_patch_insn_data`. It's located around line 385 in `kernel/bpf/fixups.c`, and given its name, I'm expecting it deals with patching instruction data within the BPF context. I'm likely interested in how it handles address fixups or other runtime modifications to BPF bytecode. My focus is going to be on understanding the exact data manipulation that occurs here. I'll probably want to see what arguments it takes, the types of operations it performs, and how it interacts with the broader BPF verifier and execution environment. It might be related to relocation or symbol resolution. Time to start digging! |
| 74/3 | 2026/10/03 02:40 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/fixups.c",
"FirstLine": 385,
"LineCount": 45
}Results: {
"Contents": " 385:\tstatic struct bpf_prog *__bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,\n 386:\t\t\t\t\t\t const struct bpf_insn *patch, u32 len,\n 387:\t\t\t\t\t\t enum bpf_patch_mode mode)\n 388:\t{\n 389:\t\tstruct bpf_prog *new_prog;\n 390:\t\tstruct bpf_insn_aux_data *new_data = NULL;\n 391:\t\t/* The first slot whose targets move. */\n 392:\t\tu32 first = mode == BPF_PATCH_MOVE_TARGET ? off : off + 1;\n 393:\t\tstruct bpf_insn original_insn;\n 394:\t\n 395:\t\tif (bpf_rewrite_must_abort())\n 396:\t\t\treturn NULL;\n 397:\t\n 398:\t\tif (len \u003e 1) {\n 399:\t\t\tnew_data = vrealloc(env-\u003einsn_aux_data,\n 400:\t\t\t\t\t array_size(env-\u003eprog-\u003elen + len - 1,\n 401:\t\t\t\t\t\t sizeof(struct bpf_insn_aux_data)),\n 402:\t\t\t\t\t GFP_KERNEL_ACCOUNT | __GFP_ZERO);\n 403:\t\t\tif (!new_data)\n 404:\t\t\t\treturn NULL;\n 405:\t\n 406:\t\t\tenv-\u003einsn_aux_data = new_data;\n 407:\t\t}\n 408:\t\n 409:\t\tmemcpy(\u0026original_insn, env-\u003eprog-\u003einsnsi + off, sizeof(struct bpf_insn));\n 410:\t\tnew_prog = bpf_patch_insn_single(env-\u003eprog, off, patch, len);\n 411:\t\tif (IS_ERR(new_prog)) {\n 412:\t\t\tif (PTR_ERR(new_prog) == -ERANGE)\n 413:\t\t\t\tverbose(env,\n 414:\t\t\t\t\t\"insn %d cannot be patched due to 16-bit range\\n\",\n 415:\t\t\t\t\tenv-\u003einsn_aux_data[off].orig_idx);\n 416:\t\t\treturn NULL;\n 417:\t\t}\n 418:\t\tadjust_insn_aux_data(env, new_prog, off, len, \u0026original_insn, mode);\n 419:\t\tadjust_subprog_starts(env, off, len);\n 420:\t\tadjust_insn_arrays(env, first, len);\n 421:\t\tbpf_adjust_func_ptrs(env, off, len);\n 422:\t\tadjust_poke_descs(new_prog, first, len);\n 423:\t\treturn new_prog;\n 424:\t}\n 425:\t\n 426:\tstruct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,\n 427:\t\t\t\t\t const struct bpf_insn *patch, u32 len)\n 428:\t{\n 429:\t\treturn __bpf_patch_insn_data(env, off, patch, len, BPF_PATCH_KEEP_TARGET);\n"
} |
| 75/3 | 2026/10/03 02:40 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 93692 output: 33 thoughts: 281 Thoughts: **Analyzing BPF Arena Initialization** Okay, so `vrealloc` is using `GFP_KERNEL_ACCOUNT | __GFP_ZERO`. That means any memory allocated for `insn_aux_data` is guaranteed to be zero-initialized. Good. Now, let's move on to criterion 4, the potential for uninitialized byte access. Specifically, I need to focus on how BPF arenas and `arena_scalar` interact with kernel memory. Right, what *is* a BPF arena again? It's a sparse memory region, mmap-able for user/kernel sharing, defined by the `BPF_MAP_TYPE_ARENA` type. Now, the critical question: how do loads and stores work through this arena? In a BPF arena, the user space either maps an existing arena page or allocates new ones via `bpf_arena_alloc_pages`. When the BPF program touches an arena address, the JIT translates this into native instructions. These instructions add the arena base address to a 32-bit offset. The wrinkle is, what happens if the page isn't actually *there*? If it's not allocated or faulted, the exception handler takes over. It either reads zeros or drops writes, handled through a BPF exception table fixup, if I recall correctly. So that's the protection mechanism. But... that raises a key question. What if the BPF program *reads* from an unallocated or, crucially, even an *allocated* page *within* the arena? Does `bpf_arena_alloc_pages` zero those pages? I need to check the code for that allocation function. That's the key to this issue. |
| 76/3 | 2026/10/03 02:40 | tool | grepper |
0mArgs:{
"Expression": "bpf_arena_alloc_pages",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/arena.c-39- * into kernel and user vma.\nkernel/bpf/arena.c:40: * bpf program can allocate a page via bpf_arena_alloc_pages() kfunc\nkernel/bpf/arena.c-41- * which will insert it into kernel vm_area.\n--\nkernel/bpf/arena.c=890=static void arena_free_pages(struct bpf_arena *arena, long uaddr, long page_cnt, bool sleepable)\n--\nkernel/bpf/arena.c-979- * Reserve an arena virtual address range without populating it. This call stops\nkernel/bpf/arena.c:980: * bpf_arena_alloc_pages from adding pages to this range.\nkernel/bpf/arena.c-981- */\n--\nkernel/bpf/arena.c=1090=__bpf_kfunc_start_defs();\nkernel/bpf/arena.c-1091-\nkernel/bpf/arena.c:1092:__bpf_kfunc void *bpf_arena_alloc_pages(void *p__map, void *addr__ign, u32 page_cnt,\nkernel/bpf/arena.c-1093-\t\t\t\t\tint node_id, u64 flags)\n--\nkernel/bpf/arena.c-1103-\nkernel/bpf/arena.c:1104:void *bpf_arena_alloc_pages_non_sleepable(void *p__map, void *addr__ign, u32 page_cnt,\nkernel/bpf/arena.c-1105-\t\t\t\t\t int node_id, u64 flags)\n--\nkernel/bpf/arena.c-1115-\nkernel/bpf/arena.c:1116:void *bpf_arena_alloc_pages_sleepable(void *p__map, void *addr__ign, u32 page_cnt,\nkernel/bpf/arena.c-1117-\t\t\t\t int node_id, u64 flags)\n--\nkernel/bpf/arena.c=1163=BTF_KFUNCS_START(arena_kfuncs)\nkernel/bpf/arena.c:1164:BTF_ID_FLAGS(func, bpf_arena_alloc_pages, KF_ARENA_RET | KF_ARENA_ARG2 | KF_SPINLOCK_SAFE)\nkernel/bpf/arena.c-1165-BTF_ID_FLAGS(func, bpf_arena_free_pages, KF_ARENA_ARG2 | KF_SPINLOCK_SAFE)\n--\nkernel/bpf/verifier.c=13102=enum special_kfunc_type {\n--\nkernel/bpf/verifier.c-13168-\tKF_bpf_call_rcu_tasks_trace,\nkernel/bpf/verifier.c:13169:\tKF_bpf_arena_alloc_pages,\nkernel/bpf/verifier.c-13170-\tKF_bpf_arena_free_pages,\n--\nkernel/bpf/verifier.c=13263=BTF_ID(func, bpf_call_rcu_tasks_trace)\nkernel/bpf/verifier.c:13264:BTF_ID(func, bpf_arena_alloc_pages)\nkernel/bpf/verifier.c-13265-BTF_ID(func, bpf_arena_free_pages)\n--\nkernel/bpf/verifier.c=22018=static int specialize_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_desc *desc, int insn_idx)\n--\nkernel/bpf/verifier.c-22055-\t\t\taddr = (unsigned long)bpf_dynptr_from_file_sleepable;\nkernel/bpf/verifier.c:22056:\t} else if (func_id == special_kfunc_list[KF_bpf_arena_alloc_pages]) {\nkernel/bpf/verifier.c-22057-\t\tif (env-\u003einsn_aux_data[insn_idx].non_sleepable)\nkernel/bpf/verifier.c:22058:\t\t\taddr = (unsigned long)bpf_arena_alloc_pages_non_sleepable;\nkernel/bpf/verifier.c-22059-\t} else if (func_id == special_kfunc_list[KF_bpf_arena_free_pages]) {\n"
} |
| 77/3 | 2026/10/03 02:40 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 95489 output: 37 thoughts: 25 Thoughts: Here's a first-person summary, tailored for an expert audience, of the provided thought process: **Diving into the BPF Arena Allocator** Okay, time to get my hands dirty. I need to take a look at the `bpf_arena_alloc_pages` function. Specifically, I'm heading over to `kernel/bpf/arena.c` to see how it's implemented. I know this area inside and out, but a fresh scan can never hurt when debugging, especially when dealing with memory allocation in the BPF subsystem. I want to see how the arena allocator is handling page allocation. I'm keen to examine the underlying mechanisms to identify any potential bottlenecks, inefficiencies, or, of course, any possible bugs. I'll likely want to trace the allocation flow, looking at the interaction with the underlying page allocator (e.g., `__get_free_pages`, etc.) and how the arena manages its internal metadata. I'm primarily focused on allocation size classes and alignment considerations. |
| 78/3 | 2026/10/03 02:40 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/arena.c",
"FirstLine": 1090,
"LineCount": 30
}Results: {
"Contents": "1090:\t__bpf_kfunc_start_defs();\n1091:\t\n1092:\t__bpf_kfunc void *bpf_arena_alloc_pages(void *p__map, void *addr__ign, u32 page_cnt,\n1093:\t\t\t\t\t\tint node_id, u64 flags)\n1094:\t{\n1095:\t\tstruct bpf_map *map = p__map;\n1096:\t\tstruct bpf_arena *arena = container_of(map, struct bpf_arena, map);\n1097:\t\n1098:\t\tif (map-\u003emap_type != BPF_MAP_TYPE_ARENA || flags || !page_cnt)\n1099:\t\t\treturn NULL;\n1100:\t\n1101:\t\treturn (void *)arena_alloc_pages(arena, (long)addr__ign, page_cnt, node_id, true);\n1102:\t}\n1103:\t\n1104:\tvoid *bpf_arena_alloc_pages_non_sleepable(void *p__map, void *addr__ign, u32 page_cnt,\n1105:\t\t\t\t\t\t int node_id, u64 flags)\n1106:\t{\n1107:\t\tstruct bpf_map *map = p__map;\n1108:\t\tstruct bpf_arena *arena = container_of(map, struct bpf_arena, map);\n1109:\t\n1110:\t\tif (map-\u003emap_type != BPF_MAP_TYPE_ARENA || flags || !page_cnt)\n1111:\t\t\treturn NULL;\n1112:\t\n1113:\t\treturn (void *)arena_alloc_pages(arena, (long)addr__ign, page_cnt, node_id, false);\n1114:\t}\n1115:\t\n1116:\tvoid *bpf_arena_alloc_pages_sleepable(void *p__map, void *addr__ign, u32 page_cnt,\n1117:\t\t\t\t\t int node_id, u64 flags)\n1118:\t{\n1119:\t\tstruct bpf_map *map = p__map;\n"
} |
| 79/3 | 2026/10/03 02:40 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 96336 output: 33 thoughts: 22 Thoughts: Here's a breakdown of my thought process: **Diving into `arena_alloc_pages`** Okay, so I'm looking at `arena_alloc_pages` in `kernel/bpf/arena.c`. Immediately, I'm thinking: this is where the core allocation logic for the BPF arena is defined. This is *the* function where the rubber meets the road. I'll need to understand how it interfaces with the underlying memory management to truly grasp the memory behavior of BPF programs, especially in high-load scenarios. I need to understand this to have any insight on optimizations or debugging of BPF code. Time to dig in! |
| 80/3 | 2026/10/03 02:40 | tool | grepper |
0mArgs:{
"Expression": "arena_alloc_pages",
"PathPrefix": "kernel/bpf/arena.c"
}Results: {
"Output": "kernel/bpf/arena.c-39- * into kernel and user vma.\nkernel/bpf/arena.c:40: * bpf program can allocate a page via bpf_arena_alloc_pages() kfunc\nkernel/bpf/arena.c-41- * which will insert it into kernel vm_area.\n--\nkernel/bpf/arena.c=702=static u64 clear_lo32(u64 val)\n--\nkernel/bpf/arena.c-710- */\nkernel/bpf/arena.c:711:static long arena_alloc_pages(struct bpf_arena *arena, long uaddr, long page_cnt, int node_id,\nkernel/bpf/arena.c-712-\t\t\t bool sleepable)\n--\nkernel/bpf/arena.c=890=static void arena_free_pages(struct bpf_arena *arena, long uaddr, long page_cnt, bool sleepable)\n--\nkernel/bpf/arena.c-979- * Reserve an arena virtual address range without populating it. This call stops\nkernel/bpf/arena.c:980: * bpf_arena_alloc_pages from adding pages to this range.\nkernel/bpf/arena.c-981- */\n--\nkernel/bpf/arena.c=1090=__bpf_kfunc_start_defs();\nkernel/bpf/arena.c-1091-\nkernel/bpf/arena.c:1092:__bpf_kfunc void *bpf_arena_alloc_pages(void *p__map, void *addr__ign, u32 page_cnt,\nkernel/bpf/arena.c-1093-\t\t\t\t\tint node_id, u64 flags)\n--\nkernel/bpf/arena.c-1100-\nkernel/bpf/arena.c:1101:\treturn (void *)arena_alloc_pages(arena, (long)addr__ign, page_cnt, node_id, true);\nkernel/bpf/arena.c-1102-}\nkernel/bpf/arena.c-1103-\nkernel/bpf/arena.c:1104:void *bpf_arena_alloc_pages_non_sleepable(void *p__map, void *addr__ign, u32 page_cnt,\nkernel/bpf/arena.c-1105-\t\t\t\t\t int node_id, u64 flags)\n--\nkernel/bpf/arena.c-1112-\nkernel/bpf/arena.c:1113:\treturn (void *)arena_alloc_pages(arena, (long)addr__ign, page_cnt, node_id, false);\nkernel/bpf/arena.c-1114-}\nkernel/bpf/arena.c-1115-\nkernel/bpf/arena.c:1116:void *bpf_arena_alloc_pages_sleepable(void *p__map, void *addr__ign, u32 page_cnt,\nkernel/bpf/arena.c-1117-\t\t\t\t int node_id, u64 flags)\n--\nkernel/bpf/arena.c-1124-\nkernel/bpf/arena.c:1125:\treturn (void *)arena_alloc_pages(arena, (long)addr__ign, page_cnt, node_id, true);\nkernel/bpf/arena.c-1126-}\n--\nkernel/bpf/arena.c=1163=BTF_KFUNCS_START(arena_kfuncs)\nkernel/bpf/arena.c:1164:BTF_ID_FLAGS(func, bpf_arena_alloc_pages, KF_ARENA_RET | KF_ARENA_ARG2 | KF_SPINLOCK_SAFE)\nkernel/bpf/arena.c-1165-BTF_ID_FLAGS(func, bpf_arena_free_pages, KF_ARENA_ARG2 | KF_SPINLOCK_SAFE)\n"
} |
| 81/3 | 2026/10/03 02:40 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 97524 output: 36 thoughts: 27 Thoughts: Here's my summary, as if I were thinking it: **Digging into BPF Arena Memory Allocation** Alright, let's get down to brass tacks. I need to understand how the memory is actually allocated within the BPF arena, specifically the lower-level page allocation. I'm going to jump straight into the code. Line 711 in `kernel/bpf/arena.c` should be the sweet spot. That's usually where the core allocation mechanics begin. I need to see if it's using the standard kernel page allocator, a custom slab allocator, or something entirely unique to the BPF context. The specifics there will tell me a lot about the performance characteristics and potential bottlenecks of the entire arena system. It's time to dig in and see how those pages are coming into existence. |
| 82/3 | 2026/10/03 02:40 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/arena.c",
"FirstLine": 711,
"LineCount": 40
}Results: {
"Contents": " 711:\tstatic long arena_alloc_pages(struct bpf_arena *arena, long uaddr, long page_cnt, int node_id,\n 712:\t\t\t\t bool sleepable)\n 713:\t{\n 714:\t\t/* user_vm_end/start are fixed before bpf prog runs */\n 715:\t\tlong page_cnt_max = (arena-\u003euser_vm_end - arena-\u003euser_vm_start) \u003e\u003e PAGE_SHIFT;\n 716:\t\tu64 kern_vm_start = bpf_arena_get_kern_vm_start(arena);\n 717:\t\tstruct mem_cgroup *new_memcg, *old_memcg;\n 718:\t\tstruct apply_range_data data;\n 719:\t\tstruct page **pages = NULL;\n 720:\t\tlong remaining, mapped = 0;\n 721:\t\tlong alloc_pages;\n 722:\t\tunsigned long flags;\n 723:\t\tlong pgoff = 0;\n 724:\t\tu32 uaddr32;\n 725:\t\tint ret, i;\n 726:\t\n 727:\t\tif (node_id != NUMA_NO_NODE \u0026\u0026\n 728:\t\t ((unsigned int)node_id \u003e= nr_node_ids || !node_online(node_id)))\n 729:\t\t\treturn 0;\n 730:\t\n 731:\t\tif (page_cnt \u003e page_cnt_max)\n 732:\t\t\treturn 0;\n 733:\t\n 734:\t\tif (uaddr) {\n 735:\t\t\tif (uaddr \u0026 ~PAGE_MASK)\n 736:\t\t\t\treturn 0;\n 737:\t\t\tpgoff = compute_pgoff(arena, uaddr);\n 738:\t\t\tif (pgoff \u003e page_cnt_max - page_cnt)\n 739:\t\t\t\t/* requested address will be outside of user VMA */\n 740:\t\t\t\treturn 0;\n 741:\t\t}\n 742:\t\n 743:\t\tbpf_map_memcg_enter(\u0026arena-\u003emap, \u0026old_memcg, \u0026new_memcg);\n 744:\t\t/* Cap allocation size to KMALLOC_MAX_CACHE_SIZE so kmalloc_nolock() can succeed. */\n 745:\t\talloc_pages = min(page_cnt, KMALLOC_MAX_CACHE_SIZE / sizeof(struct page *));\n 746:\t\tpages = kmalloc_nolock(alloc_pages * sizeof(struct page *), __GFP_ACCOUNT, NUMA_NO_NODE);\n 747:\t\tif (!pages) {\n 748:\t\t\tbpf_map_memcg_exit(old_memcg, new_memcg);\n 749:\t\t\treturn 0;\n 750:\t\t}\n"
} |
| 83/3 | 2026/10/03 02:40 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 98488 output: 36 thoughts: 24 Thoughts: Okay, let's dive into this. Here's my take on those lines of `kernel/bpf/arena.c`: **Analyzing Lines 751-800 of `kernel/bpf/arena.c`** Alright, lines 751-800, right? Immediately, my expert eye hones in on this section. I'm expecting to see operations related to memory management, likely within the BPF arena itself, so it's probably about allocation, deallocation, or tracking free blocks. Given the surrounding code, I'd bet this chunk concerns a specific aspect of how the BPF arena handles its internal structures. Is it iterating over the arena's block lists? Are there checks for alignment, boundary conditions, or potentially synchronization primitives? My gut tells me it's dealing with freeing an arena, and that involves some kind of iteration or checking to make sure it is safe to free the memory. Is this freeing of memory an operation on a BPF program, or the memory which stores the BPF programs? Are there checks for partially full or empty blocks, and if so, how are they handled? I'm also curious about the data structures being used here. Are we looking at a linked list, a bitmap, or something more specialized for the BPF arena? Knowing the data structures will help me get a read on the overall efficiency of the algorithm and whether potential bottlenecks could exist. I'll need to pay close attention to the pointer manipulations and any potential for memory corruption. Are there any `WARN_ON` calls or other error-handling mechanisms that might give insight into expected failure scenarios? I need to be careful to look out for common pitfalls, such as double-free issues or memory leaks. Let's dig in and see what's actually going on. |
| 84/3 | 2026/10/03 02:40 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/arena.c",
"FirstLine": 751,
"LineCount": 50
}Results: {
"Contents": " 751:\t\tdata.arena = arena;\n 752:\t\tdata.pages = pages;\n 753:\t\n 754:\t\tif (raw_res_spin_lock_irqsave(\u0026arena-\u003espinlock, flags))\n 755:\t\t\tgoto out_free_pages;\n 756:\t\n 757:\t\tif (uaddr) {\n 758:\t\t\tret = is_range_tree_set(\u0026arena-\u003ert, pgoff, page_cnt);\n 759:\t\t\tif (ret)\n 760:\t\t\t\tgoto out_unlock_free_pages;\n 761:\t\t\tret = range_tree_clear(\u0026arena-\u003ert, pgoff, page_cnt);\n 762:\t\t} else {\n 763:\t\t\tret = pgoff = range_tree_find(\u0026arena-\u003ert, page_cnt);\n 764:\t\t\tif (pgoff \u003e= 0)\n 765:\t\t\t\tret = range_tree_clear(\u0026arena-\u003ert, pgoff, page_cnt);\n 766:\t\t}\n 767:\t\tif (ret)\n 768:\t\t\tgoto out_unlock_free_pages;\n 769:\t\n 770:\t\tremaining = page_cnt;\n 771:\t\tuaddr32 = (u32)(arena-\u003euser_vm_start + pgoff * PAGE_SIZE);\n 772:\t\n 773:\t\twhile (remaining) {\n 774:\t\t\tlong this_batch = min(remaining, alloc_pages);\n 775:\t\n 776:\t\t\t/* zeroing is needed, since alloc_pages_bulk() only fills in non-zero entries */\n 777:\t\t\tmemset(pages, 0, this_batch * sizeof(struct page *));\n 778:\t\n 779:\t\t\tret = bpf_map_alloc_pages(\u0026arena-\u003emap, node_id, this_batch, pages);\n 780:\t\t\tif (ret)\n 781:\t\t\t\tgoto out;\n 782:\t\n 783:\t\t\t/*\n 784:\t\t\t * Earlier checks made sure that uaddr32 + page_cnt * PAGE_SIZE - 1\n 785:\t\t\t * will not overflow 32-bit. Lower 32-bit need to represent\n 786:\t\t\t * contiguous user address range.\n 787:\t\t\t * Map these pages at kern_vm_start base.\n 788:\t\t\t * kern_vm_start + uaddr32 + page_cnt * PAGE_SIZE - 1 can overflow\n 789:\t\t\t * lower 32-bit and it's ok.\n 790:\t\t\t */\n 791:\t\t\tdata.i = 0;\n 792:\t\t\tret = apply_to_page_range(\u0026init_mm,\n 793:\t\t\t\t\t\t kern_vm_start + uaddr32 + (mapped \u003c\u003c PAGE_SHIFT),\n 794:\t\t\t\t\t\t this_batch \u003c\u003c PAGE_SHIFT, apply_range_set_cb, \u0026data);\n 795:\t\t\tif (ret) {\n 796:\t\t\t\t/* data.i pages were mapped, account them and free the remaining */\n 797:\t\t\t\tmapped += data.i;\n 798:\t\t\t\tfor (i = data.i; i \u003c this_batch; i++)\n 799:\t\t\t\t\tfree_pages_nolock(pages[i], 0);\n 800:\t\t\t\tgoto out;\n"
} |
| 85/3 | 2026/10/03 02:40 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 99794 output: 33 thoughts: 41 Thoughts: Here's my take on the thought process, as if it were my own: **Investigating Memory Allocation in BPF Maps** Okay, so I need to understand how memory is allocated for BPF maps. The behavior I'm seeing suggests zeroed pages are involved. Let's dig into `bpf_map_alloc_pages` directly. I need to be sure that the allocation happens with flags like `GFP_USER | __GFP_ZERO` or equivalent. This will confirm the initialization method. I'll check its implementation to ensure the memory is cleared. |
| 86/3 | 2026/10/03 02:40 | tool | grepper |
0mArgs:{
"Expression": "bpf_map_alloc_pages",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/arena.c=266=static struct bpf_map *arena_map_alloc(union bpf_attr *attr)\n--\nkernel/bpf/arena.c-314-\nkernel/bpf/arena.c:315:\terr = bpf_map_alloc_pages(\u0026arena-\u003emap, NUMA_NO_NODE, 1, \u0026arena-\u003escratch_page);\nkernel/bpf/arena.c-316-\tif (err)\n--\nkernel/bpf/arena.c=479=static vm_fault_t arena_vm_fault(struct vm_fault *vmf)\n--\nkernel/bpf/arena.c-545-\t\t */\nkernel/bpf/arena.c:546:\t\tret = bpf_map_alloc_pages(map, map-\u003enuma_node, 1, \u0026new_page);\nkernel/bpf/arena.c-547-\t\tif (ret) {\n--\nkernel/bpf/arena.c=711=static long arena_alloc_pages(struct bpf_arena *arena, long uaddr, long page_cnt, int node_id,\n--\nkernel/bpf/arena.c-778-\nkernel/bpf/arena.c:779:\t\tret = bpf_map_alloc_pages(\u0026arena-\u003emap, node_id, this_batch, pages);\nkernel/bpf/arena.c-780-\t\tif (ret)\n--\nkernel/bpf/syscall.c=605=static struct page *__bpf_alloc_page(int nid)\n--\nkernel/bpf/syscall.c-615-\nkernel/bpf/syscall.c:616:int bpf_map_alloc_pages(const struct bpf_map *map, int nid,\nkernel/bpf/syscall.c-617-\t\t\tunsigned long nr_pages, struct page **pages)\n"
} |
| 87/3 | 2026/10/03 02:40 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 100465 output: 36 thoughts: 13 Thoughts: Okay, here's my interpretation of what's going on with `__bpf_alloc_page`, keeping in mind my expertise: **Diving into `__bpf_alloc_page`** Alright, let's dissect this function. Immediately, the name suggests that this is the core function for allocating a page within the BPF context. Given my experience with kernel memory management, I know a page allocation is fundamental for almost anything BPF is going to do. We're talking about providing the backing memory for BPF programs themselves, data maps, and possibly other internal structures. I'm assuming that it’s intended to be an internal helper, hence the double underscores. It would be good to confirm this assumption. This makes me suspect this isn’t a function I should call directly. This means, somewhere, another function is calling `__bpf_alloc_page`. This could be allocating memory for instruction pages, data pages, or maybe even something more specialized within the BPF subsystem. Let's see what more I can dig up. |
| 88/3 | 2026/10/03 02:41 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/syscall.c",
"FirstLine": 605,
"LineCount": 20
}Results: {
"Contents": " 605:\tstatic struct page *__bpf_alloc_page(int nid)\n 606:\t{\n 607:\t\tif (!can_alloc_pages())\n 608:\t\t\treturn alloc_pages_nolock(__GFP_ACCOUNT, nid, 0);\n 609:\t\n 610:\t\treturn alloc_pages_node(nid,\n 611:\t\t\t\t\tGFP_KERNEL | __GFP_ZERO | __GFP_ACCOUNT\n 612:\t\t\t\t\t| __GFP_NOWARN,\n 613:\t\t\t\t\t0);\n 614:\t}\n 615:\t\n 616:\tint bpf_map_alloc_pages(const struct bpf_map *map, int nid,\n 617:\t\t\t\tunsigned long nr_pages, struct page **pages)\n 618:\t{\n 619:\t\tunsigned long i, j;\n 620:\t\tstruct page *pg;\n 621:\t\tint ret = 0;\n 622:\t\n 623:\t\tfor (i = 0; i \u003c nr_pages; i++) {\n 624:\t\t\tpg = __bpf_alloc_page(nid);\n"
} |
| 89/3 | 2026/10/03 02:41 | llm | kmsan-evaluator |
1mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 101042 output: 31 thoughts: 59 Thoughts: Okay, here's my interpretation of this thought process, keeping in mind my expertise: **Diving into BPF Page Allocation** Alright, so I'm digging into `__bpf_alloc_page`. The flow looks pretty straightforward, but I've hit a point where allocation is conditional. When `can_alloc_pages()` returns false, it falls back to calling `alloc_pages_nolock(__GFP_ACCOUNT, nid, 0)`. My immediate thought is, "Okay, that's interesting... let's see what happens during allocation. Is the page zeroed upon allocation or is zeroing handled elsewhere?" The key question here, the one I need to answer right now, is whether `alloc_pages_nolock` performs a zeroing operation itself. I need to understand that to accurately assess the memory behavior. |
| 90/3 | 2026/10/03 02:42 | tool | grepper |
0mArgs:{
"Expression": "alloc_pages_nolock",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/syscall.c=605=static struct page *__bpf_alloc_page(int nid)\n--\nkernel/bpf/syscall.c-607-\tif (!can_alloc_pages())\nkernel/bpf/syscall.c:608:\t\treturn alloc_pages_nolock(__GFP_ACCOUNT, nid, 0);\nkernel/bpf/syscall.c-609-\n"
} |
| 91/3 | 2026/10/03 02:42 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 101422 output: 28 thoughts: 16 Thoughts: Okay, let's dive into this code. **Investigating `alloc_pages_nolock`** Alright, my immediate thought is to trace the definition of `alloc_pages_nolock`. I need to understand its exact signature, how it's implemented, and what other functions call it. Understanding its context will greatly improve my ability to understand a code base. Knowing this function is defined will allow me to explore how the function will operate within a more complete context. |
| 92/3 | 2026/10/03 02:42 | tool | grepper |
0mArgs:{
"Expression": "alloc_pages_nolock",
"PathPrefix": "mm/"
}Results: {
"Output": "mm/page_alloc.c=5343=static inline bool alloc_nolock_allowed(void)\n--\nmm/page_alloc.c-5355-/*\nmm/page_alloc.c:5356: * GFP flags to set for ALLOC_NOLOCK i.e. alloc_pages_nolock().\nmm/page_alloc.c-5357- *\n--\nmm/page_alloc.c-5363- *\nmm/page_alloc.c:5364: * Specify __GFP_NOWARN since failing alloc_pages_nolock() is not a reason\nmm/page_alloc.c-5365- * to warn. Also warn would trigger printk() which is unsafe from\n--\nmm/page_alloc.c-5373- * Though __GFP_NOMEMALLOC is not checked in the code path below,\nmm/page_alloc.c:5374: * specify it here to highlight that alloc_pages_nolock()\nmm/page_alloc.c-5375- * doesn't want to deplete reserves.\n--\nmm/page_alloc.c=5569=EXPORT_SYMBOL(__free_pages);\n--\nmm/page_alloc.c-5572- * Can be called while holding raw_spin_lock or from IRQ and NMI for any\nmm/page_alloc.c:5573: * page type (not only those that came from alloc_pages_nolock)\nmm/page_alloc.c-5574- */\n--\nmm/page_alloc.c=8041=struct page *alloc_frozen_pages_nolock_noprof(gfp_t gfp_flags, int nid, unsigned int order)\n--\nmm/page_alloc.c-8048-/**\nmm/page_alloc.c:8049: * alloc_pages_nolock - opportunistic reentrant allocation from any context\nmm/page_alloc.c-8050- * @gfp_flags: GFP flags. Only __GFP_ACCOUNT, plus some flags that get set\n--\nmm/page_alloc.c-8056- * call from any context where RCU is watching (from atomic, NMI, and also\nmm/page_alloc.c:8057: * reentrant allocator -\u003e tracepoint -\u003e alloc_pages_nolock_noprof).\nmm/page_alloc.c-8058- * Allocation is best effort and to be expected to fail easily so nobody should\n--\nmm/page_alloc.c-8064- */\nmm/page_alloc.c:8065:struct page *alloc_pages_nolock_noprof(gfp_t gfp_flags, int nid, unsigned int order)\nmm/page_alloc.c-8066-{\n--\nmm/page_alloc.c-8073-}\nmm/page_alloc.c:8074:EXPORT_SYMBOL_GPL(alloc_pages_nolock_noprof);\n--\nmm/page_owner.c=316=void __reset_page_owner(struct page *page, unsigned short order)\n--\nmm/page_owner.c-334-\t * to prevent issues in stack_depot_save().\nmm/page_owner.c:335:\t * This is similar to alloc_pages_nolock() gfp flags, but only used\nmm/page_owner.c-336-\t * to signal stack_depot to avoid spin_locks.\n"
} |
| 93/3 | 2026/10/03 02:42 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 102413 output: 36 thoughts: 33 Thoughts: Here's a summary of my immediate thought process, focusing on the provided line of code: **Diving into `alloc_pages_nolock_noprof`** Okay, so I need to understand something related to page allocation and, as an expert, my first instinct is to go directly to the source. Let's start by examining the `alloc_pages_nolock_noprof` function. I recall it's in `mm/page_alloc.c`, and based on the context, I'm likely interested in line 8060. Let's see what the code at that location reveals about the specific aspects of page allocation this function handles. I'm expecting something nuanced, given the "nolock" and "noprof" qualifiers in the function name. Likely something to do with performance or concurrency considerations, perhaps a more specialized path for page allocation. |
| 94/3 | 2026/10/03 02:42 | tool | read-file |
0mArgs:{
"File": "mm/page_alloc.c",
"FirstLine": 8060,
"LineCount": 20
}Results: {
"Contents": "8060:\t * See always fail conditions below.\n8061:\t *\n8062:\t * Return: allocated page or NULL on failure. NULL does not mean EBUSY or EAGAIN.\n8063:\t * It means ENOMEM. There is no reason to call it again and expect !NULL.\n8064:\t */\n8065:\tstruct page *alloc_pages_nolock_noprof(gfp_t gfp_flags, int nid, unsigned int order)\n8066:\t{\n8067:\t\tstruct page *page;\n8068:\t\n8069:\t\tpage = alloc_frozen_pages_nolock_noprof(gfp_flags, nid, order);\n8070:\t\tif (page)\n8071:\t\t\tset_page_refcounted(page);\n8072:\t\treturn page;\n8073:\t}\n8074:\tEXPORT_SYMBOL_GPL(alloc_pages_nolock_noprof);\n"
} |
| 95/3 | 2026/10/03 02:42 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 102934 output: 36 thoughts: 15 Thoughts: Okay, here's my summary, in the first person, as if these are my thoughts, keeping in mind I'm dealing with `alloc_frozen_pages_nolock_noprof`: **Diving Deep into Frozen Pages (Without the Lock, and Ignoring Profiling)** Alright, so I'm looking at `alloc_frozen_pages_nolock_noprof`. Right off the bat, that name tells me a lot. We're talking about allocating pages that are going to be "frozen," meaning they're likely going to be treated in a very specific, optimized manner, probably for efficiency or determinism. The "nolock" part? That's the real kicker. It means we're *not* using a traditional locking mechanism for this allocation. This screams performance. We're trading off the safety of a lock for speed, implying that this code path is either highly trusted, operating in a context where locking is unnecessary or impractical, or perhaps relies on carefully crafted atomics or other synchronization primitives. And the "noprof" suffix? Okay, so we're choosing *not* to include profiling information. This is something I'll need to keep in mind, as it means this particular allocation path is likely not going to be easily instrumented for performance analysis. Interesting. We're optimizing for speed here. I'm instantly thinking about the trade-offs involved. No lock implies potentially reduced overhead, but it also means we need to be incredibly careful about race conditions and concurrent access. What's the context here? Are we in an interrupt handler? A real-time system? Does this code interact with other parts of the memory management system, and if so, how is the synchronization handled elsewhere? This also tells me that this is likely a critical section of code, and that I'm going to have to carefully analyze how this function interacts with memory allocation on a macro scale. Ignoring profiling information means the performance metrics are going to require a more involved inspection. We'll need to use other methods to evaluate performance, such as looking for critical sections in other code, or using manual timers. I'm also curious about what "frozen" actually *means* in this context. Is it about preventing page migration? Is it related to caching behavior? Is this some sort of memory protection scheme? The implications are huge. |
| 96/3 | 2026/10/03 02:42 | tool | read-file |
0mArgs:{
"File": "mm/page_alloc.c",
"FirstLine": 8040,
"LineCount": 20
}Results: {
"Contents": "8040:\t\n8041:\tstruct page *alloc_frozen_pages_nolock_noprof(gfp_t gfp_flags, int nid, unsigned int order)\n8042:\t{\n8043:\t\tif (nid == NUMA_NO_NODE)\n8044:\t\t\tnid = numa_node_id();\n8045:\t\n8046:\t\treturn __alloc_frozen_pages_noprof(gfp_flags, order, nid, NULL, ALLOC_NOLOCK);\n8047:\t}\n8048:\t/**\n8049:\t * alloc_pages_nolock - opportunistic reentrant allocation from any context\n8050:\t * @gfp_flags: GFP flags. Only __GFP_ACCOUNT, plus some flags that get set\n8051:\t * internally regardless (see %gfp_nolock) are allowed.\n8052:\t * @nid: node to allocate from\n8053:\t * @order: allocation order size\n8054:\t *\n8055:\t * Allocates pages of a given order from the given node. This is safe to\n8056:\t * call from any context where RCU is watching (from atomic, NMI, and also\n8057:\t * reentrant allocator -\u003e tracepoint -\u003e alloc_pages_nolock_noprof).\n8058:\t * Allocation is best effort and to be expected to fail easily so nobody should\n8059:\t * rely on the success. Failures are not reported via warn_alloc().\n"
} |
| 97/3 | 2026/10/03 02:42 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 103870 output: 445 thoughts: 1965 Thoughts: **Analysis of BPF Arena Scalar Implementation for Rust Support** Okay, let's break this down. My initial observation is that arena pages are always zeroed during allocation; the existing `bpf_map_alloc_pages` logic hasn't changed regarding the zero-initialization. This is good; we're starting from a safe baseline. Now, this patch introduces Rust support for BPF programs, primarily through the concept of "arena scalars." Rust, lacking distinct address spaces, represents arena pointers as plain numbers. The core of this functionality lies in how the verifier handles the `BPF_F_ARENA_SCALAR` flag. When this flag is set, the verifier, upon encountering memory loads and stores via a scalar register, recognizes them as arena accesses, provided there's an associated arena map. This involves marking instructions with `arena_scalar = true` in `insn_aux_data` and later rewriting the instruction to use `BPF_REG_AX`, which the JIT then translates into an arena memory access. The patch also includes several BTF (BPF Type Format) changes to accommodate Rust's naming conventions and compiler behaviors. These changes allow printable characters in BTF identifiers, permit variables in BTF datasec to be smaller than their declared types (which addresses Rust's variable splitting during compilation), and ignores missing argument names for static functions. Furthermore, it sanitizes ksym names for BPF programs with Rust symbols. The other major component of this patch involves changes to pointer arithmetic within the verifier. Now, we're allowing non-ADD/SUB ALU operations on pointers if `env->allow_ptr_leaks` is set, by converting the register type to `SCALAR_VALUE`. The verifier marks the destination register as unknown using `__mark_reg_unknown()`. My primary concern here is the potential for uninitialized memory reads. But, if a scalar's value is unknown in the verifier, at runtime the BPF program performs the ALU operation on the pointer, which is an address. The result is a scalar value that accesses the arena VM mapping, which is either backed by zeroed arena pages or triggers an exception table fixup that zeroes the destination register on load fault, or ignores writes. No kernel stack or heap memory outside of the arena can be accessed through arena access. Importantly, the `arena` stack variable in `mem_access_reg` is initialized with `memset`, and `arena->type` is set to `PTR_TO_ARENA`. In `arena_scalar_access`, the fixup instructions are completely initialized. In `bpf_prog_ksym_set_name`, sanitization is applied. In `btf_datasec_resolve`, there is no way for the code to read uninitialized memory. Finally, `btf_var_is_piece` will error out early. I've re-examined the code closely. Could KASAN vs. KMSAN applicability be relevant here? * Standard KASAN is used to detect out of bounds access, or use after free vulnerabilities. * KMSAN is designed to detect reads of uninitialized memory and information leaks to user space. After detailed examination of every aspect of this patch, I can confidently say that these changes do not introduce or expose any new risks regarding uninitialized memory. Any potential issues are related to verifier logic errors, incorrect patching, or potentially out-of-bounds arena accesses – which are memory safety/bounds issues, handled via BPF exception fixups or KASAN, rather than uninitialized memory issues. Temporary structures like `arena` and `insn_buf` are explicitly initialized with `memset` or field assignments. Verifier internal arrays are allocated with `__GFP_ZERO`. Arena pages are also allocated with `__GFP_ZERO` or `clear_highpage`. Let's double-check all aspects: arm64 and x86 JIT comp, bpf_verifier.h and other header files, btf.c, core.c, fixups.c, syscall.c, verifier.c, and tools/... These are all clean. Therefore, we can set `NeedsKMSAN = false`. KMSAN is not necessary here because the changes are not related to uninitialized memory. KMSAN's focus is on detecting reads of uninitialized memory and information leaks to userspace. In contrast, this patch affects the BPF verifier and JIT fixups to support arena operations with scalar values, and BTF validation improvements. Any bugs here would manifest as logic errors in the verifier, incorrect instruction patching, or out-of-bounds arena accesses (caught by KASAN or exception fixups). The introduced kernel structures are fully initialized, with buffers initialized using `memset` and allocated with `__GFP_ZERO`. No uninitialized memory is read or copied |
| 98/3 | 2026/10/03 02:42 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch series adds support for BPF_F_ARENA_SCALAR to allow BPF programs (particularly those compiled from Rust) to access BPF arena memory using scalar values, updates BTF parsing/validation for Rust identifiers and split variables, and enables arithmetic operations on pointers to degrade to scalar values when pointer leaks are permitted.\n\nFrom a memory sanitization perspective:\n1. KMSAN applicability: KMSAN detects uses of uninitialized memory (stack, heap, or page allocations) and leaks of uninitialized memory to user space.\n - The temporary stack structure `struct bpf_reg_state arena` introduced in `verifier.c` (`mem_access_reg()`) is explicitly zeroed with `memset(arena, 0, sizeof(*arena))`.\n - `env-\u003einsn_aux_data` is allocated with `__GFP_ZERO` / `vrealloc` with `__GFP_ZERO`.\n - Instruction patches generated in `arena_scalar_access()` fully initialize all instruction fields in `insn_buf`.\n - In `btf.c`, `btf_var_is_piece()` relies on `btf_resolve_size()`, where `size` is only evaluated when `btf_resolve_size()` succeeds; and `btf_datasec_show()` skips pieces to avoid reads beyond section bounds.\n - BPF arena memory pages are allocated zero-initialized.\n - No uninitialized data or struct padding is copied to user space.\n2. KASAN and general bug detectors: Any potential issues arising from this series would involve verifier state logic, bounds checking, or JIT instruction rewriting, which fall under the purview of standard functional testing, KASAN (for out-of-bounds memory accesses), and verifier invariants checks, rather than uninitialized memory tracking.\n\nConsequently, there is no risk of uninitialized memory access or info-leaks, and a dedicated KMSAN fuzzing session is not justified.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch series adds support for BPF_F_ARENA_SCALAR to allow BPF programs (particularly those compiled from Rust) to access BPF arena memory using scalar values, updates BTF parsing/validation for Rust identifiers and split variables, and enables arithmetic operations on pointers to degrade to scalar values when pointer leaks are permitted.\n\nFrom a memory sanitization perspective:\n1. KMSAN applicability: KMSAN detects uses of uninitialized memory (stack, heap, or page allocations) and leaks of uninitialized memory to user space.\n - The temporary stack structure `struct bpf_reg_state arena` introduced in `verifier.c` (`mem_access_reg()`) is explicitly zeroed with `memset(arena, 0, sizeof(*arena))`.\n - `env-\u003einsn_aux_data` is allocated with `__GFP_ZERO` / `vrealloc` with `__GFP_ZERO`.\n - Instruction patches generated in `arena_scalar_access()` fully initialize all instruction fields in `insn_buf`.\n - In `btf.c`, `btf_var_is_piece()` relies on `btf_resolve_size()`, where `size` is only evaluated when `btf_resolve_size()` succeeds; and `btf_datasec_show()` skips pieces to avoid reads beyond section bounds.\n - BPF arena memory pages are allocated zero-initialized.\n - No uninitialized data or struct padding is copied to user space.\n2. KASAN and general bug detectors: Any potential issues arising from this series would involve verifier state logic, bounds checking, or JIT instruction rewriting, which fall under the purview of standard functional testing, KASAN (for out-of-bounds memory accesses), and verifier invariants checks, rather than uninitialized memory tracking.\n\nConsequently, there is no risk of uninitialized memory access or info-leaks, and a dedicated KMSAN fuzzing session is not justified.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|