Skip to contentSkip to conversation

SRCU updates for v6.8

PATCH In review Neeraj Upadhyay (AMD) 3 participants 10 messages Dec 12, 2023 – Dec 13, 2023

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

cover SRCU updates for v6.8

Hello,

This series contains SRCU updates:

1. Remove superfluous callbacks advancing from srcu_gp_start(),
courtesy of Frederic Weisbecker.

2. No need to advance/accelerate if no callback enqueued, courtesy
of Frederic Weisbecker.

3. Explain why callbacks invocations can't run concurrently,
courtesy of Frederic Weisbecker.

Thanks
Neeraj

------------------------------------------------------------------------

 b/kernel/rcu/srcutree.c |   10 ----------
 kernel/rcu/srcutree.c   |   14 +++++++++++---
 2 files changed, 11 insertions(+), 13 deletions(-)
#1 lore raw

PATCH 1/3 srcu: Remove superfluous callbacks advancing from srcu_gp_start()

From: Frederic Weisbecker <[email protected]>

Callbacks advancing on SRCU must be performed on two specific places:

1) On enqueue time in order to make room for the acceleration of the new callback.

2) On invocation time in order to move the callbacks ready to invoke.

Any other callback advancing callsite is needless. Remove the remaining one in srcu_gp_start().

---
 kernel/rcu/srcutree.c | 10 ----------
 1 file changed, 10 deletions(-)
kernel/rcu/srcutree.c +0 -10
index 560e99ec5333..e9356a103626 100644
@@ -772,20 +772,10 @@ EXPORT_SYMBOL_GPL(__srcu_read_unlock_nmisafe);
  */
 static void srcu_gp_start(struct srcu_struct *ssp)
 {
-	struct srcu_data *sdp;
 	int state;
 
-	if (smp_load_acquire(&ssp->srcu_sup->srcu_size_state) < SRCU_SIZE_WAIT_BARRIER)
-		sdp = per_cpu_ptr(ssp->sda, get_boot_cpu_id());
-	else
-		sdp = this_cpu_ptr(ssp->sda);
 	lockdep_assert_held(&ACCESS_PRIVATE(ssp->srcu_sup, lock));
 	WARN_ON_ONCE(ULONG_CMP_GE(ssp->srcu_sup->srcu_gp_seq, ssp->srcu_sup->srcu_gp_seq_needed));
-	spin_lock_rcu_node(sdp);  /* Interrupts already disabled. */
-	rcu_segcblist_advance(&sdp->srcu_cblist,
-			      rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq));
-	WARN_ON_ONCE(!rcu_segcblist_segempty(&sdp->srcu_cblist, RCU_NEXT_TAIL));
-	spin_unlock_rcu_node(sdp);  /* Interrupts remain disabled. */
 	WRITE_ONCE(ssp->srcu_sup->srcu_gp_start, jiffies);
 	WRITE_ONCE(ssp->srcu_sup->srcu_n_exp_nodelay, 0);
 	smp_mb(); /* Order prior store to ->srcu_gp_seq_needed vs. GP start. */
Signature · 1 line
2.40.1
#2 lore raw

PATCH 2/3 srcu: No need to advance/accelerate if no callback enqueued

In reply to Neeraj Upadhyay (AMD)

From: Frederic Weisbecker <[email protected]>

While in grace period start, there is nothing to accelerate and therefore no need to advance the callbacks either if no callback is to be enqueued.

Spare these needless operations in this case.

---
 kernel/rcu/srcutree.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
