From patchwork Mon Oct 2 11:59:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 147494 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2a8e:b0:403:3b70:6f57 with SMTP id in14csp1712508vqb; Mon, 2 Oct 2023 14:58:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEzqDrBGEnsPkTVk5YCqRrSGBNPiT3EUEKgs7NB0hcoVqZDZOM8StWgBUYSxo1DwjNe6+qa X-Received: by 2002:a05:6830:33cd:b0:6c4:7516:f2cf with SMTP id q13-20020a05683033cd00b006c47516f2cfmr13314477ott.2.1696283916860; Mon, 02 Oct 2023 14:58:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696283916; cv=none; d=google.com; s=arc-20160816; b=g0Fnj0NXtZdBM+ZhK1IS27Pe9y9SZ3+TMF1SLxGKaouwVDKVKlvCqk1DlkVfAOHSZB axleqj/lt4K0hI0HgsWvf6rDA3NMuZnLZUN/NJ60/xiCFvEYSApn/JanEco5aPc7KJnV owTPwmyyADeKXZZ8eC6/a2irQ8i6Jpt0AEv9d/2KfzVFqDh53NgzZfW8jkRRHrLRpC7f jw6+x++pyuRa3r0QDcCw9seKO0plEOSlg5STM67cTizY6qz5I6djIjQuIgmy/sq6fiVS UH+cVRmw2BHNuIi4GEF0/4ZpwZV6/wLuJZaadlbZ9YuWsqD5G5RWmlIKSG9/MUD5bk6e b6ng== 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=GB4YWv4rtaFx36R21sEFu25/AV6Nske0fJrPY0aBq78=; fh=u57tXYamzTrJA+Ht8n1u7SfTMptrQaIb6LVW+jsaYf4=; b=HxPNN9Q6FuE19q5zWFEIjaDxL0TDGrRxkmMms7EDDVP3o+A9rLrO1shN5IND1YRMIX xt1Rx2hldgaxE5nKKgLHn2gkysvuEebbM55maM93EAQfMRJgSyyJ2xuCSiwsgbhRB1ae /VzsAd8kEVAz7TvDNjInMxbe3kkfL0fn6Ls6E8pbZZ8HWyeeZh5pN92+kvOLkiiEYhff 3aJPp3Eu6XzsP6kINpWxaKyKjW2gDzB9mMdgwj2QnOBTiIwRmSwGAzyM2egAJPiHOR+3 TccwkOHB9rChbXNj71d26HoyeIyFXYNKENjuNPh8qwcyh1Dp+ljRHq4Hum15WWu8GTO9 7iag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=j0hwbKEq; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id c128-20020a633586000000b00578f7063adasi16854122pga.33.2023.10.02.14.58.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 14:58:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=j0hwbKEq; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (Postfix) with ESMTP id 07E06810C2E5; Mon, 2 Oct 2023 05:01:02 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236979AbjJBMAr (ORCPT + 18 others); Mon, 2 Oct 2023 08:00:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47018 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236968AbjJBMAX (ORCPT ); Mon, 2 Oct 2023 08:00:23 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8FFA3FE for ; Mon, 2 Oct 2023 05:00:00 -0700 (PDT) Message-ID: <20231002115903.204251527@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1696247999; 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=GB4YWv4rtaFx36R21sEFu25/AV6Nske0fJrPY0aBq78=; b=j0hwbKEqTNeguQXWS6kkYTIhBz8qjbxkqYcU9mtwAhjwNJUTRM/noc03rAXjYI6ydMtNeG UqUDouPVRZGLQEzaq+tE4BWjx5bbsWkKZA+da6JT3i2APVv2LEQQdLcV6YsS9b3sJv70x3 l3qeSFqbGz6fhgZ/ZEBoeMI1ruIWZZvVrKtU9OBT9vxfubu2x2XCzORwu0hFmv7E2K8tdo Utgk0eZA6Pbag4YXIefo+DjmZYNGd4yM/srVRmjVW3Vtv7I8VPS9cjB+lheIqOWYABydC/ IMsmsJan9TV8mmIevyw0yWwuLpOuzqx0bC3aniSABA4LK6mO/Kh87PJYHsXlCQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1696247999; 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=GB4YWv4rtaFx36R21sEFu25/AV6Nske0fJrPY0aBq78=; b=Kn0ZRuKqoslqTiylEu2PHyjDxd1mPxSP/FHSMcNaOB87SxClTKPuukXjyTk7MuExTYFTpK XfvkSYIaaljz3VBQ== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Borislav Petkov , "Chang S. Bae" , Arjan van de Ven , Nikolay Borisov Subject: [patch V4 20/30] x86/microcode: Sanitize __wait_for_cpus() References: <20231002115506.217091296@linutronix.de> MIME-Version: 1.0 Date: Mon, 2 Oct 2023 13:59:59 +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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 02 Oct 2023 05:01:02 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778682604871258347 X-GMAIL-MSGID: 1778682604871258347 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 @@ -281,31 +281,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; } /* @@ -323,7 +318,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; /* @@ -346,7 +341,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"); /* @@ -373,8 +368,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