From: Tom Lendacky <thomas.lendacky@amd.com>
To: Tony Battersby <tonyb@cybernetics.com>,
Thomas Gleixner <tglx@linutronix.de>,
Dave Hansen <dave.hansen@intel.com>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
x86@kernel.org
Cc: "H. Peter Anvin" <hpa@zytor.com>,
Mario Limonciello <mario.limonciello@amd.com>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
Andi Kleen <ak@linux.intel.com>
Subject: Re: [PATCH RFC] x86/cpu: fix intermittent lockup on poweroff
Date: Wed, 26 Apr 2023 12:51:00 -0500 [thread overview]
Message-ID: <01a44722-931a-7aff-4f4b-75e78855beb1@amd.com> (raw)
In-Reply-To: <ccf57fd2-45b8-1f1f-f46a-55d7f4c56161@cybernetics.com>
On 4/26/23 12:37, Tony Battersby wrote:
> On 4/26/23 12:37, Thomas Gleixner wrote:
>> The problem really seems to be that the control CPU goes off before the
>> other CPUs have finished and depending on timing that causes the
>> wreckage. Otherwise the mdelay(100) would not have helped at all.
>>
>> But looking at it, that num_online_cpus() == 1 check in
>> stop_other_cpus() is fragile as hell independent of that wbinvd() issue.
>>
>> Something like the completely untested below should cure that.
>>
>> Thanks,
>>
>> tglx
>> ---
>> arch/x86/include/asm/cpu.h | 2 ++
>> arch/x86/kernel/process.c | 10 ++++++++++
>> arch/x86/kernel/smp.c | 15 ++++++++++++---
>> 3 files changed, 24 insertions(+), 3 deletions(-)
>>
>> --- a/arch/x86/include/asm/cpu.h
>> +++ b/arch/x86/include/asm/cpu.h
>> @@ -98,4 +98,6 @@ extern u64 x86_read_arch_cap_msr(void);
>> int intel_find_matching_signature(void *mc, unsigned int csig, int cpf);
>> int intel_microcode_sanity_check(void *mc, bool print_err, int hdr_type);
>>
>> +extern atomic_t stop_cpus_count;
>> +
>> #endif /* _ASM_X86_CPU_H */
>> --- a/arch/x86/kernel/process.c
>> +++ b/arch/x86/kernel/process.c
>> @@ -752,6 +752,8 @@ bool xen_set_default_idle(void)
>> }
>> #endif
>>
>> +atomic_t stop_cpus_count;
>> +
>> void __noreturn stop_this_cpu(void *dummy)
>> {
>> local_irq_disable();
>> @@ -776,6 +778,14 @@ void __noreturn stop_this_cpu(void *dumm
>> */
>> if (cpuid_eax(0x8000001f) & BIT(0))
>> native_wbinvd();
>> +
>> + /*
>> + * native_stop_other_cpus() will write to @stop_cpus_count after
>> + * observing that it went down to zero, which will invalidate the
>> + * cacheline on this CPU.
>> + */
>> + atomic_dec(&stop_cpus_count);
This is probably going to pull in a cache line and cause the problem the
native_wbinvd() is trying to avoid.
Thanks,
Tom
>> +
>> for (;;) {
>> /*
>> * Use native_halt() so that memory contents don't change
>> --- a/arch/x86/kernel/smp.c
>> +++ b/arch/x86/kernel/smp.c
>> @@ -27,6 +27,7 @@
>> #include <asm/mmu_context.h>
>> #include <asm/proto.h>
>> #include <asm/apic.h>
>> +#include <asm/cpu.h>
>> #include <asm/idtentry.h>
>> #include <asm/nmi.h>
>> #include <asm/mce.h>
>> @@ -171,6 +172,8 @@ static void native_stop_other_cpus(int w
>> if (atomic_cmpxchg(&stopping_cpu, -1, safe_smp_processor_id()) != -1)
>> return;
>>
>> + atomic_set(&stop_cpus_count, num_online_cpus() - 1);
>> +
>> /* sync above data before sending IRQ */
>> wmb();
>>
>> @@ -183,12 +186,12 @@ static void native_stop_other_cpus(int w
>> * CPUs reach shutdown state.
>> */
>> timeout = USEC_PER_SEC;
>> - while (num_online_cpus() > 1 && timeout--)
>> + while (atomic_read(&stop_cpus_count) > 0 && timeout--)
>> udelay(1);
>> }
>>
>> /* if the REBOOT_VECTOR didn't work, try with the NMI */
>> - if (num_online_cpus() > 1) {
>> + if (atomic_read(&stop_cpus_count) > 0) {
>> /*
>> * If NMI IPI is enabled, try to register the stop handler
>> * and send the IPI. In any case try to wait for the other
>> @@ -208,7 +211,7 @@ static void native_stop_other_cpus(int w
>> * one or more CPUs do not reach shutdown state.
>> */
>> timeout = USEC_PER_MSEC * 10;
>> - while (num_online_cpus() > 1 && (wait || timeout--))
>> + while (atomic_read(&stop_cpus_count) > 0 && (wait || timeout--))
>> udelay(1);
>> }
>>
>> @@ -216,6 +219,12 @@ static void native_stop_other_cpus(int w
>> disable_local_APIC();
>> mcheck_cpu_clear(this_cpu_ptr(&cpu_info));
>> local_irq_restore(flags);
>> +
>> + /*
>> + * Ensure that the cache line is invalidated on the other CPUs. See
>> + * comment vs. SME in stop_this_cpu().
>> + */
>> + atomic_set(&stop_cpus_count, INT_MAX);
>> }
>>
>> /*
>>
> Tested-by: Tony Battersby <tonyb@cybernetics.com>
>
> 10 successful poweroffs in a row with wbinvd() enabled. As I mentioned
> before though, I don't have an AMD CPU to test the SME cache
> invalidation logic.
>
> I will reply with my patch with an updated title and description.
>
> Tony
>
>
next prev parent reply other threads:[~2023-04-26 17:51 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-25 19:26 Tony Battersby
2023-04-25 19:39 ` Borislav Petkov
2023-04-25 19:58 ` Tony Battersby
2023-04-25 20:03 ` Dave Hansen
2023-04-25 20:34 ` Dave Hansen
2023-04-25 21:06 ` Borislav Petkov
2023-04-25 21:05 ` Thomas Gleixner
2023-04-25 22:29 ` Dave Hansen
2023-04-25 23:00 ` Thomas Gleixner
2023-04-26 0:10 ` H. Peter Anvin
2023-04-26 14:45 ` Tony Battersby
2023-04-26 16:37 ` Thomas Gleixner
2023-04-26 17:37 ` Tony Battersby
2023-04-26 17:41 ` [PATCH v2] x86/cpu: fix SME test in stop_this_cpu() Tony Battersby
2023-05-22 14:07 ` [PATCH v2 RESEND] " Tony Battersby
2023-04-26 17:51 ` Tom Lendacky [this message]
2023-04-26 18:15 ` [PATCH RFC] x86/cpu: fix intermittent lockup on poweroff Dave Hansen
2023-04-26 19:18 ` Tom Lendacky
2023-04-26 22:02 ` Andi Kleen
2023-04-26 23:20 ` Thomas Gleixner
2023-04-26 20:00 ` Thomas Gleixner
2023-06-20 13:00 ` [tip: x86/core] x86/smp: Make stop_other_cpus() more robust tip-bot2 for Thomas Gleixner
2023-06-20 13:00 ` [tip: x86/core] x86/smp: Dont access non-existing CPUID leaf tip-bot2 for Tony Battersby
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=01a44722-931a-7aff-4f4b-75e78855beb1@amd.com \
--to=thomas.lendacky@amd.com \
--cc=ak@linux.intel.com \
--cc=bp@alien8.de \
--cc=dave.hansen@intel.com \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mario.limonciello@amd.com \
--cc=mingo@redhat.com \
--cc=tglx@linutronix.de \
--cc=tonyb@cybernetics.com \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome