AI Trajectory


Trajectory: (Expand/Collapse All)
Seq Timestamp Type Name Duration
0/0 2026/09/24 13:35 flow patch-triage
3m Args:
null

Results:
{
  "EnableConfigs": [
    "BPF_SYSCALL",
    "DEBUG_INFO_BTF"
  ],
  "FocusSymbols": [
    "bpf_task_work_schedule_signal",
    "bpf_task_work_schedule_resume",
    "bpf_task_work_irq",
    "bpf_task_work_acquire_ctx",
    "bpf_task_work_cancel_and_free",
    "bpf_task_work_cancel_scheduled"
  ],
  "KMSANReasoning": "The patch addresses concurrency and object lifetime race conditions in the BPF task_work subsystem (kernel/bpf/helpers.c). Specifically, it ensures a reference to `task_struct` is held across `task_work_add()` and potential cancellation to prevent use-after-free/NULL-dereference if the callback executes concurrently, moves `init_irq_work()` to context allocation time to avoid reinitializing an active work item, and checks `irq_work_is_busy()` before reusing contexts.\n\nNo new uninitialized stack or heap memory is introduced or read: local variables like `task` are assigned before use, and `ctx` continues to be zeroed with `memset` on allocation. No data is copied to user space, and no conditional branches depend on uninitialized values. Potential issues from this patch (such as lifetime issues, refcounting bugs, and race conditions) are within the domain of KASAN and standard kernel debugging facilities, not KMSAN. Therefore, a dedicated KMSAN fuzzing session is not warranted.",
  "NeedsKMSAN": false,
  "Reasoning": "The patch modifies reachable executable code in the BPF core subsystem (kernel/bpf/helpers.c). Specifically, it addresses races in BPF task_work handling by managing task_struct references across irq_work and cancellation, preventing task_work context reuse while irq_work is still busy, and initializing irq_work earlier upon context allocation. These kfuncs and callbacks are accessible to BPF programs loaded via the bpf syscall and executed concurrently across CPUs, making this change well worth fuzzing.",
  "WorthFuzzing": true
}

1/1 2026/09/24 13:35 action read-patch-diff
0m Args:
null

Results:
{
  "PatchDiff": "commit dce7e4bef520dc115e8e72f93ddf7999094bfb16\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate:   Thu Sep 24 13:35:37 2026 +0000\n\n    syz-cluster: applied patch under review\n\ndiff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c\nindex b3cc5c8fc8756..d8bea7538de54 100644\n--- a/kernel/bpf/helpers.c\n+++ b/kernel/bpf/helpers.c\n@@ -4437,7 +4437,8 @@ static void bpf_task_work_ctx_put(struct bpf_task_work_ctx *ctx)\n \t}\n }\n \n-static void bpf_task_work_cancel(struct bpf_task_work_ctx *ctx)\n+static void bpf_task_work_cancel(struct bpf_task_work_ctx *ctx,\n+\t\t\t\t struct task_struct *task)\n {\n \t/*\n \t * Scheduled task_work callback holds ctx ref, so if we successfully\n@@ -4445,7 +4446,7 @@ static void bpf_task_work_cancel(struct bpf_task_work_ctx *ctx)\n \t * cancel, callback will inevitably run or has already completed\n \t * running, and it would have taken care of its ctx ref itself.\n \t */\n-\tif (task_work_cancel(ctx-\u003etask, \u0026ctx-\u003ework))\n+\tif (task_work_cancel(task, \u0026ctx-\u003ework))\n \t\tbpf_task_work_ctx_put(ctx);\n }\n \n@@ -4486,6 +4487,7 @@ static void bpf_task_work_callback(struct callback_head *cb)\n static void bpf_task_work_irq(struct irq_work *irq_work)\n {\n \tstruct bpf_task_work_ctx *ctx = container_of(irq_work, struct bpf_task_work_ctx, irq_work);\n+\tstruct task_struct *task;\n \tenum bpf_task_work_state state;\n \tint err;\n \n@@ -4496,7 +4498,8 @@ static void bpf_task_work_irq(struct irq_work *irq_work)\n \t\treturn;\n \t}\n \n-\terr = task_work_add(ctx-\u003etask, \u0026ctx-\u003ework, ctx-\u003emode);\n+\ttask = get_task_struct(ctx-\u003etask);\n+\terr = task_work_add(task, \u0026ctx-\u003ework, ctx-\u003emode);\n \tif (err) {\n \t\tbpf_task_work_ctx_reset(ctx);\n \t\t/*\n@@ -4505,19 +4508,20 @@ static void bpf_task_work_irq(struct irq_work *irq_work)\n \t\t */\n \t\t(void)cmpxchg(\u0026ctx-\u003estate, BPF_TW_SCHEDULING, BPF_TW_STANDBY);\n \t\tbpf_task_work_ctx_put(ctx);\n+\t\tput_task_struct(task);\n \t\treturn;\n \t}\n \n \t/*\n-\t * It's technically possible for just scheduled task_work callback to\n-\t * complete running by now, going SCHEDULING -\u003e RUNNING and then\n-\t * dropping its ctx refcount. Instead of capturing an extra ref just\n-\t * to protect below ctx-\u003estate access, we rely on rcu_read_lock\n-\t * above to prevent kfree_rcu from freeing ctx before we return.\n+\t * The callback may have completed and reset ctx-\u003etask already. Keep the\n+\t * captured task alive until this invocation is done with cancellation.\n+\t * The RCU read-side section keeps ctx memory alive until then.\n \t */\n \tstate = cmpxchg(\u0026ctx-\u003estate, BPF_TW_SCHEDULING, BPF_TW_SCHEDULED);\n \tif (state == BPF_TW_FREED)\n-\t\tbpf_task_work_cancel(ctx); /* clean up if we switched into FREED state */\n+\t\tbpf_task_work_cancel(ctx, task); /* clean up if we switched into FREED state */\n+\n+\tput_task_struct(task);\n }\n \n static struct bpf_task_work_ctx *bpf_task_work_fetch_ctx(struct bpf_task_work *tw,\n@@ -4537,6 +4541,7 @@ static struct bpf_task_work_ctx *bpf_task_work_fetch_ctx(struct bpf_task_work *t\n \tmemset(ctx, 0, sizeof(*ctx));\n \trefcount_set(\u0026ctx-\u003erefcnt, 1); /* map's own ref */\n \tctx-\u003estate = BPF_TW_STANDBY;\n+\tinit_irq_work(\u0026ctx-\u003eirq_work, bpf_task_work_irq);\n \n \told_ctx = cmpxchg(\u0026twk-\u003ectx, NULL, ctx);\n \tif (old_ctx) {\n@@ -4579,6 +4584,18 @@ static struct bpf_task_work_ctx *bpf_task_work_acquire_ctx(struct bpf_task_work\n \t\tbpf_task_work_ctx_put(ctx);\n \t\treturn ERR_PTR(-EBUSY);\n \t}\n+\t/*\n+\t * The callback or add-failure path can publish STANDBY before the\n+\t * scheduling irq_work returns. Do not let a new round reuse the context\n+\t * until BUSY clears. Otherwise the old handler can consume the new\n+\t * round's SCHEDULING state and publish a false SCHEDULED state, and two\n+\t * invocations can overlap on different CPUs.\n+\t */\n+\tif (unlikely(irq_work_is_busy(\u0026ctx-\u003eirq_work))) {\n+\t\t(void)cmpxchg(\u0026ctx-\u003estate, BPF_TW_PENDING, BPF_TW_STANDBY);\n+\t\tbpf_task_work_ctx_put(ctx);\n+\t\treturn ERR_PTR(-EBUSY);\n+\t}\n \n \t/*\n \t * If no process or bpffs is holding a reference to the map, no new callbacks should be\n@@ -4628,7 +4645,6 @@ static int bpf_task_work_schedule(struct task_struct *task, struct bpf_task_work\n \tctx-\u003emap = map;\n \tctx-\u003emap_val = (void *)tw - map-\u003erecord-\u003etask_work_off;\n \tinit_task_work(\u0026ctx-\u003ework, bpf_task_work_callback);\n-\tinit_irq_work(\u0026ctx-\u003eirq_work, bpf_task_work_irq);\n \n \tirq_work_queue(\u0026ctx-\u003eirq_work);\n \treturn 0;\n@@ -4777,7 +4793,8 @@ static void bpf_task_work_cancel_scheduled(struct irq_work *irq_work)\n {\n \tstruct bpf_task_work_ctx *ctx = container_of(irq_work, struct bpf_task_work_ctx, irq_work);\n \n-\tbpf_task_work_cancel(ctx); /* this might put task_work callback's ref */\n+\t/* SCHEDULED -\u003e FREED keeps ctx-\u003etask stable until this runs. */\n+\tbpf_task_work_cancel(ctx, ctx-\u003etask); /* this might put task_work callback's ref */\n \tbpf_task_work_ctx_put(ctx); /* and here we put map's own ref that was transferred to us */\n }\n \ndiff --git a/tools/testing/selftests/bpf/prog_tests/test_task_work.c b/tools/testing/selftests/bpf/prog_tests/test_task_work.c\nindex 774b31a5f6ca1..ff56976f4226e 100644\n--- a/tools/testing/selftests/bpf/prog_tests/test_task_work.c\n+++ b/tools/testing/selftests/bpf/prog_tests/test_task_work.c\n@@ -5,10 +5,14 @@\n #include \u003cstdio.h\u003e\n #include \"task_work.skel.h\"\n #include \"task_work_fail.skel.h\"\n+#include \"task_work_race.skel.h\"\n #include \u003clinux/bpf.h\u003e\n #include \u003clinux/perf_event.h\u003e\n #include \u003csys/syscall.h\u003e\n #include \u003ctime.h\u003e\n+#include \u003cpthread.h\u003e\n+#include \u003csched.h\u003e\n+#include \u003cunistd.h\u003e\n \n static int perf_event_open(__u32 type, __u64 config, int pid)\n {\n@@ -155,3 +159,282 @@ void test_task_work(void)\n \n \tRUN_TESTS(task_work_fail);\n }\n+\n+#define TASK_WORK_RACE_ROUNDS\t1500\n+\n+/* Must match progs/task_work_race.c. */\n+enum task_work_race_status {\n+\tRACE_ARM_SEQ,\n+\tRACE_READY_SEQ,\n+\t/* High 32 bits hold the sequence, low 32 bits the signed error. */\n+\tRACE_RESULT,\n+};\n+\n+struct task_work_race_value {\n+\t__u32 seq;\n+\tchar data[60];\n+\tstruct bpf_task_work tw;\n+};\n+\n+struct task_work_race_ctx {\n+\tint stop;\n+\tint setup_err;\n+\tint trigger_tid;\n+\tint target_tid;\n+\tint trigger_cpu;\n+\tint target_cpu;\n+};\n+\n+static int task_work_race_pin_cpu(int cpu)\n+{\n+\tcpu_set_t set;\n+\n+\tCPU_ZERO(\u0026set);\n+\tCPU_SET(cpu, \u0026set);\n+\treturn pthread_setaffinity_np(pthread_self(), sizeof(set), \u0026set);\n+}\n+\n+static void *task_work_race_trigger(void *arg)\n+{\n+\tstruct task_work_race_ctx *ctx = arg;\n+\tint err;\n+\n+\terr = task_work_race_pin_cpu(ctx-\u003etrigger_cpu);\n+\tif (err)\n+\t\t__atomic_store_n(\u0026ctx-\u003esetup_err, err, __ATOMIC_RELEASE);\n+\t__atomic_store_n(\u0026ctx-\u003etrigger_tid, syscall(__NR_gettid),\n+\t\t\t __ATOMIC_RELEASE);\n+\twhile (!__atomic_load_n(\u0026ctx-\u003estop, __ATOMIC_ACQUIRE))\n+\t\tgetppid();\n+\treturn NULL;\n+}\n+\n+static void *task_work_race_target(void *arg)\n+{\n+\tstruct task_work_race_ctx *ctx = arg;\n+\tint err;\n+\n+\terr = task_work_race_pin_cpu(ctx-\u003etarget_cpu);\n+\tif (err)\n+\t\t__atomic_store_n(\u0026ctx-\u003esetup_err, err, __ATOMIC_RELEASE);\n+\t__atomic_store_n(\u0026ctx-\u003etarget_tid, syscall(__NR_gettid),\n+\t\t\t __ATOMIC_RELEASE);\n+\twhile (!__atomic_load_n(\u0026ctx-\u003estop, __ATOMIC_ACQUIRE))\n+\t\tgetppid();\n+\treturn NULL;\n+}\n+\n+static int task_work_race_status(struct task_work_race *skel, int idx,\n+\t\t\t\t __s64 *value)\n+{\n+\treturn bpf_map_lookup_elem(bpf_map__fd(skel-\u003emaps.status), \u0026idx,\n+\t\t\t\t   value);\n+}\n+\n+static int task_work_race_set_status(struct task_work_race *skel, int idx,\n+\t\t\t\t     __s64 value)\n+{\n+\treturn bpf_map_update_elem(bpf_map__fd(skel-\u003emaps.status), \u0026idx,\n+\t\t\t\t   \u0026value, BPF_ANY);\n+}\n+\n+static int task_work_race_wait_ready(struct task_work_race *skel, __u32 seq)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i \u003c 2000000 / 100; i++) {\n+\t\t__s64 value = 0;\n+\n+\t\tif (!task_work_race_status(skel, RACE_READY_SEQ, \u0026value) \u0026\u0026\n+\t\t    value == seq)\n+\t\t\treturn 0;\n+\t\tif (!task_work_race_status(skel, RACE_RESULT, \u0026value) \u0026\u0026\n+\t\t    (__u32)((__u64)value \u003e\u003e 32) == seq)\n+\t\t\t/*\n+\t\t\t * The BPF program finished between the two reads:\n+\t\t\t * RESULT makes waiting for READY unnecessary. Any error\n+\t\t\t * that occurred before READY is checked by the caller.\n+\t\t\t */\n+\t\t\treturn 0;\n+\t\tusleep(100);\n+\t}\n+\treturn -ETIMEDOUT;\n+}\n+\n+static int task_work_race_prepare_elem(struct task_work_race *skel)\n+{\n+\tstruct task_work_race_value value = {};\n+\tint key = 0;\n+\n+\tif (!bpf_map_lookup_elem(bpf_map__fd(skel-\u003emaps.hmap), \u0026key, \u0026value))\n+\t\treturn 0;\n+\tif (errno != ENOENT)\n+\t\treturn -errno;\n+\tif (bpf_map_update_elem(bpf_map__fd(skel-\u003emaps.hmap), \u0026key, \u0026value,\n+\t\t\t\tBPF_NOEXIST))\n+\t\treturn -errno;\n+\treturn 0;\n+}\n+\n+static int task_work_race_arm(struct task_work_race *skel, __u32 seq)\n+{\n+\tif (task_work_race_set_status(skel, RACE_ARM_SEQ, seq))\n+\t\treturn -errno;\n+\treturn task_work_race_wait_ready(skel, seq);\n+}\n+\n+static int task_work_race_check_done(struct task_work_race *skel, __u32 seq)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i \u003c 2000000 / 100; i++) {\n+\t\t__s64 result = 0;\n+\n+\t\tif (!task_work_race_status(skel, RACE_RESULT, \u0026result) \u0026\u0026\n+\t\t    (__u32)((__u64)result \u003e\u003e 32) == seq)\n+\t\t\treturn (__s32)result;\n+\t\tusleep(100);\n+\t}\n+\treturn -ETIMEDOUT;\n+}\n+\n+static int task_work_race_wait_callback(struct task_work_race *skel,\n+\t\t\t\t\t__u32 seq)\n+{\n+\tint i, key = seq;\n+\n+\tfor (i = 0; i \u003c 2000000 / 100; i++) {\n+\t\t__u64 completed = 0;\n+\n+\t\tif (!bpf_map_lookup_elem(bpf_map__fd(skel-\u003emaps.completed),\n+\t\t\t\t\t \u0026key, \u0026completed) \u0026\u0026 completed)\n+\t\t\treturn 0;\n+\t\tusleep(100);\n+\t}\n+\treturn -ETIMEDOUT;\n+}\n+\n+void serial_test_task_work_race(void)\n+{\n+\tstruct task_work_race_ctx ctx = {};\n+\tstruct task_work_race *skel;\n+\tpthread_t trigger, target;\n+\tcpu_set_t allowed;\n+\tint cpu, cpu_count = 0;\n+\tbool target_started = false, trigger_started = false;\n+\tint err, i, key = 0;\n+\n+\tif (sched_getaffinity(0, sizeof(allowed), \u0026allowed)) {\n+\t\tASSERT_OK(-errno, \"sched_getaffinity\");\n+\t\treturn;\n+\t}\n+\tfor (cpu = 0; cpu \u003c CPU_SETSIZE \u0026\u0026 cpu_count \u003c 2; cpu++) {\n+\t\tif (!CPU_ISSET(cpu, \u0026allowed))\n+\t\t\tcontinue;\n+\t\tif (!cpu_count)\n+\t\t\tctx.trigger_cpu = cpu;\n+\t\telse\n+\t\t\tctx.target_cpu = cpu;\n+\t\tcpu_count++;\n+\t}\n+\tif (cpu_count \u003c 2) {\n+\t\tprintf(\"%s:SKIP:need two CPUs in the process affinity mask\\n\",\n+\t\t       __func__);\n+\t\ttest__skip();\n+\t\treturn;\n+\t}\n+\n+\tskel = task_work_race__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"open_and_load\"))\n+\t\treturn;\n+\tif (!ASSERT_OK(task_work_race__attach(skel), \"attach\"))\n+\t\tgoto cleanup;\n+\n+\terr = pthread_create(\u0026target, NULL, task_work_race_target, \u0026ctx);\n+\tif (!ASSERT_OK(err, \"pthread_create target\"))\n+\t\tgoto cleanup;\n+\ttarget_started = true;\n+\terr = pthread_create(\u0026trigger, NULL, task_work_race_trigger, \u0026ctx);\n+\tif (!ASSERT_OK(err, \"pthread_create trigger\"))\n+\t\tgoto stop;\n+\ttrigger_started = true;\n+\n+\tfor (i = 0; i \u003c 5000000 / 100; i++) {\n+\t\tif (__atomic_load_n(\u0026ctx.trigger_tid, __ATOMIC_ACQUIRE) \u0026\u0026\n+\t\t    __atomic_load_n(\u0026ctx.target_tid, __ATOMIC_ACQUIRE))\n+\t\t\tbreak;\n+\t\tusleep(100);\n+\t}\n+\tif (!ASSERT_TRUE(__atomic_load_n(\u0026ctx.trigger_tid, __ATOMIC_ACQUIRE) \u0026\u0026\n+\t\t\t __atomic_load_n(\u0026ctx.target_tid, __ATOMIC_ACQUIRE),\n+\t\t\t \"thread start\"))\n+\t\tgoto stop;\n+\tif (!ASSERT_OK(__atomic_load_n(\u0026ctx.setup_err, __ATOMIC_ACQUIRE),\n+\t\t       \"thread affinity\"))\n+\t\tgoto stop;\n+\n+\tskel-\u003ebss-\u003etrigger_tid = __atomic_load_n(\u0026ctx.trigger_tid, __ATOMIC_ACQUIRE);\n+\tskel-\u003ebss-\u003etarget_tid = __atomic_load_n(\u0026ctx.target_tid, __ATOMIC_ACQUIRE);\n+\n+\tfor (i = 0; i \u003c TASK_WORK_RACE_ROUNDS; i++) {\n+\t\t__u32 seq = i + 1;\n+\t\tint variant = i % 3;\n+\n+\t\tif (!ASSERT_OK(task_work_race_prepare_elem(skel), \"prepare elem\") ||\n+\t\t    !ASSERT_OK(task_work_race_arm(skel, seq), \"round ready\"))\n+\t\t\tgoto stop;\n+\n+\t\tif (variant == 2 \u0026\u0026\n+\t\t    !ASSERT_OK(bpf_map_delete_elem(bpf_map__fd(skel-\u003emaps.hmap),\n+\t\t\t\t\t\t   \u0026key), \"early delete\"))\n+\t\t\tgoto stop;\n+\n+\t\terr = task_work_race_check_done(skel, seq);\n+\t\t/*\n+\t\t * Variant 2 deletes the element right after READY, so losing\n+\t\t * the race against the scheduling kfunc is a valid outcome:\n+\t\t * the schedule itself fails with -EBUSY (ctx already FREED),\n+\t\t * and the retry on the next tracepoint invocation fails with\n+\t\t * -ENOENT because the element is gone.  Either way the round\n+\t\t * exercised the deletion paths; a later variant 0 round\n+\t\t * recreates the element.\n+\t\t */\n+\t\tif (variant == 2 \u0026\u0026 (err == -EBUSY || err == -ENOENT))\n+\t\t\terr = 0;\n+\t\tif (!ASSERT_OK(err, \"schedule\")) {\n+\t\t\tfprintf(stderr, \"round %d schedule failed: %d\\n\", i, err);\n+\t\t\tgoto stop;\n+\t\t}\n+\n+\t\tif (variant != 2 \u0026\u0026\n+\t\t    !ASSERT_OK(task_work_race_wait_callback(skel, seq),\n+\t\t\t       \"callback\"))\n+\t\t\tgoto stop;\n+\n+\t\tif (variant == 1 \u0026\u0026\n+\t\t    !ASSERT_OK(bpf_map_delete_elem(bpf_map__fd(skel-\u003emaps.hmap),\n+\t\t\t\t\t\t   \u0026key), \"late delete\"))\n+\t\t\tgoto stop;\n+\t}\n+\n+\t/* A distinct completion generation prevents an old callback satisfying this. */\n+\tif (!ASSERT_OK(task_work_race_prepare_elem(skel), \"final prepare\") ||\n+\t    !ASSERT_OK(task_work_race_arm(skel, TASK_WORK_RACE_ROUNDS + 1),\n+\t\t       \"final ready\") ||\n+\t    !ASSERT_OK(task_work_race_check_done(skel,\n+\t\t\t\t\tTASK_WORK_RACE_ROUNDS + 1),\n+\t\t       \"final schedule\") ||\n+\t    !ASSERT_OK(task_work_race_wait_callback(skel,\n+\t\t\t\t\t     TASK_WORK_RACE_ROUNDS + 1),\n+\t\t       \"final callback\"))\n+\t\tgoto stop;\n+\n+stop:\n+\t__atomic_store_n(\u0026ctx.stop, 1, __ATOMIC_RELEASE);\n+\tif (trigger_started)\n+\t\tpthread_join(trigger, NULL);\n+\tif (target_started)\n+\t\tpthread_join(target, NULL);\n+cleanup:\n+\ttask_work_race__destroy(skel);\n+}\ndiff --git a/tools/testing/selftests/bpf/progs/task_work_race.c b/tools/testing/selftests/bpf/progs/task_work_race.c\nnew file mode 100644\nindex 0000000000000..c94eca510cb37\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/task_work_race.c\n@@ -0,0 +1,126 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026 KylinSoft Corporation. */\n+#include \u003cvmlinux.h\u003e\n+#include \u003cstdbool.h\u003e\n+#include \u003cerrno.h\u003e\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \u003cbpf/bpf_tracing.h\u003e\n+#include \"bpf_misc.h\"\n+\n+char _license[] SEC(\"license\") = \"GPL\";\n+\n+#define TASK_WORK_RACE_MAX_SEQ\t2048\n+\n+enum task_work_race_status {\n+\tRACE_ARM_SEQ,\n+\tRACE_READY_SEQ,\n+\t/* High 32 bits hold the sequence, low 32 bits the signed error. */\n+\tRACE_RESULT,\n+\tRACE_STATUS_MAX,\n+};\n+\n+int trigger_tid;\n+int target_tid;\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_ARRAY);\n+\t__uint(max_entries, RACE_STATUS_MAX);\n+\t__type(key, int);\n+\t__type(value, __s64);\n+} status SEC(\".maps\");\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_ARRAY);\n+\t__uint(max_entries, TASK_WORK_RACE_MAX_SEQ);\n+\t__type(key, int);\n+\t__type(value, __u64);\n+} completed SEC(\".maps\");\n+\n+struct task_work_race_value {\n+\t__u32 seq;\n+\tchar data[60];\n+\tstruct bpf_task_work tw;\n+};\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_HASH);\n+\t__uint(map_flags, BPF_F_NO_PREALLOC);\n+\t__uint(max_entries, 4);\n+\t__type(key, int);\n+\t__type(value, struct task_work_race_value);\n+} hmap SEC(\".maps\");\n+\n+static __always_inline void set_status(int key, __s64 value)\n+{\n+\t__s64 *slot;\n+\n+\tslot = bpf_map_lookup_elem(\u0026status, \u0026key);\n+\tif (slot)\n+\t\t*slot = value;\n+}\n+\n+static int process_work(struct bpf_map *map, void *key, void *value)\n+{\n+\tstruct task_work_race_value *work = value;\n+\t__u64 *done;\n+\tint seq = work-\u003eseq;\n+\n+\tif (seq \u003c= 0 || seq \u003e= TASK_WORK_RACE_MAX_SEQ)\n+\t\treturn 0;\n+\tdone = bpf_map_lookup_elem(\u0026completed, \u0026seq);\n+\tif (done)\n+\t\t*done = 1;\n+\treturn 0;\n+}\n+\n+SEC(\"tracepoint/syscalls/sys_enter_getppid\")\n+int race_sched_work(void *ctx)\n+{\n+\tstruct task_work_race_value *work;\n+\tstruct task_struct *task;\n+\t__s64 *arm, *result;\n+\t__u32 tid = (__u32)bpf_get_current_pid_tgid();\n+\tint key = 0, err;\n+\t__u32 seq;\n+\n+\tif (tid != trigger_tid)\n+\t\treturn 0;\n+\n+\tkey = RACE_ARM_SEQ;\n+\tarm = bpf_map_lookup_elem(\u0026status, \u0026key);\n+\tkey = RACE_RESULT;\n+\tresult = bpf_map_lookup_elem(\u0026status, \u0026key);\n+\tif (!arm || !result || *arm \u003c= 0 ||\n+\t    (__u32)((__u64)*result \u003e\u003e 32) == *arm)\n+\t\treturn 0;\n+\tseq = *arm;\n+\n+\ttask = bpf_task_from_pid(target_tid);\n+\tif (!task) {\n+\t\terr = -ESRCH;\n+\t\tgoto out_done;\n+\t}\n+\n+\tkey = 0;\n+\twork = bpf_map_lookup_elem(\u0026hmap, \u0026key);\n+\tif (!work) {\n+\t\terr = -ENOENT;\n+\t\tgoto out_task;\n+\t}\n+\n+\twork-\u003eseq = seq;\n+\tset_status(RACE_READY_SEQ, seq);\n+\terr = bpf_task_work_schedule_signal(task, \u0026work-\u003etw, \u0026hmap,\n+\t\t\t\t\t    process_work);\n+\tif (err == -EBUSY) {\n+\t\tbpf_task_release(task);\n+\t\treturn 0;\n+\t}\n+\n+out_task:\n+\tbpf_task_release(task);\n+out_done:\n+\t/* Publish the sequence and its error in one store. */\n+\t*result = ((__u64)seq \u003c\u003c 32) | (__u32)err;\n+\treturn 0;\n+}\n"
}

