Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1653155rwd; Thu, 15 Jun 2023 13:40:42 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6LatUhB2NgOP4r6F7FdUfrJQFpjDKP/l65vhVoReNAjaylSI2kUZle3bkjCFMn5Z43QqdC X-Received: by 2002:a05:6a00:16cf:b0:636:f5f4:5308 with SMTP id l15-20020a056a0016cf00b00636f5f45308mr254441pfc.7.1686861642155; Thu, 15 Jun 2023 13:40:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686861642; cv=none; d=google.com; s=arc-20160816; b=pgX+3ZXrQRaKX45q0WvUYxan41U8buxOzeGneKPa/SLlxDLkGIuXSsrgdEdWuSOCFu n1LKvgEbPf0wo6KvmJCfjhPoLUZhW17PAkhEmtyWKM8+RDnsqn7UYI9GnyKwcxdVquFW Oe0we7JwaWB5bDosnA8ldIp8sqNb57Ho7UWBpZMJrkDgkmsXoHrEVOu1D4aXADJDVgqX 9pl8gyLI5ZRykwXH90k+QnOuKcvn2VWR4dJ4v140O0F+CZeMuG0d3WLy7wA+3m5bdZYX BuNNsfx7WOYbBrwrOZruMQsPTThaNhR0ehHXYGxttqKKfSgq7lp5fVsaCvSQdudHXks8 Y/bA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:date:mime-version:references:subject:cc:to:from :dkim-signature:dkim-signature:message-id; bh=acEqf4q7Qtm/dIVEXjGN26S1eTXxFBpCuNJbm8wwWUk=; b=pWVmRdSyXjWwg/jbAGM3iNf9ooLw4rflwXiWvmtDBXI6YPxFqP6WXL4sxkjaBlQacr m7r5wrrHOH/bzbxcpUg6X1n/X8rPZvJ3uVrTlvrmWOROmPUry2DWoP4omo14oevASxwJ 8SnEjENjqp+NjNj2QwUbCvZewinxUvWfG875d9g8KNBL/rs1Cb4qW7xaOxK8vZvJNrYk XGtdx41EYM2sHOtzHXn/Hph1MXDwWN3GeXZyKKeCobXCdLFyuWVYR1LxjpkQzbmZWYLP cO0QqmA20hZlx6tUwhz6pnyDeNI8INmMGo7vBjuKzBc6OF8dbXxGdrsloElpUH4RDuff rK+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=wtYkDT5W; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q10-20020aa7842a000000b0064d3cf07fdcsi1095291pfn.88.2023.06.15.13.40.29; Thu, 15 Jun 2023 13:40:42 -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=@linutronix.de header.s=2020 header.b=wtYkDT5W; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232153AbjFOUd7 (ORCPT + 99 others); Thu, 15 Jun 2023 16:33:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45764 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231837AbjFOUdy (ORCPT ); Thu, 15 Jun 2023 16:33:54 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D3F21271E for ; Thu, 15 Jun 2023 13:33:52 -0700 (PDT) Message-ID: <20230615193330.263684884@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686861231; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=acEqf4q7Qtm/dIVEXjGN26S1eTXxFBpCuNJbm8wwWUk=; b=wtYkDT5Wp6cmzquQsLIDVXHRu82TcABoicmjPmIBCdv4bMY3rUr6L5NL4TdVubX56rzsp8 CQm0ipkWL7tBKZhiNUZ9kF8rGD70UM6+NB8N1JxmlNWUyWtxs4n9/wAeb1y2dcaJYPXcJ6 4W5TcaKUDXYBuI6EPmCtStHLc7GA0Mu1qy04W49HlwotirGkrJkPsW/ndRf9GADvx48Vqd wV+j/su6/vnHZ1aExpqUOvdGMM9D73RmxpBN0Mr/rpHWluywWIjiJZr+svPAScO77gnsqn Cowk7vti+/cWmE5gipPNLYhsxTOzd0mn+tYK6bPSuCd8fyjc1+VJWuCoWm4EZg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686861231; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=acEqf4q7Qtm/dIVEXjGN26S1eTXxFBpCuNJbm8wwWUk=; b=zkPF7gIjgtraA0L54z3AInrIrOMZ6k6R6IooaRpMMHKcIrF9KVYs2f10pRQy0BvTa6wtJK W0qnqkggPK+sXKCA== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Mario Limonciello , Tom Lendacky , Tony Battersby , Ashok Raj , Tony Luck , Arjan van de Veen , Eric Biederman Subject: [patch v3 1/7] x86/smp: Make stop_other_cpus() more robust References: <20230615190036.898273129@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Date: Thu, 15 Jun 2023 22:33:50 +0200 (CEST) X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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 Tony reported intermittent lockups on poweroff. His analysis identified the wbinvd() in stop_this_cpu() as the culprit. This was added to ensure that on SME enabled machines a kexec() does not leave any stale data in the caches when switching from encrypted to non-encrypted mode or vice versa. That wbindv() is conditional on the SME feature bit which is read directly from CPUID. But that readout does not check whether the CPUID leaf is available or not. If it's not available the CPU will return the value of the highest supported leaf instead. Depending on the content the "SME" bit might be set or not. That's incorrect but harmless. Making the CPUID readout conditional makes the observed hangs go away, but it does not fix the underlying problem: CPU0 CPU1 stop_other_cpus() send_IPIs(REBOOT); stop_this_cpu() while (num_online_cpus() > 1); set_online(false); proceed... -> hang wbinvd() WBINVD is an expensive operation and if multiple CPUs issue it at the same time the resulting delays are even larger. But CPU0 already observed num_online_cpus() going down to 1 and proceeds which causes the system to hang. This issue exists independent of WBINVD, but the delays caused by WBINVD make it more prominent. Make this more robust by adding a cpumask which is initialized to the online CPU mask before sending the IPIs and CPUs clear their bit in stop_this_cpu() after the WBINVD completed. Check for that cpumask to become empty in stop_other_cpus() instead of watching num_online_cpus(). The cpumask cannot plug all holes either, but it's better than a raw counter and allows to restrict the NMI fallback IPI to be sent only to the CPUs which have not reported within the timeout window. Fixes: 08f253ec3767 ("x86/cpu: Clear SME feature flag when not in use") Reported-by: Tony Battersby Signed-off-by: Thomas Gleixner Link: https://lore.kernel.org/all/3817d810-e0f1-8ef8-0bbd-663b919ca49b@cybernetics.com --- V3: Use a cpumask to make the NMI case slightly safer - Ashok --- arch/x86/include/asm/cpu.h | 2 + arch/x86/kernel/process.c | 23 +++++++++++++- arch/x86/kernel/smp.c | 71 +++++++++++++++++++++++++++++++-------------- 3 files changed, 73 insertions(+), 23 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 struct cpumask cpus_stop_mask; + #endif /* _ASM_X86_CPU_H */ --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -759,13 +759,23 @@ bool xen_set_default_idle(void) } #endif +struct cpumask cpus_stop_mask; + void __noreturn stop_this_cpu(void *dummy) { + unsigned int cpu = smp_processor_id(); + local_irq_disable(); + /* - * Remove this CPU: + * Remove this CPU from the online mask and disable it + * unconditionally. This might be redundant in case that the reboot + * vector was handled late and stop_other_cpus() sent an NMI. + * + * According to SDM and APM NMIs can be accepted even after soft + * disabling the local APIC. */ - set_cpu_online(smp_processor_id(), false); + set_cpu_online(cpu, false); disable_local_APIC(); mcheck_cpu_clear(this_cpu_ptr(&cpu_info)); @@ -783,6 +793,15 @@ void __noreturn stop_this_cpu(void *dumm */ if (cpuid_eax(0x8000001f) & BIT(0)) native_wbinvd(); + + /* + * This brings a cache line back and dirties it, but + * native_stop_other_cpus() will overwrite cpus_stop_mask after it + * observed that all CPUs reported stop. This write will invalidate + * the related cache line on this CPU. + */ + cpumask_clear_cpu(cpu, &cpus_stop_mask); + 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 @@ -146,31 +147,43 @@ static int register_stop_handler(void) static void native_stop_other_cpus(int wait) { - unsigned long flags; - unsigned long timeout; + unsigned int cpu = smp_processor_id(); + unsigned long flags, timeout; if (reboot_force) return; - /* - * Use an own vector here because smp_call_function - * does lots of things not suitable in a panic situation. - */ + /* Only proceed if this is the first CPU to reach this code */ + if (atomic_cmpxchg(&stopping_cpu, -1, cpu) != -1) + return; /* - * We start by using the REBOOT_VECTOR irq. - * The irq is treated as a sync point to allow critical - * regions of code on other cpus to release their spin locks - * and re-enable irqs. Jumping straight to an NMI might - * accidentally cause deadlocks with further shutdown/panic - * code. By syncing, we give the cpus up to one second to - * finish their work before we force them off with the NMI. + * 1) Send an IPI on the reboot vector to all other CPUs. + * + * The other CPUs should react on it after leaving critical + * sections and re-enabling interrupts. They might still hold + * locks, but there is nothing which can be done about that. + * + * 2) Wait for all other CPUs to report that they reached the + * HLT loop in stop_this_cpu() + * + * 3) If #2 timed out send an NMI to the CPUs which did not + * yet report + * + * 4) Wait for all other CPUs to report that they reached the + * HLT loop in stop_this_cpu() + * + * #3 can obviously race against a CPU reaching the HLT loop late. + * That CPU will have reported already and the "have all CPUs + * reached HLT" condition will be true despite the fact that the + * other CPU is still handling the NMI. Again, there is no + * protection against that as "disabled" APICs still respond to + * NMIs. */ - if (num_online_cpus() > 1) { - /* did someone beat us here? */ - if (atomic_cmpxchg(&stopping_cpu, -1, safe_smp_processor_id()) != -1) - return; + cpumask_copy(&cpus_stop_mask, cpu_online_mask); + cpumask_clear_cpu(cpu, &cpus_stop_mask); + if (!cpumask_empty(&cpus_stop_mask)) { /* sync above data before sending IRQ */ wmb(); @@ -183,24 +196,34 @@ static void native_stop_other_cpus(int w * CPUs reach shutdown state. */ timeout = USEC_PER_SEC; - while (num_online_cpus() > 1 && timeout--) + while (!cpumask_empty(&cpus_stop_mask) && timeout--) udelay(1); } /* if the REBOOT_VECTOR didn't work, try with the NMI */ - if (num_online_cpus() > 1) { + if (!cpumask_empty(&cpus_stop_mask)) { /* * If NMI IPI is enabled, try to register the stop handler * and send the IPI. In any case try to wait for the other * CPUs to stop. */ if (!smp_no_nmi_ipi && !register_stop_handler()) { + u32 dm; + /* Sync above data before sending IRQ */ wmb(); pr_emerg("Shutting down cpus with NMI\n"); - apic_send_IPI_allbutself(NMI_VECTOR); + dm = apic->dest_mode_logical ? APIC_DEST_LOGICAL : APIC_DEST_PHYSICAL; + dm |= APIC_DM_NMI; + + for_each_cpu(cpu, &cpus_stop_mask) { + u32 apicid = apic->cpu_present_to_apicid(cpu); + + apic_icr_write(dm, apicid); + apic_wait_icr_idle(); + } } /* * Don't wait longer than 10 ms if the caller didn't @@ -208,7 +231,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 (!cpumask_empty(&cpus_stop_mask) && (wait || timeout--)) udelay(1); } @@ -216,6 +239,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 cpus_stop_mask cache lines are invalidated on + * the other CPUs. See comment vs. SME in stop_this_cpu(). + */ + cpumask_clear(&cpus_stop_mask); } /*