kernel/rcu/srcutree.c +5 -3
index e9356a103626..2bfc8ed1eed2 100644
@@ -1261,9 +1261,11 @@ static unsigned long srcu_gp_start_if_needed(struct srcu_struct *ssp,
 	 *     period (gp_num = X + 8). So acceleration fails.
 	 */
 	s = rcu_seq_snap(&ssp->srcu_sup->srcu_gp_seq);
-	rcu_segcblist_advance(&sdp->srcu_cblist,
-			      rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq));
-	WARN_ON_ONCE(!rcu_segcblist_accelerate(&sdp->srcu_cblist, s) && rhp);
+	if (rhp) {
+		rcu_segcblist_advance(&sdp->srcu_cblist,
+				      rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq));
+		WARN_ON_ONCE(!rcu_segcblist_accelerate(&sdp->srcu_cblist, s));
+	}
 	if (ULONG_CMP_LT(sdp->srcu_gp_seq_needed, s)) {
 		sdp->srcu_gp_seq_needed = s;
 		needgp = true;
Signature · 1 line
2.40.1
#3 lore raw

PATCH 3/3 srcu: Explain why callbacks invocations can't run concurrently

In reply to Neeraj Upadhyay (AMD)

From: Frederic Weisbecker <[email protected]>

If an SRCU barrier is queued while callbacks are running and a new callbacks invocator for the same sdp were to run concurrently, the RCU barrier might execute too early. As this requirement is non-obvious, make sure to keep a record.

---
 kernel/rcu/srcutree.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/rcu/srcutree.c +6 -0
index 2bfc8ed1eed2..0351a4e83529 100644
@@ -1715,6 +1715,11 @@ static void srcu_invoke_callbacks(struct work_struct *work)
 	WARN_ON_ONCE(!rcu_segcblist_segempty(&sdp->srcu_cblist, RCU_NEXT_TAIL));
 	rcu_segcblist_advance(&sdp->srcu_cblist,
 			      rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq));
+	/*
+	 * Although this function is theoretically re-entrant, concurrent
+	 * callbacks invocation is disallowed to avoid executing an SRCU barrier
+	 * too early.
+	 */
 	if (sdp->srcu_cblist_invoking ||
 	    !rcu_segcblist_ready_cbs(&sdp->srcu_cblist)) {
 		spin_unlock_irq_rcu_node(sdp);
@@ -1745,6 +1750,7 @@ static void srcu_invoke_callbacks(struct work_struct *work)
 	sdp->srcu_cblist_invoking = false;
 	more = rcu_segcblist_ready_cbs(&sdp->srcu_cblist);
 	spin_unlock_irq_rcu_node(sdp);
+	/* An SRCU barrier or callbacks from previous nesting work pending */
 	if (more)
 		srcu_schedule_cbs_sdp(sdp, 0);
 }
Signature · 1 line
2.40.1
#4 lore raw

Re: [PATCH rcu 3/3] srcu: Explain why callbacks invocations can't run concurrently

Quoted 21 lines · Neeraj Upadhyay (AMD)

From: Frederic Weisbecker <[email protected]>

If an SRCU barrier is queued while callbacks are running and a new callbacks invocator for the same sdp were to run concurrently, the RCU barrier might execute too early. As this requirement is non-obvious, make sure to keep a record.

---
 kernel/rcu/srcutree.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/rcu/srcutree.c +0 -0
index 2bfc8ed1eed2..0351a4e83529 100644

kernel/rcu/srcutree.c static void srcu_invoke_callbacks(struct work_struct *work) · line 1722

Quoted 8 lines
kernel/rcu/srcutree.c +5 -0
@@ -1715,6 +1715,11 @@ static void srcu_invoke_callbacks(struct work_struct *work)
        WARN_ON_ONCE(!rcu_segcblist_segempty(&sdp->srcu_cblist, RCU_NEXT_TAIL));
        rcu_segcblist_advance(&sdp->srcu_cblist,
                              rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq));
+       /*
+        * Although this function is theoretically re-entrant, concurrent
+        * callbacks invocation is disallowed to avoid executing an SRCU barrier
+        * too early.
+        */

Side comment:
I guess even without the barrier reasoning, it is best not to allow concurrent CB execution anyway since it diverges from the behavior of straight RCU :)

- Joel

maintainer
#5 lore raw

Re: [PATCH rcu 3/3] srcu: Explain why callbacks invocations can't run concurrently

Quoted 28 lines · Joel Fernandes
Quoted 26 lines · Neeraj Upadhyay (AMD)

