Received: by 2002:a05:7412:6592:b0:d7:7d3a:4fe2 with SMTP id m18csp1751419rdg; Sat, 12 Aug 2023 15:08:03 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGKCSZVDhVjEG9QKhw1Gl/W/2fhGe8GgK8amckaEcA0hfQETCOvD0DJ1YXqIh/71bo86wlM X-Received: by 2002:a17:902:d481:b0:1bc:69d0:a024 with SMTP id c1-20020a170902d48100b001bc69d0a024mr5933655plg.33.1691878082596; Sat, 12 Aug 2023 15:08:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691878082; cv=none; d=google.com; s=arc-20160816; b=S3K7bdeAuB5lha/xwL0ykXovuIpF2xZlhY0NpgYwocIDaBSImyW96INVIK67y+CcCH pw63Lo6yZixT5BCb9YHjtZS3moxDBJNzuTI/Mj82lWHdzDFh6HaFAeSoZJLwNbTUk360 hs8PY4FcABFb5lLPSDN8QRIWX0QzD5qgSbjJ3ak+Pg9NaY+Jqghx+qMaF76kp9Trk45K xV6OrGkZx0BcIVoClKGtgyPae7DY/yjm2jS+BEUHgFlO7PiwfyMyc63cvHhWSPCx9gV5 LQWzvQ9TzWxsrRs3ghWFGB9rEwrE2tsB5noSjl3ZYyvN4VsRQHOqmB+fJ3TqM/0G/caB VnVA== 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=yVae3u0BRTnDkPuniOcxLkA1wgvMDfOA8XxNsc4ELm4=; b=nSqixBlDLGU/s6MHoVkgUjRU2LSzIb8BbWWaDf1US2BY9ueqTTBv1MxANTIrrE24+B a4Q61TdYeWskW+Dt99u/aHcwvaWGATalp3vY8Stwx6GfFIcNupAKd7XtkjHe5sy1lLM9 sAmALBEJTaltoeJRSSW/+Fccjl0xbGKDg0mGcsW/YGdGphl8I+fhQRl9dQyHXrjuUauK e+OIXg8jUHS/aRi/iNTQ4lqRQFTkrkQzt5GdvMcKSfLmG9pJGWwONJAU7Sb9LHFNqCpl WHnlPf/edfqq11IPCN6f1ivFFwDGI0eOOs5uN4nFuoKN0cLywtLNQ8Qn1S0Nsoz3rQrb AkIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=ugkIvFOa; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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 d10-20020a170902654a00b001bdae8ceaf8si4065017pln.326.2023.08.12.15.07.49; Sat, 12 Aug 2023 15:08:02 -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=ugkIvFOa; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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 S231132AbjHLUCR (ORCPT + 99 others); Sat, 12 Aug 2023 16:02:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35694 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230498AbjHLUCI (ORCPT ); Sat, 12 Aug 2023 16:02:08 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 02F1E1FE7 for ; Sat, 12 Aug 2023 13:01:41 -0700 (PDT) Message-ID: <20230812195729.097937370@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1691870358; 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=ugkIvFOaucjW/r7xM473OXfvK5pcVN3cknssl6tn+vYvunLZ6W+hSQkCtmIXvfA73xRLSm hPAzDXdozz4dj4vvIDkTPpuRA2ORtFo6x0+kt0gJW3DdiXGWjnHfoH84+kTHGbTObDexwb HPwk3+o2eG0sTkMTDGUU+DbnDGeN7J547WHDVEx/SwgZFL7QvTNt0eF6tcXQ2Il5fPt5tm ZzlxXsZ8f34v8J3lRgVWYidPt2dxJfLPCKmONlVLz/gZwUsOt6xHDuEXTSNjBu1IubYHQm 8cGtHdeLM0eBszebHkIBO6FKYl5mnXjR8m/o8SeZD7V/7n7yW3UV4q57WEmccA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1691870359; 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=JKRJwqneRt2yMIREkHo8laWhBjmhaaOEaLFiuW5MU6G31/bb3r7ODSBZG7tmt/NPGGFAJ5 daJHOharoQ7aZXAA== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , Ashok Raj , Arjan van de Ven , Nikolay Borisov Subject: [patch V2 27/37] x86/microcode: Sanitize __wait_for_cpus() References: <20230812194003.682298127@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Date: Sat, 12 Aug 2023 21:59:18 +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,URIBL_BLOCKED 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