Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp3421934rdg; Tue, 17 Oct 2023 14:27:16 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH/fGeaQa4LRbmzXA+pc+UhCLAGPlEKIB+8CzT0bZaoIwdWTjTLo9Oynf4i0y6QP+BDMRIm X-Received: by 2002:a17:902:d2c5:b0:1c6:d0a:cf01 with SMTP id n5-20020a170902d2c500b001c60d0acf01mr5107580plc.11.1697578036343; Tue, 17 Oct 2023 14:27:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697578036; cv=none; d=google.com; s=arc-20160816; b=Wl2K1ORbKW4kKdqO1cMk15sOFO1P3uT5yU5xvg1Tx6eggcNRZRS79U02psaR8r8cML TRuaXHl5MtO0wwJBVvagtfFoNJseWJcFXPb9L+4axuAgBRf5NHXzJtYWjRCquiSSWWYS 2Ug16xb/VcCsjNgN8bbqPggN7ikwkQsjkddBUFFu/0AefTR4klA644aBp658F4Fnw+87 Q7+0Qdh3NEmqcpU9fqPqiii8pnyny/rC0W1Xq+tp9hWwp7uHOucC7VriIIJhAMI9nOrL LDKipgQ6586QIRnlTpJXKcQQx1EU2eUGnrSHaWHUWH9DM1eskRnFDgrv+LtzNWBXWeG0 iBsQ== 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=yQPGGk4tDFoJNz7sLEo7/Vp9lMjMiS6Izxa7zaPjZi4=; fh=8iy+GLBXZdpcs/hIddJ7mbUapYjefwP9Gb111pAzFms=; b=iFcJiLMvsvdESvcTxxlXX22iugxO3q4NpI0izfQWBINHkmlzdRJZzYjSIPcmiMhBL5 ZVaNYsfeOZn0wuvMsa5cbWX4IeByfd7q4eJJ+69BKkf+KMBMtCGdS12/DWB8DERQNs7U nPTny0EVGNyWIH7NcrxpdVNc7t9uRdcGogjzhFLUWd0tRAwYdrM2bN8mBv7CNZvzogV3 8lvPL/mIgrGBC0xV6VhB8yDe2MoM7P9cjgs6jNktXOnFrXm/EboPVRE9kdu7Wvt+dknZ UepUy3Iao8ow3PPLAp2grsw7FQ5zhUwV/36Kl4fACFWgBUwkJf0B5ng8BlOk0cqmlucW VPEQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=UasE8dKL; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id g9-20020a170902c38900b001c5e0672f53si2444527plg.466.2023.10.17.14.27.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 14:27:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=UasE8dKL; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (Postfix) with ESMTP id 93F3C8041EE6; Tue, 17 Oct 2023 14:27:11 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344186AbjJQVZn (ORCPT + 99 others); Tue, 17 Oct 2023 17:25:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33692 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344295AbjJQVZN (ORCPT ); Tue, 17 Oct 2023 17:25:13 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61C8319B1 for ; Tue, 17 Oct 2023 14:24:05 -0700 (PDT) Message-ID: <20231017211723.577159633@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1697577844; 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=yQPGGk4tDFoJNz7sLEo7/Vp9lMjMiS6Izxa7zaPjZi4=; b=UasE8dKLBf1mElOAA808GuU/+ud5e1n+/zVqNK7DW7JOPvxmKmXAZJ0gAnLWXfGhsz4rDa DhnoMtsAnegZOQevVsTo6MFL7Mf3AVzxR/TQIVeWLkz1INNxw1AU8i1lRgezQ7n0Z8mkhV rTNwVaPYfsTqZffX1VaJ3IG06UbYdKg1huivXZHYeW5RK0+PJnhSNnfBxdc/7wxWDN4q5M jDBW4ZHN28q1eZZQ9QLVh5d6EcdTo0l9ufvx5IdeJU1eYSxTlz70LRy13b8o32eZowP+yE fYM476F+AlVYQhDCdaoydQfFzVD2MjvbUqHY22rCD1A9+6673vOzR7QpbXnueg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1697577844; 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=yQPGGk4tDFoJNz7sLEo7/Vp9lMjMiS6Izxa7zaPjZi4=; b=37yFBhEfzbUm/LF21lWv5/AomeqNAIF3trwQTaPVde3nfpIbVeV+l3wjBRS6CBD5i0oRBB O/0onR7n7hP/h1AQ== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov Subject: [patch V5 29/39] x86/microcode: Sanitize __wait_for_cpus() References: <20231017200758.877560658@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Date: Tue, 17 Oct 2023 23:24:03 +0200 (CEST) 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 pete.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 (pete.vger.email [0.0.0.0]); Tue, 17 Oct 2023 14:27:12 -0700 (PDT) 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 | 39 +++++++++++++++-------------------- 1 file changed, 17 insertions(+), 22 deletions(-) --- --- 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 * 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