Skip to contentSkip to conversation

srcu: Make srcu_barrier() wait for nested deferred callbacks

PATCH New Andrea Parri 2 participants 2 messages Dec 1

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 srcu: Make srcu_barrier() wait for nested deferred callbacks

With interrupts disabled, __call_srcu() defers a callback in two steps:
it adds the callback to the srcu_data's ->defer_cbs and, if that list was empty, chains the srcu_data onto the per-CPU srcu_defer list. srcu_defer_flush() walks only the per-CPU list, so it misses callbacks whose srcu_data is not chained yet.

Commit 35a26d87042b ("srcu: Make call_srcu() safe to call from any context") accepts this because only a callback whose call_srcu() has not returned can be missed. A call_srcu() nested between the steps, from an NMI or a kprobe, returns unchained, so srcu_barrier() can return before its callback runs:

CPU 0 CPU 1
local_irq_disable()
call_srcu(ssp, rhp1, f1)

    llist_add(rhp1, &sdp->defer_cbs)
    // true, ->defer_cbs was empty
    <NMI or kprobe>
    call_srcu(ssp, rhp2, f2)
      llist_add(rhp2, &sdp->defer_cbs)
      // false, so return unchained
    tell CPU 1 that rhp2 is queued
                                        srcu_barrier(ssp)
                                          srcu_defer_flush()
                                          // CPU 0's list is empty
                                        // returns, rhp2 not invoked
                                        free what f2() uses
    </NMI or kprobe>
    llist_add(&sdp->defer_link, ...)

...
f2(rhp2) // use-after-free

A test module reproduces this on x86 with a kprobe between the two llist_add() calls, whose int3 handler calls call_srcu() and spins while another CPU calls srcu_barrier(). That returns after 58us, the nested callback runs 3.3ms later, and freeing an object after srcu_barrier() gives:

  BUG: KASAN: slab-use-after-free in func2+0x6d/0x70 [srcu_defer_nmi]
  Read of size 8 at addr ffff88800247ebc0 by task kworker/0:1/13
  Workqueue: rcu_gp srcu_invoke_callbacks
  Call Trace:
   func2+0x6d/0x70 [srcu_defer_nmi]
   srcu_invoke_callbacks+0x360/0x5c0
   process_one_work+0x8d5/0x1b50
   worker_thread+0x52d/0xec0
   kthread+0x312/0x410
  Allocated by task 221:
   srcu_defer_nmi_init+0xba/0xff0 [srcu_defer_nmi]
  Freed by task 222:
   kfree+0x20b/0x520
   barrier_fn+0x94/0xa0 [srcu_defer_nmi]

No in-tree caller is known to hit this. The window spans only the ->ssp store and the second llist_add(), and the barrier callers checked wait for the interrupted irqs-disabled region to end before their barrier.

kernel/bpf/memalloc.c and kernel/bpf/bpf_local_storage.c call synchronize_rcu() before rcu_barrier_tasks_trace(). rcutorture removes its NMI perf events from each CPU's hotplug thread before its barrier.

The nested call_srcu() itself is reachable from in-tree code: BPF programs run from perf NMIs and kprobes can call bpf_call_rcu_tasks_trace(), which ends in call_srcu(), and srcu_barrier() is documented to wait for all in-flight call_srcu() callbacks.

Fix this by having srcu_barrier() wait for an IPI to each CPU whose ->defer_cbs for this srcu_struct is still non-empty after srcu_defer_flush(). The interrupted staging runs with interrupts disabled, so the IPI completes only after the srcu_data is chained, and a second drain re-issues the callback.

This does not depend on how the call_srcu() was nested. The IPI is sent only when the flush leaves callbacks of this srcu_struct staged on a CPU, that is, after a split staging or a call_srcu() racing with srcu_barrier().

srcu_barrier() checks ->defer_cbs under srcu_defer->lock. A drainer takes callbacks off that list and re-issues them under the same lock, so an empty list seen under that lock cannot hide a callback that is not yet enqueued.

