Skip to contentSkip to conversation

rcu/nocb: De-offload and re-offload support v4

PATCH In review Frederic Weisbecker 4 participants 28 messages Nov 13, 2020 – Dec 29, 2020

Subsystems (6)
lore
Share & tools
lore

Frederic Weisbecker

author maintainer
#0 lore raw

cover rcu/nocb: De-offload and re-offload support v4

This keeps growing up. Rest assured, most of it is debug code and sanity checks.

Boqun Feng found that holding rnp lock while updating the offloaded state of an rdp isn't needed, and he was right despite my initial reaction. The sites that read the offloaded state while holding the rnp lock are actually protected because they read it locally in a non preemptible context.

So I removed the rnp lock in "rcu/nocb: De-offloading CB". And just to make sure I'm not missing something, I added sanity checks that ensure we always read the offloaded state in a safe way (3 last patches).

Still passes TREE01 (but I had to fight!)

git://git.kernel.org/pub/scm/linux/kernel/git/frederic/linux-dynticks.git
rcu/nocb-toggle-v4

HEAD: 579e15efa48fb6fc4ecf14961804051f385807fe

Thanks,
Frederic
---

Frederic Weisbecker (19):
rcu/nocb: Turn enabled/offload states into a common flag rcu/nocb: Provide basic callback offloading state machine bits rcu/nocb: Always init segcblist on CPU up
rcu/nocb: De-offloading CB kthread
rcu/nocb: Don't deoffload an offline CPU with pending work rcu/nocb: De-offloading GP kthread
rcu/nocb: Re-offload support
rcu/nocb: Shutdown nocb timer on de-offloading
rcu: Flush bypass before setting SEGCBLIST_SOFTIRQ_ONLY rcu/nocb: Set SEGCBLIST_SOFTIRQ_ONLY at the very last stage of de-offloading rcu/nocb: Only cond_resched() from actual offloaded batch processing rcu/nocb: Process batch locally as long as offloading isn't complete rcu/nocb: Locally accelerate callbacks as long as offloading isn't complete tools/rcutorture: Support nocb toggle in TREE01
rcutorture: Remove weak nocb declarations
rcutorture: Export nocb (de)offloading functions
cpu/hotplug: Add lockdep_is_cpus_held()
timer: Add timer_curr_running()
rcu/nocb: Detect unsafe checks for offloaded rdp

 include/linux/cpu.h                                |   1 +
 include/linux/rcu_segcblist.h                      | 119 +++++-
 include/linux/rcupdate.h                           |   4 +
 include/linux/timer.h                              |   2 +
 kernel/cpu.c                                       |   7 +
 kernel/rcu/rcu_segcblist.c                         |  13 +-
 kernel/rcu/rcu_segcblist.h                         |  45 ++-
 kernel/rcu/rcutorture.c                            |   3 -
 kernel/rcu/tree.c                                  |  49 ++-
 kernel/rcu/tree.h                                  |   2 +
 kernel/rcu/tree_plugin.h                           | 416 +++++++++++++++++++--
 kernel/time/timer.c                                |  13 +
 .../selftests/rcutorture/configs/rcu/TREE01.boot   |   4 +-
 13 files changed, 614 insertions(+), 64 deletions(-)
author maintainer
#1 lore raw

PATCH 1/19 rcu/nocb: Turn enabled/offload states into a common flag

Gather the segcblist properties in a common map to avoid spreading booleans in the structure. And this prepares for the offloaded state to be mutable on runtime.

Cc Paul E. McKenney <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Neeraj Upadhyay <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Boqun Feng <[email protected]>
---
 include/linux/rcu_segcblist.h |  6 ++++--
 kernel/rcu/rcu_segcblist.c    |  6 +++---
 kernel/rcu/rcu_segcblist.h    | 23 +++++++++++++++++++++--
 3 files changed, 28 insertions(+), 7 deletions(-)
include/linux/rcu_segcblist.h +4 -2
index 6c01f09a6456..4714b0263c76 100644
@@ -63,6 +63,9 @@ struct rcu_cblist {
 #define RCU_NEXT_TAIL		3
 #define RCU_CBLIST_NSEGS	4
 
+#define SEGCBLIST_ENABLED	BIT(0)
+#define SEGCBLIST_OFFLOADED	BIT(1)
+
 struct rcu_segcblist {
 	struct rcu_head *head;
 	struct rcu_head **tails[RCU_CBLIST_NSEGS];
@@ -73,8 +76,7 @@ struct rcu_segcblist {
 	long len;
 #endif
 	long seglen[RCU_CBLIST_NSEGS];
-	u8 enabled;
-	u8 offloaded;
+	u8 flags;
 };
 
 #define RCU_SEGCBLIST_INITIALIZER(n) \
kernel/rcu/rcu_segcblist.c +3 -3
index 5059b6102afe..e374f9c3ec2c 100644
@@ -234,7 +234,7 @@ void rcu_segcblist_init(struct rcu_segcblist *rsclp)
 		rcu_segcblist_set_seglen(rsclp, i, 0);
 	}
 	rcu_segcblist_set_len(rsclp, 0);
-	rsclp->enabled = 1;
+	rcu_segcblist_set_flags(rsclp, SEGCBLIST_ENABLED);
 }
 
 /*
@@ -245,7 +245,7 @@ void rcu_segcblist_disable(struct rcu_segcblist *rsclp)
 {
 	WARN_ON_ONCE(!rcu_segcblist_empty(rsclp));
 	WARN_ON_ONCE(rcu_segcblist_n_cbs(rsclp));
-	rsclp->enabled = 0;
+	rcu_segcblist_clear_flags(rsclp, SEGCBLIST_ENABLED);
 }
 
 /*
@@ -254,7 +254,7 @@ void rcu_segcblist_disable(struct rcu_segcblist *rsclp)
  */
 void rcu_segcblist_offload(struct rcu_segcblist *rsclp)
 {
-	rsclp->offloaded = 1;
+	rcu_segcblist_set_flags(rsclp, SEGCBLIST_OFFLOADED);
 }
 
 /*
kernel/rcu/rcu_segcblist.h +21 -2
index cd35c9faaf51..3d1195dbf030 100644
@@ -50,19 +50,38 @@ static inline long rcu_segcblist_n_cbs(struct rcu_segcblist *rsclp)
 #endif
 }
 
+static inline void rcu_segcblist_set_flags(struct rcu_segcblist *rsclp,
+					   int flags)
+{
+	rsclp->flags |= flags;
+}
+
+static inline void rcu_segcblist_clear_flags(struct rcu_segcblist *rsclp,
+					     int flags)
+{
+	rsclp->flags &= ~flags;
+}
+
+static inline bool rcu_segcblist_test_flags(struct rcu_segcblist *rsclp,
+					    int flags)
+{
+	return READ_ONCE(rsclp->flags) & flags;
+}
+
 /*
  * Is the specified rcu_segcblist enabled, for example, not corresponding
  * to an offline CPU?
  */
 static inline bool rcu_segcblist_is_enabled(struct rcu_segcblist *rsclp)
 {
-	return rsclp->enabled;
+	return rcu_segcblist_test_flags(rsclp, SEGCBLIST_ENABLED);
 }
 
 /* Is the specified rcu_segcblist offloaded?  */
 static inline bool rcu_segcblist_is_offloaded(struct rcu_segcblist *rsclp)
 {
-	return IS_ENABLED(CONFIG_RCU_NOCB_CPU) && rsclp->offloaded;
+	return IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
+		rcu_segcblist_test_flags(rsclp, SEGCBLIST_OFFLOADED);
 }
 
 /*
Signature · 1 line
2.25.1
author maintainer
#2 lore raw

PATCH 2/19 rcu/nocb: Provide basic callback offloading state machine bits

In reply to Frederic Weisbecker

We'll need to be able to runtime offload and de-offload the processing of callback for a given CPU. In order to support a smooth transition from unlocked local processing (softirq/rcuc) to locked offloaded processing (rcuop/rcuog) and the reverse, provide the necessary bits and documentation for the state machine that will carry up all the steps to enforce correctness while serving callbacks processing all along.

Cc Paul E. McKenney <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Neeraj Upadhyay <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Boqun Feng <[email protected]>
---
 include/linux/rcu_segcblist.h | 115 +++++++++++++++++++++++++++++++++-
 kernel/rcu/rcu_segcblist.c    |   1 +
 kernel/rcu/rcu_segcblist.h    |  12 +++-
 kernel/rcu/tree.c             |   3 +
 4 files changed, 128 insertions(+), 3 deletions(-)
include/linux/rcu_segcblist.h +114 -1
index 4714b0263c76..4ca08b089faf 100644
@@ -63,8 +63,121 @@ struct rcu_cblist {
 #define RCU_NEXT_TAIL		3
 #define RCU_CBLIST_NSEGS	4
 
+
+/*
+ *                     ==NOCB Offloading state machine==
+ *
+ *
+ *  ----------------------------------------------------------------------------
+ *  |                         SEGCBLIST_SOFTIRQ_ONLY                           |
+ *  |                                                                          |
+ *  |  Callbacks processed by rcu_core() from softirqs or local                |
+ *  |  rcuc kthread, without holding nocb_lock.                                |
+ *  ----------------------------------------------------------------------------
+ *                                         |
+ *                                         v
+ *  ----------------------------------------------------------------------------
+ *  |                        SEGCBLIST_OFFLOADED                               |
+ *  |                                                                          |
+ *  | Callbacks processed by rcu_core() from softirqs or local                 |
+ *  | rcuc kthread, while holding nocb_lock. Waking up CB and GP kthreads,     |
+ *  | allowing nocb_timer to be armed.                                        |
+ *  ----------------------------------------------------------------------------
+ *                                         |
+ *                                         v
+ *                        -----------------------------------
+ *                        |                                 |
+ *                        v                                 v
+ *  ---------------------------------------  ----------------------------------|
+ *  |        SEGCBLIST_OFFLOADED |        |  |     SEGCBLIST_OFFLOADED |       |
+ *  |        SEGCBLIST_KTHREAD_CB         |  |     SEGCBLIST_KTHREAD_GP        |
+ *  |                                     |  |                                 |
+ *  |                                     |  |                                 |
+ *  | CB kthread woke up and              |  | GP kthread woke up and          |
+ *  | acknowledged SEGCBLIST_OFFLOADED.   |  | acknowledged SEGCBLIST_OFFLOADED|
+ *  | Processes callbacks concurrently    |  |                                 |
+ *  | with rcu_core(), holding            |  |                                 |
+ *  | nocb_lock.                          |  |                                 |
+ *  ---------------------------------------  -----------------------------------
+ *                        |                                 |
+ *                        -----------------------------------
+ *                                         |
+ *                                         v
+ *  |--------------------------------------------------------------------------|
+ *  |                           SEGCBLIST_OFFLOADED |                          |
+ *  |                           SEGCBLIST_KTHREAD_CB |                         |
+ *  |                           SEGCBLIST_KTHREAD_GP                           |
+ *  |                                                                          |
+ *  |   Kthreads handle callbacks holding nocb_lock, local rcu_core() stops    |
+ *  |   handling callbacks.                                                    |
+ *  ----------------------------------------------------------------------------
+ */
+
+
+
+/*
+ *                       ==NOCB De-Offloading state machine==
+ *
+ *
+ *  |--------------------------------------------------------------------------|
+ *  |                           SEGCBLIST_OFFLOADED |                          |
+ *  |                           SEGCBLIST_KTHREAD_CB |                         |
+ *  |                           SEGCBLIST_KTHREAD_GP                           |
+ *  |                                                                          |
+ *  |   CB/GP kthreads handle callbacks holding nocb_lock, local rcu_core()    |
+ *  |   ignores callbacks.                                                     |
+ *  ----------------------------------------------------------------------------
+ *                                      |
+ *                                      v
+ *  |--------------------------------------------------------------------------|
+ *  |                           SEGCBLIST_KTHREAD_CB |                         |
+ *  |                           SEGCBLIST_KTHREAD_GP                           |
+ *  |                                                                          |
+ *  |   CB/GP kthreads and local rcu_core() handle callbacks concurrently      |
+ *  |   holding nocb_lock. Wake up CB and GP kthreads if necessary.            |
+ *  ----------------------------------------------------------------------------
+ *                                      |
+ *                                      v
+ *                     -----------------------------------
+ *                     |                                 |
+ *                     v                                 v
+ *  ---------------------------------------------------------------------------|
+ *  |                                                                          |
+ *  |        SEGCBLIST_KTHREAD_CB         |       SEGCBLIST_KTHREAD_GP         |
+ *  |                                     |                                    |
+ *  | GP kthread woke up and              |   CB kthread woke up and           |
+ *  | acknowledged the fact that          |   acknowledged the fact that       |
+ *  | SEGCBLIST_OFFLOADED got cleared.    |   SEGCBLIST_OFFLOADED got cleared. |
+ *  |                                     |   The CB kthread goes to sleep     |
+ *  | The callbacks from the target CPU   |   until it ever gets re-offloaded. |
+ *  | will be ignored from the GP kthread |                                    |
+ *  | loop.                               |                                    |
+ *  ----------------------------------------------------------------------------
+ *                      |                                 |
+ *                      -----------------------------------
+ *                                      |
+ *                                      v
+ *  ----------------------------------------------------------------------------
+ *  |                                   0                                      |
+ *  |                                                                          |
+ *  | Callbacks processed by rcu_core() from softirqs or local                 |
+ *  | rcuc kthread, while holding nocb_lock. Forbid nocb_timer to be armed.    |
+ *  | Flush pending nocb_timer. Flush nocb bypass callbacks.                   |
+ *  ----------------------------------------------------------------------------
+ *                                      |
+ *                                      v
+ *  ----------------------------------------------------------------------------
+ *  |                         SEGCBLIST_SOFTIRQ_ONLY                           |
+ *  |                                                                          |
+ *  |  Callbacks processed by rcu_core() from softirqs or local                |
+ *  |  rcuc kthread, without holding nocb_lock.                                |
+ *  ----------------------------------------------------------------------------
+ */
 #define SEGCBLIST_ENABLED	BIT(0)
