Skip to contentSkip to conversation

rcu: remove redundant cpu affinity setting during teardown

PATCH In review Pingfan Liu 3 participants 10 messages Sep 5, 2022 – Sep 14, 2022 RCU

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 rcu: remove redundant cpu affinity setting during teardown

At present, during the cpu teardown, rcu_boost_kthread_setaffinity() is called twice. Firstly by rcutree_offline_cpu(), then by rcutree_dead_cpu() as the CPUHP_RCUTREE_PREP cpuhp_step callback.

From the scheduler's perspective, a bit in cpu_online_mask means that the cpu is visible to the scheduler. Furthermore, a bit in cpu_active_mask means that the cpu is suitable as a migration destination.

Now turning back to the case in rcu offlining. sched_cpu_deactivate() has disabled the dying cpu as the migration destination before rcutree_offline_cpu(). Furthermore, if the boost kthread is on the dying cpu, it will be migrated to another suitable online cpu by the scheduler. So the affinity setting by rcutree_offline_cpu() is redundant and can be eliminated.

Besides, this patch does an trival code rearrangement by unfolding rcutree_affinity_setting() into rcutree_online_cpu(), considering that the latter one is the only user of the former.

Cc "Paul E. McKenney" <[email protected]>
Cc David Woodhouse <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Neeraj Upadhyay <[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 "Jason A. Donenfeld" <[email protected]>

To: [email protected]

---
 kernel/rcu/tree.c | 14 +-------------
 1 file changed, 1 insertion(+), 13 deletions(-)
kernel/rcu/tree.c +1 -13
index 79aea7df4345..b90f6487fd45 100644
@@ -3978,16 +3978,6 @@ int rcutree_prepare_cpu(unsigned int cpu)
 	return 0;
 }
 
-/*
- * Update RCU priority boot kthread affinity for CPU-hotplug changes.
- */
-static void rcutree_affinity_setting(unsigned int cpu, int outgoing)
-{
-	struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
-
-	rcu_boost_kthread_setaffinity(rdp->mynode, outgoing);
-}
-
 /*
  * Near the end of the CPU-online process.  Pretty much all services
  * enabled, and the CPU is now very much alive.
@@ -4006,7 +3996,7 @@ int rcutree_online_cpu(unsigned int cpu)
 	if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
 		return 0; /* Too early in boot for scheduler work. */
 	sync_sched_exp_online_cleanup(cpu);
-	rcutree_affinity_setting(cpu, -1);
+	rcu_boost_kthread_setaffinity(rdp->mynode, -1);
 
 	// Stop-machine done, so allow nohz_full to disable tick.
 	tick_dep_clear(TICK_DEP_BIT_RCU);
@@ -4029,8 +4019,6 @@ int rcutree_offline_cpu(unsigned int cpu)
 	rnp->ffmask &= ~rdp->grpmask;
 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
 
-	rcutree_affinity_setting(cpu, cpu);
-
 	// nohz_full CPUs need the tick for stop-machine to work quickly
 	tick_dep_set(TICK_DEP_BIT_RCU);
 	return 0;
Signature · 1 line
2.31.1
#1 lore raw

PATCH 2/3 rcu: simplify the prototype of rcu_boost_kthread_setaffinity()

Previously, the outgoing parameter is needed by rcutree_affinity_setting() since when rcutree_offline_cpu() is called, ->qsmaskinitnext contains the outgoing cpu.

Now, it is not necessary and the prototype of rcu_boost_kthread_setaffinity() can be simplified.

Cc "Paul E. McKenney" <[email protected]>
Cc David Woodhouse <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Neeraj Upadhyay <[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 "Jason A. Donenfeld" <[email protected]>

To: [email protected]

---
 kernel/rcu/tree.c        |  6 +++---
 kernel/rcu/tree_plugin.h | 12 +++++-------
 2 files changed, 8 insertions(+), 10 deletions(-)
kernel/rcu/tree.c +3 -3
index b90f6487fd45..f0535bd210b6 100644
@@ -145,7 +145,7 @@ static void rcu_report_qs_rnp(unsigned long mask, struct rcu_node *rnp,
 			      unsigned long gps, unsigned long flags);
 static void rcu_init_new_rnp(struct rcu_node *rnp_leaf);
 static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf);
-static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
+static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp);
 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);
@@ -2170,7 +2170,7 @@ int rcutree_dead_cpu(unsigned int cpu)
 
 	WRITE_ONCE(rcu_state.n_online_cpus, rcu_state.n_online_cpus - 1);
 	/* Adjust any no-longer-needed kthreads. */
-	rcu_boost_kthread_setaffinity(rnp, -1);
+	rcu_boost_kthread_setaffinity(rnp);
 	// Stop-machine done, so allow nohz_full to disable tick.
 	tick_dep_clear(TICK_DEP_BIT_RCU);
 	return 0;