A CPU going offline leaves cpu_online_mask in take_cpu_down() but runs until it reports itself dead from the idle loop, and smp_call_function_single() returns -ENXIO for it without waiting. For such a CPU, srcu_barrier() drains and sleeps until its ->defer_cbs is empty. That ends: a dead CPU cannot be part-way through staging.

rcu_barrier() uses the same pattern: a waiting smp_call_function_single() to each CPU, and a sleep and retry when that fails for a CPU going offline.

Draining ->defer_cbs directly from srcu_barrier() would leave the srcu_data chained with an empty list, and the next call_srcu() would chain it a second time. A separate staging lane for NMIs would miss kprobes on arm64, powerpc and s390, where in_nmi() is false in the breakpoint handler.

synchronize_rcu() in srcu_barrier() would also work, but adds an RCU grace period to every barrier.

The bug was found with the help of an LLM, which was also used to write the fix, the changelog and the test.

Fixes 35a26d87042b ("srcu: Make call_srcu() safe to call from any context")

---
Based on v7.4-rc5. It also applies cleanly to paulmck/dev.

Tested in a virtme-ng guest (x86_64, 4 vCPUs, srcutree.convert_to_big=0) on a kernel with KASAN, PROVE_LOCKING (with PROVE_RAW_LOCK_NESTING), DEBUG_ATOMIC_SLEEP, PROVE_RCU and CSD_LOCK_WAIT_DEBUG, using the module below. It needs kprobe optimization disabled (sysctl debug.kprobes-optimization=0) and probe_off set to the first instruction of the branch taken when the ->defer_cbs llist_add() returns true, read from objdump of __call_srcu (__call_srcu+0x264 here, with and without this patch).

unpatched:
srcu_defer_nmi: probe fired=1 cpu=1 in_nmi=1 irqs_disabled=1 spun=1ms
srcu_defer_nmi: nested call_srcu() returned, then srcu_barrier() returned after 58us
srcu_defer_nmi: FAIL: srcu_barrier() returned before func2; func2 ran 3285us later uaf=1: BUG: KASAN: slab-use-after-free in func2+0x6d/0x70 [srcu_defer_nmi]

patched:
srcu_defer_nmi: probe fired=1 cpu=1 in_nmi=1 irqs_disabled=1 spun=2000ms
srcu_defer_nmi: nested call_srcu() returned, then srcu_barrier() returned after 2003049us
srcu_defer_nmi: PASS: func2 ran before srcu_barrier() returned
uaf=1: PASS, func2 reads valid memory, no KASAN report

With this patch, srcu_barrier() waits for the kprobe handler to return, and the handler waits for srcu_barrier(), so the handler spins until its 2s timeout. No lockdep, sleeping-in-atomic, RCU or csd-lock warnings were reported.

A second module, which I can send if useful, covers the fallback for a CPU going offline. It takes a CPU down, splits a staging on that CPU from cpuhp_report_idle_dead(), after the CPU has left cpu_online_mask, and calls srcu_barrier() from another CPU. With this patch, srcu_barrier() slept and retried 25-26 times after failed IPIs and returned only after the nested callback ran, in 3 of 3 runs, again with no warnings. Without it, srcu_barrier() returned after 8-301us and the callback ran about 53-55ms later.

srcu_barrier() latency on a private srcu_struct, 16 online vCPUs, no debug options, median p50 over 10 rounds from two boots of each kernel:

idle (no callback, no grace period), 16 possible CPUs: 0.56us -> 0.90us
idle (no callback, no grace period), 255 possible CPUs: 21.25us -> 31.89us
after call_srcu(), irqs enabled or deferred: 3.95-3.97ms, unchanged

The extra scan is most visible in the idle benchmark: srcu_barrier() then posts no callbacks and waits for no grace period, so the numbers isolate the cost of one lock and unlock per possible CPU. With a callback pending, the median was essentially unchanged. These runs do not cover the IPI wait or the retry for a CPU going offline, which add latency only when callbacks are still staged after the flush. I can send the benchmark module if useful.