2/1 2026/09/24 13:35 agent patch-evaluator
2m Model:
core

Args:
null

Results:
{
  "EnableConfigs": [
    "BPF_SYSCALL",
    "DEBUG_INFO_BTF"
  ],
  "FocusSymbols": [
    "bpf_task_work_schedule_signal",
    "bpf_task_work_schedule_resume",
    "bpf_task_work_irq",
    "bpf_task_work_acquire_ctx",
    "bpf_task_work_cancel_and_free",
    "bpf_task_work_cancel_scheduled"
  ],
  "Reasoning": "The patch modifies reachable executable code in the BPF core subsystem (kernel/bpf/helpers.c). Specifically, it addresses races in BPF task_work handling by managing task_struct references across irq_work and cancellation, preventing task_work context reuse while irq_work is still busy, and initializing irq_work earlier upon context allocation. These kfuncs and callbacks are accessible to BPF programs loaded via the bpf syscall and executed concurrently across CPUs, making this change well worth fuzzing.",
  "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 dce7e4bef520dc115e8e72f93ddf7999094bfb16
Author: syz-cluster <triage@syzkaller.com>
Date:   Thu Sep 24 13:35:37 2026 +0000

    syz-cluster: applied patch under review

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index b3cc5c8fc8756..d8bea7538de54 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -4437,7 +4437,8 @@ static void bpf_task_work_ctx_put(struct bpf_task_work_ctx *ctx)
 	}
 }
 
-static void bpf_task_work_cancel(struct bpf_task_work_ctx *ctx)
+static void bpf_task_work_cancel(struct bpf_task_work_ctx *ctx,
+				 struct task_struct *task)
 {
 	/*
 	 * Scheduled task_work callback holds ctx ref, so if we successfully
@@ -4445,7 +4446,7 @@ static void bpf_task_work_cancel(struct bpf_task_work_ctx *ctx)
 	 * cancel, callback will inevitably run or has already completed
 	 * running, and it would have taken care of its ctx ref itself.
 	 */
-	if (task_work_cancel(ctx->task, &ctx->work))
+	if (task_work_cancel(task, &ctx->work))
 		bpf_task_work_ctx_put(ctx);
 }
 
@@ -4486,6 +4487,7 @@ static void bpf_task_work_callback(struct callback_head *cb)
 static void bpf_task_work_irq(struct irq_work *irq_work)
 {
 	struct bpf_task_work_ctx *ctx = container_of(irq_work, struct bpf_task_work_ctx, irq_work);
+	struct task_struct *task;
 	enum bpf_task_work_state state;
 	int err;
 
@@ -4496,7 +4498,8 @@ static void bpf_task_work_irq(struct irq_work *irq_work)
 		return;
 	}
 
-	err = task_work_add(ctx->task, &ctx->work, ctx->mode);
+	task = get_task_struct(ctx->task);
+	err = task_work_add(task, &ctx->work, ctx->mode);
 	if (err) {
 		bpf_task_work_ctx_reset(ctx);
 		/*
@@ -4505,19 +4508,20 @@ static void bpf_task_work_irq(struct irq_work *irq_work)
 		 */
 		(void)cmpxchg(&ctx->state, BPF_TW_SCHEDULING, BPF_TW_STANDBY);
 		bpf_task_work_ctx_put(ctx);
+		put_task_struct(task);
 		return;
 	}
 
 	/*
-	 * It's technically possible for just scheduled task_work callback to
-	 * complete running by now, going SCHEDULING -> RUNNING and then
-	 * dropping its ctx refcount. Instead of capturing an extra ref just
-	 * to protect below ctx->state access, we rely on rcu_read_lock
-	 * above to prevent kfree_rcu from freeing ctx before we return.
+	 * The callback may have completed and reset ctx->task already. Keep the
+	 * captured task alive until this invocation is done with cancellation.
+	 * The RCU read-side section keeps ctx memory alive until then.
 	 */
 	state = cmpxchg(&ctx->state, BPF_TW_SCHEDULING, BPF_TW_SCHEDULED);
 	if (state == BPF_TW_FREED)
