mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Paul E. McKenney" <paulmck@kernel.org>
To: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: linux-kernel@vger.kernel.org,
	David Woodhouse <dwmw2@infradead.org>,
	Frederic Weisbecker <frederic@kernel.org>,
	Neeraj Upadhyay <neeraj.upadhyay@kernel.org>,
	Joel Fernandes <joelagnelf@nvidia.com>,
	Josh Triplett <josh@joshtriplett.org>,
	Boqun Feng <boqun@kernel.org>,
	Uladzislau Rezki <urezki@gmail.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Lai Jiangshan <jiangshanlai@gmail.com>,
	Zqiang <qiang.zhang@linux.dev>,
	Davidlohr Bueso <dave@stgolabs.net>,
	Kunwu Chan <kunwu.chan@gmail.com>,
	Puranjay Mohan <puranjay@kernel.org>,
	rcu@vger.kernel.org
Subject: Re: [PATCH v2 3/7] srcu: Make atomic SRCU readers NMI-safe
Date: Wed, 11 Nov 2026 11:55:46 -0800	[thread overview]
Message-ID: <39f69863-032d-41b8-8d64-22eed2b62223@paulmck-laptop> (raw)
In-Reply-To: <20261111145949.70845-4-mathieu.desnoyers@efficios.com>

On Wed, Nov 11, 2026 at 09:59:45AM -0500, Mathieu Desnoyers wrote:
> Atomic SRCU readers count themselves with __srcu_read_lock(), whose
> this_cpu_inc() is not NMI-safe on every architecture. With
> CONFIG_PROVE_RCU, which lockdep enables, __srcu_check_read_flavor() also
> warns when an atomic reader runs in NMI. So an NMI handler cannot use
> atomic SRCU, while a tracer or a BPF program may need to read a
> structure that atomic SRCU protects.
> 
> Count atomic readers with __srcu_read_lock_nmisafe() and
> __srcu_read_unlock_nmisafe() instead. They use NMI-safe increments on
> the architectures that need them, and keep the full barriers that
> synchronize_srcu_atomic() pairs with. The __srcu_read_lock_fast()
> helpers have no such barriers: they rely on a grace period that calls
> synchronize_rcu(), which synchronize_srcu_atomic() cannot do while it
> spins.
> 
> Also accept the atomic flavor in NMI in __srcu_check_read_flavor().
> Annotate an atomic reader in NMI as a trylock for lockdep, as
> srcu_read_lock_nmisafe() does. An NMI can interrupt lockdep itself, and
> lockdep then reports any other acquisition of a lock that a grace
> period has used.
> 
> On the architectures that need NMI-safe counters, every atomic reader
> now pays an atomic increment at lock and at unlock.
> 
> Suggested-by: Paul E. McKenney <paulmck@kernel.org>
> Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>

Reviewed-by: Paul E. McKenney <paulmck@kernel.org>

