Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp1884128rda; Tue, 24 Oct 2023 06:23:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHEoCIAS5YLk42tGTS89Mxy5dzA+JxqV0m04upnB4/xliqHbKxzCgq4ObmteLEk6eLgHTC7 X-Received: by 2002:a17:902:e5c5:b0:1c4:56c8:27f8 with SMTP id u5-20020a170902e5c500b001c456c827f8mr8048963plf.68.1698153831839; Tue, 24 Oct 2023 06:23:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698153831; cv=none; d=google.com; s=arc-20160816; b=DT5uuaM5texAIN9F+dM16gUWgloesU4OuRLcGqNWGjs0fkBmXX9+Cce+IX+frGRQ7i h2oiUswMp9+lLfW5XQ/Rv2uTnrC4fg6DtSqI9NLpLFiBKCvu6BMfW4jBYSvnQi1WO4S2 g1zcnniU0E3Uo9yDhra8IocmKihQT4KMjzwsWeM4TIqzeY0f6EP8aEr7qv+Z9V3v9+uI zBtheeLWmuvUDu/lPfNuoHFgVS9a2O7qx+S+cRBCW3582i0OI1GAmRj/6+ZXn0qY2llz yQp5uuqFk9cmgJrHZ40cwxa+v13q0m29n5k7O9w8eGfFLUs1FCCdEdw09IVbQghYxj1f o5gQ== 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=6nqIL7Iij6I91YwjLhm5Atb9zwAdORcuJ6MTRIlljPo=; fh=LWlv3U+xZ3+vQt4tlJRIlKl5VzN7iFjjE/bzRmlxKDA=; b=OTnPpBHat0gztyjSajmm+/7MtI3b+exSc1itJZElxljy8znfVPXW1uFTYPNmSt2LZP N96u7w8emrFTu+9Lso+DEfP1UorsmFip531M3h07yXMOO/d2ByZqrz/8702otFKxa+G0 jm69F5MycC/CDT8rNjUHezXyuzsoraeaTDshgRD1ugCvky91MgvgkACq13Z605ZNQ1R8 Dqsq0jTCcRgeezSQaCC0C0M/Q/apg9Iyf0bIr+RJg48bEJZbgA8Cpyr+M7z/FY3Dszoy tl5XKnCw71FpvtzRf4JlztOyvygX7WAlbjnV6tKLhifF5YMwNboE3FLraypt40p/T3xq c7qA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=JiLr3irN; 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 Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id a10-20020a170902ee8a00b001b82a4d3ea7si7988765pld.249.2023.10.24.06.23.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 06:23:51 -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=JiLr3irN; 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 EF38680B8154; Tue, 24 Oct 2023 06:23:42 -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 S1343546AbjJXNWc (ORCPT + 99 others); Tue, 24 Oct 2023 09:22:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234604AbjJXNVT (ORCPT ); Tue, 24 Oct 2023 09:21:19 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54AE210F2; Tue, 24 Oct 2023 06:20:58 -0700 (PDT) Date: Tue, 24 Oct 2023 13:20:55 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1698153656; 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=6nqIL7Iij6I91YwjLhm5Atb9zwAdORcuJ6MTRIlljPo=; b=JiLr3irN9KQhUedxbCVsmgxMVQI+2vR1Qa32ZDyGv9O7oRGQ25amTqDVw/8FbGDdVTdzfz I1VgheOnM6pU5vh/uXI5/Fbia1jd2PqR+QeolxEt1lJJIaZuhf4AfgL03uxFT9hykEs8Ed yQ49NLZQujfyhgOm6DiJG+eAocR//yU4Zhe0svtcKHw4SbSqURDBORCLK6NJvNPHggAgQ2 vMVfBMNwIgIPXr82p9EAY0bwuJ79UuPsIpcqTZJ9RbyGghxM0FpxCO64qf2rk9mjWioxKH 0uon1YhvV6ngAG6P49/KIvu8ouHgNNxJ1uzDqGJSDXwP0G6HMcKINsrcvjewyQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1698153656; 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=6nqIL7Iij6I91YwjLhm5Atb9zwAdORcuJ6MTRIlljPo=; b=qgllPvVzNuIuWXlUXhFzweYxTa2RboVnHyq1ftCvxFtpzmalPKUimqXNMHAKpkVZtUVYyJ EZb4UZwVIHvY/iBA== 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: <169815365590.3135.705159316165017003.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit 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, 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 agentk.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 (agentk.vger.email [0.0.0.0]); Tue, 24 Oct 2023 06:23:43 -0700 (PDT) The following commit has been merged into the x86/microcode branch of tip: Commit-ID: 0772b9aa1a8f7322dce8588c231cff8b57298a53 Gitweb: https://git.kernel.org/tip/0772b9aa1a8f7322dce8588c231cff8b57298a53 Author: Thomas Gleixner AuthorDate: Mon, 02 Oct 2023 13:59:59 +02:00 Committer: Borislav Petkov (AMD) CommitterDate: Tue, 24 Oct 2023 15:05:54 +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 63c4e12..9fd5a96 100644 --- a/arch/x86/kernel/cpu/microcode/core.c +++ b/arch/x86/kernel/cpu/microcode/core.c @@ -252,31 +252,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; } /* @@ -294,7 +289,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; /* @@ -317,7 +312,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"); /* @@ -344,8 +339,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