-		bpf_task_work_cancel(ctx); /* clean up if we switched into FREED state */
+		bpf_task_work_cancel(ctx, task); /* clean up if we switched into FREED state */
+
+	put_task_struct(task);
 }
 
 static struct bpf_task_work_ctx *bpf_task_work_fetch_ctx(struct bpf_task_work *tw,
@@ -4537,6 +4541,7 @@ static struct bpf_task_work_ctx *bpf_task_work_fetch_ctx(struct bpf_task_work *t
 	memset(ctx, 0, sizeof(*ctx));
 	refcount_set(&ctx->refcnt, 1); /* map's own ref */
 	ctx->state = BPF_TW_STANDBY;
+	init_irq_work(&ctx->irq_work, bpf_task_work_irq);
 
 	old_ctx = cmpxchg(&twk->ctx, NULL, ctx);
 	if (old_ctx) {
@@ -4579,6 +4584,18 @@ static struct bpf_task_work_ctx *bpf_task_work_acquire_ctx(struct bpf_task_work
 		bpf_task_work_ctx_put(ctx);
 		return ERR_PTR(-EBUSY);
 	}
+	/*
+	 * The callback or add-failure path can publish STANDBY before the
+	 * scheduling irq_work returns. Do not let a new round reuse the context
+	 * until BUSY clears. Otherwise the old handler can consume the new
+	 * round's SCHEDULING state and publish a false SCHEDULED state, and two
+	 * invocations can overlap on different CPUs.
+	 */
+	if (unlikely(irq_work_is_busy(&ctx->irq_work))) {
+		(void)cmpxchg(&ctx->state, BPF_TW_PENDING, BPF_TW_STANDBY);
+		bpf_task_work_ctx_put(ctx);
+		return ERR_PTR(-EBUSY);
+	}
 
 	/*
 	 * If no process or bpffs is holding a reference to the map, no new callbacks should be
@@ -4628,7 +4645,6 @@ static int bpf_task_work_schedule(struct task_struct *task, struct bpf_task_work
 	ctx->map = map;
 	ctx->map_val = (void *)tw - map->record->task_work_off;
 	init_task_work(&ctx->work, bpf_task_work_callback);
-	init_irq_work(&ctx->irq_work, bpf_task_work_irq);
 
 	irq_work_queue(&ctx->irq_work);
 	return 0;
@@ -4777,7 +4793,8 @@ static void bpf_task_work_cancel_scheduled(struct irq_work *irq_work)
 {
 	struct bpf_task_work_ctx *ctx = container_of(irq_work, struct bpf_task_work_ctx, irq_work);
 
-	bpf_task_work_cancel(ctx); /* this might put task_work callback's ref */
+	/* SCHEDULED -> FREED keeps ctx->task stable until this runs. */
+	bpf_task_work_cancel(ctx, ctx->task); /* this might put task_work callback's ref */
 	bpf_task_work_ctx_put(ctx); /* and here we put map's own ref that was transferred to us */
 }
 
diff --git a/tools/testing/selftests/bpf/prog_tests/test_task_work.c b/tools/testing/selftests/bpf/prog_tests/test_task_work.c
index 774b31a5f6ca1..ff56976f4226e 100644
--- a/tools/testing/selftests/bpf/prog_tests/test_task_work.c
+++ b/tools/testing/selftests/bpf/prog_tests/test_task_work.c
@@ -5,10 +5,14 @@
 #include <stdio.h>
 #include "task_work.skel.h"
 #include "task_work_fail.skel.h"
+#include "task_work_race.skel.h"
 #include <linux/bpf.h>
 #include <linux/perf_event.h>
 #include <sys/syscall.h>
 #include <time.h>
+#include <pthread.h>
+#include <sched.h>
+#include <unistd.h>
 
 static int perf_event_open(__u32 type, __u64 config, int pid)
 {
@@ -155,3 +159,282 @@ void test_task_work(void)
 
 	RUN_TESTS(task_work_fail);
 }
+
+#define TASK_WORK_RACE_ROUNDS	1500
+
+/* Must match progs/task_work_race.c. */
+enum task_work_race_status {
+	RACE_ARM_SEQ,
+	RACE_READY_SEQ,
+	/* High 32 bits hold the sequence, low 32 bits the signed error. */
+	RACE_RESULT,
+};
+
+struct task_work_race_value {
+	__u32 seq;
+	char data[60];
+	struct bpf_task_work tw;
+};
+
+struct task_work_race_ctx {
+	int stop;
+	int setup_err;
+	int trigger_tid;
+	int target_tid;
+	int trigger_cpu;
+	int target_cpu;
+};
+
+static int task_work_race_pin_cpu(int cpu)
+{
+	cpu_set_t set;
+
+	CPU_ZERO(&set);
+	CPU_SET(cpu, &set);
+	return pthread_setaffinity_np(pthread_self(), sizeof(set), &set);
+}
+
+static void *task_work_race_trigger(void *arg)
+{
+	struct task_work_race_ctx *ctx = arg;
+	int err;
+
+	err = task_work_race_pin_cpu(ctx->trigger_cpu);
+	if (err)
+		__atomic_store_n(&ctx->setup_err, err, __ATOMIC_RELEASE);
+	__atomic_store_n(&ctx->trigger_tid, syscall(__NR_gettid),
+			 __ATOMIC_RELEASE);
+	while (!__atomic_load_n(&ctx->stop, __ATOMIC_ACQUIRE))
+		getppid();
+	return NULL;
+}
+
+static void *task_work_race_target(void *arg)
+{
+	struct task_work_race_ctx *ctx = arg;
+	int err;
+
+	err = task_work_race_pin_cpu(ctx->target_cpu);
+	if (err)
+		__atomic_store_n(&ctx->setup_err, err, __ATOMIC_RELEASE);
+	__atomic_store_n(&ctx->target_tid, syscall(__NR_gettid),
+			 __ATOMIC_RELEASE);
+	while (!__atomic_load_n(&ctx->stop, __ATOMIC_ACQUIRE))
+		getppid();
+	return NULL;
+}
+
+static int task_work_race_status(struct task_work_race *skel, int idx,
+				 __s64 *value)
+{
+	return bpf_map_lookup_elem(bpf_map__fd(skel->maps.status), &idx,
+				   value);
+}
+
+static int task_work_race_set_status(struct task_work_race *skel, int idx,
+				     __s64 value)
+{
+	return bpf_map_update_elem(bpf_map__fd(skel->maps.status), &idx,
+				   &value, BPF_ANY);
+}
+
+static int task_work_race_wait_ready(struct task_work_race *skel, __u32 seq)
+{
+	int i;
+
+	for (i = 0; i < 2000000 / 100; i++) {
+		__s64 value = 0;
+
+		if (!task_work_race_status(skel, RACE_READY_SEQ, &value) &&
+		    value == seq)
+			return 0;
+		if (!task_work_race_status(skel, RACE_RESULT, &value) &&
+		    (__u32)((__u64)value >> 32) == seq)
+			/*
+			 * The BPF program finished between the two reads:
+			 * RESULT makes waiting for READY unnecessary. Any error
+			 * that occurred before READY is checked by the caller.
+			 */
+			return 0;
+		usleep(100);
+	}
+	return -ETIMEDOUT;
+}
+
+static int task_work_race_prepare_elem(struct task_work_race *skel)
+{
+	struct task_work_race_value value = {};
+	int key = 0;
+
+	if (!bpf_map_lookup_elem(bpf_map__fd(skel->maps.hmap), &key, &value))
+		return 0;
+	if (errno != ENOENT)
+		return -errno;
+	if (bpf_map_update_elem(bpf_map__fd(skel->maps.hmap), &key, &value,
+				BPF_NOEXIST))
+		return -errno;
+	return 0;
+}
+
+static int task_work_race_arm(struct task_work_race *skel, __u32 seq)
+{
+	if (task_work_race_set_status(skel, RACE_ARM_SEQ, seq))
+		return -errno;
+	return task_work_race_wait_ready(skel, seq);
+}
+
+static int task_work_race_check_done(struct task_work_race *skel, __u32 seq)
+{
+	int i;
+
+	for (i = 0; i < 2000000 / 100; i++) {
+		__s64 result = 0;
+
+		if (!task_work_race_status(skel, RACE_RESULT, &result) &&
+		    (__u32)((__u64)result >> 32) == seq)
+			return (__s32)result;
+		usleep(100);
+	}
+	return -ETIMEDOUT;
+}
+
+static int task_work_race_wait_callback(struct task_work_race *skel,
+					__u32 seq)
+{
+	int i, key = seq;
+
+	for (i = 0; i < 2000000 / 100; i++) {
+		__u64 completed = 0;
+
+		if (!bpf_map_lookup_elem(bpf_map__fd(skel->maps.completed),
+					 &key, &completed) && completed)
+			return 0;
+		usleep(100);
+	}
+	return -ETIMEDOUT;
+}
+
+void serial_test_task_work_race(void)
+{
+	struct task_work_race_ctx ctx = {};
+	struct task_work_race *skel;
+	pthread_t trigger, target;
+	cpu_set_t allowed;
+	int cpu, cpu_count = 0;
+	bool target_started = false, trigger_started = false;
+	int err, i, key = 0;
+
+	if (sched_getaffinity(0, sizeof(allowed), &allowed)) {
+		ASSERT_OK(-errno, "sched_getaffinity");
+		return;
+	}
+	for (cpu = 0; cpu < CPU_SETSIZE && cpu_count < 2; cpu++) {
+		if (!CPU_ISSET(cpu, &allowed))
+			continue;
+		if (!cpu_count)
+			ctx.trigger_cpu = cpu;
+		else
+			ctx.target_cpu = cpu;
+		cpu_count++;
+	}
+	if (cpu_count < 2) {
+		printf("%s:SKIP:need two CPUs in the process affinity mask\n",
+		       __func__);
+		test__skip();
+		return;
+	}
+
+	skel = task_work_race__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (!ASSERT_OK(task_work_race__attach(skel), "attach"))
+		goto cleanup;
+
+	err = pthread_create(&target, NULL, task_work_race_target, &ctx);
+	if (!ASSERT_OK(err, "pthread_create target"))
+		goto cleanup;
+	target_started = true;
+	err = pthread_create(&trigger, NULL, task_work_race_trigger, &ctx);
+	if (!ASSERT_OK(err, "pthread_create trigger"))
+		goto stop;
+	trigger_started = true;
+
+	for (i = 0; i < 5000000 / 100; i++) {
+		if (__atomic_load_n(&ctx.trigger_tid, __ATOMIC_ACQUIRE) &&
+		    __atomic_load_n(&ctx.target_tid, __ATOMIC_ACQUIRE))
+			break;
+		usleep(100);
+	}
+	if (!ASSERT_TRUE(__atomic_load_n(&ctx.trigger_tid, __ATOMIC_ACQUIRE) &&
+			 __atomic_load_n(&ctx.target_tid, __ATOMIC_ACQUIRE),
+			 "thread start"))
+		goto stop;
+	if (!ASSERT_OK(__atomic_load_n(&ctx.setup_err, __ATOMIC_ACQUIRE),
+		       "thread affinity"))
+		goto stop;
+
+	skel->bss->trigger_tid = __atomic_load_n(&ctx.trigger_tid, __ATOMIC_ACQUIRE);
+	skel->bss->target_tid = __atomic_load_n(&ctx.target_tid, __ATOMIC_ACQUIRE);
+
+	for (i = 0; i < TASK_WORK_RACE_ROUNDS; i++) {
+		__u32 seq = i + 1;
+		int variant = i % 3;
+
+		if (!ASSERT_OK(task_work_race_prepare_elem(skel), "prepare elem") ||
+		    !ASSERT_OK(task_work_race_arm(skel, seq), "round ready"))
+			goto stop;
+
+		if (variant == 2 &&
+		    !ASSERT_OK(bpf_map_delete_elem(bpf_map__fd(skel->maps.hmap),
+						   &key), "early delete"))
+			goto stop;
+
+		err = task_work_race_check_done(skel, seq);
+		/*
+		 * Variant 2 deletes the element right after READY, so losing
+		 * the race against the scheduling kfunc is a valid outcome:
+		 * the schedule itself fails with -EBUSY (ctx already FREED),
+		 * and the retry on the next tracepoint invocation fails with
+		 * -ENOENT because the element is gone.  Either way the round
+		 * exercised the deletion paths; a later variant 0 round
+		 * recreates the element.
+		 */
+		if (variant == 2 && (err == -EBUSY || err == -ENOENT))
+			err = 0;
+		if (!ASSERT_OK(err, "schedule")) {
+			fprintf(stderr, "round %d schedule failed: %d\n", i, err);
+			goto stop;
+		}
+
+		if (variant != 2 &&
+		    !ASSERT_OK(task_work_race_wait_callback(skel, seq),
+			       "callback"))
+			goto stop;
+
+		if (variant == 1 &&
+		    !ASSERT_OK(bpf_map_delete_elem(bpf_map__fd(skel->maps.hmap),
+						   &key), "late delete"))
+			goto stop;
+	}
+
+	/* A distinct completion generation prevents an old callback satisfying this. */
+	if (!ASSERT_OK(task_work_race_prepare_elem(skel), "final prepare") ||
+	    !ASSERT_OK(task_work_race_arm(skel, TASK_WORK_RACE_ROUNDS + 1),
+		       "final ready") ||
+	    !ASSERT_OK(task_work_race_check_done(skel,
+					TASK_WORK_RACE_ROUNDS + 1),
+		       "final schedule") ||
+	    !ASSERT_OK(task_work_race_wait_callback(skel,
+					     TASK_WORK_RACE_ROUNDS + 1),
+		       "final callback"))
+		goto stop;
+
+stop:
+	__atomic_store_n(&ctx.stop, 1, __ATOMIC_RELEASE);
+	if (trigger_started)
+		pthread_join(trigger, NULL);
+	if (target_started)
+		pthread_join(target, NULL);
+cleanup:
+	task_work_race__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/task_work_race.c b/tools/testing/selftests/bpf/progs/task_work_race.c
new file mode 100644
index 0000000000000..c94eca510cb37
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/task_work_race.c
@@ -0,0 +1,126 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 KylinSoft Corporation. */
+#include <vmlinux.h>
+#include <stdbool.h>
+#include <errno.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+#define TASK_WORK_RACE_MAX_SEQ	2048
+
+enum task_work_race_status {
+	RACE_ARM_SEQ,
+	RACE_READY_SEQ,
+	/* High 32 bits hold the sequence, low 32 bits the signed error. */
+	RACE_RESULT,
+	RACE_STATUS_MAX,
+};
+
+int trigger_tid;
+int target_tid;
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, RACE_STATUS_MAX);
+	__type(key, int);
+	__type(value, __s64);
+} status SEC(".maps");
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, TASK_WORK_RACE_MAX_SEQ);
+	__type(key, int);
+	__type(value, __u64);
+} completed SEC(".maps");
+
+struct task_work_race_value {
+	__u32 seq;
+	char data[60];
+	struct bpf_task_work tw;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__uint(map_flags, BPF_F_NO_PREALLOC);
+	__uint(max_entries, 4);
+	__type(key, int);
+	__type(value, struct task_work_race_value);
+} hmap SEC(".maps");
+
+static __always_inline void set_status(int key, __s64 value)
+{
+	__s64 *slot;
+
+	slot = bpf_map_lookup_elem(&status, &key);
+	if (slot)
+		*slot = value;
+}
+
+static int process_work(struct bpf_map *map, void *key, void *value)
+{
+	struct task_work_race_value *work = value;
+	__u64 *done;
+	int seq = work->seq;
+
+	if (seq <= 0 || seq >= TASK_WORK_RACE_MAX_SEQ)
+		return 0;
+	done = bpf_map_lookup_elem(&completed, &seq);
+	if (done)
+		*done = 1;
+	return 0;
+}
+
+SEC("tracepoint/syscalls/sys_enter_getppid")
+int race_sched_work(void *ctx)
+{
+	struct task_work_race_value *work;
+	struct task_struct *task;
+	__s64 *arm, *result;
+	__u32 tid = (__u32)bpf_get_current_pid_tgid();
+	int key = 0, err;
+	__u32 seq;
+
+	if (tid != trigger_tid)
+		return 0;
+
+	key = RACE_ARM_SEQ;
+	arm = bpf_map_lookup_elem(&status, &key);
+	key = RACE_RESULT;
+	result = bpf_map_lookup_elem(&status, &key);
+	if (!arm || !result || *arm <= 0 ||
+	    (__u32)((__u64)*result >> 32) == *arm)
+		return 0;
+	seq = *arm;
+
+	task = bpf_task_from_pid(target_tid);
+	if (!task) {
+		err = -ESRCH;
+		goto out_done;
+	}
+
+	key = 0;
+	work = bpf_map_lookup_elem(&hmap, &key);
+	if (!work) {
+		err = -ENOENT;
+		goto out_task;
+	}
+
+	work->seq = seq;
+	set_status(RACE_READY_SEQ, seq);
+	err = bpf_task_work_schedule_signal(task, &work->tw, &hmap,
+					    process_work);
+	if (err == -EBUSY) {
+		bpf_task_release(task);
+		return 0;
+	}
+
+out_task:
+	bpf_task_release(task);
+out_done:
+	/* Publish the sequence and its error in one store. */
+	*result = ((__u64)seq << 32) | (__u32)err;
+	return 0;
+}

3/2 2026/09/24 13:35 llm patch-evaluator
1m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 9410
							output: 32
							thoughts: 453
						

Thoughts:
**Evaluating the Syzkaller Fuzzing Potential of This Kernel Patch**

Okay, let's break this down. My goal here is to determine if this kernel patch justifies fuzzing with syzkaller. The core principle is reachability – can it be exercised in a standard virtualized environment like QEMU or GCE? We also can't blindly trust claims like "NFCI" or refactoring. Any new or modified assertions in reachable code *must* be fuzzed.

Now, when do I say "WorthFuzzing=false"? Basically, if the patch involves changes that aren't kernel-executable, like tools or selftests (unless those selftests touch kernel code). Also, hardware-specific changes that can't be reproduced on amd64 are out.

But this patch... this looks promising. It's touching `kernel/bpf/helpers.c`. BPF is a major subsystem, and it's definitely reachable from userspace through things like the `bpf` syscall and tracing mechanisms. Excellent!

Specifically, the patch seems to modify these functions in `kernel/bpf/helpers.c`:
*   `bpf_task_work_cancel(struct bpf_task_work_ctx *ctx, struct task_struct *task)`
*   `bpf_task_work_irq(struct irq_work *irq_work)`
*   `bpf_task_work_acquire_ctx(struct bpf_task_work *tw...)`
*   `bpf_task_work_cancel_scheduled(...)`
*   `bpf_task_work_fetch_ctx(...)`
*   `bpf_task_work_schedule(...)`

That seems like it's within reach of a user-space fuzzing tool.

Time to dive in and get some more context, look at what config options are relevant, and ensure nothing in the patch invalidates this initial assessment. I'll utilize the provided tools to get a better understanding of the code being modified.



4/2 2026/09/24 13:36 tool grepper
0m Args:
{
  "Expression": "bpf_task_work_schedule",
  "PathPrefix": "kernel/bpf"
}

Results:
{
  "Output": "kernel/bpf/helpers.c=4559=static struct bpf_task_work_ctx *bpf_task_work_acquire_ctx(struct bpf_task_work *tw,\n--\nkernel/bpf/helpers.c-4615-\nkernel/bpf/helpers.c:4616:static int bpf_task_work_schedule(struct task_struct *task, struct bpf_task_work *tw,\nkernel/bpf/helpers.c-4617-\t\t\t\t  struct bpf_map *map, void *callback_fn,\n--\nkernel/bpf/helpers.c-4659-/**\nkernel/bpf/helpers.c:4660: * bpf_task_work_schedule_signal - Schedule BPF callback using task_work_add with TWA_SIGNAL\nkernel/bpf/helpers.c-4661- * mode\n--\nkernel/bpf/helpers.c-4669- */\nkernel/bpf/helpers.c:4670:__bpf_kfunc int bpf_task_work_schedule_signal(struct task_struct *task, struct bpf_task_work *tw,\nkernel/bpf/helpers.c-4671-\t\t\t\t\t      void *map__const_map, bpf_task_work_callback_t callback,\n--\nkernel/bpf/helpers.c-4673-{\nkernel/bpf/helpers.c:4674:\treturn bpf_task_work_schedule(task, tw, map__const_map, callback, aux, TWA_SIGNAL);\nkernel/bpf/helpers.c-4675-}\n--\nkernel/bpf/helpers.c-4677-/**\nkernel/bpf/helpers.c:4678: * bpf_task_work_schedule_resume - Schedule BPF callback using task_work_add with TWA_RESUME\nkernel/bpf/helpers.c-4679- * mode\n--\nkernel/bpf/helpers.c-4687- */\nkernel/bpf/helpers.c:4688:__bpf_kfunc int bpf_task_work_schedule_resume(struct task_struct *task, struct bpf_task_work *tw,\nkernel/bpf/helpers.c-4689-\t\t\t\t\t      void *map__const_map, bpf_task_work_callback_t callback,\n--\nkernel/bpf/helpers.c-4691-{\nkernel/bpf/helpers.c:4692:\treturn bpf_task_work_schedule(task, tw, map__const_map, callback, aux, TWA_RESUME);\nkernel/bpf/helpers.c-4693-}\n--\nkernel/bpf/helpers.c=4986=BTF_ID_FLAGS(func, bpf_stream_print_stack, KF_IMPLICIT_ARGS | KF_SPINLOCK_SAFE)\nkernel/bpf/helpers.c:4987:BTF_ID_FLAGS(func, bpf_task_work_schedule_signal, KF_IMPLICIT_ARGS)\nkernel/bpf/helpers.c:4988:BTF_ID_FLAGS(func, bpf_task_work_schedule_resume, KF_IMPLICIT_ARGS)\nkernel/bpf/helpers.c-4989-BTF_ID_FLAGS(func, bpf_dynptr_from_file)\n--\nkernel/bpf/verifier.c=11678=enum special_kfunc_type {\n--\nkernel/bpf/verifier.c-11740-\tKF___bpf_trap,\nkernel/bpf/verifier.c:11741:\tKF_bpf_task_work_schedule_signal,\nkernel/bpf/verifier.c:11742:\tKF_bpf_task_work_schedule_resume,\nkernel/bpf/verifier.c-11743-\tKF_bpf_arena_alloc_pages,\n--\nkernel/bpf/verifier.c=11830=BTF_ID(func, __bpf_trap)\nkernel/bpf/verifier.c:11831:BTF_ID(func, bpf_task_work_schedule_signal)\nkernel/bpf/verifier.c:11832:BTF_ID(func, bpf_task_work_schedule_resume)\nkernel/bpf/verifier.c-11833-BTF_ID(func, bpf_arena_alloc_pages)\n--\nkernel/bpf/verifier.c=11886=static bool is_task_work_add_kfunc(u32 func_id)\nkernel/bpf/verifier.c-11887-{\nkernel/bpf/verifier.c:11888:\treturn func_id == special_kfunc_list[KF_bpf_task_work_schedule_signal] ||\nkernel/bpf/verifier.c:11889:\t       func_id == special_kfunc_list[KF_bpf_task_work_schedule_resume];\nkernel/bpf/verifier.c-11890-}\n"
}

