[PATCH] rcu-tasks: Raise ->percpu_dequeue_lim before enqueueing on new queues
From: Andrea Parri
Date: Tue Dec 01 2026 - 12:49:11 EST
When call_rcu_tasks_generic() sees enough lock contention, it switches
to per-CPU callback queues by setting ->percpu_enqueue_shift to zero
and then raising ->percpu_dequeue_lim to rcu_task_cpu_ids. Between the
two stores, another CPU can enqueue onto its own queue while the scans
in rcu_barrier_tasks_generic() and rcu_tasks_need_gpcb() still stop
at CPU 0:
CPU 0 CPU 1
----- -----
call_rcu_tasks_generic()
->percpu_enqueue_shift = 0
call_rcu_tasks(rhp, cb)
enqueue on CPU 1's queue
rcu_barrier_tasks()
->percpu_dequeue_lim == 1
entrain on CPU 0's queue only
->percpu_dequeue_lim = rcu_task_cpu_ids
rcu_barrier_tasks() can then return before cb() is invoked, so a
caller that frees what cb() uses, or unloads the module containing
cb(), could have cb() run afterwards. rcu_tasks_need_gpcb() can
likewise skip CPU 1's queue and delay the callback's grace period.
Raise ->percpu_dequeue_lim first and clear ->percpu_enqueue_shift with
smp_store_release(), so the scans cover a queue before any CPU can
pick it. The READ_ONCE() of the shift then needs no acquire.
For rcu_barrier_tasks_generic(), the full ordering of
rcu_segcblist_inc_len() on enqueue puts the load of the shift before
the barrier's load of ->percpu_dequeue_lim, or of ->barrier_q_seq when
rcu_seq_done() makes it rely on a concurrent barrier.
rcu_tasks_need_gpcb() loads the limit after rcuwait barrier (A), which
pairs with (B) in rcuwait_wake_up().
With a test-only 1000 ms mdelay() between the two stores, an 8-CPU
guest flooding call_rcu_tasks() from CPUs 2-7, with CPU 1 looping on
call_rcu_tasks() and rcu_barrier_tasks(), saw the barrier return
early 7 times in 1498 iterations; with this patch, 0 times in 1372.
Without the delay, the unpatched kernel showed no failure in 1128.
The bug was found with the help of an LLM, which was also used to write
the fix, the changelog and the tests.
Fixes: 00a8b4b54cd6 ("rcu-tasks: Set ->percpu_enqueue_shift to zero upon contention")
Signed-off-by: Andrea Parri <parri.andrea@xxxxxxxxx>
---
The existing smp_store_release() of ->percpu_enqueue_lim no longer pairs
with any acquire: commit 2cee0789b458 ("rcu-tasks: Use separate
->percpu_dequeue_lim for callback dequeueing") moved the
smp_load_acquire() calls to ->percpu_dequeue_lim, and the remaining
readers, apart from diagnostics, either hold ->cbs_gbl_lock or only use
the value to decide whether to take that lock and check again. This
patch leaves it, and the one in the collapse path, unchanged to keep
the fix minimal. I can send a follow-up that turns both into
WRITE_ONCE() if that is wanted.
The test numbers in the changelog come from the out-of-tree module and
the test-only delay listed below, run on x86_64 under virtme-ng with 8
vCPUs. The kernel was configured with "vng --kconfig" plus
CONFIG_RCU_EXPERT=y, CONFIG_FORCE_TASKS_RCU=y, CONFIG_DYNAMIC_DEBUG=y,
CONFIG_DETECT_HUNG_TASK=y and CONFIG_RCU_TORTURE_TEST=y (the boot-time
rcutorture run provided background load), and booted with:
update.dyndbg=+p
rcupdate.rcu_task_contend_lim=0
rcupdate.rcu_task_expand_delay_ms=1000
followed by "insmod rtx_test.ko duration_s=1200" and a check of the
"rtx_test: done: checks=N early_barrier_returns=M" line. A contention
limit of 0 makes the queues expand on any lock contention, and
update.dyndbg=+p prints the "Switching RCU Tasks to per-CPU callback
queuing." message for each expansion: the two 1200-second runs saw
390 and 384. The no-delay run omits rcupdate.rcu_task_expand_delay_ms
and uses duration_s=600.
Test-only delay, shown on top of this patch. On v7.4-rc5 the mdelay()
goes between the two WRITE_ONCE() calls instead:
diff --git a/kernel/rcu/tasks.h b/kernel/rcu/tasks.h
index 14ad1c84a4057..bb0d36b281fa2 100644
--- a/kernel/rcu/tasks.h
+++ b/kernel/rcu/tasks.h
@@ -180,6 +180,8 @@ static int rcu_task_collapse_lim __read_mostly = 10;
module_param(rcu_task_collapse_lim, int, 0444);
static int rcu_task_lazy_lim __read_mostly = 32;
module_param(rcu_task_lazy_lim, int, 0444);
+static int rcu_task_expand_delay_ms; /* TEST ONLY */
+module_param(rcu_task_expand_delay_ms, int, 0644);
static int rcu_task_cpu_ids;
@@ -397,6 +399,8 @@ static void call_rcu_tasks_generic(struct rcu_head *rhp, rcu_callback_t func,
// the full ordering of rcu_segcblist_inc_len() when
// the callback is queued; for rcu_tasks_need_gpcb(),
// with barriers (A) and (B) of rcuwait.
+ if (rcu_task_expand_delay_ms) /* TEST ONLY: widen race window. */
+ mdelay(rcu_task_expand_delay_ms);
smp_store_release(&rtp->percpu_enqueue_shift, 0);
smp_store_release(&rtp->percpu_enqueue_lim, rcu_task_cpu_ids);
pr_debug("Switching %s to per-CPU callback queuing.\n", rtp->name);
Test module (rtx_test.c, built with "obj-m := rtx_test.o"):
// SPDX-License-Identifier: GPL-2.0
/*
* Test for the RCU Tasks queue-expansion race in call_rcu_tasks_generic():
* ->percpu_enqueue_shift is set to 0 before ->percpu_dequeue_lim is raised,
* so a callback can land on a per-CPU queue that rcu_barrier_tasks() and
* rcu_tasks_need_gpcb() do not yet scan.
*
* Flooders on CPUs >= 2 generate bursts of call_rcu_tasks() to trigger
* contention (expansion), then go quiet so the queues collapse again.
* The checker on CPU 1 repeatedly does call_rcu_tasks() + rcu_barrier_tasks()
* and verifies that its callback was invoked before the barrier returned.
*/
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/rcupdate.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/atomic.h>
#include <linux/sched.h>
static int burst_ms = 200;
module_param(burst_ms, int, 0444);
static int quiet_ms = 1500;
module_param(quiet_ms, int, 0444);
static int duration_s = 120;
module_param(duration_s, int, 0444);
static struct task_struct *checker_task;
static struct task_struct *flood_tasks[NR_CPUS];
static atomic_long_t flood_cbs_outstanding;
static atomic_long_t n_flood_cbs;
static unsigned long n_checks, n_early;
static unsigned long end_jiffies;
static void flood_cb(struct rcu_head *rhp)
{
kfree(rhp);
atomic_long_dec(&flood_cbs_outstanding);
}
static int flood_fn(void *arg)
{
while (!kthread_should_stop() && time_before(jiffies, end_jiffies)) {
unsigned long period = msecs_to_jiffies(burst_ms + quiet_ms);
unsigned long phase = jiffies % period;
if (phase < msecs_to_jiffies(burst_ms) &&
atomic_long_read(&flood_cbs_outstanding) < 1000000) {
struct rcu_head *rhp = kmalloc(sizeof(*rhp), GFP_KERNEL);
if (rhp) {
atomic_long_inc(&flood_cbs_outstanding);
call_rcu_tasks(rhp, flood_cb);
atomic_long_inc(&n_flood_cbs);
}
cond_resched();
} else {
schedule_timeout_interruptible(1);
}
}
while (!kthread_should_stop())
schedule_timeout_interruptible(HZ);
return 0;
}
struct check_head {
struct rcu_head rh;
int invoked;
};
static void check_cb(struct rcu_head *rhp)
{
struct check_head *ch = container_of(rhp, struct check_head, rh);
smp_store_release(&ch->invoked, 1);
}
static int checker_fn(void *arg)
{
struct check_head ch;
while (!kthread_should_stop() && time_before(jiffies, end_jiffies)) {
ch.invoked = 0;
call_rcu_tasks(&ch.rh, check_cb);
rcu_barrier_tasks();
n_checks++;
if (!smp_load_acquire(&ch.invoked)) {
unsigned long t0 = jiffies;
n_early++;
while (!smp_load_acquire(&ch.invoked))
schedule_timeout_interruptible(1);
pr_err("rtx_test: rcu_barrier_tasks() returned before callback invoked (check %lu, early #%lu, cb invoked %u ms later)\n",
n_checks, n_early, jiffies_to_msecs(jiffies - t0));
}
}
pr_info("rtx_test: done: checks=%lu early_barrier_returns=%lu flood_cbs=%ld\n",
n_checks, n_early, atomic_long_read(&n_flood_cbs));
while (!kthread_should_stop())
schedule_timeout_interruptible(HZ);
return 0;
}
static int __init rtx_init(void)
{
int cpu;
end_jiffies = jiffies + duration_s * HZ;
/* Create the checker first: on failure no flooder has started. */
checker_task = kthread_create_on_cpu(checker_fn, NULL, 1, "rtx_check/%u");
if (IS_ERR(checker_task))
return PTR_ERR(checker_task);
for_each_online_cpu(cpu) {
if (cpu < 2)
continue;
flood_tasks[cpu] = kthread_create_on_cpu(flood_fn, NULL, cpu,
"rtx_flood/%u");
if (!IS_ERR(flood_tasks[cpu]))
wake_up_process(flood_tasks[cpu]);
else
flood_tasks[cpu] = NULL;
}
wake_up_process(checker_task);
return 0;
}
static void __exit rtx_exit(void)
{
int cpu;
kthread_stop(checker_task);
for_each_online_cpu(cpu)
if (flood_tasks[cpu])
kthread_stop(flood_tasks[cpu]);
rcu_barrier_tasks();
}
module_init(rtx_init);
module_exit(rtx_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("RCU Tasks queue-expansion race test");
The ordering was also checked with herd7 (tools/memory-model,
linux-kernel.cfg). With this patch, the outcome where CPU 1 sees the
zero shift but the old ->percpu_dequeue_lim is still used is "Never"
for rcu_barrier_tasks() after call_rcu_tasks(), both when it scans the
queues itself and when rcu_seq_done() makes it rely on a concurrent
barrier, and for rcu_tasks_need_gpcb() when rcuwait_wake_up() finds no
waiter. When it finds one, wake_up_process() makes the kthread scan
again. With the current store order, all three are "Sometimes". The
early-return case is also "Sometimes" with this patch if the model
leaves out the full ordering of rcu_segcblist_inc_len(). The three
tests with this patch are:
C rtx-same-task
(*
* P0: expansion with fix (dequeue_lim first, then release shift).
* P1: call_rcu_tasks() reads shift and enqueues (full ordering of
* rcu_segcblist_inc_len()), then rcu_barrier_tasks() scans:
* smp_load_acquire(dequeue_lim).
*)
{
lim=1;
shift=3;
}
P0(int *lim, int *shift)
{
WRITE_ONCE(*lim, 8);
smp_store_release(shift, 0);
}
P1(int *lim, int *shift)
{
int r0;
int r1;
r0 = READ_ONCE(*shift);
smp_mb();
r1 = smp_load_acquire(lim);
}
exists (1:r0=0 /\ 1:r1=1)
C rtx-barrier-fastpath
(*
* P0: expansion with fix (dequeue_lim first, then release shift).
* P1: call_rcu_tasks() reads shift and enqueues (full ordering of
* rcu_segcblist_inc_len()), then rcu_barrier_tasks() reads
* barrier_q_seq in rcu_seq_snap().
* P2: a concurrent rcu_barrier_tasks(): rcu_seq_start() (store, then
* smp_mb()), then smp_load_acquire(dequeue_lim).
* Bad outcome: P1 used a per-CPU queue and its snapshot predates P2's
* start, so P1 returns early via rcu_seq_done() and relies on P2's
* scan, yet P2 scanned with the old dequeue_lim.
*)
{
lim=1;
shift=3;
seq=0;
}
P0(int *lim, int *shift)
{
WRITE_ONCE(*lim, 8);
smp_store_release(shift, 0);
}
P1(int *shift, int *seq)
{
int r0;
int r1;
r0 = READ_ONCE(*shift);
smp_mb();
r1 = READ_ONCE(*seq);
}
P2(int *lim, int *seq)
{
int r2;
WRITE_ONCE(*seq, 1);
smp_mb();
r2 = smp_load_acquire(lim);
}
exists (1:r0=0 /\ 1:r1=0 /\ 2:r2=1)
C rtx-kthread
(*
* P0: expansion with fix.
* P1: call_rcu_tasks() on CPU 1: reads shift, enqueues on queue 1,
* later rcuwait_wake_up(): smp_mb() (B), then reads w->task.
* P2: rcu_tasks kthread: registers/sets state, smp_mb(), then
* rcu_tasks_need_gpcb() reads dequeue_lim.
* Bad outcome: P1 used per-CPU queue, saw no sleeper to wake, and the
* kthread scanned with the old dequeue_lim.
*)
{
lim=1;
shift=3;
task=0;
}
P0(int *lim, int *shift)
{
WRITE_ONCE(*lim, 8);
smp_store_release(shift, 0);
}
P1(int *shift, int *task)
{
int r0;
int r1;
r0 = READ_ONCE(*shift);
smp_mb();
r1 = READ_ONCE(*task);
}
P2(int *lim, int *task)
{
int r2;
WRITE_ONCE(*task, 1);
smp_mb();
r2 = smp_load_acquire(lim);
}
exists (1:r0=0 /\ 1:r1=0 /\ 2:r2=1)
kernel/rcu/tasks.h | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/kernel/rcu/tasks.h b/kernel/rcu/tasks.h
index 743f777b4c18f..14ad1c84a4057 100644
--- a/kernel/rcu/tasks.h
+++ b/kernel/rcu/tasks.h
@@ -353,6 +353,7 @@ static void call_rcu_tasks_generic(struct rcu_head *rhp, rcu_callback_t func,
rhp->func = func;
local_irq_save(flags);
rcu_read_lock();
+ // READ_ONCE() suffices, see the switch to per-CPU queuing below.
ideal_cpu = smp_processor_id() >> READ_ONCE(rtp->percpu_enqueue_shift);
chosen_cpu = cpumask_next(ideal_cpu - 1, cpu_possible_mask);
WARN_ON_ONCE(chosen_cpu >= rcu_task_cpu_ids);
@@ -387,8 +388,16 @@ static void call_rcu_tasks_generic(struct rcu_head *rhp, rcu_callback_t func,
if (unlikely(needadjust)) {
raw_spin_lock_irqsave(&rtp->cbs_gbl_lock, flags);
if (rtp->percpu_enqueue_lim != rcu_task_cpu_ids) {
- WRITE_ONCE(rtp->percpu_enqueue_shift, 0);
WRITE_ONCE(rtp->percpu_dequeue_lim, rcu_task_cpu_ids);
+ // Clear the shift only after raising the dequeue
+ // limit, so that rcu_barrier_tasks_generic() and
+ // rcu_tasks_need_gpcb() scan every queue that a CPU
+ // can pick once it sees a zero shift. For
+ // rcu_barrier_tasks_generic(), the release pairs with
+ // the full ordering of rcu_segcblist_inc_len() when
+ // the callback is queued; for rcu_tasks_need_gpcb(),
+ // with barriers (A) and (B) of rcuwait.
+ smp_store_release(&rtp->percpu_enqueue_shift, 0);
smp_store_release(&rtp->percpu_enqueue_lim, rcu_task_cpu_ids);
pr_debug("Switching %s to per-CPU callback queuing.\n", rtp->name);
}
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
--
2.53.0