[PATCH 1/4] srcu: Make atomic SRCU readers NMI-safe
From: Mathieu Desnoyers
Date: Tue Nov 10 2026 - 16:57:29 EST
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@xxxxxxxxxx>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
---
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 1a8a465a5650..7a338e3a63d3 100644
--- a/include/linux/srcu.h
+++ b/include/linux/srcu.h
@@ -92,7 +92,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)
@@ -336,6 +336,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)
@@ -355,8 +358,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;
}
@@ -563,7 +573,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 2b0e2b2d2c18..6aebd3e31654 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