From: Frederic Weisbecker <[email protected]>

If an SRCU barrier is queued while callbacks are running and a new callbacks invocator for the same sdp were to run concurrently, the RCU barrier might execute too early. As this requirement is non-obvious, make sure to keep a record.

---
 kernel/rcu/srcutree.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/rcu/srcutree.c +2 -0
index 2bfc8ed1eed2..0351a4e83529 100644
@@ -1715,6 +1715,11 @@ static void srcu_invoke_callbacks(struct work_struct *work)
        WARN_ON_ONCE(!rcu_segcblist_segempty(&sdp->srcu_cblist, RCU_NEXT_TAIL));
        rcu_segcblist_advance(&sdp->srcu_cblist,
                              rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq));
+       /*
+        * Although this function is theoretically re-entrant, concurrent
Quoted 8 lines

kernel/rcu/srcutree.c static void srcu_invoke_callbacks(struct work_struct *work) · line 1722

Quoted 3 lines
kernel/rcu/srcutree.c +3 -0
+        * callbacks invocation is disallowed to avoid executing an SRCU barrier
+        * too early.
+        */

Side comment:
I guess even without the barrier reasoning, it is best not to allow concurrent CB execution anyway since it diverges from the behavior of straight RCU :)

Good point!

But please do not forget item 12 on the list in checklist.rst. ;-) (Which I just updated to include the other call_rcu*() functions.)

Thanx, Paul

#6 lore raw

Re: [PATCH rcu 3/3] srcu: Explain why callbacks invocations can't run concurrently

Quoted 39 lines · Paul E. McKenney
Quoted 28 lines · Joel Fernandes
Quoted 26 lines · Neeraj Upadhyay (AMD)

From: Frederic Weisbecker <[email protected]>

If an SRCU barrier is queued while callbacks are running and a new callbacks invocator for the same sdp were to run concurrently, the RCU barrier might execute too early. As this requirement is non-obvious, make sure to keep a record.

---
 kernel/rcu/srcutree.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/rcu/srcutree.c +2 -0
index 2bfc8ed1eed2..0351a4e83529 100644
@@ -1715,6 +1715,11 @@ static void srcu_invoke_callbacks(struct work_struct *work)
        WARN_ON_ONCE(!rcu_segcblist_segempty(&sdp->srcu_cblist, RCU_NEXT_TAIL));
        rcu_segcblist_advance(&sdp->srcu_cblist,
                              rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq));
+       /*
+        * Although this function is theoretically re-entrant, concurrent
Quoted 8 lines

kernel/rcu/srcutree.c static void srcu_invoke_callbacks(struct work_struct *work) · line 1722

Quoted 3 lines
kernel/rcu/srcutree.c +3 -0
+        * callbacks invocation is disallowed to avoid executing an SRCU barrier
+        * too early.
+        */

Side comment:
I guess even without the barrier reasoning, it is best not to allow concurrent CB execution anyway since it diverges from the behavior of straight RCU :)

Quoted 4 lines

Good point!

But please do not forget item 12 on the list in checklist.rst. ;-) (Which I just updated to include the other call_rcu*() functions.)

I think this is more so now with recent kernels (with the dynamic nocb switch) than with older kernels right? I haven't kept up with the checklist recently (which is my bad).

My understanding comes from the fact that the RCU barrier depends on callbacks on the same CPU executing in order with straight RCU otherwise it breaks. Hence my comment. But as you pointed out, that's outdated knowledge.

I should just shut up and hide in shame now.

:-/

- Joel

maintainer
#7 lore raw

Re: [PATCH rcu 3/3] srcu: Explain why callbacks invocations can't run concurrently

Quoted 40 lines · Joel Fernandes
Quoted 39 lines · Paul E. McKenney
Quoted 36 lines · Joel Fernandes
Quoted 21 lines · Neeraj Upadhyay (AMD)

From: Frederic Weisbecker <[email protected]>

If an SRCU barrier is queued while callbacks are running and a new callbacks invocator for the same sdp were to run concurrently, the RCU barrier might execute too early. As this requirement is non-obvious, make sure to keep a record.

