| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/09/26 22:10 | flow | patch-triage |
4mArgs:null Results: {
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT"
],
"FocusSymbols": [
"bpf_jit_supports_large_stack",
"bpf_id_scratch_reserve",
"bpf_push_jmp_history",
"bpf_mark_chain_precision",
"bpf_compute_subprog_arg_access",
"check_max_stack_depth_subprog",
"check_stack_write_fixed_off",
"check_stack_read_fixed_off",
"stacksafe"
],
"KMSANReasoning": "The patch series increases the maximum BPF stack limit from 512 bytes to 2048 bytes on architectures with JIT support for large stacks (x86-64 and arm64). The verifier changes update internal tracking data structures to accommodate 256 stack slots (from 64), replacing u64 bitmasks with dynamically widened or statically sized bitmaps (e.g., in backtrack_state, frame_masks, and scratched_stack_slots), dynamically growing scratch arrays for id mapping (bpf_idmap, bpf_idset), and adjusting bounds checks.\n\nContrasting KASAN vs. KMSAN applicability:\n- KMSAN applicability: KMSAN detects use of uninitialized memory and info-leaks to user space. In this patch series:\n 1. All new or modified data structures allocated on heap/stack are fully initialized: `frame_masks` is allocated via `kvzalloc_flex`, local arrays like `new_after` are zero-initialized (`= {}`), `at_stack_in` is fully populated with `unvisited` markers, `spill_snapshot` is populated with `memcpy`, and dynamically grown entries in `bpf_idmap`/`bpf_idset` have their fields (`old`, `cur`, `id`, `cnt`) explicitly set upon growth.\n 2. In `bpf_jmp_history_entry`, all fields are explicitly assigned (`idx`, `prev_idx`, `flags`, `spi`, `frame`, `linked_regs_cnt`), and `linked_regs` is copied and accessed strictly up to `linked_regs_cnt`.\n 3. No kernel-to-user information leaks are introduced; verifier logging to user buffers is formatted strings with null terminators.\n 4. The BPF stack initialization rules enforced by the verifier (disallowing reads from uninitialized slots unless permitted by CAP_PERFMON token) remain unchanged in semantics, and the interpreter stack remains at 512 bytes with its explicit KMSAN unpoisoning intact.\n- KASAN applicability: The risks associated with this patch primarily concern potential out-of-bounds accesses or buffer overruns due to bitmap indexing, array bounds, slice reallocations, or kernel stack exhaustion (checked via KASAN, FORTIFY_SOURCE, and VMAP_STACK).\n\nSince no uninitialized memory risks or kernel-to-user info-leaks are introduced or exposed, dedicated KMSAN fuzzing is not warranted.",
"NeedsKMSAN": false,
"Reasoning": "The patch increases the maximum BPF program stack size from 512 bytes to 2048 bytes (MAX_BPF_STACK_JIT) on architectures with JIT support (including x86-64). This introduces widespread alterations across core BPF subsystem files (verifier.c, liveness.c, backtrack.c, states.c) to handle larger stack limits: changing fixed 64-bit stack masks to multi-word bitmaps, dynamically widening frame masks in liveness tracking, repacking jump history entries for linked registers and slots, dynamically allocating scratch tables for verifier state equivalence checks, and adjusting stack depth checks and pointer arithmetic bounds. These changes modify critical security invariants and verifier state pruning logic reachable from unprivileged/privileged BPF program loading.",
"WorthFuzzing": true
} |
| 1/1 | 2026/09/26 22:10 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit 6d73c2266fed3339a271d57f99edbf7dc11e3ac3\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Sat Sep 26 22:10:37 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/Documentation/bpf/bpf_design_QA.rst b/Documentation/bpf/bpf_design_QA.rst\nindex eb19c945f4d57..98d70b8a2533b 100644\n--- a/Documentation/bpf/bpf_design_QA.rst\n+++ b/Documentation/bpf/bpf_design_QA.rst\n@@ -221,9 +221,14 @@ newer kernels. BPF programs need to change accordingly when this happens.\n \n Q: How much stack space a BPF program uses?\n -------------------------------------------\n-A: Currently all program types are limited to 512 bytes of stack\n-space, but the verifier computes the actual amount of stack used\n-and both interpreter and most JITed code consume necessary amount.\n+A: A program may use up to 2 KiB of stack, combined over its call\n+chain, when the JIT of the architecture reports support for large\n+stacks (currently x86-64 and arm64); a single function may use all of\n+it, and every frame of a program running on a private stack gets the\n+whole amount. Elsewhere, and for programs verified for the interpreter,\n+the limit is 512 bytes; the interpreter itself never runs a frame\n+larger than that. The verifier computes the actual amount of stack\n+used and both interpreter and most JITed code consume necessary amount.\n \n Q: Can BPF be offloaded to HW?\n ------------------------------\ndiff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c\nindex 9544b2f483e59..2b2d6d178ea1d 100644\n--- a/arch/arm64/net/bpf_jit_comp.c\n+++ b/arch/arm64/net/bpf_jit_comp.c\n@@ -2501,6 +2501,11 @@ bool bpf_jit_supports_callx(void)\n \treturn true;\n }\n \n+bool bpf_jit_supports_large_stack(void)\n+{\n+\treturn true;\n+}\n+\n static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_node *node,\n \t\t\t int bargs_off, int retval_off, int run_ctx_off,\n \t\t\t bool save_ret)\ndiff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c\nindex 9fbef7504e51a..e2e531dd1e0bc 100644\n--- a/arch/x86/net/bpf_jit_comp.c\n+++ b/arch/x86/net/bpf_jit_comp.c\n@@ -4520,6 +4520,18 @@ bool bpf_jit_supports_subprog_tailcalls(void)\n \treturn true;\n }\n \n+/*\n+ * Frame sizes are 32-bit immediates in the prologue, epilogue and tail call\n+ * sequences, a tail call pops the caller's frame and lands in the target's\n+ * prologue before the target allocates its own, and private stacks are\n+ * allocated from the program's own depth, so MAX_BPF_STACK_JIT frames need\n+ * nothing special.\n+ */\n+bool bpf_jit_supports_large_stack(void)\n+{\n+\treturn true;\n+}\n+\n bool bpf_jit_supports_percpu_insn(void)\n {\n \treturn true;\ndiff --git a/drivers/net/ethernet/netronome/nfp/bpf/verifier.c b/drivers/net/ethernet/netronome/nfp/bpf/verifier.c\nindex 1caa87da72b51..9ddbfe9d60194 100644\n--- a/drivers/net/ethernet/netronome/nfp/bpf/verifier.c\n+++ b/drivers/net/ethernet/netronome/nfp/bpf/verifier.c\n@@ -105,7 +105,7 @@ static bool nfp_bpf_map_update_value_ok(struct bpf_verifier_env *env)\n \t\tunsigned int soff;\n \n \t\tsoff = -(off + i) - 1;\n-\t\tstack_entry = \u0026state-\u003estack[soff / BPF_REG_SIZE];\n+\t\tstack_entry = bpf_stack_slot(state, soff / BPF_REG_SIZE);\n \t\tif (stack_entry-\u003eslot_type[soff % BPF_REG_SIZE] == STACK_ZERO)\n \t\t\tcontinue;\n \ndiff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h\nindex 578bccf5cf767..f151feeaf3a55 100644\n--- a/include/linux/bpf_verifier.h\n+++ b/include/linux/bpf_verifier.h\n@@ -19,11 +19,12 @@\n * that converting umax_value to int cannot overflow.\n */\n #define BPF_MAX_VAR_SIZ\t(1 \u003c\u003c 29)\n-/* size of tmp_str_buf in bpf_verifier.\n- * we need at least 306 bytes to fit full stack mask representation\n- * (in the \"-8,-16,...,-512\" form)\n+/*\n+ * size of tmp_str_buf in bpf_verifier.\n+ * we need at least 1399 bytes to fit full stack mask representation\n+ * (in the \"-8,-16,...,-2048\" form)\n */\n-#define TMP_STR_BUF_LEN 320\n+#define TMP_STR_BUF_LEN 1408\n /* Patch buffer size */\n #define INSN_BUF_SIZE 32\n \n@@ -242,55 +243,18 @@ enum bpf_stack_slot_type {\n \n #define BPF_REG_SIZE 8\t/* size of eBPF register in bytes */\n \n+/*\n+ * Largest number of BPF_REG_SIZE stack slots a single frame can have, sized\n+ * for the largest stack budget any JIT supports. A frame may use any part of\n+ * its program's budget; check_max_stack_depth() enforces the budget on the\n+ * combined depth of frames sharing the kernel stack and on each frame using\n+ * a private stack.\n+ */\n+#define MAX_BPF_STACK_SLOTS\t(MAX_BPF_STACK_JIT / BPF_REG_SIZE)\n+\n /* 4-byte stack slot granularity for liveness analysis */\n #define BPF_HALF_REG_SIZE\t4\n #define STACK_SLOT_SZ\t\t4\n-#define STACK_SLOTS\t\t(MAX_BPF_STACK / BPF_HALF_REG_SIZE)\t/* 128 */\n-\n-typedef struct {\n-\tu64 v[2];\n-} spis_t;\n-\n-#define SPIS_ZERO\t((spis_t){})\n-#define SPIS_ALL\t((spis_t){{ U64_MAX, U64_MAX }})\n-\n-static inline bool spis_is_zero(spis_t s)\n-{\n-\treturn s.v[0] == 0 \u0026\u0026 s.v[1] == 0;\n-}\n-\n-static inline bool spis_equal(spis_t a, spis_t b)\n-{\n-\treturn a.v[0] == b.v[0] \u0026\u0026 a.v[1] == b.v[1];\n-}\n-\n-static inline spis_t spis_or(spis_t a, spis_t b)\n-{\n-\treturn (spis_t){{ a.v[0] | b.v[0], a.v[1] | b.v[1] }};\n-}\n-\n-static inline spis_t spis_and(spis_t a, spis_t b)\n-{\n-\treturn (spis_t){{ a.v[0] \u0026 b.v[0], a.v[1] \u0026 b.v[1] }};\n-}\n-\n-static inline spis_t spis_not(spis_t s)\n-{\n-\treturn (spis_t){{ ~s.v[0], ~s.v[1] }};\n-}\n-\n-static inline bool spis_test_bit(spis_t s, u32 slot)\n-{\n-\treturn s.v[slot / 64] \u0026 BIT_ULL(slot % 64);\n-}\n-\n-static inline void spis_or_range(spis_t *mask, u32 lo, u32 hi)\n-{\n-\tu32 w;\n-\n-\tfor (w = lo; w \u003c= hi \u0026\u0026 w \u003c STACK_SLOTS; w++)\n-\t\tmask-\u003ev[w / 64] |= BIT_ULL(w % 64);\n-}\n \n #define BPF_REGMASK_ARGS ((1 \u003c\u003c BPF_REG_1) | (1 \u003c\u003c BPF_REG_2) | \\\n \t\t\t (1 \u003c\u003c BPF_REG_3) | (1 \u003c\u003c BPF_REG_4) | \\\n@@ -420,30 +384,33 @@ enum {\n \tINSN_F_STACK_ARG_ACCESS = BIT(3),\n };\n \n+/* Registers linked to one jump condition that a history entry can record */\n+#define BPF_LINKED_REGS_MAX\t5\n+\n struct bpf_jmp_history_entry {\n \t/* insn idx can't be bigger than 1 million */\n \tu32 idx : 20;\n \tu32 frame : 4;\t/* stack access frame number */\n-\tu32 spi : 6;\t/* stack slot index (0..63) */\n-\tu32 : 2;\n-\tu32 prev_idx : 20;\n \t/* special INSN_F_xxx flags */\n \tu32 flags : 4;\n-\tu32 : 8;\n+\tu32 : 4;\n+\tu32 prev_idx : 20;\n+\tu32 spi : 12;\t/* stack slot index */\n \t/*\n-\t * additional registers that need precision tracking when this\n-\t * jump is backtracked, vector of five 11-bit records\n+\t * Scalar registers and spilled scalars linked to the condition of\n+\t * this jump, which need precision tracking together when the jump is\n+\t * backtracked. Each is packed as 4 bits of frame number, one bit\n+\t * telling a register from a stack slot and 11 bits of register or\n+\t * slot index, see linked_regs_pack().\n \t */\n-\tu64 linked_regs;\n+\tu16 linked_regs[BPF_LINKED_REGS_MAX];\n+\tu8 linked_regs_cnt;\n };\n \n static_assert(MAX_CALL_FRAMES \u003c= (1 \u003c\u003c 4));\n-static_assert(MAX_BPF_STACK / 8 \u003c= (1 \u003c\u003c 6));\n+static_assert(MAX_BPF_STACK_SLOTS \u003c= (1 \u003c\u003c 12));\n \n-/* Maximum number of bpf_reg_state objects that can exist at once */\n #define MAX_STACK_ARG_SLOTS (MAX_BPF_FUNC_ARGS - MAX_BPF_FUNC_REG_ARGS)\n-#define BPF_ID_MAP_SIZE ((MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE + \\\n-\t\t\t MAX_STACK_ARG_SLOTS) * MAX_CALL_FRAMES)\n struct bpf_verifier_state {\n \t/* call stack tracking */\n \tstruct bpf_func_state *frame[MAX_CALL_FRAMES];\n@@ -529,12 +496,31 @@ struct bpf_verifier_state {\n \tu32 may_goto_depth;\n };\n \n+/* Number of BPF_REG_SIZE stack slots tracked for the frame so far. */\n+static inline u32 bpf_stack_nr_slots(const struct bpf_func_state *frame)\n+{\n+\treturn frame-\u003eallocated_stack / BPF_REG_SIZE;\n+}\n+\n+/*\n+ * Stack slot @spi of @frame, covering bytes [fp - (spi + 1) * 8, fp - spi * 8).\n+ * The caller must ensure spi \u003c bpf_stack_nr_slots(frame), see grow_stack_state().\n+ */\n+static inline struct bpf_stack_state *bpf_stack_slot(const struct bpf_func_state *frame, u32 spi)\n+{\n+\treturn \u0026frame-\u003estack[spi];\n+}\n+\n static inline struct bpf_reg_state *\n bpf_get_spilled_reg(int slot, struct bpf_func_state *frame, u32 mask)\n {\n-\tif (slot \u003c frame-\u003eallocated_stack / BPF_REG_SIZE \u0026\u0026\n-\t (1 \u003c\u003c frame-\u003estack[slot].slot_type[BPF_REG_SIZE - 1]) \u0026 mask)\n-\t\treturn \u0026frame-\u003estack[slot].spilled_ptr;\n+\tstruct bpf_stack_state *ss;\n+\n+\tif (slot \u003e= bpf_stack_nr_slots(frame))\n+\t\treturn NULL;\n+\tss = bpf_stack_slot(frame, slot);\n+\tif ((1 \u003c\u003c ss-\u003eslot_type[BPF_REG_SIZE - 1]) \u0026 mask)\n+\t\treturn \u0026ss-\u003espilled_ptr;\n \treturn NULL;\n }\n \n@@ -550,7 +536,7 @@ bpf_get_spilled_stack_arg(int slot, struct bpf_func_state *frame)\n /* Iterate over 'frame', setting 'reg' to either NULL or a spilled register. */\n #define bpf_for_each_spilled_reg(iter, frame, reg, mask)\t\t\t\\\n \tfor (iter = 0, reg = bpf_get_spilled_reg(iter, frame, mask);\t\t\\\n-\t iter \u003c frame-\u003eallocated_stack / BPF_REG_SIZE;\t\t\\\n+\t iter \u003c bpf_stack_nr_slots(frame);\t\t\t\t\\\n \t iter++, reg = bpf_get_spilled_reg(iter, frame, mask))\n \n /* Iterate over 'frame', setting 'reg' to either NULL or a spilled stack arg. */\n@@ -575,7 +561,7 @@ bpf_get_spilled_stack_arg(int slot, struct bpf_func_state *frame)\n \t\t\tbpf_for_each_spilled_reg(___j, __state, __reg, __mask) { \\\n \t\t\t\tif (!__reg) \\\n \t\t\t\t\tcontinue; \\\n-\t\t\t\t__stack = \u0026__state-\u003estack[___j]; \\\n+\t\t\t\t__stack = bpf_stack_slot(__state, ___j); \\\n \t\t\t\t(void)(__expr); \\\n \t\t\t} \\\n \t\t\t__stack = NULL; \\\n@@ -733,7 +719,11 @@ struct bpf_insn_aux_data {\n #define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */\n #define MAX_USED_BTFS 64 /* max number of BTFs accessed by one BPF program */\n \n-#define BPF_VERIFIER_TMP_LOG_SIZE\t1024\n+/*\n+ * Longest line the verifier log can carry: a full stack mask of\n+ * MAX_BPF_STACK_SLOTS slots, see TMP_STR_BUF_LEN, plus its prefix.\n+ */\n+#define BPF_VERIFIER_TMP_LOG_SIZE\t2048\n \n struct bpf_verifier_log {\n \t/* Logical start and end positions of a \"log window\" of the verifier log.\n@@ -865,7 +855,7 @@ struct backtrack_state {\n \tstruct bpf_verifier_env *env;\n \tu32 frame;\n \tu32 reg_masks[MAX_CALL_FRAMES];\n-\tu64 stack_masks[MAX_CALL_FRAMES];\n+\tunsigned long stack_masks[MAX_CALL_FRAMES][BITS_TO_LONGS(MAX_BPF_STACK_SLOTS)];\n \tu8 stack_arg_masks[MAX_CALL_FRAMES];\n };\n \n@@ -874,18 +864,27 @@ struct bpf_id_pair {\n \tu32 cur;\n };\n \n+/*\n+ * Scratch map from the ids of one verifier state to those of another, also\n+ * used as a stack of ids. Grown on demand by bpf_id_scratch_reserve().\n+ */\n struct bpf_idmap {\n \tu32 tmp_id_gen;\n \tu32 cnt;\n-\tstruct bpf_id_pair map[BPF_ID_MAP_SIZE];\n+\tu32 cap;\n+\tstruct bpf_id_pair *map;\n };\n \n+struct bpf_idset_entry {\n+\tu32 id;\n+\tu32 cnt;\n+};\n+\n+/* Scratch set of ids with a use count each, grown on demand */\n struct bpf_idset {\n \tu32 num_ids;\n-\tstruct {\n-\t\tu32 id;\n-\t\tu32 cnt;\n-\t} entries[BPF_ID_MAP_SIZE];\n+\tu32 cap;\n+\tstruct bpf_idset_entry *entries;\n };\n \n /* see verifier.c:compute_scc_callchain() */\n@@ -994,10 +993,8 @@ struct bpf_verifier_env {\n \t * via callx. Allocated when the first such edge is recorded.\n \t */\n \tunsigned long *callx_edges;\n-\tunion {\n-\t\tstruct bpf_idmap idmap_scratch;\n-\t\tstruct bpf_idset idset_scratch;\n-\t};\n+\tstruct bpf_idmap idmap_scratch;\n+\tstruct bpf_idset idset_scratch;\n \tstruct {\n \t\tint *insn_state;\n \t\tint *insn_stack;\n@@ -1012,8 +1009,8 @@ struct bpf_verifier_env {\n \t} cfg;\n \tstruct backtrack_state bt;\n \tstruct bpf_jmp_history_entry *cur_hist_ent;\n-\t/* Per-callsite copy of parent's converged at_stack_in for cross-frame fills. */\n-\tstruct arg_track **callsite_at_stack;\n+\t/* Per-callsite snapshot of the parent's tracked spill slots for cross-frame fills. */\n+\tstruct spill_snapshot **callsite_at_stack;\n \tu32 pass_cnt; /* number of times do_check() was called */\n \tu32 subprog_cnt;\n \t/* number of instructions analyzed by the verifier */\n@@ -1022,6 +1019,8 @@ struct bpf_verifier_env {\n \tu32 prev_jmps_processed, jmps_processed;\n \t/* maximum combined stack depth */\n \tu32 max_stack_depth;\n+\t/* stack budget of the program, see bpf_prog_stack_limit() */\n+\tu32 stack_limit;\n \t/* total verification time */\n \tu64 verification_time;\n \t/* maximum number of verifier states kept in 'branching' instructions */\n@@ -1057,7 +1056,7 @@ struct bpf_verifier_env {\n \t */\n \tu32 scratched_regs;\n \t/* Same as scratched_regs but for stack slots */\n-\tu64 scratched_stack_slots;\n+\tDECLARE_BITMAP(scratched_stack_slots, MAX_BPF_STACK_SLOTS);\n \tu64 prev_log_pos, prev_insn_print_pos;\n \t/* buffer used to temporary hold constants as scalar registers */\n \tstruct bpf_reg_state fake_reg[1];\n@@ -1261,8 +1260,10 @@ int bpf_copy_verifier_state(struct bpf_verifier_state *dst_state,\n struct list_head *bpf_explored_state(struct bpf_verifier_env *env, int idx);\n void bpf_free_verifier_state(struct bpf_verifier_state *state, bool free_self);\n void bpf_free_backedges(struct bpf_scc_visit *visit);\n+bool bpf_id_scratch_reserve(void **arr, u32 *cap, u32 cnt, size_t elem_size);\n int bpf_push_jmp_history(struct bpf_verifier_env *env, struct bpf_verifier_state *cur,\n-\t\t\t int insn_flags, int spi, int frame, u64 linked_regs);\n+\t\t\t int insn_flags, int spi, int frame, const u16 *linked_regs,\n+\t\t\t u8 linked_regs_cnt);\n void bpf_bt_sync_linked_regs(struct backtrack_state *bt, struct bpf_jmp_history_entry *hist);\n void bpf_mark_reg_not_init(const struct bpf_verifier_env *env,\n \t\t\t struct bpf_reg_state *reg);\n@@ -1279,6 +1280,24 @@ static inline int bpf_get_spi(s32 off)\n \treturn (-off - 1) / BPF_REG_SIZE;\n }\n \n+/*\n+ * Stack a program may use in total: combined over the frames of a call\n+ * chain on the kernel stack, or per frame on a private stack. Any single\n+ * frame may reach that deep. Only a JIT that lays out such frames may go\n+ * beyond MAX_BPF_STACK, the interpreter's frame size, and only one whose\n+ * tail calls let the target set up its own frame: without subprogram\n+ * tail calls, do_misc_fixups() gives every program with tail calls a\n+ * MAX_BPF_STACK frame, which a deeper frame would overrun.\n+ */\n+static inline u32 bpf_prog_stack_limit(const struct bpf_prog *prog)\n+{\n+\t/* an offloaded program never runs on the host JIT, whatever it supports */\n+\tif (prog-\u003ejit_requested \u0026\u0026 !bpf_prog_is_offloaded(prog-\u003eaux) \u0026\u0026\n+\t bpf_jit_supports_large_stack() \u0026\u0026 bpf_jit_supports_subprog_tailcalls())\n+\t\treturn MAX_BPF_STACK_JIT;\n+\treturn MAX_BPF_STACK;\n+}\n+\n static inline struct bpf_func_state *bpf_func(struct bpf_verifier_env *env,\n \t\t\t\t\t const struct bpf_reg_state *reg)\n {\n@@ -1322,12 +1341,7 @@ static inline void bpf_bt_set_frame_reg(struct backtrack_state *bt, u32 frame, u\n \n static inline void bpf_bt_set_frame_slot(struct backtrack_state *bt, u32 frame, u32 slot)\n {\n-\tbt-\u003estack_masks[frame] |= 1ull \u003c\u003c slot;\n-}\n-\n-static inline void bpf_bt_set_frame_slot_mask(struct backtrack_state *bt, u32 frame, u64 mask)\n-{\n-\tbt-\u003estack_masks[frame] |= mask;\n+\t__set_bit(slot, bt-\u003estack_masks[frame]);\n }\n \n static inline void bt_set_frame_stack_arg_slot(struct backtrack_state *bt, u32 frame, u32 slot)\n@@ -1342,7 +1356,7 @@ static inline bool bt_is_frame_reg_set(struct backtrack_state *bt, u32 frame, u3\n \n static inline bool bt_is_frame_slot_set(struct backtrack_state *bt, u32 frame, u32 slot)\n {\n-\treturn bt-\u003estack_masks[frame] \u0026 (1ull \u003c\u003c slot);\n+\treturn test_bit(slot, bt-\u003estack_masks[frame]);\n }\n \n bool bpf_map_is_rdonly(const struct bpf_map *map);\n@@ -1480,7 +1494,7 @@ static inline void mark_reg_scratched(struct bpf_verifier_env *env, u32 regno)\n \n static inline void mark_stack_slot_scratched(struct bpf_verifier_env *env, u32 spi)\n {\n-\tenv-\u003escratched_stack_slots |= 1ULL \u003c\u003c spi;\n+\t__set_bit(spi, env-\u003escratched_stack_slots);\n }\n \n static inline bool reg_scratched(const struct bpf_verifier_env *env, u32 regno)\n@@ -1488,27 +1502,28 @@ static inline bool reg_scratched(const struct bpf_verifier_env *env, u32 regno)\n \treturn (env-\u003escratched_regs \u003e\u003e regno) \u0026 1;\n }\n \n-static inline bool stack_slot_scratched(const struct bpf_verifier_env *env, u64 regno)\n+static inline bool stack_slot_scratched(const struct bpf_verifier_env *env, u32 spi)\n {\n-\treturn (env-\u003escratched_stack_slots \u003e\u003e regno) \u0026 1;\n+\treturn test_bit(spi, env-\u003escratched_stack_slots);\n }\n \n static inline bool verifier_state_scratched(const struct bpf_verifier_env *env)\n {\n-\treturn env-\u003escratched_regs || env-\u003escratched_stack_slots;\n+\treturn env-\u003escratched_regs ||\n+\t !bitmap_empty(env-\u003escratched_stack_slots, MAX_BPF_STACK_SLOTS);\n }\n \n static inline void mark_verifier_state_clean(struct bpf_verifier_env *env)\n {\n \tenv-\u003escratched_regs = 0U;\n-\tenv-\u003escratched_stack_slots = 0ULL;\n+\tbitmap_zero(env-\u003escratched_stack_slots, MAX_BPF_STACK_SLOTS);\n }\n \n /* Used for printing the entire verifier state. */\n static inline void mark_verifier_state_scratched(struct bpf_verifier_env *env)\n {\n \tenv-\u003escratched_regs = ~0U;\n-\tenv-\u003escratched_stack_slots = ~0ULL;\n+\tbitmap_fill(env-\u003escratched_stack_slots, MAX_BPF_STACK_SLOTS);\n }\n \n static inline bool bpf_stack_narrow_access_ok(int off, int fill_size, int spill_size)\n@@ -1543,7 +1558,7 @@ struct bpf_subprog_info *bpf_find_containing_subprog(struct bpf_verifier_env *en\n const char *bpf_subprog_name(const struct bpf_verifier_env *env, int subprog);\n int bpf_jmp_offset(struct bpf_insn *insn);\n struct bpf_iarray *bpf_insn_successors(struct bpf_verifier_env *env, u32 idx);\n-void bpf_fmt_stack_mask(char *buf, ssize_t buf_sz, u64 stack_mask);\n+void bpf_fmt_stack_mask(char *buf, ssize_t buf_sz, const unsigned long *stack_mask);\n bool bpf_subprog_is_global(const struct bpf_verifier_env *env, int subprog);\n \n /* Kinds of member a by-value struct or union may be composed of. */\ndiff --git a/include/linux/filter.h b/include/linux/filter.h\nindex 4f0662e428970..e42eccb0990e6 100644\n--- a/include/linux/filter.h\n+++ b/include/linux/filter.h\n@@ -98,6 +98,11 @@ struct ctl_table_header;\n \n /* BPF program can access up to 512 bytes of stack space. */\n #define MAX_BPF_STACK\t512\n+/*\n+ * Stack budget of a program on a JIT that lays out frames of that size.\n+ * The interpreter and JITs without such support keep MAX_BPF_STACK.\n+ */\n+#define MAX_BPF_STACK_JIT\t2048\n \n /* Helper macros for filter block array initializers. */\n \n@@ -1247,6 +1252,7 @@ bool bpf_jit_supports_ptr_xchg(void);\n bool bpf_jit_supports_arena(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);\n bool bpf_jit_supports_timed_may_goto(void);\n bool bpf_jit_supports_fsession(void);\n \ndiff --git a/kernel/bpf/backtrack.c b/kernel/bpf/backtrack.c\nindex 4da99dec08184..0e38b9575328c 100644\n--- a/kernel/bpf/backtrack.c\n+++ b/kernel/bpf/backtrack.c\n@@ -9,7 +9,8 @@\n \n /* for any branch, call, exit record the history of jmps in the given state */\n int bpf_push_jmp_history(struct bpf_verifier_env *env, struct bpf_verifier_state *cur,\n-\t\t\t int insn_flags, int spi, int frame, u64 linked_regs)\n+\t\t\t int insn_flags, int spi, int frame, const u16 *linked_regs,\n+\t\t\t u8 linked_regs_cnt)\n {\n \tu32 cnt = cur-\u003ejmp_history_cnt;\n \tstruct bpf_jmp_history_entry *p;\n@@ -27,10 +28,13 @@ int bpf_push_jmp_history(struct bpf_verifier_env *env, struct bpf_verifier_state\n \t\tenv-\u003ecur_hist_ent-\u003eflags |= insn_flags;\n \t\tenv-\u003ecur_hist_ent-\u003espi = spi;\n \t\tenv-\u003ecur_hist_ent-\u003eframe = frame;\n-\t\tverifier_bug_if(env-\u003ecur_hist_ent-\u003elinked_regs != 0, env,\n-\t\t\t\t\"insn history: insn_idx %d linked_regs: %#llx\",\n-\t\t\t\tenv-\u003einsn_idx, env-\u003ecur_hist_ent-\u003elinked_regs);\n-\t\tenv-\u003ecur_hist_ent-\u003elinked_regs = linked_regs;\n+\t\tverifier_bug_if(env-\u003ecur_hist_ent-\u003elinked_regs_cnt != 0, env,\n+\t\t\t\t\"insn history: insn_idx %d has %u linked regs\",\n+\t\t\t\tenv-\u003einsn_idx, env-\u003ecur_hist_ent-\u003elinked_regs_cnt);\n+\t\tif (linked_regs_cnt)\n+\t\t\tmemcpy(env-\u003ecur_hist_ent-\u003elinked_regs, linked_regs,\n+\t\t\t linked_regs_cnt * sizeof(*linked_regs));\n+\t\tenv-\u003ecur_hist_ent-\u003elinked_regs_cnt = linked_regs_cnt;\n \t\treturn 0;\n \t}\n \n@@ -47,7 +51,9 @@ int bpf_push_jmp_history(struct bpf_verifier_env *env, struct bpf_verifier_state\n \tp-\u003eflags = insn_flags;\n \tp-\u003espi = spi;\n \tp-\u003eframe = frame;\n-\tp-\u003elinked_regs = linked_regs;\n+\tif (linked_regs_cnt)\n+\t\tmemcpy(p-\u003elinked_regs, linked_regs, linked_regs_cnt * sizeof(*linked_regs));\n+\tp-\u003elinked_regs_cnt = linked_regs_cnt;\n \tcur-\u003ejmp_history_cnt = cnt;\n \tenv-\u003ecur_hist_ent = p;\n \n@@ -123,13 +129,26 @@ static inline void bt_reset(struct backtrack_state *bt)\n \tbt-\u003eenv = env;\n }\n \n-static inline u32 bt_empty(struct backtrack_state *bt)\n+static inline bool bt_frame_stack_empty(struct backtrack_state *bt, u32 frame)\n {\n-\tu64 mask = 0;\n+\treturn bitmap_empty(bt-\u003estack_masks[frame], MAX_BPF_STACK_SLOTS);\n+}\n+\n+static inline bool bt_stack_empty(struct backtrack_state *bt)\n+{\n+\treturn bt_frame_stack_empty(bt, bt-\u003eframe);\n+}\n+\n+static inline bool bt_empty(struct backtrack_state *bt)\n+{\n+\tu32 mask = 0;\n \tint i;\n \n-\tfor (i = 0; i \u003c= bt-\u003eframe; i++)\n-\t\tmask |= bt-\u003ereg_masks[i] | bt-\u003estack_masks[i] | bt-\u003estack_arg_masks[i];\n+\tfor (i = 0; i \u003c= bt-\u003eframe; i++) {\n+\t\tmask |= bt-\u003ereg_masks[i] | bt-\u003estack_arg_masks[i];\n+\t\tif (!bt_frame_stack_empty(bt, i))\n+\t\t\treturn false;\n+\t}\n \n \treturn mask == 0;\n }\n@@ -181,7 +200,7 @@ static inline void bt_clear_reg(struct backtrack_state *bt, u32 reg)\n \n static inline void bt_clear_frame_slot(struct backtrack_state *bt, u32 frame, u32 slot)\n {\n-\tbt-\u003estack_masks[frame] \u0026= ~(1ull \u003c\u003c slot);\n+\t__clear_bit(slot, bt-\u003estack_masks[frame]);\n }\n \n static inline u32 bt_frame_reg_mask(struct backtrack_state *bt, u32 frame)\n@@ -194,14 +213,14 @@ static inline u32 bt_reg_mask(struct backtrack_state *bt)\n \treturn bt-\u003ereg_masks[bt-\u003eframe];\n }\n \n-static inline u64 bt_frame_stack_mask(struct backtrack_state *bt, u32 frame)\n+static inline unsigned long *bt_frame_stack_mask(struct backtrack_state *bt, u32 frame)\n {\n \treturn bt-\u003estack_masks[frame];\n }\n \n-static inline u64 bt_stack_mask(struct backtrack_state *bt)\n+static inline unsigned long *bt_stack_mask(struct backtrack_state *bt)\n {\n-\treturn bt-\u003estack_masks[bt-\u003eframe];\n+\treturn bt_frame_stack_mask(bt, bt-\u003eframe);\n }\n \n static inline u8 bt_stack_arg_mask(struct backtrack_state *bt)\n@@ -233,17 +252,16 @@ static void fmt_reg_mask(char *buf, ssize_t buf_sz, u32 reg_mask)\n \t\t\tbreak;\n \t}\n }\n-/* format stack slots bitmask, e.g., \"-8,-24,-40\" for 0x15 mask */\n-void bpf_fmt_stack_mask(char *buf, ssize_t buf_sz, u64 stack_mask)\n+\n+/* format stack slots bitmask, e.g., \"-8,-24,-40\" for slots 0, 2 and 4 */\n+void bpf_fmt_stack_mask(char *buf, ssize_t buf_sz, const unsigned long *stack_mask)\n {\n-\tDECLARE_BITMAP(mask, 64);\n \tbool first = true;\n \tint i, n;\n \n \tbuf[0] = '\\0';\n \n-\tbitmap_from_u64(mask, stack_mask);\n-\tfor_each_set_bit(i, mask, 64) {\n+\tfor_each_set_bit(i, stack_mask, MAX_BPF_STACK_SLOTS) {\n \t\tn = snprintf(buf, buf_sz, \"%s%d\", first ? \"\" : \",\", -(i + 1) * 8);\n \t\tfirst = false;\n \t\tbuf += n;\n@@ -455,10 +473,11 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,\n \t\t\t\t/* we are now tracking register spills correctly,\n \t\t\t\t * so any instance of leftover slots is a bug\n \t\t\t\t */\n-\t\t\t\tif (bt_stack_mask(bt) != 0) {\n-\t\t\t\t\tverifier_bug(env,\n-\t\t\t\t\t\t \"static subprog leftover stack slots %llx\",\n-\t\t\t\t\t\t bt_stack_mask(bt));\n+\t\t\t\tif (!bt_stack_empty(bt)) {\n+\t\t\t\t\tbpf_fmt_stack_mask(env-\u003etmp_str_buf, TMP_STR_BUF_LEN,\n+\t\t\t\t\t\t\t bt_stack_mask(bt));\n+\t\t\t\t\tverifier_bug(env, \"static subprog leftover stack slots %s\",\n+\t\t\t\t\t\t env-\u003etmp_str_buf);\n \t\t\t\t\treturn -EFAULT;\n \t\t\t\t}\n \t\t\t\t/* propagate r1-r5 to the caller */\n@@ -491,9 +510,11 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,\n \t\t\t\t\t bt_reg_mask(bt));\n \t\t\t\treturn -EFAULT;\n \t\t\t}\n-\t\t\tif (bt_stack_mask(bt) != 0) {\n-\t\t\t\tverifier_bug(env, \"callback leftover stack slots %llx\",\n-\t\t\t\t\t bt_stack_mask(bt));\n+\t\t\tif (!bt_stack_empty(bt)) {\n+\t\t\t\tbpf_fmt_stack_mask(env-\u003etmp_str_buf, TMP_STR_BUF_LEN,\n+\t\t\t\t\t\t bt_stack_mask(bt));\n+\t\t\t\tverifier_bug(env, \"callback leftover stack slots %s\",\n+\t\t\t\t\t env-\u003etmp_str_buf);\n \t\t\t\treturn -EFAULT;\n \t\t\t}\n \t\t\t/* clear r1-r5 in callback subprog's mask */\n@@ -711,10 +732,12 @@ void bpf_mark_all_scalars_precise(struct bpf_verifier_env *env,\n \t\t\t\t\t\ti, j);\n \t\t\t\t}\n \t\t\t}\n-\t\t\tfor (j = 0; j \u003c func-\u003eallocated_stack / BPF_REG_SIZE; j++) {\n-\t\t\t\tif (!bpf_is_spilled_reg(\u0026func-\u003estack[j]))\n+\t\t\tfor (j = 0; j \u003c bpf_stack_nr_slots(func); j++) {\n+\t\t\t\tstruct bpf_stack_state *ss = bpf_stack_slot(func, j);\n+\n+\t\t\t\tif (!bpf_is_spilled_reg(ss))\n \t\t\t\t\tcontinue;\n-\t\t\t\treg = \u0026func-\u003estack[j].spilled_ptr;\n+\t\t\t\treg = \u0026ss-\u003espilled_ptr;\n \t\t\t\tif (reg-\u003etype != SCALAR_VALUE || reg-\u003eprecise)\n \t\t\t\t\tcontinue;\n \t\t\t\treg-\u003eprecise = true;\n@@ -826,6 +849,7 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,\n \tint subseq_idx = -1;\n \tstruct bpf_func_state *func;\n \tbool tmp, skip_first = true;\n+\tstruct bpf_stack_state *ss;\n \tstruct bpf_reg_state *reg;\n \tint i, fr, err;\n \n@@ -872,7 +896,7 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,\n \t\t\tif (st-\u003ecurframe == 0 \u0026\u0026\n \t\t\t st-\u003eframe[0]-\u003esubprogno \u003e 0 \u0026\u0026\n \t\t\t st-\u003eframe[0]-\u003ecallsite == BPF_MAIN_FUNC \u0026\u0026\n-\t\t\t bt_stack_mask(bt) == 0 \u0026\u0026\n+\t\t\t bt_stack_empty(bt) \u0026\u0026\n \t\t\t (bt_reg_mask(bt) \u0026 ~BPF_REGMASK_ARGS) == 0) {\n \t\t\t\tbitmap_from_u64(mask, bt_reg_mask(bt));\n \t\t\t\tfor_each_set_bit(i, mask, 32) {\n@@ -886,8 +910,9 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,\n \t\t\t\treturn 0;\n \t\t\t}\n \n-\t\t\tverifier_bug(env, \"backtracking func entry subprog %d reg_mask %x stack_mask %llx\",\n-\t\t\t\t st-\u003eframe[0]-\u003esubprogno, bt_reg_mask(bt), bt_stack_mask(bt));\n+\t\t\tbpf_fmt_stack_mask(env-\u003etmp_str_buf, TMP_STR_BUF_LEN, bt_stack_mask(bt));\n+\t\t\tverifier_bug(env, \"backtracking func entry subprog %d reg_mask %x stack_mask %s\",\n+\t\t\t\t st-\u003eframe[0]-\u003esubprogno, bt_reg_mask(bt), env-\u003etmp_str_buf);\n \t\t\treturn -EFAULT;\n \t\t}\n \n@@ -948,18 +973,18 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,\n \t\t\t\t}\n \t\t\t}\n \n-\t\t\tbitmap_from_u64(mask, bt_frame_stack_mask(bt, fr));\n-\t\t\tfor_each_set_bit(i, mask, 64) {\n-\t\t\t\tif (verifier_bug_if(i \u003e= func-\u003eallocated_stack / BPF_REG_SIZE,\n+\t\t\tfor_each_set_bit(i, bt_frame_stack_mask(bt, fr), MAX_BPF_STACK_SLOTS) {\n+\t\t\t\tif (verifier_bug_if(i \u003e= bpf_stack_nr_slots(func),\n \t\t\t\t\t\t env, \"stack slot %d, total slots %d\",\n-\t\t\t\t\t\t i, func-\u003eallocated_stack / BPF_REG_SIZE))\n+\t\t\t\t\t\t i, bpf_stack_nr_slots(func)))\n \t\t\t\t\treturn -EFAULT;\n \n-\t\t\t\tif (!bpf_is_spilled_scalar_reg(\u0026func-\u003estack[i])) {\n+\t\t\t\tss = bpf_stack_slot(func, i);\n+\t\t\t\tif (!bpf_is_spilled_scalar_reg(ss)) {\n \t\t\t\t\tbt_clear_frame_slot(bt, fr, i);\n \t\t\t\t\tcontinue;\n \t\t\t\t}\n-\t\t\t\treg = \u0026func-\u003estack[i].spilled_ptr;\n+\t\t\t\treg = \u0026ss-\u003espilled_ptr;\n \t\t\t\tif (reg-\u003eprecise) {\n \t\t\t\t\tbt_clear_frame_slot(bt, fr, i);\n \t\t\t\t} else {\ndiff --git a/kernel/bpf/core.c b/kernel/bpf/core.c\nindex 273f74068068c..a09aa0082393c 100644\n--- a/kernel/bpf/core.c\n+++ b/kernel/bpf/core.c\n@@ -3483,6 +3483,19 @@ bool __weak bpf_jit_supports_private_stack(void)\n \treturn false;\n }\n \n+/*\n+ * Return TRUE if the JIT lays out frames of up to MAX_BPF_STACK_JIT bytes.\n+ * Its prologue, epilogue and tail call sequences must encode such frame\n+ * sizes and a private stack must be sized from the program's depth. The\n+ * budget is only granted alongside bpf_jit_supports_subprog_tailcalls(),\n+ * whose tail calls land before the target sets up its own frame; see\n+ * bpf_prog_stack_limit().\n+ */\n+bool __weak bpf_jit_supports_large_stack(void)\n+{\n+\treturn false;\n+}\n+\n void __weak arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp, u64 bp), void *cookie)\n {\n }\ndiff --git a/kernel/bpf/diagnostics.c b/kernel/bpf/diagnostics.c\nindex 5ecfa86ed49f4..af97aea49075b 100644\n--- a/kernel/bpf/diagnostics.c\n+++ b/kernel/bpf/diagnostics.c\n@@ -1600,9 +1600,9 @@ static struct bpf_reg_state *target_to_reg(struct bpf_verifier_env *env,\n \t\t\treturn NULL;\n \t\treturn \u0026state-\u003estack_arg_regs[target-\u003estack_arg];\n \tcase BPF_DIAG_MOD_TARGET_STACK_SLOT:\n-\t\tif (target-\u003espi \u003e= state-\u003eallocated_stack / BPF_REG_SIZE)\n+\t\tif (target-\u003espi \u003e= bpf_stack_nr_slots(state))\n \t\t\treturn NULL;\n-\t\treturn \u0026state-\u003estack[target-\u003espi].spilled_ptr;\n+\t\treturn \u0026bpf_stack_slot(state, target-\u003espi)-\u003espilled_ptr;\n \tdefault:\n \t\treturn NULL;\n \t}\n@@ -1618,7 +1618,7 @@ static bool reg_to_target(struct bpf_verifier_env *env, const struct bpf_reg_sta\n \tfor (frame = 0; frame \u003c= vstate-\u003ecurframe; frame++) {\n \t\tstruct bpf_func_state *state = vstate-\u003eframe[frame];\n \t\tunsigned long start, end;\n-\t\tu32 nslots = state-\u003eallocated_stack / BPF_REG_SIZE;\n+\t\tu32 nslots = bpf_stack_nr_slots(state);\n \t\tint spi;\n \n \t\tstart = (unsigned long)state-\u003eregs;\n@@ -1637,6 +1637,11 @@ static bool reg_to_target(struct bpf_verifier_env *env, const struct bpf_reg_sta\n \t\t\treturn true;\n \t\t}\n \n+\t\t/*\n+\t\t * Map the pointer back to its slot by address, which relies on\n+\t\t * the slots forming one contiguous array as bpf_stack_slot()\n+\t\t * indexes it.\n+\t\t */\n \t\tstart = (unsigned long)state-\u003estack;\n \t\tend = (unsigned long)(state-\u003estack + nslots);\n \t\tif (nslots \u0026\u0026 addr \u003e= start \u0026\u0026 addr \u003c end) {\ndiff --git a/kernel/bpf/liveness.c b/kernel/bpf/liveness.c\nindex a204b492acc8f..16fb7dc43726f 100644\n--- a/kernel/bpf/liveness.c\n+++ b/kernel/bpf/liveness.c\n@@ -10,10 +10,39 @@\n \n #define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)\n \n-struct per_frame_masks {\n-\tspis_t may_read;\t/* stack slots that may be read by this instruction */\n-\tspis_t must_write;\t/* stack slots written by this instruction */\n-\tspis_t live_before;\t/* stack slots that may be read by this insn and its successors */\n+/*\n+ * Stack liveness is tracked with a 4-byte (half register) granularity.\n+ * Half-slot 0 covers [fp-4, fp), half-slot 1 covers [fp-8, fp-4), and so on,\n+ * hence FRAME_HALF_SPIS - 1 is the deepest half-slot a frame can have.\n+ */\n+#define FRAME_HALF_SPIS\t\t(MAX_BPF_STACK_JIT / BPF_HALF_REG_SIZE)\n+#define FRAME_MAX_WORDS\t\tBITS_TO_LONGS(FRAME_HALF_SPIS)\n+\n+/* Masks tracked for each instruction of a frame */\n+enum {\n+\tFM_MAY_READ,\t/* stack slots that may be read by this instruction */\n+\tFM_MUST_WRITE,\t/* stack slots written by this instruction */\n+\tFM_LIVE_BEFORE,\t/* stack slots that may be read by this insn and its successors */\n+\tFM_MASK_CNT,\n+};\n+\n+/*\n+ * Per instruction stack masks for one frame of a function instance.\n+ *\n+ * Most frames use only a fraction of the stack budget, so instead of masks\n+ * wide enough for every half-slot of the largest possible frame, all masks\n+ * of one array share the width @words, and the marking functions widen the\n+ * array as deeper half-slots are recorded. Mask @kind of the instruction at\n+ * relative index @i is at \u0026bits[(i * FM_MASK_CNT + kind) * words]. A\n+ * half-slot at or past @words * BITS_PER_LONG is never read by this frame,\n+ * hence never live. An instruction that may read the whole frame, such as a\n+ * call passing a frame pointer to another subprog, widens the array to the\n+ * program's stack budget, the deepest an accepted program can reach, so\n+ * that the read cannot lose half-slots to a later widening.\n+ */\n+struct frame_masks {\n+\tu32 words;\n+\tunsigned long bits[];\n };\n \n /*\n@@ -29,7 +58,7 @@ struct func_instance {\n \tu32 subprog_start;\t/* cached env-\u003esubprog_info[subprog].start */\n \tu32 insn_cnt;\t\t/* cached number of insns in the function */\n \t/* Per frame, per instruction masks, frames allocated lazily. */\n-\tstruct per_frame_masks *frames[MAX_CALL_FRAMES];\n+\tstruct frame_masks *frames[MAX_CALL_FRAMES];\n \tbool must_write_initialized;\n };\n \n@@ -151,50 +180,123 @@ static int relative_idx(struct func_instance *instance, u32 insn_idx)\n \treturn insn_idx - instance-\u003esubprog_start;\n }\n \n-static struct per_frame_masks *get_frame_masks(struct func_instance *instance,\n-\t\t\t\t\t u32 frame, u32 insn_idx)\n+static u32 frame_mask_bits(struct frame_masks *fm)\n {\n-\tif (!instance-\u003eframes[frame])\n-\t\treturn NULL;\n+\treturn fm-\u003ewords * BITS_PER_LONG;\n+}\n \n-\treturn \u0026instance-\u003eframes[frame][relative_idx(instance, insn_idx)];\n+static size_t frame_mask_words(struct func_instance *instance, u32 words)\n+{\n+\treturn (size_t)instance-\u003einsn_cnt * FM_MASK_CNT * words;\n }\n \n-static struct per_frame_masks *alloc_frame_masks(struct func_instance *instance,\n-\t\t\t\t\t\t u32 frame, u32 insn_idx)\n+/* Mask @kind of the instruction at relative index @rel */\n+static unsigned long *rel_mask(struct frame_masks *fm, u32 rel, u32 kind)\n {\n-\tstruct per_frame_masks *arr;\n+\treturn fm-\u003ebits + ((size_t)rel * FM_MASK_CNT + kind) * fm-\u003ewords;\n+}\n \n-\tif (!instance-\u003eframes[frame]) {\n-\t\tarr = kvzalloc_objs(*arr, instance-\u003einsn_cnt,\n-\t\t\t\t GFP_KERNEL_ACCOUNT);\n-\t\tinstance-\u003eframes[frame] = arr;\n-\t\tif (!arr)\n-\t\t\treturn ERR_PTR(-ENOMEM);\n+/*\n+ * Make sure @frame has a mask array at least @words wide, allocating it or\n+ * copying the existing masks into the wider stride as needed.\n+ * @words must be in range [1, FRAME_MAX_WORDS].\n+ */\n+static struct frame_masks *widen_frame_masks(struct func_instance *instance,\n+\t\t\t\t\t u32 frame, u32 words)\n+{\n+\tstruct frame_masks *old = instance-\u003eframes[frame], *new;\n+\tu32 i, kind;\n+\n+\tif (old \u0026\u0026 old-\u003ewords \u003e= words)\n+\t\treturn old;\n+\tnew = kvzalloc_flex(*new, bits, frame_mask_words(instance, words), GFP_KERNEL_ACCOUNT);\n+\tif (!new)\n+\t\treturn NULL;\n+\tnew-\u003ewords = words;\n+\tif (old) {\n+\t\tfor (i = 0; i \u003c instance-\u003einsn_cnt; i++)\n+\t\t\tfor (kind = 0; kind \u003c FM_MASK_CNT; kind++)\n+\t\t\t\tmemcpy(rel_mask(new, i, kind), rel_mask(old, i, kind),\n+\t\t\t\t old-\u003ewords * sizeof(*old-\u003ebits));\n+\t\tkvfree(old);\n \t}\n-\treturn get_frame_masks(instance, frame, insn_idx);\n+\tinstance-\u003eframes[frame] = new;\n+\treturn new;\n }\n \n-/* Accumulate may_read masks for @frame at @insn_idx */\n-static int mark_stack_read(struct func_instance *instance, u32 frame, u32 insn_idx, spis_t mask)\n+/*\n+ * Set the inclusive half-slot range [lo, hi] in mask @kind of @frame at @insn_idx.\n+ * An empty range, including one with a negative @hi as computed for a write\n+ * that does not fully cover any half-slot, marks nothing.\n+ */\n+static int mark_stack_range(struct func_instance *instance, u32 frame, u32 insn_idx,\n+\t\t\t u32 kind, s32 lo, s32 hi)\n {\n-\tstruct per_frame_masks *masks;\n+\tstruct frame_masks *fm;\n \n-\tmasks = alloc_frame_masks(instance, frame, insn_idx);\n-\tif (IS_ERR(masks))\n-\t\treturn PTR_ERR(masks);\n-\tmasks-\u003emay_read = spis_or(masks-\u003emay_read, mask);\n+\t/*\n+\t * An access past the frame bottom is rejected by the main verifier\n+\t * pass later, liveness only has to avoid running off the masks.\n+\t */\n+\thi = min_t(s32, hi, FRAME_HALF_SPIS - 1);\n+\tif (lo \u003e hi)\n+\t\treturn 0;\n+\tfm = widen_frame_masks(instance, frame, BITS_TO_LONGS(hi + 1));\n+\tif (!fm)\n+\t\treturn -ENOMEM;\n+\tbitmap_set(rel_mask(fm, relative_idx(instance, insn_idx), kind), lo, hi - lo + 1);\n \treturn 0;\n }\n \n-static int mark_stack_write(struct func_instance *instance, u32 frame, u32 insn_idx, spis_t mask)\n+/* Accumulate may_read for half-slots [lo, hi] of @frame at @insn_idx */\n+static int mark_stack_read(struct func_instance *instance, u32 frame, u32 insn_idx,\n+\t\t\t s32 lo, s32 hi)\n+{\n+\treturn mark_stack_range(instance, frame, insn_idx, FM_MAY_READ, lo, hi);\n+}\n+\n+/* Accumulate must_write for half-slots [lo, hi] of @frame at @insn_idx */\n+static int mark_stack_write(struct func_instance *instance, u32 frame, u32 insn_idx,\n+\t\t\t s32 lo, s32 hi)\n+{\n+\treturn mark_stack_range(instance, frame, insn_idx, FM_MUST_WRITE, lo, hi);\n+}\n+\n+/*\n+ * Mark every half-slot of @frame as possibly read by @insn_idx. This widens\n+ * the masks to the program's stack budget: a full read recorded at a narrower\n+ * width would leave the bits added by a later widening clear and lose part of\n+ * it. No verifier state holds a slot past the budget, so no liveness query\n+ * reaches beyond it; an access past the budget in unreachable code may still\n+ * widen a frame's masks further, which is harmless.\n+ */\n+static int mark_stack_read_all(struct bpf_verifier_env *env, struct func_instance *instance,\n+\t\t\t u32 frame, u32 insn_idx)\n {\n-\tstruct per_frame_masks *masks;\n+\treturn mark_stack_read(instance, frame, insn_idx, 0,\n+\t\t\t env-\u003estack_limit / BPF_HALF_REG_SIZE - 1);\n+}\n \n-\tmasks = alloc_frame_masks(instance, frame, insn_idx);\n-\tif (IS_ERR(masks))\n-\t\treturn PTR_ERR(masks);\n-\tmasks-\u003emust_write = spis_or(masks-\u003emust_write, mask);\n+/* Accumulate @src, a mask @src_words wide, into may_read of @frame at @insn_idx */\n+static int mark_stack_read_mask(struct func_instance *instance, u32 frame, u32 insn_idx,\n+\t\t\t\tconst unsigned long *src, u32 src_words)\n+{\n+\tu32 nbits = src_words * BITS_PER_LONG;\n+\tstruct frame_masks *fm;\n+\tunsigned long *dst;\n+\tu32 last, w;\n+\n+\tlast = find_last_bit(src, nbits);\n+\tif (last == nbits)\n+\t\treturn 0;\n+\tfm = widen_frame_masks(instance, frame, BITS_TO_LONGS(last + 1));\n+\tif (!fm)\n+\t\treturn -ENOMEM;\n+\tdst = rel_mask(fm, relative_idx(instance, insn_idx), FM_MAY_READ);\n+\t/* @src has no bits set past @last, hence none past @fm-\u003ewords either */\n+\tsrc_words = min(src_words, fm-\u003ewords);\n+\tfor (w = 0; w \u003c src_words; w++)\n+\t\tdst[w] |= src[w];\n \treturn 0;\n }\n \n@@ -272,33 +374,42 @@ __diag_pop();\n static inline bool update_insn(struct bpf_verifier_env *env,\n \t\t\t struct func_instance *instance, u32 frame, u32 insn_idx)\n {\n-\tspis_t new_before, new_after;\n-\tstruct per_frame_masks *insn, *succ_insn;\n+\tunsigned long new_after[FRAME_MAX_WORDS] = {};\n+\tunsigned long *may_read, *must_write, *live_before;\n+\tstruct frame_masks *fm = instance-\u003eframes[frame];\n+\tu32 rel = relative_idx(instance, insn_idx);\n \tstruct bpf_iarray *succ;\n-\tu32 s;\n-\tbool changed;\n+\tbool changed = false;\n+\tu32 s, w;\n \n \tsucc = bpf_insn_successors(env, insn_idx);\n \tif (succ-\u003ecnt == 0)\n \t\treturn false;\n \n-\tchanged = false;\n-\tinsn = get_frame_masks(instance, frame, insn_idx);\n-\tnew_before = SPIS_ZERO;\n-\tnew_after = SPIS_ZERO;\n+\t/* All instructions of one frame array share the same mask width */\n \tfor (s = 0; s \u003c succ-\u003ecnt; ++s) {\n-\t\tsucc_insn = get_frame_masks(instance, frame, succ-\u003eitems[s]);\n-\t\tnew_after = spis_or(new_after, succ_insn-\u003elive_before);\n+\t\tunsigned long *succ_live;\n+\n+\t\tsucc_live = rel_mask(fm, relative_idx(instance, succ-\u003eitems[s]), FM_LIVE_BEFORE);\n+\t\tfor (w = 0; w \u003c fm-\u003ewords; w++)\n+\t\t\tnew_after[w] |= succ_live[w];\n \t}\n+\tmay_read = rel_mask(fm, rel, FM_MAY_READ);\n+\tmust_write = rel_mask(fm, rel, FM_MUST_WRITE);\n+\tlive_before = rel_mask(fm, rel, FM_LIVE_BEFORE);\n \t/*\n \t * New \"live_before\" is a union of all \"live_before\" of successors\n \t * minus slots written by instruction plus slots read by instruction.\n \t * new_before = (new_after \u0026 ~insn-\u003emust_write) | insn-\u003emay_read\n \t */\n-\tnew_before = spis_or(spis_and(new_after, spis_not(insn-\u003emust_write)),\n-\t\t\t insn-\u003emay_read);\n-\tchanged |= !spis_equal(new_before, insn-\u003elive_before);\n-\tinsn-\u003elive_before = new_before;\n+\tfor (w = 0; w \u003c fm-\u003ewords; w++) {\n+\t\tunsigned long new_before = (new_after[w] \u0026 ~must_write[w]) | may_read[w];\n+\n+\t\tif (new_before != live_before[w]) {\n+\t\t\tlive_before[w] = new_before;\n+\t\t\tchanged = true;\n+\t\t}\n+\t}\n \treturn changed;\n }\n \n@@ -329,10 +440,12 @@ static void update_instance(struct bpf_verifier_env *env, struct func_instance *\n \n static bool is_live_before(struct func_instance *instance, u32 insn_idx, u32 frameno, u32 half_spi)\n {\n-\tstruct per_frame_masks *masks;\n+\tstruct frame_masks *fm = instance-\u003eframes[frameno];\n \n-\tmasks = get_frame_masks(instance, frameno, insn_idx);\n-\treturn masks \u0026\u0026 spis_test_bit(masks-\u003elive_before, half_spi);\n+\t/* No recorded access reaches past the masks, so nothing there is live */\n+\tif (!fm || half_spi \u003e= frame_mask_bits(fm))\n+\t\treturn false;\n+\treturn test_bit(half_spi, rel_mask(fm, relative_idx(instance, insn_idx), FM_LIVE_BEFORE));\n }\n \n int bpf_live_stack_query_init(struct bpf_verifier_env *env, struct bpf_verifier_state *st)\n@@ -447,17 +560,19 @@ static int spi_off(int spi)\n * When only one half is set, print as \"-4h\",\"-8h\",...\n * Runs of 3+ consecutive fully-set SPIs are collapsed: \"fp0-8..-24\"\n */\n-static char *fmt_spis_mask(struct bpf_verifier_env *env, int frame, bool first, spis_t spis)\n+static char *fmt_spis_mask(struct bpf_verifier_env *env, int frame, bool first,\n+\t\t\t const unsigned long *spis, u32 words)\n {\n \tint buf_sz = sizeof(env-\u003etmp_str_buf);\n+\tint spi_cnt = words * BITS_PER_LONG / 2;\n \tchar *buf = env-\u003etmp_str_buf;\n \tint spi, n, run_start;\n \n \tbuf[0] = '\\0';\n \n-\tfor (spi = 0; spi \u003c STACK_SLOTS / 2 \u0026\u0026 buf_sz \u003e 0; spi++) {\n-\t\tbool lo = spis_test_bit(spis, spi * 2);\n-\t\tbool hi = spis_test_bit(spis, spi * 2 + 1);\n+\tfor (spi = 0; spi \u003c spi_cnt \u0026\u0026 buf_sz \u003e 0; spi++) {\n+\t\tbool lo = test_bit(spi * 2, spis);\n+\t\tbool hi = test_bit(spi * 2 + 1, spis);\n \t\tconst char *space = first ? \"\" : \" \";\n \n \t\tif (!lo \u0026\u0026 !hi)\n@@ -467,16 +582,16 @@ static char *fmt_spis_mask(struct bpf_verifier_env *env, int frame, bool first,\n \t\t\t/* half-spi */\n \t\t\tn = scnprintf(buf, buf_sz, \"%sfp%d%d%s\",\n \t\t\t\t space, frame, spi_off(spi) + (lo ? STACK_SLOT_SZ : 0), \"h\");\n-\t\t} else if (spi + 2 \u003c STACK_SLOTS / 2 \u0026\u0026\n-\t\t\t spis_test_bit(spis, spi * 2 + 2) \u0026\u0026\n-\t\t\t spis_test_bit(spis, spi * 2 + 3) \u0026\u0026\n-\t\t\t spis_test_bit(spis, spi * 2 + 4) \u0026\u0026\n-\t\t\t spis_test_bit(spis, spi * 2 + 5)) {\n+\t\t} else if (spi + 2 \u003c spi_cnt \u0026\u0026\n+\t\t\t test_bit(spi * 2 + 2, spis) \u0026\u0026\n+\t\t\t test_bit(spi * 2 + 3, spis) \u0026\u0026\n+\t\t\t test_bit(spi * 2 + 4, spis) \u0026\u0026\n+\t\t\t test_bit(spi * 2 + 5, spis)) {\n \t\t\t/* 3+ consecutive full spis */\n \t\t\trun_start = spi;\n-\t\t\twhile (spi + 1 \u003c STACK_SLOTS / 2 \u0026\u0026\n-\t\t\t spis_test_bit(spis, (spi + 1) * 2) \u0026\u0026\n-\t\t\t spis_test_bit(spis, (spi + 1) * 2 + 1))\n+\t\t\twhile (spi + 1 \u003c spi_cnt \u0026\u0026\n+\t\t\t test_bit((spi + 1) * 2, spis) \u0026\u0026\n+\t\t\t test_bit((spi + 1) * 2 + 1, spis))\n \t\t\t\tspi++;\n \t\t\tn = scnprintf(buf, buf_sz, \"%sfp%d%d..%d\",\n \t\t\t\t space, frame, spi_off(run_start), spi_off(spi));\n@@ -495,7 +610,8 @@ static void print_instance(struct bpf_verifier_env *env, struct func_instance *i\n {\n \tint start = env-\u003esubprog_info[instance-\u003esubprog].start;\n \tstruct bpf_insn *insns = env-\u003eprog-\u003einsnsi;\n-\tstruct per_frame_masks *masks;\n+\tstruct frame_masks *fm;\n+\tunsigned long *mask;\n \tint len = instance-\u003einsn_cnt;\n \tint insn_idx, frame, i;\n \tbool has_use, has_def;\n@@ -518,10 +634,13 @@ static void print_instance(struct bpf_verifier_env *env, struct func_instance *i\n \t\tpos = env-\u003elog.end_pos;\n \t\tverbose(env, \" use: \");\n \t\tfor (frame = instance-\u003edepth; frame \u003e= 0; --frame) {\n-\t\t\tmasks = get_frame_masks(instance, frame, insn_idx);\n-\t\t\tif (!masks || spis_is_zero(masks-\u003emay_read))\n+\t\t\tfm = instance-\u003eframes[frame];\n+\t\t\tif (!fm)\n+\t\t\t\tcontinue;\n+\t\t\tmask = rel_mask(fm, i, FM_MAY_READ);\n+\t\t\tif (bitmap_empty(mask, frame_mask_bits(fm)))\n \t\t\t\tcontinue;\n-\t\t\tverbose(env, \"%s\", fmt_spis_mask(env, frame, !has_use, masks-\u003emay_read));\n+\t\t\tverbose(env, \"%s\", fmt_spis_mask(env, frame, !has_use, mask, fm-\u003ewords));\n \t\t\thas_use = true;\n \t\t}\n \t\tif (!has_use)\n@@ -529,10 +648,13 @@ static void print_instance(struct bpf_verifier_env *env, struct func_instance *i\n \t\tpos = env-\u003elog.end_pos;\n \t\tverbose(env, \" def: \");\n \t\tfor (frame = instance-\u003edepth; frame \u003e= 0; --frame) {\n-\t\t\tmasks = get_frame_masks(instance, frame, insn_idx);\n-\t\t\tif (!masks || spis_is_zero(masks-\u003emust_write))\n+\t\t\tfm = instance-\u003eframes[frame];\n+\t\t\tif (!fm)\n \t\t\t\tcontinue;\n-\t\t\tverbose(env, \"%s\", fmt_spis_mask(env, frame, !has_def, masks-\u003emust_write));\n+\t\t\tmask = rel_mask(fm, i, FM_MUST_WRITE);\n+\t\t\tif (bitmap_empty(mask, frame_mask_bits(fm)))\n+\t\t\t\tcontinue;\n+\t\t\tverbose(env, \"%s\", fmt_spis_mask(env, frame, !has_def, mask, fm-\u003ewords));\n \t\t\thas_def = true;\n \t\t}\n \t\tif (!has_def)\n@@ -601,9 +723,9 @@ static int print_instances(struct bpf_verifier_env *env)\n * - same frame + different offset -\u003e offset-imprecise\n * - different frames -\u003e fully-imprecise (bitmask OR)\n *\n- * At memory access sites (LDX/STX/ST), offset-imprecise marks only\n- * the known frame's access mask as SPIS_ALL, while fully-imprecise\n- * iterates bits in the bitmask and routes each frame to its target.\n+ * At memory access sites (LDX/STX/ST), offset-imprecise marks the known\n+ * frame as fully read, while fully-imprecise iterates bits in the bitmask\n+ * and routes each frame to its target.\n */\n #define MAX_ARG_OFFSETS 4\n \n@@ -622,8 +744,15 @@ enum arg_track_state {\n \tARG_IMPRECISE\t= -3,\t/* lost identity; .mask is arg bitmask */\n };\n \n-/* Track callee stack slots fp-8 through fp-512 (64 slots of 8 bytes each) */\n-#define MAX_ARG_SPILL_SLOTS 64\n+/*\n+ * The tracker follows as many spill slots as a subprog addresses directly\n+ * through R10, see subprog_spill_slots(). A frame's slots at a call site are\n+ * kept in a snapshot for the callees that receive pointers into that frame.\n+ */\n+struct spill_snapshot {\n+\tu32 slots;\n+\tstruct arg_track at[];\n+};\n \n /*\n * Combined register + stack arg tracking: R0-R10 at indices 0-10,\n@@ -898,13 +1027,60 @@ static void arg_padd(struct arg_track *at, s64 delta)\n }\n \n /*\n- * Convert a byte offset from FP to a callee stack slot index.\n+ * Slots the spill tracker may follow for a subprog of @len instructions\n+ * without its per-instruction tables costing more than they could for the\n+ * largest program while every frame stayed within MAX_BPF_STACK: as many\n+ * entries as 64 slots need for BPF_COMPLEXITY_LIMIT_INSNS instructions.\n+ */\n+static u32 spill_slots_affordable(int len)\n+{\n+\tu32 base = MAX_BPF_STACK / BPF_REG_SIZE;\n+\n+\treturn max_t(u32, base, base * BPF_COMPLEXITY_LIMIT_INSNS / len);\n+}\n+\n+/*\n+ * Number of 8-byte spill slots to track for the instructions in [@start, @end):\n+ * the 64 slots of a MAX_BPF_STACK frame, which the tracker has always\n+ * followed, or the deepest 8-byte stack access made directly through R10 when\n+ * that reaches further, up to the program's stack budget. Spills and fills are\n+ * compiled as direct R10 accesses, so the count covers them whatever the frame\n+ * size; a fill through a derived pointer that reaches past it is treated as\n+ * imprecise, as one past the frame always was.\n+ */\n+static u32 subprog_spill_slots(struct bpf_verifier_env *env, int start, int end)\n+{\n+\tstruct bpf_insn *insns = env-\u003eprog-\u003einsnsi;\n+\tint deepest = 0;\n+\n+\tfor (int i = start; i \u003c end; i++) {\n+\t\tstruct bpf_insn *insn = \u0026insns[i];\n+\t\tu8 class = BPF_CLASS(insn-\u003ecode);\n+\t\tu8 base;\n+\n+\t\tif (BPF_SIZE(insn-\u003ecode) != BPF_DW)\n+\t\t\tcontinue;\n+\t\tif (class == BPF_LDX)\n+\t\t\tbase = insn-\u003esrc_reg;\n+\t\telse if (class == BPF_STX || class == BPF_ST)\n+\t\t\tbase = insn-\u003edst_reg;\n+\t\telse\n+\t\t\tcontinue;\n+\t\tif (base == BPF_REG_FP \u0026\u0026 insn-\u003eoff \u003c 0)\n+\t\t\tdeepest = max(deepest, -insn-\u003eoff);\n+\t}\n+\treturn clamp_t(u32, DIV_ROUND_UP(deepest, 8), MAX_BPF_STACK / BPF_REG_SIZE,\n+\t\t env-\u003estack_limit / BPF_REG_SIZE);\n+}\n+\n+/*\n+ * Convert a byte offset from FP to one of the @nslots tracked stack slots.\n * Returns -1 if out of range or not 8-byte aligned.\n * Slot 0 = fp-8, slot 1 = fp-16, ..., slot 7 = fp-64, ....\n */\n-static int fp_off_to_slot(s16 off)\n+static int fp_off_to_slot(s16 off, u32 nslots)\n {\n-\tif (off \u003e= 0 || off \u003c -(int)(MAX_ARG_SPILL_SLOTS * 8))\n+\tif (off \u003e= 0 || off \u003c -(int)(nslots * 8))\n \t\treturn -1;\n \tif (off % 8)\n \t\treturn -1;\n@@ -913,7 +1089,7 @@ static int fp_off_to_slot(s16 off)\n \n static struct arg_track fill_from_stack(struct bpf_insn *insn,\n \t\t\t\t\tstruct arg_track *at_out, int reg,\n-\t\t\t\t\tstruct arg_track *at_stack_out,\n+\t\t\t\t\tstruct arg_track *at_stack_out, u32 nslots,\n \t\t\t\t\tint depth)\n {\n \tstruct arg_track imp = {\n@@ -924,7 +1100,7 @@ static struct arg_track fill_from_stack(struct bpf_insn *insn,\n \tint cnt, i;\n \n \tif (reg == BPF_REG_FP) {\n-\t\tint slot = fp_off_to_slot(insn-\u003eoff);\n+\t\tint slot = fp_off_to_slot(insn-\u003eoff, nslots);\n \n \t\treturn slot \u003e= 0 ? at_stack_out[slot] : imp;\n \t}\n@@ -937,7 +1113,7 @@ static struct arg_track fill_from_stack(struct bpf_insn *insn,\n \n \t\tif (arg_add(at_out[reg].off[i], insn-\u003eoff, \u0026fp_off))\n \t\t\treturn imp;\n-\t\tslot = fp_off_to_slot(fp_off);\n+\t\tslot = fp_off_to_slot(fp_off, nslots);\n \t\tif (slot \u003c 0)\n \t\t\treturn imp;\n \t\tresult = __arg_track_join(result, at_stack_out[slot]);\n@@ -952,7 +1128,7 @@ static struct arg_track fill_from_stack(struct bpf_insn *insn,\n * When exact offset is unknown conservatively add reg values to all slots in at_stack_out.\n */\n static void spill_to_stack(struct bpf_insn *insn, struct arg_track *at_out,\n-\t\t\t int reg, struct arg_track *at_stack_out,\n+\t\t\t int reg, struct arg_track *at_stack_out, u32 nslots,\n \t\t\t struct arg_track *val, u32 sz)\n {\n \tstruct arg_track none = { .frame = ARG_NONE };\n@@ -960,7 +1136,7 @@ static void spill_to_stack(struct bpf_insn *insn, struct arg_track *at_out,\n \tint cnt, i;\n \n \tif (reg == BPF_REG_FP) {\n-\t\tint slot = fp_off_to_slot(insn-\u003eoff);\n+\t\tint slot = fp_off_to_slot(insn-\u003eoff, nslots);\n \n \t\tif (slot \u003e= 0)\n \t\t\tat_stack_out[slot] = new_val;\n@@ -968,7 +1144,7 @@ static void spill_to_stack(struct bpf_insn *insn, struct arg_track *at_out,\n \t}\n \tcnt = at_out[reg].off_cnt;\n \tif (cnt == 0) {\n-\t\tfor (int slot = 0; slot \u003c MAX_ARG_SPILL_SLOTS; slot++)\n+\t\tfor (int slot = 0; slot \u003c nslots; slot++)\n \t\t\tat_stack_out[slot] = __arg_track_join(at_stack_out[slot], new_val);\n \t\treturn;\n \t}\n@@ -978,7 +1154,7 @@ static void spill_to_stack(struct bpf_insn *insn, struct arg_track *at_out,\n \n \t\tif (arg_add(at_out[reg].off[i], insn-\u003eoff, \u0026fp_off))\n \t\t\tcontinue;\n-\t\tslot = fp_off_to_slot(fp_off);\n+\t\tslot = fp_off_to_slot(fp_off, nslots);\n \t\tif (slot \u003c 0)\n \t\t\tcontinue;\n \t\tif (cnt == 1)\n@@ -992,16 +1168,17 @@ static void spill_to_stack(struct bpf_insn *insn, struct arg_track *at_out,\n * Clear all tracked callee stack slots overlapping the byte range\n * [off, off+sz-1] where off is a negative FP-relative offset.\n */\n-static void clear_overlapping_stack_slots(struct arg_track *at_stack, s16 off, u32 sz, int cnt)\n+static void clear_overlapping_stack_slots(struct arg_track *at_stack, u32 nslots, s16 off,\n+\t\t\t\t\t u32 sz, int cnt)\n {\n \tstruct arg_track none = { .frame = ARG_NONE };\n \n \tif (cnt == 0) {\n-\t\tfor (int i = 0; i \u003c MAX_ARG_SPILL_SLOTS; i++)\n+\t\tfor (int i = 0; i \u003c nslots; i++)\n \t\t\tat_stack[i] = __arg_track_join(at_stack[i], none);\n \t\treturn;\n \t}\n-\tfor (int i = 0; i \u003c MAX_ARG_SPILL_SLOTS; i++) {\n+\tfor (int i = 0; i \u003c nslots; i++) {\n \t\tint slot_start = -((i + 1) * 8);\n \t\tint slot_end = slot_start + 8;\n \n@@ -1019,33 +1196,33 @@ static void clear_overlapping_stack_slots(struct arg_track *at_stack, s16 off, u\n */\n static void clear_stack_for_all_offs(struct bpf_insn *insn,\n \t\t\t\t struct arg_track *at_out, int reg,\n-\t\t\t\t struct arg_track *at_stack_out, u32 sz)\n+\t\t\t\t struct arg_track *at_stack_out, u32 nslots, u32 sz)\n {\n \tint cnt, i;\n \n \tif (reg == BPF_REG_FP) {\n-\t\tclear_overlapping_stack_slots(at_stack_out, insn-\u003eoff, sz, 1);\n+\t\tclear_overlapping_stack_slots(at_stack_out, nslots, insn-\u003eoff, sz, 1);\n \t\treturn;\n \t}\n \tcnt = at_out[reg].off_cnt;\n \tif (cnt == 0) {\n-\t\tclear_overlapping_stack_slots(at_stack_out, 0, sz, cnt);\n+\t\tclear_overlapping_stack_slots(at_stack_out, nslots, 0, sz, cnt);\n \t\treturn;\n \t}\n \tfor (i = 0; i \u003c cnt; i++) {\n \t\ts16 fp_off;\n \n \t\tif (arg_add(at_out[reg].off[i], insn-\u003eoff, \u0026fp_off)) {\n-\t\t\tclear_overlapping_stack_slots(at_stack_out, 0, sz, 0);\n+\t\t\tclear_overlapping_stack_slots(at_stack_out, nslots, 0, sz, 0);\n \t\t\tbreak;\n \t\t}\n-\t\tclear_overlapping_stack_slots(at_stack_out, fp_off, sz, cnt);\n+\t\tclear_overlapping_stack_slots(at_stack_out, nslots, fp_off, sz, cnt);\n \t}\n }\n \n static void arg_track_log(struct bpf_verifier_env *env, struct bpf_insn *insn, int idx,\n \t\t\t struct arg_track *at_in, struct arg_track *at_stack_in,\n-\t\t\t struct arg_track *at_out, struct arg_track *at_stack_out)\n+\t\t\t struct arg_track *at_out, struct arg_track *at_stack_out, u32 nslots)\n {\n \tbool printed = false;\n \tint i;\n@@ -1077,7 +1254,7 @@ static void arg_track_log(struct bpf_verifier_env *env, struct bpf_insn *insn, i\n \t\tverbose(env, \"\\tsa%d: \", i); verbose_arg_track(env, \u0026at_in[ai]);\n \t\tverbose(env, \" -\u003e \"); verbose_arg_track(env, \u0026at_out[ai]);\n \t}\n-\tfor (i = 0; i \u003c MAX_ARG_SPILL_SLOTS; i++) {\n+\tfor (i = 0; i \u003c nslots; i++) {\n \t\tif (arg_track_eq(\u0026at_stack_out[i], \u0026at_stack_in[i]))\n \t\t\tcontinue;\n \t\tif (!printed) {\n@@ -1105,7 +1282,7 @@ static bool can_be_local_fp(int depth, int regno, struct arg_track *at)\n */\n static void arg_track_xfer(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \t\t\t int insn_idx,\n-\t\t\t struct arg_track *at_out, struct arg_track *at_stack_out,\n+\t\t\t struct arg_track *at_out, struct arg_track *at_stack_out, u32 nslots,\n \t\t\t const struct arg_track *at_stack_arg_entry,\n \t\t\t struct func_instance *instance,\n \t\t\t u32 *callsites)\n@@ -1189,14 +1366,15 @@ static void arg_track_xfer(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \t\t * ARG_IMPRECISE (off_cnt == 0).\n \t\t */\n \t\tif (src_is_local_fp \u0026\u0026 BPF_MODE(insn-\u003ecode) == BPF_MEM \u0026\u0026 sz == 8) {\n-\t\t\t*dst = fill_from_stack(insn, at_out, insn-\u003esrc_reg, at_stack_out, depth);\n+\t\t\t*dst = fill_from_stack(insn, at_out, insn-\u003esrc_reg, at_stack_out, nslots,\n+\t\t\t\t\t depth);\n \t\t} else if (src-\u003eframe \u003e= 0 \u0026\u0026 src-\u003eframe \u003c depth \u0026\u0026\n \t\t\t BPF_MODE(insn-\u003ecode) == BPF_MEM \u0026\u0026 sz == 8) {\n-\t\t\tstruct arg_track *parent_stack =\n+\t\t\tstruct spill_snapshot *parent =\n \t\t\t\tenv-\u003ecallsite_at_stack[callsites[src-\u003eframe]];\n \n \t\t\t*dst = fill_from_stack(insn, at_out, insn-\u003esrc_reg,\n-\t\t\t\t\t parent_stack, src-\u003eframe);\n+\t\t\t\t\t parent-\u003eat, parent-\u003eslots, src-\u003eframe);\n \t\t} else if (src-\u003eframe == ARG_IMPRECISE \u0026\u0026\n \t\t\t !(src-\u003emask \u0026 BIT(depth)) \u0026\u0026 src-\u003emask \u0026\u0026\n \t\t\t BPF_MODE(insn-\u003ecode) == BPF_MEM \u0026\u0026 sz == 8) {\n@@ -1218,12 +1396,12 @@ static void arg_track_xfer(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \t\tdst_is_local_fp = can_be_local_fp(depth, insn-\u003edst_reg, dst);\n \t\tif (dst_is_local_fp \u0026\u0026 BPF_MODE(insn-\u003ecode) == BPF_MEM)\n \t\t\tspill_to_stack(insn, at_out, insn-\u003edst_reg,\n-\t\t\t\t at_stack_out, src, sz);\n+\t\t\t\t at_stack_out, nslots, src, sz);\n \n \t\tif (BPF_MODE(insn-\u003ecode) == BPF_ATOMIC) {\n \t\t\tif (dst_is_local_fp \u0026\u0026 insn-\u003eimm != BPF_LOAD_ACQ)\n \t\t\t\tclear_stack_for_all_offs(insn, at_out, insn-\u003edst_reg,\n-\t\t\t\t\t\t\t at_stack_out, sz);\n+\t\t\t\t\t\t\t at_stack_out, nslots, sz);\n \n \t\t\tr = bpf_atomic_load_reg(insn);\n \t\t\tif (r \u003e= 0)\n@@ -1236,7 +1414,7 @@ static void arg_track_xfer(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \t\t/* BPF_ST to FP-derived dst: clear overlapping stack slots */\n \t\tif (dst_is_local_fp)\n \t\t\tclear_stack_for_all_offs(insn, at_out, insn-\u003edst_reg,\n-\t\t\t\t\t\t at_stack_out, sz);\n+\t\t\t\t\t\t at_stack_out, nslots, sz);\n \t}\n }\n \n@@ -1252,7 +1430,6 @@ static int record_stack_access_off(struct func_instance *instance, s64 fp_off,\n \t\t\t\t s64 access_bytes, u32 frame, u32 insn_idx)\n {\n \ts32 slot_hi, slot_lo;\n-\tspis_t mask;\n \n \tif (fp_off \u003e= 0)\n \t\t/*\n@@ -1264,27 +1441,19 @@ static int record_stack_access_off(struct func_instance *instance, s64 fp_off,\n \tif (access_bytes == S64_MIN) {\n \t\t/* helper/kfunc read unknown amount of bytes from fp_off until fp+0 */\n \t\tslot_hi = (-fp_off - 1) / STACK_SLOT_SZ;\n-\t\tmask = SPIS_ZERO;\n-\t\tspis_or_range(\u0026mask, 0, slot_hi);\n-\t\treturn mark_stack_read(instance, frame, insn_idx, mask);\n+\t\treturn mark_stack_read(instance, frame, insn_idx, 0, slot_hi);\n \t}\n \tif (access_bytes \u003e 0) {\n \t\t/* Mark any touched slot as use */\n \t\tslot_hi = (-fp_off - 1) / STACK_SLOT_SZ;\n \t\tslot_lo = max_t(s32, (-fp_off - access_bytes) / STACK_SLOT_SZ, 0);\n-\t\tmask = SPIS_ZERO;\n-\t\tspis_or_range(\u0026mask, slot_lo, slot_hi);\n-\t\treturn mark_stack_read(instance, frame, insn_idx, mask);\n+\t\treturn mark_stack_read(instance, frame, insn_idx, slot_lo, slot_hi);\n \t} else if (access_bytes \u003c 0) {\n \t\t/* Mark only fully covered slots as def */\n \t\taccess_bytes = -access_bytes;\n \t\tslot_hi = (-fp_off) / STACK_SLOT_SZ - 1;\n \t\tslot_lo = max_t(s32, (-fp_off - access_bytes + STACK_SLOT_SZ - 1) / STACK_SLOT_SZ, 0);\n-\t\tif (slot_lo \u003c= slot_hi) {\n-\t\t\tmask = SPIS_ZERO;\n-\t\t\tspis_or_range(\u0026mask, slot_lo, slot_hi);\n-\t\t\treturn mark_stack_write(instance, frame, insn_idx, mask);\n-\t\t}\n+\t\treturn mark_stack_write(instance, frame, insn_idx, slot_lo, slot_hi);\n \t}\n \treturn 0;\n }\n@@ -1293,7 +1462,7 @@ static int record_stack_access_off(struct func_instance *instance, s64 fp_off,\n * 'arg' is FP-derived argument to helper/kfunc or load/store that\n * reads (positive) or writes (negative) 'access_bytes' into 'use' or 'def'.\n */\n-static int record_stack_access(struct func_instance *instance,\n+static int record_stack_access(struct bpf_verifier_env *env, struct func_instance *instance,\n \t\t\t const struct arg_track *arg,\n \t\t\t s64 access_bytes, u32 frame, u32 insn_idx)\n {\n@@ -1303,7 +1472,7 @@ static int record_stack_access(struct func_instance *instance,\n \t\treturn 0;\n \tif (arg-\u003eoff_cnt == 0) {\n \t\tif (access_bytes \u003e 0 || access_bytes == S64_MIN)\n-\t\t\treturn mark_stack_read(instance, frame, insn_idx, SPIS_ALL);\n+\t\t\treturn mark_stack_read_all(env, instance, frame, insn_idx);\n \t\treturn 0;\n \t}\n \tif (access_bytes != S64_MIN \u0026\u0026 access_bytes \u003c 0 \u0026\u0026 arg-\u003eoff_cnt != 1)\n@@ -1322,7 +1491,8 @@ static int record_stack_access(struct func_instance *instance,\n * When a pointer is ARG_IMPRECISE, conservatively mark every frame in\n * the bitmask as fully used.\n */\n-static int record_imprecise(struct func_instance *instance, u32 mask, u32 insn_idx)\n+static int record_imprecise(struct bpf_verifier_env *env, struct func_instance *instance,\n+\t\t\t u32 mask, u32 insn_idx)\n {\n \tint depth = instance-\u003edepth;\n \tint f, err;\n@@ -1331,7 +1501,7 @@ static int record_imprecise(struct func_instance *instance, u32 mask, u32 insn_i\n \t\tif (!(mask \u0026 1))\n \t\t\tcontinue;\n \t\tif (f \u003c= depth) {\n-\t\t\terr = mark_stack_read(instance, f, insn_idx, SPIS_ALL);\n+\t\t\terr = mark_stack_read_all(env, instance, f, insn_idx);\n \t\t\tif (err)\n \t\t\t\treturn err;\n \t\t}\n@@ -1400,9 +1570,9 @@ static int record_load_store_access(struct bpf_verifier_env *env,\n \t}\n \n \tif (ptr-\u003eframe \u003e= 0 \u0026\u0026 ptr-\u003eframe \u003c= depth)\n-\t\treturn record_stack_access(instance, ptr, sz, ptr-\u003eframe, insn_idx);\n+\t\treturn record_stack_access(env, instance, ptr, sz, ptr-\u003eframe, insn_idx);\n \tif (ptr-\u003eframe == ARG_IMPRECISE)\n-\t\treturn record_imprecise(instance, ptr-\u003emask, insn_idx);\n+\t\treturn record_imprecise(env, instance, ptr-\u003emask, insn_idx);\n \t/* ARG_NONE: not derived from any frame pointer, skip */\n \treturn 0;\n }\n@@ -1427,7 +1597,7 @@ static int record_arg_access(struct bpf_verifier_env *env,\n \t\tbytes = bpf_kfunc_stack_access_bytes(env, insn, arg_idx, insn_idx);\n \t} else {\n \t\tfor (int f = 0; f \u003c= depth; f++) {\n-\t\t\terr = mark_stack_read(instance, f, insn_idx, SPIS_ALL);\n+\t\t\terr = mark_stack_read_all(env, instance, f, insn_idx);\n \t\t\tif (err)\n \t\t\t\treturn err;\n \t\t}\n@@ -1437,9 +1607,9 @@ static int record_arg_access(struct bpf_verifier_env *env,\n \t\treturn 0;\n \n \tif (frame \u003e= 0 \u0026\u0026 frame \u003c= depth)\n-\t\terr = record_stack_access(instance, at, bytes, frame, insn_idx);\n+\t\terr = record_stack_access(env, instance, at, bytes, frame, insn_idx);\n \telse if (frame == ARG_IMPRECISE)\n-\t\terr = record_imprecise(instance, at-\u003emask, insn_idx);\n+\t\terr = record_imprecise(env, instance, at-\u003emask, insn_idx);\n \treturn err;\n }\n \n@@ -1542,7 +1712,7 @@ struct subprog_at_info {\n static void print_subprog_arg_access(struct bpf_verifier_env *env,\n \t\t\t\t int subprog,\n \t\t\t\t struct subprog_at_info *info,\n-\t\t\t\t struct arg_track (*at_stack_in)[MAX_ARG_SPILL_SLOTS])\n+\t\t\t\t struct arg_track *at_stack_in, u32 nslots)\n {\n \tstruct bpf_insn *insns = env-\u003eprog-\u003einsnsi;\n \tint start = env-\u003esubprog_info[subprog].start;\n@@ -1575,8 +1745,8 @@ static void print_subprog_arg_access(struct bpf_verifier_env *env,\n \t\t\t\t\thas_extra = true;\n \t\t}\n \t\tif (is_ldx_stx_call) {\n-\t\t\tfor (r = 0; r \u003c MAX_ARG_SPILL_SLOTS; r++)\n-\t\t\t\tif (arg_is_fp(\u0026at_stack_in[i][r]))\n+\t\t\tfor (r = 0; r \u003c nslots; r++)\n+\t\t\t\tif (arg_is_fp(\u0026at_stack_in[(size_t)i * nslots + r]))\n \t\t\t\t\thas_extra = true;\n \t\t}\n \n@@ -1606,11 +1776,11 @@ static void print_subprog_arg_access(struct bpf_verifier_env *env,\n \t\t}\n \n \t\tif (is_ldx_stx_call) {\n-\t\t\tfor (r = 0; r \u003c MAX_ARG_SPILL_SLOTS; r++) {\n-\t\t\t\tif (!arg_is_fp(\u0026at_stack_in[i][r]))\n+\t\t\tfor (r = 0; r \u003c nslots; r++) {\n+\t\t\t\tif (!arg_is_fp(\u0026at_stack_in[(size_t)i * nslots + r]))\n \t\t\t\t\tcontinue;\n \t\t\t\tverbose(env, \" fp%+d=\", -(r + 1) * 8);\n-\t\t\t\tverbose_arg_track(env, \u0026at_stack_in[i][r]);\n+\t\t\t\tverbose_arg_track(env, \u0026at_stack_in[(size_t)i * nslots + r]);\n \t\t\t}\n \t\t}\n \n@@ -1643,9 +1813,10 @@ static int compute_subprog_args(struct bpf_verifier_env *env,\n \tint end = env-\u003esubprog_info[subprog + 1].start;\n \tint po_end = env-\u003esubprog_info[subprog + 1].postorder_start;\n \tint len = end - start;\n+\tu32 nslots = min(subprog_spill_slots(env, start, end), spill_slots_affordable(len));\n \tstruct arg_track (*at_in)[MAX_AT_TRACK_REGS] = NULL;\n \tstruct arg_track at_out[MAX_AT_TRACK_REGS];\n-\tstruct arg_track (*at_stack_in)[MAX_ARG_SPILL_SLOTS] = NULL;\n+\tstruct arg_track *at_stack_in = NULL;\n \tstruct arg_track *at_stack_out = NULL;\n \tstruct arg_track at_stack_arg_entry[MAX_STACK_ARG_SLOTS];\n \tstruct arg_track unvisited = { .frame = ARG_UNVISITED };\n@@ -1657,19 +1828,19 @@ static int compute_subprog_args(struct bpf_verifier_env *env,\n \tif (!at_in)\n \t\tgoto err_free;\n \n-\tat_stack_in = kvmalloc_objs(*at_stack_in, len, GFP_KERNEL_ACCOUNT);\n+\tat_stack_in = kvmalloc_objs(*at_stack_in, (size_t)len * nslots, GFP_KERNEL_ACCOUNT);\n \tif (!at_stack_in)\n \t\tgoto err_free;\n \n-\tat_stack_out = kvmalloc_objs(*at_stack_out, MAX_ARG_SPILL_SLOTS, GFP_KERNEL_ACCOUNT);\n+\tat_stack_out = kvmalloc_objs(*at_stack_out, nslots, GFP_KERNEL_ACCOUNT);\n \tif (!at_stack_out)\n \t\tgoto err_free;\n \n \tfor (i = 0; i \u003c len; i++) {\n \t\tfor (r = 0; r \u003c MAX_AT_TRACK_REGS; r++)\n \t\t\tat_in[i][r] = unvisited;\n-\t\tfor (r = 0; r \u003c MAX_ARG_SPILL_SLOTS; r++)\n-\t\t\tat_stack_in[i][r] = unvisited;\n+\t\tfor (r = 0; r \u003c nslots; r++)\n+\t\t\tat_stack_in[(size_t)i * nslots + r] = unvisited;\n \t}\n \n \tfor (r = 0; r \u003c MAX_AT_TRACK_REGS; r++)\n@@ -1685,8 +1856,8 @@ static int compute_subprog_args(struct bpf_verifier_env *env,\n \t}\n \n \t/* Entry: all stack slots are ARG_NONE */\n-\tfor (r = 0; r \u003c MAX_ARG_SPILL_SLOTS; r++)\n-\t\tat_stack_in[0][r] = none;\n+\tfor (r = 0; r \u003c nslots; r++)\n+\t\tat_stack_in[r] = none;\n \n \t/* Entry: incoming stack args from caller, or ARG_NONE for main */\n \tfor (r = 0; r \u003c MAX_STACK_ARG_SLOTS; r++)\n@@ -1708,11 +1879,12 @@ static int compute_subprog_args(struct bpf_verifier_env *env,\n \t\t\tcontinue;\n \n \t\tmemcpy(at_out, at_in[i], sizeof(at_out));\n-\t\tmemcpy(at_stack_out, at_stack_in[i], MAX_ARG_SPILL_SLOTS * sizeof(*at_stack_out));\n+\t\tmemcpy(at_stack_out, \u0026at_stack_in[(size_t)i * nslots], nslots * sizeof(*at_stack_out));\n \n-\t\targ_track_xfer(env, insn, idx, at_out, at_stack_out,\n+\t\targ_track_xfer(env, insn, idx, at_out, at_stack_out, nslots,\n \t\t\t at_stack_arg_entry, instance, callsites);\n-\t\targ_track_log(env, insn, idx, at_in[i], at_stack_in[i], at_out, at_stack_out);\n+\t\targ_track_log(env, insn, idx, at_in[i], \u0026at_stack_in[(size_t)i * nslots], at_out,\n+\t\t\t at_stack_out, nslots);\n \n \t\t/* Propagate to successors within this subprogram */\n \t\tsucc = bpf_insn_successors(env, idx);\n@@ -1729,9 +1901,10 @@ static int compute_subprog_args(struct bpf_verifier_env *env,\n \t\t\t\tchanged |= arg_track_join(env, idx, target, r,\n \t\t\t\t\t\t\t \u0026at_in[ti][r], at_out[r]);\n \n-\t\t\tfor (r = 0; r \u003c MAX_ARG_SPILL_SLOTS; r++)\n+\t\t\tfor (r = 0; r \u003c nslots; r++)\n \t\t\t\tchanged |= arg_track_join(env, idx, target, -r - 1,\n-\t\t\t\t\t\t\t \u0026at_stack_in[ti][r], at_stack_out[r]);\n+\t\t\t\t\t\t\t \u0026at_stack_in[(size_t)ti * nslots + r],\n+\t\t\t\t\t\t\t at_stack_out[r]);\n \t\t}\n \t}\n \tif (changed)\n@@ -1754,23 +1927,24 @@ static int compute_subprog_args(struct bpf_verifier_env *env,\n \t\t}\n \n \t\tif (bpf_pseudo_call(insn) || bpf_calls_callback(env, idx)) {\n+\t\t\tstruct spill_snapshot *snap;\n+\n \t\t\tkvfree(env-\u003ecallsite_at_stack[idx]);\n-\t\t\tenv-\u003ecallsite_at_stack[idx] =\n-\t\t\t\tkvmalloc_objs(*env-\u003ecallsite_at_stack[idx],\n-\t\t\t\t\t MAX_ARG_SPILL_SLOTS, GFP_KERNEL_ACCOUNT);\n-\t\t\tif (!env-\u003ecallsite_at_stack[idx]) {\n+\t\t\tenv-\u003ecallsite_at_stack[idx] = kvmalloc_flex(*snap, at, nslots, GFP_KERNEL_ACCOUNT);\n+\t\t\tsnap = env-\u003ecallsite_at_stack[idx];\n+\t\t\tif (!snap) {\n \t\t\t\terr = -ENOMEM;\n \t\t\t\tgoto err_free;\n \t\t\t}\n-\t\t\tmemcpy(env-\u003ecallsite_at_stack[idx],\n-\t\t\t at_stack_in[i], sizeof(struct arg_track) * MAX_ARG_SPILL_SLOTS);\n+\t\t\tsnap-\u003eslots = nslots;\n+\t\t\tmemcpy(snap-\u003eat, \u0026at_stack_in[(size_t)i * nslots], nslots * sizeof(*snap-\u003eat));\n \t\t}\n \t}\n \n \tinfo-\u003eat_in = at_in;\n \tat_in = NULL;\n \tinfo-\u003elen = len;\n-\tprint_subprog_arg_access(env, subprog, info, at_stack_in);\n+\tprint_subprog_arg_access(env, subprog, info, at_stack_in, nslots);\n \terr = 0;\n \n err_free:\n@@ -1797,36 +1971,52 @@ static bool has_fp_args(struct arg_track *args)\n * may_read: union (any pass might read the slot).\n * must_write: intersection (only slots written on ALL passes are guaranteed).\n * live_before is recomputed by a subsequent update_instance() on @dst.\n+ *\n+ * The two instances may have settled on different mask widths for the same\n+ * frame, so @dst is widened to cover @src first. A word only @dst has counts\n+ * as zero on the @src side: it unions into may_read as a no-op and intersects\n+ * must_write to empty.\n */\n-static void merge_instances(struct func_instance *dst, struct func_instance *src)\n+static int merge_instances(struct func_instance *dst, struct func_instance *src)\n {\n-\tint f, i;\n+\tstruct frame_masks *d, *s;\n+\tu32 f, i, w;\n \n \tfor (f = 0; f \u003c= dst-\u003edepth; f++) {\n-\t\tif (!src-\u003eframes[f]) {\n+\t\ts = src-\u003eframes[f];\n+\t\td = dst-\u003eframes[f];\n+\t\tif (!s) {\n \t\t\t/* This pass didn't touch frame f — must_write intersects with empty. */\n-\t\t\tif (dst-\u003eframes[f])\n+\t\t\tif (d)\n \t\t\t\tfor (i = 0; i \u003c dst-\u003einsn_cnt; i++)\n-\t\t\t\t\tdst-\u003eframes[f][i].must_write = SPIS_ZERO;\n+\t\t\t\t\tbitmap_zero(rel_mask(d, i, FM_MUST_WRITE),\n+\t\t\t\t\t\t frame_mask_bits(d));\n \t\t\tcontinue;\n \t\t}\n-\t\tif (!dst-\u003eframes[f]) {\n+\t\tif (!d) {\n \t\t\t/* Previous pass didn't touch frame f — take src, zero must_write. */\n-\t\t\tdst-\u003eframes[f] = src-\u003eframes[f];\n+\t\t\tdst-\u003eframes[f] = s;\n \t\t\tsrc-\u003eframes[f] = NULL;\n \t\t\tfor (i = 0; i \u003c dst-\u003einsn_cnt; i++)\n-\t\t\t\tdst-\u003eframes[f][i].must_write = SPIS_ZERO;\n+\t\t\t\tbitmap_zero(rel_mask(s, i, FM_MUST_WRITE), frame_mask_bits(s));\n \t\t\tcontinue;\n \t\t}\n+\t\td = widen_frame_masks(dst, f, s-\u003ewords);\n+\t\tif (!d)\n+\t\t\treturn -ENOMEM;\n \t\tfor (i = 0; i \u003c dst-\u003einsn_cnt; i++) {\n-\t\t\tdst-\u003eframes[f][i].may_read =\n-\t\t\t\tspis_or(dst-\u003eframes[f][i].may_read,\n-\t\t\t\t\tsrc-\u003eframes[f][i].may_read);\n-\t\t\tdst-\u003eframes[f][i].must_write =\n-\t\t\t\tspis_and(dst-\u003eframes[f][i].must_write,\n-\t\t\t\t\t src-\u003eframes[f][i].must_write);\n+\t\t\tunsigned long *dst_read = rel_mask(d, i, FM_MAY_READ);\n+\t\t\tunsigned long *dst_write = rel_mask(d, i, FM_MUST_WRITE);\n+\t\t\tunsigned long *src_read = rel_mask(s, i, FM_MAY_READ);\n+\t\t\tunsigned long *src_write = rel_mask(s, i, FM_MUST_WRITE);\n+\n+\t\t\tfor (w = 0; w \u003c d-\u003ewords; w++) {\n+\t\t\t\tdst_read[w] |= w \u003c s-\u003ewords ? src_read[w] : 0;\n+\t\t\t\tdst_write[w] \u0026= w \u003c s-\u003ewords ? src_write[w] : 0;\n+\t\t\t}\n \t\t}\n \t}\n+\treturn 0;\n }\n \n static struct func_instance *fresh_instance(struct func_instance *src)\n@@ -1941,7 +2131,7 @@ static int analyze_subprog(struct bpf_verifier_env *env,\n \t\t\t\tif (info[subprog].at_in[j][caller_reg].frame == ARG_NONE)\n \t\t\t\t\tcontinue;\n \t\t\t\tfor (int f = 0; f \u003c= depth; f++) {\n-\t\t\t\t\terr = mark_stack_read(instance, f, idx, SPIS_ALL);\n+\t\t\t\t\terr = mark_stack_read_all(env, instance, f, idx);\n \t\t\t\t\tif (err)\n \t\t\t\t\t\tgoto out_free;\n \t\t\t\t}\n@@ -1980,13 +2170,18 @@ static int analyze_subprog(struct bpf_verifier_env *env,\n \t\t/* Pull callee's entry liveness back to caller's callsite */\n \t\t{\n \t\t\tu32 callee_start = callee_instance-\u003esubprog_start;\n-\t\t\tstruct per_frame_masks *entry;\n+\t\t\tstruct frame_masks *callee_fm;\n \n \t\t\tfor (int f = 0; f \u003c callee_instance-\u003edepth; f++) {\n-\t\t\t\tentry = get_frame_masks(callee_instance, f, callee_start);\n-\t\t\t\tif (!entry)\n+\t\t\t\tcallee_fm = callee_instance-\u003eframes[f];\n+\t\t\t\tif (!callee_fm)\n \t\t\t\t\tcontinue;\n-\t\t\t\terr = mark_stack_read(instance, f, idx, entry-\u003elive_before);\n+\t\t\t\terr = mark_stack_read_mask(instance, f, idx,\n+\t\t\t\t\t\t\t rel_mask(callee_fm,\n+\t\t\t\t\t\t\t\t relative_idx(callee_instance,\n+\t\t\t\t\t\t\t\t\t\t callee_start),\n+\t\t\t\t\t\t\t\t FM_LIVE_BEFORE),\n+\t\t\t\t\t\t\t callee_fm-\u003ewords);\n \t\t\t\tif (err)\n \t\t\t\t\tgoto out_free;\n \t\t\t}\n@@ -1994,9 +2189,11 @@ static int analyze_subprog(struct bpf_verifier_env *env,\n \t}\n \n \tif (prev_instance) {\n-\t\tmerge_instances(prev_instance, instance);\n+\t\terr = merge_instances(prev_instance, instance);\n \t\tfree_instance(instance);\n \t\tinstance = prev_instance;\n+\t\tif (err)\n+\t\t\treturn err;\n \t}\n \tupdate_instance(env, instance);\n \treturn 0;\ndiff --git a/kernel/bpf/log.c b/kernel/bpf/log.c\nindex fb032dfdc0dee..d850a7863d2ed 100644\n--- a/kernel/bpf/log.c\n+++ b/kernel/bpf/log.c\n@@ -716,7 +716,8 @@ void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_verifie\n \t\tverbose(env, \"=\");\n \t\tprint_reg_state(env, state, reg);\n \t}\n-\tfor (i = 0; i \u003c state-\u003eallocated_stack / BPF_REG_SIZE; i++) {\n+\tfor (i = 0; i \u003c bpf_stack_nr_slots(state); i++) {\n+\t\tstruct bpf_stack_state *slot = bpf_stack_slot(state, i);\n \t\tchar types_buf[BPF_REG_SIZE + 1];\n \t\tconst char *sep = \"\";\n \t\tbool valid = false;\n@@ -727,7 +728,7 @@ void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_verifie\n \t\t\tcontinue;\n \n \t\tfor (j = 0; j \u003c BPF_REG_SIZE; j++) {\n-\t\t\tslot_type = state-\u003estack[i].slot_type[j];\n+\t\t\tslot_type = slot-\u003eslot_type[j];\n \t\t\tif (slot_type != STACK_INVALID \u0026\u0026 slot_type != STACK_POISON)\n \t\t\t\tvalid = true;\n \t\t\ttypes_buf[j] = slot_type_char[slot_type];\n@@ -736,12 +737,12 @@ void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_verifie\n \t\tif (!valid)\n \t\t\tcontinue;\n \n-\t\treg = \u0026state-\u003estack[i].spilled_ptr;\n-\t\tswitch (state-\u003estack[i].slot_type[BPF_REG_SIZE - 1]) {\n+\t\treg = \u0026slot-\u003espilled_ptr;\n+\t\tswitch (slot-\u003eslot_type[BPF_REG_SIZE - 1]) {\n \t\tcase STACK_SPILL:\n \t\t\t/* print MISC/ZERO/INVALID slots above subreg spill */\n \t\t\tfor (j = 0; j \u003c BPF_REG_SIZE; j++)\n-\t\t\t\tif (state-\u003estack[i].slot_type[j] == STACK_SPILL)\n+\t\t\t\tif (slot-\u003eslot_type[j] == STACK_SPILL)\n \t\t\t\t\tbreak;\n \t\t\ttypes_buf[j] = '\\0';\n \n@@ -751,7 +752,7 @@ void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_verifie\n \t\tcase STACK_DYNPTR:\n \t\t\t/* skip to main dynptr slot */\n \t\t\ti += BPF_DYNPTR_NR_SLOTS - 1;\n-\t\t\treg = \u0026state-\u003estack[i].spilled_ptr;\n+\t\t\treg = \u0026bpf_stack_slot(state, i)-\u003espilled_ptr;\n \n \t\t\tverbose(env, \" fp%d\", (-i - 1) * BPF_REG_SIZE);\n \t\t\tverbose(env, \"=dynptr_%s(\", dynptr_type_str(reg-\u003edynptr.type));\ndiff --git a/kernel/bpf/states.c b/kernel/bpf/states.c\nindex 66fb11b6c6a76..a07af8f11a5a2 100644\n--- a/kernel/bpf/states.c\n+++ b/kernel/bpf/states.c\n@@ -335,21 +335,18 @@ static bool check_ids(u32 old_id, u32 cur_id, struct bpf_idmap *idmap)\n \t\t\treturn false;\n \t}\n \n-\t/* Reached the end of known mappings; haven't seen this id before */\n-\tif (idmap-\u003ecnt \u003c BPF_ID_MAP_SIZE) {\n-\t\tmap[idmap-\u003ecnt].old = old_id;\n-\t\tmap[idmap-\u003ecnt].cur = cur_id;\n-\t\tidmap-\u003ecnt++;\n-\t\treturn true;\n-\t}\n-\n \t/*\n-\t * idmap slots are bounded by the number of registers and stack slots.\n-\t * Since referenced dynptrs acquire intermediate references that do\n-\t * not live in either, so the map can be exhausted. Since it is unlikely,\n-\t * fail the verification by treating the states as not equivalent.\n+\t * Reached the end of known mappings; haven't seen this id before. If\n+\t * the map cannot grow, treat the states as not equivalent, which only\n+\t * costs pruning.\n \t */\n-\treturn false;\n+\tif (!bpf_id_scratch_reserve((void **)\u0026idmap-\u003emap, \u0026idmap-\u003ecap, idmap-\u003ecnt, sizeof(*map)))\n+\t\treturn false;\n+\tmap = idmap-\u003emap;\n+\tmap[idmap-\u003ecnt].old = old_id;\n+\tmap[idmap-\u003ecnt].cur = cur_id;\n+\tidmap-\u003ecnt++;\n+\treturn true;\n }\n \n /*\n@@ -415,14 +412,14 @@ static void __clean_func_state(struct bpf_verifier_env *env,\n \t * half_spi 2*i → lower half: slot_type[0..3] (closer to FP)\n \t * half_spi 2*i+1 → upper half: slot_type[4..7] (farther from FP)\n \t */\n-\tfor (i = 0; i \u003c st-\u003eallocated_stack / BPF_REG_SIZE; i++) {\n+\tfor (i = 0; i \u003c bpf_stack_nr_slots(st); i++) {\n \t\tbool lo_live = bpf_stack_slot_alive(env, frame, i * 2);\n \t\tbool hi_live = bpf_stack_slot_alive(env, frame, i * 2 + 1);\n \n \t\tif (!hi_live || !lo_live) {\n \t\t\tint start = !lo_live ? 0 : BPF_REG_SIZE / 2;\n \t\t\tint end = !hi_live ? BPF_REG_SIZE : BPF_REG_SIZE / 2;\n-\t\t\tu8 stype = st-\u003estack[i].slot_type[7];\n+\t\t\tu8 stype = bpf_stack_slot(st, i)-\u003eslot_type[7];\n \n \t\t\t/*\n \t\t\t * Don't clear special slots.\n@@ -442,7 +439,7 @@ static void __clean_func_state(struct bpf_verifier_env *env,\n \t\t\t * rejecting as non-scalar register fills.\n \t\t\t */\n \t\t\tif (!hi_live) {\n-\t\t\t\tstruct bpf_reg_state *spill = \u0026st-\u003estack[i].spilled_ptr;\n+\t\t\t\tstruct bpf_reg_state *spill = \u0026bpf_stack_slot(st, i)-\u003espilled_ptr;\n \n \t\t\t\tif (lo_live \u0026\u0026 stype == STACK_SPILL) {\n \t\t\t\t\tif (spill-\u003etype != SCALAR_VALUE)\n@@ -454,7 +451,7 @@ static void __clean_func_state(struct bpf_verifier_env *env,\n \t\t\t\t\tif (bpf_register_is_null(spill))\n \t\t\t\t\t\tcontinue;\n \t\t\t\t\tfor (j = 0; j \u003c 4; j++) {\n-\t\t\t\t\t\tu8 *t = \u0026st-\u003estack[i].slot_type[j];\n+\t\t\t\t\t\tu8 *t = \u0026bpf_stack_slot(st, i)-\u003eslot_type[j];\n \n \t\t\t\t\t\tif (*t == STACK_SPILL)\n \t\t\t\t\t\t\t*t = STACK_MISC;\n@@ -463,7 +460,7 @@ static void __clean_func_state(struct bpf_verifier_env *env,\n \t\t\t\tbpf_mark_reg_not_init(env, spill);\n \t\t\t}\n \t\t\tfor (j = start; j \u003c end; j++)\n-\t\t\t\tst-\u003estack[i].slot_type[j] = STACK_POISON;\n+\t\t\t\tbpf_stack_slot(st, i)-\u003eslot_type[j] = STACK_POISON;\n \t\t}\n \t}\n }\n@@ -707,37 +704,38 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,\n \t * didn't use them\n \t */\n \tfor (i = 0; i \u003c old-\u003eallocated_stack; i++) {\n+\t\tstruct bpf_stack_state *old_slot, *cur_slot;\n \t\tstruct bpf_reg_state *old_reg, *cur_reg;\n \t\tint im = i % BPF_REG_SIZE;\n+\t\tu8 old_type;\n \n \t\tspi = i / BPF_REG_SIZE;\n+\t\told_slot = bpf_stack_slot(old, spi);\n+\t\told_type = old_slot-\u003eslot_type[im];\n+\t\tcur_slot = i \u003c cur-\u003eallocated_stack ? bpf_stack_slot(cur, spi) : NULL;\n \n \t\tif (exact == EXACT) {\n-\t\t\tu8 old_type = old-\u003estack[spi].slot_type[i % BPF_REG_SIZE];\n-\t\t\tu8 cur_type = i \u003c cur-\u003eallocated_stack ?\n-\t\t\t\t cur-\u003estack[spi].slot_type[i % BPF_REG_SIZE] : STACK_INVALID;\n+\t\t\tu8 cur_type = cur_slot ? cur_slot-\u003eslot_type[im] : STACK_INVALID;\n \n \t\t\t/* STACK_INVALID and STACK_POISON are equivalent for pruning */\n \t\t\tif (old_type == STACK_POISON)\n \t\t\t\told_type = STACK_INVALID;\n \t\t\tif (cur_type == STACK_POISON)\n \t\t\t\tcur_type = STACK_INVALID;\n-\t\t\tif (i \u003e= cur-\u003eallocated_stack || old_type != cur_type)\n+\t\t\tif (!cur_slot || old_type != cur_type)\n \t\t\t\treturn false;\n \t\t}\n \n-\t\tif (old-\u003estack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID ||\n-\t\t old-\u003estack[spi].slot_type[i % BPF_REG_SIZE] == STACK_POISON)\n+\t\tif (old_type == STACK_INVALID || old_type == STACK_POISON)\n \t\t\tcontinue;\n \n-\t\tif (env-\u003eallow_uninit_stack \u0026\u0026\n-\t\t old-\u003estack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC)\n+\t\tif (env-\u003eallow_uninit_stack \u0026\u0026 old_type == STACK_MISC)\n \t\t\tcontinue;\n \n \t\t/* explored stack has more populated slots than current stack\n \t\t * and these slots were used\n \t\t */\n-\t\tif (i \u003e= cur-\u003eallocated_stack)\n+\t\tif (!cur_slot)\n \t\t\treturn false;\n \n \t\t/*\n@@ -747,8 +745,8 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,\n \t\t * regsafe() to ensure scalar ids are compared.\n \t\t */\n \t\tif (im == 0 || im == 4) {\n-\t\t\told_reg = scalar_reg_for_stack(env, \u0026old-\u003estack[spi], im);\n-\t\t\tcur_reg = scalar_reg_for_stack(env, \u0026cur-\u003estack[spi], im);\n+\t\t\told_reg = scalar_reg_for_stack(env, old_slot, im);\n+\t\t\tcur_reg = scalar_reg_for_stack(env, cur_slot, im);\n \t\t\tif (old_reg \u0026\u0026 cur_reg) {\n \t\t\t\tif (!regsafe(env, old_reg, cur_reg, idmap, exact))\n \t\t\t\t\treturn false;\n@@ -761,21 +759,19 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,\n \t\t * it will be safe with zero-initialized stack.\n \t\t * The opposite is not true\n \t\t */\n-\t\tif (old-\u003estack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC \u0026\u0026\n-\t\t cur-\u003estack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO)\n+\t\tif (old_type == STACK_MISC \u0026\u0026 cur_slot-\u003eslot_type[im] == STACK_ZERO)\n \t\t\tcontinue;\n-\t\tif (old-\u003estack[spi].slot_type[i % BPF_REG_SIZE] !=\n-\t\t cur-\u003estack[spi].slot_type[i % BPF_REG_SIZE])\n+\t\tif (old_type != cur_slot-\u003eslot_type[im])\n \t\t\t/* Ex: old explored (safe) state has STACK_SPILL in\n \t\t\t * this stack slot, but current has STACK_MISC -\u003e\n \t\t\t * this verifier states are not equivalent,\n \t\t\t * return false to continue verification of this path\n \t\t\t */\n \t\t\treturn false;\n-\t\tif (i % BPF_REG_SIZE != BPF_REG_SIZE - 1)\n+\t\tif (im != BPF_REG_SIZE - 1)\n \t\t\tcontinue;\n \t\t/* Both old and cur are having same slot_type */\n-\t\tswitch (old-\u003estack[spi].slot_type[BPF_REG_SIZE - 1]) {\n+\t\tswitch (old_type) {\n \t\tcase STACK_SPILL:\n \t\t\t/* when explored and current stack slot are both storing\n \t\t\t * spilled registers, check that stored pointers types\n@@ -787,13 +783,13 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,\n \t\t\t * such verifier states are not equivalent.\n \t\t\t * return false to continue verification of this path\n \t\t\t */\n-\t\t\tif (!regsafe(env, \u0026old-\u003estack[spi].spilled_ptr,\n-\t\t\t\t \u0026cur-\u003estack[spi].spilled_ptr, idmap, exact))\n+\t\t\tif (!regsafe(env, \u0026old_slot-\u003espilled_ptr, \u0026cur_slot-\u003espilled_ptr,\n+\t\t\t\t idmap, exact))\n \t\t\t\treturn false;\n \t\t\tbreak;\n \t\tcase STACK_DYNPTR:\n-\t\t\told_reg = \u0026old-\u003estack[spi].spilled_ptr;\n-\t\t\tcur_reg = \u0026cur-\u003estack[spi].spilled_ptr;\n+\t\t\told_reg = \u0026old_slot-\u003espilled_ptr;\n+\t\t\tcur_reg = \u0026cur_slot-\u003espilled_ptr;\n \t\t\tif (old_reg-\u003edynptr.type != cur_reg-\u003edynptr.type ||\n \t\t\t old_reg-\u003edynptr.first_slot != cur_reg-\u003edynptr.first_slot ||\n \t\t\t !check_ids(old_reg-\u003eid, cur_reg-\u003eid, idmap) ||\n@@ -801,8 +797,8 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,\n \t\t\t\treturn false;\n \t\t\tbreak;\n \t\tcase STACK_ITER:\n-\t\t\told_reg = \u0026old-\u003estack[spi].spilled_ptr;\n-\t\t\tcur_reg = \u0026cur-\u003estack[spi].spilled_ptr;\n+\t\t\told_reg = \u0026old_slot-\u003espilled_ptr;\n+\t\t\tcur_reg = \u0026cur_slot-\u003espilled_ptr;\n \t\t\t/* iter.depth is not compared between states as it\n \t\t\t * doesn't matter for correctness and would otherwise\n \t\t\t * prevent convergence; we maintain it only to prevent\n@@ -818,8 +814,8 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,\n \t\t\t\treturn false;\n \t\t\tbreak;\n \t\tcase STACK_IRQ_FLAG:\n-\t\t\told_reg = \u0026old-\u003estack[spi].spilled_ptr;\n-\t\t\tcur_reg = \u0026cur-\u003estack[spi].spilled_ptr;\n+\t\t\told_reg = \u0026old_slot-\u003espilled_ptr;\n+\t\t\tcur_reg = \u0026cur_slot-\u003espilled_ptr;\n \t\t\tif (!check_ids(old_reg-\u003eid, cur_reg-\u003eid, idmap) ||\n \t\t\t old_reg-\u003eirq.kfunc_class != cur_reg-\u003eirq.kfunc_class)\n \t\t\t\treturn false;\n@@ -966,6 +962,27 @@ static bool func_states_equal(struct bpf_verifier_env *env, struct bpf_func_stat\n \treturn true;\n }\n \n+/*\n+ * Make room for one more entry in an id scratch array, doubling it as needed.\n+ * Returns false if it could not grow; callers then treat the id as unknown\n+ * or the states as different, which is always safe.\n+ */\n+bool bpf_id_scratch_reserve(void **arr, u32 *cap, u32 cnt, size_t elem_size)\n+{\n+\tu32 new_cap;\n+\tvoid *p;\n+\n+\tif (cnt \u003c *cap)\n+\t\treturn true;\n+\tnew_cap = *cap ? *cap * 2 : 64;\n+\tp = krealloc_array(*arr, new_cap, elem_size, GFP_KERNEL_ACCOUNT | __GFP_NOWARN);\n+\tif (!p)\n+\t\treturn false;\n+\t*arr = p;\n+\t*cap = new_cap;\n+\treturn true;\n+}\n+\n static void reset_idmap_scratch(struct bpf_verifier_env *env)\n {\n \tstruct bpf_idmap *idmap = \u0026env-\u003eidmap_scratch;\n@@ -1043,10 +1060,12 @@ static int propagate_precision(struct bpf_verifier_env *env,\n \t\t\tfirst = false;\n \t\t}\n \n-\t\tfor (i = 0; i \u003c state-\u003eallocated_stack / BPF_REG_SIZE; i++) {\n-\t\t\tif (!bpf_is_spilled_reg(\u0026state-\u003estack[i]))\n+\t\tfor (i = 0; i \u003c bpf_stack_nr_slots(state); i++) {\n+\t\t\tstruct bpf_stack_state *ss = bpf_stack_slot(state, i);\n+\n+\t\t\tif (!bpf_is_spilled_reg(ss))\n \t\t\t\tcontinue;\n-\t\t\tstate_reg = \u0026state-\u003estack[i].spilled_ptr;\n+\t\t\tstate_reg = \u0026ss-\u003espilled_ptr;\n \t\t\tif (state_reg-\u003etype != SCALAR_VALUE ||\n \t\t\t !state_reg-\u003eprecise)\n \t\t\t\tcontinue;\n@@ -1192,15 +1211,17 @@ static bool iter_active_depths_differ(struct bpf_verifier_state *old, struct bpf\n \n \tfor (fr = old-\u003ecurframe; fr \u003e= 0; fr--) {\n \t\tstate = old-\u003eframe[fr];\n-\t\tfor (i = 0; i \u003c state-\u003eallocated_stack / BPF_REG_SIZE; i++) {\n-\t\t\tif (state-\u003estack[i].slot_type[0] != STACK_ITER)\n+\t\tfor (i = 0; i \u003c bpf_stack_nr_slots(state); i++) {\n+\t\t\tstruct bpf_stack_state *ss = bpf_stack_slot(state, i);\n+\n+\t\t\tif (ss-\u003eslot_type[0] != STACK_ITER)\n \t\t\t\tcontinue;\n \n-\t\t\tslot = \u0026state-\u003estack[i].spilled_ptr;\n+\t\t\tslot = \u0026ss-\u003espilled_ptr;\n \t\t\tif (slot-\u003eiter.state != BPF_ITER_STATE_ACTIVE)\n \t\t\t\tcontinue;\n \n-\t\t\tcur_slot = \u0026cur-\u003eframe[fr]-\u003estack[i].spilled_ptr;\n+\t\t\tcur_slot = \u0026bpf_stack_slot(cur-\u003eframe[fr], i)-\u003espilled_ptr;\n \t\t\tif (cur_slot-\u003eiter.depth != slot-\u003eiter.depth)\n \t\t\t\treturn true;\n \t\t}\n@@ -1222,10 +1243,12 @@ static void mark_all_scalars_imprecise(struct bpf_verifier_env *env, struct bpf_\n \t\t\t\tcontinue;\n \t\t\treg-\u003eprecise = false;\n \t\t}\n-\t\tfor (j = 0; j \u003c func-\u003eallocated_stack / BPF_REG_SIZE; j++) {\n-\t\t\tif (!bpf_is_spilled_reg(\u0026func-\u003estack[j]))\n+\t\tfor (j = 0; j \u003c bpf_stack_nr_slots(func); j++) {\n+\t\t\tstruct bpf_stack_state *ss = bpf_stack_slot(func, j);\n+\n+\t\t\tif (!bpf_is_spilled_reg(ss))\n \t\t\t\tcontinue;\n-\t\t\treg = \u0026func-\u003estack[j].spilled_ptr;\n+\t\t\treg = \u0026ss-\u003espilled_ptr;\n \t\t\tif (reg-\u003etype != SCALAR_VALUE)\n \t\t\t\tcontinue;\n \t\t\treg-\u003eprecise = false;\n@@ -1328,7 +1351,7 @@ int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx)\n \t\t\t */\n \t\t\tif (is_iter_next_insn(env, insn_idx)) {\n \t\t\t\tif (states_equal(env, \u0026sl-\u003estate, cur, RANGE_WITHIN)) {\n-\t\t\t\t\tstruct bpf_func_state *cur_frame;\n+\t\t\t\t\tstruct bpf_func_state *cur_frame, *iter_frame;\n \t\t\t\t\tstruct bpf_reg_state *iter_state, *iter_reg;\n \t\t\t\t\tint spi;\n \n@@ -1342,7 +1365,8 @@ int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx)\n \t\t\t\t\t * no need for extra (re-)validations\n \t\t\t\t\t */\n \t\t\t\t\tspi = bpf_get_spi(iter_reg-\u003evar_off.value);\n-\t\t\t\t\titer_state = \u0026bpf_func(env, iter_reg)-\u003estack[spi].spilled_ptr;\n+\t\t\t\t\titer_frame = bpf_func(env, iter_reg);\n+\t\t\t\t\titer_state = \u0026bpf_stack_slot(iter_frame, spi)-\u003espilled_ptr;\n \t\t\t\t\tif (iter_state-\u003eiter.state == BPF_ITER_STATE_ACTIVE) {\n \t\t\t\t\t\tloop = true;\n \t\t\t\t\t\tgoto hit;\n@@ -1410,7 +1434,7 @@ int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx)\n \t\t\t */\n \t\t\terr = 0;\n \t\t\tif (bpf_is_jmp_point(env, env-\u003einsn_idx))\n-\t\t\t\terr = bpf_push_jmp_history(env, cur, 0, 0, 0, 0);\n+\t\t\t\terr = bpf_push_jmp_history(env, cur, 0, 0, 0, NULL, 0);\n \t\t\terr = err ? : propagate_precision(env, \u0026sl-\u003estate, cur, NULL);\n \t\t\tif (err)\n \t\t\t\treturn err;\ndiff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c\nindex fec5a1ae6a4da..2c495f66dc341 100644\n--- a/kernel/bpf/verifier.c\n+++ b/kernel/bpf/verifier.c\n@@ -598,16 +598,17 @@ bool bpf_is_may_goto_insn(struct bpf_insn *insn)\n \n static bool is_spi_bounds_valid(struct bpf_func_state *state, int spi, int nr_slots)\n {\n- int allocated_slots = state-\u003eallocated_stack / BPF_REG_SIZE;\n+\tint allocated_slots = bpf_stack_nr_slots(state);\n \n- /* We need to check that slots between [spi - nr_slots + 1, spi] are\n-\t* within [0, allocated_stack).\n-\t*\n-\t* Please note that the spi grows downwards. For example, a dynptr\n-\t* takes the size of two stack slots; the first slot will be at\n-\t* spi and the second slot will be at spi - 1.\n-\t*/\n- return spi - nr_slots + 1 \u003e= 0 \u0026\u0026 spi \u003c allocated_slots;\n+\t/*\n+\t * We need to check that slots between [spi - nr_slots + 1, spi] are\n+\t * within [0, allocated_stack).\n+\t *\n+\t * Please note that the spi grows downwards. For example, a dynptr\n+\t * takes the size of two stack slots; the first slot will be at\n+\t * spi and the second slot will be at spi - 1.\n+\t */\n+\treturn spi - nr_slots + 1 \u003e= 0 \u0026\u0026 spi \u003c allocated_slots;\n }\n \n static int stack_slot_obj_get_spi(struct bpf_verifier_env *env, struct bpf_reg_state *reg,\n@@ -751,8 +752,8 @@ static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_\n \t\treturn err;\n \n \tfor (i = 0; i \u003c BPF_REG_SIZE; i++) {\n-\t\tstate-\u003estack[spi].slot_type[i] = STACK_DYNPTR;\n-\t\tstate-\u003estack[spi - 1].slot_type[i] = STACK_DYNPTR;\n+\t\tbpf_stack_slot(state, spi)-\u003eslot_type[i] = STACK_DYNPTR;\n+\t\tbpf_stack_slot(state, spi - 1)-\u003eslot_type[i] = STACK_DYNPTR;\n \t}\n \n \ttype = arg_to_dynptr_type(arg_type);\n@@ -785,8 +786,8 @@ static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_\n \t\tparent_id = dynptr-\u003eparent_id;\n \t}\n \n-\tmark_dynptr_stack_regs(env, \u0026state-\u003estack[spi].spilled_ptr,\n-\t\t\t \u0026state-\u003estack[spi - 1].spilled_ptr, type, parent_id);\n+\tmark_dynptr_stack_regs(env, \u0026bpf_stack_slot(state, spi)-\u003espilled_ptr,\n+\t\t\t \u0026bpf_stack_slot(state, spi - 1)-\u003espilled_ptr, type, parent_id);\n \n \treturn 0;\n }\n@@ -818,7 +819,7 @@ static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_re\n \t * all clones and derived slices. For non-referenced dynptr, only\n \t * the dynptr and slices derived from it will be invalidated.\n \t */\n-\treg = \u0026state-\u003estack[spi].spilled_ptr;\n+\treg = \u0026bpf_stack_slot(state, spi)-\u003espilled_ptr;\n \treturn release_reference(env, dynptr_type_referenced(reg-\u003edynptr.type)\n \t\t\t\t ? reg-\u003eparent_id\n \t\t\t\t : reg-\u003eid);\n@@ -857,6 +858,7 @@ static int dynptr_ref_cnt(struct bpf_verifier_env *env, int v_parent_id)\n static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env,\n \t\t\t\t struct bpf_func_state *state, int spi)\n {\n+\tstruct bpf_stack_state *slot = bpf_stack_slot(state, spi);\n \tint err = 0;\n \n \t/* We always ensure that STACK_DYNPTR is never set partially,\n@@ -864,20 +866,22 @@ static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env,\n \t * different for STACK_SPILL, where it may be only set for\n \t * 1 byte, so code has to use is_spilled_reg.\n \t */\n-\tif (state-\u003estack[spi].slot_type[0] != STACK_DYNPTR)\n+\tif (slot-\u003eslot_type[0] != STACK_DYNPTR)\n \t\treturn 0;\n \n \t/* Reposition spi to first slot */\n-\tif (!state-\u003estack[spi].spilled_ptr.dynptr.first_slot)\n+\tif (!slot-\u003espilled_ptr.dynptr.first_slot) {\n \t\tspi = spi + 1;\n+\t\tslot = bpf_stack_slot(state, spi);\n+\t}\n \n \t/*\n \t * A referenced dynptr can be overwritten only if there is at\n \t * least one other dynptr sharing the same virtual ref parent,\n \t * ensuring the reference can still be properly released.\n \t */\n-\tif (dynptr_type_referenced(state-\u003estack[spi].spilled_ptr.dynptr.type) \u0026\u0026\n-\t dynptr_ref_cnt(env, state-\u003estack[spi].spilled_ptr.parent_id) \u003c= 1) {\n+\tif (dynptr_type_referenced(slot-\u003espilled_ptr.dynptr.type) \u0026\u0026\n+\t dynptr_ref_cnt(env, slot-\u003espilled_ptr.parent_id) \u003c= 1) {\n \t\tverbose(env, \"cannot overwrite referenced dynptr\\n\");\n \t\tbpf_diag_res(\n \t\t\tenv, env-\u003einsn_idx, \"referenced dynptr overwrite\",\n@@ -887,7 +891,7 @@ static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env,\n \t}\n \n \t/* Invalidate the dynptr and any derived slices */\n-\terr = release_reference(env, state-\u003estack[spi].spilled_ptr.id);\n+\terr = release_reference(env, slot-\u003espilled_ptr.id);\n \tif (!err) {\n \t\tmark_stack_slot_scratched(env, spi);\n \t\tmark_stack_slot_scratched(env, spi - 1);\n@@ -927,6 +931,7 @@ static bool is_dynptr_reg_valid_uninit(struct bpf_verifier_env *env, struct bpf_\n static bool is_dynptr_reg_valid_init(struct bpf_verifier_env *env, struct bpf_reg_state *reg)\n {\n \tstruct bpf_func_state *state = bpf_func(env, reg);\n+\tstruct bpf_stack_state *slot;\n \tint i, spi;\n \n \t/* This already represents first slot of initialized bpf_dynptr.\n@@ -941,12 +946,13 @@ static bool is_dynptr_reg_valid_init(struct bpf_verifier_env *env, struct bpf_re\n \tspi = dynptr_get_spi(env, reg);\n \tif (spi \u003c 0)\n \t\treturn false;\n-\tif (!state-\u003estack[spi].spilled_ptr.dynptr.first_slot)\n+\tslot = bpf_stack_slot(state, spi);\n+\tif (!slot-\u003espilled_ptr.dynptr.first_slot)\n \t\treturn false;\n \n \tfor (i = 0; i \u003c BPF_REG_SIZE; i++) {\n-\t\tif (state-\u003estack[spi].slot_type[i] != STACK_DYNPTR ||\n-\t\t state-\u003estack[spi - 1].slot_type[i] != STACK_DYNPTR)\n+\t\tif (slot-\u003eslot_type[i] != STACK_DYNPTR ||\n+\t\t bpf_stack_slot(state, spi - 1)-\u003eslot_type[i] != STACK_DYNPTR)\n \t\t\treturn false;\n \t}\n \n@@ -965,7 +971,7 @@ static enum bpf_dynptr_type dynptr_reg_type(struct bpf_verifier_env *env, struct\n \tif (spi \u003c 0)\n \t\treturn BPF_DYNPTR_TYPE_INVALID;\n \tstate = bpf_func(env, reg);\n-\treturn state-\u003estack[spi].spilled_ptr.dynptr.type;\n+\treturn bpf_stack_slot(state, spi)-\u003espilled_ptr.dynptr.type;\n }\n \n static bool is_dynptr_type_expected(struct bpf_verifier_env *env, struct bpf_reg_state *reg,\n@@ -1005,7 +1011,7 @@ static int mark_stack_slots_iter(struct bpf_verifier_env *env,\n \t\treturn id;\n \n \tfor (i = 0; i \u003c nr_slots; i++) {\n-\t\tstruct bpf_stack_state *slot = \u0026state-\u003estack[spi - i];\n+\t\tstruct bpf_stack_state *slot = bpf_stack_slot(state, spi - i);\n \t\tstruct bpf_reg_state *st = \u0026slot-\u003espilled_ptr;\n \n \t\t__mark_reg_known_zero(st);\n@@ -1035,18 +1041,22 @@ static int unmark_stack_slots_iter(struct bpf_verifier_env *env,\n \t\t\t\t struct bpf_reg_state *reg, int nr_slots)\n {\n \tstruct bpf_func_state *state = bpf_func(env, reg);\n-\tint spi, i, j;\n+\tint spi, i, j, err;\n \n \tspi = iter_get_spi(env, reg, nr_slots);\n \tif (spi \u003c 0)\n \t\treturn spi;\n \n \tfor (i = 0; i \u003c nr_slots; i++) {\n-\t\tstruct bpf_stack_state *slot = \u0026state-\u003estack[spi - i];\n+\t\tstruct bpf_stack_state *slot = bpf_stack_slot(state, spi - i);\n \t\tstruct bpf_reg_state *st = \u0026slot-\u003espilled_ptr;\n \n-\t\tif (i == 0)\n-\t\t\tWARN_ON_ONCE(release_reference(env, st-\u003eid));\n+\t\tif (i == 0) {\n+\t\t\terr = release_reference(env, st-\u003eid);\n+\t\t\tif (err == -ENOMEM)\n+\t\t\t\treturn err;\n+\t\t\tWARN_ON_ONCE(err);\n+\t\t}\n \n \t\tbpf_mark_reg_not_init(env, st);\n \n@@ -1076,7 +1086,7 @@ static bool is_iter_reg_valid_uninit(struct bpf_verifier_env *env,\n \t\treturn false;\n \n \tfor (i = 0; i \u003c nr_slots; i++) {\n-\t\tstruct bpf_stack_state *slot = \u0026state-\u003estack[spi - i];\n+\t\tstruct bpf_stack_state *slot = bpf_stack_slot(state, spi - i);\n \n \t\tfor (j = 0; j \u003c BPF_REG_SIZE; j++)\n \t\t\tif (slot-\u003eslot_type[j] == STACK_ITER)\n@@ -1097,7 +1107,7 @@ static int is_iter_reg_valid_init(struct bpf_verifier_env *env, struct bpf_reg_s\n \t\treturn -EINVAL;\n \n \tfor (i = 0; i \u003c nr_slots; i++) {\n-\t\tstruct bpf_stack_state *slot = \u0026state-\u003estack[spi - i];\n+\t\tstruct bpf_stack_state *slot = bpf_stack_slot(state, spi - i);\n \t\tstruct bpf_reg_state *st = \u0026slot-\u003espilled_ptr;\n \n \t\tif (st-\u003etype \u0026 PTR_UNTRUSTED)\n@@ -1139,7 +1149,7 @@ static int mark_stack_slot_irq_flag(struct bpf_verifier_env *env,\n \tif (id \u003c 0)\n \t\treturn id;\n \n-\tslot = \u0026state-\u003estack[spi];\n+\tslot = bpf_stack_slot(state, spi);\n \tst = \u0026slot-\u003espilled_ptr;\n \n \t__mark_reg_known_zero(st);\n@@ -1166,7 +1176,7 @@ static int unmark_stack_slot_irq_flag(struct bpf_verifier_env *env, struct bpf_r\n \tif (spi \u003c 0)\n \t\treturn spi;\n \n-\tslot = \u0026state-\u003estack[spi];\n+\tslot = bpf_stack_slot(state, spi);\n \tst = \u0026slot-\u003espilled_ptr;\n \n \tif (st-\u003eirq.kfunc_class != kfunc_class) {\n@@ -1235,7 +1245,7 @@ static bool is_irq_flag_reg_valid_uninit(struct bpf_verifier_env *env, struct bp\n \tif (spi \u003c 0)\n \t\treturn false;\n \n-\tslot = \u0026state-\u003estack[spi];\n+\tslot = bpf_stack_slot(state, spi);\n \n \tfor (i = 0; i \u003c BPF_REG_SIZE; i++)\n \t\tif (slot-\u003eslot_type[i] == STACK_IRQ_FLAG)\n@@ -1254,7 +1264,7 @@ static int is_irq_flag_reg_valid_init(struct bpf_verifier_env *env, struct bpf_r\n \tif (spi \u003c 0)\n \t\treturn -EINVAL;\n \n-\tslot = \u0026state-\u003estack[spi];\n+\tslot = bpf_stack_slot(state, spi);\n \tst = \u0026slot-\u003espilled_ptr;\n \n \tif (!st-\u003eid)\n@@ -1401,7 +1411,7 @@ static int copy_reference_state(struct bpf_verifier_state *dst, const struct bpf\n \n static int copy_stack_state(struct bpf_func_state *dst, const struct bpf_func_state *src)\n {\n-\tsize_t n = src-\u003eallocated_stack / BPF_REG_SIZE;\n+\tsize_t n = bpf_stack_nr_slots(src);\n \n \tdst-\u003estack = copy_array(dst-\u003estack, src-\u003estack, n, sizeof(struct bpf_stack_state),\n \t\t\t\tGFP_KERNEL_ACCOUNT);\n@@ -1440,7 +1450,7 @@ static int resize_reference_state(struct bpf_verifier_state *state, size_t n)\n */\n static int grow_stack_state(struct bpf_verifier_env *env, struct bpf_func_state *state, int size)\n {\n-\tsize_t old_n = state-\u003eallocated_stack / BPF_REG_SIZE, n;\n+\tsize_t old_n = bpf_stack_nr_slots(state), n;\n \n \t/* The stack size is always a multiple of BPF_REG_SIZE. */\n \tsize = round_up(size, BPF_REG_SIZE);\n@@ -3368,26 +3378,25 @@ static void mark_non_stack_access(struct bpf_verifier_env *env, int idx)\n \tenv-\u003einsn_aux_data[idx].non_stack_access = true;\n }\n \n+/* Layout of one packed linked register in the jump history, see linked_regs_pack() */\n #define LR_FRAMENO_BITS\t4\n-#define LR_SPI_BITS\t6\n-#define LR_ENTRY_BITS\t(LR_SPI_BITS + LR_FRAMENO_BITS + 1)\n-#define LR_SIZE_BITS\t4\n-#define LR_FRAMENO_MASK\t((1ull \u003c\u003c LR_FRAMENO_BITS) - 1)\n-#define LR_SPI_MASK\t((1ull \u003c\u003c LR_SPI_BITS) - 1)\n-#define LR_SIZE_MASK\t((1ull \u003c\u003c LR_SIZE_BITS) - 1)\n-#define LR_SPI_OFF\tLR_FRAMENO_BITS\n-#define LR_IS_REG_OFF\t(LR_SPI_BITS + LR_FRAMENO_BITS)\n-#define LINKED_REGS_MAX\t5\n+#define LR_INDEX_BITS\t11\n+#define LR_FRAMENO_MASK\t((1u \u003c\u003c LR_FRAMENO_BITS) - 1)\n+#define LR_IS_REG\tBIT(LR_FRAMENO_BITS)\n+#define LR_INDEX_OFF\t(LR_FRAMENO_BITS + 1)\n+#define LR_INDEX_MASK\t((1u \u003c\u003c LR_INDEX_BITS) - 1)\n+#define LINKED_REGS_MAX\tBPF_LINKED_REGS_MAX\n \n static_assert(MAX_CALL_FRAMES \u003c= (1 \u003c\u003c LR_FRAMENO_BITS));\n-static_assert(LINKED_REGS_MAX \u003c (1 \u003c\u003c LR_SIZE_BITS));\n-static_assert(LINKED_REGS_MAX * LR_ENTRY_BITS + LR_SIZE_BITS \u003c= 64);\n+static_assert(MAX_BPF_REG \u003c= (1 \u003c\u003c LR_INDEX_BITS));\n+static_assert(MAX_BPF_STACK_SLOTS \u003c= (1 \u003c\u003c LR_INDEX_BITS));\n+static_assert(LR_INDEX_OFF + LR_INDEX_BITS \u003c= 16);\n \n struct linked_reg {\n \tu8 frameno;\n \tunion {\n-\t\tu8 spi;\n-\t\tu8 regno;\n+\t\tu16 spi;\n+\t\tu16 regno;\n \t};\n \tbool is_reg;\n };\n@@ -3406,48 +3415,34 @@ static struct linked_reg *linked_regs_push(struct linked_regs *s)\n }\n \n /*\n- * Use u64 as a vector of 5 11-bit values, use first 4-bits to track\n- * number of elements currently in stack.\n- * Pack one history entry for linked registers as 11 bits in the following format:\n- * - 4-bits frameno\n- * - 6-bits spi_or_reg\n- * - 1-bit is_reg\n+ * Pack linked registers for a jump history entry, one u16 each:\n+ * - 4 bits frameno\n+ * - 1 bit is_reg\n+ * - 11 bits register or stack slot index\n */\n-static u64 linked_regs_pack(struct linked_regs *s)\n+static void linked_regs_pack(const struct linked_regs *s, u16 *packed)\n {\n-\tu64 val = 0;\n \tint i;\n \n \tfor (i = 0; i \u003c s-\u003ecnt; ++i) {\n-\t\tstruct linked_reg *e = \u0026s-\u003eentries[i];\n-\t\tu64 tmp = 0;\n+\t\tconst struct linked_reg *e = \u0026s-\u003eentries[i];\n \n-\t\ttmp |= e-\u003eframeno;\n-\t\ttmp |= e-\u003espi \u003c\u003c LR_SPI_OFF;\n-\t\ttmp |= (e-\u003eis_reg ? 1 : 0) \u003c\u003c LR_IS_REG_OFF;\n-\n-\t\tval \u003c\u003c= LR_ENTRY_BITS;\n-\t\tval |= tmp;\n+\t\tpacked[i] = e-\u003eframeno | (e-\u003eis_reg ? LR_IS_REG : 0) | (e-\u003espi \u003c\u003c LR_INDEX_OFF);\n \t}\n-\tval \u003c\u003c= LR_SIZE_BITS;\n-\tval |= s-\u003ecnt;\n-\treturn val;\n }\n \n-static void linked_regs_unpack(u64 val, struct linked_regs *s)\n+static void linked_regs_unpack(const struct bpf_jmp_history_entry *hist, struct linked_regs *s)\n {\n \tint i;\n \n-\ts-\u003ecnt = val \u0026 LR_SIZE_MASK;\n-\tval \u003e\u003e= LR_SIZE_BITS;\n-\n+\ts-\u003ecnt = hist-\u003elinked_regs_cnt;\n \tfor (i = 0; i \u003c s-\u003ecnt; ++i) {\n \t\tstruct linked_reg *e = \u0026s-\u003eentries[i];\n+\t\tu16 packed = hist-\u003elinked_regs[i];\n \n-\t\te-\u003eframeno = val \u0026 LR_FRAMENO_MASK;\n-\t\te-\u003espi = (val \u003e\u003e LR_SPI_OFF) \u0026 LR_SPI_MASK;\n-\t\te-\u003eis_reg = (val \u003e\u003e LR_IS_REG_OFF) \u0026 0x1;\n-\t\tval \u003e\u003e= LR_ENTRY_BITS;\n+\t\te-\u003eframeno = packed \u0026 LR_FRAMENO_MASK;\n+\t\te-\u003eis_reg = packed \u0026 LR_IS_REG;\n+\t\te-\u003espi = (packed \u003e\u003e LR_INDEX_OFF) \u0026 LR_INDEX_MASK;\n \t}\n }\n \n@@ -3489,10 +3484,10 @@ void bpf_bt_sync_linked_regs(struct backtrack_state *bt, struct bpf_jmp_history_\n \tbool some_precise = false;\n \tint i;\n \n-\tif (!hist || hist-\u003elinked_regs == 0)\n+\tif (!hist || !hist-\u003elinked_regs_cnt)\n \t\treturn;\n \n-\tlinked_regs_unpack(hist-\u003elinked_regs, \u0026linked_regs);\n+\tlinked_regs_unpack(hist, \u0026linked_regs);\n \tfor (i = 0; i \u003c linked_regs.cnt; ++i) {\n \t\tstruct linked_reg *e = \u0026linked_regs.entries[i];\n \n@@ -3589,17 +3584,18 @@ static void save_register_state(struct bpf_verifier_env *env,\n \t\t\t\tint spi, struct bpf_reg_state *reg,\n \t\t\t\tint size)\n {\n+\tstruct bpf_stack_state *slot = bpf_stack_slot(state, spi);\n \tint i;\n \n-\tbpf_diag_mod_begin(env, \u0026state-\u003estack[spi].spilled_ptr, reg, BPF_DIAG_MOD_SPILL);\n-\tstate-\u003estack[spi].spilled_ptr = *reg;\n+\tbpf_diag_mod_begin(env, \u0026slot-\u003espilled_ptr, reg, BPF_DIAG_MOD_SPILL);\n+\tslot-\u003espilled_ptr = *reg;\n \n \tfor (i = BPF_REG_SIZE; i \u003e BPF_REG_SIZE - size; i--)\n-\t\tstate-\u003estack[spi].slot_type[i - 1] = STACK_SPILL;\n+\t\tslot-\u003eslot_type[i - 1] = STACK_SPILL;\n \n \t/* size \u003c 8 bytes spill */\n \tfor (; i; i--)\n-\t\tmark_stack_slot_misc(env, \u0026state-\u003estack[spi].slot_type[i - 1]);\n+\t\tmark_stack_slot_misc(env, \u0026slot-\u003eslot_type[i - 1]);\n \n \tbpf_diag_mod_end(env);\n }\n@@ -3641,14 +3637,15 @@ static void check_fastcall_stack_contract(struct bpf_verifier_env *env,\n \n static void scrub_special_slot(struct bpf_func_state *state, int spi)\n {\n+\tstruct bpf_stack_state *slot = bpf_stack_slot(state, spi);\n \tint i;\n \n \t/* regular write of data into stack destroys any spilled ptr */\n-\tstate-\u003estack[spi].spilled_ptr.type = NOT_INIT;\n+\tslot-\u003espilled_ptr.type = NOT_INIT;\n \t/* Mark slots as STACK_MISC if they belonged to spilled ptr/dynptr/iter. */\n-\tif (is_stack_slot_special(\u0026state-\u003estack[spi]))\n+\tif (is_stack_slot_special(slot))\n \t\tfor (i = 0; i \u003c BPF_REG_SIZE; i++)\n-\t\t\tscrub_spilled_slot(\u0026state-\u003estack[spi].slot_type[i]);\n+\t\t\tscrub_spilled_slot(\u0026slot-\u003eslot_type[i]);\n }\n \n /* check_stack_{read,write}_fixed_off functions track spill/fill of registers,\n@@ -3666,13 +3663,15 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env,\n \tstruct bpf_reg_state *reg = NULL;\n \tint insn_flags = INSN_F_STACK_ACCESS;\n \tint hist_spi = spi, hist_frame = state-\u003eframeno;\n+\tstruct bpf_stack_state *ss = bpf_stack_slot(state, spi);\n \n-\t/* caller checked that off % size == 0 and -MAX_BPF_STACK \u003c= off \u003c 0,\n+\t/*\n+\t * caller checked that off % size == 0 and -env-\u003estack_limit \u003c= off \u003c 0,\n \t * so it's aligned access and [off, off + size) are within stack limits\n \t */\n \tif (!env-\u003eallow_ptr_leaks \u0026\u0026\n-\t bpf_is_spilled_reg(\u0026state-\u003estack[spi]) \u0026\u0026\n-\t !bpf_is_spilled_scalar_reg(\u0026state-\u003estack[spi]) \u0026\u0026\n+\t bpf_is_spilled_reg(ss) \u0026\u0026\n+\t !bpf_is_spilled_scalar_reg(ss) \u0026\u0026\n \t size != BPF_REG_SIZE) {\n \t\tconst char *reason;\n \n@@ -3694,7 +3693,7 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env,\n \t\tbool sanitize = reg \u0026\u0026 is_pointer_regtype(reg-\u003etype);\n \n \t\tfor (i = 0; i \u003c size; i++) {\n-\t\t\tu8 type = state-\u003estack[spi].slot_type[(slot - i) %\n+\t\t\tu8 type = ss-\u003eslot_type[(slot - i) %\n \t\t\t\t\t\t\t BPF_REG_SIZE];\n \n \t\t\tif (type != STACK_MISC \u0026\u0026 type != STACK_ZERO) {\n@@ -3723,7 +3722,7 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env,\n \t\tsave_register_state(env, state, spi, reg, size);\n \t\t/* Break the relation on a narrowing spill. */\n \t\tif (!reg_value_fits)\n-\t\t\tstate-\u003estack[spi].spilled_ptr.id = 0;\n+\t\t\tss-\u003espilled_ptr.id = 0;\n \t} else if (!reg \u0026\u0026 !(off % BPF_REG_SIZE) \u0026\u0026 is_bpf_st_mem(insn) \u0026\u0026\n \t\t env-\u003ebpf_capable) {\n \t\tstruct bpf_reg_state *tmp_reg = \u0026env-\u003efake_reg[0];\n@@ -3747,8 +3746,8 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env,\n \t} else {\n \t\tu8 type = STACK_MISC;\n \n-\t\tif (bpf_is_spilled_reg(\u0026state-\u003estack[spi]))\n-\t\t\tbpf_diag_record_scrub(env, \u0026state-\u003estack[spi].spilled_ptr,\n+\t\tif (bpf_is_spilled_reg(ss))\n+\t\t\tbpf_diag_record_scrub(env, \u0026ss-\u003espilled_ptr,\n \t\t\t\t\t BPF_DIAG_MOD_WRITE);\n \t\tscrub_special_slot(state, spi);\n \n@@ -3769,13 +3768,13 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env,\n \n \t\t/* Mark slots affected by this stack write. */\n \t\tfor (i = 0; i \u003c size; i++)\n-\t\t\tstate-\u003estack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = type;\n+\t\t\tss-\u003eslot_type[(slot - i) % BPF_REG_SIZE] = type;\n \t\tinsn_flags = 0; /* not a register spill */\n \t}\n \n \tif (insn_flags)\n \t\treturn bpf_push_jmp_history(env, env-\u003ecur_state, insn_flags,\n-\t\t\t\t\t hist_spi, hist_frame, 0);\n+\t\t\t\t\t hist_spi, hist_frame, NULL, 0);\n \treturn 0;\n }\n \n@@ -3835,12 +3834,14 @@ static int check_stack_write_var_off(struct bpf_verifier_env *env,\n \tcheck_fastcall_stack_contract(env, state, insn_idx, min_off);\n \t/* Variable offset writes destroy any spilled pointers in range. */\n \tfor (i = min_off; i \u003c max_off; i++) {\n+\t\tstruct bpf_stack_state *ss;\n \t\tu8 new_type, *stype;\n \t\tint slot, spi;\n \n \t\tslot = -i - 1;\n \t\tspi = slot / BPF_REG_SIZE;\n-\t\tstype = \u0026state-\u003estack[spi].slot_type[slot % BPF_REG_SIZE];\n+\t\tss = bpf_stack_slot(state, spi);\n+\t\tstype = \u0026ss-\u003eslot_type[slot % BPF_REG_SIZE];\n \t\tmark_stack_slot_scratched(env, spi);\n \n \t\tif (!env-\u003eallow_ptr_leaks \u0026\u0026 *stype != STACK_MISC \u0026\u0026 *stype != STACK_ZERO) {\n@@ -3863,9 +3864,8 @@ static int check_stack_write_var_off(struct bpf_verifier_env *env,\n \t\t/* If writing_zero and the spi slot contains a spill of value 0,\n \t\t * maintain the spill type.\n \t\t */\n-\t\tif (writing_zero \u0026\u0026 *stype == STACK_SPILL \u0026\u0026\n-\t\t bpf_is_spilled_scalar_reg(\u0026state-\u003estack[spi])) {\n-\t\t\tstruct bpf_reg_state *spill_reg = \u0026state-\u003estack[spi].spilled_ptr;\n+\t\tif (writing_zero \u0026\u0026 *stype == STACK_SPILL \u0026\u0026 bpf_is_spilled_scalar_reg(ss)) {\n+\t\t\tstruct bpf_reg_state *spill_reg = \u0026ss-\u003espilled_ptr;\n \n \t\t\tif (tnum_is_const(spill_reg-\u003evar_off) \u0026\u0026 spill_reg-\u003evar_off.value == 0) {\n \t\t\t\tzero_used = true;\n@@ -3936,23 +3936,22 @@ static int mark_reg_stack_read(struct bpf_verifier_env *env,\n {\n \tstruct bpf_verifier_state *vstate = env-\u003ecur_state;\n \tstruct bpf_func_state *state = vstate-\u003eframe[vstate-\u003ecurframe];\n-\tu64 zero_spill_mask = 0;\n \tint i, slot, spi;\n \tu8 *stype;\n-\tint zeros = 0;\n+\tint zeros = 0, zero_spills = 0;\n \n \tfor (i = min_off; i \u003c max_off; i++) {\n \t\tslot = -i - 1;\n \t\tspi = slot / BPF_REG_SIZE;\n \t\tmark_stack_slot_scratched(env, spi);\n-\t\tstype = ptr_state-\u003estack[spi].slot_type;\n+\t\tstype = bpf_stack_slot(ptr_state, spi)-\u003eslot_type;\n \t\tif (stype[slot % BPF_REG_SIZE] == STACK_ZERO) {\n \t\t\tzeros++;\n \t\t\tcontinue;\n \t\t}\n \t\tif (stype[slot % BPF_REG_SIZE] == STACK_SPILL \u0026\u0026\n-\t\t bpf_register_is_null(\u0026ptr_state-\u003estack[spi].spilled_ptr)) {\n-\t\t\tzero_spill_mask |= 1ull \u003c\u003c spi;\n+\t\t bpf_register_is_null(\u0026bpf_stack_slot(ptr_state, spi)-\u003espilled_ptr)) {\n+\t\t\tzero_spills++;\n \t\t\tzeros++;\n \t\t\tcontinue;\n \t\t}\n@@ -3963,8 +3962,14 @@ static int mark_reg_stack_read(struct bpf_verifier_env *env,\n \t\t * so the whole register == const_zero.\n \t\t */\n \t\t__mark_reg_const_zero(env, \u0026state-\u003eregs[dst_regno]);\n-\t\tif (zero_spill_mask) {\n-\t\t\tbpf_bt_set_frame_slot_mask(\u0026env-\u003ebt, ptr_state-\u003eframeno, zero_spill_mask);\n+\t\tif (zero_spills) {\n+\t\t\tfor (i = min_off; i \u003c max_off; i++) {\n+\t\t\t\tslot = -i - 1;\n+\t\t\t\tspi = slot / BPF_REG_SIZE;\n+\t\t\t\tstype = bpf_stack_slot(ptr_state, spi)-\u003eslot_type;\n+\t\t\t\tif (stype[slot % BPF_REG_SIZE] == STACK_SPILL)\n+\t\t\t\t\tbpf_bt_set_frame_slot(\u0026env-\u003ebt, ptr_state-\u003eframeno, spi);\n+\t\t\t}\n \t\t\treturn mark_chain_precision_batch(env, env-\u003ecur_state);\n \t\t}\n \t} else {\n@@ -4012,9 +4017,10 @@ static int check_stack_read_fixed_off(struct bpf_verifier_env *env,\n \tint err;\n \tint insn_flags = INSN_F_STACK_ACCESS;\n \tint hist_spi = spi, hist_frame = reg_state-\u003eframeno;\n+\tstruct bpf_stack_state *ss = bpf_stack_slot(reg_state, spi);\n \n-\tstype = reg_state-\u003estack[spi].slot_type;\n-\treg = \u0026reg_state-\u003estack[spi].spilled_ptr;\n+\tstype = ss-\u003eslot_type;\n+\treg = \u0026ss-\u003espilled_ptr;\n \n \tmark_stack_slot_scratched(env, spi);\n \tcheck_fastcall_stack_contract(env, state, env-\u003einsn_idx, off);\n@@ -4025,7 +4031,7 @@ static int check_stack_read_fixed_off(struct bpf_verifier_env *env,\n \tif (dst_regno \u003e= 0)\n \t\tbpf_diag_mod_begin(env, \u0026state-\u003eregs[dst_regno], reg, BPF_DIAG_MOD_WRITE);\n \n-\tif (bpf_is_spilled_reg(\u0026reg_state-\u003estack[spi])) {\n+\tif (bpf_is_spilled_reg(ss)) {\n \t\tu8 spill_size = 1;\n \n \t\tfor (i = BPF_REG_SIZE - 1; i \u003e 0 \u0026\u0026 stype[i - 1] == STACK_SPILL; i--)\n@@ -4151,7 +4157,7 @@ static int check_stack_read_fixed_off(struct bpf_verifier_env *env,\n \t}\n \tif (insn_flags)\n \t\treturn bpf_push_jmp_history(env, env-\u003ecur_state, insn_flags,\n-\t\t\t\t\t hist_spi, hist_frame, 0);\n+\t\t\t\t\t hist_spi, hist_frame, NULL, 0);\n \treturn 0;\n }\n \n@@ -4341,7 +4347,7 @@ static int check_stack_arg_write(struct bpf_verifier_env *env, struct bpf_func_s\n \tbpf_diag_mod_end(env);\n \tstate-\u003eno_stack_arg_load = true;\n \treturn bpf_push_jmp_history(env, env-\u003ecur_state,\n-\t\t\t\t INSN_F_STACK_ARG_ACCESS, spi, 0, 0);\n+\t\t\t\t INSN_F_STACK_ARG_ACCESS, spi, 0, NULL, 0);\n }\n \n /*\n@@ -4375,7 +4381,7 @@ static int check_stack_arg_read(struct bpf_verifier_env *env, struct bpf_func_st\n \tcur-\u003eregs[dst_regno] = *arg;\n \tbpf_diag_mod_end(env);\n \treturn bpf_push_jmp_history(env, env-\u003ecur_state,\n-\t\t\t\t INSN_F_STACK_ARG_ACCESS, spi, 0, 0);\n+\t\t\t\t INSN_F_STACK_ARG_ACCESS, spi, 0, NULL, 0);\n }\n \n static int mark_stack_arg_precision(struct bpf_verifier_env *env, int arg_idx)\n@@ -5519,7 +5525,7 @@ static int check_max_stack_depth_subprog(struct bpf_verifier_env *env, int idx,\n \tif (subprog[idx].priv_stack_mode == PRIV_STACK_ADAPTIVE) {\n \t\tif (subprog_depth \u003e env-\u003emax_stack_depth)\n \t\t\tenv-\u003emax_stack_depth = subprog_depth;\n-\t\tif (subprog_depth \u003e MAX_BPF_STACK) {\n+\t\tif (subprog_depth \u003e env-\u003estack_limit) {\n \t\t\tverbose(env, \"stack size of subprog %d is %d. Too large\\n\",\n \t\t\t\tidx, subprog_depth);\n \t\t\treturn -EACCES;\n@@ -5528,7 +5534,7 @@ static int check_max_stack_depth_subprog(struct bpf_verifier_env *env, int idx,\n \t\tdepth += subprog_depth;\n \t\tif (depth \u003e env-\u003emax_stack_depth)\n \t\t\tenv-\u003emax_stack_depth = depth;\n-\t\tif (depth \u003e MAX_BPF_STACK) {\n+\t\tif (depth \u003e env-\u003estack_limit) {\n \t\t\ttotal = 0;\n \t\t\tfor (tmp = idx; tmp \u003e= 0; tmp = dinfo[tmp].caller)\n \t\t\t\ttotal++;\n@@ -6529,10 +6535,11 @@ static int check_ptr_to_map_access(struct bpf_verifier_env *env,\n \treturn 0;\n }\n \n-/* Check that the stack access at the given offset is within bounds. The\n+/*\n+ * Check that the stack access at the given offset is within bounds. The\n * maximum valid offset is -1.\n *\n- * The minimum valid offset is -MAX_BPF_STACK for writes, and\n+ * The minimum valid offset is -env-\u003estack_limit for writes, and\n * -state-\u003eallocated_stack for reads.\n */\n static int check_stack_slot_within_bounds(struct bpf_verifier_env *env,\n@@ -6543,7 +6550,7 @@ static int check_stack_slot_within_bounds(struct bpf_verifier_env *env,\n \tint min_valid_off;\n \n \tif (t == BPF_WRITE || env-\u003eallow_uninit_stack)\n-\t\tmin_valid_off = -MAX_BPF_STACK;\n+\t\tmin_valid_off = -(int)env-\u003estack_limit;\n \telse\n \t\tmin_valid_off = -state-\u003eallocated_stack;\n \n@@ -7409,6 +7416,7 @@ static int check_stack_range_initialized(\n \t}\n \n \tfor (i = min_off; i \u003c max_off + access_size; i++) {\n+\t\tstruct bpf_stack_state *ss;\n \t\tu8 *stype;\n \n \t\tslot = -i - 1;\n@@ -7418,7 +7426,8 @@ static int check_stack_range_initialized(\n \t\t\treturn -EFAULT;\n \t\t}\n \n-\t\tstype = \u0026state-\u003estack[spi].slot_type[slot % BPF_REG_SIZE];\n+\t\tss = bpf_stack_slot(state, spi);\n+\t\tstype = \u0026ss-\u003eslot_type[slot % BPF_REG_SIZE];\n \t\tif (*stype == STACK_MISC)\n \t\t\tgoto mark;\n \t\tif ((*stype == STACK_ZERO) ||\n@@ -7430,13 +7439,13 @@ static int check_stack_range_initialized(\n \t\t\tgoto mark;\n \t\t}\n \n-\t\tif (bpf_is_spilled_reg(\u0026state-\u003estack[spi]) \u0026\u0026\n-\t\t (state-\u003estack[spi].spilled_ptr.type == SCALAR_VALUE ||\n+\t\tif (bpf_is_spilled_reg(ss) \u0026\u0026\n+\t\t (ss-\u003espilled_ptr.type == SCALAR_VALUE ||\n \t\t env-\u003eallow_ptr_leaks)) {\n \t\t\tif (clobber) {\n-\t\t\t\t__mark_reg_unknown(env, \u0026state-\u003estack[spi].spilled_ptr);\n+\t\t\t\t__mark_reg_unknown(env, \u0026ss-\u003espilled_ptr);\n \t\t\t\tfor (j = 0; j \u003c BPF_REG_SIZE; j++)\n-\t\t\t\t\tscrub_spilled_slot(\u0026state-\u003estack[spi].slot_type[j]);\n+\t\t\t\t\tscrub_spilled_slot(\u0026ss-\u003eslot_type[j]);\n \t\t\t}\n \t\t\tgoto mark;\n \t\t}\n@@ -8166,7 +8175,7 @@ static int process_dynptr_func(struct bpf_verifier_env *env, struct bpf_reg_stat\n \n \t\t\tmark_stack_slots_scratched(env, spi, BPF_DYNPTR_NR_SLOTS);\n \n-\t\t\treg = \u0026state-\u003estack[spi].spilled_ptr;\n+\t\t\treg = \u0026bpf_stack_slot(state, spi)-\u003espilled_ptr;\n \t\t}\n \n \t\tmeta-\u003edynptr.type = reg-\u003edynptr.type;\n@@ -8299,7 +8308,7 @@ static int process_iter_arg(struct bpf_verifier_env *env, struct bpf_reg_state *\n \t\t/* remember meta-\u003eiter info for process_iter_next_call() */\n \t\tmeta-\u003eiter.spi = spi;\n \t\tmeta-\u003eiter.frameno = reg-\u003eframeno;\n-\t\tupdate_ref_obj(\u0026meta-\u003eref_obj, \u0026state-\u003estack[spi].spilled_ptr);\n+\t\tupdate_ref_obj(\u0026meta-\u003eref_obj, \u0026bpf_stack_slot(state, spi)-\u003espilled_ptr);\n \n \t\tif (is_iter_destroy_kfunc(meta)) {\n \t\t\terr = unmark_stack_slots_iter(env, reg, nr_slots);\n@@ -8376,16 +8385,15 @@ static int widen_imprecise_scalars(struct bpf_verifier_env *env,\n \t\t\t\t\t\u0026fold-\u003eregs[i],\n \t\t\t\t\t\u0026fcur-\u003eregs[i]);\n \n-\t\tnum_slots = min(fold-\u003eallocated_stack / BPF_REG_SIZE,\n-\t\t\t\tfcur-\u003eallocated_stack / BPF_REG_SIZE);\n+\t\tnum_slots = min(bpf_stack_nr_slots(fold), bpf_stack_nr_slots(fcur));\n \t\tfor (i = 0; i \u003c num_slots; i++) {\n-\t\t\tif (!bpf_is_spilled_reg(\u0026fold-\u003estack[i]) ||\n-\t\t\t !bpf_is_spilled_reg(\u0026fcur-\u003estack[i]))\n+\t\t\tstruct bpf_stack_state *old_ss = bpf_stack_slot(fold, i);\n+\t\t\tstruct bpf_stack_state *cur_ss = bpf_stack_slot(fcur, i);\n+\n+\t\t\tif (!bpf_is_spilled_reg(old_ss) || !bpf_is_spilled_reg(cur_ss))\n \t\t\t\tcontinue;\n \n-\t\t\tmaybe_widen_reg(env,\n-\t\t\t\t\t\u0026fold-\u003estack[i].spilled_ptr,\n-\t\t\t\t\t\u0026fcur-\u003estack[i].spilled_ptr);\n+\t\t\tmaybe_widen_reg(env, \u0026old_ss-\u003espilled_ptr, \u0026cur_ss-\u003espilled_ptr);\n \t\t}\n \t}\n \treturn 0;\n@@ -8397,7 +8405,7 @@ static struct bpf_reg_state *get_iter_from_state(struct bpf_verifier_state *cur_\n \tint iter_frameno = meta-\u003eiter.frameno;\n \tint iter_spi = meta-\u003eiter.spi;\n \n-\treturn \u0026cur_st-\u003eframe[iter_frameno]-\u003estack[iter_spi].spilled_ptr;\n+\treturn \u0026bpf_stack_slot(cur_st-\u003eframe[iter_frameno], iter_spi)-\u003espilled_ptr;\n }\n \n /* process_iter_next_call() is called when verifier gets to iterator's next\n@@ -9189,6 +9197,7 @@ static int get_constant_map_key(struct bpf_verifier_env *env,\n \t\t\t\ts64 *value)\n {\n \tstruct bpf_func_state *state = bpf_func(env, key);\n+\tstruct bpf_stack_state *ss;\n \tstruct bpf_reg_state *reg;\n \tint slot, spi, off;\n \tint spill_size = 0;\n@@ -9208,7 +9217,8 @@ static int get_constant_map_key(struct bpf_verifier_env *env,\n \tslot = -stack_off - 1;\n \tspi = slot / BPF_REG_SIZE;\n \toff = slot % BPF_REG_SIZE;\n-\tstype = state-\u003estack[spi].slot_type;\n+\tss = bpf_stack_slot(state, spi);\n+\tstype = ss-\u003eslot_type;\n \n \t/* First handle precisely tracked STACK_ZERO */\n \tfor (i = off; i \u003e= 0 \u0026\u0026 stype[i] == STACK_ZERO; i--)\n@@ -9219,14 +9229,14 @@ static int get_constant_map_key(struct bpf_verifier_env *env,\n \t}\n \n \t/* Check that stack contains a scalar spill of expected size */\n-\tif (!bpf_is_spilled_scalar_reg(\u0026state-\u003estack[spi]))\n+\tif (!bpf_is_spilled_scalar_reg(ss))\n \t\treturn -EOPNOTSUPP;\n \tfor (i = off; i \u003e= 0 \u0026\u0026 stype[i] == STACK_SPILL; i--)\n \t\tspill_size++;\n \tif (spill_size != key_size)\n \t\treturn -EOPNOTSUPP;\n \n-\treg = \u0026state-\u003estack[spi].spilled_ptr;\n+\treg = \u0026ss-\u003espilled_ptr;\n \tif (!tnum_is_const(reg-\u003evar_off))\n \t\t/* Stack value not statically known */\n \t\treturn -EOPNOTSUPP;\n@@ -10502,8 +10512,9 @@ static int idstack_push(struct bpf_idmap *idmap, u32 id)\n \t\tif (idmap-\u003emap[i].old == id)\n \t\t\treturn 0;\n \n-\tif (WARN_ON_ONCE(idmap-\u003ecnt \u003e= BPF_ID_MAP_SIZE))\n-\t\treturn -EFAULT;\n+\tif (!bpf_id_scratch_reserve((void **)\u0026idmap-\u003emap, \u0026idmap-\u003ecap, idmap-\u003ecnt,\n+\t\t\t\t sizeof(*idmap-\u003emap)))\n+\t\treturn -ENOMEM;\n \n \tidmap-\u003emap[idmap-\u003ecnt++].old = id;\n \treturn 0;\n@@ -14348,11 +14359,11 @@ s64 bpf_helper_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn\n \t\t\t}\n \t\t\t/*\n \t\t\t * Size arg is const on each path but differs across merged\n-\t\t\t * paths. MAX_BPF_STACK is a safe upper bound for reads.\n+\t\t\t * paths. Reads may extend anywhere up to the frame top.\n \t\t\t */\n \t\t\tif (full_write)\n \t\t\t\treturn 0;\n-\t\t\treturn MAX_BPF_STACK;\n+\t\t\treturn S64_MIN;\n \t\t}\n \t\treturn S64_MIN;\n \tcase ARG_PTR_TO_DYNPTR:\n@@ -14438,7 +14449,8 @@ s64 bpf_kfunc_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn *\n \t\t\tsize = (s64)aux-\u003econst_reg_vals[size_reg];\n \t\t\tgoto out;\n \t\t}\n-\t\treturn MAX_BPF_STACK;\n+\t\t/* Unknown size: the read may extend anywhere up to the frame top. */\n+\t\treturn S64_MIN;\n \t}\n \n \t/* fixed-size pointed-to type: resolve via BTF */\n@@ -15180,7 +15192,8 @@ enum {\n \tREASON_STACK\t= -5,\n };\n \n-static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,\n+static int retrieve_ptr_limit(const struct bpf_verifier_env *env,\n+\t\t\t const struct bpf_reg_state *ptr_reg,\n \t\t\t u32 *alu_limit, bool mask_to_left)\n {\n \tu32 max = 0, ptr_limit = 0;\n@@ -15192,7 +15205,7 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,\n \t\t * offset where we would need to deal with min/max bounds is\n \t\t * currently prohibited for unprivileged.\n \t\t */\n-\t\tmax = MAX_BPF_STACK + mask_to_left;\n+\t\tmax = env-\u003estack_limit + mask_to_left;\n \t\tptr_limit = -ptr_reg-\u003evar_off.value;\n \t\tbreak;\n \tcase PTR_TO_MAP_VALUE:\n@@ -15312,7 +15325,7 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env,\n \t\t\t\t (opcode == BPF_SUB \u0026\u0026 !off_is_neg);\n \t}\n \n-\terr = retrieve_ptr_limit(ptr_reg, \u0026alu_limit, info-\u003emask_to_left);\n+\terr = retrieve_ptr_limit(env, ptr_reg, \u0026alu_limit, info-\u003emask_to_left);\n \tif (err \u003c 0)\n \t\treturn err;\n \n@@ -15442,7 +15455,7 @@ static int check_stack_access_for_ptr_arithmetic(\n \t\treturn -EACCES;\n \t}\n \n-\tif (off \u003e= 0 || off \u003c -MAX_BPF_STACK) {\n+\tif (off \u003e= 0 || off \u003c -(int)env-\u003estack_limit) {\n \t\tverbose(env, \"R%d stack pointer arithmetic goes out of range, \"\n \t\t\t\"prohibited for !root; off=%d\\n\", regno, off);\n \t\treturn -EACCES;\n@@ -17777,10 +17790,12 @@ static void collect_linked_regs(struct bpf_verifier_env *env,\n \t\t\treg = \u0026func-\u003eregs[j];\n \t\t\t__collect_linked_regs(linked_regs, reg, id, i, j, true);\n \t\t}\n-\t\tfor (j = 0; j \u003c func-\u003eallocated_stack / BPF_REG_SIZE; j++) {\n-\t\t\tif (!bpf_is_spilled_reg(\u0026func-\u003estack[j]))\n+\t\tfor (j = 0; j \u003c bpf_stack_nr_slots(func); j++) {\n+\t\t\tstruct bpf_stack_state *ss = bpf_stack_slot(func, j);\n+\n+\t\t\tif (!bpf_is_spilled_reg(ss))\n \t\t\t\tcontinue;\n-\t\t\treg = \u0026func-\u003estack[j].spilled_ptr;\n+\t\t\treg = \u0026ss-\u003espilled_ptr;\n \t\t\t__collect_linked_regs(linked_regs, reg, id, i, j, false);\n \t\t}\n \t}\n@@ -17800,7 +17815,7 @@ static void sync_linked_regs(struct bpf_verifier_env *env, struct bpf_verifier_s\n \tfor (i = 0; i \u003c linked_regs-\u003ecnt; ++i) {\n \t\te = \u0026linked_regs-\u003eentries[i];\n \t\treg = e-\u003eis_reg ? \u0026vstate-\u003eframe[e-\u003eframeno]-\u003eregs[e-\u003eregno]\n-\t\t\t\t: \u0026vstate-\u003eframe[e-\u003eframeno]-\u003estack[e-\u003espi].spilled_ptr;\n+\t\t\t\t: \u0026bpf_stack_slot(vstate-\u003eframe[e-\u003eframeno], e-\u003espi)-\u003espilled_ptr;\n \t\tif (reg-\u003etype != SCALAR_VALUE || reg == known_reg)\n \t\t\tcontinue;\n \t\tif ((reg-\u003eid \u0026 ~BPF_ADD_CONST) != (known_reg-\u003eid \u0026 ~BPF_ADD_CONST))\n@@ -17918,7 +17933,7 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,\n \t}\n \n \tif (insn_flags) {\n-\t\terr = bpf_push_jmp_history(env, this_branch, insn_flags, 0, 0, 0);\n+\t\terr = bpf_push_jmp_history(env, this_branch, insn_flags, 0, 0, NULL, 0);\n \t\tif (err)\n \t\t\treturn err;\n \t}\n@@ -17988,7 +18003,10 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,\n \t * if parent state is created.\n \t */\n \tif (linked_regs.cnt \u003e 1) {\n-\t\terr = bpf_push_jmp_history(env, this_branch, 0, 0, 0, linked_regs_pack(\u0026linked_regs));\n+\t\tu16 packed[LINKED_REGS_MAX];\n+\n+\t\tlinked_regs_pack(\u0026linked_regs, packed);\n+\t\terr = bpf_push_jmp_history(env, this_branch, 0, 0, 0, packed, linked_regs.cnt);\n \t\tif (err)\n \t\t\treturn err;\n \t}\n@@ -18877,23 +18895,27 @@ static void adjust_btf_func(struct bpf_verifier_env *env)\n \t\taux-\u003efunc_info[i].insn_off = env-\u003esubprog_info[i].start;\n }\n \n-/* Find id in idset and increment its count, or add new entry */\n-static void idset_cnt_inc(struct bpf_idset *idset, u32 id)\n+/*\n+ * Find id in idset and increment its count, or add new entry. Returns false\n+ * when a new id could not be recorded, which leaves the counts incomplete.\n+ */\n+static bool idset_cnt_inc(struct bpf_idset *idset, u32 id)\n {\n \tu32 i;\n \n \tfor (i = 0; i \u003c idset-\u003enum_ids; i++) {\n \t\tif (idset-\u003eentries[i].id == id) {\n \t\t\tidset-\u003eentries[i].cnt++;\n-\t\t\treturn;\n+\t\t\treturn true;\n \t\t}\n \t}\n-\t/* New id */\n-\tif (idset-\u003enum_ids \u003c BPF_ID_MAP_SIZE) {\n-\t\tidset-\u003eentries[idset-\u003enum_ids].id = id;\n-\t\tidset-\u003eentries[idset-\u003enum_ids].cnt = 1;\n-\t\tidset-\u003enum_ids++;\n-\t}\n+\tif (!bpf_id_scratch_reserve((void **)\u0026idset-\u003eentries, \u0026idset-\u003ecap, idset-\u003enum_ids,\n+\t\t\t\t sizeof(*idset-\u003eentries)))\n+\t\treturn false;\n+\tidset-\u003eentries[idset-\u003enum_ids].id = id;\n+\tidset-\u003eentries[idset-\u003enum_ids].cnt = 1;\n+\tidset-\u003enum_ids++;\n+\treturn true;\n }\n \n /* Find id in idset and return its count, or 0 if not found */\n@@ -18919,6 +18941,7 @@ void bpf_clear_singular_ids(struct bpf_verifier_env *env,\n \tstruct bpf_idset *idset = \u0026env-\u003eidset_scratch;\n \tstruct bpf_func_state *func;\n \tstruct bpf_reg_state *reg;\n+\tbool complete = true;\n \n \tidset-\u003enum_ids = 0;\n \n@@ -18927,9 +18950,17 @@ void bpf_clear_singular_ids(struct bpf_verifier_env *env,\n \t\t\tcontinue;\n \t\tif (!reg-\u003eid)\n \t\t\tcontinue;\n-\t\tidset_cnt_inc(idset, reg-\u003eid \u0026 ~BPF_ADD_CONST);\n+\t\tcomplete \u0026= idset_cnt_inc(idset, reg-\u003eid \u0026 ~BPF_ADD_CONST);\n \t}));\n \n+\t/*\n+\t * An id that could not be recorded may be shared, and a later\n+\t * occurrence of it may have been recorded with a count of one. Without\n+\t * complete counts keep every id; clearing is only an optimization.\n+\t */\n+\tif (!complete)\n+\t\treturn;\n+\n \tbpf_for_each_reg_in_vstate(st, func, reg, ({\n \t\tif (reg-\u003etype != SCALAR_VALUE)\n \t\t\tcontinue;\n@@ -19386,7 +19417,7 @@ static int do_check(struct bpf_verifier_env *env)\n \t\t}\n \n \t\tif (bpf_is_jmp_point(env, env-\u003einsn_idx)) {\n-\t\t\terr = bpf_push_jmp_history(env, state, 0, 0, 0, 0);\n+\t\t\terr = bpf_push_jmp_history(env, state, 0, 0, 0, NULL, 0);\n \t\t\tif (err)\n \t\t\t\treturn err;\n \t\t}\n@@ -22255,6 +22286,26 @@ static int bpf_prog_verify_signature(struct bpf_verifier_env *env,\n \treturn err;\n }\n \n+/*\n+ * bpf_clone_redirect() transmits from inside the program, so a tc egress or\n+ * lwt_xmit program that redirects to its own device runs again on top of its\n+ * own frame, and again from there, until the datapath's recursion limit drops\n+ * the packet: ten frames deep. Ten MAX_BPF_STACK frames fit the kernel stack\n+ * as they always did; ten MAX_BPF_STACK_JIT frames would not, so a program\n+ * calling it keeps the smaller budget. The redirect helpers that transmit\n+ * after the program has returned leave no frame behind.\n+ */\n+static bool bpf_prog_reenters_datapath(const struct bpf_prog *prog)\n+{\n+\tconst struct bpf_insn *insn = prog-\u003einsnsi;\n+\tint i;\n+\n+\tfor (i = 0; i \u003c prog-\u003elen; i++, insn++)\n+\t\tif (bpf_helper_call(insn) \u0026\u0026 insn-\u003eimm == BPF_FUNC_clone_redirect)\n+\t\t\treturn true;\n+\treturn false;\n+}\n+\n int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,\n \t struct bpf_log_attr *attr_log)\n {\n@@ -22279,6 +22330,9 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,\n \tenv-\u003ebt.env = env;\n \tenv-\u003eprog = *prog;\n \tenv-\u003eops = bpf_verifier_ops[env-\u003eprog-\u003etype];\n+\tenv-\u003estack_limit = bpf_prog_stack_limit(env-\u003eprog);\n+\tif (bpf_prog_reenters_datapath(env-\u003eprog))\n+\t\tenv-\u003estack_limit = MAX_BPF_STACK;\n \n \tenv-\u003eallow_ptr_leaks = bpf_allow_ptr_leaks(env-\u003eprog-\u003eaux-\u003etoken);\n \tenv-\u003eallow_uninit_stack = bpf_allow_uninit_stack(env-\u003eprog-\u003eaux-\u003etoken);\n@@ -22602,6 +22656,8 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,\n \tkvfree(env-\u003egotox_tmp_buf);\n \tkvfree(env-\u003ecallx_edges);\n \tkvfree(env-\u003efunc_ptrs);\n+\tkfree(env-\u003eidmap_scratch.map);\n+\tkfree(env-\u003eidset_scratch.entries);\n \tbpf_diag_free(env);\n \tkvfree(env);\n \treturn ret;\ndiff --git a/tools/testing/selftests/bpf/prog_tests/struct_ops_private_stack.c b/tools/testing/selftests/bpf/prog_tests/struct_ops_private_stack.c\nindex 98db9bafa44b2..2b3ec2b790918 100644\n--- a/tools/testing/selftests/bpf/prog_tests/struct_ops_private_stack.c\n+++ b/tools/testing/selftests/bpf/prog_tests/struct_ops_private_stack.c\n@@ -4,6 +4,7 @@\n #include \"struct_ops_private_stack.skel.h\"\n #include \"struct_ops_private_stack_fail.skel.h\"\n #include \"struct_ops_private_stack_recur.skel.h\"\n+#include \"struct_ops_private_stack_large.skel.h\"\n \n #if defined(__x86_64__) || defined(__aarch64__) || defined(__powerpc64__)\n static void test_private_stack(void)\n@@ -78,6 +79,34 @@ static void test_private_stack_recur(void)\n \tstruct_ops_private_stack_recur__destroy(skel);\n }\n \n+/* Two frames of 2 KiB each on the private stack */\n+static void test_private_stack_large(void)\n+{\n+\tstruct struct_ops_private_stack_large *skel;\n+\tstruct bpf_link *link;\n+\n+\tif (!is_large_stack_supported()) {\n+\t\ttest__skip();\n+\t\treturn;\n+\t}\n+\n+\tskel = struct_ops_private_stack_large__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"struct_ops_private_stack_large__open_and_load\"))\n+\t\treturn;\n+\n+\tlink = bpf_map__attach_struct_ops(skel-\u003emaps.testmod_1);\n+\tif (!ASSERT_OK_PTR(link, \"attach_struct_ops\"))\n+\t\tgoto cleanup;\n+\n+\tASSERT_OK(trigger_module_test_read(256), \"trigger_read\");\n+\n+\tASSERT_EQ(skel-\u003ebss-\u003eval, 100 + 30 + 12, \"val\");\n+\n+\tbpf_link__destroy(link);\n+cleanup:\n+\tstruct_ops_private_stack_large__destroy(skel);\n+}\n+\n static void __test_struct_ops_private_stack(void)\n {\n \tif (test__start_subtest(\"private_stack\"))\n@@ -86,6 +115,8 @@ static void __test_struct_ops_private_stack(void)\n \t\ttest_private_stack_fail();\n \tif (test__start_subtest(\"private_stack_recur\"))\n \t\ttest_private_stack_recur();\n+\tif (test__start_subtest(\"private_stack_large\"))\n+\t\ttest_private_stack_large();\n }\n #else\n static void __test_struct_ops_private_stack(void)\ndiff --git a/tools/testing/selftests/bpf/prog_tests/tailcalls.c b/tools/testing/selftests/bpf/prog_tests/tailcalls.c\nindex aefb46778307a..88d223110ecd5 100644\n--- a/tools/testing/selftests/bpf/prog_tests/tailcalls.c\n+++ b/tools/testing/selftests/bpf/prog_tests/tailcalls.c\n@@ -10,6 +10,7 @@\n #include \"tc_bpf2bpf.skel.h\"\n #include \"tailcall_fail.skel.h\"\n #include \"tailcall_cgrp_storage_owner.skel.h\"\n+#include \"tailcall_large_stack.skel.h\"\n #include \"tailcall_cgrp_storage_no_storage.skel.h\"\n #include \"tailcall_cgrp_storage.skel.h\"\n #include \"tailcall_sleepable.skel.h\"\n@@ -2025,6 +2026,45 @@ static void test_tailcall_bpf2bpf_fexit_links(void)\n \ttailcall_bpf2bpf2__destroy(skel_tc);\n }\n \n+/*\n+ * test_tailcall_large_stack runs a tail call made from a subprog with a 1536\n+ * byte frame, under a 240-byte caller, into a program with a 2 KiB frame:\n+ *\n+ * entry (240) --call-\u003e subprog_tail (1536) --tailcall-\u003e classifier_0 (2048)\n+ */\n+static void test_tailcall_large_stack(void)\n+{\n+\tstruct tailcall_large_stack *skel;\n+\tint err, prog_fd, map_fd, key = 0;\n+\tchar buff[128] = {};\n+\tLIBBPF_OPTS(bpf_test_run_opts, topts,\n+\t\t .data_in = buff,\n+\t\t .data_size_in = sizeof(buff),\n+\t\t .repeat = 1,\n+\t);\n+\n+\tif (!is_large_stack_supported()) {\n+\t\ttest__skip();\n+\t\treturn;\n+\t}\n+\n+\tskel = tailcall_large_stack__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"tailcall_large_stack__open_and_load\"))\n+\t\treturn;\n+\n+\tprog_fd = bpf_program__fd(skel-\u003eprogs.classifier_0);\n+\tmap_fd = bpf_map__fd(skel-\u003emaps.jmp_table);\n+\terr = bpf_map_update_elem(map_fd, \u0026key, \u0026prog_fd, BPF_ANY);\n+\tif (!ASSERT_OK(err, \"update jmp_table\"))\n+\t\tgoto out;\n+\n+\terr = bpf_prog_test_run_opts(bpf_program__fd(skel-\u003eprogs.entry), \u0026topts);\n+\tASSERT_OK(err, \"test_run\");\n+\tASSERT_EQ(topts.retval, 42 + 7, \"retval\");\n+out:\n+\ttailcall_large_stack__destroy(skel);\n+}\n+\n void test_tailcalls(void)\n {\n \tif (test__start_subtest(\"tailcall_1\"))\n@@ -2096,4 +2136,6 @@ void test_tailcalls(void)\n \ttest_tailcall_callback();\n \tif (test__start_subtest(\"tailcall_bpf2bpf_fexit_links\"))\n \t\ttest_tailcall_bpf2bpf_fexit_links();\n+\tif (test__start_subtest(\"tailcall_large_stack\"))\n+\t\ttest_tailcall_large_stack();\n }\ndiff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c\nindex 9c3ff7d83fbe3..dbf98751aae65 100644\n--- a/tools/testing/selftests/bpf/prog_tests/verifier.c\n+++ b/tools/testing/selftests/bpf/prog_tests/verifier.c\n@@ -62,6 +62,7 @@\n #include \"verifier_kfunc_uninit.skel.h\"\n #include \"verifier_kfunc_uninit_multi.skel.h\"\n #include \"verifier_ld_ind.skel.h\"\n+#include \"verifier_large_stack.skel.h\"\n #include \"verifier_ldsx.skel.h\"\n #include \"verifier_leak_ptr.skel.h\"\n #include \"verifier_linked_scalars.skel.h\"\n@@ -244,6 +245,7 @@ void test_verifier_kfunc_uninit(void) { RUN_TESTS(verifier_kfunc_uninit)\n void test_verifier_kfunc_uninit_multi(void) { RUN_TESTS(verifier_kfunc_uninit_multi); }\n void test_verifier_load_acquire(void) { RUN(verifier_load_acquire); }\n void test_verifier_ld_ind(void) { RUN(verifier_ld_ind); }\n+void test_verifier_large_stack(void) { RUN(verifier_large_stack); }\n void test_verifier_ldsx(void) { RUN(verifier_ldsx); }\n void test_verifier_leak_ptr(void) { RUN(verifier_leak_ptr); }\n void test_verifier_linked_scalars(void) { RUN(verifier_linked_scalars); }\ndiff --git a/tools/testing/selftests/bpf/progs/async_stack_depth.c b/tools/testing/selftests/bpf/progs/async_stack_depth.c\nindex 36734683acbdb..9cd874a90b39a 100644\n--- a/tools/testing/selftests/bpf/progs/async_stack_depth.c\n+++ b/tools/testing/selftests/bpf/progs/async_stack_depth.c\n@@ -29,7 +29,49 @@ static int bad_timer_cb(void *map, int *key, struct bpf_timer *timer)\n \treturn buf[255] + timer_cb(NULL, NULL, NULL);\n }\n \n+/*\n+ * The same shapes scaled to the 2 KiB budget of JITs with large stacks. The\n+ * compiler caps a single function at 512 bytes, so the depth comes from a\n+ * chain of 480-byte frames.\n+ */\n+__attribute__((noinline))\n+static int timer_cb_large_0(void *map, int *key, struct bpf_timer *timer)\n+{\n+\tvolatile char buf[480] = {};\n+\treturn buf[69];\n+}\n+\n+__attribute__((noinline))\n+static int timer_cb_large_1(void *map, int *key, struct bpf_timer *timer)\n+{\n+\tvolatile char buf[480] = {};\n+\treturn buf[69] + timer_cb_large_0(map, key, timer);\n+}\n+\n+__attribute__((noinline))\n+static int timer_cb_large_2(void *map, int *key, struct bpf_timer *timer)\n+{\n+\tvolatile char buf[480] = {};\n+\treturn buf[69] + timer_cb_large_1(map, key, timer);\n+}\n+\n+__attribute__((noinline))\n+static int timer_cb_large_3(void *map, int *key, struct bpf_timer *timer)\n+{\n+\tvolatile char buf[480] = {};\n+\treturn buf[69] + timer_cb_large_2(map, key, timer);\n+}\n+\n+/* 5 * 480 = 2400 bytes on its own */\n+__attribute__((noinline))\n+static int bad_timer_cb_large(void *map, int *key, struct bpf_timer *timer)\n+{\n+\tvolatile char buf[480] = {};\n+\treturn buf[255] + timer_cb_large_3(map, key, timer);\n+}\n+\n SEC(\"tc\")\n+__load_if_no_large_stack()\n __failure __msg(\"combined stack size of 2 calls is\")\n int pseudo_call_check(struct __sk_buff *ctx)\n {\n@@ -44,7 +86,25 @@ int pseudo_call_check(struct __sk_buff *ctx)\n \treturn bpf_timer_set_callback(\u0026elem-\u003etimer, timer_cb) + buf[0];\n }\n \n+/* main plus the four frames under timer_cb_large_3: 2400 bytes */\n SEC(\"tc\")\n+__load_if_large_stack()\n+__failure __msg(\"combined stack size of 5 calls is\")\n+int pseudo_call_check_large(struct __sk_buff *ctx)\n+{\n+\tstruct hmap_elem *elem;\n+\tvolatile char buf[480] = {};\n+\n+\telem = bpf_map_lookup_elem(\u0026hmap, \u0026(int){0});\n+\tif (!elem)\n+\t\treturn 0;\n+\n+\ttimer_cb_large_3(NULL, NULL, NULL);\n+\treturn bpf_timer_set_callback(\u0026elem-\u003etimer, timer_cb_large_3) + buf[0];\n+}\n+\n+SEC(\"tc\")\n+__load_if_no_large_stack()\n __failure __msg(\"combined stack size of 2 calls is\")\n int async_call_root_check(struct __sk_buff *ctx)\n {\n@@ -58,4 +118,19 @@ int async_call_root_check(struct __sk_buff *ctx)\n \treturn bpf_timer_set_callback(\u0026elem-\u003etimer, bad_timer_cb) + buf[0];\n }\n \n+SEC(\"tc\")\n+__load_if_large_stack()\n+__failure __msg(\"combined stack size of 5 calls is\")\n+int async_call_root_check_large(struct __sk_buff *ctx)\n+{\n+\tstruct hmap_elem *elem;\n+\tvolatile char buf[480] = {};\n+\n+\telem = bpf_map_lookup_elem(\u0026hmap, \u0026(int){0});\n+\tif (!elem)\n+\t\treturn 0;\n+\n+\treturn bpf_timer_set_callback(\u0026elem-\u003etimer, bad_timer_cb_large) + buf[0];\n+}\n+\n char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/progs/bpf_misc.h b/tools/testing/selftests/bpf/progs/bpf_misc.h\nindex 2ced1d751acea..f3dbc3b59bff2 100644\n--- a/tools/testing/selftests/bpf/progs/bpf_misc.h\n+++ b/tools/testing/selftests/bpf/progs/bpf_misc.h\n@@ -175,6 +175,9 @@\n #define __prepare_priv\t\t__test_tag(\"test_prepare_priv\")\n #define __load_if_JITed()\t__test_tag(\"load_mode=jited\")\n #define __load_if_no_JITed()\t__test_tag(\"load_mode=no_jited\")\n+/* Whether programs may use more than 512 bytes of stack on this kernel and JIT */\n+#define __load_if_large_stack()\t\t__test_tag(\"stack_mode=large\")\n+#define __load_if_no_large_stack()\t__test_tag(\"stack_mode=small\")\n #define __stderr(msg)\t\t__test_tag(\"test_expect_stderr=\" msg)\n #define __stderr_unpriv(msg)\t__test_tag(\"test_expect_stderr_unpriv=\" msg)\n #define __stdout(msg)\t\t__test_tag(\"test_expect_stdout=\" msg)\ndiff --git a/tools/testing/selftests/bpf/progs/struct_ops_private_stack_fail.c b/tools/testing/selftests/bpf/progs/struct_ops_private_stack_fail.c\nindex 1442728f56046..c8cb35b37867b 100644\n--- a/tools/testing/selftests/bpf/progs/struct_ops_private_stack_fail.c\n+++ b/tools/testing/selftests/bpf/progs/struct_ops_private_stack_fail.c\n@@ -4,6 +4,7 @@\n #include \u003cbpf/bpf_helpers.h\u003e\n #include \u003cbpf/bpf_tracing.h\u003e\n #include \"../test_kmods/bpf_testmod.h\"\n+#include \"bpf_misc.h\"\n \n char _license[] SEC(\"license\") = \"GPL\";\n \n@@ -25,6 +26,44 @@ __noinline static int subprog1(int *a)\n \treturn subprog2(a, b);\n }\n \n+/*\n+ * A chain of 480-byte frames under test_2, so that its call chain exceeds\n+ * the 2 KiB budget of JITs with large stacks as well as the 512 bytes\n+ * allowed elsewhere. The compiler caps a single function at 512 bytes, and\n+ * the buffers are volatile so that it cannot shrink them.\n+ */\n+__noinline static int subprog_deep4(int *a)\n+{\n+\tvolatile char b[480] = {};\n+\n+\t__sink(b[479]);\n+\treturn a[10] + b[20];\n+}\n+\n+__noinline static int subprog_deep3(int *a)\n+{\n+\tvolatile char b[480] = {};\n+\n+\t__sink(b[479]);\n+\treturn subprog_deep4(a) + b[20];\n+}\n+\n+__noinline static int subprog_deep2(int *a)\n+{\n+\tvolatile char b[480] = {};\n+\n+\t__sink(b[479]);\n+\treturn subprog_deep3(a) + b[20];\n+}\n+\n+__noinline static int subprog_deep1(int *a)\n+{\n+\tvolatile char b[480] = {};\n+\n+\t__sink(b[479]);\n+\treturn subprog_deep2(a) + b[20];\n+}\n+\n \n SEC(\"struct_ops\")\n int BPF_PROG(test_1)\n@@ -41,11 +80,13 @@ int BPF_PROG(test_1)\n SEC(\"struct_ops\")\n int BPF_PROG(test_2)\n {\n-\t/* stack size 400 bytes */\n-\tint a[100] = {};\n+\t/* stack size 476 bytes, over 2 KiB with the four 480-byte deep subprogs */\n+\tvolatile char buf[376] = {};\n+\tint a[25] = {};\n \n+\t__sink(buf[375]);\n \ta[10] = 3;\n-\tval_j = subprog1(a);\n+\tval_j = subprog1(a) + subprog_deep1(a);\n \treturn 0;\n }\n \ndiff --git a/tools/testing/selftests/bpf/progs/struct_ops_private_stack_large.c b/tools/testing/selftests/bpf/progs/struct_ops_private_stack_large.c\nnew file mode 100644\nindex 0000000000000..94a25a2cff6ec\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/struct_ops_private_stack_large.c\n@@ -0,0 +1,51 @@\n+// SPDX-License-Identifier: GPL-2.0\n+\n+#include \u003cvmlinux.h\u003e\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \u003cbpf/bpf_tracing.h\u003e\n+#include \"../test_kmods/bpf_testmod.h\"\n+#include \"bpf_misc.h\"\n+\n+char _license[] SEC(\"license\") = \"GPL\";\n+\n+long val;\n+\n+/* On a private stack every frame gets the whole 2 KiB budget. */\n+__used __naked\n+static long frame_2048_leaf(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = 30;\t\t\t\t\t\\\n+\t*(u64 *)(r10 - 2048) = r1;\t\t\t\\\n+\tr1 = 12;\t\t\t\t\t\\\n+\t*(u64 *)(r10 - 8) = r1;\t\t\t\t\\\n+\tr0 = *(u64 *)(r10 - 2048);\t\t\t\\\n+\tr1 = *(u64 *)(r10 - 8);\t\t\t\t\\\n+\tr0 += r1;\t\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n+/* test_1 is the member bpf_testmod requests a private stack for */\n+SEC(\"struct_ops\")\n+__naked int test_1(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = 100;\t\t\t\t\t\\\n+\t*(u64 *)(r10 - 2048) = r1;\t\t\t\\\n+\tcall frame_2048_leaf;\t\t\t\t\\\n+\tr1 = *(u64 *)(r10 - 2048);\t\t\t\\\n+\tr0 += r1;\t\t\t\t\t\\\n+\tr1 = %[val] ll;\t\t\t\t\t\\\n+\t*(u64 *)(r1 + 0) = r0;\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(val)\n+\t: __clobber_all);\n+}\n+\n+SEC(\".struct_ops\")\n+struct bpf_testmod_ops3 testmod_1 = {\n+\t.test_1 = (void *)test_1,\n+};\ndiff --git a/tools/testing/selftests/bpf/progs/tailcall_large_stack.c b/tools/testing/selftests/bpf/progs/tailcall_large_stack.c\nnew file mode 100644\nindex 0000000000000..977197dac5d37\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/tailcall_large_stack.c\n@@ -0,0 +1,62 @@\n+// SPDX-License-Identifier: GPL-2.0\n+#include \u003clinux/bpf.h\u003e\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \"bpf_misc.h\"\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_PROG_ARRAY);\n+\t__uint(max_entries, 1);\n+\t__uint(key_size, sizeof(__u32));\n+\t__uint(value_size, sizeof(__u32));\n+} jmp_table SEC(\".maps\");\n+\n+/* The tail call target sets up a 2 KiB frame of its own and uses both of its ends. */\n+SEC(\"tc\")\n+__naked int classifier_0(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = 42;\t\t\t\t\t\\\n+\t*(u64 *)(r10 - 2048) = r1;\t\t\t\\\n+\tr1 = 7;\t\t\t\t\t\t\\\n+\t*(u64 *)(r10 - 8) = r1;\t\t\t\t\\\n+\tr0 = *(u64 *)(r10 - 2048);\t\t\t\\\n+\tr1 = *(u64 *)(r10 - 8);\t\t\t\t\\\n+\tr0 += r1;\t\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n+/*\n+ * The frame of the subprog doing the tail call is unwound by it, so it may be\n+ * large; only the frames of its callers stay behind and are limited to 256\n+ * bytes in total. Returns 1 when the tail call falls through.\n+ */\n+__used __naked\n+static int subprog_tail(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr2 = 1;\t\t\t\t\t\t\\\n+\t*(u64 *)(r10 - 1536) = r2;\t\t\t\\\n+\tr2 = %[jmp_table] ll;\t\t\t\t\\\n+\tr3 = 0;\t\t\t\t\t\t\\\n+\tcall %[bpf_tail_call];\t\t\t\t\\\n+\tr0 = 1;\t\t\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm(bpf_tail_call),\n+\t __imm_addr(jmp_table)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"tc\")\n+__naked int entry(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr2 = 2;\t\t\t\t\t\t\\\n+\t*(u64 *)(r10 - 240) = r2;\t\t\t\\\n+\tcall subprog_tail;\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n+char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/progs/test_global_func1.c b/tools/testing/selftests/bpf/progs/test_global_func1.c\nindex fc69ff18880d5..f0eca282e0d43 100644\n--- a/tools/testing/selftests/bpf/progs/test_global_func1.c\n+++ b/tools/testing/selftests/bpf/progs/test_global_func1.c\n@@ -48,8 +48,73 @@ int f3(int val, struct __sk_buff *skb, int var)\n }\n \n SEC(\"tc\")\n+__load_if_no_large_stack()\n __failure __msg(\"combined stack size of 3 calls is\")\n int global_func1(struct __sk_buff *skb)\n {\n \treturn f0(1, skb) + f1(skb) + f2(2, skb) + f3(3, skb, 4);\n }\n+\n+/*\n+ * A chain of five frames that stay under 512 bytes each but add up to more\n+ * than the 2 KiB budget of JITs with large stacks; the chain also exceeds\n+ * 512 bytes after two frames, so it is rejected everywhere.\n+ */\n+#define MAX_STACK_LARGE 480\n+\n+__attribute__ ((noinline))\n+int g0(struct __sk_buff *skb)\n+{\n+\tvolatile char buf[MAX_STACK_LARGE] = {};\n+\n+\t__sink(buf[MAX_STACK_LARGE - 1]);\n+\n+\treturn skb-\u003elen;\n+}\n+\n+__attribute__ ((noinline))\n+int g1(struct __sk_buff *skb)\n+{\n+\tvolatile char buf[MAX_STACK_LARGE] = {};\n+\n+\t__sink(buf[MAX_STACK_LARGE - 1]);\n+\n+\treturn g0(skb) + skb-\u003elen;\n+}\n+\n+__attribute__ ((noinline))\n+int g2(struct __sk_buff *skb)\n+{\n+\tvolatile char buf[MAX_STACK_LARGE] = {};\n+\n+\t__sink(buf[MAX_STACK_LARGE - 1]);\n+\n+\treturn g1(skb) + skb-\u003elen;\n+}\n+\n+__attribute__ ((noinline))\n+int g3(struct __sk_buff *skb)\n+{\n+\tvolatile char buf[MAX_STACK_LARGE] = {};\n+\n+\t__sink(buf[MAX_STACK_LARGE - 1]);\n+\n+\treturn g2(skb) + skb-\u003elen;\n+}\n+\n+__attribute__ ((noinline))\n+int g4(struct __sk_buff *skb)\n+{\n+\tvolatile char buf[MAX_STACK_LARGE] = {};\n+\n+\t__sink(buf[MAX_STACK_LARGE - 1]);\n+\n+\treturn g3(skb) + skb-\u003elen;\n+}\n+\n+SEC(\"tc\")\n+__failure __msg(\"combined stack size of {{[0-9]+}} calls is\")\n+int global_func1_deep(struct __sk_buff *skb)\n+{\n+\treturn g4(skb);\n+}\ndiff --git a/tools/testing/selftests/bpf/progs/test_global_func_deep_stack.c b/tools/testing/selftests/bpf/progs/test_global_func_deep_stack.c\nindex 1b634b543b629..edb8a223a3cb2 100644\n--- a/tools/testing/selftests/bpf/progs/test_global_func_deep_stack.c\n+++ b/tools/testing/selftests/bpf/progs/test_global_func_deep_stack.c\n@@ -67,12 +67,30 @@ int XCAT(f, n)(unsigned long a) \\\n #define F_31 F_30 FN(31, 30)\n #define F_32 F_31 FN(32, 31)\n \n+/* Same, with a 480-byte frame, to exceed the 2 KiB budget of large stacks. */\n+#define FNB(n, prev) \\\n+__attribute__((noinline)) \\\n+int XCAT(f, n)(unsigned long a) \\\n+{ \\\n+\tvolatile char buf[480] = {}; \\\n+\tvolatile long b = XCAT(f, prev)(a - 1); \\\n+\tif (!b) \\\n+\t\treturn 0; \\\n+\treturn b + buf[479] + 1; \\\n+}\n+\n+#define F_33 F_32 FNB(33, 32)\n+#define F_34 F_33 FNB(34, 33)\n+#define F_35 F_34 FNB(35, 34)\n+#define F_36 F_35 FNB(36, 35)\n+#define F_37 F_36 FNB(37, 36)\n+\n #define CAT2(a, b) a ## b\n #define XCAT2(a, b) CAT2(a, b)\n \n #define F(n) XCAT2(F_, n)\n \n-F(32)\n+F(37)\n \n /* Ensure that even 32 levels deep, the function verifies. */\n SEC(\"syscall\")\n@@ -88,8 +106,21 @@ int global_func_deep_stack_success(struct __sk_buff *skb)\n * the size.\n */\n SEC(\"syscall\")\n+__load_if_no_large_stack()\n __failure __msg(\"combined stack size of 34 calls\")\n int global_func_deep_stack_fail(struct __sk_buff *skb)\n {\n \treturn f32(123);\n }\n+\n+/*\n+ * Five 480-byte frames on top of the chain: 5 * 480 + 33 * 16 = 2928 bytes,\n+ * more than the 2 KiB budget of JITs with large stacks, and more than 512\n+ * bytes after the second frame everywhere else.\n+ */\n+SEC(\"syscall\")\n+__failure __msg(\"combined stack size of {{[0-9]+}} calls\")\n+int global_func_deep_stack_fail_large(struct __sk_buff *skb)\n+{\n+\treturn f37(123);\n+}\ndiff --git a/tools/testing/selftests/bpf/progs/verifier_callx.c b/tools/testing/selftests/bpf/progs/verifier_callx.c\nindex 238fc75ad154d..ea3d3f97da312 100644\n--- a/tools/testing/selftests/bpf/progs/verifier_callx.c\n+++ b/tools/testing/selftests/bpf/progs/verifier_callx.c\n@@ -626,19 +626,64 @@ static unsigned long use_stack_304(void)\n \t);\n }\n \n+\n+/* Four 480-byte frames, deeper than any budget together with their caller */\n+__naked __noinline __used\n+static unsigned long use_stack_480_0(void)\n+{\n+\tasm volatile (\n+\t\t\"r0 = 0;\"\n+\t\t\"*(u64 *)(r10 - 480) = r0;\"\n+\t\t\"exit;\"\n+\t);\n+}\n+\n+__naked __noinline __used\n+static unsigned long use_stack_480_1(void)\n+{\n+\tasm volatile (\n+\t\t\"r0 = 0;\"\n+\t\t\"*(u64 *)(r10 - 480) = r0;\"\n+\t\t\"call use_stack_480_0;\"\n+\t\t\"exit;\"\n+\t);\n+}\n+\n+__naked __noinline __used\n+static unsigned long use_stack_480_2(void)\n+{\n+\tasm volatile (\n+\t\t\"r0 = 0;\"\n+\t\t\"*(u64 *)(r10 - 480) = r0;\"\n+\t\t\"call use_stack_480_1;\"\n+\t\t\"exit;\"\n+\t);\n+}\n+\n+__naked __noinline __used\n+static unsigned long use_stack_480_3(void)\n+{\n+\tasm volatile (\n+\t\t\"r0 = 0;\"\n+\t\t\"*(u64 *)(r10 - 480) = r0;\"\n+\t\t\"call use_stack_480_2;\"\n+\t\t\"exit;\"\n+\t);\n+}\n+\n /* stack of the callee of callx is accounted */\n SEC(\"socket\")\n-__failure __msg(\"combined stack size of 2 calls is\")\n+__failure __msg(\"combined stack size of {{[0-9]+}} calls is\")\n __naked void callx_stack_depth(void)\n {\n \tasm volatile (\n \t\t\"r0 = 0;\"\n-\t\t\"*(u64 *)(r10 - 304) = r0;\"\n-\t\t\"r2 = %[use_stack_304] ll;\"\n+\t\t\"*(u64 *)(r10 - 480) = r0;\"\n+\t\t\"r2 = %[use_stack_480_3] ll;\"\n \t\t\"callx r2;\"\n \t\t\"exit;\"\n \t\t:\n-\t\t: __imm_addr(use_stack_304)\n+\t\t: __imm_addr(use_stack_480_3)\n \t\t: __clobber_all);\n }\n \n@@ -655,19 +700,19 @@ static unsigned long apply_stack_304(void)\n }\n \n /*\n- * The address of use_stack_304() is taken by the main prog that doesn't\n+ * The address of use_stack_480_3() is taken by the main prog that doesn't\n * use stack, but it is called from apply_stack_304().\n */\n SEC(\"socket\")\n-__failure __msg(\"combined stack size of 3 calls is\")\n+__failure __msg(\"combined stack size of {{[0-9]+}} calls is\")\n __naked void callx_stack_depth_nested(void)\n {\n \tasm volatile (\n-\t\t\"r1 = %[use_stack_304] ll;\"\n+\t\t\"r1 = %[use_stack_480_3] ll;\"\n \t\t\"call apply_stack_304;\"\n \t\t\"exit;\"\n \t\t:\n-\t\t: __imm_addr(use_stack_304)\n+\t\t: __imm_addr(use_stack_480_3)\n \t\t: __clobber_all);\n }\n \ndiff --git a/tools/testing/selftests/bpf/progs/verifier_callx_rodata.c b/tools/testing/selftests/bpf/progs/verifier_callx_rodata.c\nindex af1f9305da377..65d34769c8bfd 100644\n--- a/tools/testing/selftests/bpf/progs/verifier_callx_rodata.c\n+++ b/tools/testing/selftests/bpf/progs/verifier_callx_rodata.c\n@@ -579,23 +579,58 @@ __naked void callx_rodata_recursion(void)\n \t\t::: __clobber_all);\n }\n \n+\n+/* Four 480-byte frames, deeper than any budget together with their caller */\n __naked __noinline __used\n-static unsigned long use_stack_304(void)\n+static unsigned long use_stack_480_0(void)\n {\n \tasm volatile (\n \t\t\"r0 = 0;\"\n-\t\t\"*(u64 *)(r10 - 304) = r0;\"\n+\t\t\"*(u64 *)(r10 - 480) = r0;\"\n+\t\t\"exit;\"\n+\t);\n+}\n+\n+__naked __noinline __used\n+static unsigned long use_stack_480_1(void)\n+{\n+\tasm volatile (\n+\t\t\"r0 = 0;\"\n+\t\t\"*(u64 *)(r10 - 480) = r0;\"\n+\t\t\"call use_stack_480_0;\"\n+\t\t\"exit;\"\n+\t);\n+}\n+\n+__naked __noinline __used\n+static unsigned long use_stack_480_2(void)\n+{\n+\tasm volatile (\n+\t\t\"r0 = 0;\"\n+\t\t\"*(u64 *)(r10 - 480) = r0;\"\n+\t\t\"call use_stack_480_1;\"\n+\t\t\"exit;\"\n+\t);\n+}\n+\n+__naked __noinline __used\n+static unsigned long use_stack_480_3(void)\n+{\n+\tasm volatile (\n+\t\t\"r0 = 0;\"\n+\t\t\"*(u64 *)(r10 - 480) = r0;\"\n+\t\t\"call use_stack_480_2;\"\n \t\t\"exit;\"\n \t);\n }\n \n /* stack of all possible callees is accounted */\n SEC(\"socket\")\n-__failure __msg(\"combined stack size of 2 calls is\")\n+__failure __msg(\"combined stack size of {{[0-9]+}} calls is\")\n __naked void callx_rodata_stack_depth(void)\n {\n \tasm volatile (\n-\t\tFUNC_TABLE2(tbl, ret0, use_stack_304)\n+\t\tFUNC_TABLE2(tbl, ret0, use_stack_480_3)\n \t\t\"r0 = 0;\"\n \t\t\"*(u64 *)(r10 - 304) = r0;\"\n \t\t\"call %[bpf_get_prandom_u32];\"\n@@ -613,11 +648,11 @@ __naked void callx_rodata_stack_depth(void)\n \n /* stack of a callback that is read from the data is accounted too */\n SEC(\"socket\")\n-__failure __msg(\"combined stack size of 2 calls is\")\n+__failure __msg(\"combined stack size of {{[0-9]+}} calls is\")\n __naked void callx_rodata_callback_stack_depth(void)\n {\n \tasm volatile (\n-\t\tFUNC_TABLE2(tbl, use_stack_304, ret0)\n+\t\tFUNC_TABLE2(tbl, use_stack_480_3, ret0)\n \t\t\"r0 = 0;\"\n \t\t\"*(u64 *)(r10 - 304) = r0;\"\n \t\t\"r6 = tbl_%= ll;\"\ndiff --git a/tools/testing/selftests/bpf/progs/verifier_large_stack.c b/tools/testing/selftests/bpf/progs/verifier_large_stack.c\nnew file mode 100644\nindex 0000000000000..c2a3ddf5ac913\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/verifier_large_stack.c\n@@ -0,0 +1,425 @@\n+// SPDX-License-Identifier: GPL-2.0\n+\n+#include \u003clinux/bpf.h\u003e\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \"bpf_misc.h\"\n+\n+/*\n+ * Programs may use MAX_BPF_STACK_JIT (2 KiB) of stack on JITs that support\n+ * large stacks, combined over a call chain, with no separate limit on a\n+ * single frame. Interpreted programs and other JITs keep 512 bytes.\n+ */\n+\n+SEC(\"socket\")\n+__description(\"single frame of 2048 bytes\")\n+__load_if_large_stack()\n+__success __success_unpriv __retval(42)\n+__naked void single_frame_2048(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = r10;\t\t\t\t\t\\\n+\tr1 += -2048;\t\t\t\t\t\\\n+\tr0 = 42;\t\t\t\t\t\\\n+\t*(u64*)(r1 + 0) = r0;\t\t\t\t\\\n+\tr0 = *(u64*)(r1 + 0);\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n+SEC(\"socket\")\n+__description(\"single frame of 2048 bytes without large stack support\")\n+__load_if_no_large_stack()\n+__failure __msg(\"invalid write to stack R1 off=-2048 size=8\")\n+__naked void single_frame_2048_no_large_stack(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = r10;\t\t\t\t\t\\\n+\tr1 += -2048;\t\t\t\t\t\\\n+\tr0 = 42;\t\t\t\t\t\\\n+\t*(u64*)(r1 + 0) = r0;\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n+__used __naked\n+static void frame_512_leaf(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = 1;\t\t\t\t\t\t\\\n+\t*(u64 *)(r10 - 512) = r1;\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n+__used __naked\n+static void frame_512_depth_2(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = 2;\t\t\t\t\t\t\\\n+\t*(u64 *)(r10 - 512) = r1;\t\t\t\\\n+\tcall frame_512_leaf;\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n+__used __naked\n+static void frame_512_depth_3(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = 3;\t\t\t\t\t\t\\\n+\t*(u64 *)(r10 - 512) = r1;\t\t\t\\\n+\tcall frame_512_depth_2;\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n+__used __naked\n+static void frame_512_depth_4(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = 4;\t\t\t\t\t\t\\\n+\t*(u64 *)(r10 - 512) = r1;\t\t\t\\\n+\tcall frame_512_depth_3;\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n+SEC(\"socket\")\n+__description(\"four frames of 512 bytes fit the 2 KiB budget\")\n+__load_if_large_stack()\n+__success __log_level(4) __msg(\"stack depth max 2048\")\n+__naked void four_frames_of_512(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tcall frame_512_depth_4;\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n+SEC(\"socket\")\n+__description(\"five frames of 512 bytes exceed the 2 KiB budget\")\n+__load_if_large_stack()\n+__failure __msg(\"combined stack size of 5 calls is 2560. Too large\")\n+__naked void five_frames_of_512(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = 5;\t\t\t\t\t\t\\\n+\t*(u64 *)(r10 - 512) = r1;\t\t\t\\\n+\tcall frame_512_depth_4;\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n+__used __naked\n+static void frame_1536_leaf(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = 1;\t\t\t\t\t\t\\\n+\t*(u64 *)(r10 - 1536) = r1;\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n+SEC(\"socket\")\n+__description(\"512-byte frame calling a 1536-byte frame\")\n+__load_if_large_stack()\n+__success __log_level(4) __msg(\"stack depth max 2048\")\n+__naked void uneven_frames_fit(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = 2;\t\t\t\t\t\t\\\n+\t*(u64 *)(r10 - 512) = r1;\t\t\t\\\n+\tcall frame_1536_leaf;\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n+SEC(\"socket\")\n+__description(\"520-byte frame calling a 1536-byte frame\")\n+__load_if_large_stack()\n+__failure __msg(\"combined stack size of 2 calls is 2064. Too large\")\n+__naked void uneven_frames_exceed(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = 2;\t\t\t\t\t\t\\\n+\t*(u64 *)(r10 - 520) = r1;\t\t\t\\\n+\tcall frame_1536_leaf;\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n+#ifdef __BPF_FEATURE_MAY_GOTO\n+/* may_goto adds its counter below the frame; a JIT does not hold that against the budget */\n+SEC(\"socket\")\n+__description(\"frame of 2048 bytes with may_goto\")\n+__load_if_large_stack()\n+__success __retval(42)\n+__naked void frame_2048_with_may_goto(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = r10;\t\t\t\t\t\\\n+\tr1 += -2048;\t\t\t\t\t\\\n+\tr0 = 42;\t\t\t\t\t\\\n+\t*(u32*)(r1 + 0) = r0;\t\t\t\t\\\n+\tmay_goto l0_%=;\t\t\t\t\t\\\n+\tr2 = 100;\t\t\t\t\t\\\n+\tl0_%=:\t\t\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+#endif\n+\n+SEC(\"socket\")\n+__description(\"variable offset write reaching 2048 bytes deep\")\n+__load_if_large_stack()\n+__success\n+__naked void var_off_write_to_2048(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tcall %[bpf_get_prandom_u32];\t\t\t\\\n+\tr0 \u0026= 8;\t\t\t\t\t\\\n+\tr2 = r10;\t\t\t\t\t\\\n+\tr2 += -2048;\t\t\t\t\t\\\n+\tr2 += r0;\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+\tr0 = 0;\t\t\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm(bpf_get_prandom_u32)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"socket\")\n+__description(\"variable offset write reaching 2056 bytes deep\")\n+__load_if_large_stack()\n+__failure __msg(\"invalid variable-offset write to stack R2\")\n+__naked void var_off_write_to_2056(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tcall %[bpf_get_prandom_u32];\t\t\t\\\n+\tr0 \u0026= 8;\t\t\t\t\t\\\n+\tr2 = r10;\t\t\t\t\t\\\n+\tr2 += -2056;\t\t\t\t\t\\\n+\tr2 += r0;\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+\tr0 = 0;\t\t\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm(bpf_get_prandom_u32)\n+\t: __clobber_all);\n+}\n+\n+/* Each frame of a private stack gets the whole budget. */\n+__used __naked\n+static void priv_stack_frame_2048(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = 1;\t\t\t\t\t\t\\\n+\t*(u64 *)(r10 - 2048) = r1;\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n+SEC(\"kprobe\")\n+__description(\"private stack: two frames of 2048 bytes\")\n+__load_if_large_stack()\n+__arch_x86_64\n+__arch_arm64\n+__success __log_level(4)\n+__msg(\"stack depth max 2048\")\n+__msg(\"subprog 0 (private_stack_two_frames) main {{.*}} stack 2048\")\n+__msg(\"subprog 1 (priv_stack_frame_2048) static {{.*}} stack 2048\")\n+__naked void private_stack_two_frames(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = 2;\t\t\t\t\t\t\\\n+\t*(u64 *)(r10 - 2048) = r1;\t\t\t\\\n+\tcall priv_stack_frame_2048;\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_PROG_ARRAY);\n+\t__uint(max_entries, 1);\n+\t__uint(key_size, sizeof(__u32));\n+\t__uint(value_size, sizeof(__u32));\n+} jmp_table SEC(\".maps\");\n+\n+/*\n+ * A tail call unwinds the frame of the program doing it, so a large main\n+ * frame is fine; the 256-byte rule only concerns the frames of callers of a\n+ * subprog that tail calls.\n+ */\n+SEC(\"tc\")\n+__description(\"tail call from a 1 KiB frame\")\n+__load_if_large_stack()\n+__success\n+__naked void tail_call_from_large_frame(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr2 = 42;\t\t\t\t\t\\\n+\t*(u64 *)(r10 - 1024) = r2;\t\t\t\\\n+\tr2 = %[jmp_table] ll;\t\t\t\t\\\n+\tr3 = 0;\t\t\t\t\t\t\\\n+\tcall %[bpf_tail_call];\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_tail_call),\n+\t __imm_addr(jmp_table)\n+\t: __clobber_all);\n+}\n+\n+/*\n+ * bpf_clone_redirect() can run the program again on top of its own frame,\n+ * ten frames deep, so a program calling it keeps the 512-byte budget. The\n+ * redirect that happens after the program returns does not.\n+ */\n+SEC(\"tc\")\n+__description(\"1 KiB frame with bpf_clone_redirect keeps the 512-byte budget\")\n+__load_if_large_stack()\n+__failure __msg(\"invalid write to stack R1 off=-1024 size=8\")\n+__naked void clone_redirect_keeps_512(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr6 = r1;\t\t\t\t\t\\\n+\tr1 = r10;\t\t\t\t\t\\\n+\tr1 += -1024;\t\t\t\t\t\\\n+\tr0 = 42;\t\t\t\t\t\\\n+\t*(u64 *)(r1 + 0) = r0;\t\t\t\t\\\n+\tr1 = r6;\t\t\t\t\t\\\n+\tr2 = 1;\t\t\t\t\t\t\\\n+\tr3 = 0;\t\t\t\t\t\t\\\n+\tcall %[bpf_clone_redirect];\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_clone_redirect)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"tc\")\n+__description(\"1 KiB frame with bpf_redirect keeps the 2 KiB budget\")\n+__load_if_large_stack()\n+__success\n+__naked void redirect_keeps_2048(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = r10;\t\t\t\t\t\\\n+\tr1 += -1024;\t\t\t\t\t\\\n+\tr0 = 42;\t\t\t\t\t\\\n+\t*(u64 *)(r1 + 0) = r0;\t\t\t\t\\\n+\tr1 = 1;\t\t\t\t\t\t\\\n+\tr2 = 0;\t\t\t\t\t\t\\\n+\tcall %[bpf_redirect];\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm(bpf_redirect)\n+\t: __clobber_all);\n+}\n+\n+/* Global subprogs are verified on their own but share the call chain budget. */\n+__used __naked int global_frame_1536(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = 1;\t\t\t\t\t\t\\\n+\t*(u64 *)(r10 - 1536) = r1;\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n+SEC(\"socket\")\n+__description(\"512-byte frame calling a 1536-byte global subprog\")\n+__load_if_large_stack()\n+__success __log_level(4) __msg(\"stack depth max 2048\")\n+__naked void global_subprog_fits(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = 2;\t\t\t\t\t\t\\\n+\t*(u64 *)(r10 - 512) = r1;\t\t\t\\\n+\tcall global_frame_1536;\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n+SEC(\"socket\")\n+__description(\"520-byte frame calling a 1536-byte global subprog\")\n+__load_if_large_stack()\n+__failure __msg(\"combined stack size of 2 calls is 2064. Too large\")\n+__naked void global_subprog_exceeds(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = 2;\t\t\t\t\t\t\\\n+\t*(u64 *)(r10 - 520) = r1;\t\t\t\\\n+\tcall global_frame_1536;\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n+/* Callback frames are part of the chain of the helper that calls them. */\n+static __naked int loop_cb_1536(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = 1;\t\t\t\t\t\t\\\n+\t*(u64 *)(r10 - 1536) = r1;\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n+SEC(\"socket\")\n+__description(\"512-byte frame with a 1536-byte bpf_loop callback\")\n+__load_if_large_stack()\n+__success __log_level(4) __msg(\"stack depth max 2048\")\n+__naked void loop_callback_fits(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = 2;\t\t\t\t\t\t\\\n+\t*(u64 *)(r10 - 512) = r1;\t\t\t\\\n+\tr1 = 1;\t\t\t\t\t\t\\\n+\tr2 = %[loop_cb_1536];\t\t\t\t\\\n+\tr3 = 0;\t\t\t\t\t\t\\\n+\tr4 = 0;\t\t\t\t\t\t\\\n+\tcall %[bpf_loop];\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_ptr(loop_cb_1536),\n+\t __imm(bpf_loop)\n+\t: __clobber_common);\n+}\n+\n+SEC(\"socket\")\n+__description(\"520-byte frame with a 1536-byte bpf_loop callback\")\n+__load_if_large_stack()\n+__failure __msg(\"combined stack size of 2 calls is 2064. Too large\")\n+__naked void loop_callback_exceeds(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = 2;\t\t\t\t\t\t\\\n+\t*(u64 *)(r10 - 520) = r1;\t\t\t\\\n+\tr1 = 1;\t\t\t\t\t\t\\\n+\tr2 = %[loop_cb_1536];\t\t\t\t\\\n+\tr3 = 0;\t\t\t\t\t\t\\\n+\tr4 = 0;\t\t\t\t\t\t\\\n+\tcall %[bpf_loop];\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_ptr(loop_cb_1536),\n+\t __imm(bpf_loop)\n+\t: __clobber_common);\n+}\n+\n+char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/progs/verifier_live_stack.c b/tools/testing/selftests/bpf/progs/verifier_live_stack.c\nindex 7a1a0670f851d..16b2b1e575342 100644\n--- a/tools/testing/selftests/bpf/progs/verifier_live_stack.c\n+++ b/tools/testing/selftests/bpf/progs/verifier_live_stack.c\n@@ -318,7 +318,7 @@ struct {\n } map_array SEC(\".maps\");\n \n SEC(\"socket\")\n-__failure __msg(\"invalid read from stack R2 off=-1024 size=8\")\n+__failure __msg(\"invalid read from stack R2 off=-4096 size=8\")\n __flag(BPF_F_TEST_STATE_FREQ)\n __naked unsigned long caller_stack_write_tail_call(void)\n {\n@@ -329,7 +329,7 @@ __naked unsigned long caller_stack_write_tail_call(void)\n \"if r0 != 42 goto 1f;\"\n \"goto 2f;\"\n \"1:\"\n- \"*(u64 *)(r10 - 8) = -1024;\"\n+ \"*(u64 *)(r10 - 8) = -4096;\"\n \"2:\"\n \"r1 = r6;\"\n \"r2 = r10;\"\n@@ -1953,7 +1953,7 @@ static __used __naked void fwd_parent_key_to_helper(void)\n SEC(\"socket\")\n __log_level(2)\n __success\n-__msg(\"call bpf_map_update_elem{{.*}}; use: fp1-8..-512 fp0-8\")\n+__msg(\"call bpf_map_update_elem{{.*}}; use: fp1-8..-{{(512|2048)}} fp0-8\")\n __naked void helper_arg_fallback_keeps_scanning(void)\n {\n \tasm volatile (\n@@ -2267,7 +2267,7 @@ static __used __naked void merge_leaf_read(void)\n SEC(\"socket\")\n __log_level(2)\n __success\n-__msg(\"call bpf_loop#181 ; use: fp2-8..-512 fp1-8..-512 fp0-8..-512\")\n+__msg(\"call bpf_loop#181 ; use: fp2-8..-{{(512|2048)}} fp1-8..-{{(512|2048)}} fp0-8..-{{(512|2048)}}\")\n __naked void bpf_loop_two_callbacks(void)\n {\n \tasm volatile (\n@@ -2839,3 +2839,99 @@ static __used __naked void imprecise_dst_spill_join_sub(void)\n \t:: __imm(bpf_get_prandom_u32)\n \t: __clobber_all);\n }\n+\n+/*\n+ * A store that does not fully cover a 4-byte half-slot defines nothing, so a\n+ * narrow store at the top of the frame must not turn any slot into a \"def\",\n+ * least of all every slot of the frame: the earlier data at fp-8 stays live.\n+ */\n+SEC(\"socket\")\n+__log_level(2)\n+__msg(\"0: (79) r0 = *(u64 *)(r10 -8) ; use: fp0-8\")\n+__msg(\"1: (73) *(u8 *)(r10 -1) = r0{{$}}\")\n+__msg(\"2: (6b) *(u16 *)(r10 -4) = r0{{$}}\")\n+__msg(\"3: (79) r0 = *(u64 *)(r10 -8) ; use: fp0-8\")\n+__naked void narrow_store_defines_nothing(void)\n+{\n+\tasm volatile (\n+\t\"r0 = *(u64 *)(r10 - 8);\"\n+\t\"*(u8 *)(r10 - 1) = r0;\"\n+\t\"*(u16 *)(r10 - 4) = r0;\"\n+\t\"r0 = *(u64 *)(r10 - 8);\"\n+\t\"exit;\"\n+\t::: __clobber_all);\n+}\n+\n+/*\n+ * The same callee instance is analyzed twice: the call sites are visited in\n+ * postorder, so the second one goes first with a precise pointer 248 bytes\n+ * into the main frame, and the first one then passes a pointer of unknown\n+ * offset, which reads the whole frame. The precise pass stays within the\n+ * first word of the masks on 64-bit, so the whole-frame pass is wider under\n+ * every stack budget and merging the second into the first has to widen the\n+ * masks while keeping the whole-frame read.\n+ */\n+SEC(\"socket\")\n+__log_level(2)\n+__msg(\"stack use/def subprog#{{[0-9]+}} merge_read_all_callee (d2,cs{{[0-9]+}}):\")\n+__msg(\"(79) r0 = *(u64 *)(r1 +0){{.*}}; use: fp0-8..-{{(512|2048)}}\")\n+__naked void merge_keeps_whole_frame_read(void)\n+{\n+\tasm volatile (\n+\t\"r1 = 0;\"\n+\t\"*(u64 *)(r10 - 8) = r1;\"\n+\t\"*(u64 *)(r10 - 16) = r1;\"\n+\t\"*(u64 *)(r10 - 248) = r1;\"\n+\t\"call %[bpf_get_prandom_u32];\"\n+\t\"r0 \u0026= 8;\"\n+\t\"r1 = r10;\"\n+\t\"r1 += -16;\"\n+\t\"r1 += r0;\"\n+\t\"call merge_read_all_mid;\"\n+\t\"r1 = r10;\"\n+\t\"r1 += -248;\"\n+\t\"call merge_read_all_mid;\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\t:: __imm(bpf_get_prandom_u32)\n+\t: __clobber_all);\n+}\n+\n+static __used __naked void merge_read_all_mid(void)\n+{\n+\tasm volatile (\n+\t\"call merge_read_all_callee;\"\n+\t\"exit;\"\n+\t::: __clobber_all);\n+}\n+\n+static __used __naked void merge_read_all_callee(void)\n+{\n+\tasm volatile (\n+\t\"r0 = *(u64 *)(r1 + 0);\"\n+\t\"exit;\"\n+\t::: __clobber_all);\n+}\n+\n+/*\n+ * A frame pointer spilled below fp-512 is a spill like any other: the fill\n+ * restores its identity, and a load through it reads only the slot it names\n+ * instead of the whole frame.\n+ */\n+SEC(\"socket\")\n+__log_level(2)\n+__load_if_large_stack()\n+__msg(\"(79) r0 = *(u64 *)(r1 +0){{.*}}; use: fp0-8{{$}}\")\n+__naked void spill_below_512_stays_precise(void)\n+{\n+\tasm volatile (\n+\t\"r1 = 0;\"\n+\t\"*(u64 *)(r10 - 8) = r1;\"\n+\t\"r1 = r10;\"\n+\t\"r1 += -8;\"\n+\t\"*(u64 *)(r10 - 520) = r1;\"\n+\t\"r1 = *(u64 *)(r10 - 520);\"\n+\t\"r0 = *(u64 *)(r1 + 0);\"\n+\t\"exit;\"\n+\t::: __clobber_all);\n+}\ndiff --git a/tools/testing/selftests/bpf/progs/verifier_raw_stack.c b/tools/testing/selftests/bpf/progs/verifier_raw_stack.c\nindex 9f0f48ecb4216..0fe631411b9cc 100644\n--- a/tools/testing/selftests/bpf/progs/verifier_raw_stack.c\n+++ b/tools/testing/selftests/bpf/progs/verifier_raw_stack.c\n@@ -240,6 +240,7 @@ __naked void load_bytes_spilled_regs_data(void)\n \n SEC(\"tc\")\n __description(\"raw_stack: skb_load_bytes, invalid access 1\")\n+__load_if_no_large_stack()\n __failure __msg(\"invalid write to stack R3 off=-513 size=8\")\n __naked void load_bytes_invalid_access_1(void)\n {\n@@ -257,6 +258,26 @@ __naked void load_bytes_invalid_access_1(void)\n \t: __clobber_all);\n }\n \n+SEC(\"tc\")\n+__description(\"raw_stack: skb_load_bytes, invalid access 1, large stack\")\n+__load_if_large_stack()\n+__failure __msg(\"invalid write to stack R3 off=-2049 size=8\")\n+__naked void load_bytes_invalid_access_1_large(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr2 = 4;\t\t\t\t\t\t\\\n+\tr6 = r10;\t\t\t\t\t\\\n+\tr6 += -2049;\t\t\t\t\t\\\n+\tr3 = r6;\t\t\t\t\t\\\n+\tr4 = 8;\t\t\t\t\t\t\\\n+\tcall %[bpf_skb_load_bytes];\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(bpf_skb_load_bytes)\n+\t: __clobber_all);\n+}\n+\n SEC(\"tc\")\n __description(\"raw_stack: skb_load_bytes, invalid access 2\")\n __failure __msg(\"invalid write to stack R3 off=-1 size=8\")\ndiff --git a/tools/testing/selftests/bpf/progs/verifier_stack_ptr.c b/tools/testing/selftests/bpf/progs/verifier_stack_ptr.c\nindex 8e8cf8232255f..3e0bea9819cab 100644\n--- a/tools/testing/selftests/bpf/progs/verifier_stack_ptr.c\n+++ b/tools/testing/selftests/bpf/progs/verifier_stack_ptr.c\n@@ -235,6 +235,7 @@ __naked void to_stack_check_low_1(void)\n \n SEC(\"socket\")\n __description(\"PTR_TO_STACK check low 2\")\n+__load_if_no_large_stack()\n __success __failure_unpriv\n __msg_unpriv(\"R1 stack pointer arithmetic goes out of range\")\n __retval(42)\n@@ -250,8 +251,27 @@ __naked void to_stack_check_low_2(void)\n \"\t::: __clobber_all);\n }\n \n+SEC(\"socket\")\n+__description(\"PTR_TO_STACK check low 2, large stack\")\n+__load_if_large_stack()\n+__success __failure_unpriv\n+__msg_unpriv(\"R1 stack pointer arithmetic goes out of range\")\n+__retval(42)\n+__naked void to_stack_check_low_2_large(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = r10;\t\t\t\t\t\\\n+\tr1 += -2049;\t\t\t\t\t\\\n+\tr0 = 42;\t\t\t\t\t\\\n+\t*(u8*)(r1 + 1) = r0;\t\t\t\t\\\n+\tr0 = *(u8*)(r1 + 1);\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n SEC(\"socket\")\n __description(\"PTR_TO_STACK check low 3\")\n+__load_if_no_large_stack()\n __failure __msg(\"invalid write to stack R1 off=-513 size=1\")\n __msg_unpriv(\"R1 stack pointer arithmetic goes out of range\")\n __naked void to_stack_check_low_3(void)\n@@ -266,6 +286,23 @@ __naked void to_stack_check_low_3(void)\n \"\t::: __clobber_all);\n }\n \n+SEC(\"socket\")\n+__description(\"PTR_TO_STACK check low 3, large stack\")\n+__load_if_large_stack()\n+__failure __msg(\"invalid write to stack R1 off=-2049 size=1\")\n+__msg_unpriv(\"R1 stack pointer arithmetic goes out of range\")\n+__naked void to_stack_check_low_3_large(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = r10;\t\t\t\t\t\\\n+\tr1 += -2049;\t\t\t\t\t\\\n+\tr0 = 42;\t\t\t\t\t\\\n+\t*(u8*)(r1 + 0) = r0;\t\t\t\t\\\n+\tr0 = *(u8*)(r1 + 0);\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n SEC(\"socket\")\n __description(\"PTR_TO_STACK check low 4\")\n __failure __msg(\"math between fp pointer\")\n@@ -483,6 +520,7 @@ l1_%=:\tr0 = 42;\t\t\t\t\t\\\n \n SEC(\"socket\")\n __description(\"PTR_TO_STACK stack size \u003e 512\")\n+__load_if_no_large_stack()\n __failure __msg(\"invalid write to stack R1 off=-520 size=8\")\n __naked void stack_check_size_gt_512(void)\n {\n@@ -495,6 +533,21 @@ __naked void stack_check_size_gt_512(void)\n \"\t::: __clobber_all);\n }\n \n+SEC(\"socket\")\n+__description(\"PTR_TO_STACK stack size \u003e 2048\")\n+__load_if_large_stack()\n+__failure __msg(\"invalid write to stack R1 off=-2056 size=8\")\n+__naked void stack_check_size_gt_2048(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = r10;\t\t\t\t\t\\\n+\tr1 += -2056;\t\t\t\t\t\\\n+\tr0 = 42;\t\t\t\t\t\\\n+\t*(u64*)(r1 + 0) = r0;\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n #ifdef __BPF_FEATURE_MAY_GOTO\n SEC(\"socket\")\n __description(\"PTR_TO_STACK stack size 512 with may_goto with jit\")\ndiff --git a/tools/testing/selftests/bpf/progs/verifier_tailcall.c b/tools/testing/selftests/bpf/progs/verifier_tailcall.c\nindex b4acce60fb9b9..51687da972257 100644\n--- a/tools/testing/selftests/bpf/progs/verifier_tailcall.c\n+++ b/tools/testing/selftests/bpf/progs/verifier_tailcall.c\n@@ -28,4 +28,61 @@ __naked void invalid_map_for_tail_call(void)\n \t: __clobber_all);\n }\n \n+struct {\n+\t__uint(type, BPF_MAP_TYPE_PROG_ARRAY);\n+\t__uint(max_entries, 1);\n+\t__uint(key_size, sizeof(__u32));\n+\t__uint(value_size, sizeof(__u32));\n+} jmp_table SEC(\".maps\");\n+\n+__used __naked\n+static int subprog_tail_call(void)\n+{\n+\tasm volatile (\"\t\t\t\\\n+\tr2 = %[jmp_table] ll;\t\t\\\n+\tr3 = 0;\t\t\t\t\\\n+\tcall %[bpf_tail_call];\t\t\\\n+\tr0 = 0;\t\t\t\t\\\n+\texit;\t\t\t\t\\\n+\"\t:\n+\t: __imm(bpf_tail_call),\n+\t __imm_addr(jmp_table)\n+\t: __clobber_all);\n+}\n+\n+/*\n+ * A tail call unwinds only the frame of the subprog doing it, so the\n+ * frames of its callers stay on the stack. With up to 33 tail calls in\n+ * a chain the verifier caps the stack those frames may add up to at\n+ * 256 bytes.\n+ */\n+SEC(\"tc\")\n+__description(\"tail call from subprog with 240 bytes of caller stack\")\n+__success\n+__naked void tail_call_caller_stack_ok(void)\n+{\n+\tasm volatile (\"\t\t\t\\\n+\tr2 = 42;\t\t\t\\\n+\t*(u64 *)(r10 - 240) = r2;\t\\\n+\tcall subprog_tail_call;\t\t\\\n+\tr0 = 0;\t\t\t\t\\\n+\texit;\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n+SEC(\"tc\")\n+__description(\"tail call from subprog with 256 bytes of caller stack\")\n+__failure\n+__msg(\"tail_calls are not allowed when call stack of previous frames is 256 bytes. Too large\")\n+__naked void tail_call_caller_stack_too_large(void)\n+{\n+\tasm volatile (\"\t\t\t\\\n+\tr2 = 42;\t\t\t\\\n+\t*(u64 *)(r10 - 256) = r2;\t\\\n+\tcall subprog_tail_call;\t\t\\\n+\tr0 = 0;\t\t\t\t\\\n+\texit;\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/progs/verifier_var_off.c b/tools/testing/selftests/bpf/progs/verifier_var_off.c\nindex a63e336750918..399884911ea53 100644\n--- a/tools/testing/selftests/bpf/progs/verifier_var_off.c\n+++ b/tools/testing/selftests/bpf/progs/verifier_var_off.c\n@@ -406,6 +406,7 @@ __naked void zero_sized_access_max_out_of_bound(void)\n \n SEC(\"lwt_in\")\n __description(\"indirect variable-offset stack access, min out of bound\")\n+__load_if_no_large_stack()\n __failure __msg(\"invalid variable-offset read from stack R2\")\n __naked void access_min_out_of_bound(void)\n {\n@@ -433,6 +434,37 @@ __naked void access_min_out_of_bound(void)\n \t: __clobber_all);\n }\n \n+SEC(\"lwt_in\")\n+__description(\"indirect variable-offset stack access, min out of bound, large stack\")\n+__load_if_large_stack()\n+__failure __msg(\"invalid variable-offset read from stack R2\")\n+__naked void access_min_out_of_bound_large(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\t/* Fill the top 8 bytes of the stack */\t\t\\\n+\tr2 = 0;\t\t\t\t\t\t\\\n+\t*(u64*)(r10 - 8) = r2;\t\t\t\t\\\n+\t/* Get an unknown value */\t\t\t\\\n+\tr2 = *(u32*)(r1 + 0);\t\t\t\t\\\n+\t/* Make it small and 4-byte aligned */\t\t\\\n+\tr2 \u0026= 4;\t\t\t\t\t\\\n+\tr2 -= 2052;\t\t\t\t\t\\\n+\t/*\t\t\t\t\t\t\\\n+\t * add it to fp. We now have either fp-2052 or fp-2048, but\\\n+\t * we don't know which\t\t\t\t\\\n+\t */\t\t\t\t\t\t\\\n+\tr2 += r10;\t\t\t\t\t\\\n+\t/* dereference it indirectly */\t\t\t\\\n+\tr1 = %[map_hash_8b] ll;\t\t\t\t\\\n+\tcall %[bpf_map_lookup_elem];\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_8b)\n+\t: __clobber_all);\n+}\n+\n SEC(\"cgroup/skb\")\n __description(\"indirect variable-offset stack access, min_off \u003c min_initialized\")\n __success\ndiff --git a/tools/testing/selftests/bpf/test_loader.c b/tools/testing/selftests/bpf/test_loader.c\nindex a6e3fcc1079c6..25eeb1c1248b3 100644\n--- a/tools/testing/selftests/bpf/test_loader.c\n+++ b/tools/testing/selftests/bpf/test_loader.c\n@@ -45,6 +45,11 @@ enum load_mode {\n \tNO_JITED\t= 1 \u003c\u003c 1,\n };\n \n+enum stack_mode {\n+\tLARGE_STACK\t= 1 \u003c\u003c 0,\n+\tSMALL_STACK\t= 1 \u003c\u003c 1,\n+};\n+\n struct test_subspec {\n \tchar *name;\n \tchar *description;\n@@ -70,6 +75,7 @@ struct test_spec {\n \tint mode_mask;\n \tint arch_mask;\n \tint load_mask;\n+\tint stack_mask;\n \tint linear_sz;\n \tconst char *skip_reason;\n \tbool prepare_priv;\n@@ -425,6 +431,7 @@ static int parse_test_spec(struct test_loader *tester,\n \tint err = 0;\n \tu32 arch_mask = 0;\n \tu32 load_mask = 0;\n+\tu32 stack_mask = 0;\n \tstruct btf *btf;\n \tenum arch arch;\n \n@@ -620,6 +627,16 @@ static int parse_test_spec(struct test_loader *tester,\n \t\t\t\terr = -EINVAL;\n \t\t\t\tgoto cleanup;\n \t\t\t}\n+\t\t} else if ((val = str_has_pfx(s, \"stack_mode=\"))) {\n+\t\t\tif (strcmp(val, \"large\") == 0) {\n+\t\t\t\tstack_mask = LARGE_STACK;\n+\t\t\t} else if (strcmp(val, \"small\") == 0) {\n+\t\t\t\tstack_mask = SMALL_STACK;\n+\t\t\t} else {\n+\t\t\t\tPRINT_FAIL(\"bad stack spec: '%s'\", val);\n+\t\t\t\terr = -EINVAL;\n+\t\t\t\tgoto cleanup;\n+\t\t\t}\n \t\t} else if ((msg = str_has_pfx(s, \"test_expect_stderr=\"))) {\n \t\t\terr = push_disasm_msg(msg, \u0026stderr_on_next_line,\n \t\t\t\t\t \u0026spec-\u003epriv.stderr);\n@@ -659,6 +676,7 @@ static int parse_test_spec(struct test_loader *tester,\n \n \tspec-\u003earch_mask = arch_mask ?: -1;\n \tspec-\u003eload_mask = load_mask ?: (JITED | NO_JITED);\n+\tspec-\u003estack_mask = stack_mask ?: (LARGE_STACK | SMALL_STACK);\n \n \tif (spec-\u003emode_mask == 0)\n \t\tspec-\u003emode_mask = PRIV;\n@@ -1331,6 +1349,7 @@ void run_subtest(struct test_loader *tester,\n {\n \tstruct test_subspec *subspec = unpriv ? \u0026spec-\u003eunpriv : \u0026spec-\u003epriv;\n \tint current_runtime = is_jit_enabled() ? JITED : NO_JITED;\n+\tint current_stack = is_large_stack_supported() ? LARGE_STACK : SMALL_STACK;\n \tstruct bpf_program *tprog = NULL, *tprog_iter;\n \tstruct bpf_link *link, *links[32] = {};\n \tstruct test_spec *spec_iter;\n@@ -1360,6 +1379,11 @@ void run_subtest(struct test_loader *tester,\n \t\treturn;\n \t}\n \n+\tif ((current_stack \u0026 spec-\u003estack_mask) == 0) {\n+\t\ttest__skip();\n+\t\treturn;\n+\t}\n+\n \tif (unpriv) {\n \t\tif (!can_execute_unpriv(tester, spec)) {\n \t\t\ttest__skip();\ndiff --git a/tools/testing/selftests/bpf/testing_helpers.c b/tools/testing/selftests/bpf/testing_helpers.c\nindex d1d60451c5bcc..47fe61a1ebff1 100644\n--- a/tools/testing/selftests/bpf/testing_helpers.c\n+++ b/tools/testing/selftests/bpf/testing_helpers.c\n@@ -517,6 +517,47 @@ bool is_jit_enabled(void)\n \treturn enabled;\n }\n \n+/*\n+ * Whether the kernel accepts a program using more than 512 bytes of stack,\n+ * which depends on the JIT in use. Probed once with a program that stores\n+ * at the 2 KiB depth. Only the verifier's verdict on that store is cached:\n+ * a load that fails for another reason, such as a missing capability, is\n+ * reported and probed again on the next call.\n+ */\n+bool is_large_stack_supported(void)\n+{\n+\tstatic int supported = -1;\n+\tstruct bpf_insn insns[] = {\n+\t\tBPF_ST_MEM(BPF_DW, BPF_REG_10, -2048, 0),\n+\t\tBPF_MOV64_IMM(BPF_REG_0, 0),\n+\t\tBPF_EXIT_INSN(),\n+\t};\n+\tchar log[1024] = {};\n+\tLIBBPF_OPTS(bpf_prog_load_opts, opts,\n+\t\t.log_buf = log,\n+\t\t.log_size = sizeof(log),\n+\t\t.log_level = 1,\n+\t);\n+\tint fd;\n+\n+\tif (supported \u003e= 0)\n+\t\treturn supported;\n+\n+\tfd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, \"GPL\", insns, ARRAY_SIZE(insns),\n+\t\t\t \u0026opts);\n+\tif (fd \u003e= 0) {\n+\t\tclose(fd);\n+\t\tsupported = 1;\n+\t} else if (strstr(log, \"invalid write to stack\")) {\n+\t\tsupported = 0;\n+\t} else {\n+\t\tfprintf(stderr, \"%s: probe failed with errno %d, assuming 512 bytes:\\n%s\",\n+\t\t\t__func__, errno, log);\n+\t\treturn false;\n+\t}\n+\treturn supported;\n+}\n+\n int stack_mprotect(void)\n {\n \tvoid *buf;\ndiff --git a/tools/testing/selftests/bpf/testing_helpers.h b/tools/testing/selftests/bpf/testing_helpers.h\nindex 1c58a2f08b645..f1505108e26a4 100644\n--- a/tools/testing/selftests/bpf/testing_helpers.h\n+++ b/tools/testing/selftests/bpf/testing_helpers.h\n@@ -59,6 +59,7 @@ struct bpf_insn;\n int get_xlated_program(int fd_prog, struct bpf_insn **buf, __u32 *cnt);\n int testing_prog_flags(void);\n bool is_jit_enabled(void);\n+bool is_large_stack_supported(void);\n int stack_mprotect(void);\n \n /* Runs diff(1) on mismatch */\ndiff --git a/tools/testing/selftests/bpf/verifier/calls.c b/tools/testing/selftests/bpf/verifier/calls.c\nindex 8b94b87135bcf..0af237c02ddf9 100644\n--- a/tools/testing/selftests/bpf/verifier/calls.c\n+++ b/tools/testing/selftests/bpf/verifier/calls.c\n@@ -1037,15 +1037,34 @@\n \t.result = ACCEPT,\n },\n {\n-\t\"calls: stack overflow using two frames (pre-call access)\",\n+\t/*\n+\t * Five 480-byte frames exceed the 2 KiB budget of JITs with large\n+\t * stacks, and two of them the 512 bytes allowed elsewhere.\n+\t */\n+\t\"calls: stack overflow using five frames (pre-call access)\",\n \t.insns = {\n \t/* prog 1 */\n-\tBPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),\n+\tBPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),\n \tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1),\n \tBPF_EXIT_INSN(),\n \n \t/* prog 2 */\n-\tBPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),\n+\tBPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),\n+\tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1),\n+\tBPF_EXIT_INSN(),\n+\n+\t/* prog 3 */\n+\tBPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),\n+\tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1),\n+\tBPF_EXIT_INSN(),\n+\n+\t/* prog 4 */\n+\tBPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),\n+\tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1),\n+\tBPF_EXIT_INSN(),\n+\n+\t/* prog 5 */\n+\tBPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),\n \tBPF_MOV64_IMM(BPF_REG_0, 0),\n \tBPF_EXIT_INSN(),\n \t},\n@@ -1054,15 +1073,30 @@\n \t.result = REJECT,\n },\n {\n-\t\"calls: stack overflow using two frames (post-call access)\",\n+\t\"calls: stack overflow using five frames (post-call access)\",\n \t.insns = {\n \t/* prog 1 */\n \tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 2),\n-\tBPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),\n+\tBPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),\n \tBPF_EXIT_INSN(),\n \n \t/* prog 2 */\n-\tBPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),\n+\tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 2),\n+\tBPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),\n+\tBPF_EXIT_INSN(),\n+\n+\t/* prog 3 */\n+\tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 2),\n+\tBPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),\n+\tBPF_EXIT_INSN(),\n+\n+\t/* prog 4 */\n+\tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 2),\n+\tBPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),\n+\tBPF_EXIT_INSN(),\n+\n+\t/* prog 5 */\n+\tBPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),\n \tBPF_MOV64_IMM(BPF_REG_0, 0),\n \tBPF_EXIT_INSN(),\n \t},\n@@ -1127,7 +1161,7 @@\n \t.result = ACCEPT,\n },\n {\n-\t\"calls: stack depth check using three frames. test3\",\n+\t\"calls: stack depth check using five frames. test3\",\n \t.insns = {\n \t/* main */\n \tBPF_MOV64_REG(BPF_REG_6, BPF_REG_1),\n@@ -1135,66 +1169,104 @@\n \tBPF_MOV64_REG(BPF_REG_1, BPF_REG_6),\n \tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 8), /* call B */\n \tBPF_JMP_IMM(BPF_JGE, BPF_REG_6, 0, 1),\n-\tBPF_ST_MEM(BPF_B, BPF_REG_10, -64, 0),\n+\tBPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),\n \tBPF_MOV64_IMM(BPF_REG_0, 0),\n \tBPF_EXIT_INSN(),\n \t/* A */\n \tBPF_JMP_IMM(BPF_JLT, BPF_REG_1, 10, 1),\n \tBPF_EXIT_INSN(),\n-\tBPF_ST_MEM(BPF_B, BPF_REG_10, -224, 0),\n+\tBPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),\n \tBPF_JMP_IMM(BPF_JA, 0, 0, -3),\n \t/* B */\n \tBPF_JMP_IMM(BPF_JGT, BPF_REG_1, 2, 1),\n-\tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, -6), /* call A */\n-\tBPF_ST_MEM(BPF_B, BPF_REG_10, -256, 0),\n+\tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 2), /* call C */\n+\tBPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),\n+\tBPF_EXIT_INSN(),\n+\t/* C */\n+\tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 2), /* call D */\n+\tBPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),\n+\tBPF_EXIT_INSN(),\n+\t/* D */\n+\tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, -12), /* call A */\n+\tBPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),\n \tBPF_EXIT_INSN(),\n \t},\n \t.prog_type = BPF_PROG_TYPE_XDP,\n-\t/* stack_main=64, stack_A=224, stack_B=256\n-\t * and max(main+A, main+A+B) \u003e 512\n+\t/*\n+\t * every frame is 480 bytes, main+A = 960 \u003e 512 and\n+\t * max(main+A, main+B+C+D+A) = 2400 \u003e 2048\n \t */\n \t.errstr = \"combined stack\",\n \t.result = REJECT,\n },\n {\n-\t\"calls: stack depth check using three frames. test4\",\n-\t/* void main(void) {\n+\t\"calls: stack depth check using five frames. test4\",\n+\t/*\n+\t * void main(void) {\n \t * func1(0);\n \t * func1(1);\n \t * func2(1);\n \t * }\n-\t * void func1(int alloc_or_recurse) {\n+\t * void funcN(int alloc_or_recurse) { N = 1..4\n \t * if (alloc_or_recurse) {\n-\t * frame_pointer[-300] = 1;\n+\t * frame_pointer[-480] = 1;\n \t * } else {\n-\t * func2(alloc_or_recurse);\n+\t * funcN+1(alloc_or_recurse);\n \t * }\n \t * }\n-\t * void func2(int alloc_or_recurse) {\n+\t * void func5(int alloc_or_recurse) {\n \t * if (alloc_or_recurse) {\n-\t * frame_pointer[-300] = 1;\n+\t * frame_pointer[-480] = 1;\n \t * }\n \t * }\n+\t * main also calls func2 to func5 with 1 so that every function has a\n+\t * path allocating its 480 bytes, and the chain adds up to 2400 bytes,\n+\t * more than the 2 KiB budget of JITs with large stacks, and to 960\n+\t * bytes after two frames, more than the 512 bytes allowed elsewhere.\n \t */\n \t.insns = {\n \t/* main */\n \tBPF_MOV64_IMM(BPF_REG_1, 0),\n-\tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 6), /* call A */\n+\tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 12), /* call A */\n \tBPF_MOV64_IMM(BPF_REG_1, 1),\n-\tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 4), /* call A */\n+\tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 10), /* call A */\n+\tBPF_MOV64_IMM(BPF_REG_1, 1),\n+\tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 13), /* call B */\n+\tBPF_MOV64_IMM(BPF_REG_1, 1),\n+\tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 16), /* call C */\n \tBPF_MOV64_IMM(BPF_REG_1, 1),\n-\tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 7), /* call B */\n+\tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 19), /* call D */\n+\tBPF_MOV64_IMM(BPF_REG_1, 1),\n+\tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 22), /* call E */\n \tBPF_MOV64_IMM(BPF_REG_0, 0),\n \tBPF_EXIT_INSN(),\n \t/* A */\n \tBPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 2),\n-\tBPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),\n+\tBPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),\n \tBPF_EXIT_INSN(),\n \tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call B */\n \tBPF_EXIT_INSN(),\n \t/* B */\n+\tBPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 2),\n+\tBPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),\n+\tBPF_EXIT_INSN(),\n+\tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call C */\n+\tBPF_EXIT_INSN(),\n+\t/* C */\n+\tBPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 2),\n+\tBPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),\n+\tBPF_EXIT_INSN(),\n+\tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call D */\n+\tBPF_EXIT_INSN(),\n+\t/* D */\n+\tBPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 2),\n+\tBPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),\n+\tBPF_EXIT_INSN(),\n+\tBPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call E */\n+\tBPF_EXIT_INSN(),\n+\t/* E */\n \tBPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 1),\n-\tBPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),\n+\tBPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),\n \tBPF_EXIT_INSN(),\n \t},\n \t.prog_type = BPF_PROG_TYPE_XDP,\n"
} |
| 2/1 | 2026/09/26 22:10 | agent | patch-evaluator |
1mModel:core Args: null Results: {
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT"
],
"FocusSymbols": [
"bpf_jit_supports_large_stack",
"bpf_id_scratch_reserve",
"bpf_push_jmp_history",
"bpf_mark_chain_precision",
"bpf_compute_subprog_arg_access",
"check_max_stack_depth_subprog",
"check_stack_write_fixed_off",
"check_stack_read_fixed_off",
"stacksafe"
],
"Reasoning": "The patch increases the maximum BPF program stack size from 512 bytes to 2048 bytes (MAX_BPF_STACK_JIT) on architectures with JIT support (including x86-64). This introduces widespread alterations across core BPF subsystem files (verifier.c, liveness.c, backtrack.c, states.c) to handle larger stack limits: changing fixed 64-bit stack masks to multi-word bitmaps, dynamically widening frame masks in liveness tracking, repacking jump history entries for linked registers and slots, dynamically allocating scratch tables for verifier state equivalence checks, and adjusting stack depth checks and pointer arithmetic bounds. These changes modify critical security invariants and verifier state pruning logic reachable from unprivileged/privileged BPF program loading.",
"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 6d73c2266fed3339a271d57f99edbf7dc11e3ac3
Author: syz-cluster <triage@syzkaller.com>
Date: Sat Sep 26 22:10:37 2026 +0000
syz-cluster: applied patch under review
diff --git a/Documentation/bpf/bpf_design_QA.rst b/Documentation/bpf/bpf_design_QA.rst
index eb19c945f4d57..98d70b8a2533b 100644
--- a/Documentation/bpf/bpf_design_QA.rst
+++ b/Documentation/bpf/bpf_design_QA.rst
@@ -221,9 +221,14 @@ newer kernels. BPF programs need to change accordingly when this happens.
Q: How much stack space a BPF program uses?
-------------------------------------------
-A: Currently all program types are limited to 512 bytes of stack
-space, but the verifier computes the actual amount of stack used
-and both interpreter and most JITed code consume necessary amount.
+A: A program may use up to 2 KiB of stack, combined over its call
+chain, when the JIT of the architecture reports support for large
+stacks (currently x86-64 and arm64); a single function may use all of
+it, and every frame of a program running on a private stack gets the
+whole amount. Elsewhere, and for programs verified for the interpreter,
+the limit is 512 bytes; the interpreter itself never runs a frame
+larger than that. The verifier computes the actual amount of stack
+used and both interpreter and most JITed code consume necessary amount.
Q: Can BPF be offloaded to HW?
------------------------------
diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
index 9544b2f483e59..2b2d6d178ea1d 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -2501,6 +2501,11 @@ bool bpf_jit_supports_callx(void)
return true;
}
+bool bpf_jit_supports_large_stack(void)
+{
+ return true;
+}
+
static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_node *node,
int bargs_off, int retval_off, int run_ctx_off,
bool save_ret)
diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
index 9fbef7504e51a..e2e531dd1e0bc 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -4520,6 +4520,18 @@ bool bpf_jit_supports_subprog_tailcalls(void)
return true;
}
+/*
+ * Frame sizes are 32-bit immediates in the prologue, epilogue and tail call
+ * sequences, a tail call pops the caller's frame and lands in the target's
+ * prologue before the target allocates its own, and private stacks are
+ * allocated from the program's own depth, so MAX_BPF_STACK_JIT frames need
+ * nothing special.
+ */
+bool bpf_jit_supports_large_stack(void)
+{
+ return true;
+}
+
bool bpf_jit_supports_percpu_insn(void)
{
return true;
diff --git a/drivers/net/ethernet/netronome/nfp/bpf/verifier.c b/drivers/net/ethernet/netronome/nfp/bpf/verifier.c
index 1caa87da72b51..9ddbfe9d60194 100644
--- a/drivers/net/ethernet/netronome/nfp/bpf/verifier.c
+++ b/drivers/net/ethernet/netronome/nfp/bpf/verifier.c
@@ -105,7 +105,7 @@ static bool nfp_bpf_map_update_value_ok(struct bpf_verifier_env *env)
unsigned int soff;
soff = -(off + i) - 1;
- stack_entry = &state->stack[soff / BPF_REG_SIZE];
+ stack_entry = bpf_stack_slot(state, soff / BPF_REG_SIZE);
if (stack_entry->slot_type[soff % BPF_REG_SIZE] == STACK_ZERO)
continue;
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 578bccf5cf767..f151feeaf3a55 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -19,11 +19,12 @@
* that converting umax_value to int cannot overflow.
*/
#define BPF_MAX_VAR_SIZ (1 << 29)
-/* size of tmp_str_buf in bpf_verifier.
- * we need at least 306 bytes to fit full stack mask representation
- * (in the "-8,-16,...,-512" form)
+/*
+ * size of tmp_str_buf in bpf_verifier.
+ * we need at least 1399 bytes to fit full stack mask representation
+ * (in the "-8,-16,...,-2048" form)
*/
-#define TMP_STR_BUF_LEN 320
+#define TMP_STR_BUF_LEN 1408
/* Patch buffer size */
#define INSN_BUF_SIZE 32
@@ -242,55 +243,18 @@ enum bpf_stack_slot_type {
#define BPF_REG_SIZE 8 /* size of eBPF register in bytes */
+/*
+ * Largest number of BPF_REG_SIZE stack slots a single frame can have, sized
+ * for the largest stack budget any JIT supports. A frame may use any part of
+ * its program's budget; check_max_stack_depth() enforces the budget on the
+ * combined depth of frames sharing the kernel stack and on each frame using
+ * a private stack.
+ */
+#define MAX_BPF_STACK_SLOTS (MAX_BPF_STACK_JIT / BPF_REG_SIZE)
+
/* 4-byte stack slot granularity for liveness analysis */
#define BPF_HALF_REG_SIZE 4
#define STACK_SLOT_SZ 4
-#define STACK_SLOTS (MAX_BPF_STACK / BPF_HALF_REG_SIZE) /* 128 */
-
-typedef struct {
- u64 v[2];
-} spis_t;
-
-#define SPIS_ZERO ((spis_t){})
-#define SPIS_ALL ((spis_t){{ U64_MAX, U64_MAX }})
-
-static inline bool spis_is_zero(spis_t s)
-{
- return s.v[0] == 0 && s.v[1] == 0;
-}
-
-static inline bool spis_equal(spis_t a, spis_t b)
-{
- return a.v[0] == b.v[0] && a.v[1] == b.v[1];
-}
-
-static inline spis_t spis_or(spis_t a, spis_t b)
-{
- return (spis_t){{ a.v[0] | b.v[0], a.v[1] | b.v[1] }};
-}
-
-static inline spis_t spis_and(spis_t a, spis_t b)
-{
- return (spis_t){{ a.v[0] & b.v[0], a.v[1] & b.v[1] }};
-}
-
-static inline spis_t spis_not(spis_t s)
-{
- return (spis_t){{ ~s.v[0], ~s.v[1] }};
-}
-
-static inline bool spis_test_bit(spis_t s, u32 slot)
-{
- return s.v[slot / 64] & BIT_ULL(slot % 64);
-}
-
-static inline void spis_or_range(spis_t *mask, u32 lo, u32 hi)
-{
- u32 w;
-
- for (w = lo; w <= hi && w < STACK_SLOTS; w++)
- mask->v[w / 64] |= BIT_ULL(w % 64);
-}
#define BPF_REGMASK_ARGS ((1 << BPF_REG_1) | (1 << BPF_REG_2) | \
(1 << BPF_REG_3) | (1 << BPF_REG_4) | \
@@ -420,30 +384,33 @@ enum {
INSN_F_STACK_ARG_ACCESS = BIT(3),
};
+/* Registers linked to one jump condition that a history entry can record */
+#define BPF_LINKED_REGS_MAX 5
+
struct bpf_jmp_history_entry {
/* insn idx can't be bigger than 1 million */
u32 idx : 20;
u32 frame : 4; /* stack access frame number */
- u32 spi : 6; /* stack slot index (0..63) */
- u32 : 2;
- u32 prev_idx : 20;
/* special INSN_F_xxx flags */
u32 flags : 4;
- u32 : 8;
+ u32 : 4;
+ u32 prev_idx : 20;
+ u32 spi : 12; /* stack slot index */
/*
- * additional registers that need precision tracking when this
- * jump is backtracked, vector of five 11-bit records
+ * Scalar registers and spilled scalars linked to the condition of
+ * this jump, which need precision tracking together when the jump is
+ * backtracked. Each is packed as 4 bits of frame number, one bit
+ * telling a register from a stack slot and 11 bits of register or
+ * slot index, see linked_regs_pack().
*/
- u64 linked_regs;
+ u16 linked_regs[BPF_LINKED_REGS_MAX];
+ u8 linked_regs_cnt;
};
static_assert(MAX_CALL_FRAMES <= (1 << 4));
-static_assert(MAX_BPF_STACK / 8 <= (1 << 6));
+static_assert(MAX_BPF_STACK_SLOTS <= (1 << 12));
-/* Maximum number of bpf_reg_state objects that can exist at once */
#define MAX_STACK_ARG_SLOTS (MAX_BPF_FUNC_ARGS - MAX_BPF_FUNC_REG_ARGS)
-#define BPF_ID_MAP_SIZE ((MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE + \
- MAX_STACK_ARG_SLOTS) * MAX_CALL_FRAMES)
struct bpf_verifier_state {
/* call stack tracking */
struct bpf_func_state *frame[MAX_CALL_FRAMES];
@@ -529,12 +496,31 @@ struct bpf_verifier_state {
u32 may_goto_depth;
};
+/* Number of BPF_REG_SIZE stack slots tracked for the frame so far. */
+static inline u32 bpf_stack_nr_slots(const struct bpf_func_state *frame)
+{
+ return frame->allocated_stack / BPF_REG_SIZE;
+}
+
+/*
+ * Stack slot @spi of @frame, covering bytes [fp - (spi + 1) * 8, fp - spi * 8).
+ * The caller must ensure spi < bpf_stack_nr_slots(frame), see grow_stack_state().
+ */
+static inline struct bpf_stack_state *bpf_stack_slot(const struct bpf_func_state *frame, u32 spi)
+{
+ return &frame->stack[spi];
+}
+
static inline struct bpf_reg_state *
bpf_get_spilled_reg(int slot, struct bpf_func_state *frame, u32 mask)
{
- if (slot < frame->allocated_stack / BPF_REG_SIZE &&
- (1 << frame->stack[slot].slot_type[BPF_REG_SIZE - 1]) & mask)
- return &frame->stack[slot].spilled_ptr;
+ struct bpf_stack_state *ss;
+
+ if (slot >= bpf_stack_nr_slots(frame))
+ return NULL;
+ ss = bpf_stack_slot(frame, slot);
+ if ((1 << ss->slot_type[BPF_REG_SIZE - 1]) & mask)
+ return &ss->spilled_ptr;
return NULL;
}
@@ -550,7 +536,7 @@ bpf_get_spilled_stack_arg(int slot, struct bpf_func_state *frame)
/* Iterate over 'frame', setting 'reg' to either NULL or a spilled register. */
#define bpf_for_each_spilled_reg(iter, frame, reg, mask) \
for (iter = 0, reg = bpf_get_spilled_reg(iter, frame, mask); \
- iter < frame->allocated_stack / BPF_REG_SIZE; \
+ iter < bpf_stack_nr_slots(frame); \
iter++, reg = bpf_get_spilled_reg(iter, frame, mask))
/* Iterate over 'frame', setting 'reg' to either NULL or a spilled stack arg. */
@@ -575,7 +561,7 @@ bpf_get_spilled_stack_arg(int slot, struct bpf_func_state *frame)
bpf_for_each_spilled_reg(___j, __state, __reg, __mask) { \
if (!__reg) \
continue; \
- __stack = &__state->stack[___j]; \
+ __stack = bpf_stack_slot(__state, ___j); \
(void)(__expr); \
} \
__stack = NULL; \
@@ -733,7 +719,11 @@ struct bpf_insn_aux_data {
#define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */
#define MAX_USED_BTFS 64 /* max number of BTFs accessed by one BPF program */
-#define BPF_VERIFIER_TMP_LOG_SIZE 1024
+/*
+ * Longest line the verifier log can carry: a full stack mask of
+ * MAX_BPF_STACK_SLOTS slots, see TMP_STR_BUF_LEN, plus its prefix.
+ */
+#define BPF_VERIFIER_TMP_LOG_SIZE 2048
struct bpf_verifier_log {
/* Logical start and end positions of a "log window" of the verifier log.
@@ -865,7 +855,7 @@ struct backtrack_state {
struct bpf_verifier_env *env;
u32 frame;
u32 reg_masks[MAX_CALL_FRAMES];
- u64 stack_masks[MAX_CALL_FRAMES];
+ unsigned long stack_masks[MAX_CALL_FRAMES][BITS_TO_LONGS(MAX_BPF_STACK_SLOTS)];
u8 stack_arg_masks[MAX_CALL_FRAMES];
};
@@ -874,18 +864,27 @@ struct bpf_id_pair {
u32 cur;
};
+/*
+ * Scratch map from the ids of one verifier state to those of another, also
+ * used as a stack of ids. Grown on demand by bpf_id_scratch_reserve().
+ */
struct bpf_idmap {
u32 tmp_id_gen;
u32 cnt;
- struct bpf_id_pair map[BPF_ID_MAP_SIZE];
+ u32 cap;
+ struct bpf_id_pair *map;
};
+struct bpf_idset_entry {
+ u32 id;
+ u32 cnt;
+};
+
+/* Scratch set of ids with a use count each, grown on demand */
struct bpf_idset {
u32 num_ids;
- struct {
- u32 id;
- u32 cnt;
- } entries[BPF_ID_MAP_SIZE];
+ u32 cap;
+ struct bpf_idset_entry *entries;
};
/* see verifier.c:compute_scc_callchain() */
@@ -994,10 +993,8 @@ struct bpf_verifier_env {
* via callx. Allocated when the first such edge is recorded.
*/
unsigned long *callx_edges;
- union {
- struct bpf_idmap idmap_scratch;
- struct bpf_idset idset_scratch;
- };
+ struct bpf_idmap idmap_scratch;
+ struct bpf_idset idset_scratch;
struct {
int *insn_state;
int *insn_stack;
@@ -1012,8 +1009,8 @@ struct bpf_verifier_env {
} cfg;
struct backtrack_state bt;
struct bpf_jmp_history_entry *cur_hist_ent;
- /* Per-callsite copy of parent's converged at_stack_in for cross-frame fills. */
- struct arg_track **callsite_at_stack;
+ /* Per-callsite snapshot of the parent's tracked spill slots for cross-frame fills. */
+ struct spill_snapshot **callsite_at_stack;
u32 pass_cnt; /* number of times do_check() was called */
u32 subprog_cnt;
/* number of instructions analyzed by the verifier */
@@ -1022,6 +1019,8 @@ struct bpf_verifier_env {
u32 prev_jmps_processed, jmps_processed;
/* maximum combined stack depth */
u32 max_stack_depth;
+ /* stack budget of the program, see bpf_prog_stack_limit() */
+ u32 stack_limit;
/* total verification time */
u64 verification_time;
/* maximum number of verifier states kept in 'branching' instructions */
@@ -1057,7 +1056,7 @@ struct bpf_verifier_env {
*/
u32 scratched_regs;
/* Same as scratched_regs but for stack slots */
- u64 scratched_stack_slots;
+ DECLARE_BITMAP(scratched_stack_slots, MAX_BPF_STACK_SLOTS);
u64 prev_log_pos, prev_insn_print_pos;
/* buffer used to temporary hold constants as scalar registers */
struct bpf_reg_state fake_reg[1];
@@ -1261,8 +1260,10 @@ int bpf_copy_verifier_state(struct bpf_verifier_state *dst_state,
struct list_head *bpf_explored_state(struct bpf_verifier_env *env, int idx);
void bpf_free_verifier_state(struct bpf_verifier_state *state, bool free_self);
void bpf_free_backedges(struct bpf_scc_visit *visit);
+bool bpf_id_scratch_reserve(void **arr, u32 *cap, u32 cnt, size_t elem_size);
int bpf_push_jmp_history(struct bpf_verifier_env *env, struct bpf_verifier_state *cur,
- int insn_flags, int spi, int frame, u64 linked_regs);
+ int insn_flags, int spi, int frame, const u16 *linked_regs,
+ u8 linked_regs_cnt);
void bpf_bt_sync_linked_regs(struct backtrack_state *bt, struct bpf_jmp_history_entry *hist);
void bpf_mark_reg_not_init(const struct bpf_verifier_env *env,
struct bpf_reg_state *reg);
@@ -1279,6 +1280,24 @@ static inline int bpf_get_spi(s32 off)
return (-off - 1) / BPF_REG_SIZE;
}
+/*
+ * Stack a program may use in total: combined over the frames of a call
+ * chain on the kernel stack, or per frame on a private stack. Any single
+ * frame may reach that deep. Only a JIT that lays out such frames may go
+ * beyond MAX_BPF_STACK, the interpreter's frame size, and only one whose
+ * tail calls let the target set up its own frame: without subprogram
+ * tail calls, do_misc_fixups() gives every program with tail calls a
+ * MAX_BPF_STACK frame, which a deeper frame would overrun.
+ */
+static inline u32 bpf_prog_stack_limit(const struct bpf_prog *prog)
+{
+ /* an offloaded program never runs on the host JIT, whatever it supports */
+ if (prog->jit_requested && !bpf_prog_is_offloaded(prog->aux) &&
+ bpf_jit_supports_large_stack() && bpf_jit_supports_subprog_tailcalls())
+ return MAX_BPF_STACK_JIT;
+ return MAX_BPF_STACK;
+}
+
static inline struct bpf_func_state *bpf_func(struct bpf_verifier_env *env,
const struct bpf_reg_state *reg)
{
@@ -1322,12 +1341,7 @@ static inline void bpf_bt_set_frame_reg(struct backtrack_state *bt, u32 frame, u
static inline void bpf_bt_set_frame_slot(struct backtrack_state *bt, u32 frame, u32 slot)
{
- bt->stack_masks[frame] |= 1ull << slot;
-}
-
-static inline void bpf_bt_set_frame_slot_mask(struct backtrack_state *bt, u32 frame, u64 mask)
-{
- bt->stack_masks[frame] |= mask;
+ __set_bit(slot, bt->stack_masks[frame]);
}
static inline void bt_set_frame_stack_arg_slot(struct backtrack_state *bt, u32 frame, u32 slot)
@@ -1342,7 +1356,7 @@ static inline bool bt_is_frame_reg_set(struct backtrack_state *bt, u32 frame, u3
static inline bool bt_is_frame_slot_set(struct backtrack_state *bt, u32 frame, u32 slot)
{
- return bt->stack_masks[frame] & (1ull << slot);
+ return test_bit(slot, bt->stack_masks[frame]);
}
bool bpf_map_is_rdonly(const struct bpf_map *map);
@@ -1480,7 +1494,7 @@ static inline void mark_reg_scratched(struct bpf_verifier_env *env, u32 regno)
static inline void mark_stack_slot_scratched(struct bpf_verifier_env *env, u32 spi)
{
- env->scratched_stack_slots |= 1ULL << spi;
+ __set_bit(spi, env->scratched_stack_slots);
}
static inline bool reg_scratched(const struct bpf_verifier_env *env, u32 regno)
@@ -1488,27 +1502,28 @@ static inline bool reg_scratched(const struct bpf_verifier_env *env, u32 regno)
return (env->scratched_regs >> regno) & 1;
}
-static inline bool stack_slot_scratched(const struct bpf_verifier_env *env, u64 regno)
+static inline bool stack_slot_scratched(const struct bpf_verifier_env *env, u32 spi)
{
- return (env->scratched_stack_slots >> regno) & 1;
+ return test_bit(spi, env->scratched_stack_slots);
}
static inline bool verifier_state_scratched(const struct bpf_verifier_env *env)
{
- return env->scratched_regs || env->scratched_stack_slots;
+ return env->scratched_regs ||
+ !bitmap_empty(env->scratched_stack_slots, MAX_BPF_STACK_SLOTS);
}
static inline void mark_verifier_state_clean(struct bpf_verifier_env *env)
{
env->scratched_regs = 0U;
- env->scratched_stack_slots = 0ULL;
+ bitmap_zero(env->scratched_stack_slots, MAX_BPF_STACK_SLOTS);
}
/* Used for printing the entire verifier state. */
static inline void mark_verifier_state_scratched(struct bpf_verifier_env *env)
{
env->scratched_regs = ~0U;
- env->scratched_stack_slots = ~0ULL;
+ bitmap_fill(env->scratched_stack_slots, MAX_BPF_STACK_SLOTS);
}
static inline bool bpf_stack_narrow_access_ok(int off, int fill_size, int spill_size)
@@ -1543,7 +1558,7 @@ struct bpf_subprog_info *bpf_find_containing_subprog(struct bpf_verifier_env *en
const char *bpf_subprog_name(const struct bpf_verifier_env *env, int subprog);
int bpf_jmp_offset(struct bpf_insn *insn);
struct bpf_iarray *bpf_insn_successors(struct bpf_verifier_env *env, u32 idx);
-void bpf_fmt_stack_mask(char *buf, ssize_t buf_sz, u64 stack_mask);
+void bpf_fmt_stack_mask(char *buf, ssize_t buf_sz, const unsigned long *stack_mask);
bool bpf_subprog_is_global(const struct bpf_verifier_env *env, int subprog);
/* Kinds of member a by-value struct or union may be composed of. */
diff --git a/include/linux/filter.h b/include/linux/filter.h
index 4f0662e428970..e42eccb0990e6 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -98,6 +98,11 @@ struct ctl_table_header;
/* BPF program can access up to 512 bytes of stack space. */
#define MAX_BPF_STACK 512
+/*
+ * Stack budget of a program on a JIT that lays out frames of that size.
+ * The interpreter and JITs without such support keep MAX_BPF_STACK.
+ */
+#define MAX_BPF_STACK_JIT 2048
/* Helper macros for filter block array initializers. */
@@ -1247,6 +1252,7 @@ bool bpf_jit_supports_ptr_xchg(void);
bool bpf_jit_supports_arena(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);
bool bpf_jit_supports_timed_may_goto(void);
bool bpf_jit_supports_fsession(void);
diff --git a/kernel/bpf/backtrack.c b/kernel/bpf/backtrack.c
index 4da99dec08184..0e38b9575328c 100644
--- a/kernel/bpf/backtrack.c
+++ b/kernel/bpf/backtrack.c
@@ -9,7 +9,8 @@
/* for any branch, call, exit record the history of jmps in the given state */
int bpf_push_jmp_history(struct bpf_verifier_env *env, struct bpf_verifier_state *cur,
- int insn_flags, int spi, int frame, u64 linked_regs)
+ int insn_flags, int spi, int frame, const u16 *linked_regs,
+ u8 linked_regs_cnt)
{
u32 cnt = cur->jmp_history_cnt;
struct bpf_jmp_history_entry *p;
@@ -27,10 +28,13 @@ int bpf_push_jmp_history(struct bpf_verifier_env *env, struct bpf_verifier_state
env->cur_hist_ent->flags |= insn_flags;
env->cur_hist_ent->spi = spi;
env->cur_hist_ent->frame = frame;
- verifier_bug_if(env->cur_hist_ent->linked_regs != 0, env,
- "insn history: insn_idx %d linked_regs: %#llx",
- env->insn_idx, env->cur_hist_ent->linked_regs);
- env->cur_hist_ent->linked_regs = linked_regs;
+ verifier_bug_if(env->cur_hist_ent->linked_regs_cnt != 0, env,
+ "insn history: insn_idx %d has %u linked regs",
+ env->insn_idx, env->cur_hist_ent->linked_regs_cnt);
+ if (linked_regs_cnt)
+ memcpy(env->cur_hist_ent->linked_regs, linked_regs,
+ linked_regs_cnt * sizeof(*linked_regs));
+ env->cur_hist_ent->linked_regs_cnt = linked_regs_cnt;
return 0;
}
@@ -47,7 +51,9 @@ int bpf_push_jmp_history(struct bpf_verifier_env *env, struct bpf_verifier_state
p->flags = insn_flags;
p->spi = spi;
p->frame = frame;
- p->linked_regs = linked_regs;
+ if (linked_regs_cnt)
+ memcpy(p->linked_regs, linked_regs, linked_regs_cnt * sizeof(*linked_regs));
+ p->linked_regs_cnt = linked_regs_cnt;
cur->jmp_history_cnt = cnt;
env->cur_hist_ent = p;
@@ -123,13 +129,26 @@ static inline void bt_reset(struct backtrack_state *bt)
bt->env = env;
}
-static inline u32 bt_empty(struct backtrack_state *bt)
+static inline bool bt_frame_stack_empty(struct backtrack_state *bt, u32 frame)
{
- u64 mask = 0;
+ return bitmap_empty(bt->stack_masks[frame], MAX_BPF_STACK_SLOTS);
+}
+
+static inline bool bt_stack_empty(struct backtrack_state *bt)
+{
+ return bt_frame_stack_empty(bt, bt->frame);
+}
+
+static inline bool bt_empty(struct backtrack_state *bt)
+{
+ u32 mask = 0;
int i;
- for (i = 0; i <= bt->frame; i++)
- mask |= bt->reg_masks[i] | bt->stack_masks[i] | bt->stack_arg_masks[i];
+ for (i = 0; i <= bt->frame; i++) {
+ mask |= bt->reg_masks[i] | bt->stack_arg_masks[i];
+ if (!bt_frame_stack_empty(bt, i))
+ return false;
+ }
return mask == 0;
}
@@ -181,7 +200,7 @@ static inline void bt_clear_reg(struct backtrack_state *bt, u32 reg)
static inline void bt_clear_frame_slot(struct backtrack_state *bt, u32 frame, u32 slot)
{
- bt->stack_masks[frame] &= ~(1ull << slot);
+ __clear_bit(slot, bt->stack_masks[frame]);
}
static inline u32 bt_frame_reg_mask(struct backtrack_state *bt, u32 frame)
@@ -194,14 +213,14 @@ static inline u32 bt_reg_mask(struct backtrack_state *bt)
return bt->reg_masks[bt->frame];
}
-static inline u64 bt_frame_stack_mask(struct backtrack_state *bt, u32 frame)
+static inline unsigned long *bt_frame_stack_mask(struct backtrack_state *bt, u32 frame)
{
return bt->stack_masks[frame];
}
-static inline u64 bt_stack_mask(struct backtrack_state *bt)
+static inline unsigned long *bt_stack_mask(struct backtrack_state *bt)
{
- return bt->stack_masks[bt->frame];
+ return bt_frame_stack_mask(bt, bt->frame);
}
static inline u8 bt_stack_arg_mask(struct backtrack_state *bt)
@@ -233,17 +252,16 @@ static void fmt_reg_mask(char *buf, ssize_t buf_sz, u32 reg_mask)
break;
}
}
-/* format stack slots bitmask, e.g., "-8,-24,-40" for 0x15 mask */
-void bpf_fmt_stack_mask(char *buf, ssize_t buf_sz, u64 stack_mask)
+
+/* format stack slots bitmask, e.g., "-8,-24,-40" for slots 0, 2 and 4 */
+void bpf_fmt_stack_mask(char *buf, ssize_t buf_sz, const unsigned long *stack_mask)
{
- DECLARE_BITMAP(mask, 64);
bool first = true;
int i, n;
buf[0] = '\0';
- bitmap_from_u64(mask, stack_mask);
- for_each_set_bit(i, mask, 64) {
+ for_each_set_bit(i, stack_mask, MAX_BPF_STACK_SLOTS) {
n = snprintf(buf, buf_sz, "%s%d", first ? "" : ",", -(i + 1) * 8);
first = false;
buf += n;
@@ -455,10 +473,11 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,
/* we are now tracking register spills correctly,
* so any instance of leftover slots is a bug
*/
- if (bt_stack_mask(bt) != 0) {
- verifier_bug(env,
- "static subprog leftover stack slots %llx",
- bt_stack_mask(bt));
+ if (!bt_stack_empty(bt)) {
+ bpf_fmt_stack_mask(env->tmp_str_buf, TMP_STR_BUF_LEN,
+ bt_stack_mask(bt));
+ verifier_bug(env, "static subprog leftover stack slots %s",
+ env->tmp_str_buf);
return -EFAULT;
}
/* propagate r1-r5 to the caller */
@@ -491,9 +510,11 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,
bt_reg_mask(bt));
return -EFAULT;
}
- if (bt_stack_mask(bt) != 0) {
- verifier_bug(env, "callback leftover stack slots %llx",
- bt_stack_mask(bt));
+ if (!bt_stack_empty(bt)) {
+ bpf_fmt_stack_mask(env->tmp_str_buf, TMP_STR_BUF_LEN,
+ bt_stack_mask(bt));
+ verifier_bug(env, "callback leftover stack slots %s",
+ env->tmp_str_buf);
return -EFAULT;
}
/* clear r1-r5 in callback subprog's mask */
@@ -711,10 +732,12 @@ void bpf_mark_all_scalars_precise(struct bpf_verifier_env *env,
i, j);
}
}
- for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) {
- if (!bpf_is_spilled_reg(&func->stack[j]))
+ for (j = 0; j < bpf_stack_nr_slots(func); j++) {
+ struct bpf_stack_state *ss = bpf_stack_slot(func, j);
+
+ if (!bpf_is_spilled_reg(ss))
continue;
- reg = &func->stack[j].spilled_ptr;
+ reg = &ss->spilled_ptr;
if (reg->type != SCALAR_VALUE || reg->precise)
continue;
reg->precise = true;
@@ -826,6 +849,7 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,
int subseq_idx = -1;
struct bpf_func_state *func;
bool tmp, skip_first = true;
+ struct bpf_stack_state *ss;
struct bpf_reg_state *reg;
int i, fr, err;
@@ -872,7 +896,7 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,
if (st->curframe == 0 &&
st->frame[0]->subprogno > 0 &&
st->frame[0]->callsite == BPF_MAIN_FUNC &&
- bt_stack_mask(bt) == 0 &&
+ bt_stack_empty(bt) &&
(bt_reg_mask(bt) & ~BPF_REGMASK_ARGS) == 0) {
bitmap_from_u64(mask, bt_reg_mask(bt));
for_each_set_bit(i, mask, 32) {
@@ -886,8 +910,9 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,
return 0;
}
- verifier_bug(env, "backtracking func entry subprog %d reg_mask %x stack_mask %llx",
- st->frame[0]->subprogno, bt_reg_mask(bt), bt_stack_mask(bt));
+ bpf_fmt_stack_mask(env->tmp_str_buf, TMP_STR_BUF_LEN, bt_stack_mask(bt));
+ verifier_bug(env, "backtracking func entry subprog %d reg_mask %x stack_mask %s",
+ st->frame[0]->subprogno, bt_reg_mask(bt), env->tmp_str_buf);
return -EFAULT;
}
@@ -948,18 +973,18 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,
}
}
- bitmap_from_u64(mask, bt_frame_stack_mask(bt, fr));
- for_each_set_bit(i, mask, 64) {
- if (verifier_bug_if(i >= func->allocated_stack / BPF_REG_SIZE,
+ for_each_set_bit(i, bt_frame_stack_mask(bt, fr), MAX_BPF_STACK_SLOTS) {
+ if (verifier_bug_if(i >= bpf_stack_nr_slots(func),
env, "stack slot %d, total slots %d",
- i, func->allocated_stack / BPF_REG_SIZE))
+ i, bpf_stack_nr_slots(func)))
return -EFAULT;
- if (!bpf_is_spilled_scalar_reg(&func->stack[i])) {
+ ss = bpf_stack_slot(func, i);
+ if (!bpf_is_spilled_scalar_reg(ss)) {
bt_clear_frame_slot(bt, fr, i);
continue;
}
- reg = &func->stack[i].spilled_ptr;
+ reg = &ss->spilled_ptr;
if (reg->precise) {
bt_clear_frame_slot(bt, fr, i);
} else {
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 273f74068068c..a09aa0082393c 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -3483,6 +3483,19 @@ bool __weak bpf_jit_supports_private_stack(void)
return false;
}
+/*
+ * Return TRUE if the JIT lays out frames of up to MAX_BPF_STACK_JIT bytes.
+ * Its prologue, epilogue and tail call sequences must encode such frame
+ * sizes and a private stack must be sized from the program's depth. The
+ * budget is only granted alongside bpf_jit_supports_subprog_tailcalls(),
+ * whose tail calls land before the target sets up its own frame; see
+ * bpf_prog_stack_limit().
+ */
+bool __weak bpf_jit_supports_large_stack(void)
+{
+ return false;
+}
+
void __weak arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp, u64 bp), void *cookie)
{
}
diff --git a/kernel/bpf/diagnostics.c b/kernel/bpf/diagnostics.c
index 5ecfa86ed49f4..af97aea49075b 100644
--- a/kernel/bpf/diagnostics.c
+++ b/kernel/bpf/diagnostics.c
@@ -1600,9 +1600,9 @@ static struct bpf_reg_state *target_to_reg(struct bpf_verifier_env *env,
return NULL;
return &state->stack_arg_regs[target->stack_arg];
case BPF_DIAG_MOD_TARGET_STACK_SLOT:
- if (target->spi >= state->allocated_stack / BPF_REG_SIZE)
+ if (target->spi >= bpf_stack_nr_slots(state))
return NULL;
- return &state->stack[target->spi].spilled_ptr;
+ return &bpf_stack_slot(state, target->spi)->spilled_ptr;
default:
return NULL;
}
@@ -1618,7 +1618,7 @@ static bool reg_to_target(struct bpf_verifier_env *env, const struct bpf_reg_sta
for (frame = 0; frame <= vstate->curframe; frame++) {
struct bpf_func_state *state = vstate->frame[frame];
unsigned long start, end;
- u32 nslots = state->allocated_stack / BPF_REG_SIZE;
+ u32 nslots = bpf_stack_nr_slots(state);
int spi;
start = (unsigned long)state->regs;
@@ -1637,6 +1637,11 @@ static bool reg_to_target(struct bpf_verifier_env *env, const struct bpf_reg_sta
return true;
}
+ /*
+ * Map the pointer back to its slot by address, which relies on
+ * the slots forming one contiguous array as bpf_stack_slot()
+ * indexes it.
+ */
start = (unsigned long)state->stack;
end = (unsigned long)(state->stack + nslots);
if (nslots && addr >= start && addr < end) {
diff --git a/kernel/bpf/liveness.c b/kernel/bpf/liveness.c
index a204b492acc8f..16fb7dc43726f 100644
--- a/kernel/bpf/liveness.c
+++ b/kernel/bpf/liveness.c
@@ -10,10 +10,39 @@
#define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)
-struct per_frame_masks {
- spis_t may_read; /* stack slots that may be read by this instruction */
- spis_t must_write; /* stack slots written by this instruction */
- spis_t live_before; /* stack slots that may be read by this insn and its successors */
+/*
+ * Stack liveness is tracked with a 4-byte (half register) granularity.
+ * Half-slot 0 covers [fp-4, fp), half-slot 1 covers [fp-8, fp-4), and so on,
+ * hence FRAME_HALF_SPIS - 1 is the deepest half-slot a frame can have.
+ */
+#define FRAME_HALF_SPIS (MAX_BPF_STACK_JIT / BPF_HALF_REG_SIZE)
+#define FRAME_MAX_WORDS BITS_TO_LONGS(FRAME_HALF_SPIS)
+
+/* Masks tracked for each instruction of a frame */
+enum {
+ FM_MAY_READ, /* stack slots that may be read by this instruction */
+ FM_MUST_WRITE, /* stack slots written by this instruction */
+ FM_LIVE_BEFORE, /* stack slots that may be read by this insn and its successors */
+ FM_MASK_CNT,
+};
+
+/*
+ * Per instruction stack masks for one frame of a function instance.
+ *
+ * Most frames use only a fraction of the stack budget, so instead of masks
+ * wide enough for every half-slot of the largest possible frame, all masks
+ * of one array share the width @words, and the marking functions widen the
+ * array as deeper half-slots are recorded. Mask @kind of the instruction at
+ * relative index @i is at &bits[(i * FM_MASK_CNT + kind) * words]. A
+ * half-slot at or past @words * BITS_PER_LONG is never read by this frame,
+ * hence never live. An instruction that may read the whole frame, such as a
+ * call passing a frame pointer to another subprog, widens the array to the
+ * program's stack budget, the deepest an accepted program can reach, so
+ * that the read cannot lose half-slots to a later widening.
+ */
+struct frame_masks {
+ u32 words;
+ unsigned long bits[];
};
/*
@@ -29,7 +58,7 @@ struct func_instance {
u32 subprog_start; /* cached env->subprog_info[subprog].start */
u32 insn_cnt; /* cached number of insns in the function */
/* Per frame, per instruction masks, frames allocated lazily. */
- struct per_frame_masks *frames[MAX_CALL_FRAMES];
+ struct frame_masks *frames[MAX_CALL_FRAMES];
bool must_write_initialized;
};
@@ -151,50 +180,123 @@ static int relative_idx(struct func_instance *instance, u32 insn_idx)
return insn_idx - instance->subprog_start;
}
-static struct per_frame_masks *get_frame_masks(struct func_instance *instance,
- u32 frame, u32 insn_idx)
+static u32 frame_mask_bits(struct frame_masks *fm)
{
- if (!instance->frames[frame])
- return NULL;
+ return fm->words * BITS_PER_LONG;
+}
- return &instance->frames[frame][relative_idx(instance, insn_idx)];
+static size_t frame_mask_words(struct func_instance *instance, u32 words)
+{
+ return (size_t)instance->insn_cnt * FM_MASK_CNT * words;
}
-static struct per_frame_masks *alloc_frame_masks(struct func_instance *instance,
- u32 frame, u32 insn_idx)
+/* Mask @kind of the instruction at relative index @rel */
+static unsigned long *rel_mask(struct frame_masks *fm, u32 rel, u32 kind)
{
- struct per_frame_masks *arr;
+ return fm->bits + ((size_t)rel * FM_MASK_CNT + kind) * fm->words;
+}
- if (!instance->frames[frame]) {
- arr = kvzalloc_objs(*arr, instance->insn_cnt,
- GFP_KERNEL_ACCOUNT);
- instance->frames[frame] = arr;
- if (!arr)
- return ERR_PTR(-ENOMEM);
+/*
+ * Make sure @frame has a mask array at least @words wide, allocating it or
+ * copying the existing masks into the wider stride as needed.
+ * @words must be in range [1, FRAME_MAX_WORDS].
+ */
+static struct frame_masks *widen_frame_masks(struct func_instance *instance,
+ u32 frame, u32 words)
+{
+ struct frame_masks *old = instance->frames[frame], *new;
+ u32 i, kind;
+
+ if (old && old->words >= words)
+ return old;
+ new = kvzalloc_flex(*new, bits, frame_mask_words(instance, words), GFP_KERNEL_ACCOUNT);
+ if (!new)
+ return NULL;
+ new->words = words;
+ if (old) {
+ for (i = 0; i < instance->insn_cnt; i++)
+ for (kind = 0; kind < FM_MASK_CNT; kind++)
+ memcpy(rel_mask(new, i, kind), rel_mask(old, i, kind),
+ old->words * sizeof(*old->bits));
+ kvfree(old);
}
- return get_frame_masks(instance, frame, insn_idx);
+ instance->frames[frame] = new;
+ return new;
}
-/* Accumulate may_read masks for @frame at @insn_idx */
-static int mark_stack_read(struct func_instance *instance, u32 frame, u32 insn_idx, spis_t mask)
+/*
+ * Set the inclusive half-slot range [lo, hi] in mask @kind of @frame at @insn_idx.
+ * An empty range, including one with a negative @hi as computed for a write
+ * that does not fully cover any half-slot, marks nothing.
+ */
+static int mark_stack_range(struct func_instance *instance, u32 frame, u32 insn_idx,
+ u32 kind, s32 lo, s32 hi)
{
- struct per_frame_masks *masks;
+ struct frame_masks *fm;
- masks = alloc_frame_masks(instance, frame, insn_idx);
- if (IS_ERR(masks))
- return PTR_ERR(masks);
- masks->may_read = spis_or(masks->may_read, mask);
+ /*
+ * An access past the frame bottom is rejected by the main verifier
+ * pass later, liveness only has to avoid running off the masks.
+ */
+ hi = min_t(s32, hi, FRAME_HALF_SPIS - 1);
+ if (lo > hi)
+ return 0;
+ fm = widen_frame_masks(instance, frame, BITS_TO_LONGS(hi + 1));
+ if (!fm)
+ return -ENOMEM;
+ bitmap_set(rel_mask(fm, relative_idx(instance, insn_idx), kind), lo, hi - lo + 1);
return 0;
}
-static int mark_stack_write(struct func_instance *instance, u32 frame, u32 insn_idx, spis_t mask)
+/* Accumulate may_read for half-slots [lo, hi] of @frame at @insn_idx */
+static int mark_stack_read(struct func_instance *instance, u32 frame, u32 insn_idx,
+ s32 lo, s32 hi)
+{
+ return mark_stack_range(instance, frame, insn_idx, FM_MAY_READ, lo, hi);
+}
+
+/* Accumulate must_write for half-slots [lo, hi] of @frame at @insn_idx */
+static int mark_stack_write(struct func_instance *instance, u32 frame, u32 insn_idx,
+ s32 lo, s32 hi)
+{
+ return mark_stack_range(instance, frame, insn_idx, FM_MUST_WRITE, lo, hi);
+}
+
+/*
+ * Mark every half-slot of @frame as possibly read by @insn_idx. This widens
+ * the masks to the program's stack budget: a full read recorded at a narrower
+ * width would leave the bits added by a later widening clear and lose part of
+ * it. No verifier state holds a slot past the budget, so no liveness query
+ * reaches beyond it; an access past the budget in unreachable code may still
+ * widen a frame's masks further, which is harmless.
+ */
+static int mark_stack_read_all(struct bpf_verifier_env *env, struct func_instance *instance,
+ u32 frame, u32 insn_idx)
{
- struct per_frame_masks *masks;
+ return mark_stack_read(instance, frame, insn_idx, 0,
+ env->stack_limit / BPF_HALF_REG_SIZE - 1);
+}
- masks = alloc_frame_masks(instance, frame, insn_idx);
- if (IS_ERR(masks))
- return PTR_ERR(masks);
- masks->must_write = spis_or(masks->must_write, mask);
+/* Accumulate @src, a mask @src_words wide, into may_read of @frame at @insn_idx */
+static int mark_stack_read_mask(struct func_instance *instance, u32 frame, u32 insn_idx,
+ const unsigned long *src, u32 src_words)
+{
+ u32 nbits = src_words * BITS_PER_LONG;
+ struct frame_masks *fm;
+ unsigned long *dst;
+ u32 last, w;
+
+ last = find_last_bit(src, nbits);
+ if (last == nbits)
+ return 0;
+ fm = widen_frame_masks(instance, frame, BITS_TO_LONGS(last + 1));
+ if (!fm)
+ return -ENOMEM;
+ dst = rel_mask(fm, relative_idx(instance, insn_idx), FM_MAY_READ);
+ /* @src has no bits set past @last, hence none past @fm->words either */
+ src_words = min(src_words, fm->words);
+ for (w = 0; w < src_words; w++)
+ dst[w] |= src[w];
return 0;
}
@@ -272,33 +374,42 @@ __diag_pop();
static inline bool update_insn(struct bpf_verifier_env *env,
struct func_instance *instance, u32 frame, u32 insn_idx)
{
- spis_t new_before, new_after;
- struct per_frame_masks *insn, *succ_insn;
+ unsigned long new_after[FRAME_MAX_WORDS] = {};
+ unsigned long *may_read, *must_write, *live_before;
+ struct frame_masks *fm = instance->frames[frame];
+ u32 rel = relative_idx(instance, insn_idx);
struct bpf_iarray *succ;
- u32 s;
- bool changed;
+ bool changed = false;
+ u32 s, w;
succ = bpf_insn_successors(env, insn_idx);
if (succ->cnt == 0)
return false;
- changed = false;
- insn = get_frame_masks(instance, frame, insn_idx);
- new_before = SPIS_ZERO;
- new_after = SPIS_ZERO;
+ /* All instructions of one frame array share the same mask width */
for (s = 0; s < succ->cnt; ++s) {
- succ_insn = get_frame_masks(instance, frame, succ->items[s]);
- new_after = spis_or(new_after, succ_insn->live_before);
+ unsigned long *succ_live;
+
+ succ_live = rel_mask(fm, relative_idx(instance, succ->items[s]), FM_LIVE_BEFORE);
+ for (w = 0; w < fm->words; w++)
+ new_after[w] |= succ_live[w];
}
+ may_read = rel_mask(fm, rel, FM_MAY_READ);
+ must_write = rel_mask(fm, rel, FM_MUST_WRITE);
+ live_before = rel_mask(fm, rel, FM_LIVE_BEFORE);
/*
* New "live_before" is a union of all "live_before" of successors
* minus slots written by instruction plus slots read by instruction.
* new_before = (new_after & ~insn->must_write) | insn->may_read
*/
- new_before = spis_or(spis_and(new_after, spis_not(insn->must_write)),
- insn->may_read);
- changed |= !spis_equal(new_before, insn->live_before);
- insn->live_before = new_before;
+ for (w = 0; w < fm->words; w++) {
+ unsigned long new_before = (new_after[w] & ~must_write[w]) | may_read[w];
+
+ if (new_before != live_before[w]) {
+ live_before[w] = new_before;
+ changed = true;
+ }
+ }
return changed;
}
@@ -329,10 +440,12 @@ static void update_instance(struct bpf_verifier_env *env, struct func_instance *
static bool is_live_before(struct func_instance *instance, u32 insn_idx, u32 frameno, u32 half_spi)
{
- struct per_frame_masks *masks;
+ struct frame_masks *fm = instance->frames[frameno];
- masks = get_frame_masks(instance, frameno, insn_idx);
- return masks && spis_test_bit(masks->live_before, half_spi);
+ /* No recorded access reaches past the masks, so nothing there is live */
+ if (!fm || half_spi >= frame_mask_bits(fm))
+ return false;
+ return test_bit(half_spi, rel_mask(fm, relative_idx(instance, insn_idx), FM_LIVE_BEFORE));
}
int bpf_live_stack_query_init(struct bpf_verifier_env *env, struct bpf_verifier_state *st)
@@ -447,17 +560,19 @@ static int spi_off(int spi)
* When only one half is set, print as "-4h","-8h",...
* Runs of 3+ consecutive fully-set SPIs are collapsed: "fp0-8..-24"
*/
-static char *fmt_spis_mask(struct bpf_verifier_env *env, int frame, bool first, spis_t spis)
+static char *fmt_spis_mask(struct bpf_verifier_env *env, int frame, bool first,
+ const unsigned long *spis, u32 words)
{
int buf_sz = sizeof(env->tmp_str_buf);
+ int spi_cnt = words * BITS_PER_LONG / 2;
char *buf = env->tmp_str_buf;
int spi, n, run_start;
buf[0] = '\0';
- for (spi = 0; spi < STACK_SLOTS / 2 && buf_sz > 0; spi++) {
- bool lo = spis_test_bit(spis, spi * 2);
- bool hi = spis_test_bit(spis, spi * 2 + 1);
+ for (spi = 0; spi < spi_cnt && buf_sz > 0; spi++) {
+ bool lo = test_bit(spi * 2, spis);
+ bool hi = test_bit(spi * 2 + 1, spis);
const char *space = first ? "" : " ";
if (!lo && !hi)
@@ -467,16 +582,16 @@ static char *fmt_spis_mask(struct bpf_verifier_env *env, int frame, bool first,
/* half-spi */
n = scnprintf(buf, buf_sz, "%sfp%d%d%s",
space, frame, spi_off(spi) + (lo ? STACK_SLOT_SZ : 0), "h");
- } else if (spi + 2 < STACK_SLOTS / 2 &&
- spis_test_bit(spis, spi * 2 + 2) &&
- spis_test_bit(spis, spi * 2 + 3) &&
- spis_test_bit(spis, spi * 2 + 4) &&
- spis_test_bit(spis, spi * 2 + 5)) {
+ } else if (spi + 2 < spi_cnt &&
+ test_bit(spi * 2 + 2, spis) &&
+ test_bit(spi * 2 + 3, spis) &&
+ test_bit(spi * 2 + 4, spis) &&
+ test_bit(spi * 2 + 5, spis)) {
/* 3+ consecutive full spis */
run_start = spi;
- while (spi + 1 < STACK_SLOTS / 2 &&
- spis_test_bit(spis, (spi + 1) * 2) &&
- spis_test_bit(spis, (spi + 1) * 2 + 1))
+ while (spi + 1 < spi_cnt &&
+ test_bit((spi + 1) * 2, spis) &&
+ test_bit((spi + 1) * 2 + 1, spis))
spi++;
n = scnprintf(buf, buf_sz, "%sfp%d%d..%d",
space, frame, spi_off(run_start), spi_off(spi));
@@ -495,7 +610,8 @@ static void print_instance(struct bpf_verifier_env *env, struct func_instance *i
{
int start = env->subprog_info[instance->subprog].start;
struct bpf_insn *insns = env->prog->insnsi;
- struct per_frame_masks *masks;
+ struct frame_masks *fm;
+ unsigned long *mask;
int len = instance->insn_cnt;
int insn_idx, frame, i;
bool has_use, has_def;
@@ -518,10 +634,13 @@ static void print_instance(struct bpf_verifier_env *env, struct func_instance *i
pos = env->log.end_pos;
verbose(env, " use: ");
for (frame = instance->depth; frame >= 0; --frame) {
- masks = get_frame_masks(instance, frame, insn_idx);
- if (!masks || spis_is_zero(masks->may_read))
+ fm = instance->frames[frame];
+ if (!fm)
+ continue;
+ mask = rel_mask(fm, i, FM_MAY_READ);
+ if (bitmap_empty(mask, frame_mask_bits(fm)))
continue;
- verbose(env, "%s", fmt_spis_mask(env, frame, !has_use, masks->may_read));
+ verbose(env, "%s", fmt_spis_mask(env, frame, !has_use, mask, fm->words));
has_use = true;
}
if (!has_use)
@@ -529,10 +648,13 @@ static void print_instance(struct bpf_verifier_env *env, struct func_instance *i
pos = env->log.end_pos;
verbose(env, " def: ");
for (frame = instance->depth; frame >= 0; --frame) {
- masks = get_frame_masks(instance, frame, insn_idx);
- if (!masks || spis_is_zero(masks->must_write))
+ fm = instance->frames[frame];
+ if (!fm)
continue;
- verbose(env, "%s", fmt_spis_mask(env, frame, !has_def, masks->must_write));
+ mask = rel_mask(fm, i, FM_MUST_WRITE);
+ if (bitmap_empty(mask, frame_mask_bits(fm)))
+ continue;
+ verbose(env, "%s", fmt_spis_mask(env, frame, !has_def, mask, fm->words));
has_def = true;
}
if (!has_def)
@@ -601,9 +723,9 @@ static int print_instances(struct bpf_verifier_env *env)
* - same frame + different offset -> offset-imprecise
* - different frames -> fully-imprecise (bitmask OR)
*
- * At memory access sites (LDX/STX/ST), offset-imprecise marks only
- * the known frame's access mask as SPIS_ALL, while fully-imprecise
- * iterates bits in the bitmask and routes each frame to its target.
+ * At memory access sites (LDX/STX/ST), offset-imprecise marks the known
+ * frame as fully read, while fully-imprecise iterates bits in the bitmask
+ * and routes each frame to its target.
*/
#define MAX_ARG_OFFSETS 4
@@ -622,8 +744,15 @@ enum arg_track_state {
ARG_IMPRECISE = -3, /* lost identity; .mask is arg bitmask */
};
-/* Track callee stack slots fp-8 through fp-512 (64 slots of 8 bytes each) */
-#define MAX_ARG_SPILL_SLOTS 64
+/*
+ * The tracker follows as many spill slots as a subprog addresses directly
+ * through R10, see subprog_spill_slots(). A frame's slots at a call site are
+ * kept in a snapshot for the callees that receive pointers into that frame.
+ */
+struct spill_snapshot {
+ u32 slots;
+ struct arg_track at[];
+};
/*
* Combined register + stack arg tracking: R0-R10 at indices 0-10,
@@ -898,13 +1027,60 @@ static void arg_padd(struct arg_track *at, s64 delta)
}
/*
- * Convert a byte offset from FP to a callee stack slot index.
+ * Slots the spill tracker may follow for a subprog of @len instructions
+ * without its per-instruction tables costing more than they could for the
+ * largest program while every frame stayed within MAX_BPF_STACK: as many
+ * entries as 64 slots need for BPF_COMPLEXITY_LIMIT_INSNS instructions.
+ */
+static u32 spill_slots_affordable(int len)
+{
+ u32 base = MAX_BPF_STACK / BPF_REG_SIZE;
+
+ return max_t(u32, base, base * BPF_COMPLEXITY_LIMIT_INSNS / len);
+}
+
+/*
+ * Number of 8-byte spill slots to track for the instructions in [@start, @end):
+ * the 64 slots of a MAX_BPF_STACK frame, which the tracker has always
+ * followed, or the deepest 8-byte stack access made directly through R10 when
+ * that reaches further, up to the program's stack budget. Spills and fills are
+ * compiled as direct R10 accesses, so the count covers them whatever the frame
+ * size; a fill through a derived pointer that reaches past it is treated as
+ * imprecise, as one past the frame always was.
+ */
+static u32 subprog_spill_slots(struct bpf_verifier_env *env, int start, int end)
+{
+ struct bpf_insn *insns = env->prog->insnsi;
+ int deepest = 0;
+
+ for (int i = start; i < end; i++) {
+ struct bpf_insn *insn = &insns[i];
+ u8 class = BPF_CLASS(insn->code);
+ u8 base;
+
+ if (BPF_SIZE(insn->code) != BPF_DW)
+ continue;
+ if (class == BPF_LDX)
+ base = insn->src_reg;
+ else if (class == BPF_STX || class == BPF_ST)
+ base = insn->dst_reg;
+ else
+ continue;
+ if (base == BPF_REG_FP && insn->off < 0)
+ deepest = max(deepest, -insn->off);
+ }
+ return clamp_t(u32, DIV_ROUND_UP(deepest, 8), MAX_BPF_STACK / BPF_REG_SIZE,
+ env->stack_limit / BPF_REG_SIZE);
+}
+
+/*
+ * Convert a byte offset from FP to one of the @nslots tracked stack slots.
* Returns -1 if out of range or not 8-byte aligned.
* Slot 0 = fp-8, slot 1 = fp-16, ..., slot 7 = fp-64, ....
*/
-static int fp_off_to_slot(s16 off)
+static int fp_off_to_slot(s16 off, u32 nslots)
{
- if (off >= 0 || off < -(int)(MAX_ARG_SPILL_SLOTS * 8))
+ if (off >= 0 || off < -(int)(nslots * 8))
return -1;
if (off % 8)
return -1;
@@ -913,7 +1089,7 @@ static int fp_off_to_slot(s16 off)
static struct arg_track fill_from_stack(struct bpf_insn *insn,
struct arg_track *at_out, int reg,
- struct arg_track *at_stack_out,
+ struct arg_track *at_stack_out, u32 nslots,
int depth)
{
struct arg_track imp = {
@@ -924,7 +1100,7 @@ static struct arg_track fill_from_stack(struct bpf_insn *insn,
int cnt, i;
if (reg == BPF_REG_FP) {
- int slot = fp_off_to_slot(insn->off);
+ int slot = fp_off_to_slot(insn->off, nslots);
return slot >= 0 ? at_stack_out[slot] : imp;
}
@@ -937,7 +1113,7 @@ static struct arg_track fill_from_stack(struct bpf_insn *insn,
if (arg_add(at_out[reg].off[i], insn->off, &fp_off))
return imp;
- slot = fp_off_to_slot(fp_off);
+ slot = fp_off_to_slot(fp_off, nslots);
if (slot < 0)
return imp;
result = __arg_track_join(result, at_stack_out[slot]);
@@ -952,7 +1128,7 @@ static struct arg_track fill_from_stack(struct bpf_insn *insn,
* When exact offset is unknown conservatively add reg values to all slots in at_stack_out.
*/
static void spill_to_stack(struct bpf_insn *insn, struct arg_track *at_out,
- int reg, struct arg_track *at_stack_out,
+ int reg, struct arg_track *at_stack_out, u32 nslots,
struct arg_track *val, u32 sz)
{
struct arg_track none = { .frame = ARG_NONE };
@@ -960,7 +1136,7 @@ static void spill_to_stack(struct bpf_insn *insn, struct arg_track *at_out,
int cnt, i;
if (reg == BPF_REG_FP) {
- int slot = fp_off_to_slot(insn->off);
+ int slot = fp_off_to_slot(insn->off, nslots);
if (slot >= 0)
at_stack_out[slot] = new_val;
@@ -968,7 +1144,7 @@ static void spill_to_stack(struct bpf_insn *insn, struct arg_track *at_out,
}
cnt = at_out[reg].off_cnt;
if (cnt == 0) {
- for (int slot = 0; slot < MAX_ARG_SPILL_SLOTS; slot++)
+ for (int slot = 0; slot < nslots; slot++)
at_stack_out[slot] = __arg_track_join(at_stack_out[slot], new_val);
return;
}
@@ -978,7 +1154,7 @@ static void spill_to_stack(struct bpf_insn *insn, struct arg_track *at_out,
if (arg_add(at_out[reg].off[i], insn->off, &fp_off))
continue;
- slot = fp_off_to_slot(fp_off);
+ slot = fp_off_to_slot(fp_off, nslots);
if (slot < 0)
continue;
if (cnt == 1)
@@ -992,16 +1168,17 @@ static void spill_to_stack(struct bpf_insn *insn, struct arg_track *at_out,
* Clear all tracked callee stack slots overlapping the byte range
* [off, off+sz-1] where off is a negative FP-relative offset.
*/
-static void clear_overlapping_stack_slots(struct arg_track *at_stack, s16 off, u32 sz, int cnt)
+static void clear_overlapping_stack_slots(struct arg_track *at_stack, u32 nslots, s16 off,
+ u32 sz, int cnt)
{
struct arg_track none = { .frame = ARG_NONE };
if (cnt == 0) {
- for (int i = 0; i < MAX_ARG_SPILL_SLOTS; i++)
+ for (int i = 0; i < nslots; i++)
at_stack[i] = __arg_track_join(at_stack[i], none);
return;
}
- for (int i = 0; i < MAX_ARG_SPILL_SLOTS; i++) {
+ for (int i = 0; i < nslots; i++) {
int slot_start = -((i + 1) * 8);
int slot_end = slot_start + 8;
@@ -1019,33 +1196,33 @@ static void clear_overlapping_stack_slots(struct arg_track *at_stack, s16 off, u
*/
static void clear_stack_for_all_offs(struct bpf_insn *insn,
struct arg_track *at_out, int reg,
- struct arg_track *at_stack_out, u32 sz)
+ struct arg_track *at_stack_out, u32 nslots, u32 sz)
{
int cnt, i;
if (reg == BPF_REG_FP) {
- clear_overlapping_stack_slots(at_stack_out, insn->off, sz, 1);
+ clear_overlapping_stack_slots(at_stack_out, nslots, insn->off, sz, 1);
return;
}
cnt = at_out[reg].off_cnt;
if (cnt == 0) {
- clear_overlapping_stack_slots(at_stack_out, 0, sz, cnt);
+ clear_overlapping_stack_slots(at_stack_out, nslots, 0, sz, cnt);
return;
}
for (i = 0; i < cnt; i++) {
s16 fp_off;
if (arg_add(at_out[reg].off[i], insn->off, &fp_off)) {
- clear_overlapping_stack_slots(at_stack_out, 0, sz, 0);
+ clear_overlapping_stack_slots(at_stack_out, nslots, 0, sz, 0);
break;
}
- clear_overlapping_stack_slots(at_stack_out, fp_off, sz, cnt);
+ clear_overlapping_stack_slots(at_stack_out, nslots, fp_off, sz, cnt);
}
}
static void arg_track_log(struct bpf_verifier_env *env, struct bpf_insn *insn, int idx,
struct arg_track *at_in, struct arg_track *at_stack_in,
- struct arg_track *at_out, struct arg_track *at_stack_out)
+ struct arg_track *at_out, struct arg_track *at_stack_out, u32 nslots)
{
bool printed = false;
int i;
@@ -1077,7 +1254,7 @@ static void arg_track_log(struct bpf_verifier_env *env, struct bpf_insn *insn, i
verbose(env, "\tsa%d: ", i); verbose_arg_track(env, &at_in[ai]);
verbose(env, " -> "); verbose_arg_track(env, &at_out[ai]);
}
- for (i = 0; i < MAX_ARG_SPILL_SLOTS; i++) {
+ for (i = 0; i < nslots; i++) {
if (arg_track_eq(&at_stack_out[i], &at_stack_in[i]))
continue;
if (!printed) {
@@ -1105,7 +1282,7 @@ static bool can_be_local_fp(int depth, int regno, struct arg_track *at)
*/
static void arg_track_xfer(struct bpf_verifier_env *env, struct bpf_insn *insn,
int insn_idx,
- struct arg_track *at_out, struct arg_track *at_stack_out,
+ struct arg_track *at_out, struct arg_track *at_stack_out, u32 nslots,
const struct arg_track *at_stack_arg_entry,
struct func_instance *instance,
u32 *callsites)
@@ -1189,14 +1366,15 @@ static void arg_track_xfer(struct bpf_verifier_env *env, struct bpf_insn *insn,
* ARG_IMPRECISE (off_cnt == 0).
*/
if (src_is_local_fp && BPF_MODE(insn->code) == BPF_MEM && sz == 8) {
- *dst = fill_from_stack(insn, at_out, insn->src_reg, at_stack_out, depth);
+ *dst = fill_from_stack(insn, at_out, insn->src_reg, at_stack_out, nslots,
+ depth);
} else if (src->frame >= 0 && src->frame < depth &&
BPF_MODE(insn->code) == BPF_MEM && sz == 8) {
- struct arg_track *parent_stack =
+ struct spill_snapshot *parent =
env->callsite_at_stack[callsites[src->frame]];
*dst = fill_from_stack(insn, at_out, insn->src_reg,
- parent_stack, src->frame);
+ parent->at, parent->slots, src->frame);
} else if (src->frame == ARG_IMPRECISE &&
!(src->mask & BIT(depth)) && src->mask &&
BPF_MODE(insn->code) == BPF_MEM && sz == 8) {
@@ -1218,12 +1396,12 @@ static void arg_track_xfer(struct bpf_verifier_env *env, struct bpf_insn *insn,
dst_is_local_fp = can_be_local_fp(depth, insn->dst_reg, dst);
if (dst_is_local_fp && BPF_MODE(insn->code) == BPF_MEM)
spill_to_stack(insn, at_out, insn->dst_reg,
- at_stack_out, src, sz);
+ at_stack_out, nslots, src, sz);
if (BPF_MODE(insn->code) == BPF_ATOMIC) {
if (dst_is_local_fp && insn->imm != BPF_LOAD_ACQ)
clear_stack_for_all_offs(insn, at_out, insn->dst_reg,
- at_stack_out, sz);
+ at_stack_out, nslots, sz);
r = bpf_atomic_load_reg(insn);
if (r >= 0)
@@ -1236,7 +1414,7 @@ static void arg_track_xfer(struct bpf_verifier_env *env, struct bpf_insn *insn,
/* BPF_ST to FP-derived dst: clear overlapping stack slots */
if (dst_is_local_fp)
clear_stack_for_all_offs(insn, at_out, insn->dst_reg,
- at_stack_out, sz);
+ at_stack_out, nslots, sz);
}
}
@@ -1252,7 +1430,6 @@ static int record_stack_access_off(struct func_instance *instance, s64 fp_off,
s64 access_bytes, u32 frame, u32 insn_idx)
{
s32 slot_hi, slot_lo;
- spis_t mask;
if (fp_off >= 0)
/*
@@ -1264,27 +1441,19 @@ static int record_stack_access_off(struct func_instance *instance, s64 fp_off,
if (access_bytes == S64_MIN) {
/* helper/kfunc read unknown amount of bytes from fp_off until fp+0 */
slot_hi = (-fp_off - 1) / STACK_SLOT_SZ;
- mask = SPIS_ZERO;
- spis_or_range(&mask, 0, slot_hi);
- return mark_stack_read(instance, frame, insn_idx, mask);
+ return mark_stack_read(instance, frame, insn_idx, 0, slot_hi);
}
if (access_bytes > 0) {
/* Mark any touched slot as use */
slot_hi = (-fp_off - 1) / STACK_SLOT_SZ;
slot_lo = max_t(s32, (-fp_off - access_bytes) / STACK_SLOT_SZ, 0);
- mask = SPIS_ZERO;
- spis_or_range(&mask, slot_lo, slot_hi);
- return mark_stack_read(instance, frame, insn_idx, mask);
+ return mark_stack_read(instance, frame, insn_idx, slot_lo, slot_hi);
} else if (access_bytes < 0) {
/* Mark only fully covered slots as def */
access_bytes = -access_bytes;
slot_hi = (-fp_off) / STACK_SLOT_SZ - 1;
slot_lo = max_t(s32, (-fp_off - access_bytes + STACK_SLOT_SZ - 1) / STACK_SLOT_SZ, 0);
- if (slot_lo <= slot_hi) {
- mask = SPIS_ZERO;
- spis_or_range(&mask, slot_lo, slot_hi);
- return mark_stack_write(instance, frame, insn_idx, mask);
- }
+ return mark_stack_write(instance, frame, insn_idx, slot_lo, slot_hi);
}
return 0;
}
@@ -1293,7 +1462,7 @@ static int record_stack_access_off(struct func_instance *instance, s64 fp_off,
* 'arg' is FP-derived argument to helper/kfunc or load/store that
* reads (positive) or writes (negative) 'access_bytes' into 'use' or 'def'.
*/
-static int record_stack_access(struct func_instance *instance,
+static int record_stack_access(struct bpf_verifier_env *env, struct func_instance *instance,
const struct arg_track *arg,
s64 access_bytes, u32 frame, u32 insn_idx)
{
@@ -1303,7 +1472,7 @@ static int record_stack_access(struct func_instance *instance,
return 0;
if (arg->off_cnt == 0) {
if (access_bytes > 0 || access_bytes == S64_MIN)
- return mark_stack_read(instance, frame, insn_idx, SPIS_ALL);
+ return mark_stack_read_all(env, instance, frame, insn_idx);
return 0;
}
if (access_bytes != S64_MIN && access_bytes < 0 && arg->off_cnt != 1)
@@ -1322,7 +1491,8 @@ static int record_stack_access(struct func_instance *instance,
* When a pointer is ARG_IMPRECISE, conservatively mark every frame in
* the bitmask as fully used.
*/
-static int record_imprecise(struct func_instance *instance, u32 mask, u32 insn_idx)
+static int record_imprecise(struct bpf_verifier_env *env, struct func_instance *instance,
+ u32 mask, u32 insn_idx)
{
int depth = instance->depth;
int f, err;
@@ -1331,7 +1501,7 @@ static int record_imprecise(struct func_instance *instance, u32 mask, u32 insn_i
if (!(mask & 1))
continue;
if (f <= depth) {
- err = mark_stack_read(instance, f, insn_idx, SPIS_ALL);
+ err = mark_stack_read_all(env, instance, f, insn_idx);
if (err)
return err;
}
@@ -1400,9 +1570,9 @@ static int record_load_store_access(struct bpf_verifier_env *env,
}
if (ptr->frame >= 0 && ptr->frame <= depth)
- return record_stack_access(instance, ptr, sz, ptr->frame, insn_idx);
+ return record_stack_access(env, instance, ptr, sz, ptr->frame, insn_idx);
if (ptr->frame == ARG_IMPRECISE)
- return record_imprecise(instance, ptr->mask, insn_idx);
+ return record_imprecise(env, instance, ptr->mask, insn_idx);
/* ARG_NONE: not derived from any frame pointer, skip */
return 0;
}
@@ -1427,7 +1597,7 @@ static int record_arg_access(struct bpf_verifier_env *env,
bytes = bpf_kfunc_stack_access_bytes(env, insn, arg_idx, insn_idx);
} else {
for (int f = 0; f <= depth; f++) {
- err = mark_stack_read(instance, f, insn_idx, SPIS_ALL);
+ err = mark_stack_read_all(env, instance, f, insn_idx);
if (err)
return err;
}
@@ -1437,9 +1607,9 @@ static int record_arg_access(struct bpf_verifier_env *env,
return 0;
if (frame >= 0 && frame <= depth)
- err = record_stack_access(instance, at, bytes, frame, insn_idx);
+ err = record_stack_access(env, instance, at, bytes, frame, insn_idx);
else if (frame == ARG_IMPRECISE)
- err = record_imprecise(instance, at->mask, insn_idx);
+ err = record_imprecise(env, instance, at->mask, insn_idx);
return err;
}
@@ -1542,7 +1712,7 @@ struct subprog_at_info {
static void print_subprog_arg_access(struct bpf_verifier_env *env,
int subprog,
struct subprog_at_info *info,
- struct arg_track (*at_stack_in)[MAX_ARG_SPILL_SLOTS])
+ struct arg_track *at_stack_in, u32 nslots)
{
struct bpf_insn *insns = env->prog->insnsi;
int start = env->subprog_info[subprog].start;
@@ -1575,8 +1745,8 @@ static void print_subprog_arg_access(struct bpf_verifier_env *env,
has_extra = true;
}
if (is_ldx_stx_call) {
- for (r = 0; r < MAX_ARG_SPILL_SLOTS; r++)
- if (arg_is_fp(&at_stack_in[i][r]))
+ for (r = 0; r < nslots; r++)
+ if (arg_is_fp(&at_stack_in[(size_t)i * nslots + r]))
has_extra = true;
}
@@ -1606,11 +1776,11 @@ static void print_subprog_arg_access(struct bpf_verifier_env *env,
}
if (is_ldx_stx_call) {
- for (r = 0; r < MAX_ARG_SPILL_SLOTS; r++) {
- if (!arg_is_fp(&at_stack_in[i][r]))
+ for (r = 0; r < nslots; r++) {
+ if (!arg_is_fp(&at_stack_in[(size_t)i * nslots + r]))
continue;
verbose(env, " fp%+d=", -(r + 1) * 8);
- verbose_arg_track(env, &at_stack_in[i][r]);
+ verbose_arg_track(env, &at_stack_in[(size_t)i * nslots + r]);
}
}
@@ -1643,9 +1813,10 @@ static int compute_subprog_args(struct bpf_verifier_env *env,
int end = env->subprog_info[subprog + 1].start;
int po_end = env->subprog_info[subprog + 1].postorder_start;
int len = end - start;
+ u32 nslots = min(subprog_spill_slots(env, start, end), spill_slots_affordable(len));
struct arg_track (*at_in)[MAX_AT_TRACK_REGS] = NULL;
struct arg_track at_out[MAX_AT_TRACK_REGS];
- struct arg_track (*at_stack_in)[MAX_ARG_SPILL_SLOTS] = NULL;
+ struct arg_track *at_stack_in = NULL;
struct arg_track *at_stack_out = NULL;
struct arg_track at_stack_arg_entry[MAX_STACK_ARG_SLOTS];
struct arg_track unvisited = { .frame = ARG_UNVISITED };
@@ -1657,19 +1828,19 @@ static int compute_subprog_args(struct bpf_verifier_env *env,
if (!at_in)
goto err_free;
- at_stack_in = kvmalloc_objs(*at_stack_in, len, GFP_KERNEL_ACCOUNT);
+ at_stack_in = kvmalloc_objs(*at_stack_in, (size_t)len * nslots, GFP_KERNEL_ACCOUNT);
if (!at_stack_in)
goto err_free;
- at_stack_out = kvmalloc_objs(*at_stack_out, MAX_ARG_SPILL_SLOTS, GFP_KERNEL_ACCOUNT);
+ at_stack_out = kvmalloc_objs(*at_stack_out, nslots, GFP_KERNEL_ACCOUNT);
if (!at_stack_out)
goto err_free;
for (i = 0; i < len; i++) {
for (r = 0; r < MAX_AT_TRACK_REGS; r++)
at_in[i][r] = unvisited;
- for (r = 0; r < MAX_ARG_SPILL_SLOTS; r++)
- at_stack_in[i][r] = unvisited;
+ for (r = 0; r < nslots; r++)
+ at_stack_in[(size_t)i * nslots + r] = unvisited;
}
for (r = 0; r < MAX_AT_TRACK_REGS; r++)
@@ -1685,8 +1856,8 @@ static int compute_subprog_args(struct bpf_verifier_env *env,
}
/* Entry: all stack slots are ARG_NONE */
- for (r = 0; r < MAX_ARG_SPILL_SLOTS; r++)
- at_stack_in[0][r] = none;
+ for (r = 0; r < nslots; r++)
+ at_stack_in[r] = none;
/* Entry: incoming stack args from caller, or ARG_NONE for main */
for (r = 0; r < MAX_STACK_ARG_SLOTS; r++)
@@ -1708,11 +1879,12 @@ static int compute_subprog_args(struct bpf_verifier_env *env,
continue;
memcpy(at_out, at_in[i], sizeof(at_out));
- memcpy(at_stack_out, at_stack_in[i], MAX_ARG_SPILL_SLOTS * sizeof(*at_stack_out));
+ memcpy(at_stack_out, &at_stack_in[(size_t)i * nslots], nslots * sizeof(*at_stack_out));
- arg_track_xfer(env, insn, idx, at_out, at_stack_out,
+ arg_track_xfer(env, insn, idx, at_out, at_stack_out, nslots,
at_stack_arg_entry, instance, callsites);
- arg_track_log(env, insn, idx, at_in[i], at_stack_in[i], at_out, at_stack_out);
+ arg_track_log(env, insn, idx, at_in[i], &at_stack_in[(size_t)i * nslots], at_out,
+ at_stack_out, nslots);
/* Propagate to successors within this subprogram */
succ = bpf_insn_successors(env, idx);
@@ -1729,9 +1901,10 @@ static int compute_subprog_args(struct bpf_verifier_env *env,
changed |= arg_track_join(env, idx, target, r,
&at_in[ti][r], at_out[r]);
- for (r = 0; r < MAX_ARG_SPILL_SLOTS; r++)
+ for (r = 0; r < nslots; r++)
changed |= arg_track_join(env, idx, target, -r - 1,
- &at_stack_in[ti][r], at_stack_out[r]);
+ &at_stack_in[(size_t)ti * nslots + r],
+ at_stack_out[r]);
}
}
if (changed)
@@ -1754,23 +1927,24 @@ static int compute_subprog_args(struct bpf_verifier_env *env,
}
if (bpf_pseudo_call(insn) || bpf_calls_callback(env, idx)) {
+ struct spill_snapshot *snap;
+
kvfree(env->callsite_at_stack[idx]);
- env->callsite_at_stack[idx] =
- kvmalloc_objs(*env->callsite_at_stack[idx],
- MAX_ARG_SPILL_SLOTS, GFP_KERNEL_ACCOUNT);
- if (!env->callsite_at_stack[idx]) {
+ env->callsite_at_stack[idx] = kvmalloc_flex(*snap, at, nslots, GFP_KERNEL_ACCOUNT);
+ snap = env->callsite_at_stack[idx];
+ if (!snap) {
err = -ENOMEM;
goto err_free;
}
- memcpy(env->callsite_at_stack[idx],
- at_stack_in[i], sizeof(struct arg_track) * MAX_ARG_SPILL_SLOTS);
+ snap->slots = nslots;
+ memcpy(snap->at, &at_stack_in[(size_t)i * nslots], nslots * sizeof(*snap->at));
}
}
info->at_in = at_in;
at_in = NULL;
info->len = len;
- print_subprog_arg_access(env, subprog, info, at_stack_in);
+ print_subprog_arg_access(env, subprog, info, at_stack_in, nslots);
err = 0;
err_free:
@@ -1797,36 +1971,52 @@ static bool has_fp_args(struct arg_track *args)
* may_read: union (any pass might read the slot).
* must_write: intersection (only slots written on ALL passes are guaranteed).
* live_before is recomputed by a subsequent update_instance() on @dst.
+ *
+ * The two instances may have settled on different mask widths for the same
+ * frame, so @dst is widened to cover @src first. A word only @dst has counts
+ * as zero on the @src side: it unions into may_read as a no-op and intersects
+ * must_write to empty.
*/
-static void merge_instances(struct func_instance *dst, struct func_instance *src)
+static int merge_instances(struct func_instance *dst, struct func_instance *src)
{
- int f, i;
+ struct frame_masks *d, *s;
+ u32 f, i, w;
for (f = 0; f <= dst->depth; f++) {
- if (!src->frames[f]) {
+ s = src->frames[f];
+ d = dst->frames[f];
+ if (!s) {
/* This pass didn't touch frame f — must_write intersects with empty. */
- if (dst->frames[f])
+ if (d)
for (i = 0; i < dst->insn_cnt; i++)
- dst->frames[f][i].must_write = SPIS_ZERO;
+ bitmap_zero(rel_mask(d, i, FM_MUST_WRITE),
+ frame_mask_bits(d));
continue;
}
- if (!dst->frames[f]) {
+ if (!d) {
/* Previous pass didn't touch frame f — take src, zero must_write. */
- dst->frames[f] = src->frames[f];
+ dst->frames[f] = s;
src->frames[f] = NULL;
for (i = 0; i < dst->insn_cnt; i++)
- dst->frames[f][i].must_write = SPIS_ZERO;
+ bitmap_zero(rel_mask(s, i, FM_MUST_WRITE), frame_mask_bits(s));
continue;
}
+ d = widen_frame_masks(dst, f, s->words);
+ if (!d)
+ return -ENOMEM;
for (i = 0; i < dst->insn_cnt; i++) {
- dst->frames[f][i].may_read =
- spis_or(dst->frames[f][i].may_read,
- src->frames[f][i].may_read);
- dst->frames[f][i].must_write =
- spis_and(dst->frames[f][i].must_write,
- src->frames[f][i].must_write);
+ unsigned long *dst_read = rel_mask(d, i, FM_MAY_READ);
+ unsigned long *dst_write = rel_mask(d, i, FM_MUST_WRITE);
+ unsigned long *src_read = rel_mask(s, i, FM_MAY_READ);
+ unsigned long *src_write = rel_mask(s, i, FM_MUST_WRITE);
+
+ for (w = 0; w < d->words; w++) {
+ dst_read[w] |= w < s->words ? src_read[w] : 0;
+ dst_write[w] &= w < s->words ? src_write[w] : 0;
+ }
}
}
+ return 0;
}
static struct func_instance *fresh_instance(struct func_instance *src)
@@ -1941,7 +2131,7 @@ static int analyze_subprog(struct bpf_verifier_env *env,
if (info[subprog].at_in[j][caller_reg].frame == ARG_NONE)
continue;
for (int f = 0; f <= depth; f++) {
- err = mark_stack_read(instance, f, idx, SPIS_ALL);
+ err = mark_stack_read_all(env, instance, f, idx);
if (err)
goto out_free;
}
@@ -1980,13 +2170,18 @@ static int analyze_subprog(struct bpf_verifier_env *env,
/* Pull callee's entry liveness back to caller's callsite */
{
u32 callee_start = callee_instance->subprog_start;
- struct per_frame_masks *entry;
+ struct frame_masks *callee_fm;
for (int f = 0; f < callee_instance->depth; f++) {
- entry = get_frame_masks(callee_instance, f, callee_start);
- if (!entry)
+ callee_fm = callee_instance->frames[f];
+ if (!callee_fm)
continue;
- err = mark_stack_read(instance, f, idx, entry->live_before);
+ err = mark_stack_read_mask(instance, f, idx,
+ rel_mask(callee_fm,
+ relative_idx(callee_instance,
+ callee_start),
+ FM_LIVE_BEFORE),
+ callee_fm->words);
if (err)
goto out_free;
}
@@ -1994,9 +2189,11 @@ static int analyze_subprog(struct bpf_verifier_env *env,
}
if (prev_instance) {
- merge_instances(prev_instance, instance);
+ err = merge_instances(prev_instance, instance);
free_instance(instance);
instance = prev_instance;
+ if (err)
+ return err;
}
update_instance(env, instance);
return 0;
diff --git a/kernel/bpf/log.c b/kernel/bpf/log.c
index fb032dfdc0dee..d850a7863d2ed 100644
--- a/kernel/bpf/log.c
+++ b/kernel/bpf/log.c
@@ -716,7 +716,8 @@ void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_verifie
verbose(env, "=");
print_reg_state(env, state, reg);
}
- for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
+ for (i = 0; i < bpf_stack_nr_slots(state); i++) {
+ struct bpf_stack_state *slot = bpf_stack_slot(state, i);
char types_buf[BPF_REG_SIZE + 1];
const char *sep = "";
bool valid = false;
@@ -727,7 +728,7 @@ void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_verifie
continue;
for (j = 0; j < BPF_REG_SIZE; j++) {
- slot_type = state->stack[i].slot_type[j];
+ slot_type = slot->slot_type[j];
if (slot_type != STACK_INVALID && slot_type != STACK_POISON)
valid = true;
types_buf[j] = slot_type_char[slot_type];
@@ -736,12 +737,12 @@ void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_verifie
if (!valid)
continue;
- reg = &state->stack[i].spilled_ptr;
- switch (state->stack[i].slot_type[BPF_REG_SIZE - 1]) {
+ reg = &slot->spilled_ptr;
+ switch (slot->slot_type[BPF_REG_SIZE - 1]) {
case STACK_SPILL:
/* print MISC/ZERO/INVALID slots above subreg spill */
for (j = 0; j < BPF_REG_SIZE; j++)
- if (state->stack[i].slot_type[j] == STACK_SPILL)
+ if (slot->slot_type[j] == STACK_SPILL)
break;
types_buf[j] = '\0';
@@ -751,7 +752,7 @@ void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_verifie
case STACK_DYNPTR:
/* skip to main dynptr slot */
i += BPF_DYNPTR_NR_SLOTS - 1;
- reg = &state->stack[i].spilled_ptr;
+ reg = &bpf_stack_slot(state, i)->spilled_ptr;
verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE);
verbose(env, "=dynptr_%s(", dynptr_type_str(reg->dynptr.type));
diff --git a/kernel/bpf/states.c b/kernel/bpf/states.c
index 66fb11b6c6a76..a07af8f11a5a2 100644
--- a/kernel/bpf/states.c
+++ b/kernel/bpf/states.c
@@ -335,21 +335,18 @@ static bool check_ids(u32 old_id, u32 cur_id, struct bpf_idmap *idmap)
return false;
}
- /* Reached the end of known mappings; haven't seen this id before */
- if (idmap->cnt < BPF_ID_MAP_SIZE) {
- map[idmap->cnt].old = old_id;
- map[idmap->cnt].cur = cur_id;
- idmap->cnt++;
- return true;
- }
-
/*
- * idmap slots are bounded by the number of registers and stack slots.
- * Since referenced dynptrs acquire intermediate references that do
- * not live in either, so the map can be exhausted. Since it is unlikely,
- * fail the verification by treating the states as not equivalent.
+ * Reached the end of known mappings; haven't seen this id before. If
+ * the map cannot grow, treat the states as not equivalent, which only
+ * costs pruning.
*/
- return false;
+ if (!bpf_id_scratch_reserve((void **)&idmap->map, &idmap->cap, idmap->cnt, sizeof(*map)))
+ return false;
+ map = idmap->map;
+ map[idmap->cnt].old = old_id;
+ map[idmap->cnt].cur = cur_id;
+ idmap->cnt++;
+ return true;
}
/*
@@ -415,14 +412,14 @@ static void __clean_func_state(struct bpf_verifier_env *env,
* half_spi 2*i → lower half: slot_type[0..3] (closer to FP)
* half_spi 2*i+1 → upper half: slot_type[4..7] (farther from FP)
*/
- for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) {
+ for (i = 0; i < bpf_stack_nr_slots(st); i++) {
bool lo_live = bpf_stack_slot_alive(env, frame, i * 2);
bool hi_live = bpf_stack_slot_alive(env, frame, i * 2 + 1);
if (!hi_live || !lo_live) {
int start = !lo_live ? 0 : BPF_REG_SIZE / 2;
int end = !hi_live ? BPF_REG_SIZE : BPF_REG_SIZE / 2;
- u8 stype = st->stack[i].slot_type[7];
+ u8 stype = bpf_stack_slot(st, i)->slot_type[7];
/*
* Don't clear special slots.
@@ -442,7 +439,7 @@ static void __clean_func_state(struct bpf_verifier_env *env,
* rejecting as non-scalar register fills.
*/
if (!hi_live) {
- struct bpf_reg_state *spill = &st->stack[i].spilled_ptr;
+ struct bpf_reg_state *spill = &bpf_stack_slot(st, i)->spilled_ptr;
if (lo_live && stype == STACK_SPILL) {
if (spill->type != SCALAR_VALUE)
@@ -454,7 +451,7 @@ static void __clean_func_state(struct bpf_verifier_env *env,
if (bpf_register_is_null(spill))
continue;
for (j = 0; j < 4; j++) {
- u8 *t = &st->stack[i].slot_type[j];
+ u8 *t = &bpf_stack_slot(st, i)->slot_type[j];
if (*t == STACK_SPILL)
*t = STACK_MISC;
@@ -463,7 +460,7 @@ static void __clean_func_state(struct bpf_verifier_env *env,
bpf_mark_reg_not_init(env, spill);
}
for (j = start; j < end; j++)
- st->stack[i].slot_type[j] = STACK_POISON;
+ bpf_stack_slot(st, i)->slot_type[j] = STACK_POISON;
}
}
}
@@ -707,37 +704,38 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,
* didn't use them
*/
for (i = 0; i < old->allocated_stack; i++) {
+ struct bpf_stack_state *old_slot, *cur_slot;
struct bpf_reg_state *old_reg, *cur_reg;
int im = i % BPF_REG_SIZE;
+ u8 old_type;
spi = i / BPF_REG_SIZE;
+ old_slot = bpf_stack_slot(old, spi);
+ old_type = old_slot->slot_type[im];
+ cur_slot = i < cur->allocated_stack ? bpf_stack_slot(cur, spi) : NULL;
if (exact == EXACT) {
- u8 old_type = old->stack[spi].slot_type[i % BPF_REG_SIZE];
- u8 cur_type = i < cur->allocated_stack ?
- cur->stack[spi].slot_type[i % BPF_REG_SIZE] : STACK_INVALID;
+ u8 cur_type = cur_slot ? cur_slot->slot_type[im] : STACK_INVALID;
/* STACK_INVALID and STACK_POISON are equivalent for pruning */
if (old_type == STACK_POISON)
old_type = STACK_INVALID;
if (cur_type == STACK_POISON)
cur_type = STACK_INVALID;
- if (i >= cur->allocated_stack || old_type != cur_type)
+ if (!cur_slot || old_type != cur_type)
return false;
}
- if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID ||
- old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_POISON)
+ if (old_type == STACK_INVALID || old_type == STACK_POISON)
continue;
- if (env->allow_uninit_stack &&
- old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC)
+ if (env->allow_uninit_stack && old_type == STACK_MISC)
continue;
/* explored stack has more populated slots than current stack
* and these slots were used
*/
- if (i >= cur->allocated_stack)
+ if (!cur_slot)
return false;
/*
@@ -747,8 +745,8 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,
* regsafe() to ensure scalar ids are compared.
*/
if (im == 0 || im == 4) {
- old_reg = scalar_reg_for_stack(env, &old->stack[spi], im);
- cur_reg = scalar_reg_for_stack(env, &cur->stack[spi], im);
+ old_reg = scalar_reg_for_stack(env, old_slot, im);
+ cur_reg = scalar_reg_for_stack(env, cur_slot, im);
if (old_reg && cur_reg) {
if (!regsafe(env, old_reg, cur_reg, idmap, exact))
return false;
@@ -761,21 +759,19 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,
* it will be safe with zero-initialized stack.
* The opposite is not true
*/
- if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC &&
- cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO)
+ if (old_type == STACK_MISC && cur_slot->slot_type[im] == STACK_ZERO)
continue;
- if (old->stack[spi].slot_type[i % BPF_REG_SIZE] !=
- cur->stack[spi].slot_type[i % BPF_REG_SIZE])
+ if (old_type != cur_slot->slot_type[im])
/* Ex: old explored (safe) state has STACK_SPILL in
* this stack slot, but current has STACK_MISC ->
* this verifier states are not equivalent,
* return false to continue verification of this path
*/
return false;
- if (i % BPF_REG_SIZE != BPF_REG_SIZE - 1)
+ if (im != BPF_REG_SIZE - 1)
continue;
/* Both old and cur are having same slot_type */
- switch (old->stack[spi].slot_type[BPF_REG_SIZE - 1]) {
+ switch (old_type) {
case STACK_SPILL:
/* when explored and current stack slot are both storing
* spilled registers, check that stored pointers types
@@ -787,13 +783,13 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,
* such verifier states are not equivalent.
* return false to continue verification of this path
*/
- if (!regsafe(env, &old->stack[spi].spilled_ptr,
- &cur->stack[spi].spilled_ptr, idmap, exact))
+ if (!regsafe(env, &old_slot->spilled_ptr, &cur_slot->spilled_ptr,
+ idmap, exact))
return false;
break;
case STACK_DYNPTR:
- old_reg = &old->stack[spi].spilled_ptr;
- cur_reg = &cur->stack[spi].spilled_ptr;
+ old_reg = &old_slot->spilled_ptr;
+ cur_reg = &cur_slot->spilled_ptr;
if (old_reg->dynptr.type != cur_reg->dynptr.type ||
old_reg->dynptr.first_slot != cur_reg->dynptr.first_slot ||
!check_ids(old_reg->id, cur_reg->id, idmap) ||
@@ -801,8 +797,8 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,
return false;
break;
case STACK_ITER:
- old_reg = &old->stack[spi].spilled_ptr;
- cur_reg = &cur->stack[spi].spilled_ptr;
+ old_reg = &old_slot->spilled_ptr;
+ cur_reg = &cur_slot->spilled_ptr;
/* iter.depth is not compared between states as it
* doesn't matter for correctness and would otherwise
* prevent convergence; we maintain it only to prevent
@@ -818,8 +814,8 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,
return false;
break;
case STACK_IRQ_FLAG:
- old_reg = &old->stack[spi].spilled_ptr;
- cur_reg = &cur->stack[spi].spilled_ptr;
+ old_reg = &old_slot->spilled_ptr;
+ cur_reg = &cur_slot->spilled_ptr;
if (!check_ids(old_reg->id, cur_reg->id, idmap) ||
old_reg->irq.kfunc_class != cur_reg->irq.kfunc_class)
return false;
@@ -966,6 +962,27 @@ static bool func_states_equal(struct bpf_verifier_env *env, struct bpf_func_stat
return true;
}
+/*
+ * Make room for one more entry in an id scratch array, doubling it as needed.
+ * Returns false if it could not grow; callers then treat the id as unknown
+ * or the states as different, which is always safe.
+ */
+bool bpf_id_scratch_reserve(void **arr, u32 *cap, u32 cnt, size_t elem_size)
+{
+ u32 new_cap;
+ void *p;
+
+ if (cnt < *cap)
+ return true;
+ new_cap = *cap ? *cap * 2 : 64;
+ p = krealloc_array(*arr, new_cap, elem_size, GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+ if (!p)
+ return false;
+ *arr = p;
+ *cap = new_cap;
+ return true;
+}
+
static void reset_idmap_scratch(struct bpf_verifier_env *env)
{
struct bpf_idmap *idmap = &env->idmap_scratch;
@@ -1043,10 +1060,12 @@ static int propagate_precision(struct bpf_verifier_env *env,
first = false;
}
- for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
- if (!bpf_is_spilled_reg(&state->stack[i]))
+ for (i = 0; i < bpf_stack_nr_slots(state); i++) {
+ struct bpf_stack_state *ss = bpf_stack_slot(state, i);
+
+ if (!bpf_is_spilled_reg(ss))
continue;
- state_reg = &state->stack[i].spilled_ptr;
+ state_reg = &ss->spilled_ptr;
if (state_reg->type != SCALAR_VALUE ||
!state_reg->precise)
continue;
@@ -1192,15 +1211,17 @@ static bool iter_active_depths_differ(struct bpf_verifier_state *old, struct bpf
for (fr = old->curframe; fr >= 0; fr--) {
state = old->frame[fr];
- for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
- if (state->stack[i].slot_type[0] != STACK_ITER)
+ for (i = 0; i < bpf_stack_nr_slots(state); i++) {
+ struct bpf_stack_state *ss = bpf_stack_slot(state, i);
+
+ if (ss->slot_type[0] != STACK_ITER)
continue;
- slot = &state->stack[i].spilled_ptr;
+ slot = &ss->spilled_ptr;
if (slot->iter.state != BPF_ITER_STATE_ACTIVE)
continue;
- cur_slot = &cur->frame[fr]->stack[i].spilled_ptr;
+ cur_slot = &bpf_stack_slot(cur->frame[fr], i)->spilled_ptr;
if (cur_slot->iter.depth != slot->iter.depth)
return true;
}
@@ -1222,10 +1243,12 @@ static void mark_all_scalars_imprecise(struct bpf_verifier_env *env, struct bpf_
continue;
reg->precise = false;
}
- for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) {
- if (!bpf_is_spilled_reg(&func->stack[j]))
+ for (j = 0; j < bpf_stack_nr_slots(func); j++) {
+ struct bpf_stack_state *ss = bpf_stack_slot(func, j);
+
+ if (!bpf_is_spilled_reg(ss))
continue;
- reg = &func->stack[j].spilled_ptr;
+ reg = &ss->spilled_ptr;
if (reg->type != SCALAR_VALUE)
continue;
reg->precise = false;
@@ -1328,7 +1351,7 @@ int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx)
*/
if (is_iter_next_insn(env, insn_idx)) {
if (states_equal(env, &sl->state, cur, RANGE_WITHIN)) {
- struct bpf_func_state *cur_frame;
+ struct bpf_func_state *cur_frame, *iter_frame;
struct bpf_reg_state *iter_state, *iter_reg;
int spi;
@@ -1342,7 +1365,8 @@ int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx)
* no need for extra (re-)validations
*/
spi = bpf_get_spi(iter_reg->var_off.value);
- iter_state = &bpf_func(env, iter_reg)->stack[spi].spilled_ptr;
+ iter_frame = bpf_func(env, iter_reg);
+ iter_state = &bpf_stack_slot(iter_frame, spi)->spilled_ptr;
if (iter_state->iter.state == BPF_ITER_STATE_ACTIVE) {
loop = true;
goto hit;
@@ -1410,7 +1434,7 @@ int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx)
*/
err = 0;
if (bpf_is_jmp_point(env, env->insn_idx))
- err = bpf_push_jmp_history(env, cur, 0, 0, 0, 0);
+ err = bpf_push_jmp_history(env, cur, 0, 0, 0, NULL, 0);
err = err ? : propagate_precision(env, &sl->state, cur, NULL);
if (err)
return err;
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index fec5a1ae6a4da..2c495f66dc341 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -598,16 +598,17 @@ bool bpf_is_may_goto_insn(struct bpf_insn *insn)
static bool is_spi_bounds_valid(struct bpf_func_state *state, int spi, int nr_slots)
{
- int allocated_slots = state->allocated_stack / BPF_REG_SIZE;
+ int allocated_slots = bpf_stack_nr_slots(state);
- /* We need to check that slots between [spi - nr_slots + 1, spi] are
- * within [0, allocated_stack).
- *
- * Please note that the spi grows downwards. For example, a dynptr
- * takes the size of two stack slots; the first slot will be at
- * spi and the second slot will be at spi - 1.
- */
- return spi - nr_slots + 1 >= 0 && spi < allocated_slots;
+ /*
+ * We need to check that slots between [spi - nr_slots + 1, spi] are
+ * within [0, allocated_stack).
+ *
+ * Please note that the spi grows downwards. For example, a dynptr
+ * takes the size of two stack slots; the first slot will be at
+ * spi and the second slot will be at spi - 1.
+ */
+ return spi - nr_slots + 1 >= 0 && spi < allocated_slots;
}
static int stack_slot_obj_get_spi(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
@@ -751,8 +752,8 @@ static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_
return err;
for (i = 0; i < BPF_REG_SIZE; i++) {
- state->stack[spi].slot_type[i] = STACK_DYNPTR;
- state->stack[spi - 1].slot_type[i] = STACK_DYNPTR;
+ bpf_stack_slot(state, spi)->slot_type[i] = STACK_DYNPTR;
+ bpf_stack_slot(state, spi - 1)->slot_type[i] = STACK_DYNPTR;
}
type = arg_to_dynptr_type(arg_type);
@@ -785,8 +786,8 @@ static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_
parent_id = dynptr->parent_id;
}
- mark_dynptr_stack_regs(env, &state->stack[spi].spilled_ptr,
- &state->stack[spi - 1].spilled_ptr, type, parent_id);
+ mark_dynptr_stack_regs(env, &bpf_stack_slot(state, spi)->spilled_ptr,
+ &bpf_stack_slot(state, spi - 1)->spilled_ptr, type, parent_id);
return 0;
}
@@ -818,7 +819,7 @@ static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_re
* all clones and derived slices. For non-referenced dynptr, only
* the dynptr and slices derived from it will be invalidated.
*/
- reg = &state->stack[spi].spilled_ptr;
+ reg = &bpf_stack_slot(state, spi)->spilled_ptr;
return release_reference(env, dynptr_type_referenced(reg->dynptr.type)
? reg->parent_id
: reg->id);
@@ -857,6 +858,7 @@ static int dynptr_ref_cnt(struct bpf_verifier_env *env, int v_parent_id)
static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env,
struct bpf_func_state *state, int spi)
{
+ struct bpf_stack_state *slot = bpf_stack_slot(state, spi);
int err = 0;
/* We always ensure that STACK_DYNPTR is never set partially,
@@ -864,20 +866,22 @@ static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env,
* different for STACK_SPILL, where it may be only set for
* 1 byte, so code has to use is_spilled_reg.
*/
- if (state->stack[spi].slot_type[0] != STACK_DYNPTR)
+ if (slot->slot_type[0] != STACK_DYNPTR)
return 0;
/* Reposition spi to first slot */
- if (!state->stack[spi].spilled_ptr.dynptr.first_slot)
+ if (!slot->spilled_ptr.dynptr.first_slot) {
spi = spi + 1;
+ slot = bpf_stack_slot(state, spi);
+ }
/*
* A referenced dynptr can be overwritten only if there is at
* least one other dynptr sharing the same virtual ref parent,
* ensuring the reference can still be properly released.
*/
- if (dynptr_type_referenced(state->stack[spi].spilled_ptr.dynptr.type) &&
- dynptr_ref_cnt(env, state->stack[spi].spilled_ptr.parent_id) <= 1) {
+ if (dynptr_type_referenced(slot->spilled_ptr.dynptr.type) &&
+ dynptr_ref_cnt(env, slot->spilled_ptr.parent_id) <= 1) {
verbose(env, "cannot overwrite referenced dynptr\n");
bpf_diag_res(
env, env->insn_idx, "referenced dynptr overwrite",
@@ -887,7 +891,7 @@ static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env,
}
/* Invalidate the dynptr and any derived slices */
- err = release_reference(env, state->stack[spi].spilled_ptr.id);
+ err = release_reference(env, slot->spilled_ptr.id);
if (!err) {
mark_stack_slot_scratched(env, spi);
mark_stack_slot_scratched(env, spi - 1);
@@ -927,6 +931,7 @@ static bool is_dynptr_reg_valid_uninit(struct bpf_verifier_env *env, struct bpf_
static bool is_dynptr_reg_valid_init(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
{
struct bpf_func_state *state = bpf_func(env, reg);
+ struct bpf_stack_state *slot;
int i, spi;
/* This already represents first slot of initialized bpf_dynptr.
@@ -941,12 +946,13 @@ static bool is_dynptr_reg_valid_init(struct bpf_verifier_env *env, struct bpf_re
spi = dynptr_get_spi(env, reg);
if (spi < 0)
return false;
- if (!state->stack[spi].spilled_ptr.dynptr.first_slot)
+ slot = bpf_stack_slot(state, spi);
+ if (!slot->spilled_ptr.dynptr.first_slot)
return false;
for (i = 0; i < BPF_REG_SIZE; i++) {
- if (state->stack[spi].slot_type[i] != STACK_DYNPTR ||
- state->stack[spi - 1].slot_type[i] != STACK_DYNPTR)
+ if (slot->slot_type[i] != STACK_DYNPTR ||
+ bpf_stack_slot(state, spi - 1)->slot_type[i] != STACK_DYNPTR)
return false;
}
@@ -965,7 +971,7 @@ static enum bpf_dynptr_type dynptr_reg_type(struct bpf_verifier_env *env, struct
if (spi < 0)
return BPF_DYNPTR_TYPE_INVALID;
state = bpf_func(env, reg);
- return state->stack[spi].spilled_ptr.dynptr.type;
+ return bpf_stack_slot(state, spi)->spilled_ptr.dynptr.type;
}
static bool is_dynptr_type_expected(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
@@ -1005,7 +1011,7 @@ static int mark_stack_slots_iter(struct bpf_verifier_env *env,
return id;
for (i = 0; i < nr_slots; i++) {
- struct bpf_stack_state *slot = &state->stack[spi - i];
+ struct bpf_stack_state *slot = bpf_stack_slot(state, spi - i);
struct bpf_reg_state *st = &slot->spilled_ptr;
__mark_reg_known_zero(st);
@@ -1035,18 +1041,22 @@ static int unmark_stack_slots_iter(struct bpf_verifier_env *env,
struct bpf_reg_state *reg, int nr_slots)
{
struct bpf_func_state *state = bpf_func(env, reg);
- int spi, i, j;
+ int spi, i, j, err;
spi = iter_get_spi(env, reg, nr_slots);
if (spi < 0)
return spi;
for (i = 0; i < nr_slots; i++) {
- struct bpf_stack_state *slot = &state->stack[spi - i];
+ struct bpf_stack_state *slot = bpf_stack_slot(state, spi - i);
struct bpf_reg_state *st = &slot->spilled_ptr;
- if (i == 0)
- WARN_ON_ONCE(release_reference(env, st->id));
+ if (i == 0) {
+ err = release_reference(env, st->id);
+ if (err == -ENOMEM)
+ return err;
+ WARN_ON_ONCE(err);
+ }
bpf_mark_reg_not_init(env, st);
@@ -1076,7 +1086,7 @@ static bool is_iter_reg_valid_uninit(struct bpf_verifier_env *env,
return false;
for (i = 0; i < nr_slots; i++) {
- struct bpf_stack_state *slot = &state->stack[spi - i];
+ struct bpf_stack_state *slot = bpf_stack_slot(state, spi - i);
for (j = 0; j < BPF_REG_SIZE; j++)
if (slot->slot_type[j] == STACK_ITER)
@@ -1097,7 +1107,7 @@ static int is_iter_reg_valid_init(struct bpf_verifier_env *env, struct bpf_reg_s
return -EINVAL;
for (i = 0; i < nr_slots; i++) {
- struct bpf_stack_state *slot = &state->stack[spi - i];
+ struct bpf_stack_state *slot = bpf_stack_slot(state, spi - i);
struct bpf_reg_state *st = &slot->spilled_ptr;
if (st->type & PTR_UNTRUSTED)
@@ -1139,7 +1149,7 @@ static int mark_stack_slot_irq_flag(struct bpf_verifier_env *env,
if (id < 0)
return id;
- slot = &state->stack[spi];
+ slot = bpf_stack_slot(state, spi);
st = &slot->spilled_ptr;
__mark_reg_known_zero(st);
@@ -1166,7 +1176,7 @@ static int unmark_stack_slot_irq_flag(struct bpf_verifier_env *env, struct bpf_r
if (spi < 0)
return spi;
- slot = &state->stack[spi];
+ slot = bpf_stack_slot(state, spi);
st = &slot->spilled_ptr;
if (st->irq.kfunc_class != kfunc_class) {
@@ -1235,7 +1245,7 @@ static bool is_irq_flag_reg_valid_uninit(struct bpf_verifier_env *env, struct bp
if (spi < 0)
return false;
- slot = &state->stack[spi];
+ slot = bpf_stack_slot(state, spi);
for (i = 0; i < BPF_REG_SIZE; i++)
if (slot->slot_type[i] == STACK_IRQ_FLAG)
@@ -1254,7 +1264,7 @@ static int is_irq_flag_reg_valid_init(struct bpf_verifier_env *env, struct bpf_r
if (spi < 0)
return -EINVAL;
- slot = &state->stack[spi];
+ slot = bpf_stack_slot(state, spi);
st = &slot->spilled_ptr;
if (!st->id)
@@ -1401,7 +1411,7 @@ static int copy_reference_state(struct bpf_verifier_state *dst, const struct bpf
static int copy_stack_state(struct bpf_func_state *dst, const struct bpf_func_state *src)
{
- size_t n = src->allocated_stack / BPF_REG_SIZE;
+ size_t n = bpf_stack_nr_slots(src);
dst->stack = copy_array(dst->stack, src->stack, n, sizeof(struct bpf_stack_state),
GFP_KERNEL_ACCOUNT);
@@ -1440,7 +1450,7 @@ static int resize_reference_state(struct bpf_verifier_state *state, size_t n)
*/
static int grow_stack_state(struct bpf_verifier_env *env, struct bpf_func_state *state, int size)
{
- size_t old_n = state->allocated_stack / BPF_REG_SIZE, n;
+ size_t old_n = bpf_stack_nr_slots(state), n;
/* The stack size is always a multiple of BPF_REG_SIZE. */
size = round_up(size, BPF_REG_SIZE);
@@ -3368,26 +3378,25 @@ static void mark_non_stack_access(struct bpf_verifier_env *env, int idx)
env->insn_aux_data[idx].non_stack_access = true;
}
+/* Layout of one packed linked register in the jump history, see linked_regs_pack() */
#define LR_FRAMENO_BITS 4
-#define LR_SPI_BITS 6
-#define LR_ENTRY_BITS (LR_SPI_BITS + LR_FRAMENO_BITS + 1)
-#define LR_SIZE_BITS 4
-#define LR_FRAMENO_MASK ((1ull << LR_FRAMENO_BITS) - 1)
-#define LR_SPI_MASK ((1ull << LR_SPI_BITS) - 1)
-#define LR_SIZE_MASK ((1ull << LR_SIZE_BITS) - 1)
-#define LR_SPI_OFF LR_FRAMENO_BITS
-#define LR_IS_REG_OFF (LR_SPI_BITS + LR_FRAMENO_BITS)
-#define LINKED_REGS_MAX 5
+#define LR_INDEX_BITS 11
+#define LR_FRAMENO_MASK ((1u << LR_FRAMENO_BITS) - 1)
+#define LR_IS_REG BIT(LR_FRAMENO_BITS)
+#define LR_INDEX_OFF (LR_FRAMENO_BITS + 1)
+#define LR_INDEX_MASK ((1u << LR_INDEX_BITS) - 1)
+#define LINKED_REGS_MAX BPF_LINKED_REGS_MAX
static_assert(MAX_CALL_FRAMES <= (1 << LR_FRAMENO_BITS));
-static_assert(LINKED_REGS_MAX < (1 << LR_SIZE_BITS));
-static_assert(LINKED_REGS_MAX * LR_ENTRY_BITS + LR_SIZE_BITS <= 64);
+static_assert(MAX_BPF_REG <= (1 << LR_INDEX_BITS));
+static_assert(MAX_BPF_STACK_SLOTS <= (1 << LR_INDEX_BITS));
+static_assert(LR_INDEX_OFF + LR_INDEX_BITS <= 16);
struct linked_reg {
u8 frameno;
union {
- u8 spi;
- u8 regno;
+ u16 spi;
+ u16 regno;
};
bool is_reg;
};
@@ -3406,48 +3415,34 @@ static struct linked_reg *linked_regs_push(struct linked_regs *s)
}
/*
- * Use u64 as a vector of 5 11-bit values, use first 4-bits to track
- * number of elements currently in stack.
- * Pack one history entry for linked registers as 11 bits in the following format:
- * - 4-bits frameno
- * - 6-bits spi_or_reg
- * - 1-bit is_reg
+ * Pack linked registers for a jump history entry, one u16 each:
+ * - 4 bits frameno
+ * - 1 bit is_reg
+ * - 11 bits register or stack slot index
*/
-static u64 linked_regs_pack(struct linked_regs *s)
+static void linked_regs_pack(const struct linked_regs *s, u16 *packed)
{
- u64 val = 0;
int i;
for (i = 0; i < s->cnt; ++i) {
- struct linked_reg *e = &s->entries[i];
- u64 tmp = 0;
+ const struct linked_reg *e = &s->entries[i];
- tmp |= e->frameno;
- tmp |= e->spi << LR_SPI_OFF;
- tmp |= (e->is_reg ? 1 : 0) << LR_IS_REG_OFF;
-
- val <<= LR_ENTRY_BITS;
- val |= tmp;
+ packed[i] = e->frameno | (e->is_reg ? LR_IS_REG : 0) | (e->spi << LR_INDEX_OFF);
}
- val <<= LR_SIZE_BITS;
- val |= s->cnt;
- return val;
}
-static void linked_regs_unpack(u64 val, struct linked_regs *s)
+static void linked_regs_unpack(const struct bpf_jmp_history_entry *hist, struct linked_regs *s)
{
int i;
- s->cnt = val & LR_SIZE_MASK;
- val >>= LR_SIZE_BITS;
-
+ s->cnt = hist->linked_regs_cnt;
for (i = 0; i < s->cnt; ++i) {
struct linked_reg *e = &s->entries[i];
+ u16 packed = hist->linked_regs[i];
- e->frameno = val & LR_FRAMENO_MASK;
- e->spi = (val >> LR_SPI_OFF) & LR_SPI_MASK;
- e->is_reg = (val >> LR_IS_REG_OFF) & 0x1;
- val >>= LR_ENTRY_BITS;
+ e->frameno = packed & LR_FRAMENO_MASK;
+ e->is_reg = packed & LR_IS_REG;
+ e->spi = (packed >> LR_INDEX_OFF) & LR_INDEX_MASK;
}
}
@@ -3489,10 +3484,10 @@ void bpf_bt_sync_linked_regs(struct backtrack_state *bt, struct bpf_jmp_history_
bool some_precise = false;
int i;
- if (!hist || hist->linked_regs == 0)
+ if (!hist || !hist->linked_regs_cnt)
return;
- linked_regs_unpack(hist->linked_regs, &linked_regs);
+ linked_regs_unpack(hist, &linked_regs);
for (i = 0; i < linked_regs.cnt; ++i) {
struct linked_reg *e = &linked_regs.entries[i];
@@ -3589,17 +3584,18 @@ static void save_register_state(struct bpf_verifier_env *env,
int spi, struct bpf_reg_state *reg,
int size)
{
+ struct bpf_stack_state *slot = bpf_stack_slot(state, spi);
int i;
- bpf_diag_mod_begin(env, &state->stack[spi].spilled_ptr, reg, BPF_DIAG_MOD_SPILL);
- state->stack[spi].spilled_ptr = *reg;
+ bpf_diag_mod_begin(env, &slot->spilled_ptr, reg, BPF_DIAG_MOD_SPILL);
+ slot->spilled_ptr = *reg;
for (i = BPF_REG_SIZE; i > BPF_REG_SIZE - size; i--)
- state->stack[spi].slot_type[i - 1] = STACK_SPILL;
+ slot->slot_type[i - 1] = STACK_SPILL;
/* size < 8 bytes spill */
for (; i; i--)
- mark_stack_slot_misc(env, &state->stack[spi].slot_type[i - 1]);
+ mark_stack_slot_misc(env, &slot->slot_type[i - 1]);
bpf_diag_mod_end(env);
}
@@ -3641,14 +3637,15 @@ static void check_fastcall_stack_contract(struct bpf_verifier_env *env,
static void scrub_special_slot(struct bpf_func_state *state, int spi)
{
+ struct bpf_stack_state *slot = bpf_stack_slot(state, spi);
int i;
/* regular write of data into stack destroys any spilled ptr */
- state->stack[spi].spilled_ptr.type = NOT_INIT;
+ slot->spilled_ptr.type = NOT_INIT;
/* Mark slots as STACK_MISC if they belonged to spilled ptr/dynptr/iter. */
- if (is_stack_slot_special(&state->stack[spi]))
+ if (is_stack_slot_special(slot))
for (i = 0; i < BPF_REG_SIZE; i++)
- scrub_spilled_slot(&state->stack[spi].slot_type[i]);
+ scrub_spilled_slot(&slot->slot_type[i]);
}
/* check_stack_{read,write}_fixed_off functions track spill/fill of registers,
@@ -3666,13 +3663,15 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env,
struct bpf_reg_state *reg = NULL;
int insn_flags = INSN_F_STACK_ACCESS;
int hist_spi = spi, hist_frame = state->frameno;
+ struct bpf_stack_state *ss = bpf_stack_slot(state, spi);
- /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
+ /*
+ * caller checked that off % size == 0 and -env->stack_limit <= off < 0,
* so it's aligned access and [off, off + size) are within stack limits
*/
if (!env->allow_ptr_leaks &&
- bpf_is_spilled_reg(&state->stack[spi]) &&
- !bpf_is_spilled_scalar_reg(&state->stack[spi]) &&
+ bpf_is_spilled_reg(ss) &&
+ !bpf_is_spilled_scalar_reg(ss) &&
size != BPF_REG_SIZE) {
const char *reason;
@@ -3694,7 +3693,7 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env,
bool sanitize = reg && is_pointer_regtype(reg->type);
for (i = 0; i < size; i++) {
- u8 type = state->stack[spi].slot_type[(slot - i) %
+ u8 type = ss->slot_type[(slot - i) %
BPF_REG_SIZE];
if (type != STACK_MISC && type != STACK_ZERO) {
@@ -3723,7 +3722,7 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env,
save_register_state(env, state, spi, reg, size);
/* Break the relation on a narrowing spill. */
if (!reg_value_fits)
- state->stack[spi].spilled_ptr.id = 0;
+ ss->spilled_ptr.id = 0;
} else if (!reg && !(off % BPF_REG_SIZE) && is_bpf_st_mem(insn) &&
env->bpf_capable) {
struct bpf_reg_state *tmp_reg = &env->fake_reg[0];
@@ -3747,8 +3746,8 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env,
} else {
u8 type = STACK_MISC;
- if (bpf_is_spilled_reg(&state->stack[spi]))
- bpf_diag_record_scrub(env, &state->stack[spi].spilled_ptr,
+ if (bpf_is_spilled_reg(ss))
+ bpf_diag_record_scrub(env, &ss->spilled_ptr,
BPF_DIAG_MOD_WRITE);
scrub_special_slot(state, spi);
@@ -3769,13 +3768,13 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env,
/* Mark slots affected by this stack write. */
for (i = 0; i < size; i++)
- state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = type;
+ ss->slot_type[(slot - i) % BPF_REG_SIZE] = type;
insn_flags = 0; /* not a register spill */
}
if (insn_flags)
return bpf_push_jmp_history(env, env->cur_state, insn_flags,
- hist_spi, hist_frame, 0);
+ hist_spi, hist_frame, NULL, 0);
return 0;
}
@@ -3835,12 +3834,14 @@ static int check_stack_write_var_off(struct bpf_verifier_env *env,
check_fastcall_stack_contract(env, state, insn_idx, min_off);
/* Variable offset writes destroy any spilled pointers in range. */
for (i = min_off; i < max_off; i++) {
+ struct bpf_stack_state *ss;
u8 new_type, *stype;
int slot, spi;
slot = -i - 1;
spi = slot / BPF_REG_SIZE;
- stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE];
+ ss = bpf_stack_slot(state, spi);
+ stype = &ss->slot_type[slot % BPF_REG_SIZE];
mark_stack_slot_scratched(env, spi);
if (!env->allow_ptr_leaks && *stype != STACK_MISC && *stype != STACK_ZERO) {
@@ -3863,9 +3864,8 @@ static int check_stack_write_var_off(struct bpf_verifier_env *env,
/* If writing_zero and the spi slot contains a spill of value 0,
* maintain the spill type.
*/
- if (writing_zero && *stype == STACK_SPILL &&
- bpf_is_spilled_scalar_reg(&state->stack[spi])) {
- struct bpf_reg_state *spill_reg = &state->stack[spi].spilled_ptr;
+ if (writing_zero && *stype == STACK_SPILL && bpf_is_spilled_scalar_reg(ss)) {
+ struct bpf_reg_state *spill_reg = &ss->spilled_ptr;
if (tnum_is_const(spill_reg->var_off) && spill_reg->var_off.value == 0) {
zero_used = true;
@@ -3936,23 +3936,22 @@ static int mark_reg_stack_read(struct bpf_verifier_env *env,
{
struct bpf_verifier_state *vstate = env->cur_state;
struct bpf_func_state *state = vstate->frame[vstate->curframe];
- u64 zero_spill_mask = 0;
int i, slot, spi;
u8 *stype;
- int zeros = 0;
+ int zeros = 0, zero_spills = 0;
for (i = min_off; i < max_off; i++) {
slot = -i - 1;
spi = slot / BPF_REG_SIZE;
mark_stack_slot_scratched(env, spi);
- stype = ptr_state->stack[spi].slot_type;
+ stype = bpf_stack_slot(ptr_state, spi)->slot_type;
if (stype[slot % BPF_REG_SIZE] == STACK_ZERO) {
zeros++;
continue;
}
if (stype[slot % BPF_REG_SIZE] == STACK_SPILL &&
- bpf_register_is_null(&ptr_state->stack[spi].spilled_ptr)) {
- zero_spill_mask |= 1ull << spi;
+ bpf_register_is_null(&bpf_stack_slot(ptr_state, spi)->spilled_ptr)) {
+ zero_spills++;
zeros++;
continue;
}
@@ -3963,8 +3962,14 @@ static int mark_reg_stack_read(struct bpf_verifier_env *env,
* so the whole register == const_zero.
*/
__mark_reg_const_zero(env, &state->regs[dst_regno]);
- if (zero_spill_mask) {
- bpf_bt_set_frame_slot_mask(&env->bt, ptr_state->frameno, zero_spill_mask);
+ if (zero_spills) {
+ for (i = min_off; i < max_off; i++) {
+ slot = -i - 1;
+ spi = slot / BPF_REG_SIZE;
+ stype = bpf_stack_slot(ptr_state, spi)->slot_type;
+ if (stype[slot % BPF_REG_SIZE] == STACK_SPILL)
+ bpf_bt_set_frame_slot(&env->bt, ptr_state->frameno, spi);
+ }
return mark_chain_precision_batch(env, env->cur_state);
}
} else {
@@ -4012,9 +4017,10 @@ static int check_stack_read_fixed_off(struct bpf_verifier_env *env,
int err;
int insn_flags = INSN_F_STACK_ACCESS;
int hist_spi = spi, hist_frame = reg_state->frameno;
+ struct bpf_stack_state *ss = bpf_stack_slot(reg_state, spi);
- stype = reg_state->stack[spi].slot_type;
- reg = ®_state->stack[spi].spilled_ptr;
+ stype = ss->slot_type;
+ reg = &ss->spilled_ptr;
mark_stack_slot_scratched(env, spi);
check_fastcall_stack_contract(env, state, env->insn_idx, off);
@@ -4025,7 +4031,7 @@ static int check_stack_read_fixed_off(struct bpf_verifier_env *env,
if (dst_regno >= 0)
bpf_diag_mod_begin(env, &state->regs[dst_regno], reg, BPF_DIAG_MOD_WRITE);
- if (bpf_is_spilled_reg(®_state->stack[spi])) {
+ if (bpf_is_spilled_reg(ss)) {
u8 spill_size = 1;
for (i = BPF_REG_SIZE - 1; i > 0 && stype[i - 1] == STACK_SPILL; i--)
@@ -4151,7 +4157,7 @@ static int check_stack_read_fixed_off(struct bpf_verifier_env *env,
}
if (insn_flags)
return bpf_push_jmp_history(env, env->cur_state, insn_flags,
- hist_spi, hist_frame, 0);
+ hist_spi, hist_frame, NULL, 0);
return 0;
}
@@ -4341,7 +4347,7 @@ static int check_stack_arg_write(struct bpf_verifier_env *env, struct bpf_func_s
bpf_diag_mod_end(env);
state->no_stack_arg_load = true;
return bpf_push_jmp_history(env, env->cur_state,
- INSN_F_STACK_ARG_ACCESS, spi, 0, 0);
+ INSN_F_STACK_ARG_ACCESS, spi, 0, NULL, 0);
}
/*
@@ -4375,7 +4381,7 @@ static int check_stack_arg_read(struct bpf_verifier_env *env, struct bpf_func_st
cur->regs[dst_regno] = *arg;
bpf_diag_mod_end(env);
return bpf_push_jmp_history(env, env->cur_state,
- INSN_F_STACK_ARG_ACCESS, spi, 0, 0);
+ INSN_F_STACK_ARG_ACCESS, spi, 0, NULL, 0);
}
static int mark_stack_arg_precision(struct bpf_verifier_env *env, int arg_idx)
@@ -5519,7 +5525,7 @@ static int check_max_stack_depth_subprog(struct bpf_verifier_env *env, int idx,
if (subprog[idx].priv_stack_mode == PRIV_STACK_ADAPTIVE) {
if (subprog_depth > env->max_stack_depth)
env->max_stack_depth = subprog_depth;
- if (subprog_depth > MAX_BPF_STACK) {
+ if (subprog_depth > env->stack_limit) {
verbose(env, "stack size of subprog %d is %d. Too large\n",
idx, subprog_depth);
return -EACCES;
@@ -5528,7 +5534,7 @@ static int check_max_stack_depth_subprog(struct bpf_verifier_env *env, int idx,
depth += subprog_depth;
if (depth > env->max_stack_depth)
env->max_stack_depth = depth;
- if (depth > MAX_BPF_STACK) {
+ if (depth > env->stack_limit) {
total = 0;
for (tmp = idx; tmp >= 0; tmp = dinfo[tmp].caller)
total++;
@@ -6529,10 +6535,11 @@ static int check_ptr_to_map_access(struct bpf_verifier_env *env,
return 0;
}
-/* Check that the stack access at the given offset is within bounds. The
+/*
+ * Check that the stack access at the given offset is within bounds. The
* maximum valid offset is -1.
*
- * The minimum valid offset is -MAX_BPF_STACK for writes, and
+ * The minimum valid offset is -env->stack_limit for writes, and
* -state->allocated_stack for reads.
*/
static int check_stack_slot_within_bounds(struct bpf_verifier_env *env,
@@ -6543,7 +6550,7 @@ static int check_stack_slot_within_bounds(struct bpf_verifier_env *env,
int min_valid_off;
if (t == BPF_WRITE || env->allow_uninit_stack)
- min_valid_off = -MAX_BPF_STACK;
+ min_valid_off = -(int)env->stack_limit;
else
min_valid_off = -state->allocated_stack;
@@ -7409,6 +7416,7 @@ static int check_stack_range_initialized(
}
for (i = min_off; i < max_off + access_size; i++) {
+ struct bpf_stack_state *ss;
u8 *stype;
slot = -i - 1;
@@ -7418,7 +7426,8 @@ static int check_stack_range_initialized(
return -EFAULT;
}
- stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE];
+ ss = bpf_stack_slot(state, spi);
+ stype = &ss->slot_type[slot % BPF_REG_SIZE];
if (*stype == STACK_MISC)
goto mark;
if ((*stype == STACK_ZERO) ||
@@ -7430,13 +7439,13 @@ static int check_stack_range_initialized(
goto mark;
}
- if (bpf_is_spilled_reg(&state->stack[spi]) &&
- (state->stack[spi].spilled_ptr.type == SCALAR_VALUE ||
+ if (bpf_is_spilled_reg(ss) &&
+ (ss->spilled_ptr.type == SCALAR_VALUE ||
env->allow_ptr_leaks)) {
if (clobber) {
- __mark_reg_unknown(env, &state->stack[spi].spilled_ptr);
+ __mark_reg_unknown(env, &ss->spilled_ptr);
for (j = 0; j < BPF_REG_SIZE; j++)
- scrub_spilled_slot(&state->stack[spi].slot_type[j]);
+ scrub_spilled_slot(&ss->slot_type[j]);
}
goto mark;
}
@@ -8166,7 +8175,7 @@ static int process_dynptr_func(struct bpf_verifier_env *env, struct bpf_reg_stat
mark_stack_slots_scratched(env, spi, BPF_DYNPTR_NR_SLOTS);
- reg = &state->stack[spi].spilled_ptr;
+ reg = &bpf_stack_slot(state, spi)->spilled_ptr;
}
meta->dynptr.type = reg->dynptr.type;
@@ -8299,7 +8308,7 @@ static int process_iter_arg(struct bpf_verifier_env *env, struct bpf_reg_state *
/* remember meta->iter info for process_iter_next_call() */
meta->iter.spi = spi;
meta->iter.frameno = reg->frameno;
- update_ref_obj(&meta->ref_obj, &state->stack[spi].spilled_ptr);
+ update_ref_obj(&meta->ref_obj, &bpf_stack_slot(state, spi)->spilled_ptr);
if (is_iter_destroy_kfunc(meta)) {
err = unmark_stack_slots_iter(env, reg, nr_slots);
@@ -8376,16 +8385,15 @@ static int widen_imprecise_scalars(struct bpf_verifier_env *env,
&fold->regs[i],
&fcur->regs[i]);
- num_slots = min(fold->allocated_stack / BPF_REG_SIZE,
- fcur->allocated_stack / BPF_REG_SIZE);
+ num_slots = min(bpf_stack_nr_slots(fold), bpf_stack_nr_slots(fcur));
for (i = 0; i < num_slots; i++) {
- if (!bpf_is_spilled_reg(&fold->stack[i]) ||
- !bpf_is_spilled_reg(&fcur->stack[i]))
+ struct bpf_stack_state *old_ss = bpf_stack_slot(fold, i);
+ struct bpf_stack_state *cur_ss = bpf_stack_slot(fcur, i);
+
+ if (!bpf_is_spilled_reg(old_ss) || !bpf_is_spilled_reg(cur_ss))
continue;
- maybe_widen_reg(env,
- &fold->stack[i].spilled_ptr,
- &fcur->stack[i].spilled_ptr);
+ maybe_widen_reg(env, &old_ss->spilled_ptr, &cur_ss->spilled_ptr);
}
}
return 0;
@@ -8397,7 +8405,7 @@ static struct bpf_reg_state *get_iter_from_state(struct bpf_verifier_state *cur_
int iter_frameno = meta->iter.frameno;
int iter_spi = meta->iter.spi;
- return &cur_st->frame[iter_frameno]->stack[iter_spi].spilled_ptr;
+ return &bpf_stack_slot(cur_st->frame[iter_frameno], iter_spi)->spilled_ptr;
}
/* process_iter_next_call() is called when verifier gets to iterator's next
@@ -9189,6 +9197,7 @@ static int get_constant_map_key(struct bpf_verifier_env *env,
s64 *value)
{
struct bpf_func_state *state = bpf_func(env, key);
+ struct bpf_stack_state *ss;
struct bpf_reg_state *reg;
int slot, spi, off;
int spill_size = 0;
@@ -9208,7 +9217,8 @@ static int get_constant_map_key(struct bpf_verifier_env *env,
slot = -stack_off - 1;
spi = slot / BPF_REG_SIZE;
off = slot % BPF_REG_SIZE;
- stype = state->stack[spi].slot_type;
+ ss = bpf_stack_slot(state, spi);
+ stype = ss->slot_type;
/* First handle precisely tracked STACK_ZERO */
for (i = off; i >= 0 && stype[i] == STACK_ZERO; i--)
@@ -9219,14 +9229,14 @@ static int get_constant_map_key(struct bpf_verifier_env *env,
}
/* Check that stack contains a scalar spill of expected size */
- if (!bpf_is_spilled_scalar_reg(&state->stack[spi]))
+ if (!bpf_is_spilled_scalar_reg(ss))
return -EOPNOTSUPP;
for (i = off; i >= 0 && stype[i] == STACK_SPILL; i--)
spill_size++;
if (spill_size != key_size)
return -EOPNOTSUPP;
- reg = &state->stack[spi].spilled_ptr;
+ reg = &ss->spilled_ptr;
if (!tnum_is_const(reg->var_off))
/* Stack value not statically known */
return -EOPNOTSUPP;
@@ -10502,8 +10512,9 @@ static int idstack_push(struct bpf_idmap *idmap, u32 id)
if (idmap->map[i].old == id)
return 0;
- if (WARN_ON_ONCE(idmap->cnt >= BPF_ID_MAP_SIZE))
- return -EFAULT;
+ if (!bpf_id_scratch_reserve((void **)&idmap->map, &idmap->cap, idmap->cnt,
+ sizeof(*idmap->map)))
+ return -ENOMEM;
idmap->map[idmap->cnt++].old = id;
return 0;
@@ -14348,11 +14359,11 @@ s64 bpf_helper_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn
}
/*
* Size arg is const on each path but differs across merged
- * paths. MAX_BPF_STACK is a safe upper bound for reads.
+ * paths. Reads may extend anywhere up to the frame top.
*/
if (full_write)
return 0;
- return MAX_BPF_STACK;
+ return S64_MIN;
}
return S64_MIN;
case ARG_PTR_TO_DYNPTR:
@@ -14438,7 +14449,8 @@ s64 bpf_kfunc_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn *
size = (s64)aux->const_reg_vals[size_reg];
goto out;
}
- return MAX_BPF_STACK;
+ /* Unknown size: the read may extend anywhere up to the frame top. */
+ return S64_MIN;
}
/* fixed-size pointed-to type: resolve via BTF */
@@ -15180,7 +15192,8 @@ enum {
REASON_STACK = -5,
};
-static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
+static int retrieve_ptr_limit(const struct bpf_verifier_env *env,
+ const struct bpf_reg_state *ptr_reg,
u32 *alu_limit, bool mask_to_left)
{
u32 max = 0, ptr_limit = 0;
@@ -15192,7 +15205,7 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
* offset where we would need to deal with min/max bounds is
* currently prohibited for unprivileged.
*/
- max = MAX_BPF_STACK + mask_to_left;
+ max = env->stack_limit + mask_to_left;
ptr_limit = -ptr_reg->var_off.value;
break;
case PTR_TO_MAP_VALUE:
@@ -15312,7 +15325,7 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env,
(opcode == BPF_SUB && !off_is_neg);
}
- err = retrieve_ptr_limit(ptr_reg, &alu_limit, info->mask_to_left);
+ err = retrieve_ptr_limit(env, ptr_reg, &alu_limit, info->mask_to_left);
if (err < 0)
return err;
@@ -15442,7 +15455,7 @@ static int check_stack_access_for_ptr_arithmetic(
return -EACCES;
}
- if (off >= 0 || off < -MAX_BPF_STACK) {
+ if (off >= 0 || off < -(int)env->stack_limit) {
verbose(env, "R%d stack pointer arithmetic goes out of range, "
"prohibited for !root; off=%d\n", regno, off);
return -EACCES;
@@ -17777,10 +17790,12 @@ static void collect_linked_regs(struct bpf_verifier_env *env,
reg = &func->regs[j];
__collect_linked_regs(linked_regs, reg, id, i, j, true);
}
- for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) {
- if (!bpf_is_spilled_reg(&func->stack[j]))
+ for (j = 0; j < bpf_stack_nr_slots(func); j++) {
+ struct bpf_stack_state *ss = bpf_stack_slot(func, j);
+
+ if (!bpf_is_spilled_reg(ss))
continue;
- reg = &func->stack[j].spilled_ptr;
+ reg = &ss->spilled_ptr;
__collect_linked_regs(linked_regs, reg, id, i, j, false);
}
}
@@ -17800,7 +17815,7 @@ static void sync_linked_regs(struct bpf_verifier_env *env, struct bpf_verifier_s
for (i = 0; i < linked_regs->cnt; ++i) {
e = &linked_regs->entries[i];
reg = e->is_reg ? &vstate->frame[e->frameno]->regs[e->regno]
- : &vstate->frame[e->frameno]->stack[e->spi].spilled_ptr;
+ : &bpf_stack_slot(vstate->frame[e->frameno], e->spi)->spilled_ptr;
if (reg->type != SCALAR_VALUE || reg == known_reg)
continue;
if ((reg->id & ~BPF_ADD_CONST) != (known_reg->id & ~BPF_ADD_CONST))
@@ -17918,7 +17933,7 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
}
if (insn_flags) {
- err = bpf_push_jmp_history(env, this_branch, insn_flags, 0, 0, 0);
+ err = bpf_push_jmp_history(env, this_branch, insn_flags, 0, 0, NULL, 0);
if (err)
return err;
}
@@ -17988,7 +18003,10 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
* if parent state is created.
*/
if (linked_regs.cnt > 1) {
- err = bpf_push_jmp_history(env, this_branch, 0, 0, 0, linked_regs_pack(&linked_regs));
+ u16 packed[LINKED_REGS_MAX];
+
+ linked_regs_pack(&linked_regs, packed);
+ err = bpf_push_jmp_history(env, this_branch, 0, 0, 0, packed, linked_regs.cnt);
if (err)
return err;
}
@@ -18877,23 +18895,27 @@ static void adjust_btf_func(struct bpf_verifier_env *env)
aux->func_info[i].insn_off = env->subprog_info[i].start;
}
-/* Find id in idset and increment its count, or add new entry */
-static void idset_cnt_inc(struct bpf_idset *idset, u32 id)
+/*
+ * Find id in idset and increment its count, or add new entry. Returns false
+ * when a new id could not be recorded, which leaves the counts incomplete.
+ */
+static bool idset_cnt_inc(struct bpf_idset *idset, u32 id)
{
u32 i;
for (i = 0; i < idset->num_ids; i++) {
if (idset->entries[i].id == id) {
idset->entries[i].cnt++;
- return;
+ return true;
}
}
- /* New id */
- if (idset->num_ids < BPF_ID_MAP_SIZE) {
- idset->entries[idset->num_ids].id = id;
- idset->entries[idset->num_ids].cnt = 1;
- idset->num_ids++;
- }
+ if (!bpf_id_scratch_reserve((void **)&idset->entries, &idset->cap, idset->num_ids,
+ sizeof(*idset->entries)))
+ return false;
+ idset->entries[idset->num_ids].id = id;
+ idset->entries[idset->num_ids].cnt = 1;
+ idset->num_ids++;
+ return true;
}
/* Find id in idset and return its count, or 0 if not found */
@@ -18919,6 +18941,7 @@ void bpf_clear_singular_ids(struct bpf_verifier_env *env,
struct bpf_idset *idset = &env->idset_scratch;
struct bpf_func_state *func;
struct bpf_reg_state *reg;
+ bool complete = true;
idset->num_ids = 0;
@@ -18927,9 +18950,17 @@ void bpf_clear_singular_ids(struct bpf_verifier_env *env,
continue;
if (!reg->id)
continue;
- idset_cnt_inc(idset, reg->id & ~BPF_ADD_CONST);
+ complete &= idset_cnt_inc(idset, reg->id & ~BPF_ADD_CONST);
}));
+ /*
+ * An id that could not be recorded may be shared, and a later
+ * occurrence of it may have been recorded with a count of one. Without
+ * complete counts keep every id; clearing is only an optimization.
+ */
+ if (!complete)
+ return;
+
bpf_for_each_reg_in_vstate(st, func, reg, ({
if (reg->type != SCALAR_VALUE)
continue;
@@ -19386,7 +19417,7 @@ static int do_check(struct bpf_verifier_env *env)
}
if (bpf_is_jmp_point(env, env->insn_idx)) {
- err = bpf_push_jmp_history(env, state, 0, 0, 0, 0);
+ err = bpf_push_jmp_history(env, state, 0, 0, 0, NULL, 0);
if (err)
return err;
}
@@ -22255,6 +22286,26 @@ static int bpf_prog_verify_signature(struct bpf_verifier_env *env,
return err;
}
+/*
+ * bpf_clone_redirect() transmits from inside the program, so a tc egress or
+ * lwt_xmit program that redirects to its own device runs again on top of its
+ * own frame, and again from there, until the datapath's recursion limit drops
+ * the packet: ten frames deep. Ten MAX_BPF_STACK frames fit the kernel stack
+ * as they always did; ten MAX_BPF_STACK_JIT frames would not, so a program
+ * calling it keeps the smaller budget. The redirect helpers that transmit
+ * after the program has returned leave no frame behind.
+ */
+static bool bpf_prog_reenters_datapath(const struct bpf_prog *prog)
+{
+ const struct bpf_insn *insn = prog->insnsi;
+ int i;
+
+ for (i = 0; i < prog->len; i++, insn++)
+ if (bpf_helper_call(insn) && insn->imm == BPF_FUNC_clone_redirect)
+ return true;
+ return false;
+}
+
int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,
struct bpf_log_attr *attr_log)
{
@@ -22279,6 +22330,9 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,
env->bt.env = env;
env->prog = *prog;
env->ops = bpf_verifier_ops[env->prog->type];
+ env->stack_limit = bpf_prog_stack_limit(env->prog);
+ if (bpf_prog_reenters_datapath(env->prog))
+ env->stack_limit = MAX_BPF_STACK;
env->allow_ptr_leaks = bpf_allow_ptr_leaks(env->prog->aux->token);
env->allow_uninit_stack = bpf_allow_uninit_stack(env->prog->aux->token);
@@ -22602,6 +22656,8 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,
kvfree(env->gotox_tmp_buf);
kvfree(env->callx_edges);
kvfree(env->func_ptrs);
+ kfree(env->idmap_scratch.map);
+ kfree(env->idset_scratch.entries);
bpf_diag_free(env);
kvfree(env);
return ret;
diff --git a/tools/testing/selftests/bpf/prog_tests/struct_ops_private_stack.c b/tools/testing/selftests/bpf/prog_tests/struct_ops_private_stack.c
index 98db9bafa44b2..2b3ec2b790918 100644
--- a/tools/testing/selftests/bpf/prog_tests/struct_ops_private_stack.c
+++ b/tools/testing/selftests/bpf/prog_tests/struct_ops_private_stack.c
@@ -4,6 +4,7 @@
#include "struct_ops_private_stack.skel.h"
#include "struct_ops_private_stack_fail.skel.h"
#include "struct_ops_private_stack_recur.skel.h"
+#include "struct_ops_private_stack_large.skel.h"
#if defined(__x86_64__) || defined(__aarch64__) || defined(__powerpc64__)
static void test_private_stack(void)
@@ -78,6 +79,34 @@ static void test_private_stack_recur(void)
struct_ops_private_stack_recur__destroy(skel);
}
+/* Two frames of 2 KiB each on the private stack */
+static void test_private_stack_large(void)
+{
+ struct struct_ops_private_stack_large *skel;
+ struct bpf_link *link;
+
+ if (!is_large_stack_supported()) {
+ test__skip();
+ return;
+ }
+
+ skel = struct_ops_private_stack_large__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "struct_ops_private_stack_large__open_and_load"))
+ return;
+
+ link = bpf_map__attach_struct_ops(skel->maps.testmod_1);
+ if (!ASSERT_OK_PTR(link, "attach_struct_ops"))
+ goto cleanup;
+
+ ASSERT_OK(trigger_module_test_read(256), "trigger_read");
+
+ ASSERT_EQ(skel->bss->val, 100 + 30 + 12, "val");
+
+ bpf_link__destroy(link);
+cleanup:
+ struct_ops_private_stack_large__destroy(skel);
+}
+
static void __test_struct_ops_private_stack(void)
{
if (test__start_subtest("private_stack"))
@@ -86,6 +115,8 @@ static void __test_struct_ops_private_stack(void)
test_private_stack_fail();
if (test__start_subtest("private_stack_recur"))
test_private_stack_recur();
+ if (test__start_subtest("private_stack_large"))
+ test_private_stack_large();
}
#else
static void __test_struct_ops_private_stack(void)
diff --git a/tools/testing/selftests/bpf/prog_tests/tailcalls.c b/tools/testing/selftests/bpf/prog_tests/tailcalls.c
index aefb46778307a..88d223110ecd5 100644
--- a/tools/testing/selftests/bpf/prog_tests/tailcalls.c
+++ b/tools/testing/selftests/bpf/prog_tests/tailcalls.c
@@ -10,6 +10,7 @@
#include "tc_bpf2bpf.skel.h"
#include "tailcall_fail.skel.h"
#include "tailcall_cgrp_storage_owner.skel.h"
+#include "tailcall_large_stack.skel.h"
#include "tailcall_cgrp_storage_no_storage.skel.h"
#include "tailcall_cgrp_storage.skel.h"
#include "tailcall_sleepable.skel.h"
@@ -2025,6 +2026,45 @@ static void test_tailcall_bpf2bpf_fexit_links(void)
tailcall_bpf2bpf2__destroy(skel_tc);
}
+/*
+ * test_tailcall_large_stack runs a tail call made from a subprog with a 1536
+ * byte frame, under a 240-byte caller, into a program with a 2 KiB frame:
+ *
+ * entry (240) --call-> subprog_tail (1536) --tailcall-> classifier_0 (2048)
+ */
+static void test_tailcall_large_stack(void)
+{
+ struct tailcall_large_stack *skel;
+ int err, prog_fd, map_fd, key = 0;
+ char buff[128] = {};
+ LIBBPF_OPTS(bpf_test_run_opts, topts,
+ .data_in = buff,
+ .data_size_in = sizeof(buff),
+ .repeat = 1,
+ );
+
+ if (!is_large_stack_supported()) {
+ test__skip();
+ return;
+ }
+
+ skel = tailcall_large_stack__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "tailcall_large_stack__open_and_load"))
+ return;
+
+ prog_fd = bpf_program__fd(skel->progs.classifier_0);
+ map_fd = bpf_map__fd(skel->maps.jmp_table);
+ err = bpf_map_update_elem(map_fd, &key, &prog_fd, BPF_ANY);
+ if (!ASSERT_OK(err, "update jmp_table"))
+ goto out;
+
+ err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.entry), &topts);
+ ASSERT_OK(err, "test_run");
+ ASSERT_EQ(topts.retval, 42 + 7, "retval");
+out:
+ tailcall_large_stack__destroy(skel);
+}
+
void test_tailcalls(void)
{
if (test__start_subtest("tailcall_1"))
@@ -2096,4 +2136,6 @@ void test_tailcalls(void)
test_tailcall_callback();
if (test__start_subtest("tailcall_bpf2bpf_fexit_links"))
test_tailcall_bpf2bpf_fexit_links();
+ if (test__start_subtest("tailcall_large_stack"))
+ test_tailcall_large_stack();
}
diff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c
index 9c3ff7d83fbe3..dbf98751aae65 100644
--- a/tools/testing/selftests/bpf/prog_tests/verifier.c
+++ b/tools/testing/selftests/bpf/prog_tests/verifier.c
@@ -62,6 +62,7 @@
#include "verifier_kfunc_uninit.skel.h"
#include "verifier_kfunc_uninit_multi.skel.h"
#include "verifier_ld_ind.skel.h"
+#include "verifier_large_stack.skel.h"
#include "verifier_ldsx.skel.h"
#include "verifier_leak_ptr.skel.h"
#include "verifier_linked_scalars.skel.h"
@@ -244,6 +245,7 @@ void test_verifier_kfunc_uninit(void) { RUN_TESTS(verifier_kfunc_uninit)
void test_verifier_kfunc_uninit_multi(void) { RUN_TESTS(verifier_kfunc_uninit_multi); }
void test_verifier_load_acquire(void) { RUN(verifier_load_acquire); }
void test_verifier_ld_ind(void) { RUN(verifier_ld_ind); }
+void test_verifier_large_stack(void) { RUN(verifier_large_stack); }
void test_verifier_ldsx(void) { RUN(verifier_ldsx); }
void test_verifier_leak_ptr(void) { RUN(verifier_leak_ptr); }
void test_verifier_linked_scalars(void) { RUN(verifier_linked_scalars); }
diff --git a/tools/testing/selftests/bpf/progs/async_stack_depth.c b/tools/testing/selftests/bpf/progs/async_stack_depth.c
index 36734683acbdb..9cd874a90b39a 100644
--- a/tools/testing/selftests/bpf/progs/async_stack_depth.c
+++ b/tools/testing/selftests/bpf/progs/async_stack_depth.c
@@ -29,7 +29,49 @@ static int bad_timer_cb(void *map, int *key, struct bpf_timer *timer)
return buf[255] + timer_cb(NULL, NULL, NULL);
}
+/*
+ * The same shapes scaled to the 2 KiB budget of JITs with large stacks. The
+ * compiler caps a single function at 512 bytes, so the depth comes from a
+ * chain of 480-byte frames.
+ */
+__attribute__((noinline))
+static int timer_cb_large_0(void *map, int *key, struct bpf_timer *timer)
+{
+ volatile char buf[480] = {};
+ return buf[69];
+}
+
+__attribute__((noinline))
+static int timer_cb_large_1(void *map, int *key, struct bpf_timer *timer)
+{
+ volatile char buf[480] = {};
+ return buf[69] + timer_cb_large_0(map, key, timer);
+}
+
+__attribute__((noinline))
+static int timer_cb_large_2(void *map, int *key, struct bpf_timer *timer)
+{
+ volatile char buf[480] = {};
+ return buf[69] + timer_cb_large_1(map, key, timer);
+}
+
+__attribute__((noinline))
+static int timer_cb_large_3(void *map, int *key, struct bpf_timer *timer)
+{
+ volatile char buf[480] = {};
+ return buf[69] + timer_cb_large_2(map, key, timer);
+}
+
+/* 5 * 480 = 2400 bytes on its own */
+__attribute__((noinline))
+static int bad_timer_cb_large(void *map, int *key, struct bpf_timer *timer)
+{
+ volatile char buf[480] = {};
+ return buf[255] + timer_cb_large_3(map, key, timer);
+}
+
SEC("tc")
+__load_if_no_large_stack()
__failure __msg("combined stack size of 2 calls is")
int pseudo_call_check(struct __sk_buff *ctx)
{
@@ -44,7 +86,25 @@ int pseudo_call_check(struct __sk_buff *ctx)
return bpf_timer_set_callback(&elem->timer, timer_cb) + buf[0];
}
+/* main plus the four frames under timer_cb_large_3: 2400 bytes */
SEC("tc")
+__load_if_large_stack()
+__failure __msg("combined stack size of 5 calls is")
+int pseudo_call_check_large(struct __sk_buff *ctx)
+{
+ struct hmap_elem *elem;
+ volatile char buf[480] = {};
+
+ elem = bpf_map_lookup_elem(&hmap, &(int){0});
+ if (!elem)
+ return 0;
+
+ timer_cb_large_3(NULL, NULL, NULL);
+ return bpf_timer_set_callback(&elem->timer, timer_cb_large_3) + buf[0];
+}
+
+SEC("tc")
+__load_if_no_large_stack()
__failure __msg("combined stack size of 2 calls is")
int async_call_root_check(struct __sk_buff *ctx)
{
@@ -58,4 +118,19 @@ int async_call_root_check(struct __sk_buff *ctx)
return bpf_timer_set_callback(&elem->timer, bad_timer_cb) + buf[0];
}
+SEC("tc")
+__load_if_large_stack()
+__failure __msg("combined stack size of 5 calls is")
+int async_call_root_check_large(struct __sk_buff *ctx)
+{
+ struct hmap_elem *elem;
+ volatile char buf[480] = {};
+
+ elem = bpf_map_lookup_elem(&hmap, &(int){0});
+ if (!elem)
+ return 0;
+
+ return bpf_timer_set_callback(&elem->timer, bad_timer_cb_large) + buf[0];
+}
+
char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/bpf_misc.h b/tools/testing/selftests/bpf/progs/bpf_misc.h
index 2ced1d751acea..f3dbc3b59bff2 100644
--- a/tools/testing/selftests/bpf/progs/bpf_misc.h
+++ b/tools/testing/selftests/bpf/progs/bpf_misc.h
@@ -175,6 +175,9 @@
#define __prepare_priv __test_tag("test_prepare_priv")
#define __load_if_JITed() __test_tag("load_mode=jited")
#define __load_if_no_JITed() __test_tag("load_mode=no_jited")
+/* Whether programs may use more than 512 bytes of stack on this kernel and JIT */
+#define __load_if_large_stack() __test_tag("stack_mode=large")
+#define __load_if_no_large_stack() __test_tag("stack_mode=small")
#define __stderr(msg) __test_tag("test_expect_stderr=" msg)
#define __stderr_unpriv(msg) __test_tag("test_expect_stderr_unpriv=" msg)
#define __stdout(msg) __test_tag("test_expect_stdout=" msg)
diff --git a/tools/testing/selftests/bpf/progs/struct_ops_private_stack_fail.c b/tools/testing/selftests/bpf/progs/struct_ops_private_stack_fail.c
index 1442728f56046..c8cb35b37867b 100644
--- a/tools/testing/selftests/bpf/progs/struct_ops_private_stack_fail.c
+++ b/tools/testing/selftests/bpf/progs/struct_ops_private_stack_fail.c
@@ -4,6 +4,7 @@
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include "../test_kmods/bpf_testmod.h"
+#include "bpf_misc.h"
char _license[] SEC("license") = "GPL";
@@ -25,6 +26,44 @@ __noinline static int subprog1(int *a)
return subprog2(a, b);
}
+/*
+ * A chain of 480-byte frames under test_2, so that its call chain exceeds
+ * the 2 KiB budget of JITs with large stacks as well as the 512 bytes
+ * allowed elsewhere. The compiler caps a single function at 512 bytes, and
+ * the buffers are volatile so that it cannot shrink them.
+ */
+__noinline static int subprog_deep4(int *a)
+{
+ volatile char b[480] = {};
+
+ __sink(b[479]);
+ return a[10] + b[20];
+}
+
+__noinline static int subprog_deep3(int *a)
+{
+ volatile char b[480] = {};
+
+ __sink(b[479]);
+ return subprog_deep4(a) + b[20];
+}
+
+__noinline static int subprog_deep2(int *a)
+{
+ volatile char b[480] = {};
+
+ __sink(b[479]);
+ return subprog_deep3(a) + b[20];
+}
+
+__noinline static int subprog_deep1(int *a)
+{
+ volatile char b[480] = {};
+
+ __sink(b[479]);
+ return subprog_deep2(a) + b[20];
+}
+
SEC("struct_ops")
int BPF_PROG(test_1)
@@ -41,11 +80,13 @@ int BPF_PROG(test_1)
SEC("struct_ops")
int BPF_PROG(test_2)
{
- /* stack size 400 bytes */
- int a[100] = {};
+ /* stack size 476 bytes, over 2 KiB with the four 480-byte deep subprogs */
+ volatile char buf[376] = {};
+ int a[25] = {};
+ __sink(buf[375]);
a[10] = 3;
- val_j = subprog1(a);
+ val_j = subprog1(a) + subprog_deep1(a);
return 0;
}
diff --git a/tools/testing/selftests/bpf/progs/struct_ops_private_stack_large.c b/tools/testing/selftests/bpf/progs/struct_ops_private_stack_large.c
new file mode 100644
index 0000000000000..94a25a2cff6ec
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/struct_ops_private_stack_large.c
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "../test_kmods/bpf_testmod.h"
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+long val;
+
+/* On a private stack every frame gets the whole 2 KiB budget. */
+__used __naked
+static long frame_2048_leaf(void)
+{
+ asm volatile (" \
+ r1 = 30; \
+ *(u64 *)(r10 - 2048) = r1; \
+ r1 = 12; \
+ *(u64 *)(r10 - 8) = r1; \
+ r0 = *(u64 *)(r10 - 2048); \
+ r1 = *(u64 *)(r10 - 8); \
+ r0 += r1; \
+ exit; \
+" ::: __clobber_all);
+}
+
+/* test_1 is the member bpf_testmod requests a private stack for */
+SEC("struct_ops")
+__naked int test_1(void)
+{
+ asm volatile (" \
+ r1 = 100; \
+ *(u64 *)(r10 - 2048) = r1; \
+ call frame_2048_leaf; \
+ r1 = *(u64 *)(r10 - 2048); \
+ r0 += r1; \
+ r1 = %[val] ll; \
+ *(u64 *)(r1 + 0) = r0; \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm_addr(val)
+ : __clobber_all);
+}
+
+SEC(".struct_ops")
+struct bpf_testmod_ops3 testmod_1 = {
+ .test_1 = (void *)test_1,
+};
diff --git a/tools/testing/selftests/bpf/progs/tailcall_large_stack.c b/tools/testing/selftests/bpf/progs/tailcall_large_stack.c
new file mode 100644
index 0000000000000..977197dac5d37
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/tailcall_large_stack.c
@@ -0,0 +1,62 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+
+struct {
+ __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+ __uint(max_entries, 1);
+ __uint(key_size, sizeof(__u32));
+ __uint(value_size, sizeof(__u32));
+} jmp_table SEC(".maps");
+
+/* The tail call target sets up a 2 KiB frame of its own and uses both of its ends. */
+SEC("tc")
+__naked int classifier_0(void)
+{
+ asm volatile (" \
+ r1 = 42; \
+ *(u64 *)(r10 - 2048) = r1; \
+ r1 = 7; \
+ *(u64 *)(r10 - 8) = r1; \
+ r0 = *(u64 *)(r10 - 2048); \
+ r1 = *(u64 *)(r10 - 8); \
+ r0 += r1; \
+ exit; \
+" ::: __clobber_all);
+}
+
+/*
+ * The frame of the subprog doing the tail call is unwound by it, so it may be
+ * large; only the frames of its callers stay behind and are limited to 256
+ * bytes in total. Returns 1 when the tail call falls through.
+ */
+__used __naked
+static int subprog_tail(void)
+{
+ asm volatile (" \
+ r2 = 1; \
+ *(u64 *)(r10 - 1536) = r2; \
+ r2 = %[jmp_table] ll; \
+ r3 = 0; \
+ call %[bpf_tail_call]; \
+ r0 = 1; \
+ exit; \
+" :
+ : __imm(bpf_tail_call),
+ __imm_addr(jmp_table)
+ : __clobber_all);
+}
+
+SEC("tc")
+__naked int entry(void)
+{
+ asm volatile (" \
+ r2 = 2; \
+ *(u64 *)(r10 - 240) = r2; \
+ call subprog_tail; \
+ exit; \
+" ::: __clobber_all);
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/test_global_func1.c b/tools/testing/selftests/bpf/progs/test_global_func1.c
index fc69ff18880d5..f0eca282e0d43 100644
--- a/tools/testing/selftests/bpf/progs/test_global_func1.c
+++ b/tools/testing/selftests/bpf/progs/test_global_func1.c
@@ -48,8 +48,73 @@ int f3(int val, struct __sk_buff *skb, int var)
}
SEC("tc")
+__load_if_no_large_stack()
__failure __msg("combined stack size of 3 calls is")
int global_func1(struct __sk_buff *skb)
{
return f0(1, skb) + f1(skb) + f2(2, skb) + f3(3, skb, 4);
}
+
+/*
+ * A chain of five frames that stay under 512 bytes each but add up to more
+ * than the 2 KiB budget of JITs with large stacks; the chain also exceeds
+ * 512 bytes after two frames, so it is rejected everywhere.
+ */
+#define MAX_STACK_LARGE 480
+
+__attribute__ ((noinline))
+int g0(struct __sk_buff *skb)
+{
+ volatile char buf[MAX_STACK_LARGE] = {};
+
+ __sink(buf[MAX_STACK_LARGE - 1]);
+
+ return skb->len;
+}
+
+__attribute__ ((noinline))
+int g1(struct __sk_buff *skb)
+{
+ volatile char buf[MAX_STACK_LARGE] = {};
+
+ __sink(buf[MAX_STACK_LARGE - 1]);
+
+ return g0(skb) + skb->len;
+}
+
+__attribute__ ((noinline))
+int g2(struct __sk_buff *skb)
+{
+ volatile char buf[MAX_STACK_LARGE] = {};
+
+ __sink(buf[MAX_STACK_LARGE - 1]);
+
+ return g1(skb) + skb->len;
+}
+
+__attribute__ ((noinline))
+int g3(struct __sk_buff *skb)
+{
+ volatile char buf[MAX_STACK_LARGE] = {};
+
+ __sink(buf[MAX_STACK_LARGE - 1]);
+
+ return g2(skb) + skb->len;
+}
+
+__attribute__ ((noinline))
+int g4(struct __sk_buff *skb)
+{
+ volatile char buf[MAX_STACK_LARGE] = {};
+
+ __sink(buf[MAX_STACK_LARGE - 1]);
+
+ return g3(skb) + skb->len;
+}
+
+SEC("tc")
+__failure __msg("combined stack size of {{[0-9]+}} calls is")
+int global_func1_deep(struct __sk_buff *skb)
+{
+ return g4(skb);
+}
diff --git a/tools/testing/selftests/bpf/progs/test_global_func_deep_stack.c b/tools/testing/selftests/bpf/progs/test_global_func_deep_stack.c
index 1b634b543b629..edb8a223a3cb2 100644
--- a/tools/testing/selftests/bpf/progs/test_global_func_deep_stack.c
+++ b/tools/testing/selftests/bpf/progs/test_global_func_deep_stack.c
@@ -67,12 +67,30 @@ int XCAT(f, n)(unsigned long a) \
#define F_31 F_30 FN(31, 30)
#define F_32 F_31 FN(32, 31)
+/* Same, with a 480-byte frame, to exceed the 2 KiB budget of large stacks. */
+#define FNB(n, prev) \
+__attribute__((noinline)) \
+int XCAT(f, n)(unsigned long a) \
+{ \
+ volatile char buf[480] = {}; \
+ volatile long b = XCAT(f, prev)(a - 1); \
+ if (!b) \
+ return 0; \
+ return b + buf[479] + 1; \
+}
+
+#define F_33 F_32 FNB(33, 32)
+#define F_34 F_33 FNB(34, 33)
+#define F_35 F_34 FNB(35, 34)
+#define F_36 F_35 FNB(36, 35)
+#define F_37 F_36 FNB(37, 36)
+
#define CAT2(a, b) a ## b
#define XCAT2(a, b) CAT2(a, b)
#define F(n) XCAT2(F_, n)
-F(32)
+F(37)
/* Ensure that even 32 levels deep, the function verifies. */
SEC("syscall")
@@ -88,8 +106,21 @@ int global_func_deep_stack_success(struct __sk_buff *skb)
* the size.
*/
SEC("syscall")
+__load_if_no_large_stack()
__failure __msg("combined stack size of 34 calls")
int global_func_deep_stack_fail(struct __sk_buff *skb)
{
return f32(123);
}
+
+/*
+ * Five 480-byte frames on top of the chain: 5 * 480 + 33 * 16 = 2928 bytes,
+ * more than the 2 KiB budget of JITs with large stacks, and more than 512
+ * bytes after the second frame everywhere else.
+ */
+SEC("syscall")
+__failure __msg("combined stack size of {{[0-9]+}} calls")
+int global_func_deep_stack_fail_large(struct __sk_buff *skb)
+{
+ return f37(123);
+}
diff --git a/tools/testing/selftests/bpf/progs/verifier_callx.c b/tools/testing/selftests/bpf/progs/verifier_callx.c
index 238fc75ad154d..ea3d3f97da312 100644
--- a/tools/testing/selftests/bpf/progs/verifier_callx.c
+++ b/tools/testing/selftests/bpf/progs/verifier_callx.c
@@ -626,19 +626,64 @@ static unsigned long use_stack_304(void)
);
}
+
+/* Four 480-byte frames, deeper than any budget together with their caller */
+__naked __noinline __used
+static unsigned long use_stack_480_0(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "*(u64 *)(r10 - 480) = r0;"
+ "exit;"
+ );
+}
+
+__naked __noinline __used
+static unsigned long use_stack_480_1(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "*(u64 *)(r10 - 480) = r0;"
+ "call use_stack_480_0;"
+ "exit;"
+ );
+}
+
+__naked __noinline __used
+static unsigned long use_stack_480_2(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "*(u64 *)(r10 - 480) = r0;"
+ "call use_stack_480_1;"
+ "exit;"
+ );
+}
+
+__naked __noinline __used
+static unsigned long use_stack_480_3(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "*(u64 *)(r10 - 480) = r0;"
+ "call use_stack_480_2;"
+ "exit;"
+ );
+}
+
/* stack of the callee of callx is accounted */
SEC("socket")
-__failure __msg("combined stack size of 2 calls is")
+__failure __msg("combined stack size of {{[0-9]+}} calls is")
__naked void callx_stack_depth(void)
{
asm volatile (
"r0 = 0;"
- "*(u64 *)(r10 - 304) = r0;"
- "r2 = %[use_stack_304] ll;"
+ "*(u64 *)(r10 - 480) = r0;"
+ "r2 = %[use_stack_480_3] ll;"
"callx r2;"
"exit;"
:
- : __imm_addr(use_stack_304)
+ : __imm_addr(use_stack_480_3)
: __clobber_all);
}
@@ -655,19 +700,19 @@ static unsigned long apply_stack_304(void)
}
/*
- * The address of use_stack_304() is taken by the main prog that doesn't
+ * The address of use_stack_480_3() is taken by the main prog that doesn't
* use stack, but it is called from apply_stack_304().
*/
SEC("socket")
-__failure __msg("combined stack size of 3 calls is")
+__failure __msg("combined stack size of {{[0-9]+}} calls is")
__naked void callx_stack_depth_nested(void)
{
asm volatile (
- "r1 = %[use_stack_304] ll;"
+ "r1 = %[use_stack_480_3] ll;"
"call apply_stack_304;"
"exit;"
:
- : __imm_addr(use_stack_304)
+ : __imm_addr(use_stack_480_3)
: __clobber_all);
}
diff --git a/tools/testing/selftests/bpf/progs/verifier_callx_rodata.c b/tools/testing/selftests/bpf/progs/verifier_callx_rodata.c
index af1f9305da377..65d34769c8bfd 100644
--- a/tools/testing/selftests/bpf/progs/verifier_callx_rodata.c
+++ b/tools/testing/selftests/bpf/progs/verifier_callx_rodata.c
@@ -579,23 +579,58 @@ __naked void callx_rodata_recursion(void)
::: __clobber_all);
}
+
+/* Four 480-byte frames, deeper than any budget together with their caller */
__naked __noinline __used
-static unsigned long use_stack_304(void)
+static unsigned long use_stack_480_0(void)
{
asm volatile (
"r0 = 0;"
- "*(u64 *)(r10 - 304) = r0;"
+ "*(u64 *)(r10 - 480) = r0;"
+ "exit;"
+ );
+}
+
+__naked __noinline __used
+static unsigned long use_stack_480_1(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "*(u64 *)(r10 - 480) = r0;"
+ "call use_stack_480_0;"
+ "exit;"
+ );
+}
+
+__naked __noinline __used
+static unsigned long use_stack_480_2(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "*(u64 *)(r10 - 480) = r0;"
+ "call use_stack_480_1;"
+ "exit;"
+ );
+}
+
+__naked __noinline __used
+static unsigned long use_stack_480_3(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "*(u64 *)(r10 - 480) = r0;"
+ "call use_stack_480_2;"
"exit;"
);
}
/* stack of all possible callees is accounted */
SEC("socket")
-__failure __msg("combined stack size of 2 calls is")
+__failure __msg("combined stack size of {{[0-9]+}} calls is")
__naked void callx_rodata_stack_depth(void)
{
asm volatile (
- FUNC_TABLE2(tbl, ret0, use_stack_304)
+ FUNC_TABLE2(tbl, ret0, use_stack_480_3)
"r0 = 0;"
"*(u64 *)(r10 - 304) = r0;"
"call %[bpf_get_prandom_u32];"
@@ -613,11 +648,11 @@ __naked void callx_rodata_stack_depth(void)
/* stack of a callback that is read from the data is accounted too */
SEC("socket")
-__failure __msg("combined stack size of 2 calls is")
+__failure __msg("combined stack size of {{[0-9]+}} calls is")
__naked void callx_rodata_callback_stack_depth(void)
{
asm volatile (
- FUNC_TABLE2(tbl, use_stack_304, ret0)
+ FUNC_TABLE2(tbl, use_stack_480_3, ret0)
"r0 = 0;"
"*(u64 *)(r10 - 304) = r0;"
"r6 = tbl_%= ll;"
diff --git a/tools/testing/selftests/bpf/progs/verifier_large_stack.c b/tools/testing/selftests/bpf/progs/verifier_large_stack.c
new file mode 100644
index 0000000000000..c2a3ddf5ac913
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/verifier_large_stack.c
@@ -0,0 +1,425 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+
+/*
+ * Programs may use MAX_BPF_STACK_JIT (2 KiB) of stack on JITs that support
+ * large stacks, combined over a call chain, with no separate limit on a
+ * single frame. Interpreted programs and other JITs keep 512 bytes.
+ */
+
+SEC("socket")
+__description("single frame of 2048 bytes")
+__load_if_large_stack()
+__success __success_unpriv __retval(42)
+__naked void single_frame_2048(void)
+{
+ asm volatile (" \
+ r1 = r10; \
+ r1 += -2048; \
+ r0 = 42; \
+ *(u64*)(r1 + 0) = r0; \
+ r0 = *(u64*)(r1 + 0); \
+ exit; \
+" ::: __clobber_all);
+}
+
+SEC("socket")
+__description("single frame of 2048 bytes without large stack support")
+__load_if_no_large_stack()
+__failure __msg("invalid write to stack R1 off=-2048 size=8")
+__naked void single_frame_2048_no_large_stack(void)
+{
+ asm volatile (" \
+ r1 = r10; \
+ r1 += -2048; \
+ r0 = 42; \
+ *(u64*)(r1 + 0) = r0; \
+ exit; \
+" ::: __clobber_all);
+}
+
+__used __naked
+static void frame_512_leaf(void)
+{
+ asm volatile (" \
+ r1 = 1; \
+ *(u64 *)(r10 - 512) = r1; \
+ exit; \
+" ::: __clobber_all);
+}
+
+__used __naked
+static void frame_512_depth_2(void)
+{
+ asm volatile (" \
+ r1 = 2; \
+ *(u64 *)(r10 - 512) = r1; \
+ call frame_512_leaf; \
+ exit; \
+" ::: __clobber_all);
+}
+
+__used __naked
+static void frame_512_depth_3(void)
+{
+ asm volatile (" \
+ r1 = 3; \
+ *(u64 *)(r10 - 512) = r1; \
+ call frame_512_depth_2; \
+ exit; \
+" ::: __clobber_all);
+}
+
+__used __naked
+static void frame_512_depth_4(void)
+{
+ asm volatile (" \
+ r1 = 4; \
+ *(u64 *)(r10 - 512) = r1; \
+ call frame_512_depth_3; \
+ exit; \
+" ::: __clobber_all);
+}
+
+SEC("socket")
+__description("four frames of 512 bytes fit the 2 KiB budget")
+__load_if_large_stack()
+__success __log_level(4) __msg("stack depth max 2048")
+__naked void four_frames_of_512(void)
+{
+ asm volatile (" \
+ call frame_512_depth_4; \
+ r0 = 0; \
+ exit; \
+" ::: __clobber_all);
+}
+
+SEC("socket")
+__description("five frames of 512 bytes exceed the 2 KiB budget")
+__load_if_large_stack()
+__failure __msg("combined stack size of 5 calls is 2560. Too large")
+__naked void five_frames_of_512(void)
+{
+ asm volatile (" \
+ r1 = 5; \
+ *(u64 *)(r10 - 512) = r1; \
+ call frame_512_depth_4; \
+ r0 = 0; \
+ exit; \
+" ::: __clobber_all);
+}
+
+__used __naked
+static void frame_1536_leaf(void)
+{
+ asm volatile (" \
+ r1 = 1; \
+ *(u64 *)(r10 - 1536) = r1; \
+ exit; \
+" ::: __clobber_all);
+}
+
+SEC("socket")
+__description("512-byte frame calling a 1536-byte frame")
+__load_if_large_stack()
+__success __log_level(4) __msg("stack depth max 2048")
+__naked void uneven_frames_fit(void)
+{
+ asm volatile (" \
+ r1 = 2; \
+ *(u64 *)(r10 - 512) = r1; \
+ call frame_1536_leaf; \
+ r0 = 0; \
+ exit; \
+" ::: __clobber_all);
+}
+
+SEC("socket")
+__description("520-byte frame calling a 1536-byte frame")
+__load_if_large_stack()
+__failure __msg("combined stack size of 2 calls is 2064. Too large")
+__naked void uneven_frames_exceed(void)
+{
+ asm volatile (" \
+ r1 = 2; \
+ *(u64 *)(r10 - 520) = r1; \
+ call frame_1536_leaf; \
+ r0 = 0; \
+ exit; \
+" ::: __clobber_all);
+}
+
+#ifdef __BPF_FEATURE_MAY_GOTO
+/* may_goto adds its counter below the frame; a JIT does not hold that against the budget */
+SEC("socket")
+__description("frame of 2048 bytes with may_goto")
+__load_if_large_stack()
+__success __retval(42)
+__naked void frame_2048_with_may_goto(void)
+{
+ asm volatile (" \
+ r1 = r10; \
+ r1 += -2048; \
+ r0 = 42; \
+ *(u32*)(r1 + 0) = r0; \
+ may_goto l0_%=; \
+ r2 = 100; \
+ l0_%=: \
+ exit; \
+" ::: __clobber_all);
+}
+#endif
+
+SEC("socket")
+__description("variable offset write reaching 2048 bytes deep")
+__load_if_large_stack()
+__success
+__naked void var_off_write_to_2048(void)
+{
+ asm volatile (" \
+ call %[bpf_get_prandom_u32]; \
+ r0 &= 8; \
+ r2 = r10; \
+ r2 += -2048; \
+ r2 += r0; \
+ r1 = 0; \
+ *(u64*)(r2 + 0) = r1; \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm(bpf_get_prandom_u32)
+ : __clobber_all);
+}
+
+SEC("socket")
+__description("variable offset write reaching 2056 bytes deep")
+__load_if_large_stack()
+__failure __msg("invalid variable-offset write to stack R2")
+__naked void var_off_write_to_2056(void)
+{
+ asm volatile (" \
+ call %[bpf_get_prandom_u32]; \
+ r0 &= 8; \
+ r2 = r10; \
+ r2 += -2056; \
+ r2 += r0; \
+ r1 = 0; \
+ *(u64*)(r2 + 0) = r1; \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm(bpf_get_prandom_u32)
+ : __clobber_all);
+}
+
+/* Each frame of a private stack gets the whole budget. */
+__used __naked
+static void priv_stack_frame_2048(void)
+{
+ asm volatile (" \
+ r1 = 1; \
+ *(u64 *)(r10 - 2048) = r1; \
+ exit; \
+" ::: __clobber_all);
+}
+
+SEC("kprobe")
+__description("private stack: two frames of 2048 bytes")
+__load_if_large_stack()
+__arch_x86_64
+__arch_arm64
+__success __log_level(4)
+__msg("stack depth max 2048")
+__msg("subprog 0 (private_stack_two_frames) main {{.*}} stack 2048")
+__msg("subprog 1 (priv_stack_frame_2048) static {{.*}} stack 2048")
+__naked void private_stack_two_frames(void)
+{
+ asm volatile (" \
+ r1 = 2; \
+ *(u64 *)(r10 - 2048) = r1; \
+ call priv_stack_frame_2048; \
+ r0 = 0; \
+ exit; \
+" ::: __clobber_all);
+}
+
+struct {
+ __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+ __uint(max_entries, 1);
+ __uint(key_size, sizeof(__u32));
+ __uint(value_size, sizeof(__u32));
+} jmp_table SEC(".maps");
+
+/*
+ * A tail call unwinds the frame of the program doing it, so a large main
+ * frame is fine; the 256-byte rule only concerns the frames of callers of a
+ * subprog that tail calls.
+ */
+SEC("tc")
+__description("tail call from a 1 KiB frame")
+__load_if_large_stack()
+__success
+__naked void tail_call_from_large_frame(void)
+{
+ asm volatile (" \
+ r2 = 42; \
+ *(u64 *)(r10 - 1024) = r2; \
+ r2 = %[jmp_table] ll; \
+ r3 = 0; \
+ call %[bpf_tail_call]; \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm(bpf_tail_call),
+ __imm_addr(jmp_table)
+ : __clobber_all);
+}
+
+/*
+ * bpf_clone_redirect() can run the program again on top of its own frame,
+ * ten frames deep, so a program calling it keeps the 512-byte budget. The
+ * redirect that happens after the program returns does not.
+ */
+SEC("tc")
+__description("1 KiB frame with bpf_clone_redirect keeps the 512-byte budget")
+__load_if_large_stack()
+__failure __msg("invalid write to stack R1 off=-1024 size=8")
+__naked void clone_redirect_keeps_512(void)
+{
+ asm volatile (" \
+ r6 = r1; \
+ r1 = r10; \
+ r1 += -1024; \
+ r0 = 42; \
+ *(u64 *)(r1 + 0) = r0; \
+ r1 = r6; \
+ r2 = 1; \
+ r3 = 0; \
+ call %[bpf_clone_redirect]; \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm(bpf_clone_redirect)
+ : __clobber_all);
+}
+
+SEC("tc")
+__description("1 KiB frame with bpf_redirect keeps the 2 KiB budget")
+__load_if_large_stack()
+__success
+__naked void redirect_keeps_2048(void)
+{
+ asm volatile (" \
+ r1 = r10; \
+ r1 += -1024; \
+ r0 = 42; \
+ *(u64 *)(r1 + 0) = r0; \
+ r1 = 1; \
+ r2 = 0; \
+ call %[bpf_redirect]; \
+ exit; \
+" :
+ : __imm(bpf_redirect)
+ : __clobber_all);
+}
+
+/* Global subprogs are verified on their own but share the call chain budget. */
+__used __naked int global_frame_1536(void)
+{
+ asm volatile (" \
+ r1 = 1; \
+ *(u64 *)(r10 - 1536) = r1; \
+ r0 = 0; \
+ exit; \
+" ::: __clobber_all);
+}
+
+SEC("socket")
+__description("512-byte frame calling a 1536-byte global subprog")
+__load_if_large_stack()
+__success __log_level(4) __msg("stack depth max 2048")
+__naked void global_subprog_fits(void)
+{
+ asm volatile (" \
+ r1 = 2; \
+ *(u64 *)(r10 - 512) = r1; \
+ call global_frame_1536; \
+ r0 = 0; \
+ exit; \
+" ::: __clobber_all);
+}
+
+SEC("socket")
+__description("520-byte frame calling a 1536-byte global subprog")
+__load_if_large_stack()
+__failure __msg("combined stack size of 2 calls is 2064. Too large")
+__naked void global_subprog_exceeds(void)
+{
+ asm volatile (" \
+ r1 = 2; \
+ *(u64 *)(r10 - 520) = r1; \
+ call global_frame_1536; \
+ r0 = 0; \
+ exit; \
+" ::: __clobber_all);
+}
+
+/* Callback frames are part of the chain of the helper that calls them. */
+static __naked int loop_cb_1536(void)
+{
+ asm volatile (" \
+ r1 = 1; \
+ *(u64 *)(r10 - 1536) = r1; \
+ r0 = 0; \
+ exit; \
+" ::: __clobber_all);
+}
+
+SEC("socket")
+__description("512-byte frame with a 1536-byte bpf_loop callback")
+__load_if_large_stack()
+__success __log_level(4) __msg("stack depth max 2048")
+__naked void loop_callback_fits(void)
+{
+ asm volatile (" \
+ r1 = 2; \
+ *(u64 *)(r10 - 512) = r1; \
+ r1 = 1; \
+ r2 = %[loop_cb_1536]; \
+ r3 = 0; \
+ r4 = 0; \
+ call %[bpf_loop]; \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm_ptr(loop_cb_1536),
+ __imm(bpf_loop)
+ : __clobber_common);
+}
+
+SEC("socket")
+__description("520-byte frame with a 1536-byte bpf_loop callback")
+__load_if_large_stack()
+__failure __msg("combined stack size of 2 calls is 2064. Too large")
+__naked void loop_callback_exceeds(void)
+{
+ asm volatile (" \
+ r1 = 2; \
+ *(u64 *)(r10 - 520) = r1; \
+ r1 = 1; \
+ r2 = %[loop_cb_1536]; \
+ r3 = 0; \
+ r4 = 0; \
+ call %[bpf_loop]; \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm_ptr(loop_cb_1536),
+ __imm(bpf_loop)
+ : __clobber_common);
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/verifier_live_stack.c b/tools/testing/selftests/bpf/progs/verifier_live_stack.c
index 7a1a0670f851d..16b2b1e575342 100644
--- a/tools/testing/selftests/bpf/progs/verifier_live_stack.c
+++ b/tools/testing/selftests/bpf/progs/verifier_live_stack.c
@@ -318,7 +318,7 @@ struct {
} map_array SEC(".maps");
SEC("socket")
-__failure __msg("invalid read from stack R2 off=-1024 size=8")
+__failure __msg("invalid read from stack R2 off=-4096 size=8")
__flag(BPF_F_TEST_STATE_FREQ)
__naked unsigned long caller_stack_write_tail_call(void)
{
@@ -329,7 +329,7 @@ __naked unsigned long caller_stack_write_tail_call(void)
"if r0 != 42 goto 1f;"
"goto 2f;"
"1:"
- "*(u64 *)(r10 - 8) = -1024;"
+ "*(u64 *)(r10 - 8) = -4096;"
"2:"
"r1 = r6;"
"r2 = r10;"
@@ -1953,7 +1953,7 @@ static __used __naked void fwd_parent_key_to_helper(void)
SEC("socket")
__log_level(2)
__success
-__msg("call bpf_map_update_elem{{.*}}; use: fp1-8..-512 fp0-8")
+__msg("call bpf_map_update_elem{{.*}}; use: fp1-8..-{{(512|2048)}} fp0-8")
__naked void helper_arg_fallback_keeps_scanning(void)
{
asm volatile (
@@ -2267,7 +2267,7 @@ static __used __naked void merge_leaf_read(void)
SEC("socket")
__log_level(2)
__success
-__msg("call bpf_loop#181 ; use: fp2-8..-512 fp1-8..-512 fp0-8..-512")
+__msg("call bpf_loop#181 ; use: fp2-8..-{{(512|2048)}} fp1-8..-{{(512|2048)}} fp0-8..-{{(512|2048)}}")
__naked void bpf_loop_two_callbacks(void)
{
asm volatile (
@@ -2839,3 +2839,99 @@ static __used __naked void imprecise_dst_spill_join_sub(void)
:: __imm(bpf_get_prandom_u32)
: __clobber_all);
}
+
+/*
+ * A store that does not fully cover a 4-byte half-slot defines nothing, so a
+ * narrow store at the top of the frame must not turn any slot into a "def",
+ * least of all every slot of the frame: the earlier data at fp-8 stays live.
+ */
+SEC("socket")
+__log_level(2)
+__msg("0: (79) r0 = *(u64 *)(r10 -8) ; use: fp0-8")
+__msg("1: (73) *(u8 *)(r10 -1) = r0{{$}}")
+__msg("2: (6b) *(u16 *)(r10 -4) = r0{{$}}")
+__msg("3: (79) r0 = *(u64 *)(r10 -8) ; use: fp0-8")
+__naked void narrow_store_defines_nothing(void)
+{
+ asm volatile (
+ "r0 = *(u64 *)(r10 - 8);"
+ "*(u8 *)(r10 - 1) = r0;"
+ "*(u16 *)(r10 - 4) = r0;"
+ "r0 = *(u64 *)(r10 - 8);"
+ "exit;"
+ ::: __clobber_all);
+}
+
+/*
+ * The same callee instance is analyzed twice: the call sites are visited in
+ * postorder, so the second one goes first with a precise pointer 248 bytes
+ * into the main frame, and the first one then passes a pointer of unknown
+ * offset, which reads the whole frame. The precise pass stays within the
+ * first word of the masks on 64-bit, so the whole-frame pass is wider under
+ * every stack budget and merging the second into the first has to widen the
+ * masks while keeping the whole-frame read.
+ */
+SEC("socket")
+__log_level(2)
+__msg("stack use/def subprog#{{[0-9]+}} merge_read_all_callee (d2,cs{{[0-9]+}}):")
+__msg("(79) r0 = *(u64 *)(r1 +0){{.*}}; use: fp0-8..-{{(512|2048)}}")
+__naked void merge_keeps_whole_frame_read(void)
+{
+ asm volatile (
+ "r1 = 0;"
+ "*(u64 *)(r10 - 8) = r1;"
+ "*(u64 *)(r10 - 16) = r1;"
+ "*(u64 *)(r10 - 248) = r1;"
+ "call %[bpf_get_prandom_u32];"
+ "r0 &= 8;"
+ "r1 = r10;"
+ "r1 += -16;"
+ "r1 += r0;"
+ "call merge_read_all_mid;"
+ "r1 = r10;"
+ "r1 += -248;"
+ "call merge_read_all_mid;"
+ "r0 = 0;"
+ "exit;"
+ :: __imm(bpf_get_prandom_u32)
+ : __clobber_all);
+}
+
+static __used __naked void merge_read_all_mid(void)
+{
+ asm volatile (
+ "call merge_read_all_callee;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+static __used __naked void merge_read_all_callee(void)
+{
+ asm volatile (
+ "r0 = *(u64 *)(r1 + 0);"
+ "exit;"
+ ::: __clobber_all);
+}
+
+/*
+ * A frame pointer spilled below fp-512 is a spill like any other: the fill
+ * restores its identity, and a load through it reads only the slot it names
+ * instead of the whole frame.
+ */
+SEC("socket")
+__log_level(2)
+__load_if_large_stack()
+__msg("(79) r0 = *(u64 *)(r1 +0){{.*}}; use: fp0-8{{$}}")
+__naked void spill_below_512_stays_precise(void)
+{
+ asm volatile (
+ "r1 = 0;"
+ "*(u64 *)(r10 - 8) = r1;"
+ "r1 = r10;"
+ "r1 += -8;"
+ "*(u64 *)(r10 - 520) = r1;"
+ "r1 = *(u64 *)(r10 - 520);"
+ "r0 = *(u64 *)(r1 + 0);"
+ "exit;"
+ ::: __clobber_all);
+}
diff --git a/tools/testing/selftests/bpf/progs/verifier_raw_stack.c b/tools/testing/selftests/bpf/progs/verifier_raw_stack.c
index 9f0f48ecb4216..0fe631411b9cc 100644
--- a/tools/testing/selftests/bpf/progs/verifier_raw_stack.c
+++ b/tools/testing/selftests/bpf/progs/verifier_raw_stack.c
@@ -240,6 +240,7 @@ __naked void load_bytes_spilled_regs_data(void)
SEC("tc")
__description("raw_stack: skb_load_bytes, invalid access 1")
+__load_if_no_large_stack()
__failure __msg("invalid write to stack R3 off=-513 size=8")
__naked void load_bytes_invalid_access_1(void)
{
@@ -257,6 +258,26 @@ __naked void load_bytes_invalid_access_1(void)
: __clobber_all);
}
+SEC("tc")
+__description("raw_stack: skb_load_bytes, invalid access 1, large stack")
+__load_if_large_stack()
+__failure __msg("invalid write to stack R3 off=-2049 size=8")
+__naked void load_bytes_invalid_access_1_large(void)
+{
+ asm volatile (" \
+ r2 = 4; \
+ r6 = r10; \
+ r6 += -2049; \
+ r3 = r6; \
+ r4 = 8; \
+ call %[bpf_skb_load_bytes]; \
+ r0 = *(u64*)(r6 + 0); \
+ exit; \
+" :
+ : __imm(bpf_skb_load_bytes)
+ : __clobber_all);
+}
+
SEC("tc")
__description("raw_stack: skb_load_bytes, invalid access 2")
__failure __msg("invalid write to stack R3 off=-1 size=8")
diff --git a/tools/testing/selftests/bpf/progs/verifier_stack_ptr.c b/tools/testing/selftests/bpf/progs/verifier_stack_ptr.c
index 8e8cf8232255f..3e0bea9819cab 100644
--- a/tools/testing/selftests/bpf/progs/verifier_stack_ptr.c
+++ b/tools/testing/selftests/bpf/progs/verifier_stack_ptr.c
@@ -235,6 +235,7 @@ __naked void to_stack_check_low_1(void)
SEC("socket")
__description("PTR_TO_STACK check low 2")
+__load_if_no_large_stack()
__success __failure_unpriv
__msg_unpriv("R1 stack pointer arithmetic goes out of range")
__retval(42)
@@ -250,8 +251,27 @@ __naked void to_stack_check_low_2(void)
" ::: __clobber_all);
}
+SEC("socket")
+__description("PTR_TO_STACK check low 2, large stack")
+__load_if_large_stack()
+__success __failure_unpriv
+__msg_unpriv("R1 stack pointer arithmetic goes out of range")
+__retval(42)
+__naked void to_stack_check_low_2_large(void)
+{
+ asm volatile (" \
+ r1 = r10; \
+ r1 += -2049; \
+ r0 = 42; \
+ *(u8*)(r1 + 1) = r0; \
+ r0 = *(u8*)(r1 + 1); \
+ exit; \
+" ::: __clobber_all);
+}
+
SEC("socket")
__description("PTR_TO_STACK check low 3")
+__load_if_no_large_stack()
__failure __msg("invalid write to stack R1 off=-513 size=1")
__msg_unpriv("R1 stack pointer arithmetic goes out of range")
__naked void to_stack_check_low_3(void)
@@ -266,6 +286,23 @@ __naked void to_stack_check_low_3(void)
" ::: __clobber_all);
}
+SEC("socket")
+__description("PTR_TO_STACK check low 3, large stack")
+__load_if_large_stack()
+__failure __msg("invalid write to stack R1 off=-2049 size=1")
+__msg_unpriv("R1 stack pointer arithmetic goes out of range")
+__naked void to_stack_check_low_3_large(void)
+{
+ asm volatile (" \
+ r1 = r10; \
+ r1 += -2049; \
+ r0 = 42; \
+ *(u8*)(r1 + 0) = r0; \
+ r0 = *(u8*)(r1 + 0); \
+ exit; \
+" ::: __clobber_all);
+}
+
SEC("socket")
__description("PTR_TO_STACK check low 4")
__failure __msg("math between fp pointer")
@@ -483,6 +520,7 @@ l1_%=: r0 = 42; \
SEC("socket")
__description("PTR_TO_STACK stack size > 512")
+__load_if_no_large_stack()
__failure __msg("invalid write to stack R1 off=-520 size=8")
__naked void stack_check_size_gt_512(void)
{
@@ -495,6 +533,21 @@ __naked void stack_check_size_gt_512(void)
" ::: __clobber_all);
}
+SEC("socket")
+__description("PTR_TO_STACK stack size > 2048")
+__load_if_large_stack()
+__failure __msg("invalid write to stack R1 off=-2056 size=8")
+__naked void stack_check_size_gt_2048(void)
+{
+ asm volatile (" \
+ r1 = r10; \
+ r1 += -2056; \
+ r0 = 42; \
+ *(u64*)(r1 + 0) = r0; \
+ exit; \
+" ::: __clobber_all);
+}
+
#ifdef __BPF_FEATURE_MAY_GOTO
SEC("socket")
__description("PTR_TO_STACK stack size 512 with may_goto with jit")
diff --git a/tools/testing/selftests/bpf/progs/verifier_tailcall.c b/tools/testing/selftests/bpf/progs/verifier_tailcall.c
index b4acce60fb9b9..51687da972257 100644
--- a/tools/testing/selftests/bpf/progs/verifier_tailcall.c
+++ b/tools/testing/selftests/bpf/progs/verifier_tailcall.c
@@ -28,4 +28,61 @@ __naked void invalid_map_for_tail_call(void)
: __clobber_all);
}
+struct {
+ __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+ __uint(max_entries, 1);
+ __uint(key_size, sizeof(__u32));
+ __uint(value_size, sizeof(__u32));
+} jmp_table SEC(".maps");
+
+__used __naked
+static int subprog_tail_call(void)
+{
+ asm volatile (" \
+ r2 = %[jmp_table] ll; \
+ r3 = 0; \
+ call %[bpf_tail_call]; \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm(bpf_tail_call),
+ __imm_addr(jmp_table)
+ : __clobber_all);
+}
+
+/*
+ * A tail call unwinds only the frame of the subprog doing it, so the
+ * frames of its callers stay on the stack. With up to 33 tail calls in
+ * a chain the verifier caps the stack those frames may add up to at
+ * 256 bytes.
+ */
+SEC("tc")
+__description("tail call from subprog with 240 bytes of caller stack")
+__success
+__naked void tail_call_caller_stack_ok(void)
+{
+ asm volatile (" \
+ r2 = 42; \
+ *(u64 *)(r10 - 240) = r2; \
+ call subprog_tail_call; \
+ r0 = 0; \
+ exit; \
+" ::: __clobber_all);
+}
+
+SEC("tc")
+__description("tail call from subprog with 256 bytes of caller stack")
+__failure
+__msg("tail_calls are not allowed when call stack of previous frames is 256 bytes. Too large")
+__naked void tail_call_caller_stack_too_large(void)
+{
+ asm volatile (" \
+ r2 = 42; \
+ *(u64 *)(r10 - 256) = r2; \
+ call subprog_tail_call; \
+ r0 = 0; \
+ exit; \
+" ::: __clobber_all);
+}
+
char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/verifier_var_off.c b/tools/testing/selftests/bpf/progs/verifier_var_off.c
index a63e336750918..399884911ea53 100644
--- a/tools/testing/selftests/bpf/progs/verifier_var_off.c
+++ b/tools/testing/selftests/bpf/progs/verifier_var_off.c
@@ -406,6 +406,7 @@ __naked void zero_sized_access_max_out_of_bound(void)
SEC("lwt_in")
__description("indirect variable-offset stack access, min out of bound")
+__load_if_no_large_stack()
__failure __msg("invalid variable-offset read from stack R2")
__naked void access_min_out_of_bound(void)
{
@@ -433,6 +434,37 @@ __naked void access_min_out_of_bound(void)
: __clobber_all);
}
+SEC("lwt_in")
+__description("indirect variable-offset stack access, min out of bound, large stack")
+__load_if_large_stack()
+__failure __msg("invalid variable-offset read from stack R2")
+__naked void access_min_out_of_bound_large(void)
+{
+ asm volatile (" \
+ /* Fill the top 8 bytes of the stack */ \
+ r2 = 0; \
+ *(u64*)(r10 - 8) = r2; \
+ /* Get an unknown value */ \
+ r2 = *(u32*)(r1 + 0); \
+ /* Make it small and 4-byte aligned */ \
+ r2 &= 4; \
+ r2 -= 2052; \
+ /* \
+ * add it to fp. We now have either fp-2052 or fp-2048, but\
+ * we don't know which \
+ */ \
+ r2 += r10; \
+ /* dereference it indirectly */ \
+ r1 = %[map_hash_8b] ll; \
+ call %[bpf_map_lookup_elem]; \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm(bpf_map_lookup_elem),
+ __imm_addr(map_hash_8b)
+ : __clobber_all);
+}
+
SEC("cgroup/skb")
__description("indirect variable-offset stack access, min_off < min_initialized")
__success
diff --git a/tools/testing/selftests/bpf/test_loader.c b/tools/testing/selftests/bpf/test_loader.c
index a6e3fcc1079c6..25eeb1c1248b3 100644
--- a/tools/testing/selftests/bpf/test_loader.c
+++ b/tools/testing/selftests/bpf/test_loader.c
@@ -45,6 +45,11 @@ enum load_mode {
NO_JITED = 1 << 1,
};
+enum stack_mode {
+ LARGE_STACK = 1 << 0,
+ SMALL_STACK = 1 << 1,
+};
+
struct test_subspec {
char *name;
char *description;
@@ -70,6 +75,7 @@ struct test_spec {
int mode_mask;
int arch_mask;
int load_mask;
+ int stack_mask;
int linear_sz;
const char *skip_reason;
bool prepare_priv;
@@ -425,6 +431,7 @@ static int parse_test_spec(struct test_loader *tester,
int err = 0;
u32 arch_mask = 0;
u32 load_mask = 0;
+ u32 stack_mask = 0;
struct btf *btf;
enum arch arch;
@@ -620,6 +627,16 @@ static int parse_test_spec(struct test_loader *tester,
err = -EINVAL;
goto cleanup;
}
+ } else if ((val = str_has_pfx(s, "stack_mode="))) {
+ if (strcmp(val, "large") == 0) {
+ stack_mask = LARGE_STACK;
+ } else if (strcmp(val, "small") == 0) {
+ stack_mask = SMALL_STACK;
+ } else {
+ PRINT_FAIL("bad stack spec: '%s'", val);
+ err = -EINVAL;
+ goto cleanup;
+ }
} else if ((msg = str_has_pfx(s, "test_expect_stderr="))) {
err = push_disasm_msg(msg, &stderr_on_next_line,
&spec->priv.stderr);
@@ -659,6 +676,7 @@ static int parse_test_spec(struct test_loader *tester,
spec->arch_mask = arch_mask ?: -1;
spec->load_mask = load_mask ?: (JITED | NO_JITED);
+ spec->stack_mask = stack_mask ?: (LARGE_STACK | SMALL_STACK);
if (spec->mode_mask == 0)
spec->mode_mask = PRIV;
@@ -1331,6 +1349,7 @@ void run_subtest(struct test_loader *tester,
{
struct test_subspec *subspec = unpriv ? &spec->unpriv : &spec->priv;
int current_runtime = is_jit_enabled() ? JITED : NO_JITED;
+ int current_stack = is_large_stack_supported() ? LARGE_STACK : SMALL_STACK;
struct bpf_program *tprog = NULL, *tprog_iter;
struct bpf_link *link, *links[32] = {};
struct test_spec *spec_iter;
@@ -1360,6 +1379,11 @@ void run_subtest(struct test_loader *tester,
return;
}
+ if ((current_stack & spec->stack_mask) == 0) {
+ test__skip();
+ return;
+ }
+
if (unpriv) {
if (!can_execute_unpriv(tester, spec)) {
test__skip();
diff --git a/tools/testing/selftests/bpf/testing_helpers.c b/tools/testing/selftests/bpf/testing_helpers.c
index d1d60451c5bcc..47fe61a1ebff1 100644
--- a/tools/testing/selftests/bpf/testing_helpers.c
+++ b/tools/testing/selftests/bpf/testing_helpers.c
@@ -517,6 +517,47 @@ bool is_jit_enabled(void)
return enabled;
}
+/*
+ * Whether the kernel accepts a program using more than 512 bytes of stack,
+ * which depends on the JIT in use. Probed once with a program that stores
+ * at the 2 KiB depth. Only the verifier's verdict on that store is cached:
+ * a load that fails for another reason, such as a missing capability, is
+ * reported and probed again on the next call.
+ */
+bool is_large_stack_supported(void)
+{
+ static int supported = -1;
+ struct bpf_insn insns[] = {
+ BPF_ST_MEM(BPF_DW, BPF_REG_10, -2048, 0),
+ BPF_MOV64_IMM(BPF_REG_0, 0),
+ BPF_EXIT_INSN(),
+ };
+ char log[1024] = {};
+ LIBBPF_OPTS(bpf_prog_load_opts, opts,
+ .log_buf = log,
+ .log_size = sizeof(log),
+ .log_level = 1,
+ );
+ int fd;
+
+ if (supported >= 0)
+ return supported;
+
+ fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", insns, ARRAY_SIZE(insns),
+ &opts);
+ if (fd >= 0) {
+ close(fd);
+ supported = 1;
+ } else if (strstr(log, "invalid write to stack")) {
+ supported = 0;
+ } else {
+ fprintf(stderr, "%s: probe failed with errno %d, assuming 512 bytes:\n%s",
+ __func__, errno, log);
+ return false;
+ }
+ return supported;
+}
+
int stack_mprotect(void)
{
void *buf;
diff --git a/tools/testing/selftests/bpf/testing_helpers.h b/tools/testing/selftests/bpf/testing_helpers.h
index 1c58a2f08b645..f1505108e26a4 100644
--- a/tools/testing/selftests/bpf/testing_helpers.h
+++ b/tools/testing/selftests/bpf/testing_helpers.h
@@ -59,6 +59,7 @@ struct bpf_insn;
int get_xlated_program(int fd_prog, struct bpf_insn **buf, __u32 *cnt);
int testing_prog_flags(void);
bool is_jit_enabled(void);
+bool is_large_stack_supported(void);
int stack_mprotect(void);
/* Runs diff(1) on mismatch */
diff --git a/tools/testing/selftests/bpf/verifier/calls.c b/tools/testing/selftests/bpf/verifier/calls.c
index 8b94b87135bcf..0af237c02ddf9 100644
--- a/tools/testing/selftests/bpf/verifier/calls.c
+++ b/tools/testing/selftests/bpf/verifier/calls.c
@@ -1037,15 +1037,34 @@
.result = ACCEPT,
},
{
- "calls: stack overflow using two frames (pre-call access)",
+ /*
+ * Five 480-byte frames exceed the 2 KiB budget of JITs with large
+ * stacks, and two of them the 512 bytes allowed elsewhere.
+ */
+ "calls: stack overflow using five frames (pre-call access)",
.insns = {
/* prog 1 */
- BPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1),
BPF_EXIT_INSN(),
/* prog 2 */
- BPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1),
+ BPF_EXIT_INSN(),
+
+ /* prog 3 */
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1),
+ BPF_EXIT_INSN(),
+
+ /* prog 4 */
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1),
+ BPF_EXIT_INSN(),
+
+ /* prog 5 */
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(),
},
@@ -1054,15 +1073,30 @@
.result = REJECT,
},
{
- "calls: stack overflow using two frames (post-call access)",
+ "calls: stack overflow using five frames (post-call access)",
.insns = {
/* prog 1 */
BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 2),
- BPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
BPF_EXIT_INSN(),
/* prog 2 */
- BPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 2),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
+ BPF_EXIT_INSN(),
+
+ /* prog 3 */
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 2),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
+ BPF_EXIT_INSN(),
+
+ /* prog 4 */
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 2),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
+ BPF_EXIT_INSN(),
+
+ /* prog 5 */
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(),
},
@@ -1127,7 +1161,7 @@
.result = ACCEPT,
},
{
- "calls: stack depth check using three frames. test3",
+ "calls: stack depth check using five frames. test3",
.insns = {
/* main */
BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
@@ -1135,66 +1169,104 @@
BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 8), /* call B */
BPF_JMP_IMM(BPF_JGE, BPF_REG_6, 0, 1),
- BPF_ST_MEM(BPF_B, BPF_REG_10, -64, 0),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(),
/* A */
BPF_JMP_IMM(BPF_JLT, BPF_REG_1, 10, 1),
BPF_EXIT_INSN(),
- BPF_ST_MEM(BPF_B, BPF_REG_10, -224, 0),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
BPF_JMP_IMM(BPF_JA, 0, 0, -3),
/* B */
BPF_JMP_IMM(BPF_JGT, BPF_REG_1, 2, 1),
- BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, -6), /* call A */
- BPF_ST_MEM(BPF_B, BPF_REG_10, -256, 0),
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 2), /* call C */
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
+ BPF_EXIT_INSN(),
+ /* C */
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 2), /* call D */
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
+ BPF_EXIT_INSN(),
+ /* D */
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, -12), /* call A */
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
BPF_EXIT_INSN(),
},
.prog_type = BPF_PROG_TYPE_XDP,
- /* stack_main=64, stack_A=224, stack_B=256
- * and max(main+A, main+A+B) > 512
+ /*
+ * every frame is 480 bytes, main+A = 960 > 512 and
+ * max(main+A, main+B+C+D+A) = 2400 > 2048
*/
.errstr = "combined stack",
.result = REJECT,
},
{
- "calls: stack depth check using three frames. test4",
- /* void main(void) {
+ "calls: stack depth check using five frames. test4",
+ /*
+ * void main(void) {
* func1(0);
* func1(1);
* func2(1);
* }
- * void func1(int alloc_or_recurse) {
+ * void funcN(int alloc_or_recurse) { N = 1..4
* if (alloc_or_recurse) {
- * frame_pointer[-300] = 1;
+ * frame_pointer[-480] = 1;
* } else {
- * func2(alloc_or_recurse);
+ * funcN+1(alloc_or_recurse);
* }
* }
- * void func2(int alloc_or_recurse) {
+ * void func5(int alloc_or_recurse) {
* if (alloc_or_recurse) {
- * frame_pointer[-300] = 1;
+ * frame_pointer[-480] = 1;
* }
* }
+ * main also calls func2 to func5 with 1 so that every function has a
+ * path allocating its 480 bytes, and the chain adds up to 2400 bytes,
+ * more than the 2 KiB budget of JITs with large stacks, and to 960
+ * bytes after two frames, more than the 512 bytes allowed elsewhere.
*/
.insns = {
/* main */
BPF_MOV64_IMM(BPF_REG_1, 0),
- BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 6), /* call A */
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 12), /* call A */
BPF_MOV64_IMM(BPF_REG_1, 1),
- BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 4), /* call A */
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 10), /* call A */
+ BPF_MOV64_IMM(BPF_REG_1, 1),
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 13), /* call B */
+ BPF_MOV64_IMM(BPF_REG_1, 1),
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 16), /* call C */
BPF_MOV64_IMM(BPF_REG_1, 1),
- BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 7), /* call B */
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 19), /* call D */
+ BPF_MOV64_IMM(BPF_REG_1, 1),
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 22), /* call E */
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(),
/* A */
BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 2),
- BPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
BPF_EXIT_INSN(),
BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call B */
BPF_EXIT_INSN(),
/* B */
+ BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 2),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
+ BPF_EXIT_INSN(),
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call C */
+ BPF_EXIT_INSN(),
+ /* C */
+ BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 2),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
+ BPF_EXIT_INSN(),
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call D */
+ BPF_EXIT_INSN(),
+ /* D */
+ BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 2),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
+ BPF_EXIT_INSN(),
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call E */
+ BPF_EXIT_INSN(),
+ /* E */
BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 1),
- BPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
BPF_EXIT_INSN(),
},
.prog_type = BPF_PROG_TYPE_XDP,
|
| 3/2 | 2026/09/26 22:10 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 76677 output: 30 thoughts: 899 Thoughts: **Evaluating the Patch Series for Syzkaller Fuzzing** Alright, let's break this down. My goal is to determine if this patch series is a good candidate for fuzzing with syzkaller. First, I need to understand what's being changed. The target architecture is amd64, which is relevant for the context I'm in. I see a list of modified files – it's quite extensive, touching upon the BPF documentation, architecture-specific JIT compilers (arm64 and x86), the verifier, core BPF files, and some selftests. The core of the change seems to be introducing support for larger BPF stacks: moving from 512 bytes to 2KiB. This involves modifications to the BPF verifier (a notorious source of bugs and actively fuzzed by syzkaller), the x86 and arm64 JIT compilers, and various structures related to stack management within the verifier. Specifically, it seems stack bitmaps and arrays in the verifier are now dynamically sized, and there are changes in liveness tracking, stack slot checks, and R10 pointer arithmetic checks. The critical question now is, is this reachable code? Absolutely, and that's the key. BPF is a core subsystem and the verifier and x86 BPF JIT are directly reachable via `bpf(BPF_PROG_LOAD, ...)` calls. The verifier in particular is known to be bug-prone and extensively fuzzed by syzkaller. These changes to liveness analysis, backtracking, stack tracking, and bounds checks are practically screaming "fuzz me!" The more dynamic nature of stack management further increases the risk of subtle bugs. So, am I thinking this warrants fuzzing? Without a doubt, `WorthFuzzing=true`. Now, does it fit within the guidelines I am aware of? Definitely. This hits the "Core Subsystems" category because it modifies BPF, and it also falls into "Refactorings and Code Cleanups" as it restructures the underlying data structures and algorithms. The modifications to stack limits and pointer arithmetic are potential candidates for exploitable bugs. With `WorthFuzzing = true` confirmed, let's determine the necessary kernel configurations to enable BPF verifier and JIT on amd64. I'll need `BPF_SYSCALL` and `BPF_JIT`. Now, let's use `grepper` to inspect the relevant `Kconfig` files. |
| 4/2 | 2026/09/26 22:10 | tool | grepper |
0mError:no matches Args: {
"Expression": "config BPF_JIT",
"PathPrefix": "init/Kconfig"
}Results: {
"error": "no matches"
} |
| 5/2 | 2026/09/26 22:10 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 78152 output: 20 thoughts: 20 Thoughts: Here's my thought process, as if I were considering this myself: **Tracing Down BPF_JIT and BPF_SYSCALL Definitions** Okay, so I need to track down the definitions for `BPF_JIT` and `BPF_SYSCALL`. Since these are kernel-level constants or flags related to BPF, my first instinct is to dig into the kernel source code. I'm assuming, given the context, that I'm likely dealing with the Linux kernel here. My brain immediately starts scanning for likely locations. First, I would check the standard header files that are common for BPF-related definitions. Specifically, I'd look in headers like `linux/bpf.h`, `linux/filter.h`, and possibly even `uapi/linux/bpf.h`. The "uapi" location suggests user space interaction, which could be relevant if these are related to system calls. Then, depending on what I found (or didn't find), I'd start considering more general kernel source code locations. If I'm coming up empty-handed in the obvious header files, I'd probably go straight to the BPF implementation itself within the kernel. I'd be looking for the file(s) that handle the BPF interpreter or JIT compiler. I know from experience that kernel code tends to be well-commented, so if I find a definition, there should be some context around it. I'd also be thinking about any related system call handlers, as `BPF_SYSCALL` seems like it might relate to a specific system call implementation. I'd use grep to find these definitions, of course. |
| 6/2 | 2026/09/26 22:10 | tool | grepper |
0mArgs:{
"Expression": "config BPF_JIT"
}Results: {
"Output": "kernel/bpf/Kconfig=31=config BPF_SYSCALL\n--\nkernel/bpf/Kconfig-45-\nkernel/bpf/Kconfig:46:config BPF_JIT\nkernel/bpf/Kconfig-47-\tbool \"Enable BPF Just In Time compiler\"\n--\nkernel/bpf/Kconfig-61-\nkernel/bpf/Kconfig:62:config BPF_JIT_ALWAYS_ON\nkernel/bpf/Kconfig-63-\tbool \"Permanently enable BPF JIT and remove BPF interpreter\"\n--\nkernel/bpf/Kconfig-72-\nkernel/bpf/Kconfig:73:config BPF_JIT_DEFAULT_ON\nkernel/bpf/Kconfig-74-\tdef_bool ARCH_WANT_DEFAULT_BPF_JIT || BPF_JIT_ALWAYS_ON\n--\nkernel/bpf/Kconfig=96=config BPF_LSM\n--\nkernel/bpf/Kconfig-107-\nkernel/bpf/Kconfig:108:config BPF_JIT_KASAN\nkernel/bpf/Kconfig-109-\tbool\n"
} |
| 7/2 | 2026/09/26 22:10 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 78845 output: 34 thoughts: 22 Thoughts: Here's my summary, as if I were thinking these thoughts: **Examining the BPF Kernel Configuration** Okay, let's dive into this. To get a handle on the current BPF configuration options within the kernel, I need to take a look at `kernel/bpf/Kconfig`. Based on the context, and what I'm looking at, the key sections are likely to be somewhere around lines 31 through 75. That should cover the core configuration options that I'm interested in reviewing. This particular range is where I should focus my attention. I need to be sure I'm across the relevant options. |
| 8/2 | 2026/09/26 22:10 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/Kconfig",
"FirstLine": 25,
"LineCount": 60
}Results: {
"Contents": " 25:\t# default for kernels that were compiled with BPF JIT support.\n 26:\tconfig ARCH_WANT_DEFAULT_BPF_JIT\n 27:\t\tbool\n 28:\t\n 29:\tmenu \"BPF subsystem\"\n 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 70:\t\t is permanently set to 1 and setting any other value than that will\n 71:\t\t return failure.\n 72:\t\n 73:\tconfig BPF_JIT_DEFAULT_ON\n 74:\t\tdef_bool ARCH_WANT_DEFAULT_BPF_JIT || BPF_JIT_ALWAYS_ON\n 75:\t\tdepends on HAVE_EBPF_JIT \u0026\u0026 BPF_JIT\n 76:\t\n 77:\tconfig BPF_UNPRIV_DEFAULT_OFF\n 78:\t\tbool \"Disable unprivileged BPF by default\"\n 79:\t\tdefault y\n 80:\t\tdepends on BPF_SYSCALL\n 81:\t\thelp\n 82:\t\t Disables unprivileged BPF by default by setting the corresponding\n 83:\t\t /proc/sys/kernel/unprivileged_bpf_disabled knob to 2. An admin can\n 84:\t\t still reenable it by setting it to 0 later on, or permanently\n"
} |
| 9/2 | 2026/09/26 22:10 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 79953 output: 17 thoughts: 182 Thoughts: **Analyzing BPF Program Stack Limits** Alright, let's dive into this `bpf_prog_stack_limit` function. I need to understand how stack size is determined for BPF programs. The key here seems to be this `prog->jit_requested` flag. It looks like it differentiates between JIT-compiled programs, potentially on the host, versus offloaded programs. Hmm, the logic suggests that if JIT compilation is requested and the program isn't offloaded, and the JIT supports a large stack *and* subprogram tail calls, we can get `MAX_BPF_STACK_JIT`. Otherwise, we fall back to `MAX_BPF_STACK`. I need to figure out when this `jit_requested` flag is actually set. I'll need to trace its initialization within the kernel to fully grasp the control flow. |
| 10/2 | 2026/09/26 22:10 | tool | grepper |
0mArgs:{
"Expression": "jit_requested"
}Results: {
"Output": "arch/arc/net/bpf_jit_core.c=1327=static struct bpf_prog *do_normal_pass(struct bpf_prog *prog)\n--\narch/arc/net/bpf_jit_core.c-1331-\t/* Bail out if JIT is disabled. */\narch/arc/net/bpf_jit_core.c:1332:\tif (!prog-\u003ejit_requested)\narch/arc/net/bpf_jit_core.c-1333-\t\treturn prog;\n--\narch/arm/net/bpf_jit_32.c=2151=struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog)\n--\narch/arm/net/bpf_jit_32.c-2161-\t */\narch/arm/net/bpf_jit_32.c:2162:\tif (!prog-\u003ejit_requested)\narch/arm/net/bpf_jit_32.c-2163-\t\treturn prog;\n--\narch/arm64/net/bpf_jit_comp.c=2226=struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog)\n--\narch/arm64/net/bpf_jit_comp.c-2242-\narch/arm64/net/bpf_jit_comp.c:2243:\tif (!prog-\u003ejit_requested)\narch/arm64/net/bpf_jit_comp.c-2244-\t\treturn prog;\n--\narch/loongarch/net/bpf_jit.c=2191=struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog)\n--\narch/loongarch/net/bpf_jit.c-2204-\t */\narch/loongarch/net/bpf_jit.c:2205:\tif (!prog-\u003ejit_requested)\narch/loongarch/net/bpf_jit.c-2206-\t\treturn prog;\n--\narch/mips/net/bpf_jit_comp.c=939=struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog)\n--\narch/mips/net/bpf_jit_comp.c-951-\t */\narch/mips/net/bpf_jit_comp.c:952:\tif (!prog-\u003ejit_requested)\narch/mips/net/bpf_jit_comp.c-953-\t\treturn prog;\n--\narch/parisc/net/bpf_jit_core.c=44=struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog)\n--\narch/parisc/net/bpf_jit_core.c-51-\narch/parisc/net/bpf_jit_core.c:52:\tif (!prog-\u003ejit_requested)\narch/parisc/net/bpf_jit_core.c-53-\t\treturn prog;\n--\narch/powerpc/net/bpf_jit_comp.c=165=struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *fp)\n--\narch/powerpc/net/bpf_jit_comp.c-185-\narch/powerpc/net/bpf_jit_comp.c:186:\tif (!fp-\u003ejit_requested)\narch/powerpc/net/bpf_jit_comp.c-187-\t\treturn fp;\n--\narch/riscv/net/bpf_jit_core.c=44=struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog)\n--\narch/riscv/net/bpf_jit_core.c-52-\narch/riscv/net/bpf_jit_core.c:53:\tif (!prog-\u003ejit_requested)\narch/riscv/net/bpf_jit_core.c-54-\t\treturn prog;\n--\narch/s390/net/bpf_jit_comp.c=2386=struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *fp)\n--\narch/s390/net/bpf_jit_comp.c-2393-\narch/s390/net/bpf_jit_comp.c:2394:\tif (!fp-\u003ejit_requested)\narch/s390/net/bpf_jit_comp.c-2395-\t\treturn fp;\n--\narch/sparc/net/bpf_jit_comp_64.c=1480=struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog)\n--\narch/sparc/net/bpf_jit_comp_64.c-1489-\narch/sparc/net/bpf_jit_comp_64.c:1490:\tif (!prog-\u003ejit_requested)\narch/sparc/net/bpf_jit_comp_64.c-1491-\t\treturn prog;\n--\narch/x86/net/bpf_jit_comp.c=4283=struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog)\n--\narch/x86/net/bpf_jit_comp.c-4299-\narch/x86/net/bpf_jit_comp.c:4300:\tif (!prog-\u003ejit_requested)\narch/x86/net/bpf_jit_comp.c-4301-\t\treturn prog;\n--\narch/x86/net/bpf_jit_comp32.c=2521=struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog)\n--\narch/x86/net/bpf_jit_comp32.c-2530-\narch/x86/net/bpf_jit_comp32.c:2531:\tif (!prog-\u003ejit_requested)\narch/x86/net/bpf_jit_comp32.c-2532-\t\treturn prog;\n--\ninclude/linux/bpf.h=1924=struct bpf_prog {\n--\ninclude/linux/bpf.h-1926-\tu32\t\t\tjited:1,\t/* Is our filter JIT'ed? */\ninclude/linux/bpf.h:1927:\t\t\t\tjit_requested:1,/* archs need to JIT the prog */\ninclude/linux/bpf.h-1928-\t\t\t\tjit_required:1,\t/* program strictly requires JIT compiler */\n--\ninclude/linux/bpf_verifier.h=1292=static inline u32 bpf_prog_stack_limit(const struct bpf_prog *prog)\n--\ninclude/linux/bpf_verifier.h-1294-\t/* an offloaded program never runs on the host JIT, whatever it supports */\ninclude/linux/bpf_verifier.h:1295:\tif (prog-\u003ejit_requested \u0026\u0026 !bpf_prog_is_offloaded(prog-\u003eaux) \u0026\u0026\ninclude/linux/bpf_verifier.h-1296-\t bpf_jit_supports_large_stack() \u0026\u0026 bpf_jit_supports_subprog_tailcalls())\n--\ninclude/linux/filter.h=1492=static inline bool bpf_jit_blinding_enabled(struct bpf_prog *prog)\n--\ninclude/linux/filter.h-1499-\t\treturn false;\ninclude/linux/filter.h:1500:\tif (!prog-\u003ejit_requested)\ninclude/linux/filter.h-1501-\t\treturn false;\n--\nkernel/bpf/core.c=100=struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flags)\n--\nkernel/bpf/core.c-127-\tfp-\u003eaux-\u003eprog = fp;\nkernel/bpf/core.c:128:\tfp-\u003ejit_requested = ebpf_jit_enabled();\nkernel/bpf/core.c-129-\tfp-\u003ejit_required = IS_ENABLED(CONFIG_BPF_JIT_ALWAYS_ON);\n--\nkernel/bpf/core.c=179=int bpf_prog_alloc_jited_linfo(struct bpf_prog *prog)\nkernel/bpf/core.c-180-{\nkernel/bpf/core.c:181:\tif (!prog-\u003eaux-\u003enr_linfo || !prog-\u003ejit_requested)\nkernel/bpf/core.c-182-\t\treturn 0;\n--\nkernel/bpf/fixups.c=1184=static int jit_subprogs(struct bpf_verifier_env *env)\n--\nkernel/bpf/fixups.c-1284-\nkernel/bpf/fixups.c:1285:\t\tfunc[i]-\u003ejit_requested = 1;\nkernel/bpf/fixups.c-1286-\t\tfunc[i]-\u003eblinding_requested = prog-\u003eblinding_requested;\n--\nkernel/bpf/fixups.c=1457=int bpf_jit_subprogs(struct bpf_verifier_env *env)\n--\nkernel/bpf/fixups.c-1523-\t/* cleanup main prog to be interpreted */\nkernel/bpf/fixups.c:1524:\tprog-\u003ejit_requested = 0;\nkernel/bpf/fixups.c-1525-\tprog-\u003eblinding_requested = 0;\n--\nkernel/bpf/fixups.c=1529=int bpf_fixup_call_args(struct bpf_verifier_env *env)\n--\nkernel/bpf/fixups.c-1549-\nkernel/bpf/fixups.c:1550:\tif (env-\u003eprog-\u003ejit_requested \u0026\u0026\nkernel/bpf/fixups.c-1551-\t !bpf_prog_is_offloaded(env-\u003eprog-\u003eaux)) {\n--\nkernel/bpf/fixups.c=1632=int bpf_do_misc_fixups(struct bpf_verifier_env *env)\n--\nkernel/bpf/fixups.c-2060-\t\t\tif (env-\u003ebpf_capable \u0026\u0026 !prog-\u003eblinding_requested \u0026\u0026\nkernel/bpf/fixups.c:2061:\t\t\t prog-\u003ejit_requested \u0026\u0026\nkernel/bpf/fixups.c-2062-\t\t\t !bpf_map_key_poisoned(aux) \u0026\u0026\n--\nkernel/bpf/fixups.c-2171-\t\t */\nkernel/bpf/fixups.c:2172:\t\tif (prog-\u003ejit_requested \u0026\u0026 BITS_PER_LONG == 64 \u0026\u0026\nkernel/bpf/fixups.c-2173-\t\t (insn-\u003eimm == BPF_FUNC_map_lookup_elem ||\n--\nkernel/bpf/fixups.c-2270-\t\t/* Implement bpf_jiffies64 inline. */\nkernel/bpf/fixups.c:2271:\t\tif (prog-\u003ejit_requested \u0026\u0026 BITS_PER_LONG == 64 \u0026\u0026\nkernel/bpf/fixups.c-2272-\t\t insn-\u003eimm == BPF_FUNC_jiffies64) {\n--\nkernel/bpf/fixups.c-2451-\t\tif (IS_ENABLED(CONFIG_PERF_EVENTS) \u0026\u0026\nkernel/bpf/fixups.c:2452:\t\t prog-\u003ejit_requested \u0026\u0026 BITS_PER_LONG == 64 \u0026\u0026\nkernel/bpf/fixups.c-2453-\t\t insn-\u003eimm == BPF_FUNC_get_branch_snapshot) {\n--\nkernel/bpf/fixups.c-2506-\t\t/* Implement bpf_kptr_xchg inline */\nkernel/bpf/fixups.c:2507:\t\tif (prog-\u003ejit_requested \u0026\u0026 BITS_PER_LONG == 64 \u0026\u0026\nkernel/bpf/fixups.c-2508-\t\t insn-\u003eimm == BPF_FUNC_kptr_xchg \u0026\u0026\n--\nkernel/bpf/fixups.c-2540-\t\t\tstack_depth = subprogs[cur_subprog].stack_depth;\nkernel/bpf/fixups.c:2541:\t\t\tif (stack_depth \u003e MAX_BPF_STACK \u0026\u0026 !prog-\u003ejit_requested) {\nkernel/bpf/fixups.c-2542-\t\t\t\tverbose(env, \"stack size %d(extra %d) is too large\\n\",\n--\nkernel/bpf/verifier.c=437=static int bpf_compute_subprog_ret_regs(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-443-\nkernel/bpf/verifier.c:444:\tif (!env-\u003eprog-\u003ejit_requested || bpf_prog_is_offloaded(env-\u003eprog-\u003eaux))\nkernel/bpf/verifier.c-445-\t\treturn 0;\n--\nkernel/bpf/verifier.c=2870=int bpf_add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, u16 offset)\n--\nkernel/bpf/verifier.c-2890-\nkernel/bpf/verifier.c:2891:\t\tif (!env-\u003eprog-\u003ejit_requested) {\nkernel/bpf/verifier.c-2892-\t\t\tverbose(env, \"JIT is required for calling kernel function\\n\");\n--\nkernel/bpf/verifier.c=5431=static int round_up_stack_depth(struct bpf_verifier_env *env, int stack_depth)\nkernel/bpf/verifier.c-5432-{\nkernel/bpf/verifier.c:5433:\tif (env-\u003eprog-\u003ejit_requested)\nkernel/bpf/verifier.c-5434-\t\treturn round_up(stack_depth, 16);\n--\nkernel/bpf/verifier.c=6654=static int require_callx_jit(struct bpf_verifier_env *env)\nkernel/bpf/verifier.c-6655-{\nkernel/bpf/verifier.c:6656:\tif (!env-\u003eprog-\u003ejit_requested) {\nkernel/bpf/verifier.c-6657-\t\tverbose(env, \"JIT is required to use callx\\n\");\n--\nkernel/bpf/verifier.c=10043=bool bpf_allow_tail_call_in_subprogs(struct bpf_verifier_env *env)\nkernel/bpf/verifier.c-10044-{\nkernel/bpf/verifier.c:10045:\treturn env-\u003eprog-\u003ejit_requested \u0026\u0026\nkernel/bpf/verifier.c-10046-\t bpf_jit_supports_subprog_tailcalls();\n--\nkernel/bpf/verifier.c=18629=bool bpf_verifier_inlines_helper_call(struct bpf_verifier_env *env, s32 imm)\n--\nkernel/bpf/verifier.c-18637-#endif\nkernel/bpf/verifier.c:18638:\t\treturn env-\u003eprog-\u003ejit_requested \u0026\u0026 bpf_jit_supports_percpu_insn();\nkernel/bpf/verifier.c-18639-#endif\n--\nkernel/bpf/verifier.c=19765=static int check_map_prog_compatibility(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-19855-\t\t}\nkernel/bpf/verifier.c:19856:\t\tif (!env-\u003eprog-\u003ejit_requested) {\nkernel/bpf/verifier.c-19857-\t\t\tverbose(env, \"JIT is required to use arena\\n\");\n--\nkernel/bpf/verifier.c=21177=int bpf_check_attach_target(struct bpf_verifier_log *log,\n--\nkernel/bpf/verifier.c-21258-\t\t\t}\nkernel/bpf/verifier.c:21259:\t\t\tif (!prog-\u003ejit_requested) {\nkernel/bpf/verifier.c-21260-\t\t\t\tbpf_log(log,\n"
} |
| 11/2 | 2026/09/26 22:10 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 84564 output: 20 thoughts: 301 Thoughts: **My Analysis of eBPF JIT Stack Limits** Alright, let's break this down. My focus is immediately drawn to this `fp->jit_requested = ebpf_jit_enabled();` assignment. This is key, setting the stage for JIT decisions. Then I see `bpf_prog_stack_limit`, a critical function. The logic here is pretty straightforward. If `jit_requested` is true, meaning the JIT is, in principle, enabled, and the program isn't offloaded to some external hardware, and the JIT supports both large stacks and subprogram tail calls, then we use `MAX_BPF_STACK_JIT`. Otherwise, we fall back to `MAX_BPF_STACK`. This screams optimization based on JIT capabilities! Specifically on x86-64, I know `bpf_jit_supports_large_stack()` and `bpf_jit_supports_subprog_tailcalls()` will return true. So, on this architecture, the large stack is potentially available if the JIT is enabled. Now, `ebpf_jit_enabled()`... that's the gatekeeper. My understanding is that if the BPF JIT is enabled through any of the available mechanisms (like `BPF_JIT_ALWAYS_ON` or `/proc/sys/net/core/bpf_jit_enable`), then `jit_requested` gets set to true. This hinges on `CONFIG_BPF_SYSCALL` and `CONFIG_BPF_JIT`. Those are the essentials. Without them, we're not even in the JIT game. Let me now delve into `ebpf_jit_enabled()`'s implementation in the kernel to confirm these assumptions and explore any nuances. |
| 12/2 | 2026/09/26 22:11 | tool | grepper |
0mArgs:{
"Expression": "ebpf_jit_enabled"
}Results: {
"Output": "include/linux/filter.h=1473=static inline bool bpf_jit_is_ebpf(void)\n--\ninclude/linux/filter.h-1481-\ninclude/linux/filter.h:1482:static inline bool ebpf_jit_enabled(void)\ninclude/linux/filter.h-1483-{\n--\ninclude/linux/filter.h=1533=void bpf_prog_kallsyms_del(struct bpf_prog *fp);\n--\ninclude/linux/filter.h-1536-\ninclude/linux/filter.h:1537:static inline bool ebpf_jit_enabled(void)\ninclude/linux/filter.h-1538-{\n--\nkernel/bpf/core.c=100=struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flags)\n--\nkernel/bpf/core.c-127-\tfp-\u003eaux-\u003eprog = fp;\nkernel/bpf/core.c:128:\tfp-\u003ejit_requested = ebpf_jit_enabled();\nkernel/bpf/core.c-129-\tfp-\u003ejit_required = IS_ENABLED(CONFIG_BPF_JIT_ALWAYS_ON);\n"
} |
| 13/2 | 2026/09/26 22:11 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 85591 output: 36 thoughts: 15 Thoughts: Okay, let's dive into this `ebpf_jit_enabled` thing. As an expert, I'm already familiar with eBPF, but it's always good to refresh my memory and understand the specifics of this flag. My initial thought process is something like this: **Investigating the `ebpf_jit_enabled` Flag** Right, so `ebpf_jit_enabled`. This likely controls whether the Just-In-Time (JIT) compiler for eBPF programs is active. The JIT is crucial for performance, translating eBPF bytecode into native machine code for the target architecture. Without it, you're looking at an interpreter, which is significantly slower, especially for complex programs or high-throughput scenarios. I need to understand the implications of this flag. Is it a global enable/disable, or are there more granular controls? Are there any security considerations? Disabling the JIT might be a security measure to mitigate potential vulnerabilities in the JIT compiler itself, though that comes at a significant performance cost. Conversely, leaving it enabled, while faster, opens the door to potential exploits. What's the trade-off here? My next step would be to look at the surrounding code or documentation to see how this flag is used and how it influences the eBPF execution flow. I would need to find out: * **Where is this flag defined or declared?** What are its default values and possible states? * **How is it being used?** Is it a simple boolean, or does it control other settings or behaviors? * **What are the performance implications?** Are there benchmarks associated with disabling it? * **What are the security implications?** Are there security risks associated with the JIT that might prompt its disablement? I'll be looking for references to security hardening guides, or documentation that explicitly calls out potential attack surfaces related to JIT compilation. It's a key consideration. Ultimately, I want to understand *why* this flag exists and what impact it has on the eBPF system I'm working with. This information is key to making informed decisions about performance tuning, security hardening, or troubleshooting. Knowing the interaction of this flag with other parts of the system is essential. |
| 14/2 | 2026/09/26 22:11 | tool | read-file |
0mArgs:{
"File": "include/linux/filter.h",
"FirstLine": 1480,
"LineCount": 15
}Results: {
"Contents": "1480:\t}\n1481:\t\n1482:\tstatic inline bool ebpf_jit_enabled(void)\n1483:\t{\n1484:\t\treturn bpf_jit_enable \u0026\u0026 bpf_jit_is_ebpf();\n1485:\t}\n1486:\t\n1487:\tstatic inline bool bpf_prog_ebpf_jited(const struct bpf_prog *fp)\n1488:\t{\n1489:\t\treturn fp-\u003ejited \u0026\u0026 bpf_jit_is_ebpf();\n1490:\t}\n1491:\t\n1492:\tstatic inline bool bpf_jit_blinding_enabled(struct bpf_prog *prog)\n1493:\t{\n1494:\t\t/* These are the prerequisites, should someone ever have the\n"
} |
| 15/2 | 2026/09/26 22:11 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 86343 output: 24 thoughts: 2833 Thoughts: **BPF Stack Limit Expansion: Focus Symbol Identification** Alright, let's break down this commit and figure out the best focus symbols for our fuzzer. We're dealing with an expansion of the BPF stack limit from 512 to 2048 bytes, which necessitates changes throughout the verifier and related subsystems. So, the goal is to identify functions that specifically handle this change or are entry points for the modified logic. First, I looked at the configuration options: `CONFIG_BPF_SYSCALL` and `CONFIG_BPF_JIT` are key, with optional `BPF_JIT_ALWAYS_ON`. Then, I went through the diff. The files touched tell the story: changes in JIT compilation (`arch/x86/net/bpf_jit_comp.c`), backtracking (`kernel/bpf/backtrack.c`), core BPF logic (`kernel/bpf/core.c`), diagnostics (`kernel/bpf/diagnostics.c`), liveness analysis (`kernel/bpf/liveness.c`), state management (`kernel/bpf/states.c`), and the verifier itself (`kernel/bpf/verifier.c`). I need to filter out the ubiquitous stuff – memory allocators, core synchronization primitives, and such. Focus needs to be on the logic *added or changed*. Here's my initial list of candidates: * `bpf_id_scratch_reserve`: This looks promising, as it's a new non-static function introduced by this patch. * `bpf_jit_supports_large_stack`: Also new, and clearly related to the stack size change. * `bpf_push_jmp_history`: Modified signature and implementation. * `bpf_fmt_stack_mask`: Modified signature and implementation. * `bpf_mark_chain_precision`: Modified stack mask iteration. * `bpf_bt_sync_linked_regs`: Modified unpack call. * `check_max_stack_depth_subprog`: This function and related functions, like `check_stack_write_fixed_off`, `check_stack_read_fixed_off`, `check_stack_write_var_off`, `stacksafe`, `widen_frame_masks` check or use the new stack depth. * `bpf_prog_reenters_datapath`: NEW static function checking clone_redirect. Now, let's consider `bpf_check`. It's the entry point to the verifier, called during `BPF_PROG_LOAD`. It's not *completely* ubiquitous, because syzkaller *does* test BPF programs. Crucially, it's where the new stack limit is initialized. I think that it is a good candidate, as it is the verifier entrypoint. Ultimately, I'm leaning toward selecting those functions. I want to ensure the fuzzer spends time specifically exercising these areas. My focus should be on functions directly related to this BPF stack limit expansion and associated stack depth checks. |
| 16/2 | 2026/09/26 22:11 | tool | grepper |
0mArgs:{
"Expression": "check_max_stack_depth_subprog"
}Results: {
"Output": "kernel/bpf/verifier.c=5443=struct bpf_subprog_call_depth_info {\n--\nkernel/bpf/verifier.c-5455- */\nkernel/bpf/verifier.c:5456:static int check_max_stack_depth_subprog(struct bpf_verifier_env *env, int idx,\nkernel/bpf/verifier.c-5457-\t\t\t\t\t struct bpf_subprog_call_depth_info *dinfo,\n--\nkernel/bpf/verifier.c=5706=static int check_max_stack_depth(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-5738-\t\t\tpriv_stack_supported = !i \u0026\u0026 priv_stack_mode == PRIV_STACK_ADAPTIVE;\nkernel/bpf/verifier.c:5739:\t\t\tret = check_max_stack_depth_subprog(env, i, dinfo,\nkernel/bpf/verifier.c-5740-\t\t\t\t\tpriv_stack_supported);\n"
} |
| 17/2 | 2026/09/26 22:11 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 90139 output: 21 thoughts: 88 Thoughts: Here's my thought process, as it stands: **Diving into Stack Depth Verification in the BPF Verifier** Okay, so `check_max_stack_depth` is the function that calls `check_max_stack_depth_subprog` – that's the crucial link within `kernel/bpf/verifier.c`. I need to go back and examine `check_max_stack_depth` itself to get the full picture, since the relationship can't be one-way. This is a good starting point, but I'm not going to stop there. While I'm here, I should probably also peek at `subprog_spill_slots` and `widen_frame_masks` in `kernel/bpf/liveness.c`. The liveness analysis is where things get really interesting from a memory and resource allocation standpoint, especially as it relates to stack behavior, so I'll see if there's any bleed over. I need to understand how the allocation and deallocation happens as it moves between programs. I'm especially interested in how the mask widens. |
| 18/2 | 2026/09/26 22:11 | tool | grepper |
0mArgs:{
"Expression": "subprog_spill_slots"
}Results: {
"Output": "kernel/bpf/liveness.c=741=enum arg_track_state {\n--\nkernel/bpf/liveness.c-748- * The tracker follows as many spill slots as a subprog addresses directly\nkernel/bpf/liveness.c:749: * through R10, see subprog_spill_slots(). A frame's slots at a call site are\nkernel/bpf/liveness.c-750- * kept in a snapshot for the callees that receive pointers into that frame.\n--\nkernel/bpf/liveness.c=1035=static u32 spill_slots_affordable(int len)\n--\nkernel/bpf/liveness.c-1050- */\nkernel/bpf/liveness.c:1051:static u32 subprog_spill_slots(struct bpf_verifier_env *env, int start, int end)\nkernel/bpf/liveness.c-1052-{\n--\nkernel/bpf/liveness.c=1802=static int compute_subprog_args(struct bpf_verifier_env *env,\n--\nkernel/bpf/liveness.c-1815-\tint len = end - start;\nkernel/bpf/liveness.c:1816:\tu32 nslots = min(subprog_spill_slots(env, start, end), spill_slots_affordable(len));\nkernel/bpf/liveness.c-1817-\tstruct arg_track (*at_in)[MAX_AT_TRACK_REGS] = NULL;\n"
} |
| 19/2 | 2026/09/26 22:11 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 90880 output: 33 thoughts: 1010 Thoughts: **BPF Verifier Code Exploration: Key Functions and Configuration** Okay, let's break this down. My initial focus is on the BPF verifier's stack limit and associated functions. I see `bpf_prog_stack_limit` is a static inline function defined in `include/linux/bpf_verifier.h`. The critical part, `bpf_check` in `kernel/bpf/verifier.c`, actually calls it. Specifically, around line 22330, `bpf_check` sets the `env->stack_limit` using `bpf_prog_stack_limit(env->prog)`. That function is the main entry point to the BPF verifier, which is crucial. Now, I want to explore other potentially relevant functions. I have identified: `bpf_mark_chain_precision`, `bpf_push_jmp_history`, `bpf_id_scratch_reserve`, `bpf_jit_supports_large_stack`, `check_max_stack_depth`, `check_stack_write_fixed_off`, `check_stack_read_fixed_off`, `check_stack_write_var_off`, `stacksafe`, and `compute_subprog_args`. I dug into the header files to see which were visible: `bpf_mark_chain_precision`, `bpf_push_jmp_history`, `bpf_id_scratch_reserve`, `bpf_bt_sync_linked_regs`, and `bpf_clear_singular_ids` are all non-static functions, so they have some significance outside of their original file. I saw `stacksafe` is static in `kernel/bpf/states.c`, and called by `states_equal`. `check_max_stack_depth`, `check_stack_write_fixed_off`, and `check_stack_read_fixed_off` are static as well; the last two are called via `check_mem_access` -> `check_stack_access_rw`. `bpf_jit_supports_large_stack` is a globally visible, non-static function defined in `arch/x86/net/bpf_jit_comp.c`. It's clearly important for larger stacks. Finally, let's review configuration dependencies: the BPF syscall (`CONFIG_BPF_SYSCALL`) and JIT (`CONFIG_BPF_JIT`) configurations are prerequisites. I'm going to examine the JIT compilation specifically within `arch/x86/net/bpf_jit_comp.c` to understand exactly how that is compiled. |
| 20/2 | 2026/09/26 22:11 | 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"
} |
| 21/2 | 2026/09/26 22:11 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 92647 output: 25 thoughts: 101 Thoughts: Here's my thought process, as requested: **Investigating BPF JIT Compilation Dependencies** Okay, so I'm looking at `arch/x86/net/bpf_jit_comp.c`, which I know is compiled into the kernel when `CONFIG_BPF_JIT=y` on x86_64 systems. But that's just *one* piece of the puzzle. I need to get a clear picture of *all* the BPF-related compilation units. Specifically, I'm thinking about those core verifier and analysis files: `kernel/bpf/backtrack.c`, `kernel/bpf/liveness.c`, `kernel/bpf/states.c`, and, obviously, `kernel/bpf/verifier.c`. These are crucial for BPF's security and functionality. So, the most logical next step is to check the `kernel/bpf/Makefile`. That's where I expect to find the dependencies and compilation rules that govern these components. That should give me the complete picture, not just one piece of it. |
| 22/2 | 2026/09/26 22:11 | tool | grepper |
0mArgs:{
"Expression": "verifier",
"PathPrefix": "kernel/bpf"
}Results: {
"Output": "\nFull output is too long, showing 500 out of 6238 lines.\nUse more precise expression if possible.\n\nkernel/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/arena.c=97=u64 bpf_arena_map_kern_vm_start(struct bpf_map *map)\n--\nkernel/bpf/arena.c-105- *\nkernel/bpf/arena.c:106: * The verifier enforces at most one arena per program and stores it in\nkernel/bpf/arena.c-107- * prog-\u003eaux-\u003earena. Return that arena's underlying bpf_map, or NULL if\n--\nkernel/bpf/arraymap.c=1258=static void prog_array_map_free(struct bpf_map *map)\n--\nkernel/bpf/arraymap.c-1272-/* prog_array-\u003eaux-\u003e{type,jited} is a runtime binding.\nkernel/bpf/arraymap.c:1273: * Doing static check alone in the verifier is not enough.\nkernel/bpf/arraymap.c-1274- * Thus, prog_array_map cannot be used as an inner_map\n--\nkernel/bpf/backtrack.c-3-#include \u003clinux/bpf.h\u003e\nkernel/bpf/backtrack.c:4:#include \u003clinux/bpf_verifier.h\u003e\nkernel/bpf/backtrack.c-5-#include \u003clinux/filter.h\u003e\n--\nkernel/bpf/backtrack.c-7-\nkernel/bpf/backtrack.c:8:#define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)\nkernel/bpf/backtrack.c-9-\nkernel/bpf/backtrack.c-10-/* for any branch, call, exit record the history of jmps in the given state */\nkernel/bpf/backtrack.c:11:int bpf_push_jmp_history(struct bpf_verifier_env *env, struct bpf_verifier_state *cur,\nkernel/bpf/backtrack.c-12-\t\t\t int insn_flags, int spi, int frame, const u16 *linked_regs,\n--\nkernel/bpf/backtrack.c-23-\t\t */\nkernel/bpf/backtrack.c:24:\t\tverifier_bug_if((env-\u003ecur_hist_ent-\u003eflags \u0026 insn_flags) \u0026\u0026\nkernel/bpf/backtrack.c-25-\t\t\t\t(env-\u003ecur_hist_ent-\u003eflags \u0026 insn_flags) != insn_flags,\n--\nkernel/bpf/backtrack.c-30-\t\tenv-\u003ecur_hist_ent-\u003eframe = frame;\nkernel/bpf/backtrack.c:31:\t\tverifier_bug_if(env-\u003ecur_hist_ent-\u003elinked_regs_cnt != 0, env,\nkernel/bpf/backtrack.c-32-\t\t\t\t\"insn history: insn_idx %d has %u linked regs\",\n--\nkernel/bpf/backtrack.c=70=static bool is_atomic_fetch_insn(const struct bpf_insn *insn)\n--\nkernel/bpf/backtrack.c-89- */\nkernel/bpf/backtrack.c:90:static int get_prev_insn_idx(struct bpf_verifier_state *st, int i,\nkernel/bpf/backtrack.c-91-\t\t\t u32 *history)\n--\nkernel/bpf/backtrack.c-110-\nkernel/bpf/backtrack.c:111:static struct bpf_jmp_history_entry *get_jmp_hist_entry(struct bpf_verifier_state *st,\nkernel/bpf/backtrack.c-112-\t\t\t\t\t\t u32 hist_end, int insn_idx)\n--\nkernel/bpf/backtrack.c=124=static inline void bt_reset(struct backtrack_state *bt)\nkernel/bpf/backtrack.c-125-{\nkernel/bpf/backtrack.c:126:\tstruct bpf_verifier_env *env = bt-\u003eenv;\nkernel/bpf/backtrack.c-127-\n--\nkernel/bpf/backtrack.c=166=static inline int bt_subprog_enter(struct backtrack_state *bt)\n--\nkernel/bpf/backtrack.c-168-\tif (bt-\u003eframe == MAX_CALL_FRAMES - 1) {\nkernel/bpf/backtrack.c:169:\t\tverifier_bug(bt-\u003eenv, \"subprog enter from frame %d\", bt-\u003eframe);\nkernel/bpf/backtrack.c-170-\t\treturn -EFAULT;\n--\nkernel/bpf/backtrack.c=176=static inline int bt_subprog_exit(struct backtrack_state *bt)\n--\nkernel/bpf/backtrack.c-178-\tif (bt-\u003eframe == 0) {\nkernel/bpf/backtrack.c:179:\t\tverifier_bug(bt-\u003eenv, \"subprog exit from frame 0\");\nkernel/bpf/backtrack.c-180-\t\treturn -EFAULT;\n--\nkernel/bpf/backtrack.c=257=void bpf_fmt_stack_mask(char *buf, ssize_t buf_sz, const unsigned long *stack_mask)\n--\nkernel/bpf/backtrack.c-273-\nkernel/bpf/backtrack.c:274:/* For given verifier state backtrack_insn() is called from the last insn to\nkernel/bpf/backtrack.c-275- * the first insn. Its purpose is to compute a bitmask of registers and\nkernel/bpf/backtrack.c:276: * stack slots that needs precision in the parent verifier state.\nkernel/bpf/backtrack.c-277- *\n--\nkernel/bpf/backtrack.c-282- */\nkernel/bpf/backtrack.c:283:static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,\nkernel/bpf/backtrack.c-284-\t\t\t struct bpf_jmp_history_entry *hist, struct backtrack_state *bt)\n--\nkernel/bpf/backtrack.c-333-\t\t\t\t * Corresponding register is already marked\nkernel/bpf/backtrack.c:334:\t\t\t\t * as precise=true in this verifier state.\nkernel/bpf/backtrack.c-335-\t\t\t\t * No further markings in parent are necessary\n--\nkernel/bpf/backtrack.c-444-\t\t\t\t */\nkernel/bpf/backtrack.c:445:\t\t\t\tverifier_bug_if(idx + 1 != subseq_idx, env,\nkernel/bpf/backtrack.c-446-\t\t\t\t\t\t\"extra insn from subprog\");\n--\nkernel/bpf/backtrack.c-455-\t\t\t\tif (bt_reg_mask(bt) \u0026 BPF_REGMASK_ARGS) {\nkernel/bpf/backtrack.c:456:\t\t\t\t\tverifier_bug(env, \"global subprog unexpected regs %x\",\nkernel/bpf/backtrack.c-457-\t\t\t\t\t\t bt_reg_mask(bt));\n--\nkernel/bpf/backtrack.c-468-\t\t\t\tif (bt_reg_mask(bt) \u0026 ~BPF_REGMASK_ARGS) {\nkernel/bpf/backtrack.c:469:\t\t\t\t\tverifier_bug(env, \"static subprog unexpected regs %x\",\nkernel/bpf/backtrack.c-470-\t\t\t\t\t\t bt_reg_mask(bt));\n--\nkernel/bpf/backtrack.c-478-\t\t\t\t\t\t\t bt_stack_mask(bt));\nkernel/bpf/backtrack.c:479:\t\t\t\t\tverifier_bug(env, \"static subprog leftover stack slots %s\",\nkernel/bpf/backtrack.c-480-\t\t\t\t\t\t env-\u003etmp_str_buf);\n--\nkernel/bpf/backtrack.c-490-\t\t\t\tif (bt_stack_arg_mask(bt)) {\nkernel/bpf/backtrack.c:491:\t\t\t\t\tverifier_bug(env,\nkernel/bpf/backtrack.c-492-\t\t\t\t\t\t \"static subprog leftover stack arg slots %x\",\n--\nkernel/bpf/backtrack.c-508-\t\t\tif (bt_reg_mask(bt) \u0026 ~BPF_REGMASK_ARGS) {\nkernel/bpf/backtrack.c:509:\t\t\t\tverifier_bug(env, \"callback unexpected regs %x\",\nkernel/bpf/backtrack.c-510-\t\t\t\t\t bt_reg_mask(bt));\n--\nkernel/bpf/backtrack.c-515-\t\t\t\t\t\t bt_stack_mask(bt));\nkernel/bpf/backtrack.c:516:\t\t\t\tverifier_bug(env, \"callback leftover stack slots %s\",\nkernel/bpf/backtrack.c-517-\t\t\t\t\t env-\u003etmp_str_buf);\n--\nkernel/bpf/backtrack.c-540-\t\t\t\t */\nkernel/bpf/backtrack.c:541:\t\t\t\tverifier_bug(env, \"backtracking call unexpected regs %x\",\nkernel/bpf/backtrack.c-542-\t\t\t\t\t bt_reg_mask(bt));\n--\nkernel/bpf/backtrack.c-583-\t\t\tif (from_subprog_call \u0026\u0026 (bt_reg_mask(bt) \u0026 BPF_REGMASK_ARGS)) {\nkernel/bpf/backtrack.c:584:\t\t\t\tverifier_bug(env, \"backtracking exit unexpected regs %x\",\nkernel/bpf/backtrack.c-585-\t\t\t\t\t bt_reg_mask(bt));\n--\nkernel/bpf/backtrack.c-658- * . helper_call(.., scalar, ...) where ARG_CONST is expected\nkernel/bpf/backtrack.c:659: * backtrack through the verifier states and mark all registers and\nkernel/bpf/backtrack.c-660- * stack slots with spilled constants that these scalar registers\n--\nkernel/bpf/backtrack.c-664- *\nkernel/bpf/backtrack.c:665: * Note the verifier cannot simply walk register parentage chain,\nkernel/bpf/backtrack.c-666- * since many different registers and stack slots could have been\n--\nkernel/bpf/backtrack.c-669- * The approach of starting with precise=true for all registers and then\nkernel/bpf/backtrack.c:670: * backtrack to mark a register as not precise when the verifier detects\nkernel/bpf/backtrack.c-671- * that program doesn't care about specific value (e.g., when helper\n--\nkernel/bpf/backtrack.c-673- *\nkernel/bpf/backtrack.c:674: * It's ok to walk single parentage chain of the verifier states.\nkernel/bpf/backtrack.c-675- * It's possible that this backtracking will go all the way till 1st insn.\n--\nkernel/bpf/backtrack.c-697- * Also if parent's curframe \u003e frame where backtracking started,\nkernel/bpf/backtrack.c:698: * the verifier need to mark registers in both frames, otherwise callees\nkernel/bpf/backtrack.c-699- * may incorrectly prune callers. This is similar to\nkernel/bpf/backtrack.c:700: * commit 7640ead93924 (\"bpf: verifier: make sure callees don't prune with caller differences\")\nkernel/bpf/backtrack.c-701- *\n--\nkernel/bpf/backtrack.c-703- */\nkernel/bpf/backtrack.c:704:void bpf_mark_all_scalars_precise(struct bpf_verifier_env *env,\nkernel/bpf/backtrack.c:705:\t\t\t\t struct bpf_verifier_state *st)\nkernel/bpf/backtrack.c-706-{\n--\nkernel/bpf/backtrack.c-754- * bpf_mark_chain_precision() backtracks BPF program instruction sequence and\nkernel/bpf/backtrack.c:755: * chain of verifier states making sure that register *regno* (if regno \u003e= 0)\nkernel/bpf/backtrack.c-756- * and/or stack slot *spi* (if spi \u003e= 0) are marked as precisely tracked\n--\nkernel/bpf/backtrack.c-794- * knowledge: if precise value or range is useful to prune jump branch, BPF\nkernel/bpf/backtrack.c:795: * verifier takes this opportunity enthusiastically. Similarly, when\nkernel/bpf/backtrack.c-796- * register's value is used to calculate offset or memory address, exact\n--\nkernel/bpf/backtrack.c-798- * what we mentioned above about state comparison ignoring precise markings\nkernel/bpf/backtrack.c:799: * during state comparison, BPF verifier ignores and also assumes precise\nkernel/bpf/backtrack.c:800: * markings *at will* during instruction verification process. But as verifier\nkernel/bpf/backtrack.c-801- * assumes precision, it also propagates any precision dependencies across\n--\nkernel/bpf/backtrack.c-839- */\nkernel/bpf/backtrack.c:840:int bpf_mark_chain_precision(struct bpf_verifier_env *env,\nkernel/bpf/backtrack.c:841:\t\t\t struct bpf_verifier_state *starting_state,\nkernel/bpf/backtrack.c-842-\t\t\t int regno,\n--\nkernel/bpf/backtrack.c-844-{\nkernel/bpf/backtrack.c:845:\tstruct bpf_verifier_state *st = starting_state;\nkernel/bpf/backtrack.c-846-\tstruct backtrack_state *bt = \u0026env-\u003ebt;\n--\nkernel/bpf/backtrack.c-870-\t\tif (reg-\u003etype != SCALAR_VALUE) {\nkernel/bpf/backtrack.c:871:\t\t\tverifier_bug(env, \"backtracking misuse\");\nkernel/bpf/backtrack.c-872-\t\t\treturn -EFAULT;\n--\nkernel/bpf/backtrack.c-913-\t\t\tbpf_fmt_stack_mask(env-\u003etmp_str_buf, TMP_STR_BUF_LEN, bt_stack_mask(bt));\nkernel/bpf/backtrack.c:914:\t\t\tverifier_bug(env, \"backtracking func entry subprog %d reg_mask %x stack_mask %s\",\nkernel/bpf/backtrack.c-915-\t\t\t\t st-\u003eframe[0]-\u003esubprogno, bt_reg_mask(bt), env-\u003etmp_str_buf);\n--\nkernel/bpf/backtrack.c-950-\t\t\t\t */\nkernel/bpf/backtrack.c:951:\t\t\t\tverifier_bug(env, \"backtracking idx %d\", i);\nkernel/bpf/backtrack.c-952-\t\t\t\treturn -EFAULT;\n--\nkernel/bpf/backtrack.c-976-\t\t\tfor_each_set_bit(i, bt_frame_stack_mask(bt, fr), MAX_BPF_STACK_SLOTS) {\nkernel/bpf/backtrack.c:977:\t\t\t\tif (verifier_bug_if(i \u003e= bpf_stack_nr_slots(func),\nkernel/bpf/backtrack.c-978-\t\t\t\t\t\t env, \"stack slot %d, total slots %d\",\n--\nkernel/bpf/backtrack.c-1013-\t\t\t\tverbose(env, \"stack=%s: \", env-\u003etmp_str_buf);\nkernel/bpf/backtrack.c:1014:\t\t\t\tprint_verifier_state(env, st, fr, true);\nkernel/bpf/backtrack.c-1015-\t\t\t}\n--\nkernel/bpf/bpf_iter.c=727=BPF_CALL_4(bpf_loop, u32, nr_loops, void *, callback_fn, void *, callback_ctx,\n--\nkernel/bpf/bpf_iter.c-735-\t * is inlined, be careful to modify this code in sync. See\nkernel/bpf/bpf_iter.c:736:\t * function verifier.c:inline_bpf_loop.\nkernel/bpf/bpf_iter.c-737-\t */\n--\nkernel/bpf/bpf_lsm.c=119=BTF_ID_LIST_SINGLE(bpf_lsm_inode_init_security_btf_id, func,\n--\nkernel/bpf/bpf_lsm.c-121-\nkernel/bpf/bpf_lsm.c:122:int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,\nkernel/bpf/bpf_lsm.c-123-\t\t\tstruct bpf_prog *prog)\n--\nkernel/bpf/bpf_lsm.c-151-\t\t * trustworthy as the context it is loaded from, which\nkernel/bpf/bpf_lsm.c:152:\t\t * lsm_verifier_ops keeps read-only.\nkernel/bpf/bpf_lsm.c-153-\t\t */\n--\nkernel/bpf/bpf_lsm.c=433=const struct bpf_prog_ops lsm_prog_ops = {\n--\nkernel/bpf/bpf_lsm.c-435-\nkernel/bpf/bpf_lsm.c:436:const struct bpf_verifier_ops lsm_verifier_ops = {\nkernel/bpf/bpf_lsm.c-437-\t.get_func_proto\t\t= bpf_lsm_func_proto,\n--\nkernel/bpf/bpf_struct_ops.c-4-#include \u003clinux/bpf.h\u003e\nkernel/bpf/bpf_struct_ops.c:5:#include \u003clinux/bpf_verifier.h\u003e\nkernel/bpf/bpf_struct_ops.c-6-#include \u003clinux/btf.h\u003e\n--\nkernel/bpf/bpf_struct_ops.c=65=static DEFINE_MUTEX(update_mutex);\n--\nkernel/bpf/bpf_struct_ops.c-69-\nkernel/bpf/bpf_struct_ops.c:70:const struct bpf_verifier_ops bpf_struct_ops_verifier_ops = {\nkernel/bpf/bpf_struct_ops.c-71-};\n--\nkernel/bpf/bpf_struct_ops.c=141=void bpf_struct_ops_image_free(void *image)\n--\nkernel/bpf/bpf_struct_ops.c-162- * to provide an array of struct bpf_ctx_arg_aux, which in turn provides\nkernel/bpf/bpf_struct_ops.c:163: * the information that used by the verifier to check the arguments of the\nkernel/bpf/bpf_struct_ops.c-164- * BPF struct_ops program assigned to the member. Here, we only care about\n--\nkernel/bpf/bpf_struct_ops.c-168- * prog-\u003eaux-\u003ectx_arg_info of BPF struct_ops programs and passed to the\nkernel/bpf/bpf_struct_ops.c:169: * verifier. (See check_struct_ops_btf_id())\nkernel/bpf/bpf_struct_ops.c-170- *\n--\nkernel/bpf/bpf_struct_ops.c=174=static int prepare_arg_info(struct btf *btf,\n--\nkernel/bpf/bpf_struct_ops.c-289-\t\t\t/*\nkernel/bpf/bpf_struct_ops.c:290:\t\t\t * Both types get PTR_TO_ARENA. In verifier state,\nkernel/bpf/bpf_struct_ops.c-291-\t\t\t * PTR_TO_ARENA encompasses potential NULL values, but\n--\nkernel/bpf/bpf_struct_ops.c=359=int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc,\nkernel/bpf/bpf_struct_ops.c-360-\t\t\t struct btf *btf,\nkernel/bpf/bpf_struct_ops.c:361:\t\t\t struct bpf_verifier_log *log)\nkernel/bpf/bpf_struct_ops.c-362-{\n--\nkernel/bpf/btf.c-18-#include \u003clinux/sort.h\u003e\nkernel/bpf/btf.c:19:#include \u003clinux/bpf_verifier.h\u003e\nkernel/bpf/btf.c-20-#include \u003clinux/btf.h\u003e\n--\nkernel/bpf/btf.c-89- *\nkernel/bpf/btf.c:90: * The above is the btf_verifier debug log:\nkernel/bpf/btf.c-91- * - Each line started with \"[?]\" is a btf_type object\n--\nkernel/bpf/btf.c=255=struct btf {\n--\nkernel/bpf/btf.c-284-\nkernel/bpf/btf.c:285:enum verifier_phase {\nkernel/bpf/btf.c-286-\tCHECK_META,\n--\nkernel/bpf/btf.c=312=struct btf_sec_info {\n--\nkernel/bpf/btf.c-316-\nkernel/bpf/btf.c:317:struct btf_verifier_env {\nkernel/bpf/btf.c-318-\tstruct btf *btf;\n--\nkernel/bpf/btf.c-320-\tstruct resolve_vertex stack[MAX_RESOLVE_DEPTH];\nkernel/bpf/btf.c:321:\tstruct bpf_verifier_log log;\nkernel/bpf/btf.c-322-\tu32 log_type_id;\nkernel/bpf/btf.c-323-\tu32 top_stack;\nkernel/bpf/btf.c:324:\tenum verifier_phase phase;\nkernel/bpf/btf.c-325-\tenum resolve_mode resolve_mode;\n--\nkernel/bpf/btf.c=447=struct btf_kind_operations {\nkernel/bpf/btf.c:448:\ts32 (*check_meta)(struct btf_verifier_env *env,\nkernel/bpf/btf.c-449-\t\t\t const struct btf_type *t,\nkernel/bpf/btf.c-450-\t\t\t u32 meta_left);\nkernel/bpf/btf.c:451:\tint (*resolve)(struct btf_verifier_env *env,\nkernel/bpf/btf.c-452-\t\t const struct resolve_vertex *v);\nkernel/bpf/btf.c:453:\tint (*check_member)(struct btf_verifier_env *env,\nkernel/bpf/btf.c-454-\t\t\t const struct btf_type *struct_type,\n--\nkernel/bpf/btf.c-456-\t\t\t const struct btf_type *member_type);\nkernel/bpf/btf.c:457:\tint (*check_kflag_member)(struct btf_verifier_env *env,\nkernel/bpf/btf.c-458-\t\t\t\t const struct btf_type *struct_type,\n--\nkernel/bpf/btf.c-460-\t\t\t\t const struct btf_type *member_type);\nkernel/bpf/btf.c:461:\tvoid (*log_details)(struct btf_verifier_env *env,\nkernel/bpf/btf.c-462-\t\t\t const struct btf_type *t);\n--\nkernel/bpf/btf.c=469=static struct btf_type btf_void;\nkernel/bpf/btf.c-470-\nkernel/bpf/btf.c:471:static int btf_resolve(struct btf_verifier_env *env,\nkernel/bpf/btf.c-472-\t\t const struct btf_type *t, u32 type_id);\nkernel/bpf/btf.c-473-\nkernel/bpf/btf.c:474:static int btf_func_check(struct btf_verifier_env *env,\nkernel/bpf/btf.c-475-\t\t\t const struct btf_type *t);\n--\nkernel/bpf/btf.c=1532=static void btf_show_end_struct_type(struct btf_show *show)\n--\nkernel/bpf/btf.c-1536-\nkernel/bpf/btf.c:1537:__printf(2, 3) static void __btf_verifier_log(struct bpf_verifier_log *log,\nkernel/bpf/btf.c-1538-\t\t\t\t\t const char *fmt, ...)\n--\nkernel/bpf/btf.c-1542-\tva_start(args, fmt);\nkernel/bpf/btf.c:1543:\tbpf_verifier_vlog(log, fmt, args);\nkernel/bpf/btf.c-1544-\tva_end(args);\n--\nkernel/bpf/btf.c-1546-\nkernel/bpf/btf.c:1547:__printf(2, 3) static void btf_verifier_log(struct btf_verifier_env *env,\nkernel/bpf/btf.c-1548-\t\t\t\t\t const char *fmt, ...)\nkernel/bpf/btf.c-1549-{\nkernel/bpf/btf.c:1550:\tstruct bpf_verifier_log *log = \u0026env-\u003elog;\nkernel/bpf/btf.c-1551-\tva_list args;\nkernel/bpf/btf.c-1552-\nkernel/bpf/btf.c:1553:\tif (!bpf_verifier_log_needed(log))\nkernel/bpf/btf.c-1554-\t\treturn;\n--\nkernel/bpf/btf.c-1556-\tva_start(args, fmt);\nkernel/bpf/btf.c:1557:\tbpf_verifier_vlog(log, fmt, args);\nkernel/bpf/btf.c-1558-\tva_end(args);\n--\nkernel/bpf/btf.c-1560-\nkernel/bpf/btf.c:1561:__printf(4, 5) static void __btf_verifier_log_type(struct btf_verifier_env *env,\nkernel/bpf/btf.c-1562-\t\t\t\t\t\t const struct btf_type *t,\n--\nkernel/bpf/btf.c-1565-{\nkernel/bpf/btf.c:1566:\tstruct bpf_verifier_log *log = \u0026env-\u003elog;\nkernel/bpf/btf.c-1567-\tstruct btf *btf = env-\u003ebtf;\n--\nkernel/bpf/btf.c-1569-\nkernel/bpf/btf.c:1570:\tif (!bpf_verifier_log_needed(log))\nkernel/bpf/btf.c-1571-\t\treturn;\n--\nkernel/bpf/btf.c-1573-\tif (log-\u003elevel == BPF_LOG_KERNEL) {\nkernel/bpf/btf.c:1574:\t\t/* btf verifier prints all types it is processing via\nkernel/bpf/btf.c:1575:\t\t * btf_verifier_log_type(..., fmt = NULL).\nkernel/bpf/btf.c-1576-\t\t * Skip those prints for in-kernel BTF verification.\n--\nkernel/bpf/btf.c-1585-\nkernel/bpf/btf.c:1586:\t__btf_verifier_log(log, \"[%u] %s %s%s\",\nkernel/bpf/btf.c-1587-\t\t\t env-\u003elog_type_id,\n--\nkernel/bpf/btf.c-1595-\tif (fmt \u0026\u0026 *fmt) {\nkernel/bpf/btf.c:1596:\t\t__btf_verifier_log(log, \" \");\nkernel/bpf/btf.c-1597-\t\tva_start(args, fmt);\nkernel/bpf/btf.c:1598:\t\tbpf_verifier_vlog(log, fmt, args);\nkernel/bpf/btf.c-1599-\t\tva_end(args);\n--\nkernel/bpf/btf.c-1601-\nkernel/bpf/btf.c:1602:\t__btf_verifier_log(log, \"\\n\");\nkernel/bpf/btf.c-1603-}\nkernel/bpf/btf.c-1604-\nkernel/bpf/btf.c:1605:#define btf_verifier_log_type(env, t, ...) \\\nkernel/bpf/btf.c:1606:\t__btf_verifier_log_type((env), (t), true, __VA_ARGS__)\nkernel/bpf/btf.c:1607:#define btf_verifier_log_basic(env, t, ...) \\\nkernel/bpf/btf.c:1608:\t__btf_verifier_log_type((env), (t), false, __VA_ARGS__)\nkernel/bpf/btf.c-1609-\nkernel/bpf/btf.c=1610=__printf(4, 5)\nkernel/bpf/btf.c:1611:static void btf_verifier_log_member(struct btf_verifier_env *env,\nkernel/bpf/btf.c-1612-\t\t\t\t const struct btf_type *struct_type,\n--\nkernel/bpf/btf.c-1615-{\nkernel/bpf/btf.c:1616:\tstruct bpf_verifier_log *log = \u0026env-\u003elog;\nkernel/bpf/btf.c-1617-\tstruct btf *btf = env-\u003ebtf;\n--\nkernel/bpf/btf.c-1619-\nkernel/bpf/btf.c:1620:\tif (!bpf_verifier_log_needed(log))\nkernel/bpf/btf.c-1621-\t\treturn;\n--\nkernel/bpf/btf.c-1638-\tif (env-\u003ephase != CHECK_META)\nkernel/bpf/btf.c:1639:\t\tbtf_verifier_log_type(env, struct_type, NULL);\nkernel/bpf/btf.c-1640-\nkernel/bpf/btf.c-1641-\tif (btf_type_kflag(struct_type))\nkernel/bpf/btf.c:1642:\t\t__btf_verifier_log(log,\nkernel/bpf/btf.c-1643-\t\t\t\t \"\\t%s type_id=%u bitfield_size=%u bits_offset=%u\",\n--\nkernel/bpf/btf.c-1648-\telse\nkernel/bpf/btf.c:1649:\t\t__btf_verifier_log(log, \"\\t%s type_id=%u bits_offset=%u\",\nkernel/bpf/btf.c-1650-\t\t\t\t __btf_name_by_offset(btf, member-\u003ename_off),\n--\nkernel/bpf/btf.c-1653-\tif (fmt \u0026\u0026 *fmt) {\nkernel/bpf/btf.c:1654:\t\t__btf_verifier_log(log, \" \");\nkernel/bpf/btf.c-1655-\t\tva_start(args, fmt);\nkernel/bpf/btf.c:1656:\t\tbpf_verifier_vlog(log, fmt, args);\nkernel/bpf/btf.c-1657-\t\tva_end(args);\n--\nkernel/bpf/btf.c-1659-\nkernel/bpf/btf.c:1660:\t__btf_verifier_log(log, \"\\n\");\nkernel/bpf/btf.c-1661-}\n--\nkernel/bpf/btf.c=1663=__printf(4, 5)\nkernel/bpf/btf.c:1664:static void btf_verifier_log_vsi(struct btf_verifier_env *env,\nkernel/bpf/btf.c-1665-\t\t\t\t const struct btf_type *datasec_type,\n--\nkernel/bpf/btf.c-1668-{\nkernel/bpf/btf.c:1669:\tstruct bpf_verifier_log *log = \u0026env-\u003elog;\nkernel/bpf/btf.c-1670-\tva_list args;\nkernel/bpf/btf.c-1671-\nkernel/bpf/btf.c:1672:\tif (!bpf_verifier_log_needed(log))\nkernel/bpf/btf.c-1673-\t\treturn;\n--\nkernel/bpf/btf.c-1676-\tif (env-\u003ephase != CHECK_META)\nkernel/bpf/btf.c:1677:\t\tbtf_verifier_log_type(env, datasec_type, NULL);\nkernel/bpf/btf.c-1678-\nkernel/bpf/btf.c:1679:\t__btf_verifier_log(log, \"\\t type_id=%u offset=%u size=%u\",\nkernel/bpf/btf.c-1680-\t\t\t vsi-\u003etype, vsi-\u003eoffset, vsi-\u003esize);\nkernel/bpf/btf.c-1681-\tif (fmt \u0026\u0026 *fmt) {\nkernel/bpf/btf.c:1682:\t\t__btf_verifier_log(log, \" \");\nkernel/bpf/btf.c-1683-\t\tva_start(args, fmt);\nkernel/bpf/btf.c:1684:\t\tbpf_verifier_vlog(log, fmt, args);\nkernel/bpf/btf.c-1685-\t\tva_end(args);\n--\nkernel/bpf/btf.c-1687-\nkernel/bpf/btf.c:1688:\t__btf_verifier_log(log, \"\\n\");\nkernel/bpf/btf.c-1689-}\nkernel/bpf/btf.c-1690-\nkernel/bpf/btf.c:1691:static void btf_verifier_log_hdr(struct btf_verifier_env *env,\nkernel/bpf/btf.c-1692-\t\t\t\t u32 btf_data_size)\nkernel/bpf/btf.c-1693-{\nkernel/bpf/btf.c:1694:\tstruct bpf_verifier_log *log = \u0026env-\u003elog;\nkernel/bpf/btf.c-1695-\tconst struct btf *btf = env-\u003ebtf;\n--\nkernel/bpf/btf.c-1697-\nkernel/bpf/btf.c:1698:\tif (!bpf_verifier_log_needed(log))\nkernel/bpf/btf.c-1699-\t\treturn;\n--\nkernel/bpf/btf.c-1703-\thdr = \u0026btf-\u003ehdr;\nkernel/bpf/btf.c:1704:\t__btf_verifier_log(log, \"magic: 0x%x\\n\", hdr-\u003emagic);\nkernel/bpf/btf.c:1705:\t__btf_verifier_log(log, \"version: %u\\n\", hdr-\u003eversion);\nkernel/bpf/btf.c:1706:\t__btf_verifier_log(log, \"flags: 0x%x\\n\", hdr-\u003eflags);\nkernel/bpf/btf.c:1707:\t__btf_verifier_log(log, \"hdr_len: %u\\n\", hdr-\u003ehdr_len);\nkernel/bpf/btf.c:1708:\t__btf_verifier_log(log, \"type_off: %u\\n\", hdr-\u003etype_off);\nkernel/bpf/btf.c:1709:\t__btf_verifier_log(log, \"type_len: %u\\n\", hdr-\u003etype_len);\nkernel/bpf/btf.c:1710:\t__btf_verifier_log(log, \"str_off: %u\\n\", hdr-\u003estr_off);\nkernel/bpf/btf.c:1711:\t__btf_verifier_log(log, \"str_len: %u\\n\", hdr-\u003estr_len);\nkernel/bpf/btf.c-1712-\tif (hdr-\u003ehdr_len \u003e= sizeof(struct btf_header) \u0026\u0026\nkernel/bpf/btf.c-1713-\t btf_data_size \u003e= hdr-\u003ehdr_len) {\nkernel/bpf/btf.c:1714:\t\t__btf_verifier_log(log, \"layout_off: %u\\n\", hdr-\u003elayout_off);\nkernel/bpf/btf.c:1715:\t\t__btf_verifier_log(log, \"layout_len: %u\\n\", hdr-\u003elayout_len);\nkernel/bpf/btf.c-1716-\t}\nkernel/bpf/btf.c:1717:\t__btf_verifier_log(log, \"btf_total_size: %u\\n\", btf_data_size);\nkernel/bpf/btf.c-1718-}\nkernel/bpf/btf.c-1719-\nkernel/bpf/btf.c:1720:static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t)\nkernel/bpf/btf.c-1721-{\n--\nkernel/bpf/btf.c-1730-\t\tif (btf-\u003estart_id + btf-\u003etypes_size == BTF_MAX_TYPE) {\nkernel/bpf/btf.c:1731:\t\t\tbtf_verifier_log(env, \"Exceeded max num of types\");\nkernel/bpf/btf.c-1732-\t\t\treturn -E2BIG;\n--\nkernel/bpf/btf.c=1920=void btf_set_base_btf(struct btf *btf, const struct btf *base_btf)\n--\nkernel/bpf/btf.c-1926-\nkernel/bpf/btf.c:1927:static int env_resolve_init(struct btf_verifier_env *env)\nkernel/bpf/btf.c-1928-{\n--\nkernel/bpf/btf.c-1962-\nkernel/bpf/btf.c:1963:static void btf_verifier_env_free(struct btf_verifier_env *env)\nkernel/bpf/btf.c-1964-{\n--\nkernel/bpf/btf.c-1968-\nkernel/bpf/btf.c:1969:static bool env_type_is_resolve_sink(const struct btf_verifier_env *env,\nkernel/bpf/btf.c-1970-\t\t\t\t const struct btf_type *next_type)\n--\nkernel/bpf/btf.c-1993-\nkernel/bpf/btf.c:1994:static bool env_type_is_resolved(const struct btf_verifier_env *env,\nkernel/bpf/btf.c-1995-\t\t\t\t u32 type_id)\n--\nkernel/bpf/btf.c-2003-\nkernel/bpf/btf.c:2004:static int env_stack_push(struct btf_verifier_env *env,\nkernel/bpf/btf.c-2005-\t\t\t const struct btf_type *t, u32 type_id)\n--\nkernel/bpf/btf.c-2033-\nkernel/bpf/btf.c:2034:static void env_stack_set_next_member(struct btf_verifier_env *env,\nkernel/bpf/btf.c-2035-\t\t\t\t u32 next_member)\n--\nkernel/bpf/btf.c-2039-\nkernel/bpf/btf.c:2040:static void env_stack_pop_resolved(struct btf_verifier_env *env,\nkernel/bpf/btf.c-2041-\t\t\t\t u32 resolved_type_id,\n--\nkernel/bpf/btf.c-2052-\nkernel/bpf/btf.c:2053:static const struct resolve_vertex *env_stack_peak(struct btf_verifier_env *env)\nkernel/bpf/btf.c-2054-{\n--\nkernel/bpf/btf.c=2179=const struct btf_type *btf_type_id_size(const struct btf *btf,\n--\nkernel/bpf/btf.c-2221-\nkernel/bpf/btf.c:2222:static int btf_df_check_member(struct btf_verifier_env *env,\nkernel/bpf/btf.c-2223-\t\t\t const struct btf_type *struct_type,\n--\nkernel/bpf/btf.c-2226-{\nkernel/bpf/btf.c:2227:\tbtf_verifier_log_basic(env, struct_type,\nkernel/bpf/btf.c-2228-\t\t\t \"Unsupported check_member\");\n--\nkernel/bpf/btf.c-2231-\nkernel/bpf/btf.c:2232:static int btf_df_check_kflag_member(struct btf_verifier_env *env,\nkernel/bpf/btf.c-2233-\t\t\t\t const struct btf_type *struct_type,\n--\nkernel/bpf/btf.c-2236-{\nkernel/bpf/btf.c:2237:\tbtf_verifier_log_basic(env, struct_type,\nkernel/bpf/btf.c-2238-\t\t\t \"Unsupported check_kflag_member\");\n--\nkernel/bpf/btf.c-2244- */\nkernel/bpf/btf.c:2245:static int btf_generic_check_kflag_member(struct btf_verifier_env *env,\nkernel/bpf/btf.c-2246-\t\t\t\t\t const struct btf_type *struct_type,\n--\nkernel/bpf/btf.c-2250-\tif (BTF_MEMBER_BITFIELD_SIZE(member-\u003eoffset)) {\nkernel/bpf/btf.c:2251:\t\tbtf_verifier_log_member(env, struct_type, member,\n\n"
} |
| 23/2 | 2026/09/26 22:11 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 104162 output: 32 thoughts: 116 Thoughts: Here's my thinking process: **Diving into the BPF Makefile** Okay, so I'm focusing on the `kernel/bpf/Makefile`. My immediate interest is the object files being compiled, specifically those related to the BPF syscall itself. I see on line 9 that if `CONFIG_BPF_SYSCALL` is enabled, then `syscall.o`, `verifier.o`, `inode.o`, `helpers.o`, `tnum.o`, `cnum.o`, `log.o`, `token.o`, `liveness.o`, `const_fold.o`, and `diagnostics.o` are all included. A similar line on line 12 then adds `core.o`. My next step is to actually examine the beginning of the file to get some context and understand how these object files are put together. I need a broader view to understand the dependency graph and build process. I'll read through the first 20 lines to get a handle on the overall structure and any relevant variables or conditional logic that might be in play. |
| 24/2 | 2026/09/26 22:11 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/Makefile",
"FirstLine": 1,
"LineCount": 30
}Results: {
"Contents": " 1:\t# SPDX-License-Identifier: GPL-2.0\n 2:\tobj-y := core.o\n 3:\tifneq ($(CONFIG_BPF_JIT_ALWAYS_ON),y)\n 4:\t# ___bpf_prog_run() needs GCSE disabled on x86; see 3193c0836f203 for details\n 5:\tcflags-nogcse-$(CONFIG_X86)$(CONFIG_CC_IS_GCC) := -fno-gcse\n 6:\tendif\n 7:\tCFLAGS_core.o += -Wno-override-init $(cflags-nogcse-yy)\n 8:\t\n 9:\tobj-$(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\n 10:\tobj-$(CONFIG_BPF_SYSCALL) += bpf_iter.o map_iter.o task_iter.o prog_iter.o link_iter.o\n 11:\tobj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o\n 12:\tobj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o bpf_insn_array.o\n 13:\tobj-$(CONFIG_BPF_SYSCALL) += bpf_local_storage.o bpf_task_storage.o\n 14:\tobj-$(CONFIG_BPF_SYSCALL) += fixups.o cfg.o states.o backtrack.o check_btf.o\n 15:\tobj-${CONFIG_BPF_LSM}\t += bpf_inode_storage.o\n 16:\tobj-$(CONFIG_BPF_SYSCALL) += disasm.o mprog.o\n 17:\tobj-$(CONFIG_BPF_JIT) += trampoline.o\n 18:\tobj-$(CONFIG_BPF_SYSCALL) += btf.o memalloc.o rqspinlock.o stream.o\n 19:\tifeq ($(CONFIG_MMU)$(CONFIG_64BIT),yy)\n 20:\tobj-$(CONFIG_BPF_SYSCALL) += arena.o range_tree.o\n 21:\tendif\n 22:\tobj-$(CONFIG_BPF_JIT) += dispatcher.o\n 23:\tifeq ($(CONFIG_NET),y)\n 24:\tobj-$(CONFIG_BPF_SYSCALL) += devmap.o\n 25:\tobj-$(CONFIG_BPF_SYSCALL) += cpumap.o\n 26:\tobj-$(CONFIG_BPF_SYSCALL) += offload.o\n 27:\tobj-$(CONFIG_BPF_SYSCALL) += net_namespace.o\n 28:\tobj-$(CONFIG_BPF_SYSCALL) += tcx.o\n 29:\tendif\n 30:\tifeq ($(CONFIG_KEYS),y)\n"
} |
| 25/2 | 2026/09/26 22:11 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 105337 output: 39 thoughts: 873 Thoughts: **Analyzing BPF JIT and Stack Depth Control** Okay, so I'm digging into how the BPF JIT and stack depth are handled. From a build perspective, I know that modules like `verifier.o`, `liveness.o`, `diagnostics.o`, `backtrack.o`, and `states.o` are all conditional on `CONFIG_BPF_SYSCALL`, and `bpf_jit_comp.o` on x86 specifically, is under `CONFIG_BPF_JIT`. The JIT gets enabled when `prog->jit_requested = ebpf_jit_enabled()`, which then checks `bpf_jit_enable` and `bpf_jit_is_ebpf()`. The JIT is effectively *on* if `CONFIG_BPF_JIT_ALWAYS_ON` is set or if `bpf_jit_enable` is 1 via sysctl. Therefore, both `BPF_SYSCALL` and `BPF_JIT` are required for full JIT functionality. Let's quickly check the Kconfig files to confirm any dependencies. `BPF_SYSCALL` seems to select `BPF`, `IRQ_WORK`, `NEED_TASKS_RCU`, `TASKS_TRACE_RCU`, `BINARY_PRINTF`, `NET_SOCK_MSG` (if NET), `NET_XGRESS` (if NET), and `PAGE_POOL` (if NET). `BPF_JIT` depends on `BPF` and either `HAVE_CBPF_JIT` or `HAVE_EBPF_JIT`, and selects `EXECMEM`. So, yes, `BPF_SYSCALL` and `BPF_JIT` are the key configuration options. Now, let's get into the *interesting* bit: specific, high-value functions to trace. I want things that are *not* ubiquitous, but really reveal the stack depth changes. 1. `bpf_check`: The verifier's main entry point, where `env->stack_limit` is set based on the program's defined limit and whether the program re-enters the datapath. 2. `bpf_jit_supports_large_stack`: Specifically added on x86, which is important. 3. `bpf_id_scratch_reserve`: New helper for dynamically allocating scratch space for state equivalence checking. This is interesting. 4. `bpf_push_jmp_history`: Updated in `backtrack.c` to handle multiple linked registers and stack slots. 5. `bpf_mark_chain_precision`: A modified backtracking mechanism, now scanning over `MAX_BPF_STACK_SLOTS` with bitmaps. 6. `check_max_stack_depth`: This function checks the stack depth against `env->stack_limit` instead of a static `MAX_BPF_STACK`. 7. `check_stack_write_fixed_off`: Important: This function handles stack writes based on the new limits and slot accesses. 8. `check_stack_read_fixed_off`: Likewise, reading stack with the new limits. 9. `subprog_spill_slots`: A *newly* added function which computes tracked spill slots per subprog, meaning new stack management for subprograms. 10. `widen_frame_masks`: Also *new*, in `liveness.c` and is important: this function is designed to widen the frame mask bit arrays as deeper half-slots are touched. Hold on a second... `subprog_spill_slots` and `widen_frame_masks` are static? Yes, they are indeed in `liveness.c`. `compute_subprog_args` is also static in `liveness.c`. Hmm. Let's look at `liveness.c` more closely. What *non-static*, thus exported/globally visible, functions are there in `liveness.c`? That would be much more interesting. |
| 26/2 | 2026/09/26 22:11 | tool | grepper |
0mArgs:{
"Expression": "^int bpf_|^void bpf_",
"PathPrefix": "kernel/bpf/liveness.c"
}Results: {
"Output": "kernel/bpf/liveness.c=130=static struct func_instance *lookup_instance(struct bpf_verifier_env *env,\n--\nkernel/bpf/liveness.c-148-\nkernel/bpf/liveness.c:149:int bpf_stack_liveness_init(struct bpf_verifier_env *env)\nkernel/bpf/liveness.c-150-{\n--\nkernel/bpf/liveness.c-157-\nkernel/bpf/liveness.c:158:void bpf_stack_liveness_free(struct bpf_verifier_env *env)\nkernel/bpf/liveness.c-159-{\n--\nkernel/bpf/liveness.c=281=static int mark_stack_read_mask(struct func_instance *instance, u32 frame, u32 insn_idx,\n--\nkernel/bpf/liveness.c-302-\nkernel/bpf/liveness.c:303:int bpf_jmp_offset(struct bpf_insn *insn)\nkernel/bpf/liveness.c-304-{\n--\nkernel/bpf/liveness.c=441=static bool is_live_before(struct func_instance *instance, u32 insn_idx, u32 frameno, u32 half_spi)\n--\nkernel/bpf/liveness.c-450-\nkernel/bpf/liveness.c:451:int bpf_live_stack_query_init(struct bpf_verifier_env *env, struct bpf_verifier_state *st)\nkernel/bpf/liveness.c-452-{\n--\nkernel/bpf/liveness.c=2055=static int analyze_subprog(struct bpf_verifier_env *env,\n--\nkernel/bpf/liveness.c-2206-\nkernel/bpf/liveness.c:2207:int bpf_compute_subprog_arg_access(struct bpf_verifier_env *env)\nkernel/bpf/liveness.c-2208-{\n--\nkernel/bpf/liveness.c=2283=static void compute_insn_live_regs(struct bpf_verifier_env *env,\n--\nkernel/bpf/liveness.c-2450- */\nkernel/bpf/liveness.c:2451:int bpf_compute_live_registers(struct bpf_verifier_env *env)\nkernel/bpf/liveness.c-2452-{\n"
} |
| 27/2 | 2026/09/26 22:11 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 107776 output: 38 thoughts: 63 Thoughts: Here's my take on the thought process, as requested: **Diving into BPF Argument Access** Okay, so I'm digging into how BPF subprogram arguments are being handled, and the key function to start with is `bpf_compute_subprog_arg_access` – it's right there in `kernel/bpf/liveness.c`. This suggests a connection with how the kernel figures out which arguments a subprogram *actually* uses. Specifically, it seems like `bpf_compute_subprog_arg_access` is calling `compute_subprog_args`. Got it. Now, I need to get down into the details of what's happening. Lines 2206 through 2240 of `kernel/bpf/liveness.c` are probably where the magic is happening. Let's see what the code is *actually* doing. I'm especially interested in whether the argument access computation is done with a data-flow analysis, or some other method, and how efficiently it is done. |
| 28/2 | 2026/09/26 22:11 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/liveness.c",
"FirstLine": 2205,
"LineCount": 40
}Results: {
"Contents": "2205:\t}\n2206:\t\n2207:\tint bpf_compute_subprog_arg_access(struct bpf_verifier_env *env)\n2208:\t{\n2209:\t\tu32 callsites[MAX_CALL_FRAMES] = {};\n2210:\t\tint insn_cnt = env-\u003eprog-\u003elen;\n2211:\t\tstruct func_instance *instance;\n2212:\t\tstruct subprog_at_info *info;\n2213:\t\tint k, err = 0;\n2214:\t\n2215:\t\tinfo = kvzalloc_objs(*info, env-\u003esubprog_cnt, GFP_KERNEL_ACCOUNT);\n2216:\t\tif (!info)\n2217:\t\t\treturn -ENOMEM;\n2218:\t\n2219:\t\tenv-\u003ecallsite_at_stack = kvzalloc_objs(*env-\u003ecallsite_at_stack, insn_cnt,\n2220:\t\t\t\t\t\t GFP_KERNEL_ACCOUNT);\n2221:\t\tif (!env-\u003ecallsite_at_stack) {\n2222:\t\t\tkvfree(info);\n2223:\t\t\treturn -ENOMEM;\n2224:\t\t}\n2225:\t\n2226:\t\t/*\n2227:\t\t * Analyze every subprog in reverse topological order (callers\n2228:\t\t * before callees) so that each subprog is analyzed before its\n2229:\t\t * callees, allowing the recursive walk inside analyze_subprog()\n2230:\t\t * to naturally reach callees that receive FP-derived args.\n2231:\t\t *\n2232:\t\t * Subprogs and callbacks that don't receive FP-derived arguments\n2233:\t\t * cannot access ancestor stack frames are analyzed independently.\n2234:\t\t * Async callbacks (timer, workqueue) are handled the same way.\n2235:\t\t */\n2236:\t\tfor (k = env-\u003esubprog_cnt - 1; k \u003e= 0; k--) {\n2237:\t\t\tint sub = env-\u003esubprog_topo_order[k];\n2238:\t\n2239:\t\t\tif (info[sub].at_in \u0026\u0026 !bpf_subprog_is_global(env, sub))\n2240:\t\t\t\tcontinue;\n2241:\t\t\tinstance = call_instance(env, NULL, 0, sub);\n2242:\t\t\tif (IS_ERR(instance)) {\n2243:\t\t\t\terr = PTR_ERR(instance);\n2244:\t\t\t\tgoto out;\n"
} |
| 29/2 | 2026/09/26 22:11 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 108761 output: 310 thoughts: 1088 Thoughts: **Analysis of BPF Stack Size Increase and Verification Implications** Okay, so `bpf_compute_subprog_arg_access` seems to be the entry point for this liveness analysis in `liveness.c`. I need to follow its trail. Let's start by looking into `bpf_is_state_visited` in `kernel/bpf/states.c`. It goes through `states_equal`, which then leads to `stacksafe`, down to `check_ids`, and finally to `bpf_id_scratch_reserve`. Now, let me check `bpf_fmt_stack_mask` in `kernel/bpf/backtrack.c`; it's exported in `include/linux/bpf_verifier.h`. Then I'll check `bpf_push_jmp_history`, which also isn't static, and then I'll look into `bpf_mark_chain_precision`, not static either. I should also check `bpf_id_scratch_reserve`, again not static. And, lastly, let's see `bpf_jit_supports_large_stack`; it's important. I should also look at `check_max_stack_depth_subprog` in `kernel/bpf/verifier.c`. Right, let's review the required parameters for `set-results`. Looks good: `WorthFuzzing`, `Reasoning`, `EnableConfigs`, and `FocusSymbols`. Okay, let's double-check the reasoning: The core of this is the increase in the maximum BPF program stack size from 512 bytes (MAX_BPF_STACK) to 2048 bytes (MAX_BPF_STACK_JIT) for architectures that support large stacks (x86_64, arm64). This patch series is a deep dive into fundamental modifications of core verifier data structures and algorithms, spanning across `kernel/bpf/verifier.c`, `kernel/bpf/liveness.c`, `kernel/bpf/backtrack.c`, and `kernel/bpf/states.c`: * It involves changing stack slot representations from fixed 64-bit masks (`u64`) to dynamically sized bitmaps (`unsigned long stack_masks[...][BITS_TO_LONGS(MAX_BPF_STACK_SLOTS)]`). * It implements dynamic resizing of frame masks in liveness analysis using functions like `widen_frame_masks` and `subprog_spill_slots`. * Jump history tracking for linked scalar registers and spilled scalars is altered by packing 11-bit slot/reg indices. * It introduces dynamic reallocation of scratch space with `bpf_id_scratch_reserve` for state equivalence checks. * There are updates to stack bounds and depth checks (`check_max_stack_depth_subprog`, `check_stack_slot_within_bounds`, `retrieve_ptr_limit`, `bpf_prog_reenters_datapath`). These changes hit the core BPF subsystem, which is totally accessible via user-space syscalls (`bpf(BPF_PROG_LOAD, ...)`) and executed in standard virtualized environments. Fuzzing is *absolutely* critical to catch potential regressions, out-of-bounds stack or bitmap accesses, and any soundness issues or invariant violations. Let's nail down the `EnableConfigs`: `BPF_SYSCALL` and `BPF_JIT`. And my `FocusSymbols`: `bpf_jit_supports_large_stack`, `bpf_id_scratch_reserve`, `bpf_push_jmp_history`, `bpf_mark_chain_precision`, `bpf_compute_subprog_arg_access`, `check_max_stack_depth_subprog`, `check_stack_write_fixed_off`, `check_stack_read_fixed_off`, and `stacksafe`. Everything seems justified, precise, and correct. Let's make the tool call! |
| 30/2 | 2026/09/26 22:11 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT"
],
"FocusSymbols": [
"bpf_jit_supports_large_stack",
"bpf_id_scratch_reserve",
"bpf_push_jmp_history",
"bpf_mark_chain_precision",
"bpf_compute_subprog_arg_access",
"check_max_stack_depth_subprog",
"check_stack_write_fixed_off",
"check_stack_read_fixed_off",
"stacksafe"
],
"Reasoning": "The patch increases the maximum BPF program stack size from 512 bytes to 2048 bytes (MAX_BPF_STACK_JIT) on architectures with JIT support (including x86-64). This introduces widespread alterations across core BPF subsystem files (verifier.c, liveness.c, backtrack.c, states.c) to handle larger stack limits: changing fixed 64-bit stack masks to multi-word bitmaps, dynamically widening frame masks in liveness tracking, repacking jump history entries for linked registers and slots, dynamically allocating scratch tables for verifier state equivalence checks, and adjusting stack depth checks and pointer arithmetic bounds. These changes modify critical security invariants and verifier state pruning logic reachable from unprivileged/privileged BPF program loading.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT"
],
"FocusSymbols": [
"bpf_jit_supports_large_stack",
"bpf_id_scratch_reserve",
"bpf_push_jmp_history",
"bpf_mark_chain_precision",
"bpf_compute_subprog_arg_access",
"check_max_stack_depth_subprog",
"check_stack_write_fixed_off",
"check_stack_read_fixed_off",
"stacksafe"
],
"Reasoning": "The patch increases the maximum BPF program stack size from 512 bytes to 2048 bytes (MAX_BPF_STACK_JIT) on architectures with JIT support (including x86-64). This introduces widespread alterations across core BPF subsystem files (verifier.c, liveness.c, backtrack.c, states.c) to handle larger stack limits: changing fixed 64-bit stack masks to multi-word bitmaps, dynamically widening frame masks in liveness tracking, repacking jump history entries for linked registers and slots, dynamically allocating scratch tables for verifier state equivalence checks, and adjusting stack depth checks and pointer arithmetic bounds. These changes modify critical security invariants and verifier state pruning logic reachable from unprivileged/privileged BPF program loading.",
"WorthFuzzing": true
} |
| 31/1 | 2026/09/26 22:11 | action | If |
2mArgs:{
"WorthFuzzing": true
}Results: null |
| 32/2 | 2026/09/26 22:11 | agent | kmsan-evaluator |
2mModel:core Args: null Results: {
"KMSANReasoning": "The patch series increases the maximum BPF stack limit from 512 bytes to 2048 bytes on architectures with JIT support for large stacks (x86-64 and arm64). The verifier changes update internal tracking data structures to accommodate 256 stack slots (from 64), replacing u64 bitmasks with dynamically widened or statically sized bitmaps (e.g., in backtrack_state, frame_masks, and scratched_stack_slots), dynamically growing scratch arrays for id mapping (bpf_idmap, bpf_idset), and adjusting bounds checks.\n\nContrasting KASAN vs. KMSAN applicability:\n- KMSAN applicability: KMSAN detects use of uninitialized memory and info-leaks to user space. In this patch series:\n 1. All new or modified data structures allocated on heap/stack are fully initialized: `frame_masks` is allocated via `kvzalloc_flex`, local arrays like `new_after` are zero-initialized (`= {}`), `at_stack_in` is fully populated with `unvisited` markers, `spill_snapshot` is populated with `memcpy`, and dynamically grown entries in `bpf_idmap`/`bpf_idset` have their fields (`old`, `cur`, `id`, `cnt`) explicitly set upon growth.\n 2. In `bpf_jmp_history_entry`, all fields are explicitly assigned (`idx`, `prev_idx`, `flags`, `spi`, `frame`, `linked_regs_cnt`), and `linked_regs` is copied and accessed strictly up to `linked_regs_cnt`.\n 3. No kernel-to-user information leaks are introduced; verifier logging to user buffers is formatted strings with null terminators.\n 4. The BPF stack initialization rules enforced by the verifier (disallowing reads from uninitialized slots unless permitted by CAP_PERFMON token) remain unchanged in semantics, and the interpreter stack remains at 512 bytes with its explicit KMSAN unpoisoning intact.\n- KASAN applicability: The risks associated with this patch primarily concern potential out-of-bounds accesses or buffer overruns due to bitmap indexing, array bounds, slice reallocations, or kernel stack exhaustion (checked via KASAN, FORTIFY_SOURCE, and VMAP_STACK).\n\nSince no uninitialized memory risks or kernel-to-user info-leaks are introduced or exposed, dedicated KMSAN fuzzing is not warranted.",
"NeedsKMSAN": false
}Instruction: You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.
CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
a comprehensive suite of debugging tools and sanitizers, including KASAN
(out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
(locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
or page allocations) and kernel-to-user memory info-leaks.
Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.
Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.
Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
attributes, ioctl output arguments, socket options, or BPF buffers) where fields
or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
uninitialized bytes of existing buffers.
Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
dereferences, locking deadlocks, or use-after-free bugs (these are already caught
by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.
Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit 6d73c2266fed3339a271d57f99edbf7dc11e3ac3
Author: syz-cluster <triage@syzkaller.com>
Date: Sat Sep 26 22:10:37 2026 +0000
syz-cluster: applied patch under review
diff --git a/Documentation/bpf/bpf_design_QA.rst b/Documentation/bpf/bpf_design_QA.rst
index eb19c945f4d57..98d70b8a2533b 100644
--- a/Documentation/bpf/bpf_design_QA.rst
+++ b/Documentation/bpf/bpf_design_QA.rst
@@ -221,9 +221,14 @@ newer kernels. BPF programs need to change accordingly when this happens.
Q: How much stack space a BPF program uses?
-------------------------------------------
-A: Currently all program types are limited to 512 bytes of stack
-space, but the verifier computes the actual amount of stack used
-and both interpreter and most JITed code consume necessary amount.
+A: A program may use up to 2 KiB of stack, combined over its call
+chain, when the JIT of the architecture reports support for large
+stacks (currently x86-64 and arm64); a single function may use all of
+it, and every frame of a program running on a private stack gets the
+whole amount. Elsewhere, and for programs verified for the interpreter,
+the limit is 512 bytes; the interpreter itself never runs a frame
+larger than that. The verifier computes the actual amount of stack
+used and both interpreter and most JITed code consume necessary amount.
Q: Can BPF be offloaded to HW?
------------------------------
diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
index 9544b2f483e59..2b2d6d178ea1d 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -2501,6 +2501,11 @@ bool bpf_jit_supports_callx(void)
return true;
}
+bool bpf_jit_supports_large_stack(void)
+{
+ return true;
+}
+
static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_node *node,
int bargs_off, int retval_off, int run_ctx_off,
bool save_ret)
diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
index 9fbef7504e51a..e2e531dd1e0bc 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -4520,6 +4520,18 @@ bool bpf_jit_supports_subprog_tailcalls(void)
return true;
}
+/*
+ * Frame sizes are 32-bit immediates in the prologue, epilogue and tail call
+ * sequences, a tail call pops the caller's frame and lands in the target's
+ * prologue before the target allocates its own, and private stacks are
+ * allocated from the program's own depth, so MAX_BPF_STACK_JIT frames need
+ * nothing special.
+ */
+bool bpf_jit_supports_large_stack(void)
+{
+ return true;
+}
+
bool bpf_jit_supports_percpu_insn(void)
{
return true;
diff --git a/drivers/net/ethernet/netronome/nfp/bpf/verifier.c b/drivers/net/ethernet/netronome/nfp/bpf/verifier.c
index 1caa87da72b51..9ddbfe9d60194 100644
--- a/drivers/net/ethernet/netronome/nfp/bpf/verifier.c
+++ b/drivers/net/ethernet/netronome/nfp/bpf/verifier.c
@@ -105,7 +105,7 @@ static bool nfp_bpf_map_update_value_ok(struct bpf_verifier_env *env)
unsigned int soff;
soff = -(off + i) - 1;
- stack_entry = &state->stack[soff / BPF_REG_SIZE];
+ stack_entry = bpf_stack_slot(state, soff / BPF_REG_SIZE);
if (stack_entry->slot_type[soff % BPF_REG_SIZE] == STACK_ZERO)
continue;
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 578bccf5cf767..f151feeaf3a55 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -19,11 +19,12 @@
* that converting umax_value to int cannot overflow.
*/
#define BPF_MAX_VAR_SIZ (1 << 29)
-/* size of tmp_str_buf in bpf_verifier.
- * we need at least 306 bytes to fit full stack mask representation
- * (in the "-8,-16,...,-512" form)
+/*
+ * size of tmp_str_buf in bpf_verifier.
+ * we need at least 1399 bytes to fit full stack mask representation
+ * (in the "-8,-16,...,-2048" form)
*/
-#define TMP_STR_BUF_LEN 320
+#define TMP_STR_BUF_LEN 1408
/* Patch buffer size */
#define INSN_BUF_SIZE 32
@@ -242,55 +243,18 @@ enum bpf_stack_slot_type {
#define BPF_REG_SIZE 8 /* size of eBPF register in bytes */
+/*
+ * Largest number of BPF_REG_SIZE stack slots a single frame can have, sized
+ * for the largest stack budget any JIT supports. A frame may use any part of
+ * its program's budget; check_max_stack_depth() enforces the budget on the
+ * combined depth of frames sharing the kernel stack and on each frame using
+ * a private stack.
+ */
+#define MAX_BPF_STACK_SLOTS (MAX_BPF_STACK_JIT / BPF_REG_SIZE)
+
/* 4-byte stack slot granularity for liveness analysis */
#define BPF_HALF_REG_SIZE 4
#define STACK_SLOT_SZ 4
-#define STACK_SLOTS (MAX_BPF_STACK / BPF_HALF_REG_SIZE) /* 128 */
-
-typedef struct {
- u64 v[2];
-} spis_t;
-
-#define SPIS_ZERO ((spis_t){})
-#define SPIS_ALL ((spis_t){{ U64_MAX, U64_MAX }})
-
-static inline bool spis_is_zero(spis_t s)
-{
- return s.v[0] == 0 && s.v[1] == 0;
-}
-
-static inline bool spis_equal(spis_t a, spis_t b)
-{
- return a.v[0] == b.v[0] && a.v[1] == b.v[1];
-}
-
-static inline spis_t spis_or(spis_t a, spis_t b)
-{
- return (spis_t){{ a.v[0] | b.v[0], a.v[1] | b.v[1] }};
-}
-
-static inline spis_t spis_and(spis_t a, spis_t b)
-{
- return (spis_t){{ a.v[0] & b.v[0], a.v[1] & b.v[1] }};
-}
-
-static inline spis_t spis_not(spis_t s)
-{
- return (spis_t){{ ~s.v[0], ~s.v[1] }};
-}
-
-static inline bool spis_test_bit(spis_t s, u32 slot)
-{
- return s.v[slot / 64] & BIT_ULL(slot % 64);
-}
-
-static inline void spis_or_range(spis_t *mask, u32 lo, u32 hi)
-{
- u32 w;
-
- for (w = lo; w <= hi && w < STACK_SLOTS; w++)
- mask->v[w / 64] |= BIT_ULL(w % 64);
-}
#define BPF_REGMASK_ARGS ((1 << BPF_REG_1) | (1 << BPF_REG_2) | \
(1 << BPF_REG_3) | (1 << BPF_REG_4) | \
@@ -420,30 +384,33 @@ enum {
INSN_F_STACK_ARG_ACCESS = BIT(3),
};
+/* Registers linked to one jump condition that a history entry can record */
+#define BPF_LINKED_REGS_MAX 5
+
struct bpf_jmp_history_entry {
/* insn idx can't be bigger than 1 million */
u32 idx : 20;
u32 frame : 4; /* stack access frame number */
- u32 spi : 6; /* stack slot index (0..63) */
- u32 : 2;
- u32 prev_idx : 20;
/* special INSN_F_xxx flags */
u32 flags : 4;
- u32 : 8;
+ u32 : 4;
+ u32 prev_idx : 20;
+ u32 spi : 12; /* stack slot index */
/*
- * additional registers that need precision tracking when this
- * jump is backtracked, vector of five 11-bit records
+ * Scalar registers and spilled scalars linked to the condition of
+ * this jump, which need precision tracking together when the jump is
+ * backtracked. Each is packed as 4 bits of frame number, one bit
+ * telling a register from a stack slot and 11 bits of register or
+ * slot index, see linked_regs_pack().
*/
- u64 linked_regs;
+ u16 linked_regs[BPF_LINKED_REGS_MAX];
+ u8 linked_regs_cnt;
};
static_assert(MAX_CALL_FRAMES <= (1 << 4));
-static_assert(MAX_BPF_STACK / 8 <= (1 << 6));
+static_assert(MAX_BPF_STACK_SLOTS <= (1 << 12));
-/* Maximum number of bpf_reg_state objects that can exist at once */
#define MAX_STACK_ARG_SLOTS (MAX_BPF_FUNC_ARGS - MAX_BPF_FUNC_REG_ARGS)
-#define BPF_ID_MAP_SIZE ((MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE + \
- MAX_STACK_ARG_SLOTS) * MAX_CALL_FRAMES)
struct bpf_verifier_state {
/* call stack tracking */
struct bpf_func_state *frame[MAX_CALL_FRAMES];
@@ -529,12 +496,31 @@ struct bpf_verifier_state {
u32 may_goto_depth;
};
+/* Number of BPF_REG_SIZE stack slots tracked for the frame so far. */
+static inline u32 bpf_stack_nr_slots(const struct bpf_func_state *frame)
+{
+ return frame->allocated_stack / BPF_REG_SIZE;
+}
+
+/*
+ * Stack slot @spi of @frame, covering bytes [fp - (spi + 1) * 8, fp - spi * 8).
+ * The caller must ensure spi < bpf_stack_nr_slots(frame), see grow_stack_state().
+ */
+static inline struct bpf_stack_state *bpf_stack_slot(const struct bpf_func_state *frame, u32 spi)
+{
+ return &frame->stack[spi];
+}
+
static inline struct bpf_reg_state *
bpf_get_spilled_reg(int slot, struct bpf_func_state *frame, u32 mask)
{
- if (slot < frame->allocated_stack / BPF_REG_SIZE &&
- (1 << frame->stack[slot].slot_type[BPF_REG_SIZE - 1]) & mask)
- return &frame->stack[slot].spilled_ptr;
+ struct bpf_stack_state *ss;
+
+ if (slot >= bpf_stack_nr_slots(frame))
+ return NULL;
+ ss = bpf_stack_slot(frame, slot);
+ if ((1 << ss->slot_type[BPF_REG_SIZE - 1]) & mask)
+ return &ss->spilled_ptr;
return NULL;
}
@@ -550,7 +536,7 @@ bpf_get_spilled_stack_arg(int slot, struct bpf_func_state *frame)
/* Iterate over 'frame', setting 'reg' to either NULL or a spilled register. */
#define bpf_for_each_spilled_reg(iter, frame, reg, mask) \
for (iter = 0, reg = bpf_get_spilled_reg(iter, frame, mask); \
- iter < frame->allocated_stack / BPF_REG_SIZE; \
+ iter < bpf_stack_nr_slots(frame); \
iter++, reg = bpf_get_spilled_reg(iter, frame, mask))
/* Iterate over 'frame', setting 'reg' to either NULL or a spilled stack arg. */
@@ -575,7 +561,7 @@ bpf_get_spilled_stack_arg(int slot, struct bpf_func_state *frame)
bpf_for_each_spilled_reg(___j, __state, __reg, __mask) { \
if (!__reg) \
continue; \
- __stack = &__state->stack[___j]; \
+ __stack = bpf_stack_slot(__state, ___j); \
(void)(__expr); \
} \
__stack = NULL; \
@@ -733,7 +719,11 @@ struct bpf_insn_aux_data {
#define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */
#define MAX_USED_BTFS 64 /* max number of BTFs accessed by one BPF program */
-#define BPF_VERIFIER_TMP_LOG_SIZE 1024
+/*
+ * Longest line the verifier log can carry: a full stack mask of
+ * MAX_BPF_STACK_SLOTS slots, see TMP_STR_BUF_LEN, plus its prefix.
+ */
+#define BPF_VERIFIER_TMP_LOG_SIZE 2048
struct bpf_verifier_log {
/* Logical start and end positions of a "log window" of the verifier log.
@@ -865,7 +855,7 @@ struct backtrack_state {
struct bpf_verifier_env *env;
u32 frame;
u32 reg_masks[MAX_CALL_FRAMES];
- u64 stack_masks[MAX_CALL_FRAMES];
+ unsigned long stack_masks[MAX_CALL_FRAMES][BITS_TO_LONGS(MAX_BPF_STACK_SLOTS)];
u8 stack_arg_masks[MAX_CALL_FRAMES];
};
@@ -874,18 +864,27 @@ struct bpf_id_pair {
u32 cur;
};
+/*
+ * Scratch map from the ids of one verifier state to those of another, also
+ * used as a stack of ids. Grown on demand by bpf_id_scratch_reserve().
+ */
struct bpf_idmap {
u32 tmp_id_gen;
u32 cnt;
- struct bpf_id_pair map[BPF_ID_MAP_SIZE];
+ u32 cap;
+ struct bpf_id_pair *map;
};
+struct bpf_idset_entry {
+ u32 id;
+ u32 cnt;
+};
+
+/* Scratch set of ids with a use count each, grown on demand */
struct bpf_idset {
u32 num_ids;
- struct {
- u32 id;
- u32 cnt;
- } entries[BPF_ID_MAP_SIZE];
+ u32 cap;
+ struct bpf_idset_entry *entries;
};
/* see verifier.c:compute_scc_callchain() */
@@ -994,10 +993,8 @@ struct bpf_verifier_env {
* via callx. Allocated when the first such edge is recorded.
*/
unsigned long *callx_edges;
- union {
- struct bpf_idmap idmap_scratch;
- struct bpf_idset idset_scratch;
- };
+ struct bpf_idmap idmap_scratch;
+ struct bpf_idset idset_scratch;
struct {
int *insn_state;
int *insn_stack;
@@ -1012,8 +1009,8 @@ struct bpf_verifier_env {
} cfg;
struct backtrack_state bt;
struct bpf_jmp_history_entry *cur_hist_ent;
- /* Per-callsite copy of parent's converged at_stack_in for cross-frame fills. */
- struct arg_track **callsite_at_stack;
+ /* Per-callsite snapshot of the parent's tracked spill slots for cross-frame fills. */
+ struct spill_snapshot **callsite_at_stack;
u32 pass_cnt; /* number of times do_check() was called */
u32 subprog_cnt;
/* number of instructions analyzed by the verifier */
@@ -1022,6 +1019,8 @@ struct bpf_verifier_env {
u32 prev_jmps_processed, jmps_processed;
/* maximum combined stack depth */
u32 max_stack_depth;
+ /* stack budget of the program, see bpf_prog_stack_limit() */
+ u32 stack_limit;
/* total verification time */
u64 verification_time;
/* maximum number of verifier states kept in 'branching' instructions */
@@ -1057,7 +1056,7 @@ struct bpf_verifier_env {
*/
u32 scratched_regs;
/* Same as scratched_regs but for stack slots */
- u64 scratched_stack_slots;
+ DECLARE_BITMAP(scratched_stack_slots, MAX_BPF_STACK_SLOTS);
u64 prev_log_pos, prev_insn_print_pos;
/* buffer used to temporary hold constants as scalar registers */
struct bpf_reg_state fake_reg[1];
@@ -1261,8 +1260,10 @@ int bpf_copy_verifier_state(struct bpf_verifier_state *dst_state,
struct list_head *bpf_explored_state(struct bpf_verifier_env *env, int idx);
void bpf_free_verifier_state(struct bpf_verifier_state *state, bool free_self);
void bpf_free_backedges(struct bpf_scc_visit *visit);
+bool bpf_id_scratch_reserve(void **arr, u32 *cap, u32 cnt, size_t elem_size);
int bpf_push_jmp_history(struct bpf_verifier_env *env, struct bpf_verifier_state *cur,
- int insn_flags, int spi, int frame, u64 linked_regs);
+ int insn_flags, int spi, int frame, const u16 *linked_regs,
+ u8 linked_regs_cnt);
void bpf_bt_sync_linked_regs(struct backtrack_state *bt, struct bpf_jmp_history_entry *hist);
void bpf_mark_reg_not_init(const struct bpf_verifier_env *env,
struct bpf_reg_state *reg);
@@ -1279,6 +1280,24 @@ static inline int bpf_get_spi(s32 off)
return (-off - 1) / BPF_REG_SIZE;
}
+/*
+ * Stack a program may use in total: combined over the frames of a call
+ * chain on the kernel stack, or per frame on a private stack. Any single
+ * frame may reach that deep. Only a JIT that lays out such frames may go
+ * beyond MAX_BPF_STACK, the interpreter's frame size, and only one whose
+ * tail calls let the target set up its own frame: without subprogram
+ * tail calls, do_misc_fixups() gives every program with tail calls a
+ * MAX_BPF_STACK frame, which a deeper frame would overrun.
+ */
+static inline u32 bpf_prog_stack_limit(const struct bpf_prog *prog)
+{
+ /* an offloaded program never runs on the host JIT, whatever it supports */
+ if (prog->jit_requested && !bpf_prog_is_offloaded(prog->aux) &&
+ bpf_jit_supports_large_stack() && bpf_jit_supports_subprog_tailcalls())
+ return MAX_BPF_STACK_JIT;
+ return MAX_BPF_STACK;
+}
+
static inline struct bpf_func_state *bpf_func(struct bpf_verifier_env *env,
const struct bpf_reg_state *reg)
{
@@ -1322,12 +1341,7 @@ static inline void bpf_bt_set_frame_reg(struct backtrack_state *bt, u32 frame, u
static inline void bpf_bt_set_frame_slot(struct backtrack_state *bt, u32 frame, u32 slot)
{
- bt->stack_masks[frame] |= 1ull << slot;
-}
-
-static inline void bpf_bt_set_frame_slot_mask(struct backtrack_state *bt, u32 frame, u64 mask)
-{
- bt->stack_masks[frame] |= mask;
+ __set_bit(slot, bt->stack_masks[frame]);
}
static inline void bt_set_frame_stack_arg_slot(struct backtrack_state *bt, u32 frame, u32 slot)
@@ -1342,7 +1356,7 @@ static inline bool bt_is_frame_reg_set(struct backtrack_state *bt, u32 frame, u3
static inline bool bt_is_frame_slot_set(struct backtrack_state *bt, u32 frame, u32 slot)
{
- return bt->stack_masks[frame] & (1ull << slot);
+ return test_bit(slot, bt->stack_masks[frame]);
}
bool bpf_map_is_rdonly(const struct bpf_map *map);
@@ -1480,7 +1494,7 @@ static inline void mark_reg_scratched(struct bpf_verifier_env *env, u32 regno)
static inline void mark_stack_slot_scratched(struct bpf_verifier_env *env, u32 spi)
{
- env->scratched_stack_slots |= 1ULL << spi;
+ __set_bit(spi, env->scratched_stack_slots);
}
static inline bool reg_scratched(const struct bpf_verifier_env *env, u32 regno)
@@ -1488,27 +1502,28 @@ static inline bool reg_scratched(const struct bpf_verifier_env *env, u32 regno)
return (env->scratched_regs >> regno) & 1;
}
-static inline bool stack_slot_scratched(const struct bpf_verifier_env *env, u64 regno)
+static inline bool stack_slot_scratched(const struct bpf_verifier_env *env, u32 spi)
{
- return (env->scratched_stack_slots >> regno) & 1;
+ return test_bit(spi, env->scratched_stack_slots);
}
static inline bool verifier_state_scratched(const struct bpf_verifier_env *env)
{
- return env->scratched_regs || env->scratched_stack_slots;
+ return env->scratched_regs ||
+ !bitmap_empty(env->scratched_stack_slots, MAX_BPF_STACK_SLOTS);
}
static inline void mark_verifier_state_clean(struct bpf_verifier_env *env)
{
env->scratched_regs = 0U;
- env->scratched_stack_slots = 0ULL;
+ bitmap_zero(env->scratched_stack_slots, MAX_BPF_STACK_SLOTS);
}
/* Used for printing the entire verifier state. */
static inline void mark_verifier_state_scratched(struct bpf_verifier_env *env)
{
env->scratched_regs = ~0U;
- env->scratched_stack_slots = ~0ULL;
+ bitmap_fill(env->scratched_stack_slots, MAX_BPF_STACK_SLOTS);
}
static inline bool bpf_stack_narrow_access_ok(int off, int fill_size, int spill_size)
@@ -1543,7 +1558,7 @@ struct bpf_subprog_info *bpf_find_containing_subprog(struct bpf_verifier_env *en
const char *bpf_subprog_name(const struct bpf_verifier_env *env, int subprog);
int bpf_jmp_offset(struct bpf_insn *insn);
struct bpf_iarray *bpf_insn_successors(struct bpf_verifier_env *env, u32 idx);
-void bpf_fmt_stack_mask(char *buf, ssize_t buf_sz, u64 stack_mask);
+void bpf_fmt_stack_mask(char *buf, ssize_t buf_sz, const unsigned long *stack_mask);
bool bpf_subprog_is_global(const struct bpf_verifier_env *env, int subprog);
/* Kinds of member a by-value struct or union may be composed of. */
diff --git a/include/linux/filter.h b/include/linux/filter.h
index 4f0662e428970..e42eccb0990e6 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -98,6 +98,11 @@ struct ctl_table_header;
/* BPF program can access up to 512 bytes of stack space. */
#define MAX_BPF_STACK 512
+/*
+ * Stack budget of a program on a JIT that lays out frames of that size.
+ * The interpreter and JITs without such support keep MAX_BPF_STACK.
+ */
+#define MAX_BPF_STACK_JIT 2048
/* Helper macros for filter block array initializers. */
@@ -1247,6 +1252,7 @@ bool bpf_jit_supports_ptr_xchg(void);
bool bpf_jit_supports_arena(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);
bool bpf_jit_supports_timed_may_goto(void);
bool bpf_jit_supports_fsession(void);
diff --git a/kernel/bpf/backtrack.c b/kernel/bpf/backtrack.c
index 4da99dec08184..0e38b9575328c 100644
--- a/kernel/bpf/backtrack.c
+++ b/kernel/bpf/backtrack.c
@@ -9,7 +9,8 @@
/* for any branch, call, exit record the history of jmps in the given state */
int bpf_push_jmp_history(struct bpf_verifier_env *env, struct bpf_verifier_state *cur,
- int insn_flags, int spi, int frame, u64 linked_regs)
+ int insn_flags, int spi, int frame, const u16 *linked_regs,
+ u8 linked_regs_cnt)
{
u32 cnt = cur->jmp_history_cnt;
struct bpf_jmp_history_entry *p;
@@ -27,10 +28,13 @@ int bpf_push_jmp_history(struct bpf_verifier_env *env, struct bpf_verifier_state
env->cur_hist_ent->flags |= insn_flags;
env->cur_hist_ent->spi = spi;
env->cur_hist_ent->frame = frame;
- verifier_bug_if(env->cur_hist_ent->linked_regs != 0, env,
- "insn history: insn_idx %d linked_regs: %#llx",
- env->insn_idx, env->cur_hist_ent->linked_regs);
- env->cur_hist_ent->linked_regs = linked_regs;
+ verifier_bug_if(env->cur_hist_ent->linked_regs_cnt != 0, env,
+ "insn history: insn_idx %d has %u linked regs",
+ env->insn_idx, env->cur_hist_ent->linked_regs_cnt);
+ if (linked_regs_cnt)
+ memcpy(env->cur_hist_ent->linked_regs, linked_regs,
+ linked_regs_cnt * sizeof(*linked_regs));
+ env->cur_hist_ent->linked_regs_cnt = linked_regs_cnt;
return 0;
}
@@ -47,7 +51,9 @@ int bpf_push_jmp_history(struct bpf_verifier_env *env, struct bpf_verifier_state
p->flags = insn_flags;
p->spi = spi;
p->frame = frame;
- p->linked_regs = linked_regs;
+ if (linked_regs_cnt)
+ memcpy(p->linked_regs, linked_regs, linked_regs_cnt * sizeof(*linked_regs));
+ p->linked_regs_cnt = linked_regs_cnt;
cur->jmp_history_cnt = cnt;
env->cur_hist_ent = p;
@@ -123,13 +129,26 @@ static inline void bt_reset(struct backtrack_state *bt)
bt->env = env;
}
-static inline u32 bt_empty(struct backtrack_state *bt)
+static inline bool bt_frame_stack_empty(struct backtrack_state *bt, u32 frame)
{
- u64 mask = 0;
+ return bitmap_empty(bt->stack_masks[frame], MAX_BPF_STACK_SLOTS);
+}
+
+static inline bool bt_stack_empty(struct backtrack_state *bt)
+{
+ return bt_frame_stack_empty(bt, bt->frame);
+}
+
+static inline bool bt_empty(struct backtrack_state *bt)
+{
+ u32 mask = 0;
int i;
- for (i = 0; i <= bt->frame; i++)
- mask |= bt->reg_masks[i] | bt->stack_masks[i] | bt->stack_arg_masks[i];
+ for (i = 0; i <= bt->frame; i++) {
+ mask |= bt->reg_masks[i] | bt->stack_arg_masks[i];
+ if (!bt_frame_stack_empty(bt, i))
+ return false;
+ }
return mask == 0;
}
@@ -181,7 +200,7 @@ static inline void bt_clear_reg(struct backtrack_state *bt, u32 reg)
static inline void bt_clear_frame_slot(struct backtrack_state *bt, u32 frame, u32 slot)
{
- bt->stack_masks[frame] &= ~(1ull << slot);
+ __clear_bit(slot, bt->stack_masks[frame]);
}
static inline u32 bt_frame_reg_mask(struct backtrack_state *bt, u32 frame)
@@ -194,14 +213,14 @@ static inline u32 bt_reg_mask(struct backtrack_state *bt)
return bt->reg_masks[bt->frame];
}
-static inline u64 bt_frame_stack_mask(struct backtrack_state *bt, u32 frame)
+static inline unsigned long *bt_frame_stack_mask(struct backtrack_state *bt, u32 frame)
{
return bt->stack_masks[frame];
}
-static inline u64 bt_stack_mask(struct backtrack_state *bt)
+static inline unsigned long *bt_stack_mask(struct backtrack_state *bt)
{
- return bt->stack_masks[bt->frame];
+ return bt_frame_stack_mask(bt, bt->frame);
}
static inline u8 bt_stack_arg_mask(struct backtrack_state *bt)
@@ -233,17 +252,16 @@ static void fmt_reg_mask(char *buf, ssize_t buf_sz, u32 reg_mask)
break;
}
}
-/* format stack slots bitmask, e.g., "-8,-24,-40" for 0x15 mask */
-void bpf_fmt_stack_mask(char *buf, ssize_t buf_sz, u64 stack_mask)
+
+/* format stack slots bitmask, e.g., "-8,-24,-40" for slots 0, 2 and 4 */
+void bpf_fmt_stack_mask(char *buf, ssize_t buf_sz, const unsigned long *stack_mask)
{
- DECLARE_BITMAP(mask, 64);
bool first = true;
int i, n;
buf[0] = '\0';
- bitmap_from_u64(mask, stack_mask);
- for_each_set_bit(i, mask, 64) {
+ for_each_set_bit(i, stack_mask, MAX_BPF_STACK_SLOTS) {
n = snprintf(buf, buf_sz, "%s%d", first ? "" : ",", -(i + 1) * 8);
first = false;
buf += n;
@@ -455,10 +473,11 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,
/* we are now tracking register spills correctly,
* so any instance of leftover slots is a bug
*/
- if (bt_stack_mask(bt) != 0) {
- verifier_bug(env,
- "static subprog leftover stack slots %llx",
- bt_stack_mask(bt));
+ if (!bt_stack_empty(bt)) {
+ bpf_fmt_stack_mask(env->tmp_str_buf, TMP_STR_BUF_LEN,
+ bt_stack_mask(bt));
+ verifier_bug(env, "static subprog leftover stack slots %s",
+ env->tmp_str_buf);
return -EFAULT;
}
/* propagate r1-r5 to the caller */
@@ -491,9 +510,11 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,
bt_reg_mask(bt));
return -EFAULT;
}
- if (bt_stack_mask(bt) != 0) {
- verifier_bug(env, "callback leftover stack slots %llx",
- bt_stack_mask(bt));
+ if (!bt_stack_empty(bt)) {
+ bpf_fmt_stack_mask(env->tmp_str_buf, TMP_STR_BUF_LEN,
+ bt_stack_mask(bt));
+ verifier_bug(env, "callback leftover stack slots %s",
+ env->tmp_str_buf);
return -EFAULT;
}
/* clear r1-r5 in callback subprog's mask */
@@ -711,10 +732,12 @@ void bpf_mark_all_scalars_precise(struct bpf_verifier_env *env,
i, j);
}
}
- for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) {
- if (!bpf_is_spilled_reg(&func->stack[j]))
+ for (j = 0; j < bpf_stack_nr_slots(func); j++) {
+ struct bpf_stack_state *ss = bpf_stack_slot(func, j);
+
+ if (!bpf_is_spilled_reg(ss))
continue;
- reg = &func->stack[j].spilled_ptr;
+ reg = &ss->spilled_ptr;
if (reg->type != SCALAR_VALUE || reg->precise)
continue;
reg->precise = true;
@@ -826,6 +849,7 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,
int subseq_idx = -1;
struct bpf_func_state *func;
bool tmp, skip_first = true;
+ struct bpf_stack_state *ss;
struct bpf_reg_state *reg;
int i, fr, err;
@@ -872,7 +896,7 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,
if (st->curframe == 0 &&
st->frame[0]->subprogno > 0 &&
st->frame[0]->callsite == BPF_MAIN_FUNC &&
- bt_stack_mask(bt) == 0 &&
+ bt_stack_empty(bt) &&
(bt_reg_mask(bt) & ~BPF_REGMASK_ARGS) == 0) {
bitmap_from_u64(mask, bt_reg_mask(bt));
for_each_set_bit(i, mask, 32) {
@@ -886,8 +910,9 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,
return 0;
}
- verifier_bug(env, "backtracking func entry subprog %d reg_mask %x stack_mask %llx",
- st->frame[0]->subprogno, bt_reg_mask(bt), bt_stack_mask(bt));
+ bpf_fmt_stack_mask(env->tmp_str_buf, TMP_STR_BUF_LEN, bt_stack_mask(bt));
+ verifier_bug(env, "backtracking func entry subprog %d reg_mask %x stack_mask %s",
+ st->frame[0]->subprogno, bt_reg_mask(bt), env->tmp_str_buf);
return -EFAULT;
}
@@ -948,18 +973,18 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,
}
}
- bitmap_from_u64(mask, bt_frame_stack_mask(bt, fr));
- for_each_set_bit(i, mask, 64) {
- if (verifier_bug_if(i >= func->allocated_stack / BPF_REG_SIZE,
+ for_each_set_bit(i, bt_frame_stack_mask(bt, fr), MAX_BPF_STACK_SLOTS) {
+ if (verifier_bug_if(i >= bpf_stack_nr_slots(func),
env, "stack slot %d, total slots %d",
- i, func->allocated_stack / BPF_REG_SIZE))
+ i, bpf_stack_nr_slots(func)))
return -EFAULT;
- if (!bpf_is_spilled_scalar_reg(&func->stack[i])) {
+ ss = bpf_stack_slot(func, i);
+ if (!bpf_is_spilled_scalar_reg(ss)) {
bt_clear_frame_slot(bt, fr, i);
continue;
}
- reg = &func->stack[i].spilled_ptr;
+ reg = &ss->spilled_ptr;
if (reg->precise) {
bt_clear_frame_slot(bt, fr, i);
} else {
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 273f74068068c..a09aa0082393c 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -3483,6 +3483,19 @@ bool __weak bpf_jit_supports_private_stack(void)
return false;
}
+/*
+ * Return TRUE if the JIT lays out frames of up to MAX_BPF_STACK_JIT bytes.
+ * Its prologue, epilogue and tail call sequences must encode such frame
+ * sizes and a private stack must be sized from the program's depth. The
+ * budget is only granted alongside bpf_jit_supports_subprog_tailcalls(),
+ * whose tail calls land before the target sets up its own frame; see
+ * bpf_prog_stack_limit().
+ */
+bool __weak bpf_jit_supports_large_stack(void)
+{
+ return false;
+}
+
void __weak arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp, u64 bp), void *cookie)
{
}
diff --git a/kernel/bpf/diagnostics.c b/kernel/bpf/diagnostics.c
index 5ecfa86ed49f4..af97aea49075b 100644
--- a/kernel/bpf/diagnostics.c
+++ b/kernel/bpf/diagnostics.c
@@ -1600,9 +1600,9 @@ static struct bpf_reg_state *target_to_reg(struct bpf_verifier_env *env,
return NULL;
return &state->stack_arg_regs[target->stack_arg];
case BPF_DIAG_MOD_TARGET_STACK_SLOT:
- if (target->spi >= state->allocated_stack / BPF_REG_SIZE)
+ if (target->spi >= bpf_stack_nr_slots(state))
return NULL;
- return &state->stack[target->spi].spilled_ptr;
+ return &bpf_stack_slot(state, target->spi)->spilled_ptr;
default:
return NULL;
}
@@ -1618,7 +1618,7 @@ static bool reg_to_target(struct bpf_verifier_env *env, const struct bpf_reg_sta
for (frame = 0; frame <= vstate->curframe; frame++) {
struct bpf_func_state *state = vstate->frame[frame];
unsigned long start, end;
- u32 nslots = state->allocated_stack / BPF_REG_SIZE;
+ u32 nslots = bpf_stack_nr_slots(state);
int spi;
start = (unsigned long)state->regs;
@@ -1637,6 +1637,11 @@ static bool reg_to_target(struct bpf_verifier_env *env, const struct bpf_reg_sta
return true;
}
+ /*
+ * Map the pointer back to its slot by address, which relies on
+ * the slots forming one contiguous array as bpf_stack_slot()
+ * indexes it.
+ */
start = (unsigned long)state->stack;
end = (unsigned long)(state->stack + nslots);
if (nslots && addr >= start && addr < end) {
diff --git a/kernel/bpf/liveness.c b/kernel/bpf/liveness.c
index a204b492acc8f..16fb7dc43726f 100644
--- a/kernel/bpf/liveness.c
+++ b/kernel/bpf/liveness.c
@@ -10,10 +10,39 @@
#define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)
-struct per_frame_masks {
- spis_t may_read; /* stack slots that may be read by this instruction */
- spis_t must_write; /* stack slots written by this instruction */
- spis_t live_before; /* stack slots that may be read by this insn and its successors */
+/*
+ * Stack liveness is tracked with a 4-byte (half register) granularity.
+ * Half-slot 0 covers [fp-4, fp), half-slot 1 covers [fp-8, fp-4), and so on,
+ * hence FRAME_HALF_SPIS - 1 is the deepest half-slot a frame can have.
+ */
+#define FRAME_HALF_SPIS (MAX_BPF_STACK_JIT / BPF_HALF_REG_SIZE)
+#define FRAME_MAX_WORDS BITS_TO_LONGS(FRAME_HALF_SPIS)
+
+/* Masks tracked for each instruction of a frame */
+enum {
+ FM_MAY_READ, /* stack slots that may be read by this instruction */
+ FM_MUST_WRITE, /* stack slots written by this instruction */
+ FM_LIVE_BEFORE, /* stack slots that may be read by this insn and its successors */
+ FM_MASK_CNT,
+};
+
+/*
+ * Per instruction stack masks for one frame of a function instance.
+ *
+ * Most frames use only a fraction of the stack budget, so instead of masks
+ * wide enough for every half-slot of the largest possible frame, all masks
+ * of one array share the width @words, and the marking functions widen the
+ * array as deeper half-slots are recorded. Mask @kind of the instruction at
+ * relative index @i is at &bits[(i * FM_MASK_CNT + kind) * words]. A
+ * half-slot at or past @words * BITS_PER_LONG is never read by this frame,
+ * hence never live. An instruction that may read the whole frame, such as a
+ * call passing a frame pointer to another subprog, widens the array to the
+ * program's stack budget, the deepest an accepted program can reach, so
+ * that the read cannot lose half-slots to a later widening.
+ */
+struct frame_masks {
+ u32 words;
+ unsigned long bits[];
};
/*
@@ -29,7 +58,7 @@ struct func_instance {
u32 subprog_start; /* cached env->subprog_info[subprog].start */
u32 insn_cnt; /* cached number of insns in the function */
/* Per frame, per instruction masks, frames allocated lazily. */
- struct per_frame_masks *frames[MAX_CALL_FRAMES];
+ struct frame_masks *frames[MAX_CALL_FRAMES];
bool must_write_initialized;
};
@@ -151,50 +180,123 @@ static int relative_idx(struct func_instance *instance, u32 insn_idx)
return insn_idx - instance->subprog_start;
}
-static struct per_frame_masks *get_frame_masks(struct func_instance *instance,
- u32 frame, u32 insn_idx)
+static u32 frame_mask_bits(struct frame_masks *fm)
{
- if (!instance->frames[frame])
- return NULL;
+ return fm->words * BITS_PER_LONG;
+}
- return &instance->frames[frame][relative_idx(instance, insn_idx)];
+static size_t frame_mask_words(struct func_instance *instance, u32 words)
+{
+ return (size_t)instance->insn_cnt * FM_MASK_CNT * words;
}
-static struct per_frame_masks *alloc_frame_masks(struct func_instance *instance,
- u32 frame, u32 insn_idx)
+/* Mask @kind of the instruction at relative index @rel */
+static unsigned long *rel_mask(struct frame_masks *fm, u32 rel, u32 kind)
{
- struct per_frame_masks *arr;
+ return fm->bits + ((size_t)rel * FM_MASK_CNT + kind) * fm->words;
+}
- if (!instance->frames[frame]) {
- arr = kvzalloc_objs(*arr, instance->insn_cnt,
- GFP_KERNEL_ACCOUNT);
- instance->frames[frame] = arr;
- if (!arr)
- return ERR_PTR(-ENOMEM);
+/*
+ * Make sure @frame has a mask array at least @words wide, allocating it or
+ * copying the existing masks into the wider stride as needed.
+ * @words must be in range [1, FRAME_MAX_WORDS].
+ */
+static struct frame_masks *widen_frame_masks(struct func_instance *instance,
+ u32 frame, u32 words)
+{
+ struct frame_masks *old = instance->frames[frame], *new;
+ u32 i, kind;
+
+ if (old && old->words >= words)
+ return old;
+ new = kvzalloc_flex(*new, bits, frame_mask_words(instance, words), GFP_KERNEL_ACCOUNT);
+ if (!new)
+ return NULL;
+ new->words = words;
+ if (old) {
+ for (i = 0; i < instance->insn_cnt; i++)
+ for (kind = 0; kind < FM_MASK_CNT; kind++)
+ memcpy(rel_mask(new, i, kind), rel_mask(old, i, kind),
+ old->words * sizeof(*old->bits));
+ kvfree(old);
}
- return get_frame_masks(instance, frame, insn_idx);
+ instance->frames[frame] = new;
+ return new;
}
-/* Accumulate may_read masks for @frame at @insn_idx */
-static int mark_stack_read(struct func_instance *instance, u32 frame, u32 insn_idx, spis_t mask)
+/*
+ * Set the inclusive half-slot range [lo, hi] in mask @kind of @frame at @insn_idx.
+ * An empty range, including one with a negative @hi as computed for a write
+ * that does not fully cover any half-slot, marks nothing.
+ */
+static int mark_stack_range(struct func_instance *instance, u32 frame, u32 insn_idx,
+ u32 kind, s32 lo, s32 hi)
{
- struct per_frame_masks *masks;
+ struct frame_masks *fm;
- masks = alloc_frame_masks(instance, frame, insn_idx);
- if (IS_ERR(masks))
- return PTR_ERR(masks);
- masks->may_read = spis_or(masks->may_read, mask);
+ /*
+ * An access past the frame bottom is rejected by the main verifier
+ * pass later, liveness only has to avoid running off the masks.
+ */
+ hi = min_t(s32, hi, FRAME_HALF_SPIS - 1);
+ if (lo > hi)
+ return 0;
+ fm = widen_frame_masks(instance, frame, BITS_TO_LONGS(hi + 1));
+ if (!fm)
+ return -ENOMEM;
+ bitmap_set(rel_mask(fm, relative_idx(instance, insn_idx), kind), lo, hi - lo + 1);
return 0;
}
-static int mark_stack_write(struct func_instance *instance, u32 frame, u32 insn_idx, spis_t mask)
+/* Accumulate may_read for half-slots [lo, hi] of @frame at @insn_idx */
+static int mark_stack_read(struct func_instance *instance, u32 frame, u32 insn_idx,
+ s32 lo, s32 hi)
+{
+ return mark_stack_range(instance, frame, insn_idx, FM_MAY_READ, lo, hi);
+}
+
+/* Accumulate must_write for half-slots [lo, hi] of @frame at @insn_idx */
+static int mark_stack_write(struct func_instance *instance, u32 frame, u32 insn_idx,
+ s32 lo, s32 hi)
+{
+ return mark_stack_range(instance, frame, insn_idx, FM_MUST_WRITE, lo, hi);
+}
+
+/*
+ * Mark every half-slot of @frame as possibly read by @insn_idx. This widens
+ * the masks to the program's stack budget: a full read recorded at a narrower
+ * width would leave the bits added by a later widening clear and lose part of
+ * it. No verifier state holds a slot past the budget, so no liveness query
+ * reaches beyond it; an access past the budget in unreachable code may still
+ * widen a frame's masks further, which is harmless.
+ */
+static int mark_stack_read_all(struct bpf_verifier_env *env, struct func_instance *instance,
+ u32 frame, u32 insn_idx)
{
- struct per_frame_masks *masks;
+ return mark_stack_read(instance, frame, insn_idx, 0,
+ env->stack_limit / BPF_HALF_REG_SIZE - 1);
+}
- masks = alloc_frame_masks(instance, frame, insn_idx);
- if (IS_ERR(masks))
- return PTR_ERR(masks);
- masks->must_write = spis_or(masks->must_write, mask);
+/* Accumulate @src, a mask @src_words wide, into may_read of @frame at @insn_idx */
+static int mark_stack_read_mask(struct func_instance *instance, u32 frame, u32 insn_idx,
+ const unsigned long *src, u32 src_words)
+{
+ u32 nbits = src_words * BITS_PER_LONG;
+ struct frame_masks *fm;
+ unsigned long *dst;
+ u32 last, w;
+
+ last = find_last_bit(src, nbits);
+ if (last == nbits)
+ return 0;
+ fm = widen_frame_masks(instance, frame, BITS_TO_LONGS(last + 1));
+ if (!fm)
+ return -ENOMEM;
+ dst = rel_mask(fm, relative_idx(instance, insn_idx), FM_MAY_READ);
+ /* @src has no bits set past @last, hence none past @fm->words either */
+ src_words = min(src_words, fm->words);
+ for (w = 0; w < src_words; w++)
+ dst[w] |= src[w];
return 0;
}
@@ -272,33 +374,42 @@ __diag_pop();
static inline bool update_insn(struct bpf_verifier_env *env,
struct func_instance *instance, u32 frame, u32 insn_idx)
{
- spis_t new_before, new_after;
- struct per_frame_masks *insn, *succ_insn;
+ unsigned long new_after[FRAME_MAX_WORDS] = {};
+ unsigned long *may_read, *must_write, *live_before;
+ struct frame_masks *fm = instance->frames[frame];
+ u32 rel = relative_idx(instance, insn_idx);
struct bpf_iarray *succ;
- u32 s;
- bool changed;
+ bool changed = false;
+ u32 s, w;
succ = bpf_insn_successors(env, insn_idx);
if (succ->cnt == 0)
return false;
- changed = false;
- insn = get_frame_masks(instance, frame, insn_idx);
- new_before = SPIS_ZERO;
- new_after = SPIS_ZERO;
+ /* All instructions of one frame array share the same mask width */
for (s = 0; s < succ->cnt; ++s) {
- succ_insn = get_frame_masks(instance, frame, succ->items[s]);
- new_after = spis_or(new_after, succ_insn->live_before);
+ unsigned long *succ_live;
+
+ succ_live = rel_mask(fm, relative_idx(instance, succ->items[s]), FM_LIVE_BEFORE);
+ for (w = 0; w < fm->words; w++)
+ new_after[w] |= succ_live[w];
}
+ may_read = rel_mask(fm, rel, FM_MAY_READ);
+ must_write = rel_mask(fm, rel, FM_MUST_WRITE);
+ live_before = rel_mask(fm, rel, FM_LIVE_BEFORE);
/*
* New "live_before" is a union of all "live_before" of successors
* minus slots written by instruction plus slots read by instruction.
* new_before = (new_after & ~insn->must_write) | insn->may_read
*/
- new_before = spis_or(spis_and(new_after, spis_not(insn->must_write)),
- insn->may_read);
- changed |= !spis_equal(new_before, insn->live_before);
- insn->live_before = new_before;
+ for (w = 0; w < fm->words; w++) {
+ unsigned long new_before = (new_after[w] & ~must_write[w]) | may_read[w];
+
+ if (new_before != live_before[w]) {
+ live_before[w] = new_before;
+ changed = true;
+ }
+ }
return changed;
}
@@ -329,10 +440,12 @@ static void update_instance(struct bpf_verifier_env *env, struct func_instance *
static bool is_live_before(struct func_instance *instance, u32 insn_idx, u32 frameno, u32 half_spi)
{
- struct per_frame_masks *masks;
+ struct frame_masks *fm = instance->frames[frameno];
- masks = get_frame_masks(instance, frameno, insn_idx);
- return masks && spis_test_bit(masks->live_before, half_spi);
+ /* No recorded access reaches past the masks, so nothing there is live */
+ if (!fm || half_spi >= frame_mask_bits(fm))
+ return false;
+ return test_bit(half_spi, rel_mask(fm, relative_idx(instance, insn_idx), FM_LIVE_BEFORE));
}
int bpf_live_stack_query_init(struct bpf_verifier_env *env, struct bpf_verifier_state *st)
@@ -447,17 +560,19 @@ static int spi_off(int spi)
* When only one half is set, print as "-4h","-8h",...
* Runs of 3+ consecutive fully-set SPIs are collapsed: "fp0-8..-24"
*/
-static char *fmt_spis_mask(struct bpf_verifier_env *env, int frame, bool first, spis_t spis)
+static char *fmt_spis_mask(struct bpf_verifier_env *env, int frame, bool first,
+ const unsigned long *spis, u32 words)
{
int buf_sz = sizeof(env->tmp_str_buf);
+ int spi_cnt = words * BITS_PER_LONG / 2;
char *buf = env->tmp_str_buf;
int spi, n, run_start;
buf[0] = '\0';
- for (spi = 0; spi < STACK_SLOTS / 2 && buf_sz > 0; spi++) {
- bool lo = spis_test_bit(spis, spi * 2);
- bool hi = spis_test_bit(spis, spi * 2 + 1);
+ for (spi = 0; spi < spi_cnt && buf_sz > 0; spi++) {
+ bool lo = test_bit(spi * 2, spis);
+ bool hi = test_bit(spi * 2 + 1, spis);
const char *space = first ? "" : " ";
if (!lo && !hi)
@@ -467,16 +582,16 @@ static char *fmt_spis_mask(struct bpf_verifier_env *env, int frame, bool first,
/* half-spi */
n = scnprintf(buf, buf_sz, "%sfp%d%d%s",
space, frame, spi_off(spi) + (lo ? STACK_SLOT_SZ : 0), "h");
- } else if (spi + 2 < STACK_SLOTS / 2 &&
- spis_test_bit(spis, spi * 2 + 2) &&
- spis_test_bit(spis, spi * 2 + 3) &&
- spis_test_bit(spis, spi * 2 + 4) &&
- spis_test_bit(spis, spi * 2 + 5)) {
+ } else if (spi + 2 < spi_cnt &&
+ test_bit(spi * 2 + 2, spis) &&
+ test_bit(spi * 2 + 3, spis) &&
+ test_bit(spi * 2 + 4, spis) &&
+ test_bit(spi * 2 + 5, spis)) {
/* 3+ consecutive full spis */
run_start = spi;
- while (spi + 1 < STACK_SLOTS / 2 &&
- spis_test_bit(spis, (spi + 1) * 2) &&
- spis_test_bit(spis, (spi + 1) * 2 + 1))
+ while (spi + 1 < spi_cnt &&
+ test_bit((spi + 1) * 2, spis) &&
+ test_bit((spi + 1) * 2 + 1, spis))
spi++;
n = scnprintf(buf, buf_sz, "%sfp%d%d..%d",
space, frame, spi_off(run_start), spi_off(spi));
@@ -495,7 +610,8 @@ static void print_instance(struct bpf_verifier_env *env, struct func_instance *i
{
int start = env->subprog_info[instance->subprog].start;
struct bpf_insn *insns = env->prog->insnsi;
- struct per_frame_masks *masks;
+ struct frame_masks *fm;
+ unsigned long *mask;
int len = instance->insn_cnt;
int insn_idx, frame, i;
bool has_use, has_def;
@@ -518,10 +634,13 @@ static void print_instance(struct bpf_verifier_env *env, struct func_instance *i
pos = env->log.end_pos;
verbose(env, " use: ");
for (frame = instance->depth; frame >= 0; --frame) {
- masks = get_frame_masks(instance, frame, insn_idx);
- if (!masks || spis_is_zero(masks->may_read))
+ fm = instance->frames[frame];
+ if (!fm)
+ continue;
+ mask = rel_mask(fm, i, FM_MAY_READ);
+ if (bitmap_empty(mask, frame_mask_bits(fm)))
continue;
- verbose(env, "%s", fmt_spis_mask(env, frame, !has_use, masks->may_read));
+ verbose(env, "%s", fmt_spis_mask(env, frame, !has_use, mask, fm->words));
has_use = true;
}
if (!has_use)
@@ -529,10 +648,13 @@ static void print_instance(struct bpf_verifier_env *env, struct func_instance *i
pos = env->log.end_pos;
verbose(env, " def: ");
for (frame = instance->depth; frame >= 0; --frame) {
- masks = get_frame_masks(instance, frame, insn_idx);
- if (!masks || spis_is_zero(masks->must_write))
+ fm = instance->frames[frame];
+ if (!fm)
continue;
- verbose(env, "%s", fmt_spis_mask(env, frame, !has_def, masks->must_write));
+ mask = rel_mask(fm, i, FM_MUST_WRITE);
+ if (bitmap_empty(mask, frame_mask_bits(fm)))
+ continue;
+ verbose(env, "%s", fmt_spis_mask(env, frame, !has_def, mask, fm->words));
has_def = true;
}
if (!has_def)
@@ -601,9 +723,9 @@ static int print_instances(struct bpf_verifier_env *env)
* - same frame + different offset -> offset-imprecise
* - different frames -> fully-imprecise (bitmask OR)
*
- * At memory access sites (LDX/STX/ST), offset-imprecise marks only
- * the known frame's access mask as SPIS_ALL, while fully-imprecise
- * iterates bits in the bitmask and routes each frame to its target.
+ * At memory access sites (LDX/STX/ST), offset-imprecise marks the known
+ * frame as fully read, while fully-imprecise iterates bits in the bitmask
+ * and routes each frame to its target.
*/
#define MAX_ARG_OFFSETS 4
@@ -622,8 +744,15 @@ enum arg_track_state {
ARG_IMPRECISE = -3, /* lost identity; .mask is arg bitmask */
};
-/* Track callee stack slots fp-8 through fp-512 (64 slots of 8 bytes each) */
-#define MAX_ARG_SPILL_SLOTS 64
+/*
+ * The tracker follows as many spill slots as a subprog addresses directly
+ * through R10, see subprog_spill_slots(). A frame's slots at a call site are
+ * kept in a snapshot for the callees that receive pointers into that frame.
+ */
+struct spill_snapshot {
+ u32 slots;
+ struct arg_track at[];
+};
/*
* Combined register + stack arg tracking: R0-R10 at indices 0-10,
@@ -898,13 +1027,60 @@ static void arg_padd(struct arg_track *at, s64 delta)
}
/*
- * Convert a byte offset from FP to a callee stack slot index.
+ * Slots the spill tracker may follow for a subprog of @len instructions
+ * without its per-instruction tables costing more than they could for the
+ * largest program while every frame stayed within MAX_BPF_STACK: as many
+ * entries as 64 slots need for BPF_COMPLEXITY_LIMIT_INSNS instructions.
+ */
+static u32 spill_slots_affordable(int len)
+{
+ u32 base = MAX_BPF_STACK / BPF_REG_SIZE;
+
+ return max_t(u32, base, base * BPF_COMPLEXITY_LIMIT_INSNS / len);
+}
+
+/*
+ * Number of 8-byte spill slots to track for the instructions in [@start, @end):
+ * the 64 slots of a MAX_BPF_STACK frame, which the tracker has always
+ * followed, or the deepest 8-byte stack access made directly through R10 when
+ * that reaches further, up to the program's stack budget. Spills and fills are
+ * compiled as direct R10 accesses, so the count covers them whatever the frame
+ * size; a fill through a derived pointer that reaches past it is treated as
+ * imprecise, as one past the frame always was.
+ */
+static u32 subprog_spill_slots(struct bpf_verifier_env *env, int start, int end)
+{
+ struct bpf_insn *insns = env->prog->insnsi;
+ int deepest = 0;
+
+ for (int i = start; i < end; i++) {
+ struct bpf_insn *insn = &insns[i];
+ u8 class = BPF_CLASS(insn->code);
+ u8 base;
+
+ if (BPF_SIZE(insn->code) != BPF_DW)
+ continue;
+ if (class == BPF_LDX)
+ base = insn->src_reg;
+ else if (class == BPF_STX || class == BPF_ST)
+ base = insn->dst_reg;
+ else
+ continue;
+ if (base == BPF_REG_FP && insn->off < 0)
+ deepest = max(deepest, -insn->off);
+ }
+ return clamp_t(u32, DIV_ROUND_UP(deepest, 8), MAX_BPF_STACK / BPF_REG_SIZE,
+ env->stack_limit / BPF_REG_SIZE);
+}
+
+/*
+ * Convert a byte offset from FP to one of the @nslots tracked stack slots.
* Returns -1 if out of range or not 8-byte aligned.
* Slot 0 = fp-8, slot 1 = fp-16, ..., slot 7 = fp-64, ....
*/
-static int fp_off_to_slot(s16 off)
+static int fp_off_to_slot(s16 off, u32 nslots)
{
- if (off >= 0 || off < -(int)(MAX_ARG_SPILL_SLOTS * 8))
+ if (off >= 0 || off < -(int)(nslots * 8))
return -1;
if (off % 8)
return -1;
@@ -913,7 +1089,7 @@ static int fp_off_to_slot(s16 off)
static struct arg_track fill_from_stack(struct bpf_insn *insn,
struct arg_track *at_out, int reg,
- struct arg_track *at_stack_out,
+ struct arg_track *at_stack_out, u32 nslots,
int depth)
{
struct arg_track imp = {
@@ -924,7 +1100,7 @@ static struct arg_track fill_from_stack(struct bpf_insn *insn,
int cnt, i;
if (reg == BPF_REG_FP) {
- int slot = fp_off_to_slot(insn->off);
+ int slot = fp_off_to_slot(insn->off, nslots);
return slot >= 0 ? at_stack_out[slot] : imp;
}
@@ -937,7 +1113,7 @@ static struct arg_track fill_from_stack(struct bpf_insn *insn,
if (arg_add(at_out[reg].off[i], insn->off, &fp_off))
return imp;
- slot = fp_off_to_slot(fp_off);
+ slot = fp_off_to_slot(fp_off, nslots);
if (slot < 0)
return imp;
result = __arg_track_join(result, at_stack_out[slot]);
@@ -952,7 +1128,7 @@ static struct arg_track fill_from_stack(struct bpf_insn *insn,
* When exact offset is unknown conservatively add reg values to all slots in at_stack_out.
*/
static void spill_to_stack(struct bpf_insn *insn, struct arg_track *at_out,
- int reg, struct arg_track *at_stack_out,
+ int reg, struct arg_track *at_stack_out, u32 nslots,
struct arg_track *val, u32 sz)
{
struct arg_track none = { .frame = ARG_NONE };
@@ -960,7 +1136,7 @@ static void spill_to_stack(struct bpf_insn *insn, struct arg_track *at_out,
int cnt, i;
if (reg == BPF_REG_FP) {
- int slot = fp_off_to_slot(insn->off);
+ int slot = fp_off_to_slot(insn->off, nslots);
if (slot >= 0)
at_stack_out[slot] = new_val;
@@ -968,7 +1144,7 @@ static void spill_to_stack(struct bpf_insn *insn, struct arg_track *at_out,
}
cnt = at_out[reg].off_cnt;
if (cnt == 0) {
- for (int slot = 0; slot < MAX_ARG_SPILL_SLOTS; slot++)
+ for (int slot = 0; slot < nslots; slot++)
at_stack_out[slot] = __arg_track_join(at_stack_out[slot], new_val);
return;
}
@@ -978,7 +1154,7 @@ static void spill_to_stack(struct bpf_insn *insn, struct arg_track *at_out,
if (arg_add(at_out[reg].off[i], insn->off, &fp_off))
continue;
- slot = fp_off_to_slot(fp_off);
+ slot = fp_off_to_slot(fp_off, nslots);
if (slot < 0)
continue;
if (cnt == 1)
@@ -992,16 +1168,17 @@ static void spill_to_stack(struct bpf_insn *insn, struct arg_track *at_out,
* Clear all tracked callee stack slots overlapping the byte range
* [off, off+sz-1] where off is a negative FP-relative offset.
*/
-static void clear_overlapping_stack_slots(struct arg_track *at_stack, s16 off, u32 sz, int cnt)
+static void clear_overlapping_stack_slots(struct arg_track *at_stack, u32 nslots, s16 off,
+ u32 sz, int cnt)
{
struct arg_track none = { .frame = ARG_NONE };
if (cnt == 0) {
- for (int i = 0; i < MAX_ARG_SPILL_SLOTS; i++)
+ for (int i = 0; i < nslots; i++)
at_stack[i] = __arg_track_join(at_stack[i], none);
return;
}
- for (int i = 0; i < MAX_ARG_SPILL_SLOTS; i++) {
+ for (int i = 0; i < nslots; i++) {
int slot_start = -((i + 1) * 8);
int slot_end = slot_start + 8;
@@ -1019,33 +1196,33 @@ static void clear_overlapping_stack_slots(struct arg_track *at_stack, s16 off, u
*/
static void clear_stack_for_all_offs(struct bpf_insn *insn,
struct arg_track *at_out, int reg,
- struct arg_track *at_stack_out, u32 sz)
+ struct arg_track *at_stack_out, u32 nslots, u32 sz)
{
int cnt, i;
if (reg == BPF_REG_FP) {
- clear_overlapping_stack_slots(at_stack_out, insn->off, sz, 1);
+ clear_overlapping_stack_slots(at_stack_out, nslots, insn->off, sz, 1);
return;
}
cnt = at_out[reg].off_cnt;
if (cnt == 0) {
- clear_overlapping_stack_slots(at_stack_out, 0, sz, cnt);
+ clear_overlapping_stack_slots(at_stack_out, nslots, 0, sz, cnt);
return;
}
for (i = 0; i < cnt; i++) {
s16 fp_off;
if (arg_add(at_out[reg].off[i], insn->off, &fp_off)) {
- clear_overlapping_stack_slots(at_stack_out, 0, sz, 0);
+ clear_overlapping_stack_slots(at_stack_out, nslots, 0, sz, 0);
break;
}
- clear_overlapping_stack_slots(at_stack_out, fp_off, sz, cnt);
+ clear_overlapping_stack_slots(at_stack_out, nslots, fp_off, sz, cnt);
}
}
static void arg_track_log(struct bpf_verifier_env *env, struct bpf_insn *insn, int idx,
struct arg_track *at_in, struct arg_track *at_stack_in,
- struct arg_track *at_out, struct arg_track *at_stack_out)
+ struct arg_track *at_out, struct arg_track *at_stack_out, u32 nslots)
{
bool printed = false;
int i;
@@ -1077,7 +1254,7 @@ static void arg_track_log(struct bpf_verifier_env *env, struct bpf_insn *insn, i
verbose(env, "\tsa%d: ", i); verbose_arg_track(env, &at_in[ai]);
verbose(env, " -> "); verbose_arg_track(env, &at_out[ai]);
}
- for (i = 0; i < MAX_ARG_SPILL_SLOTS; i++) {
+ for (i = 0; i < nslots; i++) {
if (arg_track_eq(&at_stack_out[i], &at_stack_in[i]))
continue;
if (!printed) {
@@ -1105,7 +1282,7 @@ static bool can_be_local_fp(int depth, int regno, struct arg_track *at)
*/
static void arg_track_xfer(struct bpf_verifier_env *env, struct bpf_insn *insn,
int insn_idx,
- struct arg_track *at_out, struct arg_track *at_stack_out,
+ struct arg_track *at_out, struct arg_track *at_stack_out, u32 nslots,
const struct arg_track *at_stack_arg_entry,
struct func_instance *instance,
u32 *callsites)
@@ -1189,14 +1366,15 @@ static void arg_track_xfer(struct bpf_verifier_env *env, struct bpf_insn *insn,
* ARG_IMPRECISE (off_cnt == 0).
*/
if (src_is_local_fp && BPF_MODE(insn->code) == BPF_MEM && sz == 8) {
- *dst = fill_from_stack(insn, at_out, insn->src_reg, at_stack_out, depth);
+ *dst = fill_from_stack(insn, at_out, insn->src_reg, at_stack_out, nslots,
+ depth);
} else if (src->frame >= 0 && src->frame < depth &&
BPF_MODE(insn->code) == BPF_MEM && sz == 8) {
- struct arg_track *parent_stack =
+ struct spill_snapshot *parent =
env->callsite_at_stack[callsites[src->frame]];
*dst = fill_from_stack(insn, at_out, insn->src_reg,
- parent_stack, src->frame);
+ parent->at, parent->slots, src->frame);
} else if (src->frame == ARG_IMPRECISE &&
!(src->mask & BIT(depth)) && src->mask &&
BPF_MODE(insn->code) == BPF_MEM && sz == 8) {
@@ -1218,12 +1396,12 @@ static void arg_track_xfer(struct bpf_verifier_env *env, struct bpf_insn *insn,
dst_is_local_fp = can_be_local_fp(depth, insn->dst_reg, dst);
if (dst_is_local_fp && BPF_MODE(insn->code) == BPF_MEM)
spill_to_stack(insn, at_out, insn->dst_reg,
- at_stack_out, src, sz);
+ at_stack_out, nslots, src, sz);
if (BPF_MODE(insn->code) == BPF_ATOMIC) {
if (dst_is_local_fp && insn->imm != BPF_LOAD_ACQ)
clear_stack_for_all_offs(insn, at_out, insn->dst_reg,
- at_stack_out, sz);
+ at_stack_out, nslots, sz);
r = bpf_atomic_load_reg(insn);
if (r >= 0)
@@ -1236,7 +1414,7 @@ static void arg_track_xfer(struct bpf_verifier_env *env, struct bpf_insn *insn,
/* BPF_ST to FP-derived dst: clear overlapping stack slots */
if (dst_is_local_fp)
clear_stack_for_all_offs(insn, at_out, insn->dst_reg,
- at_stack_out, sz);
+ at_stack_out, nslots, sz);
}
}
@@ -1252,7 +1430,6 @@ static int record_stack_access_off(struct func_instance *instance, s64 fp_off,
s64 access_bytes, u32 frame, u32 insn_idx)
{
s32 slot_hi, slot_lo;
- spis_t mask;
if (fp_off >= 0)
/*
@@ -1264,27 +1441,19 @@ static int record_stack_access_off(struct func_instance *instance, s64 fp_off,
if (access_bytes == S64_MIN) {
/* helper/kfunc read unknown amount of bytes from fp_off until fp+0 */
slot_hi = (-fp_off - 1) / STACK_SLOT_SZ;
- mask = SPIS_ZERO;
- spis_or_range(&mask, 0, slot_hi);
- return mark_stack_read(instance, frame, insn_idx, mask);
+ return mark_stack_read(instance, frame, insn_idx, 0, slot_hi);
}
if (access_bytes > 0) {
/* Mark any touched slot as use */
slot_hi = (-fp_off - 1) / STACK_SLOT_SZ;
slot_lo = max_t(s32, (-fp_off - access_bytes) / STACK_SLOT_SZ, 0);
- mask = SPIS_ZERO;
- spis_or_range(&mask, slot_lo, slot_hi);
- return mark_stack_read(instance, frame, insn_idx, mask);
+ return mark_stack_read(instance, frame, insn_idx, slot_lo, slot_hi);
} else if (access_bytes < 0) {
/* Mark only fully covered slots as def */
access_bytes = -access_bytes;
slot_hi = (-fp_off) / STACK_SLOT_SZ - 1;
slot_lo = max_t(s32, (-fp_off - access_bytes + STACK_SLOT_SZ - 1) / STACK_SLOT_SZ, 0);
- if (slot_lo <= slot_hi) {
- mask = SPIS_ZERO;
- spis_or_range(&mask, slot_lo, slot_hi);
- return mark_stack_write(instance, frame, insn_idx, mask);
- }
+ return mark_stack_write(instance, frame, insn_idx, slot_lo, slot_hi);
}
return 0;
}
@@ -1293,7 +1462,7 @@ static int record_stack_access_off(struct func_instance *instance, s64 fp_off,
* 'arg' is FP-derived argument to helper/kfunc or load/store that
* reads (positive) or writes (negative) 'access_bytes' into 'use' or 'def'.
*/
-static int record_stack_access(struct func_instance *instance,
+static int record_stack_access(struct bpf_verifier_env *env, struct func_instance *instance,
const struct arg_track *arg,
s64 access_bytes, u32 frame, u32 insn_idx)
{
@@ -1303,7 +1472,7 @@ static int record_stack_access(struct func_instance *instance,
return 0;
if (arg->off_cnt == 0) {
if (access_bytes > 0 || access_bytes == S64_MIN)
- return mark_stack_read(instance, frame, insn_idx, SPIS_ALL);
+ return mark_stack_read_all(env, instance, frame, insn_idx);
return 0;
}
if (access_bytes != S64_MIN && access_bytes < 0 && arg->off_cnt != 1)
@@ -1322,7 +1491,8 @@ static int record_stack_access(struct func_instance *instance,
* When a pointer is ARG_IMPRECISE, conservatively mark every frame in
* the bitmask as fully used.
*/
-static int record_imprecise(struct func_instance *instance, u32 mask, u32 insn_idx)
+static int record_imprecise(struct bpf_verifier_env *env, struct func_instance *instance,
+ u32 mask, u32 insn_idx)
{
int depth = instance->depth;
int f, err;
@@ -1331,7 +1501,7 @@ static int record_imprecise(struct func_instance *instance, u32 mask, u32 insn_i
if (!(mask & 1))
continue;
if (f <= depth) {
- err = mark_stack_read(instance, f, insn_idx, SPIS_ALL);
+ err = mark_stack_read_all(env, instance, f, insn_idx);
if (err)
return err;
}
@@ -1400,9 +1570,9 @@ static int record_load_store_access(struct bpf_verifier_env *env,
}
if (ptr->frame >= 0 && ptr->frame <= depth)
- return record_stack_access(instance, ptr, sz, ptr->frame, insn_idx);
+ return record_stack_access(env, instance, ptr, sz, ptr->frame, insn_idx);
if (ptr->frame == ARG_IMPRECISE)
- return record_imprecise(instance, ptr->mask, insn_idx);
+ return record_imprecise(env, instance, ptr->mask, insn_idx);
/* ARG_NONE: not derived from any frame pointer, skip */
return 0;
}
@@ -1427,7 +1597,7 @@ static int record_arg_access(struct bpf_verifier_env *env,
bytes = bpf_kfunc_stack_access_bytes(env, insn, arg_idx, insn_idx);
} else {
for (int f = 0; f <= depth; f++) {
- err = mark_stack_read(instance, f, insn_idx, SPIS_ALL);
+ err = mark_stack_read_all(env, instance, f, insn_idx);
if (err)
return err;
}
@@ -1437,9 +1607,9 @@ static int record_arg_access(struct bpf_verifier_env *env,
return 0;
if (frame >= 0 && frame <= depth)
- err = record_stack_access(instance, at, bytes, frame, insn_idx);
+ err = record_stack_access(env, instance, at, bytes, frame, insn_idx);
else if (frame == ARG_IMPRECISE)
- err = record_imprecise(instance, at->mask, insn_idx);
+ err = record_imprecise(env, instance, at->mask, insn_idx);
return err;
}
@@ -1542,7 +1712,7 @@ struct subprog_at_info {
static void print_subprog_arg_access(struct bpf_verifier_env *env,
int subprog,
struct subprog_at_info *info,
- struct arg_track (*at_stack_in)[MAX_ARG_SPILL_SLOTS])
+ struct arg_track *at_stack_in, u32 nslots)
{
struct bpf_insn *insns = env->prog->insnsi;
int start = env->subprog_info[subprog].start;
@@ -1575,8 +1745,8 @@ static void print_subprog_arg_access(struct bpf_verifier_env *env,
has_extra = true;
}
if (is_ldx_stx_call) {
- for (r = 0; r < MAX_ARG_SPILL_SLOTS; r++)
- if (arg_is_fp(&at_stack_in[i][r]))
+ for (r = 0; r < nslots; r++)
+ if (arg_is_fp(&at_stack_in[(size_t)i * nslots + r]))
has_extra = true;
}
@@ -1606,11 +1776,11 @@ static void print_subprog_arg_access(struct bpf_verifier_env *env,
}
if (is_ldx_stx_call) {
- for (r = 0; r < MAX_ARG_SPILL_SLOTS; r++) {
- if (!arg_is_fp(&at_stack_in[i][r]))
+ for (r = 0; r < nslots; r++) {
+ if (!arg_is_fp(&at_stack_in[(size_t)i * nslots + r]))
continue;
verbose(env, " fp%+d=", -(r + 1) * 8);
- verbose_arg_track(env, &at_stack_in[i][r]);
+ verbose_arg_track(env, &at_stack_in[(size_t)i * nslots + r]);
}
}
@@ -1643,9 +1813,10 @@ static int compute_subprog_args(struct bpf_verifier_env *env,
int end = env->subprog_info[subprog + 1].start;
int po_end = env->subprog_info[subprog + 1].postorder_start;
int len = end - start;
+ u32 nslots = min(subprog_spill_slots(env, start, end), spill_slots_affordable(len));
struct arg_track (*at_in)[MAX_AT_TRACK_REGS] = NULL;
struct arg_track at_out[MAX_AT_TRACK_REGS];
- struct arg_track (*at_stack_in)[MAX_ARG_SPILL_SLOTS] = NULL;
+ struct arg_track *at_stack_in = NULL;
struct arg_track *at_stack_out = NULL;
struct arg_track at_stack_arg_entry[MAX_STACK_ARG_SLOTS];
struct arg_track unvisited = { .frame = ARG_UNVISITED };
@@ -1657,19 +1828,19 @@ static int compute_subprog_args(struct bpf_verifier_env *env,
if (!at_in)
goto err_free;
- at_stack_in = kvmalloc_objs(*at_stack_in, len, GFP_KERNEL_ACCOUNT);
+ at_stack_in = kvmalloc_objs(*at_stack_in, (size_t)len * nslots, GFP_KERNEL_ACCOUNT);
if (!at_stack_in)
goto err_free;
- at_stack_out = kvmalloc_objs(*at_stack_out, MAX_ARG_SPILL_SLOTS, GFP_KERNEL_ACCOUNT);
+ at_stack_out = kvmalloc_objs(*at_stack_out, nslots, GFP_KERNEL_ACCOUNT);
if (!at_stack_out)
goto err_free;
for (i = 0; i < len; i++) {
for (r = 0; r < MAX_AT_TRACK_REGS; r++)
at_in[i][r] = unvisited;
- for (r = 0; r < MAX_ARG_SPILL_SLOTS; r++)
- at_stack_in[i][r] = unvisited;
+ for (r = 0; r < nslots; r++)
+ at_stack_in[(size_t)i * nslots + r] = unvisited;
}
for (r = 0; r < MAX_AT_TRACK_REGS; r++)
@@ -1685,8 +1856,8 @@ static int compute_subprog_args(struct bpf_verifier_env *env,
}
/* Entry: all stack slots are ARG_NONE */
- for (r = 0; r < MAX_ARG_SPILL_SLOTS; r++)
- at_stack_in[0][r] = none;
+ for (r = 0; r < nslots; r++)
+ at_stack_in[r] = none;
/* Entry: incoming stack args from caller, or ARG_NONE for main */
for (r = 0; r < MAX_STACK_ARG_SLOTS; r++)
@@ -1708,11 +1879,12 @@ static int compute_subprog_args(struct bpf_verifier_env *env,
continue;
memcpy(at_out, at_in[i], sizeof(at_out));
- memcpy(at_stack_out, at_stack_in[i], MAX_ARG_SPILL_SLOTS * sizeof(*at_stack_out));
+ memcpy(at_stack_out, &at_stack_in[(size_t)i * nslots], nslots * sizeof(*at_stack_out));
- arg_track_xfer(env, insn, idx, at_out, at_stack_out,
+ arg_track_xfer(env, insn, idx, at_out, at_stack_out, nslots,
at_stack_arg_entry, instance, callsites);
- arg_track_log(env, insn, idx, at_in[i], at_stack_in[i], at_out, at_stack_out);
+ arg_track_log(env, insn, idx, at_in[i], &at_stack_in[(size_t)i * nslots], at_out,
+ at_stack_out, nslots);
/* Propagate to successors within this subprogram */
succ = bpf_insn_successors(env, idx);
@@ -1729,9 +1901,10 @@ static int compute_subprog_args(struct bpf_verifier_env *env,
changed |= arg_track_join(env, idx, target, r,
&at_in[ti][r], at_out[r]);
- for (r = 0; r < MAX_ARG_SPILL_SLOTS; r++)
+ for (r = 0; r < nslots; r++)
changed |= arg_track_join(env, idx, target, -r - 1,
- &at_stack_in[ti][r], at_stack_out[r]);
+ &at_stack_in[(size_t)ti * nslots + r],
+ at_stack_out[r]);
}
}
if (changed)
@@ -1754,23 +1927,24 @@ static int compute_subprog_args(struct bpf_verifier_env *env,
}
if (bpf_pseudo_call(insn) || bpf_calls_callback(env, idx)) {
+ struct spill_snapshot *snap;
+
kvfree(env->callsite_at_stack[idx]);
- env->callsite_at_stack[idx] =
- kvmalloc_objs(*env->callsite_at_stack[idx],
- MAX_ARG_SPILL_SLOTS, GFP_KERNEL_ACCOUNT);
- if (!env->callsite_at_stack[idx]) {
+ env->callsite_at_stack[idx] = kvmalloc_flex(*snap, at, nslots, GFP_KERNEL_ACCOUNT);
+ snap = env->callsite_at_stack[idx];
+ if (!snap) {
err = -ENOMEM;
goto err_free;
}
- memcpy(env->callsite_at_stack[idx],
- at_stack_in[i], sizeof(struct arg_track) * MAX_ARG_SPILL_SLOTS);
+ snap->slots = nslots;
+ memcpy(snap->at, &at_stack_in[(size_t)i * nslots], nslots * sizeof(*snap->at));
}
}
info->at_in = at_in;
at_in = NULL;
info->len = len;
- print_subprog_arg_access(env, subprog, info, at_stack_in);
+ print_subprog_arg_access(env, subprog, info, at_stack_in, nslots);
err = 0;
err_free:
@@ -1797,36 +1971,52 @@ static bool has_fp_args(struct arg_track *args)
* may_read: union (any pass might read the slot).
* must_write: intersection (only slots written on ALL passes are guaranteed).
* live_before is recomputed by a subsequent update_instance() on @dst.
+ *
+ * The two instances may have settled on different mask widths for the same
+ * frame, so @dst is widened to cover @src first. A word only @dst has counts
+ * as zero on the @src side: it unions into may_read as a no-op and intersects
+ * must_write to empty.
*/
-static void merge_instances(struct func_instance *dst, struct func_instance *src)
+static int merge_instances(struct func_instance *dst, struct func_instance *src)
{
- int f, i;
+ struct frame_masks *d, *s;
+ u32 f, i, w;
for (f = 0; f <= dst->depth; f++) {
- if (!src->frames[f]) {
+ s = src->frames[f];
+ d = dst->frames[f];
+ if (!s) {
/* This pass didn't touch frame f — must_write intersects with empty. */
- if (dst->frames[f])
+ if (d)
for (i = 0; i < dst->insn_cnt; i++)
- dst->frames[f][i].must_write = SPIS_ZERO;
+ bitmap_zero(rel_mask(d, i, FM_MUST_WRITE),
+ frame_mask_bits(d));
continue;
}
- if (!dst->frames[f]) {
+ if (!d) {
/* Previous pass didn't touch frame f — take src, zero must_write. */
- dst->frames[f] = src->frames[f];
+ dst->frames[f] = s;
src->frames[f] = NULL;
for (i = 0; i < dst->insn_cnt; i++)
- dst->frames[f][i].must_write = SPIS_ZERO;
+ bitmap_zero(rel_mask(s, i, FM_MUST_WRITE), frame_mask_bits(s));
continue;
}
+ d = widen_frame_masks(dst, f, s->words);
+ if (!d)
+ return -ENOMEM;
for (i = 0; i < dst->insn_cnt; i++) {
- dst->frames[f][i].may_read =
- spis_or(dst->frames[f][i].may_read,
- src->frames[f][i].may_read);
- dst->frames[f][i].must_write =
- spis_and(dst->frames[f][i].must_write,
- src->frames[f][i].must_write);
+ unsigned long *dst_read = rel_mask(d, i, FM_MAY_READ);
+ unsigned long *dst_write = rel_mask(d, i, FM_MUST_WRITE);
+ unsigned long *src_read = rel_mask(s, i, FM_MAY_READ);
+ unsigned long *src_write = rel_mask(s, i, FM_MUST_WRITE);
+
+ for (w = 0; w < d->words; w++) {
+ dst_read[w] |= w < s->words ? src_read[w] : 0;
+ dst_write[w] &= w < s->words ? src_write[w] : 0;
+ }
}
}
+ return 0;
}
static struct func_instance *fresh_instance(struct func_instance *src)
@@ -1941,7 +2131,7 @@ static int analyze_subprog(struct bpf_verifier_env *env,
if (info[subprog].at_in[j][caller_reg].frame == ARG_NONE)
continue;
for (int f = 0; f <= depth; f++) {
- err = mark_stack_read(instance, f, idx, SPIS_ALL);
+ err = mark_stack_read_all(env, instance, f, idx);
if (err)
goto out_free;
}
@@ -1980,13 +2170,18 @@ static int analyze_subprog(struct bpf_verifier_env *env,
/* Pull callee's entry liveness back to caller's callsite */
{
u32 callee_start = callee_instance->subprog_start;
- struct per_frame_masks *entry;
+ struct frame_masks *callee_fm;
for (int f = 0; f < callee_instance->depth; f++) {
- entry = get_frame_masks(callee_instance, f, callee_start);
- if (!entry)
+ callee_fm = callee_instance->frames[f];
+ if (!callee_fm)
continue;
- err = mark_stack_read(instance, f, idx, entry->live_before);
+ err = mark_stack_read_mask(instance, f, idx,
+ rel_mask(callee_fm,
+ relative_idx(callee_instance,
+ callee_start),
+ FM_LIVE_BEFORE),
+ callee_fm->words);
if (err)
goto out_free;
}
@@ -1994,9 +2189,11 @@ static int analyze_subprog(struct bpf_verifier_env *env,
}
if (prev_instance) {
- merge_instances(prev_instance, instance);
+ err = merge_instances(prev_instance, instance);
free_instance(instance);
instance = prev_instance;
+ if (err)
+ return err;
}
update_instance(env, instance);
return 0;
diff --git a/kernel/bpf/log.c b/kernel/bpf/log.c
index fb032dfdc0dee..d850a7863d2ed 100644
--- a/kernel/bpf/log.c
+++ b/kernel/bpf/log.c
@@ -716,7 +716,8 @@ void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_verifie
verbose(env, "=");
print_reg_state(env, state, reg);
}
- for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
+ for (i = 0; i < bpf_stack_nr_slots(state); i++) {
+ struct bpf_stack_state *slot = bpf_stack_slot(state, i);
char types_buf[BPF_REG_SIZE + 1];
const char *sep = "";
bool valid = false;
@@ -727,7 +728,7 @@ void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_verifie
continue;
for (j = 0; j < BPF_REG_SIZE; j++) {
- slot_type = state->stack[i].slot_type[j];
+ slot_type = slot->slot_type[j];
if (slot_type != STACK_INVALID && slot_type != STACK_POISON)
valid = true;
types_buf[j] = slot_type_char[slot_type];
@@ -736,12 +737,12 @@ void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_verifie
if (!valid)
continue;
- reg = &state->stack[i].spilled_ptr;
- switch (state->stack[i].slot_type[BPF_REG_SIZE - 1]) {
+ reg = &slot->spilled_ptr;
+ switch (slot->slot_type[BPF_REG_SIZE - 1]) {
case STACK_SPILL:
/* print MISC/ZERO/INVALID slots above subreg spill */
for (j = 0; j < BPF_REG_SIZE; j++)
- if (state->stack[i].slot_type[j] == STACK_SPILL)
+ if (slot->slot_type[j] == STACK_SPILL)
break;
types_buf[j] = '\0';
@@ -751,7 +752,7 @@ void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_verifie
case STACK_DYNPTR:
/* skip to main dynptr slot */
i += BPF_DYNPTR_NR_SLOTS - 1;
- reg = &state->stack[i].spilled_ptr;
+ reg = &bpf_stack_slot(state, i)->spilled_ptr;
verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE);
verbose(env, "=dynptr_%s(", dynptr_type_str(reg->dynptr.type));
diff --git a/kernel/bpf/states.c b/kernel/bpf/states.c
index 66fb11b6c6a76..a07af8f11a5a2 100644
--- a/kernel/bpf/states.c
+++ b/kernel/bpf/states.c
@@ -335,21 +335,18 @@ static bool check_ids(u32 old_id, u32 cur_id, struct bpf_idmap *idmap)
return false;
}
- /* Reached the end of known mappings; haven't seen this id before */
- if (idmap->cnt < BPF_ID_MAP_SIZE) {
- map[idmap->cnt].old = old_id;
- map[idmap->cnt].cur = cur_id;
- idmap->cnt++;
- return true;
- }
-
/*
- * idmap slots are bounded by the number of registers and stack slots.
- * Since referenced dynptrs acquire intermediate references that do
- * not live in either, so the map can be exhausted. Since it is unlikely,
- * fail the verification by treating the states as not equivalent.
+ * Reached the end of known mappings; haven't seen this id before. If
+ * the map cannot grow, treat the states as not equivalent, which only
+ * costs pruning.
*/
- return false;
+ if (!bpf_id_scratch_reserve((void **)&idmap->map, &idmap->cap, idmap->cnt, sizeof(*map)))
+ return false;
+ map = idmap->map;
+ map[idmap->cnt].old = old_id;
+ map[idmap->cnt].cur = cur_id;
+ idmap->cnt++;
+ return true;
}
/*
@@ -415,14 +412,14 @@ static void __clean_func_state(struct bpf_verifier_env *env,
* half_spi 2*i → lower half: slot_type[0..3] (closer to FP)
* half_spi 2*i+1 → upper half: slot_type[4..7] (farther from FP)
*/
- for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) {
+ for (i = 0; i < bpf_stack_nr_slots(st); i++) {
bool lo_live = bpf_stack_slot_alive(env, frame, i * 2);
bool hi_live = bpf_stack_slot_alive(env, frame, i * 2 + 1);
if (!hi_live || !lo_live) {
int start = !lo_live ? 0 : BPF_REG_SIZE / 2;
int end = !hi_live ? BPF_REG_SIZE : BPF_REG_SIZE / 2;
- u8 stype = st->stack[i].slot_type[7];
+ u8 stype = bpf_stack_slot(st, i)->slot_type[7];
/*
* Don't clear special slots.
@@ -442,7 +439,7 @@ static void __clean_func_state(struct bpf_verifier_env *env,
* rejecting as non-scalar register fills.
*/
if (!hi_live) {
- struct bpf_reg_state *spill = &st->stack[i].spilled_ptr;
+ struct bpf_reg_state *spill = &bpf_stack_slot(st, i)->spilled_ptr;
if (lo_live && stype == STACK_SPILL) {
if (spill->type != SCALAR_VALUE)
@@ -454,7 +451,7 @@ static void __clean_func_state(struct bpf_verifier_env *env,
if (bpf_register_is_null(spill))
continue;
for (j = 0; j < 4; j++) {
- u8 *t = &st->stack[i].slot_type[j];
+ u8 *t = &bpf_stack_slot(st, i)->slot_type[j];
if (*t == STACK_SPILL)
*t = STACK_MISC;
@@ -463,7 +460,7 @@ static void __clean_func_state(struct bpf_verifier_env *env,
bpf_mark_reg_not_init(env, spill);
}
for (j = start; j < end; j++)
- st->stack[i].slot_type[j] = STACK_POISON;
+ bpf_stack_slot(st, i)->slot_type[j] = STACK_POISON;
}
}
}
@@ -707,37 +704,38 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,
* didn't use them
*/
for (i = 0; i < old->allocated_stack; i++) {
+ struct bpf_stack_state *old_slot, *cur_slot;
struct bpf_reg_state *old_reg, *cur_reg;
int im = i % BPF_REG_SIZE;
+ u8 old_type;
spi = i / BPF_REG_SIZE;
+ old_slot = bpf_stack_slot(old, spi);
+ old_type = old_slot->slot_type[im];
+ cur_slot = i < cur->allocated_stack ? bpf_stack_slot(cur, spi) : NULL;
if (exact == EXACT) {
- u8 old_type = old->stack[spi].slot_type[i % BPF_REG_SIZE];
- u8 cur_type = i < cur->allocated_stack ?
- cur->stack[spi].slot_type[i % BPF_REG_SIZE] : STACK_INVALID;
+ u8 cur_type = cur_slot ? cur_slot->slot_type[im] : STACK_INVALID;
/* STACK_INVALID and STACK_POISON are equivalent for pruning */
if (old_type == STACK_POISON)
old_type = STACK_INVALID;
if (cur_type == STACK_POISON)
cur_type = STACK_INVALID;
- if (i >= cur->allocated_stack || old_type != cur_type)
+ if (!cur_slot || old_type != cur_type)
return false;
}
- if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID ||
- old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_POISON)
+ if (old_type == STACK_INVALID || old_type == STACK_POISON)
continue;
- if (env->allow_uninit_stack &&
- old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC)
+ if (env->allow_uninit_stack && old_type == STACK_MISC)
continue;
/* explored stack has more populated slots than current stack
* and these slots were used
*/
- if (i >= cur->allocated_stack)
+ if (!cur_slot)
return false;
/*
@@ -747,8 +745,8 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,
* regsafe() to ensure scalar ids are compared.
*/
if (im == 0 || im == 4) {
- old_reg = scalar_reg_for_stack(env, &old->stack[spi], im);
- cur_reg = scalar_reg_for_stack(env, &cur->stack[spi], im);
+ old_reg = scalar_reg_for_stack(env, old_slot, im);
+ cur_reg = scalar_reg_for_stack(env, cur_slot, im);
if (old_reg && cur_reg) {
if (!regsafe(env, old_reg, cur_reg, idmap, exact))
return false;
@@ -761,21 +759,19 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,
* it will be safe with zero-initialized stack.
* The opposite is not true
*/
- if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC &&
- cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO)
+ if (old_type == STACK_MISC && cur_slot->slot_type[im] == STACK_ZERO)
continue;
- if (old->stack[spi].slot_type[i % BPF_REG_SIZE] !=
- cur->stack[spi].slot_type[i % BPF_REG_SIZE])
+ if (old_type != cur_slot->slot_type[im])
/* Ex: old explored (safe) state has STACK_SPILL in
* this stack slot, but current has STACK_MISC ->
* this verifier states are not equivalent,
* return false to continue verification of this path
*/
return false;
- if (i % BPF_REG_SIZE != BPF_REG_SIZE - 1)
+ if (im != BPF_REG_SIZE - 1)
continue;
/* Both old and cur are having same slot_type */
- switch (old->stack[spi].slot_type[BPF_REG_SIZE - 1]) {
+ switch (old_type) {
case STACK_SPILL:
/* when explored and current stack slot are both storing
* spilled registers, check that stored pointers types
@@ -787,13 +783,13 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,
* such verifier states are not equivalent.
* return false to continue verification of this path
*/
- if (!regsafe(env, &old->stack[spi].spilled_ptr,
- &cur->stack[spi].spilled_ptr, idmap, exact))
+ if (!regsafe(env, &old_slot->spilled_ptr, &cur_slot->spilled_ptr,
+ idmap, exact))
return false;
break;
case STACK_DYNPTR:
- old_reg = &old->stack[spi].spilled_ptr;
- cur_reg = &cur->stack[spi].spilled_ptr;
+ old_reg = &old_slot->spilled_ptr;
+ cur_reg = &cur_slot->spilled_ptr;
if (old_reg->dynptr.type != cur_reg->dynptr.type ||
old_reg->dynptr.first_slot != cur_reg->dynptr.first_slot ||
!check_ids(old_reg->id, cur_reg->id, idmap) ||
@@ -801,8 +797,8 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,
return false;
break;
case STACK_ITER:
- old_reg = &old->stack[spi].spilled_ptr;
- cur_reg = &cur->stack[spi].spilled_ptr;
+ old_reg = &old_slot->spilled_ptr;
+ cur_reg = &cur_slot->spilled_ptr;
/* iter.depth is not compared between states as it
* doesn't matter for correctness and would otherwise
* prevent convergence; we maintain it only to prevent
@@ -818,8 +814,8 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,
return false;
break;
case STACK_IRQ_FLAG:
- old_reg = &old->stack[spi].spilled_ptr;
- cur_reg = &cur->stack[spi].spilled_ptr;
+ old_reg = &old_slot->spilled_ptr;
+ cur_reg = &cur_slot->spilled_ptr;
if (!check_ids(old_reg->id, cur_reg->id, idmap) ||
old_reg->irq.kfunc_class != cur_reg->irq.kfunc_class)
return false;
@@ -966,6 +962,27 @@ static bool func_states_equal(struct bpf_verifier_env *env, struct bpf_func_stat
return true;
}
+/*
+ * Make room for one more entry in an id scratch array, doubling it as needed.
+ * Returns false if it could not grow; callers then treat the id as unknown
+ * or the states as different, which is always safe.
+ */
+bool bpf_id_scratch_reserve(void **arr, u32 *cap, u32 cnt, size_t elem_size)
+{
+ u32 new_cap;
+ void *p;
+
+ if (cnt < *cap)
+ return true;
+ new_cap = *cap ? *cap * 2 : 64;
+ p = krealloc_array(*arr, new_cap, elem_size, GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+ if (!p)
+ return false;
+ *arr = p;
+ *cap = new_cap;
+ return true;
+}
+
static void reset_idmap_scratch(struct bpf_verifier_env *env)
{
struct bpf_idmap *idmap = &env->idmap_scratch;
@@ -1043,10 +1060,12 @@ static int propagate_precision(struct bpf_verifier_env *env,
first = false;
}
- for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
- if (!bpf_is_spilled_reg(&state->stack[i]))
+ for (i = 0; i < bpf_stack_nr_slots(state); i++) {
+ struct bpf_stack_state *ss = bpf_stack_slot(state, i);
+
+ if (!bpf_is_spilled_reg(ss))
continue;
- state_reg = &state->stack[i].spilled_ptr;
+ state_reg = &ss->spilled_ptr;
if (state_reg->type != SCALAR_VALUE ||
!state_reg->precise)
continue;
@@ -1192,15 +1211,17 @@ static bool iter_active_depths_differ(struct bpf_verifier_state *old, struct bpf
for (fr = old->curframe; fr >= 0; fr--) {
state = old->frame[fr];
- for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
- if (state->stack[i].slot_type[0] != STACK_ITER)
+ for (i = 0; i < bpf_stack_nr_slots(state); i++) {
+ struct bpf_stack_state *ss = bpf_stack_slot(state, i);
+
+ if (ss->slot_type[0] != STACK_ITER)
continue;
- slot = &state->stack[i].spilled_ptr;
+ slot = &ss->spilled_ptr;
if (slot->iter.state != BPF_ITER_STATE_ACTIVE)
continue;
- cur_slot = &cur->frame[fr]->stack[i].spilled_ptr;
+ cur_slot = &bpf_stack_slot(cur->frame[fr], i)->spilled_ptr;
if (cur_slot->iter.depth != slot->iter.depth)
return true;
}
@@ -1222,10 +1243,12 @@ static void mark_all_scalars_imprecise(struct bpf_verifier_env *env, struct bpf_
continue;
reg->precise = false;
}
- for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) {
- if (!bpf_is_spilled_reg(&func->stack[j]))
+ for (j = 0; j < bpf_stack_nr_slots(func); j++) {
+ struct bpf_stack_state *ss = bpf_stack_slot(func, j);
+
+ if (!bpf_is_spilled_reg(ss))
continue;
- reg = &func->stack[j].spilled_ptr;
+ reg = &ss->spilled_ptr;
if (reg->type != SCALAR_VALUE)
continue;
reg->precise = false;
@@ -1328,7 +1351,7 @@ int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx)
*/
if (is_iter_next_insn(env, insn_idx)) {
if (states_equal(env, &sl->state, cur, RANGE_WITHIN)) {
- struct bpf_func_state *cur_frame;
+ struct bpf_func_state *cur_frame, *iter_frame;
struct bpf_reg_state *iter_state, *iter_reg;
int spi;
@@ -1342,7 +1365,8 @@ int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx)
* no need for extra (re-)validations
*/
spi = bpf_get_spi(iter_reg->var_off.value);
- iter_state = &bpf_func(env, iter_reg)->stack[spi].spilled_ptr;
+ iter_frame = bpf_func(env, iter_reg);
+ iter_state = &bpf_stack_slot(iter_frame, spi)->spilled_ptr;
if (iter_state->iter.state == BPF_ITER_STATE_ACTIVE) {
loop = true;
goto hit;
@@ -1410,7 +1434,7 @@ int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx)
*/
err = 0;
if (bpf_is_jmp_point(env, env->insn_idx))
- err = bpf_push_jmp_history(env, cur, 0, 0, 0, 0);
+ err = bpf_push_jmp_history(env, cur, 0, 0, 0, NULL, 0);
err = err ? : propagate_precision(env, &sl->state, cur, NULL);
if (err)
return err;
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index fec5a1ae6a4da..2c495f66dc341 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -598,16 +598,17 @@ bool bpf_is_may_goto_insn(struct bpf_insn *insn)
static bool is_spi_bounds_valid(struct bpf_func_state *state, int spi, int nr_slots)
{
- int allocated_slots = state->allocated_stack / BPF_REG_SIZE;
+ int allocated_slots = bpf_stack_nr_slots(state);
- /* We need to check that slots between [spi - nr_slots + 1, spi] are
- * within [0, allocated_stack).
- *
- * Please note that the spi grows downwards. For example, a dynptr
- * takes the size of two stack slots; the first slot will be at
- * spi and the second slot will be at spi - 1.
- */
- return spi - nr_slots + 1 >= 0 && spi < allocated_slots;
+ /*
+ * We need to check that slots between [spi - nr_slots + 1, spi] are
+ * within [0, allocated_stack).
+ *
+ * Please note that the spi grows downwards. For example, a dynptr
+ * takes the size of two stack slots; the first slot will be at
+ * spi and the second slot will be at spi - 1.
+ */
+ return spi - nr_slots + 1 >= 0 && spi < allocated_slots;
}
static int stack_slot_obj_get_spi(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
@@ -751,8 +752,8 @@ static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_
return err;
for (i = 0; i < BPF_REG_SIZE; i++) {
- state->stack[spi].slot_type[i] = STACK_DYNPTR;
- state->stack[spi - 1].slot_type[i] = STACK_DYNPTR;
+ bpf_stack_slot(state, spi)->slot_type[i] = STACK_DYNPTR;
+ bpf_stack_slot(state, spi - 1)->slot_type[i] = STACK_DYNPTR;
}
type = arg_to_dynptr_type(arg_type);
@@ -785,8 +786,8 @@ static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_
parent_id = dynptr->parent_id;
}
- mark_dynptr_stack_regs(env, &state->stack[spi].spilled_ptr,
- &state->stack[spi - 1].spilled_ptr, type, parent_id);
+ mark_dynptr_stack_regs(env, &bpf_stack_slot(state, spi)->spilled_ptr,
+ &bpf_stack_slot(state, spi - 1)->spilled_ptr, type, parent_id);
return 0;
}
@@ -818,7 +819,7 @@ static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_re
* all clones and derived slices. For non-referenced dynptr, only
* the dynptr and slices derived from it will be invalidated.
*/
- reg = &state->stack[spi].spilled_ptr;
+ reg = &bpf_stack_slot(state, spi)->spilled_ptr;
return release_reference(env, dynptr_type_referenced(reg->dynptr.type)
? reg->parent_id
: reg->id);
@@ -857,6 +858,7 @@ static int dynptr_ref_cnt(struct bpf_verifier_env *env, int v_parent_id)
static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env,
struct bpf_func_state *state, int spi)
{
+ struct bpf_stack_state *slot = bpf_stack_slot(state, spi);
int err = 0;
/* We always ensure that STACK_DYNPTR is never set partially,
@@ -864,20 +866,22 @@ static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env,
* different for STACK_SPILL, where it may be only set for
* 1 byte, so code has to use is_spilled_reg.
*/
- if (state->stack[spi].slot_type[0] != STACK_DYNPTR)
+ if (slot->slot_type[0] != STACK_DYNPTR)
return 0;
/* Reposition spi to first slot */
- if (!state->stack[spi].spilled_ptr.dynptr.first_slot)
+ if (!slot->spilled_ptr.dynptr.first_slot) {
spi = spi + 1;
+ slot = bpf_stack_slot(state, spi);
+ }
/*
* A referenced dynptr can be overwritten only if there is at
* least one other dynptr sharing the same virtual ref parent,
* ensuring the reference can still be properly released.
*/
- if (dynptr_type_referenced(state->stack[spi].spilled_ptr.dynptr.type) &&
- dynptr_ref_cnt(env, state->stack[spi].spilled_ptr.parent_id) <= 1) {
+ if (dynptr_type_referenced(slot->spilled_ptr.dynptr.type) &&
+ dynptr_ref_cnt(env, slot->spilled_ptr.parent_id) <= 1) {
verbose(env, "cannot overwrite referenced dynptr\n");
bpf_diag_res(
env, env->insn_idx, "referenced dynptr overwrite",
@@ -887,7 +891,7 @@ static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env,
}
/* Invalidate the dynptr and any derived slices */
- err = release_reference(env, state->stack[spi].spilled_ptr.id);
+ err = release_reference(env, slot->spilled_ptr.id);
if (!err) {
mark_stack_slot_scratched(env, spi);
mark_stack_slot_scratched(env, spi - 1);
@@ -927,6 +931,7 @@ static bool is_dynptr_reg_valid_uninit(struct bpf_verifier_env *env, struct bpf_
static bool is_dynptr_reg_valid_init(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
{
struct bpf_func_state *state = bpf_func(env, reg);
+ struct bpf_stack_state *slot;
int i, spi;
/* This already represents first slot of initialized bpf_dynptr.
@@ -941,12 +946,13 @@ static bool is_dynptr_reg_valid_init(struct bpf_verifier_env *env, struct bpf_re
spi = dynptr_get_spi(env, reg);
if (spi < 0)
return false;
- if (!state->stack[spi].spilled_ptr.dynptr.first_slot)
+ slot = bpf_stack_slot(state, spi);
+ if (!slot->spilled_ptr.dynptr.first_slot)
return false;
for (i = 0; i < BPF_REG_SIZE; i++) {
- if (state->stack[spi].slot_type[i] != STACK_DYNPTR ||
- state->stack[spi - 1].slot_type[i] != STACK_DYNPTR)
+ if (slot->slot_type[i] != STACK_DYNPTR ||
+ bpf_stack_slot(state, spi - 1)->slot_type[i] != STACK_DYNPTR)
return false;
}
@@ -965,7 +971,7 @@ static enum bpf_dynptr_type dynptr_reg_type(struct bpf_verifier_env *env, struct
if (spi < 0)
return BPF_DYNPTR_TYPE_INVALID;
state = bpf_func(env, reg);
- return state->stack[spi].spilled_ptr.dynptr.type;
+ return bpf_stack_slot(state, spi)->spilled_ptr.dynptr.type;
}
static bool is_dynptr_type_expected(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
@@ -1005,7 +1011,7 @@ static int mark_stack_slots_iter(struct bpf_verifier_env *env,
return id;
for (i = 0; i < nr_slots; i++) {
- struct bpf_stack_state *slot = &state->stack[spi - i];
+ struct bpf_stack_state *slot = bpf_stack_slot(state, spi - i);
struct bpf_reg_state *st = &slot->spilled_ptr;
__mark_reg_known_zero(st);
@@ -1035,18 +1041,22 @@ static int unmark_stack_slots_iter(struct bpf_verifier_env *env,
struct bpf_reg_state *reg, int nr_slots)
{
struct bpf_func_state *state = bpf_func(env, reg);
- int spi, i, j;
+ int spi, i, j, err;
spi = iter_get_spi(env, reg, nr_slots);
if (spi < 0)
return spi;
for (i = 0; i < nr_slots; i++) {
- struct bpf_stack_state *slot = &state->stack[spi - i];
+ struct bpf_stack_state *slot = bpf_stack_slot(state, spi - i);
struct bpf_reg_state *st = &slot->spilled_ptr;
- if (i == 0)
- WARN_ON_ONCE(release_reference(env, st->id));
+ if (i == 0) {
+ err = release_reference(env, st->id);
+ if (err == -ENOMEM)
+ return err;
+ WARN_ON_ONCE(err);
+ }
bpf_mark_reg_not_init(env, st);
@@ -1076,7 +1086,7 @@ static bool is_iter_reg_valid_uninit(struct bpf_verifier_env *env,
return false;
for (i = 0; i < nr_slots; i++) {
- struct bpf_stack_state *slot = &state->stack[spi - i];
+ struct bpf_stack_state *slot = bpf_stack_slot(state, spi - i);
for (j = 0; j < BPF_REG_SIZE; j++)
if (slot->slot_type[j] == STACK_ITER)
@@ -1097,7 +1107,7 @@ static int is_iter_reg_valid_init(struct bpf_verifier_env *env, struct bpf_reg_s
return -EINVAL;
for (i = 0; i < nr_slots; i++) {
- struct bpf_stack_state *slot = &state->stack[spi - i];
+ struct bpf_stack_state *slot = bpf_stack_slot(state, spi - i);
struct bpf_reg_state *st = &slot->spilled_ptr;
if (st->type & PTR_UNTRUSTED)
@@ -1139,7 +1149,7 @@ static int mark_stack_slot_irq_flag(struct bpf_verifier_env *env,
if (id < 0)
return id;
- slot = &state->stack[spi];
+ slot = bpf_stack_slot(state, spi);
st = &slot->spilled_ptr;
__mark_reg_known_zero(st);
@@ -1166,7 +1176,7 @@ static int unmark_stack_slot_irq_flag(struct bpf_verifier_env *env, struct bpf_r
if (spi < 0)
return spi;
- slot = &state->stack[spi];
+ slot = bpf_stack_slot(state, spi);
st = &slot->spilled_ptr;
if (st->irq.kfunc_class != kfunc_class) {
@@ -1235,7 +1245,7 @@ static bool is_irq_flag_reg_valid_uninit(struct bpf_verifier_env *env, struct bp
if (spi < 0)
return false;
- slot = &state->stack[spi];
+ slot = bpf_stack_slot(state, spi);
for (i = 0; i < BPF_REG_SIZE; i++)
if (slot->slot_type[i] == STACK_IRQ_FLAG)
@@ -1254,7 +1264,7 @@ static int is_irq_flag_reg_valid_init(struct bpf_verifier_env *env, struct bpf_r
if (spi < 0)
return -EINVAL;
- slot = &state->stack[spi];
+ slot = bpf_stack_slot(state, spi);
st = &slot->spilled_ptr;
if (!st->id)
@@ -1401,7 +1411,7 @@ static int copy_reference_state(struct bpf_verifier_state *dst, const struct bpf
static int copy_stack_state(struct bpf_func_state *dst, const struct bpf_func_state *src)
{
- size_t n = src->allocated_stack / BPF_REG_SIZE;
+ size_t n = bpf_stack_nr_slots(src);
dst->stack = copy_array(dst->stack, src->stack, n, sizeof(struct bpf_stack_state),
GFP_KERNEL_ACCOUNT);
@@ -1440,7 +1450,7 @@ static int resize_reference_state(struct bpf_verifier_state *state, size_t n)
*/
static int grow_stack_state(struct bpf_verifier_env *env, struct bpf_func_state *state, int size)
{
- size_t old_n = state->allocated_stack / BPF_REG_SIZE, n;
+ size_t old_n = bpf_stack_nr_slots(state), n;
/* The stack size is always a multiple of BPF_REG_SIZE. */
size = round_up(size, BPF_REG_SIZE);
@@ -3368,26 +3378,25 @@ static void mark_non_stack_access(struct bpf_verifier_env *env, int idx)
env->insn_aux_data[idx].non_stack_access = true;
}
+/* Layout of one packed linked register in the jump history, see linked_regs_pack() */
#define LR_FRAMENO_BITS 4
-#define LR_SPI_BITS 6
-#define LR_ENTRY_BITS (LR_SPI_BITS + LR_FRAMENO_BITS + 1)
-#define LR_SIZE_BITS 4
-#define LR_FRAMENO_MASK ((1ull << LR_FRAMENO_BITS) - 1)
-#define LR_SPI_MASK ((1ull << LR_SPI_BITS) - 1)
-#define LR_SIZE_MASK ((1ull << LR_SIZE_BITS) - 1)
-#define LR_SPI_OFF LR_FRAMENO_BITS
-#define LR_IS_REG_OFF (LR_SPI_BITS + LR_FRAMENO_BITS)
-#define LINKED_REGS_MAX 5
+#define LR_INDEX_BITS 11
+#define LR_FRAMENO_MASK ((1u << LR_FRAMENO_BITS) - 1)
+#define LR_IS_REG BIT(LR_FRAMENO_BITS)
+#define LR_INDEX_OFF (LR_FRAMENO_BITS + 1)
+#define LR_INDEX_MASK ((1u << LR_INDEX_BITS) - 1)
+#define LINKED_REGS_MAX BPF_LINKED_REGS_MAX
static_assert(MAX_CALL_FRAMES <= (1 << LR_FRAMENO_BITS));
-static_assert(LINKED_REGS_MAX < (1 << LR_SIZE_BITS));
-static_assert(LINKED_REGS_MAX * LR_ENTRY_BITS + LR_SIZE_BITS <= 64);
+static_assert(MAX_BPF_REG <= (1 << LR_INDEX_BITS));
+static_assert(MAX_BPF_STACK_SLOTS <= (1 << LR_INDEX_BITS));
+static_assert(LR_INDEX_OFF + LR_INDEX_BITS <= 16);
struct linked_reg {
u8 frameno;
union {
- u8 spi;
- u8 regno;
+ u16 spi;
+ u16 regno;
};
bool is_reg;
};
@@ -3406,48 +3415,34 @@ static struct linked_reg *linked_regs_push(struct linked_regs *s)
}
/*
- * Use u64 as a vector of 5 11-bit values, use first 4-bits to track
- * number of elements currently in stack.
- * Pack one history entry for linked registers as 11 bits in the following format:
- * - 4-bits frameno
- * - 6-bits spi_or_reg
- * - 1-bit is_reg
+ * Pack linked registers for a jump history entry, one u16 each:
+ * - 4 bits frameno
+ * - 1 bit is_reg
+ * - 11 bits register or stack slot index
*/
-static u64 linked_regs_pack(struct linked_regs *s)
+static void linked_regs_pack(const struct linked_regs *s, u16 *packed)
{
- u64 val = 0;
int i;
for (i = 0; i < s->cnt; ++i) {
- struct linked_reg *e = &s->entries[i];
- u64 tmp = 0;
+ const struct linked_reg *e = &s->entries[i];
- tmp |= e->frameno;
- tmp |= e->spi << LR_SPI_OFF;
- tmp |= (e->is_reg ? 1 : 0) << LR_IS_REG_OFF;
-
- val <<= LR_ENTRY_BITS;
- val |= tmp;
+ packed[i] = e->frameno | (e->is_reg ? LR_IS_REG : 0) | (e->spi << LR_INDEX_OFF);
}
- val <<= LR_SIZE_BITS;
- val |= s->cnt;
- return val;
}
-static void linked_regs_unpack(u64 val, struct linked_regs *s)
+static void linked_regs_unpack(const struct bpf_jmp_history_entry *hist, struct linked_regs *s)
{
int i;
- s->cnt = val & LR_SIZE_MASK;
- val >>= LR_SIZE_BITS;
-
+ s->cnt = hist->linked_regs_cnt;
for (i = 0; i < s->cnt; ++i) {
struct linked_reg *e = &s->entries[i];
+ u16 packed = hist->linked_regs[i];
- e->frameno = val & LR_FRAMENO_MASK;
- e->spi = (val >> LR_SPI_OFF) & LR_SPI_MASK;
- e->is_reg = (val >> LR_IS_REG_OFF) & 0x1;
- val >>= LR_ENTRY_BITS;
+ e->frameno = packed & LR_FRAMENO_MASK;
+ e->is_reg = packed & LR_IS_REG;
+ e->spi = (packed >> LR_INDEX_OFF) & LR_INDEX_MASK;
}
}
@@ -3489,10 +3484,10 @@ void bpf_bt_sync_linked_regs(struct backtrack_state *bt, struct bpf_jmp_history_
bool some_precise = false;
int i;
- if (!hist || hist->linked_regs == 0)
+ if (!hist || !hist->linked_regs_cnt)
return;
- linked_regs_unpack(hist->linked_regs, &linked_regs);
+ linked_regs_unpack(hist, &linked_regs);
for (i = 0; i < linked_regs.cnt; ++i) {
struct linked_reg *e = &linked_regs.entries[i];
@@ -3589,17 +3584,18 @@ static void save_register_state(struct bpf_verifier_env *env,
int spi, struct bpf_reg_state *reg,
int size)
{
+ struct bpf_stack_state *slot = bpf_stack_slot(state, spi);
int i;
- bpf_diag_mod_begin(env, &state->stack[spi].spilled_ptr, reg, BPF_DIAG_MOD_SPILL);
- state->stack[spi].spilled_ptr = *reg;
+ bpf_diag_mod_begin(env, &slot->spilled_ptr, reg, BPF_DIAG_MOD_SPILL);
+ slot->spilled_ptr = *reg;
for (i = BPF_REG_SIZE; i > BPF_REG_SIZE - size; i--)
- state->stack[spi].slot_type[i - 1] = STACK_SPILL;
+ slot->slot_type[i - 1] = STACK_SPILL;
/* size < 8 bytes spill */
for (; i; i--)
- mark_stack_slot_misc(env, &state->stack[spi].slot_type[i - 1]);
+ mark_stack_slot_misc(env, &slot->slot_type[i - 1]);
bpf_diag_mod_end(env);
}
@@ -3641,14 +3637,15 @@ static void check_fastcall_stack_contract(struct bpf_verifier_env *env,
static void scrub_special_slot(struct bpf_func_state *state, int spi)
{
+ struct bpf_stack_state *slot = bpf_stack_slot(state, spi);
int i;
/* regular write of data into stack destroys any spilled ptr */
- state->stack[spi].spilled_ptr.type = NOT_INIT;
+ slot->spilled_ptr.type = NOT_INIT;
/* Mark slots as STACK_MISC if they belonged to spilled ptr/dynptr/iter. */
- if (is_stack_slot_special(&state->stack[spi]))
+ if (is_stack_slot_special(slot))
for (i = 0; i < BPF_REG_SIZE; i++)
- scrub_spilled_slot(&state->stack[spi].slot_type[i]);
+ scrub_spilled_slot(&slot->slot_type[i]);
}
/* check_stack_{read,write}_fixed_off functions track spill/fill of registers,
@@ -3666,13 +3663,15 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env,
struct bpf_reg_state *reg = NULL;
int insn_flags = INSN_F_STACK_ACCESS;
int hist_spi = spi, hist_frame = state->frameno;
+ struct bpf_stack_state *ss = bpf_stack_slot(state, spi);
- /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
+ /*
+ * caller checked that off % size == 0 and -env->stack_limit <= off < 0,
* so it's aligned access and [off, off + size) are within stack limits
*/
if (!env->allow_ptr_leaks &&
- bpf_is_spilled_reg(&state->stack[spi]) &&
- !bpf_is_spilled_scalar_reg(&state->stack[spi]) &&
+ bpf_is_spilled_reg(ss) &&
+ !bpf_is_spilled_scalar_reg(ss) &&
size != BPF_REG_SIZE) {
const char *reason;
@@ -3694,7 +3693,7 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env,
bool sanitize = reg && is_pointer_regtype(reg->type);
for (i = 0; i < size; i++) {
- u8 type = state->stack[spi].slot_type[(slot - i) %
+ u8 type = ss->slot_type[(slot - i) %
BPF_REG_SIZE];
if (type != STACK_MISC && type != STACK_ZERO) {
@@ -3723,7 +3722,7 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env,
save_register_state(env, state, spi, reg, size);
/* Break the relation on a narrowing spill. */
if (!reg_value_fits)
- state->stack[spi].spilled_ptr.id = 0;
+ ss->spilled_ptr.id = 0;
} else if (!reg && !(off % BPF_REG_SIZE) && is_bpf_st_mem(insn) &&
env->bpf_capable) {
struct bpf_reg_state *tmp_reg = &env->fake_reg[0];
@@ -3747,8 +3746,8 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env,
} else {
u8 type = STACK_MISC;
- if (bpf_is_spilled_reg(&state->stack[spi]))
- bpf_diag_record_scrub(env, &state->stack[spi].spilled_ptr,
+ if (bpf_is_spilled_reg(ss))
+ bpf_diag_record_scrub(env, &ss->spilled_ptr,
BPF_DIAG_MOD_WRITE);
scrub_special_slot(state, spi);
@@ -3769,13 +3768,13 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env,
/* Mark slots affected by this stack write. */
for (i = 0; i < size; i++)
- state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = type;
+ ss->slot_type[(slot - i) % BPF_REG_SIZE] = type;
insn_flags = 0; /* not a register spill */
}
if (insn_flags)
return bpf_push_jmp_history(env, env->cur_state, insn_flags,
- hist_spi, hist_frame, 0);
+ hist_spi, hist_frame, NULL, 0);
return 0;
}
@@ -3835,12 +3834,14 @@ static int check_stack_write_var_off(struct bpf_verifier_env *env,
check_fastcall_stack_contract(env, state, insn_idx, min_off);
/* Variable offset writes destroy any spilled pointers in range. */
for (i = min_off; i < max_off; i++) {
+ struct bpf_stack_state *ss;
u8 new_type, *stype;
int slot, spi;
slot = -i - 1;
spi = slot / BPF_REG_SIZE;
- stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE];
+ ss = bpf_stack_slot(state, spi);
+ stype = &ss->slot_type[slot % BPF_REG_SIZE];
mark_stack_slot_scratched(env, spi);
if (!env->allow_ptr_leaks && *stype != STACK_MISC && *stype != STACK_ZERO) {
@@ -3863,9 +3864,8 @@ static int check_stack_write_var_off(struct bpf_verifier_env *env,
/* If writing_zero and the spi slot contains a spill of value 0,
* maintain the spill type.
*/
- if (writing_zero && *stype == STACK_SPILL &&
- bpf_is_spilled_scalar_reg(&state->stack[spi])) {
- struct bpf_reg_state *spill_reg = &state->stack[spi].spilled_ptr;
+ if (writing_zero && *stype == STACK_SPILL && bpf_is_spilled_scalar_reg(ss)) {
+ struct bpf_reg_state *spill_reg = &ss->spilled_ptr;
if (tnum_is_const(spill_reg->var_off) && spill_reg->var_off.value == 0) {
zero_used = true;
@@ -3936,23 +3936,22 @@ static int mark_reg_stack_read(struct bpf_verifier_env *env,
{
struct bpf_verifier_state *vstate = env->cur_state;
struct bpf_func_state *state = vstate->frame[vstate->curframe];
- u64 zero_spill_mask = 0;
int i, slot, spi;
u8 *stype;
- int zeros = 0;
+ int zeros = 0, zero_spills = 0;
for (i = min_off; i < max_off; i++) {
slot = -i - 1;
spi = slot / BPF_REG_SIZE;
mark_stack_slot_scratched(env, spi);
- stype = ptr_state->stack[spi].slot_type;
+ stype = bpf_stack_slot(ptr_state, spi)->slot_type;
if (stype[slot % BPF_REG_SIZE] == STACK_ZERO) {
zeros++;
continue;
}
if (stype[slot % BPF_REG_SIZE] == STACK_SPILL &&
- bpf_register_is_null(&ptr_state->stack[spi].spilled_ptr)) {
- zero_spill_mask |= 1ull << spi;
+ bpf_register_is_null(&bpf_stack_slot(ptr_state, spi)->spilled_ptr)) {
+ zero_spills++;
zeros++;
continue;
}
@@ -3963,8 +3962,14 @@ static int mark_reg_stack_read(struct bpf_verifier_env *env,
* so the whole register == const_zero.
*/
__mark_reg_const_zero(env, &state->regs[dst_regno]);
- if (zero_spill_mask) {
- bpf_bt_set_frame_slot_mask(&env->bt, ptr_state->frameno, zero_spill_mask);
+ if (zero_spills) {
+ for (i = min_off; i < max_off; i++) {
+ slot = -i - 1;
+ spi = slot / BPF_REG_SIZE;
+ stype = bpf_stack_slot(ptr_state, spi)->slot_type;
+ if (stype[slot % BPF_REG_SIZE] == STACK_SPILL)
+ bpf_bt_set_frame_slot(&env->bt, ptr_state->frameno, spi);
+ }
return mark_chain_precision_batch(env, env->cur_state);
}
} else {
@@ -4012,9 +4017,10 @@ static int check_stack_read_fixed_off(struct bpf_verifier_env *env,
int err;
int insn_flags = INSN_F_STACK_ACCESS;
int hist_spi = spi, hist_frame = reg_state->frameno;
+ struct bpf_stack_state *ss = bpf_stack_slot(reg_state, spi);
- stype = reg_state->stack[spi].slot_type;
- reg = ®_state->stack[spi].spilled_ptr;
+ stype = ss->slot_type;
+ reg = &ss->spilled_ptr;
mark_stack_slot_scratched(env, spi);
check_fastcall_stack_contract(env, state, env->insn_idx, off);
@@ -4025,7 +4031,7 @@ static int check_stack_read_fixed_off(struct bpf_verifier_env *env,
if (dst_regno >= 0)
bpf_diag_mod_begin(env, &state->regs[dst_regno], reg, BPF_DIAG_MOD_WRITE);
- if (bpf_is_spilled_reg(®_state->stack[spi])) {
+ if (bpf_is_spilled_reg(ss)) {
u8 spill_size = 1;
for (i = BPF_REG_SIZE - 1; i > 0 && stype[i - 1] == STACK_SPILL; i--)
@@ -4151,7 +4157,7 @@ static int check_stack_read_fixed_off(struct bpf_verifier_env *env,
}
if (insn_flags)
return bpf_push_jmp_history(env, env->cur_state, insn_flags,
- hist_spi, hist_frame, 0);
+ hist_spi, hist_frame, NULL, 0);
return 0;
}
@@ -4341,7 +4347,7 @@ static int check_stack_arg_write(struct bpf_verifier_env *env, struct bpf_func_s
bpf_diag_mod_end(env);
state->no_stack_arg_load = true;
return bpf_push_jmp_history(env, env->cur_state,
- INSN_F_STACK_ARG_ACCESS, spi, 0, 0);
+ INSN_F_STACK_ARG_ACCESS, spi, 0, NULL, 0);
}
/*
@@ -4375,7 +4381,7 @@ static int check_stack_arg_read(struct bpf_verifier_env *env, struct bpf_func_st
cur->regs[dst_regno] = *arg;
bpf_diag_mod_end(env);
return bpf_push_jmp_history(env, env->cur_state,
- INSN_F_STACK_ARG_ACCESS, spi, 0, 0);
+ INSN_F_STACK_ARG_ACCESS, spi, 0, NULL, 0);
}
static int mark_stack_arg_precision(struct bpf_verifier_env *env, int arg_idx)
@@ -5519,7 +5525,7 @@ static int check_max_stack_depth_subprog(struct bpf_verifier_env *env, int idx,
if (subprog[idx].priv_stack_mode == PRIV_STACK_ADAPTIVE) {
if (subprog_depth > env->max_stack_depth)
env->max_stack_depth = subprog_depth;
- if (subprog_depth > MAX_BPF_STACK) {
+ if (subprog_depth > env->stack_limit) {
verbose(env, "stack size of subprog %d is %d. Too large\n",
idx, subprog_depth);
return -EACCES;
@@ -5528,7 +5534,7 @@ static int check_max_stack_depth_subprog(struct bpf_verifier_env *env, int idx,
depth += subprog_depth;
if (depth > env->max_stack_depth)
env->max_stack_depth = depth;
- if (depth > MAX_BPF_STACK) {
+ if (depth > env->stack_limit) {
total = 0;
for (tmp = idx; tmp >= 0; tmp = dinfo[tmp].caller)
total++;
@@ -6529,10 +6535,11 @@ static int check_ptr_to_map_access(struct bpf_verifier_env *env,
return 0;
}
-/* Check that the stack access at the given offset is within bounds. The
+/*
+ * Check that the stack access at the given offset is within bounds. The
* maximum valid offset is -1.
*
- * The minimum valid offset is -MAX_BPF_STACK for writes, and
+ * The minimum valid offset is -env->stack_limit for writes, and
* -state->allocated_stack for reads.
*/
static int check_stack_slot_within_bounds(struct bpf_verifier_env *env,
@@ -6543,7 +6550,7 @@ static int check_stack_slot_within_bounds(struct bpf_verifier_env *env,
int min_valid_off;
if (t == BPF_WRITE || env->allow_uninit_stack)
- min_valid_off = -MAX_BPF_STACK;
+ min_valid_off = -(int)env->stack_limit;
else
min_valid_off = -state->allocated_stack;
@@ -7409,6 +7416,7 @@ static int check_stack_range_initialized(
}
for (i = min_off; i < max_off + access_size; i++) {
+ struct bpf_stack_state *ss;
u8 *stype;
slot = -i - 1;
@@ -7418,7 +7426,8 @@ static int check_stack_range_initialized(
return -EFAULT;
}
- stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE];
+ ss = bpf_stack_slot(state, spi);
+ stype = &ss->slot_type[slot % BPF_REG_SIZE];
if (*stype == STACK_MISC)
goto mark;
if ((*stype == STACK_ZERO) ||
@@ -7430,13 +7439,13 @@ static int check_stack_range_initialized(
goto mark;
}
- if (bpf_is_spilled_reg(&state->stack[spi]) &&
- (state->stack[spi].spilled_ptr.type == SCALAR_VALUE ||
+ if (bpf_is_spilled_reg(ss) &&
+ (ss->spilled_ptr.type == SCALAR_VALUE ||
env->allow_ptr_leaks)) {
if (clobber) {
- __mark_reg_unknown(env, &state->stack[spi].spilled_ptr);
+ __mark_reg_unknown(env, &ss->spilled_ptr);
for (j = 0; j < BPF_REG_SIZE; j++)
- scrub_spilled_slot(&state->stack[spi].slot_type[j]);
+ scrub_spilled_slot(&ss->slot_type[j]);
}
goto mark;
}
@@ -8166,7 +8175,7 @@ static int process_dynptr_func(struct bpf_verifier_env *env, struct bpf_reg_stat
mark_stack_slots_scratched(env, spi, BPF_DYNPTR_NR_SLOTS);
- reg = &state->stack[spi].spilled_ptr;
+ reg = &bpf_stack_slot(state, spi)->spilled_ptr;
}
meta->dynptr.type = reg->dynptr.type;
@@ -8299,7 +8308,7 @@ static int process_iter_arg(struct bpf_verifier_env *env, struct bpf_reg_state *
/* remember meta->iter info for process_iter_next_call() */
meta->iter.spi = spi;
meta->iter.frameno = reg->frameno;
- update_ref_obj(&meta->ref_obj, &state->stack[spi].spilled_ptr);
+ update_ref_obj(&meta->ref_obj, &bpf_stack_slot(state, spi)->spilled_ptr);
if (is_iter_destroy_kfunc(meta)) {
err = unmark_stack_slots_iter(env, reg, nr_slots);
@@ -8376,16 +8385,15 @@ static int widen_imprecise_scalars(struct bpf_verifier_env *env,
&fold->regs[i],
&fcur->regs[i]);
- num_slots = min(fold->allocated_stack / BPF_REG_SIZE,
- fcur->allocated_stack / BPF_REG_SIZE);
+ num_slots = min(bpf_stack_nr_slots(fold), bpf_stack_nr_slots(fcur));
for (i = 0; i < num_slots; i++) {
- if (!bpf_is_spilled_reg(&fold->stack[i]) ||
- !bpf_is_spilled_reg(&fcur->stack[i]))
+ struct bpf_stack_state *old_ss = bpf_stack_slot(fold, i);
+ struct bpf_stack_state *cur_ss = bpf_stack_slot(fcur, i);
+
+ if (!bpf_is_spilled_reg(old_ss) || !bpf_is_spilled_reg(cur_ss))
continue;
- maybe_widen_reg(env,
- &fold->stack[i].spilled_ptr,
- &fcur->stack[i].spilled_ptr);
+ maybe_widen_reg(env, &old_ss->spilled_ptr, &cur_ss->spilled_ptr);
}
}
return 0;
@@ -8397,7 +8405,7 @@ static struct bpf_reg_state *get_iter_from_state(struct bpf_verifier_state *cur_
int iter_frameno = meta->iter.frameno;
int iter_spi = meta->iter.spi;
- return &cur_st->frame[iter_frameno]->stack[iter_spi].spilled_ptr;
+ return &bpf_stack_slot(cur_st->frame[iter_frameno], iter_spi)->spilled_ptr;
}
/* process_iter_next_call() is called when verifier gets to iterator's next
@@ -9189,6 +9197,7 @@ static int get_constant_map_key(struct bpf_verifier_env *env,
s64 *value)
{
struct bpf_func_state *state = bpf_func(env, key);
+ struct bpf_stack_state *ss;
struct bpf_reg_state *reg;
int slot, spi, off;
int spill_size = 0;
@@ -9208,7 +9217,8 @@ static int get_constant_map_key(struct bpf_verifier_env *env,
slot = -stack_off - 1;
spi = slot / BPF_REG_SIZE;
off = slot % BPF_REG_SIZE;
- stype = state->stack[spi].slot_type;
+ ss = bpf_stack_slot(state, spi);
+ stype = ss->slot_type;
/* First handle precisely tracked STACK_ZERO */
for (i = off; i >= 0 && stype[i] == STACK_ZERO; i--)
@@ -9219,14 +9229,14 @@ static int get_constant_map_key(struct bpf_verifier_env *env,
}
/* Check that stack contains a scalar spill of expected size */
- if (!bpf_is_spilled_scalar_reg(&state->stack[spi]))
+ if (!bpf_is_spilled_scalar_reg(ss))
return -EOPNOTSUPP;
for (i = off; i >= 0 && stype[i] == STACK_SPILL; i--)
spill_size++;
if (spill_size != key_size)
return -EOPNOTSUPP;
- reg = &state->stack[spi].spilled_ptr;
+ reg = &ss->spilled_ptr;
if (!tnum_is_const(reg->var_off))
/* Stack value not statically known */
return -EOPNOTSUPP;
@@ -10502,8 +10512,9 @@ static int idstack_push(struct bpf_idmap *idmap, u32 id)
if (idmap->map[i].old == id)
return 0;
- if (WARN_ON_ONCE(idmap->cnt >= BPF_ID_MAP_SIZE))
- return -EFAULT;
+ if (!bpf_id_scratch_reserve((void **)&idmap->map, &idmap->cap, idmap->cnt,
+ sizeof(*idmap->map)))
+ return -ENOMEM;
idmap->map[idmap->cnt++].old = id;
return 0;
@@ -14348,11 +14359,11 @@ s64 bpf_helper_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn
}
/*
* Size arg is const on each path but differs across merged
- * paths. MAX_BPF_STACK is a safe upper bound for reads.
+ * paths. Reads may extend anywhere up to the frame top.
*/
if (full_write)
return 0;
- return MAX_BPF_STACK;
+ return S64_MIN;
}
return S64_MIN;
case ARG_PTR_TO_DYNPTR:
@@ -14438,7 +14449,8 @@ s64 bpf_kfunc_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn *
size = (s64)aux->const_reg_vals[size_reg];
goto out;
}
- return MAX_BPF_STACK;
+ /* Unknown size: the read may extend anywhere up to the frame top. */
+ return S64_MIN;
}
/* fixed-size pointed-to type: resolve via BTF */
@@ -15180,7 +15192,8 @@ enum {
REASON_STACK = -5,
};
-static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
+static int retrieve_ptr_limit(const struct bpf_verifier_env *env,
+ const struct bpf_reg_state *ptr_reg,
u32 *alu_limit, bool mask_to_left)
{
u32 max = 0, ptr_limit = 0;
@@ -15192,7 +15205,7 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
* offset where we would need to deal with min/max bounds is
* currently prohibited for unprivileged.
*/
- max = MAX_BPF_STACK + mask_to_left;
+ max = env->stack_limit + mask_to_left;
ptr_limit = -ptr_reg->var_off.value;
break;
case PTR_TO_MAP_VALUE:
@@ -15312,7 +15325,7 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env,
(opcode == BPF_SUB && !off_is_neg);
}
- err = retrieve_ptr_limit(ptr_reg, &alu_limit, info->mask_to_left);
+ err = retrieve_ptr_limit(env, ptr_reg, &alu_limit, info->mask_to_left);
if (err < 0)
return err;
@@ -15442,7 +15455,7 @@ static int check_stack_access_for_ptr_arithmetic(
return -EACCES;
}
- if (off >= 0 || off < -MAX_BPF_STACK) {
+ if (off >= 0 || off < -(int)env->stack_limit) {
verbose(env, "R%d stack pointer arithmetic goes out of range, "
"prohibited for !root; off=%d\n", regno, off);
return -EACCES;
@@ -17777,10 +17790,12 @@ static void collect_linked_regs(struct bpf_verifier_env *env,
reg = &func->regs[j];
__collect_linked_regs(linked_regs, reg, id, i, j, true);
}
- for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) {
- if (!bpf_is_spilled_reg(&func->stack[j]))
+ for (j = 0; j < bpf_stack_nr_slots(func); j++) {
+ struct bpf_stack_state *ss = bpf_stack_slot(func, j);
+
+ if (!bpf_is_spilled_reg(ss))
continue;
- reg = &func->stack[j].spilled_ptr;
+ reg = &ss->spilled_ptr;
__collect_linked_regs(linked_regs, reg, id, i, j, false);
}
}
@@ -17800,7 +17815,7 @@ static void sync_linked_regs(struct bpf_verifier_env *env, struct bpf_verifier_s
for (i = 0; i < linked_regs->cnt; ++i) {
e = &linked_regs->entries[i];
reg = e->is_reg ? &vstate->frame[e->frameno]->regs[e->regno]
- : &vstate->frame[e->frameno]->stack[e->spi].spilled_ptr;
+ : &bpf_stack_slot(vstate->frame[e->frameno], e->spi)->spilled_ptr;
if (reg->type != SCALAR_VALUE || reg == known_reg)
continue;
if ((reg->id & ~BPF_ADD_CONST) != (known_reg->id & ~BPF_ADD_CONST))
@@ -17918,7 +17933,7 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
}
if (insn_flags) {
- err = bpf_push_jmp_history(env, this_branch, insn_flags, 0, 0, 0);
+ err = bpf_push_jmp_history(env, this_branch, insn_flags, 0, 0, NULL, 0);
if (err)
return err;
}
@@ -17988,7 +18003,10 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
* if parent state is created.
*/
if (linked_regs.cnt > 1) {
- err = bpf_push_jmp_history(env, this_branch, 0, 0, 0, linked_regs_pack(&linked_regs));
+ u16 packed[LINKED_REGS_MAX];
+
+ linked_regs_pack(&linked_regs, packed);
+ err = bpf_push_jmp_history(env, this_branch, 0, 0, 0, packed, linked_regs.cnt);
if (err)
return err;
}
@@ -18877,23 +18895,27 @@ static void adjust_btf_func(struct bpf_verifier_env *env)
aux->func_info[i].insn_off = env->subprog_info[i].start;
}
-/* Find id in idset and increment its count, or add new entry */
-static void idset_cnt_inc(struct bpf_idset *idset, u32 id)
+/*
+ * Find id in idset and increment its count, or add new entry. Returns false
+ * when a new id could not be recorded, which leaves the counts incomplete.
+ */
+static bool idset_cnt_inc(struct bpf_idset *idset, u32 id)
{
u32 i;
for (i = 0; i < idset->num_ids; i++) {
if (idset->entries[i].id == id) {
idset->entries[i].cnt++;
- return;
+ return true;
}
}
- /* New id */
- if (idset->num_ids < BPF_ID_MAP_SIZE) {
- idset->entries[idset->num_ids].id = id;
- idset->entries[idset->num_ids].cnt = 1;
- idset->num_ids++;
- }
+ if (!bpf_id_scratch_reserve((void **)&idset->entries, &idset->cap, idset->num_ids,
+ sizeof(*idset->entries)))
+ return false;
+ idset->entries[idset->num_ids].id = id;
+ idset->entries[idset->num_ids].cnt = 1;
+ idset->num_ids++;
+ return true;
}
/* Find id in idset and return its count, or 0 if not found */
@@ -18919,6 +18941,7 @@ void bpf_clear_singular_ids(struct bpf_verifier_env *env,
struct bpf_idset *idset = &env->idset_scratch;
struct bpf_func_state *func;
struct bpf_reg_state *reg;
+ bool complete = true;
idset->num_ids = 0;
@@ -18927,9 +18950,17 @@ void bpf_clear_singular_ids(struct bpf_verifier_env *env,
continue;
if (!reg->id)
continue;
- idset_cnt_inc(idset, reg->id & ~BPF_ADD_CONST);
+ complete &= idset_cnt_inc(idset, reg->id & ~BPF_ADD_CONST);
}));
+ /*
+ * An id that could not be recorded may be shared, and a later
+ * occurrence of it may have been recorded with a count of one. Without
+ * complete counts keep every id; clearing is only an optimization.
+ */
+ if (!complete)
+ return;
+
bpf_for_each_reg_in_vstate(st, func, reg, ({
if (reg->type != SCALAR_VALUE)
continue;
@@ -19386,7 +19417,7 @@ static int do_check(struct bpf_verifier_env *env)
}
if (bpf_is_jmp_point(env, env->insn_idx)) {
- err = bpf_push_jmp_history(env, state, 0, 0, 0, 0);
+ err = bpf_push_jmp_history(env, state, 0, 0, 0, NULL, 0);
if (err)
return err;
}
@@ -22255,6 +22286,26 @@ static int bpf_prog_verify_signature(struct bpf_verifier_env *env,
return err;
}
+/*
+ * bpf_clone_redirect() transmits from inside the program, so a tc egress or
+ * lwt_xmit program that redirects to its own device runs again on top of its
+ * own frame, and again from there, until the datapath's recursion limit drops
+ * the packet: ten frames deep. Ten MAX_BPF_STACK frames fit the kernel stack
+ * as they always did; ten MAX_BPF_STACK_JIT frames would not, so a program
+ * calling it keeps the smaller budget. The redirect helpers that transmit
+ * after the program has returned leave no frame behind.
+ */
+static bool bpf_prog_reenters_datapath(const struct bpf_prog *prog)
+{
+ const struct bpf_insn *insn = prog->insnsi;
+ int i;
+
+ for (i = 0; i < prog->len; i++, insn++)
+ if (bpf_helper_call(insn) && insn->imm == BPF_FUNC_clone_redirect)
+ return true;
+ return false;
+}
+
int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,
struct bpf_log_attr *attr_log)
{
@@ -22279,6 +22330,9 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,
env->bt.env = env;
env->prog = *prog;
env->ops = bpf_verifier_ops[env->prog->type];
+ env->stack_limit = bpf_prog_stack_limit(env->prog);
+ if (bpf_prog_reenters_datapath(env->prog))
+ env->stack_limit = MAX_BPF_STACK;
env->allow_ptr_leaks = bpf_allow_ptr_leaks(env->prog->aux->token);
env->allow_uninit_stack = bpf_allow_uninit_stack(env->prog->aux->token);
@@ -22602,6 +22656,8 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,
kvfree(env->gotox_tmp_buf);
kvfree(env->callx_edges);
kvfree(env->func_ptrs);
+ kfree(env->idmap_scratch.map);
+ kfree(env->idset_scratch.entries);
bpf_diag_free(env);
kvfree(env);
return ret;
diff --git a/tools/testing/selftests/bpf/prog_tests/struct_ops_private_stack.c b/tools/testing/selftests/bpf/prog_tests/struct_ops_private_stack.c
index 98db9bafa44b2..2b3ec2b790918 100644
--- a/tools/testing/selftests/bpf/prog_tests/struct_ops_private_stack.c
+++ b/tools/testing/selftests/bpf/prog_tests/struct_ops_private_stack.c
@@ -4,6 +4,7 @@
#include "struct_ops_private_stack.skel.h"
#include "struct_ops_private_stack_fail.skel.h"
#include "struct_ops_private_stack_recur.skel.h"
+#include "struct_ops_private_stack_large.skel.h"
#if defined(__x86_64__) || defined(__aarch64__) || defined(__powerpc64__)
static void test_private_stack(void)
@@ -78,6 +79,34 @@ static void test_private_stack_recur(void)
struct_ops_private_stack_recur__destroy(skel);
}
+/* Two frames of 2 KiB each on the private stack */
+static void test_private_stack_large(void)
+{
+ struct struct_ops_private_stack_large *skel;
+ struct bpf_link *link;
+
+ if (!is_large_stack_supported()) {
+ test__skip();
+ return;
+ }
+
+ skel = struct_ops_private_stack_large__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "struct_ops_private_stack_large__open_and_load"))
+ return;
+
+ link = bpf_map__attach_struct_ops(skel->maps.testmod_1);
+ if (!ASSERT_OK_PTR(link, "attach_struct_ops"))
+ goto cleanup;
+
+ ASSERT_OK(trigger_module_test_read(256), "trigger_read");
+
+ ASSERT_EQ(skel->bss->val, 100 + 30 + 12, "val");
+
+ bpf_link__destroy(link);
+cleanup:
+ struct_ops_private_stack_large__destroy(skel);
+}
+
static void __test_struct_ops_private_stack(void)
{
if (test__start_subtest("private_stack"))
@@ -86,6 +115,8 @@ static void __test_struct_ops_private_stack(void)
test_private_stack_fail();
if (test__start_subtest("private_stack_recur"))
test_private_stack_recur();
+ if (test__start_subtest("private_stack_large"))
+ test_private_stack_large();
}
#else
static void __test_struct_ops_private_stack(void)
diff --git a/tools/testing/selftests/bpf/prog_tests/tailcalls.c b/tools/testing/selftests/bpf/prog_tests/tailcalls.c
index aefb46778307a..88d223110ecd5 100644
--- a/tools/testing/selftests/bpf/prog_tests/tailcalls.c
+++ b/tools/testing/selftests/bpf/prog_tests/tailcalls.c
@@ -10,6 +10,7 @@
#include "tc_bpf2bpf.skel.h"
#include "tailcall_fail.skel.h"
#include "tailcall_cgrp_storage_owner.skel.h"
+#include "tailcall_large_stack.skel.h"
#include "tailcall_cgrp_storage_no_storage.skel.h"
#include "tailcall_cgrp_storage.skel.h"
#include "tailcall_sleepable.skel.h"
@@ -2025,6 +2026,45 @@ static void test_tailcall_bpf2bpf_fexit_links(void)
tailcall_bpf2bpf2__destroy(skel_tc);
}
+/*
+ * test_tailcall_large_stack runs a tail call made from a subprog with a 1536
+ * byte frame, under a 240-byte caller, into a program with a 2 KiB frame:
+ *
+ * entry (240) --call-> subprog_tail (1536) --tailcall-> classifier_0 (2048)
+ */
+static void test_tailcall_large_stack(void)
+{
+ struct tailcall_large_stack *skel;
+ int err, prog_fd, map_fd, key = 0;
+ char buff[128] = {};
+ LIBBPF_OPTS(bpf_test_run_opts, topts,
+ .data_in = buff,
+ .data_size_in = sizeof(buff),
+ .repeat = 1,
+ );
+
+ if (!is_large_stack_supported()) {
+ test__skip();
+ return;
+ }
+
+ skel = tailcall_large_stack__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "tailcall_large_stack__open_and_load"))
+ return;
+
+ prog_fd = bpf_program__fd(skel->progs.classifier_0);
+ map_fd = bpf_map__fd(skel->maps.jmp_table);
+ err = bpf_map_update_elem(map_fd, &key, &prog_fd, BPF_ANY);
+ if (!ASSERT_OK(err, "update jmp_table"))
+ goto out;
+
+ err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.entry), &topts);
+ ASSERT_OK(err, "test_run");
+ ASSERT_EQ(topts.retval, 42 + 7, "retval");
+out:
+ tailcall_large_stack__destroy(skel);
+}
+
void test_tailcalls(void)
{
if (test__start_subtest("tailcall_1"))
@@ -2096,4 +2136,6 @@ void test_tailcalls(void)
test_tailcall_callback();
if (test__start_subtest("tailcall_bpf2bpf_fexit_links"))
test_tailcall_bpf2bpf_fexit_links();
+ if (test__start_subtest("tailcall_large_stack"))
+ test_tailcall_large_stack();
}
diff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c
index 9c3ff7d83fbe3..dbf98751aae65 100644
--- a/tools/testing/selftests/bpf/prog_tests/verifier.c
+++ b/tools/testing/selftests/bpf/prog_tests/verifier.c
@@ -62,6 +62,7 @@
#include "verifier_kfunc_uninit.skel.h"
#include "verifier_kfunc_uninit_multi.skel.h"
#include "verifier_ld_ind.skel.h"
+#include "verifier_large_stack.skel.h"
#include "verifier_ldsx.skel.h"
#include "verifier_leak_ptr.skel.h"
#include "verifier_linked_scalars.skel.h"
@@ -244,6 +245,7 @@ void test_verifier_kfunc_uninit(void) { RUN_TESTS(verifier_kfunc_uninit)
void test_verifier_kfunc_uninit_multi(void) { RUN_TESTS(verifier_kfunc_uninit_multi); }
void test_verifier_load_acquire(void) { RUN(verifier_load_acquire); }
void test_verifier_ld_ind(void) { RUN(verifier_ld_ind); }
+void test_verifier_large_stack(void) { RUN(verifier_large_stack); }
void test_verifier_ldsx(void) { RUN(verifier_ldsx); }
void test_verifier_leak_ptr(void) { RUN(verifier_leak_ptr); }
void test_verifier_linked_scalars(void) { RUN(verifier_linked_scalars); }
diff --git a/tools/testing/selftests/bpf/progs/async_stack_depth.c b/tools/testing/selftests/bpf/progs/async_stack_depth.c
index 36734683acbdb..9cd874a90b39a 100644
--- a/tools/testing/selftests/bpf/progs/async_stack_depth.c
+++ b/tools/testing/selftests/bpf/progs/async_stack_depth.c
@@ -29,7 +29,49 @@ static int bad_timer_cb(void *map, int *key, struct bpf_timer *timer)
return buf[255] + timer_cb(NULL, NULL, NULL);
}
+/*
+ * The same shapes scaled to the 2 KiB budget of JITs with large stacks. The
+ * compiler caps a single function at 512 bytes, so the depth comes from a
+ * chain of 480-byte frames.
+ */
+__attribute__((noinline))
+static int timer_cb_large_0(void *map, int *key, struct bpf_timer *timer)
+{
+ volatile char buf[480] = {};
+ return buf[69];
+}
+
+__attribute__((noinline))
+static int timer_cb_large_1(void *map, int *key, struct bpf_timer *timer)
+{
+ volatile char buf[480] = {};
+ return buf[69] + timer_cb_large_0(map, key, timer);
+}
+
+__attribute__((noinline))
+static int timer_cb_large_2(void *map, int *key, struct bpf_timer *timer)
+{
+ volatile char buf[480] = {};
+ return buf[69] + timer_cb_large_1(map, key, timer);
+}
+
+__attribute__((noinline))
+static int timer_cb_large_3(void *map, int *key, struct bpf_timer *timer)
+{
+ volatile char buf[480] = {};
+ return buf[69] + timer_cb_large_2(map, key, timer);
+}
+
+/* 5 * 480 = 2400 bytes on its own */
+__attribute__((noinline))
+static int bad_timer_cb_large(void *map, int *key, struct bpf_timer *timer)
+{
+ volatile char buf[480] = {};
+ return buf[255] + timer_cb_large_3(map, key, timer);
+}
+
SEC("tc")
+__load_if_no_large_stack()
__failure __msg("combined stack size of 2 calls is")
int pseudo_call_check(struct __sk_buff *ctx)
{
@@ -44,7 +86,25 @@ int pseudo_call_check(struct __sk_buff *ctx)
return bpf_timer_set_callback(&elem->timer, timer_cb) + buf[0];
}
+/* main plus the four frames under timer_cb_large_3: 2400 bytes */
SEC("tc")
+__load_if_large_stack()
+__failure __msg("combined stack size of 5 calls is")
+int pseudo_call_check_large(struct __sk_buff *ctx)
+{
+ struct hmap_elem *elem;
+ volatile char buf[480] = {};
+
+ elem = bpf_map_lookup_elem(&hmap, &(int){0});
+ if (!elem)
+ return 0;
+
+ timer_cb_large_3(NULL, NULL, NULL);
+ return bpf_timer_set_callback(&elem->timer, timer_cb_large_3) + buf[0];
+}
+
+SEC("tc")
+__load_if_no_large_stack()
__failure __msg("combined stack size of 2 calls is")
int async_call_root_check(struct __sk_buff *ctx)
{
@@ -58,4 +118,19 @@ int async_call_root_check(struct __sk_buff *ctx)
return bpf_timer_set_callback(&elem->timer, bad_timer_cb) + buf[0];
}
+SEC("tc")
+__load_if_large_stack()
+__failure __msg("combined stack size of 5 calls is")
+int async_call_root_check_large(struct __sk_buff *ctx)
+{
+ struct hmap_elem *elem;
+ volatile char buf[480] = {};
+
+ elem = bpf_map_lookup_elem(&hmap, &(int){0});
+ if (!elem)
+ return 0;
+
+ return bpf_timer_set_callback(&elem->timer, bad_timer_cb_large) + buf[0];
+}
+
char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/bpf_misc.h b/tools/testing/selftests/bpf/progs/bpf_misc.h
index 2ced1d751acea..f3dbc3b59bff2 100644
--- a/tools/testing/selftests/bpf/progs/bpf_misc.h
+++ b/tools/testing/selftests/bpf/progs/bpf_misc.h
@@ -175,6 +175,9 @@
#define __prepare_priv __test_tag("test_prepare_priv")
#define __load_if_JITed() __test_tag("load_mode=jited")
#define __load_if_no_JITed() __test_tag("load_mode=no_jited")
+/* Whether programs may use more than 512 bytes of stack on this kernel and JIT */
+#define __load_if_large_stack() __test_tag("stack_mode=large")
+#define __load_if_no_large_stack() __test_tag("stack_mode=small")
#define __stderr(msg) __test_tag("test_expect_stderr=" msg)
#define __stderr_unpriv(msg) __test_tag("test_expect_stderr_unpriv=" msg)
#define __stdout(msg) __test_tag("test_expect_stdout=" msg)
diff --git a/tools/testing/selftests/bpf/progs/struct_ops_private_stack_fail.c b/tools/testing/selftests/bpf/progs/struct_ops_private_stack_fail.c
index 1442728f56046..c8cb35b37867b 100644
--- a/tools/testing/selftests/bpf/progs/struct_ops_private_stack_fail.c
+++ b/tools/testing/selftests/bpf/progs/struct_ops_private_stack_fail.c
@@ -4,6 +4,7 @@
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include "../test_kmods/bpf_testmod.h"
+#include "bpf_misc.h"
char _license[] SEC("license") = "GPL";
@@ -25,6 +26,44 @@ __noinline static int subprog1(int *a)
return subprog2(a, b);
}
+/*
+ * A chain of 480-byte frames under test_2, so that its call chain exceeds
+ * the 2 KiB budget of JITs with large stacks as well as the 512 bytes
+ * allowed elsewhere. The compiler caps a single function at 512 bytes, and
+ * the buffers are volatile so that it cannot shrink them.
+ */
+__noinline static int subprog_deep4(int *a)
+{
+ volatile char b[480] = {};
+
+ __sink(b[479]);
+ return a[10] + b[20];
+}
+
+__noinline static int subprog_deep3(int *a)
+{
+ volatile char b[480] = {};
+
+ __sink(b[479]);
+ return subprog_deep4(a) + b[20];
+}
+
+__noinline static int subprog_deep2(int *a)
+{
+ volatile char b[480] = {};
+
+ __sink(b[479]);
+ return subprog_deep3(a) + b[20];
+}
+
+__noinline static int subprog_deep1(int *a)
+{
+ volatile char b[480] = {};
+
+ __sink(b[479]);
+ return subprog_deep2(a) + b[20];
+}
+
SEC("struct_ops")
int BPF_PROG(test_1)
@@ -41,11 +80,13 @@ int BPF_PROG(test_1)
SEC("struct_ops")
int BPF_PROG(test_2)
{
- /* stack size 400 bytes */
- int a[100] = {};
+ /* stack size 476 bytes, over 2 KiB with the four 480-byte deep subprogs */
+ volatile char buf[376] = {};
+ int a[25] = {};
+ __sink(buf[375]);
a[10] = 3;
- val_j = subprog1(a);
+ val_j = subprog1(a) + subprog_deep1(a);
return 0;
}
diff --git a/tools/testing/selftests/bpf/progs/struct_ops_private_stack_large.c b/tools/testing/selftests/bpf/progs/struct_ops_private_stack_large.c
new file mode 100644
index 0000000000000..94a25a2cff6ec
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/struct_ops_private_stack_large.c
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "../test_kmods/bpf_testmod.h"
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+long val;
+
+/* On a private stack every frame gets the whole 2 KiB budget. */
+__used __naked
+static long frame_2048_leaf(void)
+{
+ asm volatile (" \
+ r1 = 30; \
+ *(u64 *)(r10 - 2048) = r1; \
+ r1 = 12; \
+ *(u64 *)(r10 - 8) = r1; \
+ r0 = *(u64 *)(r10 - 2048); \
+ r1 = *(u64 *)(r10 - 8); \
+ r0 += r1; \
+ exit; \
+" ::: __clobber_all);
+}
+
+/* test_1 is the member bpf_testmod requests a private stack for */
+SEC("struct_ops")
+__naked int test_1(void)
+{
+ asm volatile (" \
+ r1 = 100; \
+ *(u64 *)(r10 - 2048) = r1; \
+ call frame_2048_leaf; \
+ r1 = *(u64 *)(r10 - 2048); \
+ r0 += r1; \
+ r1 = %[val] ll; \
+ *(u64 *)(r1 + 0) = r0; \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm_addr(val)
+ : __clobber_all);
+}
+
+SEC(".struct_ops")
+struct bpf_testmod_ops3 testmod_1 = {
+ .test_1 = (void *)test_1,
+};
diff --git a/tools/testing/selftests/bpf/progs/tailcall_large_stack.c b/tools/testing/selftests/bpf/progs/tailcall_large_stack.c
new file mode 100644
index 0000000000000..977197dac5d37
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/tailcall_large_stack.c
@@ -0,0 +1,62 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+
+struct {
+ __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+ __uint(max_entries, 1);
+ __uint(key_size, sizeof(__u32));
+ __uint(value_size, sizeof(__u32));
+} jmp_table SEC(".maps");
+
+/* The tail call target sets up a 2 KiB frame of its own and uses both of its ends. */
+SEC("tc")
+__naked int classifier_0(void)
+{
+ asm volatile (" \
+ r1 = 42; \
+ *(u64 *)(r10 - 2048) = r1; \
+ r1 = 7; \
+ *(u64 *)(r10 - 8) = r1; \
+ r0 = *(u64 *)(r10 - 2048); \
+ r1 = *(u64 *)(r10 - 8); \
+ r0 += r1; \
+ exit; \
+" ::: __clobber_all);
+}
+
+/*
+ * The frame of the subprog doing the tail call is unwound by it, so it may be
+ * large; only the frames of its callers stay behind and are limited to 256
+ * bytes in total. Returns 1 when the tail call falls through.
+ */
+__used __naked
+static int subprog_tail(void)
+{
+ asm volatile (" \
+ r2 = 1; \
+ *(u64 *)(r10 - 1536) = r2; \
+ r2 = %[jmp_table] ll; \
+ r3 = 0; \
+ call %[bpf_tail_call]; \
+ r0 = 1; \
+ exit; \
+" :
+ : __imm(bpf_tail_call),
+ __imm_addr(jmp_table)
+ : __clobber_all);
+}
+
+SEC("tc")
+__naked int entry(void)
+{
+ asm volatile (" \
+ r2 = 2; \
+ *(u64 *)(r10 - 240) = r2; \
+ call subprog_tail; \
+ exit; \
+" ::: __clobber_all);
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/test_global_func1.c b/tools/testing/selftests/bpf/progs/test_global_func1.c
index fc69ff18880d5..f0eca282e0d43 100644
--- a/tools/testing/selftests/bpf/progs/test_global_func1.c
+++ b/tools/testing/selftests/bpf/progs/test_global_func1.c
@@ -48,8 +48,73 @@ int f3(int val, struct __sk_buff *skb, int var)
}
SEC("tc")
+__load_if_no_large_stack()
__failure __msg("combined stack size of 3 calls is")
int global_func1(struct __sk_buff *skb)
{
return f0(1, skb) + f1(skb) + f2(2, skb) + f3(3, skb, 4);
}
+
+/*
+ * A chain of five frames that stay under 512 bytes each but add up to more
+ * than the 2 KiB budget of JITs with large stacks; the chain also exceeds
+ * 512 bytes after two frames, so it is rejected everywhere.
+ */
+#define MAX_STACK_LARGE 480
+
+__attribute__ ((noinline))
+int g0(struct __sk_buff *skb)
+{
+ volatile char buf[MAX_STACK_LARGE] = {};
+
+ __sink(buf[MAX_STACK_LARGE - 1]);
+
+ return skb->len;
+}
+
+__attribute__ ((noinline))
+int g1(struct __sk_buff *skb)
+{
+ volatile char buf[MAX_STACK_LARGE] = {};
+
+ __sink(buf[MAX_STACK_LARGE - 1]);
+
+ return g0(skb) + skb->len;
+}
+
+__attribute__ ((noinline))
+int g2(struct __sk_buff *skb)
+{
+ volatile char buf[MAX_STACK_LARGE] = {};
+
+ __sink(buf[MAX_STACK_LARGE - 1]);
+
+ return g1(skb) + skb->len;
+}
+
+__attribute__ ((noinline))
+int g3(struct __sk_buff *skb)
+{
+ volatile char buf[MAX_STACK_LARGE] = {};
+
+ __sink(buf[MAX_STACK_LARGE - 1]);
+
+ return g2(skb) + skb->len;
+}
+
+__attribute__ ((noinline))
+int g4(struct __sk_buff *skb)
+{
+ volatile char buf[MAX_STACK_LARGE] = {};
+
+ __sink(buf[MAX_STACK_LARGE - 1]);
+
+ return g3(skb) + skb->len;
+}
+
+SEC("tc")
+__failure __msg("combined stack size of {{[0-9]+}} calls is")
+int global_func1_deep(struct __sk_buff *skb)
+{
+ return g4(skb);
+}
diff --git a/tools/testing/selftests/bpf/progs/test_global_func_deep_stack.c b/tools/testing/selftests/bpf/progs/test_global_func_deep_stack.c
index 1b634b543b629..edb8a223a3cb2 100644
--- a/tools/testing/selftests/bpf/progs/test_global_func_deep_stack.c
+++ b/tools/testing/selftests/bpf/progs/test_global_func_deep_stack.c
@@ -67,12 +67,30 @@ int XCAT(f, n)(unsigned long a) \
#define F_31 F_30 FN(31, 30)
#define F_32 F_31 FN(32, 31)
+/* Same, with a 480-byte frame, to exceed the 2 KiB budget of large stacks. */
+#define FNB(n, prev) \
+__attribute__((noinline)) \
+int XCAT(f, n)(unsigned long a) \
+{ \
+ volatile char buf[480] = {}; \
+ volatile long b = XCAT(f, prev)(a - 1); \
+ if (!b) \
+ return 0; \
+ return b + buf[479] + 1; \
+}
+
+#define F_33 F_32 FNB(33, 32)
+#define F_34 F_33 FNB(34, 33)
+#define F_35 F_34 FNB(35, 34)
+#define F_36 F_35 FNB(36, 35)
+#define F_37 F_36 FNB(37, 36)
+
#define CAT2(a, b) a ## b
#define XCAT2(a, b) CAT2(a, b)
#define F(n) XCAT2(F_, n)
-F(32)
+F(37)
/* Ensure that even 32 levels deep, the function verifies. */
SEC("syscall")
@@ -88,8 +106,21 @@ int global_func_deep_stack_success(struct __sk_buff *skb)
* the size.
*/
SEC("syscall")
+__load_if_no_large_stack()
__failure __msg("combined stack size of 34 calls")
int global_func_deep_stack_fail(struct __sk_buff *skb)
{
return f32(123);
}
+
+/*
+ * Five 480-byte frames on top of the chain: 5 * 480 + 33 * 16 = 2928 bytes,
+ * more than the 2 KiB budget of JITs with large stacks, and more than 512
+ * bytes after the second frame everywhere else.
+ */
+SEC("syscall")
+__failure __msg("combined stack size of {{[0-9]+}} calls")
+int global_func_deep_stack_fail_large(struct __sk_buff *skb)
+{
+ return f37(123);
+}
diff --git a/tools/testing/selftests/bpf/progs/verifier_callx.c b/tools/testing/selftests/bpf/progs/verifier_callx.c
index 238fc75ad154d..ea3d3f97da312 100644
--- a/tools/testing/selftests/bpf/progs/verifier_callx.c
+++ b/tools/testing/selftests/bpf/progs/verifier_callx.c
@@ -626,19 +626,64 @@ static unsigned long use_stack_304(void)
);
}
+
+/* Four 480-byte frames, deeper than any budget together with their caller */
+__naked __noinline __used
+static unsigned long use_stack_480_0(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "*(u64 *)(r10 - 480) = r0;"
+ "exit;"
+ );
+}
+
+__naked __noinline __used
+static unsigned long use_stack_480_1(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "*(u64 *)(r10 - 480) = r0;"
+ "call use_stack_480_0;"
+ "exit;"
+ );
+}
+
+__naked __noinline __used
+static unsigned long use_stack_480_2(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "*(u64 *)(r10 - 480) = r0;"
+ "call use_stack_480_1;"
+ "exit;"
+ );
+}
+
+__naked __noinline __used
+static unsigned long use_stack_480_3(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "*(u64 *)(r10 - 480) = r0;"
+ "call use_stack_480_2;"
+ "exit;"
+ );
+}
+
/* stack of the callee of callx is accounted */
SEC("socket")
-__failure __msg("combined stack size of 2 calls is")
+__failure __msg("combined stack size of {{[0-9]+}} calls is")
__naked void callx_stack_depth(void)
{
asm volatile (
"r0 = 0;"
- "*(u64 *)(r10 - 304) = r0;"
- "r2 = %[use_stack_304] ll;"
+ "*(u64 *)(r10 - 480) = r0;"
+ "r2 = %[use_stack_480_3] ll;"
"callx r2;"
"exit;"
:
- : __imm_addr(use_stack_304)
+ : __imm_addr(use_stack_480_3)
: __clobber_all);
}
@@ -655,19 +700,19 @@ static unsigned long apply_stack_304(void)
}
/*
- * The address of use_stack_304() is taken by the main prog that doesn't
+ * The address of use_stack_480_3() is taken by the main prog that doesn't
* use stack, but it is called from apply_stack_304().
*/
SEC("socket")
-__failure __msg("combined stack size of 3 calls is")
+__failure __msg("combined stack size of {{[0-9]+}} calls is")
__naked void callx_stack_depth_nested(void)
{
asm volatile (
- "r1 = %[use_stack_304] ll;"
+ "r1 = %[use_stack_480_3] ll;"
"call apply_stack_304;"
"exit;"
:
- : __imm_addr(use_stack_304)
+ : __imm_addr(use_stack_480_3)
: __clobber_all);
}
diff --git a/tools/testing/selftests/bpf/progs/verifier_callx_rodata.c b/tools/testing/selftests/bpf/progs/verifier_callx_rodata.c
index af1f9305da377..65d34769c8bfd 100644
--- a/tools/testing/selftests/bpf/progs/verifier_callx_rodata.c
+++ b/tools/testing/selftests/bpf/progs/verifier_callx_rodata.c
@@ -579,23 +579,58 @@ __naked void callx_rodata_recursion(void)
::: __clobber_all);
}
+
+/* Four 480-byte frames, deeper than any budget together with their caller */
__naked __noinline __used
-static unsigned long use_stack_304(void)
+static unsigned long use_stack_480_0(void)
{
asm volatile (
"r0 = 0;"
- "*(u64 *)(r10 - 304) = r0;"
+ "*(u64 *)(r10 - 480) = r0;"
+ "exit;"
+ );
+}
+
+__naked __noinline __used
+static unsigned long use_stack_480_1(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "*(u64 *)(r10 - 480) = r0;"
+ "call use_stack_480_0;"
+ "exit;"
+ );
+}
+
+__naked __noinline __used
+static unsigned long use_stack_480_2(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "*(u64 *)(r10 - 480) = r0;"
+ "call use_stack_480_1;"
+ "exit;"
+ );
+}
+
+__naked __noinline __used
+static unsigned long use_stack_480_3(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "*(u64 *)(r10 - 480) = r0;"
+ "call use_stack_480_2;"
"exit;"
);
}
/* stack of all possible callees is accounted */
SEC("socket")
-__failure __msg("combined stack size of 2 calls is")
+__failure __msg("combined stack size of {{[0-9]+}} calls is")
__naked void callx_rodata_stack_depth(void)
{
asm volatile (
- FUNC_TABLE2(tbl, ret0, use_stack_304)
+ FUNC_TABLE2(tbl, ret0, use_stack_480_3)
"r0 = 0;"
"*(u64 *)(r10 - 304) = r0;"
"call %[bpf_get_prandom_u32];"
@@ -613,11 +648,11 @@ __naked void callx_rodata_stack_depth(void)
/* stack of a callback that is read from the data is accounted too */
SEC("socket")
-__failure __msg("combined stack size of 2 calls is")
+__failure __msg("combined stack size of {{[0-9]+}} calls is")
__naked void callx_rodata_callback_stack_depth(void)
{
asm volatile (
- FUNC_TABLE2(tbl, use_stack_304, ret0)
+ FUNC_TABLE2(tbl, use_stack_480_3, ret0)
"r0 = 0;"
"*(u64 *)(r10 - 304) = r0;"
"r6 = tbl_%= ll;"
diff --git a/tools/testing/selftests/bpf/progs/verifier_large_stack.c b/tools/testing/selftests/bpf/progs/verifier_large_stack.c
new file mode 100644
index 0000000000000..c2a3ddf5ac913
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/verifier_large_stack.c
@@ -0,0 +1,425 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+
+/*
+ * Programs may use MAX_BPF_STACK_JIT (2 KiB) of stack on JITs that support
+ * large stacks, combined over a call chain, with no separate limit on a
+ * single frame. Interpreted programs and other JITs keep 512 bytes.
+ */
+
+SEC("socket")
+__description("single frame of 2048 bytes")
+__load_if_large_stack()
+__success __success_unpriv __retval(42)
+__naked void single_frame_2048(void)
+{
+ asm volatile (" \
+ r1 = r10; \
+ r1 += -2048; \
+ r0 = 42; \
+ *(u64*)(r1 + 0) = r0; \
+ r0 = *(u64*)(r1 + 0); \
+ exit; \
+" ::: __clobber_all);
+}
+
+SEC("socket")
+__description("single frame of 2048 bytes without large stack support")
+__load_if_no_large_stack()
+__failure __msg("invalid write to stack R1 off=-2048 size=8")
+__naked void single_frame_2048_no_large_stack(void)
+{
+ asm volatile (" \
+ r1 = r10; \
+ r1 += -2048; \
+ r0 = 42; \
+ *(u64*)(r1 + 0) = r0; \
+ exit; \
+" ::: __clobber_all);
+}
+
+__used __naked
+static void frame_512_leaf(void)
+{
+ asm volatile (" \
+ r1 = 1; \
+ *(u64 *)(r10 - 512) = r1; \
+ exit; \
+" ::: __clobber_all);
+}
+
+__used __naked
+static void frame_512_depth_2(void)
+{
+ asm volatile (" \
+ r1 = 2; \
+ *(u64 *)(r10 - 512) = r1; \
+ call frame_512_leaf; \
+ exit; \
+" ::: __clobber_all);
+}
+
+__used __naked
+static void frame_512_depth_3(void)
+{
+ asm volatile (" \
+ r1 = 3; \
+ *(u64 *)(r10 - 512) = r1; \
+ call frame_512_depth_2; \
+ exit; \
+" ::: __clobber_all);
+}
+
+__used __naked
+static void frame_512_depth_4(void)
+{
+ asm volatile (" \
+ r1 = 4; \
+ *(u64 *)(r10 - 512) = r1; \
+ call frame_512_depth_3; \
+ exit; \
+" ::: __clobber_all);
+}
+
+SEC("socket")
+__description("four frames of 512 bytes fit the 2 KiB budget")
+__load_if_large_stack()
+__success __log_level(4) __msg("stack depth max 2048")
+__naked void four_frames_of_512(void)
+{
+ asm volatile (" \
+ call frame_512_depth_4; \
+ r0 = 0; \
+ exit; \
+" ::: __clobber_all);
+}
+
+SEC("socket")
+__description("five frames of 512 bytes exceed the 2 KiB budget")
+__load_if_large_stack()
+__failure __msg("combined stack size of 5 calls is 2560. Too large")
+__naked void five_frames_of_512(void)
+{
+ asm volatile (" \
+ r1 = 5; \
+ *(u64 *)(r10 - 512) = r1; \
+ call frame_512_depth_4; \
+ r0 = 0; \
+ exit; \
+" ::: __clobber_all);
+}
+
+__used __naked
+static void frame_1536_leaf(void)
+{
+ asm volatile (" \
+ r1 = 1; \
+ *(u64 *)(r10 - 1536) = r1; \
+ exit; \
+" ::: __clobber_all);
+}
+
+SEC("socket")
+__description("512-byte frame calling a 1536-byte frame")
+__load_if_large_stack()
+__success __log_level(4) __msg("stack depth max 2048")
+__naked void uneven_frames_fit(void)
+{
+ asm volatile (" \
+ r1 = 2; \
+ *(u64 *)(r10 - 512) = r1; \
+ call frame_1536_leaf; \
+ r0 = 0; \
+ exit; \
+" ::: __clobber_all);
+}
+
+SEC("socket")
+__description("520-byte frame calling a 1536-byte frame")
+__load_if_large_stack()
+__failure __msg("combined stack size of 2 calls is 2064. Too large")
+__naked void uneven_frames_exceed(void)
+{
+ asm volatile (" \
+ r1 = 2; \
+ *(u64 *)(r10 - 520) = r1; \
+ call frame_1536_leaf; \
+ r0 = 0; \
+ exit; \
+" ::: __clobber_all);
+}
+
+#ifdef __BPF_FEATURE_MAY_GOTO
+/* may_goto adds its counter below the frame; a JIT does not hold that against the budget */
+SEC("socket")
+__description("frame of 2048 bytes with may_goto")
+__load_if_large_stack()
+__success __retval(42)
+__naked void frame_2048_with_may_goto(void)
+{
+ asm volatile (" \
+ r1 = r10; \
+ r1 += -2048; \
+ r0 = 42; \
+ *(u32*)(r1 + 0) = r0; \
+ may_goto l0_%=; \
+ r2 = 100; \
+ l0_%=: \
+ exit; \
+" ::: __clobber_all);
+}
+#endif
+
+SEC("socket")
+__description("variable offset write reaching 2048 bytes deep")
+__load_if_large_stack()
+__success
+__naked void var_off_write_to_2048(void)
+{
+ asm volatile (" \
+ call %[bpf_get_prandom_u32]; \
+ r0 &= 8; \
+ r2 = r10; \
+ r2 += -2048; \
+ r2 += r0; \
+ r1 = 0; \
+ *(u64*)(r2 + 0) = r1; \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm(bpf_get_prandom_u32)
+ : __clobber_all);
+}
+
+SEC("socket")
+__description("variable offset write reaching 2056 bytes deep")
+__load_if_large_stack()
+__failure __msg("invalid variable-offset write to stack R2")
+__naked void var_off_write_to_2056(void)
+{
+ asm volatile (" \
+ call %[bpf_get_prandom_u32]; \
+ r0 &= 8; \
+ r2 = r10; \
+ r2 += -2056; \
+ r2 += r0; \
+ r1 = 0; \
+ *(u64*)(r2 + 0) = r1; \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm(bpf_get_prandom_u32)
+ : __clobber_all);
+}
+
+/* Each frame of a private stack gets the whole budget. */
+__used __naked
+static void priv_stack_frame_2048(void)
+{
+ asm volatile (" \
+ r1 = 1; \
+ *(u64 *)(r10 - 2048) = r1; \
+ exit; \
+" ::: __clobber_all);
+}
+
+SEC("kprobe")
+__description("private stack: two frames of 2048 bytes")
+__load_if_large_stack()
+__arch_x86_64
+__arch_arm64
+__success __log_level(4)
+__msg("stack depth max 2048")
+__msg("subprog 0 (private_stack_two_frames) main {{.*}} stack 2048")
+__msg("subprog 1 (priv_stack_frame_2048) static {{.*}} stack 2048")
+__naked void private_stack_two_frames(void)
+{
+ asm volatile (" \
+ r1 = 2; \
+ *(u64 *)(r10 - 2048) = r1; \
+ call priv_stack_frame_2048; \
+ r0 = 0; \
+ exit; \
+" ::: __clobber_all);
+}
+
+struct {
+ __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+ __uint(max_entries, 1);
+ __uint(key_size, sizeof(__u32));
+ __uint(value_size, sizeof(__u32));
+} jmp_table SEC(".maps");
+
+/*
+ * A tail call unwinds the frame of the program doing it, so a large main
+ * frame is fine; the 256-byte rule only concerns the frames of callers of a
+ * subprog that tail calls.
+ */
+SEC("tc")
+__description("tail call from a 1 KiB frame")
+__load_if_large_stack()
+__success
+__naked void tail_call_from_large_frame(void)
+{
+ asm volatile (" \
+ r2 = 42; \
+ *(u64 *)(r10 - 1024) = r2; \
+ r2 = %[jmp_table] ll; \
+ r3 = 0; \
+ call %[bpf_tail_call]; \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm(bpf_tail_call),
+ __imm_addr(jmp_table)
+ : __clobber_all);
+}
+
+/*
+ * bpf_clone_redirect() can run the program again on top of its own frame,
+ * ten frames deep, so a program calling it keeps the 512-byte budget. The
+ * redirect that happens after the program returns does not.
+ */
+SEC("tc")
+__description("1 KiB frame with bpf_clone_redirect keeps the 512-byte budget")
+__load_if_large_stack()
+__failure __msg("invalid write to stack R1 off=-1024 size=8")
+__naked void clone_redirect_keeps_512(void)
+{
+ asm volatile (" \
+ r6 = r1; \
+ r1 = r10; \
+ r1 += -1024; \
+ r0 = 42; \
+ *(u64 *)(r1 + 0) = r0; \
+ r1 = r6; \
+ r2 = 1; \
+ r3 = 0; \
+ call %[bpf_clone_redirect]; \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm(bpf_clone_redirect)
+ : __clobber_all);
+}
+
+SEC("tc")
+__description("1 KiB frame with bpf_redirect keeps the 2 KiB budget")
+__load_if_large_stack()
+__success
+__naked void redirect_keeps_2048(void)
+{
+ asm volatile (" \
+ r1 = r10; \
+ r1 += -1024; \
+ r0 = 42; \
+ *(u64 *)(r1 + 0) = r0; \
+ r1 = 1; \
+ r2 = 0; \
+ call %[bpf_redirect]; \
+ exit; \
+" :
+ : __imm(bpf_redirect)
+ : __clobber_all);
+}
+
+/* Global subprogs are verified on their own but share the call chain budget. */
+__used __naked int global_frame_1536(void)
+{
+ asm volatile (" \
+ r1 = 1; \
+ *(u64 *)(r10 - 1536) = r1; \
+ r0 = 0; \
+ exit; \
+" ::: __clobber_all);
+}
+
+SEC("socket")
+__description("512-byte frame calling a 1536-byte global subprog")
+__load_if_large_stack()
+__success __log_level(4) __msg("stack depth max 2048")
+__naked void global_subprog_fits(void)
+{
+ asm volatile (" \
+ r1 = 2; \
+ *(u64 *)(r10 - 512) = r1; \
+ call global_frame_1536; \
+ r0 = 0; \
+ exit; \
+" ::: __clobber_all);
+}
+
+SEC("socket")
+__description("520-byte frame calling a 1536-byte global subprog")
+__load_if_large_stack()
+__failure __msg("combined stack size of 2 calls is 2064. Too large")
+__naked void global_subprog_exceeds(void)
+{
+ asm volatile (" \
+ r1 = 2; \
+ *(u64 *)(r10 - 520) = r1; \
+ call global_frame_1536; \
+ r0 = 0; \
+ exit; \
+" ::: __clobber_all);
+}
+
+/* Callback frames are part of the chain of the helper that calls them. */
+static __naked int loop_cb_1536(void)
+{
+ asm volatile (" \
+ r1 = 1; \
+ *(u64 *)(r10 - 1536) = r1; \
+ r0 = 0; \
+ exit; \
+" ::: __clobber_all);
+}
+
+SEC("socket")
+__description("512-byte frame with a 1536-byte bpf_loop callback")
+__load_if_large_stack()
+__success __log_level(4) __msg("stack depth max 2048")
+__naked void loop_callback_fits(void)
+{
+ asm volatile (" \
+ r1 = 2; \
+ *(u64 *)(r10 - 512) = r1; \
+ r1 = 1; \
+ r2 = %[loop_cb_1536]; \
+ r3 = 0; \
+ r4 = 0; \
+ call %[bpf_loop]; \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm_ptr(loop_cb_1536),
+ __imm(bpf_loop)
+ : __clobber_common);
+}
+
+SEC("socket")
+__description("520-byte frame with a 1536-byte bpf_loop callback")
+__load_if_large_stack()
+__failure __msg("combined stack size of 2 calls is 2064. Too large")
+__naked void loop_callback_exceeds(void)
+{
+ asm volatile (" \
+ r1 = 2; \
+ *(u64 *)(r10 - 520) = r1; \
+ r1 = 1; \
+ r2 = %[loop_cb_1536]; \
+ r3 = 0; \
+ r4 = 0; \
+ call %[bpf_loop]; \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm_ptr(loop_cb_1536),
+ __imm(bpf_loop)
+ : __clobber_common);
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/verifier_live_stack.c b/tools/testing/selftests/bpf/progs/verifier_live_stack.c
index 7a1a0670f851d..16b2b1e575342 100644
--- a/tools/testing/selftests/bpf/progs/verifier_live_stack.c
+++ b/tools/testing/selftests/bpf/progs/verifier_live_stack.c
@@ -318,7 +318,7 @@ struct {
} map_array SEC(".maps");
SEC("socket")
-__failure __msg("invalid read from stack R2 off=-1024 size=8")
+__failure __msg("invalid read from stack R2 off=-4096 size=8")
__flag(BPF_F_TEST_STATE_FREQ)
__naked unsigned long caller_stack_write_tail_call(void)
{
@@ -329,7 +329,7 @@ __naked unsigned long caller_stack_write_tail_call(void)
"if r0 != 42 goto 1f;"
"goto 2f;"
"1:"
- "*(u64 *)(r10 - 8) = -1024;"
+ "*(u64 *)(r10 - 8) = -4096;"
"2:"
"r1 = r6;"
"r2 = r10;"
@@ -1953,7 +1953,7 @@ static __used __naked void fwd_parent_key_to_helper(void)
SEC("socket")
__log_level(2)
__success
-__msg("call bpf_map_update_elem{{.*}}; use: fp1-8..-512 fp0-8")
+__msg("call bpf_map_update_elem{{.*}}; use: fp1-8..-{{(512|2048)}} fp0-8")
__naked void helper_arg_fallback_keeps_scanning(void)
{
asm volatile (
@@ -2267,7 +2267,7 @@ static __used __naked void merge_leaf_read(void)
SEC("socket")
__log_level(2)
__success
-__msg("call bpf_loop#181 ; use: fp2-8..-512 fp1-8..-512 fp0-8..-512")
+__msg("call bpf_loop#181 ; use: fp2-8..-{{(512|2048)}} fp1-8..-{{(512|2048)}} fp0-8..-{{(512|2048)}}")
__naked void bpf_loop_two_callbacks(void)
{
asm volatile (
@@ -2839,3 +2839,99 @@ static __used __naked void imprecise_dst_spill_join_sub(void)
:: __imm(bpf_get_prandom_u32)
: __clobber_all);
}
+
+/*
+ * A store that does not fully cover a 4-byte half-slot defines nothing, so a
+ * narrow store at the top of the frame must not turn any slot into a "def",
+ * least of all every slot of the frame: the earlier data at fp-8 stays live.
+ */
+SEC("socket")
+__log_level(2)
+__msg("0: (79) r0 = *(u64 *)(r10 -8) ; use: fp0-8")
+__msg("1: (73) *(u8 *)(r10 -1) = r0{{$}}")
+__msg("2: (6b) *(u16 *)(r10 -4) = r0{{$}}")
+__msg("3: (79) r0 = *(u64 *)(r10 -8) ; use: fp0-8")
+__naked void narrow_store_defines_nothing(void)
+{
+ asm volatile (
+ "r0 = *(u64 *)(r10 - 8);"
+ "*(u8 *)(r10 - 1) = r0;"
+ "*(u16 *)(r10 - 4) = r0;"
+ "r0 = *(u64 *)(r10 - 8);"
+ "exit;"
+ ::: __clobber_all);
+}
+
+/*
+ * The same callee instance is analyzed twice: the call sites are visited in
+ * postorder, so the second one goes first with a precise pointer 248 bytes
+ * into the main frame, and the first one then passes a pointer of unknown
+ * offset, which reads the whole frame. The precise pass stays within the
+ * first word of the masks on 64-bit, so the whole-frame pass is wider under
+ * every stack budget and merging the second into the first has to widen the
+ * masks while keeping the whole-frame read.
+ */
+SEC("socket")
+__log_level(2)
+__msg("stack use/def subprog#{{[0-9]+}} merge_read_all_callee (d2,cs{{[0-9]+}}):")
+__msg("(79) r0 = *(u64 *)(r1 +0){{.*}}; use: fp0-8..-{{(512|2048)}}")
+__naked void merge_keeps_whole_frame_read(void)
+{
+ asm volatile (
+ "r1 = 0;"
+ "*(u64 *)(r10 - 8) = r1;"
+ "*(u64 *)(r10 - 16) = r1;"
+ "*(u64 *)(r10 - 248) = r1;"
+ "call %[bpf_get_prandom_u32];"
+ "r0 &= 8;"
+ "r1 = r10;"
+ "r1 += -16;"
+ "r1 += r0;"
+ "call merge_read_all_mid;"
+ "r1 = r10;"
+ "r1 += -248;"
+ "call merge_read_all_mid;"
+ "r0 = 0;"
+ "exit;"
+ :: __imm(bpf_get_prandom_u32)
+ : __clobber_all);
+}
+
+static __used __naked void merge_read_all_mid(void)
+{
+ asm volatile (
+ "call merge_read_all_callee;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+static __used __naked void merge_read_all_callee(void)
+{
+ asm volatile (
+ "r0 = *(u64 *)(r1 + 0);"
+ "exit;"
+ ::: __clobber_all);
+}
+
+/*
+ * A frame pointer spilled below fp-512 is a spill like any other: the fill
+ * restores its identity, and a load through it reads only the slot it names
+ * instead of the whole frame.
+ */
+SEC("socket")
+__log_level(2)
+__load_if_large_stack()
+__msg("(79) r0 = *(u64 *)(r1 +0){{.*}}; use: fp0-8{{$}}")
+__naked void spill_below_512_stays_precise(void)
+{
+ asm volatile (
+ "r1 = 0;"
+ "*(u64 *)(r10 - 8) = r1;"
+ "r1 = r10;"
+ "r1 += -8;"
+ "*(u64 *)(r10 - 520) = r1;"
+ "r1 = *(u64 *)(r10 - 520);"
+ "r0 = *(u64 *)(r1 + 0);"
+ "exit;"
+ ::: __clobber_all);
+}
diff --git a/tools/testing/selftests/bpf/progs/verifier_raw_stack.c b/tools/testing/selftests/bpf/progs/verifier_raw_stack.c
index 9f0f48ecb4216..0fe631411b9cc 100644
--- a/tools/testing/selftests/bpf/progs/verifier_raw_stack.c
+++ b/tools/testing/selftests/bpf/progs/verifier_raw_stack.c
@@ -240,6 +240,7 @@ __naked void load_bytes_spilled_regs_data(void)
SEC("tc")
__description("raw_stack: skb_load_bytes, invalid access 1")
+__load_if_no_large_stack()
__failure __msg("invalid write to stack R3 off=-513 size=8")
__naked void load_bytes_invalid_access_1(void)
{
@@ -257,6 +258,26 @@ __naked void load_bytes_invalid_access_1(void)
: __clobber_all);
}
+SEC("tc")
+__description("raw_stack: skb_load_bytes, invalid access 1, large stack")
+__load_if_large_stack()
+__failure __msg("invalid write to stack R3 off=-2049 size=8")
+__naked void load_bytes_invalid_access_1_large(void)
+{
+ asm volatile (" \
+ r2 = 4; \
+ r6 = r10; \
+ r6 += -2049; \
+ r3 = r6; \
+ r4 = 8; \
+ call %[bpf_skb_load_bytes]; \
+ r0 = *(u64*)(r6 + 0); \
+ exit; \
+" :
+ : __imm(bpf_skb_load_bytes)
+ : __clobber_all);
+}
+
SEC("tc")
__description("raw_stack: skb_load_bytes, invalid access 2")
__failure __msg("invalid write to stack R3 off=-1 size=8")
diff --git a/tools/testing/selftests/bpf/progs/verifier_stack_ptr.c b/tools/testing/selftests/bpf/progs/verifier_stack_ptr.c
index 8e8cf8232255f..3e0bea9819cab 100644
--- a/tools/testing/selftests/bpf/progs/verifier_stack_ptr.c
+++ b/tools/testing/selftests/bpf/progs/verifier_stack_ptr.c
@@ -235,6 +235,7 @@ __naked void to_stack_check_low_1(void)
SEC("socket")
__description("PTR_TO_STACK check low 2")
+__load_if_no_large_stack()
__success __failure_unpriv
__msg_unpriv("R1 stack pointer arithmetic goes out of range")
__retval(42)
@@ -250,8 +251,27 @@ __naked void to_stack_check_low_2(void)
" ::: __clobber_all);
}
+SEC("socket")
+__description("PTR_TO_STACK check low 2, large stack")
+__load_if_large_stack()
+__success __failure_unpriv
+__msg_unpriv("R1 stack pointer arithmetic goes out of range")
+__retval(42)
+__naked void to_stack_check_low_2_large(void)
+{
+ asm volatile (" \
+ r1 = r10; \
+ r1 += -2049; \
+ r0 = 42; \
+ *(u8*)(r1 + 1) = r0; \
+ r0 = *(u8*)(r1 + 1); \
+ exit; \
+" ::: __clobber_all);
+}
+
SEC("socket")
__description("PTR_TO_STACK check low 3")
+__load_if_no_large_stack()
__failure __msg("invalid write to stack R1 off=-513 size=1")
__msg_unpriv("R1 stack pointer arithmetic goes out of range")
__naked void to_stack_check_low_3(void)
@@ -266,6 +286,23 @@ __naked void to_stack_check_low_3(void)
" ::: __clobber_all);
}
+SEC("socket")
+__description("PTR_TO_STACK check low 3, large stack")
+__load_if_large_stack()
+__failure __msg("invalid write to stack R1 off=-2049 size=1")
+__msg_unpriv("R1 stack pointer arithmetic goes out of range")
+__naked void to_stack_check_low_3_large(void)
+{
+ asm volatile (" \
+ r1 = r10; \
+ r1 += -2049; \
+ r0 = 42; \
+ *(u8*)(r1 + 0) = r0; \
+ r0 = *(u8*)(r1 + 0); \
+ exit; \
+" ::: __clobber_all);
+}
+
SEC("socket")
__description("PTR_TO_STACK check low 4")
__failure __msg("math between fp pointer")
@@ -483,6 +520,7 @@ l1_%=: r0 = 42; \
SEC("socket")
__description("PTR_TO_STACK stack size > 512")
+__load_if_no_large_stack()
__failure __msg("invalid write to stack R1 off=-520 size=8")
__naked void stack_check_size_gt_512(void)
{
@@ -495,6 +533,21 @@ __naked void stack_check_size_gt_512(void)
" ::: __clobber_all);
}
+SEC("socket")
+__description("PTR_TO_STACK stack size > 2048")
+__load_if_large_stack()
+__failure __msg("invalid write to stack R1 off=-2056 size=8")
+__naked void stack_check_size_gt_2048(void)
+{
+ asm volatile (" \
+ r1 = r10; \
+ r1 += -2056; \
+ r0 = 42; \
+ *(u64*)(r1 + 0) = r0; \
+ exit; \
+" ::: __clobber_all);
+}
+
#ifdef __BPF_FEATURE_MAY_GOTO
SEC("socket")
__description("PTR_TO_STACK stack size 512 with may_goto with jit")
diff --git a/tools/testing/selftests/bpf/progs/verifier_tailcall.c b/tools/testing/selftests/bpf/progs/verifier_tailcall.c
index b4acce60fb9b9..51687da972257 100644
--- a/tools/testing/selftests/bpf/progs/verifier_tailcall.c
+++ b/tools/testing/selftests/bpf/progs/verifier_tailcall.c
@@ -28,4 +28,61 @@ __naked void invalid_map_for_tail_call(void)
: __clobber_all);
}
+struct {
+ __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+ __uint(max_entries, 1);
+ __uint(key_size, sizeof(__u32));
+ __uint(value_size, sizeof(__u32));
+} jmp_table SEC(".maps");
+
+__used __naked
+static int subprog_tail_call(void)
+{
+ asm volatile (" \
+ r2 = %[jmp_table] ll; \
+ r3 = 0; \
+ call %[bpf_tail_call]; \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm(bpf_tail_call),
+ __imm_addr(jmp_table)
+ : __clobber_all);
+}
+
+/*
+ * A tail call unwinds only the frame of the subprog doing it, so the
+ * frames of its callers stay on the stack. With up to 33 tail calls in
+ * a chain the verifier caps the stack those frames may add up to at
+ * 256 bytes.
+ */
+SEC("tc")
+__description("tail call from subprog with 240 bytes of caller stack")
+__success
+__naked void tail_call_caller_stack_ok(void)
+{
+ asm volatile (" \
+ r2 = 42; \
+ *(u64 *)(r10 - 240) = r2; \
+ call subprog_tail_call; \
+ r0 = 0; \
+ exit; \
+" ::: __clobber_all);
+}
+
+SEC("tc")
+__description("tail call from subprog with 256 bytes of caller stack")
+__failure
+__msg("tail_calls are not allowed when call stack of previous frames is 256 bytes. Too large")
+__naked void tail_call_caller_stack_too_large(void)
+{
+ asm volatile (" \
+ r2 = 42; \
+ *(u64 *)(r10 - 256) = r2; \
+ call subprog_tail_call; \
+ r0 = 0; \
+ exit; \
+" ::: __clobber_all);
+}
+
char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/verifier_var_off.c b/tools/testing/selftests/bpf/progs/verifier_var_off.c
index a63e336750918..399884911ea53 100644
--- a/tools/testing/selftests/bpf/progs/verifier_var_off.c
+++ b/tools/testing/selftests/bpf/progs/verifier_var_off.c
@@ -406,6 +406,7 @@ __naked void zero_sized_access_max_out_of_bound(void)
SEC("lwt_in")
__description("indirect variable-offset stack access, min out of bound")
+__load_if_no_large_stack()
__failure __msg("invalid variable-offset read from stack R2")
__naked void access_min_out_of_bound(void)
{
@@ -433,6 +434,37 @@ __naked void access_min_out_of_bound(void)
: __clobber_all);
}
+SEC("lwt_in")
+__description("indirect variable-offset stack access, min out of bound, large stack")
+__load_if_large_stack()
+__failure __msg("invalid variable-offset read from stack R2")
+__naked void access_min_out_of_bound_large(void)
+{
+ asm volatile (" \
+ /* Fill the top 8 bytes of the stack */ \
+ r2 = 0; \
+ *(u64*)(r10 - 8) = r2; \
+ /* Get an unknown value */ \
+ r2 = *(u32*)(r1 + 0); \
+ /* Make it small and 4-byte aligned */ \
+ r2 &= 4; \
+ r2 -= 2052; \
+ /* \
+ * add it to fp. We now have either fp-2052 or fp-2048, but\
+ * we don't know which \
+ */ \
+ r2 += r10; \
+ /* dereference it indirectly */ \
+ r1 = %[map_hash_8b] ll; \
+ call %[bpf_map_lookup_elem]; \
+ r0 = 0; \
+ exit; \
+" :
+ : __imm(bpf_map_lookup_elem),
+ __imm_addr(map_hash_8b)
+ : __clobber_all);
+}
+
SEC("cgroup/skb")
__description("indirect variable-offset stack access, min_off < min_initialized")
__success
diff --git a/tools/testing/selftests/bpf/test_loader.c b/tools/testing/selftests/bpf/test_loader.c
index a6e3fcc1079c6..25eeb1c1248b3 100644
--- a/tools/testing/selftests/bpf/test_loader.c
+++ b/tools/testing/selftests/bpf/test_loader.c
@@ -45,6 +45,11 @@ enum load_mode {
NO_JITED = 1 << 1,
};
+enum stack_mode {
+ LARGE_STACK = 1 << 0,
+ SMALL_STACK = 1 << 1,
+};
+
struct test_subspec {
char *name;
char *description;
@@ -70,6 +75,7 @@ struct test_spec {
int mode_mask;
int arch_mask;
int load_mask;
+ int stack_mask;
int linear_sz;
const char *skip_reason;
bool prepare_priv;
@@ -425,6 +431,7 @@ static int parse_test_spec(struct test_loader *tester,
int err = 0;
u32 arch_mask = 0;
u32 load_mask = 0;
+ u32 stack_mask = 0;
struct btf *btf;
enum arch arch;
@@ -620,6 +627,16 @@ static int parse_test_spec(struct test_loader *tester,
err = -EINVAL;
goto cleanup;
}
+ } else if ((val = str_has_pfx(s, "stack_mode="))) {
+ if (strcmp(val, "large") == 0) {
+ stack_mask = LARGE_STACK;
+ } else if (strcmp(val, "small") == 0) {
+ stack_mask = SMALL_STACK;
+ } else {
+ PRINT_FAIL("bad stack spec: '%s'", val);
+ err = -EINVAL;
+ goto cleanup;
+ }
} else if ((msg = str_has_pfx(s, "test_expect_stderr="))) {
err = push_disasm_msg(msg, &stderr_on_next_line,
&spec->priv.stderr);
@@ -659,6 +676,7 @@ static int parse_test_spec(struct test_loader *tester,
spec->arch_mask = arch_mask ?: -1;
spec->load_mask = load_mask ?: (JITED | NO_JITED);
+ spec->stack_mask = stack_mask ?: (LARGE_STACK | SMALL_STACK);
if (spec->mode_mask == 0)
spec->mode_mask = PRIV;
@@ -1331,6 +1349,7 @@ void run_subtest(struct test_loader *tester,
{
struct test_subspec *subspec = unpriv ? &spec->unpriv : &spec->priv;
int current_runtime = is_jit_enabled() ? JITED : NO_JITED;
+ int current_stack = is_large_stack_supported() ? LARGE_STACK : SMALL_STACK;
struct bpf_program *tprog = NULL, *tprog_iter;
struct bpf_link *link, *links[32] = {};
struct test_spec *spec_iter;
@@ -1360,6 +1379,11 @@ void run_subtest(struct test_loader *tester,
return;
}
+ if ((current_stack & spec->stack_mask) == 0) {
+ test__skip();
+ return;
+ }
+
if (unpriv) {
if (!can_execute_unpriv(tester, spec)) {
test__skip();
diff --git a/tools/testing/selftests/bpf/testing_helpers.c b/tools/testing/selftests/bpf/testing_helpers.c
index d1d60451c5bcc..47fe61a1ebff1 100644
--- a/tools/testing/selftests/bpf/testing_helpers.c
+++ b/tools/testing/selftests/bpf/testing_helpers.c
@@ -517,6 +517,47 @@ bool is_jit_enabled(void)
return enabled;
}
+/*
+ * Whether the kernel accepts a program using more than 512 bytes of stack,
+ * which depends on the JIT in use. Probed once with a program that stores
+ * at the 2 KiB depth. Only the verifier's verdict on that store is cached:
+ * a load that fails for another reason, such as a missing capability, is
+ * reported and probed again on the next call.
+ */
+bool is_large_stack_supported(void)
+{
+ static int supported = -1;
+ struct bpf_insn insns[] = {
+ BPF_ST_MEM(BPF_DW, BPF_REG_10, -2048, 0),
+ BPF_MOV64_IMM(BPF_REG_0, 0),
+ BPF_EXIT_INSN(),
+ };
+ char log[1024] = {};
+ LIBBPF_OPTS(bpf_prog_load_opts, opts,
+ .log_buf = log,
+ .log_size = sizeof(log),
+ .log_level = 1,
+ );
+ int fd;
+
+ if (supported >= 0)
+ return supported;
+
+ fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", insns, ARRAY_SIZE(insns),
+ &opts);
+ if (fd >= 0) {
+ close(fd);
+ supported = 1;
+ } else if (strstr(log, "invalid write to stack")) {
+ supported = 0;
+ } else {
+ fprintf(stderr, "%s: probe failed with errno %d, assuming 512 bytes:\n%s",
+ __func__, errno, log);
+ return false;
+ }
+ return supported;
+}
+
int stack_mprotect(void)
{
void *buf;
diff --git a/tools/testing/selftests/bpf/testing_helpers.h b/tools/testing/selftests/bpf/testing_helpers.h
index 1c58a2f08b645..f1505108e26a4 100644
--- a/tools/testing/selftests/bpf/testing_helpers.h
+++ b/tools/testing/selftests/bpf/testing_helpers.h
@@ -59,6 +59,7 @@ struct bpf_insn;
int get_xlated_program(int fd_prog, struct bpf_insn **buf, __u32 *cnt);
int testing_prog_flags(void);
bool is_jit_enabled(void);
+bool is_large_stack_supported(void);
int stack_mprotect(void);
/* Runs diff(1) on mismatch */
diff --git a/tools/testing/selftests/bpf/verifier/calls.c b/tools/testing/selftests/bpf/verifier/calls.c
index 8b94b87135bcf..0af237c02ddf9 100644
--- a/tools/testing/selftests/bpf/verifier/calls.c
+++ b/tools/testing/selftests/bpf/verifier/calls.c
@@ -1037,15 +1037,34 @@
.result = ACCEPT,
},
{
- "calls: stack overflow using two frames (pre-call access)",
+ /*
+ * Five 480-byte frames exceed the 2 KiB budget of JITs with large
+ * stacks, and two of them the 512 bytes allowed elsewhere.
+ */
+ "calls: stack overflow using five frames (pre-call access)",
.insns = {
/* prog 1 */
- BPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1),
BPF_EXIT_INSN(),
/* prog 2 */
- BPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1),
+ BPF_EXIT_INSN(),
+
+ /* prog 3 */
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1),
+ BPF_EXIT_INSN(),
+
+ /* prog 4 */
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1),
+ BPF_EXIT_INSN(),
+
+ /* prog 5 */
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(),
},
@@ -1054,15 +1073,30 @@
.result = REJECT,
},
{
- "calls: stack overflow using two frames (post-call access)",
+ "calls: stack overflow using five frames (post-call access)",
.insns = {
/* prog 1 */
BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 2),
- BPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
BPF_EXIT_INSN(),
/* prog 2 */
- BPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 2),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
+ BPF_EXIT_INSN(),
+
+ /* prog 3 */
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 2),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
+ BPF_EXIT_INSN(),
+
+ /* prog 4 */
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 2),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
+ BPF_EXIT_INSN(),
+
+ /* prog 5 */
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(),
},
@@ -1127,7 +1161,7 @@
.result = ACCEPT,
},
{
- "calls: stack depth check using three frames. test3",
+ "calls: stack depth check using five frames. test3",
.insns = {
/* main */
BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
@@ -1135,66 +1169,104 @@
BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 8), /* call B */
BPF_JMP_IMM(BPF_JGE, BPF_REG_6, 0, 1),
- BPF_ST_MEM(BPF_B, BPF_REG_10, -64, 0),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(),
/* A */
BPF_JMP_IMM(BPF_JLT, BPF_REG_1, 10, 1),
BPF_EXIT_INSN(),
- BPF_ST_MEM(BPF_B, BPF_REG_10, -224, 0),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
BPF_JMP_IMM(BPF_JA, 0, 0, -3),
/* B */
BPF_JMP_IMM(BPF_JGT, BPF_REG_1, 2, 1),
- BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, -6), /* call A */
- BPF_ST_MEM(BPF_B, BPF_REG_10, -256, 0),
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 2), /* call C */
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
+ BPF_EXIT_INSN(),
+ /* C */
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 2), /* call D */
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
+ BPF_EXIT_INSN(),
+ /* D */
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, -12), /* call A */
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
BPF_EXIT_INSN(),
},
.prog_type = BPF_PROG_TYPE_XDP,
- /* stack_main=64, stack_A=224, stack_B=256
- * and max(main+A, main+A+B) > 512
+ /*
+ * every frame is 480 bytes, main+A = 960 > 512 and
+ * max(main+A, main+B+C+D+A) = 2400 > 2048
*/
.errstr = "combined stack",
.result = REJECT,
},
{
- "calls: stack depth check using three frames. test4",
- /* void main(void) {
+ "calls: stack depth check using five frames. test4",
+ /*
+ * void main(void) {
* func1(0);
* func1(1);
* func2(1);
* }
- * void func1(int alloc_or_recurse) {
+ * void funcN(int alloc_or_recurse) { N = 1..4
* if (alloc_or_recurse) {
- * frame_pointer[-300] = 1;
+ * frame_pointer[-480] = 1;
* } else {
- * func2(alloc_or_recurse);
+ * funcN+1(alloc_or_recurse);
* }
* }
- * void func2(int alloc_or_recurse) {
+ * void func5(int alloc_or_recurse) {
* if (alloc_or_recurse) {
- * frame_pointer[-300] = 1;
+ * frame_pointer[-480] = 1;
* }
* }
+ * main also calls func2 to func5 with 1 so that every function has a
+ * path allocating its 480 bytes, and the chain adds up to 2400 bytes,
+ * more than the 2 KiB budget of JITs with large stacks, and to 960
+ * bytes after two frames, more than the 512 bytes allowed elsewhere.
*/
.insns = {
/* main */
BPF_MOV64_IMM(BPF_REG_1, 0),
- BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 6), /* call A */
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 12), /* call A */
BPF_MOV64_IMM(BPF_REG_1, 1),
- BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 4), /* call A */
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 10), /* call A */
+ BPF_MOV64_IMM(BPF_REG_1, 1),
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 13), /* call B */
+ BPF_MOV64_IMM(BPF_REG_1, 1),
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 16), /* call C */
BPF_MOV64_IMM(BPF_REG_1, 1),
- BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 7), /* call B */
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 19), /* call D */
+ BPF_MOV64_IMM(BPF_REG_1, 1),
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 22), /* call E */
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(),
/* A */
BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 2),
- BPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
BPF_EXIT_INSN(),
BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call B */
BPF_EXIT_INSN(),
/* B */
+ BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 2),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
+ BPF_EXIT_INSN(),
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call C */
+ BPF_EXIT_INSN(),
+ /* C */
+ BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 2),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
+ BPF_EXIT_INSN(),
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call D */
+ BPF_EXIT_INSN(),
+ /* D */
+ BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 2),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
+ BPF_EXIT_INSN(),
+ BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call E */
+ BPF_EXIT_INSN(),
+ /* E */
BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 1),
- BPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),
+ BPF_ST_MEM(BPF_B, BPF_REG_10, -480, 0),
BPF_EXIT_INSN(),
},
.prog_type = BPF_PROG_TYPE_XDP,
|
| 33/3 | 2026/09/26 22:11 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 75889 output: 27 thoughts: 1690 Thoughts: **KMSAN Fuzzing Session Assessment**
Okay, so I need to decide if this BPF stack size patch series warrants a dedicated KMSAN fuzzing run. My initial gut feeling is that it's probably *not* necessary, as this patch seems to be about increasing a stack size limit and adapting various data structures for that. However, I need to be absolutely sure. KMSAN is resource-intensive, so I need to justify the request.
I've got the patch summary in front of me: it's about increasing the BPF stack size for some architectures with JIT support (x86_64 and arm64) from 512 bytes to 2048 bytes. A large part of this patch seems to involve adapting the BPF verifier to handle this larger stack. There are some changes in data structures, bitfield updates, helper functions, and dynamic sizing strategies related to the increased stack. I'm seeing changes in: `Documentation/`, `arch/`, `drivers/`, `include/`, `kernel/bpf/`, and selftests, which is a fairly wide spread across the kernel.
Now I need to focus on those KMSAN criteria. My goal here is to determine whether it introduces uninitialized memory risks. Let me go through the criteria:
1. **Kernel structures sent to user space:** I don't immediately see any *direct* indication of structures being copied to user space in a manner that's not fully initialized or zeroed. I see changes to `struct bpf_jmp_history_entry`, the stack masks, and more, but a quick scan doesn't scream *user-space exposure* of uninitialized values.
2. **Conditional logic depending on uninitialized variables:** I will need to really inspect the updated code for any branching or conditional logic that relies on variables that might be uninitialized. It seems we're primarily dealing with new stack management, so I *hope* there is a clean initialization in place.
3. **Allocation/initialization of complex data structures:** This is where I need to be especially careful. I see a lot of modifications and additions to data structures here. The patch includes new structures like `frame_masks` and `struct bpf_jmp_history_entry` and updates to existing structures like `struct backtrack_state`. I *must* verify that all of their fields are properly zeroed or initialized *before* they're used. Dynamic sizing is also a keyword here: I need to verify that those dynamically sized buffers are correctly initialized and that any potentially uninitialized values are correctly handled, which is likely where the risk would be.
4. **Bounds checks exposing uninitialized bytes:** The patch does touch on bounds checks, which is a warning sign. It modifies the verifier to use `env->stack_limit` instead of `MAX_BPF_STACK` and introduces helper functions like `bpf_stack_nr_slots` and `bpf_stack_slot` to manage stack slots. There are new checks against `env->stack_limit` and dynamic resizing of `bpf_idmap` and `bpf_idset` using `bpf_id_scratch_reserve`, so I need to check all calls to these helpers. There could be an off-by-one or other bounds check error that lets uninitialized memory get read.
Okay, let's zoom in on `bpf_jmp_history_entry`, as it looks like a good place to start my deep dive. I will grep for the allocations and uses of this struct: `jmp_history`, `cur_hist_ent`, `bpf_push_jmp_history` - and verify all of them.
If I can be absolutely certain that all of the newly introduced or modified structures are properly initialized (e.g., using `{0}`, `memset`, or `kzalloc`) *before* they are used, then the risk should be mitigated. I'll need to double-check that this is true, particularly for dynamically sized arrays, bitmap operations, and the `linked_regs` field. This is going to require careful code review. The same goes for any conditional logic that uses these structures. If I can't find clear evidence of uninitialized memory *that could be exposed*, then I can argue against the need for a KMSAN fuzzing session, and this patch won't expose any uninitialized memory risks.
|
| 34/3 | 2026/09/26 22:12 | tool | grepper |
0mArgs:{
"Expression": "jmp_history",
"PathPrefix": "kernel/bpf"
}Results: {
"Output": "kernel/bpf/backtrack.c-10-/* for any branch, call, exit record the history of jmps in the given state */\nkernel/bpf/backtrack.c:11:int bpf_push_jmp_history(struct bpf_verifier_env *env, struct bpf_verifier_state *cur,\nkernel/bpf/backtrack.c-12-\t\t\t int insn_flags, int spi, int frame, const u16 *linked_regs,\n--\nkernel/bpf/backtrack.c-14-{\nkernel/bpf/backtrack.c:15:\tu32 cnt = cur-\u003ejmp_history_cnt;\nkernel/bpf/backtrack.c:16:\tstruct bpf_jmp_history_entry *p;\nkernel/bpf/backtrack.c-17-\tsize_t alloc_size;\n--\nkernel/bpf/backtrack.c-42-\talloc_size = kmalloc_size_roundup(size_mul(cnt, sizeof(*p)));\nkernel/bpf/backtrack.c:43:\tp = krealloc(cur-\u003ejmp_history, alloc_size, GFP_KERNEL_ACCOUNT);\nkernel/bpf/backtrack.c-44-\tif (!p)\nkernel/bpf/backtrack.c-45-\t\treturn -ENOMEM;\nkernel/bpf/backtrack.c:46:\tcur-\u003ejmp_history = p;\nkernel/bpf/backtrack.c-47-\nkernel/bpf/backtrack.c:48:\tp = \u0026cur-\u003ejmp_history[cnt - 1];\nkernel/bpf/backtrack.c-49-\tp-\u003eidx = env-\u003einsn_idx;\n--\nkernel/bpf/backtrack.c-56-\tp-\u003elinked_regs_cnt = linked_regs_cnt;\nkernel/bpf/backtrack.c:57:\tcur-\u003ejmp_history_cnt = cnt;\nkernel/bpf/backtrack.c-58-\tenv-\u003ecur_hist_ent = p;\n--\nkernel/bpf/backtrack.c=90=static int get_prev_insn_idx(struct bpf_verifier_state *st, int i,\n--\nkernel/bpf/backtrack.c-97-\t\t\treturn -ENOENT;\nkernel/bpf/backtrack.c:98:\t\tif (cnt == 1 \u0026\u0026 st-\u003ejmp_history[0].idx == i)\nkernel/bpf/backtrack.c-99-\t\t\treturn -ENOENT;\n--\nkernel/bpf/backtrack.c-101-\nkernel/bpf/backtrack.c:102:\tif (cnt \u0026\u0026 st-\u003ejmp_history[cnt - 1].idx == i) {\nkernel/bpf/backtrack.c:103:\t\ti = st-\u003ejmp_history[cnt - 1].prev_idx;\nkernel/bpf/backtrack.c-104-\t\t(*history)--;\n--\nkernel/bpf/backtrack.c-110-\nkernel/bpf/backtrack.c:111:static struct bpf_jmp_history_entry *get_jmp_hist_entry(struct bpf_verifier_state *st,\nkernel/bpf/backtrack.c-112-\t\t\t\t\t\t u32 hist_end, int insn_idx)\nkernel/bpf/backtrack.c-113-{\nkernel/bpf/backtrack.c:114:\tif (hist_end \u003e 0 \u0026\u0026 st-\u003ejmp_history[hist_end - 1].idx == insn_idx)\nkernel/bpf/backtrack.c:115:\t\treturn \u0026st-\u003ejmp_history[hist_end - 1];\nkernel/bpf/backtrack.c-116-\treturn NULL;\n--\nkernel/bpf/backtrack.c=283=static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,\nkernel/bpf/backtrack.c:284:\t\t\t struct bpf_jmp_history_entry *hist, struct backtrack_state *bt)\nkernel/bpf/backtrack.c-285-{\n--\nkernel/bpf/backtrack.c=704=void bpf_mark_all_scalars_precise(struct bpf_verifier_env *env,\n--\nkernel/bpf/backtrack.c-759- * assembly instructions (see backtrack_insns() for exact instruction handling\nkernel/bpf/backtrack.c:760: * logic). This backtracking relies on recorded jmp_history and is able to\nkernel/bpf/backtrack.c-761- * traverse entire chain of parent states. This process ends only when all the\n--\nkernel/bpf/backtrack.c=840=int bpf_mark_chain_precision(struct bpf_verifier_env *env,\n--\nkernel/bpf/backtrack.c-881-\t\tDECLARE_BITMAP(mask, 64);\nkernel/bpf/backtrack.c:882:\t\tu32 history = st-\u003ejmp_history_cnt;\nkernel/bpf/backtrack.c:883:\t\tstruct bpf_jmp_history_entry *hist;\nkernel/bpf/backtrack.c-884-\n--\nkernel/bpf/states.c=1259=int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx)\n--\nkernel/bpf/states.c-1269-\t\t\t /* Avoid accumulating infinitely long jmp history */\nkernel/bpf/states.c:1270:\t\t\t cur-\u003ejmp_history_cnt \u003e 40;\nkernel/bpf/states.c-1271-\n--\nkernel/bpf/states.c-1436-\t\t\tif (bpf_is_jmp_point(env, env-\u003einsn_idx))\nkernel/bpf/states.c:1437:\t\t\t\terr = bpf_push_jmp_history(env, cur, 0, 0, 0, NULL, 0);\nkernel/bpf/states.c-1438-\t\t\terr = err ? : propagate_precision(env, \u0026sl-\u003estate, cur, NULL);\n--\nkernel/bpf/states.c-1617-\tcur-\u003edfs_depth = new-\u003edfs_depth + 1;\nkernel/bpf/states.c:1618:\tbpf_clear_jmp_history(cur);\nkernel/bpf/states.c-1619-\tlist_add(\u0026new_sl-\u003enode, head);\n--\nkernel/bpf/verifier.c=1670=static void free_func_state(struct bpf_func_state *state)\n--\nkernel/bpf/verifier.c-1678-\nkernel/bpf/verifier.c:1679:void bpf_clear_jmp_history(struct bpf_verifier_state *state)\nkernel/bpf/verifier.c-1680-{\nkernel/bpf/verifier.c:1681:\tkfree(state-\u003ejmp_history);\nkernel/bpf/verifier.c:1682:\tstate-\u003ejmp_history = NULL;\nkernel/bpf/verifier.c:1683:\tstate-\u003ejmp_history_cnt = 0;\nkernel/bpf/verifier.c-1684-}\n--\nkernel/bpf/verifier.c=1686=void bpf_free_verifier_state(struct bpf_verifier_state *state,\n--\nkernel/bpf/verifier.c-1695-\tkfree(state-\u003erefs);\nkernel/bpf/verifier.c:1696:\tbpf_clear_jmp_history(state);\nkernel/bpf/verifier.c-1697-\tif (free_self)\n--\nkernel/bpf/verifier.c=1713=int bpf_copy_verifier_state(struct bpf_verifier_state *dst_state,\n--\nkernel/bpf/verifier.c-1718-\nkernel/bpf/verifier.c:1719:\tdst_state-\u003ejmp_history = copy_array(dst_state-\u003ejmp_history, src-\u003ejmp_history,\nkernel/bpf/verifier.c:1720:\t\t\t\t\t src-\u003ejmp_history_cnt, sizeof(*dst_state-\u003ejmp_history),\nkernel/bpf/verifier.c-1721-\t\t\t\t\t GFP_KERNEL_ACCOUNT);\nkernel/bpf/verifier.c:1722:\tif (!dst_state-\u003ejmp_history)\nkernel/bpf/verifier.c-1723-\t\treturn -ENOMEM;\nkernel/bpf/verifier.c:1724:\tdst_state-\u003ejmp_history_cnt = src-\u003ejmp_history_cnt;\nkernel/bpf/verifier.c-1725-\n--\nkernel/bpf/verifier.c=3423=static void linked_regs_pack(const struct linked_regs *s, u16 *packed)\n--\nkernel/bpf/verifier.c-3433-\nkernel/bpf/verifier.c:3434:static void linked_regs_unpack(const struct bpf_jmp_history_entry *hist, struct linked_regs *s)\nkernel/bpf/verifier.c-3435-{\n--\nkernel/bpf/verifier.c=3467=void bpf_verbose_insn(struct bpf_verifier_env *env, struct bpf_insn *insn)\n--\nkernel/bpf/verifier.c-3480- */\nkernel/bpf/verifier.c:3481:void bpf_bt_sync_linked_regs(struct backtrack_state *bt, struct bpf_jmp_history_entry *hist)\nkernel/bpf/verifier.c-3482-{\n--\nkernel/bpf/verifier.c=3654=static int check_stack_write_fixed_off(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-3775-\tif (insn_flags)\nkernel/bpf/verifier.c:3776:\t\treturn bpf_push_jmp_history(env, env-\u003ecur_state, insn_flags,\nkernel/bpf/verifier.c-3777-\t\t\t\t\t hist_spi, hist_frame, NULL, 0);\n--\nkernel/bpf/verifier.c=4007=static int check_stack_read_fixed_off(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-4158-\tif (insn_flags)\nkernel/bpf/verifier.c:4159:\t\treturn bpf_push_jmp_history(env, env-\u003ecur_state, insn_flags,\nkernel/bpf/verifier.c-4160-\t\t\t\t\t hist_spi, hist_frame, NULL, 0);\n--\nkernel/bpf/verifier.c=4314=static int check_stack_arg_write(struct bpf_verifier_env *env, struct bpf_func_state *state,\n--\nkernel/bpf/verifier.c-4348-\tstate-\u003eno_stack_arg_load = true;\nkernel/bpf/verifier.c:4349:\treturn bpf_push_jmp_history(env, env-\u003ecur_state,\nkernel/bpf/verifier.c-4350-\t\t\t\t INSN_F_STACK_ARG_ACCESS, spi, 0, NULL, 0);\n--\nkernel/bpf/verifier.c=4357=static int check_stack_arg_read(struct bpf_verifier_env *env, struct bpf_func_state *state,\n--\nkernel/bpf/verifier.c-4382-\tbpf_diag_mod_end(env);\nkernel/bpf/verifier.c:4383:\treturn bpf_push_jmp_history(env, env-\u003ecur_state,\nkernel/bpf/verifier.c-4384-\t\t\t\t INSN_F_STACK_ARG_ACCESS, spi, 0, NULL, 0);\n--\nkernel/bpf/verifier.c=17862=static int check_cond_jmp_op(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-17935-\tif (insn_flags) {\nkernel/bpf/verifier.c:17936:\t\terr = bpf_push_jmp_history(env, this_branch, insn_flags, 0, 0, NULL, 0);\nkernel/bpf/verifier.c-17937-\t\tif (err)\n--\nkernel/bpf/verifier.c-18008-\t\tlinked_regs_pack(\u0026linked_regs, packed);\nkernel/bpf/verifier.c:18009:\t\terr = bpf_push_jmp_history(env, this_branch, 0, 0, 0, packed, linked_regs.cnt);\nkernel/bpf/verifier.c-18010-\t\tif (err)\n--\nkernel/bpf/verifier.c=19340=static int do_check(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-19419-\t\tif (bpf_is_jmp_point(env, env-\u003einsn_idx)) {\nkernel/bpf/verifier.c:19420:\t\t\terr = bpf_push_jmp_history(env, state, 0, 0, 0, NULL, 0);\nkernel/bpf/verifier.c-19421-\t\t\tif (err)\n"
} |
| 35/3 | 2026/09/26 22:12 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 81891 output: 36 thoughts: 51 Thoughts: Here's my analysis of the provided code snippet from `kernel/bpf/backtrack.c`, presented as a first-person expert perspective: **Diving into `bpf_push_jmp_history`** Alright, let's zero in on `bpf_push_jmp_history` within `kernel/bpf/backtrack.c`. My immediate focus will be on lines 15 through 65. I'm already anticipating that this function probably deals with tracking the history of jumps or branches within a BPF program – crucial for backtracing and potentially for optimization or analysis. I'm expecting this section to outline how the jump history is stored, updated, and potentially managed, perhaps with considerations for things like stack size, potential overflows, or even subtle aspects of memory management given the kernel context. I will carefully consider the data structures and how they interrelate. I'll be looking for any hints about potential performance bottlenecks or subtle vulnerabilities in how jump history is handled. Finally, I'll be particularly interested in any error-handling mechanisms and what kind of checks are performed to ensure the integrity of the tracking process. |
| 36/3 | 2026/09/26 22:12 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/backtrack.c",
"FirstLine": 15,
"LineCount": 55
}Results: {
"Contents": " 15:\t\tu32 cnt = cur-\u003ejmp_history_cnt;\n 16:\t\tstruct bpf_jmp_history_entry *p;\n 17:\t\tsize_t alloc_size;\n 18:\t\n 19:\t\t/* combine instruction flags if we already recorded this instruction */\n 20:\t\tif (env-\u003ecur_hist_ent) {\n 21:\t\t\t/* atomic instructions push insn_flags twice, for READ and\n 22:\t\t\t * WRITE sides, but they should agree on stack slot\n 23:\t\t\t */\n 24:\t\t\tverifier_bug_if((env-\u003ecur_hist_ent-\u003eflags \u0026 insn_flags) \u0026\u0026\n 25:\t\t\t\t\t(env-\u003ecur_hist_ent-\u003eflags \u0026 insn_flags) != insn_flags,\n 26:\t\t\t\t\tenv, \"insn history: insn_idx %d cur flags %x new flags %x\",\n 27:\t\t\t\t\tenv-\u003einsn_idx, env-\u003ecur_hist_ent-\u003eflags, insn_flags);\n 28:\t\t\tenv-\u003ecur_hist_ent-\u003eflags |= insn_flags;\n 29:\t\t\tenv-\u003ecur_hist_ent-\u003espi = spi;\n 30:\t\t\tenv-\u003ecur_hist_ent-\u003eframe = frame;\n 31:\t\t\tverifier_bug_if(env-\u003ecur_hist_ent-\u003elinked_regs_cnt != 0, env,\n 32:\t\t\t\t\t\"insn history: insn_idx %d has %u linked regs\",\n 33:\t\t\t\t\tenv-\u003einsn_idx, env-\u003ecur_hist_ent-\u003elinked_regs_cnt);\n 34:\t\t\tif (linked_regs_cnt)\n 35:\t\t\t\tmemcpy(env-\u003ecur_hist_ent-\u003elinked_regs, linked_regs,\n 36:\t\t\t\t linked_regs_cnt * sizeof(*linked_regs));\n 37:\t\t\tenv-\u003ecur_hist_ent-\u003elinked_regs_cnt = linked_regs_cnt;\n 38:\t\t\treturn 0;\n 39:\t\t}\n 40:\t\n 41:\t\tcnt++;\n 42:\t\talloc_size = kmalloc_size_roundup(size_mul(cnt, sizeof(*p)));\n 43:\t\tp = krealloc(cur-\u003ejmp_history, alloc_size, GFP_KERNEL_ACCOUNT);\n 44:\t\tif (!p)\n 45:\t\t\treturn -ENOMEM;\n 46:\t\tcur-\u003ejmp_history = p;\n 47:\t\n 48:\t\tp = \u0026cur-\u003ejmp_history[cnt - 1];\n 49:\t\tp-\u003eidx = env-\u003einsn_idx;\n 50:\t\tp-\u003eprev_idx = env-\u003eprev_insn_idx;\n 51:\t\tp-\u003eflags = insn_flags;\n 52:\t\tp-\u003espi = spi;\n 53:\t\tp-\u003eframe = frame;\n 54:\t\tif (linked_regs_cnt)\n 55:\t\t\tmemcpy(p-\u003elinked_regs, linked_regs, linked_regs_cnt * sizeof(*linked_regs));\n 56:\t\tp-\u003elinked_regs_cnt = linked_regs_cnt;\n 57:\t\tcur-\u003ejmp_history_cnt = cnt;\n 58:\t\tenv-\u003ecur_hist_ent = p;\n 59:\t\n 60:\t\treturn 0;\n 61:\t}\n 62:\t\n 63:\tstatic bool is_atomic_load_insn(const struct bpf_insn *insn)\n 64:\t{\n 65:\t\treturn BPF_CLASS(insn-\u003ecode) == BPF_STX \u0026\u0026\n 66:\t\t BPF_MODE(insn-\u003ecode) == BPF_ATOMIC \u0026\u0026\n 67:\t\t insn-\u003eimm == BPF_LOAD_ACQ;\n 68:\t}\n 69:\t\n"
} |
| 37/3 | 2026/09/26 22:12 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 83163 output: 28 thoughts: 1898 Thoughts: **Analyzing Potential Uninitialized Memory Reads in BPF Verifier** Okay, let's break this down. First, I'm looking at `bpf_push_jmp_history`, specifically focusing on how the `jmp_history` is handled. The code uses `krealloc` which immediately raises a flag since it doesn't guarantee zero initialization like `krealloc_array` with `__GFP_ZERO` or `kzalloc` would. I need to be certain that all allocated fields are indeed initialized before use. The structure `bpf_jmp_history_entry` contains several fields. I can see the code populating: `idx`, `prev_idx`, `flags`, `spi`, `frame`, and, conditionally, `linked_regs`. It also updates `linked_regs_cnt`. A potential problem arises when `linked_regs_cnt` is less than `BPF_LINKED_REGS_MAX` because the code might be reading beyond the valid range. A quick check confirms that `linked_regs_unpack` correctly only unpacks up to `hist->linked_regs_cnt`, which is good. Before the current patch, `linked_regs` was a direct assignment, which means there was no risk of uninitialized memory if used correctly. I'm now focusing on `env->cur_hist_ent`. Before the `memcpy` and update to `linked_regs_cnt`, the code explicitly checks `verifier_bug_if(env->cur_hist_ent->linked_regs_cnt != 0, ...)`. That means that the current entry will always be empty (initialized to zero by krealloc). So the old value of `env->cur_hist_ent->linked_regs_cnt` doesn't matter, and the check verifies that the existing entry's `linked_regs_cnt` is zero. In the current implementation the fields were previously set. Crucially, I confirm that *every* field of the `bpf_jmp_history_entry` that's ever *read* is correctly initialized *before* it's accessed, including `linked_regs_cnt`, even if `krealloc` isn't zeroing. The logic seems safe. I also confirm that `jmp_history` is *not* copied to userspace; it's internal to the verifier, so any uninitialized reads wouldn't leak information. The copies within `bpf_copy_verifier_state` uses `copy_array` and copies up to `src->jmp_history_cnt`, so that's handled. `bpf_clear_jmp_history` correctly frees it. Now I move on to `bpf_id_scratch_reserve`. Again, `krealloc_array` is used without `__GFP_ZERO`. I have to verify that all written-to fields are initialized. `bpf_id_scratch_reserve` is called by `check_ids` and `idstack_push`. In `check_ids`, the code *does* explicitly initialize `.old` and `.cur` of `struct bpf_id_pair` after reservation, and I verify that `struct bpf_id_pair` doesn't have any padding. The access logic only loops up to `idmap->cnt`, which is updated after initialization. In the case of `idstack_push`, the code correctly pushes the `old` field. But, `idmap->map[idmap->cnt].cur` *isn't* initialized explicitly in `idstack_push`! It looks like there's a potential use-after-free or read-of-uninitialized-memory condition here. Let's trace how `idstack_push` is used and how those values are consumed, and where/if `cur` is read. I need to examine how `idstack_push` is used to determine if the lack of initialization of `cur` is problematic, which it *looks* like it is. I need to see if it is ever read. |
| 38/3 | 2026/09/26 22:12 | tool | grepper |
0mArgs:{
"Expression": "idstack_push",
"PathPrefix": "kernel/bpf"
}Results: {
"Output": "kernel/bpf/verifier.c=10494=static int release_reference_nomark(struct bpf_verifier_env *env, int id)\n--\nkernel/bpf/verifier.c-10503-\nkernel/bpf/verifier.c:10504:static int idstack_push(struct bpf_idmap *idmap, u32 id)\nkernel/bpf/verifier.c-10505-{\n--\nkernel/bpf/verifier.c=10532=static int release_reference(struct bpf_verifier_env *env, int id)\n--\nkernel/bpf/verifier.c-10542-\tidstack-\u003ecnt = 0;\nkernel/bpf/verifier.c:10543:\terr = idstack_push(idstack, id);\nkernel/bpf/verifier.c-10544-\tif (err)\n--\nkernel/bpf/verifier.c-10572-\t\t\tif (reg-\u003eparent_id == id) {\nkernel/bpf/verifier.c:10573:\t\t\t\terr = idstack_push(idstack, reg-\u003eid);\nkernel/bpf/verifier.c-10574-\t\t\t\tif (err)\n"
} |
| 39/3 | 2026/09/26 22:12 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 86263 output: 38 thoughts: 35 Thoughts: Okay, let's dive into this. Here's my thought process on the matter: **Analyzing the BPF Verifier in `kernel/bpf/verifier.c`** Alright, I need to understand the behavior of the BPF verifier in a specific section. Let me check the code located around line 10530 to 10580 in `kernel/bpf/verifier.c`. I have a feeling that `release_reference` is going to be crucial in this section, I need to see what it's doing there and how the verifier utilizes it. I suspect this part of the code likely handles the process of releasing a reference or resource within the BPF execution environment, which could be critical for memory management, preventing leaks, or managing state within the BPF program. I'll focus on the context surrounding `release_reference` and try to understand the logic flow, specifically what variables and data structures are being affected by it. I will keep an eye on how it contributes to the overall safety and correctness of BPF program execution. |
| 40/3 | 2026/09/26 22:12 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/verifier.c",
"FirstLine": 10530,
"LineCount": 60
}Results: {
"Contents": "10530:\t\n10531:\t/* Release id and objects derived from it iteratively in a DFS manner */\n10532:\tstatic int release_reference(struct bpf_verifier_env *env, int id)\n10533:\t{\n10534:\t\tu32 mask = (1 \u003c\u003c STACK_SPILL) | (1 \u003c\u003c STACK_DYNPTR);\n10535:\t\tstruct bpf_verifier_state *vstate = env-\u003ecur_state;\n10536:\t\tstruct bpf_idmap *idstack = \u0026env-\u003eidmap_scratch;\n10537:\t\tstruct bpf_stack_state *stack;\n10538:\t\tstruct bpf_func_state *state;\n10539:\t\tstruct bpf_reg_state *reg;\n10540:\t\tint i, err;\n10541:\t\n10542:\t\tidstack-\u003ecnt = 0;\n10543:\t\terr = idstack_push(idstack, id);\n10544:\t\tif (err)\n10545:\t\t\treturn err;\n10546:\t\n10547:\t\tif (find_reference_state(vstate, id)) {\n10548:\t\t\terr = release_reference_nomark(env, id);\n10549:\t\t\tWARN_ON_ONCE(err);\n10550:\t\t}\n10551:\t\n10552:\t\twhile ((id = idstack_pop(idstack))) {\n10553:\t\t\t/*\n10554:\t\t\t * Child references are inaccessible after parent is released,\n10555:\t\t\t * any child references that exist at this point are a leak.\n10556:\t\t\t */\n10557:\t\t\tfor (i = 0; i \u003c vstate-\u003eacquired_refs; i++) {\n10558:\t\t\t\tif (vstate-\u003erefs[i].type != REF_TYPE_PTR)\n10559:\t\t\t\t\tcontinue;\n10560:\t\t\t\tif (vstate-\u003erefs[i].parent_id != id)\n10561:\t\t\t\t\tcontinue;\n10562:\t\t\t\tverbose(env, \"Leaking reference id=%d alloc_insn=%d. Release it first.\\n\",\n10563:\t\t\t\t\tvstate-\u003erefs[i].id, vstate-\u003erefs[i].insn_idx);\n10564:\t\t\t\treturn -EINVAL;\n10565:\t\t\t}\n10566:\t\n10567:\t\t\tbpf_for_each_reg_in_vstate_mask(vstate, state, reg, stack, mask, ({\n10568:\t\t\t\tif (reg-\u003eid != id \u0026\u0026 reg-\u003eparent_id != id)\n10569:\t\t\t\t\tcontinue;\n10570:\t\n10571:\t\t\t\t/* Free objects derived from the current object */\n10572:\t\t\t\tif (reg-\u003eparent_id == id) {\n10573:\t\t\t\t\terr = idstack_push(idstack, reg-\u003eid);\n10574:\t\t\t\t\tif (err)\n10575:\t\t\t\t\t\treturn err;\n10576:\t\t\t\t}\n10577:\t\n10578:\t\t\t\t/*\n10579:\t\t\t\t * A dynptr occupies two stack slots that invalidate_dynptr()\n10580:\t\t\t\t * clears together. Record both scrubs before invalidating it.\n10581:\t\t\t\t */\n10582:\t\t\t\tif (stack \u0026\u0026 stack-\u003eslot_type[BPF_REG_SIZE - 1] == STACK_DYNPTR) {\n10583:\t\t\t\t\tstruct bpf_stack_state *dyn_stack = stack;\n10584:\t\n10585:\t\t\t\t\tif (reg-\u003edynptr.first_slot)\n10586:\t\t\t\t\t\tdyn_stack--;\n10587:\t\t\t\t\tbpf_diag_record_scrub(env, \u0026dyn_stack[0].spilled_ptr,\n10588:\t\t\t\t\t\t\t BPF_DIAG_MOD_REF_RELEASE);\n10589:\t\t\t\t\tbpf_diag_record_scrub(env, \u0026dyn_stack[1].spilled_ptr,\n"
} |
| 41/3 | 2026/09/26 22:12 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 87628 output: 28 thoughts: 22 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience, and delivered as a first-person summary: **Analyzing the `idstack_pop` Function** Alright, let's dive in. My immediate focus is on the `idstack_pop` function. My instinct is to quickly assess its core function. I'm already formulating hypotheses about its role within the larger system. I'm anticipating that it's designed to remove an identifier, likely from a stack data structure. My thought process is now immediately probing for the context; what system and data type am I working with? Is this related to memory management? Scoping? Object lifetimes? The implications of a "pop" operation are central, of course, and I'm instantly considering potential side effects, like underflow checks and error handling. I'm probably looking at the adjacent code to understand how this function fits into the overall scheme. My primary goal is to understand precisely what this `idstack_pop` achieves and its interactions within the greater architecture. |
| 42/3 | 2026/09/26 22:12 | tool | grepper |
0mArgs:{
"Expression": "idstack_pop",
"PathPrefix": "kernel/bpf"
}Results: {
"Output": "kernel/bpf/verifier.c=10504=static int idstack_push(struct bpf_idmap *idmap, u32 id)\n--\nkernel/bpf/verifier.c-10522-\nkernel/bpf/verifier.c:10523:static int idstack_pop(struct bpf_idmap *idmap)\nkernel/bpf/verifier.c-10524-{\n--\nkernel/bpf/verifier.c=10532=static int release_reference(struct bpf_verifier_env *env, int id)\n--\nkernel/bpf/verifier.c-10551-\nkernel/bpf/verifier.c:10552:\twhile ((id = idstack_pop(idstack))) {\nkernel/bpf/verifier.c-10553-\t\t/*\n"
} |
| 43/3 | 2026/09/26 22:12 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 88115 output: 38 thoughts: 34 Thoughts: Okay, let's dive into this. I need to understand what's happening with `idstack_pop`, specifically lines 10520-10530 within `kernel/bpf/verifier.c`. This is likely a critical part of the BPF verifier, and I need to refresh my memory on its exact function within that context. I anticipate this is part of a stack-based argument-handling procedure, perhaps dealing with how the verifier tracks and manages IDs during analysis. I expect to see operations related to popping elements off a stack structure, and likely some error checking and bookkeeping. This could include checking for underflow conditions, updating internal state, or perhaps releasing resources related to the popped ID. Let's see what the code reveals. Time to go line by line and mentally reconstruct the flow. I know this area, so I anticipate I will be able to get through this with relative ease. |
| 44/3 | 2026/09/26 22:12 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/verifier.c",
"FirstLine": 10520,
"LineCount": 10
}Results: {
"Contents": "10520:\t\treturn 0;\n10521:\t}\n10522:\t\n10523:\tstatic int idstack_pop(struct bpf_idmap *idmap)\n10524:\t{\n10525:\t\tif (!idmap-\u003ecnt)\n10526:\t\t\treturn 0;\n10527:\t\n10528:\t\treturn idmap-\u003emap[--idmap-\u003ecnt].old;\n10529:\t}\n"
} |
| 45/3 | 2026/09/26 22:12 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 88522 output: 27 thoughts: 1412 Thoughts: **Analyzing Potential Uninitialized Memory Leaks in BPF** Okay, let's break this down systematically. I'm focusing on potential uninitialized memory reads or leaks within the BPF subsystem, especially considering the stack size increase. First, I examined several code paths. `idstack_pop` only reads from `.old`, which is initialized in `idstack_push`. In `idset_cnt_inc`, both the `id` and `cnt` fields of `struct bpf_idset_entry` are explicitly initialized when allocated. Then, I looked at `kernel/bpf/liveness.c`. In `widen_frame_masks`, `kvzalloc_flex` is used which guarantees zero-initialization of the newly allocated `new` memory. The existing `old` data is then copied to this zeroed memory and the remaining words are already zero. Next, I reviewed `compute_subprog_args`. Here, `at_stack_in` is fully initialized with `unvisited` values within a nested loop, covering the entire allocated space. `at_stack_out` is then initialized from the initialized `at_stack_in` via `memcpy`. `callsite_at_stack`, allocates memory with `kvmalloc_flex` and then also uses `memcpy` to copy the initialized data from the appropriate section of `at_stack_in`. There should be no uninitialized memory here either. I also checked `struct spill_snapshot`, but confirmed that `callsite_at_stack` is verifier-internal and not exposed to user space. Okay, now for the crux of the issue: the BPF stack itself. I need to consider the impact of the stack size increase from 512 to 2048 bytes on the BPF stack inside the verified BPF programs. The BPF stack is kernel stack or a private stack. When a BPF program runs, the stack pointer `R10` points to it. On x86-64, the JIT code (in `arch/x86/net/bpf_jit_comp.c`) typically allocates the stack with something like `push rbp; mov rbp, rsp; sub rsp, round_up(stack_depth, 8);`. This sets up the stack frame for the BPF program within the kernel's context. The critical question is this: Does the BPF verifier permit reading uninitialized memory from this stack? I need to rigorously examine the verifier's code. My next step is to search for keywords like `STACK_INVALID`, `allow_uninit_stack`, `check_stack_read`, and to specifically look for sections that deal with uninitialized stack reads within the verifier. The verifier *must* be preventing reads from uninitialized stack memory, otherwise the stack size increase would indeed be a security risk. I need to be sure! |
| 46/3 | 2026/09/26 22:12 | tool | grepper |
0mArgs:{
"Expression": "STACK_INVALID",
"PathPrefix": "kernel/bpf"
}Results: {
"Output": "kernel/bpf/log.c=497=static char slot_type_char[] = {\nkernel/bpf/log.c:498:\t[STACK_INVALID]\t= '?',\nkernel/bpf/log.c-499-\t[STACK_SPILL]\t= 'r',\n--\nkernel/bpf/log.c=700=void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_verifier_state *vstate,\n--\nkernel/bpf/log.c-731-\t\t\tslot_type = slot-\u003eslot_type[j];\nkernel/bpf/log.c:732:\t\t\tif (slot_type != STACK_INVALID \u0026\u0026 slot_type != STACK_POISON)\nkernel/bpf/log.c-733-\t\t\t\tvalid = true;\n--\nkernel/bpf/states.c=668=static bool is_stack_misc_after(struct bpf_verifier_env *env,\n--\nkernel/bpf/states.c-674-\t\tif ((stack-\u003eslot_type[i] == STACK_MISC) ||\nkernel/bpf/states.c:675:\t\t ((stack-\u003eslot_type[i] == STACK_INVALID || stack-\u003eslot_type[i] == STACK_POISON) \u0026\u0026\nkernel/bpf/states.c-676-\t\t env-\u003eallow_uninit_stack))\n--\nkernel/bpf/states.c=696=static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,\n--\nkernel/bpf/states.c-717-\t\tif (exact == EXACT) {\nkernel/bpf/states.c:718:\t\t\tu8 cur_type = cur_slot ? cur_slot-\u003eslot_type[im] : STACK_INVALID;\nkernel/bpf/states.c-719-\nkernel/bpf/states.c:720:\t\t\t/* STACK_INVALID and STACK_POISON are equivalent for pruning */\nkernel/bpf/states.c-721-\t\t\tif (old_type == STACK_POISON)\nkernel/bpf/states.c:722:\t\t\t\told_type = STACK_INVALID;\nkernel/bpf/states.c-723-\t\t\tif (cur_type == STACK_POISON)\nkernel/bpf/states.c:724:\t\t\t\tcur_type = STACK_INVALID;\nkernel/bpf/states.c-725-\t\t\tif (!cur_slot || old_type != cur_type)\n--\nkernel/bpf/states.c-728-\nkernel/bpf/states.c:729:\t\tif (old_type == STACK_INVALID || old_type == STACK_POISON)\nkernel/bpf/states.c-730-\t\t\tcontinue;\n--\nkernel/bpf/states.c-824-\t\tcase STACK_ZERO:\nkernel/bpf/states.c:825:\t\tcase STACK_INVALID:\nkernel/bpf/states.c-826-\t\tcase STACK_POISON:\n--\nkernel/bpf/verifier.c=795=static void invalidate_dynptr(struct bpf_verifier_env *env, struct bpf_stack_state *stack)\n--\nkernel/bpf/verifier.c-799-\tfor (i = 0; i \u003c BPF_REG_SIZE; i++) {\nkernel/bpf/verifier.c:800:\t\tstack[0].slot_type[i] = STACK_INVALID;\nkernel/bpf/verifier.c:801:\t\tstack[1].slot_type[i] = STACK_INVALID;\nkernel/bpf/verifier.c-802-\t}\n--\nkernel/bpf/verifier.c=1040=static int unmark_stack_slots_iter(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-1063-\t\tfor (j = 0; j \u003c BPF_REG_SIZE; j++)\nkernel/bpf/verifier.c:1064:\t\t\tslot-\u003eslot_type[j] = STACK_INVALID;\nkernel/bpf/verifier.c-1065-\n--\nkernel/bpf/verifier.c=1167=static int unmark_stack_slot_irq_flag(struct bpf_verifier_env *env, struct bpf_reg_state *reg,\n--\nkernel/bpf/verifier.c-1225-\tfor (i = 0; i \u003c BPF_REG_SIZE; i++)\nkernel/bpf/verifier.c:1226:\t\tslot-\u003eslot_type[i] = STACK_INVALID;\nkernel/bpf/verifier.c-1227-\n--\nkernel/bpf/verifier.c=1285=static bool is_stack_slot_special(const struct bpf_stack_state *stack)\n--\nkernel/bpf/verifier.c-1294-\t\treturn true;\nkernel/bpf/verifier.c:1295:\tcase STACK_INVALID:\nkernel/bpf/verifier.c-1296-\tcase STACK_POISON:\n--\nkernel/bpf/verifier.c-1311- * Mark stack slot as STACK_MISC, unless it is already:\nkernel/bpf/verifier.c:1312: * - STACK_INVALID, in which case they are equivalent.\nkernel/bpf/verifier.c-1313- * - STACK_ZERO, in which case we preserve more precise STACK_ZERO.\n--\nkernel/bpf/verifier.c-1315- * Regardless of allow_ptr_leaks setting (i.e., privileged or unprivileged\nkernel/bpf/verifier.c:1316: * mode), we won't promote STACK_INVALID to STACK_MISC. In privileged case it is\nkernel/bpf/verifier.c-1317- * unnecessary as both are considered equivalent when loading data and pruning,\n--\nkernel/bpf/verifier.c=1321=static void mark_stack_slot_misc(struct bpf_verifier_env *env, u8 *stype)\n--\nkernel/bpf/verifier.c-1324-\t\treturn;\nkernel/bpf/verifier.c:1325:\tif (*stype == STACK_INVALID || *stype == STACK_POISON)\nkernel/bpf/verifier.c-1326-\t\treturn;\n--\nkernel/bpf/verifier.c=1330=static void scrub_spilled_slot(u8 *stype)\nkernel/bpf/verifier.c-1331-{\nkernel/bpf/verifier.c:1332:\tif (*stype != STACK_INVALID \u0026\u0026 *stype != STACK_POISON)\nkernel/bpf/verifier.c-1333-\t\t*stype = STACK_MISC;\n--\nkernel/bpf/verifier.c=3799=static int check_stack_write_var_off(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-3853-\t\t\t *\nkernel/bpf/verifier.c:3854:\t\t\t * We do however catch STACK_INVALID case below, and\nkernel/bpf/verifier.c-3855-\t\t\t * only allow reading possibly uninitialized memory\n--\nkernel/bpf/verifier.c-3889-\t\t}\nkernel/bpf/verifier.c:3890:\t\t/* If the slot is STACK_INVALID, we check whether it's OK to\nkernel/bpf/verifier.c-3891-\t\t * pretend that it will be initialized by this write. The slot\n--\nkernel/bpf/verifier.c-3898-\t\t */\nkernel/bpf/verifier.c:3899:\t\tif ((*stype == STACK_INVALID || *stype == STACK_POISON) \u0026\u0026\nkernel/bpf/verifier.c-3900-\t\t !env-\u003eallow_uninit_stack) {\n--\nkernel/bpf/verifier.c=4007=static int check_stack_read_fixed_off(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-4079-\t\t\t\t\t}\nkernel/bpf/verifier.c:4080:\t\t\t\t\tif (type == STACK_INVALID \u0026\u0026 env-\u003eallow_uninit_stack)\nkernel/bpf/verifier.c-4081-\t\t\t\t\t\tcontinue;\n--\nkernel/bpf/verifier.c-4138-\t\t\t\tcontinue;\nkernel/bpf/verifier.c:4139:\t\t\tif (type == STACK_INVALID \u0026\u0026 env-\u003eallow_uninit_stack)\nkernel/bpf/verifier.c-4140-\t\t\t\tcontinue;\n--\nkernel/bpf/verifier.c=7351=static int check_stack_range_initialized(\n--\nkernel/bpf/verifier.c-7433-\t\tif ((*stype == STACK_ZERO) ||\nkernel/bpf/verifier.c:7434:\t\t (*stype == STACK_INVALID \u0026\u0026 (uninit || env-\u003eallow_uninit_stack))) {\nkernel/bpf/verifier.c:7435:\t\t\tif (clobber \u0026\u0026 (*stype != STACK_INVALID || env-\u003eallow_uninit_stack)) {\nkernel/bpf/verifier.c-7436-\t\t\t\t/* helper can write anything into the stack */\n"
} |
| 47/3 | 2026/09/26 22:12 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 92919 output: 37 thoughts: 191 Thoughts: **Analyzing Stack Bounds in BPF Verification**
Okay, let's break this down. First, I'm examining this `check_stack_slot_within_bounds` function. My initial question is: how is this determining if a stack access is valid? I see the code is checking offsets within a BPF program. Specifically, this block here:
```c
static int check_stack_slot_within_bounds(struct bpf_verifier_env *env,
struct bpf_func_state *state,
int off,
enum bpf_access_type t)
{
int min_valid_off;
if (t == BPF_WRITE || env->allow_uninit_stack)
min_valid_off = -(int)env->stack_limit;
else
min_valid_off = -state->allocated_stack;
...
```
I note the calculation of `min_valid_off`. It seems to change based on `BPF_WRITE` access or if uninitialized stack accesses are allowed. *That's interesting*... it's differentiating between read/write and potentially uninitialized reads. Specifically, in the write case and if uninitialized stack accesses are allowed, it's checking against `env->stack_limit`. However, for reads (assuming `!BPF_WRITE` and `!env->allow_uninit_stack`), it uses `state->allocated_stack`.
Now, I need to understand how stack allocation actually *happens*. This makes me think I should investigate `grow_stack_state()` to see how that affects these values. I need to read the code for `grow_stack_state` to get a better understanding of the overall stack allocation process. I should go to `kernel/bpf/verifier.c` and check that function out. Let's see how that works.
|
| 48/3 | 2026/09/26 22:12 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/verifier.c",
"FirstLine": 1445,
"LineCount": 45
}Results: {
"Contents": "1445:\t\treturn 0;\n1446:\t}\n1447:\t\n1448:\t/* Possibly update state-\u003eallocated_stack to be at least size bytes. Also\n1449:\t * possibly update the function's high-water mark in its bpf_subprog_info.\n1450:\t */\n1451:\tstatic int grow_stack_state(struct bpf_verifier_env *env, struct bpf_func_state *state, int size)\n1452:\t{\n1453:\t\tsize_t old_n = bpf_stack_nr_slots(state), n;\n1454:\t\n1455:\t\t/* The stack size is always a multiple of BPF_REG_SIZE. */\n1456:\t\tsize = round_up(size, BPF_REG_SIZE);\n1457:\t\tn = size / BPF_REG_SIZE;\n1458:\t\n1459:\t\tif (old_n \u003e= n)\n1460:\t\t\treturn 0;\n1461:\t\n1462:\t\tstate-\u003estack = realloc_array(state-\u003estack, old_n, n, sizeof(struct bpf_stack_state));\n1463:\t\tif (!state-\u003estack)\n1464:\t\t\treturn -ENOMEM;\n1465:\t\n1466:\t\tstate-\u003eallocated_stack = size;\n1467:\t\n1468:\t\t/* update known max for given subprogram */\n1469:\t\tif (env-\u003esubprog_info[state-\u003esubprogno].stack_depth \u003c size)\n1470:\t\t\tenv-\u003esubprog_info[state-\u003esubprogno].stack_depth = size;\n1471:\t\n1472:\t\treturn 0;\n1473:\t}\n1474:\t\n1475:\tstatic int grow_stack_arg_slots(struct bpf_verifier_env *env,\n1476:\t\t\t\t\tstruct bpf_func_state *state, int cnt)\n1477:\t{\n1478:\t\tsize_t old_n = state-\u003eout_stack_arg_cnt;\n1479:\t\n1480:\t\tif (old_n \u003e= cnt)\n1481:\t\t\treturn 0;\n1482:\t\n1483:\t\tstate-\u003estack_arg_regs = realloc_array(state-\u003estack_arg_regs, old_n, cnt,\n1484:\t\t\t\t\t\t sizeof(struct bpf_reg_state));\n1485:\t\tif (!state-\u003estack_arg_regs)\n1486:\t\t\treturn -ENOMEM;\n1487:\t\n1488:\t\tstate-\u003eout_stack_arg_cnt = cnt;\n1489:\t\treturn 0;\n"
} |
| 49/3 | 2026/09/26 22:12 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 94271 output: 28 thoughts: 32 Thoughts: Okay, here's my take on those thoughts, framed as an expert's internal monologue: **Diving into `realloc_array` in the BPF Verifier** Alright, let's pull up the source code for `realloc_array` within `kernel/bpf/verifier.c`. I need to refresh my memory on the implementation details – specifically how it's being used within the BPF verifier. It's crucial to understand how this function handles memory allocation and resizing, particularly within this critical context. I'm keen to examine the potential edge cases and performance characteristics when the verifier dynamically adjusts the size of its internal data structures. Let's see how they're handling things, looking for any potential vulnerabilities or performance bottlenecks. |
| 50/3 | 2026/09/26 22:12 | tool | grepper |
0mArgs:{
"Expression": "realloc_array",
"PathPrefix": "kernel/bpf"
}Results: {
"Output": "kernel/bpf/core.c=830=int bpf_jit_add_poke_descriptor(struct bpf_prog *prog,\n--\nkernel/bpf/core.c-852-\nkernel/bpf/core.c:853:\ttab = krealloc_array(tab, size, sizeof(*poke), GFP_KERNEL);\nkernel/bpf/core.c-854-\tif (!tab)\n--\nkernel/bpf/states.c=970=bool bpf_id_scratch_reserve(void **arr, u32 *cap, u32 cnt, size_t elem_size)\n--\nkernel/bpf/states.c-977-\tnew_cap = *cap ? *cap * 2 : 64;\nkernel/bpf/states.c:978:\tp = krealloc_array(*arr, new_cap, elem_size, GFP_KERNEL_ACCOUNT | __GFP_NOWARN);\nkernel/bpf/states.c-979-\tif (!p)\n--\nkernel/bpf/verifier.c=1343=static void *copy_array(void *dst, const void *src, size_t n, size_t size, gfp_t flags)\n--\nkernel/bpf/verifier.c-1369- *\nkernel/bpf/verifier.c:1370: * Contrary to krealloc_array, does not free arr if new_n is zero.\nkernel/bpf/verifier.c-1371- */\nkernel/bpf/verifier.c:1372:static void *realloc_array(void *arr, size_t old_n, size_t new_n, size_t size)\nkernel/bpf/verifier.c-1373-{\n--\nkernel/bpf/verifier.c=1437=static int resize_reference_state(struct bpf_verifier_state *state, size_t n)\nkernel/bpf/verifier.c-1438-{\nkernel/bpf/verifier.c:1439:\tstate-\u003erefs = realloc_array(state-\u003erefs, state-\u003eacquired_refs, n,\nkernel/bpf/verifier.c-1440-\t\t\t\t sizeof(struct bpf_reference_state));\n--\nkernel/bpf/verifier.c=1451=static int grow_stack_state(struct bpf_verifier_env *env, struct bpf_func_state *state, int size)\n--\nkernel/bpf/verifier.c-1461-\nkernel/bpf/verifier.c:1462:\tstate-\u003estack = realloc_array(state-\u003estack, old_n, n, sizeof(struct bpf_stack_state));\nkernel/bpf/verifier.c-1463-\tif (!state-\u003estack)\n--\nkernel/bpf/verifier.c=1475=static int grow_stack_arg_slots(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-1482-\nkernel/bpf/verifier.c:1483:\tstate-\u003estack_arg_regs = realloc_array(state-\u003estack_arg_regs, old_n, cnt,\nkernel/bpf/verifier.c-1484-\t\t\t\t\t sizeof(struct bpf_reg_state));\n"
} |
| 51/3 | 2026/09/26 22:12 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 95305 output: 37 thoughts: 28 Thoughts: Okay, let's dive into this. I'm going to take a look at the `realloc_array` function, specifically around line 1370 in `kernel/bpf/verifier.c`. This is where the rubber meets the road in the BPF verifier, right? I'm expecting this to be a crucial piece of memory management, likely dealing with allocation and resizing within the context of analyzing and validating BPF programs. Given the context, it's probably handling potentially untrusted data or structures, making it a critical point for security. I'm expecting to see careful checks, overflow handling, and robust error management. This is where vulnerabilities could easily hide, so I'll be scrutinizing it for any potential weaknesses. I'll need to understand how it interacts with other parts of the verifier's memory model. Let's see how it's implemented. |
| 52/3 | 2026/09/26 22:12 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/verifier.c",
"FirstLine": 1370,
"LineCount": 30
}Results: {
"Contents": "1370:\t * Contrary to krealloc_array, does not free arr if new_n is zero.\n1371:\t */\n1372:\tstatic void *realloc_array(void *arr, size_t old_n, size_t new_n, size_t size)\n1373:\t{\n1374:\t\tsize_t alloc_size;\n1375:\t\tvoid *new_arr;\n1376:\t\n1377:\t\tif (!new_n || old_n == new_n)\n1378:\t\t\tgoto out;\n1379:\t\n1380:\t\talloc_size = kmalloc_size_roundup(size_mul(new_n, size));\n1381:\t\tnew_arr = krealloc(arr, alloc_size, GFP_KERNEL_ACCOUNT);\n1382:\t\tif (!new_arr) {\n1383:\t\t\tkfree(arr);\n1384:\t\t\treturn NULL;\n1385:\t\t}\n1386:\t\tarr = new_arr;\n1387:\t\n1388:\t\tif (new_n \u003e old_n)\n1389:\t\t\tmemset(arr + old_n * size, 0, (new_n - old_n) * size);\n1390:\t\n1391:\tout:\n1392:\t\treturn arr ? arr : ZERO_SIZE_PTR;\n1393:\t}\n1394:\t\n1395:\tstatic int copy_reference_state(struct bpf_verifier_state *dst, const struct bpf_verifier_state *src)\n1396:\t{\n1397:\t\tdst-\u003erefs = copy_array(dst-\u003erefs, src-\u003erefs, src-\u003eacquired_refs,\n1398:\t\t\t\t sizeof(struct bpf_reference_state), GFP_KERNEL_ACCOUNT);\n1399:\t\tif (!dst-\u003erefs)\n"
} |
| 53/3 | 2026/09/26 22:12 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 96050 output: 30 thoughts: 189 Thoughts: **Analyzing Stack Initialization in `realloc_array` and BPF** Okay, let's zero in on line 1388 of that code snippet. I see that `realloc_array` is being used, and crucially, it's zeroing out the newly allocated memory with `memset`. That's important. So, when the `state->stack` grows, `memset` clears it to zero. This means the `slot_type` is initialized to 0, which corresponds to `STACK_INVALID`. But hold on, what's the behavior at *runtime*? What happens in the actual running program? And specifically in the context of BPF, can a BPF program potentially read uninitialized stack bytes? That's the core question here. Now, let's consider this `env->allow_uninit_stack` setting. What impact does that have? Does it permit reading uninitialized memory, or does something else happen? I need to understand exactly what `allow_uninit_stack` controls. Time to go dig into its definition and usage. |
| 54/3 | 2026/09/26 22:12 | tool | grepper |
0mArgs:{
"Expression": "allow_uninit_stack",
"PathPrefix": "kernel/bpf"
}Results: {
"Output": "kernel/bpf/states.c=668=static bool is_stack_misc_after(struct bpf_verifier_env *env,\n--\nkernel/bpf/states.c-675-\t\t ((stack-\u003eslot_type[i] == STACK_INVALID || stack-\u003eslot_type[i] == STACK_POISON) \u0026\u0026\nkernel/bpf/states.c:676:\t\t env-\u003eallow_uninit_stack))\nkernel/bpf/states.c-677-\t\t\tcontinue;\n--\nkernel/bpf/states.c=696=static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,\n--\nkernel/bpf/states.c-731-\nkernel/bpf/states.c:732:\t\tif (env-\u003eallow_uninit_stack \u0026\u0026 old_type == STACK_MISC)\nkernel/bpf/states.c-733-\t\t\tcontinue;\n--\nkernel/bpf/verifier.c=3799=static int check_stack_write_var_off(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-3899-\t\tif ((*stype == STACK_INVALID || *stype == STACK_POISON) \u0026\u0026\nkernel/bpf/verifier.c:3900:\t\t !env-\u003eallow_uninit_stack) {\nkernel/bpf/verifier.c-3901-\t\t\tverbose(env, \"uninit stack in range of var-offset write prohibited for !root; insn %d, off: %d\",\n--\nkernel/bpf/verifier.c=4007=static int check_stack_read_fixed_off(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-4079-\t\t\t\t\t}\nkernel/bpf/verifier.c:4080:\t\t\t\t\tif (type == STACK_INVALID \u0026\u0026 env-\u003eallow_uninit_stack)\nkernel/bpf/verifier.c-4081-\t\t\t\t\t\tcontinue;\n--\nkernel/bpf/verifier.c-4138-\t\t\t\tcontinue;\nkernel/bpf/verifier.c:4139:\t\t\tif (type == STACK_INVALID \u0026\u0026 env-\u003eallow_uninit_stack)\nkernel/bpf/verifier.c-4140-\t\t\t\tcontinue;\n--\nkernel/bpf/verifier.c=6545=static int check_stack_slot_within_bounds(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-6551-\nkernel/bpf/verifier.c:6552:\tif (t == BPF_WRITE || env-\u003eallow_uninit_stack)\nkernel/bpf/verifier.c-6553-\t\tmin_valid_off = -(int)env-\u003estack_limit;\n--\nkernel/bpf/verifier.c=7351=static int check_stack_range_initialized(\n--\nkernel/bpf/verifier.c-7371-\t\t (meta-\u003earg_raw_mem.mask \u0026 BIT(arg_slot));\nkernel/bpf/verifier.c:7372:\tbool raw_mode = uninit \u0026\u0026 env-\u003eallow_uninit_stack \u0026\u0026\nkernel/bpf/verifier.c-7373-\t\t\t!(meta-\u003earg_raw_mem.var_size_mask \u0026 BIT(arg_slot));\n--\nkernel/bpf/verifier.c-7433-\t\tif ((*stype == STACK_ZERO) ||\nkernel/bpf/verifier.c:7434:\t\t (*stype == STACK_INVALID \u0026\u0026 (uninit || env-\u003eallow_uninit_stack))) {\nkernel/bpf/verifier.c:7435:\t\t\tif (clobber \u0026\u0026 (*stype != STACK_INVALID || env-\u003eallow_uninit_stack)) {\nkernel/bpf/verifier.c-7436-\t\t\t\t/* helper can write anything into the stack */\n--\nkernel/bpf/verifier.c-7454-\t\t\tif (allow_poison) {\nkernel/bpf/verifier.c:7455:\t\t\t\tif (uninit \u0026\u0026 env-\u003eallow_uninit_stack)\nkernel/bpf/verifier.c-7456-\t\t\t\t\t*stype = STACK_MISC;\n--\nkernel/bpf/verifier.c=14276=s64 bpf_helper_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-14294-\tfull_write = (at \u0026 MEM_UNINIT) \u0026\u0026\nkernel/bpf/verifier.c:14295:\t\t (!arg_type_is_raw_mem(at) || env-\u003eallow_uninit_stack);\nkernel/bpf/verifier.c-14296-\n--\nkernel/bpf/verifier.c=14402=s64 bpf_kfunc_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-14468-\tif (is_kfunc_arg_uninit(btf, \u0026args[i]) \u0026\u0026\nkernel/bpf/verifier.c:14469:\t (is_kfunc_arg_dynptr(btf, \u0026args[i]) || env-\u003eallow_uninit_stack))\nkernel/bpf/verifier.c-14470-\t\treturn -size;\n--\nkernel/bpf/verifier.c=22309=int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,\n--\nkernel/bpf/verifier.c-22337-\tenv-\u003eallow_ptr_leaks = bpf_allow_ptr_leaks(env-\u003eprog-\u003eaux-\u003etoken);\nkernel/bpf/verifier.c:22338:\tenv-\u003eallow_uninit_stack = bpf_allow_uninit_stack(env-\u003eprog-\u003eaux-\u003etoken);\nkernel/bpf/verifier.c-22339-\tenv-\u003ebypass_spec_v1 = bpf_bypass_spec_v1(env-\u003eprog-\u003eaux-\u003etoken);\n"
} |
| 55/3 | 2026/09/26 22:12 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 98034 output: 33 thoughts: 38 Thoughts: Okay, let's break this down. **Investigating the `bpf_allow_uninit_stack` Function** Right, so I need to figure out what `bpf_allow_uninit_stack` is actually *doing*. Specifically, I need to know what kind of return values this function produces, and what information they convey. The name itself gives a clue, but a deeper understanding requires the code. I'll need to know whether this affects verification, and what verification constraints it enables or disables. A quick grep should reveal all the usages, so I can see its interaction with other parts of the BPF subsystem. I want to build a clear mental model of its behavior. |
| 56/3 | 2026/09/26 22:12 | tool | grepper |
0mArgs:{
"Expression": "bpf_allow_uninit_stack",
"PathPrefix": "kernel/bpf"
}Results: {
"Output": "kernel/bpf/verifier.c=22309=int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,\n--\nkernel/bpf/verifier.c-22337-\tenv-\u003eallow_ptr_leaks = bpf_allow_ptr_leaks(env-\u003eprog-\u003eaux-\u003etoken);\nkernel/bpf/verifier.c:22338:\tenv-\u003eallow_uninit_stack = bpf_allow_uninit_stack(env-\u003eprog-\u003eaux-\u003etoken);\nkernel/bpf/verifier.c-22339-\tenv-\u003ebypass_spec_v1 = bpf_bypass_spec_v1(env-\u003eprog-\u003eaux-\u003etoken);\n"
} |
| 57/3 | 2026/09/26 22:12 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 98443 output: 23 thoughts: 18 Thoughts: Okay, here's my attempt at summarizing the thought process, tailored for an expert audience, and written in the first person: **Investigating `bpf_allow_uninit_stack`** Alright, let's dive into this `bpf_allow_uninit_stack` business. The name itself hints at something related to the Berkeley Packet Filter (BPF) and potentially uninitialized stack memory. My initial instinct is to go straight to the kernel source. I suspect this is a relatively recent addition, or perhaps a feature with specific implications for security or performance. I'm imagining this is probably an option or flag controlling a security-related behavior. Is it a compile-time option? A runtime control? The context will clarify this. I'm mentally preparing for a deep dive into the BPF subsystem, tracing the code from its potential declaration or usage in the BPF verifier or instruction set, all the way to its potential impacts on how BPF programs are validated and executed. We're certainly talking about a low-level detail. I’m expecting potential interactions with memory safety, stack layout, and the overall security posture of BPF programs. I know there are risks involved with uninitialized memory in any context, and I'm guessing this flag likely provides a mechanism to mitigate those risks within the BPF execution environment. I want to see how this flag interacts with the various BPF helpers and data structures. It's probably tied into hardening BPF programs, maybe to defend against certain exploit techniques that involve reading uninitialized memory. Now to start my search... |
| 58/3 | 2026/09/26 22:12 | tool | grepper |
0mArgs:{
"Expression": "bpf_allow_uninit_stack"
}Results: {
"Output": "include/linux/bpf.h=2974=static inline bool bpf_allow_ptr_leaks(const struct bpf_token *token)\n--\ninclude/linux/bpf.h-2978-\ninclude/linux/bpf.h:2979:static inline bool bpf_allow_uninit_stack(const struct bpf_token *token)\ninclude/linux/bpf.h-2980-{\n--\nkernel/bpf/verifier.c=22309=int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,\n--\nkernel/bpf/verifier.c-22337-\tenv-\u003eallow_ptr_leaks = bpf_allow_ptr_leaks(env-\u003eprog-\u003eaux-\u003etoken);\nkernel/bpf/verifier.c:22338:\tenv-\u003eallow_uninit_stack = bpf_allow_uninit_stack(env-\u003eprog-\u003eaux-\u003etoken);\nkernel/bpf/verifier.c-22339-\tenv-\u003ebypass_spec_v1 = bpf_bypass_spec_v1(env-\u003eprog-\u003eaux-\u003etoken);\n"
} |
| 59/3 | 2026/09/26 22:12 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 99113 output: 37 thoughts: 26 Thoughts: Okay, let's dive into this. I'm going to pull up `include/linux/bpf.h` and focus on lines 2975 through 2990. I need to refresh my memory on what's defined in that section; it's probably related to some core BPF data structures or helper functions. This should give me a good handle on some of the underlying mechanics of what is happening within that range of lines. Let me get right to it! |
| 60/3 | 2026/09/26 22:12 | tool | read-file |
0mArgs:{
"File": "include/linux/bpf.h",
"FirstLine": 2975,
"LineCount": 20
}Results: {
"Contents": "2975:\t{\n2976:\t\treturn bpf_token_capable(token, CAP_PERFMON);\n2977:\t}\n2978:\t\n2979:\tstatic inline bool bpf_allow_uninit_stack(const struct bpf_token *token)\n2980:\t{\n2981:\t\treturn bpf_token_capable(token, CAP_PERFMON);\n2982:\t}\n2983:\t\n2984:\tstatic inline bool bpf_bypass_spec_v1(const struct bpf_token *token)\n2985:\t{\n2986:\t\treturn bpf_jit_bypass_spec_v1() ||\n2987:\t\t\tcpu_mitigations_off() ||\n2988:\t\t\tbpf_token_capable(token, CAP_PERFMON);\n2989:\t}\n2990:\t\n2991:\tstatic inline bool bpf_bypass_spec_v4(const struct bpf_token *token)\n2992:\t{\n2993:\t\treturn bpf_jit_bypass_spec_v4() ||\n2994:\t\t\tcpu_mitigations_off() ||\n"
} |
| 61/3 | 2026/09/26 22:12 | llm | kmsan-evaluator |
1mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 99597 output: 26 thoughts: 156 Thoughts: **KMSAN and BPF Interaction: A Quick Scramble** Okay, hold on a minute. This `bpf_allow_uninit_stack` thing… it's letting privileged BPF programs (those with CAP_PERFMON) peek at uninitialized stack memory? That's a potential can of worms, security-wise. Now, the real question is, does KMSAN (Kernel Memory Sanitizer) even *care* about this during BPF execution? Does BPF have some special KMSAN handling, or does it operate in its own little world? Wait, I need to remind myself how KMSAN works. It instruments *kernel code* specifically, the C code that was compiled with Clang and with KMSAN enabled. BPF JIT'ed code, however, is *not* touched by Clang KMSAN. But… what about when a BPF helper function or kfunc gets called? What happens when I pass uninitialized stack memory to a BPF helper or kfunc? Does KMSAN check it *then*? Or am I missing something crucial here? Alright, let's take a step back. I need to get a clearer picture of KMSAN and how it actually interacts with BPF. Is it even relevant to this situation? This is something I've got to understand before I go any further down this rabbit hole. Let me fire up a `grep` and search for "kmsan" within the `kernel/bpf/` directory. Maybe there are some clues in the existing code that can give me an idea of what's going on. |
| 62/3 | 2026/09/26 22:13 | tool | grepper |
0mArgs:{
"Expression": "kmsan",
"PathPrefix": "kernel/bpf"
}Results: {
"Output": "kernel/bpf/core.c=2410=static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \\\n--\nkernel/bpf/core.c-2414-\\\nkernel/bpf/core.c:2415:\tkmsan_unpoison_memory(stack, sizeof(stack)); \\\nkernel/bpf/core.c-2416-\tFP = (u64) (unsigned long) \u0026stack[ARRAY_SIZE(stack)]; \\\n--\nkernel/bpf/core.c=2423=static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \\\n--\nkernel/bpf/core.c-2428-\\\nkernel/bpf/core.c:2429:\tkmsan_unpoison_memory(stack, sizeof(stack)); \\\nkernel/bpf/core.c-2430-\tFP = (u64) (unsigned long) \u0026stack[ARRAY_SIZE(stack)]; \\\n"
} |
| 63/3 | 2026/09/26 22:13 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 100402 output: 28 thoughts: 1980 Thoughts: **KMSAN Assessment: Interpreted BPF Stack Size Increase**
Okay, so I'm diving into this patch concerning the BPF interpreter's stack size, specifically looking for uninitialized memory risks. First, the interpreter. The code uses `kmsan_unpoison_memory(stack, sizeof(stack))` in `kernel/bpf/core.c` for the interpreter's stack. The documentation, `Documentation/bpf/bpf_design_QA.rst`, confirms the interpreter's limit is 512 bytes, enforced in `bpf_prog_stack_limit()`. If JIT isn't requested, the limit *remains* 512 bytes, which is the key. The JIT, on x86_64 at least, *does* support larger stacks according to `arch/x86/net/bpf_jit_comp.c`, confirmed with `bpf_jit_supports_large_stack()`. But, importantly, KMSAN *doesn't* instrument JITed code, so this isn't relevant for KMSAN's purpose here.
Now, the core of the matter: Does the patch introduce any risk related to uninitialized memory, potentially detectable *only* by KMSAN? The critical distinction is that standard KASAN configurations already catch many issues. KMSAN is only needed for risks related to *uninitialized* memory usage or potential information leaks. I need to see if the patch affects kernel structures being sent to userspace, conditional logic using uninitialized variables, the allocation/initialization of data structures where uninitialized fields could be read, or bounds checks that introduce uninitialized byte exposure.
Let's break down the criteria.
**Condition 1:** Kernel structures sent to userspace. I'm focusing on anything that copies data out to userspace. Does this patch change `bpf_verifier_log`? Looking at `bpf_verifier.h`, I see `BPF_VERIFIER_TMP_LOG_SIZE 2048` and `TMP_STR_BUF_LEN 1408`. The code in `bpf_fmt_stack_mask` with `snprintf` ensures that any verifier log entries are properly formatted and null-terminated. The code uses string formatting so I don't see any risk. No structs copied to userspace. So, Condition 1 is good.
**Condition 2:** Conditional logic with potentially uninitialized variables. Now, I'm analyzing the changes to `struct bpf_jmp_history_entry`, the most involved section. Here I'm checking the following initialization cases:
`p->idx = env->insn_idx;`
`p->prev_idx = env->prev_insn_idx;`
`p->flags = insn_flags;`
`p->spi = spi;`
`p->frame = frame;`
`if (linked_regs_cnt) memcpy(p->linked_regs, linked_regs, linked_regs_cnt * sizeof(*linked_regs));`
`p->linked_regs_cnt = linked_regs_cnt;`
And the checking:
` verifier_bug_if(env->cur_hist_ent->linked_regs_cnt != 0, env,
"insn history: insn_idx %d has %u linked regs",
env->insn_idx, env->cur_hist_ent->linked_regs_cnt);
if (linked_regs_cnt)
memcpy(env->cur_hist_ent->linked_regs, linked_regs,
linked_regs_cnt * sizeof(*linked_regs));
env->cur_hist_ent->linked_regs_cnt = linked_regs_cnt;`
Is `linked_regs_cnt` ever uninitialized when used in conditional branching? No. It's explicitly initialized to the provided `linked_regs_cnt`. In `check_cond_jmp_op`, the linked_regs struct has an explicit initialization:
`struct linked_regs linked_regs = {};`
Now, I'm checking the definition of linked_regs!
I need to check how linked_regs is initialized in check_cond_jmp_op! I need to read `check_cond_jmp_op` in `kernel/bpf/verifier.c`.
I need to continue my analysis here, but it looks like the current review of these conditions means that NeedsKMSAN = false so far.
|
| 64/3 | 2026/09/26 22:14 | tool | grepper |
0mArgs:{
"Expression": "struct linked_regs",
"PathPrefix": "kernel/bpf"
}Results: {
"Output": "kernel/bpf/verifier.c=3395=struct linked_reg {\n--\nkernel/bpf/verifier.c-3403-\nkernel/bpf/verifier.c:3404:struct linked_regs {\nkernel/bpf/verifier.c-3405-\tint cnt;\n--\nkernel/bpf/verifier.c-3408-\nkernel/bpf/verifier.c:3409:static struct linked_reg *linked_regs_push(struct linked_regs *s)\nkernel/bpf/verifier.c-3410-{\n--\nkernel/bpf/verifier.c-3422- */\nkernel/bpf/verifier.c:3423:static void linked_regs_pack(const struct linked_regs *s, u16 *packed)\nkernel/bpf/verifier.c-3424-{\n--\nkernel/bpf/verifier.c-3433-\nkernel/bpf/verifier.c:3434:static void linked_regs_unpack(const struct bpf_jmp_history_entry *hist, struct linked_regs *s)\nkernel/bpf/verifier.c-3435-{\n--\nkernel/bpf/verifier.c=3481=void bpf_bt_sync_linked_regs(struct backtrack_state *bt, struct bpf_jmp_history_entry *hist)\nkernel/bpf/verifier.c-3482-{\nkernel/bpf/verifier.c:3483:\tstruct linked_regs linked_regs;\nkernel/bpf/verifier.c-3484-\tbool some_precise = false;\n--\nkernel/bpf/verifier.c=17645=static bool try_match_pkt_pointers(const struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-17749-\nkernel/bpf/verifier.c:17750:static void __collect_linked_regs(struct linked_regs *reg_set, struct bpf_reg_state *reg,\nkernel/bpf/verifier.c-17751-\t\t\t\t u32 id, u32 frameno, u32 spi_or_reg, bool is_reg)\n--\nkernel/bpf/verifier.c=17772=static void collect_linked_regs(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-17774-\t\t\t\tu32 id,\nkernel/bpf/verifier.c:17775:\t\t\t\tstruct linked_regs *linked_regs)\nkernel/bpf/verifier.c-17776-{\n--\nkernel/bpf/verifier.c=17807=static void sync_linked_regs(struct bpf_verifier_env *env, struct bpf_verifier_state *vstate,\nkernel/bpf/verifier.c:17808:\t\t\t struct bpf_reg_state *known_reg, struct linked_regs *linked_regs)\nkernel/bpf/verifier.c-17809-{\n--\nkernel/bpf/verifier.c=17862=static int check_cond_jmp_op(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-17869-\tstruct bpf_reg_state *eq_branch_regs;\nkernel/bpf/verifier.c:17870:\tstruct linked_regs linked_regs = {};\nkernel/bpf/verifier.c-17871-\tu8 opcode = BPF_OP(insn-\u003ecode);\n"
} |
| 65/3 | 2026/09/26 22:14 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 104268 output: 33 thoughts: 545 Thoughts: **Analyzing Potential Uninitialized Reads**
Okay, so I'm digging into this code to make sure there aren't any sneaky uninitialized reads happening, particularly in these linked register operations. First, I see `struct linked_regs linked_regs = {};` being initialized with `{}`. Good start, that should clear it out.
Now, looking at `check_cond_jmp_op`, it seems like `packed` gets filled by `linked_regs_pack` with elements from 0 up to `linked_regs.cnt - 1`. And `bpf_push_jmp_history` copies `linked_regs_cnt * sizeof(*linked_regs)` bytes into `p->linked_regs`. It also sets the `p->linked_regs_cnt` field. I see a similar process in the reverse, in `bpf_bt_sync_linked_regs`. `linked_regs_unpack` copies data from `hist` and sets `s->cnt = hist->linked_regs_cnt;`, then unpacks elements up to `s->cnt - 1`. So, it all looks consistent: the write and read of `linked_regs` are correctly synchronized. There's no obvious gap where a field might be read before being written.
Alright, let's move on to the more general check for Condition 3: "Allocation or initialization of complex data structures where uninitialized fields could be read later in reachable code paths." We need to verify that *all* newly allocated or modified structures have their fields properly initialized. Specifically, let's look at `bpf_idmap`.
```c
struct bpf_idmap {
u32 tmp_id_gen;
u32 cnt;
u32 cap;
struct bpf_id_pair *map;
};
```
I'm thinking, where is `idmap_scratch` initialized? Aha! It's part of `env`, which is in the `bpf_verifier_env` struct. Now, how is `env` allocated? Looks like it's allocated with `kvzalloc`. Okay, now I have to go back up to where `bpf_check` is called. That should give me a good picture of how and when `env` is initialized. I'll need to double check the allocation and make sure `kvzalloc` guarantees zero-initialization.
|
| 66/3 | 2026/09/26 22:14 | tool | grepper |
0mError:no matches Args: {
"Expression": "kvzalloc.*sizeof\\(\\*env",
"PathPrefix": "kernel/bpf"
}Results: {
"error": "no matches"
} |
| 67/3 | 2026/09/26 22:14 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 105373 output: 28 thoughts: 21 Thoughts: Here's my summary, as I delve into the code: **Locating the `env` Variable in the BPF Verifier** Alright, let's get down to brass tacks. I need to understand how the `env` variable is handled within the BPF verifier, specifically in `kernel/bpf/verifier.c`. I need to figure out how its memory is managed and allocated. It's crucial for understanding how the verifier analyzes and validates BPF programs. I'm going to pinpoint exactly where this `env` variable gets its start, that initial declaration, and where it potentially might get redefined or reallocated. This is a common starting point in tracing variable usage. Let's see what the code reveals! |
| 68/3 | 2026/09/26 22:14 | tool | grepper |
0mArgs:{
"Expression": "kzalloc.*env",
"PathPrefix": "kernel/bpf"
}Results: {
"Output": "kernel/bpf/btf.c=5988=static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr,\n--\nkernel/bpf/btf.c-6000-\nkernel/bpf/btf.c:6001:\tenv = kzalloc_obj(*env, GFP_KERNEL | __GFP_NOWARN);\nkernel/bpf/btf.c-6002-\tif (!env)\n--\nkernel/bpf/btf.c=6477=struct btf *btf_parse_vmlinux(void)\n--\nkernel/bpf/btf.c-6483-\nkernel/bpf/btf.c:6484:\tenv = kzalloc_obj(*env, GFP_KERNEL | __GFP_NOWARN);\nkernel/bpf/btf.c-6485-\tif (!env)\n--\nkernel/bpf/btf.c=6519=static struct btf *btf_parse_module(const char *module_name, const void *data,\n--\nkernel/bpf/btf.c-6533-\nkernel/bpf/btf.c:6534:\tenv = kzalloc_obj(*env, GFP_KERNEL | __GFP_NOWARN);\nkernel/bpf/btf.c-6535-\tif (!env)\n--\nkernel/bpf/fixups.c=1184=static int jit_subprogs(struct bpf_verifier_env *env)\n--\nkernel/bpf/fixups.c-1234-\terr = -ENOMEM;\nkernel/bpf/fixups.c:1235:\tfunc = kzalloc_objs(prog, env-\u003esubprog_cnt);\nkernel/bpf/fixups.c-1236-\tif (!func)\n"
} |
| 69/3 | 2026/09/26 22:14 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 106049 output: 33 thoughts: 21 Thoughts: Here's a first-person summary, tailored for an expert audience, as requested: **Diving into the BPF Verifier** Okay, time to get my hands dirty. Let's start by digging into `bpf_check`. It's housed right where I'd expect it, in `kernel/bpf/verifier.c`. I need to refresh my understanding of the static analysis process, and `bpf_check` is the likely linchpin of the verification engine. I’ll bet it handles the crucial initial checks on the BPF program bytecode before any actual execution. My intuition is telling me to look for things like opcode validation, register state management, and jump target analysis within its core logic. I expect this function to lay the groundwork for ensuring that the BPF program conforms to the necessary security and safety constraints to prevent potential abuse of the kernel. I’ll be especially interested in how it handles indirect jumps and the handling of the global data buffer access, as these points are commonly exploited by malicious BPF programs. This function has to be rock-solid and the security of the kernel depends on its correctness. Let's see what this is all about. |
| 70/3 | 2026/09/26 22:14 | tool | grepper |
0mArgs:{
"Expression": "kv.*alloc.*env",
"PathPrefix": "kernel/bpf/verifier.c"
}Results: {
"Output": "kernel/bpf/verifier.c=5706=static int check_max_stack_depth(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-5713-\nkernel/bpf/verifier.c:5714:\tdinfo = kvzalloc_objs(*dinfo, env-\u003esubprog_cnt, GFP_KERNEL_ACCOUNT);\nkernel/bpf/verifier.c-5715-\tif (!dinfo)\n--\nkernel/bpf/verifier.c=20359=static int add_func_ptr(struct bpf_verifier_env *env, struct bpf_map *map, u32 map_off,\n--\nkernel/bpf/verifier.c-20371-\tif (!(env-\u003efunc_ptr_cnt \u0026 (env-\u003efunc_ptr_cnt - 1))) {\nkernel/bpf/verifier.c:20372:\t\tptrs = kvrealloc(env-\u003efunc_ptrs,\nkernel/bpf/verifier.c-20373-\t\t\t\t array_size(max(2 * env-\u003efunc_ptr_cnt, 16U), sizeof(*ptrs)),\n--\nkernel/bpf/verifier.c=21824=static int process_fd_array_continuous(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-21835-\nkernel/bpf/verifier.c:21836:\tenv-\u003efd_array = kvzalloc_objs(*env-\u003efd_array, cnt, GFP_KERNEL_ACCOUNT);\nkernel/bpf/verifier.c-21837-\tif (!env-\u003efd_array)\n--\nkernel/bpf/verifier.c=22309=int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,\n--\nkernel/bpf/verifier.c-22325-\t */\nkernel/bpf/verifier.c:22326:\tenv = kvzalloc_obj(struct bpf_verifier_env, GFP_KERNEL_ACCOUNT);\nkernel/bpf/verifier.c-22327-\tif (!env)\n"
} |
| 71/3 | 2026/09/26 22:14 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 106895 output: 38 thoughts: 206 Thoughts: Here's my analysis of the provided code snippet: **Deconstructing BPF ID Management** Okay, so I see that the `env` struct, crucial for the BPF verifier, is allocated using `kvzalloc_obj`. This means both `env->idmap_scratch` and `env->idset_scratch` are going to start out zero-initialized. Good, that's a clean starting point. Now, let's trace what happens during resets. `reset_idmap_scratch` zeroes out `idmap->cnt`. It does *not* touch either the capacity (`cap`) or the actual map data. Similarly, `bpf_clear_singular_ids` simply resets `idset->num_ids`, leaving the capacity and entries alone. Ah, I see... so the growth comes in when we actually *need* to use the scratch space, right? `bpf_id_scratch_reserve` utilizes `krealloc_array` to expand the `map` or `entries` as needed. That makes sense; allocate only what we require. Hold on... I need to consider something. I have to wonder about the `merge_instances` function within `kernel/bpf/liveness.c`. I need to review those lines to make sure nothing in there is counter to my current understanding of how BPF IDs are managed in these scratch spaces. Let me check lines 1970-2025... |
| 72/3 | 2026/09/26 22:14 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/liveness.c",
"FirstLine": 1970,
"LineCount": 55
}Results: {
"Contents": "1970:\t * Merge a freshly analyzed instance into the original.\n1971:\t * may_read: union (any pass might read the slot).\n1972:\t * must_write: intersection (only slots written on ALL passes are guaranteed).\n1973:\t * live_before is recomputed by a subsequent update_instance() on @dst.\n1974:\t *\n1975:\t * The two instances may have settled on different mask widths for the same\n1976:\t * frame, so @dst is widened to cover @src first. A word only @dst has counts\n1977:\t * as zero on the @src side: it unions into may_read as a no-op and intersects\n1978:\t * must_write to empty.\n1979:\t */\n1980:\tstatic int merge_instances(struct func_instance *dst, struct func_instance *src)\n1981:\t{\n1982:\t\tstruct frame_masks *d, *s;\n1983:\t\tu32 f, i, w;\n1984:\t\n1985:\t\tfor (f = 0; f \u003c= dst-\u003edepth; f++) {\n1986:\t\t\ts = src-\u003eframes[f];\n1987:\t\t\td = dst-\u003eframes[f];\n1988:\t\t\tif (!s) {\n1989:\t\t\t\t/* This pass didn't touch frame f — must_write intersects with empty. */\n1990:\t\t\t\tif (d)\n1991:\t\t\t\t\tfor (i = 0; i \u003c dst-\u003einsn_cnt; i++)\n1992:\t\t\t\t\t\tbitmap_zero(rel_mask(d, i, FM_MUST_WRITE),\n1993:\t\t\t\t\t\t\t frame_mask_bits(d));\n1994:\t\t\t\tcontinue;\n1995:\t\t\t}\n1996:\t\t\tif (!d) {\n1997:\t\t\t\t/* Previous pass didn't touch frame f — take src, zero must_write. */\n1998:\t\t\t\tdst-\u003eframes[f] = s;\n1999:\t\t\t\tsrc-\u003eframes[f] = NULL;\n2000:\t\t\t\tfor (i = 0; i \u003c dst-\u003einsn_cnt; i++)\n2001:\t\t\t\t\tbitmap_zero(rel_mask(s, i, FM_MUST_WRITE), frame_mask_bits(s));\n2002:\t\t\t\tcontinue;\n2003:\t\t\t}\n2004:\t\t\td = widen_frame_masks(dst, f, s-\u003ewords);\n2005:\t\t\tif (!d)\n2006:\t\t\t\treturn -ENOMEM;\n2007:\t\t\tfor (i = 0; i \u003c dst-\u003einsn_cnt; i++) {\n2008:\t\t\t\tunsigned long *dst_read = rel_mask(d, i, FM_MAY_READ);\n2009:\t\t\t\tunsigned long *dst_write = rel_mask(d, i, FM_MUST_WRITE);\n2010:\t\t\t\tunsigned long *src_read = rel_mask(s, i, FM_MAY_READ);\n2011:\t\t\t\tunsigned long *src_write = rel_mask(s, i, FM_MUST_WRITE);\n2012:\t\n2013:\t\t\t\tfor (w = 0; w \u003c d-\u003ewords; w++) {\n2014:\t\t\t\t\tdst_read[w] |= w \u003c s-\u003ewords ? src_read[w] : 0;\n2015:\t\t\t\t\tdst_write[w] \u0026= w \u003c s-\u003ewords ? src_write[w] : 0;\n2016:\t\t\t\t}\n2017:\t\t\t}\n2018:\t\t}\n2019:\t\treturn 0;\n2020:\t}\n2021:\t\n2022:\tstatic struct func_instance *fresh_instance(struct func_instance *src)\n2023:\t{\n2024:\t\tstruct func_instance *f;\n"
} |
| 73/3 | 2026/09/26 22:14 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 108431 output: 31 thoughts: 1386 Thoughts: **Analyzing Potential Uninitialized Stack Accesses in BPF**
Okay, let's break this down systematically. My primary concern here is the potential for uninitialized stack reads given the changes, especially regarding the increased stack size in BPF programs. I'm focusing on "Condition 4," which is all about bounds checks, lengths, and logic leading to uninitialized data access.
First, I see the `merge_instances` function, which seems to be carefully bounded in its operations. That's a good starting point, but I need more context. My attention is drawn to `update_insn` in `liveness.c` and how it handles frame state. Specifically, the initialization of `new_after` with `{}` (zeroed) is crucial. Also `fm->words` is bounded, and that's good.
I then went to checking how the kernel manages stack memory during backtracking. The `bt_reset` function zeros out the `backtrack_state` structure, which *should* include the `stack_masks`. That `memset` call is key. It's initializing the `stack_masks` array: `unsigned long stack_masks[MAX_CALL_FRAMES][BITS_TO_LONGS(MAX_BPF_STACK_SLOTS)];`. This is critical for keeping track of stack liveness.
Now, my mind shifts to the core issue: the increased stack size. The patch allows access up to `MAX_BPF_STACK_JIT` (2048 bytes), while before it was limited to `MAX_BPF_STACK` (512 bytes). This is a big jump! My question is *how* is this managed?
The verifier enforces stack initialization on BPF read (helper/kfunc read). When the verifier reads a stack slot, it checks the type (`STYPE`) of the slot to determine if it is valid or initialized. The `check_stack_read_fixed_off` function is what I want to see here. It checks the type of the stack slot to be read: `STACK_MISC`, `STACK_ZERO`, `STACK_INVALID`, or `STACK_SPILL`.
Crucially, if `env->allow_uninit_stack` is *false*, any read from `STACK_INVALID` (uninitialized memory) results in an `-EACCES` error. That's the primary protection. This enforces that uninitialized stack is not read, except with `CAP_PERFMON`.
However, the caveat is that `env->allow_uninit_stack` being *true*, requires `CAP_PERFMON` (which is privileged). If that's the case, programs can read uninitialized stack slots. This is okay, because the BPF program stack frames on the CPU stack are allocated via the JIT prologue (`sub rsp, depth`).
This is where the alarm bells ring. BPF JIT is a very fast path, and there's a strong potential for uninitialized reads if the stack isn't properly handled, even with the check. I must consider the role of KMSAN here. Does KMSAN support JITed BPF execution and is it enabled? I need to search the kernel for `CONFIG_KMSAN` and `BPF` to find out if KMSAN can help detect this.
|
| 74/3 | 2026/09/26 22:14 | tool | grepper |
0mError:no matches Args: {
"Expression": "CONFIG_KMSAN.*BPF|BPF.*CONFIG_KMSAN"
}Results: {
"error": "no matches"
} |
| 75/3 | 2026/09/26 22:14 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 110575 output: 38 thoughts: 35 Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and written in the first person: **Kernel Memory Safety Investigation: Targetting the BPF JIT** Alright, I'm thinking, "Where would a memory safety issue, specifically one detectable by K-MSAN, be most likely to rear its ugly head in relation to BPF?" Given the potential for complex code generation, the BPF JIT is a prime suspect. Let's start by zeroing in on code related to the JIT, and specifically look in places like `bpf_jit`, likely to be in the kernel source code. More precisely, since we are working on `x86`, the code in `arch/x86/net/bpf_jit_comp.c` is going to be of prime interest. I'll need to check the code here. Let's start there and see what K-MSAN finds. |
| 76/3 | 2026/09/26 22:14 | tool | grepper |
0mError:no matches Args: {
"Expression": "kmsan",
"PathPrefix": "arch/x86/net/bpf_jit_comp.c"
}Results: {
"error": "no matches"
} |
| 77/3 | 2026/09/26 22:14 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 110863 output: 27 thoughts: 27 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience, presented in the first person: **Navigating the Kernel's KMSAN Landscape** Alright, so I'm thinking, where's the most likely place to start digging into the integration or usage of Kernel MemorySanitizer (`KMSAN`) within the mainline kernel? My gut feeling is to start with a targeted search. I'm going to focus my initial exploration on two key areas: `arch/x86/` and `kernel/bpf/`. `arch/x86/` feels like a natural candidate. System architecture-specific code often requires explicit memory management considerations, and `KMSAN`'s role in detecting uninitialized memory usage might be particularly relevant there. I'm imagining potentially seeing it used to harden core x86-specific routines, or perhaps within specific drivers or low-level components. I'm going to look for implementations and integrations. Then there's `kernel/bpf/`, which is a different beast entirely. eBPF, by its very nature, interacts heavily with user-supplied bytecode and kernel-level data. Memory safety is absolutely paramount there. It's likely `KMSAN` has been carefully integrated (or is at least actively considered for integration) to ensure the integrity of the kernel and prevent potentially malicious bytecode from exploiting memory errors. I expect to find some code related to eBPF verification, probe-points or tracing tools. My mental checklist now includes: 1. **Search Strings:** 'KMSAN', 'memory sanitizer', any relevant defines or macros. 2. **Specific Files:** Looking for `.c`, `.h` files, relevant make files, and other kernel build config related components. 3. **Relevant Functionality:** Memory allocation, device drivers, and data structure initialization. I'm expecting to find either direct code usages or configurations that enable `KMSAN` for specific modules or kernel builds. This should give me a good handle on its usage patterns and how it's being leveraged in these critical areas. |
| 78/3 | 2026/09/26 22:14 | tool | grepper |
0mError:no matches Args: {
"Expression": "KMSAN",
"PathPrefix": "kernel/bpf"
}Results: {
"error": "no matches"
} |
| 79/3 | 2026/09/26 22:14 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 111374 output: 26 thoughts: 21 Thoughts: Here's my thought process, summarized as you requested: **Searching the Kernel Tree for KSMAN and BPF Interactions** Okay, I need to investigate something related to `bpf` within the `mm/kmsan/` directory. My initial thought is, let's just go ahead and do a quick search across the entire kernel source tree. I want to see how `bpf` interacts, if at all, with the Kernel Memory Sanitizer (`kmsan`) code. This likely means using `grep` or a similar tool to find all instances of `bpf` within that specific directory structure. I'm hoping to quickly identify relevant code paths or configuration options. This will give me a better understanding of the relationship between these two areas, and is likely the first step in debugging or exploring their interplay. |
| 80/3 | 2026/09/26 22:14 | tool | grepper |
0mError:no matches Args: {
"Expression": "bpf",
"PathPrefix": "mm/kmsan"
}Results: {
"error": "no matches"
} |
| 81/3 | 2026/09/26 22:14 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 111606 output: 555 thoughts: 1620 Thoughts: **KMSAN Evaluation: Patch Analysis and Justification**
Okay, let's break this down. First off, I've got to say, it's a bit surprising that KMSAN wasn't even mentioned in the context of BPF JIT – it’s a standard tool for this type of work.
My primary concern is this: what kinds of bugs *could* this patch introduce, and crucially, which existing detectors handle them? The patch is fundamentally about increasing the BPF JIT stack depth limit and making corresponding changes in the verifier's stack tracking. Specifically:
1. We're going from 512 to 2048 bytes for the JIT stack.
2. The verifier is seeing data structure adjustments:
* `TMP_STR_BUF_LEN` and `BPF_VERIFIER_TMP_LOG_SIZE` are increased.
* `stack_masks` uses bitmaps now instead of `u64` shifts, with `DECLARE_BITMAP`.
* Stack slot access changed from direct indexing to `bpf_stack_slot()`.
* Dynamic reallocation (`bpf_id_scratch_reserve`) for `idmap` and `idset` is introduced.
* Liveness analysis has widened frames now (`frame_masks` allocated with `kvzalloc_flex`).
* Bounds checks now consider `env->stack_limit`.
* JIT prologue/epilogue for stack frame allocation could introduce stack overflow/out-of-bounds/register corruption.
Now, what detectors are relevant here?
* KASAN is clearly going to catch any out-of-bounds accesses in arrays, bitmaps, any kind of integer overflows, or any buffer overflows.
* `CONFIG_VMAP_STACK` and KASAN should handle stack overflows if the larger 2048-byte frames exceed the thread stack size.
* KASAN also handles use-after-free or double-free scenarios with `realloc_array` or `bpf_id_scratch_reserve`.
The critical question now is, are there any *uninitialized memory* risks? That's what KMSAN is for, after all. Let's run through the allocations:
1. `widen_frame_masks`: `kvzalloc_flex` is used which means it's zeroed.
2. `new_after` in `update_insn`: Initialized to zero using the `{}`.
3. `at_stack_in`: `kvmalloc_objs` plus a loop to initialize everything to `unvisited`.
4. `at_stack_out`: `kvmalloc_objs` and `memcpy`. Fully initialized.
5. `snap`: `kvmalloc_flex`, and assignment to its fields via `nslots` and `memcpy`.
6. `bpf_id_scratch_reserve`: Using `krealloc_array`. Important: the code *always* initializes the new entry's fields before the relevant counter (`idmap->cnt` or `idset->num_ids`) is incremented.
7. `bpf_push_jmp_history`: Allocates with `krealloc`. The fields ( `idx`, `prev_idx`, `flags`, `spi`, `frame`, `linked_regs_cnt`) are assigned directly, and `linked_regs` uses `memcpy` with `linked_regs_cnt` matching `linked_regs_unpack`.
8. `env->scratched_stack_slots`: either `bitmap_zero`, `bitmap_fill`, or `__set_bit`.
9. `bpf_fmt_stack_mask`: `buf[0] = '\0'`.
10. `bpf_check`: Frees `idmap_scratch.map` and `idset_scratch.entries`.
Looking at this, I see absolutely no uninitialized variables or struct fields being read or branched on. There are also no kernel-to-user info leaks introduced here. The primary changes deal with BPF verifier stack tracking and JIT stack depth limits. Any failure modes look like they'd be out-of-bounds, stack overflow, or verifier bugs – precisely the things KASAN, `VMAP_STACK`, and our standard tests will catch.
Therefore, applying the rule: "THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN...", `NeedsKMSAN` is unequivocally `false`. The primary tool is KASAN.
Here's my rationale:
* **NeedsKMSAN = false.**
* **KASAN Applicability:** KASAN will detect
|
| 82/3 | 2026/09/26 22:14 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch series increases the maximum BPF stack limit from 512 bytes to 2048 bytes on architectures with JIT support for large stacks (x86-64 and arm64). The verifier changes update internal tracking data structures to accommodate 256 stack slots (from 64), replacing u64 bitmasks with dynamically widened or statically sized bitmaps (e.g., in backtrack_state, frame_masks, and scratched_stack_slots), dynamically growing scratch arrays for id mapping (bpf_idmap, bpf_idset), and adjusting bounds checks.\n\nContrasting KASAN vs. KMSAN applicability:\n- KMSAN applicability: KMSAN detects use of uninitialized memory and info-leaks to user space. In this patch series:\n 1. All new or modified data structures allocated on heap/stack are fully initialized: `frame_masks` is allocated via `kvzalloc_flex`, local arrays like `new_after` are zero-initialized (`= {}`), `at_stack_in` is fully populated with `unvisited` markers, `spill_snapshot` is populated with `memcpy`, and dynamically grown entries in `bpf_idmap`/`bpf_idset` have their fields (`old`, `cur`, `id`, `cnt`) explicitly set upon growth.\n 2. In `bpf_jmp_history_entry`, all fields are explicitly assigned (`idx`, `prev_idx`, `flags`, `spi`, `frame`, `linked_regs_cnt`), and `linked_regs` is copied and accessed strictly up to `linked_regs_cnt`.\n 3. No kernel-to-user information leaks are introduced; verifier logging to user buffers is formatted strings with null terminators.\n 4. The BPF stack initialization rules enforced by the verifier (disallowing reads from uninitialized slots unless permitted by CAP_PERFMON token) remain unchanged in semantics, and the interpreter stack remains at 512 bytes with its explicit KMSAN unpoisoning intact.\n- KASAN applicability: The risks associated with this patch primarily concern potential out-of-bounds accesses or buffer overruns due to bitmap indexing, array bounds, slice reallocations, or kernel stack exhaustion (checked via KASAN, FORTIFY_SOURCE, and VMAP_STACK).\n\nSince no uninitialized memory risks or kernel-to-user info-leaks are introduced or exposed, dedicated KMSAN fuzzing is not warranted.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch series increases the maximum BPF stack limit from 512 bytes to 2048 bytes on architectures with JIT support for large stacks (x86-64 and arm64). The verifier changes update internal tracking data structures to accommodate 256 stack slots (from 64), replacing u64 bitmasks with dynamically widened or statically sized bitmaps (e.g., in backtrack_state, frame_masks, and scratched_stack_slots), dynamically growing scratch arrays for id mapping (bpf_idmap, bpf_idset), and adjusting bounds checks.\n\nContrasting KASAN vs. KMSAN applicability:\n- KMSAN applicability: KMSAN detects use of uninitialized memory and info-leaks to user space. In this patch series:\n 1. All new or modified data structures allocated on heap/stack are fully initialized: `frame_masks` is allocated via `kvzalloc_flex`, local arrays like `new_after` are zero-initialized (`= {}`), `at_stack_in` is fully populated with `unvisited` markers, `spill_snapshot` is populated with `memcpy`, and dynamically grown entries in `bpf_idmap`/`bpf_idset` have their fields (`old`, `cur`, `id`, `cnt`) explicitly set upon growth.\n 2. In `bpf_jmp_history_entry`, all fields are explicitly assigned (`idx`, `prev_idx`, `flags`, `spi`, `frame`, `linked_regs_cnt`), and `linked_regs` is copied and accessed strictly up to `linked_regs_cnt`.\n 3. No kernel-to-user information leaks are introduced; verifier logging to user buffers is formatted strings with null terminators.\n 4. The BPF stack initialization rules enforced by the verifier (disallowing reads from uninitialized slots unless permitted by CAP_PERFMON token) remain unchanged in semantics, and the interpreter stack remains at 512 bytes with its explicit KMSAN unpoisoning intact.\n- KASAN applicability: The risks associated with this patch primarily concern potential out-of-bounds accesses or buffer overruns due to bitmap indexing, array bounds, slice reallocations, or kernel stack exhaustion (checked via KASAN, FORTIFY_SOURCE, and VMAP_STACK).\n\nSince no uninitialized memory risks or kernel-to-user info-leaks are introduced or exposed, dedicated KMSAN fuzzing is not warranted.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|