rcutorture on v7.4-rc5 plus this patch, 15 minutes per scenario, all ending in SUCCESS with no warnings, stalls or barrier failures:

SRCU-N ------- 30054 GPs (33.3933/s) [srcu: g297728 f0x0 total-gps=74533] n_max_cbs: 150000 SRCU-P ------- 3738 GPs (4.15333/s) [srcud: g54596 f0x0 total-gps=13750] n_max_cbs: 150000 TRACE01 ------- 22133 GPs (24.5922/s) [tasks-tracing: g0 f0x0 total-gps=0] n_max_cbs: 50000 TRACE02 ------- 39035 GPs (43.3722/s) [tasks-tracing: g0 f0x0 total-gps=0] n_max_cbs: 100000

The guest has no PMU, so rcutorture's NMI ->call() exerciser does not run there (nmi-calls: 0) and nested staging is exercised only by the two modules.

// SPDX-License-Identifier: GPL-2.0
/*
* Reproducer for the two-step deferred call_srcu() staging race. *
* A stager kthread on @stage_cpu calls call_srcu(rhp1) with IRQs disabled,
* which takes the deferred path. A kprobe placed inside the window between
* llist_add(rhp, &sdp->defer_cbs) and llist_add(&sdp->defer_link, &sndp->list)
* fires there. On x86 a kernel int3 enters via irqentry_nmi_enter(), so the
* handler runs with in_nmi() and irqs_disabled() true, standing in for an NMI.
* The handler calls call_srcu(rhp2), which returns without chaining the
* srcu_data, signals a barrier kthread on @barrier_cpu, and spins until that
* kthread's srcu_barrier() has returned (or a timeout).
*
* srcu_barrier() is obliged to wait for rhp2, since call_srcu(rhp2) returned
* before srcu_barrier() was called. The module reports whether it did. With
* the fix, srcu_barrier() also waits for the handler to return, so the handler
* spins until the timeout.
*
* With uaf=1 the barrier kthread frees an object after srcu_barrier()
* returns, the way a teardown path would, and func2 dereferences it. *
* Parameters probe_sym/probe_off locate the window; see the disassembly. */
#include <linux/module.h>
#include <linux/kprobes.h>
#include <linux/kthread.h>
#include <linux/srcu.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/ktime.h>
#include <linux/completion.h>

  static char *probe_sym = "__call_srcu";
  module_param(probe_sym, charp, 0444);
  static unsigned int probe_off;
  module_param(probe_off, uint, 0444);
  static int stage_cpu = 1;
  module_param(stage_cpu, int, 0444);
  static int barrier_cpu = 2;
  module_param(barrier_cpu, int, 0444);
  static bool uaf;
  module_param(uaf, bool, 0444);
  static unsigned int spin_ms = 2000;
  module_param(spin_ms, uint, 0444);

  struct victim {
  	unsigned long magic;
  };

