Received: by 2002:a05:7412:6592:b0:d7:7d3a:4fe2 with SMTP id m18csp585876rdg; Thu, 10 Aug 2023 12:18:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEf5o+lsjR9cmhWwIhXIXdzOQLwBYacfnzLs2seWPpL3YtTkn4y+6j+Y+DAvosEfSGVzzwR X-Received: by 2002:a17:902:d2c2:b0:1ae:6947:e63b with SMTP id n2-20020a170902d2c200b001ae6947e63bmr4268088plc.16.1691695092823; Thu, 10 Aug 2023 12:18:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691695092; cv=none; d=google.com; s=arc-20160816; b=xgTP9TorddUNJK9/HlvfuP7KPZkqo9+D7nn+EieqW5HRa7aLbtHVoeBHc0cmjukUmp /gErGXxSJVDlnTPS5M6MB9tHgb7LCMyWJP0HVM61QMR9w1KjhItH2Vtp8pfi9BhCpzOv tHIDxrfa1ROuXD6ek8C6WEjVQ8x2+LbrVe0tQ4bn+WzK5aNsBtpKbOilDwUeuWUyWqIH /PMjFSdOtO4R1R+e+Vir4tbytiVZmZ5tOg52XQqsmTOUPEG6IyNk9zAJr9LF+wnmOpEt Pl7lrYf89Mc6vfmf0zxr3d2GvfwZX4yXxyaW2cpY4w+SGtSGLF5QYF90/ecVl6Te+uBS pizA== 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=U6Q/Iq8+DIojIyZFEw1VWVmhyDeuqbI0eLU8eGojt5M=; fh=Vp72a/EEov1VOHpGPkCjAnDQEmhhYrQPa+PSfT8H2jQ=; b=ax8sgDE4xVYBCObeRyku0Cd0wzAl7ob9KT2RFuaGPZvUJFrTOjO+xL+XPNIfc7w6Pq dIGGG3eg4EJaQ5KhFv3RuSqkJPLnpyNnXAVdY/vutK+nIeQaFzXv73Wzi2NNVMiUHnvy GdlALJmDuMash+mxuY8JGx6KvZxYi5WYHbTN08OfaOTeHfJLx77kB2gH+YahhIuknw0E QYl6WyfK+H1Kl0YsJZybxHqgFif/Vbl42ZDEv8hQJDz3FCeNBLcAoidCaFnycf6+I53Z H+jCIXuT2DBYlYGde8IRf2Z79rKNJs1n1UztmP7Lwx3uDJSaTwKvgC0WVPaO2q42ivea AHCg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=MX4tsTwk; dkim=neutral (no key) header.i=@linutronix.de; 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 f7-20020a170902684700b001bdb3c09695si195960pln.222.2023.08.10.12.18.00; Thu, 10 Aug 2023 12:18:12 -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=MX4tsTwk; dkim=neutral (no key) header.i=@linutronix.de; 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 S235748AbjHJSj2 (ORCPT + 99 others); Thu, 10 Aug 2023 14:39:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55524 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235750AbjHJSjB (ORCPT ); Thu, 10 Aug 2023 14:39:01 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E0A432D57 for ; Thu, 10 Aug 2023 11:38:29 -0700 (PDT) Message-ID: <20230810160806.391887806@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1691692683; 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=U6Q/Iq8+DIojIyZFEw1VWVmhyDeuqbI0eLU8eGojt5M=; b=MX4tsTwk1iKTOdT3hHt/FFPLZ1KJXE89rgzIrWWzPOscnT3IGW6Y9WPD4hYxTXVuoWU/K9 a5SFwlhqyDnENmvcpC3KETFmzM7JaPncRwsIMn/745kFMU0vRtXiOjwT1Ao9tABpQAQK+Q 4YcZn9dpuKHQoY6sh1HZ167rjL0vysZyLm2Qm5eNd7t4GFVWt6mJVyUoSFOdOO2IctR63V V6U9qKHSGSFYjqmODBCxvaTitRh21wiWjFm0DEYJI2HWdTfpr2BMokbgZtdKGwsR1rUypb TZviRLstl5TwE51NMZveGLgYaemdhYgM4br4UI0RvDqOXaoBxPvi0vSE+pTJRg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1691692683; 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=U6Q/Iq8+DIojIyZFEw1VWVmhyDeuqbI0eLU8eGojt5M=; b=5bPI/tfYNRqWnxgsMO3Kha9gRvcDmt/WRprqS3mgs56kiKRg+iGAkq2egJ9QwJc8jCjf+i gZ1gQ3fbYX1GcjAg== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , Ashok Raj , Arjan van de Ven Subject: [patch 25/30] x86/microcode: Rendezvous and load in NMI References: <20230810153317.850017756@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Date: Thu, 10 Aug 2023 20:38:03 +0200 (CEST) X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS 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 From: Thomas Gleixner stop_machine() does not prevent the spin-waiting sibling from handling an NMI, which is obviously violating the whole concept of rendezvous. Implement a static branch right in the beginning of the NMI handler which is NOOPed except when enabled by the late loading mechanism. The later loader enables the static branch before stop_machine() is invoked. Each CPU has an nmi_enable in its control structure which indicates whether the CPU should go into the update routine. This is required to bridge the gap between enabling the branch and actually being at the point where it makes sense. Each CPU which arrives in the stopper thread function sets that flag and issues a self NMI right after that. If the NMI function sees the flag clear, it returns. If it's set it clears the flag and enters the rendezvous. This is safe against a real NMI which hits in between setting the flag and sending the NMI to itself. The real NMI will be swallowed by the microcode update and the self NMI will then let stuff continue. Otherwise this would end up with a spurious NMI. Signed-off-by: Thomas Gleixner --- arch/x86/include/asm/microcode.h | 12 ++++++++ arch/x86/kernel/cpu/microcode/core.c | 42 ++++++++++++++++++++++++++++--- arch/x86/kernel/cpu/microcode/intel.c | 1 arch/x86/kernel/cpu/microcode/internal.h | 3 +- arch/x86/kernel/nmi.c | 4 ++ 5 files changed, 57 insertions(+), 5 deletions(-) --- --- a/arch/x86/include/asm/microcode.h +++ b/arch/x86/include/asm/microcode.h @@ -78,4 +78,16 @@ void show_ucode_info_early(void); static inline void show_ucode_info_early(void) { } #endif /* !CONFIG_CPU_SUP_INTEL */ +bool microcode_nmi_handler(void); + +#ifdef CONFIG_MICROCODE_LATE_LOADING +DECLARE_STATIC_KEY_FALSE(microcode_nmi_handler_enable); +static __always_inline bool microcode_nmi_handler_enabled(void) +{ + return static_branch_unlikely(µcode_nmi_handler_enable); +} +#else +static __always_inline bool microcode_nmi_handler_enabled(void) { return false; } +#endif + #endif /* _ASM_X86_MICROCODE_H */ --- a/arch/x86/kernel/cpu/microcode/core.c +++ b/arch/x86/kernel/cpu/microcode/core.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -31,6 +32,7 @@ #include #include +#include #include #include #include @@ -334,8 +336,10 @@ struct ucode_ctrl { enum sibling_ctrl ctrl; enum ucode_state result; unsigned int ctrl_cpu; + bool nmi_enabled; }; +DEFINE_STATIC_KEY_FALSE(microcode_nmi_handler_enable); static DEFINE_PER_CPU(struct ucode_ctrl, ucode_ctrl); static atomic_t late_cpus_in; @@ -349,7 +353,8 @@ static bool wait_for_cpus(atomic_t *cnt) if (!atomic_read(cnt)) return true; udelay(1); - if (!(timeout % 1000)) + /* If invoked directly, tickle the NMI watchdog */ + if (!microcode_ops->use_nmi && !(timeout % 1000)) touch_nmi_watchdog(); } /* Prevent the late comers to make progress and let them time out */ @@ -365,7 +370,8 @@ static bool wait_for_ctrl(void) if (this_cpu_read(ucode_ctrl.ctrl) != SCTRL_WAIT) return true; udelay(1); - if (!(timeout % 1000)) + /* If invoked directly, tickle the NMI watchdog */ + if (!microcode_ops->use_nmi && !(timeout % 1000)) touch_nmi_watchdog(); } return false; @@ -443,7 +449,7 @@ static void ucode_load_primary(unsigned } } -static int ucode_load_cpus_stopped(void *unused) +static bool microcode_update_handler(void) { unsigned int cpu = smp_processor_id(); @@ -452,7 +458,29 @@ static int ucode_load_cpus_stopped(void else ucode_load_secondary(cpu); - /* No point to wait here. The CPUs will all wait in stop_machine(). */ + touch_nmi_watchdog(); + return true; +} + +bool microcode_nmi_handler(void) +{ + if (!this_cpu_read(ucode_ctrl.nmi_enabled)) + return false; + + this_cpu_write(ucode_ctrl.nmi_enabled, false); + return microcode_update_handler(); +} + +static int ucode_load_cpus_stopped(void *unused) +{ + if (microcode_ops->use_nmi) { + /* Enable the NMI handler and raise NMI */ + this_cpu_write(ucode_ctrl.nmi_enabled, true); + apic->send_IPI(smp_processor_id(), NMI_VECTOR); + } else { + /* Just invoke the handler directly */ + microcode_update_handler(); + } return 0; } @@ -473,8 +501,14 @@ static int ucode_load_late_stop_cpus(voi */ store_cpu_caps(&prev_info); + if (microcode_ops->use_nmi) + static_branch_enable_cpuslocked(µcode_nmi_handler_enable); + stop_machine_cpuslocked(ucode_load_cpus_stopped, NULL, cpu_online_mask); + if (microcode_ops->use_nmi) + static_branch_disable_cpuslocked(µcode_nmi_handler_enable); + /* Analyze the results */ for_each_cpu_and(cpu, cpu_present_mask, &cpus_booted_once_mask) { switch (per_cpu(ucode_ctrl.result, cpu)) { --- a/arch/x86/kernel/cpu/microcode/intel.c +++ b/arch/x86/kernel/cpu/microcode/intel.c @@ -698,6 +698,7 @@ static struct microcode_ops microcode_in .collect_cpu_info = collect_cpu_info, .apply_microcode = apply_microcode_late, .finalize_late_load = finalize_late_load, + .use_nmi = IS_ENABLED(CONFIG_X86_64), }; static __init void calc_llc_size_per_core(struct cpuinfo_x86 *c) --- a/arch/x86/kernel/cpu/microcode/internal.h +++ b/arch/x86/kernel/cpu/microcode/internal.h @@ -31,7 +31,8 @@ struct microcode_ops { enum ucode_state (*apply_microcode)(int cpu); int (*collect_cpu_info)(int cpu, struct cpu_signature *csig); void (*finalize_late_load)(int result); - unsigned int nmi_safe : 1; + unsigned int nmi_safe : 1, + use_nmi : 1; }; extern struct ucode_cpu_info ucode_cpu_info[]; --- a/arch/x86/kernel/nmi.c +++ b/arch/x86/kernel/nmi.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #define CREATE_TRACE_POINTS @@ -343,6 +344,9 @@ static noinstr void default_do_nmi(struc instrumentation_begin(); + if (microcode_nmi_handler_enabled() && microcode_nmi_handler()) + goto out; + handled = nmi_handle(NMI_LOCAL, regs); __this_cpu_add(nmi_stats.normal, handled); if (handled) {