Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1656285rwd; Thu, 15 Jun 2023 13:44:11 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ68zns8uz9z9x8u4uwy81zb5B09M0O7gymYOvBE6uypz47X4Mqzb4bTJVN/+Ouo1c7QzfxA X-Received: by 2002:a17:902:c106:b0:1af:d9d2:a234 with SMTP id 6-20020a170902c10600b001afd9d2a234mr207674pli.3.1686861851409; Thu, 15 Jun 2023 13:44:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686861851; cv=none; d=google.com; s=arc-20160816; b=n+VL74NGSd3Bom8OifWAP2x21QaNUpHMDbTTf6Kru0kSj1PnIfxdetBH/Zie9voSnM iDLc0fIkktNYDhTPeSA4qrSpMMVKefy7y2W/Nr1fCZ2AbRZhYzJLljZ2M30AD+GY8n7w +Jj833oqBAa9ApXu92iMlc58aOcFXW6I4sfHxjYzMn30a5J4T7V6W9g52dF7HSXlcJSn gJnRT0TsNHvoGNHcldV9upYBVxfEJhsbtlxHR2OSMeym473Z02Aggc8RSSV7W3ksu4nW AFQPHMgf+xStQc55OPpiyJ8zMZeAyEGV3Gv7D2Wlo+tVXmx19ynunEoFPLtXpKt13YgY ayGA== 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=sB20Kb5T4u+9i61NvPvEsJiORwRSTA74S2zqtl199PY=; b=jS4V3Bzu1lk+45GSBZ17uwMAl5JghlHR4axOV2zKp8ZgcB2U4zsSJTdyi8DHo0oeLa LuFRcaju7y39IzHLTxAQlMesp8pnUkVVWfQkJKCZbC5hS59JjfQAntpXy9hbzmoFbAjk YuKKbCk8WdolNMuWCZi9FFR5dUFtYfKlFJcswHaSxRgBzRzuvwOBFdBEWi+dqAIN6gKB YmJaLzbVvaBeKBV/tJW0L/qsJNSjo3ZI0/0NvIwCuyT00DOm9WyOLQL+sL4yYUrnXx8P PSknJH6bX/bb/aw0U1I5SE86+TzWKWOyuvs3WvMRs9V75H8DZ07LiB324+Jn/FN+0AbX 8tlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=BaHhWLU2; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=g4FohfeP; 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 n17-20020a170902e55100b001acb03ca5aasi14202398plf.612.2023.06.15.13.43.59; Thu, 15 Jun 2023 13:44:11 -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=BaHhWLU2; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=g4FohfeP; 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 S237966AbjFOUeU (ORCPT + 99 others); Thu, 15 Jun 2023 16:34:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45852 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230219AbjFOUeE (ORCPT ); Thu, 15 Jun 2023 16:34:04 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6DDA32726 for ; Thu, 15 Jun 2023 13:34:02 -0700 (PDT) Message-ID: <20230615193330.608657211@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686861241; 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=sB20Kb5T4u+9i61NvPvEsJiORwRSTA74S2zqtl199PY=; b=BaHhWLU2EShAROun32+ROuKfZitFcUI5v3U7bROdAZZ+ckMrySgLc6wkjRD9eMH/aRZymJ Nqhwos4Mm+AzCezgDOxqtjrLZIkwk77SeZceGfXdBeB5Bz2owgmnkxeIZZKR5CRUaktjRx vsdx3FEXH1wleZ9nHyTAdZsKxEe1h46OHVvifivtS7H2ZxNimLANOPFDsNz7r5ofpXafva 7k4HcrkVQim8S8pJbI7EoESkdShYAkJlZ0AWnrv2zoNfSvD6T4nb0At6k20gC/M0YRHm4m 32SoFhkjwOM6qjDr+NYT0+FJ2l+yV+udh3/8REXIkmtiCr68V4eOGYfHhx2JJQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686861241; 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=sB20Kb5T4u+9i61NvPvEsJiORwRSTA74S2zqtl199PY=; b=g4FohfePyTHWmZB41seYYWB41AN9TC1rgqtGNVATbK2B6nrMKeBT3NOlKk72yoPJY5Y5H5 fHJgAOsNIAGRrmAA== 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 , Ashok Raj Subject: [patch v3 7/7] x86/smp: Put CPUs into INIT on shutdown if possible References: <20230615190036.898273129@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Date: Thu, 15 Jun 2023 22:34:00 +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 Parking CPUs in a HLT loop is not completely safe vs. kexec() as HLT can resume execution due to NMI, SMI and MCE, which has the same issue as the MWAIT loop. Kicking the secondary CPUs into INIT makes this safe against NMI and SMI. A broadcast MCE will take the machine down, but a broadcast MCE which makes HLT resume and execute overwritten text, pagetables or data will end up in a disaster too. So chose the lesser of two evils and kick the secondary CPUs into INIT unless the system has installed special wakeup mechanisms which are not using INIT. Signed-off-by: Thomas Gleixner Reviewed-by: Ashok Raj --- V3: Renamed the function to smp_park_other_cpus_in_init() so it can be reused for crash eventually. --- arch/x86/include/asm/smp.h | 2 ++ arch/x86/kernel/smp.c | 39 ++++++++++++++++++++++++++++++++------- arch/x86/kernel/smpboot.c | 19 +++++++++++++++++++ 3 files changed, 53 insertions(+), 7 deletions(-) --- a/arch/x86/include/asm/smp.h +++ b/arch/x86/include/asm/smp.h @@ -139,6 +139,8 @@ void native_send_call_func_ipi(const str void native_send_call_func_single_ipi(int cpu); void x86_idle_thread_init(unsigned int cpu, struct task_struct *idle); +bool smp_park_other_cpus_in_init(void); + void smp_store_boot_cpu_info(void); void smp_store_cpu_info(int id); --- a/arch/x86/kernel/smp.c +++ b/arch/x86/kernel/smp.c @@ -131,7 +131,7 @@ static int smp_stop_nmi_callback(unsigne } /* - * this function calls the 'stop' function on all other CPUs in the system. + * Disable virtualization, APIC etc. and park the CPU in a HLT loop */ DEFINE_IDTENTRY_SYSVEC(sysvec_reboot) { @@ -172,13 +172,17 @@ static void native_stop_other_cpus(int w * 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 + * 3) If the system uses INIT/STARTUP for CPU bringup, then + * send all present CPUs an INIT vector, which brings them + * completely out of the way. * - * 4) Wait for all other CPUs to report that they reached the + * 4) If #3 is not possible and #2 timed out send an NMI to the + * CPUs which did not yet report + * + * 5) 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. + * #4 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 @@ -194,7 +198,7 @@ static void native_stop_other_cpus(int w /* * Don't wait longer than a second for IPI completion. The * wait request is not checked here because that would - * prevent an NMI shutdown attempt in case that not all + * prevent an NMI/INIT shutdown in case that not all * CPUs reach shutdown state. */ timeout = USEC_PER_SEC; @@ -202,7 +206,27 @@ static void native_stop_other_cpus(int w udelay(1); } - /* if the REBOOT_VECTOR didn't work, try with the NMI */ + /* + * Park all other CPUs in INIT including "offline" CPUs, if + * possible. That's a safe place where they can't resume execution + * of HLT and then execute the HLT loop from overwritten text or + * page tables. + * + * The only downside is a broadcast MCE, but up to the point where + * the kexec() kernel brought all APs online again an MCE will just + * make HLT resume and handle the MCE. The machine crashs and burns + * due to overwritten text, page tables and data. So there is a + * choice between fire and frying pan. The result is pretty much + * the same. Chose frying pan until x86 provides a sane mechanism + * to park a CPU. + */ + if (smp_park_other_cpus_in_init()) + goto done; + + /* + * If park with INIT was not possible and the REBOOT_VECTOR didn't + * take all secondary CPUs offline, try with the NMI. + */ if (!cpumask_empty(&cpus_stop_mask)) { /* * If NMI IPI is enabled, try to register the stop handler @@ -234,6 +258,7 @@ static void native_stop_other_cpus(int w udelay(1); } +done: local_irq_save(flags); disable_local_APIC(); mcheck_cpu_clear(this_cpu_ptr(&cpu_info)); --- a/arch/x86/kernel/smpboot.c +++ b/arch/x86/kernel/smpboot.c @@ -1465,6 +1465,25 @@ void arch_thaw_secondary_cpus_end(void) cache_aps_init(); } +bool smp_park_other_cpus_in_init(void) +{ + unsigned int cpu, this_cpu = smp_processor_id(); + unsigned int apicid; + + if (apic->wakeup_secondary_cpu_64 || apic->wakeup_secondary_cpu) + return false; + + for_each_present_cpu(cpu) { + if (cpu == this_cpu) + continue; + apicid = apic->cpu_present_to_apicid(cpu); + if (apicid == BAD_APICID) + continue; + send_init_sequence(apicid); + } + return true; +} + /* * Early setup to make printk work. */