From patchwork Sat Dec 30 17:23:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13506944 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 61C59C46CD2 for ; Sat, 30 Dec 2023 17:24:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C21E16B0159; Sat, 30 Dec 2023 12:24:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BA9526B015B; Sat, 30 Dec 2023 12:24:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9D5666B015D; Sat, 30 Dec 2023 12:24:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 8B0D56B0159 for ; Sat, 30 Dec 2023 12:24:54 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id CA3DFA11E1 for ; Sat, 30 Dec 2023 17:24:53 +0000 (UTC) X-FDA: 81624159666.19.6594C20 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2041.outbound.protection.outlook.com [40.107.237.41]) by imf09.hostedemail.com (Postfix) with ESMTP id 15D9A140017 for ; Sat, 30 Dec 2023 17:24:50 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=f3qFmrx5; spf=pass (imf09.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.41 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1703957091; h=from:from:sender: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:dkim-signature; bh=Ha70LUUwBF2GKOLwk2IDsBKjAfjqiTrz3NfZpC2t4HA=; b=0JaxHnlo1ItyG03IiCtv+5hIqvQD7a7omCsttFnCiDbh168quxww6+Me83chK7ODbyojM7 BYkBEVpuI5rWRmX9ZQ3WPze6DrCqcnfH/K/WfIYYmMMK5Lwu/zQ0nBmXod2CBwmwyxwIwy sgbYiB6ZPOOJnWuod8xuC48YmQZTAC8= ARC-Authentication-Results: i=2; imf09.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=f3qFmrx5; spf=pass (imf09.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.41 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1703957091; a=rsa-sha256; cv=pass; b=czSnfH1H3fRUc6ZtVcJTducVtAEmdqraAxzYGJrJWpC7GHPXniA2YC4guZhjLiKxv1CfmI UZUib+yyOvm3yECo428RT2r3wyiiycqzWoy5AVg3tQxMIU743oSn/GyZ9NFq+BJAlkMEmg D4FOpw49jzAtYffqPxUmj6cTOpQlhBM= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lhQ9Z214uC+757akySjjl4qwK0kDPwRbjytpQlgEkYMGPh8docme+RZSWo5wN14kqmEL1tElRviu0W8Hv680GBRhg9Xl5A6MFCOiad3gl8NKgUAMBbmjWFXeXIMieIjd4iUb2X1ShH3gt+vXf9kho4MpP8xgSWxoJb1CqcHn81BYPbY6RTN+hnsSVpW+Rq9etxpuXo4v0VBY3GEdHUh46n8KSkFyRgEfq2rqWpK6vlbwO4p2lEuv4LPPQYpXDWm/6+tX/RILWFOhp0rFM45O9Mo3BSMZPjD3GSBQMspUz6d4EsuGN6CFKOBhsfZT4vFKFPUe/qX4MX1tDt+v8mS6SQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=Ha70LUUwBF2GKOLwk2IDsBKjAfjqiTrz3NfZpC2t4HA=; b=fiDHKoCH7St0tJq0uh1Kz5Lpkvf2a3M96fKcbLvYFgMe6qRm1I4SGO2ieUoPfPh7jdwArNNcTE2gi8iISxrdYeRhI/+TT8jHHnMnTUNJPiJ0JZXiqBRubdt1RsjBiqigyahkRgtdKhqm76XPNCa03zAixI2KrVs5+vrhU19g6d3AO7Nnza3MMZfaHc9FRrEnpR40SpULQ+N5SZmVOAEeY5c6Fi/VDdl+ses1a3MRgZhhg/psLRUEj243SSZLPnAsjYMAlsKZWSKejhXMT2Ays6LC77bzTymO5NJctrWteygqrbjWqGMa74zDv3sYolsEI102h7bIjVz90SmCBKnlNQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org 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=Ha70LUUwBF2GKOLwk2IDsBKjAfjqiTrz3NfZpC2t4HA=; b=f3qFmrx55ItdoNrG0bSu71sWsBYqnPLnCtXBwUhavmBTOMXG0aHVU8ZkMaEB1edOEwlPRZbHKqP5IqAmFiDzmkoW2PV0B2ERu7jZLgINjcOXM75tYhwiKbQKQr7ooChdNrXh3xpLCM2+O+j/93zJW84tQ8cQEtuwUK5ls2sLFlo= Received: from BL1PR13CA0096.namprd13.prod.outlook.com (2603:10b6:208:2b9::11) by IA0PR12MB7531.namprd12.prod.outlook.com (2603:10b6:208:43f::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7135.22; Sat, 30 Dec 2023 17:24:46 +0000 Received: from MN1PEPF0000ECD4.namprd02.prod.outlook.com (2603:10b6:208:2b9:cafe::8) by BL1PR13CA0096.outlook.office365.com (2603:10b6:208:2b9::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7159.7 via Frontend Transport; Sat, 30 Dec 2023 17:24:46 +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 MN1PEPF0000ECD4.mail.protection.outlook.com (10.167.242.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7159.9 via Frontend Transport; Sat, 30 Dec 2023 17:24:46 +0000 Received: from localhost (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.34; Sat, 30 Dec 2023 11:24:41 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Alexey Kardashevskiy Subject: [PATCH v11 10/35] KVM: SEV: Do not intercept accesses to MSR_IA32_XSS for SEV-ES guests Date: Sat, 30 Dec 2023 11:23:26 -0600 Message-ID: <20231230172351.574091-11-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231230172351.574091-1-michael.roth@amd.com> References: <20231230172351.574091-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MN1PEPF0000ECD4:EE_|IA0PR12MB7531:EE_ X-MS-Office365-Filtering-Correlation-Id: 4a11e442-b6d0-4604-85f0-08dc095c3818 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: WUXxigJ5jBElF/DMeYYtioPmeGAqvw5qbvg9YnhOGpzqPE2iWvHoqrpK4I0+AVtbjEaM0r0WrMxF3Rtbt/pZ6SYOW2vSjswHlUBKB/eHFi+kB6Ohhh5Wzo7Rsvfvk77MqQzIYP8dO6nf4yShpJM0ZOxkq8RL0yQzmKPTOQ12aXnczgRWtQKI3mzvdvsI/RP7HhImUxiZ8hgc7MsmPefuCPoWExyl8AS2OF7VvAEBOPZI2KtfmTsOweKXD9y8rkxqx0BQBGu7UHb/v7Gce7jkfmww4nTThhSjtgRczC+PsuYtEF9NbBru3Sf6tyCtnLkAHjD/sNUhsFKB5RmIxtJ2uMlanaJ1WzhTCLFzFMs4Sy16Dfpw5paxEURnXLNIGbIS1nw5dpjeDElYKAloj1bEmHeXiZoTB0zTaWw3sJhqFiW2xqYYTLMB/SqXbx4KIF1dGP20/YkHBRC7avYRJ5nnlKNcIUBSVw8xa/GoLfCeXpgRfrNvyUZcjN3H7nDtBMjoHH9lRjWGsenVGHNSV6osyn0hbjwJkOoK7zsC2jjfqNWtsGeKUUlzX4rsgDDb9BbBdGaiVmy6MyaxfBVO7Fsbjb69W7VcM7MXUKeSB0MEQJvCoQmNuWa+rok/j73dHVHGiJiTqEcQiNkBNRPYO3MzuZ9WR2dLF934XuMk5bigXAVWH5OvK1Bk16c/oLsbUdLhKeHXgp3W1ifUBhh7YPtyLmrwEM+u/utnTazyMP/NMX7OKf367UbMsrMHTb6WdMti5ar8hJU4Db6LtsIqsJGjkQ== 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:(13230031)(4636009)(136003)(376002)(346002)(39860400002)(396003)(230922051799003)(1800799012)(64100799003)(451199024)(82310400011)(186009)(36840700001)(40470700004)(46966006)(44832011)(5660300002)(356005)(81166007)(2906002)(40480700001)(40460700003)(82740400003)(86362001)(4326008)(8676002)(7406005)(7416002)(36860700001)(8936002)(47076005)(316002)(6916009)(70586007)(70206006)(41300700001)(336012)(478600001)(426003)(16526019)(54906003)(6666004)(26005)(83380400001)(1076003)(36756003)(2616005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Dec 2023 17:24:46.1915 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4a11e442-b6d0-4604-85f0-08dc095c3818 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: MN1PEPF0000ECD4.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7531 X-R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/DR2xHerZJHV idI4YO3G 02zTbeyJsljc672EqO+UDaCKSCFYkGBOc+6WB X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: When intercepts are enabled for MSR_IA32_XSS, the host will swap in/out the guest-defined values while context-switching to/from guest mode. However, in the case of SEV-ES, vcpu->arch.guest_state_protected is set, so the guest-defined value is effectively ignored when switching to guest mode with the understanding that the VMSA will handle swapping in/out this register state. However, SVM is still configured to intercept these accesses for SEV-ES guests, so the values in the initial MSR_IA32_XSS are effectively read-only, and a guest will experience undefined behavior if it actually tries to write to this MSR. Fortunately, only CET/shadowstack makes use of this register on SEV-ES-capable systems currently, which isn't yet widely used, but this may become more of an issue in the future. Additionally, enabling intercepts of MSR_IA32_XSS results in #VC exceptions in the guest in certain paths that can lead to unexpected #VC nesting levels. One example is SEV-SNP guests when handling #VC exceptions for CPUID instructions involving leaf 0xD, subleaf 0x1, since they will access MSR_IA32_XSS as part of servicing the CPUID #VC, then generate another #VC when accessing MSR_IA32_XSS, which can lead to guest crashes if an NMI occurs at that point in time. Running perf on a guest while it is issuing such a sequence is one example where these can be problematic. Address this by disabling intercepts of MSR_IA32_XSS for SEV-ES guests if the host/guest configuration allows it. If the host/guest configuration doesn't allow for MSR_IA32_XSS, leave it intercepted so that it can be caught by the existing checks in kvm_{set,get}_msr_common() if the guest still attempts to access it. Fixes: 376c6d285017 ("KVM: SVM: Provide support for SEV-ES vCPU creation/loading") Cc: Alexey Kardashevskiy Suggested-by: Tom Lendacky Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 19 +++++++++++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 2 +- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 2efe3ed89808..f99435b6648f 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2978,6 +2978,25 @@ static void sev_es_vcpu_after_set_cpuid(struct vcpu_svm *svm) set_msr_interception(vcpu, svm->msrpm, MSR_TSC_AUX, v_tsc_aux, v_tsc_aux); } + + /* + * For SEV-ES, accesses to MSR_IA32_XSS should not be intercepted if + * the host/guest supports its use. + * + * guest_can_use() checks a number of requirements on the host/guest to + * ensure that MSR_IA32_XSS is available, but it might report true even + * if X86_FEATURE_XSAVES isn't configured in the guest to ensure host + * MSR_IA32_XSS is always properly restored. For SEV-ES, it is better + * to further check that the guest CPUID actually supports + * X86_FEATURE_XSAVES so that accesses to MSR_IA32_XSS by misbehaved + * guests will still get intercepted and caught in the normal + * kvm_emulate_rdmsr()/kvm_emulated_wrmsr() paths. + */ + if (guest_can_use(vcpu, X86_FEATURE_XSAVES) && + guest_cpuid_has(vcpu, X86_FEATURE_XSAVES)) + set_msr_interception(vcpu, svm->msrpm, MSR_IA32_XSS, 1, 1); + else + set_msr_interception(vcpu, svm->msrpm, MSR_IA32_XSS, 0, 0); } void sev_vcpu_after_set_cpuid(struct vcpu_svm *svm) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index b6179696861a..18d55df7fa5f 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -103,6 +103,7 @@ static const struct svm_direct_access_msrs { { .index = MSR_IA32_LASTBRANCHTOIP, .always = false }, { .index = MSR_IA32_LASTINTFROMIP, .always = false }, { .index = MSR_IA32_LASTINTTOIP, .always = false }, + { .index = MSR_IA32_XSS, .always = false }, { .index = MSR_EFER, .always = false }, { .index = MSR_IA32_CR_PAT, .always = false }, { .index = MSR_AMD64_SEV_ES_GHCB, .always = true }, diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 9ed9d72546b3..7f1fbd874c45 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -30,7 +30,7 @@ #define IOPM_SIZE PAGE_SIZE * 3 #define MSRPM_SIZE PAGE_SIZE * 2 -#define MAX_DIRECT_ACCESS_MSRS 46 +#define MAX_DIRECT_ACCESS_MSRS 47 #define MSRPM_OFFSETS 32 extern u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly; extern bool npt_enabled;