AI Trajectory


Trajectory: (Expand/Collapse All)
Seq Timestamp Type Name Duration
0/0 2026/07/23 14:15 flow patch-triage
0m Args:
null

Results:
{
  "EnableConfigs": [
    "BPF_SYSCALL"
  ],
  "FocusSymbols": [
    "bpf_fixup_kfunc_call",
    "check_kfunc_call",
    "update_iter_num_new_state",
    "inline_bpf_iter_num_new",
    "inline_bpf_iter_num_next",
    "inline_bpf_iter_num_destroy"
  ],
  "Reasoning": "The patch modifies the BPF verifier to inline bpf_iter_num_new, bpf_iter_num_next, and bpf_iter_num_destroy kfuncs. This is a functional change to the BPF verifier that optimizes the execution of these kfuncs and can be reached by loading BPF programs from userspace.",
  "WorthFuzzing": true
}

1/1 2026/07/23 14:15 action read-patch-diff
0m Args:
null

Results:
{
  "PatchDiff": "commit 46f7815ceb25c374f60f0738ede828f08a942501\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate:   Thu Jul 23 14:15:34 2026 +0000\n\n    syz-cluster: applied patch under review\n\ndiff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h\nindex 682c2cd3b84430..8c04e2fd80170b 100644\n--- a/include/linux/bpf_verifier.h\n+++ b/include/linux/bpf_verifier.h\n@@ -612,6 +612,15 @@ struct bpf_loop_inline_state {\n \tu32 callback_subprogno; /* valid when fit_for_inline is true */\n };\n \n+struct bpf_iter_num_new_state {\n+\tunsigned int initialized:1; /* set to true upon first entry */\n+\tunsigned int fit_for_inline:1; /* true if start and end are constant\n+\t\t\t\t\t* with the same value at each call\n+\t\t\t\t\t*/\n+\ts32 start; /* valid when fit_for_inline is true */\n+\ts32 end;   /* valid when fit_for_inline is true */\n+};\n+\n /* pointer and state for maps */\n struct bpf_map_ptr_state {\n \tstruct bpf_map *map_ptr;\n@@ -661,6 +670,10 @@ struct bpf_insn_aux_data {\n \t\t * the state of the relevant registers to make decision about inlining\n \t\t */\n \t\tstruct bpf_loop_inline_state loop_inline_state;\n+\t\t/* if instruction is a call to bpf_iter_num_new this field tracks its start/end\n+\t\t * arguments to make a decision about eliding the range checks when inlining\n+\t\t */\n+\t\tstruct bpf_iter_num_new_state iter_num_new_state;\n \t};\n \tunion {\n \t\t/* remember the size of type passed to bpf_obj_new to rewrite R1 */\ndiff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c\nindex 52be0a118cce07..d5b73ae8019443 100644\n--- a/kernel/bpf/verifier.c\n+++ b/kernel/bpf/verifier.c\n@@ -12962,6 +12962,47 @@ static int check_special_kfunc(struct bpf_verifier_env *env, struct bpf_call_arg\n \n static int check_return_code(struct bpf_verifier_env *env, int regno, const char *reg_name);\n \n+/*\n+ * bpf_iter_num_new() can be inlined more tightly when its start and end arguments are constant,\n+ * as the range checks and their error paths can then be resolved at rewrite time. Record, per\n+ * call site, whether both arguments are constant with the same value on every visit.\n+ */\n+static int update_iter_num_new_state(struct bpf_verifier_env *env)\n+{\n+\tstruct bpf_iter_num_new_state *state = \u0026cur_aux(env)-\u003eiter_num_new_state;\n+\tstruct bpf_reg_state *start_reg = \u0026cur_regs(env)[BPF_REG_2];\n+\tstruct bpf_reg_state *end_reg = \u0026cur_regs(env)[BPF_REG_3];\n+\tbool known = tnum_is_const(start_reg-\u003evar_off) \u0026\u0026 tnum_is_const(end_reg-\u003evar_off);\n+\ts32 start = start_reg-\u003evar_off.value;\n+\ts32 end = end_reg-\u003evar_off.value;\n+\tint err;\n+\n+\tif (!state-\u003einitialized) {\n+\t\tstate-\u003einitialized = 1;\n+\t\tstate-\u003efit_for_inline = known;\n+\t\tstate-\u003estart = start;\n+\t\tstate-\u003eend = end;\n+\t} else if (state-\u003efit_for_inline) {\n+\t\tstate-\u003efit_for_inline = known \u0026\u0026 state-\u003estart == start \u0026\u0026 state-\u003eend == end;\n+\t}\n+\n+\t/*\n+\t * While folding is still viable the decision relies on the exact constant values, so mark\n+\t * the registers precise; otherwise the verifier could prune a path that reaches this call\n+\t * with different constants and the folded code would run with a stale value.\n+\t */\n+\tif (state-\u003efit_for_inline) {\n+\t\terr = mark_chain_precision(env, BPF_REG_2);\n+\t\tif (err)\n+\t\t\treturn err;\n+\t\terr = mark_chain_precision(env, BPF_REG_3);\n+\t\tif (err)\n+\t\t\treturn err;\n+\t}\n+\n+\treturn 0;\n+}\n+\n static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \t\t\t    int *insn_idx_p)\n {\n@@ -13163,6 +13204,12 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \t\t}\n \t}\n \n+\tif (meta.func_id == special_kfunc_list[KF_bpf_iter_num_new]) {\n+\t\terr = update_iter_num_new_state(env);\n+\t\tif (err)\n+\t\t\treturn err;\n+\t}\n+\n \tfor (i = 0; i \u003c CALLER_SAVED_REGS; i++) {\n \t\tu32 regno = caller_saved[i];\n \n@@ -19770,6 +19817,109 @@ static void __fixup_collection_insert_kfunc(struct bpf_insn_aux_data *insn_aux,\n \t*cnt = 4;\n }\n \n+/*\n+ * Inline bpf_iter_num_new(). R1 holds the pointer to the iterator, R2 and R3 hold the (int)\n+ * start and end arguments. Keep in sync with the kfunc in kernel/bpf/bpf_iter.c.\n+ */\n+static int inline_bpf_iter_num_new(struct bpf_insn *insn_buf, struct bpf_iter_num_new_state *state)\n+{\n+\tint i = 0;\n+\n+\t/*\n+\t * When start and end are constant the range checks and their error paths fold away,\n+\t * leaving just the state init or the error result.\n+\t */\n+\tif (state-\u003efit_for_inline) {\n+\t\ts32 start = state-\u003estart, end = state-\u003eend;\n+\n+\t\tif (start \u003e end) {\n+\t\t\t/* s-\u003ecur = s-\u003eend = 0; return -EINVAL; */\n+\t\t\tinsn_buf[i++] = BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 0);\n+\t\t\tinsn_buf[i++] = BPF_MOV64_IMM(BPF_REG_0, -EINVAL);\n+\t\t} else if ((s64)end - (s64)start \u003e BPF_MAX_LOOPS) {\n+\t\t\t/* s-\u003ecur = s-\u003eend = 0; return -E2BIG; */\n+\t\t\tinsn_buf[i++] = BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 0);\n+\t\t\tinsn_buf[i++] = BPF_MOV64_IMM(BPF_REG_0, -E2BIG);\n+\t\t} else {\n+\t\t\t/* s-\u003ecur = start - 1; s-\u003eend = end; return 0; */\n+\t\t\tinsn_buf[i++] = BPF_ST_MEM(BPF_W, BPF_REG_1, 0, start - 1);\n+\t\t\tinsn_buf[i++] = BPF_ST_MEM(BPF_W, BPF_REG_1, 4, end);\n+\t\t\tinsn_buf[i++] = BPF_MOV64_IMM(BPF_REG_0, 0);\n+\t\t}\n+\t\treturn i;\n+\t}\n+\n+\t/* if (start \u003e end) goto einval; */\n+\tinsn_buf[i++] = BPF_JMP32_REG(BPF_JSGT, BPF_REG_2, BPF_REG_3, 8);\n+\t/*\n+\t * start \u003c= end here, so the range end - start fits in a u32; compute it as a 32-bit\n+\t * subtraction that zero-extends into r0.\n+\t */\n+\tinsn_buf[i++] = BPF_MOV32_REG(BPF_REG_0, BPF_REG_3);\n+\tinsn_buf[i++] = BPF_ALU32_REG(BPF_SUB, BPF_REG_0, BPF_REG_2);\n+\t/* if (r0 \u003e BPF_MAX_LOOPS) goto e2big; */\n+\tinsn_buf[i++] = BPF_JMP_IMM(BPF_JSGT, BPF_REG_0, BPF_MAX_LOOPS, 8);\n+\t/* s-\u003ecur = start - 1; */\n+\tinsn_buf[i++] = BPF_ALU32_IMM(BPF_ADD, BPF_REG_2, -1);\n+\tinsn_buf[i++] = BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_2, 0);\n+\t/* s-\u003eend = end; */\n+\tinsn_buf[i++] = BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_3, 4);\n+\t/* return 0; */\n+\tinsn_buf[i++] = BPF_MOV64_IMM(BPF_REG_0, 0);\n+\tinsn_buf[i++] = BPF_JMP_A(5);\n+\t/* einval: s-\u003ecur = s-\u003eend = 0; return -EINVAL; */\n+\tinsn_buf[i++] = BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 0);\n+\tinsn_buf[i++] = BPF_MOV64_IMM(BPF_REG_0, -EINVAL);\n+\tinsn_buf[i++] = BPF_JMP_A(2);\n+\t/* e2big: s-\u003ecur = s-\u003eend = 0; return -E2BIG; */\n+\tinsn_buf[i++] = BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 0);\n+\tinsn_buf[i++] = BPF_MOV64_IMM(BPF_REG_0, -E2BIG);\n+\n+\treturn i;\n+}\n+\n+/*\n+ * Inline bpf_iter_num_next(). R1 holds the pointer to the iterator. Keep in sync with the\n+ * kfunc in kernel/bpf/bpf_iter.c.\n+ */\n+static int inline_bpf_iter_num_next(struct bpf_insn *insn_buf)\n+{\n+\tint i = 0;\n+\n+\t/*\n+\t * s-\u003ecur and s-\u003eend are int, so the (s64)(s-\u003ecur + 1) \u003e= s-\u003eend check is equivalent to a\n+\t * signed 32-bit comparison of (s-\u003ecur + 1) against s-\u003eend and needs no sign extension.\n+\t */\n+\tinsn_buf[i++] = BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, 0);\n+\tinsn_buf[i++] = BPF_ALU32_IMM(BPF_ADD, BPF_REG_0, 1);\n+\tinsn_buf[i++] = BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, 4);\n+\t/* if ((s32)(s-\u003ecur + 1) \u003e= (s32)s-\u003eend) goto done; */\n+\tinsn_buf[i++] = BPF_JMP32_REG(BPF_JSGE, BPF_REG_0, BPF_REG_2, 3);\n+\t/* s-\u003ecur++; return \u0026s-\u003ecur; */\n+\tinsn_buf[i++] = BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0, 0);\n+\tinsn_buf[i++] = BPF_MOV64_REG(BPF_REG_0, BPF_REG_1);\n+\tinsn_buf[i++] = BPF_JMP_A(2);\n+\t/* done: s-\u003ecur = s-\u003eend = 0; return NULL; */\n+\tinsn_buf[i++] = BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 0);\n+\tinsn_buf[i++] = BPF_MOV64_IMM(BPF_REG_0, 0);\n+\n+\treturn i;\n+}\n+\n+/*\n+ * Inline bpf_iter_num_destroy(). R1 holds the pointer to the iterator. Keep in sync with\n+ * the kfunc in kernel/bpf/bpf_iter.c.\n+ */\n+static int inline_bpf_iter_num_destroy(struct bpf_insn *insn_buf)\n+{\n+\tint i = 0;\n+\n+\t/* s-\u003ecur = s-\u003eend = 0; */\n+\tinsn_buf[i++] = BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 0);\n+\n+\treturn i;\n+}\n+\n int bpf_fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \t\t     struct bpf_insn *insn_buf, int insn_idx, int *cnt)\n {\n@@ -19899,6 +20049,13 @@ int bpf_fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \t\tinsn_buf[4] = BPF_ALU64_REG(BPF_SUB, BPF_REG_0, BPF_REG_1);\n \t\tinsn_buf[5] = BPF_ALU64_IMM(BPF_NEG, BPF_REG_0, 0);\n \t\t*cnt = 6;\n+\t} else if (desc-\u003efunc_id == special_kfunc_list[KF_bpf_iter_num_new]) {\n+\t\t*cnt = inline_bpf_iter_num_new(insn_buf,\n+\t\t\t\t\t       \u0026env-\u003einsn_aux_data[insn_idx].iter_num_new_state);\n+\t} else if (desc-\u003efunc_id == special_kfunc_list[KF_bpf_iter_num_next]) {\n+\t\t*cnt = inline_bpf_iter_num_next(insn_buf);\n+\t} else if (desc-\u003efunc_id == special_kfunc_list[KF_bpf_iter_num_destroy]) {\n+\t\t*cnt = inline_bpf_iter_num_destroy(insn_buf);\n \t}\n \n \tif (env-\u003einsn_aux_data[insn_idx].arg_prog) {\ndiff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile\nindex 55d394438705a6..3174c2f18e7150 100644\n--- a/tools/testing/selftests/bpf/Makefile\n+++ b/tools/testing/selftests/bpf/Makefile\n@@ -965,6 +965,7 @@ $(OUTPUT)/bench_ringbufs.o: $(OUTPUT)/ringbuf_bench.skel.h \\\n \t\t\t    $(OUTPUT)/perfbuf_bench.skel.h\n $(OUTPUT)/bench_bloom_filter_map.o: $(OUTPUT)/bloom_filter_bench.skel.h\n $(OUTPUT)/bench_bpf_loop.o: $(OUTPUT)/bpf_loop_bench.skel.h\n+$(OUTPUT)/bench_bpf_for.o: $(OUTPUT)/bpf_for_bench.skel.h\n $(OUTPUT)/bench_strncmp.o: $(OUTPUT)/strncmp_bench.skel.h\n $(OUTPUT)/bench_bpf_hashmap_full_update.o: $(OUTPUT)/bpf_hashmap_full_update_bench.skel.h\n $(OUTPUT)/bench_local_storage.o: $(OUTPUT)/local_storage_bench.skel.h\n@@ -990,6 +991,7 @@ $(OUTPUT)/bench: $(OUTPUT)/bench.o \\\n \t\t $(OUTPUT)/bench_ringbufs.o \\\n \t\t $(OUTPUT)/bench_bloom_filter_map.o \\\n \t\t $(OUTPUT)/bench_bpf_loop.o \\\n+\t\t $(OUTPUT)/bench_bpf_for.o \\\n \t\t $(OUTPUT)/bench_strncmp.o \\\n \t\t $(OUTPUT)/bench_bpf_hashmap_full_update.o \\\n \t\t $(OUTPUT)/bench_local_storage.o \\\ndiff --git a/tools/testing/selftests/bpf/bench.c b/tools/testing/selftests/bpf/bench.c\nindex 3d9d2cd7764bd3..b86b73456d3ca8 100644\n--- a/tools/testing/selftests/bpf/bench.c\n+++ b/tools/testing/selftests/bpf/bench.c\n@@ -276,6 +276,7 @@ static const struct argp_option opts[] = {\n extern struct argp bench_ringbufs_argp;\n extern struct argp bench_bloom_map_argp;\n extern struct argp bench_bpf_loop_argp;\n+extern struct argp bench_bpf_for_argp;\n extern struct argp bench_local_storage_argp;\n extern struct argp bench_local_storage_rcu_tasks_trace_argp;\n extern struct argp bench_strncmp_argp;\n@@ -292,6 +293,7 @@ static const struct argp_child bench_parsers[] = {\n \t{ \u0026bench_ringbufs_argp, 0, \"Ring buffers benchmark\", 0 },\n \t{ \u0026bench_bloom_map_argp, 0, \"Bloom filter map benchmark\", 0 },\n \t{ \u0026bench_bpf_loop_argp, 0, \"bpf_loop helper benchmark\", 0 },\n+\t{ \u0026bench_bpf_for_argp, 0, \"bpf_for loop benchmark\", 0 },\n \t{ \u0026bench_local_storage_argp, 0, \"local_storage benchmark\", 0 },\n \t{ \u0026bench_strncmp_argp, 0, \"bpf_strncmp helper benchmark\", 0 },\n \t{ \u0026bench_local_storage_rcu_tasks_trace_argp, 0,\n@@ -557,6 +559,7 @@ extern const struct bench bench_bloom_false_positive;\n extern const struct bench bench_hashmap_without_bloom;\n extern const struct bench bench_hashmap_with_bloom;\n extern const struct bench bench_bpf_loop;\n+extern const struct bench bench_bpf_for;\n extern const struct bench bench_strncmp_no_helper;\n extern const struct bench bench_strncmp_helper;\n extern const struct bench bench_bpf_hashmap_full_update;\n@@ -640,6 +643,7 @@ static const struct bench *benchs[] = {\n \t\u0026bench_hashmap_without_bloom,\n \t\u0026bench_hashmap_with_bloom,\n \t\u0026bench_bpf_loop,\n+\t\u0026bench_bpf_for,\n \t\u0026bench_strncmp_no_helper,\n \t\u0026bench_strncmp_helper,\n \t\u0026bench_bpf_hashmap_full_update,\ndiff --git a/tools/testing/selftests/bpf/benchs/bench_bpf_for.c b/tools/testing/selftests/bpf/benchs/bench_bpf_for.c\nnew file mode 100644\nindex 00000000000000..730c51ad2dec34\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/benchs/bench_bpf_for.c\n@@ -0,0 +1,104 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+\n+#include \u003cargp.h\u003e\n+#include \"bench.h\"\n+#include \"bpf_for_bench.skel.h\"\n+\n+/* BPF triggering benchmarks */\n+static struct ctx {\n+\tstruct bpf_for_bench *skel;\n+} ctx;\n+\n+static struct {\n+\t__u32 nr_loops;\n+} args = {\n+\t/*\n+\t * Default to a large loop count so the per-iteration bpf_iter_num_next() cost dominates\n+\t * the one-time bpf_iter_num_new()/destroy() setup and teardown.\n+\t */\n+\t.nr_loops = 1000,\n+};\n+\n+enum {\n+\tARG_NR_LOOPS = 4000,\n+};\n+\n+static const struct argp_option opts[] = {\n+\t{ \"nr_loops\", ARG_NR_LOOPS, \"nr_loops\", 0,\n+\t\t\"Set number of iterations for the bpf_for() loop\"},\n+\t{},\n+};\n+\n+static error_t parse_arg(int key, char *arg, struct argp_state *state)\n+{\n+\tswitch (key) {\n+\tcase ARG_NR_LOOPS:\n+\t\targs.nr_loops = strtol(arg, NULL, 10);\n+\t\tbreak;\n+\tdefault:\n+\t\treturn ARGP_ERR_UNKNOWN;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+/* exported into benchmark runner */\n+const struct argp bench_bpf_for_argp = {\n+\t.options = opts,\n+\t.parser = parse_arg,\n+};\n+\n+static void validate(void)\n+{\n+\tif (env.consumer_cnt != 0) {\n+\t\tfprintf(stderr, \"benchmark doesn't support consumer!\\n\");\n+\t\texit(1);\n+\t}\n+}\n+\n+static void *producer(void *input)\n+{\n+\twhile (true)\n+\t\t/* trigger the bpf program */\n+\t\tsyscall(__NR_getpgid);\n+\n+\treturn NULL;\n+}\n+\n+static void measure(struct bench_res *res)\n+{\n+\tres-\u003ehits = atomic_swap(\u0026ctx.skel-\u003ebss-\u003ehits, 0);\n+}\n+\n+static void setup(void)\n+{\n+\tstruct bpf_link *link;\n+\n+\tsetup_libbpf();\n+\n+\tctx.skel = bpf_for_bench__open_and_load();\n+\tif (!ctx.skel) {\n+\t\tfprintf(stderr, \"failed to open skeleton\\n\");\n+\t\texit(1);\n+\t}\n+\n+\tlink = bpf_program__attach(ctx.skel-\u003eprogs.benchmark);\n+\tif (!link) {\n+\t\tfprintf(stderr, \"failed to attach program!\\n\");\n+\t\texit(1);\n+\t}\n+\n+\tctx.skel-\u003ebss-\u003enr_loops = args.nr_loops;\n+}\n+\n+const struct bench bench_bpf_for = {\n+\t.name = \"bpf-for\",\n+\t.argp = \u0026bench_bpf_for_argp,\n+\t.validate = validate,\n+\t.setup = setup,\n+\t.producer_thread = producer,\n+\t.measure = measure,\n+\t.report_progress = ops_report_progress,\n+\t.report_final = ops_report_final,\n+};\ndiff --git a/tools/testing/selftests/bpf/benchs/run_bench_bpf_for.sh b/tools/testing/selftests/bpf/benchs/run_bench_bpf_for.sh\nnew file mode 100755\nindex 00000000000000..7da6453920dab3\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/benchs/run_bench_bpf_for.sh\n@@ -0,0 +1,15 @@\n+#!/bin/bash\n+# SPDX-License-Identifier: GPL-2.0\n+\n+source ./benchs/run_common.sh\n+\n+set -eufo pipefail\n+\n+for t in 1 4 8 12 16; do\n+for i in 10 100 500 1000 5000 10000 50000 100000 500000 1000000; do\n+subtitle \"nr_loops: $i, nr_threads: $t\"\n+\tsummarize_ops \"bpf_for: \" \\\n+\t    \"$($RUN_BENCH -p $t --nr_loops $i bpf-for)\"\n+\tprintf \"\\n\"\n+done\n+done\ndiff --git a/tools/testing/selftests/bpf/progs/bpf_for_bench.c b/tools/testing/selftests/bpf/progs/bpf_for_bench.c\nnew file mode 100644\nindex 00000000000000..2752de5c44f57a\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/bpf_for_bench.c\n@@ -0,0 +1,32 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+\n+#include \"vmlinux.h\"\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \"bpf_misc.h\"\n+\n+char _license[] SEC(\"license\") = \"GPL\";\n+\n+u32 nr_loops;\n+long hits;\n+\n+static int outer_loop(__u32 index, void *data)\n+{\n+\tint i;\n+\n+\t/*\n+\t * Empty body: the work being measured is the open-coded numeric iterator itself\n+\t * (bpf_iter_num_new/next/destroy behind bpf_for()).\n+\t */\n+\tbpf_for(i, 0, nr_loops)\n+\t\t;\n+\t__sync_add_and_fetch(\u0026hits, nr_loops);\n+\treturn 0;\n+}\n+\n+SEC(\"fentry/\" SYS_PREFIX \"sys_getpgid\")\n+int benchmark(void *ctx)\n+{\n+\tbpf_loop(1000, outer_loop, NULL, 0);\n+\treturn 0;\n+}\ndiff --git a/tools/testing/selftests/bpf/progs/iters.c b/tools/testing/selftests/bpf/progs/iters.c\nindex 0fa70b133d9322..c4df589ecd7ae4 100644\n--- a/tools/testing/selftests/bpf/progs/iters.c\n+++ b/tools/testing/selftests/bpf/progs/iters.c\n@@ -88,6 +88,151 @@ int iter_err_unsafe_asm_loop(const void *ctx)\n \treturn 0;\n }\n \n+/*\n+ * Naked functions, so there is no compiler-generated glue and the whole inlined program can be\n+ * matched. Pinned to arches whose JITs zero-extend 32-bit writes implicitly\n+ * (bpf_jit_needs_zext() == false); on arches that need explicit zero-extension the verifier\n+ * interleaves \"wN = wN\" insns and the fixed shape below would not match. The inlining itself is\n+ * arch independent, so checking it on these arches is sufficient.\n+ *\n+ * With constant bounds bpf_iter_num_new() is inlined with the range checks folded away, just\n+ * storing the initial state (s-\u003ecur = start - 1, s-\u003eend = end); bpf_iter_num_next() and\n+ * bpf_iter_num_destroy() are inlined too, leaving a call-free loop.\n+ */\n+SEC(\"raw_tp\")\n+__arch_x86_64\n+__arch_arm64\n+__success\n+__xlated(\"r6 = r10\")\n+__xlated(\"r6 += -8\")\n+__xlated(\"r1 = r6\")\n+__xlated(\"r2 = 0\")\n+__xlated(\"r3 = 10\")\n+/* bpf_iter_num_new(\u0026it, 0, 10) folded to the state init */\n+__xlated(\"*(u32 *)(r1 +0) = -1\")\n+__xlated(\"*(u32 *)(r1 +4) = 10\")\n+__xlated(\"r0 = 0\")\n+__xlated(\"r1 = r6\")\n+/* bpf_iter_num_next(\u0026it) */\n+__xlated(\"r0 = *(u32 *)(r1 +0)\")\n+__xlated(\"w0 += 1\")\n+__xlated(\"r2 = *(u32 *)(r1 +4)\")\n+__xlated(\"if w0 s\u003e= w2 goto pc+3\")\n+__xlated(\"*(u32 *)(r1 +0) = r0\")\n+__xlated(\"r0 = r1\")\n+__xlated(\"goto pc+2\")\n+__xlated(\"*(u64 *)(r1 +0) = 0\")\n+__xlated(\"r0 = 0\")\n+__xlated(\"if r0 != 0x0 goto pc-11\")\n+__xlated(\"r1 = r6\")\n+/* bpf_iter_num_destroy(\u0026it) */\n+__xlated(\"*(u64 *)(r1 +0) = 0\")\n+__xlated(\"r0 = 0\")\n+__xlated(\"exit\")\n+int __naked iter_num_new_const_folded(void)\n+{\n+\tasm volatile (\n+\t\t/* r6 points to struct bpf_iter_num on the stack */\n+\t\t\"r6 = r10;\"\n+\t\t\"r6 += -8;\"\n+\t\t\"r1 = r6;\"\n+\t\t\"r2 = 0;\"\n+\t\t\"r3 = 10;\"\n+\t\t\"call %[bpf_iter_num_new];\"\n+\t\"1:\"\n+\t\t\"r1 = r6;\"\n+\t\t\"call %[bpf_iter_num_next];\"\n+\t\t\"if r0 != 0 goto 1b;\"\n+\t\t\"r1 = r6;\"\n+\t\t\"call %[bpf_iter_num_destroy];\"\n+\t\t\"r0 = 0;\"\n+\t\t\"exit;\"\n+\t\t:\n+\t\t: __imm(bpf_iter_num_new),\n+\t\t  __imm(bpf_iter_num_next),\n+\t\t  __imm(bpf_iter_num_destroy)\n+\t\t: __clobber_common, \"r6\"\n+\t);\n+}\n+\n+/*\n+ * Same arch pinning and naked-function rationale as above. With a non-constant bound\n+ * bpf_iter_num_new() keeps the full range check: the distance is computed and both the -EINVAL\n+ * and -E2BIG error paths remain. bpf_iter_num_next() and bpf_iter_num_destroy() are inlined too.\n+ */\n+SEC(\"raw_tp\")\n+__arch_x86_64\n+__arch_arm64\n+__success\n+__xlated(\"r6 = r10\")\n+__xlated(\"r6 += -8\")\n+__xlated(\"call unknown\")\n+__xlated(\"r3 = r0\")\n+__xlated(\"r3 \u0026= 65535\")\n+__xlated(\"r1 = r6\")\n+__xlated(\"r2 = 0\")\n+/* bpf_iter_num_new(\u0026it, 0, \u003cnon-const\u003e) with the range check kept */\n+__xlated(\"if w2 s\u003e w3 goto pc+8\")\n+__xlated(\"w0 = w3\")\n+__xlated(\"w0 -= w2\")\n+__xlated(\"if r0 s\u003e 0x800000 goto pc+8\")\n+__xlated(\"w2 += -1\")\n+__xlated(\"*(u32 *)(r1 +0) = r2\")\n+__xlated(\"*(u32 *)(r1 +4) = r3\")\n+__xlated(\"r0 = 0\")\n+__xlated(\"goto pc+5\")\n+__xlated(\"*(u64 *)(r1 +0) = 0\")\n+__xlated(\"r0 = -22\")\n+__xlated(\"goto pc+2\")\n+__xlated(\"*(u64 *)(r1 +0) = 0\")\n+__xlated(\"r0 = -7\")\n+__xlated(\"r1 = r6\")\n+/* bpf_iter_num_next(\u0026it) */\n+__xlated(\"r0 = *(u32 *)(r1 +0)\")\n+__xlated(\"w0 += 1\")\n+__xlated(\"r2 = *(u32 *)(r1 +4)\")\n+__xlated(\"if w0 s\u003e= w2 goto pc+3\")\n+__xlated(\"*(u32 *)(r1 +0) = r0\")\n+__xlated(\"r0 = r1\")\n+__xlated(\"goto pc+2\")\n+__xlated(\"*(u64 *)(r1 +0) = 0\")\n+__xlated(\"r0 = 0\")\n+__xlated(\"if r0 != 0x0 goto pc-11\")\n+__xlated(\"r1 = r6\")\n+/* bpf_iter_num_destroy(\u0026it) */\n+__xlated(\"*(u64 *)(r1 +0) = 0\")\n+__xlated(\"r0 = 0\")\n+__xlated(\"exit\")\n+int __naked iter_num_new_inlined(void)\n+{\n+\tasm volatile (\n+\t\t/* r6 points to struct bpf_iter_num on the stack */\n+\t\t\"r6 = r10;\"\n+\t\t\"r6 += -8;\"\n+\t\t/* non-constant end so the range checks are kept */\n+\t\t\"call %[bpf_get_prandom_u32];\"\n+\t\t\"r3 = r0;\"\n+\t\t\"r3 \u0026= 0xffff;\"\n+\t\t\"r1 = r6;\"\n+\t\t\"r2 = 0;\"\n+\t\t\"call %[bpf_iter_num_new];\"\n+\t\"1:\"\n+\t\t\"r1 = r6;\"\n+\t\t\"call %[bpf_iter_num_next];\"\n+\t\t\"if r0 != 0 goto 1b;\"\n+\t\t\"r1 = r6;\"\n+\t\t\"call %[bpf_iter_num_destroy];\"\n+\t\t\"r0 = 0;\"\n+\t\t\"exit;\"\n+\t\t:\n+\t\t: __imm(bpf_get_prandom_u32),\n+\t\t  __imm(bpf_iter_num_new),\n+\t\t  __imm(bpf_iter_num_next),\n+\t\t  __imm(bpf_iter_num_destroy)\n+\t\t: __clobber_common, \"r6\"\n+\t);\n+}\n+\n SEC(\"raw_tp\")\n __success\n int iter_while_loop(const void *ctx)\n"
}

