AI Trajectory


Trajectory: (Expand/Collapse All)
Seq Timestamp Type Name Duration
0/0 2026/10/14 19:06 flow patch-triage
2m Args:
null

Results:
{
  "EnableConfigs": [
    "BPF_SYSCALL",
    "BPF_JIT"
  ],
  "FocusSymbols": [
    "bpf_int_jit_compile",
    "do_jit",
    "emit_atomic_fetch_rmw",
    "emit_arena_exentry",
    "bpf_jit_supports_insn",
    "ex_handler_bpf"
  ],
  "KMSANReasoning": "The patch series adds x86 BPF JIT support for atomic fetch read-modify-write operations (BPF_AND | BPF_FETCH, BPF_OR | BPF_FETCH, BPF_XOR | BPF_FETCH) on BPF arena memory. It lowers these operations to a CMPXCHG loop, registers exception table entries for both the load and CMPXCHG instructions to handle faults, adjusts num_exentries allocation calculations, and updates the relevant BPF selftests and sched_ext header helpers.\n\nKMSAN vs KASAN Applicability:\n- KMSAN detects uninitialized memory reads (heap, stack, page allocations) and kernel-to-user info-leaks. This patch introduces no new kernel-to-user data copies, does not alter serialization/padding of structs exposed to userspace, and does not add conditional logic on uninitialized kernel variables. The exception table entries initialized by emit_arena_exentry() have all their struct fields (insn, fixup, data) explicitly assigned.\n- Potential risks of these changes include JIT instruction encoding errors, register clobbering, incorrect jump offsets, or extable sizing/bounds errors. Array bounds issues in extable allocations are detected by KASAN, while execution/encoding issues are caught by the verifier, selftests, and standard hardware exception handling.\n- Because no uninitialized memory read or info-leak risks are introduced, a dedicated KMSAN fuzzing session is not warranted.",
  "NeedsKMSAN": false,
  "Reasoning": "The patch adds support in the x86 BPF JIT compiler for atomic fetch read-modify-write operations (BPF_AND/OR/XOR with BPF_FETCH) on BPF arena memory pointers. It implements a CMPXCHG loop for atomic fetch RMW, generates exception table entries for fault recovery during arena accesses, adjusts the BPF prog exception entry count, and lifts previous rejection in bpf_jit_supports_insn. This touches reachable core BPF JIT and exception handling logic on amd64 accessible via the bpf() system call.",
  "WorthFuzzing": true
}

1/1 2026/10/14 19:06 action read-patch-diff
0m Args:
null

