mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] cpu: Reverse order of iteration in freeze_secondary_cpus
@ 2024-05-24 16:04 Stanislav Spassov
  2024-06-17 13:26 ` [tip: smp/core] cpu/hotplug: Reverse order of iteration in freeze_secondary_cpus() tip-bot2 for Stanislav Spassov
  0 siblings, 1 reply; 2+ messages in thread
From: Stanislav Spassov @ 2024-05-24 16:04 UTC (permalink / raw)
  To: linux-kernel
  Cc: Thomas Gleixner, Peter Zijlstra, David Woodhouse, James Gowans,
	Stanislav Spassov

Whenever CPU hotplug state callbacks are registered (cpuhp_setup_state),
the startup callback is invoked on CPUs that have already reached the
provided state in order of ascending CPU id. For symmetry, we change
freeze_secondary_cpus to iterate through the CPUs in opposite order,
so that the teardown callback invocations happen in order of descending
CPU id.

One case where this is known to make a difference is the current
implementation of these callbacks in arch/x86/events/intel/uncore.c:

- uncore_event_cpu_online: designates the first CPU it is invoked for
  on each package as the uncore event collector for that package

- uncore_event_cpu_offline: if the CPU being offlined is the event
  collector for its package, transfers that responsibility over to
  the next (by ascending CPU id) one in the same package

Without reversing the order of teardowns in freeze_secondary_cpus, the
latter ends up doing the ownership transfer work on every single CPU.
That work involves a synchronize_rcu call (in perf_pmu_migrate_context),
ultimately unnecessarily degrading the performance of the CPU offlining.

Signed-off-by: Stanislav Spassov <stanspas@amazon.de>
---
 kernel/cpu.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/kernel/cpu.c b/kernel/cpu.c
index cc4a8068747c..aaa8b7d5f93e 100644
--- a/kernel/cpu.c
+++ b/kernel/cpu.c
@@ -1942,7 +1942,10 @@ int freeze_secondary_cpus(int primary)
 	cpumask_clear(frozen_cpus);
 
 	pr_info("Disabling non-boot CPUs ...\n");
-	for_each_online_cpu(cpu) {
+	for (cpu = nr_cpu_ids - 1; cpu >= 0; cpu--) {
+		if (!cpu_online(cpu))
+			continue;
+
 		if (cpu == primary)
 			continue;
 

base-commit: 266e95786452d97f42dcb9a881bba223584b9648
-- 
2.40.1




Amazon Web Services Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [tip: smp/core] cpu/hotplug: Reverse order of iteration in freeze_secondary_cpus()
  2024-05-24 16:04 [PATCH] cpu: Reverse order of iteration in freeze_secondary_cpus Stanislav Spassov
@ 2024-06-17 13:26 ` tip-bot2 for Stanislav Spassov
  0 siblings, 0 replies; 2+ messages in thread
From: tip-bot2 for Stanislav Spassov @ 2024-06-17 13:26 UTC (permalink / raw)
  To: linux-tip-commits; +Cc: Stanislav Spassov, Thomas Gleixner, x86, linux-kernel

The following commit has been merged into the smp/core branch of tip:

Commit-ID:     fde78e4673afcb0bad382af8b81543476dc77655
Gitweb:        https://git.kernel.org/tip/fde78e4673afcb0bad382af8b81543476dc77655
Author:        Stanislav Spassov <stanspas@amazon.de>
AuthorDate:    Fri, 24 May 2024 16:04:49 
Committer:     Thomas Gleixner <tglx@linutronix.de>
CommitterDate: Mon, 17 Jun 2024 15:17:44 +02:00

cpu/hotplug: Reverse order of iteration in freeze_secondary_cpus()

Whenever CPU hotplug state callbacks are registered, the startup callback
is invoked on CPUs that have already reached the provided state in order of
ascending CPU IDs.

In freeze_secondary_cpus() the teardown of CPUs happens in the same are
invoked in the same order. This is known to make a difference is the
current implementation of these callbacks in arch/x86/events/intel/uncore.c:

 - uncore_event_cpu_online() designates the first CPU it is invoked for
   on each package as the uncore event collector for that package

 - uncore_event_cpu_offline() if the CPU being offlined is the event
   collector for its package, transfers that responsibility over to
   the next (by ascending CPU id) one in the same package

With the current order of CPU teardowns in freeze_secondary_cpus(), the
latter ends up doing the ownership transfer work on every single CPU.  That
work involves a synchronize_rcu() call, ultimately unnecessarily degrading
the performance of CPU offlining.

To address this make freeze_secondary_cpus() iterate through the CPUs in
reverse order, so that the teardown happens in order of descending CPU IDs.

[ tglx: Massage change log ]

Signed-off-by: Stanislav Spassov <stanspas@amazon.de>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/r/20240524160449.48594-1-stanspas@amazon.de

---
 kernel/cpu.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/cpu.c b/kernel/cpu.c
index 563877d..1979a99 100644
--- a/kernel/cpu.c
+++ b/kernel/cpu.c
@@ -1891,8 +1891,8 @@ int freeze_secondary_cpus(int primary)
 	cpumask_clear(frozen_cpus);
 
 	pr_info("Disabling non-boot CPUs ...\n");
-	for_each_online_cpu(cpu) {
-		if (cpu == primary)
+	for (cpu = nr_cpu_ids - 1; cpu >= 0; cpu--) {
+		if (!cpu_online(cpu) || cpu == primary)
 			continue;
 
 		if (pm_wakeup_pending()) {

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-06-17 13:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-24 16:04 [PATCH] cpu: Reverse order of iteration in freeze_secondary_cpus Stanislav Spassov
2024-06-17 13:26 ` [tip: smp/core] cpu/hotplug: Reverse order of iteration in freeze_secondary_cpus() tip-bot2 for Stanislav Spassov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®