public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] x86/aperfmperf: Fix deadlock on cpu_hotplug_lock
@ 2024-07-29 10:55 Jonathan Cameron
  2024-07-29 11:29 ` Borislav Petkov
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Jonathan Cameron @ 2024-07-29 10:55 UTC (permalink / raw)
  To: Mikhail Gavrilov, rafael.j.wysocki, catalin.marinas, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, H . Peter Anvin, Terry.bowman
  Cc: linuxarm, guohanjun, gshan, miguel.luis,
	Linux List Kernel Mailing, Linux regressions mailing list,
	shameerali.kolothum.thodi

The broken patch results in a call to init_freq_invariance_cppc() in a CPU
hotplug handler in both the path for initially present CPUs and those
hotplugged later.  That function includes a one time call to
amd_set_max_freq_ratio() which in turn calls freq_invariance_enable() that
has a static_branch_enable() which takes the cpu_hotlug_lock which is
already held.

Avoid the deadlock by using static_branch_enable_cpuslocked() as the lock
will always be already held.  The equivalent path on Intel does not
already hold this lock, so take it around the call to
freq_invariance_enable(), which results in it being held over the call to
register_syscall_ops, which looks to be safe to do.

Fixes: c1385c1f0ba3 ("ACPI: processor: Simplify initial onlining to use same path for cold and hotplug")
Reported-by: Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com>
Closes: https://lore.kernel.org/all/CABXGCsPvqBfL5hQDOARwfqasLRJ_eNPBbCngZ257HOe=xbWDkA@mail.gmail.com/
Suggested-by: Thomas Gleixner <tglx@linutronix.de>
Tested-by: Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
---
 arch/x86/kernel/cpu/aperfmperf.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kernel/cpu/aperfmperf.c b/arch/x86/kernel/cpu/aperfmperf.c
index b3fa61d45352..0b69bfbf345d 100644
--- a/arch/x86/kernel/cpu/aperfmperf.c
+++ b/arch/x86/kernel/cpu/aperfmperf.c
@@ -306,7 +306,7 @@ static void freq_invariance_enable(void)
 		WARN_ON_ONCE(1);
 		return;
 	}
-	static_branch_enable(&arch_scale_freq_key);
+	static_branch_enable_cpuslocked(&arch_scale_freq_key);
 	register_freq_invariance_syscore_ops();
 	pr_info("Estimated ratio of average max frequency by base frequency (times 1024): %llu\n", arch_max_freq_ratio);
 }
@@ -323,8 +323,10 @@ static void __init bp_init_freq_invariance(void)
 	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
 		return;
 
-	if (intel_set_max_freq_ratio())
+	if (intel_set_max_freq_ratio()) {
+		guard(cpus_read_lock)();
 		freq_invariance_enable();
+	}
 }
 
 static void disable_freq_invariance_workfn(struct work_struct *work)
-- 
2.43.0


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

* Re: [PATCH] x86/aperfmperf: Fix deadlock on cpu_hotplug_lock
  2024-07-29 10:55 [PATCH] x86/aperfmperf: Fix deadlock on cpu_hotplug_lock Jonathan Cameron
@ 2024-07-29 11:29 ` Borislav Petkov
  2024-07-29 11:40 ` Thomas Gleixner
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Borislav Petkov @ 2024-07-29 11:29 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Mikhail Gavrilov, rafael.j.wysocki, catalin.marinas, Ingo Molnar,
	Dave Hansen, x86, H . Peter Anvin, Terry.bowman, linuxarm,
	guohanjun, gshan, miguel.luis, Linux List Kernel Mailing,
	Linux regressions mailing list, shameerali.kolothum.thodi

On Mon, Jul 29, 2024 at 11:55:04AM +0100, Jonathan Cameron wrote:
> The broken patch results in a call to init_freq_invariance_cppc() in a CPU
> hotplug handler in both the path for initially present CPUs and those
> hotplugged later.  That function includes a one time call to
> amd_set_max_freq_ratio() which in turn calls freq_invariance_enable() that
> has a static_branch_enable() which takes the cpu_hotlug_lock which is
> already held.
> 
> Avoid the deadlock by using static_branch_enable_cpuslocked() as the lock
> will always be already held.  The equivalent path on Intel does not
> already hold this lock, so take it around the call to
> freq_invariance_enable(), which results in it being held over the call to
> register_syscall_ops, which looks to be safe to do.
> 
> Fixes: c1385c1f0ba3 ("ACPI: processor: Simplify initial onlining to use same path for cold and hotplug")
> Reported-by: Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com>
> Closes: https://lore.kernel.org/all/CABXGCsPvqBfL5hQDOARwfqasLRJ_eNPBbCngZ257HOe=xbWDkA@mail.gmail.com/
> Suggested-by: Thomas Gleixner <tglx@linutronix.de>
> Tested-by: Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com>
> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> ---
>  arch/x86/kernel/cpu/aperfmperf.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)

Tested-by: Borislav Petkov (AMD) <bp@alien8.de>

I'll take it through tip if no one objects...

Thx.

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette

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

* Re: [PATCH] x86/aperfmperf: Fix deadlock on cpu_hotplug_lock
  2024-07-29 10:55 [PATCH] x86/aperfmperf: Fix deadlock on cpu_hotplug_lock Jonathan Cameron
  2024-07-29 11:29 ` Borislav Petkov
