From patchwork Tue Oct 24 13:20:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 157474 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp1943481vqx; Tue, 24 Oct 2023 06:31:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGfoSYUu3gZ/Px3Mu3iTARrHuZdl4Weiv8C7Re5sYlcu61zdpkS6i3JpWFDaaoCcaRHqExK X-Received: by 2002:a05:6358:6911:b0:168:f48f:4414 with SMTP id d17-20020a056358691100b00168f48f4414mr3742319rwh.25.1698154316954; Tue, 24 Oct 2023 06:31:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698154316; cv=none; d=google.com; s=arc-20160816; b=lDxmJLVqMbktmGjGoHMaKQc936Bc62umLmfZIfvry64eaRxiInjtfBD8v/bRAdWcAR 0MrBlmqiZj9kWqFmBh8mxd4QppMpWxBgrc7Z+RdYZVvgyLnqjLG7a72EKWNOmifbj5vp TivBy1e4HbI/cge+hd5It/kfwv19FLkfhfHfH2cLaCU3/z7UUdTvu5KSYoB8dLYcS+n/ UaB+9J4MCM3heBJ9F7o+buPdl3ZMj5h6mDOFwq0HUngp4uTvvSW17R8ZX1kUp2agT/dG bot97zMUvhQwzt/j+XbEvqFhCHk1Arzjb2jR1+DQaZiL2wV1FrpnDniASppnXIF7q6Mw f8jg== 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; 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 s20-20020a639254000000b00578a66db83fsi8372594pgn.141.2023.10.24.06.31.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 06:31:56 -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=aK9hT9VY; 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 3229D802C8CA; Tue, 24 Oct 2023 06:31:55 -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 S234366AbjJXNbx (ORCPT + 26 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 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,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]); Tue, 24 Oct 2023 06:31:55 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778649797995562767 X-GMAIL-MSGID: 1780643861069707595 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();