From patchwork Mon Oct 9 12:29:47 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: 150055 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a888:0:b0:403:3b70:6f57 with SMTP id x8csp1833790vqo; Mon, 9 Oct 2023 05:32:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE2qQBYUg+JLrFOA+6DS8aah1A3zqEhRCmTugHJO375Jkx9Ya5CQbSwCPcNSsqXymKAEosg X-Received: by 2002:a17:90b:38b:b0:274:9121:382 with SMTP id ga11-20020a17090b038b00b0027491210382mr16698198pjb.22.1696854743285; Mon, 09 Oct 2023 05:32:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696854743; cv=none; d=google.com; s=arc-20160816; b=afy03wXBoplTrVIfXr1illM1eVQESuH6w72145CLRE2NJQwWZLs0QlW73VPEkeEh3K ZjRq0D8Gs97PzhGnBHkKQi1Rbgw2Q8DYgYggwNya0Fp6KKxPycz06EX4h4FLZjZlmQkS q6uWkPoJhTzRAbH9tAGwGdacb+mHAs28RCxRBZ9sH5rAJUAfNS3a6hQaxUFxlFKLmmDC tuyi0DL7JZc1tL26ks/hRYGbOQU9XK4PbIL0rFQrJtB+a2TnKW9nENHdi/Z7PWHjGgTm 5taYul0tTGkD856Q3CWDfShBvDM9tJeMUdeePlRfhlDgMZznRrXHVWvXU2bPV3pibgif x80Q== 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=3aEE3XOrTmiV23yEt0enup40FBfSYVb/ErZkvbAaV3k=; fh=LWlv3U+xZ3+vQt4tlJRIlKl5VzN7iFjjE/bzRmlxKDA=; b=pmbK7kgpDqjgp7yFkLfLMPA1qX/B7SnBITwmslVsssk3zhDLfjc0VIXwFB34cEydBg K7VfvyVC1my5bnOpDHba5Q1rrhGG04AQGgWhOzjp1MzMWtHMvEy49JfHkn62L20RMdPx m0QRqtUdJRnQF/y5LlQvml1qMHMUJjlLhl6/ieDIQmpEO6gX2ivHbpPYhsWO4GPiDoYO 4QcDphZTjg3NmtacGXng1q+7GdKLq11aDnojGX5oWfVW/XyARZ5XDaFmOHZqUnoJF/x7 EIEQxioM4pMDKOntnK9Lqn92SDnT3dRbgfHrfqD4rNme2V0cQDzH8Q2FX/KDLOPS4JFy o/+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=BSNKuDc2; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=duH8hJbO; 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 n7-20020a17090a9f0700b00276f10d241fsi11706381pjp.37.2023.10.09.05.32.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 05:32:23 -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=BSNKuDc2; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=duH8hJbO; 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 205B2805ECDD; Mon, 9 Oct 2023 05:30:32 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376570AbjJIMaX (ORCPT + 18 others); Mon, 9 Oct 2023 08:30:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346563AbjJIM3z (ORCPT ); Mon, 9 Oct 2023 08:29: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 3C736B9; Mon, 9 Oct 2023 05:29:51 -0700 (PDT) Date: Mon, 09 Oct 2023 12:29:47 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1696854588; 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=3aEE3XOrTmiV23yEt0enup40FBfSYVb/ErZkvbAaV3k=; b=BSNKuDc2sCIhsV12ghSrUr5yUzLDJYTJSrO2qOfxSvC2u4gVsjKtO5cf8EBMKtNEmrfYM3 7OliJ/RMKseHJw8uXWAT3DS7sG4B6nY1uX+48BPthoqrQvfHHbdVlOvI+eNyRg8FMLFQld XQIvZJuYtJzK6OtoCx25ctD1cS3sLtsedMapCDqdTgOZZQ1abAprBSIW5euVVsf6z9+0I+ 7RCVXGQePWe5H9igKcI90RcxBCM3ZoWXt1EhdRtr4JLBjS8shNyOf30F4MpVWR46k4t1Mi OfW5/Kq8pnyU7jNBDQ/GerDlFPq0njaRl5nUQ2Bb4W3z7ZHH+yy3xfY9ahfWuw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1696854588; 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=3aEE3XOrTmiV23yEt0enup40FBfSYVb/ErZkvbAaV3k=; b=duH8hJbO0vPHoP0ZBJyXMqBObUOG1wGygx/xAAsUEYsz1QppkbqPSXt5mBJwJFMpUrF1kW Rp/MqxuLMgRSqhAw== 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: Sanitize __wait_for_cpus() Cc: Thomas Gleixner , "Borislav Petkov (AMD)" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20231002115903.204251527@linutronix.de> References: <20231002115903.204251527@linutronix.de> MIME-Version: 1.0 Message-ID: <169685458791.3135.18135037272376022845.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 09 Oct 2023 05:30:32 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778682604871258347 X-GMAIL-MSGID: 1779281159547293264 The following commit has been merged into the x86/microcode branch of tip: Commit-ID: 89d6d026e43bd8b093f6238b569a0efc9485d19f Gitweb: https://git.kernel.org/tip/89d6d026e43bd8b093f6238b569a0efc9485d19f Author: Thomas Gleixner AuthorDate: Mon, 02 Oct 2023 13:59:59 +02:00 Committer: Borislav Petkov (AMD) CommitterDate: Fri, 06 Oct 2023 15:12:23 +02:00 x86/microcode: Sanitize __wait_for_cpus() 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 Signed-off-by: Borislav Petkov (AMD) Link: https://lore.kernel.org/r/20231002115903.204251527@linutronix.de --- arch/x86/kernel/cpu/microcode/core.c | 39 +++++++++++---------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/arch/x86/kernel/cpu/microcode/core.c b/arch/x86/kernel/cpu/microcode/core.c index d82d763..2b8e9b6 100644 --- a/arch/x86/kernel/cpu/microcode/core.c +++ b/arch/x86/kernel/cpu/microcode/core.c @@ -281,31 +281,26 @@ static struct platform_device *microcode_pdev; * 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(); + unsigned int timeout; - atomic_inc(t); + WARN_ON_ONCE(atomic_dec_return(cnt) < 0); - 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; - } + for (timeout = 0; timeout < USEC_PER_SEC; timeout++) { + if (!atomic_read(cnt)) + return true; - ndelay(SPINUNIT); - timeout -= SPINUNIT; + udelay(1); - touch_nmi_watchdog(); + if (!(timeout % USEC_PER_MSEC)) + touch_nmi_watchdog(); } - return 0; + /* Prevent the late comers from making progress and let them time out */ + atomic_inc(cnt); + return false; } /* @@ -323,7 +318,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; /* @@ -346,7 +341,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"); /* @@ -373,8 +368,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