5/2 2026/09/24 13:36 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 11600
							output: 37
							thoughts: 39
						

Thoughts:
Here's a breakdown of my thought process:

**Diving Deep into `kernel/bpf/helpers.c`**

Okay, so I need to understand some changes in `kernel/bpf/helpers.c`, specifically between lines 4437 and 4800. My initial thought is to pinpoint *exactly* what modifications occurred within that segment of code. I'll need to examine the diffs to see the added, deleted, and modified lines. I expect these changes relate to some specific functionality, so I should be able to deduce the nature of the change through the context.

Beyond the code itself, I need to consider the broader implications. I'll immediately start thinking about what kernel configurations might be relevant here. What features or options within the kernel build process are likely tied to this portion of `helpers.c`? Are we talking about specific BPF helper calls, potentially some new features, or maybe fixes for existing issues? I'm going to quickly try to cross-reference any identifiers I see in the lines to see if there are associated documentation or relevant kernel commit messages.

In short, I'm aiming to understand the *what* and the *why* of these code changes, along with any external dependencies that might be related to them.



6/2 2026/09/24 13:36 tool read-file
0m Args:
{
  "File": "kernel/bpf/helpers.c",
  "FirstLine": 4480,
  "LineCount": 80
}

Results:
{
  "Contents": "4480:\t\n4481:\t\tbpf_task_work_ctx_reset(ctx);\n4482:\t\t(void)cmpxchg(\u0026ctx-\u003estate, BPF_TW_RUNNING, BPF_TW_STANDBY);\n4483:\t\n4484:\t\tbpf_task_work_ctx_put(ctx);\n4485:\t}\n4486:\t\n4487:\tstatic void bpf_task_work_irq(struct irq_work *irq_work)\n4488:\t{\n4489:\t\tstruct bpf_task_work_ctx *ctx = container_of(irq_work, struct bpf_task_work_ctx, irq_work);\n4490:\t\tstruct task_struct *task;\n4491:\t\tenum bpf_task_work_state state;\n4492:\t\tint err;\n4493:\t\n4494:\t\tguard(rcu)();\n4495:\t\n4496:\t\tif (cmpxchg(\u0026ctx-\u003estate, BPF_TW_PENDING, BPF_TW_SCHEDULING) != BPF_TW_PENDING) {\n4497:\t\t\tbpf_task_work_ctx_put(ctx);\n4498:\t\t\treturn;\n4499:\t\t}\n4500:\t\n4501:\t\ttask = get_task_struct(ctx-\u003etask);\n4502:\t\terr = task_work_add(task, \u0026ctx-\u003ework, ctx-\u003emode);\n4503:\t\tif (err) {\n4504:\t\t\tbpf_task_work_ctx_reset(ctx);\n4505:\t\t\t/*\n4506:\t\t\t * try to switch back to STANDBY for another task_work reuse, but we might have\n4507:\t\t\t * gone to FREED already, which is fine as we already cleaned up after ourselves\n4508:\t\t\t */\n4509:\t\t\t(void)cmpxchg(\u0026ctx-\u003estate, BPF_TW_SCHEDULING, BPF_TW_STANDBY);\n4510:\t\t\tbpf_task_work_ctx_put(ctx);\n4511:\t\t\tput_task_struct(task);\n4512:\t\t\treturn;\n4513:\t\t}\n4514:\t\n4515:\t\t/*\n4516:\t\t * The callback may have completed and reset ctx-\u003etask already. Keep the\n4517:\t\t * captured task alive until this invocation is done with cancellation.\n4518:\t\t * The RCU read-side section keeps ctx memory alive until then.\n4519:\t\t */\n4520:\t\tstate = cmpxchg(\u0026ctx-\u003estate, BPF_TW_SCHEDULING, BPF_TW_SCHEDULED);\n4521:\t\tif (state == BPF_TW_FREED)\n4522:\t\t\tbpf_task_work_cancel(ctx, task); /* clean up if we switched into FREED state */\n4523:\t\n4524:\t\tput_task_struct(task);\n4525:\t}\n4526:\t\n4527:\tstatic struct bpf_task_work_ctx *bpf_task_work_fetch_ctx(struct bpf_task_work *tw,\n4528:\t\t\t\t\t\t\t\t struct bpf_map *map)\n4529:\t{\n4530:\t\tstruct bpf_task_work_kern *twk = (void *)tw;\n4531:\t\tstruct bpf_task_work_ctx *ctx, *old_ctx;\n4532:\t\n4533:\t\tctx = READ_ONCE(twk-\u003ectx);\n4534:\t\tif (ctx)\n4535:\t\t\treturn ctx;\n4536:\t\n4537:\t\tctx = bpf_map_kmalloc_nolock(map, sizeof(*ctx), 0, NUMA_NO_NODE);\n4538:\t\tif (!ctx)\n4539:\t\t\treturn ERR_PTR(-ENOMEM);\n4540:\t\n4541:\t\tmemset(ctx, 0, sizeof(*ctx));\n4542:\t\trefcount_set(\u0026ctx-\u003erefcnt, 1); /* map's own ref */\n4543:\t\tctx-\u003estate = BPF_TW_STANDBY;\n4544:\t\tinit_irq_work(\u0026ctx-\u003eirq_work, bpf_task_work_irq);\n4545:\t\n4546:\t\told_ctx = cmpxchg(\u0026twk-\u003ectx, NULL, ctx);\n4547:\t\tif (old_ctx) {\n4548:\t\t\t/*\n4549:\t\t\t * tw-\u003ectx is set by concurrent BPF program, release allocated\n4550:\t\t\t * memory and try to reuse already set context.\n4551:\t\t\t */\n4552:\t\t\tkfree_nolock(ctx);\n4553:\t\t\treturn old_ctx;\n4554:\t\t}\n4555:\t\n4556:\t\treturn ctx; /* Success */\n4557:\t}\n4558:\t\n4559:\tstatic struct bpf_task_work_ctx *bpf_task_work_acquire_ctx(struct bpf_task_work *tw,\n"
}

7/2 2026/09/24 13:36 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 13281
							output: 38
							thoughts: 41
						

Thoughts:
Okay, here's my interpretation of what I need to do:

**Analyzing BPF Task Work Acquisition and Scheduling**

Alright, let's dive in. To get a comprehensive understanding of the BPF task work mechanics, I need to focus on lines 4560-4660. The specific functions that warrant attention are `bpf_task_work_acquire_ctx` and `bpf_task_work_schedule`. This section likely holds the core logic for how a BPF program interacts with the kernel's task work mechanism. I expect to find details about how contexts are acquired and managed and how work is scheduled within the task. I will look for how data structures are allocated, the locking mechanisms being used (if any), and the overall flow of execution. I'll need to pay close attention to the context switching implications, error handling, and any potential race conditions that might be present in this area. This deeper dive will solidify my comprehension of BPF's role in the task management subsystem.



8/2 2026/09/24 13:36 tool read-file
0m Args:
{
  "File": "kernel/bpf/helpers.c",
  "FirstLine": 4560,
  "LineCount": 100
}

Results:
{
  "Contents": "4560:\t\t\t\t\t\t\t\t   struct bpf_map *map)\n4561:\t{\n4562:\t\tstruct bpf_task_work_ctx *ctx;\n4563:\t\n4564:\t\t/*\n4565:\t\t * Sleepable BPF programs hold rcu_read_lock_trace but not\n4566:\t\t * regular rcu_read_lock. Since kfree_rcu waits for regular\n4567:\t\t * RCU GP, the ctx can be freed while we're between reading\n4568:\t\t * the pointer and incrementing the refcount. Take regular\n4569:\t\t * rcu_read_lock to prevent kfree_rcu from freeing the ctx\n4570:\t\t * before we can tryget it.\n4571:\t\t */\n4572:\t\tscoped_guard(rcu) {\n4573:\t\t\tctx = bpf_task_work_fetch_ctx(tw, map);\n4574:\t\t\tif (IS_ERR(ctx))\n4575:\t\t\t\treturn ctx;\n4576:\t\n4577:\t\t\t/* try to get ref for task_work callback to hold */\n4578:\t\t\tif (!bpf_task_work_ctx_tryget(ctx))\n4579:\t\t\t\treturn ERR_PTR(-EBUSY);\n4580:\t\t}\n4581:\t\n4582:\t\tif (cmpxchg(\u0026ctx-\u003estate, BPF_TW_STANDBY, BPF_TW_PENDING) != BPF_TW_STANDBY) {\n4583:\t\t\t/* lost acquiring race or map_release_uref() stole it from us, put ref and bail */\n4584:\t\t\tbpf_task_work_ctx_put(ctx);\n4585:\t\t\treturn ERR_PTR(-EBUSY);\n4586:\t\t}\n4587:\t\t/*\n4588:\t\t * The callback or add-failure path can publish STANDBY before the\n4589:\t\t * scheduling irq_work returns. Do not let a new round reuse the context\n4590:\t\t * until BUSY clears. Otherwise the old handler can consume the new\n4591:\t\t * round's SCHEDULING state and publish a false SCHEDULED state, and two\n4592:\t\t * invocations can overlap on different CPUs.\n4593:\t\t */\n4594:\t\tif (unlikely(irq_work_is_busy(\u0026ctx-\u003eirq_work))) {\n4595:\t\t\t(void)cmpxchg(\u0026ctx-\u003estate, BPF_TW_PENDING, BPF_TW_STANDBY);\n4596:\t\t\tbpf_task_work_ctx_put(ctx);\n4597:\t\t\treturn ERR_PTR(-EBUSY);\n4598:\t\t}\n4599:\t\n4600:\t\t/*\n4601:\t\t * If no process or bpffs is holding a reference to the map, no new callbacks should be\n4602:\t\t * scheduled. This does not address any race or correctness issue, but rather is a policy\n4603:\t\t * choice: dropping user references should stop everything.\n4604:\t\t */\n4605:\t\tif (!atomic64_read(\u0026map-\u003eusercnt)) {\n4606:\t\t\t/* drop ref we just got for task_work callback itself */\n4607:\t\t\tbpf_task_work_ctx_put(ctx);\n4608:\t\t\t/* transfer map's ref into cancel_and_free() */\n4609:\t\t\tbpf_task_work_cancel_and_free(tw);\n4610:\t\t\treturn ERR_PTR(-EBUSY);\n4611:\t\t}\n4612:\t\n4613:\t\treturn ctx;\n4614:\t}\n4615:\t\n4616:\tstatic int bpf_task_work_schedule(struct task_struct *task, struct bpf_task_work *tw,\n4617:\t\t\t\t\t  struct bpf_map *map, void *callback_fn,\n4618:\t\t\t\t\t  struct bpf_prog_aux *aux, enum task_work_notify_mode mode)\n4619:\t{\n4620:\t\tstruct bpf_prog *prog;\n4621:\t\tstruct bpf_task_work_ctx *ctx;\n4622:\t\tint err;\n4623:\t\n4624:\t\tBTF_TYPE_EMIT(struct bpf_task_work);\n4625:\t\n4626:\t\tprog = bpf_prog_inc_not_zero(aux-\u003eprog);\n4627:\t\tif (IS_ERR(prog))\n4628:\t\t\treturn -EBADF;\n4629:\t\ttask = bpf_task_acquire(task);\n4630:\t\tif (!task) {\n4631:\t\t\terr = -EBADF;\n4632:\t\t\tgoto release_prog;\n4633:\t\t}\n4634:\t\n4635:\t\tctx = bpf_task_work_acquire_ctx(tw, map);\n4636:\t\tif (IS_ERR(ctx)) {\n4637:\t\t\terr = PTR_ERR(ctx);\n4638:\t\t\tgoto release_all;\n4639:\t\t}\n4640:\t\n4641:\t\tctx-\u003etask = task;\n4642:\t\tctx-\u003ecallback_fn = (bpf_callback_t)callback_fn;\n4643:\t\tctx-\u003eprog = prog;\n4644:\t\tctx-\u003emode = mode;\n4645:\t\tctx-\u003emap = map;\n4646:\t\tctx-\u003emap_val = (void *)tw - map-\u003erecord-\u003etask_work_off;\n4647:\t\tinit_task_work(\u0026ctx-\u003ework, bpf_task_work_callback);\n4648:\t\n4649:\t\tirq_work_queue(\u0026ctx-\u003eirq_work);\n4650:\t\treturn 0;\n4651:\t\n4652:\trelease_all:\n4653:\t\tbpf_task_release(task);\n4654:\trelease_prog:\n4655:\t\tbpf_prog_put(prog);\n4656:\t\treturn err;\n4657:\t}\n4658:\t\n4659:\t/**\n"
}

9/2 2026/09/24 13:36 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 15174
							output: 37
							thoughts: 18
						

Thoughts:
Okay, I'm ready. Here's a summary as if I'm thinking the text's prompt:

**Analyzing the Relevant Section**

Right, let's zero in on those lines, 4760 through 4810. That specific range of code, or whatever content is contained there, should hold the key insights I'm looking for. I need to understand the nuances of the concepts discussed in that passage to fully comprehend the matter, so this is where I'll concentrate my efforts. Hopefully, this section will shed light on the core problem I'm trying to solve.



10/2 2026/09/24 13:36 tool read-file
0m Args:
{
  "File": "kernel/bpf/helpers.c",
  "FirstLine": 4760,
  "LineCount": 60
}

Results:
{
  "Contents": "4760:\t\tstruct bpf_async_kern *async = (void *)timer;\n4761:\t\tstruct bpf_async_cb *cb;\n4762:\t\tint ret;\n4763:\t\n4764:\t\tcb = READ_ONCE(async-\u003ecb);\n4765:\t\tif (!cb)\n4766:\t\t\treturn -EINVAL;\n4767:\t\n4768:\t\t/*\n4769:\t\t * Unlike hrtimer_start() it's ok to synchronously call\n4770:\t\t * hrtimer_try_to_cancel() when refcnt reached zero, but deferring to\n4771:\t\t * irq_work is not, since irq callback may execute after RCU GP and\n4772:\t\t * cb could be freed at that time. Check for refcnt zero for\n4773:\t\t * consistency.\n4774:\t\t */\n4775:\t\tif (!refcount_inc_not_zero(\u0026cb-\u003erefcnt))\n4776:\t\t\treturn -ENOENT;\n4777:\t\n4778:\t\tif (!defer_timer_wq_op()) {\n4779:\t\t\tstruct bpf_hrtimer *t = container_of(cb, struct bpf_hrtimer, cb);\n4780:\t\n4781:\t\t\tret = hrtimer_try_to_cancel(\u0026t-\u003etimer);\n4782:\t\t\tbpf_async_refcount_put(cb);\n4783:\t\t\treturn ret;\n4784:\t\t} else {\n4785:\t\t\tret = bpf_async_schedule_op(cb, BPF_ASYNC_CANCEL, 0, 0);\n4786:\t\t\treturn ret ? ret : -ECANCELED;\n4787:\t\t}\n4788:\t}\n4789:\t\n4790:\t__bpf_kfunc_end_defs();\n4791:\t\n4792:\tstatic void bpf_task_work_cancel_scheduled(struct irq_work *irq_work)\n4793:\t{\n4794:\t\tstruct bpf_task_work_ctx *ctx = container_of(irq_work, struct bpf_task_work_ctx, irq_work);\n4795:\t\n4796:\t\t/* SCHEDULED -\u003e FREED keeps ctx-\u003etask stable until this runs. */\n4797:\t\tbpf_task_work_cancel(ctx, ctx-\u003etask); /* this might put task_work callback's ref */\n4798:\t\tbpf_task_work_ctx_put(ctx); /* and here we put map's own ref that was transferred to us */\n4799:\t}\n4800:\t\n4801:\tvoid bpf_task_work_cancel_and_free(void *val)\n4802:\t{\n4803:\t\tstruct bpf_task_work_kern *twk = val;\n4804:\t\tstruct bpf_task_work_ctx *ctx;\n4805:\t\tenum bpf_task_work_state state;\n4806:\t\n4807:\t\tctx = xchg(\u0026twk-\u003ectx, NULL);\n4808:\t\tif (!ctx)\n4809:\t\t\treturn;\n4810:\t\n4811:\t\tstate = xchg(\u0026ctx-\u003estate, BPF_TW_FREED);\n4812:\t\tif (state == BPF_TW_SCHEDULED) {\n4813:\t\t\t/* run in irq_work to avoid locks in NMI */\n4814:\t\t\tinit_irq_work(\u0026ctx-\u003eirq_work, bpf_task_work_cancel_scheduled);\n4815:\t\t\tirq_work_queue(\u0026ctx-\u003eirq_work);\n4816:\t\t\treturn;\n4817:\t\t}\n4818:\t\n4819:\t\tbpf_task_work_ctx_put(ctx); /* put bpf map's ref */\n"
}