-#define SEGCBLIST_OFFLOADED	BIT(1)
+#define SEGCBLIST_SOFTIRQ_ONLY	BIT(1)
+#define SEGCBLIST_KTHREAD_CB	BIT(2)
+#define SEGCBLIST_KTHREAD_GP	BIT(3)
+#define SEGCBLIST_OFFLOADED	BIT(4)
 
 struct rcu_segcblist {
 	struct rcu_head *head;
kernel/rcu/rcu_segcblist.c +1 -0
index e374f9c3ec2c..d7cb9dc044ea 100644
@@ -254,6 +254,7 @@ void rcu_segcblist_disable(struct rcu_segcblist *rsclp)
  */
 void rcu_segcblist_offload(struct rcu_segcblist *rsclp)
 {
+	rcu_segcblist_clear_flags(rsclp, SEGCBLIST_SOFTIRQ_ONLY);
 	rcu_segcblist_set_flags(rsclp, SEGCBLIST_OFFLOADED);
 }
 
kernel/rcu/rcu_segcblist.h +10 -2
index 3d1195dbf030..70e108c0e7c9 100644
@@ -80,8 +80,16 @@ static inline bool rcu_segcblist_is_enabled(struct rcu_segcblist *rsclp)
 /* Is the specified rcu_segcblist offloaded?  */
 static inline bool rcu_segcblist_is_offloaded(struct rcu_segcblist *rsclp)
 {
-	return IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
-		rcu_segcblist_test_flags(rsclp, SEGCBLIST_OFFLOADED);
+	if (IS_ENABLED(CONFIG_RCU_NOCB_CPU)) {
+		/*
+		 * Complete de-offloading happens only when SEGCBLIST_SOFTIRQ_ONLY
+		 * is set.
+		 */
+		if (!rcu_segcblist_test_flags(rsclp, SEGCBLIST_SOFTIRQ_ONLY))
+			return true;
+	}
+
+	return false;
 }
 
 /*
kernel/rcu/tree.c +3 -0
index 413831b48648..48e8e63cdeb2 100644
@@ -83,6 +83,9 @@ static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, rcu_data) = {
 	.dynticks_nesting = 1,
 	.dynticks_nmi_nesting = DYNTICK_IRQ_NONIDLE,
 	.dynticks = ATOMIC_INIT(RCU_DYNTICK_CTRL_CTR),
+#ifdef CONFIG_RCU_NOCB_CPU
+	.cblist.flags = SEGCBLIST_SOFTIRQ_ONLY,
+#endif
 };
 static struct rcu_state rcu_state = {
 	.level = { &rcu_state.node[0] },
Signature · 1 line
2.25.1
author maintainer
#3 lore raw

PATCH 3/19 rcu/nocb: Always init segcblist on CPU up

In reply to Frederic Weisbecker

An rdp's segcblist enabled state is treated differently on CPU hotplug operations, depending on whether it is offloaded or not.

1) Not offloaded: An rdp is disabled on CPU down. All its callbacks are migrated and no more aren't supposed to be enqueued until it gets re-enabled on CPU up.

2) Offloaded: An rdp is not disabled on CPU down in order to let the CB/GP kthreads finish their jobs on remaining callbacks. Hence it is not re-enabled on CPU up either.

Since an rdp's offloaded state is set in stone at boot, we expect the offloaded state to remain the same between CPU down and CPU up. So 1) and 2) are symmetrical.

Now the offloaded state will become toggable at runtime. Hence the new possible asymmetrical scenarios:

3) An rdp goes into CPU down while in a not-offloaded state. It gets later set to offloaded and finally goes into CPU up.

4) An rdp goes into CPU down while in an offloaded state. It gets later set to not-offloaded and finally goes into CPU up.

The scenario 4) is currently well handled. The rdp isn't disabled on CPU down and it gets re-initialized on CPU up. We require the segcblist to be empty in order to toggle to non-offloaded state while a CPU is offlined.

The scenario 3) would run into trouble though, as the rdp is disabled on CPU down and not re-initialized/re-enabled on CPU up.

In order to fix this, always re-initialize/re-enable an rdp on CPU up unless it still has callbacks at that time, which anyway can only happen when the rdp went down and up in offloaded state (case 2), the only case that doesn't need re-initialization.

NOTE: The proper longer term fix will be to wait for all the offloaded callbacks to be processed before completing CPU down operations. So we can unconditionally re-initialize on CPU up.

Cc Paul E. McKenney <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Neeraj Upadhyay <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Boqun Feng <[email protected]>
---
 kernel/rcu/tree.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
kernel/rcu/tree.c +9 -3
index 48e8e63cdeb2..049433d0fa05 100644
@@ -4004,12 +4004,18 @@ int rcutree_prepare_cpu(unsigned int cpu)
 	rdp->qlen_last_fqs_check = 0;
 	rdp->n_force_qs_snap = rcu_state.n_force_qs;
 	rdp->blimit = blimit;
-	if (rcu_segcblist_empty(&rdp->cblist) && /* No early-boot CBs? */
-	    !rcu_segcblist_is_offloaded(&rdp->cblist))
-		rcu_segcblist_init(&rdp->cblist);  /* Re-enable callbacks. */
 	rdp->dynticks_nesting = 1;	/* CPU not up, no tearing. */
 	rcu_dynticks_eqs_online();
 	raw_spin_unlock_rcu_node(rnp);		/* irqs remain disabled. */
+	/*
+	 * Lock in case the CB/GP kthreads are still around handling
+	 * old callbacks (longer term we should flush all callbacks
+	 * before completing CPU offline)
+	 */
+	rcu_nocb_lock(rdp);
+	if (rcu_segcblist_empty(&rdp->cblist)) /* No early-boot CBs? */
+		rcu_segcblist_init(&rdp->cblist);  /* Re-enable callbacks. */
+	rcu_nocb_unlock(rdp);
 
 	/*
 	 * Add CPU to leaf rcu_node pending-online bitmask.  Any needed
Signature · 1 line
2.25.1
author maintainer
#4 lore raw

PATCH 4/19 rcu/nocb: De-offloading CB kthread

In reply to Frederic Weisbecker

In order to de-offload the callbacks processing of an rdp, we must clear SEGCBLIST_OFFLOAD and notify the CB kthread so that it clears its own bit flag and goes to sleep to stop handling callbacks. The GP kthread will also be notified the same way. Whoever acknowledges and clears its own bit last must notify the de-offloading worker so that it can resume the de-offloading while being sure that callbacks won't be handled remotely anymore.

Cc Paul E. McKenney <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Neeraj Upadhyay <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Boqun Feng <[email protected]>
---
 include/linux/rcupdate.h   |   2 +
 kernel/rcu/rcu_segcblist.c |  10 ++-
 kernel/rcu/rcu_segcblist.h |   2 +-
 kernel/rcu/tree.h          |   1 +
 kernel/rcu/tree_plugin.h   | 131 +++++++++++++++++++++++++++++++------
 5 files changed, 123 insertions(+), 23 deletions(-)
include/linux/rcupdate.h +2 -0
index de0826411311..40266eb418b6 100644
@@ -104,8 +104,10 @@ static inline void rcu_user_exit(void) { }
 
 #ifdef CONFIG_RCU_NOCB_CPU
 void rcu_init_nohz(void);
+int rcu_nocb_cpu_deoffload(int cpu);
 #else /* #ifdef CONFIG_RCU_NOCB_CPU */
 static inline void rcu_init_nohz(void) { }
+static inline int rcu_nocb_cpu_deoffload(int cpu) { return 0; }
 #endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */
 
 /**
kernel/rcu/rcu_segcblist.c +7 -3
index d7cb9dc044ea..ccfa09cb2431 100644
@@ -252,10 +252,14 @@ void rcu_segcblist_disable(struct rcu_segcblist *rsclp)
  * Mark the specified rcu_segcblist structure as offloaded.  This
  * structure must be empty.
  */
