Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp1190734rwr; Wed, 26 Apr 2023 11:10:57 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5F3XO+CU6NO7G1xjkSA8gUpC1ZPAoNdDnuDbRbCEaaKYWG+kJvJ7hS7kYjTMAEepDtIa+F X-Received: by 2002:a17:902:db0c:b0:1a6:846f:90cb with SMTP id m12-20020a170902db0c00b001a6846f90cbmr4682405plx.11.1682532657244; Wed, 26 Apr 2023 11:10:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682532657; cv=none; d=google.com; s=arc-20160816; b=by9Pi6LLwfyMQPXdgeG7jwqH4n0cCHxUH49zaoa92al2KZtVjr2E1FS6I1ImmhYlwt SlAOKGl8A50bAQzwXYboahsbZ8Wh+m3vRX2/YqmiPCloizzZrXS+5v94I+7Dfnoqd2Rf grVmTk3ze8Mldwkw1r/WRfjDoSSlVuB24P1hp2oBsmPK6FV1z6Az8inhHtGgPS/VM1A+ Qxvg2+Seh0xfeK3jOWtVlWdJ6y/6gMiauc47nqMpwUhqvZSqe4g+fsswJTblEw5A+AZ9 0GaI/qGtzjwtrrlGhuENw4cvr+3GJ3ahujLcbWCCDYMV7e+0/gJjdjVeCZyD3Rj9OyJU TRgg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=ATNwJSLFyohux/M/YpzBNZaYsaxhBJGvwGF+sS4vHgk=; b=ugO9QARSQJ7LUcsBY6x4ErxkNeJxU3+wo7kVScsLr+xEbVO6GjER54ettiYfG2Lt8d JZ4JwlrT+vsSprmNcwQtggiQGCVL6wsnh5agAwG1iiPG8V1cx3YbGxrip3o4Tzhz3fiJ fcnM5CRKPMW+2+vrKTj49sP66U2qVI3CPK9SXwsyektwzAjfWfbErYfsxSC+HY2+7q2f RBifeCr9W30KIjJE5scNF12zLFfF6y2qWW6GW8BlcYG2kCuYTas2bLcPMdiH6ER0/t73 eMhCTs/r0AN+29yj/aEUdDU1p0oWZNwtIxNb5BNksEjc92by1YTNvO4EY21JGcWAbeAn M4AA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cybernetics.com header.s=mail header.b=MryUhUcc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=cybernetics.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l7-20020a170903244700b0019b7766afdfsi13626435pls.626.2023.04.26.11.10.44; Wed, 26 Apr 2023 11:10:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@cybernetics.com header.s=mail header.b=MryUhUcc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=cybernetics.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234977AbjDZR5P (ORCPT + 99 others); Wed, 26 Apr 2023 13:57:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39940 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230043AbjDZR5N (ORCPT ); Wed, 26 Apr 2023 13:57:13 -0400 Received: from mail.cybernetics.com (mail.cybernetics.com [173.71.130.66]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B096F35AD for ; Wed, 26 Apr 2023 10:57:11 -0700 (PDT) X-ASG-Debug-ID: 1682530672-1cf43912153204b0001-xx1T2L Received: from cybernetics.com ([10.10.4.126]) by mail.cybernetics.com with ESMTP id Dn3l0AKuadEIoHu3; Wed, 26 Apr 2023 13:37:52 -0400 (EDT) X-Barracuda-Envelope-From: tonyb@cybernetics.com X-ASG-Whitelist: Client DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=cybernetics.com; s=mail; bh=ATNwJSLFyohux/M/YpzBNZaYsaxhBJGvwGF+sS4vHgk=; h=Content-Transfer-Encoding:Content-Type:In-Reply-To:From:References:Cc:To: Content-Language:Subject:MIME-Version:Date:Message-ID; b=MryUhUccxU/FrKL6qT7I 6c7ae0y2kKBTIwWvyMf93qaVYM1C6JcurHWJOGT7EX35E9hVvE+ykWJrqvMen2D7p9wXGAdIN/6kk HrdWXLruTVFztCUmGCPNMSeCLwZmY2CEpPllODlBw6u0XIuEpUqu3b/P0rg6akRxIq0dsWqkCU= Received: from [10.157.2.224] (HELO [192.168.200.1]) by cybernetics.com (CommuniGate Pro SMTP 7.1.1) with ESMTPS id 12591941; Wed, 26 Apr 2023 13:37:52 -0400 Message-ID: Date: Wed, 26 Apr 2023 13:37:52 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Subject: Re: [PATCH RFC] x86/cpu: fix intermittent lockup on poweroff Content-Language: en-US X-ASG-Orig-Subj: Re: [PATCH RFC] x86/cpu: fix intermittent lockup on poweroff To: Thomas Gleixner , Dave Hansen , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org Cc: "H. Peter Anvin" , Mario Limonciello , Tom Lendacky , "linux-kernel@vger.kernel.org" , Andi Kleen References: <3817d810-e0f1-8ef8-0bbd-663b919ca49b@cybernetics.com> <87o7nbzn8w.ffs@tglx> <5f8a9cb8-70cf-2a17-cfc4-cb31cb658de4@cybernetics.com> <87y1mey503.ffs@tglx> From: Tony Battersby In-Reply-To: <87y1mey503.ffs@tglx> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Barracuda-Connect: UNKNOWN[10.10.4.126] X-Barracuda-Start-Time: 1682530672 X-Barracuda-URL: https://10.10.4.122:443/cgi-mod/mark.cgi X-Virus-Scanned: by bsmtpd at cybernetics.com X-Barracuda-Scan-Msg-Size: 3908 X-Barracuda-BRTS-Status: 1 X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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); > + > 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 > #include > #include > +#include > #include > #include > #include > @@ -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 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