11/2 2026/09/24 13:36 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 16372
							output: 27
							thoughts: 41
						

Thoughts:
Okay, let's dive into this. Here's how I'd approach this task, as if these were my internal thoughts:

**Exploring BPF Task Work and Helper Build Configuration**

Alright, I need to get a handle on how BPF task work and its associated helpers are configured and built within the kernel. My first instinct is to poke around Kconfig, see how these things are enabled or disabled. That's the go-to place for feature flags, after all. Specifically, I'm thinking about how the BPF task work functionality is governed.

Next, I need to understand where the implementation of those helper functions resides. I have a hunch it's in `kernel/bpf/helpers.c`, but I need to confirm. The Makefile for that directory would be my next target. It should provide insight into the build process, dependencies, and any conditional compilation based on Kconfig options I might have identified earlier. This is the critical link between the configuration and the actual compilation. The Makefile will show me which sources get compiled into what object files, and under what conditions.



12/2 2026/09/24 13:37 tool grepper
0m Args:
{
  "Expression": "helpers\\.o",
  "PathPrefix": "kernel/bpf"
}

Results:
{
  "Output": "kernel/bpf/Makefile=7=CFLAGS_core.o += -Wno-override-init $(cflags-nogcse-yy)\nkernel/bpf/Makefile-8-\nkernel/bpf/Makefile:9:obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o cnum.o log.o token.o liveness.o const_fold.o diagnostics.o\nkernel/bpf/Makefile-10-obj-$(CONFIG_BPF_SYSCALL) += bpf_iter.o map_iter.o task_iter.o prog_iter.o link_iter.o\n"
}

13/2 2026/09/24 13:37 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 16834
							output: 29
							thoughts: 54
						

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

**Investigating BPF Helper Dependencies**

Right, so I'm looking at `kernel/bpf/helpers.o` and it seems to have a dependency on `CONFIG_BPF_SYSCALL`. That's the starting point. I need to understand if this dependency actually matters for the specific code I'm dealing with.

My immediate thought is, let's drill down. I suspect that the dependency on `CONFIG_BPF_SYSCALL` *could* be related to certain BPF helper functions, but I want to verify that. My next step is to examine the `kernel/bpf/helpers.c` source code, specifically looking for any `#ifdef` preprocessor directives. I'm especially interested in whether any code *conditionally* includes the definition or usage of `bpf_task_work` based on that configuration option. If `bpf_task_work` is wrapped in an `#ifdef CONFIG_BPF_SYSCALL`, that's the most direct indicator of a link, and my next actions will be contingent on the results of that check. This is standard procedure when dealing with kernel configuration dependencies.



14/2 2026/09/24 13:37 tool grepper
0m Error:
no matches

Args:
{
  "Expression": "ifdef.*task_work",
  "PathPrefix": "kernel/bpf"
}

Results:
{
  "error": "no matches"
}

15/2 2026/09/24 13:37 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 17191
							output: 37
							thoughts: 36
						

Thoughts:
Okay, let's dive into this. Here's how I'd approach that kernel code:

**Diving into the BPF Task Work Implementation**

Alright, my first instinct is to zero in on the `kernel/bpf/helpers.c` file, specifically the section concerning `task_work`. I need to understand how the BPF subsystem interacts with scheduled task work items. My initial focus will be lines 4400-4450. That should give me the entry point or, at least, the beginning of the implementation for BPF task work functions. From there, I'll expect to find function declarations, perhaps some initialization routines, and likely, how BPF programs are attached to and triggered by this `task_work` mechanism. Understanding the control flow and data structures there is key. This is where the magic happens and this section of the code should contain those mechanics. I'll need to pay close attention to the data structures used here too. Let's get to it.



16/2 2026/09/24 13:37 tool read-file
0m Args:
{
  "File": "kernel/bpf/helpers.c",
  "FirstLine": 4400,
  "LineCount": 50
}

Results:
{
  "Contents": "4400:\t};\n4401:\t\n4402:\tstatic void bpf_task_work_ctx_reset(struct bpf_task_work_ctx *ctx)\n4403:\t{\n4404:\t\tif (ctx-\u003eprog) {\n4405:\t\t\tbpf_prog_put(ctx-\u003eprog);\n4406:\t\t\tctx-\u003eprog = NULL;\n4407:\t\t}\n4408:\t\tif (ctx-\u003etask) {\n4409:\t\t\tbpf_task_release(ctx-\u003etask);\n4410:\t\t\tctx-\u003etask = NULL;\n4411:\t\t}\n4412:\t}\n4413:\t\n4414:\tstatic bool bpf_task_work_ctx_tryget(struct bpf_task_work_ctx *ctx)\n4415:\t{\n4416:\t\treturn refcount_inc_not_zero(\u0026ctx-\u003erefcnt);\n4417:\t}\n4418:\t\n4419:\tstatic void bpf_task_work_destroy(struct irq_work *irq_work)\n4420:\t{\n4421:\t\tstruct bpf_task_work_ctx *ctx = container_of(irq_work, struct bpf_task_work_ctx, irq_work);\n4422:\t\n4423:\t\tbpf_task_work_ctx_reset(ctx);\n4424:\t\tkfree_rcu(ctx, rcu);\n4425:\t}\n4426:\t\n4427:\tstatic void bpf_task_work_ctx_put(struct bpf_task_work_ctx *ctx)\n4428:\t{\n4429:\t\tif (!refcount_dec_and_test(\u0026ctx-\u003erefcnt))\n4430:\t\t\treturn;\n4431:\t\n4432:\t\tif (irqs_disabled()) {\n4433:\t\t\tctx-\u003eirq_work = IRQ_WORK_INIT(bpf_task_work_destroy);\n4434:\t\t\tirq_work_queue(\u0026ctx-\u003eirq_work);\n4435:\t\t} else {\n4436:\t\t\tbpf_task_work_destroy(\u0026ctx-\u003eirq_work);\n4437:\t\t}\n4438:\t}\n4439:\t\n4440:\tstatic void bpf_task_work_cancel(struct bpf_task_work_ctx *ctx,\n4441:\t\t\t\t\t struct task_struct *task)\n4442:\t{\n4443:\t\t/*\n4444:\t\t * Scheduled task_work callback holds ctx ref, so if we successfully\n4445:\t\t * cancelled, we put that ref on callback's behalf. If we couldn't\n4446:\t\t * cancel, callback will inevitably run or has already completed\n4447:\t\t * running, and it would have taken care of its ctx ref itself.\n4448:\t\t */\n4449:\t\tif (task_work_cancel(task, \u0026ctx-\u003ework))\n"
}

17/2 2026/09/24 13:37 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 18261
							output: 31
							thoughts: 536
						

Thoughts:
**WorthFuzzing Analysis: BPF Task Work Scheduling Fix**

Okay, let's break this down. My initial thought is to understand the scope of `bpf_task_work_schedule_signal` and `bpf_task_work_schedule_resume`. I know these are BPF kfuncs, exposed to BPF programs. The critical question here is, are they reachable? The answer is yes, they are exposed and, therefore, accessible.

Given that reachability, can syzkaller fuzz BPF programs? Yes, syzkaller utilizes `bpf(BPF_PROG_LOAD, ...)` to load BPF programs, and executes them, so that confirms my hypothesis.

Now, let's consider the code changes themselves, located in `kernel/bpf/helpers.c`. We're talking about modifications to the concurrency and lifecycle handling of `task_work` within the BPF framework. Specifically, I see:

*   Extra reference via `get_task_struct` in `bpf_task_work_irq`.
*   A busy check via `irq_work_is_busy(&ctx->irq_work)` in `bpf_task_work_acquire_ctx`.
*   Moving `init_irq_work(...)` to `bpf_task_work_fetch_ctx`.
*   Passing the `task` pointer to `bpf_task_work_cancel`.

It's immediately clear this is a bugfix. It addresses race conditions in the BPF task\_work scheduling and cancellation mechanisms. The BPF subsystem is a core kernel subsystem, and the code path is completely reachable from userspace, thanks to the BPF syscall.

Therefore, applying our criteria, specifically the "Core Subsystems" criterion of "Any logic modifications in BPF", the analysis results in `WorthFuzzing = true`. This is a definite positive.

Now, I need to identify the enabling kernel configs. Since this relates to BPF, I'll examine `BPF_SYSCALL` and its dependencies, probably inspecting `init/Kconfig` or `kernel/bpf/Kconfig`.



18/2 2026/09/24 13:37 tool grepper
0m Args:
{
  "Expression": "config BPF_SYSCALL",
  "PathPrefix": "kernel/bpf"
}

Results:
{
  "Output": "kernel/bpf/Kconfig=25=menu \"BPF subsystem\"\nkernel/bpf/Kconfig-26-\nkernel/bpf/Kconfig:27:config BPF_SYSCALL\nkernel/bpf/Kconfig-28-\tbool \"Enable bpf() system call\"\n"
}

19/2 2026/09/24 13:37 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 19372
							output: 34
							thoughts: 19
						

Thoughts:
Here's a summary of my thought process, keeping in mind my expert understanding:

**Navigating the BPF Kernel Configuration**

Okay, I need to understand something about the BPF configuration in the kernel. My initial thought is to zero in on `kernel/bpf/Kconfig`, specifically around line 27. I'm assuming that this area contains the crucial settings or options that control how BPF is integrated and enabled in this particular kernel version. I'll need to rapidly interpret the context provided by that section of the Kconfig file. I'll be looking for relevant `bool`, `tristate`, or `depends on` statements to get a handle on what features are available and what dependencies they have. I’m thinking there will be options to enable or disable different BPF features, maybe related to eBPF or specific BPF helpers. I’m also mentally preparing to evaluate the default settings and see if they align with my current needs or debugging tasks. Let's dig in and quickly assess the relevant configurations.



20/2 2026/09/24 13:37 tool read-file
0m Args:
{
  "File": "kernel/bpf/Kconfig",
  "FirstLine": 20,
  "LineCount": 40
}

Results:
{
  "Contents": "  20:\t# Used by archs to tell that they want the BPF JIT compiler enabled by\n  21:\t# default for kernels that were compiled with BPF JIT support.\n  22:\tconfig ARCH_WANT_DEFAULT_BPF_JIT\n  23:\t\tbool\n  24:\t\n  25:\tmenu \"BPF subsystem\"\n  26:\t\n  27:\tconfig BPF_SYSCALL\n  28:\t\tbool \"Enable bpf() system call\"\n  29:\t\tselect BPF\n  30:\t\tselect IRQ_WORK\n  31:\t\tselect NEED_TASKS_RCU\n  32:\t\tselect TASKS_TRACE_RCU\n  33:\t\tselect BINARY_PRINTF\n  34:\t\tselect NET_SOCK_MSG if NET\n  35:\t\tselect NET_XGRESS if NET\n  36:\t\tselect PAGE_POOL if NET\n  37:\t\tdefault n\n  38:\t\thelp\n  39:\t\t  Enable the bpf() system call that allows to manipulate BPF programs\n  40:\t\t  and maps via file descriptors.\n  41:\t\n  42:\tconfig BPF_JIT\n  43:\t\tbool \"Enable BPF Just In Time compiler\"\n  44:\t\tdepends on BPF\n  45:\t\tdepends on HAVE_CBPF_JIT || HAVE_EBPF_JIT\n  46:\t\tselect EXECMEM\n  47:\t\thelp\n  48:\t\t  BPF programs are normally handled by a BPF interpreter. This option\n  49:\t\t  allows the kernel to generate native code when a program is loaded\n  50:\t\t  into the kernel. This will significantly speed-up processing of BPF\n  51:\t\t  programs.\n  52:\t\n  53:\t\t  Note, an admin should enable this feature changing:\n  54:\t\t  /proc/sys/net/core/bpf_jit_enable\n  55:\t\t  /proc/sys/net/core/bpf_jit_harden   (optional)\n  56:\t\t  /proc/sys/net/core/bpf_jit_kallsyms (optional)\n  57:\t\n  58:\tconfig BPF_JIT_ALWAYS_ON\n  59:\t\tbool \"Permanently enable BPF JIT and remove BPF interpreter\"\n"
}

21/2 2026/09/24 13:37 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 20223
							output: 35
							thoughts: 85
						

Thoughts:
**My Current Kfunc/BTF Configuration Concerns**

Okay, so I'm thinking, are there any other configurations I might be missing for these kernel function (kfunc) and BTF (Berkeley Packet Filter Type Format) registrations? I need to make sure I haven't overlooked something crucial. Let's see, the most common pitfall I can foresee is forgetting to enable `DEBUG_INFO_BTF`. My experience tells me that BTF kfunc registration almost always goes hand-in-hand with that particular config option. So, that's the obvious place to start checking.

After that, I really need to get a handle on the code flow. I should probably hunt down where `register_btf_kfunc_id_set` is called. That should give me a good overview of the BTF kfunc registration process. And while I'm at it, I should also look for calls related to registering the BTF IDs for `bpf_task_work_schedule_signal`. Those are key pieces of the puzzle and will highlight registration paths.



22/2 2026/09/24 13:37 tool grepper
0m Args:
{
  "Expression": "register_btf_kfunc_id_set",
  "PathPrefix": "kernel/bpf"
}

Results:
{
  "Output": "kernel/bpf/arena.c=1131=static int __init kfunc_init(void)\nkernel/bpf/arena.c-1132-{\nkernel/bpf/arena.c:1133:\treturn register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, \u0026common_kfunc_set);\nkernel/bpf/arena.c-1134-}\n--\nkernel/bpf/btf.c=8925=static int btf_populate_kfunc_set(struct btf *btf, enum btf_kfunc_hook hook,\n--\nkernel/bpf/btf.c-8971-\tset = tab-\u003esets[hook];\nkernel/bpf/btf.c:8972:\t/* Warn when register_btf_kfunc_id_set is called twice for the same hook\nkernel/bpf/btf.c-8973-\t * for module sets.\n--\nkernel/bpf/btf.c=9196=u32 *btf_kfunc_is_modify_return(const struct btf *btf, u32 kfunc_btf_id,\n--\nkernel/bpf/btf.c-9204-\nkernel/bpf/btf.c:9205:static int __register_btf_kfunc_id_set(enum btf_kfunc_hook hook,\nkernel/bpf/btf.c-9206-\t\t\t\t       const struct btf_kfunc_id_set *kset)\n--\nkernel/bpf/btf.c-9231-/* This function must be invoked only from initcalls/module init functions */\nkernel/bpf/btf.c:9232:int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,\nkernel/bpf/btf.c-9233-\t\t\t      const struct btf_kfunc_id_set *kset)\n--\nkernel/bpf/btf.c-9245-\thook = bpf_prog_type_to_kfunc_hook(prog_type);\nkernel/bpf/btf.c:9246:\treturn __register_btf_kfunc_id_set(hook, kset);\nkernel/bpf/btf.c-9247-}\nkernel/bpf/btf.c:9248:EXPORT_SYMBOL_GPL(register_btf_kfunc_id_set);\nkernel/bpf/btf.c-9249-\n--\nkernel/bpf/btf.c=9251=int register_btf_fmodret_id_set(const struct btf_kfunc_id_set *kset)\nkernel/bpf/btf.c-9252-{\nkernel/bpf/btf.c:9253:\treturn __register_btf_kfunc_id_set(BTF_KFUNC_HOOK_FMODRET, kset);\nkernel/bpf/btf.c-9254-}\n--\nkernel/bpf/cpumask.c=515=static int __init cpumask_kfunc_init(void)\n--\nkernel/bpf/cpumask.c-525-\tret = bpf_mem_alloc_init(\u0026bpf_cpumask_ma, sizeof(struct bpf_cpumask), false);\nkernel/bpf/cpumask.c:526:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, \u0026cpumask_kfunc_set);\nkernel/bpf/cpumask.c:527:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, \u0026cpumask_kfunc_set);\nkernel/bpf/cpumask.c:528:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, \u0026cpumask_kfunc_set);\nkernel/bpf/cpumask.c-529-\treturn  ret ?: register_btf_id_dtor_kfuncs(cpumask_dtors,\n--\nkernel/bpf/crypto.c=379=static int __init crypto_kfunc_init(void)\n--\nkernel/bpf/crypto.c-388-\nkernel/bpf/crypto.c:389:\tret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, \u0026crypt_kfunc_set);\nkernel/bpf/crypto.c:390:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_ACT, \u0026crypt_kfunc_set);\nkernel/bpf/crypto.c:391:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, \u0026crypt_kfunc_set);\nkernel/bpf/crypto.c:392:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL,\nkernel/bpf/crypto.c-393-\t\t\t\t\t       \u0026crypt_init_kfunc_set);\n--\nkernel/bpf/helpers.c=4999=static int __init kfunc_init(void)\n--\nkernel/bpf/helpers.c-5014-\nkernel/bpf/helpers.c:5015:\tret = register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, \u0026generic_kfunc_set);\nkernel/bpf/helpers.c:5016:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, \u0026generic_kfunc_set);\nkernel/bpf/helpers.c:5017:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, \u0026generic_kfunc_set);\nkernel/bpf/helpers.c:5018:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, \u0026generic_kfunc_set);\nkernel/bpf/helpers.c:5019:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, \u0026generic_kfunc_set);\nkernel/bpf/helpers.c:5020:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SKB, \u0026generic_kfunc_set);\nkernel/bpf/helpers.c-5021-\tret = ret ?: register_btf_id_dtor_kfuncs(generic_dtors,\n--\nkernel/bpf/helpers.c-5023-\t\t\t\t\t\t  THIS_MODULE);\nkernel/bpf/helpers.c:5024:\treturn ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, \u0026common_kfunc_set);\nkernel/bpf/helpers.c-5025-}\n--\nkernel/bpf/map_iter.c=230=static int init_subsystem(void)\nkernel/bpf/map_iter.c-231-{\nkernel/bpf/map_iter.c:232:\treturn register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, \u0026bpf_map_iter_kfunc_set);\nkernel/bpf/map_iter.c-233-}\n--\nkernel/bpf/rqspinlock.c=759=static __init int rqspinlock_register_kfuncs(void)\nkernel/bpf/rqspinlock.c-760-{\nkernel/bpf/rqspinlock.c:761:\treturn register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, \u0026rqspinlock_kfunc_set);\nkernel/bpf/rqspinlock.c-762-}\n"
}

23/2 2026/09/24 13:37 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 22597
							output: 37
							thoughts: 52
						

Thoughts:
Here's my take on those thoughts:

**Analyzing `generic_kfunc_set` in the BPF Helpers**

Okay, so I'm digging into `generic_kfunc_set` within `kernel/bpf/helpers.c`. I need to understand its behavior, and to do that, I'm focusing on the code around lines 4990 to 5030. That block should give me a good overview of how it's implemented. I will investigate how it's used within the broader BPF context.



24/2 2026/09/24 13:37 tool read-file
0m Args:
{
  "File": "kernel/bpf/helpers.c",
  "FirstLine": 4980,
  "LineCount": 50
}

Results:
{
  "Contents": "4980:\tBTF_ID_FLAGS(func, bpf_strnstr);\n4981:\tBTF_ID_FLAGS(func, bpf_strncasestr);\n4982:\t#if defined(CONFIG_BPF_LSM) \u0026\u0026 defined(CONFIG_CGROUPS)\n4983:\tBTF_ID_FLAGS(func, bpf_cgroup_read_xattr, KF_RCU)\n4984:\t#endif\n4985:\tBTF_ID_FLAGS(func, bpf_stream_vprintk, KF_IMPLICIT_ARGS | KF_SPINLOCK_SAFE)\n4986:\tBTF_ID_FLAGS(func, bpf_stream_print_stack, KF_IMPLICIT_ARGS | KF_SPINLOCK_SAFE)\n4987:\tBTF_ID_FLAGS(func, bpf_task_work_schedule_signal, KF_IMPLICIT_ARGS)\n4988:\tBTF_ID_FLAGS(func, bpf_task_work_schedule_resume, KF_IMPLICIT_ARGS)\n4989:\tBTF_ID_FLAGS(func, bpf_dynptr_from_file)\n4990:\tBTF_ID_FLAGS(func, bpf_dynptr_file_discard, KF_RELEASE)\n4991:\tBTF_ID_FLAGS(func, bpf_timer_cancel_async)\n4992:\tBTF_KFUNCS_END(common_btf_ids)\n4993:\t\n4994:\tstatic const struct btf_kfunc_id_set common_kfunc_set = {\n4995:\t\t.owner = THIS_MODULE,\n4996:\t\t.set   = \u0026common_btf_ids,\n4997:\t};\n4998:\t\n4999:\tstatic int __init kfunc_init(void)\n5000:\t{\n5001:\t\tint ret;\n5002:\t\tconst struct btf_id_dtor_kfunc generic_dtors[] = {\n5003:\t\t\t{\n5004:\t\t\t\t.btf_id       = generic_dtor_ids[0],\n5005:\t\t\t\t.kfunc_btf_id = generic_dtor_ids[1]\n5006:\t\t\t},\n5007:\t#ifdef CONFIG_CGROUPS\n5008:\t\t\t{\n5009:\t\t\t\t.btf_id       = generic_dtor_ids[2],\n5010:\t\t\t\t.kfunc_btf_id = generic_dtor_ids[3]\n5011:\t\t\t},\n5012:\t#endif\n5013:\t\t};\n5014:\t\n5015:\t\tret = register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, \u0026generic_kfunc_set);\n5016:\t\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, \u0026generic_kfunc_set);\n5017:\t\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, \u0026generic_kfunc_set);\n5018:\t\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, \u0026generic_kfunc_set);\n5019:\t\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, \u0026generic_kfunc_set);\n5020:\t\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SKB, \u0026generic_kfunc_set);\n5021:\t\tret = ret ?: register_btf_id_dtor_kfuncs(generic_dtors,\n5022:\t\t\t\t\t\t\t  ARRAY_SIZE(generic_dtors),\n5023:\t\t\t\t\t\t\t  THIS_MODULE);\n5024:\t\treturn ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, \u0026common_kfunc_set);\n5025:\t}\n5026:\t\n5027:\tlate_initcall(kfunc_init);\n5028:\t\n5029:\t/* Get a pointer to dynptr data up to len bytes for read only access. If\n"
}

25/2 2026/09/24 13:37 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 23919
							output: 33
							thoughts: 112
						

Thoughts:
**My Assessment of Kernel Function Registration**

Okay, so I'm looking at this registration process. It appears we're using `register_btf_kfunc_id_set` to register `common_kfunc_set` for the unspecified BPF program type, which is interesting. Right, and `common_kfunc_set` is holding the kernel function IDs for `bpf_task_work_schedule_signal` and `bpf_task_work_schedule_resume`. That makes sense; these seem like foundational functions related to task management.

But wait, these BTF IDs need to be resolved, don't they? And that, of course, means we need BTF support enabled in the kernel. So, a quick reality check is needed. Let me just verify that `CONFIG_DEBUG_INFO_BTF` is actually set. I need to be absolutely sure this essential configuration option is enabled for everything to work as expected. Without it, all of this is pointless.



26/2 2026/09/24 13:37 tool grepper
0m Args:
{
  "Expression": "config DEBUG_INFO_BTF",
  "PathPrefix": "lib/Kconfig.debug"
}

Results:
{
  "Output": "lib/Kconfig.debug=378=config DEBUG_INFO_SPLIT\n--\nlib/Kconfig.debug-397-\nlib/Kconfig.debug:398:config DEBUG_INFO_BTF\nlib/Kconfig.debug-399-\tbool \"Generate BTF type information\"\n--\nlib/Kconfig.debug=419=config PAHOLE_HAS_LANG_EXCLUDE\n--\nlib/Kconfig.debug-427-\nlib/Kconfig.debug:428:config DEBUG_INFO_BTF_MODULES\nlib/Kconfig.debug-429-\tbool \"Generate BTF type information for kernel modules\"\n"
}

27/2 2026/09/24 13:37 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 24445
							output: 36
							thoughts: 367
						

Thoughts:
**Analyzing the BPF Task Work Patch**

Okay, let's dive into this patch. My immediate goal is to understand what `bpf_task_work_schedule_signal` needs. I'm seeing several related functions in `kernel/bpf/helpers.c`: `bpf_task_work_schedule_signal`, `bpf_task_work_schedule_resume`, `bpf_task_work_schedule`, `bpf_task_work_acquire_ctx`, `bpf_task_work_fetch_ctx`, `bpf_task_work_irq`, `bpf_task_work_cancel`, `bpf_task_work_cancel_scheduled`, and `bpf_task_work_cancel_and_free`.

Now, I want to zero in on what's changed and what's new. Looks like `bpf_task_work_cancel`, `bpf_task_work_irq`, `bpf_task_work_acquire_ctx`, `bpf_task_work_cancel_scheduled`, `bpf_task_work_fetch_ctx`, and `bpf_task_work_schedule` have been modified, meaning they're existing code that's been tweaked. `bpf_task_work_schedule_signal` and `bpf_task_work_schedule_resume` are marked as entrypoint kfuncs, which are interesting because they represent new functionality, presumably making the patch's main change. Lastly, `bpf_task_work_cancel_and_free` is another entrypoint, and it's called when a BPF map element or the map itself is deleted. That's a key detail to understand resource cleanup. Now, I need to know precisely where `bpf_task_work_cancel_and_free` is called!



28/2 2026/09/24 13:37 tool grepper
0m Args:
{
  "Expression": "bpf_task_work_cancel_and_free",
  "PathPrefix": "kernel/bpf"
}

Results:
{
  "Output": "kernel/bpf/helpers.c=4559=static struct bpf_task_work_ctx *bpf_task_work_acquire_ctx(struct bpf_task_work *tw,\n--\nkernel/bpf/helpers.c-4608-\t\t/* transfer map's ref into cancel_and_free() */\nkernel/bpf/helpers.c:4609:\t\tbpf_task_work_cancel_and_free(tw);\nkernel/bpf/helpers.c-4610-\t\treturn ERR_PTR(-EBUSY);\n--\nkernel/bpf/helpers.c=4792=static void bpf_task_work_cancel_scheduled(struct irq_work *irq_work)\n--\nkernel/bpf/helpers.c-4800-\nkernel/bpf/helpers.c:4801:void bpf_task_work_cancel_and_free(void *val)\nkernel/bpf/helpers.c-4802-{\n--\nkernel/bpf/syscall.c=802=void bpf_obj_free_task_work(const struct btf_record *rec, void *obj)\n--\nkernel/bpf/syscall.c-805-\t\treturn;\nkernel/bpf/syscall.c:806:\tbpf_task_work_cancel_and_free(obj + rec-\u003etask_work_off);\nkernel/bpf/syscall.c-807-}\n--\nkernel/bpf/syscall.c=814=void bpf_obj_free_fields(const struct btf_record *rec, void *obj)\n--\nkernel/bpf/syscall.c-838-\t\tcase BPF_TASK_WORK:\nkernel/bpf/syscall.c:839:\t\t\tbpf_task_work_cancel_and_free(field_ptr);\nkernel/bpf/syscall.c-840-\t\t\tbreak;\n"
}

29/2 2026/09/24 13:37 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 25723
							output: 231
							thoughts: 1750
						

Thoughts:
**Analysis of BPF Task Work for Fuzzing**

Okay, let's break this down for fuzzing purposes. My initial focus is on the `bpf_task_work` subsystem within `kernel/bpf/helpers.c`. I need to identify the key functions that have been modified or are critical entrypoints for this subsystem. `bpf_task_work_cancel_and_free` is definitely important because it's non-static and exported. Both `bpf_task_work_schedule_signal` and `bpf_task_work_schedule_resume` are non-static kfuncs, which are also good targets.

I also see `bpf_task_work_irq` and `bpf_task_work_cancel_scheduled`, which are static but are irq_work callbacks. Can't be inlined and distinct in the symbol table, so they're relevant, too. I verified those are static.

Looking at Section 4 regarding symbol extraction, I need to pinpoint the *specific* feature logic and entrypoints. I'm avoiding generic functions, focusing on the ones directly impacted by this patch.

Specifically, these functions are modified in `kernel/bpf/helpers.c`: `bpf_task_work_irq`, `bpf_task_work_cancel`, `bpf_task_work_cancel_scheduled`, `bpf_task_work_acquire_ctx`, `bpf_task_work_fetch_ctx`, `bpf_task_work_schedule`, `bpf_task_work_schedule_signal`, `bpf_task_work_schedule_resume`, and `bpf_task_work_cancel_and_free`.

`bpf_task_work_schedule_signal` and `bpf_task_work_schedule_resume` are clear kfunc entrypoints. `bpf_task_work_cancel_and_free` handles teardown during map element freeing.  `bpf_task_work_irq` is where the key race condition fixes were introduced, including the `get_task_struct` and cancellation logic. `bpf_task_work_acquire_ctx` now has an `irq_work_is_busy` check, and `bpf_task_work_cancel_scheduled` was updated.

Therefore, the focus symbols should be: `bpf_task_work_irq`, `bpf_task_work_schedule_signal`, `bpf_task_work_schedule_resume`, `bpf_task_work_acquire_ctx`, `bpf_task_work_cancel_and_free`, and `bpf_task_work_cancel_scheduled`.

Even if `bpf_task_work_cancel` is static or potentially inlined, including it or the callers is acceptable, but listing the key entrypoints and modified functions provides sufficient coverage.

Now for configs. I need to make sure the fuzzing environment is properly set up. `CONFIG_BPF_SYSCALL` is essential, and yes, it's what compiles `helpers.c`. Also, kfuncs require `CONFIG_DEBUG_INFO_BTF`. I checked to see if any other configs are needed but those are the primary.

I verified that `bpf_task_work_schedule_signal` and `bpf_task_work_schedule_resume` are not behind any `#ifdef` directives in the source. Everything is straightforward and confirmed against the source code.

I can now confidently conclude that this BPF task work subsystem patch should be targeted for fuzzing, which is valuable.



30/2 2026/09/24 13:38 tool set-results
0m Args:
{
  "EnableConfigs": [
    "BPF_SYSCALL",
    "DEBUG_INFO_BTF"
  ],
  "FocusSymbols": [
    "bpf_task_work_schedule_signal",
    "bpf_task_work_schedule_resume",
    "bpf_task_work_irq",
    "bpf_task_work_acquire_ctx",
    "bpf_task_work_cancel_and_free",
    "bpf_task_work_cancel_scheduled"
  ],
  "Reasoning": "The patch modifies reachable executable code in the BPF core subsystem (kernel/bpf/helpers.c). Specifically, it addresses races in BPF task_work handling by managing task_struct references across irq_work and cancellation, preventing task_work context reuse while irq_work is still busy, and initializing irq_work earlier upon context allocation. These kfuncs and callbacks are accessible to BPF programs loaded via the bpf syscall and executed concurrently across CPUs, making this change well worth fuzzing.",
  "WorthFuzzing": true
}

Results:
{
  "EnableConfigs": [
    "BPF_SYSCALL",
    "DEBUG_INFO_BTF"
  ],
  "FocusSymbols": [
    "bpf_task_work_schedule_signal",
    "bpf_task_work_schedule_resume",
    "bpf_task_work_irq",
    "bpf_task_work_acquire_ctx",
    "bpf_task_work_cancel_and_free",
    "bpf_task_work_cancel_scheduled"
  ],
  "Reasoning": "The patch modifies reachable executable code in the BPF core subsystem (kernel/bpf/helpers.c). Specifically, it addresses races in BPF task_work handling by managing task_struct references across irq_work and cancellation, preventing task_work context reuse while irq_work is still busy, and initializing irq_work earlier upon context allocation. These kfuncs and callbacks are accessible to BPF programs loaded via the bpf syscall and executed concurrently across CPUs, making this change well worth fuzzing.",
  "WorthFuzzing": true
}

31/1 2026/09/24 13:38 action If
0m Args:
{
  "WorthFuzzing": true
}

Results:
null

32/2 2026/09/24 13:38 agent kmsan-evaluator
0m Model:
core

Args:
null

Results:
{
  "KMSANReasoning": "The patch addresses concurrency and object lifetime race conditions in the BPF task_work subsystem (kernel/bpf/helpers.c). Specifically, it ensures a reference to `task_struct` is held across `task_work_add()` and potential cancellation to prevent use-after-free/NULL-dereference if the callback executes concurrently, moves `init_irq_work()` to context allocation time to avoid reinitializing an active work item, and checks `irq_work_is_busy()` before reusing contexts.\n\nNo new uninitialized stack or heap memory is introduced or read: local variables like `task` are assigned before use, and `ctx` continues to be zeroed with `memset` on allocation. No data is copied to user space, and no conditional branches depend on uninitialized values. Potential issues from this patch (such as lifetime issues, refcounting bugs, and race conditions) are within the domain of KASAN and standard kernel debugging facilities, not KMSAN. Therefore, 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 dce7e4bef520dc115e8e72f93ddf7999094bfb16
Author: syz-cluster <triage@syzkaller.com>
Date:   Thu Sep 24 13:35:37 2026 +0000

    syz-cluster: applied patch under review

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index b3cc5c8fc8756..d8bea7538de54 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -4437,7 +4437,8 @@ static void bpf_task_work_ctx_put(struct bpf_task_work_ctx *ctx)
 	}
 }
 
-static void bpf_task_work_cancel(struct bpf_task_work_ctx *ctx)
+static void bpf_task_work_cancel(struct bpf_task_work_ctx *ctx,
+				 struct task_struct *task)
 {
 	/*
 	 * Scheduled task_work callback holds ctx ref, so if we successfully
@@ -4445,7 +4446,7 @@ static void bpf_task_work_cancel(struct bpf_task_work_ctx *ctx)
 	 * cancel, callback will inevitably run or has already completed
 	 * running, and it would have taken care of its ctx ref itself.
 	 */
-	if (task_work_cancel(ctx->task, &ctx->work))
+	if (task_work_cancel(task, &ctx->work))
 		bpf_task_work_ctx_put(ctx);
 }
 
@@ -4486,6 +4487,7 @@ static void bpf_task_work_callback(struct callback_head *cb)
 static void bpf_task_work_irq(struct irq_work *irq_work)
 {
 	struct bpf_task_work_ctx *ctx = container_of(irq_work, struct bpf_task_work_ctx, irq_work);
+	struct task_struct *task;
 	enum bpf_task_work_state state;
 	int err;
 
@@ -4496,7 +4498,8 @@ static void bpf_task_work_irq(struct irq_work *irq_work)
 		return;
 	}
 