2/1 2026/07/23 14:15 agent patch-evaluator
0m Model:
best-expensive

Args:
null

Results:
{
  "EnableConfigs": [
    "BPF_SYSCALL"
  ],
  "FocusSymbols": [
    "bpf_fixup_kfunc_call",
    "check_kfunc_call",
    "update_iter_num_new_state",
    "inline_bpf_iter_num_new",
    "inline_bpf_iter_num_next",
    "inline_bpf_iter_num_destroy"
  ],
  "Reasoning": "The patch modifies the BPF verifier to inline bpf_iter_num_new, bpf_iter_num_next, and bpf_iter_num_destroy kfuncs. This is a functional change to the BPF verifier that optimizes the execution of these kfuncs and can be reached by loading BPF programs from userspace.",
  "WorthFuzzing": true
}

Instruction:
You are an expert Linux kernel maintainer.
Your job is to review a provided patch series and determine
if it makes functional changes to the kernel that should be fuzzed.

IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on your internal knowledge of the kernel. You must actively
use your code access tools to examine the actual source code and confirm any assumptions.

Return WorthFuzzing=false if the patch only contains:
- Modifications to Documentation/, Kconfig files, or code comments.
- Purely decorative changes, such as logging (e.g., pr_err, printk) or tracepoints.
- Changes to numeric constants or macros that do not functionally alter execution flow.
- Code paths that are impossible to reach in virtualized environments like GCE or QEMU,
even when utilizing software-emulated hardware (e.g., usb gadget, mac80211_hwsim).