-void rcu_segcblist_offload(struct rcu_segcblist *rsclp)
+void rcu_segcblist_offload(struct rcu_segcblist *rsclp, bool offload)
 {
-	rcu_segcblist_clear_flags(rsclp, SEGCBLIST_SOFTIRQ_ONLY);
-	rcu_segcblist_set_flags(rsclp, SEGCBLIST_OFFLOADED);
+	if (offload) {
+		rcu_segcblist_clear_flags(rsclp, SEGCBLIST_SOFTIRQ_ONLY);
+		rcu_segcblist_set_flags(rsclp, SEGCBLIST_OFFLOADED);
+	} else {
+		rcu_segcblist_clear_flags(rsclp, SEGCBLIST_OFFLOADED);
+	}
 }
 
 /*
kernel/rcu/rcu_segcblist.h +1 -1
index 70e108c0e7c9..f42f7a13faa0 100644
@@ -106,7 +106,7 @@ 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);
+void rcu_segcblist_offload(struct rcu_segcblist *rsclp, bool offload);
 bool rcu_segcblist_ready_cbs(struct rcu_segcblist *rsclp);
 bool rcu_segcblist_pend_cbs(struct rcu_segcblist *rsclp);
 struct rcu_head *rcu_segcblist_first_cb(struct rcu_segcblist *rsclp);
kernel/rcu/tree.h +1 -0
index 7708ed161f4a..e0deb4829847 100644
@@ -201,6 +201,7 @@ struct rcu_data {
 	/* 5) Callback offloading. */
 #ifdef CONFIG_RCU_NOCB_CPU
 	struct swait_queue_head nocb_cb_wq; /* For nocb kthreads to sleep on. */
+	struct swait_queue_head nocb_state_wq; /* For offloading state changes */
 	struct task_struct *nocb_gp_kthread;
 	raw_spinlock_t nocb_lock;	/* Guard following pair of fields. */
 	atomic_t nocb_lock_contended;	/* Contention experienced. */
kernel/rcu/tree_plugin.h +112 -19

179 lines not on this page · raw message

Signature · 1 line
2.25.1
author maintainer
#5 lore raw

PATCH 5/19 rcu/nocb: Don't deoffload an offline CPU with pending work

In reply to Frederic Weisbecker

Offlining offloaded CPUs don't migrate their callbacks just like non-offloaded CPUs do. It's up to their CB/GP kthread to handle what remains.

Therefore we can't afford to de-offload an offline CPU that still has pending work to do, or the callbacks would be ignored.

NOTE: The long term solution will be to wait for all pending callbacks to be processed before completing a CPU down operation.

Cc Paul E. McKenney <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Neeraj Upadhyay <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Boqun Feng <[email protected]>
---
 kernel/rcu/tree_plugin.h | 9 +++++++++
 1 file changed, 9 insertions(+)
kernel/rcu/tree_plugin.h +9 -0
index 08eb035da9e7..5075bf219b23 100644
@@ -2227,6 +2227,15 @@ static int __rcu_nocb_rdp_deoffload(struct rcu_data *rdp)
 	printk("De-offloading %d\n", rdp->cpu);
 
 	rcu_nocb_lock_irqsave(rdp, flags);
+	/*
+	 * If there are still pending work offloaded, the offline
+	 * CPU won't help much handling them.
+	 */
+	if (cpu_is_offline(rdp->cpu) && !rcu_segcblist_empty(&rdp->cblist)) {
+		rcu_nocb_unlock_irqrestore(rdp, flags);
+		return -EBUSY;
+	}
+
 	rcu_segcblist_offload(cblist, false);
 
 	if (rdp->nocb_cb_sleep) {
Signature · 1 line
2.25.1
author maintainer
#6 lore raw

PATCH 6/19 rcu/nocb: De-offloading GP kthread

In reply to Frederic Weisbecker

In order to de-offload the callbacks processing of an rdp, we must clear SEGCBLIST_OFFLOAD and notify the GP kthread so that it clears its own bit flag and ignore the target rdp from its loop. The CB kthread is also notified the same way. Whoever acknowledges and clears its own bit last must notify the de-offloading worker so that it can resume the de-offloading while being sure that callbacks won't be handled remotely anymore.

Cc Paul E. McKenney <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Neeraj Upadhyay <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Boqun Feng <[email protected]>
---
 kernel/rcu/tree_plugin.h | 54 +++++++++++++++++++++++++++++++++++++---
 1 file changed, 51 insertions(+), 3 deletions(-)
kernel/rcu/tree_plugin.h +51 -3
index 5075bf219b23..10e7a8962a47 100644
@@ -1928,6 +1928,33 @@ static void do_nocb_bypass_wakeup_timer(struct timer_list *t)
 	__call_rcu_nocb_wake(rdp, true, flags);
 }
 
+static inline bool nocb_gp_enabled_cb(struct rcu_data *rdp)
+{
+	u8 flags = SEGCBLIST_OFFLOADED | SEGCBLIST_KTHREAD_GP;
+
+	return rcu_segcblist_test_flags(&rdp->cblist, flags);
+}
+
+static inline bool nocb_gp_update_state(struct rcu_data *rdp, bool *needwake_state)
+{
+	struct rcu_segcblist *cblist = &rdp->cblist;
+
+	if (rcu_segcblist_test_flags(cblist, SEGCBLIST_OFFLOADED)) {
+		return true;
+	} else {
+		/*
+		 * De-offloading. Clear our flag and notify the de-offload worker.
+		 * We will ignore this rdp until it ever gets re-offloaded.
+		 */
+		WARN_ON_ONCE(!rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_GP));
+		rcu_segcblist_clear_flags(cblist, SEGCBLIST_KTHREAD_GP);
+		if (!rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB))
+			*needwake_state = true;
+		return false;
+	}
+}
+
+
 /*
  * No-CBs GP kthreads come here to wait for additional callbacks to show up
  * or for grace periods to end.
@@ -1956,8 +1983,17 @@ static void nocb_gp_wait(struct rcu_data *my_rdp)
 	 */
 	WARN_ON_ONCE(my_rdp->nocb_gp_rdp != my_rdp);
 	for (rdp = my_rdp; rdp; rdp = rdp->nocb_next_cb_rdp) {
+		bool needwake_state = false;
+		if (!nocb_gp_enabled_cb(rdp))
+			continue;
 		trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("Check"));
 		rcu_nocb_lock_irqsave(rdp, flags);
+		if (!nocb_gp_update_state(rdp, &needwake_state)) {
+			rcu_nocb_unlock_irqrestore(rdp, flags);
+			if (needwake_state)
+				swake_up_one(&rdp->nocb_state_wq);
+			continue;
+		}
 		bypass_ncbs = rcu_cblist_n_cbs(&rdp->nocb_bypass);
 		if (bypass_ncbs &&
 		    (time_after(j, READ_ONCE(rdp->nocb_bypass_first) + 1) ||
@@ -2221,7 +2257,8 @@ static void do_nocb_deferred_wakeup(struct rcu_data *rdp)
 static int __rcu_nocb_rdp_deoffload(struct rcu_data *rdp)
 {
 	struct rcu_segcblist *cblist = &rdp->cblist;
-	bool wake_cb = false;
+	struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
+	bool wake_cb = false, wake_gp = false;
 	unsigned long flags;
 
 	printk("De-offloading %d\n", rdp->cpu);
@@ -2247,9 +2284,19 @@ static int __rcu_nocb_rdp_deoffload(struct rcu_data *rdp)
 	if (wake_cb)
 		swake_up_one(&rdp->nocb_cb_wq);
 
+	raw_spin_lock_irqsave(&rdp_gp->nocb_gp_lock, flags);
+	if (rdp_gp->nocb_gp_sleep) {
+		rdp_gp->nocb_gp_sleep = false;
+		wake_gp = true;
+	}
+	raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
+
+	if (wake_gp)
+		wake_up_process(rdp_gp->nocb_gp_kthread);
+
 	swait_event_exclusive(rdp->nocb_state_wq,
-			      !rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB));
-
+			      !rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB |
+							SEGCBLIST_KTHREAD_GP));
 	return 0;
 }
 
@@ -2331,6 +2378,7 @@ void __init rcu_init_nohz(void)
 			rcu_segcblist_init(&rdp->cblist);
 		rcu_segcblist_offload(&rdp->cblist, true);
 		rcu_segcblist_set_flags(&rdp->cblist, SEGCBLIST_KTHREAD_CB);
+		rcu_segcblist_set_flags(&rdp->cblist, SEGCBLIST_KTHREAD_GP);
 	}
 	rcu_organize_nocb_kthreads();
 }
Signature · 1 line
2.25.1
author maintainer
#7 lore raw

PATCH 7/19 rcu/nocb: Re-offload support

In reply to Frederic Weisbecker

In order to re-offload the callbacks processing of an rdp, we must clear SEGCBLIST_SOFTIRQ_ONLY, set SEGCBLIST_OFFLOADED and notify the CB and GP kthreads so that they both set their own bit flag and start processing the callbacks remotely. The re-offloading worker is then notified that it can stop processing the callbacks locally.

Ordering must be carefully enforced so that the callbacks that used to be processed locally without locking must have their latest updates visible by the time they get processed by the kthreads.

Cc Paul E. McKenney <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Neeraj Upadhyay <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Boqun Feng <[email protected]>
---
 include/linux/rcupdate.h |   2 +
 kernel/rcu/tree_plugin.h | 162 +++++++++++++++++++++++++++++++++------
 2 files changed, 140 insertions(+), 24 deletions(-)
include/linux/rcupdate.h +2 -0
index 40266eb418b6..e0ee52e2756d 100644
@@ -104,9 +104,11 @@ static inline void rcu_user_exit(void) { }
 
 #ifdef CONFIG_RCU_NOCB_CPU
 void rcu_init_nohz(void);
+int rcu_nocb_cpu_offload(int cpu);
 int rcu_nocb_cpu_deoffload(int cpu);
 #else /* #ifdef CONFIG_RCU_NOCB_CPU */
 static inline void rcu_init_nohz(void) { }
+static inline int rcu_nocb_cpu_offload(int cpu) { return -EINVAL; }
 static inline int rcu_nocb_cpu_deoffload(int cpu) { return 0; }
 #endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */
 
kernel/rcu/tree_plugin.h +138 -24

217 lines not on this page · raw message

Signature · 1 line
2.25.1
author maintainer
#8 lore raw

PATCH 8/19 rcu/nocb: Shutdown nocb timer on de-offloading

In reply to Frederic Weisbecker

Make sure the nocb timer can't fire anymore before we reach the final de-offload state. Spuriously waking up the GP kthread is no big deal but we must prevent from executing the timer callback without nocb locking.

Cc Paul E. McKenney <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Neeraj Upadhyay <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Boqun Feng <[email protected]>
---
 kernel/rcu/tree.h        |  1 +
 kernel/rcu/tree_plugin.h | 12 +++++++++++-
 2 files changed, 12 insertions(+), 1 deletion(-)
kernel/rcu/tree.h +1 -0
index e0deb4829847..5d359b9f9fec 100644
@@ -257,6 +257,7 @@ struct rcu_data {
 };
 
 /* Values for nocb_defer_wakeup field in struct rcu_data. */
