From patchwork Tue Sep 12 07:57:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138181 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp409033vqx; Tue, 12 Sep 2023 06:31:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEWAz9aoazh2Tb/3dACjWrfGDUULHmZe2acfJj/u8CI+ZBHBj66fG0iKiR3Jx/VURw9Eqir X-Received: by 2002:a05:6e02:214a:b0:34f:1de2:150a with SMTP id d10-20020a056e02214a00b0034f1de2150amr16442203ilv.9.1694525482221; Tue, 12 Sep 2023 06:31:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694525482; cv=none; d=google.com; s=arc-20160816; b=MG4aDlOCZL71NUKZfRQZ0t2k74UhF8d2jXPtAU7MjutURwydwqCAgvuc8PVuSFUCZ7 3yMKswvDfQkbc3Ln00fA5zhDebmTre0o45PSyCTKPW2kUq/JixFVSui12g8+NflYFIm2 dItIZVczeTqA3pe4RsigS8VskA8RHSFkFpNy7OirvSysIKEuHtpjmniJZbQWH3h5v2R0 zAwWyBS0/ngurKNCrm5+UW77OPhmxvUdMpKUS4LgSnmLcCosP0X3J2KUZVcMJl+5ahGC TSrdr9SFVfgAD8vDDremszyZQZUDH9tR1B5a7ZHGmtFbGP4SVubHqx7KCBUEFwJzqxt4 1RWQ== 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=jaKHr6eiysuu+qBYXUtlJbn5DXhl3rPJSwWAFNDS4aU=; fh=HxDzQ6PpfGskWPjhN1cA9fZ24R2R+GZAlvKaaxU0Ayo=; b=XyPa8IwDBX6Thk8SmCrVRceXqGQYDTD0lCm35Y9pu/9xAmYutCRBs13oF4Fs4bybxC bGf2vhGF1xO1S3E2OulAHFif45Db9ZpD7yYGDIrvHKdgpTytickZ26ExGtNXwTrUyUf5 O/BhlFymubDlcLRrE8MXAOkcv6i3xtJce3DKB6qdh14N4fDgRaxZWXLh1Qdi2CjqK1+R 8W5idJvc2HiGfuEUyuyd+veFilAI6TmKxzWzjlH84rVhKNCVH8PSh5JMp7tKLdRot5K5 RN8h7UNio7tgPlsI9Fx1/E73//OrHrBbh9yPJkZkcgxgQunhdQ0k4+MmM9aEYaRNAUcC bcgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=wd5uyino; 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::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id s3-20020a637703000000b00573fe811f14si7822401pgc.847.2023.09.12.06.31.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 06:31:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=wd5uyino; 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::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 4FEB080BEF27; Tue, 12 Sep 2023 00:58:36 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.8 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231867AbjILH55 (ORCPT + 39 others); Tue, 12 Sep 2023 03:57:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231921AbjILH5u (ORCPT ); Tue, 12 Sep 2023 03:57:50 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D204A10C3 for ; Tue, 12 Sep 2023 00:57:46 -0700 (PDT) Message-ID: <20230912065500.823314239@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505465; 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=jaKHr6eiysuu+qBYXUtlJbn5DXhl3rPJSwWAFNDS4aU=; b=wd5uyinoAXJtlAwc1/gUhJXMFtpSN02/3F8ENRAn43QD528NW/BhR+io1+meFRzccqOqDr K60L7RjA5giU/8rP7Baco89zTH77WFQme+NkVgSH9e5CTQJHQ378UxOgc026MG9VMyQA5m aYOnyrmFRgpHFGLQSXT8Gw0VUz0pOH3m/0BEKI3vPrL13Y+lnNURb8bS9pyL322McmEnKC OGt/HMIG7q9DLO+wbNVWvKEHqzZYJxvHSkIumbozrtLn5Bkch9DB0qq+ZeqCfpJU78OTa/ xQA9/fnIHfp+8TGo+qqIsR60luBo5v3SOTYBt3iTL75Pmiye70d0WRfMbkgaaQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505465; 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=jaKHr6eiysuu+qBYXUtlJbn5DXhl3rPJSwWAFNDS4aU=; b=o9whDX/taYtY/Cks6BdnIlG6cGIHTpcDiK1AQUv/VhTDVs9ruN9K27I104LJZv50FZHMrE b8TNPJbeH92/rEBQ== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov , Fenghua Yu , Peter Anvin Subject: [patch V3 01/30] x86/microcode/32: Move early loading after paging enable References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:57:44 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 12 Sep 2023 00:58:43 -0700 (PDT) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776838752145804848 X-GMAIL-MSGID: 1776838752145804848 32-bit loads microcode before paging is enabled. The commit which introduced that has zero justification in the changelog. The cover letter has slightly more content, but it does not give any technical justification either: "The problem in current microcode loading method is that we load a microcode way, way too late; ideally we should load it before turning paging on. This may only be practical on 32 bits since we can't get to 64-bit mode without paging on, but we should still do it as early as at all possible." Handwaving word salad with zero technical content. Someone claimed in an offlist conversation that this is required for curing the ATOM erratum AAE44/AAF40/AAG38/AAH41. That erratum requires an microcode update in order to make the usage of PSE safe. But during early boot PSE is completely irrelevant and it is evaluated way later. Neither is it relevant for the AP on single core HT enabled CPUs as the microcode loading on the AP is not doing anything. On dual core CPUs there is a theoretical problem if a split of an executable large page between enabling paging including PSE and loading the microcode happens. But that's only theoretical, it's practically irrelevant because the affected dual core CPUs are 64bit enabled and therefore have paging and PSE enabled before loading the microcode on the second core. So why would it work on 64-bit but not on 32-bit? The erratum: "AAG38 Code Fetch May Occur to Incorrect Address After a Large Page is Split Into 4-Kbyte Pages Problem: If software clears the PS (page size) bit in a present PDE (page directory entry), that will cause linear addresses mapped through this PDE to use 4-KByte pages instead of using a large page after old TLB entries are invalidated. Due to this erratum, if a code fetch uses this PDE before the TLB entry for the large page is invalidated then it may fetch from a different physical address than specified by either the old large page translation or the new 4-KByte page translation. This erratum may also cause speculative code fetches from incorrect addresses." The practical relevance for this is exactly zero because there is no splitting of large text pages during early boot-time, i.e. between paging enable and microcode loading, and neither during CPU hotplug. IOW, this load microcode before paging enable is yet another voodoo programming solution in search of a problem. What's worse is that it causes at least two serious problems: 1) When stackprotector is enabled then the microcode loader code has the stackprotector mechanics enabled. The read from the per CPU variable __stack_chk_guard is always accessing the virtual address either directly on UP or via FS on SMP. In physical address mode this results in an access to memory above 3GB. So this works by chance as the hardware returns the same value when there is no RAM at this physical address. When there is RAM populated above 3G then the read is by chance the same as nothing changes that memory during the very early boot stage. That's not necessarily true during runtime CPU hotplug. 2) When function tracing is enabled, then the relevant microcode loader functions and the functions invoked from there will call into the tracing code and evaluate global and per CPU variables in physical address mode. What could potentially go wrong? Cure this and move the microcode loading after the early paging enable and remove the gunk in the microcode loader which is required to handle physical address mode. Signed-off-by: Thomas Gleixner Cc: Fenghua Yu Cc: Peter Anvin Link: https://lore.kernel.org/lkml/1356075872-3054-1-git-send-email-fenghua.yu@intel.com --- arch/x86/include/asm/microcode.h | 5 - arch/x86/kernel/cpu/common.c | 12 --- arch/x86/kernel/cpu/microcode/amd.c | 31 +------- arch/x86/kernel/cpu/microcode/core.c | 73 ++++---------------- arch/x86/kernel/cpu/microcode/intel.c | 108 +++---------------------------- arch/x86/kernel/cpu/microcode/internal.h | 2 arch/x86/kernel/head32.c | 3 arch/x86/kernel/head_32.S | 10 -- arch/x86/kernel/smpboot.c | 12 +-- 9 files changed, 41 insertions(+), 215 deletions(-) --- a/arch/x86/include/asm/microcode.h +++ b/arch/x86/include/asm/microcode.h @@ -68,11 +68,6 @@ static inline u32 intel_get_microcode_re return rev; } - -void show_ucode_info_early(void); - -#else /* CONFIG_CPU_SUP_INTEL */ -static inline void show_ucode_info_early(void) { } #endif /* !CONFIG_CPU_SUP_INTEL */ #endif /* _ASM_X86_MICROCODE_H */ --- a/arch/x86/kernel/cpu/common.c +++ b/arch/x86/kernel/cpu/common.c @@ -2123,8 +2123,6 @@ static inline void setup_getcpu(int cpu) } #ifdef CONFIG_X86_64 -static inline void ucode_cpu_init(int cpu) { } - static inline void tss_setup_ist(struct tss_struct *tss) { /* Set up the per-CPU TSS IST stacks */ @@ -2135,16 +2133,8 @@ static inline void tss_setup_ist(struct /* Only mapped when SEV-ES is active */ tss->x86_tss.ist[IST_INDEX_VC] = __this_cpu_ist_top_va(VC); } - #else /* CONFIG_X86_64 */ - -static inline void ucode_cpu_init(int cpu) -{ - show_ucode_info_early(); -} - static inline void tss_setup_ist(struct tss_struct *tss) { } - #endif /* !CONFIG_X86_64 */ static inline void tss_setup_io_bitmap(struct tss_struct *tss) @@ -2200,8 +2190,6 @@ void cpu_init(void) struct task_struct *cur = current; int cpu = raw_smp_processor_id(); - ucode_cpu_init(cpu); - #ifdef CONFIG_NUMA if (this_cpu_read(numa_node) == 0 && early_cpu_to_node(cpu) != NUMA_NO_NODE) --- a/arch/x86/kernel/cpu/microcode/amd.c +++ b/arch/x86/kernel/cpu/microcode/amd.c @@ -456,14 +456,8 @@ static bool early_apply_microcode(u32 cp { struct cont_desc desc = { 0 }; struct microcode_amd *mc; - u32 rev, dummy, *new_rev; bool ret = false; - -#ifdef CONFIG_X86_32 - new_rev = (u32 *)__pa_nodebug(&ucode_new_rev); -#else - new_rev = &ucode_new_rev; -#endif + u32 rev, dummy; desc.cpuid_1_eax = cpuid_1_eax; @@ -484,8 +478,8 @@ static bool early_apply_microcode(u32 cp return ret; if (!__apply_microcode_amd(mc)) { - *new_rev = mc->hdr.patch_id; - ret = true; + ucode_new_rev = mc->hdr.patch_id; + ret = true; } return ret; @@ -514,26 +508,13 @@ static bool get_builtin_microcode(struct static void find_blobs_in_containers(unsigned int cpuid_1_eax, struct cpio_data *ret) { - struct ucode_cpu_info *uci; struct cpio_data cp; - const char *path; - bool use_pa; - - if (IS_ENABLED(CONFIG_X86_32)) { - uci = (struct ucode_cpu_info *)__pa_nodebug(ucode_cpu_info); - path = (const char *)__pa_nodebug(ucode_path); - use_pa = true; - } else { - uci = ucode_cpu_info; - path = ucode_path; - use_pa = false; - } if (!get_builtin_microcode(&cp, x86_family(cpuid_1_eax))) - cp = find_microcode_in_initrd(path, use_pa); + cp = find_microcode_in_initrd(ucode_path); /* Needed in load_microcode_amd() */ - uci->cpu_sig.sig = cpuid_1_eax; + ucode_cpu_info->cpu_sig.sig = cpuid_1_eax; *ret = cp; } @@ -562,7 +543,7 @@ int __init save_microcode_in_initrd_amd( enum ucode_state ret; struct cpio_data cp; - cp = find_microcode_in_initrd(ucode_path, false); + cp = find_microcode_in_initrd(ucode_path); if (!(cp.data && cp.size)) return -EINVAL; --- a/arch/x86/kernel/cpu/microcode/core.c +++ b/arch/x86/kernel/cpu/microcode/core.c @@ -90,10 +90,7 @@ static bool amd_check_current_patch_leve native_rdmsr(MSR_AMD64_PATCH_LEVEL, lvl, dummy); - if (IS_ENABLED(CONFIG_X86_32)) - levels = (u32 *)__pa_nodebug(&final_levels); - else - levels = final_levels; + levels = final_levels; for (i = 0; levels[i]; i++) { if (lvl == levels[i]) @@ -105,17 +102,8 @@ static bool amd_check_current_patch_leve static bool __init check_loader_disabled_bsp(void) { static const char *__dis_opt_str = "dis_ucode_ldr"; - -#ifdef CONFIG_X86_32 - const char *cmdline = (const char *)__pa_nodebug(boot_command_line); - const char *option = (const char *)__pa_nodebug(__dis_opt_str); - bool *res = (bool *)__pa_nodebug(&dis_ucode_ldr); - -#else /* CONFIG_X86_64 */ const char *cmdline = boot_command_line; const char *option = __dis_opt_str; - bool *res = &dis_ucode_ldr; -#endif /* * CPUID(1).ECX[31]: reserved for hypervisor use. This is still not @@ -123,17 +111,17 @@ static bool __init check_loader_disabled * that's good enough as they don't land on the BSP path anyway. */ if (native_cpuid_ecx(1) & BIT(31)) - return *res; + return true; if (x86_cpuid_vendor() == X86_VENDOR_AMD) { if (amd_check_current_patch_level()) - return *res; + return true; } if (cmdline_find_option_bool(cmdline, option) <= 0) - *res = false; + dis_ucode_ldr = false; - return *res; + return dis_ucode_ldr; } void __init load_ucode_bsp(void) @@ -171,20 +159,11 @@ void __init load_ucode_bsp(void) load_ucode_amd_early(cpuid_1_eax); } -static bool check_loader_disabled_ap(void) -{ -#ifdef CONFIG_X86_32 - return *((bool *)__pa_nodebug(&dis_ucode_ldr)); -#else - return dis_ucode_ldr; -#endif -} - void load_ucode_ap(void) { unsigned int cpuid_1_eax; - if (check_loader_disabled_ap()) + if (dis_ucode_ldr) return; cpuid_1_eax = native_cpuid_eax(1); @@ -226,40 +205,31 @@ static int __init save_microcode_in_init return ret; } -struct cpio_data find_microcode_in_initrd(const char *path, bool use_pa) +struct cpio_data find_microcode_in_initrd(const char *path) { #ifdef CONFIG_BLK_DEV_INITRD unsigned long start = 0; size_t size; #ifdef CONFIG_X86_32 - struct boot_params *params; - - if (use_pa) - params = (struct boot_params *)__pa_nodebug(&boot_params); - else - params = &boot_params; - - size = params->hdr.ramdisk_size; - + size = boot_params.hdr.ramdisk_size; /* * Set start only if we have an initrd image. We cannot use initrd_start * because it is not set that early yet. */ if (size) - start = params->hdr.ramdisk_image; + start = boot_params.hdr.ramdisk_image; -# else /* CONFIG_X86_64 */ +#else /* CONFIG_X86_64 */ size = (unsigned long)boot_params.ext_ramdisk_size << 32; size |= boot_params.hdr.ramdisk_size; if (size) { start = (unsigned long)boot_params.ext_ramdisk_image << 32; start |= boot_params.hdr.ramdisk_image; - start += PAGE_OFFSET; } -# endif +#endif /* * Fixup the start address: after reserve_initrd() runs, initrd_start @@ -270,23 +240,10 @@ struct cpio_data find_microcode_in_initr * initrd_gone is for the hotplug case where we've thrown out initrd * already. */ - if (!use_pa) { - if (initrd_gone) - return (struct cpio_data){ NULL, 0, "" }; - if (initrd_start) - start = initrd_start; - } else { - /* - * The picture with physical addresses is a bit different: we - * need to get the *physical* address to which the ramdisk was - * relocated, i.e., relocated_ramdisk (not initrd_start) and - * since we're running from physical addresses, we need to access - * relocated_ramdisk through its *physical* address too. - */ - u64 *rr = (u64 *)__pa_nodebug(&relocated_ramdisk); - if (*rr) - start = *rr; - } + if (initrd_gone) + return (struct cpio_data){ NULL, 0, "" }; + if (initrd_start) + start = initrd_start; return find_cpio_data(path, (void *)start, size, NULL); #else /* !CONFIG_BLK_DEV_INITRD */ --- a/arch/x86/kernel/cpu/microcode/intel.c +++ b/arch/x86/kernel/cpu/microcode/intel.c @@ -319,15 +319,8 @@ static void save_microcode_patch(struct if (!intel_find_matching_signature(p->data, uci->cpu_sig.sig, uci->cpu_sig.pf)) return; - /* - * Save for early loading. On 32-bit, that needs to be a physical - * address as the APs are running from physical addresses, before - * paging has been enabled. - */ - if (IS_ENABLED(CONFIG_X86_32)) - intel_ucode_patch = (struct microcode_intel *)__pa_nodebug(p->data); - else - intel_ucode_patch = p->data; + /* Save for early loading */ + intel_ucode_patch = p->data; } /* @@ -420,66 +413,10 @@ static bool load_builtin_intel_microcode return false; } -static void print_ucode_info(int old_rev, int new_rev, unsigned int date) -{ - pr_info_once("updated early: 0x%x -> 0x%x, date = %04x-%02x-%02x\n", - old_rev, - new_rev, - date & 0xffff, - date >> 24, - (date >> 16) & 0xff); -} - -#ifdef CONFIG_X86_32 - -static int delay_ucode_info; -static int current_mc_date; -static int early_old_rev; - -/* - * Print early updated ucode info after printk works. This is delayed info dump. - */ -void show_ucode_info_early(void) -{ - struct ucode_cpu_info uci; - - if (delay_ucode_info) { - intel_cpu_collect_info(&uci); - print_ucode_info(early_old_rev, uci.cpu_sig.rev, current_mc_date); - delay_ucode_info = 0; - } -} - -/* - * At this point, we can not call printk() yet. Delay printing microcode info in - * show_ucode_info_early() until printk() works. - */ -static void print_ucode(int old_rev, int new_rev, int date) -{ - int *delay_ucode_info_p; - int *current_mc_date_p; - int *early_old_rev_p; - - delay_ucode_info_p = (int *)__pa_nodebug(&delay_ucode_info); - current_mc_date_p = (int *)__pa_nodebug(¤t_mc_date); - early_old_rev_p = (int *)__pa_nodebug(&early_old_rev); - - *delay_ucode_info_p = 1; - *current_mc_date_p = date; - *early_old_rev_p = old_rev; -} -#else - -static inline void print_ucode(int old_rev, int new_rev, int date) -{ - print_ucode_info(old_rev, new_rev, date); -} -#endif - static int apply_microcode_early(struct ucode_cpu_info *uci, bool early) { struct microcode_intel *mc; - u32 rev, old_rev; + u32 rev, old_rev, date; mc = uci->mc; if (!mc) @@ -513,11 +450,9 @@ static int apply_microcode_early(struct uci->cpu_sig.rev = rev; - if (early) - print_ucode(old_rev, uci->cpu_sig.rev, mc->hdr.date); - else - print_ucode_info(old_rev, uci->cpu_sig.rev, mc->hdr.date); - + date = mc->hdr.date; + pr_info_once("updated early: 0x%x -> 0x%x, date = %04x-%02x-%02x\n", + old_rev, rev, date & 0xffff, date >> 24, (date >> 16) & 0xff); return 0; } @@ -535,7 +470,7 @@ int __init save_microcode_in_initrd_inte intel_ucode_patch = NULL; if (!load_builtin_intel_microcode(&cp)) - cp = find_microcode_in_initrd(ucode_path, false); + cp = find_microcode_in_initrd(ucode_path); if (!(cp.data && cp.size)) return 0; @@ -551,21 +486,11 @@ int __init save_microcode_in_initrd_inte */ static struct microcode_intel *__load_ucode_intel(struct ucode_cpu_info *uci) { - static const char *path; struct cpio_data cp; - bool use_pa; - - if (IS_ENABLED(CONFIG_X86_32)) { - path = (const char *)__pa_nodebug(ucode_path); - use_pa = true; - } else { - path = ucode_path; - use_pa = false; - } /* try built-in microcode first */ if (!load_builtin_intel_microcode(&cp)) - cp = find_microcode_in_initrd(path, use_pa); + cp = find_microcode_in_initrd(ucode_path); if (!(cp.data && cp.size)) return NULL; @@ -591,24 +516,15 @@ void __init load_ucode_intel_bsp(void) void load_ucode_intel_ap(void) { - struct microcode_intel *patch, **iup; struct ucode_cpu_info uci; - if (IS_ENABLED(CONFIG_X86_32)) - iup = (struct microcode_intel **) __pa_nodebug(&intel_ucode_patch); - else - iup = &intel_ucode_patch; - - if (!*iup) { - patch = __load_ucode_intel(&uci); - if (!patch) + if (!intel_ucode_patch) { + intel_ucode_patch = __load_ucode_intel(&uci); + if (!intel_ucode_patch) return; - - *iup = patch; } - uci.mc = *iup; - + uci.mc = intel_ucode_patch; apply_microcode_early(&uci, true); } --- a/arch/x86/kernel/cpu/microcode/internal.h +++ b/arch/x86/kernel/cpu/microcode/internal.h @@ -44,7 +44,7 @@ struct microcode_ops { }; extern struct ucode_cpu_info ucode_cpu_info[]; -struct cpio_data find_microcode_in_initrd(const char *path, bool use_pa); +struct cpio_data find_microcode_in_initrd(const char *path); #define MAX_UCODE_COUNT 128 --- a/arch/x86/kernel/head32.c +++ b/arch/x86/kernel/head32.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -34,6 +35,8 @@ asmlinkage __visible void __init __noret /* Make sure IDT is set up before any exception happens */ idt_setup_early_handler(); + load_ucode_bsp(); + cr4_init_shadow(); sanitize_boot_params(&boot_params); --- a/arch/x86/kernel/head_32.S +++ b/arch/x86/kernel/head_32.S @@ -118,11 +118,6 @@ SYM_CODE_START(startup_32) movl %eax, pa(olpc_ofw_pgd) #endif -#ifdef CONFIG_MICROCODE - /* Early load ucode on BSP. */ - call load_ucode_bsp -#endif - /* Create early pagetables. */ call mk_early_pgtbl_32 @@ -157,11 +152,6 @@ SYM_FUNC_START(startup_32_smp) movl %eax,%ss leal -__PAGE_OFFSET(%ecx),%esp -#ifdef CONFIG_MICROCODE - /* Early load ucode on AP. */ - call load_ucode_ap -#endif - .Ldefault_entry: movl $(CR0_STATE & ~X86_CR0_PG),%eax movl %eax,%cr0 --- a/arch/x86/kernel/smpboot.c +++ b/arch/x86/kernel/smpboot.c @@ -258,12 +258,9 @@ static void notrace start_secondary(void cpu_init_exception_handling(); /* - * 32-bit systems load the microcode from the ASM startup code for - * historical reasons. - * - * On 64-bit systems load it before reaching the AP alive - * synchronization point below so it is not part of the full per - * CPU serialized bringup part when "parallel" bringup is enabled. + * Load the microcode before reaching the AP alive synchronization + * point below so it is not part of the full per CPU serialized + * bringup part when "parallel" bringup is enabled. * * That's even safe when hyperthreading is enabled in the CPU as * the core code starts the primary threads first and leaves the @@ -276,8 +273,7 @@ static void notrace start_secondary(void * CPUID, MSRs etc. must be strictly serialized to maintain * software state correctness. */ - if (IS_ENABLED(CONFIG_X86_64)) - load_ucode_ap(); + load_ucode_ap(); /* * Synchronization point with the hotplug core. Sets this CPUs From patchwork Tue Sep 12 07:57:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138011 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp245606vqx; Tue, 12 Sep 2023 01:00:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGDisCi/QHhpXXdvcu9wIufyU+l7v9cLvL5+HD+iycISieA+qnaN0bG5BbTAS+c1pfKCVGk X-Received: by 2002:a17:902:ea0d:b0:1c3:2423:8e24 with SMTP id s13-20020a170902ea0d00b001c324238e24mr12248741plg.8.1694505644045; Tue, 12 Sep 2023 01:00:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694505644; cv=none; d=google.com; s=arc-20160816; b=iMqK8bCSzA2VRRmgWXGZB5GB8h9MtMsOwaAfwKfQKUhA16iVa+cBL00Mga9E2d6Tz9 UcKSf0UxGukeOBAhTwzmpRfy2SOcjssYRlGIriYPHB9VUt3IiYtBavqOsE6VWS27Z3mV 0ksnfrgKsVTyqN1/OcdeWt2Jf4RHnfJyTxomLXMWBNKW1EyFCAekizIMLBk3zOPftsuw 4UcvuIlwRUui44LijiGWd5QgILWs8cWxEQnirhLwsr0F2jFnGecRcYhR64pn7INEDUk5 XGxxfkvcBK6u+yk6fb+FTqrxaQ97q8tHSxL9pTXh11zLofmn3G7klX+nPH9OowVejmrK qNXg== 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=w4iWy4H04gKoxDxbBdcXl1DLzwa8M24xJYhbSoNX0o4=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=nnqjSf76JabOI97AcAUKbs0AlE4/OlgoCxIkfdDWRFZWHUEhQLvbh/ztgRgQpSBeQE jBINFNojS9JGz60cDIzzaNPwGp91McMCuuBMQojmbcCllgSEU6PCJtU2OCtBCf/D1WCH +qLZaudMPvna+VXjZWsfPuYW0oBrC0SzGCJDkeWEkVrZxu5D/GB3rHpRrF68uy4wYjdS RMwUD4mGNEdnZwOISTeB4E6UlwdvpByrq8SVvgQnhyoo0J0CumhKOwQLo0CAuGr05edy KBToGCVvd9uBn9+akm2+kUoJRkY9oOL9dbrUyORprYQNPhXuQV7m9VMddPO0jZkYuw2r Ozcw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=QVS7DXoH; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=fR4UnP7R; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id i4-20020a170902e48400b001b9c7300823si7305549ple.221.2023.09.12.01.00.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 01:00:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=QVS7DXoH; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=fR4UnP7R; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 0CA5E80298AF; Tue, 12 Sep 2023 00:59:18 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.8 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231887AbjILH57 (ORCPT + 39 others); Tue, 12 Sep 2023 03:57:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48124 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231571AbjILH5w (ORCPT ); Tue, 12 Sep 2023 03:57:52 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D808E7C for ; Tue, 12 Sep 2023 00:57:48 -0700 (PDT) Message-ID: <20230912065500.886599632@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505467; 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=w4iWy4H04gKoxDxbBdcXl1DLzwa8M24xJYhbSoNX0o4=; b=QVS7DXoHfiAtIMT7nPbf0KrP6V5vPE3x1jqj17hq83l58q7P0CiBSeqOYOSAgszoPRRM9J civFqb+6Pwy9vMgNKeOBngH+zSSwDoNC3ehWh2/r0E9Ek2LswhofmNnOanwBawgZN+WppN 4z0EyFvWIDdcTtbAZr8VTNinktpYQTJ3+rkO0eyI5dIMcCcJTl6t1ZN7xeRxgSpL+AycIp wwDP4veUqz305eFXyzEeWoOs4gN4zU8yJZfYjqOX/+o1vMfhW1U1MMFpo5xv1Z6eLQXRG5 SocWvTc/gCFwxStw5A7ApDzS5l6PNIva4RzMoR3CptQyOQbBDDBLrWY8k6UPgQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505467; 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=w4iWy4H04gKoxDxbBdcXl1DLzwa8M24xJYhbSoNX0o4=; b=fR4UnP7Rrd5eP0Du3qjVv8dtgpIH2R/xhVPFAjUnYJjXBL58MlfMA73V6Yj0AMk1m+0KcZ S9mGLs/V6hqSRBCQ== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 02/30] x86/boot/32: Disable stackprotector and tracing for mk_early_pgtbl_32() References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:57:46 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Tue, 12 Sep 2023 00:59:19 -0700 (PDT) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776817950180076022 X-GMAIL-MSGID: 1776817950180076022 Stackprotector cannot work before paging is enabled. The read from the per CPU variable __stack_chk_guard is always accessing the virtual address either directly on UP or via FS on SMP. In physical address mode this results in an access to memory above 3GB. So this works by chance as the hardware returns the same value when there is no RAM at this physical address. When there is RAM populated above 3G then the read is by chance the same as nothing changes that memory during the very early boot stage. Stop relying on pure luck and disable the stack protector for the only C function which is called during early boot before paging is enabled. Remove function tracing from the whole source file as there is no way to trace this at all, but in case of CONFIG_DYNAMIC_FTRACE=n mk_early_pgtbl_32() would access global function tracer variables in physcial address mode which again might work by chance. Signed-off-by: Thomas Gleixner --- arch/x86/kernel/Makefile | 1 + arch/x86/kernel/head32.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -16,6 +16,7 @@ CFLAGS_REMOVE_kvmclock.o = -pg CFLAGS_REMOVE_ftrace.o = -pg CFLAGS_REMOVE_early_printk.o = -pg CFLAGS_REMOVE_head64.o = -pg +CFLAGS_REMOVE_head32.o = -pg CFLAGS_REMOVE_sev.o = -pg CFLAGS_REMOVE_rethook.o = -pg endif --- a/arch/x86/kernel/head32.c +++ b/arch/x86/kernel/head32.c @@ -73,7 +73,8 @@ asmlinkage __visible void __init __noret * always zero at this stage. */ void __init mk_early_pgtbl_32(void); -void __init mk_early_pgtbl_32(void) + +void __init __no_stack_protector mk_early_pgtbl_32(void) { #ifdef __pa #undef __pa From patchwork Tue Sep 12 07:57:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138537 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp794243vqx; Tue, 12 Sep 2023 18:45:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEeCR+gBtaLAL3C8qvLf7dcGwAD8S90FIj2w9wnIPL5UnstoCnZYH2WxD0HQAcLCK7tqWlQ X-Received: by 2002:a05:6830:1bd7:b0:6bd:680:dc13 with SMTP id v23-20020a0568301bd700b006bd0680dc13mr1623693ota.21.1694569519391; Tue, 12 Sep 2023 18:45:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694569519; cv=none; d=google.com; s=arc-20160816; b=lQOvyw/X8qjk72beQf4PDzSGywT7D8+MGIx7i5asy2sedBNgFssn8reUUyR3V388vM cQaGi0uKO17jRIEBoWUkQC7c9gjjrpUpJniUV/uhGx4tRpYN2kduBpo1Fw0hwlrZNgBZ SEUBwHQcZ4YZSk6VQPmskFFg+Tzqdj+CHDWUBpK4/FXXGF3IPPN/fhT4LAUNyMdBesbO RWHGQGzg8zAGO41KvsYVUGFDFDHwHHDRkq2viyhBq7JNDYOIFZo3h7TuXK/H0dGKvWjZ ESgKuAbwcRcCcsVrszuBkE1bQuWApypi4R3y5ohWyngz14CviL8lWVLp3pEEYyYDATGz eczQ== 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=qRYc4KYBL4S/fSkVnA97uBorXZ9Ql1rG09MbkAiu0bU=; fh=Fw4dspu3lEWS7xf2bQx5+rNTgFONiGLilI8487/7QuM=; b=l1tUNVG4FSDZdXfn1XLABwR03+nNbVljxlq9iR/JtAMYaOqo9Cb7KSlbHWfKqydNwU 4Jh8RgsIETxujNwz8J5iODV79bRCaq2k3uEtxikhQ5K1ihWZe1cJQE/AmTOg+46lDyGW QHiLCRBP35fGjoOPHUFOibiv34W2MZuGpnLEH2cLJm1r4WrtUIELxIvLpppY8lA5566H zrgIwIuCaXbOFdlfeDoEQLs9VBSQwlgJ09cyZgUVP3A+LVXHk5nEBOtFbTKDecZw+omG x67V0OZG57uahdvgcF92AuNtNNcgjOdjtXoO14gGqFDgTcXa23lEoEaXhQokjYi5HxNm YIrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=i0oQhWKQ; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=mbT2ZyvA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id bv15-20020a056a00414f00b0068a676c5f2csi8940089pfb.32.2023.09.12.18.45.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 18:45:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=i0oQhWKQ; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=mbT2ZyvA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 47E00807758C; Tue, 12 Sep 2023 00:58:19 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.8 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231906AbjILH6D (ORCPT + 39 others); Tue, 12 Sep 2023 03:58:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48172 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231622AbjILH5y (ORCPT ); Tue, 12 Sep 2023 03:57: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 0574F10C6 for ; Tue, 12 Sep 2023 00:57:50 -0700 (PDT) Message-ID: <20230912065500.948602532@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505468; 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=qRYc4KYBL4S/fSkVnA97uBorXZ9Ql1rG09MbkAiu0bU=; b=i0oQhWKQFG9kbw5Sz3ZhXMknYYRF2yauDcHZ5bgwhKNJ1YLDClZiedHHt7FM0o707kCQA7 OnaZh2vF2j9Hk7XXQrcOsFxYhI+o7/92/F6X6RuSTupy6I/A4oIC1XvyrKhtmHdfl8EQVc jVlcb/XUg+HVpHQu0KKUU/392Kh7dfUFYlEh9RJUHBd7Cl77khCjhTBofzZynfT9bN4jTP /kLmeZEOJmf22h1CIfjxvC1XeNfEZpzYBfLjb1IPPuT41fffeVzM9U3uvN3bdT/b/ewQM8 2hzsSt31t7t0MNc4wWlPqnoNTVpt7PlrojU6mtKYFf+bnZKNC8PtCKsm/8NvDA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505468; 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=qRYc4KYBL4S/fSkVnA97uBorXZ9Ql1rG09MbkAiu0bU=; b=mbT2ZyvAchIl0ZD8qnVKaUXZVJdAX/92XnGxZ6IWZQ9zdXlrdf2Y605ECHz5mRz+8D4hHW 4u+bHBQruGVT2oDQ== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov , Ashok Raj Subject: [patch V3 03/30] x86/microcode/intel: Rip out mixed stepping support for Intel CPUs References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:57:48 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 12 Sep 2023 00:58:19 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776884928438623151 X-GMAIL-MSGID: 1776884928438623151 From: Ashok Raj Mixed steppings aren't supported on Intel CPUs. Only one patch is required for the entire system. The caching of micro code blobs which match the family and model is therefore pointless and in fact it is disfunctional as CPU hotplug updates use only a single microcode blob, i.e. the one where *intel_ucode_patch points to. Remove the microcode cache and make it an AMD local feature. [ tglx: Save only at the end. Otherwise random microcode ends up in the pointer for early loading ] Originally-by: Thomas Gleixner Signed-off-by: Ashok Raj Signed-off-by: Thomas Gleixner --- V2: Fix the bogus condition - Borislav --- arch/x86/kernel/cpu/microcode/amd.c | 10 ++ arch/x86/kernel/cpu/microcode/core.c | 2 arch/x86/kernel/cpu/microcode/intel.c | 133 +++++-------------------------- arch/x86/kernel/cpu/microcode/internal.h | 10 -- 4 files changed, 35 insertions(+), 120 deletions(-) --- --- a/arch/x86/kernel/cpu/microcode/amd.c +++ b/arch/x86/kernel/cpu/microcode/amd.c @@ -37,6 +37,16 @@ #include "internal.h" +struct ucode_patch { + struct list_head plist; + void *data; + unsigned int size; + u32 patch_id; + u16 equiv_cpu; +}; + +static LIST_HEAD(microcode_cache); + #define UCODE_MAGIC 0x00414d44 #define UCODE_EQUIV_CPU_TABLE_TYPE 0x00000000 #define UCODE_UCODE_TYPE 0x00000001 --- a/arch/x86/kernel/cpu/microcode/core.c +++ b/arch/x86/kernel/cpu/microcode/core.c @@ -46,8 +46,6 @@ static bool dis_ucode_ldr = true; bool initrd_gone; -LIST_HEAD(microcode_cache); - /* * Synchronization. * --- a/arch/x86/kernel/cpu/microcode/intel.c +++ b/arch/x86/kernel/cpu/microcode/intel.c @@ -33,10 +33,10 @@ static const char ucode_path[] = "kernel/x86/microcode/GenuineIntel.bin"; /* Current microcode patch used in early patching on the APs. */ -static struct microcode_intel *intel_ucode_patch; +static struct microcode_intel *intel_ucode_patch __read_mostly; /* last level cache size per core */ -static int llc_size_per_core; +static int llc_size_per_core __ro_after_init; /* microcode format is extended from prescott processors */ struct extended_signature { @@ -253,74 +253,19 @@ static int has_newer_microcode(void *mc, return intel_find_matching_signature(mc, csig, cpf); } -static struct ucode_patch *memdup_patch(void *data, unsigned int size) +static void save_microcode_patch(void *data, unsigned int size) { - struct ucode_patch *p; + struct microcode_header_intel *p; - p = kzalloc(sizeof(struct ucode_patch), GFP_KERNEL); - if (!p) - return NULL; - - p->data = kmemdup(data, size, GFP_KERNEL); - if (!p->data) { - kfree(p); - return NULL; - } - - return p; -} - -static void save_microcode_patch(struct ucode_cpu_info *uci, void *data, unsigned int size) -{ - struct microcode_header_intel *mc_hdr, *mc_saved_hdr; - struct ucode_patch *iter, *tmp, *p = NULL; - bool prev_found = false; - unsigned int sig, pf; - - mc_hdr = (struct microcode_header_intel *)data; - - list_for_each_entry_safe(iter, tmp, µcode_cache, plist) { - mc_saved_hdr = (struct microcode_header_intel *)iter->data; - sig = mc_saved_hdr->sig; - pf = mc_saved_hdr->pf; - - if (intel_find_matching_signature(data, sig, pf)) { - prev_found = true; - - if (mc_hdr->rev <= mc_saved_hdr->rev) - continue; - - p = memdup_patch(data, size); - if (!p) - pr_err("Error allocating buffer %p\n", data); - else { - list_replace(&iter->plist, &p->plist); - kfree(iter->data); - kfree(iter); - } - } - } - - /* - * There weren't any previous patches found in the list cache; save the - * newly found. - */ - if (!prev_found) { - p = memdup_patch(data, size); - if (!p) - pr_err("Error allocating buffer for %p\n", data); - else - list_add_tail(&p->plist, µcode_cache); - } + kfree(intel_ucode_patch); + intel_ucode_patch = NULL; + p = kmemdup(data, size, GFP_KERNEL); if (!p) return; - if (!intel_find_matching_signature(p->data, uci->cpu_sig.sig, uci->cpu_sig.pf)) - return; - /* Save for early loading */ - intel_ucode_patch = p->data; + intel_ucode_patch = (struct microcode_intel *)p; } /* @@ -332,6 +277,7 @@ scan_microcode(void *data, size_t size, { struct microcode_header_intel *mc_header; struct microcode_intel *patch = NULL; + u32 cur_rev = uci->cpu_sig.rev; unsigned int mc_size; while (size) { @@ -341,8 +287,7 @@ scan_microcode(void *data, size_t size, mc_header = (struct microcode_header_intel *)data; mc_size = get_totalsize(mc_header); - if (!mc_size || - mc_size > size || + if (!mc_size || mc_size > size || intel_microcode_sanity_check(data, false, MC_HEADER_TYPE_MICROCODE) < 0) break; @@ -354,31 +299,16 @@ scan_microcode(void *data, size_t size, continue; } - if (save) { - save_microcode_patch(uci, data, mc_size); + /* BSP scan: Check whether there is newer microcode */ + if (!save && cur_rev >= mc_header->rev) goto next; - } - - if (!patch) { - if (!has_newer_microcode(data, - uci->cpu_sig.sig, - uci->cpu_sig.pf, - uci->cpu_sig.rev)) - goto next; - - } else { - struct microcode_header_intel *phdr = &patch->hdr; - - if (!has_newer_microcode(data, - phdr->sig, - phdr->pf, - phdr->rev)) - goto next; - } + /* Save scan: Check whether there is newer or matching microcode */ + if (save && cur_rev != mc_header->rev) + goto next; - /* We have a newer patch, save it. */ patch = data; + cur_rev = mc_header->rev; next: data += mc_size; @@ -387,6 +317,9 @@ scan_microcode(void *data, size_t size, if (size) return NULL; + if (save && patch) + save_microcode_patch(patch, mc_size); + return patch; } @@ -528,26 +461,10 @@ void load_ucode_intel_ap(void) apply_microcode_early(&uci, true); } -static struct microcode_intel *find_patch(struct ucode_cpu_info *uci) +/* Accessor for microcode pointer */ +static struct microcode_intel *ucode_get_patch(void) { - struct microcode_header_intel *phdr; - struct ucode_patch *iter, *tmp; - - list_for_each_entry_safe(iter, tmp, µcode_cache, plist) { - - phdr = (struct microcode_header_intel *)iter->data; - - if (phdr->rev <= uci->cpu_sig.rev) - continue; - - if (!intel_find_matching_signature(phdr, - uci->cpu_sig.sig, - uci->cpu_sig.pf)) - continue; - - return iter->data; - } - return NULL; + return intel_ucode_patch; } void reload_ucode_intel(void) @@ -557,7 +474,7 @@ void reload_ucode_intel(void) intel_cpu_collect_info(&uci); - p = find_patch(&uci); + p = ucode_get_patch(); if (!p) return; @@ -601,7 +518,7 @@ static enum ucode_state apply_microcode_ return UCODE_ERROR; /* Look for a newer patch in our cache: */ - mc = find_patch(uci); + mc = ucode_get_patch(); if (!mc) { mc = uci->mc; if (!mc) @@ -730,7 +647,7 @@ static enum ucode_state generic_load_mic uci->mc = (struct microcode_intel *)new_mc; /* Save for CPU hotplug */ - save_microcode_patch(uci, new_mc, new_mc_size); + save_microcode_patch(new_mc, new_mc_size); pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n", cpu, new_rev, uci->cpu_sig.rev); --- a/arch/x86/kernel/cpu/microcode/internal.h +++ b/arch/x86/kernel/cpu/microcode/internal.h @@ -8,16 +8,6 @@ #include #include -struct ucode_patch { - struct list_head plist; - void *data; /* Intel uses only this one */ - unsigned int size; - u32 patch_id; - u16 equiv_cpu; -}; - -extern struct list_head microcode_cache; - struct device; enum ucode_state { From patchwork Tue Sep 12 07:57:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138141 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp369589vqx; Tue, 12 Sep 2023 05:28:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGvchBfzyfeE1e1Qifh5EQwIGHRoQ0i8PNfnRxdbWUdEbj679dm7JitOGQacTkgfINBS5ll X-Received: by 2002:a17:90a:d597:b0:271:90d6:f335 with SMTP id v23-20020a17090ad59700b0027190d6f335mr11978127pju.25.1694521727923; Tue, 12 Sep 2023 05:28:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694521727; cv=none; d=google.com; s=arc-20160816; b=AdNuxmgBnC2MEDTt0LqRDPMHbHbg/aa+6vl4RiSdgynQUi6MXB/D5pALwkHAGbEgha E6E35JSb/0JL2WRwueZ+HZXd2yoWEUm16h5TaF2X7Srwiv485H6melX6xEHjBPAUGm+s oZNd9ZqF31eC05agCzGjzlWNhNZoRYmY0ghdeQnbV9TmxTgZT854k6Ph8TtZsDe9TWP6 d8RwitssEL9LmDBebwm8ZXxBee15++HBeju+ZL7qBoCDiXstqUb8i2VjyNsr3ZxDik/v tm8af2k+W1km/+2fKG5boq2LieTAytkaJGIHszPN5eiwWcDouhUultuj74Yj+AneZZwl eDmw== 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=sOJ+sRQt3nfyL5UePtEvx89xEa11XaUFA1X2Va9tGj4=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=KF9CjpGceBFJlLkWn+Oh3skPuHWTE5Qf7tQ0EBjkWReH7hl6hYhDLh97jxx5ryHtW7 dLYGhxhgtPiHi3x29VZQi0hZ7Z5SuPgydOs7hVCjGcOFD1v4h8lFZCkbq6AdTGWWYpD2 bSZAbRnozzK9FqZtFaEMyvYcD9OLCWr4ajZQVez2QX8CuLzxfL/3y4gl1v+f1Em0W1/T UZNAhWXcKgPkVdJstE9gQYTUfSOKDCr8cqrxbr8qX6vJgFWp4c0kJru6JVfjT2PNynoj 4A7vFVWqZgFxKsAjgT8XehIxfslIKBIojXlv5sjw5OEknbkWYVhoYvMEYjSP64zcyMyG zjwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=VC+m64f1; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id b12-20020a170902bd4c00b001bf520e829csi7692501plx.49.2023.09.12.05.28.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 05:28:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=VC+m64f1; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 1A398805C14B; Tue, 12 Sep 2023 01:00:17 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231827AbjILH6J (ORCPT + 39 others); Tue, 12 Sep 2023 03:58:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231628AbjILH5z (ORCPT ); Tue, 12 Sep 2023 03:57:55 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 895B910CB for ; Tue, 12 Sep 2023 00:57:51 -0700 (PDT) Message-ID: <20230912065501.009527288@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505470; 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=sOJ+sRQt3nfyL5UePtEvx89xEa11XaUFA1X2Va9tGj4=; b=VC+m64f1NSEghCidAPnUeMhh72xF1UtXYpCyT9SE1Bo0ntnBG1TqEFhierCGKqxcyj+51j UTntDrbqA0yBnyI7+bKdDSCVdGGLWOOgGAq5kg+ru4MPIRVga1rFgl+r+kn1xF2DKoXPdB CTeSfFDbTzdBgUL7Anet+F24sHVUft7mfyiuAjHZxF1eFBKYLpfhrSf4LY82IdWK4CesNc 7P+ueJ4JvJD/s2xZYAIFYOxZTcaQLIlWcUT3sxCWSXVWQmlWbowe7tNZMQ4+u8iVkHb/HW BF73iMkVYZyR4/El/5obBGSF9zlj4PiOVqfpq/dupV19+gMLmEa9A9HtpeL5PQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505470; 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=sOJ+sRQt3nfyL5UePtEvx89xEa11XaUFA1X2Va9tGj4=; b=OLeVeCJtmnPOW04+SrRKrohKMyDny6DsczrcxlMaK4d8/78mD/GpeRwU53uQeB1Uq9tf9Q vbXz5SrLR0ZfKYBA== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 04/30] x86/microcode/intel: Simplify scan_microcode() References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:57:49 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Tue, 12 Sep 2023 01:00:18 -0700 (PDT) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776834814985262544 X-GMAIL-MSGID: 1776834814985262544 Make it readable and comprehensible. Signed-off-by: Thomas Gleixner --- arch/x86/kernel/cpu/microcode/intel.c | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) --- a/arch/x86/kernel/cpu/microcode/intel.c +++ b/arch/x86/kernel/cpu/microcode/intel.c @@ -265,25 +265,19 @@ static void save_microcode_patch(void *d return; /* Save for early loading */ - intel_ucode_patch = (struct microcode_intel *)p; + intel_ucode_patch = (struct microcode_intel *)p; } -/* - * Get microcode matching with BSP's model. Only CPUs with the same model as - * BSP can stay in the platform. - */ -static struct microcode_intel * -scan_microcode(void *data, size_t size, struct ucode_cpu_info *uci, bool save) +/* Scan CPIO for microcode matching the boot CPUs family, model, stepping */ +static struct microcode_intel *scan_microcode(void *data, size_t size, + struct ucode_cpu_info *uci, bool save) { struct microcode_header_intel *mc_header; struct microcode_intel *patch = NULL; u32 cur_rev = uci->cpu_sig.rev; unsigned int mc_size; - while (size) { - if (size < sizeof(struct microcode_header_intel)) - break; - + for (; size >= sizeof(struct microcode_header_intel); size -= mc_size, data += mc_size) { mc_header = (struct microcode_header_intel *)data; mc_size = get_totalsize(mc_header); @@ -291,27 +285,19 @@ scan_microcode(void *data, size_t size, intel_microcode_sanity_check(data, false, MC_HEADER_TYPE_MICROCODE) < 0) break; - size -= mc_size; - - if (!intel_find_matching_signature(data, uci->cpu_sig.sig, - uci->cpu_sig.pf)) { - data += mc_size; + if (!intel_find_matching_signature(data, uci->cpu_sig.sig, uci->cpu_sig.pf)) continue; - } /* BSP scan: Check whether there is newer microcode */ if (!save && cur_rev >= mc_header->rev) - goto next; + continue; /* Save scan: Check whether there is newer or matching microcode */ if (save && cur_rev != mc_header->rev) - goto next; + continue; patch = data; cur_rev = mc_header->rev; - -next: - data += mc_size; } if (size) From patchwork Tue Sep 12 07:57:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138038 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp270773vqx; Tue, 12 Sep 2023 02:03:34 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG8XwSmkqnGUfVSWjgSSO7ED3tHKlBuQsnkqeUKL31fBWYBx+ZGFCUxN7nhWAjSxBJsQYyM X-Received: by 2002:a05:6a20:9489:b0:151:991c:84b6 with SMTP id hs9-20020a056a20948900b00151991c84b6mr9511778pzb.59.1694509413786; Tue, 12 Sep 2023 02:03:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694509413; cv=none; d=google.com; s=arc-20160816; b=uKFVWEP4X7+76XpLw6Q0sC0eEDn5t1yC01lnMN7crGRi29Ffl2zAv6qQv5ZkDWbBli QouGUgoh77zVtNUKTVJJKu/snDfvEN6tcU0xsW41J/HJQkfiZOg+im98zu92b+IP/g1N ZWZvsqATo7wdQ5NQ/f/yFb1IgzNLgQqnoKMQ7Sl4pl1u9b3tZZ0nhmNdIFJj5dUY209m ilI0TwATVmFmnItd0KaHh4ooKkzugtn9eh9rKoUB/CoRvrQbBi1LEnbS5TPxQyYMBJPy R15162Jw5upAsQ8hyxSWHLAR4SjSo7UAY0ksxUgKE3ZS4IGfbOGjPxt5ENl6tmybnPAa Lpaw== 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=CN2QFAx6JB0LSmuYjwWY54K/mbyCNDPxFixGJ6298Ak=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=tstBLX35syxFrdQFIelswJKSIZ31dtNcgf+/D5pgD4Rn0gGh2rRPVyW2dbcSCYyIxi bwxQ50wdMPZw7ZGEQeAoXKMaQiCnPzqxv2h0DruNENPOQ423KYJY+v8+1ufBZZsG9LrG 585kcxP8bV9xK+gtd/FGas3dTy49TrvCZ3oHd53MJU7/cYkjjzWVJpYNQZIeN5AfppR0 WHGJ4PH1EPm33kYIFx5rS1DOGOoF2AQnPGs0vwACnIU431d1g3+UrDQc7lozeMjZlYrv 2P8BTDnhE18BDnfNQZ3UYLot/DidTkUIoWD+5VQ5RVSDl68ClgaUNKRCrYxOhK4MC8Ue XqgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=uAr8WVy1; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id q22-20020a637516000000b0055793097dbesi4399798pgc.469.2023.09.12.02.03.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 02:03:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=uAr8WVy1; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id C5BB081D6D8E; Tue, 12 Sep 2023 00:58:28 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231953AbjILH6L (ORCPT + 39 others); Tue, 12 Sep 2023 03:58:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48180 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231840AbjILH55 (ORCPT ); Tue, 12 Sep 2023 03:57:57 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5241110C7 for ; Tue, 12 Sep 2023 00:57:53 -0700 (PDT) Message-ID: <20230912065501.070512232@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505471; 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=CN2QFAx6JB0LSmuYjwWY54K/mbyCNDPxFixGJ6298Ak=; b=uAr8WVy1xauXNH5nuhSyal+A8QHgW32mnXLnA8qqj9qnC/tW9q7qyPd+kr64cS8xNJF9pO SdClBiuM/6yKRIXuoqQpL37qxmzZ3PRxQKEqrn9873CQUSPZA/UOcjbqF2PbvZ+3eUUny7 YhPFv6ByF/mMmiklzweM+2SaiQsewpJ6Srb6g0uSjovyiOXxU22nkAC6ojDxf0UDJB3flZ hwiuOqfqe+eIBAMRbjJ71EETLbzn9PmmB/fd81uPfCrp1XKXkb3UD8ryBxq8L7XG2baqkW KXqKfSE/9l5J+kzo1KI5m2jy72nZwN9S17rZUN9h/DJDqMg+ZuEQhz7UA3IDGg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505471; 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=CN2QFAx6JB0LSmuYjwWY54K/mbyCNDPxFixGJ6298Ak=; b=6nRRBNKGK0sJ+MDapmUWy//UBZFo5Reef+ccpmE7+rXMuOYlFA9VWYhObWgDhqNvxqSnBA kr4oMi2/mD8HJTCA== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 05/30] x86/microcode/intel: Simplify and rename generic_load_microcode() References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:57:51 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 12 Sep 2023 00:58:28 -0700 (PDT) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776821903017320193 X-GMAIL-MSGID: 1776821903017320193 so it becomes less obfuscated and rename it because there is nothing generic about it. Signed-off-by: Thomas Gleixner --- V3: Rename to parse_microcode_blobs() - Borislav --- arch/x86/kernel/cpu/microcode/intel.c | 47 ++++++++++++---------------------- 1 file changed, 17 insertions(+), 30 deletions(-) --- a/arch/x86/kernel/cpu/microcode/intel.c +++ b/arch/x86/kernel/cpu/microcode/intel.c @@ -240,19 +240,6 @@ int intel_microcode_sanity_check(void *m } EXPORT_SYMBOL_GPL(intel_microcode_sanity_check); -/* - * Returns 1 if update has been found, 0 otherwise. - */ -static int has_newer_microcode(void *mc, unsigned int csig, int cpf, int new_rev) -{ - struct microcode_header_intel *mc_hdr = mc; - - if (mc_hdr->rev <= new_rev) - return 0; - - return intel_find_matching_signature(mc, csig, cpf); -} - static void save_microcode_patch(void *data, unsigned int size) { struct microcode_header_intel *p; @@ -561,14 +548,12 @@ static enum ucode_state apply_microcode_ return ret; } -static enum ucode_state generic_load_microcode(int cpu, struct iov_iter *iter) +static enum ucode_state parse_microcode_blobs(int cpu, struct iov_iter *iter) { struct ucode_cpu_info *uci = ucode_cpu_info + cpu; unsigned int curr_mc_size = 0, new_mc_size = 0; - enum ucode_state ret = UCODE_OK; - int new_rev = uci->cpu_sig.rev; + int cur_rev = uci->cpu_sig.rev; u8 *new_mc = NULL, *mc = NULL; - unsigned int csig, cpf; while (iov_iter_count(iter)) { struct microcode_header_intel mc_header; @@ -585,6 +570,7 @@ static enum ucode_state generic_load_mic pr_err("error! Bad data in microcode data file (totalsize too small)\n"); break; } + data_size = mc_size - sizeof(mc_header); if (data_size > iov_iter_count(iter)) { pr_err("error! Bad data in microcode data file (truncated file?)\n"); @@ -607,16 +593,17 @@ static enum ucode_state generic_load_mic break; } - csig = uci->cpu_sig.sig; - cpf = uci->cpu_sig.pf; - if (has_newer_microcode(mc, csig, cpf, new_rev)) { - vfree(new_mc); - new_rev = mc_header.rev; - new_mc = mc; - new_mc_size = mc_size; - mc = NULL; /* trigger new vmalloc */ - ret = UCODE_NEW; - } + if (cur_rev >= mc_header.rev) + continue; + + if (!intel_find_matching_signature(mc, uci->cpu_sig.sig, uci->cpu_sig.pf)) + continue; + + vfree(new_mc); + cur_rev = mc_header.rev; + new_mc = mc; + new_mc_size = mc_size; + mc = NULL; } vfree(mc); @@ -636,9 +623,9 @@ static enum ucode_state generic_load_mic save_microcode_patch(new_mc, new_mc_size); pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n", - cpu, new_rev, uci->cpu_sig.rev); + cpu, cur_rev, uci->cpu_sig.rev); - return ret; + return UCODE_NEW; } static bool is_blacklisted(unsigned int cpu) @@ -687,7 +674,7 @@ static enum ucode_state request_microcod kvec.iov_base = (void *)firmware->data; kvec.iov_len = firmware->size; iov_iter_kvec(&iter, ITER_SOURCE, &kvec, 1, firmware->size); - ret = generic_load_microcode(cpu, &iter); + ret = parse_microcode_blobs(cpu, &iter); release_firmware(firmware); From patchwork Tue Sep 12 07:57:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 141097 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp1809415vqi; Sat, 16 Sep 2023 12:06:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE5/R1dpkCksowUVvw18pVeGF0SRhOj6EDu+be3v3QUoY5ttvVcf6iwwQpI3GnE6nI6BgRD X-Received: by 2002:a05:6a20:160e:b0:14b:887a:6d70 with SMTP id l14-20020a056a20160e00b0014b887a6d70mr6133387pzj.15.1694891164725; Sat, 16 Sep 2023 12:06:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694891164; cv=none; d=google.com; s=arc-20160816; b=FaC164czmG8pKTTCkXxzWewpY6k+RPmbvq908i+2W9K0WBh5pvhOOyW1juGNJc+JZT hDJVleHicNonnwXy7XRzW1ygLcK0B9YGV2gIp9e0jKmJLBwxBe4Pz1KeSPj6j7jXac5q vlY85r+ZKdZiiDx7FF3HkoL1iSPvxI6pZQCGNWlviOdpnaq7jsZiNe81BUwOUG8e70eF u4KPfdfXOgfqRvR80otd3VbkbwaXI9yGWjI7TTc3K3RFgqdKRA+nJTmUgCtfWtvt0ylG Lk5yrx9xTpnN5Ri5pGVsbAXD0Vz1nmWauXKNUlzetFm9V6XV0gNYgShG0L+k7Xnte3KP 9fBg== 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=YKmioiJQ3sncgsUZ7rGcHgZzunvCDH7pLQJNxPbmmRI=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=QjVglZnRn+Kh1rQhHIOzHLiLIgolr9Hb9Qps1YWbw+D+tR5FOweoW8O4bxcxlcWcZ0 nKkOgYYtx4Ay4CWgAdiKKTjWmpAyXViqEIdUYX5PLGBDtDlKU7LovqRxa7pUCdKJnHOz yVmdehEsPjo5urJM9Iw/ZhCRgj+Yh9J8AHceYNs8bbzC9rklnyBNPG7XHSS5afZVO0Pn y0XVfBK5I89Urzkzq6iGys06LynJUwZJcpYo+LuewDhhke4/3S8HHoP9lWJLoEllXQF6 n9L7Utwc5mLPPuSqiAC8y5s7LIypXsjJzdB8uWJGRUNlZeDgaXU6RxAvj70QofVhPIda RJFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=BvrcFWMm; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b="MGztH/02"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id s34-20020a056a0017a200b0069014d63f21si5439815pfg.148.2023.09.16.12.06.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Sep 2023 12:06:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=BvrcFWMm; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b="MGztH/02"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id B15BB8282A15; Tue, 12 Sep 2023 00:58:33 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.8 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232032AbjILH6U (ORCPT + 39 others); Tue, 12 Sep 2023 03:58:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42582 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231915AbjILH6G (ORCPT ); Tue, 12 Sep 2023 03:58:06 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE6D410D0 for ; Tue, 12 Sep 2023 00:57:54 -0700 (PDT) Message-ID: <20230912065501.146684094@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505473; 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=YKmioiJQ3sncgsUZ7rGcHgZzunvCDH7pLQJNxPbmmRI=; b=BvrcFWMm5+PGIOA+l7zcsZ8u92SGB9pNjwRotmPgz3UO0W9GsTkLL++b4aFOqlh04ejYxI WEK7vS2tO17UdVD5XnD9Dn1X0gpzCfpmIEvUoTjcFeWZBlJN5s60Bzw2LbhWnJq0vVZ5xo Bf5sT9xc8rtdmlKGy377+XgeeX9NJSR+NRm17s5AuuNfZMQBdcuQLovqHHu+o9YaBexi2L uvoad+YiFRPcaX1A8uBPSbvnSSACD8kbZitA7P1a3RYdUPeMk9QnI4CEjew1dZ5lBgP1F2 4NjMHfPuMdAMWn/Qz3TTeosCagiWT0ZsqF69yJc21aGr2v4XGAkE4YAHn4JkDw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505473; 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=YKmioiJQ3sncgsUZ7rGcHgZzunvCDH7pLQJNxPbmmRI=; b=MGztH/02DST6gJzZFp8kE/G0J7VMtm1f1e3VIuhiayJal5UzsXtHK5Vh02fATrdZwe/PBf bjTK8w1P/RvqnUCg== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 06/30] x86/microcode/intel: Cleanup code further References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:57:52 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 12 Sep 2023 00:58:33 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777222198183128025 X-GMAIL-MSGID: 1777222198183128025 From: Thomas Gleixner Sanitize the microcode scan loop, fixup printks and move the initrd loading function next to the place where it is used and mark it __init. Signed-off-by: Thomas Gleixner --- V2: Fix changelog - Nikolay --- arch/x86/kernel/cpu/microcode/intel.c | 76 ++++++++++++++-------------------- 1 file changed, 32 insertions(+), 44 deletions(-) --- --- a/arch/x86/kernel/cpu/microcode/intel.c +++ b/arch/x86/kernel/cpu/microcode/intel.c @@ -36,7 +36,7 @@ static const char ucode_path[] = "kernel static struct microcode_intel *intel_ucode_patch __read_mostly; /* last level cache size per core */ -static int llc_size_per_core __ro_after_init; +static unsigned int llc_size_per_core __ro_after_init; /* microcode format is extended from prescott processors */ struct extended_signature { @@ -296,29 +296,6 @@ static struct microcode_intel *scan_micr return patch; } -static bool load_builtin_intel_microcode(struct cpio_data *cp) -{ - unsigned int eax = 1, ebx, ecx = 0, edx; - struct firmware fw; - char name[30]; - - if (IS_ENABLED(CONFIG_X86_32)) - return false; - - native_cpuid(&eax, &ebx, &ecx, &edx); - - sprintf(name, "intel-ucode/%02x-%02x-%02x", - x86_family(eax), x86_model(eax), x86_stepping(eax)); - - if (firmware_request_builtin(&fw, name)) { - cp->size = fw.size; - cp->data = (void *)fw.data; - return true; - } - - return false; -} - static int apply_microcode_early(struct ucode_cpu_info *uci, bool early) { struct microcode_intel *mc; @@ -362,6 +339,28 @@ static int apply_microcode_early(struct return 0; } +static bool load_builtin_intel_microcode(struct cpio_data *cp) +{ + unsigned int eax = 1, ebx, ecx = 0, edx; + struct firmware fw; + char name[30]; + + if (IS_ENABLED(CONFIG_X86_32)) + return false; + + native_cpuid(&eax, &ebx, &ecx, &edx); + + sprintf(name, "intel-ucode/%02x-%02x-%02x", + x86_family(eax), x86_model(eax), x86_stepping(eax)); + + if (firmware_request_builtin(&fw, name)) { + cp->size = fw.size; + cp->data = (void *)fw.data; + return true; + } + return false; +} + int __init save_microcode_in_initrd_intel(void) { struct ucode_cpu_info uci; @@ -434,25 +433,16 @@ void load_ucode_intel_ap(void) apply_microcode_early(&uci, true); } -/* Accessor for microcode pointer */ -static struct microcode_intel *ucode_get_patch(void) -{ - return intel_ucode_patch; -} - void reload_ucode_intel(void) { - struct microcode_intel *p; struct ucode_cpu_info uci; intel_cpu_collect_info(&uci); - p = ucode_get_patch(); - if (!p) + uci.mc = intel_ucode_patch; + if (!uci.mc) return; - uci.mc = p; - apply_microcode_early(&uci, false); } @@ -490,8 +480,7 @@ static enum ucode_state apply_microcode_ if (WARN_ON(raw_smp_processor_id() != cpu)) return UCODE_ERROR; - /* Look for a newer patch in our cache: */ - mc = ucode_get_patch(); + mc = intel_ucode_patch; if (!mc) { mc = uci->mc; if (!mc) @@ -682,18 +671,17 @@ static enum ucode_state request_microcod } static struct microcode_ops microcode_intel_ops = { - .request_microcode_fw = request_microcode_fw, - .collect_cpu_info = collect_cpu_info, - .apply_microcode = apply_microcode_intel, + .request_microcode_fw = request_microcode_fw, + .collect_cpu_info = collect_cpu_info, + .apply_microcode = apply_microcode_intel, }; -static int __init calc_llc_size_per_core(struct cpuinfo_x86 *c) +static __init void calc_llc_size_per_core(struct cpuinfo_x86 *c) { u64 llc_size = c->x86_cache_size * 1024ULL; do_div(llc_size, c->x86_max_cores); - - return (int)llc_size; + llc_size_per_core = (unsigned int)llc_size; } struct microcode_ops * __init init_intel_microcode(void) @@ -706,7 +694,7 @@ struct microcode_ops * __init init_intel return NULL; } - llc_size_per_core = calc_llc_size_per_core(c); + calc_llc_size_per_core(c); return µcode_intel_ops; } From patchwork Tue Sep 12 07:57:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138120 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp346474vqx; Tue, 12 Sep 2023 04:49:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEhWqjTVW7H2E3Q4THsnrakkPemX64EpEFFlg3RNVbIdxDTJoTAQpRiCjheWhY/VCR9uN/9 X-Received: by 2002:a17:903:32ca:b0:1af:aafb:64c8 with SMTP id i10-20020a17090332ca00b001afaafb64c8mr12535283plr.21.1694519343734; Tue, 12 Sep 2023 04:49:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694519343; cv=none; d=google.com; s=arc-20160816; b=oMpIsd8bT9GIGgT811e8Q5bDTAEKS5vp7SUCnXPENFG3cTYhbFrj9qpP4/4CUak4Am ByoZ4djO59lvShHhPpwBNG7ozq8lI5nw3lDHGZRK3Z3mVt/5nX5JhNhWbKAenduLZ/ir A1jZRtwF9WVGQB019db2qwqiGFRhobUdbr7j5ZIm+ESbvujbSA9cevPT3a9U++YLpX+D Tse11hiu3K8oAhgeXmiWZHvrqAOjDvLimeN0/L/3OgyaalXDHP1lnhFG5q2uo5ifNNkz O1iQ8ueMZZfiXJiIqPd5dZIuj6KnoJc8Gl8WIsLsEujcpVuIlHthAzbaIqz1i4sPe+m1 EjTw== 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=xycivm/oTJlbqsNId8Iah6SwofmZC3GQW0sFPIUqyuw=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=Y0HyOvmpkPHeqOIqQyrVODBG2mcgNk9fB2AD8iBMGD6ZY0vuqhWLkG/DLQQnyLHRkL VmcMo9k8S1EJZe3hg3zxNuQFuRe/qjPf342cdrx5FT+jt8JDcxdINTqLp0TSkVKdkmlW 0c1/j+CgmS4N9Ef6hIoxlY8BaIaGgSATZU992IMAgq9+xREtPjMXfrqkDDvDfTOvAuMm jUJ4HxMARtRPN80dkF/Mpti7GKOSAF8hcQ0RIU6czIuKjqTYrJNUdr1QJ2hdBKgvP9Bo RkorRboGsbfcrAJTUY2Q2dyatH5a+PBaD0G7M+MRgbqX4zu/c5UzGRTppxTTmePutUTA 1TeQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="hHgn/P9Y"; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id lf12-20020a170902fb4c00b001bba90f8b73si7731583plb.78.2023.09.12.04.48.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 04:49:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="hHgn/P9Y"; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 7E72581D6DA0; Tue, 12 Sep 2023 00:58:54 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232004AbjILH60 (ORCPT + 39 others); Tue, 12 Sep 2023 03:58:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54148 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231968AbjILH6M (ORCPT ); Tue, 12 Sep 2023 03:58:12 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 641CE10DA for ; Tue, 12 Sep 2023 00:57:56 -0700 (PDT) Message-ID: <20230912065501.208060138@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505475; 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=xycivm/oTJlbqsNId8Iah6SwofmZC3GQW0sFPIUqyuw=; b=hHgn/P9YFx5yE+Gz+nPDcHbILzrE1snjUOVkOOtaoqpE0f82uoHMbdBuV2mMo632y0Ml6p nPM0BsFHV8nwg2bRSUMTtGEgQNm9zVMT4m2lw4a8sbTxA/PJkveIQ9LNfGfaowZDf3S6/Z E2k0tptXEP+4qmEK18ciJpdKZOAZe0+6dMdXhGuwdbfTTRt2GFkpOOWSUF0xDx9w/qqePA sOhU/pXQz48l6qNWhMoDzFDwmOap7yHqoMURG4eLHNkTpM9Yg0BmD0XwhxxN5F+Z6IeDsj aqoRkgJoVjWWqzlY0qHvWheojqQoxksLBvy3X1NilKFM9YwQG0+afwYenrpUuA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505475; 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=xycivm/oTJlbqsNId8Iah6SwofmZC3GQW0sFPIUqyuw=; b=Dwrugw6FCmDV59iq8aIISfKR4TfK3YUtkWnj5tDh3/BnmuNgCIY1cKfBg0Uo2m3EI1u1E/ EbFtvfEdUaEfY+DA== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 07/30] x86/microcode/intel: Simplify early loading References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:57:54 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 12 Sep 2023 00:58:54 -0700 (PDT) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776832315754868350 X-GMAIL-MSGID: 1776832315754868350 From: Thomas Gleixner The early loading code is overly complicated: - It scans the builtin/initrd for microcode not only on the BSP, but also on all APs during early boot and then later in the boot process it scans again to duplicate and save the microcode before initrd goes away. That's a pointless exercise because this can be simply done before bringing up the APs when the memory allocator is up and running. - Saving the microcode from within the scan loop is completely non-obvious and a left over of the microcode cache. This can be done at the call site now which makes it obvious. Rework the code so that only the BSP scans the builtin/initrd microcode once during early boot and save it away in an early initcall for later use. Signed-off-by: Thomas Gleixner --- arch/x86/kernel/cpu/microcode/core.c | 4 arch/x86/kernel/cpu/microcode/intel.c | 148 +++++++++++++------------------ arch/x86/kernel/cpu/microcode/internal.h | 2 3 files changed, 64 insertions(+), 90 deletions(-) --- --- a/arch/x86/kernel/cpu/microcode/core.c +++ b/arch/x86/kernel/cpu/microcode/core.c @@ -186,10 +186,6 @@ static int __init save_microcode_in_init int ret = -EINVAL; switch (c->x86_vendor) { - case X86_VENDOR_INTEL: - if (c->x86 >= 6) - ret = save_microcode_in_initrd_intel(); - break; case X86_VENDOR_AMD: if (c->x86 >= 0x10) ret = save_microcode_in_initrd_amd(cpuid_eax(1)); --- a/arch/x86/kernel/cpu/microcode/intel.c +++ b/arch/x86/kernel/cpu/microcode/intel.c @@ -33,7 +33,7 @@ static const char ucode_path[] = "kernel/x86/microcode/GenuineIntel.bin"; /* Current microcode patch used in early patching on the APs. */ -static struct microcode_intel *intel_ucode_patch __read_mostly; +static struct microcode_intel *ucode_patch_va __read_mostly; /* last level cache size per core */ static unsigned int llc_size_per_core __ro_after_init; @@ -240,24 +240,29 @@ int intel_microcode_sanity_check(void *m } EXPORT_SYMBOL_GPL(intel_microcode_sanity_check); -static void save_microcode_patch(void *data, unsigned int size) +static void update_ucode_pointer(struct microcode_intel *mc) { - struct microcode_header_intel *p; + kfree(ucode_patch_va); - kfree(intel_ucode_patch); - intel_ucode_patch = NULL; + /* + * Save the virtual address for early loading and for eventual free + * on late loading. + */ + ucode_patch_va = mc; +} - p = kmemdup(data, size, GFP_KERNEL); - if (!p) - return; +static void save_microcode_patch(struct microcode_intel *patch) +{ + struct microcode_intel *mc; - /* Save for early loading */ - intel_ucode_patch = (struct microcode_intel *)p; + mc = kmemdup(patch, get_totalsize(&patch->hdr), GFP_KERNEL); + if (mc) + update_ucode_pointer(mc); } /* Scan CPIO for microcode matching the boot CPUs family, model, stepping */ -static struct microcode_intel *scan_microcode(void *data, size_t size, - struct ucode_cpu_info *uci, bool save) +static __init struct microcode_intel *scan_microcode(void *data, size_t size, + struct ucode_cpu_info *uci) { struct microcode_header_intel *mc_header; struct microcode_intel *patch = NULL; @@ -275,35 +280,25 @@ static struct microcode_intel *scan_micr if (!intel_find_matching_signature(data, uci->cpu_sig.sig, uci->cpu_sig.pf)) continue; - /* BSP scan: Check whether there is newer microcode */ - if (!save && cur_rev >= mc_header->rev) - continue; - - /* Save scan: Check whether there is newer or matching microcode */ - if (save && cur_rev != mc_header->rev) + /* Check whether there is newer microcode */ + if (cur_rev >= mc_header->rev) continue; patch = data; cur_rev = mc_header->rev; } - if (size) - return NULL; - - if (save && patch) - save_microcode_patch(patch, mc_size); - - return patch; + return size ? NULL : patch; } -static int apply_microcode_early(struct ucode_cpu_info *uci, bool early) +static enum ucode_state apply_microcode_early(struct ucode_cpu_info *uci, bool early) { struct microcode_intel *mc; u32 rev, old_rev, date; mc = uci->mc; if (!mc) - return 0; + return UCODE_NFOUND; /* * Save us the MSR write below - which is a particular expensive @@ -329,17 +324,17 @@ static int apply_microcode_early(struct rev = intel_get_microcode_revision(); if (rev != mc->hdr.rev) - return -1; + return UCODE_ERROR; uci->cpu_sig.rev = rev; date = mc->hdr.date; pr_info_once("updated early: 0x%x -> 0x%x, date = %04x-%02x-%02x\n", old_rev, rev, date & 0xffff, date >> 24, (date >> 16) & 0xff); - return 0; + return UCODE_UPDATED; } -static bool load_builtin_intel_microcode(struct cpio_data *cp) +static __init bool load_builtin_intel_microcode(struct cpio_data *cp) { unsigned int eax = 1, ebx, ecx = 0, edx; struct firmware fw; @@ -361,89 +356,75 @@ static bool load_builtin_intel_microcode return false; } -int __init save_microcode_in_initrd_intel(void) +static __init struct microcode_intel *get_ucode_from_cpio(struct ucode_cpu_info *uci) { - struct ucode_cpu_info uci; struct cpio_data cp; - /* - * initrd is going away, clear patch ptr. We will scan the microcode one - * last time before jettisoning and save a patch, if found. Then we will - * update that pointer too, with a stable patch address to use when - * resuming the cores. - */ - intel_ucode_patch = NULL; - if (!load_builtin_intel_microcode(&cp)) cp = find_microcode_in_initrd(ucode_path); if (!(cp.data && cp.size)) - return 0; + return NULL; - intel_cpu_collect_info(&uci); + intel_cpu_collect_info(uci); - scan_microcode(cp.data, cp.size, &uci, true); - return 0; + return scan_microcode(cp.data, cp.size, uci); } +static struct microcode_intel *ucode_early_pa __initdata; + /* - * @res_patch, output: a pointer to the patch we found. + * Invoked from an early init call to save the microcode blob which was + * selected during early boot when mm was not usable. The microcode must be + * saved because initrd is going away. It's an early init call so the APs + * just can use the pointer and do not have to scan initrd/builtin firmware + * again. */ -static struct microcode_intel *__load_ucode_intel(struct ucode_cpu_info *uci) +static int __init save_microcode_from_cpio(void) { - struct cpio_data cp; - - /* try built-in microcode first */ - if (!load_builtin_intel_microcode(&cp)) - cp = find_microcode_in_initrd(ucode_path); - - if (!(cp.data && cp.size)) - return NULL; + struct microcode_intel *mc; - intel_cpu_collect_info(uci); + if (!ucode_early_pa) + return 0; - return scan_microcode(cp.data, cp.size, uci, false); + mc = __va((void *)ucode_early_pa); + save_microcode_patch(mc); + return 0; } +early_initcall(save_microcode_from_cpio); +/* Load microcode on BSP from CPIO */ void __init load_ucode_intel_bsp(void) { - struct microcode_intel *patch; struct ucode_cpu_info uci; - patch = __load_ucode_intel(&uci); - if (!patch) + uci.mc = get_ucode_from_cpio(&uci); + if (!uci.mc) return; - uci.mc = patch; + if (apply_microcode_early(&uci, true) != UCODE_UPDATED) + return; - apply_microcode_early(&uci, true); + /* Store the physical address as KASLR happens after this. */ + ucode_early_pa = (struct microcode_intel *)__pa_nodebug(uci.mc); } void load_ucode_intel_ap(void) { struct ucode_cpu_info uci; - if (!intel_ucode_patch) { - intel_ucode_patch = __load_ucode_intel(&uci); - if (!intel_ucode_patch) - return; - } - - uci.mc = intel_ucode_patch; - apply_microcode_early(&uci, true); + uci.mc = ucode_patch_va; + if (uci.mc) + apply_microcode_early(&uci, true); } +/* Reload microcode on resume */ void reload_ucode_intel(void) { - struct ucode_cpu_info uci; - - intel_cpu_collect_info(&uci); + struct ucode_cpu_info uci = { .mc = ucode_patch_va, }; - uci.mc = intel_ucode_patch; - if (!uci.mc) - return; - - apply_microcode_early(&uci, false); + if (uci.mc) + apply_microcode_early(&uci, false); } static int collect_cpu_info(int cpu_num, struct cpu_signature *csig) @@ -480,7 +461,7 @@ static enum ucode_state apply_microcode_ if (WARN_ON(raw_smp_processor_id() != cpu)) return UCODE_ERROR; - mc = intel_ucode_patch; + mc = ucode_patch_va; if (!mc) { mc = uci->mc; if (!mc) @@ -540,8 +521,8 @@ static enum ucode_state apply_microcode_ static enum ucode_state parse_microcode_blobs(int cpu, struct iov_iter *iter) { struct ucode_cpu_info *uci = ucode_cpu_info + cpu; - unsigned int curr_mc_size = 0, new_mc_size = 0; int cur_rev = uci->cpu_sig.rev; + unsigned int curr_mc_size = 0; u8 *new_mc = NULL, *mc = NULL; while (iov_iter_count(iter)) { @@ -591,7 +572,6 @@ static enum ucode_state parse_microcode_ vfree(new_mc); cur_rev = mc_header.rev; new_mc = mc; - new_mc_size = mc_size; mc = NULL; } @@ -605,11 +585,11 @@ static enum ucode_state parse_microcode_ if (!new_mc) return UCODE_NFOUND; - vfree(uci->mc); - uci->mc = (struct microcode_intel *)new_mc; - /* Save for CPU hotplug */ - save_microcode_patch(new_mc, new_mc_size); + save_microcode_patch((struct microcode_intel *)new_mc); + uci->mc = ucode_patch_va; + + vfree(new_mc); pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n", cpu, cur_rev, uci->cpu_sig.rev); --- a/arch/x86/kernel/cpu/microcode/internal.h +++ b/arch/x86/kernel/cpu/microcode/internal.h @@ -107,13 +107,11 @@ static inline void exit_amd_microcode(vo #ifdef CONFIG_CPU_SUP_INTEL void load_ucode_intel_bsp(void); void load_ucode_intel_ap(void); -int save_microcode_in_initrd_intel(void); void reload_ucode_intel(void); struct microcode_ops *init_intel_microcode(void); #else /* CONFIG_CPU_SUP_INTEL */ static inline void load_ucode_intel_bsp(void) { } static inline void load_ucode_intel_ap(void) { } -static inline int save_microcode_in_initrd_intel(void) { return -EINVAL; } static inline void reload_ucode_intel(void) { } static inline struct microcode_ops *init_intel_microcode(void) { return NULL; } #endif /* !CONFIG_CPU_SUP_INTEL */ From patchwork Tue Sep 12 07:57:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138043 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp271694vqx; Tue, 12 Sep 2023 02:05:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFo8FB9NGCE6K63OE9c3kY8XHl1GgTk1VEfyyR++qjE8bDrfMBDSi5OM9MRJdRP9mqukt2S X-Received: by 2002:a05:6808:10cd:b0:3a3:7a28:f841 with SMTP id s13-20020a05680810cd00b003a37a28f841mr17952306ois.41.1694509518817; Tue, 12 Sep 2023 02:05:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694509518; cv=none; d=google.com; s=arc-20160816; b=XY4MHFRO80vByUq3taFZ9oTKQiGY6h3T7gmDRNV26mgvPrwFKRLi65L6lPE4EOtRQc WnGZ1Mdv9/Y0pgQa9B7Dr+ZfIORDBwU2HlipFXCE+CEhgpBj1AsQxfNnKEyJPp7bI6Wa lGShhhflD/lAFcAFk5kYvvADVxgq7H1wkga9pu5o/Gp71E4uKmlRZZIzFiPeIIVQ42pA zgBuZNreQcLeiAw8owvmbgHIOKAmauMXDzQpKxzSJqj0QUMIg2eIBmUtqOAFbbXtncuz 5cqavpVgj3ZxvWolnBHdlCdXpkUBk24y3s8jfFG3sM6yxr03AAbqh7e8Fy/KJSyQ03Py DkVw== 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=HiC6v2fxtRjR8xApH6q4tcaG7wy0kTM5+263Xe7tZgo=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=HyYCDJd3xEgshXGZqbEIkoSnm+ZuIEN4LYDpLt9ulskHJY0ZB6U3oX0A+SjiSjGoaD SolQF1e/P1X3UGLfTXG2t+VMB0ocmXqGWOjzpqwES8cEAkTHHMCGheAwHHC13jQIn9zC dIPhTuhpb0F+TVtABEb+YsyljBAjgAJ0C3ou3pz3cHpG5Wb6Ht7qaeqso+wk3qMmqAJg CXfA6ChNhhwo46k5sfyUjzsxlI35WY4PxIiSF6ivcJFe7dVzQNGUmWDYd2sKosckUfmr B261oeGR7K6WtnQxO9MGhyHL+yOUzCnz7x1Ssn12WSckmmTxGh3H9jC/y2VCTEVyVfXk BM2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=rH0ZxaSy; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=elZFAiMq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id x190-20020a6386c7000000b0056baff5c553si7525137pgd.101.2023.09.12.02.05.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 02:05:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=rH0ZxaSy; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=elZFAiMq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id F21AE80E9E99; Tue, 12 Sep 2023 00:59:16 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232011AbjILH62 (ORCPT + 39 others); Tue, 12 Sep 2023 03:58:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42582 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231686AbjILH6S (ORCPT ); Tue, 12 Sep 2023 03:58:18 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F02B510E7 for ; Tue, 12 Sep 2023 00:57:57 -0700 (PDT) Message-ID: <20230912065501.269244004@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505476; 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=HiC6v2fxtRjR8xApH6q4tcaG7wy0kTM5+263Xe7tZgo=; b=rH0ZxaSyiHJj2ThngUo0cJMEKrtEBxu2se0MHjUZ4H5phU6SL3DXCWUTGFmQzMXTCr9523 +UqAOV2MeOgLYj3MdWkaNCADt5W8kViihKF7sQUJi25bSFo4/ADfS78mohgHsiJA9/TpxP 3yiGP5uQO3zOIAs46zf38p6N1D1mNkqVw66/Mpp9FdDMwu+sD/v1mWPwBJXMyD25VnzLpT h4BxG0Dwm9p458LffjoOUCNS5l+0iWIKGF1VaWALzfKTCBwAnmRKMSh97dObcrKyi2OExR i4opU8tPoiEjKjD2xINIw67/e10YBAGRks6cZW688mrDdIb6WME378DG04lpPg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505476; 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=HiC6v2fxtRjR8xApH6q4tcaG7wy0kTM5+263Xe7tZgo=; b=elZFAiMqugkD+SD+beAqWnUHSs9h0g52qd3XQmBLlC5T5m7rei6lMNjOY76AC7wh6ACEFA yIcR9Xqz/xfjK0Aw== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 08/30] x86/microcode/intel: Save the microcode only after a successful late-load References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:57:56 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Tue, 12 Sep 2023 00:59:17 -0700 (PDT) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776822013147274022 X-GMAIL-MSGID: 1776822013147274022 From: Thomas Gleixner There are situations where the late microcode is loaded into memory, but is not applied: 1) The rendevouz fails 2) The microcode is rejected by the CPUs If any of this happens then the pointer which was updated at firmware load time is stale and subsequent CPU hotplug operations either fail to update or create inconsistent microcode state. Save the loaded microcode in a separate pointer from with the late load is attempted and when successful, update the hotplug pointer accordingly via a new micrcode_ops callback. Remove the pointless fallback in the loader to a microcode pointer which is never populated. Signed-off-by: Thomas Gleixner --- arch/x86/kernel/cpu/microcode/core.c | 4 ++++ arch/x86/kernel/cpu/microcode/intel.c | 30 +++++++++++++++--------------- arch/x86/kernel/cpu/microcode/internal.h | 1 + 3 files changed, 20 insertions(+), 15 deletions(-) --- --- a/arch/x86/kernel/cpu/microcode/core.c +++ b/arch/x86/kernel/cpu/microcode/core.c @@ -400,6 +400,10 @@ static int microcode_reload_late(void) store_cpu_caps(&prev_info); ret = stop_machine_cpuslocked(__reload_late, NULL, cpu_online_mask); + + if (microcode_ops->finalize_late_load) + microcode_ops->finalize_late_load(ret); + if (!ret) { pr_info("Reload succeeded, microcode revision: 0x%x -> 0x%x\n", old, boot_cpu_data.microcode); --- a/arch/x86/kernel/cpu/microcode/intel.c +++ b/arch/x86/kernel/cpu/microcode/intel.c @@ -34,6 +34,7 @@ static const char ucode_path[] = "kernel /* Current microcode patch used in early patching on the APs. */ static struct microcode_intel *ucode_patch_va __read_mostly; +static struct microcode_intel *ucode_patch_late __read_mostly; /* last level cache size per core */ static unsigned int llc_size_per_core __ro_after_init; @@ -461,12 +462,9 @@ static enum ucode_state apply_microcode_ if (WARN_ON(raw_smp_processor_id() != cpu)) return UCODE_ERROR; - mc = ucode_patch_va; - if (!mc) { - mc = uci->mc; - if (!mc) - return UCODE_NFOUND; - } + mc = ucode_patch_late; + if (!mc) + return UCODE_NFOUND; /* * Save us the MSR write below - which is a particular expensive @@ -585,15 +583,7 @@ static enum ucode_state parse_microcode_ if (!new_mc) return UCODE_NFOUND; - /* Save for CPU hotplug */ - save_microcode_patch((struct microcode_intel *)new_mc); - uci->mc = ucode_patch_va; - - vfree(new_mc); - - pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n", - cpu, cur_rev, uci->cpu_sig.rev); - + ucode_patch_late = (struct microcode_intel *)new_mc; return UCODE_NEW; } @@ -650,10 +640,20 @@ static enum ucode_state request_microcod return ret; } +static void finalize_late_load(int result) +{ + if (!result) + save_microcode_patch(ucode_patch_late); + + vfree(ucode_patch_late); + ucode_patch_late = NULL; +} + static struct microcode_ops microcode_intel_ops = { .request_microcode_fw = request_microcode_fw, .collect_cpu_info = collect_cpu_info, .apply_microcode = apply_microcode_intel, + .finalize_late_load = finalize_late_load, }; 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,6 +31,7 @@ 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); }; extern struct ucode_cpu_info ucode_cpu_info[]; From patchwork Tue Sep 12 07:57:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138149 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp377122vqx; Tue, 12 Sep 2023 05:42:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEsUhtWbnmSOBxJBt1wmYo6XEJcQlrhxVnnHC5RpcQD/Px32/YjgirOf9UTF7xBtaU6iA42 X-Received: by 2002:a05:6870:d24b:b0:1b7:4655:2ac9 with SMTP id h11-20020a056870d24b00b001b746552ac9mr16280731oac.6.1694522569900; Tue, 12 Sep 2023 05:42:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694522569; cv=none; d=google.com; s=arc-20160816; b=XG59m4S6TcfJ1bmodq1UDNTtI2MrGdWEdjpsDWPgkEZFBtYox0b2jRBXwfCcr8utxz rNrdx4oy/bL3WypYYGQWkx6hR5io7qTk4WYDfLFdTz5utjS/GiSzCheMDQpomkqsS8JS S/P250LgJHwPknFClTGny8am4hnBRAOrL4KRDS5utdRcTzVJShZMHfdyeMY4KmDGC2AN Fq3zr6YnRitvZzib1f9Sa89KLRgban9Br6UHsu1PGtSdx6BtfEH20FyA4HZ/pRlSFiNg btusxC/ooZ+h7o/f4zD0+09I6heF5I/RbLiENrZ5enURTCbJU3z29KbecwbDLLyODZwV VCAA== 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=COGYr1lbubEBgG3WbXJ/GVEKTFAghSZ0FbZctrPsvVg=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=KbWEdubW/LCZX3gDIK6D26gveXXsRgSC2wBk+ABtfrghOVbk4imchsSrsGV6Dg9VCK KOTrUn7c+fsx6sdbokR2YrEPfajR/Zkq604MysAYoRw0Dt77OAVBZQB/p1BbMYA/ScV6 JT2TP0LcTWQkVll4spN8LIZZFMGMR4FeL07FdWEga6bDdgaTZQyw/1Zih6wlhDGlpET6 TDOUcxEkv9WTc2oDn1Bd2ooHIthSLnXyhyY2IsegBLtqrWp0uKDQgXQ14M7qM0LYALZL vZlMpYXHCLtKQt0u8x6HCwVcJCIMXijXXGYCvQsdEPnNlM7CJWyo8HukDcZrN2qnJvGf SCFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=YzkFuM3c; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id t64-20020a638143000000b00565f76f86d8si7850466pgd.818.2023.09.12.05.42.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 05:42:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=YzkFuM3c; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 07BD481D6DA5; Tue, 12 Sep 2023 00:58:45 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231868AbjILH6c (ORCPT + 39 others); Tue, 12 Sep 2023 03:58:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232005AbjILH6T (ORCPT ); Tue, 12 Sep 2023 03:58:19 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A770510F9 for ; Tue, 12 Sep 2023 00:57:59 -0700 (PDT) Message-ID: <20230912065501.335403273@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505478; 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=COGYr1lbubEBgG3WbXJ/GVEKTFAghSZ0FbZctrPsvVg=; b=YzkFuM3cXvL7U+GFXHBgzX+Itn6q5lxrdVInucl/2b4dkqwYdNXJjH8K9kgQpoWv3+57rI Ti3gtX3exvhhqH2WwOX/w4KYF8FcQaIHhYV8YkElUtTyGMaYSqYMTxfIGK7pluirZxNfY3 PuAEkXJeHiK5a11mhHJ7FW0VDbNu1FZr1Gcpu27qIEj6HaSWPtAKj0XsbwGpc+RHccWK31 Znf0STWPS7uyexU07tV2FLRmdW8xZVlyke5er+KQHOkgdlaPO3RRrTBD1idPwana0LCy0e COjldBjzysxhRdHvEQ66GmSxjQ1jBg/3nhXCHuMX3fMUohfH6OsAxhVrV+MvCg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505478; 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=COGYr1lbubEBgG3WbXJ/GVEKTFAghSZ0FbZctrPsvVg=; b=2HZZaqcxlL+qDqMELaTxx4FwXn7FKdec/94PPTZ8yDAljReKhNA6udFZFPpDyzJyvrEkg4 6v533y9o3NKWbvCg== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 09/30] x86/microcode/intel: Switch to kvmalloc() References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:57:57 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 12 Sep 2023 00:58:45 -0700 (PDT) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776835698373335686 X-GMAIL-MSGID: 1776835698373335686 From: Thomas Gleixner Microcode blobs are getting larger and might soon reach the kmalloc() limit. Switch over kvmalloc(). Signed-off-by: Thomas Gleixner --- arch/x86/kernel/cpu/microcode/intel.c | 50 +++++++++++++++++----------------- 1 file changed, 26 insertions(+), 24 deletions(-) --- --- a/arch/x86/kernel/cpu/microcode/intel.c +++ b/arch/x86/kernel/cpu/microcode/intel.c @@ -14,7 +14,6 @@ #include #include #include -#include #include #include #include @@ -243,7 +242,7 @@ EXPORT_SYMBOL_GPL(intel_microcode_sanity static void update_ucode_pointer(struct microcode_intel *mc) { - kfree(ucode_patch_va); + kvfree(ucode_patch_va); /* * Save the virtual address for early loading and for eventual free @@ -254,11 +253,14 @@ static void update_ucode_pointer(struct static void save_microcode_patch(struct microcode_intel *patch) { - struct microcode_intel *mc; + unsigned int size = get_totalsize(&patch->hdr); + struct microcode_intel *mc = NULL; - mc = kmemdup(patch, get_totalsize(&patch->hdr), GFP_KERNEL); + mc = kvmemdup(patch, size, GFP_KERNEL); if (mc) update_ucode_pointer(mc); + else + pr_err("Unable to allocate microcode memory size: %u\n", size); } /* Scan CPIO for microcode matching the boot CPUs family, model, stepping */ @@ -530,36 +532,34 @@ static enum ucode_state read_ucode_intel if (!copy_from_iter_full(&mc_header, sizeof(mc_header), iter)) { pr_err("error! Truncated or inaccessible header in microcode data file\n"); - break; + goto fail; } mc_size = get_totalsize(&mc_header); if (mc_size < sizeof(mc_header)) { pr_err("error! Bad data in microcode data file (totalsize too small)\n"); - break; + goto fail; } - data_size = mc_size - sizeof(mc_header); if (data_size > iov_iter_count(iter)) { pr_err("error! Bad data in microcode data file (truncated file?)\n"); - break; + goto fail; } /* For performance reasons, reuse mc area when possible */ if (!mc || mc_size > curr_mc_size) { - vfree(mc); - mc = vmalloc(mc_size); + kvfree(mc); + mc = kvmalloc(mc_size, GFP_KERNEL); if (!mc) - break; + goto fail; curr_mc_size = mc_size; } memcpy(mc, &mc_header, sizeof(mc_header)); data = mc + sizeof(mc_header); if (!copy_from_iter_full(data, data_size, iter) || - intel_microcode_sanity_check(mc, true, MC_HEADER_TYPE_MICROCODE) < 0) { - break; - } + intel_microcode_sanity_check(mc, true, MC_HEADER_TYPE_MICROCODE) < 0) + goto fail; if (cur_rev >= mc_header.rev) continue; @@ -567,24 +567,26 @@ static enum ucode_state read_ucode_intel if (!intel_find_matching_signature(mc, uci->cpu_sig.sig, uci->cpu_sig.pf)) continue; - vfree(new_mc); + kvfree(new_mc); cur_rev = mc_header.rev; new_mc = mc; mc = NULL; } - vfree(mc); - - if (iov_iter_count(iter)) { - vfree(new_mc); - return UCODE_ERROR; - } + if (iov_iter_count(iter)) + goto fail; + kvfree(mc); if (!new_mc) return UCODE_NFOUND; ucode_patch_late = (struct microcode_intel *)new_mc; return UCODE_NEW; + +fail: + kvfree(mc); + kvfree(new_mc); + return UCODE_ERROR; } static bool is_blacklisted(unsigned int cpu) @@ -643,9 +645,9 @@ static enum ucode_state request_microcod static void finalize_late_load(int result) { if (!result) - save_microcode_patch(ucode_patch_late); - - vfree(ucode_patch_late); + update_ucode_pointer(ucode_patch_late); + else + kvfree(ucode_patch_late); ucode_patch_late = NULL; } From patchwork Tue Sep 12 07:57:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138016 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp253054vqx; Tue, 12 Sep 2023 01:16:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE7+16PQfYhBa3PJQPFPL486CTmIMNbZ59sTLlXnRASodIfKCJ0LUqe94nND2He3daUE5FU X-Received: by 2002:a17:902:8549:b0:1c1:d934:26b4 with SMTP id d9-20020a170902854900b001c1d93426b4mr9677938plo.66.1694506594781; Tue, 12 Sep 2023 01:16:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694506594; cv=none; d=google.com; s=arc-20160816; b=NT019TFfmgK/rD5qkQDBcNfmKWuyWSCwt9Lov7qEthqkE4hbJ6bIDDaMVaU7zggx5N gvxjRmef/LHLKDRMNTpbHNpbeMxKI4DDuq0zlgDROAIIjhp7bKcpMcfXCD7wCezJ/XUt E21YV3fX5+TOOtLkF1gN1WddWDB+jwjx4DBsgVn9dODHL2mzRXIHmQJ6zPt8vtxcI9F8 n+wRpXiwSL2XcO9I0d9f118xiYz4v3D/MGBQXNzTgeQkycgFK+0Epo+r1PfMtE7O5Zwh iaxheDzgnlcI11f3lv8HZShO+79dyAqHc7LhLbPCYMoF4xL59+zLSvVZrQ4fd2mgB7vY +9zw== 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=xw07BwAqDpIg1aejZ2/Mpq9I8tO564XE8NU6U0bYGnA=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=Zvq7pDvaMyUHHFo102LQiuqhG7LBxkw9hcowkcAP/GRGPi1kT/zSzIYPRzBsT7cPdl BqccgB6sqI3Jy9eQ9hTqMB301lTE+FIACho8BTgXQp51FVcv9cl1k4exOOOd0uF9NJ1M DiI/VCQmx1k+WLlmUE4aSjRz71h86knwVESwmQYCliIalihgCL8Lr70vn4L+LCwlpihq zy/wnpJv3l4IRSzKbD3eh9QKqefwKsd4z4G+i2KOrlvObKtkvdm1gwcmCOKS+hktSv6p sjWBH0QJQIE5D3TdWfZhNS/yGrOHBKSISTav0yYK2mdyhrHC/0DMaRmJtgFU7JvzTmW1 JlSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=f49RL7CZ; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b="StO5y/k6"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id o13-20020a170902d4cd00b001c0dab08decsi1844900plg.499.2023.09.12.01.16.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 01:16:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=f49RL7CZ; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b="StO5y/k6"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 7AE6B818D50C; Tue, 12 Sep 2023 00:59:22 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231915AbjILH6j (ORCPT + 39 others); Tue, 12 Sep 2023 03:58:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54170 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232046AbjILH6U (ORCPT ); Tue, 12 Sep 2023 03:58:20 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 656951715 for ; Tue, 12 Sep 2023 00:58:01 -0700 (PDT) Message-ID: <20230912065501.405080839@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505480; 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=xw07BwAqDpIg1aejZ2/Mpq9I8tO564XE8NU6U0bYGnA=; b=f49RL7CZ5JlbcLqs+Vgehqe/Ge2l61PlQY9TjXJLKs5K/AhPszMFATAe9FSENJv/x5lmy9 fdQTp6FR6zAidxZGW2R/TjnY/hdUThr47hWOkkU+zOyw2ScTOv5b9mDsP4Hc9zUpDIBFQM thfBP5zMxlobHVbsmHAwDcM0hczQvmpISja0R3v7mMeME3N41nTXdHv0P2HACiEqioBDs/ Iyvpm6V9LrwlaKD8hi7IXqwK82VsuqwJiB5I4A0fDWu1KmiXRkOmfMYsgCn/1dLSs83va6 4NV1RCj/M8CetjeSE/V7QlfsU2uweZTmCTjq/TxR7/nbKytC/NEy1b0POD9AEQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505480; 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=xw07BwAqDpIg1aejZ2/Mpq9I8tO564XE8NU6U0bYGnA=; b=StO5y/k6iq/fxKNq058YXPtrinnmV3mX3xUqO57L5MV6rmbNoOuJp/6JO2AGPc2/5XIioQ INYQke4DMRvnS/AQ== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 10/30] x86/microcode/intel: Unify microcode apply() functions References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:57:59 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Tue, 12 Sep 2023 00:59:22 -0700 (PDT) X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, PDS_OTHER_BAD_TLD,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776818946670430967 X-GMAIL-MSGID: 1776818946670430967 Deduplicate the early and late apply() functions. Signed-off-by: Thomas Gleixner --- arch/x86/kernel/cpu/microcode/intel.c | 105 +++++++++++----------------------- 1 file changed, 36 insertions(+), 69 deletions(-) --- a/arch/x86/kernel/cpu/microcode/intel.c +++ b/arch/x86/kernel/cpu/microcode/intel.c @@ -294,12 +294,11 @@ static __init struct microcode_intel *sc return size ? NULL : patch; } -static enum ucode_state apply_microcode_early(struct ucode_cpu_info *uci, bool early) +static enum ucode_state apply_microcode(struct ucode_cpu_info *uci, struct microcode_intel *mc, + u32 *cur_rev) { - struct microcode_intel *mc; - u32 rev, old_rev, date; + u32 rev; - mc = uci->mc; if (!mc) return UCODE_NFOUND; @@ -308,14 +307,12 @@ static enum ucode_state apply_microcode_ * operation - when the other hyperthread has updated the microcode * already. */ - rev = intel_get_microcode_revision(); - if (rev >= mc->hdr.rev) { - uci->cpu_sig.rev = rev; + *cur_rev = intel_get_microcode_revision(); + if (*cur_rev >= mc->hdr.rev) { + uci->cpu_sig.rev = *cur_rev; return UCODE_OK; } - old_rev = rev; - /* * Writeback and invalidate caches before updating microcode to avoid * internal issues depending on what the microcode is updating. @@ -330,13 +327,24 @@ static enum ucode_state apply_microcode_ return UCODE_ERROR; uci->cpu_sig.rev = rev; - - date = mc->hdr.date; - pr_info_once("updated early: 0x%x -> 0x%x, date = %04x-%02x-%02x\n", - old_rev, rev, date & 0xffff, date >> 24, (date >> 16) & 0xff); return UCODE_UPDATED; } +static enum ucode_state apply_microcode_early(struct ucode_cpu_info *uci, bool early) +{ + struct microcode_intel *mc = uci->mc; + enum ucode_state ret; + u32 cur_rev, date; + + ret = apply_microcode(uci, mc, &cur_rev); + if (ret == UCODE_UPDATED) { + date = mc->hdr.date; + pr_info_once("updated early: 0x%x -> 0x%x, date = %04x-%02x-%02x\n", + cur_rev, mc->hdr.rev, date & 0xffff, date >> 24, (date >> 16) & 0xff); + } + return ret; +} + static __init bool load_builtin_intel_microcode(struct cpio_data *cp) { unsigned int eax = 1, ebx, ecx = 0, edx; @@ -450,70 +458,29 @@ static int collect_cpu_info(int cpu_num, return 0; } -static enum ucode_state apply_microcode_intel(int cpu) +static enum ucode_state apply_microcode_late(int cpu) { struct ucode_cpu_info *uci = ucode_cpu_info + cpu; - struct cpuinfo_x86 *c = &cpu_data(cpu); - bool bsp = c->cpu_index == boot_cpu_data.cpu_index; - struct microcode_intel *mc; + struct microcode_intel *mc = ucode_patch_late; enum ucode_state ret; - static int prev_rev; - u32 rev; - - /* We should bind the task to the CPU */ - if (WARN_ON(raw_smp_processor_id() != cpu)) - return UCODE_ERROR; - - mc = ucode_patch_late; - if (!mc) - return UCODE_NFOUND; + u32 cur_rev; - /* - * Save us the MSR write below - which is a particular expensive - * operation - when the other hyperthread has updated the microcode - * already. - */ - rev = intel_get_microcode_revision(); - if (rev >= mc->hdr.rev) { - ret = UCODE_OK; - goto out; - } - - /* - * Writeback and invalidate caches before updating microcode to avoid - * internal issues depending on what the microcode is updating. - */ - native_wbinvd(); - - /* write microcode via MSR 0x79 */ - wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits); - - rev = intel_get_microcode_revision(); - - if (rev != mc->hdr.rev) { - pr_err("CPU%d update to revision 0x%x failed\n", - cpu, mc->hdr.rev); + if (WARN_ON_ONCE(smp_processor_id() != cpu)) return UCODE_ERROR; - } - if (bsp && rev != prev_rev) { - pr_info("updated to revision 0x%x, date = %04x-%02x-%02x\n", - rev, - mc->hdr.date & 0xffff, - mc->hdr.date >> 24, + ret = apply_microcode(uci, mc, &cur_rev); + if (ret != UCODE_UPDATED && ret != UCODE_OK) + return ret; + + if (!cpu && uci->cpu_sig.rev != cur_rev) { + pr_info("Updated to revision 0x%x, date = %04x-%02x-%02x\n", + uci->cpu_sig.rev, mc->hdr.date & 0xffff, mc->hdr.date >> 24, (mc->hdr.date >> 16) & 0xff); - prev_rev = rev; } - ret = UCODE_UPDATED; - -out: - uci->cpu_sig.rev = rev; - c->microcode = rev; - - /* Update boot_cpu_data's revision too, if we're on the BSP: */ - if (bsp) - boot_cpu_data.microcode = rev; + cpu_data(cpu).microcode = uci->cpu_sig.rev; + if (!cpu) + boot_cpu_data.microcode = uci->cpu_sig.rev; return ret; } @@ -654,7 +621,7 @@ static void finalize_late_load(int resul static struct microcode_ops microcode_intel_ops = { .request_microcode_fw = request_microcode_fw, .collect_cpu_info = collect_cpu_info, - .apply_microcode = apply_microcode_intel, + .apply_microcode = apply_microcode_late, .finalize_late_load = finalize_late_load, }; From patchwork Tue Sep 12 07:58:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138328 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp560831vqx; Tue, 12 Sep 2023 10:19:21 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHwuSbJRIK6KG6pX6DLShuxtfS4cnP9PEPOyzQnvDErXx8+4t47e44WATibCgj4t7+UJ8N0 X-Received: by 2002:a05:6a20:8e19:b0:129:d944:2e65 with SMTP id y25-20020a056a208e1900b00129d9442e65mr58484pzj.13.1694539160839; Tue, 12 Sep 2023 10:19:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694539160; cv=none; d=google.com; s=arc-20160816; b=OqwD9X/lt2lJWexM7qr5NegRZz6mxs2NSGuqjeDi9p0oDXDOJ7Ai6YzYE3HY/YkpdS c7TexkcNM+hh7wOB29VNE9FyOPtac/AX/rvGQEhyHdhYkiguE3YQJG7vsKtNFadFC+18 MBGqUkbbsCIIHi4GxzPxGXDrWzFoB/o1xGkrdaE20CbAJxDk6KYyz9zywkfdYth3PU9M bvp5aeFpT0fldVRziXRIYpWBs1zI/2jUaBmf1ssrrFo3o4rz1/xbNr89W9EW1piJrz+d XbrPEvFEgbd4Gw6BJXh193IClHmzr1VeLoYP0BBzOGaLcVqhjkCsUxbfyHaH0runljqx txIg== 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=mwSPC6sBO3Mtax7VG/Ayowc5WMyDJfVT9RHyC3osMGE=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=BhAY3UIWRuZd1Bwmxj6wZlSOPcq5grh0KDjqw7IHz7HgJrey13x3tyOK1IfNjcpHFT O/s5DLleCEODqFR1/vnkPeN43WCAhyDRz1sHrHH0PxZ1CVAawBfzSLK9YUmVxOQCy7em rjYfF9+fSn8dOrRAwAA/GB64OuC9FueYlSKHXzxbLNhmGtCJWWE3qtyd1BYIS29vrwB1 LP24L3o/MhEcM0cfbb0Nk22DzqDvXjIboN6CNX8wWsyonGnShfAFz3xWGNyY8qxyv0TA PuJcdRATlwJnHogXiljN9udN7J+hwrPrKWnFNFd+7qq9ybc8+o9t+xOaeHO5nT5VNoZq ZSLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=oA2gkW5v; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id b12-20020aa7870c000000b0068895dea43csi6294501pfo.78.2023.09.12.10.19.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 10:19:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=oA2gkW5v; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 742A1817710F; Tue, 12 Sep 2023 00:58:56 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231925AbjILH6m (ORCPT + 39 others); Tue, 12 Sep 2023 03:58:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232057AbjILH6V (ORCPT ); Tue, 12 Sep 2023 03:58:21 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1A56B1722 for ; Tue, 12 Sep 2023 00:58:02 -0700 (PDT) Message-ID: <20230912065501.467154157@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505481; 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=mwSPC6sBO3Mtax7VG/Ayowc5WMyDJfVT9RHyC3osMGE=; b=oA2gkW5v4GuBzi/G26ehen714YPvWpaKoetk19ENGTJzeoY/VYc//e6lekJylSeeDH3FH1 pr2dURiPAk2SjSeG/00Tc2BKsjpOZv+TAvAA14CCTl2pYQqzocyNfcY0b9mBrwXLaJlBzp UFW0HbY4xd86eu7IO6mHhq05GSupWYOtaeKYgNzkvtvFutoEqEilCIkZTKag7mXcmqNnnS RKk85lkmYNFzUqECEoCdHXwp229FhdDUN0jXCmd/jhZOiGSo0LHKKc5LWHcLszOetzcf9g Xi6uHFOGlm3bI66asn1201rPioAbb+ZPTzg7H9yKS1XSHN9T055lJzQ0kfTayw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505481; 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=mwSPC6sBO3Mtax7VG/Ayowc5WMyDJfVT9RHyC3osMGE=; b=bM5+0jXDtcvIORAXYZkEd+WvG4sNwRIP71gTU+S6h950zXuQ3l4L8m6asxopwEAmugncTD x1yLfgAc1yxRnoCg== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 11/30] x86/microcode/intel: Rework intel_cpu_collect_info() References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:58:00 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Tue, 12 Sep 2023 00:58:56 -0700 (PDT) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776853095333057350 X-GMAIL-MSGID: 1776853095333057350 Nothing needs struct ucode_cpu_info. Make it take struct cpu_signature, let it return a boolean and simplify the implementation. Rename it now that the silly name clash with collect_cpu_info() is gone. Signed-off-by: Thomas Gleixner --- V2: New patch --- arch/x86/include/asm/cpu.h | 4 ++-- arch/x86/kernel/cpu/microcode/intel.c | 33 +++++++++------------------------ drivers/platform/x86/intel/ifs/load.c | 8 +++----- 3 files changed, 14 insertions(+), 31 deletions(-) --- a/arch/x86/include/asm/cpu.h +++ b/arch/x86/include/asm/cpu.h @@ -73,9 +73,9 @@ static inline void init_ia32_feat_ctl(st extern __noendbr void cet_disable(void); -struct ucode_cpu_info; +struct cpu_signature; -int intel_cpu_collect_info(struct ucode_cpu_info *uci); +void intel_collect_cpu_info(struct cpu_signature *sig); static inline bool intel_cpu_signatures_match(unsigned int s1, unsigned int p1, unsigned int s2, unsigned int p2) --- a/arch/x86/kernel/cpu/microcode/intel.c +++ b/arch/x86/kernel/cpu/microcode/intel.c @@ -66,36 +66,21 @@ static inline unsigned int exttable_size return et->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE; } -int intel_cpu_collect_info(struct ucode_cpu_info *uci) +void intel_collect_cpu_info(struct cpu_signature *sig) { - unsigned int val[2]; - unsigned int family, model; - struct cpu_signature csig = { 0 }; - unsigned int eax, ebx, ecx, edx; + sig->sig = cpuid_eax(1); + sig->pf = 0; + sig->rev = intel_get_microcode_revision(); - memset(uci, 0, sizeof(*uci)); + if (x86_model(sig->sig) >= 5 || x86_family(sig->sig) > 6) { + unsigned int val[2]; - eax = 0x00000001; - ecx = 0; - native_cpuid(&eax, &ebx, &ecx, &edx); - csig.sig = eax; - - family = x86_family(eax); - model = x86_model(eax); - - if (model >= 5 || family > 6) { /* get processor flags from MSR 0x17 */ native_rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]); - csig.pf = 1 << ((val[1] >> 18) & 7); + sig->pf = 1 << ((val[1] >> 18) & 7); } - - csig.rev = intel_get_microcode_revision(); - - uci->cpu_sig = csig; - - return 0; } -EXPORT_SYMBOL_GPL(intel_cpu_collect_info); +EXPORT_SYMBOL_GPL(intel_collect_cpu_info); /* * Returns 1 if update has been found, 0 otherwise. @@ -381,7 +366,7 @@ static __init struct microcode_intel *ge if (!(cp.data && cp.size)) return NULL; - intel_cpu_collect_info(uci); + intel_collect_cpu_info(&uci->cpu_sig); return scan_microcode(cp.data, cp.size, uci); } --- a/drivers/platform/x86/intel/ifs/load.c +++ b/drivers/platform/x86/intel/ifs/load.c @@ -227,7 +227,7 @@ static int scan_chunks_sanity_check(stru static int image_sanity_check(struct device *dev, const struct microcode_header_intel *data) { - struct ucode_cpu_info uci; + struct cpu_signature sig; /* Provide a specific error message when loading an older/unsupported image */ if (data->hdrver != MC_HEADER_TYPE_IFS) { @@ -240,11 +240,9 @@ static int image_sanity_check(struct dev return -EINVAL; } - intel_cpu_collect_info(&uci); + intel_collect_cpu_info(&sig); - if (!intel_find_matching_signature((void *)data, - uci.cpu_sig.sig, - uci.cpu_sig.pf)) { + if (!intel_find_matching_signature((void *)data, sig.sig, sig.pf)) { dev_err(dev, "cpu signature, processor flags not matching\n"); return -EINVAL; } From patchwork Tue Sep 12 07:58:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138685 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp915239vqx; Wed, 13 Sep 2023 00:43:20 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGZAcmPtf2bUURpHSYPFEXWZ5FiaTqZUE/gP1YMdEVxVw8oQac6PpDNCbo6fgadEU8xw8go X-Received: by 2002:a05:6a00:4791:b0:68e:2c2a:aa1d with SMTP id dh17-20020a056a00479100b0068e2c2aaa1dmr5726808pfb.11.1694591000582; Wed, 13 Sep 2023 00:43:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694591000; cv=none; d=google.com; s=arc-20160816; b=g89/blPpmM9fTa11rXnjAbTcW7pmVqRT7gWXn+sNoW6wBrqAKQDkUkxeJYbOKMscol jVSyVMKHly7kVhx6ZSJAELQaNMs1Xh1FvGw95YVf6a25eRbMl4WV2WXw0QBe/ZhL2BCy bzMvn4Tx+9CE/n8M1SZsjRP0ngXcZ17BMtOXF0wXblJOWd6oGqswzW8ql0fxU/iodUOB E2j0E/1tyq+2GAxiDIv9DCKLW0CoGSvD1cDU34Q/hKpurCvqWTHsuGLnipjyt9qAmLGh XiXdFzNWJEEe4NjUC+lWqwPCm4AmHgj2MDRhlabSUnYUo8cQVQfKxSLkoeEMIovY/LhR Fx3w== 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=ihivJowKww+fe1UCzdxyPRQezAmQeOqgMJYs1huTU/U=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=GJfYtARSwoBXQIdGAnIZRpaZp7s+xIBJkcFanC9JJ+M5JHZimrABbEXqklubjpv1wd TpJ9+cDbIaxaUCsjCJHVjORn/FyhlEctSmA9p/llExZaVe5rKX4MuPnjy04Gy+XvnNgn Pxt0tvh0DFxxAbca2a8RIHICB4P5ElCDuLzUHlxBVy7cwzgyJXZY4rRkGKXemjcKDKVJ i1HrDioJu8ee6dZUvzBXyUWhDrg3734XOUrQZpV8Mgbr3JnL57xewtFlFFTWPs+8X9VQ Ko1HjXtzca7qiM94ArfJ/WaK28NLTsW4BNPBfJJCV7/qcPJ7oSHTiFRoumdzu0Yel3r8 T61Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=wj1dVLTG; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id z16-20020a656650000000b00573fd89e62asi9336002pgv.483.2023.09.13.00.43.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 00:43:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=wj1dVLTG; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 168BA816EF53; Tue, 12 Sep 2023 00:59:03 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232057AbjILH6o (ORCPT + 39 others); Tue, 12 Sep 2023 03:58:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55214 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232095AbjILH62 (ORCPT ); Tue, 12 Sep 2023 03:58:28 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6651C1733 for ; Tue, 12 Sep 2023 00:58:04 -0700 (PDT) Message-ID: <20230912065501.530637507@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505483; 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=ihivJowKww+fe1UCzdxyPRQezAmQeOqgMJYs1huTU/U=; b=wj1dVLTGF6RgQZ93P3v00gpeCt+n3ShYIi8VyQFE55ZOsZOXkp6ajCHFqdMtSDVxOo4qVm s9AoaiRwqEmjibvFZnZ02gCd0yYaNngTLCZqDYCOG5ukSHrxA8gn775Kd++ALwx8y3nlOd Bv875kw34ZDaO/BgQEecef9lrugWjpN7o1YHSLZXFc2HjjejFqfzVNZ0g6mq8Z+vCgW8tt +uBcYa128XANDG0st3buDjsZo2mSUOW4fEJjRwKjuDGu49TyNYqcUwmeebS5G+bymprNke UmBec4clGrp9SVwcPic0rnNj0Cv0hGb2vzGshg6+uPhzouO9mxNgekxQbrUibA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505483; 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=ihivJowKww+fe1UCzdxyPRQezAmQeOqgMJYs1huTU/U=; b=pjHTtTC9LC4Q6P8H4sYw/dU1Irf8uOq7L4qiAeB9uAO0JCf+WSRPi7+kH1Vcpg4K5aOnMk bkHtbiYKaEiWYLCQ== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 12/30] x86/microcode/intel: Reuse intel_cpu_collect_info() References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:58:02 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 12 Sep 2023 00:59:03 -0700 (PDT) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776907452821683618 X-GMAIL-MSGID: 1776907452821683618 No point for an almost duplicate function. Signed-off-by: Thomas Gleixner Signed-off-by: Borislav Petkov (AMD) --- V2: New patch --- arch/x86/kernel/cpu/microcode/intel.c | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) --- a/arch/x86/kernel/cpu/microcode/intel.c +++ b/arch/x86/kernel/cpu/microcode/intel.c @@ -425,21 +425,7 @@ void reload_ucode_intel(void) static int collect_cpu_info(int cpu_num, struct cpu_signature *csig) { - struct cpuinfo_x86 *c = &cpu_data(cpu_num); - unsigned int val[2]; - - memset(csig, 0, sizeof(*csig)); - - csig->sig = cpuid_eax(0x00000001); - - if ((c->x86_model >= 5) || (c->x86 > 6)) { - /* get processor flags from MSR 0x17 */ - rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]); - csig->pf = 1 << ((val[1] >> 18) & 7); - } - - csig->rev = c->microcode; - + intel_collect_cpu_info(csig); return 0; } From patchwork Tue Sep 12 07:58:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138140 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp366367vqx; Tue, 12 Sep 2023 05:22:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFvsJhd1ymhh2H6vog/TC7dJoe5VLvTcqo3EXvTctLLAZnA/rb2MnUKYHGcDZyjDfhy/8Nx X-Received: by 2002:a17:90b:1805:b0:26d:20b8:445d with SMTP id lw5-20020a17090b180500b0026d20b8445dmr9753582pjb.9.1694521360312; Tue, 12 Sep 2023 05:22:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694521360; cv=none; d=google.com; s=arc-20160816; b=PoJ5jQ0kkVR+I52BxXQQhiVQBqqT/x96wuA+1l3PvWXhFa48QeovyFNWlKaJv/ShQI VDHFymNx82MdbGjU4sUKHs6bno0QRG0Yb1xLqSnUhxuU7rvJ/Tn6oh6eQYYHC6vNn7Z6 Ubtn+bMNlcokBbDziHwXIsCe/CfUp2Rnvbfvj90Vg6Rca7yt9V/E0zyKw2MDyMJEmjzS 566EDjzPXUJhohdyAT7IkMAw7dqVK1FvvHYMo/JKUYaSkguGuT/NfnsIZGSaOTGf5uaI 9LPGOcMt5kMpLVvn/t6zvcIE0OiE+ITeYYk7wY6SLHi09mICtGj/iL9laxswNOb9lrGN acCw== 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=KPQ747hpIr0GFPtwS8ofAyiGhdmmSNrNmfk+STn9gD4=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=GH8QK0p3qgxzlrF7woD+03iAVq0/utXomEqCPZfL757c96tVfAfScSmGZ+UmsW1Jpe hhgHdgcQYAxGgp5B2F2EcbcamKtc5zhUSLVu2bQkyppNQ99iF3FlfY10tnWj2Eaifw5D Ddm+AxYHMRxF19pSeD/fB4GAc8Vn+rqrkT8dJ8XqtYO1Mpsf1EXFjYuttChWcX5gGh+x ozNSdYSgsBFUc4cMiiC9Mp9LGFhNEd8RMVV3/Q+fA2p+nRglBBZapE+Mj/OhA2lIBoiU yAS7Jo73EA3PNhqjbI/O5ZJKAjNGriWnJxLYl6Mmr5hcPayFs3XpbeqsP8s0i9VQJXXb CvjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=nupoqRkk; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id oa18-20020a17090b1bd200b002734f48cfd6si7942815pjb.155.2023.09.12.05.22.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 05:22:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=nupoqRkk; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 1516F82AE67A; Tue, 12 Sep 2023 00:59:04 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.8 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232116AbjILH6v (ORCPT + 39 others); Tue, 12 Sep 2023 03:58:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232115AbjILH6h (ORCPT ); Tue, 12 Sep 2023 03:58:37 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC45F198D for ; Tue, 12 Sep 2023 00:58:05 -0700 (PDT) Message-ID: <20230912065501.592043301@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505484; 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=KPQ747hpIr0GFPtwS8ofAyiGhdmmSNrNmfk+STn9gD4=; b=nupoqRkkIetoIHhWk6H9LzlH8Ne/sqhNyLvsyTVLCVLoZ4eXAX+jW4SRgsh2hJj2QOZrn3 e42tVNYYz7sjbsh4neSV1SA1oBx6K7absoHOn2e2Ni5xj4IYrwrLpB/pBfNRtUbksd2zn8 h/zHZjGsifAzAVA27pYZcAlv7ulSboghyJX7hN1m1KSzDeBFkItCuMLnXuDMhfjNdQ/Plu 0N9SYtPiVsAJdU4d2/5hIAOngZxprsdnz+23SWfA66Raw9bZLtaiRf/d6mwuHSlQt1D3YX 1PcPCLM0pHbtmin1KHFgJkTpS0FfpVaV8d51oB5hKhibVQP9m2mYHQ7PYvyAFg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505484; 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=KPQ747hpIr0GFPtwS8ofAyiGhdmmSNrNmfk+STn9gD4=; b=2bD5uX3YOs6c2shQYU267D6hgBS7iSzQ+BPkoXbrdUnEV7jL9wwgAurfQLoEg5CZ6B5L7C WMZ2IbwqgdWp7GDg== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 13/30] x86/microcode/intel: Rework intel_find_matching_signature() References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:58:04 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 12 Sep 2023 00:59:04 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776834429542880910 X-GMAIL-MSGID: 1776834429542880910 Take a cpu_signature argument and work from there. Move the match() helper next to the callsite as there is no point for having it in a header. Signed-off-by: Thomas Gleixner --- V2: New patch --- arch/x86/include/asm/cpu.h | 16 +--------------- arch/x86/kernel/cpu/microcode/intel.c | 31 +++++++++++++++++++------------ drivers/platform/x86/intel/ifs/load.c | 2 +- 3 files changed, 21 insertions(+), 28 deletions(-) --- a/arch/x86/include/asm/cpu.h +++ b/arch/x86/include/asm/cpu.h @@ -77,22 +77,8 @@ struct cpu_signature; void intel_collect_cpu_info(struct cpu_signature *sig); -static inline bool intel_cpu_signatures_match(unsigned int s1, unsigned int p1, - unsigned int s2, unsigned int p2) -{ - if (s1 != s2) - return false; - - /* Processor flags are either both 0 ... */ - if (!p1 && !p2) - return true; - - /* ... or they intersect. */ - return p1 & p2; -} - extern u64 x86_read_arch_cap_msr(void); -int intel_find_matching_signature(void *mc, unsigned int csig, int cpf); +bool intel_find_matching_signature(void *mc, struct cpu_signature *sig); int intel_microcode_sanity_check(void *mc, bool print_err, int hdr_type); extern struct cpumask cpus_stop_mask; --- a/arch/x86/kernel/cpu/microcode/intel.c +++ b/arch/x86/kernel/cpu/microcode/intel.c @@ -82,29 +82,36 @@ void intel_collect_cpu_info(struct cpu_s } EXPORT_SYMBOL_GPL(intel_collect_cpu_info); -/* - * Returns 1 if update has been found, 0 otherwise. - */ -int intel_find_matching_signature(void *mc, unsigned int csig, int cpf) +static inline bool cpu_signatures_match(struct cpu_signature *s1, unsigned int sig2, + unsigned int pf2) +{ + if (s1->sig != sig2) + return false; + + /* Processor flags are either both 0 or they intersect. */ + return ((!s1->pf && !pf2) || (s1->pf & pf2)); +} + +bool intel_find_matching_signature(void *mc, struct cpu_signature *sig) { struct microcode_header_intel *mc_hdr = mc; - struct extended_sigtable *ext_hdr; struct extended_signature *ext_sig; + struct extended_sigtable *ext_hdr; int i; - if (intel_cpu_signatures_match(csig, cpf, mc_hdr->sig, mc_hdr->pf)) - return 1; + if (cpu_signatures_match(sig, mc_hdr->sig, mc_hdr->pf)) + return true; /* Look for ext. headers: */ if (get_totalsize(mc_hdr) <= intel_microcode_get_datasize(mc_hdr) + MC_HEADER_SIZE) - return 0; + return false; ext_hdr = mc + intel_microcode_get_datasize(mc_hdr) + MC_HEADER_SIZE; ext_sig = (void *)ext_hdr + EXT_HEADER_SIZE; for (i = 0; i < ext_hdr->count; i++) { - if (intel_cpu_signatures_match(csig, cpf, ext_sig->sig, ext_sig->pf)) - return 1; + if (cpu_signatures_match(sig, ext_sig->sig, ext_sig->pf)) + return true; ext_sig++; } return 0; @@ -265,7 +272,7 @@ static __init struct microcode_intel *sc intel_microcode_sanity_check(data, false, MC_HEADER_TYPE_MICROCODE) < 0) break; - if (!intel_find_matching_signature(data, uci->cpu_sig.sig, uci->cpu_sig.pf)) + if (!intel_find_matching_signature(data, &uci->cpu_sig)) continue; /* Check whether there is newer microcode */ @@ -502,7 +509,7 @@ static enum ucode_state read_ucode_intel if (cur_rev >= mc_header.rev) continue; - if (!intel_find_matching_signature(mc, uci->cpu_sig.sig, uci->cpu_sig.pf)) + if (!intel_find_matching_signature(mc, &uci->cpu_sig)) continue; kvfree(new_mc); --- a/drivers/platform/x86/intel/ifs/load.c +++ b/drivers/platform/x86/intel/ifs/load.c @@ -242,7 +242,7 @@ static int image_sanity_check(struct dev intel_collect_cpu_info(&sig); - if (!intel_find_matching_signature((void *)data, sig.sig, sig.pf)) { + if (!intel_find_matching_signature((void *)data, &sig)) { dev_err(dev, "cpu signature, processor flags not matching\n"); return -EINVAL; } From patchwork Tue Sep 12 07:58:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138214 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp445496vqx; Tue, 12 Sep 2023 07:26:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFKHoia+5R/EHW7QlfRAftqxYfH2sr+Hd9iXeuFL55E8ii2Ev04boUWN0XEzTGQ6lApZhCr X-Received: by 2002:a17:902:d4c6:b0:1c3:8464:cad1 with SMTP id o6-20020a170902d4c600b001c38464cad1mr12836775plg.26.1694528806912; Tue, 12 Sep 2023 07:26:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694528806; cv=none; d=google.com; s=arc-20160816; b=L3Ac51xNvTkUwOrwhER9F54BfN14t72tO82t0ty+Y92U4lYK1t2QPlC2XSyFmeYtTw B9E0z69/uJFBK3ErezAMbWVT1CiNUwjgC9KgQm4YjWa+o7GpzLciVIpvGt1vLZGP7eFr efKyVUUVok9rwIOGBp8oTg1aBN3QowpPJveL/blkjeonX+rgZxWQd0Znb/Gbo/tVrvZn 0UjU/9QPRiQAzRLn/P1SW+cC27g+QfSAujrchpTRL2y6pOw1qT0ZTessV7ie17S/1VRr OFYNO91tOiyV0N4NONHmG6mA1onLw39phsvDjaHJzdp4cVleJ7cc36zdJ56PJBl4SM5M VODg== 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=kkIW+v+zNIk9lQpoyroVUgRr0ogOwnyVqvNr/VLjf7s=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=NvMZfe+KxlnRUhc9u3ComfKhzte0df60GtKNi3jOKHbgIlfxqR4bUmb9cPkiibIjTX nDLYGCh5ZkxkDY3wyZkgwJOLTOz/wIwuOPORllsOIUnB+QIjBilQUkYkcSuLRaR1XlEe XfVWtbUC6GKlgHNXHhouwuqli6H7Y4aoUSeqsOK028+LPdGKRbp8+c1rFyaTTi1R2elk Fhcby1ugHF1E+DgewavNpPBL5Yrgjm0SYhnlG2xMrcn4ikj/BxhwLLSYL7sMcGiB78d6 XxCvhHOqEFh6+EKMG2G+In1SliSq/cylirBgzW85Fs+n2VXRi7yEoKgmGmdkhc8n6k6i I/GA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=MmUz0z9x; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=k8+2iG5J; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id lq13-20020a170903144d00b001bd9c43c896si7738196plb.287.2023.09.12.07.26.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 07:26:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=MmUz0z9x; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=k8+2iG5J; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id B9A4481D6DB1; Tue, 12 Sep 2023 00:59:11 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231996AbjILH6y (ORCPT + 39 others); Tue, 12 Sep 2023 03:58:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232127AbjILH6i (ORCPT ); Tue, 12 Sep 2023 03:58:38 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8598219A1 for ; Tue, 12 Sep 2023 00:58:07 -0700 (PDT) Message-ID: <20230912065501.656264495@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505486; 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=kkIW+v+zNIk9lQpoyroVUgRr0ogOwnyVqvNr/VLjf7s=; b=MmUz0z9xeJcrJnxPWAhM0Sg9irL5LYgARz0uyeEaDH49TpRkPWpFfdIeun756Hw9CzJCgr HcE0+36CM8yqUrZTC3+N4XKggFfmqs2nEeTMUnbrtSA4jYJdoX0SnTEhdUuxl9wTCoqcmF cJfWnR6QFOJNGFZQ2Yn826PEDE0YPyW+1TSBpaa4E6a+8xcf5iVKMGxq9iL/v0X5u1pvTh QfHnC59V/7lkdTO8uDeUTXJ73kaVBLIjBrYgUSSg1rrWw05XdnPI2kFESehEwXhV+bS8LL i8ECiTGFkpP2+uqiFvEGf7ArEgcC/uEfmOCVnq1HmHpawVwzWGAVvFa7CU4qzA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505486; 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=kkIW+v+zNIk9lQpoyroVUgRr0ogOwnyVqvNr/VLjf7s=; b=k8+2iG5JfnEGJAuQRw8Z7ZzyuRb880eqYgK9XpNXEhGaZUVQv5PAFFf05u5VWP+04t5PSY jE+MezHcy/Fb0gAg== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 14/30] x86/microcode/amd: Read revision from hardware in collect_cpu_info_amd() References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:58:05 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 12 Sep 2023 00:59:11 -0700 (PDT) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776842237757282028 X-GMAIL-MSGID: 1776842237757282028 Prepare to decrapify the core initialization logic which invokes microcode_ops::apply_microcode() several times just to set cpu_data::microcode. Signed-off-by: Thomas Gleixner --- V2: New patch --- arch/x86/kernel/cpu/microcode/amd.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) --- a/arch/x86/kernel/cpu/microcode/amd.c +++ b/arch/x86/kernel/cpu/microcode/amd.c @@ -673,12 +673,12 @@ void reload_ucode_amd(unsigned int cpu) static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig) { - struct cpuinfo_x86 *c = &cpu_data(cpu); struct ucode_cpu_info *uci = ucode_cpu_info + cpu; + u32 dummy __always_unused; struct ucode_patch *p; csig->sig = cpuid_eax(0x00000001); - csig->rev = c->microcode; + rdmsr(MSR_AMD64_PATCH_LEVEL, csig->rev, dummy); /* * a patch could have been loaded early, set uci->mc so that From patchwork Tue Sep 12 07:58:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138198 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp428281vqx; Tue, 12 Sep 2023 07:03:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG7VeWBwnXEaTK3osm34RNbxMk1vRj+kO8pNAbCph3mTrS2YPGcrscmJF0lzePAQVd+Qx8F X-Received: by 2002:a05:6a00:2290:b0:68e:2f6e:b4c0 with SMTP id f16-20020a056a00229000b0068e2f6eb4c0mr11559106pfe.28.1694527409885; Tue, 12 Sep 2023 07:03:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694527409; cv=none; d=google.com; s=arc-20160816; b=oM6mC2ni/TpejIX2trNLmI80Kue9OHag1ww3Ac6uIYtMjYNutOwAYjYNXunM2Xlf+o WNKqlWCjbwH5Zvvc2vstRyu0szb89Q2M56G15xUzAPO69WdiRtokf94JJ5esL47pid8q Rb61yvQzZ1rV7PSoQhXYPFciUNWUvnsVsCNlyKF7NOv4pBS1eWLBPy6f7LeQ+LW0AYyo mBQCJxO0X4n6tTNJFmMECX9dkMUA9DDACDTSIqB0cHTWUszWP0jqG+fMZ4PYoAqqAg+5 2S4kLLSkaySJTVTmkLgu+JqPKrRWrG3w+MBlAS+qAqX5vV6JL2ON+QpfRaYHkMFmvLY+ aAWQ== 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=BeckiNcudXRg0sDRJH13jVMFBVk42CtM+dSN32B7aSQ=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=oZomQJe6+dFiu27gZpiI9+mTVHIhKlA4mC6G08aihvDeoVt36OWaDg0vNSoj5Lnuz+ 5iV9ORxu+IHzFhmC1wzpFwlw8cXnAVGeE2IHrBrtCkxkaEWd6wiju2CNGCcH8/lxLbby qklIWci3MVqTepouykj99uUb1jChmNqZyZIB/5y+XeBkoHYpSrTc9XkozFhT8TpKL0WG JRvKNm2NcBsOgT+BojvLkh0Fb0WLAmslRj/7wVA2kg8wqwcmbHVoOHe5uxUN3T1s/9J0 sYHtiy6dOZl0YQtIEih2x3AyMkNTiXTlC0XxIJkyK+gCKjHwAtUXSTlVZmTz7Xp81+O5 +tjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=TZAUUZfb; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id bq7-20020a056a000e0700b0068e3707f02bsi5306963pfb.214.2023.09.12.07.03.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 07:03:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=TZAUUZfb; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 2234881CDB8E; Tue, 12 Sep 2023 01:00:54 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.8 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232083AbjILH64 (ORCPT + 39 others); Tue, 12 Sep 2023 03:58:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231990AbjILH6j (ORCPT ); Tue, 12 Sep 2023 03:58:39 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 310FD19B5 for ; Tue, 12 Sep 2023 00:58:09 -0700 (PDT) Message-ID: <20230912065501.717580036@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505487; 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=BeckiNcudXRg0sDRJH13jVMFBVk42CtM+dSN32B7aSQ=; b=TZAUUZfbFIkMxB7TDFN3B3QXILFVgVbeA1b5O7xkC1F9oP4ZMe+6z+rtPLUlE+ZAGz8l0a afdzzj0ff2JLqjXzRKIH4plQ6scDHi6HMOEGW9c9pAyc/7HEs/lp2fV/8cnKR3v6ya/UhU 9ZHT4ZC/LpzVDYmOVIGAhdugWGwGVJMSKPPrfgm92ALWajkz6FlfL+XCEtqyjm/F/I7qrf m0vDkRkgeWaAHyQRJSTpT/V4gX9bI3hq+KUw+/vStE7HtSwagChhDYEcdxI7mzhHrCHvEt pU7aU2maz4YKrO0cph1iTe4Blr/svchFrGRDae2Y72jiodCKaFTQIuT1YFRIkA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505487; 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=BeckiNcudXRg0sDRJH13jVMFBVk42CtM+dSN32B7aSQ=; b=NeDM9/W0hh5OVKj/sdHfdbGu9I07vQP2nNbKL1tI6b0H388qyyN5JyLK0eqdDShP9FRC/J IrZB9EJ2BrKHT9BQ== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 15/30] x86/microcode: Remove pointless apply() invocation References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:58:07 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 12 Sep 2023 01:00:54 -0700 (PDT) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776840773287504056 X-GMAIL-MSGID: 1776840773287504056 Microcode is applied on the APs during early bringup. There is no point in trying to apply the microcode again during the hotplug operations and neither at the point where the microcode device is initialized. Collect CPU info and microcode revision in setup_online_cpu() for now. This will move to the CPU hotplug callback in the next step. Signed-off-by: Thomas Gleixner --- V2: New patch --- arch/x86/kernel/cpu/microcode/core.c | 34 ++++++---------------------------- include/linux/cpuhotplug.h | 1 - 2 files changed, 6 insertions(+), 29 deletions(-) --- a/arch/x86/kernel/cpu/microcode/core.c +++ b/arch/x86/kernel/cpu/microcode/core.c @@ -511,17 +511,6 @@ static void microcode_fini_cpu(int cpu) microcode_ops->microcode_fini_cpu(cpu); } -static enum ucode_state microcode_init_cpu(int cpu) -{ - struct ucode_cpu_info *uci = ucode_cpu_info + cpu; - - memset(uci, 0, sizeof(*uci)); - - microcode_ops->collect_cpu_info(cpu, &uci->cpu_sig); - - return microcode_ops->apply_microcode(cpu); -} - /** * microcode_bsp_resume - Update boot CPU microcode during resume. */ @@ -540,15 +529,6 @@ static struct syscore_ops mc_syscore_ops .resume = microcode_bsp_resume, }; -static int mc_cpu_starting(unsigned int cpu) -{ - enum ucode_state err = microcode_ops->apply_microcode(cpu); - - pr_debug("%s: CPU%d, err: %d\n", __func__, cpu, err); - - return err == UCODE_ERROR; -} - static int mc_cpu_online(unsigned int cpu) { struct device *dev = get_cpu_device(cpu); @@ -576,14 +556,14 @@ static int mc_cpu_down_prep(unsigned int static void setup_online_cpu(struct work_struct *work) { int cpu = smp_processor_id(); - enum ucode_state err; + struct ucode_cpu_info *uci = ucode_cpu_info + cpu; - err = microcode_init_cpu(cpu); - if (err == UCODE_ERROR) { - pr_err("Error applying microcode on CPU%d\n", cpu); - return; - } + memset(uci, 0, sizeof(*uci)); + microcode_ops->collect_cpu_info(cpu, &uci->cpu_sig); + cpu_data(cpu).microcode = uci->cpu_sig.rev; + if (!cpu) + boot_cpu_data.microcode = uci->cpu_sig.rev; mc_cpu_online(cpu); } @@ -636,8 +616,6 @@ static int __init microcode_init(void) schedule_on_each_cpu(setup_online_cpu); register_syscore_ops(&mc_syscore_ops); - cpuhp_setup_state_nocalls(CPUHP_AP_MICROCODE_LOADER, "x86/microcode:starting", - mc_cpu_starting, NULL); cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "x86/microcode:online", mc_cpu_online, mc_cpu_down_prep); --- a/include/linux/cpuhotplug.h +++ b/include/linux/cpuhotplug.h @@ -156,7 +156,6 @@ enum cpuhp_state { CPUHP_AP_IRQ_LOONGARCH_STARTING, CPUHP_AP_IRQ_SIFIVE_PLIC_STARTING, CPUHP_AP_ARM_MVEBU_COHERENCY, - CPUHP_AP_MICROCODE_LOADER, CPUHP_AP_PERF_X86_AMD_UNCORE_STARTING, CPUHP_AP_PERF_X86_STARTING, CPUHP_AP_PERF_X86_AMD_IBS_STARTING, From patchwork Tue Sep 12 07:58:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138134 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp363592vqx; Tue, 12 Sep 2023 05:17:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHekImx7nU0hvZo3awouLcbXM1r9zBSKAlgiCnigQAn0SlekTT3wPvOwO2AgiU2s/0To+ka X-Received: by 2002:a05:6a00:17a2:b0:68f:d44c:22f8 with SMTP id s34-20020a056a0017a200b0068fd44c22f8mr3505820pfg.1.1694521062508; Tue, 12 Sep 2023 05:17:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694521062; cv=none; d=google.com; s=arc-20160816; b=SunAwKZrdNbCQHvf+qnZPJLEUvCF5SyF0EylGmTmVyP0leGmNItJNEL5kV1jQ95d1A N8Y90iNo7gxMXTsx8AVXrEnRwiT2+vEyoSxGJ0nIxJZxjzF13mohDb4+epaQnQu2zWv2 sFNPlaC2wn1higfxSuANg/HcobDt+GZfUNJTkRGhy5SINw7DfDxxIClRpg6L9snJD0p/ CbUWwHxDLTVoXHMqDGVTaE40GDIwzrVN1jSgzPhVs1xSxGyZe4zrvkoZGvBrRHJ2ALCD Lxa5+VBYF0AAcNSPc7nygP9dbeMlphmvM20bM45Ln1J0L3lfKdmMMKt0rhSqsCnwLZ1i kFaA== 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=dBHUwt2VZzLKlhZD7lus6ZMnFKpXbbWJtwBvilrxafU=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=T+sHTQDopXdhDkmo0zOJQtuOfU223AV5UEmW2S1Ye9j9rd0RlZBqIBehH32/0RKAwa 12HoGdTCU6iES39a9JxmdVcVtRtXyWnyvyP0S3jiIDJdRw7JVZyLtEHZwK1wdP0Cqxnw 12XAgDw/WIwdIl3qi/bobXh2962xmDYbPLyc+MPF4MkA5fF2dt/aQvs8IP8MpYv0ZYBZ xTtfUPXitQfKuHKaGYuUNuBmS2S32/pKR6XtytUNIzMKx5FRzY2i2iJH8ra0MBh5EhhF ejgF0GyfZKhY5RbqZtr4EIOrJgtptu8qB4yU4y2HOCHFR9OwaLQOw1CozJm4VEaPoGj9 2YSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=WNmU6iY9; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id az1-20020a056a02004100b005776a454f3csi3788644pgb.379.2023.09.12.05.17.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 05:17:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=WNmU6iY9; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id CC311818D53E; Tue, 12 Sep 2023 00:59:55 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232145AbjILH7G (ORCPT + 39 others); Tue, 12 Sep 2023 03:59:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232157AbjILH6k (ORCPT ); Tue, 12 Sep 2023 03:58:40 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B32C51BEA for ; Tue, 12 Sep 2023 00:58:10 -0700 (PDT) Message-ID: <20230912065501.777893171@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505489; 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=dBHUwt2VZzLKlhZD7lus6ZMnFKpXbbWJtwBvilrxafU=; b=WNmU6iY9mLJRMZGkGeQdBmj9PHK4NojOS348gK8kWx9n8wdhoIQtZv+pFsYG1i3+ow8lWJ i5Dmy30dLW1n/mUZasLMRQA8Pzl/HDnc14N+gw75DEqGyTaVK3ZQU/bhF9GNV9Lhl9JcYv s/TD/8kBRb3/AwVOcoKuBpDbrkIK4F6fHySkjTJiSx+314zh33lWZrGuS16Y7pONHPOvm5 RndoYo0gGdYKfoREkUS1gDSMkrXBzPfiogcW85PxfTvfyNbsYfac1qEFedxqXQY5qy8hQ1 CEESiNxNiQnnLFxKJrTdyrXu1uMvArjl+2i/6SbXsARUIJHGfoUeW3M5AtZ+9Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505489; 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=dBHUwt2VZzLKlhZD7lus6ZMnFKpXbbWJtwBvilrxafU=; b=fyo9/Q/8h/II6rdgCACFg1uN+807cKfPKMZbwfGmdh4g0z+kmveZDimC6jbu4fUdWuETXx WWkhie4MYJZXqRAg== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 16/30] x86/microcode: Get rid of the schedule work indirection References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:58:08 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Tue, 12 Sep 2023 00:59:55 -0700 (PDT) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776834117748859147 X-GMAIL-MSGID: 1776834117748859147 Scheduling work on all CPUs to collect the microcode information is just another extra step for no value. Let the CPU hotplug callback registration do it. Signed-off-by: Thomas Gleixner --- V2: New patch --- arch/x86/kernel/cpu/microcode/core.c | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) --- a/arch/x86/kernel/cpu/microcode/core.c +++ b/arch/x86/kernel/cpu/microcode/core.c @@ -531,8 +531,16 @@ static struct syscore_ops mc_syscore_ops static int mc_cpu_online(unsigned int cpu) { + struct ucode_cpu_info *uci = ucode_cpu_info + cpu; struct device *dev = get_cpu_device(cpu); + memset(uci, 0, sizeof(*uci)); + + microcode_ops->collect_cpu_info(cpu, &uci->cpu_sig); + cpu_data(cpu).microcode = uci->cpu_sig.rev; + if (!cpu) + boot_cpu_data.microcode = uci->cpu_sig.rev; + if (sysfs_create_group(&dev->kobj, &mc_attr_group)) pr_err("Failed to create group for CPU%d\n", cpu); return 0; @@ -553,20 +561,6 @@ static int mc_cpu_down_prep(unsigned int return 0; } -static void setup_online_cpu(struct work_struct *work) -{ - int cpu = smp_processor_id(); - struct ucode_cpu_info *uci = ucode_cpu_info + cpu; - - memset(uci, 0, sizeof(*uci)); - - microcode_ops->collect_cpu_info(cpu, &uci->cpu_sig); - cpu_data(cpu).microcode = uci->cpu_sig.rev; - if (!cpu) - boot_cpu_data.microcode = uci->cpu_sig.rev; - mc_cpu_online(cpu); -} - static struct attribute *cpu_root_microcode_attrs[] = { #ifdef CONFIG_MICROCODE_LATE_LOADING &dev_attr_reload.attr, @@ -612,12 +606,9 @@ static int __init microcode_init(void) } } - /* Do per-CPU setup */ - schedule_on_each_cpu(setup_online_cpu); - register_syscore_ops(&mc_syscore_ops); - cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "x86/microcode:online", - mc_cpu_online, mc_cpu_down_prep); + cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/microcode:online", + mc_cpu_online, mc_cpu_down_prep); pr_info("Microcode Update Driver: v%s.", DRIVER_VERSION); From patchwork Tue Sep 12 07:58:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138041 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp271205vqx; Tue, 12 Sep 2023 02:04:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEcubL5Q+0BjMJqRsOlAyFkWaQFXMphGEmuij8fqSA437AkY5efE2gKlIynWD1hZpx5bSLZ X-Received: by 2002:a05:6358:16c7:b0:141:51d:4c93 with SMTP id r7-20020a05635816c700b00141051d4c93mr3164777rwl.29.1694509462433; Tue, 12 Sep 2023 02:04:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694509462; cv=none; d=google.com; s=arc-20160816; b=qRDRVrooJse4+bMw9yrLmgzeEPV/WzsYs9VYQgdrbEJTZB91Idp45FXgtSkJZZ3Nre cf4Mc9iZLs4x5EuSEYeQfd/TG19GTlgK+MRlskotbZ3LVCJdAdJsT1i1lYquDmZfjfa8 tfMwkie7b1QBlxdCF47FiVRA4Fw4NE7AgghpdaRW1kbA6JinXShEOodP7BvhvctUEfT2 JR9uu7zChv4+pEjFi5/huY0zyVRhSFC+cXBd7+AvPcY/9KpnTQmeN90RsRpt0JL9TNvL El9J4fsxXLtuOiY27SXjtdZwjr11PxnzZ26Fb1VmHNaJgE2Qj6cIV61VOOTVrQZLt6ak CZvQ== 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=29AYhkI+1B+aP2ulSjBZYvwAxHR9z6zN13x7VQxgXac=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=Ooky713Qc1zlrHl449t/Pu6PUndekE2tgwLtfmaN+ioRhWkC5MuiZ4/kotFr1F6eHK MkVBihgfNWMlWmYXFEHV6Myda2aTr0jaSmM9vylfLqgVyfFBnKtOV/X7m5PfZxKAwWPT jGiKnff2zSHh3oRt4gGyVBELs59HohoLOTw9d4iatbbEip0AT2JJfa6onT2ho8IzJUvo hH1XcI26zQj7pmRBlJ1wGY57v880qs1HDRVNb5NHPK3vHlFcEAs2QfP0Nk+H+GisjTgy ciT1+QVCtz0yGUJqkh0QFu8lMh5GlgeyJaTyb07k/HQQi9oiG9angAxt+prL+7hjCO5X 3FFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=o5K9GLXM; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=KBTh1d6S; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id m187-20020a6326c4000000b005648d3f2031si7703297pgm.362.2023.09.12.02.04.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 02:04:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=o5K9GLXM; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=KBTh1d6S; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 793AB8271DF5; Tue, 12 Sep 2023 00:59:20 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232062AbjILH7N (ORCPT + 39 others); Tue, 12 Sep 2023 03:59:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49582 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232090AbjILH6l (ORCPT ); Tue, 12 Sep 2023 03:58:41 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E6DB2102 for ; Tue, 12 Sep 2023 00:58:12 -0700 (PDT) Message-ID: <20230912065501.838955709@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505491; 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=29AYhkI+1B+aP2ulSjBZYvwAxHR9z6zN13x7VQxgXac=; b=o5K9GLXM3XJpdsaGpiBbbxvmmBIEGDFBMHADfwXSBVH3+l+2a6D+OMu0ia1mVRFIBR+2/W uqhaCgGahmSxDrsgnpSSfKNZgh2/GoQ9wgUxow4iRFVmePrffytWctRvaSFsl+96+kX74g zlJgwaZvvgCQmI6EiyN6ZGRhVwjXgtKyuA0JdnL86v769V7k0rqWg+VX12BZ81XK90BKrx UDfvQWrLaM5X5TCNsxvuQju+uFM5ejimEM0tmkqd6UFbmMKHK4NoN//og2uzek166vUZS3 SmYm44zVjymVBgVX0RHw8UiGBGFkOnvzrc5lpTvDA2aOkRo55AUu6Zu3sgCdLg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505491; 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=29AYhkI+1B+aP2ulSjBZYvwAxHR9z6zN13x7VQxgXac=; b=KBTh1d6SrsaSukcbTTTJGUkoMfBfIkLyuLjz/8JnxI6kpyDX2Zvx2BOGrpl8ML2w5Q8AHK rbusKTV3I4h1YaDQ== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 17/30] x86/microcode: Clean up mc_cpu_down_prep() References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:58:10 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 12 Sep 2023 00:59:20 -0700 (PDT) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776821953648501676 X-GMAIL-MSGID: 1776821953648501676 This function has nothing to do with suspend. It's a hotplug callback. Remove the bogus comment. Drop the pointless debug printk. The hotplug core provides tracepoints which track the invocation of those callbacks. Signed-off-by: Thomas Gleixner --- V2: New patch --- arch/x86/kernel/cpu/microcode/core.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) --- a/arch/x86/kernel/cpu/microcode/core.c +++ b/arch/x86/kernel/cpu/microcode/core.c @@ -569,16 +569,10 @@ static int mc_cpu_online(unsigned int cp static int mc_cpu_down_prep(unsigned int cpu) { - struct device *dev; - - dev = get_cpu_device(cpu); + struct device *dev = get_cpu_device(cpu); microcode_fini_cpu(cpu); - - /* Suspend is in progress, only remove the interface */ sysfs_remove_group(&dev->kobj, &mc_attr_group); - pr_debug("%s: CPU%d\n", __func__, cpu); - return 0; } From patchwork Tue Sep 12 07:58:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138246 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp483587vqx; Tue, 12 Sep 2023 08:23:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE2HF/w0ggkU2BwZS3v+2T3g4k5U2gUrh3v3KQA830elLD/F2fB5uCxIFEBUJzaOAm54CkW X-Received: by 2002:a05:6a00:84a:b0:68c:a81:4368 with SMTP id q10-20020a056a00084a00b0068c0a814368mr125503pfk.6.1694532195472; Tue, 12 Sep 2023 08:23:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694532195; cv=none; d=google.com; s=arc-20160816; b=AZPY4LlN7pPoharSnD+BFRuOf3NO9dAul/NjmFTFMwnJ5hjVglWwi+Wh8q1VGM0q0C 2Pqm4/Y44bG6t8Lv/8qW9UqZyQctWFigzKClNFNy57on/S4bzA6XxRsRMhDBYS+vOOAZ e4z57kNjtKnxiE/TIitHrg9ifM5bp30ING3yvfbfvouDVI8pFmx90YcB+NaMbpaKwo6g ur8BMBtCgjzuTcuNzpwZphEK7AC6dyZEhs++icyp3AU9bIHuEDGDNRi+U02gLpFRF1Ih 1QfNAcbn7fgSMnLWCXPY7Rr+Q6S8CZFOXkrPdvL/E9T3eKDRlc9oHsgcx+DEpdIYWGyl bATA== 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=Q4ru7NAfdX5E2q0WOPskDN2at23E5jYbJEoHUeyYgF4=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=IdVpr4uiRWAmJyQKFPxGpIoSZdUV5ZMtkSxrhJCFvOhd+i6ViXBBho0o7gxWqMsEhr quaYpbBoUmPNDgf1VwDfWTMXNpfHgFdgU2irmNnS1z/P83fDT0lYfhxAmEtvalWxGoRy YvSj+Q0y5q+QmjfTuaryPah44JHxs3yCbW0oPYNFs8QtVMSbRAClxYYDXKQBaxsJJhBp s2jOTKHAbpnBtdw0c8AL/JN7QeeVzyK1qKcDnrcnz3PqmtP0ymBOKpH5Lq+0JlFyx691 0Sh+7tIozLArGFTABNQZbh7BM3sfiHF+i99MxtxUiGLaARqj07f14jEXUihWra3QYurJ KCCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=TcYrBqZ+; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id bv185-20020a632ec2000000b0056357813fc2si7828350pgb.183.2023.09.12.08.23.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 08:23:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=TcYrBqZ+; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 57670819D9E6; Tue, 12 Sep 2023 01:03:34 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232118AbjILIAP (ORCPT + 39 others); Tue, 12 Sep 2023 04:00:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54148 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232101AbjILH6s (ORCPT ); Tue, 12 Sep 2023 03:58:48 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 86CB82122 for ; Tue, 12 Sep 2023 00:58:14 -0700 (PDT) Message-ID: <20230912065501.899886649@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505492; 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=Q4ru7NAfdX5E2q0WOPskDN2at23E5jYbJEoHUeyYgF4=; b=TcYrBqZ+SVaKkchzBHbrsg1CWP5Totia+mjt1S62DOrsjqpzr2myhzDmfFIJgcXS687mbW pFOMyhPYw8jpSde+jptSKfoqopy4OwRcIptOPXRzK3Oy5VLwwXhWEtJ9fCI3AlPCRon+cv 0SUb4ErB0vBRTyqpalOAIPEe06ER+OkXDhub2TxCTRAuymf8r+Az97vp6QrJMq1+shcrMV GxRR72VZQWjF3Otu8SJb/S6twc+kG986u3b6ZMexDk7rlVcd0+Po/ZpZhTrZDBkCIIMEOc BNBe5aeMhlLyefImUH00Ui4UDUfgY3rvbqsL5x0SMOu5F40pjGAxRhODtXCbxQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505492; 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=Q4ru7NAfdX5E2q0WOPskDN2at23E5jYbJEoHUeyYgF4=; b=UWxqK4b75j2w6T5PE8NCAeDHJEJhASRyEfzwys0UndjdljmpMh3VCP9JYa9PpsFxOGYaUG eUkfOtYtJnKPFCDA== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 18/30] x86/microcode: Handle "nosmt" correctly References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:58:12 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Tue, 12 Sep 2023 01:03:34 -0700 (PDT) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776845791582482014 X-GMAIL-MSGID: 1776845791582482014 From: Thomas Gleixner On CPUs where microcode loading is not NMI safe the SMT sibling which is parked in one of the play_dead() variants, these parked CPUs still react on NMIs. So if a NMI hits while the primary thread updates the microcode the resulting behaviour is undefined. The default play_dead() implementation on modern CPUs is using MWAIT, which is not guaranteed to be safe against an microcode update which affects MWAIT. Take the cpus_booted_once_mask into account to detect this case and refuse to load late if the vendor specific driver does not advertise that late loading is NMI safe. AMD stated that this is safe, so mark the AMD driver accordingly. This requirement will be partially lifted in later changes. Signed-off-by: Thomas Gleixner --- arch/x86/Kconfig | 2 - arch/x86/kernel/cpu/microcode/amd.c | 9 +++-- arch/x86/kernel/cpu/microcode/core.c | 51 +++++++++++++++++++------------ arch/x86/kernel/cpu/microcode/internal.h | 13 +++---- 4 files changed, 44 insertions(+), 31 deletions(-) --- --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -1314,7 +1314,7 @@ config MICROCODE config MICROCODE_LATE_LOADING bool "Late microcode loading (DANGEROUS)" default n - depends on MICROCODE + depends on MICROCODE && SMP help Loading microcode late, when the system is up and executing instructions is a tricky business and should be avoided if possible. Just the sequence --- a/arch/x86/kernel/cpu/microcode/amd.c +++ b/arch/x86/kernel/cpu/microcode/amd.c @@ -948,10 +948,11 @@ static void microcode_fini_cpu_amd(int c } static struct microcode_ops microcode_amd_ops = { - .request_microcode_fw = request_microcode_amd, - .collect_cpu_info = collect_cpu_info_amd, - .apply_microcode = apply_microcode_amd, - .microcode_fini_cpu = microcode_fini_cpu_amd, + .request_microcode_fw = request_microcode_amd, + .collect_cpu_info = collect_cpu_info_amd, + .apply_microcode = apply_microcode_amd, + .microcode_fini_cpu = microcode_fini_cpu_amd, + .nmi_safe = true, }; struct microcode_ops * __init init_amd_microcode(void) --- a/arch/x86/kernel/cpu/microcode/core.c +++ b/arch/x86/kernel/cpu/microcode/core.c @@ -326,23 +326,6 @@ static struct platform_device *microcode */ #define SPINUNIT 100 /* 100 nsec */ -static int check_online_cpus(void) -{ - unsigned int cpu; - - /* - * Make sure all CPUs are online. It's fine for SMT to be disabled if - * all the primary threads are still online. - */ - for_each_present_cpu(cpu) { - if (topology_is_primary_thread(cpu) && !cpu_online(cpu)) { - pr_err("Not all CPUs online, aborting microcode update.\n"); - return -EINVAL; - } - } - - return 0; -} static atomic_t late_cpus_in; static atomic_t late_cpus_out; @@ -459,6 +442,35 @@ static int microcode_reload_late(void) return ret; } +/* + * Ensure that all required CPUs which are present and have been booted + * once are online. + * + * To pass this check, all primary threads must be online. + * + * If the microcode load is not safe against NMI then all SMT threads + * must be online as well because they still react on NMI when they are + * soft-offlined and parked in one of the play_dead() variants. So if a + * NMI hits while the primary thread updates the microcode the resulting + * behaviour is undefined. The default play_dead() implementation on + * modern CPUs is using MWAIT, which is also not guaranteed to be safe + * against a microcode update which affects MWAIT. + */ +static bool ensure_cpus_are_online(void) +{ + unsigned int cpu; + + for_each_cpu_and(cpu, cpu_present_mask, &cpus_booted_once_mask) { + if (!cpu_online(cpu)) { + if (topology_is_primary_thread(cpu) || !microcode_ops->nmi_safe) { + pr_err("CPU %u not online\n", cpu); + return false; + } + } + } + return true; +} + static ssize_t reload_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) @@ -474,9 +486,10 @@ static ssize_t reload_store(struct devic cpus_read_lock(); - ret = check_online_cpus(); - if (ret) + if (!ensure_cpus_are_online()) { + ret = -EBUSY; goto put; + } tmp_ret = microcode_ops->request_microcode_fw(bsp, µcode_pdev->dev); if (tmp_ret != UCODE_NEW) --- a/arch/x86/kernel/cpu/microcode/internal.h +++ b/arch/x86/kernel/cpu/microcode/internal.h @@ -20,18 +20,17 @@ enum ucode_state { struct microcode_ops { enum ucode_state (*request_microcode_fw)(int cpu, struct device *dev); - void (*microcode_fini_cpu)(int cpu); /* - * The generic 'microcode_core' part guarantees that - * the callbacks below run on a target cpu when they - * are being called. + * The generic 'microcode_core' part guarantees that the callbacks + * below run on a target cpu when they are being called. * See also the "Synchronization" section in microcode_core.c. */ - enum ucode_state (*apply_microcode)(int cpu); - int (*collect_cpu_info)(int cpu, struct cpu_signature *csig); - void (*finalize_late_load)(int result); + 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; }; extern struct ucode_cpu_info ucode_cpu_info[]; From patchwork Tue Sep 12 07:58:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138012 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp246072vqx; Tue, 12 Sep 2023 01:01:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFEmgoifb2Wc39isi3qT9eDXMhmVgGblG4NDHSLIwMFYHDV/ecWsUxYWbJes0eWQUlFktrH X-Received: by 2002:a05:6a20:a120:b0:14c:d494:77d1 with SMTP id q32-20020a056a20a12000b0014cd49477d1mr12750494pzk.10.1694505688288; Tue, 12 Sep 2023 01:01:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694505688; cv=none; d=google.com; s=arc-20160816; b=e/n9/suAaZhlezm5PmbRnnPq7Lg5C2y+zNyu0zmunrntn2Y4HCq7DXWX1jYKTupj8u Gr2eOfWcSQodZSZhSGAg8BPWF7KEK3zLDo7BysFCYwMUxf45mAiZbsvhR5PnqWhwd7UM TYAtW/+rWJXcQl5u8+r0Wp8Wt3UPJ6FqbA6sRtcvtPZRWGr1jLmwEciborJaLBaWapai w9DeiJB4X/fzS+X0ZENChnouazwe8Db8sivHy4yhUyJ471X2KjxrynTZ6HBty+PmHUjM CQP914xqUDRb4H8CM2R4db5zP1s2higlKJ8EeNDjhEbqc+6Y6+Z9baRxwDoehhFpllKK rRqw== 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=1lBdPitYyTH3fsPE7V/4ABSpwPZct6295smjQAkgNa4=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=TcnGBGMXOWoCHK/RX5kVYHxGdFhYqtEZCqoVwG2lTlvAUn05wlcmHnbdiQKDCjD+Sh +8seCFIe/knRtRmFNYtNTX5BJNcQkSnhHqPWZGfE69ZAPOr7N0G6wncEE96OFpQS9/NY IO9ywNLktPfUdu2YK3P1+6szlz6iyILyrtpSKWN4DxAOX6Cd8z7YgV4Qi6o+6aXEouSA qGAGDK1v1k7g6Iape1cZqkW+i6lCHkecvkmCclSTNsE7gpUPXQU9uG4W4RJvwiiaMDne eSBLwDRgp/26YZto5nT/4e8MRTDdLbIfPK3UNg7ASKu4dvuNpOiYIEDQ4Q/puscXs14I iKUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=ftqcaC+b; dkim=neutral (no key) header.i=@linutronix.de header.b=5TFRvLxv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id n11-20020a17090a670b00b00271bc76ca5fsi7551362pjj.39.2023.09.12.01.01.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 01:01:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=ftqcaC+b; dkim=neutral (no key) header.i=@linutronix.de header.b=5TFRvLxv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 70CB282C92D2; Tue, 12 Sep 2023 00:59:36 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.8 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231800AbjILH70 (ORCPT + 39 others); Tue, 12 Sep 2023 03:59:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54176 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231984AbjILH6u (ORCPT ); Tue, 12 Sep 2023 03:58:50 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CFE0F10D7 for ; Tue, 12 Sep 2023 00:58:15 -0700 (PDT) Message-ID: <20230912065501.961705755@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505494; 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=1lBdPitYyTH3fsPE7V/4ABSpwPZct6295smjQAkgNa4=; b=ftqcaC+bXsFBXJg518lIMU2YZ+XCbYlWfLkJDQ+ggwR43AVjKULRrKmYqwJVuRHIfb9IM8 mRAN/rfy8F+TTDo+w80gLMR0HUkB2fOzPpk7idfojOGhkANnQSJNq6G8X+7/8WN7fd/ubu 2WBVMLsVVy8Dqgnwus1NkyiVBZTdTwfxUxkVCSjBiOa+ygV4uzKKS4ckvXOMygvDTer8yD XiCG7wG8BjFEKXY1m0Pzq2+Ox47uWhp7NEFECWlTlvqgsHnlwCdjkQGjhFGK1GIEKTQeUG /P/7cmGlDzlyObunNmqVYkjH4X6O3OcRgTakYCtW935HRbXuv5T/GQ8Tm6Y27w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505494; 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=1lBdPitYyTH3fsPE7V/4ABSpwPZct6295smjQAkgNa4=; b=5TFRvLxvtYwar2kxPLc4wxw6Uxwqbs5L/fMbICJx4ZHjBGNBNX0dKeScZkx4F7bKzPRf39 ngy7S5oODr13RlDA== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 19/30] x86/microcode: Clarify the late load logic References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:58:13 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 12 Sep 2023 00:59:36 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776817996813103310 X-GMAIL-MSGID: 1776817996813103310 From: Thomas Gleixner reload_store() is way too complicated. Split the inner workings out and make the following enhancements: - Taint the kernel only when the microcode was actually updated. If. e.g. the rendevouz fails, then nothing happened and there is no reason for tainting. - Return useful error codes Signed-off-by: Thomas Gleixner Reviewed-by: Nikolay Borisov --- arch/x86/kernel/cpu/microcode/core.c | 39 +++++++++++++++-------------------- 1 file changed, 17 insertions(+), 22 deletions(-) --- --- a/arch/x86/kernel/cpu/microcode/core.c +++ b/arch/x86/kernel/cpu/microcode/core.c @@ -434,11 +434,11 @@ static int microcode_reload_late(void) pr_info("Reload succeeded, microcode revision: 0x%x -> 0x%x\n", old, boot_cpu_data.microcode); microcode_check(&prev_info); + add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK); } else { pr_info("Reload failed, current microcode revision: 0x%x\n", boot_cpu_data.microcode); } - return ret; } @@ -471,40 +471,35 @@ static bool ensure_cpus_are_online(void) return true; } +static int ucode_load_late_locked(void) +{ + int ret; + + if (!ensure_cpus_are_online()) + return -EBUSY; + + ret = microcode_ops->request_microcode_fw(0, µcode_pdev->dev); + if (ret != UCODE_NEW) + return ret == UCODE_NFOUND ? -ENOENT : -EBADFD; + return microcode_reload_late(); +} + static ssize_t reload_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) { - enum ucode_state tmp_ret = UCODE_OK; - int bsp = boot_cpu_data.cpu_index; unsigned long val; - ssize_t ret = 0; + ssize_t ret; ret = kstrtoul(buf, 0, &val); if (ret || val != 1) return -EINVAL; cpus_read_lock(); - - if (!ensure_cpus_are_online()) { - ret = -EBUSY; - goto put; - } - - tmp_ret = microcode_ops->request_microcode_fw(bsp, µcode_pdev->dev); - if (tmp_ret != UCODE_NEW) - goto put; - - ret = microcode_reload_late(); -put: + ret = ucode_load_late_locked(); cpus_read_unlock(); - if (ret == 0) - ret = size; - - add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK); - - return ret; + return ret ? : size; } static DEVICE_ATTR_WO(reload); From patchwork Tue Sep 12 07:58:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138651 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp868963vqx; Tue, 12 Sep 2023 22:38:29 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGwwqePkbpgWuqO6dH4Gp4wdaZe5B0RHLlsQfp8tMKOct1fmwvg0q7eCvYuBcGC3HBzPeGk X-Received: by 2002:a05:6a00:168a:b0:68f:b5cb:cecd with SMTP id k10-20020a056a00168a00b0068fb5cbcecdmr2355190pfc.16.1694583509049; Tue, 12 Sep 2023 22:38:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694583509; cv=none; d=google.com; s=arc-20160816; b=ifUe8uzxkYOdqvtQPfvix/oL/fJy5lF17ukKQmrQicMDWo7QP3oTKrdOeQaDXZg/w0 gId3vAgZt64OfvKxsAxJ4S76Nv0XD0oJ/XpTBvYFKBUZXgLrAh6nGEtXZa5HnfUT7S8S nvfgRm/17Ye/1dzgbbm6wGb2c9SE7OiyT8xq1grsvEBrJCDQjs4ogHgbcQ915ynvfUI4 6jOO75OpK6Ab0x6pttTVZPJw3/2hdQ3cb4KwVi+g/DrMk17mOvywrK14JD7l88eYNw6b 4f/9nii6miyyMDkZKPEOtSl8j28z+hRdIh3XqBuzW5AGUOjsLpnTC41fXfT7hLTVvXNj 77CQ== 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=/mf0toaJNTepRqxmdRBH+YrhPsSto1ib/DfMaTt6vf0=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=wMj096gTpgmZmEQjEVs/cY+fVYu7/r/vbn7cXepIGQUKIr+qn0oz4lQ6rtChhwv7wH rEkvWdFKACtnVlnkTCV4J2oRevST1vXo8zOlvwpGuuOk+/uF757w2aKKEDiwqLTMpLv8 rhnKRbDVrp4wcE3XK/R0ZmMNvowyMIs3BIBqQhff2FgMdIooy2McBODFtfIEXc7oO0eE SxpRkKiX1isGzOM2Vy1KGXrciPwvMOFBZAjxFTB46GGLwiU5vXe9MO1Suh+zn+JsS+Hw OVTFrIabft/I5a3pS4yd+Ym1Aw/H+ZNGFrgy4akvn+nd16G/OupPAU/Mxm0DwjzsF3Ki FOQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=g45VjkWr; dkim=neutral (no key) header.i=@linutronix.de header.b="/tqFuXrI"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id u21-20020a056a00125500b0068fcc84dda7si4289650pfi.327.2023.09.12.22.38.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 22:38:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=g45VjkWr; dkim=neutral (no key) header.i=@linutronix.de header.b="/tqFuXrI"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id B0E9787A32F9; Tue, 12 Sep 2023 01:00:04 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232164AbjILIAB (ORCPT + 39 others); Tue, 12 Sep 2023 04:00:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232124AbjILH6x (ORCPT ); Tue, 12 Sep 2023 03:58:53 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20B7710E2 for ; Tue, 12 Sep 2023 00:58:17 -0700 (PDT) Message-ID: <20230912065502.022650614@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505495; 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=/mf0toaJNTepRqxmdRBH+YrhPsSto1ib/DfMaTt6vf0=; b=g45VjkWr4SZn3yjEAj2eWLFNcRhU+UHPjUtzawCFgwj/Glp/er+bg3ogM+X258L6hsbPhd 8uOzYpmvZJRf5MXhO8pMKfhGcFtfLds6u8ni7NeMIGI6giAtelJvB6oW5/VPgpaszxG82j h+O684VQFMepqlCLQxUPWe2WB2U6G0u90vB9vvVITwx7anvS+JQIMtMQxB82rCbHXH5tPy M432JypKh6p+rafj1Gyz6gRn2mwi8/MZszvSku1nZuIfMXpYgWlbxbxewAJZFdE46sdJ2+ RJid0AjY9zasC7LhtRNAFFMuSVdWdAOumkNDXCA/mXDZoNYwjjF9qmIn2xFgtg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505495; 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=/mf0toaJNTepRqxmdRBH+YrhPsSto1ib/DfMaTt6vf0=; b=/tqFuXrIH08IG44vB+PEpoTe/F4Eob70a3awXZKlp3feakto4GsvX4axyQ/YfZtYZlDYvg OyvKE/06FGM4XuDQ== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 20/30] x86/microcode: Sanitize __wait_for_cpus() References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:58:15 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 12 Sep 2023 01:00:04 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776899597745843011 X-GMAIL-MSGID: 1776899597745843011 From: Thomas Gleixner The code is too complicated for no reason: - The return value is pointless as this is a strict boolean. - It's way simpler to count down from num_online_cpus() and check for zero. - The timeout argument is pointless as this is always one second. - Touching the NMI watchdog every 100ns does not make any sense, neither does checking every 100ns. This is really not a hotpath operation. Preload the atomic counter with the number of online CPUs and simplify the whole timeout logic. Delay for one microsecond and touch the NMI watchdog once per millisecond. Signed-off-by: Thomas Gleixner --- arch/x86/kernel/cpu/microcode/core.c | 41 ++++++++++++++--------------------- 1 file changed, 17 insertions(+), 24 deletions(-) --- --- a/arch/x86/kernel/cpu/microcode/core.c +++ b/arch/x86/kernel/cpu/microcode/core.c @@ -324,31 +324,24 @@ static struct platform_device *microcode * requirement can be relaxed in the future. Right now, this is conservative * and good. */ -#define SPINUNIT 100 /* 100 nsec */ +static atomic_t late_cpus_in, late_cpus_out; - -static atomic_t late_cpus_in; -static atomic_t late_cpus_out; - -static int __wait_for_cpus(atomic_t *t, long long timeout) +static bool wait_for_cpus(atomic_t *cnt) { - int all_cpus = num_online_cpus(); - - atomic_inc(t); - - while (atomic_read(t) < all_cpus) { - if (timeout < SPINUNIT) { - pr_err("Timeout while waiting for CPUs rendezvous, remaining: %d\n", - all_cpus - atomic_read(t)); - return 1; - } + unsigned int timeout; - ndelay(SPINUNIT); - timeout -= SPINUNIT; + WARN_ON_ONCE(atomic_dec_return(cnt) < 0); - touch_nmi_watchdog(); + for (timeout = 0; timeout < USEC_PER_SEC; timeout++) { + if (!atomic_read(cnt)) + return true; + udelay(1); + if (!(timeout % 1000)) + touch_nmi_watchdog(); } - return 0; + /* Prevent the late comers to make progress and let them time out */ + atomic_inc(cnt); + return false; } /* @@ -366,7 +359,7 @@ static int __reload_late(void *info) * Wait for all CPUs to arrive. A load will not be attempted unless all * CPUs show up. * */ - if (__wait_for_cpus(&late_cpus_in, NSEC_PER_SEC)) + if (!wait_for_cpus(&late_cpus_in)) return -1; /* @@ -389,7 +382,7 @@ static int __reload_late(void *info) } wait_for_siblings: - if (__wait_for_cpus(&late_cpus_out, NSEC_PER_SEC)) + if (!wait_for_cpus(&late_cpus_out)) panic("Timeout during microcode update!\n"); /* @@ -416,8 +409,8 @@ static int microcode_reload_late(void) pr_err("Attempting late microcode loading - it is dangerous and taints the kernel.\n"); pr_err("You should switch to early loading, if possible.\n"); - atomic_set(&late_cpus_in, 0); - atomic_set(&late_cpus_out, 0); + atomic_set(&late_cpus_in, num_online_cpus()); + atomic_set(&late_cpus_out, num_online_cpus()); /* * Take a snapshot before the microcode update in order to compare and From patchwork Tue Sep 12 07:58:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138063 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp284802vqx; Tue, 12 Sep 2023 02:37:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHc8yMWzduFh02as3va1bjrsQgQC0nYirSF7iH0AUioNKbt3bXpVJtZRvHklul37tg33Bjl X-Received: by 2002:a05:6358:2789:b0:134:c859:d32a with SMTP id l9-20020a056358278900b00134c859d32amr11305517rwb.25.1694511432385; Tue, 12 Sep 2023 02:37:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694511432; cv=none; d=google.com; s=arc-20160816; b=apdRqfknPqrxR3t81el3ruLZJjeD7sokzeReb6kxJZvFhil9whqWHjKz7NnPRGONHH C7Sn5CCyX0dsNkMoq/kAn1n2yHpDRVO0QZPlE4Z8H86EAQvGawAyn4UGEqMy0KQcqkKz YT0g+xE9u0Mu1yr6mLN+keJM0LykOJ8zGPUlQIkT9lpX6lFxISDXWzmmrw5uTanIKMmQ TFKstgOOME4ZHCXZvr4TfJ76LDHgDVdhJf9XPICqlj4fwiSaqX8FPgfJgOxoRO+v1f4d rnvQzkd+vRDmYMQ4acpQoMRqgh1p2Mc8Cx3NZvrSV2QIn8JnP2UloVfnyMrAHzNQfbiG yOqA== 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=2kqLUw1a4gV9FrIDWZ/O+PSOOuBOB0W9vRdpe8yDcXg=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=IylhhTZxjo36nbWdeWZDmIAASX7CLWCQR6R0ySvAMm2Xx8rdTk06UGV/kqbew01WrR 1ganpTRmG4H1ZHgERsu3CzP7Hs8HxVMa/XK7QJ+bqGjJF1EcZUO53Kn+siJ5HmppYaD4 fU++cmxhjd6zmDsefZh6BjIhL+JISGf/AiB4BALWugS46ydseUj2swYwtGiV23D7OZsu 6MWGLk2pK9GDVtAa4Jw8fjnwLjkWinpJz3HzvzaaIQX/MDTeb5zSSOG/DkBx0z+1AZ04 F5ysW0cB/idg8uqV9+POufOgwDYZQtDW3T5Vyqd+P+HXA43zgRMXm9g5nNO8wkFrt/kY UEnA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=QqaPuXD8; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=PqPSfxGD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id v3-20020a655c43000000b00565e42df278si7385753pgr.759.2023.09.12.02.37.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 02:37:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=QqaPuXD8; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=PqPSfxGD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id F098D81DE681; Tue, 12 Sep 2023 01:01:36 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.8 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232032AbjILIA2 (ORCPT + 39 others); Tue, 12 Sep 2023 04:00:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232211AbjILH6y (ORCPT ); Tue, 12 Sep 2023 03:58:54 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C3E3610F3 for ; Tue, 12 Sep 2023 00:58:18 -0700 (PDT) Message-ID: <20230912065502.082789879@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505497; 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=2kqLUw1a4gV9FrIDWZ/O+PSOOuBOB0W9vRdpe8yDcXg=; b=QqaPuXD8otO8MOEIXvyvoMFbgqrpc8ZVKklazBuJHJC9cWBbTtTw1x3eGLytyNxgdmimyp WBzksNyIlnKhE12fHgbaM8Z7xBsWve9SDi2jxGaJmk38DpyRNkAVnQw4QV02fS183isYgS s74li/BXboVIoqEr9LtkbE0rLqY8+Aqec3jyTu+eaJ6gs65O5US7LuWGV98RfmTz/K6N+l 6gorRnyMPBhtqD6COqK2L6x8iHs14w28TNmytckJckq9A5p0idz43iIZweDXN8DnAGJbos CA4rvHzMHsJV8U3RfJAGjOpyk5ofnU5eQDYoMDuHlqRZqCWiyRWr5tbHAQt9nQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505497; 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=2kqLUw1a4gV9FrIDWZ/O+PSOOuBOB0W9vRdpe8yDcXg=; b=PqPSfxGDhKa1TXBJ4lTbeEw9jb3ASVQZbMMAeaWiCeX7/ccIi0d6SHszRnlHLIpKrgPJVG fTrFWjT0+pB6r5BQ== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 21/30] x86/microcode: Add per CPU result state References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:58:16 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 12 Sep 2023 01:01:37 -0700 (PDT) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776824019877386150 X-GMAIL-MSGID: 1776824019877386150 From: Thomas Gleixner The microcode rendevouz is purely acting on global state, which does not allow to analyze fails in a coherent way. Introduce per CPU state where the results are written into, which allows to analyze the return codes of the individual CPUs. Initialize the state when walking the cpu_present_mask in the online check to avoid another for_each_cpu() loop. Enhance the result print out with that. The structure is intentionally named ucode_ctrl as it will gain control fields in subsequent changes. Signed-off-by: Thomas Gleixner --- arch/x86/kernel/cpu/microcode/core.c | 108 ++++++++++++++++++------------- arch/x86/kernel/cpu/microcode/internal.h | 1 2 files changed, 65 insertions(+), 44 deletions(-) --- --- a/arch/x86/kernel/cpu/microcode/core.c +++ b/arch/x86/kernel/cpu/microcode/core.c @@ -324,6 +324,11 @@ static struct platform_device *microcode * requirement can be relaxed in the future. Right now, this is conservative * and good. */ +struct ucode_ctrl { + enum ucode_state result; +}; + +static DEFINE_PER_CPU(struct ucode_ctrl, ucode_ctrl); static atomic_t late_cpus_in, late_cpus_out; static bool wait_for_cpus(atomic_t *cnt) @@ -344,23 +349,19 @@ static bool wait_for_cpus(atomic_t *cnt) return false; } -/* - * Returns: - * < 0 - on error - * 0 - success (no update done or microcode was updated) - */ -static int __reload_late(void *info) +static int ucode_load_cpus_stopped(void *unused) { int cpu = smp_processor_id(); - enum ucode_state err; - int ret = 0; + enum ucode_state ret; /* * Wait for all CPUs to arrive. A load will not be attempted unless all * CPUs show up. * */ - if (!wait_for_cpus(&late_cpus_in)) - return -1; + if (!wait_for_cpus(&late_cpus_in)) { + this_cpu_write(ucode_ctrl.result, UCODE_TIMEOUT); + return 0; + } /* * On an SMT system, it suffices to load the microcode on one sibling of @@ -369,17 +370,11 @@ static int __reload_late(void *info) * loading attempts happen on multiple threads of an SMT core. See * below. */ - if (cpumask_first(topology_sibling_cpumask(cpu)) == cpu) - err = microcode_ops->apply_microcode(cpu); - else + if (cpumask_first(topology_sibling_cpumask(cpu)) != cpu) goto wait_for_siblings; - if (err >= UCODE_NFOUND) { - if (err == UCODE_ERROR) { - pr_warn("Error reloading microcode on CPU %d\n", cpu); - ret = -1; - } - } + ret = microcode_ops->apply_microcode(cpu); + this_cpu_write(ucode_ctrl.result, ret); wait_for_siblings: if (!wait_for_cpus(&late_cpus_out)) @@ -391,19 +386,18 @@ static int __reload_late(void *info) * per-cpu cpuinfo can be updated with right microcode * revision. */ - if (cpumask_first(topology_sibling_cpumask(cpu)) != cpu) - err = microcode_ops->apply_microcode(cpu); + if (cpumask_first(topology_sibling_cpumask(cpu)) == cpu) + return 0; - return ret; + ret = microcode_ops->apply_microcode(cpu); + this_cpu_write(ucode_ctrl.result, ret); + return 0; } -/* - * Reload microcode late on all CPUs. Wait for a sec until they - * all gather together. - */ -static int microcode_reload_late(void) +static int ucode_load_late_stop_cpus(void) { - int old = boot_cpu_data.microcode, ret; + unsigned int cpu, updated = 0, failed = 0, timedout = 0, siblings = 0; + int old_rev = boot_cpu_data.microcode; struct cpuinfo_x86 prev_info; pr_err("Attempting late microcode loading - it is dangerous and taints the kernel.\n"); @@ -418,26 +412,47 @@ static int microcode_reload_late(void) */ store_cpu_caps(&prev_info); - ret = stop_machine_cpuslocked(__reload_late, NULL, cpu_online_mask); + stop_machine_cpuslocked(ucode_load_cpus_stopped, NULL, cpu_online_mask); + + /* Analyze the results */ + for_each_cpu_and(cpu, cpu_present_mask, &cpus_booted_once_mask) { + switch (per_cpu(ucode_ctrl.result, cpu)) { + case UCODE_UPDATED: updated++; break; + case UCODE_TIMEOUT: timedout++; break; + case UCODE_OK: siblings++; break; + default: failed++; break; + } + } if (microcode_ops->finalize_late_load) - microcode_ops->finalize_late_load(ret); + microcode_ops->finalize_late_load(!updated); - if (!ret) { - pr_info("Reload succeeded, microcode revision: 0x%x -> 0x%x\n", - old, boot_cpu_data.microcode); - microcode_check(&prev_info); - add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK); - } else { - pr_info("Reload failed, current microcode revision: 0x%x\n", - boot_cpu_data.microcode); + if (!updated) { + /* Nothing changed. */ + if (!failed && !timedout) + return 0; + pr_err("Microcode update failed: %u CPUs failed %u CPUs timed out\n", + failed, timedout); + return -EIO; } - return ret; + + add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK); + pr_info("Microcode load: updated on %u primary CPUs with %u siblings\n", updated, siblings); + if (failed || timedout) { + pr_err("Microcode load incomplete. %u CPUs timed out or failed\n", + num_online_cpus() - (updated + siblings)); + } + pr_info("Microcode revision: 0x%x -> 0x%x\n", old_rev, boot_cpu_data.microcode); + microcode_check(&prev_info); + + return updated + siblings == num_online_cpus() ? 0 : -EIO; } /* - * Ensure that all required CPUs which are present and have been booted - * once are online. + * This function does two things: + * + * 1) Ensure that all required CPUs which are present and have been booted + * once are online. * * To pass this check, all primary threads must be online. * @@ -448,9 +463,12 @@ static int microcode_reload_late(void) * behaviour is undefined. The default play_dead() implementation on * modern CPUs is using MWAIT, which is also not guaranteed to be safe * against a microcode update which affects MWAIT. + * + * 2) Initialize the per CPU control structure */ -static bool ensure_cpus_are_online(void) +static bool ucode_setup_cpus(void) { + struct ucode_ctrl ctrl = { .result = -1, }; unsigned int cpu; for_each_cpu_and(cpu, cpu_present_mask, &cpus_booted_once_mask) { @@ -460,6 +478,8 @@ static bool ensure_cpus_are_online(void) return false; } } + /* Initialize the per CPU state */ + per_cpu(ucode_ctrl, cpu) = ctrl; } return true; } @@ -468,13 +488,13 @@ static int ucode_load_late_locked(void) { int ret; - if (!ensure_cpus_are_online()) + if (!ucode_setup_cpus()) return -EBUSY; ret = microcode_ops->request_microcode_fw(0, µcode_pdev->dev); if (ret != UCODE_NEW) return ret == UCODE_NFOUND ? -ENOENT : -EBADFD; - return microcode_reload_late(); + return ucode_load_late_stop_cpus(); } static ssize_t reload_store(struct device *dev, --- a/arch/x86/kernel/cpu/microcode/internal.h +++ b/arch/x86/kernel/cpu/microcode/internal.h @@ -16,6 +16,7 @@ enum ucode_state { UCODE_UPDATED, UCODE_NFOUND, UCODE_ERROR, + UCODE_TIMEOUT, }; struct microcode_ops { From patchwork Tue Sep 12 07:58:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 139874 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp660794vqi; Thu, 14 Sep 2023 15:24:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH1kvDnJhKGbe6uSu0zkhUUJoFlC5a1B78T0eqf7Rm10YI/HhCAvJE/0lnjDh35qCROvhJL X-Received: by 2002:a17:903:1110:b0:1c3:bc2a:f6b7 with SMTP id n16-20020a170903111000b001c3bc2af6b7mr7683258plh.52.1694730298976; Thu, 14 Sep 2023 15:24:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694730298; cv=none; d=google.com; s=arc-20160816; b=oaOttVnAjo+xnU14NL45Aq/9fAMDxIpWqhrqpwGls1dhqx3w3UCFLMod/nUJM0nSqy JIbBcNr+48Huyw5XpvwHpF6Ru1OgV0PHSXg0D5Tduwwejkw7s7O9Utz4B2eLI8isGfC6 GJIwKI/Rhk62i402Zxztq3aR29IWRoc3UqNTbnZSBv8zIEbkx+P2iEbQFaqPy+KjZwif eyUS9b3wOieQ4I+nr3lyKm9k3v+NqO5E3ro3gf+GlF0VSytDpCFFYKVc6VZUaa1yJ1sm xTRznWzdoObN/YnoLeetIz4Xzcf1zW9lOB1+Q69io5xEuChGJpm5pCf5B+PfOmdHPrlp g/UQ== 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=gvgWHgk38J3qGADoq/3qmkZUlBXc3nLuAkYAzeigZSY=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=k5XEl/qTtIIPwJe9gsZoXoGo8wmLHl1nwTQY4mClJ+B3NAQdWtXj0iUjf1eFg46W+g IXzC6Y0lWcT/5g6XCtaalzHsNi9FnPvljOVIEs+UkvWXGJAKr0YE+Cjqtqd3mUALCpVO yKMSp9q0Ami1yk0XJEik5RHWZucM+ZP6WFh+9fIM9pFhlO+qmmcF+VxbUyeTcMIWWczg Zu2Zc/ZKf7sCuzon4gKtb8iWtGsdQfeOYk/nv42RmKTWDCvRET016F9saO7e6TB1CydV pv1JjyJ4ixKhd/17O2P2Kv9etF4mM2yKNdz+nbnj8lMA5j5zOKi18D6Vs+1sSx8qhEog prZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=SWeVI0WK; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=4O+bGAnH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id o12-20020a17090323cc00b001aaf1734d1dsi2181926plh.241.2023.09.14.15.24.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 15:24:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=SWeVI0WK; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=4O+bGAnH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 9AEBE851B9E6; Tue, 12 Sep 2023 01:00:40 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232236AbjILIAb (ORCPT + 39 others); Tue, 12 Sep 2023 04:00:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42608 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232075AbjILH7I (ORCPT ); Tue, 12 Sep 2023 03:59:08 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B7655269E for ; Tue, 12 Sep 2023 00:58:20 -0700 (PDT) Message-ID: <20230912065502.143157215@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505499; 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=gvgWHgk38J3qGADoq/3qmkZUlBXc3nLuAkYAzeigZSY=; b=SWeVI0WK8cMrRFB6TR1C+N3NwUVDWm5AVEvmrYmAlTtkOxeZsnexP4ezkMSAciU+eNl3ar v5bKyHND27TzTXabykHUbt2rFrl1Zo6AT9zJHn08MumRY6bP37ei3vUtrp9g9sALQdTEUI JzUDN+lx6PWoDxGZQTym/+yI8uMx++rnqoV5oxW8zmJ1CWTij03I+2fMjFvCKUHbykNKH3 GGF9aE6AvMPqajGwRpdjcEMXECHz2wuZRpoRrlPpBmqBlf2B/pePmS/N/G82Eakr6AWwGW N1XxvNwGWLowdh/b2Lwh6RFhYEr5wbzpxVG5frq1dsD1kzaXv6CvBSYzMT1lRA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505499; 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=gvgWHgk38J3qGADoq/3qmkZUlBXc3nLuAkYAzeigZSY=; b=4O+bGAnHVHvrD+IwSwWv138yd9cTQPjIWL+EY3XqskIPxyDIl4ucC4C57Zb4vI4Jeij/u4 j4YCHTvq+ybbyqDA== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 22/30] x86/microcode: Add per CPU control field References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:58:18 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 12 Sep 2023 01:00:40 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777053518196401022 X-GMAIL-MSGID: 1777053518196401022 From: Thomas Gleixner Add a per CPU control field to ucode_ctrl and define constants for it: SCTRL_WAIT indicates that the CPU needs to spinwait with timeout SCTRL_APPLY indicates that the CPU needs to invoke the microcode_apply() callback SCTRL_DONE indicates that the CPU can proceed without invoking the microcode_apply() callback. In theory this could be a global control field, but a global control does not cover the following case: 15 primary CPUs load microcode successfully 1 primary CPU fails and returns with an error code With global control the sibling of the failed CPU would either try again or the whole operation would be aborted with the consequence that the 15 siblings do not invoke the apply path and end up with inconsistent software state. The result in dmesg would be inconsistent too. There are two additional fields added and initialized: ctrl_cpu and secondaries. ctrl_cpu is the CPU number of the primary thread for now, but with the upcoming uniform loading at package or system scope this will be one CPU per package or just one CPU. Secondaries hands the control CPU a CPU mask which will be required to release the secondary CPUs out of the wait loop. Preparatory change for implementing a properly split control flow for primary and secondary CPUs. Signed-off-by: Thomas Gleixner --- arch/x86/kernel/cpu/microcode/core.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) --- --- a/arch/x86/kernel/cpu/microcode/core.c +++ b/arch/x86/kernel/cpu/microcode/core.c @@ -324,8 +324,16 @@ static struct platform_device *microcode * requirement can be relaxed in the future. Right now, this is conservative * and good. */ +enum sibling_ctrl { + SCTRL_WAIT, + SCTRL_APPLY, + SCTRL_DONE, +}; + struct ucode_ctrl { + enum sibling_ctrl ctrl; enum ucode_state result; + unsigned int ctrl_cpu; }; static DEFINE_PER_CPU(struct ucode_ctrl, ucode_ctrl); @@ -468,7 +476,7 @@ static int ucode_load_late_stop_cpus(voi */ static bool ucode_setup_cpus(void) { - struct ucode_ctrl ctrl = { .result = -1, }; + struct ucode_ctrl ctrl = { .ctrl = SCTRL_WAIT, .result = -1, }; unsigned int cpu; for_each_cpu_and(cpu, cpu_present_mask, &cpus_booted_once_mask) { @@ -478,7 +486,15 @@ static bool ucode_setup_cpus(void) return false; } } - /* Initialize the per CPU state */ + + /* + * Initialize the per CPU state. This is core scope for now, + * but prepared to take package or system scope into account. + */ + if (topology_is_primary_thread(cpu)) + ctrl.ctrl_cpu = cpu; + else + ctrl.ctrl_cpu = cpumask_first(topology_sibling_cpumask(cpu)); per_cpu(ucode_ctrl, cpu) = ctrl; } return true; From patchwork Tue Sep 12 07:58:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138014 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp250479vqx; Tue, 12 Sep 2023 01:10:31 -0700 (PDT) X-Google-Smtp-Source: AGHT+IETk0Yfu+56wX61LUeBqFM/Xqal00CqUxETNBGACF6Ggu1hu7UwbVqSv2UuAWenMxFp7AdM X-Received: by 2002:a05:6870:e38b:b0:1c8:39a6:77a9 with SMTP id x11-20020a056870e38b00b001c839a677a9mr11791618oad.31.1694506231721; Tue, 12 Sep 2023 01:10:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694506231; cv=none; d=google.com; s=arc-20160816; b=anPN+shCGfFCVdCjMaJUL5DupWOLgWvEUs17N6qImWhi3K879lLZq8j/g/hYqqev6f SAiswQCGV5wmbDduh2F6LNcRJs7UKFFnIwxPeKgAp4KXlIF/e5KnkPXUOQVKhATXKZtU HxlR04i10XjggadTnd9a693rFZXqf0c+jz3p7qYQriKbxhHAyjRBwa7FLV0LZ1ltLfRK F5u3uDKp+MlPacECRegiA1IfI+XNI4/PzkLYHJKXAFTFbnSrDVZyKsKn4oax5LmjFRCN 8Hvyxs44sOsmDEt+p38pdYEYs117xGfxSuIU+WJUtZSquihDiITj52qtfPNUo9Ev1wCw uHrQ== 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=tcStbJBYGm1S6mX3lupmzFXgqW4kHxxe4AHt7jQaeKc=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=x6pv7eS+HC0rGeoui5DCgfUCH2215RJiXy7cxo6PAcmumF9YT8nsdhHdi8uDPR+Lb6 layhqspjWTEi1v/ZxpWcQsXxR9lTMBOHN5MqjeqC8kB7T3QfkNTZH+PrgPH4BG9EqnBQ iR7AhsHuX87o7Aco96f/iiXUIeOZd4sCVAhv/LgCglAN9mOf/B3lJYjxmLjbkLuwBIsU Y2h9y+qGwBYWPNp9bgBKuM8YTsPCXyBybWdU/6wdIhmA4zxIWCZDkk2fGlMrlbjzy5Ug 1ted9fIsp+yXKOg/H02GuaBfwKW9qdCJ2gfAqIr6KMpkp0hY1RmFs+MmKI3jTfEAT272 3VFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=TxPEeNSA; 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::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id u69-20020a638548000000b00565efb7b3efsi4943569pgd.225.2023.09.12.01.10.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 01:10:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=TxPEeNSA; 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::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 41BE78029896; Tue, 12 Sep 2023 01:01:21 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.8 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232062AbjILIAe (ORCPT + 39 others); Tue, 12 Sep 2023 04:00:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232169AbjILH7N (ORCPT ); Tue, 12 Sep 2023 03:59:13 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F259926AC for ; Tue, 12 Sep 2023 00:58:21 -0700 (PDT) Message-ID: <20230912065502.202675936@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505500; 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=tcStbJBYGm1S6mX3lupmzFXgqW4kHxxe4AHt7jQaeKc=; b=TxPEeNSADqF/1espoa4joMFPyaHU6sd/ZlOPhCdIrC7wCV7KkIwcL/CpJ3otO5/BeCCDCo eQM3VPvorqp+UHwR7xfGfR7oqCQMrU6H2AcGCDyFDt8NSD/2zPH3lA7Gbjqn4jFKln0mKf SvzyPyAAw/gjlJuf9mzIRFtSt5z/x0/+/YbOY7SGuSJDaiOaIpxabQPr/ONgil6EmnyV3y sMju8Gu3jM0lADivxRf8YWsQG72GcHPQNwgKPfHr4QMWpvfHJL/RVnxECXZ7eadRM0V1JL YnXR2WMROfkL7M/aK2fvIj7V2oMjuGhCn0C3CVYbwmp0C8NGIvdJYreJzxl/0A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505500; 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=tcStbJBYGm1S6mX3lupmzFXgqW4kHxxe4AHt7jQaeKc=; b=cAq/Q/RnWWyE8qyo5fCPHQbCSaX+Da2dUskr+Di+xg/82NEnYLBZSh4+X9yo3WN+YSDnT7 fO+si2bL29tUcAAw== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 23/30] x86/microcode: Provide new control functions References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:58:20 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Tue, 12 Sep 2023 01:01:21 -0700 (PDT) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776818566037316163 X-GMAIL-MSGID: 1776818566037316163 From: Thomas Gleixner The current all in one code is unreadable and really not suited for adding future features like uniform loading with package or system scope. Provide a set of new control functions which split the handling of the primary and secondary CPUs. These will replace the current rendevouz all in one function in the next step. This is intentionally a separate change because diff makes an complete unreadable mess otherwise. So the flow separates the primary and the secondary CPUs into their own functions, which use the control field in the per CPU ucode_ctrl struct. primary() secondary() wait_for_all() wait_for_all() apply_ucode() wait_for_release() release() apply_ucode() Signed-off-by: Thomas Gleixner --- arch/x86/kernel/cpu/microcode/core.c | 86 +++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) --- --- a/arch/x86/kernel/cpu/microcode/core.c +++ b/arch/x86/kernel/cpu/microcode/core.c @@ -357,6 +357,92 @@ static bool wait_for_cpus(atomic_t *cnt) return false; } +static bool wait_for_ctrl(void) +{ + unsigned int timeout; + + for (timeout = 0; timeout < USEC_PER_SEC; timeout++) { + if (this_cpu_read(ucode_ctrl.ctrl) != SCTRL_WAIT) + return true; + udelay(1); + if (!(timeout % 1000)) + touch_nmi_watchdog(); + } + return false; +} + +static __maybe_unused void ucode_load_secondary(unsigned int cpu) +{ + unsigned int ctrl_cpu = this_cpu_read(ucode_ctrl.ctrl_cpu); + enum ucode_state ret; + + /* Initial rendevouz to ensure that all CPUs have arrived */ + if (!wait_for_cpus(&late_cpus_in)) { + pr_err_once("Microcode load: %d CPUs timed out\n", + atomic_read(&late_cpus_in) - 1); + this_cpu_write(ucode_ctrl.result, UCODE_TIMEOUT); + return; + } + + /* + * Wait for primary threads to complete. If one of them hangs due + * to the update, there is no way out. This is non-recoverable + * because the CPU might hold locks or resources and confuse the + * scheduler, watchdogs etc. There is no way to safely evacuate the + * machine. + */ + if (!wait_for_ctrl()) + panic("Microcode load: Primary CPU %d timed out\n", ctrl_cpu); + + /* + * If the primary succeeded then invoke the apply() callback, + * otherwise copy the state from the primary thread. + */ + if (this_cpu_read(ucode_ctrl.ctrl) == SCTRL_APPLY) + ret = microcode_ops->apply_microcode(cpu); + else + ret = per_cpu(ucode_ctrl.result, ctrl_cpu); + + this_cpu_write(ucode_ctrl.result, ret); + this_cpu_write(ucode_ctrl.ctrl, SCTRL_DONE); +} + +static __maybe_unused void ucode_load_primary(unsigned int cpu) +{ + struct cpumask *secondaries = topology_sibling_cpumask(cpu); + enum sibling_ctrl ctrl; + enum ucode_state ret; + unsigned int sibling; + + /* Initial rendevouz to ensure that all CPUs have arrived */ + if (!wait_for_cpus(&late_cpus_in)) { + this_cpu_write(ucode_ctrl.result, UCODE_TIMEOUT); + pr_err_once("Microcode load: %d CPUs timed out\n", + atomic_read(&late_cpus_in) - 1); + return; + } + + ret = microcode_ops->apply_microcode(cpu); + this_cpu_write(ucode_ctrl.result, ret); + this_cpu_write(ucode_ctrl.ctrl, SCTRL_DONE); + + /* + * If the update was successful, let the siblings run the apply() + * callback. If not, tell them it's done. This also covers the + * case where the CPU has uniform loading at package or system + * scope implemented but does not advertise it. + */ + if (ret == UCODE_UPDATED || ret == UCODE_OK) + ctrl = SCTRL_APPLY; + else + ctrl = SCTRL_DONE; + + for_each_cpu(sibling, secondaries) { + if (sibling != cpu) + per_cpu(ucode_ctrl.ctrl, sibling) = ctrl; + } +} + static int ucode_load_cpus_stopped(void *unused) { int cpu = smp_processor_id(); From patchwork Tue Sep 12 07:58:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138646 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp863301vqx; Tue, 12 Sep 2023 22:20:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFjdu5tF+cze21xB7kPJADS1AgMMiur11vdpYUq9H7mJrgrkw9FLXwpdD0od3MsMvIaLK7t X-Received: by 2002:a05:6358:98a7:b0:13e:ea2a:40aa with SMTP id q39-20020a05635898a700b0013eea2a40aamr1861181rwa.8.1694582415348; Tue, 12 Sep 2023 22:20:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694582415; cv=none; d=google.com; s=arc-20160816; b=gzGVn1QIpQjaBkcnI0FD+vJng6oVFMtskkqktTVv7rw4qIkW+Pk7g3oTn1KOpZxi+r fN0p62y0ijOTAsgj+skLTk4Lt8K5ImxGfQCA+XYyPcylY15BlbFp28eT98Ik6dYQ8esd 1+q8+aBXfGzT0dxVFroHadsLxsyAugEWVieBR3bKIsd+xRlD7yGY+v7PnrWDiOocs71/ ZNqfjRUkwzwrd/TPapmFFKxvcRxOBCDuuQ9WO2OowtLBLM0mf7MKV7SejuEZi6k2nCP8 RMjFPkC+MeE1HSfipi+SyE7yIS0rP8lqltcEbfwuvaebw2WdURmRKAAbrb1eQkJcbBf4 0qtQ== 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=tBiZyVFg2RByesw9s33ESky6BK8jMzTV8d/B5WDIvI8=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=FwYT/DPEpOxLASTVQF+n+iA2+iy7Y8qkeYr/uOP7qZQHcNSG0tVji9H+76xdDic9va FovGAvl2BGoiqN11LpFQUf/EXUSNZuj4YvmDux1Hm6RjCZjioue8xmQEBYINez4gbkSj Dg9AvnlhkenWyoP3Hcl1y6NmJUEuYAOrC++34VC2bvzA8AMEOWxGlvCanuPU1ML/S+KE rABaNdRMMKI892sBcCVVAG+etXNY1oEk+PH20XglIf0z9Sn21uiiKmUi0gdzlyZFe0sZ g5OBqae/Cgmvt8W+FmUH+mQuRntjao/0qTO5begkpSkdOxKY99v2VbtwRMXzAxkPWc6b jc1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=ZZG7THSJ; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id s17-20020a056a00195100b0068c0300a3f7si9397423pfk.279.2023.09.12.22.20.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 22:20:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=ZZG7THSJ; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 81564851B9FE; Tue, 12 Sep 2023 01:00:49 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232040AbjILIAj (ORCPT + 39 others); Tue, 12 Sep 2023 04:00:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60772 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232220AbjILH7P (ORCPT ); Tue, 12 Sep 2023 03:59:15 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C9142701 for ; Tue, 12 Sep 2023 00:58:23 -0700 (PDT) Message-ID: <20230912065502.265405353@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505502; 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=tBiZyVFg2RByesw9s33ESky6BK8jMzTV8d/B5WDIvI8=; b=ZZG7THSJj1FSt3wSuX5xQNdGvOs2Cwn2089apJGMssuXfq1xxC47x5KnycQ14WAZUlpObq LG5K2NafGw0BeRifHaJcCodesPMLMGynYR4o4236a2UaIW9VnDPAq01dgYkzt5RLBTVY2N 5gBcFasCpmB8jV/tWJlSr+yHG4puA3xZanxYvLiykuIriKgKb3P+COP0Ty2ZKWiN3hbRCu 3U5uwZb5B7hTJOUsMfXAVcg8fgkHy63KICgOoeOQQtNitjngK5j15G+ejJbP3dRxoXM0o6 PZeAUb1OQ+RESWgmxzLAsZdaGMkbZkWJzI0uXO2fskyM3sEBJgr48qE3Ki3+Kg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505502; 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=tBiZyVFg2RByesw9s33ESky6BK8jMzTV8d/B5WDIvI8=; b=V/LNYjqIkgOictOpkJfjKYzl3XBC7Nbi3r6Hl6Hajb8CDdxUNdn5x2wQuVSDeT7swWQP5E KEHjxvrP2sdVZGCA== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 24/30] x86/microcode: Replace the all in one rendevouz handler References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:58:21 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 12 Sep 2023 01:00:49 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776898450532169233 X-GMAIL-MSGID: 1776898450532169233 From: Thomas Gleixner with a new handler which just separates the control flow of primary and secondary CPUs. Signed-off-by: Thomas Gleixner --- arch/x86/kernel/cpu/microcode/core.c | 51 ++++++----------------------------- 1 file changed, 9 insertions(+), 42 deletions(-) --- --- a/arch/x86/kernel/cpu/microcode/core.c +++ b/arch/x86/kernel/cpu/microcode/core.c @@ -337,7 +337,7 @@ struct ucode_ctrl { }; static DEFINE_PER_CPU(struct ucode_ctrl, ucode_ctrl); -static atomic_t late_cpus_in, late_cpus_out; +static atomic_t late_cpus_in; static bool wait_for_cpus(atomic_t *cnt) { @@ -371,7 +371,7 @@ static bool wait_for_ctrl(void) return false; } -static __maybe_unused void ucode_load_secondary(unsigned int cpu) +static void ucode_load_secondary(unsigned int cpu) { unsigned int ctrl_cpu = this_cpu_read(ucode_ctrl.ctrl_cpu); enum ucode_state ret; @@ -407,7 +407,7 @@ static __maybe_unused void ucode_load_se this_cpu_write(ucode_ctrl.ctrl, SCTRL_DONE); } -static __maybe_unused void ucode_load_primary(unsigned int cpu) +static void ucode_load_primary(unsigned int cpu) { struct cpumask *secondaries = topology_sibling_cpumask(cpu); enum sibling_ctrl ctrl; @@ -445,46 +445,14 @@ static __maybe_unused void ucode_load_pr static int ucode_load_cpus_stopped(void *unused) { - int cpu = smp_processor_id(); - enum ucode_state ret; - - /* - * Wait for all CPUs to arrive. A load will not be attempted unless all - * CPUs show up. - * */ - if (!wait_for_cpus(&late_cpus_in)) { - this_cpu_write(ucode_ctrl.result, UCODE_TIMEOUT); - return 0; - } - - /* - * On an SMT system, it suffices to load the microcode on one sibling of - * the core because the microcode engine is shared between the threads. - * Synchronization still needs to take place so that no concurrent - * loading attempts happen on multiple threads of an SMT core. See - * below. - */ - if (cpumask_first(topology_sibling_cpumask(cpu)) != cpu) - goto wait_for_siblings; + unsigned int cpu = smp_processor_id(); - ret = microcode_ops->apply_microcode(cpu); - this_cpu_write(ucode_ctrl.result, ret); - -wait_for_siblings: - if (!wait_for_cpus(&late_cpus_out)) - panic("Timeout during microcode update!\n"); - - /* - * At least one thread has completed update on each core. - * For others, simply call the update to make sure the - * per-cpu cpuinfo can be updated with right microcode - * revision. - */ - if (cpumask_first(topology_sibling_cpumask(cpu)) == cpu) - return 0; + if (this_cpu_read(ucode_ctrl.ctrl_cpu) == cpu) + ucode_load_primary(cpu); + else + ucode_load_secondary(cpu); - ret = microcode_ops->apply_microcode(cpu); - this_cpu_write(ucode_ctrl.result, ret); + /* No point to wait here. The CPUs will all wait in stop_machine(). */ return 0; } @@ -498,7 +466,6 @@ static int ucode_load_late_stop_cpus(voi pr_err("You should switch to early loading, if possible.\n"); atomic_set(&late_cpus_in, num_online_cpus()); - atomic_set(&late_cpus_out, num_online_cpus()); /* * Take a snapshot before the microcode update in order to compare and From patchwork Tue Sep 12 07:58:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138244 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp479193vqx; Tue, 12 Sep 2023 08:16:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHlN8/ot3fD2QrMSX43OUU2PxWkEQjRox3OlAa/j+bBtvEQaLvxj/LHSli8EW7JKK1+gdXd X-Received: by 2002:a05:6a20:9191:b0:155:5c28:ea63 with SMTP id v17-20020a056a20919100b001555c28ea63mr3379473pzd.31.1694531808986; Tue, 12 Sep 2023 08:16:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694531808; cv=none; d=google.com; s=arc-20160816; b=DI15syVrxE/UyJTQpgtzrKnU1sHM1FVSI2dWThQNJVjlsZ6QtfM2VJ2nOlXYTZEYOe axOsdCzVjdPpzyLeTv6vPXxRGWN/Rim3uhE3P2bP1A+00ZrypcdfKj3awWWF70mrL+fU jWF7+Ecm5QmtWbDTA2fHNiXP/Gf+UG290bRgN5jUz2NwotWvJStjsfLkaURCvIg+29Ul WrI+SPDjOTQHrgrtad7ilpXVf2BEeSO+ukydT7NBMFO900f/x/E81MP/r7qcTdi4kz+2 b1ZqWQInaevEdyPRXrQN89ScntXhR2TuYn5F7LdlDTYMaYGLqSXthwuRDxzCLIOWXdKK W9gA== 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=XQeEWe6WdR6oM1asQ2nr00uwr74uMZqlzfl8j1F3xiU=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=LfcfDvr/TwO12pBapWOvCHRU5ykcw8NWkMRjjYvlqwSi0/DMzHiIWXt4sOgg5A4aMw lmiz4p60va4hredZHRUAl/FhX7snQYdL3wHFqCiYOv55z6QplJLoU344Lsf7lnNOZIXZ Q7+M5+SykzToccOL/OBPEQiHURW2Se/LqwpUrDoLJVHZZF7TJY/6gcBlPDKCRiblzmy6 F3AZUtJaTClsb2xrefkWtvxMNXc9BI/LEPcjI45qbedL44cFFv+HVfibflpapsWkA5DZ nulixDlfGjRbWVRl0cgmVnkxoKp5JsRHih4weGkNxmHuMQei2rRBtafHfShSIJ94t1Pg H5ag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=41N+FOZm; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id u64-20020a638543000000b0053b928deb7asi5333739pgd.344.2023.09.12.08.16.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 08:16:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=41N+FOZm; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 9B1C081972DA; Tue, 12 Sep 2023 01:01:15 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232272AbjILIAm (ORCPT + 39 others); Tue, 12 Sep 2023 04:00:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49640 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232238AbjILH7P (ORCPT ); Tue, 12 Sep 2023 03:59:15 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B35DA2711 for ; Tue, 12 Sep 2023 00:58:25 -0700 (PDT) Message-ID: <20230912065502.327149876@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505503; 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=XQeEWe6WdR6oM1asQ2nr00uwr74uMZqlzfl8j1F3xiU=; b=41N+FOZmOtQlwvjGiX1RLxK0vHR1VCly6S/Zss2dkcbmTFMg9sdGEwh/TEuiuYxfR584XA OQ/JySfQN8sY9+ws0djE4+7LszmK+TaN5UMzcv9TyM6XLnwqbqSxxOqf3fqR6C76TRiCL2 eRq5FJMF4ymy5guSJs7dMeAjxxDWWvVoBq3WeTRxVC6zcLLLuyJZanuKoOn4PWOUwWyjt2 wX2NC9XK8S7S/1XszSCiHZvQOYi/RHVGzyYTmehnk+cbOdfpwm9Tca+jBN/1AGMXWr3/P9 0zHZ3ehJ9tdJUqnq+mKS3QqdgiBHg4VKbdpMjJE5UB/wR/EYwH4XNucThkypMg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505503; 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=XQeEWe6WdR6oM1asQ2nr00uwr74uMZqlzfl8j1F3xiU=; b=LNbW5uPCaviP6ObvLuRLbPW0FvYw6tiWxSvBzo9G8WNN9YlKy3j5vGWb2jwN4g9tvgCcye 9OSFMWsCvDM+nCCg== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 25/30] x86/microcode: Rendezvous and load in NMI References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:58:23 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 12 Sep 2023 01:01:15 -0700 (PDT) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776845386227570169 X-GMAIL-MSGID: 1776845386227570169 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 @@ -70,4 +70,16 @@ static inline u32 intel_get_microcode_re } #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 @@ -312,8 +314,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; @@ -327,7 +331,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 */ @@ -343,7 +348,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; @@ -421,7 +427,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(); @@ -430,7 +436,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; } @@ -451,8 +479,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 @@ -601,6 +601,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) { From patchwork Tue Sep 12 07:58:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138338 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp571284vqx; Tue, 12 Sep 2023 10:37:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGKf6Pai0LJ44pzcvGgeJSPmrYAsmgNhYIupFTD9TmwDeY6X2dnakFvTTWx+YRot7iJfzzh X-Received: by 2002:a17:90b:3d7:b0:269:60ed:d495 with SMTP id go23-20020a17090b03d700b0026960edd495mr10203968pjb.27.1694540239089; Tue, 12 Sep 2023 10:37:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694540239; cv=none; d=google.com; s=arc-20160816; b=Q1RSpVf+gqkgpbIJwkqHvJTW6x0sb+DraN/Kpa6ecehkffKIbGW2tBwQoF+5MKPqLv Jkvm3Gwj5ShDEFGwFI/uRlKyMeQ/qzRZ0EjOV+MJ0B/WZ41bvI4ufc7aRvp9pHNNsC/M OaNnRfxpCyMTGU/4aNGdhdxsyWitaT6f4JgOMbwEqOdOop6ZCOzDVnZ+nJw6NZgJShi8 410R6ztruTfZspD5RFpDnfUPt1kRPSMWT7Hi7NmI2K+in4KUE0UqUuZZqQ4AwdrHEf3T uWQvyi9nyVOlXZgQKGEG/Rkg0EdC3VFclQ69bs04uLSRRHcp+xyTCpvJq3273wXaPXBR Bftg== 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=AUqgutA67pCMFlyFQkQb2NLp+uq+lsysGvpYhmGpJnk=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=NqqcFl9pky6Nl7lE6NH7dfhTYxcGvp9CtaafSCVjrrPmm13KK++hqwahp/bNw+Mjfb DXgu+T8jczxlHr/UrnY9e2G/V0ZHvGZZblbk60bSwgFjUT219oQhWJzflzMe5lvrD954 sXuz0P5xMpwTK1V1oc31WnH6FCUISHJaG2RkqRU9k05N/u0FWuSTsP1U1oYASGX+yAbQ UMd0bcOHrzkx5vePNUvHZKDv8TCFpQJ4UHHyeLtygRAw9cm3StnO24ooBLGMIYcnSfcv TggnqfGHOFMxAEUcI4kmr1KRqmwYnqVafhdxYYV9Raw64BwkRS1o9Ii2GtA0DZk4GSmi jWtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=vVvNjb6D; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id j10-20020a17090a2a8a00b002630c9e77ddsi8239369pjd.46.2023.09.12.10.37.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 10:37:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=vVvNjb6D; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 329ED869EAE4; Tue, 12 Sep 2023 01:00:57 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232085AbjILIAv (ORCPT + 39 others); Tue, 12 Sep 2023 04:00:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48180 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232280AbjILH7V (ORCPT ); Tue, 12 Sep 2023 03:59:21 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 505072728 for ; Tue, 12 Sep 2023 00:58:26 -0700 (PDT) Message-ID: <20230912065502.386652173@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505505; 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=AUqgutA67pCMFlyFQkQb2NLp+uq+lsysGvpYhmGpJnk=; b=vVvNjb6DoESiSCvmGTjtbFzfa6DPy3PuxgW5gY3N6BWwzUWem/iicInu5LX0u55WVg8O1i M/GEc+8CWdLVi0P5WIvTsbFyglGRMwQr9l6Qs9iGuXuq231vJ2UKY0YWJsYTjOFjqx3K9B L8rk331Lhgl9Q0U8OF3e6E2YaqzmNTiv3PuKV88Vv4yizG2fxfK62JGU0BNUubYBeGewug M5xUj3vTDQfuUUC0X4k+DzXp4T9PV2o8PEY5T+/1SczxUe8KCNIaAzLKZb1Y6w5YXUCcd2 DV7o6IR88qpicxCLjSrIJ+Jg0gpPR6WTP7lIlf4lbtSl9dnApReBbaWh+R2/EA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505505; 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=AUqgutA67pCMFlyFQkQb2NLp+uq+lsysGvpYhmGpJnk=; b=IUFdw3vcZ8X+udEbRs8UFEK1ZPGNlLrfelZKhY1ovRGKza23og2UjRohm8QF+PU2gevEGH rvofICT3kLdzp2CA== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 26/30] x86/microcode: Protect against instrumentation References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:58:24 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 12 Sep 2023 01:00:57 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776854225519836691 X-GMAIL-MSGID: 1776854225519836691 From: Thomas Gleixner The wait for control loop in which the siblings are waiting for the microcode update on the primary thread must be protected against instrumentation as instrumentation can end up in #INT3, #DB or #PF, which then returns with IRET. That IRET reenables NMI which is the opposite of what the NMI rendezvouz is trying to achieve. Signed-off-by: Thomas Gleixner --- arch/x86/kernel/cpu/microcode/core.c | 112 ++++++++++++++++++++++++++--------- 1 file changed, 84 insertions(+), 28 deletions(-) --- --- a/arch/x86/kernel/cpu/microcode/core.c +++ b/arch/x86/kernel/cpu/microcode/core.c @@ -319,53 +319,65 @@ struct ucode_ctrl { DEFINE_STATIC_KEY_FALSE(microcode_nmi_handler_enable); static DEFINE_PER_CPU(struct ucode_ctrl, ucode_ctrl); +static unsigned int loops_per_usec; static atomic_t late_cpus_in; -static bool wait_for_cpus(atomic_t *cnt) +static noinstr bool wait_for_cpus(atomic_t *cnt) { - unsigned int timeout; + unsigned int timeout, loops; - WARN_ON_ONCE(atomic_dec_return(cnt) < 0); + WARN_ON_ONCE(raw_atomic_dec_return(cnt) < 0); for (timeout = 0; timeout < USEC_PER_SEC; timeout++) { - if (!atomic_read(cnt)) + if (!raw_atomic_read(cnt)) return true; - udelay(1); + + for (loops = 0; loops < loops_per_usec; loops++) + cpu_relax(); + /* If invoked directly, tickle the NMI watchdog */ - if (!microcode_ops->use_nmi && !(timeout % 1000)) + if (!microcode_ops->use_nmi && !(timeout % 1000)) { + instrumentation_begin(); touch_nmi_watchdog(); + instrumentation_end(); + } } /* Prevent the late comers to make progress and let them time out */ - atomic_inc(cnt); + raw_atomic_inc(cnt); return false; } -static bool wait_for_ctrl(void) +static noinstr bool wait_for_ctrl(void) { - unsigned int timeout; + unsigned int timeout, loops; for (timeout = 0; timeout < USEC_PER_SEC; timeout++) { - if (this_cpu_read(ucode_ctrl.ctrl) != SCTRL_WAIT) + if (raw_cpu_read(ucode_ctrl.ctrl) != SCTRL_WAIT) return true; - udelay(1); + + for (loops = 0; loops < loops_per_usec; loops++) + cpu_relax(); + /* If invoked directly, tickle the NMI watchdog */ - if (!microcode_ops->use_nmi && !(timeout % 1000)) + if (!microcode_ops->use_nmi && !(timeout % 1000)) { + instrumentation_begin(); touch_nmi_watchdog(); + instrumentation_end(); + } } return false; } -static void ucode_load_secondary(unsigned int cpu) +/* + * Protected against instrumentation up to the point where the primary + * thread completed the update. See microcode_nmi_handler() for details. + */ +static noinstr bool ucode_load_secondary_wait(unsigned int ctrl_cpu) { - unsigned int ctrl_cpu = this_cpu_read(ucode_ctrl.ctrl_cpu); - enum ucode_state ret; - /* Initial rendevouz to ensure that all CPUs have arrived */ if (!wait_for_cpus(&late_cpus_in)) { - pr_err_once("Microcode load: %d CPUs timed out\n", - atomic_read(&late_cpus_in) - 1); this_cpu_write(ucode_ctrl.result, UCODE_TIMEOUT); - return; + return false; } /* @@ -375,9 +387,33 @@ static void ucode_load_secondary(unsigne * scheduler, watchdogs etc. There is no way to safely evacuate the * machine. */ - if (!wait_for_ctrl()) - panic("Microcode load: Primary CPU %d timed out\n", ctrl_cpu); + if (wait_for_ctrl()) + return true; + + instrumentation_begin(); + panic("Microcode load: Primary CPU %d timed out\n", ctrl_cpu); + instrumentation_end(); +} +/* + * Protected against instrumentation up to the point where the primary + * thread completed the update. See microcode_nmi_handler() for details. + */ +static noinstr void ucode_load_secondary(unsigned int cpu) +{ + unsigned int ctrl_cpu = raw_cpu_read(ucode_ctrl.ctrl_cpu); + enum ucode_state ret; + + if (!ucode_load_secondary_wait(ctrl_cpu)) { + instrumentation_begin(); + pr_err_once("Microcode load: %d CPUs timed out\n", + atomic_read(&late_cpus_in) - 1); + instrumentation_end(); + return; + } + + /* Primary thread completed. Allow to invoke instrumentable code */ + instrumentation_begin(); /* * If the primary succeeded then invoke the apply() callback, * otherwise copy the state from the primary thread. @@ -389,6 +425,7 @@ static void ucode_load_secondary(unsigne this_cpu_write(ucode_ctrl.result, ret); this_cpu_write(ucode_ctrl.ctrl, SCTRL_DONE); + instrumentation_end(); } static void ucode_load_primary(unsigned int cpu) @@ -427,25 +464,43 @@ static void ucode_load_primary(unsigned } } -static bool microcode_update_handler(void) +static noinstr bool microcode_update_handler(void) { - unsigned int cpu = smp_processor_id(); + unsigned int cpu = raw_smp_processor_id(); - if (this_cpu_read(ucode_ctrl.ctrl_cpu) == cpu) + if (raw_cpu_read(ucode_ctrl.ctrl_cpu) == cpu) { + instrumentation_begin(); ucode_load_primary(cpu); - else + instrumentation_end(); + } else { ucode_load_secondary(cpu); + } + instrumentation_begin(); touch_nmi_watchdog(); + instrumentation_end(); + return true; } -bool microcode_nmi_handler(void) +/* + * Protection against instrumentation is required for CPUs which are not + * safe against an NMI which is delivered to the secondary SMT sibling + * while the primary thread updates the microcode. Instrumentation can end + * up in #INT3, #DB and #PF. The IRET from those exceptions reenables NMI + * which is the opposite of what the NMI rendevouz is trying to achieve. + * + * The primary thread is safe versus instrumentation as the actual + * microcode update handles this correctly. It's only the sibling code + * path which must be NMI safe until the primary thread completed the + * update. + */ +bool noinstr microcode_nmi_handler(void) { - if (!this_cpu_read(ucode_ctrl.nmi_enabled)) + if (!raw_cpu_read(ucode_ctrl.nmi_enabled)) return false; - this_cpu_write(ucode_ctrl.nmi_enabled, false); + raw_cpu_write(ucode_ctrl.nmi_enabled, false); return microcode_update_handler(); } @@ -472,6 +527,7 @@ static int ucode_load_late_stop_cpus(voi pr_err("You should switch to early loading, if possible.\n"); atomic_set(&late_cpus_in, num_online_cpus()); + loops_per_usec = loops_per_jiffy / (TICK_NSEC / 1000); /* * Take a snapshot before the microcode update in order to compare and From patchwork Tue Sep 12 07:58:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138423 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp664189vqx; Tue, 12 Sep 2023 13:32:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEhlu3QfaPLFesQ70CfJsy3c+dHtYxw8zFYhdfeSn3W24+KQ/7B6PzeWUdacVRY0wrGq28N X-Received: by 2002:a17:903:2687:b0:1c3:9b9b:8db0 with SMTP id jf7-20020a170903268700b001c39b9b8db0mr816336plb.33.1694550755970; Tue, 12 Sep 2023 13:32:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694550755; cv=none; d=google.com; s=arc-20160816; b=dbWV0cusoKjMOWGxA/ABY8nI2R8BsxeKVLZQ561nZn61dt5wX7C7q1+PlUmGZqm9YV H5q2H5vbTGU9TvEjYwWD4BX8p9AY8P5bAyj9wsUDV/gORsQ1k7/xtk5g3prb5H2T4WAF zW78z6csGqE+AM7MhUpHM7EGf3UzQ86AuHMoQqXuoYgen8x35978jhp/ybJRCtFTGW4g PYg9RWbbici5oJ7KRsEopz608CjkxTjYlzevAxtKEsKziKan3+ouI/iKYqP3ku+Y0uz6 df3iEoc0GyhyMQJb6FKUG3mbPoQgPGmXOBzpJsfpC1fSpRupo9ui/ITtGEu19kMRJToV NRAQ== 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=5EsTuHLRMt+mh9/+wwjfJDEI240zRMwQu4qtcC7nL1o=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=boUPQSl19YvzCXXpDHGytBoKRFkB1oU5bkWDzkty5Bl0O7/H6xPsBIqLAqxg/IoVja 5HNrFyztoY66jPRgVe3kcS7t8L1gsvV15ouramfFQvCdJqPdcnFhu/uu4LG1Oc3py0zZ Ey7P/fTNB2uGv1z4QWZARQPMwPrRecmMmWGJqFyTbZsapPuU5sSLGAJOvt7vevIunnUX fYzu5NBOpkwdgSsmFAclS8t5fhW4yvh95RPnO+KFBMVzsiqM3jhf2KCrX3eLQAXyAS2H b954rwAuSGJHQ9NevzacREc4Q5YL4cXLVnnEdLxZfKHzJcbmQx69F2O6sPOCxr2dHnGm aLHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=JIweMxVu; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=onPRJkE8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id u7-20020a170903124700b001b7fee7d5f0si8850156plh.25.2023.09.12.13.32.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 13:32:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=JIweMxVu; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=onPRJkE8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 7F90A811679E; Tue, 12 Sep 2023 01:01:34 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232365AbjILIBF (ORCPT + 39 others); Tue, 12 Sep 2023 04:01:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232209AbjILH7k (ORCPT ); Tue, 12 Sep 2023 03:59:40 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E9972735 for ; Tue, 12 Sep 2023 00:58:28 -0700 (PDT) Message-ID: <20230912065502.447323060@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505507; 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=5EsTuHLRMt+mh9/+wwjfJDEI240zRMwQu4qtcC7nL1o=; b=JIweMxVumgLN2Jb9TYNX4i6lx3NVqn5j9hplEau6xHGzTKkg+WFNcyVH895L1WO9Gr/1+i icl8vj10fqY31MV0CjcfTOE5k360HfwWOtLcB/NWFttcWmEEfxfOnU041zsrlWql2DC0ix Y8GXH/+U02p7fCXbNWXOiWyqCw22faG2CTXLfz8Diw9xLeyXGeKwq1xrYgDQMTy4LzeR0S tDLVlAOsDRn/8COgxxpX/xySnZZ4N6MXzMKzirgZSm5kwA7kuaXDZYeY2J6GeuUpD2D+6A 7b0cW/oofG4xVoTcbKidHjxIuPXOSR46dEl8JS77wT1MsVsC103GtuOgBWBPYg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505507; 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=5EsTuHLRMt+mh9/+wwjfJDEI240zRMwQu4qtcC7nL1o=; b=onPRJkE8jmhAVCNUauItBBIxPd3hEt/0KKcvmTt+3Fd5TSeUNrP5uxkLpaQhwhK5kqGQeU MOohhjhtM2pfGlBg== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 27/30] x86/apic: Provide apic_force_nmi_on_cpu() References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:58:26 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 12 Sep 2023 01:01:34 -0700 (PDT) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776865253669065245 X-GMAIL-MSGID: 1776865253669065245 From: Thomas Gleixner When SMT siblings are soft-offlined and parked in one of the play_dead() variants they still react on NMI, which is problematic on affected Intel CPUs. The default play_dead() variant uses MWAIT on modern CPUs, which is not guaranteed to be safe when updated concurrently. Right now late loading is prevented when not all SMT siblings are online, but as they still react on NMI, it is possible to bring them out of their park position into a trivial rendevouz handler. Provide a function which allows to do that. I does sanity checks whether the target is in the cpus_booted_once_mask and whether the APIC driver supports it. Mark X2APIC and XAPIC as capable, but exclude 32bit and the UV and NUMACHIP variants as that needs feedback from the relevant experts. Signed-off-by: Thomas Gleixner --- arch/x86/include/asm/apic.h | 5 ++++- arch/x86/kernel/apic/apic_flat_64.c | 2 ++ arch/x86/kernel/apic/ipi.c | 8 ++++++++ arch/x86/kernel/apic/x2apic_cluster.c | 1 + arch/x86/kernel/apic/x2apic_phys.c | 1 + 5 files changed, 16 insertions(+), 1 deletion(-) --- --- a/arch/x86/include/asm/apic.h +++ b/arch/x86/include/asm/apic.h @@ -276,7 +276,8 @@ struct apic { u32 disable_esr : 1, dest_mode_logical : 1, - x2apic_set_max_apicid : 1; + x2apic_set_max_apicid : 1, + nmi_to_offline_cpu : 1; u32 (*calc_dest_apicid)(unsigned int cpu); @@ -542,6 +543,8 @@ extern bool default_check_apicid_used(ph extern void default_ioapic_phys_id_map(physid_mask_t *phys_map, physid_mask_t *retmap); extern int default_cpu_present_to_apicid(int mps_cpu); +void apic_send_nmi_to_offline_cpu(unsigned int cpu); + #else /* CONFIG_X86_LOCAL_APIC */ static inline unsigned int read_apic_id(void) { return 0; } --- a/arch/x86/kernel/apic/apic_flat_64.c +++ b/arch/x86/kernel/apic/apic_flat_64.c @@ -103,6 +103,7 @@ static struct apic apic_flat __ro_after_ .send_IPI_allbutself = default_send_IPI_allbutself, .send_IPI_all = default_send_IPI_all, .send_IPI_self = default_send_IPI_self, + .nmi_to_offline_cpu = true, .read = native_apic_mem_read, .write = native_apic_mem_write, @@ -175,6 +176,7 @@ static struct apic apic_physflat __ro_af .send_IPI_allbutself = default_send_IPI_allbutself, .send_IPI_all = default_send_IPI_all, .send_IPI_self = default_send_IPI_self, + .nmi_to_offline_cpu = true, .read = native_apic_mem_read, .write = native_apic_mem_write, --- a/arch/x86/kernel/apic/ipi.c +++ b/arch/x86/kernel/apic/ipi.c @@ -97,6 +97,14 @@ void native_send_call_func_ipi(const str __apic_send_IPI_mask(mask, CALL_FUNCTION_VECTOR); } +void apic_send_nmi_to_offline_cpu(unsigned int cpu) +{ + if (WARN_ON_ONCE(!apic->nmi_to_offline_cpu)) + return; + if (WARN_ON_ONCE(!cpumask_test_cpu(cpu, &cpus_booted_once_mask))) + return; + apic->send_IPI(cpu, NMI_VECTOR); +} #endif /* CONFIG_SMP */ static inline int __prepare_ICR2(unsigned int mask) --- a/arch/x86/kernel/apic/x2apic_cluster.c +++ b/arch/x86/kernel/apic/x2apic_cluster.c @@ -251,6 +251,7 @@ static struct apic apic_x2apic_cluster _ .send_IPI_allbutself = x2apic_send_IPI_allbutself, .send_IPI_all = x2apic_send_IPI_all, .send_IPI_self = x2apic_send_IPI_self, + .nmi_to_offline_cpu = true, .read = native_apic_msr_read, .write = native_apic_msr_write, --- a/arch/x86/kernel/apic/x2apic_phys.c +++ b/arch/x86/kernel/apic/x2apic_phys.c @@ -166,6 +166,7 @@ static struct apic apic_x2apic_phys __ro .send_IPI_allbutself = x2apic_send_IPI_allbutself, .send_IPI_all = x2apic_send_IPI_all, .send_IPI_self = x2apic_send_IPI_self, + .nmi_to_offline_cpu = true, .read = native_apic_msr_read, .write = native_apic_msr_write, From patchwork Tue Sep 12 07:58:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138245 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp483192vqx; Tue, 12 Sep 2023 08:22:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF60S3Z0sd5bik/AFLanTxZeqQ+8a+vWv3hxBcE3Yet2V6fV3Vo0GAdb8GS8sMQYn+2k230 X-Received: by 2002:a17:90a:f191:b0:26b:3751:652a with SMTP id bv17-20020a17090af19100b0026b3751652amr12104093pjb.38.1694532157768; Tue, 12 Sep 2023 08:22:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694532157; cv=none; d=google.com; s=arc-20160816; b=prt5ZkBUhzoNtChCyp2ZsQM/SZrNvhB1LP+a1MsKcw2N1VwSOyde8e5oCL8bD6v+Or nuLPY1H5t+iWWecN3dn37wOB8msW+2naSWsD+AOxdWCHCppy9pHluTCIZdhyWrdxrWOt DTTYb7J/g+DPeKA/rzPtUqUa4LPsKwVZCB1k3p9rfgmXmQVxo5p4vqsOVzU0tyKpGHq4 ijusj0gZv1+ww+1LYsJVhUxybSZ4M6ukYBN8AtHBh3IwNYHeRDjjAOlEhdT0zy86bJ+Z Kz9FEh7DCWODO7VwHxJYq7r2j3xnRq92C7C53tw5LGvVcQpl45EY8c16nNH0e7ZZaXrQ JSoA== 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=M0PdCWTDbxqvJEjnQT+Zz1AFGlSL0PoJj3xhmsUVjSU=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=XK5l3Ja/KJjAC8KOR9pJcv2up2JYIW9knDqyfL7rR8ozZUVCevE+9uenfFXt/DCztL MPuCZyIHRRYd+/iaHKyJQ9GI3LWE7bd6ScMJh+/dg/vAInQm9Dm5YS4XtKbw9DNykZxy iZH+WxVEqvcvziHIA4DOiB0g72LO9Xa0yXB17MK/LKH8hqcFvr/f0/7vaETIWF8S2DV5 ia2LVJ0HW5+CVtzwZRwFzNmoXI3ur+as819obRvwqFKmkFCJ+X9FUFb+PUk4LyIafiUh jxJdmdtOT943GOyxJXt3u1ZJ4OXYKM5mE8xLVhw22n4E8Loe/wKsIFWQnLHpMS6HbO8/ AL/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=WJFNhXUR; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=qkm8ylO3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id me3-20020a17090b17c300b00262ff3a4545si6139861pjb.169.2023.09.12.08.22.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 08:22:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=WJFNhXUR; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=qkm8ylO3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id D74D980A52B8; Tue, 12 Sep 2023 01:04:20 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.8 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232375AbjILIBJ (ORCPT + 39 others); Tue, 12 Sep 2023 04:01:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42608 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232319AbjILH7z (ORCPT ); Tue, 12 Sep 2023 03:59:55 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90D762D4C for ; Tue, 12 Sep 2023 00:58:30 -0700 (PDT) Message-ID: <20230912065502.507838060@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505508; 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=M0PdCWTDbxqvJEjnQT+Zz1AFGlSL0PoJj3xhmsUVjSU=; b=WJFNhXURunT+UUAt0yLxmQJYOM9x7pobFvhwAsGCYnxu1NtcAYYPy136l0anxbcST+2uGD dFhI+2o9Rd5WtByPHl4zHPkIlizrt18BCHsueMwrHHBVsK5ys10C25PbOFb29Qa/23m6dA 3GGQQy2rEHubTdYuKjkMTdOj4+3Qhy0slm18eG2C0hVeKTS/TGZTNDk98OvRhmu7RZe3Wh u/vLdb5QMCji8nn6iO4YFvHwfvhenCdrday43dUYkRkCqnbDQkO+iDDF5AhXhlitXHv4YW gMryfPe+wNz7j9MY2S0N9tKVOuPc4GicGEBhZRzPp0fsGZwnTuBfC7z8+ktJlg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505508; 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=M0PdCWTDbxqvJEjnQT+Zz1AFGlSL0PoJj3xhmsUVjSU=; b=qkm8ylO36PDb1/S3GKM+jtPlcSRQCQ6iGq10TaOtVE0DDRHX9WB2UhPfJDyxVifxdM3nLP OqssZpdfx96u1rDQ== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 28/30] x86/microcode: Handle "offline" CPUs correctly References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:58:28 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 12 Sep 2023 01:04:21 -0700 (PDT) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776845751710238289 X-GMAIL-MSGID: 1776845751710238289 From: Thomas Gleixner Offline CPUs need to be parked in a safe loop when microcode update is in progress on the primary CPU. Currently offline CPUs are parked in 'mwait_play_dead()', and for Intel CPUs, its not a safe instruction, because 'mwait' instruction can be patched in the new microcode update that can cause instability. - Adds a new microcode state 'UCODE_OFFLINE' to report status on per-cpu basis. - Force NMI on the offline CPUs. Wakeup offline CPUs while the update is in progress and then return them back to 'mwait_play_dead()' after microcode update is complete. Signed-off-by: Thomas Gleixner --- arch/x86/include/asm/microcode.h | 1 arch/x86/kernel/cpu/microcode/core.c | 112 +++++++++++++++++++++++++++++-- arch/x86/kernel/cpu/microcode/internal.h | 1 arch/x86/kernel/nmi.c | 5 + 4 files changed, 113 insertions(+), 6 deletions(-) --- --- a/arch/x86/include/asm/microcode.h +++ b/arch/x86/include/asm/microcode.h @@ -71,6 +71,7 @@ static inline u32 intel_get_microcode_re #endif /* !CONFIG_CPU_SUP_INTEL */ bool microcode_nmi_handler(void); +void microcode_offline_nmi_handler(void); #ifdef CONFIG_MICROCODE_LATE_LOADING DECLARE_STATIC_KEY_FALSE(microcode_nmi_handler_enable); --- a/arch/x86/kernel/cpu/microcode/core.c +++ b/arch/x86/kernel/cpu/microcode/core.c @@ -298,8 +298,9 @@ struct ucode_ctrl { DEFINE_STATIC_KEY_FALSE(microcode_nmi_handler_enable); static DEFINE_PER_CPU(struct ucode_ctrl, ucode_ctrl); +static atomic_t late_cpus_in, offline_in_nmi; static unsigned int loops_per_usec; -static atomic_t late_cpus_in; +static cpumask_t cpu_offline_mask; static noinstr bool wait_for_cpus(atomic_t *cnt) { @@ -407,7 +408,7 @@ static noinstr void ucode_load_secondary instrumentation_end(); } -static void ucode_load_primary(unsigned int cpu) +static void __ucode_load_primary(unsigned int cpu) { struct cpumask *secondaries = topology_sibling_cpumask(cpu); enum sibling_ctrl ctrl; @@ -443,6 +444,67 @@ static void ucode_load_primary(unsigned } } +static bool ucode_kick_offline_cpus(unsigned int nr_offl) +{ + unsigned int cpu, timeout; + + for_each_cpu(cpu, &cpu_offline_mask) { + /* Enable the rendevouz handler and send NMI */ + per_cpu(ucode_ctrl.nmi_enabled, cpu) = true; + apic_send_nmi_to_offline_cpu(cpu); + } + + /* Wait for them to arrive */ + for (timeout = 0; timeout < (USEC_PER_SEC / 2); timeout++) { + if (atomic_read(&offline_in_nmi) == nr_offl) + return true; + udelay(1); + } + /* Let the others time out */ + return false; +} + +static void ucode_release_offline_cpus(void) +{ + unsigned int cpu; + + for_each_cpu(cpu, &cpu_offline_mask) + per_cpu(ucode_ctrl.ctrl, cpu) = SCTRL_DONE; +} + +static void ucode_load_primary(unsigned int cpu) +{ + unsigned int nr_offl = cpumask_weight(&cpu_offline_mask); + bool proceed = true; + + /* Kick soft-offlined SMT siblings if required */ + if (!cpu && nr_offl) + proceed = ucode_kick_offline_cpus(nr_offl); + + /* If the soft-offlined CPUs did not respond, abort */ + if (proceed) + __ucode_load_primary(cpu); + + /* Unconditionally release soft-offlined SMT siblings if required */ + if (!cpu && nr_offl) + ucode_release_offline_cpus(); +} + +/* + * Minimal stub rendevouz handler for soft-offlined CPUs which participate + * in the NMI rendevouz to protect against a concurrent NMI on affected + * CPUs. + */ +void noinstr microcode_offline_nmi_handler(void) +{ + if (!raw_cpu_read(ucode_ctrl.nmi_enabled)) + return; + raw_cpu_write(ucode_ctrl.nmi_enabled, false); + raw_cpu_write(ucode_ctrl.result, UCODE_OFFLINE); + raw_atomic_inc(&offline_in_nmi); + wait_for_ctrl(); +} + static noinstr bool microcode_update_handler(void) { unsigned int cpu = raw_smp_processor_id(); @@ -499,6 +561,7 @@ static int ucode_load_cpus_stopped(void static int ucode_load_late_stop_cpus(void) { unsigned int cpu, updated = 0, failed = 0, timedout = 0, siblings = 0; + unsigned int nr_offl, offline = 0; int old_rev = boot_cpu_data.microcode; struct cpuinfo_x86 prev_info; @@ -506,6 +569,7 @@ static int ucode_load_late_stop_cpus(voi pr_err("You should switch to early loading, if possible.\n"); atomic_set(&late_cpus_in, num_online_cpus()); + atomic_set(&offline_in_nmi, 0); loops_per_usec = loops_per_jiffy / (TICK_NSEC / 1000); /* @@ -528,6 +592,7 @@ static int ucode_load_late_stop_cpus(voi case UCODE_UPDATED: updated++; break; case UCODE_TIMEOUT: timedout++; break; case UCODE_OK: siblings++; break; + case UCODE_OFFLINE: offline++; break; default: failed++; break; } } @@ -539,6 +604,13 @@ static int ucode_load_late_stop_cpus(voi /* Nothing changed. */ if (!failed && !timedout) return 0; + + nr_offl = cpumask_weight(&cpu_offline_mask); + if (offline < nr_offl) { + pr_warn("%u offline siblings did not respond.\n", + nr_offl - atomic_read(&offline_in_nmi)); + return -EIO; + } pr_err("Microcode update failed: %u CPUs failed %u CPUs timed out\n", failed, timedout); return -EIO; @@ -572,19 +644,49 @@ static int ucode_load_late_stop_cpus(voi * modern CPUs is using MWAIT, which is also not guaranteed to be safe * against a microcode update which affects MWAIT. * - * 2) Initialize the per CPU control structure + * As soft-offlined CPUs still react on NMIs, the SMT sibling + * restriction can be lifted when the vendor driver signals to use NMI + * for rendevouz and the APIC provides a mechanism to send an NMI to a + * soft-offlined CPU. The soft-offlined CPUs are then able to + * participate in the rendezvouz in a trivial stub handler. + * + * 2) Initialize the per CPU control structure and create a cpumask + * which contains "offline"; secondary threads, so they can be handled + * correctly by a control CPU. */ static bool ucode_setup_cpus(void) { struct ucode_ctrl ctrl = { .ctrl = SCTRL_WAIT, .result = -1, }; + bool allow_smt_offline; unsigned int cpu; + allow_smt_offline = microcode_ops->nmi_safe || + (microcode_ops->use_nmi && apic->nmi_to_offline_cpu); + + cpumask_clear(&cpu_offline_mask); + for_each_cpu_and(cpu, cpu_present_mask, &cpus_booted_once_mask) { + /* + * Offline CPUs sit in one of the play_dead() functions + * with interrupts disabled, but they still react on NMIs + * and execute arbitrary code. Also MWAIT being updated + * while the offline CPU sits there is not necessarily safe + * on all CPU variants. + * + * Mark them in the offline_cpus mask which will be handled + * by CPU0 later in the update process. + * + * Ensure that the primary thread is online so that it is + * guaranteed that all cores are updated. + */ if (!cpu_online(cpu)) { - if (topology_is_primary_thread(cpu) || !microcode_ops->nmi_safe) { - pr_err("CPU %u not online\n", cpu); + if (topology_is_primary_thread(cpu) || !allow_smt_offline) { + pr_err("CPU %u not online, loading aborted\n", cpu); return false; } + cpumask_set_cpu(cpu, &cpu_offline_mask); + per_cpu(ucode_ctrl, cpu) = ctrl; + continue; } /* --- a/arch/x86/kernel/cpu/microcode/internal.h +++ b/arch/x86/kernel/cpu/microcode/internal.h @@ -17,6 +17,7 @@ enum ucode_state { UCODE_NFOUND, UCODE_ERROR, UCODE_TIMEOUT, + UCODE_OFFLINE, }; struct microcode_ops { --- a/arch/x86/kernel/nmi.c +++ b/arch/x86/kernel/nmi.c @@ -502,8 +502,11 @@ DEFINE_IDTENTRY_RAW(exc_nmi) if (IS_ENABLED(CONFIG_NMI_CHECK_CPU)) raw_atomic_long_inc(&nsp->idt_calls); - if (IS_ENABLED(CONFIG_SMP) && arch_cpu_is_offline(smp_processor_id())) + if (IS_ENABLED(CONFIG_SMP) && arch_cpu_is_offline(smp_processor_id())) { + if (microcode_nmi_handler_enabled()) + microcode_offline_nmi_handler(); return; + } if (this_cpu_read(nmi_state) != NMI_NOT_RUNNING) { this_cpu_write(nmi_state, NMI_LATCHED); From patchwork Tue Sep 12 07:58:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138015 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp251481vqx; Tue, 12 Sep 2023 01:13:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG2Yos7C5Nt7gUjq74KdiGVJopC99Mj+FsLLD9wER1hCXqRB4TN+FrrReqFi44jg9Ax0Ez4 X-Received: by 2002:a05:6a20:3ca1:b0:122:10f9:f635 with SMTP id b33-20020a056a203ca100b0012210f9f635mr12625431pzj.19.1694506382514; Tue, 12 Sep 2023 01:13:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694506382; cv=none; d=google.com; s=arc-20160816; b=tEgD6syp7Bnpj9Y7ljLIhZpgh88KNhbdqNt7huXM+bVRt8VTfNWiwf5oLIZA0WWpBG zGf2hKwTk+r7Pcvx1W2qW2f0fYDRyufVYl01Pz/8zePsCj4+fkcII89K7BOc/5frxOVd /r5X1p0BiRppDWXlJ4LmzHr9VpcQRjn1XCMT+ay71D2uN3gkwyVCkMpUUm67obxyUtR3 mXVpjyXuQCCkN5xR29+IEbiwgThuq5Q8NbjgnChqTE1uzmmOyvcfEpgQ0oqoFUJ+h2ki yGj2+vd/nO0XrZPEV8kxQ6xeK8lDcA/ub2XP4biCQfZJvZ7CP9kRfjavGzXx79hnQaOy CI4g== 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=VdESLJN1UDe6KHq+3VNdkhAeyWMXP2vTFS4VMSOD8jo=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=bEZEYHqhgFMwclZ1vwoERXx6+QTU4s5K6yr+9a9m3X3eEOTGyLYgaI7Pak86HB79Nx bra78y+GhU2pWQ1ObIJ4V5bvWBpXIyM+WDGu9pZT50z4qrtJLa26lIuyyrkuv4Gvj5eM xKG3i0Za6wmCFxQf8fOwFWyq7m08o+KeleGsiqWPWebjp1JaC/OVgh2O/CHKG9Dgl5ZQ bLeX6usNuCMjDHtJejUmRyJyailUKXC8+LHt0Jk/FAXRvlzkSklBQJIpmmCvNILwsy9H 9a2eyVo9/b+fHR1ySTamhWwZJkg6RuPcPWShAAUk8+RZKI1naG8/JrkOPzqvbVToEdKK BfDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=YaLzRKQK; 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::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id c11-20020a056a00248b00b0068a52819fd2si7858638pfv.331.2023.09.12.01.13.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 01:13:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=YaLzRKQK; 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::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 561B981167A6; Tue, 12 Sep 2023 01:01:48 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232046AbjILIBh (ORCPT + 39 others); Tue, 12 Sep 2023 04:01:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49950 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231876AbjILIAG (ORCPT ); Tue, 12 Sep 2023 04:00:06 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20EC52D58 for ; Tue, 12 Sep 2023 00:58:32 -0700 (PDT) Message-ID: <20230912065502.568284573@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505510; 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=VdESLJN1UDe6KHq+3VNdkhAeyWMXP2vTFS4VMSOD8jo=; b=YaLzRKQK8+LTZpmgX3paV0GM/G1hHhtrG9N3KG8YaaNQs2KNWHVQE+0wEvHkfIi8lsAIEt x5Np+s61VMdQ5TxNy2GoqmWXM+lhEFTOKnID5KvJvHDLIVoH4SP9pXeXJl0nrAwjSWKpO+ cLjt8FU1BqcouG9gA5P5mxh7ofubs3JJ+sSVcLoLduyoCfEpPT7iXbyfgqG/tvChRy2TSS OtS4uxcz76tvaisuCHtMYFqM8PN6i9dWMvVpAtl57CWIj16PbZKHT0j8g5HwVGPNVH4krF 2QZVCYZRPykScAoZTHKk/MjjJbD00/5Qs7mjynKO3OUSY84+Sv6lALwYnrQxcQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505510; 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=VdESLJN1UDe6KHq+3VNdkhAeyWMXP2vTFS4VMSOD8jo=; b=Cosagv9a1arZfw8X5ur0v7Q3gMaEvrlXcCDIrn4psoiXiQrJ+Z9CGOOJ+CZhZnEB4WKfzv 0SW5s+hrTDgba4Bw== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V3 29/30] x86/microcode: Prepare for minimal revision check References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:58:30 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 12 Sep 2023 01:01:48 -0700 (PDT) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776818724546019980 X-GMAIL-MSGID: 1776818724546019980 From: Thomas Gleixner Applying microcode late can be fatal for the running kernel when the update changes functionality which is in use already in a non-compatible way, e.g. by removing a CPUID bit. There is no way for admins which do not have access to the vendors deep technical support to decide whether late loading of such a microcode is safe or not. Intel has added a new field to the microcode header which tells the minimal microcode revision which is required to be active in the CPU in order to be safe. Provide infrastructure for handling this in the core code and a command line switch which allows to enforce it. If the update is considered safe the kernel is not tainted and the annoying warning message not emitted. If it's enforced and the currently loaded microcode revision is not safe for late loading then the load is aborted. Signed-off-by: Thomas Gleixner --- Documentation/admin-guide/kernel-parameters.txt | 5 ++++ arch/x86/Kconfig | 23 ++++++++++++++++++- arch/x86/kernel/cpu/microcode/amd.c | 3 ++ arch/x86/kernel/cpu/microcode/core.c | 29 ++++++++++++++++++------ arch/x86/kernel/cpu/microcode/intel.c | 3 ++ arch/x86/kernel/cpu/microcode/internal.h | 3 ++ 6 files changed, 58 insertions(+), 8 deletions(-) --- --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -3275,6 +3275,11 @@ mga= [HW,DRM] + microcode.force_minrev= [X86] + Format: + Enable or disable the microcode minimal revision + enforcement for the runtime microcode loader. + min_addr=nn[KMG] [KNL,BOOT,IA-64] All physical memory below this physical address is ignored. --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -1322,7 +1322,28 @@ config MICROCODE_LATE_LOADING is a tricky business and should be avoided if possible. Just the sequence of synchronizing all cores and SMT threads is one fragile dance which does not guarantee that cores might not softlock after the loading. Therefore, - use this at your own risk. Late loading taints the kernel too. + use this at your own risk. Late loading taints the kernel unless the + microcode header indicates that it is safe for late loading via the + minimal revision check. This minimal revision check can be enforced on + the kernel command line with "microcode.minrev=Y". + +config MICROCODE_LATE_FORCE_MINREV + bool "Enforce late microcode loading minimal revision check" + default n + depends on MICROCODE_LATE_LOADING + help + To prevent that users load microcode late which modifies already + in use features, newer microcodes have a minimum revision field + in the microcode header, which tells the kernel which minimum + revision must be active in the CPU to safely load that new microcode + late into the running system. If disabled the check will not + be enforced but the kernel will be tainted when the minimal + revision check fails. + + This minimal revision check can also be controlled via the + "microcode.minrev" parameter on the kernel command line. + + If unsure say Y. config X86_MSR tristate "/dev/cpu/*/msr - Model-specific register support" --- a/arch/x86/kernel/cpu/microcode/amd.c +++ b/arch/x86/kernel/cpu/microcode/amd.c @@ -900,6 +900,9 @@ static enum ucode_state request_microcod enum ucode_state ret = UCODE_NFOUND; const struct firmware *fw; + if (force_minrev) + return UCODE_NFOUND; + if (c->x86 >= 0x15) snprintf(fw_name, sizeof(fw_name), "amd-ucode/microcode_amd_fam%.2xh.bin", c->x86); --- a/arch/x86/kernel/cpu/microcode/core.c +++ b/arch/x86/kernel/cpu/microcode/core.c @@ -46,6 +46,9 @@ static struct microcode_ops *microcode_ops; static bool dis_ucode_ldr = true; +bool force_minrev = IS_ENABLED(CONFIG_MICROCODE_LATE_FORCE_MINREV); +module_param(force_minrev, bool, S_IRUSR | S_IWUSR); + bool initrd_gone; /* @@ -558,15 +561,17 @@ static int ucode_load_cpus_stopped(void return 0; } -static int ucode_load_late_stop_cpus(void) +static int ucode_load_late_stop_cpus(bool is_safe) { unsigned int cpu, updated = 0, failed = 0, timedout = 0, siblings = 0; unsigned int nr_offl, offline = 0; int old_rev = boot_cpu_data.microcode; struct cpuinfo_x86 prev_info; - pr_err("Attempting late microcode loading - it is dangerous and taints the kernel.\n"); - pr_err("You should switch to early loading, if possible.\n"); + if (!is_safe) { + pr_err("Late microcode loading without minimal revision check.\n"); + pr_err("You should switch to early loading, if possible.\n"); + } atomic_set(&late_cpus_in, num_online_cpus()); atomic_set(&offline_in_nmi, 0); @@ -616,7 +621,9 @@ static int ucode_load_late_stop_cpus(voi return -EIO; } - add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK); + if (!is_safe || failed || timedout) + add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK); + pr_info("Microcode load: updated on %u primary CPUs with %u siblings\n", updated, siblings); if (failed || timedout) { pr_err("Microcode load incomplete. %u CPUs timed out or failed\n", @@ -710,9 +717,17 @@ static int ucode_load_late_locked(void) return -EBUSY; ret = microcode_ops->request_microcode_fw(0, µcode_pdev->dev); - if (ret != UCODE_NEW) - return ret == UCODE_NFOUND ? -ENOENT : -EBADFD; - return ucode_load_late_stop_cpus(); + + switch (ret) { + case UCODE_NEW: + case UCODE_NEW_SAFE: + break; + case UCODE_NFOUND: + return -ENOENT; + default: + return -EBADFD; + } + return ucode_load_late_stop_cpus(ret == UCODE_NEW_SAFE); } static ssize_t reload_store(struct device *dev, --- a/arch/x86/kernel/cpu/microcode/intel.c +++ b/arch/x86/kernel/cpu/microcode/intel.c @@ -470,6 +470,9 @@ static enum ucode_state read_ucode_intel unsigned int curr_mc_size = 0; u8 *new_mc = NULL, *mc = NULL; + if (force_minrev) + return UCODE_NFOUND; + while (iov_iter_count(iter)) { struct microcode_header_intel mc_header; unsigned int mc_size, data_size; --- a/arch/x86/kernel/cpu/microcode/internal.h +++ b/arch/x86/kernel/cpu/microcode/internal.h @@ -13,6 +13,7 @@ struct device; enum ucode_state { UCODE_OK = 0, UCODE_NEW, + UCODE_NEW_SAFE, UCODE_UPDATED, UCODE_NFOUND, UCODE_ERROR, @@ -36,6 +37,8 @@ struct microcode_ops { use_nmi : 1; }; +extern bool force_minrev; + extern struct ucode_cpu_info ucode_cpu_info[]; struct cpio_data find_microcode_in_initrd(const char *path); From patchwork Tue Sep 12 07:58:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 138180 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp406261vqx; Tue, 12 Sep 2023 06:26:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE49fjM9gU2IMBRHxbihq1ZG//HGdS66CCR7Axksh3FdkZyjxNtYiSyg3RtT7mDpw7I/Eja X-Received: by 2002:a17:90a:17a8:b0:26d:ae3:f6a6 with SMTP id q37-20020a17090a17a800b0026d0ae3f6a6mr9418932pja.18.1694525207092; Tue, 12 Sep 2023 06:26:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694525207; cv=none; d=google.com; s=arc-20160816; b=FvXN8m62/5RxpIeD7/R4uuv+VpOwKrCzLeKQHyVcuAzJkHfUUliWbwSvs3V51pXzGI KwSUZQ0dChYERhM5sT1ABGKvM/K5mvgJftHpiFbE9St3rXOQfVXxBt7Jsyx1j9UiaIZX kg5oJB0mYUnRMNt69An8mh+wFpsiievVgtvmizJTXCXpYidjCbz+omtwV5k8Uyp57dMp FCXPftg6XEYezqbflHXOM0sOnlj3pHa50lyHJisEIHdaB0mdEBKWCiYi4HKQhAxSYUbY 62robVMhyx4IhEH8qsh/x8pcLFxaYWfLrKMcRKdnDLYwGbkcPmAxdP09bh27nZMKcV1H JMCQ== 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=wqtyBr0f+DByo/zgMoM7oUe4I3Cn1ru+tCLK7XgMuJ0=; fh=Fw4dspu3lEWS7xf2bQx5+rNTgFONiGLilI8487/7QuM=; b=I0XfeYEaWnCge1juCvLwrDQKJaSwvTSGQ7Y6LRwsUfh2vW9IRbmKiA5PKdHlWXNoS2 rvRrDZ13Lle2svn/JsorWga2ZIHCaSG63qHovOvPT7avODkoGaqce1Eznx2rmQ/vxbdL PoC6SXZSSCLT+a+skhj176Ms1PoYxWpr/G46KPgxv7zZCF0cmTJeVtPn5wdUaRD6BnHT 8rU39x9qERRyJy4CjBqA4lgmZMWuo7YKGXgnI7jX5wGUsensupDKkNR7am7DApyx6YHT +kQbPO65kkzAkRuiaLys0ctVTLMyZ/D+5wQzmh43Lf5O16CU/Ifgd3U4CJWC5vX0RB6a ok/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=SrGwwF6+; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=8GNJ3IE+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id v12-20020a17090ad58c00b002681895d0d9si9450913pju.120.2023.09.12.06.26.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 06:26:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=SrGwwF6+; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=8GNJ3IE+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id DD7E581167B4; Tue, 12 Sep 2023 01:01:59 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232109AbjILIBn (ORCPT + 39 others); Tue, 12 Sep 2023 04:01:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54274 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232176AbjILIAX (ORCPT ); Tue, 12 Sep 2023 04:00:23 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 873C62D5F for ; Tue, 12 Sep 2023 00:58:33 -0700 (PDT) Message-ID: <20230912065502.631634574@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694505512; 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=wqtyBr0f+DByo/zgMoM7oUe4I3Cn1ru+tCLK7XgMuJ0=; b=SrGwwF6+u+lo69AJGhecm1RM+RQhIyd+dUpFL09tQZTcb/y+xqbDxUY8x4B4er/x4Dod7m /0hKATF5R+fHLtvWNQGsLSeGqNR7Pi68sizjhOoTXrkhfElkJkG3PcBfAB2bk6DkNAGEse K5qVdRw30oZ/RWAnA1BbUaDluS/m8soROZas5OlQW7kuh1iMAkaLYBIMjkjvjzRQtsm0es VUUzv+7/eOat+CfzaWnDkm0t61b6oPW249WXZCoDjHjXV8TTkF074dxblefNHa9PIIRCRM 47/CzMWDsbiKg9DG8TPDLtNQMJd5A2djfBtk3UM5wO22MYEJx+MDNmb5IKKADA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694505512; 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=wqtyBr0f+DByo/zgMoM7oUe4I3Cn1ru+tCLK7XgMuJ0=; b=8GNJ3IE+gdmxufx9Yv2Bcb1HvLlevctl7bxX99Z7nB/YvjEK4c7sufulmHrUFG0AXI7nx4 9h3LwGAq0ezMTlAA== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov , Ashok Raj Subject: [patch V3 30/30] x86/microcode/intel: Add a minimum required revision for late-loads References: <20230912065249.695681286@linutronix.de> MIME-Version: 1.0 Date: Tue, 12 Sep 2023 09:58:31 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 12 Sep 2023 01:01:59 -0700 (PDT) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776838463785565800 X-GMAIL-MSGID: 1776838463785565800 From: Ashok Raj In general users don't have the necessary information to determine whether late loading of a new microcode version is safe and does not modify anything which the currently running kernel uses already, e.g. removal of CPUID bits or behavioural changes of MSRs. To address this issue, Intel has added a "minimum required version" field to a previously reserved field in the microcode header. Microcode updates should only be applied if the current microcode version is equal to, or greater than this minimum required version. Thomas made some suggestions on how meta-data in the microcode file could provide Linux with information to decide if the new microcode is suitable candidate for late loading. But even the "simpler" option requires a lot of metadata and corresponding kernel code to parse it, so the final suggestion was to add the 'minimum required version' field in the header. When microcode changes visible features, microcode will set the minimum required version to its own revision which prevents late loading. Old microcode blobs have the minimum revision field always set to 0, which indicates that there is no information and the kernel considers it as unsafe. This is a pure OS software mechanism. The hardware/firmware ignores this header field. For early loading there is no restriction because OS visible features are enumerated after the early load and therefor a change has no effect. The check is always enabled, but by default not enforced. It can be enforced via Kconfig or kernel command line. If enforced, the kernel refuses to late load microcode with a minium required version field which is zero or when the currently loaded microcode revision is smaller than the minimum required revision. If not enforced the load happens independent of the revision check to stay compatible with the existing behaviour, but it influences the decision whether the kernel is tainted or not. If the check signals that the late load is safe, then the kernel is not tainted. Early loading is not affected by this. [ tglx: Massaged changelog and fixed up the implementation ] Suggested-by: Thomas Gleixner Signed-off-by: Ashok Raj Signed-off-by: Thomas Gleixner --- arch/x86/include/asm/microcode.h | 3 +- arch/x86/kernel/cpu/microcode/intel.c | 37 ++++++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 5 deletions(-) --- --- a/arch/x86/include/asm/microcode.h +++ b/arch/x86/include/asm/microcode.h @@ -36,7 +36,8 @@ struct microcode_header_intel { unsigned int datasize; unsigned int totalsize; unsigned int metasize; - unsigned int reserved[2]; + unsigned int min_req_ver; + unsigned int reserved; }; struct microcode_intel { --- a/arch/x86/kernel/cpu/microcode/intel.c +++ b/arch/x86/kernel/cpu/microcode/intel.c @@ -463,16 +463,40 @@ static enum ucode_state apply_microcode_ return ret; } +static bool ucode_validate_minrev(struct microcode_header_intel *mc_header) +{ + int cur_rev = boot_cpu_data.microcode; + + /* + * When late-loading, ensure the header declares a minimum revision + * required to perform a late-load. The previously reserved field + * is 0 in older microcode blobs. + */ + if (!mc_header->min_req_ver) { + pr_info("Unsafe microcode update: Microcode header does not specify a required min version\n"); + return false; + } + + /* + * Check whether the minimum revision specified in the header is either + * greater or equal to the current revision. + */ + if (cur_rev < mc_header->min_req_ver) { + pr_info("Unsafe microcode update: Current revision 0x%x too old\n", cur_rev); + pr_info("Current should be at 0x%x or higher. Use early loading instead\n", mc_header->min_req_ver); + return false; + } + return true; +} + static enum ucode_state read_ucode_intel(int cpu, struct iov_iter *iter) { struct ucode_cpu_info *uci = ucode_cpu_info + cpu; + bool is_safe, new_is_safe = false; int cur_rev = uci->cpu_sig.rev; unsigned int curr_mc_size = 0; u8 *new_mc = NULL, *mc = NULL; - if (force_minrev) - return UCODE_NFOUND; - while (iov_iter_count(iter)) { struct microcode_header_intel mc_header; unsigned int mc_size, data_size; @@ -515,9 +539,14 @@ static enum ucode_state read_ucode_intel if (!intel_find_matching_signature(mc, &uci->cpu_sig)) continue; + is_safe = ucode_validate_minrev(&mc_header); + if (force_minrev && !is_safe) + continue; + kvfree(new_mc); cur_rev = mc_header.rev; new_mc = mc; + new_is_safe = is_safe; mc = NULL; } @@ -529,7 +558,7 @@ static enum ucode_state read_ucode_intel return UCODE_NFOUND; ucode_patch_late = (struct microcode_intel *)new_mc; - return UCODE_NEW; + return new_is_safe ? UCODE_NEW_SAFE : UCODE_NEW; fail: kvfree(mc);