From patchwork Fri Oct 20 11:37:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 156062 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2010:b0:403:3b70:6f57 with SMTP id fe16csp992515vqb; Fri, 20 Oct 2023 04:42:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF81upQE4m5fI8DPbEyebK6OliIU7ZvxmGxcDwococF989szELT+yVwSnXC01EBHqlS0niM X-Received: by 2002:a17:902:e1d2:b0:1ca:12f3:6775 with SMTP id t18-20020a170902e1d200b001ca12f36775mr1130643pla.33.1697802146638; Fri, 20 Oct 2023 04:42:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697802146; cv=none; d=google.com; s=arc-20160816; b=DyxYTl8w68YNwqgaBo7fYaifir5COOelxnhCQwH2cyUhoH26K/NKbOFqWUKAq4VtRr oTg8wpnl9D8ZLp3lEy9dFQwmqtUH/cj8n5cafLM82+F8WiMO2yxure2F9X1sklBWH0wp AglayTjQF1kaBKQ1cYZt6Rhw8VJ5XEJiikng1OPpPfwPSJSwAPEOTo++0B4ExlLLUR8k Lc4qBzIlzLhVLX+puQc+Ry48hsiWQ4XA5rVU0DNAs4pvaUCo8MsUYWOlnvdXG9xMb6K5 qwraNQpgRkG8g3Q+ObMPUbI/pIa5NLVD2B8CeAx/9eEd4tKy1b9YgyZJzJ6dFKFgNKQv ImIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=iA+XHiJye0DRxD55xZH/30ty9G1j4LqGF9CApSDLFTc=; fh=21oOLqZrsdqszPj8OgnjfQavEO8hJ4yT0sqEMQ3fADg=; b=Ny4D+tCGklBCkSw+qruE875NDKVa+beH9rZ7Oq2FAztTn4PYz6nqhDphG5wNROJ9zx Gpf2ZV063VrK6o1ZQu40QwjOfo3wgcGLfnKD0Ml/ehoYdCVMvjk2tSycoteclQoEFxN8 RIrLuIIeY2HNoEGHP80kaZGT0IHOIXKYDjn29ZGFuIwBfRHdTev8CVPvxYIeRfFE6rLV kqdbHXt6plWqH+A4fRQxsHySoOOzZu5JDFM4c7KLMm/sGpGe61zziulB9POMGtOWtkhP t9tEhzNFfEmol/5q6tS+SaS6/kfp0MwNtB+DyyU2VBUT4jxDXU2ct3li4j8YpaiVrZ4n h8mw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=H5Sz6JTL; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=owSjlb4e; 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 k2-20020a170902c40200b001b674055d72si1682459plk.621.2023.10.20.04.42.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 04:42:26 -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=H5Sz6JTL; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=owSjlb4e; 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 7614783014A7; Fri, 20 Oct 2023 04:42:18 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377522AbjJTLmB (ORCPT + 25 others); Fri, 20 Oct 2023 07:42:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44470 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377396AbjJTLld (ORCPT ); Fri, 20 Oct 2023 07:41:33 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61A2A2D4B; Fri, 20 Oct 2023 04:38:30 -0700 (PDT) Date: Fri, 20 Oct 2023 11:37:58 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1697801879; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iA+XHiJye0DRxD55xZH/30ty9G1j4LqGF9CApSDLFTc=; b=H5Sz6JTLvNsN3ZRbXhpublXB3QbeH8FRGqGZsiR+ZZJ+KxmupX6GwwRt46o0z5lGgrt2Pg 6qWpdnkO8EISFsfwYfCeecS3ALfwiyF8ba+6R4NeYzEy0foiIt8B3s6s2wJVA5P6rpMBJu C8Bm+XnO/C+ACZ6orKKGFOX+djkPRW3I6uLmP0GGiyMI1hpw1SfAOPgAh1/TiXkdJSV1CE QF5vjvzcafHF7zEddyOzmmOtew67iKQXknkekrT0ZJxo7FmMz3ePxbVnMJSEj1v3+QbNGn 4kXWET6gSGG7oDPorU60q54unieRzCtQfjTu4oCPYotOY8HpWdvUGiYLb20gDQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1697801879; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iA+XHiJye0DRxD55xZH/30ty9G1j4LqGF9CApSDLFTc=; b=owSjlb4e6zVDi8udX3oEEZIOfHfPiWkQq+PKrXoJgtZUKAJ2WyLmKFVBMFELei00Vs4Uu1 78KRF5G93RtDkjBw== From: "tip-bot2 for Thomas Gleixner" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/microcode] x86/microcode/intel: Unify microcode apply() functions Cc: "Borislav Petkov (AMD)" , Thomas Gleixner , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20231017211722.795508212@linutronix.de> References: <20231017211722.795508212@linutronix.de> MIME-Version: 1.0 Message-ID: <169780187847.3135.11328314690661960193.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=1.1 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=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email 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]); Fri, 20 Oct 2023 04:42:18 -0700 (PDT) X-Spam-Level: * X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780039430774148202 X-GMAIL-MSGID: 1780274583761675526 The following commit has been merged into the x86/microcode branch of tip: Commit-ID: ce0df92ad67133a77f9c0ab25f41ae28f112666e Gitweb: https://git.kernel.org/tip/ce0df92ad67133a77f9c0ab25f41ae28f112666e Author: Thomas Gleixner AuthorDate: Tue, 17 Oct 2023 23:23:44 +02:00 Committer: Borislav Petkov (AMD) CommitterDate: Thu, 19 Oct 2023 14:24:25 +02:00 x86/microcode/intel: Unify microcode apply() functions Deduplicate the early and late apply() functions. [ bp: Rename the function which does the actual application to __apply_microcode() to differentiate it from microcode_ops.apply_microcode(). ] Signed-off-by: Borislav Petkov (AMD) Signed-off-by: Thomas Gleixner Link: https://lore.kernel.org/r/20231017211722.795508212@linutronix.de --- arch/x86/kernel/cpu/microcode/intel.c | 104 ++++++++----------------- 1 file changed, 36 insertions(+), 68 deletions(-) diff --git a/arch/x86/kernel/cpu/microcode/intel.c b/arch/x86/kernel/cpu/microcode/intel.c index 304a7a9..d3198de 100644 --- a/arch/x86/kernel/cpu/microcode/intel.c +++ b/arch/x86/kernel/cpu/microcode/intel.c @@ -294,12 +294,12 @@ static __init struct microcode_intel *scan_microcode(void *data, size_t size, return size ? NULL : patch; } -static enum ucode_state apply_microcode_early(struct ucode_cpu_info *uci) +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 +308,12 @@ static enum ucode_state apply_microcode_early(struct ucode_cpu_info *uci) * 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 +328,24 @@ static enum ucode_state apply_microcode_early(struct ucode_cpu_info *uci) 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) +{ + 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; @@ -443,70 +452,29 @@ static int collect_cpu_info(int cpu_num, struct cpu_signature *csig) 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; + u32 cur_rev; - /* We should bind the task to the CPU */ - if (WARN_ON(raw_smp_processor_id() != cpu)) + if (WARN_ON_ONCE(smp_processor_id() != cpu)) return UCODE_ERROR; - mc = ucode_patch_late; - if (!mc) - return UCODE_NFOUND; + ret = __apply_microcode(uci, mc, &cur_rev); + if (ret != UCODE_UPDATED && ret != UCODE_OK) + return ret; - /* - * 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); - 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, + 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; } @@ -647,7 +615,7 @@ static void finalize_late_load(int result) 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, };