---
 kernel/rcu/srcutree.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/rcu/srcutree.c +0 -0
index 2bfc8ed1eed2..0351a4e83529 100644

kernel/rcu/srcutree.c static void srcu_invoke_callbacks(struct work_struct *work) · line 1722

Quoted 8 lines
kernel/rcu/srcutree.c +5 -0
@@ -1715,6 +1715,11 @@ static void srcu_invoke_callbacks(struct work_struct *work)
        WARN_ON_ONCE(!rcu_segcblist_segempty(&sdp->srcu_cblist, RCU_NEXT_TAIL));
        rcu_segcblist_advance(&sdp->srcu_cblist,
                              rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq));
+       /*
+        * Although this function is theoretically re-entrant, concurrent
+        * callbacks invocation is disallowed to avoid executing an SRCU barrier
+        * too early.
+        */

Side comment:
I guess even without the barrier reasoning, it is best not to allow concurrent CB execution anyway since it diverges from the behavior of straight RCU :)

Quoted 8 lines
Quoted 4 lines

Good point!

But please do not forget item 12 on the list in checklist.rst. ;-) (Which I just updated to include the other call_rcu*() functions.)

I think this is more so now with recent kernels (with the dynamic nocb switch) than with older kernels right? I haven't kept up with the checklist recently (which is my bad).

You are quite correct! But even before this, I was saying that lack of same-CPU callback concurrency was an accident of the current implementation rather than a guarantee. For example, there might come a time when RCU needs to respond to callback flooding with concurrent execution of the flooded CPU's callbacks. Or not, but we do need to keep this option open.

Quoted 4 lines

My understanding comes from the fact that the RCU barrier depends on callbacks on the same CPU executing in order with straight RCU otherwise it breaks. Hence my comment. But as you pointed out, that's outdated knowledge.

That is still one motivation for ordered execution of callbacks. For the dynamic nocb switch, we could have chosen to make rcu_barrier() place a callback on both lists, but we instead chose to exclude rcu_barrier() calls during the switch.

Quoted 1 line

I should just shut up and hide in shame now.

No need for that! After all, one motivation for Requirements.rst was to help me keep track of all this stuff.

Thanx, Paul

Quoted 3 lines

:-/

- Joel

#8 lore raw

Re: [PATCH rcu 3/3] srcu: Explain why callbacks invocations can't run concurrently

Quoted 51 lines · Paul E. McKenney
Quoted 40 lines · Joel Fernandes
Quoted 39 lines · Paul E. McKenney
Quoted 36 lines · Joel Fernandes
Quoted 21 lines · Neeraj Upadhyay (AMD)

From: Frederic Weisbecker <[email protected]>

If an SRCU barrier is queued while callbacks are running and a new callbacks invocator for the same sdp were to run concurrently, the RCU barrier might execute too early. As this requirement is non-obvious, make sure to keep a record.

---
 kernel/rcu/srcutree.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/rcu/srcutree.c +0 -0
index 2bfc8ed1eed2..0351a4e83529 100644

kernel/rcu/srcutree.c static void srcu_invoke_callbacks(struct work_struct *work) · line 1722

Quoted 8 lines
kernel/rcu/srcutree.c +5 -0
@@ -1715,6 +1715,11 @@ static void srcu_invoke_callbacks(struct work_struct *work)
        WARN_ON_ONCE(!rcu_segcblist_segempty(&sdp->srcu_cblist, RCU_NEXT_TAIL));
        rcu_segcblist_advance(&sdp->srcu_cblist,
                              rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq));
+       /*
+        * Although this function is theoretically re-entrant, concurrent
+        * callbacks invocation is disallowed to avoid executing an SRCU barrier
+        * too early.
+        */

Side comment:
I guess even without the barrier reasoning, it is best not to allow concurrent CB execution anyway since it diverges from the behavior of straight RCU :)

Quoted 8 lines
Quoted 4 lines

Good point!

