| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/10/12 06:35 | flow | patch-triage |
6mArgs: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_scheduled",
"bpf_task_work_cancel_and_free"
],
"KMSANReasoning": "The patch addresses race conditions in the BPF task_work scheduling infrastructure (kernel/bpf/helpers.c). Specifically, it ensures that:\n1. The `task_struct` pointer is captured prior to publishing the task work, preventing a race where the callback runs concurrently, resets `ctx-\u003etask`, and causes a NULL pointer dereference or use-after-free in `bpf_task_work_cancel()`.\n2. `init_irq_work()` is moved to initialization time in `bpf_task_work_fetch_ctx()` rather than per-schedule, and `irq_work_is_busy()` is checked before reusing the context to prevent re-enqueueing or reinitializing an active `irq_work`.\n\nAll relevant memory structures (`struct bpf_task_work_ctx`) are fully zeroed upon allocation via `memset(ctx, 0, sizeof(*ctx))` and properly initialized. No uninitialized memory is read, branched on, or copied to user space. The risks addressed by these changes involve lifetime, concurrency, and pointer validity (use-after-free and NULL pointer dereference), which are thoroughly monitored by standard KASAN and lock/debugging infrastructure. A dedicated KMSAN session is not warranted.",
"NeedsKMSAN": false,
"Reasoning": "The patch addresses concurrency race conditions in BPF task_work scheduling, cancellation, and context lifecycle in kernel/bpf/helpers.c. Specifically, it prevents use-after-free/NULL-dereference when ctx-\u003etask is cleared by a completed callback before bpf_task_work_irq() finishes reading it, and adds a check for irq_work_is_busy() to prevent reusing task_work contexts while the previous scheduling irq_work is still active. These paths are reachable from userspace via BPF programs scheduling task_work callbacks using kfuncs. Fuzzing this complex concurrency and state machine logic is valuable for detecting exposed latent race conditions and assertions.",
"WorthFuzzing": true
} |
| 1/1 | 2026/10/12 06:35 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit c0f793eb9550fd57d2c54c37baf7a7b04bbe5037\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Mon Oct 12 06:35:25 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..350269b8646e4 100644\n--- a/kernel/bpf/helpers.c\n+++ b/kernel/bpf/helpers.c\n@@ -4437,15 +4437,21 @@ 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 \t * cancelled, we put that ref on callback's behalf. If we couldn't\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+\t * The caller passes the task explicitly: the scheduling handler must\n+\t * not read ctx-\u003etask after the work is published (the callback may\n+\t * have reset it), while in the asynchronous cancellation path\n+\t * ctx-\u003etask is stable, see bpf_task_work_cancel_scheduled().\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 +4492,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 +4503,16 @@ 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+\t/*\n+\t * Capture the task pointer before the work is published: once the\n+\t * callback runs, it may reset ctx-\u003etask. The capture happens while\n+\t * this round's task reference is held, and every path that can\n+\t * release it runs after this handler entered its rcu read-side\n+\t * section. bpf_task_release() is put_task_struct_rcu_user(), so the\n+\t * task_struct itself cannot be freed before this section ends.\n+\t */\n+\ttask = 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@@ -4510,14 +4526,14 @@ static void bpf_task_work_irq(struct irq_work *irq_work)\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 * complete running by now, going SCHEDULING -\u003e RUNNING, resetting\n+\t * ctx-\u003etask and dropping its ctx refcount. The rcu read-side section\n+\t * above keeps both the ctx memory and the captured task pointer\n+\t * valid until this handler returns.\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 \n static struct bpf_task_work_ctx *bpf_task_work_fetch_ctx(struct bpf_task_work *tw,\n@@ -4537,6 +4553,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 +4596,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 * STANDBY can be published before the previous scheduling irq_work\n+\t * returns. Claim PENDING before checking BUSY to order the check after\n+\t * that publication. No irq_work has been queued for this attempt yet,\n+\t * so reject reuse and roll back to STANDBY while BUSY is set. Preserve\n+\t * FREED if deletion wins the rollback race.\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 +4657,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 +4805,13 @@ 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/*\n+\t * Deletion observed SCHEDULED and won against the callback's\n+\t * transition to RUNNING. The callback takes its FREED exit without\n+\t * resetting ctx-\u003etask, and the transferred map reference prevents\n+\t * destruction until cancellation completes.\n+\t */\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..deabc524dd497 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,15 @@\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 \"task_work_busy.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 +160,692 @@ void test_task_work(void)\n \n \tRUN_TESTS(task_work_fail);\n }\n+\n+#define TASK_WORK_RACE_ROUNDS\t\t1500\n+#define TASK_WORK_POLL_US\t\t100\n+#define TASK_WORK_POLL_ITERS\t20000\n+#define TASK_WORK_START_POLL_ITERS\t50000\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_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+};\n+\n+struct task_work_race_thread_arg {\n+\tstruct task_work_race_ctx *ctx;\n+\tint cpu;\n+\tint *tid;\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_worker(void *arg)\n+{\n+\tstruct task_work_race_thread_arg *thread = arg;\n+\tstruct task_work_race_ctx *ctx = thread-\u003ectx;\n+\tint err;\n+\n+\terr = task_work_race_pin_cpu(thread-\u003ecpu);\n+\tif (err)\n+\t\t__atomic_store_n(\u0026ctx-\u003esetup_err, err, __ATOMIC_RELEASE);\n+\t__atomic_store_n(thread-\u003etid, syscall(__NR_gettid), __ATOMIC_RELEASE);\n+\twhile (!__atomic_load_n(\u0026ctx-\u003estop, __ATOMIC_ACQUIRE))\n+\t\tgetppid();\n+\treturn NULL;\n+}\n+\n+static int task_work_status(struct bpf_map *map, int idx, __s64 *value)\n+{\n+\treturn bpf_map_lookup_elem(bpf_map__fd(map), \u0026idx, value);\n+}\n+\n+static int task_work_set_status(struct bpf_map *map, int idx, __s64 value)\n+{\n+\treturn bpf_map_update_elem(bpf_map__fd(map), \u0026idx, \u0026value, BPF_ANY);\n+}\n+\n+/* Wait for seq, keeping polling errors separate from the scheduling result. */\n+static int task_work_wait_seq(struct bpf_map *map, int idx, __u32 seq,\n+\t\t\t __s32 *result)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i \u003c TASK_WORK_POLL_ITERS; i++) {\n+\t\t__s64 value = 0;\n+\n+\t\tif (!task_work_status(map, idx, \u0026value) \u0026\u0026\n+\t\t (__u32)((__u64)value \u003e\u003e 32) == seq) {\n+\t\t\t*result = (__s32)value;\n+\t\t\treturn 0;\n+\t\t}\n+\t\tusleep(TASK_WORK_POLL_US);\n+\t}\n+\treturn -ETIMEDOUT;\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 TASK_WORK_POLL_ITERS; i++) {\n+\t\t__s64 value = 0;\n+\n+\t\tif (!task_work_status(skel-\u003emaps.status, RACE_READY_SEQ, \u0026value) \u0026\u0026\n+\t\t value == seq)\n+\t\t\treturn 0;\n+\t\tif (!task_work_status(skel-\u003emaps.status, 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(TASK_WORK_POLL_US);\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_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_set_status(skel-\u003emaps.status, 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+\t__s32 result;\n+\tint err;\n+\n+\terr = task_work_wait_seq(skel-\u003emaps.status, RACE_RESULT, seq, \u0026result);\n+\treturn err ?: result;\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 TASK_WORK_POLL_ITERS; 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(TASK_WORK_POLL_US);\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_thread_arg trigger_arg = {\n+\t\t.ctx = \u0026ctx,\n+\t\t.tid = \u0026ctx.trigger_tid,\n+\t};\n+\tstruct task_work_race_thread_arg target_arg = {\n+\t\t.ctx = \u0026ctx,\n+\t\t.tid = \u0026ctx.target_tid,\n+\t};\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\ttrigger_arg.cpu = cpu;\n+\t\telse\n+\t\t\ttarget_arg.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_worker, \u0026target_arg);\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_worker, \u0026trigger_arg);\n+\tif (!ASSERT_OK(err, \"pthread_create trigger\"))\n+\t\tgoto stop;\n+\ttrigger_started = true;\n+\n+\tfor (i = 0; i \u003c TASK_WORK_START_POLL_ITERS; 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(TASK_WORK_POLL_US);\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 after READY. A scheduling attempt\n+\t\t * racing with deletion can return -EBUSY, which the BPF program\n+\t\t * retries without publishing RESULT. The retry can then report\n+\t\t * -ENOENT because the element is gone; accept that terminal result.\n+\t\t * A later variant 0 round recreates the element.\n+\t\t */\n+\t\tif (variant == 2 \u0026\u0026 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+}\n+\n+#define TASK_WORK_BUSY_ROUNDS\t\t32\n+#define TASK_WORK_BUSY_MAX_ATTEMPTS\t96\n+\n+/* Must match progs/task_work_busy.c. */\n+enum task_work_busy_status {\n+\tBUSY_ARM_SEQ,\n+\t/*\n+\t * High 32 bits hold the sequence. BUSY_WINDOW low bits: 1 open,\n+\t * 2 the in-window attempt was seen before the handler returned,\n+\t * 3 the wait bound was reached without it. Result slots: signed\n+\t * error in the low 32 bits.\n+\t */\n+\tBUSY_WINDOW,\n+\tBUSY_ACK,\n+\tBUSY_GEN1_RESULT,\n+\tBUSY_GEN2_RESULT,\n+\tBUSY_CB1_SEQ,\n+\tBUSY_CB2_COUNT,\n+\tBUSY_CB2_SEQ,\n+\tBUSY_RECOVER_SEQ,\n+\tBUSY_LAST_WAIT_NS,\n+\tBUSY_STATUS_MAX,\n+};\n+\n+enum task_work_busy_attempt {\n+\tTWB_VALID,\n+\tTWB_MISSED,\n+\tTWB_HARD_FAIL,\n+};\n+\n+static const char *twb_attempt_name(enum task_work_busy_attempt r)\n+{\n+\tswitch (r) {\n+\tcase TWB_VALID:\n+\t\treturn \"valid\";\n+\tcase TWB_MISSED:\n+\t\treturn \"missed\";\n+\tdefault:\n+\t\treturn \"fail\";\n+\t}\n+}\n+\n+static int task_work_busy_wait_value(struct task_work_busy *skel, int idx,\n+\t\t\t\t __s64 want)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i \u003c TASK_WORK_POLL_ITERS; i++) {\n+\t\t__s64 value = 0;\n+\n+\t\tif (!task_work_status(skel-\u003emaps.status, idx, \u0026value) \u0026\u0026 value == want)\n+\t\t\treturn 0;\n+\t\tusleep(TASK_WORK_POLL_US);\n+\t}\n+\treturn -ETIMEDOUT;\n+}\n+\n+/* Wait until the window slot reports a final state (2 or 3) for seq. */\n+static int task_work_busy_wait_window(struct task_work_busy *skel, __u32 seq,\n+\t\t\t\t __s64 *window)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i \u003c TASK_WORK_POLL_ITERS; i++) {\n+\t\t__s64 value = 0;\n+\n+\t\tif (!task_work_status(skel-\u003emaps.status, BUSY_WINDOW, \u0026value) \u0026\u0026\n+\t\t (__u32)((__u64)value \u003e\u003e 32) == seq \u0026\u0026\n+\t\t (__u32)value \u003e= 2) {\n+\t\t\t*window = value;\n+\t\t\treturn 0;\n+\t\t}\n+\t\tusleep(TASK_WORK_POLL_US);\n+\t}\n+\treturn -ETIMEDOUT;\n+}\n+\n+static int task_work_busy_prepare_elem(struct task_work_busy *skel, int key)\n+{\n+\tstruct task_work_value value = {};\n+\n+\tif (!bpf_map_update_elem(bpf_map__fd(skel-\u003emaps.hmap), \u0026key, \u0026value,\n+\t\t\t\t BPF_NOEXIST))\n+\t\treturn 0;\n+\treturn -errno;\n+}\n+\n+static int task_work_busy_reset(struct task_work_busy *skel)\n+{\n+\tstatic const int zeroed[] = {\n+\t\tBUSY_WINDOW, BUSY_ACK, BUSY_GEN1_RESULT, BUSY_GEN2_RESULT,\n+\t\tBUSY_CB1_SEQ, BUSY_CB2_COUNT, BUSY_CB2_SEQ, BUSY_RECOVER_SEQ,\n+\t\tBUSY_LAST_WAIT_NS,\n+\t};\n+\tint i;\n+\n+\tfor (i = 0; i \u003c ARRAY_SIZE(zeroed); i++)\n+\t\tif (task_work_set_status(skel-\u003emaps.status, zeroed[i], 0))\n+\t\t\treturn -errno;\n+\treturn 0;\n+}\n+\n+/*\n+ * join waits for clear_child_tid, which is cleared before exit_task_work().\n+ * PID lookup stops finding the worker only after its exit task_work has run.\n+ * Probe with signal 0; no signal is delivered. Do not create new workers\n+ * until both old TIDs have disappeared.\n+ */\n+static int task_work_busy_wait_exit(int tid)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i \u003c TASK_WORK_POLL_ITERS; i++) {\n+\t\tif (syscall(__NR_tgkill, getpid(), tid, 0)) {\n+\t\t\tif (errno == ESRCH)\n+\t\t\t\treturn 0;\n+\t\t\treturn -errno;\n+\t\t}\n+\t\tusleep(TASK_WORK_POLL_US);\n+\t}\n+\treturn -ETIMEDOUT;\n+}\n+\n+/*\n+ * Run one attempt on dedicated workers and report its outcome. The\n+ * workers are stopped, joined and waited out of PID lookup before another\n+ * attempt can reset the shared state. join alone does not wait for their\n+ * exit task_work to finish.\n+ */\n+static enum task_work_busy_attempt\n+task_work_busy_run_attempt(struct task_work_busy *skel, int seq,\n+\t\t\t int trigger_cpu, int target_cpu, __s64 *out_wait_ns)\n+{\n+\tstruct task_work_race_ctx ctx = {};\n+\tstruct task_work_race_thread_arg trigger_arg = {\n+\t\t.ctx = \u0026ctx,\n+\t\t.cpu = trigger_cpu,\n+\t\t.tid = \u0026ctx.trigger_tid,\n+\t};\n+\tstruct task_work_race_thread_arg target_arg = {\n+\t\t.ctx = \u0026ctx,\n+\t\t.cpu = target_cpu,\n+\t\t.tid = \u0026ctx.target_tid,\n+\t};\n+\tenum task_work_busy_attempt result = TWB_HARD_FAIL;\n+\tpthread_t trigger, target;\n+\tbool trigger_started = false, target_started = false;\n+\tbool cleanup_ok = true;\n+\t__s64 window = 0, wait_ns = 0, count = -1;\n+\t__s32 res = 0;\n+\tint err, i;\n+\n+\t*out_wait_ns = 0;\n+\t/* Do not let a reused TID match the previous attempt's filter. */\n+\tskel-\u003ebss-\u003etrigger_tid = 0;\n+\tskel-\u003ebss-\u003etarget_tid = 0;\n+\n+\t/* Clear old state before enabling workers that can read it. */\n+\tif (!ASSERT_OK(task_work_busy_prepare_elem(skel, seq), \"prepare elem\"))\n+\t\tgoto out;\n+\tif (!ASSERT_OK(task_work_busy_reset(skel), \"reset slots\"))\n+\t\tgoto out;\n+\n+\terr = pthread_create(\u0026target, NULL, task_work_race_worker, \u0026target_arg);\n+\tif (err) {\n+\t\tASSERT_OK(err, \"pthread_create target\");\n+\t\tgoto out;\n+\t}\n+\ttarget_started = true;\n+\terr = pthread_create(\u0026trigger, NULL, task_work_race_worker, \u0026trigger_arg);\n+\tif (err) {\n+\t\tASSERT_OK(err, \"pthread_create trigger\");\n+\t\tgoto out;\n+\t}\n+\ttrigger_started = true;\n+\n+\tfor (i = 0; i \u003c TASK_WORK_START_POLL_ITERS; 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(TASK_WORK_POLL_US);\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 out;\n+\tif (!ASSERT_OK(__atomic_load_n(\u0026ctx.setup_err, __ATOMIC_ACQUIRE),\n+\t\t \"thread affinity\"))\n+\t\tgoto out;\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+\t/* Arm last, once this attempt's value, slots and TIDs are in place. */\n+\tif (!ASSERT_OK(task_work_set_status(skel-\u003emaps.status, BUSY_ARM_SEQ, seq),\n+\t\t \"arm\"))\n+\t\tgoto out;\n+\n+\t/* A fresh value has no expected BUSY source: check the first result. */\n+\terr = task_work_wait_seq(skel-\u003emaps.status, BUSY_GEN1_RESULT, seq, \u0026res);\n+\tif (!ASSERT_OK(err, \"gen1 result\"))\n+\t\tgoto out;\n+\tif (!ASSERT_OK(res, \"gen1 schedule\")) {\n+\t\tfprintf(stderr, \"attempt %d: gen1 failed: %d\\n\", seq, res);\n+\t\tgoto out;\n+\t}\n+\n+\tif (!ASSERT_OK(task_work_busy_wait_window(skel, seq, \u0026window),\n+\t\t \"window close\"))\n+\t\tgoto out;\n+\n+\tif ((__u32)window == 3) {\n+\t\t/*\n+\t\t * The attempt did not arrive inside the wait bound. The\n+\t\t * elapsed time is a diagnostic only; count\n+\t\t * the miss and retry with a fresh attempt.\n+\t\t */\n+\t\tresult = TWB_MISSED;\n+\t\tgoto out;\n+\t}\n+\n+\t/*\n+\t * Valid window: the handler saw this sequence's attempt before\n+\t * returning, so the attempt ran while the scheduling irq_work was\n+\t * still BUSY. The elapsed time is a diagnostic only: host vCPU\n+\t * preemption can stretch wall-clock time without affecting the\n+\t * gate behavior under test.\n+\t */\n+\terr = task_work_wait_seq(skel-\u003emaps.status, BUSY_ACK, seq, \u0026res);\n+\tif (!ASSERT_OK(err, \"in-window attempt\"))\n+\t\tgoto out;\n+\tif (res != -EBUSY) {\n+\t\tfprintf(stderr,\n+\t\t\t\"attempt %d: in-window reuse %s (%d), expected -EBUSY\\n\",\n+\t\t\tseq, res == 0 ? \"accepted\" : \"failed\", res);\n+\t\tASSERT_EQ(res, -EBUSY, \"in-window BUSY gate rejection\");\n+\t\tgoto out;\n+\t}\n+\n+\t/*\n+\t * The window was released; retry until BUSY clears. The second\n+\t * callback must then run exactly once.\n+\t */\n+\tif (!ASSERT_OK(task_work_set_status(skel-\u003emaps.status, BUSY_RECOVER_SEQ, seq),\n+\t\t \"enable recovery\"))\n+\t\tgoto out;\n+\terr = task_work_wait_seq(skel-\u003emaps.status, BUSY_GEN2_RESULT, seq, \u0026res);\n+\tif (!ASSERT_OK(err, \"recovery result\"))\n+\t\tgoto out;\n+\tif (!ASSERT_OK(res, \"recovery schedule\")) {\n+\t\tfprintf(stderr, \"attempt %d: recovery failed: %d\\n\", seq, res);\n+\t\tgoto out;\n+\t}\n+\tif (!ASSERT_OK(task_work_busy_wait_value(skel, BUSY_CB2_SEQ, seq),\n+\t\t \"gen2 callback\"))\n+\t\tgoto out;\n+\tresult = TWB_VALID;\n+out:\n+\t__atomic_store_n(\u0026ctx.stop, 1, __ATOMIC_RELEASE);\n+\tif (trigger_started \u0026\u0026\n+\t (!ASSERT_OK(pthread_join(trigger, NULL), \"trigger join\") ||\n+\t !ASSERT_OK(task_work_busy_wait_exit(ctx.trigger_tid), \"trigger exit\"))) {\n+\t\tcleanup_ok = false;\n+\t\tresult = TWB_HARD_FAIL;\n+\t}\n+\tif (target_started \u0026\u0026\n+\t (!ASSERT_OK(pthread_join(target, NULL), \"target join\") ||\n+\t !ASSERT_OK(task_work_busy_wait_exit(ctx.target_tid), \"target exit\"))) {\n+\t\tcleanup_ok = false;\n+\t\tresult = TWB_HARD_FAIL;\n+\t}\n+\t/* Both workers have passed exit_task_work; the count is now stable. */\n+\tif (result == TWB_VALID) {\n+\t\tif (!ASSERT_OK(task_work_status(skel-\u003emaps.status, BUSY_CB2_COUNT, \u0026count),\n+\t\t\t \"cb2 count\")) {\n+\t\t\tresult = TWB_HARD_FAIL;\n+\t\t} else if (!ASSERT_EQ(count, 1, \"gen2 callback exactly once\")) {\n+\t\t\tfprintf(stderr, \"attempt %d: gen2 callback count %lld\\n\",\n+\t\t\t\tseq, (long long)count);\n+\t\t\tresult = TWB_HARD_FAIL;\n+\t\t}\n+\t}\n+\t/*\n+\t * Only a captured final window belongs to this attempt. The fexit\n+\t * runs in the trigger's interrupted context, so successful worker\n+\t * cleanup also completes its diagnostic write. Read once here;\n+\t * the elapsed value does not decide whether the attempt passed.\n+\t */\n+\tif (cleanup_ok \u0026\u0026 window) {\n+\t\terr = task_work_status(skel-\u003emaps.status, BUSY_LAST_WAIT_NS, \u0026wait_ns);\n+\t\tif (err) {\n+\t\t\terr = -errno;\n+\t\t\tif (result != TWB_HARD_FAIL) {\n+\t\t\t\tASSERT_OK(err, \"wait diagnostic\");\n+\t\t\t\tresult = TWB_HARD_FAIL;\n+\t\t\t} else {\n+\t\t\t\tfprintf(stderr,\n+\t\t\t\t\t\"attempt %d: elapsed diagnostic unavailable: %d\\n\",\n+\t\t\t\t\tseq, err);\n+\t\t\t}\n+\t\t}\n+\t}\n+\t*out_wait_ns = wait_ns;\n+\tfprintf(stderr, \"attempt %d: %s (window %u wait_ns %lld)\\n\",\n+\t\tseq, twb_attempt_name(result), (__u32)window, (long long)wait_ns);\n+\treturn result;\n+}\n+\n+void serial_test_task_work_busy(void)\n+{\n+\tstruct task_work_busy *skel;\n+\tstruct bpf_link *link_tp = NULL, *link_fexit = NULL;\n+\tcpu_set_t allowed;\n+\tint cpu, cpu_count = 0, trigger_cpu = -1, target_cpu = -1;\n+\tint attempt, attempts_started = 0, valid = 0, missed = 0;\n+\tbool hard_fail = false;\n+\t__s64 max_wait_ns = 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\ttrigger_cpu = cpu;\n+\t\telse\n+\t\t\ttarget_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_busy__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"open_and_load\"))\n+\t\treturn;\n+\tif (skel-\u003ekconfig-\u003eCONFIG_PREEMPT_RT) {\n+\t\tprintf(\"%s:SKIP:CONFIG_PREEMPT_RT=y runs irq_work in a kernel thread\\n\",\n+\t\t __func__);\n+\t\ttest__skip();\n+\t\tgoto cleanup;\n+\t}\n+\n+\tlink_tp = bpf_program__attach_tracepoint(skel-\u003eprogs.busy_sched_work,\n+\t\t\t\t\t\t \"syscalls\",\n+\t\t\t\t\t\t \"sys_enter_getppid\");\n+\tif (!ASSERT_OK_PTR(link_tp, \"attach tracepoint\"))\n+\t\tgoto cleanup;\n+\n+\tlink_fexit = bpf_program__attach_trace(skel-\u003eprogs.busy_fexit_task_work_add);\n+\tif (!ASSERT_OK_PTR(link_fexit, \"attach fexit\"))\n+\t\tgoto cleanup;\n+\n+\tfor (attempt = 1;\n+\t attempt \u003c= TASK_WORK_BUSY_MAX_ATTEMPTS \u0026\u0026\n+\t valid \u003c TASK_WORK_BUSY_ROUNDS;\n+\t attempt++) {\n+\t\tenum task_work_busy_attempt res;\n+\t\t__s64 wait_ns = 0;\n+\n+\t\tattempts_started = attempt;\n+\t\tres = task_work_busy_run_attempt(skel, attempt, trigger_cpu,\n+\t\t\t\t\t\t target_cpu, \u0026wait_ns);\n+\t\tswitch (res) {\n+\t\tcase TWB_VALID:\n+\t\t\tvalid++;\n+\t\t\tif (wait_ns \u003e max_wait_ns)\n+\t\t\t\tmax_wait_ns = wait_ns;\n+\t\t\tbreak;\n+\t\tcase TWB_MISSED:\n+\t\t\tmissed++;\n+\t\t\tbreak;\n+\t\tcase TWB_HARD_FAIL:\n+\t\t\thard_fail = true;\n+\t\t\tbreak;\n+\t\t}\n+\t\tif (hard_fail)\n+\t\t\tbreak;\n+\t}\n+\n+\tfprintf(stderr,\n+\t\t\"task_work_busy: %d/%d valid, %d missed, %d attempts, max_wait %lld ns\\n\",\n+\t\tvalid, TASK_WORK_BUSY_ROUNDS, missed, attempts_started,\n+\t\t(long long)max_wait_ns);\n+\n+\t/*\n+\t * The coverage assertion only makes sense when no hard\n+\t * failure aborted the loop; after a hard failure keep the primary\n+\t * reason instead of deriving more assertion noise from it.\n+\t */\n+\tif (!hard_fail)\n+\t\tASSERT_EQ(valid, TASK_WORK_BUSY_ROUNDS, \"valid busy windows\");\n+cleanup:\n+\tbpf_link__destroy(link_tp);\n+\tbpf_link__destroy(link_fexit);\n+\ttask_work_busy__destroy(skel);\n+}\ndiff --git a/tools/testing/selftests/bpf/progs/task_work_busy.c b/tools/testing/selftests/bpf/progs/task_work_busy.c\nnew file mode 100644\nindex 0000000000000..9960011edadc5\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/task_work_busy.c\n@@ -0,0 +1,325 @@\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 \u003cbpf/bpf_core_read.h\u003e\n+\n+char _license[] SEC(\"license\") = \"GPL\";\n+\n+extern bool CONFIG_PREEMPT_RT __kconfig __weak;\n+\n+/*\n+ * Bounds for the hardirq busy-wait that holds the scheduling handler\n+ * inside the real window: task_work_add() has published the callback,\n+ * but the handler's tail state transition has not run yet.\n+ * irq_work_single() clears BUSY only after the handler returns, so\n+ * while this wait runs, the context's scheduling irq_work is BUSY,\n+ * even though the first callback may already have completed and\n+ * returned the context to STANDBY.\n+ *\n+ * The wait is bounded both by a bpf_ktime_get_ns() check and by an\n+ * iteration budget. The time bound is checked against a monotonic\n+ * clock: host vCPU preemption can stretch the wall-clock duration of\n+ * the window, which only makes the window longer, never shorter.\n+ */\n+#define BUSY_WAIT_NS\t\t4000000ULL\n+#define BUSY_WAIT_ITERS\t\t1000000ULL\n+\n+int trigger_tid;\n+int target_tid;\n+\n+/*\n+ * High 32 bits of the result slots hold the sequence, low 32 bits the\n+ * signed error. BUSY_WINDOW low bits: 1 open, 2 the in-window attempt\n+ * was seen before the handler returned, 3 the wait bound was reached\n+ * without it. Must match the test.\n+ */\n+enum task_work_busy_status {\n+\tBUSY_ARM_SEQ,\n+\tBUSY_WINDOW,\n+\tBUSY_ACK,\n+\tBUSY_GEN1_RESULT,\n+\tBUSY_GEN2_RESULT,\n+\tBUSY_CB1_SEQ,\n+\tBUSY_CB2_COUNT,\n+\tBUSY_CB2_SEQ,\n+\tBUSY_RECOVER_SEQ,\n+\tBUSY_LAST_WAIT_NS,\n+\tBUSY_STATUS_MAX,\n+};\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_ARRAY);\n+\t__uint(max_entries, BUSY_STATUS_MAX);\n+\t__type(key, int);\n+\t__type(value, __s64);\n+} status SEC(\".maps\");\n+\n+struct task_work_busy_value {\n+\t__u32 seq;\n+\tchar data[60];\n+\tstruct bpf_task_work tw;\n+};\n+\n+/*\n+ * max_entries must cover TASK_WORK_BUSY_MAX_ATTEMPTS in the test: each\n+ * attempt schedules into its own fresh value.\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, 128);\n+\t__type(key, int);\n+\t__type(value, struct task_work_busy_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 __always_inline __s64 get_status(int key)\n+{\n+\t__s64 *slot = bpf_map_lookup_elem(\u0026status, \u0026key);\n+\n+\treturn slot ? *slot : 0;\n+}\n+\n+struct busy_wait_ctx {\n+\t__u64 seq;\n+\t__u64 start;\n+\tbool acked;\n+};\n+\n+/* bpf_loop() callback: return 0 continues the loop, return 1 stops it. */\n+static long busy_wait_cb(__u32 i, void *pctx)\n+{\n+\tstruct busy_wait_ctx *w = pctx;\n+\n+\tif (((__u64)get_status(BUSY_ACK) \u003e\u003e 32) == w-\u003eseq) {\n+\t\tw-\u003eacked = true;\n+\t\treturn 1;\n+\t}\n+\tif (bpf_ktime_get_ns() - w-\u003estart \u003e BUSY_WAIT_NS)\n+\t\treturn 1;\n+\treturn 0;\n+}\n+\n+static int busy_cb_gen1(struct bpf_map *map, void *key, void *value)\n+{\n+\tstruct task_work_busy_value *work = value;\n+\t__u64 we = (__u64)get_status(BUSY_WINDOW) \u003e\u003e 32;\n+\n+\t/*\n+\t * Backstop for deliveries from abandoned attempts: accept only\n+\t * while no window for a different sequence is published. The\n+\t * test waits for the old workers to disappear from PID lookup,\n+\t * after their exit task_work, before resetting the shared state.\n+\t * This callback may run before fexit publishes the window state.\n+\t */\n+\tif (we \u0026\u0026 we != (__u64)work-\u003eseq)\n+\t\treturn 0;\n+\tset_status(BUSY_CB1_SEQ, work-\u003eseq);\n+\treturn 0;\n+}\n+\n+static int busy_cb_gen2(struct bpf_map *map, void *key, void *value)\n+{\n+\tstruct task_work_busy_value *work = value;\n+\t__s64 *count;\n+\tint key2 = BUSY_CB2_COUNT;\n+\n+\t/*\n+\t * Count only deliveries belonging to the sequence whose window is\n+\t * (or was) published: a late delivery from an abandoned attempt\n+\t * must not satisfy a later exactly-once check.\n+\t */\n+\tif ((__u64)work-\u003eseq != ((__u64)get_status(BUSY_WINDOW) \u003e\u003e 32))\n+\t\treturn 0;\n+\tset_status(BUSY_CB2_SEQ, work-\u003eseq);\n+\tcount = bpf_map_lookup_elem(\u0026status, \u0026key2);\n+\tif (count)\n+\t\t__sync_fetch_and_add(count, 1);\n+\treturn 0;\n+}\n+\n+SEC(\"fexit/task_work_add\")\n+int BPF_PROG(busy_fexit_task_work_add, struct task_struct *task,\n+\t struct callback_head *work, int notify, int retval)\n+{\n+\tstruct busy_wait_ctx w = {};\n+\t__u64 seq, elapsed;\n+\n+\tseq = (__u64)get_status(BUSY_ARM_SEQ);\n+\tif (!seq)\n+\t\treturn 0;\n+\n+\t/*\n+\t * One-shot arm. Only this test's own first-round add may consume\n+\t * it and hold the window: a successful TWA_SIGNAL add of the\n+\t * bpf_task_work callback, issued from the trigger thread's CPU by\n+\t * the self-IPI'd scheduling handler and targeting the target\n+\t * thread. Everything else leaves the arm untouched.\n+\t */\n+\tif (retval != 0 || notify != TWA_SIGNAL ||\n+\t BPF_CORE_READ(task, pid) != target_tid)\n+\t\treturn 0;\n+\tif ((__u32)bpf_get_current_pid_tgid() != (__u32)trigger_tid)\n+\t\treturn 0;\n+\n+\tset_status(BUSY_ARM_SEQ, 0);\n+\tset_status(BUSY_WINDOW, (seq \u003c\u003c 32) | 1);\n+\n+\tw.seq = seq;\n+\tw.start = bpf_ktime_get_ns();\n+\tbpf_loop(BUSY_WAIT_ITERS, busy_wait_cb, \u0026w, 0);\n+\telapsed = bpf_ktime_get_ns() - w.start;\n+\n+\t/*\n+\t * Publish elapsed time for logging after worker cleanup. Window\n+\t * validity depends on seeing ACK here, not on this diagnostic.\n+\t */\n+\tset_status(BUSY_LAST_WAIT_NS, (__s64)elapsed);\n+\tif (w.acked)\n+\t\tset_status(BUSY_WINDOW, (seq \u003c\u003c 32) | 2);\n+\telse\n+\t\tset_status(BUSY_WINDOW, (seq \u003c\u003c 32) | 3);\n+\treturn 0;\n+}\n+\n+/*\n+ * Attempt the second scheduling round on the same context, targeting the\n+ * trigger thread. The attempt uses the value keyed by its own sequence,\n+ * so it can never touch another attempt's context. Returns true if a\n+ * terminal result was published into res_key; a transient -EBUSY is left\n+ * unpublished for the caller's retry policy, mirroring the race test's\n+ * convention.\n+ */\n+static __always_inline bool busy_try_gen2(__u64 seq, int res_key)\n+{\n+\tstruct task_work_busy_value *work;\n+\tstruct task_struct *task;\n+\tint key = (int)seq, err;\n+\n+\ttask = bpf_task_from_pid(trigger_tid);\n+\tif (!task) {\n+\t\tset_status(res_key, ((__s64)seq \u003c\u003c 32) | (__u32)-ESRCH);\n+\t\treturn true;\n+\t}\n+\twork = bpf_map_lookup_elem(\u0026hmap, \u0026key);\n+\tif (!work) {\n+\t\tbpf_task_release(task);\n+\t\tset_status(res_key, ((__s64)seq \u003c\u003c 32) | (__u32)-ENOENT);\n+\t\treturn true;\n+\t}\n+\twork-\u003eseq = seq;\n+\terr = bpf_task_work_schedule_signal(task, \u0026work-\u003etw, \u0026hmap,\n+\t\t\t\t\t busy_cb_gen2);\n+\tbpf_task_release(task);\n+\tif (err == -EBUSY)\n+\t\treturn false;\n+\tset_status(res_key, ((__s64)seq \u003c\u003c 32) | (__u32)err);\n+\treturn true;\n+}\n+\n+SEC(\"tracepoint/syscalls/sys_enter_getppid\")\n+int busy_sched_work(void *ctx)\n+{\n+\t__u32 tid = (__u32)bpf_get_current_pid_tgid();\n+\tstruct task_work_busy_value *work;\n+\tstruct task_struct *task;\n+\t__u64 window, seq;\n+\tint key, err;\n+\n+\t/* Also keep the kconfig extern in object BTF for the userspace skip. */\n+\tif (CONFIG_PREEMPT_RT)\n+\t\treturn 0;\n+\n+\tif (tid == (__u32)trigger_tid) {\n+\t\t/*\n+\t\t * Trigger thread: schedule the first round once per armed\n+\t\t * sequence, targeting the target thread. The kfunc queues\n+\t\t * the scheduling irq_work and returns; the handler then\n+\t\t * parks inside the fexit window above. A fresh value has no\n+\t\t * expected BUSY source, so publish every scheduling result.\n+\t\t */\n+\t\tseq = (__u64)get_status(BUSY_ARM_SEQ);\n+\t\tif (!seq ||\n+\t\t ((__u64)get_status(BUSY_GEN1_RESULT) \u003e\u003e 32) == seq)\n+\t\t\treturn 0;\n+\t\tkey = (int)seq;\n+\t\ttask = bpf_task_from_pid(target_tid);\n+\t\tif (!task) {\n+\t\t\tset_status(BUSY_GEN1_RESULT,\n+\t\t\t\t ((__s64)seq \u003c\u003c 32) | (__u32)-ESRCH);\n+\t\t\tset_status(BUSY_ARM_SEQ, 0);\n+\t\t\treturn 0;\n+\t\t}\n+\t\twork = bpf_map_lookup_elem(\u0026hmap, \u0026key);\n+\t\tif (!work) {\n+\t\t\tbpf_task_release(task);\n+\t\t\tset_status(BUSY_GEN1_RESULT,\n+\t\t\t\t ((__s64)seq \u003c\u003c 32) | (__u32)-ENOENT);\n+\t\t\tset_status(BUSY_ARM_SEQ, 0);\n+\t\t\treturn 0;\n+\t\t}\n+\t\twork-\u003eseq = seq;\n+\t\terr = bpf_task_work_schedule_signal(task, \u0026work-\u003etw, \u0026hmap,\n+\t\t\t\t\t\t busy_cb_gen1);\n+\t\tbpf_task_release(task);\n+\t\tset_status(BUSY_GEN1_RESULT, ((__s64)seq \u003c\u003c 32) | (__u32)err);\n+\t\treturn 0;\n+\t}\n+\n+\tif (tid != (__u32)target_tid)\n+\t\treturn 0;\n+\n+\twindow = (__u64)get_status(BUSY_WINDOW);\n+\tseq = window \u003e\u003e 32;\n+\tif (!seq || (__u32)window == 0)\n+\t\treturn 0;\n+\t/*\n+\t * The first callback must have completed for this sequence. Being\n+\t * inside the target thread's own subsequent syscall proves the\n+\t * callback wrapper has returned and the context is STANDBY, which\n+\t * is stronger than observing the completion marker from another\n+\t * thread.\n+\t */\n+\tif ((__u64)get_status(BUSY_CB1_SEQ) != seq)\n+\t\treturn 0;\n+\n+\tif ((__u64)get_status(BUSY_ACK) \u003e\u003e 32 != seq) {\n+\t\t/*\n+\t\t * The in-window attempt, exactly once per sequence. On a\n+\t\t * fixed kernel it must be rejected with -EBUSY: the context\n+\t\t * is STANDBY, the map and program references are alive and\n+\t\t * no other scheduler exists, so only the BUSY gate can\n+\t\t * reject here.\n+\t\t */\n+\t\tif (!busy_try_gen2(seq, BUSY_ACK))\n+\t\t\tset_status(BUSY_ACK,\n+\t\t\t\t ((__s64)seq \u003c\u003c 32) | (__u32)-EBUSY);\n+\t\treturn 0;\n+\t}\n+\n+\t/*\n+\t * Recovery, gated by the test: the fexit program releasing the\n+\t * window does not mean the whole old handler has returned and\n+\t * BUSY has cleared yet, so retry until the schedule succeeds.\n+\t */\n+\tif ((__u64)get_status(BUSY_RECOVER_SEQ) != seq)\n+\t\treturn 0;\n+\tif ((__s32)get_status(BUSY_ACK) == 0)\n+\t\treturn 0;\t/* scheduled in-window; nothing to recover */\n+\tif (((__u64)get_status(BUSY_GEN2_RESULT) \u003e\u003e 32) == seq)\n+\t\treturn 0;\t/* terminal recovery result published */\n+\tbusy_try_gen2(seq, BUSY_GEN2_RESULT);\n+\treturn 0;\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/10/12 06:35 | agent | patch-evaluator |
5mModel: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_scheduled",
"bpf_task_work_cancel_and_free"
],
"Reasoning": "The patch addresses concurrency race conditions in BPF task_work scheduling, cancellation, and context lifecycle in kernel/bpf/helpers.c. Specifically, it prevents use-after-free/NULL-dereference when ctx-\u003etask is cleared by a completed callback before bpf_task_work_irq() finishes reading it, and adds a check for irq_work_is_busy() to prevent reusing task_work contexts while the previous scheduling irq_work is still active. These paths are reachable from userspace via BPF programs scheduling task_work callbacks using kfuncs. Fuzzing this complex concurrency and state machine logic is valuable for detecting exposed latent race conditions and assertions.",
"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 c0f793eb9550fd57d2c54c37baf7a7b04bbe5037
Author: syz-cluster <triage@syzkaller.com>
Date: Mon Oct 12 06:35:25 2026 +0000
syz-cluster: applied patch under review
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index b3cc5c8fc8756..350269b8646e4 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -4437,15 +4437,21 @@ 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
* cancelled, we put that ref on callback's behalf. If we couldn't
* cancel, callback will inevitably run or has already completed
* running, and it would have taken care of its ctx ref itself.
+ *
+ * The caller passes the task explicitly: the scheduling handler must
+ * not read ctx->task after the work is published (the callback may
+ * have reset it), while in the asynchronous cancellation path
+ * ctx->task is stable, see bpf_task_work_cancel_scheduled().
*/
- if (task_work_cancel(ctx->task, &ctx->work))
+ if (task_work_cancel(task, &ctx->work))
bpf_task_work_ctx_put(ctx);
}
@@ -4486,6 +4492,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 +4503,16 @@ static void bpf_task_work_irq(struct irq_work *irq_work)
return;
}
- err = task_work_add(ctx->task, &ctx->work, ctx->mode);
+ /*
+ * Capture the task pointer before the work is published: once the
+ * callback runs, it may reset ctx->task. The capture happens while
+ * this round's task reference is held, and every path that can
+ * release it runs after this handler entered its rcu read-side
+ * section. bpf_task_release() is put_task_struct_rcu_user(), so the
+ * task_struct itself cannot be freed before this section ends.
+ */
+ task = ctx->task;
+ err = task_work_add(task, &ctx->work, ctx->mode);
if (err) {
bpf_task_work_ctx_reset(ctx);
/*
@@ -4510,14 +4526,14 @@ static void bpf_task_work_irq(struct irq_work *irq_work)
/*
* 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.
+ * complete running by now, going SCHEDULING -> RUNNING, resetting
+ * ctx->task and dropping its ctx refcount. The rcu read-side section
+ * above keeps both the ctx memory and the captured task pointer
+ * valid until this handler returns.
*/
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 */
}
static struct bpf_task_work_ctx *bpf_task_work_fetch_ctx(struct bpf_task_work *tw,
@@ -4537,6 +4553,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 +4596,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);
}
+ /*
+ * STANDBY can be published before the previous scheduling irq_work
+ * returns. Claim PENDING before checking BUSY to order the check after
+ * that publication. No irq_work has been queued for this attempt yet,
+ * so reject reuse and roll back to STANDBY while BUSY is set. Preserve
+ * FREED if deletion wins the rollback race.
+ */
+ 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 +4657,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 +4805,13 @@ 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 */
+ /*
+ * Deletion observed SCHEDULED and won against the callback's
+ * transition to RUNNING. The callback takes its FREED exit without
+ * resetting ctx->task, and the transferred map reference prevents
+ * destruction until cancellation completes.
+ */
+ 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..deabc524dd497 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,15 @@
#include <stdio.h>
#include "task_work.skel.h"
#include "task_work_fail.skel.h"
+#include "task_work_race.skel.h"
+#include "task_work_busy.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 +160,692 @@ void test_task_work(void)
RUN_TESTS(task_work_fail);
}
+
+#define TASK_WORK_RACE_ROUNDS 1500
+#define TASK_WORK_POLL_US 100
+#define TASK_WORK_POLL_ITERS 20000
+#define TASK_WORK_START_POLL_ITERS 50000
+
+/* 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_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;
+};
+
+struct task_work_race_thread_arg {
+ struct task_work_race_ctx *ctx;
+ int cpu;
+ int *tid;
+};
+
+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_worker(void *arg)
+{
+ struct task_work_race_thread_arg *thread = arg;
+ struct task_work_race_ctx *ctx = thread->ctx;
+ int err;
+
+ err = task_work_race_pin_cpu(thread->cpu);
+ if (err)
+ __atomic_store_n(&ctx->setup_err, err, __ATOMIC_RELEASE);
+ __atomic_store_n(thread->tid, syscall(__NR_gettid), __ATOMIC_RELEASE);
+ while (!__atomic_load_n(&ctx->stop, __ATOMIC_ACQUIRE))
+ getppid();
+ return NULL;
+}
+
+static int task_work_status(struct bpf_map *map, int idx, __s64 *value)
+{
+ return bpf_map_lookup_elem(bpf_map__fd(map), &idx, value);
+}
+
+static int task_work_set_status(struct bpf_map *map, int idx, __s64 value)
+{
+ return bpf_map_update_elem(bpf_map__fd(map), &idx, &value, BPF_ANY);
+}
+
+/* Wait for seq, keeping polling errors separate from the scheduling result. */
+static int task_work_wait_seq(struct bpf_map *map, int idx, __u32 seq,
+ __s32 *result)
+{
+ int i;
+
+ for (i = 0; i < TASK_WORK_POLL_ITERS; i++) {
+ __s64 value = 0;
+
+ if (!task_work_status(map, idx, &value) &&
+ (__u32)((__u64)value >> 32) == seq) {
+ *result = (__s32)value;
+ return 0;
+ }
+ usleep(TASK_WORK_POLL_US);
+ }
+ return -ETIMEDOUT;
+}
+
+static int task_work_race_wait_ready(struct task_work_race *skel, __u32 seq)
+{
+ int i;
+
+ for (i = 0; i < TASK_WORK_POLL_ITERS; i++) {
+ __s64 value = 0;
+
+ if (!task_work_status(skel->maps.status, RACE_READY_SEQ, &value) &&
+ value == seq)
+ return 0;
+ if (!task_work_status(skel->maps.status, 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(TASK_WORK_POLL_US);
+ }
+ return -ETIMEDOUT;
+}
+
+static int task_work_race_prepare_elem(struct task_work_race *skel)
+{
+ struct task_work_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_set_status(skel->maps.status, 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)
+{
+ __s32 result;
+ int err;
+
+ err = task_work_wait_seq(skel->maps.status, RACE_RESULT, seq, &result);
+ return err ?: result;
+}
+
+static int task_work_race_wait_callback(struct task_work_race *skel,
+ __u32 seq)
+{
+ int i, key = seq;
+
+ for (i = 0; i < TASK_WORK_POLL_ITERS; i++) {
+ __u64 completed = 0;
+
+ if (!bpf_map_lookup_elem(bpf_map__fd(skel->maps.completed),
+ &key, &completed) && completed)
+ return 0;
+ usleep(TASK_WORK_POLL_US);
+ }
+ return -ETIMEDOUT;
+}
+
+void serial_test_task_work_race(void)
+{
+ struct task_work_race_ctx ctx = {};
+ struct task_work_race_thread_arg trigger_arg = {
+ .ctx = &ctx,
+ .tid = &ctx.trigger_tid,
+ };
+ struct task_work_race_thread_arg target_arg = {
+ .ctx = &ctx,
+ .tid = &ctx.target_tid,
+ };
+ 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)
+ trigger_arg.cpu = cpu;
+ else
+ target_arg.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_worker, &target_arg);
+ if (!ASSERT_OK(err, "pthread_create target"))
+ goto cleanup;
+ target_started = true;
+ err = pthread_create(&trigger, NULL, task_work_race_worker, &trigger_arg);
+ if (!ASSERT_OK(err, "pthread_create trigger"))
+ goto stop;
+ trigger_started = true;
+
+ for (i = 0; i < TASK_WORK_START_POLL_ITERS; i++) {
+ if (__atomic_load_n(&ctx.trigger_tid, __ATOMIC_ACQUIRE) &&
+ __atomic_load_n(&ctx.target_tid, __ATOMIC_ACQUIRE))
+ break;
+ usleep(TASK_WORK_POLL_US);
+ }
+ 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 after READY. A scheduling attempt
+ * racing with deletion can return -EBUSY, which the BPF program
+ * retries without publishing RESULT. The retry can then report
+ * -ENOENT because the element is gone; accept that terminal result.
+ * A later variant 0 round recreates the element.
+ */
+ if (variant == 2 && 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);
+}
+
+#define TASK_WORK_BUSY_ROUNDS 32
+#define TASK_WORK_BUSY_MAX_ATTEMPTS 96
+
+/* Must match progs/task_work_busy.c. */
+enum task_work_busy_status {
+ BUSY_ARM_SEQ,
+ /*
+ * High 32 bits hold the sequence. BUSY_WINDOW low bits: 1 open,
+ * 2 the in-window attempt was seen before the handler returned,
+ * 3 the wait bound was reached without it. Result slots: signed
+ * error in the low 32 bits.
+ */
+ BUSY_WINDOW,
+ BUSY_ACK,
+ BUSY_GEN1_RESULT,
+ BUSY_GEN2_RESULT,
+ BUSY_CB1_SEQ,
+ BUSY_CB2_COUNT,
+ BUSY_CB2_SEQ,
+ BUSY_RECOVER_SEQ,
+ BUSY_LAST_WAIT_NS,
+ BUSY_STATUS_MAX,
+};
+
+enum task_work_busy_attempt {
+ TWB_VALID,
+ TWB_MISSED,
+ TWB_HARD_FAIL,
+};
+
+static const char *twb_attempt_name(enum task_work_busy_attempt r)
+{
+ switch (r) {
+ case TWB_VALID:
+ return "valid";
+ case TWB_MISSED:
+ return "missed";
+ default:
+ return "fail";
+ }
+}
+
+static int task_work_busy_wait_value(struct task_work_busy *skel, int idx,
+ __s64 want)
+{
+ int i;
+
+ for (i = 0; i < TASK_WORK_POLL_ITERS; i++) {
+ __s64 value = 0;
+
+ if (!task_work_status(skel->maps.status, idx, &value) && value == want)
+ return 0;
+ usleep(TASK_WORK_POLL_US);
+ }
+ return -ETIMEDOUT;
+}
+
+/* Wait until the window slot reports a final state (2 or 3) for seq. */
+static int task_work_busy_wait_window(struct task_work_busy *skel, __u32 seq,
+ __s64 *window)
+{
+ int i;
+
+ for (i = 0; i < TASK_WORK_POLL_ITERS; i++) {
+ __s64 value = 0;
+
+ if (!task_work_status(skel->maps.status, BUSY_WINDOW, &value) &&
+ (__u32)((__u64)value >> 32) == seq &&
+ (__u32)value >= 2) {
+ *window = value;
+ return 0;
+ }
+ usleep(TASK_WORK_POLL_US);
+ }
+ return -ETIMEDOUT;
+}
+
+static int task_work_busy_prepare_elem(struct task_work_busy *skel, int key)
+{
+ struct task_work_value value = {};
+
+ if (!bpf_map_update_elem(bpf_map__fd(skel->maps.hmap), &key, &value,
+ BPF_NOEXIST))
+ return 0;
+ return -errno;
+}
+
+static int task_work_busy_reset(struct task_work_busy *skel)
+{
+ static const int zeroed[] = {
+ BUSY_WINDOW, BUSY_ACK, BUSY_GEN1_RESULT, BUSY_GEN2_RESULT,
+ BUSY_CB1_SEQ, BUSY_CB2_COUNT, BUSY_CB2_SEQ, BUSY_RECOVER_SEQ,
+ BUSY_LAST_WAIT_NS,
+ };
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(zeroed); i++)
+ if (task_work_set_status(skel->maps.status, zeroed[i], 0))
+ return -errno;
+ return 0;
+}
+
+/*
+ * join waits for clear_child_tid, which is cleared before exit_task_work().
+ * PID lookup stops finding the worker only after its exit task_work has run.
+ * Probe with signal 0; no signal is delivered. Do not create new workers
+ * until both old TIDs have disappeared.
+ */
+static int task_work_busy_wait_exit(int tid)
+{
+ int i;
+
+ for (i = 0; i < TASK_WORK_POLL_ITERS; i++) {
+ if (syscall(__NR_tgkill, getpid(), tid, 0)) {
+ if (errno == ESRCH)
+ return 0;
+ return -errno;
+ }
+ usleep(TASK_WORK_POLL_US);
+ }
+ return -ETIMEDOUT;
+}
+
+/*
+ * Run one attempt on dedicated workers and report its outcome. The
+ * workers are stopped, joined and waited out of PID lookup before another
+ * attempt can reset the shared state. join alone does not wait for their
+ * exit task_work to finish.
+ */
+static enum task_work_busy_attempt
+task_work_busy_run_attempt(struct task_work_busy *skel, int seq,
+ int trigger_cpu, int target_cpu, __s64 *out_wait_ns)
+{
+ struct task_work_race_ctx ctx = {};
+ struct task_work_race_thread_arg trigger_arg = {
+ .ctx = &ctx,
+ .cpu = trigger_cpu,
+ .tid = &ctx.trigger_tid,
+ };
+ struct task_work_race_thread_arg target_arg = {
+ .ctx = &ctx,
+ .cpu = target_cpu,
+ .tid = &ctx.target_tid,
+ };
+ enum task_work_busy_attempt result = TWB_HARD_FAIL;
+ pthread_t trigger, target;
+ bool trigger_started = false, target_started = false;
+ bool cleanup_ok = true;
+ __s64 window = 0, wait_ns = 0, count = -1;
+ __s32 res = 0;
+ int err, i;
+
+ *out_wait_ns = 0;
+ /* Do not let a reused TID match the previous attempt's filter. */
+ skel->bss->trigger_tid = 0;
+ skel->bss->target_tid = 0;
+
+ /* Clear old state before enabling workers that can read it. */
+ if (!ASSERT_OK(task_work_busy_prepare_elem(skel, seq), "prepare elem"))
+ goto out;
+ if (!ASSERT_OK(task_work_busy_reset(skel), "reset slots"))
+ goto out;
+
+ err = pthread_create(&target, NULL, task_work_race_worker, &target_arg);
+ if (err) {
+ ASSERT_OK(err, "pthread_create target");
+ goto out;
+ }
+ target_started = true;
+ err = pthread_create(&trigger, NULL, task_work_race_worker, &trigger_arg);
+ if (err) {
+ ASSERT_OK(err, "pthread_create trigger");
+ goto out;
+ }
+ trigger_started = true;
+
+ for (i = 0; i < TASK_WORK_START_POLL_ITERS; i++) {
+ if (__atomic_load_n(&ctx.trigger_tid, __ATOMIC_ACQUIRE) &&
+ __atomic_load_n(&ctx.target_tid, __ATOMIC_ACQUIRE))
+ break;
+ usleep(TASK_WORK_POLL_US);
+ }
+ if (!ASSERT_TRUE(__atomic_load_n(&ctx.trigger_tid, __ATOMIC_ACQUIRE) &&
+ __atomic_load_n(&ctx.target_tid, __ATOMIC_ACQUIRE),
+ "thread start"))
+ goto out;
+ if (!ASSERT_OK(__atomic_load_n(&ctx.setup_err, __ATOMIC_ACQUIRE),
+ "thread affinity"))
+ goto out;
+
+ skel->bss->trigger_tid = __atomic_load_n(&ctx.trigger_tid, __ATOMIC_ACQUIRE);
+ skel->bss->target_tid = __atomic_load_n(&ctx.target_tid, __ATOMIC_ACQUIRE);
+
+ /* Arm last, once this attempt's value, slots and TIDs are in place. */
+ if (!ASSERT_OK(task_work_set_status(skel->maps.status, BUSY_ARM_SEQ, seq),
+ "arm"))
+ goto out;
+
+ /* A fresh value has no expected BUSY source: check the first result. */
+ err = task_work_wait_seq(skel->maps.status, BUSY_GEN1_RESULT, seq, &res);
+ if (!ASSERT_OK(err, "gen1 result"))
+ goto out;
+ if (!ASSERT_OK(res, "gen1 schedule")) {
+ fprintf(stderr, "attempt %d: gen1 failed: %d\n", seq, res);
+ goto out;
+ }
+
+ if (!ASSERT_OK(task_work_busy_wait_window(skel, seq, &window),
+ "window close"))
+ goto out;
+
+ if ((__u32)window == 3) {
+ /*
+ * The attempt did not arrive inside the wait bound. The
+ * elapsed time is a diagnostic only; count
+ * the miss and retry with a fresh attempt.
+ */
+ result = TWB_MISSED;
+ goto out;
+ }
+
+ /*
+ * Valid window: the handler saw this sequence's attempt before
+ * returning, so the attempt ran while the scheduling irq_work was
+ * still BUSY. The elapsed time is a diagnostic only: host vCPU
+ * preemption can stretch wall-clock time without affecting the
+ * gate behavior under test.
+ */
+ err = task_work_wait_seq(skel->maps.status, BUSY_ACK, seq, &res);
+ if (!ASSERT_OK(err, "in-window attempt"))
+ goto out;
+ if (res != -EBUSY) {
+ fprintf(stderr,
+ "attempt %d: in-window reuse %s (%d), expected -EBUSY\n",
+ seq, res == 0 ? "accepted" : "failed", res);
+ ASSERT_EQ(res, -EBUSY, "in-window BUSY gate rejection");
+ goto out;
+ }
+
+ /*
+ * The window was released; retry until BUSY clears. The second
+ * callback must then run exactly once.
+ */
+ if (!ASSERT_OK(task_work_set_status(skel->maps.status, BUSY_RECOVER_SEQ, seq),
+ "enable recovery"))
+ goto out;
+ err = task_work_wait_seq(skel->maps.status, BUSY_GEN2_RESULT, seq, &res);
+ if (!ASSERT_OK(err, "recovery result"))
+ goto out;
+ if (!ASSERT_OK(res, "recovery schedule")) {
+ fprintf(stderr, "attempt %d: recovery failed: %d\n", seq, res);
+ goto out;
+ }
+ if (!ASSERT_OK(task_work_busy_wait_value(skel, BUSY_CB2_SEQ, seq),
+ "gen2 callback"))
+ goto out;
+ result = TWB_VALID;
+out:
+ __atomic_store_n(&ctx.stop, 1, __ATOMIC_RELEASE);
+ if (trigger_started &&
+ (!ASSERT_OK(pthread_join(trigger, NULL), "trigger join") ||
+ !ASSERT_OK(task_work_busy_wait_exit(ctx.trigger_tid), "trigger exit"))) {
+ cleanup_ok = false;
+ result = TWB_HARD_FAIL;
+ }
+ if (target_started &&
+ (!ASSERT_OK(pthread_join(target, NULL), "target join") ||
+ !ASSERT_OK(task_work_busy_wait_exit(ctx.target_tid), "target exit"))) {
+ cleanup_ok = false;
+ result = TWB_HARD_FAIL;
+ }
+ /* Both workers have passed exit_task_work; the count is now stable. */
+ if (result == TWB_VALID) {
+ if (!ASSERT_OK(task_work_status(skel->maps.status, BUSY_CB2_COUNT, &count),
+ "cb2 count")) {
+ result = TWB_HARD_FAIL;
+ } else if (!ASSERT_EQ(count, 1, "gen2 callback exactly once")) {
+ fprintf(stderr, "attempt %d: gen2 callback count %lld\n",
+ seq, (long long)count);
+ result = TWB_HARD_FAIL;
+ }
+ }
+ /*
+ * Only a captured final window belongs to this attempt. The fexit
+ * runs in the trigger's interrupted context, so successful worker
+ * cleanup also completes its diagnostic write. Read once here;
+ * the elapsed value does not decide whether the attempt passed.
+ */
+ if (cleanup_ok && window) {
+ err = task_work_status(skel->maps.status, BUSY_LAST_WAIT_NS, &wait_ns);
+ if (err) {
+ err = -errno;
+ if (result != TWB_HARD_FAIL) {
+ ASSERT_OK(err, "wait diagnostic");
+ result = TWB_HARD_FAIL;
+ } else {
+ fprintf(stderr,
+ "attempt %d: elapsed diagnostic unavailable: %d\n",
+ seq, err);
+ }
+ }
+ }
+ *out_wait_ns = wait_ns;
+ fprintf(stderr, "attempt %d: %s (window %u wait_ns %lld)\n",
+ seq, twb_attempt_name(result), (__u32)window, (long long)wait_ns);
+ return result;
+}
+
+void serial_test_task_work_busy(void)
+{
+ struct task_work_busy *skel;
+ struct bpf_link *link_tp = NULL, *link_fexit = NULL;
+ cpu_set_t allowed;
+ int cpu, cpu_count = 0, trigger_cpu = -1, target_cpu = -1;
+ int attempt, attempts_started = 0, valid = 0, missed = 0;
+ bool hard_fail = false;
+ __s64 max_wait_ns = 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)
+ trigger_cpu = cpu;
+ else
+ 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_busy__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ return;
+ if (skel->kconfig->CONFIG_PREEMPT_RT) {
+ printf("%s:SKIP:CONFIG_PREEMPT_RT=y runs irq_work in a kernel thread\n",
+ __func__);
+ test__skip();
+ goto cleanup;
+ }
+
+ link_tp = bpf_program__attach_tracepoint(skel->progs.busy_sched_work,
+ "syscalls",
+ "sys_enter_getppid");
+ if (!ASSERT_OK_PTR(link_tp, "attach tracepoint"))
+ goto cleanup;
+
+ link_fexit = bpf_program__attach_trace(skel->progs.busy_fexit_task_work_add);
+ if (!ASSERT_OK_PTR(link_fexit, "attach fexit"))
+ goto cleanup;
+
+ for (attempt = 1;
+ attempt <= TASK_WORK_BUSY_MAX_ATTEMPTS &&
+ valid < TASK_WORK_BUSY_ROUNDS;
+ attempt++) {
+ enum task_work_busy_attempt res;
+ __s64 wait_ns = 0;
+
+ attempts_started = attempt;
+ res = task_work_busy_run_attempt(skel, attempt, trigger_cpu,
+ target_cpu, &wait_ns);
+ switch (res) {
+ case TWB_VALID:
+ valid++;
+ if (wait_ns > max_wait_ns)
+ max_wait_ns = wait_ns;
+ break;
+ case TWB_MISSED:
+ missed++;
+ break;
+ case TWB_HARD_FAIL:
+ hard_fail = true;
+ break;
+ }
+ if (hard_fail)
+ break;
+ }
+
+ fprintf(stderr,
+ "task_work_busy: %d/%d valid, %d missed, %d attempts, max_wait %lld ns\n",
+ valid, TASK_WORK_BUSY_ROUNDS, missed, attempts_started,
+ (long long)max_wait_ns);
+
+ /*
+ * The coverage assertion only makes sense when no hard
+ * failure aborted the loop; after a hard failure keep the primary
+ * reason instead of deriving more assertion noise from it.
+ */
+ if (!hard_fail)
+ ASSERT_EQ(valid, TASK_WORK_BUSY_ROUNDS, "valid busy windows");
+cleanup:
+ bpf_link__destroy(link_tp);
+ bpf_link__destroy(link_fexit);
+ task_work_busy__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/task_work_busy.c b/tools/testing/selftests/bpf/progs/task_work_busy.c
new file mode 100644
index 0000000000000..9960011edadc5
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/task_work_busy.c
@@ -0,0 +1,325 @@
+// 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/bpf_core_read.h>
+
+char _license[] SEC("license") = "GPL";
+
+extern bool CONFIG_PREEMPT_RT __kconfig __weak;
+
+/*
+ * Bounds for the hardirq busy-wait that holds the scheduling handler
+ * inside the real window: task_work_add() has published the callback,
+ * but the handler's tail state transition has not run yet.
+ * irq_work_single() clears BUSY only after the handler returns, so
+ * while this wait runs, the context's scheduling irq_work is BUSY,
+ * even though the first callback may already have completed and
+ * returned the context to STANDBY.
+ *
+ * The wait is bounded both by a bpf_ktime_get_ns() check and by an
+ * iteration budget. The time bound is checked against a monotonic
+ * clock: host vCPU preemption can stretch the wall-clock duration of
+ * the window, which only makes the window longer, never shorter.
+ */
+#define BUSY_WAIT_NS 4000000ULL
+#define BUSY_WAIT_ITERS 1000000ULL
+
+int trigger_tid;
+int target_tid;
+
+/*
+ * High 32 bits of the result slots hold the sequence, low 32 bits the
+ * signed error. BUSY_WINDOW low bits: 1 open, 2 the in-window attempt
+ * was seen before the handler returned, 3 the wait bound was reached
+ * without it. Must match the test.
+ */
+enum task_work_busy_status {
+ BUSY_ARM_SEQ,
+ BUSY_WINDOW,
+ BUSY_ACK,
+ BUSY_GEN1_RESULT,
+ BUSY_GEN2_RESULT,
+ BUSY_CB1_SEQ,
+ BUSY_CB2_COUNT,
+ BUSY_CB2_SEQ,
+ BUSY_RECOVER_SEQ,
+ BUSY_LAST_WAIT_NS,
+ BUSY_STATUS_MAX,
+};
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARRAY);
+ __uint(max_entries, BUSY_STATUS_MAX);
+ __type(key, int);
+ __type(value, __s64);
+} status SEC(".maps");
+
+struct task_work_busy_value {
+ __u32 seq;
+ char data[60];
+ struct bpf_task_work tw;
+};
+
+/*
+ * max_entries must cover TASK_WORK_BUSY_MAX_ATTEMPTS in the test: each
+ * attempt schedules into its own fresh value.
+ */
+struct {
+ __uint(type, BPF_MAP_TYPE_HASH);
+ __uint(map_flags, BPF_F_NO_PREALLOC);
+ __uint(max_entries, 128);
+ __type(key, int);
+ __type(value, struct task_work_busy_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 __always_inline __s64 get_status(int key)
+{
+ __s64 *slot = bpf_map_lookup_elem(&status, &key);
+
+ return slot ? *slot : 0;
+}
+
+struct busy_wait_ctx {
+ __u64 seq;
+ __u64 start;
+ bool acked;
+};
+
+/* bpf_loop() callback: return 0 continues the loop, return 1 stops it. */
+static long busy_wait_cb(__u32 i, void *pctx)
+{
+ struct busy_wait_ctx *w = pctx;
+
+ if (((__u64)get_status(BUSY_ACK) >> 32) == w->seq) {
+ w->acked = true;
+ return 1;
+ }
+ if (bpf_ktime_get_ns() - w->start > BUSY_WAIT_NS)
+ return 1;
+ return 0;
+}
+
+static int busy_cb_gen1(struct bpf_map *map, void *key, void *value)
+{
+ struct task_work_busy_value *work = value;
+ __u64 we = (__u64)get_status(BUSY_WINDOW) >> 32;
+
+ /*
+ * Backstop for deliveries from abandoned attempts: accept only
+ * while no window for a different sequence is published. The
+ * test waits for the old workers to disappear from PID lookup,
+ * after their exit task_work, before resetting the shared state.
+ * This callback may run before fexit publishes the window state.
+ */
+ if (we && we != (__u64)work->seq)
+ return 0;
+ set_status(BUSY_CB1_SEQ, work->seq);
+ return 0;
+}
+
+static int busy_cb_gen2(struct bpf_map *map, void *key, void *value)
+{
+ struct task_work_busy_value *work = value;
+ __s64 *count;
+ int key2 = BUSY_CB2_COUNT;
+
+ /*
+ * Count only deliveries belonging to the sequence whose window is
+ * (or was) published: a late delivery from an abandoned attempt
+ * must not satisfy a later exactly-once check.
+ */
+ if ((__u64)work->seq != ((__u64)get_status(BUSY_WINDOW) >> 32))
+ return 0;
+ set_status(BUSY_CB2_SEQ, work->seq);
+ count = bpf_map_lookup_elem(&status, &key2);
+ if (count)
+ __sync_fetch_and_add(count, 1);
+ return 0;
+}
+
+SEC("fexit/task_work_add")
+int BPF_PROG(busy_fexit_task_work_add, struct task_struct *task,
+ struct callback_head *work, int notify, int retval)
+{
+ struct busy_wait_ctx w = {};
+ __u64 seq, elapsed;
+
+ seq = (__u64)get_status(BUSY_ARM_SEQ);
+ if (!seq)
+ return 0;
+
+ /*
+ * One-shot arm. Only this test's own first-round add may consume
+ * it and hold the window: a successful TWA_SIGNAL add of the
+ * bpf_task_work callback, issued from the trigger thread's CPU by
+ * the self-IPI'd scheduling handler and targeting the target
+ * thread. Everything else leaves the arm untouched.
+ */
+ if (retval != 0 || notify != TWA_SIGNAL ||
+ BPF_CORE_READ(task, pid) != target_tid)
+ return 0;
+ if ((__u32)bpf_get_current_pid_tgid() != (__u32)trigger_tid)
+ return 0;
+
+ set_status(BUSY_ARM_SEQ, 0);
+ set_status(BUSY_WINDOW, (seq << 32) | 1);
+
+ w.seq = seq;
+ w.start = bpf_ktime_get_ns();
+ bpf_loop(BUSY_WAIT_ITERS, busy_wait_cb, &w, 0);
+ elapsed = bpf_ktime_get_ns() - w.start;
+
+ /*
+ * Publish elapsed time for logging after worker cleanup. Window
+ * validity depends on seeing ACK here, not on this diagnostic.
+ */
+ set_status(BUSY_LAST_WAIT_NS, (__s64)elapsed);
+ if (w.acked)
+ set_status(BUSY_WINDOW, (seq << 32) | 2);
+ else
+ set_status(BUSY_WINDOW, (seq << 32) | 3);
+ return 0;
+}
+
+/*
+ * Attempt the second scheduling round on the same context, targeting the
+ * trigger thread. The attempt uses the value keyed by its own sequence,
+ * so it can never touch another attempt's context. Returns true if a
+ * terminal result was published into res_key; a transient -EBUSY is left
+ * unpublished for the caller's retry policy, mirroring the race test's
+ * convention.
+ */
+static __always_inline bool busy_try_gen2(__u64 seq, int res_key)
+{
+ struct task_work_busy_value *work;
+ struct task_struct *task;
+ int key = (int)seq, err;
+
+ task = bpf_task_from_pid(trigger_tid);
+ if (!task) {
+ set_status(res_key, ((__s64)seq << 32) | (__u32)-ESRCH);
+ return true;
+ }
+ work = bpf_map_lookup_elem(&hmap, &key);
+ if (!work) {
+ bpf_task_release(task);
+ set_status(res_key, ((__s64)seq << 32) | (__u32)-ENOENT);
+ return true;
+ }
+ work->seq = seq;
+ err = bpf_task_work_schedule_signal(task, &work->tw, &hmap,
+ busy_cb_gen2);
+ bpf_task_release(task);
+ if (err == -EBUSY)
+ return false;
+ set_status(res_key, ((__s64)seq << 32) | (__u32)err);
+ return true;
+}
+
+SEC("tracepoint/syscalls/sys_enter_getppid")
+int busy_sched_work(void *ctx)
+{
+ __u32 tid = (__u32)bpf_get_current_pid_tgid();
+ struct task_work_busy_value *work;
+ struct task_struct *task;
+ __u64 window, seq;
+ int key, err;
+
+ /* Also keep the kconfig extern in object BTF for the userspace skip. */
+ if (CONFIG_PREEMPT_RT)
+ return 0;
+
+ if (tid == (__u32)trigger_tid) {
+ /*
+ * Trigger thread: schedule the first round once per armed
+ * sequence, targeting the target thread. The kfunc queues
+ * the scheduling irq_work and returns; the handler then
+ * parks inside the fexit window above. A fresh value has no
+ * expected BUSY source, so publish every scheduling result.
+ */
+ seq = (__u64)get_status(BUSY_ARM_SEQ);
+ if (!seq ||
+ ((__u64)get_status(BUSY_GEN1_RESULT) >> 32) == seq)
+ return 0;
+ key = (int)seq;
+ task = bpf_task_from_pid(target_tid);
+ if (!task) {
+ set_status(BUSY_GEN1_RESULT,
+ ((__s64)seq << 32) | (__u32)-ESRCH);
+ set_status(BUSY_ARM_SEQ, 0);
+ return 0;
+ }
+ work = bpf_map_lookup_elem(&hmap, &key);
+ if (!work) {
+ bpf_task_release(task);
+ set_status(BUSY_GEN1_RESULT,
+ ((__s64)seq << 32) | (__u32)-ENOENT);
+ set_status(BUSY_ARM_SEQ, 0);
+ return 0;
+ }
+ work->seq = seq;
+ err = bpf_task_work_schedule_signal(task, &work->tw, &hmap,
+ busy_cb_gen1);
+ bpf_task_release(task);
+ set_status(BUSY_GEN1_RESULT, ((__s64)seq << 32) | (__u32)err);
+ return 0;
+ }
+
+ if (tid != (__u32)target_tid)
+ return 0;
+
+ window = (__u64)get_status(BUSY_WINDOW);
+ seq = window >> 32;
+ if (!seq || (__u32)window == 0)
+ return 0;
+ /*
+ * The first callback must have completed for this sequence. Being
+ * inside the target thread's own subsequent syscall proves the
+ * callback wrapper has returned and the context is STANDBY, which
+ * is stronger than observing the completion marker from another
+ * thread.
+ */
+ if ((__u64)get_status(BUSY_CB1_SEQ) != seq)
+ return 0;
+
+ if ((__u64)get_status(BUSY_ACK) >> 32 != seq) {
+ /*
+ * The in-window attempt, exactly once per sequence. On a
+ * fixed kernel it must be rejected with -EBUSY: the context
+ * is STANDBY, the map and program references are alive and
+ * no other scheduler exists, so only the BUSY gate can
+ * reject here.
+ */
+ if (!busy_try_gen2(seq, BUSY_ACK))
+ set_status(BUSY_ACK,
+ ((__s64)seq << 32) | (__u32)-EBUSY);
+ return 0;
+ }
+
+ /*
+ * Recovery, gated by the test: the fexit program releasing the
+ * window does not mean the whole old handler has returned and
+ * BUSY has cleared yet, so retry until the schedule succeeds.
+ */
+ if ((__u64)get_status(BUSY_RECOVER_SEQ) != seq)
+ return 0;
+ if ((__s32)get_status(BUSY_ACK) == 0)
+ return 0; /* scheduled in-window; nothing to recover */
+ if (((__u64)get_status(BUSY_GEN2_RESULT) >> 32) == seq)
+ return 0; /* terminal recovery result published */
+ busy_try_gen2(seq, BUSY_GEN2_RESULT);
+ return 0;
+}
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/10/12 06:35 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 18508 output: 37 thoughts: 574 Thoughts: **Analyzing the bpf_task_work Patch Series for Syzkaller** Okay, let's dive into this patch series for syzkaller fuzzing. My initial goal is a thorough evaluation of the changes. I need to understand exactly what's being altered and whether these modifications are sound and relevant for fuzzing. First, I'll carefully examine the patch diff itself. I see changes in `kernel/bpf/helpers.c` and new test cases within `tools/testing/selftests/bpf/`. My immediate focus will be the core changes in `kernel/bpf/helpers.c`. Specifically, I need to understand these key alterations: 1. `bpf_task_work_cancel`: The function now explicitly accepts a `task` argument. This seems like a potential fix for a race condition, making the task parameter explicit is usually a sign of a refinement in how the underlying data is being handled. I need to understand why this was necessary. And where the old task parameter was obtained. 2. `bpf_task_work_fetch_ctx`: The `init_irq_work` call has been moved. This smells like a subtle optimization or a refactoring to prevent redundant initialization. The implication here needs to be clear; the implications of calling `init_irq_work` from different contexts. I need to determine why this function was moved. 3. `bpf_task_work_acquire_ctx`: There's a new check here that's interesting: `irq_work_is_busy(&ctx->irq_work)`. This is a concurrency-related addition. This is good to prevent potential double-frees or other race conditions. I need to understand the implications of this new check and its impact on performance and correctness. 4. `bpf_task_work_schedule`: The removal of `init_irq_work` is directly related to the move in `bpf_task_work_fetch_ctx`. The comment here mentions a bug with re-initialization. I need to thoroughly understand what this bug was and how the move fixes it. 5. `bpf_task_work_cancel_scheduled`: Again, we see a call to `bpf_task_work_cancel`, which is good since it's now explicit. Now, beyond the specifics of the patch, I need to step back and get a broader context. I'll need to read around `kernel/bpf/helpers.c` to see how `bpf_task_work` functions within the broader BPF ecosystem. I want to understand: What other kfuncs/helpers expose this functionality? What are the relevant CONFIG options that influence `bpf_task_work`? Most importantly, I need to determine if this functionality is accessible and potentially useful for userspace fuzzing via syzkaller. Does the functionality have any surface exposure to userspace? If it's exposed, how is it protected? Are there any obvious security implications? I'll start with a `grep` through the file, and, potentially, if that is not exhaustive, a closer reading of the code. This is a critical step in assessing the impact and potential value of these changes for syzkaller's fuzzing efforts. |
| 4/2 | 2026/10/12 06:36 | tool | grepper |
0mArgs:{
"Expression": "bpf_task_work_schedule",
"PathPrefixes": [
"kernel/bpf/helpers.c"
]
}Results: {
"Output": "kernel/bpf/helpers.c=4571=static struct bpf_task_work_ctx *bpf_task_work_acquire_ctx(struct bpf_task_work *tw,\n--\nkernel/bpf/helpers.c-4627-\nkernel/bpf/helpers.c:4628:static int bpf_task_work_schedule(struct task_struct *task, struct bpf_task_work *tw,\nkernel/bpf/helpers.c-4629-\t\t\t\t struct bpf_map *map, void *callback_fn,\n--\nkernel/bpf/helpers.c-4671-/**\nkernel/bpf/helpers.c:4672: * bpf_task_work_schedule_signal - Schedule BPF callback using task_work_add with TWA_SIGNAL\nkernel/bpf/helpers.c-4673- * mode\n--\nkernel/bpf/helpers.c-4681- */\nkernel/bpf/helpers.c:4682:__bpf_kfunc int bpf_task_work_schedule_signal(struct task_struct *task, struct bpf_task_work *tw,\nkernel/bpf/helpers.c-4683-\t\t\t\t\t void *map__const_map, bpf_task_work_callback_t callback,\n--\nkernel/bpf/helpers.c-4685-{\nkernel/bpf/helpers.c:4686:\treturn bpf_task_work_schedule(task, tw, map__const_map, callback, aux, TWA_SIGNAL);\nkernel/bpf/helpers.c-4687-}\n--\nkernel/bpf/helpers.c-4689-/**\nkernel/bpf/helpers.c:4690: * bpf_task_work_schedule_resume - Schedule BPF callback using task_work_add with TWA_RESUME\nkernel/bpf/helpers.c-4691- * mode\n--\nkernel/bpf/helpers.c-4699- */\nkernel/bpf/helpers.c:4700:__bpf_kfunc int bpf_task_work_schedule_resume(struct task_struct *task, struct bpf_task_work *tw,\nkernel/bpf/helpers.c-4701-\t\t\t\t\t void *map__const_map, bpf_task_work_callback_t callback,\n--\nkernel/bpf/helpers.c-4703-{\nkernel/bpf/helpers.c:4704:\treturn bpf_task_work_schedule(task, tw, map__const_map, callback, aux, TWA_RESUME);\nkernel/bpf/helpers.c-4705-}\n--\nkernel/bpf/helpers.c=5003=BTF_ID_FLAGS(func, bpf_stream_print_stack, KF_IMPLICIT_ARGS | KF_SPINLOCK_SAFE)\nkernel/bpf/helpers.c:5004:BTF_ID_FLAGS(func, bpf_task_work_schedule_signal, KF_IMPLICIT_ARGS)\nkernel/bpf/helpers.c:5005:BTF_ID_FLAGS(func, bpf_task_work_schedule_resume, KF_IMPLICIT_ARGS)\nkernel/bpf/helpers.c-5006-BTF_ID_FLAGS(func, bpf_dynptr_from_file)\n"
} |
| 5/2 | 2026/10/12 06:36 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 20657 output: 37 thoughts: 34 Thoughts: Here's my analysis of the provided text, rendered in the first person, assuming an expert user: **Deep Dive into BPF Helper Function Implementation** Alright, let's get into the nitty-gritty of how this BPF helper mechanism is actually implemented. My first instinct is to dive directly into the source code, specifically `kernel/bpf/helpers.c`. I'm going to focus my attention around lines 4420 to 4820. That seems like a reasonable chunk to encompass the core logic, or at least a significant portion, of the implementation we're interested in. I'm expecting to find the key function calls, data structures, and the flow of execution within this section that handles the helper functions. Hopefully, this will clearly lay out the interaction with userspace BPF programs and the kernel itself. I will be looking for the crucial pieces. |
| 6/2 | 2026/10/12 06:36 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/helpers.c",
"FirstLine": 4420,
"LineCount": 80
}Results: {
"Contents": "4420:\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\t * The caller passes the task explicitly: the scheduling handler must\n4450:\t\t * not read ctx-\u003etask after the work is published (the callback may\n4451:\t\t * have reset it), while in the asynchronous cancellation path\n4452:\t\t * ctx-\u003etask is stable, see bpf_task_work_cancel_scheduled().\n4453:\t\t */\n4454:\t\tif (task_work_cancel(task, \u0026ctx-\u003ework))\n4455:\t\t\tbpf_task_work_ctx_put(ctx);\n4456:\t}\n4457:\t\n4458:\tstatic void bpf_task_work_callback(struct callback_head *cb)\n4459:\t{\n4460:\t\tstruct bpf_task_work_ctx *ctx = container_of(cb, struct bpf_task_work_ctx, work);\n4461:\t\tenum bpf_task_work_state state;\n4462:\t\tu32 idx;\n4463:\t\tvoid *key;\n4464:\t\n4465:\t\t/* Read lock is needed to protect ctx and map key/value access */\n4466:\t\tguard(rcu_tasks_trace)();\n4467:\t\t/*\n4468:\t\t * This callback may start running before bpf_task_work_irq() switched to\n4469:\t\t * SCHEDULED state, so handle both transition variants SCHEDULING|SCHEDULED -\u003e RUNNING.\n4470:\t\t */\n4471:\t\tstate = cmpxchg(\u0026ctx-\u003estate, BPF_TW_SCHEDULING, BPF_TW_RUNNING);\n4472:\t\tif (state == BPF_TW_SCHEDULED)\n4473:\t\t\tstate = cmpxchg(\u0026ctx-\u003estate, BPF_TW_SCHEDULED, BPF_TW_RUNNING);\n4474:\t\tif (state == BPF_TW_FREED) {\n4475:\t\t\tbpf_task_work_ctx_put(ctx);\n4476:\t\t\treturn;\n4477:\t\t}\n4478:\t\n4479:\t\tkey = (void *)map_key_from_value(ctx-\u003emap, ctx-\u003emap_val, \u0026idx);\n4480:\t\n4481:\t\tmigrate_disable();\n4482:\t\tctx-\u003ecallback_fn((u64)(long)ctx-\u003emap, (u64)(long)key,\n4483:\t\t\t\t (u64)(long)ctx-\u003emap_val, 0, 0);\n4484:\t\tmigrate_enable();\n4485:\t\n4486:\t\tbpf_task_work_ctx_reset(ctx);\n4487:\t\t(void)cmpxchg(\u0026ctx-\u003estate, BPF_TW_RUNNING, BPF_TW_STANDBY);\n4488:\t\n4489:\t\tbpf_task_work_ctx_put(ctx);\n4490:\t}\n4491:\t\n4492:\tstatic void bpf_task_work_irq(struct irq_work *irq_work)\n4493:\t{\n4494:\t\tstruct bpf_task_work_ctx *ctx = container_of(irq_work, struct bpf_task_work_ctx, irq_work);\n4495:\t\tstruct task_struct *task;\n4496:\t\tenum bpf_task_work_state state;\n4497:\t\tint err;\n4498:\t\n4499:\t\tguard(rcu)();\n"
} |
| 7/2 | 2026/10/12 06:36 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 22292 output: 38 thoughts: 17 Thoughts: Okay, here's my attempt at summarizing the thought process, tailored for an expert audience, and written in the first person: **Diving into `kernel/bpf/helpers.c`** Alright, time to dig deeper. I need to understand this behavior, and the best place to start, given the context, is definitely within the BPF helper implementation. So, my focus is now shifting to `kernel/bpf/helpers.c`. I expect to find the core logic for the BPF helper functions here. This is where the magic happens, where the user-space calls translate into kernel-level actions. I'm looking for the definitions, the implementations of those functions, especially those relevant to [mention the specific topic, if known from context, otherwise keep it general, e.g., "memory access," or "tracing"]. I'm mentally parsing the structure; function prototypes, any internal helper functions called within. I'll be looking for potential performance bottlenecks, any subtle race conditions, or anything else that might have implications for the current challenge. I'll pay attention to any use of locks, atomic operations, and how the functions interact with different kernel subsystems. I'll need to look for calls to other kernel functions that might be relevant, and track those down, if relevant. Time to get my hands dirty. |
| 8/2 | 2026/10/12 06:37 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/helpers.c",
"FirstLine": 4500,
"LineCount": 100
}Results: {
"Contents": "4500:\t\n4501:\t\tif (cmpxchg(\u0026ctx-\u003estate, BPF_TW_PENDING, BPF_TW_SCHEDULING) != BPF_TW_PENDING) {\n4502:\t\t\tbpf_task_work_ctx_put(ctx);\n4503:\t\t\treturn;\n4504:\t\t}\n4505:\t\n4506:\t\t/*\n4507:\t\t * Capture the task pointer before the work is published: once the\n4508:\t\t * callback runs, it may reset ctx-\u003etask. The capture happens while\n4509:\t\t * this round's task reference is held, and every path that can\n4510:\t\t * release it runs after this handler entered its rcu read-side\n4511:\t\t * section. bpf_task_release() is put_task_struct_rcu_user(), so the\n4512:\t\t * task_struct itself cannot be freed before this section ends.\n4513:\t\t */\n4514:\t\ttask = ctx-\u003etask;\n4515:\t\terr = task_work_add(task, \u0026ctx-\u003ework, ctx-\u003emode);\n4516:\t\tif (err) {\n4517:\t\t\tbpf_task_work_ctx_reset(ctx);\n4518:\t\t\t/*\n4519:\t\t\t * try to switch back to STANDBY for another task_work reuse, but we might have\n4520:\t\t\t * gone to FREED already, which is fine as we already cleaned up after ourselves\n4521:\t\t\t */\n4522:\t\t\t(void)cmpxchg(\u0026ctx-\u003estate, BPF_TW_SCHEDULING, BPF_TW_STANDBY);\n4523:\t\t\tbpf_task_work_ctx_put(ctx);\n4524:\t\t\treturn;\n4525:\t\t}\n4526:\t\n4527:\t\t/*\n4528:\t\t * It's technically possible for just scheduled task_work callback to\n4529:\t\t * complete running by now, going SCHEDULING -\u003e RUNNING, resetting\n4530:\t\t * ctx-\u003etask and dropping its ctx refcount. The rcu read-side section\n4531:\t\t * above keeps both the ctx memory and the captured task pointer\n4532:\t\t * valid until this handler returns.\n4533:\t\t */\n4534:\t\tstate = cmpxchg(\u0026ctx-\u003estate, BPF_TW_SCHEDULING, BPF_TW_SCHEDULED);\n4535:\t\tif (state == BPF_TW_FREED)\n4536:\t\t\tbpf_task_work_cancel(ctx, task); /* clean up if we switched into FREED state */\n4537:\t}\n4538:\t\n4539:\tstatic struct bpf_task_work_ctx *bpf_task_work_fetch_ctx(struct bpf_task_work *tw,\n4540:\t\t\t\t\t\t\t\t struct bpf_map *map)\n4541:\t{\n4542:\t\tstruct bpf_task_work_kern *twk = (void *)tw;\n4543:\t\tstruct bpf_task_work_ctx *ctx, *old_ctx;\n4544:\t\n4545:\t\tctx = READ_ONCE(twk-\u003ectx);\n4546:\t\tif (ctx)\n4547:\t\t\treturn ctx;\n4548:\t\n4549:\t\tctx = bpf_map_kmalloc_nolock(map, sizeof(*ctx), 0, NUMA_NO_NODE);\n4550:\t\tif (!ctx)\n4551:\t\t\treturn ERR_PTR(-ENOMEM);\n4552:\t\n4553:\t\tmemset(ctx, 0, sizeof(*ctx));\n4554:\t\trefcount_set(\u0026ctx-\u003erefcnt, 1); /* map's own ref */\n4555:\t\tctx-\u003estate = BPF_TW_STANDBY;\n4556:\t\tinit_irq_work(\u0026ctx-\u003eirq_work, bpf_task_work_irq);\n4557:\t\n4558:\t\told_ctx = cmpxchg(\u0026twk-\u003ectx, NULL, ctx);\n4559:\t\tif (old_ctx) {\n4560:\t\t\t/*\n4561:\t\t\t * tw-\u003ectx is set by concurrent BPF program, release allocated\n4562:\t\t\t * memory and try to reuse already set context.\n4563:\t\t\t */\n4564:\t\t\tkfree_nolock(ctx);\n4565:\t\t\treturn old_ctx;\n4566:\t\t}\n4567:\t\n4568:\t\treturn ctx; /* Success */\n4569:\t}\n4570:\t\n4571:\tstatic struct bpf_task_work_ctx *bpf_task_work_acquire_ctx(struct bpf_task_work *tw,\n4572:\t\t\t\t\t\t\t\t struct bpf_map *map)\n4573:\t{\n4574:\t\tstruct bpf_task_work_ctx *ctx;\n4575:\t\n4576:\t\t/*\n4577:\t\t * Sleepable BPF programs hold rcu_read_lock_trace but not\n4578:\t\t * regular rcu_read_lock. Since kfree_rcu waits for regular\n4579:\t\t * RCU GP, the ctx can be freed while we're between reading\n4580:\t\t * the pointer and incrementing the refcount. Take regular\n4581:\t\t * rcu_read_lock to prevent kfree_rcu from freeing the ctx\n4582:\t\t * before we can tryget it.\n4583:\t\t */\n4584:\t\tscoped_guard(rcu) {\n4585:\t\t\tctx = bpf_task_work_fetch_ctx(tw, map);\n4586:\t\t\tif (IS_ERR(ctx))\n4587:\t\t\t\treturn ctx;\n4588:\t\n4589:\t\t\t/* try to get ref for task_work callback to hold */\n4590:\t\t\tif (!bpf_task_work_ctx_tryget(ctx))\n4591:\t\t\t\treturn ERR_PTR(-EBUSY);\n4592:\t\t}\n4593:\t\n4594:\t\tif (cmpxchg(\u0026ctx-\u003estate, BPF_TW_STANDBY, BPF_TW_PENDING) != BPF_TW_STANDBY) {\n4595:\t\t\t/* lost acquiring race or map_release_uref() stole it from us, put ref and bail */\n4596:\t\t\tbpf_task_work_ctx_put(ctx);\n4597:\t\t\treturn ERR_PTR(-EBUSY);\n4598:\t\t}\n4599:\t\t/*\n"
} |
| 9/2 | 2026/10/12 06:37 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 24330 output: 38 thoughts: 18 Thoughts: Okay, here's the summary, formatted as requested: **Delving Deeper: Lines 4600-4750** Alright, let's zero in on lines 4600-4750. I need to get a better handle on the specific arguments presented within that section, and to see if the arguments match my expectations. I want to see if this supports my prior suppositions or, perhaps, if it subtly contradicts. I need to refresh my mind regarding the context. |
| 10/2 | 2026/10/12 06:37 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/helpers.c",
"FirstLine": 4600,
"LineCount": 100
}Results: {
"Contents": "4600:\t\t * STANDBY can be published before the previous scheduling irq_work\n4601:\t\t * returns. Claim PENDING before checking BUSY to order the check after\n4602:\t\t * that publication. No irq_work has been queued for this attempt yet,\n4603:\t\t * so reject reuse and roll back to STANDBY while BUSY is set. Preserve\n4604:\t\t * FREED if deletion wins the rollback race.\n4605:\t\t */\n4606:\t\tif (unlikely(irq_work_is_busy(\u0026ctx-\u003eirq_work))) {\n4607:\t\t\t(void)cmpxchg(\u0026ctx-\u003estate, BPF_TW_PENDING, BPF_TW_STANDBY);\n4608:\t\t\tbpf_task_work_ctx_put(ctx);\n4609:\t\t\treturn ERR_PTR(-EBUSY);\n4610:\t\t}\n4611:\t\n4612:\t\t/*\n4613:\t\t * If no process or bpffs is holding a reference to the map, no new callbacks should be\n4614:\t\t * scheduled. This does not address any race or correctness issue, but rather is a policy\n4615:\t\t * choice: dropping user references should stop everything.\n4616:\t\t */\n4617:\t\tif (!atomic64_read(\u0026map-\u003eusercnt)) {\n4618:\t\t\t/* drop ref we just got for task_work callback itself */\n4619:\t\t\tbpf_task_work_ctx_put(ctx);\n4620:\t\t\t/* transfer map's ref into cancel_and_free() */\n4621:\t\t\tbpf_task_work_cancel_and_free(tw);\n4622:\t\t\treturn ERR_PTR(-EBUSY);\n4623:\t\t}\n4624:\t\n4625:\t\treturn ctx;\n4626:\t}\n4627:\t\n4628:\tstatic int bpf_task_work_schedule(struct task_struct *task, struct bpf_task_work *tw,\n4629:\t\t\t\t\t struct bpf_map *map, void *callback_fn,\n4630:\t\t\t\t\t struct bpf_prog_aux *aux, enum task_work_notify_mode mode)\n4631:\t{\n4632:\t\tstruct bpf_prog *prog;\n4633:\t\tstruct bpf_task_work_ctx *ctx;\n4634:\t\tint err;\n4635:\t\n4636:\t\tBTF_TYPE_EMIT(struct bpf_task_work);\n4637:\t\n4638:\t\tprog = bpf_prog_inc_not_zero(aux-\u003eprog);\n4639:\t\tif (IS_ERR(prog))\n4640:\t\t\treturn -EBADF;\n4641:\t\ttask = bpf_task_acquire(task);\n4642:\t\tif (!task) {\n4643:\t\t\terr = -EBADF;\n4644:\t\t\tgoto release_prog;\n4645:\t\t}\n4646:\t\n4647:\t\tctx = bpf_task_work_acquire_ctx(tw, map);\n4648:\t\tif (IS_ERR(ctx)) {\n4649:\t\t\terr = PTR_ERR(ctx);\n4650:\t\t\tgoto release_all;\n4651:\t\t}\n4652:\t\n4653:\t\tctx-\u003etask = task;\n4654:\t\tctx-\u003ecallback_fn = (bpf_callback_t)callback_fn;\n4655:\t\tctx-\u003eprog = prog;\n4656:\t\tctx-\u003emode = mode;\n4657:\t\tctx-\u003emap = map;\n4658:\t\tctx-\u003emap_val = (void *)tw - map-\u003erecord-\u003etask_work_off;\n4659:\t\tinit_task_work(\u0026ctx-\u003ework, bpf_task_work_callback);\n4660:\t\n4661:\t\tirq_work_queue(\u0026ctx-\u003eirq_work);\n4662:\t\treturn 0;\n4663:\t\n4664:\trelease_all:\n4665:\t\tbpf_task_release(task);\n4666:\trelease_prog:\n4667:\t\tbpf_prog_put(prog);\n4668:\t\treturn err;\n4669:\t}\n4670:\t\n4671:\t/**\n4672:\t * bpf_task_work_schedule_signal - Schedule BPF callback using task_work_add with TWA_SIGNAL\n4673:\t * mode\n4674:\t * @task: Task struct for which callback should be scheduled\n4675:\t * @tw: Pointer to struct bpf_task_work in BPF map value for internal bookkeeping\n4676:\t * @map__const_map: bpf_map that embeds struct bpf_task_work in the values\n4677:\t * @callback: pointer to BPF subprogram to call\n4678:\t * @aux: pointer to bpf_prog_aux of the caller BPF program, implicitly set by the verifier\n4679:\t *\n4680:\t * Return: 0 if task work has been scheduled successfully, negative error code otherwise\n4681:\t */\n4682:\t__bpf_kfunc int bpf_task_work_schedule_signal(struct task_struct *task, struct bpf_task_work *tw,\n4683:\t\t\t\t\t\t void *map__const_map, bpf_task_work_callback_t callback,\n4684:\t\t\t\t\t\t struct bpf_prog_aux *aux)\n4685:\t{\n4686:\t\treturn bpf_task_work_schedule(task, tw, map__const_map, callback, aux, TWA_SIGNAL);\n4687:\t}\n4688:\t\n4689:\t/**\n4690:\t * bpf_task_work_schedule_resume - Schedule BPF callback using task_work_add with TWA_RESUME\n4691:\t * mode\n4692:\t * @task: Task struct for which callback should be scheduled\n4693:\t * @tw: Pointer to struct bpf_task_work in BPF map value for internal bookkeeping\n4694:\t * @map__const_map: bpf_map that embeds struct bpf_task_work in the values\n4695:\t * @callback: pointer to BPF subprogram to call\n4696:\t * @aux: pointer to bpf_prog_aux of the caller BPF program, implicitly set by the verifier\n4697:\t *\n4698:\t * Return: 0 if task work has been scheduled successfully, negative error code otherwise\n4699:\t */\n"
} |
| 11/2 | 2026/10/12 06:37 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 26208 output: 39 thoughts: 24 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Analyzing the Codebase History** Alright, my first instinct when tackling this is to dive into the commit history. I need to understand the evolution of this particular section of code. So, let me quickly check for a `commit log` or, you know, a standard `git log` command within the project. It's often the quickest way to get a high-level overview of recent changes and identify any relevant commits. Ideally, I want to see the lineage of the specific files or functions I'm interested in, to grasp the context of the current state. Starting with the `HEAD` commit is usually a good bet, as it gives the most immediate perspective on what's changed most recently. This initial analysis will give me a base to dive into if anything specific stands out. |
| 12/2 | 2026/10/12 06:37 | tool | grepper |
0mArgs:{
"Expression": "bpf_task_work_cancel_scheduled",
"PathPrefixes": [
"kernel/bpf/helpers.c"
]
}Results: {
"Output": "kernel/bpf/helpers.c=4440=static void bpf_task_work_cancel(struct bpf_task_work_ctx *ctx,\n--\nkernel/bpf/helpers.c-4451-\t * have reset it), while in the asynchronous cancellation path\nkernel/bpf/helpers.c:4452:\t * ctx-\u003etask is stable, see bpf_task_work_cancel_scheduled().\nkernel/bpf/helpers.c-4453-\t */\n--\nkernel/bpf/helpers.c=4802=__bpf_kfunc_end_defs();\nkernel/bpf/helpers.c-4803-\nkernel/bpf/helpers.c:4804:static void bpf_task_work_cancel_scheduled(struct irq_work *irq_work)\nkernel/bpf/helpers.c-4805-{\n--\nkernel/bpf/helpers.c=4818=void bpf_task_work_cancel_and_free(void *val)\n--\nkernel/bpf/helpers.c-4830-\t\t/* run in irq_work to avoid locks in NMI */\nkernel/bpf/helpers.c:4831:\t\tinit_irq_work(\u0026ctx-\u003eirq_work, bpf_task_work_cancel_scheduled);\nkernel/bpf/helpers.c-4832-\t\tirq_work_queue(\u0026ctx-\u003eirq_work);\n"
} |
| 13/2 | 2026/10/12 06:37 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 26818 output: 36 thoughts: 146 Thoughts: **Analyzing BPF Task Work Scheduling** Okay, let's dive into these `bpf_task_work_schedule_signal` and `bpf_task_work_schedule_resume` functions. My immediate focus is on understanding their registration and usage within the BPF framework. Right, they're defined as BPF kfuncs, indicated by that `__bpf_kfunc` designation. Now, the key is to pinpoint where these kfuncs are actually integrated into the system. A quick scan reveals their registration on lines 5004-5005, using `BTF_ID_FLAGS`: `BTF_ID_FLAGS(func, bpf_task_work_schedule_signal, KF_IMPLICIT_ARGS)` and `BTF_ID_FLAGS(func, bpf_task_work_schedule_resume, KF_IMPLICIT_ARGS)`. Alright, so `BTF_ID_FLAGS` is how they're exposed. Now, I need to know in which BTF set they belong, to understand how they are exposed to the BPF programs. That's the next step. |
| 14/2 | 2026/10/12 06:37 | tool | grepper |
0mArgs:{
"Expression": "bpf_task_work_schedule_signal",
"PathPrefixes": [
"kernel/bpf/"
]
}Results: {
"Output": "kernel/bpf/helpers.c=4628=static int bpf_task_work_schedule(struct task_struct *task, struct bpf_task_work *tw,\n--\nkernel/bpf/helpers.c-4671-/**\nkernel/bpf/helpers.c:4672: * bpf_task_work_schedule_signal - Schedule BPF callback using task_work_add with TWA_SIGNAL\nkernel/bpf/helpers.c-4673- * mode\n--\nkernel/bpf/helpers.c-4681- */\nkernel/bpf/helpers.c:4682:__bpf_kfunc int bpf_task_work_schedule_signal(struct task_struct *task, struct bpf_task_work *tw,\nkernel/bpf/helpers.c-4683-\t\t\t\t\t void *map__const_map, bpf_task_work_callback_t callback,\n--\nkernel/bpf/helpers.c=5003=BTF_ID_FLAGS(func, bpf_stream_print_stack, KF_IMPLICIT_ARGS | KF_SPINLOCK_SAFE)\nkernel/bpf/helpers.c:5004:BTF_ID_FLAGS(func, bpf_task_work_schedule_signal, KF_IMPLICIT_ARGS)\nkernel/bpf/helpers.c-5005-BTF_ID_FLAGS(func, bpf_task_work_schedule_resume, KF_IMPLICIT_ARGS)\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,\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)\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];\n"
} |
| 15/2 | 2026/10/12 06:37 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 27968 output: 37 thoughts: 25 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience and presented in the first person: **Decoding the BTF Kfunc Puzzle** Alright, so I'm digging into this BTF kfunc set – a good starting point is always to pinpoint its origin within the kernel. I'm immediately thinking, "Where is this thing defined? Let's trace it back to `kernel/bpf/helpers.c`. That's the likely spot, the central hub for a lot of those core BPF helper functions, so let's start there." I want to understand what context its part of and that file is likely to give me the most complete understanding. Time to dig in and unravel this kfunc set. |
| 16/2 | 2026/10/12 06:38 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/helpers.c",
"FirstLine": 4995,
"LineCount": 50
}Results: {
"Contents": "4995:\tBTF_ID_FLAGS(func, bpf_strstr);\n4996:\tBTF_ID_FLAGS(func, bpf_strcasestr);\n4997:\tBTF_ID_FLAGS(func, bpf_strnstr);\n4998:\tBTF_ID_FLAGS(func, bpf_strncasestr);\n4999:\t#if defined(CONFIG_BPF_LSM) \u0026\u0026 defined(CONFIG_CGROUPS)\n5000:\tBTF_ID_FLAGS(func, bpf_cgroup_read_xattr, KF_RCU)\n5001:\t#endif\n5002:\tBTF_ID_FLAGS(func, bpf_stream_vprintk, KF_IMPLICIT_ARGS | KF_SPINLOCK_SAFE)\n5003:\tBTF_ID_FLAGS(func, bpf_stream_print_stack, KF_IMPLICIT_ARGS | KF_SPINLOCK_SAFE)\n5004:\tBTF_ID_FLAGS(func, bpf_task_work_schedule_signal, KF_IMPLICIT_ARGS)\n5005:\tBTF_ID_FLAGS(func, bpf_task_work_schedule_resume, KF_IMPLICIT_ARGS)\n5006:\tBTF_ID_FLAGS(func, bpf_dynptr_from_file)\n5007:\tBTF_ID_FLAGS(func, bpf_dynptr_file_discard, KF_RELEASE)\n5008:\tBTF_ID_FLAGS(func, bpf_timer_cancel_async)\n5009:\tBTF_KFUNCS_END(common_btf_ids)\n5010:\t\n5011:\tstatic const struct btf_kfunc_id_set common_kfunc_set = {\n5012:\t\t.owner = THIS_MODULE,\n5013:\t\t.set = \u0026common_btf_ids,\n5014:\t};\n5015:\t\n5016:\tstatic int __init kfunc_init(void)\n5017:\t{\n5018:\t\tint ret;\n5019:\t\tconst struct btf_id_dtor_kfunc generic_dtors[] = {\n5020:\t\t\t{\n5021:\t\t\t\t.btf_id = generic_dtor_ids[0],\n5022:\t\t\t\t.kfunc_btf_id = generic_dtor_ids[1]\n5023:\t\t\t},\n5024:\t#ifdef CONFIG_CGROUPS\n5025:\t\t\t{\n5026:\t\t\t\t.btf_id = generic_dtor_ids[2],\n5027:\t\t\t\t.kfunc_btf_id = generic_dtor_ids[3]\n5028:\t\t\t},\n5029:\t#endif\n5030:\t\t};\n5031:\t\n5032:\t\tret = register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, \u0026generic_kfunc_set);\n5033:\t\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, \u0026generic_kfunc_set);\n5034:\t\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, \u0026generic_kfunc_set);\n5035:\t\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, \u0026generic_kfunc_set);\n5036:\t\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, \u0026generic_kfunc_set);\n5037:\t\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SKB, \u0026generic_kfunc_set);\n5038:\t\tret = ret ?: register_btf_id_dtor_kfuncs(generic_dtors,\n5039:\t\t\t\t\t\t\t ARRAY_SIZE(generic_dtors),\n5040:\t\t\t\t\t\t\t THIS_MODULE);\n5041:\t\treturn ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, \u0026common_kfunc_set);\n5042:\t}\n5043:\t\n5044:\tlate_initcall(kfunc_init);\n"
} |
| 17/2 | 2026/10/12 06:38 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 29312 output: 38 thoughts: 1062 Thoughts: **Analysis of BPF Task Work Patch for Fuzzing Suitability** Okay, so `common_kfunc_set` is registered with `BPF_PROG_TYPE_UNSPEC`, meaning it's available across BPF programs. That's immediately interesting from a fuzzing perspective, because it means BPF kfuncs, such as `bpf_task_work_schedule_signal` and `bpf_task_work_schedule_resume`, are accessible from the BPF programs themselves. This patch touches `kernel/bpf/helpers.c`, and BPF is definitely a core subsystem according to the criteria. Let's see what this patch is actually changing. The patch appears to be addressing race conditions within the `bpf_task_work` implementation, specifically in `bpf_task_work_irq`, `bpf_task_work_acquire_ctx`, `bpf_task_work_fetch_ctx`, and `bpf_task_work_cancel`. The fixes seem to center around preventing the `ctx->task` pointer from being accessed after the callback modifies it, and preventing the reuse of a context when it's still busy. This is reachable kernel code, and it's within a core subsystem (BPF). Therefore, *WorthFuzzing* has to be `true`. The goal of patch fuzzing is to find crashes, regressions, latent bugs, and new assertions introduced by the change. This definitely applies here. Now, I need to figure out the *FocusSymbols*, *EnableConfigs*, and the reasoning for this evaluation. Let's carefully consider the *FocusSymbols*. The instructions say to target specific logic or entrypoints and avoid ubiquitous functions. The patch modifies several functions in `kernel/bpf/helpers.c`: `bpf_task_work_cancel` (static), `bpf_task_work_irq` (static, callback), `bpf_task_work_fetch_ctx` (static), `bpf_task_work_acquire_ctx` (static), `bpf_task_work_schedule` (static), and `bpf_task_work_cancel_scheduled` (static, callback). The compiler *might* inline the static functions, so listing those directly might not be effective. So, how to focus the fuzzer on the relevant entrypoints. The non-static entrypoints that call the static helpers are: * `bpf_task_work_schedule_signal` and `bpf_task_work_schedule_resume` * `bpf_task_work_cancel_and_free(void *val)` (used in map free / value destructor) So, these functions should be in the FocusSymbols list because they are the exposed entrypoints and callers to the patched code. I need to confirm if that last entrypoint is the correct one. |
| 18/2 | 2026/10/12 06:38 | tool | grepper |
0mArgs:{
"Expression": "bpf_task_work_cancel_and_free",
"PathPrefixes": [
"kernel/bpf/"
]
}Results: {
"Output": "kernel/bpf/helpers.c=4571=static struct bpf_task_work_ctx *bpf_task_work_acquire_ctx(struct bpf_task_work *tw,\n--\nkernel/bpf/helpers.c-4620-\t\t/* transfer map's ref into cancel_and_free() */\nkernel/bpf/helpers.c:4621:\t\tbpf_task_work_cancel_and_free(tw);\nkernel/bpf/helpers.c-4622-\t\treturn ERR_PTR(-EBUSY);\n--\nkernel/bpf/helpers.c=4804=static void bpf_task_work_cancel_scheduled(struct irq_work *irq_work)\n--\nkernel/bpf/helpers.c-4817-\nkernel/bpf/helpers.c:4818:void bpf_task_work_cancel_and_free(void *val)\nkernel/bpf/helpers.c-4819-{\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"
} |
| 19/2 | 2026/10/12 06:38 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 31506 output: 29 thoughts: 671 Thoughts: **Analyzing BPF Task Work Symbols for Focus and Configuration** Okay, let's break this down. My goal is to refine the selection of functions related to BPF task work for a specific analysis. First, I see `bpf_task_work_cancel_and_free` listed as a global function, residing in `kernel/bpf/helpers.c`. But what about the other related functions? I'm noting that `bpf_task_work_irq` is a static function, which would usually be a red flag for inlining, but the fact that its address is taken by `init_irq_work`, along with `bpf_task_work_cancel_scheduled` and `bpf_task_work_callback`, means the compiler can't easily inline them. Same goes for `bpf_task_work_acquire_ctx` and `bpf_task_work_schedule`. And then there are the entry points: `bpf_task_work_schedule_signal`, `bpf_task_work_schedule_resume`, and `bpf_task_work_cancel_and_free`. Now, I'm checking which symbols were directly involved in the diff: `bpf_task_work_cancel`, `bpf_task_work_irq`, `bpf_task_work_fetch_ctx`, `bpf_task_work_acquire_ctx`, `bpf_task_work_schedule`, and `bpf_task_work_cancel_scheduled`. And directly, the kfunc entrypoints `bpf_task_work_schedule_signal`, `bpf_task_work_schedule_resume`, and `bpf_task_work_cancel_and_free`. I'm thinking, could all of these, or even a subset, be what I'm looking for in my `FocusSymbols`? Let's narrow it down. I think the more specific functions are better targets. So I'll go with: `bpf_task_work_schedule_signal`, `bpf_task_work_schedule_resume`, `bpf_task_work_irq`, `bpf_task_work_acquire_ctx`, `bpf_task_work_cancel_scheduled`, and `bpf_task_work_cancel_and_free`. I need to ensure they are the right level of specificity. Wait, are any of them super common functions across the kernel? No! They are all quite specific to this recent BPF task work mechanism, which is good. Finally, I'll need to check the required `CONFIG` flags. What flags are needed for all the `bpf_task_work_*` functions? I'll need to see where `kernel/bpf/helpers.c` is compiled and will check `kernel/bpf/Makefile`. |
| 20/2 | 2026/10/12 06:38 | tool | grepper |
0mArgs:{
"Expression": "helpers\\.o",
"PathPrefixes": [
"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"
} |
| 21/2 | 2026/10/12 06:38 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 32984 output: 35 thoughts: 81 Thoughts: Here's my thought process, as it stands: **BPF Helper Function Verification** Okay, so `kernel/bpf/helpers.o` is definitely built only if `CONFIG_BPF_SYSCALL` is enabled. That's a given. Now, I need to confirm something related to the `bpf_task_work_*` functions. I have a suspicion there might be some conditional compilation (`#ifdef`) going on around those functions within `kernel/bpf/helpers.c`. I need to check to see if the availability of these helpers is tied to a specific configuration option. I'm thinking that area of interest for this should be around line 4400-4850 of `kernel/bpf/helpers.c`. Let me dive in and have a look. |
| 22/2 | 2026/10/12 06:38 | tool | grepper |
0mError:no matches Args: {
"Expression": "#if.*task_work",
"PathPrefixes": [
"kernel/bpf/helpers.c"
]
}Results: {
"error": "no matches"
} |
| 23/2 | 2026/10/12 06:38 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 33287 output: 37 thoughts: 26 Thoughts: Here's my summary, as if I were thinking these thoughts: **Examining BPF Helper Function Implementation** Okay, let's dive in. I need to take a look at the implementation of a specific BPF helper function, likely to understand its behavior, debug a potential issue, or optimize its performance. Specifically, I'm going to focus on lines 4400 through 4440 within `kernel/bpf/helpers.c`. That should give me the core logic of the function, any relevant data structure interactions, and hopefully, some hints about its interaction with the underlying kernel infrastructure. I'll be looking for anything non-obvious, potentially problematic, or just plain interesting. I'll need to remember to check for potential race conditions, atomicity issues, and resource leaks within that range of code. Let's see what we've got. |
| 24/2 | 2026/10/12 06:39 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/helpers.c",
"FirstLine": 4400,
"LineCount": 40
}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\n"
} |
| 25/2 | 2026/10/12 06:39 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 34144 output: 35 thoughts: 19 Thoughts: Okay, here's my attempt at summarizing that thought process, keeping in mind the expert user: **Locating the `bpf_task_work_ctx` Definition** Alright, I'm diving into this. The initial thought process is straightforward: where *exactly* is `struct bpf_task_work_ctx` defined? It's a key structure in this particular BPF context, and I need to verify its layout and any relevant fields before proceeding. Without knowing the actual source code I'm dealing with, I need a clear definition to ensure I'm correctly leveraging its members and purpose. Let's get that definition. |
| 26/2 | 2026/10/12 06:39 | tool | grepper |
0mArgs:{
"Expression": "struct bpf_task_work_ctx",
"PathPrefixes": [
"kernel/bpf/"
]
}Results: {
"Output": "kernel/bpf/helpers.c=4365=enum bpf_task_work_state {\n--\nkernel/bpf/helpers.c-4379-\nkernel/bpf/helpers.c:4380:struct bpf_task_work_ctx {\nkernel/bpf/helpers.c-4381-\tenum bpf_task_work_state state;\n--\nkernel/bpf/helpers.c=4398=struct bpf_task_work_kern {\nkernel/bpf/helpers.c:4399:\tstruct bpf_task_work_ctx *ctx;\nkernel/bpf/helpers.c-4400-};\nkernel/bpf/helpers.c-4401-\nkernel/bpf/helpers.c:4402:static void bpf_task_work_ctx_reset(struct bpf_task_work_ctx *ctx)\nkernel/bpf/helpers.c-4403-{\n--\nkernel/bpf/helpers.c-4413-\nkernel/bpf/helpers.c:4414:static bool bpf_task_work_ctx_tryget(struct bpf_task_work_ctx *ctx)\nkernel/bpf/helpers.c-4415-{\n--\nkernel/bpf/helpers.c=4419=static void bpf_task_work_destroy(struct irq_work *irq_work)\nkernel/bpf/helpers.c-4420-{\nkernel/bpf/helpers.c:4421:\tstruct bpf_task_work_ctx *ctx = container_of(irq_work, struct bpf_task_work_ctx, irq_work);\nkernel/bpf/helpers.c-4422-\n--\nkernel/bpf/helpers.c-4426-\nkernel/bpf/helpers.c:4427:static void bpf_task_work_ctx_put(struct bpf_task_work_ctx *ctx)\nkernel/bpf/helpers.c-4428-{\n--\nkernel/bpf/helpers.c-4439-\nkernel/bpf/helpers.c:4440:static void bpf_task_work_cancel(struct bpf_task_work_ctx *ctx,\nkernel/bpf/helpers.c-4441-\t\t\t\t struct task_struct *task)\n--\nkernel/bpf/helpers.c=4458=static void bpf_task_work_callback(struct callback_head *cb)\nkernel/bpf/helpers.c-4459-{\nkernel/bpf/helpers.c:4460:\tstruct bpf_task_work_ctx *ctx = container_of(cb, struct bpf_task_work_ctx, work);\nkernel/bpf/helpers.c-4461-\tenum bpf_task_work_state state;\n--\nkernel/bpf/helpers.c=4492=static void bpf_task_work_irq(struct irq_work *irq_work)\nkernel/bpf/helpers.c-4493-{\nkernel/bpf/helpers.c:4494:\tstruct bpf_task_work_ctx *ctx = container_of(irq_work, struct bpf_task_work_ctx, irq_work);\nkernel/bpf/helpers.c-4495-\tstruct task_struct *task;\n--\nkernel/bpf/helpers.c-4538-\nkernel/bpf/helpers.c:4539:static struct bpf_task_work_ctx *bpf_task_work_fetch_ctx(struct bpf_task_work *tw,\nkernel/bpf/helpers.c-4540-\t\t\t\t\t\t\t struct bpf_map *map)\n--\nkernel/bpf/helpers.c-4542-\tstruct bpf_task_work_kern *twk = (void *)tw;\nkernel/bpf/helpers.c:4543:\tstruct bpf_task_work_ctx *ctx, *old_ctx;\nkernel/bpf/helpers.c-4544-\n--\nkernel/bpf/helpers.c-4570-\nkernel/bpf/helpers.c:4571:static struct bpf_task_work_ctx *bpf_task_work_acquire_ctx(struct bpf_task_work *tw,\nkernel/bpf/helpers.c-4572-\t\t\t\t\t\t\t struct bpf_map *map)\nkernel/bpf/helpers.c-4573-{\nkernel/bpf/helpers.c:4574:\tstruct bpf_task_work_ctx *ctx;\nkernel/bpf/helpers.c-4575-\n--\nkernel/bpf/helpers.c=4628=static int bpf_task_work_schedule(struct task_struct *task, struct bpf_task_work *tw,\n--\nkernel/bpf/helpers.c-4632-\tstruct bpf_prog *prog;\nkernel/bpf/helpers.c:4633:\tstruct bpf_task_work_ctx *ctx;\nkernel/bpf/helpers.c-4634-\tint err;\n--\nkernel/bpf/helpers.c=4804=static void bpf_task_work_cancel_scheduled(struct irq_work *irq_work)\nkernel/bpf/helpers.c-4805-{\nkernel/bpf/helpers.c:4806:\tstruct bpf_task_work_ctx *ctx = container_of(irq_work, struct bpf_task_work_ctx, irq_work);\nkernel/bpf/helpers.c-4807-\n--\nkernel/bpf/helpers.c=4818=void bpf_task_work_cancel_and_free(void *val)\n--\nkernel/bpf/helpers.c-4820-\tstruct bpf_task_work_kern *twk = val;\nkernel/bpf/helpers.c:4821:\tstruct bpf_task_work_ctx *ctx;\nkernel/bpf/helpers.c-4822-\tenum bpf_task_work_state state;\n"
} |
| 27/2 | 2026/10/12 06:39 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 35861 output: 37 thoughts: 105 Thoughts: **Delving into BPF kfuncs** Okay, so I'm thinking about the mechanics of employing BPF kfuncs. My immediate concern is the setup – how do I actually *register* and subsequently *call* these kfuncs? Ah, right, I remember, `BPF_SYSCALL` is essential. Now, let's circle back to something I've worked with before, `DEBUG_INFO_BTF`. I think this is where it ties in... and it looks like I'm correct. Kfuncs are, ultimately, resolved via BTF, which means `DEBUG_INFO_BTF` is a hard dependency. It makes the necessary BTF ID sets available and allows the verifier to correctly check the kfuncs. Got it. To double-check my understanding and maybe refresh my memory, I should really check through `kernel/bpf/btf.c` or the Kconfig files. This should confirm my current mental model. |
| 28/2 | 2026/10/12 06:39 | tool | grepper |
0mArgs:{
"Expression": "register_btf_kfunc_id_set",
"PathPrefixes": [
"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=5016=static int __init kfunc_init(void)\n--\nkernel/bpf/helpers.c-5031-\nkernel/bpf/helpers.c:5032:\tret = register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, \u0026generic_kfunc_set);\nkernel/bpf/helpers.c:5033:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, \u0026generic_kfunc_set);\nkernel/bpf/helpers.c:5034:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, \u0026generic_kfunc_set);\nkernel/bpf/helpers.c:5035:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, \u0026generic_kfunc_set);\nkernel/bpf/helpers.c:5036:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, \u0026generic_kfunc_set);\nkernel/bpf/helpers.c:5037:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SKB, \u0026generic_kfunc_set);\nkernel/bpf/helpers.c-5038-\tret = ret ?: register_btf_id_dtor_kfuncs(generic_dtors,\n--\nkernel/bpf/helpers.c-5040-\t\t\t\t\t\t THIS_MODULE);\nkernel/bpf/helpers.c:5041:\treturn ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, \u0026common_kfunc_set);\nkernel/bpf/helpers.c-5042-}\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"
} |
| 29/2 | 2026/10/12 06:39 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 38237 output: 35 thoughts: 37 Thoughts: Here's my take: **Investigating BTF Dependencies** Okay, so I need to quickly clarify something about BTF. I'm pretty sure I recall some dependencies, specifically in relation to the kernel config. Let's dig into `kernel/bpf/btf.c`. I want to confirm whether we *require* either `CONFIG_BPF_SYSCALL` or `DEBUG_INFO_BTF` to be enabled for BTF to function correctly. This is a critical point for understanding its deployment scope and any potential limitations. I need to be sure before I move forward with my analysis. |
| 30/2 | 2026/10/12 06:40 | tool | grepper |
0mArgs:{
"Expression": "CONFIG_BPF_SYSCALL",
"PathPrefixes": [
"kernel/bpf/Makefile"
]
}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\nkernel/bpf/Makefile:11:obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o\nkernel/bpf/Makefile:12:obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o bpf_insn_array.o\nkernel/bpf/Makefile:13:obj-$(CONFIG_BPF_SYSCALL) += bpf_local_storage.o bpf_task_storage.o\nkernel/bpf/Makefile:14:obj-$(CONFIG_BPF_SYSCALL) += fixups.o cfg.o states.o backtrack.o check_btf.o\nkernel/bpf/Makefile-15-obj-${CONFIG_BPF_LSM}\t += bpf_inode_storage.o\nkernel/bpf/Makefile:16:obj-$(CONFIG_BPF_SYSCALL) += disasm.o mprog.o\nkernel/bpf/Makefile-17-obj-$(CONFIG_BPF_JIT) += trampoline.o\nkernel/bpf/Makefile:18:obj-$(CONFIG_BPF_SYSCALL) += btf.o memalloc.o rqspinlock.o stream.o\nkernel/bpf/Makefile-19-ifeq ($(CONFIG_MMU)$(CONFIG_64BIT),yy)\nkernel/bpf/Makefile:20:obj-$(CONFIG_BPF_SYSCALL) += arena.o range_tree.o\nkernel/bpf/Makefile-21-endif\n--\nkernel/bpf/Makefile=23=ifeq ($(CONFIG_NET),y)\nkernel/bpf/Makefile:24:obj-$(CONFIG_BPF_SYSCALL) += devmap.o\nkernel/bpf/Makefile:25:obj-$(CONFIG_BPF_SYSCALL) += cpumap.o\nkernel/bpf/Makefile:26:obj-$(CONFIG_BPF_SYSCALL) += offload.o\nkernel/bpf/Makefile:27:obj-$(CONFIG_BPF_SYSCALL) += net_namespace.o\nkernel/bpf/Makefile:28:obj-$(CONFIG_BPF_SYSCALL) += tcx.o\nkernel/bpf/Makefile-29-endif\nkernel/bpf/Makefile=30=ifeq ($(CONFIG_PERF_EVENTS),y)\nkernel/bpf/Makefile:31:obj-$(CONFIG_BPF_SYSCALL) += stackmap.o\nkernel/bpf/Makefile-32-endif\nkernel/bpf/Makefile=33=ifeq ($(CONFIG_CGROUPS),y)\nkernel/bpf/Makefile:34:obj-$(CONFIG_BPF_SYSCALL) += cgroup_iter.o bpf_cgrp_storage.o\nkernel/bpf/Makefile-35-endif\n--\nkernel/bpf/Makefile=37=ifeq ($(CONFIG_INET),y)\nkernel/bpf/Makefile:38:obj-$(CONFIG_BPF_SYSCALL) += reuseport_array.o\nkernel/bpf/Makefile-39-endif\n--\nkernel/bpf/Makefile=43=ifeq ($(CONFIG_BPF_JIT),y)\nkernel/bpf/Makefile:44:obj-$(CONFIG_BPF_SYSCALL) += bpf_struct_ops.o\nkernel/bpf/Makefile:45:obj-$(CONFIG_BPF_SYSCALL) += cpumask.o\nkernel/bpf/Makefile-46-# bpf_lsm_proto.o must precede bpf_lsm.o. The current pahole logic\n--\nkernel/bpf/Makefile=58=ifneq ($(CONFIG_CRYPTO),)\nkernel/bpf/Makefile:59:obj-$(CONFIG_BPF_SYSCALL) += crypto.o\nkernel/bpf/Makefile-60-endif\nkernel/bpf/Makefile=61=obj-$(CONFIG_BPF_PRELOAD) += preload/\nkernel/bpf/Makefile-62-\nkernel/bpf/Makefile:63:obj-$(CONFIG_BPF_SYSCALL) += relo_core.o\nkernel/bpf/Makefile:64:obj-$(CONFIG_BPF_SYSCALL) += btf_iter.o\nkernel/bpf/Makefile:65:obj-$(CONFIG_BPF_SYSCALL) += btf_relocate.o\nkernel/bpf/Makefile:66:obj-$(CONFIG_BPF_SYSCALL) += kmem_cache_iter.o\nkernel/bpf/Makefile-67-ifeq ($(CONFIG_DMA_SHARED_BUFFER),y)\nkernel/bpf/Makefile:68:obj-$(CONFIG_BPF_SYSCALL) += dmabuf_iter.o\nkernel/bpf/Makefile-69-endif\n"
} |
| 31/2 | 2026/10/12 06:40 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 39788 output: 34 thoughts: 67 Thoughts: Here's my summary, as I see it: **Kernel Configuration Dependencies for BTF and Debug Information** Okay, so I'm looking at how these kernel objects, `helpers.o`, `btf.o`, and `verifier.o`, all tie back to `CONFIG_BPF_SYSCALL`. That's the baseline. Now, I need to understand if `DEBUG_INFO_BTF` is also a critical dependency, especially when generating the BTF data that's *actually* going to be in the kernel image. I need to make sure I understand the relationship and whether we can optimize the inclusion of debug info. Time to dive into the `Kconfig` files and trace those dependencies. I know where to look. |
| 32/2 | 2026/10/12 06:40 | tool | grepper |
0mArgs:{
"Expression": "config DEBUG_INFO_BTF",
"PathPrefixes": [
"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"
} |
| 33/2 | 2026/10/12 06:40 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 40217 output: 36 thoughts: 79 Thoughts: Here's my summary, as though I'm thinking through this myself: **Debugging BTF and Kernel Function Verification** Okay, so I'm digging into this BTF situation, and I think I've spotted a crucial link. It seems pretty clear that if `DEBUG_INFO_BTF` isn't enabled, we're not getting that vmlinux BTF generated. That's a huge problem because the verifier uses these BTF entries to find and call kernel functions. Specifically, `register_btf_kfunc_id_set` is going to bomb out if it doesn't have either a vmlinux BTF or a module BTF to work with. The `btf_vmlinux` pointer is going to be NULL. Right, so to confirm this behavior, I should probably go straight into `btf.c` and trace what's happening there to confirm that the conditional path is being taken. Need to see how this translates into the absence of the correct BTF data. Let's start there. |
| 34/2 | 2026/10/12 06:40 | tool | grepper |
0mArgs:{
"Expression": "btf_vmlinux",
"PathPrefixes": [
"kernel/bpf/btf.c"
]
}Results: {
"Output": "kernel/bpf/btf.c=679=s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p)\n--\nkernel/bpf/btf.c-684-\nkernel/bpf/btf.c:685:\tbtf = bpf_get_btf_vmlinux();\nkernel/bpf/btf.c-686-\tif (IS_ERR(btf))\n--\nkernel/bpf/btf.c=6072=extern char __stop_BTF[];\nkernel/bpf/btf.c:6073:extern struct btf *btf_vmlinux;\nkernel/bpf/btf.c-6074-\n--\nkernel/bpf/btf.c=6105=static const struct btf_type *find_canonical_prog_ctx_type(enum bpf_prog_type prog_type)\n--\nkernel/bpf/btf.c-6117-\t */\nkernel/bpf/btf.c:6118:\treturn btf_type_by_id(btf_vmlinux, ctx_type-\u003etype);\nkernel/bpf/btf.c-6119-}\n--\nkernel/bpf/btf.c=6146=bool btf_is_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,\n--\nkernel/bpf/btf.c-6186-\tif (!ctx_type) {\nkernel/bpf/btf.c:6187:\t\tbpf_log(log, \"btf_vmlinux is malformed\\n\");\nkernel/bpf/btf.c-6188-\t\t/* should not happen */\n--\nkernel/bpf/btf.c-6191-again:\nkernel/bpf/btf.c:6192:\tctx_tname = btf_name_by_offset(btf_vmlinux, ctx_type-\u003ename_off);\nkernel/bpf/btf.c-6193-\tif (!ctx_tname) {\n--\nkernel/bpf/btf.c-6216-\t\twhile (btf_type_is_modifier(ctx_type))\nkernel/bpf/btf.c:6217:\t\t\tctx_type = btf_type_by_id(btf_vmlinux, ctx_type-\u003etype);\nkernel/bpf/btf.c-6218-\t\tgoto again;\n--\nkernel/bpf/btf.c=6232=static int btf_validate_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,\n--\nkernel/bpf/btf.c-6340-\t\t/* should not happen */\nkernel/bpf/btf.c:6341:\t\tbpf_log(log, \"btf_vmlinux is malformed\\n\");\nkernel/bpf/btf.c-6342-\t\treturn -EINVAL;\n--\nkernel/bpf/btf.c-6346-\twhile (btf_type_is_modifier(ctx_type))\nkernel/bpf/btf.c:6347:\t\tctx_type = btf_type_by_id(btf_vmlinux, ctx_type-\u003etype);\nkernel/bpf/btf.c-6348-\n--\nkernel/bpf/btf.c-6357-\nkernel/bpf/btf.c:6358:\tctx_tname = btf_name_by_offset(btf_vmlinux, ctx_type-\u003ename_off);\nkernel/bpf/btf.c-6359-\tif (!__btf_type_is_struct(t) || strcmp(ctx_tname, tname) != 0) {\n--\nkernel/bpf/btf.c=6378=int get_kern_ctx_btf_id(struct bpf_verifier_log *log, enum bpf_prog_type prog_type)\n--\nkernel/bpf/btf.c-6388-\tkctx_type_id = kctx_member-\u003etype;\nkernel/bpf/btf.c:6389:\tkctx_type = btf_type_by_id(btf_vmlinux, kctx_type_id);\nkernel/bpf/btf.c-6390-\tif (!btf_type_is_struct(kctx_type)) {\n--\nkernel/bpf/btf.c=6453=struct btf *btf_parse_vmlinux(void)\n--\nkernel/bpf/btf.c-6469-\nkernel/bpf/btf.c:6470:\t/* btf_parse_vmlinux() runs under btf_vmlinux_lock */\nkernel/bpf/btf.c-6471-\tbpf_ctx_convert.t = btf_type_by_id(btf, bpf_ctx_convert_btf_id[0]);\n--\nkernel/bpf/btf.c=6495=static struct btf *btf_parse_module(const char *module_name, const void *data,\n--\nkernel/bpf/btf.c-6503-\nkernel/bpf/btf.c:6504:\tvmlinux_btf = bpf_get_btf_vmlinux();\nkernel/bpf/btf.c-6505-\tif (IS_ERR(vmlinux_btf))\n--\nkernel/bpf/btf.c=6864=bool btf_ctx_access(int off, int size, enum bpf_access_type type,\n--\nkernel/bpf/btf.c-7019-\t\t\tinfo-\u003ereg_type = ctx_arg_info-\u003ereg_type;\nkernel/bpf/btf.c:7020:\t\t\tinfo-\u003ebtf = ctx_arg_info-\u003ebtf ? : btf_vmlinux;\nkernel/bpf/btf.c-7021-\t\t\tinfo-\u003ebtf_id = ctx_arg_info-\u003ebtf_id;\n--\nkernel/bpf/btf.c-7076-\t\tif (ret \u003e 0) {\nkernel/bpf/btf.c:7077:\t\t\tinfo-\u003ebtf = btf_vmlinux;\nkernel/bpf/btf.c-7078-\t\t\tinfo-\u003ebtf_id = ret;\n--\nkernel/bpf/btf.c=8016=int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog)\n--\nkernel/bpf/btf.c-8179-\nkernel/bpf/btf.c:8180:\t\t\tvmlinux_btf = bpf_get_btf_vmlinux();\nkernel/bpf/btf.c-8181-\t\t\tref_t = btf_type_by_id(vmlinux_btf, kern_type_id);\n--\nkernel/bpf/btf.c=8682=struct btf *btf_get_module_btf(const struct module *module)\n--\nkernel/bpf/btf.c-8689-\tif (!module) {\nkernel/bpf/btf.c:8690:\t\tbtf = bpf_get_btf_vmlinux();\nkernel/bpf/btf.c-8691-\t\tif (!IS_ERR_OR_NULL(btf))\n--\nkernel/bpf/btf.c=8860=static int btf_check_kfunc_name(struct btf *btf, const char *func_name, u32 kind)\n--\nkernel/bpf/btf.c-8869-\nkernel/bpf/btf.c:8870:\tid = btf_find_by_name_kind(bpf_get_btf_vmlinux(), func_name, kind);\nkernel/bpf/btf.c-8871-\tif (id \u003e= 0) {\n--\nkernel/bpf/btf.c=9644=bpf_core_find_cands(struct bpf_core_ctx *ctx, u32 local_type_id)\n--\nkernel/bpf/btf.c-9654-\nkernel/bpf/btf.c:9655:\tmain_btf = bpf_get_btf_vmlinux();\nkernel/bpf/btf.c-9656-\tif (IS_ERR(main_btf))\n"
} |
| 35/2 | 2026/10/12 06:40 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 42730 output: 30 thoughts: 97 Thoughts: Okay, here's my understanding, tailored for an expert audience, as if it were my own thought process: **BTF, BPF, and Interrupt Context: A Quick Check** Right, so `bpf_get_btf_vmlinux()` clearly relies on BTF data generated when `CONFIG_DEBUG_INFO_BTF=y`. That's standard and expected. And, of course, `CONFIG_BPF_SYSCALL=y` is another crucial piece – no BPF without the syscall interface. Now, I'm thinking about broader implications, especially concerning context switching and interrupt handling. My immediate thought is `CONFIG_IRQ_WORK`. That one's pretty ubiquitous; it's practically enabled by default on amd64 systems, especially with SMP, PERF_EVENTS, and all the rest. I need to quickly refresh my memory on where `IRQ_WORK` fits into the overall kernel configuration. Let me just take a quick peek at `init/Kconfig` to confirm its dependencies and role. |
| 36/2 | 2026/10/12 06:40 | tool | grepper |
0mArgs:{
"Expression": "config IRQ_WORK",
"PathPrefixes": [
"init/Kconfig"
]
}Results: {
"Output": "init/Kconfig=200=config CONSTRUCTORS\n--\ninit/Kconfig-202-\ninit/Kconfig:203:config IRQ_WORK\ninit/Kconfig-204-\tdef_bool y if SMP\n"
} |
| 37/2 | 2026/10/12 06:40 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 43146 output: 37 thoughts: 1265 Thoughts: **Analyzing BPF Task Work for Fuzzing Opportunities**
Okay, so `IRQ_WORK` is enabled if we're dealing with SMP, and `CONFIG_BPF_SYSCALL` is standard for BPF, and `CONFIG_DEBUG_INFO_BTF` is there. Let's dig into the `bpf_task_work` implementation.
First, I need to check how the test cases and the `bpf_task_work` functions actually work, so I'll be looking into `tools/testing/selftests/bpf/prog_tests/test_task_work.c` and also `progs/task_work_busy.c`. I can see a `struct bpf_task_work` being embedded in a map value, like this:
```c
struct task_work_busy_value {
__u32 seq;
char data[60];
struct bpf_task_work tw;
};
```
And then in BPF the functions `bpf_task_work_schedule_signal(task, &work->tw, &hmap, busy_cb1)` or `bpf_task_work_schedule_resume(task, &work->tw, &hmap, ...)` are called.
Now, let's identify the critical functions to focus on for fuzzing:
- `bpf_task_work_schedule_signal`
- `bpf_task_work_schedule_resume`
- `bpf_task_work_irq`
- `bpf_task_work_acquire_ctx`
- `bpf_task_work_cancel_scheduled`
- `bpf_task_work_cancel_and_free`
Wait a minute, `bpf_task_work_schedule` is static, but `bpf_task_work_schedule_signal` and `bpf_task_work_schedule_resume` are the kfunc wrappers that call it. `bpf_task_work_acquire_ctx` is also static and is called by `bpf_task_work_schedule`. `bpf_task_work_irq` handles the irq\_work, `bpf_task_work_cancel_scheduled` is for cancellation via irq\_work, and `bpf_task_work_cancel_and_free` is triggered when maps or map values are freed. Also `bpf_task_work_callback` is just the `task_work` callback.
I need to confirm if `bpf_task_work_cancel` is static inline or static. Let's check `kernel/bpf/helpers.c`:
`static void bpf_task_work_cancel(struct bpf_task_work_ctx *ctx, struct task_struct *task)`
It's static, called from `bpf_task_work_irq` and `bpf_task_work_cancel_scheduled`.
Are there any other callers or places where it's used? I need to check the history, the commit message if possible. I don't need a git log tool, the diff itself speaks for itself, and the selftest comments are very descriptive! I can see the intent clearly: "Bounds for the hardirq busy-wait that holds the scheduling handler inside the real window...".
There are also these comments in helpers.c: "The caller passes the task explicitly: the scheduling handler must not read ctx->task after the work is published (the callback may have reset it)...", "Capture the task pointer before the work is published: once the callback runs, it may reset ctx->task...". These comments point directly to concurrency issues. "STANDBY can be published before the previous scheduling irq_work returns. Claim PENDING before checking BUSY to order the check after that publication. No irq_work has been queued for this attempt yet, so reject reuse and roll back to STANDBY while BUSY is set. Preserve FREED if deletion wins the rollback race.".
Right, so this fix is about resolving race conditions between `task_work`, `irq_work`, and BPF map element deletion/reuse. Concurrency races in the BPF subsystem are prime targets for fuzzing, especially when you consider syzkaller's focus on BPF syscalls, multi-threading, map operations, and signal handling. This makes it an ideal candidate.
Let's summarize everything and verify the requirements:
1. `WorthFuzzing`: True.
2. `Reasoning`: The change fixes race conditions in BPF `task_work` scheduling, cancellation, and context lifecycle where task pointers could be accessed after being reset and contexts could be
|
| 38/2 | 2026/10/12 06:40 | tool | grepper |
0mArgs:{
"Expression": "bpf_task_work",
"PathPrefixes": [
"kernel/bpf/",
"include/"
]
}Results: {
"Output": "\nFull output is too long, showing 200 out of 303 lines.\nUse more precise expression or PathPrefixes if possible.\n\nNumber of matching lines per file (6 files in total):\ninclude/linux/bpf.h:4\ninclude/uapi/linux/bpf.h:1\nkernel/bpf/btf.c:1\nkernel/bpf/helpers.c:74\nkernel/bpf/syscall.c:2\nkernel/bpf/verifier.c:9\n\ninclude/linux/bpf.h=346=static inline const char *btf_field_type_name(enum btf_field_type type)\n--\ninclude/linux/bpf.h-374-\tcase BPF_TASK_WORK:\ninclude/linux/bpf.h:375:\t\treturn \"bpf_task_work\";\ninclude/linux/bpf.h-376-\tdefault:\n--\ninclude/linux/bpf.h=388=static inline u32 btf_field_type_size(enum btf_field_type type)\n--\ninclude/linux/bpf.h-414-\tcase BPF_TASK_WORK:\ninclude/linux/bpf.h:415:\t\treturn sizeof(struct bpf_task_work);\ninclude/linux/bpf.h-416-\tdefault:\n--\ninclude/linux/bpf.h=422=static inline u32 btf_field_type_align(enum btf_field_type type)\n--\ninclude/linux/bpf.h-448-\tcase BPF_TASK_WORK:\ninclude/linux/bpf.h:449:\t\treturn __alignof__(struct bpf_task_work);\ninclude/linux/bpf.h-450-\tdefault:\n--\ninclude/linux/bpf.h=646=void bpf_wq_cancel_and_free(void *timer);\ninclude/linux/bpf.h:647:void bpf_task_work_cancel_and_free(void *timer);\ninclude/linux/bpf.h-648-void bpf_list_head_free(const struct btf_field *field, void *list_head,\n--\ninclude/uapi/linux/bpf.h=7595=struct bpf_timer {\n--\ninclude/uapi/linux/bpf.h-7598-\ninclude/uapi/linux/bpf.h:7599:struct bpf_task_work {\ninclude/uapi/linux/bpf.h-7600-\t__u64 __opaque;\n--\nkernel/bpf/btf.c=3669=static int btf_get_field_type(const struct btf *btf, const struct btf_type *var_type,\n--\nkernel/bpf/btf.c-3680-\t\t{ BPF_WORKQUEUE, \"bpf_wq\", true },\nkernel/bpf/btf.c:3681:\t\t{ BPF_TASK_WORK, \"bpf_task_work\", true },\nkernel/bpf/btf.c-3682-\t\t{ BPF_LIST_HEAD, \"bpf_list_head\", false },\n--\nkernel/bpf/helpers.c=4318=__bpf_kfunc int bpf_verify_pkcs7_signature(const struct bpf_dynptr *data_p,\n--\nkernel/bpf/helpers.c-4362-\nkernel/bpf/helpers.c:4363:typedef int (*bpf_task_work_callback_t)(struct bpf_map *map, void *key, void *value);\nkernel/bpf/helpers.c-4364-\nkernel/bpf/helpers.c:4365:enum bpf_task_work_state {\nkernel/bpf/helpers.c:4366:\t/* bpf_task_work is ready to be used */\nkernel/bpf/helpers.c-4367-\tBPF_TW_STANDBY = 0,\n--\nkernel/bpf/helpers.c-4379-\nkernel/bpf/helpers.c:4380:struct bpf_task_work_ctx {\nkernel/bpf/helpers.c:4381:\tenum bpf_task_work_state state;\nkernel/bpf/helpers.c-4382-\trefcount_t refcnt;\n--\nkernel/bpf/helpers.c-4396-\nkernel/bpf/helpers.c:4397:/* Actual type for struct bpf_task_work */\nkernel/bpf/helpers.c:4398:struct bpf_task_work_kern {\nkernel/bpf/helpers.c:4399:\tstruct bpf_task_work_ctx *ctx;\nkernel/bpf/helpers.c-4400-};\nkernel/bpf/helpers.c-4401-\nkernel/bpf/helpers.c:4402:static void bpf_task_work_ctx_reset(struct bpf_task_work_ctx *ctx)\nkernel/bpf/helpers.c-4403-{\n--\nkernel/bpf/helpers.c-4413-\nkernel/bpf/helpers.c:4414:static bool bpf_task_work_ctx_tryget(struct bpf_task_work_ctx *ctx)\nkernel/bpf/helpers.c-4415-{\n--\nkernel/bpf/helpers.c-4418-\nkernel/bpf/helpers.c:4419:static void bpf_task_work_destroy(struct irq_work *irq_work)\nkernel/bpf/helpers.c-4420-{\nkernel/bpf/helpers.c:4421:\tstruct bpf_task_work_ctx *ctx = container_of(irq_work, struct bpf_task_work_ctx, irq_work);\nkernel/bpf/helpers.c-4422-\nkernel/bpf/helpers.c:4423:\tbpf_task_work_ctx_reset(ctx);\nkernel/bpf/helpers.c-4424-\tkfree_rcu(ctx, rcu);\n--\nkernel/bpf/helpers.c-4426-\nkernel/bpf/helpers.c:4427:static void bpf_task_work_ctx_put(struct bpf_task_work_ctx *ctx)\nkernel/bpf/helpers.c-4428-{\n--\nkernel/bpf/helpers.c-4432-\tif (irqs_disabled()) {\nkernel/bpf/helpers.c:4433:\t\tctx-\u003eirq_work = IRQ_WORK_INIT(bpf_task_work_destroy);\nkernel/bpf/helpers.c-4434-\t\tirq_work_queue(\u0026ctx-\u003eirq_work);\nkernel/bpf/helpers.c-4435-\t} else {\nkernel/bpf/helpers.c:4436:\t\tbpf_task_work_destroy(\u0026ctx-\u003eirq_work);\nkernel/bpf/helpers.c-4437-\t}\n--\nkernel/bpf/helpers.c-4439-\nkernel/bpf/helpers.c:4440:static void bpf_task_work_cancel(struct bpf_task_work_ctx *ctx,\nkernel/bpf/helpers.c-4441-\t\t\t\t struct task_struct *task)\n--\nkernel/bpf/helpers.c-4451-\t * have reset it), while in the asynchronous cancellation path\nkernel/bpf/helpers.c:4452:\t * ctx-\u003etask is stable, see bpf_task_work_cancel_scheduled().\nkernel/bpf/helpers.c-4453-\t */\nkernel/bpf/helpers.c-4454-\tif (task_work_cancel(task, \u0026ctx-\u003ework))\nkernel/bpf/helpers.c:4455:\t\tbpf_task_work_ctx_put(ctx);\nkernel/bpf/helpers.c-4456-}\nkernel/bpf/helpers.c-4457-\nkernel/bpf/helpers.c:4458:static void bpf_task_work_callback(struct callback_head *cb)\nkernel/bpf/helpers.c-4459-{\nkernel/bpf/helpers.c:4460:\tstruct bpf_task_work_ctx *ctx = container_of(cb, struct bpf_task_work_ctx, work);\nkernel/bpf/helpers.c:4461:\tenum bpf_task_work_state state;\nkernel/bpf/helpers.c-4462-\tu32 idx;\n--\nkernel/bpf/helpers.c-4467-\t/*\nkernel/bpf/helpers.c:4468:\t * This callback may start running before bpf_task_work_irq() switched to\nkernel/bpf/helpers.c-4469-\t * SCHEDULED state, so handle both transition variants SCHEDULING|SCHEDULED -\u003e RUNNING.\n--\nkernel/bpf/helpers.c-4474-\tif (state == BPF_TW_FREED) {\nkernel/bpf/helpers.c:4475:\t\tbpf_task_work_ctx_put(ctx);\nkernel/bpf/helpers.c-4476-\t\treturn;\n--\nkernel/bpf/helpers.c-4485-\nkernel/bpf/helpers.c:4486:\tbpf_task_work_ctx_reset(ctx);\nkernel/bpf/helpers.c-4487-\t(void)cmpxchg(\u0026ctx-\u003estate, BPF_TW_RUNNING, BPF_TW_STANDBY);\nkernel/bpf/helpers.c-4488-\nkernel/bpf/helpers.c:4489:\tbpf_task_work_ctx_put(ctx);\nkernel/bpf/helpers.c-4490-}\nkernel/bpf/helpers.c-4491-\nkernel/bpf/helpers.c:4492:static void bpf_task_work_irq(struct irq_work *irq_work)\nkernel/bpf/helpers.c-4493-{\nkernel/bpf/helpers.c:4494:\tstruct bpf_task_work_ctx *ctx = container_of(irq_work, struct bpf_task_work_ctx, irq_work);\nkernel/bpf/helpers.c-4495-\tstruct task_struct *task;\nkernel/bpf/helpers.c:4496:\tenum bpf_task_work_state state;\nkernel/bpf/helpers.c-4497-\tint err;\n--\nkernel/bpf/helpers.c-4501-\tif (cmpxchg(\u0026ctx-\u003estate, BPF_TW_PENDING, BPF_TW_SCHEDULING) != BPF_TW_PENDING) {\nkernel/bpf/helpers.c:4502:\t\tbpf_task_work_ctx_put(ctx);\nkernel/bpf/helpers.c-4503-\t\treturn;\n--\nkernel/bpf/helpers.c-4516-\tif (err) {\nkernel/bpf/helpers.c:4517:\t\tbpf_task_work_ctx_reset(ctx);\nkernel/bpf/helpers.c-4518-\t\t/*\n--\nkernel/bpf/helpers.c-4522-\t\t(void)cmpxchg(\u0026ctx-\u003estate, BPF_TW_SCHEDULING, BPF_TW_STANDBY);\nkernel/bpf/helpers.c:4523:\t\tbpf_task_work_ctx_put(ctx);\nkernel/bpf/helpers.c-4524-\t\treturn;\n--\nkernel/bpf/helpers.c-4535-\tif (state == BPF_TW_FREED)\nkernel/bpf/helpers.c:4536:\t\tbpf_task_work_cancel(ctx, task); /* clean up if we switched into FREED state */\nkernel/bpf/helpers.c-4537-}\nkernel/bpf/helpers.c-4538-\nkernel/bpf/helpers.c:4539:static struct bpf_task_work_ctx *bpf_task_work_fetch_ctx(struct bpf_task_work *tw,\nkernel/bpf/helpers.c-4540-\t\t\t\t\t\t\t struct bpf_map *map)\nkernel/bpf/helpers.c-4541-{\nkernel/bpf/helpers.c:4542:\tstruct bpf_task_work_kern *twk = (void *)tw;\nkernel/bpf/helpers.c:4543:\tstruct bpf_task_work_ctx *ctx, *old_ctx;\nkernel/bpf/helpers.c-4544-\n--\nkernel/bpf/helpers.c-4555-\tctx-\u003estate = BPF_TW_STANDBY;\nkernel/bpf/helpers.c:4556:\tinit_irq_work(\u0026ctx-\u003eirq_work, bpf_task_work_irq);\nkernel/bpf/helpers.c-4557-\n--\nkernel/bpf/helpers.c-4570-\nkernel/bpf/helpers.c:4571:static struct bpf_task_work_ctx *bpf_task_work_acquire_ctx(struct bpf_task_work *tw,\nkernel/bpf/helpers.c-4572-\t\t\t\t\t\t\t struct bpf_map *map)\nkernel/bpf/helpers.c-4573-{\nkernel/bpf/helpers.c:4574:\tstruct bpf_task_work_ctx *ctx;\nkernel/bpf/helpers.c-4575-\n--\nkernel/bpf/helpers.c-4584-\tscoped_guard(rcu) {\nkernel/bpf/helpers.c:4585:\t\tctx = bpf_task_work_fetch_ctx(tw, map);\nkernel/bpf/helpers.c-4586-\t\tif (IS_ERR(ctx))\n--\nkernel/bpf/helpers.c-4589-\t\t/* try to get ref for task_work callback to hold */\nkernel/bpf/helpers.c:4590:\t\tif (!bpf_task_work_ctx_tryget(ctx))\nkernel/bpf/helpers.c-4591-\t\t\treturn ERR_PTR(-EBUSY);\n--\nkernel/bpf/helpers.c-4595-\t\t/* lost acquiring race or map_release_uref() stole it from us, put ref and bail */\nkernel/bpf/helpers.c:4596:\t\tbpf_task_work_ctx_put(ctx);\nkernel/bpf/helpers.c-4597-\t\treturn ERR_PTR(-EBUSY);\n--\nkernel/bpf/helpers.c-4607-\t\t(void)cmpxchg(\u0026ctx-\u003estate, BPF_TW_PENDING, BPF_TW_STANDBY);\nkernel/bpf/helpers.c:4608:\t\tbpf_task_work_ctx_put(ctx);\nkernel/bpf/helpers.c-4609-\t\treturn ERR_PTR(-EBUSY);\n--\nkernel/bpf/helpers.c-4618-\t\t/* drop ref we just got for task_work callback itself */\nkernel/bpf/helpers.c:4619:\t\tbpf_task_work_ctx_put(ctx);\nkernel/bpf/helpers.c-4620-\t\t/* transfer map's ref into cancel_and_free() */\nkernel/bpf/helpers.c:4621:\t\tbpf_task_work_cancel_and_free(tw);\nkernel/bpf/helpers.c-4622-\t\treturn ERR_PTR(-EBUSY);\n--\nkernel/bpf/helpers.c-4627-\nkernel/bpf/helpers.c:4628:static int bpf_task_work_schedule(struct task_struct *task, struct bpf_task_work *tw,\nkernel/bpf/helpers.c-4629-\t\t\t\t struct bpf_map *map, void *callback_fn,\n--\nkernel/bpf/helpers.c-4632-\tstruct bpf_prog *prog;\nkernel/bpf/helpers.c:4633:\tstruct bpf_task_work_ctx *ctx;\nkernel/bpf/helpers.c-4634-\tint err;\nkernel/bpf/helpers.c-4635-\nkernel/bpf/helpers.c:4636:\tBTF_TYPE_EMIT(struct bpf_task_work);\nkernel/bpf/helpers.c-4637-\n--\nkernel/bpf/helpers.c-4646-\nkernel/bpf/helpers.c:4647:\tctx = bpf_task_work_acquire_ctx(tw, map);\nkernel/bpf/helpers.c-4648-\tif (IS_ERR(ctx)) {\n--\nkernel/bpf/helpers.c-4658-\tctx-\u003emap_val = (void *)tw - map-\u003erecord-\u003etask_work_off;\nkernel/bpf/helpers.c:4659:\tinit_task_work(\u0026ctx-\u003ework, bpf_task_work_callback);\nkernel/bpf/helpers.c-4660-\n--\nkernel/bpf/helpers.c-4671-/**\nkernel/bpf/helpers.c:4672: * bpf_task_work_schedule_signal - Schedule BPF callback using task_work_add with TWA_SIGNAL\nkernel/bpf/helpers.c-4673- * mode\nkernel/bpf/helpers.c-4674- * @task: Task struct for which callback should be scheduled\nkernel/bpf/helpers.c:4675: * @tw: Pointer to struct bpf_task_work in BPF map value for internal bookkeeping\nkernel/bpf/helpers.c:4676: * @map__const_map: bpf_map that embeds struct bpf_task_work in the values\n\n"
} |
| 39/2 | 2026/10/12 06:40 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 50000 output: 268 thoughts: 265 Thoughts: **My Assessment of the Concurrency Fixes** Alright, this is straightforward and well-executed. The analysis nails it: the core subsystem here is undeniably BPF. The reachability is a slam dunk – the code clearly outlines how these functions, `bpf_task_work_schedule_signal` and `bpf_task_work_schedule_resume`, are invoked through BPF programs, dealing with `task_work`, `irq_work`, and the all-important map value destruction via `bpf_task_work_cancel_and_free`. I see the concurrency bugs being addressed, the data race on `ctx->task` potentially cleared before `irq_work` is completed is a classic, and the context reuse race is something I've dealt with myself. The focus symbols are exactly what I'd expect to see, `bpf_task_work_schedule_signal`, `bpf_task_work_schedule_resume`, `bpf_task_work_irq`, `bpf_task_work_acquire_ctx`, `bpf_task_work_cancel_scheduled`, `bpf_task_work_cancel_and_free` - a complete list. The enablement configurations are spot on: `BPF_SYSCALL` and `DEBUG_INFO_BTF`, absolutely. No surprises there. I'm satisfied. Let's call `set-results`. It's a clean analysis. |
| 40/2 | 2026/10/12 06:40 | tool | set-results |
0mArgs:{
"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_scheduled",
"bpf_task_work_cancel_and_free"
],
"Reasoning": "The patch addresses concurrency race conditions in BPF task_work scheduling, cancellation, and context lifecycle in kernel/bpf/helpers.c. Specifically, it prevents use-after-free/NULL-dereference when ctx-\u003etask is cleared by a completed callback before bpf_task_work_irq() finishes reading it, and adds a check for irq_work_is_busy() to prevent reusing task_work contexts while the previous scheduling irq_work is still active. These paths are reachable from userspace via BPF programs scheduling task_work callbacks using kfuncs. Fuzzing this complex concurrency and state machine logic is valuable for detecting exposed latent race conditions and assertions.",
"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_scheduled",
"bpf_task_work_cancel_and_free"
],
"Reasoning": "The patch addresses concurrency race conditions in BPF task_work scheduling, cancellation, and context lifecycle in kernel/bpf/helpers.c. Specifically, it prevents use-after-free/NULL-dereference when ctx-\u003etask is cleared by a completed callback before bpf_task_work_irq() finishes reading it, and adds a check for irq_work_is_busy() to prevent reusing task_work contexts while the previous scheduling irq_work is still active. These paths are reachable from userspace via BPF programs scheduling task_work callbacks using kfuncs. Fuzzing this complex concurrency and state machine logic is valuable for detecting exposed latent race conditions and assertions.",
"WorthFuzzing": true
} |
| 41/1 | 2026/10/12 06:40 | action | If |
1mArgs:{
"WorthFuzzing": true
}Results: null |
| 42/2 | 2026/10/12 06:40 | agent | kmsan-evaluator |
1mModel:core Args: null Results: {
"KMSANReasoning": "The patch addresses race conditions in the BPF task_work scheduling infrastructure (kernel/bpf/helpers.c). Specifically, it ensures that:\n1. The `task_struct` pointer is captured prior to publishing the task work, preventing a race where the callback runs concurrently, resets `ctx-\u003etask`, and causes a NULL pointer dereference or use-after-free in `bpf_task_work_cancel()`.\n2. `init_irq_work()` is moved to initialization time in `bpf_task_work_fetch_ctx()` rather than per-schedule, and `irq_work_is_busy()` is checked before reusing the context to prevent re-enqueueing or reinitializing an active `irq_work`.\n\nAll relevant memory structures (`struct bpf_task_work_ctx`) are fully zeroed upon allocation via `memset(ctx, 0, sizeof(*ctx))` and properly initialized. No uninitialized memory is read, branched on, or copied to user space. The risks addressed by these changes involve lifetime, concurrency, and pointer validity (use-after-free and NULL pointer dereference), which are thoroughly monitored by standard KASAN and lock/debugging infrastructure. A dedicated KMSAN 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 c0f793eb9550fd57d2c54c37baf7a7b04bbe5037
Author: syz-cluster <triage@syzkaller.com>
Date: Mon Oct 12 06:35:25 2026 +0000
syz-cluster: applied patch under review
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index b3cc5c8fc8756..350269b8646e4 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -4437,15 +4437,21 @@ 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
* cancelled, we put that ref on callback's behalf. If we couldn't
* cancel, callback will inevitably run or has already completed
* running, and it would have taken care of its ctx ref itself.
+ *
+ * The caller passes the task explicitly: the scheduling handler must
+ * not read ctx->task after the work is published (the callback may
+ * have reset it), while in the asynchronous cancellation path
+ * ctx->task is stable, see bpf_task_work_cancel_scheduled().
*/
- if (task_work_cancel(ctx->task, &ctx->work))
+ if (task_work_cancel(task, &ctx->work))
bpf_task_work_ctx_put(ctx);
}
@@ -4486,6 +4492,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 +4503,16 @@ static void bpf_task_work_irq(struct irq_work *irq_work)
return;
}
- err = task_work_add(ctx->task, &ctx->work, ctx->mode);
+ /*
+ * Capture the task pointer before the work is published: once the
+ * callback runs, it may reset ctx->task. The capture happens while
+ * this round's task reference is held, and every path that can
+ * release it runs after this handler entered its rcu read-side
+ * section. bpf_task_release() is put_task_struct_rcu_user(), so the
+ * task_struct itself cannot be freed before this section ends.
+ */
+ task = ctx->task;
+ err = task_work_add(task, &ctx->work, ctx->mode);
if (err) {
bpf_task_work_ctx_reset(ctx);
/*
@@ -4510,14 +4526,14 @@ static void bpf_task_work_irq(struct irq_work *irq_work)
/*
* 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.
+ * complete running by now, going SCHEDULING -> RUNNING, resetting
+ * ctx->task and dropping its ctx refcount. The rcu read-side section
+ * above keeps both the ctx memory and the captured task pointer
+ * valid until this handler returns.
*/
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 */
}
static struct bpf_task_work_ctx *bpf_task_work_fetch_ctx(struct bpf_task_work *tw,
@@ -4537,6 +4553,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 +4596,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);
}
+ /*
+ * STANDBY can be published before the previous scheduling irq_work
+ * returns. Claim PENDING before checking BUSY to order the check after
+ * that publication. No irq_work has been queued for this attempt yet,
+ * so reject reuse and roll back to STANDBY while BUSY is set. Preserve
+ * FREED if deletion wins the rollback race.
+ */
+ 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 +4657,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 +4805,13 @@ 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 */
+ /*
+ * Deletion observed SCHEDULED and won against the callback's
+ * transition to RUNNING. The callback takes its FREED exit without
+ * resetting ctx->task, and the transferred map reference prevents
+ * destruction until cancellation completes.
+ */
+ 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..deabc524dd497 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,15 @@
#include <stdio.h>
#include "task_work.skel.h"
#include "task_work_fail.skel.h"
+#include "task_work_race.skel.h"
+#include "task_work_busy.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 +160,692 @@ void test_task_work(void)
RUN_TESTS(task_work_fail);
}
+
+#define TASK_WORK_RACE_ROUNDS 1500
+#define TASK_WORK_POLL_US 100
+#define TASK_WORK_POLL_ITERS 20000
+#define TASK_WORK_START_POLL_ITERS 50000
+
+/* 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_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;
+};
+
+struct task_work_race_thread_arg {
+ struct task_work_race_ctx *ctx;
+ int cpu;
+ int *tid;
+};
+
+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_worker(void *arg)
+{
+ struct task_work_race_thread_arg *thread = arg;
+ struct task_work_race_ctx *ctx = thread->ctx;
+ int err;
+
+ err = task_work_race_pin_cpu(thread->cpu);
+ if (err)
+ __atomic_store_n(&ctx->setup_err, err, __ATOMIC_RELEASE);
+ __atomic_store_n(thread->tid, syscall(__NR_gettid), __ATOMIC_RELEASE);
+ while (!__atomic_load_n(&ctx->stop, __ATOMIC_ACQUIRE))
+ getppid();
+ return NULL;
+}
+
+static int task_work_status(struct bpf_map *map, int idx, __s64 *value)
+{
+ return bpf_map_lookup_elem(bpf_map__fd(map), &idx, value);
+}
+
+static int task_work_set_status(struct bpf_map *map, int idx, __s64 value)
+{
+ return bpf_map_update_elem(bpf_map__fd(map), &idx, &value, BPF_ANY);
+}
+
+/* Wait for seq, keeping polling errors separate from the scheduling result. */
+static int task_work_wait_seq(struct bpf_map *map, int idx, __u32 seq,
+ __s32 *result)
+{
+ int i;
+
+ for (i = 0; i < TASK_WORK_POLL_ITERS; i++) {
+ __s64 value = 0;
+
+ if (!task_work_status(map, idx, &value) &&
+ (__u32)((__u64)value >> 32) == seq) {
+ *result = (__s32)value;
+ return 0;
+ }
+ usleep(TASK_WORK_POLL_US);
+ }
+ return -ETIMEDOUT;
+}
+
+static int task_work_race_wait_ready(struct task_work_race *skel, __u32 seq)
+{
+ int i;
+
+ for (i = 0; i < TASK_WORK_POLL_ITERS; i++) {
+ __s64 value = 0;
+
+ if (!task_work_status(skel->maps.status, RACE_READY_SEQ, &value) &&
+ value == seq)
+ return 0;
+ if (!task_work_status(skel->maps.status, 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(TASK_WORK_POLL_US);
+ }
+ return -ETIMEDOUT;
+}
+
+static int task_work_race_prepare_elem(struct task_work_race *skel)
+{
+ struct task_work_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_set_status(skel->maps.status, 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)
+{
+ __s32 result;
+ int err;
+
+ err = task_work_wait_seq(skel->maps.status, RACE_RESULT, seq, &result);
+ return err ?: result;
+}
+
+static int task_work_race_wait_callback(struct task_work_race *skel,
+ __u32 seq)
+{
+ int i, key = seq;
+
+ for (i = 0; i < TASK_WORK_POLL_ITERS; i++) {
+ __u64 completed = 0;
+
+ if (!bpf_map_lookup_elem(bpf_map__fd(skel->maps.completed),
+ &key, &completed) && completed)
+ return 0;
+ usleep(TASK_WORK_POLL_US);
+ }
+ return -ETIMEDOUT;
+}
+
+void serial_test_task_work_race(void)
+{
+ struct task_work_race_ctx ctx = {};
+ struct task_work_race_thread_arg trigger_arg = {
+ .ctx = &ctx,
+ .tid = &ctx.trigger_tid,
+ };
+ struct task_work_race_thread_arg target_arg = {
+ .ctx = &ctx,
+ .tid = &ctx.target_tid,
+ };
+ 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)
+ trigger_arg.cpu = cpu;
+ else
+ target_arg.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_worker, &target_arg);
+ if (!ASSERT_OK(err, "pthread_create target"))
+ goto cleanup;
+ target_started = true;
+ err = pthread_create(&trigger, NULL, task_work_race_worker, &trigger_arg);
+ if (!ASSERT_OK(err, "pthread_create trigger"))
+ goto stop;
+ trigger_started = true;
+
+ for (i = 0; i < TASK_WORK_START_POLL_ITERS; i++) {
+ if (__atomic_load_n(&ctx.trigger_tid, __ATOMIC_ACQUIRE) &&
+ __atomic_load_n(&ctx.target_tid, __ATOMIC_ACQUIRE))
+ break;
+ usleep(TASK_WORK_POLL_US);
+ }
+ 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 after READY. A scheduling attempt
+ * racing with deletion can return -EBUSY, which the BPF program
+ * retries without publishing RESULT. The retry can then report
+ * -ENOENT because the element is gone; accept that terminal result.
+ * A later variant 0 round recreates the element.
+ */
+ if (variant == 2 && 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);
+}
+
+#define TASK_WORK_BUSY_ROUNDS 32
+#define TASK_WORK_BUSY_MAX_ATTEMPTS 96
+
+/* Must match progs/task_work_busy.c. */
+enum task_work_busy_status {
+ BUSY_ARM_SEQ,
+ /*
+ * High 32 bits hold the sequence. BUSY_WINDOW low bits: 1 open,
+ * 2 the in-window attempt was seen before the handler returned,
+ * 3 the wait bound was reached without it. Result slots: signed
+ * error in the low 32 bits.
+ */
+ BUSY_WINDOW,
+ BUSY_ACK,
+ BUSY_GEN1_RESULT,
+ BUSY_GEN2_RESULT,
+ BUSY_CB1_SEQ,
+ BUSY_CB2_COUNT,
+ BUSY_CB2_SEQ,
+ BUSY_RECOVER_SEQ,
+ BUSY_LAST_WAIT_NS,
+ BUSY_STATUS_MAX,
+};
+
+enum task_work_busy_attempt {
+ TWB_VALID,
+ TWB_MISSED,
+ TWB_HARD_FAIL,
+};
+
+static const char *twb_attempt_name(enum task_work_busy_attempt r)
+{
+ switch (r) {
+ case TWB_VALID:
+ return "valid";
+ case TWB_MISSED:
+ return "missed";
+ default:
+ return "fail";
+ }
+}
+
+static int task_work_busy_wait_value(struct task_work_busy *skel, int idx,
+ __s64 want)
+{
+ int i;
+
+ for (i = 0; i < TASK_WORK_POLL_ITERS; i++) {
+ __s64 value = 0;
+
+ if (!task_work_status(skel->maps.status, idx, &value) && value == want)
+ return 0;
+ usleep(TASK_WORK_POLL_US);
+ }
+ return -ETIMEDOUT;
+}
+
+/* Wait until the window slot reports a final state (2 or 3) for seq. */
+static int task_work_busy_wait_window(struct task_work_busy *skel, __u32 seq,
+ __s64 *window)
+{
+ int i;
+
+ for (i = 0; i < TASK_WORK_POLL_ITERS; i++) {
+ __s64 value = 0;
+
+ if (!task_work_status(skel->maps.status, BUSY_WINDOW, &value) &&
+ (__u32)((__u64)value >> 32) == seq &&
+ (__u32)value >= 2) {
+ *window = value;
+ return 0;
+ }
+ usleep(TASK_WORK_POLL_US);
+ }
+ return -ETIMEDOUT;
+}
+
+static int task_work_busy_prepare_elem(struct task_work_busy *skel, int key)
+{
+ struct task_work_value value = {};
+
+ if (!bpf_map_update_elem(bpf_map__fd(skel->maps.hmap), &key, &value,
+ BPF_NOEXIST))
+ return 0;
+ return -errno;
+}
+
+static int task_work_busy_reset(struct task_work_busy *skel)
+{
+ static const int zeroed[] = {
+ BUSY_WINDOW, BUSY_ACK, BUSY_GEN1_RESULT, BUSY_GEN2_RESULT,
+ BUSY_CB1_SEQ, BUSY_CB2_COUNT, BUSY_CB2_SEQ, BUSY_RECOVER_SEQ,
+ BUSY_LAST_WAIT_NS,
+ };
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(zeroed); i++)
+ if (task_work_set_status(skel->maps.status, zeroed[i], 0))
+ return -errno;
+ return 0;
+}
+
+/*
+ * join waits for clear_child_tid, which is cleared before exit_task_work().
+ * PID lookup stops finding the worker only after its exit task_work has run.
+ * Probe with signal 0; no signal is delivered. Do not create new workers
+ * until both old TIDs have disappeared.
+ */
+static int task_work_busy_wait_exit(int tid)
+{
+ int i;
+
+ for (i = 0; i < TASK_WORK_POLL_ITERS; i++) {
+ if (syscall(__NR_tgkill, getpid(), tid, 0)) {
+ if (errno == ESRCH)
+ return 0;
+ return -errno;
+ }
+ usleep(TASK_WORK_POLL_US);
+ }
+ return -ETIMEDOUT;
+}
+
+/*
+ * Run one attempt on dedicated workers and report its outcome. The
+ * workers are stopped, joined and waited out of PID lookup before another
+ * attempt can reset the shared state. join alone does not wait for their
+ * exit task_work to finish.
+ */
+static enum task_work_busy_attempt
+task_work_busy_run_attempt(struct task_work_busy *skel, int seq,
+ int trigger_cpu, int target_cpu, __s64 *out_wait_ns)
+{
+ struct task_work_race_ctx ctx = {};
+ struct task_work_race_thread_arg trigger_arg = {
+ .ctx = &ctx,
+ .cpu = trigger_cpu,
+ .tid = &ctx.trigger_tid,
+ };
+ struct task_work_race_thread_arg target_arg = {
+ .ctx = &ctx,
+ .cpu = target_cpu,
+ .tid = &ctx.target_tid,
+ };
+ enum task_work_busy_attempt result = TWB_HARD_FAIL;
+ pthread_t trigger, target;
+ bool trigger_started = false, target_started = false;
+ bool cleanup_ok = true;
+ __s64 window = 0, wait_ns = 0, count = -1;
+ __s32 res = 0;
+ int err, i;
+
+ *out_wait_ns = 0;
+ /* Do not let a reused TID match the previous attempt's filter. */
+ skel->bss->trigger_tid = 0;
+ skel->bss->target_tid = 0;
+
+ /* Clear old state before enabling workers that can read it. */
+ if (!ASSERT_OK(task_work_busy_prepare_elem(skel, seq), "prepare elem"))
+ goto out;
+ if (!ASSERT_OK(task_work_busy_reset(skel), "reset slots"))
+ goto out;
+
+ err = pthread_create(&target, NULL, task_work_race_worker, &target_arg);
+ if (err) {
+ ASSERT_OK(err, "pthread_create target");
+ goto out;
+ }
+ target_started = true;
+ err = pthread_create(&trigger, NULL, task_work_race_worker, &trigger_arg);
+ if (err) {
+ ASSERT_OK(err, "pthread_create trigger");
+ goto out;
+ }
+ trigger_started = true;
+
+ for (i = 0; i < TASK_WORK_START_POLL_ITERS; i++) {
+ if (__atomic_load_n(&ctx.trigger_tid, __ATOMIC_ACQUIRE) &&
+ __atomic_load_n(&ctx.target_tid, __ATOMIC_ACQUIRE))
+ break;
+ usleep(TASK_WORK_POLL_US);
+ }
+ if (!ASSERT_TRUE(__atomic_load_n(&ctx.trigger_tid, __ATOMIC_ACQUIRE) &&
+ __atomic_load_n(&ctx.target_tid, __ATOMIC_ACQUIRE),
+ "thread start"))
+ goto out;
+ if (!ASSERT_OK(__atomic_load_n(&ctx.setup_err, __ATOMIC_ACQUIRE),
+ "thread affinity"))
+ goto out;
+
+ skel->bss->trigger_tid = __atomic_load_n(&ctx.trigger_tid, __ATOMIC_ACQUIRE);
+ skel->bss->target_tid = __atomic_load_n(&ctx.target_tid, __ATOMIC_ACQUIRE);
+
+ /* Arm last, once this attempt's value, slots and TIDs are in place. */
+ if (!ASSERT_OK(task_work_set_status(skel->maps.status, BUSY_ARM_SEQ, seq),
+ "arm"))
+ goto out;
+
+ /* A fresh value has no expected BUSY source: check the first result. */
+ err = task_work_wait_seq(skel->maps.status, BUSY_GEN1_RESULT, seq, &res);
+ if (!ASSERT_OK(err, "gen1 result"))
+ goto out;
+ if (!ASSERT_OK(res, "gen1 schedule")) {
+ fprintf(stderr, "attempt %d: gen1 failed: %d\n", seq, res);
+ goto out;
+ }
+
+ if (!ASSERT_OK(task_work_busy_wait_window(skel, seq, &window),
+ "window close"))
+ goto out;
+
+ if ((__u32)window == 3) {
+ /*
+ * The attempt did not arrive inside the wait bound. The
+ * elapsed time is a diagnostic only; count
+ * the miss and retry with a fresh attempt.
+ */
+ result = TWB_MISSED;
+ goto out;
+ }
+
+ /*
+ * Valid window: the handler saw this sequence's attempt before
+ * returning, so the attempt ran while the scheduling irq_work was
+ * still BUSY. The elapsed time is a diagnostic only: host vCPU
+ * preemption can stretch wall-clock time without affecting the
+ * gate behavior under test.
+ */
+ err = task_work_wait_seq(skel->maps.status, BUSY_ACK, seq, &res);
+ if (!ASSERT_OK(err, "in-window attempt"))
+ goto out;
+ if (res != -EBUSY) {
+ fprintf(stderr,
+ "attempt %d: in-window reuse %s (%d), expected -EBUSY\n",
+ seq, res == 0 ? "accepted" : "failed", res);
+ ASSERT_EQ(res, -EBUSY, "in-window BUSY gate rejection");
+ goto out;
+ }
+
+ /*
+ * The window was released; retry until BUSY clears. The second
+ * callback must then run exactly once.
+ */
+ if (!ASSERT_OK(task_work_set_status(skel->maps.status, BUSY_RECOVER_SEQ, seq),
+ "enable recovery"))
+ goto out;
+ err = task_work_wait_seq(skel->maps.status, BUSY_GEN2_RESULT, seq, &res);
+ if (!ASSERT_OK(err, "recovery result"))
+ goto out;
+ if (!ASSERT_OK(res, "recovery schedule")) {
+ fprintf(stderr, "attempt %d: recovery failed: %d\n", seq, res);
+ goto out;
+ }
+ if (!ASSERT_OK(task_work_busy_wait_value(skel, BUSY_CB2_SEQ, seq),
+ "gen2 callback"))
+ goto out;
+ result = TWB_VALID;
+out:
+ __atomic_store_n(&ctx.stop, 1, __ATOMIC_RELEASE);
+ if (trigger_started &&
+ (!ASSERT_OK(pthread_join(trigger, NULL), "trigger join") ||
+ !ASSERT_OK(task_work_busy_wait_exit(ctx.trigger_tid), "trigger exit"))) {
+ cleanup_ok = false;
+ result = TWB_HARD_FAIL;
+ }
+ if (target_started &&
+ (!ASSERT_OK(pthread_join(target, NULL), "target join") ||
+ !ASSERT_OK(task_work_busy_wait_exit(ctx.target_tid), "target exit"))) {
+ cleanup_ok = false;
+ result = TWB_HARD_FAIL;
+ }
+ /* Both workers have passed exit_task_work; the count is now stable. */
+ if (result == TWB_VALID) {
+ if (!ASSERT_OK(task_work_status(skel->maps.status, BUSY_CB2_COUNT, &count),
+ "cb2 count")) {
+ result = TWB_HARD_FAIL;
+ } else if (!ASSERT_EQ(count, 1, "gen2 callback exactly once")) {
+ fprintf(stderr, "attempt %d: gen2 callback count %lld\n",
+ seq, (long long)count);
+ result = TWB_HARD_FAIL;
+ }
+ }
+ /*
+ * Only a captured final window belongs to this attempt. The fexit
+ * runs in the trigger's interrupted context, so successful worker
+ * cleanup also completes its diagnostic write. Read once here;
+ * the elapsed value does not decide whether the attempt passed.
+ */
+ if (cleanup_ok && window) {
+ err = task_work_status(skel->maps.status, BUSY_LAST_WAIT_NS, &wait_ns);
+ if (err) {
+ err = -errno;
+ if (result != TWB_HARD_FAIL) {
+ ASSERT_OK(err, "wait diagnostic");
+ result = TWB_HARD_FAIL;
+ } else {
+ fprintf(stderr,
+ "attempt %d: elapsed diagnostic unavailable: %d\n",
+ seq, err);
+ }
+ }
+ }
+ *out_wait_ns = wait_ns;
+ fprintf(stderr, "attempt %d: %s (window %u wait_ns %lld)\n",
+ seq, twb_attempt_name(result), (__u32)window, (long long)wait_ns);
+ return result;
+}
+
+void serial_test_task_work_busy(void)
+{
+ struct task_work_busy *skel;
+ struct bpf_link *link_tp = NULL, *link_fexit = NULL;
+ cpu_set_t allowed;
+ int cpu, cpu_count = 0, trigger_cpu = -1, target_cpu = -1;
+ int attempt, attempts_started = 0, valid = 0, missed = 0;
+ bool hard_fail = false;
+ __s64 max_wait_ns = 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)
+ trigger_cpu = cpu;
+ else
+ 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_busy__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ return;
+ if (skel->kconfig->CONFIG_PREEMPT_RT) {
+ printf("%s:SKIP:CONFIG_PREEMPT_RT=y runs irq_work in a kernel thread\n",
+ __func__);
+ test__skip();
+ goto cleanup;
+ }
+
+ link_tp = bpf_program__attach_tracepoint(skel->progs.busy_sched_work,
+ "syscalls",
+ "sys_enter_getppid");
+ if (!ASSERT_OK_PTR(link_tp, "attach tracepoint"))
+ goto cleanup;
+
+ link_fexit = bpf_program__attach_trace(skel->progs.busy_fexit_task_work_add);
+ if (!ASSERT_OK_PTR(link_fexit, "attach fexit"))
+ goto cleanup;
+
+ for (attempt = 1;
+ attempt <= TASK_WORK_BUSY_MAX_ATTEMPTS &&
+ valid < TASK_WORK_BUSY_ROUNDS;
+ attempt++) {
+ enum task_work_busy_attempt res;
+ __s64 wait_ns = 0;
+
+ attempts_started = attempt;
+ res = task_work_busy_run_attempt(skel, attempt, trigger_cpu,
+ target_cpu, &wait_ns);
+ switch (res) {
+ case TWB_VALID:
+ valid++;
+ if (wait_ns > max_wait_ns)
+ max_wait_ns = wait_ns;
+ break;
+ case TWB_MISSED:
+ missed++;
+ break;
+ case TWB_HARD_FAIL:
+ hard_fail = true;
+ break;
+ }
+ if (hard_fail)
+ break;
+ }
+
+ fprintf(stderr,
+ "task_work_busy: %d/%d valid, %d missed, %d attempts, max_wait %lld ns\n",
+ valid, TASK_WORK_BUSY_ROUNDS, missed, attempts_started,
+ (long long)max_wait_ns);
+
+ /*
+ * The coverage assertion only makes sense when no hard
+ * failure aborted the loop; after a hard failure keep the primary
+ * reason instead of deriving more assertion noise from it.
+ */
+ if (!hard_fail)
+ ASSERT_EQ(valid, TASK_WORK_BUSY_ROUNDS, "valid busy windows");
+cleanup:
+ bpf_link__destroy(link_tp);
+ bpf_link__destroy(link_fexit);
+ task_work_busy__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/task_work_busy.c b/tools/testing/selftests/bpf/progs/task_work_busy.c
new file mode 100644
index 0000000000000..9960011edadc5
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/task_work_busy.c
@@ -0,0 +1,325 @@
+// 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/bpf_core_read.h>
+
+char _license[] SEC("license") = "GPL";
+
+extern bool CONFIG_PREEMPT_RT __kconfig __weak;
+
+/*
+ * Bounds for the hardirq busy-wait that holds the scheduling handler
+ * inside the real window: task_work_add() has published the callback,
+ * but the handler's tail state transition has not run yet.
+ * irq_work_single() clears BUSY only after the handler returns, so
+ * while this wait runs, the context's scheduling irq_work is BUSY,
+ * even though the first callback may already have completed and
+ * returned the context to STANDBY.
+ *
+ * The wait is bounded both by a bpf_ktime_get_ns() check and by an
+ * iteration budget. The time bound is checked against a monotonic
+ * clock: host vCPU preemption can stretch the wall-clock duration of
+ * the window, which only makes the window longer, never shorter.
+ */
+#define BUSY_WAIT_NS 4000000ULL
+#define BUSY_WAIT_ITERS 1000000ULL
+
+int trigger_tid;
+int target_tid;
+
+/*
+ * High 32 bits of the result slots hold the sequence, low 32 bits the
+ * signed error. BUSY_WINDOW low bits: 1 open, 2 the in-window attempt
+ * was seen before the handler returned, 3 the wait bound was reached
+ * without it. Must match the test.
+ */
+enum task_work_busy_status {
+ BUSY_ARM_SEQ,
+ BUSY_WINDOW,
+ BUSY_ACK,
+ BUSY_GEN1_RESULT,
+ BUSY_GEN2_RESULT,
+ BUSY_CB1_SEQ,
+ BUSY_CB2_COUNT,
+ BUSY_CB2_SEQ,
+ BUSY_RECOVER_SEQ,
+ BUSY_LAST_WAIT_NS,
+ BUSY_STATUS_MAX,
+};
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARRAY);
+ __uint(max_entries, BUSY_STATUS_MAX);
+ __type(key, int);
+ __type(value, __s64);
+} status SEC(".maps");
+
+struct task_work_busy_value {
+ __u32 seq;
+ char data[60];
+ struct bpf_task_work tw;
+};
+
+/*
+ * max_entries must cover TASK_WORK_BUSY_MAX_ATTEMPTS in the test: each
+ * attempt schedules into its own fresh value.
+ */
+struct {
+ __uint(type, BPF_MAP_TYPE_HASH);
+ __uint(map_flags, BPF_F_NO_PREALLOC);
+ __uint(max_entries, 128);
+ __type(key, int);
+ __type(value, struct task_work_busy_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 __always_inline __s64 get_status(int key)
+{
+ __s64 *slot = bpf_map_lookup_elem(&status, &key);
+
+ return slot ? *slot : 0;
+}
+
+struct busy_wait_ctx {
+ __u64 seq;
+ __u64 start;
+ bool acked;
+};
+
+/* bpf_loop() callback: return 0 continues the loop, return 1 stops it. */
+static long busy_wait_cb(__u32 i, void *pctx)
+{
+ struct busy_wait_ctx *w = pctx;
+
+ if (((__u64)get_status(BUSY_ACK) >> 32) == w->seq) {
+ w->acked = true;
+ return 1;
+ }
+ if (bpf_ktime_get_ns() - w->start > BUSY_WAIT_NS)
+ return 1;
+ return 0;
+}
+
+static int busy_cb_gen1(struct bpf_map *map, void *key, void *value)
+{
+ struct task_work_busy_value *work = value;
+ __u64 we = (__u64)get_status(BUSY_WINDOW) >> 32;
+
+ /*
+ * Backstop for deliveries from abandoned attempts: accept only
+ * while no window for a different sequence is published. The
+ * test waits for the old workers to disappear from PID lookup,
+ * after their exit task_work, before resetting the shared state.
+ * This callback may run before fexit publishes the window state.
+ */
+ if (we && we != (__u64)work->seq)
+ return 0;
+ set_status(BUSY_CB1_SEQ, work->seq);
+ return 0;
+}
+
+static int busy_cb_gen2(struct bpf_map *map, void *key, void *value)
+{
+ struct task_work_busy_value *work = value;
+ __s64 *count;
+ int key2 = BUSY_CB2_COUNT;
+
+ /*
+ * Count only deliveries belonging to the sequence whose window is
+ * (or was) published: a late delivery from an abandoned attempt
+ * must not satisfy a later exactly-once check.
+ */
+ if ((__u64)work->seq != ((__u64)get_status(BUSY_WINDOW) >> 32))
+ return 0;
+ set_status(BUSY_CB2_SEQ, work->seq);
+ count = bpf_map_lookup_elem(&status, &key2);
+ if (count)
+ __sync_fetch_and_add(count, 1);
+ return 0;
+}
+
+SEC("fexit/task_work_add")
+int BPF_PROG(busy_fexit_task_work_add, struct task_struct *task,
+ struct callback_head *work, int notify, int retval)
+{
+ struct busy_wait_ctx w = {};
+ __u64 seq, elapsed;
+
+ seq = (__u64)get_status(BUSY_ARM_SEQ);
+ if (!seq)
+ return 0;
+
+ /*
+ * One-shot arm. Only this test's own first-round add may consume
+ * it and hold the window: a successful TWA_SIGNAL add of the
+ * bpf_task_work callback, issued from the trigger thread's CPU by
+ * the self-IPI'd scheduling handler and targeting the target
+ * thread. Everything else leaves the arm untouched.
+ */
+ if (retval != 0 || notify != TWA_SIGNAL ||
+ BPF_CORE_READ(task, pid) != target_tid)
+ return 0;
+ if ((__u32)bpf_get_current_pid_tgid() != (__u32)trigger_tid)
+ return 0;
+
+ set_status(BUSY_ARM_SEQ, 0);
+ set_status(BUSY_WINDOW, (seq << 32) | 1);
+
+ w.seq = seq;
+ w.start = bpf_ktime_get_ns();
+ bpf_loop(BUSY_WAIT_ITERS, busy_wait_cb, &w, 0);
+ elapsed = bpf_ktime_get_ns() - w.start;
+
+ /*
+ * Publish elapsed time for logging after worker cleanup. Window
+ * validity depends on seeing ACK here, not on this diagnostic.
+ */
+ set_status(BUSY_LAST_WAIT_NS, (__s64)elapsed);
+ if (w.acked)
+ set_status(BUSY_WINDOW, (seq << 32) | 2);
+ else
+ set_status(BUSY_WINDOW, (seq << 32) | 3);
+ return 0;
+}
+
+/*
+ * Attempt the second scheduling round on the same context, targeting the
+ * trigger thread. The attempt uses the value keyed by its own sequence,
+ * so it can never touch another attempt's context. Returns true if a
+ * terminal result was published into res_key; a transient -EBUSY is left
+ * unpublished for the caller's retry policy, mirroring the race test's
+ * convention.
+ */
+static __always_inline bool busy_try_gen2(__u64 seq, int res_key)
+{
+ struct task_work_busy_value *work;
+ struct task_struct *task;
+ int key = (int)seq, err;
+
+ task = bpf_task_from_pid(trigger_tid);
+ if (!task) {
+ set_status(res_key, ((__s64)seq << 32) | (__u32)-ESRCH);
+ return true;
+ }
+ work = bpf_map_lookup_elem(&hmap, &key);
+ if (!work) {
+ bpf_task_release(task);
+ set_status(res_key, ((__s64)seq << 32) | (__u32)-ENOENT);
+ return true;
+ }
+ work->seq = seq;
+ err = bpf_task_work_schedule_signal(task, &work->tw, &hmap,
+ busy_cb_gen2);
+ bpf_task_release(task);
+ if (err == -EBUSY)
+ return false;
+ set_status(res_key, ((__s64)seq << 32) | (__u32)err);
+ return true;
+}
+
+SEC("tracepoint/syscalls/sys_enter_getppid")
+int busy_sched_work(void *ctx)
+{
+ __u32 tid = (__u32)bpf_get_current_pid_tgid();
+ struct task_work_busy_value *work;
+ struct task_struct *task;
+ __u64 window, seq;
+ int key, err;
+
+ /* Also keep the kconfig extern in object BTF for the userspace skip. */
+ if (CONFIG_PREEMPT_RT)
+ return 0;
+
+ if (tid == (__u32)trigger_tid) {
+ /*
+ * Trigger thread: schedule the first round once per armed
+ * sequence, targeting the target thread. The kfunc queues
+ * the scheduling irq_work and returns; the handler then
+ * parks inside the fexit window above. A fresh value has no
+ * expected BUSY source, so publish every scheduling result.
+ */
+ seq = (__u64)get_status(BUSY_ARM_SEQ);
+ if (!seq ||
+ ((__u64)get_status(BUSY_GEN1_RESULT) >> 32) == seq)
+ return 0;
+ key = (int)seq;
+ task = bpf_task_from_pid(target_tid);
+ if (!task) {
+ set_status(BUSY_GEN1_RESULT,
+ ((__s64)seq << 32) | (__u32)-ESRCH);
+ set_status(BUSY_ARM_SEQ, 0);
+ return 0;
+ }
+ work = bpf_map_lookup_elem(&hmap, &key);
+ if (!work) {
+ bpf_task_release(task);
+ set_status(BUSY_GEN1_RESULT,
+ ((__s64)seq << 32) | (__u32)-ENOENT);
+ set_status(BUSY_ARM_SEQ, 0);
+ return 0;
+ }
+ work->seq = seq;
+ err = bpf_task_work_schedule_signal(task, &work->tw, &hmap,
+ busy_cb_gen1);
+ bpf_task_release(task);
+ set_status(BUSY_GEN1_RESULT, ((__s64)seq << 32) | (__u32)err);
+ return 0;
+ }
+
+ if (tid != (__u32)target_tid)
+ return 0;
+
+ window = (__u64)get_status(BUSY_WINDOW);
+ seq = window >> 32;
+ if (!seq || (__u32)window == 0)
+ return 0;
+ /*
+ * The first callback must have completed for this sequence. Being
+ * inside the target thread's own subsequent syscall proves the
+ * callback wrapper has returned and the context is STANDBY, which
+ * is stronger than observing the completion marker from another
+ * thread.
+ */
+ if ((__u64)get_status(BUSY_CB1_SEQ) != seq)
+ return 0;
+
+ if ((__u64)get_status(BUSY_ACK) >> 32 != seq) {
+ /*
+ * The in-window attempt, exactly once per sequence. On a
+ * fixed kernel it must be rejected with -EBUSY: the context
+ * is STANDBY, the map and program references are alive and
+ * no other scheduler exists, so only the BUSY gate can
+ * reject here.
+ */
+ if (!busy_try_gen2(seq, BUSY_ACK))
+ set_status(BUSY_ACK,
+ ((__s64)seq << 32) | (__u32)-EBUSY);
+ return 0;
+ }
+
+ /*
+ * Recovery, gated by the test: the fexit program releasing the
+ * window does not mean the whole old handler has returned and
+ * BUSY has cleared yet, so retry until the schedule succeeds.
+ */
+ if ((__u64)get_status(BUSY_RECOVER_SEQ) != seq)
+ return 0;
+ if ((__s32)get_status(BUSY_ACK) == 0)
+ return 0; /* scheduled in-window; nothing to recover */
+ if (((__u64)get_status(BUSY_GEN2_RESULT) >> 32) == seq)
+ return 0; /* terminal recovery result published */
+ busy_try_gen2(seq, BUSY_GEN2_RESULT);
+ return 0;
+}
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;
+}
|
| 43/3 | 2026/10/12 06:40 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 17720 output: 37 thoughts: 2095 Thoughts: **Analyzing a Kernel Patch for Potential Uninitialized Memory Risks** Okay, so I need to assess this kernel patch for any uninitialized memory vulnerabilities that might warrant a KMSAN fuzzing session. Let's see... the diff is in `kernel/bpf/helpers.c`, and it looks like it's touching `bpf_task_work` functionality. That's interesting, as it is a place where context switches and memory management could introduce issues. I see changes related to `bpf_task_work_ctx_put`, `bpf_task_work_cancel`, `bpf_task_work_irq`, `bpf_task_work_fetch_ctx`, `bpf_task_work_acquire_ctx`, `bpf_task_work_schedule`, and `bpf_task_work_cancel_scheduled`. So, this patch seems to be about improving concurrency and correctness around how `bpf_task_work` contexts are handled, specifically in relation to IRQ handling, task scheduling, and cancellation. The core of the patch involves adding `task` to `bpf_task_work_cancel`, and a more robust approach to how the `task_struct` is handled, and how a context is acquired and scheduled. I'm reading notes about how the `ctx->task` pointer might be reset by the callback, and about RCU read-side sections. There's a capture happening within the IRQ handler to ensure the `task` pointer is valid. I see comments about preventing the ctx being freed before a section ends. This is all about ensuring the right reference is kept to the context memory. The comments make it clear that this is a race condition issue, likely involving freeing of memory concurrently to another thread needing it. The addition of the `irq_work_is_busy` check and the PENDING state is interesting. This looks like a fix to prevent multiple schedules on an active `irq_work`. There is a lot of effort going into properly managing refcounts and memory synchronization. The patch seems to be making things safer, particularly around races between the IRQ handler, the task work callback, and cancellation. I don't immediately see any obvious uninitialized memory reads. But, given the nature of these changes – focusing on concurrency, refcounting, and preventing use-after-free – it is possible there are some issues. I need to dig deeper. I need to understand the control flow, the data dependencies, and look for potential paths where an uninitialized value might leak out. A KMSAN session might be justified if I am unable to definitively rule out any risks. |
| 44/3 | 2026/10/12 06:41 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/helpers.c",
"FirstLine": 4480,
"LineCount": 80
}Results: {
"Contents": "4480:\t\n4481:\t\tmigrate_disable();\n4482:\t\tctx-\u003ecallback_fn((u64)(long)ctx-\u003emap, (u64)(long)key,\n4483:\t\t\t\t (u64)(long)ctx-\u003emap_val, 0, 0);\n4484:\t\tmigrate_enable();\n4485:\t\n4486:\t\tbpf_task_work_ctx_reset(ctx);\n4487:\t\t(void)cmpxchg(\u0026ctx-\u003estate, BPF_TW_RUNNING, BPF_TW_STANDBY);\n4488:\t\n4489:\t\tbpf_task_work_ctx_put(ctx);\n4490:\t}\n4491:\t\n4492:\tstatic void bpf_task_work_irq(struct irq_work *irq_work)\n4493:\t{\n4494:\t\tstruct bpf_task_work_ctx *ctx = container_of(irq_work, struct bpf_task_work_ctx, irq_work);\n4495:\t\tstruct task_struct *task;\n4496:\t\tenum bpf_task_work_state state;\n4497:\t\tint err;\n4498:\t\n4499:\t\tguard(rcu)();\n4500:\t\n4501:\t\tif (cmpxchg(\u0026ctx-\u003estate, BPF_TW_PENDING, BPF_TW_SCHEDULING) != BPF_TW_PENDING) {\n4502:\t\t\tbpf_task_work_ctx_put(ctx);\n4503:\t\t\treturn;\n4504:\t\t}\n4505:\t\n4506:\t\t/*\n4507:\t\t * Capture the task pointer before the work is published: once the\n4508:\t\t * callback runs, it may reset ctx-\u003etask. The capture happens while\n4509:\t\t * this round's task reference is held, and every path that can\n4510:\t\t * release it runs after this handler entered its rcu read-side\n4511:\t\t * section. bpf_task_release() is put_task_struct_rcu_user(), so the\n4512:\t\t * task_struct itself cannot be freed before this section ends.\n4513:\t\t */\n4514:\t\ttask = ctx-\u003etask;\n4515:\t\terr = task_work_add(task, \u0026ctx-\u003ework, ctx-\u003emode);\n4516:\t\tif (err) {\n4517:\t\t\tbpf_task_work_ctx_reset(ctx);\n4518:\t\t\t/*\n4519:\t\t\t * try to switch back to STANDBY for another task_work reuse, but we might have\n4520:\t\t\t * gone to FREED already, which is fine as we already cleaned up after ourselves\n4521:\t\t\t */\n4522:\t\t\t(void)cmpxchg(\u0026ctx-\u003estate, BPF_TW_SCHEDULING, BPF_TW_STANDBY);\n4523:\t\t\tbpf_task_work_ctx_put(ctx);\n4524:\t\t\treturn;\n4525:\t\t}\n4526:\t\n4527:\t\t/*\n4528:\t\t * It's technically possible for just scheduled task_work callback to\n4529:\t\t * complete running by now, going SCHEDULING -\u003e RUNNING, resetting\n4530:\t\t * ctx-\u003etask and dropping its ctx refcount. The rcu read-side section\n4531:\t\t * above keeps both the ctx memory and the captured task pointer\n4532:\t\t * valid until this handler returns.\n4533:\t\t */\n4534:\t\tstate = cmpxchg(\u0026ctx-\u003estate, BPF_TW_SCHEDULING, BPF_TW_SCHEDULED);\n4535:\t\tif (state == BPF_TW_FREED)\n4536:\t\t\tbpf_task_work_cancel(ctx, task); /* clean up if we switched into FREED state */\n4537:\t}\n4538:\t\n4539:\tstatic struct bpf_task_work_ctx *bpf_task_work_fetch_ctx(struct bpf_task_work *tw,\n4540:\t\t\t\t\t\t\t\t struct bpf_map *map)\n4541:\t{\n4542:\t\tstruct bpf_task_work_kern *twk = (void *)tw;\n4543:\t\tstruct bpf_task_work_ctx *ctx, *old_ctx;\n4544:\t\n4545:\t\tctx = READ_ONCE(twk-\u003ectx);\n4546:\t\tif (ctx)\n4547:\t\t\treturn ctx;\n4548:\t\n4549:\t\tctx = bpf_map_kmalloc_nolock(map, sizeof(*ctx), 0, NUMA_NO_NODE);\n4550:\t\tif (!ctx)\n4551:\t\t\treturn ERR_PTR(-ENOMEM);\n4552:\t\n4553:\t\tmemset(ctx, 0, sizeof(*ctx));\n4554:\t\trefcount_set(\u0026ctx-\u003erefcnt, 1); /* map's own ref */\n4555:\t\tctx-\u003estate = BPF_TW_STANDBY;\n4556:\t\tinit_irq_work(\u0026ctx-\u003eirq_work, bpf_task_work_irq);\n4557:\t\n4558:\t\told_ctx = cmpxchg(\u0026twk-\u003ectx, NULL, ctx);\n4559:\t\tif (old_ctx) {\n"
} |
| 45/3 | 2026/10/12 06:41 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 21820 output: 37 thoughts: 17 Thoughts: Okay, here's my attempt at summarizing that thought process, keeping in mind I'm speaking to a fellow expert: **Diving Deeper into bpf_task_work Functions** Right, let's dig into the `bpf_task_work_*` functions. We've been looking at BPF and its task management capabilities, but I feel like we're just scratching the surface. Time to really understand how these specific functions operate. I want to see how they're implemented, what their limitations are, and how we might leverage them more effectively. What nuances are hidden in their internals? Are there any unexpected behaviors or edge cases we should be aware of? Let's get reading and dissect these functions. |
| 46/3 | 2026/10/12 06:41 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/helpers.c",
"FirstLine": 4420,
"LineCount": 60
}Results: {
"Contents": "4420:\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\t * The caller passes the task explicitly: the scheduling handler must\n4450:\t\t * not read ctx-\u003etask after the work is published (the callback may\n4451:\t\t * have reset it), while in the asynchronous cancellation path\n4452:\t\t * ctx-\u003etask is stable, see bpf_task_work_cancel_scheduled().\n4453:\t\t */\n4454:\t\tif (task_work_cancel(task, \u0026ctx-\u003ework))\n4455:\t\t\tbpf_task_work_ctx_put(ctx);\n4456:\t}\n4457:\t\n4458:\tstatic void bpf_task_work_callback(struct callback_head *cb)\n4459:\t{\n4460:\t\tstruct bpf_task_work_ctx *ctx = container_of(cb, struct bpf_task_work_ctx, work);\n4461:\t\tenum bpf_task_work_state state;\n4462:\t\tu32 idx;\n4463:\t\tvoid *key;\n4464:\t\n4465:\t\t/* Read lock is needed to protect ctx and map key/value access */\n4466:\t\tguard(rcu_tasks_trace)();\n4467:\t\t/*\n4468:\t\t * This callback may start running before bpf_task_work_irq() switched to\n4469:\t\t * SCHEDULED state, so handle both transition variants SCHEDULING|SCHEDULED -\u003e RUNNING.\n4470:\t\t */\n4471:\t\tstate = cmpxchg(\u0026ctx-\u003estate, BPF_TW_SCHEDULING, BPF_TW_RUNNING);\n4472:\t\tif (state == BPF_TW_SCHEDULED)\n4473:\t\t\tstate = cmpxchg(\u0026ctx-\u003estate, BPF_TW_SCHEDULED, BPF_TW_RUNNING);\n4474:\t\tif (state == BPF_TW_FREED) {\n4475:\t\t\tbpf_task_work_ctx_put(ctx);\n4476:\t\t\treturn;\n4477:\t\t}\n4478:\t\n4479:\t\tkey = (void *)map_key_from_value(ctx-\u003emap, ctx-\u003emap_val, \u0026idx);\n"
} |
| 47/3 | 2026/10/12 06:41 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 23069 output: 38 thoughts: 19 Thoughts: Okay, here's the summary, rendered as if I am the one thinking these thoughts: **Focusing In for Precision** Alright, I need to get a handle on this quickly. I've got a general understanding, but I need to pinpoint the critical section for this particular query. Let's see... the text seems extensive. To avoid wasting time, I'm going to jump to lines 4550-4660. That feels like a reasonable bracket to start with, given the context. Hopefully, that will give me the targeted information I need without excessive searching. I'll read through that section carefully and make sure to extract the relevant data efficiently. |
| 48/3 | 2026/10/12 06:41 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/helpers.c",
"FirstLine": 4560,
"LineCount": 100
}Results: {
"Contents": "4560:\t\t\t/*\n4561:\t\t\t * tw-\u003ectx is set by concurrent BPF program, release allocated\n4562:\t\t\t * memory and try to reuse already set context.\n4563:\t\t\t */\n4564:\t\t\tkfree_nolock(ctx);\n4565:\t\t\treturn old_ctx;\n4566:\t\t}\n4567:\t\n4568:\t\treturn ctx; /* Success */\n4569:\t}\n4570:\t\n4571:\tstatic struct bpf_task_work_ctx *bpf_task_work_acquire_ctx(struct bpf_task_work *tw,\n4572:\t\t\t\t\t\t\t\t struct bpf_map *map)\n4573:\t{\n4574:\t\tstruct bpf_task_work_ctx *ctx;\n4575:\t\n4576:\t\t/*\n4577:\t\t * Sleepable BPF programs hold rcu_read_lock_trace but not\n4578:\t\t * regular rcu_read_lock. Since kfree_rcu waits for regular\n4579:\t\t * RCU GP, the ctx can be freed while we're between reading\n4580:\t\t * the pointer and incrementing the refcount. Take regular\n4581:\t\t * rcu_read_lock to prevent kfree_rcu from freeing the ctx\n4582:\t\t * before we can tryget it.\n4583:\t\t */\n4584:\t\tscoped_guard(rcu) {\n4585:\t\t\tctx = bpf_task_work_fetch_ctx(tw, map);\n4586:\t\t\tif (IS_ERR(ctx))\n4587:\t\t\t\treturn ctx;\n4588:\t\n4589:\t\t\t/* try to get ref for task_work callback to hold */\n4590:\t\t\tif (!bpf_task_work_ctx_tryget(ctx))\n4591:\t\t\t\treturn ERR_PTR(-EBUSY);\n4592:\t\t}\n4593:\t\n4594:\t\tif (cmpxchg(\u0026ctx-\u003estate, BPF_TW_STANDBY, BPF_TW_PENDING) != BPF_TW_STANDBY) {\n4595:\t\t\t/* lost acquiring race or map_release_uref() stole it from us, put ref and bail */\n4596:\t\t\tbpf_task_work_ctx_put(ctx);\n4597:\t\t\treturn ERR_PTR(-EBUSY);\n4598:\t\t}\n4599:\t\t/*\n4600:\t\t * STANDBY can be published before the previous scheduling irq_work\n4601:\t\t * returns. Claim PENDING before checking BUSY to order the check after\n4602:\t\t * that publication. No irq_work has been queued for this attempt yet,\n4603:\t\t * so reject reuse and roll back to STANDBY while BUSY is set. Preserve\n4604:\t\t * FREED if deletion wins the rollback race.\n4605:\t\t */\n4606:\t\tif (unlikely(irq_work_is_busy(\u0026ctx-\u003eirq_work))) {\n4607:\t\t\t(void)cmpxchg(\u0026ctx-\u003estate, BPF_TW_PENDING, BPF_TW_STANDBY);\n4608:\t\t\tbpf_task_work_ctx_put(ctx);\n4609:\t\t\treturn ERR_PTR(-EBUSY);\n4610:\t\t}\n4611:\t\n4612:\t\t/*\n4613:\t\t * If no process or bpffs is holding a reference to the map, no new callbacks should be\n4614:\t\t * scheduled. This does not address any race or correctness issue, but rather is a policy\n4615:\t\t * choice: dropping user references should stop everything.\n4616:\t\t */\n4617:\t\tif (!atomic64_read(\u0026map-\u003eusercnt)) {\n4618:\t\t\t/* drop ref we just got for task_work callback itself */\n4619:\t\t\tbpf_task_work_ctx_put(ctx);\n4620:\t\t\t/* transfer map's ref into cancel_and_free() */\n4621:\t\t\tbpf_task_work_cancel_and_free(tw);\n4622:\t\t\treturn ERR_PTR(-EBUSY);\n4623:\t\t}\n4624:\t\n4625:\t\treturn ctx;\n4626:\t}\n4627:\t\n4628:\tstatic int bpf_task_work_schedule(struct task_struct *task, struct bpf_task_work *tw,\n4629:\t\t\t\t\t struct bpf_map *map, void *callback_fn,\n4630:\t\t\t\t\t struct bpf_prog_aux *aux, enum task_work_notify_mode mode)\n4631:\t{\n4632:\t\tstruct bpf_prog *prog;\n4633:\t\tstruct bpf_task_work_ctx *ctx;\n4634:\t\tint err;\n4635:\t\n4636:\t\tBTF_TYPE_EMIT(struct bpf_task_work);\n4637:\t\n4638:\t\tprog = bpf_prog_inc_not_zero(aux-\u003eprog);\n4639:\t\tif (IS_ERR(prog))\n4640:\t\t\treturn -EBADF;\n4641:\t\ttask = bpf_task_acquire(task);\n4642:\t\tif (!task) {\n4643:\t\t\terr = -EBADF;\n4644:\t\t\tgoto release_prog;\n4645:\t\t}\n4646:\t\n4647:\t\tctx = bpf_task_work_acquire_ctx(tw, map);\n4648:\t\tif (IS_ERR(ctx)) {\n4649:\t\t\terr = PTR_ERR(ctx);\n4650:\t\t\tgoto release_all;\n4651:\t\t}\n4652:\t\n4653:\t\tctx-\u003etask = task;\n4654:\t\tctx-\u003ecallback_fn = (bpf_callback_t)callback_fn;\n4655:\t\tctx-\u003eprog = prog;\n4656:\t\tctx-\u003emode = mode;\n4657:\t\tctx-\u003emap = map;\n4658:\t\tctx-\u003emap_val = (void *)tw - map-\u003erecord-\u003etask_work_off;\n4659:\t\tinit_task_work(\u0026ctx-\u003ework, bpf_task_work_callback);\n"
} |
| 49/3 | 2026/10/12 06:41 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 24899 output: 289 thoughts: 1573 Thoughts: **Analysis of BPF Task Work Patch and KMSAN Applicability** Okay, let's break this down. The core of this patch seems to be addressing concurrency issues within the `bpf_task_work` mechanism, specifically around the lifecycle and synchronization of the `bpf_task_work_ctx` structure. Initially, `bpf_task_work_cancel` had a potential race because it accessed `ctx->task` without proper synchronization, which could be cleared concurrently. The same issues can arise from `ctx` being re-initialized and queued before the prior irq\_work has completed. My immediate concern is whether this touches on any uninitialized memory vulnerabilities that KMSAN would care about. Let's run through the checks. 1. *Kernel structures sent to userspace*: Nope. No evidence of data leaks or copying kernel data to user space. This is all internal kernel synchronization logic. 2. *Conditional logic dependent on uninitialized variables*: The code uses `memset(ctx, 0, sizeof(*ctx))` to initialize the context, and `ctx->task` is initialized right before use. The other fields are also initialized before being accessed. Everything is being initialized, so no issues there. 3. *Allocation of complex data structures with uninitialized fields*: No complex structures. The context is allocated, zeroed, and populated in a clear, straightforward manner. 4. *Bounds checks, lengths, etc., exposing uninitialized bytes*: No buffer overruns or anything that could reveal uninitialized memory. This is all about synchronization and state management. The races being fixed are UAF/NULL dereference related, related to concurrent access to `ctx->task` between the irq handler and the callback, as well as reusing `ctx->irq_work` before the previous irq\_work has finished. These races can lead to classic concurrency issues like use-after-free, double-free, and NULL dereference. Standard bug detectors and KASAN will detect those issues. KMSAN is there to catch reads of uninitialized memory, but this patch doesn't create any. The self-tests added are good, but they are userspace tests, testing the behavior of the kernel's BPF functionality. Looking at the code again, the key operations are `kmalloc`, `memset`, and explicit initialization of fields. Nothing is being left to chance, and there's no way uninitialized memory is being read or exposed. Therefore, after a thorough review, `NeedsKMSAN` is definitively `false`. The primary focus of the patch is on concurrency bugs and memory safety issues that are addressed by KASAN and standard sanitizers, not by KMSAN. |
| 50/3 | 2026/10/12 06:41 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch addresses race conditions in the BPF task_work scheduling infrastructure (kernel/bpf/helpers.c). Specifically, it ensures that:\n1. The `task_struct` pointer is captured prior to publishing the task work, preventing a race where the callback runs concurrently, resets `ctx-\u003etask`, and causes a NULL pointer dereference or use-after-free in `bpf_task_work_cancel()`.\n2. `init_irq_work()` is moved to initialization time in `bpf_task_work_fetch_ctx()` rather than per-schedule, and `irq_work_is_busy()` is checked before reusing the context to prevent re-enqueueing or reinitializing an active `irq_work`.\n\nAll relevant memory structures (`struct bpf_task_work_ctx`) are fully zeroed upon allocation via `memset(ctx, 0, sizeof(*ctx))` and properly initialized. No uninitialized memory is read, branched on, or copied to user space. The risks addressed by these changes involve lifetime, concurrency, and pointer validity (use-after-free and NULL pointer dereference), which are thoroughly monitored by standard KASAN and lock/debugging infrastructure. A dedicated KMSAN session is not warranted.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch addresses race conditions in the BPF task_work scheduling infrastructure (kernel/bpf/helpers.c). Specifically, it ensures that:\n1. The `task_struct` pointer is captured prior to publishing the task work, preventing a race where the callback runs concurrently, resets `ctx-\u003etask`, and causes a NULL pointer dereference or use-after-free in `bpf_task_work_cancel()`.\n2. `init_irq_work()` is moved to initialization time in `bpf_task_work_fetch_ctx()` rather than per-schedule, and `irq_work_is_busy()` is checked before reusing the context to prevent re-enqueueing or reinitializing an active `irq_work`.\n\nAll relevant memory structures (`struct bpf_task_work_ctx`) are fully zeroed upon allocation via `memset(ctx, 0, sizeof(*ctx))` and properly initialized. No uninitialized memory is read, branched on, or copied to user space. The risks addressed by these changes involve lifetime, concurrency, and pointer validity (use-after-free and NULL pointer dereference), which are thoroughly monitored by standard KASAN and lock/debugging infrastructure. A dedicated KMSAN session is not warranted.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|