Received: by 2002:a05:7412:6592:b0:d7:7d3a:4fe2 with SMTP id m18csp600986rdg; Thu, 10 Aug 2023 13:00:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE50ksQZqeVLj1IqWcx18wb8plaXAW7DE6l1Ejpipdx92Q1Ho0VoZ5qdWSSqbK9Phh+3ywq X-Received: by 2002:aa7:cd0c:0:b0:522:30cc:a1f0 with SMTP id b12-20020aa7cd0c000000b0052230cca1f0mr16942edw.14.1691697605093; Thu, 10 Aug 2023 13:00:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691697605; cv=none; d=google.com; s=arc-20160816; b=WNpEs8TYLEqCt0Y/Qi3lbK6T8OWRCkJgkJWonH+vaPs5RGLfcm3J+I3grB6uwCQN0c 1U9+EUyHzkWyqGWClWZ/xWoeDVNLwEbrLYYoyu/Bt9hiC41LdhvmhYafTIzlxRPENrzR Rx02prWKHCxB7LxAHNN+WFgEJKdt8LVDctJN/6LqwglI2p824rJdpijMFuLzTwxJbO95 ADYcsNfzyyywmHICe7VwmtiPBU4xSunJMV/BA7AtDLjA22WIn2fI1Ebt25+u7u8NbcTK ZpLUdV1gUrUa+2IcS9SFl7VVHFPKPo+dVPPV+0rArBvWS9J3yUKiUeqDRMkbC9+60qNs dmSw== 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=Vp72a/EEov1VOHpGPkCjAnDQEmhhYrQPa+PSfT8H2jQ=; b=j1NjM0tSSkY7Jz8TMRXQNjWPLs/9txZHgpkL9Qd3+CsrMnegS648LewW6hm7ZggaV2 JVm+YeqFFGE6Frw8rd9rJ7N9+z/0ZAOkjuRCqIyZ40fCw+3zT3lkk3VqxrO8aYQ3PflU mHme23yxHOixmL8K5mM14YisIMYZsS2wE6Ire5wQGspYDP7KG43xMzTw4ZnpXPh+VqRb b97cPN7jL+eEQhnSBSwKVM9P/igMo9gNvWf/zJjBtBOJZ7xKv3mRamn5YVNrY32rERrZ fa/mS8EkUOTW9MBoPQgIzJ0TD2Ni4CVP0i3BZsaVa8+aUyNz1hNvYFBzitOTWKk1ib0B drGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=UAjkdYMi; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=DVrOz3cO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s10-20020a056402164a00b005237b4b5082si2004518edx.81.2023.08.10.12.59.39; Thu, 10 Aug 2023 13:00:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=UAjkdYMi; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=DVrOz3cO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236021AbjHJSiz (ORCPT + 99 others); Thu, 10 Aug 2023 14:38:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59850 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235893AbjHJSiW (ORCPT ); Thu, 10 Aug 2023 14:38:22 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5AB4530C7 for ; Thu, 10 Aug 2023 11:38:06 -0700 (PDT) Message-ID: <20230810160806.103791682@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1691692676; 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=UAjkdYMii7C+JL70axRtF22S7GqkjgMyjVYk1NPlMMDWj2yIVVY+btcQ+PGNZg8JOQ+dpF Gc28czrsf5hMuIDheHQbIKpN/JtG7VUX5TW3VjohEIkbOoZv3s3KdoSml65VOSVHy9A2uy pHZRDoZSgHh3vzz3a1A1AtwK8ykOAhA3XaMpKwpXGBt4upAjtSkwIAB98kjdeqtWRXzhSU Jv6Jn80Dr0V+JuXKopLzLCE0112K+QELqvTEeMEbWfMC+vlLcphegZ7+xy463N4XQ5P4lZ 8oHyUdgDR2M+zf9pUBkkRkOspMAenADadGWXTW+AHHeLzweP2HFbbfGl+iyZ7Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1691692676; 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=DVrOz3cOohcHSque6Fewmr1QsvJQ9sfVGQbHSzfV/ZaoqAFGZ3DHnD8MSZg2zXg/jatNPu Qs7Vaudgzrxm5kAg== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , Ashok Raj , Arjan van de Ven Subject: [patch 20/30] x86/microcode: Sanitize __wait_for_cpus() References: <20230810153317.850017756@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Date: Thu, 10 Aug 2023 20:37:55 +0200 (CEST) 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 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