+#define RCU_NOCB_WAKE_OFF	-1
 #define RCU_NOCB_WAKE_NOT	0
 #define RCU_NOCB_WAKE		1
 #define RCU_NOCB_WAKE_FORCE	2
kernel/rcu/tree_plugin.h +11 -1
index 3b4f01a111d8..44b4ab9b3953 100644
@@ -1665,6 +1665,8 @@ static void wake_nocb_gp(struct rcu_data *rdp, bool force,
 static void wake_nocb_gp_defer(struct rcu_data *rdp, int waketype,
 			       const char *reason)
 {
+	if (rdp->nocb_defer_wakeup == RCU_NOCB_WAKE_OFF)
+		return;
 	if (rdp->nocb_defer_wakeup == RCU_NOCB_WAKE_NOT)
 		mod_timer(&rdp->nocb_timer, jiffies + 1);
 	if (rdp->nocb_defer_wakeup < waketype)
@@ -2243,7 +2245,7 @@ static int rcu_nocb_cb_kthread(void *arg)
 /* Is a deferred wakeup of rcu_nocb_kthread() required? */
 static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp)
 {
-	return READ_ONCE(rdp->nocb_defer_wakeup);
+	return READ_ONCE(rdp->nocb_defer_wakeup) > RCU_NOCB_WAKE_NOT;
 }
 
 /* Do a deferred wakeup of rcu_nocb_kthread(). */
@@ -2337,6 +2339,12 @@ static int __rcu_nocb_rdp_deoffload(struct rcu_data *rdp)
 	swait_event_exclusive(rdp->nocb_state_wq,
 			      !rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB |
 							SEGCBLIST_KTHREAD_GP));
+	/* Make sure nocb timer won't stay around */
+	rcu_nocb_lock_irqsave(rdp, flags);
+	WRITE_ONCE(rdp->nocb_defer_wakeup, RCU_NOCB_WAKE_OFF);
+	rcu_nocb_unlock_irqrestore(rdp, flags);
+	del_timer_sync(&rdp->nocb_timer);
+
 	return ret;
 }
 
@@ -2393,6 +2401,8 @@ static int __rcu_nocb_rdp_offload(struct rcu_data *rdp)
 	 * SEGCBLIST_SOFTIRQ_ONLY mode.
 	 */
 	raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
+	/* Re-enable nocb timer */
+	WRITE_ONCE(rdp->nocb_defer_wakeup, RCU_NOCB_WAKE_NOT);
 	/*
 	 * We didn't take the nocb lock while working on the
 	 * rdp->cblist in SEGCBLIST_SOFTIRQ_ONLY mode.
Signature · 1 line
2.25.1
author maintainer
#9 lore raw

PATCH 9/19 rcu: Flush bypass before setting SEGCBLIST_SOFTIRQ_ONLY

In reply to Frederic Weisbecker

Make sure to handle the pending bypass queue before we switch to the final de-offload state. We'll have to be careful and later set SEGCBLIST_SOFTIRQ_ONLY before re-enabling again IRQs, or new bypass callbacks could be queued in the meantine.

Cc Paul E. McKenney <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Neeraj Upadhyay <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Boqun Feng <[email protected]>
---
 kernel/rcu/tree_plugin.h | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
kernel/rcu/tree_plugin.h +10 -1
index 44b4ab9b3953..dfb4b62c6b88 100644
@@ -2339,12 +2339,21 @@ static int __rcu_nocb_rdp_deoffload(struct rcu_data *rdp)
 	swait_event_exclusive(rdp->nocb_state_wq,
 			      !rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB |
 							SEGCBLIST_KTHREAD_GP));
+	rcu_nocb_lock_irqsave(rdp, flags);
 	/* Make sure nocb timer won't stay around */
-	rcu_nocb_lock_irqsave(rdp, flags);
 	WRITE_ONCE(rdp->nocb_defer_wakeup, RCU_NOCB_WAKE_OFF);
 	rcu_nocb_unlock_irqrestore(rdp, flags);
 	del_timer_sync(&rdp->nocb_timer);
 
+	/*
+	 * Flush bypass. While IRQs are disabled and once we set
+	 * SEGCBLIST_SOFTIRQ_ONLY, no callback is supposed to be
+	 * enqueued on bypass.
+	 */
+	rcu_nocb_lock_irqsave(rdp, flags);
+	rcu_nocb_flush_bypass(rdp, NULL, jiffies);
+	rcu_nocb_unlock_irqrestore(rdp, flags);
+
 	return ret;
 }
 
Signature · 1 line
2.25.1
author maintainer
#10 lore raw

PATCH 10/19 rcu/nocb: Set SEGCBLIST_SOFTIRQ_ONLY at the very last stage of de-offloading

In reply to Frederic Weisbecker

Set SEGCBLIST_SOFTIRQ_ONLY once everything is settled. After that, the callbacks are handled locklessly and locally.

Cc Paul E. McKenney <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Neeraj Upadhyay <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Boqun Feng <[email protected]>
---
 kernel/rcu/tree_plugin.h | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
kernel/rcu/tree_plugin.h +8 -1
index dfb4b62c6b88..b68714e5ce64 100644
@@ -2352,7 +2352,14 @@ static int __rcu_nocb_rdp_deoffload(struct rcu_data *rdp)
 	 */
 	rcu_nocb_lock_irqsave(rdp, flags);
 	rcu_nocb_flush_bypass(rdp, NULL, jiffies);
-	rcu_nocb_unlock_irqrestore(rdp, flags);
+	rcu_segcblist_set_flags(cblist, SEGCBLIST_SOFTIRQ_ONLY);
+	/*
+	 * With SEGCBLIST_SOFTIRQ_ONLY, we can't use
+	 * rcu_nocb_unlock_irqrestore() anymore. Theoretically we
+	 * could set SEGCBLIST_SOFTIRQ_ONLY with cb unlocked and IRQs
+	 * disabled now, but let's be paranoid.
+	 */
+	raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
 
 	return ret;
 }
Signature · 1 line
2.25.1
author maintainer
#11 lore raw

PATCH 11/19 rcu/nocb: Only cond_resched() from actual offloaded batch processing

In reply to Frederic Weisbecker

rcu_do_batch() will be callable concurrently by softirqs and offloaded processing. So make sure we actually call cond resched only from the offloaded context.

Cc Paul E. McKenney <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Neeraj Upadhyay <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Boqun Feng <[email protected]>
---
 kernel/rcu/tree.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/rcu/tree.c +1 -2
index 049433d0fa05..da1ecf60c541 100644
@@ -2510,8 +2510,7 @@ static void rcu_do_batch(struct rcu_data *rdp)
 			/* Exceeded the time limit, so leave. */
 			break;
 		}
-		if (offloaded) {
-			WARN_ON_ONCE(in_serving_softirq());
+		if (!in_serving_softirq()) {
 			local_bh_enable();
 			lockdep_assert_irqs_enabled();
 			cond_resched_tasks_rcu_qs();
Signature · 1 line
2.25.1
author maintainer
#12 lore raw

PATCH 12/19 rcu/nocb: Process batch locally as long as offloading isn't complete

In reply to Frederic Weisbecker

During the offloading or de-offloading process, make sure to process the callbacks batch locally whenever the segcblist isn't entirely offloaded. This enforces callback service processing while we are still in intermediate (de-)offloading state.

CHECKME: Note that __call_rcu_core() isn't called during these intermediate states. Some pieces there may still be necessary.

Cc Paul E. McKenney <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Neeraj Upadhyay <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Boqun Feng <[email protected]>
---
 kernel/rcu/rcu_segcblist.h | 12 ++++++++++++
 kernel/rcu/tree.c          |  3 ++-
 2 files changed, 14 insertions(+), 1 deletion(-)
kernel/rcu/rcu_segcblist.h +12 -0
index f42f7a13faa0..c8f4d4ac650f 100644
@@ -92,6 +92,18 @@ static inline bool rcu_segcblist_is_offloaded(struct rcu_segcblist *rsclp)
 	return false;
 }
 
+static inline bool rcu_segcblist_completely_offloaded(struct rcu_segcblist *rsclp)
+{
+	int flags = SEGCBLIST_KTHREAD_CB | SEGCBLIST_KTHREAD_GP | SEGCBLIST_OFFLOADED;
+
+	if (IS_ENABLED(CONFIG_RCU_NOCB_CPU)) {
+		if ((rsclp->flags & flags) == flags)
+			return true;
+	}
+
+	return false;
+}
+
 /*
  * Are all segments following the specified segment of the specified
  * rcu_segcblist structure empty of callbacks?  (The specified
kernel/rcu/tree.c +2 -1
index da1ecf60c541..cc141337f3f9 100644
@@ -2691,6 +2691,7 @@ static __latent_entropy void rcu_core(void)
 	struct rcu_data *rdp = raw_cpu_ptr(&rcu_data);
 	struct rcu_node *rnp = rdp->mynode;
 	const bool offloaded = rcu_segcblist_is_offloaded(&rdp->cblist);
+	const bool do_batch = !rcu_segcblist_completely_offloaded(&rdp->cblist);
 
 	if (cpu_is_offline(smp_processor_id()))
 		return;
@@ -2720,7 +2721,7 @@ static __latent_entropy void rcu_core(void)
 	rcu_check_gp_start_stall(rnp, rdp, rcu_jiffies_till_stall_check());
 
 	/* If there are callbacks ready, invoke them. */
-	if (!offloaded && rcu_segcblist_ready_cbs(&rdp->cblist) &&
+	if (do_batch && rcu_segcblist_ready_cbs(&rdp->cblist) &&
 	    likely(READ_ONCE(rcu_scheduler_fully_active)))
 		rcu_do_batch(rdp);
 
Signature · 1 line
2.25.1
author maintainer
#13 lore raw

PATCH 13/19 rcu/nocb: Locally accelerate callbacks as long as offloading isn't complete

In reply to Frederic Weisbecker

The local callbacks processing checks if some callbacks need acceleration. Keep that behaviour under nocb lock protection when rcu_core() executes concurrently with GP/CB kthreads.

Cc Paul E. McKenney <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Neeraj Upadhyay <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Boqun Feng <[email protected]>
---
 kernel/rcu/tree.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
kernel/rcu/tree.c +3 -4
index cc141337f3f9..b24d60a52f34 100644
@@ -2690,7 +2690,6 @@ static __latent_entropy void rcu_core(void)
 	unsigned long flags;
 	struct rcu_data *rdp = raw_cpu_ptr(&rcu_data);
 	struct rcu_node *rnp = rdp->mynode;
-	const bool offloaded = rcu_segcblist_is_offloaded(&rdp->cblist);
 	const bool do_batch = !rcu_segcblist_completely_offloaded(&rdp->cblist);
 
 	if (cpu_is_offline(smp_processor_id()))
@@ -2711,11 +2710,11 @@ static __latent_entropy void rcu_core(void)
 
 	/* No grace period and unregistered callbacks? */
 	if (!rcu_gp_in_progress() &&
-	    rcu_segcblist_is_enabled(&rdp->cblist) && !offloaded) {
-		local_irq_save(flags);
+	    rcu_segcblist_is_enabled(&rdp->cblist) && do_batch) {
+		rcu_nocb_lock_irqsave(rdp, flags);
 		if (!rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL))
 			rcu_accelerate_cbs_unlocked(rnp, rdp);
-		local_irq_restore(flags);
+		rcu_nocb_unlock_irqrestore(rdp, flags);
 	}
 
 	rcu_check_gp_start_stall(rnp, rdp, rcu_jiffies_till_stall_check());
Signature · 1 line
2.25.1
author maintainer
#14 lore raw

PATCH 14/19 tools/rcutorture: Support nocb toggle in TREE01

In reply to Frederic Weisbecker

Add periodic toggling of 7 CPUs over 8 every second in order to test NOCB toggle code. Choose TREE01 for that as it's already testing nocb.

Cc Paul E. McKenney <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Neeraj Upadhyay <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Boqun Feng <[email protected]>
---
 tools/testing/selftests/rcutorture/configs/rcu/TREE01.boot | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/testing/selftests/rcutorture/configs/rcu/TREE01.boot +3 -1
index d6da9a61d44a..40af3df0f397 100644
@@ -2,5 +2,7 @@ maxcpus=8 nr_cpus=43
 rcutree.gp_preinit_delay=3
 rcutree.gp_init_delay=3
 rcutree.gp_cleanup_delay=3
-rcu_nocbs=0
+rcu_nocbs=0-1,3-7
+rcutorture.nocbs_nthreads=8
+rcutorture.nocbs_toggle=1000
 rcutorture.fwd_progress=0
Signature · 1 line
2.25.1
author maintainer
#15 lore raw

PATCH 15/19 rcutorture: Remove weak nocb declarations

In reply to Frederic Weisbecker

("rcutorture: Test runtime toggling of CPUs' callback offloading") should be moved on top of this pile and include this fixup.

Cc Paul E. McKenney <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Neeraj Upadhyay <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Boqun Feng <[email protected]>
---
 kernel/rcu/rcutorture.c | 3 ---
 1 file changed, 3 deletions(-)
kernel/rcu/rcutorture.c +0 -3
index 022a86722518..8358c7b7d491 100644
@@ -1580,9 +1580,6 @@ rcu_torture_reader(void *arg)
 	return 0;
 }
 