static struct srcu_struct test_srcu;
static struct rcu_head rhp1, rhp2;
static struct victim *victim;

  static int armed;
  static int nmi_done;
  static int barrier_done;
  static int func1_ran, func2_ran;
  static bool h_in_nmi, h_irqs_off, h_fired;
  static int h_cpu;
  static u64 t_nmi_call_ret, t_barrier_ret, t_func2;
  static bool func2_ran_at_barrier_ret;
  static unsigned int spun_ms;
  static DECLARE_COMPLETION(func2_done);
  static DECLARE_COMPLETION(barrier_thread_done);

  static void func1(struct rcu_head *rhp)
  {
  	WRITE_ONCE(func1_ran, 1);
  }

  static void func2(struct rcu_head *rhp)
  {
  	WRITE_ONCE(t_func2, ktime_get_mono_fast_ns());
  	if (uaf)
  		pr_info("srcu_defer_nmi: func2: victim->magic=%#lx\n", victim->magic);
  	smp_store_release(&func2_ran, 1);
  	complete(&func2_done);
  }

  static int kp_pre(struct kprobe *p, struct pt_regs *regs)
  {
  	struct srcu_data *sdp;
  	unsigned int i;

  	if (!READ_ONCE(armed) || smp_processor_id() != stage_cpu)
  		return 0;
  	/* Only fire for our own outer call_srcu(rhp1), mid-staging. */
  	sdp = this_cpu_ptr(test_srcu.sda);
  	if (READ_ONCE(sdp->defer_cbs.first) != (struct llist_node *)&rhp1)
  		return 0;
  	if (cmpxchg(&armed, 1, 0) != 1)
  		return 0;

  	h_fired = true;
  	h_cpu = smp_processor_id();
  	h_in_nmi = in_nmi();
  	h_irqs_off = irqs_disabled();

  	call_srcu(&test_srcu, &rhp2, func2);	/* the "NMI" call_srcu() */
  	t_nmi_call_ret = ktime_get_mono_fast_ns();
  	smp_store_release(&nmi_done, 1);	/* "call_srcu() returned" */

  	/* Hold the outer call_srcu() inside its window. */
  	for (i = 0; i < spin_ms && !smp_load_acquire(&barrier_done); i++)
  		mdelay(1);
  	spun_ms = i;
  	return 0;
  }

  static struct kprobe kp = {
  	.pre_handler = kp_pre,
  };

  static int barrier_fn(void *unused)
  {
  	while (!smp_load_acquire(&nmi_done)) {
  		if (kthread_should_stop())
  			goto out;
  		cpu_relax();
  	}
  	srcu_barrier(&test_srcu);
  	t_barrier_ret = ktime_get_mono_fast_ns();
  	func2_ran_at_barrier_ret = smp_load_acquire(&func2_ran);
  	if (uaf && !func2_ran_at_barrier_ret) {
  		/* "Teardown": srcu_barrier() said all callbacks are done. */
  		kfree(victim);
  	}
  	smp_store_release(&barrier_done, 1);
  out:
  	complete(&barrier_thread_done);
  	while (!kthread_should_stop())
  		schedule_timeout_interruptible(HZ / 10);
  	return 0;
  }

  static int stage_fn(void *unused)
  {
  	WRITE_ONCE(armed, 1);
  	local_irq_disable();
  	call_srcu(&test_srcu, &rhp1, func1);
  	local_irq_enable();
  	WRITE_ONCE(armed, 0);
  	while (!kthread_should_stop())
  		schedule_timeout_interruptible(HZ / 10);
  	return 0;
  }

  static int __init srcu_defer_nmi_init(void)
  {
  	struct task_struct *bt, *st;
  	int ret;

  	if (stage_cpu == barrier_cpu || !cpu_online(stage_cpu) ||
  	    !cpu_online(barrier_cpu)) {
  		pr_err("srcu_defer_nmi: need distinct online stage/barrier CPUs\n");
  		return -EINVAL;
  	}
  	if (stage_cpu == 0)	/* __boot_cpu_id is not exported; assume CPU 0 */
  		pr_warn("srcu_defer_nmi: stage_cpu is the boot CPU; small-mode srcu_barrier() may block on it\n");

  	victim = kmalloc(sizeof(*victim), GFP_KERNEL);
  	if (!victim)
  		return -ENOMEM;
  	victim->magic = 0x5ac1f1ceUL;

  	ret = init_srcu_struct(&test_srcu);
  	if (ret)
  		goto out_free;

  	kp.symbol_name = probe_sym;
  	kp.offset = probe_off;
  	ret = register_kprobe(&kp);
  	if (ret) {
  		pr_err("srcu_defer_nmi: register_kprobe(%s+%#x) = %d\n",
  		       probe_sym, probe_off, ret);
  		goto out_srcu;
  	}
  	pr_info("srcu_defer_nmi: kprobe at %s+%#x (%pS), optimized=%d\n",
  		probe_sym, probe_off, kp.addr, kprobe_optimized(&kp));

  	bt = kthread_create_on_cpu(barrier_fn, NULL, barrier_cpu, "srcu_dn_barrier");
  	if (IS_ERR(bt)) {
  		ret = PTR_ERR(bt);
  		goto out_kp;
  	}
  	st = kthread_create_on_cpu(stage_fn, NULL, stage_cpu, "srcu_dn_stage");
  	if (IS_ERR(st)) {
  		ret = PTR_ERR(st);
  		kthread_stop(bt);
  		goto out_kp;
  	}
  	wake_up_process(bt);
  	wake_up_process(st);

  	if (!wait_for_completion_timeout(&barrier_thread_done, 10 * HZ))
  		pr_err("srcu_defer_nmi: barrier thread did not finish\n");
  	if (!wait_for_completion_timeout(&func2_done, 10 * HZ))
  		pr_err("srcu_defer_nmi: func2 not invoked within 10s\n");
  	kthread_stop(st);
  	kthread_stop(bt);
  	unregister_kprobe(&kp);

  	pr_info("srcu_defer_nmi: probe fired=%d cpu=%d in_nmi=%d irqs_disabled=%d spun=%ums\n",
  		h_fired, h_cpu, h_in_nmi, h_irqs_off, spun_ms);
  	if (!h_fired) {
  		pr_err("srcu_defer_nmi: probe never fired in the window; check probe_off\n");
  	} else if (!READ_ONCE(barrier_done)) {
  		pr_info("srcu_defer_nmi: srcu_barrier() did not return\n");
  	} else {
  		pr_info("srcu_defer_nmi: nested call_srcu() returned, then srcu_barrier() returned after %lluus\n",
  			(t_barrier_ret - t_nmi_call_ret) / NSEC_PER_USEC);
  		/*
  		 * func2_ran is sampled after t_barrier_ret, so check func2's own
  		 * timestamp too: a func2 that ran in between is not a PASS.
  		 */
  		if (func2_ran_at_barrier_ret && READ_ONCE(t_func2) <= t_barrier_ret) {
  			pr_info("srcu_defer_nmi: PASS: func2 ran before srcu_barrier() returned\n");
  		} else if (smp_load_acquire(&func2_ran)) {
  			pr_info("srcu_defer_nmi: FAIL: srcu_barrier() returned before func2; func2 ran %lluus later\n",
  				(READ_ONCE(t_func2) - t_barrier_ret) / NSEC_PER_USEC);
  		} else {
  			pr_info("srcu_defer_nmi: FAIL: srcu_barrier() returned before func2; func2 never ran\n");
  		}
  	}
  	pr_info("srcu_defer_nmi: func1_ran=%d func2_ran=%d\n",
  		READ_ONCE(func1_ran), READ_ONCE(func2_ran));

  	srcu_barrier(&test_srcu);
  	cleanup_srcu_struct(&test_srcu);
  	if (!(uaf && barrier_done && !func2_ran_at_barrier_ret))
  		kfree(victim);
  	/* Report via dmesg only; nothing to keep loaded. */
  	return -EAGAIN;

  out_kp:
  	unregister_kprobe(&kp);
  out_srcu:
  	cleanup_srcu_struct(&test_srcu);
  out_free:
  	kfree(victim);
  	return ret;
  }
  module_init(srcu_defer_nmi_init);

  MODULE_LICENSE("GPL");
  MODULE_DESCRIPTION("Reproducer: NMI-nested deferred call_srcu() vs srcu_barrier()");
 kernel/rcu/srcutree.c | 58 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)
