Skip to contentSkip to conversation

Add support for length of each segment in the segcblist

PATCH Superseded Joel Fernandes (Google) 4 participants 16 messages Sep 23, 2020 – Oct 15, 2020

Subsystems (2)

v6 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v6 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.

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:
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/[email protected]/)

Joel Fernandes (Google) (4):
rcu/tree: Make rcu_do_batch count how many callbacks were executed rcu/segcblist: Add counters to segcblist datastructure rcu/trace: Add tracing for how segcb list changes rcu/segcblist: Remove useless rcupdate.h include

include/linux/rcu_segcblist.h |   2 +
include/trace/events/rcu.h    |  25 ++++++
kernel/rcu/rcu_segcblist.c    | 161 +++++++++++++++++++++++++---------
kernel/rcu/rcu_segcblist.h    |   8 +-
kernel/rcu/tree.c             |  18 ++--
5 files changed, 165 insertions(+), 49 deletions(-)

--
2.28.0.681.g6f77f65b4e-goog

#1 lore raw

PATCH v6 1/4 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 have 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          | 9 ++++-----
 3 files changed, 6 insertions(+), 6 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 5c293afc07b8..b90725f81d77 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 +4 -5
index 7623128d0020..50af465729f4 100644
@@ -2427,7 +2427,7 @@ static void rcu_do_batch(struct rcu_data *rdp)
 			       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. */
@@ -2472,6 +2472,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);
@@ -2485,9 +2486,8 @@ 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;
@@ -2510,7 +2510,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());
@@ -2518,7 +2517,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.28.0.681.g6f77f65b4e-goog
maintainer Reviewed-by on v6 1/4
#2 lore raw

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

Quoted 11 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.

Quoted 5 lines

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.

Right :)

Quoted 2 lines

This commit therefore explicitly counts have many callbacks were executed in

s/have/how

Quoted 4 lines

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.

Thanks.

#3 lore raw

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

Quoted 13 lines · Frederic Weisbecker
Quoted 11 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.

Quoted 7 lines
Quoted 5 lines

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.

Right :)

:)

Quoted 10 lines
Quoted 1 line

This commit therefore explicitly counts have many callbacks were executed in

s/have/how

Quoted 4 lines

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.

Thanks! Paul would be Ok to make the minor fixup s/have/how/ that Frederic pointed?

- Joel
(Due to COVID issues at home, I'm intermittently working so advance apologies for slow replies.)

maintainer
#4 lore raw

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

Quoted 31 lines · Joel Fernandes
Quoted 13 lines · Frederic Weisbecker
Quoted 11 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.

Quoted 7 lines
Quoted 5 lines

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.

Right :)

:)

Quoted 7 lines
Quoted 1 line

This commit therefore explicitly counts have many callbacks were executed in

s/have/how

Quoted 3 lines

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 6 lines
Quoted 3 lines
Quoted 1 line

Thanks! Paul would be Ok to make the minor fixup s/have/how/ that Frederic pointed?

But of course! I was waiting until Frederic gets them all reviewed, with an eye to applying and wordsmithing them as a set.

Quoted 3 lines

- Joel
(Due to COVID issues at home, I'm intermittently working so advance apologies for slow replies.)

And I hope that this is going as well as it possibly can!

Thanx, Paul

#5 lore raw

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

In reply to Joel Fernandes (Google)
Quoted 76 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.

This commit therefore explicitly counts have 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          | 9 ++++-----
  3 files changed, 6 insertions(+), 6 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 5c293afc07b8..b90725f81d77 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 +2 -1
index 7623128d0020..50af465729f4 100644
@@ -2427,7 +2427,7 @@ static void rcu_do_batch(struct rcu_data *rdp)
  			       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. */
@@ -2472,6 +2472,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);

kernel/rcu/tree.c static void rcu_do_batch(struct rcu_data *rdp) · line 2493

Quoted 10 lines
kernel/rcu/tree.c +1 -2
@@ -2485,9 +2486,8 @@ 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;

Update below usage of -rcl.len also?

if (likely((-rcl.len & 31) || local_clock() < tlimit))

Thanks
Neeraj

Quoted 18 lines
kernel/rcu/tree.c +1 -2
@@ -2510,7 +2510,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());
@@ -2518,7 +2517,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
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation
#6 lore raw

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