@@ -3996,7 +3996,7 @@ int rcutree_online_cpu(unsigned int cpu)
 	if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
 		return 0; /* Too early in boot for scheduler work. */
 	sync_sched_exp_online_cleanup(cpu);
-	rcu_boost_kthread_setaffinity(rdp->mynode, -1);
+	rcu_boost_kthread_setaffinity(rdp->mynode);
 
 	// Stop-machine done, so allow nohz_full to disable tick.
 	tick_dep_clear(TICK_DEP_BIT_RCU);
kernel/rcu/tree_plugin.h +5 -7
index 438ecae6bd7e..0bf6de185af5 100644
@@ -1217,11 +1217,10 @@ static void rcu_spawn_one_boost_kthread(struct rcu_node *rnp)
  * served by the rcu_node in question.  The CPU hotplug lock is still
  * held, so the value of rnp->qsmaskinit will be stable.
  *
- * We don't include outgoingcpu in the affinity set, use -1 if there is
- * no outgoing CPU.  If there are no CPUs left in the affinity set,
- * this function allows the kthread to execute on any CPU.
+ * If there are no CPUs left in the affinity set, this function allows the
+ * kthread to execute on any CPU.
  */
-static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
+static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp)
 {
 	struct task_struct *t = rnp->boost_kthread_task;
 	unsigned long mask = rcu_rnp_online_cpus(rnp);
@@ -1234,8 +1233,7 @@ static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
 		return;
 	mutex_lock(&rnp->boost_kthread_mutex);
 	for_each_leaf_node_possible_cpu(rnp, cpu)
-		if ((mask & leaf_node_cpu_bit(rnp, cpu)) &&
-		    cpu != outgoingcpu)
+		if ((mask & leaf_node_cpu_bit(rnp, cpu)))
 			cpumask_set_cpu(cpu, cm);
 	cpumask_and(cm, cm, housekeeping_cpumask(HK_TYPE_RCU));
 	if (cpumask_empty(cm))
@@ -1261,7 +1259,7 @@ static void rcu_spawn_one_boost_kthread(struct rcu_node *rnp)
 {
 }
 
-static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
+static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp)
 {
 }
 
Signature · 1 line
2.31.1
#2 lore raw

PATCH 3/3 rcu: Keep qsmaskinitnext fresh for rcu_boost_kthread_setaffinity()

In reply to Pingfan Liu

rcutree_online_cpu() is concurrent, so there is the following race scene:

CPU 1 CPU2
mask_old = rcu_rnp_online_cpus(rnp);
...

                                   mask_new = rcu_rnp_online_cpus(rnp);
                                   ...
                                   set_cpus_allowed_ptr(t, cm);

set_cpus_allowed_ptr(t, cm);

Consequently, the old mask will overwrite the new mask in the task's cpus_ptr.

Since there is a mutex ->boost_kthread_mutex, using it to build an order, then the latest ->qsmaskinitnext will be fetched for updating cpus_ptr.

Notes about the cpu teardown: The cpu hot-removing initiator executes rcutree_dead_cpu() for the teardown cpu. If in future, an initiator can hot-remove several cpus at a time, it executes rcutree_dead_cpu() in series for each cpu. So it is still race-free without the mutex. But considering this is a cold path, applying that redundant mutex is harmless.

Cc "Paul E. McKenney" <[email protected]>
Cc David Woodhouse <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Neeraj Upadhyay <[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 "Jason A. Donenfeld" <[email protected]>

To: [email protected]

---
 kernel/rcu/tree_plugin.h | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/rcu/tree_plugin.h +6 -1
index 0bf6de185af5..b868ac6c6ac8 100644
@@ -1223,7 +1223,7 @@ static void rcu_spawn_one_boost_kthread(struct rcu_node *rnp)
 static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp)
 {
 	struct task_struct *t = rnp->boost_kthread_task;
-	unsigned long mask = rcu_rnp_online_cpus(rnp);
+	unsigned long mask;
 	cpumask_var_t cm;
 	int cpu;
 
@@ -1232,6 +1232,11 @@ static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp)
 	if (!zalloc_cpumask_var(&cm, GFP_KERNEL))
 		return;
 	mutex_lock(&rnp->boost_kthread_mutex);
+	/*
+	 * Relying on the lock to serialize, so the latest qsmaskinitnext is for
+	 * cpus_ptr.
+	 */
+	mask = rcu_rnp_online_cpus(rnp);
 	for_each_leaf_node_possible_cpu(rnp, cpu)
 		if ((mask & leaf_node_cpu_bit(rnp, cpu)))
 			cpumask_set_cpu(cpu, cm);