Results:
{
  "PatchDiff": "commit b883a66f73e1dc8ca4b2e7a43e9c65ea1f8e1cdd\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate:   Wed Oct 14 19:06:06 2026 +0000\n\n    syz-cluster: applied patch under review\n\ndiff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c\nindex 083fcd6cf15b7..5fb1a31f71a26 100644\n--- a/arch/x86/net/bpf_jit_comp.c\n+++ b/arch/x86/net/bpf_jit_comp.c\n@@ -239,6 +239,13 @@ static const int reg2pt_regs[] = {\n \t[BPF_REG_AX] = offsetof(struct pt_regs, r10),\n };\n \n+static bool is_atomic_fetch_op(const struct bpf_insn *insn)\n+{\n+\treturn insn-\u003eimm == (BPF_AND | BPF_FETCH) ||\n+\t       insn-\u003eimm == (BPF_OR | BPF_FETCH) ||\n+\t       insn-\u003eimm == (BPF_XOR | BPF_FETCH);\n+}\n+\n /*\n  * is_ereg() == true if BPF register 'reg' maps to x86-64 r8..r15\n  * which need extra byte of encoding.\n@@ -1680,6 +1687,72 @@ static int emit_atomic_ld_st_index(u8 **pprog, u32 atomic_op, u32 size,\n \treturn 0;\n }\n \n+/*\n+ * A fetching AND/OR/XOR can't be implemented with a single x86 insn, so do a\n+ * CMPXCHG loop. @index_reg is X86_REG_R12 for an arena access or -1 otherwise,\n+ * and @dst_reg/@src_reg are already substituted for R0 by the caller.\n+ *\n+ * Both the load and the CMPXCHG can fault when accessing the arena, so their\n+ * addresses are handed back in @fault[0] and @fault[1]. A fault has to resume\n+ * at @resume, which is past the loop and past the move that delivers the old\n+ * value, but before the R0 restore. Both are NULL for the non-arena case,\n+ * which needs no fixup.\n+ */\n+static int emit_atomic_fetch_rmw(u8 **pprog, struct bpf_insn *insn, u32 dst_reg,\n+\t\t\t\t u32 src_reg, int index_reg, u8 **fault,\n+\t\t\t\t u8 **resume)\n+{\n+\tbool is64 = BPF_SIZE(insn-\u003ecode) == BPF_DW;\n+\tu8 *branch_target, *prog = *pprog;\n+\tint err;\n+\n+\tbranch_target = prog;\n+\n+\t/* Load old value */\n+\tif (fault)\n+\t\tfault[0] = prog;\n+\tif (index_reg \u003c 0)\n+\t\temit_ldx(\u0026prog, BPF_SIZE(insn-\u003ecode), BPF_REG_0, dst_reg, insn-\u003eoff);\n+\telse\n+\t\temit_ldx_index(\u0026prog, BPF_SIZE(insn-\u003ecode), BPF_REG_0, dst_reg,\n+\t\t\t       index_reg, insn-\u003eoff);\n+\n+\t/*\n+\t * Perform the (commutative) operation locally, put the result in\n+\t * the AUX_REG.\n+\t */\n+\temit_mov_reg(\u0026prog, is64, AUX_REG, BPF_REG_0);\n+\tmaybe_emit_mod(\u0026prog, AUX_REG, src_reg, is64);\n+\tEMIT2(simple_alu_opcodes[BPF_OP(insn-\u003eimm)],\n+\t      add_2reg(0xC0, AUX_REG, src_reg));\n+\n+\t/* Attempt to swap in new value */\n+\tif (fault)\n+\t\tfault[1] = prog;\n+\tif (index_reg \u003c 0)\n+\t\terr = emit_atomic_rmw(\u0026prog, BPF_CMPXCHG, dst_reg, AUX_REG,\n+\t\t\t\t      insn-\u003eoff, BPF_SIZE(insn-\u003ecode));\n+\telse\n+\t\terr = emit_atomic_rmw_index(\u0026prog, BPF_CMPXCHG, BPF_SIZE(insn-\u003ecode),\n+\t\t\t\t\t    dst_reg, AUX_REG, index_reg, insn-\u003eoff);\n+\tif (WARN_ON(err))\n+\t\treturn err;\n+\n+\t/* ZF tells us whether we won the race. If it's cleared we need to try again. */\n+\tEMIT2(X86_JNE, -(prog - branch_target) - 2);\n+\t/* Return the pre-modification value */\n+\temit_mov_reg(\u0026prog, is64, src_reg, BPF_REG_0);\n+\n+\tif (resume)\n+\t\t*resume = prog;\n+\n+\t/* Restore R0 after clobbering RAX */\n+\temit_mov_reg(\u0026prog, true, BPF_REG_0, BPF_REG_AX);\n+\n+\t*pprog = prog;\n+\treturn 0;\n+}\n+\n /*\n  * Metadata encoding for exception handling in JITed code.\n  *\n@@ -1693,7 +1766,12 @@ static int emit_atomic_ld_st_index(u8 **pprog, u32 atomic_op, u32 size,\n  * | ARENA_ACC | ARENA_WRITE | Unused | ARENA_REG | DST_REG | INSN_LEN |\n  * +-----------+-------------+--------+-----------+---------+----------+\n  *\n- * - INSN_LEN (8 bits): Length of faulting insn (max x86 insn = 15 bytes (fits in 8 bits)).\n+ * - INSN_LEN (8 bits): How far past the faulting insn to resume. That is its own length\n+ *                      for a single-insn access, but the distance to the end of the whole\n+ *                      sequence where one BPF insn became several, as for the CMPXCHG loop\n+ *                      of a fetching AND/OR/XOR. A sequence long enough to overflow this\n+ *                      field would first have to exceed BPF_MAX_INSN_SIZE, which do_jit()\n+ *                      rejects with -EFAULT before the image is used.\n  * - DST_REG  (8 bits): Offset of dst_reg from reg2pt_regs[] (max offset = 112 (fits in 8 bits)).\n  *                      This is set to DONT_CLEAR if the insn does not read into a register.\n  * - ARENA_REG (8 bits): Offset of the register that is used to calculate the\n@@ -1748,6 +1826,44 @@ bool ex_handler_bpf(const struct exception_table_entry *x, struct pt_regs *regs)\n \treturn true;\n }\n \n+/*\n+ * Record an arena access that may fault. @fault_ip is the address of the\n+ * faulting insn in the RO image, @resume_off how far past it execution has to\n+ * resume: for a multi-insn lowering that is the end of the whole sequence, not\n+ * the end of the one insn.\n+ */\n+static int emit_arena_exentry(struct bpf_prog *bpf_prog, u8 *image, u8 *rw_image,\n+\t\t\t      int *excnt, u8 *fault_ip, u32 resume_off,\n+\t\t\t      u32 fixup_reg, u32 arena_reg, bool is_write, s16 off)\n+{\n+\tstruct exception_table_entry *ex;\n+\ts64 delta;\n+\n+\tif (!bpf_prog-\u003eaux-\u003eextable)\n+\t\treturn 0;\n+\n+\tif (*excnt \u003e= bpf_prog-\u003eaux-\u003enum_exentries) {\n+\t\tpr_err(\"arena extable bug\\n\");\n+\t\treturn -EFAULT;\n+\t}\n+\tex = \u0026bpf_prog-\u003eaux-\u003eextable[(*excnt)++];\n+\n+\tdelta = fault_ip - (u8 *)\u0026ex-\u003einsn;\n+\t/* switch ex to rw buffer for writes */\n+\tex = (void *)rw_image + ((void *)ex - (void *)image);\n+\n+\tex-\u003einsn = delta;\n+\tex-\u003edata = EX_TYPE_BPF | FIELD_PREP(DATA_ARENA_OFFSET_MASK, off);\n+\tex-\u003efixup = FIELD_PREP(FIXUP_INSN_LEN_MASK, resume_off) |\n+\t\t    FIELD_PREP(FIXUP_ARENA_REG_MASK, arena_reg) |\n+\t\t    FIELD_PREP(FIXUP_REG_MASK, fixup_reg) |\n+\t\t    FIXUP_ARENA_ACCESS;\n+\tif (is_write)\n+\t\tex-\u003efixup |= FIXUP_ARENA_WRITE;\n+\n+\treturn 0;\n+}\n+\n static void detect_reg_usage(struct bpf_insn *insn, int insn_cnt,\n \t\t\t     bool *regs_used)\n {\n@@ -2625,28 +2741,8 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *\n \t\t\t}\n populate_extable:\n \t\t\t{\n-\t\t\t\tstruct exception_table_entry *ex;\n-\t\t\t\tu8 *_insn = image + proglen + (start_of_ldx - temp);\n \t\t\t\tu32 arena_reg, fixup_reg;\n \t\t\t\tbool is_write;\n-\t\t\t\ts64 delta;\n-\n-\t\t\t\tif (!bpf_prog-\u003eaux-\u003eextable)\n-\t\t\t\t\tbreak;\n-\n-\t\t\t\tif (excnt \u003e= bpf_prog-\u003eaux-\u003enum_exentries) {\n-\t\t\t\t\tpr_err(\"mem32 extable bug\\n\");\n-\t\t\t\t\treturn -EFAULT;\n-\t\t\t\t}\n-\t\t\t\tex = \u0026bpf_prog-\u003eaux-\u003eextable[excnt++];\n-\n-\t\t\t\tdelta = _insn - (u8 *)\u0026ex-\u003einsn;\n-\t\t\t\t/* switch ex to rw buffer for writes */\n-\t\t\t\tex = (void *)rw_image + ((void *)ex - (void *)image);\n-\n-\t\t\t\tex-\u003einsn = delta;\n-\n-\t\t\t\tex-\u003edata = EX_TYPE_BPF;\n \n \t\t\t\t/*\n \t\t\t\t * src_reg/dst_reg holds the address in the arena region with upper\n@@ -2682,14 +2778,12 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *\n \t\t\t\t\tis_write = true;\n \t\t\t\t}\n \n-\t\t\t\tex-\u003efixup = FIELD_PREP(FIXUP_INSN_LEN_MASK, prog - start_of_ldx) |\n-\t\t\t\t\t    FIELD_PREP(FIXUP_ARENA_REG_MASK, arena_reg) |\n-\t\t\t\t\t    FIELD_PREP(FIXUP_REG_MASK, fixup_reg);\n-\t\t\t\tex-\u003efixup |= FIXUP_ARENA_ACCESS;\n-\t\t\t\tif (is_write)\n-\t\t\t\t\tex-\u003efixup |= FIXUP_ARENA_WRITE;\n-\n-\t\t\t\tex-\u003edata |= FIELD_PREP(DATA_ARENA_OFFSET_MASK, insn-\u003eoff);\n+\t\t\t\terr = emit_arena_exentry(bpf_prog, image, rw_image, \u0026excnt,\n+\t\t\t\t\t\t\t image + proglen + (start_of_ldx - temp),\n+\t\t\t\t\t\t\t prog - start_of_ldx, fixup_reg,\n+\t\t\t\t\t\t\t arena_reg, is_write, insn-\u003eoff);\n+\t\t\t\tif (err)\n+\t\t\t\t\treturn err;\n \t\t\t}\n \t\t\tbreak;\n \n@@ -2841,20 +2935,12 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *\n \t\t\tfallthrough;\n \t\tcase BPF_STX | BPF_ATOMIC | BPF_W:\n \t\tcase BPF_STX | BPF_ATOMIC | BPF_DW: {\n-\t\t\tbool is64 = BPF_SIZE(insn-\u003ecode) == BPF_DW;\n \t\t\tu32 real_src_reg = src_reg;\n \t\t\tu32 real_dst_reg = dst_reg;\n+\t\t\tbool is_atomic_fetch = is_atomic_fetch_op(insn);\n \t\t\tu8 *old_prog;\n-\t\t\tbool is_atomic_fetch =\n-\t\t\t\t(insn-\u003eimm == (BPF_AND | BPF_FETCH) ||\n-\t\t\t\t insn-\u003eimm == (BPF_OR | BPF_FETCH) ||\n-\t\t\t\t insn-\u003eimm == (BPF_XOR | BPF_FETCH));\n-\t\t\tif (is_atomic_fetch) {\n-\t\t\t\t/*\n-\t\t\t\t * Can't be implemented with a single x86 insn.\n-\t\t\t\t * Need to do a CMPXCHG loop.\n-\t\t\t\t */\n \n+\t\t\tif (is_atomic_fetch) {\n \t\t\t\t/* Will need RAX as a CMPXCHG operand so save R0 */\n \t\t\t\told_prog = prog;\n \t\t\t\temit_mov_reg(\u0026prog, true, BPF_REG_AX, BPF_REG_0);\n@@ -2875,34 +2961,11 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *\n \t\t\t\t}\n \t\t\t}\n \t\t\tif (is_atomic_fetch) {\n-\t\t\t\tu8 *branch_target = prog;\n-\t\t\t\t/* Load old value */\n-\t\t\t\temit_ldx(\u0026prog, BPF_SIZE(insn-\u003ecode),\n-\t\t\t\t\t BPF_REG_0, real_dst_reg, insn-\u003eoff);\n-\t\t\t\t/*\n-\t\t\t\t * Perform the (commutative) operation locally,\n-\t\t\t\t * put the result in the AUX_REG.\n-\t\t\t\t */\n-\t\t\t\temit_mov_reg(\u0026prog, is64, AUX_REG, BPF_REG_0);\n-\t\t\t\tmaybe_emit_mod(\u0026prog, AUX_REG, real_src_reg, is64);\n-\t\t\t\tEMIT2(simple_alu_opcodes[BPF_OP(insn-\u003eimm)],\n-\t\t\t\t      add_2reg(0xC0, AUX_REG, real_src_reg));\n-\t\t\t\t/* Attempt to swap in new value */\n-\t\t\t\terr = emit_atomic_rmw(\u0026prog, BPF_CMPXCHG,\n-\t\t\t\t\t\t      real_dst_reg, AUX_REG,\n-\t\t\t\t\t\t      insn-\u003eoff,\n-\t\t\t\t\t\t      BPF_SIZE(insn-\u003ecode));\n-\t\t\t\tif (WARN_ON(err))\n+\t\t\t\terr = emit_atomic_fetch_rmw(\u0026prog, insn, real_dst_reg,\n+\t\t\t\t\t\t\t    real_src_reg, -1, NULL,\n+\t\t\t\t\t\t\t    NULL);\n+\t\t\t\tif (err)\n \t\t\t\t\treturn err;\n-\t\t\t\t/*\n-\t\t\t\t * ZF tells us whether we won the race. If it's\n-\t\t\t\t * cleared we need to try again.\n-\t\t\t\t */\n-\t\t\t\tEMIT2(X86_JNE, -(prog - branch_target) - 2);\n-\t\t\t\t/* Return the pre-modification value */\n-\t\t\t\temit_mov_reg(\u0026prog, is64, real_src_reg, BPF_REG_0);\n-\t\t\t\t/* Restore R0 after clobbering RAX */\n-\t\t\t\temit_mov_reg(\u0026prog, true, BPF_REG_0, BPF_REG_AX);\n \t\t\t\tbreak;\n \t\t\t}\n \n@@ -2926,6 +2989,51 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *\n \t\t\tfallthrough;\n \t\tcase BPF_STX | BPF_PROBE_ATOMIC | BPF_W:\n \t\tcase BPF_STX | BPF_PROBE_ATOMIC | BPF_DW:\n+\t\t\tif (is_atomic_fetch_op(insn)) {\n+\t\t\t\tu32 real_src_reg = src_reg, real_dst_reg = dst_reg;\n+\t\t\t\tu8 *fault[2], *resume;\n+\t\t\t\tint j;\n+\n+\t\t\t\t/*\n+\t\t\t\t * An access through a number has its address in\n+\t\t\t\t * BPF_REG_AX (see arena_scalar_access()), where the\n+\t\t\t\t * loop below has to save R0.\n+\t\t\t\t */\n+\t\t\t\tif (dst_reg == BPF_REG_AX)\n+\t\t\t\t\treturn -EOPNOTSUPP;\n+\n+\t\t\t\t/* Will need RAX as a CMPXCHG operand so save R0 */\n+\t\t\t\temit_mov_reg(\u0026prog, true, BPF_REG_AX, BPF_REG_0);\n+\t\t\t\tif (src_reg == BPF_REG_0)\n+\t\t\t\t\treal_src_reg = BPF_REG_AX;\n+\t\t\t\tif (dst_reg == BPF_REG_0)\n+\t\t\t\t\treal_dst_reg = BPF_REG_AX;\n+\n+\t\t\t\terr = emit_atomic_fetch_rmw(\u0026prog, insn, real_dst_reg,\n+\t\t\t\t\t\t\t    real_src_reg, X86_REG_R12,\n+\t\t\t\t\t\t\t    fault, \u0026resume);\n+\t\t\t\tif (err)\n+\t\t\t\t\treturn err;\n+\n+\t\t\t\t/*\n+\t\t\t\t * The page may be unmapped between the load and the\n+\t\t\t\t * CMPXCHG, so both need an entry. Both report a write,\n+\t\t\t\t * since the BPF insn is a read-modify-write.\n+\t\t\t\t */\n+\t\t\t\tfor (j = 0; j \u003c 2; j++) {\n+\t\t\t\t\terr = emit_arena_exentry(bpf_prog, image, rw_image,\n+\t\t\t\t\t\t\t\t \u0026excnt,\n+\t\t\t\t\t\t\t\t image + proglen +\n+\t\t\t\t\t\t\t\t\t (fault[j] - temp),\n+\t\t\t\t\t\t\t\t resume - fault[j],\n+\t\t\t\t\t\t\t\t reg2pt_regs[real_src_reg],\n+\t\t\t\t\t\t\t\t reg2pt_regs[real_dst_reg],\n+\t\t\t\t\t\t\t\t true, insn-\u003eoff);\n+\t\t\t\t\tif (err)\n+\t\t\t\t\t\treturn err;\n+\t\t\t\t}\n+\t\t\t\tbreak;\n+\t\t\t}\n \t\t\tstart_of_ldx = prog;\n \n \t\t\tif (bpf_atomic_is_load_store(insn))\n@@ -4353,6 +4461,21 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr\n \t\tpadding = true;\n \t\tgoto skip_init_addrs;\n \t}\n+\n+\t/*\n+\t * An arena fetching AND/OR/XOR is lowered to a CMPXCHG loop whose load\n+\t * and CMPXCHG can both fault, one more entry than the verifier reserved.\n+\t * Only reachable on the first pass, so the count is adjusted once.\n+\t */\n+\tfor (i = 0; prog-\u003eaux-\u003earena \u0026\u0026 i \u003c prog-\u003elen; i++) {\n+\t\tconst struct bpf_insn *insn = \u0026prog-\u003einsnsi[i];\n+\n+\t\tif (BPF_CLASS(insn-\u003ecode) == BPF_STX \u0026\u0026\n+\t\t    BPF_MODE(insn-\u003ecode) == BPF_PROBE_ATOMIC \u0026\u0026\n+\t\t    is_atomic_fetch_op(insn))\n+\t\t\tprog-\u003eaux-\u003enum_exentries++;\n+\t}\n+\n \taddrs = kvmalloc_objs(*addrs, prog-\u003elen + 1);\n \tif (!addrs)\n \t\tgoto out_addrs;\n@@ -4407,7 +4530,9 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr\n \t\t\t * The number of entries in extable is the number of BPF_LDX\n \t\t\t * insns that access kernel memory via \"pointer to BTF type\".\n \t\t\t * The verifier changed their opcode from LDX|MEM|size\n-\t\t\t * to LDX|PROBE_MEM|size to make JITing easier.\n+\t\t\t * to LDX|PROBE_MEM|size to make JITing easier. Arena\n+\t\t\t * accesses add one each, and an arena fetching AND/OR/XOR\n+\t\t\t * one more, see the adjustment of num_exentries above.\n \t\t\t */\n \t\t\tu32 align = __alignof__(struct exception_table_entry);\n \t\t\tu32 extable_size = prog-\u003eaux-\u003enum_exentries *\n@@ -4681,16 +4806,6 @@ bool bpf_jit_supports_arena_scalar(void)\n \n bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)\n {\n-\tif (!in_arena)\n-\t\treturn true;\n-\tswitch (insn-\u003ecode) {\n-\tcase BPF_STX | BPF_ATOMIC | BPF_W:\n-\tcase BPF_STX | BPF_ATOMIC | BPF_DW:\n-\t\tif (insn-\u003eimm == (BPF_AND | BPF_FETCH) ||\n-\t\t    insn-\u003eimm == (BPF_OR | BPF_FETCH) ||\n-\t\t    insn-\u003eimm == (BPF_XOR | BPF_FETCH))\n-\t\t\treturn false;\n-\t}\n \treturn true;\n }\n \ndiff --git a/tools/sched_ext/include/scx/cid.bpf.h b/tools/sched_ext/include/scx/cid.bpf.h\nindex 69fb4e97bc771..d0d4cea19e29f 100644\n--- a/tools/sched_ext/include/scx/cid.bpf.h\n+++ b/tools/sched_ext/include/scx/cid.bpf.h\n@@ -3,8 +3,8 @@\n  * BPF-side helpers for cids and cmasks. See kernel/sched/ext/cid.h for the\n  * authoritative layout and semantics. The BPF-side helpers use the cmask_*\n  * naming (no scx_ prefix); cmask is the SCX bitmap type so the prefix is\n- * redundant in BPF code. Atomics use __sync_val_compare_and_swap and every\n- * helper is inline (no .c counterpart).\n+ * redundant in BPF code. Atomics use the __sync_* builtins and every helper\n+ * is inline (no .c counterpart).\n  *\n  * Included by scx/common.bpf.h; don't include directly.\n  *\n@@ -129,101 +129,38 @@ static __always_inline bool cmask_test(u32 cid, const struct scx_cmask __arena *\n \treturn *__cmask_word(cid, m) \u0026 BIT_U64(cid \u0026 63);\n }\n \n-/*\n- * x86 BPF JIT rejects BPF_OR | BPF_FETCH and BPF_AND | BPF_FETCH on arena\n- * pointers (see bpf_jit_supports_insn() in arch/x86/net/bpf_jit_comp.c). Only\n- * BPF_CMPXCHG / BPF_XCHG / BPF_ADD with FETCH are allowed. Implement\n- * test_and_{set,clear} and the atomic set/clear via a cmpxchg loop.\n- *\n- * CMASK_CAS_TRIES is sized so exhausting it means seconds of real spinning\n- * on one word - past any plausible contention. Abort hard.\n- */\n-#define CMASK_CAS_TRIES\t\t(1U \u003c\u003c 23)\n-\n static __always_inline void cmask_set(u32 cid, struct scx_cmask __arena *m)\n {\n-\tu64 __arena *w;\n-\tu64 bit, old, new;\n-\tu32 i;\n-\n \tif (!__cmask_contains(cid, m))\n \t\treturn;\n-\tw = __cmask_word(cid, m);\n-\tbit = BIT_U64(cid \u0026 63);\n-\tbpf_for(i, 0, CMASK_CAS_TRIES) {\n-\t\told = *w;\n-\t\tif (old \u0026 bit)\n-\t\t\treturn;\n-\t\tnew = old | bit;\n-\t\tif (__sync_val_compare_and_swap(w, old, new) == old)\n-\t\t\treturn;\n-\t}\n-\tscx_bpf_error(\"cmask_set CAS exhausted at cid %u\", cid);\n+\t__sync_fetch_and_or(__cmask_word(cid, m), BIT_U64(cid \u0026 63));\n }\n \n static __always_inline void cmask_clear(u32 cid, struct scx_cmask __arena *m)\n {\n-\tu64 __arena *w;\n-\tu64 bit, old, new;\n-\tu32 i;\n-\n \tif (!__cmask_contains(cid, m))\n \t\treturn;\n-\tw = __cmask_word(cid, m);\n-\tbit = BIT_U64(cid \u0026 63);\n-\tbpf_for(i, 0, CMASK_CAS_TRIES) {\n-\t\told = *w;\n-\t\tif (!(old \u0026 bit))\n-\t\t\treturn;\n-\t\tnew = old \u0026 ~bit;\n-\t\tif (__sync_val_compare_and_swap(w, old, new) == old)\n-\t\t\treturn;\n-\t}\n-\tscx_bpf_error(\"cmask_clear CAS exhausted at cid %u\", cid);\n+\t__sync_fetch_and_and(__cmask_word(cid, m), ~BIT_U64(cid \u0026 63));\n }\n \n static __always_inline bool cmask_test_and_set(u32 cid, struct scx_cmask __arena *m)\n {\n-\tu64 __arena *w;\n-\tu64 bit, old, new;\n-\tu32 i;\n+\tu64 bit;\n \n \tif (!__cmask_contains(cid, m))\n \t\treturn false;\n-\tw = __cmask_word(cid, m);\n \tbit = BIT_U64(cid \u0026 63);\n-\tbpf_for(i, 0, CMASK_CAS_TRIES) {\n-\t\told = *w;\n-\t\tif (old \u0026 bit)\n-\t\t\treturn true;\n-\t\tnew = old | bit;\n-\t\tif (__sync_val_compare_and_swap(w, old, new) == old)\n-\t\t\treturn false;\n-\t}\n-\tscx_bpf_error(\"cmask_test_and_set CAS exhausted at cid %u\", cid);\n-\treturn false;\n+\treturn __sync_fetch_and_or(__cmask_word(cid, m), bit) \u0026 bit;\n }\n \n static __always_inline bool cmask_test_and_clear(u32 cid, struct scx_cmask __arena *m)\n {\n-\tu64 __arena *w;\n-\tu64 bit, old, new;\n-\tu32 i;\n+\tu64 bit;\n \n \tif (!__cmask_contains(cid, m))\n \t\treturn false;\n-\tw = __cmask_word(cid, m);\n \tbit = BIT_U64(cid \u0026 63);\n-\tbpf_for(i, 0, CMASK_CAS_TRIES) {\n-\t\told = *w;\n-\t\tif (!(old \u0026 bit))\n-\t\t\treturn false;\n-\t\tnew = old \u0026 ~bit;\n-\t\tif (__sync_val_compare_and_swap(w, old, new) == old)\n-\t\t\treturn true;\n-\t}\n-\tscx_bpf_error(\"cmask_test_and_clear CAS exhausted at cid %u\", cid);\n-\treturn false;\n+\treturn __sync_fetch_and_and(__cmask_word(cid, m), ~bit) \u0026 bit;\n }\n \n static __always_inline void __cmask_set(u32 cid, struct scx_cmask __arena *m)\ndiff --git a/tools/testing/selftests/bpf/prog_tests/arena_atomics.c b/tools/testing/selftests/bpf/prog_tests/arena_atomics.c\nindex 1ad5d03d07adb..81f6e554a8c12 100644\n--- a/tools/testing/selftests/bpf/prog_tests/arena_atomics.c\n+++ b/tools/testing/selftests/bpf/prog_tests/arena_atomics.c\n@@ -68,6 +68,12 @@ static void test_and(struct arena_atomics *skel)\n \n \tASSERT_EQ(skel-\u003earena-\u003eand64_value, 0x010ull \u003c\u003c 32, \"and64_value\");\n \tASSERT_EQ(skel-\u003earena-\u003eand32_value, 0x010, \"and32_value\");\n+\n+\tASSERT_EQ(skel-\u003earena-\u003eand64_result, 0x110ull \u003c\u003c 32, \"and64_result\");\n+\tASSERT_EQ(skel-\u003earena-\u003eand32_result, 0x110, \"and32_result\");\n+\n+\tASSERT_EQ(skel-\u003earena-\u003eand64_noreturn_value, 0x010ull \u003c\u003c 32, \"and64_noreturn_value\");\n+\tASSERT_EQ(skel-\u003earena-\u003eand32_noreturn_value, 0x010, \"and32_noreturn_value\");\n }\n \n static void test_or(struct arena_atomics *skel)\n@@ -85,6 +91,12 @@ static void test_or(struct arena_atomics *skel)\n \n \tASSERT_EQ(skel-\u003earena-\u003eor64_value, 0x111ull \u003c\u003c 32, \"or64_value\");\n \tASSERT_EQ(skel-\u003earena-\u003eor32_value, 0x111, \"or32_value\");\n+\n+\tASSERT_EQ(skel-\u003earena-\u003eor64_result, 0x110ull \u003c\u003c 32, \"or64_result\");\n+\tASSERT_EQ(skel-\u003earena-\u003eor32_result, 0x110, \"or32_result\");\n+\n+\tASSERT_EQ(skel-\u003earena-\u003eor64_noreturn_value, 0x111ull \u003c\u003c 32, \"or64_noreturn_value\");\n+\tASSERT_EQ(skel-\u003earena-\u003eor32_noreturn_value, 0x111, \"or32_noreturn_value\");\n }\n \n static void test_xor(struct arena_atomics *skel)\n@@ -102,6 +114,12 @@ static void test_xor(struct arena_atomics *skel)\n \n \tASSERT_EQ(skel-\u003earena-\u003exor64_value, 0x101ull \u003c\u003c 32, \"xor64_value\");\n \tASSERT_EQ(skel-\u003earena-\u003exor32_value, 0x101, \"xor32_value\");\n+\n+\tASSERT_EQ(skel-\u003earena-\u003exor64_result, 0x110ull \u003c\u003c 32, \"xor64_result\");\n+\tASSERT_EQ(skel-\u003earena-\u003exor32_result, 0x110, \"xor32_result\");\n+\n+\tASSERT_EQ(skel-\u003earena-\u003exor64_noreturn_value, 0x101ull \u003c\u003c 32, \"xor64_noreturn_value\");\n+\tASSERT_EQ(skel-\u003earena-\u003exor32_noreturn_value, 0x101, \"xor32_noreturn_value\");\n }\n \n static void test_cmpxchg(struct arena_atomics *skel)\n@@ -146,6 +164,27 @@ static void test_xchg(struct arena_atomics *skel)\n \tASSERT_EQ(skel-\u003earena-\u003exchg32_result, 1, \"xchg32_result\");\n }\n \n+static void test_fetch_r0(struct arena_atomics *skel)\n+{\n+\tLIBBPF_OPTS(bpf_test_run_opts, topts);\n+\tint err, prog_fd;\n+\n+\t/* No need to attach it, just run it directly */\n+\tprog_fd = bpf_program__fd(skel-\u003eprogs.fetch_r0);\n+\terr = bpf_prog_test_run_opts(prog_fd, \u0026topts);\n+\tif (!ASSERT_OK(err, \"test_run_opts err\"))\n+\t\treturn;\n+\tif (!ASSERT_OK(topts.retval, \"test_run_opts retval\"))\n+\t\treturn;\n+\n+\tASSERT_EQ(skel-\u003earena-\u003efetch_src_r0_value, 0x11100000111ull, \"fetch_src_r0_value\");\n+\tASSERT_EQ(skel-\u003earena-\u003efetch_src_r0_result, 0x11000000110ull, \"fetch_src_r0_result\");\n+\n+\tASSERT_EQ(skel-\u003earena-\u003efetch_dst_r0_value, 0x11100000111ull, \"fetch_dst_r0_value\");\n+\tASSERT_EQ(skel-\u003earena-\u003efetch_dst_r0_result, 0x11000000110ull, \"fetch_dst_r0_result\");\n+\tASSERT_EQ(skel-\u003earena-\u003efetch_dst_r0_readback, 0x11100000111ull, \"fetch_dst_r0_readback\");\n+}\n+\n static void test_uaf(struct arena_atomics *skel)\n {\n \tLIBBPF_OPTS(bpf_test_run_opts, topts);\n@@ -160,6 +199,12 @@ static void test_uaf(struct arena_atomics *skel)\n \t\treturn;\n \n \tASSERT_EQ(skel-\u003earena-\u003euaf_recovery_fails, 0, \"uaf_recovery_fails\");\n+\n+\t/* A fault clears the fetched register and leaves the address alone */\n+\tASSERT_EQ(skel-\u003earena-\u003euaf_r1_after_dst_r0, 0, \"uaf_r1_after_dst_r0\");\n+\tASSERT_EQ(skel-\u003earena-\u003euaf_r0_after_dst_r0, skel-\u003earena-\u003euaf_r0_before_dst_r0,\n+\t\t  \"uaf_r0_after_dst_r0\");\n+\tASSERT_EQ(skel-\u003earena-\u003euaf_r0_after_src_r0, 0, \"uaf_r0_after_src_r0\");\n }\n \n static void test_load_acquire(struct arena_atomics *skel)\n@@ -256,6 +301,8 @@ void serial_test_arena_atomics(void)\n \t\ttest_cmpxchg(skel);\n \tif (test__start_subtest(\"xchg\"))\n \t\ttest_xchg(skel);\n+\tif (test__start_subtest(\"fetch_r0\"))\n+\t\ttest_fetch_r0(skel);\n \tif (test__start_subtest(\"uaf\"))\n \t\ttest_uaf(skel);\n \tif (test__start_subtest(\"load_acquire\"))\ndiff --git a/tools/testing/selftests/bpf/progs/arena_atomics.c b/tools/testing/selftests/bpf/progs/arena_atomics.c\nindex 73bc2b835f3fe..e89b468fc6226 100644\n--- a/tools/testing/selftests/bpf/progs/arena_atomics.c\n+++ b/tools/testing/selftests/bpf/progs/arena_atomics.c\n@@ -91,13 +91,17 @@ int sub(const void *ctx)\n \treturn 0;\n }\n \n-#ifdef __BPF_FEATURE_ATOMIC_MEM_ORDERING\n-_Atomic __u64 __arena_global and64_value = (0x110ull \u003c\u003c 32);\n-_Atomic __u32 __arena_global and32_value = 0x110;\n-#else\n+/*\n+ * A relaxed __c11_atomic_fetch_*() whose result is discarded is what selects\n+ * the non-fetching insn, so each operation keeps a *_noreturn_value of each\n+ * width to cover that lowering as well.\n+ */\n __u64 __arena_global and64_value = (0x110ull \u003c\u003c 32);\n+__u64 __arena_global and64_result = 0;\n+_Atomic __u64 __arena_global and64_noreturn_value = (0x110ull \u003c\u003c 32);\n __u32 __arena_global and32_value = 0x110;\n-#endif\n+__u32 __arena_global and32_result = 0;\n+_Atomic __u32 __arena_global and32_noreturn_value = 0x110;\n \n SEC(\"raw_tp/sys_enter\")\n int and(const void *ctx)\n@@ -105,25 +109,22 @@ int and(const void *ctx)\n \tif (pid != (bpf_get_current_pid_tgid() \u003e\u003e 32))\n \t\treturn 0;\n #ifdef ENABLE_ATOMICS_TESTS\n-#ifdef __BPF_FEATURE_ATOMIC_MEM_ORDERING\n-\t__c11_atomic_fetch_and(\u0026and64_value, 0x011ull \u003c\u003c 32, memory_order_relaxed);\n-\t__c11_atomic_fetch_and(\u0026and32_value, 0x011, memory_order_relaxed);\n-#else\n-\t__sync_fetch_and_and(\u0026and64_value, 0x011ull \u003c\u003c 32);\n-\t__sync_fetch_and_and(\u0026and32_value, 0x011);\n-#endif\n+\tand64_result = __sync_fetch_and_and(\u0026and64_value, 0x011ull \u003c\u003c 32);\n+\tand32_result = __sync_fetch_and_and(\u0026and32_value, 0x011);\n+\t__c11_atomic_fetch_and(\u0026and64_noreturn_value, 0x011ull \u003c\u003c 32, memory_order_relaxed);\n+\t__c11_atomic_fetch_and(\u0026and32_noreturn_value, 0x011, memory_order_relaxed);\n #endif\n \n \treturn 0;\n }\n \n-#ifdef __BPF_FEATURE_ATOMIC_MEM_ORDERING\n-_Atomic __u32 __arena_global or32_value = 0x110;\n-_Atomic __u64 __arena_global or64_value = (0x110ull \u003c\u003c 32);\n-#else\n+/* u32s first: .addr_space.1 must not have holes, see the clang-17 note below */\n __u32 __arena_global or32_value = 0x110;\n+__u32 __arena_global or32_result = 0;\n+_Atomic __u32 __arena_global or32_noreturn_value = 0x110;\n __u64 __arena_global or64_value = (0x110ull \u003c\u003c 32);\n-#endif\n+__u64 __arena_global or64_result = 0;\n+_Atomic __u64 __arena_global or64_noreturn_value = (0x110ull \u003c\u003c 32);\n \n SEC(\"raw_tp/sys_enter\")\n int or(const void *ctx)\n@@ -131,25 +132,21 @@ int or(const void *ctx)\n \tif (pid != (bpf_get_current_pid_tgid() \u003e\u003e 32))\n \t\treturn 0;\n #ifdef ENABLE_ATOMICS_TESTS\n-#ifdef __BPF_FEATURE_ATOMIC_MEM_ORDERING\n-\t__c11_atomic_fetch_or(\u0026or64_value, 0x011ull \u003c\u003c 32, memory_order_relaxed);\n-\t__c11_atomic_fetch_or(\u0026or32_value, 0x011, memory_order_relaxed);\n-#else\n-\t__sync_fetch_and_or(\u0026or64_value, 0x011ull \u003c\u003c 32);\n-\t__sync_fetch_and_or(\u0026or32_value, 0x011);\n-#endif\n+\tor64_result = __sync_fetch_and_or(\u0026or64_value, 0x011ull \u003c\u003c 32);\n+\tor32_result = __sync_fetch_and_or(\u0026or32_value, 0x011);\n+\t__c11_atomic_fetch_or(\u0026or64_noreturn_value, 0x011ull \u003c\u003c 32, memory_order_relaxed);\n+\t__c11_atomic_fetch_or(\u0026or32_noreturn_value, 0x011, memory_order_relaxed);\n #endif\n \n \treturn 0;\n }\n \n-#ifdef __BPF_FEATURE_ATOMIC_MEM_ORDERING\n-_Atomic __u64 __arena_global xor64_value = (0x110ull \u003c\u003c 32);\n-_Atomic __u32 __arena_global xor32_value = 0x110;\n-#else\n __u64 __arena_global xor64_value = (0x110ull \u003c\u003c 32);\n+__u64 __arena_global xor64_result = 0;\n+_Atomic __u64 __arena_global xor64_noreturn_value = (0x110ull \u003c\u003c 32);\n __u32 __arena_global xor32_value = 0x110;\n-#endif\n+__u32 __arena_global xor32_result = 0;\n+_Atomic __u32 __arena_global xor32_noreturn_value = 0x110;\n \n SEC(\"raw_tp/sys_enter\")\n int xor(const void *ctx)\n@@ -157,13 +154,10 @@ int xor(const void *ctx)\n \tif (pid != (bpf_get_current_pid_tgid() \u003e\u003e 32))\n \t\treturn 0;\n #ifdef ENABLE_ATOMICS_TESTS\n-#ifdef __BPF_FEATURE_ATOMIC_MEM_ORDERING\n-\t__c11_atomic_fetch_xor(\u0026xor64_value, 0x011ull \u003c\u003c 32, memory_order_relaxed);\n-\t__c11_atomic_fetch_xor(\u0026xor32_value, 0x011, memory_order_relaxed);\n-#else\n-\t__sync_fetch_and_xor(\u0026xor64_value, 0x011ull \u003c\u003c 32);\n-\t__sync_fetch_and_xor(\u0026xor32_value, 0x011);\n-#endif\n+\txor64_result = __sync_fetch_and_xor(\u0026xor64_value, 0x011ull \u003c\u003c 32);\n+\txor32_result = __sync_fetch_and_xor(\u0026xor32_value, 0x011);\n+\t__c11_atomic_fetch_xor(\u0026xor64_noreturn_value, 0x011ull \u003c\u003c 32, memory_order_relaxed);\n+\t__c11_atomic_fetch_xor(\u0026xor32_noreturn_value, 0x011, memory_order_relaxed);\n #endif\n \n \treturn 0;\n@@ -213,23 +207,93 @@ int xchg(const void *ctx)\n \treturn 0;\n }\n \n+__u64 __arena_global fetch_src_r0_value = 0x11000000110ull;\n+__u64 __arena_global fetch_src_r0_result = 0;\n+__u64 __arena_global fetch_dst_r0_value = 0x11000000110ull;\n+__u64 __arena_global fetch_dst_r0_result = 0;\n+__u64 __arena_global fetch_dst_r0_readback = 0;\n+\n+/*\n+ * A fetching OR with the operand in r0, and one with the arena pointer in r0.\n+ * The x86 JIT needs RAX for its CMPXCHG loop and substitutes BPF_REG_AX for\n+ * whichever of the two is r0, so both have to keep working. Hand-written\n+ * because clang picks its own registers and will not reliably emit either.\n+ */\n+SEC(\"raw_tp/sys_enter\")\n+int fetch_r0(const void *ctx)\n+{\n+\tif (pid != (bpf_get_current_pid_tgid() \u003e\u003e 32))\n+\t\treturn 0;\n+#if defined(ENABLE_ATOMICS_TESTS) \u0026\u0026 defined(__BPF_FEATURE_ADDR_SPACE_CAST)\n+\tasm volatile (\n+\t\"r1 = %[fetch_src_r0_value] ll;\"\n+\t\"r1 = addr_space_cast(r1, 0x0, 0x1);\"\n+\t\"r0 = 0x1100000011 ll;\"\n+\t\".8byte %[fetch_src_r0_insn];\"\n+\t\"r2 = %[fetch_src_r0_result] ll;\"\n+\t\"r2 = addr_space_cast(r2, 0x0, 0x1);\"\n+\t\"*(u64 *)(r2 + 0) = r0;\"\n+\t:\n+\t: __imm_addr(fetch_src_r0_value),\n+\t  __imm_insn(fetch_src_r0_insn,\n+\t\t     BPF_ATOMIC_OP(BPF_DW, BPF_OR | BPF_FETCH, BPF_REG_1, BPF_REG_0, 0)),\n+\t  __imm_addr(fetch_src_r0_result)\n+\t: __clobber_all);\n+\n+\tasm volatile (\n+\t\"r0 = %[fetch_dst_r0_value] ll;\"\n+\t\"r0 = addr_space_cast(r0, 0x0, 0x1);\"\n+\t\"r1 = 0x1100000011 ll;\"\n+\t\".8byte %[fetch_dst_r0_insn];\"\n+\t\"r2 = %[fetch_dst_r0_result] ll;\"\n+\t\"r2 = addr_space_cast(r2, 0x0, 0x1);\"\n+\t\"*(u64 *)(r2 + 0) = r1;\"\n+\t/* r0 is only read by the atomic, so it must still be the pointer. */\n+\t\"r3 = *(u64 *)(r0 + 0);\"\n+\t\"r2 = %[fetch_dst_r0_readback] ll;\"\n+\t\"r2 = addr_space_cast(r2, 0x0, 0x1);\"\n+\t\"*(u64 *)(r2 + 0) = r3;\"\n+\t:\n+\t: __imm_addr(fetch_dst_r0_value),\n+\t  __imm_insn(fetch_dst_r0_insn,\n+\t\t     BPF_ATOMIC_OP(BPF_DW, BPF_OR | BPF_FETCH, BPF_REG_0, BPF_REG_1, 0)),\n+\t  __imm_addr(fetch_dst_r0_result),\n+\t  __imm_addr(fetch_dst_r0_readback)\n+\t: __clobber_all);\n+#endif\n+\n+\treturn 0;\n+}\n+\n __u64 __arena_global uaf_sink;\n volatile __u64 __arena_global uaf_recovery_fails;\n+/* Register state after the r0 forms below fault, checked by the test */\n+__u64 __arena_global uaf_r1_after_dst_r0;\n+__u64 __arena_global uaf_r0_before_dst_r0;\n+__u64 __arena_global uaf_r0_after_dst_r0;\n+__u64 __arena_global uaf_r0_after_src_r0;\n \n SEC(\"syscall\")\n int uaf(const void *ctx)\n {\n \tif (pid != (bpf_get_current_pid_tgid() \u003e\u003e 32))\n \t\treturn 0;\n+/*\n+ * __BPF_FEATURE_ADDR_SPACE_CAST is required by the asm below, which spells\n+ * addr_space_cast textually. That and the mnemonic both arrived in LLVM 19.\n+ */\n #if defined(ENABLE_ATOMICS_TESTS) \u0026\u0026 !defined(__TARGET_ARCH_arm64) \u0026\u0026 \\\n-    !defined(__TARGET_ARCH_x86)\n+\tdefined(__BPF_FEATURE_ADDR_SPACE_CAST)\n \t__u32 __arena *page32;\n \t__u64 __arena *page64;\n \tvoid __arena *page;\n \n \tpage = bpf_arena_alloc_pages(\u0026arena, NULL, 1, NUMA_NO_NODE, 0);\n \tbpf_arena_free_pages(\u0026arena, page, 1);\n-\tuaf_recovery_fails = 24;\n+\tuaf_recovery_fails = 26;\n+\tuaf_r1_after_dst_r0 = 1;\n+\tuaf_r0_after_dst_r0 = 1;\n+\tuaf_r0_after_src_r0 = 1;\n \n \tpage32 = (__u32 __arena *)page;\n \tuaf_sink += __sync_fetch_and_add(page32, 1);\n@@ -282,6 +346,50 @@ int uaf(const void *ctx)\n \tuaf_recovery_fails -= 1;\n \tuaf_sink += __sync_lock_test_and_set(page64, 1);\n \tuaf_recovery_fails -= 1;\n+\n+\t/*\n+\t * The x86 JIT needs RAX for the CMPXCHG loop it lowers a fetching\n+\t * AND/OR/XOR into, and substitutes BPF_REG_AX for r0 when r0 is either\n+\t * operand. Only then does the fault fixup have to name that register,\n+\t * so spell both forms out: clang picks its own and never produces them.\n+\t */\n+\tasm volatile (\n+\t\"r0 = %[page];\"\n+\t\"r0 = addr_space_cast(r0, 0x0, 0x1);\"\n+\t\"r1 = 1;\"\n+\t\"r2 = %[uaf_r0_before_dst_r0] ll;\"\n+\t\"r2 = addr_space_cast(r2, 0x0, 0x1);\"\n+\t\"*(u64 *)(r2 + 0) = r0;\"\n+\t\".8byte %[fetch_dst_r0];\"\n+\t\"r2 = %[uaf_r0_after_dst_r0] ll;\"\n+\t\"r2 = addr_space_cast(r2, 0x0, 0x1);\"\n+\t\"*(u64 *)(r2 + 0) = r0;\"\n+\t\"r2 = %[uaf_r1_after_dst_r0] ll;\"\n+\t\"r2 = addr_space_cast(r2, 0x0, 0x1);\"\n+\t\"*(u64 *)(r2 + 0) = r1;\"\n+\t: : [page]\"r\"(page64),\n+\t    __imm_insn(fetch_dst_r0,\n+\t\t       BPF_ATOMIC_OP(BPF_DW, BPF_OR | BPF_FETCH, BPF_REG_0, BPF_REG_1, 0)),\n+\t    __imm_addr(uaf_r0_before_dst_r0),\n+\t    __imm_addr(uaf_r0_after_dst_r0),\n+\t    __imm_addr(uaf_r1_after_dst_r0)\n+\t: \"r0\", \"r1\", \"r2\", \"memory\");\n+\tuaf_recovery_fails -= 1;\n+\n+\tasm volatile (\n+\t\"r1 = %[page];\"\n+\t\"r1 = addr_space_cast(r1, 0x0, 0x1);\"\n+\t\"r0 = 1;\"\n+\t\".8byte %[fetch_src_r0];\"\n+\t\"r2 = %[uaf_r0_after_src_r0] ll;\"\n+\t\"r2 = addr_space_cast(r2, 0x0, 0x1);\"\n+\t\"*(u64 *)(r2 + 0) = r0;\"\n+\t: : [page]\"r\"(page64),\n+\t    __imm_insn(fetch_src_r0,\n+\t\t       BPF_ATOMIC_OP(BPF_DW, BPF_OR | BPF_FETCH, BPF_REG_1, BPF_REG_0, 0)),\n+\t    __imm_addr(uaf_r0_after_src_r0)\n+\t: \"r0\", \"r1\", \"r2\", \"memory\");\n+\tuaf_recovery_fails -= 1;\n #endif\n \n \treturn 0;\ndiff --git a/tools/testing/selftests/bpf/progs/verifier_arena_scalar.c b/tools/testing/selftests/bpf/progs/verifier_arena_scalar.c\nindex bebc37f501b7e..7a7e06d9596ce 100644\n--- a/tools/testing/selftests/bpf/progs/verifier_arena_scalar.c\n+++ b/tools/testing/selftests/bpf/progs/verifier_arena_scalar.c\n@@ -256,6 +256,104 @@ __naked void atomics(void)\n \t: __clobber_all);\n }\n \n+SEC(\"syscall\")\n+__flag(BPF_F_ARENA_SCALAR) __arch_arm64\n+__description(\"arena_scalar: fetching and/or/xor\")\n+__success __retval(0)\n+__load_if_JITed()\n+__naked void fetch_and_or_xor(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[arena] ll;\t\t\t\t\\\n+\tr2 = 0;\t\t\t\t\t\t\\\n+\tr3 = 1;\t\t\t\t\t\t\\\n+\tr4 = -1;\t\t\t\t\t\\\n+\tr5 = 0;\t\t\t\t\t\t\\\n+\tcall %[bpf_arena_alloc_pages];\t\t\t\\\n+\tr6 = r0;\t\t\t\t\t\\\n+\tr0 = 100;\t\t\t\t\t\\\n+\tif r6 == 0 goto 9f;\t\t\t\t\\\n+\t*(u64 *)(r6 + 8) = 0x110;\t\t\t\\\n+\tr1 = 0x011;\t\t\t\t\t\\\n+\t.8byte %[fetch_or_insn]; /* r1 = atomic_fetch_or((u64 *)(r6 + 8), r1) */ \\\n+\tr0 = 1;\t\t\t\t\t\t\\\n+\tif r1 != 0x110 goto 9f;\t\t\t\t\\\n+\tr1 = 0x101;\t\t\t\t\t\\\n+\t.8byte %[fetch_and_insn]; /* r1 = atomic_fetch_and((u64 *)(r6 + 8), r1) */ \\\n+\tr0 = 2;\t\t\t\t\t\t\\\n+\tif r1 != 0x111 goto 9f;\t\t\t\t\\\n+\tr1 = 0x011;\t\t\t\t\t\\\n+\t.8byte %[fetch_xor_insn]; /* r1 = atomic_fetch_xor((u64 *)(r6 + 8), r1) */ \\\n+\tr0 = 3;\t\t\t\t\t\t\\\n+\tif r1 != 0x101 goto 9f;\t\t\t\t\\\n+\tr2 = *(u64 *)(r6 + 8);\t\t\t\t\\\n+\tr0 = 4;\t\t\t\t\t\t\\\n+\tif r2 != 0x110 goto 9f;\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+9:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(arena),\n+\t  __imm(bpf_arena_alloc_pages),\n+\t  __imm_insn(fetch_or_insn, BPF_ATOMIC_OP(BPF_DW, BPF_OR | BPF_FETCH,\n+\t\t\t\t\t\t  BPF_REG_6, BPF_REG_1, 8)),\n+\t  __imm_insn(fetch_and_insn, BPF_ATOMIC_OP(BPF_DW, BPF_AND | BPF_FETCH,\n+\t\t\t\t\t\t   BPF_REG_6, BPF_REG_1, 8)),\n+\t  __imm_insn(fetch_xor_insn, BPF_ATOMIC_OP(BPF_DW, BPF_XOR | BPF_FETCH,\n+\t\t\t\t\t\t   BPF_REG_6, BPF_REG_1, 8))\n+\t: __clobber_all);\n+}\n+\n+/*\n+ * The x86-64 JIT saves R0 in BPF_REG_AX to lower these, which is where an\n+ * access through a number keeps its address, so it refuses them.\n+ */\n+SEC(\"syscall\")\n+__flag(BPF_F_ARENA_SCALAR) __arch_x86_64\n+__description(\"arena_scalar: fetching and/or/xor refused on x86-64\")\n+__failure\n+__load_if_JITed()\n+__naked void fetch_and_or_xor_x86(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[arena] ll;\t\t\t\t\\\n+\tr2 = 0;\t\t\t\t\t\t\\\n+\tr3 = 1;\t\t\t\t\t\t\\\n+\tr4 = -1;\t\t\t\t\t\\\n+\tr5 = 0;\t\t\t\t\t\t\\\n+\tcall %[bpf_arena_alloc_pages];\t\t\t\\\n+\tr6 = r0;\t\t\t\t\t\\\n+\tr0 = 100;\t\t\t\t\t\\\n+\tif r6 == 0 goto 9f;\t\t\t\t\\\n+\t*(u64 *)(r6 + 8) = 0x110;\t\t\t\\\n+\tr1 = 0x011;\t\t\t\t\t\\\n+\t.8byte %[fetch_or_insn]; /* r1 = atomic_fetch_or((u64 *)(r6 + 8), r1) */ \\\n+\tr0 = 1;\t\t\t\t\t\t\\\n+\tif r1 != 0x110 goto 9f;\t\t\t\t\\\n+\tr1 = 0x101;\t\t\t\t\t\\\n+\t.8byte %[fetch_and_insn]; /* r1 = atomic_fetch_and((u64 *)(r6 + 8), r1) */ \\\n+\tr0 = 2;\t\t\t\t\t\t\\\n+\tif r1 != 0x111 goto 9f;\t\t\t\t\\\n+\tr1 = 0x011;\t\t\t\t\t\\\n+\t.8byte %[fetch_xor_insn]; /* r1 = atomic_fetch_xor((u64 *)(r6 + 8), r1) */ \\\n+\tr0 = 3;\t\t\t\t\t\t\\\n+\tif r1 != 0x101 goto 9f;\t\t\t\t\\\n+\tr2 = *(u64 *)(r6 + 8);\t\t\t\t\\\n+\tr0 = 4;\t\t\t\t\t\t\\\n+\tif r2 != 0x110 goto 9f;\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+9:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(arena),\n+\t  __imm(bpf_arena_alloc_pages),\n+\t  __imm_insn(fetch_or_insn, BPF_ATOMIC_OP(BPF_DW, BPF_OR | BPF_FETCH,\n+\t\t\t\t\t\t  BPF_REG_6, BPF_REG_1, 8)),\n+\t  __imm_insn(fetch_and_insn, BPF_ATOMIC_OP(BPF_DW, BPF_AND | BPF_FETCH,\n+\t\t\t\t\t\t   BPF_REG_6, BPF_REG_1, 8)),\n+\t  __imm_insn(fetch_xor_insn, BPF_ATOMIC_OP(BPF_DW, BPF_XOR | BPF_FETCH,\n+\t\t\t\t\t\t   BPF_REG_6, BPF_REG_1, 8))\n+\t: __clobber_all);\n+}\n+\n SEC(\"syscall\")\n __arena_scalar\n __description(\"arena_scalar: cmpxchg through r0\")\n"
}