Quoted 87 lines · Neeraj Upadhyay
Quoted 72 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.

This commit therefore explicitly counts have 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          | 9 ++++-----
  3 files changed, 6 insertions(+), 6 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 5c293afc07b8..b90725f81d77 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 +2 -1
index 7623128d0020..50af465729f4 100644
@@ -2427,7 +2427,7 @@ static void rcu_do_batch(struct rcu_data *rdp)
  			       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. */
@@ -2472,6 +2472,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);

kernel/rcu/tree.c static void rcu_do_batch(struct rcu_data *rdp) · line 2492

Quoted 9 lines
kernel/rcu/tree.c +1 -2
@@ -2485,9 +2486,8 @@ 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;

Update below usage of -rcl.len also?

Quoted 1 line

if (likely((-rcl.len & 31) || local_clock() < tlimit))

Yes, you are right. I need to change that as well, will do. Thanks!

thanks,

- Joel

#7 lore raw

PATCH v6 2/4 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 |   2 +
 kernel/rcu/rcu_segcblist.c    | 124 +++++++++++++++++++++++-----------
 kernel/rcu/rcu_segcblist.h    |   2 -
 3 files changed, 86 insertions(+), 42 deletions(-)
include/linux/rcu_segcblist.h +2 -0
index b36afe7b22c9..d462ae5e340a 100644
@@ -69,8 +69,10 @@ struct rcu_segcblist {
 	unsigned long gp_seq[RCU_CBLIST_NSEGS];
 #ifdef CONFIG_RCU_NOCB_CPU
 	atomic_long_t len;
+	atomic_long_t seglen[RCU_CBLIST_NSEGS];
 #else
 	long len;
+	long seglen[RCU_CBLIST_NSEGS];
 #endif
 	u8 enabled;
 	u8 offloaded;
kernel/rcu/rcu_segcblist.c +84 -40

235 lines not on this page · raw message

kernel/rcu/rcu_segcblist.h +0 -2
index b90725f81d77..3e0eb1056ae9 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.28.0.681.g6f77f65b4e-goog
#8 lore raw

Re: [PATCH v6 2/4] rcu/segcblist: Add counters to segcblist datastructure

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

Quoted 13 lines · Joel Fernandes (Google)
kernel/rcu/rcu_segcblist.c +13 -0
+/* 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)
+{
+#ifdef CONFIG_RCU_NOCB_CPU
+	smp_mb__before_atomic(); /* Up to the caller! */
+	atomic_long_add(v, &rsclp->seglen[seg]);
+	smp_mb__after_atomic(); /* Up to the caller! */
+#else
+	smp_mb(); /* Up to the caller! */
+	WRITE_ONCE(rsclp->seglen[seg], rsclp->seglen[seg] + v);
+	smp_mb(); /* Up to the caller! */
+#endif
+}

I know that these "Up to the caller" comments come from the existing len functions but perhaps we should explain a bit more against what it is ordering and what it pairs to.

Also why do we need one before _and_ after?

And finally do we have the same ordering requirements than the unsegmented len field?

Quoted 17 lines
kernel/rcu/rcu_segcblist.c +17 -0
+
+/* 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);
+}
+

[...]

kernel/rcu/rcu_segcblist.c void rcu_segcblist_enqueue(struct rcu_segcblist *rsclp, · line 287

Quoted 6 lines
kernel/rcu/rcu_segcblist.c +1 -0
@@ -245,6 +283,7 @@ void rcu_segcblist_enqueue(struct rcu_segcblist *rsclp,
 			   struct rcu_head *rhp)
 {
 	rcu_segcblist_inc_len(rsclp);
+	rcu_segcblist_inc_seglen(rsclp, RCU_NEXT_TAIL);
 	smp_mb(); /* Ensure counts are updated before callback is enqueued. */

Since inc_len and even now inc_seglen have two full barriers embracing the add up, we can probably spare the above smp_mb()?

Quoted 14 lines
kernel/rcu/rcu_segcblist.c +1 -0
 	rhp->next = NULL;
 	WRITE_ONCE(*rsclp->tails[RCU_NEXT_TAIL], rhp);
@@ -274,27 +313,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;
 }
 

kernel/rcu/rcu_segcblist.c void rcu_segcblist_advance(struct rcu_segcblist *rsclp, unsigned long seq) · line 440