Signature · 1 line
2.31.1
maintainer
#3 lore raw

Re: [PATCH 3/3] rcu: Keep qsmaskinitnext fresh for rcu_boost_kthread_setaffinity()

Quoted 36 lines · Pingfan Liu

rcutree_online_cpu() is concurrent, so there is the following race scene:

CPU 1 CPU2
mask_old = rcu_rnp_online_cpus(rnp);
...

                                   mask_new = rcu_rnp_online_cpus(rnp);
                                   ...
                                   set_cpus_allowed_ptr(t, cm);

set_cpus_allowed_ptr(t, cm);

Consequently, the old mask will overwrite the new mask in the task's cpus_ptr.

Since there is a mutex ->boost_kthread_mutex, using it to build an order, then the latest ->qsmaskinitnext will be fetched for updating cpus_ptr.

Notes about the cpu teardown: The cpu hot-removing initiator executes rcutree_dead_cpu() for the teardown cpu. If in future, an initiator can hot-remove several cpus at a time, it executes rcutree_dead_cpu() in series for each cpu. So it is still race-free without the mutex. But considering this is a cold path, applying that redundant mutex is harmless.

Cc "Paul E. McKenney" <[email protected]>
Cc David Woodhouse <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Neeraj Upadhyay <[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 "Jason A. Donenfeld" <[email protected]>
Quoted 1 line

To: [email protected]

Good choice, thank you!

Because I did not take your patch #2, I had to hand-apply this one. Please check the result below and let me know if I messed something up.

Thanx, Paul

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

commit 7139c19d2ea117976aa892de4fb75682e989ba12

Author: Pingfan Liu <[email protected]>
Date:   Tue Sep 6 11:36:42 2022 -0700

rcu: Synchronize ->qsmaskinitnext in rcu_boost_kthread_setaffinity()

Once either rcutree_online_cpu() or rcutree_dead_cpu() is invoked concurrently, the following rcu_boost_kthread_setaffinity() race can occur:

CPU 1 CPU2
mask = rcu_rnp_online_cpus(rnp);
...

mask = rcu_rnp_online_cpus(rnp);
...
set_cpus_allowed_ptr(t, cm);

set_cpus_allowed_ptr(t, cm);

This results in CPU2's update being overwritten by that of CPU1, and thus the possibility of ->boost_kthread_task continuing to run on a to-be-offlined CPU.

This commit therefore eliminates this race by relying on the pre-existing acquisition of ->boost_kthread_mutex to serialize the full process of changing the affinity of ->boost_kthread_task.

Cc David Woodhouse <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Neeraj Upadhyay <[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 "Jason A. Donenfeld" <[email protected]>
kernel/rcu/tree_plugin.h +4 -1
index e3142ee35fc6a..7b0fe741a0886 100644
@@ -1221,11 +1221,13 @@ static void rcu_spawn_one_boost_kthread(struct rcu_node *rnp)
  * We don't include outgoingcpu in the affinity set, use -1 if there is
  * no outgoing CPU.  If there are no CPUs left in the affinity set,
  * this function allows the kthread to execute on any CPU.
+ *
+ * Any future concurrent calls are serialized via ->boost_kthread_mutex.
  */
 static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
 {
 	struct task_struct *t = rnp->boost_kthread_task;
-	unsigned long mask = rcu_rnp_online_cpus(rnp);
+	unsigned long mask;
 	cpumask_var_t cm;
 	int cpu;
 
@@ -1234,6 +1236,7 @@ static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
 	if (!zalloc_cpumask_var(&cm, GFP_KERNEL))
 		return;
 	mutex_lock(&rnp->boost_kthread_mutex);
+	mask = rcu_rnp_online_cpus(rnp);
 	for_each_leaf_node_possible_cpu(rnp, cpu)
 		if ((mask & leaf_node_cpu_bit(rnp, cpu)) &&
 		    cpu != outgoingcpu)
maintainer
#4 lore raw
In reply to Pingfan Liu
Quoted 42 lines · Pingfan Liu

At present, during the cpu teardown, rcu_boost_kthread_setaffinity() is called twice. Firstly by rcutree_offline_cpu(), then by rcutree_dead_cpu() as the CPUHP_RCUTREE_PREP cpuhp_step callback.

Quoted 1 line

From the scheduler's perspective, a bit in cpu_online_mask means that the cpu

is visible to the scheduler. Furthermore, a bit in cpu_active_mask means that the cpu is suitable as a migration destination.

Now turning back to the case in rcu offlining. sched_cpu_deactivate() has disabled the dying cpu as the migration destination before rcutree_offline_cpu(). Furthermore, if the boost kthread is on the dying cpu, it will be migrated to another suitable online cpu by the scheduler. So the affinity setting by rcutree_offline_cpu() is redundant and can be eliminated.

Besides, this patch does an trival code rearrangement by unfolding rcutree_affinity_setting() into rcutree_online_cpu(), considering that the latter one is the only user of the former.

Cc "Paul E. McKenney" <[email protected]>
Cc David Woodhouse <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Neeraj Upadhyay <[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 "Jason A. Donenfeld" <[email protected]>

To: [email protected]

---
 kernel/rcu/tree.c | 14 +-------------
 1 file changed, 1 insertion(+), 13 deletions(-)
kernel/rcu/tree.c +0 -0
index 79aea7df4345..b90f6487fd45 100644
@@ -3978,16 +3978,6 @@ int rcutree_prepare_cpu(unsigned int cpu)
 	return 0;
 }

kernel/rcu/tree.c int rcutree_prepare_cpu(unsigned int cpu) · line 3989

Quoted 10 lines
kernel/rcu/tree.c +0 -9
 
-/*
- * Update RCU priority boot kthread affinity for CPU-hotplug changes.
- */
-static void rcutree_affinity_setting(unsigned int cpu, int outgoing)
-{
-	struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
-
-	rcu_boost_kthread_setaffinity(rdp->mynode, outgoing);
-}

Good point, tiven how simple a wrapper this is and how little it is used, getting rid of it does sound like a reasonable idea.

Quoted 13 lines
kernel/rcu/tree.c +1 -1
 /*
  * Near the end of the CPU-online process.  Pretty much all services
  * enabled, and the CPU is now very much alive.
@@ -4006,7 +3996,7 @@ int rcutree_online_cpu(unsigned int cpu)
 	if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
 		return 0; /* Too early in boot for scheduler work. */
 	sync_sched_exp_online_cleanup(cpu);
-	rcutree_affinity_setting(cpu, -1);
+	rcu_boost_kthread_setaffinity(rdp->mynode, -1);
 
 	// Stop-machine done, so allow nohz_full to disable tick.
 	tick_dep_clear(TICK_DEP_BIT_RCU);

kernel/rcu/tree.c int rcutree_offline_cpu(unsigned int cpu) · line 4032

Quoted 4 lines
kernel/rcu/tree.c +0 -1
@@ -4029,8 +4019,6 @@ int rcutree_offline_cpu(unsigned int cpu)
 	rnp->ffmask &= ~rdp->grpmask;
 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
 
-	rcutree_affinity_setting(cpu, cpu);

We do need to keep this one because the CPU is going away.

One the other hand, it might well be that we could get rid of the call to rcutree_affinity_setting() in rcutree_dead_cpu().

Or am I missing something subtle here?

Thanx, Paul

Quoted 7 lines
kernel/rcu/tree.c +0 -1
-
 	// nohz_full CPUs need the tick for stop-machine to work quickly
 	tick_dep_set(TICK_DEP_BIT_RCU);
 	return 0;
Signature · 1 line
2.31.1
#5 lore raw
Quoted 73 lines · Paul E. McKenney
Quoted 42 lines · Pingfan Liu

At present, during the cpu teardown, rcu_boost_kthread_setaffinity() is called twice. Firstly by rcutree_offline_cpu(), then by rcutree_dead_cpu() as the CPUHP_RCUTREE_PREP cpuhp_step callback.

Quoted 1 line

From the scheduler's perspective, a bit in cpu_online_mask means that the cpu

is visible to the scheduler. Furthermore, a bit in cpu_active_mask means that the cpu is suitable as a migration destination.

Now turning back to the case in rcu offlining. sched_cpu_deactivate() has disabled the dying cpu as the migration destination before rcutree_offline_cpu(). Furthermore, if the boost kthread is on the dying cpu, it will be migrated to another suitable online cpu by the scheduler. So the affinity setting by rcutree_offline_cpu() is redundant and can be eliminated.

Besides, this patch does an trival code rearrangement by unfolding rcutree_affinity_setting() into rcutree_online_cpu(), considering that the latter one is the only user of the former.

Cc "Paul E. McKenney" <[email protected]>
Cc David Woodhouse <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Neeraj Upadhyay <[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 "Jason A. Donenfeld" <[email protected]>

To: [email protected]

---
 kernel/rcu/tree.c | 14 +-------------
 1 file changed, 1 insertion(+), 13 deletions(-)
kernel/rcu/tree.c +0 -0
index 79aea7df4345..b90f6487fd45 100644
@@ -3978,16 +3978,6 @@ int rcutree_prepare_cpu(unsigned int cpu)
 	return 0;
 }

kernel/rcu/tree.c int rcutree_prepare_cpu(unsigned int cpu) · line 3989

Quoted 10 lines
kernel/rcu/tree.c +0 -9
 
-/*
- * Update RCU priority boot kthread affinity for CPU-hotplug changes.
- */
-static void rcutree_affinity_setting(unsigned int cpu, int outgoing)
-{
-	struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
-
-	rcu_boost_kthread_setaffinity(rdp->mynode, outgoing);
-}

Good point, tiven how simple a wrapper this is and how little it is used, getting rid of it does sound like a reasonable idea.

Quoted 16 lines
kernel/rcu/tree.c +1 -1
 /*
  * Near the end of the CPU-online process.  Pretty much all services
  * enabled, and the CPU is now very much alive.
@@ -4006,7 +3996,7 @@ int rcutree_online_cpu(unsigned int cpu)
 	if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
 		return 0; /* Too early in boot for scheduler work. */
 	sync_sched_exp_online_cleanup(cpu);
-	rcutree_affinity_setting(cpu, -1);
+	rcu_boost_kthread_setaffinity(rdp->mynode, -1);
 
 	// Stop-machine done, so allow nohz_full to disable tick.
 	tick_dep_clear(TICK_DEP_BIT_RCU);
@@ -4029,8 +4019,6 @@ int rcutree_offline_cpu(unsigned int cpu)
 	rnp->ffmask &= ~rdp->grpmask;
 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
 
Quoted 9 lines

kernel/rcu/tree.c int rcutree_offline_cpu(unsigned int cpu) · line 4032

Quoted 1 line
kernel/rcu/tree.c +0 -1
-	rcutree_affinity_setting(cpu, cpu);

We do need to keep this one because the CPU is going away.

One the other hand, it might well be that we could get rid of the call to rcutree_affinity_setting() in rcutree_dead_cpu().

Or am I missing something subtle here?

Oops, I think I need to rephrase my commit log to describe this nuance. The keypoint is whether ->qsmaskinitnext is stable.

The teardown code path on a single dying cpu looks like:

sched_cpu_deactivate() // prevent this dying cpu as a migration dst.

rcutree_offline_cpu() // as a result, the scheduler core will take care

                       // of the transient affinity mismatching until
		       // rcutree_dead_cpu(). (I think it also stands in
		       // the concurrent offlining)

rcu_report_dead() // running on the dying cpu, and clear its bit in ->qsmaskinitnext

rcutree_dead_cpu() // running on the initiator (a initiator cpu will

                       // execute this function for each dying cpu)
		       // At this point, ->qsmaskinitnext reflects the
		       // offlining, and the affinity can get right.

Sorry that my commit log had emphasized on the first part, but forgot to mention the ->qsmaskinitnext.

Does this justification stand?

Thanks,

Pingfan

maintainer
#6 lore raw
Quoted 81 lines · Pingfan Liu
Quoted 80 lines · Paul E. McKenney
Quoted 42 lines · Pingfan Liu

At present, during the cpu teardown, rcu_boost_kthread_setaffinity() is called twice. Firstly by rcutree_offline_cpu(), then by rcutree_dead_cpu() as the CPUHP_RCUTREE_PREP cpuhp_step callback.

Quoted 1 line

From the scheduler's perspective, a bit in cpu_online_mask means that the cpu

is visible to the scheduler. Furthermore, a bit in cpu_active_mask means that the cpu is suitable as a migration destination.

Now turning back to the case in rcu offlining. sched_cpu_deactivate() has disabled the dying cpu as the migration destination before rcutree_offline_cpu(). Furthermore, if the boost kthread is on the dying cpu, it will be migrated to another suitable online cpu by the scheduler. So the affinity setting by rcutree_offline_cpu() is redundant and can be eliminated.

Besides, this patch does an trival code rearrangement by unfolding rcutree_affinity_setting() into rcutree_online_cpu(), considering that the latter one is the only user of the former.

Cc "Paul E. McKenney" <[email protected]>
Cc David Woodhouse <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Neeraj Upadhyay <[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 "Jason A. Donenfeld" <[email protected]>

To: [email protected]

---
 kernel/rcu/tree.c | 14 +-------------
 1 file changed, 1 insertion(+), 13 deletions(-)
kernel/rcu/tree.c +0 -0
index 79aea7df4345..b90f6487fd45 100644
@@ -3978,16 +3978,6 @@ int rcutree_prepare_cpu(unsigned int cpu)
 	return 0;
 }

kernel/rcu/tree.c int rcutree_prepare_cpu(unsigned int cpu) · line 3989

Quoted 10 lines
kernel/rcu/tree.c +0 -9
 
-/*
- * Update RCU priority boot kthread affinity for CPU-hotplug changes.
- */
-static void rcutree_affinity_setting(unsigned int cpu, int outgoing)
-{
-	struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
-
-	rcu_boost_kthread_setaffinity(rdp->mynode, outgoing);
-}

Good point, tiven how simple a wrapper this is and how little it is used, getting rid of it does sound like a reasonable idea.

Quoted 13 lines
kernel/rcu/tree.c +1 -1
 /*
  * Near the end of the CPU-online process.  Pretty much all services
  * enabled, and the CPU is now very much alive.
@@ -4006,7 +3996,7 @@ int rcutree_online_cpu(unsigned int cpu)
 	if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
 		return 0; /* Too early in boot for scheduler work. */
 	sync_sched_exp_online_cleanup(cpu);
-	rcutree_affinity_setting(cpu, -1);
+	rcu_boost_kthread_setaffinity(rdp->mynode, -1);
 
 	// Stop-machine done, so allow nohz_full to disable tick.
 	tick_dep_clear(TICK_DEP_BIT_RCU);

kernel/rcu/tree.c int rcutree_offline_cpu(unsigned int cpu) · line 4032

Quoted 4 lines
kernel/rcu/tree.c +0 -1
@@ -4029,8 +4019,6 @@ int rcutree_offline_cpu(unsigned int cpu)
 	rnp->ffmask &= ~rdp->grpmask;
 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
 
-	rcutree_affinity_setting(cpu, cpu);

We do need to keep this one because the CPU is going away.

One the other hand, it might well be that we could get rid of the call to rcutree_affinity_setting() in rcutree_dead_cpu().

Quoted 8 lines
Quoted 1 line

Or am I missing something subtle here?

Oops, I think I need to rephrase my commit log to describe this nuance. The keypoint is whether ->qsmaskinitnext is stable.

The teardown code path on a single dying cpu looks like:

sched_cpu_deactivate() // prevent this dying cpu as a migration dst.

Suppose that this was the last CPU that the task was permitted to run on.

Quoted 12 lines

rcutree_offline_cpu() // as a result, the scheduler core will take care

                       // of the transient affinity mismatching until
		       // rcutree_dead_cpu(). (I think it also stands in
		       // the concurrent offlining)

rcu_report_dead() // running on the dying cpu, and clear its bit in ->qsmaskinitnext

rcutree_dead_cpu() // running on the initiator (a initiator cpu will

                       // execute this function for each dying cpu)
		       // At this point, ->qsmaskinitnext reflects the
		       // offlining, and the affinity can get right.
Quoted 5 lines

Sorry that my commit log had emphasized on the first part, but forgot to mention the ->qsmaskinitnext.

Does this justification stand?

We should ensure that the task's permitted set of CPUs always contained at least one online CPU. Unless I am missing something, your suggested change will sometimes end up with the task having no online CPUs in its mask.

So what am I missing here?

Thanx, Paul

#7 lore raw
Quoted 87 lines · Paul E. McKenney
Quoted 86 lines · Pingfan Liu
Quoted 73 lines · Paul E. McKenney
Quoted 42 lines · Pingfan Liu

At present, during the cpu teardown, rcu_boost_kthread_setaffinity() is called twice. Firstly by rcutree_offline_cpu(), then by rcutree_dead_cpu() as the CPUHP_RCUTREE_PREP cpuhp_step callback.

Quoted 1 line

From the scheduler's perspective, a bit in cpu_online_mask means that the cpu

is visible to the scheduler. Furthermore, a bit in cpu_active_mask means that the cpu is suitable as a migration destination.

Now turning back to the case in rcu offlining. sched_cpu_deactivate() has disabled the dying cpu as the migration destination before rcutree_offline_cpu(). Furthermore, if the boost kthread is on the dying cpu, it will be migrated to another suitable online cpu by the scheduler. So the affinity setting by rcutree_offline_cpu() is redundant and can be eliminated.

Besides, this patch does an trival code rearrangement by unfolding rcutree_affinity_setting() into rcutree_online_cpu(), considering that the latter one is the only user of the former.

Cc "Paul E. McKenney" <[email protected]>
Cc David Woodhouse <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Neeraj Upadhyay <[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 "Jason A. Donenfeld" <[email protected]>

To: [email protected]

---
 kernel/rcu/tree.c | 14 +-------------
 1 file changed, 1 insertion(+), 13 deletions(-)
kernel/rcu/tree.c +0 -0
index 79aea7df4345..b90f6487fd45 100644
@@ -3978,16 +3978,6 @@ int rcutree_prepare_cpu(unsigned int cpu)
 	return 0;
 }

kernel/rcu/tree.c int rcutree_prepare_cpu(unsigned int cpu) · line 3989

Quoted 10 lines
kernel/rcu/tree.c +0 -9
 
-/*
- * Update RCU priority boot kthread affinity for CPU-hotplug changes.
- */
-static void rcutree_affinity_setting(unsigned int cpu, int outgoing)
-{
-	struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
-
-	rcu_boost_kthread_setaffinity(rdp->mynode, outgoing);
-}

Good point, tiven how simple a wrapper this is and how little it is used, getting rid of it does sound like a reasonable idea.

Quoted 16 lines
kernel/rcu/tree.c +1 -1
 /*
  * Near the end of the CPU-online process.  Pretty much all services
  * enabled, and the CPU is now very much alive.
@@ -4006,7 +3996,7 @@ int rcutree_online_cpu(unsigned int cpu)
 	if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
 		return 0; /* Too early in boot for scheduler work. */
 	sync_sched_exp_online_cleanup(cpu);
-	rcutree_affinity_setting(cpu, -1);
+	rcu_boost_kthread_setaffinity(rdp->mynode, -1);
 
 	// Stop-machine done, so allow nohz_full to disable tick.
 	tick_dep_clear(TICK_DEP_BIT_RCU);
@@ -4029,8 +4019,6 @@ int rcutree_offline_cpu(unsigned int cpu)
 	rnp->ffmask &= ~rdp->grpmask;
 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
 
Quoted 8 lines

kernel/rcu/tree.c int rcutree_offline_cpu(unsigned int cpu) · line 4032

Quoted 1 line
kernel/rcu/tree.c +0 -1
-	rcutree_affinity_setting(cpu, cpu);

We do need to keep this one because the CPU is going away.

One the other hand, it might well be that we could get rid of the call to rcutree_affinity_setting() in rcutree_dead_cpu().

Or am I missing something subtle here?

Oops, I think I need to rephrase my commit log to describe this nuance. The keypoint is whether ->qsmaskinitnext is stable.

Quoted 7 lines
Quoted 3 lines

The teardown code path on a single dying cpu looks like:

sched_cpu_deactivate() // prevent this dying cpu as a migration dst.

Suppose that this was the last CPU that the task was permitted to run on.

Get your concern, please see the comment below.

Quoted 16 lines
Quoted 16 lines

rcutree_offline_cpu() // as a result, the scheduler core will take care

                       // of the transient affinity mismatching until
		       // rcutree_dead_cpu(). (I think it also stands in
		       // the concurrent offlining)

rcu_report_dead() // running on the dying cpu, and clear its bit in ->qsmaskinitnext

rcutree_dead_cpu() // running on the initiator (a initiator cpu will

                       // execute this function for each dying cpu)
		       // At this point, ->qsmaskinitnext reflects the
		       // offlining, and the affinity can get right.

Sorry that my commit log had emphasized on the first part, but forgot to mention the ->qsmaskinitnext.

Quoted 7 lines
Quoted 1 line

Does this justification stand?

We should ensure that the task's permitted set of CPUs always contained at least one online CPU. Unless I am missing something, your suggested change will sometimes end up with the task having no online CPUs in its mask.

Actually, the scheduler will pick up one online cpu for the boost thread.

On the last cpu, boost is subject to migration, and is pushed away by __balance_push_cpu_stop()

{
        if (task_rq(p) == rq && task_on_rq_queued(p)) {
                cpu = select_fallback_rq(rq->cpu, p);
                rq = __migrate_task(rq, &rf, p, cpu);
        }
}

Now, turning to select_fallback_rq(), inside that function, if p->cpus_ptr has no suitable cpus, then case cpuset or possible will make it point to cpuset or cpu_possible_mask. So finally, there is a valid cpu is picked up. (I have not found the online cpu there. The trick should hide elsewhere. But that rendered the same result as in v5.16, where rcu_boost_kthread_setaffinity()->cpumask_setall(cm))

But now, in 6.0, it changes into housekeeping_cpumask(HK_TYPE_RCU), which appeals for a more careful thinking.
If there is a cpuset for housekeeping so that select_fallback_rq() can pick up one from that cpuset?

To Frederic, could you show some light here and is it worth introducing such a cpuset so that select_fallback_rq() can pick up a cpu in housekeeping set in this case?

Anyway, as the last resort, TICK_DEP_BIT_RCU has already appealed for the awareness of the concurrent rcutree_offline_cpu(), the affinity setting could be done there if select_fallback_rq() can not work.

Thanks,

Pingfan

#8 lore raw
Quoted 25 lines · Pingfan Liu

On Sat, Sep 10, 2022 at 01:36:22PM -0700, Paul E. McKenney wrote:
Actually, the scheduler will pick up one online cpu for the boost thread.

On the last cpu, boost is subject to migration, and is pushed away by __balance_push_cpu_stop()

{
        if (task_rq(p) == rq && task_on_rq_queued(p)) {
                cpu = select_fallback_rq(rq->cpu, p);
                rq = __migrate_task(rq, &rf, p, cpu);
        }
}

Now, turning to select_fallback_rq(), inside that function, if p->cpus_ptr has no suitable cpus, then case cpuset or possible will make it point to cpuset or cpu_possible_mask. So finally, there is a valid cpu is picked up. (I have not found the online cpu there. The trick should hide elsewhere. But that rendered the same result as in v5.16, where rcu_boost_kthread_setaffinity()->cpumask_setall(cm))

But now, in 6.0, it changes into housekeeping_cpumask(HK_TYPE_RCU), which appeals for a more careful thinking.
If there is a cpuset for housekeeping so that select_fallback_rq() can pick up one from that cpuset?

Quoted 8 lines

To Frederic, could you show some light here and is it worth introducing such a cpuset so that select_fallback_rq() can pick up a cpu in housekeeping set in this case?

Anyway, as the last resort, TICK_DEP_BIT_RCU has already appealed for the awareness of the concurrent rcutree_offline_cpu(), the affinity setting could be done there if select_fallback_rq() can not work.

Here is the way I understand it: suppose the outgoing CPU was the last online one in this rcu node, in this case the migration performed in sched_cpu_deactivate() will find only one CPU in p->cpus_mask, but that CPU is now out of cpu_active_mask, so the task is affined by default to cpu_possible_mask.

So there is a short window between sched_cpu_deactivate() and rcutree_offline_cpu() where the task may run on all cpu_possible_mask. Does it matter? Maybe, I don't know...

After that we reach rcutree_offline_cpu() -> rcu_boost_kthread_setaffinity() that fails to fill the cpumask since the node is now empty. As the resulting cpumask is empty, it fills it as a last resort to the HK_TYPE_RCU housekeeping set which has to have at least 1 online CPU (nohz_full constraint). So the task is finally affine to that housekeeping set.

Like Paul said, I'd rather indeed remove the rcu_boost_kthread_setaffinity() call from rcutree_dead_cpu() as this one looks useless.

Thanks.

Quoted 4 lines

Thanks,

Pingfan

#9 lore raw
Quoted 36 lines · Frederic Weisbecker
Quoted 25 lines · Pingfan Liu

On Sat, Sep 10, 2022 at 01:36:22PM -0700, Paul E. McKenney wrote:
Actually, the scheduler will pick up one online cpu for the boost thread.

On the last cpu, boost is subject to migration, and is pushed away by __balance_push_cpu_stop()

{
        if (task_rq(p) == rq && task_on_rq_queued(p)) {
                cpu = select_fallback_rq(rq->cpu, p);
                rq = __migrate_task(rq, &rf, p, cpu);
        }
}

Now, turning to select_fallback_rq(), inside that function, if p->cpus_ptr has no suitable cpus, then case cpuset or possible will make it point to cpuset or cpu_possible_mask. So finally, there is a valid cpu is picked up. (I have not found the online cpu there. The trick should hide elsewhere. But that rendered the same result as in v5.16, where rcu_boost_kthread_setaffinity()->cpumask_setall(cm))

But now, in 6.0, it changes into housekeeping_cpumask(HK_TYPE_RCU), which appeals for a more careful thinking.
If there is a cpuset for housekeeping so that select_fallback_rq() can pick up one from that cpuset?

Quoted 8 lines

To Frederic, could you show some light here and is it worth introducing such a cpuset so that select_fallback_rq() can pick up a cpu in housekeeping set in this case?

Anyway, as the last resort, TICK_DEP_BIT_RCU has already appealed for the awareness of the concurrent rcutree_offline_cpu(), the affinity setting could be done there if select_fallback_rq() can not work.

Quoted 9 lines

Here is the way I understand it: suppose the outgoing CPU was the last online one in this rcu node, in this case the migration performed in sched_cpu_deactivate() will find only one CPU in p->cpus_mask, but that CPU is now out of cpu_active_mask, so the task is affined by default to cpu_possible_mask.

So there is a short window between sched_cpu_deactivate() and rcutree_offline_cpu() where the task may run on all cpu_possible_mask. Does it matter? Maybe, I don't know...

OK, then it is pointless to introduce a housekeeping cpuset.

Quoted 6 lines

After that we reach rcutree_offline_cpu() -> rcu_boost_kthread_setaffinity() that fails to fill the cpumask since the node is now empty. As the resulting cpumask is empty, it fills it as a last resort to the HK_TYPE_RCU housekeeping set which has to have at least 1 online CPU (nohz_full constraint). So the task is finally affine to that housekeeping set.

Yes, exactly.

Quoted 3 lines

Like Paul said, I'd rather indeed remove the rcu_boost_kthread_setaffinity() call from rcutree_dead_cpu() as this one looks useless.

OK, I will take that way.

Thanks to both of you for the generous help. I will bring up V2 soon.

Thanks,

Pingfan