kernel/rcu/srcutree.c +58 -0
index 2b0e2b2d2c187..157a4d94f2589 100644
@@ -112,6 +112,63 @@ static void srcu_defer_flush(void)
 		__srcu_defer_drain(&per_cpu(srcu_defer, cpu));
 }
 
+static void srcu_defer_ipi(void *unused)
+{
+}
+
+/*
+ * Does @sdp have deferred callbacks?  A drainer takes them off ->defer_cbs and
+ * re-issues them under ->lock, so an empty list seen under ->lock means none
+ * is left mid-drain.
+ */
+static bool srcu_defer_pending(struct srcu_defer *sndp, struct srcu_data *sdp)
+{
+	unsigned long flags;
+	bool ret;
+
+	raw_spin_lock_irqsave(&sndp->lock, flags);
+	ret = !llist_empty(&sdp->defer_cbs);
+	raw_spin_unlock_irqrestore(&sndp->lock, flags);
+	return ret;
+}
+
+/*
+ * Wait for @ssp's deferred callbacks whose staging was still in progress when
+ * srcu_defer_flush() ran.  __call_srcu() stages with interrupts disabled, in
+ * two steps: it adds the callback to ->defer_cbs, then chains the srcu_data
+ * onto the per-CPU list.  A call_srcu() nested between the steps, from an NMI
+ * or a kprobe, returns before the srcu_data is chained, so the flush misses its
+ * callback and leaves ->defer_cbs non-empty.  An IPI to that CPU returns only
+ * after the interrupted staging has finished, and a second drain then picks
+ * the callback up.
+ *
+ * A CPU going offline leaves cpu_online_mask in take_cpu_down() but runs until
+ * it reports itself dead from the idle loop, and the IPI fails without waiting
+ * for it.  Drain and sleep until its ->defer_cbs is empty instead.  That ends:
+ * a dead CPU cannot be part-way through staging, so all it staged is chained.
+ */
+static void srcu_defer_wait_staging(struct srcu_struct *ssp)
+{
+	int cpu;
+
+	if (!IS_ENABLED(CONFIG_RCU_DEFER))
+		return;
+
+	for_each_possible_cpu(cpu) {
+		struct srcu_data *sdp = per_cpu_ptr(ssp->sda, cpu);
+		struct srcu_defer *sndp = &per_cpu(srcu_defer, cpu);
+
+		while (srcu_defer_pending(sndp, sdp)) {
+			int ret = smp_call_function_single(cpu, srcu_defer_ipi, NULL, 1);
+
+			__srcu_defer_drain(sndp);
+			if (!ret)
+				break;
+			schedule_timeout_uninterruptible(1);
+		}
+	}
+}
+
 /*
  * Initialize SRCU per-CPU data.  Note that statically allocated
  * srcu_struct structures might already have srcu_read_lock() and
@@ -1907,6 +1964,7 @@ void srcu_barrier(struct srcu_struct *ssp)
 	 * other srcu_structs'.
 	 */
 	srcu_defer_flush();