2/1 2026/10/14 19:06 agent patch-evaluator
1m Model:
core

Args:
null

Results:
{
  "EnableConfigs": [
    "BPF_SYSCALL",
    "BPF_JIT"
  ],
  "FocusSymbols": [
    "bpf_int_jit_compile",
    "do_jit",
    "emit_atomic_fetch_rmw",
    "emit_arena_exentry",
    "bpf_jit_supports_insn",
    "ex_handler_bpf"
  ],
  "Reasoning": "The patch adds support in the x86 BPF JIT compiler for atomic fetch read-modify-write operations (BPF_AND/OR/XOR with BPF_FETCH) on BPF arena memory pointers. It implements a CMPXCHG loop for atomic fetch RMW, generates exception table entries for fault recovery during arena accesses, adjusts the BPF prog exception entry count, and lifts previous rejection in bpf_jit_supports_insn. This touches reachable core BPF JIT and exception handling logic on amd64 accessible via the bpf() system call.",
  "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 b883a66f73e1dc8ca4b2e7a43e9c65ea1f8e1cdd
Author: syz-cluster <triage@syzkaller.com>
Date:   Wed Oct 14 19:06:06 2026 +0000

    syz-cluster: applied patch under review

diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
index 083fcd6cf15b7..5fb1a31f71a26 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -239,6 +239,13 @@ static const int reg2pt_regs[] = {
 	[BPF_REG_AX] = offsetof(struct pt_regs, r10),
 };
 
+static bool is_atomic_fetch_op(const struct bpf_insn *insn)
+{
+	return insn->imm == (BPF_AND | BPF_FETCH) ||
+	       insn->imm == (BPF_OR | BPF_FETCH) ||
+	       insn->imm == (BPF_XOR | BPF_FETCH);
+}
+
 /*
  * is_ereg() == true if BPF register 'reg' maps to x86-64 r8..r15
  * which need extra byte of encoding.
@@ -1680,6 +1687,72 @@ static int emit_atomic_ld_st_index(u8 **pprog, u32 atomic_op, u32 size,
 	return 0;
 }
 
+/*
+ * A fetching AND/OR/XOR can't be implemented with a single x86 insn, so do a
+ * CMPXCHG loop. @index_reg is X86_REG_R12 for an arena access or -1 otherwise,
+ * and @dst_reg/@src_reg are already substituted for R0 by the caller.
+ *
+ * Both the load and the CMPXCHG can fault when accessing the arena, so their
+ * addresses are handed back in @fault[0] and @fault[1]. A fault has to resume
+ * at @resume, which is past the loop and past the move that delivers the old
+ * value, but before the R0 restore. Both are NULL for the non-arena case,
+ * which needs no fixup.
+ */
+static int emit_atomic_fetch_rmw(u8 **pprog, struct bpf_insn *insn, u32 dst_reg,
+				 u32 src_reg, int index_reg, u8 **fault,
+				 u8 **resume)
+{
+	bool is64 = BPF_SIZE(insn->code) == BPF_DW;
+	u8 *branch_target, *prog = *pprog;
+	int err;
+
+	branch_target = prog;
+
+	/* Load old value */
+	if (fault)
+		fault[0] = prog;
+	if (index_reg < 0)
+		emit_ldx(&prog, BPF_SIZE(insn->code), BPF_REG_0, dst_reg, insn->off);
+	else
+		emit_ldx_index(&prog, BPF_SIZE(insn->code), BPF_REG_0, dst_reg,
+			       index_reg, insn->off);
+
+	/*
+	 * Perform the (commutative) operation locally, put the result in
+	 * the AUX_REG.
+	 */
+	emit_mov_reg(&prog, is64, AUX_REG, BPF_REG_0);
+	maybe_emit_mod(&prog, AUX_REG, src_reg, is64);
+	EMIT2(simple_alu_opcodes[BPF_OP(insn->imm)],
+	      add_2reg(0xC0, AUX_REG, src_reg));
+
+	/* Attempt to swap in new value */
+	if (fault)
+		fault[1] = prog;
+	if (index_reg < 0)
+		err = emit_atomic_rmw(&prog, BPF_CMPXCHG, dst_reg, AUX_REG,
+				      insn->off, BPF_SIZE(insn->code));
+	else
+		err = emit_atomic_rmw_index(&prog, BPF_CMPXCHG, BPF_SIZE(insn->code),
+					    dst_reg, AUX_REG, index_reg, insn->off);
+	if (WARN_ON(err))
+		return err;
+
+	/* ZF tells us whether we won the race. If it's cleared we need to try again. */
+	EMIT2(X86_JNE, -(prog - branch_target) - 2);
+	/* Return the pre-modification value */
+	emit_mov_reg(&prog, is64, src_reg, BPF_REG_0);
+
+	if (resume)
+		*resume = prog;
+
+	/* Restore R0 after clobbering RAX */
+	emit_mov_reg(&prog, true, BPF_REG_0, BPF_REG_AX);
+
+	*pprog = prog;
+	return 0;
+}
+
 /*
  * Metadata encoding for exception handling in JITed code.
  *
@@ -1693,7 +1766,12 @@ static int emit_atomic_ld_st_index(u8 **pprog, u32 atomic_op, u32 size,
  * | ARENA_ACC | ARENA_WRITE | Unused | ARENA_REG | DST_REG | INSN_LEN |
  * +-----------+-------------+--------+-----------+---------+----------+
  *
- * - INSN_LEN (8 bits): Length of faulting insn (max x86 insn = 15 bytes (fits in 8 bits)).
+ * - INSN_LEN (8 bits): How far past the faulting insn to resume. That is its own length
+ *                      for a single-insn access, but the distance to the end of the whole
+ *                      sequence where one BPF insn became several, as for the CMPXCHG loop
+ *                      of a fetching AND/OR/XOR. A sequence long enough to overflow this
+ *                      field would first have to exceed BPF_MAX_INSN_SIZE, which do_jit()
+ *                      rejects with -EFAULT before the image is used.
  * - DST_REG  (8 bits): Offset of dst_reg from reg2pt_regs[] (max offset = 112 (fits in 8 bits)).
  *                      This is set to DONT_CLEAR if the insn does not read into a register.
  * - ARENA_REG (8 bits): Offset of the register that is used to calculate the
@@ -1748,6 +1826,44 @@ bool ex_handler_bpf(const struct exception_table_entry *x, struct pt_regs *regs)
 	return true;
 }
 
+/*
+ * Record an arena access that may fault. @fault_ip is the address of the
+ * faulting insn in the RO image, @resume_off how far past it execution has to
+ * resume: for a multi-insn lowering that is the end of the whole sequence, not
+ * the end of the one insn.
+ */
+static int emit_arena_exentry(struct bpf_prog *bpf_prog, u8 *image, u8 *rw_image,
+			      int *excnt, u8 *fault_ip, u32 resume_off,
+			      u32 fixup_reg, u32 arena_reg, bool is_write, s16 off)
+{
+	struct exception_table_entry *ex;
+	s64 delta;
+
+	if (!bpf_prog->aux->extable)
+		return 0;
+
+	if (*excnt >= bpf_prog->aux->num_exentries) {
+		pr_err("arena extable bug\n");
+		return -EFAULT;
+	}
+	ex = &bpf_prog->aux->extable[(*excnt)++];
+
+	delta = fault_ip - (u8 *)&ex->insn;
+	/* switch ex to rw buffer for writes */
+	ex = (void *)rw_image + ((void *)ex - (void *)image);
+
+	ex->insn = delta;
+	ex->data = EX_TYPE_BPF | FIELD_PREP(DATA_ARENA_OFFSET_MASK, off);
+	ex->fixup = FIELD_PREP(FIXUP_INSN_LEN_MASK, resume_off) |
+		    FIELD_PREP(FIXUP_ARENA_REG_MASK, arena_reg) |
+		    FIELD_PREP(FIXUP_REG_MASK, fixup_reg) |
+		    FIXUP_ARENA_ACCESS;
+	if (is_write)
+		ex->fixup |= FIXUP_ARENA_WRITE;
+
+	return 0;
+}
+
 static void detect_reg_usage(struct bpf_insn *insn, int insn_cnt,
 			     bool *regs_used)
 {
@@ -2625,28 +2741,8 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 			}
 populate_extable:
 			{
-				struct exception_table_entry *ex;
-				u8 *_insn = image + proglen + (start_of_ldx - temp);
 				u32 arena_reg, fixup_reg;
 				bool is_write;
-				s64 delta;
-
-				if (!bpf_prog->aux->extable)
-					break;
-
-				if (excnt >= bpf_prog->aux->num_exentries) {
-					pr_err("mem32 extable bug\n");
-					return -EFAULT;
-				}
-				ex = &bpf_prog->aux->extable[excnt++];
-
-				delta = _insn - (u8 *)&ex->insn;
-				/* switch ex to rw buffer for writes */
-				ex = (void *)rw_image + ((void *)ex - (void *)image);
-
-				ex->insn = delta;
-
-				ex->data = EX_TYPE_BPF;
 
 				/*
 				 * src_reg/dst_reg holds the address in the arena region with upper
@@ -2682,14 +2778,12 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 					is_write = true;
 				}
 
-				ex->fixup = FIELD_PREP(FIXUP_INSN_LEN_MASK, prog - start_of_ldx) |
-					    FIELD_PREP(FIXUP_ARENA_REG_MASK, arena_reg) |
-					    FIELD_PREP(FIXUP_REG_MASK, fixup_reg);
-				ex->fixup |= FIXUP_ARENA_ACCESS;
-				if (is_write)
-					ex->fixup |= FIXUP_ARENA_WRITE;
-
-				ex->data |= FIELD_PREP(DATA_ARENA_OFFSET_MASK, insn->off);
+				err = emit_arena_exentry(bpf_prog, image, rw_image, &excnt,
+							 image + proglen + (start_of_ldx - temp),
+							 prog - start_of_ldx, fixup_reg,
+							 arena_reg, is_write, insn->off);
+				if (err)
+					return err;
 			}
 			break;
 
@@ -2841,20 +2935,12 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 			fallthrough;
 		case BPF_STX | BPF_ATOMIC | BPF_W:
 		case BPF_STX | BPF_ATOMIC | BPF_DW: {
-			bool is64 = BPF_SIZE(insn->code) == BPF_DW;
 			u32 real_src_reg = src_reg;
 			u32 real_dst_reg = dst_reg;
+			bool is_atomic_fetch = is_atomic_fetch_op(insn);
 			u8 *old_prog;
-			bool is_atomic_fetch =
-				(insn->imm == (BPF_AND | BPF_FETCH) ||
-				 insn->imm == (BPF_OR | BPF_FETCH) ||
-				 insn->imm == (BPF_XOR | BPF_FETCH));
-			if (is_atomic_fetch) {
-				/*
-				 * Can't be implemented with a single x86 insn.
-				 * Need to do a CMPXCHG loop.
-				 */
 
+			if (is_atomic_fetch) {
 				/* Will need RAX as a CMPXCHG operand so save R0 */
 				old_prog = prog;
 				emit_mov_reg(&prog, true, BPF_REG_AX, BPF_REG_0);
@@ -2875,34 +2961,11 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 				}
 			}
 			if (is_atomic_fetch) {
-				u8 *branch_target = prog;
-				/* Load old value */
-				emit_ldx(&prog, BPF_SIZE(insn->code),
-					 BPF_REG_0, real_dst_reg, insn->off);
-				/*
-				 * Perform the (commutative) operation locally,
-				 * put the result in the AUX_REG.
-				 */
-				emit_mov_reg(&prog, is64, AUX_REG, BPF_REG_0);
-				maybe_emit_mod(&prog, AUX_REG, real_src_reg, is64);
-				EMIT2(simple_alu_opcodes[BPF_OP(insn->imm)],
-				      add_2reg(0xC0, AUX_REG, real_src_reg));
-				/* Attempt to swap in new value */
-				err = emit_atomic_rmw(&prog, BPF_CMPXCHG,
-						      real_dst_reg, AUX_REG,
-						      insn->off,
-						      BPF_SIZE(insn->code));
-				if (WARN_ON(err))
+				err = emit_atomic_fetch_rmw(&prog, insn, real_dst_reg,
+							    real_src_reg, -1, NULL,
+							    NULL);
+				if (err)
 					return err;
-				/*
-				 * ZF tells us whether we won the race. If it's
-				 * cleared we need to try again.
-				 */
-				EMIT2(X86_JNE, -(prog - branch_target) - 2);
-				/* Return the pre-modification value */
-				emit_mov_reg(&prog, is64, real_src_reg, BPF_REG_0);
-				/* Restore R0 after clobbering RAX */
-				emit_mov_reg(&prog, true, BPF_REG_0, BPF_REG_AX);
 				break;
 			}
 
@@ -2926,6 +2989,51 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 			fallthrough;
 		case BPF_STX | BPF_PROBE_ATOMIC | BPF_W:
 		case BPF_STX | BPF_PROBE_ATOMIC | BPF_DW:
+			if (is_atomic_fetch_op(insn)) {
+				u32 real_src_reg = src_reg, real_dst_reg = dst_reg;
+				u8 *fault[2], *resume;
+				int j;
+
+				/*
+				 * An access through a number has its address in
+				 * BPF_REG_AX (see arena_scalar_access()), where the
+				 * loop below has to save R0.
+				 */
+				if (dst_reg == BPF_REG_AX)
+					return -EOPNOTSUPP;
+
+				/* Will need RAX as a CMPXCHG operand so save R0 */
+				emit_mov_reg(&prog, true, BPF_REG_AX, BPF_REG_0);
+				if (src_reg == BPF_REG_0)
+					real_src_reg = BPF_REG_AX;
+				if (dst_reg == BPF_REG_0)
+					real_dst_reg = BPF_REG_AX;
+
+				err = emit_atomic_fetch_rmw(&prog, insn, real_dst_reg,
+							    real_src_reg, X86_REG_R12,
+							    fault, &resume);
+				if (err)
+					return err;
+
+				/*
+				 * The page may be unmapped between the load and the
+				 * CMPXCHG, so both need an entry. Both report a write,
+				 * since the BPF insn is a read-modify-write.
+				 */
+				for (j = 0; j < 2; j++) {
+					err = emit_arena_exentry(bpf_prog, image, rw_image,
+								 &excnt,
+								 image + proglen +
+									 (fault[j] - temp),
+								 resume - fault[j],
+								 reg2pt_regs[real_src_reg],
+								 reg2pt_regs[real_dst_reg],
+								 true, insn->off);
+					if (err)
+						return err;
+				}
+				break;
+			}
 			start_of_ldx = prog;
 
 			if (bpf_atomic_is_load_store(insn))
@@ -4353,6 +4461,21 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
 		padding = true;
 		goto skip_init_addrs;
 	}
+
+	/*
+	 * An arena fetching AND/OR/XOR is lowered to a CMPXCHG loop whose load
+	 * and CMPXCHG can both fault, one more entry than the verifier reserved.
+	 * Only reachable on the first pass, so the count is adjusted once.
+	 */
+	for (i = 0; prog->aux->arena && i < prog->len; i++) {
+		const struct bpf_insn *insn = &prog->insnsi[i];
+
+		if (BPF_CLASS(insn->code) == BPF_STX &&
+		    BPF_MODE(insn->code) == BPF_PROBE_ATOMIC &&
+		    is_atomic_fetch_op(insn))
+			prog->aux->num_exentries++;
+	}
+
 	addrs = kvmalloc_objs(*addrs, prog->len + 1);
 	if (!addrs)
 		goto out_addrs;
@@ -4407,7 +4530,9 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
 			 * The number of entries in extable is the number of BPF_LDX
 			 * insns that access kernel memory via "pointer to BTF type".
 			 * The verifier changed their opcode from LDX|MEM|size
-			 * to LDX|PROBE_MEM|size to make JITing easier.
+			 * to LDX|PROBE_MEM|size to make JITing easier. Arena
+			 * accesses add one each, and an arena fetching AND/OR/XOR
+			 * one more, see the adjustment of num_exentries above.
 			 */
 			u32 align = __alignof__(struct exception_table_entry);
 			u32 extable_size = prog->aux->num_exentries *
@@ -4681,16 +4806,6 @@ bool bpf_jit_supports_arena_scalar(void)
 
 bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)
 {
-	if (!in_arena)
-		return true;
-	switch (insn->code) {
-	case BPF_STX | BPF_ATOMIC | BPF_W:
-	case BPF_STX | BPF_ATOMIC | BPF_DW:
-		if (insn->imm == (BPF_AND | BPF_FETCH) ||
-		    insn->imm == (BPF_OR | BPF_FETCH) ||
-		    insn->imm == (BPF_XOR | BPF_FETCH))
-			return false;
-	}
 	return true;
 }
 
diff --git a/tools/sched_ext/include/scx/cid.bpf.h b/tools/sched_ext/include/scx/cid.bpf.h
index 69fb4e97bc771..d0d4cea19e29f 100644
--- a/tools/sched_ext/include/scx/cid.bpf.h
+++ b/tools/sched_ext/include/scx/cid.bpf.h
@@ -3,8 +3,8 @@
  * BPF-side helpers for cids and cmasks. See kernel/sched/ext/cid.h for the
  * authoritative layout and semantics. The BPF-side helpers use the cmask_*
  * naming (no scx_ prefix); cmask is the SCX bitmap type so the prefix is
- * redundant in BPF code. Atomics use __sync_val_compare_and_swap and every
- * helper is inline (no .c counterpart).
+ * redundant in BPF code. Atomics use the __sync_* builtins and every helper
+ * is inline (no .c counterpart).
  *
  * Included by scx/common.bpf.h; don't include directly.
  *
@@ -129,101 +129,38 @@ static __always_inline bool cmask_test(u32 cid, const struct scx_cmask __arena *
 	return *__cmask_word(cid, m) & BIT_U64(cid & 63);
 }
 
-/*
- * x86 BPF JIT rejects BPF_OR | BPF_FETCH and BPF_AND | BPF_FETCH on arena
- * pointers (see bpf_jit_supports_insn() in arch/x86/net/bpf_jit_comp.c). Only
- * BPF_CMPXCHG / BPF_XCHG / BPF_ADD with FETCH are allowed. Implement
- * test_and_{set,clear} and the atomic set/clear via a cmpxchg loop.
- *
- * CMASK_CAS_TRIES is sized so exhausting it means seconds of real spinning
- * on one word - past any plausible contention. Abort hard.
- */
-#define CMASK_CAS_TRIES		(1U << 23)
-
 static __always_inline void cmask_set(u32 cid, struct scx_cmask __arena *m)
 {
-	u64 __arena *w;
-	u64 bit, old, new;
-	u32 i;
-
 	if (!__cmask_contains(cid, m))
 		return;
-	w = __cmask_word(cid, m);
-	bit = BIT_U64(cid & 63);
-	bpf_for(i, 0, CMASK_CAS_TRIES) {
-		old = *w;
-		if (old & bit)
-			return;
-		new = old | bit;
-		if (__sync_val_compare_and_swap(w, old, new) == old)
-			return;
-	}
-	scx_bpf_error("cmask_set CAS exhausted at cid %u", cid);
+	__sync_fetch_and_or(__cmask_word(cid, m), BIT_U64(cid & 63));
 }
 
 static __always_inline void cmask_clear(u32 cid, struct scx_cmask __arena *m)
 {
-	u64 __arena *w;
-	u64 bit, old, new;
-	u32 i;
-
 	if (!__cmask_contains(cid, m))
 		return;
-	w = __cmask_word(cid, m);
-	bit = BIT_U64(cid & 63);
-	bpf_for(i, 0, CMASK_CAS_TRIES) {
-		old = *w;
-		if (!(old & bit))
-			return;
-		new = old & ~bit;
-		if (__sync_val_compare_and_swap(w, old, new) == old)
-			return;
-	}
-	scx_bpf_error("cmask_clear CAS exhausted at cid %u", cid);
+	__sync_fetch_and_and(__cmask_word(cid, m), ~BIT_U64(cid & 63));
 }
 
 static __always_inline bool cmask_test_and_set(u32 cid, struct scx_cmask __arena *m)
 {
-	u64 __arena *w;
-	u64 bit, old, new;
-	u32 i;
+	u64 bit;
 
 	if (!__cmask_contains(cid, m))
 		return false;
-	w = __cmask_word(cid, m);
 	bit = BIT_U64(cid & 63);
-	bpf_for(i, 0, CMASK_CAS_TRIES) {
-		old = *w;
-		if (old & bit)
-			return true;
-		new = old | bit;
-		if (__sync_val_compare_and_swap(w, old, new) == old)
-			return false;
-	}
-	scx_bpf_error("cmask_test_and_set CAS exhausted at cid %u", cid);
-	return false;
+	return __sync_fetch_and_or(__cmask_word(cid, m), bit) & bit;
 }
 
 static __always_inline bool cmask_test_and_clear(u32 cid, struct scx_cmask __arena *m)
 {
-	u64 __arena *w;
-	u64 bit, old, new;
-	u32 i;
+	u64 bit;
 
 	if (!__cmask_contains(cid, m))
 		return false;
-	w = __cmask_word(cid, m);
 	bit = BIT_U64(cid & 63);
-	bpf_for(i, 0, CMASK_CAS_TRIES) {
-		old = *w;
-		if (!(old & bit))
-			return false;
-		new = old & ~bit;
-		if (__sync_val_compare_and_swap(w, old, new) == old)
-			return true;
-	}
-	scx_bpf_error("cmask_test_and_clear CAS exhausted at cid %u", cid);
-	return false;
+	return __sync_fetch_and_and(__cmask_word(cid, m), ~bit) & bit;
 }
 
 static __always_inline void __cmask_set(u32 cid, struct scx_cmask __arena *m)
diff --git a/tools/testing/selftests/bpf/prog_tests/arena_atomics.c b/tools/testing/selftests/bpf/prog_tests/arena_atomics.c
index 1ad5d03d07adb..81f6e554a8c12 100644
--- a/tools/testing/selftests/bpf/prog_tests/arena_atomics.c
+++ b/tools/testing/selftests/bpf/prog_tests/arena_atomics.c
@@ -68,6 +68,12 @@ static void test_and(struct arena_atomics *skel)
 
 	ASSERT_EQ(skel->arena->and64_value, 0x010ull << 32, "and64_value");
 	ASSERT_EQ(skel->arena->and32_value, 0x010, "and32_value");
+
+	ASSERT_EQ(skel->arena->and64_result, 0x110ull << 32, "and64_result");
+	ASSERT_EQ(skel->arena->and32_result, 0x110, "and32_result");
+
+	ASSERT_EQ(skel->arena->and64_noreturn_value, 0x010ull << 32, "and64_noreturn_value");
+	ASSERT_EQ(skel->arena->and32_noreturn_value, 0x010, "and32_noreturn_value");
 }
 
 static void test_or(struct arena_atomics *skel)
@@ -85,6 +91,12 @@ static void test_or(struct arena_atomics *skel)
 
 	ASSERT_EQ(skel->arena->or64_value, 0x111ull << 32, "or64_value");
 	ASSERT_EQ(skel->arena->or32_value, 0x111, "or32_value");
+
+	ASSERT_EQ(skel->arena->or64_result, 0x110ull << 32, "or64_result");
+	ASSERT_EQ(skel->arena->or32_result, 0x110, "or32_result");
+
+	ASSERT_EQ(skel->arena->or64_noreturn_value, 0x111ull << 32, "or64_noreturn_value");
+	ASSERT_EQ(skel->arena->or32_noreturn_value, 0x111, "or32_noreturn_value");
 }
 
 static void test_xor(struct arena_atomics *skel)
@@ -102,6 +114,12 @@ static void test_xor(struct arena_atomics *skel)
 
 	ASSERT_EQ(skel->arena->xor64_value, 0x101ull << 32, "xor64_value");
 	ASSERT_EQ(skel->arena->xor32_value, 0x101, "xor32_value");
+
+	ASSERT_EQ(skel->arena->xor64_result, 0x110ull << 32, "xor64_result");
+	ASSERT_EQ(skel->arena->xor32_result, 0x110, "xor32_result");
+
+	ASSERT_EQ(skel->arena->xor64_noreturn_value, 0x101ull << 32, "xor64_noreturn_value");
+	ASSERT_EQ(skel->arena->xor32_noreturn_value, 0x101, "xor32_noreturn_value");
 }
 
 static void test_cmpxchg(struct arena_atomics *skel)
@@ -146,6 +164,27 @@ static void test_xchg(struct arena_atomics *skel)
 	ASSERT_EQ(skel->arena->xchg32_result, 1, "xchg32_result");
 }
 
+static void test_fetch_r0(struct arena_atomics *skel)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, topts);
+	int err, prog_fd;
+
+	/* No need to attach it, just run it directly */
+	prog_fd = bpf_program__fd(skel->progs.fetch_r0);
+	err = bpf_prog_test_run_opts(prog_fd, &topts);
+	if (!ASSERT_OK(err, "test_run_opts err"))
+		return;
+	if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
+		return;
+
+	ASSERT_EQ(skel->arena->fetch_src_r0_value, 0x11100000111ull, "fetch_src_r0_value");
+	ASSERT_EQ(skel->arena->fetch_src_r0_result, 0x11000000110ull, "fetch_src_r0_result");
+
+	ASSERT_EQ(skel->arena->fetch_dst_r0_value, 0x11100000111ull, "fetch_dst_r0_value");
+	ASSERT_EQ(skel->arena->fetch_dst_r0_result, 0x11000000110ull, "fetch_dst_r0_result");
+	ASSERT_EQ(skel->arena->fetch_dst_r0_readback, 0x11100000111ull, "fetch_dst_r0_readback");
+}
+
 static void test_uaf(struct arena_atomics *skel)
 {
 	LIBBPF_OPTS(bpf_test_run_opts, topts);
@@ -160,6 +199,12 @@ static void test_uaf(struct arena_atomics *skel)
 		return;
 
 	ASSERT_EQ(skel->arena->uaf_recovery_fails, 0, "uaf_recovery_fails");
+
+	/* A fault clears the fetched register and leaves the address alone */
+	ASSERT_EQ(skel->arena->uaf_r1_after_dst_r0, 0, "uaf_r1_after_dst_r0");
+	ASSERT_EQ(skel->arena->uaf_r0_after_dst_r0, skel->arena->uaf_r0_before_dst_r0,
+		  "uaf_r0_after_dst_r0");
+	ASSERT_EQ(skel->arena->uaf_r0_after_src_r0, 0, "uaf_r0_after_src_r0");
 }
 
 static void test_load_acquire(struct arena_atomics *skel)