-	err = task_work_add(ctx->task, &ctx->work, ctx->mode);
+	task = get_task_struct(ctx->task);
+	err = task_work_add(task, &ctx->work, ctx->mode);
 	if (err) {
 		bpf_task_work_ctx_reset(ctx);
 		/*
@@ -4505,19 +4508,20 @@ static void bpf_task_work_irq(struct irq_work *irq_work)
 		 */
 		(void)cmpxchg(&ctx->state, BPF_TW_SCHEDULING, BPF_TW_STANDBY);
 		bpf_task_work_ctx_put(ctx);
+		put_task_struct(task);
 		return;
 	}
 
 	/*
-	 * It's technically possible for just scheduled task_work callback to
-	 * complete running by now, going SCHEDULING -> RUNNING and then
-	 * dropping its ctx refcount. Instead of capturing an extra ref just
-	 * to protect below ctx->state access, we rely on rcu_read_lock
-	 * above to prevent kfree_rcu from freeing ctx before we return.
+	 * The callback may have completed and reset ctx->task already. Keep the
+	 * captured task alive until this invocation is done with cancellation.
+	 * The RCU read-side section keeps ctx memory alive until then.
 	 */
 	state = cmpxchg(&ctx->state, BPF_TW_SCHEDULING, BPF_TW_SCHEDULED);
 	if (state == BPF_TW_FREED)
-		bpf_task_work_cancel(ctx); /* clean up if we switched into FREED state */
+		bpf_task_work_cancel(ctx, task); /* clean up if we switched into FREED state */
+
+	put_task_struct(task);
 }
 
 static struct bpf_task_work_ctx *bpf_task_work_fetch_ctx(struct bpf_task_work *tw,
@@ -4537,6 +4541,7 @@ static struct bpf_task_work_ctx *bpf_task_work_fetch_ctx(struct bpf_task_work *t
 	memset(ctx, 0, sizeof(*ctx));
 	refcount_set(&ctx->refcnt, 1); /* map's own ref */
 	ctx->state = BPF_TW_STANDBY;
+	init_irq_work(&ctx->irq_work, bpf_task_work_irq);
 
 	old_ctx = cmpxchg(&twk->ctx, NULL, ctx);
 	if (old_ctx) {
@@ -4579,6 +4584,18 @@ static struct bpf_task_work_ctx *bpf_task_work_acquire_ctx(struct bpf_task_work
 		bpf_task_work_ctx_put(ctx);
 		return ERR_PTR(-EBUSY);
 	}
+	/*
+	 * The callback or add-failure path can publish STANDBY before the
+	 * scheduling irq_work returns. Do not let a new round reuse the context
+	 * until BUSY clears. Otherwise the old handler can consume the new
+	 * round's SCHEDULING state and publish a false SCHEDULED state, and two
+	 * invocations can overlap on different CPUs.
+	 */
+	if (unlikely(irq_work_is_busy(&ctx->irq_work))) {
+		(void)cmpxchg(&ctx->state, BPF_TW_PENDING, BPF_TW_STANDBY);
+		bpf_task_work_ctx_put(ctx);
+		return ERR_PTR(-EBUSY);
+	}
 
 	/*
 	 * If no process or bpffs is holding a reference to the map, no new callbacks should be
@@ -4628,7 +4645,6 @@ static int bpf_task_work_schedule(struct task_struct *task, struct bpf_task_work
 	ctx->map = map;
 	ctx->map_val = (void *)tw - map->record->task_work_off;
 	init_task_work(&ctx->work, bpf_task_work_callback);
-	init_irq_work(&ctx->irq_work, bpf_task_work_irq);
 
 	irq_work_queue(&ctx->irq_work);
 	return 0;
@@ -4777,7 +4793,8 @@ static void bpf_task_work_cancel_scheduled(struct irq_work *irq_work)
 {
 	struct bpf_task_work_ctx *ctx = container_of(irq_work, struct bpf_task_work_ctx, irq_work);
 
-	bpf_task_work_cancel(ctx); /* this might put task_work callback's ref */
+	/* SCHEDULED -> FREED keeps ctx->task stable until this runs. */
+	bpf_task_work_cancel(ctx, ctx->task); /* this might put task_work callback's ref */
 	bpf_task_work_ctx_put(ctx); /* and here we put map's own ref that was transferred to us */
 }
 
diff --git a/tools/testing/selftests/bpf/prog_tests/test_task_work.c b/tools/testing/selftests/bpf/prog_tests/test_task_work.c
index 774b31a5f6ca1..ff56976f4226e 100644
--- a/tools/testing/selftests/bpf/prog_tests/test_task_work.c
+++ b/tools/testing/selftests/bpf/prog_tests/test_task_work.c
@@ -5,10 +5,14 @@
 #include <stdio.h>
 #include "task_work.skel.h"
 #include "task_work_fail.skel.h"
+#include "task_work_race.skel.h"
 #include <linux/bpf.h>
 #include <linux/perf_event.h>
 #include <sys/syscall.h>
 #include <time.h>
+#include <pthread.h>
+#include <sched.h>
+#include <unistd.h>
 
 static int perf_event_open(__u32 type, __u64 config, int pid)
 {
@@ -155,3 +159,282 @@ void test_task_work(void)
 
 	RUN_TESTS(task_work_fail);
 }
+
+#define TASK_WORK_RACE_ROUNDS	1500
+
+/* Must match progs/task_work_race.c. */
+enum task_work_race_status {
+	RACE_ARM_SEQ,
+	RACE_READY_SEQ,
+	/* High 32 bits hold the sequence, low 32 bits the signed error. */
+	RACE_RESULT,
+};
+
+struct task_work_race_value {
+	__u32 seq;
+	char data[60];
+	struct bpf_task_work tw;
+};
+
+struct task_work_race_ctx {
+	int stop;
+	int setup_err;
+	int trigger_tid;
+	int target_tid;
+	int trigger_cpu;
+	int target_cpu;
+};
+
+static int task_work_race_pin_cpu(int cpu)
+{
+	cpu_set_t set;
+
+	CPU_ZERO(&set);
+	CPU_SET(cpu, &set);
+	return pthread_setaffinity_np(pthread_self(), sizeof(set), &set);
+}
+
+static void *task_work_race_trigger(void *arg)
+{
+	struct task_work_race_ctx *ctx = arg;
+	int err;
+
+	err = task_work_race_pin_cpu(ctx->trigger_cpu);
+	if (err)
+		__atomic_store_n(&ctx->setup_err, err, __ATOMIC_RELEASE);
+	__atomic_store_n(&ctx->trigger_tid, syscall(__NR_gettid),
+			 __ATOMIC_RELEASE);
+	while (!__atomic_load_n(&ctx->stop, __ATOMIC_ACQUIRE))
+		getppid();
+	return NULL;
+}
+
+static void *task_work_race_target(void *arg)
+{
+	struct task_work_race_ctx *ctx = arg;
+	int err;
+
+	err = task_work_race_pin_cpu(ctx->target_cpu);
+	if (err)
+		__atomic_store_n(&ctx->setup_err, err, __ATOMIC_RELEASE);
+	__atomic_store_n(&ctx->target_tid, syscall(__NR_gettid),
+			 __ATOMIC_RELEASE);
+	while (!__atomic_load_n(&ctx->stop, __ATOMIC_ACQUIRE))
+		getppid();
+	return NULL;
+}
+
+static int task_work_race_status(struct task_work_race *skel, int idx,
+				 __s64 *value)
+{
+	return bpf_map_lookup_elem(bpf_map__fd(skel->maps.status), &idx,
+				   value);
+}
+
+static int task_work_race_set_status(struct task_work_race *skel, int idx,
+				     __s64 value)
+{
+	return bpf_map_update_elem(bpf_map__fd(skel->maps.status), &idx,
+				   &value, BPF_ANY);
+}
+
+static int task_work_race_wait_ready(struct task_work_race *skel, __u32 seq)
+{
+	int i;
+
+	for (i = 0; i < 2000000 / 100; i++) {
+		__s64 value = 0;
+
+		if (!task_work_race_status(skel, RACE_READY_SEQ, &value) &&
+		    value == seq)
+			return 0;
+		if (!task_work_race_status(skel, RACE_RESULT, &value) &&
+		    (__u32)((__u64)value >> 32) == seq)
+			/*
+			 * The BPF program finished between the two reads:
+			 * RESULT makes waiting for READY unnecessary. Any error
+			 * that occurred before READY is checked by the caller.
+			 */
+			return 0;
+		usleep(100);
+	}
+	return -ETIMEDOUT;
+}
+
+static int task_work_race_prepare_elem(struct task_work_race *skel)
+{
+	struct task_work_race_value value = {};
+	int key = 0;
+
+	if (!bpf_map_lookup_elem(bpf_map__fd(skel->maps.hmap), &key, &value))
+		return 0;
+	if (errno != ENOENT)
+		return -errno;
+	if (bpf_map_update_elem(bpf_map__fd(skel->maps.hmap), &key, &value,
+				BPF_NOEXIST))
+		return -errno;
+	return 0;
+}
+
+static int task_work_race_arm(struct task_work_race *skel, __u32 seq)
+{
+	if (task_work_race_set_status(skel, RACE_ARM_SEQ, seq))
+		return -errno;
+	return task_work_race_wait_ready(skel, seq);
+}
+
+static int task_work_race_check_done(struct task_work_race *skel, __u32 seq)
+{
+	int i;
+
+	for (i = 0; i < 2000000 / 100; i++) {
+		__s64 result = 0;
+
+		if (!task_work_race_status(skel, RACE_RESULT, &result) &&
+		    (__u32)((__u64)result >> 32) == seq)
+			return (__s32)result;
+		usleep(100);
+	}
+	return -ETIMEDOUT;
+}
+
+static int task_work_race_wait_callback(struct task_work_race *skel,
+					__u32 seq)
+{
+	int i, key = seq;
+
+	for (i = 0; i < 2000000 / 100; i++) {
+		__u64 completed = 0;
+
+		if (!bpf_map_lookup_elem(bpf_map__fd(skel->maps.completed),
+					 &key, &completed) && completed)
+			return 0;
+		usleep(100);
+	}
+	return -ETIMEDOUT;
+}
+
+void serial_test_task_work_race(void)
+{
+	struct task_work_race_ctx ctx = {};
+	struct task_work_race *skel;
+	pthread_t trigger, target;
+	cpu_set_t allowed;
+	int cpu, cpu_count = 0;
+	bool target_started = false, trigger_started = false;
+	int err, i, key = 0;
+
+	if (sched_getaffinity(0, sizeof(allowed), &allowed)) {
+		ASSERT_OK(-errno, "sched_getaffinity");
+		return;
+	}
+	for (cpu = 0; cpu < CPU_SETSIZE && cpu_count < 2; cpu++) {
+		if (!CPU_ISSET(cpu, &allowed))
+			continue;
+		if (!cpu_count)
+			ctx.trigger_cpu = cpu;
+		else
+			ctx.target_cpu = cpu;
+		cpu_count++;
+	}
+	if (cpu_count < 2) {
+		printf("%s:SKIP:need two CPUs in the process affinity mask\n",
+		       __func__);
+		test__skip();
+		return;
+	}
+
+	skel = task_work_race__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (!ASSERT_OK(task_work_race__attach(skel), "attach"))
+		goto cleanup;
+
+	err = pthread_create(&target, NULL, task_work_race_target, &ctx);
+	if (!ASSERT_OK(err, "pthread_create target"))
+		goto cleanup;
+	target_started = true;
+	err = pthread_create(&trigger, NULL, task_work_race_trigger, &ctx);
+	if (!ASSERT_OK(err, "pthread_create trigger"))
+		goto stop;
+	trigger_started = true;
+
+	for (i = 0; i < 5000000 / 100; i++) {
+		if (__atomic_load_n(&ctx.trigger_tid, __ATOMIC_ACQUIRE) &&
+		    __atomic_load_n(&ctx.target_tid, __ATOMIC_ACQUIRE))
+			break;
+		usleep(100);
+	}
+	if (!ASSERT_TRUE(__atomic_load_n(&ctx.trigger_tid, __ATOMIC_ACQUIRE) &&
+			 __atomic_load_n(&ctx.target_tid, __ATOMIC_ACQUIRE),
+			 "thread start"))
+		goto stop;
+	if (!ASSERT_OK(__atomic_load_n(&ctx.setup_err, __ATOMIC_ACQUIRE),
+		       "thread affinity"))
+		goto stop;
+
+	skel->bss->trigger_tid = __atomic_load_n(&ctx.trigger_tid, __ATOMIC_ACQUIRE);
+	skel->bss->target_tid = __atomic_load_n(&ctx.target_tid, __ATOMIC_ACQUIRE);
+
+	for (i = 0; i < TASK_WORK_RACE_ROUNDS; i++) {
+		__u32 seq = i + 1;
+		int variant = i % 3;
+
+		if (!ASSERT_OK(task_work_race_prepare_elem(skel), "prepare elem") ||
+		    !ASSERT_OK(task_work_race_arm(skel, seq), "round ready"))
+			goto stop;
+
+		if (variant == 2 &&
+		    !ASSERT_OK(bpf_map_delete_elem(bpf_map__fd(skel->maps.hmap),
+						   &key), "early delete"))
+			goto stop;
+
+		err = task_work_race_check_done(skel, seq);
+		/*
+		 * Variant 2 deletes the element right after READY, so losing
+		 * the race against the scheduling kfunc is a valid outcome:
+		 * the schedule itself fails with -EBUSY (ctx already FREED),
+		 * and the retry on the next tracepoint invocation fails with
+		 * -ENOENT because the element is gone.  Either way the round
+		 * exercised the deletion paths; a later variant 0 round
+		 * recreates the element.
+		 */
+		if (variant == 2 && (err == -EBUSY || err == -ENOENT))
+			err = 0;
+		if (!ASSERT_OK(err, "schedule")) {
+			fprintf(stderr, "round %d schedule failed: %d\n", i, err);
+			goto stop;
+		}
+
+		if (variant != 2 &&
+		    !ASSERT_OK(task_work_race_wait_callback(skel, seq),
+			       "callback"))
+			goto stop;
+
+		if (variant == 1 &&
+		    !ASSERT_OK(bpf_map_delete_elem(bpf_map__fd(skel->maps.hmap),
+						   &key), "late delete"))
+			goto stop;
+	}
+
+	/* A distinct completion generation prevents an old callback satisfying this. */
+	if (!ASSERT_OK(task_work_race_prepare_elem(skel), "final prepare") ||
+	    !ASSERT_OK(task_work_race_arm(skel, TASK_WORK_RACE_ROUNDS + 1),
+		       "final ready") ||
+	    !ASSERT_OK(task_work_race_check_done(skel,
+					TASK_WORK_RACE_ROUNDS + 1),
+		       "final schedule") ||
+	    !ASSERT_OK(task_work_race_wait_callback(skel,
+					     TASK_WORK_RACE_ROUNDS + 1),
+		       "final callback"))
+		goto stop;
+
+stop:
+	__atomic_store_n(&ctx.stop, 1, __ATOMIC_RELEASE);
+	if (trigger_started)
+		pthread_join(trigger, NULL);
+	if (target_started)
+		pthread_join(target, NULL);
+cleanup:
+	task_work_race__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/task_work_race.c b/tools/testing/selftests/bpf/progs/task_work_race.c
new file mode 100644
index 0000000000000..c94eca510cb37
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/task_work_race.c
@@ -0,0 +1,126 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 KylinSoft Corporation. */
+#include <vmlinux.h>
+#include <stdbool.h>
+#include <errno.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+#define TASK_WORK_RACE_MAX_SEQ	2048
+
+enum task_work_race_status {
+	RACE_ARM_SEQ,
+	RACE_READY_SEQ,
+	/* High 32 bits hold the sequence, low 32 bits the signed error. */
+	RACE_RESULT,
+	RACE_STATUS_MAX,
+};
+
+int trigger_tid;
+int target_tid;
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, RACE_STATUS_MAX);
+	__type(key, int);
+	__type(value, __s64);
+} status SEC(".maps");
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, TASK_WORK_RACE_MAX_SEQ);
+	__type(key, int);
+	__type(value, __u64);
+} completed SEC(".maps");
+
+struct task_work_race_value {
+	__u32 seq;
+	char data[60];
+	struct bpf_task_work tw;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__uint(map_flags, BPF_F_NO_PREALLOC);
+	__uint(max_entries, 4);
+	__type(key, int);
+	__type(value, struct task_work_race_value);
+} hmap SEC(".maps");
+
+static __always_inline void set_status(int key, __s64 value)
+{
+	__s64 *slot;
+
+	slot = bpf_map_lookup_elem(&status, &key);
+	if (slot)
+		*slot = value;
+}
+
+static int process_work(struct bpf_map *map, void *key, void *value)
+{
+	struct task_work_race_value *work = value;
+	__u64 *done;
+	int seq = work->seq;
+
+	if (seq <= 0 || seq >= TASK_WORK_RACE_MAX_SEQ)
+		return 0;
+	done = bpf_map_lookup_elem(&completed, &seq);
+	if (done)
+		*done = 1;
+	return 0;
+}
+
+SEC("tracepoint/syscalls/sys_enter_getppid")
+int race_sched_work(void *ctx)
+{
+	struct task_work_race_value *work;
+	struct task_struct *task;
+	__s64 *arm, *result;
+	__u32 tid = (__u32)bpf_get_current_pid_tgid();
+	int key = 0, err;
+	__u32 seq;
+
+	if (tid != trigger_tid)
+		return 0;
+
+	key = RACE_ARM_SEQ;
+	arm = bpf_map_lookup_elem(&status, &key);
+	key = RACE_RESULT;
+	result = bpf_map_lookup_elem(&status, &key);
+	if (!arm || !result || *arm <= 0 ||
+	    (__u32)((__u64)*result >> 32) == *arm)
+		return 0;
+	seq = *arm;
+
+	task = bpf_task_from_pid(target_tid);
+	if (!task) {
+		err = -ESRCH;
+		goto out_done;
+	}
+
+	key = 0;
+	work = bpf_map_lookup_elem(&hmap, &key);
+	if (!work) {
+		err = -ENOENT;
+		goto out_task;
+	}
+
+	work->seq = seq;
+	set_status(RACE_READY_SEQ, seq);
+	err = bpf_task_work_schedule_signal(task, &work->tw, &hmap,
+					    process_work);
+	if (err == -EBUSY) {
+		bpf_task_release(task);
+		return 0;
+	}
+
+out_task:
+	bpf_task_release(task);
+out_done:
+	/* Publish the sequence and its error in one store. */
+	*result = ((__u64)seq << 32) | (__u32)err;
+	return 0;
+}

