| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/09/26 22:34 | flow | patch-triage |
3mArgs:null Results: {
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT"
],
"FocusSymbols": [
"bpf_check",
"bpf_push_jmp_history",
"bpf_mark_chain_precision",
"bpf_id_scratch_reserve",
"bpf_compute_subprog_arg_access",
"bpf_clear_singular_ids",
"bpf_bt_sync_linked_regs",
"bpf_jit_supports_large_stack"
],
"KMSANReasoning": "The patch series increases the maximum stack size budget for BPF programs from 512 bytes to 2048 bytes (MAX_BPF_STACK_JIT) on architectures with supporting JIT compilers (x86-64 and arm64). This requires adapting the BPF verifier's internal data structures and algorithms—including backtrack bitmasks, jump history entries, liveness analysis frame masks, and scratch id sets/maps—to handle up to 256 8-byte stack slots (or 512 4-byte half-slots).\n\nKASAN vs KMSAN applicability:\n1. Memory Safety Risks: The main risks of this change involve out-of-bounds accesses (e.g., if stack indexing or slot calculations exceed allocated bounds), bitmap indexing overflows, or memory management bugs (use-after-free, double free, memory leak) during dynamic reallocation of scratch buffers and liveness masks (e.g., in widen_frame_masks(), bpf_id_scratch_reserve(), or compute_subprog_args()). All of these conditions are directly caught by KASAN, KASAN-extra, and standard kernel bug detectors.\n2. Uninitialized Memory \u0026 Info Leaks: The patch does not introduce reads of uninitialized memory or expose kernel data to userspace.\n - Internal structures like `frame_masks` are allocated with `kvzalloc_flex()` (zero-initialized).\n - `realloc_array()` used for stack states explicitly zeroes out newly grown array elements.\n - Dynamic scratch structures (`bpf_idmap`, `bpf_idset`) and `jmp_history_entry` only access entries up to their explicitly maintained count (`cnt`, `num_ids`, `linked_regs_cnt`).\n - JITed BPF execution is not instrumented by KMSAN compiler passes, while interpreter execution remains capped at 512 bytes (and already unpoisons its interpreter stack buffer via `kmsan_unpoison_memory()`).\n\nBecause any potential memory corruption bugs arising from these changes fall squarely under KASAN detection and no uninitialized memory risks are introduced, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false,
"Reasoning": "The patch increases the maximum BPF program stack size from 512 bytes to 2048 bytes on JIT architectures supporting large stacks (x86_64 and arm64). To support this, significant changes are introduced to the BPF verifier and liveness/backtracking engine: replacing fixed 64-bit stack slot masks with dynamically sized bitmaps, refactoring jump history linked register tracking, dynamically reallocating scratch ID maps, and updating stack bounds checks throughout program verification. These modifications directly affect reachable kernel code executed during BPF program load via the bpf() syscall and warrant fuzzing for verifier bugs, memory leaks, and bounds inconsistencies.",
"WorthFuzzing": true
} |
| 1/1 | 2026/09/26 22:34 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit 0929b6d759163a715d7e79025cda7c32f2a904ad\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Sat Sep 26 22:34:46 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 44e3a9e683e8d..475e706534543 100644\n--- a/arch/arm64/net/bpf_jit_comp.c\n+++ b/arch/arm64/net/bpf_jit_comp.c\n@@ -2503,6 +2503,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 73cb097859a12..1c787208ff829 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@@ -243,55 +244,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@@ -421,30 +385,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@@ -530,12 +497,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@@ -551,7 +537,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@@ -576,7 +562,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@@ -735,7 +721,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@@ -867,7 +857,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@@ -876,18 +866,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@@ -996,10 +995,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@@ -1014,8 +1011,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@@ -1024,6 +1021,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@@ -1059,7 +1058,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@@ -1265,8 +1264,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@@ -1283,6 +1284,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 /*\n * Return the function state a stack pointer register refers to. frameno\n * shares storage with other pointer metadata, so return NULL for any\n@@ -1333,12 +1352,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@@ -1353,7 +1367,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@@ -1491,7 +1505,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@@ -1499,27 +1513,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@@ -1554,7 +1569,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 81b6de655b9be..aa72ad3ec6892 100644\n--- a/kernel/bpf/core.c\n+++ b/kernel/bpf/core.c\n@@ -3496,6 +3496,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 012b82513a3bc..a1f7b4a87502a 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@@ -709,37 +706,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@@ -749,8 +747,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@@ -763,21 +761,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@@ -789,13 +785,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@@ -803,8 +799,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@@ -820,8 +816,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@@ -968,6 +964,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@@ -1045,10 +1062,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@@ -1194,15 +1213,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@@ -1224,10 +1245,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@@ -1330,7 +1353,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@@ -1344,7 +1367,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@@ -1412,7 +1436,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 4c20e5bf8b69a..865b6c6eb8dd7 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@@ -3373,26 +3383,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@@ -3411,48 +3420,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@@ -3494,10 +3489,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@@ -3594,17 +3589,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@@ -3646,14 +3642,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@@ -3671,13 +3668,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@@ -3699,7 +3698,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@@ -3727,7 +3726,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\tclear_scalar_id(\u0026state-\u003estack[spi].spilled_ptr);\n+\t\t\tclear_scalar_id(\u0026ss-\u003espilled_ptr);\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@@ -3751,8 +3750,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@@ -3773,13 +3772,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@@ -3838,12 +3837,14 @@ static int check_stack_write_var_off(struct bpf_verifier_env *env,\n \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@@ -3866,9 +3867,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@@ -3939,23 +3939,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@@ -3966,8 +3965,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@@ -4015,9 +4020,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 \n@@ -4027,7 +4033,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@@ -4153,7 +4159,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@@ -4342,7 +4348,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@@ -4376,7 +4382,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@@ -5520,7 +5526,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@@ -5529,7 +5535,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@@ -6530,10 +6536,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@@ -6544,7 +6551,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@@ -7428,6 +7435,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@@ -7437,7 +7445,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@@ -7449,13 +7458,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@@ -8185,7 +8194,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@@ -8318,7 +8327,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@@ -8395,16 +8404,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@@ -8416,7 +8424,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@@ -9208,6 +9216,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@@ -9227,7 +9236,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@@ -9238,14 +9248,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@@ -10521,8 +10531,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@@ -14381,11 +14392,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@@ -14471,7 +14482,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@@ -15222,7 +15234,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@@ -15234,7 +15247,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@@ -15354,7 +15367,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@@ -15484,7 +15497,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@@ -17841,10 +17854,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@@ -17864,7 +17879,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@@ -17982,7 +17997,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@@ -18052,7 +18067,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@@ -18941,23 +18959,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@@ -18983,6 +19005,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@@ -18991,9 +19014,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@@ -19450,7 +19481,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@@ -22326,6 +22357,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@@ -22350,6 +22401,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@@ -22683,6 +22737,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 dc2c4180ee803..8a6d341b754ac 100644\n--- a/tools/testing/selftests/bpf/prog_tests/verifier.c\n+++ b/tools/testing/selftests/bpf/prog_tests/verifier.c\n@@ -64,6 +64,7 @@\n #include \"verifier_kfunc_uninit_multi.skel.h\"\n #include \"verifier_kfunc_perfmon.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@@ -248,6 +249,7 @@ void test_verifier_kfunc_uninit_multi(void) { RUN_TESTS(verifier_kfunc_uninit_\n void test_verifier_kfunc_perfmon(void) { RUN(verifier_kfunc_perfmon); }\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:34 | agent | patch-evaluator |
0mModel:core Args: null Results: {
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT"
],
"FocusSymbols": [
"bpf_check",
"bpf_push_jmp_history",
"bpf_mark_chain_precision",
"bpf_id_scratch_reserve",
"bpf_compute_subprog_arg_access",
"bpf_clear_singular_ids",
"bpf_bt_sync_linked_regs",
"bpf_jit_supports_large_stack"
],
"Reasoning": "The patch increases the maximum BPF program stack size from 512 bytes to 2048 bytes on JIT architectures supporting large stacks (x86_64 and arm64). To support this, significant changes are introduced to the BPF verifier and liveness/backtracking engine: replacing fixed 64-bit stack slot masks with dynamically sized bitmaps, refactoring jump history linked register tracking, dynamically reallocating scratch ID maps, and updating stack bounds checks throughout program verification. These modifications directly affect reachable kernel code executed during BPF program load via the bpf() syscall and warrant fuzzing for verifier bugs, memory leaks, and bounds inconsistencies.",
"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 0929b6d759163a715d7e79025cda7c32f2a904ad
Author: syz-cluster <triage@syzkaller.com>
Date: Sat Sep 26 22:34:46 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 44e3a9e683e8d..475e706534543 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -2503,6 +2503,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 73cb097859a12..1c787208ff829 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
@@ -243,55 +244,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) | \
@@ -421,30 +385,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];
@@ -530,12 +497,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;
}
@@ -551,7 +537,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. */
@@ -576,7 +562,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; \
@@ -735,7 +721,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.
@@ -867,7 +857,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];
};
@@ -876,18 +866,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() */
@@ -996,10 +995,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;
@@ -1014,8 +1011,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 */
@@ -1024,6 +1021,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 */
@@ -1059,7 +1058,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];
@@ -1265,8 +1264,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);
@@ -1283,6 +1284,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;
+}
+
/*
* Return the function state a stack pointer register refers to. frameno
* shares storage with other pointer metadata, so return NULL for any
@@ -1333,12 +1352,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)
@@ -1353,7 +1367,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);
@@ -1491,7 +1505,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)
@@ -1499,27 +1513,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)
@@ -1554,7 +1569,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 81b6de655b9be..aa72ad3ec6892 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -3496,6 +3496,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 012b82513a3bc..a1f7b4a87502a 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;
}
}
}
@@ -709,37 +706,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;
/*
@@ -749,8 +747,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;
@@ -763,21 +761,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
@@ -789,13 +785,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) ||
@@ -803,8 +799,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
@@ -820,8 +816,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;
@@ -968,6 +964,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;
@@ -1045,10 +1062,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;
@@ -1194,15 +1213,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;
}
@@ -1224,10 +1245,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;
@@ -1330,7 +1353,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;
@@ -1344,7 +1367,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;
@@ -1412,7 +1436,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 4c20e5bf8b69a..865b6c6eb8dd7 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);
@@ -3373,26 +3383,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;
};
@@ -3411,48 +3420,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;
}
}
@@ -3494,10 +3489,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];
@@ -3594,17 +3589,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);
}
@@ -3646,14 +3642,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,
@@ -3671,13 +3668,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;
@@ -3699,7 +3698,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) {
@@ -3727,7 +3726,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)
- clear_scalar_id(&state->stack[spi].spilled_ptr);
+ clear_scalar_id(&ss->spilled_ptr);
} 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];
@@ -3751,8 +3750,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);
@@ -3773,13 +3772,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;
}
@@ -3838,12 +3837,14 @@ static int check_stack_write_var_off(struct bpf_verifier_env *env,
/* 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) {
@@ -3866,9 +3867,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;
@@ -3939,23 +3939,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;
}
@@ -3966,8 +3965,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 {
@@ -4015,9 +4020,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);
@@ -4027,7 +4033,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--)
@@ -4153,7 +4159,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;
}
@@ -4342,7 +4348,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);
}
/*
@@ -4376,7 +4382,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)
@@ -5520,7 +5526,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;
@@ -5529,7 +5535,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++;
@@ -6530,10 +6536,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,
@@ -6544,7 +6551,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;
@@ -7428,6 +7435,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;
@@ -7437,7 +7445,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) ||
@@ -7449,13 +7458,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;
}
@@ -8185,7 +8194,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;
@@ -8318,7 +8327,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);
@@ -8395,16 +8404,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;
@@ -8416,7 +8424,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
@@ -9208,6 +9216,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;
@@ -9227,7 +9236,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--)
@@ -9238,14 +9248,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;
@@ -10521,8 +10531,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;
@@ -14381,11 +14392,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:
@@ -14471,7 +14482,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 */
@@ -15222,7 +15234,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;
@@ -15234,7 +15247,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:
@@ -15354,7 +15367,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;
@@ -15484,7 +15497,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;
@@ -17841,10 +17854,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);
}
}
@@ -17864,7 +17879,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))
@@ -17982,7 +17997,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;
}
@@ -18052,7 +18067,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;
}
@@ -18941,23 +18959,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 */
@@ -18983,6 +19005,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;
@@ -18991,9 +19014,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;
@@ -19450,7 +19481,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;
}
@@ -22326,6 +22357,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)
{
@@ -22350,6 +22401,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);
@@ -22683,6 +22737,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 dc2c4180ee803..8a6d341b754ac 100644
--- a/tools/testing/selftests/bpf/prog_tests/verifier.c
+++ b/tools/testing/selftests/bpf/prog_tests/verifier.c
@@ -64,6 +64,7 @@
#include "verifier_kfunc_uninit_multi.skel.h"
#include "verifier_kfunc_perfmon.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"
@@ -248,6 +249,7 @@ void test_verifier_kfunc_uninit_multi(void) { RUN_TESTS(verifier_kfunc_uninit_
void test_verifier_kfunc_perfmon(void) { RUN(verifier_kfunc_perfmon); }
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:34 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 76627 output: 30 thoughts: 793 Thoughts: **My Assessment of the BPF Patch Series** Okay, so I've been asked to assess this patch series for its fuzzing potential with syzkaller, and my initial impression is *very* promising. Let me break down my thought process. First, I examined the scope of the changes. The patch touches a substantial number of files, primarily centered around the BPF (Berkeley Packet Filter) subsystem. Specifically, I see modifications to documentation, architecture-specific JIT compilers (x86_64, arm64), the verifier, and a good number of core BPF files. This already raises a flag. A deep dive into the code reveals the primary focus: increasing the maximum stack size available to BPF programs when running on JITs that support it, from 512 bytes to 2048 bytes. This isn't just a simple size increase, though. The implications are far more complex. The verifier, in particular, has undergone significant surgery. We're talking about extensive modifications to how it handles stack memory and tracks its liveness, including the introduction of dynamic bitmaps for stack masks in `backtrack.c`, a rewrite of stack liveness tracking in `liveness.c` using the `frame_masks` and the like, as well as changes in `states.c` to handle the `idscratch` array. Plus, there are major revisions within `verifier.c` to manage stack slot limits, write and read checks, and, crucially, linked registers. The way these linked registers are packed and unpacked is changing from a simple `u64` to an `u16` array, allowing for a far greater number of stack slots. Given the target architecture is amd64, this whole set of modifications is definitely reachable executable code within the BPF subsystem, which is reachable via the `bpf()` syscall and the `BPF_PROG_LOAD` command. This represents a significant risk for security vulnerabilities since any logic modifications in memory management, synchronization, BPF, scheduler, core networking, VFS, or syscall handling *must* be fuzzed, given they're potential sources for memory safety bugs. The BPF verifier is already a known attack surface, and this patch *massively* alters its core functionality. We are talking about changes that touch liveness analysis, backtracking, precision tracking, stack slot tracking, jumping history, state pruning, memory allocation, and more. This sounds like an excellent target for syzkaller. Let me see... based on the rules for evaluation I have been given. The `WorthFuzzing` flag is clearly set to true. Based on the rules, the `FocusSymbols` should be functions modified or added in `kernel/bpf/*.` The BPF subsystem is complex enough, but this patch is very deep and modifies the verification process, stack limits, memory allocation, and related logic. That sounds just right. I now need to identify the optimal, non-ubiquitous focus symbols. I will look for any changes in the `Kconfig` files and header files to assist this. |
| 4/2 | 2026/09/26 22:34 | tool | grepper |
0mArgs:{
"Expression": "bpf_prog_stack_limit",
"PathPrefix": "include/"
}Results: {
"Output": "include/linux/bpf_verifier.h=937=struct bpf_verifier_env {\n--\ninclude/linux/bpf_verifier.h-1023-\tu32 max_stack_depth;\ninclude/linux/bpf_verifier.h:1024:\t/* stack budget of the program, see bpf_prog_stack_limit() */\ninclude/linux/bpf_verifier.h-1025-\tu32 stack_limit;\n--\ninclude/linux/bpf_verifier.h=1282=static inline int bpf_get_spi(s32 off)\n--\ninclude/linux/bpf_verifier.h-1295- */\ninclude/linux/bpf_verifier.h:1296:static inline u32 bpf_prog_stack_limit(const struct bpf_prog *prog)\ninclude/linux/bpf_verifier.h-1297-{\n"
} |
| 5/2 | 2026/09/26 22:34 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 78336 output: 51 thoughts: 503 Thoughts: **Analyzing BPF Function Modifications** Okay, let's dive into these BPF function modifications. My initial thought is to get a quick overview of what's been touched in `kernel/bpf/`. Starting with `backtrack.c`, it looks like we have changes to `bpf_push_jmp_history`, `bpf_fmt_stack_mask`, `backtrack_insn`, `bpf_mark_all_scalars_precise`, and `bpf_mark_chain_precision`. These all seem related to instruction backtracking, stack manipulation, and potentially scalar precision tracking. Interesting. Then, there's `core.c`, where I see `bpf_jit_supports_large_stack`. That's weak and implemented in arch/x86/net/bpf_jit_comp.c. Something to keep in mind, as it relates to the JIT and stack size support. Now, `liveness.c` looks like the meat of the changes. There's a flurry of static functions: `widen_frame_masks`, `mark_stack_range`, `mark_stack_read`, `mark_stack_write`, `mark_stack_read_all`, `mark_stack_read_mask`, `update_insn`, `is_live_before`, `fmt_spis_mask`, `print_instance`, `spill_slots_affordable`, `subprog_spill_slots`, `fp_off_to_slot`, `fill_from_stack`, `spill_to_stack`, `clear_overlapping_stack_slots`, `clear_stack_for_all_offs`, `arg_track_log`, `arg_track_xfer`, `record_stack_access_off`, `record_stack_access`, `record_imprecise`, `print_subprog_arg_access`, `compute_subprog_args`, `merge_instances`, and `analyze_subprog`. That's a huge list, all static functions! That screams "stack liveness analysis and management" to me. It is highly probable, given my expertise, that this is the area where the most important changes have happened. My next step is to check if there are any non-static functions in `liveness.c`. The static keyword is important, because this potentially gives me a starting point for analyzing control flow. If these functions are modified to interact with the broader system, and they aren't static, they'll be key. |
| 6/2 | 2026/09/26 22:35 | tool | grepper |
0mArgs:{
"Expression": "^[a-zA-Z_][a-zA-Z0-9_]*.*bpf_",
"PathPrefix": "kernel/bpf/liveness.c"
}Results: {
"Output": "kernel/bpf/liveness.c=65=struct live_stack_query {\n--\nkernel/bpf/liveness.c-71-\nkernel/bpf/liveness.c:72:struct bpf_liveness {\nkernel/bpf/liveness.c-73-\tDECLARE_HASHTABLE(func_instances, 8);\t\t/* maps (depth, callsite) to func_instance */\n--\nkernel/bpf/liveness.c=83=static u32 instance_hash(u32 callsite, u32 depth)\n--\nkernel/bpf/liveness.c-89-\nkernel/bpf/liveness.c:90:static struct func_instance *find_instance(struct bpf_verifier_env *env,\nkernel/bpf/liveness.c-91-\t\t\t\t\t u32 callsite, u32 depth)\n--\nkernel/bpf/liveness.c-102-\nkernel/bpf/liveness.c:103:static struct func_instance *call_instance(struct bpf_verifier_env *env,\nkernel/bpf/liveness.c-104-\t\t\t\t\t struct func_instance *caller,\n--\nkernel/bpf/liveness.c-129-\nkernel/bpf/liveness.c:130:static struct func_instance *lookup_instance(struct bpf_verifier_env *env,\nkernel/bpf/liveness.c-131-\t\t\t\t\t struct bpf_verifier_state *st,\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=259=static int mark_stack_write(struct func_instance *instance, u32 frame, u32 insn_idx,\n--\nkernel/bpf/liveness.c-272- */\nkernel/bpf/liveness.c:273:static int mark_stack_read_all(struct bpf_verifier_env *env, struct func_instance *instance,\nkernel/bpf/liveness.c-274-\t\t\t u32 frame, u32 insn_idx)\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=313=__diag_ignore_all(\"-Woverride-init\", \"Allow field initialization overrides for opcode_info_tbl\");\n--\nkernel/bpf/liveness.c-318- */\nkernel/bpf/liveness.c:319:inline struct bpf_iarray *\nkernel/bpf/liveness.c:320:bpf_insn_successors(struct bpf_verifier_env *env, u32 idx)\nkernel/bpf/liveness.c-321-{\n--\nkernel/bpf/liveness.c=372=__diag_pop();\nkernel/bpf/liveness.c-373-\nkernel/bpf/liveness.c:374:static inline bool update_insn(struct bpf_verifier_env *env,\nkernel/bpf/liveness.c-375-\t\t\t struct func_instance *instance, u32 frame, u32 insn_idx)\n--\nkernel/bpf/liveness.c-416-/* Fixed-point computation of @live_before marks */\nkernel/bpf/liveness.c:417:static void update_instance(struct bpf_verifier_env *env, struct func_instance *instance)\nkernel/bpf/liveness.c-418-{\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-482- */\nkernel/bpf/liveness.c:483:static bool callee_stack_access_at_callsite(struct bpf_verifier_env *env, u32 callsite)\nkernel/bpf/liveness.c-484-{\n--\nkernel/bpf/liveness.c-488-\nkernel/bpf/liveness.c:489:bool bpf_stack_slot_alive(struct bpf_verifier_env *env, u32 frameno, u32 half_spi)\nkernel/bpf/liveness.c-490-{\n--\nkernel/bpf/liveness.c-536-\nkernel/bpf/liveness.c:537:static char *fmt_subprog(struct bpf_verifier_env *env, int subprog)\nkernel/bpf/liveness.c-538-{\n--\nkernel/bpf/liveness.c-545-\nkernel/bpf/liveness.c:546:static char *fmt_instance(struct bpf_verifier_env *env, struct func_instance *instance)\nkernel/bpf/liveness.c-547-{\n--\nkernel/bpf/liveness.c=553=static int spi_off(int spi)\n--\nkernel/bpf/liveness.c-562- */\nkernel/bpf/liveness.c:563:static char *fmt_spis_mask(struct bpf_verifier_env *env, int frame, bool first,\nkernel/bpf/liveness.c-564-\t\t\t const unsigned long *spis, u32 words)\n--\nkernel/bpf/liveness.c-608-\nkernel/bpf/liveness.c:609:static void print_instance(struct bpf_verifier_env *env, struct func_instance *instance)\nkernel/bpf/liveness.c-610-{\n--\nkernel/bpf/liveness.c=668=static int cmp_instances(const void *pa, const void *pb)\n--\nkernel/bpf/liveness.c-682-/* print use/def slots for all instances ordered by callsite first, then by depth */\nkernel/bpf/liveness.c:683:static int print_instances(struct bpf_verifier_env *env)\nkernel/bpf/liveness.c-684-{\n--\nkernel/bpf/liveness.c=777=static bool arg_is_fp(const struct arg_track *at)\n--\nkernel/bpf/liveness.c-781-\nkernel/bpf/liveness.c:782:static void verbose_arg_track(struct bpf_verifier_env *env, struct arg_track *at)\nkernel/bpf/liveness.c-783-{\n--\nkernel/bpf/liveness.c=898=static struct arg_track __arg_track_join(struct arg_track a, struct arg_track b)\n--\nkernel/bpf/liveness.c-938-\nkernel/bpf/liveness.c:939:static bool arg_track_join(struct bpf_verifier_env *env, int idx, int target, int r,\nkernel/bpf/liveness.c-940-\t\t\t struct arg_track *in, struct arg_track out)\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=1081=static int fp_off_to_slot(s16 off, u32 nslots)\n--\nkernel/bpf/liveness.c-1089-\nkernel/bpf/liveness.c:1090:static struct arg_track fill_from_stack(struct bpf_insn *insn,\nkernel/bpf/liveness.c-1091-\t\t\t\t\tstruct arg_track *at_out, int reg,\n--\nkernel/bpf/liveness.c-1129- */\nkernel/bpf/liveness.c:1130:static void spill_to_stack(struct bpf_insn *insn, struct arg_track *at_out,\nkernel/bpf/liveness.c-1131-\t\t\t int reg, struct arg_track *at_stack_out, u32 nslots,\n--\nkernel/bpf/liveness.c=1171=static void clear_overlapping_stack_slots(struct arg_track *at_stack, u32 nslots, s16 off,\n--\nkernel/bpf/liveness.c-1196- */\nkernel/bpf/liveness.c:1197:static void clear_stack_for_all_offs(struct bpf_insn *insn,\nkernel/bpf/liveness.c-1198-\t\t\t\t struct arg_track *at_out, int reg,\n--\nkernel/bpf/liveness.c-1222-\nkernel/bpf/liveness.c:1223:static void arg_track_log(struct bpf_verifier_env *env, struct bpf_insn *insn, int idx,\nkernel/bpf/liveness.c-1224-\t\t\t struct arg_track *at_in, struct arg_track *at_stack_in,\n--\nkernel/bpf/liveness.c=1272=static bool can_be_local_fp(int depth, int regno, struct arg_track *at)\n--\nkernel/bpf/liveness.c-1282- */\nkernel/bpf/liveness.c:1283:static void arg_track_xfer(struct bpf_verifier_env *env, struct bpf_insn *insn,\nkernel/bpf/liveness.c-1284-\t\t\t int insn_idx,\n--\nkernel/bpf/liveness.c=1429=static int record_stack_access_off(struct func_instance *instance, s64 fp_off,\n--\nkernel/bpf/liveness.c-1464- */\nkernel/bpf/liveness.c:1465:static int record_stack_access(struct bpf_verifier_env *env, struct func_instance *instance,\nkernel/bpf/liveness.c-1466-\t\t\t const struct arg_track *arg,\n--\nkernel/bpf/liveness.c-1493- */\nkernel/bpf/liveness.c:1494:static int record_imprecise(struct bpf_verifier_env *env, struct func_instance *instance,\nkernel/bpf/liveness.c-1495-\t\t\t u32 mask, u32 insn_idx)\n--\nkernel/bpf/liveness.c-1512-/* Record load/store access for a given 'at' state of 'insn'. */\nkernel/bpf/liveness.c:1513:static int record_load_store_access(struct bpf_verifier_env *env,\nkernel/bpf/liveness.c-1514-\t\t\t\t struct func_instance *instance,\n--\nkernel/bpf/liveness.c-1579-\nkernel/bpf/liveness.c:1580:static int record_arg_access(struct bpf_verifier_env *env,\nkernel/bpf/liveness.c-1581-\t\t\t struct func_instance *instance,\n--\nkernel/bpf/liveness.c-1616-/* Record stack access for a given 'at' state of helper/kfunc 'insn' */\nkernel/bpf/liveness.c:1617:static int record_call_access(struct bpf_verifier_env *env,\nkernel/bpf/liveness.c-1618-\t\t\t struct func_instance *instance,\n--\nkernel/bpf/liveness.c-1658- */\nkernel/bpf/liveness.c:1659:static int find_callback_subprog(struct bpf_verifier_env *env,\nkernel/bpf/liveness.c-1660-\t\t\t\t struct bpf_insn *insn, int insn_idx,\n--\nkernel/bpf/liveness.c=1707=struct subprog_at_info {\n--\nkernel/bpf/liveness.c-1711-\nkernel/bpf/liveness.c:1712:static void print_subprog_arg_access(struct bpf_verifier_env *env,\nkernel/bpf/liveness.c-1713-\t\t\t\t int subprog,\n--\nkernel/bpf/liveness.c-1801- */\nkernel/bpf/liveness.c:1802:static int compute_subprog_args(struct bpf_verifier_env *env,\nkernel/bpf/liveness.c-1803-\t\t\t\tstruct subprog_at_info *info,\n--\nkernel/bpf/liveness.c=2037=static void free_instance(struct func_instance *instance)\n--\nkernel/bpf/liveness.c-2054- */\nkernel/bpf/liveness.c:2055:static int analyze_subprog(struct bpf_verifier_env *env,\nkernel/bpf/liveness.c-2056-\t\t\t struct arg_track *entry_args,\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=2280=static inline u16 mask_hi(u32 m) { return (u16)(m \u003e\u003e 16); }\n--\nkernel/bpf/liveness.c-2282-/* Compute info-\u003e{use,def} fields for the instruction */\nkernel/bpf/liveness.c:2283:static void compute_insn_live_regs(struct bpf_verifier_env *env,\nkernel/bpf/liveness.c-2284-\t\t\t\t struct bpf_insn *insn,\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"
} |
| 7/2 | 2026/09/26 22:35 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 83336 output: 28 thoughts: 384 Thoughts: **My Deep Dive into BPF Stack Analysis** Okay, let's get down to brass tacks. I need to understand how the BPF verifier handles stack safety, so I'm going to start by looking at key functions and files. My initial focus will be on the functions: `bpf_compute_subprog_arg_access()`, `bpf_stack_slot_alive()`, and `bpf_live_stack_query_init()`. These seem like good starting points for understanding the core stack analysis logic. Then I'll shift over to `kernel/bpf/verifier.c`. Specifically, I'll pay close attention to: * `bpf_check`: This is the high-level entry point, so it'll provide context. * `check_stack_write_fixed_off`, `check_stack_read_fixed_off`, `check_stack_write_var_off`: These are critical for analyzing different stack access patterns. * `check_max_stack_depth_subprog`: Stack depth constraints are crucial for safety. * `check_cond_jmp_op`: I want to understand how conditional jumps impact the stack state. * `bpf_prog_reenters_datapath`: This will be important for understanding how the verifier handles interaction with the data path. Next, I need to understand the control flow aspects in `kernel/bpf/backtrack.c`, looking at these: * `bpf_push_jmp_history` * `bpf_mark_chain_precision` * `backtrack_insn` The `kernel/bpf/states.c` file looks promising too. I'll need to explore the following functions: * `bpf_id_scratch_reserve` * `stacksafe` * `propagate_precision` And since stack size matters, I'll check `arch/x86/net/bpf_jit_comp.c`, specifically `bpf_jit_supports_large_stack`. This helps understand how architecture-specific JIT optimizations factor in. Now, I want to drill down on `bpf_id_scratch_reserve`. Where is this function actually *used*? I'm seeing calls in both `kernel/bpf/states.c`: `check_ids`, `bpf_id_scratch_reserve`, and `kernel/bpf/verifier.c`: `idstack_push`, `idset_cnt_inc`. This function looks fundamental. Finally, let's make sure the BPF verifier is actually enabled. I need to confirm `CONFIG_BPF_SYSCALL` is set in the kernel configuration. I'll verify this by checking the `kernel/bpf/Makefile`. This is foundational, otherwise I'm barking up the wrong tree. |
| 8/2 | 2026/09/26 22:35 | tool | grepper |
0mArgs:{
"Expression": "verifier.o",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/Makefile=7=CFLAGS_core.o += -Wno-override-init $(cflags-nogcse-yy)\nkernel/bpf/Makefile-8-\nkernel/bpf/Makefile:9:obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o cnum.o log.o token.o liveness.o const_fold.o diagnostics.o\nkernel/bpf/Makefile-10-obj-$(CONFIG_BPF_SYSCALL) += bpf_iter.o map_iter.o task_iter.o prog_iter.o link_iter.o\n--\nkernel/bpf/bpf_lsm.c=122=int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,\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=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/btf.c=6336=static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr,\n--\nkernel/bpf/btf.c-6352-\nkernel/bpf/btf.c:6353:\t/* user could have requested verbose verifier output\nkernel/bpf/btf.c-6354-\t * and supplied buffer to store the verification trace\n--\nkernel/bpf/cgroup.c=2085=const struct bpf_prog_ops cg_dev_prog_ops = {\n--\nkernel/bpf/cgroup.c-2087-\nkernel/bpf/cgroup.c:2088:const struct bpf_verifier_ops cg_dev_verifier_ops = {\nkernel/bpf/cgroup.c-2089-\t.get_func_proto\t\t= cgroup_dev_func_proto,\n--\nkernel/bpf/cgroup.c=2640=static u32 sysctl_convert_ctx_access(enum bpf_access_type type,\n--\nkernel/bpf/cgroup.c-2703-\nkernel/bpf/cgroup.c:2704:const struct bpf_verifier_ops cg_sysctl_verifier_ops = {\nkernel/bpf/cgroup.c-2705-\t.get_func_proto\t\t= sysctl_func_proto,\n--\nkernel/bpf/cgroup.c=2918=static int cg_sockopt_get_prologue(struct bpf_insn *insn_buf,\n--\nkernel/bpf/cgroup.c-2926-\nkernel/bpf/cgroup.c:2927:const struct bpf_verifier_ops cg_sockopt_verifier_ops = {\nkernel/bpf/cgroup.c-2928-\t.get_func_proto\t\t= cg_sockopt_func_proto,\n--\nkernel/bpf/fixups.c=826=int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)\n--\nkernel/bpf/fixups.c-828-\tstruct bpf_subprog_info *subprogs = env-\u003esubprog_info;\nkernel/bpf/fixups.c:829:\tconst struct bpf_verifier_ops *ops = env-\u003eops;\nkernel/bpf/fixups.c-830-\tint i, cnt, size, ctx_field_size, ret, delta = 0, epilogue_cnt = 0;\n--\nkernel/bpf/helpers.c-35-/* If kernel subsystem is allowing eBPF programs to call this function,\nkernel/bpf/helpers.c:36: * inside its own verifier_ops-\u003eget_func_proto() callback it should return\nkernel/bpf/helpers.c-37- * bpf_map_lookup_elem_proto, so that verifier can properly check the arguments\n--\nkernel/bpf/syscall.c=6646=syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)\n--\nkernel/bpf/syscall.c-6662-\nkernel/bpf/syscall.c:6663:const struct bpf_verifier_ops bpf_syscall_verifier_ops = {\nkernel/bpf/syscall.c-6664-\t.get_func_proto = syscall_prog_func_proto,\n--\nkernel/bpf/task_iter.c=992=__bpf_kfunc struct vm_area_struct *bpf_iter_task_vma_next(struct bpf_iter_task_vma *it)\n--\nkernel/bpf/task_iter.c-1010-\t/*\nkernel/bpf/task_iter.c:1011:\t * The verifier only trusts vm_mm and vm_file (see\nkernel/bpf/task_iter.c-1012-\t * BTF_TYPE_SAFE_TRUSTED_OR_NULL in verifier.c). Take a reference\n--\nkernel/bpf/trampoline.c-17-/* dummy _ops. The verifier will operate on target program's ops. */\nkernel/bpf/trampoline.c:18:const struct bpf_verifier_ops bpf_extension_verifier_ops = {\nkernel/bpf/trampoline.c-19-};\n--\nkernel/bpf/verifier.c-40-\nkernel/bpf/verifier.c:41:static const struct bpf_verifier_ops * const bpf_verifier_ops[] = {\nkernel/bpf/verifier.c-42-#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \\\nkernel/bpf/verifier.c:43:\t[_id] = \u0026 _name ## _verifier_ops,\nkernel/bpf/verifier.c-44-#define BPF_MAP_TYPE(_id, _ops)\n--\nkernel/bpf/verifier.c=7770=enum {\n--\nkernel/bpf/verifier.c-7781- * For traditional PTR_TO_MAP_VALUE or PTR_TO_BTF_ID | MEM_ALLOC, the verifier\nkernel/bpf/verifier.c:7782: * clears reg-\u003eid after value_or_null-\u003evalue transition, since the verifier only\nkernel/bpf/verifier.c-7783- * cares about the range of access to valid map value pointer and doesn't care\n--\nkernel/bpf/verifier.c=15546=static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-15720-\t\t\t\tenv, \"This operation subtracts pointer register R%d from scalar register R%d. \"\nkernel/bpf/verifier.c:15721:\t\t\t\t\"The verifier only tracks pointer-minus-scalar arithmetic for allowed pointer types.\",\nkernel/bpf/verifier.c-15722-\t\t\t\tptr_regno, dst);\n--\nkernel/bpf/verifier.c=18712=bool bpf_get_call_summary(struct bpf_verifier_env *env, struct bpf_insn *call,\n--\nkernel/bpf/verifier.c-18777- *\nkernel/bpf/verifier.c:18778: * - if so, and if verifier or current JIT inlines the call to the\nkernel/bpf/verifier.c-18779- * bpf_fastcall function (e.g. a helper call), kernel removes unnecessary\n--\nkernel/bpf/verifier.c=20920=static int check_struct_ops_btf_id(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-21037-\tprog-\u003eaux-\u003eattach_func_name = mname;\nkernel/bpf/verifier.c:21038:\tenv-\u003eops = st_ops-\u003everifier_ops;\nkernel/bpf/verifier.c-21039-\n--\nkernel/bpf/verifier.c=21671=static int check_attach_btf_id(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-21710-\t\t */\nkernel/bpf/verifier.c:21711:\t\tenv-\u003eops = bpf_verifier_ops[tgt_prog-\u003etype];\nkernel/bpf/verifier.c-21712-\t\tprog-\u003eexpected_attach_type = tgt_prog-\u003eexpected_attach_type;\n--\nkernel/bpf/verifier.c=22380=int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,\n--\nkernel/bpf/verifier.c-22390-\t/* no program is valid */\nkernel/bpf/verifier.c:22391:\tif (ARRAY_SIZE(bpf_verifier_ops) == 0)\nkernel/bpf/verifier.c-22392-\t\treturn -EINVAL;\n--\nkernel/bpf/verifier.c-22402-\tenv-\u003eprog = *prog;\nkernel/bpf/verifier.c:22403:\tenv-\u003eops = bpf_verifier_ops[env-\u003eprog-\u003etype];\nkernel/bpf/verifier.c-22404-\tenv-\u003estack_limit = bpf_prog_stack_limit(env-\u003eprog);\n--\nkernel/bpf/verifier.c-22414-\nkernel/bpf/verifier.c:22415:\t/* user could have requested verbose verifier output\nkernel/bpf/verifier.c-22416-\t * and supplied buffer to store the verification trace\n"
} |
| 9/2 | 2026/09/26 22:35 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 87090 output: 34 thoughts: 263 Thoughts: **Examining BPF JIT Configuration**
Okay, let's dive into this BPF JIT business. I need to understand how the system's configured. First, I'll check the BPF JIT configuration options themselves. I'm going to focus on `arch/x86/net/bpf_jit_comp.c` to see if `BPF_JIT` is even required here. Let's see what the code says about `bpf_prog_stack_limit`. Aha, I see:
```c
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;
}
```
The key is that `prog->jit_requested` flag. I know this flag usually gets set when `CONFIG_BPF_JIT` is enabled, or potentially via the `bpf_jit_enable` sysctl. Okay, that makes sense. So, to round this out, I'm going to check the `arch/x86/net/Makefile` to see how `bpf_jit_comp.c` is actually compiled, to confirm the configuration's impact.
|
| 10/2 | 2026/09/26 22:35 | 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"
} |
| 11/2 | 2026/09/26 22:35 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 87884 output: 22 thoughts: 230 Thoughts: **My Analysis of BPF JIT and Large Stack Support** Okay, so I'm digging into this BPF JIT code, and I'm seeing this relationship with large stack support. I've got this line: `obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o`. Right, so the `bpf_jit_comp.o` file is only built when `CONFIG_BPF_JIT` is enabled. And, I see `bpf_jit_supports_large_stack()` is defined in `arch/x86/net/bpf_jit_comp.c`. That's where the architecture-specific support lives. But wait, there's a `__weak` definition of `bpf_jit_supports_large_stack()` in `kernel/bpf/core.c`! It's set to return `false` by default. Aha! So, when `CONFIG_BPF_JIT` *is* enabled on x86, the definition in `arch/x86/net/bpf_jit_comp.c` effectively overrides the weak one, and returns `true`. That makes sense. It's how we enable large stacks on x86, or the architecture of our choice. Now, if `CONFIG_BPF_JIT` is *not* enabled, the `prog->jit_requested` flag won't be set, and the `__weak` definition in `kernel/bpf/core.c` will kick in, returning `false` for the stack support. That's the control flow. So, in essence, `BPF_JIT` and potentially `BPF_SYSCALL` are key configurations here! I need to ensure those are configured correctly depending on the system architecture and requirements for BPF JIT and stack size. Time to confirm the details in the Kconfig files. |
| 12/2 | 2026/09/26 22:35 | tool | grepper |
0mArgs:{
"Expression": "config BPF_JIT\\b"
}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"
} |
| 13/2 | 2026/09/26 22:35 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 88636 output: 34 thoughts: 19 Thoughts: Here's a summary of my thought process, as requested: **Navigating the Kernel's BPF Configuration** Okay, so I'm trying to get a handle on the current configuration for BPF within the kernel. Let's see... the most logical starting point is probably the `Kconfig` file in the `kernel/bpf` directory. I'm specifically interested in seeing the main BPF configuration options. I recall there was a specific area for that, roughly around line 45, that I want to double-check. Let me jump there and confirm my memory. This is where the core BPF feature flags are likely defined. |
| 14/2 | 2026/09/26 22:35 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/Kconfig",
"FirstLine": 30,
"LineCount": 40
}Results: {
"Contents": " 30:\t\n 31:\tconfig BPF_SYSCALL\n 32:\t\tbool \"Enable bpf() system call\"\n 33:\t\tselect BPF\n 34:\t\tselect IRQ_WORK\n 35:\t\tselect NEED_TASKS_RCU\n 36:\t\tselect TASKS_TRACE_RCU\n 37:\t\tselect BINARY_PRINTF\n 38:\t\tselect NET_SOCK_MSG if NET\n 39:\t\tselect NET_XGRESS if NET\n 40:\t\tselect PAGE_POOL if NET\n 41:\t\tdefault n\n 42:\t\thelp\n 43:\t\t Enable the bpf() system call that allows to manipulate BPF programs\n 44:\t\t and maps via file descriptors.\n 45:\t\n 46:\tconfig BPF_JIT\n 47:\t\tbool \"Enable BPF Just In Time compiler\"\n 48:\t\tdepends on BPF\n 49:\t\tdepends on HAVE_CBPF_JIT || HAVE_EBPF_JIT\n 50:\t\tselect EXECMEM\n 51:\t\thelp\n 52:\t\t BPF programs are normally handled by a BPF interpreter. This option\n 53:\t\t allows the kernel to generate native code when a program is loaded\n 54:\t\t into the kernel. This will significantly speed-up processing of BPF\n 55:\t\t programs.\n 56:\t\n 57:\t\t Note, an admin should enable this feature changing:\n 58:\t\t /proc/sys/net/core/bpf_jit_enable\n 59:\t\t /proc/sys/net/core/bpf_jit_harden (optional)\n 60:\t\t /proc/sys/net/core/bpf_jit_kallsyms (optional)\n 61:\t\n 62:\tconfig BPF_JIT_ALWAYS_ON\n 63:\t\tbool \"Permanently enable BPF JIT and remove BPF interpreter\"\n 64:\t\tdepends on BPF_SYSCALL \u0026\u0026 HAVE_EBPF_JIT \u0026\u0026 BPF_JIT\n 65:\t\thelp\n 66:\t\t Enables BPF JIT and removes BPF interpreter to avoid speculative\n 67:\t\t execution of BPF instructions by the interpreter.\n 68:\t\n 69:\t\t When CONFIG_BPF_JIT_ALWAYS_ON is enabled, /proc/sys/net/core/bpf_jit_enable\n"
} |
| 15/2 | 2026/09/26 22:35 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 89436 output: 36 thoughts: 819 Thoughts: **Analyzing BPF Symbol Changes** Okay, so I'm looking at this diff, specifically focusing on the implications of `BPF_SYSCALL` and `BPF_JIT` being enabled. My goal is to understand how the BPF subsystem has changed and what those changes mean for the verifier, particularly around stack size handling. First, I need to identify the key symbols that have been added, removed, or modified. I'm using the diff output to generate a list of symbols to investigate. I'm focusing on "reachable code", which means looking at symbols that are accessible externally. In `kernel/bpf/backtrack.c`, I see a modification to `bpf_push_jmp_history`, which is interesting because that's involved in tracing the control flow. The parameter change (`const u16 *linked_regs, u8 linked_regs_cnt`) suggests potential changes in how it handles register tracking within the history. Other changes to `bpf_fmt_stack_mask`, `bpf_mark_all_scalars_precise`, and `bpf_mark_chain_precision` are also worth a closer look, especially given the context of stack mask manipulation. Moving to `kernel/bpf/states.c`, the new non-static function `bpf_id_scratch_reserve` stands out. This likely deals with allocating scratch space for ID maps and sets, which could be related to the new large stack support. `bpf_is_state_visited` has been modified, likely to use the updated `bpf_push_jmp_history`, so this change is important to consider. In `kernel/bpf/liveness.c`, I see several new non-static functions related to argument access and stack slot liveness: `bpf_compute_subprog_arg_access`, `bpf_live_stack_query_init`, and `bpf_stack_slot_alive`. My hunch is these are part of a revamped stack analysis. `bpf_compute_subprog_arg_access` specifically is what I am interested in. Now, focusing on `kernel/bpf/verifier.c`, `bpf_check` is, of course, the main entry point for the verifier. `bpf_clear_singular_ids` is modified, which probably integrates with the changes in scratch space allocation, and `bpf_bt_sync_linked_regs` is also modified, likely tied to the backtrack history. Finally, in `arch/x86/net/bpf_jit_comp.c`, I see `bpf_jit_supports_large_stack`, which is exactly what I would expect to see, given that we are enabling large stack support. To summarize, I've identified the key non-static functions affected: * `bpf_check`: The verifier entry point. * `bpf_push_jmp_history`: Backtrack history, tracking jumps and registers. * `bpf_mark_chain_precision`: Backtrack precision propagation, now probably supporting a large stack. * `bpf_id_scratch_reserve`: New helper for growing scratch buffers for ID maps and sets. * `bpf_compute_subprog_arg_access`: Liveness analysis entrypoint (key for stack tracking). * `bpf_clear_singular_ids`: Handles growing idsets. * `bpf_jit_supports_large_stack`: JIT support for large stacks. I'm now going to double-check that `bpf_compute_subprog_arg_access` is indeed called during verification to confirm my assumption that it is key to the new large stack support. |
| 16/2 | 2026/09/26 22:35 | tool | grepper |
0mArgs:{
"Expression": "bpf_compute_subprog_arg_access",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/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=2451=int bpf_compute_live_registers(struct bpf_verifier_env *env)\n--\nkernel/bpf/liveness.c-2495-\t/* Forward pass: resolve stack access through FP-derived pointers */\nkernel/bpf/liveness.c:2496:\terr = bpf_compute_subprog_arg_access(env);\nkernel/bpf/liveness.c-2497-\tif (err)\n"
} |
| 17/2 | 2026/09/26 22:35 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 91335 output: 276 thoughts: 1323 Thoughts: **My Assessment of the BPF Stack Limit Increase Patch** Okay, let's break this down systematically. I see that `bpf_compute_subprog_arg_access` is a key function here, called from `bpf_compute_live_registers`, which is in turn called by `bpf_check`. This points to a core part of the BPF verification process, especially given the significant refactoring of `compute_subprog_args` and `analyze_subprog` – clearly they're handling things like spill slots and dynamic frame mask resizing. We also have `bpf_mark_chain_precision`, `bpf_push_jmp_history`, and `bpf_id_scratch_reserve`, which deal with backtracking, precision tracking, and scratch buffer reallocation, respectively. `bpf_bt_sync_linked_regs` is non-static and defined in the verifier code, which makes it a potential point of interest. I'll need to check if there are any static functions being inlined, but the listed symbols (`bpf_check`, `bpf_push_jmp_history`, `bpf_mark_chain_precision`, `bpf_id_scratch_reserve`, `bpf_bt_sync_linked_regs`, `bpf_clear_singular_ids`, `bpf_compute_subprog_arg_access`, `bpf_jit_supports_large_stack`) all exist in the vmlinux image. Crucially, none of these functions are generic infrastructure components like allocators or lock implementations. They're all very specific to BPF verifier and JIT execution, and are, therefore, highly relevant. Given the substantial changes to the BPF stack limit (512 to 2048 bytes), and the fundamental shift in stack tracking – bitmasks replacing fixed-size structures, dynamic allocation of scratch tables – I'd say this patch is absolutely "WorthFuzzing". This touches core verifier and JIT logic directly. Now, let's verify the configuration dependencies. `BPF_SYSCALL` is essential, as it enables the `bpf()` syscall and, consequently, the verifier code (`kernel/bpf/verifier.c`, `liveness.c`, `backtrack.c`, `states.c`). Then, `BPF_JIT` enables the JIT compiler, which is where the increased stack limit is effective, and it needs `arch/x86/net/bpf_jit_comp.c`. The code explicitly shows that `bpf_prog_stack_limit` returns 2048 instead of 512 when JIT is enabled. I also need to verify that x86 JIT needs `CONFIG_BPF_JIT` which depends on `BPF` and `HAVE_EBPF_JIT` (selected by `X86_64`), as expected. The code inside `bpf_prog_stack_limit` confirms that, when the JIT is requested for a program, the large stack limit is used only if supported by the JIT and also supports subprog tailcalls. It also shows `BPF_JIT` being enabled. So, in summary, `BPF_SYSCALL` and `BPF_JIT` are the key configurations. My sanity check on the important functions (`bpf_check`, `bpf_push_jmp_history`, `bpf_mark_chain_precision`, `bpf_id_scratch_reserve`, `bpf_compute_subprog_arg_access`, `bpf_clear_singular_ids`, `bpf_bt_sync_linked_regs`, and `bpf_jit_supports_large_stack`) confirms that they are non-static symbols in the modified files. My conclusion is this: The BPF stack limit increase necessitates a complete overhaul of stack tracking in the verifier. Specifically: 1. Stack masks are evolving from 64-bit to multi-word bitmasks. 2. Instruction history is now packed into smaller, 16-bit structures. 3. Liveness analysis is rewritten to accommodate variable-width masks and more spill slots. 4. Scratch structures are dynamically allocated. 5. Verifier checks have been adapted to the increased stack limit. These changes are significant and touch critical parts of the BPF verifier logic, including security-critical verification logic. Fuzzing is warranted to uncover potential soundness issues, memory errors, or integer overflows. |
| 18/2 | 2026/09/26 22:35 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT"
],
"FocusSymbols": [
"bpf_check",
"bpf_push_jmp_history",
"bpf_mark_chain_precision",
"bpf_id_scratch_reserve",
"bpf_compute_subprog_arg_access",
"bpf_clear_singular_ids",
"bpf_bt_sync_linked_regs",
"bpf_jit_supports_large_stack"
],
"Reasoning": "The patch increases the maximum BPF program stack size from 512 bytes to 2048 bytes on JIT architectures supporting large stacks (x86_64 and arm64). To support this, significant changes are introduced to the BPF verifier and liveness/backtracking engine: replacing fixed 64-bit stack slot masks with dynamically sized bitmaps, refactoring jump history linked register tracking, dynamically reallocating scratch ID maps, and updating stack bounds checks throughout program verification. These modifications directly affect reachable kernel code executed during BPF program load via the bpf() syscall and warrant fuzzing for verifier bugs, memory leaks, and bounds inconsistencies.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT"
],
"FocusSymbols": [
"bpf_check",
"bpf_push_jmp_history",
"bpf_mark_chain_precision",
"bpf_id_scratch_reserve",
"bpf_compute_subprog_arg_access",
"bpf_clear_singular_ids",
"bpf_bt_sync_linked_regs",
"bpf_jit_supports_large_stack"
],
"Reasoning": "The patch increases the maximum BPF program stack size from 512 bytes to 2048 bytes on JIT architectures supporting large stacks (x86_64 and arm64). To support this, significant changes are introduced to the BPF verifier and liveness/backtracking engine: replacing fixed 64-bit stack slot masks with dynamically sized bitmaps, refactoring jump history linked register tracking, dynamically reallocating scratch ID maps, and updating stack bounds checks throughout program verification. These modifications directly affect reachable kernel code executed during BPF program load via the bpf() syscall and warrant fuzzing for verifier bugs, memory leaks, and bounds inconsistencies.",
"WorthFuzzing": true
} |
| 19/1 | 2026/09/26 22:35 | action | If |
2mArgs:{
"WorthFuzzing": true
}Results: null |
| 20/2 | 2026/09/26 22:35 | agent | kmsan-evaluator |
2mModel:core Args: null Results: {
"KMSANReasoning": "The patch series increases the maximum stack size budget for BPF programs from 512 bytes to 2048 bytes (MAX_BPF_STACK_JIT) on architectures with supporting JIT compilers (x86-64 and arm64). This requires adapting the BPF verifier's internal data structures and algorithms—including backtrack bitmasks, jump history entries, liveness analysis frame masks, and scratch id sets/maps—to handle up to 256 8-byte stack slots (or 512 4-byte half-slots).\n\nKASAN vs KMSAN applicability:\n1. Memory Safety Risks: The main risks of this change involve out-of-bounds accesses (e.g., if stack indexing or slot calculations exceed allocated bounds), bitmap indexing overflows, or memory management bugs (use-after-free, double free, memory leak) during dynamic reallocation of scratch buffers and liveness masks (e.g., in widen_frame_masks(), bpf_id_scratch_reserve(), or compute_subprog_args()). All of these conditions are directly caught by KASAN, KASAN-extra, and standard kernel bug detectors.\n2. Uninitialized Memory \u0026 Info Leaks: The patch does not introduce reads of uninitialized memory or expose kernel data to userspace.\n - Internal structures like `frame_masks` are allocated with `kvzalloc_flex()` (zero-initialized).\n - `realloc_array()` used for stack states explicitly zeroes out newly grown array elements.\n - Dynamic scratch structures (`bpf_idmap`, `bpf_idset`) and `jmp_history_entry` only access entries up to their explicitly maintained count (`cnt`, `num_ids`, `linked_regs_cnt`).\n - JITed BPF execution is not instrumented by KMSAN compiler passes, while interpreter execution remains capped at 512 bytes (and already unpoisons its interpreter stack buffer via `kmsan_unpoison_memory()`).\n\nBecause any potential memory corruption bugs arising from these changes fall squarely under KASAN detection and no uninitialized memory risks are introduced, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
}Instruction: You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.
CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
a comprehensive suite of debugging tools and sanitizers, including KASAN
(out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
(locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
or page allocations) and kernel-to-user memory info-leaks.
Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.
Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.
Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
attributes, ioctl output arguments, socket options, or BPF buffers) where fields
or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
uninitialized bytes of existing buffers.
Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
dereferences, locking deadlocks, or use-after-free bugs (these are already caught
by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.
Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit 0929b6d759163a715d7e79025cda7c32f2a904ad
Author: syz-cluster <triage@syzkaller.com>
Date: Sat Sep 26 22:34:46 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 44e3a9e683e8d..475e706534543 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -2503,6 +2503,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 73cb097859a12..1c787208ff829 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
@@ -243,55 +244,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) | \
@@ -421,30 +385,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];
@@ -530,12 +497,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;
}
@@ -551,7 +537,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. */
@@ -576,7 +562,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; \
@@ -735,7 +721,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.
@@ -867,7 +857,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];
};
@@ -876,18 +866,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() */
@@ -996,10 +995,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;
@@ -1014,8 +1011,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 */
@@ -1024,6 +1021,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 */
@@ -1059,7 +1058,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];
@@ -1265,8 +1264,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);
@@ -1283,6 +1284,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;
+}
+
/*
* Return the function state a stack pointer register refers to. frameno
* shares storage with other pointer metadata, so return NULL for any
@@ -1333,12 +1352,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)
@@ -1353,7 +1367,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);
@@ -1491,7 +1505,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)
@@ -1499,27 +1513,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)
@@ -1554,7 +1569,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 81b6de655b9be..aa72ad3ec6892 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -3496,6 +3496,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 012b82513a3bc..a1f7b4a87502a 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;
}
}
}
@@ -709,37 +706,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;
/*
@@ -749,8 +747,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;
@@ -763,21 +761,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
@@ -789,13 +785,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) ||
@@ -803,8 +799,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
@@ -820,8 +816,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;
@@ -968,6 +964,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;
@@ -1045,10 +1062,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;
@@ -1194,15 +1213,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;
}
@@ -1224,10 +1245,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;
@@ -1330,7 +1353,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;
@@ -1344,7 +1367,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;
@@ -1412,7 +1436,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 4c20e5bf8b69a..865b6c6eb8dd7 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);
@@ -3373,26 +3383,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;
};
@@ -3411,48 +3420,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;
}
}
@@ -3494,10 +3489,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];
@@ -3594,17 +3589,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);
}
@@ -3646,14 +3642,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,
@@ -3671,13 +3668,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;
@@ -3699,7 +3698,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) {
@@ -3727,7 +3726,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)
- clear_scalar_id(&state->stack[spi].spilled_ptr);
+ clear_scalar_id(&ss->spilled_ptr);
} 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];
@@ -3751,8 +3750,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);
@@ -3773,13 +3772,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;
}
@@ -3838,12 +3837,14 @@ static int check_stack_write_var_off(struct bpf_verifier_env *env,
/* 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) {
@@ -3866,9 +3867,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;
@@ -3939,23 +3939,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;
}
@@ -3966,8 +3965,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 {
@@ -4015,9 +4020,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);
@@ -4027,7 +4033,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--)
@@ -4153,7 +4159,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;
}
@@ -4342,7 +4348,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);
}
/*
@@ -4376,7 +4382,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)
@@ -5520,7 +5526,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;
@@ -5529,7 +5535,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++;
@@ -6530,10 +6536,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,
@@ -6544,7 +6551,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;
@@ -7428,6 +7435,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;
@@ -7437,7 +7445,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) ||
@@ -7449,13 +7458,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;
}
@@ -8185,7 +8194,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;
@@ -8318,7 +8327,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);
@@ -8395,16 +8404,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;
@@ -8416,7 +8424,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
@@ -9208,6 +9216,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;
@@ -9227,7 +9236,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--)
@@ -9238,14 +9248,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;
@@ -10521,8 +10531,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;
@@ -14381,11 +14392,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:
@@ -14471,7 +14482,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 */
@@ -15222,7 +15234,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;
@@ -15234,7 +15247,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:
@@ -15354,7 +15367,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;
@@ -15484,7 +15497,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;
@@ -17841,10 +17854,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);
}
}
@@ -17864,7 +17879,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))
@@ -17982,7 +17997,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;
}
@@ -18052,7 +18067,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;
}
@@ -18941,23 +18959,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 */
@@ -18983,6 +19005,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;
@@ -18991,9 +19014,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;
@@ -19450,7 +19481,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;
}
@@ -22326,6 +22357,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)
{
@@ -22350,6 +22401,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);
@@ -22683,6 +22737,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 dc2c4180ee803..8a6d341b754ac 100644
--- a/tools/testing/selftests/bpf/prog_tests/verifier.c
+++ b/tools/testing/selftests/bpf/prog_tests/verifier.c
@@ -64,6 +64,7 @@
#include "verifier_kfunc_uninit_multi.skel.h"
#include "verifier_kfunc_perfmon.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"
@@ -248,6 +249,7 @@ void test_verifier_kfunc_uninit_multi(void) { RUN_TESTS(verifier_kfunc_uninit_
void test_verifier_kfunc_perfmon(void) { RUN(verifier_kfunc_perfmon); }
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,
|
| 21/3 | 2026/09/26 22:35 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 75839 output: 28 thoughts: 1490 Thoughts: **KMSAN Evaluation for BPF Stack Expansion** Okay, so the task is to determine if this kernel patch, which expands the BPF stack size, necessitates a dedicated KMSAN fuzzing session. I need to be precise here, as I know that running KMSAN where it's unnecessary is a waste of resources. Standard KASAN is already enabled, so I need to focus on what KMSAN uniquely detects – specifically, uninitialized memory reads (stack, heap, page allocations) and kernel-to-user memory leaks. My critical rule is: if KASAN, LOCKDEP, or standard detectors can catch it, no KMSAN is needed. I'm looking for these specific scenarios that would trigger a KMSAN requirement: 1. Structures sent to user space where uninitialized fields could be exposed. 2. Conditional logic that relies on uninitialized variables. 3. Complex data structure allocations where uninitialized fields are later read. 4. Potential for reading uninitialized bytes due to bounds check issues. If it's just about out-of-bounds, overflows, NULL derefs, or locking issues, then standard KASAN should be sufficient, assuming all stack/heap structures are properly initialized. Now, let's dive into the patch. This patch increases the BPF stack size to 2 KiB on architectures supporting larger stacks – x86-64 and arm64. I see changes in the documentation and in the architecture-specific JIT compiler code, specifically implementing `bpf_jit_supports_large_stack()`. The netronome driver's BPF verifier is being updated to use `bpf_stack_slot()`. There are changes to the BPF verifier header, with new definitions for stack slot calculations and dynamic structures. Some structs like `spis_t` are gone, and we now have dynamic bitmasks and helper functions like `bpf_stack_nr_slots()` and `bpf_stack_slot()`. We also have some dynamic allocations with `kvzalloc_flex` and `kvmalloc_objs` now. The patch includes updates to the backtrack functions, diagnostics, and liveness analysis. I notice flexible/dynamic frame masks and spill slot changes in `liveness.c` using `kvmalloc_objs` and `kvmalloc_flex`. There are also changes to idmap management, stack limit checks within the verifier. Wait! Could any of this affect uninitialized memory? The verifier itself is key here. Does the verifier have adequate uninitialized stack tracking? My initial question is how BPF handles uninitialized stack memory. It's normally forbidden to read from uninitialized stack slots. But, does `env->allow_uninit_stack` change that behavior, or do certain helpers or functions break that rule? Okay, *crucially*, if a BPF program runs *in the kernel* does increasing the stack to 2048 bytes create an increased risk of reading uninitialized *kernel* stack? I remember that BPF stack memory is allocated on the *kernel* thread stack for JITed programs (x86-64 and arm64): `sub rsp, frame_size`! I remember this. Thus, if a BPF program allocates 2048 bytes on the *kernel* stack does the verifier verify the memory *is initialized* before read? That's what I need to understand. How does the BPF verifier ensure stack initialization? I'll need to search the verifier code for how it handles stack initialization *and* reads to properly determine whether we need KMSAN. |
| 22/3 | 2026/09/26 22:35 | 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=670=static bool is_stack_misc_after(struct bpf_verifier_env *env,\n--\nkernel/bpf/states.c-676-\t\tif ((stack-\u003eslot_type[i] == STACK_MISC) ||\nkernel/bpf/states.c:677:\t\t ((stack-\u003eslot_type[i] == STACK_INVALID || stack-\u003eslot_type[i] == STACK_POISON) \u0026\u0026\nkernel/bpf/states.c-678-\t\t env-\u003eallow_uninit_stack))\n--\nkernel/bpf/states.c=698=static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,\n--\nkernel/bpf/states.c-719-\t\tif (exact == EXACT) {\nkernel/bpf/states.c:720:\t\t\tu8 cur_type = cur_slot ? cur_slot-\u003eslot_type[im] : STACK_INVALID;\nkernel/bpf/states.c-721-\nkernel/bpf/states.c:722:\t\t\t/* STACK_INVALID and STACK_POISON are equivalent for pruning */\nkernel/bpf/states.c-723-\t\t\tif (old_type == STACK_POISON)\nkernel/bpf/states.c:724:\t\t\t\told_type = STACK_INVALID;\nkernel/bpf/states.c-725-\t\t\tif (cur_type == STACK_POISON)\nkernel/bpf/states.c:726:\t\t\t\tcur_type = STACK_INVALID;\nkernel/bpf/states.c-727-\t\t\tif (!cur_slot || old_type != cur_type)\n--\nkernel/bpf/states.c-730-\nkernel/bpf/states.c:731:\t\tif (old_type == STACK_INVALID || old_type == STACK_POISON)\nkernel/bpf/states.c-732-\t\t\tcontinue;\n--\nkernel/bpf/states.c-826-\t\tcase STACK_ZERO:\nkernel/bpf/states.c:827:\t\tcase STACK_INVALID:\nkernel/bpf/states.c-828-\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=3803=static int check_stack_write_var_off(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-3856-\t\t\t *\nkernel/bpf/verifier.c:3857:\t\t\t * We do however catch STACK_INVALID case below, and\nkernel/bpf/verifier.c-3858-\t\t\t * only allow reading possibly uninitialized memory\n--\nkernel/bpf/verifier.c-3892-\t\t}\nkernel/bpf/verifier.c:3893:\t\t/* If the slot is STACK_INVALID, we check whether it's OK to\nkernel/bpf/verifier.c-3894-\t\t * pretend that it will be initialized by this write. The slot\n--\nkernel/bpf/verifier.c-3901-\t\t */\nkernel/bpf/verifier.c:3902:\t\tif ((*stype == STACK_INVALID || *stype == STACK_POISON) \u0026\u0026\nkernel/bpf/verifier.c-3903-\t\t !env-\u003eallow_uninit_stack) {\n--\nkernel/bpf/verifier.c=4010=static int check_stack_read_fixed_off(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-4081-\t\t\t\t\t}\nkernel/bpf/verifier.c:4082:\t\t\t\t\tif (type == STACK_INVALID \u0026\u0026 env-\u003eallow_uninit_stack)\nkernel/bpf/verifier.c-4083-\t\t\t\t\t\tcontinue;\n--\nkernel/bpf/verifier.c-4140-\t\t\t\tcontinue;\nkernel/bpf/verifier.c:4141:\t\t\tif (type == STACK_INVALID \u0026\u0026 env-\u003eallow_uninit_stack)\nkernel/bpf/verifier.c-4142-\t\t\t\tcontinue;\n--\nkernel/bpf/verifier.c=7370=static int check_stack_range_initialized(\n--\nkernel/bpf/verifier.c-7452-\t\tif ((*stype == STACK_ZERO) ||\nkernel/bpf/verifier.c:7453:\t\t (*stype == STACK_INVALID \u0026\u0026 (uninit || env-\u003eallow_uninit_stack))) {\nkernel/bpf/verifier.c:7454:\t\t\tif (clobber \u0026\u0026 (*stype != STACK_INVALID || env-\u003eallow_uninit_stack)) {\nkernel/bpf/verifier.c-7455-\t\t\t\t/* helper can write anything into the stack */\n"
} |
| 23/3 | 2026/09/26 22:35 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 80456 output: 34 thoughts: 50 Thoughts: Here's my analysis of the provided code snippet, thinking like an expert: **Investigating Uninitialized Stack Access in BPF Programs** Alright, let's break this down. First, I see an assignment: `env->allow_uninit_stack = bpf_allow_uninit_stack(env->prog->aux->token);`. My initial focus is immediately on the significance of this `env->allow_uninit_stack` flag. This *strongly* suggests we're dealing with a check or setting that governs whether or not the BPF program (referred to here as `env->prog`) is permitted to access uninitialized memory on its stack. That's a potential security vulnerability, or at the very least, a source of unpredictable behavior if it's mishandled. The crucial element here, the one I need to fully understand, is the `bpf_allow_uninit_stack()` function itself. It's taking `env->prog->aux->token` as an argument. The token *likely* plays a key role, maybe some sort of permission or configuration metadata. I need to dig into the definition of this `bpf_allow_uninit_stack` function. I will need to understand how it interprets the token. Is the token a bitfield? Is it a hash? Is it based on the program metadata or configuration? I need to understand what controls the value returned by this function, as it is going to determine whether uninitialized stack access is permitted or denied. My focus now is squarely on the semantics of `bpf_allow_uninit_stack` and its interaction with that `token`. That's where the answer lies. |
| 24/3 | 2026/09/26 22:35 | tool | grepper |
0mArgs:{
"Expression": "bpf_allow_uninit_stack",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/verifier.c=22380=int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,\n--\nkernel/bpf/verifier.c-22408-\tenv-\u003eallow_ptr_leaks = bpf_allow_ptr_leaks(env-\u003eprog-\u003eaux-\u003etoken);\nkernel/bpf/verifier.c:22409:\tenv-\u003eallow_uninit_stack = bpf_allow_uninit_stack(env-\u003eprog-\u003eaux-\u003etoken);\nkernel/bpf/verifier.c-22410-\tenv-\u003ebypass_spec_v1 = bpf_bypass_spec_v1(env-\u003eprog-\u003eaux-\u003etoken);\n"
} |
| 25/3 | 2026/09/26 22:35 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 81087 output: 23 thoughts: 19 Thoughts: Here's my take on those thoughts: **Tracing the Source of `bpf_allow_uninit_stack`** Okay, so I need to understand where `bpf_allow_uninit_stack` is defined. This likely means I'm trying to figure out its scope, usage, and implications. I'll need to dive into the kernel source, probably starting with a quick grep or a more sophisticated code navigation tool. Finding the definition will be the first step; from there, I can analyze the surrounding code to determine its purpose and how it impacts BPF program behavior, especially in relation to uninitialized stack usage. Understanding this is crucial for [**insert relevant field of expertise, e.g., security analysis, performance optimization, BPF program design**] because [**insert specific reason relevant to that field, e.g., it could expose potential vulnerabilities, it impacts stack frame allocation, it determines the level of control for this feature**]. |
| 26/3 | 2026/09/26 22:35 | tool | grepper |
0mArgs:{
"Expression": "bpf_allow_uninit_stack"
}Results: {
"Output": "include/linux/bpf.h=2975=static inline bool bpf_allow_ptr_leaks(const struct bpf_token *token)\n--\ninclude/linux/bpf.h-2979-\ninclude/linux/bpf.h:2980:static inline bool bpf_allow_uninit_stack(const struct bpf_token *token)\ninclude/linux/bpf.h-2981-{\n--\nkernel/bpf/verifier.c=22380=int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,\n--\nkernel/bpf/verifier.c-22408-\tenv-\u003eallow_ptr_leaks = bpf_allow_ptr_leaks(env-\u003eprog-\u003eaux-\u003etoken);\nkernel/bpf/verifier.c:22409:\tenv-\u003eallow_uninit_stack = bpf_allow_uninit_stack(env-\u003eprog-\u003eaux-\u003etoken);\nkernel/bpf/verifier.c-22410-\tenv-\u003ebypass_spec_v1 = bpf_bypass_spec_v1(env-\u003eprog-\u003eaux-\u003etoken);\n"
} |
| 27/3 | 2026/09/26 22:35 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 81630 output: 37 thoughts: 22 Thoughts: Here's a summary of my thought process, tailored for an expert audience: **Diving into BPF Header Definitions** Alright, let's zero in on a specific area within the BPF landscape. My immediate thought is to examine the `include/linux/bpf.h` header file, and I'm particularly interested in what's happening around line 2980. Given that this is the kernel BPF header, my initial expectation is to find potentially crucial structure definitions, enum declarations, or macro definitions related to BPF program loading, instruction sets, or perhaps even helper function interfaces. There could be key details regarding internal BPF interpreter behavior that are relevant to performance optimization or security. I'll be looking for any hints on how the kernel exposes BPF functionality. What exactly is defined around there? |
| 28/3 | 2026/09/26 22:35 | tool | read-file |
0mArgs:{
"File": "include/linux/bpf.h",
"FirstLine": 2975,
"LineCount": 20
}Results: {
"Contents": "2975:\tstatic inline bool bpf_allow_ptr_leaks(const struct bpf_token *token)\n2976:\t{\n2977:\t\treturn bpf_token_capable(token, CAP_PERFMON);\n2978:\t}\n2979:\t\n2980:\tstatic inline bool bpf_allow_uninit_stack(const struct bpf_token *token)\n2981:\t{\n2982:\t\treturn bpf_token_capable(token, CAP_PERFMON);\n2983:\t}\n2984:\t\n2985:\tstatic inline bool bpf_bypass_spec_v1(const struct bpf_token *token)\n2986:\t{\n2987:\t\treturn bpf_jit_bypass_spec_v1() ||\n2988:\t\t\tcpu_mitigations_off() ||\n2989:\t\t\tbpf_token_capable(token, CAP_PERFMON);\n2990:\t}\n2991:\t\n2992:\tstatic inline bool bpf_bypass_spec_v4(const struct bpf_token *token)\n2993:\t{\n2994:\t\treturn bpf_jit_bypass_spec_v4() ||\n"
} |
| 29/3 | 2026/09/26 22:35 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 82192 output: 37 thoughts: 33 Thoughts: Okay, let's dive into this. Here's my thought process on this: **Inspecting `grow_stack_state` in `kernel/bpf/verifier.c`** Wait a minute, I need to pause here. I need to take a closer look at `grow_stack_state`. I recall seeing that function in `kernel/bpf/verifier.c`, and I have a feeling that might be the key to understanding this. Let me pull up that file and focus specifically on `grow_stack_state` now. I need to understand its logic and how it relates to the current issue. Perhaps by understanding how the stack state is grown, I can identify the source of the problem. Time to dig in! |
| 30/3 | 2026/09/26 22:36 | 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"
} |
| 31/3 | 2026/09/26 22:36 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 83132 output: 29 thoughts: 35 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience: **Analyzing `realloc_array` Behavior** Okay, so I'm thinking about `realloc_array`. The core question here is how this function handles new memory allocated during a resize. Specifically, does it initialize the newly allocated space with zeros, or is it left uninitialized? This is a crucial detail for memory safety and potential security vulnerabilities, depending on how the data is handled later. I really need to verify the implementation. Time to dive into the `realloc_array` source code and confirm the initialization behavior. |
| 32/3 | 2026/09/26 22:36 | tool | grepper |
0mArgs:{
"Expression": "realloc_array",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/core.c=831=int bpf_jit_add_poke_descriptor(struct bpf_prog *prog,\n--\nkernel/bpf/core.c-853-\nkernel/bpf/core.c:854:\ttab = krealloc_array(tab, size, sizeof(*poke), GFP_KERNEL);\nkernel/bpf/core.c-855-\tif (!tab)\n--\nkernel/bpf/states.c=972=bool bpf_id_scratch_reserve(void **arr, u32 *cap, u32 cnt, size_t elem_size)\n--\nkernel/bpf/states.c-979-\tnew_cap = *cap ? *cap * 2 : 64;\nkernel/bpf/states.c:980:\tp = krealloc_array(*arr, new_cap, elem_size, GFP_KERNEL_ACCOUNT | __GFP_NOWARN);\nkernel/bpf/states.c-981-\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"
} |
| 33/3 | 2026/09/26 22:36 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 84137 output: 37 thoughts: 29 Thoughts: Okay, let's dive into this. Here's how I'd approach that kernel code: **Diving into the BPF Verifier's Memory Allocation** Alright, I need to get a handle on the memory allocation strategy within the BPF verifier, specifically, let's pinpoint the `realloc_array` implementation. I know it's a common area for potential vulnerabilities, and understanding its behavior is critical. My focus is going to be around line 1372 within `kernel/bpf/verifier.c`. I'll be keeping a sharp eye out for potential overflow issues, incorrect size calculations, or any unusual memory management patterns that might be exploitable. This is where the rubber meets the road in terms of BPF's security; any weakness here could be leveraged to wreak havoc. Let's see how they're handling that re-allocation... I'm expecting to look for edge-case checks, error handling, and of course, careful arithmetic, given the potential for exploitation. |
| 34/3 | 2026/09/26 22:36 | 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"
} |
| 35/3 | 2026/09/26 22:36 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 84910 output: 41 thoughts: 194 Thoughts: Okay, let me break this down. **Initial Thoughts on `realloc_array` and BPF Stack Initialization** Right, so I'm looking at `realloc_array` and I'm seeing this `memset` call: `if (new_n > old_n) memset(arr + old_n * size, 0, (new_n - old_n) * size);`. And I'm recalling that `STACK_INVALID` is defined as 0. This means that whenever we expand the stack in `realloc_array`, we're initializing the *new* slots with zeroes. Which, in turn, suggests that newly allocated stack slots will have their `slot_type` as `STACK_INVALID` (because it's zero) and the `spilled_ptr` will also be zero. That's a reasonable starting point. But then, the alarm bells are going off. That's fine for our management of it, but what about the *actual* BPF program execution? Specifically, what happens on the kernel stack during BPF program execution itself? How are stack frames handled when the BPF program is running? The key question is, when the JIT (Just-In-Time) compiler generates code for x86-64 or arm64, does it explicitly *initialize* the stack frame as part of its prologue? Or is that assumed to be a responsibility of higher-level mechanisms, or not done at all which gives us an opportunity? My next step is clear. Time to dig into the JIT code, specifically `arch/x86/net/bpf_jit_comp.c`, to check how the stack frame is actually allocated within the JIT's prologue. That should tell me whether the JIT handles stack initialization or leaves it to some other part of the stack management code to deal with. |
| 36/3 | 2026/09/26 22:36 | tool | grepper |
0mArgs:{
"Expression": "emit_prologue",
"PathPrefix": "arch/x86/net/bpf_jit_comp.c"
}Results: {
"Output": "arch/x86/net/bpf_jit_comp.c=506=static void emit_cfi(u8 **pprog, u8 *ip, u32 hash, int arity)\n--\narch/x86/net/bpf_jit_comp.c-526-\narch/x86/net/bpf_jit_comp.c:527:static void emit_prologue_tail_call(u8 **pprog, bool is_subprog)\narch/x86/net/bpf_jit_comp.c-528-{\n--\narch/x86/net/bpf_jit_comp.c-562- */\narch/x86/net/bpf_jit_comp.c:563:static void emit_prologue(u8 **pprog, u8 *ip, u32 stack_depth, bool ebpf_from_cbpf,\narch/x86/net/bpf_jit_comp.c-564-\t\t\t bool tail_call_reachable, bool is_subprog,\n--\narch/x86/net/bpf_jit_comp.c-611-\tif (tail_call_reachable)\narch/x86/net/bpf_jit_comp.c:612:\t\temit_prologue_tail_call(\u0026prog, is_subprog);\narch/x86/net/bpf_jit_comp.c-613-\t*pprog = prog;\n--\narch/x86/net/bpf_jit_comp.c=692=int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,\n--\narch/x86/net/bpf_jit_comp.c-701-\t/*\narch/x86/net/bpf_jit_comp.c:702:\t * See emit_prologue(), for IBT builds the trampoline hook is preceded\narch/x86/net/bpf_jit_comp.c-703-\t * with an ENDBR instruction.\n--\narch/x86/net/bpf_jit_comp.c=2002=static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *addrs, u8 *image,\n--\narch/x86/net/bpf_jit_comp.c-2057-\narch/x86/net/bpf_jit_comp.c:2058:\temit_prologue(\u0026prog, image, stack_depth,\narch/x86/net/bpf_jit_comp.c-2059-\t\t bpf_prog_was_classic(bpf_prog), tail_call_reachable,\n"
} |
| 37/3 | 2026/09/26 22:36 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 86237 output: 44 thoughts: 30 Thoughts: Okay, here's my interpretation of that line of thought, tailored for an expert audience: **Diving into the x86 BPF JIT Prologue** Right, so I'm jumping into `emit_prologue` within `arch/x86/net/bpf_jit_comp.c`. Clearly, we're looking at the prologue generation for the BPF JIT compiler on x86. This suggests we're about to examine the initial setup phase – the critical code that handles things like saving registers, setting up the stack frame, and generally preparing the x86 environment for the compiled BPF code to run. I need to understand how the architecture is initialized and how it maps to the BPF's virtual machine. Let's see how the compiled code gets its footing. I'm keen to see how we manage the registers and memory to handle the BPF program execution. |
| 38/3 | 2026/09/26 22:36 | tool | read-file |
0mArgs:{
"File": "arch/x86/net/bpf_jit_comp.c",
"FirstLine": 560,
"LineCount": 45
}Results: {
"Contents": " 560:\t * bpf_tail_call helper will skip the first X86_TAIL_CALL_OFFSET bytes\n 561:\t * while jumping to another program\n 562:\t */\n 563:\tstatic void emit_prologue(u8 **pprog, u8 *ip, u32 stack_depth, bool ebpf_from_cbpf,\n 564:\t\t\t\t bool tail_call_reachable, bool is_subprog,\n 565:\t\t\t\t bool is_exception_cb)\n 566:\t{\n 567:\t\tu8 *prog = *pprog;\n 568:\t\n 569:\t\tif (is_subprog) {\n 570:\t\t\temit_cfi(\u0026prog, ip, cfi_bpf_subprog_hash, 5);\n 571:\t\t} else {\n 572:\t\t\temit_cfi(\u0026prog, ip, cfi_bpf_hash, 1);\n 573:\t\t}\n 574:\t\t/* BPF trampoline can be made to work without these nops,\n 575:\t\t * but let's waste 5 bytes for now and optimize later\n 576:\t\t */\n 577:\t\temit_nops(\u0026prog, X86_PATCH_SIZE);\n 578:\t\tif (!ebpf_from_cbpf) {\n 579:\t\t\tif (tail_call_reachable \u0026\u0026 !is_subprog)\n 580:\t\t\t\t/* When it's the entry of the whole tailcall context,\n 581:\t\t\t\t * zeroing rax means initialising tail_call_cnt.\n 582:\t\t\t\t */\n 583:\t\t\t\tEMIT3(0x48, 0x31, 0xC0); /* xor rax, rax */\n 584:\t\t\telse\n 585:\t\t\t\t/* Keep the same instruction layout. */\n 586:\t\t\t\temit_nops(\u0026prog, 3); /* nop3 */\n 587:\t\t}\n 588:\t\t/* Exception callback receives FP as third parameter */\n 589:\t\tif (is_exception_cb) {\n 590:\t\t\tEMIT3(0x48, 0x89, 0xF4); /* mov rsp, rsi */\n 591:\t\t\tEMIT3(0x48, 0x89, 0xD5); /* mov rbp, rdx */\n 592:\t\t\t/* The main frame must have exception_boundary as true, so we\n 593:\t\t\t * first restore those callee-saved regs from stack, before\n 594:\t\t\t * reusing the stack frame.\n 595:\t\t\t */\n 596:\t\t\tpop_callee_regs(\u0026prog, all_callee_regs_used);\n 597:\t\t\tpop_r12(\u0026prog);\n 598:\t\t\t/* Reset the stack frame. */\n 599:\t\t\tEMIT3(0x48, 0x89, 0xEC); /* mov rsp, rbp */\n 600:\t\t} else {\n 601:\t\t\tEMIT1(0x55); /* push rbp */\n 602:\t\t\tEMIT3(0x48, 0x89, 0xE5); /* mov rbp, rsp */\n 603:\t\t}\n 604:\t\n"
} |
| 39/3 | 2026/09/26 22:36 | llm | kmsan-evaluator |
1mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 87350 output: 44 thoughts: 14 Thoughts: Okay, here's my attempt at summarizing the thought process, tailored for an expert audience, and written in the first person: **Diving into `emit_prologue`** Alright, let's see what's going on down here in the `emit_prologue` section. My initial thought is to assess its overall purpose within the larger context of the system. I'm expecting this to be the place where we lay the groundwork for our generated output, right? Think declarations, initializations, maybe some global setup – the stuff that needs to happen before the "meat" of the code gets generated. I'm mentally running through the typical prologue tasks in similar systems I've worked on. I'm looking for things like: * **Header Inclusion:** Are we explicitly including any necessary libraries or modules? If so, which ones, and are we doing it efficiently (e.g., using precompiled headers or forward declarations where appropriate)? * **Constant Definitions:** Any macros or constant values being declared? I'm curious about the naming conventions and whether we're using well-defined, easily understandable constants or potentially magic numbers. * **Type Declarations:** Are there any custom types, structures, or classes being defined here? I'm watching to see if we're utilizing existing types where possible, or if the definitions appear optimized for the target language. * **Variable Declarations:** Initialization of any global variables? I'm looking at scope, and wondering how that interacts with the rest of the code. I'm also mentally checking for any potential optimization opportunities. Can we defer some of this work to be done later, in the actual program, so the prologue is as lean as possible? Is there redundancy or duplication that we can eliminate? I'm also anticipating any edge cases, like how the prologue interacts with potentially incomplete input, or a changing build environment. Let's dig in. |
| 40/3 | 2026/09/26 22:37 | tool | read-file |
0mArgs:{
"File": "arch/x86/net/bpf_jit_comp.c",
"FirstLine": 605,
"LineCount": 45
}Results: {
"Contents": " 605:\t\t/* X86_TAIL_CALL_OFFSET is here */\n 606:\t\tEMIT_ENDBR();\n 607:\t\n 608:\t\t/* sub rsp, rounded_stack_depth */\n 609:\t\tif (stack_depth)\n 610:\t\t\tEMIT3_off32(0x48, 0x81, 0xEC, round_up(stack_depth, 8));\n 611:\t\tif (tail_call_reachable)\n 612:\t\t\temit_prologue_tail_call(\u0026prog, is_subprog);\n 613:\t\t*pprog = prog;\n 614:\t}\n 615:\t\n 616:\tstatic int emit_patch(u8 **pprog, void *func, void *ip, u8 opcode)\n 617:\t{\n 618:\t\tu8 *prog = *pprog;\n 619:\t\ts64 offset;\n 620:\t\n 621:\t\toffset = func - (ip + X86_PATCH_SIZE);\n 622:\t\tif (!is_simm32(offset)) {\n 623:\t\t\tpr_err(\"Target call %p is out of range\\n\", func);\n 624:\t\t\treturn -ERANGE;\n 625:\t\t}\n 626:\t\tEMIT1_off32(opcode, offset);\n 627:\t\t*pprog = prog;\n 628:\t\treturn 0;\n 629:\t}\n 630:\t\n 631:\tstatic int emit_call(u8 **pprog, void *func, void *ip)\n 632:\t{\n 633:\t\treturn emit_patch(pprog, func, ip, 0xE8);\n 634:\t}\n 635:\t\n 636:\tstatic int emit_rsb_call(u8 **pprog, void *func, void *ip)\n 637:\t{\n 638:\t\tOPTIMIZER_HIDE_VAR(func);\n 639:\t\tip += x86_call_depth_emit_accounting(pprog, func, ip);\n 640:\t\treturn emit_patch(pprog, func, ip, 0xE8);\n 641:\t}\n 642:\t\n 643:\tstatic int emit_jump(u8 **pprog, void *func, void *ip)\n 644:\t{\n 645:\t\treturn emit_patch(pprog, func, ip, 0xE9);\n 646:\t}\n 647:\t\n 648:\tstatic int __bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,\n 649:\t\t\t\t\tenum bpf_text_poke_type new_t,\n"
} |
| 41/3 | 2026/09/26 22:37 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 88550 output: 27 thoughts: 281 Thoughts: **My BPF Stack Musings** Okay, so I'm looking at `emit_prologue` and that `EMIT3_off32` call has me thinking. Specifically, the part where we're calculating the stack space using `round_up(stack_depth, 8)`. That `stack_depth` is the verified stack size for the BPF program, right? It's verifier-determined, which means it should be safe...but wait a second. Are we potentially allowing BPF programs to read uninitialized stack slots? This brings me to `env->allow_uninit_stack`. If that's true, which typically means we're in privileged mode with `CAP_PERFMON`, *can* a BPF program then read uninitialized stack slots? And if so, what are the implications for unprivileged users, even with a limited stack size? The `bpf_allow_uninit_stack(token)` function clearly requires `CAP_PERFMON`, so that offers some protection... but is it enough? Hold on... even with a 512-byte stack, privileged BPF programs could be reading uninitialized data on the kernel stack itself. That's a huge potential security hole. Whatever junk happened to be on the kernel stack before is now potentially exposed. Now, is KMSAN catching this behavior? Is KMSAN even *tracking* BPF stack usage? I need to understand how KMSAN interacts with the BPF JIT and BPF programs themselves. I need to know if it is detecting the issue, or if it is blind. Okay, let's search for KMSAN mentions in `kernel/bpf/` and also in `arch/x86/net/bpf_jit_comp.c`, and see what the code has to say. |
| 42/3 | 2026/09/26 22:37 | tool | grepper |
0mArgs:{
"Expression": "kmsan",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/core.c=2413=static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \\\n--\nkernel/bpf/core.c-2417-\\\nkernel/bpf/core.c:2418:\tkmsan_unpoison_memory(stack, sizeof(stack)); \\\nkernel/bpf/core.c-2419-\tFP = (u64) (unsigned long) \u0026stack[ARRAY_SIZE(stack)]; \\\n--\nkernel/bpf/core.c=2426=static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \\\n--\nkernel/bpf/core.c-2431-\\\nkernel/bpf/core.c:2432:\tkmsan_unpoison_memory(stack, sizeof(stack)); \\\nkernel/bpf/core.c-2433-\tFP = (u64) (unsigned long) \u0026stack[ARRAY_SIZE(stack)]; \\\n"
} |
| 43/3 | 2026/09/26 22:37 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 89537 output: 37 thoughts: 53 Thoughts: Here's my take on those thoughts: **Analyzing a Kernel Crash Report** Alright, take a look at *that*! I see a `kmsan_unpoison_memory(stack, sizeof(stack))` call right there in `kernel/bpf/core.c`! My spider senses are tingling; this smells like a potential memory safety issue, specifically in the BPF subsystem. Let's dig into that `kernel/bpf/core.c` file and see what's happening around that line. That function call is significant, a key pointer to what might have gone wrong. It's usually a critical point for understanding a potential memory corruption. I need to understand the context around this call to piece together the entire issue at hand. Let me dive in and analyze. |
| 44/3 | 2026/09/26 22:37 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/core.c",
"FirstLine": 2410,
"LineCount": 35
}Results: {
"Contents": "2410:\t\n2411:\t#define PROG_NAME(stack_size) __bpf_prog_run##stack_size\n2412:\t#define DEFINE_BPF_PROG_RUN(stack_size) \\\n2413:\tstatic unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \\\n2414:\t{ \\\n2415:\t\tu64 stack[stack_size / sizeof(u64)]; \\\n2416:\t\tu64 regs[MAX_BPF_EXT_REG] = {}; \\\n2417:\t\\\n2418:\t\tkmsan_unpoison_memory(stack, sizeof(stack)); \\\n2419:\t\tFP = (u64) (unsigned long) \u0026stack[ARRAY_SIZE(stack)]; \\\n2420:\t\tARG1 = (u64) (unsigned long) ctx; \\\n2421:\t\treturn ___bpf_prog_run(regs, insn); \\\n2422:\t}\n2423:\t\n2424:\t#define PROG_NAME_ARGS(stack_size) __bpf_prog_run_args##stack_size\n2425:\t#define DEFINE_BPF_PROG_RUN_ARGS(stack_size) \\\n2426:\tstatic u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \\\n2427:\t\t\t\t\t const struct bpf_insn *insn) \\\n2428:\t{ \\\n2429:\t\tu64 stack[stack_size / sizeof(u64)]; \\\n2430:\t\tu64 regs[MAX_BPF_EXT_REG]; \\\n2431:\t\\\n2432:\t\tkmsan_unpoison_memory(stack, sizeof(stack)); \\\n2433:\t\tFP = (u64) (unsigned long) \u0026stack[ARRAY_SIZE(stack)]; \\\n2434:\t\tBPF_R1 = r1; \\\n2435:\t\tBPF_R2 = r2; \\\n2436:\t\tBPF_R3 = r3; \\\n2437:\t\tBPF_R4 = r4; \\\n2438:\t\tBPF_R5 = r5; \\\n2439:\t\treturn ___bpf_prog_run(regs, insn); \\\n2440:\t}\n2441:\t\n2442:\t#define EVAL1(FN, X) FN(X)\n2443:\t#define EVAL2(FN, X, Y...) FN(X) EVAL1(FN, Y)\n2444:\t#define EVAL3(FN, X, Y...) FN(X) EVAL2(FN, Y)\n"
} |
| 45/3 | 2026/09/26 22:37 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 90521 output: 35 thoughts: 672 Thoughts: **Analyzing the BPF Stack Unpoisoning Patch** Okay, so the interpreter explicitly unpoisons the stack, that's the first thing I note. And since we're dealing with JITed BPF, the JIT doesn't even bother with KMSAN checks, so that's out of the equation. Now, I need to methodically dissect this patch, focusing on the verifier, the kernel data structures, memory allocations, and any data being passed to userspace. This requires a granular item-by-item inspection. First, we have the documentation changes in `Documentation/bpf/bpf_design_QA.rst`. Nothing substantial there. Next, we see changes in `arch/arm64/net/bpf_jit_comp.c` and `arch/x86/net/bpf_jit_comp.c`, where `bpf_jit_supports_large_stack()` is now returning `true`. Pretty straightforward so far. Then, there's a modification in `drivers/net/ethernet/netronome/nfp/bpf/verifier.c`: `stack_entry = bpf_stack_slot(state, soff / BPF_REG_SIZE);` No concerns here. Now, things get a bit more interesting in `include/linux/bpf_verifier.h`. I see the `TMP_STR_BUF_LEN` increased significantly from 320 to 1408, along with `MAX_BPF_STACK_SLOTS` defined. Also, we have a definition for `BPF_LINKED_REGS_MAX` and the most crucial change – the new `struct bpf_jmp_history_entry`. It contains a bunch of interesting fields like `idx`, `frame`, `flags`, `prev_idx`, `spi`, and `linked_regs`. This calls for a closer look. Hang on... I need to understand how this structure is allocated and initialized. Is it on the stack, dynamically allocated, or what? I need to find where `struct bpf_jmp_history_entry` is actually *used*. Let's track down calls to it, specifically focusing on `bpf_push_jmp_history`. Okay, I'm heading over to `kernel/bpf/backtrack.c`, specifically around line 15, to scrutinize `bpf_push_jmp_history`. I need to understand how the `env`, `cur`, `insn_flags`, `spi`, `frame`, `linked_regs`, and `linked_regs_cnt` parameters are used within that function. This is where the magic (or potential problems) likely lie. I'm going to carefully read this section and try to figure out what could be happening here. |
| 46/3 | 2026/09/26 22:37 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/backtrack.c",
"FirstLine": 1,
"LineCount": 60
}Results: {
"Contents": " 1:\t// SPDX-License-Identifier: GPL-2.0-only\n 2:\t/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n 3:\t#include \u003clinux/bpf.h\u003e\n 4:\t#include \u003clinux/bpf_verifier.h\u003e\n 5:\t#include \u003clinux/filter.h\u003e\n 6:\t#include \u003clinux/bitmap.h\u003e\n 7:\t\n 8:\t#define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)\n 9:\t\n 10:\t/* for any branch, call, exit record the history of jmps in the given state */\n 11:\tint bpf_push_jmp_history(struct bpf_verifier_env *env, struct bpf_verifier_state *cur,\n 12:\t\t\t\t int insn_flags, int spi, int frame, const u16 *linked_regs,\n 13:\t\t\t\t u8 linked_regs_cnt)\n 14:\t{\n 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"
} |
| 47/3 | 2026/09/26 22:37 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 92894 output: 28 thoughts: 582 Thoughts: **Analyzing Potential Memory Corruption in BPF Jump History**
Okay, hold on a second! Lines 41-57 are the key here. We've got `alloc_size = kmalloc_size_roundup(size_mul(cnt, sizeof(*p)));` and then a `krealloc` without `__GFP_ZERO`? Alarm bells, because that can mean uninitialized memory is in use. First, I need to remind myself what `p` was before the reallocation. Ah, right:
```c
- p->flags = insn_flags;
- p->spi = spi;
- p->frame = frame;
- p->linked_regs = linked_regs;
- cur->jmp_history_cnt = cnt;
- env->cur_hist_ent = p;
```
And after:
```c
+ if (linked_regs_cnt)
+ memcpy(p->linked_regs, linked_regs, linked_regs_cnt * sizeof(*linked_regs));
+ p->linked_regs_cnt = linked_regs_cnt;
```
Okay, so we're *reallocating* `p->linked_regs`, potentially growing it. Here's where it gets interesting: What if `linked_regs_cnt` is *less* than `BPF_LINKED_REGS_MAX`? Are we reading from uninitialized memory within `p->linked_regs` beyond the `linked_regs_cnt` count? This could cause some real issues.
I need to trace where `linked_regs` is read to understand the impact. Let's look through `kernel/bpf/backtrack.c` and `kernel/bpf/verifier.c`. Okay, found it in `bpf_bt_sync_linked_regs`. Here is that code:
```c
static void linked_regs_unpack(const struct bpf_jmp_history_entry *hist, struct linked_regs *s)
{
int i;
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 = packed & LR_FRAMENO_MASK;
e->is_reg = packed & LR_IS_REG;
e->spi = (packed >> LR_INDEX_OFF) & LR_INDEX_MASK;
}
}
```
Good. I see that `s->cnt = hist->linked_regs_cnt;` and the loop only iterates up to `i < s->cnt`. So the code *should* only read from `hist->linked_regs[i]` up to `linked_regs_cnt`. So far, so good, but only under that assumption. But what about when `cur` state is copied in `bpf_copy_verifier_state`? Need to check that.
|
| 48/3 | 2026/09/26 22:37 | 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=1261=int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx)\n--\nkernel/bpf/states.c-1271-\t\t\t /* Avoid accumulating infinitely long jmp history */\nkernel/bpf/states.c:1272:\t\t\t cur-\u003ejmp_history_cnt \u003e 40;\nkernel/bpf/states.c-1273-\n--\nkernel/bpf/states.c-1438-\t\t\tif (bpf_is_jmp_point(env, env-\u003einsn_idx))\nkernel/bpf/states.c:1439:\t\t\t\terr = bpf_push_jmp_history(env, cur, 0, 0, 0, NULL, 0);\nkernel/bpf/states.c-1440-\t\t\terr = err ? : propagate_precision(env, \u0026sl-\u003estate, cur, NULL);\n--\nkernel/bpf/states.c-1619-\tcur-\u003edfs_depth = new-\u003edfs_depth + 1;\nkernel/bpf/states.c:1620:\tbpf_clear_jmp_history(cur);\nkernel/bpf/states.c-1621-\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=3428=static void linked_regs_pack(const struct linked_regs *s, u16 *packed)\n--\nkernel/bpf/verifier.c-3438-\nkernel/bpf/verifier.c:3439:static void linked_regs_unpack(const struct bpf_jmp_history_entry *hist, struct linked_regs *s)\nkernel/bpf/verifier.c-3440-{\n--\nkernel/bpf/verifier.c=3472=void bpf_verbose_insn(struct bpf_verifier_env *env, struct bpf_insn *insn)\n--\nkernel/bpf/verifier.c-3485- */\nkernel/bpf/verifier.c:3486:void bpf_bt_sync_linked_regs(struct backtrack_state *bt, struct bpf_jmp_history_entry *hist)\nkernel/bpf/verifier.c-3487-{\n--\nkernel/bpf/verifier.c=3659=static int check_stack_write_fixed_off(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-3779-\tif (insn_flags)\nkernel/bpf/verifier.c:3780:\t\treturn bpf_push_jmp_history(env, env-\u003ecur_state, insn_flags,\nkernel/bpf/verifier.c-3781-\t\t\t\t\t hist_spi, hist_frame, NULL, 0);\n--\nkernel/bpf/verifier.c=4010=static int check_stack_read_fixed_off(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-4160-\tif (insn_flags)\nkernel/bpf/verifier.c:4161:\t\treturn bpf_push_jmp_history(env, env-\u003ecur_state, insn_flags,\nkernel/bpf/verifier.c-4162-\t\t\t\t\t hist_spi, hist_frame, NULL, 0);\n--\nkernel/bpf/verifier.c=4315=static int check_stack_arg_write(struct bpf_verifier_env *env, struct bpf_func_state *state,\n--\nkernel/bpf/verifier.c-4349-\tstate-\u003eno_stack_arg_load = true;\nkernel/bpf/verifier.c:4350:\treturn bpf_push_jmp_history(env, env-\u003ecur_state,\nkernel/bpf/verifier.c-4351-\t\t\t\t INSN_F_STACK_ARG_ACCESS, spi, 0, NULL, 0);\n--\nkernel/bpf/verifier.c=4358=static int check_stack_arg_read(struct bpf_verifier_env *env, struct bpf_func_state *state,\n--\nkernel/bpf/verifier.c-4383-\tbpf_diag_mod_end(env);\nkernel/bpf/verifier.c:4384:\treturn bpf_push_jmp_history(env, env-\u003ecur_state,\nkernel/bpf/verifier.c-4385-\t\t\t\t INSN_F_STACK_ARG_ACCESS, spi, 0, NULL, 0);\n--\nkernel/bpf/verifier.c=17926=static int check_cond_jmp_op(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-17999-\tif (insn_flags) {\nkernel/bpf/verifier.c:18000:\t\terr = bpf_push_jmp_history(env, this_branch, insn_flags, 0, 0, NULL, 0);\nkernel/bpf/verifier.c-18001-\t\tif (err)\n--\nkernel/bpf/verifier.c-18072-\t\tlinked_regs_pack(\u0026linked_regs, packed);\nkernel/bpf/verifier.c:18073:\t\terr = bpf_push_jmp_history(env, this_branch, 0, 0, 0, packed, linked_regs.cnt);\nkernel/bpf/verifier.c-18074-\t\tif (err)\n--\nkernel/bpf/verifier.c=19404=static int do_check(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-19483-\t\tif (bpf_is_jmp_point(env, env-\u003einsn_idx)) {\nkernel/bpf/verifier.c:19484:\t\t\terr = bpf_push_jmp_history(env, state, 0, 0, 0, NULL, 0);\nkernel/bpf/verifier.c-19485-\t\t\tif (err)\n"
} |
| 49/3 | 2026/09/26 22:37 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 97528 output: 37 thoughts: 426 Thoughts: **My Concerns Regarding `bpf_copy_verifier_state` and Potential Information Leakage** Okay, let me break this down. In `bpf_copy_verifier_state`, I see this call to `copy_array` handling the `jmp_history` data. It looks like it's copying `src->jmp_history_cnt` entries, each the size of `*dst_state->jmp_history`. That's fine, standard copying. But hold on, let me refresh my memory of how that history is *created*. Now, in `bpf_push_jmp_history`, I'm seeing that we're calculating an index `p = &cur->jmp_history[cnt - 1]` and then writing to it. I'm focusing on the allocation now. **Bingo!** This memory was allocated using `krealloc`, but not with the `__GFP_ZERO` flag. This is a potential problem. Now, my mind goes to the details. We have an anonymous bitfield `u32 : 4;` in `struct bpf_jmp_history_entry`. If this is uninitialized, the content of this bitfield will be unpredictable. Then, there's `p->linked_regs`. We allocate enough space for `linked_regs_cnt` elements, but the remaining elements up to `BPF_LINKED_REGS_MAX - 1` are left uninitialized. The core question now is: when `copy_array` is used, does it actually *read* those uninitialized bytes? And thinking about that `copy_array` function, it's just a `memcpy`. So, yes, it will read those uninitialized bytes! This feels like a potential information leak. I need to dive deeper into `copy_array` and see what's actually happening. |
| 50/3 | 2026/09/26 22:37 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/verifier.c",
"FirstLine": 1340,
"LineCount": 30
}Results: {
"Contents": "1340:\t * Leaves dst untouched if src is NULL or length is zero. Returns NULL if memory could\n1341:\t * not be allocated.\n1342:\t */\n1343:\tstatic void *copy_array(void *dst, const void *src, size_t n, size_t size, gfp_t flags)\n1344:\t{\n1345:\t\tsize_t alloc_bytes;\n1346:\t\tvoid *orig = dst;\n1347:\t\tsize_t bytes;\n1348:\t\n1349:\t\tif (ZERO_OR_NULL_PTR(src))\n1350:\t\t\tgoto out;\n1351:\t\n1352:\t\tif (unlikely(check_mul_overflow(n, size, \u0026bytes)))\n1353:\t\t\treturn NULL;\n1354:\t\n1355:\t\talloc_bytes = max(ksize(orig), kmalloc_size_roundup(bytes));\n1356:\t\tdst = krealloc(orig, alloc_bytes, flags);\n1357:\t\tif (!dst) {\n1358:\t\t\tkfree(orig);\n1359:\t\t\treturn NULL;\n1360:\t\t}\n1361:\t\n1362:\t\tmemcpy(dst, src, bytes);\n1363:\tout:\n1364:\t\treturn dst ? dst : ZERO_SIZE_PTR;\n1365:\t}\n1366:\t\n1367:\t/* resize an array from old_n items to new_n items. the array is reallocated if it's too\n1368:\t * small to hold new_n items. new items are zeroed out if the array grows.\n1369:\t *\n"
} |
| 51/3 | 2026/09/26 22:37 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 98835 output: 28 thoughts: 132 Thoughts: **KMSAN and `memcpy` Shenanigans** Okay, hold on a second! I'm remembering something critical about `memcpy` and KMSAN. Right, `memcpy` *itself* propagates shadow memory, moving uninitialized memory from the source to the destination. That's how it *avoids* triggering a KMSAN report in a lot of cases. KMSAN lets you copy uninitialized memory, as long as you're not doing anything *with* it that actually uses that uninitialized data. Specifically, it's allowed if the uninitialized data is not used in conditional branches, dereferenced, passed to syscalls or user space, or formatted into logs. So, the key question is: are those uninitialized parts of `bpf_jmp_history_entry` – the ones that are being copied around with `memcpy` – ever *actually* read or used in a way that KMSAN cares about? This needs immediate investigation. Right, I need to check every single place where `cur->jmp_history`, `st->jmp_history`, or just `hist` is read. I need to understand the full context of these reads. And now, I'm thinking about related things. Given the importance of tracking jumps and such, let's also investigate usages of the `linked_regs` parameter. To start the investigation, I'm going to grep for `linked_regs` and see how it is being used in relation to this history and memory usage. |
| 52/3 | 2026/09/26 22:37 | tool | grepper |
0mArgs:{
"Expression": "linked_regs",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/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,\nkernel/bpf/backtrack.c:13:\t\t\t u8 linked_regs_cnt)\nkernel/bpf/backtrack.c-14-{\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\",\nkernel/bpf/backtrack.c:33:\t\t\t\tenv-\u003einsn_idx, env-\u003ecur_hist_ent-\u003elinked_regs_cnt);\nkernel/bpf/backtrack.c:34:\t\tif (linked_regs_cnt)\nkernel/bpf/backtrack.c:35:\t\t\tmemcpy(env-\u003ecur_hist_ent-\u003elinked_regs, linked_regs,\nkernel/bpf/backtrack.c:36:\t\t\t linked_regs_cnt * sizeof(*linked_regs));\nkernel/bpf/backtrack.c:37:\t\tenv-\u003ecur_hist_ent-\u003elinked_regs_cnt = linked_regs_cnt;\nkernel/bpf/backtrack.c-38-\t\treturn 0;\n--\nkernel/bpf/backtrack.c-53-\tp-\u003eframe = frame;\nkernel/bpf/backtrack.c:54:\tif (linked_regs_cnt)\nkernel/bpf/backtrack.c:55:\t\tmemcpy(p-\u003elinked_regs, linked_regs, linked_regs_cnt * sizeof(*linked_regs));\nkernel/bpf/backtrack.c:56:\tp-\u003elinked_regs_cnt = linked_regs_cnt;\nkernel/bpf/backtrack.c-57-\tcur-\u003ejmp_history_cnt = cnt;\n--\nkernel/bpf/backtrack.c=283=static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,\n--\nkernel/bpf/backtrack.c-310-\t */\nkernel/bpf/backtrack.c:311:\tbpf_bt_sync_linked_regs(bt, hist);\nkernel/bpf/backtrack.c-312-\n--\nkernel/bpf/backtrack.c-647-\t */\nkernel/bpf/backtrack.c:648:\tbpf_bt_sync_linked_regs(bt, hist);\nkernel/bpf/backtrack.c-649-\treturn 0;\n--\nkernel/bpf/states.c=502=static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold,\n--\nkernel/bpf/states.c-573-\t\t * - at (4) same bpf_reg_state::id (b) would be assigned to r6 and r7;\nkernel/bpf/states.c:574:\t\t * - at (5) r6 would be marked \u003c= X, sync_linked_regs() would also mark\nkernel/bpf/states.c-575-\t\t * r7 \u003c= X, because r6 and r7 share same id.\n--\nkernel/bpf/states.c-589-\t\t * BPF_ADD_CONST64 have different linking semantics in\nkernel/bpf/states.c:590:\t\t * sync_linked_regs() (alu32 zero-extends, alu64 does not),\nkernel/bpf/states.c-591-\t\t * so pruning across different flag types is unsafe.\n--\nkernel/bpf/verifier.c=3381=static void mark_non_stack_access(struct bpf_verifier_env *env, int idx)\n--\nkernel/bpf/verifier.c-3385-\nkernel/bpf/verifier.c:3386:/* Layout of one packed linked register in the jump history, see linked_regs_pack() */\nkernel/bpf/verifier.c-3387-#define LR_FRAMENO_BITS\t4\n--\nkernel/bpf/verifier.c=3400=struct linked_reg {\n--\nkernel/bpf/verifier.c-3408-\nkernel/bpf/verifier.c:3409:struct linked_regs {\nkernel/bpf/verifier.c-3410-\tint cnt;\n--\nkernel/bpf/verifier.c-3413-\nkernel/bpf/verifier.c:3414:static struct linked_reg *linked_regs_push(struct linked_regs *s)\nkernel/bpf/verifier.c-3415-{\n--\nkernel/bpf/verifier.c-3427- */\nkernel/bpf/verifier.c:3428:static void linked_regs_pack(const struct linked_regs *s, u16 *packed)\nkernel/bpf/verifier.c-3429-{\n--\nkernel/bpf/verifier.c-3438-\nkernel/bpf/verifier.c:3439:static void linked_regs_unpack(const struct bpf_jmp_history_entry *hist, struct linked_regs *s)\nkernel/bpf/verifier.c-3440-{\n--\nkernel/bpf/verifier.c-3442-\nkernel/bpf/verifier.c:3443:\ts-\u003ecnt = hist-\u003elinked_regs_cnt;\nkernel/bpf/verifier.c-3444-\tfor (i = 0; i \u003c s-\u003ecnt; ++i) {\nkernel/bpf/verifier.c-3445-\t\tstruct linked_reg *e = \u0026s-\u003eentries[i];\nkernel/bpf/verifier.c:3446:\t\tu16 packed = hist-\u003elinked_regs[i];\nkernel/bpf/verifier.c-3447-\n--\nkernel/bpf/verifier.c=3472=void bpf_verbose_insn(struct bpf_verifier_env *env, struct bpf_insn *insn)\n--\nkernel/bpf/verifier.c-3482-\nkernel/bpf/verifier.c:3483:/* If any register R in hist-\u003elinked_regs is marked as precise in bt,\nkernel/bpf/verifier.c:3484: * do bt_set_frame_{reg,slot}(bt, R) for all registers in hist-\u003elinked_regs.\nkernel/bpf/verifier.c-3485- */\nkernel/bpf/verifier.c:3486:void bpf_bt_sync_linked_regs(struct backtrack_state *bt, struct bpf_jmp_history_entry *hist)\nkernel/bpf/verifier.c-3487-{\nkernel/bpf/verifier.c:3488:\tstruct linked_regs linked_regs;\nkernel/bpf/verifier.c-3489-\tbool some_precise = false;\n--\nkernel/bpf/verifier.c-3491-\nkernel/bpf/verifier.c:3492:\tif (!hist || !hist-\u003elinked_regs_cnt)\nkernel/bpf/verifier.c-3493-\t\treturn;\nkernel/bpf/verifier.c-3494-\nkernel/bpf/verifier.c:3495:\tlinked_regs_unpack(hist, \u0026linked_regs);\nkernel/bpf/verifier.c:3496:\tfor (i = 0; i \u003c linked_regs.cnt; ++i) {\nkernel/bpf/verifier.c:3497:\t\tstruct linked_reg *e = \u0026linked_regs.entries[i];\nkernel/bpf/verifier.c-3498-\n--\nkernel/bpf/verifier.c-3508-\nkernel/bpf/verifier.c:3509:\tfor (i = 0; i \u003c linked_regs.cnt; ++i) {\nkernel/bpf/verifier.c:3510:\t\tstruct linked_reg *e = \u0026linked_regs.entries[i];\nkernel/bpf/verifier.c-3511-\n--\nkernel/bpf/verifier.c=3566=static void assign_scalar_id_before_mov(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-3579-\t * Ensure that src_reg has a valid ID that will be copied to\nkernel/bpf/verifier.c:3580:\t * dst_reg and then will be used by sync_linked_regs() to\nkernel/bpf/verifier.c-3581-\t * propagate min/max range.\n--\nkernel/bpf/verifier.c=16705=static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-16885-\t\t * Make sure ID is cleared otherwise dst_reg min/max could be\nkernel/bpf/verifier.c:16886:\t\t * incorrectly propagated into other registers by sync_linked_regs()\nkernel/bpf/verifier.c-16887-\t\t */\n--\nkernel/bpf/verifier.c=16894=static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)\n--\nkernel/bpf/verifier.c-16999-\t\t\t\t\t\t * range otherwise dst_reg min/max could be incorrectly\nkernel/bpf/verifier.c:17000:\t\t\t\t\t\t * propagated into src_reg by sync_linked_regs()\nkernel/bpf/verifier.c-17001-\t\t\t\t\t\t */\n--\nkernel/bpf/verifier.c=17709=static bool try_match_pkt_pointers(const struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-17813-\nkernel/bpf/verifier.c:17814:static void __collect_linked_regs(struct linked_regs *reg_set, struct bpf_reg_state *reg,\nkernel/bpf/verifier.c-17815-\t\t\t\t u32 id, u32 frameno, u32 spi_or_reg, bool is_reg)\n--\nkernel/bpf/verifier.c-17821-\nkernel/bpf/verifier.c:17822:\te = linked_regs_push(reg_set);\nkernel/bpf/verifier.c-17823-\tif (e) {\n--\nkernel/bpf/verifier.c-17832-/* For all R being scalar registers or spilled scalar registers\nkernel/bpf/verifier.c:17833: * in verifier state, save R in linked_regs if R-\u003eid == id.\nkernel/bpf/verifier.c-17834- * If there are too many Rs sharing same id, reset id for leftover Rs.\nkernel/bpf/verifier.c-17835- */\nkernel/bpf/verifier.c:17836:static void collect_linked_regs(struct bpf_verifier_env *env,\nkernel/bpf/verifier.c-17837-\t\t\t\tstruct bpf_verifier_state *vstate,\nkernel/bpf/verifier.c-17838-\t\t\t\tu32 id,\nkernel/bpf/verifier.c:17839:\t\t\t\tstruct linked_regs *linked_regs)\nkernel/bpf/verifier.c-17840-{\n--\nkernel/bpf/verifier.c-17854-\t\t\treg = \u0026func-\u003eregs[j];\nkernel/bpf/verifier.c:17855:\t\t\t__collect_linked_regs(linked_regs, reg, id, i, j, true);\nkernel/bpf/verifier.c-17856-\t\t}\n--\nkernel/bpf/verifier.c-17862-\t\t\treg = \u0026ss-\u003espilled_ptr;\nkernel/bpf/verifier.c:17863:\t\t\t__collect_linked_regs(linked_regs, reg, id, i, j, false);\nkernel/bpf/verifier.c-17864-\t\t}\n--\nkernel/bpf/verifier.c-17867-\nkernel/bpf/verifier.c:17868:/* For all R in linked_regs, copy known_reg range into R\nkernel/bpf/verifier.c-17869- * if R-\u003eid == known_reg-\u003eid.\nkernel/bpf/verifier.c-17870- */\nkernel/bpf/verifier.c:17871:static void sync_linked_regs(struct bpf_verifier_env *env, struct bpf_verifier_state *vstate,\nkernel/bpf/verifier.c:17872:\t\t\t struct bpf_reg_state *known_reg, struct linked_regs *linked_regs)\nkernel/bpf/verifier.c-17873-{\n--\nkernel/bpf/verifier.c-17878-\nkernel/bpf/verifier.c:17879:\tfor (i = 0; i \u003c linked_regs-\u003ecnt; ++i) {\nkernel/bpf/verifier.c:17880:\t\te = \u0026linked_regs-\u003eentries[i];\nkernel/bpf/verifier.c-17881-\t\treg = e-\u003eis_reg ? \u0026vstate-\u003eframe[e-\u003eframeno]-\u003eregs[e-\u003eregno]\n--\nkernel/bpf/verifier.c-17905-\t\t\t/*\nkernel/bpf/verifier.c:17906:\t\t\t * Must preserve off and id, otherwise another sync_linked_regs()\nkernel/bpf/verifier.c-17907-\t\t\t * will be incorrect.\n--\nkernel/bpf/verifier.c=17926=static int check_cond_jmp_op(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-17933-\tstruct bpf_reg_state *eq_branch_regs;\nkernel/bpf/verifier.c:17934:\tstruct linked_regs linked_regs = {};\nkernel/bpf/verifier.c-17935-\tu8 opcode = BPF_OP(insn-\u003ecode);\n--\nkernel/bpf/verifier.c-18006-\t * Collect the linked registers before env-\u003e{true,false}_reg{1,2} setup,\nkernel/bpf/verifier.c:18007:\t * otherwise ids dropped by collect_linked_regs() would be resurrected\nkernel/bpf/verifier.c-18008-\t * when env-\u003e{true,false}_reg{1,2} are copied back.\n--\nkernel/bpf/verifier.c-18010-\tif (BPF_SRC(insn-\u003ecode) == BPF_X \u0026\u0026 src_reg-\u003etype == SCALAR_VALUE \u0026\u0026 src_reg-\u003eid)\nkernel/bpf/verifier.c:18011:\t\tcollect_linked_regs(env, this_branch, src_reg-\u003eid, \u0026linked_regs);\nkernel/bpf/verifier.c-18012-\tif (dst_reg-\u003etype == SCALAR_VALUE \u0026\u0026 dst_reg-\u003eid)\nkernel/bpf/verifier.c:18013:\t\tcollect_linked_regs(env, this_branch, dst_reg-\u003eid, \u0026linked_regs);\nkernel/bpf/verifier.c-18014-\n--\nkernel/bpf/verifier.c-18068-\t */\nkernel/bpf/verifier.c:18069:\tif (linked_regs.cnt \u003e 1) {\nkernel/bpf/verifier.c-18070-\t\tu16 packed[LINKED_REGS_MAX];\nkernel/bpf/verifier.c-18071-\nkernel/bpf/verifier.c:18072:\t\tlinked_regs_pack(\u0026linked_regs, packed);\nkernel/bpf/verifier.c:18073:\t\terr = bpf_push_jmp_history(env, this_branch, 0, 0, 0, packed, linked_regs.cnt);\nkernel/bpf/verifier.c-18074-\t\tif (err)\n--\nkernel/bpf/verifier.c-18095-\t !WARN_ON_ONCE(src_reg-\u003eid != other_branch_regs[insn-\u003esrc_reg].id)) {\nkernel/bpf/verifier.c:18096:\t\tsync_linked_regs(env, this_branch, src_reg, \u0026linked_regs);\nkernel/bpf/verifier.c:18097:\t\tsync_linked_regs(env, other_branch, \u0026other_branch_regs[insn-\u003esrc_reg],\nkernel/bpf/verifier.c:18098:\t\t\t\t \u0026linked_regs);\nkernel/bpf/verifier.c-18099-\t}\n--\nkernel/bpf/verifier.c-18101-\t !WARN_ON_ONCE(dst_reg-\u003eid != other_branch_regs[insn-\u003edst_reg].id)) {\nkernel/bpf/verifier.c:18102:\t\tsync_linked_regs(env, this_branch, dst_reg, \u0026linked_regs);\nkernel/bpf/verifier.c:18103:\t\tsync_linked_regs(env, other_branch, \u0026other_branch_regs[insn-\u003edst_reg],\nkernel/bpf/verifier.c:18104:\t\t\t\t \u0026linked_regs);\nkernel/bpf/verifier.c-18105-\t}\n"
} |
| 53/3 | 2026/09/26 22:37 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 103740 output: 28 thoughts: 1391 Thoughts: **Analyzing Potential Uninitialized Memory Reads in BPF Verifier** Okay, let's break this down. First, I was looking at `bpf_push_jmp_history`. The critical part is when we're dealing with the `jmp_history` entries and the `linked_regs`. I noticed a check (line 20) where if `env->cur_hist_ent` is not null, there is a check to ensure that `env->cur_hist_ent->linked_regs_cnt` is zero. Then, if `linked_regs_cnt` is not zero, the code copies `linked_regs` into `env->cur_hist_ent->linked_regs` using `memcpy`. My concern was whether we could read uninitialized memory during this copying. After looking at `bpf_bt_sync_linked_regs`, I saw `linked_regs_unpack` which copies `hist->linked_regs_cnt` to a local variable `s->cnt`. This suggests that the code *only* accesses valid indices. And then it accesses indices 0 through `s->cnt - 1`. That makes sense - the verifier should be safe. Plus, `bpf_jmp_history_entry` is internal, so we don't have to worry about user space. Next, I shifted my focus to the `struct bpf_idmap` and `struct bpf_idset`. I observed the relevant structures defined in `bpf_verifier.h` and the corresponding members, including `idmap->map`, `idmap->cnt`, and `idmap->cap`. Notably, the `idmap` and `idset` used to be a union, but now they are separate. The use of `krealloc_array` without `__GFP_ZERO` immediately flagged my concern: potential uninitialized memory reads. The `bpf_id_scratch_reserve` function is responsible for resizing these arrays. My question was whether `check_ids`, `idstack_push`, or `idset_cnt_inc` could read uninitialized memory that the `krealloc_array` call might leave behind. I scrutinized `check_ids`, and it iterates from `i = 0` to `i < idmap->cnt`, never reading beyond that. When it grows the array, it writes to `map[idmap->cnt]` and increments `idmap->cnt`, ensuring that it only ever reads from initialized locations. Looks good there. Then, I looked at `idstack_push`. The potential issue is with `idmap->map[idmap->cnt++].old = id;`. Is there a subsequent read of `idmap->map[i].cur` where `cur` has *not* been initialized? So, now I need to figure out where `idstack_push` or `idstack_pop` are actually used to see how they're used. That's the next step in my investigation. |
| 54/3 | 2026/09/26 22:37 | tool | grepper |
0mArgs:{
"Expression": "idstack_",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/verifier.c=10513=static int release_reference_nomark(struct bpf_verifier_env *env, int id)\n--\nkernel/bpf/verifier.c-10522-\nkernel/bpf/verifier.c:10523:static int idstack_push(struct bpf_idmap *idmap, u32 id)\nkernel/bpf/verifier.c-10524-{\n--\nkernel/bpf/verifier.c-10541-\nkernel/bpf/verifier.c:10542:static int idstack_pop(struct bpf_idmap *idmap)\nkernel/bpf/verifier.c-10543-{\n--\nkernel/bpf/verifier.c=10551=static int release_reference(struct bpf_verifier_env *env, int id)\n--\nkernel/bpf/verifier.c-10561-\tidstack-\u003ecnt = 0;\nkernel/bpf/verifier.c:10562:\terr = idstack_push(idstack, id);\nkernel/bpf/verifier.c-10563-\tif (err)\n--\nkernel/bpf/verifier.c-10570-\nkernel/bpf/verifier.c:10571:\twhile ((id = idstack_pop(idstack))) {\nkernel/bpf/verifier.c-10572-\t\t/*\n--\nkernel/bpf/verifier.c-10591-\t\t\tif (reg-\u003eparent_id == id) {\nkernel/bpf/verifier.c:10592:\t\t\t\terr = idstack_push(idstack, reg-\u003eid);\nkernel/bpf/verifier.c-10593-\t\t\t\tif (err)\n"
} |
| 55/3 | 2026/09/26 22:37 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 106243 output: 38 thoughts: 33 Thoughts: Okay, here's my interpretation of that thought process, assuming I'm already deep in the weeds of eBPF and kernel internals. **Diving into `idstack_pop`** Alright, so the task at hand is `idstack_pop`. Seems straightforward enough, but I always believe in confirming the source. Let's get right into the source code, specifically `kernel/bpf/verifier.c`. That's where all the interesting BPF verification logic lives. I need to refresh my memory on exactly how `idstack_pop` is implemented within that context, its role in the verifier, and what potential interactions or dependencies it might have. This is where the real understanding begins. Time to dig in. |
| 56/3 | 2026/09/26 22:37 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/verifier.c",
"FirstLine": 10525,
"LineCount": 25
}Results: {
"Contents": "10525:\t\tint i;\n10526:\t\n10527:\t\tif (!id)\n10528:\t\t\treturn 0;\n10529:\t\n10530:\t\tfor (i = 0; i \u003c idmap-\u003ecnt; i++)\n10531:\t\t\tif (idmap-\u003emap[i].old == id)\n10532:\t\t\t\treturn 0;\n10533:\t\n10534:\t\tif (!bpf_id_scratch_reserve((void **)\u0026idmap-\u003emap, \u0026idmap-\u003ecap, idmap-\u003ecnt,\n10535:\t\t\t\t\t sizeof(*idmap-\u003emap)))\n10536:\t\t\treturn -ENOMEM;\n10537:\t\n10538:\t\tidmap-\u003emap[idmap-\u003ecnt++].old = id;\n10539:\t\treturn 0;\n10540:\t}\n10541:\t\n10542:\tstatic int idstack_pop(struct bpf_idmap *idmap)\n10543:\t{\n10544:\t\tif (!idmap-\u003ecnt)\n10545:\t\t\treturn 0;\n10546:\t\n10547:\t\treturn idmap-\u003emap[--idmap-\u003ecnt].old;\n10548:\t}\n10549:\t\n"
} |
| 57/3 | 2026/09/26 22:37 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 106834 output: 31 thoughts: 2451 Thoughts: **Liveness Analysis Deep Dive**
Okay, let's break down these liveness analysis code snippets. The initial exploration of `idstack_push` and `idstack_pop` reveals that only `.old` is read, implying that the `.cur` field isn't utilized. Then, looking at `idset_cnt_inc`, I see the `id` and `cnt` fields are initialized during insertion and that the `num_ids` variable accurately bounds the loop. The `bpf_clear_singular_ids` function correctly resets the counter.
Next, I shifted focus to `kernel/bpf/liveness.c`. In `widen_frame_masks`, `kvzalloc_flex` is used to allocate `new`. Importantly, it zero-initializes the memory, including the newly added words. The existing masks are then copied. This guarantees that uninitialized bits are zero. Also, `new->words = words` looks safe.
Moving on to `compute_subprog_args`, I see `at_stack_in` is initialized to `unvisited` elements via loops. At the entry, stack slots are set to `none`. During callsite snapshotting, `snap->slots` is initialized and `memcpy` copies the required data from `at_stack_in` to the snapshot. Then, in the code path that reads from the snapshot, `parent->slots` is used as a bound to validate accesses into the `at` array using `fp_off_to_slot`. This looks correct and safe, and I see a check to return `imp` if out of bounds.
In `update_insn`, `new_after` is initialized to `{}`. The loop logic appears sound, with bits logically OR'd together.
`merge_instances` looks good too. I see the frame masks are widened if needed, and operations use bounds checks, specifically with the `w < s->words` checks.
Finally, I checked `kernel/bpf/verifier.c` and specifically `bpf_check`, the critical part. And I see that `env->stack_limit` is set which looks important, and that after completion, scratch memory is freed. The important part is: is `env->idmap_scratch` properly initialized? Let's go back and see how `env` is initialized. Looks like it is `kzalloc` or `kvzalloc`, which means zero-initialized!
|
| 58/3 | 2026/09/26 22:38 | tool | grepper |
0mArgs:{
"Expression": "env = ",
"PathPrefix": "kernel/bpf/verifier.c"
}Results: {
"Output": "kernel/bpf/verifier.c=320=__printf(2, 3) static void verbose(void *private_data, const char *fmt, ...)\nkernel/bpf/verifier.c-321-{\nkernel/bpf/verifier.c:322:\tstruct bpf_verifier_env *env = private_data;\nkernel/bpf/verifier.c-323-\tva_list args;\n--\nkernel/bpf/verifier.c=22380=int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,\n--\nkernel/bpf/verifier.c-22396-\t */\nkernel/bpf/verifier.c:22397:\tenv = kvzalloc_obj(struct bpf_verifier_env, GFP_KERNEL_ACCOUNT);\nkernel/bpf/verifier.c-22398-\tif (!env)\n--\nkernel/bpf/verifier.c-22400-\nkernel/bpf/verifier.c:22401:\tenv-\u003ebt.env = env;\nkernel/bpf/verifier.c-22402-\tenv-\u003eprog = *prog;\n"
} |
| 59/3 | 2026/09/26 22:38 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 110143 output: 493 thoughts: 4247 Thoughts: **Analysis of BPF Stack Limit Expansion and KMSAN Necessity** Okay, so I've been tasked with evaluating this patch that increases the BPF stack limit. My initial thought is to make sure it's safe and doesn't introduce any vulnerabilities, especially regarding uninitialized memory. I started by checking the allocation with `kvzalloc_obj`. I already know the data structures start zeroed. So no worries there. First, I examined whether any kernel structures are now being sent to user space where uninitialized fields could be exposed. The verifier log buffer *is* enlarged, but that's handled with null-terminated strings and was already tested. That's a "no" for me. Next, I looked for any conditional logic or branching depending on uninitialized variables. Everything looks initialized correctly. All bitmaps and arrays are zeroed or initialized before being used. Then, I focused on data structure initialization, and whether fields might be read before being populated. Everything is still good. All arrays are properly zeroed or initialized before use. Now, that's where things get interesting and where I had to really dig in: bounds checks and uninitialized bytes, which is really about stack access. The patch increases the stack size to 2048 bytes (from 512). I thought, "Could this allow privileged users to read uninitialized stack slots?" I checked how the verifier handles stack reads. Unprivileged users *must* have written every byte they read (`stype != STACK_INVALID`). For privileged users, they could read uninitialized stack, but what's on the stack? The thread stack, right? Wait, does KMSAN catch that? The BPF JIT code does *not* run under KMSAN and the interpreter code explicitly `kmsan_unpoison_memory`s the stack. The interpreter is only 512 bytes, anyway. I checked the stack limit: the `bpf_prog_stack_limit` returns 512, unless it's JITed. The JIT's KMSAN'd? No, JITed code does NOT have KMSAN instrumentation! I then investigated the verifier itself: Does the *verifier* access uninitialized memory? I checked `backtrack.c`, confirming that `bt->stack_masks` is zeroed out by `memset`. I also checked `scratched_stack_slots`, where `bitmap_zero` and `bitmap_fill` are used for initialization. I looked at `bpf_push_jmp_history`, verifying that `linked_regs_cnt` limits the access to `p->linked_regs` and that `states_equal` is used instead of `memcmp` for comparing state data, not the jmp history. What about `bpf_fmt_stack_mask`? `for_each_set_bit` is limited by `MAX_BPF_STACK_SLOTS`, which is the properly allocated size of the array, so no problem. `liveness.c` and `fmt_spis_mask`? All accesses are within the initialized words. `print_subprog_arg_access` initialized with `unvisited`. `bpf_id_scratch_reserve` confirms that no uninitialized entries are read. `bpf_clear_singular_ids` checks for OOM to avoid using incomplete counts. The nfp driver change just uses `bpf_stack_slot` instead of direct array index. The `bpf_clone_redirect` restriction is present. `retrieve_ptr_limit` uses `env->stack_limit + mask_to_left;`. `mark_reg_stack_read` uses `bpf_bt_set_frame_slot`. So, the key question: Does this patch introduce a risk of *uninitialized* memory usage that *only* KMSAN would catch? Let's carefully consider KASAN versus KMSAN: KASAN catches out-of-bounds accesses, use-after-free, etc. KMSAN detects *reads* of uninitialized memory. KASAN is already enabled. The expansion itself is about increased stack size. This expansion could lead to array overflows, but KASAN catches that. Memory leaks can also be there, and KASAN catches that too. The logic in the verifier is prone to bugs. No kernel structures are copied to userspace with uninitialized padding. All dynamically allocated buffers are properly initialized before being read. BPF code doesn't use KMSAN, and interpreter code unpoisons stack memory. Given this analysis, I'm confident that this patch does *not* introduce KMSAN-specific vulnerabilities. The patch's primary risks are related to bounds errors (which KASAN catches) rather than uninitialized memory reads. **NeedsKMSAN = false.** |
| 60/3 | 2026/09/26 22:38 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch series increases the maximum stack size budget for BPF programs from 512 bytes to 2048 bytes (MAX_BPF_STACK_JIT) on architectures with supporting JIT compilers (x86-64 and arm64). This requires adapting the BPF verifier's internal data structures and algorithms—including backtrack bitmasks, jump history entries, liveness analysis frame masks, and scratch id sets/maps—to handle up to 256 8-byte stack slots (or 512 4-byte half-slots).\n\nKASAN vs KMSAN applicability:\n1. Memory Safety Risks: The main risks of this change involve out-of-bounds accesses (e.g., if stack indexing or slot calculations exceed allocated bounds), bitmap indexing overflows, or memory management bugs (use-after-free, double free, memory leak) during dynamic reallocation of scratch buffers and liveness masks (e.g., in widen_frame_masks(), bpf_id_scratch_reserve(), or compute_subprog_args()). All of these conditions are directly caught by KASAN, KASAN-extra, and standard kernel bug detectors.\n2. Uninitialized Memory \u0026 Info Leaks: The patch does not introduce reads of uninitialized memory or expose kernel data to userspace.\n - Internal structures like `frame_masks` are allocated with `kvzalloc_flex()` (zero-initialized).\n - `realloc_array()` used for stack states explicitly zeroes out newly grown array elements.\n - Dynamic scratch structures (`bpf_idmap`, `bpf_idset`) and `jmp_history_entry` only access entries up to their explicitly maintained count (`cnt`, `num_ids`, `linked_regs_cnt`).\n - JITed BPF execution is not instrumented by KMSAN compiler passes, while interpreter execution remains capped at 512 bytes (and already unpoisons its interpreter stack buffer via `kmsan_unpoison_memory()`).\n\nBecause any potential memory corruption bugs arising from these changes fall squarely under KASAN detection and no uninitialized memory risks are introduced, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch series increases the maximum stack size budget for BPF programs from 512 bytes to 2048 bytes (MAX_BPF_STACK_JIT) on architectures with supporting JIT compilers (x86-64 and arm64). This requires adapting the BPF verifier's internal data structures and algorithms—including backtrack bitmasks, jump history entries, liveness analysis frame masks, and scratch id sets/maps—to handle up to 256 8-byte stack slots (or 512 4-byte half-slots).\n\nKASAN vs KMSAN applicability:\n1. Memory Safety Risks: The main risks of this change involve out-of-bounds accesses (e.g., if stack indexing or slot calculations exceed allocated bounds), bitmap indexing overflows, or memory management bugs (use-after-free, double free, memory leak) during dynamic reallocation of scratch buffers and liveness masks (e.g., in widen_frame_masks(), bpf_id_scratch_reserve(), or compute_subprog_args()). All of these conditions are directly caught by KASAN, KASAN-extra, and standard kernel bug detectors.\n2. Uninitialized Memory \u0026 Info Leaks: The patch does not introduce reads of uninitialized memory or expose kernel data to userspace.\n - Internal structures like `frame_masks` are allocated with `kvzalloc_flex()` (zero-initialized).\n - `realloc_array()` used for stack states explicitly zeroes out newly grown array elements.\n - Dynamic scratch structures (`bpf_idmap`, `bpf_idset`) and `jmp_history_entry` only access entries up to their explicitly maintained count (`cnt`, `num_ids`, `linked_regs_cnt`).\n - JITed BPF execution is not instrumented by KMSAN compiler passes, while interpreter execution remains capped at 512 bytes (and already unpoisons its interpreter stack buffer via `kmsan_unpoison_memory()`).\n\nBecause any potential memory corruption bugs arising from these changes fall squarely under KASAN detection and no uninitialized memory risks are introduced, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|