+	srcu_defer_wait_staging(ssp);
 
 	s = rcu_seq_snap(&ssp->srcu_sup->srcu_barrier_seq);
 	mutex_lock(&ssp->srcu_sup->srcu_barrier_mutex);

base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244

Signature · 1 line
2.53.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [High] srcu: callback list corruption on…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] srcu: callback list corruption on duplicate deferred __call_srcu() [1]
--

commit 44be3f7a05942ce31e28c2b995f0b59469ca7b2d
Author: Andrea Parri <[email protected]>

srcu: Make srcu_barrier() wait for nested deferred callbacks

This commit updates srcu_barrier() to wait for an IPI to any CPU that still has callbacks on ->defer_cbs after srcu_defer_flush() and drain them again, ensuring callbacks from nested deferred call_srcu() invocations are not missed.

Quoted 54 lines
kernel/rcu/srcutree.c +46 -0
index 2b0e2b2d2c187..157a4d94f2589 100644
@@ -112,6 +112,63 @@ static void srcu_defer_flush(void)
 		__srcu_defer_drain(&per_cpu(srcu_defer, cpu));
 }
 
+static void srcu_defer_ipi(void *unused)
+{
+}
+
+/*
+ * Does @sdp have deferred callbacks?  A drainer takes them off ->defer_cbs and
+ * re-issues them under ->lock, so an empty list seen under ->lock means none
+ * is left mid-drain.
+ */
+static bool srcu_defer_pending(struct srcu_defer *sndp, struct srcu_data *sdp)
+{
+	unsigned long flags;
+	bool ret;
+
+	raw_spin_lock_irqsave(&sndp->lock, flags);
+	ret = !llist_empty(&sdp->defer_cbs);
+	raw_spin_unlock_irqrestore(&sndp->lock, flags);
+	return ret;
+}
+
+/*
+ * Wait for @ssp's deferred callbacks whose staging was still in progress when
+ * srcu_defer_flush() ran.  __call_srcu() stages with interrupts disabled, in
+ * two steps: it adds the callback to ->defer_cbs, then chains the srcu_data
+ * onto the per-CPU list.  A call_srcu() nested between the steps, from an NMI
+ * or a kprobe, returns before the srcu_data is chained, so the flush misses its
+ * callback and leaves ->defer_cbs non-empty.  An IPI to that CPU returns only
+ * after the interrupted staging has finished, and a second drain then picks
+ * the callback up.
+ *
+ * A CPU going offline leaves cpu_online_mask in take_cpu_down() but runs until
+ * it reports itself dead from the idle loop, and the IPI fails without waiting
+ * for it.  Drain and sleep until its ->defer_cbs is empty instead.  That ends:
+ * a dead CPU cannot be part-way through staging, so all it staged is chained.
+ */
+static void srcu_defer_wait_staging(struct srcu_struct *ssp)
+{
+	int cpu;
+
+	if (!IS_ENABLED(CONFIG_RCU_DEFER))
+		return;
+
+	for_each_possible_cpu(cpu) {
+		struct srcu_data *sdp = per_cpu_ptr(ssp->sda, cpu);
+		struct srcu_defer *sndp = &per_cpu(srcu_defer, cpu);
+

kernel/rcu/srcutree.c static void srcu_defer_flush(void) · line 170

Quoted 10 lines
kernel/rcu/srcutree.c +10 -0
+		while (srcu_defer_pending(sndp, sdp)) {
+			int ret = smp_call_function_single(cpu, srcu_defer_ipi, NULL, 1);
+
+			__srcu_defer_drain(sndp);
+			if (!ret)
+				break;
+			schedule_timeout_uninterruptible(1);
+		}
+	}
+}

