All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Andrea Parri" <parri.andrea@gmail.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH] srcu: Make srcu_barrier() wait for nested deferred callbacks
Date: Tue, 01 Dec 2026 18:08:51 +0000	[thread overview]
Message-ID: <sashiko-outbox-243937@kernel.org> (raw)
In-Reply-To: <20261201175532.72556-1-parri.andrea@gmail.com>

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 <parri.andrea@gmail.com>

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.

> diff --git a/kernel/rcu/srcutree.c b/kernel/rcu/srcutree.c
> index 2b0e2b2d2c187..157a4d94f2589 100644
> --- a/kernel/rcu/srcutree.c
> +++ b/kernel/rcu/srcutree.c
> @@ -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);
> +		}
> +	}
> +}

[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

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201175532.72556-1-parri.andrea@gmail.com?part=1

  reply	other threads:[~2026-12-01 18:08 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 17:55 [PATCH] srcu: Make srcu_barrier() wait for nested deferred callbacks Andrea Parri
2026-12-01 18:08 ` sashiko-bot [this message]
2026-12-02  0:22   ` Andrea Parri

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-243937@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=parri.andrea@gmail.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.