@@ -256,6 +301,8 @@ void serial_test_arena_atomics(void)
 		test_cmpxchg(skel);
 	if (test__start_subtest("xchg"))
 		test_xchg(skel);
+	if (test__start_subtest("fetch_r0"))
+		test_fetch_r0(skel);
 	if (test__start_subtest("uaf"))
 		test_uaf(skel);
 	if (test__start_subtest("load_acquire"))
diff --git a/tools/testing/selftests/bpf/progs/arena_atomics.c b/tools/testing/selftests/bpf/progs/arena_atomics.c
index 73bc2b835f3fe..e89b468fc6226 100644
--- a/tools/testing/selftests/bpf/progs/arena_atomics.c
+++ b/tools/testing/selftests/bpf/progs/arena_atomics.c
@@ -91,13 +91,17 @@ int sub(const void *ctx)
 	return 0;
 }
 
-#ifdef __BPF_FEATURE_ATOMIC_MEM_ORDERING
-_Atomic __u64 __arena_global and64_value = (0x110ull << 32);
-_Atomic __u32 __arena_global and32_value = 0x110;
-#else
+/*
+ * A relaxed __c11_atomic_fetch_*() whose result is discarded is what selects
+ * the non-fetching insn, so each operation keeps a *_noreturn_value of each
+ * width to cover that lowering as well.
+ */
 __u64 __arena_global and64_value = (0x110ull << 32);
+__u64 __arena_global and64_result = 0;
+_Atomic __u64 __arena_global and64_noreturn_value = (0x110ull << 32);
 __u32 __arena_global and32_value = 0x110;
-#endif
+__u32 __arena_global and32_result = 0;
+_Atomic __u32 __arena_global and32_noreturn_value = 0x110;
 
 SEC("raw_tp/sys_enter")
 int and(const void *ctx)
@@ -105,25 +109,22 @@ int and(const void *ctx)
 	if (pid != (bpf_get_current_pid_tgid() >> 32))
 		return 0;
 #ifdef ENABLE_ATOMICS_TESTS
-#ifdef __BPF_FEATURE_ATOMIC_MEM_ORDERING
-	__c11_atomic_fetch_and(&and64_value, 0x011ull << 32, memory_order_relaxed);
-	__c11_atomic_fetch_and(&and32_value, 0x011, memory_order_relaxed);
-#else
-	__sync_fetch_and_and(&and64_value, 0x011ull << 32);
-	__sync_fetch_and_and(&and32_value, 0x011);
-#endif
+	and64_result = __sync_fetch_and_and(&and64_value, 0x011ull << 32);
+	and32_result = __sync_fetch_and_and(&and32_value, 0x011);
+	__c11_atomic_fetch_and(&and64_noreturn_value, 0x011ull << 32, memory_order_relaxed);
+	__c11_atomic_fetch_and(&and32_noreturn_value, 0x011, memory_order_relaxed);
 #endif
 
 	return 0;
 }
 
-#ifdef __BPF_FEATURE_ATOMIC_MEM_ORDERING
-_Atomic __u32 __arena_global or32_value = 0x110;
-_Atomic __u64 __arena_global or64_value = (0x110ull << 32);
-#else
+/* u32s first: .addr_space.1 must not have holes, see the clang-17 note below */
 __u32 __arena_global or32_value = 0x110;
+__u32 __arena_global or32_result = 0;
+_Atomic __u32 __arena_global or32_noreturn_value = 0x110;
 __u64 __arena_global or64_value = (0x110ull << 32);
-#endif
+__u64 __arena_global or64_result = 0;
+_Atomic __u64 __arena_global or64_noreturn_value = (0x110ull << 32);
 
 SEC("raw_tp/sys_enter")
 int or(const void *ctx)
@@ -131,25 +132,21 @@ int or(const void *ctx)
 	if (pid != (bpf_get_current_pid_tgid() >> 32))
 		return 0;
 #ifdef ENABLE_ATOMICS_TESTS
-#ifdef __BPF_FEATURE_ATOMIC_MEM_ORDERING
-	__c11_atomic_fetch_or(&or64_value, 0x011ull << 32, memory_order_relaxed);
-	__c11_atomic_fetch_or(&or32_value, 0x011, memory_order_relaxed);
-#else
-	__sync_fetch_and_or(&or64_value, 0x011ull << 32);
-	__sync_fetch_and_or(&or32_value, 0x011);
-#endif
+	or64_result = __sync_fetch_and_or(&or64_value, 0x011ull << 32);
+	or32_result = __sync_fetch_and_or(&or32_value, 0x011);
+	__c11_atomic_fetch_or(&or64_noreturn_value, 0x011ull << 32, memory_order_relaxed);
+	__c11_atomic_fetch_or(&or32_noreturn_value, 0x011, memory_order_relaxed);
 #endif
 
 	return 0;
 }
 
-#ifdef __BPF_FEATURE_ATOMIC_MEM_ORDERING
-_Atomic __u64 __arena_global xor64_value = (0x110ull << 32);
-_Atomic __u32 __arena_global xor32_value = 0x110;
-#else
 __u64 __arena_global xor64_value = (0x110ull << 32);
+__u64 __arena_global xor64_result = 0;
+_Atomic __u64 __arena_global xor64_noreturn_value = (0x110ull << 32);
 __u32 __arena_global xor32_value = 0x110;
-#endif
+__u32 __arena_global xor32_result = 0;
+_Atomic __u32 __arena_global xor32_noreturn_value = 0x110;
 
 SEC("raw_tp/sys_enter")
 int xor(const void *ctx)
@@ -157,13 +154,10 @@ int xor(const void *ctx)
 	if (pid != (bpf_get_current_pid_tgid() >> 32))
 		return 0;
 #ifdef ENABLE_ATOMICS_TESTS
-#ifdef __BPF_FEATURE_ATOMIC_MEM_ORDERING
-	__c11_atomic_fetch_xor(&xor64_value, 0x011ull << 32, memory_order_relaxed);
-	__c11_atomic_fetch_xor(&xor32_value, 0x011, memory_order_relaxed);
-#else
-	__sync_fetch_and_xor(&xor64_value, 0x011ull << 32);
-	__sync_fetch_and_xor(&xor32_value, 0x011);
-#endif
+	xor64_result = __sync_fetch_and_xor(&xor64_value, 0x011ull << 32);
+	xor32_result = __sync_fetch_and_xor(&xor32_value, 0x011);
+	__c11_atomic_fetch_xor(&xor64_noreturn_value, 0x011ull << 32, memory_order_relaxed);
+	__c11_atomic_fetch_xor(&xor32_noreturn_value, 0x011, memory_order_relaxed);
 #endif
 
 	return 0;
@@ -213,23 +207,93 @@ int xchg(const void *ctx)
 	return 0;
 }
 
+__u64 __arena_global fetch_src_r0_value = 0x11000000110ull;
+__u64 __arena_global fetch_src_r0_result = 0;
+__u64 __arena_global fetch_dst_r0_value = 0x11000000110ull;
+__u64 __arena_global fetch_dst_r0_result = 0;
+__u64 __arena_global fetch_dst_r0_readback = 0;
+
+/*
+ * A fetching OR with the operand in r0, and one with the arena pointer in r0.
+ * The x86 JIT needs RAX for its CMPXCHG loop and substitutes BPF_REG_AX for
+ * whichever of the two is r0, so both have to keep working. Hand-written
+ * because clang picks its own registers and will not reliably emit either.
+ */
+SEC("raw_tp/sys_enter")
+int fetch_r0(const void *ctx)
+{
+	if (pid != (bpf_get_current_pid_tgid() >> 32))
+		return 0;
+#if defined(ENABLE_ATOMICS_TESTS) && defined(__BPF_FEATURE_ADDR_SPACE_CAST)
+	asm volatile (
+	"r1 = %[fetch_src_r0_value] ll;"
+	"r1 = addr_space_cast(r1, 0x0, 0x1);"
+	"r0 = 0x1100000011 ll;"
+	".8byte %[fetch_src_r0_insn];"
+	"r2 = %[fetch_src_r0_result] ll;"
+	"r2 = addr_space_cast(r2, 0x0, 0x1);"
+	"*(u64 *)(r2 + 0) = r0;"
+	:
+	: __imm_addr(fetch_src_r0_value),
+	  __imm_insn(fetch_src_r0_insn,
+		     BPF_ATOMIC_OP(BPF_DW, BPF_OR | BPF_FETCH, BPF_REG_1, BPF_REG_0, 0)),
+	  __imm_addr(fetch_src_r0_result)
+	: __clobber_all);
+
+	asm volatile (
+	"r0 = %[fetch_dst_r0_value] ll;"
+	"r0 = addr_space_cast(r0, 0x0, 0x1);"
+	"r1 = 0x1100000011 ll;"
+	".8byte %[fetch_dst_r0_insn];"
+	"r2 = %[fetch_dst_r0_result] ll;"
+	"r2 = addr_space_cast(r2, 0x0, 0x1);"
+	"*(u64 *)(r2 + 0) = r1;"
+	/* r0 is only read by the atomic, so it must still be the pointer. */
+	"r3 = *(u64 *)(r0 + 0);"
+	"r2 = %[fetch_dst_r0_readback] ll;"
+	"r2 = addr_space_cast(r2, 0x0, 0x1);"
+	"*(u64 *)(r2 + 0) = r3;"
+	:
+	: __imm_addr(fetch_dst_r0_value),
+	  __imm_insn(fetch_dst_r0_insn,
+		     BPF_ATOMIC_OP(BPF_DW, BPF_OR | BPF_FETCH, BPF_REG_0, BPF_REG_1, 0)),
+	  __imm_addr(fetch_dst_r0_result),
+	  __imm_addr(fetch_dst_r0_readback)
+	: __clobber_all);
+#endif
+
+	return 0;
+}
+
 __u64 __arena_global uaf_sink;
 volatile __u64 __arena_global uaf_recovery_fails;
+/* Register state after the r0 forms below fault, checked by the test */
+__u64 __arena_global uaf_r1_after_dst_r0;
+__u64 __arena_global uaf_r0_before_dst_r0;
+__u64 __arena_global uaf_r0_after_dst_r0;
+__u64 __arena_global uaf_r0_after_src_r0;
 
 SEC("syscall")
 int uaf(const void *ctx)
 {
 	if (pid != (bpf_get_current_pid_tgid() >> 32))
 		return 0;
+/*
+ * __BPF_FEATURE_ADDR_SPACE_CAST is required by the asm below, which spells
+ * addr_space_cast textually. That and the mnemonic both arrived in LLVM 19.
+ */
 #if defined(ENABLE_ATOMICS_TESTS) && !defined(__TARGET_ARCH_arm64) && \
-    !defined(__TARGET_ARCH_x86)
+	defined(__BPF_FEATURE_ADDR_SPACE_CAST)
 	__u32 __arena *page32;
 	__u64 __arena *page64;
 	void __arena *page;
 
 	page = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
 	bpf_arena_free_pages(&arena, page, 1);
-	uaf_recovery_fails = 24;
+	uaf_recovery_fails = 26;
+	uaf_r1_after_dst_r0 = 1;
+	uaf_r0_after_dst_r0 = 1;
+	uaf_r0_after_src_r0 = 1;
 
 	page32 = (__u32 __arena *)page;
 	uaf_sink += __sync_fetch_and_add(page32, 1);
@@ -282,6 +346,50 @@ int uaf(const void *ctx)
 	uaf_recovery_fails -= 1;
 	uaf_sink += __sync_lock_test_and_set(page64, 1);
 	uaf_recovery_fails -= 1;
+
+	/*
+	 * The x86 JIT needs RAX for the CMPXCHG loop it lowers a fetching
+	 * AND/OR/XOR into, and substitutes BPF_REG_AX for r0 when r0 is either
+	 * operand. Only then does the fault fixup have to name that register,
+	 * so spell both forms out: clang picks its own and never produces them.
+	 */
+	asm volatile (
+	"r0 = %[page];"
+	"r0 = addr_space_cast(r0, 0x0, 0x1);"
+	"r1 = 1;"
+	"r2 = %[uaf_r0_before_dst_r0] ll;"
+	"r2 = addr_space_cast(r2, 0x0, 0x1);"
+	"*(u64 *)(r2 + 0) = r0;"
+	".8byte %[fetch_dst_r0];"
+	"r2 = %[uaf_r0_after_dst_r0] ll;"
+	"r2 = addr_space_cast(r2, 0x0, 0x1);"
+	"*(u64 *)(r2 + 0) = r0;"
+	"r2 = %[uaf_r1_after_dst_r0] ll;"
+	"r2 = addr_space_cast(r2, 0x0, 0x1);"
+	"*(u64 *)(r2 + 0) = r1;"
+	: : [page]"r"(page64),
+	    __imm_insn(fetch_dst_r0,
+		       BPF_ATOMIC_OP(BPF_DW, BPF_OR | BPF_FETCH, BPF_REG_0, BPF_REG_1, 0)),
+	    __imm_addr(uaf_r0_before_dst_r0),
+	    __imm_addr(uaf_r0_after_dst_r0),
+	    __imm_addr(uaf_r1_after_dst_r0)
+	: "r0", "r1", "r2", "memory");
+	uaf_recovery_fails -= 1;
+
+	asm volatile (
+	"r1 = %[page];"
+	"r1 = addr_space_cast(r1, 0x0, 0x1);"
+	"r0 = 1;"
+	".8byte %[fetch_src_r0];"
+	"r2 = %[uaf_r0_after_src_r0] ll;"
+	"r2 = addr_space_cast(r2, 0x0, 0x1);"
+	"*(u64 *)(r2 + 0) = r0;"
+	: : [page]"r"(page64),
+	    __imm_insn(fetch_src_r0,
+		       BPF_ATOMIC_OP(BPF_DW, BPF_OR | BPF_FETCH, BPF_REG_1, BPF_REG_0, 0)),
+	    __imm_addr(uaf_r0_after_src_r0)
+	: "r0", "r1", "r2", "memory");
+	uaf_recovery_fails -= 1;
 #endif
 
 	return 0;
diff --git a/tools/testing/selftests/bpf/progs/verifier_arena_scalar.c b/tools/testing/selftests/bpf/progs/verifier_arena_scalar.c
index bebc37f501b7e..7a7e06d9596ce 100644
--- a/tools/testing/selftests/bpf/progs/verifier_arena_scalar.c
+++ b/tools/testing/selftests/bpf/progs/verifier_arena_scalar.c
@@ -256,6 +256,104 @@ __naked void atomics(void)
 	: __clobber_all);
 }
 
+SEC("syscall")
+__flag(BPF_F_ARENA_SCALAR) __arch_arm64
+__description("arena_scalar: fetching and/or/xor")
+__success __retval(0)
+__load_if_JITed()
+__naked void fetch_and_or_xor(void)
+{
+	asm volatile ("					\
+	r1 = %[arena] ll;				\
+	r2 = 0;						\
+	r3 = 1;						\
+	r4 = -1;					\
+	r5 = 0;						\
+	call %[bpf_arena_alloc_pages];			\
+	r6 = r0;					\
+	r0 = 100;					\
+	if r6 == 0 goto 9f;				\
+	*(u64 *)(r6 + 8) = 0x110;			\
+	r1 = 0x011;					\
+	.8byte %[fetch_or_insn]; /* r1 = atomic_fetch_or((u64 *)(r6 + 8), r1) */ \
+	r0 = 1;						\
+	if r1 != 0x110 goto 9f;				\
+	r1 = 0x101;					\
+	.8byte %[fetch_and_insn]; /* r1 = atomic_fetch_and((u64 *)(r6 + 8), r1) */ \
+	r0 = 2;						\
+	if r1 != 0x111 goto 9f;				\
+	r1 = 0x011;					\
+	.8byte %[fetch_xor_insn]; /* r1 = atomic_fetch_xor((u64 *)(r6 + 8), r1) */ \
+	r0 = 3;						\
+	if r1 != 0x101 goto 9f;				\
+	r2 = *(u64 *)(r6 + 8);				\
+	r0 = 4;						\
+	if r2 != 0x110 goto 9f;				\
+	r0 = 0;						\
+9:	exit;						\
+"	:
+	: __imm_addr(arena),
+	  __imm(bpf_arena_alloc_pages),
+	  __imm_insn(fetch_or_insn, BPF_ATOMIC_OP(BPF_DW, BPF_OR | BPF_FETCH,
+						  BPF_REG_6, BPF_REG_1, 8)),
+	  __imm_insn(fetch_and_insn, BPF_ATOMIC_OP(BPF_DW, BPF_AND | BPF_FETCH,
+						   BPF_REG_6, BPF_REG_1, 8)),
+	  __imm_insn(fetch_xor_insn, BPF_ATOMIC_OP(BPF_DW, BPF_XOR | BPF_FETCH,
+						   BPF_REG_6, BPF_REG_1, 8))
+	: __clobber_all);
+}
+
+/*
+ * The x86-64 JIT saves R0 in BPF_REG_AX to lower these, which is where an
+ * access through a number keeps its address, so it refuses them.
+ */
+SEC("syscall")
+__flag(BPF_F_ARENA_SCALAR) __arch_x86_64
+__description("arena_scalar: fetching and/or/xor refused on x86-64")
+__failure
+__load_if_JITed()
+__naked void fetch_and_or_xor_x86(void)
+{
+	asm volatile ("					\
+	r1 = %[arena] ll;				\
+	r2 = 0;						\
+	r3 = 1;						\
+	r4 = -1;					\
+	r5 = 0;						\
+	call %[bpf_arena_alloc_pages];			\
+	r6 = r0;					\
+	r0 = 100;					\
+	if r6 == 0 goto 9f;				\
+	*(u64 *)(r6 + 8) = 0x110;			\
+	r1 = 0x011;					\
+	.8byte %[fetch_or_insn]; /* r1 = atomic_fetch_or((u64 *)(r6 + 8), r1) */ \
+	r0 = 1;						\
+	if r1 != 0x110 goto 9f;				\
+	r1 = 0x101;					\
+	.8byte %[fetch_and_insn]; /* r1 = atomic_fetch_and((u64 *)(r6 + 8), r1) */ \
+	r0 = 2;						\
+	if r1 != 0x111 goto 9f;				\
+	r1 = 0x011;					\
+	.8byte %[fetch_xor_insn]; /* r1 = atomic_fetch_xor((u64 *)(r6 + 8), r1) */ \
+	r0 = 3;						\
+	if r1 != 0x101 goto 9f;				\
+	r2 = *(u64 *)(r6 + 8);				\
+	r0 = 4;						\
+	if r2 != 0x110 goto 9f;				\
+	r0 = 0;						\
+9:	exit;						\
+"	:
+	: __imm_addr(arena),
+	  __imm(bpf_arena_alloc_pages),
+	  __imm_insn(fetch_or_insn, BPF_ATOMIC_OP(BPF_DW, BPF_OR | BPF_FETCH,
+						  BPF_REG_6, BPF_REG_1, 8)),
+	  __imm_insn(fetch_and_insn, BPF_ATOMIC_OP(BPF_DW, BPF_AND | BPF_FETCH,
+						   BPF_REG_6, BPF_REG_1, 8)),
+	  __imm_insn(fetch_xor_insn, BPF_ATOMIC_OP(BPF_DW, BPF_XOR | BPF_FETCH,
+						   BPF_REG_6, BPF_REG_1, 8))
+	: __clobber_all);
+}
+
 SEC("syscall")
 __arena_scalar
 __description("arena_scalar: cmpxchg through r0")

3/2 2026/10/14 19:06 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 17959
							output: 61
							thoughts: 811
						

Thoughts:
**BPF JIT Patch Analysis: WorthFuzzing Evaluation**

Okay, let's break down this patch. It's targeting amd64, and the commit message "syz-cluster: applied patch under review" suggests it's been through some review, but that doesn't tell us much about its quality. The primary file of interest is `arch/x86/net/bpf_jit_comp.c`. That's the x86-64 BPF JIT compiler! This is core functionality. The diff tells me the patch implements CMPXCHG loops for atomics AND/OR/XOR operations (both arena and non-arena) and updates the exception handling. Specifically, it's enabling atomic fetch operations on BPF arena pointers – something that was previously rejected.

Crucially, the JIT compiler generates x86 code and sets up exception table entries. It's handling fault IP, resume IP, all that good stuff. This is significant because BPF programs are loaded via the `bpf()` syscall and then *JIT-compiled*. The patch touches the core of that process by enabling the JIT compilation of arena atomic operations.

Now, considering the provided instructions, REACHABILITY IS THE PRIMARY GATE. If code is reachable and executable, we need to consider fuzzing. The instructions explicitly state that modifications in "BPF" and "core networking" constitute reachable executable code, and we are working with the BPF JIT. The target architecture is amd64, meaning this code is definitively reachable in a standard environment. Therefore, `WorthFuzzing` is unequivocally `true`. This patch is absolutely in the target area for fuzzing.

My next step is to investigate the Kconfig options needed to enable BPF, BPF_JIT, and BPF arena functionality. I need to ensure that the code path is enabled in the configuration before proceeding. I'll also be examining the entrypoints and any new symbols that have been modified to pinpoint focus symbols. I will use `grepper` or `read-file` to get the necessary information.



4/2 2026/10/14 19:06 tool grepper
0m Args:
{
  "Expression": "bpf_int_jit_compile|emit_atomic_fetch_rmw|emit_arena_exentry",
  "PathPrefixes": [
    "arch/x86/net/bpf_jit_comp.c"
  ]
}

Results:
{
  "Output": "arch/x86/net/bpf_jit_comp.c=1668=static int emit_atomic_ld_st_index(u8 **pprog, u32 atomic_op, u32 size,\n--\narch/x86/net/bpf_jit_comp.c-1700- */\narch/x86/net/bpf_jit_comp.c:1701:static int emit_atomic_fetch_rmw(u8 **pprog, struct bpf_insn *insn, u32 dst_reg,\narch/x86/net/bpf_jit_comp.c-1702-\t\t\t\t u32 src_reg, int index_reg, u8 **fault,\n--\narch/x86/net/bpf_jit_comp.c=1804=bool ex_handler_bpf(const struct exception_table_entry *x, struct pt_regs *regs)\n--\narch/x86/net/bpf_jit_comp.c-1834- */\narch/x86/net/bpf_jit_comp.c:1835:static int emit_arena_exentry(struct bpf_prog *bpf_prog, u8 *image, u8 *rw_image,\narch/x86/net/bpf_jit_comp.c-1836-\t\t\t      int *excnt, u8 *fault_ip, u32 resume_off,\n--\narch/x86/net/bpf_jit_comp.c=2119=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-2780-\narch/x86/net/bpf_jit_comp.c:2781:\t\t\t\terr = emit_arena_exentry(bpf_prog, image, rw_image, \u0026excnt,\narch/x86/net/bpf_jit_comp.c-2782-\t\t\t\t\t\t\t image + proglen + (start_of_ldx - temp),\n--\narch/x86/net/bpf_jit_comp.c-2963-\t\t\tif (is_atomic_fetch) {\narch/x86/net/bpf_jit_comp.c:2964:\t\t\t\terr = emit_atomic_fetch_rmw(\u0026prog, insn, real_dst_reg,\narch/x86/net/bpf_jit_comp.c-2965-\t\t\t\t\t\t\t    real_src_reg, -1, NULL,\n--\narch/x86/net/bpf_jit_comp.c-3011-\narch/x86/net/bpf_jit_comp.c:3012:\t\t\t\terr = emit_atomic_fetch_rmw(\u0026prog, insn, real_dst_reg,\narch/x86/net/bpf_jit_comp.c-3013-\t\t\t\t\t\t\t    real_src_reg, X86_REG_R12,\n--\narch/x86/net/bpf_jit_comp.c-3023-\t\t\t\tfor (j = 0; j \u003c 2; j++) {\narch/x86/net/bpf_jit_comp.c:3024:\t\t\t\t\terr = emit_arena_exentry(bpf_prog, image, rw_image,\narch/x86/net/bpf_jit_comp.c-3025-\t\t\t\t\t\t\t\t \u0026excnt,\n--\narch/x86/net/bpf_jit_comp.c=4398=struct x64_jit_data {\n--\narch/x86/net/bpf_jit_comp.c-4409-\narch/x86/net/bpf_jit_comp.c:4410:struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog)\narch/x86/net/bpf_jit_comp.c-4411-{\n"
}

5/2 2026/10/14 19:06 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 20290
							output: 53
							thoughts: 55
						

Thoughts:
Okay, let's dive into this. I need to get a handle on the broader context here. Given I'm looking at `arch/x86/net/bpf_jit_comp.c`, my initial hunch is that we're dealing with BPF JIT compilation on x86, probably for network packet filtering or something similar. I need to expand my perspective.

**Contextualizing Further: Exploring the Landscape**

I think it's important to look at other symbols defined within this file to understand the role this specific function plays. Right now, I should look at `bpf_jit_supports_insn` to see if that gives me any more information. I need to understand whether this is a function that checks for the x86-specific architecture features or if it is doing something more.

I should also check `ex_handler_bpf` to see what kind of exception handling mechanisms are being utilized. My initial thoughts would be that this might relate to error conditions encountered during the JIT compilation process or the execution of the compiled BPF code.

Let me take another look...



6/2 2026/10/14 19:06 tool grepper
0m Args:
{
  "Expression": "bpf_jit_supports_insn|ex_handler_bpf",
  "PathPrefixes": [
    "arch/x86/net/bpf_jit_comp.c"
  ]
}

Results:
{
  "Output": "arch/x86/net/bpf_jit_comp.c=1701=static int emit_atomic_fetch_rmw(u8 **pprog, struct bpf_insn *insn, u32 dst_reg,\n--\narch/x86/net/bpf_jit_comp.c-1803-\narch/x86/net/bpf_jit_comp.c:1804:bool ex_handler_bpf(const struct exception_table_entry *x, struct pt_regs *regs)\narch/x86/net/bpf_jit_comp.c-1805-{\n--\narch/x86/net/bpf_jit_comp.c=2119=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-2753-\t\t\t\t * the reg holding the unmodified 32-bit address to\narch/x86/net/bpf_jit_comp.c:2754:\t\t\t\t * ex_handler_bpf().\narch/x86/net/bpf_jit_comp.c-2755-\t\t\t\t *\n--\narch/x86/net/bpf_jit_comp.c-2917-\t\t\t\t * Compute size of x86 insn and its target dest x86 register.\narch/x86/net/bpf_jit_comp.c:2918:\t\t\t\t * ex_handler_bpf() will use lower 8 bits to adjust\narch/x86/net/bpf_jit_comp.c-2919-\t\t\t\t * pt_regs-\u003eip to jump over this x86 instruction\n--\narch/x86/net/bpf_jit_comp.c=4802=bool bpf_jit_supports_arena_scalar(void)\n--\narch/x86/net/bpf_jit_comp.c-4806-\narch/x86/net/bpf_jit_comp.c:4807:bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)\narch/x86/net/bpf_jit_comp.c-4808-{\n"
}

7/2 2026/10/14 19:06 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 21214
							output: 45
							thoughts: 276
						

Thoughts:
**Analyzing BPF JIT Compilation Flow**