If it modifies reachable core kernel logic, drivers, or architectures, use your code search
tools to verify the code can be executed, then return WorthFuzzing=true.

When returning WorthFuzzing=true, you MUST ALSO:
1. Extract any specific kernel functions that should be heavily fuzzed into FocusSymbols.
   Avoid listing generic hot-path functions to prevent skewed test distributions.
2. Identify any specific CONFIG_ options required to properly test this new/modified feature.
   Go and look into the Kconfig files and check for ifdefs around the code, do not make assumptions.
   Do not list too generic configs (we already have them enabled). Only list those that
   specifically cover the modified code. List them in the EnableConfigs output array,
   and DO NOT add a 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.


Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.

Prompt:
For your convenience, here is the diff of the changes:
commit 46f7815ceb25c374f60f0738ede828f08a942501
Author: syz-cluster <triage@syzkaller.com>
Date:   Thu Jul 23 14:15:34 2026 +0000

    syz-cluster: applied patch under review

diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 682c2cd3b84430..8c04e2fd80170b 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -612,6 +612,15 @@ struct bpf_loop_inline_state {
 	u32 callback_subprogno; /* valid when fit_for_inline is true */
 };
 
+struct bpf_iter_num_new_state {
+	unsigned int initialized:1; /* set to true upon first entry */
+	unsigned int fit_for_inline:1; /* true if start and end are constant
+					* with the same value at each call
+					*/
+	s32 start; /* valid when fit_for_inline is true */
+	s32 end;   /* valid when fit_for_inline is true */
+};
+
 /* pointer and state for maps */
 struct bpf_map_ptr_state {
 	struct bpf_map *map_ptr;
@@ -661,6 +670,10 @@ struct bpf_insn_aux_data {
 		 * the state of the relevant registers to make decision about inlining
 		 */
 		struct bpf_loop_inline_state loop_inline_state;
+		/* if instruction is a call to bpf_iter_num_new this field tracks its start/end
+		 * arguments to make a decision about eliding the range checks when inlining
+		 */
+		struct bpf_iter_num_new_state iter_num_new_state;
 	};
 	union {
 		/* remember the size of type passed to bpf_obj_new to rewrite R1 */
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 52be0a118cce07..d5b73ae8019443 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -12962,6 +12962,47 @@ static int check_special_kfunc(struct bpf_verifier_env *env, struct bpf_call_arg
 
 static int check_return_code(struct bpf_verifier_env *env, int regno, const char *reg_name);
 
+/*
+ * bpf_iter_num_new() can be inlined more tightly when its start and end arguments are constant,
+ * as the range checks and their error paths can then be resolved at rewrite time. Record, per
+ * call site, whether both arguments are constant with the same value on every visit.
+ */
+static int update_iter_num_new_state(struct bpf_verifier_env *env)
+{
+	struct bpf_iter_num_new_state *state = &cur_aux(env)->iter_num_new_state;
+	struct bpf_reg_state *start_reg = &cur_regs(env)[BPF_REG_2];
+	struct bpf_reg_state *end_reg = &cur_regs(env)[BPF_REG_3];
+	bool known = tnum_is_const(start_reg->var_off) && tnum_is_const(end_reg->var_off);
+	s32 start = start_reg->var_off.value;
+	s32 end = end_reg->var_off.value;
+	int err;
+
+	if (!state->initialized) {
+		state->initialized = 1;
+		state->fit_for_inline = known;
+		state->start = start;
+		state->end = end;
+	} else if (state->fit_for_inline) {
+		state->fit_for_inline = known && state->start == start && state->end == end;
+	}
+
+	/*
+	 * While folding is still viable the decision relies on the exact constant values, so mark
+	 * the registers precise; otherwise the verifier could prune a path that reaches this call
+	 * with different constants and the folded code would run with a stale value.
+	 */
+	if (state->fit_for_inline) {
+		err = mark_chain_precision(env, BPF_REG_2);
+		if (err)
+			return err;
+		err = mark_chain_precision(env, BPF_REG_3);
+		if (err)
+			return err;
+	}
+
+	return 0;
+}
+
 static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 			    int *insn_idx_p)
 {
@@ -13163,6 +13204,12 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 		}
 	}
 
+	if (meta.func_id == special_kfunc_list[KF_bpf_iter_num_new]) {
+		err = update_iter_num_new_state(env);
+		if (err)
+			return err;
+	}
+
 	for (i = 0; i < CALLER_SAVED_REGS; i++) {
 		u32 regno = caller_saved[i];
 
@@ -19770,6 +19817,109 @@ static void __fixup_collection_insert_kfunc(struct bpf_insn_aux_data *insn_aux,
 	*cnt = 4;
 }
 
+/*
+ * Inline bpf_iter_num_new(). R1 holds the pointer to the iterator, R2 and R3 hold the (int)
+ * start and end arguments. Keep in sync with the kfunc in kernel/bpf/bpf_iter.c.
+ */
+static int inline_bpf_iter_num_new(struct bpf_insn *insn_buf, struct bpf_iter_num_new_state *state)
+{
+	int i = 0;
+
+	/*
+	 * When start and end are constant the range checks and their error paths fold away,
+	 * leaving just the state init or the error result.
+	 */
+	if (state->fit_for_inline) {
+		s32 start = state->start, end = state->end;
+
+		if (start > end) {
+			/* s->cur = s->end = 0; return -EINVAL; */
+			insn_buf[i++] = BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 0);
+			insn_buf[i++] = BPF_MOV64_IMM(BPF_REG_0, -EINVAL);
+		} else if ((s64)end - (s64)start > BPF_MAX_LOOPS) {
+			/* s->cur = s->end = 0; return -E2BIG; */
+			insn_buf[i++] = BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 0);
+			insn_buf[i++] = BPF_MOV64_IMM(BPF_REG_0, -E2BIG);
+		} else {
+			/* s->cur = start - 1; s->end = end; return 0; */
+			insn_buf[i++] = BPF_ST_MEM(BPF_W, BPF_REG_1, 0, start - 1);
+			insn_buf[i++] = BPF_ST_MEM(BPF_W, BPF_REG_1, 4, end);
+			insn_buf[i++] = BPF_MOV64_IMM(BPF_REG_0, 0);
+		}
+		return i;
+	}
+
+	/* if (start > end) goto einval; */
+	insn_buf[i++] = BPF_JMP32_REG(BPF_JSGT, BPF_REG_2, BPF_REG_3, 8);
+	/*
+	 * start <= end here, so the range end - start fits in a u32; compute it as a 32-bit
+	 * subtraction that zero-extends into r0.
+	 */
+	insn_buf[i++] = BPF_MOV32_REG(BPF_REG_0, BPF_REG_3);
+	insn_buf[i++] = BPF_ALU32_REG(BPF_SUB, BPF_REG_0, BPF_REG_2);
+	/* if (r0 > BPF_MAX_LOOPS) goto e2big; */
+	insn_buf[i++] = BPF_JMP_IMM(BPF_JSGT, BPF_REG_0, BPF_MAX_LOOPS, 8);
+	/* s->cur = start - 1; */
+	insn_buf[i++] = BPF_ALU32_IMM(BPF_ADD, BPF_REG_2, -1);
+	insn_buf[i++] = BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_2, 0);
+	/* s->end = end; */
+	insn_buf[i++] = BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_3, 4);
+	/* return 0; */
+	insn_buf[i++] = BPF_MOV64_IMM(BPF_REG_0, 0);
+	insn_buf[i++] = BPF_JMP_A(5);
+	/* einval: s->cur = s->end = 0; return -EINVAL; */
+	insn_buf[i++] = BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 0);
+	insn_buf[i++] = BPF_MOV64_IMM(BPF_REG_0, -EINVAL);
+	insn_buf[i++] = BPF_JMP_A(2);
+	/* e2big: s->cur = s->end = 0; return -E2BIG; */
+	insn_buf[i++] = BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 0);
+	insn_buf[i++] = BPF_MOV64_IMM(BPF_REG_0, -E2BIG);
+
+	return i;
+}
+
+/*
+ * Inline bpf_iter_num_next(). R1 holds the pointer to the iterator. Keep in sync with the
+ * kfunc in kernel/bpf/bpf_iter.c.
+ */
+static int inline_bpf_iter_num_next(struct bpf_insn *insn_buf)
+{
+	int i = 0;
+
+	/*
+	 * s->cur and s->end are int, so the (s64)(s->cur + 1) >= s->end check is equivalent to a
+	 * signed 32-bit comparison of (s->cur + 1) against s->end and needs no sign extension.
+	 */
+	insn_buf[i++] = BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, 0);
+	insn_buf[i++] = BPF_ALU32_IMM(BPF_ADD, BPF_REG_0, 1);
+	insn_buf[i++] = BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, 4);
+	/* if ((s32)(s->cur + 1) >= (s32)s->end) goto done; */
+	insn_buf[i++] = BPF_JMP32_REG(BPF_JSGE, BPF_REG_0, BPF_REG_2, 3);
+	/* s->cur++; return &s->cur; */
+	insn_buf[i++] = BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0, 0);
+	insn_buf[i++] = BPF_MOV64_REG(BPF_REG_0, BPF_REG_1);
+	insn_buf[i++] = BPF_JMP_A(2);
+	/* done: s->cur = s->end = 0; return NULL; */
+	insn_buf[i++] = BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 0);
+	insn_buf[i++] = BPF_MOV64_IMM(BPF_REG_0, 0);
+
+	return i;
+}
+
+/*
+ * Inline bpf_iter_num_destroy(). R1 holds the pointer to the iterator. Keep in sync with
+ * the kfunc in kernel/bpf/bpf_iter.c.
+ */
+static int inline_bpf_iter_num_destroy(struct bpf_insn *insn_buf)
+{
+	int i = 0;
+
+	/* s->cur = s->end = 0; */
+	insn_buf[i++] = BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 0);
+
+	return i;
+}
+
 int bpf_fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 		     struct bpf_insn *insn_buf, int insn_idx, int *cnt)
 {
@@ -19899,6 +20049,13 @@ int bpf_fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 		insn_buf[4] = BPF_ALU64_REG(BPF_SUB, BPF_REG_0, BPF_REG_1);
 		insn_buf[5] = BPF_ALU64_IMM(BPF_NEG, BPF_REG_0, 0);
 		*cnt = 6;
+	} else if (desc->func_id == special_kfunc_list[KF_bpf_iter_num_new]) {
+		*cnt = inline_bpf_iter_num_new(insn_buf,
+					       &env->insn_aux_data[insn_idx].iter_num_new_state);
+	} else if (desc->func_id == special_kfunc_list[KF_bpf_iter_num_next]) {
+		*cnt = inline_bpf_iter_num_next(insn_buf);
+	} else if (desc->func_id == special_kfunc_list[KF_bpf_iter_num_destroy]) {
+		*cnt = inline_bpf_iter_num_destroy(insn_buf);
 	}
 
 	if (env->insn_aux_data[insn_idx].arg_prog) {
diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
index 55d394438705a6..3174c2f18e7150 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -965,6 +965,7 @@ $(OUTPUT)/bench_ringbufs.o: $(OUTPUT)/ringbuf_bench.skel.h \
 			    $(OUTPUT)/perfbuf_bench.skel.h
 $(OUTPUT)/bench_bloom_filter_map.o: $(OUTPUT)/bloom_filter_bench.skel.h
 $(OUTPUT)/bench_bpf_loop.o: $(OUTPUT)/bpf_loop_bench.skel.h
+$(OUTPUT)/bench_bpf_for.o: $(OUTPUT)/bpf_for_bench.skel.h
 $(OUTPUT)/bench_strncmp.o: $(OUTPUT)/strncmp_bench.skel.h
 $(OUTPUT)/bench_bpf_hashmap_full_update.o: $(OUTPUT)/bpf_hashmap_full_update_bench.skel.h
 $(OUTPUT)/bench_local_storage.o: $(OUTPUT)/local_storage_bench.skel.h
@@ -990,6 +991,7 @@ $(OUTPUT)/bench: $(OUTPUT)/bench.o \
 		 $(OUTPUT)/bench_ringbufs.o \
 		 $(OUTPUT)/bench_bloom_filter_map.o \
 		 $(OUTPUT)/bench_bpf_loop.o \
+		 $(OUTPUT)/bench_bpf_for.o \
 		 $(OUTPUT)/bench_strncmp.o \
 		 $(OUTPUT)/bench_bpf_hashmap_full_update.o \
 		 $(OUTPUT)/bench_local_storage.o \
diff --git a/tools/testing/selftests/bpf/bench.c b/tools/testing/selftests/bpf/bench.c
index 3d9d2cd7764bd3..b86b73456d3ca8 100644
--- a/tools/testing/selftests/bpf/bench.c
+++ b/tools/testing/selftests/bpf/bench.c
@@ -276,6 +276,7 @@ static const struct argp_option opts[] = {
 extern struct argp bench_ringbufs_argp;
 extern struct argp bench_bloom_map_argp;
 extern struct argp bench_bpf_loop_argp;
+extern struct argp bench_bpf_for_argp;
 extern struct argp bench_local_storage_argp;
 extern struct argp bench_local_storage_rcu_tasks_trace_argp;
 extern struct argp bench_strncmp_argp;
@@ -292,6 +293,7 @@ static const struct argp_child bench_parsers[] = {
 	{ &bench_ringbufs_argp, 0, "Ring buffers benchmark", 0 },
 	{ &bench_bloom_map_argp, 0, "Bloom filter map benchmark", 0 },
 	{ &bench_bpf_loop_argp, 0, "bpf_loop helper benchmark", 0 },
+	{ &bench_bpf_for_argp, 0, "bpf_for loop benchmark", 0 },
 	{ &bench_local_storage_argp, 0, "local_storage benchmark", 0 },
 	{ &bench_strncmp_argp, 0, "bpf_strncmp helper benchmark", 0 },
 	{ &bench_local_storage_rcu_tasks_trace_argp, 0,
@@ -557,6 +559,7 @@ extern const struct bench bench_bloom_false_positive;
 extern const struct bench bench_hashmap_without_bloom;
 extern const struct bench bench_hashmap_with_bloom;
 extern const struct bench bench_bpf_loop;
+extern const struct bench bench_bpf_for;
 extern const struct bench bench_strncmp_no_helper;
 extern const struct bench bench_strncmp_helper;
 extern const struct bench bench_bpf_hashmap_full_update;
@@ -640,6 +643,7 @@ static const struct bench *benchs[] = {
 	&bench_hashmap_without_bloom,
 	&bench_hashmap_with_bloom,
 	&bench_bpf_loop,
+	&bench_bpf_for,
 	&bench_strncmp_no_helper,
 	&bench_strncmp_helper,
 	&bench_bpf_hashmap_full_update,
diff --git a/tools/testing/selftests/bpf/benchs/bench_bpf_for.c b/tools/testing/selftests/bpf/benchs/bench_bpf_for.c
new file mode 100644
index 00000000000000..730c51ad2dec34
--- /dev/null
+++ b/tools/testing/selftests/bpf/benchs/bench_bpf_for.c
@@ -0,0 +1,104 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <argp.h>
+#include "bench.h"
+#include "bpf_for_bench.skel.h"
+
+/* BPF triggering benchmarks */
+static struct ctx {
+	struct bpf_for_bench *skel;
+} ctx;
+
+static struct {
+	__u32 nr_loops;
+} args = {
+	/*
+	 * Default to a large loop count so the per-iteration bpf_iter_num_next() cost dominates
+	 * the one-time bpf_iter_num_new()/destroy() setup and teardown.
+	 */
+	.nr_loops = 1000,
+};
+
+enum {
+	ARG_NR_LOOPS = 4000,
+};
+
+static const struct argp_option opts[] = {
+	{ "nr_loops", ARG_NR_LOOPS, "nr_loops", 0,
+		"Set number of iterations for the bpf_for() loop"},
+	{},
+};
+
+static error_t parse_arg(int key, char *arg, struct argp_state *state)
+{
+	switch (key) {
+	case ARG_NR_LOOPS:
+		args.nr_loops = strtol(arg, NULL, 10);
+		break;
+	default:
+		return ARGP_ERR_UNKNOWN;
+	}
+
+	return 0;
+}
+
+/* exported into benchmark runner */
+const struct argp bench_bpf_for_argp = {
+	.options = opts,
+	.parser = parse_arg,
+};
+
+static void validate(void)
+{
+	if (env.consumer_cnt != 0) {
+		fprintf(stderr, "benchmark doesn't support consumer!\n");
+		exit(1);
+	}
+}
+
+static void *producer(void *input)
+{
+	while (true)
+		/* trigger the bpf program */
+		syscall(__NR_getpgid);
+
+	return NULL;
+}
+
+static void measure(struct bench_res *res)
+{
+	res->hits = atomic_swap(&ctx.skel->bss->hits, 0);
+}
+
+static void setup(void)
+{
+	struct bpf_link *link;
+
+	setup_libbpf();
+
+	ctx.skel = bpf_for_bench__open_and_load();
+	if (!ctx.skel) {
+		fprintf(stderr, "failed to open skeleton\n");
+		exit(1);
+	}
+
+	link = bpf_program__attach(ctx.skel->progs.benchmark);
+	if (!link) {
+		fprintf(stderr, "failed to attach program!\n");
+		exit(1);
+	}
+
+	ctx.skel->bss->nr_loops = args.nr_loops;
+}
+
+const struct bench bench_bpf_for = {
+	.name = "bpf-for",
+	.argp = &bench_bpf_for_argp,
+	.validate = validate,
+	.setup = setup,
+	.producer_thread = producer,
+	.measure = measure,
+	.report_progress = ops_report_progress,
+	.report_final = ops_report_final,
+};
diff --git a/tools/testing/selftests/bpf/benchs/run_bench_bpf_for.sh b/tools/testing/selftests/bpf/benchs/run_bench_bpf_for.sh
new file mode 100755
index 00000000000000..7da6453920dab3
--- /dev/null
+++ b/tools/testing/selftests/bpf/benchs/run_bench_bpf_for.sh
@@ -0,0 +1,15 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+source ./benchs/run_common.sh
+
+set -eufo pipefail
+
+for t in 1 4 8 12 16; do
+for i in 10 100 500 1000 5000 10000 50000 100000 500000 1000000; do
+subtitle "nr_loops: $i, nr_threads: $t"
+	summarize_ops "bpf_for: " \
+	    "$($RUN_BENCH -p $t --nr_loops $i bpf-for)"
+	printf "\n"
+done
+done
diff --git a/tools/testing/selftests/bpf/progs/bpf_for_bench.c b/tools/testing/selftests/bpf/progs/bpf_for_bench.c
new file mode 100644
index 00000000000000..2752de5c44f57a
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/bpf_for_bench.c
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+u32 nr_loops;
+long hits;
+
+static int outer_loop(__u32 index, void *data)
+{
+	int i;
+
+	/*
+	 * Empty body: the work being measured is the open-coded numeric iterator itself
+	 * (bpf_iter_num_new/next/destroy behind bpf_for()).
+	 */
+	bpf_for(i, 0, nr_loops)
+		;
+	__sync_add_and_fetch(&hits, nr_loops);
+	return 0;
+}
+
+SEC("fentry/" SYS_PREFIX "sys_getpgid")
+int benchmark(void *ctx)
+{
+	bpf_loop(1000, outer_loop, NULL, 0);
+	return 0;
+}
diff --git a/tools/testing/selftests/bpf/progs/iters.c b/tools/testing/selftests/bpf/progs/iters.c
index 0fa70b133d9322..c4df589ecd7ae4 100644
--- a/tools/testing/selftests/bpf/progs/iters.c
+++ b/tools/testing/selftests/bpf/progs/iters.c
@@ -88,6 +88,151 @@ int iter_err_unsafe_asm_loop(const void *ctx)
 	return 0;
 }
 
+/*
+ * Naked functions, so there is no compiler-generated glue and the whole inlined program can be
+ * matched. Pinned to arches whose JITs zero-extend 32-bit writes implicitly
+ * (bpf_jit_needs_zext() == false); on arches that need explicit zero-extension the verifier
+ * interleaves "wN = wN" insns and the fixed shape below would not match. The inlining itself is
+ * arch independent, so checking it on these arches is sufficient.
+ *
+ * With constant bounds bpf_iter_num_new() is inlined with the range checks folded away, just
+ * storing the initial state (s->cur = start - 1, s->end = end); bpf_iter_num_next() and
+ * bpf_iter_num_destroy() are inlined too, leaving a call-free loop.
+ */
+SEC("raw_tp")
+__arch_x86_64
+__arch_arm64
+__success
+__xlated("r6 = r10")
+__xlated("r6 += -8")
+__xlated("r1 = r6")
+__xlated("r2 = 0")
+__xlated("r3 = 10")
+/* bpf_iter_num_new(&it, 0, 10) folded to the state init */
+__xlated("*(u32 *)(r1 +0) = -1")
+__xlated("*(u32 *)(r1 +4) = 10")
+__xlated("r0 = 0")
+__xlated("r1 = r6")
+/* bpf_iter_num_next(&it) */
+__xlated("r0 = *(u32 *)(r1 +0)")
+__xlated("w0 += 1")
+__xlated("r2 = *(u32 *)(r1 +4)")
+__xlated("if w0 s>= w2 goto pc+3")
+__xlated("*(u32 *)(r1 +0) = r0")
+__xlated("r0 = r1")
+__xlated("goto pc+2")
+__xlated("*(u64 *)(r1 +0) = 0")
+__xlated("r0 = 0")
+__xlated("if r0 != 0x0 goto pc-11")
+__xlated("r1 = r6")
+/* bpf_iter_num_destroy(&it) */
+__xlated("*(u64 *)(r1 +0) = 0")
+__xlated("r0 = 0")
+__xlated("exit")
+int __naked iter_num_new_const_folded(void)
+{
+	asm volatile (
+		/* r6 points to struct bpf_iter_num on the stack */
+		"r6 = r10;"
+		"r6 += -8;"
+		"r1 = r6;"
+		"r2 = 0;"
+		"r3 = 10;"
+		"call %[bpf_iter_num_new];"
+	"1:"
+		"r1 = r6;"
+		"call %[bpf_iter_num_next];"
+		"if r0 != 0 goto 1b;"
+		"r1 = r6;"
+		"call %[bpf_iter_num_destroy];"
+		"r0 = 0;"
+		"exit;"
+		:
+		: __imm(bpf_iter_num_new),
+		  __imm(bpf_iter_num_next),
+		  __imm(bpf_iter_num_destroy)
+		: __clobber_common, "r6"
+	);
+}
+
+/*
+ * Same arch pinning and naked-function rationale as above. With a non-constant bound
+ * bpf_iter_num_new() keeps the full range check: the distance is computed and both the -EINVAL
+ * and -E2BIG error paths remain. bpf_iter_num_next() and bpf_iter_num_destroy() are inlined too.
+ */
+SEC("raw_tp")
+__arch_x86_64
+__arch_arm64
+__success
+__xlated("r6 = r10")
+__xlated("r6 += -8")
+__xlated("call unknown")
+__xlated("r3 = r0")
+__xlated("r3 &= 65535")
+__xlated("r1 = r6")
+__xlated("r2 = 0")
+/* bpf_iter_num_new(&it, 0, <non-const>) with the range check kept */
+__xlated("if w2 s> w3 goto pc+8")
+__xlated("w0 = w3")
+__xlated("w0 -= w2")
+__xlated("if r0 s> 0x800000 goto pc+8")
+__xlated("w2 += -1")
+__xlated("*(u32 *)(r1 +0) = r2")
+__xlated("*(u32 *)(r1 +4) = r3")
+__xlated("r0 = 0")
+__xlated("goto pc+5")
+__xlated("*(u64 *)(r1 +0) = 0")
+__xlated("r0 = -22")
+__xlated("goto pc+2")
+__xlated("*(u64 *)(r1 +0) = 0")
+__xlated("r0 = -7")
+__xlated("r1 = r6")
+/* bpf_iter_num_next(&it) */
+__xlated("r0 = *(u32 *)(r1 +0)")
+__xlated("w0 += 1")
+__xlated("r2 = *(u32 *)(r1 +4)")
+__xlated("if w0 s>= w2 goto pc+3")
+__xlated("*(u32 *)(r1 +0) = r0")
+__xlated("r0 = r1")
+__xlated("goto pc+2")
+__xlated("*(u64 *)(r1 +0) = 0")
+__xlated("r0 = 0")
+__xlated("if r0 != 0x0 goto pc-11")
+__xlated("r1 = r6")
+/* bpf_iter_num_destroy(&it) */
+__xlated("*(u64 *)(r1 +0) = 0")
+__xlated("r0 = 0")
+__xlated("exit")
+int __naked iter_num_new_inlined(void)
+{
+	asm volatile (
+		/* r6 points to struct bpf_iter_num on the stack */
+		"r6 = r10;"
+		"r6 += -8;"
+		/* non-constant end so the range checks are kept */
+		"call %[bpf_get_prandom_u32];"
+		"r3 = r0;"
+		"r3 &= 0xffff;"
+		"r1 = r6;"
+		"r2 = 0;"
+		"call %[bpf_iter_num_new];"
+	"1:"
+		"r1 = r6;"
+		"call %[bpf_iter_num_next];"
+		"if r0 != 0 goto 1b;"
+		"r1 = r6;"
+		"call %[bpf_iter_num_destroy];"
+		"r0 = 0;"
+		"exit;"
+		:
+		: __imm(bpf_get_prandom_u32),
+		  __imm(bpf_iter_num_new),
+		  __imm(bpf_iter_num_next),
+		  __imm(bpf_iter_num_destroy)
+		: __clobber_common, "r6"
+	);
+}
+
 SEC("raw_tp")
 __success
 int iter_while_loop(const void *ctx)

3/2 2026/07/23 14:15 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 10562
							output: 33
							thoughts: 681
						

Thoughts:
**BPF Verifier Optimization: Iteration Inlining**

Okay, so we have a patch that's doing some clever inlining within the BPF verifier. It's targeting `bpf_iter_num_new`, `bpf_iter_num_next`, and `bpf_iter_num_destroy` kfuncs, which is a good optimization target. I can see the changes; they've added a `struct bpf_iter_num_new_state` to the `bpf_insn_aux_data` to track the arguments' constant-ness and therefore their suitability for inlining. The main logic is in `kernel/bpf/verifier.c`, where they're updating this state and performing the actual inlining in `bpf_fixup_kfunc_call`. They've also added a benchmark (`bench_bpf_for.c`) and some tests in `iters.c`, which is the right approach.

This is a functional change, a pretty significant optimization for anyone using `bpf_for` or calling `bpf_iter_num_new` directly from userspace via loaded BPF programs. I need to understand this impact from the security perspective.

Let's see, the core functions modified/added are: `update_iter_num_new_state`, `inline_bpf_iter_num_new`, `inline_bpf_iter_num_next`, and `inline_bpf_iter_num_destroy`. All of these, interestingly, are static within `kernel/bpf/verifier.c`. The entry points are `check_kfunc_call` and `bpf_fixup_kfunc_call`.

My primary fuzzing focus should now definitely be the BPF verifier, specifically around kfunc calls and inlining. I should zero in on the following functions, though I will be limited since they are static:

*   `update_iter_num_new_state` (static, but still a code path to consider)
*   `inline_bpf_iter_num_new`
*   `inline_bpf_iter_num_next`
*   `inline_bpf_iter_num_destroy`
*   `bpf_fixup_kfunc_call` (an existing function they are using, which is good to add it to the list)
*   `check_kfunc_call` (an existing function that is key to target).

`update_iter_num_new_state` and `inline_bpf_iter_num_new` are static, I already see. Good.

Configs required are `CONFIG_BPF_SYSCALL` and `CONFIG_BPF_JIT`. Easy to verify. `kernel/bpf/verifier.c` is definitely compiled when `CONFIG_BPF_SYSCALL` is enabled, as expected.

I should also check if there are any specific configs relating to BPF iterators; let me grep for `bpf_iter_num_new` to check if it depends on anything. The function is defined in `kernel/bpf/bpf_iter.c`, so I should check the Makefile or Kconfig for that directory. Good. I need to make sure I get the configs right.

Now, let's call `set-results` with these values. I can see how this all connects.



4/2 2026/07/23 14:15 tool grepper
0m Args:
{
  "Expression": "bpf_iter_num_new",
  "PathPrefix": "kernel/bpf/"
}

Results:
{
  "Output": "kernel/bpf/bpf_iter.c=768=__bpf_kfunc_start_defs();\nkernel/bpf/bpf_iter.c-769-\nkernel/bpf/bpf_iter.c:770:__bpf_kfunc int bpf_iter_num_new(struct bpf_iter_num *it, int start, int end)\nkernel/bpf/bpf_iter.c-771-{\n--\nkernel/bpf/helpers.c=4886=BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL)\nkernel/bpf/helpers.c:4887:BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW)\nkernel/bpf/helpers.c-4888-BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)\n--\nkernel/bpf/states.c=1113=static bool states_maybe_looping(struct bpf_verifier_state *old,\n--\nkernel/bpf/states.c-1181- *\nkernel/bpf/states.c:1182: *   bpf_iter_num_new(\u0026it, 0, 10);\nkernel/bpf/states.c-1183- *   while ((p = bpf_iter_num_next(\u0026t))) {\n--\nkernel/bpf/verifier.c=7597=static struct bpf_reg_state *get_iter_from_state(struct bpf_verifier_state *cur_st,\n--\nkernel/bpf/verifier.c-7671- *     int i = 0, a = 0;\nkernel/bpf/verifier.c:7672: *     bpf_iter_num_new(\u0026it, 0, 10);\nkernel/bpf/verifier.c-7673- *     while (bpf_iter_num_next(\u0026it)) {\n--\nkernel/bpf/verifier.c=11068=enum special_kfunc_type {\n--\nkernel/bpf/verifier.c-11118-\tKF_bpf_local_irq_restore,\nkernel/bpf/verifier.c:11119:\tKF_bpf_iter_num_new,\nkernel/bpf/verifier.c-11120-\tKF_bpf_iter_num_next,\n--\nkernel/bpf/verifier.c=11206=BTF_ID(func, bpf_local_irq_restore)\nkernel/bpf/verifier.c:11207:BTF_ID(func, bpf_iter_num_new)\nkernel/bpf/verifier.c-11208-BTF_ID(func, bpf_iter_num_next)\n--\nkernel/bpf/verifier.c=11690=static bool is_bpf_iter_num_api_kfunc(u32 btf_id)\nkernel/bpf/verifier.c-11691-{\nkernel/bpf/verifier.c:11692:\treturn btf_id == special_kfunc_list[KF_bpf_iter_num_new] ||\nkernel/bpf/verifier.c-11693-\t       btf_id == special_kfunc_list[KF_bpf_iter_num_next] ||\n--\nkernel/bpf/verifier.c=12963=static int check_return_code(struct bpf_verifier_env *env, int regno, const char *reg_name);\n--\nkernel/bpf/verifier.c-12965-/*\nkernel/bpf/verifier.c:12966: * bpf_iter_num_new() can be inlined more tightly when its start and end arguments are constant,\nkernel/bpf/verifier.c-12967- * as the range checks and their error paths can then be resolved at rewrite time. Record, per\n--\nkernel/bpf/verifier.c=12970=static int update_iter_num_new_state(struct bpf_verifier_env *env)\nkernel/bpf/verifier.c-12971-{\nkernel/bpf/verifier.c:12972:\tstruct bpf_iter_num_new_state *state = \u0026cur_aux(env)-\u003eiter_num_new_state;\nkernel/bpf/verifier.c-12973-\tstruct bpf_reg_state *start_reg = \u0026cur_regs(env)[BPF_REG_2];\n--\nkernel/bpf/verifier.c=13006=static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-13206-\nkernel/bpf/verifier.c:13207:\tif (meta.func_id == special_kfunc_list[KF_bpf_iter_num_new]) {\nkernel/bpf/verifier.c-13208-\t\terr = update_iter_num_new_state(env);\n--\nkernel/bpf/verifier.c=19803=static void __fixup_collection_insert_kfunc(struct bpf_insn_aux_data *insn_aux,\n--\nkernel/bpf/verifier.c-19820-/*\nkernel/bpf/verifier.c:19821: * Inline bpf_iter_num_new(). R1 holds the pointer to the iterator, R2 and R3 hold the (int)\nkernel/bpf/verifier.c-19822- * start and end arguments. Keep in sync with the kfunc in kernel/bpf/bpf_iter.c.\nkernel/bpf/verifier.c-19823- */\nkernel/bpf/verifier.c:19824:static int inline_bpf_iter_num_new(struct bpf_insn *insn_buf, struct bpf_iter_num_new_state *state)\nkernel/bpf/verifier.c-19825-{\n--\nkernel/bpf/verifier.c=19923=int bpf_fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-20051-\t\t*cnt = 6;\nkernel/bpf/verifier.c:20052:\t} else if (desc-\u003efunc_id == special_kfunc_list[KF_bpf_iter_num_new]) {\nkernel/bpf/verifier.c:20053:\t\t*cnt = inline_bpf_iter_num_new(insn_buf,\nkernel/bpf/verifier.c-20054-\t\t\t\t\t       \u0026env-\u003einsn_aux_data[insn_idx].iter_num_new_state);\n"
}

5/2 2026/07/23 14:15 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 13745
							output: 183
							thoughts: 245
						

Thoughts:
**My Assessment of the BPF Verifier Optimization Patch**

Okay, so this patch introduces an optimization to the BPF verifier. It's inlining `bpf_iter_num_new`, `bpf_iter_num_next`, and `bpf_iter_num_destroy`. That seems pretty solid from a functionality standpoint. My immediate thought is, "Yep, this absolutely needs fuzzing." We need to make sure the inlining doesn't introduce any subtle issues, especially around edge cases.

Specifically, I need to focus my attention on the `bpf_fixup_kfunc_call` and `check_kfunc_call` functions. Those are going to be critical for understanding how this is implemented. Since the inlining functions, like `update_iter_num_new_state`, `inline_bpf_iter_num_new`, `inline_bpf_iter_num_next`, and `inline_bpf_iter_num_destroy`, are static, I should probably keep `bpf_fixup_kfunc_call` and `check_kfunc_call` on the list as well.

Now, regarding configuration, it looks like `BPF_SYSCALL` is the key requirement here. I’ll make sure that is enabled. Let me quickly double-check if there are any other dependencies. I know `BPF_JIT` is pretty standard for BPF, so I'll keep that in mind, but `BPF_SYSCALL` seems to be the core requirement.

Alright, I'm ready to proceed. I'll call `set-results`.



6/2 2026/07/23 14:15 tool set-results
0m Args:
{
  "EnableConfigs": [
    "BPF_SYSCALL"
  ],
  "FocusSymbols": [
    "bpf_fixup_kfunc_call",
    "check_kfunc_call",
    "update_iter_num_new_state",
    "inline_bpf_iter_num_new",
    "inline_bpf_iter_num_next",
    "inline_bpf_iter_num_destroy"
  ],
  "Reasoning": "The patch modifies the BPF verifier to inline bpf_iter_num_new, bpf_iter_num_next, and bpf_iter_num_destroy kfuncs. This is a functional change to the BPF verifier that optimizes the execution of these kfuncs and can be reached by loading BPF programs from userspace.",
  "WorthFuzzing": true
}

Results:
{
  "EnableConfigs": [
    "BPF_SYSCALL"
  ],
  "FocusSymbols": [
    "bpf_fixup_kfunc_call",
    "check_kfunc_call",
    "update_iter_num_new_state",
    "inline_bpf_iter_num_new",
    "inline_bpf_iter_num_next",
    "inline_bpf_iter_num_destroy"
  ],
  "Reasoning": "The patch modifies the BPF verifier to inline bpf_iter_num_new, bpf_iter_num_next, and bpf_iter_num_destroy kfuncs. This is a functional change to the BPF verifier that optimizes the execution of these kfuncs and can be reached by loading BPF programs from userspace.",
  "WorthFuzzing": true
}

LLM Calls Summary:
Total Calls Total Tokens Avg Tokens Total Duration (Seconds) Avg Duration (Seconds)
Tool Calls Summary:
Total Calls Total Duration (Seconds) Avg Duration (Seconds)