> ---
>  include/linux/srcu.h  | 18 ++++++++++++++----
>  kernel/rcu/srcutree.c |  3 ++-
>  2 files changed, 16 insertions(+), 5 deletions(-)
> 
> diff --git a/include/linux/srcu.h b/include/linux/srcu.h
> index d4dcaeb83e2b..e3b541ceb8dd 100644
> --- a/include/linux/srcu.h
> +++ b/include/linux/srcu.h
> @@ -93,7 +93,7 @@ int init_srcu_struct_atomic(struct srcu_struct *ssp);
>  //					0x04		// SRCU-lite is no longer with us.
>  #define SRCU_READ_FLAVOR_FAST		0x04		// srcu_read_lock_fast(), also NMI-safe.
>  #define SRCU_READ_FLAVOR_FAST_UPDOWN	0x08		// srcu_read_lock_fast_updown().
> -#define SRCU_READ_FLAVOR_ATOMIC		0x10		// srcu_read_lock_atomic().
> +#define SRCU_READ_FLAVOR_ATOMIC		0x10		// srcu_read_lock_atomic(), also NMI-safe.
>  #define SRCU_READ_FLAVOR_ALL		(SRCU_READ_FLAVOR_NORMAL | SRCU_READ_FLAVOR_NMI | \
>  					 SRCU_READ_FLAVOR_FAST | SRCU_READ_FLAVOR_FAST_UPDOWN | \
>  					 SRCU_READ_FLAVOR_ATOMIC)
> @@ -372,6 +372,9 @@ static inline int srcu_read_lock(struct srcu_struct *ssp)
>   * The lock and matching srcu_read_unlock_atomic() must be invoked on
>   * the same CPU, from the same context; passing the return value to
>   * another task is not permitted for this flavor.
> + *
> + * This function is NMI-safe, in a manner similar to
> + * srcu_read_lock_nmisafe().
>   */
>  static inline int srcu_read_lock_atomic(struct srcu_struct *ssp)
>  	__acquires_shared(ssp)
> @@ -391,8 +394,15 @@ static inline int srcu_read_lock_atomic(struct srcu_struct *ssp)
>  	if (in_task())
>  		non_block_start();
>  	srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_ATOMIC);
> -	retval = __srcu_read_lock(ssp);
> -	srcu_lock_acquire(&ssp->dep_map);
> +	retval = __srcu_read_lock_nmisafe(ssp);
> +	/*
> +	 * An NMI may interrupt lockdep itself.  Annotate it as a trylock
> +	 * there, as srcu_read_lock_nmisafe() does.
> +	 */
> +	if (in_nmi())
> +		rcu_try_lock_acquire(&ssp->dep_map);
> +	else
> +		srcu_lock_acquire(&ssp->dep_map);
>  	return retval;
>  }
>  
> @@ -599,7 +609,7 @@ static inline void srcu_read_unlock_atomic(struct srcu_struct *ssp, int idx)
>  	WARN_ON_ONCE(idx & ~0x1);
>  	srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_ATOMIC);
>  	srcu_lock_release(&ssp->dep_map);
> -	__srcu_read_unlock(ssp, idx);
> +	__srcu_read_unlock_nmisafe(ssp, idx);
>  	if (in_task())
>  		non_block_end();
>  	preempt_enable();
> diff --git a/kernel/rcu/srcutree.c b/kernel/rcu/srcutree.c
> index 8075da4b1bb5..8fa366861dcc 100644
> --- a/kernel/rcu/srcutree.c
> +++ b/kernel/rcu/srcutree.c
> @@ -828,7 +828,8 @@ void __srcu_check_read_flavor(struct srcu_struct *ssp, int read_flavor)
>  
>  	/* NMI-unsafe use in NMI is a bad sign, as is multi-bit read_flavor values. */
>  	WARN_ON_ONCE(read_flavor != SRCU_READ_FLAVOR_NMI &&
> -		     read_flavor != SRCU_READ_FLAVOR_FAST && in_nmi());
> +		     read_flavor != SRCU_READ_FLAVOR_FAST &&
> +		     read_flavor != SRCU_READ_FLAVOR_ATOMIC && in_nmi());
>  	WARN_ON_ONCE(read_flavor & (read_flavor - 1));
>  
>  	sdp = raw_cpu_ptr(ssp->sda);
> -- 
> 2.47.3
> 

  reply	other threads:[~2026-11-11 19:55 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-11 14:59 [PATCH v2 0/7] srcu: NMI-safe atomic SRCU readers, and their irqsave forms Mathieu Desnoyers
2026-11-11 14:59 ` [PATCH v2 1/7] srcutiny: Do not call synchronize_rcu() from synchronize_srcu_atomic() Mathieu Desnoyers
2026-11-11 18:35   ` Paul E. McKenney
2026-11-11 14:59 ` [PATCH v2 2/7] srcu: Rule for using synchronize_srcu_atomic() from interrupt handlers Mathieu Desnoyers
2026-11-11 18:54   ` Paul E. McKenney
2026-11-11 14:59 ` [PATCH v2 3/7] srcu: Make atomic SRCU readers NMI-safe Mathieu Desnoyers
2026-11-11 19:55   ` Paul E. McKenney [this message]
2026-11-11 14:59 ` [PATCH v2 4/7] srcu: Add irqsave variants of the atomic SRCU readers Mathieu Desnoyers
2026-11-11 19:58   ` Paul E. McKenney
2026-11-11 20:04     ` Paul E. McKenney
2026-11-11 20:13       ` Mathieu Desnoyers
2026-11-11 14:59 ` [PATCH v2 5/7] rcutorture: Run NMI-safe SRCU readers from NMI Mathieu Desnoyers
2026-11-11 14:59 ` [PATCH v2 6/7] rcutorture: Test the irqsave forms of atomic SRCU readers Mathieu Desnoyers
2026-11-11 14:59 ` [PATCH v2 7/7] rcutorture: Check an atomic SRCU wait within an RCU reader Mathieu Desnoyers
2026-11-11 19:02 ` [PATCH v2 0/7] srcu: NMI-safe atomic SRCU readers, and their irqsave forms David Woodhouse
2026-11-11 19:33   ` Mathieu Desnoyers
2026-11-11 19:35     ` Paul E. McKenney
2026-11-11 19:37     ` David Woodhouse
2026-11-11 19:40       ` Mathieu Desnoyers
2026-11-11 19:50         ` Paul E. McKenney
2026-11-11 19:34   ` Paul E. McKenney

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=39f69863-032d-41b8-8d64-22eed2b62223@paulmck-laptop \
    --to=paulmck@kernel.org \
    --cc=boqun@kernel.org \
    --cc=dave@stgolabs.net \
    --cc=dwmw2@infradead.org \
    --cc=frederic@kernel.org \
    --cc=jiangshanlai@gmail.com \
    --cc=joelagnelf@nvidia.com \
    --cc=josh@joshtriplett.org \
    --cc=kunwu.chan@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=neeraj.upadhyay@kernel.org \
    --cc=puranjay@kernel.org \
    --cc=qiang.zhang@linux.dev \
    --cc=rcu@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=urezki@gmail.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®