Okay, let's dive into this BPF JIT compilation flow. My initial thought is to trace the call paths of `bpf_jit_supports_insn` and `bpf_int_jit_compile`, and to understand if `emit_atomic_fetch_rmw`, `emit_arena_exentry`, or `do_jit` are inlined or not. Immediately, I see that `emit_atomic_fetch_rmw` and `emit_arena_exentry` are declared as static functions within `bpf_jit_comp.c`. That narrows things down a bit. Now, I should confirm whether `do_jit` is also static, and it looks like it is: `static int do_jit`. Okay, what about `bpf_int_jit_compile` and `ex_handler_bpf`? They are *not* static, as their signatures show (`struct bpf_prog *bpf_int_jit_compile(...)` and `bool ex_handler_bpf(...)`). And the same is true for `bpf_jit_supports_insn` which is also non-static.

Next, I need to see how `ex_handler_bpf` is used. I'll focus on `arch/x86/net/bpf_jit_comp.c`, around line 1804. I'll need to read around that area, from about line 1770 to 1835, to get the full context.



8/2 2026/10/14 19:06 tool read-file
0m Args:
{
  "File": "arch/x86/net/bpf_jit_comp.c",
  "FirstLine": 1770,
  "LineCount": 60
}

Results:
{
  "Contents": "1770:\t *                      for a single-insn access, but the distance to the end of the whole\n1771:\t *                      sequence where one BPF insn became several, as for the CMPXCHG loop\n1772:\t *                      of a fetching AND/OR/XOR. A sequence long enough to overflow this\n1773:\t *                      field would first have to exceed BPF_MAX_INSN_SIZE, which do_jit()\n1774:\t *                      rejects with -EFAULT before the image is used.\n1775:\t * - DST_REG  (8 bits): Offset of dst_reg from reg2pt_regs[] (max offset = 112 (fits in 8 bits)).\n1776:\t *                      This is set to DONT_CLEAR if the insn does not read into a register.\n1777:\t * - ARENA_REG (8 bits): Offset of the register that is used to calculate the\n1778:\t *                       address for load/store when accessing the arena region.\n1779:\t * - ARENA_WRITE (1 bit): This bit is set when the faulting instruction wrote to the arena region.\n1780:\t *                        It is independent of DST_REG, since a read-modify-write both writes to\n1781:\t *                        memory and reads the old value into a register.\n1782:\t * - ARENA_ACCESS (1 bit): This bit is set when the faulting instruction accessed the arena region.\n1783:\t *\n1784:\t * Bit layout of `data` (32-bit):\n1785:\t *\n1786:\t * +--------------+--------+--------------+\n1787:\t * |\t31-16\t  |  15-8  |     7-0      |\n1788:\t * |              |\t   |              |\n1789:\t * | ARENA_OFFSET | Unused |  EX_TYPE_BPF |\n1790:\t * +--------------+--------+--------------+\n1791:\t *\n1792:\t * - ARENA_OFFSET (16 bits): Offset used to calculate the address for load/store when\n1793:\t *                           accessing the arena region.\n1794:\t */\n1795:\t\n1796:\t#define DONT_CLEAR 1\n1797:\t#define FIXUP_INSN_LEN_MASK\tGENMASK(7, 0)\n1798:\t#define FIXUP_REG_MASK\t\tGENMASK(15, 8)\n1799:\t#define FIXUP_ARENA_REG_MASK\tGENMASK(23, 16)\n1800:\t#define FIXUP_ARENA_WRITE\tBIT(30)\n1801:\t#define FIXUP_ARENA_ACCESS\tBIT(31)\n1802:\t#define DATA_ARENA_OFFSET_MASK\tGENMASK(31, 16)\n1803:\t\n1804:\tbool ex_handler_bpf(const struct exception_table_entry *x, struct pt_regs *regs)\n1805:\t{\n1806:\t\tu32 reg = FIELD_GET(FIXUP_REG_MASK, x-\u003efixup);\n1807:\t\tu32 insn_len = FIELD_GET(FIXUP_INSN_LEN_MASK, x-\u003efixup);\n1808:\t\tbool is_arena = !!(x-\u003efixup \u0026 FIXUP_ARENA_ACCESS);\n1809:\t\tbool is_write = !!(x-\u003efixup \u0026 FIXUP_ARENA_WRITE);\n1810:\t\tunsigned long addr;\n1811:\t\ts16 off;\n1812:\t\tu32 arena_reg;\n1813:\t\n1814:\t\tif (is_arena) {\n1815:\t\t\tarena_reg = FIELD_GET(FIXUP_ARENA_REG_MASK, x-\u003efixup);\n1816:\t\t\toff = FIELD_GET(DATA_ARENA_OFFSET_MASK, x-\u003edata);\n1817:\t\t\taddr = *(unsigned long *)((void *)regs + arena_reg) + off;\n1818:\t\t\tbpf_prog_report_arena_violation(is_write, addr, regs-\u003eip);\n1819:\t\t}\n1820:\t\n1821:\t\t/* jump over faulting load and clear dest register */\n1822:\t\tif (reg != DONT_CLEAR)\n1823:\t\t\t*(unsigned long *)((void *)regs + reg) = 0;\n1824:\t\tregs-\u003eip += insn_len;\n1825:\t\n1826:\t\treturn true;\n1827:\t}\n1828:\t\n1829:\t/*\n"
}

9/2 2026/10/14 19:06 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 23073
							output: 23
							thoughts: 76
						

Thoughts:
Okay, here's my interpretation of these thoughts, formatted for an expert audience:

**Investigating JIT Exception Handling and Instruction Support**

Right, so I'm thinking about exception handling in JITed BPF code. Specifically, `ex_handler_bpf` is the key. It's triggered when something goes wrong *within* the JITted x86-64 code, like a page fault or an arena violation. I need to understand that better. I know `bpf_int_jit_compile` is the function responsible for compiling the BPF program into JITted x86-64.

My next step is to dig into the instruction support. I need to know the context of `bpf_jit_supports_insn`. I'm going to track down the callers of that function. Understanding what instructions are being supported, and under what conditions, is crucial for both performance optimization and error handling. I need to know why and when the JIT decides an instruction is, or isn't, supported. This is going to inform my view on how to improve the exception handling, and if there are limitations on the kinds of programs we can JIT effectively.



10/2 2026/10/14 19:06 tool grepper
0m Args:
{
  "Expression": "bpf_jit_supports_insn"
}

Results:
{
  "Output": "arch/arm64/net/bpf_jit_comp.c=3423=bool bpf_jit_supports_arena_scalar(void)\n--\narch/arm64/net/bpf_jit_comp.c-3427-\narch/arm64/net/bpf_jit_comp.c:3428:bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)\narch/arm64/net/bpf_jit_comp.c-3429-{\n--\narch/powerpc/net/bpf_jit_comp.c=551=bool bpf_jit_supports_far_kfunc_call(void)\n--\narch/powerpc/net/bpf_jit_comp.c-555-\narch/powerpc/net/bpf_jit_comp.c:556:bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)\narch/powerpc/net/bpf_jit_comp.c-557-{\n--\narch/riscv/net/bpf_jit_comp64.c=2237=bool bpf_jit_supports_arena(void)\n--\narch/riscv/net/bpf_jit_comp64.c-2249-\narch/riscv/net/bpf_jit_comp64.c:2250:bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)\narch/riscv/net/bpf_jit_comp64.c-2251-{\n--\narch/s390/net/bpf_jit_comp.c=3085=bool bpf_jit_supports_fsession(void)\n--\narch/s390/net/bpf_jit_comp.c-3089-\narch/s390/net/bpf_jit_comp.c:3090:bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)\narch/s390/net/bpf_jit_comp.c-3091-{\n--\narch/x86/net/bpf_jit_comp.c=4802=bool bpf_jit_supports_arena_scalar(void)\n--\narch/x86/net/bpf_jit_comp.c-4806-\narch/x86/net/bpf_jit_comp.c:4807:bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)\narch/x86/net/bpf_jit_comp.c-4808-{\n--\ninclude/linux/filter.h=1253=bool bpf_jit_supports_arena_scalar(void);\ninclude/linux/filter.h:1254:bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena);\ninclude/linux/filter.h-1255-bool bpf_jit_supports_private_stack(void);\n--\nkernel/bpf/core.c=3444=bool __weak bpf_jit_supports_arena_scalar(void)\n--\nkernel/bpf/core.c-3448-\nkernel/bpf/core.c:3449:bool __weak bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)\nkernel/bpf/core.c-3450-{\n--\nkernel/bpf/fixups.c=873=int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)\n--\nkernel/bpf/fixups.c-1066-\t\t\tif (BPF_MODE(insn-\u003ecode) == BPF_MEMSX) {\nkernel/bpf/fixups.c:1067:\t\t\t\tif (!bpf_jit_supports_insn(insn, true)) {\nkernel/bpf/fixups.c-1068-\t\t\t\t\tverbose(env, \"sign extending loads from arena are not supported yet\\n\");\n--\nkernel/bpf/verifier.c=5458=static bool atomic_ptr_type_ok(struct bpf_verifier_env *env, int regno,\n--\nkernel/bpf/verifier.c-5469-\tif (is_arena_reg(env, regno) || is_arena_scalar(env, regno))\nkernel/bpf/verifier.c:5470:\t\treturn bpf_jit_supports_insn(insn, true);\nkernel/bpf/verifier.c-5471-\tif (is_load_acq_unsafe(env, regno, insn))\n"
}

11/2 2026/10/14 19:06 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 24614
							output: 34
							thoughts: 649
						

Thoughts:
**My Analysis of Recent BPF JIT Enhancements for Atomic Operations on x86**

Okay, so looking at `kernel/bpf/verifier.c`, specifically the `atomic_ptr_type_ok` function, I see it now calls `bpf_jit_supports_insn(insn, true)`.  This is key because it seems we've significantly improved the JIT compilation for atomic operations on arena pointers on x86. Previously, `bpf_jit_supports_insn` on x86 would reject instructions using the `BPF_AND | BPF_FETCH`, `BPF_OR | BPF_FETCH`, and `BPF_XOR | BPF_FETCH` opcodes with arena pointers. We've loosened those restrictions.

My initial thought is to figure out the config settings required to use this new functionality, so I'm going to check the configs. And the Makefile for that architecture.

Now, looking deeper, in `do_jit()`, we're now handling the `BPF_STX | BPF_PROBE_ATOMIC` instructions, specifically `BPF_W` and `BPF_DW`, with a new implementation that supports `is_atomic_fetch_op(insn)`. This means we are now able to generate the `CMPXCHG` loop correctly using `emit_atomic_fetch_rmw(&prog, insn, real_dst_reg, real_src_reg, X86_REG_R12, fault, &resume)`. We are also setting up exception table entries for arena faults using `emit_arena_exentry`. This is where the code handles potential exceptions that may occur during atomics on arena pointers.

The code in `bpf_int_jit_compile` has been updated to count exception entries for the new atomic probe instructions.  We're iterating through instructions, checking for `BPF_STX | BPF_PROBE_ATOMIC` instructions that use `is_atomic_fetch_op(insn)`, and incrementing the `num_exentries` count accordingly. This suggests a careful tracking of these new operations.

Importantly, it looks like `do_jit` refactored existing `BPF_STX | BPF_ATOMIC` operations to leverage the same `emit_atomic_fetch_rmw` functionality. This is a very good refactor: consistency and efficiency are important!

Finally, the `emit_arena_exentry` helper function has been extracted and reused, being added to `populate_extable:` and the new probe atomic case. This suggests that the code has been updated to support new use cases, and that we have a standard way of handling arena pointer faults.