33/3 2026/09/24 13:38 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 8622
							output: 37
							thoughts: 1535
						

Thoughts:
**KMSAN Fuzzing Assessment for this BPF Patch Series**

Okay, let's break this down. My goal here is to determine if these changes necessitate a dedicated KMSAN fuzzing session. I need to be diligent about this, so I will take my time.

First, I need to understand what's been changed within the kernel itself. This looks like a series of modifications primarily focused on `bpf_task_work` within `kernel/bpf/helpers.c`. Specifically, I see changes to `bpf_task_work_cancel`, `bpf_task_work_irq`, `bpf_task_work_fetch_ctx`, `bpf_task_work_acquire_ctx`, and `bpf_task_work_schedule`. The code snippets are right here.

I note that the `bpf_task_work_cancel` function now takes an additional `task` parameter, and the call sites in `bpf_task_work_irq` and `bpf_task_work_cancel_scheduled` are updated to reflect the change, with task references acquired and released using `get_task_struct` and `put_task_struct` where appropriate. I see this is for proper tracking of the `task` pointer. In addition, the changes include the addition of irq work initialization in `bpf_task_work_fetch_ctx`. The update to `bpf_task_work_acquire_ctx` adds a check using `irq_work_is_busy` to prevent race conditions. The `init_irq_work` call has been removed from `bpf_task_work_schedule`. Finally, I see that this patch series includes updates to the selftests which gives me more confidence.

Given these changes, especially the added checks for race conditions with `irq_work_is_busy` and the refined `task` pointer handling, my primary concern is whether these changes could introduce subtle data races or use-after-free conditions related to the task work context and the `task_struct`. The changes related to `init_irq_work` also stand out to me as they could affect synchronization.

Therefore, since these updates touch the core mechanisms of how BPF tasks are scheduled and managed, especially around locking and context management, I would lean toward considering a dedicated KMSAN fuzzing session as a prudent measure. It's a low-risk, high-reward approach for catching these types of subtle concurrency bugs that might be missed by less specialized testing. I have to be conservative here, so I will propose this session to the team.



34/3 2026/09/24 13:38 tool read-file
0m Args:
{
  "File": "kernel/bpf/helpers.c",
  "FirstLine": 4430,
  "LineCount": 80
}

Results:
{
  "Contents": "4430:\t\t\treturn;\n4431:\t\n4432:\t\tif (irqs_disabled()) {\n4433:\t\t\tctx-\u003eirq_work = IRQ_WORK_INIT(bpf_task_work_destroy);\n4434:\t\t\tirq_work_queue(\u0026ctx-\u003eirq_work);\n4435:\t\t} else {\n4436:\t\t\tbpf_task_work_destroy(\u0026ctx-\u003eirq_work);\n4437:\t\t}\n4438:\t}\n4439:\t\n4440:\tstatic void bpf_task_work_cancel(struct bpf_task_work_ctx *ctx,\n4441:\t\t\t\t\t struct task_struct *task)\n4442:\t{\n4443:\t\t/*\n4444:\t\t * Scheduled task_work callback holds ctx ref, so if we successfully\n4445:\t\t * cancelled, we put that ref on callback's behalf. If we couldn't\n4446:\t\t * cancel, callback will inevitably run or has already completed\n4447:\t\t * running, and it would have taken care of its ctx ref itself.\n4448:\t\t */\n4449:\t\tif (task_work_cancel(task, \u0026ctx-\u003ework))\n4450:\t\t\tbpf_task_work_ctx_put(ctx);\n4451:\t}\n4452:\t\n4453:\tstatic void bpf_task_work_callback(struct callback_head *cb)\n4454:\t{\n4455:\t\tstruct bpf_task_work_ctx *ctx = container_of(cb, struct bpf_task_work_ctx, work);\n4456:\t\tenum bpf_task_work_state state;\n4457:\t\tu32 idx;\n4458:\t\tvoid *key;\n4459:\t\n4460:\t\t/* Read lock is needed to protect ctx and map key/value access */\n4461:\t\tguard(rcu_tasks_trace)();\n4462:\t\t/*\n4463:\t\t * This callback may start running before bpf_task_work_irq() switched to\n4464:\t\t * SCHEDULED state, so handle both transition variants SCHEDULING|SCHEDULED -\u003e RUNNING.\n4465:\t\t */\n4466:\t\tstate = cmpxchg(\u0026ctx-\u003estate, BPF_TW_SCHEDULING, BPF_TW_RUNNING);\n4467:\t\tif (state == BPF_TW_SCHEDULED)\n4468:\t\t\tstate = cmpxchg(\u0026ctx-\u003estate, BPF_TW_SCHEDULED, BPF_TW_RUNNING);\n4469:\t\tif (state == BPF_TW_FREED) {\n4470:\t\t\tbpf_task_work_ctx_put(ctx);\n4471:\t\t\treturn;\n4472:\t\t}\n4473:\t\n4474:\t\tkey = (void *)map_key_from_value(ctx-\u003emap, ctx-\u003emap_val, \u0026idx);\n4475:\t\n4476:\t\tmigrate_disable();\n4477:\t\tctx-\u003ecallback_fn((u64)(long)ctx-\u003emap, (u64)(long)key,\n4478:\t\t\t\t (u64)(long)ctx-\u003emap_val, 0, 0);\n4479:\t\tmigrate_enable();\n4480:\t\n4481:\t\tbpf_task_work_ctx_reset(ctx);\n4482:\t\t(void)cmpxchg(\u0026ctx-\u003estate, BPF_TW_RUNNING, BPF_TW_STANDBY);\n4483:\t\n4484:\t\tbpf_task_work_ctx_put(ctx);\n4485:\t}\n4486:\t\n4487:\tstatic void bpf_task_work_irq(struct irq_work *irq_work)\n4488:\t{\n4489:\t\tstruct bpf_task_work_ctx *ctx = container_of(irq_work, struct bpf_task_work_ctx, irq_work);\n4490:\t\tstruct task_struct *task;\n4491:\t\tenum bpf_task_work_state state;\n4492:\t\tint err;\n4493:\t\n4494:\t\tguard(rcu)();\n4495:\t\n4496:\t\tif (cmpxchg(\u0026ctx-\u003estate, BPF_TW_PENDING, BPF_TW_SCHEDULING) != BPF_TW_PENDING) {\n4497:\t\t\tbpf_task_work_ctx_put(ctx);\n4498:\t\t\treturn;\n4499:\t\t}\n4500:\t\n4501:\t\ttask = get_task_struct(ctx-\u003etask);\n4502:\t\terr = task_work_add(task, \u0026ctx-\u003ework, ctx-\u003emode);\n4503:\t\tif (err) {\n4504:\t\t\tbpf_task_work_ctx_reset(ctx);\n4505:\t\t\t/*\n4506:\t\t\t * try to switch back to STANDBY for another task_work reuse, but we might have\n4507:\t\t\t * gone to FREED already, which is fine as we already cleaned up after ourselves\n4508:\t\t\t */\n4509:\t\t\t(void)cmpxchg(\u0026ctx-\u003estate, BPF_TW_SCHEDULING, BPF_TW_STANDBY);\n"
}

35/3 2026/09/24 13:38 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 12136
							output: 37
							thoughts: 57
						

Thoughts:
Okay, let's dive into this kernel space exploration.

**Delving into Task Work Context Management**

My intuition tells me that the 4530-4660 range in the code should be highly informative. I need to understand how the BPF subsystem interacts with the task work mechanism. Specifically, I'm keen to dissect the roles and interactions of `bpf_task_work_fetch_ctx`, `bpf_task_work_acquire_ctx`, and `bpf_task_work_schedule`. These function names strongly suggest they're responsible for fetching, acquiring, and scheduling context information related to BPF programs when they're enqueued on a task work queue. I need to determine precisely what context is being managed and how the lifecycle of that context ties into the broader task scheduling and execution model. This will give me a better understanding of how these BPF features work together.



36/3 2026/09/24 13:38 tool read-file
0m Args:
{
  "File": "kernel/bpf/helpers.c",
  "FirstLine": 4530,
  "LineCount": 80
}

Results:
{
  "Contents": "4530:\t\tstruct bpf_task_work_kern *twk = (void *)tw;\n4531:\t\tstruct bpf_task_work_ctx *ctx, *old_ctx;\n4532:\t\n4533:\t\tctx = READ_ONCE(twk-\u003ectx);\n4534:\t\tif (ctx)\n4535:\t\t\treturn ctx;\n4536:\t\n4537:\t\tctx = bpf_map_kmalloc_nolock(map, sizeof(*ctx), 0, NUMA_NO_NODE);\n4538:\t\tif (!ctx)\n4539:\t\t\treturn ERR_PTR(-ENOMEM);\n4540:\t\n4541:\t\tmemset(ctx, 0, sizeof(*ctx));\n4542:\t\trefcount_set(\u0026ctx-\u003erefcnt, 1); /* map's own ref */\n4543:\t\tctx-\u003estate = BPF_TW_STANDBY;\n4544:\t\tinit_irq_work(\u0026ctx-\u003eirq_work, bpf_task_work_irq);\n4545:\t\n4546:\t\told_ctx = cmpxchg(\u0026twk-\u003ectx, NULL, ctx);\n4547:\t\tif (old_ctx) {\n4548:\t\t\t/*\n4549:\t\t\t * tw-\u003ectx is set by concurrent BPF program, release allocated\n4550:\t\t\t * memory and try to reuse already set context.\n4551:\t\t\t */\n4552:\t\t\tkfree_nolock(ctx);\n4553:\t\t\treturn old_ctx;\n4554:\t\t}\n4555:\t\n4556:\t\treturn ctx; /* Success */\n4557:\t}\n4558:\t\n4559:\tstatic struct bpf_task_work_ctx *bpf_task_work_acquire_ctx(struct bpf_task_work *tw,\n4560:\t\t\t\t\t\t\t\t   struct bpf_map *map)\n4561:\t{\n4562:\t\tstruct bpf_task_work_ctx *ctx;\n4563:\t\n4564:\t\t/*\n4565:\t\t * Sleepable BPF programs hold rcu_read_lock_trace but not\n4566:\t\t * regular rcu_read_lock. Since kfree_rcu waits for regular\n4567:\t\t * RCU GP, the ctx can be freed while we're between reading\n4568:\t\t * the pointer and incrementing the refcount. Take regular\n4569:\t\t * rcu_read_lock to prevent kfree_rcu from freeing the ctx\n4570:\t\t * before we can tryget it.\n4571:\t\t */\n4572:\t\tscoped_guard(rcu) {\n4573:\t\t\tctx = bpf_task_work_fetch_ctx(tw, map);\n4574:\t\t\tif (IS_ERR(ctx))\n4575:\t\t\t\treturn ctx;\n4576:\t\n4577:\t\t\t/* try to get ref for task_work callback to hold */\n4578:\t\t\tif (!bpf_task_work_ctx_tryget(ctx))\n4579:\t\t\t\treturn ERR_PTR(-EBUSY);\n4580:\t\t}\n4581:\t\n4582:\t\tif (cmpxchg(\u0026ctx-\u003estate, BPF_TW_STANDBY, BPF_TW_PENDING) != BPF_TW_STANDBY) {\n4583:\t\t\t/* lost acquiring race or map_release_uref() stole it from us, put ref and bail */\n4584:\t\t\tbpf_task_work_ctx_put(ctx);\n4585:\t\t\treturn ERR_PTR(-EBUSY);\n4586:\t\t}\n4587:\t\t/*\n4588:\t\t * The callback or add-failure path can publish STANDBY before the\n4589:\t\t * scheduling irq_work returns. Do not let a new round reuse the context\n4590:\t\t * until BUSY clears. Otherwise the old handler can consume the new\n4591:\t\t * round's SCHEDULING state and publish a false SCHEDULED state, and two\n4592:\t\t * invocations can overlap on different CPUs.\n4593:\t\t */\n4594:\t\tif (unlikely(irq_work_is_busy(\u0026ctx-\u003eirq_work))) {\n4595:\t\t\t(void)cmpxchg(\u0026ctx-\u003estate, BPF_TW_PENDING, BPF_TW_STANDBY);\n4596:\t\t\tbpf_task_work_ctx_put(ctx);\n4597:\t\t\treturn ERR_PTR(-EBUSY);\n4598:\t\t}\n4599:\t\n4600:\t\t/*\n4601:\t\t * If no process or bpffs is holding a reference to the map, no new callbacks should be\n4602:\t\t * scheduled. This does not address any race or correctness issue, but rather is a policy\n4603:\t\t * choice: dropping user references should stop everything.\n4604:\t\t */\n4605:\t\tif (!atomic64_read(\u0026map-\u003eusercnt)) {\n4606:\t\t\t/* drop ref we just got for task_work callback itself */\n4607:\t\t\tbpf_task_work_ctx_put(ctx);\n4608:\t\t\t/* transfer map's ref into cancel_and_free() */\n4609:\t\t\tbpf_task_work_cancel_and_free(tw);\n"
}

37/3 2026/09/24 13:38 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 13799
							output: 243
							thoughts: 1712
						

Thoughts:
**KMSAN Evaluation for BPF Task Work Patch**

Okay, let's break this down. My primary concern here is determining whether this patch necessitates a KMSAN check, or if standard bug detectors are sufficient. The patch deals with race conditions and lifetime management in BPF task work. Specifically, it seems to be guarding against use-after-free and concurrent access issues related to the `bpf_task_work_ctx` structure and the associated `task_struct`.

Initially, I see the core of the fix revolves around proper reference counting (`get_task_struct`, `put_task_struct`) to protect the `task_struct`, along with checks for `irq_work` busy status. The introduction of `init_irq_work` earlier in the lifecycle of the context is also relevant. The code in the `bpf_task_work_irq` path makes use of a newly ref-counted `task` pointer. There is also a state transition check. These changes are crucial for resolving concurrency problems.

Critically, there are *no* copies to userspace, no potential information leaks from kernel to user, and no use of uninitialized variables or struct fields, as I understand it. I see no instance of uninitialized memory being *read* within the patch. I verified this by reviewing initialization routines and usages of the members in `bpf_task_work_ctx`. `memset` is called at allocation to guarantee initialization, `init_irq_work` is now called during context allocation.

The patch's intent is to resolve race conditions and use-after-free scenarios. KASAN and other standard bug detectors (such as LOCKDEP) are designed to identify precisely these types of issues. KMSAN's purpose, as defined in the rules, is to detect uninitialized memory reads. That's not what we have here. The patch deals with concurrent modification of state.

Therefore, because this patch addresses race conditions, lifetime management, and concurrency issues, and because it *does not* introduce any risk of uninitialized memory reads or information leaks, I am confident in declaring that KMSAN is *not* needed here. The standard bug detectors (KASAN, etc.) are fully sufficient. `NeedsKMSAN=false`. I will ensure that my commit message reflects this reasoning clearly.



38/3 2026/09/24 13:39 tool set-results
0m Args:
{
  "KMSANReasoning": "The patch addresses concurrency and object lifetime race conditions in the BPF task_work subsystem (kernel/bpf/helpers.c). Specifically, it ensures a reference to `task_struct` is held across `task_work_add()` and potential cancellation to prevent use-after-free/NULL-dereference if the callback executes concurrently, moves `init_irq_work()` to context allocation time to avoid reinitializing an active work item, and checks `irq_work_is_busy()` before reusing contexts.\n\nNo new uninitialized stack or heap memory is introduced or read: local variables like `task` are assigned before use, and `ctx` continues to be zeroed with `memset` on allocation. No data is copied to user space, and no conditional branches depend on uninitialized values. Potential issues from this patch (such as lifetime issues, refcounting bugs, and race conditions) are within the domain of KASAN and standard kernel debugging facilities, not KMSAN. Therefore, a dedicated KMSAN fuzzing session is not warranted.",
  "NeedsKMSAN": false
}

Results:
{
  "KMSANReasoning": "The patch addresses concurrency and object lifetime race conditions in the BPF task_work subsystem (kernel/bpf/helpers.c). Specifically, it ensures a reference to `task_struct` is held across `task_work_add()` and potential cancellation to prevent use-after-free/NULL-dereference if the callback executes concurrently, moves `init_irq_work()` to context allocation time to avoid reinitializing an active work item, and checks `irq_work_is_busy()` before reusing contexts.\n\nNo new uninitialized stack or heap memory is introduced or read: local variables like `task` are assigned before use, and `ctx` continues to be zeroed with `memset` on allocation. No data is copied to user space, and no conditional branches depend on uninitialized values. Potential issues from this patch (such as lifetime issues, refcounting bugs, and race conditions) are within the domain of KASAN and standard kernel debugging facilities, not KMSAN. Therefore, 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)