-void __weak rcu_nocb_cpu_offload(int cpu) {}
-void __weak rcu_nocb_cpu_deoffload(int cpu) {}
-
 /*
  * Randomly Toggle CPUs' callback-offload state.  This uses hrtimers to
  * increase race probabilities and fuzzes the interval between toggling.
Signature · 1 line
2.25.1
author maintainer
#16 lore raw

PATCH 16/19 rcutorture: Export nocb (de)offloading functions

In reply to Frederic Weisbecker

("rcutorture: Test runtime toggling of CPUs' callback offloading") should be moved on top of this pile and include this fixup.

Cc Paul E. McKenney <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Neeraj Upadhyay <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Boqun Feng <[email protected]>
---
 kernel/rcu/tree_plugin.h | 2 ++
 1 file changed, 2 insertions(+)
kernel/rcu/tree_plugin.h +2 -0
index b68714e5ce64..ada4b875e489 100644
@@ -2397,6 +2397,7 @@ int rcu_nocb_cpu_deoffload(int cpu)
 
 	return ret;
 }
+EXPORT_SYMBOL_GPL(rcu_nocb_cpu_deoffload);
 
 static int __rcu_nocb_rdp_offload(struct rcu_data *rdp)
 {
@@ -2472,6 +2473,7 @@ int rcu_nocb_cpu_offload(int cpu)
 
 	return ret;
 }
+EXPORT_SYMBOL_GPL(rcu_nocb_cpu_offload);
 
 
 void __init rcu_init_nohz(void)
Signature · 1 line
2.25.1
author maintainer
#17 lore raw

PATCH 17/19 cpu/hotplug: Add lockdep_is_cpus_held()

In reply to Frederic Weisbecker

RCU needs to check if the cpu hotplug lock is held, in the middle of other conditions to check the sanity of RCU-nocb. Provide a helper for that.

Cc Paul E. McKenney <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Neeraj Upadhyay <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Boqun Feng <[email protected]>
---
 include/linux/cpu.h | 1 +
 kernel/cpu.c        | 7 +++++++
 2 files changed, 8 insertions(+)
include/linux/cpu.h +1 -0
index d6428aaf67e7..27eb3875e45a 100644
@@ -118,6 +118,7 @@ extern void cpus_read_lock(void);
 extern void cpus_read_unlock(void);
 extern int  cpus_read_trylock(void);
 extern void lockdep_assert_cpus_held(void);
+extern int lockdep_is_cpus_held(void);
 extern void cpu_hotplug_disable(void);
 extern void cpu_hotplug_enable(void);
 void clear_tasks_mm_cpumask(int cpu);
kernel/cpu.c +7 -0
index 6ff2578ecf17..3cca5b27de25 100644
@@ -330,6 +330,13 @@ void lockdep_assert_cpus_held(void)
 	percpu_rwsem_assert_held(&cpu_hotplug_lock);
 }
 
+#ifdef CONFIG_LOCKDEP
+int lockdep_is_cpus_held(void)
+{
+	return percpu_rwsem_is_held(&cpu_hotplug_lock);
+}
+#endif
+
 static void lockdep_acquire_cpus_lock(void)
 {
 	rwsem_acquire(&cpu_hotplug_lock.dep_map, 0, 0, _THIS_IP_);
Signature · 1 line
2.25.1
author maintainer
#18 lore raw

Re: [PATCH 17/19] cpu/hotplug: Add lockdep_is_cpus_held()

Quoted 39 lines · Frederic Weisbecker

RCU needs to check if the cpu hotplug lock is held, in the middle of other conditions to check the sanity of RCU-nocb. Provide a helper for that.

Cc Paul E. McKenney <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Neeraj Upadhyay <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Boqun Feng <[email protected]>
---
 include/linux/cpu.h | 1 +
 kernel/cpu.c        | 7 +++++++
 2 files changed, 8 insertions(+)
include/linux/cpu.h +1 -0
index d6428aaf67e7..27eb3875e45a 100644
@@ -118,6 +118,7 @@ extern void cpus_read_lock(void);
 extern void cpus_read_unlock(void);
 extern int  cpus_read_trylock(void);
 extern void lockdep_assert_cpus_held(void);
+extern int lockdep_is_cpus_held(void);
 extern void cpu_hotplug_disable(void);
 extern void cpu_hotplug_enable(void);
 void clear_tasks_mm_cpumask(int cpu);
kernel/cpu.c +0 -0
index 6ff2578ecf17..3cca5b27de25 100644
@@ -330,6 +330,13 @@ void lockdep_assert_cpus_held(void)
 	percpu_rwsem_assert_held(&cpu_hotplug_lock);
 }
 

kernel/cpu.c void lockdep_assert_cpus_held(void) · line 342

Quoted 10 lines
kernel/cpu.c +7 -0
+#ifdef CONFIG_LOCKDEP
+int lockdep_is_cpus_held(void)
+{
+	return percpu_rwsem_is_held(&cpu_hotplug_lock);
+}
+#endif
+
 static void lockdep_acquire_cpus_lock(void)
 {
 	rwsem_acquire(&cpu_hotplug_lock.dep_map, 0, 0, _THIS_IP_);

This patch may trigger a build error with !CONFIG_HOTPLUG.

Here is a fixed version:

---
From 80609608b29758e43e8d8d6beaca486b8c42bb43 Mon Sep 17 00:00:00 2001
From: Frederic Weisbecker <[email protected]>
Date: Wed, 11 Nov 2020 23:53:13 +0100
Subject: [PATCH] cpu/hotplug: Add lockdep_is_cpus_held()

RCU needs to check if the cpu hotplug lock is held, in the middle of other conditions to check the sanity of RCU-nocb. Provide a helper for that.

Cc Paul E. McKenney <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Neeraj Upadhyay <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Boqun Feng <[email protected]>
---
 include/linux/cpu.h | 2 ++
 kernel/cpu.c        | 7 +++++++
 2 files changed, 9 insertions(+)
include/linux/cpu.h +2 -0
index d6428aaf67e7..3aaa0687e8df 100644
@@ -111,6 +111,8 @@ static inline void cpu_maps_update_done(void)
 #endif /* CONFIG_SMP */
 extern struct bus_type cpu_subsys;
 
+extern int lockdep_is_cpus_held(void);
+
 #ifdef CONFIG_HOTPLUG_CPU
 extern void cpus_write_lock(void);
 extern void cpus_write_unlock(void);
kernel/cpu.c +7 -0
index 6ff2578ecf17..3cca5b27de25 100644
@@ -330,6 +330,13 @@ void lockdep_assert_cpus_held(void)
 	percpu_rwsem_assert_held(&cpu_hotplug_lock);
 }
 
+#ifdef CONFIG_LOCKDEP
+int lockdep_is_cpus_held(void)
+{
+	return percpu_rwsem_is_held(&cpu_hotplug_lock);
+}
+#endif
+
 static void lockdep_acquire_cpus_lock(void)
 {
 	rwsem_acquire(&cpu_hotplug_lock.dep_map, 0, 0, _THIS_IP_);
Signature · 1 line
2.25.1
author maintainer
#19 lore raw

PATCH 18/19 timer: Add timer_curr_running()

In reply to Frederic Weisbecker

RCU needs to check if the current code is running a specific timer callback, in the middle of other conditions to check the sanity of RCU-nocb. Provide a helper for that.

Cc Paul E. McKenney <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Neeraj Upadhyay <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Boqun Feng <[email protected]>
---
 include/linux/timer.h |  2 ++
 kernel/time/timer.c   | 13 +++++++++++++
 2 files changed, 15 insertions(+)
include/linux/timer.h +2 -0
index d10bc7e73b41..c75dbf7d118f 100644
@@ -192,6 +192,8 @@ extern int try_to_del_timer_sync(struct timer_list *timer);
 
 #define del_singleshot_timer_sync(t) del_timer_sync(t)
 
+extern bool timer_curr_running(struct timer_list *timer);
+
 extern void init_timers(void);
 extern void run_local_timers(void);
 struct hrtimer;
kernel/time/timer.c +13 -0
index de37e33a868d..9d41e4c4cfd4 100644
@@ -1242,6 +1242,19 @@ int try_to_del_timer_sync(struct timer_list *timer)
 }
 EXPORT_SYMBOL(try_to_del_timer_sync);
 