But please do not forget item 12 on the list in checklist.rst. ;-) (Which I just updated to include the other call_rcu*() functions.)

I think this is more so now with recent kernels (with the dynamic nocb switch) than with older kernels right? I haven't kept up with the checklist recently (which is my bad).

Quoted 6 lines

You are quite correct! But even before this, I was saying that lack of same-CPU callback concurrency was an accident of the current implementation rather than a guarantee. For example, there might come a time when RCU needs to respond to callback flooding with concurrent execution of the flooded CPU's callbacks. Or not, but we do need to keep this option open.

Got it, reminds me to focus on requirements as well along with implementation.

Quoted 9 lines
Quoted 4 lines

My understanding comes from the fact that the RCU barrier depends on callbacks on the same CPU executing in order with straight RCU otherwise it breaks. Hence my comment. But as you pointed out, that's outdated knowledge.

That is still one motivation for ordered execution of callbacks. For the dynamic nocb switch, we could have chosen to make rcu_barrier() place a callback on both lists, but we instead chose to exclude rcu_barrier() calls during the switch.

Right!

Quoted 4 lines
Quoted 1 line

I should just shut up and hide in shame now.

No need for that! After all, one motivation for Requirements.rst was to help me keep track of all this stuff.

Thanks!

- Joel

#9 lore raw

[PATCH rcu 4/3] srcu: Use try-lock lockdep annotation for NMI-safe access.

In reply to Neeraj Upadhyay (AMD)

From: Sebastian Andrzej Siewior <[email protected]>

It is claimed that srcu_read_lock_nmisafe() NMI-safe. However it triggers a lockdep if used from NMI because lockdep expects a deadlock since nothing disables NMIs while the lock is acquired.

This is because commit f0f44752f5f61 ("rcu: Annotate SRCU's update-side lockdep dependencies") annotates synchronize_srcu() as a write lock usage. This helps to detect a deadlocks such as

	srcu_read_lock();
	synchronize_srcu();
	srcu_read_unlock();

The side effect is that the lock srcu_struct now has a USED usage in normal contexts, so it conflicts with a USED_READ usage in NMI. But this shouldn't cause a real deadlock because the write lock usage from synchronize_srcu() is a fake one and only used for read/write deadlock detection.

Use a try-lock annotation for srcu_read_lock_nmisafe() to avoid lockdep complains if used from NMI.

---
 include/linux/rcupdate.h | 6 ++++++
 include/linux/srcu.h     | 2 +-
 2 files changed, 7 insertions(+), 1 deletion(-)
include/linux/rcupdate.h +6 -0
index f7206b2623c9..31d523c4e089 100644
@@ -301,6 +301,11 @@ static inline void rcu_lock_acquire(struct lockdep_map *map)
 	lock_acquire(map, 0, 0, 2, 0, NULL, _THIS_IP_);
 }
 
+static inline void rcu_try_lock_acquire(struct lockdep_map *map)
+{
+	lock_acquire(map, 0, 1, 2, 0, NULL, _THIS_IP_);
+}
+
 static inline void rcu_lock_release(struct lockdep_map *map)
 {
 	lock_release(map, _THIS_IP_);
@@ -315,6 +320,7 @@ int rcu_read_lock_any_held(void);
 #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
 
 # define rcu_lock_acquire(a)		do { } while (0)
+# define rcu_try_lock_acquire(a)	do { } while (0)
 # define rcu_lock_release(a)		do { } while (0)
 
 static inline int rcu_read_lock_held(void)
include/linux/srcu.h +1 -1
index 127ef3b2e607..236610e4a8fa 100644
@@ -229,7 +229,7 @@ static inline int srcu_read_lock_nmisafe(struct srcu_struct *ssp) __acquires(ssp
 
 	srcu_check_nmi_safety(ssp, true);
 	retval = __srcu_read_lock_nmisafe(ssp);
-	rcu_lock_acquire(&ssp->dep_map);
+	rcu_try_lock_acquire(&ssp->dep_map);
 	return retval;
 }
 
Signature · 1 line
2.40.1