@ 2024-07-29 11:40 ` Thomas Gleixner
  2024-07-29 13:51 ` [tip: x86/urgent] " tip-bot2 for Jonathan Cameron
  2024-07-30  0:59 ` [PATCH] " Gavin Shan
  3 siblings, 0 replies; 5+ messages in thread
From: Thomas Gleixner @ 2024-07-29 11:40 UTC (permalink / raw)
  To: Jonathan Cameron, Mikhail Gavrilov, rafael.j.wysocki,
	catalin.marinas, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	H . Peter Anvin, Terry.bowman
  Cc: linuxarm, guohanjun, gshan, miguel.luis,
	Linux List Kernel Mailing, Linux regressions mailing list,
	shameerali.kolothum.thodi

On Mon, Jul 29 2024 at 11:55, Jonathan Cameron wrote:
> The broken patch results in a call to init_freq_invariance_cppc() in a CPU
> hotplug handler in both the path for initially present CPUs and those
> hotplugged later.  That function includes a one time call to
> amd_set_max_freq_ratio() which in turn calls freq_invariance_enable() that
> has a static_branch_enable() which takes the cpu_hotlug_lock which is
> already held.
>
> Avoid the deadlock by using static_branch_enable_cpuslocked() as the lock
> will always be already held.  The equivalent path on Intel does not
> already hold this lock, so take it around the call to
> freq_invariance_enable(), which results in it being held over the call to
> register_syscall_ops, which looks to be safe to do.
>
> Fixes: c1385c1f0ba3 ("ACPI: processor: Simplify initial onlining to use same path for cold and hotplug")
> Reported-by: Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com>
> Closes: https://lore.kernel.org/all/CABXGCsPvqBfL5hQDOARwfqasLRJ_eNPBbCngZ257HOe=xbWDkA@mail.gmail.com/
> Suggested-by: Thomas Gleixner <tglx@linutronix.de>
> Tested-by: Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com>
> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

Reviewed-by: Thomas Gleixner <tglx@linutronix.de>

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

* [tip: x86/urgent] x86/aperfmperf: Fix deadlock on cpu_hotplug_lock
  2024-07-29 10:55 [PATCH] x86/aperfmperf: Fix deadlock on cpu_hotplug_lock Jonathan Cameron
  2024-07-29 11:29 ` Borislav Petkov
  2024-07-29 11:40 ` Thomas Gleixner
@ 2024-07-29 13:51 ` tip-bot2 for Jonathan Cameron
  2024-07-30  0:59 ` [PATCH] " Gavin Shan
  3 siblings, 0 replies; 5+ messages in thread
From: tip-bot2 for Jonathan Cameron @ 2024-07-29 13:51 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Mikhail Gavrilov, Thomas Gleixner, Jonathan Cameron,
	Borislav Petkov (AMD), x86, linux-kernel

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     0f7ced7d620ecc7a986843d6aeec41cce3116f41
Gitweb:        https://git.kernel.org/tip/0f7ced7d620ecc7a986843d6aeec41cce3116f41
Author:        Jonathan Cameron <Jonathan.Cameron@huawei.com>
AuthorDate:    Mon, 29 Jul 2024 11:55:04 +01:00
Committer:     Borislav Petkov (AMD) <bp@alien8.de>
CommitterDate: Mon, 29 Jul 2024 15:32:37 +02:00

x86/aperfmperf: Fix deadlock on cpu_hotplug_lock

The broken patch results in a call to init_freq_invariance_cppc() in a CPU
hotplug handler in both the path for initially present CPUs and those
hotplugged later.  That function includes a one time call to
amd_set_max_freq_ratio() which in turn calls freq_invariance_enable() that has
a static_branch_enable() which takes the cpu_hotlug_lock which is already
held.

Avoid the deadlock by using static_branch_enable_cpuslocked() as the lock will
always be already held.  The equivalent path on Intel does not already hold
this lock, so take it around the call to freq_invariance_enable(), which
results in it being held over the call to register_syscall_ops, which looks to
be safe to do.

Fixes: c1385c1f0ba3 ("ACPI: processor: Simplify initial onlining to use same path for cold and hotplug")
Closes: https://lore.kernel.org/all/CABXGCsPvqBfL5hQDOARwfqasLRJ_eNPBbCngZ257HOe=xbWDkA@mail.gmail.com/
Reported-by: Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com>
Suggested-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Tested-by: Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com>
Tested-by: Borislav Petkov (AMD) <bp@alien8.de>
Link: https://lore.kernel.org/r/20240729105504.2170-1-Jonathan.Cameron@huawei.com
---
 arch/x86/kernel/cpu/aperfmperf.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kernel/cpu/aperfmperf.c b/arch/x86/kernel/cpu/aperfmperf.c
index b3fa61d..0b69bfb 100644
--- a/arch/x86/kernel/cpu/aperfmperf.c
+++ b/arch/x86/kernel/cpu/aperfmperf.c
@@ -306,7 +306,7 @@ static void freq_invariance_enable(void)
 		WARN_ON_ONCE(1);
 		return;
 	}
-	static_branch_enable(&arch_scale_freq_key);
+	static_branch_enable_cpuslocked(&arch_scale_freq_key);
 	register_freq_invariance_syscore_ops();
 	pr_info("Estimated ratio of average max frequency by base frequency (times 1024): %llu\n", arch_max_freq_ratio);
 }
@@ -323,8 +323,10 @@ static void __init bp_init_freq_invariance(void)
 	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
 		return;
 
-	if (intel_set_max_freq_ratio())
+	if (intel_set_max_freq_ratio()) {
+		guard(cpus_read_lock)();
 		freq_invariance_enable();
+	}
 }
 
 static void disable_freq_invariance_workfn(struct work_struct *work)

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

* Re: [PATCH] x86/aperfmperf: Fix deadlock on cpu_hotplug_lock
  2024-07-29 10:55 [PATCH] x86/aperfmperf: Fix deadlock on cpu_hotplug_lock Jonathan Cameron
                   ` (2 preceding siblings ...)
  2024-07-29 13:51 ` [tip: x86/urgent] " tip-bot2 for Jonathan Cameron
@ 2024-07-30  0:59 ` Gavin Shan
  3 siblings, 0 replies; 5+ messages in thread