Quoted 4 lines
kernel/rcu/rcu_segcblist.c +1 -0
@@ -403,6 +437,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);

Do we still need the same amount of full barriers contained in add() called by move() here? It's called in the reverse order (write queue then len) than usual. If I trust the comment in rcu_segcblist_enqueue(), the point of the barrier is to make the length visible before the new callback for rcu_barrier() (although that concerns len and not seglen). But here above, the unsegmented length doesn't change. I could understand a write barrier between add_seglen(x, i) and set_seglen(0, RCU_DONE_TAIL) but I couldn't find a paired couple either.

kernel/rcu/rcu_segcblist.c void rcu_segcblist_advance(struct rcu_segcblist *rsclp, unsigned long seq) · line 461

Quoted 8 lines
kernel/rcu/rcu_segcblist.c +1 -0
 	}
 
 	/* If no callbacks moved, nothing more need be done. */
@@ -423,6 +458,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);

Same question here (feel free to reply "same answer" :o)

Thanks!

#9 lore raw

Re: [PATCH v6 2/4] rcu/segcblist: Add counters to segcblist datastructure

Quoted 19 lines · Frederic Weisbecker

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

Quoted 13 lines · Joel Fernandes (Google)
kernel/rcu/rcu_segcblist.c +13 -0
+/* 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)
+{
+#ifdef CONFIG_RCU_NOCB_CPU
+	smp_mb__before_atomic(); /* Up to the caller! */
+	atomic_long_add(v, &rsclp->seglen[seg]);
+	smp_mb__after_atomic(); /* Up to the caller! */
+#else
+	smp_mb(); /* Up to the caller! */
+	WRITE_ONCE(rsclp->seglen[seg], rsclp->seglen[seg] + v);
+	smp_mb(); /* Up to the caller! */
+#endif
+}

I know that these "Up to the caller" comments come from the existing len functions but perhaps we should explain a bit more against what it is ordering and what it pairs to.

Quoted 4 lines

Also why do we need one before _and_ after?

And finally do we have the same ordering requirements than the unsegmented len field?

Hi Paul and Neeraj,
Would be nice to discuss this on the call. I actually borrowed the memory barriers from add_len() just to be safe, but I think Frederic's points are valid. Would be nice if we can go over all the usecases and discuss which memory barriers are needed. Thanks for your help!

Another thought: inc_len() calls add_len() which already has smp_mb(), so callers of inc_len also do not need memory barriers I think.

thanks,

- Joel

Quoted 66 lines
Quoted 17 lines
kernel/rcu/rcu_segcblist.c +17 -0
+
+/* 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);
+}
+

[...]

kernel/rcu/rcu_segcblist.c void rcu_segcblist_enqueue(struct rcu_segcblist *rsclp, · line 287

Quoted 6 lines
kernel/rcu/rcu_segcblist.c +1 -0
@@ -245,6 +283,7 @@ void rcu_segcblist_enqueue(struct rcu_segcblist *rsclp,
 			   struct rcu_head *rhp)
 {
 	rcu_segcblist_inc_len(rsclp);
+	rcu_segcblist_inc_seglen(rsclp, RCU_NEXT_TAIL);
 	smp_mb(); /* Ensure counts are updated before callback is enqueued. */

Since inc_len and even now inc_seglen have two full barriers embracing the add up, we can probably spare the above smp_mb()?

Quoted 14 lines
kernel/rcu/rcu_segcblist.c +1 -0
 	rhp->next = NULL;
 	WRITE_ONCE(*rsclp->tails[RCU_NEXT_TAIL], rhp);
@@ -274,27 +313,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;
 }
 

kernel/rcu/rcu_segcblist.c void rcu_segcblist_advance(struct rcu_segcblist *rsclp, unsigned long seq) · line 440

Quoted 4 lines
kernel/rcu/rcu_segcblist.c +1 -0
@@ -403,6 +437,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);

Do we still need the same amount of full barriers contained in add() called by move() here? It's called in the reverse order (write queue then len) than usual. If I trust the comment in rcu_segcblist_enqueue(), the point of the barrier is to make the length visible before the new callback for rcu_barrier() (although that concerns len and not seglen). But here above, the unsegmented length doesn't change. I could understand a write barrier between add_seglen(x, i) and set_seglen(0, RCU_DONE_TAIL) but I couldn't find a paired couple either.

