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