From: Gavin Shan @ 2024-07-30  0:59 UTC (permalink / raw)
  To: Jonathan Cameron, Mikhail Gavrilov, rafael.j.wysocki,
	catalin.marinas, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	H . Peter Anvin, Terry.bowman
  Cc: linuxarm, guohanjun, miguel.luis, Linux List Kernel Mailing,
	Linux regressions mailing list, shameerali.kolothum.thodi

On 7/29/24 8:55 PM, Jonathan Cameron wrote:
> The broken patch results in a call to init_freq_invariance_cppc() in a CPU
> hotplug handler in both the path for initially present CPUs and those
> hotplugged later.  That function includes a one time call to
> amd_set_max_freq_ratio() which in turn calls freq_invariance_enable() that
> has a static_branch_enable() which takes the cpu_hotlug_lock which is
> already held.
> 
> Avoid the deadlock by using static_branch_enable_cpuslocked() as the lock
> will always be already held.  The equivalent path on Intel does not
> already hold this lock, so take it around the call to
> freq_invariance_enable(), which results in it being held over the call to
> register_syscall_ops, which looks to be safe to do.
> 
> Fixes: c1385c1f0ba3 ("ACPI: processor: Simplify initial onlining to use same path for cold and hotplug")
> Reported-by: Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com>
> Closes: https://lore.kernel.org/all/CABXGCsPvqBfL5hQDOARwfqasLRJ_eNPBbCngZ257HOe=xbWDkA@mail.gmail.com/
> Suggested-by: Thomas Gleixner <tglx@linutronix.de>
> Tested-by: Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com>
> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> ---
>   arch/x86/kernel/cpu/aperfmperf.c | 6 ++++--
>   1 file changed, 4 insertions(+), 2 deletions(-)
> 

Reviewed-by: Gavin Shan <gshan@redhat.com>


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

end of thread, other threads:[~2024-07-30  0:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-29 10:55 [PATCH] x86/aperfmperf: Fix deadlock on cpu_hotplug_lock Jonathan Cameron
2024-07-29 11:29 ` Borislav Petkov
2024-07-29 11:40 ` Thomas Gleixner
2024-07-29 13:51 ` [tip: x86/urgent] " tip-bot2 for Jonathan Cameron
2024-07-30  0:59 ` [PATCH] " Gavin Shan

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