Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp1889317rda; Tue, 24 Oct 2023 06:32:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IELbHcSJ+Q77xovikcP8Sk88krTo+TEIrQO9fGhOtutOfGCycmhrvzY3l1AtQgtq7BnwKm6 X-Received: by 2002:a05:6a20:2450:b0:152:1ce8:ce3a with SMTP id t16-20020a056a20245000b001521ce8ce3amr2500243pzc.18.1698154326825; Tue, 24 Oct 2023 06:32:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698154326; cv=none; d=google.com; s=arc-20160816; b=DzRh1hAky0hDfHVeLz1lCrFWprjz+HdplTEqgTB/0sQ3M1D7xWbKAPZ85lpz3trq8M OuopdUzhgCAFyvRc9jrUlDE5NkzKVCtC7IS+eA1h2+w3jb8icBt1zKjF2XHIVbWkZQs4 cQeRERA8wGrJsfrDfTvH0S59Fj1O16byxwx0VyxRQHhv1+4+qWcIpX9KwBiHM0ZmFC2h kCd0QGG/a65svLRsXDH2amSmSLf+cRGPMPHAlG8UM+an0d+jXBroLzGAMN9o2rPW9l6w /vi4/FkyexeJq9h8mOcUW1kpWYLd+h1e8TCvEVPfhClUDDcCdhrQL0lzACADMVjaKbpV i0/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=5MqPqwLf8mXkrafTg2UGQBPmLjpBsqIYiEWFcLzr5D0=; fh=LWlv3U+xZ3+vQt4tlJRIlKl5VzN7iFjjE/bzRmlxKDA=; b=AiewzUD7ct/N7TgCz4zhxCzZBpPjJKlZTr+guv6AOmfhZWNXdwPMB901Xb1WHP3EIM gbqCeEpXFamX0yIXoaBd3jjAqkM559mtZFcu282G0Dn+LWqhPPfwgC3k8DE3hPTmP6YL TBhR7ctBC0plV+pPW9ixBdGfA9/ZMGQiVqHnfg+I8x7q+sXfIluKqvLa+L25R3kl+hZa mr8YH+NJwhFwstb3miyuPmeNBKlvrAadSTIsUeWeNwmQJXczgkQgAzC4ptYa6ku4hrYj rrC8pcihw7r6+m1ibJhhRLy/RJIX/vp1yI3kdrSJsmxFhTNN175iJkCWbYt+t1LC2ZkO mh1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=aK9hT9VY; 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::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id l10-20020a170902eb0a00b001c60d334996si7999676plb.622.2023.10.24.06.32.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 06:32:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=aK9hT9VY; 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::3:2 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 agentk.vger.email (Postfix) with ESMTP id 6D36A8041FEA; Tue, 24 Oct 2023 06:32:01 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234265AbjJXNbx (ORCPT + 99 others); Tue, 24 Oct 2023 09:31:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343502AbjJXNVN (ORCPT ); Tue, 24 Oct 2023 09:21:13 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 99F8610D5; Tue, 24 Oct 2023 06:20:55 -0700 (PDT) Date: Tue, 24 Oct 2023 13:20:53 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1698153654; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5MqPqwLf8mXkrafTg2UGQBPmLjpBsqIYiEWFcLzr5D0=; b=aK9hT9VYtc1FwNjIL105xXUCT2YucWMXYSyGNkPSOUwyh3Mk+F5IXwWyEBcYjPrc0QxRzl rN+2NbsXA1W4x/Rh342chz2My9D2bJS9M03Wowf36zS1J/3kFVD0JG6pFd09Wio4WHvv99 cttUwaL8Kz+EeLr0t9wXwZqHURsamlL0orHItbLOY9aE+Vabox9pxmh8+9zXhWFLL1HqRr pcZXXiE5lwPBtt4REsBCarJMzlYLkU8TNKx1FNrvHU1beks5klMr8/qwWcDLxz4yWQYjZz SDKmYJH+DGc1MsTEaATKiCLE/q6jWKjdajSz42UbD6Vy3jO5wEBxBnuhPmwdCA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1698153654; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5MqPqwLf8mXkrafTg2UGQBPmLjpBsqIYiEWFcLzr5D0=; b=3L6tv0uuL/nBk8cHNFYFf3uExk3vX5ZBgiItpGiU/kkDnF36EWLD7I6/tkPab1nzLuE3KP qatHeTbM2anCrlAg== From: "tip-bot2 for Thomas Gleixner" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/microcode] x86/microcode: Provide new control functions Cc: Thomas Gleixner , "Borislav Petkov (AMD)" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20231002115903.377922731@linutronix.de> References: <20231002115903.377922731@linutronix.de> MIME-Version: 1.0 Message-ID: <169815365354.3135.5223675509710461748.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit 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 agentk.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 (agentk.vger.email [0.0.0.0]); Tue, 24 Oct 2023 06:32:01 -0700 (PDT) The following commit has been merged into the x86/microcode branch of tip: Commit-ID: 6067788f04b1020b316344fe34746f96d594a042 Gitweb: https://git.kernel.org/tip/6067788f04b1020b316344fe34746f96d594a042 Author: Thomas Gleixner AuthorDate: Mon, 02 Oct 2023 14:00:02 +02:00 Committer: Borislav Petkov (AMD) CommitterDate: Tue, 24 Oct 2023 15:05:55 +02:00 x86/microcode: Provide new control functions The current all in one code is unreadable and really not suited for adding future features like uniform loading with package or system scope. Provide a set of new control functions which split the handling of the primary and secondary CPUs. These will replace the current rendezvous all in one function in the next step. This is intentionally a separate change because diff makes an complete unreadable mess otherwise. So the flow separates the primary and the secondary CPUs into their own functions which use the control field in the per CPU ucode_ctrl struct. primary() secondary() wait_for_all() wait_for_all() apply_ucode() wait_for_release() release() apply_ucode() Signed-off-by: Thomas Gleixner Signed-off-by: Borislav Petkov (AMD) Link: https://lore.kernel.org/r/20231002115903.377922731@linutronix.de --- arch/x86/kernel/cpu/microcode/core.c | 84 +++++++++++++++++++++++++++- 1 file changed, 84 insertions(+) diff --git a/arch/x86/kernel/cpu/microcode/core.c b/arch/x86/kernel/cpu/microcode/core.c index 001c004..1ff38f9 100644 --- a/arch/x86/kernel/cpu/microcode/core.c +++ b/arch/x86/kernel/cpu/microcode/core.c @@ -290,6 +290,90 @@ static bool wait_for_cpus(atomic_t *cnt) return false; } +static bool wait_for_ctrl(void) +{ + unsigned int timeout; + + for (timeout = 0; timeout < USEC_PER_SEC; timeout++) { + if (this_cpu_read(ucode_ctrl.ctrl) != SCTRL_WAIT) + return true; + udelay(1); + if (!(timeout % 1000)) + touch_nmi_watchdog(); + } + return false; +} + +static __maybe_unused void load_secondary(unsigned int cpu) +{ + unsigned int ctrl_cpu = this_cpu_read(ucode_ctrl.ctrl_cpu); + enum ucode_state ret; + + /* Initial rendezvous to ensure that all CPUs have arrived */ + if (!wait_for_cpus(&late_cpus_in)) { + pr_err_once("load: %d CPUs timed out\n", atomic_read(&late_cpus_in) - 1); + this_cpu_write(ucode_ctrl.result, UCODE_TIMEOUT); + return; + } + + /* + * Wait for primary threads to complete. If one of them hangs due + * to the update, there is no way out. This is non-recoverable + * because the CPU might hold locks or resources and confuse the + * scheduler, watchdogs etc. There is no way to safely evacuate the + * machine. + */ + if (!wait_for_ctrl()) + panic("Microcode load: Primary CPU %d timed out\n", ctrl_cpu); + + /* + * If the primary succeeded then invoke the apply() callback, + * otherwise copy the state from the primary thread. + */ + if (this_cpu_read(ucode_ctrl.ctrl) == SCTRL_APPLY) + ret = microcode_ops->apply_microcode(cpu); + else + ret = per_cpu(ucode_ctrl.result, ctrl_cpu); + + this_cpu_write(ucode_ctrl.result, ret); + this_cpu_write(ucode_ctrl.ctrl, SCTRL_DONE); +} + +static __maybe_unused void load_primary(unsigned int cpu) +{ + struct cpumask *secondaries = topology_sibling_cpumask(cpu); + enum sibling_ctrl ctrl; + enum ucode_state ret; + unsigned int sibling; + + /* Initial rendezvous to ensure that all CPUs have arrived */ + if (!wait_for_cpus(&late_cpus_in)) { + this_cpu_write(ucode_ctrl.result, UCODE_TIMEOUT); + pr_err_once("load: %d CPUs timed out\n", atomic_read(&late_cpus_in) - 1); + return; + } + + ret = microcode_ops->apply_microcode(cpu); + this_cpu_write(ucode_ctrl.result, ret); + this_cpu_write(ucode_ctrl.ctrl, SCTRL_DONE); + + /* + * If the update was successful, let the siblings run the apply() + * callback. If not, tell them it's done. This also covers the + * case where the CPU has uniform loading at package or system + * scope implemented but does not advertise it. + */ + if (ret == UCODE_UPDATED || ret == UCODE_OK) + ctrl = SCTRL_APPLY; + else + ctrl = SCTRL_DONE; + + for_each_cpu(sibling, secondaries) { + if (sibling != cpu) + per_cpu(ucode_ctrl.ctrl, sibling) = ctrl; + } +} + static int load_cpus_stopped(void *unused) { int cpu = smp_processor_id();