12/2 2026/10/14 19:07 tool grepper
0m Args:
{
  "Expression": "bpf_jit_comp",
  "PathPrefixes": [
    "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"
}

13/2 2026/10/14 19:07 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 26032
							output: 32
							thoughts: 107
						

Thoughts:
Okay, here's my understanding, focusing on my internal thought process:

**Investigating a Kernel Configuration Dependency**

Alright, I'm trying to figure out the dependencies for `arch/x86/net/bpf_jit_comp.o`. It looks like `CONFIG_BPF_JIT` is a hard requirement, which isn't entirely surprising. However, before I go any further, I know that loading BPF programs and managing BPF arenas also needs `BPF_SYSCALL`. Since `BPF_SYSCALL` is a key ingredient here, I'll need to understand its relationship to `CONFIG_BPF_JIT`.

To get a clearer picture of their relationships and dependencies within the kernel's build configuration, I'm going to quickly search the `Kconfig` files for both `config BPF_JIT` and `config BPF_SYSCALL`. That should give me a solid overview of what conditions trigger the enabling of these features. It's the most efficient way to trace the relevant configuration options.



14/2 2026/10/14 19:07 tool grepper
0m Args:
{
  "Expression": "config BPF_JIT\\b|config BPF_SYSCALL\\b"
}

Results:
{
  "Output": "kernel/bpf/Kconfig=29=menu \"BPF subsystem\"\nkernel/bpf/Kconfig-30-\nkernel/bpf/Kconfig:31:config BPF_SYSCALL\nkernel/bpf/Kconfig-32-\tbool \"Enable bpf() system call\"\n--\nkernel/bpf/Kconfig-45-\nkernel/bpf/Kconfig:46:config BPF_JIT\nkernel/bpf/Kconfig-47-\tbool \"Enable BPF Just In Time compiler\"\n"
}

15/2 2026/10/14 19:07 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 26535
							output: 34
							thoughts: 22
						

Thoughts:
Here's my summary, as if I were thinking these thoughts:

**Examining the BPF Kernel Configuration Options**

Okay, let's dive into the `kernel/bpf/Kconfig` file, focusing specifically on lines 29 through 70. I want to refresh my memory on the specific configuration options we have available for BPF within the kernel. I need to understand what's enabled, disabled, and what flags are available in the build. It is important to know which features are supported on our target. I'm primarily interested in what's exposed to userspace, or what can be tweaked for performance. I need to grasp the granular control we have over BPF functionality. I'm expecting to see definitions for things like whether we are using an eBPF or cBPF or not. Also, I would expect to see configuration options that define the various subsystems that will be compiled into BPF. This Kconfig file will ultimately determine the scope of our project with BPF.



16/2 2026/10/14 19:07 tool read-file
0m Args:
{
  "File": "kernel/bpf/Kconfig",
  "FirstLine": 29,
  "LineCount": 45
}

Results:
{
  "Contents": "  29:\tmenu \"BPF subsystem\"\n  30:\t\n  31:\tconfig BPF_SYSCALL\n  32:\t\tbool \"Enable bpf() system call\"\n  33:\t\tselect BPF\n  34:\t\tselect IRQ_WORK\n  35:\t\tselect NEED_TASKS_RCU\n  36:\t\tselect TASKS_TRACE_RCU\n  37:\t\tselect BINARY_PRINTF\n  38:\t\tselect NET_SOCK_MSG if NET\n  39:\t\tselect NET_XGRESS if NET\n  40:\t\tselect PAGE_POOL if NET\n  41:\t\tdefault n\n  42:\t\thelp\n  43:\t\t  Enable the bpf() system call that allows to manipulate BPF programs\n  44:\t\t  and maps via file descriptors.\n  45:\t\n  46:\tconfig BPF_JIT\n  47:\t\tbool \"Enable BPF Just In Time compiler\"\n  48:\t\tdepends on BPF\n  49:\t\tdepends on HAVE_CBPF_JIT || HAVE_EBPF_JIT\n  50:\t\tselect EXECMEM\n  51:\t\thelp\n  52:\t\t  BPF programs are normally handled by a BPF interpreter. This option\n  53:\t\t  allows the kernel to generate native code when a program is loaded\n  54:\t\t  into the kernel. This will significantly speed-up processing of BPF\n  55:\t\t  programs.\n  56:\t\n  57:\t\t  Note, an admin should enable this feature changing:\n  58:\t\t  /proc/sys/net/core/bpf_jit_enable\n  59:\t\t  /proc/sys/net/core/bpf_jit_harden   (optional)\n  60:\t\t  /proc/sys/net/core/bpf_jit_kallsyms (optional)\n  61:\t\n  62:\tconfig BPF_JIT_ALWAYS_ON\n  63:\t\tbool \"Permanently enable BPF JIT and remove BPF interpreter\"\n  64:\t\tdepends on BPF_SYSCALL \u0026\u0026 HAVE_EBPF_JIT \u0026\u0026 BPF_JIT\n  65:\t\thelp\n  66:\t\t  Enables BPF JIT and removes BPF interpreter to avoid speculative\n  67:\t\t  execution of BPF instructions by the interpreter.\n  68:\t\n  69:\t\t  When CONFIG_BPF_JIT_ALWAYS_ON is enabled, /proc/sys/net/core/bpf_jit_enable\n  70:\t\t  is permanently set to 1 and setting any other value than that will\n  71:\t\t  return failure.\n  72:\t\n  73:\tconfig BPF_JIT_DEFAULT_ON\n"
}

17/2 2026/10/14 19:07 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 27476
							output: 23
							thoughts: 53
						

Thoughts:
Okay, here's my take on those thoughts, framed as my own:

**Initial BPF Configuration Assessment**

Alright, so the core of this seems pretty straightforward: `BPF_SYSCALL` and `BPF_JIT` are the critical kernel configurations we're concerned with for our BPF setup. But before we get too far ahead of ourselves, I want to make sure we've got all the pieces in place. Specifically, I'm wondering if the arena allocation mechanism has any specific config requirements or dependencies. It's always best to be thorough, especially when dealing with this low-level stuff. Let's do a quick grep search for "arena" within the `kernel/bpf/` directory, and also in the `Kconfig` files. That should give me a good picture of whether we need to tweak any other settings before we proceed. Just need to be sure we have a clear picture of what the arena allocation mechanism needs.



18/2 2026/10/14 19:07 tool grepper
0m Args:
{
  "Expression": "BPF_MAP_TYPE_ARENA"
}

Results:
{
  "Output": "\nFull output is too long, showing 200 out of 299 lines.\nUse more precise expression or PathPrefixes if possible.\n\nNumber of matching lines per file (37 files in total):\ninclude/linux/bpf_types.h:1\ninclude/uapi/linux/bpf.h:2\nkernel/bpf/arena.c:7\nkernel/bpf/syscall.c:3\nkernel/bpf/verifier.c:3\ntools/bpf/bpftool/gen.c:1\ntools/include/uapi/linux/bpf.h:2\ntools/lib/bpf/libbpf.c:10\ntools/lib/bpf/libbpf_probes.c:1\ntools/sched_ext/scx_qmap.bpf.c:1\ntools/sched_ext/scx_sdt.bpf.c:1\ntools/testing/selftests/bpf/libarena/include/libarena/common.h:1\ntools/testing/selftests/bpf/prog_tests/arena_direct_value.c:1\ntools/testing/selftests/bpf/prog_tests/data_in_arena.c:3\ntools/testing/selftests/bpf/progs/aggregate_ret_kfunc_arena.c:1\ntools/testing/selftests/bpf/progs/arena_atomics.c:1\ntools/testing/selftests/bpf/progs/arena_htab.c:1\ntools/testing/selftests/bpf/progs/arena_kfunc.c:1\ntools/testing/selftests/bpf/progs/arena_kfunc_jit.c:1\ntools/testing/selftests/bpf/progs/arena_list.c:1\ntools/testing/selftests/bpf/progs/arena_mem_usage.c:1\ntools/testing/selftests/bpf/progs/arena_memcg.c:1\ntools/testing/selftests/bpf/progs/arena_spin_lock.c:1\ntools/testing/selftests/bpf/progs/arena_strsearch.c:1\ntools/testing/selftests/bpf/progs/compute_live_registers.c:1\ntools/testing/selftests/bpf/progs/data_in_arena_decl.c:1\ntools/testing/selftests/bpf/progs/linked_arena1.c:1\ntools/testing/selftests/bpf/progs/stream.c:1\ntools/testing/selftests/bpf/progs/struct_ops_arena.c:1\ntools/testing/selftests/bpf/progs/verifier_arena.c:1\ntools/testing/selftests/bpf/progs/verifier_arena_globals1.c:1\ntools/testing/selftests/bpf/progs/verifier_arena_globals2.c:1\ntools/testing/selftests/bpf/progs/verifier_arena_large.c:1\ntools/testing/selftests/bpf/progs/verifier_arena_scalar.c:1\ntools/testing/selftests/bpf/progs/verifier_ldsx.c:1\ntools/testing/selftests/bpf/progs/verifier_zext.c:1\ntools/testing/selftests/sched_ext/enable_cmask.bpf.c:1\n\ninclude/linux/bpf_types.h=134=BPF_MAP_TYPE(BPF_MAP_TYPE_USER_RINGBUF, user_ringbuf_map_ops)\ninclude/linux/bpf_types.h:135:BPF_MAP_TYPE(BPF_MAP_TYPE_ARENA, arena_map_ops)\ninclude/linux/bpf_types.h-136-BPF_MAP_TYPE(BPF_MAP_TYPE_INSN_ARRAY, insn_array_map_ops)\n--\ninclude/uapi/linux/bpf.h=1028=enum bpf_map_type {\n--\ninclude/uapi/linux/bpf.h-1075-\tBPF_MAP_TYPE_CGRP_STORAGE,\ninclude/uapi/linux/bpf.h:1076:\tBPF_MAP_TYPE_ARENA,\ninclude/uapi/linux/bpf.h-1077-\tBPF_MAP_TYPE_INSN_ARRAY,\n--\ninclude/uapi/linux/bpf.h=1566=union bpf_attr {\n--\ninclude/uapi/linux/bpf.h-1593-\t\t *\ninclude/uapi/linux/bpf.h:1594:\t\t * BPF_MAP_TYPE_ARENA - contains the address where user space\ninclude/uapi/linux/bpf.h-1595-\t\t * is going to mmap() the arena. It has to be page aligned.\n--\nkernel/bpf/arena.c=86=u64 bpf_arena_get_user_vm_start(struct bpf_arena *arena)\n--\nkernel/bpf/arena.c-92- * bpf_arena_map_kern_vm_start - kern_vm_start lookup by struct bpf_map *\nkernel/bpf/arena.c:93: * @map: a BPF_MAP_TYPE_ARENA map\nkernel/bpf/arena.c-94- *\n--\nkernel/bpf/arena.c=1096=__bpf_kfunc void *bpf_arena_alloc_pages(void *p__map, void *addr__ign, u32 page_cnt,\n--\nkernel/bpf/arena.c-1101-\nkernel/bpf/arena.c:1102:\tif (map-\u003emap_type != BPF_MAP_TYPE_ARENA || flags || !page_cnt)\nkernel/bpf/arena.c-1103-\t\treturn NULL;\n--\nkernel/bpf/arena.c=1108=void *bpf_arena_alloc_pages_non_sleepable(void *p__map, void *addr__ign, u32 page_cnt,\n--\nkernel/bpf/arena.c-1113-\nkernel/bpf/arena.c:1114:\tif (map-\u003emap_type != BPF_MAP_TYPE_ARENA || flags || !page_cnt)\nkernel/bpf/arena.c-1115-\t\treturn NULL;\n--\nkernel/bpf/arena.c=1120=void *bpf_arena_alloc_pages_sleepable(void *p__map, void *addr__ign, u32 page_cnt,\n--\nkernel/bpf/arena.c-1125-\nkernel/bpf/arena.c:1126:\tif (map-\u003emap_type != BPF_MAP_TYPE_ARENA || flags || !page_cnt)\nkernel/bpf/arena.c-1127-\t\treturn NULL;\n--\nkernel/bpf/arena.c=1132=__bpf_kfunc void bpf_arena_free_pages(void *p__map, void *ptr__ign, u32 page_cnt)\n--\nkernel/bpf/arena.c-1136-\nkernel/bpf/arena.c:1137:\tif (map-\u003emap_type != BPF_MAP_TYPE_ARENA || !page_cnt || !ptr__ign)\nkernel/bpf/arena.c-1138-\t\treturn;\n--\nkernel/bpf/arena.c=1142=void bpf_arena_free_pages_non_sleepable(void *p__map, void *ptr__ign, u32 page_cnt)\n--\nkernel/bpf/arena.c-1146-\nkernel/bpf/arena.c:1147:\tif (map-\u003emap_type != BPF_MAP_TYPE_ARENA || !page_cnt || !ptr__ign)\nkernel/bpf/arena.c-1148-\t\treturn;\n--\nkernel/bpf/arena.c=1152=__bpf_kfunc int bpf_arena_reserve_pages(void *p__map, void *ptr__ign, u32 page_cnt)\n--\nkernel/bpf/arena.c-1156-\nkernel/bpf/arena.c:1157:\tif (map-\u003emap_type != BPF_MAP_TYPE_ARENA)\nkernel/bpf/arena.c-1158-\t\treturn -EINVAL;\n--\nkernel/bpf/syscall.c=250=static int bpf_map_update_value(struct bpf_map *map, struct file *map_file,\n--\nkernel/bpf/syscall.c-258-\t} else if (map-\u003emap_type == BPF_MAP_TYPE_CPUMAP ||\nkernel/bpf/syscall.c:259:\t\t   map-\u003emap_type == BPF_MAP_TYPE_ARENA ||\nkernel/bpf/syscall.c-260-\t\t   map-\u003emap_type == BPF_MAP_TYPE_STRUCT_OPS) {\n--\nkernel/bpf/syscall.c=1401=static int map_create_alloc(union bpf_attr *attr, bpfptr_t uattr, struct bpf_verifier_log *log,\n--\nkernel/bpf/syscall.c-1438-\tif (attr-\u003emap_type != BPF_MAP_TYPE_BLOOM_FILTER \u0026\u0026\nkernel/bpf/syscall.c:1439:\t    attr-\u003emap_type != BPF_MAP_TYPE_ARENA \u0026\u0026\nkernel/bpf/syscall.c-1440-\t    attr-\u003emap_type != BPF_MAP_TYPE_RHASH \u0026\u0026\n--\nkernel/bpf/syscall.c-1534-\tcase BPF_MAP_TYPE_CPUMAP:\nkernel/bpf/syscall.c:1535:\tcase BPF_MAP_TYPE_ARENA:\nkernel/bpf/syscall.c-1536-\tcase BPF_MAP_TYPE_INSN_ARRAY:\n--\nkernel/bpf/verifier.c=18731=static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)\n--\nkernel/bpf/verifier.c-18815-\t    insn-\u003esrc_reg == BPF_PSEUDO_MAP_IDX_VALUE) {\nkernel/bpf/verifier.c:18816:\t\tif (map-\u003emap_type == BPF_MAP_TYPE_ARENA) {\nkernel/bpf/verifier.c-18817-\t\t\t__mark_reg_unknown(env, dst_reg);\n--\nkernel/bpf/verifier.c=20586=static int check_map_prog_compatibility(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-20651-\t\tcase BPF_MAP_TYPE_STACK:\nkernel/bpf/verifier.c:20652:\t\tcase BPF_MAP_TYPE_ARENA:\nkernel/bpf/verifier.c-20653-\t\tcase BPF_MAP_TYPE_INSN_ARRAY:\n--\nkernel/bpf/verifier.c-20667-\nkernel/bpf/verifier.c:20668:\tif (map-\u003emap_type == BPF_MAP_TYPE_ARENA) {\nkernel/bpf/verifier.c-20669-\t\tif (env-\u003eprog-\u003eaux-\u003earena) {\n--\ntools/bpf/bpftool/gen.c=263=static bool is_skel_data(const struct bpf_map *map, char *buf, size_t sz)\n--\ntools/bpf/bpftool/gen.c-266-\ntools/bpf/bpftool/gen.c:267:\tif (bpf_map__type(map) == BPF_MAP_TYPE_ARENA \u0026\u0026 bpf_map__initial_value(map, \u0026tmp_sz)) {\ntools/bpf/bpftool/gen.c-268-\t\tsnprintf(buf, sz, \"arena\");\n--\ntools/include/uapi/linux/bpf.h=1028=enum bpf_map_type {\n--\ntools/include/uapi/linux/bpf.h-1075-\tBPF_MAP_TYPE_CGRP_STORAGE,\ntools/include/uapi/linux/bpf.h:1076:\tBPF_MAP_TYPE_ARENA,\ntools/include/uapi/linux/bpf.h-1077-\tBPF_MAP_TYPE_INSN_ARRAY,\n--\ntools/include/uapi/linux/bpf.h=1566=union bpf_attr {\n--\ntools/include/uapi/linux/bpf.h-1593-\t\t *\ntools/include/uapi/linux/bpf.h:1594:\t\t * BPF_MAP_TYPE_ARENA - contains the address where user space\ntools/include/uapi/linux/bpf.h-1595-\t\t * is going to mmap() the arena. It has to be page aligned.\n--\ntools/lib/bpf/libbpf.c=163=static const char * const map_type_name[] = {\n--\ntools/lib/bpf/libbpf.c-196-\t[BPF_MAP_TYPE_CGRP_STORAGE]\t\t= \"cgrp_storage\",\ntools/lib/bpf/libbpf.c:197:\t[BPF_MAP_TYPE_ARENA]\t\t\t= \"arena\",\ntools/lib/bpf/libbpf.c-198-\t[BPF_MAP_TYPE_INSN_ARRAY]\t\t= \"insn_array\",\n--\ntools/lib/bpf/libbpf.c=1923=static size_t bpf_map_mmap_sz(const struct bpf_map *map)\n--\ntools/lib/bpf/libbpf.c-1930-\t\treturn array_map_mmap_sz(map-\u003edef.value_size, map-\u003edef.max_entries);\ntools/lib/bpf/libbpf.c:1931:\tcase BPF_MAP_TYPE_ARENA:\ntools/lib/bpf/libbpf.c-1932-\t\treturn page_sz * map-\u003edef.max_entries;\n--\ntools/lib/bpf/libbpf.c=3177=static int bpf_object__init_arena_data(struct bpf_object *obj)\n--\ntools/lib/bpf/libbpf.c-3215-\t\tmap-\u003esec_idx = -1;\ntools/lib/bpf/libbpf.c:3216:\t\tmap-\u003edef.type = BPF_MAP_TYPE_ARENA;\ntools/lib/bpf/libbpf.c-3217-\t\tmap-\u003edef.max_entries = roundup(sz, page_sz) / page_sz;\n--\ntools/lib/bpf/libbpf.c=3253=static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,\n--\ntools/lib/bpf/libbpf.c-3304-\ntools/lib/bpf/libbpf.c:3305:\t\tif (map-\u003edef.type != BPF_MAP_TYPE_ARENA)\ntools/lib/bpf/libbpf.c-3306-\t\t\tcontinue;\n--\ntools/lib/bpf/libbpf.c=5684=static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, bool is_inner)\n--\ntools/lib/bpf/libbpf.c-5753-\tcase BPF_MAP_TYPE_STACK:\ntools/lib/bpf/libbpf.c:5754:\tcase BPF_MAP_TYPE_ARENA:\ntools/lib/bpf/libbpf.c-5755-\t\tcreate_attr.btf_fd = 0;\n--\ntools/lib/bpf/libbpf.c=5926=bpf_object__create_maps(struct bpf_object *obj)\n--\ntools/lib/bpf/libbpf.c-5965-\t\t/* see bpf_object__relocate_data_ptrs() */\ntools/lib/bpf/libbpf.c:5966:\t\tif (map-\u003edef.type == BPF_MAP_TYPE_ARENA \u0026\u0026 obj-\u003earena_mapped)\ntools/lib/bpf/libbpf.c-5967-\t\t\tcontinue;\n--\ntools/lib/bpf/libbpf.c-6004-\t\t\t\t\tgoto err_out;\ntools/lib/bpf/libbpf.c:6005:\t\t\t} else if (map-\u003edef.type == BPF_MAP_TYPE_ARENA) {\ntools/lib/bpf/libbpf.c-6006-\t\t\t\terr = bpf_object__mmap_arena(obj, map);\n--\ntools/lib/bpf/libbpf.c=11997=int bpf_map__set_initial_value(struct bpf_map *map,\n--\ntools/lib/bpf/libbpf.c-12007-\ntools/lib/bpf/libbpf.c:12008:\tif (map-\u003edef.type == BPF_MAP_TYPE_ARENA)\ntools/lib/bpf/libbpf.c-12009-\t\tactual_sz = map-\u003eobj-\u003earena_data_sz;\n--\ntools/lib/bpf/libbpf.c=12019=void *bpf_map__initial_value(const struct bpf_map *map, size_t *psize)\n--\ntools/lib/bpf/libbpf.c-12029-\ntools/lib/bpf/libbpf.c:12030:\tif (map-\u003edef.type == BPF_MAP_TYPE_ARENA)\ntools/lib/bpf/libbpf.c-12031-\t\t*psize = map-\u003eobj-\u003earena_data_sz;\n--\ntools/lib/bpf/libbpf.c=16073=int bpf_object__load_skeleton(struct bpf_object_skeleton *s)\n--\ntools/lib/bpf/libbpf.c-16089-\ntools/lib/bpf/libbpf.c:16090:\t\tif (map-\u003edef.type == BPF_MAP_TYPE_ARENA)\ntools/lib/bpf/libbpf.c-16091-\t\t\t*map_skel-\u003emmaped = map-\u003emmaped + map-\u003eobj-\u003earena_data_off;\n--\ntools/lib/bpf/libbpf_probes.c=295=static int probe_map_create(enum bpf_map_type map_type)\n--\ntools/lib/bpf/libbpf_probes.c-356-\t\tbreak;\ntools/lib/bpf/libbpf_probes.c:357:\tcase BPF_MAP_TYPE_ARENA:\ntools/lib/bpf/libbpf_probes.c-358-\t\tkey_size\t= 0;\n--\ntools/sched_ext/scx_qmap.bpf.c=94=struct {\ntools/sched_ext/scx_qmap.bpf.c:95:\t__uint(type, BPF_MAP_TYPE_ARENA);\ntools/sched_ext/scx_qmap.bpf.c-96-\t__uint(map_flags, BPF_F_MMAPABLE);\n--\ntools/sched_ext/scx_sdt.bpf.c=26=struct {\ntools/sched_ext/scx_sdt.bpf.c:27:\t__uint(type, BPF_MAP_TYPE_ARENA);\ntools/sched_ext/scx_sdt.bpf.c-28-\t__uint(map_flags, BPF_F_MMAPABLE);\n--\ntools/testing/selftests/bpf/libarena/include/libarena/common.h=29=struct {\ntools/testing/selftests/bpf/libarena/include/libarena/common.h:30:\t__uint(type, BPF_MAP_TYPE_ARENA);\ntools/testing/selftests/bpf/libarena/include/libarena/common.h-31-\t__uint(map_flags, BPF_F_MMAPABLE);\n--\ntools/testing/selftests/bpf/prog_tests/arena_direct_value.c=13=static void test_arena_direct_value_one_past_end(void)\n--\ntools/testing/selftests/bpf/prog_tests/arena_direct_value.c-31-\ntools/testing/selftests/bpf/prog_tests/arena_direct_value.c:32:\tmap_fd = bpf_map_create(BPF_MAP_TYPE_ARENA, \"arena_direct_value\",\ntools/testing/selftests/bpf/prog_tests/arena_direct_value.c-33-\t\t\t\t0, 0, ARENA_PAGES, \u0026map_opts);\n--\ntools/testing/selftests/bpf/prog_tests/data_in_arena.c=29=static void test_in_arena(void)\n--\ntools/testing/selftests/bpf/prog_tests/data_in_arena.c-43-\t\tgoto out;\ntools/testing/selftests/bpf/prog_tests/data_in_arena.c:44:\tASSERT_EQ(bpf_map__type(arena), BPF_MAP_TYPE_ARENA, \"arena type\");\ntools/testing/selftests/bpf/prog_tests/data_in_arena.c-45-\tASSERT_EQ(bpf_map__max_entries(arena), 1, \"arena pages\");\n--\ntools/testing/selftests/bpf/prog_tests/data_in_arena.c=86=static void test_declared_arena(void)\n--\ntools/testing/selftests/bpf/prog_tests/data_in_arena.c-95-\tbpf_object__for_each_map(map, skel-\u003eobj)\ntools/testing/selftests/bpf/prog_tests/data_in_arena.c:96:\t\tarenas += bpf_map__type(map) == BPF_MAP_TYPE_ARENA;\n\n"
}

19/2 2026/10/14 19:07 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 33368
							output: 28
							thoughts: 23
						

Thoughts:
Okay, here's my take on those thoughts, tailored for an expert audience and presented in the first person:

**Investigating the BPF Arena Compilation Process**

Alright, before I dive too deep into debugging this BPF issue, I need to get a clear picture of how `arena.c` is actually being compiled. My immediate thought is to check the `kernel/bpf/Makefile`. That's where all the build rules for the BPF subsystem should reside, and it should give me the crucial details about any specific flags or dependencies used when building this `arena.c` component. I need to understand this compilation process thoroughly to avoid getting tripped up by any unexpected behavior resulting from the build configuration. Time to dig in there and see what it reveals.



20/2 2026/10/14 19:07 tool grepper
0m Args:
{
  "Expression": "arena\\.o",
  "PathPrefixes": [
    "kernel/bpf"
  ]
}

Results:
{
  "Output": "kernel/bpf/Makefile=19=ifeq ($(CONFIG_MMU)$(CONFIG_64BIT),yy)\nkernel/bpf/Makefile:20:obj-$(CONFIG_BPF_SYSCALL) += arena.o range_tree.o\nkernel/bpf/Makefile-21-endif\n"
}

21/2 2026/10/14 19:07 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 33662
							output: 224
							thoughts: 2515
						

Thoughts:
**Focusing on x86-64 BPF JIT Compiler Changes**

Okay, so I'm diving into this BPF JIT compilation change for x86-64. I need to figure out what's new and how to effectively fuzz it. First, I see that the core compilation happens when `CONFIG_BPF_SYSCALL` and `CONFIG_BPF_JIT` are enabled. These are pretty standard, and x86-64 automatically has `CONFIG_MMU` and `CONFIG_64BIT`. Let's confirm no other configs are needed, and that `BPF_JIT` is sufficient (which it usually is, especially with syzkaller).

I need to nail down the *specific* functions that are added or modified. The prompt is very specific about avoiding ubiquitous functions like memory allocators, page fault handlers, or synchronization primitives in the `FocusSymbols`. That's critical to ensure the fuzzer spends time on what *actually* changed. I've got to focus on feature-specific logic.

Right, so in `arch/x86/net/bpf_jit_comp.c`, the main entry point is `bpf_int_jit_compile`, which does the JIT compilation for a BPF program. I need to make sure this isn't some generic function. Nope, it's specific to x86 JIT compilation. Excellent. Then I see several *static* functions: `emit_atomic_fetch_rmw`, `emit_arena_exentry`, and `do_jit`. In C files, static functions *do* have symbols in the vmlinux if not inlined. These are key because they implement the new atomic fetch and arena exception table logic. `do_jit` is the main worker function for the code generation. There's also `bpf_jit_supports_insn`, which is called by the verifier, and `ex_handler_bpf`, called when the JITed BPF program faults. This is *not* a ubiquitous page fault handler; it's specific to JITed BPF code.

Looking at the diff, the changed functions are: `is_atomic_fetch_op` (static inline), `emit_atomic_fetch_rmw`, `emit_arena_exentry`, `do_jit`, `bpf_int_jit_compile`, and `bpf_jit_supports_insn`. Also, `ex_handler_bpf` is heavily modified.

The other files are in `tools/`, which are user-space/selftests/BPF program headers, so they are not important for this fuzzing effort.

Now, is this *worth* fuzzing? Absolutely! It's worth fuzzing because this patch fundamentally changes how atomic fetch operations are handled within the x86-64 BPF JIT compiler. It adds CMPXCHG loops, adjusts exception table entries, and touches core JIT compiler and exception handling reachable from userspace via `bpf()`!

So, the `FocusSymbols` are definitely: `bpf_int_jit_compile`, `do_jit`, `emit_atomic_fetch_rmw`, `emit_arena_exentry`, `bpf_jit_supports_insn`, and `ex_handler_bpf`. None of those are ubiquitous, and that's critical. `bpf_int_jit_compile` compiles BPF code. `do_jit` does code generation. `emit_atomic_fetch_rmw` handles atomic operations. `emit_arena_exentry` creates arena entries. `bpf_jit_supports_insn` is used by the verifier. `ex_handler_bpf` is the specific exception handler.

EnableConfigs: `BPF_SYSCALL` and `BPF_JIT`. Confirmed. Perfect.



22/2 2026/10/14 19:07 tool set-results
0m Args:
{
  "EnableConfigs": [
    "BPF_SYSCALL",
    "BPF_JIT"
  ],
  "FocusSymbols": [
    "bpf_int_jit_compile",
    "do_jit",
    "emit_atomic_fetch_rmw",
    "emit_arena_exentry",
    "bpf_jit_supports_insn",
    "ex_handler_bpf"
  ],
  "Reasoning": "The patch adds support in the x86 BPF JIT compiler for atomic fetch read-modify-write operations (BPF_AND/OR/XOR with BPF_FETCH) on BPF arena memory pointers. It implements a CMPXCHG loop for atomic fetch RMW, generates exception table entries for fault recovery during arena accesses, adjusts the BPF prog exception entry count, and lifts previous rejection in bpf_jit_supports_insn. This touches reachable core BPF JIT and exception handling logic on amd64 accessible via the bpf() system call.",
  "WorthFuzzing": true
}

Results:
{
  "EnableConfigs": [
    "BPF_SYSCALL",
    "BPF_JIT"
  ],
  "FocusSymbols": [
    "bpf_int_jit_compile",
    "do_jit",
    "emit_atomic_fetch_rmw",
    "emit_arena_exentry",
    "bpf_jit_supports_insn",
    "ex_handler_bpf"
  ],
  "Reasoning": "The patch adds support in the x86 BPF JIT compiler for atomic fetch read-modify-write operations (BPF_AND/OR/XOR with BPF_FETCH) on BPF arena memory pointers. It implements a CMPXCHG loop for atomic fetch RMW, generates exception table entries for fault recovery during arena accesses, adjusts the BPF prog exception entry count, and lifts previous rejection in bpf_jit_supports_insn. This touches reachable core BPF JIT and exception handling logic on amd64 accessible via the bpf() system call.",
  "WorthFuzzing": true
}

23/1 2026/10/14 19:07 action If
1m Args:
{
  "WorthFuzzing": true
}

Results:
null

24/2 2026/10/14 19:07 agent kmsan-evaluator
1m Model:
core

Args:
null

Results:
{
  "KMSANReasoning": "The patch series adds x86 BPF JIT support for atomic fetch read-modify-write operations (BPF_AND | BPF_FETCH, BPF_OR | BPF_FETCH, BPF_XOR | BPF_FETCH) on BPF arena memory. It lowers these operations to a CMPXCHG loop, registers exception table entries for both the load and CMPXCHG instructions to handle faults, adjusts num_exentries allocation calculations, and updates the relevant BPF selftests and sched_ext header helpers.\n\nKMSAN vs KASAN Applicability:\n- KMSAN detects uninitialized memory reads (heap, stack, page allocations) and kernel-to-user info-leaks. This patch introduces no new kernel-to-user data copies, does not alter serialization/padding of structs exposed to userspace, and does not add conditional logic on uninitialized kernel variables. The exception table entries initialized by emit_arena_exentry() have all their struct fields (insn, fixup, data) explicitly assigned.\n- Potential risks of these changes include JIT instruction encoding errors, register clobbering, incorrect jump offsets, or extable sizing/bounds errors. Array bounds issues in extable allocations are detected by KASAN, while execution/encoding issues are caught by the verifier, selftests, and standard hardware exception handling.\n- Because no uninitialized memory read or info-leak 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 b883a66f73e1dc8ca4b2e7a43e9c65ea1f8e1cdd
Author: syz-cluster <triage@syzkaller.com>
Date:   Wed Oct 14 19:06:06 2026 +0000

    syz-cluster: applied patch under review

diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
index 083fcd6cf15b7..5fb1a31f71a26 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -239,6 +239,13 @@ static const int reg2pt_regs[] = {
 	[BPF_REG_AX] = offsetof(struct pt_regs, r10),
 };
 
+static bool is_atomic_fetch_op(const struct bpf_insn *insn)
+{
+	return insn->imm == (BPF_AND | BPF_FETCH) ||
+	       insn->imm == (BPF_OR | BPF_FETCH) ||
+	       insn->imm == (BPF_XOR | BPF_FETCH);
+}
+
 /*
  * is_ereg() == true if BPF register 'reg' maps to x86-64 r8..r15
  * which need extra byte of encoding.
@@ -1680,6 +1687,72 @@ static int emit_atomic_ld_st_index(u8 **pprog, u32 atomic_op, u32 size,
 	return 0;
 }
 
+/*
+ * A fetching AND/OR/XOR can't be implemented with a single x86 insn, so do a
+ * CMPXCHG loop. @index_reg is X86_REG_R12 for an arena access or -1 otherwise,
+ * and @dst_reg/@src_reg are already substituted for R0 by the caller.
+ *
+ * Both the load and the CMPXCHG can fault when accessing the arena, so their
+ * addresses are handed back in @fault[0] and @fault[1]. A fault has to resume
+ * at @resume, which is past the loop and past the move that delivers the old
+ * value, but before the R0 restore. Both are NULL for the non-arena case,
+ * which needs no fixup.
+ */
+static int emit_atomic_fetch_rmw(u8 **pprog, struct bpf_insn *insn, u32 dst_reg,
+				 u32 src_reg, int index_reg, u8 **fault,
+				 u8 **resume)
+{
+	bool is64 = BPF_SIZE(insn->code) == BPF_DW;
+	u8 *branch_target, *prog = *pprog;
+	int err;
+
+	branch_target = prog;
+
+	/* Load old value */
+	if (fault)
+		fault[0] = prog;
+	if (index_reg < 0)
+		emit_ldx(&prog, BPF_SIZE(insn->code), BPF_REG_0, dst_reg, insn->off);
+	else
+		emit_ldx_index(&prog, BPF_SIZE(insn->code), BPF_REG_0, dst_reg,
+			       index_reg, insn->off);
+
+	/*
+	 * Perform the (commutative) operation locally, put the result in
+	 * the AUX_REG.
+	 */
+	emit_mov_reg(&prog, is64, AUX_REG, BPF_REG_0);
+	maybe_emit_mod(&prog, AUX_REG, src_reg, is64);
+	EMIT2(simple_alu_opcodes[BPF_OP(insn->imm)],
+	      add_2reg(0xC0, AUX_REG, src_reg));
+
+	/* Attempt to swap in new value */
+	if (fault)
+		fault[1] = prog;
+	if (index_reg < 0)
+		err = emit_atomic_rmw(&prog, BPF_CMPXCHG, dst_reg, AUX_REG,
+				      insn->off, BPF_SIZE(insn->code));
+	else
+		err = emit_atomic_rmw_index(&prog, BPF_CMPXCHG, BPF_SIZE(insn->code),
+					    dst_reg, AUX_REG, index_reg, insn->off);
+	if (WARN_ON(err))
+		return err;
+
+	/* ZF tells us whether we won the race. If it's cleared we need to try again. */
+	EMIT2(X86_JNE, -(prog - branch_target) - 2);
+	/* Return the pre-modification value */
+	emit_mov_reg(&prog, is64, src_reg, BPF_REG_0);
+
+	if (resume)
+		*resume = prog;
+
+	/* Restore R0 after clobbering RAX */
+	emit_mov_reg(&prog, true, BPF_REG_0, BPF_REG_AX);
+
+	*pprog = prog;
+	return 0;
+}
+
 /*
  * Metadata encoding for exception handling in JITed code.
  *
@@ -1693,7 +1766,12 @@ static int emit_atomic_ld_st_index(u8 **pprog, u32 atomic_op, u32 size,
  * | ARENA_ACC | ARENA_WRITE | Unused | ARENA_REG | DST_REG | INSN_LEN |
  * +-----------+-------------+--------+-----------+---------+----------+
  *
- * - INSN_LEN (8 bits): Length of faulting insn (max x86 insn = 15 bytes (fits in 8 bits)).
+ * - INSN_LEN (8 bits): How far past the faulting insn to resume. That is its own length
+ *                      for a single-insn access, but the distance to the end of the whole
+ *                      sequence where one BPF insn became several, as for the CMPXCHG loop
+ *                      of a fetching AND/OR/XOR. A sequence long enough to overflow this
+ *                      field would first have to exceed BPF_MAX_INSN_SIZE, which do_jit()
+ *                      rejects with -EFAULT before the image is used.
  * - DST_REG  (8 bits): Offset of dst_reg from reg2pt_regs[] (max offset = 112 (fits in 8 bits)).
  *                      This is set to DONT_CLEAR if the insn does not read into a register.
  * - ARENA_REG (8 bits): Offset of the register that is used to calculate the
@@ -1748,6 +1826,44 @@ bool ex_handler_bpf(const struct exception_table_entry *x, struct pt_regs *regs)
 	return true;
 }
 
+/*
+ * Record an arena access that may fault. @fault_ip is the address of the
+ * faulting insn in the RO image, @resume_off how far past it execution has to
+ * resume: for a multi-insn lowering that is the end of the whole sequence, not
+ * the end of the one insn.
+ */
+static int emit_arena_exentry(struct bpf_prog *bpf_prog, u8 *image, u8 *rw_image,
+			      int *excnt, u8 *fault_ip, u32 resume_off,
+			      u32 fixup_reg, u32 arena_reg, bool is_write, s16 off)
+{
+	struct exception_table_entry *ex;
+	s64 delta;
+
+	if (!bpf_prog->aux->extable)
+		return 0;
+
+	if (*excnt >= bpf_prog->aux->num_exentries) {
+		pr_err("arena extable bug\n");
+		return -EFAULT;
+	}
+	ex = &bpf_prog->aux->extable[(*excnt)++];
+
+	delta = fault_ip - (u8 *)&ex->insn;
+	/* switch ex to rw buffer for writes */
+	ex = (void *)rw_image + ((void *)ex - (void *)image);
+
+	ex->insn = delta;
+	ex->data = EX_TYPE_BPF | FIELD_PREP(DATA_ARENA_OFFSET_MASK, off);
+	ex->fixup = FIELD_PREP(FIXUP_INSN_LEN_MASK, resume_off) |
+		    FIELD_PREP(FIXUP_ARENA_REG_MASK, arena_reg) |
+		    FIELD_PREP(FIXUP_REG_MASK, fixup_reg) |
+		    FIXUP_ARENA_ACCESS;
+	if (is_write)
+		ex->fixup |= FIXUP_ARENA_WRITE;
+
+	return 0;
+}
+
 static void detect_reg_usage(struct bpf_insn *insn, int insn_cnt,
 			     bool *regs_used)
 {
@@ -2625,28 +2741,8 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 			}
 populate_extable:
 			{
-				struct exception_table_entry *ex;
-				u8 *_insn = image + proglen + (start_of_ldx - temp);
 				u32 arena_reg, fixup_reg;
 				bool is_write;
-				s64 delta;
-
-				if (!bpf_prog->aux->extable)
-					break;
-
-				if (excnt >= bpf_prog->aux->num_exentries) {
-					pr_err("mem32 extable bug\n");
-					return -EFAULT;
-				}
-				ex = &bpf_prog->aux->extable[excnt++];
-
-				delta = _insn - (u8 *)&ex->insn;
-				/* switch ex to rw buffer for writes */
-				ex = (void *)rw_image + ((void *)ex - (void *)image);
-
-				ex->insn = delta;
-
-				ex->data = EX_TYPE_BPF;
 
 				/*
 				 * src_reg/dst_reg holds the address in the arena region with upper
@@ -2682,14 +2778,12 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 					is_write = true;
 				}
 
-				ex->fixup = FIELD_PREP(FIXUP_INSN_LEN_MASK, prog - start_of_ldx) |
-					    FIELD_PREP(FIXUP_ARENA_REG_MASK, arena_reg) |
-					    FIELD_PREP(FIXUP_REG_MASK, fixup_reg);
-				ex->fixup |= FIXUP_ARENA_ACCESS;
-				if (is_write)
-					ex->fixup |= FIXUP_ARENA_WRITE;
-
-				ex->data |= FIELD_PREP(DATA_ARENA_OFFSET_MASK, insn->off);
+				err = emit_arena_exentry(bpf_prog, image, rw_image, &excnt,
+							 image + proglen + (start_of_ldx - temp),
+							 prog - start_of_ldx, fixup_reg,
+							 arena_reg, is_write, insn->off);
+				if (err)
+					return err;
 			}
 			break;
 
@@ -2841,20 +2935,12 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 			fallthrough;
 		case BPF_STX | BPF_ATOMIC | BPF_W:
 		case BPF_STX | BPF_ATOMIC | BPF_DW: {
-			bool is64 = BPF_SIZE(insn->code) == BPF_DW;
 			u32 real_src_reg = src_reg;
 			u32 real_dst_reg = dst_reg;
+			bool is_atomic_fetch = is_atomic_fetch_op(insn);
 			u8 *old_prog;
-			bool is_atomic_fetch =
-				(insn->imm == (BPF_AND | BPF_FETCH) ||
-				 insn->imm == (BPF_OR | BPF_FETCH) ||
-				 insn->imm == (BPF_XOR | BPF_FETCH));
-			if (is_atomic_fetch) {
-				/*
-				 * Can't be implemented with a single x86 insn.
-				 * Need to do a CMPXCHG loop.
-				 */
 
+			if (is_atomic_fetch) {
 				/* Will need RAX as a CMPXCHG operand so save R0 */
 				old_prog = prog;
 				emit_mov_reg(&prog, true, BPF_REG_AX, BPF_REG_0);
@@ -2875,34 +2961,11 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 				}
 			}
 			if (is_atomic_fetch) {
-				u8 *branch_target = prog;
-				/* Load old value */
-				emit_ldx(&prog, BPF_SIZE(insn->code),
-					 BPF_REG_0, real_dst_reg, insn->off);
-				/*
-				 * Perform the (commutative) operation locally,
-				 * put the result in the AUX_REG.
-				 */
-				emit_mov_reg(&prog, is64, AUX_REG, BPF_REG_0);
-				maybe_emit_mod(&prog, AUX_REG, real_src_reg, is64);
-				EMIT2(simple_alu_opcodes[BPF_OP(insn->imm)],
-				      add_2reg(0xC0, AUX_REG, real_src_reg));
-				/* Attempt to swap in new value */
-				err = emit_atomic_rmw(&prog, BPF_CMPXCHG,
-						      real_dst_reg, AUX_REG,
-						      insn->off,
-						      BPF_SIZE(insn->code));
-				if (WARN_ON(err))
+				err = emit_atomic_fetch_rmw(&prog, insn, real_dst_reg,
+							    real_src_reg, -1, NULL,
+							    NULL);
+				if (err)
 					return err;
-				/*
-				 * ZF tells us whether we won the race. If it's
-				 * cleared we need to try again.
-				 */
-				EMIT2(X86_JNE, -(prog - branch_target) - 2);
-				/* Return the pre-modification value */
-				emit_mov_reg(&prog, is64, real_src_reg, BPF_REG_0);
-				/* Restore R0 after clobbering RAX */
-				emit_mov_reg(&prog, true, BPF_REG_0, BPF_REG_AX);
 				break;
 			}
 
@@ -2926,6 +2989,51 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 			fallthrough;
 		case BPF_STX | BPF_PROBE_ATOMIC | BPF_W:
 		case BPF_STX | BPF_PROBE_ATOMIC | BPF_DW:
+			if (is_atomic_fetch_op(insn)) {
+				u32 real_src_reg = src_reg, real_dst_reg = dst_reg;
+				u8 *fault[2], *resume;
+				int j;
+
+				/*
+				 * An access through a number has its address in
+				 * BPF_REG_AX (see arena_scalar_access()), where the
+				 * loop below has to save R0.
+				 */
+				if (dst_reg == BPF_REG_AX)
+					return -EOPNOTSUPP;
+
+				/* Will need RAX as a CMPXCHG operand so save R0 */
+				emit_mov_reg(&prog, true, BPF_REG_AX, BPF_REG_0);
+				if (src_reg == BPF_REG_0)
+					real_src_reg = BPF_REG_AX;
+				if (dst_reg == BPF_REG_0)
+					real_dst_reg = BPF_REG_AX;
+
+				err = emit_atomic_fetch_rmw(&prog, insn, real_dst_reg,
+							    real_src_reg, X86_REG_R12,
+							    fault, &resume);
+				if (err)
+					return err;
+
+				/*
+				 * The page may be unmapped between the load and the
+				 * CMPXCHG, so both need an entry. Both report a write,
+				 * since the BPF insn is a read-modify-write.
+				 */
+				for (j = 0; j < 2; j++) {
+					err = emit_arena_exentry(bpf_prog, image, rw_image,
+								 &excnt,
+								 image + proglen +
+									 (fault[j] - temp),
+								 resume - fault[j],
+								 reg2pt_regs[real_src_reg],
+								 reg2pt_regs[real_dst_reg],
+								 true, insn->off);
+					if (err)
+						return err;
+				}
+				break;
+			}
 			start_of_ldx = prog;
 
 			if (bpf_atomic_is_load_store(insn))
@@ -4353,6 +4461,21 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
 		padding = true;
 		goto skip_init_addrs;
 	}
+
+	/*
+	 * An arena fetching AND/OR/XOR is lowered to a CMPXCHG loop whose load
+	 * and CMPXCHG can both fault, one more entry than the verifier reserved.
+	 * Only reachable on the first pass, so the count is adjusted once.
+	 */
+	for (i = 0; prog->aux->arena && i < prog->len; i++) {
+		const struct bpf_insn *insn = &prog->insnsi[i];
+
+		if (BPF_CLASS(insn->code) == BPF_STX &&
+		    BPF_MODE(insn->code) == BPF_PROBE_ATOMIC &&
+		    is_atomic_fetch_op(insn))
+			prog->aux->num_exentries++;
+	}
+
 	addrs = kvmalloc_objs(*addrs, prog->len + 1);
 	if (!addrs)
 		goto out_addrs;
@@ -4407,7 +4530,9 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
 			 * The number of entries in extable is the number of BPF_LDX
 			 * insns that access kernel memory via "pointer to BTF type".
 			 * The verifier changed their opcode from LDX|MEM|size
-			 * to LDX|PROBE_MEM|size to make JITing easier.
+			 * to LDX|PROBE_MEM|size to make JITing easier. Arena
+			 * accesses add one each, and an arena fetching AND/OR/XOR
+			 * one more, see the adjustment of num_exentries above.
 			 */
 			u32 align = __alignof__(struct exception_table_entry);
 			u32 extable_size = prog->aux->num_exentries *
@@ -4681,16 +4806,6 @@ bool bpf_jit_supports_arena_scalar(void)
 
 bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)
 {
-	if (!in_arena)
-		return true;
-	switch (insn->code) {
-	case BPF_STX | BPF_ATOMIC | BPF_W:
-	case BPF_STX | BPF_ATOMIC | BPF_DW:
-		if (insn->imm == (BPF_AND | BPF_FETCH) ||
-		    insn->imm == (BPF_OR | BPF_FETCH) ||
-		    insn->imm == (BPF_XOR | BPF_FETCH))
-			return false;
-	}
 	return true;
 }
 