+bool timer_curr_running(struct timer_list *timer)
+{
+	int i;
+
+	for (i = 0; i < NR_BASES; i++) {
+		struct timer_base *base = this_cpu_ptr(&timer_bases[i]);
+		if (base->running_timer == timer)
+			return true;
+	}
+
+	return false;
+}
+
 #ifdef CONFIG_PREEMPT_RT
 static __init void timer_base_init_expiry_lock(struct timer_base *base)
 {
Signature · 1 line
2.25.1
author maintainer
#20 lore raw

PATCH 19/19 rcu/nocb: Detect unsafe checks for offloaded rdp

In reply to Frederic Weisbecker

Provide CONFIG_PROVE_RCU sanity checks to ensure we are always reading the offloaded state of an rdp in a safe and stable way and prevent from its value to be changed under us. We must either hold the barrier mutex, the cpu-hotplug lock (read or write) or the nocb lock.
Local non-preemptible reads are also safe. NOCB kthreads and timers have their own means of synchronization against the offloaded state updaters.

Cc Paul E. McKenney <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Neeraj Upadhyay <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Boqun Feng <[email protected]>
---
 kernel/rcu/tree.c        | 21 +++++-----
 kernel/rcu/tree_plugin.h | 90 +++++++++++++++++++++++++++++++++-------
 2 files changed, 87 insertions(+), 24 deletions(-)
kernel/rcu/tree.c +11 -10
index b24d60a52f34..5293444b9683 100644
@@ -154,6 +154,7 @@ static void invoke_rcu_core(void);
 static void rcu_report_exp_rdp(struct rcu_data *rdp);
 static void sync_sched_exp_online_cleanup(int cpu);
 static void check_cb_ovld_locked(struct rcu_data *rdp, struct rcu_node *rnp);
+static bool rcu_rdp_is_offloaded(struct rcu_data *rdp);
 
 /* rcuc/rcub kthread realtime priority */
 static int kthread_prio = IS_ENABLED(CONFIG_RCU_BOOST) ? 1 : 0;
@@ -1617,7 +1618,7 @@ static bool __note_gp_changes(struct rcu_node *rnp, struct rcu_data *rdp)
 {
 	bool ret = false;
 	bool need_qs;
-	const bool offloaded = rcu_segcblist_is_offloaded(&rdp->cblist);
+	const bool offloaded = rcu_rdp_is_offloaded(rdp);
 
 	raw_lockdep_assert_held_rcu_node(rnp);
 
@@ -2068,7 +2069,7 @@ static void rcu_gp_cleanup(void)
 		needgp = true;
 	}
 	/* Advance CBs to reduce false positives below. */
-	offloaded = rcu_segcblist_is_offloaded(&rdp->cblist);
+	offloaded = rcu_rdp_is_offloaded(rdp);
 	if ((offloaded || !rcu_accelerate_cbs(rnp, rdp)) && needgp) {
 		WRITE_ONCE(rcu_state.gp_flags, RCU_GP_FLAG_INIT);
 		WRITE_ONCE(rcu_state.gp_req_activity, jiffies);
@@ -2267,7 +2268,7 @@ rcu_report_qs_rdp(struct rcu_data *rdp)
 	unsigned long flags;
 	unsigned long mask;
 	bool needwake = false;
-	const bool offloaded = rcu_segcblist_is_offloaded(&rdp->cblist);
+	const bool offloaded = rcu_rdp_is_offloaded(rdp);
 	struct rcu_node *rnp;
 
 	WARN_ON_ONCE(rdp->cpu != smp_processor_id());
@@ -2436,7 +2437,7 @@ static void rcu_do_batch(struct rcu_data *rdp)
 {
 	int div;
 	unsigned long flags;
-	const bool offloaded = rcu_segcblist_is_offloaded(&rdp->cblist);
+	const bool offloaded = rcu_rdp_is_offloaded(rdp);
 	struct rcu_head *rhp;
 	struct rcu_cblist rcl = RCU_CBLIST_INITIALIZER(rcl);
 	long bl, count = 0;
@@ -2992,7 +2993,7 @@ __call_rcu(struct rcu_head *head, rcu_callback_t func)
 				   rcu_segcblist_n_cbs(&rdp->cblist));
 
 	/* Go handle any RCU core processing required. */
-	if (unlikely(rcu_segcblist_is_offloaded(&rdp->cblist))) {
+	if (unlikely(rcu_rdp_is_offloaded(rdp))) {
 		__call_rcu_nocb_wake(rdp, was_alldone, flags); /* unlocks */
 	} else {
 		__call_rcu_core(rdp, head, flags);
@@ -3766,13 +3767,13 @@ static int rcu_pending(int user)
 		return 1;
 
 	/* Does this CPU have callbacks ready to invoke? */
-	if (!rcu_segcblist_is_offloaded(&rdp->cblist) &&
+	if (!rcu_rdp_is_offloaded(rdp) &&
 	    rcu_segcblist_ready_cbs(&rdp->cblist))
 		return 1;
 
 	/* Has RCU gone idle with this CPU needing another grace period? */
 	if (!gp_in_progress && rcu_segcblist_is_enabled(&rdp->cblist) &&
-	    !rcu_segcblist_is_offloaded(&rdp->cblist) &&
+	    !rcu_rdp_is_offloaded(rdp) &&
 	    !rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL))
 		return 1;
 
@@ -3891,7 +3892,7 @@ void rcu_barrier(void)
 	for_each_possible_cpu(cpu) {
 		rdp = per_cpu_ptr(&rcu_data, cpu);
 		if (cpu_is_offline(cpu) &&
-		    !rcu_segcblist_is_offloaded(&rdp->cblist))
+		    !rcu_rdp_is_offloaded(rdp))
 			continue;
 		if (rcu_segcblist_n_cbs(&rdp->cblist) && cpu_online(cpu)) {
 			rcu_barrier_trace(TPS("OnlineQ"), cpu,
@@ -4210,7 +4211,7 @@ void rcutree_migrate_callbacks(int cpu)
 	struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
 	bool needwake;
 
-	if (rcu_segcblist_is_offloaded(&rdp->cblist) ||
+	if (rcu_rdp_is_offloaded(rdp) ||
 	    rcu_segcblist_empty(&rdp->cblist))
 		return;  /* No callbacks to migrate. */
 
@@ -4228,7 +4229,7 @@ void rcutree_migrate_callbacks(int cpu)
 	rcu_segcblist_disable(&rdp->cblist);
 	WARN_ON_ONCE(rcu_segcblist_empty(&my_rdp->cblist) !=
 		     !rcu_segcblist_n_cbs(&my_rdp->cblist));
-	if (rcu_segcblist_is_offloaded(&my_rdp->cblist)) {
+	if (rcu_rdp_is_offloaded(my_rdp)) {
 		raw_spin_unlock_rcu_node(my_rnp); /* irqs remain disabled. */
 		__call_rcu_nocb_wake(my_rdp, true, flags);
 	} else {
kernel/rcu/tree_plugin.h +76 -14

182 lines not on this page · raw message

Signature · 1 line
2.25.1
#21 lore raw

Re: [PATCH 19/19] rcu/nocb: Detect unsafe checks for offloaded rdp

I love your patch! Yet something to improve:

Hi Frederic,

I love your patch! Yet something to improve:

[auto build test ERROR on rcu/dev]
[also build test ERROR on rcu/rcu/next next-20201113] [cannot apply to tip/timers/core linus/master linux/master v5.10-rc3] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Frederic-Weisbecker/rcu-nocb-De-offload-and-re-offload-support-v4/20201113-201658
base:   https://git.kernel.org/pub/scm/linux/kernel/git/paulmck/linux-rcu.git dev
config: arm-randconfig-r013-20201113 (attached as .config)
compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project 9e0c35655b6e8186baef8840b26ba4090503b554)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm cross compiling tool for clang build
# apt-get install binutils-arm-linux-gnueabi
# https://github.com/0day-ci/linux/commit/4c0d56c4bf8ec775e46d5e895080a2df42e306d8
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Frederic-Weisbecker/rcu-nocb-De-offload-and-re-offload-support-v4/20201113-201658 git checkout 4c0d56c4bf8ec775e46d5e895080a2df42e306d8
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=arm

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

In file included from kernel/rcu/tree.c:4591:

>> kernel/rcu/tree_plugin.h:71:40: error: implicit declaration of function 'lockdep_is_cpus_held' [-Werror,-Wimplicit-function-declaration]
                     (IS_ENABLED(CONFIG_HOTPLUG_CPU) && lockdep_is_cpus_held()) ||
                                                        ^
   kernel/rcu/tree_plugin.h:71:40: note: did you mean 'lockdep_assert_cpus_held'?
   include/linux/cpu.h:136:20: note: 'lockdep_assert_cpus_held' declared here
   static inline void lockdep_assert_cpus_held(void) { }
                      ^

1 error generated.

vim +/lockdep_is_cpus_held +71 kernel/rcu/tree_plugin.h

57
58 static bool rcu_rdp_is_offloaded(struct rcu_data *rdp)
59 {
60 /*
61 * In order to read the offloaded state of an rdp is a safe
62 * and stable way and prevent from its value to be changed
63 * under us, we must either hold the barrier mutex, the cpu
64 * hotplug lock (read or write) or the nocb lock. Local
65 * non-preemptible reads are also safe. NOCB kthreads and
66 * timers have their own means of synchronization against the
67 * offloaded state updaters.
68 */
69 RCU_LOCKDEP_WARN(
70 !(lockdep_is_held(&rcu_state.barrier_mutex) ||

  > 71			  (IS_ENABLED(CONFIG_HOTPLUG_CPU) && lockdep_is_cpus_held()) ||
    72			  rcu_lockdep_is_held_nocb(rdp) ||
    73			  (rdp == this_cpu_ptr(&rcu_data) &&
    74			   !(IS_ENABLED(CONFIG_PREEMPT_COUNT) && preemptible())) ||
    75			  rcu_current_is_nocb_kthread(rdp) ||
    76			  rcu_running_nocb_timer(rdp)),
    77			"Unsafe read of RCU_NOCB offloaded state"
    78		);
    79	
    80		return rcu_segcblist_is_offloaded(&rdp->cblist);
    81	}
    82	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#22 lore raw
In reply to Frederic Weisbecker

Hi Frederic,

Quoted 13 lines · Frederic Weisbecker

This keeps growing up. Rest assured, most of it is debug code and sanity checks.

Boqun Feng found that holding rnp lock while updating the offloaded state of an rdp isn't needed, and he was right despite my initial reaction. The sites that read the offloaded state while holding the rnp lock are actually protected because they read it locally in a non preemptible context.

So I removed the rnp lock in "rcu/nocb: De-offloading CB". And just to make sure I'm not missing something, I added sanity checks that ensure we always read the offloaded state in a safe way (3 last patches).

Quoted 7 lines

Still passes TREE01 (but I had to fight!)

git://git.kernel.org/pub/scm/linux/kernel/git/frederic/linux-dynticks.git
rcu/nocb-toggle-v4

HEAD: 579e15efa48fb6fc4ecf14961804051f385807fe

This whole series look good to me, plus I've run a test, so far everything seems working ;-) Here is my setup for the test:

I'm using a ARM64 guest (running on Hyper-V) to do the test, and the guest has 8 VCPUs. The code I'm using is v5.10-rc6 + Hyper-V ARM64 guest support [1] + your patchset (I actually did a merge from your rcu/nocb-toggle-v5 branch, because IIUC some modification for rcutorture is still in Paul's tree). I compiled with my normal configuration for ARM64 Hyper-V guest plus TREE01, boot the kernel with:

ignore_loglevel rcutree.gp_preinit_delay=3 rcutree.gp_init_delay=3 rcutree.gp_cleanup_delay=3 rcu_nocbs=0-1,3-7

and run rcutorture via:

modprobe rcutorture nocbs_nthreads=8 nocbs_toggle=1000 fwd_progress=0

I ran the rcutorture twice, one last for a week or so and one for a day or two and I didn't observe any problem so far. The latest test summary is:

	[...] rcu-torture: rtc: 00000000f794686f ver: 2226396 tfle: 0 rta: 2226397 rtaf: 0 rtf: 2226385 rtmbe: 0 rtmbkf: 0/1390141 rtbe: 0 rtbke: 0 rtbre: 0 rtbf: 0 rtb: 0 nt: 181415346 onoff: 0/0:0/0 -1,0:-1,0 0:0 (HZ=1000) barrier: 0/0:0 read-exits: 108102 nocb-toggles: 306964:306974 

Is there anything I'm missing for a useful test? Do you have other setup (kernel cmdline or rcutorture parameters) that you want me to try?

Regards,
Boqun

Quoted 40 lines

Thanks,
Frederic
---

Frederic Weisbecker (19):
rcu/nocb: Turn enabled/offload states into a common flag rcu/nocb: Provide basic callback offloading state machine bits rcu/nocb: Always init segcblist on CPU up
rcu/nocb: De-offloading CB kthread
rcu/nocb: Don't deoffload an offline CPU with pending work rcu/nocb: De-offloading GP kthread
rcu/nocb: Re-offload support
rcu/nocb: Shutdown nocb timer on de-offloading
rcu: Flush bypass before setting SEGCBLIST_SOFTIRQ_ONLY rcu/nocb: Set SEGCBLIST_SOFTIRQ_ONLY at the very last stage of de-offloading rcu/nocb: Only cond_resched() from actual offloaded batch processing rcu/nocb: Process batch locally as long as offloading isn't complete rcu/nocb: Locally accelerate callbacks as long as offloading isn't complete tools/rcutorture: Support nocb toggle in TREE01
rcutorture: Remove weak nocb declarations
rcutorture: Export nocb (de)offloading functions
cpu/hotplug: Add lockdep_is_cpus_held()
timer: Add timer_curr_running()
rcu/nocb: Detect unsafe checks for offloaded rdp

 include/linux/cpu.h                                |   1 +
 include/linux/rcu_segcblist.h                      | 119 +++++-
 include/linux/rcupdate.h                           |   4 +
 include/linux/timer.h                              |   2 +
 kernel/cpu.c                                       |   7 +
 kernel/rcu/rcu_segcblist.c                         |  13 +-
 kernel/rcu/rcu_segcblist.h                         |  45 ++-
 kernel/rcu/rcutorture.c                            |   3 -
 kernel/rcu/tree.c                                  |  49 ++-
 kernel/rcu/tree.h                                  |   2 +
 kernel/rcu/tree_plugin.h                           | 416 +++++++++++++++++++--
 kernel/time/timer.c                                |  13 +
 .../selftests/rcutorture/configs/rcu/TREE01.boot   |   4 +-
 13 files changed, 614 insertions(+), 64 deletions(-)
author maintainer
#23 lore raw

Hi Boqun Feng,

Quoted 40 lines · Boqun Feng

Hi Frederic,

Quoted 13 lines · Frederic Weisbecker

This keeps growing up. Rest assured, most of it is debug code and sanity checks.

Boqun Feng found that holding rnp lock while updating the offloaded state of an rdp isn't needed, and he was right despite my initial reaction. The sites that read the offloaded state while holding the rnp lock are actually protected because they read it locally in a non preemptible context.

So I removed the rnp lock in "rcu/nocb: De-offloading CB". And just to make sure I'm not missing something, I added sanity checks that ensure we always read the offloaded state in a safe way (3 last patches).

Quoted 7 lines

Still passes TREE01 (but I had to fight!)

git://git.kernel.org/pub/scm/linux/kernel/git/frederic/linux-dynticks.git
rcu/nocb-toggle-v4

HEAD: 579e15efa48fb6fc4ecf14961804051f385807fe

This whole series look good to me, plus I've run a test, so far everything seems working ;-) Here is my setup for the test:

I'm using a ARM64 guest (running on Hyper-V) to do the test, and the guest has 8 VCPUs. The code I'm using is v5.10-rc6 + Hyper-V ARM64 guest support [1] + your patchset (I actually did a merge from your rcu/nocb-toggle-v5 branch, because IIUC some modification for rcutorture is still in Paul's tree). I compiled with my normal configuration for ARM64 Hyper-V guest plus TREE01, boot the kernel with:

ignore_loglevel rcutree.gp_preinit_delay=3 rcutree.gp_init_delay=3 rcutree.gp_cleanup_delay=3 rcu_nocbs=0-1,3-7

and run rcutorture via:

modprobe rcutorture nocbs_nthreads=8 nocbs_toggle=1000 fwd_progress=0

Quoted 8 lines

I ran the rcutorture twice, one last for a week or so and one for a day or two and I didn't observe any problem so far. The latest test summary is:

	[...] rcu-torture: rtc: 00000000f794686f ver: 2226396 tfle: 0 rta: 2226397 rtaf: 0 rtf: 2226385 rtmbe: 0 rtmbkf: 0/1390141 rtbe: 0 rtbke: 0 rtbre: 0 rtbf: 0 rtb: 0 nt: 181415346 onoff: 0/0:0/0 -1,0:-1,0 0:0 (HZ=1000) barrier: 0/0:0 read-exits: 108102 nocb-toggles: 306964:306974 

Is there anything I'm missing for a useful test? Do you have other setup (kernel cmdline or rcutorture parameters) that you want me to try?

Thanks a lot for reviewing and testing. You seem to have tested with the right options, I have nothing better to suggest. Plus I'm glad you tested on ARM64. x86 is the only target I have tested so far.

maintainer
#24 lore raw
Quoted 47 lines · Frederic Weisbecker

Hi Boqun Feng,

Quoted 44 lines · Boqun Feng

Hi Frederic,

Quoted 13 lines · Frederic Weisbecker

This keeps growing up. Rest assured, most of it is debug code and sanity checks.

Boqun Feng found that holding rnp lock while updating the offloaded state of an rdp isn't needed, and he was right despite my initial reaction. The sites that read the offloaded state while holding the rnp lock are actually protected because they read it locally in a non preemptible context.

So I removed the rnp lock in "rcu/nocb: De-offloading CB". And just to make sure I'm not missing something, I added sanity checks that ensure we always read the offloaded state in a safe way (3 last patches).

Quoted 7 lines

Still passes TREE01 (but I had to fight!)

git://git.kernel.org/pub/scm/linux/kernel/git/frederic/linux-dynticks.git
rcu/nocb-toggle-v4

HEAD: 579e15efa48fb6fc4ecf14961804051f385807fe

This whole series look good to me, plus I've run a test, so far everything seems working ;-) Here is my setup for the test:

I'm using a ARM64 guest (running on Hyper-V) to do the test, and the guest has 8 VCPUs. The code I'm using is v5.10-rc6 + Hyper-V ARM64 guest support [1] + your patchset (I actually did a merge from your rcu/nocb-toggle-v5 branch, because IIUC some modification for rcutorture is still in Paul's tree). I compiled with my normal configuration for ARM64 Hyper-V guest plus TREE01, boot the kernel with:

ignore_loglevel rcutree.gp_preinit_delay=3 rcutree.gp_init_delay=3 rcutree.gp_cleanup_delay=3 rcu_nocbs=0-1,3-7

and run rcutorture via:

modprobe rcutorture nocbs_nthreads=8 nocbs_toggle=1000 fwd_progress=0

I ran the rcutorture twice, one last for a week or so and one for a day or two and I didn't observe any problem so far. The latest test summary is:

Quoted 8 lines
Quoted 4 lines
	[...] rcu-torture: rtc: 00000000f794686f ver: 2226396 tfle: 0 rta: 2226397 rtaf: 0 rtf: 2226385 rtmbe: 0 rtmbkf: 0/1390141 rtbe: 0 rtbke: 0 rtbre: 0 rtbf: 0 rtb: 0 nt: 181415346 onoff: 0/0:0/0 -1,0:-1,0 0:0 (HZ=1000) barrier: 0/0:0 read-exits: 108102 nocb-toggles: 306964:306974 

Is there anything I'm missing for a useful test? Do you have other setup (kernel cmdline or rcutorture parameters) that you want me to try?

Thanks a lot for reviewing and testing. You seem to have tested with the right options, I have nothing better to suggest. Plus I'm glad you tested on ARM64. x86 is the only target I have tested so far.

Boqun, would you be willing to give your Tested-by?

Thanx, Paul

Tested-by
#25 lore raw
Quoted 50 lines · Paul E. McKenney
Quoted 49 lines · Frederic Weisbecker

Hi Boqun Feng,

Quoted 46 lines · Boqun Feng

Hi Frederic,

Quoted 13 lines · Frederic Weisbecker

This keeps growing up. Rest assured, most of it is debug code and sanity checks.

Boqun Feng found that holding rnp lock while updating the offloaded state of an rdp isn't needed, and he was right despite my initial reaction. The sites that read the offloaded state while holding the rnp lock are actually protected because they read it locally in a non preemptible context.

So I removed the rnp lock in "rcu/nocb: De-offloading CB". And just to make sure I'm not missing something, I added sanity checks that ensure we always read the offloaded state in a safe way (3 last patches).

Quoted 7 lines

Still passes TREE01 (but I had to fight!)

git://git.kernel.org/pub/scm/linux/kernel/git/frederic/linux-dynticks.git
rcu/nocb-toggle-v4

HEAD: 579e15efa48fb6fc4ecf14961804051f385807fe

This whole series look good to me, plus I've run a test, so far everything seems working ;-) Here is my setup for the test:

I'm using a ARM64 guest (running on Hyper-V) to do the test, and the guest has 8 VCPUs. The code I'm using is v5.10-rc6 + Hyper-V ARM64 guest support [1] + your patchset (I actually did a merge from your rcu/nocb-toggle-v5 branch, because IIUC some modification for rcutorture is still in Paul's tree). I compiled with my normal configuration for ARM64 Hyper-V guest plus TREE01, boot the kernel with:

ignore_loglevel rcutree.gp_preinit_delay=3 rcutree.gp_init_delay=3 rcutree.gp_cleanup_delay=3 rcu_nocbs=0-1,3-7

and run rcutorture via:

modprobe rcutorture nocbs_nthreads=8 nocbs_toggle=1000 fwd_progress=0

I ran the rcutorture twice, one last for a week or so and one for a day or two and I didn't observe any problem so far. The latest test summary is:

	[...] rcu-torture: rtc: 00000000f794686f ver: 2226396 tfle: 0 rta: 2226397 rtaf: 0 rtf: 2226385 rtmbe: 0 rtmbkf: 0/1390141 rtbe: 0 rtbke: 0 rtbre: 0 rtbf: 0 rtb: 0 nt: 181415346 onoff: 0/0:0/0 -1,0:-1,0 0:0 (HZ=1000) barrier: 0/0:0 read-exits: 108102 nocb-toggles: 306964:306974 
Quoted 9 lines
Quoted 6 lines
Quoted 2 lines

Is there anything I'm missing for a useful test? Do you have other setup (kernel cmdline or rcutorture parameters) that you want me to try?

Thanks a lot for reviewing and testing. You seem to have tested with the right options, I have nothing better to suggest. Plus I'm glad you tested on ARM64. x86 is the only target I have tested so far.

Boqun, would you be willing to give your Tested-by?

Sure, FWIW, for the whole series, feel free to add:

Regards,
Boqun

Quoted 1 line

Thanx, Paul

maintainer
#26 lore raw
In reply to Frederic Weisbecker
Quoted 15 lines · Frederic Weisbecker

This keeps growing up. Rest assured, most of it is debug code and sanity checks.

Boqun Feng found that holding rnp lock while updating the offloaded state of an rdp isn't needed, and he was right despite my initial reaction. The sites that read the offloaded state while holding the rnp lock are actually protected because they read it locally in a non preemptible context.

So I removed the rnp lock in "rcu/nocb: De-offloading CB". And just to make sure I'm not missing something, I added sanity checks that ensure we always read the offloaded state in a safe way (3 last patches).

Still passes TREE01 (but I had to fight!)

Quoted 7 lines

git://git.kernel.org/pub/scm/linux/kernel/git/frederic/linux-dynticks.git
rcu/nocb-toggle-v4

HEAD: 579e15efa48fb6fc4ecf14961804051f385807fe

Thanks,
Frederic

Thank you both!

Quoted 17 lines

---

Frederic Weisbecker (19):
rcu/nocb: Turn enabled/offload states into a common flag rcu/nocb: Provide basic callback offloading state machine bits rcu/nocb: Always init segcblist on CPU up
rcu/nocb: De-offloading CB kthread
rcu/nocb: Don't deoffload an offline CPU with pending work rcu/nocb: De-offloading GP kthread
rcu/nocb: Re-offload support
rcu/nocb: Shutdown nocb timer on de-offloading
rcu: Flush bypass before setting SEGCBLIST_SOFTIRQ_ONLY rcu/nocb: Set SEGCBLIST_SOFTIRQ_ONLY at the very last stage of de-offloading rcu/nocb: Only cond_resched() from actual offloaded batch processing rcu/nocb: Process batch locally as long as offloading isn't complete rcu/nocb: Locally accelerate callbacks as long as offloading isn't complete tools/rcutorture: Support nocb toggle in TREE01

I applied the above, with the usual commit-log wordsmithing.

Quoted 2 lines

rcutorture: Remove weak nocb declarations
rcutorture: Export nocb (de)offloading functions

These I folded into the rcutorture commit, as you suggested.

Quoted 2 lines

cpu/hotplug: Add lockdep_is_cpus_held()
timer: Add timer_curr_running()

I applied these two.

Quoted 1 line

rcu/nocb: Detect unsafe checks for offloaded rdp

This one didn't apply, probably due to recent changes in -rcu. Could you please take a look?

I do get the occasional rcutorture writer stall in TREE01. I sent you the console log offlist, but I suspect that this is related to some of your questions. I am retesting with the new TREE01 boot parameters removed (but with the new rcu_nocbs= layout). These stalls are temporary, with torturing progressing normally after the warnings are printed.

This warning usually indicates that grace periods are progressing

I believe that the following enhancements will be needed:

o Forbid toggling of offlined CPUs. This is a simple rule that
results in deterministic success or failure. The current setup (which I freely admit that I suggested) will fail very rarely, only if a newly offlined offloaded CPU is toggled before its remaining callbacks are invoked. The current state is therefore an accident waiting to happen.

This will entail fixing a few comments as well.

o Drain the bypass early in the de-offloading process and prohibit
queuing onto the bypass anywhere in the toggling process. Then the only CPUs permitted to use the bypass are those that are fully offloaded. This approach allows rcu_core() and the rcuog kthreads to safely manipulate callbacks and grace periods concurrently. (Famous last words!) This might address the rcutorture writer stall warnings I am seeing.

This will entail fixing a few comments as well.

o Avoid double write to rdp->nocb_cb_sleep in nocb_cb_wait().
Unless there is some reason why this is absolutely required. It will cause confusion as it is.

o What bad thing happens if we de-offload the CPU corresponding to
the nocb GP kthread? It does work fine when that CPU is offlined.

Coincidence or not, all but one of the rcutorture writer stalls is followed by a message refusing to de-offload this CPU.

o The nocb_gp_update_state() function's return value seems backwards.
What am I missing here?

o __rcu_nocb_rdp_deoffload(): Why move rcu_nocb_lock_irqsave()
across a comment?

o We need better debug. For example, "Can't deoffload an rdp GP
leader (yet)". Well, which CPU? For another example, we need
deoffload state in rcutorture writer-stall dumps.

I intend to do the following, and, if feasible, fold them into the original commits:

o Make rcu_segcblist_is_offloaded() use "&&" instead of a
nested "if" statement.

o nocb_cb_wait() needs the "^^^ Ensure CB invocation follows
_sleep test" to be adjusted so that it is properly attached to its smp_load_acquire().

o nocb_cb_wait() -- alphabetical order for local variables, please!
Yeah, I know, others like inverted tree for reasons that escape me completely.

Thanx, Paul

Quoted 14 lines
 include/linux/cpu.h                                |   1 +
 include/linux/rcu_segcblist.h                      | 119 +++++-
 include/linux/rcupdate.h                           |   4 +
 include/linux/timer.h                              |   2 +
 kernel/cpu.c                                       |   7 +
 kernel/rcu/rcu_segcblist.c                         |  13 +-
 kernel/rcu/rcu_segcblist.h                         |  45 ++-
 kernel/rcu/rcutorture.c                            |   3 -
 kernel/rcu/tree.c                                  |  49 ++-
 kernel/rcu/tree.h                                  |   2 +
 kernel/rcu/tree_plugin.h                           | 416 +++++++++++++++++++--
 kernel/time/timer.c                                |  13 +
 .../selftests/rcutorture/configs/rcu/TREE01.boot   |   4 +-
 13 files changed, 614 insertions(+), 64 deletions(-)
author maintainer
#27 lore raw
Quoted 18 lines · Paul E. McKenney
Quoted 16 lines · Frederic Weisbecker

Frederic Weisbecker (19):
rcu/nocb: Turn enabled/offload states into a common flag rcu/nocb: Provide basic callback offloading state machine bits rcu/nocb: Always init segcblist on CPU up
rcu/nocb: De-offloading CB kthread
rcu/nocb: Don't deoffload an offline CPU with pending work rcu/nocb: De-offloading GP kthread
rcu/nocb: Re-offload support
rcu/nocb: Shutdown nocb timer on de-offloading
rcu: Flush bypass before setting SEGCBLIST_SOFTIRQ_ONLY rcu/nocb: Set SEGCBLIST_SOFTIRQ_ONLY at the very last stage of de-offloading rcu/nocb: Only cond_resched() from actual offloaded batch processing rcu/nocb: Process batch locally as long as offloading isn't complete rcu/nocb: Locally accelerate callbacks as long as offloading isn't complete tools/rcutorture: Support nocb toggle in TREE01

Quoted 6 lines

I applied the above, with the usual commit-log wordsmithing.

Quoted 2 lines

rcutorture: Remove weak nocb declarations
rcutorture: Export nocb (de)offloading functions

These I folded into the rcutorture commit, as you suggested.

Good!

Quoted 10 lines
Quoted 3 lines

cpu/hotplug: Add lockdep_is_cpus_held()
timer: Add timer_curr_running()

I applied these two.

Quoted 1 line

rcu/nocb: Detect unsafe checks for offloaded rdp

This one didn't apply, probably due to recent changes in -rcu. Could you please take a look?

Ok I'm going to rebase it.

Quoted 10 lines

I believe that the following enhancements will be needed:

o Forbid toggling of offlined CPUs. This is a simple rule that
results in deterministic success or failure. The current setup (which I freely admit that I suggested) will fail very rarely, only if a newly offlined offloaded CPU is toggled before its remaining callbacks are invoked. The current state is therefore an accident waiting to happen.

This will entail fixing a few comments as well.

Sure, I'm always glad to remove lines!

Quoted 10 lines

o Drain the bypass early in the de-offloading process and prohibit
queuing onto the bypass anywhere in the toggling process. Then the only CPUs permitted to use the bypass are those that are fully offloaded. This approach allows rcu_core() and the rcuog kthreads to safely manipulate callbacks and grace periods concurrently. (Famous last words!) This might address the rcutorture writer stall warnings I am seeing.

This will entail fixing a few comments as well.

Ok, I'm checking that.

Quoted 4 lines

o Avoid double write to rdp->nocb_cb_sleep in nocb_cb_wait().
Unless there is some reason why this is absolutely required. It will cause confusion as it is.

Ok.

Quoted 6 lines

o What bad thing happens if we de-offload the CPU corresponding to
the nocb GP kthread? It does work fine when that CPU is offlined.

Coincidence or not, all but one of the rcutorture writer stalls is followed by a message refusing to de-offload this CPU.

It probably started with a brainfart that I elaborated a bit too far. Let me check that again.

Quoted 3 lines

o The nocb_gp_update_state() function's return value seems backwards.
What am I missing here?

Indeed I should rename it to nocb_gp_check_rdp() or something.

Quoted 3 lines

o __rcu_nocb_rdp_deoffload(): Why move rcu_nocb_lock_irqsave()
across a comment?

Because the comment only concerns the locking?

Quoted 4 lines

o We need better debug. For example, "Can't deoffload an rdp GP
leader (yet)". Well, which CPU? For another example, we need
deoffload state in rcutorture writer-stall dumps.

Ok.

Quoted 7 lines

I intend to do the following, and, if feasible, fold them into the original commits:

o Make rcu_segcblist_is_offloaded() use "&&" instead of a
nested "if" statement.

Quoted 7 lines

o nocb_cb_wait() needs the "^^^ Ensure CB invocation follows
_sleep test" to be adjusted so that it is properly attached to its smp_load_acquire().

o nocb_cb_wait() -- alphabetical order for local variables, please!
Yeah, I know, others like inverted tree for reasons that escape me completely.

Ah, ok noted.

Thanks!

Quoted 16 lines

Thanx, Paul

Quoted 14 lines
 include/linux/cpu.h                                |   1 +
 include/linux/rcu_segcblist.h                      | 119 +++++-
 include/linux/rcupdate.h                           |   4 +
 include/linux/timer.h                              |   2 +
 kernel/cpu.c                                       |   7 +
 kernel/rcu/rcu_segcblist.c                         |  13 +-
 kernel/rcu/rcu_segcblist.h                         |  45 ++-
 kernel/rcu/rcutorture.c                            |   3 -
 kernel/rcu/tree.c                                  |  49 ++-
 kernel/rcu/tree.h                                  |   2 +
 kernel/rcu/tree_plugin.h                           | 416 +++++++++++++++++++--
 kernel/time/timer.c                                |  13 +
 .../selftests/rcutorture/configs/rcu/TREE01.boot   |   4 +-
 13 files changed, 614 insertions(+), 64 deletions(-)