Skip to contentSkip to conversation

Add support for length of each segment in the segcblist

PATCH In review Joel Fernandes (Google) 5 participants 40 messages Nov 3, 2020 – Nov 11, 2020

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

v9 cover Add support for length of each segment in the segcblist

This is required for several usecases identified. One of them being tracing how the segmented callback list changes. Tracing this has identified issues in RCU code in the past.

Quoted 1 line

From Paul:

Another use case is of course more accurately determining whether a given CPU's large pile of callbacks can be best served by making grace periods go faster, invoking callbacks more vigorously, or both. It should also be possible to simplify some of the callback handling a bit, given that some of the unnatural acts are due to there having been no per-batch counts.

Revision history:
v9: Fix SRCU issues, other minor style changes (Frederic). Added Frederic's Reviewed-by to all but the last patch..

v8: Small style changes, making the seglen as non-atomic since it is always
under lock (Frederic).

v7: Cleaned up memory barriers (thanks fweisbec@ for reviewing), made minor corrections per Neeraj (thanks).

v6: Fixed TREE04, and restored older logic to ensure rcu_barrier works.

v5: Various changes, bug fixes. Discovery of rcu_barrier issue.

v4: Restructured rcu_do_batch() and segcblist merging to avoid issues.
Fixed minor nit from Davidlohr.
v1->v3: minor nits.
(https://lore.kernel.org/lkml/20200719034210.2382053-1-joel@xxxxxxxxxxxxxxxxx/)

Joel Fernandes (Google) (7):
rcu/tree: Make rcu_do_batch count how many callbacks were executed rcu/segcblist: Add counters to segcblist datastructure
srcu: Fix invoke_rcu_callbacks() segcb length adjustment rcu/trace: Add tracing for how segcb list changes rcu/segcblist: Remove useless rcupdate.h include rcu/tree: segcblist: Remove redundant smp_mb()s rcu/segcblist: Add additional comments to explain smp_mb()

include/linux/rcu_segcblist.h |   1 +
include/trace/events/rcu.h    |  25 +++++
kernel/rcu/rcu_segcblist.c    | 198 +++++++++++++++++++++++++---------
kernel/rcu/rcu_segcblist.h    |   8 +-
kernel/rcu/srcutree.c         |   5 +-
kernel/rcu/tree.c             |  21 ++--
6 files changed, 199 insertions(+), 59 deletions(-)

--
2.29.1.341.ge80a0c044ae-goog

#1 lore raw

PATCH v9 1/7 rcu/tree: Make rcu_do_batch count how many callbacks were executed

Currently, rcu_do_batch() depends on the unsegmented callback list's len field to know how many CBs are executed. This fields counts down from 0 as CBs are dequeued. It is possible that all CBs could not be run because of reaching limits in which case the remaining unexecuted callbacks are requeued in the CPU's segcblist.

The number of callbacks that were not requeued are then the negative count (how many CBs were run) stored in the rcl->len which has been counting down on every dequeue. This negative count is then added to the per-cpu segmented callback list's to correct its count.

Such a design works against future efforts to track the length of each segment of the segmented callback list. The reason is because rcu_segcblist_extract_done_cbs() will be populating the unsegmented callback list's length field (rcl->len) during extraction.

Also, the design of counting down from 0 is confusing and error-prone IMHO.

This commit therefore explicitly counts how many callbacks were executed in rcu_do_batch() itself, and uses that to update the per-CPU segcb list's ->len field, without relying on the negativity of rcl->len.

---
 kernel/rcu/rcu_segcblist.c |  2 +-
 kernel/rcu/rcu_segcblist.h |  1 +
 kernel/rcu/tree.c          | 11 +++++------
 3 files changed, 7 insertions(+), 7 deletions(-)
kernel/rcu/rcu_segcblist.c +1 -1
index 2d2a6b6b9dfb..bb246d8c6ef1 100644
@@ -95,7 +95,7 @@ static void rcu_segcblist_set_len(struct rcu_segcblist *rsclp, long v)
  * This increase is fully ordered with respect to the callers accesses
  * both before and after.
  */
-static void rcu_segcblist_add_len(struct rcu_segcblist *rsclp, long v)
+void rcu_segcblist_add_len(struct rcu_segcblist *rsclp, long v)
 {
 #ifdef CONFIG_RCU_NOCB_CPU
 	smp_mb__before_atomic(); /* Up to the caller! */
kernel/rcu/rcu_segcblist.h +1 -0
index 492262bcb591..1d2d61406463 100644
@@ -76,6 +76,7 @@ static inline bool rcu_segcblist_restempty(struct rcu_segcblist *rsclp, int seg)
 }
 
 void rcu_segcblist_inc_len(struct rcu_segcblist *rsclp);
+void rcu_segcblist_add_len(struct rcu_segcblist *rsclp, long v);
 void rcu_segcblist_init(struct rcu_segcblist *rsclp);
 void rcu_segcblist_disable(struct rcu_segcblist *rsclp);
 void rcu_segcblist_offload(struct rcu_segcblist *rsclp);
kernel/rcu/tree.c +5 -6
index 286dc0a1b184..24c00020ab83 100644
@@ -2429,7 +2429,7 @@ static void rcu_do_batch(struct rcu_data *rdp)
 	const bool offloaded = rcu_segcblist_is_offloaded(&rdp->cblist);
 	struct rcu_head *rhp;
 	struct rcu_cblist rcl = RCU_CBLIST_INITIALIZER(rcl);
-	long bl, count;
+	long bl, count = 0;
 	long pending, tlimit = 0;
 
 	/* If no callbacks are ready, just return. */
@@ -2474,6 +2474,7 @@ static void rcu_do_batch(struct rcu_data *rdp)
 	for (; rhp; rhp = rcu_cblist_dequeue(&rcl)) {
 		rcu_callback_t f;
 
+		count++;
 		debug_rcu_head_unqueue(rhp);
 
 		rcu_lock_acquire(&rcu_callback_map);
@@ -2487,15 +2488,14 @@ static void rcu_do_batch(struct rcu_data *rdp)
 
 		/*
 		 * Stop only if limit reached and CPU has something to do.
-		 * Note: The rcl structure counts down from zero.
 		 */
-		if (-rcl.len >= bl && !offloaded &&
+		if (count >= bl && !offloaded &&
 		    (need_resched() ||
 		     (!is_idle_task(current) && !rcu_is_callbacks_kthread())))
 			break;
 		if (unlikely(tlimit)) {
 			/* only call local_clock() every 32 callbacks */
-			if (likely((-rcl.len & 31) || local_clock() < tlimit))
+			if (likely((count & 31) || local_clock() < tlimit))
 				continue;
 			/* Exceeded the time limit, so leave. */
 			break;
@@ -2512,7 +2512,6 @@ static void rcu_do_batch(struct rcu_data *rdp)
 
 	local_irq_save(flags);
 	rcu_nocb_lock(rdp);
-	count = -rcl.len;
 	rdp->n_cbs_invoked += count;
 	trace_rcu_batch_end(rcu_state.name, count, !!rcl.head, need_resched(),
 			    is_idle_task(current), rcu_is_callbacks_kthread());
@@ -2520,7 +2519,7 @@ static void rcu_do_batch(struct rcu_data *rdp)
 	/* Update counts and requeue any remaining callbacks. */
 	rcu_segcblist_insert_done_cbs(&rdp->cblist, &rcl);
 	smp_mb(); /* List handling before counting for rcu_barrier(). */
-	rcu_segcblist_insert_count(&rdp->cblist, &rcl);
+	rcu_segcblist_add_len(&rdp->cblist, -count);
 
 	/* Reinstate batch limit if we have worked down the excess. */
 	count = rcu_segcblist_n_cbs(&rdp->cblist);
Signature · 1 line
2.29.1.341.ge80a0c044ae-goog
maintainer Reviewed-by Frederic Weisbecker Reviewed-by Neeraj Upadhyay on v9 1/7
#2 lore raw

Re: [PATCH v9 1/7] rcu/tree: Make rcu_do_batch count how many callbacks were executed

Quoted 18 lines · Joel Fernandes (Google)

Currently, rcu_do_batch() depends on the unsegmented callback list's len field to know how many CBs are executed. This fields counts down from 0 as CBs are dequeued. It is possible that all CBs could not be run because of reaching limits in which case the remaining unexecuted callbacks are requeued in the CPU's segcblist.

The number of callbacks that were not requeued are then the negative count (how many CBs were run) stored in the rcl->len which has been counting down on every dequeue. This negative count is then added to the per-cpu segmented callback list's to correct its count.

Such a design works against future efforts to track the length of each segment of the segmented callback list. The reason is because rcu_segcblist_extract_done_cbs() will be populating the unsegmented callback list's length field (rcl->len) during extraction.

Also, the design of counting down from 0 is confusing and error-prone IMHO.

Quoted 7 lines

This commit therefore explicitly counts how many callbacks were executed in rcu_do_batch() itself, and uses that to update the per-CPU segcb list's ->len field, without relying on the negativity of rcl->len.

Queued for testing and further review, thank you all!

I will be pulling these in as I look them over, and running tests in between times, so this will take some time.

As usual, I could not resist editing the commit log, so please see below to check whether I messed something up.

Thanx, Paul

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

commit d77ef2684cbff728c14b3c84a356139c52ca3a5e

Author: Joel Fernandes (Google) <[email protected]>
Date:   Tue Nov 3 09:25:57 2020 -0500

rcu/tree: Make rcu_do_batch count how many callbacks were executed

The rcu_do_batch() function extracts the ready-to-invoke callbacks from the rcu_segcblist located in the ->cblist field of the current CPU's rcu_data structure. These callbacks are first moved to a local (unsegmented) rcu_cblist. The rcu_do_batch() function then uses this rcu_cblist's ->len field to count how many CBs it has invoked, but it does so by counting that field down from zero. Finally, this function negates the value in this ->len field (resulting in a positive number) and subtracts the result from the ->len field of the current CPU's ->cblist field.

Except that it is sometimes necessary for rcu_do_batch() to stop invoking callbacks mid-stream, despite there being more ready to invoke, for example, if a high-priority task wakes up. In this case the remaining not-yet-invoked callbacks are requeued back onto the CPU's ->cblist, but remain in the ready-to-invoke segment of that list. As above, the negative of the local rcu_cblist's ->len field is still subtracted from the ->len field of the current CPU's ->cblist field.

The design of counting down from 0 is confusing and error-prone, plus use of a positive count will make it easier to provide a uniform and consistent API to deal with the per-segment counts that are added later in this series. For example, rcu_segcblist_extract_done_cbs() can unconditionally populate the resulting unsegmented list's ->len field during extraction.

This commit therefore explicitly counts how many callbacks were executed in rcu_do_batch() itself, counting up from zero, and then uses that to update the per-CPU segcb list's ->len field, without relying on the downcounting of rcl->len from zero.

kernel/rcu/rcu_segcblist.c +1 -1
index 2d2a6b6b9..bb246d8 100644
@@ -95,7 +95,7 @@ static void rcu_segcblist_set_len(struct rcu_segcblist *rsclp, long v)
  * This increase is fully ordered with respect to the callers accesses
  * both before and after.
  */
-static void rcu_segcblist_add_len(struct rcu_segcblist *rsclp, long v)
+void rcu_segcblist_add_len(struct rcu_segcblist *rsclp, long v)
 {
 #ifdef CONFIG_RCU_NOCB_CPU
 	smp_mb__before_atomic(); /* Up to the caller! */
kernel/rcu/rcu_segcblist.h +1 -0
index 492262b..1d2d614 100644
@@ -76,6 +76,7 @@ static inline bool rcu_segcblist_restempty(struct rcu_segcblist *rsclp, int seg)
 }
 
 void rcu_segcblist_inc_len(struct rcu_segcblist *rsclp);
+void rcu_segcblist_add_len(struct rcu_segcblist *rsclp, long v);
 void rcu_segcblist_init(struct rcu_segcblist *rsclp);
 void rcu_segcblist_disable(struct rcu_segcblist *rsclp);
 void rcu_segcblist_offload(struct rcu_segcblist *rsclp);
kernel/rcu/tree.c +5 -6
index b6ec565..c4035e7 100644
@@ -2436,7 +2436,7 @@ static void rcu_do_batch(struct rcu_data *rdp)
 	const bool offloaded = rcu_segcblist_is_offloaded(&rdp->cblist);
 	struct rcu_head *rhp;
 	struct rcu_cblist rcl = RCU_CBLIST_INITIALIZER(rcl);
-	long bl, count;
+	long bl, count = 0;
 	long pending, tlimit = 0;
 
 	/* If no callbacks are ready, just return. */
@@ -2481,6 +2481,7 @@ static void rcu_do_batch(struct rcu_data *rdp)
 	for (; rhp; rhp = rcu_cblist_dequeue(&rcl)) {
 		rcu_callback_t f;
 
+		count++;
 		debug_rcu_head_unqueue(rhp);
 
 		rcu_lock_acquire(&rcu_callback_map);
@@ -2494,15 +2495,14 @@ static void rcu_do_batch(struct rcu_data *rdp)
 
 		/*
 		 * Stop only if limit reached and CPU has something to do.
-		 * Note: The rcl structure counts down from zero.
 		 */
-		if (-rcl.len >= bl && !offloaded &&
+		if (count >= bl && !offloaded &&
 		    (need_resched() ||
 		     (!is_idle_task(current) && !rcu_is_callbacks_kthread())))
 			break;
 		if (unlikely(tlimit)) {
 			/* only call local_clock() every 32 callbacks */
-			if (likely((-rcl.len & 31) || local_clock() < tlimit))
+			if (likely((count & 31) || local_clock() < tlimit))
 				continue;
 			/* Exceeded the time limit, so leave. */
 			break;
@@ -2519,7 +2519,6 @@ static void rcu_do_batch(struct rcu_data *rdp)
 
 	local_irq_save(flags);
 	rcu_nocb_lock(rdp);
-	count = -rcl.len;
 	rdp->n_cbs_invoked += count;
 	trace_rcu_batch_end(rcu_state.name, count, !!rcl.head, need_resched(),
 			    is_idle_task(current), rcu_is_callbacks_kthread());
@@ -2527,7 +2526,7 @@ static void rcu_do_batch(struct rcu_data *rdp)
 	/* Update counts and requeue any remaining callbacks. */
 	rcu_segcblist_insert_done_cbs(&rdp->cblist, &rcl);
 	smp_mb(); /* List handling before counting for rcu_barrier(). */
-	rcu_segcblist_insert_count(&rdp->cblist, &rcl);
+	rcu_segcblist_add_len(&rdp->cblist, -count);
 
 	/* Reinstate batch limit if we have worked down the excess. */
 	count = rcu_segcblist_n_cbs(&rdp->cblist);
#3 lore raw

Re: [PATCH v9 1/7] rcu/tree: Make rcu_do_batch count how many callbacks were executed

Quoted 27 lines · Paul E. McKenney
Quoted 18 lines · Joel Fernandes (Google)

Currently, rcu_do_batch() depends on the unsegmented callback list's len field to know how many CBs are executed. This fields counts down from 0 as CBs are dequeued. It is possible that all CBs could not be run because of reaching limits in which case the remaining unexecuted callbacks are requeued in the CPU's segcblist.

The number of callbacks that were not requeued are then the negative count (how many CBs were run) stored in the rcl->len which has been counting down on every dequeue. This negative count is then added to the per-cpu segmented callback list's to correct its count.

Such a design works against future efforts to track the length of each segment of the segmented callback list. The reason is because rcu_segcblist_extract_done_cbs() will be populating the unsegmented callback list's length field (rcl->len) during extraction.

Also, the design of counting down from 0 is confusing and error-prone IMHO.

Quoted 7 lines

This commit therefore explicitly counts how many callbacks were executed in rcu_do_batch() itself, and uses that to update the per-CPU segcb list's ->len field, without relying on the negativity of rcl->len.

Quoted 7 lines

Queued for testing and further review, thank you all!

I will be pulling these in as I look them over, and running tests in between times, so this will take some time.

As usual, I could not resist editing the commit log, so please see below to check whether I messed something up.

Looks good to me, thanks.

- Joel

Quoted 127 lines

Thanx, Paul

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

commit d77ef2684cbff728c14b3c84a356139c52ca3a5e

Author: Joel Fernandes (Google) <[email protected]>
Date:   Tue Nov 3 09:25:57 2020 -0500

rcu/tree: Make rcu_do_batch count how many callbacks were executed

The rcu_do_batch() function extracts the ready-to-invoke callbacks from the rcu_segcblist located in the ->cblist field of the current CPU's rcu_data structure. These callbacks are first moved to a local (unsegmented) rcu_cblist. The rcu_do_batch() function then uses this rcu_cblist's ->len field to count how many CBs it has invoked, but it does so by counting that field down from zero. Finally, this function negates the value in this ->len field (resulting in a positive number) and subtracts the result from the ->len field of the current CPU's ->cblist field.

Except that it is sometimes necessary for rcu_do_batch() to stop invoking callbacks mid-stream, despite there being more ready to invoke, for example, if a high-priority task wakes up. In this case the remaining not-yet-invoked callbacks are requeued back onto the CPU's ->cblist, but remain in the ready-to-invoke segment of that list. As above, the negative of the local rcu_cblist's ->len field is still subtracted from the ->len field of the current CPU's ->cblist field.

The design of counting down from 0 is confusing and error-prone, plus use of a positive count will make it easier to provide a uniform and consistent API to deal with the per-segment counts that are added later in this series. For example, rcu_segcblist_extract_done_cbs() can unconditionally populate the resulting unsegmented list's ->len field during extraction.

This commit therefore explicitly counts how many callbacks were executed in rcu_do_batch() itself, counting up from zero, and then uses that to update the per-CPU segcb list's ->len field, without relying on the downcounting of rcl->len from zero.

kernel/rcu/rcu_segcblist.c +1 -1
index 2d2a6b6b9..bb246d8 100644
@@ -95,7 +95,7 @@ static void rcu_segcblist_set_len(struct rcu_segcblist *rsclp, long v)
  * This increase is fully ordered with respect to the callers accesses
  * both before and after.
  */
-static void rcu_segcblist_add_len(struct rcu_segcblist *rsclp, long v)
+void rcu_segcblist_add_len(struct rcu_segcblist *rsclp, long v)
 {
 #ifdef CONFIG_RCU_NOCB_CPU
 	smp_mb__before_atomic(); /* Up to the caller! */
kernel/rcu/rcu_segcblist.h +1 -0
index 492262b..1d2d614 100644
@@ -76,6 +76,7 @@ static inline bool rcu_segcblist_restempty(struct rcu_segcblist *rsclp, int seg)
 }
 
 void rcu_segcblist_inc_len(struct rcu_segcblist *rsclp);
+void rcu_segcblist_add_len(struct rcu_segcblist *rsclp, long v);
 void rcu_segcblist_init(struct rcu_segcblist *rsclp);
 void rcu_segcblist_disable(struct rcu_segcblist *rsclp);
 void rcu_segcblist_offload(struct rcu_segcblist *rsclp);
kernel/rcu/tree.c +5 -6
index b6ec565..c4035e7 100644
@@ -2436,7 +2436,7 @@ static void rcu_do_batch(struct rcu_data *rdp)
 	const bool offloaded = rcu_segcblist_is_offloaded(&rdp->cblist);
 	struct rcu_head *rhp;
 	struct rcu_cblist rcl = RCU_CBLIST_INITIALIZER(rcl);
-	long bl, count;
+	long bl, count = 0;
 	long pending, tlimit = 0;
 
 	/* If no callbacks are ready, just return. */
@@ -2481,6 +2481,7 @@ static void rcu_do_batch(struct rcu_data *rdp)
 	for (; rhp; rhp = rcu_cblist_dequeue(&rcl)) {
 		rcu_callback_t f;
 
+		count++;
 		debug_rcu_head_unqueue(rhp);
 
 		rcu_lock_acquire(&rcu_callback_map);
@@ -2494,15 +2495,14 @@ static void rcu_do_batch(struct rcu_data *rdp)
 
 		/*
 		 * Stop only if limit reached and CPU has something to do.
-		 * Note: The rcl structure counts down from zero.
 		 */
-		if (-rcl.len >= bl && !offloaded &&
+		if (count >= bl && !offloaded &&
 		    (need_resched() ||
 		     (!is_idle_task(current) && !rcu_is_callbacks_kthread())))
 			break;
 		if (unlikely(tlimit)) {
 			/* only call local_clock() every 32 callbacks */
-			if (likely((-rcl.len & 31) || local_clock() < tlimit))
+			if (likely((count & 31) || local_clock() < tlimit))
 				continue;
 			/* Exceeded the time limit, so leave. */
 			break;
@@ -2519,7 +2519,6 @@ static void rcu_do_batch(struct rcu_data *rdp)
 
 	local_irq_save(flags);
 	rcu_nocb_lock(rdp);
-	count = -rcl.len;
 	rdp->n_cbs_invoked += count;
 	trace_rcu_batch_end(rcu_state.name, count, !!rcl.head, need_resched(),
 			    is_idle_task(current), rcu_is_callbacks_kthread());
@@ -2527,7 +2526,7 @@ static void rcu_do_batch(struct rcu_data *rdp)
 	/* Update counts and requeue any remaining callbacks. */
 	rcu_segcblist_insert_done_cbs(&rdp->cblist, &rcl);
 	smp_mb(); /* List handling before counting for rcu_barrier(). */
-	rcu_segcblist_insert_count(&rdp->cblist, &rcl);
+	rcu_segcblist_add_len(&rdp->cblist, -count);
 
 	/* Reinstate batch limit if we have worked down the excess. */
 	count = rcu_segcblist_n_cbs(&rdp->cblist);
#4 lore raw

PATCH v9 2/7 rcu/segcblist: Add counters to segcblist datastructure

In reply to Joel Fernandes (Google)

Add counting of segment lengths of segmented callback list.

This will be useful for a number of things such as knowing how big the ready-to-execute segment have gotten. The immediate benefit is ability to trace how the callbacks in the segmented callback list change.

Also this patch remove hacks related to using donecbs's ->len field as a temporary variable to save the segmented callback list's length. This cannot be done anymore and is not needed.

---
 include/linux/rcu_segcblist.h |   1 +
 kernel/rcu/rcu_segcblist.c    | 120 ++++++++++++++++++++++------------
 kernel/rcu/rcu_segcblist.h    |   2 -
 3 files changed, 79 insertions(+), 44 deletions(-)
include/linux/rcu_segcblist.h +1 -0
index b36afe7b22c9..6c01f09a6456 100644
@@ -72,6 +72,7 @@ struct rcu_segcblist {
 #else
 	long len;
 #endif
+	long seglen[RCU_CBLIST_NSEGS];
 	u8 enabled;
 	u8 offloaded;
 };
kernel/rcu/rcu_segcblist.c +78 -42

241 lines not on this page · raw message

kernel/rcu/rcu_segcblist.h +0 -2
index 1d2d61406463..cd35c9faaf51 100644
@@ -89,8 +89,6 @@ void rcu_segcblist_enqueue(struct rcu_segcblist *rsclp,
 			   struct rcu_head *rhp);
 bool rcu_segcblist_entrain(struct rcu_segcblist *rsclp,
 			   struct rcu_head *rhp);
-void rcu_segcblist_extract_count(struct rcu_segcblist *rsclp,
-				 struct rcu_cblist *rclp);
 void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp,
 				    struct rcu_cblist *rclp);
 void rcu_segcblist_extract_pend_cbs(struct rcu_segcblist *rsclp,
Signature · 1 line
2.29.1.341.ge80a0c044ae-goog
Reviewed-by on v9 2/7
#5 lore raw

Re: [PATCH v9 2/7] rcu/segcblist: Add counters to segcblist datastructure

Quoted 6 lines · Joel Fernandes (Google)

Add counting of segment lengths of segmented callback list.

This will be useful for a number of things such as knowing how big the ready-to-execute segment have gotten. The immediate benefit is ability to trace how the callbacks in the segmented callback list change.

Quoted 7 lines

Also this patch remove hacks related to using donecbs's ->len field as a temporary variable to save the segmented callback list's length. This cannot be done anymore and is not needed.

---

Quoted 293 lines
  include/linux/rcu_segcblist.h |   1 +
  kernel/rcu/rcu_segcblist.c    | 120 ++++++++++++++++++++++------------
  kernel/rcu/rcu_segcblist.h    |   2 -
  3 files changed, 79 insertions(+), 44 deletions(-)
include/linux/rcu_segcblist.h +1 -0
index b36afe7b22c9..6c01f09a6456 100644
@@ -72,6 +72,7 @@ struct rcu_segcblist {
  #else
  	long len;
  #endif
+	long seglen[RCU_CBLIST_NSEGS];
  	u8 enabled;
  	u8 offloaded;
  };
kernel/rcu/rcu_segcblist.c +78 -42
index bb246d8c6ef1..357c19bbcb00 100644
@@ -7,10 +7,11 @@
   * Authors: Paul E. McKenney <[email protected]>
   */
 
-#include <linux/types.h>
-#include <linux/kernel.h>
+#include <linux/cpu.h>
  #include <linux/interrupt.h>
+#include <linux/kernel.h>
  #include <linux/rcupdate.h>
+#include <linux/types.h>
 
  #include "rcu_segcblist.h"
 
@@ -88,6 +89,46 @@ static void rcu_segcblist_set_len(struct rcu_segcblist *rsclp, long v)
  #endif
  }
 
+/* Get the length of a segment of the rcu_segcblist structure. */
+static long rcu_segcblist_get_seglen(struct rcu_segcblist *rsclp, int seg)
+{
+	return READ_ONCE(rsclp->seglen[seg]);
+}
+
+/* Set the length of a segment of the rcu_segcblist structure. */
+static void rcu_segcblist_set_seglen(struct rcu_segcblist *rsclp, int seg, long v)
+{
+	WRITE_ONCE(rsclp->seglen[seg], v);
+}
+
+/* Return number of callbacks in a segment of the segmented callback list. */
+static void rcu_segcblist_add_seglen(struct rcu_segcblist *rsclp, int seg, long v)
+{
+	WRITE_ONCE(rsclp->seglen[seg], rsclp->seglen[seg] + v);
+}
+
+/* Move from's segment length to to's segment. */
+static void rcu_segcblist_move_seglen(struct rcu_segcblist *rsclp, int from, int to)
+{
+	long len;
+
+	if (from == to)
+		return;
+
+	len = rcu_segcblist_get_seglen(rsclp, from);
+	if (!len)
+		return;
+
+	rcu_segcblist_add_seglen(rsclp, to, len);
+	rcu_segcblist_set_seglen(rsclp, from, 0);
+}
+
+/* Increment segment's length. */
+static void rcu_segcblist_inc_seglen(struct rcu_segcblist *rsclp, int seg)
+{
+	rcu_segcblist_add_seglen(rsclp, seg, 1);
+}
+
  /*
   * Increase the numeric length of an rcu_segcblist structure by the
   * specified amount, which can be negative.  This can cause the ->len
@@ -119,26 +160,6 @@ void rcu_segcblist_inc_len(struct rcu_segcblist *rsclp)
  	rcu_segcblist_add_len(rsclp, 1);
  }
 
-/*
- * Exchange the numeric length of the specified rcu_segcblist structure
- * with the specified value.  This can cause the ->len field to disagree
- * with the actual number of callbacks on the structure.  This exchange is
- * fully ordered with respect to the callers accesses both before and after.
- */
-static long rcu_segcblist_xchg_len(struct rcu_segcblist *rsclp, long v)
-{
-#ifdef CONFIG_RCU_NOCB_CPU
-	return atomic_long_xchg(&rsclp->len, v);
-#else
-	long ret = rsclp->len;
-
-	smp_mb(); /* Up to the caller! */
-	WRITE_ONCE(rsclp->len, v);
-	smp_mb(); /* Up to the caller! */
-	return ret;
-#endif
-}
-
  /*
   * Initialize an rcu_segcblist structure.
   */
@@ -149,8 +170,10 @@ void rcu_segcblist_init(struct rcu_segcblist *rsclp)
  	BUILD_BUG_ON(RCU_NEXT_TAIL + 1 != ARRAY_SIZE(rsclp->gp_seq));
  	BUILD_BUG_ON(ARRAY_SIZE(rsclp->tails) != ARRAY_SIZE(rsclp->gp_seq));
  	rsclp->head = NULL;
-	for (i = 0; i < RCU_CBLIST_NSEGS; i++)
+	for (i = 0; i < RCU_CBLIST_NSEGS; i++) {
  		rsclp->tails[i] = &rsclp->head;
+		rcu_segcblist_set_seglen(rsclp, i, 0);
+	}
  	rcu_segcblist_set_len(rsclp, 0);
  	rsclp->enabled = 1;
  }
@@ -246,6 +269,7 @@ void rcu_segcblist_enqueue(struct rcu_segcblist *rsclp,
  {
  	rcu_segcblist_inc_len(rsclp);
  	smp_mb(); /* Ensure counts are updated before callback is enqueued. */
+	rcu_segcblist_inc_seglen(rsclp, RCU_NEXT_TAIL);
  	rhp->next = NULL;
  	WRITE_ONCE(*rsclp->tails[RCU_NEXT_TAIL], rhp);
  	WRITE_ONCE(rsclp->tails[RCU_NEXT_TAIL], &rhp->next);
@@ -274,27 +298,13 @@ bool rcu_segcblist_entrain(struct rcu_segcblist *rsclp,
  	for (i = RCU_NEXT_TAIL; i > RCU_DONE_TAIL; i--)
  		if (rsclp->tails[i] != rsclp->tails[i - 1])
  			break;
+	rcu_segcblist_inc_seglen(rsclp, i);
  	WRITE_ONCE(*rsclp->tails[i], rhp);
  	for (; i <= RCU_NEXT_TAIL; i++)
  		WRITE_ONCE(rsclp->tails[i], &rhp->next);
  	return true;
  }
 
-/*
- * Extract only the counts from the specified rcu_segcblist structure,
- * and place them in the specified rcu_cblist structure.  This function
- * supports both callback orphaning and invocation, hence the separation
- * of counts and callbacks.  (Callbacks ready for invocation must be
- * orphaned and adopted separately from pending callbacks, but counts
- * apply to all callbacks.  Locking must be used to make sure that
- * both orphaned-callbacks lists are consistent.)
- */
-void rcu_segcblist_extract_count(struct rcu_segcblist *rsclp,
-					       struct rcu_cblist *rclp)
-{
-	rclp->len = rcu_segcblist_xchg_len(rsclp, 0);
-}
-
  /*
   * Extract only those callbacks ready to be invoked from the specified
   * rcu_segcblist structure and place them in the specified rcu_cblist
@@ -307,6 +317,7 @@ void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp,
 
  	if (!rcu_segcblist_ready_cbs(rsclp))
  		return; /* Nothing to do. */
+	rclp->len = rcu_segcblist_get_seglen(rsclp, RCU_DONE_TAIL);
  	*rclp->tail = rsclp->head;
  	WRITE_ONCE(rsclp->head, *rsclp->tails[RCU_DONE_TAIL]);
  	WRITE_ONCE(*rsclp->tails[RCU_DONE_TAIL], NULL);
@@ -314,6 +325,7 @@ void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp,
  	for (i = RCU_CBLIST_NSEGS - 1; i >= RCU_DONE_TAIL; i--)
  		if (rsclp->tails[i] == rsclp->tails[RCU_DONE_TAIL])
  			WRITE_ONCE(rsclp->tails[i], &rsclp->head);
+	rcu_segcblist_set_seglen(rsclp, RCU_DONE_TAIL, 0);
  }
 
  /*
@@ -330,11 +342,16 @@ void rcu_segcblist_extract_pend_cbs(struct rcu_segcblist *rsclp,
 
  	if (!rcu_segcblist_pend_cbs(rsclp))
  		return; /* Nothing to do. */
+	rclp->len = rcu_segcblist_get_seglen(rsclp, RCU_WAIT_TAIL) +
+		    rcu_segcblist_get_seglen(rsclp, RCU_NEXT_READY_TAIL) +
+		    rcu_segcblist_get_seglen(rsclp, RCU_NEXT_TAIL);
  	*rclp->tail = *rsclp->tails[RCU_DONE_TAIL];
  	rclp->tail = rsclp->tails[RCU_NEXT_TAIL];
  	WRITE_ONCE(*rsclp->tails[RCU_DONE_TAIL], NULL);
-	for (i = RCU_DONE_TAIL + 1; i < RCU_CBLIST_NSEGS; i++)
+	for (i = RCU_DONE_TAIL + 1; i < RCU_CBLIST_NSEGS; i++) {
  		WRITE_ONCE(rsclp->tails[i], rsclp->tails[RCU_DONE_TAIL]);
+		rcu_segcblist_set_seglen(rsclp, i, 0);
+	}
  }
 
  /*
@@ -345,7 +362,6 @@ void rcu_segcblist_insert_count(struct rcu_segcblist *rsclp,
  				struct rcu_cblist *rclp)
  {
  	rcu_segcblist_add_len(rsclp, rclp->len);
-	rclp->len = 0;
  }
 
  /*
@@ -359,6 +375,7 @@ void rcu_segcblist_insert_done_cbs(struct rcu_segcblist *rsclp,
 
  	if (!rclp->head)
  		return; /* No callbacks to move. */
+	rcu_segcblist_add_seglen(rsclp, RCU_DONE_TAIL, rclp->len);
  	*rclp->tail = rsclp->head;
  	WRITE_ONCE(rsclp->head, rclp->head);
  	for (i = RCU_DONE_TAIL; i < RCU_CBLIST_NSEGS; i++)
@@ -379,6 +396,8 @@ void rcu_segcblist_insert_pend_cbs(struct rcu_segcblist *rsclp,
  {
  	if (!rclp->head)
  		return; /* Nothing to do. */
+
+	rcu_segcblist_add_seglen(rsclp, RCU_NEXT_TAIL, rclp->len);
  	WRITE_ONCE(*rsclp->tails[RCU_NEXT_TAIL], rclp->head);
  	WRITE_ONCE(rsclp->tails[RCU_NEXT_TAIL], rclp->tail);
  }
@@ -403,6 +422,7 @@ void rcu_segcblist_advance(struct rcu_segcblist *rsclp, unsigned long seq)
  		if (ULONG_CMP_LT(seq, rsclp->gp_seq[i]))
  			break;
  		WRITE_ONCE(rsclp->tails[RCU_DONE_TAIL], rsclp->tails[i]);
+		rcu_segcblist_move_seglen(rsclp, i, RCU_DONE_TAIL);
  	}
 
  	/* If no callbacks moved, nothing more need be done. */
@@ -423,6 +443,7 @@ void rcu_segcblist_advance(struct rcu_segcblist *rsclp, unsigned long seq)
  		if (rsclp->tails[j] == rsclp->tails[RCU_NEXT_TAIL])
  			break;  /* No more callbacks. */
  		WRITE_ONCE(rsclp->tails[j], rsclp->tails[i]);
+		rcu_segcblist_move_seglen(rsclp, i, j);
  		rsclp->gp_seq[j] = rsclp->gp_seq[i];
  	}
  }
@@ -444,7 +465,7 @@ void rcu_segcblist_advance(struct rcu_segcblist *rsclp, unsigned long seq)
   */
  bool rcu_segcblist_accelerate(struct rcu_segcblist *rsclp, unsigned long seq)
  {
-	int i;
+	int i, j;
 
  	WARN_ON_ONCE(!rcu_segcblist_is_enabled(rsclp));
  	if (rcu_segcblist_restempty(rsclp, RCU_DONE_TAIL))
@@ -487,6 +508,10 @@ bool rcu_segcblist_accelerate(struct rcu_segcblist *rsclp, unsigned long seq)
  	if (rcu_segcblist_restempty(rsclp, i) || ++i >= RCU_NEXT_TAIL)
  		return false;
 
+	/* Accounting: everything below i is about to get merged into i. */
+	for (j = i + 1; j <= RCU_NEXT_TAIL; j++)
+		rcu_segcblist_move_seglen(rsclp, j, i);
+
  	/*
  	 * Merge all later callbacks, including newly arrived callbacks,
  	 * into the segment located by the for-loop above.  Assign "seq"
@@ -514,13 +539,24 @@ void rcu_segcblist_merge(struct rcu_segcblist *dst_rsclp,
  	struct rcu_cblist donecbs;
  	struct rcu_cblist pendcbs;
 
+	lockdep_assert_cpus_held();
+
  	rcu_cblist_init(&donecbs);
  	rcu_cblist_init(&pendcbs);
-	rcu_segcblist_extract_count(src_rsclp, &donecbs);
+
  	rcu_segcblist_extract_done_cbs(src_rsclp, &donecbs);
  	rcu_segcblist_extract_pend_cbs(src_rsclp, &pendcbs);
+
+	/*
+	 * No need smp_mb() before setting length to 0, because CPU hotplug
+	 * lock excludes rcu_barrier.
+	 */
+	rcu_segcblist_set_len(src_rsclp, 0);
+
  	rcu_segcblist_insert_count(dst_rsclp, &donecbs);
+	rcu_segcblist_insert_count(dst_rsclp, &pendcbs);
  	rcu_segcblist_insert_done_cbs(dst_rsclp, &donecbs);
  	rcu_segcblist_insert_pend_cbs(dst_rsclp, &pendcbs);
+
  	rcu_segcblist_init(src_rsclp);
  }
kernel/rcu/rcu_segcblist.h +0 -2
index 1d2d61406463..cd35c9faaf51 100644
@@ -89,8 +89,6 @@ void rcu_segcblist_enqueue(struct rcu_segcblist *rsclp,
  			   struct rcu_head *rhp);
  bool rcu_segcblist_entrain(struct rcu_segcblist *rsclp,
  			   struct rcu_head *rhp);
-void rcu_segcblist_extract_count(struct rcu_segcblist *rsclp,
-				 struct rcu_cblist *rclp);
  void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp,
  				    struct rcu_cblist *rclp);
  void rcu_segcblist_extract_pend_cbs(struct rcu_segcblist *rsclp,
Signature · 1 line
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation
maintainer
#6 lore raw

Re: [PATCH v9 2/7] rcu/segcblist: Add counters to segcblist datastructure

In reply to Joel Fernandes (Google)
Quoted 6 lines · Joel Fernandes (Google)

Add counting of segment lengths of segmented callback list.

This will be useful for a number of things such as knowing how big the ready-to-execute segment have gotten. The immediate benefit is ability to trace how the callbacks in the segmented callback list change.

Quoted 6 lines

Also this patch remove hacks related to using donecbs's ->len field as a temporary variable to save the segmented callback list's length. This cannot be done anymore and is not needed.

Thank you both!

A few questions and comments below.

Thanx, Paul

Quoted 43 lines
---
 include/linux/rcu_segcblist.h |   1 +
 kernel/rcu/rcu_segcblist.c    | 120 ++++++++++++++++++++++------------
 kernel/rcu/rcu_segcblist.h    |   2 -
 3 files changed, 79 insertions(+), 44 deletions(-)
include/linux/rcu_segcblist.h +1 -0
index b36afe7b22c9..6c01f09a6456 100644
@@ -72,6 +72,7 @@ struct rcu_segcblist {
 #else
 	long len;
 #endif
+	long seglen[RCU_CBLIST_NSEGS];
 	u8 enabled;
 	u8 offloaded;
 };
kernel/rcu/rcu_segcblist.c +6 -2
index bb246d8c6ef1..357c19bbcb00 100644
@@ -7,10 +7,11 @@
  * Authors: Paul E. McKenney <[email protected]>
  */
 
-#include <linux/types.h>
-#include <linux/kernel.h>
+#include <linux/cpu.h>
 #include <linux/interrupt.h>
+#include <linux/kernel.h>
 #include <linux/rcupdate.h>
+#include <linux/types.h>
 
 #include "rcu_segcblist.h"
 
@@ -88,6 +89,46 @@ static void rcu_segcblist_set_len(struct rcu_segcblist *rsclp, long v)
 #endif
 }
 
+/* Get the length of a segment of the rcu_segcblist structure. */
+static long rcu_segcblist_get_seglen(struct rcu_segcblist *rsclp, int seg)
+{

kernel/rcu/rcu_segcblist.c static void rcu_segcblist_set_len(struct rcu_segcblist *rsclp, long v) · line 104

Quoted 10 lines
kernel/rcu/rcu_segcblist.c +10 -0
+	return READ_ONCE(rsclp->seglen[seg]);
+}
+
+/* Set the length of a segment of the rcu_segcblist structure. */
+static void rcu_segcblist_set_seglen(struct rcu_segcblist *rsclp, int seg, long v)
+{
+	WRITE_ONCE(rsclp->seglen[seg], v);
+}
+
+/* Return number of callbacks in a segment of the segmented callback list. */

A casual reader might be forgiven for being confused by the combination of "Return" in the above comment and the "void" function type below. So shouldn't this comment be something like "Add the specified number of callbacks to the specified segment..."?

Quoted 123 lines
kernel/rcu/rcu_segcblist.c +34 -36
+static void rcu_segcblist_add_seglen(struct rcu_segcblist *rsclp, int seg, long v)
+{
+	WRITE_ONCE(rsclp->seglen[seg], rsclp->seglen[seg] + v);
+}
+
+/* Move from's segment length to to's segment. */
+static void rcu_segcblist_move_seglen(struct rcu_segcblist *rsclp, int from, int to)
+{
+	long len;
+
+	if (from == to)
+		return;
+
+	len = rcu_segcblist_get_seglen(rsclp, from);
+	if (!len)
+		return;
+
+	rcu_segcblist_add_seglen(rsclp, to, len);
+	rcu_segcblist_set_seglen(rsclp, from, 0);
+}
+
+/* Increment segment's length. */
+static void rcu_segcblist_inc_seglen(struct rcu_segcblist *rsclp, int seg)
+{
+	rcu_segcblist_add_seglen(rsclp, seg, 1);
+}
+
 /*
  * Increase the numeric length of an rcu_segcblist structure by the
  * specified amount, which can be negative.  This can cause the ->len
@@ -119,26 +160,6 @@ void rcu_segcblist_inc_len(struct rcu_segcblist *rsclp)
 	rcu_segcblist_add_len(rsclp, 1);
 }
 
-/*
- * Exchange the numeric length of the specified rcu_segcblist structure
- * with the specified value.  This can cause the ->len field to disagree
- * with the actual number of callbacks on the structure.  This exchange is
- * fully ordered with respect to the callers accesses both before and after.
- */
-static long rcu_segcblist_xchg_len(struct rcu_segcblist *rsclp, long v)
-{
-#ifdef CONFIG_RCU_NOCB_CPU
-	return atomic_long_xchg(&rsclp->len, v);
-#else
-	long ret = rsclp->len;
-
-	smp_mb(); /* Up to the caller! */
-	WRITE_ONCE(rsclp->len, v);
-	smp_mb(); /* Up to the caller! */
-	return ret;
-#endif
-}
-
 /*
  * Initialize an rcu_segcblist structure.
  */
@@ -149,8 +170,10 @@ void rcu_segcblist_init(struct rcu_segcblist *rsclp)
 	BUILD_BUG_ON(RCU_NEXT_TAIL + 1 != ARRAY_SIZE(rsclp->gp_seq));
 	BUILD_BUG_ON(ARRAY_SIZE(rsclp->tails) != ARRAY_SIZE(rsclp->gp_seq));
 	rsclp->head = NULL;
-	for (i = 0; i < RCU_CBLIST_NSEGS; i++)
+	for (i = 0; i < RCU_CBLIST_NSEGS; i++) {
 		rsclp->tails[i] = &rsclp->head;
+		rcu_segcblist_set_seglen(rsclp, i, 0);
+	}
 	rcu_segcblist_set_len(rsclp, 0);
 	rsclp->enabled = 1;
 }
@@ -246,6 +269,7 @@ void rcu_segcblist_enqueue(struct rcu_segcblist *rsclp,
 {
 	rcu_segcblist_inc_len(rsclp);
 	smp_mb(); /* Ensure counts are updated before callback is enqueued. */
+	rcu_segcblist_inc_seglen(rsclp, RCU_NEXT_TAIL);
 	rhp->next = NULL;
 	WRITE_ONCE(*rsclp->tails[RCU_NEXT_TAIL], rhp);
 	WRITE_ONCE(rsclp->tails[RCU_NEXT_TAIL], &rhp->next);
@@ -274,27 +298,13 @@ bool rcu_segcblist_entrain(struct rcu_segcblist *rsclp,
 	for (i = RCU_NEXT_TAIL; i > RCU_DONE_TAIL; i--)
 		if (rsclp->tails[i] != rsclp->tails[i - 1])
 			break;
+	rcu_segcblist_inc_seglen(rsclp, i);
 	WRITE_ONCE(*rsclp->tails[i], rhp);
 	for (; i <= RCU_NEXT_TAIL; i++)
 		WRITE_ONCE(rsclp->tails[i], &rhp->next);
 	return true;
 }
 
-/*
- * Extract only the counts from the specified rcu_segcblist structure,
- * and place them in the specified rcu_cblist structure.  This function
- * supports both callback orphaning and invocation, hence the separation
- * of counts and callbacks.  (Callbacks ready for invocation must be
- * orphaned and adopted separately from pending callbacks, but counts
- * apply to all callbacks.  Locking must be used to make sure that
- * both orphaned-callbacks lists are consistent.)
- */
-void rcu_segcblist_extract_count(struct rcu_segcblist *rsclp,
-					       struct rcu_cblist *rclp)
-{
-	rclp->len = rcu_segcblist_xchg_len(rsclp, 0);
-}
-
 /*
  * Extract only those callbacks ready to be invoked from the specified
  * rcu_segcblist structure and place them in the specified rcu_cblist
@@ -307,6 +317,7 @@ void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp,
 
 	if (!rcu_segcblist_ready_cbs(rsclp))
 		return; /* Nothing to do. */
+	rclp->len = rcu_segcblist_get_seglen(rsclp, RCU_DONE_TAIL);
 	*rclp->tail = rsclp->head;
 	WRITE_ONCE(rsclp->head, *rsclp->tails[RCU_DONE_TAIL]);
 	WRITE_ONCE(*rsclp->tails[RCU_DONE_TAIL], NULL);
@@ -314,6 +325,7 @@ void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp,
 	for (i = RCU_CBLIST_NSEGS - 1; i >= RCU_DONE_TAIL; i--)
 		if (rsclp->tails[i] == rsclp->tails[RCU_DONE_TAIL])
 			WRITE_ONCE(rsclp->tails[i], &rsclp->head);
+	rcu_segcblist_set_seglen(rsclp, RCU_DONE_TAIL, 0);
 }
 
 /*

kernel/rcu/rcu_segcblist.c void rcu_segcblist_extract_pend_cbs(struct rcu_segcblist *rsclp, · line 347

Quoted 6 lines
kernel/rcu/rcu_segcblist.c +3 -0
@@ -330,11 +342,16 @@ void rcu_segcblist_extract_pend_cbs(struct rcu_segcblist *rsclp,
 
 	if (!rcu_segcblist_pend_cbs(rsclp))
 		return; /* Nothing to do. */
+	rclp->len = rcu_segcblist_get_seglen(rsclp, RCU_WAIT_TAIL) +
+		    rcu_segcblist_get_seglen(rsclp, RCU_NEXT_READY_TAIL) +
+		    rcu_segcblist_get_seglen(rsclp, RCU_NEXT_TAIL);

This should be a "for" loop. Yes, the number and names of the segments hasn't changed for a good long time, but nothing like code as above to inspire Murphy to more mischief. :-/

Actually, why not put the summation in the existing "for" loop below? That would save a line of code in addition to providing less inspiration for Mr. Murphy.

Quoted 12 lines
kernel/rcu/rcu_segcblist.c +3 -1
 	*rclp->tail = *rsclp->tails[RCU_DONE_TAIL];
 	rclp->tail = rsclp->tails[RCU_NEXT_TAIL];
 	WRITE_ONCE(*rsclp->tails[RCU_DONE_TAIL], NULL);
-	for (i = RCU_DONE_TAIL + 1; i < RCU_CBLIST_NSEGS; i++)
+	for (i = RCU_DONE_TAIL + 1; i < RCU_CBLIST_NSEGS; i++) {
 		WRITE_ONCE(rsclp->tails[i], rsclp->tails[RCU_DONE_TAIL]);
+		rcu_segcblist_set_seglen(rsclp, i, 0);
+	}
 }
 
 /*

kernel/rcu/rcu_segcblist.c void rcu_segcblist_insert_count(struct rcu_segcblist *rsclp, · line 348

Quoted 4 lines
kernel/rcu/rcu_segcblist.c +0 -1
@@ -345,7 +362,6 @@ void rcu_segcblist_insert_count(struct rcu_segcblist *rsclp,
 				struct rcu_cblist *rclp)
 {
 	rcu_segcblist_add_len(rsclp, rclp->len);
-	rclp->len = 0;

You audited the callers, correct?

Quoted 98 lines
kernel/rcu/rcu_segcblist.c +22 -2
 }
 
 /*
@@ -359,6 +375,7 @@ void rcu_segcblist_insert_done_cbs(struct rcu_segcblist *rsclp,
 
 	if (!rclp->head)
 		return; /* No callbacks to move. */
+	rcu_segcblist_add_seglen(rsclp, RCU_DONE_TAIL, rclp->len);
 	*rclp->tail = rsclp->head;
 	WRITE_ONCE(rsclp->head, rclp->head);
 	for (i = RCU_DONE_TAIL; i < RCU_CBLIST_NSEGS; i++)
@@ -379,6 +396,8 @@ void rcu_segcblist_insert_pend_cbs(struct rcu_segcblist *rsclp,
 {
 	if (!rclp->head)
 		return; /* Nothing to do. */
+
+	rcu_segcblist_add_seglen(rsclp, RCU_NEXT_TAIL, rclp->len);
 	WRITE_ONCE(*rsclp->tails[RCU_NEXT_TAIL], rclp->head);
 	WRITE_ONCE(rsclp->tails[RCU_NEXT_TAIL], rclp->tail);
 }
@@ -403,6 +422,7 @@ void rcu_segcblist_advance(struct rcu_segcblist *rsclp, unsigned long seq)
 		if (ULONG_CMP_LT(seq, rsclp->gp_seq[i]))
 			break;
 		WRITE_ONCE(rsclp->tails[RCU_DONE_TAIL], rsclp->tails[i]);
+		rcu_segcblist_move_seglen(rsclp, i, RCU_DONE_TAIL);
 	}
 
 	/* If no callbacks moved, nothing more need be done. */
@@ -423,6 +443,7 @@ void rcu_segcblist_advance(struct rcu_segcblist *rsclp, unsigned long seq)
 		if (rsclp->tails[j] == rsclp->tails[RCU_NEXT_TAIL])
 			break;  /* No more callbacks. */
 		WRITE_ONCE(rsclp->tails[j], rsclp->tails[i]);
+		rcu_segcblist_move_seglen(rsclp, i, j);
 		rsclp->gp_seq[j] = rsclp->gp_seq[i];
 	}
 }
@@ -444,7 +465,7 @@ void rcu_segcblist_advance(struct rcu_segcblist *rsclp, unsigned long seq)
  */
 bool rcu_segcblist_accelerate(struct rcu_segcblist *rsclp, unsigned long seq)
 {
-	int i;
+	int i, j;
 
 	WARN_ON_ONCE(!rcu_segcblist_is_enabled(rsclp));
 	if (rcu_segcblist_restempty(rsclp, RCU_DONE_TAIL))
@@ -487,6 +508,10 @@ bool rcu_segcblist_accelerate(struct rcu_segcblist *rsclp, unsigned long seq)
 	if (rcu_segcblist_restempty(rsclp, i) || ++i >= RCU_NEXT_TAIL)
 		return false;
 
+	/* Accounting: everything below i is about to get merged into i. */
+	for (j = i + 1; j <= RCU_NEXT_TAIL; j++)
+		rcu_segcblist_move_seglen(rsclp, j, i);
+
 	/*
 	 * Merge all later callbacks, including newly arrived callbacks,
 	 * into the segment located by the for-loop above.  Assign "seq"
@@ -514,13 +539,24 @@ void rcu_segcblist_merge(struct rcu_segcblist *dst_rsclp,
 	struct rcu_cblist donecbs;
 	struct rcu_cblist pendcbs;
 
+	lockdep_assert_cpus_held();
+
 	rcu_cblist_init(&donecbs);
 	rcu_cblist_init(&pendcbs);
-	rcu_segcblist_extract_count(src_rsclp, &donecbs);
+
 	rcu_segcblist_extract_done_cbs(src_rsclp, &donecbs);
 	rcu_segcblist_extract_pend_cbs(src_rsclp, &pendcbs);
+
+	/*
+	 * No need smp_mb() before setting length to 0, because CPU hotplug
+	 * lock excludes rcu_barrier.
+	 */
+	rcu_segcblist_set_len(src_rsclp, 0);
+
 	rcu_segcblist_insert_count(dst_rsclp, &donecbs);
+	rcu_segcblist_insert_count(dst_rsclp, &pendcbs);
 	rcu_segcblist_insert_done_cbs(dst_rsclp, &donecbs);
 	rcu_segcblist_insert_pend_cbs(dst_rsclp, &pendcbs);
+
 	rcu_segcblist_init(src_rsclp);
 }
kernel/rcu/rcu_segcblist.h +0 -2
index 1d2d61406463..cd35c9faaf51 100644
@@ -89,8 +89,6 @@ void rcu_segcblist_enqueue(struct rcu_segcblist *rsclp,
 			   struct rcu_head *rhp);
 bool rcu_segcblist_entrain(struct rcu_segcblist *rsclp,
 			   struct rcu_head *rhp);
-void rcu_segcblist_extract_count(struct rcu_segcblist *rsclp,
-				 struct rcu_cblist *rclp);
 void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp,
 				    struct rcu_cblist *rclp);
 void rcu_segcblist_extract_pend_cbs(struct rcu_segcblist *rsclp,
Signature · 1 line
2.29.1.341.ge80a0c044ae-goog
#7 lore raw

Re: [PATCH v9 2/7] rcu/segcblist: Add counters to segcblist datastructure

Quoted 4 lines · Paul E. McKenney

A casual reader might be forgiven for being confused by the combination of "Return" in the above comment and the "void" function type below. So shouldn't this comment be something like "Add the specified number of callbacks to the specified segment..."?

You are right, sorry and will fix it.

Quoted 12 lines

kernel/rcu/rcu_segcblist.c void rcu_segcblist_extract_pend_cbs(struct rcu_segcblist *rsclp, · line 347

Quoted 7 lines
kernel/rcu/rcu_segcblist.c +3 -0
@@ -330,11 +342,16 @@ void rcu_segcblist_extract_pend_cbs(struct rcu_segcblist *rsclp,
 
 	if (!rcu_segcblist_pend_cbs(rsclp))
 		return; /* Nothing to do. */
+	rclp->len = rcu_segcblist_get_seglen(rsclp, RCU_WAIT_TAIL) +
+		    rcu_segcblist_get_seglen(rsclp, RCU_NEXT_READY_TAIL) +
+		    rcu_segcblist_get_seglen(rsclp, RCU_NEXT_TAIL);

This should be a "for" loop. Yes, the number and names of the segments hasn't changed for a good long time, but nothing like code as above to inspire Murphy to more mischief. :-/

Quoted 3 lines

Actually, why not put the summation in the existing "for" loop below? That would save a line of code in addition to providing less inspiration for Mr. Murphy.

I can do that. Actually Frederic suggested the same thing but I was reluctant as I felt it did not give much LOC benefit. Will revisit it.

Quoted 11 lines
Quoted 11 lines
kernel/rcu/rcu_segcblist.c +3 -1
 	*rclp->tail = *rsclp->tails[RCU_DONE_TAIL];
 	rclp->tail = rsclp->tails[RCU_NEXT_TAIL];
 	WRITE_ONCE(*rsclp->tails[RCU_DONE_TAIL], NULL);
-	for (i = RCU_DONE_TAIL + 1; i < RCU_CBLIST_NSEGS; i++)
+	for (i = RCU_DONE_TAIL + 1; i < RCU_CBLIST_NSEGS; i++) {
 		WRITE_ONCE(rsclp->tails[i], rsclp->tails[RCU_DONE_TAIL]);
+		rcu_segcblist_set_seglen(rsclp, i, 0);
+	}
 }
 
Quoted 8 lines

kernel/rcu/rcu_segcblist.c void rcu_segcblist_insert_count(struct rcu_segcblist *rsclp, · line 348

Quoted 6 lines
kernel/rcu/rcu_segcblist.c +0 -1
 /*
@@ -345,7 +362,6 @@ void rcu_segcblist_insert_count(struct rcu_segcblist *rsclp,
 				struct rcu_cblist *rclp)
 {
 	rcu_segcblist_add_len(rsclp, rclp->len);
-	rclp->len = 0;

You audited the callers, correct?

Yep.

thanks,

- Joel

#8 lore raw

Re: [PATCH v9 2/7] rcu/segcblist: Add counters to segcblist datastructure

Quoted 21 lines · Joel Fernandes
Quoted 4 lines · Paul E. McKenney

A casual reader might be forgiven for being confused by the combination of "Return" in the above comment and the "void" function type below. So shouldn't this comment be something like "Add the specified number of callbacks to the specified segment..."?

You are right, sorry and will fix it.

Quoted 12 lines

Review void rcu_segcblist_extract_pend_cbs(struct rcu_segcblist *rsclp, · line 347

Quoted 7 lines
(quoted diff) +3 -0
@@ -330,11 +342,16 @@ void rcu_segcblist_extract_pend_cbs(struct rcu_segcblist *rsclp,
 
 	if (!rcu_segcblist_pend_cbs(rsclp))
 		return; /* Nothing to do. */
+	rclp->len = rcu_segcblist_get_seglen(rsclp, RCU_WAIT_TAIL) +
+		    rcu_segcblist_get_seglen(rsclp, RCU_NEXT_READY_TAIL) +
+		    rcu_segcblist_get_seglen(rsclp, RCU_NEXT_TAIL);

This should be a "for" loop. Yes, the number and names of the segments hasn't changed for a good long time, but nothing like code as above to inspire Murphy to more mischief. :-/

Quoted 6 lines
Quoted 3 lines

Actually, why not put the summation in the existing "for" loop below? That would save a line of code in addition to providing less inspiration for Mr. Murphy.

I can do that. Actually Frederic suggested the same thing but I was reluctant as I felt it did not give much LOC benefit. Will revisit it.

It reduces 1 line of code :) I changed it to the below, will update the patch:

---8<-----------------------

kernel/rcu/rcu_segcblist.c +3 -4
index 9b43d686b1f3..bff9b2253e50 100644
@@ -101,7 +101,7 @@ static void rcu_segcblist_set_seglen(struct rcu_segcblist *rsclp, int seg, long
 	WRITE_ONCE(rsclp->seglen[seg], v);
 }
 
-/* Return number of callbacks in a segment of the segmented callback list. */
+/* Increase the numeric length of a segment by a specified amount. */
 static void rcu_segcblist_add_seglen(struct rcu_segcblist *rsclp, int seg, long v)
 {
 	WRITE_ONCE(rsclp->seglen[seg], rsclp->seglen[seg] + v);
@@ -406,13 +406,12 @@ void rcu_segcblist_extract_pend_cbs(struct rcu_segcblist *rsclp,
 
 	if (!rcu_segcblist_pend_cbs(rsclp))
 		return; /* Nothing to do. */
-	rclp->len = rcu_segcblist_get_seglen(rsclp, RCU_WAIT_TAIL) +
-		    rcu_segcblist_get_seglen(rsclp, RCU_NEXT_READY_TAIL) +
-		    rcu_segcblist_get_seglen(rsclp, RCU_NEXT_TAIL);
+	rclp->len = 0;
 	*rclp->tail = *rsclp->tails[RCU_DONE_TAIL];
 	rclp->tail = rsclp->tails[RCU_NEXT_TAIL];
 	WRITE_ONCE(*rsclp->tails[RCU_DONE_TAIL], NULL);
 	for (i = RCU_DONE_TAIL + 1; i < RCU_CBLIST_NSEGS; i++) {
+		rclp->len += rcu_segcblist_get_seglen(rsclp, i);
 		WRITE_ONCE(rsclp->tails[i], rsclp->tails[RCU_DONE_TAIL]);
 		rcu_segcblist_set_seglen(rsclp, i, 0);
 	}
maintainer
#9 lore raw

Re: [PATCH v9 2/7] rcu/segcblist: Add counters to segcblist datastructure

Quoted 22 lines · Joel Fernandes
Quoted 21 lines · Joel Fernandes
Quoted 4 lines · Paul E. McKenney

A casual reader might be forgiven for being confused by the combination of "Return" in the above comment and the "void" function type below. So shouldn't this comment be something like "Add the specified number of callbacks to the specified segment..."?

You are right, sorry and will fix it.

Quoted 12 lines

kernel/rcu/rcu_segcblist.c void rcu_segcblist_extract_pend_cbs(struct rcu_segcblist *rsclp, · line 347

Quoted 7 lines
kernel/rcu/rcu_segcblist.c +3 -0
@@ -330,11 +342,16 @@ void rcu_segcblist_extract_pend_cbs(struct rcu_segcblist *rsclp,
 
 	if (!rcu_segcblist_pend_cbs(rsclp))
 		return; /* Nothing to do. */
+	rclp->len = rcu_segcblist_get_seglen(rsclp, RCU_WAIT_TAIL) +
+		    rcu_segcblist_get_seglen(rsclp, RCU_NEXT_READY_TAIL) +
+		    rcu_segcblist_get_seglen(rsclp, RCU_NEXT_TAIL);

This should be a "for" loop. Yes, the number and names of the segments hasn't changed for a good long time, but nothing like code as above to inspire Murphy to more mischief. :-/

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Actually, why not put the summation in the existing "for" loop below? That would save a line of code in addition to providing less inspiration for Mr. Murphy.

I can do that. Actually Frederic suggested the same thing but I was reluctant as I felt it did not give much LOC benefit. Will revisit it.

It reduces 1 line of code :) I changed it to the below, will update the patch:

Thank you! And yes, I am much more concerned about the constraints on Mr. Murphy than on the lines of code. ;-)

Thanx, Paul

Quoted 31 lines

---8<-----------------------

kernel/rcu/rcu_segcblist.c +3 -4
index 9b43d686b1f3..bff9b2253e50 100644
@@ -101,7 +101,7 @@ static void rcu_segcblist_set_seglen(struct rcu_segcblist *rsclp, int seg, long
 	WRITE_ONCE(rsclp->seglen[seg], v);
 }
 
-/* Return number of callbacks in a segment of the segmented callback list. */
+/* Increase the numeric length of a segment by a specified amount. */
 static void rcu_segcblist_add_seglen(struct rcu_segcblist *rsclp, int seg, long v)
 {
 	WRITE_ONCE(rsclp->seglen[seg], rsclp->seglen[seg] + v);
@@ -406,13 +406,12 @@ void rcu_segcblist_extract_pend_cbs(struct rcu_segcblist *rsclp,
 
 	if (!rcu_segcblist_pend_cbs(rsclp))
 		return; /* Nothing to do. */
-	rclp->len = rcu_segcblist_get_seglen(rsclp, RCU_WAIT_TAIL) +
-		    rcu_segcblist_get_seglen(rsclp, RCU_NEXT_READY_TAIL) +
-		    rcu_segcblist_get_seglen(rsclp, RCU_NEXT_TAIL);
+	rclp->len = 0;
 	*rclp->tail = *rsclp->tails[RCU_DONE_TAIL];
 	rclp->tail = rsclp->tails[RCU_NEXT_TAIL];
 	WRITE_ONCE(*rsclp->tails[RCU_DONE_TAIL], NULL);
 	for (i = RCU_DONE_TAIL + 1; i < RCU_CBLIST_NSEGS; i++) {
+		rclp->len += rcu_segcblist_get_seglen(rsclp, i);
 		WRITE_ONCE(rsclp->tails[i], rsclp->tails[RCU_DONE_TAIL]);
 		rcu_segcblist_set_seglen(rsclp, i, 0);
 	}
maintainer
#10 lore raw

Re: [PATCH v9 2/7] rcu/segcblist: Add counters to segcblist datastructure

Quoted 27 lines · Paul E. McKenney
Quoted 26 lines · Joel Fernandes
Quoted 25 lines · Joel Fernandes
Quoted 4 lines · Paul E. McKenney

A casual reader might be forgiven for being confused by the combination of "Return" in the above comment and the "void" function type below. So shouldn't this comment be something like "Add the specified number of callbacks to the specified segment..."?

You are right, sorry and will fix it.

Quoted 15 lines

kernel/rcu/rcu_segcblist.c void rcu_segcblist_extract_pend_cbs(struct rcu_segcblist *rsclp, · line 347

Quoted 7 lines
kernel/rcu/rcu_segcblist.c +3 -0
@@ -330,11 +342,16 @@ void rcu_segcblist_extract_pend_cbs(struct rcu_segcblist *rsclp,
 
 	if (!rcu_segcblist_pend_cbs(rsclp))
 		return; /* Nothing to do. */
+	rclp->len = rcu_segcblist_get_seglen(rsclp, RCU_WAIT_TAIL) +
+		    rcu_segcblist_get_seglen(rsclp, RCU_NEXT_READY_TAIL) +
+		    rcu_segcblist_get_seglen(rsclp, RCU_NEXT_TAIL);

This should be a "for" loop. Yes, the number and names of the segments hasn't changed for a good long time, but nothing like code as above to inspire Murphy to more mischief. :-/

Actually, why not put the summation in the existing "for" loop below? That would save a line of code in addition to providing less inspiration for Mr. Murphy.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

I can do that. Actually Frederic suggested the same thing but I was reluctant as I felt it did not give much LOC benefit. Will revisit it.

It reduces 1 line of code :) I changed it to the below, will update the patch:

Thank you! And yes, I am much more concerned about the constraints on Mr. Murphy than on the lines of code. ;-)

And I have pulled in the updated commit, thank you all!

Thanx, Paul

Quoted 31 lines
Quoted 31 lines

---8<-----------------------

kernel/rcu/rcu_segcblist.c +3 -4
index 9b43d686b1f3..bff9b2253e50 100644
@@ -101,7 +101,7 @@ static void rcu_segcblist_set_seglen(struct rcu_segcblist *rsclp, int seg, long
 	WRITE_ONCE(rsclp->seglen[seg], v);
 }
 
-/* Return number of callbacks in a segment of the segmented callback list. */
+/* Increase the numeric length of a segment by a specified amount. */
 static void rcu_segcblist_add_seglen(struct rcu_segcblist *rsclp, int seg, long v)
 {
 	WRITE_ONCE(rsclp->seglen[seg], rsclp->seglen[seg] + v);
@@ -406,13 +406,12 @@ void rcu_segcblist_extract_pend_cbs(struct rcu_segcblist *rsclp,
 
 	if (!rcu_segcblist_pend_cbs(rsclp))
 		return; /* Nothing to do. */
-	rclp->len = rcu_segcblist_get_seglen(rsclp, RCU_WAIT_TAIL) +
-		    rcu_segcblist_get_seglen(rsclp, RCU_NEXT_READY_TAIL) +
-		    rcu_segcblist_get_seglen(rsclp, RCU_NEXT_TAIL);
+	rclp->len = 0;
 	*rclp->tail = *rsclp->tails[RCU_DONE_TAIL];
 	rclp->tail = rsclp->tails[RCU_NEXT_TAIL];
 	WRITE_ONCE(*rsclp->tails[RCU_DONE_TAIL], NULL);
 	for (i = RCU_DONE_TAIL + 1; i < RCU_CBLIST_NSEGS; i++) {
+		rclp->len += rcu_segcblist_get_seglen(rsclp, i);
 		WRITE_ONCE(rsclp->tails[i], rsclp->tails[RCU_DONE_TAIL]);
 		rcu_segcblist_set_seglen(rsclp, i, 0);
 	}
#11 lore raw

PATCH v9 3/7 srcu: Fix invoke_rcu_callbacks() segcb length adjustment

In reply to Joel Fernandes (Google)

With earlier patches, the negative counting of the unsegmented list cannot be used to adjust the segmented one. To fix this, sample the unsegmented length in advance, and use it after CB execution to adjust the segmented list's length.

---
 kernel/rcu/srcutree.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/rcu/srcutree.c +4 -1
index 0f23d20d485a..79b7081143a7 100644
@@ -1160,6 +1160,7 @@ static void srcu_advance_state(struct srcu_struct *ssp)
  */
 static void srcu_invoke_callbacks(struct work_struct *work)
 {
+	long len;
 	bool more;
 	struct rcu_cblist ready_cbs;
 	struct rcu_head *rhp;
@@ -1182,6 +1183,7 @@ static void srcu_invoke_callbacks(struct work_struct *work)
 	/* We are on the job!  Extract and invoke ready callbacks. */
 	sdp->srcu_cblist_invoking = true;
 	rcu_segcblist_extract_done_cbs(&sdp->srcu_cblist, &ready_cbs);
+	len = ready_cbs.len;
 	spin_unlock_irq_rcu_node(sdp);
 	rhp = rcu_cblist_dequeue(&ready_cbs);
 	for (; rhp != NULL; rhp = rcu_cblist_dequeue(&ready_cbs)) {
@@ -1190,13 +1192,14 @@ static void srcu_invoke_callbacks(struct work_struct *work)
 		rhp->func(rhp);
 		local_bh_enable();
 	}
+	WARN_ON_ONCE(ready_cbs.len);
 
 	/*
 	 * Update counts, accelerate new callbacks, and if needed,
 	 * schedule another round of callback invocation.
 	 */
 	spin_lock_irq_rcu_node(sdp);
-	rcu_segcblist_insert_count(&sdp->srcu_cblist, &ready_cbs);
+	rcu_segcblist_add_len(&sdp->srcu_cblist, -len);
 	(void)rcu_segcblist_accelerate(&sdp->srcu_cblist,
 				       rcu_seq_snap(&ssp->srcu_gp_seq));
 	sdp->srcu_cblist_invoking = false;
Signature · 1 line
2.29.1.341.ge80a0c044ae-goog
#12 lore raw

Re: [PATCH v9 3/7] srcu: Fix invoke_rcu_callbacks() segcb length adjustment

Quoted 8 lines · Joel Fernandes (Google)

With earlier patches, the negative counting of the unsegmented list cannot be used to adjust the segmented one. To fix this, sample the unsegmented length in advance, and use it after CB execution to adjust the segmented list's length.

This breaks bisection, you need to either fix up the previous patch by adding this diff inside or better yet: expand what you did in "rcu/tree: Make rcu_do_batch count how many callbacks were executed" to also handle srcu before introducing the segcb count.

Thanks.

#13 lore raw

Re: [PATCH v9 3/7] srcu: Fix invoke_rcu_callbacks() segcb length adjustment

Quoted 6 lines · Frederic Weisbecker
Quoted 5 lines · Joel Fernandes (Google)

With earlier patches, the negative counting of the unsegmented list cannot be used to adjust the segmented one. To fix this, sample the unsegmented length in advance, and use it after CB execution to adjust the segmented list's length.

Quoted 8 lines
Quoted 3 lines

This breaks bisection, you need to either fix up the previous patch by adding this diff inside or better yet: expand what you did in "rcu/tree: Make rcu_do_batch count how many callbacks were executed" to also handle srcu before introducing the segcb count.

Right, the latter is better. Let us do that.

thanks,

- Joel

#14 lore raw

Re: [PATCH v9 3/7] srcu: Fix invoke_rcu_callbacks() segcb length adjustment

In reply to Frederic Weisbecker
Quoted 6 lines · Frederic Weisbecker
Quoted 5 lines · Joel Fernandes (Google)

With earlier patches, the negative counting of the unsegmented list cannot be used to adjust the segmented one. To fix this, sample the unsegmented length in advance, and use it after CB execution to adjust the segmented list's length.

Quoted 8 lines
Quoted 3 lines

This breaks bisection, you need to either fix up the previous patch by adding this diff inside or better yet: expand what you did in "rcu/tree: Make rcu_do_batch count how many callbacks were executed" to also handle srcu before introducing the segcb count.

Since doing the latter is a lot more tedious and I want to get reviewing other's RCU patches today :) , I just squashed the suggestion into the counters patch to fix bissection:
https://git.kernel.org/pub/scm/linux/kernel/git/jfern/linux.git/commit/?h=rcu/segcb-counts&id=595e3a65eeef109cb8fcbfcc114fd3ea2064b873

Hope that's Ok. Also, so that I don't have to resend everything, here is the final branch if Paul wants to take it:
git://git.kernel.org/pub/scm/linux/kernel/git/jfern/linux.git (branch rcu/segcb-counts)

Thank you for your time, Frederick!

- Joel

maintainer
#15 lore raw

Re: [PATCH v9 3/7] srcu: Fix invoke_rcu_callbacks() segcb length adjustment

Quoted 16 lines · Joel Fernandes
Quoted 6 lines · Frederic Weisbecker
Quoted 5 lines · Joel Fernandes (Google)

With earlier patches, the negative counting of the unsegmented list cannot be used to adjust the segmented one. To fix this, sample the unsegmented length in advance, and use it after CB execution to adjust the segmented list's length.

Quoted 8 lines
Quoted 3 lines

This breaks bisection, you need to either fix up the previous patch by adding this diff inside or better yet: expand what you did in "rcu/tree: Make rcu_do_batch count how many callbacks were executed" to also handle srcu before introducing the segcb count.

Quoted 8 lines

Since doing the latter is a lot more tedious and I want to get reviewing other's RCU patches today :) , I just squashed the suggestion into the counters patch to fix bissection:
https://git.kernel.org/pub/scm/linux/kernel/git/jfern/linux.git/commit/?h=rcu/segcb-counts&id=595e3a65eeef109cb8fcbfcc114fd3ea2064b873

Hope that's Ok. Also, so that I don't have to resend everything, here is the final branch if Paul wants to take it:
git://git.kernel.org/pub/scm/linux/kernel/git/jfern/linux.git (branch rcu/segcb-counts)

Either way, it sounds like it is time for me to review this series.

Thank you both very much!!!

Thaxn, Paul

#16 lore raw

Re: [PATCH v9 3/7] srcu: Fix invoke_rcu_callbacks() segcb length adjustment

In reply to Joel Fernandes
Quoted 16 lines · Joel Fernandes
Quoted 6 lines · Frederic Weisbecker
Quoted 5 lines · Joel Fernandes (Google)

With earlier patches, the negative counting of the unsegmented list cannot be used to adjust the segmented one. To fix this, sample the unsegmented length in advance, and use it after CB execution to adjust the segmented list's length.

Quoted 8 lines
Quoted 3 lines

This breaks bisection, you need to either fix up the previous patch by adding this diff inside or better yet: expand what you did in "rcu/tree: Make rcu_do_batch count how many callbacks were executed" to also handle srcu before introducing the segcb count.

Quoted 6 lines

Since doing the latter is a lot more tedious and I want to get reviewing other's RCU patches today :) , I just squashed the suggestion into the counters patch to fix bissection:
https://git.kernel.org/pub/scm/linux/kernel/git/jfern/linux.git/commit/?h=rcu/segcb-counts&id=595e3a65eeef109cb8fcbfcc114fd3ea2064b873

Hope that's Ok.

Works for me.

Thanks!

Reviewed-by on v9 3/7
#17 lore raw

Re: [PATCH v9 3/7] srcu: Fix invoke_rcu_callbacks() segcb length adjustment

In reply to Joel Fernandes (Google)
Quoted 9 lines · Joel Fernandes (Google)

With earlier patches, the negative counting of the unsegmented list cannot be used to adjust the segmented one. To fix this, sample the unsegmented length in advance, and use it after CB execution to adjust the segmented list's length.

---

Quoted 40 lines
  kernel/rcu/srcutree.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
kernel/rcu/srcutree.c +4 -1
index 0f23d20d485a..79b7081143a7 100644
@@ -1160,6 +1160,7 @@ static void srcu_advance_state(struct srcu_struct *ssp)
   */
  static void srcu_invoke_callbacks(struct work_struct *work)
  {
+	long len;
  	bool more;
  	struct rcu_cblist ready_cbs;
  	struct rcu_head *rhp;
@@ -1182,6 +1183,7 @@ static void srcu_invoke_callbacks(struct work_struct *work)
  	/* We are on the job!  Extract and invoke ready callbacks. */
  	sdp->srcu_cblist_invoking = true;
  	rcu_segcblist_extract_done_cbs(&sdp->srcu_cblist, &ready_cbs);
+	len = ready_cbs.len;
  	spin_unlock_irq_rcu_node(sdp);
  	rhp = rcu_cblist_dequeue(&ready_cbs);
  	for (; rhp != NULL; rhp = rcu_cblist_dequeue(&ready_cbs)) {
@@ -1190,13 +1192,14 @@ static void srcu_invoke_callbacks(struct work_struct *work)
  		rhp->func(rhp);
  		local_bh_enable();
  	}
+	WARN_ON_ONCE(ready_cbs.len);
 
  	/*
  	 * Update counts, accelerate new callbacks, and if needed,
  	 * schedule another round of callback invocation.
  	 */
  	spin_lock_irq_rcu_node(sdp);
-	rcu_segcblist_insert_count(&sdp->srcu_cblist, &ready_cbs);
+	rcu_segcblist_add_len(&sdp->srcu_cblist, -len);
  	(void)rcu_segcblist_accelerate(&sdp->srcu_cblist,
  				       rcu_seq_snap(&ssp->srcu_gp_seq));
  	sdp->srcu_cblist_invoking = false;
Signature · 1 line
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation
#18 lore raw

PATCH v9 4/7 rcu/trace: Add tracing for how segcb list changes

In reply to Joel Fernandes (Google)

Track how the segcb list changes before/after acceleration, during queuing and during dequeuing.

This has proved useful to discover an optimization to avoid unwanted GP requests when there are no callbacks accelerated. The overhead is minimal as each segment's length is now stored in the respective segment.

---
 include/trace/events/rcu.h | 25 +++++++++++++++++++++++++
 kernel/rcu/rcu_segcblist.c | 34 ++++++++++++++++++++++++++++++++++
 kernel/rcu/rcu_segcblist.h |  5 +++++
 kernel/rcu/tree.c          |  9 +++++++++
 4 files changed, 73 insertions(+)
include/trace/events/rcu.h +25 -0
index 155b5cb43cfd..5f8f2ee1a936 100644
@@ -505,6 +505,31 @@ TRACE_EVENT_RCU(rcu_callback,
 		  __entry->qlen)
 );
 
+TRACE_EVENT_RCU(rcu_segcb_stats,
+
+		TP_PROTO(const char *ctx, int *cb_count, unsigned long *gp_seq),
+
+		TP_ARGS(ctx, cb_count, gp_seq),
+
+		TP_STRUCT__entry(
+			__field(const char *, ctx)
+			__array(int, cb_count, RCU_CBLIST_NSEGS)
+			__array(unsigned long, gp_seq, RCU_CBLIST_NSEGS)
+		),
+
+		TP_fast_assign(
+			__entry->ctx = ctx;
+			memcpy(__entry->cb_count, cb_count, RCU_CBLIST_NSEGS * sizeof(int));
+			memcpy(__entry->gp_seq, gp_seq, RCU_CBLIST_NSEGS * sizeof(unsigned long));
+		),
+
+		TP_printk("%s cb_count: (DONE=%d, WAIT=%d, NEXT_READY=%d, NEXT=%d) "
+			  "gp_seq: (DONE=%lu, WAIT=%lu, NEXT_READY=%lu, NEXT=%lu)", __entry->ctx,
+			  __entry->cb_count[0], __entry->cb_count[1], __entry->cb_count[2], __entry->cb_count[3],
+			  __entry->gp_seq[0], __entry->gp_seq[1], __entry->gp_seq[2], __entry->gp_seq[3])
+
+);
+
 /*
  * Tracepoint for the registration of a single RCU callback of the special
  * kvfree() form.  The first argument is the RCU type, the second argument
kernel/rcu/rcu_segcblist.c +34 -0
index 357c19bbcb00..2a03949d0b82 100644
@@ -14,6 +14,7 @@
 #include <linux/types.h>
 
 #include "rcu_segcblist.h"
+#include "rcu.h"
 
 /* Initialize simple callback list. */
 void rcu_cblist_init(struct rcu_cblist *rclp)
@@ -328,6 +329,39 @@ void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp,
 	rcu_segcblist_set_seglen(rsclp, RCU_DONE_TAIL, 0);
 }
 
+/*
+ * Return how many CBs each segment along with their gp_seq values.
+ *
+ * This function is O(N) where N is the number of segments. Only used from
+ * tracing code which is usually disabled in production.
+ */
+#ifdef CONFIG_RCU_TRACE
+static void rcu_segcblist_countseq(struct rcu_segcblist *rsclp,
+			 int cbcount[RCU_CBLIST_NSEGS],
+			 unsigned long gpseq[RCU_CBLIST_NSEGS])
+{
+	int i;
+
+	for (i = 0; i < RCU_CBLIST_NSEGS; i++) {
+		cbcount[i] = rcu_segcblist_get_seglen(rsclp, i);
+		gpseq[i] = rsclp->gp_seq[i];
+	}
+}
+
+void __trace_rcu_segcb_stats(struct rcu_segcblist *rsclp, const char *context)
+{
+	int cbs[RCU_CBLIST_NSEGS];
+	unsigned long gps[RCU_CBLIST_NSEGS];
+
+	if (!trace_rcu_segcb_stats_enabled())
+		return;
+
+	rcu_segcblist_countseq(rsclp, cbs, gps);
+
+	trace_rcu_segcb_stats(context, cbs, gps);
+}
+#endif
+
 /*
  * Extract only those callbacks still pending (not yet ready to be
  * invoked) from the specified rcu_segcblist structure and place them in
kernel/rcu/rcu_segcblist.h +5 -0
index cd35c9faaf51..7750734fa116 100644
@@ -103,3 +103,8 @@ void rcu_segcblist_advance(struct rcu_segcblist *rsclp, unsigned long seq);
 bool rcu_segcblist_accelerate(struct rcu_segcblist *rsclp, unsigned long seq);
 void rcu_segcblist_merge(struct rcu_segcblist *dst_rsclp,
 			 struct rcu_segcblist *src_rsclp);
+#ifdef CONFIG_RCU_TRACE
+void __trace_rcu_segcb_stats(struct rcu_segcblist *rsclp, const char *context);
+#else
+#define __trace_rcu_segcb_stats(...)
+#endif
kernel/rcu/tree.c +9 -0
index 24c00020ab83..f6c6653b3ec2 100644
@@ -1497,6 +1497,8 @@ static bool rcu_accelerate_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
 	if (!rcu_segcblist_pend_cbs(&rdp->cblist))
 		return false;
 
+	__trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCbPreAcc"));
+
 	/*
 	 * Callbacks are often registered with incomplete grace-period
 	 * information.  Something about the fact that getting exact
@@ -1517,6 +1519,8 @@ static bool rcu_accelerate_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
 	else
 		trace_rcu_grace_period(rcu_state.name, gp_seq_req, TPS("AccReadyCB"));
 
+	__trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCbPostAcc"));
+
 	return ret;
 }
 
@@ -2466,11 +2470,14 @@ static void rcu_do_batch(struct rcu_data *rdp)
 	rcu_segcblist_extract_done_cbs(&rdp->cblist, &rcl);
 	if (offloaded)
 		rdp->qlen_last_fqs_check = rcu_segcblist_n_cbs(&rdp->cblist);
+
+	__trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCbDequeued"));
 	rcu_nocb_unlock_irqrestore(rdp, flags);
 
 	/* Invoke callbacks. */
 	tick_dep_set_task(current, TICK_DEP_BIT_RCU);
 	rhp = rcu_cblist_dequeue(&rcl);
+
 	for (; rhp; rhp = rcu_cblist_dequeue(&rcl)) {
 		rcu_callback_t f;
 
@@ -2983,6 +2990,8 @@ __call_rcu(struct rcu_head *head, rcu_callback_t func)
 		trace_rcu_callback(rcu_state.name, head,
 				   rcu_segcblist_n_cbs(&rdp->cblist));
 
+	__trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCBQueued"));
+
 	/* Go handle any RCU core processing required. */
 	if (unlikely(rcu_segcblist_is_offloaded(&rdp->cblist))) {
 		__call_rcu_nocb_wake(rdp, was_alldone, flags); /* unlocks */
Signature · 1 line
2.29.1.341.ge80a0c044ae-goog
#19 lore raw

Re: [PATCH v9 4/7] rcu/trace: Add tracing for how segcb list changes

Quoted 16 lines · Joel Fernandes (Google)
kernel/rcu/rcu_segcblist.c +16 -0
+/*
+ * Return how many CBs each segment along with their gp_seq values.
+ *
+ * This function is O(N) where N is the number of segments. Only used from
+ * tracing code which is usually disabled in production.
+ */
+#ifdef CONFIG_RCU_TRACE
+static void rcu_segcblist_countseq(struct rcu_segcblist *rsclp,
+			 int cbcount[RCU_CBLIST_NSEGS],
+			 unsigned long gpseq[RCU_CBLIST_NSEGS])
+{
+	int i;
+
+	for (i = 0; i < RCU_CBLIST_NSEGS; i++) {
+		cbcount[i] = rcu_segcblist_get_seglen(rsclp, i);
+		gpseq[i] = rsclp->gp_seq[i];

kernel/rcu/rcu_segcblist.c void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp, · line 357

Quoted 10 lines
kernel/rcu/rcu_segcblist.c +10 -0
+	}
+}
+
+void __trace_rcu_segcb_stats(struct rcu_segcblist *rsclp, const char *context)
+{
+	int cbs[RCU_CBLIST_NSEGS];
+	unsigned long gps[RCU_CBLIST_NSEGS];
+
+	if (!trace_rcu_segcb_stats_enabled())
+		return;

Yes, very good!

Paul just told me that RCU_TRACE can be used in production so that confirms that we wanted to avoid this loop of 8 iterations when tracing is disabled.

Thanks.

maintainer
#20 lore raw

Re: [PATCH v9 4/7] rcu/trace: Add tracing for how segcb list changes

Quoted 30 lines · Frederic Weisbecker
Quoted 16 lines · Joel Fernandes (Google)
kernel/rcu/rcu_segcblist.c +16 -0
+/*
+ * Return how many CBs each segment along with their gp_seq values.
+ *
+ * This function is O(N) where N is the number of segments. Only used from
+ * tracing code which is usually disabled in production.
+ */
+#ifdef CONFIG_RCU_TRACE
+static void rcu_segcblist_countseq(struct rcu_segcblist *rsclp,
+			 int cbcount[RCU_CBLIST_NSEGS],
+			 unsigned long gpseq[RCU_CBLIST_NSEGS])
+{
+	int i;
+
+	for (i = 0; i < RCU_CBLIST_NSEGS; i++) {
+		cbcount[i] = rcu_segcblist_get_seglen(rsclp, i);
+		gpseq[i] = rsclp->gp_seq[i];

kernel/rcu/rcu_segcblist.c void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp, · line 357

Quoted 10 lines
kernel/rcu/rcu_segcblist.c +10 -0
+	}
+}
+
+void __trace_rcu_segcb_stats(struct rcu_segcblist *rsclp, const char *context)
+{
+	int cbs[RCU_CBLIST_NSEGS];
+	unsigned long gps[RCU_CBLIST_NSEGS];
+
+	if (!trace_rcu_segcb_stats_enabled())
+		return;

Yes, very good!

Quoted 2 lines

Paul just told me that RCU_TRACE can be used in production so that confirms that we wanted to avoid this loop of 8 iterations when tracing is disabled.

RCU's "don't try this in production" Kconfig option is PROVE_RCU.

I would be looking for checks that the sum of the segment lengths match the overall ->len or checks that all of the segment lengths are zero when ->cblist is empty to be guarded by something like IS_ENABLED(CONFIG_PROVE_RCU). Of course, checks of this sort need to be confined to those portions of rcu_do_batch() that are excluding other accesses to ->cblist.

But if rcu_segcblist_countseq() is invoked only when a specific trace event is enabled, it should be OK to have it guarded only by RCU_TRACE.

Thanx, Paul

#21 lore raw

Re: [PATCH v9 4/7] rcu/trace: Add tracing for how segcb list changes

Quoted 34 lines · Paul E. McKenney
Quoted 30 lines · Frederic Weisbecker
Quoted 16 lines · Joel Fernandes (Google)
kernel/rcu/rcu_segcblist.c +16 -0
+/*
+ * Return how many CBs each segment along with their gp_seq values.
+ *
+ * This function is O(N) where N is the number of segments. Only used from
+ * tracing code which is usually disabled in production.
+ */
+#ifdef CONFIG_RCU_TRACE
+static void rcu_segcblist_countseq(struct rcu_segcblist *rsclp,
+			 int cbcount[RCU_CBLIST_NSEGS],
+			 unsigned long gpseq[RCU_CBLIST_NSEGS])
+{
+	int i;
+
+	for (i = 0; i < RCU_CBLIST_NSEGS; i++) {
+		cbcount[i] = rcu_segcblist_get_seglen(rsclp, i);
+		gpseq[i] = rsclp->gp_seq[i];

kernel/rcu/rcu_segcblist.c void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp, · line 357

Quoted 10 lines
kernel/rcu/rcu_segcblist.c +10 -0
+	}
+}
+
+void __trace_rcu_segcb_stats(struct rcu_segcblist *rsclp, const char *context)
+{
+	int cbs[RCU_CBLIST_NSEGS];
+	unsigned long gps[RCU_CBLIST_NSEGS];
+
+	if (!trace_rcu_segcb_stats_enabled())
+		return;

Yes, very good!

Quoted 2 lines

Paul just told me that RCU_TRACE can be used in production so that confirms that we wanted to avoid this loop of 8 iterations when tracing is disabled.

Quoted 8 lines

RCU's "don't try this in production" Kconfig option is PROVE_RCU.

I would be looking for checks that the sum of the segment lengths match the overall ->len or checks that all of the segment lengths are zero when ->cblist is empty to be guarded by something like IS_ENABLED(CONFIG_PROVE_RCU). Of course, checks of this sort need to be confined to those portions of rcu_do_batch() that are excluding other accesses to ->cblist.

Right.

Quoted 3 lines

But if rcu_segcblist_countseq() is invoked only when a specific trace event is enabled, it should be OK to have it guarded only by RCU_TRACE.

Indeed, so I think we are good.

Thanks.

#22 lore raw

Re: [PATCH v9 4/7] rcu/trace: Add tracing for how segcb list changes

Quoted 44 lines · Frederic Weisbecker
Quoted 34 lines · Paul E. McKenney
Quoted 30 lines · Frederic Weisbecker
Quoted 16 lines · Joel Fernandes (Google)
kernel/rcu/rcu_segcblist.c +16 -0
+/*
+ * Return how many CBs each segment along with their gp_seq values.
+ *
+ * This function is O(N) where N is the number of segments. Only used from
+ * tracing code which is usually disabled in production.
+ */
+#ifdef CONFIG_RCU_TRACE
+static void rcu_segcblist_countseq(struct rcu_segcblist *rsclp,
+			 int cbcount[RCU_CBLIST_NSEGS],
+			 unsigned long gpseq[RCU_CBLIST_NSEGS])
+{
+	int i;
+
+	for (i = 0; i < RCU_CBLIST_NSEGS; i++) {
+		cbcount[i] = rcu_segcblist_get_seglen(rsclp, i);
+		gpseq[i] = rsclp->gp_seq[i];

kernel/rcu/rcu_segcblist.c void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp, · line 357

Quoted 10 lines
kernel/rcu/rcu_segcblist.c +10 -0
+	}
+}
+
+void __trace_rcu_segcb_stats(struct rcu_segcblist *rsclp, const char *context)
+{
+	int cbs[RCU_CBLIST_NSEGS];
+	unsigned long gps[RCU_CBLIST_NSEGS];
+
+	if (!trace_rcu_segcb_stats_enabled())
+		return;

Yes, very good!

Quoted 2 lines

Paul just told me that RCU_TRACE can be used in production so that confirms that we wanted to avoid this loop of 8 iterations when tracing is disabled.

Quoted 8 lines

RCU's "don't try this in production" Kconfig option is PROVE_RCU.

I would be looking for checks that the sum of the segment lengths match the overall ->len or checks that all of the segment lengths are zero when ->cblist is empty to be guarded by something like IS_ENABLED(CONFIG_PROVE_RCU). Of course, checks of this sort need to be confined to those portions of rcu_do_batch() that are excluding other accesses to ->cblist.

Quoted 7 lines

Right.

Quoted 3 lines

But if rcu_segcblist_countseq() is invoked only when a specific trace event is enabled, it should be OK to have it guarded only by RCU_TRACE.

Indeed, so I think we are good.

Thanks, so the only changes are to patches 2/7 and 4/7 which I will work on. 1/7 was already taken by Paul. For 7/7, it sounds like I did not understand Paul's reworks on the comments and we're still discussing it; but some comments are better than none, so I am Ok with Pauls version of it.

thanks,

- Joel

#23 lore raw

Re: [PATCH v9 4/7] rcu/trace: Add tracing for how segcb list changes

In reply to Joel Fernandes (Google)
Quoted 22 lines · Joel Fernandes (Google)

Track how the segcb list changes before/after acceleration, during queuing and during dequeuing.

This has proved useful to discover an optimization to avoid unwanted GP requests when there are no callbacks accelerated. The overhead is minimal as each segment's length is now stored in the respective segment.

---
  include/trace/events/rcu.h | 25 +++++++++++++++++++++++++
  kernel/rcu/rcu_segcblist.c | 34 ++++++++++++++++++++++++++++++++++
  kernel/rcu/rcu_segcblist.h |  5 +++++
  kernel/rcu/tree.c          |  9 +++++++++
  4 files changed, 73 insertions(+)
include/trace/events/rcu.h +0 -0
index 155b5cb43cfd..5f8f2ee1a936 100644

include/trace/events/rcu.h TRACE_EVENT_RCU(rcu_callback, · line 510

Quoted 6 lines
include/trace/events/rcu.h +3 -0
@@ -505,6 +505,31 @@ TRACE_EVENT_RCU(rcu_callback,
  		  __entry->qlen)
  );
 
+TRACE_EVENT_RCU(rcu_segcb_stats,
+
+		TP_PROTO(const char *ctx, int *cb_count, unsigned long *gp_seq),

I think we need to use long[] instead of int[] for cb_count everywhere in this patch?

Thanks
Neeraj

Quoted 137 lines
include/trace/events/rcu.h +22 -0
+
+		TP_ARGS(ctx, cb_count, gp_seq),
+
+		TP_STRUCT__entry(
+			__field(const char *, ctx)
+			__array(int, cb_count, RCU_CBLIST_NSEGS)
+			__array(unsigned long, gp_seq, RCU_CBLIST_NSEGS)
+		),
+
+		TP_fast_assign(
+			__entry->ctx = ctx;
+			memcpy(__entry->cb_count, cb_count, RCU_CBLIST_NSEGS * sizeof(int));
+			memcpy(__entry->gp_seq, gp_seq, RCU_CBLIST_NSEGS * sizeof(unsigned long));
+		),
+
+		TP_printk("%s cb_count: (DONE=%d, WAIT=%d, NEXT_READY=%d, NEXT=%d) "
+			  "gp_seq: (DONE=%lu, WAIT=%lu, NEXT_READY=%lu, NEXT=%lu)", __entry->ctx,
+			  __entry->cb_count[0], __entry->cb_count[1], __entry->cb_count[2], __entry->cb_count[3],
+			  __entry->gp_seq[0], __entry->gp_seq[1], __entry->gp_seq[2], __entry->gp_seq[3])
+
+);
+
  /*
   * Tracepoint for the registration of a single RCU callback of the special
   * kvfree() form.  The first argument is the RCU type, the second argument
kernel/rcu/rcu_segcblist.c +34 -0
index 357c19bbcb00..2a03949d0b82 100644
@@ -14,6 +14,7 @@
  #include <linux/types.h>
 
  #include "rcu_segcblist.h"
+#include "rcu.h"
 
  /* Initialize simple callback list. */
  void rcu_cblist_init(struct rcu_cblist *rclp)
@@ -328,6 +329,39 @@ void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp,
  	rcu_segcblist_set_seglen(rsclp, RCU_DONE_TAIL, 0);
  }
 
+/*
+ * Return how many CBs each segment along with their gp_seq values.
+ *
+ * This function is O(N) where N is the number of segments. Only used from
+ * tracing code which is usually disabled in production.
+ */
+#ifdef CONFIG_RCU_TRACE
+static void rcu_segcblist_countseq(struct rcu_segcblist *rsclp,
+			 int cbcount[RCU_CBLIST_NSEGS],
+			 unsigned long gpseq[RCU_CBLIST_NSEGS])
+{
+	int i;
+
+	for (i = 0; i < RCU_CBLIST_NSEGS; i++) {
+		cbcount[i] = rcu_segcblist_get_seglen(rsclp, i);
+		gpseq[i] = rsclp->gp_seq[i];
+	}
+}
+
+void __trace_rcu_segcb_stats(struct rcu_segcblist *rsclp, const char *context)
+{
+	int cbs[RCU_CBLIST_NSEGS];
+	unsigned long gps[RCU_CBLIST_NSEGS];
+
+	if (!trace_rcu_segcb_stats_enabled())
+		return;
+
+	rcu_segcblist_countseq(rsclp, cbs, gps);
+
+	trace_rcu_segcb_stats(context, cbs, gps);
+}
+#endif
+
  /*
   * Extract only those callbacks still pending (not yet ready to be
   * invoked) from the specified rcu_segcblist structure and place them in
kernel/rcu/rcu_segcblist.h +5 -0
index cd35c9faaf51..7750734fa116 100644
@@ -103,3 +103,8 @@ void rcu_segcblist_advance(struct rcu_segcblist *rsclp, unsigned long seq);
  bool rcu_segcblist_accelerate(struct rcu_segcblist *rsclp, unsigned long seq);
  void rcu_segcblist_merge(struct rcu_segcblist *dst_rsclp,
  			 struct rcu_segcblist *src_rsclp);
+#ifdef CONFIG_RCU_TRACE
+void __trace_rcu_segcb_stats(struct rcu_segcblist *rsclp, const char *context);
+#else
+#define __trace_rcu_segcb_stats(...)
+#endif
kernel/rcu/tree.c +9 -0
index 24c00020ab83..f6c6653b3ec2 100644
@@ -1497,6 +1497,8 @@ static bool rcu_accelerate_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
  	if (!rcu_segcblist_pend_cbs(&rdp->cblist))
  		return false;
 
+	__trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCbPreAcc"));
+
  	/*
  	 * Callbacks are often registered with incomplete grace-period
  	 * information.  Something about the fact that getting exact
@@ -1517,6 +1519,8 @@ static bool rcu_accelerate_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
  	else
  		trace_rcu_grace_period(rcu_state.name, gp_seq_req, TPS("AccReadyCB"));
 
+	__trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCbPostAcc"));
+
  	return ret;
  }
 
@@ -2466,11 +2470,14 @@ static void rcu_do_batch(struct rcu_data *rdp)
  	rcu_segcblist_extract_done_cbs(&rdp->cblist, &rcl);
  	if (offloaded)
  		rdp->qlen_last_fqs_check = rcu_segcblist_n_cbs(&rdp->cblist);
+
+	__trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCbDequeued"));
  	rcu_nocb_unlock_irqrestore(rdp, flags);
 
  	/* Invoke callbacks. */
  	tick_dep_set_task(current, TICK_DEP_BIT_RCU);
  	rhp = rcu_cblist_dequeue(&rcl);
+
  	for (; rhp; rhp = rcu_cblist_dequeue(&rcl)) {
  		rcu_callback_t f;
 
@@ -2983,6 +2990,8 @@ __call_rcu(struct rcu_head *head, rcu_callback_t func)
  		trace_rcu_callback(rcu_state.name, head,
  				   rcu_segcblist_n_cbs(&rdp->cblist));
 
+	__trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCBQueued"));
+
  	/* Go handle any RCU core processing required. */
  	if (unlikely(rcu_segcblist_is_offloaded(&rdp->cblist))) {
  		__call_rcu_nocb_wake(rdp, was_alldone, flags); /* unlocks */
Signature · 1 line
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation
#24 lore raw

Re: [PATCH v9 4/7] rcu/trace: Add tracing for how segcb list changes

Quoted 20 lines · Neeraj Upadhyay
Quoted 17 lines · Joel Fernandes (Google)

Track how the segcb list changes before/after acceleration, during queuing and during dequeuing.

This has proved useful to discover an optimization to avoid unwanted GP requests when there are no callbacks accelerated. The overhead is minimal as each segment's length is now stored in the respective segment.

---
  include/trace/events/rcu.h | 25 +++++++++++++++++++++++++
  kernel/rcu/rcu_segcblist.c | 34 ++++++++++++++++++++++++++++++++++
  kernel/rcu/rcu_segcblist.h |  5 +++++
  kernel/rcu/tree.c          |  9 +++++++++
  4 files changed, 73 insertions(+)
Quoted 13 lines

include/trace/events/rcu.h TRACE_EVENT_RCU(rcu_callback, · line 509

Quoted 10 lines
include/trace/events/rcu.h +3 -0
index 155b5cb43cfd..5f8f2ee1a936 100644
@@ -505,6 +505,31 @@ TRACE_EVENT_RCU(rcu_callback,
  		  __entry->qlen)
  );
+TRACE_EVENT_RCU(rcu_segcb_stats,
+
+		TP_PROTO(const char *ctx, int *cb_count, unsigned long *gp_seq),

I think we need to use long[] instead of int[] for cb_count everywhere in this patch?

More than 4 billion callbacks on a single cblist sounds like a bug though so int should work. Plus I prefer to keep the tracepoint size small (on 64-bit systems, long is 64 bits, int is 32 bits).

Thanks for all the reviews on this and other patches!

- Joel

Quoted 137 lines

Thanks
Neeraj

Quoted 128 lines
include/trace/events/rcu.h +22 -0
+
+		TP_ARGS(ctx, cb_count, gp_seq),
+
+		TP_STRUCT__entry(
+			__field(const char *, ctx)
+			__array(int, cb_count, RCU_CBLIST_NSEGS)
+			__array(unsigned long, gp_seq, RCU_CBLIST_NSEGS)
+		),
+
+		TP_fast_assign(
+			__entry->ctx = ctx;
+			memcpy(__entry->cb_count, cb_count, RCU_CBLIST_NSEGS * sizeof(int));
+			memcpy(__entry->gp_seq, gp_seq, RCU_CBLIST_NSEGS * sizeof(unsigned long));
+		),
+
+		TP_printk("%s cb_count: (DONE=%d, WAIT=%d, NEXT_READY=%d, NEXT=%d) "
+			  "gp_seq: (DONE=%lu, WAIT=%lu, NEXT_READY=%lu, NEXT=%lu)", __entry->ctx,
+			  __entry->cb_count[0], __entry->cb_count[1], __entry->cb_count[2], __entry->cb_count[3],
+			  __entry->gp_seq[0], __entry->gp_seq[1], __entry->gp_seq[2], __entry->gp_seq[3])
+
+);
+
  /*
   * Tracepoint for the registration of a single RCU callback of the special
   * kvfree() form.  The first argument is the RCU type, the second argument
kernel/rcu/rcu_segcblist.c +34 -0
index 357c19bbcb00..2a03949d0b82 100644
@@ -14,6 +14,7 @@
  #include <linux/types.h>
  #include "rcu_segcblist.h"
+#include "rcu.h"
  /* Initialize simple callback list. */
  void rcu_cblist_init(struct rcu_cblist *rclp)
@@ -328,6 +329,39 @@ void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp,
  	rcu_segcblist_set_seglen(rsclp, RCU_DONE_TAIL, 0);
  }
+/*
+ * Return how many CBs each segment along with their gp_seq values.
+ *
+ * This function is O(N) where N is the number of segments. Only used from
+ * tracing code which is usually disabled in production.
+ */
+#ifdef CONFIG_RCU_TRACE
+static void rcu_segcblist_countseq(struct rcu_segcblist *rsclp,
+			 int cbcount[RCU_CBLIST_NSEGS],
+			 unsigned long gpseq[RCU_CBLIST_NSEGS])
+{
+	int i;
+
+	for (i = 0; i < RCU_CBLIST_NSEGS; i++) {
+		cbcount[i] = rcu_segcblist_get_seglen(rsclp, i);
+		gpseq[i] = rsclp->gp_seq[i];
+	}
+}
+
+void __trace_rcu_segcb_stats(struct rcu_segcblist *rsclp, const char *context)
+{
+	int cbs[RCU_CBLIST_NSEGS];
+	unsigned long gps[RCU_CBLIST_NSEGS];
+
+	if (!trace_rcu_segcb_stats_enabled())
+		return;
+
+	rcu_segcblist_countseq(rsclp, cbs, gps);
+
+	trace_rcu_segcb_stats(context, cbs, gps);
+}
+#endif
+
  /*
   * Extract only those callbacks still pending (not yet ready to be
   * invoked) from the specified rcu_segcblist structure and place them in
kernel/rcu/rcu_segcblist.h +5 -0
index cd35c9faaf51..7750734fa116 100644
@@ -103,3 +103,8 @@ void rcu_segcblist_advance(struct rcu_segcblist *rsclp, unsigned long seq);
  bool rcu_segcblist_accelerate(struct rcu_segcblist *rsclp, unsigned long seq);
  void rcu_segcblist_merge(struct rcu_segcblist *dst_rsclp,
  			 struct rcu_segcblist *src_rsclp);
+#ifdef CONFIG_RCU_TRACE
+void __trace_rcu_segcb_stats(struct rcu_segcblist *rsclp, const char *context);
+#else
+#define __trace_rcu_segcb_stats(...)
+#endif
kernel/rcu/tree.c +9 -0
index 24c00020ab83..f6c6653b3ec2 100644
@@ -1497,6 +1497,8 @@ static bool rcu_accelerate_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
  	if (!rcu_segcblist_pend_cbs(&rdp->cblist))
  		return false;
+	__trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCbPreAcc"));
+
  	/*
  	 * Callbacks are often registered with incomplete grace-period
  	 * information.  Something about the fact that getting exact
@@ -1517,6 +1519,8 @@ static bool rcu_accelerate_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
  	else
  		trace_rcu_grace_period(rcu_state.name, gp_seq_req, TPS("AccReadyCB"));
+	__trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCbPostAcc"));
+
  	return ret;
  }
@@ -2466,11 +2470,14 @@ static void rcu_do_batch(struct rcu_data *rdp)
  	rcu_segcblist_extract_done_cbs(&rdp->cblist, &rcl);
  	if (offloaded)
  		rdp->qlen_last_fqs_check = rcu_segcblist_n_cbs(&rdp->cblist);
+
+	__trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCbDequeued"));
  	rcu_nocb_unlock_irqrestore(rdp, flags);
  	/* Invoke callbacks. */
  	tick_dep_set_task(current, TICK_DEP_BIT_RCU);
  	rhp = rcu_cblist_dequeue(&rcl);
+
  	for (; rhp; rhp = rcu_cblist_dequeue(&rcl)) {
  		rcu_callback_t f;
@@ -2983,6 +2990,8 @@ __call_rcu(struct rcu_head *head, rcu_callback_t func)
  		trace_rcu_callback(rcu_state.name, head,
  				   rcu_segcblist_n_cbs(&rdp->cblist));
+	__trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCBQueued"));
+
  	/* Go handle any RCU core processing required. */
  	if (unlikely(rcu_segcblist_is_offloaded(&rdp->cblist))) {
  		__call_rcu_nocb_wake(rdp, was_alldone, flags); /* unlocks */
 
Signature · 2 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of
the Code Aurora Forum, hosted by The Linux Foundation
maintainer
#25 lore raw

Re: [PATCH v9 4/7] rcu/trace: Add tracing for how segcb list changes

In reply to Joel Fernandes (Google)
Quoted 10 lines · Joel Fernandes (Google)

Track how the segcb list changes before/after acceleration, during queuing and during dequeuing.

This has proved useful to discover an optimization to avoid unwanted GP requests when there are no callbacks accelerated. The overhead is minimal as each segment's length is now stored in the respective segment.

I thought I had responded to this one, but I find no record of having done so. But please see below for some questions and comments.

Quoted 21 lines
---
 include/trace/events/rcu.h | 25 +++++++++++++++++++++++++
 kernel/rcu/rcu_segcblist.c | 34 ++++++++++++++++++++++++++++++++++
 kernel/rcu/rcu_segcblist.h |  5 +++++
 kernel/rcu/tree.c          |  9 +++++++++
 4 files changed, 73 insertions(+)
include/trace/events/rcu.h +6 -0
index 155b5cb43cfd..5f8f2ee1a936 100644
@@ -505,6 +505,31 @@ TRACE_EVENT_RCU(rcu_callback,
 		  __entry->qlen)
 );
 
+TRACE_EVENT_RCU(rcu_segcb_stats,
+
+		TP_PROTO(const char *ctx, int *cb_count, unsigned long *gp_seq),
+
+		TP_ARGS(ctx, cb_count, gp_seq),
+

include/trace/events/rcu.h TRACE_EVENT_RCU(rcu_callback, · line 523

Quoted 10 lines
include/trace/events/rcu.h +10 -0
+		TP_STRUCT__entry(
+			__field(const char *, ctx)
+			__array(int, cb_count, RCU_CBLIST_NSEGS)
+			__array(unsigned long, gp_seq, RCU_CBLIST_NSEGS)
+		),
+
+		TP_fast_assign(
+			__entry->ctx = ctx;
+			memcpy(__entry->cb_count, cb_count, RCU_CBLIST_NSEGS * sizeof(int));
+			memcpy(__entry->gp_seq, gp_seq, RCU_CBLIST_NSEGS * sizeof(unsigned long));

OK, I will bite...

Why not include linux/rcu_segcblist.h and then fold all of rcu_segcblist_countseq() and much of __trace_rcu_segcb_stats() into this TP_fast_assign()? If you must have "unsigned int" for cb_count, then you can use "do {} while (0)" to declare the index variable, but if you can use unsigned long as suggested by Neeraj, you can just use a pair of memcpy()s.

Quoted 44 lines
include/trace/events/rcu.h +9 -0
+		),
+
+		TP_printk("%s cb_count: (DONE=%d, WAIT=%d, NEXT_READY=%d, NEXT=%d) "
+			  "gp_seq: (DONE=%lu, WAIT=%lu, NEXT_READY=%lu, NEXT=%lu)", __entry->ctx,
+			  __entry->cb_count[0], __entry->cb_count[1], __entry->cb_count[2], __entry->cb_count[3],
+			  __entry->gp_seq[0], __entry->gp_seq[1], __entry->gp_seq[2], __entry->gp_seq[3])
+
+);
+
 /*
  * Tracepoint for the registration of a single RCU callback of the special
  * kvfree() form.  The first argument is the RCU type, the second argument
kernel/rcu/rcu_segcblist.c +17 -0
index 357c19bbcb00..2a03949d0b82 100644
@@ -14,6 +14,7 @@
 #include <linux/types.h>
 
 #include "rcu_segcblist.h"
+#include "rcu.h"
 
 /* Initialize simple callback list. */
 void rcu_cblist_init(struct rcu_cblist *rclp)
@@ -328,6 +329,39 @@ void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp,
 	rcu_segcblist_set_seglen(rsclp, RCU_DONE_TAIL, 0);
 }
 
+/*
+ * Return how many CBs each segment along with their gp_seq values.
+ *
+ * This function is O(N) where N is the number of segments. Only used from
+ * tracing code which is usually disabled in production.
+ */
+#ifdef CONFIG_RCU_TRACE
+static void rcu_segcblist_countseq(struct rcu_segcblist *rsclp,
+			 int cbcount[RCU_CBLIST_NSEGS],
+			 unsigned long gpseq[RCU_CBLIST_NSEGS])
+{
+	int i;
+
+	for (i = 0; i < RCU_CBLIST_NSEGS; i++) {
+		cbcount[i] = rcu_segcblist_get_seglen(rsclp, i);
+		gpseq[i] = rsclp->gp_seq[i];

kernel/rcu/rcu_segcblist.c void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp, · line 357

Quoted 10 lines
kernel/rcu/rcu_segcblist.c +10 -0
+	}
+}
+
+void __trace_rcu_segcb_stats(struct rcu_segcblist *rsclp, const char *context)
+{
+	int cbs[RCU_CBLIST_NSEGS];
+	unsigned long gps[RCU_CBLIST_NSEGS];
+
+	if (!trace_rcu_segcb_stats_enabled())
+		return;

Can't you rely on the trace system to enable and disable this trace event? If the thought is to save instructions, then moving all this into TP_fast_assign() enables the trace system to deal with that as well.

Quoted 14 lines
kernel/rcu/rcu_segcblist.c +6 -0
+	rcu_segcblist_countseq(rsclp, cbs, gps);
+
+	trace_rcu_segcb_stats(context, cbs, gps);
+}
+#endif
+
 /*
  * Extract only those callbacks still pending (not yet ready to be
  * invoked) from the specified rcu_segcblist structure and place them in
kernel/rcu/rcu_segcblist.h +0 -0
index cd35c9faaf51..7750734fa116 100644

kernel/rcu/rcu_segcblist.h void rcu_segcblist_advance(struct rcu_segcblist *rsclp, unsigned long seq); · line 110

Quoted 8 lines
kernel/rcu/rcu_segcblist.h +5 -0
@@ -103,3 +103,8 @@ void rcu_segcblist_advance(struct rcu_segcblist *rsclp, unsigned long seq);
 bool rcu_segcblist_accelerate(struct rcu_segcblist *rsclp, unsigned long seq);
 void rcu_segcblist_merge(struct rcu_segcblist *dst_rsclp,
 			 struct rcu_segcblist *src_rsclp);
+#ifdef CONFIG_RCU_TRACE
+void __trace_rcu_segcb_stats(struct rcu_segcblist *rsclp, const char *context);
+#else
+#define __trace_rcu_segcb_stats(...)
+#endif

And letting TP_fast_assign() to the work allows this to go away...

kernel/rcu/tree.c static bool rcu_accelerate_cbs(struct rcu_node *rnp, struct rcu_data *rdp) · line 1500

Quoted 9 lines
kernel/rcu/tree.c +1 -0
index 24c00020ab83..f6c6653b3ec2 100644
@@ -1497,6 +1497,8 @@ static bool rcu_accelerate_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
 	if (!rcu_segcblist_pend_cbs(&rdp->cblist))
 		return false;
 
+	__trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCbPreAcc"));

...and you don't need the "__" everywhere.

So what am I missing here?

Thanx, Paul

Quoted 40 lines
kernel/rcu/tree.c +8 -0
+
 	/*
 	 * Callbacks are often registered with incomplete grace-period
 	 * information.  Something about the fact that getting exact
@@ -1517,6 +1519,8 @@ static bool rcu_accelerate_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
 	else
 		trace_rcu_grace_period(rcu_state.name, gp_seq_req, TPS("AccReadyCB"));
 
+	__trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCbPostAcc"));
+
 	return ret;
 }
 
@@ -2466,11 +2470,14 @@ static void rcu_do_batch(struct rcu_data *rdp)
 	rcu_segcblist_extract_done_cbs(&rdp->cblist, &rcl);
 	if (offloaded)
 		rdp->qlen_last_fqs_check = rcu_segcblist_n_cbs(&rdp->cblist);
+
+	__trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCbDequeued"));
 	rcu_nocb_unlock_irqrestore(rdp, flags);
 
 	/* Invoke callbacks. */
 	tick_dep_set_task(current, TICK_DEP_BIT_RCU);
 	rhp = rcu_cblist_dequeue(&rcl);
+
 	for (; rhp; rhp = rcu_cblist_dequeue(&rcl)) {
 		rcu_callback_t f;
 
@@ -2983,6 +2990,8 @@ __call_rcu(struct rcu_head *head, rcu_callback_t func)
 		trace_rcu_callback(rcu_state.name, head,
 				   rcu_segcblist_n_cbs(&rdp->cblist));
 
+	__trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCBQueued"));
+
 	/* Go handle any RCU core processing required. */
 	if (unlikely(rcu_segcblist_is_offloaded(&rdp->cblist))) {
 		__call_rcu_nocb_wake(rdp, was_alldone, flags); /* unlocks */
Signature · 1 line
2.29.1.341.ge80a0c044ae-goog
maintainer
#26 lore raw

Re: [PATCH v9 4/7] rcu/trace: Add tracing for how segcb list changes

Quoted 12 lines · On Tue, 10 Nov 2020 16:35:30 -0800 "Paul E. McKenney" <[email protected]>…

kernel/rcu/rcu_segcblist.c void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp, · line 357

Quoted 7 lines
kernel/rcu/rcu_segcblist.c +7 -0
+void __trace_rcu_segcb_stats(struct rcu_segcblist *rsclp, const char *context)
+{
+	int cbs[RCU_CBLIST_NSEGS];
+	unsigned long gps[RCU_CBLIST_NSEGS];
+
+	if (!trace_rcu_segcb_stats_enabled())
+		return;  

Can't you rely on the trace system to enable and disable this trace event? If the thought is to save instructions, then moving all this into TP_fast_assign() enables the trace system to deal with that as well.

Quoted 5 lines

kernel/rcu/rcu_segcblist.c void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp, · line 363

Quoted 5 lines
kernel/rcu/rcu_segcblist.c +5 -0
+	rcu_segcblist_countseq(rsclp, cbs, gps);
+
+	trace_rcu_segcb_stats(context, cbs, gps);
+}
+#endif

Yeah, I agree with Paul. I think it is possible to move this all into the TP_fast_assign. If you have trouble doing so, let me know.

-- Steve

#27 lore raw

Re: [PATCH v9 4/7] rcu/trace: Add tracing for how segcb list changes

Quoted 14 lines · Steven Rostedt
Quoted 11 lines · On Tue, 10 Nov 2020 16:35:30 -0800 "Paul E. McKenney" <[email protected]>…

kernel/rcu/rcu_segcblist.c void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp, · line 357

Quoted 7 lines
kernel/rcu/rcu_segcblist.c +7 -0
+void __trace_rcu_segcb_stats(struct rcu_segcblist *rsclp, const char *context)
+{
+	int cbs[RCU_CBLIST_NSEGS];
+	unsigned long gps[RCU_CBLIST_NSEGS];
+
+	if (!trace_rcu_segcb_stats_enabled())
+		return;  

Can't you rely on the trace system to enable and disable this trace event? If the thought is to save instructions, then moving all this into TP_fast_assign() enables the trace system to deal with that as well.

Makes sense.

Quoted 8 lines
Quoted 5 lines

kernel/rcu/rcu_segcblist.c void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp, · line 363

Quoted 5 lines
kernel/rcu/rcu_segcblist.c +5 -0
+	rcu_segcblist_countseq(rsclp, cbs, gps);
+
+	trace_rcu_segcb_stats(context, cbs, gps);
+}
+#endif

Yeah, I agree with Paul. I think it is possible to move this all into the TP_fast_assign. If you have trouble doing so, let me know.

Sure. Last time I tried this for this patch, I ran into some issue. I will try again and let you know if I need help.

thanks,

- Joel

Quoted 2 lines

-- Steve

#28 lore raw

PATCH v9 5/7 rcu/segcblist: Remove useless rcupdate.h include

In reply to Joel Fernandes (Google)
---
 kernel/rcu/rcu_segcblist.c | 1 -
 1 file changed, 1 deletion(-)
kernel/rcu/rcu_segcblist.c +0 -1
index 2a03949d0b82..e9e72d72f7a6 100644
@@ -10,7 +10,6 @@
 #include <linux/cpu.h>
 #include <linux/interrupt.h>
 #include <linux/kernel.h>
-#include <linux/rcupdate.h>
 #include <linux/types.h>
 
 #include "rcu_segcblist.h"
Signature · 1 line
2.29.1.341.ge80a0c044ae-goog
maintainer
#29 lore raw

Re: [PATCH v9 5/7] rcu/segcblist: Remove useless rcupdate.h include

Quoted 1 line · Joel Fernandes (Google)

This one looks fine, but depends on the earlier "rcu/segcblist: Add counters to segcblist datastructure" patch, which also changes the list of #include directives for this file. It manages to avoid conflicting with "rcu/trace: Add tracing for how segcb list changes", despite this one also changing the #include directives.

I am testing it just out of curiosity, but it might make more sense to fold this one into "rcu/segcblist: Add counters to segcblist datastructure".

Thanx, Paul

Quoted 19 lines
---
 kernel/rcu/rcu_segcblist.c | 1 -
 1 file changed, 1 deletion(-)
kernel/rcu/rcu_segcblist.c +0 -1
index 2a03949d0b82..e9e72d72f7a6 100644
@@ -10,7 +10,6 @@
 #include <linux/cpu.h>
 #include <linux/interrupt.h>
 #include <linux/kernel.h>
-#include <linux/rcupdate.h>
 #include <linux/types.h>
 
 #include "rcu_segcblist.h"
Signature · 1 line
2.29.1.341.ge80a0c044ae-goog
maintainer
#30 lore raw

Re: [PATCH v9 5/7] rcu/segcblist: Remove useless rcupdate.h include

Quoted 9 lines · Paul E. McKenney
Quoted 1 line · Joel Fernandes (Google)

This one looks fine, but depends on the earlier "rcu/segcblist: Add counters to segcblist datastructure" patch, which also changes the list of #include directives for this file. It manages to avoid conflicting with "rcu/trace: Add tracing for how segcb list changes", despite this one also changing the #include directives.

Quoted 3 lines

I am testing it just out of curiosity, but it might make more sense to fold this one into "rcu/segcblist: Add counters to segcblist datastructure".

And it does pass light rcutorture. ;-)

Thanx, Paul

Quoted 19 lines
Quoted 19 lines
---
 kernel/rcu/rcu_segcblist.c | 1 -
 1 file changed, 1 deletion(-)
kernel/rcu/rcu_segcblist.c +0 -1
index 2a03949d0b82..e9e72d72f7a6 100644
@@ -10,7 +10,6 @@
 #include <linux/cpu.h>
 #include <linux/interrupt.h>
 #include <linux/kernel.h>
-#include <linux/rcupdate.h>
 #include <linux/types.h>
 
 #include "rcu_segcblist.h"
Signature · 1 line
2.29.1.341.ge80a0c044ae-goog
#31 lore raw

Re: [PATCH v9 5/7] rcu/segcblist: Remove useless rcupdate.h include

Quoted 10 lines · Paul E. McKenney
Quoted 9 lines · Paul E. McKenney
Quoted 1 line · Joel Fernandes (Google)

This one looks fine, but depends on the earlier "rcu/segcblist: Add counters to segcblist datastructure" patch, which also changes the list of #include directives for this file. It manages to avoid conflicting with "rcu/trace: Add tracing for how segcb list changes", despite this one also changing the #include directives.

Quoted 5 lines
Quoted 3 lines

I am testing it just out of curiosity, but it might make more sense to fold this one into "rcu/segcblist: Add counters to segcblist datastructure".

And it does pass light rcutorture. ;-)

Cool, I squashed it into 2/7 and updated my tree.

thanks,

- Joel

Quoted 21 lines

Thanx, Paul

Quoted 19 lines
Quoted 19 lines
---
 kernel/rcu/rcu_segcblist.c | 1 -
 1 file changed, 1 deletion(-)
kernel/rcu/rcu_segcblist.c +0 -1
index 2a03949d0b82..e9e72d72f7a6 100644
@@ -10,7 +10,6 @@
 #include <linux/cpu.h>
 #include <linux/interrupt.h>
 #include <linux/kernel.h>
-#include <linux/rcupdate.h>
 #include <linux/types.h>
 
 #include "rcu_segcblist.h"
Signature · 1 line
2.29.1.341.ge80a0c044ae-goog
#32 lore raw

PATCH v9 6/7 rcu/tree: segcblist: Remove redundant smp_mb()s

In reply to Joel Fernandes (Google)

This memory barrier is not needed as rcu_segcblist_add_len() already includes a memory barrier *before* and *after* the length of the list is updated.

Same reasoning for rcu_segcblist_enqueue().

---
 kernel/rcu/rcu_segcblist.c | 1 -
 kernel/rcu/tree.c          | 1 -
 2 files changed, 2 deletions(-)
kernel/rcu/rcu_segcblist.c +0 -1
index e9e72d72f7a6..d96272e8d604 100644
@@ -268,7 +268,6 @@ void rcu_segcblist_enqueue(struct rcu_segcblist *rsclp,
 			   struct rcu_head *rhp)
 {
 	rcu_segcblist_inc_len(rsclp);
-	smp_mb(); /* Ensure counts are updated before callback is enqueued. */
 	rcu_segcblist_inc_seglen(rsclp, RCU_NEXT_TAIL);
 	rhp->next = NULL;
 	WRITE_ONCE(*rsclp->tails[RCU_NEXT_TAIL], rhp);
kernel/rcu/tree.c +0 -1
index f6c6653b3ec2..fb2a5ac4a59c 100644
@@ -2525,7 +2525,6 @@ static void rcu_do_batch(struct rcu_data *rdp)
 
 	/* Update counts and requeue any remaining callbacks. */
 	rcu_segcblist_insert_done_cbs(&rdp->cblist, &rcl);
-	smp_mb(); /* List handling before counting for rcu_barrier(). */
 	rcu_segcblist_add_len(&rdp->cblist, -count);
 
 	/* Reinstate batch limit if we have worked down the excess. */
Signature · 1 line
2.29.1.341.ge80a0c044ae-goog
maintainer
#33 lore raw

Re: [PATCH v9 6/7] rcu/tree: segcblist: Remove redundant smp_mb()s

Quoted 5 lines · Joel Fernandes (Google)

This memory barrier is not needed as rcu_segcblist_add_len() already includes a memory barrier *before* and *after* the length of the list is updated.

Same reasoning for rcu_segcblist_enqueue().

I suggest a commit log like the following:

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

The full memory barriers in rcu_segcblist_enqueue() and in rcu_do_batch() are not needed because rcu_segcblist_add_len(), and thus also rcu_segcblist_inc_len(), already includes a memory barrier *before* and *after* the length of the list is updated.

This commit therefore removes these redundant smp_mb() invocations.

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

Other than that, looks good! I could hand-apply it, but that would just cause more churn with the addition of the call to rcu_segcblist_inc_seglen(). So could you please update the commit log when you repost, whether to the mailing list or from your git tree?

Thanx, Paul

Quoted 34 lines
---
 kernel/rcu/rcu_segcblist.c | 1 -
 kernel/rcu/tree.c          | 1 -
 2 files changed, 2 deletions(-)
kernel/rcu/rcu_segcblist.c +0 -1
index e9e72d72f7a6..d96272e8d604 100644
@@ -268,7 +268,6 @@ void rcu_segcblist_enqueue(struct rcu_segcblist *rsclp,
 			   struct rcu_head *rhp)
 {
 	rcu_segcblist_inc_len(rsclp);
-	smp_mb(); /* Ensure counts are updated before callback is enqueued. */
 	rcu_segcblist_inc_seglen(rsclp, RCU_NEXT_TAIL);
 	rhp->next = NULL;
 	WRITE_ONCE(*rsclp->tails[RCU_NEXT_TAIL], rhp);
kernel/rcu/tree.c +0 -1
index f6c6653b3ec2..fb2a5ac4a59c 100644
@@ -2525,7 +2525,6 @@ static void rcu_do_batch(struct rcu_data *rdp)
 
 	/* Update counts and requeue any remaining callbacks. */
 	rcu_segcblist_insert_done_cbs(&rdp->cblist, &rcl);
-	smp_mb(); /* List handling before counting for rcu_barrier(). */
 	rcu_segcblist_add_len(&rdp->cblist, -count);
 
 	/* Reinstate batch limit if we have worked down the excess. */
Signature · 1 line
2.29.1.341.ge80a0c044ae-goog
#34 lore raw

Re: [PATCH v9 6/7] rcu/tree: segcblist: Remove redundant smp_mb()s

Quoted 16 lines · Paul E. McKenney
Quoted 5 lines · Joel Fernandes (Google)

This memory barrier is not needed as rcu_segcblist_add_len() already includes a memory barrier *before* and *after* the length of the list is updated.

Same reasoning for rcu_segcblist_enqueue().

I suggest a commit log like the following:

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

The full memory barriers in rcu_segcblist_enqueue() and in rcu_do_batch() are not needed because rcu_segcblist_add_len(), and thus also rcu_segcblist_inc_len(), already includes a memory barrier *before* and *after* the length of the list is updated.

Quoted 8 lines

This commit therefore removes these redundant smp_mb() invocations.

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

Other than that, looks good! I could hand-apply it, but that would just cause more churn with the addition of the call to rcu_segcblist_inc_seglen(). So could you please update the commit log when you repost, whether to the mailing list or from your git tree?

Done, I updated it in my tree. I will share the link to tree on IRC.

thanks,

- Joel

Quoted 37 lines

Thanx, Paul

Quoted 34 lines
---
 kernel/rcu/rcu_segcblist.c | 1 -
 kernel/rcu/tree.c          | 1 -
 2 files changed, 2 deletions(-)
kernel/rcu/rcu_segcblist.c +0 -1
index e9e72d72f7a6..d96272e8d604 100644
@@ -268,7 +268,6 @@ void rcu_segcblist_enqueue(struct rcu_segcblist *rsclp,
 			   struct rcu_head *rhp)
 {
 	rcu_segcblist_inc_len(rsclp);
-	smp_mb(); /* Ensure counts are updated before callback is enqueued. */
 	rcu_segcblist_inc_seglen(rsclp, RCU_NEXT_TAIL);
 	rhp->next = NULL;
 	WRITE_ONCE(*rsclp->tails[RCU_NEXT_TAIL], rhp);
kernel/rcu/tree.c +0 -1
index f6c6653b3ec2..fb2a5ac4a59c 100644
@@ -2525,7 +2525,6 @@ static void rcu_do_batch(struct rcu_data *rdp)
 
 	/* Update counts and requeue any remaining callbacks. */
 	rcu_segcblist_insert_done_cbs(&rdp->cblist, &rcl);
-	smp_mb(); /* List handling before counting for rcu_barrier(). */
 	rcu_segcblist_add_len(&rdp->cblist, -count);
 
 	/* Reinstate batch limit if we have worked down the excess. */
Signature · 1 line
2.29.1.341.ge80a0c044ae-goog
maintainer
#35 lore raw

Re: [PATCH v9 6/7] rcu/tree: segcblist: Remove redundant smp_mb()s

Quoted 19 lines · Joel Fernandes
Quoted 18 lines · Paul E. McKenney
Quoted 5 lines · Joel Fernandes (Google)

This memory barrier is not needed as rcu_segcblist_add_len() already includes a memory barrier *before* and *after* the length of the list is updated.

Same reasoning for rcu_segcblist_enqueue().

I suggest a commit log like the following:

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

The full memory barriers in rcu_segcblist_enqueue() and in rcu_do_batch() are not needed because rcu_segcblist_add_len(), and thus also rcu_segcblist_inc_len(), already includes a memory barrier *before* and *after* the length of the list is updated.

This commit therefore removes these redundant smp_mb() invocations.

Quoted 8 lines
Quoted 6 lines

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

Other than that, looks good! I could hand-apply it, but that would just cause more churn with the addition of the call to rcu_segcblist_inc_seglen(). So could you please update the commit log when you repost, whether to the mailing list or from your git tree?

Done, I updated it in my tree. I will share the link to tree on IRC.

And I have pulled this in, thank you!

Thanx, Paul

Quoted 41 lines

thanks,

- Joel

Quoted 37 lines

Thanx, Paul

Quoted 34 lines
---
 kernel/rcu/rcu_segcblist.c | 1 -
 kernel/rcu/tree.c          | 1 -
 2 files changed, 2 deletions(-)
kernel/rcu/rcu_segcblist.c +0 -1
index e9e72d72f7a6..d96272e8d604 100644
@@ -268,7 +268,6 @@ void rcu_segcblist_enqueue(struct rcu_segcblist *rsclp,
 			   struct rcu_head *rhp)
 {
 	rcu_segcblist_inc_len(rsclp);
-	smp_mb(); /* Ensure counts are updated before callback is enqueued. */
 	rcu_segcblist_inc_seglen(rsclp, RCU_NEXT_TAIL);
 	rhp->next = NULL;
 	WRITE_ONCE(*rsclp->tails[RCU_NEXT_TAIL], rhp);
kernel/rcu/tree.c +0 -1
index f6c6653b3ec2..fb2a5ac4a59c 100644
@@ -2525,7 +2525,6 @@ static void rcu_do_batch(struct rcu_data *rdp)
 
 	/* Update counts and requeue any remaining callbacks. */
 	rcu_segcblist_insert_done_cbs(&rdp->cblist, &rcl);
-	smp_mb(); /* List handling before counting for rcu_barrier(). */
 	rcu_segcblist_add_len(&rdp->cblist, -count);
 
 	/* Reinstate batch limit if we have worked down the excess. */
Signature · 1 line
2.29.1.341.ge80a0c044ae-goog
#36 lore raw

PATCH v9 7/7 rcu/segcblist: Add additional comments to explain smp_mb()

In reply to Joel Fernandes (Google)

Memory barriers are needed when updating the full length of the segcblist, however it is not fully clearly why one is needed before and after. This patch therefore adds additional comments to the function header to explain it.

---
 kernel/rcu/rcu_segcblist.c | 40 ++++++++++++++++++++++++++++++++++----
 1 file changed, 36 insertions(+), 4 deletions(-)
kernel/rcu/rcu_segcblist.c +36 -4
index d96272e8d604..9b43d686b1f3 100644
@@ -135,17 +135,49 @@ static void rcu_segcblist_inc_seglen(struct rcu_segcblist *rsclp, int seg)
  * field to disagree with the actual number of callbacks on the structure.
  * This increase is fully ordered with respect to the callers accesses
  * both before and after.
+ *
+ * About memory barriers:
+ * There is a situation where rcu_barrier() locklessly samples the full
+ * length of the segmented cblist before deciding what to do. That can
+ * race with another path that calls this function such as enqueue or dequeue.
+ * rcu_barrier() should not wrongly assume there are no callbacks, so any
+ * transitions from 1->0 and 0->1 have to be carefully ordered with respect to
+ * list modifications and with whatever follows the rcu_barrier().
+ *
+ * There are at least 2 cases:
+ * CASE 1: Memory barrier is needed before adding to length, for the case where
+ * v is negative (happens during dequeue). When length transitions from 1 -> 0,
+ * the write to 0 has to be ordered to appear to be *after* the memory accesses
+ * of the CBs that were dequeued and the segcblist modifications:
+ * To illustrate the problematic scenario to avoid:
+ * P0 (what P1 sees)	P1
+ * set len = 0
+ *                      rcu_barrier sees len as 0
+ * dequeue from list
+ *                      rcu_barrier does nothing.
+ *
+ * CASE 2: Memory barrier is needed after adding to length for the case
+ * where length transitions from 0 -> 1. This is because rcu_barrier()
+ * should never miss an update to the length. So the update to length
+ * has to be seen *before* any modifications to the segmented list. Otherwise a
+ * race can happen.
+ * To illustrate the problematic scenario to avoid:
+ * P0 (what P1 sees)	P1
+ * queue to list
+ *                      rcu_barrier sees len as 0
+ * set len = 1.
+ *                      rcu_barrier does nothing.
  */
 void rcu_segcblist_add_len(struct rcu_segcblist *rsclp, long v)
 {
 #ifdef CONFIG_RCU_NOCB_CPU
-	smp_mb__before_atomic(); /* Up to the caller! */
+	smp_mb__before_atomic(); /* Read function's comments */
 	atomic_long_add(v, &rsclp->len);
-	smp_mb__after_atomic(); /* Up to the caller! */
+	smp_mb__after_atomic();  /* Read function's comments */
 #else
-	smp_mb(); /* Up to the caller! */
+	smp_mb(); /* Read function's comments */
 	WRITE_ONCE(rsclp->len, rsclp->len + v);
-	smp_mb(); /* Up to the caller! */
+	smp_mb(); /* Read function's comments */
 #endif
 }
 
Signature · 1 line
2.29.1.341.ge80a0c044ae-goog
maintainer
#37 lore raw

Re: [PATCH v9 7/7] rcu/segcblist: Add additional comments to explain smp_mb()

Quoted 6 lines · Joel Fernandes (Google)

Memory barriers are needed when updating the full length of the segcblist, however it is not fully clearly why one is needed before and after. This patch therefore adds additional comments to the function header to explain it.

Looks good, thank you! As always, I could not resist the urge to do a bit of wordsmithing, so that the queued commit is as shown below. Please let me know if I messed anything up.

Thanx, Paul

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

commit 7dac7adefcae7558b3a85a16f51186d621623733

Author: Joel Fernandes (Google) <[email protected]>
Date:   Tue Nov 3 09:26:03 2020 -0500

rcu/segcblist: Add additional comments to explain smp_mb()

One counter-intuitive property of RCU is the fact that full memory barriers are needed both before and after updates to the full (non-segmented) length. This patch therefore helps to assist the reader's intuition by adding appropriate comments.

[ paulmck: Wordsmithing. ]
kernel/rcu/rcu_segcblist.c +64 -4
index bb246d8..b6dda7c 100644
@@ -94,17 +94,77 @@ static void rcu_segcblist_set_len(struct rcu_segcblist *rsclp, long v)
  * field to disagree with the actual number of callbacks on the structure.
  * This increase is fully ordered with respect to the callers accesses
  * both before and after.
+ *
+ * So why on earth is a memory barrier required both before and after
+ * the update to the ->len field???
+ *
+ * The reason is that rcu_barrier() locklessly samples each CPU's ->len
+ * field, and if a given CPU's field is zero, avoids IPIing that CPU.
+ * This can of course race with both queuing and invoking of callbacks.
+ * Failng to correctly handle either of these races could result in
+ * rcu_barrier() failing to IPI a CPU that actually had callbacks queued
+ * which rcu_barrier() was obligated to wait on.  And if rcu_barrier()
+ * failed to wait on such a callback, unloading certain kernel modules
+ * would result in calls to functions whose code was no longer present in
+ * the kernel, for but one example.
+ *
+ * Therefore, ->len transitions from 1->0 and 0->1 have to be carefully
+ * ordered with respect with both list modifications and the rcu_barrier().
+ *
+ * The queuing case is CASE 1 and the invoking case is CASE 2.
+ *
+ * CASE 1: Suppose that CPU 0 has no callbacks queued, but invokes
+ * call_rcu() just as CPU 1 invokes rcu_barrier().  CPU 0's ->len field
+ * will transition from 0->1, which is one of the transitions that must be
+ * handled carefully.  Without the full memory barriers before the ->len
+ * update and at the beginning of rcu_barrier(), the following could happen:
+ *
+ * CPU 0				CPU 1
+ *
+ * call_rcu().
+ *                      		rcu_barrier() sees ->len as 0.
+ * set ->len = 1.
+ *                      		rcu_barrier() does nothing.
+ *					module is unloaded.
+ * callback invokes unloaded function!
+ *
+ * With the full barriers, any case where rcu_barrier() sees ->len as 0 will
+ * have unambiguously preceded the return from the racing call_rcu(), which
+ * means that this call_rcu() invocation is OK to not wait on.  After all,
+ * you are supposed to make sure that any problematic call_rcu() invocations
+ * happen before the rcu_barrier().
+ *
+ *
+ * CASE 2: Suppose that CPU 0 is invoking its last callback just as CPU 1 invokes
+ * rcu_barrier().  CPU 0's ->len field will transition from 1->0, which is one
+ * of the transitions that must be handled carefully.  Without the full memory
+ * barriers after the ->len update and at the end of rcu_barrier(), the following
+ * could happen:
+ * 
+ * CPU 0				CPU 1
+ *
+ * start invoking last callback
+ * set ->len = 0 (reordered)
+ *                      		rcu_barrier() sees ->len as 0
+ *                      		rcu_barrier() does nothing.
+ *					module is unloaded
+ * callback executing after unloaded!
+ *
+ * With the full barriers, any case where rcu_barrier() sees ->len as 0
+ * will be fully ordered after the completion of the callback function,
+ * so that the module unloading operation is completely safe.
+ * 
  */
 void rcu_segcblist_add_len(struct rcu_segcblist *rsclp, long v)
 {
 #ifdef CONFIG_RCU_NOCB_CPU
-	smp_mb__before_atomic(); /* Up to the caller! */
+	smp_mb__before_atomic(); // Read header comment above.
 	atomic_long_add(v, &rsclp->len);
-	smp_mb__after_atomic(); /* Up to the caller! */
+	smp_mb__after_atomic();  // Read header comment above.
 #else
-	smp_mb(); /* Up to the caller! */
+	smp_mb(); // Read header comment above.
 	WRITE_ONCE(rsclp->len, rsclp->len + v);
-	smp_mb(); /* Up to the caller! */
+	smp_mb(); // Read header comment above.
 #endif
 }
 
#38 lore raw

Re: [PATCH v9 7/7] rcu/segcblist: Add additional comments to explain smp_mb()

Quoted 68 lines · Paul E. McKenney
Quoted 6 lines · Joel Fernandes (Google)

Memory barriers are needed when updating the full length of the segcblist, however it is not fully clearly why one is needed before and after. This patch therefore adds additional comments to the function header to explain it.

Looks good, thank you! As always, I could not resist the urge to do a bit of wordsmithing, so that the queued commit is as shown below. Please let me know if I messed anything up.

Thanx, Paul

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

commit 7dac7adefcae7558b3a85a16f51186d621623733

Author: Joel Fernandes (Google) <[email protected]>
Date:   Tue Nov 3 09:26:03 2020 -0500

rcu/segcblist: Add additional comments to explain smp_mb()

One counter-intuitive property of RCU is the fact that full memory barriers are needed both before and after updates to the full (non-segmented) length. This patch therefore helps to assist the reader's intuition by adding appropriate comments.

[ paulmck: Wordsmithing. ]
kernel/rcu/rcu_segcblist.c +29 -0
index bb246d8..b6dda7c 100644
@@ -94,17 +94,77 @@ static void rcu_segcblist_set_len(struct rcu_segcblist *rsclp, long v)
  * field to disagree with the actual number of callbacks on the structure.
  * This increase is fully ordered with respect to the callers accesses
  * both before and after.
+ *
+ * So why on earth is a memory barrier required both before and after
+ * the update to the ->len field???
+ *
+ * The reason is that rcu_barrier() locklessly samples each CPU's ->len
+ * field, and if a given CPU's field is zero, avoids IPIing that CPU.
+ * This can of course race with both queuing and invoking of callbacks.
+ * Failng to correctly handle either of these races could result in
+ * rcu_barrier() failing to IPI a CPU that actually had callbacks queued
+ * which rcu_barrier() was obligated to wait on.  And if rcu_barrier()
+ * failed to wait on such a callback, unloading certain kernel modules
+ * would result in calls to functions whose code was no longer present in
+ * the kernel, for but one example.
+ *
+ * Therefore, ->len transitions from 1->0 and 0->1 have to be carefully
+ * ordered with respect with both list modifications and the rcu_barrier().
+ *
+ * The queuing case is CASE 1 and the invoking case is CASE 2.
+ *
+ * CASE 1: Suppose that CPU 0 has no callbacks queued, but invokes
+ * call_rcu() just as CPU 1 invokes rcu_barrier().  CPU 0's ->len field
+ * will transition from 0->1, which is one of the transitions that must be
+ * handled carefully.  Without the full memory barriers before the ->len
+ * update and at the beginning of rcu_barrier(), the following could happen:
+ *
+ * CPU 0				CPU 1
+ *
+ * call_rcu().
+ *                      		rcu_barrier() sees ->len as 0.

kernel/rcu/rcu_segcblist.c static void rcu_segcblist_set_len(struct rcu_segcblist *rsclp, long v) · line 135

Quoted 10 lines
kernel/rcu/rcu_segcblist.c +10 -0
+ * set ->len = 1.
+ *                      		rcu_barrier() does nothing.
+ *					module is unloaded.
+ * callback invokes unloaded function!
+ *
+ * With the full barriers, any case where rcu_barrier() sees ->len as 0 will
+ * have unambiguously preceded the return from the racing call_rcu(), which
+ * means that this call_rcu() invocation is OK to not wait on.  After all,
+ * you are supposed to make sure that any problematic call_rcu() invocations
+ * happen before the rcu_barrier().

Unfortunately, I did not understand your explanation. To me the barrier *before* the setting of length is needed on CPU0 only for 1->0 transition (Dequeue). Where as in
your example above, it is for enqueue.

This was case 1 in my patch:

+ * To illustrate the problematic scenario to avoid:
+ * P0 (what P1 sees) P1
+ * set len = 0
+ * rcu_barrier sees len as 0
+ * dequeue from list
+ * rcu_barrier does nothing.
+ *

Here, P1 should see the transition of 1->0 *after* the CB is dequeued. Which means you needed a memory barrier *before* the setting of len from 1->0 and *after* the dequeue. IOW, rcu_barrier should 'see' the memory ordering as:

1. dequeue
2. set len from 1 -> 0.

For the enqueue case, it is the reverse, rcu_barrier should see:
1. set len from 0 -> 1
2. enqueue

Either way, the point I think I was trying to make is that the length should always be seen as non-zero if the list is non-empty. Basically, the rcu_barrier() should always not do the fast-path if the list is non-empty. Worst-case it might do the slow-path when it is not necessary, but it should never do the fast-path when it was not supposed to.

Thoughts?

thanks,

- Joel

Quoted 39 lines
kernel/rcu/rcu_segcblist.c +25 -4
+ *
+ *
+ * CASE 2: Suppose that CPU 0 is invoking its last callback just as CPU 1 invokes
+ * rcu_barrier().  CPU 0's ->len field will transition from 1->0, which is one
+ * of the transitions that must be handled carefully.  Without the full memory
+ * barriers after the ->len update and at the end of rcu_barrier(), the following
+ * could happen:
+ * 
+ * CPU 0				CPU 1
+ *
+ * start invoking last callback
+ * set ->len = 0 (reordered)
+ *                      		rcu_barrier() sees ->len as 0
+ *                      		rcu_barrier() does nothing.
+ *					module is unloaded
+ * callback executing after unloaded!
+ *
+ * With the full barriers, any case where rcu_barrier() sees ->len as 0
+ * will be fully ordered after the completion of the callback function,
+ * so that the module unloading operation is completely safe.
+ * 
  */
 void rcu_segcblist_add_len(struct rcu_segcblist *rsclp, long v)
 {
 #ifdef CONFIG_RCU_NOCB_CPU
-	smp_mb__before_atomic(); /* Up to the caller! */
+	smp_mb__before_atomic(); // Read header comment above.
 	atomic_long_add(v, &rsclp->len);
-	smp_mb__after_atomic(); /* Up to the caller! */
+	smp_mb__after_atomic();  // Read header comment above.
 #else
-	smp_mb(); /* Up to the caller! */
+	smp_mb(); // Read header comment above.
 	WRITE_ONCE(rsclp->len, rsclp->len + v);
-	smp_mb(); /* Up to the caller! */
+	smp_mb(); // Read header comment above.
 #endif
 }
 
maintainer
#39 lore raw

Re: [PATCH v9 7/7] rcu/segcblist: Add additional comments to explain smp_mb()

Quoted 107 lines · Joel Fernandes
Quoted 68 lines · Paul E. McKenney
Quoted 6 lines · Joel Fernandes (Google)

Memory barriers are needed when updating the full length of the segcblist, however it is not fully clearly why one is needed before and after. This patch therefore adds additional comments to the function header to explain it.

Looks good, thank you! As always, I could not resist the urge to do a bit of wordsmithing, so that the queued commit is as shown below. Please let me know if I messed anything up.

Thanx, Paul

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

commit 7dac7adefcae7558b3a85a16f51186d621623733

Author: Joel Fernandes (Google) <[email protected]>
Date:   Tue Nov 3 09:26:03 2020 -0500

rcu/segcblist: Add additional comments to explain smp_mb()

One counter-intuitive property of RCU is the fact that full memory barriers are needed both before and after updates to the full (non-segmented) length. This patch therefore helps to assist the reader's intuition by adding appropriate comments.

[ paulmck: Wordsmithing. ]
kernel/rcu/rcu_segcblist.c +29 -0
index bb246d8..b6dda7c 100644
@@ -94,17 +94,77 @@ static void rcu_segcblist_set_len(struct rcu_segcblist *rsclp, long v)
  * field to disagree with the actual number of callbacks on the structure.
  * This increase is fully ordered with respect to the callers accesses
  * both before and after.
+ *
+ * So why on earth is a memory barrier required both before and after
+ * the update to the ->len field???
+ *
+ * The reason is that rcu_barrier() locklessly samples each CPU's ->len
+ * field, and if a given CPU's field is zero, avoids IPIing that CPU.
+ * This can of course race with both queuing and invoking of callbacks.
+ * Failng to correctly handle either of these races could result in
+ * rcu_barrier() failing to IPI a CPU that actually had callbacks queued
+ * which rcu_barrier() was obligated to wait on.  And if rcu_barrier()
+ * failed to wait on such a callback, unloading certain kernel modules
+ * would result in calls to functions whose code was no longer present in
+ * the kernel, for but one example.
+ *
+ * Therefore, ->len transitions from 1->0 and 0->1 have to be carefully
+ * ordered with respect with both list modifications and the rcu_barrier().
+ *
+ * The queuing case is CASE 1 and the invoking case is CASE 2.
+ *
+ * CASE 1: Suppose that CPU 0 has no callbacks queued, but invokes
+ * call_rcu() just as CPU 1 invokes rcu_barrier().  CPU 0's ->len field
+ * will transition from 0->1, which is one of the transitions that must be
+ * handled carefully.  Without the full memory barriers before the ->len
+ * update and at the beginning of rcu_barrier(), the following could happen:
+ *
+ * CPU 0				CPU 1
+ *
+ * call_rcu().
+ *                      		rcu_barrier() sees ->len as 0.

kernel/rcu/rcu_segcblist.c static void rcu_segcblist_set_len(struct rcu_segcblist *rsclp, long v) · line 135

Quoted 10 lines
kernel/rcu/rcu_segcblist.c +10 -0
+ * set ->len = 1.
+ *                      		rcu_barrier() does nothing.
+ *					module is unloaded.
+ * callback invokes unloaded function!
+ *
+ * With the full barriers, any case where rcu_barrier() sees ->len as 0 will
+ * have unambiguously preceded the return from the racing call_rcu(), which
+ * means that this call_rcu() invocation is OK to not wait on.  After all,
+ * you are supposed to make sure that any problematic call_rcu() invocations
+ * happen before the rcu_barrier().

Unfortunately, I did not understand your explanation. To me the barrier *before* the setting of length is needed on CPU0 only for 1->0 transition (Dequeue). Where as in
your example above, it is for enqueue.

This was case 1 in my patch:

+ * To illustrate the problematic scenario to avoid:
+ * P0 (what P1 sees) P1
+ * set len = 0
+ * rcu_barrier sees len as 0
+ * dequeue from list
+ * rcu_barrier does nothing.
+ *

Here, P1 should see the transition of 1->0 *after* the CB is dequeued. Which means you needed a memory barrier *before* the setting of len from 1->0 and *after* the dequeue. IOW, rcu_barrier should 'see' the memory ordering as:

1. dequeue
2. set len from 1 -> 0.

For the enqueue case, it is the reverse, rcu_barrier should see:
1. set len from 0 -> 1
2. enqueue

Quoted 7 lines

Either way, the point I think I was trying to make is that the length should always be seen as non-zero if the list is non-empty. Basically, the rcu_barrier() should always not do the fast-path if the list is non-empty. Worst-case it might do the slow-path when it is not necessary, but it should never do the fast-path when it was not supposed to.

Thoughts?

Right you are! I reversed the before/after associated with ->len. I will fix this.

Thanx, Paul

Quoted 45 lines

thanks,

- Joel

Quoted 39 lines
kernel/rcu/rcu_segcblist.c +25 -4
+ *
+ *
+ * CASE 2: Suppose that CPU 0 is invoking its last callback just as CPU 1 invokes
+ * rcu_barrier().  CPU 0's ->len field will transition from 1->0, which is one
+ * of the transitions that must be handled carefully.  Without the full memory
+ * barriers after the ->len update and at the end of rcu_barrier(), the following
+ * could happen:
+ * 
+ * CPU 0				CPU 1
+ *
+ * start invoking last callback
+ * set ->len = 0 (reordered)
+ *                      		rcu_barrier() sees ->len as 0
+ *                      		rcu_barrier() does nothing.
+ *					module is unloaded
+ * callback executing after unloaded!
+ *
+ * With the full barriers, any case where rcu_barrier() sees ->len as 0
+ * will be fully ordered after the completion of the callback function,
+ * so that the module unloading operation is completely safe.
+ * 
  */
 void rcu_segcblist_add_len(struct rcu_segcblist *rsclp, long v)
 {
 #ifdef CONFIG_RCU_NOCB_CPU
-	smp_mb__before_atomic(); /* Up to the caller! */
+	smp_mb__before_atomic(); // Read header comment above.
 	atomic_long_add(v, &rsclp->len);
-	smp_mb__after_atomic(); /* Up to the caller! */
+	smp_mb__after_atomic();  // Read header comment above.
 #else
-	smp_mb(); /* Up to the caller! */
+	smp_mb(); // Read header comment above.
 	WRITE_ONCE(rsclp->len, rsclp->len + v);
-	smp_mb(); /* Up to the caller! */
+	smp_mb(); // Read header comment above.
 #endif
 }