diff --git a/tools/sched_ext/include/scx/cid.bpf.h b/tools/sched_ext/include/scx/cid.bpf.h
index 69fb4e97bc771..d0d4cea19e29f 100644
--- a/tools/sched_ext/include/scx/cid.bpf.h
+++ b/tools/sched_ext/include/scx/cid.bpf.h
@@ -3,8 +3,8 @@
  * BPF-side helpers for cids and cmasks. See kernel/sched/ext/cid.h for the
  * authoritative layout and semantics. The BPF-side helpers use the cmask_*
  * naming (no scx_ prefix); cmask is the SCX bitmap type so the prefix is
- * redundant in BPF code. Atomics use __sync_val_compare_and_swap and every
- * helper is inline (no .c counterpart).
+ * redundant in BPF code. Atomics use the __sync_* builtins and every helper
+ * is inline (no .c counterpart).
  *
  * Included by scx/common.bpf.h; don't include directly.
  *
@@ -129,101 +129,38 @@ static __always_inline bool cmask_test(u32 cid, const struct scx_cmask __arena *
 	return *__cmask_word(cid, m) & BIT_U64(cid & 63);
 }
 
-/*
- * x86 BPF JIT rejects BPF_OR | BPF_FETCH and BPF_AND | BPF_FETCH on arena
- * pointers (see bpf_jit_supports_insn() in arch/x86/net/bpf_jit_comp.c). Only
- * BPF_CMPXCHG / BPF_XCHG / BPF_ADD with FETCH are allowed. Implement
- * test_and_{set,clear} and the atomic set/clear via a cmpxchg loop.
- *
- * CMASK_CAS_TRIES is sized so exhausting it means seconds of real spinning
- * on one word - past any plausible contention. Abort hard.
- */
-#define CMASK_CAS_TRIES		(1U << 23)
-
 static __always_inline void cmask_set(u32 cid, struct scx_cmask __arena *m)
 {
-	u64 __arena *w;
-	u64 bit, old, new;
-	u32 i;
-
 	if (!__cmask_contains(cid, m))
 		return;
-	w = __cmask_word(cid, m);
-	bit = BIT_U64(cid & 63);
-	bpf_for(i, 0, CMASK_CAS_TRIES) {
-		old = *w;
-		if (old & bit)
-			return;
-		new = old | bit;
-		if (__sync_val_compare_and_swap(w, old, new) == old)
-			return;
-	}
-	scx_bpf_error("cmask_set CAS exhausted at cid %u", cid);
+	__sync_fetch_and_or(__cmask_word(cid, m), BIT_U64(cid & 63));
 }
 
 static __always_inline void cmask_clear(u32 cid, struct scx_cmask __arena *m)
 {
-	u64 __arena *w;
-	u64 bit, old, new;
-	u32 i;
-
 	if (!__cmask_contains(cid, m))
 		return;
-	w = __cmask_word(cid, m);
-	bit = BIT_U64(cid & 63);
-	bpf_for(i, 0, CMASK_CAS_TRIES) {
-		old = *w;
-		if (!(old & bit))
-			return;
-		new = old & ~bit;
-		if (__sync_val_compare_and_swap(w, old, new) == old)
-			return;
-	}
-	scx_bpf_error("cmask_clear CAS exhausted at cid %u", cid);
+	__sync_fetch_and_and(__cmask_word(cid, m), ~BIT_U64(cid & 63));
 }
 
 static __always_inline bool cmask_test_and_set(u32 cid, struct scx_cmask __arena *m)
 {
-	u64 __arena *w;
-	u64 bit, old, new;
-	u32 i;
+	u64 bit;
 
 	if (!__cmask_contains(cid, m))
 		return false;
-	w = __cmask_word(cid, m);
 	bit = BIT_U64(cid & 63);
-	bpf_for(i, 0, CMASK_CAS_TRIES) {
-		old = *w;
-		if (old & bit)
-			return true;
-		new = old | bit;
-		if (__sync_val_compare_and_swap(w, old, new) == old)
-			return false;
-	}
-	scx_bpf_error("cmask_test_and_set CAS exhausted at cid %u", cid);
-	return false;
+	return __sync_fetch_and_or(__cmask_word(cid, m), bit) & bit;
 }
 
 static __always_inline bool cmask_test_and_clear(u32 cid, struct scx_cmask __arena *m)
 {
-	u64 __arena *w;
-	u64 bit, old, new;
-	u32 i;
+	u64 bit;
 
 	if (!__cmask_contains(cid, m))
 		return false;
-	w = __cmask_word(cid, m);
 	bit = BIT_U64(cid & 63);
-	bpf_for(i, 0, CMASK_CAS_TRIES) {
-		old = *w;
-		if (!(old & bit))
-			return false;
-		new = old & ~bit;
-		if (__sync_val_compare_and_swap(w, old, new) == old)
-			return true;
-	}
-	scx_bpf_error("cmask_test_and_clear CAS exhausted at cid %u", cid);
-	return false;
+	return __sync_fetch_and_and(__cmask_word(cid, m), ~bit) & bit;
 }
 
 static __always_inline void __cmask_set(u32 cid, struct scx_cmask __arena *m)
diff --git a/tools/testing/selftests/bpf/prog_tests/arena_atomics.c b/tools/testing/selftests/bpf/prog_tests/arena_atomics.c
index 1ad5d03d07adb..81f6e554a8c12 100644
--- a/tools/testing/selftests/bpf/prog_tests/arena_atomics.c
+++ b/tools/testing/selftests/bpf/prog_tests/arena_atomics.c
@@ -68,6 +68,12 @@ static void test_and(struct arena_atomics *skel)
 
 	ASSERT_EQ(skel->arena->and64_value, 0x010ull << 32, "and64_value");
 	ASSERT_EQ(skel->arena->and32_value, 0x010, "and32_value");
+
+	ASSERT_EQ(skel->arena->and64_result, 0x110ull << 32, "and64_result");
+	ASSERT_EQ(skel->arena->and32_result, 0x110, "and32_result");
+
+	ASSERT_EQ(skel->arena->and64_noreturn_value, 0x010ull << 32, "and64_noreturn_value");
+	ASSERT_EQ(skel->arena->and32_noreturn_value, 0x010, "and32_noreturn_value");
 }
 
 static void test_or(struct arena_atomics *skel)
@@ -85,6 +91,12 @@ static void test_or(struct arena_atomics *skel)
 
 	ASSERT_EQ(skel->arena->or64_value, 0x111ull << 32, "or64_value");
 	ASSERT_EQ(skel->arena->or32_value, 0x111, "or32_value");
+
+	ASSERT_EQ(skel->arena->or64_result, 0x110ull << 32, "or64_result");
+	ASSERT_EQ(skel->arena->or32_result, 0x110, "or32_result");
+
+	ASSERT_EQ(skel->arena->or64_noreturn_value, 0x111ull << 32, "or64_noreturn_value");
+	ASSERT_EQ(skel->arena->or32_noreturn_value, 0x111, "or32_noreturn_value");
 }
 
 static void test_xor(struct arena_atomics *skel)
@@ -102,6 +114,12 @@ static void test_xor(struct arena_atomics *skel)
 
 	ASSERT_EQ(skel->arena->xor64_value, 0x101ull << 32, "xor64_value");
 	ASSERT_EQ(skel->arena->xor32_value, 0x101, "xor32_value");
+
+	ASSERT_EQ(skel->arena->xor64_result, 0x110ull << 32, "xor64_result");
+	ASSERT_EQ(skel->arena->xor32_result, 0x110, "xor32_result");
+
+	ASSERT_EQ(skel->arena->xor64_noreturn_value, 0x101ull << 32, "xor64_noreturn_value");
+	ASSERT_EQ(skel->arena->xor32_noreturn_value, 0x101, "xor32_noreturn_value");
 }
 
 static void test_cmpxchg(struct arena_atomics *skel)
@@ -146,6 +164,27 @@ static void test_xchg(struct arena_atomics *skel)
 	ASSERT_EQ(skel->arena->xchg32_result, 1, "xchg32_result");
 }
 
+static void test_fetch_r0(struct arena_atomics *skel)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, topts);
+	int err, prog_fd;
+
+	/* No need to attach it, just run it directly */
+	prog_fd = bpf_program__fd(skel->progs.fetch_r0);
+	err = bpf_prog_test_run_opts(prog_fd, &topts);
+	if (!ASSERT_OK(err, "test_run_opts err"))
+		return;
+	if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
+		return;
+
+	ASSERT_EQ(skel->arena->fetch_src_r0_value, 0x11100000111ull, "fetch_src_r0_value");
+	ASSERT_EQ(skel->arena->fetch_src_r0_result, 0x11000000110ull, "fetch_src_r0_result");
+
+	ASSERT_EQ(skel->arena->fetch_dst_r0_value, 0x11100000111ull, "fetch_dst_r0_value");
+	ASSERT_EQ(skel->arena->fetch_dst_r0_result, 0x11000000110ull, "fetch_dst_r0_result");
+	ASSERT_EQ(skel->arena->fetch_dst_r0_readback, 0x11100000111ull, "fetch_dst_r0_readback");
+}
+
 static void test_uaf(struct arena_atomics *skel)
 {
 	LIBBPF_OPTS(bpf_test_run_opts, topts);
@@ -160,6 +199,12 @@ static void test_uaf(struct arena_atomics *skel)
 		return;
 
 	ASSERT_EQ(skel->arena->uaf_recovery_fails, 0, "uaf_recovery_fails");
+
+	/* A fault clears the fetched register and leaves the address alone */
+	ASSERT_EQ(skel->arena->uaf_r1_after_dst_r0, 0, "uaf_r1_after_dst_r0");
+	ASSERT_EQ(skel->arena->uaf_r0_after_dst_r0, skel->arena->uaf_r0_before_dst_r0,
+		  "uaf_r0_after_dst_r0");
+	ASSERT_EQ(skel->arena->uaf_r0_after_src_r0, 0, "uaf_r0_after_src_r0");
 }
 
 static void test_load_acquire(struct arena_atomics *skel)
@@ -256,6 +301,8 @@ void serial_test_arena_atomics(void)
 		test_cmpxchg(skel);
 	if (test__start_subtest("xchg"))
 		test_xchg(skel);
+	if (test__start_subtest("fetch_r0"))
+		test_fetch_r0(skel);
 	if (test__start_subtest("uaf"))
 		test_uaf(skel);
 	if (test__start_subtest("load_acquire"))
diff --git a/tools/testing/selftests/bpf/progs/arena_atomics.c b/tools/testing/selftests/bpf/progs/arena_atomics.c
index 73bc2b835f3fe..e89b468fc6226 100644
--- a/tools/testing/selftests/bpf/progs/arena_atomics.c
+++ b/tools/testing/selftests/bpf/progs/arena_atomics.c
@@ -91,13 +91,17 @@ int sub(const void *ctx)
 	return 0;
 }
 
-#ifdef __BPF_FEATURE_ATOMIC_MEM_ORDERING
-_Atomic __u64 __arena_global and64_value = (0x110ull << 32);
-_Atomic __u32 __arena_global and32_value = 0x110;
-#else
+/*
+ * A relaxed __c11_atomic_fetch_*() whose result is discarded is what selects
+ * the non-fetching insn, so each operation keeps a *_noreturn_value of each
+ * width to cover that lowering as well.
+ */
 __u64 __arena_global and64_value = (0x110ull << 32);
+__u64 __arena_global and64_result = 0;
+_Atomic __u64 __arena_global and64_noreturn_value = (0x110ull << 32);
 __u32 __arena_global and32_value = 0x110;
-#endif
+__u32 __arena_global and32_result = 0;
+_Atomic __u32 __arena_global and32_noreturn_value = 0x110;
 
 SEC("raw_tp/sys_enter")
 int and(const void *ctx)
@@ -105,25 +109,22 @@ int and(const void *ctx)
 	if (pid != (bpf_get_current_pid_tgid() >> 32))
 		return 0;
 #ifdef ENABLE_ATOMICS_TESTS
-#ifdef __BPF_FEATURE_ATOMIC_MEM_ORDERING
-	__c11_atomic_fetch_and(&and64_value, 0x011ull << 32, memory_order_relaxed);
-	__c11_atomic_fetch_and(&and32_value, 0x011, memory_order_relaxed);
-#else
-	__sync_fetch_and_and(&and64_value, 0x011ull << 32);
-	__sync_fetch_and_and(&and32_value, 0x011);
-#endif
+	and64_result = __sync_fetch_and_and(&and64_value, 0x011ull << 32);
+	and32_result = __sync_fetch_and_and(&and32_value, 0x011);
+	__c11_atomic_fetch_and(&and64_noreturn_value, 0x011ull << 32, memory_order_relaxed);
+	__c11_atomic_fetch_and(&and32_noreturn_value, 0x011, memory_order_relaxed);
 #endif
 
 	return 0;
 }
 
-#ifdef __BPF_FEATURE_ATOMIC_MEM_ORDERING
-_Atomic __u32 __arena_global or32_value = 0x110;
-_Atomic __u64 __arena_global or64_value = (0x110ull << 32);
-#else
+/* u32s first: .addr_space.1 must not have holes, see the clang-17 note below */
 __u32 __arena_global or32_value = 0x110;
+__u32 __arena_global or32_result = 0;
+_Atomic __u32 __arena_global or32_noreturn_value = 0x110;
 __u64 __arena_global or64_value = (0x110ull << 32);
-#endif
+__u64 __arena_global or64_result = 0;
+_Atomic __u64 __arena_global or64_noreturn_value = (0x110ull << 32);
 
 SEC("raw_tp/sys_enter")
 int or(const void *ctx)
@@ -131,25 +132,21 @@ int or(const void *ctx)
 	if (pid != (bpf_get_current_pid_tgid() >> 32))
 		return 0;
 #ifdef ENABLE_ATOMICS_TESTS
-#ifdef __BPF_FEATURE_ATOMIC_MEM_ORDERING
-	__c11_atomic_fetch_or(&or64_value, 0x011ull << 32, memory_order_relaxed);
-	__c11_atomic_fetch_or(&or32_value, 0x011, memory_order_relaxed);
-#else
-	__sync_fetch_and_or(&or64_value, 0x011ull << 32);
-	__sync_fetch_and_or(&or32_value, 0x011);
-#endif
+	or64_result = __sync_fetch_and_or(&or64_value, 0x011ull << 32);
+	or32_result = __sync_fetch_and_or(&or32_value, 0x011);
+	__c11_atomic_fetch_or(&or64_noreturn_value, 0x011ull << 32, memory_order_relaxed);
+	__c11_atomic_fetch_or(&or32_noreturn_value, 0x011, memory_order_relaxed);
 #endif
 
 	return 0;
 }
 
-#ifdef __BPF_FEATURE_ATOMIC_MEM_ORDERING
-_Atomic __u64 __arena_global xor64_value = (0x110ull << 32);
-_Atomic __u32 __arena_global xor32_value = 0x110;
-#else
 __u64 __arena_global xor64_value = (0x110ull << 32);
+__u64 __arena_global xor64_result = 0;
+_Atomic __u64 __arena_global xor64_noreturn_value = (0x110ull << 32);
 __u32 __arena_global xor32_value = 0x110;
-#endif
+__u32 __arena_global xor32_result = 0;
+_Atomic __u32 __arena_global xor32_noreturn_value = 0x110;
 
 SEC("raw_tp/sys_enter")
 int xor(const void *ctx)
@@ -157,13 +154,10 @@ int xor(const void *ctx)
 	if (pid != (bpf_get_current_pid_tgid() >> 32))
 		return 0;
 #ifdef ENABLE_ATOMICS_TESTS
-#ifdef __BPF_FEATURE_ATOMIC_MEM_ORDERING
-	__c11_atomic_fetch_xor(&xor64_value, 0x011ull << 32, memory_order_relaxed);
-	__c11_atomic_fetch_xor(&xor32_value, 0x011, memory_order_relaxed);
-#else
-	__sync_fetch_and_xor(&xor64_value, 0x011ull << 32);
-	__sync_fetch_and_xor(&xor32_value, 0x011);
-#endif
+	xor64_result = __sync_fetch_and_xor(&xor64_value, 0x011ull << 32);
+	xor32_result = __sync_fetch_and_xor(&xor32_value, 0x011);
+	__c11_atomic_fetch_xor(&xor64_noreturn_value, 0x011ull << 32, memory_order_relaxed);
+	__c11_atomic_fetch_xor(&xor32_noreturn_value, 0x011, memory_order_relaxed);
 #endif
 
 	return 0;
@@ -213,23 +207,93 @@ int xchg(const void *ctx)
 	return 0;
 }
 
+__u64 __arena_global fetch_src_r0_value = 0x11000000110ull;
+__u64 __arena_global fetch_src_r0_result = 0;
+__u64 __arena_global fetch_dst_r0_value = 0x11000000110ull;
+__u64 __arena_global fetch_dst_r0_result = 0;
+__u64 __arena_global fetch_dst_r0_readback = 0;
+
+/*
+ * A fetching OR with the operand in r0, and one with the arena pointer in r0.
+ * The x86 JIT needs RAX for its CMPXCHG loop and substitutes BPF_REG_AX for
+ * whichever of the two is r0, so both have to keep working. Hand-written
+ * because clang picks its own registers and will not reliably emit either.
+ */
+SEC("raw_tp/sys_enter")
+int fetch_r0(const void *ctx)
+{
+	if (pid != (bpf_get_current_pid_tgid() >> 32))
+		return 0;
+#if defined(ENABLE_ATOMICS_TESTS) && defined(__BPF_FEATURE_ADDR_SPACE_CAST)
+	asm volatile (
+	"r1 = %[fetch_src_r0_value] ll;"
+	"r1 = addr_space_cast(r1, 0x0, 0x1);"
+	"r0 = 0x1100000011 ll;"
+	".8byte %[fetch_src_r0_insn];"
+	"r2 = %[fetch_src_r0_result] ll;"
+	"r2 = addr_space_cast(r2, 0x0, 0x1);"
+	"*(u64 *)(r2 + 0) = r0;"
+	:
+	: __imm_addr(fetch_src_r0_value),
+	  __imm_insn(fetch_src_r0_insn,
+		     BPF_ATOMIC_OP(BPF_DW, BPF_OR | BPF_FETCH, BPF_REG_1, BPF_REG_0, 0)),
+	  __imm_addr(fetch_src_r0_result)
+	: __clobber_all);
+
+	asm volatile (
+	"r0 = %[fetch_dst_r0_value] ll;"
+	"r0 = addr_space_cast(r0, 0x0, 0x1);"
+	"r1 = 0x1100000011 ll;"
+	".8byte %[fetch_dst_r0_insn];"
+	"r2 = %[fetch_dst_r0_result] ll;"
+	"r2 = addr_space_cast(r2, 0x0, 0x1);"
+	"*(u64 *)(r2 + 0) = r1;"
+	/* r0 is only read by the atomic, so it must still be the pointer. */
+	"r3 = *(u64 *)(r0 + 0);"
+	"r2 = %[fetch_dst_r0_readback] ll;"
+	"r2 = addr_space_cast(r2, 0x0, 0x1);"
+	"*(u64 *)(r2 + 0) = r3;"
+	:
+	: __imm_addr(fetch_dst_r0_value),
+	  __imm_insn(fetch_dst_r0_insn,
+		     BPF_ATOMIC_OP(BPF_DW, BPF_OR | BPF_FETCH, BPF_REG_0, BPF_REG_1, 0)),
+	  __imm_addr(fetch_dst_r0_result),
+	  __imm_addr(fetch_dst_r0_readback)
+	: __clobber_all);
+#endif
+
+	return 0;
+}
+
 __u64 __arena_global uaf_sink;
 volatile __u64 __arena_global uaf_recovery_fails;
+/* Register state after the r0 forms below fault, checked by the test */
+__u64 __arena_global uaf_r1_after_dst_r0;
+__u64 __arena_global uaf_r0_before_dst_r0;
+__u64 __arena_global uaf_r0_after_dst_r0;
+__u64 __arena_global uaf_r0_after_src_r0;
 
 SEC("syscall")
 int uaf(const void *ctx)
 {
 	if (pid != (bpf_get_current_pid_tgid() >> 32))
 		return 0;
+/*
+ * __BPF_FEATURE_ADDR_SPACE_CAST is required by the asm below, which spells
+ * addr_space_cast textually. That and the mnemonic both arrived in LLVM 19.
+ */
 #if defined(ENABLE_ATOMICS_TESTS) && !defined(__TARGET_ARCH_arm64) && \
-    !defined(__TARGET_ARCH_x86)
+	defined(__BPF_FEATURE_ADDR_SPACE_CAST)
 	__u32 __arena *page32;
 	__u64 __arena *page64;
 	void __arena *page;
 
 	page = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
 	bpf_arena_free_pages(&arena, page, 1);
-	uaf_recovery_fails = 24;
+	uaf_recovery_fails = 26;
+	uaf_r1_after_dst_r0 = 1;
+	uaf_r0_after_dst_r0 = 1;
+	uaf_r0_after_src_r0 = 1;
 
 	page32 = (__u32 __arena *)page;
 	uaf_sink += __sync_fetch_and_add(page32, 1);
@@ -282,6 +346,50 @@ int uaf(const void *ctx)
 	uaf_recovery_fails -= 1;
 	uaf_sink += __sync_lock_test_and_set(page64, 1);
 	uaf_recovery_fails -= 1;
+
+	/*
+	 * The x86 JIT needs RAX for the CMPXCHG loop it lowers a fetching
+	 * AND/OR/XOR into, and substitutes BPF_REG_AX for r0 when r0 is either
+	 * operand. Only then does the fault fixup have to name that register,
+	 * so spell both forms out: clang picks its own and never produces them.
+	 */
+	asm volatile (
+	"r0 = %[page];"
+	"r0 = addr_space_cast(r0, 0x0, 0x1);"
+	"r1 = 1;"
+	"r2 = %[uaf_r0_before_dst_r0] ll;"
+	"r2 = addr_space_cast(r2, 0x0, 0x1);"
+	"*(u64 *)(r2 + 0) = r0;"
+	".8byte %[fetch_dst_r0];"
+	"r2 = %[uaf_r0_after_dst_r0] ll;"
+	"r2 = addr_space_cast(r2, 0x0, 0x1);"
+	"*(u64 *)(r2 + 0) = r0;"
+	"r2 = %[uaf_r1_after_dst_r0] ll;"
+	"r2 = addr_space_cast(r2, 0x0, 0x1);"
+	"*(u64 *)(r2 + 0) = r1;"
+	: : [page]"r"(page64),
+	    __imm_insn(fetch_dst_r0,
+		       BPF_ATOMIC_OP(BPF_DW, BPF_OR | BPF_FETCH, BPF_REG_0, BPF_REG_1, 0)),
+	    __imm_addr(uaf_r0_before_dst_r0),
+	    __imm_addr(uaf_r0_after_dst_r0),
+	    __imm_addr(uaf_r1_after_dst_r0)
+	: "r0", "r1", "r2", "memory");
+	uaf_recovery_fails -= 1;
+
+	asm volatile (
+	"r1 = %[page];"
+	"r1 = addr_space_cast(r1, 0x0, 0x1);"
+	"r0 = 1;"
+	".8byte %[fetch_src_r0];"
+	"r2 = %[uaf_r0_after_src_r0] ll;"
+	"r2 = addr_space_cast(r2, 0x0, 0x1);"
+	"*(u64 *)(r2 + 0) = r0;"
+	: : [page]"r"(page64),
+	    __imm_insn(fetch_src_r0,
+		       BPF_ATOMIC_OP(BPF_DW, BPF_OR | BPF_FETCH, BPF_REG_1, BPF_REG_0, 0)),
+	    __imm_addr(uaf_r0_after_src_r0)
+	: "r0", "r1", "r2", "memory");
+	uaf_recovery_fails -= 1;
 #endif
 
 	return 0;
diff --git a/tools/testing/selftests/bpf/progs/verifier_arena_scalar.c b/tools/testing/selftests/bpf/progs/verifier_arena_scalar.c
index bebc37f501b7e..7a7e06d9596ce 100644
--- a/tools/testing/selftests/bpf/progs/verifier_arena_scalar.c
+++ b/tools/testing/selftests/bpf/progs/verifier_arena_scalar.c
@@ -256,6 +256,104 @@ __naked void atomics(void)
 	: __clobber_all);
 }
 
