From patchwork Fri Oct 20 11:37:50 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: 156060 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2010:b0:403:3b70:6f57 with SMTP id fe16csp992410vqb; Fri, 20 Oct 2023 04:42:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IELOwJcWYRO4N+xRZxlP0QX5Ywzr/EWHjNnAnuA8mWpk++g5V09oZuS5wsbftoxnmLh4sKB X-Received: by 2002:a17:903:110d:b0:1c9:ea6e:5a63 with SMTP id n13-20020a170903110d00b001c9ea6e5a63mr2007982plh.32.1697802135442; Fri, 20 Oct 2023 04:42:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697802135; cv=none; d=google.com; s=arc-20160816; b=LVkKclT2n1rIc2QN+wJHLkaKFQCjKMhrepetXEqiZZO+2BAfb9P9MFJAebK9mOr+VG R4dIkV8jod3AVlkE983w234bLHEewHsLcCYI+RYI22DiSumHI2isFkmM5CLnf9syT5gt 7ce0shISMT74tHRkWHcaP9qFG1qgz+faJv2ln1kw4oBqg/14rmDbsw1b1VIIlFONMzeP dbt5UB2pwSXqglAzsrQSeMmgcpVl4eS4lwrFzN1TyB7EOCRP2PyaeiuqSeQP/G+iEeOR hpqLlJ0VLWFlzJnmYWrJbI23qgj8oUaRNURK81Wwer1Q6CxxCWZbTqPQa2j9xVk0Ec53 UG/w== 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=SCuiTwxOEeQPdkNesnDq60kBaeq+ULJll4LvEfr8PA8=; fh=LWlv3U+xZ3+vQt4tlJRIlKl5VzN7iFjjE/bzRmlxKDA=; b=c9foWczVK36JwqDE6N7Sly/+jUukczO3aMnk25VlYkrlEV3XXT3WqKNPG/7NSLJajJ Tajx++m8OWvur++pb6GFy7veZfHGZnJBBVhVE+WcPdIzN1SARE8nyG2Kam5vZqxtKleD IOebgnO+LoRaNIGp3Gg7An3br+MSiFf+DGXzw4rCexk2DubZ/O+Yx4goOEKsvraUk/AK ppW47cgWHjjumsyaS9w07ruergCp5uktf89oJ3TwLbpBW4usYZYy7q2PJvxn9hPLhQO2 ozsCOBEiSdUumiu7m4JXXhKa6+DtuEqBxdU7kfveKL2Wg3Eu/xV6TFtFG1/xNiKfedk2 Bqpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=OHMAhqh8; dkim=neutral (no key) header.i=@linutronix.de header.b=q4Zw3g0t; 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 i8-20020a170902c94800b001bc74f6a951si1776122pla.250.2023.10.20.04.42.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 04:42:15 -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=OHMAhqh8; dkim=neutral (no key) header.i=@linutronix.de header.b=q4Zw3g0t; 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 5E61E819AC6F; Fri, 20 Oct 2023 04:41:18 -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 S1377452AbjJTLjo (ORCPT + 25 others); Fri, 20 Oct 2023 07:39:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52726 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377407AbjJTLi5 (ORCPT ); Fri, 20 Oct 2023 07:38: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 CF60B1FE6; Fri, 20 Oct 2023 04:38:07 -0700 (PDT) Date: Fri, 20 Oct 2023 11:37:50 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1697801871; 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=SCuiTwxOEeQPdkNesnDq60kBaeq+ULJll4LvEfr8PA8=; b=OHMAhqh89cCwJN3I+zgRkImtX6KAbqeh64ALIOz3IlxzfWkafhkn47jUpeJkMj2eytvTYU aImFG47Awtn859Goo1XHZEvyAyQmvcNQMLF5acnBo01OoGTX0cVqZ9/Ffo8lM+F5mns9Tr Nsko1rhzoNy7Diht3ae+robllRn4bQmClBA0Tg0cN6x6oOU+CYEZG1kpyA/Dv4znvj2BMi EBzaLtJSOUaEiaF1+o8+5H0e5yT2qhTtojFKoCGBBOtIInkjloEN0E8h08cxxVoIGj5WyY 563lJNFYpIy4RI9nXKiBGECinnFLvS9WwhCM2xSbir/4dCvjkO1AT1VzNp2T4A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1697801871; 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=SCuiTwxOEeQPdkNesnDq60kBaeq+ULJll4LvEfr8PA8=; b=q4Zw3g0tHuz7+S47ZqMb0wCgJBf37Xt1wyqm3HxkbK3LjewQDxDuIECK4mJpYif8+iSyZt rt13S+CsphLeGQAg== 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: <169780187089.3135.7641533277446562008.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails 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 lipwig.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 (lipwig.vger.email [0.0.0.0]); Fri, 20 Oct 2023 04:41:18 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778682604871258347 X-GMAIL-MSGID: 1780274572071630667 The following commit has been merged into the x86/microcode branch of tip: Commit-ID: 7f21c0ed2cfefcbc6e2b383a6001c0c48a135ffe Gitweb: https://git.kernel.org/tip/7f21c0ed2cfefcbc6e2b383a6001c0c48a135ffe Author: Thomas Gleixner AuthorDate: Mon, 02 Oct 2023 13:59:59 +02:00 Committer: Borislav Petkov (AMD) CommitterDate: Thu, 19 Oct 2023 15:56:17 +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