[Severity: High]
This problem wasn't introduced by this patch, but can a duplicate deferred call_srcu() truncate sdp->defer_cbs before __srcu_defer_drain() runs [1]?

When should_rcu_defer() is true (such as during rcutorture's rcu_test_debug_objects() runs with interrupts disabled), __call_srcu() adds rhp directly to sdp->defer_cbs via llist_add() before srcu_do_enqueue() can check debug_rcu_head_queue():

kernel/rcu/srcutree.c:__call_srcu() {
	if (should_rcu_defer()) {
		...
		sdp = this_cpu_ptr(ssp->sda);
		rhp->func = func;
		if (llist_add((struct llist_node *)rhp, &sdp->defer_cbs)) {
			sdp->ssp = ssp;
			if (llist_add(&sdp->defer_link, &sndp->list))
				irq_work_queue(&sndp->iw);
		}
		return;
	}
}

If call_srcu() is called a second time on the same rhp before __srcu_defer_drain() runs, the second llist_add() overwrites rhp->next to point to itself (or to an intermediate node). When __srcu_defer_drain() clears rhp->next to bound the cycle, any earlier valid callbacks queued on sdp->defer_cbs behind the duplicate are detached and permanently lost, hanging waiters such as synchronize_srcu().

[1] https://sashiko.dev/#/bug/linux-426c730e-bf31-4178-bd44-460b2a8e5ae3

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1