kernel/rcu/rcu_segcblist.c void rcu_segcblist_advance(struct rcu_segcblist *rsclp, unsigned long seq) · line 461

Quoted 8 lines
kernel/rcu/rcu_segcblist.c +1 -0
 	}
 
 	/* If no callbacks moved, nothing more need be done. */
@@ -423,6 +458,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);

Same question here (feel free to reply "same answer" :o)

Thanks!

#10 lore raw

Re: [PATCH v6 2/4] rcu/segcblist: Add counters to segcblist datastructure

In reply to Frederic Weisbecker
Quoted 10 lines · Frederic Weisbecker
Quoted 9 lines · Joel Fernandes (Google)
kernel/rcu/rcu_segcblist.c +9 -0
+/* 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)
+{
+#ifdef CONFIG_RCU_NOCB_CPU
+	smp_mb__before_atomic(); /* Up to the caller! */
+	atomic_long_add(v, &rsclp->seglen[seg]);
+	smp_mb__after_atomic(); /* Up to the caller! */
+#else
+	smp_mb(); /* Up to the caller! */
Quoted 8 lines

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

Quoted 4 lines
kernel/rcu/rcu_segcblist.c +4 -0
+	WRITE_ONCE(rsclp->seglen[seg], rsclp->seglen[seg] + v);
+	smp_mb(); /* Up to the caller! */
+#endif
+}

I know that these "Up to the caller" comments come from the existing len functions but perhaps we should explain a bit more against what it is ordering and what it pairs to.

Sure.

Quoted 1 line

Also why do we need one before _and_ after?

I removed these memory barriers since they should not be needed, I will update it this way for v7.

Quoted 2 lines

And finally do we have the same ordering requirements than the unsegmented len field?

Do you mean ordering for the rsclp->seglen ? Yes we need not have ordering for that since there are no races AFAICS (all accesses have either IRQs are disabled, or nocb lock is held for the offloaded case). If you meant something else like rcl->len, let me know. AFAICS, we don't have ordering needs for those. Further, current readers of ->seglen are only for tracing. ->seglen does not influence rcu_barrier yet.

Quoted 17 lines
Quoted 16 lines
kernel/rcu/rcu_segcblist.c +16 -0
+/* 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);
+}
+

[...]

Quoted 9 lines

kernel/rcu/rcu_segcblist.c void rcu_segcblist_enqueue(struct rcu_segcblist *rsclp, · line 287

Quoted 6 lines
kernel/rcu/rcu_segcblist.c +1 -0
@@ -245,6 +283,7 @@ void rcu_segcblist_enqueue(struct rcu_segcblist *rsclp,
 			   struct rcu_head *rhp)
 {
 	rcu_segcblist_inc_len(rsclp);
+	rcu_segcblist_inc_seglen(rsclp, RCU_NEXT_TAIL);
 	smp_mb(); /* Ensure counts are updated before callback is enqueued. */

Since inc_len and even now inc_seglen have two full barriers embracing the add up, we can probably spare the above smp_mb()?

Good point, I'll remove it.

Quoted 13 lines
Quoted 13 lines
kernel/rcu/rcu_segcblist.c +1 -0
 	rhp->next = NULL;
 	WRITE_ONCE(*rsclp->tails[RCU_NEXT_TAIL], rhp);
@@ -274,27 +313,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;
 }
 
Quoted 12 lines

kernel/rcu/rcu_segcblist.c void rcu_segcblist_advance(struct rcu_segcblist *rsclp, unsigned long seq) · line 440

Quoted 5 lines
kernel/rcu/rcu_segcblist.c +1 -0
@@ -403,6 +437,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);

Do we still need the same amount of full barriers contained in add() called by move() here? It's called in the reverse order (write queue then len) than usual. If I trust the comment in rcu_segcblist_enqueue(), the point of the barrier is to make the length visible before the new callback for rcu_barrier() (although that concerns len and not seglen). But here above, the unsegmented length doesn't change. I could understand a write barrier between add_seglen(x, i) and set_seglen(0, RCU_DONE_TAIL) but I couldn't find a paired couple either.

I'm guessing since I removed the memory barriers from seglen updates, this is resolved.

Quoted 10 lines

