From nobody Fri Dec 19 19:04:59 2025 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2064.outbound.protection.outlook.com [40.107.92.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 569FD20371E for ; Wed, 8 Jan 2025 20:25:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.92.64 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736367948; cv=fail; b=Z/O+IPRNqzTFTV8qD9bQfaTjWYPfJuGCiINoCiOgwud7v5vtINzm+wFSG5Y86pezBqFrtFrc/cgERoKNtZvQmIqjZ4bUz2jbrQNs2g8MMSsH05Aqgy7nO8yYCn/Ao1sKbsbKaL1rzV8LA+AkPo0Hyo/ST0us0OTnD6KYLxbSpyk= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736367948; c=relaxed/simple; bh=vn/U3judM+6C23j7zOtdg0xS2TOiMCs3yPJws/Qb/+s=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=JukyFuMtlfbXNVB9YlRnyJD6aFWn5Gy36WYVRZPSWLfSJZ1/mQ6wzrpUFR1Ij15Jh+wf78AKxW5JQnqA2CEwPjqJio1Af4J+hT+kpfG2A7QWyS7su7KkmESxA75EBMEQpGBwZ6EmAP47ILnAq7vAnkCqfLYHLLgsKMNZTqlKkfQ= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=SPCFHGbo; arc=fail smtp.client-ip=40.107.92.64 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="SPCFHGbo" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=gM5ax/SEmzk85Kna6AcXCiAD444zWHeX/p3eq0bk4N9hu9Q/3D2YPCXF5jX/rvlkrbXBZX4x+cCziVR346eUq9uqhy6cEgRza7MmJd3H2YtgZ0R1uJb/geR8mW2WRrktrYFJ6auN0PU+XWVS0wXk8q6jvZuYLFHswN35qstDO8PbMlo+ofHs5+HiW57jOCTufrJ/F40WFywL7vwaF057tvTfV1wzwZStPkfdqdcrVOWlND1yxs/7VuZgZrAT2oN1fehAvA0YtWBKHqNGAURBn4/HqGRiOIADtiZO6H+aHN0rS85iwc9KXUjm4Lresxz7pmHPS6GoWrD6+CUhMdKFAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=pAVYDnG0yQikOyz+w7b49rxyT2cEoPUjJafzgA+nDOo=; b=tTLcoq7NFLLRctRCSef5HO5Z7na17ZuyxuOb7GXU+n/dwJ56UL/6BkS2pQBcG222eo7CNjR1xdzZZr4NJ0rlbvrZnXnrFx1hhYNmgcTvTdqiosfclB0i0N7wiWm8CKpH+DR5fiZ7W5vzPB7FLHZs06DexPbAEUngIIg4oShcxc5cWNyuCi6w+/7QQAvEqKTSXgYjki5iu7B3yyEAhMHbb31RPh8N1XvcCOaxlpKpI3oZdQESJFBnKPrLyVn4CNhscFAyYh7inl4lmJ1CzhFHDdj+9iOSbz9od7RcH1OcAXRZFIS6aFgw6MPxXErKfRXk7jbIs06wk6nK6JKH7jvWvA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=linutronix.de smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=pAVYDnG0yQikOyz+w7b49rxyT2cEoPUjJafzgA+nDOo=; b=SPCFHGboufqgLNajWHUcidMsHpcgwYvUII3XxrjugpjsrXYQxKB/qQwakPI+1rvDfS/hntyclXXli3QOcXPyX7Key2lOFqRPZHxfFrkvzpaBMRAGs5bOhsGD+v4b2WnlRnG7GCSfNTnYZwqqGfr2+mR5zKrlqpSsHqI4ZYNXXoA= Received: from BL0PR02CA0082.namprd02.prod.outlook.com (2603:10b6:208:51::23) by DS0PR12MB8766.namprd12.prod.outlook.com (2603:10b6:8:14e::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8335.11; Wed, 8 Jan 2025 20:25:40 +0000 Received: from DS3PEPF000099DB.namprd04.prod.outlook.com (2603:10b6:208:51:cafe::7f) by BL0PR02CA0082.outlook.office365.com (2603:10b6:208:51::23) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8093.24 via Frontend Transport; Wed, 8 Jan 2025 20:25:40 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DS3PEPF000099DB.mail.protection.outlook.com (10.167.17.197) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8335.7 via Frontend Transport; Wed, 8 Jan 2025 20:25:40 +0000 Received: from tiny.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Wed, 8 Jan 2025 14:25:37 -0600 From: David Kaplan To: Thomas Gleixner , Borislav Petkov , Peter Zijlstra , Josh Poimboeuf , Pawan Gupta , Ingo Molnar , Dave Hansen , , "H . Peter Anvin" CC: Subject: [PATCH v3 04/35] x86/bugs: Restructure mds mitigation Date: Wed, 8 Jan 2025 14:24:44 -0600 Message-ID: <20250108202515.385902-5-david.kaplan@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250108202515.385902-1-david.kaplan@amd.com> References: <20250108202515.385902-1-david.kaplan@amd.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS3PEPF000099DB:EE_|DS0PR12MB8766:EE_ X-MS-Office365-Filtering-Correlation-Id: bc078d46-5faa-4177-7304-08dd30229e84 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|376014|7416014|82310400026|36860700013; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?MtpCQ6EzmWP9lGVhIailt9ktUiiDtVJCEQ1mj/mV1Riz0HVU9Bx9rriwIH0h?= =?us-ascii?Q?e7GIlCvAWExmI/W/ufsk5Rsvmzq1/3nKb7uGiaIkbt6AiCjBu6ulrd1Qq5Ax?= =?us-ascii?Q?61YpG3IPX9cH8xipgk5/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4n/JmkIQniO?= =?us-ascii?Q?5WIXigRp4CPaeDG1aYANIlKJRqVlDsezE8wztDBoY9bm8nkC3xVrPGLFD8ha?= =?us-ascii?Q?3LWr8BjhMs5lxCvKDKc2JPLa8LfY2nPHBlo5V9OJhxVk54j8x+1Tm58RZoJo?= =?us-ascii?Q?hfic2q8D/vmCUYmFVRQPTgsJ7qDTbpLhjng48Znb7sLGfxltFfouGm4HJenL?= =?us-ascii?Q?TQ64U6lxmwzDOb8swjYBLPqDLUNZ59GAs6Y4vaHqXs3x/fderDGPoox9fYG7?= =?us-ascii?Q?Wmpalh+DPVQ8dWwtWzT6ya3/fyyk8b/e5XoZV+mkdgOHXI+xGp3jIvb2KQMj?= =?us-ascii?Q?x/3YvB5F9hlzMVXse75HIog3n3z6a3NjY493ww/G2WSfMD6kUrv32nnsiHSI?= =?us-ascii?Q?2Bzro1fPYSx07Y4Q0bSiIIbxNSoi/gvW3vH/lxqL4mCg14la0cqQtVtTkb6g?= =?us-ascii?Q?bKzr70fJ/iJt03px7CtX70G9BQOvo144MvsEGrla35PyEd0dWaMuxg5y8Psz?= =?us-ascii?Q?qOtsFoCkfilnAyc=3D?= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(1800799024)(376014)(7416014)(82310400026)(36860700013);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Jan 2025 20:25:40.1886 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: bc078d46-5faa-4177-7304-08dd30229e84 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DS3PEPF000099DB.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8766 Content-Type: text/plain; charset="utf-8" Restructure mds mitigation selection to use select/update/apply functions to create consistent vulnerability handling. Signed-off-by: David Kaplan --- arch/x86/kernel/cpu/bugs.c | 70 +++++++++++++++++++++++++++++++++----- 1 file changed, 62 insertions(+), 8 deletions(-) diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c index 592d40551432..ff2d6f2e01f4 100644 --- a/arch/x86/kernel/cpu/bugs.c +++ b/arch/x86/kernel/cpu/bugs.c @@ -34,6 +34,25 @@ =20 #include "cpu.h" =20 +/* + * Speculation Vulnerability Handling + * + * Each vulnerability is handled with the following functions: + * _select_mitigation() -- Selects a mitigation to use. This shou= ld + * take into account all relevant command line + * options. + * _update_mitigation() -- This is called after all vulnerabilitie= s have + * selected a mitigation, in case the selection + * may want to change based on other choices + * made. This function is optional. + * _apply_mitigation() -- Enable the selected mitigation. + * + * The compile-time mitigation in all cases should be AUTO. An explicit + * command-line option can override AUTO. If no such option is + * provided, _select_mitigation() will override AUTO to the best + * mitigation option. + */ + static void __init spectre_v1_select_mitigation(void); static void __init spectre_v2_select_mitigation(void); static void __init retbleed_select_mitigation(void); @@ -41,6 +60,8 @@ static void __init spectre_v2_user_select_mitigation(void= ); static void __init ssb_select_mitigation(void); static void __init l1tf_select_mitigation(void); static void __init mds_select_mitigation(void); +static void __init mds_update_mitigation(void); +static void __init mds_apply_mitigation(void); static void __init md_clear_update_mitigation(void); static void __init md_clear_select_mitigation(void); static void __init taa_select_mitigation(void); @@ -165,6 +186,7 @@ void __init cpu_select_mitigations(void) spectre_v2_user_select_mitigation(); ssb_select_mitigation(); l1tf_select_mitigation(); + mds_select_mitigation(); md_clear_select_mitigation(); srbds_select_mitigation(); l1d_flush_select_mitigation(); @@ -175,6 +197,14 @@ void __init cpu_select_mitigations(void) */ srso_select_mitigation(); gds_select_mitigation(); + + /* + * After mitigations are selected, some may need to update their + * choices. + */ + mds_update_mitigation(); + + mds_apply_mitigation(); } =20 /* @@ -229,9 +259,6 @@ static void x86_amd_ssb_disable(void) wrmsrl(MSR_AMD64_LS_CFG, msrval); } =20 -#undef pr_fmt -#define pr_fmt(fmt) "MDS: " fmt - /* Default mitigation for MDS-affected CPUs */ static enum mds_mitigations mds_mitigation __ro_after_init =3D IS_ENABLED(CONFIG_MITIGATION_MDS) ? MDS_MITIGATION_AUTO : MDS_MITIGATION_= OFF; @@ -277,12 +304,20 @@ enum rfds_mitigations { static enum rfds_mitigations rfds_mitigation __ro_after_init =3D IS_ENABLED(CONFIG_MITIGATION_RFDS) ? RFDS_MITIGATION_AUTO : RFDS_MITIGATI= ON_OFF; =20 +/* Return TRUE if any VERW-based mitigation is enabled. */ +static bool __init verw_mitigation_enabled(void) +{ + return (mds_mitigation !=3D MDS_MITIGATION_OFF || + (taa_mitigation !=3D TAA_MITIGATION_OFF && + taa_mitigation !=3D TAA_MITIGATION_TSX_DISABLED) || + mmio_mitigation !=3D MMIO_MITIGATION_OFF || + rfds_mitigation !=3D RFDS_MITIGATION_OFF); +} + static void __init mds_select_mitigation(void) { - if (!boot_cpu_has_bug(X86_BUG_MDS) || cpu_mitigations_off()) { + if (!boot_cpu_has_bug(X86_BUG_MDS) || cpu_mitigations_off()) mds_mitigation =3D MDS_MITIGATION_OFF; - return; - } =20 if (mds_mitigation =3D=3D MDS_MITIGATION_AUTO) mds_mitigation =3D MDS_MITIGATION_FULL; @@ -290,9 +325,29 @@ static void __init mds_select_mitigation(void) if (mds_mitigation =3D=3D MDS_MITIGATION_FULL) { if (!boot_cpu_has(X86_FEATURE_MD_CLEAR)) mds_mitigation =3D MDS_MITIGATION_VMWERV; + } +} =20 - setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF); +static void __init mds_update_mitigation(void) +{ + if (!boot_cpu_has_bug(X86_BUG_MDS) || cpu_mitigations_off()) + return; + + /* If TAA, MMIO, or RFDS are being mitigated, MDS gets mitigated too. */ + if (verw_mitigation_enabled()) { + if (boot_cpu_has(X86_FEATURE_MD_CLEAR)) + mds_mitigation =3D MDS_MITIGATION_FULL; + else + mds_mitigation =3D MDS_MITIGATION_VMWERV; + } + + pr_info("MDS: %s\n", mds_strings[mds_mitigation]); +} =20 +static void __init mds_apply_mitigation(void) +{ + if (mds_mitigation =3D=3D MDS_MITIGATION_FULL) { + setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF); if (!boot_cpu_has(X86_BUG_MSBDS_ONLY) && (mds_nosmt || cpu_mitigations_auto_nosmt())) cpu_smt_disable(false); @@ -595,7 +650,6 @@ static void __init md_clear_update_mitigation(void) =20 static void __init md_clear_select_mitigation(void) { - mds_select_mitigation(); taa_select_mitigation(); mmio_select_mitigation(); rfds_select_mitigation(); --=20 2.34.1