diff options
| author | Alexei Starovoitov <ast@kernel.org> | 2026-10-16 00:39:49 +0000 |
|---|---|---|
| committer | Alexei Starovoitov <ast@kernel.org> | 2026-10-16 00:39:49 +0000 |
| commit | 257d4bce6a75eb68c8786ce6761f4773e3bf078c (patch) | |
| tree | 1d242466b9f307c0944cd982e5b5b9a43350c854 /scripts/Makefile.thinlto | |
| parent | 70024d04ea27a92132eb83b12911dc8de2bfad4b (diff) | |
| parent | 33b2c64f073c08c93433f71f78586e7571ff3a13 (diff) | |
Merge branch 'bpf-fix-task-work-scheduling-races'
Yun Lu says:
====================
bpf: Fix task work scheduling races
From: Yun Lu <luyun@kylinos.cn>
This series fixes two races in the bpf_task_work scheduling kfuncs and
adds a regression test for each. Each fix is followed by its test.
Patch 1 captures the task pointer before publishing the callback, which
can clear ctx->task before the scheduling handler attempts cancellation.
The existing RCU read-side section keeps the captured task alive because
bpf_task_release() is put_task_struct_rcu_user().
Patch 2 adds a 1500-round cross-CPU stress test for the cancellation
races: context reuse and map-value deletion racing with scheduling or
callback completion. It does not force the exact faulting interleavings.
Patch 3 rejects context reuse while the previous scheduling irq_work is
still BUSY. Otherwise a delayed handler can consume the next round's
SCHEDULING state and leave a false SCHEDULED state with a NULL task.
Patch 4 adds a separate test for that BUSY gate. A bounded fexit wait on
task_work_add() holds the real scheduling handler after publication.
The target's next syscall attempts context reuse after its callback
wrapper has returned. Observing its acknowledgment inside the fexit
wait proves the attempt occurred while the old irq_work was still BUSY.
Check rejection with -EBUSY, bounded recovery and exactly-once delivery.
Missed windows are retried within a bounded attempt budget.
Changes since v6 (patch 4 only):
- Skip on PREEMPT_RT through the CONFIG_PREEMPT_RT kconfig extern
read from the skeleton before attaching, instead of the
/sys/kernel/realtime probe and the irq_work thread-name detection.
- Share the status accessors, sequence polling, polling bounds and
the userspace value layout with the cancellation race test.
- Drop the auxiliary stats map, the loop-count diagnostic and the
wall-clock assertion; the elapsed time is read once after the
worker cleanup, purely as a diagnostic. Publish every first-round
scheduling result, including -EBUSY, and drop the redundant
userspace CB1 wait and the pacing-only inter-attempt sleep.
The core window protocol and its assertions are unchanged.
v6: https://lore.kernel.org/all/20261010042132.137430-1-luyun_611@163.com/
Changes since v5:
- Narrow the race test to the cancellation races it covers and add the
task_work_busy test: a bounded fexit wait on task_work_add() holds
the real window while the target thread attempts reuse after its
callback wrapper returned, asserting -EBUSY, bounded recovery and
exactly-once delivery.
- Give each attempt dedicated workers and a fresh map value; reset the
shared state before the workers are enabled and reuse it only after
their TIDs have left PID lookup.
- Skip on fewer than two CPUs or confirmed PREEMPT_RT. Reorder the
series so each fix is followed by its test. Kernel patches
unchanged.
v5: https://lore.kernel.org/all/20260930085417.186729-1-luyun_611@163.com/
Changes since v4 (selftest only; patches 1 and 2 are unchanged):
- Rework the commit message to map each variant to the races fixed by
this series, and note that the stress test does not force the exact
faulting interleavings.
- Merge the trigger and target threads into one worker function, name
the polling bounds, and accept only -ENOENT in the deletion-race
variant (-EBUSY is retried inside the BPF program and never
published to userspace).
v4: https://lore.kernel.org/all/20260929072800.113129-1-luyun_611@163.com/
Changes since v3:
- Drop the get_task_struct()/put_task_struct() pair: capturing the
pointer before task_work_add() is sufficient, since
bpf_task_release() is put_task_struct_rcu_user() and the handler's
RCU read-side section keeps the captured task alive. Reword the
RCU explanation.
- Split the BUSY check and the one-time irq_work initialization into
a separate patch 2, with the full interleaving and the failure it
prevents.
- Selftest code unchanged; commit message wording only.
v3: https://lore.kernel.org/all/20260922100042.136818-1-luyun_611@163.com/
Changes since v2:
- Kernel: drop irq_work_sync() from the callback and both
WARN_ON_ONCE() checks, as requested in review. Following the
suggested direction, the scheduling handler now takes its own task
reference around task_work_add() and the post-add cancellation; the
temporary ctx refcount from v2 is gone as well. The callback ends
up unchanged from upstream.
- The BUSY check in acquire is kept: without it a handler delayed
between task_work_add() and its state cmpxchg can flip the next
round into a bogus SCHEDULED, which later routes map-value deletion
into task_work_cancel() with an already-reset ctx->task.
- Selftest is unchanged apart from the rebase.
v2: https://lore.kernel.org/all/20260921101453.69273-1-luyun_611@163.com/
Changes since v1:
- Kernel: rework the fix per review feedback. Replace the round_refs
ownership counter and the extra irq_work objects with a completion
edge: the callback claims RUNNING and waits for the scheduling
irq_work via irq_work_sync(), plus one temporary reference from the
existing ctx refcount and a BUSY check covering the add-failure
path. No context fields or states are added.
- Selftest: address review feedback (single packed result store,
bounded thread-start wait, tolerate the expected deletion-race
outcomes).
v1: https://lore.kernel.org/bpf/20260918080014.54012-1-luyun_611@163.com/
Testing:
- QEMU/KVM on x86-64 with KASAN, lockdep, PROVE_RCU and
DEBUG_ATOMIC_SLEEP: task_work_busy passed three ordinary runs and
three under target-CPU contention, with missed windows retried
within the attempt budget; the cancellation race test passed as
well.
- Without the BUSY fix the same binary fails the in-window rejection
assertion as expected. A PREEMPT_RT kernel skips before attaching,
and a tracefs instance without the global function listing still
attaches and passes.
====================
Link: https://patch.msgid.link/20261012061915.231254-1-luyun_611@163.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Diffstat (limited to 'scripts/Makefile.thinlto')
0 files changed, 0 insertions, 0 deletions