+SEC("syscall")
+__flag(BPF_F_ARENA_SCALAR) __arch_arm64
+__description("arena_scalar: fetching and/or/xor")
+__success __retval(0)
+__load_if_JITed()
+__naked void fetch_and_or_xor(void)
+{
+	asm volatile ("					\
+	r1 = %[arena] ll;				\
+	r2 = 0;						\
+	r3 = 1;						\
+	r4 = -1;					\
+	r5 = 0;						\
+	call %[bpf_arena_alloc_pages];			\
+	r6 = r0;					\
+	r0 = 100;					\
+	if r6 == 0 goto 9f;				\
+	*(u64 *)(r6 + 8) = 0x110;			\
+	r1 = 0x011;					\
+	.8byte %[fetch_or_insn]; /* r1 = atomic_fetch_or((u64 *)(r6 + 8), r1) */ \
+	r0 = 1;						\
+	if r1 != 0x110 goto 9f;				\
+	r1 = 0x101;					\
+	.8byte %[fetch_and_insn]; /* r1 = atomic_fetch_and((u64 *)(r6 + 8), r1) */ \
+	r0 = 2;						\
+	if r1 != 0x111 goto 9f;				\
+	r1 = 0x011;					\
+	.8byte %[fetch_xor_insn]; /* r1 = atomic_fetch_xor((u64 *)(r6 + 8), r1) */ \
+	r0 = 3;						\
+	if r1 != 0x101 goto 9f;				\
+	r2 = *(u64 *)(r6 + 8);				\
+	r0 = 4;						\
+	if r2 != 0x110 goto 9f;				\
+	r0 = 0;						\
+9:	exit;						\
+"	:
+	: __imm_addr(arena),
+	  __imm(bpf_arena_alloc_pages),
+	  __imm_insn(fetch_or_insn, BPF_ATOMIC_OP(BPF_DW, BPF_OR | BPF_FETCH,
+						  BPF_REG_6, BPF_REG_1, 8)),
+	  __imm_insn(fetch_and_insn, BPF_ATOMIC_OP(BPF_DW, BPF_AND | BPF_FETCH,
+						   BPF_REG_6, BPF_REG_1, 8)),
+	  __imm_insn(fetch_xor_insn, BPF_ATOMIC_OP(BPF_DW, BPF_XOR | BPF_FETCH,
+						   BPF_REG_6, BPF_REG_1, 8))
+	: __clobber_all);
+}
+
+/*
+ * The x86-64 JIT saves R0 in BPF_REG_AX to lower these, which is where an
+ * access through a number keeps its address, so it refuses them.
+ */
+SEC("syscall")
+__flag(BPF_F_ARENA_SCALAR) __arch_x86_64
+__description("arena_scalar: fetching and/or/xor refused on x86-64")
+__failure
+__load_if_JITed()
+__naked void fetch_and_or_xor_x86(void)
+{
+	asm volatile ("					\
+	r1 = %[arena] ll;				\
+	r2 = 0;						\
+	r3 = 1;						\
+	r4 = -1;					\
+	r5 = 0;						\
+	call %[bpf_arena_alloc_pages];			\
+	r6 = r0;					\
+	r0 = 100;					\
+	if r6 == 0 goto 9f;				\
+	*(u64 *)(r6 + 8) = 0x110;			\
+	r1 = 0x011;					\
+	.8byte %[fetch_or_insn]; /* r1 = atomic_fetch_or((u64 *)(r6 + 8), r1) */ \
+	r0 = 1;						\
+	if r1 != 0x110 goto 9f;				\
+	r1 = 0x101;					\
+	.8byte %[fetch_and_insn]; /* r1 = atomic_fetch_and((u64 *)(r6 + 8), r1) */ \
+	r0 = 2;						\
+	if r1 != 0x111 goto 9f;				\
+	r1 = 0x011;					\
+	.8byte %[fetch_xor_insn]; /* r1 = atomic_fetch_xor((u64 *)(r6 + 8), r1) */ \
+	r0 = 3;						\
+	if r1 != 0x101 goto 9f;				\
+	r2 = *(u64 *)(r6 + 8);				\
+	r0 = 4;						\
+	if r2 != 0x110 goto 9f;				\
+	r0 = 0;						\
+9:	exit;						\
+"	:
+	: __imm_addr(arena),
+	  __imm(bpf_arena_alloc_pages),
+	  __imm_insn(fetch_or_insn, BPF_ATOMIC_OP(BPF_DW, BPF_OR | BPF_FETCH,
+						  BPF_REG_6, BPF_REG_1, 8)),
+	  __imm_insn(fetch_and_insn, BPF_ATOMIC_OP(BPF_DW, BPF_AND | BPF_FETCH,
+						   BPF_REG_6, BPF_REG_1, 8)),
+	  __imm_insn(fetch_xor_insn, BPF_ATOMIC_OP(BPF_DW, BPF_XOR | BPF_FETCH,
+						   BPF_REG_6, BPF_REG_1, 8))
+	: __clobber_all);
+}
+
 SEC("syscall")
 __arena_scalar
 __description("arena_scalar: cmpxchg through r0")

25/3 2026/10/14 19:07 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 17171
							output: 51
							thoughts: 1621
						

Thoughts:
**KMSAN Fuzzing Session Decision**

Alright, let's break this down. My goal is to determine if this patch necessitates a dedicated KMSAN fuzzing session (NeedsKMSAN = true) or if the standard checks are sufficient (NeedsKMSAN = false). KMSAN, as I know, is for detecting reads of uninitialized memory and information leaks. Running a KMSAN session is costly, so I only want to trigger it if the patch introduces risks KMSAN uniquely catches.

This patch adds support for atomic fetch operations in the x86 BPF JIT, specifically within `arch/x86/net/bpf_jit_comp.c`. It's enabling atomic operations on arena memory. The key functions are `is_atomic_fetch_op`, `emit_atomic_fetch_rmw`, and `emit_arena_exentry`. It also simplifies atomic operations in `tools/sched_ext/include/scx/cid.bpf.h`, and includes associated selftests.

Now, I need to focus on potential memory safety issues that only KMSAN would find. I'm looking for places where uninitialized data could be read. I need to check for things like kernel structures sent to user space where fields might be uninitialized, conditional logic dependent on uninitialized variables, the allocation/initialization of complex data structures where uninitialized fields could be read, and the introduction of bounds checks or logic that might expose uninitialized bytes. If the change just risks out-of-bounds access, use-after-free, or typical bugs, KASAN or standard checkers should catch it.

Let's dig into `arch/x86/net/bpf_jit_comp.c` and specifically `emit_arena_exentry`. Here, the function populates `struct exception_table_entry *ex`. I can see that all fields of `ex` are explicitly written. The code in `bpf_int_jit_compile` also increments `prog->aux->num_exentries` and allocates an `extable`. Since the content of `ex` gets fully written, I don't see an obvious uninitialized memory read here directly. The `extable` allocation is part of the JIT image so that doesn't appear concerning either. It looks like `extable` contents may come from the bpf verifier; I would expect it to be safe.

I'm not seeing any immediate red flags for KMSAN related to uninitialized memory or information leaks.

Given what I've seen so far, and absent additional information, I'm leaning toward setting **NeedsKMSAN = false**. The patch's primary focus is on atomic operations and JIT compilation, and I haven't identified any obvious paths where uninitialized memory would be read or leaked. While it is always good practice to check if the patch adds potential hazards, this patch doesn't look like it's introducing any risks that would be uniquely detected by KMSAN.



26/3 2026/10/14 19:08 tool grepper
0m Args:
{
  "Expression": "prog-\u003eaux-\u003eextable",
  "PathPrefixes": [
    "arch/x86/net/bpf_jit_comp.c",
    "kernel/bpf/"
  ]
}

Results:
{
  "Output": "arch/x86/net/bpf_jit_comp.c=1835=static int emit_arena_exentry(struct bpf_prog *bpf_prog, u8 *image, u8 *rw_image,\n--\narch/x86/net/bpf_jit_comp.c-1841-\narch/x86/net/bpf_jit_comp.c:1842:\tif (!bpf_prog-\u003eaux-\u003eextable)\narch/x86/net/bpf_jit_comp.c-1843-\t\treturn 0;\n--\narch/x86/net/bpf_jit_comp.c-1848-\t}\narch/x86/net/bpf_jit_comp.c:1849:\tex = \u0026bpf_prog-\u003eaux-\u003eextable[(*excnt)++];\narch/x86/net/bpf_jit_comp.c-1850-\n--\narch/x86/net/bpf_jit_comp.c=2119=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-2890-\narch/x86/net/bpf_jit_comp.c:2891:\t\t\t\tif (!bpf_prog-\u003eaux-\u003eextable)\narch/x86/net/bpf_jit_comp.c-2892-\t\t\t\t\tbreak;\n--\narch/x86/net/bpf_jit_comp.c-2897-\t\t\t\t}\narch/x86/net/bpf_jit_comp.c:2898:\t\t\t\tex = \u0026bpf_prog-\u003eaux-\u003eextable[excnt++];\narch/x86/net/bpf_jit_comp.c-2899-\n--\narch/x86/net/bpf_jit_comp.c=4410=struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog)\n--\narch/x86/net/bpf_jit_comp.c-4547-\t\t\t\tgoto out_addrs;\narch/x86/net/bpf_jit_comp.c:4548:\t\t\tprog-\u003eaux-\u003eextable = (void *) image + roundup(proglen, align);\narch/x86/net/bpf_jit_comp.c-4549-\t\t}\n--\nkernel/bpf/core.c=789=const struct exception_table_entry *search_bpf_extables(unsigned long addr)\n--\nkernel/bpf/core.c-800-\nkernel/bpf/core.c:801:\te = search_extable(prog-\u003eaux-\u003eextable, prog-\u003eaux-\u003enum_exentries, addr);\nkernel/bpf/core.c-802-out:\n--\nkernel/bpf/fixups.c=1259=static int jit_subprogs(struct bpf_verifier_env *env)\n--\nkernel/bpf/fixups.c-1496-\tprog-\u003ejited_len = func[0]-\u003ejited_len;\nkernel/bpf/fixups.c:1497:\tprog-\u003eaux-\u003eextable = func[0]-\u003eaux-\u003eextable;\nkernel/bpf/fixups.c-1498-\tprog-\u003eaux-\u003enum_exentries = func[0]-\u003eaux-\u003enum_exentries;\n"
}

27/3 2026/10/14 19:08 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 20369
							output: 45
							thoughts: 34
						

Thoughts:
Okay, let's dive into this. Here's how I'd approach that snippet, considering I'm trying to distill my understanding:

**Analyzing `arch/x86/net/bpf_jit_comp.c` (Lines 4530-4550)**

Alright, so I'm honing in on lines 4530-4550 within the `bpf_jit_comp.c` file, specifically within the `arch/x86/net` directory. Given the context, this is almost certainly dealing with JIT compilation of BPF (Berkeley Packet Filter) code, and specifically, the x86 architecture. Those lines are probably critical to understanding how BPF instructions are translated into x86 machine code – a core process for enhancing network performance through filtering and processing packets in the kernel.

I need to understand what those specific lines are *doing*. Are they part of the instruction selection phase? Are they handling a specific BPF opcode? Are they writing a particular x86 instruction sequence? Are they related to register allocation or instruction scheduling? Perhaps some form of optimization is happening there? I should be focusing on the logic, but considering that this is at the x86 architecture level, I need to know how the bits are working at that level.

I'm thinking that I should check:

1.  **Instruction Decoding:** I want to determine if the lines involve parsing the BPF instructions into an internal representation.
2.  **Code Generation:** I need to look for the translation of BPF instructions to x86 assembly.
3.  **Optimization:** I should check for optimizations, like peephole optimizations or constant folding at this level, as these could influence performance drastically.
4.  **Register usage:** I'll be looking for register allocation/spilling logic. How does the JIT compiler map BPF virtual registers to x86 physical registers?

Once I understand what those lines accomplish, I should be able to get a better understanding of the overall JIT compilation process and the x86 architecture-specific adaptations.



28/3 2026/10/14 19:08 tool read-file
0m Args:
{
  "File": "arch/x86/net/bpf_jit_comp.c",
  "FirstLine": 4525,
  "LineCount": 40
}

Results:
{
  "Contents": "4525:\t\t\t\t}\n4526:\t\t\t\tbreak;\n4527:\t\t\t}\n4528:\t\t\tif (proglen == oldproglen) {\n4529:\t\t\t\t/*\n4530:\t\t\t\t * The number of entries in extable is the number of BPF_LDX\n4531:\t\t\t\t * insns that access kernel memory via \"pointer to BTF type\".\n4532:\t\t\t\t * The verifier changed their opcode from LDX|MEM|size\n4533:\t\t\t\t * to LDX|PROBE_MEM|size to make JITing easier. Arena\n4534:\t\t\t\t * accesses add one each, and an arena fetching AND/OR/XOR\n4535:\t\t\t\t * one more, see the adjustment of num_exentries above.\n4536:\t\t\t\t */\n4537:\t\t\t\tu32 align = __alignof__(struct exception_table_entry);\n4538:\t\t\t\tu32 extable_size = prog-\u003eaux-\u003enum_exentries *\n4539:\t\t\t\t\tsizeof(struct exception_table_entry);\n4540:\t\n4541:\t\t\t\t/* allocate module memory for x86 insns and extable */\n4542:\t\t\t\theader = bpf_jit_binary_pack_alloc(roundup(proglen, align) + extable_size,\n4543:\t\t\t\t\t\t\t\t   \u0026image, align, \u0026rw_header, \u0026rw_image,\n4544:\t\t\t\t\t\t\t\t   jit_fill_hole,\n4545:\t\t\t\t\t\t\t\t   bpf_prog_was_classic(prog));\n4546:\t\t\t\tif (!header)\n4547:\t\t\t\t\tgoto out_addrs;\n4548:\t\t\t\tprog-\u003eaux-\u003eextable = (void *) image + roundup(proglen, align);\n4549:\t\t\t}\n4550:\t\t\toldproglen = proglen;\n4551:\t\t\tcond_resched();\n4552:\t\t}\n4553:\t\n4554:\t\tif (bpf_jit_enable \u003e 1)\n4555:\t\t\tbpf_jit_dump(prog-\u003elen, proglen, pass + 1, rw_image);\n4556:\t\n4557:\t\tif (image) {\n4558:\t\t\tif (!prog-\u003eis_func || extra_pass) {\n4559:\t\t\t\t/*\n4560:\t\t\t\t * bpf_jit_binary_pack_finalize fails in two scenarios:\n4561:\t\t\t\t *   1) header is not pointing to proper module memory;\n4562:\t\t\t\t *   2) the arch doesn't support bpf_arch_text_copy().\n4563:\t\t\t\t *\n4564:\t\t\t\t * Both cases are serious bugs and justify WARN_ON.\n"
}

29/3 2026/10/14 19:08 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 21573
							output: 38
							thoughts: 128
						

Thoughts:
**Examining the Exception Table Entry Mechanism**

Okay, let's dissect this. I need to understand how these exception table entries are actually constructed. From what I see here, `emit_arena_exentry` writes to `ex`, and the key operations seem to involve pointer manipulation, which I'll have to keep in mind. Specifically, I'm observing the following: `ex = &bpf_prog->aux->extable[(*excnt)++];` This looks like we're incrementing a counter and taking an entry from an exception table. I'm also seeing an adjustment: `ex = (void *)rw_image + ((void *)ex - (void *)image);` which seems to be performing a relocation or address adjustment. Further, the code writes to members of `ex`: `ex->insn = delta;` and `ex->data = ...;`, which is understandable, storing instruction delta values and data, and `ex->fixup = ...;` which likely refers to fixup information.

Now, I really need to refresh my memory on what `struct exception_table_entry` actually *is* on x86. I need to see the exact structure definition to understand what the `insn`, `data`, and `fixup` fields are storing and how they relate to the underlying architecture and exception handling. Let's look that up.



30/3 2026/10/14 19:08 tool grepper
0m Args:
{
  "Expression": "struct exception_table_entry \\{",
  "PathPrefixes": [
    "arch/x86/include/asm/"
  ]
}

Results:
{
  "Output": "arch/x86/include/asm/extable.h-22-\narch/x86/include/asm/extable.h:23:struct exception_table_entry {\narch/x86/include/asm/extable.h-24-\tint insn, fixup, data;\n"
}

31/3 2026/10/14 19:08 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 22117
							output: 45
							thoughts: 96
						

Thoughts:
Okay, let me break this down.

**Analyzing the Instruction Layout and Exception Handling in BPF JIT Compilation**

Alright, so I'm looking at this BPF JIT compilation setup. Specifically, I'm focusing on the data structures involved, and then how exception handling is designed when memory accesses within an arena fault. I see we have three `int` fields: `insn`, `fixup`, and `data`. Each is 32 bits, making it a total of 12 bytes across the structure, with no padding, which is good for performance. Importantly, I see that these are *explicitly* initialized, so there's no ambiguity there.

Now, let's switch gears and investigate exception handling, and *specifically* how things work if there's a fault during an arena access. I'm going to jump into `arch/x86/net/bpf_jit_comp.c`, and I'll focus around line 1748. I expect to find the logic that deals with these potential memory access issues.



32/3 2026/10/14 19:08 tool read-file
0m Args:
{
  "File": "arch/x86/net/bpf_jit_comp.c",
  "FirstLine": 1760,
  "LineCount": 75
}

Results:
{
  "Contents": "1760:\t *\n1761:\t * Bit layout of `fixup` (32-bit):\n1762:\t *\n1763:\t * +-----------+-------------+--------+-----------+---------+----------+\n1764:\t * | 31        | 30          | 29-24  |   23-16   |   15-8  |    7-0   |\n1765:\t * |           |             |        |           |         |          |\n1766:\t * | ARENA_ACC | ARENA_WRITE | Unused | ARENA_REG | DST_REG | INSN_LEN |\n1767:\t * +-----------+-------------+--------+-----------+---------+----------+\n1768:\t *\n1769:\t * - INSN_LEN (8 bits): How far past the faulting insn to resume. That is its own length\n1770:\t *                      for a single-insn access, but the distance to the end of the whole\n1771:\t *                      sequence where one BPF insn became several, as for the CMPXCHG loop\n1772:\t *                      of a fetching AND/OR/XOR. A sequence long enough to overflow this\n1773:\t *                      field would first have to exceed BPF_MAX_INSN_SIZE, which do_jit()\n1774:\t *                      rejects with -EFAULT before the image is used.\n1775:\t * - DST_REG  (8 bits): Offset of dst_reg from reg2pt_regs[] (max offset = 112 (fits in 8 bits)).\n1776:\t *                      This is set to DONT_CLEAR if the insn does not read into a register.\n1777:\t * - ARENA_REG (8 bits): Offset of the register that is used to calculate the\n1778:\t *                       address for load/store when accessing the arena region.\n1779:\t * - ARENA_WRITE (1 bit): This bit is set when the faulting instruction wrote to the arena region.\n1780:\t *                        It is independent of DST_REG, since a read-modify-write both writes to\n1781:\t *                        memory and reads the old value into a register.\n1782:\t * - ARENA_ACCESS (1 bit): This bit is set when the faulting instruction accessed the arena region.\n1783:\t *\n1784:\t * Bit layout of `data` (32-bit):\n1785:\t *\n1786:\t * +--------------+--------+--------------+\n1787:\t * |\t31-16\t  |  15-8  |     7-0      |\n1788:\t * |              |\t   |              |\n1789:\t * | ARENA_OFFSET | Unused |  EX_TYPE_BPF |\n1790:\t * +--------------+--------+--------------+\n1791:\t *\n1792:\t * - ARENA_OFFSET (16 bits): Offset used to calculate the address for load/store when\n1793:\t *                           accessing the arena region.\n1794:\t */\n1795:\t\n1796:\t#define DONT_CLEAR 1\n1797:\t#define FIXUP_INSN_LEN_MASK\tGENMASK(7, 0)\n1798:\t#define FIXUP_REG_MASK\t\tGENMASK(15, 8)\n1799:\t#define FIXUP_ARENA_REG_MASK\tGENMASK(23, 16)\n1800:\t#define FIXUP_ARENA_WRITE\tBIT(30)\n1801:\t#define FIXUP_ARENA_ACCESS\tBIT(31)\n1802:\t#define DATA_ARENA_OFFSET_MASK\tGENMASK(31, 16)\n1803:\t\n1804:\tbool ex_handler_bpf(const struct exception_table_entry *x, struct pt_regs *regs)\n1805:\t{\n1806:\t\tu32 reg = FIELD_GET(FIXUP_REG_MASK, x-\u003efixup);\n1807:\t\tu32 insn_len = FIELD_GET(FIXUP_INSN_LEN_MASK, x-\u003efixup);\n1808:\t\tbool is_arena = !!(x-\u003efixup \u0026 FIXUP_ARENA_ACCESS);\n1809:\t\tbool is_write = !!(x-\u003efixup \u0026 FIXUP_ARENA_WRITE);\n1810:\t\tunsigned long addr;\n1811:\t\ts16 off;\n1812:\t\tu32 arena_reg;\n1813:\t\n1814:\t\tif (is_arena) {\n1815:\t\t\tarena_reg = FIELD_GET(FIXUP_ARENA_REG_MASK, x-\u003efixup);\n1816:\t\t\toff = FIELD_GET(DATA_ARENA_OFFSET_MASK, x-\u003edata);\n1817:\t\t\taddr = *(unsigned long *)((void *)regs + arena_reg) + off;\n1818:\t\t\tbpf_prog_report_arena_violation(is_write, addr, regs-\u003eip);\n1819:\t\t}\n1820:\t\n1821:\t\t/* jump over faulting load and clear dest register */\n1822:\t\tif (reg != DONT_CLEAR)\n1823:\t\t\t*(unsigned long *)((void *)regs + reg) = 0;\n1824:\t\tregs-\u003eip += insn_len;\n1825:\t\n1826:\t\treturn true;\n1827:\t}\n1828:\t\n1829:\t/*\n1830:\t * Record an arena access that may fault. @fault_ip is the address of the\n1831:\t * faulting insn in the RO image, @resume_off how far past it execution has to\n1832:\t * resume: for a multi-insn lowering that is the end of the whole sequence, not\n1833:\t * the end of the one insn.\n1834:\t */\n"
}

33/3 2026/10/14 19:08 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 24015
							output: 341
							thoughts: 2660
						

Thoughts:
**Analysis of x86 JIT Compilation for Atomic Fetch Instructions**

Okay, let's break this down. My focus here is on ensuring the safety of this new JIT compilation for atomic fetch instructions. I'm looking at the `emit_atomic_fetch_rmw` function specifically, and how it handles potential faults. The core logic involves loading an old value, performing a local operation, and attempting a swap with `cmpxchg`. The critical part, from my perspective, is the fault handling and the preservation of register state across that fault. If a fault occurs, the code correctly jumps to a *resume* address. In this scenario the extable entry uses the `fault[0]` and `fault[1]` addresses to calculate the instruction length (`insn_len`). The fault handler, `ex_handler_bpf`, zeros the destination register. And then the code jumps to `*resume`, restoring `BPF_REG_0` from the backup register `BPF_REG_AX`, which has saved the pre-modification value. Looking at `do_jit`, If `src_reg` is equal to `BPF_REG_0`, `real_src_reg` equals `BPF_REG_AX`. The destination register is zeroed when the source register (`src_reg`) is `BPF_REG_0`. This appears to be the correct behavior. Everything's cleanly zeroed and handled on a fault.

Now, let's step back and consider security implications, specifically KMSAN (Kernel Memory Sanitizer). What is the nature of this patch? It enables JIT compilation on x86-64 for arena-based atomic fetch instructions (`BPF_AND | BPF_FETCH`, `BPF_OR | BPF_FETCH`, and `BPF_XOR | BPF_FETCH`). It lowers these instructions to CMPXCHG loops within the x86 instruction set. The arena is extended to handle the additional extable entries.

Does this patch introduce any risks related to uninitialized memory use or information leaks? No. It doesn't modify kernel structures sent to user space where there's a risk of uninitialized fields leaking. It does not introduce conditional logic that depends on uninitialized variables. This also doesn't involve the allocation of complex data structures with uninitialized fields. There's also no changes to bounds checks, lengths, or other logic that could expose uninitialized bytes from existing buffers.

What bugs could arise from this patch? Incorrect assembly generation, JIT extable size miscalculations, concurrency issues (atomicity problems), fault handling bugs (wrong instruction pointer offsets), and potential verifier/JIT desynchronization. Importantly, none of these potential bugs are directly related to KMSAN because KMSAN looks for kernel-allocated uninitialized memory. These are standard JIT compiler bugs that will be caught by KASAN for buffer overflows, other panics for crashes, BPF selftests, etc.

Now, let's verify against the criteria:

1.  Kernel structures sent to userspace with the potential for uninitialized data? No.
2.  Conditional logic depending on uninitialized variables? No.
3.  Allocation of complex data structures with uninitialized fields? No.
4.  Bounds checks or logic exposing uninitialized bytes? No.

Therefore, this patch should have `NeedsKMSAN=false`. The core issue is that this patch changes the BPF JIT compiler and how it handles x86 assembly. The patch does not introduce the risk of uninitialized kernel memory use or info-leaks because the modified code is within the JIT compiler, and no kernel memory is exposed to userspace.



34/3 2026/10/14 19:08 tool set-results
0m Args:
{
  "KMSANReasoning": "The patch series adds x86 BPF JIT support for atomic fetch read-modify-write operations (BPF_AND | BPF_FETCH, BPF_OR | BPF_FETCH, BPF_XOR | BPF_FETCH) on BPF arena memory. It lowers these operations to a CMPXCHG loop, registers exception table entries for both the load and CMPXCHG instructions to handle faults, adjusts num_exentries allocation calculations, and updates the relevant BPF selftests and sched_ext header helpers.\n\nKMSAN vs KASAN Applicability:\n- KMSAN detects uninitialized memory reads (heap, stack, page allocations) and kernel-to-user info-leaks. This patch introduces no new kernel-to-user data copies, does not alter serialization/padding of structs exposed to userspace, and does not add conditional logic on uninitialized kernel variables. The exception table entries initialized by emit_arena_exentry() have all their struct fields (insn, fixup, data) explicitly assigned.\n- Potential risks of these changes include JIT instruction encoding errors, register clobbering, incorrect jump offsets, or extable sizing/bounds errors. Array bounds issues in extable allocations are detected by KASAN, while execution/encoding issues are caught by the verifier, selftests, and standard hardware exception handling.\n- Because no uninitialized memory read or info-leak risks are introduced, a dedicated KMSAN fuzzing session is not warranted.",
  "NeedsKMSAN": false
}

Results:
{
  "KMSANReasoning": "The patch series adds x86 BPF JIT support for atomic fetch read-modify-write operations (BPF_AND | BPF_FETCH, BPF_OR | BPF_FETCH, BPF_XOR | BPF_FETCH) on BPF arena memory. It lowers these operations to a CMPXCHG loop, registers exception table entries for both the load and CMPXCHG instructions to handle faults, adjusts num_exentries allocation calculations, and updates the relevant BPF selftests and sched_ext header helpers.\n\nKMSAN vs KASAN Applicability:\n- KMSAN detects uninitialized memory reads (heap, stack, page allocations) and kernel-to-user info-leaks. This patch introduces no new kernel-to-user data copies, does not alter serialization/padding of structs exposed to userspace, and does not add conditional logic on uninitialized kernel variables. The exception table entries initialized by emit_arena_exentry() have all their struct fields (insn, fixup, data) explicitly assigned.\n- Potential risks of these changes include JIT instruction encoding errors, register clobbering, incorrect jump offsets, or extable sizing/bounds errors. Array bounds issues in extable allocations are detected by KASAN, while execution/encoding issues are caught by the verifier, selftests, and standard hardware exception handling.\n- Because no uninitialized memory read or info-leak risks are introduced, a dedicated KMSAN fuzzing session is not warranted.",
  "NeedsKMSAN": false
}

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)