kernel/rcu/rcu_segcblist.c void rcu_segcblist_advance(struct rcu_segcblist *rsclp, unsigned long seq) · line 461

Quoted 8 lines
kernel/rcu/rcu_segcblist.c +1 -0
 	}
 
 	/* If no callbacks moved, nothing more need be done. */
@@ -423,6 +458,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);

Same question here (feel free to reply "same answer" :o)

Same answer :P

So based on these and other comments, I will update the patches and send them out shortly.

thanks,

- Joel

#11 lore raw

PATCH v6 3/4 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..7b84df3c95df 100644
@@ -505,6 +505,31 @@ TRACE_EVENT_RCU(rcu_callback,
 		  __entry->qlen)
 );
 
+TRACE_EVENT_RCU(rcu_segcb,
+
+		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, 4)
+			__array(unsigned long, gp_seq, 4)
+		),
+
+		TP_fast_assign(
+			__entry->ctx = ctx;
+			memcpy(__entry->cb_count, cb_count, 4 * sizeof(int));
+			memcpy(__entry->gp_seq, gp_seq, 4 * 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 0e6d19bd3de9..df0f31e30947 100644
@@ -13,6 +13,7 @@
 #include <linux/rcupdate.h>
 
 #include "rcu_segcblist.h"
+#include "rcu.h"
 
 /* Initialize simple callback list. */
 void rcu_cblist_init(struct rcu_cblist *rclp)
@@ -343,6 +344,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 callbacks. 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] = 0;
+
+	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_list(struct rcu_segcblist *rsclp, char *context)
+{
+	int cbs[RCU_CBLIST_NSEGS];
+	unsigned long gps[RCU_CBLIST_NSEGS];
+
+	rcu_segcblist_countseq(rsclp, cbs, gps);
+
+	trace_rcu_segcb(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 3e0eb1056ae9..15c10d30f88c 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_list(struct rcu_segcblist *rsclp, char *context);
+#else
+#define trace_rcu_segcb_list(...)
+#endif
kernel/rcu/tree.c +9 -0
index 50af465729f4..e3381ff67fc6 100644
@@ -1492,6 +1492,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_list(&rdp->cblist, "SegCbPreAcc");
+
 	/*
 	 * Callbacks are often registered with incomplete grace-period
 	 * information.  Something about the fact that getting exact
@@ -1512,6 +1514,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_list(&rdp->cblist, "SegCbPostAcc");
+
 	return ret;
 }
 
@@ -2469,6 +2473,9 @@ static void rcu_do_batch(struct rcu_data *rdp)
 	/* Invoke callbacks. */
 	tick_dep_set_task(current, TICK_DEP_BIT_RCU);
 	rhp = rcu_cblist_dequeue(&rcl);
+
+	trace_rcu_segcb_list(&rdp->cblist, "SegCbDequeued");
+
 	for (; rhp; rhp = rcu_cblist_dequeue(&rcl)) {
 		rcu_callback_t f;
 
@@ -2982,6 +2989,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_list(&rdp->cblist, "SegCBQueued");
+
 	/* Go handle any RCU core processing required. */
 	if (IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
 	    unlikely(rcu_segcblist_is_offloaded(&rdp->cblist))) {
Signature · 1 line
2.28.0.681.g6f77f65b4e-goog
#12 lore raw

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

Quoted 23 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..7b84df3c95df 100644
@@ -505,6 +505,31 @@ TRACE_EVENT_RCU(rcu_callback,
  		  __entry->qlen)
  );
 

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

Quoted 10 lines
include/trace/events/rcu.h +10 -0
+TRACE_EVENT_RCU(rcu_segcb,
+
+		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, 4)
+			__array(unsigned long, gp_seq, 4)

Use RCU_CBLIST_NSEGS in place of 4 ?

Quoted 31 lines
include/trace/events/rcu.h +15 -0
+		),
+
+		TP_fast_assign(
+			__entry->ctx = ctx;
+			memcpy(__entry->cb_count, cb_count, 4 * sizeof(int));
+			memcpy(__entry->gp_seq, gp_seq, 4 * 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 +1 -0
index 0e6d19bd3de9..df0f31e30947 100644
@@ -13,6 +13,7 @@
  #include <linux/rcupdate.h>
 
  #include "rcu_segcblist.h"
+#include "rcu.h"
 
  /* Initialize simple callback list. */
  void rcu_cblist_init(struct rcu_cblist *rclp)

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

Quoted 7 lines
kernel/rcu/rcu_segcblist.c +4 -0
@@ -343,6 +344,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 callbacks. Only used from

N is number of segments?

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

Quoted 12 lines
kernel/rcu/rcu_segcblist.c +12 -0
+ * 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] = 0;
+

What is the reason for initializing to 0?

Quoted 38 lines
kernel/rcu/rcu_segcblist.c +17 -0
+	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_list(struct rcu_segcblist *rsclp, char *context)
+{
+	int cbs[RCU_CBLIST_NSEGS];
+	unsigned long gps[RCU_CBLIST_NSEGS];
+
+	rcu_segcblist_countseq(rsclp, cbs, gps);
+
+	trace_rcu_segcb(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 3e0eb1056ae9..15c10d30f88c 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_list(struct rcu_segcblist *rsclp, char *context);
+#else
+#define trace_rcu_segcb_list(...)
+#endif
kernel/rcu/tree.c +0 -0
index 50af465729f4..e3381ff67fc6 100644

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

Quoted 4 lines
kernel/rcu/tree.c +1 -0
@@ -1492,6 +1492,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_list(&rdp->cblist, "SegCbPreAcc");

Use TPS("SegCbPreAcc") ?

Thanks
Neeraj

Quoted 33 lines
kernel/rcu/tree.c +8 -0
+
  	/*
  	 * Callbacks are often registered with incomplete grace-period
  	 * information.  Something about the fact that getting exact
@@ -1512,6 +1514,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_list(&rdp->cblist, "SegCbPostAcc");
+
  	return ret;
  }
 
@@ -2469,6 +2473,9 @@ static void rcu_do_batch(struct rcu_data *rdp)
  	/* Invoke callbacks. */
  	tick_dep_set_task(current, TICK_DEP_BIT_RCU);
  	rhp = rcu_cblist_dequeue(&rcl);
+
+	trace_rcu_segcb_list(&rdp->cblist, "SegCbDequeued");
+
  	for (; rhp; rhp = rcu_cblist_dequeue(&rcl)) {
  		rcu_callback_t f;
 
@@ -2982,6 +2989,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_list(&rdp->cblist, "SegCBQueued");
+
  	/* Go handle any RCU core processing required. */
  	if (IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
  	    unlikely(rcu_segcblist_is_offloaded(&rdp->cblist))) {
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
#13 lore raw

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

Quoted 29 lines · Neeraj Upadhyay
Quoted 26 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 +4 -0
index 155b5cb43cfd..7b84df3c95df 100644
@@ -505,6 +505,31 @@ TRACE_EVENT_RCU(rcu_callback,
  		  __entry->qlen)
  );
+TRACE_EVENT_RCU(rcu_segcb,
+
+		TP_PROTO(const char *ctx, int *cb_count, unsigned long *gp_seq),
+
Quoted 8 lines

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

Quoted 6 lines
include/trace/events/rcu.h +6 -0
+		TP_ARGS(ctx, cb_count, gp_seq),
+
+		TP_STRUCT__entry(
+			__field(const char *, ctx)
+			__array(int, cb_count, 4)
+			__array(unsigned long, gp_seq, 4)

Use RCU_CBLIST_NSEGS in place of 4 ?

Done.

Quoted 29 lines
Quoted 29 lines
include/trace/events/rcu.h +15 -0
+		),
+
+		TP_fast_assign(
+			__entry->ctx = ctx;
+			memcpy(__entry->cb_count, cb_count, 4 * sizeof(int));
+			memcpy(__entry->gp_seq, gp_seq, 4 * 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 +1 -0
index 0e6d19bd3de9..df0f31e30947 100644
@@ -13,6 +13,7 @@
  #include <linux/rcupdate.h>
  #include "rcu_segcblist.h"
+#include "rcu.h"
  /* Initialize simple callback list. */
  void rcu_cblist_init(struct rcu_cblist *rclp)
@@ -343,6 +344,39 @@ void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp,
Quoted 8 lines

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

Quoted 6 lines
kernel/rcu/rcu_segcblist.c +4 -0
  	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 callbacks. Only used from

N is number of segments?

Yes, will fix.

Quoted 6 lines
Quoted 6 lines
kernel/rcu/rcu_segcblist.c +6 -0
+ * 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])
Quoted 8 lines

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

Quoted 6 lines
kernel/rcu/rcu_segcblist.c +6 -0
+{
+	int i;
+
+	for (i = 0; i < RCU_CBLIST_NSEGS; i++)
+		cbcount[i] = 0;
+

What is the reason for initializing to 0?

You are right, not needed. I'll remove.

Quoted 35 lines
Quoted 35 lines
kernel/rcu/rcu_segcblist.c +17 -0
+	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_list(struct rcu_segcblist *rsclp, char *context)
+{
+	int cbs[RCU_CBLIST_NSEGS];
+	unsigned long gps[RCU_CBLIST_NSEGS];
+
+	rcu_segcblist_countseq(rsclp, cbs, gps);
+
+	trace_rcu_segcb(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 3e0eb1056ae9..15c10d30f88c 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_list(struct rcu_segcblist *rsclp, char *context);
+#else
+#define trace_rcu_segcb_list(...)
+#endif
kernel/rcu/tree.c +0 -0
index 50af465729f4..e3381ff67fc6 100644
Quoted 8 lines

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

Quoted 6 lines
kernel/rcu/tree.c +1 -0
@@ -1492,6 +1492,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_list(&rdp->cblist, "SegCbPreAcc");

Use TPS("SegCbPreAcc") ?

Fixed, thanks!

thanks,

- Joel

Quoted 38 lines

Thanks
Neeraj

Quoted 29 lines
kernel/rcu/tree.c +8 -0
+
  	/*
  	 * Callbacks are often registered with incomplete grace-period
  	 * information.  Something about the fact that getting exact
@@ -1512,6 +1514,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_list(&rdp->cblist, "SegCbPostAcc");
+
  	return ret;
  }
@@ -2469,6 +2473,9 @@ static void rcu_do_batch(struct rcu_data *rdp)
  	/* Invoke callbacks. */
  	tick_dep_set_task(current, TICK_DEP_BIT_RCU);
  	rhp = rcu_cblist_dequeue(&rcl);
+
+	trace_rcu_segcb_list(&rdp->cblist, "SegCbDequeued");
+
  	for (; rhp; rhp = rcu_cblist_dequeue(&rcl)) {
  		rcu_callback_t f;
@@ -2982,6 +2989,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_list(&rdp->cblist, "SegCBQueued");
+
  	/* Go handle any RCU core processing required. */
  	if (IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
  	    unlikely(rcu_segcblist_is_offloaded(&rdp->cblist))) {
 
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
#14 lore raw

PATCH v6 4/4 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 df0f31e30947..b65ac8c85b56 100644
@@ -10,7 +10,6 @@
 #include <linux/types.h>
 #include <linux/kernel.h>
 #include <linux/interrupt.h>
-#include <linux/rcupdate.h>
 
 #include "rcu_segcblist.h"
 #include "rcu.h"
Signature · 1 line
2.28.0.681.g6f77f65b4e-goog
maintainer
#15 lore raw
In reply to Joel Fernandes (Google)
Quoted 15 lines · Joel Fernandes (Google)

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:
v6: Fixed TREE04, and restored older logic to ensure rcu_barrier works.

Quoted 6 lines

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/[email protected]/)

Looking much improved, thank you!

I have placed these on branch rcu/test in -rcu for testing and inspection. I had to apply them at b94e6291a208 ("torture: Force weak-hashed pointers on console log") and cherry-pick them onto the "dev" branch, but it looks like things worked nicely.

Thanx, Paul

Quoted 16 lines

Joel Fernandes (Google) (4):
rcu/tree: Make rcu_do_batch count how many callbacks were executed rcu/segcblist: Add counters to segcblist datastructure rcu/trace: Add tracing for how segcb list changes rcu/segcblist: Remove useless rcupdate.h include

include/linux/rcu_segcblist.h |   2 +
include/trace/events/rcu.h    |  25 ++++++
kernel/rcu/rcu_segcblist.c    | 161 +++++++++++++++++++++++++---------
kernel/rcu/rcu_segcblist.h    |   8 +-
kernel/rcu/tree.c             |  18 ++--
5 files changed, 165 insertions(+), 49 deletions(-)

--
2.28.0.681.g6f77f65b4e-goog