From patchwork Mon Jun 12 04:25:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275479 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 E3A7EC7EE25 for ; Mon, 12 Jun 2023 04:34:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 802B18E0002; Mon, 12 Jun 2023 00:34:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7B1F76B0075; Mon, 12 Jun 2023 00:34:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 605588E0002; Mon, 12 Jun 2023 00:34:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 535096B0074 for ; Mon, 12 Jun 2023 00:34:10 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 27508C026F for ; Mon, 12 Jun 2023 04:34:10 +0000 (UTC) X-FDA: 80892828660.29.A07F87B Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2050.outbound.protection.outlook.com [40.107.93.50]) by imf04.hostedemail.com (Postfix) with ESMTP id E900940014 for ; Mon, 12 Jun 2023 04:34:05 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=AGlzkRAt; spf=pass (imf04.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.50 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686544446; 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=H7xoPysbgoWBbQUpSlg9IvZXhRgyNb+X2UAvt5tpwug=; b=6NGYbomLMbLMJr90yrndS0Sl3c8cC7sHb9TwNuUZAr4f9HJvWN76tyBTPsFT9y5iCd+Xjl ZSszPW+A+oCTKAkSu1p/PnE06Z5+alFAOlPQRcHuubgro7jwHLkfljrYSoKypCtzgD5fdp gxYgdobnFRtQjbuXwqLjmm9/oCmH+AU= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686544446; a=rsa-sha256; cv=pass; b=O5+fTFhcA4PE7Bv7bWQs9vuHp8oPzzx8E4PgfhO2rlnmwrXrvJHii2CTKZwhCp3fJpALFH e8/HmE05+tHvAAfTqDIZOHLChqX4Y2LT7WonZ/6N/LYMolgJIYFVrbv+Q3GqJ/qBhlja7/ vOP+oeQAmoEA7dd4MFtTHc/9I4xNTjU= ARC-Authentication-Results: i=2; imf04.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=AGlzkRAt; spf=pass (imf04.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.50 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Hn4q7mHD5BK0aeeOkBE8nYaypjBM5ndCIznV0RoHiQQycl+gJYsFhH+utIgi8HMLM8aOKu5FhIS6l+akwJZClL7bCKeb/WN8GwJzzkSlW1Tt4m89fiUZCZRHwT1VPzjf/uxw615CIWrA8aQAh7In5P8aicxJPzEISuW2U7lSrpipTQzUUfDwOiV8dmOpJEN32iM88b+vK1TjngH7C/YBWTf43XlxyoheY+LVc/n44G63eEHp3AWfv5Ka2LkVNAIY8bDi8/yBJQGjr904jETx/RSvR6bTjmePCjWIdSXtUDC6YegXqsqik4nXLC39q/DoSUTwN6JflrCIY196vyIR3w== 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=H7xoPysbgoWBbQUpSlg9IvZXhRgyNb+X2UAvt5tpwug=; b=Qpg3o+o+tham3Y0PV6lXpigtQCRTiLczp7Le1wxEXfJvHwNMMVRb5GPMebBu4ziZOwG3vzAvgZ2eUW1TCsUMowxH9dnk3xk6Grg5F2Jno4Wfzm7ylMCAF+0HYMI/Mksrmu3rJh/mAEMjKtEdxDOhUwNhAZRjP2ntZPg4lGu0m2zcJEyIDibp3JurgjS3i6ZyqBHu3likKKyLwcSn+wYJml4ybwvc7hJ4PuyM9enn7PcjVZHwNQHc2mOwTPJMze/U20rQcbYv6+7YMGdHhDQsGSgoWF1zj6N8s9JZokJbEaN+l0UK7Z5nvZtWByewUIxxXvvvZVHPB6DerCguqP6CDg== 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 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=H7xoPysbgoWBbQUpSlg9IvZXhRgyNb+X2UAvt5tpwug=; b=AGlzkRAtOa6UVTIK60KK83iTDr6noPf6Cc7x0QPYtclEZy8ukP3z/3yDbv6p5bEWo17vdjWTVl8Tu35tJF1kV9yB0Hww0NfQaPDBUi3hk2Cx8imhQX61ZxGCaw/QjZxpsazDsHFnyQM7/y1odkstbq5oJqregniLkoZzNvdj1sA= Received: from DM6PR18CA0031.namprd18.prod.outlook.com (2603:10b6:5:15b::44) by CH0PR12MB5284.namprd12.prod.outlook.com (2603:10b6:610:d7::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.46; Mon, 12 Jun 2023 04:34:03 +0000 Received: from DM6NAM11FT094.eop-nam11.prod.protection.outlook.com (2603:10b6:5:15b:cafe::b7) by DM6PR18CA0031.outlook.office365.com (2603:10b6:5:15b::44) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:34:03 +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 DM6NAM11FT094.mail.protection.outlook.com (10.13.172.195) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:34:03 +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.23; Sun, 11 Jun 2023 23:34:00 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 01/51] KVM: x86: Add gmem hook for initializing private memory Date: Sun, 11 Jun 2023 23:25:09 -0500 Message-ID: <20230612042559.375660-2-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: DM6NAM11FT094:EE_|CH0PR12MB5284:EE_ X-MS-Office365-Filtering-Correlation-Id: c5ebba7e-e173-429e-4a71-08db6afe4006 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: CpsgVA9e1+4w7kD0JEz9Cl7ky/lBURcO1ZPvbF/ATtYS4+sY7MCZZ56Js2PbVIpzZQAmE2q3Yukxe6mO9sl8b3ifL32ZERyD8YSSeTCkw8M/uclClgFtN/g2j7ibIml6KXNG50VwgzAHKFY7rJrBUWrjIa+/uanHzJjLkSbUGa+5EU4sLWTRR1F+U5T/zhmdcyXlu9ahl/IMVf2pvYXtPX2BID6kDtVudRQ7LX+i0et0moDKAcdfucyGvgakaKX/UOFRFZJ3/7nRvcuJKaobgW3l0eI0S4cZk2+mx78u6DW8VvhmPP6f3QyD3vf0AcZ+Vs7UfFCmF30NK9CidxfUaBzGSvp1QJKkLjONPK2DKwqA4fsMJUwz73iwJE422v2M4at5DkI6bXWTwbCWO6AfA2y4K0FjkVeXrzaGh5NTZx+RxluMk73Ll89Lol1cLJK/tRcMc5ildl6tQnYExKsvXxwAEQ2OITDKIXqwQGMOYdoL8Mnhhn2jbkKW3yGyTV3mycpOHjsP01fHIE286xV4qkUIPE4wLnv0JpHvOs5FJis6MyP3S6HhYDFNdoSs/HzUBbowmRJ7X8Ebj4nfCl9V52WfSADAfJyEPvfJfDgp/6LCWJCfYpXpyKW+qO5Dv4LFh9IjeIwXMj4OHd0TArpUAz6StDEjujoyjO0OiGYmgcDJjC0Gg/mC8Yjg7QDBxC3dI5wX8Juw5IMwPsFI8pvrzBXMj4rzXJk9GBYf5puFpdLsXd3qxlrFmeSAPWKt0G0zbWu3f8tKLmVSPTqOY5jo6Q== 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:(13230028)(4636009)(346002)(396003)(376002)(136003)(39860400002)(451199021)(40470700004)(46966006)(36840700001)(40480700001)(36860700001)(426003)(336012)(47076005)(83380400001)(70206006)(6916009)(70586007)(1076003)(26005)(478600001)(2616005)(54906003)(16526019)(186003)(86362001)(44832011)(7406005)(7416002)(2906002)(8936002)(8676002)(5660300002)(40460700003)(316002)(36756003)(4326008)(82740400003)(81166007)(356005)(41300700001)(82310400005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:34:03.0511 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c5ebba7e-e173-429e-4a71-08db6afe4006 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: DM6NAM11FT094.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5284 X-Rspamd-Queue-Id: E900940014 X-Rspam-User: X-Stat-Signature: weo7x9qnweshincc4qup98o9aohutewj X-Rspamd-Server: rspam03 X-HE-Tag: 1686544445-254526 X-HE-Meta: U2FsdGVkX18dg+4U+orZ/GzL6IK3727XVHPF2mC4o/DpqFA5AZaJvesLJNbZNGV0ernpHCXg8YBtgfuGjWvOqszw3uMbQ/86kRP6/RKIbbGXzXxQawNMdcYv3kU5ShhZdZPOb79fdEGZslWKyZ2Z80ii6vFISodDj1KMS1YHCtY40TXivoygzD9w+3HqXmx/UPfag5Sk6lrV0RnqK2kE6C9wQjl2imGkycXhIaXUDZz3DEwMA9E9OFNTE0DRYZd1jCo2MUDhQUkfhO54QbJK/V5zIxaizDUI915YDEdPmUJD3NB5vwNVXFYDq0gAT6G8P4p6gIBzaRLgVSoIMAKLN/z/Aht2lHAInwA08fcrDRMRQ1QAPeTidPqVUG4rfJ9yrdGMsFyrC6TMtUGcZSzOpAPpw5EoM1CbYjWCYk/Es2f7xuO//kHDBdgTPwLx+CCUqZwCsUtwPGDfR8YJsoEMvSbrxI2ZLaiK7g63/8oNxla02vNF2KUT6qLT0NAQ5AzktS4HzjnC2gF+YyJexhYInevhLhU1IGb1MYBTpH2IuP5XK7GS6AG3X2NP+JfBLjCfA6yyFFftqAUwy5OwIR1dfHWToENo4VYlI7J6YPv+11wA7AZuNpFGY3xvJiLj0Lnhfirw82uu5CF2UTnRiiswmG05wYEk1WI+0/w/7QXu1YI43RwSWt4Oxa3jTdGQkEm7cI0BaY4M+lloW99meS/hw3sxA/S/xlE7PW+0NPEVap/FlgXNLgZeH+DbMR/P9hJo+tMcRlinKpnWPd+WKmwvSBPD7CiPO1Yg5yChNtlTbqcFmNUOjNZ3zciJ04B3hrnuk8PLrPAvBfaSQpNbRg/BhjlPZ5gNtfuyV5wXXKgkv+CDkOz4ZFEWP1XhX/PB2vMn3piIz4BYoqPmyQC7ESqrGMRBOXtDvtLMygD6QNdBv2+4D6u5FE4SSx2R1YcTfJjym8gDpz7J/6ko6BfSmWW IiK2KF2I pmpGvTjF2HRBkVzVOCo3wN2nI18D4UPddoGcPur+hkJRQCeLhoI4yUdxpdql2xP+OmekaWbDwFvXkSVcK0tfr4ZGjaJUGlGJ/I/UTMeg/WAc+FJQq71UKCgo7kSvhShNdp/N1SA0kmdgUUOxGG3lPxJcsS35A2XAPaNhVn4T5HCy7/chxbt227YZupcRtPweOuUp/iGkZjzX/KJP+m/VWGMCQA+XPtO2PeZuAYBJdm1a0Jw0PSlMf9RW2kgRXFx01o17Bxs36MLxT099D4/YQ847XxIfTl1LAw41eUOiWAMMMLIEijPWZOAiYTw== 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: All gmem pages are expected to be 'private' as defined by a particular arch/platform. Platforms like SEV-SNP require additional operations to move these pages into a private state, so implement a hook that can be used to prepare this memory prior to mapping it into a guest. In the case of SEV-SNP, whether or not a 2MB page can be mapped via a 2MB mapping in the guest's nested page table depends on whether or not any subpages within the range have already been initialized as private in the RMP table, so this hook will also be used by the KVM MMU to clamp the maximum mapping size accordingly. Signed-off-by: Michael Roth --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 3 +++ arch/x86/kvm/mmu/mmu.c | 11 ++++++++++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index 13bc212cd4bc..439ba4beb5af 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -133,6 +133,7 @@ KVM_X86_OP(msr_filter_changed) KVM_X86_OP(complete_emulated_msr) KVM_X86_OP(vcpu_deliver_sipi_vector) KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons); +KVM_X86_OP_OPTIONAL_RET0(gmem_prepare) #undef KVM_X86_OP #undef KVM_X86_OP_OPTIONAL diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 8ae131dc645d..bd03b6cf40fb 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1732,6 +1732,9 @@ struct kvm_x86_ops { * Returns vCPU specific APICv inhibit reasons */ unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu); + + int (*gmem_prepare)(struct kvm *kvm, struct kvm_memory_slot *slot, + kvm_pfn_t pfn, gfn_t gfn, u8 *max_level); }; struct kvm_x86_nested_ops { diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index dc2b9a2f717c..c54672ad6cbc 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4341,6 +4341,7 @@ static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) { int order, r; + u8 max_level; if (!kvm_slot_can_be_private(fault->slot)) return kvm_do_memory_fault_exit(vcpu, fault); @@ -4349,7 +4350,15 @@ static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu, if (r) return r; - fault->max_level = min(kvm_max_level_for_order(order), fault->max_level); + max_level = kvm_max_level_for_order(order); + r = static_call(kvm_x86_gmem_prepare)(vcpu->kvm, fault->slot, fault->pfn, + fault->gfn, &max_level); + if (r) { + kvm_release_pfn_clean(fault->pfn); + return r; + } + + fault->max_level = min(max_level, fault->max_level); fault->map_writable = !(fault->slot->flags & KVM_MEM_READONLY); return RET_PF_CONTINUE; } From patchwork Mon Jun 12 04:25:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275506 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 61D96C7EE2E for ; Mon, 12 Jun 2023 04:40:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0434F6B0072; Mon, 12 Jun 2023 00:40:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F29FE8E0003; Mon, 12 Jun 2023 00:40:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DF3448E0002; Mon, 12 Jun 2023 00:40:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id D20D56B0072 for ; Mon, 12 Jun 2023 00:40:32 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id A3033140243 for ; Mon, 12 Jun 2023 04:40:32 +0000 (UTC) X-FDA: 80892844704.14.2E74247 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2080.outbound.protection.outlook.com [40.107.94.80]) by imf03.hostedemail.com (Postfix) with ESMTP id 8401B20007 for ; Mon, 12 Jun 2023 04:40:29 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=wyqwRw3j; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf03.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.80 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; 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=1686544829; 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=cJNMBNV5gRu6D6upRq1tHyg7V/pfpJqpC1rXwTN2h3M=; b=12LWRKBGTRfp4+YaeRKrVs0Xn9wpGRgW5x4oEiCb41FbHawXHQzrZ2DLPrnhDD09Wu4HDc RV3ZCDGO+Gg2jpwW/ilAGkKHyWtof3ufg6JRgO+STjHKDJx8GauSG4qNwtdtzjSnq7a6Dd tLTsOR3azJiYQAtrE0HkRmMDe5+87Pk= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686544829; a=rsa-sha256; cv=pass; b=JrfEOKc4lC4K+WaM2QH00PMgUvi4tn9cM0HCeGYQQrRuhIiKDaqasuHaj8g6zr3c1BfGxu gTSTgf9jdrQFZtAxAhR7wipkH2c6BtDj78fGT4ij7IL6vwTilErOIyo8OIzmOpRsXL91ug dr8a5nl/S+i3Zw8tAqhMHanRk4ly+Xg= ARC-Authentication-Results: i=2; imf03.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=wyqwRw3j; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf03.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.80 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aLqXvpUSK+wHgVafC6iDd01N9ykBb+irdQY0N5faGQhqA3i2Js4B2bWqfYDRBNYFzywdWfRgoGCGPLxjsafajsvvvqaA4xdiPRSgEw0Wp5U02ME/kgVoD+YQw3BPDimqYpHghKuDostZdjZo4TqsZWB4SPLjjEh8rXnMe7o/ayNH9AiO1gj2C3NgYTH/tbWeN1baQ1OI796SXtpdLITzS5WMVzSKwlvnh6JcqNPbevSVwf3TR+2tOO5h0EIMuuj7dIOSTa34FYNKd411mPZqksNKaFgS6yYgL8rSIL64vrRhG70hdLBPpnDCHxT4PjT3vo6gpJCLcwvqlQXJ8eq7LA== 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=cJNMBNV5gRu6D6upRq1tHyg7V/pfpJqpC1rXwTN2h3M=; b=VbSriC1Hem0AQllHzl/tyFQZElaho6qIyYkM/nJRPL8EhB4tW/0o4Fe5PGmgVU/Y2uLp/MtzC4E3n+yDIzMP95uwPrnZlrrcC7peZktmIzl1XPunIYoJ3rlkqCZPgheE5qkD7zrFOE8HwT2V7Pt1KgdGfLwT8w/rop0qKqt61pMw4I+TxCMi71tEAm6pvWalUGeFkkKsWHkpgdL9//fkl7FB4bxp5cncG28zj5xyjb/6IqCKgMODcB8sWPQG3BtJHzc96gOBaLPF1V1JMzP5c+JpmHGtkT1rz3bt6R47wNje948Xs7Z3QpiBKnBiuLvgSVfLZ9rL7M9AtCPvGj+F9A== 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 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=cJNMBNV5gRu6D6upRq1tHyg7V/pfpJqpC1rXwTN2h3M=; b=wyqwRw3j1oIcohVGFf0reuiQzOaTg9uw6OJpTY0eRrGlLyRa/MG/Y1acjkp0kZAsy7m2qkAUB8eUUbMqf0rDavHW+ohmRFGSxGKhl6HuAg9t2jKGCH+6tK4ae4DVfln5yPYLnPPYvXZYA1xHPknBseb7U9DM+kFXDBunDX1F9Rc= Received: from MW4PR03CA0092.namprd03.prod.outlook.com (2603:10b6:303:b7::7) by SA3PR12MB9228.namprd12.prod.outlook.com (2603:10b6:806:39c::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.41; Mon, 12 Jun 2023 04:40:26 +0000 Received: from CO1PEPF000042AD.namprd03.prod.outlook.com (2603:10b6:303:b7:cafe::40) by MW4PR03CA0092.outlook.office365.com (2603:10b6:303:b7::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:40:26 +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 CO1PEPF000042AD.mail.protection.outlook.com (10.167.243.42) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:40:26 +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.23; Sun, 11 Jun 2023 23:40:25 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 02/51] KVM: x86: Add gmem hook for invalidating private memory Date: Sun, 11 Jun 2023 23:25:10 -0500 Message-ID: <20230612042559.375660-3-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CO1PEPF000042AD:EE_|SA3PR12MB9228:EE_ X-MS-Office365-Filtering-Correlation-Id: 88d0529d-0589-404a-394b-08db6aff247f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: INa9uhXEUXDUpe7tt2xwI8GF5TLwtw9xLCawQrR/qMMiUwmpoDn90FC775HebqMSW6yPN5nu+wWsCTY/czUsidaJr0/np8VVWk3+0p1Efoc49G5tR9UE3SZdycz6ky0p8u+NiQvzEs4M0pktIZ/Kx67Abrk+R46T4LK+tC1UkfjCoYAOQ8IFKhPnZTg+ngPL8gKOvENrHdrWa2MTk8ojHlqxCbAN+/ZSbATg/QjxBGVkvVrz6tT4Ch+vmSJhGZ/G32safyAtR3AEu/pqgcCZq7GLxzK9QlcWKNk/1aBwoblvM6WQUe9VqjGcM570FGlGQj0QVBDSsm8myuAO0e/sCoe8xlswQ2DY39YcKGuhDgQ+w68lPgc0PBB4PXwEtTI5UzxdwYJHtDOWYqhfJA+zMTISf6mih1hph8ugbjv1Ik6EsttsuLGL7DNlYTe96/SM1d6iORoXo8K3waXuu08enBwbjt5OPoM8m18ynetxO/OhMRFKgNb163HJRH56/T/fDsuAs0UKM8ZbKe+lnRlnN3U4K0i7WpkjCLFE2rZps6FcoUEWXcp8r/PzN0vKU6GDKBWgQf3wsquWje6R7S+ztaguvoPv0hH8SZaIVtRc6bTKk1PmDewIZNtIxaSO/iyLQJONpOdFc2mtsVHHNxTecary+rCfpZbID1doaiYul0G0PMGjyvmlnPSOQDcv74QwV0ThT7zHqU64C9HlINW8kMSO1MTcXhihoexDsKcqSgsYDyRYM/M+ou1H/9mT1xmQJH6UAUjgmBtIQvAENOJc3A== 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:(13230028)(4636009)(39860400002)(136003)(376002)(396003)(346002)(451199021)(46966006)(36840700001)(40470700004)(82310400005)(36860700001)(40480700001)(47076005)(83380400001)(426003)(336012)(356005)(81166007)(4326008)(82740400003)(36756003)(41300700001)(316002)(6666004)(1076003)(26005)(6916009)(70206006)(70586007)(2906002)(86362001)(44832011)(7406005)(7416002)(5660300002)(40460700003)(8936002)(8676002)(2616005)(54906003)(478600001)(16526019)(186003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:40:26.3016 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 88d0529d-0589-404a-394b-08db6aff247f 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: CO1PEPF000042AD.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA3PR12MB9228 X-Stat-Signature: u4zjtj69auxsho1cy48mn1w49cbphcpj X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 8401B20007 X-Rspam-User: X-HE-Tag: 1686544829-339647 X-HE-Meta: U2FsdGVkX18hHpgMNOBvUhfgHo8nmqX3VTw2dVADfOczm1k45XezojQqwVd9vb5l/U+O1A0nxAfEDyEcMK7ez42WYZyTRse/+dGYp+Xq+0wPNE9Yui+sQQDWKCX0vXdSlsedZxMj5CN0IPR2V2WKPtimSKcFclkoTzl4wNHQp3PK/B+Jtdy9koiE81Whp1k/rhqu4Beos7/LNc2TGM//BPSonwqzeJjftP3iH2oNdi1KgVZEP8ojwlXDsqL3LgHCnpaKASTvzl/zpAnVbzUugW5oHb7H8+LUB7FrDzUT3MfsvbLlLSdEbIkEYB4fuAAbwN6PXzyIHkhxIEkWTwT3mI6QkGJHL7sV+kYQrswh51QV2NA6nFUMbqovY21JHoVH0PNdKWBmfAbWUSg0xNmAfkz0yG8VO4fZV8K02ehS+rch/WVzFCeiZ4tlxiXWN+0vShR0p3M/fm7HXF/NnRwYjTmR+OWuKGQb4xBu4I2jb1ZKrB/YbYscjo2K4aCClJOdC+da2LTWQdsUI3pp8eoMTSu4T8tm7k9h+ykni/yiwoftumulJJaNA9BB1LUNzwezNHHRlrgcwdxtSYfU+hZCQmBM9ECyezSqACWOR9xI8ZNO9B7nBBspAHV75Vu6GwsykolLAvAXFZysGBXn6XRSo1BYG0JiGnIhSEQS2Ecmb1r4MdOAl2TsH94kUQh2GFcHNoUFDs7KFoA5sfOHRbGzQPNr9uzPaJyYen3qD+0WoqgtHqXtNAW9rdoEwUZtZa9mvYchcXNvSgwYwo09sAlm9R+8cypRHP0nGhuEjVwMu4rjc6nX1DEzhHXJR4zuQQjdwp5cc2cLIUdh6Ln0UYrprQQoPHs5VKEHJBOlkJQO/lUCw2/7cfhJzc6eISc9gnvMVN7+tTuRMrCrqQnO1TysQjtg89nQGvAQn36XTuX2lRH1A8GIN08LH7ll4WTnt0BcG4p8QgU22Hoi9PcHnKL kbG2s3Ar v+Kpf3q4eW2EsRwjgCA9zWed5ocf8bDVvGTAG6gqwS+Ng85MEwQ8ALJbSkg4x7Z5VmZzjH2KBpBXhN8t4xT1QmjklBhmTGJrKv0ORl0RU3Z6xIgjt0wT5Xz4CxTofyxuicWoUJ/F3BNSHVVUd9HwEyj3HV/SjxDkdCBawMDiFf5h8H1UbG14kAfKhUay1dIHagkP9Sh9vpSD6qoF8/bTqOTNn6Ps93SuPBFGq0krl24ot1ksCtMwYhk8AoR1OxFArMBlx4YTWNi+zHXSqk1I+YFCdyLBRSFNtNIq82bYpJUWHlUjAulDPsOqYTw== 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: TODO: add a CONFIG option that can be to completely skip arch invalidation loop and avoid __weak references for arch/platforms that don't need an additional invalidation hook. In some cases, like with SEV-SNP, guest memory needs to be updated in a platform-specific manner before it can be safely freed back to the host. Add hooks to wire up handling of this sort when freeing memory in response to FALLOC_FL_PUNCH_HOLE operations. Also issue invalidations of all allocated pages when releasing the gmem file so that the pages are not left in an unusable state when they get freed back to the host. Signed-off-by: Michael Roth --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 6 ++++ include/linux/kvm_host.h | 3 ++ virt/kvm/guest_mem.c | 48 ++++++++++++++++++++++++++++-- 5 files changed, 57 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index 439ba4beb5af..48f043de2ec0 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -134,6 +134,7 @@ KVM_X86_OP(complete_emulated_msr) KVM_X86_OP(vcpu_deliver_sipi_vector) KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons); KVM_X86_OP_OPTIONAL_RET0(gmem_prepare) +KVM_X86_OP_OPTIONAL(gmem_invalidate) #undef KVM_X86_OP #undef KVM_X86_OP_OPTIONAL diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index bd03b6cf40fb..b3bd24f2a390 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1735,6 +1735,7 @@ struct kvm_x86_ops { int (*gmem_prepare)(struct kvm *kvm, struct kvm_memory_slot *slot, kvm_pfn_t pfn, gfn_t gfn, u8 *max_level); + void (*gmem_invalidate)(struct kvm *kvm, kvm_pfn_t start, kvm_pfn_t end); }; struct kvm_x86_nested_ops { diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index c9e1c9369be2..10d76afa23d9 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13252,6 +13252,12 @@ bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) } EXPORT_SYMBOL_GPL(kvm_arch_no_poll); +#ifdef CONFIG_KVM_PRIVATE_MEM +void kvm_arch_gmem_invalidate(struct kvm *kvm, kvm_pfn_t start, kvm_pfn_t end) +{ + static_call_cond(kvm_x86_gmem_invalidate)(kvm, start, end); +} +#endif int kvm_spec_ctrl_test_value(u64 value) { diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 1a47cedae8a1..7de06add2235 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2343,6 +2343,7 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) #ifdef CONFIG_KVM_PRIVATE_MEM int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, kvm_pfn_t *pfn, int *order); +void kvm_arch_gmem_invalidate(struct kvm *kvm, kvm_pfn_t start, kvm_pfn_t end); #else static inline int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, @@ -2351,6 +2352,8 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm, KVM_BUG_ON(1, kvm); return -EIO; } + +void kvm_arch_gmem_invalidate(struct kvm *kvm, kvm_pfn_t start, kvm_pfn_t end) { } #endif /* CONFIG_KVM_PRIVATE_MEM */ #endif diff --git a/virt/kvm/guest_mem.c b/virt/kvm/guest_mem.c index cdf2d84683c8..a7e926af4255 100644 --- a/virt/kvm/guest_mem.c +++ b/virt/kvm/guest_mem.c @@ -140,16 +140,58 @@ static void kvm_gmem_invalidate_end(struct kvm *kvm, struct kvm_gmem *gmem, KVM_MMU_UNLOCK(kvm); } +void __weak kvm_arch_gmem_invalidate(struct kvm *kvm, kvm_pfn_t start, kvm_pfn_t end) +{ +} + +/* Handle arch-specific hooks needed before releasing guarded pages. */ +static void kvm_gmem_issue_arch_invalidate(struct kvm *kvm, struct file *file, + pgoff_t start, pgoff_t end) +{ + pgoff_t file_end = i_size_read(file_inode(file)) >> PAGE_SHIFT; + pgoff_t index = start; + + end = min(end, file_end); + + while (index < end) { + struct folio *folio; + unsigned int order; + struct page *page; + kvm_pfn_t pfn; + + folio = __filemap_get_folio(file->f_mapping, index, + FGP_LOCK, 0); + if (!folio) { + index++; + continue; + } + + page = folio_file_page(folio, index); + pfn = page_to_pfn(page); + order = folio_order(folio); + + kvm_arch_gmem_invalidate(kvm, pfn, pfn + min((1ul << order), end - index)); + + index = folio_next_index(folio); + folio_unlock(folio); + folio_put(folio); + + cond_resched(); + } +} + static long kvm_gmem_punch_hole(struct file *file, loff_t offset, loff_t len) { struct kvm_gmem *gmem = file->private_data; - pgoff_t start = offset >> PAGE_SHIFT; - pgoff_t end = (offset + len) >> PAGE_SHIFT; struct kvm *kvm = gmem->kvm; + pgoff_t start, end; if (!PAGE_ALIGNED(offset) || !PAGE_ALIGNED(len)) return 0; + start = offset >> PAGE_SHIFT; + end = (offset + len) >> PAGE_SHIFT; + /* * Bindings must stable across invalidation to ensure the start+end * are balanced. @@ -158,6 +200,7 @@ static long kvm_gmem_punch_hole(struct file *file, loff_t offset, loff_t len) kvm_gmem_invalidate_begin(kvm, gmem, start, end); + kvm_gmem_issue_arch_invalidate(kvm, file, start, end); truncate_inode_pages_range(file->f_mapping, offset, offset + len - 1); kvm_gmem_invalidate_end(kvm, gmem, start, end); @@ -264,6 +307,7 @@ static int kvm_gmem_release(struct inode *inode, struct file *file) * pointed at this file. */ kvm_gmem_invalidate_begin(kvm, gmem, 0, -1ul); + kvm_gmem_issue_arch_invalidate(gmem->kvm, file, 0, -1ul); truncate_inode_pages_final(file->f_mapping); kvm_gmem_invalidate_end(kvm, gmem, 0, -1ul); From patchwork Mon Jun 12 04:25:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275557 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 4FAB8C7EE25 for ; Mon, 12 Jun 2023 04:50:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D53D56B0072; Mon, 12 Jun 2023 00:50:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CDCB26B0074; Mon, 12 Jun 2023 00:50:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B55FB8E0002; Mon, 12 Jun 2023 00:50:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 9FAB36B0072 for ; Mon, 12 Jun 2023 00:50:58 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 74B68AE324 for ; Mon, 12 Jun 2023 04:50:58 +0000 (UTC) X-FDA: 80892870996.14.97D38BD Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2042.outbound.protection.outlook.com [40.107.102.42]) by imf14.hostedemail.com (Postfix) with ESMTP id 6E2BC100006 for ; Mon, 12 Jun 2023 04:50:55 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=UUfF3W1y; spf=pass (imf14.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.102.42 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=1686545455; a=rsa-sha256; cv=pass; b=IaK1/9VNmbKHkH2d6rVcwDxtGl2kHmhRFhWjvBIYA405Tb6D9xuLl17LKb6+JRKecbyVgf U6rUoHkMPQtqTVZM/xPcOzGk7CMx9KqxQYZPfxfoHJBnbwRzpP90W7rOEjEXWj8qSeJzbV XcHR8DbYz9iaGFosMPCLtvQnGED8jXc= ARC-Authentication-Results: i=2; imf14.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=UUfF3W1y; spf=pass (imf14.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.102.42 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=1686545455; 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=s218HDiFv2eZBxy4ZcFM21BtWf4J8w71c4nYAY2Bnnc=; b=DO9vNFGXo5zimc1ejYZyTOnLsDCnMkA7k2Pijwj8gghYu6uSfDCYH4BbNG9UaJ0QAv0f44 8psDAZDXJ2CgNDGvOZqFjyfA1A+x2VfCd3S4H3fkxV8ZjkoP9NykwZAyQ0hOCuUkmL10vw mGvH2bfmFXxfAbI52XPnYknBU8wj+P8= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Hr9K7FC9j34n1LlFCJy9piR1fC7/nj9koxs6u3uWx9oKk4+w+U3uBCiClu1tSv3/f8mv1hDyDGgkwlozBKTjkP/kgTdqkU1IYn+FlBrBzhPD4yShw6aumeabJ3qtCEB13n1IqUizVqC1zeQJD3FcjbwFRzgj5kdPpPPBfbCZiAURce8AoNJX0GdPp0d0pfu7GdpJ6uvwWNnqKoxw4iU/hVQ8jIO4hMYDP36o8DydfV80kbAogiznQHxHChRDlnCiTkGM0/wpcMWIZZdKC+kFjCjdcjOCg9Q8PUP/o/hwDtWGNhskmYieTAe7mr0BWN/1eRyMbHAqOEKN/quVCcC+nA== 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=s218HDiFv2eZBxy4ZcFM21BtWf4J8w71c4nYAY2Bnnc=; b=HfFEeFtEQ7p3hTNnj1KWhxvfNIzmoqIxB0egB/sUd5xtuHuCQzmb0NL/RGr7liBuSxUP2ipSzLFuWMWINNYSU9SDlNHvKLIBppBzu9CXDh2cZfAn/4wsCMSbddkf/EKZKGskMg4U4z3ADt/FsHVAZwoXjaktcYL7TAJjTTic2gHxFOLWIrMYQEL0T9Q7Jp5jyIrnYo0x44F9j5cBtRy8ZXd9ZjRCcCJGOyLLW4ASCkIddtF4uNI2/vge9kz4zoiYkVOwuiwAZ4zvxjw5r2qRASaUp+GW6lZyruQK8Y8TU5buctMJ7y1oQvlx5URDXti+fz0hJW8TfBGeAjYRGB7XYQ== 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 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=s218HDiFv2eZBxy4ZcFM21BtWf4J8w71c4nYAY2Bnnc=; b=UUfF3W1yo8GdhhtHwYnzq4wvAkKs6xROhS4NVgnunYA5lqMw34ZLZeZkIl8cajtZzRhGa6aBLUYTGCdqhqISsv3NdqAhmGkzW4g/Ux5G2osXh1sZ0C+zv/S1WhXEQAImVAZCrUnN0Onz0q8oAIZyiuwyZBQLLaZY3XNqWoOBqpA= Received: from SA1PR05CA0002.namprd05.prod.outlook.com (2603:10b6:806:2d2::8) by DM4PR12MB8475.namprd12.prod.outlook.com (2603:10b6:8:190::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.44; Mon, 12 Jun 2023 04:50:49 +0000 Received: from SA2PEPF00001504.namprd04.prod.outlook.com (2603:10b6:806:2d2:cafe::86) by SA1PR05CA0002.outlook.office365.com (2603:10b6:806:2d2::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:50:48 +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 SA2PEPF00001504.mail.protection.outlook.com (10.167.242.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:50:48 +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.23; Sun, 11 Jun 2023 23:48:05 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 03/51] KVM: x86: Use full 64-bit error code for kvm_mmu_do_page_fault Date: Sun, 11 Jun 2023 23:25:11 -0500 Message-ID: <20230612042559.375660-4-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: SA2PEPF00001504:EE_|DM4PR12MB8475:EE_ X-MS-Office365-Filtering-Correlation-Id: 66e8f260-5b71-4611-a0c9-08db6b009782 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: aDTmU/JF0XyDbGUVKUj65tBEtjtLuT5BwMkK4wsD/2Ki/WhY9VFnF6Rq49Y75ezZ7cTxNJ+WZf7sVCbJXTP0HvFMwXNNoTRn1R6m2DX1ig5NleoiLUUBzYLNwkhlqnTcQN2eLxC3FIdE8jCf6MA2X5+W9fBtzcFY5k43YvJpIXuyP1pXsr9VqZIfex4qbTED18c8HVoAIobgk0PYNqq4zKxIF+7cKR/Way2gOI1KOCcXIhbEHXOjv/y5tLVxRa3fbUOTp26MoooeFiv7kChTmEB+PQE1fPU0wXFxHs6vdThFVM3In3HrCpdxfa2qrPL2/JCf6PKXMKmJAiUEAR+jbXUvl+qAVQILrlEYj35BwOme/tCm9QLUVVmPfdPyCtPeEIxNn+V21acxnF1uBg4SNW8LRk4IOiY98tl/GIPTDV4e3UcAXOq9MEMt8JmjccGfjRzhQX755kZMazURioKJkTkaYJvo9iTER/p2irw6pMnoYXDwS4MKaACC//GE1/nWVVu8CiN1aeX5KqTjN6ETW3GcLk54PMT+JhACavujG4KJ3SinBUmZw34DXS9qtzRZX/4cib8DsIxRdG8cxaaw3ow5vEO985Lx7U44XFHNgGjFhca7M1msx2dOgIe/TcRP8oAELulRsjAJg8Wh78+5+uJoPvdBLm/SerfqU0oZiAFLNjVY74lPnK8ZxHnAFemamNFMC2syWhpG6UKgtBWVDHuAApurLbcwtXXzF7QUwS3tocuMXsNxFDeEtCRD4wrv/HAOPYFV8vaxHcYKih5sCA== 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:(13230028)(4636009)(39860400002)(376002)(396003)(346002)(136003)(451199021)(36840700001)(46966006)(40470700004)(426003)(336012)(2616005)(47076005)(83380400001)(36860700001)(356005)(82740400003)(40480700001)(36756003)(86362001)(82310400005)(81166007)(40460700003)(478600001)(54906003)(44832011)(6666004)(8936002)(8676002)(2906002)(5660300002)(7416002)(7406005)(70206006)(70586007)(4326008)(6916009)(41300700001)(316002)(186003)(16526019)(1076003)(26005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:50:48.8524 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 66e8f260-5b71-4611-a0c9-08db6b009782 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: SA2PEPF00001504.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB8475 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 6E2BC100006 X-Stat-Signature: cjp6o9qic6y1rwxneafnw37brus41duk X-Rspam-User: X-HE-Tag: 1686545455-613708 X-HE-Meta: U2FsdGVkX1+iDXr0bqqeCI4EKdF+zGrRrUYs6yvopYh8Tw6znR54mX6LDa0JbUYEVZr398+jBWj/BE9BoYdZuX/CgxyEq9I0lHzj6mEYB7o1KqF+5/FlXOk5X3nNE7ORlDu9pyd9/1pK+EF0E5W7b50Vc0q2YPQXUeiwTRMF1vOQHH9B9vhJ06VXzYiQd8ooEt3SEbnorJnkiUnrz/yfSmmAPrkjFDNjx+4fpPVhaAi+FNhV2HlK/Pin92FOC2zOkM2oPIi0g9HLgJpP+a4ZUUxjSx7bVBenB1Py9alCZaJbsAGLRW3G5hmCV86557HeQ+FS1SrRItaFoPONjAi07yEOXy6zZgGDS0kOckH4X8Rwdb5CgFmgP/kCYLZe3cC+vLjBpne3zOj2Bn4YWZGfLh5rwa7HHdWfJj2rRq33YHQ5601jgIRCTR0404nS6FP94/kl7DNnMnaih+x7gnlF97z17xxQteqG9eAfqNZqK7n6x0mjd+P9+3+FM5dZ4HchwLuQ7ONHHIiIfxc8rxZVbw7g7LUa5L74yiogIN3LrGW0gofElDtEABAqOGxqv5PV694kmvIiPHpMVZD03KLrBm+Ewh1tDzMaMFy4Q/ayVvAbqkL4cD20iWi5lGEejv/FQUQzM12OdQYYAvIwBSW3jXDb9zyVfU8NPaZKvErIHtoWBtopIUekwK46nW0DQPYmQm6pgzhhtsq116nqV07ctNsUW/tJto0r5Kz1MoxU5bOGgBxfSVx1kQWAg2m98iOgokYwrakS3RmydJnTBSfv81BEIJTUEQ/p+1YQeAUagjOx2UdTkDBYuIO/i/18qH5pF7pMIBtXdgX0yvWyNAO4GM/7iW1g642UVJi3dxYSIxSZjqrl865wzn0UBUNdsGLski1dlGLeNNSlhpsRFm5sUDQ4MOdg+9vEKp1i/qXUoJINom8QsiU8W/7XkANZJ16oYJJ3zZYV0Cooi1f9qZt RI/z++dh qyRaU2SUkAlWW1AlRgDmfY4tmCiCdN76sAk+FTBHJQtTcnQkbq4FStChiiW7hscfS5pcncsgknXQ2sdhdWLuS81dbvxdO0Ctz28+8ENM87bclK1rEl9IT6FpTVf/0x3glU/qRzr7S9G+GDR0Bv4QgkYHnCmsYqt6Z+b2kTc1xoZnkBrIono8UeEdqMRs9Ykj0mn6Vh5ER3HTPrZlCBRRyyt1H5sFIAOcGyU9bT1mtCA7GujJNSKn8elQ5NCVDmKPjIsDTvelGIdOk1iRlzlZwWwUyhxSn97NB/jyVJgW294yEZsLZleUG/O4FJmKOwIoOwSqi/Fj6PKbtn5jbwMhQXivSyEqurX2W6BHYgu07D99hYJ4= 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: The upper bits will be needed in some cases to distinguish between nested page faults for private/shared pages, so pass along the full 64-bit value. Signed-off-by: Michael Roth Signed-off-by: Isaku Yamahata --- arch/x86/kvm/mmu/mmu.c | 3 +-- arch/x86/kvm/mmu/mmu_internal.h | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index c54672ad6cbc..0d3983b9aa7e 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -5829,8 +5829,7 @@ int noinline kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 err } if (r == RET_PF_INVALID) { - r = kvm_mmu_do_page_fault(vcpu, cr2_or_gpa, - lower_32_bits(error_code), false, + r = kvm_mmu_do_page_fault(vcpu, cr2_or_gpa, error_code, false, &emulation_type); if (KVM_BUG_ON(r == RET_PF_INVALID, vcpu->kvm)) return -EIO; diff --git a/arch/x86/kvm/mmu/mmu_internal.h b/arch/x86/kvm/mmu/mmu_internal.h index f1786698ae00..780b91e1da9f 100644 --- a/arch/x86/kvm/mmu/mmu_internal.h +++ b/arch/x86/kvm/mmu/mmu_internal.h @@ -283,11 +283,11 @@ enum { }; static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, - u32 err, bool prefetch, int *emulation_type) + u64 err, bool prefetch, int *emulation_type) { struct kvm_page_fault fault = { .addr = cr2_or_gpa, - .error_code = err, + .error_code = lower_32_bits(err), .exec = err & PFERR_FETCH_MASK, .write = err & PFERR_WRITE_MASK, .present = err & PFERR_PRESENT_MASK, From patchwork Mon Jun 12 04:25:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275583 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 5445FC7EE25 for ; Mon, 12 Jun 2023 04:57:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DEE236B0074; Mon, 12 Jun 2023 00:57:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D9D2E6B0075; Mon, 12 Jun 2023 00:57:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C65788E0002; Mon, 12 Jun 2023 00:57:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id B88836B0074 for ; Mon, 12 Jun 2023 00:57:13 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 862FC120110 for ; Mon, 12 Jun 2023 04:57:13 +0000 (UTC) X-FDA: 80892886746.15.DEE2533 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2046.outbound.protection.outlook.com [40.107.93.46]) by imf26.hostedemail.com (Postfix) with ESMTP id 9CB5D140002 for ; Mon, 12 Jun 2023 04:57:10 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=xMJCEKRt; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.46 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686545830; 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=s6CZtGI3H4GAYAVMJzlyGlbmtiu/olxIiSDrYkbBXYE=; b=8c10ySD8GP7V7rWpDMqTLmRA6sUNvis/XG6S6iIlBaN+T6cxotQcbPkhhwZQvhmO+9K8VC YRjGXyBW7zIijLI7RhZ16WTD3Jzm4lj/Rl/ykZCjXQiHJs3tKtaXBNMWysNhriIa/czl0z +nyCQYYb3G6J5z+Voub8ARxnKDrFgmY= ARC-Authentication-Results: i=2; imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=xMJCEKRt; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.46 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686545830; a=rsa-sha256; cv=pass; b=VAjn0v0GoUxAoxPj/2r5z2QE215OudpMKUrah3UscZeGCFLfXSeFpme8qPeQFel+Or43oB k/S+cslar/HR5ZLv8CHxLAP7roZxQKzA+8ZuL5yhlh1/H/WQj3cdgC7Auz4qaAUdfVkJl7 LSImljWYOZLXeHRW6t995fZcADecHlU= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dSMM1sXVfktDwQu5Bw0PhsI5OLEzWTkpBZHZ364Ea5i4m7nou3FRlLghQ+gYmxAz/yXxAkVKqqCGW4Ka6ddFdrOYCWBRcGPWogi5NJ+KG5VKsggdPJ/2mDk+aw/qH9O0U1nkzbpKepOWVv9EaJLpHDxMIVH558oWGeEq7bYzyhvM76/hzkCSLARDorN0ifg1bJwKVPvx3bGz54GDHTRhDHN6+8BIH5ytetXG+5lAHMlZ5jnSd95DUXMyyeYrqtJKvjAWKnzZ1ab/cBCcAfWP/rURyWMFMtcWCWtuIfTeVDAdYVPxv9RPaKj9qgh7ObNRN5wRF/48H+Af83Nvh9TGgQ== 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=s6CZtGI3H4GAYAVMJzlyGlbmtiu/olxIiSDrYkbBXYE=; b=TM1tUeqOjKRmDzl8XGPwzGl6ckRUP0QezfAjHkdwfyUteeDXfb6AaFIMIipt5lXoF2ZYt/5PeTArKA8/6XZoIfIz7f9xDM1iKB7N/hF52ZF1YB7zAmUpMMXWiqijPyEnnJdLvjVfDlrfNWcmMxHMqKiW19YFvKDfXkc24+lJ3tzajS+TnX311mvyd1h1a/Kw14MqFJMfeKKqFsnLxi36UJfN5QNgN35QGGiSSltymChqqw++Div4YG6KXwydTLIL2001BHWmFT1g6bNe7rHGvI8ftxsnRv9Tyh0yzsY6u1WYgHpzY1yaAQukU6mrmRkixyMbdgVOSxPkVix6QFGLrg== 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 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=s6CZtGI3H4GAYAVMJzlyGlbmtiu/olxIiSDrYkbBXYE=; b=xMJCEKRtjEGKkQpanHWDrK8bUbDFvm0/4lpDnaYYJ6CA68hdqoVt7tVNkyfqsg6R3N5pYT9IJUOsPLcNnaL7JwuCptNOaUR5miS70VUl0RrdlKFRW01+hmINDK3UvUvEFogzvfuA+kT1eoPY2MxUAibf244aQKNzjBf1HBgFdJU= Received: from DS7PR06CA0022.namprd06.prod.outlook.com (2603:10b6:8:2a::10) by CY8PR12MB7562.namprd12.prod.outlook.com (2603:10b6:930:95::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.44; Mon, 12 Jun 2023 04:57:08 +0000 Received: from CY4PEPF0000E9DA.namprd05.prod.outlook.com (2603:10b6:8:2a:cafe::c2) by DS7PR06CA0022.outlook.office365.com (2603:10b6:8:2a::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:57:08 +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 CY4PEPF0000E9DA.mail.protection.outlook.com (10.167.241.79) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:57:07 +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.23; Sun, 11 Jun 2023 23:56:46 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 04/51] KVM: x86: Determine shared/private faults using a configurable mask Date: Sun, 11 Jun 2023 23:25:12 -0500 Message-ID: <20230612042559.375660-5-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CY4PEPF0000E9DA:EE_|CY8PR12MB7562:EE_ X-MS-Office365-Filtering-Correlation-Id: dd3dd272-df8a-4926-2570-08db6b017979 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: OlfPSbJS/xpr/4TLPGEjKnOFjpnp/Juw8RbQsmQ1EqJnBCcdOz+FJzdlYDRAtml3CtOffVki5/hyyKRIrJIcRLOLIeQySe7h0Q2YeAL9TmWLc0RsRfwULdmSYSzshhKNQ9i4EIeZ+GeF/8u1m0j7ywv300fkBRkmjJULSGemQ8MXXKt0742UKsWgudd6Lxr++bEkCuBVNjpuBGHTiqcD6ai9/8gLm1sE+UkZpWbnvA2qGye0eEcBs2ZdbB2DP5i96d6AEjUrTH5zrIsTLodVUXCW8mWoWFzZzX0EQUDj7FCMyz7W1DjlKGwdpWFQ0krPtTLx7gUzFpmKG2+68z1JcKLR/NGrVG49O4KN/Ys1sd9AqdvdsSQ2AU+RVbRSVxfcbE8qB2mrMNWBXURBr1nK37ffQuG9E+XcHeW2EyyfQA3swhEGF4hg4SduFfRoPHremf9Bg+TrfslDXVz75mBqhU+Gamk74MjDSPPGpHW/GkhEmMzhD17wtDy3ORSOstDWpAi2gpo41JkSdXqpJdY02yttmDmsf1mSgXrnv3vLmlk2VMwZTWryJMuyE7hWtE3tLDJiCzq2rejGlGnJgpNIFoLX3RRYUogej7Gjb8i14WUQYnqL4HvmR7SeKSzNaAmRAseAR4mbYpJkemedGO0U/vo0H3x7mUDp8dBUaVC3sqZC60lYz9plI/3cDORElA6DRUWtQ5hUhvOAGXypurmB2HVCr8rQURnMo4AzhsM1cK02xXe9vuNUezt8AxX+jzyXpSpSOxeD3vocXDAc/C5Svg== 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:(13230028)(4636009)(39860400002)(376002)(346002)(136003)(396003)(451199021)(36840700001)(40470700004)(46966006)(54906003)(5660300002)(6916009)(4326008)(7406005)(7416002)(8936002)(8676002)(44832011)(41300700001)(316002)(186003)(16526019)(2906002)(478600001)(70206006)(70586007)(6666004)(40460700003)(1076003)(356005)(82740400003)(81166007)(40480700001)(26005)(83380400001)(426003)(336012)(47076005)(36860700001)(36756003)(86362001)(82310400005)(2616005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:57:07.8929 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: dd3dd272-df8a-4926-2570-08db6b017979 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: CY4PEPF0000E9DA.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7562 X-Rspamd-Queue-Id: 9CB5D140002 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: bwp9xsqp6frjymrgkf1oj6r1ankiop5w X-HE-Tag: 1686545830-935780 X-HE-Meta: U2FsdGVkX19F5+zrok8GK5zbXl6ssiOneIu13ifiDFAgGIv3NJMfJUX55Ka39SVhDBYwlYxMK0QaMXePEpgdMyXjaErNh8kcEye76VYoZ61qv8SZq2c6Om/KzTwpg6fcgeA5vmkIzuaidBmjvDYvz1VLH8MLZfWcC2OsGboqVRsvYQKdh+t0UOEgiyzU93QgXIxrV15sSeZWW4JVCULbhjhvIyz+mH8B+iQw+/22VDgPm7iMmEZG2onuGRFduUudqBEdb8aWYvg0xelm0/7ImtzOFt8cixmdBbNsnV3fB+tITJ2Bxw5h+e/nAkWPmVkcXkb88mUlmZp06J7Cngln2Q5JTopw7Gp4mNHdw12sCHLU6OOJXBmsYf1GU8aiIDEkpToVm3CMihl5OCIiebTzA0KkpcFZKqgzUg5KzhtJ7VzEaMpp4qe8uBh9D/hKENO5t92+J5ENXL2whIQ/V9K1CBqk2pdOgox79OMST4VzO9wKsSu+RcOv25sTRRUBMEYIJCAWH9AooNgxLhefVq3rmSxZR+U5I/FJET6r9SwKfjfKmqqV+iKgpbcsMMbV+14r+kWIgNG3UApX+PozpDVbSg3+/N8qtZ8wI9NSb8Af/k7esNAEoHjkIcffFllo7d8KVRXsoJgac3QYiYXiVzPF5Yp0ps72ONBBVewYcT9R1TonuryVqsG5Q6xOQ73j0pRPiXHIB6KrhyEsn2npwXehcWios7XcmSdA8AB3NDjOjqrKfAZhrFB63ReEdYIPqqEgpQe67P3Ben8b2NMQFj5o5xKFyfy/e0Zx2tjMbJ+ntE2+rigvrfxeRvpwK2o7Tw4dYftfTxcMEOonrf7srjbicmabrfYvAjbD13VVc8AdAktY3ql1qfhBkUMwqao1d3mtD82spYrTmJYSMLLDen05DLYriOju7MRjK+A01I61etewKU0jDME0yl3/HI6xIN/oTN7d4Mb9SiwHXX7KLZx 5D8iPS2Z /bBlILFlbt7IHSCsgBb4zc7AmnbfbamXKzjww39yQzwAvMUe1mF53/a+Lmbxe1tJnXN0zfu/jz5WdKCLthuO2w/wRMnyV+nzM6uGgSxUdD43TCQyWoAOULtAsNalDshfZLr0ZaVe9GxlZ8WhvCmGdKPxyRWIShKLOcUA5546SDs6Xn46PcJ1pH3eIeBpUbB65QW/qwURW9MrNMZ4CbJaXq+/QEwWJ7Cv8SKtIItaXP9VQ+AThCyTaHVPQEW9NxYI/FErnHrSfKoWauZdCrAh8xWabpUTw4qZpREg3gURXQM0bqFqUljFnw8R4pw== 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: This will be used to determine whether or not an #NPF should be serviced using a normal page vs. a guarded/gmem one. Signed-off-by: Michael Roth --- arch/x86/include/asm/kvm_host.h | 7 +++++++ arch/x86/kvm/mmu/mmu_internal.h | 35 ++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index b3bd24f2a390..c26f76641121 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1445,6 +1445,13 @@ struct kvm_arch { */ #define SPLIT_DESC_CACHE_MIN_NR_OBJECTS (SPTE_ENT_PER_PAGE + 1) struct kvm_mmu_memory_cache split_desc_cache; + + /* + * When set, used to determine whether a fault should be treated as + * private in the context of protected VMs which use a separate gmem + * pool to back private guest pages. + */ + u64 mmu_private_fault_mask; }; struct kvm_vm_stat { diff --git a/arch/x86/kvm/mmu/mmu_internal.h b/arch/x86/kvm/mmu/mmu_internal.h index 780b91e1da9f..9b9e75aa43f4 100644 --- a/arch/x86/kvm/mmu/mmu_internal.h +++ b/arch/x86/kvm/mmu/mmu_internal.h @@ -252,6 +252,39 @@ struct kvm_page_fault { int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault); +static bool kvm_mmu_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 err) +{ + struct kvm_memory_slot *slot; + bool private_fault = false; + gfn_t gfn = gpa_to_gfn(gpa); + + slot = gfn_to_memslot(kvm, gfn); + if (!slot) { + pr_debug("%s: no slot, GFN: 0x%llx\n", __func__, gfn); + goto out; + } + + if (!kvm_slot_can_be_private(slot)) { + pr_debug("%s: slot is not private, GFN: 0x%llx\n", __func__, gfn); + goto out; + } + + if (kvm->arch.mmu_private_fault_mask) { + private_fault = !!(err & kvm->arch.mmu_private_fault_mask); + goto out; + } + + /* + * Handling below is for UPM self-tests and guests that treat userspace + * as the authority on whether a fault should be private or not. + */ + private_fault = kvm_mem_is_private(kvm, gpa >> PAGE_SHIFT); + +out: + pr_debug("%s: GFN: 0x%llx, private: %d\n", __func__, gfn, private_fault); + return private_fault; +} + /* * Return values of handle_mmio_page_fault(), mmu.page_fault(), fast_page_fault(), * and of course kvm_mmu_do_page_fault(). @@ -301,7 +334,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .max_level = KVM_MAX_HUGEPAGE_LEVEL, .req_level = PG_LEVEL_4K, .goal_level = PG_LEVEL_4K, - .is_private = kvm_mem_is_private(vcpu->kvm, cr2_or_gpa >> PAGE_SHIFT), + .is_private = kvm_mmu_fault_is_private(vcpu->kvm, cr2_or_gpa, err), }; int r; From patchwork Mon Jun 12 04:25:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275585 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 ECAB5C7EE2E for ; Mon, 12 Jun 2023 04:58:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8E3D86B0072; Mon, 12 Jun 2023 00:58:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8937D8E0002; Mon, 12 Jun 2023 00:58:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 734846B007B; Mon, 12 Jun 2023 00:58:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 6651D6B0072 for ; Mon, 12 Jun 2023 00:58:09 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 39D7540002 for ; Mon, 12 Jun 2023 04:58:09 +0000 (UTC) X-FDA: 80892889098.17.66445B7 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2059.outbound.protection.outlook.com [40.107.102.59]) by imf26.hostedemail.com (Postfix) with ESMTP id 4CE8614000B for ; Mon, 12 Jun 2023 04:58:06 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=XWPixkjo; spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.102.59 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=1686545886; a=rsa-sha256; cv=pass; b=ruyOIQOIsX8yNxS4e2eKTA+gWicel3A6TnsHMZLM5TjzDsiiNSaevoVCYDglkqHfIa44pK h6Zf4LJcBvC5hroFUMqYCYQ/37youQdvs5BTbJnOwEPv3xAMdf0cPHdEKDGptXIeiyA5x4 wUBa4qs+JRSX3IFGXDCx732OzlZrXx4= ARC-Authentication-Results: i=2; imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=XWPixkjo; spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.102.59 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=1686545886; 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=CeyhY905hNs52Bqq6Oa0uJDMuGrsDGEdsZEoDZbc4iM=; b=WPxwt3aw0OFc1m8HitsdU/Os40lav2O/RFhmP3fsUKYUXy1UlT76dLML7tPUwVBOnkhyPe nWxtoRu/RLES6w2B96KhI1LaCS0BZWMsGXC+IYJNTzT5GWrl25VdJJCvEAGx7MVymNb/CE QLm1s8D1lFMC+qHC7eXzaV85hMZI+Pc= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gNVEd/9vqDK/M27AU3zQDh98UQpgronGZOS0Du7jbejSwaI4pzJduissWfAD4YGXjgVvYfdM0NyTpouiqy5cC0UFyNBnAXARMLdi+mWkEZ2UEBchVr60dILky9KlVHAYVONafBAq2ILD/NU9vs00+6MiDdFsCWNCtitEglIk+lY6SH+dtxNmiLyzKSMf5z9DZAMqtLf7GijeJAyELkbkA7BpiAk/DHEG3EuvvlpB5X1lHLcae4u+1BDnCTJlUqCzw/OZnNTDqcftglKxVcaQ3VAoL6FWd3sF5SaEyvepYejAz8a6yMJwN63xedFVrlsuzSaUha9Km4dlhdLYv/yhbg== 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=CeyhY905hNs52Bqq6Oa0uJDMuGrsDGEdsZEoDZbc4iM=; b=kKAOtqn8UgOEQAldoVX34sEwYtHW/PzvNGtPVbQn7KqIZdb4T13xWgBrr496MVlwzcmoCrZE1zp1M/BCpSIus2R2klaOnAcQUiOli77Ek3sFoEtQG7oudiHSmGB3k8DZOfN/L8suC4AHHXIkm6zlCqE+Q6FojjCVfRTvBV3FLcRbFinH1NLiJtVwDcwqFHC4rLKknQEi7RhXpxyY9EyVuFOLJNP+/4ll+DQ+t+9kBlUzkZ8WqcCsaGmmzkhJFmyvKwPvCP1aY2nbTCx5glDAp/caSwpddOyXM0BinHQSQzrm7hdSTP/kjkJcrOek2MdaRPoLh6G5vJ0AYK2hojqD+g== 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 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=CeyhY905hNs52Bqq6Oa0uJDMuGrsDGEdsZEoDZbc4iM=; b=XWPixkjoXVP3U/qxiMxybV5lrJe6T4vufv2cusmsG8hmxvX2ZwDszUgxp9kJdI5I/OLVW84UdiGDWei+LtAV8xk3Nsd2m/NZyN5i3NEcOEHsLLwMunpJYblqDIyEQKQlTFaIWryUURlTNFxQsUXaNxtmbYUe0x9V/m9Aak0jnHo= Received: from CY5PR04CA0015.namprd04.prod.outlook.com (2603:10b6:930:1e::9) by CY8PR12MB7099.namprd12.prod.outlook.com (2603:10b6:930:61::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.33; Mon, 12 Jun 2023 04:58:04 +0000 Received: from CY4PEPF0000E9D9.namprd05.prod.outlook.com (2603:10b6:930:1e:cafe::52) by CY5PR04CA0015.outlook.office365.com (2603:10b6:930:1e::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.34 via Frontend Transport; Mon, 12 Jun 2023 04:58:04 +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 CY4PEPF0000E9D9.mail.protection.outlook.com (10.167.241.77) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:58:03 +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.23; Sun, 11 Jun 2023 23:57:57 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , "Kirill A . Shutemov" Subject: [PATCH RFC v9 05/51] x86/coco: move CONFIG_HAS_CC_PLATFORM check down into coco/Makefile Date: Sun, 11 Jun 2023 23:25:13 -0500 Message-ID: <20230612042559.375660-6-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CY4PEPF0000E9D9:EE_|CY8PR12MB7099:EE_ X-MS-Office365-Filtering-Correlation-Id: 74ea92ed-daa1-464e-f918-08db6b019aea X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: G8Qtn5v8D3fN7nlKCmp3QF5sxzqZo38E/6vXvRNeh/UL9L8OyTg+68KT2Sn58QW1u5k60W6EWag526oFEIxqekXMPO0Qzgy1A/oqZ+Wjq6joklDfYCf5+ihIp41VPn44muVOd7tOtu2Hb99Qs8sDcrObyvj2E6cjgA2PP9jPjmgQtJSY5/UWTXAXsmRoT9M0oPbcksWWUeZhBr9Wr/Sa4q48RpZ7YSrhR/Fh+XWXLwIvRhww7I0zNiISeCdTsSJW/AzMtCBcWg+NHHqJToOiNRODXEVQ0Na4h/GUc51F9YU8RKwVK7p76D9tLbv/5qqVCFixIDOox/vy/T1KKNbaSKJGdRI8sMticR8Uzgwuda05EJ6PgSqPCdXLhBqOrTX61nEPCLj+fHnpP7HiYNoyRTysLfvY83dOiEf5yDPRe6FJDTg2y0p7+q7550rPkJJj2GoOBd3j/Zia79TUB+xblOcJYKZfw/8A3DhskUZEVV54MNwmc86sJbbQD2oupvbDpugggfGyEiA7GO5U60BJCTuKtW+urN9XvL5tEcPiXgAHjQWdLRlsqAsKbQ2d5cjsHcWwf5fv9z5j73A8jFDlr8O7Urvr5VxXkxJ20tqQM6Jo1DS7GwR9Ga4a2eSH59JsExClOIoWVbxw9d+9Siy/0FI99nVkKAdy9eQj2ZG82syMddR0NKQgFkZ2IclOqrXdgeyCzA0SNLnrzFw2zpF15Z3b3/kfHYNoEUgPwAHg9PlXYzkSaozSykLmuZiSt0twNwbWWnZ45dh0WB1fJOZEsg== 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:(13230028)(4636009)(396003)(376002)(136003)(39860400002)(346002)(451199021)(40470700004)(46966006)(36840700001)(44832011)(7406005)(8676002)(7416002)(70206006)(316002)(8936002)(4326008)(70586007)(2906002)(6916009)(41300700001)(5660300002)(54906003)(6666004)(426003)(336012)(26005)(1076003)(186003)(36756003)(2616005)(16526019)(83380400001)(47076005)(36860700001)(82310400005)(478600001)(356005)(82740400003)(81166007)(40460700003)(40480700001)(86362001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:58:03.9724 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 74ea92ed-daa1-464e-f918-08db6b019aea 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: CY4PEPF0000E9D9.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7099 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 4CE8614000B X-Stat-Signature: hcc87ca31nbyijpe7aayo9znpsacgzbx X-Rspam-User: X-HE-Tag: 1686545886-594505 X-HE-Meta: U2FsdGVkX1+YsYR3vd7JrmWtnDeeybfLCbhmilcVAcxOmRevWNbp4Agjq1AC52JC5XvdgbYWuLAF57eDvroEZ3KiYt/kp9LEL5Hz8kfuYlXeYyhLr8NFyrAMZrGITWkuLkj926PgpPD0UmuarFtVXKjaGFU8qFvfK3QfeVZR3IFCH2aj+qhsztkZg35Olv1X4YVPDjamI6nwLxu97VOdCU5IyYfl1/ilsa8vH2HsUypcHIxWLNllK6yG6eL3uvZKcZsoCj35JtCHT09x1oXjGCsexHDIbMcnnL8GYlI/3ZgQ3vp+ggz3SKrkp8FehUTCA9eKgfZVccY5ovyx33rbcFn8pis1KZ7e3e7PWiCY5JyBX/czAL2uGEPajvBbRti4pNu9SJKohDxVLXkZiURdQ592R+7AH+OfWH7qK1ghIbWP8bCs6sjuoVs7aHpEdd5lewsf3Srun31Zvs4cb1qm0PcFPW7iBL7ScKldmMzlkZ8kmsqr8X2PVh2WZ9CQJIxrbsaYfR9Xx8yPLhH6IqRDa2CR5ew5Swlwr2K89/kPEymcYVUGjAfFqMuOAO6BU2FAbHd+eQP5CNZa36xk/gbRkLkeXenhL8nAWRuebnHx4qIu4/grN2YSgMJM8rVqW+qeINgd4svbe59XcV3v69r5/rvS3okKb4prCJo/u/tytdNgNPn/HkNQWMA3FkF/PD9er2v7U9mm5rRR+IGN4q1H4cnq+I3QvXWnlK3fCs9hkc5Xf9OoZ4Wn2UgxqPJE6loYJXHfi01n+tm8BB8Eq7bwQfVq4QbU8AMGeoGqBO885f656h5yB7QWdC7807RucPSCED5Y5kNm7cGP4TnzlsDPww24MkQEHiXkt41rcPzNmq/11JxF3WzzAky5up+xAyRF6W5usu1i97lFcmY/GaAdFhqKVkNWNzI8Kg3i1iU2MmrhMt2NJ8xYuyDGZDfx6OFEABxJ2nP3Qc4FnCEF2Im P/WIfWgv WY/6yKf3exKXWPfusR8V8I+4crovZ/I54s5fOXcpr1dqDZXvevcMphezreOO5jCFxObjRRLEutZuQMHdBPsvpz9Pw/E4ExGbvs1e586Mfzn1OCeIM3lvygCGl7FJIGcyv+BDxH56yNyjyVeMdSdiBQahU//N/DUfgE3qTlf2FV3wvaENI9MZeTkr0oWd1tgLf0GTXvMdbTP0ntt3aB0UOcWnGqTwDKeNqlXnjxJunMfmHhpsnofBOuhOhc197oRp7GFP+I/bdfckcASo2TPYL4cx7EVdfwnba3ZJmH8+gcfTnHhfjYgyjGi2fWBY4hU/mYEeOWmPT32JRRh0naSDbFPdK7P4uPGhwMp3g/mCs4heu77IYX19gch7rDI4BD33mA5Rl X-Bogosity: Ham, tests=bogofilter, spamicity=0.000230, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Currently CONFIG_HAS_CC_PLATFORM is a prereq for building anything in arch/x86/coco, but that is generally only applicable for guest support. For SEV-SNP, helpers related purely to host support will also live in arch/x86/coco. To allow for CoCo-related host support code in arch/x86/coco, move that check down into the Makefile and check for it specifically when needed. Cc: Kirill A. Shutemov Suggested-by: Tom Lendacky Signed-off-by: Michael Roth --- arch/x86/Kbuild | 2 +- arch/x86/coco/Makefile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/x86/Kbuild b/arch/x86/Kbuild index 5a83da703e87..1889cef48b58 100644 --- a/arch/x86/Kbuild +++ b/arch/x86/Kbuild @@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 -obj-$(CONFIG_ARCH_HAS_CC_PLATFORM) += coco/ +obj-y += coco/ obj-y += entry/ diff --git a/arch/x86/coco/Makefile b/arch/x86/coco/Makefile index c816acf78b6a..6aa52e719bf5 100644 --- a/arch/x86/coco/Makefile +++ b/arch/x86/coco/Makefile @@ -3,6 +3,6 @@ CFLAGS_REMOVE_core.o = -pg KASAN_SANITIZE_core.o := n CFLAGS_core.o += -fno-stack-protector -obj-y += core.o +obj-$(CONFIG_ARCH_HAS_CC_PLATFORM) += core.o obj-$(CONFIG_INTEL_TDX_GUEST) += tdx/ From patchwork Mon Jun 12 04:25:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275586 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 D3C22C7EE25 for ; Mon, 12 Jun 2023 04:58:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 70F626B0074; Mon, 12 Jun 2023 00:58:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6BEB36B0075; Mon, 12 Jun 2023 00:58:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 586986B007B; Mon, 12 Jun 2023 00:58:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 4A7986B0074 for ; Mon, 12 Jun 2023 00:58:59 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 1CB2E40002 for ; Mon, 12 Jun 2023 04:58:59 +0000 (UTC) X-FDA: 80892891198.01.4F009C3 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2068.outbound.protection.outlook.com [40.107.93.68]) by imf13.hostedemail.com (Postfix) with ESMTP id 1EE162000B for ; Mon, 12 Jun 2023 04:58:55 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=QgHULNBn; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf13.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.68 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686545936; 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=glZy7PdWi8qJOsWcUCj2WROa6to0Lr5SQ7LImMDzPlQ=; b=IpHBzVga2lT91LebYuEnqj5tUJVZe40Akuai9qyIRInncDPlL6QDdD+LJOJvrdnaZ9uYOL 4Fxe7gZLPn6HKp6hZtdsSCggbnjpBdnXhP2Xxc3YfVowoUZd7GTS9U4ixLc0rCqyzQdlZr mTn5SLOQ0Aj4ihFZzvsi+8AGteHDydA= ARC-Authentication-Results: i=2; imf13.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=QgHULNBn; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf13.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.68 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686545936; a=rsa-sha256; cv=pass; b=sI+hnijKZeDtRAOTWUvncZ5ZG9GhLHt2MdME2zoGALuic1OejhNzEnCNPv/5Aj8xg9So6B izWgWbo5M1Hqu3cSuWrtw8AB8PWp/Z2xC1pJNiyxTAhNqwawOtYACRxB5FZcyG4vJVgE7w sUIG7W3QXa2kJEWzSPQcNVFgOTL+FhQ= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lVNDhns2qWnC7dzNxhUGXl+LjT5/fEx9dW22kTcOdQssl70bqljhrSc5lYccP1O0z6ip6IJ1RO+TqpOpmTCAtKXd/YONOuiafsT4mD7zNuiv8Z7ueEj2RNZmpG3fj7kIbbqgP5n3J/3uWJ932XjeDR0k8KWasDsHD+6+swsAAOZ3K49t8H/p0tv39Wgvih9+/RAqSZESCL4fsYazbOjE+cGFZ8+duMHPLvoIDaCUW/9KJxKxMzKJ6JnqqOSkK/b6cWX10AKECHrzqNQmN/mh60rlFRJYphBd33fJ48XPzVzCPQKVRnkggIkNpCDnFCj/BYE88LTF2vpX4ymW38xvUg== 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=glZy7PdWi8qJOsWcUCj2WROa6to0Lr5SQ7LImMDzPlQ=; b=HRHBUmpUhJ1fumTaD8y19u61lCsV2HCxMYtiAMwwm4y846ZPD+S5NbF46ejNncykKmPSFQ+kexPLQ+OOvjfzLmgPwpasaeAnehgyaNeyCR4kjZNpgoSXorTwbHsoQc4O4D0QObEcEuUWKwm/LOHStvBIfTrQiS9QjnpLnHW8bYLYvlwjYWRS+JOCy2zLv5zFN8G+wHxL++2JrigtS71LOuFIa3fVm3gy1AYEeRtGcXTpiPjxBjMSnX56TcQMICOrEau+eQAA8b6tJocloMv59frl6BPGlYeV4dLT7/yPAv4EVUVMlwBJ5BecNtYDulDI5zmOj4QhNAYGfUHHBqefHA== 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 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=glZy7PdWi8qJOsWcUCj2WROa6to0Lr5SQ7LImMDzPlQ=; b=QgHULNBnG/IH8Z9FXbOkHKmxdeFTWaIvKi77Sxd4xq5Xa/vQZVHCVpDFzdN0dDN3L9+zHMYFTMa9o+jLVnVCpR/BLrwhYFMC36b9OQCVhNU4jXCyycFOUxF9zDefnciz2pzMJGLvqOGgUjobh8AdzA+BR6bw9jUWqefEdGbtpaE= Received: from MW4PR04CA0107.namprd04.prod.outlook.com (2603:10b6:303:83::22) by SA3PR12MB7829.namprd12.prod.outlook.com (2603:10b6:806:316::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.44; Mon, 12 Jun 2023 04:58:53 +0000 Received: from CO1NAM11FT078.eop-nam11.prod.protection.outlook.com (2603:10b6:303:83:cafe::56) by MW4PR04CA0107.outlook.office365.com (2603:10b6:303:83::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:58:53 +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 CO1NAM11FT078.mail.protection.outlook.com (10.13.175.177) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6455.23 via Frontend Transport; Mon, 12 Jun 2023 04:58:53 +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.23; Sun, 11 Jun 2023 23:58:49 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Jarkko Sakkinen , Ashish Kalra Subject: [PATCH RFC v9 06/51] x86/cpufeatures: Add SEV-SNP CPU feature Date: Sun, 11 Jun 2023 23:25:14 -0500 Message-ID: <20230612042559.375660-7-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CO1NAM11FT078:EE_|SA3PR12MB7829:EE_ X-MS-Office365-Filtering-Correlation-Id: fc23a14d-6efd-4d36-2bca-08db6b01b86a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: yhydRVHiUiYjT4p746BxUSPDAhj9l1dvJ9tTzkkIf2g2kydVfOY/iqTEi5/xlWpnsVRsNw5u6DUF6wxByPLCo0vmhMQz3xkjoNF5lXGJoPzk6j81PFa/XbqfBh7oxEZg7bkFyW6XQbLCnn2/JLd66Gbx238SmF0gbWMBHteor56NWW4F6XnkUdf7Fb54vk2rtYOPv3lcO2recETGHATvAiQeo1yNF1wzcL68XcT1cvrdO5kph8IAntVHhTNElwaOvDvB70Ry5Di2mqTQHcgc95vXJGbthzB/WX1yxUbTHT438a+YN+f4l/uKS722f0DUAeN2igRsAar4RVw0taRnWoJpqjD1pT5AFpNRdJRFNz3eJzdHFP1CBPDmyoNUeu8Su+gGPZUfL1/dGJmHSbiBCQ4UQY0Jyoi0z06sXePp/kb7orfB5P3r3IQL7DH4Z6BRqLfAEBOWvaRknrKDhvHQtcI2MT9wQofqc4xJ4tGyZwTyEG6QzuMdtXlWKwzpLqIFOwAPCmuPtVrlPWVALb95UMk9t6ZyirgmKLOaZj9bSzlGOEqn2sqHzaQvSKQHiL36gpkQxvPmVSIiPcFONml6J/yGQ2YuGDnR4nNWiVf/H2iQ41coXh9Sk3MoOQ5uoR0s7PWgEr2Z+bFEkZMWET0b+HAojMx3rUAzkEjwB9Py2Zxrz/ma5pjPiJf3IouAp9FWYww/8nYa/hlJ97h3qQswoTDn8RkE5lnfb4OFcdkLVKUsqaZXb8sx2Q18QRiimZO+nhyOnnZ3zXRbWO9nvCeLLQ== 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:(13230028)(4636009)(39860400002)(136003)(346002)(396003)(376002)(451199021)(36840700001)(40470700004)(46966006)(336012)(426003)(2616005)(47076005)(83380400001)(36860700001)(82740400003)(356005)(40480700001)(86362001)(82310400005)(81166007)(36756003)(40460700003)(478600001)(54906003)(44832011)(4326008)(6666004)(8936002)(8676002)(2906002)(5660300002)(7416002)(7406005)(70206006)(70586007)(6916009)(41300700001)(316002)(16526019)(186003)(1076003)(26005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:58:53.4756 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fc23a14d-6efd-4d36-2bca-08db6b01b86a 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: CO1NAM11FT078.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA3PR12MB7829 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 1EE162000B X-Stat-Signature: ufecms8uczdnjhea4odxiifna4r6bg1m X-Rspam-User: X-HE-Tag: 1686545935-752762 X-HE-Meta: U2FsdGVkX19Sy8PFxGVUO8L99YKyf66YBsdInmoL0vzXXP3VbMVD9KRPCd/0w34JKmv8LYQ88MIn1vHd7rZ+ffyZE3CfSAzsI1UTYso1gD7kW/25Lc6xo5klviUiOggFHyAJfodE026NEUcciBv8WvvqFmzWrTjm55nJMVAUtmpgQz6EtWB5ixmGJTPPjFBTi3Cl4UeR6CvrzvfN5bxrKYTimKITzNX1BrXPa/f4qefvCgOnRlyqd5WowX1PL7X6+M7S1jXaXEYv8llelvee7NtR4CjOhVeQhUsM2ywOajKhl/9CbrDHJ+h5KY+Q6wHtyDGhIM4HLHjRG/HTnzgI4Ukp8DpsO8DX0bPka1FjhlM699fqYrJTjA7K6MBsmeQEW4FxNbbRLytLag5l1lS8cqIshQqvfnyE8tlYozNlukKHpRuBgjurckefPbJe9cbuz6av/ptKCLanI17ZLUE/6CD6FZEGMV6T0wTD/UHXwjKdT0H2RuqhD7XMOavnt0J6iWKzzsa/ON7Er6ve/PsFtB1vvJgQEKXL5T92LF3Yzw8cP++vwBYIp9XqId/ZQNE88cv6ZTCU4CrrXLDUtJmDU4Csp80wTxMRCB3LngcL9lr7X+Hr8dEMUwwVe1qdmoFf80aB8mI9B21UEMqKxwAYhP4BD8eCoddSYTCHmE7iHsWpJ7+mP8My7NGw3EsL/VWDhsXq8mc/KkeSbyPsySNCdZsXjIh9CDGbDAcOrL7FJkSUah4ei+SUP7fgoEII/4bl7ktlcuQEV9ZlOG6hNuZEjKUs6nsWr5IS9VeGZiEo6MJY4UvpztQZSdn6Xr8fuyICJ+T6R2WXWZ58B4FHjqEHz3cTL2/L/bKb5NmmQegITkOpXkBIDsL2lWHgFZXJkCsZDsTtSQCZ4eYnit++0fUL1aUVmp/9hSjMP3kcVK92KXeY2ahEpHjVmDMA80JM6Sgn09pKimQD5LstNolbhsP U7Yv51ss 8BVTLSGB5zPXV3RT1L+EL9F8pd7/WnIJ+DI9T5PC9mXVIIqhhJYDsKkMtrZMjB7OzQhX5/jeVSlzkPlgb57C8NpuLiRQNWc26V7DcAoOB3lJmdsfcjFgpaT9r5XP0Znye/jR92s8VR6REAPloQf+Y4mhVykG207iSiNgmHZAXSkPhCaBKv3//W8E2rpo5pO33h3JAxyJhUO2UfF7G9TqNyARY/YyDxWRXzkS7f8sZLGCX9cMxNc6BseEBUiAifmtivDIywc/MPrPuSRMKQzHD0mS9YU/pQBxjcKmgy3GNkfKG7bsWsiofiakdUzY3NiLblySNZRbpYgDbwVAd/JmZRhuufjbXTq68+g0N8uG83zGGLGU+acZdLWdOxQ== 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: From: Brijesh Singh Add CPU feature detection for Secure Encrypted Virtualization with Secure Nested Paging. This feature adds a strong memory integrity protection to help prevent malicious hypervisor-based attacks like data replay, memory re-mapping, and more. Signed-off-by: Brijesh Singh Signed-off-by: Jarkko Sakkinen Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/kernel/cpu/amd.c | 5 +++-- tools/arch/x86/include/asm/cpufeatures.h | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h index 97327a1e3aff..b60b32f47884 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -431,6 +431,7 @@ #define X86_FEATURE_SEV (19*32+ 1) /* AMD Secure Encrypted Virtualization */ #define X86_FEATURE_VM_PAGE_FLUSH (19*32+ 2) /* "" VM Page Flush MSR is supported */ #define X86_FEATURE_SEV_ES (19*32+ 3) /* AMD Secure Encrypted Virtualization - Encrypted State */ +#define X86_FEATURE_SEV_SNP (19*32+ 4) /* AMD Secure Encrypted Virtualization - Secure Nested Paging */ #define X86_FEATURE_V_TSC_AUX (19*32+ 9) /* "" Virtual TSC_AUX */ #define X86_FEATURE_SME_COHERENT (19*32+10) /* "" AMD hardware-enforced cache coherency */ diff --git a/arch/x86/kernel/cpu/amd.c b/arch/x86/kernel/cpu/amd.c index 95cdd08c4cbb..a79774181f22 100644 --- a/arch/x86/kernel/cpu/amd.c +++ b/arch/x86/kernel/cpu/amd.c @@ -558,8 +558,8 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) * SME feature (set in scattered.c). * If the kernel has not enabled SME via any means then * don't advertise the SME feature. - * For SEV: If BIOS has not enabled SEV then don't advertise the - * SEV and SEV_ES feature (set in scattered.c). + * For SEV: If BIOS has not enabled SEV then don't advertise SEV and + * any additional functionality based on it. * * In all cases, since support for SME and SEV requires long mode, * don't advertise the feature under CONFIG_X86_32. @@ -594,6 +594,7 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) clear_sev: setup_clear_cpu_cap(X86_FEATURE_SEV); setup_clear_cpu_cap(X86_FEATURE_SEV_ES); + setup_clear_cpu_cap(X86_FEATURE_SEV_SNP); } } diff --git a/tools/arch/x86/include/asm/cpufeatures.h b/tools/arch/x86/include/asm/cpufeatures.h index b89005819cd5..b4f11a500470 100644 --- a/tools/arch/x86/include/asm/cpufeatures.h +++ b/tools/arch/x86/include/asm/cpufeatures.h @@ -424,6 +424,7 @@ #define X86_FEATURE_SEV (19*32+ 1) /* AMD Secure Encrypted Virtualization */ #define X86_FEATURE_VM_PAGE_FLUSH (19*32+ 2) /* "" VM Page Flush MSR is supported */ #define X86_FEATURE_SEV_ES (19*32+ 3) /* AMD Secure Encrypted Virtualization - Encrypted State */ +#define X86_FEATURE_SEV_SNP (19*32+ 4) /* AMD Secure Encrypted Virtualization - Secure Nested Paging */ #define X86_FEATURE_V_TSC_AUX (19*32+ 9) /* "" Virtual TSC_AUX */ #define X86_FEATURE_SME_COHERENT (19*32+10) /* "" AMD hardware-enforced cache coherency */ From patchwork Mon Jun 12 04:25:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275587 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 40311C7EE25 for ; Mon, 12 Jun 2023 04:59:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D9DC16B0075; Mon, 12 Jun 2023 00:59:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D4E516B0078; Mon, 12 Jun 2023 00:59:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C16666B007B; Mon, 12 Jun 2023 00:59:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id B3FF96B0075 for ; Mon, 12 Jun 2023 00:59:47 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 7604DA00AE for ; Mon, 12 Jun 2023 04:59:46 +0000 (UTC) X-FDA: 80892893172.23.4BCB46D Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2056.outbound.protection.outlook.com [40.107.94.56]) by imf07.hostedemail.com (Postfix) with ESMTP id 0415740011 for ; Mon, 12 Jun 2023 04:59:42 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=fsM19JYE; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf07.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.56 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686545983; 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=sQGmwA3MslzJEd0Pg5pUDqAuG/ZGYX7PxVZ9iQIDSmY=; b=uWjbCylGk79gOhmv3ooCTa0tMo541U4s9WchZkH+KZbLA7vYa14lZdZsP/r+1DHR7bbPfl bKH8RkL04b3JN+ymCHqGsdYpK8c3D8YB6w2ShrCp6dDNKeO+7TmGh5XKpHKcPyn321kb9o oluG5NDzhaNBL1zngbi+eybKXFA4BlI= ARC-Authentication-Results: i=2; imf07.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=fsM19JYE; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf07.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.56 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686545983; a=rsa-sha256; cv=pass; b=Pgfvcu/nJRIl2L/FGfnzqotVys16/vR8Ayaeprf4MGiQTEzEfkRMl3jiRm3T/sfkZAvKMP sTT6zdAQwidlg0/LVxLv1CNNCucnRe42BnkPdjkEYUKYagfJldiN3N75NDEZIO5wfK31Rg mzSqxNTLz6aJwe8B2o0/TxktA3htoM0= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MBmt2jieVjIJ9OLFCLOWjkwpRsxKrmrz+6skWOzuLb3eLD2hEcU/tULkZxHuROuNnOw5ScOhFt7VXb0E7NfvHhooS5r0eIoQe0iOSqI4Pp52rD0FZ1iYvJ1Re/o+A3HYn7fb42Q2UU2f8KD+Mxsw9vzZewJwtvt4sKvyf/mYi8Y46qM8jW3mcoZNNYQqLZ+BwOtSkTXWcm+pgHCYzrL7EnxOjVBB2krqNkS3I50xtAiWpCoECPs1+aQOJ0c9I47ixr7U67aM9juQvmwF76mX5l2y8HrDEx4wajpBeHV2wpMfkSRSIhaTFL8AjS5SCD7q0m8E5v6rMTPA+1HQPfqcgg== 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=sQGmwA3MslzJEd0Pg5pUDqAuG/ZGYX7PxVZ9iQIDSmY=; b=J/VhEUHrApEKClXN3/OaILzx4FQwW8Drfm6cHnzMrfWhLNYfbbsgV8FSHFSmOp8Y9EIwnOxLMFqaQQgqiELUoS2WXKq/aCMoo9pLEI3/hvtFS5/SPOck6pdPNnrFEoDXI9qEesDtcEg7h7tquxDUKatcyPbVHSqTBBA97+3eYl6q1XVBIJLkCPv5z8TJoVQ0TomKD4J/M4APRdqXOcDFts8y8C1MK9nPdFEQtrWFIpL7A9LWbYthBaYqCjK9THuVwiX2o2Ls0HCiBtEHjvhmhrwbMVIfIinaBxs7DeV4Sln7YgdNp2/yj1YrZHI7+8Od5Qun/bhOFsmfwzLP6tKYmw== 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 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=sQGmwA3MslzJEd0Pg5pUDqAuG/ZGYX7PxVZ9iQIDSmY=; b=fsM19JYEGYeC5J1q3jnknNK9vBDKUnFyxg+RP1FKNl+zqTAvAm3lcg/ofl1Qf3/uX5GNI1DeYG7XEwlVIbfyG054ZxL5wKaWCxZmQleLurxR3x6mCfHvxlbA26Ssiw4HQnutgoLdVEFMEekKfrazas1/V7k4G9YeSVc71WN+tGU= Received: from MW4PR04CA0238.namprd04.prod.outlook.com (2603:10b6:303:87::33) by BN9PR12MB5161.namprd12.prod.outlook.com (2603:10b6:408:11a::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.41; Mon, 12 Jun 2023 04:59:40 +0000 Received: from CO1NAM11FT103.eop-nam11.prod.protection.outlook.com (2603:10b6:303:87:cafe::96) by MW4PR04CA0238.outlook.office365.com (2603:10b6:303:87::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:59:39 +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 CO1NAM11FT103.mail.protection.outlook.com (10.13.174.252) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.31 via Frontend Transport; Mon, 12 Jun 2023 04:59:39 +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.23; Sun, 11 Jun 2023 23:59:33 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 07/51] x86/sev: Add the host SEV-SNP initialization support Date: Sun, 11 Jun 2023 23:25:15 -0500 Message-ID: <20230612042559.375660-8-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CO1NAM11FT103:EE_|BN9PR12MB5161:EE_ X-MS-Office365-Filtering-Correlation-Id: 0eb86ce6-0fb1-461d-7576-08db6b01d3b8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bBJ4PsVpKpraMgA2z/oBsrD4Jqhcqh2JokcwUWxV56BTRPKrbl2jkQIsk5Gcc4cPymDpj+Vs2hK4XrWdkfzYaoFbTgeC6yEEo7Yy1DWmrJHXM9GRkIRXneFqGL3CbLTAsPRsALJL/MyI8A8iPllh1qewCRoD0UWFeoK4trz92WIGZZUq7bDNVDZeJGPJE7zM71haVXEky17iaz0EQY8ARqghDGOZbg6C1O6D8XGd9T4GjIqaS2DoqVhUHnHBkFOdEE/+hcqq5MzJmbmNXMoVb1W4o/tl/74GgMNHgYNamSV8gXUEPtE9MSsrrqtwt7pTjnb5BPxHuWR7Sjr0L5dWAdJmlg1Ye7+w8TNTSjrMvYrTzhmqCxu7CrfJBzvQp3lOSoomGoNCQmTO/bLQgD16ghc/pSIOI5/nQIiNPw+5AL3SleotwPN4eQams5CytyVMUGWJ3ZUt5F6PD9Yg+c0UcxWAIaKf1j5khhd2xamgNaSZu6LFWlHRQyo/flRnY4fiVnRsPuVyY8UeSLD1Bv+DDDfKEmxiQSMugXT1NRtOiF7GJRexmTAdhJZPdy0MlYiUDTOHH293sQ8kZ/qVGgBhZoqTAZv9QKV4A8NCh/vBEvN0eYMyg7gIpgwLlJZzijjvpUmVFCtrBuMfumUTAATUlxDyJVQij+p3uOJHGMOxj1qk428XvrYuSEQb/3rs/w6NPEqmIgfGPqTQfhqKCUJd49bzurLRVH1gVkgyRN+8FPS5zHh6SpUOSMOJ88LjUPjE+cN7N/YZ8CmE8Klm7uWPpQ== 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:(13230028)(4636009)(376002)(396003)(346002)(136003)(39860400002)(451199021)(36840700001)(40470700004)(46966006)(8936002)(8676002)(44832011)(5660300002)(7406005)(7416002)(4326008)(6916009)(70586007)(70206006)(316002)(54906003)(2906002)(41300700001)(30864003)(36860700001)(40460700003)(6666004)(478600001)(81166007)(40480700001)(356005)(26005)(1076003)(16526019)(36756003)(426003)(336012)(83380400001)(186003)(47076005)(2616005)(82740400003)(82310400005)(86362001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:59:39.2715 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0eb86ce6-0fb1-461d-7576-08db6b01d3b8 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: CO1NAM11FT103.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5161 X-Rspamd-Queue-Id: 0415740011 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 6yarjkhqo3oyixt86uzzfkarznfnjnsz X-HE-Tag: 1686545982-486981 X-HE-Meta: U2FsdGVkX18uo02pLw/VqZl9fJ3OhfoYxvwPOomcMUV3f9UeCMkE1aFsETnnEhNcnMzmiJF33juiBFN3+cS5oICMCl35xOWOILkdtSW68AF+hiuN2x8BKSP4ZXezXZlMpkkoh72jcdLBnNxehORLE7baDOtWpjmZW6h148MVdou72ouYXJCiQF8qDHI13f83cIyFmJKZDxfIb9khdZZMMksyLIXq/TIQa5sbfgE1522u+ejhtmlpOAUMZCBlKtm2htu1cukB/cJhBrmDIlRuwvlveCBgEuteq7NOe8ZA5vB6Dop10GqKGm8Jjp8i3L/s4cflm/GehRfIdfFXm0HWzTSUykwNWQMBV6EmqJfdWfqqj7c2nekxKc4tZ33h53oiXaySPhWrH1BJclPSGClrBf5vK4O6QbVljJkLF+w5Kg1LXkJM/8QzY0/PljPVSv+5Dx9HUk9uI7EEDin5mbB/i72ja+tKKaVpY/D5+PD+A9x/u0Uj0R6aIxhn0KVpiUjT03TX97Jo4QAV5Gc8hj/6SRSpoDUYtjNoKhq9aMe+X3+XvUXaKtAJd2yoQ6a75U25+2PHg1uqClU2pWm9vD7bdTt2nnP3apusp1siXlrUxPZ1prbDwOo2CfIZ892ExBt6fZ+mjZdeex4Nd6sgEzVqe/zNZCr4rDS61b9lumhtQuvTksPbnFJeGb6aZXazYLbG7Ef89CTFl5ArL6EHZYyAociqcGao0oQGdApwsuA4lYQB0uOhjoe/C7PNyCR7vBeSSkvTxPemoK568FXTrT9nxAM8dC8jLB49gP7uBQJXla18BHN8TwH3Drln5S6MVWCxrk82JuGIM5rRUnt5yGo+L2JwR6490OwL/V/9aYRFdAPVJqJI89Cj3HGRUbht64u+jct19zgUvcJWBhX+TZ4GRznv6zb8uu53mb4qzPoQknThvRUaN99OLP25z7bsAEnSfh0qt2Tn+aUx1FDGi+e p6brhVee lNEGEyr2zOJED+sZiGA7Sz995ptrIIpxUD5NuuE0wVWeB33gvopz2h4ghkFSqavvB+KggDpv0OTGwFPNzSfl1kTdV6As4ucktSZare8oiBJ164DDXclHHxommFbstsYDlVP+R10hRH3txJXLHGouSnwUOLNP3QYh6vPuDr+APDQfJvQRtnTLxk0VoYB2G5fLLR+LFWUZekzCrRnNG3DnfwXdVYZPKQLMCQw0Bi/8lDVmazbc/2/A+0RdBdUxzVUWzcY62YQfOe20uEzgG/RHbppSzwk4tZxKInVVizIXrCDXdLyNxgOw6gdhD7w== 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: From: Brijesh Singh The memory integrity guarantees of SEV-SNP are enforced through a new structure called the Reverse Map Table (RMP). The RMP is a single data structure shared across the system that contains one entry for every 4K page of DRAM that may be used by SEV-SNP VMs. APM2 section 15.36 details a number of steps needed to detect/enable SEV-SNP and RMP table support on the host: - Detect SEV-SNP support based on CPUID bit - Initialize the RMP table memory reported by the RMP base/end MSR registers and configure IOMMU to be compatible with RMP access restrictions - Set the MtrrFixDramModEn bit in SYSCFG MSR - Set the SecureNestedPagingEn and VMPLEn bits in the SYSCFG MSR - Configure IOMMU RMP table entry format is non-architectural and it can vary by processor. It is defined by the PPR. Restrict SNP support to CPU models/families which are compatible with the current RMP table entry format to guard against any undefined behavior when running on other system types. Future models/support will handle this through an architectural mechanism to allow for broader compatibility. SNP host code depends on CONFIG_KVM_AMD_SEV config flag, which may be enabled even when CONFIG_AMD_MEM_ENCRYPT isn't set, so update the SNP-specific IOMMU helpers used here to rely on CONFIG_KVM_AMD_SEV instead of CONFIG_AMD_MEM_ENCRYPT. Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra Co-developed-by: Tom Lendacky Signed-off-by: Tom Lendacky Signed-off-by: Brijesh Singh [mdr: rework commit message to be clearer about what patch does, squash in early_rmptable_check() handling from Tom] Signed-off-by: Michael Roth --- arch/x86/coco/Makefile | 1 + arch/x86/coco/sev/Makefile | 3 + arch/x86/coco/sev/host.c | 212 +++++++++++++++++++++++ arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/msr-index.h | 11 +- arch/x86/include/asm/sev.h | 2 + arch/x86/kernel/cpu/amd.c | 19 ++ drivers/iommu/amd/init.c | 2 +- include/linux/amd-iommu.h | 2 +- 9 files changed, 256 insertions(+), 4 deletions(-) create mode 100644 arch/x86/coco/sev/Makefile create mode 100644 arch/x86/coco/sev/host.c diff --git a/arch/x86/coco/Makefile b/arch/x86/coco/Makefile index 6aa52e719bf5..6a7d876130e2 100644 --- a/arch/x86/coco/Makefile +++ b/arch/x86/coco/Makefile @@ -6,3 +6,4 @@ CFLAGS_core.o += -fno-stack-protector obj-$(CONFIG_ARCH_HAS_CC_PLATFORM) += core.o obj-$(CONFIG_INTEL_TDX_GUEST) += tdx/ +obj-$(CONFIG_KVM_AMD_SEV) += sev/ diff --git a/arch/x86/coco/sev/Makefile b/arch/x86/coco/sev/Makefile new file mode 100644 index 000000000000..27c0500d75c8 --- /dev/null +++ b/arch/x86/coco/sev/Makefile @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-y += host.o diff --git a/arch/x86/coco/sev/host.c b/arch/x86/coco/sev/host.c new file mode 100644 index 000000000000..6907ce887b23 --- /dev/null +++ b/arch/x86/coco/sev/host.c @@ -0,0 +1,212 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * AMD SVM-SEV Host Support. + * + * Copyright (C) 2023 Advanced Micro Devices, Inc. + * + * Author: Ashish Kalra + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * The first 16KB from the RMP_BASE is used by the processor for the + * bookkeeping, the range needs to be added during the RMP entry lookup. + */ +#define RMPTABLE_CPU_BOOKKEEPING_SZ 0x4000 + +static unsigned long rmptable_start __ro_after_init; +static unsigned long rmptable_end __ro_after_init; + +#undef pr_fmt +#define pr_fmt(fmt) "SEV-SNP: " fmt + +static int __mfd_enable(unsigned int cpu) +{ + u64 val; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return 0; + + rdmsrl(MSR_AMD64_SYSCFG, val); + + val |= MSR_AMD64_SYSCFG_MFDM; + + wrmsrl(MSR_AMD64_SYSCFG, val); + + return 0; +} + +static __init void mfd_enable(void *arg) +{ + __mfd_enable(smp_processor_id()); +} + +static int __snp_enable(unsigned int cpu) +{ + u64 val; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return 0; + + rdmsrl(MSR_AMD64_SYSCFG, val); + + val |= MSR_AMD64_SYSCFG_SNP_EN; + val |= MSR_AMD64_SYSCFG_SNP_VMPL_EN; + + wrmsrl(MSR_AMD64_SYSCFG, val); + + return 0; +} + +static __init void snp_enable(void *arg) +{ + __snp_enable(smp_processor_id()); +} + +bool snp_get_rmptable_info(u64 *start, u64 *len) +{ + u64 max_rmp_pfn, calc_rmp_sz, rmp_sz, rmp_base, rmp_end; + + rdmsrl(MSR_AMD64_RMP_BASE, rmp_base); + rdmsrl(MSR_AMD64_RMP_END, rmp_end); + + if (!rmp_base || !rmp_end) { + pr_err("Memory for the RMP table has not been reserved by BIOS\n"); + return false; + } + + rmp_sz = rmp_end - rmp_base + 1; + + /* + * Calculate the amount the memory that must be reserved by the BIOS to + * address the whole RAM, including the bookkeeping area. The RMP itself + * must also be covered. + */ + max_rmp_pfn = max_pfn; + if (PHYS_PFN(rmp_end) > max_pfn) + max_rmp_pfn = PHYS_PFN(rmp_end); + + calc_rmp_sz = (max_rmp_pfn << 4) + RMPTABLE_CPU_BOOKKEEPING_SZ; + + if (calc_rmp_sz > rmp_sz) { + pr_err("Memory reserved for the RMP table does not cover full system RAM (expected 0x%llx got 0x%llx)\n", + calc_rmp_sz, rmp_sz); + return false; + } + + *start = rmp_base; + *len = rmp_sz; + + return true; +} + +static __init int __snp_rmptable_init(void) +{ + u64 rmp_base, sz; + void *start; + u64 val; + + if (!snp_get_rmptable_info(&rmp_base, &sz)) + return 1; + + pr_info("RMP table physical address [0x%016llx - 0x%016llx]\n", + rmp_base, rmp_base + sz - 1); + + start = memremap(rmp_base, sz, MEMREMAP_WB); + if (!start) { + pr_err("Failed to map RMP table addr 0x%llx size 0x%llx\n", rmp_base, sz); + return 1; + } + + /* + * Check if SEV-SNP is already enabled, this can happen in case of + * kexec boot. + */ + rdmsrl(MSR_AMD64_SYSCFG, val); + if (val & MSR_AMD64_SYSCFG_SNP_EN) + goto skip_enable; + + /* Initialize the RMP table to zero */ + memset(start, 0, sz); + + /* Flush the caches to ensure that data is written before SNP is enabled. */ + wbinvd_on_all_cpus(); + + /* MFDM must be enabled on all the CPUs prior to enabling SNP. */ + on_each_cpu(mfd_enable, NULL, 1); + + /* Enable SNP on all CPUs. */ + on_each_cpu(snp_enable, NULL, 1); + +skip_enable: + rmptable_start = (unsigned long)start; + rmptable_end = rmptable_start + sz - 1; + + return 0; +} + +static int __init snp_rmptable_init(void) +{ + int family, model; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return 0; + + family = boot_cpu_data.x86; + model = boot_cpu_data.x86_model; + + /* + * RMP table entry format is not architectural and it can vary by processor and + * is defined by the per-processor PPR. Restrict SNP support on the known CPU + * model and family for which the RMP table entry format is currently defined for. + */ + if (!(family == 0x19 && model <= 0xaf) && !(family == 0x1a && model <= 0xf)) + goto nosnp; + + if (amd_iommu_snp_enable()) + goto nosnp; + + if (__snp_rmptable_init()) + goto nosnp; + + cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/rmptable_init:online", __snp_enable, NULL); + + return 0; + +nosnp: + setup_clear_cpu_cap(X86_FEATURE_SEV_SNP); + return -ENOSYS; +} + +/* + * This must be called after the PCI subsystem. This is because amd_iommu_snp_enable() + * is called to ensure the IOMMU supports the SEV-SNP feature, which can only be + * called after subsys_initcall(). + * + * NOTE: IOMMU is enforced by SNP to ensure that hypervisor cannot program DMA + * directly into guest private memory. In case of SNP, the IOMMU ensures that + * the page(s) used for DMA are hypervisor owned. + */ +fs_initcall(snp_rmptable_init); diff --git a/arch/x86/include/asm/disabled-features.h b/arch/x86/include/asm/disabled-features.h index 5dfa4fb76f4b..0a9938aea305 100644 --- a/arch/x86/include/asm/disabled-features.h +++ b/arch/x86/include/asm/disabled-features.h @@ -99,6 +99,12 @@ # define DISABLE_TDX_GUEST (1 << (X86_FEATURE_TDX_GUEST & 31)) #endif +#ifdef CONFIG_KVM_AMD_SEV +# define DISABLE_SEV_SNP 0 +#else +# define DISABLE_SEV_SNP (1 << (X86_FEATURE_SEV_SNP & 31)) +#endif + /* * Make sure to add features to the correct mask */ @@ -123,7 +129,7 @@ DISABLE_ENQCMD) #define DISABLED_MASK17 0 #define DISABLED_MASK18 0 -#define DISABLED_MASK19 0 +#define DISABLED_MASK19 (DISABLE_SEV_SNP) #define DISABLED_MASK20 0 #define DISABLED_MASK_CHECK BUILD_BUG_ON_ZERO(NCAPINTS != 21) diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h index ad35355ee43e..db0f3a041930 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -574,6 +574,8 @@ #define MSR_AMD64_SEV_ENABLED BIT_ULL(MSR_AMD64_SEV_ENABLED_BIT) #define MSR_AMD64_SEV_ES_ENABLED BIT_ULL(MSR_AMD64_SEV_ES_ENABLED_BIT) #define MSR_AMD64_SEV_SNP_ENABLED BIT_ULL(MSR_AMD64_SEV_SNP_ENABLED_BIT) +#define MSR_AMD64_RMP_BASE 0xc0010132 +#define MSR_AMD64_RMP_END 0xc0010133 /* SNP feature bits enabled by the hypervisor */ #define MSR_AMD64_SNP_VTOM BIT_ULL(3) @@ -675,7 +677,14 @@ #define MSR_K8_TOP_MEM2 0xc001001d #define MSR_AMD64_SYSCFG 0xc0010010 #define MSR_AMD64_SYSCFG_MEM_ENCRYPT_BIT 23 -#define MSR_AMD64_SYSCFG_MEM_ENCRYPT BIT_ULL(MSR_AMD64_SYSCFG_MEM_ENCRYPT_BIT) +#define MSR_AMD64_SYSCFG_MEM_ENCRYPT BIT_ULL(MSR_AMD64_SYSCFG_MEM_ENCRYPT_BIT) +#define MSR_AMD64_SYSCFG_SNP_EN_BIT 24 +#define MSR_AMD64_SYSCFG_SNP_EN BIT_ULL(MSR_AMD64_SYSCFG_SNP_EN_BIT) +#define MSR_AMD64_SYSCFG_SNP_VMPL_EN_BIT 25 +#define MSR_AMD64_SYSCFG_SNP_VMPL_EN BIT_ULL(MSR_AMD64_SYSCFG_SNP_VMPL_EN_BIT) +#define MSR_AMD64_SYSCFG_MFDM_BIT 19 +#define MSR_AMD64_SYSCFG_MFDM BIT_ULL(MSR_AMD64_SYSCFG_MFDM_BIT) + #define MSR_K8_INT_PENDING_MSG 0xc0010055 /* C1E active bits in int pending message */ #define K8_INTP_C1E_ACTIVE_MASK 0x18000000 diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index ebc271bb6d8e..d34c46db7dd1 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -197,6 +197,7 @@ void snp_set_wakeup_secondary_cpu(void); bool snp_init(struct boot_params *bp); void __init __noreturn snp_abort(void); int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, unsigned long *fw_err); +bool snp_get_rmptable_info(u64 *start, u64 *len); #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } static inline void sev_es_ist_exit(void) { } @@ -221,6 +222,7 @@ static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *in { return -ENOTTY; } +static inline bool snp_get_rmptable_info(u64 *start, u64 *len) { return false; } #endif #endif diff --git a/arch/x86/kernel/cpu/amd.c b/arch/x86/kernel/cpu/amd.c index a79774181f22..1493ddf89fdf 100644 --- a/arch/x86/kernel/cpu/amd.c +++ b/arch/x86/kernel/cpu/amd.c @@ -20,6 +20,7 @@ #include #include #include +#include #ifdef CONFIG_X86_64 # include @@ -546,6 +547,20 @@ static void bsp_init_amd(struct cpuinfo_x86 *c) resctrl_cpu_detect(c); } +static bool early_rmptable_check(void) +{ + u64 rmp_base, rmp_size; + + /* + * For early BSP initialization, max_pfn won't be set up yet, wait until + * it is set before performing the RMP table calculations. + */ + if (!max_pfn) + return true; + + return snp_get_rmptable_info(&rmp_base, &rmp_size); +} + static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) { u64 msr; @@ -587,6 +602,9 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) if (!(msr & MSR_K7_HWCR_SMMLOCK)) goto clear_sev; + if (cpu_has(c, X86_FEATURE_SEV_SNP) && !early_rmptable_check()) + goto clear_snp; + return; clear_all: @@ -594,6 +612,7 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) clear_sev: setup_clear_cpu_cap(X86_FEATURE_SEV); setup_clear_cpu_cap(X86_FEATURE_SEV_ES); +clear_snp: setup_clear_cpu_cap(X86_FEATURE_SEV_SNP); } } diff --git a/drivers/iommu/amd/init.c b/drivers/iommu/amd/init.c index 19a46b9f7357..33ea62d93540 100644 --- a/drivers/iommu/amd/init.c +++ b/drivers/iommu/amd/init.c @@ -3665,7 +3665,7 @@ int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true); } -#ifdef CONFIG_AMD_MEM_ENCRYPT +#ifdef CONFIG_KVM_AMD_SEV int amd_iommu_snp_enable(void) { /* diff --git a/include/linux/amd-iommu.h b/include/linux/amd-iommu.h index 953e6f12fa1c..8f0cde2d451c 100644 --- a/include/linux/amd-iommu.h +++ b/include/linux/amd-iommu.h @@ -206,7 +206,7 @@ int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value); struct amd_iommu *get_amd_iommu(unsigned int idx); -#ifdef CONFIG_AMD_MEM_ENCRYPT +#ifdef CONFIG_KVM_AMD_SEV int amd_iommu_snp_enable(void); #endif From patchwork Mon Jun 12 04:25:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275588 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 22C70C7EE25 for ; Mon, 12 Jun 2023 05:00:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B7BA86B0075; Mon, 12 Jun 2023 01:00:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ADD356B0078; Mon, 12 Jun 2023 01:00:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 97E9C6B007B; Mon, 12 Jun 2023 01:00:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 82D0C6B0075 for ; Mon, 12 Jun 2023 01:00:08 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 5921C16010B for ; Mon, 12 Jun 2023 05:00:08 +0000 (UTC) X-FDA: 80892894096.28.582A295 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2082.outbound.protection.outlook.com [40.107.220.82]) by imf21.hostedemail.com (Postfix) with ESMTP id 547D11C0009 for ; Mon, 12 Jun 2023 05:00:04 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=GcxrSPZv; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf21.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.82 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686546005; 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=JkCAa24YyiWlkukD/LaO5dOR1Z6gqOaYm5Qyl4UEKYk=; b=eygMK0aaN9lxfF3juGNB6H3HyI8lFEpeUP1qXjkD5zUcxka1I0A2OfIM61Tg93LGPeI9gh iqaZTeolqDblnJrUUxSGTI+MHEtVQqKEJ0Djk4V1dM7UDqB78qQh7T3yEBCX+fnJBcnWkZ bwLSeqoGA1uF4vQhNKctsgGEtf/bvsw= ARC-Authentication-Results: i=2; imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=GcxrSPZv; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf21.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.82 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686546005; a=rsa-sha256; cv=pass; b=B+YEGl1YJuNQSjT80UCBIlrTg47UUd0Xqp2KFD+gpdX3vG14ZqQo9fOIrxnjZ1KIgMcysP j58QPXawm4e3LIkaQdI8x7cRUnXn+Id+RQq90OKFqVEtFJZEVa0+A32roz5vVYyG+6R8A+ QlHO7cEKL0c7pHGGtJxiBdfs4zifeqA= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nGKg4VRglm7w5OTKYyS0K1SdAzDUmCd/9Ld80KGxPNgUly0//ZLWuKdOd+trJX1RTUipKcf3C458Ra6LmEmyLDg1l1V5M9pFnlUS+yZAjm/KDQIaauL62F5QqTCZTZKc2dsr5vRI+8+Czd5WNXh+vDXO83OTMBT3+oC3z3eRbUz4X8sxIBv1d2AL4u5Kamhq7holAS+by4b1xbqw7nG3S31Nb0BHBj3U4i1fV9MTkUs6Or5jwpSDl+bdiH+p3pVolHYohxT7Egt2sjtE8zEFy7y34vzgYthf8gFtGgvpX/6j2WtxXxr+tQ6ctBUiNr/9ye2zqu9axjKkmlWeD7zH4w== 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=JkCAa24YyiWlkukD/LaO5dOR1Z6gqOaYm5Qyl4UEKYk=; b=AXP82GuEEG5gjQda9i0jKKagsCn1/axvwc//tHsK3wAZjcoGZem7q5cyPCRVdGTvzTtMmuLuIKFk+YaPlU6pcgOWfsCxBzI22XqCH3CvLVrZN61JHmYpoJB5YvCawO0BuYY82aTc4UdR/tvIje4vgtldnR5A+20ujlpeWz7MEHIOc8wbIDe7NtgqTQCPVuVV974gDWzG64YOnA4CiuwZ+kAb9k/N+WyhQUpQzhl0plC5EEkuDVikEl3tzCuYqdQDKMPWsYaRW7tMZbdRqA6M2Xewif0V9v9vj3WDRUYBko3XhfEuUiVS5skhXw2dxD1o90xFGVowHWUsxOgOrlLhVg== 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 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=JkCAa24YyiWlkukD/LaO5dOR1Z6gqOaYm5Qyl4UEKYk=; b=GcxrSPZvj/0Dcy3Cuh6cSWUJy/83nPOH9MXyUj1HRVqrke+c+O+fIKJNnVxpzSivyX6KcybJFSlvykIAi6hpdrPZ/jempsc93KzRQtQ/Q4YlKgeDaxmTJPhXNIFcZpe1/DbQDNvqQZfzbJGZQG1crQ2SfkBWt0m45TjMdRWYYZs= Received: from MW3PR06CA0028.namprd06.prod.outlook.com (2603:10b6:303:2a::33) by PH8PR12MB6771.namprd12.prod.outlook.com (2603:10b6:510:1c6::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.46; Mon, 12 Jun 2023 05:00:02 +0000 Received: from CO1NAM11FT012.eop-nam11.prod.protection.outlook.com (2603:10b6:303:2a:cafe::17) by MW3PR06CA0028.outlook.office365.com (2603:10b6:303:2a::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 05:00:01 +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 CO1NAM11FT012.mail.protection.outlook.com (10.13.175.192) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 05:00:01 +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.23; Sun, 11 Jun 2023 23:59:55 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Kim Phillips Subject: [PATCH RFC v9 08/51] x86/speculation: Do not enable Automatic IBRS if SEV SNP is enabled Date: Sun, 11 Jun 2023 23:25:16 -0500 Message-ID: <20230612042559.375660-9-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CO1NAM11FT012:EE_|PH8PR12MB6771:EE_ X-MS-Office365-Filtering-Correlation-Id: 96b215c0-b78c-4111-db5e-08db6b01e11a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: qSzdUl9gWvMpdS01A5WUdoWi5VSKEY+M/DIT8NKlYs5wNCB5MG4aqVMw1tn8Rsjz7NPYLs0ui2xLLBMe5chKrEosF8SUF76emJ/C9vVkrrz/y7tqU+kFUcQknCxMW4a8tJW2JC4k0NoLsNxxX6Bl5CiPvUeYb6W8tCTSA0ceGokW7SsAlCBHUEHx4S88My+Ii4eY+BZ0aLibwwg+9erzpUew4V6dPDPg+R+05yqV37VmtGBjj9rMjzsdgjr1iLmA2BdooEpVm6j8veAMpc8ipqRtBvIcoXZAeDGUcBcEgYp4kkn3pxGlAsm3kw+Lwew4/3pz26mR6DYJt/WN1/rya0VC4vu16OEM2THI7MmFo4iwu7OUQ8EYhq4sTFr1Auw4K34ik/s9FWhvwbERJsg7GqgSlPv7+kXFE/InXyraRDRFhYwbTLqL7niyQspUG2U9tdzZ5b6VfzOl1GSiKgAAr/+IPFOLhaR2LL+wlrapZbsPS4sVWQJv39B+TBfSKCVkXdNuV86yK90tt2f2cyZzDim9sUOGQ9X5O+YRGRBgE9fmc+hu7jFM/gh6oV2Jg3VejaybhiqlGE2qpGEY60TkoOReb26gXlVxG0or60J/KKgb60vc/iTEJqJHb6+cn+ujeXJRQq87nb9TR9vm29PRPJkgPt0DVd1cZGSy2u6k1d94gXaC7BcxVbC1fFd+nH3/bRSX0ggyqv1QYNOKiIIK4S1odBYIsLkvohJRPy5CouNw6Jqd2HncgIFYIXHluEgAoRhjQweNiVVw6rMpPO4QWA== 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:(13230028)(4636009)(39860400002)(376002)(346002)(396003)(136003)(451199021)(36840700001)(40470700004)(46966006)(426003)(336012)(2616005)(47076005)(83380400001)(36860700001)(356005)(82740400003)(40480700001)(36756003)(86362001)(82310400005)(81166007)(40460700003)(478600001)(54906003)(44832011)(6666004)(4326008)(8936002)(8676002)(2906002)(5660300002)(7416002)(7406005)(70206006)(70586007)(6916009)(41300700001)(316002)(186003)(16526019)(1076003)(26005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 05:00:01.7267 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 96b215c0-b78c-4111-db5e-08db6b01e11a 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: CO1NAM11FT012.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB6771 X-Rspamd-Queue-Id: 547D11C0009 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: k3kwora4rsio7jd5gu8bqjnoypbzkq7r X-HE-Tag: 1686546004-296174 X-HE-Meta: U2FsdGVkX1+bq9F2nt6h5/LophzaxVQ9Ni5sr0L9k38SlhmY49R5FuyXgO2idi5/UqsrmgNzR1oA9T0zLzFLN9ViYbYib4vCvcYjR2MCdyAcqRRIAFVabyvV/ZS2WSAfDZ63yiBFSSVSbNaNMeC5Tpa5xFlMLwuvbKyDIW68GSIOcPiD2rhtGhcrgF8G3Xk0WMaz1j2Zl1BZMPNYWJow6RHKrQM/DeasxCSJe0YeqW56KLXAz1lT86paXEAwSZ/g/D/oM+PNk/cmZ6EEe90Sbk4+gv1ekfiPZlHKb1KH4Gk6/oA1mexzV+2IiOdmTFviobWfC1Xpn3n4Sxh0VJIax+DSGGKiafZoXLFOnPLYdNHTZYsHAbKIS1OitVjSzqjNuCpnnsZlVmAB2b3ZoqjN6xBMdX7eA5KPwUsrNHvhYvh++HKLBINL/eH/BF8egFS9P+b4bStv+lZG8zsKlE75pqXQxkvdkf47Rd+fuGYrWHNeQ4SiDVdIiO/rWFQ9eISmhfioVs/7d+6UNlMXsxVkCVeAu8jt5eWUTwrJ8iSgN+j7SVMHPc5uh4NjUVN/h+7bzdt6qEtiUZvyA4V+0F/Vl0TYl+dDBqD8j6uQ1oJOlje8EDXKAwK+bJfnUpsJBsNBb9cx6QWzdbk1bW3IKmjwuf2L69FzTsEW9lb8KiS5NIRUZr/rvCWDzKcy2QjDpafHBAa0CDVmOFxMKI1jV8CQA65zK1V4hAVkZ3nHPH0b8CmCxCy4Hj4qCZ4pz1ZtrcUG27o8fNgytMy2OXWoRpBNnus/TYAuAYj11nT2ohcZuPGo4Hp3xXPeXDhazOHJnoaVBoJZUKZus0DCNFG6caj2WvOjzg34zEyw3H7nChbqyk5pmR3Sbz7DmcQd4aKct8t70bpiMZfqZ0GTDbgIu15TqdtRwclBntF7tSCd+N8zyENdNTyJGh2tWM2dZcIz7eCCLC/+owcYZivn4EsIimg xBInCqCr MfM/hxGgMQ3eyVUJm0AkBkVFDj0S6qaMWyhukwGy87m0fZfjKX6ec4KI0s/PhdYTF3r1UGfVYmf6Yd9AKLYmfifnAbdo8GEpxI9bZYckz6YzWIYvQXbWBaGH31hYuI17tNnDHSw+SG80polXItoycKUfX4Nw2nmJJDg1vsHwGta5qlcHv+axikcKUWrleoKKQFvsoXgndu7bTk2XqQgfFsCCcZq/DUJYnq7AVAiIdVKm0trhecZECw+PC8XEMfv4nMzrpk7QP3TCQzfbecqIuIF+K978TPku0o8tpTcZ09HZI/GZGGa7WwxaQmg== 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: From: Kim Phillips A hardware limitation prevents the host from enabling Automatic IBRS when SNP is enabled. Instead, fall back to retpolines. Signed-off-by: Kim Phillips Signed-off-by: Michael Roth Signed-off-by: Kim Phillips Signed-off-by: Kim Phillips Signed-off-by: Kim Phillips --- arch/x86/kernel/cpu/bugs.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c index f9d060e71c3e..3fba3623ff64 100644 --- a/arch/x86/kernel/cpu/bugs.c +++ b/arch/x86/kernel/cpu/bugs.c @@ -1507,7 +1507,12 @@ static void __init spectre_v2_select_mitigation(void) if (spectre_v2_in_ibrs_mode(mode)) { if (boot_cpu_has(X86_FEATURE_AUTOIBRS)) { - msr_set_bit(MSR_EFER, _EFER_AUTOIBRS); + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) { + msr_set_bit(MSR_EFER, _EFER_AUTOIBRS); + } else { + pr_err("SNP feature available, not enabling AutoIBRS on the host.\n"); + mode = spectre_v2_select_retpoline(); + } } else { x86_spec_ctrl_base |= SPEC_CTRL_IBRS; update_spec_ctrl(x86_spec_ctrl_base); From patchwork Mon Jun 12 04:25:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275450 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 859F1C7EE25 for ; Mon, 12 Jun 2023 04:27:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DD1CC8E0002; Mon, 12 Jun 2023 00:27:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D81AF6B0074; Mon, 12 Jun 2023 00:27:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C21A68E0002; Mon, 12 Jun 2023 00:27:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id B20E06B0072 for ; Mon, 12 Jun 2023 00:27:01 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 7A8B2A025B for ; Mon, 12 Jun 2023 04:27:01 +0000 (UTC) X-FDA: 80892810642.14.D716914 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2054.outbound.protection.outlook.com [40.107.220.54]) by imf19.hostedemail.com (Postfix) with ESMTP id 607761A0007 for ; Mon, 12 Jun 2023 04:26:57 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=KZfhxEGH; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf19.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.54 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686544017; 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=WlfUBo/FyI8IPg/IWyuoH/hWaTywD2b9n9I2Lt8R/Us=; b=egy2Jao+Dr9ZlqqKBnYXYTrntuZQq8wfSwGWlMegmwnVzH93vvUGz6D8CiarLK8hMkFS1u NS8ljjBfNbTa6FRPyX5uDbzUv1I8Zf7fBEqbUqY6U1n7VDiAlx5xTJIW9fKFCZ2WKn7I/f ThO7BSfUtAB8IxYEFNC6w7iAWAihKco= ARC-Authentication-Results: i=2; imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=KZfhxEGH; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf19.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.54 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686544017; a=rsa-sha256; cv=pass; b=vBQ/4Yby9xlH9Kd4n/PrqUIcP3WOAZR4cumnbsj57OTERwvnVasxSAcNoDdf46+wdfihP0 INPya5yEUwx55Jp9qDwwfzIujTvzKjvVTE5dOIbOVTJ+Z1LRT9xe0LNeCCjNsEvGAuF3db G+zFUx06qaO8npgS3kcR9tGPieDuot8= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BIG4ldRGetGcHHgn5DhjUD5JrTINL0kA4DtdJ7uu4HR58y1LBpLx/BFHt5N6zhbimhS1Prw1I6tKam6nKylAEgh6QcVnEqeEPisJoJYMPzNzmZ2ZD/XcyP8R8wiJi2KkmszntrUYeDNQ5ZQCmsv+v1O68PqOeYp5DbUxx0Bq2JVVWOQDC7XAT0visrLqsOiVuRr/qBAJHmZpI3Qsx+cCwGa1D9hv/GGhljAEbd672UTTmYZQzbcnlOm2n/lKenkFl1IQzB1vlRhuroQofV3dDtULaADBEf8ufTuiyIQei1jZtSwA3DOKpwhOovxNzCSWDXOLdbrgItgzO+odUVpVMw== 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=WlfUBo/FyI8IPg/IWyuoH/hWaTywD2b9n9I2Lt8R/Us=; b=C1B94VZchwKv3F94bOkJ1tjmpUTzydlPwl0TqTip01c/qMWaHfisWKB237bbrUGeq2ttebcxmvrqvs36DRp520gSLmX1JXnjIFhI/ZY7HCIyiD78oo1uJNR3goh3CxkznLnHQadcGldg3rcOVDTWgfA7AAAM4dV3PTwMQZEh9sq9JSsQ2CYhnEOaC/bt15ZgpK0vQOap2/VnaXntLFVKTNcd7GNkknTmp4aD/hWmkQ+0vhwh/v7FS8HjDvWfT3KvP9uuoeqYs7vLZJQbbjMHUbXbFbSW3wXE6NDc/f9fM+xT7asluv5CH8iydSRIzEhzGGFcNt8Jq2SA55zwSRdULg== 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 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=WlfUBo/FyI8IPg/IWyuoH/hWaTywD2b9n9I2Lt8R/Us=; b=KZfhxEGHUCpzZv5AZWYn3o5O0rij1zTtcu2i4BA5BqMbWhvNygQqG8lylRaiGSdx0vav4dk6wOVOjd/mOnady979WlJu7+/OVxCz8EMJL0obXb9LI7JZ/YZT0gnXHvV+HAcUje+6qvEMEA4VBjC9vcnNMDN1dPAEAlnhhI5tjxU= Received: from MW2PR16CA0025.namprd16.prod.outlook.com (2603:10b6:907::38) by CYYPR12MB8701.namprd12.prod.outlook.com (2603:10b6:930:bf::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.44; Mon, 12 Jun 2023 04:26:53 +0000 Received: from CO1PEPF000044F1.namprd05.prod.outlook.com (2603:10b6:907:0:cafe::f8) by MW2PR16CA0025.outlook.office365.com (2603:10b6:907::38) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:26:53 +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 CO1PEPF000044F1.mail.protection.outlook.com (10.167.241.71) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:26:52 +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.23; Sun, 11 Jun 2023 23:26:51 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 09/51] x86/sev: Add RMP entry lookup helpers Date: Sun, 11 Jun 2023 23:25:17 -0500 Message-ID: <20230612042559.375660-10-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CO1PEPF000044F1:EE_|CYYPR12MB8701:EE_ X-MS-Office365-Filtering-Correlation-Id: 280b3df5-4843-4484-af1c-08db6afd3fa0 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: DQ826GZCtXMQmsxLj56srWSPkfqaN/BwIjmcqprxu/9TitlM1EnxxLdZ+Y0wVs1CkN2Hh90DoOcRGd8bsohMzUeSE4yTSPDz0ZTWwICEOA6jfG8/LuKq4lJGiSz5DdVo7qWY0xUP3gnPZ/hc0/UiPTV7etC3pdCa0KKOVD7VgBMi+1xN/YbGjNbkTWYQdd3aK3MJyqzKly77zGebHzESoTLUSL+NMAhy068J2leiQxXVgri9BtCeujf1y4CHsCPWiBRNX8WhAiNM2+8ImUOVJ6esEcuVUqTpjVWHruLjEtRkx4luX/dwl52tXHqdNFprwidKy475hsnEjHUnV6TPYyjHjiwhCZifVLqSeVAc+kxEKLSRmG9BhTounJEycTNPloHW1LeEuDesRbUV+5KOQgzn6W5Xzny8UIJm2oYBenz6j6mO4d5HXhu0Iok7LMDa+/7+XINUazJdATlbSKpjRMH4ikISkNlia2HVHnZm0xNeL9c2PqflRJv5hQeS+7xHkT/WLf1xden8OMwuo2L0/E+3YeF3fddy6hvYJRCoDE4ZXcvld+PZlmTAuGlt5k1Rg0D6KqXToKpvqw439zNfgfr3QNtbTq/xdgbjielwQ7Yt9A1QZYCmIwFHIxGaeyuKYmPcviXD7Kx4bkRGLp8G2X8fv/Y4mR+awrtRcmaAxnndFBmnSeypffoSrEdphccNfcCrK4QF8r+NfoCyXCqcTCKQKbXebGmqPeJQijt9P5Ypst2LwTGgS7Rd1IBVK8CYlBnzMVHsasGyhPgOEqEh3oXdy5nwUlxAD9ibKbkk3CY= 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:(13230028)(4636009)(346002)(39860400002)(136003)(376002)(396003)(451199021)(40470700004)(36840700001)(46966006)(7406005)(5660300002)(7416002)(47076005)(8936002)(2906002)(8676002)(41300700001)(83380400001)(336012)(36860700001)(44832011)(426003)(82740400003)(356005)(81166007)(6916009)(70206006)(82310400005)(316002)(2616005)(70586007)(40460700003)(4326008)(186003)(16526019)(40480700001)(26005)(86362001)(1076003)(54906003)(966005)(478600001)(36756003)(6666004)(36900700001)(309714004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:26:52.8260 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 280b3df5-4843-4484-af1c-08db6afd3fa0 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: CO1PEPF000044F1.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CYYPR12MB8701 X-Rspamd-Queue-Id: 607761A0007 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: bsd3chpxj8j1pz7rbqeupk5fkk5t3pqu X-HE-Tag: 1686544017-66270 X-HE-Meta: U2FsdGVkX1+qmYCOPYRGlOYvN13sngmOjrtmuLKvHEZzk6in3zR7tsknKhr7qkjqxB1+MztPn92LCqUh1ek8IYc+sjmKtAnq/B2gxXBUQuiGZYV2pdDaS61Zk6wmdwgMD74XDMWU2+llKFsabOdD5dr8L2jYloCOBfP+465L79X3owi/vOE4mRiP9o4W2+tCFkea3qky9rHQO1N/t2mP/t/5mNZl/MMB1JkSwW5Bnzm/569sV4W8wuZquW3nsAm6UgyCByb4ERSoHNaHfm0GtpV3hupMyGV8pgrWd1lt3gmwKeWVIa3wfKPA/NeSTrUy9us1nxT8khysF+lUrl5uwGb97EH4cT48rcBptSKAva5qdL3VL2Ff+/DzWKLxMfVX5sq9djFfojILdNW1cegn511eWrfkhBNs91n4L4/TAWmmQcwHm2TzSu5Ahis60hjBoAd3j/2nXLqDa2PFiwWmB1mzJWNSIIqrotTbBFgHaGPRg+Hhzq0KjhJGVVkKBldqeXMqyohzcIpSZS0GESQbkuQhm0raSyOhqDlAhoFtHW/+mmHsQhI1cQWnFwz6ChMk0g8hIDUUE7DODaj94qopjk1e0sx/VfjjshdFq/CMzmtiRvCC2gpQglZFXDGG/eGCzzehHLTWQqLgy9XDuy0mOZnmvwMXvZYeLY89IucveJRY1r2Fh5+KLMcoZxxPtgZdp3xB0CpPeEnkYusN+iGp0eMpo6T88oVQb0N1r6nqxzSpQ7CD0tIgCZT4DeqrKUWYLjJouTcia6CdC98uJRIdYHwHThdb/h2cQ42Yag7t4YfCrq4wsAnGlyRB9/Ou6RWprKWQ01pXfmhH5ZKL7g7cBw65iFbY12ig3bcPuIeft8VGrIa7P5WmckZHnhscNNWR4UNr11l9BVhANqZfPVx+C9c/PBVgesrDfqSRPRLa/JQCBsOgT8vHN7KXi5y+dPGT32j9WjtNJ0MBcjPUrdt 7eQYui45 TVrK1Kexq3tm7mSvZkw+z7wYrmjptw+7AI2JEvfhzQ3eMm9A2wUQ7Al3jb4ITvVbZcpgi5zQpOcHSzGg0gPQSsGHGDmVbigKKi6T2WkdeeOmDQR4cgwAIhLiei4oIAtc6Ort3oeNbLDfaTzDAdbvghzYB9oV9RbfVBu3f14/G0Lmt9lT3MJlQb406X92PyDw+WbtxZ1anTrQGF+HlZ40g8L2zoO7a7KWF14tPFtoD1Z9jT+nsW1ajkDIG5rd1O4om1rZ8vNWU6Y2Mtek2k3t+DaINq3ixMQqoFESOFBvHdbMqfKssTs08tq+DVguILvHC8Y9XNlZM304Lm8uv5KvyAMSa7AxSrNsi2NN/yBqvtcA/MkVQiYlYV4QKXA== 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: From: Brijesh Singh The snp_lookup_page_in_rmptable() can be used by the host to read the RMP entry for a given page. The RMP entry format is documented in AMD PPR, see https://bugzilla.kernel.org/attachment.cgi?id=296015. Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra Signed-off-by: Brijesh Singh [mdr: separate 'assigned' indicator from return code] Signed-off-by: Michael Roth --- arch/x86/coco/sev/host.c | 85 +++++++++++++++++++++++++++++++ arch/x86/include/asm/sev-common.h | 4 ++ arch/x86/include/asm/sev-host.h | 22 ++++++++ arch/x86/include/asm/sev.h | 3 -- 4 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 arch/x86/include/asm/sev-host.h diff --git a/arch/x86/coco/sev/host.c b/arch/x86/coco/sev/host.c index 6907ce887b23..0cc5a6d11b25 100644 --- a/arch/x86/coco/sev/host.c +++ b/arch/x86/coco/sev/host.c @@ -30,11 +30,36 @@ #include #include +/* + * The RMP entry format is not architectural. The format is defined in PPR + * Family 19h Model 01h, Rev B1 processor. + */ +struct rmpentry { + union { + struct { + u64 assigned : 1, + pagesize : 1, + immutable : 1, + rsvd1 : 9, + gpa : 39, + asid : 10, + vmsa : 1, + validated : 1, + rsvd2 : 1; + } info; + u64 low; + }; + u64 high; +} __packed; + /* * The first 16KB from the RMP_BASE is used by the processor for the * bookkeeping, the range needs to be added during the RMP entry lookup. */ #define RMPTABLE_CPU_BOOKKEEPING_SZ 0x4000 +#define RMPENTRY_SHIFT 8 +#define rmptable_page_offset(x) (RMPTABLE_CPU_BOOKKEEPING_SZ + \ + (((unsigned long)x) >> RMPENTRY_SHIFT)) static unsigned long rmptable_start __ro_after_init; static unsigned long rmptable_end __ro_after_init; @@ -210,3 +235,63 @@ static int __init snp_rmptable_init(void) * the page(s) used for DMA are hypervisor owned. */ fs_initcall(snp_rmptable_init); + +static inline unsigned int rmpentry_assigned(const struct rmpentry *e) +{ + return e->info.assigned; +} + +static inline unsigned int rmpentry_pagesize(const struct rmpentry *e) +{ + return e->info.pagesize; +} + +static int rmptable_entry(unsigned long paddr, struct rmpentry *entry) +{ + unsigned long vaddr; + + vaddr = rmptable_start + rmptable_page_offset(paddr); + if (unlikely(vaddr > rmptable_end)) + return -EFAULT; + + *entry = *(struct rmpentry *)vaddr; + + return 0; +} + +static int __snp_lookup_rmpentry(u64 pfn, struct rmpentry *entry, int *level) +{ + unsigned long paddr = pfn << PAGE_SHIFT; + struct rmpentry large_entry; + int ret; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return -ENXIO; + + ret = rmptable_entry(paddr, entry); + if (ret) + return ret; + + /* Read a large RMP entry to get the correct page level used in RMP entry. */ + ret = rmptable_entry(paddr & PMD_MASK, &large_entry); + if (ret) + return ret; + + *level = RMP_TO_X86_PG_LEVEL(rmpentry_pagesize(&large_entry)); + + return 0; +} + +int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) +{ + struct rmpentry e; + int ret; + + ret = __snp_lookup_rmpentry(pfn, &e, level); + if (ret) + return ret; + + *assigned = !!rmpentry_assigned(&e); + return 0; +} +EXPORT_SYMBOL_GPL(snp_lookup_rmpentry); diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index b8357d6ecd47..bf0378136289 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -171,4 +171,8 @@ struct snp_psc_desc { #define GHCB_ERR_INVALID_INPUT 5 #define GHCB_ERR_INVALID_EVENT 6 +/* RMP page size */ +#define RMP_PG_SIZE_4K 0 +#define RMP_TO_X86_PG_LEVEL(level) (((level) == RMP_PG_SIZE_4K) ? PG_LEVEL_4K : PG_LEVEL_2M) + #endif diff --git a/arch/x86/include/asm/sev-host.h b/arch/x86/include/asm/sev-host.h new file mode 100644 index 000000000000..30d47e20081d --- /dev/null +++ b/arch/x86/include/asm/sev-host.h @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * AMD SVM-SEV Host Support. + * + * Copyright (C) 2023 Advanced Micro Devices, Inc. + * + * Author: Ashish Kalra + * + */ + +#ifndef __ASM_X86_SEV_HOST_H +#define __ASM_X86_SEV_HOST_H + +#include + +#ifdef CONFIG_KVM_AMD_SEV +int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level); +#else +static inline int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) { return 0; } +#endif + +#endif diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index d34c46db7dd1..446fc7a9f7b0 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -81,9 +81,6 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs); /* Software defined (when rFlags.CF = 1) */ #define PVALIDATE_FAIL_NOUPDATE 255 -/* RMP page size */ -#define RMP_PG_SIZE_4K 0 - #define RMPADJUST_VMSA_PAGE_BIT BIT(16) /* SNP Guest message request */ From patchwork Mon Jun 12 04:25:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275451 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 8D2D4C7EE23 for ; Mon, 12 Jun 2023 04:27:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E46416B0072; Mon, 12 Jun 2023 00:27:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DF61E8E0005; Mon, 12 Jun 2023 00:27:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CA1928E0003; Mon, 12 Jun 2023 00:27:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id BBF616B0072 for ; Mon, 12 Jun 2023 00:27:34 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 8A088120232 for ; Mon, 12 Jun 2023 04:27:34 +0000 (UTC) X-FDA: 80892812028.21.CABD127 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2071.outbound.protection.outlook.com [40.107.93.71]) by imf28.hostedemail.com (Postfix) with ESMTP id 88E32C000A for ; Mon, 12 Jun 2023 04:27:31 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=s48ASt0B; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf28.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.71 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; 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=1686544051; 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=aRmnZV9+q0iWnxivqpSKbZUlVCI+wjQITiyoM9Ekv08=; b=n0R8D29UC0doJnr+tNQTw4BDwNoH/16mL+b5f6UlUX6IeeSsFKZzMsbaj/ibFCsv7FoC78 oLleUrOhd42ep2edf3Zzl7iPSkQE9VB8Qtz7CLTyIeCbgB3fXdzQhvq17qoAStfuhc3af7 Irg5cYgA9NXh6xd8yR5nJtjhxx1EdCs= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686544051; a=rsa-sha256; cv=pass; b=s9IRr/gAdIf8qyAPziZg5ARP6LnGNsRbGTzPmJNkvVK3sq6vVmX94Z/wUmFIa30go2+vg9 Jm/HR/uNZPE5FxUIe0UlzO3sUZEJyEJ3REGy4to++NMrJYsZVMFlup8vxdKCHFXjH175t8 OYS8f7RkVt50j8ljKAYyBFVKAna48h8= ARC-Authentication-Results: i=2; imf28.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=s48ASt0B; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf28.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.71 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kVl++eSid4DAaBvxslCd3GmMmzrocTZ9rdLXfv/gpRqdyFisH9GVw3JZXXIV/G+lOSNk2OS34zp7TkWQPgykPQOzRFr0k9OcRwJ1eUqIdxbJAsyIJf6AYX7Hha6NV2HXBm1BXSwADJzHDIvYVlfUtT4wC37NliAz+xm3GV1GIIMBuC/e6tSR7rmq0WSmrrPCakFzuTD2tDd7ewMDEC/NoDm6RxkqJb5WhqWKiQ7sgbgGHwP+igb/XI0hSQLRn/0/679AcJWebHeYz9lhB4pg1iw45XBF92+WCl4qohWqEtyt6ohMLHCqOW1Errz/kxNLwcj1jwaD9iuJo44XgRROfw== 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=aRmnZV9+q0iWnxivqpSKbZUlVCI+wjQITiyoM9Ekv08=; b=h/fHsJsQ7re9v4WUgZZs0GkMMv4KlRrW0H0vrSgb7fxhajkZ9Up0rHFCvt1o86Ds5r8lyH0NdarRExZgOAGDGfwwmM40daKajeVo8tJXnu47u47RVag7m6G8mp31iuXO7kS8R8kz4yXLZFh0wCRK9MD1h8Zpd52l7xoxfB9cCCYyJqIYPL0tHv9B1dMXfKWI7vPPt0VsRfLPIBZIZ0DiE0LXgWEg2Dt055BsgdMeGdhdZRAQW1hSVkyt7I93SpQb6hMb7kmWDDEQFA/KG5zF/3QBWuOIQggJ4zqF5bX86UqLl9YHALi8nDTekr1BRkIy6ypubrmyeWyEhdpvmFTpHw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=temperror (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=temperror action=none header.from=amd.com; dkim=none (message not signed); arc=none 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=aRmnZV9+q0iWnxivqpSKbZUlVCI+wjQITiyoM9Ekv08=; b=s48ASt0BZuLSJb9nucZ5nCuw/r7v7eUM5VGKcwEgMETM0TdMYhp3Da/iSy7jB7aIrqiw8GVRf5ueVKz37BO8w17h9P1E17ikLCnIUx/k9DL7vpT8nRjelC9sAczTLNmBrdn6nynKEBocT2Sy61OKwR+IbaWxoDjfavU5ykZOTNc= Received: from CY5PR15CA0136.namprd15.prod.outlook.com (2603:10b6:930:68::24) by PH0PR12MB7840.namprd12.prod.outlook.com (2603:10b6:510:28a::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.29; Mon, 12 Jun 2023 04:27:28 +0000 Received: from CY4PEPF0000E9D2.namprd03.prod.outlook.com (2603:10b6:930:68:cafe::26) by CY5PR15CA0136.outlook.office365.com (2603:10b6:930:68::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.34 via Frontend Transport; Mon, 12 Jun 2023 04:27:28 +0000 X-MS-Exchange-Authentication-Results: spf=temperror (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=temperror action=none header.from=amd.com; Received-SPF: TempError (protection.outlook.com: error in processing during lookup of amd.com: DNS Timeout) Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9D2.mail.protection.outlook.com (10.167.241.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:27:26 +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.23; Sun, 11 Jun 2023 23:27:25 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 10/51] x86/fault: Add helper for dumping RMP entries Date: Sun, 11 Jun 2023 23:25:18 -0500 Message-ID: <20230612042559.375660-11-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CY4PEPF0000E9D2:EE_|PH0PR12MB7840:EE_ X-MS-Office365-Filtering-Correlation-Id: 2ed0c52a-060f-4081-22ae-08db6afd53b4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: sCNboHJeVkb0hpgOeku8Xu7OKeC7TXZ2u1xhdTH7MQ2Y00/p7IcfXeDW/9Fgqr97oUI7x0XkoSGBfot4rJqw2VDQJRb21k4JtpizXwjWM5Mt1YGRXWvTxu6mpso/vTZQdkV7NpHB2IxrWmMieNGKKJ5vNMHbl8gTJfkrERd/tk/ezhWrU2ftgQIWGHHsYPZffmV/LnpUaPnncl1RF7jB3lcVlZ1e0nwkMQ1+ugCWc8JYEtVPLTfiQGISTfzWFFgbm4+Nn6WW9L9YF3yO5x0Hg4Ub7U1iksX56sIpA3lWM2mo3WQ9gljsJM0qgkx2DlPlIcpqNMXn2fAJbAVSxgslTldv4AFjDuHAc7idptaqkpHt5IrnEoQdXYt+iwIcmIyooqCflCgv8m2rbc5TAusCQQ0OO+rax6xUhCJBdiX+YMaCnFjcr5oUGxJ67bwrzg8L4QV/WtuJBQhLqxRgSVC2TLUPq0JuAEC8sovxMSQJglQH57Bh645fEAUaqsCDy85w3XbNwfmN/LfwDXxBv5axiny5KcRONq26ioxPUpfvV9hVtQXsf5u1MTHRS4/zDFhqGDk/qoLJCa/spJKY4yMqYCtmzZzrj+XoBWBj6IoAG/ox7VMlfBVml5B58sj6wbaw0fnI9gPAvtT4J1x6Gb00BGwV2StmCL6Uvohg55w8+YVIK0DfcD10PmH27Em0M+pMGycx3eoEKYibHsy+Q5E8GPkUkO/jG9xpzvwtYrfG4+/pRmWxuucF/n3P5NdJnJv7NK79LS/zNPw6li967hbyYw== 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:(13230028)(4636009)(396003)(39860400002)(346002)(136003)(376002)(451199021)(46966006)(36840700001)(40470700004)(40460700003)(6666004)(47076005)(36756003)(63350400001)(426003)(336012)(83380400001)(63370400001)(2616005)(86362001)(82310400005)(82740400003)(81166007)(1076003)(26005)(356005)(40480700001)(36860700001)(186003)(16526019)(2906002)(5660300002)(54906003)(44832011)(8676002)(7406005)(7416002)(8936002)(316002)(41300700001)(6916009)(4326008)(70206006)(70586007)(478600001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:27:26.5235 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2ed0c52a-060f-4081-22ae-08db6afd53b4 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: CY4PEPF0000E9D2.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB7840 X-Stat-Signature: qgjcqnmcpy4mzch7ejgzx5fpkgqfuc5k X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 88E32C000A X-Rspam-User: X-HE-Tag: 1686544051-992566 X-HE-Meta: U2FsdGVkX1/i/mis1xfuMsKxZ7NuJCN0xbkZKSnTt3ybvhWsGIQe8vHUVQYbuqOk45tHarVoxS3mYM9/ZtyUSuTQ762AP3Yw1nswV81ejD6kVfkCja0sB3oh3aBXAcCLkZg7N3gIDJBkGZB6gW6qPGz0nmW4matgvaael/X0TZEFykb3v7CYLwM5TxluHcw4GkVXcgHcsMGiz5oDi6p6kWu2XBXDOSOlTxojbl/h1edNxwTxjUQTWWVJ0Kj+jaT6d6cCymsTJJwHMhX5hBCjC9KDbM0rbX/5+WLiPGqQFXu6NAXcC7rF19c8SaN2lD7t00x9xRNMAreBv+sZF9iIeWkYCGPv9LjTmdbLRgNVFMyzkwUTsEBZao94vuNnqm9mABiIGVOmlqd+PlAB3Q3DDUHADnwQAYgeGcC7JMsPakcYCi0+ifbqiU41+XzF3Apj5FAE5m24xYdXQpzm+teww2gKm10Koiwh4rKDTxnTl9DffWTmNcapV1yE35RwezTZbN4oWkW17zZYCGCV8Ka3u+cQWL9NivGzVa6OsJg8p9iL5OweQ6Rk2dW0IBvODr4kVL3pDO3a1/QOznKfjyiy+pJTGgxGumToAZLt2SojQ+HRcrxfVKIBMTJQ3Mwp4RpXVPInGhMTUf4kvLoQPYUQHEFI37jYi2Gj0R/a+brDuy2lNAtGRmcynyEHhiV/uiQajjiF44fUN6fAbkLDq1DCr8JCbTnX78rT7EDIu+ym6nODlQnvM5wQF0GZ3iAnOo5vOvtYCYT8qtxK9ablc4K9+OjWzMgPdtl7TalymEjErUlQdL0XlpRnf6u5YdnNvIE1fr33YiXx10GdalMGYLAxgf3hHWHsruFl+Fyoi+JOSERuHLcjexgrgVWhcVoiGL3+L2gT/7eFmKDyTHS7qK6KTcqmO/PvBrv6kwpNKzJg3XS5jo3hpvD89iyEUZZCG8eyUm8yKF7yT6BMMqDO9m0 LsV0ETVy wyBasMDrQ/Ydftn7Eo4Kp3c+KcV0uWSSoGc6DQadtNrh947xp/0oGnW51jrUc/MCZklM+fQESFhVNO82JiF1Gv121m0oIBQdtVqMG1yyAifUrW2+88sWJ424G3Opxrg7ck0nXihw30wzRApH/PjjIInvQ0cJ+MtabGzMW4Fotvy3Pw0RKmXJ8s9wDxl9XeepM/Wctt5ZMeCbelxY/QT5J4uDINtB+Aj6A7gKZXpcC6JvVEkDBZz1KioTRlQVFLePlFyZEpiM2sFmeakP2TlBKn3K8NF5fARvNwPbu+fqko0VxtFN+emlx/cfI/Q== 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: From: Brijesh Singh This information will be useful for debugging things like page faults due to RMP access violations and RMPUPDATE failures. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra [mdr: move helper to standalone patch] Signed-off-by: Michael Roth --- arch/x86/coco/sev/host.c | 43 +++++++++++++++++++++++++++++++++ arch/x86/include/asm/sev-host.h | 2 ++ 2 files changed, 45 insertions(+) diff --git a/arch/x86/coco/sev/host.c b/arch/x86/coco/sev/host.c index 0cc5a6d11b25..d766b3bc6647 100644 --- a/arch/x86/coco/sev/host.c +++ b/arch/x86/coco/sev/host.c @@ -295,3 +295,46 @@ int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) return 0; } EXPORT_SYMBOL_GPL(snp_lookup_rmpentry); + +void sev_dump_rmpentry(u64 pfn) +{ + unsigned long pfn_end; + struct rmpentry e; + int level, ret; + + ret = __snp_lookup_rmpentry(pfn, &e, &level); + if (ret) { + pr_info("Failed to read RMP entry for PFN 0x%llx, error %d\n", pfn, ret); + return; + } + + if (rmpentry_assigned(&e)) { + pr_info("RMPEntry paddr 0x%llx: [high=0x%016llx low=0x%016llx]\n", + pfn << PAGE_SHIFT, e.high, e.low); + return; + } + + /* + * If the RMP entry at the faulting pfn was not assigned, then not sure + * what caused the RMP violation. To get some useful debug information, + * iterate through the entire 2MB region, and dump the RMP entries if + * one of the bit in the RMP entry is set. + */ + pfn = pfn & ~(PTRS_PER_PMD - 1); + pfn_end = pfn + PTRS_PER_PMD; + + while (pfn < pfn_end) { + ret = __snp_lookup_rmpentry(pfn, &e, &level); + if (ret) { + pr_info("Failed to read RMP entry for PFN 0x%llx\n", pfn); + pfn++; + continue; + } + + if (e.low || e.high) + pr_info("RMPEntry paddr 0x%llx: [high=0x%016llx low=0x%016llx]\n", + pfn << PAGE_SHIFT, e.high, e.low); + pfn++; + } +} +EXPORT_SYMBOL_GPL(sev_dump_rmpentry); diff --git a/arch/x86/include/asm/sev-host.h b/arch/x86/include/asm/sev-host.h index 30d47e20081d..85cfe577155c 100644 --- a/arch/x86/include/asm/sev-host.h +++ b/arch/x86/include/asm/sev-host.h @@ -15,8 +15,10 @@ #ifdef CONFIG_KVM_AMD_SEV int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level); +void sev_dump_rmpentry(u64 pfn); #else static inline int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) { return 0; } +static inline void sev_dump_rmpentry(u64 pfn) {} #endif #endif From patchwork Mon Jun 12 04:25:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275453 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 787A0C7EE23 for ; Mon, 12 Jun 2023 04:28:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F09C18E0002; Mon, 12 Jun 2023 00:28:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EE0C36B0074; Mon, 12 Jun 2023 00:28:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D818A8E0002; Mon, 12 Jun 2023 00:28:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id C55AC6B0072 for ; Mon, 12 Jun 2023 00:28:31 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 7DC591201F9 for ; Mon, 12 Jun 2023 04:28:31 +0000 (UTC) X-FDA: 80892814422.18.44CC073 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2060.outbound.protection.outlook.com [40.107.243.60]) by imf20.hostedemail.com (Postfix) with ESMTP id 8B10C1C0006 for ; Mon, 12 Jun 2023 04:28:27 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=wqmCPdyT; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf20.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.243.60 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686544107; 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=NMB31nd+imPpavDDp2i3hNbkegevF9k3G64/47I11j0=; b=N2xhAe+bYzN9XddyIEE53l31VUABzJTIHL0GJ+X8Kru5h19n1V9T6fh7ZH/VD6lJlU7rKa YsVNWNPdlqvfBwFvWAlK+vL8j7orHKdaX3XRO6NAwa63XCWuucsuE88cvRqc/eDPYvGGoq keKiFrwSMauj2Tak4Pz594XTnytJqUo= ARC-Authentication-Results: i=2; imf20.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=wqmCPdyT; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf20.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.243.60 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686544107; a=rsa-sha256; cv=pass; b=Y9KWyqHJiGNbVmp/Gkj+bnZMyr4DF2jBy3jCZ4zNK5hVzYzuc7//J0o87pZgX6XCzoipNx sIlbxPAYE+39dIuIbyQODb2xfEy0T2y1vA8Qa5htaSARV0DvefXPL+mF2Ey7nKM0r4sj68 ODESmPdpkhSTgm3qJsCg/RR/nxGJSSE= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BiN8aGoUKjnn+v6tPniY4KVrcUTrN5gQmlzQqnTifbMNkLRi9jKTMukg7gd0mhMjpSPzbjhfO7CaoqwbnycACOvZ/JSkDLykX76qOYe+7cpuXs0J3JF8mvEwIfzqPKY/1uxWuya+bcrIMfPPLoZtjMtdEGykr6AKw4UCzlWSSNBoY4zzf/gimEXNjHXqRcn2z7a1KTYjOWnHK9Lh5HSc4JTUJWe7YfZixCp3XEshsCRJPRnqLk/yshkI/6siGKntXHhIdilK6S9H8oduhb8eUgKYmiwooMNtv9P/U7GnHk/a7F33riJz7HFdsOxGlZTQcx0mIm629Q5e5VcDgPH4/w== 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=NMB31nd+imPpavDDp2i3hNbkegevF9k3G64/47I11j0=; b=BDrkFr7uObYL01NT3H8A8p5WB610X7EjMMre7+Bhqa3q6RtSn0cGI8oTeuVrfRuFk5lDGF0jHZJwQPYuM47yNskery1sVzgzMDXuxpg6H6QyhbDmTL7CGJJK+5ZKBI5WWy8G8PMtZuYmQLX1yXQFq00jJHNgENtpEjt7oxjcbqTEzV0jTL8C02X3IfPqpvwyJ647Fz1W3yuzjNLIL5Fs4z3Q3Mcs11Va+l8/L6oc8tt3g7teDg9WXaNPcKohi6T3P9i9RfYRXgso1uD/lCWud/faSrl2LnTLsL6VKiLzs0vQwAajhaGWWYJHUUWtyNCGhd2GZDG/c/AJlPVHsuU8Zw== 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 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=NMB31nd+imPpavDDp2i3hNbkegevF9k3G64/47I11j0=; b=wqmCPdyThfxb8PAACYTf/ZJxUuvXMoGJJ4cgCVRrxrZtL7eh6uoQwjxY+K2SSxMGhx6yvKDCJtrBSQCWGhz2L+qrFJS/MDuG3ZAYokmVghmlSfthy3hDqdnaRnzWQ8EWhlWHcRd2+CIgK0q22bTPIT3JL6Gcu2UQJtLaWIEAhXw= Received: from CY5PR16CA0012.namprd16.prod.outlook.com (2603:10b6:930:10::20) by DS0PR12MB7560.namprd12.prod.outlook.com (2603:10b6:8:133::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.39; Mon, 12 Jun 2023 04:28:23 +0000 Received: from CY4PEPF0000E9CD.namprd03.prod.outlook.com (2603:10b6:930:10:cafe::98) by CY5PR16CA0012.outlook.office365.com (2603:10b6:930:10::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:28:23 +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 CY4PEPF0000E9CD.mail.protection.outlook.com (10.167.241.140) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:28:22 +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.23; Sun, 11 Jun 2023 23:28:18 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 11/51] x86/traps: Define RMP violation #PF error code Date: Sun, 11 Jun 2023 23:25:19 -0500 Message-ID: <20230612042559.375660-12-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CY4PEPF0000E9CD:EE_|DS0PR12MB7560:EE_ X-MS-Office365-Filtering-Correlation-Id: e27b4765-fac9-42fc-d1e5-08db6afd7553 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bqCp7dI7Fv/h+/ZknTqMCpt//yb8DFffSDq7a6+PDKu3uLc4SOJ3vmXoEBX20kHv3r+Sr8EsATnXaDGfHP1B4s3UVsmrE2yJhp2/IMo8Ru5CQwvFdKCNF4AiG63cIh9DP87PjvqUo4Mc8kvzt2yscllho9d1JLX9o6FcVrVSHR27N8aSs8WHNioTsxgqe4bFJTlU90dQFe/uVfb88kQ3EZJn3y92Mk99CLOR9pxNPGIYJ+mIPLAwdJdup+ywsbpgAhbDt/97wSkQd4XqtFIi8EH4BnaukBTxTauOf/BvnKdFxn8BJ7e37+Ub5G9S5cjpiTZxXfXPg/L4cRAYS5BUOhy4tvrXM48s1CRYCedkOLKX1tdmGks3IboXRJ4kYsInaitRdrGJf2oD202u2ppMyaJZ7BaHrzq0TFgtiveRspFUG+66aZBzsPwbpN67MjSgUkewKzD/5yTiQj6xfkZ7XVNr2Feckssna7lhWzWtXvhnoLsxntywVvKhc7ieP6HASowb23fXZsu0+WvgGzbnsNjmJuK0apVsde6fOWh5+sLaRt3tQDh+WIsrZNn/1aCdewNaXjIgMh54j5iZgywgypvnFU4XZCjblMi/h3EK3Kb0dKj9y917ZdS/nxhc8NqRwMoE8sSCiOQktK+pm1QTBCm9IVUNyjVOSVw4Lgos44DpDYwT8gbqc7VCgU1z71RQuAj1GYfkM/Y4M6okokkO1hVucLhPfvQrafz8z/WDbQLRCHKLFcnoycthqIE4G+4cN1npeoVlnSFWyKVlhOV0rA== 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:(13230028)(4636009)(396003)(376002)(346002)(136003)(39860400002)(451199021)(46966006)(40470700004)(36840700001)(16526019)(186003)(426003)(336012)(2616005)(26005)(1076003)(47076005)(36860700001)(82310400005)(70586007)(36756003)(356005)(70206006)(82740400003)(81166007)(4326008)(478600001)(40460700003)(83380400001)(44832011)(86362001)(5660300002)(8936002)(40480700001)(8676002)(7406005)(54906003)(6916009)(7416002)(316002)(41300700001)(2906002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:28:22.9348 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e27b4765-fac9-42fc-d1e5-08db6afd7553 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: CY4PEPF0000E9CD.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB7560 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 8B10C1C0006 X-Stat-Signature: bxkkkuesx714n56447634bserbm9dukw X-HE-Tag: 1686544107-3296 X-HE-Meta: U2FsdGVkX19MeHNiWdRF1OEBdkbmFKRhUHbXr5bKfye4c/YYIF01KFQq80DP8XvPEgnYjvKo0VwC6f9aeGcqaStV85iTQdMaGuV6fHrYKrpf+sL4v/ydxL7OB3hXMQpYaYCN3ByiFJsNSs9dY9buNiR63aBqh9zq2U7kY2nOq5RTb5PmuIVienESIsGxp8Wk9ty1ihuEQenbR8GvAkv/nmsJHvzOqytwZvduYm4QwJwoCGogkqitjIhY25PEDzk0qbIFLgL2Cqcw4x5xN1I+U7FolIobZOHUqdmedFRfC0eEQogWvLl6MnT+IdmGAbH6g85GFeql2lsc0etN6E1s8J/+3AHS180gEh+YPm/gXPWIka+de8t7VmkqMnGItC+3pJGHQvPt92FEtZPjWatVYfX078D275ulsMSbajvG0Mf84t8tb5kWsv1efTbiuDv755CIUDxITYHlCNXHoZ7jFSHsTIlHx/jmsFW/3pe3v/nsXXgPOwFdcmPiGj7s7yjkuzBoU6UTKu/wFcFF9NqoDbeH6M4eM1gpik85An6X9G+uvARCnIWKHGJoMdMnFh5L4dQqUpb/fkRD3BbqxHYZiZOWC9CRhDHVSuTVz8LVbMcVHVsLgjv2fA6L+iPsJq6No3pXsvMbjCMjmh0PI71W8/FdSrLnNTdJfX5RDCFW8Kbd4mqYKBUGUH0C6lNMSNPnzP/TSdYmWEve/8NcyPoO+H9EWTOCGf75y+Zlgu5lDfojrVUBsoUkQNO2NE93R1tllbzaLc5Hz51gt3I3XaHa6Iv5Efr744/TE3akT/ea7oQOX7Vuvn0/IRI/wo89Pj6IKNp0qoyro+YjPr5/oA6T2TcwBwxllsy6REdxc8kbAQSsxi6fb4UPQJPi5dDAjUimS0BGrZ91tUeM3lUJeWtcUJ35Z5+sH586fwiD0Ocjb4zVbCWMujM6xE85aDvg8BxwNB23OCcHzY1UxmlsXV7 i0HyYvp9 F5F8XSR/QXjBM2Qt6C8LkoLgjFaKI07WjZPtmSJVkz7AlUcfmZe8DPyQRCf4SJnHs3oHb12MPeNvruOmLLqDEWWnC8lvTUnDxBYAjFc1v1dWbBm5pu23ERkFF0HwwU6J9zxMb3mu1OEiHKpkrcflRvxHsxNtDilUbcU4FbQvcnjrf3inAfJp4nhoEOJXQlVXrHiNWxMzihgZ2gsWYmWRdZEYn5XDYVNWLtfYZ3gruBTogxLn1xD9qcFvGlQgytqsgmvI/5tRt2QQ9t37FnyQwS8VcatvdOuWLWur+DNyRXFRzznOSJfB2jha4AsJvMrk5SS+j 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: From: Brijesh Singh Bit 31 in the page fault-error bit will be set when processor encounters an RMP violation. While at it, use the BIT_ULL() macro. Signed-off-by: Brijesh Singh Signed-off by: Ashish Kalra Signed-off-by: Michael Roth Acked-by: Dave Hansen --- arch/x86/include/asm/trap_pf.h | 18 +++++++++++------- arch/x86/mm/fault.c | 1 + 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/arch/x86/include/asm/trap_pf.h b/arch/x86/include/asm/trap_pf.h index 10b1de500ab1..295be06f8db7 100644 --- a/arch/x86/include/asm/trap_pf.h +++ b/arch/x86/include/asm/trap_pf.h @@ -2,6 +2,8 @@ #ifndef _ASM_X86_TRAP_PF_H #define _ASM_X86_TRAP_PF_H +#include /* BIT() macro */ + /* * Page fault error code bits: * @@ -12,15 +14,17 @@ * bit 4 == 1: fault was an instruction fetch * bit 5 == 1: protection keys block access * bit 15 == 1: SGX MMU page-fault + * bit 31 == 1: fault was due to RMP violation */ enum x86_pf_error_code { - X86_PF_PROT = 1 << 0, - X86_PF_WRITE = 1 << 1, - X86_PF_USER = 1 << 2, - X86_PF_RSVD = 1 << 3, - X86_PF_INSTR = 1 << 4, - X86_PF_PK = 1 << 5, - X86_PF_SGX = 1 << 15, + X86_PF_PROT = BIT(0), + X86_PF_WRITE = BIT(1), + X86_PF_USER = BIT(2), + X86_PF_RSVD = BIT(3), + X86_PF_INSTR = BIT(4), + X86_PF_PK = BIT(5), + X86_PF_SGX = BIT(15), + X86_PF_RMP = BIT(31), }; #endif /* _ASM_X86_TRAP_PF_H */ diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index a498ae1fbe66..95791071e3cd 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -546,6 +546,7 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long ad !(error_code & X86_PF_PROT) ? "not-present page" : (error_code & X86_PF_RSVD) ? "reserved bit violation" : (error_code & X86_PF_PK) ? "protection keys violation" : + (error_code & X86_PF_RMP) ? "RMP violation" : "permissions violation"); if (!(error_code & X86_PF_USER) && user_mode(regs)) { From patchwork Mon Jun 12 04:25:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275454 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 74408C7EE23 for ; Mon, 12 Jun 2023 04:28:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 14D9E8E0005; Mon, 12 Jun 2023 00:28:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0FE126B0074; Mon, 12 Jun 2023 00:28:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F081E8E0005; Mon, 12 Jun 2023 00:28:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id E41EF6B0072 for ; Mon, 12 Jun 2023 00:28:47 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id B27901C7930 for ; Mon, 12 Jun 2023 04:28:47 +0000 (UTC) X-FDA: 80892815094.13.1345606 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by imf18.hostedemail.com (Postfix) with ESMTP id C1FF91C0013 for ; Mon, 12 Jun 2023 04:28:44 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=dS7c9Ocl; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf18.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.40 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686544124; 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=jk9yzL0mP2Rv14wKUYtpBKvHMRw9BQjiMQ4iCXVZfbc=; b=e5MAwxcXBH+kl1Xb+iElRnCH2T/hjpAb6vGiTYz+ukPPoBUcByJLIA1Ryg060QFe1bSvuP XEMU0Ahlq/Mmgn9tMfqfqDnWIp2IGdIIGGCwzliwQMy5dJFF6Y9//fcr8VjWlhQ3Z385ws OEsdUzaDfk0s2VA+aCzyqxIo3pEC9HQ= ARC-Authentication-Results: i=2; imf18.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=dS7c9Ocl; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf18.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.40 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686544124; a=rsa-sha256; cv=pass; b=tnW+OAUPrZmMGpXKSj688OLLqgP760xLiZvGZjd2B6o+8pEpK2cz6qnEQlceuPe38Jxh97 e931R6dveJ0L2YSljEHa4unymac+iD+oTuqsJ8tsaxiBA3d5h8mwlW0JvXJrYaLHFBdiK5 98Ljrznjfa9UwbBHFz1ZuNsGc/9sAB0= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ctKXp3Rhh+jhEi4kRRRI+iMULRhZj219Og7fQrSvQ4YpY27fgmnKmM1wqngJkDv6atLXothVkz0yiNsaJ8oo/OzGW5pdMY0mHVLnRB9og4P2rqzhPl2L8ys4YFtQc1WaVW9YfXGa67DKL8iax1S7ebLkACCNkVjfwyMikiCGLPTkLvrnAbYnBs1q3s9AhBkRaVgmr1bcgEHRV+b89dSS6+zHtKb8OdlFUJybxiyUf2RwRUVswRnetEiI76wwfh+DQIj6/JXeezJxRkrUb+7ukO8hP0EGelSdpxVSf+DtKJR5Lwh8Ic0ym8uvgMQAhpNZQ5hL4NmMWOyRwWXriwDEdg== 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=jk9yzL0mP2Rv14wKUYtpBKvHMRw9BQjiMQ4iCXVZfbc=; b=Zcyp1GSOR8njiKd74DUV+FhDZ3g+Gzv/Uem3PHBu2x5bXLFHtTg3zcXblc3hQ/PGyxZo838wVlPfdW8aYu91PXLBf4h5CUe3e8w3bKegQ/Nx0nJQIaBkxF64Y3jz9TCUh+RJpy4pA4+UweunotntkI4vU0RhyvIU938X2R2D1HALx4G+ooico4MEIoQ4dZkbvE1FuiE6VUUZ7ZTjFVwx39d9vstbw0e687lPngQ1d94dVFXCHnWEsv7pTc6SVst2luSWd3a94+G9mD9pX5auOsD8WowseG9798MBTE8iDmuHWEJJQbkMgnAusBxa3bkElGXo90kAIXlArL1NFRRFDg== 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 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=jk9yzL0mP2Rv14wKUYtpBKvHMRw9BQjiMQ4iCXVZfbc=; b=dS7c9OclY+LS+q9vROLu/KQsyjmE9zu0vC5cNRkcxrvIjnA+Myqp0CvJ6coHDnZlVlCO2m6lDL+fqA7ZXlHEe0L1tGa8L9yZ7pzvvRZB/+NYOfwhftaazyOj5QqFW3hFFuqTfehGlrmFaaJFQelnif3lxQshfHJKmuelydM88Ao= Received: from DM6PR11CA0029.namprd11.prod.outlook.com (2603:10b6:5:190::42) by MW4PR12MB7382.namprd12.prod.outlook.com (2603:10b6:303:22b::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.33; Mon, 12 Jun 2023 04:28:41 +0000 Received: from CY4PEPF0000E9D0.namprd03.prod.outlook.com (2603:10b6:5:190:cafe::f4) by DM6PR11CA0029.outlook.office365.com (2603:10b6:5:190::42) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:28:41 +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 CY4PEPF0000E9D0.mail.protection.outlook.com (10.167.241.143) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:28:41 +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.23; Sun, 11 Jun 2023 23:28:40 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 12/51] x86/fault: Report RMP page faults for kernel addresses Date: Sun, 11 Jun 2023 23:25:20 -0500 Message-ID: <20230612042559.375660-13-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CY4PEPF0000E9D0:EE_|MW4PR12MB7382:EE_ X-MS-Office365-Filtering-Correlation-Id: ebbc56db-f55d-489a-5d8f-08db6afd8041 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: dVaVaCSnwHogKXOGyNX3sOMjlPnoUc4F31VjOHowW1uLQaNhgsnmm72zPDfSYO5we62CmLTKkURz727sx73JF5OFEi10FswjAvLJoQiTX0Ck/YJT1bU2InCXubJzqyKNpkvV9NgznC1pfYyleo/eLu18pUI7EJkD7ZkbOMRfLHDKHLDguG92ryGghWhjFctUjl45rA7NVT0cDIsMgXj0Z9FX5VxPg1qlDBOoz2VamFy6rslXKZgxFeuM44Lj5KKIhIHAspxxSLHkEPuOvKtPrxDFV9adNvl/1Aowm7E5woUPSCPSysuILi0b+PAma9wg8uJRh0i3niySO2IilnoKjvn8Y+JVNYqdVdiTEgKyWK4vWwK5mDAM0kaACchF9RVMbexuVYsKVy9P0l5qxrIEFOkP5+Z3Zlba4oeXQ9ZcHqoO5Sb2RKWp9YZ1a88c3oGUl6toYiejKmJDtbnh80G6nBw4cC0i9Y/Kk4QbMTvqGzVAybl41zonmKwb+gn44k1lbsQWW/uUlkQRtuCAl4+ROt0U+ONIUmJ9yYapCA1ULlVkGpHmind/4DAw1/Zo6YMU0IKkGM9WNyl4mmS/spAqEdbSiIsSaVmh+7HrHvBZS75Gs3jWJs3cTCpD+avo8dI49sxeHMOGQhemHLr1AUs4Telu8ImiAdyV9snrpYQjtg1TMeMj0rkzjYRZuRVLtJ1KvDspaXTiqw58IQmxfXhrawjtjNLLHhBZ/CbF5lDQQMXw3BcJyVNUxlSJ5TxGx/DWT8gyUWy/eXF2T1ECjKwT/A== 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:(13230028)(4636009)(396003)(376002)(136003)(39860400002)(346002)(451199021)(36840700001)(40470700004)(46966006)(7406005)(81166007)(7416002)(70206006)(70586007)(6916009)(356005)(4326008)(316002)(41300700001)(8676002)(40460700003)(5660300002)(8936002)(86362001)(36756003)(82310400005)(2906002)(44832011)(40480700001)(16526019)(2616005)(186003)(478600001)(6666004)(1076003)(26005)(47076005)(54906003)(36860700001)(82740400003)(426003)(336012)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:28:41.2532 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ebbc56db-f55d-489a-5d8f-08db6afd8041 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: CY4PEPF0000E9D0.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB7382 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: C1FF91C0013 X-Stat-Signature: akc15c55c4y47izfjpb6gq5q79emjm7o X-HE-Tag: 1686544124-801604 X-HE-Meta: U2FsdGVkX18ernmZzibn/iz4bhvJHnbgUBDM0zywiyy4n7TilHnnL5dl+pHtJk0/jL73IhLaWBGHXSlxDyxpIRboINdZWjTJDbxAbouqeupABVzFHurPy1T+TA+RIvI0gwUF8+SrsfCnchhxh2hjTGjYLkAzqYJhrYP6g32nw9dAeDcG+1WDsAQFoqP0tacxql+p2FKHHitOAyaZPeqkU+DGRi8GBkvUocuXQDY5mF8Os8mRFa+EJuCtU3PQ3KhJM5+TT1u4/sd3OtdJnDGXoLl8fR6WFjfi5YTu+fnGgD2Rc5/gOhxKoKKdTKp5rmqOYHlpPKNongov41REW6+bErPJTXI4iwap5YQxgR97W40lnUzKHRHNL/X+jVrpyfRT9v5lZbbCmfD5faYG8qPnLPICY135yuykEPlMqPovrDJ89xxFvEF4FviB1ogMxHotY1UAiB9hXwFl+mV+wKc+VQbjQPTA6fBknrCQ1v4BdIbhaAzCKHieeXwcH4jHet6PKSHdR6TkjYMLITO8Zlc+wnno1+S8R3j1ZqMqKQ5ZackgJF0vdykQYsSl8p0DaHL3PGIIqAgIQfJcJDAILR/zg0kOjv5USkxXypjfccLoFeL7trAKvDYP8gt2yntwwUJqpE3DaXC87Z1TbhtqAi6bf8JKMqIOT5GVdwY3Cc9+C3oM38tyTPwPVGXXxC8PQPrm41NmJv5i0DMGN2KbGu/VJ1GaPdwGX6dlyR7G5kc9RRLsk90a/PDq1rojD7eCEc2Y0jwp9rxvNcY2tkfzhb1+hk88ElAirLPzLOIPxu1eSbcgOlXqHykr9T94abbkCwmRWYuMqRsWHbMIQyy5E2L9LqvEnvKP4hxQcHvhtjJlyWHCou3VLno+yk2NKz26a6EgSJaB5oNcAOp1c4c4SoqZ/JnH9g1hdYi3RDuOIRKOpANRh76ZSAVag/rthjUAWcLkxTg6AhSbaUXO9iKRn8z FqhKneV8 VNhufAyVwM41jcuqz61s0DmOvyDA4+/H+NuZ+CPkLol/+A42udM0hz98NNL5e8HBjSWrIuiohHdqR+qgRLo83FwFfYL/t+cqbW0UeL6vsyWSoB8zw7PYWo0T0/rFaYDfSNUfebiMqm1s4JhFmHFFkDE9bjO86D2daSs7JSNgIUXS7mUWxHOKJKWYpywYdmprJuBp9hwJBMuctHIIiHqN8zl2v4/ZWay89Rqyh4duN+9UvRzvCIHFYNkuS6dqWhxnWVSXMZ0f1yqSCUbelSrT5GndWkJRA4NftgaIycxh5rXtLlEP0TUeTOi2S/4U+uaTz5NaO019Q3mvEOSD/2tlMrFYl3VAw9SgwL3zI2b56iQOUGJHfnBS0RWpIpx384cG1j8ja0KoCPgX5tWN6qikEGzavW9F9RjLdWrs2w5h5ghmbOiM= 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: RMP #PFs on kernel addresses are fatal and should never happen in practice. They indicate a bug in the host kernel somewhere, so dump some information about any RMP entries related to the faulting address to aid with debugging. Signed-off-by: Michael Roth --- arch/x86/mm/fault.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index 95791071e3cd..d46b9cf832b9 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -33,6 +33,7 @@ #include /* kvm_handle_async_pf */ #include /* fixup_vdso_exception() */ #include +#include /* sev_dump_rmpentry() */ #define CREATE_TRACE_POINTS #include @@ -579,6 +580,18 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long ad } dump_pagetable(address); + + if (error_code & X86_PF_RMP) { + unsigned int level; + pgd_t *pgd; + pte_t *pte; + + pgd = __va(read_cr3_pa()); + pgd += pgd_index(address); + pte = lookup_address_in_pgd(pgd, address, &level); + + sev_dump_rmpentry(pte_pfn(*pte)); + } } static noinline void From patchwork Mon Jun 12 04:25:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275455 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 99DDBC7EE23 for ; Mon, 12 Jun 2023 04:29:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E93638E0002; Mon, 12 Jun 2023 00:29:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E44226B0074; Mon, 12 Jun 2023 00:29:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CE55B8E0002; Mon, 12 Jun 2023 00:29:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id C023C6B0072 for ; Mon, 12 Jun 2023 00:29:43 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 8EA83AF211 for ; Mon, 12 Jun 2023 04:29:43 +0000 (UTC) X-FDA: 80892817446.15.5AF1687 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2078.outbound.protection.outlook.com [40.107.237.78]) by imf01.hostedemail.com (Postfix) with ESMTP id AD28440004 for ; Mon, 12 Jun 2023 04:29:39 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=z5K4dPyK; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf01.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.78 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686544179; 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=0d3dQu/aF3mwtDoMAmT2kZoy0ldEPLwmCUnZfhjJTkU=; b=XcJVZnbZR6khJpJwDbfomajG0K7s4Cw/WpNJcQ7IGxnJj0XYhPqCFfm0KW7i1brSxGJplD mzWfYlVWcXKY0dfJPNsQvkD2FKac2yWY+v/IfYmhRawVHulgawXU65coZjiNUfSbzVDuaY /Pgf7SBD8k0M5dEIrs8dl5/6Y86n6F8= ARC-Authentication-Results: i=2; imf01.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=z5K4dPyK; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf01.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.78 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686544179; a=rsa-sha256; cv=pass; b=pJgzAYdVYe2j9ISfKOQNCOilA5FdbErqlO7AzuipTNRhQ7drUzQj3OSDJC9SUzufPRqUQK 4OBbHA3UECkhfcz2H3ogrKo6Vqun7WQ6dmeqjxv7IUzGBFzkPJRGkKTI3lQ89W6+MeaGcZ 3WWnlnM7HhPjNp1F95vfP4wNT270TaY= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QB5gB7E7JZbWL5dBsZrTMV9nkBDPKPIvjmirCxurVZNmEFEVb3vKqLRewFYWroO+iY7crn78wn8NRlWNPCN2ltCTGMqbtW4H5ZtWgOt/a3Ismd8kqHIQOQ83jL56SuvQxKgKYJS4gm9ajJzSJf6SmT2f7HArC/PNtF0A3EIzHYtr3RUZEbLt0kgdVy5SmTqyzPIvtFjezH7eE8DEYYfhM78mcFPk86hOKfS0gvgvpOzet8xmk8Kc8Tgtm3X3fiF50YPhjUKOaWWBq5pjpfucCb4WTmXYIqd95PxH8KFdTdmPs6PsHi/smKY1ACRkLqzRMPb29Oy5WRuM4QISdhtY7Q== 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=0d3dQu/aF3mwtDoMAmT2kZoy0ldEPLwmCUnZfhjJTkU=; b=fMKwPt7SBgqPmsvESGp2q35mWSdClXs2Pj/+CDpFCSGzeLN+XqJgeQYkMf4p1pYlAaqNWlGFNs8cX+vz3vO4H8jXy9oJmsIQvYKHhXvniAUufuhtGdldcOkW6IfQzPVmarQmR4tr0VyVG+zOVPKOteTu74V0DxufoMDELCCLndi4rmVqnGb1H/fjHTyW5ZUwPi/4crSuGOQm1p0qOnRrkDpawEhcC1Ayx44rZr8akl8XHsJ8irqMFK7CY9m7mGYTyzzSI++wk5IAEwsvDpZ/I3lLiLI2pzZr8SCDWTFIRm8kSfF2lpFZUZjbpJ8EINsjnNHOfaLd12lS0Y1VdCd0HA== 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 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=0d3dQu/aF3mwtDoMAmT2kZoy0ldEPLwmCUnZfhjJTkU=; b=z5K4dPyKhy/4yFKD2LCylE3xg5Fmxyx3XXi8TKox4EGKWgVYu/Jdfu+ZNV1lBlkL8L3sPk7HmM1NdypyZg0pTQPT4ml47uFF/qUoHWiR6ITJ34SHyfxpx6RsohniHD6PJkhmxVmfK3t3KtHPbcwkN9WFtCHhfQq27gpvWChu06w= Received: from DS7PR05CA0059.namprd05.prod.outlook.com (2603:10b6:8:2f::32) by PH7PR12MB8828.namprd12.prod.outlook.com (2603:10b6:510:26b::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.41; Mon, 12 Jun 2023 04:29:36 +0000 Received: from CY4PEPF0000E9D4.namprd03.prod.outlook.com (2603:10b6:8:2f:cafe::2d) by DS7PR05CA0059.outlook.office365.com (2603:10b6:8:2f::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:29:36 +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 CY4PEPF0000E9D4.mail.protection.outlook.com (10.167.241.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:29:36 +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.23; Sun, 11 Jun 2023 23:29:35 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Jarkko Sakkinen Subject: [PATCH RFC v9 13/51] x86/fault: Handle RMP page faults for user addresses Date: Sun, 11 Jun 2023 23:25:21 -0500 Message-ID: <20230612042559.375660-14-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CY4PEPF0000E9D4:EE_|PH7PR12MB8828:EE_ X-MS-Office365-Filtering-Correlation-Id: 4c459862-4178-4799-b1b5-08db6afda10b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: yqxNPlB2qGzRvl/LTI4VOTNiZ5WbycA91BY4M8iNOfGbc1bZ0/bpqyP7WQ/Me002DIp90WL/wGSSaFiBUoRC+O8FzjP1i/rXZW2oprDkBDN5qydg8/fzjdjpL39dtykKYfZ+pKfDh41EGnIgrerTLYhc+3ZY0bzCYqBWYM/Oq+EzKPyQKi92qG/qwAf2FG2LPWicnWJ7sSgOnPk1UMa2C7tFfEaUwZUcscl2rYLDxljx3IRr7RPM9hQnrIFQqwCc58BCQqG1MGoUBUtmO5oV8NrV3IKhflcYYoUIyXIDcyEAfGMdjrX0wnbZ79YV0gqhN08qX67sbWGgbtnWnrJ2F6fHpVwTC2tYIliphRZYifK4eTe5RM8d2sC21BEcQQh5dvqz3EAOJzOOlUanw2mb4PdPXVbAC34bfJHs4ohkKR0ysYFT/wZgXIxG93EYfv9/NI2w7h9/1djsz1t18x1tNyQY0qgyw0BnxvvYZQfalVSqfczrGpCLAddKk/yJnTKFyq/ToysBOUqsscoHMyp+YWYCj42zH+xQ+Sx+pFmRuveFAkZmm0ffc1yY3HPhOldbFQQq5em28TV4a5YOdN4LF7MSWM+YCMQX17mBfUpzwSPGs9ba2YRN//IOBQipFPddC/hoyrM7vRLxyxjniVE/Li1QfJceJAjkczRvfNPfzloAq+ADZezooKbeofM6k584u9TnbYlAF9VrY9WnNzMJONCwQyMTiz2jUTma9WuAwkGPDq9TxAXivmWGQxRSlVhA2X1dALiEa7PFFy1xGy5fWQ== 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:(13230028)(4636009)(346002)(396003)(376002)(39860400002)(136003)(451199021)(40470700004)(36840700001)(46966006)(316002)(40460700003)(41300700001)(336012)(82310400005)(40480700001)(16526019)(186003)(26005)(44832011)(7416002)(47076005)(86362001)(7406005)(36860700001)(2616005)(2906002)(83380400001)(1076003)(81166007)(82740400003)(356005)(426003)(36756003)(8936002)(5660300002)(8676002)(70206006)(70586007)(54906003)(478600001)(6666004)(4326008)(6916009)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:29:36.2818 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4c459862-4178-4799-b1b5-08db6afda10b 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: CY4PEPF0000E9D4.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB8828 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: AD28440004 X-Stat-Signature: j3cgac6aabauw61cf9y81kw1hd958pqj X-HE-Tag: 1686544179-767146 X-HE-Meta: U2FsdGVkX19R81yxnYf3ubRu9GSKEXzRtjMlOKok2QateSyoEFATNhyJeROTpTmD96/Umfus1WyLZSEyjvT78Qet7ZhPwq2O7ylyrT/DcKFg3qFU+Ty9Ai4aX/iWbF7i/maEpY1zoEtkWekPzYQ3wob8nborVLJa3oDk3GjbUbfgKlrb7EhM+sLQ9Luvqp7NAoeIKOyeYe7NmB+Uec7A4VBds04kwnzp1ap1Y/4eDrHeokX5uBoNnNi5i9Nhdcry4g5bvoY1c/bFqkcNj4bai60dTZYp/bA8vEEKudc1bL8vSL26gZ1Rp8fLYi3WmCfqeAO5b5yfUkDkvOabuQDgzuY/ZKNt2GLLg/xo5yh/xoE6DkhPJBqvbq0FiBfXitLDVmitmGsPw6XFYq6olmDGPFo/luorxCASC2+DVBYyZB7RhoQ/SVFthNJ9+ztGxqf+OieqIeBoD64EQn5mcvKBmBV495pVQHUBC8SOYzNYpBUV++PMOR+3KZFDnos4Q5hxaH0nYJtlr7TngjQ5P6rci4GQ6xaLVncXp8v5sBIhssnwx0bxvWogpvQEOx4l672B6KWpnRa0y6L+AYyVSpmwVNdbogP7kjTOkO13K36evDyuQEikvy6jZEgmw0Ga27Q3VHk1rEH0H22NLStUMLyjJD1aqzaXsFHnkgrcDaY4BnHzc2rDUjxA4oIKonllLe5FnfE4/bWb59/dKGR4LZXZC2pqDZEA64ATmRh8X901DNTaKeHmK7WF0Z62fmgKzaX2ATfPueGoQU73SnO8s/gJT9z5NQrHgD2u7NzillXIH8/RVEkhPE35GZg/hUtUEvA4Y1mevTzRc6Ut8xBMc0jTccqQiPuXxSyFJ2VXpslTnpuNhRSQd2ZsVEKCz1J1Dpfm4Yc/xm8rN6tB9x6jDkQNEo0Yrs2UyTn1kC8bF03lSEKIBUYA5ryum0h193sCuNTue5yE96Qab3qSyTjN4NB 1uzHPqkQ I1JjfbS1vTivueXP2wqIxSAeYOC7O+ODKzz2zxp0gYWuG4PKVTVhhxLYpzmT5QTcCvQOcxdXWEIDQPFZfvZmAtTNXmxR2W8yeVoqPJ9Pt/FbUnmJ1wK4rZ+BTOPfiY63aScr9lNBvUX0YxRb8v6GXyWfFHAlhGxrvg+vG5WXXlzXIXff74UMzTtFLLElgSSgekWqTDitlvuAD6rKfc5Px3ZJgToL0x36w/rYRSaITNxU8rwbXWKas8cYuFh3mI0Kd8r1yrl0OFG69ItPMcKRIdL3ViZlqrzroWhIGnjQvsAV/vJl2SxmA4m6dfvWX6KTpusDiYVN3aDUFK3lEAuaf7teURtFUkiU4DJ+3S2nIIZToGGW6+OhAUpFCiw== 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: From: Brijesh Singh When SEV-SNP is enabled globally, a write from the host is subject to checks performed by the hardware against the RMP table (APM2 15.36.10) at the end of a page walk: 1. Assigned bit in the RMP table is not set (i.e page is shared). 2. Immutable bit in the RMP table is not set. 3. If the page table entry that gives the sPA indicates that the target page size is a large page, then all RMP entries for the 4KB constituting pages of the target must have the assigned bit 0. Nothing constructive can come of an attempt by userspace to violate case 1) (which will result in writing garbage due to page encryption) or case 2) (userspace should not ever need or be allowed to write to a page that the host has specifically needed to mark immutable). Case 3) is dependent on the hypervisor. In case of KVM, due to how shared/private pages are partitioned into separate memory pools via restricted/guarded memory, there should never be a case where a page in the private pool overlaps with a shared page: either it is a hugepage-sized allocation and all the sub-pages are private, or it is a single-page allocation, in which case it cannot overlap with anything but itself. Therefore, for all 3 cases, it is appropriate to simply kill the userspace process if it ever generates an RMP #PF. Implement that logic here. Co-developed-by: Jarkko Sakkinen Signed-off-by: Jarkko Sakkinen Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra Signed-off-by: Brijesh Singh [mdr: drop all previous page-splitting logic since it is no longer needed with restricted/guarded memory, update commit message accordingly] Signed-off-by: Michael Roth --- arch/x86/mm/fault.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index d46b9cf832b9..6465bff9d1ba 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -1329,6 +1329,13 @@ void do_user_addr_fault(struct pt_regs *regs, if (error_code & X86_PF_INSTR) flags |= FAULT_FLAG_INSTRUCTION; + if (error_code & X86_PF_RMP) { + pr_err("Unexpected RMP page fault for address 0x%lx, terminating process\n", + address); + do_sigbus(regs, error_code, address, VM_FAULT_SIGBUS); + return; + } + #ifdef CONFIG_X86_64 /* * Faults in the vsyscall page might need emulation. The From patchwork Mon Jun 12 04:25:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275456 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 55516C7EE23 for ; Mon, 12 Jun 2023 04:30:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E47286B0072; Mon, 12 Jun 2023 00:30:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DF6208E0006; Mon, 12 Jun 2023 00:30:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C96968E0003; Mon, 12 Jun 2023 00:30:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id BC22E6B0072 for ; Mon, 12 Jun 2023 00:30:04 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 5A2A41A027F for ; Mon, 12 Jun 2023 04:30:04 +0000 (UTC) X-FDA: 80892818328.20.9880934 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2087.outbound.protection.outlook.com [40.107.92.87]) by imf22.hostedemail.com (Postfix) with ESMTP id 5662BC0002 for ; Mon, 12 Jun 2023 04:30:01 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=5czQLo7v; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf22.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.87 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; 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=1686544201; 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=vFANIoBEvW5rShPu60LwTEesSM4XP/tF5PsDV4uYZYY=; b=gyusRj5w48uPdJ7MEt+TjM6MR4YwNWqspDJu4a3Dt6+obZpRf6xwGR4vAc0qfbyrEw88AD z2KT7tY+eQAAWW19tFR+VjABn6r2Xt4azjO0iQn9D5RV6Gokh2QtTYo4woaDOd0fWyapVM PMLeRqHFDqO2LQ2K30cfFZ5wU2qBKUE= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686544201; a=rsa-sha256; cv=pass; b=XbKjOnrqlIiDiGAlLDrAwKhLVzwhVBmmuDCxN9wgrSv8mfKdNTDr0GciKbY88ShNd5Dorf lWZgNuYaT0tnlUvMq64tSSitBvlWq312m5hhSJa1Y40su7b8shvVZT4bHFROsL7dtctY9g JLU8ms/AYJIUjqPDAtbQR8Mz4lBiLzs= ARC-Authentication-Results: i=2; imf22.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=5czQLo7v; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf22.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.87 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ON74fhmb2Z1ghvMvCk83tRfulyUCwUfEX0mIiLRJ5HOrELUXkpCmstlNptzUiuMaPizjCqDnWzHDm13IV14gEhlNr3p1gG067KsT6+hEmEN2Cnj00d7h3pJEXoPS0/O5tlFD0i54M9kJ3YKR3QISbLVyX/voxb8C0kBc61qQ7jwQL3hBWNobnRgROclpLedspwh0EP8IrEaRWa8rWnO/enZAx/vOiLptA/fHbbQg8X0is9Z+XMJg0U9Jfokq5e67R2vag9nTTF6tFnJ40G20AYRj25nWefSieb1KHsojHpmXQTEtoyAF8syTXerIxk/GD+05acuCoVysGik7x2bxYg== 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=vFANIoBEvW5rShPu60LwTEesSM4XP/tF5PsDV4uYZYY=; b=G2rOHByURF1yvDTl6L85QhtZcek4j0tbbVmTvcz+lkabi+f31m1VMe7FAM7bANfy5qYi7H44ENWy0WvE2fPtVHPU9rMhj49upkgH9B3WdeQi0gi0yvaju8Gf3dCC8G35oEVcpJd/4f8zLSeOhgJ3purtmh4t7eD57wmX/0fk2M+rYOGn10aef+zF3veYHBHJAvFmvHy/R83mSNSvR7Mu8N/5efpqWm1jdXmEfvZtFuyh145JwHNM0q2j42CBpi7vGhzJX76Wm7yR0cRx7H3ictIOr+Djj8bAwfYvmUglVHWaCs2MYASk97dwqTAlydMOxt6j74y5kKaVfyTATMrtLw== 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 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=vFANIoBEvW5rShPu60LwTEesSM4XP/tF5PsDV4uYZYY=; b=5czQLo7vQZY+/u/TtvChvGoD5d8M+8RiSVaUvU60SCNKA35fwdaExNjiUHBbJBrO2zQcu5j9XFET0KidVLGfxmJm0UlQe7P0YZp83q5NG9iBL4ebavXBSMyV1wlxnQAFwh5gYzyeYpDWWo5QjgaZLzklivau4EGMKH8Pw0UTcG8= Received: from CY5PR16CA0016.namprd16.prod.outlook.com (2603:10b6:930:10::26) by DM6PR12MB4435.namprd12.prod.outlook.com (2603:10b6:5:2a6::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.38; Mon, 12 Jun 2023 04:29:58 +0000 Received: from CY4PEPF0000E9CD.namprd03.prod.outlook.com (2603:10b6:930:10:cafe::dd) by CY5PR16CA0016.outlook.office365.com (2603:10b6:930:10::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:29:58 +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 CY4PEPF0000E9CD.mail.protection.outlook.com (10.167.241.140) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:29:58 +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.23; Sun, 11 Jun 2023 23:29:57 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 14/51] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction Date: Sun, 11 Jun 2023 23:25:22 -0500 Message-ID: <20230612042559.375660-15-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CY4PEPF0000E9CD:EE_|DM6PR12MB4435:EE_ X-MS-Office365-Filtering-Correlation-Id: 39ba6556-3c53-4fa1-dc99-08db6afdae0d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 04MwOKBMy0hHqW72uwGFCIQAR5VZ0XGfGh13jB0YlT/VVFeD5TBEw1iJJVQCM81pyaGS2kKKNx2Bwct827AM6ESe513J7QhQBRxY38+3R1qZVDVqCOtq/qDPNbIfafwB9zhPLP/gn8rMQzOL46tpSMuvV1gVsp0IktEoIsg1Djfx7bo63PW7Z2E02znn+yMCVCnybCs1i3PQuszH7RPpFqk42flhhK+cs9jjMhpgLAxFcckSmW5nuVxLnlxHP0xke0TF6FUWzRTMb1qsi5uBcQPPKj8hvxL38aBlg8RF1P1p3SdwvtzHl6gVQKrx5NXD72YTvu6zOoUc16mQF4l+ibznEYYn1VHHBtRAdYrdRM2mC6qQ7EtSwlUW1QQwzFB4ozacij4TSDb0HsnmGKS2glp5EXfQVM0qEkdBJ79BqgJ3hc7pLjxNmAGUM7QrCTkd1X7V21hX5xEz09rXNn9s9kmgu+AdBVPxToHoOM30umP4vTeEBm8SaOF0zdatKN+2pWEgwm5uVR4bc7iXKAPPg2zjEw15aUaZU2X98NEjmWt6RKZyNW9YTyA/hDg/dNZMWDXAWgyt6x1+1DsYsIGzudux81NLzmpsLugb81r5SKahHa8PsBrSXUYZ2lJa8hEgiK+91SaA/tyWd2BUkvB0tPnLl2lH4DR8q1ACy5aYTjLrVJkIeXT8VdEbh6LGVqw0xkX1k35wHWOogPcTEBRsq2R4IePsLc2yDej7J0gssohqozPqngsQjIf9BtSok7cj9jivkRY5shtvcXnSOrmfBA== 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:(13230028)(4636009)(396003)(136003)(376002)(39860400002)(346002)(451199021)(40470700004)(46966006)(36840700001)(5660300002)(7416002)(8936002)(8676002)(2906002)(70206006)(70586007)(7406005)(4326008)(54906003)(44832011)(6666004)(1076003)(26005)(316002)(6916009)(41300700001)(186003)(16526019)(36860700001)(356005)(82740400003)(426003)(336012)(47076005)(83380400001)(2616005)(40460700003)(478600001)(40480700001)(82310400005)(36756003)(86362001)(81166007)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:29:58.1065 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 39ba6556-3c53-4fa1-dc99-08db6afdae0d 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: CY4PEPF0000E9CD.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4435 X-Stat-Signature: twe8c8y19y3g7m7u5yrxxk5dbukhhcms X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 5662BC0002 X-Rspam-User: X-HE-Tag: 1686544201-63321 X-HE-Meta: U2FsdGVkX18EMcgwQsO/vJQgkx1mhqmNCBJSyCaSkwmfQKJaDRMTYNaj3vqzJRDv+ac74MqiXog/VIEwYNxBO44uN9e4kGJH+sBGOJmjfeSOUeGS2GcgJQpeU78AoLjSj1qbDrp1+8F8pynxdlQ55i82m+KtP+z+sShdiQdqMFpYIbx2G+xf/K1KzUndpHnAYwc35NE6b8CD1+MgPB+DRmfEHVkyM4Ga84WZkRBOJmlpERYNw0sryXWtL7Ne8gSo1dxcW7kiGGqQ03w8oV5vODqhTfCYYFKIqlz0SLzMACWx2Bk6ZpErGDy2K1Dz46yWAy/rlCImuLNfBJhUGN1D/cg5TEkSduoE1Ojt7pGaD1MKefSwpSZ1fkFRokxg0zdZDi+jhbDPJVeZIl8VOYqpY65x/UiQoipUMFXbdNp7uLCQMm2V5ngLyAdCrSleVb54U1iKaZmUJP3uKD2/ik3fFiWandLpAnxSUfZewdqTb6SYJjQ9+ZE1tax6yn6F7Y9G9Dvx0VYNlipoKkJCJM1pnXATrcZCpXDHlfjTnsbc6ohz9Wp20GWIVR8S6SrYpD+h2OSfRmhEzu4zYWEnDGTR8UtC4sZdogUi6ZHC15zz/+FvEBUmbT1DLirVDzQq4LBVWjUtQy+qHJvSW/RqcRiuiZwxy8ChUF0xOjluzmmwxllwROYxDVJCF4+gZhOcip4stdDpj2IYFxR+dyNo6ZteMVrQftYXmH+/St5733KT0fRO8xeZ983iX1xNExWNw0d07l62EN3LP6RJ6SvzmAFbIqzXhRUYlTX56WfMHeKWrZFaccFKXt8dl879P9f0ZLCsCapc0I994Teugr69Wz9PO/bsG3L+epY14MSXb449ONHAv1ekVACtGqTPyuy84bDvWu4OeEZbea/96pnHFLIxnkzh6sJ+/wjMB5VQ2emSUjscyBqhUw9vNIytYj7Ij6JDc9m3q8outn50a9IMtYX 4e0nkc+U KIzvuUeDvDyGJanV61rQJSgNzGWQVbBjbt5NkG2e6YjnitcTWki1wmFoVD8s29Y6qCIKQnKYh05vw/vZGk0G44Xen8aW0wT5Rrufk8DSl9QOt+z1YldfaILeCtaQw1xkAWRQ08FJ9mrwOk9y3Jp3FTta8Xi5FLCOQjSrZYmU/uC5AfnmORVLIX6CoLJhqw1Qoy+OqLHd8aHKG1thx7eK+iG3wvig2SFnrgmkPPm/bWpyG5FdmShNQ198JiEemcYOYS1WyVZUaIMk7vD1L5BGrCqRJ8MzcE7qfp7RRNnTZqFiq0sQ5hdmYIT83gg== 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: From: Brijesh Singh The RMPUPDATE instruction writes a new RMP entry in the RMP Table. The hypervisor will use the instruction to add pages to the RMP table. See APM3 for details on the instruction operations. The PSMASH instruction expands a 2MB RMP entry into a corresponding set of contiguous 4KB-Page RMP entries. The hypervisor will use this instruction to adjust the RMP entry without invalidating the previous RMP entry. Add the following external interface API functions: psmash(): Used to smash a 2MB aligned page into 4K pages while preserving the Validated bit in the RMP. rmp_make_private(): Used to assign a page to guest using the RMPUPDATE instruction. rmp_make_shared(): Used to transition a page to hypervisor/shared state using the RMPUPDATE instruction. Signed-off-by: Ashish Kalra Signed-off-by: Brijesh Singh [mdr: add RMPUPDATE retry logic for transient FAIL_OVERLAP errors] Signed-off-by: Michael Roth --- arch/x86/coco/sev/host.c | 94 +++++++++++++++++++++++++++++++ arch/x86/include/asm/sev-common.h | 14 +++++ arch/x86/include/asm/sev-host.h | 10 ++++ 3 files changed, 118 insertions(+) diff --git a/arch/x86/coco/sev/host.c b/arch/x86/coco/sev/host.c index d766b3bc6647..9df690b0b263 100644 --- a/arch/x86/coco/sev/host.c +++ b/arch/x86/coco/sev/host.c @@ -338,3 +338,97 @@ void sev_dump_rmpentry(u64 pfn) } } EXPORT_SYMBOL_GPL(sev_dump_rmpentry); + +/* + * PSMASH a 2MB aligned page into 4K pages in the RMP table while preserving the + * Validated bit. + */ +int psmash(u64 pfn) +{ + unsigned long paddr = pfn << PAGE_SHIFT; + int ret; + + pr_debug("%s: PFN: 0x%llx\n", __func__, pfn); + + if (!pfn_valid(pfn)) + return -EINVAL; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return -ENXIO; + + /* Binutils version 2.36 supports the PSMASH mnemonic. */ + asm volatile(".byte 0xF3, 0x0F, 0x01, 0xFF" + : "=a"(ret) + : "a"(paddr) + : "memory", "cc"); + + return ret; +} +EXPORT_SYMBOL_GPL(psmash); + +static int rmpupdate(u64 pfn, struct rmp_state *val) +{ + unsigned long paddr = pfn << PAGE_SHIFT; + int ret, level, npages; + int attempts = 0; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return -ENXIO; + + do { + /* Binutils version 2.36 supports the RMPUPDATE mnemonic. */ + asm volatile(".byte 0xF2, 0x0F, 0x01, 0xFE" + : "=a"(ret) + : "a"(paddr), "c"((unsigned long)val) + : "memory", "cc"); + + attempts++; + } while (ret == RMPUPDATE_FAIL_OVERLAP); + + if (ret) { + pr_err("RMPUPDATE failed after %d attempts, ret: %d, pfn: %llx, npages: %d, level: %d\n", + attempts, ret, pfn, npages, level); + sev_dump_rmpentry(pfn); + dump_stack(); + return -EFAULT; + } + + return 0; +} + +/* + * Assign a page to guest using the RMPUPDATE instruction. + */ +int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int asid, bool immutable) +{ + struct rmp_state val; + + pr_debug("%s: GPA: 0x%llx, PFN: 0x%llx, level: %d, immutable: %d\n", + __func__, gpa, pfn, level, immutable); + + memset(&val, 0, sizeof(val)); + val.assigned = 1; + val.asid = asid; + val.immutable = immutable; + val.gpa = gpa; + val.pagesize = X86_TO_RMP_PG_LEVEL(level); + + return rmpupdate(pfn, &val); +} +EXPORT_SYMBOL_GPL(rmp_make_private); + +/* + * Transition a page to hypervisor/shared state using the RMPUPDATE instruction. + */ +int rmp_make_shared(u64 pfn, enum pg_level level) +{ + struct rmp_state val; + + pr_debug("%s: PFN: 0x%llx, level: %d\n", __func__, pfn, level); + + memset(&val, 0, sizeof(val)); + val.pagesize = X86_TO_RMP_PG_LEVEL(level); + + return rmpupdate(pfn, &val); +} +EXPORT_SYMBOL_GPL(rmp_make_shared); diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index bf0378136289..9eb20b416251 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -171,8 +171,22 @@ struct snp_psc_desc { #define GHCB_ERR_INVALID_INPUT 5 #define GHCB_ERR_INVALID_EVENT 6 +/* RMUPDATE detected 4K page and 2MB page overlap. */ +#define RMPUPDATE_FAIL_OVERLAP 4 + /* RMP page size */ #define RMP_PG_SIZE_4K 0 +#define RMP_PG_SIZE_2M 1 #define RMP_TO_X86_PG_LEVEL(level) (((level) == RMP_PG_SIZE_4K) ? PG_LEVEL_4K : PG_LEVEL_2M) +#define X86_TO_RMP_PG_LEVEL(level) (((level) == PG_LEVEL_4K) ? RMP_PG_SIZE_4K : RMP_PG_SIZE_2M) + +struct rmp_state { + u64 gpa; + u8 assigned; + u8 pagesize; + u8 immutable; + u8 rsvd; + u32 asid; +} __packed; #endif diff --git a/arch/x86/include/asm/sev-host.h b/arch/x86/include/asm/sev-host.h index 85cfe577155c..753e80d16433 100644 --- a/arch/x86/include/asm/sev-host.h +++ b/arch/x86/include/asm/sev-host.h @@ -16,9 +16,19 @@ #ifdef CONFIG_KVM_AMD_SEV int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level); void sev_dump_rmpentry(u64 pfn); +int psmash(u64 pfn); +int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int asid, bool immutable); +int rmp_make_shared(u64 pfn, enum pg_level level); #else static inline int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) { return 0; } static inline void sev_dump_rmpentry(u64 pfn) {} +static inline int psmash(u64 pfn) { return -ENXIO; } +static inline int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int asid, + bool immutable) +{ + return -ENODEV; +} +static inline int rmp_make_shared(u64 pfn, enum pg_level level) { return -ENODEV; } #endif #endif From patchwork Mon Jun 12 04:25:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275457 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 DB8FAC7EE25 for ; Mon, 12 Jun 2023 04:30:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 78E576B0074; Mon, 12 Jun 2023 00:30:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 765448E0003; Mon, 12 Jun 2023 00:30:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 62E878E0002; Mon, 12 Jun 2023 00:30:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 5719C6B0074 for ; Mon, 12 Jun 2023 00:30:42 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 2CF7240252 for ; Mon, 12 Jun 2023 04:30:42 +0000 (UTC) X-FDA: 80892819924.20.A148A10 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2072.outbound.protection.outlook.com [40.107.95.72]) by imf06.hostedemail.com (Postfix) with ESMTP id 3DE59180005 for ; Mon, 12 Jun 2023 04:30:39 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=JoK9PkOV; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf06.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.95.72 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686544239; a=rsa-sha256; cv=pass; b=qYMh1O/rL0nsVOut8BG33NhpB7H+cjTOjrMZjWIec4bHwQ8AY1SqZU6wUTmZ8xZevRbHZo rUwDKWaW4cQREWNRFNhKK/pps6wGjnVLZrxCsZPsGQdEU3BNRGY8KLUZfY/kk3tqH93ikB Eg6it2E1zoudKqdvWIBrwtw8cd1+X4U= ARC-Authentication-Results: i=2; imf06.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=JoK9PkOV; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf06.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.95.72 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686544239; 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=7D8NSAImjaylny2njAump970VGru0JZBPQv/dh1rn8E=; b=qkJ+iNoj4PyAqawLnW7gslofCpHN/l+U8Peh4cU0u44uuK+7Rc7QzkVUhcGSfdGL55u5WJ E7B98zLAkRuDCv0LNTpKNId5ssU4pn7r7ZnO05vAYb/KMY0NDty4RbWRUi9UUN/i+Mjhbx A/EpOy+QRWkz/HF0T3wPh/4Z1yzBQzA= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UXD41z/c3O0mYE3WzOZNFDq7WyLwQv+14HXxsr02H0yiqvvDdRTUB1Mi/8sqzcZ8ju30iyGb4sDrGLm2tQ3X/lF6Z/r8vuyIYWhXMnNQHyUDdNQogWhIrKYxOT5hDwmnG3JQh9IfKr5Cy4IGUtMiutMnbvrKRJlPlOSlqcHRmAMVrVzDiNja4RJ8yLLUN4KpesAAGYCclxySXYxgbFDKOpdXT2kxaOzCATqZOGY6zTsjJ+1QeRdCxu6wyG9Y2BqSK4pAPlZ1vrCfzFKnkDUT9kdbkCRA2F9t2e8106ayZZiIYEf6YHhCj33gCwR3eOkPhRkSE7LpOZYSXSNWJY8ZKw== 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=7D8NSAImjaylny2njAump970VGru0JZBPQv/dh1rn8E=; b=ZX1hh/fhboNKRs5TqYY1dGwwusAdYsdWX5WovPrONiKJ13sS5GVEPxCzDaO2n+3x1PQ84+3A6f/1WbqHCVgIBk7v67qIB32ivv2LwuXtpb9g4cVrCIAVjwJkHipK+O2tX2YnIx9P3rM5YLdrALD/691INo+Z3fBVjNEtpSSizv2o1rVD49X3xczquSj/KO1CKlzbrhr4Jku0RL5NdD0g2iO2Hvj4oDpbA9ra+P8A3sezIrwcO+5Erp8UROThR+secv5MIe8NzyJw7JV8RDOiYVctlichb8lo9/A0pgdQSTQHOS8gTViC4R0FOu22YBOAGdycY+e2bkVXZu0kj9ZIgQ== 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 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=7D8NSAImjaylny2njAump970VGru0JZBPQv/dh1rn8E=; b=JoK9PkOVJ0xdPfgjKlQfz78T3IuWWYDntxT7O46V8Vvy3xsgJjFtlJ+07miyFT2nu7QDKTxBnS4k52DfxQghDowl1siPHzGu9/FckngwkZ+DYNFozEJuMVfDviuN1bmloX7jwp9+QDiVdRtKbFEnB50QQTXlGyKT2POxh4AByRU= Received: from DS7PR05CA0057.namprd05.prod.outlook.com (2603:10b6:8:2f::21) by BL1PR12MB5111.namprd12.prod.outlook.com (2603:10b6:208:31b::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.38; Mon, 12 Jun 2023 04:30:37 +0000 Received: from CY4PEPF0000E9D4.namprd03.prod.outlook.com (2603:10b6:8:2f:cafe::6e) by DS7PR05CA0057.outlook.office365.com (2603:10b6:8:2f::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:30:36 +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 CY4PEPF0000E9D4.mail.protection.outlook.com (10.167.241.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:30:36 +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.23; Sun, 11 Jun 2023 23:30:33 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 15/51] x86/sev: Invalidate pages from the direct map when adding them to the RMP table Date: Sun, 11 Jun 2023 23:25:23 -0500 Message-ID: <20230612042559.375660-16-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CY4PEPF0000E9D4:EE_|BL1PR12MB5111:EE_ X-MS-Office365-Filtering-Correlation-Id: 37be58db-3847-4dd0-0f9b-08db6afdc4cf X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: YVIwqOiaBzXW7N8vmS8g4n7UbapTkP9kys5RWUQR8eX2eA1zFtJ2DUr2Z0JEWW2GpI/4BtsHGyDxcunMADsodtx3ScrOtRiZxyHA4T1jzfgCptWlbuiLHhu30vfy0qdFbUCylMI808UkJ7sF5KFfNqqHJ0JS/bLpTGLIC2RgArSAI2Ozwk1mtmGgeIwya/VysDVSzgUGz8vvShV5WYEZ4PDSpJG9iux9Sh50eLX0deIe7qsJVNcth8FpHErD58gGExlMHO3VqJ1KhNLq4ahgBmYzYBPUVaHSBW3k1/0lwocYU/sYAtSbHxMdY8fu3GTf3Ri3Dq5j+CpcmTajO8jvQUduZj7D2k+DfZ2Yl3CN+UhkLHNpTQO49Gkw7vQcvsRmTZqadcgGxptFsrwBG9ZIIqpUsPe6I12x3THqVZCx9tDO3YC1JC22jTWZDZBcIW7OB1tOyz2iUiV3zOJ4N3qnZfiy+9NXKu4+VJ/f5ClEwnWc+t7dOba8ygs0xHF8CCApS9Z6xg0sMg+QJrBdEMJbcBb+r/4Bo0NuiGjRp0odrEXxrSe+9MGbmlMIMb32V/wXz5SNKlgW2nwILz++PJ2O8t5LQlkIE8Ow2M+DDjF026kR+3ZWWSQuxBllkmea55o9VdOd/a2DU0v8G3+T7/ZDm2OU1TopHnRe8A3F+iRtTF/+qlKsUdlmVLyQou5oWAfdz3j7A+1/A66afcjymifskBT29HnXBeyiLodoPAo/3i74d4QoHaadWrjCt+2LplDA7cnc9n9UgQZN4Dv5EjZlGw== 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:(13230028)(4636009)(396003)(376002)(39860400002)(346002)(136003)(451199021)(40470700004)(36840700001)(46966006)(5660300002)(7416002)(8936002)(8676002)(2906002)(70206006)(70586007)(7406005)(54906003)(44832011)(6666004)(4326008)(1076003)(26005)(316002)(6916009)(41300700001)(186003)(16526019)(36860700001)(356005)(82740400003)(336012)(426003)(47076005)(83380400001)(2616005)(40460700003)(478600001)(40480700001)(36756003)(86362001)(82310400005)(81166007)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:30:36.2668 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 37be58db-3847-4dd0-0f9b-08db6afdc4cf 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: CY4PEPF0000E9D4.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5111 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 3DE59180005 X-Stat-Signature: qamg68dpntmky51qq6dp551jpikqf3us X-HE-Tag: 1686544239-267034 X-HE-Meta: U2FsdGVkX18e54agf9f9tVhsm3eShkfVQfQQd9b32bl9vJ1AgUIv/n64QSHBM3BnXUPRTv0g0kgzfWnvNKWX1qjggVb7LOUIQXwsPMeVixtLvIY2Ddbyb05g24qYBqKEBSIfwQng9c4Au0xy28i+qaMpdKNDnsJjlzhK8mr/1HjM+HQ/iFF2ofZLxb6Sw8FVnplEWSsd9hKUH5ccGfYfEvr4BlHbOi/Atk9lW+0Ghoz591m5UjoKIZckd9NFUOx8YyaFX59V91BH0Db6FF12e/AZsLyk5qOuQxQwGjChvsdjtVUCBVAHUdLGkq79E9j01Fq2h954JAky2rKAy+gzio8jM8NZNaw53kpwMYWIkNEAaHmX6iwXSGEcGbkZSMjyoSQyGKeNkAVV7EV0R7OueSjuG7XtOzE7aJFgnPcY4v+mkoER52b72XWl7YRTfdwsX4qRNflyN5YKpe2B24pgRzyID0WNMsr4t/ZWDEOp9V82DUpRfkbjXBrZfY5yGjYUHiHg4Mq/EGFwQguM/G9KXnOUZBsP5owuOsp4c0Gn+WKlNw1XjYot4qB/D5GI3PdeJvrpgdxyCFC3THviv2wqoVdM5kpkewxzX+/xvcsGJc/qk38nKeDGOvHQROFwlD0h9gREF4wlDoBEetb3lXmEq+YQY1HK4lvfr6Cu4BhRbeQl0jp7r5cvebQOj+GbKc3OT8misK11Jk4c5m2MKmC5QLtqD/rQhBV4uapBrMRuFa62AU3FtITWArOeDzICOVazceCXvU3S/nkQwN79+rOKmPDkNeIPg5fDntm8+1PwZYpkIfi78Vkd8sz9BhZqq1oDZZtd7Kthbp2aWmxFjsfw3tz6329lTY0VtCFRRMt1EaDOSUYTIEGykah/L3Bxdu7jFIaIU9lJPwRyuqHMghRHKbco0T93SF0aCsrA9nMr6ZuuD/ZIuFKWLDFzmagkDfo8j7O30UCX66SW6e+vwIg +Ig0LFMv Zn4VkxUVDKj20GdT8uDCqfmc330cbKIChTyRqIxfqWcVjdzLJOafLN4kgaR6PMGva0Ru8wMsyDP+y5gj5FjDd1nNVvGVlokGQLBYdwDHU3TyR5z/rv/2kEczEk8iDKLX1xH/yI66pqtSkYMqnyyLCzPg7nTovE9ydpAb8ot4z8t3hJ3x++Cy0WZYA6LmirF8zHgKG5t+C7IrsARRgRgwXeZ+ACJuySmz3hfDwOQPz268c5G6Hs9tLydymkVhJmdlGOk9Dbk/DMWN5o+PMVIoq9eBqgccrQ9XDkXizDKAsJlIAk8gVyojL7AD9Gg== 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: From: Brijesh Singh The integrity guarantee of SEV-SNP is enforced through the RMP table. The RMP is used with standard x86 and IOMMU page tables to enforce memory restrictions and page access rights. The RMP check is enforced as soon as SEV-SNP is enabled globally in the system. When hardware encounters an RMP-check failure, it raises a page-fault exception. The rmp_make_private() and rmp_make_shared() helpers are used to add or remove the pages from the RMP table. Improve the rmp_make_private() to invalidate state so that pages cannot be used in the direct-map after they are added the RMP table, and restored to their default valid permission after the pages are removed from the RMP table. Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra Signed-off-by: Brijesh Singh Signed-off-by: Michael Roth --- arch/x86/coco/sev/host.c | 62 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/arch/x86/coco/sev/host.c b/arch/x86/coco/sev/host.c index 9df690b0b263..cd3b4c6a25bc 100644 --- a/arch/x86/coco/sev/host.c +++ b/arch/x86/coco/sev/host.c @@ -366,6 +366,42 @@ int psmash(u64 pfn) } EXPORT_SYMBOL_GPL(psmash); +static int restore_direct_map(u64 pfn, int npages) +{ + int i, ret = 0; + + for (i = 0; i < npages; i++) { + ret = set_direct_map_default_noflush(pfn_to_page(pfn + i)); + if (ret) + break; + } + + if (ret) + pr_warn("Failed to restore direct map for pfn 0x%llx, ret: %d\n", + pfn + i, ret); + + return ret; +} + +static int invalidate_direct_map(u64 pfn, int npages) +{ + int i, ret = 0; + + for (i = 0; i < npages; i++) { + ret = set_direct_map_invalid_noflush(pfn_to_page(pfn + i)); + if (ret) + break; + } + + if (ret) { + pr_warn("Failed to invalidate direct map for pfn 0x%llx, ret: %d\n", + pfn + i, ret); + restore_direct_map(pfn, i); + } + + return ret; +} + static int rmpupdate(u64 pfn, struct rmp_state *val) { unsigned long paddr = pfn << PAGE_SHIFT; @@ -375,6 +411,21 @@ static int rmpupdate(u64 pfn, struct rmp_state *val) if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) return -ENXIO; + level = RMP_TO_X86_PG_LEVEL(val->pagesize); + npages = page_level_size(level) / PAGE_SIZE; + + /* + * If page is getting assigned in the RMP table then unmap it from the + * direct map. + */ + if (val->assigned) { + if (invalidate_direct_map(pfn, npages)) { + pr_err("Failed to unmap %d pages at pfn 0x%llx from the direct_map\n", + npages, pfn); + return -EFAULT; + } + } + do { /* Binutils version 2.36 supports the RMPUPDATE mnemonic. */ asm volatile(".byte 0xF2, 0x0F, 0x01, 0xFE" @@ -393,6 +444,17 @@ static int rmpupdate(u64 pfn, struct rmp_state *val) return -EFAULT; } + /* + * Restore the direct map after the page is removed from the RMP table. + */ + if (!val->assigned) { + if (restore_direct_map(pfn, npages)) { + pr_err("Failed to map %d pages at pfn 0x%llx into the direct_map\n", + npages, pfn); + return -EFAULT; + } + } + return 0; } From patchwork Mon Jun 12 04:25:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275474 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 6B792C7EE23 for ; Mon, 12 Jun 2023 04:31:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0B1368E0002; Mon, 12 Jun 2023 00:31:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 061B26B0078; Mon, 12 Jun 2023 00:31:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E1CF38E0002; Mon, 12 Jun 2023 00:31:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id D3D286B0075 for ; Mon, 12 Jun 2023 00:31:10 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id AD2F21A027E for ; Mon, 12 Jun 2023 04:31:10 +0000 (UTC) X-FDA: 80892821100.04.7C97755 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2056.outbound.protection.outlook.com [40.107.101.56]) by imf04.hostedemail.com (Postfix) with ESMTP id 951C540014 for ; Mon, 12 Jun 2023 04:31:07 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="lH/2OwST"; spf=pass (imf04.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.101.56 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=1686544267; a=rsa-sha256; cv=pass; b=1xn4Tn7U5WuFagFuzKdpclcseT6VJwM1rC+uMIkCkU9IU4zQ7lLW/RIMwUf9ZNhnXlen7V aXPJdANWg0+mTZkkjpT/hCU/QlXoe8F2qDnh/nv6qBvKS7QdQI2MTJbWiy+DpWZY3Jz92s yrIC0DY+ZZMfOFXnBNbsUUdKV9jCrV4= ARC-Authentication-Results: i=2; imf04.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="lH/2OwST"; spf=pass (imf04.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.101.56 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=1686544267; 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=2Zsc6mV3GrvXD9w3AgjvVW3vHnvwWqzD9ghwmSVayr4=; b=X3pqPhinsmDAwZmPNcq3pfke0Kv4pBDIh2ZkCDJ4Hj/m2brFg4mpkbdu+0RXZyXAeQ8+Pt e11Z61e5HZPAmqcAAt7HDVyfIKKKOPyfuLPYmor7kKa41eUMHcvYMkge2I5HwJ/JLxxiTY qQSzcIus7xc/PUBpVo32Iaxx/WqSQzY= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=H967fgcnWDfI8f1DAABvenBz5Yfdm+n/GDCDu2fm7tZhV8d+STOHSLDbPsOlnZOo4nOwYX3fBeT5CePouZ9ggI9hPSnQ2LxTcOEk6+x61mKiIK9QCtgAo5+3SnFVWRuYGgIPch0UVfXwRk1tIgQKADy2/qKQHz3pJQ815M/TFMA2cqJcvIu0uW4RJWjZA8qqiXLXvtt59sBUpZUQBABn3M+JdVac3OZZqHhQDVuTWeeRcdz9sV9VgWUHsdbl+IgVr9r2ahG+zbdsmQYyMUnFnXmhPb8mzEUOy28lgB7H/wGZnYgIFJ/fqhiPAOm5MMBCbGGHIEvm6ePEURsQ37ZiaQ== 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=2Zsc6mV3GrvXD9w3AgjvVW3vHnvwWqzD9ghwmSVayr4=; b=grKXx+OOh7xGgaYfkGq6vChAsHOg/KH2r6OTTxZTfMymU8XggV+fclKCqe3o8+VDAZwEeT6rdXq6a1DI6lWA1SoNZNz0ZyZrj695q2Miox9Gy3h3aIsls7Zb/7K5j6hduJTg9S2SnyRqVnDG89CuUqNHnqAd1h5G/Y4beSSbjNghD6kE2BJ74yUIEXLM5MxO+/sKBfQfdM9w4mrVzjlpKdL1uJCs4An8iEVDY8/d/U/peTpHKwpUKNZo5yv0zrr2Yas1Cm/fqGwnCEfjo2vgbkYB28QRRvb8Qg8hMBUEhk31QLL9ca4Xf0dVP1qgWsC95I83/8oYy43cu6woBvuwYA== 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 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=2Zsc6mV3GrvXD9w3AgjvVW3vHnvwWqzD9ghwmSVayr4=; b=lH/2OwSTOzmy7Jxd1ZRgMVF+OTA0tXuCn+c1Xiy451c4BCuUi0Vg3vKs8GqjgH/F1kL/vK8Q3sy62x7m6Lo9uYT6cScKN87qawWgunHKbLChwqX1LhTQQmwZIMcLh1d79ml/imtNT+pAcjTBtGjPJYddn7cqgJLOG7eqcJw5krc= Received: from CY5PR19CA0115.namprd19.prod.outlook.com (2603:10b6:930:64::23) by CH0PR12MB8532.namprd12.prod.outlook.com (2603:10b6:610:191::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.29; Mon, 12 Jun 2023 04:31:04 +0000 Received: from CY4PEPF0000E9D1.namprd03.prod.outlook.com (2603:10b6:930:64:cafe::e5) by CY5PR19CA0115.outlook.office365.com (2603:10b6:930:64::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:31:03 +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 CY4PEPF0000E9D1.mail.protection.outlook.com (10.167.241.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:31:03 +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.23; Sun, 11 Jun 2023 23:31:01 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 16/51] crypto: ccp: Define the SEV-SNP commands Date: Sun, 11 Jun 2023 23:25:24 -0500 Message-ID: <20230612042559.375660-17-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CY4PEPF0000E9D1:EE_|CH0PR12MB8532:EE_ X-MS-Office365-Filtering-Correlation-Id: bdb54a42-0649-427a-20d3-08db6afdd516 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2qUrvsZ7WQdvcw7jNRRQ1QvZlFnsVj0Ngig1WJFmkuxmkrMzaBfAjhF+qmuBoiXOAwXExGsx/+142TFDJMY1fYB/ai3oiAeTz+T+jT+4uVIQQsvOR/J/ujKZ7nSiursecQFIibzPk5pmFRLYpJZsRBChA0I4K+I8FCuePNxOUtQyvlPVekN0tuttkWGD/F8lPNvBj62iQgu9hloH2VUa1gX0WQA3gRYir/dd35yxkOZ3B31uufMSs69+CWMO1dZ2kthKUEC4DRoWrX53kNc4uTDk+Ed6LdyVZlw/oxT9eAuxx56zgI2Qr9Z+NmxYgF/UcZbXDrbSKUD4chsIcwV/qDuWaev9dTiGfOllD71grNZjFtpPfk6gSzsQbehFkXeZZIYtkpq/p1JfqeUr9+mdhU4mAi6MYtRvvr6aiELP4wYBXmBx1l621WGgcaMZLtTzIw/jaPBOPttf9aScQB5pKIw5DtNfEjAosuQNdWjBAkc2qIws6XlqbPrfEe2WwxGCo+SjosmIzRAjotsaP5FKsOl3fZ5Yz28sqDHtzB6u1hTdOYrCQxwgfEbiXtsMVMLk7DT1eJB7vM96wabW3UtiaqlAp6mfijoKr2XAMKlr6SosQtPojauJyeQBMV0EUfApz6kmZ0RJHoP4U1CzzggvKIgRz6kssze0a2iP1x2lf/tICvrYAdvZ5hIiGtVOVCS06emKAbbrIhQdrDrIpKuaZXikdi3te/N51qTZZxDwuykUElLMvXyaV2KJN9DZ5p6jcRX4/AwFBLKdbE+WjMjLAw== 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:(13230028)(4636009)(39860400002)(136003)(396003)(376002)(346002)(451199021)(36840700001)(46966006)(40470700004)(336012)(2616005)(47076005)(83380400001)(426003)(36860700001)(82740400003)(356005)(40480700001)(86362001)(82310400005)(81166007)(36756003)(40460700003)(478600001)(44832011)(54906003)(4326008)(6666004)(84970400001)(8936002)(8676002)(30864003)(2906002)(5660300002)(7416002)(7406005)(70206006)(70586007)(6916009)(41300700001)(316002)(16526019)(186003)(1076003)(26005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:31:03.6074 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: bdb54a42-0649-427a-20d3-08db6afdd516 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: CY4PEPF0000E9D1.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB8532 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 951C540014 X-Stat-Signature: iigem5odmu46wwktebgntqug8dpwj5qw X-Rspam-User: X-HE-Tag: 1686544267-335332 X-HE-Meta: U2FsdGVkX1/MfxEzKQAE9unhVbz7jZ8dyfV1GmSvYmcKXTPuLVr/anWrOQnT5XJJI93RniuaFzyjE7JM4AceaznHvOUQoUKx8+kINQkeBC4Fet+u2pIxw9hqg3cl1gdQsSiyf9p1mEe/UHxTiUF/ekaFODjeEWo43sOFKzGCAJb8s/5MCFTrjBjei5dsYRwxOL0D1cF5ZpRJbRsb8Kqo+WP1Hafb4UA4Yd/khPgCWIwlJzfBcPWsF/qiy9kNHfECfbMx0S+shoKyon0oVjcAwcRWOy2Vj79pBxNTguFGd3A5dNAC1TRyVaCUOZJHXlccPjig90xRyORWmQ97ODjYHWBRO3ntTUZzsuZJZO8vUgj9AsSMChf0u+zaaW7gSWrsBF0LgTkGOAubqmF1xvhuPQlH2R6b9lClKDNrRCKUU8mThdZPbiR40tPufRsvvxufJnCVBHv0qAy6y6rs/fQlpLhv0HoN/KfCFHPkh0un3B48f7p/SlcmEAswfzArJf6YRjh68qTGxkD5Wp094TnnaZqylJYTvgHd2L26ogkEVugL/NM8zYgHQIZtNSnreafsdfZWeNcHLxq4vDYBP+PpDoOoVhtkIo/XABQX26Y4JYkYUiOmny3xYnslaGMXqho9YFVQ3rODbbWObTVvXN36b5eI6L5Zm4kZ49/t5uNlBwY9ZmAYeitvMgp4km53N4k1t4kKdL+l8NJxs1Er0RVhm5X2oa4Q+lpwZgxWIZbG8g15VcW9m/ZA7ZQuKmCf4IdrQGyBldgip9Ufp7fNjL3Nr9OzXaipsxZxoeYtJ7GDJSmEmPXMMk0EFyM/4MbaJwQFiHQmyY8FVGrh2r0XEsX7fuXEQy5J+k0J1NrRQ+Qx1frFYQZxb+J/VLjTBrt3btcD/aehLR05LMydMXWFn/TRrlxWZj1r3NOamcebzGvTe14xjifQc+jbUCW1RUF+FO/c+IuQded+tJ2DHeeXd9w 85eKeHWA o0jBQjSmoE+SEmTdt3YyDac047ApWblm227X3+gEAhjuGpsK9e1/+Q2ncU3nGswChqr0G+G7rqL+EOSbkwn2W5kGDV/zkDtD2x04Buth5r9Tlsgjq2lVAvDBlgAi8PCNnMuwQ0UI2YhWHhW86iG9I/rylR4lNheyOeIOQ2zXlddVkv0idwxvMh83HuNmlvFzStkYwnn79ikoE179//IFpX7H5xNfI1uycu8eUlg76FvSKYhSdE/kgULaJPc6zv4M9sjcbGLJg5Dd5/AErkiVcVGIckpLoEnJ05lwWzvCKwvPxjNZ3MdBTvJaCeHTw8QSPbkARkArCwANybUcSlNcdm7bjuRluc3gck0xZToV4nxGCtFFX4msch8i5tX/dFaxKaPnoINzR83p5dGU= 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: From: Brijesh Singh AMD introduced the next generation of SEV called SEV-SNP (Secure Nested Paging). SEV-SNP builds upon existing SEV and SEV-ES functionality while adding new hardware security protection. Define the commands and structures used to communicate with the AMD-SP when creating and managing the SEV-SNP guests. The SEV-SNP firmware spec is available at developer.amd.com/sev. Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra Signed-off-by: Brijesh Singh [mdr: update SNP command list and SNP status struct based on current spec] Signed-off-by: Michael Roth --- drivers/crypto/ccp/sev-dev.c | 16 +++ include/linux/psp-sev.h | 246 +++++++++++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 53 ++++++++ 3 files changed, 315 insertions(+) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index e2f25926eb51..ab3572286755 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -128,6 +128,8 @@ static int sev_cmd_buffer_len(int cmd) switch (cmd) { case SEV_CMD_INIT: return sizeof(struct sev_data_init); case SEV_CMD_INIT_EX: return sizeof(struct sev_data_init_ex); + case SEV_CMD_SNP_SHUTDOWN_EX: return sizeof(struct sev_data_snp_shutdown_ex); + case SEV_CMD_SNP_INIT_EX: return sizeof(struct sev_data_snp_init_ex); case SEV_CMD_PLATFORM_STATUS: return sizeof(struct sev_user_data_status); case SEV_CMD_PEK_CSR: return sizeof(struct sev_data_pek_csr); case SEV_CMD_PEK_CERT_IMPORT: return sizeof(struct sev_data_pek_cert_import); @@ -156,6 +158,20 @@ static int sev_cmd_buffer_len(int cmd) case SEV_CMD_GET_ID: return sizeof(struct sev_data_get_id); case SEV_CMD_ATTESTATION_REPORT: return sizeof(struct sev_data_attestation_report); case SEV_CMD_SEND_CANCEL: return sizeof(struct sev_data_send_cancel); + case SEV_CMD_SNP_GCTX_CREATE: return sizeof(struct sev_data_snp_addr); + case SEV_CMD_SNP_LAUNCH_START: return sizeof(struct sev_data_snp_launch_start); + case SEV_CMD_SNP_LAUNCH_UPDATE: return sizeof(struct sev_data_snp_launch_update); + case SEV_CMD_SNP_ACTIVATE: return sizeof(struct sev_data_snp_activate); + case SEV_CMD_SNP_DECOMMISSION: return sizeof(struct sev_data_snp_addr); + case SEV_CMD_SNP_PAGE_RECLAIM: return sizeof(struct sev_data_snp_page_reclaim); + case SEV_CMD_SNP_GUEST_STATUS: return sizeof(struct sev_data_snp_guest_status); + case SEV_CMD_SNP_LAUNCH_FINISH: return sizeof(struct sev_data_snp_launch_finish); + case SEV_CMD_SNP_DBG_DECRYPT: return sizeof(struct sev_data_snp_dbg); + case SEV_CMD_SNP_DBG_ENCRYPT: return sizeof(struct sev_data_snp_dbg); + case SEV_CMD_SNP_PAGE_UNSMASH: return sizeof(struct sev_data_snp_page_unsmash); + case SEV_CMD_SNP_PLATFORM_STATUS: return sizeof(struct sev_data_snp_addr); + case SEV_CMD_SNP_GUEST_REQUEST: return sizeof(struct sev_data_snp_guest_request); + case SEV_CMD_SNP_CONFIG: return sizeof(struct sev_user_data_snp_config); default: return 0; } diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 1595088c428b..06d0619ca442 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -86,6 +86,36 @@ enum sev_cmd { SEV_CMD_DBG_DECRYPT = 0x060, SEV_CMD_DBG_ENCRYPT = 0x061, + /* SNP specific commands */ + SEV_CMD_SNP_INIT = 0x81, + SEV_CMD_SNP_SHUTDOWN = 0x82, + SEV_CMD_SNP_PLATFORM_STATUS = 0x83, + SEV_CMD_SNP_DF_FLUSH = 0x84, + SEV_CMD_SNP_INIT_EX = 0x85, + SEV_CMD_SNP_SHUTDOWN_EX = 0x86, + SEV_CMD_SNP_DECOMMISSION = 0x90, + SEV_CMD_SNP_ACTIVATE = 0x91, + SEV_CMD_SNP_GUEST_STATUS = 0x92, + SEV_CMD_SNP_GCTX_CREATE = 0x93, + SEV_CMD_SNP_GUEST_REQUEST = 0x94, + SEV_CMD_SNP_ACTIVATE_EX = 0x95, + SEV_CMD_SNP_LAUNCH_START = 0xA0, + SEV_CMD_SNP_LAUNCH_UPDATE = 0xA1, + SEV_CMD_SNP_LAUNCH_FINISH = 0xA2, + SEV_CMD_SNP_DBG_DECRYPT = 0xB0, + SEV_CMD_SNP_DBG_ENCRYPT = 0xB1, + SEV_CMD_SNP_PAGE_SWAP_OUT = 0xC0, + SEV_CMD_SNP_PAGE_SWAP_IN = 0xC1, + SEV_CMD_SNP_PAGE_MOVE = 0xC2, + SEV_CMD_SNP_PAGE_MD_INIT = 0xC3, + SEV_CMD_SNP_PAGE_SET_STATE = 0xC6, + SEV_CMD_SNP_PAGE_RECLAIM = 0xC7, + SEV_CMD_SNP_PAGE_UNSMASH = 0xC8, + SEV_CMD_SNP_CONFIG = 0xC9, + SEV_CMD_SNP_DOWNLOAD_FIRMWARE_EX = 0xCA, + SEV_CMD_SNP_COMMIT = 0xCB, + SEV_CMD_SNP_VLEK_LOAD = 0xCD, + SEV_CMD_MAX, }; @@ -531,6 +561,222 @@ struct sev_data_attestation_report { u32 len; /* In/Out */ } __packed; +/** + * struct sev_data_snp_download_firmware - SNP_DOWNLOAD_FIRMWARE command params + * + * @address: physical address of firmware image + * @len: len of the firmware image + */ +struct sev_data_snp_download_firmware { + u64 address; /* In */ + u32 len; /* In */ +} __packed; + +/** + * struct sev_data_snp_activate - SNP_ACTIVATE command params + * + * @gctx_paddr: system physical address guest context page + * @asid: ASID to bind to the guest + */ +struct sev_data_snp_activate { + u64 gctx_paddr; /* In */ + u32 asid; /* In */ +} __packed; + +/** + * struct sev_data_snp_addr - generic SNP command params + * + * @address: system physical address guest context page + */ +struct sev_data_snp_addr { + u64 gctx_paddr; /* In */ +} __packed; + +/** + * struct sev_data_snp_launch_start - SNP_LAUNCH_START command params + * + * @gctx_addr: system physical address of guest context page + * @policy: guest policy + * @ma_gctx_addr: system physical address of migration agent + * @imi_en: launch flow is launching an IMI for the purpose of + * guest-assisted migration. + * @ma_en: the guest is associated with a migration agent + */ +struct sev_data_snp_launch_start { + u64 gctx_paddr; /* In */ + u64 policy; /* In */ + u64 ma_gctx_paddr; /* In */ + u32 ma_en:1; /* In */ + u32 imi_en:1; /* In */ + u32 rsvd:30; + u8 gosvw[16]; /* In */ +} __packed; + +/* SNP support page type */ +enum { + SNP_PAGE_TYPE_NORMAL = 0x1, + SNP_PAGE_TYPE_VMSA = 0x2, + SNP_PAGE_TYPE_ZERO = 0x3, + SNP_PAGE_TYPE_UNMEASURED = 0x4, + SNP_PAGE_TYPE_SECRET = 0x5, + SNP_PAGE_TYPE_CPUID = 0x6, + + SNP_PAGE_TYPE_MAX +}; + +/** + * struct sev_data_snp_launch_update - SNP_LAUNCH_UPDATE command params + * + * @gctx_addr: system physical address of guest context page + * @imi_page: indicates that this page is part of the IMI of the guest + * @page_type: encoded page type + * @page_size: page size 0 indicates 4K and 1 indicates 2MB page + * @address: system physical address of destination page to encrypt + * @vmpl1_perms: VMPL permission mask for VMPL1 + * @vmpl2_perms: VMPL permission mask for VMPL2 + * @vmpl3_perms: VMPL permission mask for VMPL3 + */ +struct sev_data_snp_launch_update { + u64 gctx_paddr; /* In */ + u32 page_size:1; /* In */ + u32 page_type:3; /* In */ + u32 imi_page:1; /* In */ + u32 rsvd:27; + u32 rsvd2; + u64 address; /* In */ + u32 rsvd3:8; + u32 vmpl1_perms:8; /* In */ + u32 vmpl2_perms:8; /* In */ + u32 vmpl3_perms:8; /* In */ + u32 rsvd4; +} __packed; + +/** + * struct sev_data_snp_launch_finish - SNP_LAUNCH_FINISH command params + * + * @gctx_addr: system physical address of guest context page + */ +struct sev_data_snp_launch_finish { + u64 gctx_paddr; + u64 id_block_paddr; + u64 id_auth_paddr; + u8 id_block_en:1; + u8 auth_key_en:1; + u64 rsvd:62; + u8 host_data[32]; +} __packed; + +/** + * struct sev_data_snp_guest_status - SNP_GUEST_STATUS command params + * + * @gctx_paddr: system physical address of guest context page + * @address: system physical address of guest status page + */ +struct sev_data_snp_guest_status { + u64 gctx_paddr; + u64 address; +} __packed; + +/** + * struct sev_data_snp_page_reclaim - SNP_PAGE_RECLAIM command params + * + * @paddr: system physical address of page to be claimed. The 0th bit + * in the address indicates the page size. 0h indicates 4 kB and + * 1h indicates 2 MB page. + */ +struct sev_data_snp_page_reclaim { + u64 paddr; +} __packed; + +/** + * struct sev_data_snp_page_unsmash - SNP_PAGE_UNSMASH command params + * + * @paddr: system physical address of page to be unsmashed. The 0th bit + * in the address indicates the page size. 0h indicates 4 kB and + * 1h indicates 2 MB page. + */ +struct sev_data_snp_page_unsmash { + u64 paddr; +} __packed; + +/** + * struct sev_data_dbg - DBG_ENCRYPT/DBG_DECRYPT command parameters + * + * @handle: handle of the VM to perform debug operation + * @src_addr: source address of data to operate on + * @dst_addr: destination address of data to operate on + */ +struct sev_data_snp_dbg { + u64 gctx_paddr; /* In */ + u64 src_addr; /* In */ + u64 dst_addr; /* In */ +} __packed; + +/** + * struct sev_snp_guest_request - SNP_GUEST_REQUEST command params + * + * @gctx_paddr: system physical address of guest context page + * @req_paddr: system physical address of request page + * @res_paddr: system physical address of response page + */ +struct sev_data_snp_guest_request { + u64 gctx_paddr; /* In */ + u64 req_paddr; /* In */ + u64 res_paddr; /* In */ +} __packed; + +/** + * struct sev_data_snp_init - SNP_INIT_EX structure + * + * @init_rmp: indicate that the RMP should be initialized. + * @list_paddr_en: indicate that list_paddr is valid + * @list_paddr: system physical address of range list + */ +struct sev_data_snp_init_ex { + u32 init_rmp:1; + u32 list_paddr_en:1; + u32 rsvd:30; + u32 rsvd1; + u64 list_paddr; + u8 rsvd2[48]; +} __packed; + +/** + * struct sev_data_range - RANGE structure + * + * @base: system physical address of first byte of range + * @page_count: number of 4KB pages in this range + */ +struct sev_data_range { + u64 base; + u32 page_count; + u32 rsvd; +} __packed; + +/** + * struct sev_data_range_list - RANGE_LIST structure + * + * @num_elements: number of elements in RANGE_ARRAY + * @ranges: array of num_elements of type RANGE + */ +struct sev_data_range_list { + u32 num_elements; + u32 rsvd; + struct sev_data_range ranges[0]; +} __packed; + +/** + * struct sev_data_snp_shutdown_ex - SNP_SHUTDOWN_EX structure + * + * @length: len of the command buffer read by the PSP + * @iommu_snp_shutdown: Disable enforcement of SNP in the IOMMU + */ +struct sev_data_snp_shutdown_ex { + u32 length; + u32 iommu_snp_shutdown:1; + u32 rsvd1:31; +} __packed; + #ifdef CONFIG_CRYPTO_DEV_SP_PSP /** diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h index 91b4c63d5cbf..7d8a2dd20273 100644 --- a/include/uapi/linux/psp-sev.h +++ b/include/uapi/linux/psp-sev.h @@ -61,6 +61,13 @@ typedef enum { SEV_RET_INVALID_PARAM, SEV_RET_RESOURCE_LIMIT, SEV_RET_SECURE_DATA_INVALID, + SEV_RET_INVALID_PAGE_SIZE, + SEV_RET_INVALID_PAGE_STATE, + SEV_RET_INVALID_MDATA_ENTRY, + SEV_RET_INVALID_PAGE_OWNER, + SEV_RET_INVALID_PAGE_AEAD_OFLOW, + SEV_RET_RMP_INIT_REQUIRED, + SEV_RET_MAX, } sev_ret_code; @@ -147,6 +154,52 @@ struct sev_user_data_get_id2 { __u32 length; /* In/Out */ } __packed; +/** + * struct sev_user_data_snp_status - SNP status + * + * @major: API major version + * @minor: API minor version + * @state: current platform state + * @is_rmp_initialized: whether RMP is initialized or not + * @build: firmware build id for the API version + * @mask_chip_id: whether chip id is present in attestation reports or not + * @mask_chip_key: whether attestation reports are signed or not + * @vlek_en: VLEK hashstick is loaded + * @guest_count: the number of guest currently managed by the firmware + * @current_tcb_version: current TCB version + * @reported_tcb_version: reported TCB version + */ +struct sev_user_data_snp_status { + __u8 api_major; /* Out */ + __u8 api_minor; /* Out */ + __u8 state; /* Out */ + __u8 is_rmp_initialized:1; /* Out */ + __u8 rsvd:7; + __u32 build_id; /* Out */ + __u32 mask_chip_id:1; /* Out */ + __u32 mask_chip_key:1; /* Out */ + __u32 vlek_en:1; /* Out */ + __u32 rsvd1:29; + __u32 guest_count; /* Out */ + __u64 current_tcb_version; /* Out */ + __u64 reported_tcb_version; /* Out */ +} __packed; + +/* + * struct sev_user_data_snp_config - system wide configuration value for SNP. + * + * @reported_tcb: The TCB version to report in the guest attestation report. + * @mask_chip_id: Indicates that the CHID_ID field in the attestation report + * will always be zero. + */ +struct sev_user_data_snp_config { + __u64 reported_tcb ; /* In */ + __u32 mask_chip_id:1; /* In */ + __u32 mask_chip_key:1; /* In */ + __u32 rsvd:30; /* In */ + __u8 rsvd1[52]; +} __packed; + /** * struct sev_issue_cmd - SEV ioctl parameters * From patchwork Mon Jun 12 04:25:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275475 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 4908BC7EE25 for ; Mon, 12 Jun 2023 04:31:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DD04B6B0072; Mon, 12 Jun 2023 00:31:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D7F858E0005; Mon, 12 Jun 2023 00:31:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BD3D38E0003; Mon, 12 Jun 2023 00:31:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id AA9206B0072 for ; Mon, 12 Jun 2023 00:31:33 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 86ACA160268 for ; Mon, 12 Jun 2023 04:31:33 +0000 (UTC) X-FDA: 80892822066.12.C1B3329 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by imf21.hostedemail.com (Postfix) with ESMTP id 894F31C0004 for ; Mon, 12 Jun 2023 04:31:30 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=V4IjMOKt; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf21.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.41 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686544290; 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=1xj2/U+IIWPesaqCOErKg1ZrLHLEuUpPUlqimBPs3Vo=; b=VKWIkcyuZ1UGG5V1LUmCBR5WMtKrE+iHImo6MN7DFVraRcxaUDzlN4TskkS8G+u6f5DFY+ tSxsVrML9KRsbfI5ed/b06CevTNXRbE4/BxQ8pQSmoJR5EfQWMV8Nl+ftkDYGK5obq2V0h hqGWDGlFIk8zPU9SerLuDUi4uNsbSDE= ARC-Authentication-Results: i=2; imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=V4IjMOKt; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf21.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.41 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686544290; a=rsa-sha256; cv=pass; b=aBumTNQ05wtU4luPNr+l4DU8++cvAJt2Uenfe1O7yqHqqlFB7CNyBaOKSkvKF/ey2/FSCQ od4FF7C5BzYpf9GiZOU4zHo+8Xw2n4XkLAmGCHAoVtX1jBliXcPq5qytD9TZSeVYd1mj/U PMfbAbFPAUL5ZUzegTPG2iuG8SmLpL0= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nEW2lAsy/SpMy6HClYRfLE2cqoZPjqGDJiuyY33JssL9+CxuooFyehaieyb/cNnZXL+CiTOg8MjfyCa36qdxonKdFqbFPG2VtpB6nRuSwvIPdoCwhpgdjBYFAUGBmWK7EIUCh0ALTIPU1DaYeKqfvnFkQDHXejDwrEQXTgdbu/KARLHQR53s76PlYL5k2BjIOOuj6McmL7gn/YlS/zWiLJxcAGj7kDSFmksDVaqW7ajf2AhD4IxrRWe5oiBTdw7xkONRlbdbxErhR/jLQ8+AIWbNAWCnuDwU9ihx+zldn/IyI5iSryxFsW9tkO3GOI1d8LjrBDE4wQOPj51f4uTPNg== 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=1xj2/U+IIWPesaqCOErKg1ZrLHLEuUpPUlqimBPs3Vo=; b=JC5u+MdZxXL0SB5ek0dLbNYTVMFj98SRdaMWw6glDZTHWlsi7w5sp6Akj4BjvBBGTcq+sp85JgjHN8uqKTHxByg/eOBaMuWuc4YP2bB+04ZdvZF9ovVwRBFOiIOkz4iaU9NPGCLDGmM9lLMj/+7wyvHOitRjs3NGZHYEufJqkyczMTHA8mET11ZnQkFuNjh7SaYAMM4DkVCf0EpZ1xOr7rcSOd799/+1SjaYAeYmEW4lPG+0vWZ6ii4iRzNYzfFLHbd/ds8QGt6waenozc3gdKuDGQk2MyuekHEKAsp8AC2EfuY4vIPlq5oC2pUDQFOEvIGpJ4f+Scz8oz9UKHEJVQ== 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 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=1xj2/U+IIWPesaqCOErKg1ZrLHLEuUpPUlqimBPs3Vo=; b=V4IjMOKt1RDBfKmtcF22eBSmW2Spcnbl7rXGOH2QzHOganmsgS8eiScXp2Owd+tW6mhbt6ON+e2InIhwdUKKcQEW/oaIh7EPjbRPstqfXnC2vcBRlrnaOezushkkIMQyJ8NQlVt/3Gmn9YXFrNk3zDDG99GLb0mi8vgnXIJYRL4= Received: from DS7PR05CA0041.namprd05.prod.outlook.com (2603:10b6:8:2f::8) by CH0PR12MB5073.namprd12.prod.outlook.com (2603:10b6:610:e0::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.44; Mon, 12 Jun 2023 04:31:24 +0000 Received: from CY4PEPF0000E9D4.namprd03.prod.outlook.com (2603:10b6:8:2f:cafe::bf) by DS7PR05CA0041.outlook.office365.com (2603:10b6:8:2f::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:31:24 +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 CY4PEPF0000E9D4.mail.protection.outlook.com (10.167.241.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:31:24 +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.23; Sun, 11 Jun 2023 23:31:23 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Jarkko Sakkinen Subject: [PATCH RFC v9 17/51] crypto: ccp: Add support to initialize the AMD-SP for SEV-SNP Date: Sun, 11 Jun 2023 23:25:25 -0500 Message-ID: <20230612042559.375660-18-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CY4PEPF0000E9D4:EE_|CH0PR12MB5073:EE_ X-MS-Office365-Filtering-Correlation-Id: 66d9803f-3d76-49bb-09ad-08db6afde17c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: WUux1d9jyn/bXMRejASeMzmuK+MHo1xI208XauE+psHBZrlF3PntuUmMpKzL9q9MRJuz/yMI9MGijA0JW3cT5JAN9fdtJqjhlaOHVUnlPA9o+xQSB1ZjXGaDgQuk2W6Lpzkzg8GOJvprCCj+6DVdTehVcxxNWrAJ2QMIhUEGu/wTP3YBykBhrYLjBz//dXiVvQeewBQcI5i4bydIacOPX/k+HEuQXVBqiKxeEAN0b8jl1o2NNC07tCOuyVuf8TD7BsdYJnZAvAajwsU7k45QFpHj38dLvLyvx4voQ9z4uatT3uLCHEG/hIljCKTTaNidPA2WdmLef7Of/izenQcSb0D5FgLnpSsRo3JwO8wGKIo4xX4PB8eTjf32qTZ4GoiILXgZcDvXTO90ggAMPSfhGDmvTAviCmMhchDRJkc0fMnIe5D+hzqcrCN4YJlfexpqFNf4MJDpkDVsn0qLwQAT/Oe4EaCMuf1Ak/Z5uoYORricZuGOZUQs5+U2ByIwgPbi6H+rq5KrorKhp9D+B26iDBMXmuj8fIy2WqgQUmKcXc8trJScC5rzE6CdfGFjUbPrwEEj4nyw0su/JjEKdGbdA6subuBD9oKOkQ2BxLcYKhjScP04bZkeYaTFveYIazjZuecGF7VsbPtoesAoF330n1k0sZDR/zuGiGU9PAinDJCgiOH9KpckzDe0iRRmJ7BnLBefmjTd8GUDcr1KybBwEtLQiUbtojdJKWrCKMZJAOTj/Efp2cYruiwilCtSxRtaZbYgrnTHdDhbmXvFB/KFsQ== 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:(13230028)(4636009)(136003)(346002)(376002)(396003)(39860400002)(451199021)(40470700004)(36840700001)(46966006)(86362001)(82310400005)(7406005)(7416002)(40460700003)(316002)(8676002)(41300700001)(82740400003)(83380400001)(5660300002)(26005)(40480700001)(81166007)(1076003)(356005)(36860700001)(8936002)(44832011)(36756003)(336012)(4326008)(426003)(6916009)(70586007)(47076005)(70206006)(478600001)(186003)(16526019)(30864003)(2906002)(54906003)(2616005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:31:24.3923 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 66d9803f-3d76-49bb-09ad-08db6afde17c 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: CY4PEPF0000E9D4.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5073 X-Rspamd-Queue-Id: 894F31C0004 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 4s5bhp49cs8dorejdu3k8d455fex67xs X-HE-Tag: 1686544290-107771 X-HE-Meta: U2FsdGVkX190eU0b23VMjX1kQwgQk/zig7Bo1v20YNSQCwKpxF619MmurMLtj8nmLx14UR97hiDfpnCXBmZXQ5S0V2BYAIsdPaHezPcDYi0jp84rLPracNWTK9XIjhluZ7wN/6NcnhAgbCoLD0F/FfSVtcfgjAZTNPxW2OuK6fTgWQIBPcfIZ7DtzxjCJam8TktzqfPCYnKu96mKywS3pwYffm85j+7lN38qMI2BnbtqKaTUMxoWmvlFdbX5Huj6GtWPWDSEzAov38yGJ4n+vvH/QYoMQ6HMWML/Bly9TbonCM+q8Sg8boXai2cWRfq8W715Fds6XK/OIlKb2dOCVAjWBC8FBGdJNPrSQt4LBHWv0Uku1d1/qHS7qwI+zo3MPsHrcatMLDee12304IThvZRoVc45SAcvXJSOsRPPISx0rDRQEua314fKVjnzfKhycDO7J4tPvbqJ0wdsR/2WN3xpaEvW3i/OVeS7jbcGNomsLDFPnDJJlBA+6fnv+25KC90aIO12LkxqT2nhzU+x9j4hwyv+rP8c1Zhcbhml1RodNIzJZWy+hIpA/FyshHdov5wsvv6SJB+hMjljLavSz62BCw4hDrOOHcbYBueF6Gu6IB3BOAYj1PZUiKU0ZMYy2twDAvs35c0xiNDU8UQgtIswm7G3yvxtWdLlEVaIScmxlwEq9PMDaZWg12U8AWWRBJtWcUEErzmqsObUzolB+HRwD3oUO/2KwfGGUOJG2CUhMKEwmWgCGLKXnbsH1dnKXblfvFRoK3y0w5z2j1OJBKlaSozuJABq5B64JMJegk4dqsQtaax8CAwkQxM67/VyFI0rqUcRLrypYl5PWQ6/MmK71GcgyIsLt+RZhYF8jH1wyf5upAJWx4YrxVTom76D50mj/L/5d7M5mQMh+VLpL+88EejPKWuWCQgjC0w8CgQwvwIs76xMLJa8fVOnVMmDLRi+uYw/CVaivJzgtKX j896PdlO /LWEMsuHu7fGFdLoCt26LJOlnSuPIMM4xKHWK8t5FY5fBOeEP5ZY/m1VUluKRWJuLfk54bx0zP1hsozHG3B3foRGLI0MHSTPSZztZp4nISwZeb8WmsudlkF0zVQy51KghOiP61S916ZMmlkDnYB9ckO880kCrG86ebmpl9NYLh4asHBn03/Ik6c2B7V4O2uSTlE1K/VeXhWQErDFMxK1oaw9roY1IAVSH5QCPQZP2Qgq43aP9Zgn6Jv0NyoLYHIyjUSxG40jRcg7mK4/lfR6wW7SdyYtMVd2UgdT0PjuYJNeAFz8uba16/pqv8VVpzCr2OJrt1K+XyGGTB/psrPIyc+D+JHiPujtomUgd2JEKDOe6bhnHiRGTh/9WVw== 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: From: Brijesh Singh Before SNP VMs can be launched, the platform must be appropriately configured and initialized. Platform initialization is accomplished via the SNP_INIT command. Make sure to do a WBINVD and issue DF_FLUSH command to prepare for the first SNP guest launch after INIT. During the execution of SNP_INIT command, the firmware configures and enables SNP security policy enforcement in many system components. Some system components write to regions of memory reserved by early x86 firmware (e.g. UEFI). Other system components write to regions provided by the operation system, hypervisor, or x86 firmware. Such system components can only write to HV-fixed pages or Default pages. They will error when attempting to write to other page states after SNP_INIT enables their SNP enforcement. Starting in SNP firmware v1.52, the SNP_INIT_EX command takes a list of system physical address ranges to convert into the HV-fixed page states during the RMP initialization. If INIT_RMP is 1, hypervisors should provide all system physical address ranges that the hypervisor will never assign to a guest until the next RMP re-initialization. For instance, the memory that UEFI reserves should be included in the range list. This allows system components that occasionally write to memory (e.g. logging to UEFI reserved regions) to not fail due to RMP initialization and SNP enablement. Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra Co-developed-by: Jarkko Sakkinen Signed-off-by: Jarkko Sakkinen Signed-off-by: Brijesh Singh Signed-off-by: Michael Roth --- drivers/crypto/ccp/sev-dev.c | 242 +++++++++++++++++++++++++++++++++-- drivers/crypto/ccp/sev-dev.h | 2 + 2 files changed, 234 insertions(+), 10 deletions(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index ab3572286755..d3764ee073f3 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -27,6 +27,7 @@ #include #include +#include #include "psp-dev.h" #include "sev-dev.h" @@ -35,6 +36,10 @@ #define SEV_FW_FILE "amd/sev.fw" #define SEV_FW_NAME_SIZE 64 +/* Minimum firmware version required for the SEV-SNP support */ +#define SNP_MIN_API_MAJOR 1 +#define SNP_MIN_API_MINOR 51 + static DEFINE_MUTEX(sev_cmd_mutex); static struct sev_misc_dev *misc_dev; @@ -78,6 +83,14 @@ static void *sev_es_tmr; #define NV_LENGTH (32 * 1024) static void *sev_init_ex_buffer; +/* + * SEV_DATA_RANGE_LIST: + * Array containing range of pages that firmware transitions to HV-fixed + * page state. + */ +struct sev_data_range_list *snp_range_list; +static int __sev_snp_init_locked(int *error); + static inline bool sev_version_greater_or_equal(u8 maj, u8 min) { struct sev_device *sev = psp_master->sev_data; @@ -462,7 +475,8 @@ static int __sev_platform_init_locked(int *error) { struct psp_device *psp = psp_master; struct sev_device *sev; - int rc = 0, psp_ret = -1; + int psp_ret = -1; + int rc; int (*init_function)(int *error); if (!psp || !psp->sev_data) @@ -473,6 +487,26 @@ static int __sev_platform_init_locked(int *error) if (sev->state == SEV_STATE_INIT) return 0; + rc = __sev_snp_init_locked(error); + if (rc && rc != -ENODEV) { + /* + * Don't abort the probe if SNP INIT failed, + * continue to initialize the legacy SEV firmware. + */ + dev_err(sev->dev, "SEV-SNP: failed to INIT rc %d, error %#x\n", rc, *error); + } + + if (!sev_es_tmr) { + /* Obtain the TMR memory area for SEV-ES use */ + sev_es_tmr = sev_fw_alloc(SEV_ES_TMR_SIZE); + if (sev_es_tmr) + /* Must flush the cache before giving it to the firmware */ + clflush_cache_range(sev_es_tmr, SEV_ES_TMR_SIZE); + else + dev_warn(sev->dev, + "SEV: TMR allocation failed, SEV-ES support unavailable\n"); + } + if (sev_init_ex_buffer) { init_function = __sev_init_ex_locked; rc = sev_read_init_ex_file(); @@ -832,6 +866,191 @@ static int sev_update_firmware(struct device *dev) return ret; } +static void snp_set_hsave_pa(void *arg) +{ + wrmsrl(MSR_VM_HSAVE_PA, 0); +} + +static int snp_filter_reserved_mem_regions(struct resource *rs, void *arg) +{ + struct sev_data_range_list *range_list = arg; + struct sev_data_range *range = &range_list->ranges[range_list->num_elements]; + size_t size; + + if ((range_list->num_elements * sizeof(struct sev_data_range) + + sizeof(struct sev_data_range_list)) > PAGE_SIZE) + return -E2BIG; + + switch (rs->desc) { + case E820_TYPE_RESERVED: + case E820_TYPE_PMEM: + case E820_TYPE_ACPI: + range->base = rs->start & PAGE_MASK; + size = (rs->end + 1) - rs->start; + range->page_count = size >> PAGE_SHIFT; + range_list->num_elements++; + break; + default: + break; + } + + return 0; +} + +static int __sev_snp_init_locked(int *error) +{ + struct psp_device *psp = psp_master; + struct sev_data_snp_init_ex data; + struct sev_device *sev; + int rc = 0; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return -ENODEV; + + if (!psp || !psp->sev_data) + return -ENODEV; + + sev = psp->sev_data; + + if (sev->snp_initialized) + return 0; + + if (!sev_version_greater_or_equal(SNP_MIN_API_MAJOR, SNP_MIN_API_MINOR)) { + dev_dbg(sev->dev, "SEV-SNP support requires firmware version >= %d:%d\n", + SNP_MIN_API_MAJOR, SNP_MIN_API_MINOR); + return 0; + } + + /* + * The SNP_INIT requires the MSR_VM_HSAVE_PA must be set to 0h + * across all cores. + */ + on_each_cpu(snp_set_hsave_pa, NULL, 1); + + /* + * Starting in SNP firmware v1.52, the SNP_INIT_EX command takes a list of + * system physical address ranges to convert into the HV-fixed page states + * during the RMP initialization. For instance, the memory that UEFI + * reserves should be included in the range list. This allows system + * components that occasionally write to memory (e.g. logging to UEFI + * reserved regions) to not fail due to RMP initialization and SNP enablement. + */ + if (sev_version_greater_or_equal(SNP_MIN_API_MAJOR, 52)) { + /* + * Firmware checks that the pages containing the ranges enumerated + * in the RANGES structure are either in the Default page state or in the + * firmware page state. + */ + snp_range_list = kzalloc(PAGE_SIZE, GFP_KERNEL); + if (!snp_range_list) { + dev_err(sev->dev, + "SEV: SNP_INIT_EX range list memory allocation failed\n"); + return -ENOMEM; + } + + /* + * Retrieve all reserved memory regions setup by UEFI from the e820 memory map + * to be setup as HV-fixed pages. + */ + + rc = walk_iomem_res_desc(IORES_DESC_NONE, IORESOURCE_MEM, 0, ~0, + snp_range_list, snp_filter_reserved_mem_regions); + if (rc) { + dev_err(sev->dev, + "SEV: SNP_INIT_EX walk_iomem_res_desc failed rc = %d\n", rc); + return rc; + } + + memset(&data, 0, sizeof(data)); + data.init_rmp = 1; + data.list_paddr_en = 1; + data.list_paddr = __psp_pa(snp_range_list); + + /* + * Before invoking SNP_INIT_EX with INIT_RMP=1, make sure that + * all dirty cache lines containing the RMP are flushed. + * + * NOTE: that includes writes via RMPUPDATE instructions, which + * are also cacheable writes. + */ + wbinvd_on_all_cpus(); + + rc = __sev_do_cmd_locked(SEV_CMD_SNP_INIT_EX, &data, error); + if (rc) + return rc; + } else { + /* + * SNP_INIT is equivalent to SNP_INIT_EX with INIT_RMP=1, so + * just as with that case, make sure all dirty cache lines + * containing the RMP are flushed. + */ + wbinvd_on_all_cpus(); + + rc = __sev_do_cmd_locked(SEV_CMD_SNP_INIT, NULL, error); + if (rc) + return rc; + } + + /* Prepare for first SNP guest launch after INIT */ + wbinvd_on_all_cpus(); + rc = __sev_do_cmd_locked(SEV_CMD_SNP_DF_FLUSH, NULL, error); + if (rc) + return rc; + + sev->snp_initialized = true; + dev_dbg(sev->dev, "SEV-SNP firmware initialized\n"); + + return rc; +} + +static int __sev_snp_shutdown_locked(int *error) +{ + struct sev_device *sev = psp_master->sev_data; + struct sev_data_snp_shutdown_ex data; + int ret; + + if (!sev->snp_initialized) + return 0; + + memset(&data, 0, sizeof(data)); + data.length = sizeof(data); + data.iommu_snp_shutdown = 1; + + wbinvd_on_all_cpus(); + +retry: + ret = __sev_do_cmd_locked(SEV_CMD_SNP_SHUTDOWN_EX, &data, error); + /* SHUTDOWN may require DF_FLUSH */ + if (*error == SEV_RET_DFFLUSH_REQUIRED) { + ret = __sev_do_cmd_locked(SEV_CMD_SNP_DF_FLUSH, NULL, NULL); + if (ret) { + dev_err(sev->dev, "SEV-SNP DF_FLUSH failed\n"); + return ret; + } + goto retry; + } + if (ret) { + dev_err(sev->dev, "SEV-SNP firmware shutdown failed\n"); + return ret; + } + + sev->snp_initialized = false; + dev_dbg(sev->dev, "SEV-SNP firmware shutdown\n"); + + return ret; +} + +static int sev_snp_shutdown(int *error) +{ + int rc; + + mutex_lock(&sev_cmd_mutex); + rc = __sev_snp_shutdown_locked(error); + mutex_unlock(&sev_cmd_mutex); + + return rc; +} + static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable) { struct sev_device *sev = psp_master->sev_data; @@ -1279,6 +1498,8 @@ int sev_dev_init(struct psp_device *psp) static void sev_firmware_shutdown(struct sev_device *sev) { + int error; + sev_platform_shutdown(NULL); if (sev_es_tmr) { @@ -1295,6 +1516,13 @@ static void sev_firmware_shutdown(struct sev_device *sev) get_order(NV_LENGTH)); sev_init_ex_buffer = NULL; } + + if (snp_range_list) { + kfree(snp_range_list); + snp_range_list = NULL; + } + + sev_snp_shutdown(&error); } void sev_dev_destroy(struct psp_device *psp) @@ -1350,15 +1578,6 @@ void sev_pci_init(void) } } - /* Obtain the TMR memory area for SEV-ES use */ - sev_es_tmr = sev_fw_alloc(SEV_ES_TMR_SIZE); - if (sev_es_tmr) - /* Must flush the cache before giving it to the firmware */ - clflush_cache_range(sev_es_tmr, SEV_ES_TMR_SIZE); - else - dev_warn(sev->dev, - "SEV: TMR allocation failed, SEV-ES support unavailable\n"); - if (!psp_init_on_probe) return; @@ -1368,6 +1587,9 @@ void sev_pci_init(void) dev_err(sev->dev, "SEV: failed to INIT error %#x, rc %d\n", error, rc); + dev_info(sev->dev, "SEV%s API:%d.%d build:%d\n", sev->snp_initialized ? + "-SNP" : "", sev->api_major, sev->api_minor, sev->build); + return; err: diff --git a/drivers/crypto/ccp/sev-dev.h b/drivers/crypto/ccp/sev-dev.h index 666c21eb81ab..34767657beb5 100644 --- a/drivers/crypto/ccp/sev-dev.h +++ b/drivers/crypto/ccp/sev-dev.h @@ -52,6 +52,8 @@ struct sev_device { u8 build; void *cmd_buf; + + bool snp_initialized; }; int sev_dev_init(struct psp_device *psp); From patchwork Mon Jun 12 04:25: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: 13275476 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 9B6C2C7EE37 for ; Mon, 12 Jun 2023 04:32:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3BD396B0074; Mon, 12 Jun 2023 00:32:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 346888E0003; Mon, 12 Jun 2023 00:32:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1C03A8E0002; Mon, 12 Jun 2023 00:32:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 0B92E6B0074 for ; Mon, 12 Jun 2023 00:32:12 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id D4CA216026B for ; Mon, 12 Jun 2023 04:32:11 +0000 (UTC) X-FDA: 80892823662.29.523151C Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2086.outbound.protection.outlook.com [40.107.93.86]) by imf04.hostedemail.com (Postfix) with ESMTP id C7B3440010 for ; Mon, 12 Jun 2023 04:32:08 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=oCC2mau1; spf=pass (imf04.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.86 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686544328; 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=dJEJCtXjTD3p6zQVrVmrCzCQ8ktEmSSTgiuPOntccjk=; b=MbGlwf3MFCXA9LfRlPj0stHoW7LesjUK3KhYee7wiQmUkhs4us0yfESNOSAbYxoxVdKPCM YRtViFcF86BhXznzi9/rqK6c2Jvft7eRcdtBM/172IFopRqme1QP9q251TkwS+3yAoEYHg cMCZFgcAUWeDZnHeWT88AvB0u7HRg3k= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686544328; a=rsa-sha256; cv=pass; b=mpRkGoQWbrBnFafIUBF+0VMhbQHoW1V2WZ9YHdsZUtRahRGn+buHhN82s1dgGRJRXq/sgd 3cU47K9HMwoNkjuX2xz38cwkj5S/ah2biTEVy83HHiHy3KLHXjxksFcI0tCtjnLriIoj08 EGkHtpsfF1XWQ1x6xfEwHLavfDmCneY= ARC-Authentication-Results: i=2; imf04.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=oCC2mau1; spf=pass (imf04.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.86 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=G88XIdgK7dbYM786YjyBSPIoyhGxD8ae4u/yC3a0HRCbUZpudILnGcL9krvoxozyTAqX3ELBm0pF8RULlQQssL8qgmocpDYyaeQw13mecZDqtmMovcaoZBrKAzLnugE25KXbPkDmeeeXbhaAicxlgig2nVjIiJkFfxdhaRXj54ixYFKYzf9/hV88xmpQRYE9YC/0arHFG3NU9+zO52CgXbEsNQjnGD72/4Nz9kiEkHvDPLyQL/lMd8046+9M9rMFcWrVET/WOf7A0/ptiubmKcfzg//KEf2CZfZkMGkNAaC3HDOvTlnE99mpFSjGaXX3b8C4QRHpYCpDrl8103hK5w== 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=dJEJCtXjTD3p6zQVrVmrCzCQ8ktEmSSTgiuPOntccjk=; b=GCablkPMyq91VWPtNUN6bcwXkE7hdCQ1u9D5oaPkWqEZwzKNWLO3ohHc538Bd/Yfbibk+Jk9Kn5VkLm+95xp+BJKDEZnF7r6Eye+Xl6eZ/7bqV7c0gBiDWwTIeHAebR2x4hNa9GePEraZ4qmBBI1ayaD3MdWREO6vQu7p7TjW6LBxJPO2b4fE7oaoeMlbfKit/MU2s9zbRg+HEk03WztIWb1fD3DER1p3S9CK8cWKHDum5KEEtJub+N/2ljs/f+FplQTmSOlpnufKVKjDH4Hkb6bGEAnJXh1qaNTJ8ZBNhUta73tLEtDVfscdRO2b53WEmZcO5CWOVpMAQ6i1sJrNw== 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 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=dJEJCtXjTD3p6zQVrVmrCzCQ8ktEmSSTgiuPOntccjk=; b=oCC2mau1k5WHj2ypSYcugidetyViTxVJEp829Fzw36lXXSriVx8+tsBcALcXbAiWYaZbA9cj3NTYukYgu0gMgqtNtZGEH6ghWzIc/ks3UNjA88lH8KdadbLmdVmmwuXih9gJUrhaL4hAmXoGoVxCk0tomcVRGHe11Llng9Z5TV4= Received: from CY5PR03CA0030.namprd03.prod.outlook.com (2603:10b6:930:8::25) by PH7PR12MB6561.namprd12.prod.outlook.com (2603:10b6:510:213::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.29; Mon, 12 Jun 2023 04:32:04 +0000 Received: from CY4PEPF0000E9D2.namprd03.prod.outlook.com (2603:10b6:930:8:cafe::95) by CY5PR03CA0030.outlook.office365.com (2603:10b6:930:8::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.34 via Frontend Transport; Mon, 12 Jun 2023 04:32:04 +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 CY4PEPF0000E9D2.mail.protection.outlook.com (10.167.241.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:32:04 +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.23; Sun, 11 Jun 2023 23:32:03 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 18/51] crypto: ccp: Provide API to issue SEV and SNP commands Date: Sun, 11 Jun 2023 23:25:26 -0500 Message-ID: <20230612042559.375660-19-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CY4PEPF0000E9D2:EE_|PH7PR12MB6561:EE_ X-MS-Office365-Filtering-Correlation-Id: 08a2e49d-4b15-4d5d-5508-08db6afdf95c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: A8tqZ1uVCZaTqtuZtO1IPhsTKT7lYbW5efi5Xli+dF3TsegNHy5ezpWx44nd+PRF4oFxBPmCssmDyKYD1JpZ5rMOZyT8Xapt/BGE9Ik4lqeRztWElaws0x1c6sj1rsD+JFj6XRR96e4rio9SlQ4x8qmnYKTk0twWAHltvx25sGFAfOmmHnjdJfH126a8pl9ahZN79If+cpiizQVVON+nFzU4+p/BYUhlc9029P0poZRlld1lrDdZpbeFuo9RWIHErxea4Gebs4eoyn93/B5KEkNZK9RFQZYniUVrUWMD7vgWiKjz3KrP/gG2a33jCMnxFVymQjsCsb5bpYdWks92M6rnYLo7B83o74e6bErdfcWQKdtSGk+SBW6N/yinCv4zdW1i+QNuJvCurwXKCmlQso4dL08jsh+mOBltXICSb6arVDG0Dd+3Atu1QndSPCxYMAjxbkmHY4sNYsoqEffpRB/ZNodkplzgNQ4ecIEFeCrI1MWDsHS3yDs6L82/AyHzydhBVoQHijtwzP/B9Cpp9bdwxTvD82H4USXyp0+9U0woCyqkNr5y4+DVKUVnwZnfTEUbv91CT7rAdUNqW56Nq17fbQpVgsygMA9YqhIU8+weebDLcuPhw3eopuL5N0uFut5nBYQgje5Mnv80WyfVGSuSidsJ+haqUErN5O3gB771AvzpGjphrVHflpl7SxEoOdIeqP83vWaCPRVgUsf5SKsEnYp8PPATppvdN928VmGWa1qMOD6Z6kTaRh+LpLM4u7X30mrEejm3pou5s89g2nP7hhLChrcUWKAhL5ctk/0= 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:(13230028)(4636009)(396003)(136003)(376002)(346002)(39860400002)(451199021)(46966006)(36840700001)(40470700004)(316002)(40460700003)(41300700001)(336012)(426003)(82310400005)(83380400001)(40480700001)(2616005)(36860700001)(26005)(186003)(7416002)(7406005)(44832011)(2906002)(47076005)(86362001)(16526019)(1076003)(81166007)(356005)(82740400003)(8676002)(36756003)(5660300002)(8936002)(70586007)(70206006)(478600001)(54906003)(4326008)(6666004)(6916009)(36900700001)(134885004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:32:04.4342 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 08a2e49d-4b15-4d5d-5508-08db6afdf95c 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: CY4PEPF0000E9D2.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB6561 X-Rspamd-Queue-Id: C7B3440010 X-Rspam-User: X-Stat-Signature: b3gpo854ect3np71zmkrswnq489jopyt X-Rspamd-Server: rspam03 X-HE-Tag: 1686544328-608230 X-HE-Meta: U2FsdGVkX18GRBc4JcSIZoTpcnyCymPC13YIDLkEKi50d4rOsq0RFo/kskdUFluQYwOK8jF/AkCsnUGvjOP61gop4eAg1X2C8a0c70v0H3N/K9Fes64OOJw6xwUYWFdKW+iyZx/R3t6HrWqURqi3qUMGhByoiNc3CnI/CePTXojqKHPvhIFZBj8wO61YSlPV/uUJX8NVxU3j4ETtQSKuQWdbMt8J7HWkA/lcsuqQGQEXQgJX1us9h7N3fAcWWaVMB+3gpkWuG/rJmSu9i9B3mdDHLQ8DaLagDPyo5fNFA1PRcKNnn+Tqk73s6B8JP/jaxtZ4zW6paqouREoRlcgwrljljddv8K7BeMg78FnmhXjrKN4I9VNe0KtreHBaDrrnQw3aOs8Vvn/0GemfN0TyL5lyTlNOQHfnH2jQrrz54ddjERDbi+edZpdoseBhS7DhHT0XYbRyZ+dP8dbSrLwX44iwfMqiPKFA8FmstZTabpZ01AGr72ee9e7blI3ZFdigM7cBuJMiMspPVmPjgEP2/+i+3tOXx42RKLq6Qf69jPkqvqtH83s8NdXxHrcX5VZIvzuUycmzLX7OJ0yihK+5RXj/oK06Vxw9nTSArBR4ybLI4BJeqZOQVaLleaYOaf5nlvmMUHcSfDazCXML7Ic6EMDN0cVGcBBlDNqkz2a0WBKxFPI7XL0Sy+6wIniFcts6gC+1gYfHET3KezLBDOrira/FGdsejUI+arO5g5IFpTpPo1DtxzBgpXen7JU5DRlCLYEi6F0fJbkVD5tnHUJNSHFHEU4TGSdP3TeQYXcmUSsBYGDN9zw2IekVc+zVhEgdTEavJ0MEKN0Fc1Ptge3mXR9gTS1ZrhB6v8xfd5WRb3uGknxEz96bHg3mGwDFwowcCB8ZWYT7FpYHtFU7I01fGOH3BbpGqlqgHyghyqvli2CvBE5hGX9n8pdqhA/YQy781WRX60h5mHSuul0QZl0 29tJ7sMt lVWuvmvqu73GH6ecKw10DfkXO2FLW4dDdRUy6eGDdcmkXq83PDkqU0xTBOBfSF2JVX2LUqZAOwrcXzOLKnTUG1IKrJYybkg1UyTD1+fDDJDsAMG3bMLB+qQH6wHad5Y5qxnkBldFSdb/+w82agIT30GUy2RJW2tA5MLBL9cRUxBAppBYfrsnmF23EfKJZwtbynuZ2qUQrcxGFp1DaIFX7CliGtOMLUbjMZDrc4nSP19TH41Bjdwv8LtlX0ff9Wq2L1nVtMHmIdR1lYO+DWVyTLBYCKSIdlEw22LbdKFO9XPj/SR3WXou+4toMOA+4aGI8CrrVq9r2qImE8m+PLNNcrS5sEQ== 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: From: Brijesh Singh Make sev_do_cmd() a generic API interface for the hypervisor to issue commands to manage an SEV and SNP guest. The commands for SEV and SNP are defined in the SEV and SEV-SNP firmware specifications. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- drivers/crypto/ccp/sev-dev.c | 3 ++- include/linux/psp-sev.h | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index d3764ee073f3..88c5bf264a87 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -418,7 +418,7 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) return ret; } -static int sev_do_cmd(int cmd, void *data, int *psp_ret) +int sev_do_cmd(int cmd, void *data, int *psp_ret) { int rc; @@ -428,6 +428,7 @@ static int sev_do_cmd(int cmd, void *data, int *psp_ret) return rc; } +EXPORT_SYMBOL_GPL(sev_do_cmd); static int __sev_init_locked(int *error) { diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 06d0619ca442..c8656a36baeb 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -891,6 +891,20 @@ int sev_guest_df_flush(int *error); */ int sev_guest_decommission(struct sev_data_decommission *data, int *error); +/** + * sev_do_cmd - perform SEV command + * + * @error: SEV command return code + * + * Returns: + * 0 if the SEV successfully processed the command + * -%ENODEV if the SEV device is not available + * -%ENOTSUPP if the SEV does not support SEV + * -%ETIMEDOUT if the SEV command timed out + * -%EIO if the SEV returned a non-zero return code + */ +int sev_do_cmd(int cmd, void *data, int *psp_ret); + void *psp_copy_user_blob(u64 uaddr, u32 len); #else /* !CONFIG_CRYPTO_DEV_SP_PSP */ @@ -906,6 +920,9 @@ sev_guest_deactivate(struct sev_data_deactivate *data, int *error) { return -ENO static inline int sev_guest_decommission(struct sev_data_decommission *data, int *error) { return -ENODEV; } +static inline int +sev_do_cmd(int cmd, void *data, int *psp_ret) { return -ENODEV; } + static inline int sev_guest_activate(struct sev_data_activate *data, int *error) { return -ENODEV; } From patchwork Mon Jun 12 04:25:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275477 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 A939FC7EE25 for ; Mon, 12 Jun 2023 04:32:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 432DD6B0075; Mon, 12 Jun 2023 00:32:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3BB848E0003; Mon, 12 Jun 2023 00:32:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 20E508E0002; Mon, 12 Jun 2023 00:32:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 0A3526B0075 for ; Mon, 12 Jun 2023 00:32:33 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id D15A04024B for ; Mon, 12 Jun 2023 04:32:32 +0000 (UTC) X-FDA: 80892824544.29.7A1BD83 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2042.outbound.protection.outlook.com [40.107.100.42]) by imf28.hostedemail.com (Postfix) with ESMTP id DF6C7C000A for ; Mon, 12 Jun 2023 04:32:29 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=wrgBiGt0; spf=pass (imf28.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.100.42 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=1686544350; 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=gEd/MCK8LB8MNsiER/01EW7HgLCLIvaG50Hd8XNvNzg=; b=8YoOZqWhr5fi783OvP2fQDnJLNtYs4PRrattbct1Z8VVyiEhpdwRLVOQdqqx+J2gxzauzy Cr/qpF6c3N3qBY6wvlmy2w0bNBoGkoEmSHzBkFpLJEtrelnMG9tKGDpCvDOwuFbwLoxdtR Kue4muPd7j0vUnA2rZXk5lopraR1RLs= ARC-Authentication-Results: i=2; imf28.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=wrgBiGt0; spf=pass (imf28.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.100.42 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=1686544350; a=rsa-sha256; cv=pass; b=SW3PqX6jOcMtEEhrXYs+/tZDfMTnV5dQZnUUmYsYytaPswb8mil6PuHyGhHPK9NGtxri6g T9NLsfnnJiXctbm6V7vrciolHDmGwkn6BRnzOj3bqB7re4KLBhlckx593dZD2hwuQiRrsc TI9BuUh1h3Z+zlJaEavZ/LmDD9MAqRg= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=l5dqAoni8U7DvhaUFVyjNAH5i55tKrKmpNV/NPxsivGBxVJ7Mcl7KvFYB/UsKIKcWBNENyj2IDo8IF3xOPGe/+avB/J8DL2A+icF92I+oaBgaMNaJvNT9pA8mwXJG84dUvykIGeKiexF2R/j5xSwWdERcZJ1XEcpUPwlh6jnFUae4WWu/d5+jrm3a1qdZD4n9Xwf1t7xslo7nQJd1Og/7N1jFvcg6f2jL0tTUTbilSo+wg2e9QRDFk0GcJPeYhohph/DXziom/zB9MxJ12DCWKvpft5SahHeTRyEjaTlivbjf3sxyoXWHrDpFD3ywISls2KCd84A1wM1y0hzZ3t4sw== 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=gEd/MCK8LB8MNsiER/01EW7HgLCLIvaG50Hd8XNvNzg=; b=mzhy7sN7tIKjhYaD/9AMUKHhsTZi5BHcb0hKNZXZyAZ77eRGamgJFc4Q3KJFLBw4eZyW4JMD1vKIu4lrOvfckUJStDtKOcELnukE9qtrGFFrfx2kJQ+5rNbuLdCtnWqf02ZEJBXwex9obRh8nc7F78hVi/Djj8+jR8ufLt18bb6Pla9OsupNKEfFU2HlypzygOZkQPK9HVshHEypL6HlT12tIjbpixknkpi2X/8e8kru4j6qmwPeUESxybkH1EbaepYhXATRlI8qkkUyISY/zSbe61n1Yddl9THSSnga4P3rpcs3uJDYcZ/ZGJgAGoHd92czOdDUh1U1LPYpJo0B9g== 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 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=gEd/MCK8LB8MNsiER/01EW7HgLCLIvaG50Hd8XNvNzg=; b=wrgBiGt0FO4uFsCPHIj+JBuQvEhng2hP8I8bNpinvE+kF2l6oTQBv7SK0OfPm4gJDL94QX2ZQybqbq7uBvHaKgyajgz0tMblb3BP8vCuhLKgDKf3uSKLzLw/nUXTAsiv5iYMXesmFgJblceGAvzKChn8sio5HGrSnylZGX8MScQ= Received: from DS7PR05CA0032.namprd05.prod.outlook.com (2603:10b6:8:2f::31) by PH7PR12MB7355.namprd12.prod.outlook.com (2603:10b6:510:20e::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.32; Mon, 12 Jun 2023 04:32:26 +0000 Received: from CY4PEPF0000E9D4.namprd03.prod.outlook.com (2603:10b6:8:2f:cafe::24) by DS7PR05CA0032.outlook.office365.com (2603:10b6:8:2f::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:32:26 +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 CY4PEPF0000E9D4.mail.protection.outlook.com (10.167.241.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:32:26 +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.23; Sun, 11 Jun 2023 23:32:25 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 19/51] x86/sev: Introduce snp leaked pages list Date: Sun, 11 Jun 2023 23:25:27 -0500 Message-ID: <20230612042559.375660-20-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CY4PEPF0000E9D4:EE_|PH7PR12MB7355:EE_ X-MS-Office365-Filtering-Correlation-Id: 1660822a-76c1-4ebb-d3f6-08db6afe064c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: pg8/+Sf+npALiYaoZ2JZsTyszQWVPMSC384tr/tp6/43WpoR5REFrs4u+S9/hKqE6dj069oECbl6FWPI9hjIF6bgbqqNtaHeqvcJHoVsfK/sTu7k5twIEaXHUu+2scWDROXEJBeiMfzKJFO+1eyA0FuTOv+otbJt6C7pYgdNWAdd2/penWh8ZVtOemlHChqyv0DOyQWgMYzJ9pN6kCqQpVuWjiF7RqgIorQ/qwPKTOmBKv+Jsg9KyHTh1R88v7T/z1jE/YM13hkYgLk2nnhGKvlTb0P6m1Oams7RqNI4/gbpYJQzg0Yy9nNLqaFvMJF3yChsZDA1DcT9v18h4h/1qwd0O2pohEbfvXXAkrZu08DTLw0CMS9PjwHl9SlUklbNUWemhG9lbot6BBqUnDQaPqWYx+atarCc0Tg0/GXNVeuKmiGIkJcDBhrDQccLyYSmnKIz43TL4VeeWCsu+rEgs0J2zHfbEOq2x8+NOZ8fjS/Hlp45NIbWWHEZw9LU5ZJzTez4ZchLjn5KbYucqZzt2f+cg3zPOPHL29KfDdXWfiQLDMEyYTBEP+4dksXGKsKnOfLXfCnVl8SAuZeIt4PEHEPIQyHekljr3Gf2mpcmNoHaZk7yd4GewipQu3ET0HhTCzHofSmghVjjXOpTx5UOvJW2huxQ8jm3FpHd59olulp6n2hAXr0+gSXsHPtBvukt17m42AAJio+CGjgTUwMCnuHwu13E3NGKu2604CCEgSSaEOwW61XwiDzK4Xh5AUSwe2vuTg4jvEcKZkVBIOd71g== 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:(13230028)(4636009)(136003)(39860400002)(396003)(376002)(346002)(451199021)(36840700001)(46966006)(40470700004)(36756003)(2906002)(82310400005)(7406005)(86362001)(7416002)(44832011)(40480700001)(47076005)(186003)(16526019)(83380400001)(36860700001)(40460700003)(426003)(336012)(5660300002)(26005)(1076003)(82740400003)(478600001)(356005)(81166007)(54906003)(70586007)(6916009)(4326008)(2616005)(70206006)(41300700001)(316002)(8936002)(8676002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:32:26.1586 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1660822a-76c1-4ebb-d3f6-08db6afe064c 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: CY4PEPF0000E9D4.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7355 X-Rspamd-Queue-Id: DF6C7C000A X-Rspam-User: X-Stat-Signature: mcsbo5eabt3ybrzj5qcd8dmjdb3fpb6x X-Rspamd-Server: rspam01 X-HE-Tag: 1686544349-917256 X-HE-Meta: U2FsdGVkX18C6wb4ZBRhyaHh+ch2XmFo16xC2GBfcTuCcS7JhW0jk5pYWrml4VX+7uesGwgB0FIq/GLLsBoViqqL4T9vEoyBcju/pcUIpvtyNDV2VickCYE2Kx9SR98SIY3s/mNiu6D7j1Yffb2w0fUb3kY+sQownctpL+VMeXJkJul9WEA1gQ4VDK5RYe8+Py9f/g07ZTRpNWiB1NdcVl0J2QY3r+4xWag/jHVrHhKSIt9shltNW7EiLLLy0Fkr/wMnx0EDEk+LN4UrmtidWPkz+mfSYGZsX03chrENFV3Ar+JliQ8teYm+63GBjhGQtGGBmxqq5ObNiODNMH1N3j+NX2WMRXlmMChyc6H61UX597kV2ZtR9ICOZOy2lnWjBSVPL60cW7g0+hm5IiQLuNCa0ukvB/f4CprNRatFLS+UbsQC/HNcyie+dPBapZVGJLRmXZGMHCcqUBiGj40tqnrwE1x8ET1+1Bvl957fbj7HAnyO94f7GqWLbr9Hn4xX5ZhGOW0KGNQUNyMnNYXptkjuzXdqi2UHitwl0HEYDL5dY1tBsxR5aM6MGZ8D/FV1DCWECTJbWTzYaeLrixRZvZbGyHXgLDu0JsQ1hJBks+QYD1bVEdUtaQxvmKS6GlMT2zRNoa+3MRUIgwNEmlPmHaCa4bnIEkKPSNvUiy3nxGttvrIyDqB8Xwj9rlrNc6pZQCEQagD0To5zvGJyLtkWvn7ahq4pFYQHyV4Id4jLQtmKp49vazeYBRh2CUw0RvxHxkA7vkp2C2rHgWu1XVUOhyrGmz22LeymIJpdFzraMzYKaW4qCiPcfEbDi7AwLJpYgqJI1yeSBuDq6etv0OTk0bjYC8TnuX1bQedXMP2jGDnx1vK22Tip6SpXu58PycmQ+0BkmZJBMa9OBGf7hETWAVnbw7ZFgQzajKEQsvdTN+AeU7JnQ1vzXOaQk1pq0gg5f0GyIqoju1idk+VU7o0 k5wPLfYE diWIgWtVaKeF+0DlH+HU937D2YigF1t0xZAuOOrpBkwgIXWFwiNY7cqzoiB73eHKzmjciFrgsSPQrj+C30IW1ZPXawqAHzNKhq7cpbs1SwRMawbhY3FZJ47FspjO81NmvxOGA4KuJYEJ8vjZoLZrLi4iLuPDL4833TlcRhv0Nvx+5JaynPV+4WVAQTcsMNmrAaEMihiJv2Dez/w3CZPsdoc8apleC8kATUDyCXbw8j+u8iz8lBEtNfN7+uIPBUFkCnzkZojjZqHM63dj6r74YDZBpX1H9qfIDFjPl84UBSrc797R1OhNZIJK9Gg== 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: From: Ashish Kalra Pages are unsafe to be released back to the page-allocator, if they have been transitioned to firmware/guest state and can't be reclaimed or transitioned back to hypervisor/shared state. In this case add them to an internal leaked pages list to ensure that they are not freed or touched/accessed to cause fatal page faults. Signed-off-by: Ashish Kalra [mdr: relocate to arch/x86/coco/sev/host.c] Signed-off-by: Michael Roth --- arch/x86/coco/sev/host.c | 28 ++++++++++++++++++++++++++++ arch/x86/include/asm/sev-host.h | 3 +++ 2 files changed, 31 insertions(+) diff --git a/arch/x86/coco/sev/host.c b/arch/x86/coco/sev/host.c index cd3b4c6a25bc..373e91f5a337 100644 --- a/arch/x86/coco/sev/host.c +++ b/arch/x86/coco/sev/host.c @@ -64,6 +64,12 @@ struct rmpentry { static unsigned long rmptable_start __ro_after_init; static unsigned long rmptable_end __ro_after_init; +/* list of pages which are leaked and cannot be reclaimed */ +static LIST_HEAD(snp_leaked_pages_list); +static DEFINE_SPINLOCK(snp_leaked_pages_list_lock); + +static atomic_long_t snp_nr_leaked_pages = ATOMIC_LONG_INIT(0); + #undef pr_fmt #define pr_fmt(fmt) "SEV-SNP: " fmt @@ -494,3 +500,25 @@ int rmp_make_shared(u64 pfn, enum pg_level level) return rmpupdate(pfn, &val); } EXPORT_SYMBOL_GPL(rmp_make_shared); + +void snp_leak_pages(unsigned long pfn, unsigned int npages) +{ + struct page *page = pfn_to_page(pfn); + + WARN(1, "psc failed, pfn 0x%lx pages %d (marked offline)\n", pfn, npages); + + spin_lock(&snp_leaked_pages_list_lock); + while (npages--) { + /* + * Reuse the page's buddy list for chaining into the leaked + * pages list. This page should not be on a free list currently + * and is also unsafe to be added to a free list. + */ + list_add_tail(&page->buddy_list, &snp_leaked_pages_list); + sev_dump_rmpentry(pfn); + pfn++; + } + spin_unlock(&snp_leaked_pages_list_lock); + atomic_long_inc(&snp_nr_leaked_pages); +} +EXPORT_SYMBOL_GPL(snp_leak_pages); diff --git a/arch/x86/include/asm/sev-host.h b/arch/x86/include/asm/sev-host.h index 753e80d16433..bab3b226777a 100644 --- a/arch/x86/include/asm/sev-host.h +++ b/arch/x86/include/asm/sev-host.h @@ -19,6 +19,8 @@ void sev_dump_rmpentry(u64 pfn); int psmash(u64 pfn); int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int asid, bool immutable); int rmp_make_shared(u64 pfn, enum pg_level level); +void snp_leak_pages(unsigned long pfn, unsigned int npages); + #else static inline int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) { return 0; } static inline void sev_dump_rmpentry(u64 pfn) {} @@ -29,6 +31,7 @@ static inline int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int as return -ENODEV; } static inline int rmp_make_shared(u64 pfn, enum pg_level level) { return -ENODEV; } +void snp_leak_pages(unsigned long pfn, unsigned int npages) {} #endif #endif From patchwork Mon Jun 12 04:25:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275478 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 28941C7EE25 for ; Mon, 12 Jun 2023 04:33:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B5DDB8E0002; Mon, 12 Jun 2023 00:33:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AE87A6B007B; Mon, 12 Jun 2023 00:33:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9131C8E0002; Mon, 12 Jun 2023 00:33:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 80EAE6B0078 for ; Mon, 12 Jun 2023 00:33:16 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 576BFAEA2E for ; Mon, 12 Jun 2023 04:33:16 +0000 (UTC) X-FDA: 80892826392.06.E7E857F Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by imf29.hostedemail.com (Postfix) with ESMTP id 71624120002 for ; Mon, 12 Jun 2023 04:33:13 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Ap7wXx3Y; spf=pass (imf29.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.41 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686544393; 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=21O9V8fXiW3DdE/EWkyVZmcvDCjg+nAvrOW5CbIppOs=; b=FOyBSuPGJP5N6KntpGB+wf5WpHm5EsvHH080q0J6GVdPOGS99TO/LZWfyxDQ0UaR1cr4TD +EeJfeyNlYCYJgQCCZ52RaXMzLu68+TViZlFO9hDluawKG6utEZ1Psch+UC1oosx5w/6hK Ce9ZAp+OfTyhA1REm0ixScJZ9/uH+iA= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686544393; a=rsa-sha256; cv=pass; b=rYx5hXN+O9gYPEL7D5LI43isulgsX3mCdx0eRD3b3i47gZYF43jOtLUKDpHx/x2xMYWGi8 0pcb3msyO/7Waat3de0Bhg8DKc0xJA/Fa2q4YvNUsKZDB9BcCb2Upedp2DScwBA/K05bS2 9hK4TtNLqB55GqLV+tul6+J+trPUiys= ARC-Authentication-Results: i=2; imf29.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Ap7wXx3Y; spf=pass (imf29.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.41 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=INMgf6i4cDaPTI+dOPEyrPA2uEoSjGA314/tivaZGNTYqjugRu4VT6A5+etFlwE1uJa7E9iOHsmSsOTrTBb81JEkMiWX+7LHLfmTrjX6flsQfKXF4Ol23n3DQwBGCdilc4omny2PUD9CrfCsQXmhZBOwIZuNPjeEvV+/ZQGiUwFOGKHWWu1HM4wFZQjEozJ3SUjYRpVRdUahkpwJqQAsRqVcPJLDib0eLQ7UdHRn49w3V6bCBz9ROfJ7Jl6w0cp54kZQ24JDJQ4CA3QnLazkp58xVKHD3uU8Kr+3J43Wi4tmPRVjXM25b9kszGT1HSSOXIpA5tHXt2znnYHeXkqKWw== 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=21O9V8fXiW3DdE/EWkyVZmcvDCjg+nAvrOW5CbIppOs=; b=PWfmsjhRAL0/cdL3ZyKLTN6oiaAglpx6UtOe2nqBIe+jpygdUJIK8vLwmZRvV8YWyEy/DBAOInUjMLnJdboX5qvsBAO2a7q26ELIIdK817T7SOYI94iomSLPsCfCjPF0o4CDNw1x8uBnWQ3OBNVhfPOEITEO6a2b5K2ncND/TOD02YQimcvqNteMizEKLZ9GE00hmpSjPPiheJGfyHN5whyHiX/xC4ucTbrAzLCwiuHnkVTeP7XqqfNfSho4WokumN4CL8s1C2MsYSzUz94qgw3TuV+AUPvfG+q0EmhAvXC+0Ggrg2DI4qDjVXfeeTUGDNtfhtxeLvmYVPFurCzufw== 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 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=21O9V8fXiW3DdE/EWkyVZmcvDCjg+nAvrOW5CbIppOs=; b=Ap7wXx3YWuMfpLKJLLlGBJyev29ltYk56xeh2jBX0a0KpDHbh0qt5kI/8DMfikFQ+i2DTNwPr0EnEjmP6IhK2XewPOD3OzH8G0+90YWW5PhbNw0bi4vKfGIntXq3Ei6e8Ndjgmh3vP1wLJS4gL9zCMaYp0BSi+YRfDN2+vzbkq4= Received: from DM6PR05CA0050.namprd05.prod.outlook.com (2603:10b6:5:335::19) by MN2PR12MB4254.namprd12.prod.outlook.com (2603:10b6:208:1d0::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.29; Mon, 12 Jun 2023 04:33:10 +0000 Received: from DM6NAM11FT042.eop-nam11.prod.protection.outlook.com (2603:10b6:5:335:cafe::b4) by DM6PR05CA0050.outlook.office365.com (2603:10b6:5:335::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:33:10 +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 DM6NAM11FT042.mail.protection.outlook.com (10.13.173.165) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.29 via Frontend Transport; Mon, 12 Jun 2023 04:33:10 +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.23; Sun, 11 Jun 2023 23:33:04 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 20/51] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled Date: Sun, 11 Jun 2023 23:25:28 -0500 Message-ID: <20230612042559.375660-21-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: DM6NAM11FT042:EE_|MN2PR12MB4254:EE_ X-MS-Office365-Filtering-Correlation-Id: 1a77be9f-a83b-4c4f-8bdb-08db6afe208b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2Sg4XbqAhiK/QXS0msCLvjxEgyEWAnCLG02T5kqRVLllS2r1QlK0G6v91H/fcXC9+MwUTab9qwrD1yqkI/1HysRny+N4pv41yX0xC8H9WTlnyvmNg1t4Dgwumcda3sPj1UFjHWLJDIkvF2BWFqoGcX/nxwY+nrMJAjqAX6jhM5kKLNXNkSeXlJWZTTMej4HtL6TDx9+bvuBk03GsKyhRR3Bz3510sw4V5hh/Cm6A5r4SH1aU4tQLiAh+3TVFIkenub0ZqCZL5vDL1t9j+pORRlNpIzO//vjlJNvpKxMJ5+pr35hAyZTUKFSMv5GHSqbLEvCO0gUH2OMnB6glRe+5hHsGRKH+ibJ9U/5d6DlyLaS2phqRnbYBNw0IPhBCF8GM7ca2kEODgQDWOlNjaW9ze8AwziXqR+Khg/S38OHQGNYR8rHy5WBTSl7PyMI8qYryTc0HpUnwZBpvi5I3hcQQ6gx+wWCDNfL72dQ1nUthjIT2D2ROIuJbEmnkFKNMrwaIeY/3May7uR1FQS3LJTA9GoDT0KemaHXF8TFOKUYbwzCI+UWzamTJijsNWuWMU1NtZLcpnNu2mdo8MwIsZ5D5pxGT+nM8n97jI7a2oOKJe44jbRayordJNBUMwyU+ZMsjl28CNoGdUkqCfifOib10FYfkKUXZ75AI4SB8MJwrlCqtxVuYxBvhRDJPQKw9O7KgbVp92Xd7nKMZOhlIt7xtnUTD2q8eA6B/nDJKjHvc2UL6LTRG+WA6dgYlQ+4t5ODyaU0d/8IPT3Z2DOti7SEr6w== 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:(13230028)(4636009)(39860400002)(396003)(136003)(376002)(346002)(451199021)(40470700004)(36840700001)(46966006)(5660300002)(7406005)(7416002)(47076005)(8936002)(2906002)(8676002)(41300700001)(83380400001)(336012)(36860700001)(426003)(44832011)(356005)(82740400003)(81166007)(6916009)(70206006)(82310400005)(316002)(2616005)(40460700003)(70586007)(4326008)(186003)(16526019)(40480700001)(26005)(86362001)(1076003)(54906003)(478600001)(36756003)(6666004)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:33:10.2060 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1a77be9f-a83b-4c4f-8bdb-08db6afe208b 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: DM6NAM11FT042.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB4254 X-Rspamd-Queue-Id: 71624120002 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: yubd71x9njaikazrayad4e1hos435nia X-HE-Tag: 1686544393-972629 X-HE-Meta: U2FsdGVkX18sEWBiF1pp8YjKZ/Z3ykGkajtJyNKRx2DkyNnBUNvg92xvlR8uR26PhGiABiAipeRwKaTXDrNelRQbGoQ4FJHnkKVh5jYe+1k8w18+gmrD6j/MzxORFZt78ZYjwKUCScBVrL5SyJXYwN++j4CXsygw5R30su8jg9wyV+TY9140mbcSdobp53d6T7rK7CUBkYKtV6051dlw4+MVgRzOI+LbH1T3Gx8PnLC4mJWuttK1r4XOFdDt0PUAWuqbL27qIshM/j8hjPobGZ4xHzmTY4eTVieW/Oa1c+4AhNS/XrS4lYsgpMKafYnMDPv1CcVfKwYQUxjZj465jjy1hy6s3pwFImLq8ixPjUSdhXCzOZK7LhkJeQ5BiS2nNzkk7r6IDE+Oz16DMmX4IyYX8cPPxNxYda9AAYLYMpMWBdyAWCX3C/u56Pbz1iDLkkMuJa2ORwo8zOOokwCF90pCsE9N4uJRjgVddWkGh2wThWqgmQVQ0x0MW9dbN2trsfSI7Z62x+r4VwxJtgH/e+UajASQmpS9PaX4eFEnZytRm20jwFsY1g7LfPS0PneMMoZeCRJwOEgZEcXnyzgs9Edo6G5eHeeNaarFIQ7BVCT4trTZCDtrmxuwPQRa6p7603PornrFPslFwZY/yxtlY0x0gFAaK1p13y9DvAAZZYvlgUWxDu5ykKTSs1JsVjQhip6AU+U5VVmpVHSiNUC0kvg6hey4KnWfzUEijPFo3fxgpea7QYWs52fCfwX1hyUObD5lL8kj5KBZxs+gurS19f79m7uRMzIVWxcPLgLVf7vem6h/u5JSqZ6HcsKK2dq88Va2HGbCVFNN5NV6o1EjDzW7WtV2bKdtpz8iVg8gyoI+3L50udtxEZHIQ+ZF/5jWqCRHBD6YJiYg1bTWQvXpipxsAliLuTLu+W6w0+d2RVq4T0iaF1lGlailjos1jQIQNCozTI57Kjt4U/TnHap kIlWv6P1 wuejD6Mzgd5IfXgm1kXlyoAO+oV4khfv4y4QjS2MR2IoRo9eC5Wn5UtHF/KHLX9Q46NdotQ7SXV58efieHDMeSTfIOtR4xiZ6hArmIie/8pjbKH6nfpoes8Hy+CQ4W89CN6WRxFvLxWye09+b+BkNcgDpuLsVf5ghosIg0LetMhV2rk2n5UFtImloTKWHxEJQB0eQnLsKcnzroVxGE8b5zL8mSdv/tKRIBFVY7vVDL15g0bNGp00Tg9Jms/kUG6XJgYUnMJzWZ8xzExO0ToCbU9cy/1Lgjlvt0FsClwZKGPmPE9IbcPyovUwxkg== 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: From: Brijesh Singh The behavior and requirement for the SEV-legacy command is altered when the SNP firmware is in the INIT state. See SEV-SNP firmware specification for more details. Allocate the Trusted Memory Region (TMR) as a 2mb sized/aligned region when SNP is enabled to satisfy new requirements for the SNP. Continue allocating a 1mb region for !SNP configuration. While at it, provide API that can be used by others to allocate a page that can be used by the firmware. The immediate user for this API will be the KVM driver. The KVM driver to need to allocate a firmware context page during the guest creation. The context page need to be updated by the firmware. See the SEV-SNP specification for further details. Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra Signed-off-by: Brijesh Singh [mdr: use struct sev_data_snp_page_reclaim instead of passing paddr directly to SEV_CMD_SNP_PAGE_RECLAIM] Signed-off-by: Michael Roth --- drivers/crypto/ccp/sev-dev.c | 150 ++++++++++++++++++++++++++++++++--- include/linux/psp-sev.h | 9 +++ 2 files changed, 150 insertions(+), 9 deletions(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 88c5bf264a87..d8124d33c831 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -91,6 +91,13 @@ static void *sev_init_ex_buffer; struct sev_data_range_list *snp_range_list; static int __sev_snp_init_locked(int *error); +/* When SEV-SNP is enabled the TMR needs to be 2MB aligned and 2MB size. */ +#define SEV_SNP_ES_TMR_SIZE (2 * 1024 * 1024) + +static size_t sev_es_tmr_size = SEV_ES_TMR_SIZE; + +static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret); + static inline bool sev_version_greater_or_equal(u8 maj, u8 min) { struct sev_device *sev = psp_master->sev_data; @@ -191,11 +198,131 @@ static int sev_cmd_buffer_len(int cmd) return 0; } +static int snp_reclaim_pages(unsigned long paddr, unsigned int npages, bool locked) +{ + /* Cbit maybe set in the paddr */ + unsigned long pfn = __sme_clr(paddr) >> PAGE_SHIFT; + int ret, err, i, n = 0; + + for (i = 0; i < npages; i++, pfn++, n++) { + struct sev_data_snp_page_reclaim data = {0}; + + data.paddr = pfn << PAGE_SHIFT; + + if (locked) + ret = __sev_do_cmd_locked(SEV_CMD_SNP_PAGE_RECLAIM, &data, &err); + else + ret = sev_do_cmd(SEV_CMD_SNP_PAGE_RECLAIM, &data, &err); + + if (ret) + goto cleanup; + + ret = rmp_make_shared(pfn, PG_LEVEL_4K); + if (ret) + goto cleanup; + } + + return 0; + +cleanup: + /* + * If failed to reclaim the page then page is no longer safe to + * be release back to the system, leak it. + */ + snp_leak_pages(pfn, npages - n); + return ret; +} + +static int rmp_mark_pages_firmware(unsigned long paddr, unsigned int npages, bool locked) +{ + /* Cbit maybe set in the paddr */ + unsigned long pfn = __sme_clr(paddr) >> PAGE_SHIFT; + int rc, n = 0, i; + + for (i = 0; i < npages; i++, n++, pfn++) { + rc = rmp_make_private(pfn, 0, PG_LEVEL_4K, 0, true); + if (rc) + goto cleanup; + } + + return 0; + +cleanup: + /* + * Try unrolling the firmware state changes by + * reclaiming the pages which were already changed to the + * firmware state. + */ + snp_reclaim_pages(paddr, n, locked); + + return rc; +} + +static struct page *__snp_alloc_firmware_pages(gfp_t gfp_mask, int order, bool locked) +{ + unsigned long npages = 1ul << order, paddr; + struct sev_device *sev; + struct page *page; + + if (!psp_master || !psp_master->sev_data) + return NULL; + + page = alloc_pages(gfp_mask, order); + if (!page) + return NULL; + + /* If SEV-SNP is initialized then add the page in RMP table. */ + sev = psp_master->sev_data; + if (!sev->snp_initialized) + return page; + + paddr = __pa((unsigned long)page_address(page)); + if (rmp_mark_pages_firmware(paddr, npages, locked)) + return NULL; + + return page; +} + +void *snp_alloc_firmware_page(gfp_t gfp_mask) +{ + struct page *page; + + page = __snp_alloc_firmware_pages(gfp_mask, 0, false); + + return page ? page_address(page) : NULL; +} +EXPORT_SYMBOL_GPL(snp_alloc_firmware_page); + +static void __snp_free_firmware_pages(struct page *page, int order, bool locked) +{ + struct sev_device *sev = psp_master->sev_data; + unsigned long paddr, npages = 1ul << order; + + if (!page) + return; + + paddr = __pa((unsigned long)page_address(page)); + if (sev->snp_initialized && + snp_reclaim_pages(paddr, npages, locked)) + return; + + __free_pages(page, order); +} + +void snp_free_firmware_page(void *addr) +{ + if (!addr) + return; + + __snp_free_firmware_pages(virt_to_page(addr), 0, false); +} +EXPORT_SYMBOL_GPL(snp_free_firmware_page); + static void *sev_fw_alloc(unsigned long len) { struct page *page; - page = alloc_pages(GFP_KERNEL, get_order(len)); + page = __snp_alloc_firmware_pages(GFP_KERNEL, get_order(len), false); if (!page) return NULL; @@ -443,7 +570,7 @@ static int __sev_init_locked(int *error) data.tmr_address = __pa(sev_es_tmr); data.flags |= SEV_INIT_FLAGS_SEV_ES; - data.tmr_len = SEV_ES_TMR_SIZE; + data.tmr_len = sev_es_tmr_size; } return __sev_do_cmd_locked(SEV_CMD_INIT, &data, error); @@ -466,7 +593,7 @@ static int __sev_init_ex_locked(int *error) data.tmr_address = __pa(sev_es_tmr); data.flags |= SEV_INIT_FLAGS_SEV_ES; - data.tmr_len = SEV_ES_TMR_SIZE; + data.tmr_len = sev_es_tmr_size; } return __sev_do_cmd_locked(SEV_CMD_INIT_EX, &data, error); @@ -499,14 +626,16 @@ static int __sev_platform_init_locked(int *error) if (!sev_es_tmr) { /* Obtain the TMR memory area for SEV-ES use */ - sev_es_tmr = sev_fw_alloc(SEV_ES_TMR_SIZE); - if (sev_es_tmr) + sev_es_tmr = sev_fw_alloc(sev_es_tmr_size); + if (sev_es_tmr) { /* Must flush the cache before giving it to the firmware */ - clflush_cache_range(sev_es_tmr, SEV_ES_TMR_SIZE); - else + if (!sev->snp_initialized) + clflush_cache_range(sev_es_tmr, sev_es_tmr_size); + } else { dev_warn(sev->dev, "SEV: TMR allocation failed, SEV-ES support unavailable\n"); } + } if (sev_init_ex_buffer) { init_function = __sev_init_ex_locked; @@ -1001,6 +1130,8 @@ static int __sev_snp_init_locked(int *error) sev->snp_initialized = true; dev_dbg(sev->dev, "SEV-SNP firmware initialized\n"); + sev_es_tmr_size = SEV_SNP_ES_TMR_SIZE; + return rc; } @@ -1507,8 +1638,9 @@ static void sev_firmware_shutdown(struct sev_device *sev) /* The TMR area was encrypted, flush it from the cache */ wbinvd_on_all_cpus(); - free_pages((unsigned long)sev_es_tmr, - get_order(SEV_ES_TMR_SIZE)); + __snp_free_firmware_pages(virt_to_page(sev_es_tmr), + get_order(sev_es_tmr_size), + false); sev_es_tmr = NULL; } diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index c8656a36baeb..5ae61de96e44 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -906,6 +906,8 @@ int sev_guest_decommission(struct sev_data_decommission *data, int *error); int sev_do_cmd(int cmd, void *data, int *psp_ret); void *psp_copy_user_blob(u64 uaddr, u32 len); +void *snp_alloc_firmware_page(gfp_t mask); +void snp_free_firmware_page(void *addr); #else /* !CONFIG_CRYPTO_DEV_SP_PSP */ @@ -933,6 +935,13 @@ sev_issue_cmd_external_user(struct file *filep, unsigned int id, void *data, int static inline void *psp_copy_user_blob(u64 __user uaddr, u32 len) { return ERR_PTR(-EINVAL); } +static inline void *snp_alloc_firmware_page(gfp_t mask) +{ + return NULL; +} + +static inline void snp_free_firmware_page(void *addr) { } + #endif /* CONFIG_CRYPTO_DEV_SP_PSP */ #endif /* __PSP_SEV_H__ */ From patchwork Mon Jun 12 04:25:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275480 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 1D493C7EE23 for ; Mon, 12 Jun 2023 04:34:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9AF8E6B0074; Mon, 12 Jun 2023 00:34:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 960108E0005; Mon, 12 Jun 2023 00:34:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7DAC78E0003; Mon, 12 Jun 2023 00:34:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 6B7946B0074 for ; Mon, 12 Jun 2023 00:34:37 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 475E680255 for ; Mon, 12 Jun 2023 04:34:37 +0000 (UTC) X-FDA: 80892829794.06.12CD65B Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2076.outbound.protection.outlook.com [40.107.243.76]) by imf16.hostedemail.com (Postfix) with ESMTP id 0D83A180017 for ; Mon, 12 Jun 2023 04:34:33 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=3jUmAcJZ; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf16.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.243.76 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686544474; a=rsa-sha256; cv=pass; b=bgExE0XCnEVypmqavyO3EfaIKixhPJAlU+Ks8fLnEHCAT0boI4N/qNLOS8AbWxRBQFkIiQ AuEi4V4moVjGnu2d4LXMRmUiDuTMFwdczagrxd6ZdgiWzeorUz3Wi25zc/7wE1ZfyMKsEB C4DYh1fcdQiWgIIX87sA6NVH5eUonc8= ARC-Authentication-Results: i=2; imf16.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=3jUmAcJZ; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf16.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.243.76 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686544474; 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=FtlNrZkBs+mDmj0lD3T2KGzFDdVu9lV4Sfe9JlYuGEc=; b=ndzVLJyggrZTwbFlfJEnbmPQYu9wBS+ti1chSV3UTR5GZn2Adw3F20NZD9YRdzITUlKVLY B+0djBhPs9wg4Fysy5jSg0eD4NG2lYNx6xWZasmmMuAlyH1cOOtBGmoKI2fYYzq+96Lc3b lVDYq69bXrQtZm6K+dvx9cPdMhC/JGE= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=L4kJjcsIR5AhX8Q9VuuNAGKlqFZb7gmylOfq5ShJfOiqEmYwFsIU60DwpMCExMhcd7GOx7fKsaioLkYlztBZ4lfMhT/mXqKHwtSm4+fYkBQp31lSDnw8kbteoNGXL6WUmQNV+iZtwiySxp+J39/tm4Pks1Uo+nFA1MLfcSiqfn7ZJQ5eiSlMuYBtCrAlNzIdp9meZ8sJCIvww9vCDsW2h/1guVHE2FYyPE4KsbQWIbE458EzrAOzlxFt2wnqX9JFSPlIRd1ARDYU3EKLUMkzow0X+J60FBfzCueSKd1Ob12iqA0hT31or1P1JfXL7elJeweGdVnvQR6boeKZ67NVIg== 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=FtlNrZkBs+mDmj0lD3T2KGzFDdVu9lV4Sfe9JlYuGEc=; b=W3f2nMqdaQT0uZjw0gGd1njBfjp1bRJNG8/FXw2Ew8L5GB++tdrW1rfrpR6D0HewRn5BFfhF8u/H9d2pdqRYanZCaK8LkAMmCdx8MWompMevehtxfksD5/00PEbOnYLxp0FFfM+75xFRHiBcexvkdJJWZhLZZC+JLFDQyMN38X1F+ZUg3m4Qr8LkO+nOUzd0OB6O9Applri95L8T8o7hCUmKpe9s2/mktNfY1dtgxOU56SsCZ2FC5/K6bfjpfcxHovQ1LTUHpXLhJuUODtRCVxYWaGZaSeLS1AcQPztZEOL5Yxla0IeA3xBk4jjtMiMMclV5yNrxr++C+wM81muYAw== 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 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=FtlNrZkBs+mDmj0lD3T2KGzFDdVu9lV4Sfe9JlYuGEc=; b=3jUmAcJZbc758CLRnNKO8b9lezHl48ZH9S11doolj5rSPdAk5qQs+ylOVb9vVoOAnetZ3VDcHMkHzp6IgqCJs0m/nQ0W93Yi8jY/2BLpqRm5tcbxV4qDeVd/wqLMtdKTsbySIQopG+xpRx4HloGboj5oWbv6ibSUxTxyasAMjaY= Received: from BY3PR03CA0005.namprd03.prod.outlook.com (2603:10b6:a03:39a::10) by BL1PR12MB5335.namprd12.prod.outlook.com (2603:10b6:208:317::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.38; Mon, 12 Jun 2023 04:34:30 +0000 Received: from DM6NAM11FT062.eop-nam11.prod.protection.outlook.com (2603:10b6:a03:39a:cafe::e4) by BY3PR03CA0005.outlook.office365.com (2603:10b6:a03:39a::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:34:30 +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 DM6NAM11FT062.mail.protection.outlook.com (10.13.173.40) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:34:29 +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.23; Sun, 11 Jun 2023 23:34:25 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 21/51] crypto: ccp: Handle the legacy SEV command when SNP is enabled Date: Sun, 11 Jun 2023 23:25:29 -0500 Message-ID: <20230612042559.375660-22-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: DM6NAM11FT062:EE_|BL1PR12MB5335:EE_ X-MS-Office365-Filtering-Correlation-Id: 0d352f9b-ad2f-42b2-c8e9-08db6afe5009 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: DvPe/j7WtUOROF+vN0nk6wN9zCriPinr01iviun9TQtqQITNq7JHVipiJnL0nWsOB+xDi5ChSoDa7SA1NuDUDKTjfCEqKwNbK3XNuHCXkY3ezPKcs6Cr8iyQhRc6gCRU75OsVRhhe5DEsVmVa3dSF1V+N9x93Tk9cDcKqKmWRDTAFPpWgZhltHbgL98A3Y3JPE2+ipIcethfgQI42U7hLEOS/FgelsFzANRDwRMeftRuziaqxn/o+F2YmQqCetr4i03eFnxv7Dyzz9Th2k1eR4SMPgymfE1flsIrbo8xDk3nzKjW4J8lzoBPZj2wCF8kN0IoJ/hPbaOsaUId6PcKGC7lYCI0i50bT92r01+2+90hHicVk/QiN8zWn0HNk8XfP56PnG+J9R1T8KMEKPR7AALAsVRpn2AyxRwltlSSAInJs9fzFYalVw+HLDstMjvPYLKPtWykKM0T8xK2Tgo9PCTwPcwe5pv0AaAAv5PoGYct04XFQUvAIkKDqNQrxpU+o6kZB2qH0XKe4L8Xw8K7hwUBTCP8tXGYdI2AEaiS+uGC82jY//F0hHcDs+MpK7I3JYo9Ow/27lxN4LrCU5hRjMgifvhoR+mAIn9fa3xpyvfCiHliiKAzeXIzsPvTj2MVx2azGrjr6aH7bJ/J6Z0KKHYuDpeCvv+I9bd7GL3Te8nbvuPDp+HrNezDriYKbqJbQUhj85/UhSy24BakqdqQ7QYNgIJ8EQHOpwIa6fxRbwM0h3sk9ors4noDrZEWn5MAMHYVZ0eMS8I/Si31RQ9ibg== 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:(13230028)(4636009)(136003)(376002)(346002)(39860400002)(396003)(451199021)(36840700001)(40470700004)(46966006)(316002)(40460700003)(41300700001)(336012)(426003)(26005)(83380400001)(36860700001)(82310400005)(7416002)(186003)(16526019)(44832011)(47076005)(1076003)(7406005)(2616005)(2906002)(86362001)(30864003)(82740400003)(356005)(40480700001)(81166007)(36756003)(8936002)(8676002)(70206006)(70586007)(478600001)(5660300002)(54906003)(6916009)(4326008)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:34:29.9331 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0d352f9b-ad2f-42b2-c8e9-08db6afe5009 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: DM6NAM11FT062.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5335 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 0D83A180017 X-Stat-Signature: zm7g1znadub438ukqiwyrzh14jibx97n X-HE-Tag: 1686544473-533745 X-HE-Meta: U2FsdGVkX18zzrt+a3ujLlimaQNZJ10Vh0J8TXfv4g2uqSnz36gL18tWCs046X0G+W2tWTACPP1Wckn+HMCn9Y6KiIOa1U2e0ZlriLEf62RqtiIhz9M/svWENdpHKHakmimITGjEfv+BiQwiXVeqE9OlmSA2zs0h74GzjTuDGP6V6aWbAxlscHz+uQfbNT8rW2zogd2GT9SjIAwYfbt9tCaodQ5zd0jMppgmmVDL4JuF42U2DZKZauZp//fe7s8VKC63sLwpgWDFFThaX71kwsmrDk3aGpG1pZulxlIv45lVe0dmi1nscU1xQ7ULDUlIEtFMSjYPTrQiEwxsIcfuiACf+hITU2zGn64JsM9IfdQ3OKwXN9K/TZWb54sYqcWyYzNEMDd/bAuDAfw70zH+TP0MVzTbkCFzaIqCVcdbrgAF0wTft20dkzyu8f8D6yg+etdNnx2ko6KQoHym2gS45Bmj1Fwlku3OrVimKSVWOYDtfMupH1IXwfRE8rql2X3sEezielQGZpCppr4NkPlV0xoH8/cNPK36sNFlzm5RaYrz9MXWR1Y/aE9pxO+7Msxw0oFWyFyAIPEals0rZjWb+dKlwZD2eFWojEWx7tjAtP56SG9D7IlFZX2WF0/7L39zpQBPk7Zkc8HJlrY5l8Ttwt2aI+ICHnZ+WJC6ttmYnZDdfm7vkxxS10dtuuXZE2jWqSQtOAEkT/OuaIXwSqrM5eCQcRmvd40a9t0jjSSwUyBv7pBGGSllNBmyxeEV7UADjbdMEZWxQ2EvEvxEyD6jgKQ0xlvmBSd23LOuOSUyQU/x5iWwVnm0xwJHVSf89s6BOcS3cY+uzsBzM9QxK40QNrOaJ6/dPTFIiW2vVYHhV0jYVtBN/820PIdi5hUDr193SiyfMHI99fg4oYa1hdS4+vFsTyD2LkJAVACO8/C//lvxCOKjjoURHb/KlmyYue4NFVVozgnZzyfiBBWh7gN Rs9Ih/46 nmBdmz21kxmDVtVLCo5iYa2uU1iIOXigY/rcgdObHxbDqCvAVGkOk8KtdItUv5O8wGXWjdEkiQdqP/Yhst0j91MTFbJA9uzJTZ9arxC1vFUSXvKl6GYBQ4UYPT1IIkA8xn0N6phGNj/d0y+/N/LAAmL3uw1Xw9OxUhoiNzRhyCOu97DMpj3S6YerfOR5E8rE2onuThZNvT9b3cdwOL6ZuI51kCg3aLIJb5v624XSz+O948PbZnxHQl60j3ArOn7XBITwBRyyUI52AWKeO+Wui9yqfDw64XE31kP6CL8OB4Ni+8CxL53kWTR8Llg== 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: From: Brijesh Singh The behavior of the SEV-legacy commands is altered when the SNP firmware is in the INIT state. When SNP is in INIT state, all the SEV-legacy commands that cause the firmware to write to memory must be in the firmware state before issuing the command.. A command buffer may contains a system physical address that the firmware may write to. There are two cases that need to be handled: 1) system physical address points to a guest memory 2) system physical address points to a host memory To handle the case #1, change the page state to the firmware in the RMP table before issuing the command and restore the state to shared after the command completes. For the case #2, use a bounce buffer to complete the request. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- drivers/crypto/ccp/sev-dev.c | 371 ++++++++++++++++++++++++++++++++++- drivers/crypto/ccp/sev-dev.h | 12 ++ 2 files changed, 373 insertions(+), 10 deletions(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index d8124d33c831..10bb0a7dcfd6 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -28,6 +28,7 @@ #include #include #include +#include #include "psp-dev.h" #include "sev-dev.h" @@ -258,6 +259,30 @@ static int rmp_mark_pages_firmware(unsigned long paddr, unsigned int npages, boo return rc; } +static int rmp_mark_pages_shared(unsigned long paddr, unsigned int npages) +{ + /* Cbit maybe set in the paddr */ + unsigned long pfn = __sme_clr(paddr) >> PAGE_SHIFT; + int rc, n = 0, i; + + for (i = 0; i < npages; i++, pfn++, n++) { + rc = rmp_make_shared(pfn, PG_LEVEL_4K); + if (rc) + goto cleanup; + } + + return 0; + +cleanup: + /* + * If failed to change the page state to shared, then its not safe + * to release the page back to the system, leak it. + */ + snp_leak_pages(pfn, npages - n); + + return rc; +} + static struct page *__snp_alloc_firmware_pages(gfp_t gfp_mask, int order, bool locked) { unsigned long npages = 1ul << order, paddr; @@ -459,12 +484,295 @@ static int sev_write_init_ex_file_if_required(int cmd_id) return sev_write_init_ex_file(); } +static int alloc_snp_host_map(struct sev_device *sev) +{ + struct page *page; + int i; + + for (i = 0; i < MAX_SNP_HOST_MAP_BUFS; i++) { + struct snp_host_map *map = &sev->snp_host_map[i]; + + memset(map, 0, sizeof(*map)); + + page = alloc_pages(GFP_KERNEL_ACCOUNT, get_order(SEV_FW_BLOB_MAX_SIZE)); + if (!page) + return -ENOMEM; + + map->host = page_address(page); + } + + return 0; +} + +static void free_snp_host_map(struct sev_device *sev) +{ + int i; + + for (i = 0; i < MAX_SNP_HOST_MAP_BUFS; i++) { + struct snp_host_map *map = &sev->snp_host_map[i]; + + if (map->host) { + __free_pages(virt_to_page(map->host), get_order(SEV_FW_BLOB_MAX_SIZE)); + memset(map, 0, sizeof(*map)); + } + } +} + +static int map_firmware_writeable(u64 *paddr, u32 len, bool guest, struct snp_host_map *map) +{ + unsigned int npages = PAGE_ALIGN(len) >> PAGE_SHIFT; + + map->active = false; + + if (!paddr || !len) + return 0; + + map->paddr = *paddr; + map->len = len; + + /* If paddr points to a guest memory then change the page state to firmwware. */ + if (guest) { + if (rmp_mark_pages_firmware(*paddr, npages, true)) + return -EFAULT; + + goto done; + } + + if (!map->host) + return -ENOMEM; + + /* Check if the pre-allocated buffer can be used to fullfil the request. */ + if (len > SEV_FW_BLOB_MAX_SIZE) + return -EINVAL; + + /* Transition the pre-allocated buffer to the firmware state. */ + if (rmp_mark_pages_firmware(__pa(map->host), npages, true)) + return -EFAULT; + + /* Set the paddr to use pre-allocated firmware buffer */ + *paddr = __psp_pa(map->host); + +done: + map->active = true; + return 0; +} + +static int unmap_firmware_writeable(u64 *paddr, u32 len, bool guest, struct snp_host_map *map) +{ + unsigned int npages = PAGE_ALIGN(len) >> PAGE_SHIFT; + + if (!map->active) + return 0; + + /* If paddr points to a guest memory then restore the page state to hypervisor. */ + if (guest) { + if (snp_reclaim_pages(*paddr, npages, true)) + return -EFAULT; + + goto done; + } + + /* + * Transition the pre-allocated buffer to hypervisor state before the access. + * + * This is because while changing the page state to firmware, the kernel unmaps + * the pages from the direct map, and to restore the direct map the pages must + * be transitioned back to the shared state. + */ + if (snp_reclaim_pages(__pa(map->host), npages, true)) + return -EFAULT; + + /* Copy the response data firmware buffer to the callers buffer. */ + memcpy(__va(__sme_clr(map->paddr)), map->host, min_t(size_t, len, map->len)); + *paddr = map->paddr; + +done: + map->active = false; + return 0; +} + +static bool sev_legacy_cmd_buf_writable(int cmd) +{ + switch (cmd) { + case SEV_CMD_PLATFORM_STATUS: + case SEV_CMD_GUEST_STATUS: + case SEV_CMD_LAUNCH_START: + case SEV_CMD_RECEIVE_START: + case SEV_CMD_LAUNCH_MEASURE: + case SEV_CMD_SEND_START: + case SEV_CMD_SEND_UPDATE_DATA: + case SEV_CMD_SEND_UPDATE_VMSA: + case SEV_CMD_PEK_CSR: + case SEV_CMD_PDH_CERT_EXPORT: + case SEV_CMD_GET_ID: + case SEV_CMD_ATTESTATION_REPORT: + return true; + default: + return false; + } +} + +#define prep_buffer(name, addr, len, guest, map) \ + func(&((typeof(name *))cmd_buf)->addr, ((typeof(name *))cmd_buf)->len, guest, map) + +static int __snp_cmd_buf_copy(int cmd, void *cmd_buf, bool to_fw, int fw_err) +{ + int (*func)(u64 *paddr, u32 len, bool guest, struct snp_host_map *map); + struct sev_device *sev = psp_master->sev_data; + bool from_fw = !to_fw; + + /* + * After the command is completed, change the command buffer memory to + * hypervisor state. + * + * The immutable bit is automatically cleared by the firmware, so + * no not need to reclaim the page. + */ + if (from_fw && sev_legacy_cmd_buf_writable(cmd)) { + if (rmp_mark_pages_shared(__pa(cmd_buf), 1)) + return -EFAULT; + + /* No need to go further if firmware failed to execute command. */ + if (fw_err) + return 0; + } + + if (to_fw) + func = map_firmware_writeable; + else + func = unmap_firmware_writeable; + + /* + * A command buffer may contains a system physical address. If the address + * points to a host memory then use an intermediate firmware page otherwise + * change the page state in the RMP table. + */ + switch (cmd) { + case SEV_CMD_PDH_CERT_EXPORT: + if (prep_buffer(struct sev_data_pdh_cert_export, pdh_cert_address, + pdh_cert_len, false, &sev->snp_host_map[0])) + goto err; + if (prep_buffer(struct sev_data_pdh_cert_export, cert_chain_address, + cert_chain_len, false, &sev->snp_host_map[1])) + goto err; + break; + case SEV_CMD_GET_ID: + if (prep_buffer(struct sev_data_get_id, address, len, + false, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_PEK_CSR: + if (prep_buffer(struct sev_data_pek_csr, address, len, + false, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_LAUNCH_UPDATE_DATA: + if (prep_buffer(struct sev_data_launch_update_data, address, len, + true, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_LAUNCH_UPDATE_VMSA: + if (prep_buffer(struct sev_data_launch_update_vmsa, address, len, + true, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_LAUNCH_MEASURE: + if (prep_buffer(struct sev_data_launch_measure, address, len, + false, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_LAUNCH_UPDATE_SECRET: + if (prep_buffer(struct sev_data_launch_secret, guest_address, guest_len, + true, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_DBG_DECRYPT: + if (prep_buffer(struct sev_data_dbg, dst_addr, len, false, + &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_DBG_ENCRYPT: + if (prep_buffer(struct sev_data_dbg, dst_addr, len, true, + &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_ATTESTATION_REPORT: + if (prep_buffer(struct sev_data_attestation_report, address, len, + false, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_SEND_START: + if (prep_buffer(struct sev_data_send_start, session_address, + session_len, false, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_SEND_UPDATE_DATA: + if (prep_buffer(struct sev_data_send_update_data, hdr_address, hdr_len, + false, &sev->snp_host_map[0])) + goto err; + if (prep_buffer(struct sev_data_send_update_data, trans_address, + trans_len, false, &sev->snp_host_map[1])) + goto err; + break; + case SEV_CMD_SEND_UPDATE_VMSA: + if (prep_buffer(struct sev_data_send_update_vmsa, hdr_address, hdr_len, + false, &sev->snp_host_map[0])) + goto err; + if (prep_buffer(struct sev_data_send_update_vmsa, trans_address, + trans_len, false, &sev->snp_host_map[1])) + goto err; + break; + case SEV_CMD_RECEIVE_UPDATE_DATA: + if (prep_buffer(struct sev_data_receive_update_data, guest_address, + guest_len, true, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_RECEIVE_UPDATE_VMSA: + if (prep_buffer(struct sev_data_receive_update_vmsa, guest_address, + guest_len, true, &sev->snp_host_map[0])) + goto err; + break; + default: + break; + } + + /* The command buffer need to be in the firmware state. */ + if (to_fw && sev_legacy_cmd_buf_writable(cmd)) { + if (rmp_mark_pages_firmware(__pa(cmd_buf), 1, true)) + return -EFAULT; + } + + return 0; + +err: + return -EINVAL; +} + +static inline bool need_firmware_copy(int cmd) +{ + struct sev_device *sev = psp_master->sev_data; + + /* After SNP is INIT'ed, the behavior of legacy SEV command is changed. */ + return ((cmd < SEV_CMD_SNP_INIT) && sev->snp_initialized) ? true : false; +} + +static int snp_aware_copy_to_firmware(int cmd, void *data) +{ + return __snp_cmd_buf_copy(cmd, data, true, 0); +} + +static int snp_aware_copy_from_firmware(int cmd, void *data, int fw_err) +{ + return __snp_cmd_buf_copy(cmd, data, false, fw_err); +} + static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) { struct psp_device *psp = psp_master; struct sev_device *sev; unsigned int phys_lsb, phys_msb; unsigned int reg, ret = 0; + void *cmd_buf; int buf_len; if (!psp || !psp->sev_data) @@ -484,12 +792,28 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) * work for some memory, e.g. vmalloc'd addresses, and @data may not be * physically contiguous. */ - if (data) - memcpy(sev->cmd_buf, data, buf_len); + if (data) { + if (sev->cmd_buf_active > 2) + return -EBUSY; + + cmd_buf = sev->cmd_buf_active ? sev->cmd_buf_backup : sev->cmd_buf; + + memcpy(cmd_buf, data, buf_len); + sev->cmd_buf_active++; + + /* + * The behavior of the SEV-legacy commands is altered when the + * SNP firmware is in the INIT state. + */ + if (need_firmware_copy(cmd) && snp_aware_copy_to_firmware(cmd, cmd_buf)) + return -EFAULT; + } else { + cmd_buf = sev->cmd_buf; + } /* Get the physical address of the command buffer */ - phys_lsb = data ? lower_32_bits(__psp_pa(sev->cmd_buf)) : 0; - phys_msb = data ? upper_32_bits(__psp_pa(sev->cmd_buf)) : 0; + phys_lsb = data ? lower_32_bits(__psp_pa(cmd_buf)) : 0; + phys_msb = data ? upper_32_bits(__psp_pa(cmd_buf)) : 0; dev_dbg(sev->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n", cmd, phys_msb, phys_lsb, psp_timeout); @@ -532,15 +856,24 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) ret = sev_write_init_ex_file_if_required(cmd); } - print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data, - buf_len, false); - /* * Copy potential output from the PSP back to data. Do this even on * failure in case the caller wants to glean something from the error. */ - if (data) - memcpy(data, sev->cmd_buf, buf_len); + if (data) { + /* + * Restore the page state after the command completes. + */ + if (need_firmware_copy(cmd) && + snp_aware_copy_from_firmware(cmd, cmd_buf, ret)) + return -EFAULT; + + memcpy(data, cmd_buf, buf_len); + sev->cmd_buf_active--; + } + + print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data, + buf_len, false); return ret; } @@ -624,6 +957,14 @@ static int __sev_platform_init_locked(int *error) dev_err(sev->dev, "SEV-SNP: failed to INIT rc %d, error %#x\n", rc, *error); } + /* + * Allocate the intermediate buffers used for the legacy command handling. + */ + if (rc != -ENODEV && alloc_snp_host_map(sev)) { + dev_notice(sev->dev, "Failed to alloc host map (disabling legacy SEV)\n"); + goto skip_legacy; + } + if (!sev_es_tmr) { /* Obtain the TMR memory area for SEV-ES use */ sev_es_tmr = sev_fw_alloc(sev_es_tmr_size); @@ -677,6 +1018,7 @@ static int __sev_platform_init_locked(int *error) dev_info(sev->dev, "SEV API:%d.%d build:%d\n", sev->api_major, sev->api_minor, sev->build); +skip_legacy: return 0; } @@ -1586,10 +1928,12 @@ int sev_dev_init(struct psp_device *psp) if (!sev) goto e_err; - sev->cmd_buf = (void *)devm_get_free_pages(dev, GFP_KERNEL, 0); + sev->cmd_buf = (void *)devm_get_free_pages(dev, GFP_KERNEL, 1); if (!sev->cmd_buf) goto e_sev; + sev->cmd_buf_backup = (uint8_t *)sev->cmd_buf + PAGE_SIZE; + psp->sev_data = sev; sev->dev = dev; @@ -1655,6 +1999,12 @@ static void sev_firmware_shutdown(struct sev_device *sev) snp_range_list = NULL; } + /* + * The host map need to clear the immutable bit so it must be free'd before the + * SNP firmware shutdown. + */ + free_snp_host_map(sev); + sev_snp_shutdown(&error); } @@ -1726,6 +2076,7 @@ void sev_pci_init(void) return; err: + free_snp_host_map(sev); psp_master->sev_data = NULL; } diff --git a/drivers/crypto/ccp/sev-dev.h b/drivers/crypto/ccp/sev-dev.h index 34767657beb5..19d79f9d4212 100644 --- a/drivers/crypto/ccp/sev-dev.h +++ b/drivers/crypto/ccp/sev-dev.h @@ -29,11 +29,20 @@ #define SEV_CMDRESP_CMD_SHIFT 16 #define SEV_CMDRESP_IOC BIT(0) +#define MAX_SNP_HOST_MAP_BUFS 2 + struct sev_misc_dev { struct kref refcount; struct miscdevice misc; }; +struct snp_host_map { + u64 paddr; + u32 len; + void *host; + bool active; +}; + struct sev_device { struct device *dev; struct psp_device *psp; @@ -52,8 +61,11 @@ struct sev_device { u8 build; void *cmd_buf; + void *cmd_buf_backup; + int cmd_buf_active; bool snp_initialized; + struct snp_host_map snp_host_map[MAX_SNP_HOST_MAP_BUFS]; }; int sev_dev_init(struct psp_device *psp); From patchwork Mon Jun 12 04:25:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275481 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 0A9C3C7EE25 for ; Mon, 12 Jun 2023 04:35:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 988186B0075; Mon, 12 Jun 2023 00:35:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 938A68E0005; Mon, 12 Jun 2023 00:35:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7B4A18E0003; Mon, 12 Jun 2023 00:35:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 6E29A6B0075 for ; Mon, 12 Jun 2023 00:35:10 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 353FB1C78BF for ; Mon, 12 Jun 2023 04:35:09 +0000 (UTC) X-FDA: 80892831138.22.39E63DF Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2057.outbound.protection.outlook.com [40.107.94.57]) by imf16.hostedemail.com (Postfix) with ESMTP id 3C2B7180010 for ; Mon, 12 Jun 2023 04:35:04 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=tvRLduvJ; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf16.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.57 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; 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=1686544505; 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=M5PixqsA5QQA7A/TruSoyBOLJ81zSazDzW860FjASDM=; b=0fQWDHILSu0NqfFFSwPKNPjZWNUGlS1xGmcfEJCVixKGdLLpyVSO9+SCJ9mq+ojkKIj1rE QWWyj1IefwCGR0d/Dx5TKRI7FUkXVp5K6T9d3Klq2yFDrZKxsi6VinLqcnHfYk0DU4BC2x IwZh/FfmjUreSrycSqo0Aus/xbwtcJ8= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686544505; a=rsa-sha256; cv=pass; b=VkqTZew2qVHkLvsDIaFkC0gLqeVCnFrMu/LnP1tgWvROWO1GxqXGDJ2r5MYkzO9+u3JBQU iIZ9S2mvJM9Mzd1LuKiKvHm442fmFuEzZgEtyu+7tHIIy2nbAumvjLAeWwxWI5n+DUnXdm agjA3M/ce4dF8lzY+NDXHyQQZcjZyJ8= ARC-Authentication-Results: i=2; imf16.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=tvRLduvJ; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf16.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.57 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IQl1tdIjBibIEFuipN+aaTxMV2HbOsbS8Os/ymr3CFj2GR3isN90l5UOC1ER8OK3ebqzSRJyT6BV+AdHrG/Gy0OfZb6djpIw5GI/d2Ik4bajySzpnIfAXcXNtfLPbV6fOhJbHGL/U2NoyMh/8aEHq32OTW9XLtvHIg0nW+K3Z/3Wpk5jaRrg7f7lY1OzH/ycCKxwTv6cd214eWWyJtF+Pn9YvFgHevKYh7x5Tjkv7FAYOWMHmxhhInonPSoZ19FldaqdQ6QeXVK74nu1U4BTPZJavpXY5Q6x9XP9UqW/mTLZzD9z3rcS54f5Otoljnoq5sSFRvI+CY2IKNC2m4XUTw== 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=M5PixqsA5QQA7A/TruSoyBOLJ81zSazDzW860FjASDM=; b=Vt6jpzBLv1fLwWoqkqmAUQ48nRmNkZM5WfBEORVTyqo/1CDxa/GnLgpzplAcXcTYSTfVPtvBrYuhbZGpJKDqknogICTMrgptEC39FH1wTZOCYCG8YQTgy0gBTnXHRZY53tBwKt1tjqwKfcerzkhbwXSg9U2H1qSfRQCGkY1D+QkhFSdj3wcJacV7RuoUN8JkrhpwmSzBpTgtcHsjpr/rhXWO2SHA3vxjPeQ3ImMuThhJbrlrSBTO5hogZKqBGMpajFK81YII7pRsXBZ5A5w1PNf2YA8VPtC8XKXnvflpMsdOWCWuvA/mU007Mko+P7Za+Bhj8rNNONy5hB//oyBNdQ== 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 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=M5PixqsA5QQA7A/TruSoyBOLJ81zSazDzW860FjASDM=; b=tvRLduvJeIPeBQi00OeM5XF2DKp//bfS7DSmDW6QverV7JVujMsh1uIM3tVuROwUxMn5IJOJckey94lQGzzjL9ehJsmWo7AIoT7PKx2c8qZyy2aUzIGUEgTIVhTetHZZ7pIbS/ZTCyBIfmwQ2qgt+b1bTAPZmV8MxL7b7vAOhHY= Received: from DM6PR06CA0022.namprd06.prod.outlook.com (2603:10b6:5:120::35) by IA1PR12MB8264.namprd12.prod.outlook.com (2603:10b6:208:3f5::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.29; Mon, 12 Jun 2023 04:35:02 +0000 Received: from DM6NAM11FT042.eop-nam11.prod.protection.outlook.com (2603:10b6:5:120:cafe::db) by DM6PR06CA0022.outlook.office365.com (2603:10b6:5:120::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:35:02 +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 DM6NAM11FT042.mail.protection.outlook.com (10.13.173.165) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.29 via Frontend Transport; Mon, 12 Jun 2023 04:35:02 +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.23; Sun, 11 Jun 2023 23:35:01 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 22/51] crypto: ccp: Add the SNP_PLATFORM_STATUS command Date: Sun, 11 Jun 2023 23:25:30 -0500 Message-ID: <20230612042559.375660-23-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: DM6NAM11FT042:EE_|IA1PR12MB8264:EE_ X-MS-Office365-Filtering-Correlation-Id: e5162c36-bd59-4262-029b-08db6afe633c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: x/nzRJqVhQegFZIFjd69mC9F6yvWmKohIrk/5lP+hGKK9DKXj1CQZxEj0Juf6Vq5uEnjlBtsxXjX3U6OrXcOrc/3s338NMCgHVdIKfGeYW9uSU1Wm2bbjCFpZGPacre19SJPpqHNsXPzNTbA82fPg0eAUrUczSilmZxUjqdj0q+4hoeK/FvER6JzTxCcZcM9A3GtmbQN8B+7ZqBHYwDubssca0oA37d9d2Ggakl8ptwxa9blznG6XN4YA9/eud/uWyps2078qpKJPv7BUVOMiqaJ+v1smTxbDyiCdSWgs8ETvNVD45EFfx18KlNo9/+3w47HJbNhsKzW+7+W/HD5HtfaCuG4v1pft72dgAIy2UPxPJF4Jp5MeIkaG/CElIsQra0Okul1/UNT5Xew3Eub2drOqw4uG2Qg5uManqT5oK51tWcr1AweI3TzwZqLaFdzdnMnxvlscvgGZJ17mEdlpJeSHlDu21qzUObv/kTPLGtDTHhSe2yAvIFfDAjjdeyF0NhDybsRH0IxSMKgxRywZ6HeepF9LWPVTjd2h7X70ozvICtrJAQR1nWWxZHNmfXL2aJ4dK/o2JpValyF1zFg8bn081GUAXDcVLK5GnpNUKg1O0xMsKPXuQowMNmbTQaBlRyBTBgwx3hEh68wp9e2cXS7rEOyvxqi0bj7porylowKT4Wd71d8bnGHWxkqkjD6effA96Kp1hKrH7JxG3NXVqK+8o2RnRJ0KuCMFk2vz98EHUoazo0XF2P2DrrvjSfNiFGwDErpC3rtgw6NS16+4w== 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:(13230028)(4636009)(346002)(136003)(376002)(39860400002)(396003)(451199021)(36840700001)(46966006)(40470700004)(40460700003)(70586007)(70206006)(316002)(6666004)(4326008)(6916009)(478600001)(54906003)(36756003)(86362001)(47076005)(36860700001)(1076003)(26005)(426003)(336012)(16526019)(186003)(83380400001)(41300700001)(5660300002)(8936002)(8676002)(7416002)(44832011)(82310400005)(2906002)(40480700001)(7406005)(356005)(2616005)(81166007)(82740400003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:35:02.1078 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e5162c36-bd59-4262-029b-08db6afe633c 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: DM6NAM11FT042.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB8264 X-Stat-Signature: tagdf9ysd4p8e6ym4i5ygraaqruodjw5 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 3C2B7180010 X-Rspam-User: X-HE-Tag: 1686544504-691198 X-HE-Meta: U2FsdGVkX192UTejLjb7tJEY7w93Ut9Jae9HhW8yVP3nhEbTpe/lpGYehv5qjmID1QMP7bWnssO9laisMoCy0KaHuAnxC43nb3KbjjGMwSUZzK9x/DdOxxyiBZV+vwIOXqqYzDWXvvG1h7AdcWAdfLJVNFV9sKYwsfpCzTV04W0c8szm9ytG+e8WjoSW3F3NtlRGLUYF+xn56T4vbuS2kKMAdF4MFhkbk3rYh8J3ap1ilPeDrQs+sF9C3JL5X9Kj5Ex4HEBACqKnFtnkNr+dDzBjw648i7fY9iLDxmqe/VedA8hHrUHNdIIB/vYrivJy6Dc54ofbKWdyuuWKEaIVsr+uk2EJWlENwjRbZtp+kUP7cPUIqqQV5EKxmFPEzhMu5yrKYU8A+4y/uDZrMB+HgNntR6jF9CNGJXLRv5nxmUB2MKGJ5JZKKiuOu/O9MwuIiTk+1bes/2/mC50i6Mfp719cjJYi3OsLJPfHJttNPI4F/1Egoqmw+1leoLCz50tqDGeRpFBi741T4eilcO6/x8YML7f4Zg+6+ByG1t60BpDKjvukkV6gOexwaYL5le/9nC0iB+DfQ92lsVcvE6wgbtoftfvu6Me965ekydRoJgupO0e94L5kwgqCzkRjr6u1TnCraVthszOiocOsORihMnAxTVPg282zGjGBtolKyfL/j86DofwYe9n7ZZChsSycaFlpTLGshgUfNhHhkvjDe3OUHlQHAr7epj6jmaRAvP5vA8wLCzV5yGNIxiDy5Y+JE/00TJ9Tnh/gU2WB+HZxuHBanMr5mSjUSLgib1GUfUA1cGGPHNDSngJyvx/b0KskV+lVw+VxZOF3YaFSGxGEPFWwenzDsg4Ybu9ur7g3AV5lqajVXHPATSFvE9tud3jAd9mZ3+qvlZRiKo5k1IhQFiPeA66OlP83GqYgTTeImgb0CUQHB4rBESQ2HhiUGZBGp00nuooDBbb+OPmIEWP dizrgVTs q2TDVbZSwq7I1EFnehhvwhV83DV1An18y/xCJ+yHBOnn+OOplo8/SuHLBu4ZdUzGtl588YEl6V+M0Q74QfHjw0rStT3TCkgqJ7n5hag6zBzWy6VGVrQf/iaqOYKCInmd0e8Q7DuAKytnOhKxRi8vHSY70GpPHGCMHFVA2RUPU9rfAnq4UOHzQgCW6GCa26MOdwEQUSBHRjJImsZ5saM8OYp13E+DPKIZonFqUU7qpLv5pv6pVzkCPK/GgEXqBEbgc7BJ2onkTGJFmLTrtQBO3TMnBwwkq/gUz/pqjo1Gyf66YBt84/rOrpD0uEqiRyz2zy/+Ebb4KdQTiLEomsjXt2OLy6pFGSrGOslNTlZXC3J5eQkg= 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: From: Brijesh Singh The command can be used by the userspace to query the SNP platform status report. See the SEV-SNP spec for more details. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- Documentation/virt/coco/sev-guest.rst | 27 ++++++++++++++++ drivers/crypto/ccp/sev-dev.c | 45 +++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 1 + 3 files changed, 73 insertions(+) diff --git a/Documentation/virt/coco/sev-guest.rst b/Documentation/virt/coco/sev-guest.rst index bf593e88cfd9..11ea67c944df 100644 --- a/Documentation/virt/coco/sev-guest.rst +++ b/Documentation/virt/coco/sev-guest.rst @@ -61,6 +61,22 @@ counter (e.g. counter overflow), then -EIO will be returned. __u64 fw_err; }; +The host ioctl should be called to /dev/sev device. The ioctl accepts command +id and command input structure. + +:: + struct sev_issue_cmd { + /* Command ID */ + __u32 cmd; + + /* Command request structure */ + __u64 data; + + /* firmware error code on failure (see psp-sev.h) */ + __u32 error; + }; + + 2.1 SNP_GET_REPORT ------------------ @@ -118,6 +134,17 @@ be updated with the expected value. See GHCB specification for further detail on how to parse the certificate blob. +2.4 SNP_PLATFORM_STATUS +----------------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (in): struct sev_data_snp_platform_status +:Returns (out): 0 on success, -negative on error + +The SNP_PLATFORM_STATUS command is used to query the SNP platform status. The +status includes API major, minor version and more. See the SEV-SNP +specification for further details. + 3. SEV-SNP CPUID Enforcement ============================ diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 10bb0a7dcfd6..0bfe9721c977 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -1767,6 +1767,48 @@ static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable) return ret; } +static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp) +{ + struct sev_device *sev = psp_master->sev_data; + struct sev_data_snp_addr buf; + struct page *status_page; + void *data; + int ret; + + if (!sev->snp_initialized || !argp->data) + return -EINVAL; + + status_page = alloc_page(GFP_KERNEL_ACCOUNT); + if (!status_page) + return -ENOMEM; + + data = page_address(status_page); + if (rmp_mark_pages_firmware(__pa(data), 1, true)) { + __free_pages(status_page, 0); + return -EFAULT; + } + + buf.gctx_paddr = __psp_pa(data); + ret = __sev_do_cmd_locked(SEV_CMD_SNP_PLATFORM_STATUS, &buf, &argp->error); + + /* Change the page state before accessing it */ + if (snp_reclaim_pages(__pa(data), 1, true)) { + snp_leak_pages(__pa(data) >> PAGE_SHIFT, 1); + return -EFAULT; + } + + if (ret) + goto cleanup; + + if (copy_to_user((void __user *)argp->data, data, + sizeof(struct sev_user_data_snp_status))) + ret = -EFAULT; + +cleanup: + __free_pages(status_page, 0); + return ret; +} + static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) { void __user *argp = (void __user *)arg; @@ -1818,6 +1860,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) case SEV_GET_ID2: ret = sev_ioctl_do_get_id2(&input); break; + case SNP_PLATFORM_STATUS: + ret = sev_ioctl_snp_platform_status(&input); + break; default: ret = -EINVAL; goto out; diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h index 7d8a2dd20273..4dc6a3e7b3d5 100644 --- a/include/uapi/linux/psp-sev.h +++ b/include/uapi/linux/psp-sev.h @@ -28,6 +28,7 @@ enum { SEV_PEK_CERT_IMPORT, SEV_GET_ID, /* This command is deprecated, use SEV_GET_ID2 */ SEV_GET_ID2, + SNP_PLATFORM_STATUS, SEV_MAX, }; From patchwork Mon Jun 12 04:25:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275482 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 F0D65C7EE25 for ; Mon, 12 Jun 2023 04:35:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 913666B0074; Mon, 12 Jun 2023 00:35:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8C4358E0003; Mon, 12 Jun 2023 00:35:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 764B88E0002; Mon, 12 Jun 2023 00:35:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 672CE6B0074 for ; Mon, 12 Jun 2023 00:35:42 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 3BAEAA025B for ; Mon, 12 Jun 2023 04:35:42 +0000 (UTC) X-FDA: 80892832524.14.CE4D996 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2079.outbound.protection.outlook.com [40.107.92.79]) by imf10.hostedemail.com (Postfix) with ESMTP id 36768C000A for ; Mon, 12 Jun 2023 04:35:37 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=eovlZJ5k; spf=pass (imf10.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.79 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=1686544538; a=rsa-sha256; cv=pass; b=FRc9rpvtMBkgWE0v2dq9s0/0O3R5wAg9La117puSzHbD3dwiQhtyfMyasImx/aVYLFe13Z s/NN6t0diti33QQaPeB3GaSX9Xpdw2bPfmNgj2B8T7Ur35D2D8kmkO8IioDYdv13oIslIe Z5NTM3Y+LC1sI4ZnMNpu89hjZ9z7LXM= ARC-Authentication-Results: i=2; imf10.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=eovlZJ5k; spf=pass (imf10.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.79 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=1686544538; 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=d4wfb4qGv5SePlU/T9QnyyuYBpCMuLO/Bu9lKFk7jQ4=; b=SB2u/R9bFevO0enyXhDLx/jvY9xX9XMtdGMi6HPPcURuLFWd9oyLCkATsOP+vuj+AMyc6k TOnyqSHMF0PNDzj9PkKTUw0VznYT48R1QHAZ62xrqOTHIe+x2Avu+anrZnWvv2FXqO9Z1h JBOBq+gTNYnuwBF257uZmgJCJU+oALw= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=D8i5nQwB/HjU+uPR828/aad7kv67DnXqL/PwyIwBcWrwYuqW011fPKDhrBAbJ0fbBakYlEMUgTB5CUJ9AzH5Ch2Ff61AjORrc1GDNaCLq2cM9p2vh6ALr2XlpTQ6hHuBQZJUNPB1+xl9eQ3WW4vBaXzRaVNfeIffq745W9bsXghgHsXy2lV1W3M+IyeFAw8SXvtn3quJQgJZJdz2RrybQO8L3uaxBdfPOZlo91IDvLdzQXIZeZaq2dcU/CY0wqgbpvGdU7rajYVqAnw1xkHhqzZf7UvqzXPk7IXErY6GydLewhUS6MJXIEiIpvYVtzqaIS5awSpmuxrs1taRV3FNeA== 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=d4wfb4qGv5SePlU/T9QnyyuYBpCMuLO/Bu9lKFk7jQ4=; b=mxljwh4NsMm32WvOFbJSJHL0ylcX1d3tdhyo4iCjX9mvNPVQUES/qGa95IZph+lPOVm5KB8vNAoKZSz8wdG6iVbtpnLFOSnqcgdvcfYe/Ebis9sIM2UOsICAoAJp8Vz+9GvN/syIcN7EMLOwdhyQSk53Ce6OKgmXB88AIOAzUTvtxmx/oBh65GOmChmksJJTESK4p797fJ2ZcxldRSj4PNSw3dryF+JimeijhxtOdROrU9mN1dAFmgNyEsEtuV/DB+zQhWGCC6oAf4kbNKrwQCvUW9irZPVJct1YFvEIItwhoVErrVp/NH1N6UVpLicqr//ouv9TxLAtoKgGWimKQA== 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 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=d4wfb4qGv5SePlU/T9QnyyuYBpCMuLO/Bu9lKFk7jQ4=; b=eovlZJ5kzFJCKFPxwIPETtb0LXJ4MaCToPGe1OCNAJbctz5UcYNvfG3x4JZe+fH8qKTXYh+RCmGgfuE3CHQSzOVNyLm/tvsczLH/XJWAqqN1j22UTRhMcDGLHb5+4Kc7Xq/dvGFbpYq0adbjC3cudDnFzN9FsOPhWAK6M2MCk8M= Received: from DS7PR03CA0189.namprd03.prod.outlook.com (2603:10b6:5:3b6::14) by BY5PR12MB4225.namprd12.prod.outlook.com (2603:10b6:a03:211::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.46; Mon, 12 Jun 2023 04:35:34 +0000 Received: from DM6NAM11FT093.eop-nam11.prod.protection.outlook.com (2603:10b6:5:3b6:cafe::2c) by DS7PR03CA0189.outlook.office365.com (2603:10b6:5:3b6::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:35:34 +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 DM6NAM11FT093.mail.protection.outlook.com (10.13.172.235) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.29 via Frontend Transport; Mon, 12 Jun 2023 04:35:33 +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.23; Sun, 11 Jun 2023 23:35:32 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 23/51] KVM: SEV: Select CONFIG_KVM_PROTECTED_VM when CONFIG_KVM_AMD_SEV=y Date: Sun, 11 Jun 2023 23:25:31 -0500 Message-ID: <20230612042559.375660-24-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: DM6NAM11FT093:EE_|BY5PR12MB4225:EE_ X-MS-Office365-Filtering-Correlation-Id: a2b78bd2-671e-412f-81fb-08db6afe7639 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: b8s8DOnfa7UpiLqSFpaMNyRbN2WOU6/wyCDBQpLKX4nofNIqEs3XB20cx68Zj1BO1zofxGhbMrfKc9j4cNq0aIQiHt54BWFwdpIxbbjfBDGUoBrviKT7s2YLkb/qhygc6gzEeB/+5FNqZlqaA0sGXOx5siJ1r/E4ZjiYCPrfyuDLMUi8sF5zFBmQtMjOFOZhAnYMUuZDxgCwqf+W8dnRXQbOoIK8SNJVYbMo4JPd7/EAvVf0uy/bA+dHTNg67YBJIsH5cJTvSqlb3aHLprdOmAH+OIVCx0pyirRYiJBUSv/2BJYmnnZB/RyCASEtJjAGPDCBr6phfLbTU/96js/urRf5xlERlnOoeeZIUBY3IkZPfRdP7GCWh1qnZjS/AH9Rxk6ewM2uTvLybMf33YaYTnQ8hzPX8mSVGGZrgVn6rVLbyEhG2RSi9ID0IM0hvxWAibqcRf7zXjeRoHZsTHCNR7e8fbBhdelLzSxWtMoSBnkN9gomjPfvtqhFYffX9wnw6mmA764kRX4ZlCs4BJdJZHGb6a6Nsqmcfhc9B3VC7fE0FJ0jPCr+ivUMnJJGsS4Do6zM2IqDExMnEP9NTkj9nKjixW+/6M2rIxdytPcmXY7KmLuJVcAh/hxg/Y8dfcAQDXLmoKAuZXQcizKx73IeCgndkq7yp8M7TWi3RS0I3wfSQTd+gCvBrReqOlkKaVG2a9YfZNmOp8G9EqbhmFO1RI6sKf9S0qzCRhnPuAWnlfZLQQxESkHccDbPnrhlByqIVFjETccXriNr6bHmcZcugA== 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:(13230028)(4636009)(376002)(39850400004)(396003)(346002)(136003)(451199021)(36840700001)(40470700004)(46966006)(40460700003)(478600001)(36860700001)(47076005)(16526019)(186003)(1076003)(26005)(2616005)(426003)(336012)(36756003)(81166007)(82310400005)(86362001)(356005)(82740400003)(40480700001)(6916009)(4326008)(70586007)(70206006)(41300700001)(316002)(44832011)(7406005)(7416002)(4744005)(2906002)(8676002)(8936002)(54906003)(5660300002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:35:33.9993 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a2b78bd2-671e-412f-81fb-08db6afe7639 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: DM6NAM11FT093.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4225 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 36768C000A X-Stat-Signature: ruceet9hp5aabnp8bb4niijcoy1i58r7 X-Rspam-User: X-HE-Tag: 1686544537-258773 X-HE-Meta: U2FsdGVkX1/3jVwi2i4hwJqijqgflY68JUQ6Vgf9LN4u8P01i5tMTmd9oVg+gnfEaP080yX+OsRhG7/IX5K1sUjTHzAXoaovWmszpH6Kd/E43jyeDMzI4CxE9nQJNQQqCPMJobjgt66/413PHAt78gAN6xMtilm4ZMNAxSB3oEQv2DaIQa049rpUh6lR2vfzeWNGdAoZZQlVMLiuKl0Qa5fo8wH28C33eImFV8eTQ2oA8d5KLEgdYTXhccgcuefBPcMTATcnjVV3b9e/zD64J71ax+xCqav62ZWuhfmepueOyVwHBWS7C4zj+OBkEqLZBx6LZGiPKHtdU9meaWmE3q6wIXchhyzdDtNzoyGuyQQnueVVrp2B1jb3DtHzbUdPHytle8iiN4PjNwVN1RbYThZGLLT4E+1tfwS4uUBoJbmY2xVFYeJO191XlAeZfi7JGWyxvKXM2tEaN+aiIvnFtj9tL8oTWvBK3RUvnRCBFOWGWWjmN7kG/Y0JuZyS4dv7kczmKi3136QNHceezrkCA1S09C1M2CRaOqFPW30G6XRxLpaxFeWzE8UqXNfMVgWmsc64Gtz3LiKSu63u3pMco+c2TUChyTaA+c4coVkBY1NDN8Sh29TEPxS+ZBTykBSDtLGtHt3ybRK5kM9HY/mjuJRImR9DzAVGWW/vFe3kN7i5ewior3zdUGQLSZnHkB78eH9OjSD7Re2osqYPrzU+Y/VhsT40zuZ2TO2VtyOuHJrA4Fy8EYMKNbgiBaM4VbbDoSNNkWfFZdpiO4UO0jVH96aAZHmx9NAs8ReJ6NOWZwd9pTgoawBeM0/5s2mOVu+nQyAvL9LNq5gdtRBT0x007lyZLjYLUs8ysJ2Lv13ipWWDgBHR8O2/WFH9gRKzUq9jURab/3o6Vi52bGn7cTS+nE3vZ2zRGaLBXH2FQUD8RN5fkOlducXMPRd+eomEbpWWpNaxmP3J2E2HajgIN8d ZZaA6IcD gD0hLHnJU7SyJcUwWAnrDMBeq0dRRCktn0z8itMrvRq0X8eydn8+TwupMnZ8GS/hQ58YtcZnGZ3SvxOf5FeDwKQnBMj9yiLtKrzBdvBmH6qtn1Q0h4Zl2FL3t5xtjTX5QAsQV/7IMT6F5DDx1qqCPZIIeAAdXo37xe/WieU5br/xTfWsL1+rv2AILIAkQANcRDiEw7MEPn3+R9P55jkaMv1sOgEqMQBW3MoM2V2Fme8sEmdevENQUrvRS63pnvqpnogbvy35UvgWwZs7GE6qHlP9Fj7IYNS2CGO+EJVMVSPtUkyxjVjBhcvHBQgvsFnb1kxZie2F/sTtpbf+CWPgvZl+kGbDwme+ml71e50AGIqPIDjGG9siafaVOs9Hlxk/tOGSv X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: AMD SEV relies on the restricted/protected memory support to run guests in some cases (such as SEV lazy-pinning), so make sure to enable that support with the CONFIG_KVM_PROTECTED_VM build option. Signed-off-by: Michael Roth --- arch/x86/kvm/Kconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig index 718010600956..638679a4e5dc 100644 --- a/arch/x86/kvm/Kconfig +++ b/arch/x86/kvm/Kconfig @@ -126,6 +126,7 @@ config KVM_AMD_SEV bool "AMD Secure Encrypted Virtualization (SEV) support" depends on KVM_AMD && X86_64 depends on CRYPTO_DEV_SP_PSP && !(KVM_AMD=y && CRYPTO_DEV_CCP_DD=m) + select KVM_PROTECTED_VM help Provides support for launching Encrypted VMs (SEV) and Encrypted VMs with Encrypted State (SEV-ES) on AMD processors. From patchwork Mon Jun 12 04:25:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275499 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 3FFA6C7EE23 for ; Mon, 12 Jun 2023 04:36:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CE4186B0075; Mon, 12 Jun 2023 00:36:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C93708E0003; Mon, 12 Jun 2023 00:36:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B34D88E0002; Mon, 12 Jun 2023 00:36:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id A635F6B0075 for ; Mon, 12 Jun 2023 00:36:05 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 7E78CA025C for ; Mon, 12 Jun 2023 04:36:05 +0000 (UTC) X-FDA: 80892833490.26.F51365B Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2080.outbound.protection.outlook.com [40.107.237.80]) by imf24.hostedemail.com (Postfix) with ESMTP id 7BC88180003 for ; Mon, 12 Jun 2023 04:36:02 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=vaLMD9Uz; spf=pass (imf24.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.80 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=1686544562; 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=9nJ5zpVhl4br77hne2iYfznSmBLEzrcXtOigBe9hqVM=; b=a06HN6cQFnPPjfwvh3N3MdSaMeCmWGhFCMASzdTH+2ECPT9kHEhGzXbD+gInBkm+SwRu9B DX61+sZfgRbocM/8GO3qc6Zvnn35Zi3JhVpmUm8LHaO1lNu5P/IYtT4bWzg7R7PXRLzmtP kK966aSl/8X1i0poYR3SePsJTKjr/V0= ARC-Authentication-Results: i=2; imf24.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=vaLMD9Uz; spf=pass (imf24.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.80 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=1686544562; a=rsa-sha256; cv=pass; b=t6dPhLYjELZ/AYkbrPlJYf6uYEomH8RyNmMO3irZTO8PUd58yq5mxMV1ksoKtc1zbOteOT aGCAJAsqFUDq54VJjggs6Q2SRjMuco4SDQ1eDaauUWNWU7BEpZpXrJ37A77Cj88prRcFU3 GMTokJE8Hclsi7Vdf7rZ7vHBzWZRlcQ= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PzBuvqVrVgx5QaX4z/cLqNIN5Ug9MJOjvMT5LYhqtgnkvCfNgbaCnmHuwkJMiO6Q+z00ck0FpHcHEgIQ0AIF0bdEpbegyi7AIwU6NPL/sOXZankkiExX7xE7GKKCSbzXpBG9VEujVxti6mk9fzdf8krjYVsSJzxkEkhYFcdFQIJP5J7FFd6QjJiVCWzzftji7ChlVowuTXBt1OPnl+SzNwQjdRdOIikfuLbZhf1zbRlhevdXIIH54LucyH+tHiCg1NlF3BZA7KXktw2PihvAVOo7cq+Yl0u8t/TVTKs+7rX/8RqKr/AjdVp7e6e2+QdhxoHhFZX8Zfnuy1oXktacvw== 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=9nJ5zpVhl4br77hne2iYfznSmBLEzrcXtOigBe9hqVM=; b=M7h+WJ5cI/EXV94N42vs2Bu2lmBv4mofSQjxy0h6GxKRCo7aNPbWgYS3eJg9EG+Z2TK9JT7zkTMhi7y/AeGEAJr5yErI38y4xRptcwN44OcIfiZu6aNp8mlKbrbTrRO26WVKSnFhG2RkCTbEVJ9JLQ3RAQOAnxbxFW8eu7PZmHEZHfz2b4lqY3djia4GLdlqoenLNlLgMvepIe+/1UGXxchi8qkBmHXPXtUm0+rm+yr8RkrfY6B/bbBWX5Mmxez37H22jNlVZVwI2/8Lw3pDE7dgOQEW1h5/JWZG2UhHfBoLqi35SzsmJMhGvK8vi2WCUt5elVG+fAs2jhuUyJIkBg== 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 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=9nJ5zpVhl4br77hne2iYfznSmBLEzrcXtOigBe9hqVM=; b=vaLMD9UzS4ZbAyWuRaAwriD9K/MuqNrWCZbgruqca8ZLVdnf66g/sliaPoSoun9qGYJqMgIdkbmIYUfTiU7yypV3PJfxXrb5h+PWwgvDLeIOIwhl6nJTMPMvNvAP9gMAsquV6niZdyQBpjrh4sfvXoYDtpoUy/ewoNOl9doKteM= Received: from DM6PR07CA0097.namprd07.prod.outlook.com (2603:10b6:5:337::30) by CH0PR12MB5074.namprd12.prod.outlook.com (2603:10b6:610:e1::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.46; Mon, 12 Jun 2023 04:35:58 +0000 Received: from DM6NAM11FT010.eop-nam11.prod.protection.outlook.com (2603:10b6:5:337:cafe::b4) by DM6PR07CA0097.outlook.office365.com (2603:10b6:5:337::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:35:58 +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 DM6NAM11FT010.mail.protection.outlook.com (10.13.172.222) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.31 via Frontend Transport; Mon, 12 Jun 2023 04:35:58 +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.23; Sun, 11 Jun 2023 23:35:57 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 24/51] KVM: SVM: Add support to handle AP reset MSR protocol Date: Sun, 11 Jun 2023 23:25:32 -0500 Message-ID: <20230612042559.375660-25-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: DM6NAM11FT010:EE_|CH0PR12MB5074:EE_ X-MS-Office365-Filtering-Correlation-Id: e5920512-a81a-4d9c-1b6c-08db6afe84bb X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: rqK7AFtqnNuXhApJzdERleiUko9HJRWeIj+Stb0wE4TiiBbKMVb8Mk7dJuABz/IDCwvAkfUTMVtMHLSpmcmGkNTi9TyJL0L2xmxMho/ie9B/W6wTXdzGuDW9ujcfhwBgsW+nSMISEukZlRYwsI0zRpAkLnD8/+CLZE022YmLnUBcu6c0LfUgGpdxeIhDFM5GomyCEJnEn6oQXOsPAGB7in8U01EMLOxvQ6FGQzjdC1IQrRlpT8JNXnS5YfFFGbb/o6DdZVez+8lmuQ/k7ueR9UO+lTYOWMct2EXvGLQN3M9LU44kZFKrow5UginpBfKr5/X8wDZtctBrlNeVLqyUqpJuiUjeWCXzsqlrQbLiXZ1agGcXwwJPG+/NWfPi1VpcoZCK4BS+3m+693tLWar/xtqd+5P0a6tHcxKdltUYp5rQN+KkzAtAfuQfl7cshDvCOZFYJImzDx3tYnno+OAv9rR1CIAZXySRCrxJ8/x2/857ztKvLVkQ6+W5sXysGGWY2WFqVF5XsoEQG3a78bZz8/npOqHfWSm+j2buSHjNY9vw33Xh3oTZE8BlaTBlif1tiaprL39l+ZndeouUupitE+a407rmpQq6HJt+BmBha4Put+H2UqLXog4p5x7s+SQJ+qO6wCVTg3amPcOo5gNvKQHpS/I9PbR/PaqVCv15rgR+dGDcpHyAcypY5QjX1Vs5HhNvwQl3O7xf8GqXdB4eQQkzIr0mY2kthNuRQR053c4GNvYpjoAofU5FVNlSL2k1gPwbJ5sTIyOhmJmcOxzK2A== 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:(13230028)(4636009)(136003)(346002)(39860400002)(396003)(376002)(451199021)(36840700001)(46966006)(40470700004)(426003)(336012)(2616005)(47076005)(83380400001)(36860700001)(356005)(82740400003)(40480700001)(86362001)(82310400005)(81166007)(36756003)(40460700003)(478600001)(54906003)(44832011)(4326008)(6666004)(8936002)(8676002)(2906002)(5660300002)(7416002)(7406005)(70206006)(70586007)(6916009)(41300700001)(316002)(16526019)(186003)(1076003)(26005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:35:58.3211 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e5920512-a81a-4d9c-1b6c-08db6afe84bb 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: DM6NAM11FT010.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5074 X-Rspamd-Queue-Id: 7BC88180003 X-Rspam-User: X-Stat-Signature: dr3azymww8pwgfqjyf1cjqk4a8sj8mx3 X-Rspamd-Server: rspam01 X-HE-Tag: 1686544562-130724 X-HE-Meta: U2FsdGVkX18GDppry+pTvlfDyYb8r3iad60k+zXmQXS5IP81UvkDQInQUsj9acqXLT+6azjb9/3fqg9iqTdfMnf0SMXqdW1FVrpoybr8jBw+5glOIqkOksn5UnHjDiKfUS89IOHc0X0B/kr3nxRAeMc2zN0L1senZzbETGQmMuL1W9Y3V6Om7XvS+oXaaGd1lx+VZFs2tMFKKjnaUNox75JCivpadXRRxQU2EciuxyBoSpg/NxC6EXopmpzE87faiXqSctZVJPYA4K2q5Kq55yAAHs3uhoqdDz3vpOGxwqUxRAfa0YUncbQFy+z6YLwHSh7RsrIIVThegBkFbBVaZvQsukHNqrhvmj8FST1CvnQR7bIvwI7nKNqxQohyoz7SIUiekJAYfMRhphYVPx3bVTWByLzrmHJm4bNQQVOjPfJ/RvHRSazh4vpoRU/Y17KnZTvXSFES7F0kPTsA9HdfrcHFD4i0wwO59cLFwnScQF61mp57GSAoBIfFl3pw91jYeNyvSChnhuH6Y64Zh9yanlsVc4HogbP7IW+VEuoCTHi1XNfJhRNGEjx0fhErujVThTPxWyab+4kddpjufLS+77Vu7MtMvFkbIfxdI6twf9173q8p8YyJL43V6YyKn8Rzjq59ZYVmAi6UxqAjlZu5qYevivXV0WH6jd0VG4qKKxak6UZNRNe0FZsVl0hYp3FYtF91WMW+IhoaXLIWOzHGI9F0SXI2CdmZYY3RAjgUx6ZSpT9y7VwFjsJ0uPBl41l4VvfKuYs1fOH1mHvR5ZvFiglkRQDyRlROHw8ziwNiG/k0YIHupHjn839Seenw2FbbjRvN2+CPyP2p8vjk7rWVK2GVdB0usE4kVNn0r33QjyO3JlK8vgGKSx4klRhim7MkaEFbtJR3KOzPKT1Ui6Jm6PSFmwA7mawHvlz8TBVqr4XAo+SNcHMIX/++6Uc8gBew81fLP+15mxI1P7g3oXh rshJ6ghm KAgr98hglTp+oK6wJnAsEjcMicYUsmk/nnyozcSnqAqHV+8L5NzsA5S4GkMN9Y5pDAloIxXfhFCgyT2n4G6QcY76MEZoZIYdK/DRol2J1gpnE+ml27TujKq2bhQTz1azYA9+RirItXsXLoiQTydXpL4IpFHNOTjPC7HO9s2VVXfgOrgD855V3bCdHQDrXRz8fSMpCFD2VVf8UYC0xWe7BDoDVNfhHoSraoJWKjkf4pROoUST24d+cFPZA7g97zCDvqObz5TfuVpsRRNZ8dC2MCq3ShRfe3S2RHLS5QHBFzAv7bPypf7HXrSmcvA== 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: From: Tom Lendacky Add support for AP Reset Hold being invoked using the GHCB MSR protocol, available in version 2 of the GHCB specification. Signed-off-by: Tom Lendacky Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/include/asm/sev-common.h | 2 ++ arch/x86/kvm/svm/sev.c | 56 ++++++++++++++++++++++++++----- arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index 9eb20b416251..a4fb53fd15d7 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -56,6 +56,8 @@ /* AP Reset Hold */ #define GHCB_MSR_AP_RESET_HOLD_REQ 0x006 #define GHCB_MSR_AP_RESET_HOLD_RESP 0x007 +#define GHCB_MSR_AP_RESET_HOLD_RESULT_POS 12 +#define GHCB_MSR_AP_RESET_HOLD_RESULT_MASK GENMASK_ULL(51, 0) /* GHCB GPA Register */ #define GHCB_MSR_REG_GPA_REQ 0x012 diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index c25aeb550cd9..b88295aa7124 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -58,6 +58,10 @@ module_param_named(sev_es, sev_es_enabled, bool, 0444); #define sev_es_enabled false #endif /* CONFIG_KVM_AMD_SEV */ +#define AP_RESET_HOLD_NONE 0 +#define AP_RESET_HOLD_NAE_EVENT 1 +#define AP_RESET_HOLD_MSR_PROTO 2 + static u8 sev_enc_bit; static DECLARE_RWSEM(sev_deactivate_lock); static DEFINE_MUTEX(sev_bitmap_lock); @@ -2569,6 +2573,9 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) void sev_es_unmap_ghcb(struct vcpu_svm *svm) { + /* Clear any indication that the vCPU is in a type of AP Reset Hold */ + svm->sev_es.ap_reset_hold_type = AP_RESET_HOLD_NONE; + if (!svm->sev_es.ghcb) return; @@ -2781,6 +2788,22 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) GHCB_MSR_INFO_POS); break; } + case GHCB_MSR_AP_RESET_HOLD_REQ: + svm->sev_es.ap_reset_hold_type = AP_RESET_HOLD_MSR_PROTO; + ret = kvm_emulate_ap_reset_hold(&svm->vcpu); + + /* + * Preset the result to a non-SIPI return and then only set + * the result to non-zero when delivering a SIPI. + */ + set_ghcb_msr_bits(svm, 0, + GHCB_MSR_AP_RESET_HOLD_RESULT_MASK, + GHCB_MSR_AP_RESET_HOLD_RESULT_POS); + + set_ghcb_msr_bits(svm, GHCB_MSR_AP_RESET_HOLD_RESP, + GHCB_MSR_INFO_MASK, + GHCB_MSR_INFO_POS); + break; case GHCB_MSR_TERM_REQ: { u64 reason_set, reason_code; @@ -2880,6 +2903,7 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) ret = svm_invoke_exit_handler(vcpu, SVM_EXIT_IRET); break; case SVM_VMGEXIT_AP_HLT_LOOP: + svm->sev_es.ap_reset_hold_type = AP_RESET_HOLD_NAE_EVENT; ret = kvm_emulate_ap_reset_hold(vcpu); break; case SVM_VMGEXIT_AP_JUMP_TABLE: { @@ -3040,13 +3064,29 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) return; } - /* - * Subsequent SIPI: Return from an AP Reset Hold VMGEXIT, where - * the guest will set the CS and RIP. Set SW_EXIT_INFO_2 to a - * non-zero value. - */ - if (!svm->sev_es.ghcb) - return; + /* Subsequent SIPI */ + switch (svm->sev_es.ap_reset_hold_type) { + case AP_RESET_HOLD_NAE_EVENT: + /* + * Return from an AP Reset Hold VMGEXIT, where the guest will + * set the CS and RIP. Set SW_EXIT_INFO_2 to a non-zero value. + */ + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, 1); + break; + case AP_RESET_HOLD_MSR_PROTO: + /* + * Return from an AP Reset Hold VMGEXIT, where the guest will + * set the CS and RIP. Set GHCB data field to a non-zero value. + */ + set_ghcb_msr_bits(svm, 1, + GHCB_MSR_AP_RESET_HOLD_RESULT_MASK, + GHCB_MSR_AP_RESET_HOLD_RESULT_POS); - ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, 1); + set_ghcb_msr_bits(svm, GHCB_MSR_AP_RESET_HOLD_RESP, + GHCB_MSR_INFO_MASK, + GHCB_MSR_INFO_POS); + break; + default: + break; + } } diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index f44751dd8d5d..50be41fa16a0 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -192,6 +192,7 @@ struct vcpu_sev_es_state { struct ghcb *ghcb; struct kvm_host_map ghcb_map; bool received_first_sipi; + unsigned int ap_reset_hold_type; /* SEV-ES scratch area support */ void *ghcb_sa; From patchwork Mon Jun 12 04:25:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275500 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 146ECC7EE23 for ; Mon, 12 Jun 2023 04:36:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9FCEF6B0074; Mon, 12 Jun 2023 00:36:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9AD708E0003; Mon, 12 Jun 2023 00:36:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 84E268E0002; Mon, 12 Jun 2023 00:36:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 773C16B0074 for ; Mon, 12 Jun 2023 00:36:31 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 53775A025A for ; Mon, 12 Jun 2023 04:36:31 +0000 (UTC) X-FDA: 80892834582.09.65F982F Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2078.outbound.protection.outlook.com [40.107.243.78]) by imf16.hostedemail.com (Postfix) with ESMTP id 60F9C180010 for ; Mon, 12 Jun 2023 04:36:28 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=CCLbG9z3; spf=pass (imf16.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.243.78 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=1686544588; 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=8zzsL1kp415Jf3ZR08yu8zyJesz2ZChIlRl31hCycZw=; b=2iDK9mTTeQKqSIwTn1AkxstaNlzYacXdZPyU7aSdUhHEX+HBmLcMlp2NTt07UC1asyQpWM uESLSzmW26zleHeG+Vch25DvvoJSJTOKhM8WtoMuJOvJ56arigt1cFx8zTVVI6U4943HOC ovaGBwyxH0cqZMqQrgSXR3GD69a8lRc= ARC-Authentication-Results: i=2; imf16.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=CCLbG9z3; spf=pass (imf16.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.243.78 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=1686544588; a=rsa-sha256; cv=pass; b=XObIIcY3mvUIdmBVzBcZvtpWyMdedJg3GKK8+i9pTDC9UoWuGeqEWgy0IVc7xxD6v4JDrF bsCzB+NsrVlbTIDfm0v+X9Hg1+WevGn/NyxYa2ucdAnhMVIS5I9tSXgXNl9zjxd60m7eUI rP/ta0JLCiLhdDqI2ug6kPytBnTgmCg= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aahd68F4z6eqQpVYMaVh/MPQMKXnZj26w881Lwf7SaB75uRx10IY8xu3WA5/FEGSw9ZujGD4lexTM3YT6Ojjo/XSpcLXPTNPMFJporWCTM9jHtNg7/IjCs27kcja0/hHokN0XwjoOvRXeNYldl9L0s/zRpv8857Zw52rDaBevtXqiuh3spAbxHMhIctdjwKbAWQaOMl7Sc7Bmm0keKsM9hX8/k84QgiweANvAJlp3SY9vfIVbeuCrPFm3Qlrz8Pdk5P8kfc7dcjlrPzQPvi1uXu7xG/zDVR3Ov6andOEnMGIU2Ol/vA0Hyub9m/yvktqYVNI/5vVpxlxTlJ0HDoyBg== 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=8zzsL1kp415Jf3ZR08yu8zyJesz2ZChIlRl31hCycZw=; b=a/lPMdcbBRzc4VqfQFS20DGcFuOLpP0uSeDSb1fMhhgprQqbTc28iCpq4CBbas98UmbachZCh67xu9V8IKbrUqO1/vhfl/fslswGCr2XDOri5+TP+BK0gDPB/g6bQBXa7ijEa2Lp2hsKYcsVjuCTixTpxoIY4KawGs72E2FBsilKTxmFxS0eUwLSoXjyGe9hMgb3qWunMpjys7dpx6cZ9NYLdeeaoocaDQUsnwHf2EjzUbivsDVxNhLuIyZU4LQ9umnU0wAEFsSL6VpbaVpHcvo/9r8aMY0gIl5k27FYakIBGc/hH5+/oR1G6HgkpXn9Y06X3bTHWEijmO1IhXiGZw== 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 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=8zzsL1kp415Jf3ZR08yu8zyJesz2ZChIlRl31hCycZw=; b=CCLbG9z3lgeW6i036KK2b+Gr/4qgTdYKCS/3bTcpPclaYrO+KVyAzusr2saz/EMG+PX0z5wfCUzKMU27fG72t9NtvLcyC1ozo7gVJ53Dbi8wGYbN1AK+/qL7ZUfKscgQP7MRWjdYpluIENvxp9p0qETLleywb4QY4EakS72cH1Q= Received: from DM6PR06CA0031.namprd06.prod.outlook.com (2603:10b6:5:120::44) by SJ2PR12MB8012.namprd12.prod.outlook.com (2603:10b6:a03:4c7::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.32; Mon, 12 Jun 2023 04:36:21 +0000 Received: from DM6NAM11FT048.eop-nam11.prod.protection.outlook.com (2603:10b6:5:120:cafe::3f) by DM6PR06CA0031.outlook.office365.com (2603:10b6:5:120::44) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:36:20 +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 DM6NAM11FT048.mail.protection.outlook.com (10.13.173.114) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:36:20 +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.23; Sun, 11 Jun 2023 23:36:19 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 25/51] KVM: SVM: Add GHCB handling for Hypervisor Feature Support requests Date: Sun, 11 Jun 2023 23:25:33 -0500 Message-ID: <20230612042559.375660-26-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: DM6NAM11FT048:EE_|SJ2PR12MB8012:EE_ X-MS-Office365-Filtering-Correlation-Id: 39aef988-d67f-4a0e-fab2-08db6afe9227 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: iBTBvJS9Nb8tlWWoAQnxaDXixIBqez+9IO+HO5ryNUfCBF2WURY5vMhy9JgbF/23bcMk0AfTwEW88A5S5sbysRN4IovXi4gO2GdM15lvoIwI1mbNeA24a728lUOyXzsV+hHotS2g4nJiY1UO/f/0JLabwzp1VmBipz59JA/aoWqJadTdEwoLuriBsrEtq1ZvDNMs+nRAduPQOKS1URtwaqAJiE3ejswPf/D+jPEpsxiDSwNL6uqaPuZNQcJgN9rBgc4nuEwmu1fuAOuyjWTmHL4vSzHlWRkhom56pfQV6QNtahHAqrxb6clq0lXwipp1JPVhPccMw6WrNH1Rx5CQCNjEzxRG80m6cugGj3ayzFTe3nPAXZil3+LmPuYJoxF1aQSWWQ+MdsSp8BskFfOSpLhYp5dOqdO9xqgkFbS2eXt1xzToWFta3hV9A3E+oVhsOyAhThk2iJhBrBVtjILQZ2uX0vvlVU/bq734/b7QKSkxh6kJ0bQ01Up2iDEfI6r0KtrICrgKKWLlhFc+rUzoy91X+pw+gfMc18nqL5gTnDQus3toKW+DXjcFPTS3CVAmCcu4duTUINzchqkxDLuNhh5zHlQomp8d4/RjswOnDbn3jVc4RFmVOOs7Vdi2qjjCSCsMirKNCjW42ABpNrgScgB4oaBEjYFCoroDgPB2Byvh5uLL1mVrpfq0ZEQkmJfxLmBV9DQDrzNjiDo3hhdGB58GW+kZF1F2JreCIQtuc3ITPvIXLzLR9uCrWaKHZbBHVhwoF5Gg/XK/E1vABfvmBg== 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:(13230028)(4636009)(346002)(396003)(376002)(39860400002)(136003)(451199021)(40470700004)(46966006)(36840700001)(316002)(40480700001)(54906003)(36860700001)(40460700003)(82740400003)(356005)(478600001)(81166007)(70586007)(41300700001)(8676002)(8936002)(70206006)(2616005)(4326008)(16526019)(426003)(186003)(336012)(47076005)(6666004)(26005)(83380400001)(1076003)(82310400005)(7406005)(86362001)(7416002)(5660300002)(44832011)(2906002)(6916009)(36756003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:36:20.8606 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 39aef988-d67f-4a0e-fab2-08db6afe9227 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: DM6NAM11FT048.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB8012 X-Rspamd-Queue-Id: 60F9C180010 X-Rspam-User: X-Stat-Signature: uaicx4imwxrissqtp43rpd6u3e99dqbc X-Rspamd-Server: rspam01 X-HE-Tag: 1686544588-687062 X-HE-Meta: U2FsdGVkX1++BWsfSfX4DdBKdIbtZ5Jvvvmqourh4MRuoEbaDaEJha+GZDgMRez85khIA2+1E7ozTjk89tK3y+VDUsdJ8wYKuS7nbSj8zVcuGqZkixhCmJGn2EGJzmlJDT6MBUpeosNzVf8zZzQD9j4GuZxlmJ09AcNw6ZEsOcf+dM+PI4NLFkE3rSSQMvvxRCzrWBeM90bYU8h+6gR4CcRG+BRZ0iMoJFAdlp1SLNdfNCY7tpG/okcQWpva57pwVhjDiSV6iDW2Z/Zz8+nmVnD7gB6ZO2OLLVeuxI2BOsEpwp+Yr7yuUYRN/JxeVKXC6D8laHKlCQHCYaUQ5LRKdjB0izag+Q5UVTcC8NGLXsh9A53CHbMyuPBxd8qPPK5k8S7tA6HmGKJR6vLc/RoeNiiqZj7Fcf+KktlR2lKNe+a1lA38p9Y/DZVnHDOKrs5584vCVgD0el0lOU7WEb3wpaEKXdvq8wvyIOYCxHejvQhLJde53eNoum3o6dhLML3Pg7MRrkmX1UmKtsFmg/0tVFqxL2+84RGZNORRbs+kYJ/VKiQ6wWYTEP4746d9hOdzA3OhUaGMthVUG3q3gkqqS3a7sK9a/TKg+PvZHHtfOyB+7I6gesaB2tGO+8ygdTScXwLc/kTLFtTYgieAaGgJgABpIEedge4VqMYMtTwofPvvYR/DwwZ/POooOwyp8ITcZXUq8JYmQRK0la0v9c8te65DX3IFBeTnoh4oyGQGL6Dhe/WM/Q3/7MnnCq1Gerj+u9nLi6bBP7xO9IGAuh99L7NUcHKCfb0Z77Y3psvclHliIvSQlkT6YDAqOG5DJBtQ7fVJ1M4rAfTVow9CFL0MYVTkDoOWCNeCI07A/xNf14krv9NTw8LvDR85ALf8W4hWkNJl/CpHviAUSWdTprdNRwDZ5UoUCQ+Z18iouewcMIhB0X1iGkThFsFweGfIpIUF/EhiDRzeAqX/RfHp1j2 b5EZ6XvJ SeHzuN5lCRVmT8NBSFZBKYTb4b8Icj22265BDGY99dSkw76tNGKan55xq+gQR2YrfXU0SlxxWmd9xEnQYeMnawYjVXbNrsBCQfv01WrfufrleGbU2DCA2+5EbBf4W2DHQ0JXTn+E9yVtI9/Hg3QdhgUn4d3fpPVzTVTWeAGk03Ieh5QbYCfoxK1Z9sT5WWuxlwp1I80YR+40JGmznbcpl56VNmVLSGD0fG+x87io/E8FRd0NeGBQ7uikYh113raq3Vhn7GlfxsTScE1tkWf+IExM2XBVmqAKpb97imnkmRLzJabf9Ze6BMN+IID6xkB+5WMCv0kpO1OIw8KjOae/C63U0SQ== 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: From: Brijesh Singh Version 2 of the GHCB specification introduced advertisement of features that are supported by the Hypervisor. Now that KVM supports version 2 of the GHCB specification, bump the maximum supported protocol version. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/include/asm/sev-common.h | 2 ++ arch/x86/kvm/svm/sev.c | 14 ++++++++++++++ arch/x86/kvm/svm/svm.h | 3 ++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index a4fb53fd15d7..aaea4afcda98 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -101,6 +101,8 @@ enum psc_op { /* GHCB Hypervisor Feature Request/Response */ #define GHCB_MSR_HV_FT_REQ 0x080 #define GHCB_MSR_HV_FT_RESP 0x081 +#define GHCB_MSR_HV_FT_POS 12 +#define GHCB_MSR_HV_FT_MASK GENMASK_ULL(51, 0) #define GHCB_MSR_HV_FT_RESP_VAL(v) \ /* GHCBData[63:12] */ \ (((u64)(v) & GENMASK_ULL(63, 12)) >> 12) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index b88295aa7124..2bceb0060880 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2538,6 +2538,7 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) case SVM_VMGEXIT_AP_HLT_LOOP: case SVM_VMGEXIT_AP_JUMP_TABLE: case SVM_VMGEXIT_UNSUPPORTED_EVENT: + case SVM_VMGEXIT_HV_FEATURES: break; default: reason = GHCB_ERR_INVALID_EVENT; @@ -2804,6 +2805,13 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) GHCB_MSR_INFO_MASK, GHCB_MSR_INFO_POS); break; + case GHCB_MSR_HV_FT_REQ: { + set_ghcb_msr_bits(svm, GHCB_HV_FT_SUPPORTED, + GHCB_MSR_HV_FT_MASK, GHCB_MSR_HV_FT_POS); + set_ghcb_msr_bits(svm, GHCB_MSR_HV_FT_RESP, + GHCB_MSR_INFO_MASK, GHCB_MSR_INFO_POS); + break; + } case GHCB_MSR_TERM_REQ: { u64 reason_set, reason_code; @@ -2928,6 +2936,12 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) ret = 1; break; } + case SVM_VMGEXIT_HV_FEATURES: { + ghcb_set_sw_exit_info_2(ghcb, GHCB_HV_FT_SUPPORTED); + + ret = 1; + break; + } case SVM_VMGEXIT_UNSUPPORTED_EVENT: vcpu_unimpl(vcpu, "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n", diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 50be41fa16a0..1ab117daebd9 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -711,9 +711,10 @@ void avic_refresh_virtual_apic_mode(struct kvm_vcpu *vcpu); /* sev.c */ -#define GHCB_VERSION_MAX 1ULL +#define GHCB_VERSION_MAX 2ULL #define GHCB_VERSION_MIN 1ULL +#define GHCB_HV_FT_SUPPORTED GHCB_HV_FT_SNP extern unsigned int max_sev_asid; From patchwork Mon Jun 12 04:25:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275501 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 5972CC7EE23 for ; Mon, 12 Jun 2023 04:37:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F07756B0072; Mon, 12 Jun 2023 00:37:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EB6528E0003; Mon, 12 Jun 2023 00:37:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D571D8E0002; Mon, 12 Jun 2023 00:37:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id C82886B0072 for ; Mon, 12 Jun 2023 00:37:10 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 902FE14024E for ; Mon, 12 Jun 2023 04:37:10 +0000 (UTC) X-FDA: 80892836220.13.D28AEFD Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2089.outbound.protection.outlook.com [40.107.102.89]) by imf26.hostedemail.com (Postfix) with ESMTP id A0F3514000B for ; Mon, 12 Jun 2023 04:37:07 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=WQNV7Hua; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.102.89 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686544627; 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=HAG99mlA6jc200AafFpw4ErvSud5hXObZsDWDpLaHt8=; b=nNDEbpZsu7SD+pzJb3OHBNIObhgcl+SbLxeOvmYpHDT87QB/foHUITIfDX8J0i9dRDT5fn GITuz7nSx57+HVqRDD5TYxlK/Ii+yQ/JwqOqo1hJdGK6V92a39Q4AVXJZ6eaQ1gD655RGH 1PM6I0fN7MmG4wli5X0HjTESlumjAsk= ARC-Authentication-Results: i=2; imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=WQNV7Hua; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.102.89 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686544627; a=rsa-sha256; cv=pass; b=FjXEDbC9nuJvKo16xvuDhgyBmnGg9ew/GKrrXuSxeTu435//wENZCxf/Ql0l+bjXIWd60J +5+lbuc2AhCJMSgrTjq8VVXuFnG3nvFLyQ644Am3W4oQN6bFSNb24r/d1MgJ6yPxKHDSeM 3TQJJ0fGz9c8wNMCFRDh+7mI5lKT+IY= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AnPXLP0mY1IeMJpSHgR3nv6D5WhJcPcVvrpavYsQ9F7g6y1NWdSbJbP5B8PH5BBqMlvJhWIB/+yZYhnyJUle5owBzSc/LK5UqoP5QBE7OpTeGJlXAZbXlslFpciGETcLTh91gdiL9WGU/PO1FYSl3HI6WNun0YPSeSnRzwHcmS/DNCagHi0qP7l+YB7+x0vCRwa3ikiwmf55Qpt8BT5G0W/82yJlahz15j4Xk5WFvPzOCLCamkh10bcT9M1unVdVyN6M/Q4y1XxmNSNXXl0dRrC0X7e4907+BgLDoipkjl2zGOU+l8DbycrOACs2RWIdccg7swdbR7jWgXwXNQgwJg== 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=HAG99mlA6jc200AafFpw4ErvSud5hXObZsDWDpLaHt8=; b=i3blII3YZx3zWIEPgNfJdlH9FuFxY7O2hmnK/6palLgW9uS8Wedo9WO8D421MhX2uyO7CXxCndskqn3rMr8JAycHvQCrTnHpNMJKipjiYHE08ntcP97sTd5wxqQg8TxWFT5spwSObtq1bJn4Tb656X2Ei0TmdZ+rfN4BPjCsEmM8e81c8xR0xStNzo+cjfe4dUqH7d6/ELi34x0lApqhfW0rkQ6GAMhJZRTo57OWPn1v78ifsr/nXt71Eqn12mDH0Ml9vbVTGtGF6TJxWA2DQA5AS4rM70u11PyXLr4RQ1tV57Qw7IZh51JNXt1miUiLwq/kGKjTl9uv2lcVDL6sJw== 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 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=HAG99mlA6jc200AafFpw4ErvSud5hXObZsDWDpLaHt8=; b=WQNV7Hua977WTx8KoyO5Q2uQCtsNqbn4NYF8Ms32YN/G4Mw9MADZwtIo2mJQQxqJDhVDCv+MGWVJfpX1KM2IefFQXPx5JPpOwK5axvcENWdvXBtXRNd79KHEfu9GjrXUpHebQ6CdUU085RO6jidmOkbhjwZ6Z+j55SKUH7QaR3Q= Received: from DM6PR04CA0005.namprd04.prod.outlook.com (2603:10b6:5:334::10) by SJ0PR12MB5674.namprd12.prod.outlook.com (2603:10b6:a03:42c::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.33; Mon, 12 Jun 2023 04:37:04 +0000 Received: from DM6NAM11FT083.eop-nam11.prod.protection.outlook.com (2603:10b6:5:334:cafe::41) by DM6PR04CA0005.outlook.office365.com (2603:10b6:5:334::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:37:04 +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 DM6NAM11FT083.mail.protection.outlook.com (10.13.173.97) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:37:03 +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.23; Sun, 11 Jun 2023 23:37:03 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 26/51] KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe Date: Sun, 11 Jun 2023 23:25:34 -0500 Message-ID: <20230612042559.375660-27-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: DM6NAM11FT083:EE_|SJ0PR12MB5674:EE_ X-MS-Office365-Filtering-Correlation-Id: 923afc12-2499-46bf-bd5a-08db6afeabd7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 5leXYoIZh7AnkZJ9YUvuvDQlcQ73AB41RhQg7fgqFaY/ZFgVZWq3/SWB7aWWydQx5md3WHFh6rFR1bYg7RB06xQinD7mrqnD9mHIHp+RZ2b2LYwZzz8Y2V6R0MWcLmO20210WMF9xQ4hafqmlOlTvhsDk1/hyb8DF1w7Gpo4b3vPY37c5A8zy5xxYmrRm7YA5eLFGeNdzCfykO104SYdvIsD12/n9+C+Sq9UdiZCjB35+yqiLDZYLYfPEPMtImd03i8b8HVoimA9KBtHvFu4dqcaW7Tde6cLswqjHxSNhtD9ezdklPoltv8ilJMoJIxiTCy5TJqNySfnP/KVsFifXoTnTbd3DpLJIhR2m06nibFQTSe+vFqd3ZlD3PgTcWCPJbW4zPS5iRSpaUA8mOBClZcVOXZDZj58SoZKvriWcrpe83ndfoB5avOXvGmWRwSeB5jtOBP9/6MEx5JANbTOUIlTPiG+c3CjP8VrHF0LzdtPBUBzZjXEgBLzgSVVGPodSilHKrvQ0qIXZJCkNKiTjO7Canjt/MVSaQJFOs7n94Z4Ysm/jUh6LS/0F7ITecfK7zmgaW261cfZJnLZ2fe71GUlSTSrYd0QwPQm6eJ+hzllj7nIRXLY/yhZKzcwkSFzRpLLSfAMofC8ZpV+q3RfKMkiP6gjmoljsQU3qCFC1GyiCZMVlQeYeVaTbxrpC80Au4XJp2RCBC1/ap84OJJFX7bDqsB4CSipLqTIBPzIcBVOKL2DZ4oHKiSlajNH57ECidQ4c/QpbgIMu+5JlgV3VQ== 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:(13230028)(4636009)(346002)(39860400002)(376002)(136003)(396003)(451199021)(40470700004)(36840700001)(46966006)(40460700003)(1076003)(26005)(16526019)(40480700001)(186003)(41300700001)(426003)(5660300002)(82310400005)(47076005)(36756003)(36860700001)(7406005)(7416002)(44832011)(70586007)(4326008)(70206006)(8936002)(6666004)(83380400001)(336012)(478600001)(8676002)(2616005)(54906003)(2906002)(86362001)(6916009)(356005)(316002)(81166007)(82740400003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:37:03.9399 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 923afc12-2499-46bf-bd5a-08db6afeabd7 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: DM6NAM11FT083.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB5674 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: A0F3514000B X-Stat-Signature: yotiytsdk6srhe94gfh3ez6rhxhqsyio X-HE-Tag: 1686544627-609465 X-HE-Meta: U2FsdGVkX1+zGsDUu2qiwRJNlyoBgaVrU91cgQZyVpz9aeQw6CCFR88IfbLt/aUhQZWkfKueMnIAxAT6iXnTvRdDN9Wbq5H4D9vdaV/BNKTJjGJFct9m3Ch4bqwILHnVtZB9LxQ/YlSl8boS6ZaUWhd8zlEnfg6AACk8ETdoTRrGrWhaKouj/e4mHKfKXXMDbIqD9keTr3a5UPoez8+io8teuioZ34Vp9o/xj6L4KDOyxxLpx/YkBx5dVBhVaJjl7twbxHeGknuFLgcmnnFt49i2/6mhBaM56tN+kNJIYwDT0vUQN6zKZLkqz5r+sxgF1o/PEn//tTRv5+BlEOHz/ETdj9urN6Q29Ci3+CnaGxxvQxfnrbW8FjhClqFir9xFIcNwmmi5IcVbofbwpEi0WWnzrDASAd6XcbtMXfutFx13+k9EFQMIf6oUp9Kn7j1X9zth+9SX73XP8Uq5UMuA9G2IISObtC6fud4pf0FjJ2bYTHe7FSS6arlkVZqwMAGQo+CMvzlT/vgpI8nc0f2KUFlsmW8lrxPynYq4uS5wKa4jieBKrlOzbaQ6sjGiQt5u/BuXjY5w9XdOzxu7H0fd+wmrbpzA3DtSrLegJ2vytetJodZGcR2YpNr7hfnCmW20ve7Eyfej/Itib0dbpj+Urc4JKoOAbAkHL3/Zetb5wB0C5+EK73rIDRGpHF8mudyVTRT+w2iY4Zjz18dKJG9KZC4S4c9n6GFdHLmPTBin3zloR618woH/ix2yzQ3dz5pad2f9Q/7a/6Nfjahlpn0LAvAjG+/1QDsqptm+oXCttsjUvH4QmAne06fNWmIb9wZkOfx/WX+NIz7PnZglk8FVswtiZymICkW3bacImEr3/ApuX7/2WD2t/mkNUIs4Z3t6qBxKQG3b48jMi/zBuCmy4+KtlCX06mTA1z1c1Ihp7e+PwTyvjvhlJetNcAGWLzBC2l2MstOpOghFrJh5LRI Jw82ocsB 84VgHGXI8JY383dY+x5WmbqgPpXN937aEkIdca0PbUjnV7iKWo09GeEDMVy2qjV0LrHTTp90e0YlBXO02n+CUkIzO8HZQKIBsxcVaRvbyQhAr3JARqrZ2pZfM5N/TPrYfi3nEcoEcJfYNsyzaFLTjLnos6fsS+kpnA2iLvElZmUkD6tigO5mTmPgA5MdJVuatdhdYH2yto2q0BeGMSgbmLJspMyKxD/xxe30GjL5ZQDZUbf3lYYGXtPwsAeut9M2B094n7AJyZu/z16gCCwyIbaV0k35ebU6PZqNDlZ5kMepHaTtVGZAHU2YS31EsCMBY/PxoRUbeUAr145w= 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: From: Brijesh Singh Implement a workaround for an SNP erratum where the CPU will incorrectly signal an RMP violation #PF if a hugepage (2mb or 1gb) collides with the RMP entry of a VMCB, VMSA or AVIC backing page. When SEV-SNP is globally enabled, the CPU marks the VMCB, VMSA, and AVIC backing pages as "in-use" via a reserved bit in the corresponding RMP entry after a successful VMRUN. This is done for _all_ VMs, not just SNP-Active VMs. If the hypervisor accesses an in-use page through a writable translation, the CPU will throw an RMP violation #PF. On early SNP hardware, if an in-use page is 2mb aligned and software accesses any part of the associated 2mb region with a hupage, the CPU will incorrectly treat the entire 2mb region as in-use and signal a spurious RMP violation #PF. The recommended is to not use the hugepage for the VMCB, VMSA or AVIC backing page for similar reasons. Add a generic allocator that will ensure that the page returns is not hugepage (2mb or 1gb) and is safe to be used when SEV-SNP is enabled. Also implement similar handling for the VMCB/VMSA pages of nested guests. Co-developed-by: Marc Orr Signed-off-by: Marc Orr Reported-by: Alper Gun # for nested VMSA case Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra Signed-off-by: Brijesh Singh [mdr: squash in nested guest handling from Ashish] Signed-off-by: Michael Roth --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/lapic.c | 5 ++++- arch/x86/kvm/svm/nested.c | 2 +- arch/x86/kvm/svm/sev.c | 33 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 17 ++++++++++++--- arch/x86/kvm/svm/svm.h | 1 + 7 files changed, 55 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index 48f043de2ec0..28456b497198 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -135,6 +135,7 @@ KVM_X86_OP(vcpu_deliver_sipi_vector) KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons); KVM_X86_OP_OPTIONAL_RET0(gmem_prepare) KVM_X86_OP_OPTIONAL(gmem_invalidate) +KVM_X86_OP_OPTIONAL(alloc_apic_backing_page) #undef KVM_X86_OP #undef KVM_X86_OP_OPTIONAL diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index c26f76641121..8d2bb3ff66a2 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1743,6 +1743,7 @@ struct kvm_x86_ops { int (*gmem_prepare)(struct kvm *kvm, struct kvm_memory_slot *slot, kvm_pfn_t pfn, gfn_t gfn, u8 *max_level); void (*gmem_invalidate)(struct kvm *kvm, kvm_pfn_t start, kvm_pfn_t end); + void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); }; struct kvm_x86_nested_ops { diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index e542cf285b51..94311938651a 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2769,7 +2769,10 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns) vcpu->arch.apic = apic; - apic->regs = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT); + if (kvm_x86_ops.alloc_apic_backing_page) + apic->regs = static_call(kvm_x86_alloc_apic_backing_page)(vcpu); + else + apic->regs = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT); if (!apic->regs) { printk(KERN_ERR "malloc apic regs error for vcpu %x\n", vcpu->vcpu_id); diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 96936ddf1b3c..fb981c8b82c4 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -1185,7 +1185,7 @@ int svm_allocate_nested(struct vcpu_svm *svm) if (svm->nested.initialized) return 0; - vmcb02_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); + vmcb02_page = snp_safe_alloc_page(&svm->vcpu); if (!vmcb02_page) return -ENOMEM; svm->nested.vmcb02.ptr = page_address(vmcb02_page); diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 2bceb0060880..69b57e8f0a7f 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3104,3 +3104,36 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) break; } } + +struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) +{ + unsigned long pfn; + struct page *p; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); + + /* + * Allocate an SNP safe page to workaround the SNP erratum where + * the CPU will incorrectly signal an RMP violation #PF if a + * hugepage (2mb or 1gb) collides with the RMP entry of VMCB, VMSA + * or AVIC backing page. The recommeded workaround is to not use the + * hugepage. + * + * Allocate one extra page, use a page which is not 2mb aligned + * and free the other. + */ + p = alloc_pages(GFP_KERNEL_ACCOUNT | __GFP_ZERO, 1); + if (!p) + return NULL; + + split_page(p, 1); + + pfn = page_to_pfn(p); + if (IS_ALIGNED(pfn, PTRS_PER_PMD)) + __free_page(p++); + else + __free_page(p + 1); + + return p; +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index eb308c9994f9..065167b42f90 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -668,7 +668,7 @@ static int svm_cpu_init(int cpu) int ret = -ENOMEM; memset(sd, 0, sizeof(struct svm_cpu_data)); - sd->save_area = alloc_page(GFP_KERNEL | __GFP_ZERO); + sd->save_area = snp_safe_alloc_page(NULL); if (!sd->save_area) return ret; @@ -1381,7 +1381,7 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu) svm = to_svm(vcpu); err = -ENOMEM; - vmcb01_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); + vmcb01_page = snp_safe_alloc_page(vcpu); if (!vmcb01_page) goto out; @@ -1390,7 +1390,7 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu) * SEV-ES guests require a separate VMSA page used to contain * the encrypted register state of the guest. */ - vmsa_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); + vmsa_page = snp_safe_alloc_page(vcpu); if (!vmsa_page) goto error_free_vmcb_page; @@ -4770,6 +4770,16 @@ static int svm_vm_init(struct kvm *kvm) return 0; } +static void *svm_alloc_apic_backing_page(struct kvm_vcpu *vcpu) +{ + struct page *page = snp_safe_alloc_page(vcpu); + + if (!page) + return NULL; + + return page_address(page); +} + static struct kvm_x86_ops svm_x86_ops __initdata = { .name = KBUILD_MODNAME, @@ -4900,6 +4910,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, .vcpu_get_apicv_inhibit_reasons = avic_vcpu_get_apicv_inhibit_reasons, + .alloc_apic_backing_page = svm_alloc_apic_backing_page, }; /* diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 1ab117daebd9..e45b54e95495 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -741,6 +741,7 @@ void sev_es_vcpu_reset(struct vcpu_svm *svm); void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector); void sev_es_prepare_switch_to_guest(struct sev_es_save_area *hostsa); void sev_es_unmap_ghcb(struct vcpu_svm *svm); +struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu); /* vmenter.S */ From patchwork Mon Jun 12 04:25:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275502 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 470B5C7EE37 for ; Mon, 12 Jun 2023 04:38:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D7F538E0002; Mon, 12 Jun 2023 00:38:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D56716B0074; Mon, 12 Jun 2023 00:38:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C1EA98E0002; Mon, 12 Jun 2023 00:38:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id B4D8E6B0072 for ; Mon, 12 Jun 2023 00:38:01 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 8C4C5AF3B8 for ; Mon, 12 Jun 2023 04:38:01 +0000 (UTC) X-FDA: 80892838362.09.31EE1A8 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2050.outbound.protection.outlook.com [40.107.93.50]) by imf14.hostedemail.com (Postfix) with ESMTP id 84F4F100008 for ; Mon, 12 Jun 2023 04:37:57 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=1g1eaNBG; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf14.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.50 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686544677; 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=pOpw3KzGxdOjwJ/TkrcnFihbFz41wbR17m9Hf+7f9aQ=; b=S0/i5vapRWYki2rYQUhPStBp+e0+7zcrOuYZzqOzLeJWEUH5w9Fe/jT901Fh4fT+xxbsRe 3DAzn2W8SGIyCIJCBq+WdPBZGzRf/0DzAAsK3pkca1/dBX9dswjdJpCG879zlguEr3pDyi hx08gqc7d6ZkICJ+1+q6P3U/C1Jh/vk= ARC-Authentication-Results: i=2; imf14.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=1g1eaNBG; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf14.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.50 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686544677; a=rsa-sha256; cv=pass; b=j4S96OF4ac1aNduOqH612msVdjEZVSMLfga1ln/O2TfHIFMpbp39ZgW6S7tEjB0AtdrI5e dq0p52WnK1rmYUAm/mEP+0ikXzQ4uvNLmoc02Nz/Iz5GrcCcRU90746SgnLZe9qvtUGI4+ IXnwdNjRg44rzzVkCDjSHyepSMTY7+Y= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lfJ4DMH5UeL4MLfR70vtA0ZBekZIPM9Vtivv1eQTZrVm2G1KNpXlh+Hpqbhc3wou4StxEiJFjpJtUCiyLDV0QUHmBlRLpmVHoCxrTkXFF0npzq7lrF8yS7eQG34y3JtYTDXqcBax/L8ji+P2K9JSyASNTmhEbfZ22t29Gpbdqg2xUsCEY7oHIUrV42eAesnTxRTuHKs7TXve8qdb+v3ownwMbE/WqUZqFOgBiG/S6/C09yYF0TbQfzpDgsJ3Otohl4Kmm42I09ourWftwPYGRP9Amvoie0pUZyy2whCQI4Pfxpycp8+3kW08Gy3o+I66j6RQ4CRDN4wAcFiB93+xSA== 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=pOpw3KzGxdOjwJ/TkrcnFihbFz41wbR17m9Hf+7f9aQ=; b=kw9r0XvrmMdG+rFbrGD2NOq4pmQXK0/ozB/oM+UBe2+rg7L/Rp72lRPhL8RSDpSYCVGE9Ps0uWE6sQhlBLSVqr/MG552tp9TryYd4I3ZChIaREQb1NUc7w1zpzHGj/VNUMq/eczCU2iSX97fQKbMG3mWkLMh2fqPw4U+y3tqK0JV1gK1ywlbCVHnhQ7F9U+9y/9Ljs+BcxdtwgViHIYYYSKGShkLTZlKNHlgVOqcVnpyFOVwNnmuxCOq3gVw0hTkzLJ2HYxG2a7gse6VdDhgif66FrUJZU8D2ymSh8zSpL8YmSBkYsxQBPjAZoXyGPl0YJQKBZYIE0Z1/MA39BGFZg== 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 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=pOpw3KzGxdOjwJ/TkrcnFihbFz41wbR17m9Hf+7f9aQ=; b=1g1eaNBGzrRncDD0LAr1f9sRtbUZYIoFWbWXpGADs9AoNjVFcJBfFR0b1Hm8aFS+gny5nHSsjfwdbtvZRJpe6PCv9hXRF+5BOfU0E4z+XLXjN2JDV4hZRmZsIutTjy90e6gu86YVfmQhUp19lokKGD+k8chuoCYVYs1TUhWXAMo= Received: from MW4PR04CA0148.namprd04.prod.outlook.com (2603:10b6:303:84::33) by SJ2PR12MB7823.namprd12.prod.outlook.com (2603:10b6:a03:4c9::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.33; Mon, 12 Jun 2023 04:37:52 +0000 Received: from CO1PEPF000042AC.namprd03.prod.outlook.com (2603:10b6:303:84:cafe::cc) by MW4PR04CA0148.outlook.office365.com (2603:10b6:303:84::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:37:52 +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 CO1PEPF000042AC.mail.protection.outlook.com (10.167.243.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.13 via Frontend Transport; Mon, 12 Jun 2023 04:37:52 +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.23; Sun, 11 Jun 2023 23:37:50 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 27/51] KVM: SVM: Add initial SEV-SNP support Date: Sun, 11 Jun 2023 23:25:35 -0500 Message-ID: <20230612042559.375660-28-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CO1PEPF000042AC:EE_|SJ2PR12MB7823:EE_ X-MS-Office365-Filtering-Correlation-Id: 2a304f1d-13d0-4bf6-408d-08db6afec899 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: uLxOKIpZMWOThwXCbr064xUgH6E6BEXvwurUtUUJE6y8Xyh3Ns+MGwSrTLe2xDOBDlNlNpDhqzQM2ox38G2h45Slq50VWTbYTks1w/1NHzNqG2TId4ITzuON1LozCGV7g5A2eLfx6ybroQKMJFHG58oy4B87yx9k2vvCKl2uVIjuT2x9Qi9xYZccGSm+dfi/1kq6LACuFvjvCF6E5kpf7UwWejtJLvMPV1XHw9Wc9Ee5di9Z7Xb3qbgRuCvJjBbGuiy7JoyQZr2nwgl91Iy7e9Es4unsup6Dx2N8LYk5tXgfVHz0vvSZSVv+/lsYs5G2xPmxvBX3ELbp1q3Kv2oPFBsdyl89zfRBXYvRQZI7qX4sOZk+URGizZzTdIcLeljBipQikoCd62+e92Wx3M3B6Z4lvj4zQgtUsk4YlJFPmvA3iYLr0GvfZITr/p5F2lQRbjiMUjOz1lFvkzb3VKtZi7jTOgQMeMd46PE0L4WlfDo1y5PJznczKj4h/5oiBYW7vdshghfPrxgeH394eKuFiRKMrshDcOvZ1aoEde2LKJByWLUrS+YoQ0OKWQpPfqH/oogsqr5O1XVODSZrh0gdAy4XHID+tzKf+EzMQ4Kfz2EyLeuKdPlgcPL2g6bbrEeKK9q8JQuXRjVY/q7wEY4pcYf1al5kYfIOZJ6nBo6NsKKOrVPJ3PbsDXOTgQLl9b8mQJJYKwx3K8o8a/UviNZ4VKPcKAMA4/Ud+50moDtDSzvlcZrzbYpIaXiDQ/aPUv06KeNlyrtCdWdJ0R5bl589hA== 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:(13230028)(4636009)(39860400002)(376002)(346002)(136003)(396003)(451199021)(36840700001)(40470700004)(46966006)(70206006)(70586007)(5660300002)(8936002)(8676002)(36756003)(4326008)(6666004)(6916009)(54906003)(478600001)(40460700003)(41300700001)(316002)(81166007)(356005)(40480700001)(82740400003)(336012)(426003)(16526019)(26005)(186003)(44832011)(1076003)(47076005)(7416002)(82310400005)(7406005)(2616005)(83380400001)(86362001)(36860700001)(2906002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:37:52.1218 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2a304f1d-13d0-4bf6-408d-08db6afec899 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: CO1PEPF000042AC.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB7823 X-Rspamd-Queue-Id: 84F4F100008 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: dy7z6h5tp1ar8c7prpi7oo1zg9k5hf8e X-HE-Tag: 1686544677-958346 X-HE-Meta: U2FsdGVkX1//Xc8bAEQQ7ZHUUSrevXIMEGM5va9pX/kpjurGZ2x2OWclqz6ZbmmlJ+jWtcav0UunkSpreeZNkLOouMND62TmsyfNnRD0OiI0OxtY6uqynQU3RpjCLIwQXjhtIOOEps1Y+m9enaoGMbSV33+2uTFcF2dnpSAyxx2Q0D6s+i3ouVvn/foB55JXfAJl12xGE5brOFIwS7ICdSDoEcLY2oiLRfPyYbbQC2OpBnDTplCh+W5lSc8OJchQPPqitXoW3nlh4nvV57yTm5qWdzI7/x9V0iG2EgeaXoPg7vpTEDyVOa7WCyDFxUr32j2dyz4sV/VuDwdarbgPWVHHyxoiHvzGXVVhTOJIuNCI9FQ8tuUt58NDBxLU0qCxG7wV5wmM1mdjVygNSkUP7xXHQB+dQqXKjN9gEJNjctuY4z1xMEXEtuAg9fIh3W6UlJJ/R3DKvwR0tvebUZwd1T1K6IbdcjGDutf05Nu6ax3DXAOpa57CwdyBRFTx6zJKT1Ew4Y6WRuS7aIFCEmouJiU8RFYyM5UeSIQk2/PAa6Ht7onMr/QDbZcL3MNHAY/YiBrKP5KniaSNeBGdUGWuTzBEwVUkJTt8JNEzUKa1SrxASlnlxFJWUcK7VQlRr3NFcLIN7JCmEa/KY+yRn+ppFX2tuzDhku+GiMoMBJMhIIpKpSaBW5Owzuz5N/aw2FBAChIJyJ6+1Zm0KpCVwWasbiMmACLi8ope7ZKu2KXuVdz/3Ka4PDj9XwXOldOMdwdyTAA2GUiTD1ck0q09Y77OWgMfsWYOUMavSQ8VVfiP7ciqjfP36UhqkqAGGhqijqBFr330Bz5vcu+c6dQsRVKdfEXZI7X0gIOsIVy1t4/OIRqS+lkU5bJJVK7H9drzSy0EUOXvEtSrG+vX9keocP0gXt3zg2a4wEbjwRDPlr4LKHvHWZO+OqaLOc7lQIszP/lHGmVXxj8cX0KJefg2BcB HE5Br5DW 3vBpilYj6sLtdfeYsYfHp7bkvUGG1HNgppawARqyK1I4Jzjmp+vHEu7t389OxRtXI7SxVQ6jiom0iC7M/u/bWoj+8+Q4WX0qN5ld3rPnpBYQ1LUe94jovXTy9V65ZQQ1cGCj5B2P/nHQvjjCiV/qlzvJI6V2v7CWW+ek4x/uLd53D689ehCCKY+YXGQZRHkxdoykQk5VA3r+meGLjkEdShqPZUBJaf3i7P5JBfEdMJyMzYHihe+NZPKvCr7yGrTG9gVFwdgv5Ihvuk+BsP0u0yCmJtSDIYZXpHTNGMOq125oMTgnS0tjc2vY/kPjlTOrzSWoYaI4coadfmVB2MSkR4WGQWg== 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: From: Brijesh Singh The next generation of SEV is called SEV-SNP (Secure Nested Paging). SEV-SNP builds upon existing SEV and SEV-ES functionality while adding new hardware based security protection. SEV-SNP adds strong memory encryption integrity protection to help prevent malicious hypervisor-based attacks such as data replay, memory re-mapping, and more, to create an isolated execution environment. The SNP feature is added incrementally, the later patches adds a new module parameters that can be used to enabled SEV-SNP in the KVM. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 10 +++++++++- arch/x86/kvm/svm/svm.h | 8 ++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 69b57e8f0a7f..f5fcf6c33583 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -58,6 +58,9 @@ module_param_named(sev_es, sev_es_enabled, bool, 0444); #define sev_es_enabled false #endif /* CONFIG_KVM_AMD_SEV */ +/* enable/disable SEV-SNP support */ +static bool sev_snp_enabled; + #define AP_RESET_HOLD_NONE 0 #define AP_RESET_HOLD_NAE_EVENT 1 #define AP_RESET_HOLD_MSR_PROTO 2 @@ -2169,6 +2172,7 @@ void __init sev_hardware_setup(void) { #ifdef CONFIG_KVM_AMD_SEV unsigned int eax, ebx, ecx, edx, sev_asid_count, sev_es_asid_count; + bool sev_snp_supported = false; bool sev_es_supported = false; bool sev_supported = false; @@ -2248,12 +2252,16 @@ void __init sev_hardware_setup(void) if (misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count)) goto out; - pr_info("SEV-ES supported: %u ASIDs\n", sev_es_asid_count); sev_es_supported = true; + sev_snp_supported = sev_snp_enabled && cpu_feature_enabled(X86_FEATURE_SEV_SNP); + + pr_info("SEV-ES %ssupported: %u ASIDs\n", + sev_snp_supported ? "and SEV-SNP " : "", sev_es_asid_count); out: sev_enabled = sev_supported; sev_es_enabled = sev_es_supported; + sev_snp_enabled = sev_snp_supported; #endif } diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index e45b54e95495..6974d63c84f9 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -77,6 +77,7 @@ enum { struct kvm_sev_info { bool active; /* SEV enabled guest */ bool es_active; /* SEV-ES enabled guest */ + bool snp_active; /* SEV-SNP enabled guest */ unsigned int asid; /* ASID used for this guest */ unsigned int handle; /* SEV firmware handle */ int fd; /* SEV device fd */ @@ -346,6 +347,13 @@ static __always_inline bool sev_es_guest(struct kvm *kvm) #endif } +static __always_inline bool sev_snp_guest(struct kvm *kvm) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + + return sev_es_guest(kvm) && sev->snp_active; +} + static inline void vmcb_mark_all_dirty(struct vmcb *vmcb) { vmcb->control.clean = 0; From patchwork Mon Jun 12 04:25:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275503 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 336EAC7EE25 for ; Mon, 12 Jun 2023 04:38:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C1F6A8E0003; Mon, 12 Jun 2023 00:38:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BCF7B6B0074; Mon, 12 Jun 2023 00:38:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A49588E0003; Mon, 12 Jun 2023 00:38:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 965D26B0072 for ; Mon, 12 Jun 2023 00:38:19 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 6548E120292 for ; Mon, 12 Jun 2023 04:38:19 +0000 (UTC) X-FDA: 80892839118.25.1ABA43A Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2074.outbound.protection.outlook.com [40.107.223.74]) by imf24.hostedemail.com (Postfix) with ESMTP id 76F48180003 for ; Mon, 12 Jun 2023 04:38:16 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=tVvWTGPE; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf24.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.74 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686544696; a=rsa-sha256; cv=pass; b=QtrrkvtaejXxVgGNYmtUWkihQfMyKU8W67bvceKb35pYjy0Eprg3c0IkS6o3HT3NYdt78v Npdzu13QTbiL5xzkbL6lKfPu9n29WnyfHUrk23ZKodD8v/iQDvK1zvhZKT25hg75Aq1VPO Bbc5V4zmpS8yfaVkhd1oEYLmjtbxniY= ARC-Authentication-Results: i=2; imf24.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=tVvWTGPE; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf24.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.74 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686544696; 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=4q6MewkvTsxk63o0PqCssBYpMxPC1G6Szq/4zLuo80A=; b=w4Vm0gSD5zcTNGWR854zXOkbCjfJTQL6cwPYlACLmgcHj3hJ9qVktSb/+JWAbdInlPRDCP MPr/Kgi7l5fip+1oS4/yq5N4xh1WwctEAQK3T8ET8vp1q7lvDIlNyGPrX8+cDNZ4yyMvrR hyK61K+zYxaKfUEyrSjNiWpxbRvK5p4= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YHL5ICaabVRjAtFzcxY053uJmWnjEzsrQBW/k5LAsbQ4j+ITZtst/FTpBzGd+8K7p2QGrOIhaxIR2JxSk/IsMHd3BGISdGs51ZdP0sCBBDvaFh0YckBTFLEmEo9yezdUk4iIBgp5JCXW1Ny4ob2kci1rae5qSPEXuonkFPb0MKgNrBBkc3ro2YMkDOppf+I6YOAE1wG3tO6karPwXfa7useTjdmb0xLgnKHHfr4Kh5xLwORCrPXl4CQIGw0wSxshnedPdQ2Xl9l7SmjevqPqZTpHi4dOWKxuvCUIbGEtH0903IioZbMdDJOS4IhfPVLSsvVDnn3UevNezwRkivt2qw== 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=4q6MewkvTsxk63o0PqCssBYpMxPC1G6Szq/4zLuo80A=; b=cF0+QqqnKsL00Jywi98sTgNIu6kW+9SRx8/MbmocxUL6op/yo1vVhf1DL1ptyR077CmcvigROzJ5l8PkV+LKVHt6pKMcOP6Bj/UiDRPkDMlanj5LDpmmN0mnES6OIPgAxebGk3wyILJuS49qClQ0t5z8VDnQBp0hf++atmiJLM/TpmdsMwSgtrrruUrhqQc3AXjE8L7gchjk8RkA+xO2fwNCxoHiYebS+5wJNd77Rmy+sDjM2SZu3EujOqhahpXt3Vg2Gc6kRRHNIth8VCrzDOX8VKD3Pqd2cCsqwtdEBl5thsuyUAdk1Qqa57FXENsLKCuVvTxBbexB/mz/7BVerw== 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 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=4q6MewkvTsxk63o0PqCssBYpMxPC1G6Szq/4zLuo80A=; b=tVvWTGPEeP659g2BnR94YvY6clV1ysEmh3bpN/H79jqHKFPDacn9UDEPtRbdJ9PyydBoNWoa+ZjMZAmIV01QDTiS9ovlFi+n3bd33ZfozMtUO0wcfbRNMbW/GXEH4ap8MWpoftLv/g5TP2ovb4isnYgh8T8tkukv52LaMaWAu+s= Received: from MW2PR16CA0033.namprd16.prod.outlook.com (2603:10b6:907::46) by DS0PR12MB8344.namprd12.prod.outlook.com (2603:10b6:8:fe::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.33; Mon, 12 Jun 2023 04:38:13 +0000 Received: from CO1PEPF000042A8.namprd03.prod.outlook.com (2603:10b6:907:0:cafe::aa) by MW2PR16CA0033.outlook.office365.com (2603:10b6:907::46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:38:13 +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 CO1PEPF000042A8.mail.protection.outlook.com (10.167.243.37) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:38:13 +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.23; Sun, 11 Jun 2023 23:38:12 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Pavan Kumar Paluri Subject: [PATCH RFC v9 28/51] KVM: SVM: Add KVM_SNP_INIT command Date: Sun, 11 Jun 2023 23:25:36 -0500 Message-ID: <20230612042559.375660-29-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CO1PEPF000042A8:EE_|DS0PR12MB8344:EE_ X-MS-Office365-Filtering-Correlation-Id: b2939f41-0cc9-441f-db32-08db6afed560 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 27Vuipl4yrJpshaqF8PFVYj9+w+eXxN+OWiDN6fAAI6dufeGUf5sziAbtcCvJbyHNdjClkNBk+9/D6Wx6m7x0wrGZe4RK7xvfga9ui0AtCCXdiHz8wa2/MbUx7GKeQfiab9CjtH/1EqY0hagvr3y3nWTMaYBbudGK2pNAXiN6VULUR6YZO1SifyjLMcgt66cDsvaNmaoYgIhwl/5Wi/npYUfM6HeSCfjw9rR3VPyENVLIbKm3Hk04m452uuu3g69CoqH2wO/BvhjnPTJzCO7+FrRlViY8brtGL2VeMgxT6ka0uJSLtOfac7ii8jiNLfNWdXBOfe6ruzAdU2a3cyBvS1L9hufVTzxl7FpV2KqijLqgalR3GnQtLkn7lLAs5nKJc/nmXiqYcEgs9RTUedUHNi3uwoOtDS0cq2qBxmg/vW6+YLyM1gkef5+tCkF5hhebjIeTXWGo6ogvb+XqIn334v6goW2caf8o5jOFoKSkpDfuG3Zgam1gvlRdsH3E5G3C0fJ9CgFixR5KyuzwXDKIw++LN2AqZb5jl3H0EXdkgYV1nF7CXc2Fofy3L48NTNhTS05g3FvSNUE3nzqH6r7q/BTpwppI7wrrXfaM/Uv/BeR0A9ywD4L4n3E5UNsZ+5/Cr8UYIaohfDHFoS4K3AWITvMi3zRCORkFIPqzuNG3nOCe6dPtc98xRkJwHI07u1Ftu1MXmSy5E2ytD+GuuJc0ssZ9w7m1sxEiHLm2Chk3jPfdJgnPNjB+1dkviX1mhUlY5P6uUb8WNG0GwiXfn4z4Q== 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:(13230028)(4636009)(346002)(376002)(396003)(39860400002)(136003)(451199021)(36840700001)(46966006)(40470700004)(5660300002)(7406005)(7416002)(70586007)(8936002)(8676002)(316002)(40480700001)(70206006)(6916009)(4326008)(41300700001)(54906003)(44832011)(2906002)(40460700003)(356005)(82740400003)(26005)(86362001)(81166007)(16526019)(1076003)(186003)(36756003)(36860700001)(336012)(426003)(2616005)(478600001)(82310400005)(47076005)(83380400001)(6666004)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:38:13.5782 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b2939f41-0cc9-441f-db32-08db6afed560 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: CO1PEPF000042A8.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8344 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 76F48180003 X-Stat-Signature: rtmpby9dqq5mrdt63cy4sggmwphk3fy6 X-HE-Tag: 1686544696-383211 X-HE-Meta: U2FsdGVkX1+e/NhvD5YJNIad1WqC7mcRm6QF/dsXxGESrysNwTfmWSpSQ/TqUhB1gDPdWpQjlvYnJgFetAM6Cu3JsRnFVz027nooaQNbXUMQU3Byz98EhYBbyOLEfaisRhFgGozyDbiuOhLdYVsuQxRNPXik02z60MXucB379lTiMVOWdUxwgAQFHI4Tht8lGmYKK0uXR2o+q4y7LjrzFXLwCqaAtno9w98PzkMNrFhzPj+ojpC9yEa4OFkHkJZG9d+ASo2HtY5GKkrFF8eY0FYwHAhb5anF438dUEToWVgbAP/eCiYpknKEviURnLjlgqUSWYZgSXhG9QC2V8Ms06ljBIRWvkXa7yqDleCONG7IwjB4Lr4j0Q+BzPQNbN1SLDinThQAIFiHaX3MRjU9JewYoYNWtVqyINpAXXB4w9TXO/ce+sVxLSsfoH9EA/MStpA7YOc7Kune3GQ3PFOEDPNvXiRIS6rEDQ1K9RNUxmNozR6fULeTVCWe8tG1q6QVlcMqD2GtkIzVVo4108LQXxMys1I/hA39eZLlqgEQvNDZyA1zoxrnEIeFvbof+wGy/y6ddR4SmxmQtGwQ+jOeaXqQMW4TD7NV6yIc6vz/CJ7WaIF5qyGexnM4qPSxVnamrOnVH5qT+crDVsnyUlVlXvYHSPbgBX885UcLxwrKsKguBveXTfZq46rzqYKXMoMvCFQVv3VgErnYUYpobFU+3vEt6zHdIimQQO8zArjY/mGWdMIQOlJ+TnNXte/2FZBCFNOpVz1UMNxNszaY1v7mMQBeuWLrJcwiBmf2DTxagfikEpr9USv6rl5ur/0Pt2OO6DjATuUw8fWtoeuQzUjDuq7GPdtj3n4W3+RKpK2iA1hOXJxigAYXoGO6NNx0QvH/IYrOsrzfUYmZZSxEUqSz2dodCDrJC9cRmzxc1n8Rjiba1KGEKTNKotjS90vmS59QPzPrZ40ravv2MUAjEVi a4K0mwov xraTQaoXapvdW5yLAQCgX6RMu8jlunFzsvY3kGVZIZ2wHvO6XKZSVuXJHU4+n5wFfGeLPKIgGN8vP+F2SUC06PBfQnluVEx1/cShbXq06UgmAsz5Is442VuNE1XUhGuu+65ne9pTABWhfIHseiCwlKXEq2Qv5aycfzBiQGqjNl3YKYHltJYbhnmY8haCi5PIIF4jbontC97upjm/k9ffgzagEZfbIRZkK1vv4GmYf0XDyjM21qFyuPBZdDwwpZBVdrjNm/Frh+x5t1/clW3YaSkDEHj3Jfmxqtp/Y9gzQW9EU+twmRHqiOQVQAyoFmZBTr3pVGSZ4br2G7eucVSYn1WxWHoC8SnoakVwPrCXYE/lFRyk= 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: From: Brijesh Singh The KVM_SNP_INIT command is used by the hypervisor to initialize the SEV-SNP platform context. In a typical workflow, this command should be the first command issued. When creating SEV-SNP guest, the VMM must use this command instead of the KVM_SEV_INIT or KVM_SEV_ES_INIT. The flags value must be zero, it will be extended in future SNP support to communicate the optional features (such as restricted INT injection etc). Co-developed-by: Pavan Kumar Paluri Signed-off-by: Pavan Kumar Paluri Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- .../virt/kvm/x86/amd-memory-encryption.rst | 27 +++++++++++++ arch/x86/include/asm/svm.h | 1 + arch/x86/kvm/svm/sev.c | 39 ++++++++++++++++++- arch/x86/kvm/svm/svm.h | 4 ++ include/uapi/linux/kvm.h | 13 +++++++ 5 files changed, 83 insertions(+), 1 deletion(-) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documentation/virt/kvm/x86/amd-memory-encryption.rst index 487b6328b3e7..1240d28badd6 100644 --- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst @@ -434,6 +434,33 @@ issued by the hypervisor to make the guest ready for execution. Returns: 0 on success, -negative on error +18. KVM_SNP_INIT +---------------- + +The KVM_SNP_INIT command can be used by the hypervisor to initialize SEV-SNP +context. In a typical workflow, this command should be the first command issued. + +Parameters (in/out): struct kvm_snp_init + +Returns: 0 on success, -negative on error + +:: + + struct kvm_snp_init { + __u64 flags; + }; + +The flags bitmap is defined as:: + + /* enable the restricted injection */ + #define KVM_SEV_SNP_RESTRICTED_INJET (1<<0) + + /* enable the restricted injection timer */ + #define KVM_SEV_SNP_RESTRICTED_TIMER_INJET (1<<1) + +If the specified flags is not supported then return -EOPNOTSUPP, and the supported +flags are returned. + References ========== diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index e7c7379d6ac7..ac8edfdd60fa 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -288,6 +288,7 @@ static_assert((X2AVIC_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == X2AVIC_ #define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF) +#define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) struct vmcb_seg { u16 selector; diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index f5fcf6c33583..70e0576a32d0 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -243,6 +243,25 @@ static void sev_unbind_asid(struct kvm *kvm, unsigned int handle) sev_decommission(handle); } +static int verify_snp_init_flags(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_snp_init params; + int ret = 0; + + if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) + return -EFAULT; + + if (params.flags & ~SEV_SNP_SUPPORTED_FLAGS) + ret = -EOPNOTSUPP; + + params.flags = SEV_SNP_SUPPORTED_FLAGS; + + if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, sizeof(params))) + ret = -EFAULT; + + return ret; +} + static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) { struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; @@ -256,12 +275,19 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) return ret; sev->active = true; - sev->es_active = argp->id == KVM_SEV_ES_INIT; + sev->es_active = (argp->id == KVM_SEV_ES_INIT || argp->id == KVM_SEV_SNP_INIT); + sev->snp_active = argp->id == KVM_SEV_SNP_INIT; asid = sev_asid_new(sev); if (asid < 0) goto e_no_asid; sev->asid = asid; + if (sev->snp_active) { + ret = verify_snp_init_flags(kvm, argp); + if (ret) + goto e_free; + } + ret = sev_platform_init(&argp->error); if (ret) goto e_free; @@ -277,6 +303,7 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) sev_asid_free(sev); sev->asid = 0; e_no_asid: + sev->snp_active = false; sev->es_active = false; sev->active = false; return ret; @@ -612,6 +639,10 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) save->xss = svm->vcpu.arch.ia32_xss; save->dr6 = svm->vcpu.arch.dr6; + /* Enable the SEV-SNP feature */ + if (sev_snp_guest(svm->vcpu.kvm)) + save->sev_features |= SVM_SEV_FEAT_SNP_ACTIVE; + pr_debug("Virtual Machine Save Area (VMSA):\n"); print_hex_dump_debug("", DUMP_PREFIX_NONE, 16, 1, save, sizeof(*save), false); @@ -1864,6 +1895,12 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) } switch (sev_cmd.id) { + case KVM_SEV_SNP_INIT: + if (!sev_snp_enabled) { + r = -ENOTTY; + goto out; + } + fallthrough; case KVM_SEV_ES_INIT: if (!sev_es_enabled) { r = -ENOTTY; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 6974d63c84f9..4360cf04f53a 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -74,6 +74,9 @@ enum { /* TPR and CR2 are always written before VMRUN */ #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2)) +/* Supported init feature flags */ +#define SEV_SNP_SUPPORTED_FLAGS 0x0 + struct kvm_sev_info { bool active; /* SEV enabled guest */ bool es_active; /* SEV-ES enabled guest */ @@ -89,6 +92,7 @@ struct kvm_sev_info { struct list_head mirror_entry; /* Use as a list entry of mirrors */ struct misc_cg *misc_cg; /* For misc cgroup accounting */ atomic_t migration_in_progress; + u64 snp_init_flags; }; struct kvm_svm { diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 0fa665e8862a..43b6291e3a80 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1926,6 +1926,9 @@ enum sev_cmd_id { /* Guest Migration Extension */ KVM_SEV_SEND_CANCEL, + /* SNP specific commands */ + KVM_SEV_SNP_INIT, + KVM_SEV_NR_MAX, }; @@ -2022,6 +2025,16 @@ struct kvm_sev_receive_update_data { __u32 trans_len; }; +/* enable the restricted injection */ +#define KVM_SEV_SNP_RESTRICTED_INJET (1 << 0) + +/* enable the restricted injection timer */ +#define KVM_SEV_SNP_RESTRICTED_TIMER_INJET (1 << 1) + +struct kvm_snp_init { + __u64 flags; +}; + #define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0) #define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1) #define KVM_DEV_ASSIGN_MASK_INTX (1 << 2) From patchwork Mon Jun 12 04:25:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275504 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 66F96C7EE2E for ; Mon, 12 Jun 2023 04:38:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 038388E0002; Mon, 12 Jun 2023 00:38:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F2A7D6B0074; Mon, 12 Jun 2023 00:38:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DCBBA8E0002; Mon, 12 Jun 2023 00:38:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id CCD4D6B0072 for ; Mon, 12 Jun 2023 00:38:51 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id A179414024E for ; Mon, 12 Jun 2023 04:38:51 +0000 (UTC) X-FDA: 80892840462.20.78536C3 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2089.outbound.protection.outlook.com [40.107.220.89]) by imf21.hostedemail.com (Postfix) with ESMTP id 9B9C01C0006 for ; Mon, 12 Jun 2023 04:38:48 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=afUNk6L3; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf21.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.89 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686544728; a=rsa-sha256; cv=pass; b=HADVheyybtXM05e/pkwvBPU7piBWSS5J6M/neyxcjhIxs6mhOAuZ72ztYSW+dudBeoflJl c0L9pj84btgaU6KIZVYuBcOKRyhPqAfiKKqZaG0F19S1wpZu4/Q3qz1k/aTcibbvzTORM+ GHtcAzLBGFiFDKXajqLBowrAEuYNGXI= ARC-Authentication-Results: i=2; imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=afUNk6L3; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf21.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.89 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686544728; 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=QK4QZkCIIAqJ+tunV1KOANKN1pjYrjaWRxkGX4t4nd0=; b=0zHlkTSDIopoB1ztMLTQ0SN/ptvkF9jAriQTfFxzuhtLFgQ2DHA25pAh2M00LIrOGeCtSA 9hTkLWyOh8soj2nETWK7IupG8DURO/HZI9U/sbmjr2LvkqANXxwvYZhqnJmBdmOpjew1S0 FoNgAMQ7KBjFIy8aeIqePchbsKmF/jE= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YEpM1EOxCyk88+trB8HWjrvLcTORIYv+IcP06jJKjEUgFXu4FEgPMa2bgbCi8Co3p54G+20ZSaoU2i5yfLP19lG7zyGhiM/4sGTzw1DHhjzjBnlxIfIAUp16iGHjCIF9hTAcZ1/SvCTfSnY1bJFL/DeA53lJ5+esQbjZALzIzRdPG+7yf4Bby6dchR7Lp8GqBkpz1iSDBwZIj9GptpObDnr09v3PLWqxaCtT6ZOlX7j08yN8Vt9h4o38r46cQ62mVmodAnVb4eI8V0jgh4lraY55/G1zM9LmkIKVhmpBlocWMNxPalMQAK6kPNe6GmBnzORomRJzGpxB/5VFFCbaKA== 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=QK4QZkCIIAqJ+tunV1KOANKN1pjYrjaWRxkGX4t4nd0=; b=ASLk7X9syfErP8FD29sbFJFFnsCMSVR+08/vaD3ZcxD25IBf9tU9h1uqQAl25wtebXnI23MRvJRmoOgVfLtL5TyHHqKv8KU99+/KUDJ+hsESYUjN0jxmyPMZSRE7dFxkPlwYosADwX+FCYNL6ZKQKStvX0BOmZHD42M0brzlbr5v5BwQl/fRw7OdWUylve3gmFuvYg1NJU0t93JG+PxSeg2jmRQrxzJCTDvrarUjjEwUCmtSbtR7kigakfuaWXUGmr5aLZeMYgMLDQBa/6csAdH4hE3RS4v3kTwZFJPaDjwiwoatemC8pj51J1UBzLCj9WRpKIMTpk0cFwBJ9GdXTA== 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 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=QK4QZkCIIAqJ+tunV1KOANKN1pjYrjaWRxkGX4t4nd0=; b=afUNk6L31NJMbNfF6fYryZHs+mSM50U3Eok1IJvt56r9mIuCKzs36SKhz7VALt7syEmtIcTSdmMQwPTTBppTfGfEz+pvZlHagckJG3hLSGPIoP+PpmfEvAfvDWhctSXqcx5QpU5c3SdGllzEFmrgnRsYRiP/8xq0YP3/8xgkE3w= Received: from MW4PR04CA0238.namprd04.prod.outlook.com (2603:10b6:303:87::33) by SJ1PR12MB6337.namprd12.prod.outlook.com (2603:10b6:a03:456::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.41; Mon, 12 Jun 2023 04:38:45 +0000 Received: from CO1PEPF000042A7.namprd03.prod.outlook.com (2603:10b6:303:87:cafe::cf) by MW4PR04CA0238.outlook.office365.com (2603:10b6:303:87::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:38:45 +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 CO1PEPF000042A7.mail.protection.outlook.com (10.167.243.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:38:45 +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.23; Sun, 11 Jun 2023 23:38:40 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 29/51] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_START command Date: Sun, 11 Jun 2023 23:25:37 -0500 Message-ID: <20230612042559.375660-30-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CO1PEPF000042A7:EE_|SJ1PR12MB6337:EE_ X-MS-Office365-Filtering-Correlation-Id: 85b63579-e684-40b1-ee7d-08db6afee869 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 0UHqzijJkBtN2Q3L393p5DofWlEQvqPkjdFfojVk4esCIMk4+jpl5FSbMTDcJgVha91KpugKqFcjCdDO0stpM53ATPVmo5xt2/kGJcAPgwLwAaupemD7Pqzf4/Yuz4FG/vBZm8SSGuTO/F+6qgLhxyFyK2HBtA1Q1vFHPMd878O7bJlfCIkmFaOroDByv0R+vWTLyT937u5wV7+gM6Y+RIpdNqWyMJiFfa+UeG7NcMXYiSG+vu11rqXtWsqN1zBoCJ2mPVMpeKreJ+oa4MzoYO5plO6fZFPTMCHs3wXgj+EAosK87cVOAXITlFh12LNyHfeAgL+kazn4HbG01OyRZU84fj+LqPT6p99f48Cp5yO/gTUOliNpatfmCVUJ1li0UlGR8whH6NEJ7opNZzJ4lXFhMtG2npEAPKRosJJxjwaOFhBfJHW0QZtZL1QboZwS6M2OiTH/K8V9fqchzcXTr5RQ+J0d63h1SnxIIrtvaGg5hxFDC6Xzm1w9jumCykEOAopNuD8bg5Wl+4XEQ8TPjq5rT7zXK9brK2ZtW0/Na+WyrlP3RGPYPrckaFVywVtgDnwaRtjCHfnc/vj03yXpM68/YpyKkX8fDa1lEoWopFpi5ierjnzhRp9vq0HoO//ZdEbWh3CmKPAAuBhXtyQjNixkiDLEmBVWCBgBbhkR+BRXS5o1W8FzvTkKJunm1M1QaZ3D4nbCfiIwPtaYcH9BGCsqC7esx17Y3o98SLXy3lxCdHfmCo+JDJSHB4d7S1cCy/ICR+h5BBDdk5jtOJBj3g== 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:(13230028)(4636009)(346002)(396003)(376002)(136003)(39860400002)(451199021)(40470700004)(46966006)(36840700001)(2906002)(7406005)(7416002)(44832011)(8676002)(8936002)(5660300002)(41300700001)(6916009)(54906003)(70586007)(70206006)(316002)(4326008)(6666004)(36860700001)(82310400005)(478600001)(1076003)(36756003)(40460700003)(83380400001)(86362001)(2616005)(16526019)(26005)(47076005)(336012)(426003)(81166007)(186003)(356005)(40480700001)(82740400003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:38:45.4832 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 85b63579-e684-40b1-ee7d-08db6afee869 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: CO1PEPF000042A7.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ1PR12MB6337 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 9B9C01C0006 X-Stat-Signature: rzeazkck6sgran3du8smjctgkot36izf X-HE-Tag: 1686544728-685712 X-HE-Meta: U2FsdGVkX18ws3UQ3VJV166WVdAZ5MfzwKimD8zR1lJ4bYXAz8EErhB17ilTfV0JM9IzqPu1/C8mTHC38M/nNhVsDTYA7a8zEH1LsBLfXrqD7G2YN/YC3g488PWkv6pDIBqkeuqPwXrdIPreoBK2MprK8P1NevKzaeUWyuY+RRqQKEVGjM76SDEtDcHW1GVu3RO3X5l3qgxecVpRfH1SJas6uOiXeDTAsKUGDG5omo4HRYIAj/HVCXer7bFKJdahb64PBnXgFvRTpuRW9oeaqZM2kzoViil2qWNpuk0/borwvDh00vrMM1WfsTawCZxi5uSWFnkvBkbED7hZjVZQumwNunpjzgHBMMpA6zdjbhCohw9JGcy4e+PRVfTF0ofVJutOpKSsWD50Yp1a6fDjrqsVPV3wSde9nMvRX1AkfgvbTosL66vELF5p4vP5ycmBPL9RCpnu07S5dbqTtQwTm3jQAPkHMDbUPzORRZX7LdD+HQbopZPzBX078Re1Lmv2W2YRgZUeAvO4m1yW9qOXGdnYwGvGn56Dt8yRwh+4sh1+ZwZuZAweS1ZqVvNnjn4VZku442rxnr7TtRk2NS+02/cEnBRtYC0rbZ3aXpHqWRe19VSsXgrFM2MowFEEo0Vo1JalR8VeGbCQh6N+KHajwvXfG84C7SoUCYyGLMY6xUGwN+3OKS44dzLmFXNyrrkx3YONYFRHykUMTitYpwY3Wx4cOXR0+wrnIcOSwTl5lLVJ5SlKb/oEm0wnNKTvVDn7AW4egHulKMf1UK1HauNbE7D8dfO5aXLaopJdIqaJqp3i8JOLiMrDF35YWluYk5hHO++QlBpVy+IbXeA9bGOQlBnwm638vN9PaCBzszS3k7O9SYEGA5QbDzWtDLBbrC4MZzPNlDxBDmFeTkSDchd2Q4dO++Cn4cogUzPi4SBAkp+8hNnb0wjqnLU+71F8V6iujmGJoF0Za1QuqC3ruen L28Jm9Pf PWoL73GDA5LtL+69eGKi962ypUCdLFTuY/r1P9kEZitx5GDjCbIO2smkvoO4r12vhKtEALg7cCfLtfyCDUJwJiZJKyEzx4sVwtDBtFldlxpzFaIEFIyV0ekmg1kUFgNiSnJNujFc9NOesFYNjWGr3Gue+TwDka9D0WX46aj9WlamQ8IEO3ZK6VgdFEWHKMslWQUWGx2yrotb9ELxDflLqv0DZDKebHjPuu0UkCOTC+kRNgJxXfehxUxWEXChxWUixXXqWJBInzqJqUTIMQFuKKLnBIypVfybZ7CVVxQK9Zo7f7iH3cP9EoWWM1rcVof5pnpYe9tSjuAC9ied9Hh5AxsGKlqlox/XrkQVWXlD4XsMuOzk= 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: From: Brijesh Singh KVM_SEV_SNP_LAUNCH_START begins the launch process for an SEV-SNP guest. The command initializes a cryptographic digest context used to construct the measurement of the guest. If the guest is expected to be migrated, the command also binds a migration agent (MA) to the guest. For more information see the SEV-SNP specification. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra [mdr: hold sev_deactivate_lock when calling SEV_CMD_SNP_DECOMMISSION] Signed-off-by: Michael Roth --- .../virt/kvm/x86/amd-memory-encryption.rst | 24 ++++ arch/x86/kvm/svm/sev.c | 126 +++++++++++++++++- arch/x86/kvm/svm/svm.h | 1 + include/uapi/linux/kvm.h | 10 ++ 4 files changed, 158 insertions(+), 3 deletions(-) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documentation/virt/kvm/x86/amd-memory-encryption.rst index 1240d28badd6..3293e86f9b8a 100644 --- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst @@ -461,6 +461,30 @@ The flags bitmap is defined as:: If the specified flags is not supported then return -EOPNOTSUPP, and the supported flags are returned. +19. KVM_SNP_LAUNCH_START +------------------------ + +The KVM_SNP_LAUNCH_START command is used for creating the memory encryption +context for the SEV-SNP guest. To create the encryption context, user must +provide a guest policy, migration agent (if any) and guest OS visible +workarounds value as defined SEV-SNP specification. + +Parameters (in): struct kvm_snp_launch_start + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_snp_launch_start { + __u64 policy; /* Guest policy to use. */ + __u64 ma_uaddr; /* userspace address of migration agent */ + __u8 ma_en; /* 1 if the migration agent is enabled */ + __u8 imi_en; /* set IMI to 1. */ + __u8 gosvw[16]; /* guest OS visible workarounds */ + }; + +See the SEV-SNP specification for further detail on the launch input. + References ========== diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 70e0576a32d0..e65f3be67c23 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -22,6 +22,7 @@ #include #include #include +#include #include "mmu.h" #include "x86.h" @@ -75,6 +76,8 @@ static unsigned int nr_asids; static unsigned long *sev_asid_bitmap; static unsigned long *sev_reclaim_asid_bitmap; +static int snp_decommission_context(struct kvm *kvm); + struct enc_region { struct list_head list; unsigned long npages; @@ -100,12 +103,17 @@ static int sev_flush_asids(int min_asid, int max_asid) down_write(&sev_deactivate_lock); wbinvd_on_all_cpus(); - ret = sev_guest_df_flush(&error); + + if (sev_snp_enabled) + ret = sev_do_cmd(SEV_CMD_SNP_DF_FLUSH, NULL, &error); + else + ret = sev_guest_df_flush(&error); up_write(&sev_deactivate_lock); if (ret) - pr_err("SEV: DF_FLUSH failed, ret=%d, error=%#x\n", ret, error); + pr_err("SEV%s: DF_FLUSH failed, ret=%d, error=%#x\n", + sev_snp_enabled ? "-SNP" : "", ret, error); return ret; } @@ -1871,6 +1879,80 @@ int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd) return ret; } +/* + * The guest context contains all the information, keys and metadata + * associated with the guest that the firmware tracks to implement SEV + * and SNP features. The firmware stores the guest context in hypervisor + * provide page via the SNP_GCTX_CREATE command. + */ +static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct sev_data_snp_addr data = {}; + void *context; + int rc; + + /* Allocate memory for context page */ + context = snp_alloc_firmware_page(GFP_KERNEL_ACCOUNT); + if (!context) + return NULL; + + data.gctx_paddr = __psp_pa(context); + rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_GCTX_CREATE, &data, &argp->error); + if (rc) { + snp_free_firmware_page(context); + return NULL; + } + + return context; +} + +static int snp_bind_asid(struct kvm *kvm, int *error) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_snp_activate data = {0}; + + data.gctx_paddr = __psp_pa(sev->snp_context); + data.asid = sev_get_asid(kvm); + return sev_issue_cmd(kvm, SEV_CMD_SNP_ACTIVATE, &data, error); +} + +static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_snp_launch_start start = {0}; + struct kvm_sev_snp_launch_start params; + int rc; + + if (!sev_snp_guest(kvm)) + return -ENOTTY; + + if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) + return -EFAULT; + + sev->snp_context = snp_context_create(kvm, argp); + if (!sev->snp_context) + return -ENOTTY; + + start.gctx_paddr = __psp_pa(sev->snp_context); + start.policy = params.policy; + memcpy(start.gosvw, params.gosvw, sizeof(params.gosvw)); + rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_START, &start, &argp->error); + if (rc) + goto e_free_context; + + sev->fd = argp->sev_fd; + rc = snp_bind_asid(kvm, &argp->error); + if (rc) + goto e_free_context; + + return 0; + +e_free_context: + snp_decommission_context(kvm); + + return rc; +} + int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -1961,6 +2043,9 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) case KVM_SEV_RECEIVE_FINISH: r = sev_receive_finish(kvm, &sev_cmd); break; + case KVM_SEV_SNP_LAUNCH_START: + r = snp_launch_start(kvm, &sev_cmd); + break; default: r = -EINVAL; goto out; @@ -2152,6 +2237,33 @@ int sev_vm_copy_enc_context_from(struct kvm *kvm, unsigned int source_fd) return ret; } +static int snp_decommission_context(struct kvm *kvm) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_snp_addr data = {}; + int ret; + + /* If context is not created then do nothing */ + if (!sev->snp_context) + return 0; + + data.gctx_paddr = __sme_pa(sev->snp_context); + down_write(&sev_deactivate_lock); + ret = sev_do_cmd(SEV_CMD_SNP_DECOMMISSION, &data, NULL); + if (WARN_ONCE(ret, "failed to release guest context")) { + up_write(&sev_deactivate_lock); + return ret; + } + + up_write(&sev_deactivate_lock); + + /* free the context page now */ + snp_free_firmware_page(sev->snp_context); + sev->snp_context = NULL; + + return 0; +} + void sev_vm_destroy(struct kvm *kvm) { struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; @@ -2193,7 +2305,15 @@ void sev_vm_destroy(struct kvm *kvm) } } - sev_unbind_asid(kvm, sev->handle); + if (sev_snp_guest(kvm)) { + if (snp_decommission_context(kvm)) { + WARN_ONCE(1, "Failed to free SNP guest context, leaking asid!\n"); + return; + } + } else { + sev_unbind_asid(kvm, sev->handle); + } + sev_asid_free(sev); } diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 4360cf04f53a..9a7cafb018fe 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -93,6 +93,7 @@ struct kvm_sev_info { struct misc_cg *misc_cg; /* For misc cgroup accounting */ atomic_t migration_in_progress; u64 snp_init_flags; + void *snp_context; /* SNP guest context page */ }; struct kvm_svm { diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 43b6291e3a80..b4c7ac9710d3 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1928,6 +1928,7 @@ enum sev_cmd_id { /* SNP specific commands */ KVM_SEV_SNP_INIT, + KVM_SEV_SNP_LAUNCH_START, KVM_SEV_NR_MAX, }; @@ -2035,6 +2036,15 @@ struct kvm_snp_init { __u64 flags; }; +struct kvm_sev_snp_launch_start { + __u64 policy; + __u64 ma_uaddr; + __u8 ma_en; + __u8 imi_en; + __u8 gosvw[16]; + __u8 pad[6]; +}; + #define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0) #define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1) #define KVM_DEV_ASSIGN_MASK_INTX (1 << 2) From patchwork Mon Jun 12 04:25:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275505 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 25929C7EE2E for ; Mon, 12 Jun 2023 04:39:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A4A3A6B0072; Mon, 12 Jun 2023 00:39:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A213A8E0003; Mon, 12 Jun 2023 00:39:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8C2698E0002; Mon, 12 Jun 2023 00:39:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 800436B0072 for ; Mon, 12 Jun 2023 00:39:33 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 5658D1A0281 for ; Mon, 12 Jun 2023 04:39:33 +0000 (UTC) X-FDA: 80892842226.30.C5F49A7 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2074.outbound.protection.outlook.com [40.107.92.74]) by imf25.hostedemail.com (Postfix) with ESMTP id 61752A0009 for ; Mon, 12 Jun 2023 04:39:30 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=mWEiunBI; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf25.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.74 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686544770; 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=eHNcgb3bW0RGuw2lf/r2QANgbsO2xqcPD/u1M2WbB/M=; b=HjXgWxmRAQK/AueO3mHhpHeAk5aMYIYVl1QxM44W2evj/DCuGAL+Oe9hmb2wNg44Ypy5A7 W4nND8N4y5ByeBxaZelra3jlT+XdwxJzLrrwoteUQM1R7NO9BUQUynFhC4KPZMRMH6MnV4 HrLwN1X8jke2/PBRPsIeKufx1/JgyI8= ARC-Authentication-Results: i=2; imf25.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=mWEiunBI; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf25.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.74 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686544770; a=rsa-sha256; cv=pass; b=A3sfTrd5gjqInTh0ZIhJhayW/afxM+cnH1LmlH5q9cpoIN5LobG39EIxU1wXsQiQt/+X4/ ZgifaYwupmWTBxKrvSfZXuSQpkaOSpG1Sn+YpVMHL7SKU28iZS4MwAxoPpszaHqbxOvgsp 2URQ88tTVBC9KZfwg1MQUhbXSpYoEto= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NPi3DdT2PUo1OlJ+MiySWa7ZyCAQoxHyDwiJLMgT4bFeSQxnVte4gjr0i+o1HDxe0aP+VpSYSRSnwRjnEWPaomvX/JrZ8ScJ25jzj+Z25/0z8kaQijzr+DIJGOVpyjIyWX++k+qX9soDp6REMBGslhbyYbXlA2tw8qnNONScFKkqfX+q9HADKUXXtfNgl9uBncwxM3D5PUiYPlkJxhQs9mSh9dJFU6DrMrtNQHhcQzoQIcgnwhlemqVs4mFs/7WqaqpFIoV1UUzB5p3qHAmBFzkrBaRF7H7dzeMMdnhTHiJ+vetHX1SF3yCaqtUj5ALVEKuw+G4C8DC2UTn95P9f9Q== 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=eHNcgb3bW0RGuw2lf/r2QANgbsO2xqcPD/u1M2WbB/M=; b=g5mZMjYIulpTeandK60uHJQtTnOPx2LTYje7ns5VW2kwNvuu1C5J046mFjXWw8NPiVMguJ7i2SCNr0txjAq11j2IjVdmA3ucXOv2iQmrdlIm3/MiqWPeiFUgPum8A2qd5UyCwE4mC++jsKwD7RaVDe+CfiHya7RVFFqB08HXUPH53KTf26fO0TUJORabvWJVCKPYA31HBsSa+wo6WI7kzm9nH1zidZtgTuDip/nppgdiMg7CyY6sI3nI6D05KMrNc4Wk9tXOdbjDh6ge9pQCSSLppkIBEZeFeKhuvwoja2pyE4ORZEDmadhQ6IfCL3czZHC5+5RTUfjJKTQvlNZ6fg== 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 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=eHNcgb3bW0RGuw2lf/r2QANgbsO2xqcPD/u1M2WbB/M=; b=mWEiunBIUYm5h3yljexd3mZ6hGY0lEc/viKTVhNo5PUPfPqQfwk/s1oCiuc/ofwqfBh2ULiYFslFfrwX0EH8+5YB9WOXz87uO8ITRRibB2Pep9BLdUVqFqAB78fXypfGuReP3nzCKsvRBG4j9bzqa2LFWJaE0FYauBed7atIAeM= Received: from MW4PR03CA0003.namprd03.prod.outlook.com (2603:10b6:303:8f::8) by CH2PR12MB4873.namprd12.prod.outlook.com (2603:10b6:610:63::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.29; Mon, 12 Jun 2023 04:39:26 +0000 Received: from CO1PEPF000042AE.namprd03.prod.outlook.com (2603:10b6:303:8f:cafe::d3) by MW4PR03CA0003.outlook.office365.com (2603:10b6:303:8f::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:39:26 +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 CO1PEPF000042AE.mail.protection.outlook.com (10.167.243.43) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:39:26 +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.23; Sun, 11 Jun 2023 23:39:23 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Vishal Annapurve Subject: [PATCH RFC v9 30/51] KVM: Add HVA range operator Date: Sun, 11 Jun 2023 23:25:38 -0500 Message-ID: <20230612042559.375660-31-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CO1PEPF000042AE:EE_|CH2PR12MB4873:EE_ X-MS-Office365-Filtering-Correlation-Id: 5e65158f-8517-41a6-9a81-08db6aff00bd X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: /13cltSALSFlfzZt1R+Xu8figwINoNYALkqfZNNREKs963ny4VOWlyKZ3mv8mVuBHneJDOpqFLJKms8nCy38XsAKGVnKoD4L9UbSGDLwILyT4j0RWsblsM7EU8ffvy3hLterW+n8RK3DMEstvcFWrren5+wQOFMRTrWACQOyCq8FAhghYrfZjS1Cy8AxE1FQ5NFKV0O/qSbC9sblYDk9uwP2NgSd0L4kx+hoA3aRdiQbPLMXaeI+6MfjcOINQjNFr5JI4isHSrRzr5473WZ/uYNEODZiw+DqD05enRuCLTA+WU/A2Ylju/8/rK2P92bs8sTbzCNoxwMwHCo/DH/Mv9XAzgZSwbdV0katiImI2WOdErShobcIinGmWEedAbK+J3U2mDRjcJsuvmMaqtVFi0kGazxkuzaJOp1f7V2fSssJVNfjZlBGKHgqJ+qgR04dtiY2QU6wZ4z+p1CZv5qkwqDG3f5L1mIu59omczQBEw4PArEXaAbExfUQCODGfWEbEoKNutLALoqrcmF5TOMffY6cmCymoGNqrfzLJ48K946x3Nr0hQdK/RxSMlnEaM8CO86e4cGVjlqCpoFWTT46K2lydTkKxuK5IiIX8LiNkxIlA5+2ir8w5FxOYiG5Q2xGmEQeCSrFXeWN2Z+km7g7RrA3dqNMRrV14PntUFZERbwiixKrbYcqlz/GVAIHd1Qr/w5nxZu7NLFwW+0Im7iAFo+GHBu7Tm7/31qenbj9+vmrzuxERln+lXJJPfZpyHpvrzmfTmRW453YZR9IUfnuKQ== 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:(13230028)(4636009)(39860400002)(376002)(346002)(396003)(136003)(451199021)(36840700001)(46966006)(40470700004)(426003)(336012)(2616005)(47076005)(83380400001)(36860700001)(82740400003)(356005)(40480700001)(86362001)(82310400005)(81166007)(36756003)(40460700003)(478600001)(44832011)(54906003)(4326008)(6666004)(8936002)(8676002)(2906002)(5660300002)(7416002)(7406005)(70206006)(70586007)(6916009)(41300700001)(316002)(16526019)(186003)(1076003)(26005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:39:26.3281 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 5e65158f-8517-41a6-9a81-08db6aff00bd 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: CO1PEPF000042AE.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4873 X-Rspamd-Queue-Id: 61752A0009 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: p1os96kzra3w9gdwquzeboibx7bdx6am X-HE-Tag: 1686544770-225921 X-HE-Meta: U2FsdGVkX191z9Sx2cTrfFnFJCM9Pi50iX8Oq8tkO+4gxjB/sc1XTqdWKsr3CTMMcrcg7CSwdRArG2GnpD/9pVb0Jr1btlYCOvxzLs707AOYBPTge2W9Z8C8vhY3TS6wEnCRw50m/Tsbwx/w7UhlwKAhIK4AC6sZyYWX5A8cDEF5zP03e0S9kefH8ucDm10ocSTUHx/8aT6z4TwGax0fjkh3IwtrQzy9hn/vMIv+rRoDVbMkYiYk0cuqJkSrplEKPCwCB/6WE1CjNQ+7XXiYcYamnXUwVgnbiBLdy0Z78CEx7D1OXULJcz1ZlKhMw5L6g+oQg1rkBkfVp7l+eovZpMdpJUdOrmBUpZ1k0Hgj4sIiZphMXiziYaiz+LrjWmbNibH28MWwqazc+5TC2One+3k7laLS+I8oPR9pl84F50aJFE6LZoeeahjQ2fNyurKxGGxZsMQyQf2Idu6ASTt5CWA666Qa4+9w56C9Xe5hraM0xjMWZS2bfMyNNobqhOALbC6r/qytuRSjcRshHu25qqfszMrmsZe/PY+MgxWmyc4NhnvkcpmbVmLKPPRMJdZ5MuE6CBJbkGcT+jH2VDODOtVEzs3BLmZkDLV2b88pgro52NS2sUiXQQ40iygN4Zs0LefoFxOooEDP/zwM3ePdgAvyeuUhvPjJW64YiBQRa6HVeGnh1j4PcBTJOMq3X8tCWSn4ESoPIHHa2MCDko7/E4J7Cx/XmMqzialA8fH1TkyZqM94osMZIBpx1TFVywbed2jwrX7CbW26PJS0vmL0LZrFTYIxnEUHzkeTv12DHPF0nioTQ6xVRgVTeHuV5MJhyO0Tu7D4PjLBOpIsMS6EZnRybEIBDfNN/UM+MY4qgc72f5N9CN3eP9ltR6m5/eS1em8fCDH4WANGFZz7mUoS47Wr4QN0u5QYKskyxIpZmp9xhphu0IzNEFuW96XW3Eq/yX+tuXSGC/VSO1TkOUL 81oRWHhq SsT005KS5QP4uScktvgXguX066bwYrbzVw/ygh5bYWBWAdRvhBDghGalF/UJlu3ZYHt2f0KcbpJkABuZXJa4IEqnbUgHVKJGy9fJU2BgNOAXWwvpQw6b6XhsTsFHDQerU4TRlLeA3wQjQC099LwlumRSkS6OqB/aoExlkfauDQXCYi9hcIzUYBafLnQFuy/uALo75oihGFF2JeAJQgJt89521DH3VKieeSKRSsYb7xQ5g9p5EBPBvoMAF8MNFxSw5ra4WSbyigka97xlOnlMgW500zaaYYBNQBX9fByP3kvtmRvyRC7fJyxzwXYyriczwag2q5LNE8b5X6RY= 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: From: Vishal Annapurve Introduce HVA range operator so that other KVM subsystems can operate on HVA range. Signed-off-by: Vishal Annapurve [mdr: minor checkpatch alignment fixups] Signed-off-by: Michael Roth --- include/linux/kvm_host.h | 6 +++++ virt/kvm/kvm_main.c | 49 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 7de06add2235..9a9d4141ba74 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1400,6 +1400,12 @@ void kvm_mmu_invalidate_begin(struct kvm *kvm); void kvm_mmu_invalidate_range_add(struct kvm *kvm, gfn_t start, gfn_t end); void kvm_mmu_invalidate_end(struct kvm *kvm); +typedef int (*kvm_hva_range_op_t)(struct kvm *kvm, + struct kvm_gfn_range *range, void *data); + +int kvm_vm_do_hva_range_op(struct kvm *kvm, unsigned long hva_start, + unsigned long hva_end, kvm_hva_range_op_t handler, void *data); + long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg); long kvm_arch_vcpu_ioctl(struct file *filp, diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 422d49634c56..48beffca6b67 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -642,6 +642,55 @@ static __always_inline int __kvm_handle_hva_range(struct kvm *kvm, return (int)ret; } +int kvm_vm_do_hva_range_op(struct kvm *kvm, unsigned long hva_start, + unsigned long hva_end, kvm_hva_range_op_t handler, void *data) +{ + int ret = 0; + struct kvm_gfn_range gfn_range; + struct kvm_memory_slot *slot; + struct kvm_memslots *slots; + int i, idx; + + if (WARN_ON_ONCE(hva_end <= hva_start)) + return -EINVAL; + + idx = srcu_read_lock(&kvm->srcu); + + for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) { + struct interval_tree_node *node; + + slots = __kvm_memslots(kvm, i); + kvm_for_each_memslot_in_hva_range(node, slots, + hva_start, hva_end - 1) { + unsigned long start, end; + + slot = container_of(node, struct kvm_memory_slot, + hva_node[slots->node_idx]); + start = max(hva_start, slot->userspace_addr); + end = min(hva_end, slot->userspace_addr + + (slot->npages << PAGE_SHIFT)); + + /* + * {gfn(page) | page intersects with [hva_start, hva_end)} = + * {gfn_start, gfn_start+1, ..., gfn_end-1}. + */ + gfn_range.start = hva_to_gfn_memslot(start, slot); + gfn_range.end = hva_to_gfn_memslot(end + PAGE_SIZE - 1, slot); + gfn_range.slot = slot; + + ret = handler(kvm, &gfn_range, data); + if (ret) + goto e_ret; + } + } + +e_ret: + srcu_read_unlock(&kvm->srcu, idx); + + return ret; +} +EXPORT_SYMBOL_GPL(kvm_vm_do_hva_range_op); + static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn, unsigned long start, unsigned long end, From patchwork Mon Jun 12 04:25:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275521 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 A2CBFC7EE25 for ; Mon, 12 Jun 2023 04:41:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 457656B0072; Mon, 12 Jun 2023 00:41:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3DF5D8E0003; Mon, 12 Jun 2023 00:41:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 232118E0002; Mon, 12 Jun 2023 00:41:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 12BC86B0072 for ; Mon, 12 Jun 2023 00:41:05 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id CC412120295 for ; Mon, 12 Jun 2023 04:41:04 +0000 (UTC) X-FDA: 80892846048.20.8E64515 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2053.outbound.protection.outlook.com [40.107.223.53]) by imf18.hostedemail.com (Postfix) with ESMTP id C6FD21C000D for ; Mon, 12 Jun 2023 04:41:01 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=P47QNxkf; spf=pass (imf18.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.53 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=1686544861; a=rsa-sha256; cv=pass; b=dlEGBPz5hmLGTdbzcoxphx8jKa/csEUlP1MjLuWYKoN1WC3en+Q7B8+die70y8L1qoSUFY bS7hqWEHUmjaQHIsPwofvxDt1rKeHKLbUl3+bDEhfG7Fudyluplf6tk47n8yi1MI1aZw6O 8qENRSaIFyaWD/BPTX5TsxqwuOS17Bk= ARC-Authentication-Results: i=2; imf18.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=P47QNxkf; spf=pass (imf18.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.53 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=1686544861; 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=28cE1nFtQUfuCs5Hi5DEX8uBfUlxeTHiFFKicSfKOZU=; b=PjbMVmxfOqjtr0pxJ8X9+eJ9c+JKk3HwPuL7DC3lUpiRryli8Q1XSFl3CYMtkyyvGiaJls 05FQGCpppF+NKqUlNoq6F0w6KXgQ1I91NqRHNTGTI9saLk/h/l70yeFRUqR+FYOqZYmvW2 s2yIW5OdiG9gSyejfNoX4HjpG3WlDLc= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=U681qMlhTPyX4zBte/1J08WgQdSXtpNRC8GHMG0lZwz6PaTHoR/NtPZddqCxvIKkJNbinV3auUC43G78ahxCQlVAzO4/LaB+MN/JutX4tTrpGoZARZ85v/SlxBMZl0XGGNDzoLqFi+ifT1D7yyqxfaCuF6QJtZlFamF7KG4Gkw+2wtRnh/38Ns3GhRPzX9VIytaxs04PcSOtWQWvz6OGKhiz2oVL7+h1PsFV8onvNjZhL/KnXMAwmG/mO3f8wbsN3cEpqX2dIuqn6oLVrQLbTEcEhGD6/QMDSW8+EB2Xkp4wqyW+1zXHlgNZhirHOegB0fWF6A0B6Nr+OkHr7YqIhg== 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=28cE1nFtQUfuCs5Hi5DEX8uBfUlxeTHiFFKicSfKOZU=; b=TDBUsMiWw0LylSoax6yvRNF1u01EEn+zzdc/m5xR3B8uYHl9U+zBn0I/dUGy/mvxpffJjWx2x9h7BHgf5HdruSoNDP9KgDAUG1mKwFvcoVcMe/Uui1Pdkw/Ym3jKZeMiQku3yJwFdBkUROBDsCnb2dCMkZJHMML4UyynAynRTf9P+26RJCzuvTpS4uUIQnY9g1KCADh3o4HVjTfNrJyuvT6vJzFoAoCPmgqJhIXb3tr6gxs5Z1ntL34lgTE1TjX/5lv+GNdGUeLTywcqevh95zEIz9Xn2SKI+wxqJurANE1AEw0auOlNzWwjnNIkQ/7jS/4Q+v3GmPhYoT2J9TGTYA== 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 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=28cE1nFtQUfuCs5Hi5DEX8uBfUlxeTHiFFKicSfKOZU=; b=P47QNxkfRU0oZJ6vNm7Wqx/m0Iu0Vsyv44k5mZOB4QiIDM6s+zu/0iVzPQIIBKAsTtkl08BMIk6V9Yaypy6ZwLoEzI1hX1KlKU63rUZuVqLv76eEoZW9aDKZmespl+2ZAIiI94DpLqCSiM93fLyWvUTP69ARxK3ATUAPGmlca+I= Received: from MW4PR04CA0233.namprd04.prod.outlook.com (2603:10b6:303:87::28) by LV2PR12MB5752.namprd12.prod.outlook.com (2603:10b6:408:14d::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.44; Mon, 12 Jun 2023 04:40:59 +0000 Received: from CO1PEPF000042A7.namprd03.prod.outlook.com (2603:10b6:303:87:cafe::4) by MW4PR04CA0233.outlook.office365.com (2603:10b6:303:87::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:40:58 +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 CO1PEPF000042A7.mail.protection.outlook.com (10.167.243.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:40:58 +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.23; Sun, 11 Jun 2023 23:40:53 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 31/51] KVM: Split out memory attribute xarray updates to helper function Date: Sun, 11 Jun 2023 23:25:39 -0500 Message-ID: <20230612042559.375660-32-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CO1PEPF000042A7:EE_|LV2PR12MB5752:EE_ X-MS-Office365-Filtering-Correlation-Id: 3062944b-838f-4225-c420-08db6aff37b9 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 8k/qp+GwPlSpluzG5rMPQ0v1hvJXRBpRDZBx2Tg5D6MifWTeMcZ2SR3HqrxFSkvLXRA26DJNPHvCbQVwBrBLCGRn50F2N4qLClJA+r286oKWvPnDSXf0tTBXGCA9SUJUu6x97S3xD1CQ+KgRqws2Iu1tdDbZKysOt1tJ9OmsdXf1CUzqKuQLEZgTS95Z4NcblhCPFp3XhxdZyOyblNzTDB9XjEPBJheG7KohvRJhDBrShthEjDDPS8shnIpUpIQ2uYZ3ZhL5d3nPdb3RIF2j9Lvpp3ce7kILcpC48wig6kYAAK2c4H/GH03bnY3r12RXvBJxSPh5coprTwgb5kHZthirdLs85xPW5TNMvZReDmdDedLZsVfKFcNXSRK2XR/5KlUBSEpKZfZ3OKGPAIMUR2XGgyi+SIW5IEgH3cCbfvMYg5OLX9bjM/aEGJ0WgV7X6dLLzovqs6OWhqsYxmwTVKSb4xP9JWKAIXu7UyHpbe1l/ZCDqFp2Gfkp3SFgATcLpIl2vc+9ElnO1n77XQzZotqnQ+MeTSdaV3c0RBPblp8RGVpXuk8XEuGiLkkuCS53C4kfyr0jUkW1BG5zkIgi+hvAPZU1WgND++KqPZthR9dV1PzTLavAqFYoBkrNGxJiFzQAbJF6fK4rzlyjXxMjeH6ahpaQCrL+fQ7pwheAI0tY9UFOCsRySCXCuReBVfHGeMIAf3KAa4OCycgPVGRLTJRFmnaYDx4pCamG+taXZ2Et7pub069/Grv694eiVYgrmgSKJV6jehH+r7BrsWIu2g== 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:(13230028)(4636009)(346002)(376002)(396003)(39860400002)(136003)(451199021)(46966006)(40470700004)(36840700001)(6916009)(4326008)(44832011)(316002)(7406005)(7416002)(70586007)(15650500001)(41300700001)(70206006)(186003)(16526019)(2906002)(54906003)(478600001)(8676002)(8936002)(5660300002)(6666004)(40460700003)(1076003)(82740400003)(356005)(81166007)(40480700001)(36860700001)(26005)(83380400001)(426003)(336012)(47076005)(36756003)(86362001)(82310400005)(2616005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:40:58.5776 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3062944b-838f-4225-c420-08db6aff37b9 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: CO1PEPF000042A7.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV2PR12MB5752 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: C6FD21C000D X-Stat-Signature: 9ow4tdy3z4dibei7ouyazkkr5noygonr X-Rspam-User: X-HE-Tag: 1686544861-468339 X-HE-Meta: U2FsdGVkX1/4Cji1XtWxB+7Bk6SvVzw/DZlegCottYTfjL4oekI3SYJ7BU3JwolxbPeIIGvE+RAqxdpW16cuec5MbKhkZY3X4TqHqPYDknQhIQfavGtcSW4VdC06izVUzwoX3Bnk8wCDhWjbL5RVhlbCpjR0IMU6KyvhwW14ZPiidyyJQ+Jb+/g7GA9/77bw8BGDSweVMYciysXN/v3yJQTVIMqdTznJnsaH/soGXWtOZVGICmuHvRIXFtLqEd3aGfXnu05WXpnzBns/+NsZU8hX+6Taa4yTQn3m4ANvaVCqLBcurmwA7O1xLTJIGKycnsw2KNqDMj04QndRwzFrz55e1Aq5Ie/DYBljEIToEHxuM9pURLqPglfa8ILXLeQLJLHMV22Z/tT2T1ouXQRrbPDh/CI8PeTgYjODTNZYK1J9zQEMqte/Y1ftldvmzuIoTg610i9YP8dfTCLRXoIscu1rXM7zf6llcbV3Ry1F2xVZPQJhQPUESDy+6Xhyu1FssHOpYt9C0pdzvk/40iQ56L6FQCjxCYI0i7N3RKj2lx7NSNVOQ6NLLuMmw7Hfeg0uFKO1FMa2El+KrnR5hxa1AYKjUXFtUedpjNyMvbXjjyxIKwT0W5uHoWwK4jJMEahTxMdZ75LZYte0xSYjiEJ3xYJbUqGGee652GrM4mZObNv1c7BMpbRgHBAxVFlM57vPCyPdYI+eTCVOj2wjyyuTodN+EbRSUPrPxQoEUIKi9mMSf2AgLHh1+2UWVyNeRD4toAinlPaGk/MgGfLTwyJK9rTu/PWVen7vq26GNL4DZun6xY3vXarCsQ2FOixsRl4hOR+5NnlSzL+Gxi1YzHFnubhxID2W2fWMWS6NMBLIZz0w8vZ6DE7WhVAwO4yboLL7Jzr/Y7qJvy45FwaC2S1di6pzS6YAHWvf8tsLRWHs+59PtlCl5KbkfhuZLt/qESSrmLOBeMULQ1KjN645L2c W/TUWiTz xexaT4y7pc1xTU+Njq4/eyYcd0M1hXJsMqaUu6mlXigqBoVHDthhP+JEWSe3XOF1SZYlAOUmjRl0z4CVAeLPFDcXz+L+ujH5NY33inEx1n3bp3lfToklxa+HmUYdULqCOMSi6ZvA6giVAVqeI63Sd7mq2e7KNp2NnjxaoNiJxK2lXe/GNDasqbaNGr5eqAdpjM4A4lLYjVAcQa5/PuVmeDopneBB1wP5ox8+kcpAYRPJ9mCNBX8i2AayL8GSqB7XQveZ9C5OTNdrtX1I/BYTh0GkMbp1MfE6SCLGZw4Vdr2UM0sc1Qa9Fe20PiwWRKPYth9z3dfl28QHrJCNxZ7uFSuhyuvlptfLNidTKMiXGxbdge+igF9a2p6xa+a2Dq8NdCTA74FuBhCBrbR7I9GlPWHMgupeCXSEf+GHabXVB1yYDdc4= 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: This will be useful to other callers that need to update memory attributes for things like setting up the initial private memory payload for a guest. Signed-off-by: Michael Roth --- include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 9a9d4141ba74..56055692cdfa 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -991,6 +991,7 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module); void kvm_exit(void); void kvm_get_kvm(struct kvm *kvm); +int kvm_vm_set_region_attr(struct kvm *kvm, gfn_t start, gfn_t end, u64 attributes); bool kvm_get_kvm_safe(struct kvm *kvm); void kvm_put_kvm(struct kvm *kvm); bool file_is_kvm(struct file *file); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 48beffca6b67..0c0a75affab6 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2466,12 +2466,28 @@ static void kvm_mem_attrs_changed(struct kvm *kvm, unsigned long attrs, kvm_flush_remote_tlbs(kvm); } +int kvm_vm_set_region_attr(struct kvm *kvm, gfn_t start, gfn_t end, + u64 attributes) +{ + gfn_t index; + void *entry; + + entry = attributes ? xa_mk_value(attributes) : NULL; + + for (index = start; index < end; index++) + if (xa_err(xa_store(&kvm->mem_attr_array, index, entry, + GFP_KERNEL_ACCOUNT))) + break; + + return index; +} +EXPORT_SYMBOL_GPL(kvm_vm_set_region_attr); + static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, struct kvm_memory_attributes *attrs) { gfn_t start, end; unsigned long i; - void *entry; /* flags is currently not used. */ if (attrs->flags) @@ -2486,8 +2502,6 @@ static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, start = attrs->address >> PAGE_SHIFT; end = (attrs->address + attrs->size - 1 + PAGE_SIZE) >> PAGE_SHIFT; - entry = attrs->attributes ? xa_mk_value(attrs->attributes) : NULL; - mutex_lock(&kvm->slots_lock); KVM_MMU_LOCK(kvm); @@ -2495,11 +2509,7 @@ static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, kvm_mmu_invalidate_range_add(kvm, start, end); KVM_MMU_UNLOCK(kvm); - for (i = start; i < end; i++) { - if (xa_err(xa_store(&kvm->mem_attr_array, i, entry, - GFP_KERNEL_ACCOUNT))) - break; - } + i = kvm_vm_set_region_attr(kvm, start, end, attrs->attributes); KVM_MMU_LOCK(kvm); if (i > start) From patchwork Mon Jun 12 04:25:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275522 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 59382C7EE2E for ; Mon, 12 Jun 2023 04:41:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EB68D6B0072; Mon, 12 Jun 2023 00:41:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E3EDF8E0003; Mon, 12 Jun 2023 00:41:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C920B8E0002; Mon, 12 Jun 2023 00:41:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id B669B6B0072 for ; Mon, 12 Jun 2023 00:41:45 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 847691A027B for ; Mon, 12 Jun 2023 04:41:45 +0000 (UTC) X-FDA: 80892847770.01.EF4DB0F Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2057.outbound.protection.outlook.com [40.107.223.57]) by imf11.hostedemail.com (Postfix) with ESMTP id 6449A4000C for ; Mon, 12 Jun 2023 04:41:42 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=BWWaeu2D; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf11.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.57 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686544902; a=rsa-sha256; cv=pass; b=zYpajLkEQXI4169VNf3FYaQR22aL+Mh1AVUIfl8FsRsQ7iQQTKV7SIIH3NXI0sIhnP13I8 dq0ad/MeyPmGNxroCZ2VaXN1JNcwaYTR+jSQqwZ+N1+OcOs8e4M/Oyl+X07mTRc3jl3OVw HEOi4bNXB/WZDKmLvnFNkH1d1sBxb58= ARC-Authentication-Results: i=2; imf11.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=BWWaeu2D; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf11.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.57 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686544902; 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=yAfVn+Tp6NOaS7IR660oZK9mdnFT1BxHWZYI0Sm7SxM=; b=V6VhIDR61frLrbUC4iS6rH/XTS/CLmgHMHUplMh0uc60etaGJWqLTX0fDeQIK1fy54y+6E mQBXCk6vXq/PM60NjCFScPQVG8EOMWcOOPumAW5HGPPEHqpiayz+M4MAMVGjZNZAuJKN5a tklvz9SnQjKbt3r2LcUl3RvU38N/0/o= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aBI1Lr52kik5DoVeEFjZnJnBcDtWobBQ6DHqA4I8RwXWyZRzY7eAUKN9EnjZECq3dFE4H89zJQ5YyvDpWmYmlm93vhUhlE2D5m0KUhBCdEqL+MG3HSwrGkmIgkWCXqgOjM1g6efA5Ul7nZcu/CwfHPCDkJoO0By/64/B4TftHTgPMpBTkmZUEhfPx91SC3uB2xkWiwvMgmrJnppgZz69q+aKka8QUWvkx5lAgJ3PzAPB0gVkWrHM55SaWjvAUEeOhHanrZyajkYpRqQKaTsY+fMsESXL3+NbZPTVKGKR5ziG2YdCBIt5udvQlb5J7i9XW4lFpsvqRpmrdUhApoGAVQ== 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=yAfVn+Tp6NOaS7IR660oZK9mdnFT1BxHWZYI0Sm7SxM=; b=XCHF/bgKSHNCz4Uq+uDQnaAJsCJQlzm4D9baX+OtAOeKC46JeU7JYQe+citZEwY0Kv0bFCdlZH/qn2tNv97MrmLqVRtL6TFbm5GUHpOiEg5cWioxIKA44s9mwHHTSL9YxbwosD3C0BAvf2oMlyxEr4wBmGrmS9uZwR+EsVQH6NNSlkRHP6QEKTN4FqW6lXN0UTTnIUTtWNexoNA7cxx3AeQzYQ+eEsgnoKwEHYCkTGf0CFtSWd1G8Yu+TQ49kaclRwWboLWeFkRFeHDPkyYCYv2mmi9/aL3MvbP4F5uF9DlLWROwKh8wDm8dJJ/yTdGte6xXZsyIpq4EvsCvmQ2fCQ== 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 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=yAfVn+Tp6NOaS7IR660oZK9mdnFT1BxHWZYI0Sm7SxM=; b=BWWaeu2DQBVs4nicVo2LONo40leKI6SHM09ffbZ9fcbL62f1arEfB2vrnc5SkrtlYHUugnUB0JAxHr8/pqR7E5FTTCKcO+mvjfGuY2+52lmNqgW35obzJrRnGHtaWcGv2OyOEI00ESUzlRSXFJfkBfhe083OZy1VsYP+sRtgzO8= Received: from MW4PR03CA0095.namprd03.prod.outlook.com (2603:10b6:303:b7::10) by PH8PR12MB8605.namprd12.prod.outlook.com (2603:10b6:510:1cc::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.33; Mon, 12 Jun 2023 04:41:39 +0000 Received: from CO1PEPF000042AD.namprd03.prod.outlook.com (2603:10b6:303:b7:cafe::db) by MW4PR03CA0095.outlook.office365.com (2603:10b6:303:b7::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:41:39 +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 CO1PEPF000042AD.mail.protection.outlook.com (10.167.243.42) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:41:39 +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.23; Sun, 11 Jun 2023 23:41:37 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 32/51] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_UPDATE command Date: Sun, 11 Jun 2023 23:25:40 -0500 Message-ID: <20230612042559.375660-33-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CO1PEPF000042AD:EE_|PH8PR12MB8605:EE_ X-MS-Office365-Filtering-Correlation-Id: 6d2609bb-3086-457e-c1cb-08db6aff4ff8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: OeXH5NnRpjhKUb+uvMr3z4QnLoj1z7oAd9Zkjk6leQXQK1I1bxHuPR8BuE+Y5xSmvq/CkD0RbKRZILGZ+DUtyYgh3tHogCYKnSgXWBcijrg5w7Na2Gqer5whDZunyBTP4ENlVUZAvZYUScqCoikVA3lGpB26iCy2w51UHBxFuc8OMcXOLGGRI2+J9296SCbQ0/8rg94sZ6NvqJfgy4SWziEgUHFaq26rkldGYTVnaSvIOGhe9qdOSMHad8RRf9wFdLowPFHe/6os1Xz53ni7PKdUe+pIJhpkIBjTKYnos0abNsCVe/3Di+tmPyLhrO3DF4y6FcGWvaCs1GW/B/DndRfwIMD9LcY5G417RRvLm4NIytYUp+GY/9NJOrOskAoH+X7VULSzaQFq4dLs396huAEr5XHPSgCwk8low7k8s7LqL9Lv86lK4mbdVyJlwkKB5Xa0Sh50XD+AIPoL1GLpCSeolcWwyPOP0gESUtkhJhzqfikJvRGDRRnB2vhm3QpJHvo6hBZT5LL2n3E6OWD/7oyiV2KArOaIFA507xwfaBMqOmZGlkvGoboKyd2F/Wx7OROPNT8nATRPB4WfGCbHkLh+kps4FDOZHDTMWgKZ/pBs9iyxjB1FDVZrZUdy78mRv1B3ntYSi0GxThtJjktjRh2sH9qt2feSJGnwmDaOF0GmsoiChBKFhQlcMU5jgyt6EmJKrqnzWNhcAYMnHGoIlTAkTeUoLZfMPLYzM777NkOVTPggAW7eIfl8Ebut5FyUIxvthIgEhpjlpYNsn5Kb/g== 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:(13230028)(4636009)(376002)(39860400002)(136003)(346002)(396003)(451199021)(40470700004)(46966006)(36840700001)(16526019)(186003)(478600001)(426003)(36860700001)(1076003)(26005)(83380400001)(2616005)(336012)(47076005)(40480700001)(6666004)(2906002)(40460700003)(8676002)(4326008)(316002)(8936002)(86362001)(41300700001)(82310400005)(356005)(82740400003)(70586007)(81166007)(36756003)(44832011)(7406005)(54906003)(7416002)(6916009)(70206006)(5660300002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:41:39.2402 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6d2609bb-3086-457e-c1cb-08db6aff4ff8 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: CO1PEPF000042AD.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB8605 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 6449A4000C X-Stat-Signature: fpk9ctj5s5aic4qtn5qw3yh38m949arc X-HE-Tag: 1686544902-88061 X-HE-Meta: U2FsdGVkX19Rzip6+/jbehDEasuxgmduCk3U5FrxGtkBKJw36jOiJTXod9L9HOXuQfgyvXOOnMemVREmb/f5F2Pqy8VDuGLSNSWTyKPZIGGJIyoxn3nEkfUI65kVyiYCFuCk0EiVxWE7wCjhSdPaIKjuOQmD9E+izw8d+crXtmjLeTKBvu5yw0PHR6wQaMRUd9rlbWAGd46aNRql4U+qp6v9xu/VUl4m7svFJtwfTYSweFXT6lilY5H8BHRiWEfZNYBxsfgYQpuU2ciRgxdrw6II6YdxSMPOOe8LxcQ9R7vgTMpq3GPbFmpVockIE3LsV488xz02f9KW/zvAVDY2URquKKFksZmPFomLpEGiwUQdkylbQoD8scZJ4378KQtzFYGQGwGtgoQALLgBPXQNqg8ofE0sl4qc9UstZMvBXH5Rj6NlWbhi+ipywso92NKF5Zf+2oXI1orjTP5G7G9IAYSUcgtNOjwNsMJw5BGkkG6SzaYZjQo4C/vgUxj1lErsVxIAqdYlIZnlkOAvrwkUsrFRZpUpPFCepHVGnxbUoDZoi8T7bFB3SxRvtFoThi/O//wQ49ytjd+Us5Mp83/nGfrIRXjqIG72IO/9HTSSj1qjbUOMTFURmBrGiA4tOrRBBAJNOrv7kI5CzxKVgapXyglHGUk1ZqJkYizkHJl+yvkEFGH4X7EGfSfaUJBupe0bRiTIIHB/BQJZb3pniIGn4rOJD0X3yFvZHZOEZyRTlBksABikFxWJT0FRcErzPViVvynSYnJb1xfHG0SuM2VgeE+CF4gwhqukJvsZ3bVZjKTqHbYlJmN85AJjljpiT6Kwim7kU0w7D9KQYwUygHwjIR6BbLAqV4P08cfGIuff/4q71uKPDUiMYyzCLpBikkPQkRy5TVILo65X4jNGM6bpR8s2gxorlQgjs9rUZmSNNa99aeb8jVPPCyQ6zeGCrOTHbAdEoCQiSgPS/bkP+ma uFjmEjeM t3Q9FkMUj5kg55AbiGOJB/VK02bQp8sQFzlJ13IRB7MNCsdFQhtmAw9JFshR1STaRf/RrDvSgbpMWFbwKpZ+Tc4WGu6dDu2kGleJ17pN2U0YLpkVui+BepfZc50uOv7yJe4qrbs5e5moqL9zsAHkqmqTzw1sWGqKfCKqWI+UkAaAg+H+Aiw1hFu6TAIfN+3ckdWHqr/Wd0+FiUAMROd8upVVDvW9rr3KGGJq2atf40sHaqEDQjUu8tHAy3iw/W+m/O6otdvfqhqZILFHVl+NhZXGKNAjTU3tW+oZ6LpmTOePgNS+jhAHT86jKNJm6KSlIkxWlxCzxEBxRTcSVg27r7iEEnb5fMfwcBbp3WterJiOME78= 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: From: Brijesh Singh The KVM_SEV_SNP_LAUNCH_UPDATE command can be used to insert data into the guest's memory. The data is encrypted with the cryptographic context created with the KVM_SEV_SNP_LAUNCH_START. In addition to the inserting data, it can insert a two special pages into the guests memory: the secrets page and the CPUID page. While terminating the guest, reclaim the guest pages added in the RMP table. If the reclaim fails, then the page is no longer safe to be released back to the system and leak them. For more information see the SEV-SNP specification. Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- .../virt/kvm/x86/amd-memory-encryption.rst | 28 +++ arch/x86/kvm/svm/sev.c | 189 ++++++++++++++++++ include/uapi/linux/kvm.h | 19 ++ 3 files changed, 236 insertions(+) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documentation/virt/kvm/x86/amd-memory-encryption.rst index 3293e86f9b8a..d8492af09796 100644 --- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst @@ -485,6 +485,34 @@ Returns: 0 on success, -negative on error See the SEV-SNP specification for further detail on the launch input. +20. KVM_SNP_LAUNCH_UPDATE +------------------------- + +The KVM_SNP_LAUNCH_UPDATE is used for encrypting a memory region. It also +calculates a measurement of the memory contents. The measurement is a signature +of the memory contents that can be sent to the guest owner as an attestation +that the memory was encrypted correctly by the firmware. + +Parameters (in): struct kvm_snp_launch_update + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_snp_launch_update { + __u64 start_gfn; /* Guest page number to start from. */ + __u64 uaddr; /* userspace address need to be encrypted */ + __u32 len; /* length of memory region */ + __u8 imi_page; /* 1 if memory is part of the IMI */ + __u8 page_type; /* page type */ + __u8 vmpl3_perms; /* VMPL3 permission mask */ + __u8 vmpl2_perms; /* VMPL2 permission mask */ + __u8 vmpl1_perms; /* VMPL1 permission mask */ + }; + +See the SEV-SNP spec for further details on how to build the VMPL permission +mask and page type. + References ========== diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index e65f3be67c23..6a82767d940f 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -234,6 +234,36 @@ static void sev_decommission(unsigned int handle) sev_guest_decommission(&decommission, NULL); } +static int snp_page_reclaim(u64 pfn) +{ + struct sev_data_snp_page_reclaim data = {0}; + int err, rc; + + data.paddr = __sme_set(pfn << PAGE_SHIFT); + rc = sev_do_cmd(SEV_CMD_SNP_PAGE_RECLAIM, &data, &err); + if (rc) { + /* + * If the reclaim failed, then page is no longer safe + * to use. + */ + snp_leak_pages(pfn, 1); + } + + return rc; +} + +static int host_rmp_make_shared(u64 pfn, enum pg_level level, bool leak) +{ + int rc; + + rc = rmp_make_shared(pfn, level); + if (rc && leak) + snp_leak_pages(pfn, + page_level_size(level) >> PAGE_SHIFT); + + return rc; +} + static void sev_unbind_asid(struct kvm *kvm, unsigned int handle) { struct sev_data_deactivate deactivate; @@ -1953,6 +1983,162 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) return rc; } +static int snp_launch_update_gfn_handler(struct kvm *kvm, + struct kvm_gfn_range *range, + void *opaque) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct kvm_memory_slot *memslot = range->slot; + struct sev_data_snp_launch_update data = {0}; + struct kvm_sev_snp_launch_update params; + struct kvm_sev_cmd *argp = opaque; + int *error = &argp->error; + int i, n = 0, ret = 0; + unsigned long npages; + kvm_pfn_t *pfns; + gfn_t gfn; + + if (!kvm_slot_can_be_private(memslot)) { + pr_err("SEV-SNP requires restricted memory.\n"); + return -EINVAL; + } + + if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) { + pr_err("Failed to copy user parameters for SEV-SNP launch.\n"); + return -EFAULT; + } + + data.gctx_paddr = __psp_pa(sev->snp_context); + + npages = range->end - range->start; + pfns = kvmalloc_array(npages, sizeof(*pfns), GFP_KERNEL_ACCOUNT); + if (!pfns) + return -ENOMEM; + + pr_debug("%s: GFN range 0x%llx-0x%llx, type %d\n", __func__, + range->start, range->end, params.page_type); + + for (gfn = range->start, i = 0; gfn < range->end; gfn++, i++) { + int order, level; + bool assigned; + void *kvaddr; + + ret = kvm_gmem_get_pfn(kvm, memslot, gfn, &pfns[i], &order); + if (ret) + goto e_release; + + n++; + ret = snp_lookup_rmpentry((u64)pfns[i], &assigned, &level); + if (ret || assigned) { + pr_err("Failed to ensure GFN 0x%llx is in initial shared state, ret: %d, assigned: %d\n", + gfn, ret, assigned); + return -EFAULT; + } + + kvaddr = pfn_to_kaddr(pfns[i]); + if (!virt_addr_valid(kvaddr)) { + pr_err("Invalid HVA 0x%llx for GFN 0x%llx\n", (uint64_t)kvaddr, gfn); + ret = -EINVAL; + goto e_release; + } + + ret = kvm_read_guest_page(kvm, gfn, kvaddr, 0, PAGE_SIZE); + if (ret) { + pr_err("Guest read failed, ret: 0x%x\n", ret); + goto e_release; + } + + ret = rmp_make_private(pfns[i], gfn << PAGE_SHIFT, PG_LEVEL_4K, + sev_get_asid(kvm), true); + if (ret) { + ret = -EFAULT; + goto e_release; + } + + data.address = __sme_set(pfns[i] << PAGE_SHIFT); + data.page_size = X86_TO_RMP_PG_LEVEL(PG_LEVEL_4K); + data.page_type = params.page_type; + data.vmpl3_perms = params.vmpl3_perms; + data.vmpl2_perms = params.vmpl2_perms; + data.vmpl1_perms = params.vmpl1_perms; + ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_UPDATE, + &data, error); + if (ret) { + pr_err("SEV-SNP launch update failed, ret: 0x%x, fw_error: 0x%x\n", + ret, *error); + snp_page_reclaim(pfns[i]); + + /* + * When invalid CPUID function entries are detected, the firmware + * corrects these entries for debugging purpose and leaves the + * page unencrypted so it can be provided users for debugging + * and error-reporting. + * + * Copy the corrected CPUID page back to shared memory so + * userpsace can retrieve this information. + */ + if (params.page_type == SNP_PAGE_TYPE_CPUID && + *error == SEV_RET_INVALID_PARAM) { + int ret; + + host_rmp_make_shared(pfns[i], PG_LEVEL_4K, true); + + ret = kvm_write_guest_page(kvm, gfn, kvaddr, 0, PAGE_SIZE); + if (ret) + pr_err("Failed to write CPUID page back to userspace, ret: 0x%x\n", + ret); + } + + goto e_release; + } + } + + /* + * Memory attribute updates via KVM_SET_MEMORY_ATTRIBUTES are serialized + * via kvm->slots_lock, so use the same protocol for updating them here. + */ + mutex_lock(&kvm->slots_lock); + kvm_vm_set_region_attr(kvm, range->start, range->end, KVM_MEMORY_ATTRIBUTE_PRIVATE); + mutex_unlock(&kvm->slots_lock); + +e_release: + /* Content of memory is updated, mark pages dirty */ + for (i = 0; i < n; i++) { + set_page_dirty(pfn_to_page(pfns[i])); + mark_page_accessed(pfn_to_page(pfns[i])); + + /* + * If its an error, then update RMP entry to change page ownership + * to the hypervisor. + */ + if (ret) + host_rmp_make_shared(pfns[i], PG_LEVEL_4K, true); + + put_page(pfn_to_page(pfns[i])); + } + + kvfree(pfns); + return ret; +} + +static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct kvm_sev_snp_launch_update params; + + if (!sev_snp_guest(kvm)) + return -ENOTTY; + + if (!sev->snp_context) + return -EINVAL; + + if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) + return -EFAULT; + + return kvm_vm_do_hva_range_op(kvm, params.uaddr, params.uaddr + params.len, + snp_launch_update_gfn_handler, argp); +} + int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -2046,6 +2232,9 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) case KVM_SEV_SNP_LAUNCH_START: r = snp_launch_start(kvm, &sev_cmd); break; + case KVM_SEV_SNP_LAUNCH_UPDATE: + r = snp_launch_update(kvm, &sev_cmd); + break; default: r = -EINVAL; goto out; diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index b4c7ac9710d3..4961d2e67a4b 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1929,6 +1929,7 @@ enum sev_cmd_id { /* SNP specific commands */ KVM_SEV_SNP_INIT, KVM_SEV_SNP_LAUNCH_START, + KVM_SEV_SNP_LAUNCH_UPDATE, KVM_SEV_NR_MAX, }; @@ -2045,6 +2046,24 @@ struct kvm_sev_snp_launch_start { __u8 pad[6]; }; +#define KVM_SEV_SNP_PAGE_TYPE_NORMAL 0x1 +#define KVM_SEV_SNP_PAGE_TYPE_VMSA 0x2 +#define KVM_SEV_SNP_PAGE_TYPE_ZERO 0x3 +#define KVM_SEV_SNP_PAGE_TYPE_UNMEASURED 0x4 +#define KVM_SEV_SNP_PAGE_TYPE_SECRETS 0x5 +#define KVM_SEV_SNP_PAGE_TYPE_CPUID 0x6 + +struct kvm_sev_snp_launch_update { + __u64 start_gfn; + __u64 uaddr; + __u32 len; + __u8 imi_page; + __u8 page_type; + __u8 vmpl3_perms; + __u8 vmpl2_perms; + __u8 vmpl1_perms; +}; + #define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0) #define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1) #define KVM_DEV_ASSIGN_MASK_INTX (1 << 2) From patchwork Mon Jun 12 04:25:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275523 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 3EEDEC7EE25 for ; Mon, 12 Jun 2023 04:42:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C6BB08E0002; Mon, 12 Jun 2023 00:42:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BF5326B0074; Mon, 12 Jun 2023 00:42:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A6FE38E0002; Mon, 12 Jun 2023 00:42:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 9507D6B0072 for ; Mon, 12 Jun 2023 00:42:30 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 6A97BAF3E7 for ; Mon, 12 Jun 2023 04:42:30 +0000 (UTC) X-FDA: 80892849660.20.8B44B3A Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2070.outbound.protection.outlook.com [40.107.100.70]) by imf16.hostedemail.com (Postfix) with ESMTP id 5ED97180008 for ; Mon, 12 Jun 2023 04:42:27 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=HoTGlsIl; spf=pass (imf16.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.100.70 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686544947; 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=FG6QkCjqXiLpNLeaBHakaidVeB3qMARoqImjyr/5kEc=; b=nh85RTbP3v5IrJxl73VWkFgppOJ5+DXkXAkvTOb7E+kNZeijd5Yo/czkuAMw5aulLaPcrq BxlLpj0BgtTmLYYfqqAMmUUrmsLmqN7PHv20W752K71oilRc9MCe3A8nikCUzUKVPNz5hi 69Apr6vW4QJJHDHn1EErt9Lcjenh1CI= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686544947; a=rsa-sha256; cv=pass; b=rIZIR5cCUHvUcybTn3ua9u7nN5i7Ok5fTT1uqxib+vEDHnSFkp0g7l0CqTUQr3FDew/7wW WHSeji08uZNPGbp25isQLWirX4QA1zD0WrBmKRwQGxU9MEmfjsRNkp3oAJ4TmXnT0qFCGD Vly/QZ8eSSmAT1sKykeTUb0GDUUiuHA= ARC-Authentication-Results: i=2; imf16.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=HoTGlsIl; spf=pass (imf16.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.100.70 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=catWJ6PST+CGnxINGTq2d5U6JAwDFLk3aq53EDSzwU5OVmlvZUeD2+2PXTYMnrdtzXtKTvwBK4izIVGx1K/JZaZ4rLlJeNbJIsMJRDcP1a0+YOHroEIfW4ueHu2U3+E67CY1fwrfewy1dkGC9UNoLryIfSkWGbngHITbHIi8iI2LJzn8ZO9+FAwmR5xVvmjPuRnhKSFWLd6+DY97EgkzinNk1YH18NE22OYlETw2ExkdUFUtJsCheTdhiK5wDi9DSCwcYfVzEZsz+mfqeiTDeAz1pi5DTGpkk1z5LrXjA0PH3Uw4m8l2cnXhGKOYgMoKPEoE4UBm5rDWRKLoRCS1hQ== 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=FG6QkCjqXiLpNLeaBHakaidVeB3qMARoqImjyr/5kEc=; b=Gwn9+NSgzskG8n4coqckBqN6zLJbNblXN/2MaL3QAs3rIAJs7hsozwK2rh0E0DsfNtFYsiaXQbNmlY0lm443qEY8LSWIH0kCf+p+nZR+mHvQ/W7k3cOEFiTAcuBRLStUe5/t2SxG4poSTD/brlPglC7CIj/QHGEvDG5xm68uuT1UnVzh0jCa4xrt/Bo57RH9k730FoGCkRm1zcfBT9dF2VWp2foAu+v+CwRU1LAuqzYOvr+46VuNtOhWbZbA1M7bStppXE10PVRvajeGvOd61byFOv0+17udoM1s4G7Z51v8UgplmeCEbHqlhVRLjAqD1n9CyidhQacA2mOKZocBqQ== 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 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=FG6QkCjqXiLpNLeaBHakaidVeB3qMARoqImjyr/5kEc=; b=HoTGlsIlsnI4JrdCtxy+tcL9r9DAXj3Oap3mpA9qtYJrV+QRPVquVfLgLf6JCwkG5QeUPQnPRfT6BaqerF7tySpNmbrjxVmWc7J27fS+sWnKPpoTXZcuvc/CFCNj3BevNGpj/PbZ06awu4/gCIpXc+5Xn5RGOVsowb0ixD9pr1s= Received: from SJ2PR07CA0004.namprd07.prod.outlook.com (2603:10b6:a03:505::10) by DS7PR12MB6048.namprd12.prod.outlook.com (2603:10b6:8:9f::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.29; Mon, 12 Jun 2023 04:42:23 +0000 Received: from CO1PEPF000042AC.namprd03.prod.outlook.com (2603:10b6:a03:505:cafe::28) by SJ2PR07CA0004.outlook.office365.com (2603:10b6:a03:505::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:42:22 +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 CO1PEPF000042AC.mail.protection.outlook.com (10.167.243.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.13 via Frontend Transport; Mon, 12 Jun 2023 04:42:22 +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.23; Sun, 11 Jun 2023 23:42:19 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Harald Hoyer Subject: [PATCH RFC v9 33/51] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_FINISH command Date: Sun, 11 Jun 2023 23:25:41 -0500 Message-ID: <20230612042559.375660-34-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CO1PEPF000042AC:EE_|DS7PR12MB6048:EE_ X-MS-Office365-Filtering-Correlation-Id: 1944eb23-9154-49d7-7573-08db6aff69de X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Ht8gzIVqf1TWuxzF9gRQOPGEWtDUsvncpha3zyEKdL0NL52JDoxgR2+nVa7y2A+oMVE7j/AmKtMTgTSMnL7mv6oMQQ18bcSRGVGE+mz7ZtOk2rPxjLgMNa5taWd1UWtxOa/tvbivlTCsGxSr5hchq5qqBJUpBTmpjTBhrcDJRKkYaSuIKkWCT8rFGrw/VOEuAoQAnA0LcW/QtYheJwDafdbv0HoYsX7oEgpZW86sATpyPS97bD0fWUOmILMvtreIMYupQkjD+Mpf7VvuoWJcTj2tVGy/WFBIJgTJHj3rPANpimvGP0Htkr7pmYBtgCt8cgqTOdkqJEy0mfPFExVkD/hwsVzizH0yERpzmlyf+Ra+KdKXL25JMG0G3jsts0ud9cZ9ihI5SfvyxD0mH3QMEvDDhavA48yU3ig2sbGeg6pjM6/BTf+HPzXahhvY4s8BmClpHNzNtz11Sm1/f4GbQ/rtmZmHnC9heBAAYXDaScoavxM2lrur4T5Z2S1qiVWxCCBF7EA1ni59275zIt3yX0WFBQZ8e+FAvociyVHoQijxl051ufL7qufEUexJFSUAyyBXq5JA+tLQGCka4y22I1an/YrDtn5g109TUi+QKvOdcp6yNta7bxhrljrI8SIUuBohC3FubMukezBDpurINAHlp1mapmZo+k5lXy+hYN+2e5yBHohJ9/6ltEGjQCerL95QLgdIvzQ+u4QLEkgUHa/+XdyaaZdW3hB3mk3shZ5f9nv4o/Dms0IHkKDlcaL8VNIZ/WzZw8hwzGWw1Og5sg== 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:(13230028)(4636009)(396003)(136003)(376002)(346002)(39860400002)(451199021)(46966006)(40470700004)(36840700001)(316002)(40460700003)(41300700001)(336012)(426003)(82310400005)(83380400001)(86362001)(2616005)(36860700001)(26005)(1076003)(186003)(7416002)(7406005)(2906002)(47076005)(44832011)(16526019)(81166007)(356005)(82740400003)(40480700001)(8676002)(36756003)(5660300002)(8936002)(70586007)(70206006)(478600001)(54906003)(6666004)(4326008)(6916009)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:42:22.7042 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1944eb23-9154-49d7-7573-08db6aff69de 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: CO1PEPF000042AC.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB6048 X-Rspamd-Queue-Id: 5ED97180008 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: fdeiwpjq7qpax7pe9ozg9re4hts6mow8 X-HE-Tag: 1686544947-914889 X-HE-Meta: U2FsdGVkX1/+1ROtF2IpOhhAjodu1iByC4s0jeUdrw09yeGaGckJQKef/E7XE4kByUM3E6Mgri9dcWff9P3IdTw5830sjmjm1oWcIgcymYZiEWouPGJuL19rmGGo+6CUE4EB3E3BgxwfeQ8PMML9/DfSSYRWfhfFQWmciUTK4jTiReuDGfxDUzW4XUKhzwZQqP4KaeLjkw1WUDiIBOupIt0vomI2NKr8w8CCzpVCiXzzvDcWUHaXwccSvk77f9aWZEk20OfBzc7fsEnfYNbwYqMxdoKNI+xbOw0cZeHCB7WDNhXLoc/5XCGm77KR5raXAViGft+wq2Zx1ZzsWc1K3plgV1qDHSyCzwbYAYdjvclZsguW0xOtD3Y6uXs7twwX3X/ODRMCQHFJ7aKWsbBTSME392HWXXnIOfrWqtx+QzIqEDS7qwT+2dTLchGXYz1y4AhTWEUnih9j/iHEQAKF5wGOp5x/OdgvCqnP2ykj6QuVCAmXqj/wsrFJe7t+5RRyi1dSXmDFDe232U+u3paU2KAwGfQc6wJfVIbLw7AmhS4Mc8kInRg9gYv9H2u7uhkcljv2brGz/90LtYvxq9qa93YIqqFbfaBhUZ+m1yaJddp6gHh43tKM6WIIJuJCXo8miYofhhXI7KwBBXw5o6O2LPSAA2M36hp+i1D8UipCJcEHgiOeNj3aUDpNMinQJ7sXD5BEQIs2Wtj0A3TLg7cebnlxabFGqvBQxEi4gtLdudkZq2AY97msS1UYAfxfIr9mDai7L8JaIWODJefuZl59Nii+u4vAOeOLcqwB7GcpKEF9HT9vK1KZgCYmH/HbHZwaiXSzmSUefwi6gFQPuMi9dlXT79VLBAtj5PnYn/hasVAZIB0lXWlZ3/J0sLxTDrrqF6KxaU7+/cFgO8182Ljr2Zi+tkXe00DBOnO9Md8gyDEiR9U9LblZvrudas96PgzDWFcRP/hrsHhhssBawSG IJhS3WCm 4IQEPgdX9WTRixrGM6UQzM4iM2ppP64PrzDFW4ya/VSwjcYyapa16yB58QstJhNAV/5uUkMv2+NWOXQ9tn2FircnfBcgWmos8Tk+WtfRTG73tx92GG24CudfAHFmq3vZAOKGZY7X9wY7TbU+7MP/bJpgl8Pi4Ak8TFdhTu5ij6ieU1op7xYUZKOzv8RU1bxscw95jOx7WQLsrfDkOu+qLVwYCNQD111X8YM0JNWhSnyBth67atJmXWKlipGq+AKM1FjmZaOq9gXOGPsknenypnt5g9ccskIydjCiHxBfJo3nHey00+DyVWXeJ6BrSoXVFlQpFUUAnsjmLi2WekmbB5kM08wC+UuqHc73Ry5yxUSV0NeJz2drY3NTcEuxxz/uEP20meZu1J56sqADsVVwHcfpCXt/W93XdS9f0 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: From: Brijesh Singh The KVM_SEV_SNP_LAUNCH_FINISH finalize the cryptographic digest and stores it as the measurement of the guest at launch. While finalizing the launch flow, it also issues the LAUNCH_UPDATE command to encrypt the VMSA pages. If its an SNP guest, then VMSA was added in the RMP entry as a guest owned page and also removed from the kernel direct map so flush it later after it is transitioned back to hypervisor state and restored in the direct map. Signed-off-by: Brijesh Singh Signed-off-by: Harald Hoyer Signed-off-by: Ashish Kalra [mdr: always measure BSP first to get consistent launch measurements] Signed-off-by: Michael Roth --- .../virt/kvm/x86/amd-memory-encryption.rst | 24 +++ arch/x86/kvm/svm/sev.c | 146 ++++++++++++++++++ include/uapi/linux/kvm.h | 14 ++ 3 files changed, 184 insertions(+) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documentation/virt/kvm/x86/amd-memory-encryption.rst index d8492af09796..cd77a19577fe 100644 --- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst @@ -513,6 +513,30 @@ Returns: 0 on success, -negative on error See the SEV-SNP spec for further details on how to build the VMPL permission mask and page type. +21. KVM_SNP_LAUNCH_FINISH +------------------------- + +After completion of the SNP guest launch flow, the KVM_SNP_LAUNCH_FINISH command can be +issued to make the guest ready for the execution. + +Parameters (in): struct kvm_sev_snp_launch_finish + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_snp_launch_finish { + __u64 id_block_uaddr; + __u64 id_auth_uaddr; + __u8 id_block_en; + __u8 auth_key_en; + __u8 host_data[32]; + __u8 pad[6]; + }; + + +See SEV-SNP specification for further details on launch finish input parameters. + References ========== diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 6a82767d940f..a7cbdc24ccdb 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -66,6 +66,8 @@ static bool sev_snp_enabled; #define AP_RESET_HOLD_NAE_EVENT 1 #define AP_RESET_HOLD_MSR_PROTO 2 +#define INITIAL_VMSA_GPA 0xFFFFFFFFF000 + static u8 sev_enc_bit; static DECLARE_RWSEM(sev_deactivate_lock); static DEFINE_MUTEX(sev_bitmap_lock); @@ -727,7 +729,29 @@ static int sev_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp) if (!sev_es_guest(kvm)) return -ENOTTY; + /* Handle boot vCPU first to ensure consistent measurement of initial state. */ + kvm_for_each_vcpu(i, vcpu, kvm) { + if (vcpu->vcpu_id != 0) + continue; + + ret = mutex_lock_killable(&vcpu->mutex); + if (ret) + return ret; + + ret = __sev_launch_update_vmsa(kvm, vcpu, &argp->error); + + mutex_unlock(&vcpu->mutex); + if (ret) + return ret; + + break; + } + + /* Handle remaining vCPUs. */ kvm_for_each_vcpu(i, vcpu, kvm) { + if (vcpu->vcpu_id == 0) + continue; + ret = mutex_lock_killable(&vcpu->mutex); if (ret) return ret; @@ -2139,6 +2163,109 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) snp_launch_update_gfn_handler, argp); } +static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_snp_launch_update data = {}; + struct kvm_vcpu *vcpu; + unsigned long i; + int ret; + + data.gctx_paddr = __psp_pa(sev->snp_context); + data.page_type = SNP_PAGE_TYPE_VMSA; + + kvm_for_each_vcpu(i, vcpu, kvm) { + struct vcpu_svm *svm = to_svm(vcpu); + u64 pfn = __pa(svm->sev_es.vmsa) >> PAGE_SHIFT; + + /* Perform some pre-encryption checks against the VMSA */ + ret = sev_es_sync_vmsa(svm); + if (ret) + return ret; + + /* Transition the VMSA page to a firmware state. */ + ret = rmp_make_private(pfn, INITIAL_VMSA_GPA, PG_LEVEL_4K, sev->asid, true); + if (ret) + return ret; + + /* Issue the SNP command to encrypt the VMSA */ + data.address = __sme_pa(svm->sev_es.vmsa); + ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_UPDATE, + &data, &argp->error); + if (ret) { + snp_page_reclaim(pfn); + return ret; + } + + svm->vcpu.arch.guest_state_protected = true; + } + + return 0; +} + +static int snp_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct kvm_sev_snp_launch_finish params; + struct sev_data_snp_launch_finish *data; + void *id_block = NULL, *id_auth = NULL; + int ret; + + if (!sev_snp_guest(kvm)) + return -ENOTTY; + + if (!sev->snp_context) + return -EINVAL; + + if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) + return -EFAULT; + + /* Measure all vCPUs using LAUNCH_UPDATE before finalizing the launch flow. */ + ret = snp_launch_update_vmsa(kvm, argp); + if (ret) + return ret; + + data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); + if (!data) + return -ENOMEM; + + if (params.id_block_en) { + id_block = psp_copy_user_blob(params.id_block_uaddr, KVM_SEV_SNP_ID_BLOCK_SIZE); + if (IS_ERR(id_block)) { + ret = PTR_ERR(id_block); + goto e_free; + } + + data->id_block_en = 1; + data->id_block_paddr = __sme_pa(id_block); + + id_auth = psp_copy_user_blob(params.id_auth_uaddr, KVM_SEV_SNP_ID_AUTH_SIZE); + if (IS_ERR(id_auth)) { + ret = PTR_ERR(id_auth); + goto e_free_id_block; + } + + data->id_auth_paddr = __sme_pa(id_auth); + + if (params.auth_key_en) + data->auth_key_en = 1; + } + + memcpy(data->host_data, params.host_data, KVM_SEV_SNP_FINISH_DATA_SIZE); + data->gctx_paddr = __psp_pa(sev->snp_context); + ret = sev_issue_cmd(kvm, SEV_CMD_SNP_LAUNCH_FINISH, data, &argp->error); + + kfree(id_auth); + +e_free_id_block: + kfree(id_block); + +e_free: + kfree(data); + + return ret; +} + int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -2235,6 +2362,9 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) case KVM_SEV_SNP_LAUNCH_UPDATE: r = snp_launch_update(kvm, &sev_cmd); break; + case KVM_SEV_SNP_LAUNCH_FINISH: + r = snp_launch_finish(kvm, &sev_cmd); + break; default: r = -EINVAL; goto out; @@ -2695,11 +2825,27 @@ void sev_free_vcpu(struct kvm_vcpu *vcpu) svm = to_svm(vcpu); + /* + * If its an SNP guest, then VMSA was added in the RMP entry as + * a guest owned page. Transition the page to hypervisor state + * before releasing it back to the system. + * Also the page is removed from the kernel direct map, so flush it + * later after it is transitioned back to hypervisor state and + * restored in the direct map. + */ + if (sev_snp_guest(vcpu->kvm)) { + u64 pfn = __pa(svm->sev_es.vmsa) >> PAGE_SHIFT; + + if (host_rmp_make_shared(pfn, PG_LEVEL_4K, true)) + goto skip_vmsa_free; + } + if (vcpu->arch.guest_state_protected) sev_flush_encrypted_page(vcpu, svm->sev_es.vmsa); __free_page(virt_to_page(svm->sev_es.vmsa)); +skip_vmsa_free: if (svm->sev_es.ghcb_sa_free) kvfree(svm->sev_es.ghcb_sa); } diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 4961d2e67a4b..1fb6a6615d09 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1930,6 +1930,7 @@ enum sev_cmd_id { KVM_SEV_SNP_INIT, KVM_SEV_SNP_LAUNCH_START, KVM_SEV_SNP_LAUNCH_UPDATE, + KVM_SEV_SNP_LAUNCH_FINISH, KVM_SEV_NR_MAX, }; @@ -2064,6 +2065,19 @@ struct kvm_sev_snp_launch_update { __u8 vmpl1_perms; }; +#define KVM_SEV_SNP_ID_BLOCK_SIZE 96 +#define KVM_SEV_SNP_ID_AUTH_SIZE 4096 +#define KVM_SEV_SNP_FINISH_DATA_SIZE 32 + +struct kvm_sev_snp_launch_finish { + __u64 id_block_uaddr; + __u64 id_auth_uaddr; + __u8 id_block_en; + __u8 auth_key_en; + __u8 host_data[KVM_SEV_SNP_FINISH_DATA_SIZE]; + __u8 pad[6]; +}; + #define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0) #define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1) #define KVM_DEV_ASSIGN_MASK_INTX (1 << 2) From patchwork Mon Jun 12 04:25:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275524 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 2CE84C7EE2E for ; Mon, 12 Jun 2023 04:43:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C93EE8E0003; Mon, 12 Jun 2023 00:43:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C1BEE6B0074; Mon, 12 Jun 2023 00:43:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A96018E0003; Mon, 12 Jun 2023 00:43:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 991066B0072 for ; Mon, 12 Jun 2023 00:43:11 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 6AE8BAF3F3 for ; Mon, 12 Jun 2023 04:43:11 +0000 (UTC) X-FDA: 80892851382.06.36757ED Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2043.outbound.protection.outlook.com [40.107.243.43]) by imf25.hostedemail.com (Postfix) with ESMTP id 78F99A0005 for ; Mon, 12 Jun 2023 04:43:08 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=sJeOJvOV; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf25.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.243.43 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686544988; a=rsa-sha256; cv=pass; b=F13j5DPLVOeYdIP64dZ1gpBchFr7VvpqYvz/5FVMVDZmY2320XSweDBWfx64zvGYoPSk/i x6hsgeXvVeXC+xcuItUNsOae+nQXeq/Y9mbo3UcKpaoSFqIExrQVnVQ1V24gQTKgVrbKXq BqpKhWeq+sOpLyjHEFgdqS2QBPVi7R8= ARC-Authentication-Results: i=2; imf25.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=sJeOJvOV; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf25.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.243.43 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686544988; 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=8o93p9mYoqybhgkAygPC0Obm+t8oGE9xTvNcEMl5QmM=; b=jvP+1wOlVHXVIclYNDUndEOIcnVF1gaSpkSGpc/D/O88U92KlMDUmRJ5K7xxFeh9qwWxZX Kl5MwW0ge3zomiQ0w4JBGmFuIOW+FtFUhTVIU4bdu0PJg+BnIcc8nqmbkwJmJz3lmRmoIA bWuMkx5/07mAcNNz5LSpJEYKQEBkA1o= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=O12/pME0uzjckk/ABw6QRC4tnn3lbuYvnXvz9qqT69vKlBPvuW6B2oDifguCp5TKopURcv+wdNufKXytJQUEWENeyohdiWeDSD3XwTQ0X2Twh56saQON8k8tn3NPgsUoB9+Nyf9KY8h5gjT4qu/QoDgpvDZaHshQPJPoj0+aJhbE2jTTXHlNT+Ngup04a9IK4gSZYf8DENeBaJZJePRlypn7ix7dYhDTcBcnbDciP0rrBPRjfG8gfKR96ZNJB4rtgNjgrcQGdiedTTqGP/YweBonTP/k2oiVtgktydCNXy6o4vg3jfEWkIic1UJW0wynXFAdmAzoUFZy4dJ6/5PGEw== 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=8o93p9mYoqybhgkAygPC0Obm+t8oGE9xTvNcEMl5QmM=; b=APqRrqIBYBUQcXOMV4k/cxzuQ4hSe7cJSBvLqLZEJhjVgp+8T7GdRRRBuB2J66SG78XIvNG38q67t+0TSecOdfnlbbgQ8XGEkoMe0hKW3C7gnafCl+8J+vrZHrxg4jb740KTrFCNLV9DVgzNZXt0EAncem99WHwRFXKzrzJx8iLLeeDyAP8TbViiruWoTAYrLYm4yQL2eLKkqV4H5Nyngutap99tkEbFuKhhJghXwaviHzaywqw16gfeZUESxur8tfjL1TOCp9LueHR2+3UBEJXhCoF8x/iEfyTeDiBKSKz3esPp/gqw6ToHr8bz1TFggYK6ApuwyblWE1mOHEr6BQ== 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 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=8o93p9mYoqybhgkAygPC0Obm+t8oGE9xTvNcEMl5QmM=; b=sJeOJvOVTtkjsMBo/7ie9TkKrFWihO4tOBUERnY7dAm//EW2LCRV16bQ1/Vuwej4icA2lO2FT7jO/Uw4r53MgKxMPJaXfenB4T9JcxHg7UVMmty4y/tPhZytVaJ8YLC3+h1z6dhYDsSYQUMw5ZmFzlD8xws3WsnyfQaPezoMAn8= Received: from DS7PR05CA0056.namprd05.prod.outlook.com (2603:10b6:8:2f::17) by DM4PR12MB6591.namprd12.prod.outlook.com (2603:10b6:8:8e::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.42; Mon, 12 Jun 2023 04:43:06 +0000 Received: from DM6NAM11FT024.eop-nam11.prod.protection.outlook.com (2603:10b6:8:2f:cafe::2e) by DS7PR05CA0056.outlook.office365.com (2603:10b6:8:2f::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:43:06 +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 DM6NAM11FT024.mail.protection.outlook.com (10.13.172.159) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:43:05 +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.23; Sun, 11 Jun 2023 23:43:03 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 34/51] KVM: SVM: Add support to handle GHCB GPA register VMGEXIT Date: Sun, 11 Jun 2023 23:25:42 -0500 Message-ID: <20230612042559.375660-35-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: DM6NAM11FT024:EE_|DM4PR12MB6591:EE_ X-MS-Office365-Filtering-Correlation-Id: 46dfd1bf-083e-4c81-62bc-08db6aff838a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xbR7swrgqngFy9fYN9jwKBTPShikayOIVylnXb1sUAyy/9mAA60vMeabHj2cu2WcJPmA70IJn/llAtuqXZi/0LgVJ/UeY8rT/aCf5hP0O9WXmCA4PUvEPKXyvK6LilGDQ6S6tzsus6EJig0Ei2Z9KxJRSoS3ysPlzBDx5Tv9j6VVBU2aInYA6zHR7tsOc0E5AbpM+u5G86pCxCzSjF1ChQgydywnVDuxhi5JRZt+nsuWzW8IU95+Ow0zn6tjkHV02YGOlnV4bzbMY+aUtZ4hytjC4V2O89zEP+Nx6FNcW3Nfwh71EMeuUiXGA/5Q0IBbRYPaklbYy+rfSdLT0o+Em0Q0t2dT3pjLNj3e94cJaaMGMz3RQC+qPe0oteTjDq1PI30n3B/89SDe8tu0FJogbTMmtJEjhoUZBPby2VVL6hh47yM4vdLKjVWQsmM3gtGAO7QHdTceznZfV4/A6Y8/mfEklKHthXWXfIRin/Z4jt/vQL/MnCdr3wrbO7d32iVV1PClT4G8P8Hh5p+Jw1aPpx06RyJy4shN2maASRpGFljZMykbw79EyyzK/QfIPbTCXC3kJ7lHGzCI8uOgiWxaEQOOoOnVhFlYlTb2Y1J5uRqTLbLsITJBegPuTqc0zUhh9/L/L5ZXQzBNSfM0yU6kLERCB7VaGakT8XH1dYkUA8TST4ReWEl6RFGd/m3PmCChUAd/mQBWBE/CV50NuWQQ7p6danGEF/9RECdKSCPBfQEIS5+B4j9Jpg5Ws/JjCWV+m4eIXGOdTI7wDoTYTEGhHA== 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:(13230028)(4636009)(136003)(39860400002)(396003)(346002)(376002)(451199021)(46966006)(36840700001)(40470700004)(36756003)(47076005)(478600001)(54906003)(6916009)(4326008)(70586007)(6666004)(316002)(70206006)(86362001)(40480700001)(8676002)(8936002)(5660300002)(41300700001)(2906002)(44832011)(82310400005)(7416002)(356005)(7406005)(81166007)(82740400003)(2616005)(426003)(1076003)(26005)(16526019)(186003)(83380400001)(36860700001)(336012)(40460700003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:43:05.8365 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 46dfd1bf-083e-4c81-62bc-08db6aff838a 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: DM6NAM11FT024.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB6591 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 78F99A0005 X-Stat-Signature: dcf1en3exfozrixsszu9m6xrqg4p38gp X-HE-Tag: 1686544988-1796 X-HE-Meta: U2FsdGVkX1/rStYcG+Xpkt7MQNgwqud5cr4EW/zYCBP/AicW46WSzEfbpTo1xsMV3ZOUhrELH/6fykDrru9ln6Hmid+glWK/+IrBM/nRwh5FQ12JjZy7u863suNDLf3z+HuiBFFTNYPf8K2RcABstg48/QagDPs98HDxRk+54qywrwNIJlwwxrDOSqJpcWPp89CU7bZjIQ6bA607FenzpLsLasjHgndeeFO8VRrXXg3F/UvSLtSHNQeQAsgeBM0K4QO+GOXASgvRjKbH1opjQ1uUEzy0wDWk5uAph8nVgjC0CAxCPsxxCFZbfr8gZ8Z+9khY7Ul7ojpNijQJRF3N0QBpE0p4d/IlYtTM3OWYTDUsGlUvPKR9KIdhsxSY4M9c4emOiVv5sZ/fhp8vck3uLXk+bbe0ZiJsNKbhkOtpCZAMCn3d+38BmhdSwxBXqLE5AWyi3nm/aUROJPy+atrtAoUtdUlJivTW7GVIPpn2mzV5oNW9s5JBiGTQHE96dIwj5vO/OxVzgeyeRaM6VGaCna0LRbJb2GZV7GF9w2DkFe0kVy4QwrFQK1epVE/AOy/BXFU63f2mqLJc6TWjSxHodBHnuNnVXbdcZkpE89bp8QFzJIQNu8kNMEFlrDJvkVRi8pAc3LkvkKQQLj+W5HGS4QD7mjPjHurVYbrXMzpVqndC9aVAwUsJy4iImBHiK3KLewtKu8LUXP9qlmnbKZyHfZnCNZPpvPctH8XVoiZ0nmHBVYrSX7SJeZkeRwD67lQyaKOs8G3oY7DA1GTt8mR+GZyfV5M25nnPanh6fCDcKx3NsJ9YV1MPDlyunY5QIWtQfuQ6lLDVxNKZkonZmXofNBanphdrwAG/T/WP+qSFR15QpMHwgulaI/VeibTnu93/+Jv+UAWjX0CneL6yXiW2iFjxsGO+d/CxIqqHf/kdToXbKzC01WLdHQSye5xYl/dyVvOPcnT6A8JrynzL4d7 ELZlykxl dGRu7NNhZ102/wgUyvgEQxB2vytVzdXwahyuzbNIR+v6LyhJZqKEKpMcSKDV1PJgwCvOLOcxFhtUV4n5kF+hgNpbvg0BAhhsgaSm2zkS/UQ6jpvAhYEVs8ePWU+WEKhwCAKIPALxDttKnwDx0yumK6uk4kpWiTvcjJDREL9h36P8SCxbdgd+fFY2t0qE3de5W5/2VfhSI2Str7BPlCzK6lWO7Uwe6EU0m7xE3aPZz3Xy7AtkDFxycYJ0oPj8KiIXGS6gZB2vs4pfrkummncfZuNbEV1dpfq6ATWYvJMAspxlPsBTLJZHDWqRe9F9hba/knuhMmx/G+zOqNh30R07MRnn8Fg== 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: From: Brijesh Singh SEV-SNP guests are required to perform a GHCB GPA registration. Before using a GHCB GPA for a vCPU the first time, a guest must register the vCPU GHCB GPA. If hypervisor can work with the guest requested GPA then it must respond back with the same GPA otherwise return -1. On VMEXIT, Verify that GHCB GPA matches with the registered value. If a mismatch is detected then abort the guest. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/include/asm/sev-common.h | 8 ++++++++ arch/x86/kvm/svm/sev.c | 27 +++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.h | 7 +++++++ 3 files changed, 42 insertions(+) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index aaea4afcda98..d499730e1f15 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -59,6 +59,14 @@ #define GHCB_MSR_AP_RESET_HOLD_RESULT_POS 12 #define GHCB_MSR_AP_RESET_HOLD_RESULT_MASK GENMASK_ULL(51, 0) +/* Preferred GHCB GPA Request */ +#define GHCB_MSR_PREF_GPA_REQ 0x010 +#define GHCB_MSR_GPA_VALUE_POS 12 +#define GHCB_MSR_GPA_VALUE_MASK GENMASK_ULL(51, 0) + +#define GHCB_MSR_PREF_GPA_RESP 0x011 +#define GHCB_MSR_PREF_GPA_NONE 0xfffffffffffff + /* GHCB GPA Register */ #define GHCB_MSR_REG_GPA_REQ 0x012 #define GHCB_MSR_REG_GPA_REQ_VAL(v) \ diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index a7cbdc24ccdb..44fdcf407759 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3312,6 +3312,27 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) GHCB_MSR_INFO_MASK, GHCB_MSR_INFO_POS); break; } + case GHCB_MSR_PREF_GPA_REQ: { + set_ghcb_msr_bits(svm, GHCB_MSR_PREF_GPA_NONE, GHCB_MSR_GPA_VALUE_MASK, + GHCB_MSR_GPA_VALUE_POS); + set_ghcb_msr_bits(svm, GHCB_MSR_PREF_GPA_RESP, GHCB_MSR_INFO_MASK, + GHCB_MSR_INFO_POS); + break; + } + case GHCB_MSR_REG_GPA_REQ: { + u64 gfn; + + gfn = get_ghcb_msr_bits(svm, GHCB_MSR_GPA_VALUE_MASK, + GHCB_MSR_GPA_VALUE_POS); + + svm->sev_es.ghcb_registered_gpa = gfn_to_gpa(gfn); + + set_ghcb_msr_bits(svm, gfn, GHCB_MSR_GPA_VALUE_MASK, + GHCB_MSR_GPA_VALUE_POS); + set_ghcb_msr_bits(svm, GHCB_MSR_REG_GPA_RESP, GHCB_MSR_INFO_MASK, + GHCB_MSR_INFO_POS); + break; + } case GHCB_MSR_TERM_REQ: { u64 reason_set, reason_code; @@ -3378,6 +3399,12 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) exit_code = ghcb_get_sw_exit_code(ghcb); + /* SEV-SNP guest requires that the GHCB GPA must be registered */ + if (sev_snp_guest(svm->vcpu.kvm) && !ghcb_gpa_is_registered(svm, ghcb_gpa)) { + vcpu_unimpl(&svm->vcpu, "vmgexit: GHCB GPA [%#llx] is not registered.\n", ghcb_gpa); + return -EINVAL; + } + ret = sev_es_validate_vmgexit(svm); if (ret) return ret; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 9a7cafb018fe..02edbdd443e4 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -205,6 +205,8 @@ struct vcpu_sev_es_state { u32 ghcb_sa_len; bool ghcb_sa_sync; bool ghcb_sa_free; + + u64 ghcb_registered_gpa; }; struct vcpu_svm { @@ -359,6 +361,11 @@ static __always_inline bool sev_snp_guest(struct kvm *kvm) return sev_es_guest(kvm) && sev->snp_active; } +static inline bool ghcb_gpa_is_registered(struct vcpu_svm *svm, u64 val) +{ + return svm->sev_es.ghcb_registered_gpa == val; +} + static inline void vmcb_mark_all_dirty(struct vmcb *vmcb) { vmcb->control.clean = 0; From patchwork Mon Jun 12 04:25:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275525 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 13927C7EE2E for ; Mon, 12 Jun 2023 04:44:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A965C6B0072; Mon, 12 Jun 2023 00:44:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A46AB8E0005; Mon, 12 Jun 2023 00:44:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8E8508E0002; Mon, 12 Jun 2023 00:44:24 -0400 (EDT) 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 7FB3E6B0072 for ; Mon, 12 Jun 2023 00:44:24 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 4502B120260 for ; Mon, 12 Jun 2023 04:44:24 +0000 (UTC) X-FDA: 80892854448.26.639A33C Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by imf21.hostedemail.com (Postfix) with ESMTP id 55C861C0011 for ; Mon, 12 Jun 2023 04:44:21 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=K8tMeCgF; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf21.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.41 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686545061; a=rsa-sha256; cv=pass; b=2bC9mBZDHX+WwvMBQIkQtNpmb6qdnDArECNHORN4tFAXoLc1hQKqZ109WK9Np8nfM79iTg +ASQh+L3FLoxexD0BuXlHu2xcdeIBPxjMHN+PDI7DErS3fsflOS0WuBwwcbqvVYElvZJqZ gHf2eDCriJxojCpCZ/KrFHUDb1VXnUk= ARC-Authentication-Results: i=2; imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=K8tMeCgF; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf21.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.41 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686545061; 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=sWai5t1V65lb7xUaoMrw0zZrJ8os1/1Uqdh6BDEyZfA=; b=aMA/uCCqLlL/aZiAKjU0dySCMDAWNK469g2JzgfFgibU1vCcuuoCPF78n1/3Ug/OPzHtAz NjQgvJf8c9826Nhco6a0ePE8O+DpfKE1koA7kNzLjFSFPEl/cLQZXO2PX9e6UzNPLMnuCw L6A+8Bjcy2icCh4JRRizb7inAwHx65g= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OoQiZSZJ3evbsB4MiGembIQ7pv8R7U/436c8hr7YAvDO789SmV8AUynOdBah55yBydVju8QIg9Nwibi+WVNrDELrx1dvrWSM+4V5gZcY0A3qhvhpdEATK2A+lzcsyJf2N9Y1o509P+mu5RQ08ybW/vC/3YwM0andkNXQrVwGIt66HMsaRczXs7WzJpYmADjB683HNuS2KQRpIR1BgtmNUdglrGrKZjQXMQeqPUmiOP8s/idnP2dzl12H+4CFJg+gUAJ8sIrh8jPh+yjapmrdwMO5qhjC77JeIJdJ6bKJaSR7oO8YrJkAtfCmaFw/dnza7/lhUynvA9k9KOJs8O6KfQ== 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=sWai5t1V65lb7xUaoMrw0zZrJ8os1/1Uqdh6BDEyZfA=; b=bnENNvoCuZ/PBvT8LNOfdIxi8coiDRoNatr7wmXXr07a/NJDqYQXFwNLIBQhMxozTDum44CzfIAtmM2BqAzJPX3hQW/MI/1OzIiW/FY/waAobABIWJxFkZycS15XEk8GMRy+IG3IS4W1xeSUIXJ/D5KejN9F+Wg/vOga49fDzVuFMUZmyedwsSTxGT7p84v2MY3btwnliIrBWcI42lZCOt29lgMCGVUtyO+GyCOjamSIAmVGiOGall7JTanYNt+cloTURfQp8Bdk14GLJYxly5n8ErNBCGNdlrVd5iOaCUAGDqkgfNGvB0GwvawewYW90S6m76Wu5rHzCTP3Nftm9w== 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 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=sWai5t1V65lb7xUaoMrw0zZrJ8os1/1Uqdh6BDEyZfA=; b=K8tMeCgFyBOgP4/qhbuVKWfPNXd0OfLuZJUOqYzqac/HGZqjVamSEMmgdE/XpDdCN4GpEYBwRGm80YX4KmUEe1/TdU+TJVoj4kcfnWZtAek6gThR1IGx8t8+EChwCWoM8WxZL3doVHfEfaRIwC43xyQ+mlPcsrxGMUfpjXmPUnI= Received: from DS7PR03CA0238.namprd03.prod.outlook.com (2603:10b6:5:3ba::33) by CY8PR12MB7241.namprd12.prod.outlook.com (2603:10b6:930:5a::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.44; Mon, 12 Jun 2023 04:44:18 +0000 Received: from DM6NAM11FT092.eop-nam11.prod.protection.outlook.com (2603:10b6:5:3ba:cafe::39) by DS7PR03CA0238.outlook.office365.com (2603:10b6:5:3ba::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:44:18 +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 DM6NAM11FT092.mail.protection.outlook.com (10.13.173.44) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.34 via Frontend Transport; Mon, 12 Jun 2023 04:44:02 +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.23; Sun, 11 Jun 2023 23:43:56 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 35/51] KVM: SVM: Add KVM_EXIT_VMGEXIT Date: Sun, 11 Jun 2023 23:25:43 -0500 Message-ID: <20230612042559.375660-36-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: DM6NAM11FT092:EE_|CY8PR12MB7241:EE_ X-MS-Office365-Filtering-Correlation-Id: eb2150d7-2116-484d-f9f4-08db6affae20 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: b98vSCAvPfVoSDdb4x4k8fKxjgS4WnROpmXnLYSppgfc1XsG7iGt3BP5JsWgnMa3HwjjHhgPfpCvRrTBZOo2tpu7bjFx5xAgFans2DQCuDz4ZCW74W3DZjBp8JcGKaeYhGix0SEs7ugNXjF8rylL+I4aag6pP/rLOtm5cpQ0SfW476D5mZ52GyX+gbcoKJ6pIOFW+qVClK84wJgTHUhziVA3NjqgnkeQRbTuc36TxQK84nuVAfyF4SxpPL56+9XR9sglYudQQA3vOtePlIjYMRatAIV/IkZG+LsSuNgI6P9O18/GwxUnnrokhyQNUMUSrnKbd3T/uYzlTBag9ZhMeww9OpryX9kPDNjb85a2gg4Xnb3glNsTHHtZV8N1WVT0L6mM2BScqnSx2ozaQfMdnRmJW715IVCA3qYII7cXGNi4aSZeJgEKeA9ZTFWwZyXCo9FnSAic4ZHDPdWf1tcRqFmrfJIjq981xnA2GrCyZf8YuheCSsRvTZUetkz6s0yTJ6IX7m3BXxm6ERNyrvwGUMge+rXcCOWSnUIZJQ6Z0SIk3GSTNr4zbPojguEH7/vueyA9CZ5PfH3P6Tqcx7VfhNjNZUQT1/ZJ7hi4ZKBVx04LjhRGRLf8+WgXlIH4mbLu9y7jrD3OtFyFrBZsgPTjeRRnI4IrmsHwRoKl0wnxW4PYhp+DQXqNOB+GzFWsjdwIeaP9yU+rPIvHIaGSMOjqVLRWHV0WDtRBAGm3/25CUnOI6kKcux+5Wxfq/qcuyaW3LikerNZxWFd7pXAofze/yw== 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:(13230028)(4636009)(39860400002)(376002)(396003)(346002)(136003)(451199021)(36840700001)(46966006)(40470700004)(426003)(336012)(2616005)(47076005)(83380400001)(36860700001)(356005)(82740400003)(40480700001)(36756003)(86362001)(82310400005)(81166007)(40460700003)(478600001)(54906003)(44832011)(6666004)(8936002)(8676002)(2906002)(5660300002)(7416002)(7406005)(70206006)(70586007)(4326008)(6916009)(41300700001)(316002)(186003)(16526019)(1076003)(26005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:44:02.6940 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: eb2150d7-2116-484d-f9f4-08db6affae20 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: DM6NAM11FT092.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7241 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 55C861C0011 X-Stat-Signature: 7nk1g1zuputnr63tksyrat7bnqnqzoei X-HE-Tag: 1686545061-856155 X-HE-Meta: U2FsdGVkX1/atSvzubUhPzI7ssi9yyZ5mjJlLCiLbxVNiE9kPf3kKxbDtyGmepmyjAoIfzUQX/Ye+FLuUNEOpOjGrCWmpWhWFHPfjRCiX9Och+YKtOJ6XE4PwYiUW2SYstOCKMkt0abHDbKZfcuzjApU3tg1FNYpu0SIlZA7wtVcIHxZ6PjT6aU3tATqZ9cpXDtXdgA/OGd9++aIY95f0OPEJfR+yeeEkgCQZ4gQfQvkc3mHrEcOSE5OHFJRgJ8h6R6n0auJbilnG/TSul4Lm94LWI6P7c7rDPZfjxzPmpD8fKvfLJlWwjBTFJyQ890Zh6Hf2Tpa7GeVM/XM4vrvd7to1EhIioY2QCc/vZjjRuxK2EnqqlKZGAJ1GlmHJvq29+56UkOeDDLf+ZhasAGwS6M9GxKbeh9w/mnYLECbBugx3GY7ZYQSKXb2U+97drar4HvMHyaGyP2Ed+cDfuNVx7K9qBF99x4VnUveipCPZjPkj6vM8zW94wTVWGvToPMEFkcMZS9qAQZ4Fel7DRveYsKmVjfC+O++35iklcVPQh0fhg04mxA9/uncVgNUiWLjC/i8W8XRJSJUWDl8TVHWlJyF7UxOPK4zQKqLOYg5nlxl0poah1Sa8DHRvTlVhI4GywERWihvoAu1ErNHrzlJr4Ct5l7jLTBVeOb+BNwlS11briMINEVW60Icy6wBxitywtKK7JAmc3mqph5IQvWdGJp90VCk7wQKAyW3am13Zge5GQY5a9ZvHUJqHfBkW4TkPmPC+/GOITmmlcU8CenOB6MqcK2IaVjS37FEmyz46urHgWbfpLs9Ubtbi7y2t9aC8s3ABAI3mBQggv4nhC/Pk0rwSibESCutFEZCErF3R5maSPH6hfXKF33sWdZd91EZwyJtGIi2q0Iola1P3U01Y3+h9APSNXBaWWCFF1SsQd1rJ7kA1pOS2OzpH6WXoC7l4VcPJWzO+okm8YbyIeh hkzUrQap Fkj8qqgyVVluu3oqhOIEmbrxlVQU1wL1W53PA3GZ7kTIbgULCjmc51c9e2fgKAP+zTmLaK0LCru3m5z+CqIkrL7xms2V8jkxgxXBoDV2dpSRshvwmxtBiEby9j3UxLDTK+4zn482AfxXPl81VGwKnYeJtWRZ5RO28uDPRPeDeMIrFBbUp82SMXLE1IZHHKj03gvVwiRltwBPsiLjwBCTv1ptP0wZjhqaicorcoaS3Fyr3rDELinng2l4wrnhUehTNKwGngkFV0JU8GVZIFeBqYtFpOZDuNUl5vpdpu4Zwlbjnk8Qt8a8/VCmNCtKModfoTuT9Kpm/zCJROct1XDIIBOr3eJ/9PZCPJDKoC7EzYXoI3nw= 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: For private memslots, GHCB page state change requests will be forwarded to userspace for processing. Define a new KVM_EXIT_VMGEXIT for exits of this type, as well as other potential userspace handling for VMGEXITs in the future. Signed-off-by: Michael Roth --- Documentation/virt/kvm/api.rst | 34 ++++++++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 6 ++++++ 2 files changed, 40 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index df37aa11512d..028fd3fa50a7 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6780,6 +6780,40 @@ Please note that the kernel is allowed to use the kvm_run structure as the primary storage for certain register types. Therefore, the kernel may use the values in kvm_run even if the corresponding bit in kvm_dirty_regs is not set. +:: + + /* KVM_EXIT_VMGEXIT */ + struct { + __u64 ghcb_msr; /* GHCB MSR contents */ + __u64 ret; /* user -> kernel return value */ + } memory; + +If exit reason is KVM_EXIT_VMGEXIT then it indicates that an SEV-SNP guest has +issued a VMGEXIT instruction (as documented by the AMD Architecture +Programmer's Manual (APM)) to the hypervisor that needs to be serviced by +userspace. This is generally handled via the Guest-Hypervisor Communication +Block (GHCB) specification. The value of 'ghcb_msr' will be the contents of +the GHCB MSR register at the time of the VMGEXIT, which can either be the GPA +of the GHCB page for page-based GHCB requests, or an encoding of an MSR-based +GHCB request. The mechanism to distinguish between these two and determine the +type of request is the same as what is documented in the GHCB specification. + +Not all VMGEXITs or GHCB requests will be forwarded to userspace. Currently +this will only be the case for "SNP Page State Change" requests (PSCs), and +only for the subset of these which involve actual shared <-> private +transition. Userspace is expected to process these requests in accordance +with the GHCB specification and issue KVM_SET_MEMORY_ATTRIBUTE ioctls to +perform the shared/private transitions. + +GHCB page-based PSC requests require returning a 64-bit return value to the +guest via the SW_EXITINFO2 field of the vCPU's VMCB structure, as documented +in the GHCB. Userspace must set 'ret' to what the GHCB specification documents +the SW_EXITINFO2 VMCB field should be set to after processing a PSC request. + +For MSR-based PSC requests, userspace must set the value of 'ghcb_msr' to be +the same as what the GHCB specification documents the actual GHCB MSR register +should be set to after processing a PSC request. + 6. Capabilities that can be enabled on vCPUs ============================================ diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 1fb6a6615d09..175b958f103f 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -279,6 +279,7 @@ struct kvm_xen_exit { #define KVM_EXIT_RISCV_CSR 36 #define KVM_EXIT_NOTIFY 37 #define KVM_EXIT_MEMORY_FAULT 38 +#define KVM_EXIT_VMGEXIT 50 /* For KVM_EXIT_INTERNAL_ERROR */ /* Emulate instruction failed. */ @@ -527,6 +528,11 @@ struct kvm_run { __u64 gpa; __u64 size; } memory; + /* KVM_EXIT_VMGEXIT */ + struct { + __u64 ghcb_msr; /* GHCB MSR contents */ + __u64 ret; /* user -> kernel */ + } vmgexit; /* Fix the size of the union. */ char padding[256]; }; From patchwork Mon Jun 12 04:25:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275526 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 8EDF4C7EE37 for ; Mon, 12 Jun 2023 04:44:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2F6E16B0074; Mon, 12 Jun 2023 00:44:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 27F008E0005; Mon, 12 Jun 2023 00:44:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0F88A8E0002; Mon, 12 Jun 2023 00:44:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id F34F66B0074 for ; Mon, 12 Jun 2023 00:44:26 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id A6830A0261 for ; Mon, 12 Jun 2023 04:44:26 +0000 (UTC) X-FDA: 80892854532.22.A3FE862 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2074.outbound.protection.outlook.com [40.107.237.74]) by imf12.hostedemail.com (Postfix) with ESMTP id 9FA8940007 for ; Mon, 12 Jun 2023 04:44:23 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=ZFg1dBhI; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf12.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.74 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686545063; 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=3sDrVPW9K6hr5iqjy87+FTvyv+9ltC0aCsqlHuApz6k=; b=X4NaxDQnQhPrsemfPFo6kacjmT1bBmHIksysBMFq8XvyQ2qEZ1hlJH7eYQQy8IZeP1vNLp +dDr03wAKHOM4A9VAwoxJE9mcEG4cQZfZASisVsQM6sU7ch0BW13gZut2CtesUG3fHIZTJ Jg/89NfY7eLYlnyBWFIN83/ZmKp8p5s= ARC-Authentication-Results: i=2; imf12.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=ZFg1dBhI; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf12.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.74 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686545063; a=rsa-sha256; cv=pass; b=aovI+s6Lb6kFFcZ7mu6ZrQ2wIzroMONnNN/nXbcqipQvr6OtWbEjNoUwdQ01tt58hZz3ms ZF1A/t7V/GG5hYdNBcI6eAO0FG9pAOi/m2Y34KOG9K7er25IghDYCxdQC5u+BAGd93/rxw cfPORyk4LlDUirn3trsj+8WKBKpuEAM= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VOmWeOCa6NkYH7ePV3RsOvkDgBdH4Chk5VCtWI8IYBX/NzqQ+uUg4n747GRM0oCbxzs5+m/xLHZhfv5Atb14ieTbqW2iuBbbPOJr1otAAmScD1Jaz9leomaopWIXZ0NuYVnOMnYUNDGO3g8bzodyh+gS0rw5HuvG8dlP8lkLVT/VKzli5MTnlckbJ/TmqpHKnKGNJ8rvkNx3GcnnEk2LGUgZA9JSsVAYbLf0k0PgEFHnC5gzMDUNAnXIlrzy49YS1Vs+ToXlgcUKHDHSOi3CyjdW53bXx/sEUzDbkK26KTn4q/JdTE57t1O2xzVcHsqEnML6Jj2qVqp5qriTamwwvw== 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=3sDrVPW9K6hr5iqjy87+FTvyv+9ltC0aCsqlHuApz6k=; b=eBbZMCxgr7rn9nKM7H+ydg7gfayNpM8tczu6Ao6pJ9J7wYBK3WB6ngIhpHZrULiOGIpFJDwCwAx/p3H7/5J25H91aQd4+MbB3DQsgVlBJZafwTQDcSnBGPWxBCyxnZ0fD1dS8OT2yvBHKnt4IHDOoh/uNl7FfnZA+zCXPmpHTfv3Ve4JZchQftPun3GpIgn3EaBAVmHgpamnGCBBM76DmAcoqo47AUTrzaHiTIseRqkVXGiAwOP59XnH7Fnr8RhmggAnx2VgGChpas5s4Xbqdse+zaAJ4eHgGaf3EPQ3TnivdRjTkdXVKaeJQZKp0+7TgvI8QI1L/PtVNCTvNfO1NQ== 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 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=3sDrVPW9K6hr5iqjy87+FTvyv+9ltC0aCsqlHuApz6k=; b=ZFg1dBhIO/GPB96ZYa0CbeIMDY4CVnzwabNCjvbpGdwRsIe5XkfcW4UCW2bzHvNKCam70NLibNEODKA87b1Bl1eHlt9fknedxu5ttOgVgHc8rA4zk3fX67zLSMmBJ6XmGXWEJ6ZRK2Mcn0r7pRnxElMvJLFs5blIVRJrjLih05Q= Received: from DM6PR02CA0113.namprd02.prod.outlook.com (2603:10b6:5:1b4::15) by DM6PR12MB4500.namprd12.prod.outlook.com (2603:10b6:5:28f::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.46; Mon, 12 Jun 2023 04:44:19 +0000 Received: from DM6NAM11FT046.eop-nam11.prod.protection.outlook.com (2603:10b6:5:1b4:cafe::f4) by DM6PR02CA0113.outlook.office365.com (2603:10b6:5:1b4::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:44:19 +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 DM6NAM11FT046.mail.protection.outlook.com (10.13.172.121) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:44:19 +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.23; Sun, 11 Jun 2023 23:44:18 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 36/51] KVM: SVM: Add support to handle MSR based Page State Change VMGEXIT Date: Sun, 11 Jun 2023 23:25:44 -0500 Message-ID: <20230612042559.375660-37-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: DM6NAM11FT046:EE_|DM6PR12MB4500:EE_ X-MS-Office365-Filtering-Correlation-Id: 7d9c5aea-57bc-4b7d-8f60-08db6affaf32 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: YnoRJBIiWIS8VmyL7Q71Yz9l76DEfTekyzl/7U8sJW4ZomDagrcBw4q+7e0B4+ge21/zXE7IgkBmUOpPuIS3Vyq0SBsOTiBWoGiygSDwp8DwZsATzmnJvL93/5eqGfnboNR2AjvgBLErx3m1aNe17uZWOkqL/HJgNUv2clhZIXxj8328s10Mh7hAOvVHBrinjeV3Z1IgMOEJtu80oKpYPnOtRC/DubE1B5cjAzmUe9RYIQTMo0Jdz6Oj3hMxW7/qx+1xMOm9GxK1c6Eh9c1VstOB5jNghJ2e4Ib3Dve4gvIY2q0+Z1Z6IoIdHAzZp7h7CPk3xzArVfoFVe9SGIy6wesCLwVlbSxPHZhguzgHR0bukTnTr7nIzBvZDj5J2JnJ+12KoPdNRfh5tArl5uFXF+DV0TczLiVahUrrwq+eL2uCzwB63f5TenNUnKjE9duvgH6tGRsSSIc+BvVK6aWoBSWJXIMQEnRAqe1x0tcYHyJv8PX0zVl2Gkv8ccm1++a9pyyaYFXWJCV57em8ZrgcVFOlde2O0tM3tfaaQ09/C0B6V0slzxhjL6rMI3oW1eHpUp30MpxSehxglBFhqpBwdUk8APBHKPnvuzZa1w732I4FjgVOawmPzAuiJCULHNGobzFbmoQmE3lOa6EC/b+TciBikkpjjU0zevWu5r2IKmJCplqky14Bd4ppQ3ZvZF/3ivmwHNxsJd01RSoZOIWEp92Oo4CLHDJiFgSFiWAZBEZ9TZyjKyoqkMQRCz5q1e3alXMV0HG5GddoUXS0pGCUBw== 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:(13230028)(4636009)(396003)(376002)(136003)(346002)(39860400002)(451199021)(36840700001)(40470700004)(46966006)(7416002)(7406005)(5660300002)(70586007)(70206006)(4326008)(6916009)(44832011)(2906002)(316002)(41300700001)(6666004)(54906003)(8676002)(8936002)(426003)(16526019)(336012)(2616005)(186003)(26005)(1076003)(83380400001)(47076005)(36860700001)(86362001)(82740400003)(356005)(81166007)(478600001)(40460700003)(36756003)(40480700001)(82310400005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:44:19.0807 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7d9c5aea-57bc-4b7d-8f60-08db6affaf32 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: DM6NAM11FT046.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4500 X-Rspam-User: X-Stat-Signature: fa9jgfs1zzfjq9n81y4oh85jfxh9kddi X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 9FA8940007 X-HE-Tag: 1686545063-35802 X-HE-Meta: U2FsdGVkX185SUthYK2DSPhRebA4JZO1xxBvKm0zBXmAS6KFbi7ZJg8mNR7unk3Kx8X+eZdl7AAze1lH8gatU2o/vF8qy8qvL1AvJJIWF0wCLnlmNg776yw/jeS2UFEAdZwPsBf8M0OMvkRrIC8AQTj2q5HF9ufuNBCpWSimNhl2oQ2PJ6oPsLvZflfkKc9ei1F86qRZiHfOP+i2X2XZAx+aQy+qYWmkU4LGrqi0BCa3p2EL/TwBWnHI0qFRI5pLi02yKbMJpqg6PvuwJGB53WKOoGLNGu66FQa0F6DkW6S4KxrwkfXrfWix/jfqnrxB7cD+0GkDW0VmdYfwbBAhG0X0gLVY9rTxayqmIfVzRHBLgNfpkSHHs0gNLy3PbdRKDwH/eo51hDm0aXDdZohtPgoK+yp65HiFoS33xaVWj3fdUu05THcXvtGe2kNfKDLrl6hNbDEUoAFIs5AEFUwqxfFs3DvwJ6kB+RmeVEIWyCRzGKN0THFp6qRmkiDg3uWJPYiahJ+glRmihlgqjvCscQEOpetfCwOT0Q5uxv1oQiH0I87YYX7/kiVEpnlBtTowGg9PDZbqcIkUvNZ16NA8fpuqC4c165oo9qN/BMAYxwJgK+BRHTSbwGOTt6HVSUwrL/+OhF4ktNsBAgdi7QMuAncf0s28kFQHTm45lDb/GSRJcSO3tCu6PnPvNWPMHhAxaYKU6/zaATRr2WxmXZmO24MKB20azlCQAnG2Oya0ZTFMIoChWZ2H2tHHwGH1ayFty4SpUs+0FBRvIGEvHVJ9HCaKCtOPzqo6UDuAGQI6rFiviqJqcqk2iPQKDu4v94+Qv8qBHV5xz9nT0+kG322J+sJbt3+UvZHPszSouLBdgMZRA3PCQWMRdYVmw1uaWlMupDcRj6RfN2eVrEPFeM79n6fh2T1i3W7FVI2xzIQbc5FiRU7xNiVQiPzc9iqPgIEtukyEPO/A7s2SO+C9NfS f4Ykdb5K wn8AHoMJk6llA6PhN0IMXM9wMea0k7yJ0iCeVMXtJ1PlVnazknsarMM/pP++Dz8oY2o7cw+U64JlTwV6VvyDd0mizP9oscIMwdh+OuyiNmuYF88KjHSgevdMLFBVrIOTqIGxaGl78DGS373NCxPzUvAJ3CxxI7Q2fUbbI0/4RTHDbj++aPvjag5WQP4TiC0CAoKu3qRrWKKCXcVDwcs88og//h52q5UWbXTkf/+cIRK0EXld2vefI01QMGxmMZnIzpVVDRUBoBqrXKY7aWznwfU/Lo7XJ9KSfr55Flr652U4k9ZeweNW2V0dHRK/25EmcWVL+5dUHH5KrYTAMk/g97jUJoA== 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: From: Brijesh Singh SEV-SNP VMs can ask the hypervisor to change the page state in the RMP table to be private or shared using the Page State Change MSR protocol as defined in the GHCB specification. When using gmem, private/shared memory is allocated through separate pools, and KVM relies on userspace issuing a KVM_SET_MEMORY_ATTRIBUTES KVM ioctl to tell KVM MMU whether or not a particular GFN should be backed by private memory or not. Forward these page state change requests to userspace so that it can issue the expected KVM ioctls. The KVM MMU will handle updating the RMP entries when it is ready to map a private page into a guest. Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- arch/x86/kvm/svm/sev.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 44fdcf407759..2afc59b86b91 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3233,6 +3233,15 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) svm->vmcb->control.ghcb_gpa = value; } +static int snp_complete_psc_msr_protocol(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + + set_ghcb_msr(svm, vcpu->run->vmgexit.ghcb_msr); + + return 1; /* resume */ +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3333,6 +3342,13 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) GHCB_MSR_INFO_POS); break; } + case GHCB_MSR_PSC_REQ: + vcpu->run->exit_reason = KVM_EXIT_VMGEXIT; + vcpu->run->vmgexit.ghcb_msr = control->ghcb_gpa; + vcpu->arch.complete_userspace_io = snp_complete_psc_msr_protocol; + + ret = -1; + break; case GHCB_MSR_TERM_REQ: { u64 reason_set, reason_code; From patchwork Mon Jun 12 04:25:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275527 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 359E2C7EE25 for ; Mon, 12 Jun 2023 04:44:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CE0566B0075; Mon, 12 Jun 2023 00:44:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C90918E0003; Mon, 12 Jun 2023 00:44:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B58928E0002; Mon, 12 Jun 2023 00:44:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id A66D56B0075 for ; Mon, 12 Jun 2023 00:44:50 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 682A5C0267 for ; Mon, 12 Jun 2023 04:44:50 +0000 (UTC) X-FDA: 80892855540.01.0DB4033 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2065.outbound.protection.outlook.com [40.107.92.65]) by imf26.hostedemail.com (Postfix) with ESMTP id 6BB2B140002 for ; Mon, 12 Jun 2023 04:44:47 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=jTlHICZQ; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.65 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686545087; a=rsa-sha256; cv=pass; b=NKTlIL8ta1Smi4P1HH8ixXP5AwW3WLnlNRwB847tn+y6BSyCWtwUMo6juQI7ylEVKIR9N5 d/BuUYOBclJ+Sc7zLQr6Ncai8JUPUJ9QyoHZvaI0lwEA8+Qtdb8L4Ewnv94Tasw7qhNZhw IXkm23S5kqEFUTNDCFM9jBlM4BFB2Og= ARC-Authentication-Results: i=2; imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=jTlHICZQ; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.65 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686545087; 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=dGa3k8AqLLW1tjtn3WwuRM7764enwizu98BPdPH3+7E=; b=GIW8NexizrPkXR/yNCj22PaY5FpcSaZBGX32AZ7mQR0CHsmx9zdKerm4UxCWr+1GqvLjBV 2Sx1MZsntA27RhJxwKkWUXRlv/mXMYB1EBupGV6xqTojhCMWkxs+srqijokHwzeDcdFyw4 w2D+zgBqaqck0UfmkAS7YpsSChNoUEY= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nKaPZ7aEC2jAlLLPmgR+fbjpfjmlpCX/egPf7OZMIX4kw31xOJgtkGlAwddWSbvxNKbcVHahsdP43jI188X/Q9fTckIvO0VspYqm1UoiJUFfOwuZhciHwV/QVic8pqvLkx1nPpp1pgtpd+iK9mfDpJcMVu/pTI3w1eycFe2MRY7KezNWVjxtyK7hnu5bzxJeimllUiy3SB25C3UIRh+8aPYy+prQMei9CcSN8vqyWyuOgBdvgcU9w6OP7bqO/Ov1p01iSif8GFWPRTy6oI068clpeXm0od6ERbosrjmqauqqc2sxBP51iChPekCmnHvlvBLyXaaPaUySPs29e6yuUg== 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=dGa3k8AqLLW1tjtn3WwuRM7764enwizu98BPdPH3+7E=; b=Rrv1aR+LqM+8MfM67HNHDMUDtYSUeT3doTEpcAfQ4DtBb1s7sRWvZxC72nBMC8fIFU+0ccTCKfxwwRqzgRQg9nF2dPsOHKasu0aZeuWjXkWMmeAVcxXEdJFvNviToOMAHVlDrtumbwHUgpqx919/9TVBXkk7IRoI5Pd0BaOpd0ZiPml0fhLfcxqzlH4ytsrFzWjxL2e8mfsLt5HeCy8fps5xMCa4CDZRB2rvhlA0AuWlM+6Q8rikMG2w7PI9BPq1e7TFSxLPXPWSBEvxgTWa2akp/xfAyZi9H+zCPugtVxQD+rtqXNLneERf64t9Cc8dmAmndUGCLHS47I7y9kYIgA== 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 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=dGa3k8AqLLW1tjtn3WwuRM7764enwizu98BPdPH3+7E=; b=jTlHICZQsVSzwXB4VGld5ziBwiQpm+spu7W8hU9v21MqeFPT+hOhWl3azHL68az39CIPXPPwVqvyez4jl5L0DFSPUCuYW4fGuHA1svMWlkgiU21rULNIXQXAp3vsvBj6k1VAR/bzT/gwsZx73jLMZKFfoGKC0FyQigg88l3Zdhg= Received: from DM6PR13CA0068.namprd13.prod.outlook.com (2603:10b6:5:134::45) by SA1PR12MB8120.namprd12.prod.outlook.com (2603:10b6:806:331::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.33; Mon, 12 Jun 2023 04:44:43 +0000 Received: from DM6NAM11FT012.eop-nam11.prod.protection.outlook.com (2603:10b6:5:134:cafe::dd) by DM6PR13CA0068.outlook.office365.com (2603:10b6:5:134::45) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:44:43 +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 DM6NAM11FT012.mail.protection.outlook.com (10.13.173.109) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.29 via Frontend Transport; Mon, 12 Jun 2023 04:44:43 +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.23; Sun, 11 Jun 2023 23:44:42 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 37/51] KVM: SVM: Add support to handle Page State Change VMGEXIT Date: Sun, 11 Jun 2023 23:25:45 -0500 Message-ID: <20230612042559.375660-38-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: DM6NAM11FT012:EE_|SA1PR12MB8120:EE_ X-MS-Office365-Filtering-Correlation-Id: 7a03cd3d-401c-4a77-a599-08db6affbda7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xcB6CwXrHsHJ1KLRi1+GIz3PgRuuOCmHjWLwGIQfSvedys5FVRUjiXor8ghIJR2dmyc58lHkNg5jTMdEBwMebyAiOlkvI0N2HSUd9xMfVu8W8XnUtNhkNgiqvhCjDMm5lGNeoMRh0CXIabBybwvm016jTrtkSD67EnMSlnp/Pm22nGCowaFts0t+ytVdRi1zamnD6pLs51aWiYWSGC7ZupGxMBU+QcUuSAuUhYPcAdHs8K2O3AOvoWT72PsSMw51XegVsxdijOJMZutGgFYZcNvs79teq0o6CoTAaH/du6xomzNQ+IJLrGDBG6o2jCUbV20tSgL8md3VTWybsRbiyNS/1FiZTBEEawGGyS/SVnUczOV8B047BSw/d+nmCJuIA90qK2K/mMTMPYgOtuXn/tQTJr1SmSl9EQHv3dMHjTEDqlTRa9HC96yT16s6zAhCi4GCBAD6txPistJOZvhq0GJDFS+dylcXISAseEluWn277Cha1O42g5T8kO+Qz+9sr8LgjFL7jUxL7fp6QCytPFEu3+RtbHFewl5ydrr4hVa5qnW4equTmCJSuSRZgCSPho36oGjn6paw2fPuCvE8+8YlNq45EEfQWUyBHdNr1W6/1Sb19Nu9HkR+8axZgJeT7VYh4DWzp2c6ezU6h5K+paoo9WxW7ikpl4pS2t2y+ujejTNsx2zX66IVxIRWQGosY6UGC0FYEEzrHZOJAtMDsSJutCBCP3ALSC1lemSmOTCmBcTQoEs0rAXH+LRrOYvc/yNvzLa9iLVylSIQIp0byw== 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:(13230028)(4636009)(346002)(39860400002)(376002)(136003)(396003)(451199021)(40470700004)(36840700001)(46966006)(40460700003)(66899021)(1076003)(26005)(16526019)(40480700001)(186003)(5660300002)(41300700001)(426003)(82310400005)(36756003)(47076005)(36860700001)(7406005)(7416002)(44832011)(70586007)(4326008)(70206006)(8936002)(83380400001)(336012)(478600001)(8676002)(2616005)(54906003)(2906002)(86362001)(6916009)(356005)(316002)(81166007)(82740400003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:44:43.3361 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7a03cd3d-401c-4a77-a599-08db6affbda7 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: DM6NAM11FT012.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB8120 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 6BB2B140002 X-Stat-Signature: 8ygqswkybw5fbgrf4j8umgn6odyaze5i X-HE-Tag: 1686545087-622206 X-HE-Meta: U2FsdGVkX18Z3/mURl8tia+HnZFm0+cIxk5bHwcPhlUpV3VdViXs2FN1/e/9vK539p3r4TH17UHQEFED5q4rODLSHutrgDB7uhWfyZ1izTzW6KTEZHyvI43aOHsWaZ3bIGipOQtsZCy/93bx2ywJ5KgmLBL4Qn7cvgRmqbtzf2ck0datoroiPSUJrnqHu0QfPlh+sHMgOFG615jxFJXC6Flkzd67zDiEosjOqxjT2aoORGy3U5T4ursBmA3zN+a4DCbO0jJ1KCd9srtmK6IbaMaYPjeKRk5EBNcrNdXLEPGHvugaXzrNB6rBt+9xzTrpHw98BGujwER6YdxbSjAhQpZHsimGNbIpHBSZlMlc9Qv7paF/BevLCGt2RvPVeQmxlVToLZ33X1QykYftWATECZiIsvDskAzQz9ffYKLrJAjFocGnzpjZRszyQUexaQNWc7QbP9+t1wv99Ws/obOQ70RSslXWOe//UlzQxXVlPvQr2Yu/PVmMCSU3EBxPIgdEpK5qhCIkpx7oXFz8qQ7vrTUGNYhk2DXS7MUVnKwtQJJmP5LNpSE+Z6a/Xxhz+BqS+PHDLhL2/xnt8bjWuvLzsRP7WDenZ3xHarAymisiHjfLKDxYfFkGFK7nkeBCw6Z/kzZf88yNk/rnvWl8bbeFqUbofA2Izx0XCPDzUW/N34SiHZOL6HHjZWN0YB1PR0o5zmocimXaDlCWTOk2RpigXyo0OS59vqpGRTE469c588u8j9S7jY4IrtbBrTQlhq1ENLm0BG+wSGc/H5XmHp/cPQQYc0wCYu03QdOExhfIfjQuxQWE3tPaHH/BPKAQfn+GYzGG4ngSjLMTqX252RGwJfH+3XI2vncqGMA3ypJLpUEQ5hjAwqWTp7IUYKe+Zrg67MKACVqK9pPTzFZYdfu8BkwO1JrvppL3fhsbCTysb9UwT/uvkvEdX4NSYlVPOqPvjxvz7BRkE2PBqd36kPW nvQ5E+gx yAb57wmKUg7hm+vTalbonYun65BKFv5Ui7uNkp3MMMt90egYdxQOApSSnUaLjGIHVLpdH+OT3aRL/TlLAR1gBr4/TJZed6cscStxsPZ3cAp4IgNzhNE+uACOU9jDCXUJ862bZgRaq+NYOp+ttNajsk0hgtGbXmukKfx4Xxe4WZ006Ny8w27aET95AWVwcZS3djWpaAn4hmu+YSOBdTeG9yDeTMXM+H6M8hiSsr3QoXRqFWEIOtwFoLO/1XF5k55cwjaCDLsK62CP3lXa19+5d/oWq7juu+diha6q+lEbNGgVU2wVSGlnuZ+Qik7LIGTVvG/TSL5mmZ0FDjpDclI6OA7JscA== 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: From: Brijesh Singh SEV-SNP VMs can ask the hypervisor to change the page state in the RMP table to be private or shared using the Page State Change NAE event as defined in the GHCB specification version 2. Forward these requests to userspace as KVM_EXIT_VMGEXITs, similar to how it is done for requests that don't use a GHCB page. Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- arch/x86/kvm/svm/sev.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 2afc59b86b91..9b9dff7728c8 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3039,6 +3039,7 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) case SVM_VMGEXIT_AP_JUMP_TABLE: case SVM_VMGEXIT_UNSUPPORTED_EVENT: case SVM_VMGEXIT_HV_FEATURES: + case SVM_VMGEXIT_PSC: break; default: reason = GHCB_ERR_INVALID_EVENT; @@ -3242,6 +3243,15 @@ static int snp_complete_psc_msr_protocol(struct kvm_vcpu *vcpu) return 1; /* resume */ } +static int snp_complete_psc(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, vcpu->run->vmgexit.ret); + + return 1; /* resume */ +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3485,6 +3495,12 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) ret = 1; break; } + case SVM_VMGEXIT_PSC: + /* Let userspace handling allocating/deallocating backing pages. */ + vcpu->run->exit_reason = KVM_EXIT_VMGEXIT; + vcpu->run->vmgexit.ghcb_msr = ghcb_gpa; + vcpu->arch.complete_userspace_io = snp_complete_psc; + break; case SVM_VMGEXIT_UNSUPPORTED_EVENT: vcpu_unimpl(vcpu, "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n", From patchwork Mon Jun 12 04:25:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275540 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 0F0FEC7EE2E for ; Mon, 12 Jun 2023 04:45:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A47FA8E0002; Mon, 12 Jun 2023 00:45:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9FB596B0074; Mon, 12 Jun 2023 00:45:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 898DD8E0002; Mon, 12 Jun 2023 00:45:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 7CEC66B0072 for ; Mon, 12 Jun 2023 00:45:52 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 4825812027E for ; Mon, 12 Jun 2023 04:45:52 +0000 (UTC) X-FDA: 80892858144.14.AE1658B Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2053.outbound.protection.outlook.com [40.107.92.53]) by imf30.hostedemail.com (Postfix) with ESMTP id 5019980002 for ; Mon, 12 Jun 2023 04:45:48 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=3MbAzWy+; spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.53 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686545148; 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=iPne3TgHOrPCdZDAp0qle3bNYwKkBJf0eNd3G8dRABg=; b=hfqkl7JDeN+QKWHD3zfwRjNZelZkrpVKgsGtHwz7/cfCtNtElNeGtTZSFSakmzfB5KfWcD wYDTgcSupZiBjbkcGhZ92P4r4kHpvtL+Ve+hImX/WzMLX1A8dsK93hBFcikdJow4BQ2cLg jEeXZO6RdgvkXcpzXSZC9/Zlf6SdbeU= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686545148; a=rsa-sha256; cv=pass; b=srHPDEnEcVHnxfxnDxqfSxaAwhh6f6+Dhxy/x54XPSYi1W+lf0uRwGF1aiy+pWvjBtPcgc 85xYqwJ2c21+oH8BxbWaKA9cts5Vap7SemE6Q3Cx6OfsNs3wZ+vOygQbI1O2s4oHnmUvpi I+cmiF+91OvRHOMp/mU9kQjn7PY+Xhw= ARC-Authentication-Results: i=2; imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=3MbAzWy+; spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.53 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=niBCB9g78SHtte+VkwNKWSL37ouWuBmblcvgIbkfv9/G3trub3uqMGsp3vsClds4puZocPUxnT2Vkr04kcvnL+qVsTIT4sKk4JO0V/LymMnajmbvGNdAQwsGPktSnCQebKRaxRy0vmojaQc/dP5xVQPUHt56ZB1Wf08zfvpb+cjPuKuy6BdgZWtUcfUxtUrifhoBBVeEfkLwIkZTAQF5vY1nxTdpLxI1AkGgLq+Ss1qR1JMOn8kjZ5zre+63tGW7dYorXXHupk44CcUVVa6SpiMOWGVrrg9eSvHrmkxSPrLwzGkb1Pr5z6WPES9vpcfRjXmmpsS3osB7yzXtmXWOxQ== 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=iPne3TgHOrPCdZDAp0qle3bNYwKkBJf0eNd3G8dRABg=; b=UmodQYZB5mERboIo6UtKCjIV0ECrNI2JLFH6HL3NcdvjxOywBBv53wLk7xpdEE9q35AlFJlpnHWLcOTIJqUWpzp8ExnTjXCVoyiS+VG5t63nMLs2su90KbLB/8anIWoOsipFVn5lcetulPR9HHi0Jz4O4E+hQ0n1GrBfFw34hoIaXdJ3BqfMn7wFeO3oNs+KCVzAelZr/PVWk9GC1dLTMebjRhrCQwcP4cGmKI0dI+X3vtYPx/BLHM/zQleXgtHFTENjw2aDkWAdbBVvA3nG0kZhWpYOWh6fNCiJ1lUfw7wg0SnJBCX+QxzE2wIfrA+PDiQLBxA3VO/V68cWK5NLMg== 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 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=iPne3TgHOrPCdZDAp0qle3bNYwKkBJf0eNd3G8dRABg=; b=3MbAzWy++c82qom2Jh3QOB3Re0H8ipMe2eDzugUQST5pamcC85Vc14iiD9XL1MwQSJVdTKV2cNK/bLWpDvwUbGztsYPMRQlLwki4+iR6D8IgXyxTJV9lbsGlNcCKQ9spXch3uhJtjOwohumUe70PiS3EO7TioNS+7Daf+ls/RAQ= Received: from BYAPR05CA0068.namprd05.prod.outlook.com (2603:10b6:a03:74::45) by LV2PR12MB5725.namprd12.prod.outlook.com (2603:10b6:408:14c::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.29; Mon, 12 Jun 2023 04:45:46 +0000 Received: from DM6NAM11FT040.eop-nam11.prod.protection.outlook.com (2603:10b6:a03:74:cafe::d7) by BYAPR05CA0068.outlook.office365.com (2603:10b6:a03:74::45) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:45:45 +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 DM6NAM11FT040.mail.protection.outlook.com (10.13.173.133) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.29 via Frontend Transport; Mon, 12 Jun 2023 04:45:44 +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.23; Sun, 11 Jun 2023 23:45:16 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 38/51] KVM: x86: Export the kvm_zap_gfn_range() for the SNP use Date: Sun, 11 Jun 2023 23:25:46 -0500 Message-ID: <20230612042559.375660-39-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: DM6NAM11FT040:EE_|LV2PR12MB5725:EE_ X-MS-Office365-Filtering-Correlation-Id: f1efeb91-f5a8-485b-f3df-08db6affe24f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: wpywAS+W9P7Vgt28bgPCLwxkXiYKi9rqssu9d6pI/XwesOEAayHV+SNu6XdSKYI8m6p6Qexq44Jqydiw+xwNpKPaZz4duLBp5RllvCIXIj5fVcvx/8/vKcywST3xUF6IiREux46MIQP1nA+PfWJ7ALLiVnXHZ8XruN5/4VKvTt29R+Eva23aaJLUctt5Ck0zGmiErguiE90unoWG8fT36rRewDMSOHQ4SUNe1sUpQyFMSVo+mhyOkmWnd7vNi6x3EhkwxabFL1g9vzqwSDD8Omovm1vo3glcrPn1/zklG7lAPLK3FY0Ommrcf7z1tfcIkSYyK+x/F/l1Wcxo6j0HRyhtHxgNpmmRC1x/pCfiqnXwEjZvOg5QeHcAb8lFn6Mu5XE7vH0JmrpxG39EWO24QNGFPHwDgnoqEwaIYJx2HqGjxls5UP6Y3/ugSA7zrJhkpSQd6KYHkViRpbqPplR53tb1favlBJlyMFQK3mCi9eD8ZmAW+nFSbJc+QmipsOVqSdCaUbVDkDptpiWAfpanlImPPj1hP/0FIOAnKw0p++xuRVBx9Lu0p3Vzqd1AAs7Jwi3almnA+37YorZXDdiTyAZjdX15QBk+VfHm5RTzaPvyHf3nGdEIUN281JuwmiOKE13Dde+zlTbuF4zsNrfutGf7KFMYH7w5rdMYLNmLc3T4smQxarExc4oFeLRH3+cDfHxMb8TQitaxPvY2TqbZgiPr2mPQ0VPJNGhMmxm9NxKq0GiqoExFH+W+FI9opvLXEKA91Sh7dC54WcuX+DkrzA== 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:(13230028)(4636009)(396003)(346002)(136003)(376002)(39860400002)(451199021)(46966006)(40470700004)(36840700001)(40460700003)(6666004)(36860700001)(36756003)(47076005)(426003)(336012)(83380400001)(2616005)(82310400005)(86362001)(82740400003)(81166007)(1076003)(26005)(356005)(40480700001)(186003)(16526019)(2906002)(5660300002)(54906003)(44832011)(8676002)(7416002)(8936002)(316002)(7406005)(41300700001)(6916009)(4326008)(70206006)(70586007)(478600001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:45:44.8371 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f1efeb91-f5a8-485b-f3df-08db6affe24f 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: DM6NAM11FT040.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV2PR12MB5725 X-Rspamd-Queue-Id: 5019980002 X-Rspam-User: X-Stat-Signature: b6qitqhoeuooi1i1r68qnnd6uq9yeh41 X-Rspamd-Server: rspam03 X-HE-Tag: 1686545148-989837 X-HE-Meta: U2FsdGVkX1+lxw2RzkOIgxjCf7Y+OTXvOjixSER6bEhfNq37cgFDqONrR0REIKM54OzeklcL26/udV5VKDnnK6NwdEBTRtgiBQ8G2TbKpIzVpIgyr73DkdKI0i7zyclgOdCPfBlRbr2Hbv/Zuy/IF57RDDVY5MRqtR5HaCYjUzn3WcoNGbA+ujRWCVkTUj1ee2Cj8EUwzZFNOgEtE2utBJRRp6baRGoAlEKCAXKOn+pQeeqMJ1BYl9eJB9OgUtxBI/tyJ0sGleXGo70+PiQsEtWnnc6jmAtBjnbb37QtemS9Yva5VxL1QI/KXd1tsVpWtm4+X234B4rMYRxNFjUM8PJdSXBOZoFoLqeKCZpOoHC7kGk1y7jSIzbCisM/TEfBP1wD6G2SR9SkxfwxJqZyqeAbiYw2xLfGdyk41OaX3xb8CkC+WhB6QBrV0LgHY1qTOJZ3FyTrFxtFydpLSMCgZl8Tp1c+2etM4oaeduaWwpj3POMq2Kkp6mc3dTCGrfEizbaQdzTFEmGWjA+6D5xSSv8n57jwybedhZMR9v9u4nBjxEo4IpV16Stt/tiexT0x6qoEOOIxqys5UpaXPWrKYXJZYTHORXs8ViJGcw26PMKeqKRZKcXmfUz5jovR2vXEFnOaDdaw+Cpimde619KmU5ZitwqmAZ9xTFUnIwQXB0zAZXbtKo462pWjxM9K2wwAxMvADGZl0fY8Dr1G0JnXv65gv2CV/up3o2HQnQqzlXhkGUmLEQYRwYIgFulTMJvjoWKs8f0/SeJggUNBBdQ+BnBXGokA4dh/KMcRkrrJIJGYQE5qIKOh1rpOM7z1iFbekp4b7SBeo26dVZv4AW8nyDDd6RjkB0WU+1ZMfo9nnkL3AVsE4HTDT/4ZXZv0q8L9S46GuE5bM2rMrOvI/sSPO5S9+MK656j0pdsEHj8LHL3B0r55co16GDNddeWS91YTrqGuguckNY5Lrgax0JV BqOpLnOm g4NgDqVrosY/04jLXYMJh66qnXM7ZagDVImAGnW5YbndLWUgBvMlj0X0ARnT2IVZeMEItAZs8ln/NOBh2p0tmfus3kkoyY+OaE+2vQrzQsMPoOPRLVrOmrLMrWYU8ReayyBDJM9HomHKeIaQi0ZlkQGbZfNy2GEtB/ygtEeupoG0O+Kdg/VZWoKd2DVggWzwrzFfFoNmcDwvvDX5FTlIYbnGsOc4E2cO3+WEn2qgcF24qgl3FLO7HLlY5XGdqoMpw9qSCTfPsIooJQyO5ZG8+Fa0IK403Bcw+puVbepHJ3wJ+NdMP88g+gDvZYqbuzkJi+S7AwH3eJwKK10Tj/TXExRGWBCLhW7NM1TAognwv55cfJBBCDABNOVM8/7GnB6V5wrl5lE/8oSoAzvs= 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: From: Brijesh Singh While resolving the RMP page fault, there may be cases where the page level between the RMP entry and TDP does not match and the 2M RMP entry must be split into 4K RMP entries. Or a 2M TDP page need to be broken into multiple of 4K pages. To keep the RMP and TDP page level in sync, zap the gfn range after splitting the pages in the RMP entry. The zap should force the TDP to gets rebuilt with the new page level. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu.h | 2 -- arch/x86/kvm/mmu/mmu.c | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 8d2bb3ff66a2..026bfc4446ee 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1851,6 +1851,8 @@ void kvm_mmu_slot_leaf_clear_dirty(struct kvm *kvm, void kvm_mmu_zap_all(struct kvm *kvm); void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, u64 gen); void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned long kvm_nr_mmu_pages); +void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end); + int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3); diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index 92d5a1924fc1..963c734642f6 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -235,8 +235,6 @@ static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, return -(u32)fault & errcode; } -void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end); - int kvm_arch_write_log_dirty(struct kvm_vcpu *vcpu); int kvm_mmu_post_init_vm(struct kvm *kvm); diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 0d3983b9aa7e..dff0eb018b27 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -6732,6 +6732,7 @@ static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm, return need_tlb_flush; } +EXPORT_SYMBOL_GPL(kvm_zap_gfn_range); static void kvm_rmap_zap_collapsible_sptes(struct kvm *kvm, const struct kvm_memory_slot *slot) From patchwork Mon Jun 12 04:25:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275541 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 C792AC7EE25 for ; Mon, 12 Jun 2023 04:46:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6C69F8E0002; Mon, 12 Jun 2023 00:46:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 676306B0074; Mon, 12 Jun 2023 00:46:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 516638E0002; Mon, 12 Jun 2023 00:46:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 445B46B0072 for ; Mon, 12 Jun 2023 00:46:59 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 204691A0280 for ; Mon, 12 Jun 2023 04:46:59 +0000 (UTC) X-FDA: 80892860958.10.FD55753 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2066.outbound.protection.outlook.com [40.107.93.66]) by imf05.hostedemail.com (Postfix) with ESMTP id 37EB710000D for ; Mon, 12 Jun 2023 04:46:56 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=eU2LCqFb; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf05.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.66 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686545216; 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=Rx8POHMeEO6GjBi6mYzRZT5bILI2FMQUM3oFJFtiCLY=; b=L0FnNTDA1TeaT0DrkUZuY0NNmR2V4zvRsyx0e8b4YnfqnOwFkT0B2hhaThU2f4h9uKcPn1 PIITcwmG81yn60uTQwiWWvuqq5WRAKIZgOOXFyNe1PQHAse9D9rz9OXaMKHVANosfeYiyL J2UftvGNHCozTKb/H0aK629RD4eH4Dc= ARC-Authentication-Results: i=2; imf05.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=eU2LCqFb; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf05.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.66 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686545216; a=rsa-sha256; cv=pass; b=dkSlWki9Yv1s6FKx/SVWSmmI8gMxfhJ/7lWS5bLVgPyf927ksgU3XBfhHT5zFiI/yjhkA9 +NfLLgToXmtpyvyKTmHdRNMk+V/RAJJIVGaoKqECs5o7YD+ptjr1G/61oBkST8aMw9DsO6 KOKuSKQO22s1fLB9vkBTptgPOHJt3Ew= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CkmBUcULQoz6g5oqyaqFulU/sfr/HbKIBqsafxdviY0z8H7gFDcDzyeXv1gYPKnaxAjPx+BrzHBUc4TVQhSUrK8G78YgaVhDvkKOAjW/ZUQrEK5jdrmUwibxnrzMfbFWwzeURRDMr+hm3AmXu5g+5DY/EJ7Iz8IDv0tp3ZVG3WC/S13VlXi2/7zUpr+0uUHEpFmgfbY1qAviACWAq4yxMBmVvd3O+aPOD5/bfGE0t1/AtKGCQ+SnLzBzYiczWxQnq8KmXVDrwF0/vYIVnA1JUgcxw0ymHmi0DMEK/I1fgZUfYdjV7iNPIxm5EFnuTgRJWJSJWH/X4VFhj26tBrGhag== 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=Rx8POHMeEO6GjBi6mYzRZT5bILI2FMQUM3oFJFtiCLY=; b=lnl5Momd+9jObD0Md5oeQVp7JZWml0r1TO5mm71ArD9MF0rwyW1c/GQ0b4AueL7KJyd9YGaP6eM4rWTqw4XmE0gdIDWyFWpEGu0fhToSltViNTK3DpU6aMabimmoEu3RMP3KURsnxK0z0ZtzcDu5pNdRO+UfSHxQw+Zip3ZWMFIQ5wk1ABjDO4lSfnriU/cqbdNQKycIUv+/2GlM49m7fEtLMme2honU3wJULg/kyATXVzYYMlNzGH7H/Zo0/oPzs1R2EbAZAKrOH0EKvIZguaCxXEBPLtL7DYWu284YQA5cPBvqbniSpChU9l8R9lrqKlrKH3+fGgFAPvCy/KvNmg== 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 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=Rx8POHMeEO6GjBi6mYzRZT5bILI2FMQUM3oFJFtiCLY=; b=eU2LCqFbd/NtkXBNhn4oNsZ9LCJbN6tASAK603dcXmOhiS0sSGHGumB+5hFj3vhXkPFLpIM7zE52J/7xXV15h/mn2j0XaNFSGf0vtRkID4AvRhYBZ1rbSyQzGFmNIwoBKiWmqd+U237q/XlHEHhP5Kxha6rAekgCx6qMyYbGSrE= Received: from DM6PR02CA0058.namprd02.prod.outlook.com (2603:10b6:5:177::35) by CH3PR12MB8259.namprd12.prod.outlook.com (2603:10b6:610:124::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.44; Mon, 12 Jun 2023 04:46:53 +0000 Received: from DM6NAM11FT097.eop-nam11.prod.protection.outlook.com (2603:10b6:5:177:cafe::34) by DM6PR02CA0058.outlook.office365.com (2603:10b6:5:177::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.34 via Frontend Transport; Mon, 12 Jun 2023 04:46:53 +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 DM6NAM11FT097.mail.protection.outlook.com (10.13.172.72) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.29 via Frontend Transport; Mon, 12 Jun 2023 04:46:53 +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.23; Sun, 11 Jun 2023 23:45:51 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 39/51] KVM: x86: Define RMP page fault error bits for #NPF Date: Sun, 11 Jun 2023 23:25:47 -0500 Message-ID: <20230612042559.375660-40-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: DM6NAM11FT097:EE_|CH3PR12MB8259:EE_ X-MS-Office365-Filtering-Correlation-Id: e177e363-8050-4100-9f42-08db6b000b01 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: SWhc41FjYkcwFVb61dw840GlOpjTFgiF60jbUN7hLBmhNskATgqiFMFDybAuLAARZpdBy8OlBTlI6K1RetT+k1+g0Sb7ROIQrDPeVMMnPPaiAFCdndLBGZl8dfH0uAKV35PB7wSBVylNVYoJRgeB4+v6hKmk6q5KlOY7u9GS39cCX1cKgo6690vOgYrkOr95wuZHQw/sIKiCFfIgMZCLyhCYoyc26rqHIrmSLpTy9LvhH7J/vplItX5KSF1ECvQD6pnWB2pHfLPxa/Q9XXZIH0Xz8MBOOL1QI4YAExO0w48QziWZ68xARaNrkeVNLTSFCkME5CrRWQQ1ix3tnaQB5+UzdlhRocKSLHvoWBkqg3nP1ik7LVczWlJHcFmsCai2abVb/4myEHroPTfNW2rVM7hDw30wTn1vY459chdgP6LZ/xNbFeoPYUciEBtkk/XBkLGqjL843atIoJBZcoJA4HAiGi/DH3oE18/lhGG11NdOAmVm9lY/BgGSrLeJljNDjOGJ5/0uYnJHe9u1BhxPo+gB7MriGNUgoPZfo5LCRaY56wbUEeHZ8fYfF9M6sG9jFTte1LeULIcN6t5cYoO2a2LBdGnHDPtT9kskVlgDlKlOTbF+ne2dprVw0EywgPmO/jeZJJlJELauI+J4//4EX5bXXHSJppt05n2IEYTK/I2R8kS/nqjMf0teG8kTtkRsemwMeKPBjdwFCUS3tDB2OHvAb+7Vi6REnyzoScLxg7zdF5pYkYjAk52rpExrhciGIn9uTGvGpUOd2CGeVJx45A== 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:(13230028)(4636009)(396003)(376002)(136003)(346002)(39860400002)(451199021)(46966006)(40470700004)(36840700001)(44832011)(8936002)(8676002)(70586007)(70206006)(2906002)(4326008)(6916009)(7406005)(7416002)(5660300002)(316002)(54906003)(478600001)(6666004)(1076003)(26005)(16526019)(186003)(41300700001)(36860700001)(356005)(82740400003)(426003)(336012)(47076005)(83380400001)(2616005)(40460700003)(40480700001)(36756003)(82310400005)(86362001)(81166007)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:46:53.1103 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e177e363-8050-4100-9f42-08db6b000b01 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: DM6NAM11FT097.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB8259 X-Rspam-User: X-Stat-Signature: ccqcgqxc6cuwyj1n6qajobbywpr9abyr X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 37EB710000D X-HE-Tag: 1686545216-609421 X-HE-Meta: U2FsdGVkX1+M+Dga5wKrSFTXgRLOXPTO90XTY7Gxzn8AKJuI5qHlx3inIy9v5T6XBPPipB1PtfNsGK7OAHkKhOimhxfMs39U1tzpsIRfp6eAcQffL1E8mf05XFDB1rZ8U0vCpwkyWUAYmaS/atDIBa5SM2ZRXMcGbd9OTGGTCr1Qkt48VXSPI1tE0/5oI+v83zNLEHhokBfxsTLTRo9Tp2jxAkGf9vn6xmCOgl8IQ5zxtYfVVY07bVGjYjxAZhY7tGkMmKxfbUP6yjP7ix0fn1FUE8OuKnjPw/GO9xtAkzB9ABp/p2J4X2VYY9Jmj9dL2D41JvAKojPo3MzqXHpds5U0XT/Yok2oe8RL2gCT6B10RG4Zjn+albzo+Z6n2dS1G8MckoUc2csiLkvV34qWK8ejrP4WcjUIV+1C/oJ+9XSf1CSnHtd0kor3flwMnjNW8NiX6sft7uZuSIQWE3gFQD3gCr9axDAvE8giiGPEQszXY/172tYJPcQlhKwd+3miJxufZpkRgd6nwLGQYOSwZArD3diaby5L6G9B1CpEuS/vLC/FoKbSvC6WpMGgnknjqzmm25ANinvTTX2kZSr4hB64bE39OL4Mhm69U7K9Jdbfchzv+jpCfsbv7eaDCZQCFF6A0ZWCUvZ7KkkkW7SMR1zpFta1pRw2/EUEGEEDGEjEZLzrhhGMwgESxaKHDlFIV/4p8FNKv+Tdgx5s9CdM0MREjNfv+mTQ1bUDECOMwdXgKWjC8mRHCzfWlb01rVIMNsPRsqFqyno1cLJ/qtitz91y7UMVWjm0NWN3RiHCyQtj7OdKCIIZ02qnqixjmZyz945QSONjSHdB9qTia9mI87zkdDfrVFmVXyt6vlhH/5PawkMF+Gmi2IQS+AWKI1B/f0O4+UTrh29eD7pOquywddXSg5qRjZzpQTS4jAtTPYj88xgkkA1jfb5CW4+FJLq8TmKlzRMzjrGLpfEqeXP LnIKKP9v EgSvKIEqUhE6QERzffNGoiWiMCFfZjbTwyg7fSF6co5jbTdBEAi76H08/fKGLm29YGej73AkSmP9l4znGT9Xtq+jRla7T8VjEHP4waQmhzl3e6u0kQ1fKYuT+basYOPq16D7AQ985w6+dAWRTArB8WXoXFRDZYb5Prpzr+QRwqE/DTLxFi7ij4BMesdzTxzws40ihfoQmhwNDFl2kIhn5TDVKWUVIAQegL8hZphqBOWdBlSBccIkxRwWlxs8Un3U0qwxOthOZUOqGiJDVIAgS7igvb8tPsH3EEYcOYpRBoH36CHMvR12FF/+C3k9WGckK2e09DLvWi6ZRKnzPUKjRiweg3Q== 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: From: Brijesh Singh When SEV-SNP is enabled globally, the hardware places restrictions on all memory accesses based on the RMP entry, whether the hypervisor or a VM, performs the accesses. When hardware encounters an RMP access violation during a guest access, it will cause a #VMEXIT(NPF). See APM2 section 16.36.10 for more details. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/include/asm/kvm_host.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 026bfc4446ee..2fcd309fd9fb 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -253,9 +253,13 @@ enum x86_intercept_stage; #define PFERR_FETCH_BIT 4 #define PFERR_PK_BIT 5 #define PFERR_SGX_BIT 15 +#define PFERR_GUEST_RMP_BIT 31 #define PFERR_GUEST_FINAL_BIT 32 #define PFERR_GUEST_PAGE_BIT 33 #define PFERR_IMPLICIT_ACCESS_BIT 48 +#define PFERR_GUEST_ENC_BIT 34 +#define PFERR_GUEST_SIZEM_BIT 35 +#define PFERR_GUEST_VMPL_BIT 36 #define PFERR_PRESENT_MASK BIT(PFERR_PRESENT_BIT) #define PFERR_WRITE_MASK BIT(PFERR_WRITE_BIT) @@ -267,6 +271,10 @@ enum x86_intercept_stage; #define PFERR_GUEST_FINAL_MASK BIT_ULL(PFERR_GUEST_FINAL_BIT) #define PFERR_GUEST_PAGE_MASK BIT_ULL(PFERR_GUEST_PAGE_BIT) #define PFERR_IMPLICIT_ACCESS BIT_ULL(PFERR_IMPLICIT_ACCESS_BIT) +#define PFERR_GUEST_RMP_MASK BIT_ULL(PFERR_GUEST_RMP_BIT) +#define PFERR_GUEST_ENC_MASK BIT_ULL(PFERR_GUEST_ENC_BIT) +#define PFERR_GUEST_SIZEM_MASK BIT_ULL(PFERR_GUEST_SIZEM_BIT) +#define PFERR_GUEST_VMPL_MASK BIT_ULL(PFERR_GUEST_VMPL_BIT) #define PFERR_NESTED_GUEST_PAGE (PFERR_GUEST_PAGE_MASK | \ PFERR_WRITE_MASK | \ From patchwork Mon Jun 12 04:25:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275542 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 C899FC7EE25 for ; Mon, 12 Jun 2023 04:47:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6764A8E0002; Mon, 12 Jun 2023 00:47:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 626F46B0074; Mon, 12 Jun 2023 00:47:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4C83D8E0002; Mon, 12 Jun 2023 00:47:24 -0400 (EDT) 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 3E42A6B0072 for ; Mon, 12 Jun 2023 00:47:24 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 0F97080265 for ; Mon, 12 Jun 2023 04:47:24 +0000 (UTC) X-FDA: 80892862008.17.094527E Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by imf26.hostedemail.com (Postfix) with ESMTP id 0F386140002 for ; Mon, 12 Jun 2023 04:47:20 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=0D7OkJEx; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.40 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686545241; a=rsa-sha256; cv=pass; b=AvCCdgCWdqIESuhHuRVQ6jbe/tP9w89CFHdCRGU/5mulFvjXf7RBtLmfV9Bu1gGZrKS0rq /2mA11WdA4yRw7wm5nco1zSDevmCkLCsdM48k9FqKkfq5EVmw6RjL4RTUySb4055jw7luj PK+bAAmhXTqAXQ5l2IpKR6mFQbtziBg= ARC-Authentication-Results: i=2; imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=0D7OkJEx; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.40 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686545241; 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=E3y+s7+t3OqbSQoznIHk/l3yDEommf/ftwO+lV/tIro=; b=Pn8jJZQeZz8MpC7UOgmPycn+XkzYfDb80v4CHlhxTuxQgOamFJtxvP8j/kLpABJVtaelP+ KTSWQEMWeHYkY19eRKrP7zGLblcQ9417rl7GRTmqlfH7EhWoiNAFTfoNN+stBThqJKAKKe AVYXtboBIaMNbMGaw8uVsu8ZSJmcN8E= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ibpd94S9wJ3R8CscOFRtFYoJjT7wdfbFkoi0cpevstqNCy450RJrBDGAfHhF/cbUlVDDCu5GErJNw6Uu2UkK2mShVCJnAmiNxXIwTZ9FiPnRZsxwM5pmoRfkqECtMmRAEv54kDovelxsXNr1qBaZn9hU6fc2nhjufYeq4k8Z/W/ajMvqUKvmD00uUEswBq0rNI2ZjoR3Vz5LeXdf2srrfhd1qkKOUzspKU9ngJAv52TdqOROTovn6IDS9bFu68JUVx/H56DFDLMo6zimZTWlCg8+U/OAh+tDofozEovLLBQ1WKDitzXTylOxzOxdSRX90dJyXIeFhFz7b8RckJV+Ow== 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=E3y+s7+t3OqbSQoznIHk/l3yDEommf/ftwO+lV/tIro=; b=PhCOCDg9L6nejZyiXnmARwOdXw/k9l9cluYFIP+pgi/AheMatX/WdLzGBcQeBWFUIl3UEdDc4GqaaBXXG20OPa2ANtnUmHVAYxQfUrgT0GQQkJDUU0C5ZJMviLtCzoLCal6O/VIx3riRXyVFWHdH3SEGFzeBBN4wDhQuaZGpPUxq9oNgu9BwEO3yYM+Dgga+1r11O08Lej7xL5RpVV1XMesnm4iL0qy3MG8dT22hDJf92/ugSLLIhn8cRQM+/XB9nQANpb581YmuqBbbWLnMh+COkzIX2fg79bukspyUhFWFpfQsOCCsJb/IDAWrQTTaHGbZSr5uI9W76YKPoutkIQ== 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 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=E3y+s7+t3OqbSQoznIHk/l3yDEommf/ftwO+lV/tIro=; b=0D7OkJExtOdzx2+c0N0ecrasyFJZsd0eSynonvg2FZCuzI8tuQscvi6cD4mEnZEu3jRO/Ckx2pCJ87I1XEIXv7DIA0VDayflS2zdDB3pl2OHVIW/zsbrPVY4CEsKxauj7ETNNxU/RDEOPHjduyt5Z6gKu48YTuL77C3CDOTskbo= Received: from DM6PR02CA0071.namprd02.prod.outlook.com (2603:10b6:5:177::48) by CY5PR12MB6177.namprd12.prod.outlook.com (2603:10b6:930:26::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.44; Mon, 12 Jun 2023 04:47:17 +0000 Received: from DM6NAM11FT097.eop-nam11.prod.protection.outlook.com (2603:10b6:5:177:cafe::e6) by DM6PR02CA0071.outlook.office365.com (2603:10b6:5:177::48) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:47:17 +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 DM6NAM11FT097.mail.protection.outlook.com (10.13.172.72) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.29 via Frontend Transport; Mon, 12 Jun 2023 04:47:16 +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.23; Sun, 11 Jun 2023 23:47:11 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 40/51] KVM: SVM: Add support to handle RMP nested page faults Date: Sun, 11 Jun 2023 23:25:48 -0500 Message-ID: <20230612042559.375660-41-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: DM6NAM11FT097:EE_|CY5PR12MB6177:EE_ X-MS-Office365-Filtering-Correlation-Id: 5ccfaa0e-7e76-4413-6a9b-08db6b001935 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: V2Zyvwb47JdoxYXjhACqkbRhZOMMHH4VCfyv07v4gBce+KYaEYYz7PFqP+Kf8m96s9RcCd+Zp/U+4FxCEW54SFw/5u9Rt2mx/dRcRT69PZHpEZK/wCI+r7W8xUTA5QunwYXAE50/p9SA2/j3RZkeObXmkErzudq5v/iWcefZWoZNzY60qageCLyDRukcsB8LwcIwP4L8xAoq0WfxJFyMcaiTeHVFeNnBPXJxoQNVSjWDT3yIXost9ZNV4R2CFrZ4XsqxlaHa/URGmXb9qGBuIprHuEMpsxk77a2jCHfYtO+7JrcbDy+JG6CzWzi54hhEYnRqdNcqE2Cb2EtgQZQ7JFG2JGsADvlHFAYN07F1M+Lu5KB86ivc1+wMpNd5druakJ4vHW0ccjoBnfYr6UTnItPy5KLmcp4kXL9UIYvFnLUe81ZNj6yrJ2VsE+lSZY4trJ3Lk0x/LPzIogpuRSQVf2ueRjcpKagRwOWpjMbePHmqo9VDAtDg5vLQpDE7K52J2Mzmx9V9YWS/qrK7tCrHWTNOizTqxj+9Yf6l3XAtP6bDjnw8+d4V5F+VHb6dkNSlypY8cU6U++CZuKZBKonSMAAbU/Q6uyvHu0sS44dVXfYiyMQ2O3/NhIb/9wKBWK+iIuvPMfRIQvx++7U4dGD/RBVYU9ke5l6dpFwHaI8bqtW98i8Fo8lOxi/ZAYLy8cQwZB/QouwfxvHlq2C0VzFyW+Lu2mmsHCRP+jSjyi7ywRad3JP4d6ZG/cQ8T7kvdn4K6VZnv8hhEOhT/RguuIsw1A== 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:(13230028)(4636009)(346002)(376002)(136003)(39860400002)(396003)(451199021)(40470700004)(46966006)(36840700001)(86362001)(82310400005)(7406005)(7416002)(40460700003)(316002)(8676002)(41300700001)(82740400003)(83380400001)(5660300002)(26005)(40480700001)(81166007)(1076003)(356005)(6666004)(36860700001)(8936002)(44832011)(36756003)(336012)(4326008)(426003)(6916009)(70586007)(47076005)(70206006)(478600001)(186003)(16526019)(2906002)(54906003)(2616005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:47:16.9379 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 5ccfaa0e-7e76-4413-6a9b-08db6b001935 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: DM6NAM11FT097.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY5PR12MB6177 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 0F386140002 X-Stat-Signature: zqo6my6c11cjihxusedfq5kxo5nywrxd X-HE-Tag: 1686545240-879243 X-HE-Meta: U2FsdGVkX184zNN8LoWv7WDbRdl7sZYKjABhXkQAawTtI/4eGRLEDOIMjDrmm88NHY0/HfmbhVb/PTi+x7q6cmPUuZRdYUQoERUSMGXrielY2qypkcRQ2aroDz/fBlhjll5gXvbB+fTmCXJSaVOlj3JzaO2Iq9UP2wms8vTAOZyyPb0eMvodLiFu+4rH/0K3GOZtkG2typXXVKHvHs2k38BkM7Spm2JMi/uv07YgEQLFJXhNP90CLKhKWtFuRCO7IYVU+Oez314rtfoO83Y8upsp++BjcRjdfmGcnqZGldMcTqEo0z08vj3Qek/aJ9sqTz68nFCud3sFwHoor8o8kzXyX8D9SGpjCkppW9Clw9jEigayJCd7WoklzEMzTLtYXBL7dpQxMq/vbZj4GU50x3TAObCS/mZhZzBM8knJQdM4es37B3FEW5NDTwpFQPtM1eLcFmJajp85Rcfbf3jJ3e4D7hWcW7x0tmIiF4jNowwO07kjxIX4MIJXvdFux/PvOrc2fSy+VXBV7/olUUE7hvLJYQnNfx4z+O4tc7ZRP7iIn9Xu7FpkteIj+HQwFO1XA2pZZq4O1w/fjfaFGnYz4GeMQmDzaf4tYudlCKjy8fVK7IUowLn4+vrvC4tYWG8HWwkyBr5CcO3IsRL/eU3S/hjb5uIs7NIUxaF2tjTG0FIo1fiMv+5rd1cl84qZad2Eybzr7ocONwV1tjDxeVkmaQ7/yO0VxYpG9oA/2yiGAuFXVqJyePtOiSCyRL5ka2plWT+8arPwas8DPMU1nkV0FEruPg6hm2kYafXPxhrQd/vv8GC6CBu5jT2d6ESvnZx1Wsz+qOmO5ms7SONKTKqq4tdw2i8wPBi17URDaP63Fjd+t61iIw81T5zXbPjG8jd9ghl0s+LCfg08cz8VLxHAAwA4BX4mVLcwoQw3ukNALdIijHJOSY9TQjcnbzrQQMR6PQ+vFJrPyCtTkMpSUEw vck5eQ3H BWh96/iRLWWmqgXXHDuvgKOMmCo9bsosvUJEinEn7hsRL5TTHTGjm9Jxghf0PI4wHkvm60R5wVA1jkPCdCJ3i89gJabaj6eyKZWwzdbY0EmNjF6V6aWU7YvZxyYxyoQ5+jJ80mTqbvAZgCSYC7Tack2J1igdZfLICHA5uahMZxY2p/79VL7vxoaUugElaiq8HhWIZuJOKrxZTpCtTwMsqvG7+aUVqvJgdYzTDwJK9lrYx4I9mnq3HEYVm5i02nN5oPpA+CUA+tD7iurAsuHLCKzwvhdT9CZN7I5im9dd8qLc2FrPFWOytlxo7FJLiSlvYCg/qkDU8FPfmM5WeU/TWwzsZJw0g4uGXJoY1RJkVzWb2bJsq7Uujx9VrCDi8tfphOxWfy3/LJa9faWo= 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: From: Brijesh Singh When SEV-SNP is enabled in the guest, the hardware places restrictions on all memory accesses based on the contents of the RMP table. When hardware encounters RMP check failure caused by the guest memory access it raises the #NPF. The error code contains additional information on the access type. See the APM volume 2 for additional information. When using gmem, RMP faults resulting from mismatches between the state in the RMP table vs. what the guest expects via its page table result in KVM_EXIT_MEMORY_FAULTs being forwarded to userspace to handle. This means the only expected case that needs to be handled in the kernel is when the page size of the entry in the RMP table is larger than the mapping in the nested page table, in which case a PSMASH instruction needs to be issued to split the large RMP entry into individual 4K entries so that subsequent accesses can succeed. Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- arch/x86/kvm/svm/sev.c | 85 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 21 +++++++++-- arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 103 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 9b9dff7728c8..1ba49c5ebaed 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3234,6 +3234,13 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) svm->vmcb->control.ghcb_gpa = value; } +static int snp_rmptable_psmash(struct kvm *kvm, kvm_pfn_t pfn) +{ + pfn = pfn & ~(KVM_PAGES_PER_HPAGE(PG_LEVEL_2M) - 1); + + return psmash(pfn); +} + static int snp_complete_psc_msr_protocol(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm = to_svm(vcpu); @@ -3696,3 +3703,81 @@ struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) return p; } + +void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) +{ + struct kvm_memory_slot *slot; + struct kvm *kvm = vcpu->kvm; + int order, rmp_level, ret; + bool assigned; + kvm_pfn_t pfn; + gfn_t gfn; + + /* + * Private memslots forward handling of implicit page state changes + * to userspace, so the only RMP faults expected here are for + * PFERR_GUEST_SIZEM_MASK. Anything else suggests that the RMP table + * has gotten out of sync with the private memslot. Generally... + * + * However, there is a transient case where access to an NPT mapping + * that has just been split/PSMASH'd can generate an RMP fault. In this + * case the PFERR_GUEST_SIZEM bit might not be set. In these cases it + * should be safe to ignore and let the guest retry, but allow for + * these to be optionally logged to diagnose exceptional cases. + */ + if (!(error_code & PFERR_GUEST_SIZEM_MASK)) { + pr_debug_ratelimited("Unexpected RMP fault for GPA 0x%llx, error_code 0x%llx", + gpa, error_code); + return; + } + + gfn = gpa >> PAGE_SHIFT; + + /* + * Only RMPADJUST/PVALIDATE should cause PFERR_GUEST_SIZEM. + * + * For PVALIDATE, this should only happen if a guest PVALIDATEs a 4K GFN + * that is backed by a huge page in the host whose RMP entry has the + * hugepage/assigned bits set. With UPM, that should only ever happen + * for private pages. + * + * For RMPADJUST, this assumption might not hold, in which case handling + * for obtaining the PFN from HVA-backed memory may be needed. For now, + * just print warnings. + */ + if (!kvm_mem_is_private(kvm, gfn)) { + pr_warn_ratelimited("Unexpected RMP fault, size-mismatch for non-private GPA 0x%llx\n", + gpa); + return; + } + + slot = gfn_to_memslot(kvm, gfn); + if (!kvm_slot_can_be_private(slot)) { + pr_warn_ratelimited("Unexpected RMP fault, non-private slot for GPA 0x%llx\n", + gpa); + return; + } + + ret = kvm_gmem_get_pfn(kvm, slot, gfn, &pfn, &order); + if (ret) { + pr_warn_ratelimited("Unexpected RMP fault, no private backing page for GPA 0x%llx\n", + gpa); + return; + } + + ret = snp_lookup_rmpentry(pfn, &assigned, &rmp_level); + if (ret || !assigned) { + pr_warn_ratelimited("Unexpected RMP fault, no assigned RMP entry found for GPA 0x%llx PFN 0x%llx error %d\n", + gpa, pfn, ret); + goto out; + } + + ret = snp_rmptable_psmash(kvm, pfn); + if (ret) + pr_err_ratelimited("Unable to split RMP entries for GPA 0x%llx PFN 0x%llx ret %d\n", + gpa, pfn, ret); + +out: + kvm_zap_gfn_range(kvm, gfn, gfn + PTRS_PER_PMD); + put_page(pfn_to_page(pfn)); +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 065167b42f90..0cff050bf5bb 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1995,15 +1995,28 @@ static int pf_interception(struct kvm_vcpu *vcpu) static int npf_interception(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm = to_svm(vcpu); + int rc; u64 fault_address = svm->vmcb->control.exit_info_2; u64 error_code = svm->vmcb->control.exit_info_1; trace_kvm_page_fault(vcpu, fault_address, error_code); - return kvm_mmu_page_fault(vcpu, fault_address, error_code, - static_cpu_has(X86_FEATURE_DECODEASSISTS) ? - svm->vmcb->control.insn_bytes : NULL, - svm->vmcb->control.insn_len); + rc = kvm_mmu_page_fault(vcpu, fault_address, error_code, + static_cpu_has(X86_FEATURE_DECODEASSISTS) ? + svm->vmcb->control.insn_bytes : NULL, + svm->vmcb->control.insn_len); + + /* + * rc == 0 indicates a userspace exit is needed to handle page + * transitions, so do that first before updating the RMP table. + */ + if (error_code & PFERR_GUEST_RMP_MASK) { + if (rc == 0) + return rc; + handle_rmp_page_fault(vcpu, fault_address, error_code); + } + + return rc; } static int db_interception(struct kvm_vcpu *vcpu) diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 02edbdd443e4..4cf9dbc442e9 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -762,6 +762,7 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector); void sev_es_prepare_switch_to_guest(struct sev_es_save_area *hostsa); void sev_es_unmap_ghcb(struct vcpu_svm *svm); struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu); +void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code); /* vmenter.S */ From patchwork Mon Jun 12 04:25:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275558 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 34CE9C7EE25 for ; Mon, 12 Jun 2023 04:51:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BCD5D6B0074; Mon, 12 Jun 2023 00:51:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B57066B0075; Mon, 12 Jun 2023 00:51:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9CF6C8E0002; Mon, 12 Jun 2023 00:51:17 -0400 (EDT) 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 8A9A26B0074 for ; Mon, 12 Jun 2023 00:51:17 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 52C13400FE for ; Mon, 12 Jun 2023 04:51:17 +0000 (UTC) X-FDA: 80892871794.06.9F2FA4F Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2073.outbound.protection.outlook.com [40.107.244.73]) by imf20.hostedemail.com (Postfix) with ESMTP id 3D37B1C0002 for ; Mon, 12 Jun 2023 04:51:13 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=3GEJ5Iex; spf=pass (imf20.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.73 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686545474; 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=zwFcoomX4hZVadAOtXXAh+HWWkMbGG/uK/b829NNo8E=; b=ytXaVhAMGkZ/Ij3U76K71Gx2QoCxolgeTMVStVmDAqeHVV1HWSS9KTsz9SC8rzws7ZQrAa Eo+hia107JEfY8SObG2/hnYCZDp/KhwTEe6XvQFTD5Bk8GiqtaZjWxlu8c4hHCMubRIczy riYF8iRDQ5KggJxujQU7GgqwPYDk908= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686545474; a=rsa-sha256; cv=pass; b=kc4+hjj/J8Uv9wXX6kRgZNT+00pgW7+8VmumjlVN2P+K/+F6Lwm7OONM4fL4h7KwYp6fe+ /2jsmx79XpTRXaIuXiJBtJPF2W5Zwi7paAHrrtq0Rgh2NNU0We9m5rvyubmuqpx/g2B5wR X+lAjpNhUtz5p3u4SG6xsdo/JBqsrIk= ARC-Authentication-Results: i=2; imf20.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=3GEJ5Iex; spf=pass (imf20.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.73 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gABl3st8+RPuXnOTJ5AIdQuX+inGNFxwqxAYTsWwSisK+Qpo2wMopsUbYyl1UvUUElKefA5rw1mTbLFWhprrfFuH/lFV4qkbG0d/0CsEpIPb6AviqCiZQqeTBwmsbcqOYw8VKwgUGZ6pxZAom3OS77+DA/nE4J31eg1rwhisJ6gFGYkt/111Cx9Q7knlCXzt4yY3LWJ6cSL1fZKgxBIhwLu+LoYtz9r/5AzsT7gDXrz6U+gNmc7fvxAa9mjtJkuUofqJHx2foA4mBnbeklI6zqLx6q4dAtTWZDQi5rHG2v2Z3hQWvG16QNBzOajAiAWt2juaBG2h/Mf3RyVc11Cdsg== 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=zwFcoomX4hZVadAOtXXAh+HWWkMbGG/uK/b829NNo8E=; b=g1mDykJccu9gR+8B7TqgE9ubiSFrNCPn2fBYOGaWZxRqHCRFWQSPsGZ5E/4dn7x/fPq1cVeqG+kNnaxmw3+DwHqNT20J9tJUV0BJgHpQV24klsDhNYowhXhBoNoU+hlpTZpsfAVjxjOqdwR6bsbetMB6fIwWhBBeS4ZsR9sb5wt+GTyyeSn5sYZrEPllVjmOZVqpxU8pi1BG0FXreq8GzUZDm8vNQtLdLlulBe6isiL3B2Y4e2ghGQTOqJ+6vz2Pv8SagytJJQaQIENd1masa8THDgCqvM+u4+Uwglr7g3ClOlYF7AIRmzRpY6B9kAwKVNz6poEzVJEVtafD43c6ow== 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 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=zwFcoomX4hZVadAOtXXAh+HWWkMbGG/uK/b829NNo8E=; b=3GEJ5IexLSvILwWbJ7mTwJpWT9Ef30Bum61UHRfIk7xkhMGR2sweQTWTtqSK5tGGDx4PdD0Oj//3z/gxHYJ50EpkshYiT215VFvDDpis1GhysvvcwqJQ+kWdxMKRi8TOyGrVPZLSkMD43iqS3ajQIwbTsIrM04icHstBY4nNJyg= Received: from SA1P222CA0078.NAMP222.PROD.OUTLOOK.COM (2603:10b6:806:2c1::25) by IA1PR12MB6604.namprd12.prod.outlook.com (2603:10b6:208:3a0::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.44; Mon, 12 Jun 2023 04:51:09 +0000 Received: from SA2PEPF00001507.namprd04.prod.outlook.com (2603:10b6:806:2c1:cafe::4f) by SA1P222CA0078.outlook.office365.com (2603:10b6:806:2c1::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.34 via Frontend Transport; Mon, 12 Jun 2023 04:51:09 +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 SA2PEPF00001507.mail.protection.outlook.com (10.167.242.39) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.24 via Frontend Transport; Mon, 12 Jun 2023 04:51:08 +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.23; Sun, 11 Jun 2023 23:51:04 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 41/51] KVM: SVM: Use a VMSA physical address variable for populating VMCB Date: Sun, 11 Jun 2023 23:25:49 -0500 Message-ID: <20230612042559.375660-42-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: SA2PEPF00001507:EE_|IA1PR12MB6604:EE_ X-MS-Office365-Filtering-Correlation-Id: 2e9a3f11-ad05-4d53-ba08-08db6b00a378 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2S3+ttjoDMZZfxFdU6K9nG5tsU3Pw56v06hAPztdBntfzCtMRLhG4bcnqUZv+hDcoU3FB2h8pclhW+3E2OaYTbBNeApqgeBgoyZ0AuFYzW5CyCINGE7Yf1uOuDaTJRAvJTvOK0ydIweVkOMMMO297gHBXbrk27sj/Fmw8oO27QIUuVj8baKVfMis9I4v5xcs5DzW4msIMK/m5U/9cXtbfCyQipj0FnsxmdFSzv1AzYm9JPGqxIwUk2/nrLHWLVRcAR2OMwjlvgp6juYvHfiLgRwkMuPLudbHpPIj24gOs134WJ7j/CZHszwFQMUQSA7IMwZRbtgDpGB8ty7AeXsR2xnY4c1+Skg4cwdHJHl6JXFJLDCnQYmaQzwcV4UnfGsN1VX+6PbZchd6a/5clj4sKrKzWr4Z5J34BGJ8Ouhsscn9SnVFpoWC8aIJO9Hbp/IzhTACFcY0hU4wTcT2MoUh5IOyG2+Q+voPrnZ94ihSeiz0TQfAfu9G7i8Onq3+qk/siHiWg/l6GZ4dmDwiOyJBeIytV7bR6n2zBPXA9M6LiqfDrj+0bZO+Bd7+WVuq3JvwYrXyRkS9aXBNjWXqDx+TnSrSFfO1xVC2OfEaxAzqm52Gger7FD3RMPpfV/qHFNKObp4+UwioyA3GjvXfgAOd3P/bfOx1BWafi5TF1XFkqfHKZ2hYl1f4JS4yCJxLoU/S0/BRWjnBp2o75w2vVYk7yRpTifQKstKqClrBP16HWPmgDSGKZn+Om7CceXf+0opNGl/Qw8C0YR5aHuRoPWPJ7Q== 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:(13230028)(4636009)(136003)(39860400002)(396003)(376002)(346002)(451199021)(36840700001)(40470700004)(46966006)(86362001)(40480700001)(36756003)(82310400005)(81166007)(356005)(82740400003)(40460700003)(478600001)(6666004)(54906003)(8676002)(8936002)(44832011)(5660300002)(7416002)(7406005)(6916009)(4326008)(70586007)(316002)(70206006)(2906002)(41300700001)(36860700001)(16526019)(426003)(2616005)(336012)(1076003)(186003)(83380400001)(26005)(47076005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:51:08.9202 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2e9a3f11-ad05-4d53-ba08-08db6b00a378 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: SA2PEPF00001507.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6604 X-Rspamd-Queue-Id: 3D37B1C0002 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 1oqxtw573cbzb9jb6mfyx9feu3uhi3e7 X-HE-Tag: 1686545473-592697 X-HE-Meta: U2FsdGVkX1/Xjy9/0a086GcJMutqPjxh/el66hJ0nmDi0EymhRGI7asGDYPY69ToAsdKK7XS1ilGSykRElSkZEC46byLpP3oHqKn6Xmr+sRJ9cclekpGVKDp/tZl1+LTHPG2TRXpB0RiJOfOeaw1PDQi0HKpC+z1KHX3CUoriZ8iD1pcxZt+hm5bXdMVxqttTlKxO8q3N0myYPW7iTIcU7VZhK5VgkzV0/ZuZUTqzvUdFc89OkoSknMosGvAbCwiARsUims+kn17TQrLCpjKdb7XvJZoRjRrtGH0bIcsNsMk9a18MOHt52gKmnxV6anas1LXad3sTVnHytBczfh9KFlSwUjmmcQkFWSGvUVCiFwU/FUOD/A2eAMXZ3H4i33KfpbYmI5Zi49S3meIw55GJbUejCgFRd/qkz6zPIOXw7Pm/3ohk1IFkCLKeXuvKoIKjNwFRDl8wkhfSA8Lc7j4TQ8AviH+y5AhSMFO5ueWajKnzFtByfgvUvwc9kjt9uuYaeJeXLeCED2wKuZD5szUkw3fGWGaS/+axBj6/2dTHgzJC6inLy7M3itv3q7yyTYpFcnwlAY+lZ04m68epu2WENmT2xU2bJjYjrAtAEkMT6LfiK+Ezxbbx+euAJ9fZxo+PHTV/pKzGd0UMu8b+DNU/huy5UQWF8Bj2kFfVyh2imwuehfXVqQvaW5MUwlAX7frjpchmBrerP7AtMczUEhJV+5Gaz02PofMGTfCYkxvNsJ02/lSHyObLSskip6ILEsptAA6SSwvl2GKI8G4t2urPfvS6i3v4C/boCVnET7Y7OPvoxxeMwF+0SZ383SUCYVIt2EQvxfnIudTlx7nYmIMxYUWyCOZL7XU7Dlma5E8IwW5wdB1mE9OCUIzWylfkLgrcMPHb/HUjjMpBPKaYhkuiwE2ziHHgJma0Nn7yMokyqjA3iA/kHMYvMlvx2j7RZWXCeJBJdiWlhcCqYQFHIJ z/mNXDCB GMBe4+/UV+z1WkwvBB4BrckhBau5eNauu0O04jmJqonKV3wv5yswMxerCGO+8NC36WbwrokGufFvp5Su2PYIV9fROUKUxO9Rb6akBm+Uee7bOwKznMRA+aiT/o4H4RkEvo/t7ekqdoVNYn3iIAyMj4Lx68CCs34BF7usQiDUOtDKiklAEYLDG6u0QKWcZ81md4CHtg547PdEGHWidMA11EKspKOhJGAlO0gVHLm/frbzbwfu6G8hNIqiY7is5gWg4ygrrfxO2SX4E3FuxSMd5X0V+nDva+hXg0xsKT97GLbOjYboqiivteGBjQg== 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: From: Tom Lendacky In preparation to support SEV-SNP AP Creation, use a variable that holds the VMSA physical address rather than converting the virtual address. This will allow SEV-SNP AP Creation to set the new physical address that will be used should the vCPU reset path be taken. Signed-off-by: Tom Lendacky Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 5 ++--- arch/x86/kvm/svm/svm.c | 9 ++++++++- arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 1ba49c5ebaed..111e43eede15 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3551,10 +3551,9 @@ static void sev_es_init_vmcb(struct vcpu_svm *svm) /* * An SEV-ES guest requires a VMSA area that is a separate from the - * VMCB page. Do not include the encryption mask on the VMSA physical - * address since hardware will access it using the guest key. + * VMCB page. */ - svm->vmcb->control.vmsa_pa = __pa(svm->sev_es.vmsa); + svm->vmcb->control.vmsa_pa = svm->sev_es.vmsa_pa; /* Can't intercept CR register access, HV can't modify CR registers */ svm_clr_intercept(svm, INTERCEPT_CR0_READ); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 0cff050bf5bb..77195d8c1aa3 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1419,9 +1419,16 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu) svm->vmcb01.pa = __sme_set(page_to_pfn(vmcb01_page) << PAGE_SHIFT); svm_switch_vmcb(svm, &svm->vmcb01); - if (vmsa_page) + if (vmsa_page) { svm->sev_es.vmsa = page_address(vmsa_page); + /* + * Do not include the encryption mask on the VMSA physical + * address since hardware will access it using the guest key. + */ + svm->sev_es.vmsa_pa = __pa(svm->sev_es.vmsa); + } + svm->guest_state_loaded = false; return 0; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 4cf9dbc442e9..8dc7946ab634 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -197,6 +197,7 @@ struct vcpu_sev_es_state { struct sev_es_save_area *vmsa; struct ghcb *ghcb; struct kvm_host_map ghcb_map; + hpa_t vmsa_pa; bool received_first_sipi; unsigned int ap_reset_hold_type; From patchwork Mon Jun 12 04:25:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275559 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 5302EC7EE25 for ; Mon, 12 Jun 2023 04:51:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E65E36B0072; Mon, 12 Jun 2023 00:51:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DED1B6B0075; Mon, 12 Jun 2023 00:51:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C3FD08E0002; Mon, 12 Jun 2023 00:51:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id B074B6B0072 for ; Mon, 12 Jun 2023 00:51:36 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 8694F400B0 for ; Mon, 12 Jun 2023 04:51:36 +0000 (UTC) X-FDA: 80892872592.23.381FFCA Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2085.outbound.protection.outlook.com [40.107.223.85]) by imf16.hostedemail.com (Postfix) with ESMTP id 6C5A3180008 for ; Mon, 12 Jun 2023 04:51:33 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=pLdYNgLY; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf16.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.85 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686545493; 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=Sr6+Hk4gRHkhZVI0+Aeq+AC5gkb36b1EgW2twd01CQE=; b=dqQ4qRWMaX2SOm+oXqyiuWvk6LlGaa6KPp/goCy0hiHIUlWfJ5XLVaFJo2C/gk0y8Nxrya /v+NPMsCgAfOS04A//Razk39wbJrsTLe8lE2ha+KfXKufR7Sb3gvbjHA8OPpAl+/FwbZba v1YHKxQFdgbq8ijpZaO5NcR75TL6Ntc= ARC-Authentication-Results: i=2; imf16.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=pLdYNgLY; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf16.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.85 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686545493; a=rsa-sha256; cv=pass; b=r7uFAEhImwVAOF52um5ybeTSkvjtXe2PXK5LITn3E5zacuBk9QGUsEkaUe+9q2szT0Mz70 DstIfiThuPyv5WaTc8ARrnjKAz3DAaQsWZ7b0sJuxPMQUZ5V1EPp6JLxAhCkiefyUMkmNP MTb8g0Xsf+DQLffQ4IcqhbnLk8aMTRU= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XOIEUdMsOj8hOgySfwQ2RdLKhup8IVFulmYG4yvtApT9ffmG/l6UajfGEb1RrxVl6zWTYOxAbALJcuGjgiCUq0Qtn7xrEtdjjyFJtyX3DBak1NpgVnxU+4/iGFN0/am+JrEI3BPpIT2qGkJC5X4roUO5wBaUr1OjvdHIl7JZfDMckW35DyVw2ETqyzrzgdJd9uh+yfZUE7HOC7xHYV940v/s3qobHEh/08OJ6L6ULDwMQx8dYaAeGq04HYu3H33yFwvMDPzq4xFPTEKcYpOf3l1YvZQ64flcDiMr9LSwFnfJGqgM5NtllGQpcOo4o4lUbNmRT+YOBY+YWEdpkHvznw== 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=Sr6+Hk4gRHkhZVI0+Aeq+AC5gkb36b1EgW2twd01CQE=; b=T/mV2JqVACQ6fXEJ5qT+X3J9J7iGpVcUG6s8lrVffGraGcz49uZJUJOuRwQvZkLxZfcwUWpnQ17Jio51DWRcezFpR9wX9qrxQcfEM2nvqjOZAE/ficnbGlsQTxmdDO7Zr6cTgnc09CKN1sqN6yPCDKNlM/hjGDgbY83E7cd5l0JYiFDnbj15T/Mc59GBJ/Dg6YTgXDv8otczgBNOcHainYkJV4BHzFJYW/Lm6P3jDuzsLuAhPv1MSo99ua/kNC9v3f3BZSLQrU74Jfru/TcbZOWZpZ4/BEGIbiZrDgrEXrfQEDejiazan0JA9M1KX2gV822HUqdd2BHrRxIRmVfZpg== 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 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=Sr6+Hk4gRHkhZVI0+Aeq+AC5gkb36b1EgW2twd01CQE=; b=pLdYNgLYg+/X2h1XNhNzHUPLzuOvpJSxrngrfhOmCIfZVfpIajYCtXY0NxR/EeFnSuo1PvSBqc6kigyVODrFQkfJQj5/z6bXWgFOkvA98UJAUgAvHzOdWMRHunIPvYvTrn+4cL5ZDuxAcub/My2C/fpkg78SJ9tE9ZrhJhi29Gw= Received: from SN6PR08CA0015.namprd08.prod.outlook.com (2603:10b6:805:66::28) by DS7PR12MB5933.namprd12.prod.outlook.com (2603:10b6:8:7c::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.29; Mon, 12 Jun 2023 04:51:28 +0000 Received: from SA2PEPF00001509.namprd04.prod.outlook.com (2603:10b6:805:66:cafe::b2) by SN6PR08CA0015.outlook.office365.com (2603:10b6:805:66::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:51:28 +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 SA2PEPF00001509.mail.protection.outlook.com (10.167.242.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:51:27 +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.23; Sun, 11 Jun 2023 23:51:27 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 42/51] KVM: SVM: Support SEV-SNP AP Creation NAE event Date: Sun, 11 Jun 2023 23:25:50 -0500 Message-ID: <20230612042559.375660-43-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: SA2PEPF00001509:EE_|DS7PR12MB5933:EE_ X-MS-Office365-Filtering-Correlation-Id: de353946-87cc-4705-43d2-08db6b00aed2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: A01qHqeaqr7WQHFaxyhfcQuUVQi4v+0mACbGK9FYIM7NgLO03D19Yp4b5+QbUWK+VxSRqmPvhrrHy2tp4FKcocl02gnKrFei+gJj4HJdje8ZpwmjfFeQZQ/f5dT2/22sZMhFUIFN5RH/m+r/vACzduamkSdKdNLYQwGZXzmfhBOHgV3zagLsBPwR1NiOnFNyymvSYAR1SxXyMmHZQZ/I+FtZisKLv72QmMGB5/XMuSuEvT3wy/s0yGTUTPZvxqr/LsI1IoM4IhCInysEgixDJqPtbInkn6by57a2sW5XfhTzsqvGd7FdfnYdvqdELdnHUoMGbMp+dhelbpY1OE6YV5WrANbC0ZVsdLmzFXlba5qo1peUiO86CiOZKFUb2tjwnw+eA4aQogxhftp97ea9q5/ZnhL0UVI9i7DqhGsQmjFaIfImSl8+YZvWjQ/VKp+eYbSia/OftU/tyy7BwU9zb5C6V8TqDpSaCOk3WCQOYgTvHaspY5205D18kAwCc3+7UUI1cuvTjP0VpO4NMsqdj8XmdiXD2sYA5ZTMEjYNbOvH/MzJ6aKegbW+mzqiY6z19o78xTGei4iJ5simbMw3gR7i0j5wVw6+OCS2qxztLNZ8dWuanIwIrGxgC4ii2jLoIchYGo4vqK4VzMVhmH3htXBpai/KqPdSnt4P0I/WX85iLlUsPtuxT+ATz1sGYYVHSRu7cgztMDv99EZ1v22NnrTac1StkrGE5NpurxDQPEohllADAtplOnyKXfkPMyVKI04nGzTvse/RC6m/eGo65A== 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:(13230028)(4636009)(346002)(376002)(39860400002)(396003)(136003)(451199021)(40470700004)(46966006)(36840700001)(86362001)(82310400005)(7406005)(7416002)(40460700003)(316002)(8676002)(41300700001)(82740400003)(83380400001)(5660300002)(26005)(40480700001)(81166007)(1076003)(356005)(6666004)(36860700001)(8936002)(44832011)(36756003)(336012)(4326008)(426003)(6916009)(70586007)(47076005)(70206006)(478600001)(186003)(16526019)(30864003)(2906002)(54906003)(2616005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:51:27.9463 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: de353946-87cc-4705-43d2-08db6b00aed2 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: SA2PEPF00001509.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB5933 X-Rspam-User: X-Stat-Signature: zd1zsxhcs4dfhaexcnfjdmt6tfki5x6r X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 6C5A3180008 X-HE-Tag: 1686545493-844619 X-HE-Meta: U2FsdGVkX18wGwNB4vPbrTwvdd88zidGBEQ7hYuG3+ybAEySEA8yQkwF6+I+LVwDdqU6RWqcQ79yqdC8GXF5YlkV4jGGe6zq2L0NtKfdejifa3L6mQzKKMgVLOv8ArUycqnZWtvjsG35wijkyQ8hN8GnlV5IY2Oy6fb4hDUw1DuevzAF/A0NG1oOgJk4Ahi8D+1H+hui5pBPSggANwzoJbeuQtbFjriuFYCHzSWxXHtucT0qMNMXx+paYqwxJCvmPSkb+qhbxbzUst41CWfVgmzINveV3I30/GvqGfKMWb108K8KM7XtXBSKLFo0nDRWafdvD70BivsTzv2nEdFgQU9RltUpHSSm+tl7XmlEUg/sYdLQTwVqm3elZZnC+uOvqPxLKKgsvG5K7pUmvMae6AWxyE3FrWFs97O6TwZa1B7XUjQbGTRGCMSvUeBhh7H//0SIcJ+fFEnS9BkAqtdke5cd4RMYeDXz2ZdHSQBZoNYFcrdi8zzFH5Ofh/L9c8Ajc/6ArC8oKsEK2lTr/ZxK/iQ9YFDigS09wnSET7kuDll9VuKRRenm5ekSqbyE60wpDDp2Xm95TZB1xnIR62cG77F+9xCP/tBI4ujClDdinH22t2LiXSA6JB9E50wbfKoR2gwfnmOLmod2rqHJrhxURboV3erj1sE6pKyJyYzx+vLsUvT9EK1Xrx0MUWJgavveEIC3IRkkf3o/734GCz1OE5YB/7XIxfIXL0MGrb9s8v8KpNMmS+plByE7deXSMulV5lI0yZx+w0mlXxO/IXxoKzOjSLqX8vQRc4FSi/k7fpgLyfpapDhgU1WJEr7ALEYDCoHRcE+cs/2kWnGPLfekT5duSEsmZmaUtVNSYMA6z+9L7Frc79ZF8Xp4N2pa0iHHn3MAOR6Xg4VPHxg+s/GSIK4v/xuiAFTlCEh+DyMem5xEQaudWQfIHRPsk8d4RCHnoZPKSHwVJRbgEafz+kv p/8GHc4c bGb014wyyzFtzAoJwpqmSs4/pAJwMh+EzSBlu2JB+xC7QiTGgBLEY7hELJHuLEBvFRoi5Ili+6ZDGJ58HL7TUQYgi05INFnoA2g9sKkWGxYsWPrUHg5WodSjNldXXb6MTln0eJQue+EQYrWnc+DiglCfqv80RJamfOkrXEneEBJZqSFX5ZeijgyVS/ilSGsKHCNLooi4QEdeDvFmrcYJN5x5Ved7TaNoRAvICVZeKGwYEReiAEfoU//8IwTrnYxtTasOoC8P3OmTMDFDhouDvZJi0UH6TjHGvoSO7AEncCF1dxYH6YSJHBm6JvA== 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: From: Tom Lendacky Add support for the SEV-SNP AP Creation NAE event. This allows SEV-SNP guests to alter the register state of the APs on their own. This allows the guest a way of simulating INIT-SIPI. A new event, KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, is created and used so as to avoid updating the VMSA pointer while the vCPU is running. For CREATE The guest supplies the GPA of the VMSA to be used for the vCPU with the specified APIC ID. The GPA is saved in the svm struct of the target vCPU, the KVM_REQ_UPDATE_PROTECTED_GUEST_STATE event is added to the vCPU and then the vCPU is kicked. For CREATE_ON_INIT: The guest supplies the GPA of the VMSA to be used for the vCPU with the specified APIC ID the next time an INIT is performed. The GPA is saved in the svm struct of the target vCPU. For DESTROY: The guest indicates it wishes to stop the vCPU. The GPA is cleared from the svm struct, the KVM_REQ_UPDATE_PROTECTED_GUEST_STATE event is added to vCPU and then the vCPU is kicked. The KVM_REQ_UPDATE_PROTECTED_GUEST_STATE event handler will be invoked as a result of the event or as a result of an INIT. The handler sets the vCPU to the KVM_MP_STATE_UNINITIALIZED state, so that any errors will leave the vCPU as not runnable. Any previous VMSA pages that were installed as part of an SEV-SNP AP Creation NAE event are un-pinned. If a new VMSA is to be installed, the VMSA guest page is pinned and set as the VMSA in the vCPU VMCB and the vCPU state is set to KVM_MP_STATE_RUNNABLE. If a new VMSA is not to be installed, the VMSA is cleared in the vCPU VMCB and the vCPU state is left as KVM_MP_STATE_UNINITIALIZED to prevent it from being run. Signed-off-by: Tom Lendacky Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra [mdr: add handling for restrictedmem] Signed-off-by: Michael Roth --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/asm/svm.h | 7 +- arch/x86/kvm/svm/sev.c | 240 ++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 3 + arch/x86/kvm/svm/svm.h | 8 +- arch/x86/kvm/x86.c | 11 ++ 6 files changed, 268 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 2fcd309fd9fb..8f515e0386a0 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -113,6 +113,7 @@ KVM_ARCH_REQ_FLAGS(31, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) #define KVM_REQ_HV_TLB_FLUSH \ KVM_ARCH_REQ_FLAGS(32, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) +#define KVM_REQ_UPDATE_PROTECTED_GUEST_STATE KVM_ARCH_REQ(34) #define CR0_RESERVED_BITS \ (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \ diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index ac8edfdd60fa..0deb83ac800b 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -288,7 +288,12 @@ static_assert((X2AVIC_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == X2AVIC_ #define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF) -#define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) +#define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) +#define SVM_SEV_FEAT_RESTRICTED_INJECTION BIT(3) +#define SVM_SEV_FEAT_ALTERNATE_INJECTION BIT(4) +#define SVM_SEV_FEAT_INT_INJ_MODES \ + (SVM_SEV_FEAT_RESTRICTED_INJECTION | \ + SVM_SEV_FEAT_ALTERNATE_INJECTION) struct vmcb_seg { u16 selector; diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 111e43eede15..ec74ff5e09c7 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -638,6 +638,7 @@ static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) static int sev_es_sync_vmsa(struct vcpu_svm *svm) { + struct kvm_sev_info *sev = &to_kvm_svm(svm->vcpu.kvm)->sev_info; struct sev_es_save_area *save = svm->sev_es.vmsa; /* Check some debug related fields before encrypting the VMSA */ @@ -683,6 +684,12 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) if (sev_snp_guest(svm->vcpu.kvm)) save->sev_features |= SVM_SEV_FEAT_SNP_ACTIVE; + /* + * Save the VMSA synced SEV features. For now, they are the same for + * all vCPUs, so just save each time. + */ + sev->sev_features = save->sev_features; + pr_debug("Virtual Machine Save Area (VMSA):\n"); print_hex_dump_debug("", DUMP_PREFIX_NONE, 16, 1, save, sizeof(*save), false); @@ -3034,6 +3041,11 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) if (!ghcb_sw_scratch_is_valid(ghcb)) goto vmgexit_err; break; + case SVM_VMGEXIT_AP_CREATION: + if (lower_32_bits(ghcb_get_sw_exit_info_1(ghcb)) != SVM_VMGEXIT_AP_DESTROY) + if (!ghcb_rax_is_valid(ghcb)) + goto vmgexit_err; + break; case SVM_VMGEXIT_NMI_COMPLETE: case SVM_VMGEXIT_AP_HLT_LOOP: case SVM_VMGEXIT_AP_JUMP_TABLE: @@ -3259,6 +3271,220 @@ static int snp_complete_psc(struct kvm_vcpu *vcpu) return 1; /* resume */ } +static kvm_pfn_t gfn_to_pfn_gmem(struct kvm *kvm, gfn_t gfn) +{ + struct kvm_memory_slot *slot; + kvm_pfn_t pfn; + int order = 0; + + slot = gfn_to_memslot(kvm, gfn); + if (!kvm_slot_can_be_private(slot)) { + pr_err("SEV: Failure retrieving restricted memslot for GFN 0x%llx, flags 0x%x, userspace_addr: 0x%lx\n", + gfn, slot->flags, slot->userspace_addr); + return INVALID_PAGE; + } + + if (!kvm_mem_is_private(kvm, gfn)) { + pr_err("SEV: Failure retrieving restricted PFN for GFN 0x%llx\n", gfn); + return INVALID_PAGE; + } + + if (kvm_gmem_get_pfn(kvm, slot, gfn, &pfn, &order)) { + pr_err("SEV: Failure retrieving restricted PFN for GFN 0x%llx\n", gfn); + return INVALID_PAGE; + } + + return pfn; +} + +static int __sev_snp_update_protected_guest_state(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + kvm_pfn_t pfn; + hpa_t cur_pa; + + WARN_ON(!mutex_is_locked(&svm->sev_es.snp_vmsa_mutex)); + + /* Save off the current VMSA PA for later checks */ + cur_pa = svm->sev_es.vmsa_pa; + + /* Mark the vCPU as offline and not runnable */ + vcpu->arch.pv.pv_unhalted = false; + vcpu->arch.mp_state = KVM_MP_STATE_HALTED; + + /* Clear use of the VMSA */ + svm->sev_es.vmsa_pa = INVALID_PAGE; + svm->vmcb->control.vmsa_pa = INVALID_PAGE; + + /* + * sev->sev_es.vmsa holds the virtual address of the VMSA initially + * allocated by the host. If the guest specified a new a VMSA via + * AP_CREATION, it will have been pinned to avoid future issues + * with things like page migration support. Make sure to un-pin it + * before switching to a newer guest-specified VMSA. + */ + if (cur_pa != __pa(svm->sev_es.vmsa) && VALID_PAGE(cur_pa)) + kvm_release_pfn_dirty(__phys_to_pfn(cur_pa)); + + if (VALID_PAGE(svm->sev_es.snp_vmsa_gpa)) { + /* + * The VMSA is referenced by the hypervisor physical address, + * so retrieve the PFN and ensure it is restricted memory. + */ + pfn = gfn_to_pfn_gmem(vcpu->kvm, gpa_to_gfn(svm->sev_es.snp_vmsa_gpa)); + if (!VALID_PAGE(pfn) || is_error_pfn(pfn)) + return -EINVAL; + + /* Use the new VMSA */ + svm->sev_es.vmsa_pa = pfn_to_hpa(pfn); + svm->vmcb->control.vmsa_pa = svm->sev_es.vmsa_pa; + + /* Mark the vCPU as runnable */ + vcpu->arch.pv.pv_unhalted = false; + vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; + + svm->sev_es.snp_vmsa_gpa = INVALID_PAGE; + } + + /* + * When replacing the VMSA during SEV-SNP AP creation, + * mark the VMCB dirty so that full state is always reloaded. + */ + vmcb_mark_all_dirty(svm->vmcb); + + return 0; +} + +/* + * Invoked as part of svm_vcpu_reset() processing of an init event. + */ +void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + int ret; + + if (!sev_snp_guest(vcpu->kvm)) + return; + + mutex_lock(&svm->sev_es.snp_vmsa_mutex); + + if (!svm->sev_es.snp_ap_create) + goto unlock; + + svm->sev_es.snp_ap_create = false; + + ret = __sev_snp_update_protected_guest_state(vcpu); + if (ret) + vcpu_unimpl(vcpu, "snp: AP state update on init failed\n"); + +unlock: + mutex_unlock(&svm->sev_es.snp_vmsa_mutex); +} + +static int sev_snp_ap_creation(struct vcpu_svm *svm) +{ + struct kvm_sev_info *sev = &to_kvm_svm(svm->vcpu.kvm)->sev_info; + struct kvm_vcpu *vcpu = &svm->vcpu; + struct kvm_vcpu *target_vcpu; + struct vcpu_svm *target_svm; + unsigned int request; + unsigned int apic_id; + bool kick; + int ret; + + request = lower_32_bits(svm->vmcb->control.exit_info_1); + apic_id = upper_32_bits(svm->vmcb->control.exit_info_1); + + /* Validate the APIC ID */ + target_vcpu = kvm_get_vcpu_by_id(vcpu->kvm, apic_id); + if (!target_vcpu) { + vcpu_unimpl(vcpu, "vmgexit: invalid AP APIC ID [%#x] from guest\n", + apic_id); + return -EINVAL; + } + + ret = 0; + + target_svm = to_svm(target_vcpu); + + /* + * The target vCPU is valid, so the vCPU will be kicked unless the + * request is for CREATE_ON_INIT. For any errors at this stage, the + * kick will place the vCPU in an non-runnable state. + */ + kick = true; + + mutex_lock(&target_svm->sev_es.snp_vmsa_mutex); + + target_svm->sev_es.snp_vmsa_gpa = INVALID_PAGE; + target_svm->sev_es.snp_ap_create = true; + + /* Interrupt injection mode shouldn't change for AP creation */ + if (request < SVM_VMGEXIT_AP_DESTROY) { + u64 sev_features; + + sev_features = vcpu->arch.regs[VCPU_REGS_RAX]; + sev_features ^= sev->sev_features; + if (sev_features & SVM_SEV_FEAT_INT_INJ_MODES) { + vcpu_unimpl(vcpu, "vmgexit: invalid AP injection mode [%#lx] from guest\n", + vcpu->arch.regs[VCPU_REGS_RAX]); + ret = -EINVAL; + goto out; + } + } + + switch (request) { + case SVM_VMGEXIT_AP_CREATE_ON_INIT: + kick = false; + fallthrough; + case SVM_VMGEXIT_AP_CREATE: + if (!page_address_valid(vcpu, svm->vmcb->control.exit_info_2)) { + vcpu_unimpl(vcpu, "vmgexit: invalid AP VMSA address [%#llx] from guest\n", + svm->vmcb->control.exit_info_2); + ret = -EINVAL; + goto out; + } + + /* + * Malicious guest can RMPADJUST a large page into VMSA which + * will hit the SNP erratum where the CPU will incorrectly signal + * an RMP violation #PF if a hugepage collides with the RMP entry + * of VMSA page, reject the AP CREATE request if VMSA address from + * guest is 2M aligned. + */ + if (IS_ALIGNED(svm->vmcb->control.exit_info_2, PMD_SIZE)) { + vcpu_unimpl(vcpu, + "vmgexit: AP VMSA address [%llx] from guest is unsafe as it is 2M aligned\n", + svm->vmcb->control.exit_info_2); + ret = -EINVAL; + goto out; + } + + target_svm->sev_es.snp_vmsa_gpa = svm->vmcb->control.exit_info_2; + break; + case SVM_VMGEXIT_AP_DESTROY: + break; + default: + vcpu_unimpl(vcpu, "vmgexit: invalid AP creation request [%#x] from guest\n", + request); + ret = -EINVAL; + break; + } + +out: + if (kick) { + if (target_vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED) + target_vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; + + kvm_make_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, target_vcpu); + kvm_vcpu_kick(target_vcpu); + } + + mutex_unlock(&target_svm->sev_es.snp_vmsa_mutex); + + return ret; +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3508,6 +3734,18 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) vcpu->run->vmgexit.ghcb_msr = ghcb_gpa; vcpu->arch.complete_userspace_io = snp_complete_psc; break; + case SVM_VMGEXIT_AP_CREATION: + ret = sev_snp_ap_creation(svm); + if (ret) { + ghcb_set_sw_exit_info_1(ghcb, 1); + ghcb_set_sw_exit_info_2(ghcb, + X86_TRAP_GP | + SVM_EVTINJ_TYPE_EXEPT | + SVM_EVTINJ_VALID); + } + + ret = 1; + break; case SVM_VMGEXIT_UNSUPPORTED_EVENT: vcpu_unimpl(vcpu, "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n", @@ -3612,6 +3850,8 @@ void sev_es_vcpu_reset(struct vcpu_svm *svm) set_ghcb_msr(svm, GHCB_MSR_SEV_INFO(GHCB_VERSION_MAX, GHCB_VERSION_MIN, sev_enc_bit)); + + mutex_init(&svm->sev_es.snp_vmsa_mutex); } void sev_es_prepare_switch_to_guest(struct sev_es_save_area *hostsa) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 77195d8c1aa3..81b9f4e04a8d 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1358,6 +1358,9 @@ static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) svm->spec_ctrl = 0; svm->virt_spec_ctrl = 0; + if (init_event) + sev_snp_init_protected_guest_state(vcpu); + init_vmcb(vcpu); if (!init_event) diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 8dc7946ab634..e73a58e489c7 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -94,6 +94,7 @@ struct kvm_sev_info { atomic_t migration_in_progress; u64 snp_init_flags; void *snp_context; /* SNP guest context page */ + u64 sev_features; /* Features set at VMSA creation */ }; struct kvm_svm { @@ -208,6 +209,10 @@ struct vcpu_sev_es_state { bool ghcb_sa_free; u64 ghcb_registered_gpa; + + struct mutex snp_vmsa_mutex; /* Used to handle concurrent updates of VMSA. */ + gpa_t snp_vmsa_gpa; + bool snp_ap_create; }; struct vcpu_svm { @@ -735,7 +740,7 @@ void avic_refresh_virtual_apic_mode(struct kvm_vcpu *vcpu); #define GHCB_VERSION_MAX 2ULL #define GHCB_VERSION_MIN 1ULL -#define GHCB_HV_FT_SUPPORTED GHCB_HV_FT_SNP +#define GHCB_HV_FT_SUPPORTED (GHCB_HV_FT_SNP | GHCB_HV_FT_SNP_AP_CREATION) extern unsigned int max_sev_asid; @@ -764,6 +769,7 @@ void sev_es_prepare_switch_to_guest(struct sev_es_save_area *hostsa); void sev_es_unmap_ghcb(struct vcpu_svm *svm); struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu); void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code); +void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu); /* vmenter.S */ diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 10d76afa23d9..9e3c41e2a3ef 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10621,6 +10621,14 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) if (kvm_check_request(KVM_REQ_UPDATE_CPU_DIRTY_LOGGING, vcpu)) static_call(kvm_x86_update_cpu_dirty_logging)(vcpu); + + if (kvm_check_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, vcpu)) { + kvm_vcpu_reset(vcpu, true); + if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE) { + r = 1; + goto out; + } + } } if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win || @@ -12816,6 +12824,9 @@ static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu) return true; #endif + if (kvm_test_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, vcpu)) + return true; + if (kvm_arch_interrupt_allowed(vcpu) && (kvm_cpu_has_interrupt(vcpu) || kvm_guest_apic_has_interrupt(vcpu))) From patchwork Mon Jun 12 04:25:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275560 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 806F1C7EE2E for ; Mon, 12 Jun 2023 04:52:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 00D526B0072; Mon, 12 Jun 2023 00:52:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EE2478E0002; Mon, 12 Jun 2023 00:52:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D513A6B0075; Mon, 12 Jun 2023 00:52:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id C61336B0072 for ; Mon, 12 Jun 2023 00:52:15 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 9E5C7400BD for ; Mon, 12 Jun 2023 04:52:15 +0000 (UTC) X-FDA: 80892874230.27.B1B40F8 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02on2054.outbound.protection.outlook.com [40.107.212.54]) by imf29.hostedemail.com (Postfix) with ESMTP id AAF14120010 for ; Mon, 12 Jun 2023 04:52:12 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=EfGg3KHU; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf29.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.212.54 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; 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=1686545532; 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=sKJx0R4sv5FkQcubDKo/v4xIgCFKS+E772CH7PG+a7A=; b=SCKl+O+a96nCtgV7oejWeXD5+cpXGdyh9l8eYAhljCB+3Mo/zGtEBUTojoEi6n4/FJvq30 FH9xCLBYTL3MjatIT4+UjeHVy32we8pToQoAGhoRHRWfm+MmLdITVRe4eLlUAcz0buOCwP T7DRkrAoIbfNugvZIp/DNL2gfOGEDnY= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686545532; a=rsa-sha256; cv=pass; b=hymEtwwof8Z6jrFnM1RrR+Clm0x+JT5bSh+BFDb/M+LyEZq4ThKqoukUe1Ogb6GPTRUWAz C8t2wEjCweXqva1NfuiIgI4SF9cmuJpRYtF+t7PMBPdyzejjfVhRS1fsbIAC7N8U7dpsaz Z+ajmIc6lusvg1VEjAzg/1us8XHbNNY= ARC-Authentication-Results: i=2; imf29.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=EfGg3KHU; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf29.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.212.54 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LxeWqIGdkhCYuov+4xBX1NWAVaVJCAfokYratb0heeYvUMHzHTF5qI+5ncuR5OhpPRWCMAGSAT6x0mMWE50uYPtu4SG0vTlefL/JFOHTnVNT1I6nAkkEH9NdSmfk8lkH9unQ9KptzrrgKu8D1WuggZmbgffMGFTq4PyJ/rJPIkieOiLr+RdUd3OS2nz+TCvg/3tPtdk4MfsQYa+Kk91xS5vHY60O9YEKoICMOsfSthn9DbfykXEbt/5qV6+utA03Xk0zEUQzlO06gKASTXNr0hv766Gr77R1rveq1p9z0DfRCHEvgTzV6Z2AzZKLBsFP4extTM8TUlpihu8gqr+vBQ== 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=sKJx0R4sv5FkQcubDKo/v4xIgCFKS+E772CH7PG+a7A=; b=O3mm5XdQKg5O959KSCIKfa7DPmR8cuoHkQkwM9OS2Tl/tM+VmpmHhWBP07PLCsGpPggqHbjZpuUCA2tq2NQdb69YHc65W10CP3ivVjPiBzKQNeU0XnN9sEq1/pbhIp7ylS0wHSo43RCo0ZFn/JeyKmXHaGefuKIZD9Kx3sMg53fpJF30KBRc0Y7+selMZqPDpsWQe52wH1c8CS77/KgZ72eKqskQmb7mL2ThRf8Tuwoyj31+kDlDE3Lk0at5UHE0IhrBDmeQJAM8XbPjbssBcrYCOcI89pk1A7kqQRFfk0zIEEmdF8cjqT73FAAbdWQeX6dvsrvHm41OmDtgCnBNrQ== 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 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=sKJx0R4sv5FkQcubDKo/v4xIgCFKS+E772CH7PG+a7A=; b=EfGg3KHUhajVvLRs0Lb2DIVSrMtwI1r38XO3vQidQ8jN5aHTYavFvJw3TRk4FTF9HCHS/fq6W95K5CFIioLtlbDWEpjWd+Ov0b07SRLs/0VSu+ADd+at1OCklAfVnelrlnuuas95u/1LBjs9tkq+uEhyT+Mzkcv0etHOz/NmpFs= Received: from SN6PR08CA0004.namprd08.prod.outlook.com (2603:10b6:805:66::17) by PH0PR12MB7792.namprd12.prod.outlook.com (2603:10b6:510:281::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.19; Mon, 12 Jun 2023 04:52:08 +0000 Received: from SA2PEPF00001509.namprd04.prod.outlook.com (2603:10b6:805:66:cafe::f1) by SN6PR08CA0004.outlook.office365.com (2603:10b6:805:66::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.34 via Frontend Transport; Mon, 12 Jun 2023 04:52:08 +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 SA2PEPF00001509.mail.protection.outlook.com (10.167.242.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:52:08 +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.23; Sun, 11 Jun 2023 23:52:02 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 43/51] KVM: SEV: Configure MMU to check for private fault flags Date: Sun, 11 Jun 2023 23:25:51 -0500 Message-ID: <20230612042559.375660-44-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: SA2PEPF00001509:EE_|PH0PR12MB7792:EE_ X-MS-Office365-Filtering-Correlation-Id: c99a4267-3fa7-436a-e8e3-08db6b00c6fa X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: HyjRnG38AX9bs9UjcbW9XGreVbmQQPpE2V285In5KT1Ed79j7kE+Xm5rXnl8lMChvClUNfaCvP2DSEJtSIglR0ilpyt+/caOv9KLN6UIERaj0s4yCuc4a1QD3CPh7DFbfVkY/JVJDkco6PyqWmA+VYiI+l2ja/1lgW5UBBDEXC0enWCCdeEhyHqzVfx66loCtGk01Gkw0QxvGnHnVWHCyOA9fTszysabp7thQyjBC49n0RIaUNNxQ45QM2ofTc3RSlogrsZPsd/D2LugMKP+og5pM2POU1ab/btlpZubaYaLwq35dd/Zczk5c06eQ8d7HU9Yrf2FPML8junDaueXN+crEnVfW0dmW9CUHSd05ioHCqghJsCx1UVGZohqSiNSAE17BJqQPSQy/8N7WJFW5W4Y2OyTetSbAHS9SgLCtv+76nLaakMwxliG+kQkoOOkpqGt5dZ1Vc0vE/sdqazJAvuF3hTgM6r/qiR2FWYu864AHpfGKHc8UWhUf/tv0Sehdz1wdBUqei+TiESI5jHFrsNactQ4ru+LNvCdOsMhI9Tmbl0AOKntM/TQVM14gHYnh+Lm3OXjIRbqRfg+d3eVNvf1nsdatB3cO92reQOe+kS2KDsNntQu7zsZoaATe6rGq2lByDIJ/3pA78K/AOdVQZ+Wr2ZdW8QF+qONO6IWzQVpgCLODNKDHtPBryApihzHzaq8TEEVFaGJOOHY6IV42XMfGME1TelH32bv1tvG75SkbjbVH2/v2ECBmKgFSrryT+rqtNxC771LK/Np4vYJ9g== 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:(13230028)(4636009)(136003)(396003)(376002)(39860400002)(346002)(451199021)(36840700001)(40470700004)(46966006)(47076005)(16526019)(426003)(336012)(86362001)(186003)(4744005)(82740400003)(36756003)(40480700001)(316002)(81166007)(356005)(7406005)(26005)(1076003)(2906002)(44832011)(2616005)(36860700001)(5660300002)(82310400005)(7416002)(41300700001)(6916009)(54906003)(4326008)(70206006)(6666004)(478600001)(70586007)(8936002)(8676002)(40460700003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:52:08.4780 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c99a4267-3fa7-436a-e8e3-08db6b00c6fa 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: SA2PEPF00001509.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB7792 X-Stat-Signature: pxiho5619jf1f91wowe1n9iibxiwbzx5 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: AAF14120010 X-Rspam-User: X-HE-Tag: 1686545532-662472 X-HE-Meta: U2FsdGVkX195+no0jK3fui1B95oOGh7ww4vPWF6qOucLYc8QzZkGEd3vXxAFyfvTEh8nHPTyFsUvHQAxwAksGgAhbLzV1tKOKZ323cPE9PsuQK5C2IUoFnDmnjn9La5C0XMP2hPU48pIduBDwGVSAX7OqHAsDGZ780bID6Pqwjw+itl0FL/KJENdqHfxih+3HmKg4o/ZiNyoHQl1jSnREBGg1HQb8ozIrMjlt7N+SCFccD6BOOIoDWKqbdU0FQDaCJrL+LDsXPik4Fg6a9L1sTQZuUBIA9o9HGyvJEKcf5e1nEYhJbkZMT7JbAmCEHmNqQL2cBVhWo4mJ0PVT4Z8ATiSP0DnB3sJKR5DM5Dkrv85XoDNENgPtrzIAX94Egbqn2Y7u91ScKH3uAlqwE2WZQXL5mQVPxp8C6tYl3t2Zopp4KSUeyDj2jhv0k3L59kfVy1IuxkBc/hP0yeE+sX16P6Dz3oGSLqKg4jhTlLovWVofTH0pO1z8RLEFmg6ADQ/jPTAyIdzVYDpVzUsEtkzrZ7TgkOf4AEWBeDJDik6NvuS2cB5SDAvYj8jC1QasPsoZefiSs9zZn+Ad3v0DF7EaDauPQTAeXX6i91uMUmNdKu1CflEvS7yxH+6pUP7F8jMsVQc3szKKWAUMPMRgZNvU5Wdzb/+wZmMf0rXr5GP9UdRTsAJUutaFv2OzAC9EOp7mXMY3SfojjzyuV9H9QfTbGZOEYWLT/cTY6T1HK2coYezBj94ubK6efqMf+eievr2WP9AzGqP8cDWy0f0oR10KK+YHiP3kanIfZnCkwq7nhJCblYDYIrPNs8DrVn2KkVOdZxRc6zw/5w4dph/WKURXQQNXjatSwlVwfrH78hUIKMHTcCGjpphulU615gOdKdl3VSRus7DXKR9iHPeuPDCO3wnPscQW5AVv67L7ic/IRXeo975O7zSGjuxc/cai1JLdcqsfed3LzjROkF2hWh mlV4uFvg ZXJMAC6zUyQNTiX1ORn19+FmGc4X/NnFSHC3q5ownnEt+0TjvyetKTprkrj6Pa5RU9h262DqE52H5JQM/dB8DvhRGW3yvM1M6IED2ksjdV5bZzTZZHCCXm3BqYyf5tJEkGSh8U00CvnWYOs5cmSfgfWM1VdJJk13wXWMevCbQRfEs1ULHsOnKKD9800+52mUyBldMdVRSOiWVMErtSNvuWB3VzQ3kkwl7yxzYTPmdVTJFrabIsA6/f+wN/kFHQpLhI4x+5sPjMoLAWjq/Y27QoD6Ao+Wc0BkIJYB5rbtBT6MEGYx8a80Gk1kMQQ== 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: SEV-SNP uses a #NPF flag to indicate whether the guest expects a shared or a private page, which in turn dictates whether the KVM MMU should map a normal page or a gmem page. Set the appropriate mask to enable this handling for SEV-SNP guests. Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index ec74ff5e09c7..909ecd90d199 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2006,6 +2006,8 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) if (rc) goto e_free_context; + kvm->arch.mmu_private_fault_mask = PFERR_GUEST_ENC_MASK; + return 0; e_free_context: From patchwork Mon Jun 12 04:25:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275561 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 D5741C7EE25 for ; Mon, 12 Jun 2023 04:53:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7D6346B0072; Mon, 12 Jun 2023 00:53:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 784498E0002; Mon, 12 Jun 2023 00:53:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 625006B0075; Mon, 12 Jun 2023 00:53:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 550486B0072 for ; Mon, 12 Jun 2023 00:53:24 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 24DC6400BD for ; Mon, 12 Jun 2023 04:53:24 +0000 (UTC) X-FDA: 80892877128.16.A6A4F9D Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02on2054.outbound.protection.outlook.com [40.107.212.54]) by imf08.hostedemail.com (Postfix) with ESMTP id 375D316000D for ; Mon, 12 Jun 2023 04:53:20 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=3ZLzc0RV; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf08.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.212.54 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686545601; 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=WOF0hFPBW7R+0qZTxRk+TtpaZhSYVvpORM/XQ7vmlUo=; b=k/aGvHDvRet0BHBDQcyHaz26BVKHlXIrmoQ+wRlK9YyXSfQkJxjAfCcqDg/xel5tRilmKv jF0VDgSSa/4FNtD+ENiJcdZ+JL7kpW0B3k0bsSz33qKPwrWjFUrRhAOw5Oj28f8fx45mXb 9UaB3h4lESQJylTtri5f8A3jSBIvVlM= ARC-Authentication-Results: i=2; imf08.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=3ZLzc0RV; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf08.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.212.54 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686545601; a=rsa-sha256; cv=pass; b=QHTzFufD2XsGbNciauNdLSBU+U0ytMl6MVP/xTasgxVrZ86RRbmKW86FNLncceCOFYyxn2 j66LfmdfLos8jqKf6a4f38b6Zy+IXq5FX0lPskYl83bg0EZYiLicMT4zUGUsDPvoLFruXe 3rJAbEOmIkBbTOiEOQVcfxm2jjo6idg= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AYWJ+BPq3u4kFEGUa3D7tD9gXtCGgsJc5FwFeLskZXsiqIgMMbxYgwcuO4Zd1Jymaxv1ehNAK7P3RoHwKB9ddeYvtIZWDuuWw2P38lceIIdlRvHYNCIFNdkI/3I9/2MsB/44LCANICqA81axkE9oZ/I/gEAl3JRfvF0Tnl7btkGFA+dLPCbLLSqcZT/ly36tOdrEgbDWBVPC5W7bLhMEhHDBe41GJ+S6y+mlfniBPPs4nauND3N7YZ0JHLvA0oM4h55+2uRqESzr9DuWHzstuCcOivI7OuXvMq/yjU3TdGXpPy8XUIQMgFBMLj16Us9M+YB+EvB4pmRICQNgvgNokg== 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=WOF0hFPBW7R+0qZTxRk+TtpaZhSYVvpORM/XQ7vmlUo=; b=hUZZv7AFFPuRm4j2wJLb/u9JNf6//pH+SMAC1mdfgFe8loZbolvIUQcIzx48n/caDJg7m2gYiRKg5QbNRXyIz8EbaoA/qPBkerW7bEJ/s0QFvbatRvuWZaCGRmUi2sw3t9wP7c5MGz3Cqa+0YjInIXm5Jyu1g1TKTUTvDkQ3lPj/yvZdQOJh5SseSCL0b3rIeV05eIQwIttaBDZhs2WgvMjoZE4Ad4JP2sQs9lDnqr82sNhal6/RGXF2eS4bs3OWMEak/rVdOEqWb9x5C5QNrNS1A8OOdpRqfXZYgXLAUvHir+qntwNhySVrRAY178KOvzFZ+Q6B5TKVgG+TTU5qLg== 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 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=WOF0hFPBW7R+0qZTxRk+TtpaZhSYVvpORM/XQ7vmlUo=; b=3ZLzc0RVys1/wb7SyxYwo0RmL6rRAugPzCp7YFc+lazIax3sYqYbXyJ6Yu+ZYe1Ju1xbz22ri2YfckYMmT8kW/V87N/GOH4NTX/qc5UrV1OIZxOxM67HUcTtNoO+xGEIRuITzphYGTiFPIVx6K/J6Dgo2glqgY4nKie+bfwW6/4= Received: from DM6PR06CA0043.namprd06.prod.outlook.com (2603:10b6:5:54::20) by DS0PR12MB7771.namprd12.prod.outlook.com (2603:10b6:8:138::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.42; Mon, 12 Jun 2023 04:53:17 +0000 Received: from CY4PEPF0000E9DB.namprd05.prod.outlook.com (2603:10b6:5:54:cafe::c5) by DM6PR06CA0043.outlook.office365.com (2603:10b6:5:54::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.34 via Frontend Transport; Mon, 12 Jun 2023 04:53:17 +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 CY4PEPF0000E9DB.mail.protection.outlook.com (10.167.241.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:53:16 +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.23; Sun, 11 Jun 2023 23:53:09 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 44/51] KVM: SEV: Implement gmem hook for initializing private pages Date: Sun, 11 Jun 2023 23:25:52 -0500 Message-ID: <20230612042559.375660-45-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CY4PEPF0000E9DB:EE_|DS0PR12MB7771:EE_ X-MS-Office365-Filtering-Correlation-Id: fcd15a63-b251-4390-65c3-08db6b00efd8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: dqS0zYY7V6/SXVxsgnGA//ryGTcwnR4TiO8wPG4RvRQbNzIzLJeMo6pkNE2kP4bhaCGYHMtwT1BEclNiqf0CS21osC+D4ag3hGOQllS1GMTBA0DXk/diympqflIZrmRfs4hrutXHit4uRqu8f8nx729JimVp8R7G0CO5OMYP+23hdLakSL0a+cdjys1ak5201JgAuBC7X2ey4Enmkz3jqjRuIuw5NGkEdWKhFBr9s4tZkc07GsP9UeeBQhFthk2rzbeM1cf2lEI8/mOHMrmnorfkF97kj1erwyUUgKtHDcbM4mH4zxeXCsNUIvaN4vYvdhtAdE3gmoPVMY9XQRea6mygrn4WlrDKDck7V8rJNp/ERAOsbf5Xqi9gkRToL7So7JI5kp8adTWwYRUjlOxdc2B/6CTbWqFznejmR0vW8erTl/1gn/9G3rCr6WlkyrGRU7fzQUyye2bM7cJL+vvmWvC3BE+Oi1eshgEjXwJXFQnuKqCvsLVy0oUYkJL7qhAemNdH7vZ5NoLpomRLWDkrtuRLaLxpupOCzyFTZIMKfNSGsplg6LlV230gSF7LvJK2EYN8vn5Xlw/tn2pCQe4H/r2lSb5plEMZ8kcGASNMzlmgntbaACdu4IZAvlN8VrIl2iPhneDaZKgSGrILa6pBjKE4ebVUAEwbA4uiBQt8HhIa0b+6CyCjbBuu9iWk8MkAgdj4VKQT8/1jWrSk05Pq5OOH0L1ncm7Efe3Rof4eZRxaNAfhhaU9VLpSMD+t+LY3Aaw17l84v0ktGzHyI9JKkg== 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:(13230028)(4636009)(376002)(396003)(39860400002)(346002)(136003)(451199021)(36840700001)(46966006)(40470700004)(186003)(2906002)(426003)(44832011)(26005)(1076003)(7406005)(40460700003)(16526019)(7416002)(36756003)(83380400001)(36860700001)(47076005)(82310400005)(86362001)(82740400003)(356005)(81166007)(40480700001)(2616005)(478600001)(41300700001)(54906003)(6666004)(316002)(70586007)(4326008)(70206006)(336012)(6916009)(8676002)(8936002)(5660300002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:53:16.9759 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fcd15a63-b251-4390-65c3-08db6b00efd8 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: CY4PEPF0000E9DB.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB7771 X-Rspamd-Queue-Id: 375D316000D X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 7q7h49icb7q16iohof78bqk59bjtkh3z X-HE-Tag: 1686545600-340041 X-HE-Meta: U2FsdGVkX18q99MADHnPCNAeCCBNH2iNoLm9eOgk5mTpsXZLutfexptHt/wtj6zUF4n9aAqKjZ/yEjkOFDZOclsdffVNN5F+A+P1uyxP2xaV8l41/dqvSyyrJ5w+W6lmi4p9AJQFq8a5/SaUSKLqWI53U2OwfTmUMzmd9IS1CbMDA9HJ5WDAbsNkS5rlWN2NpmTLgXf8IKFS+5YgSJqUudvudrqHXGWgpra3Lz8mbcdyXYeAoBzp26W40EK3iLP2D+utd6Rv60CgP8ZMTSsgJqiDcPpP93o67/ymSXsmSRgChOoT/lFDaHoLH2txmMsW86ghxMVV59FK7DL7KX4TifKxQ76owt4W8lzUqTeWTzux10KDNNgYjCN6D8djFOddFitgPx5Em9GMzceBJR06jsR+k5s8mrzLiCNG4WLZS7BuX2hkKX7P8balsH9/r8epvQG3VG3RnYB/DWxwiBxRwqHiZ5w8SGs8cCrrph5t6hdJPPEu3U1eLYy7Gi+w8CCRkIrO2JaAqFCLRDT7AvN45qLaAUe7Y34QhV7yiA8g4RloGLmHe8XnOBG/JovI7JoSXjxMU6UizUSKMDCFQdbM6eSWIW36Np3PKjda7sxkzCO5bPPKPx1LATYwIdjGcMjHoMfaV4Rbo+u4qtzdJvZWUCMW/ASytVcbWiGgU+EapmK4gT+0HujyP7/+5w8D4SKFjeU3Ov4VH50FsUBbBJ4mE4AuWAalU8rU/vjrdFc+z4OjNJr/N7rslaM3/hBIV9+g85agX4kuo4qoEOxZ3L4K+3Gc7prEl8HBVJER0AoLaeUHQCmHW8W6nhza/ttns03clSnKMJpFGa9xdfXtx4BGb4zXd/a3yIBGSjlsJ8KHFWoBggaWbSjDC0EDXkfPq4K+FeUVEdpWkFJK0+z4+uk0OqIhkhztlAP7D2iW5Q2sJKyr/i//5mwXlvXdgW5pU1VY16JC/BVXsR/m+Yqfu5R UszTkwT5 OAhkBqXANUpxEVC5/ec4iW41lgrP02jPaF6hNztlxayOB5b0IRwawo9xG3OxaiFfXkkoXM1BGvKUBVNwhElkABqkJd2jXNRlhr1ZrFOm5XdZG55xRlXlqv6nw19nrBvycmZxk1ImLKrjENwqLp+oTM/693ZjATR+/4rig0w5E1Oer9i31NQxT2iSZWcUbPEtiBUyMP4gz0QhYZPoMyor1vXnlVTyPlGenmUb2Md7RKSYt8QacsS8LyWA8Kf3LGoPB4mW2YqgHDG+pOA3Tazuy0o4CXdJCNZy7jfkswP/4whKC8ma9uT/MGF6etg== 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: This will handle RMP table updates and direct map changes needed to put a page into a private state before mapping it into an SEV-SNP guest. Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 95 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 2 + arch/x86/kvm/svm/svm.h | 2 + 3 files changed, 99 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 909ecd90d199..c5a1706387bf 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -4022,3 +4022,98 @@ void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) kvm_zap_gfn_range(kvm, gfn, gfn + PTRS_PER_PMD); put_page(pfn_to_page(pfn)); } + +/* Check if GFN range is marked private in the KVM/gmem xarray. */ +static bool is_gfn_range_private(struct kvm *kvm, gfn_t start, gfn_t end) +{ + gfn_t gfn = start; + + while (gfn++ < end) + if (!kvm_mem_is_private(kvm, gfn)) { + pr_debug("%s: overlap detected, GFN 0x%llx start 0x%llx end 0x%llx\n", + __func__, gfn, start, end); + return false; + } + + return true; +} + +/* Check that no pages in PFN range have already been set to private in RMP table. */ +static bool is_pfn_range_shared(kvm_pfn_t start, kvm_pfn_t end) +{ + kvm_pfn_t pfn = start; + + while (pfn++ < end) { + int ret, rmp_level; + bool assigned; + + ret = snp_lookup_rmpentry(pfn, &assigned, &rmp_level); + if (ret) { + pr_debug("%s: failed to retrieve RMP entry, assuming overlap, PFN 0x%llx start 0x%llx end 0x%llx RMP level %d error %d\n", + __func__, pfn, start, end, rmp_level, ret); + return false; + } + + if (assigned == 1) { + pr_debug("%s: overlap detected, PFN 0x%llx start 0x%llx end 0x%llx RMP level %d\n", + __func__, pfn, start, end, rmp_level); + return false; + } + } + + return true; +} + +static int get_supported_rmp_level(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn) +{ + if (!IS_ALIGNED(pfn, PTRS_PER_PMD) || !IS_ALIGNED(gfn, PTRS_PER_PMD)) + return PG_LEVEL_4K; + + /* + * Check that both the desired GFN range states in the xarray, and + * current PFN range states in the RMP table, are conducive to + * creating a 2M private RMP entry. + */ + if (is_gfn_range_private(kvm, gfn, gfn + PTRS_PER_PMD) && + is_pfn_range_shared(pfn, pfn + PTRS_PER_PMD)) + return PG_LEVEL_2M; + + return PG_LEVEL_4K; +} + +int sev_gmem_prepare(struct kvm *kvm, struct kvm_memory_slot *slot, + kvm_pfn_t pfn, gfn_t gfn, u8 *max_level) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + int level, rc = 0; + bool assigned; + + if (!sev_snp_guest(kvm)) + return 0; + + rc = snp_lookup_rmpentry(pfn, &assigned, &level); + if (rc) + return rc; + + /* No conversion needed, just clamp xax_level according to RMP entry. */ + if (assigned) + goto out_adjust_level; + + if (*max_level == PG_LEVEL_4K) + level = PG_LEVEL_4K; + else + level = get_supported_rmp_level(kvm, pfn, gfn); + + rc = rmp_make_private(pfn, gfn_to_gpa(gfn), level, sev->asid, false); + if (rc) + pr_err_ratelimited("%s: failed gfn %llx pfn %llx level %d rc %d\n", + __func__, gfn, pfn, level, rc); + +out_adjust_level: + pr_debug("%s: pfn %llx gfn %llx max_level %d level %d assigned %d\n", + __func__, pfn, gfn, *max_level, level, assigned); + if (*max_level > level) + *max_level = level; + + return rc; +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 81b9f4e04a8d..9085a122907c 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4934,6 +4934,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, .vcpu_get_apicv_inhibit_reasons = avic_vcpu_get_apicv_inhibit_reasons, .alloc_apic_backing_page = svm_alloc_apic_backing_page, + + .gmem_prepare = sev_gmem_prepare, }; /* diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index e73a58e489c7..0438f52e4396 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -770,6 +770,8 @@ void sev_es_unmap_ghcb(struct vcpu_svm *svm); struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu); void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code); void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu); +int sev_gmem_prepare(struct kvm *kvm, struct kvm_memory_slot *slot, + kvm_pfn_t pfn, gfn_t gfn, u8 *max_level); /* vmenter.S */ From patchwork Mon Jun 12 04:25:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275562 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 520E0C83005 for ; Mon, 12 Jun 2023 04:53:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A1F886B0075; Mon, 12 Jun 2023 00:53:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9A85C6B0078; Mon, 12 Jun 2023 00:53:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7D35E8E0002; Mon, 12 Jun 2023 00:53:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 6C1526B0075 for ; Mon, 12 Jun 2023 00:53:40 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 3635FAE24A for ; Mon, 12 Jun 2023 04:53:40 +0000 (UTC) X-FDA: 80892877800.16.A811BAF Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2073.outbound.protection.outlook.com [40.107.237.73]) by imf26.hostedemail.com (Postfix) with ESMTP id 403D2140002 for ; Mon, 12 Jun 2023 04:53:36 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=bB+ToZTH; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.73 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686545617; a=rsa-sha256; cv=pass; b=VaYfEvq3Yx1WqFCuhtTwnwxpIjBe6daHA/lO/AYcfje9eLLq1E8x5SlvuOukWsKorbpCNW Kj/I4y6HWJzoOHEVZhqx8+406I8QF55W+SVD71NV1f9WCePh1MEYhWW44axnzfBefvCQbf C03WjpGnSKOLG/3KWLZxRpnStjY9AXU= ARC-Authentication-Results: i=2; imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=bB+ToZTH; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.73 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686545617; 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=oCqcVUonepiEjlzgOGv6GcVfSdkI7MUY7xviMc0Iq1o=; b=m+Q4DobLz9U4f+Sau8GqIFr/GjpbQnOJZq2ju+NlVnlnWywGbKRSqpcIMNhftBQQuNNm0B yW/NYTvIP25An5KvVrGF5eVDO/1r6RRwmJNI/og/gDx6snVvabisbi+cnfNOYQGbr87Ace S2RXkyJmRsNSZ0BIY/VqiiW4wY0AuAw= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YsKC+Tj6vv9K6MyEmihXturAIvX2cRP14OEt/ylsvYSlcrHxSXwfCTnkMpiKea1bAY9oWxnsiw7wmpge0vIVjGD+srUnCCxACy2zlrtMKFYzuUdvBCmB/DbLYVF16GGpUFmKw3cMUlK1DgH4FprQorTONXYu3I9ZF/sbvjRipOpli7y3x/kKEGeiICooqNGKMC9LjehqA1Q4ODjjXjYAk+/VIaYC6SQ79OYDYd8Z7eJ8VKfupt/ZiqCpR5vrIYd06DbcvKQTLElMDznoE0Nh5HVjIT0BVdlGIPxfUwJizI5D9N7Miw3PWfPyT9p1zMzb6bwLobIPnqrFQbVWsBD0yQ== 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=oCqcVUonepiEjlzgOGv6GcVfSdkI7MUY7xviMc0Iq1o=; b=DVzLFdxVG8FYtW6qhAb1qxIcDQfI9abm5VKdLdhOVyxAaTiny5PlNB3A1n1Pdmj0NZDMhOPNc9sETaIwsay5LqRixU+8l//WMdPBAJZevWF2ohr7mSl9Lgb2fXtRqlSIVImIGatTU/hQ3zTaefc6K0KglxwNFMzIhcfdfksZvQl1cfA2p+6wmjxFap/Uwj56HjpMrt96e0uZ/aph2agopveKej9lus0e6nZVOOD5Nv2D6OrC/LNq0+bG+ho7tqp0UaF8lFUkqnZRIBBxeWXu60Iphtu0sQymoQodxLfkJIyHcmZu0iHGXFftGGHoYHdo5VS5EAXSBH2ver5c1rs4Jg== 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 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=oCqcVUonepiEjlzgOGv6GcVfSdkI7MUY7xviMc0Iq1o=; b=bB+ToZTHqjgjvUENeAZzDmvV9kQFy8vk+QhA0ogyshgqRzLxPqtJMsPN5qb1Q5f23xtyIp8sbphWG48X6kHvzLF42831z1BOnsy23mp3R1Ft+vBSmVDd8J8//sSw8TyhBNVeIkSGa0qhJ5Ol/0DcPaDvS+s4Ud8HULGtSojEgTc= Received: from DM6PR06CA0051.namprd06.prod.outlook.com (2603:10b6:5:54::28) by BY5PR12MB4033.namprd12.prod.outlook.com (2603:10b6:a03:213::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.38; Mon, 12 Jun 2023 04:53:33 +0000 Received: from CY4PEPF0000E9DB.namprd05.prod.outlook.com (2603:10b6:5:54:cafe::a4) by DM6PR06CA0051.outlook.office365.com (2603:10b6:5:54::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.34 via Frontend Transport; Mon, 12 Jun 2023 04:53:33 +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 CY4PEPF0000E9DB.mail.protection.outlook.com (10.167.241.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:53:32 +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.23; Sun, 11 Jun 2023 23:53:32 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 45/51] KVM: SEV: Implement gmem hook for invalidating private pages Date: Sun, 11 Jun 2023 23:25:53 -0500 Message-ID: <20230612042559.375660-46-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CY4PEPF0000E9DB:EE_|BY5PR12MB4033:EE_ X-MS-Office365-Filtering-Correlation-Id: e5dfa6b3-63fc-4e73-ad9b-08db6b00f95a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: CcduQt2Zpch0HP3FPh+ToWmsS9MZaye7g2VwFR3+IEo864sV4H0DUKFBuJHsf1RGkzjwo/umEnCy0OnfsojR2Q/c4wES/YOj3o4du7n6b2tLNMkeEaByJz9pYbphkQfHHEAfqa6cLmTPZ+MFtEGKVPvOtLfy3O+/5h58w0+CiyLSFk1BdRBYGn5yktMltMVdFe+THcBkIos1s4u8A4wQ91RJgiKzuFBol1fqxFcrdgc3cEEizrNZGbVCkGsONU8AyLOrqijW35vycipghflrWR4NAxmpw5uYbb30G1yUfjM/3k6Mk+7p8hc62aM0QMZ+qfKPquZpndjGmnpK/ALYodPaQz0bSruT+WOPlfIV+bWMuER97u4K0r0+7VnMz49nC8BnAiC3pLgbi87zv63mmKQdjEbuAEL5Mp5r85IbUe7iy5YwJQAjEjnoN9o6HJSnqGy5CmOwpRgjIVrESAIm7l6KD1j+k6tqMe45tUno9VuoMTtSf/TIZj5iexNWzfLgRvb0XPp5XSkrUbx/rq8VllAKmPPAMbSSkMDDX7oyxEcpgCVXAbjXmqo00Q0kTH6Gr383tBS9lC7rTxSw9svZQYXZYau+DXOzEqx62egIamfi7mcIsO/LmlHBpEJHPc9AdFk3FoJd9qu29aAPhQrpw+tFHIOwumOTrJh64Esl4TxvvdSVw9AIRUvFZ7EBvjyOBDeBQ7T355eKAql+igUNTKNsQYu5tjwcS3TGlfvtbDsI4uAQSDSICB6QoNVU0zeANazb8badDm/qNpone7txuA== 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:(13230028)(4636009)(396003)(376002)(136003)(39850400004)(346002)(451199021)(40470700004)(46966006)(36840700001)(36756003)(2906002)(44832011)(82310400005)(5660300002)(7416002)(7406005)(86362001)(40460700003)(83380400001)(40480700001)(6666004)(47076005)(426003)(1076003)(186003)(36860700001)(16526019)(336012)(26005)(356005)(478600001)(82740400003)(81166007)(54906003)(70206006)(6916009)(316002)(2616005)(41300700001)(4326008)(8936002)(70586007)(8676002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:53:32.9292 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e5dfa6b3-63fc-4e73-ad9b-08db6b00f95a 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: CY4PEPF0000E9DB.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4033 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 403D2140002 X-Stat-Signature: ejj7qi5ndwhcdxhc5sbx7xrxzyyjebcf X-HE-Tag: 1686545616-198583 X-HE-Meta: U2FsdGVkX18qAp7qNyIs9TKuf4imxQ2SI1rsGnzzWwBSp8586tlqDfRWyJMluOA++SO6u2tNKs/1SnKKc9t37aRsegyl9HLDBigTBGVgFcFUC0sd+zGKFK9wa/BIAQJHd32gxMvISvz1dbm2Ykm36jM+Ml4JobOlJl9kcJNCXAdWmP/GT68FzxbqgGMzPjWEQ1SRHrKslHuCVNiOprI4fcs6KVCScukncD1VZKW+uEUpMUBkWfM4+Z5X5FmKD8DO1ur9dgMH8mliEhuBwBjWMI1qqmEUYRdnXSHERWbvQAUWkYAvTt53LrVKr0bjkBhz49fpioEu9Eyrri9JGQOykRwO8mjGNbo4v0g4vcXnxTj5PIy5CPm2oLoLEr8LESCPsf/O/CEZJtYB1K0o/cbVS7y2BmWuAnPVv7IuQudmswc1KYa4CKlkWBe2vpO/k1uGCEzOiPd7xdtcb8yC70kymBdL3safpxshpdM/uEx7olbmOIEtP21/mZyd7mcfbPCdwBmxzHdJmjeGa29HNph/hQ0HaVx9AIw1Jq4vykSEFV8V/UX5nBidRAqbn4iVWWiRN7lPpSDR0tih3suwUsPJKr1pYAoxQBkkaFQvVJtl5dChnkTw1EWvpbL2mxePGtIddHpmvBfaA+6qYrEW79wqDu+7LqQwE1hqgWTxf2m2rzCDEAq2u98wVGWd49K7/USp1+Iiul/ezmWDicu7hikwYyUdWIR7hxbUZe+lHKcHevdlIl/AfxluRmAALQxS8khJR967iiwHqoqVh0CmLd1ydaLvVTDuX3msGk56+u4zGTUh0O9IgOE16NQSm2AqUc5avUvb5Ov41/4RUlS+7V/xAuqSmI/7mh8w3ATjVTK1vPNA+aexEItOTArBCBNkcKkhGfvmPzurjh13stCGSjPSAgxJjAeChojA8f/fZ+rnMR8IdU4FCbnAowlFMKaUQEwcLhYlAkLl7qIMLI3RHuW jLsHSnTb WSp6QQwrhAlH/KXPLvlD7I+yVCHz1p/G9TQk+Znt6PIGC0wlQPWjkCzw9VdC6KdsMvt1STO49e2+Stixt9yJPAN0GEYSZ249ioPdQzSrpr1PjOOdpFXrt7p/puJh4wqZMmcTiLd2608Nyr+O0HSd4zXSKap9jtW7tOwW6g6ohYD7KA5UsoClIazuTkNKid2EF98mnK1IURH3AnspZMTvyR29pz8D9to0jkXzRBpH5Zfr80AxcnjP5D+Lp9Dj8s+Vd0nfZGVqrB709SZb4F6jKPT/oLOcwUFe0cNy8E7+QPe17n1CkJ/lw9VcBfA== 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: Implement a platform hook to do the work of restoring the direct map entries of gmem-managed pages and transitioning the corresponding RMP table entries back to the default shared/hypervisor-owned state. Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 43 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 45 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index c5a1706387bf..543926fa3200 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -4117,3 +4117,46 @@ int sev_gmem_prepare(struct kvm *kvm, struct kvm_memory_slot *slot, return rc; } + +void sev_gmem_invalidate(struct kvm *kvm, kvm_pfn_t start, kvm_pfn_t end) +{ + kvm_pfn_t pfn; + + if (!sev_snp_guest(kvm)) + return; + + pr_debug("%s: kvm %p pfn 0x%llx pfn_end 0x%llx\n", + __func__, kvm, start, end); + + for (pfn = start; pfn < end; pfn++) { + int rc, rmp_level; + bool assigned; + + rc = snp_lookup_rmpentry(pfn, &assigned, &rmp_level); + if (rc) { + pr_warn_ratelimited("SEV: Failed to retrieve RMP entry for PFN 0x%llx error %d\n", + pfn, rc); + continue; + } + + if (!assigned) + continue; + + /* + * If PFN is currently assigned as a 2M page, PSMASH it into + * individual 4K RMP entries before attempting to convert a + * 4K sub-page. + */ + if (rmp_level > PG_LEVEL_4K) { + rc = snp_rmptable_psmash(kvm, pfn); + if (rc) + pr_warn_ratelimited("SEV: Failed to PSMASH RMP entry for PFN 0x%llx error %d\n", + pfn, rc); + } + + rc = rmp_make_shared(pfn, PG_LEVEL_4K); + if (rc) + pr_warn_ratelimited("SEV: Failed to update RMP entry for PFN 0x%llx error %d\n", + pfn, rc); + } +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 9085a122907c..1390e47d0aa5 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4936,6 +4936,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .alloc_apic_backing_page = svm_alloc_apic_backing_page, .gmem_prepare = sev_gmem_prepare, + .gmem_invalidate = sev_gmem_invalidate, }; /* diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 0438f52e4396..0d4c29a4300a 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -772,6 +772,7 @@ void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code); void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu); int sev_gmem_prepare(struct kvm *kvm, struct kvm_memory_slot *slot, kvm_pfn_t pfn, gfn_t gfn, u8 *max_level); +void sev_gmem_invalidate(struct kvm *kvm, kvm_pfn_t start, kvm_pfn_t end); /* vmenter.S */ From patchwork Mon Jun 12 04:25:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275563 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 7702EC7EE2E for ; Mon, 12 Jun 2023 04:54:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0F6746B0078; Mon, 12 Jun 2023 00:54:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0A7206B007B; Mon, 12 Jun 2023 00:54:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E62D48E0002; Mon, 12 Jun 2023 00:54:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id D73996B0078 for ; Mon, 12 Jun 2023 00:54:01 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id ABBDF160109 for ; Mon, 12 Jun 2023 04:54:01 +0000 (UTC) X-FDA: 80892878682.24.5E2A3A0 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2076.outbound.protection.outlook.com [40.107.237.76]) by imf09.hostedemail.com (Postfix) with ESMTP id AFE0A140008 for ; Mon, 12 Jun 2023 04:53:58 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=aWz0M+jj; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf09.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.76 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686545638; a=rsa-sha256; cv=pass; b=ENekQLnXzXzlLJreXUs8xxFee8z8LEksAzuCjqO/b7grfsaNsZB00gzghf5aoTmsJSsV0/ dc6pcdS8YlBsunC9A6XmtjFmpSTw/165LIYIfbfwK/56Ffl7wnNjo12qJu9RsHmv36SdVO Z/JjEy8Zn0M8bPeIAmarT/GRb8ouauY= ARC-Authentication-Results: i=2; imf09.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=aWz0M+jj; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf09.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.76 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686545638; 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=ARsbbAueoW5+HOX2d28yHASiDj/JlfTj9Ohag+P+WKw=; b=ofP3ZjSAevb7iP2uh4LCaRRDugPM8jHTnGvKFyL9lE1oquhKd65XF+ov16Cot+QYv6Os4w /A9mIBK61LO76ctfg6GwKYH/eT2ngcuVKOQdaKqeYIs/S2maA03qJn255t0NsV9npnc3Bc /zxwbZt0TQwMMj3McxqEKI1YR/JaGmI= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hbK1RVBoLakfJKebKIj2MALNHZzQw1p+CvLKpXQrvPIsWr9eU3Tq5vX+LOgz4uGVGcGkn+wTj9F5QXDRlsADltqd0W9Inak4rJdeJU36Laz4r5uUMs3flHCPYSvHz8PHyp4XjJ9obDPh4F5UL/c3O9sripDINsiCs7XWUNNsVL5PsDBgRgNtnSCR5tEIjccctKrz8lSImgrC+cDBGW2abF/bSTOuF0twR9MDCuawPR/BIxBpiZZ1MVTs6lvNVpkrP+o4kMRzJHJ8WbqK+22hYZSUkzlGny9YhOT9MRq41qZcb9709grEUpxJ2gWFOj1sXtwOG266MqzlJwcSuJpOJQ== 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=ARsbbAueoW5+HOX2d28yHASiDj/JlfTj9Ohag+P+WKw=; b=JH05SSQTd321eg5s4dht+w72gHVo0XidnWNqhdm+bk/JXUQl9l+L5Qa40Vf2hGOprYLinLEMbHrMHXCcJ1CyIlzH9SbJnCWnEJ/YDhxd+ZGjkm/Aw0MoxyBZCQ5Y3GUV7DBwAHrvqZR7IYoO9SKfa8wFXQ2/6UuROinLsMoMUmvtbSDehMP8HZQDak0tp+GcRfSIsA/UgJhfD8A/rlTDcfWQmtx9ofFyIfcv6ooMv8mVg+HKGLt0kbMc6v3WjmQP759clxKeZBDVS01YNhJHQu8Ebz9H0ckEixUSZ/ut4jzKRvBjCfGRj5UdLRaZGh5Pk4YdHq0U5+KUa1l3xkRxGA== 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 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=ARsbbAueoW5+HOX2d28yHASiDj/JlfTj9Ohag+P+WKw=; b=aWz0M+jjPfMVQWsNEAxpSWNSEEyP8OCwkLgtqkb2xCuDpkf/MONxbMolHaFhvmD55pEJJ3rry1se8iEDluYeA2/gUG1nyvAqFwJXhJrfNT0nZBlhPz6cSyTm+yBV+cQkfgxFbF8VH6jjX5YxmYNMTbAsXQU330CStc8BXTUMqr4= Received: from DS7PR03CA0288.namprd03.prod.outlook.com (2603:10b6:5:3ad::23) by DS0PR12MB6581.namprd12.prod.outlook.com (2603:10b6:8:d3::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.36; Mon, 12 Jun 2023 04:53:55 +0000 Received: from CY4PEPF0000E9D9.namprd05.prod.outlook.com (2603:10b6:5:3ad:cafe::6e) by DS7PR03CA0288.outlook.office365.com (2603:10b6:5:3ad::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.34 via Frontend Transport; Mon, 12 Jun 2023 04:53:54 +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 CY4PEPF0000E9D9.mail.protection.outlook.com (10.167.241.77) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:53:54 +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.23; Sun, 11 Jun 2023 23:53:54 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 46/51] KVM: SVM: Add module parameter to enable the SEV-SNP Date: Sun, 11 Jun 2023 23:25:54 -0500 Message-ID: <20230612042559.375660-47-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CY4PEPF0000E9D9:EE_|DS0PR12MB6581:EE_ X-MS-Office365-Filtering-Correlation-Id: 0169d4fa-b07c-4f8b-4d08-08db6b010661 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: TecPksSkmZkpyzqHwDKTafx7h6RP/MFFlXu9cOpeq2Mz2nCzdjJdIukZy3b8j1i8P50hPP+gjbq68Xa+Qecc8aX0/6FwclY+dzJ8es44Xig6ayNdyECOoY3i6QaQosgkUTvlon0U5m8taTtAMWdYWe71aObzQzQ3XaTCqL6EOQEvE6ODIVdbpxCFZ6uCKrnnEk5cG/F3LDA6FbeN9tgyJ5vHgR3va5PXHlIQdCwPOFvRaUm3AjarrXgcsiijz2ncVrWeJOJ3qtqNFufZA7mfDRvzK8ciAziXWJxvLQAMKfGJil1X5366hQRQZNYd5h9gqAUNMj10zk9h4wIcBHy9AgxkH2Y5Rj4oPmoMo6NaFV3VUGzol9cGqG33yA0wo50l6TihXb5jaECxoIR4RZyXEVw7AM45d/CB2Yjq709ipAtrST+G2xW3LqRjeH3O3xdTIoyMoIsdbgZEwcmpjQHKsBwZVvks+4Ix2IKrS0sap5zmocmyX+H+fEbqHQd/CP1yxIWxhh51gyPUeDP0P+MXkxgsFsrSrzcb1tLOCMn46thc3vyvcoTaLsSrnlO1PToy1WH29MYXHfS2+PzUvpEcaGcLL/jC0oVxu0auId4i3hdRaU8UO2QLiwaKtAc9WY3bDVSMSmX81Q4KIwMIgbyjdkFyC6WklESp4IfxxmQt1l2pK1oqNohwBDEhDXSGgNYupQnZ00lTacfnqoDzXy9csXwVWJQdimd6SUd3C8F20VbO7xP1DpqSla8WoNfICsuqnhM9JtBEaqt78sztvbzQtw== 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:(13230028)(4636009)(376002)(396003)(346002)(136003)(39860400002)(451199021)(36840700001)(40470700004)(46966006)(8936002)(8676002)(44832011)(5660300002)(7406005)(7416002)(4326008)(6916009)(70586007)(70206006)(316002)(54906003)(2906002)(41300700001)(36860700001)(40460700003)(478600001)(81166007)(40480700001)(356005)(26005)(1076003)(16526019)(36756003)(426003)(336012)(83380400001)(186003)(47076005)(2616005)(82740400003)(82310400005)(86362001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:53:54.7986 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0169d4fa-b07c-4f8b-4d08-08db6b010661 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: CY4PEPF0000E9D9.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB6581 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: AFE0A140008 X-Stat-Signature: bwhzugzkaiurf6u8c7tnyna319hmfybs X-HE-Tag: 1686545638-154850 X-HE-Meta: U2FsdGVkX18l55VGsaP6AAvf+ltU3Oj3qSyme+Ero9QyddLXuKxhSTnz9ealNb3wyOU+jVY+Ye7N7ln0D/H74if3vfqGv6RxpSDMihTp23xglgb4LDLDYQE6za50r60N1ceFtkwtrBalgkzdIuJL6B+N3g67c1anUn5s/Egm1Z1MYaEqmBJTSgvZqEaoGvftr/PqTTh+g+iMouSYomHTAzAay8JUCciADgbVOnyMDKqENWrYviJ+HUVAo0R4uBCVlyym5gqn0gab5eVbmoJOgjbg51Z3eTUp+JQbl8ovoLd82mqSvwwRNuTgiMPQQvqoGp8n9P4X98VXw/Xln3cjCy5Z0bdJUUbzPf5GrjLJ/xeahDhy7GSkLg8cIZfdX6nRMTfQsk/Z2L/g5wYia2KdySSC/2iN7Lmlgg4eM+wMupIB952t6N0CakU2E1C30XTJ0rjfPC8OuLQ9bAH54fT1zrGHPxtFo43OTUtAs77VFX5aJSgpTzAhsp2sxGN15/9wlU4dclJ2kN/lGJ5OAK5kWUcvzss+MFEkdgdYsuYn3wk/9Lr4HUMfD3KUQAgQyLS5kNvKrwJ4LwGzQHB7hU9x4XoO/iMZ4Zpy5YSAgpfHTHO6Z4GWXNkP4sDaqM4lxzoiMBvqAtAixd8kY3SH1+yamXvfFaEONtYBO0yqlXT9yE+0ESvJsEd+S5HJZQDwqySCmOlDUq+Onv57oh/q/+Kfh8EM/CM+RtRR+6n1sFBMCisbmIh8HkqA4UGooysvsCeVsvr0kk4dvkSKTimtEltczI3crdH/G8TBoO1g8+OgicFTaoxZzjJe26AZaMefPVd6MC6+pqnauo8fZjiDUW6VbP8N0NKBsooCB7wvt1hnNV+oPJRF+toTbm3wKZ/KNV3gwn0iuoxsk1y8uoDPjEuW/oyrT8rYcYN073ONBzws0LS8i8rzPj259PS9Wd95ztQDCtmCRnhnEodozpfZuee UalCtWl1 v8D7iWZRsB1G+5smr77UUy3D3Uv1WpqlHv0hdqSkoKSgyRRUmgSIOK6f5iIsSZVWSpaBbGqY2yhTP4WxiMsv19mrGv7p7jnGzelvhSu1qc4PNxANbaGpQZGOB3kEIALYPBPbMuO2PAw+n7dxaNF8/m0kirLTS1SiA1BGhm/p4fmQnkmGvjudL8JKr2wvR4hnAWHomFjT+KMgqHMWomKaZCL8+R8KK5WCt05RYFRB47j3F5G8vneuy5dSyL4+vlZW13o4XFob4kNGzTDdUlqqYJghln1vJVVVL6jqTrqekwswxrQ2pVOIhGoBrofsReBozv0ZvVtFRPRKIu9jf0kvGnjNQnw== 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: From: Brijesh Singh Add a module parameter than can be used to enable or disable the SEV-SNP feature. Now that KVM contains the support for the SNP set the GHCB hypervisor feature flag to indicate that SNP is supported. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 543926fa3200..adbe8c242d81 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -54,14 +54,16 @@ module_param_named(sev, sev_enabled, bool, 0444); /* enable/disable SEV-ES support */ static bool sev_es_enabled = true; module_param_named(sev_es, sev_es_enabled, bool, 0444); + +/* enable/disable SEV-SNP support */ +static bool sev_snp_enabled = true; +module_param_named(sev_snp, sev_snp_enabled, bool, 0444); #else #define sev_enabled false #define sev_es_enabled false +#define sev_snp_enabled false #endif /* CONFIG_KVM_AMD_SEV */ -/* enable/disable SEV-SNP support */ -static bool sev_snp_enabled; - #define AP_RESET_HOLD_NONE 0 #define AP_RESET_HOLD_NAE_EVENT 1 #define AP_RESET_HOLD_MSR_PROTO 2 From patchwork Mon Jun 12 04:25:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275564 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 D43C8C7EE2E for ; Mon, 12 Jun 2023 04:54:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 786146B0072; Mon, 12 Jun 2023 00:54:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 736A46B0074; Mon, 12 Jun 2023 00:54:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5D7488E0002; Mon, 12 Jun 2023 00:54:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 4D37F6B0072 for ; Mon, 12 Jun 2023 00:54:24 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 26C7EAE516 for ; Mon, 12 Jun 2023 04:54:24 +0000 (UTC) X-FDA: 80892879648.03.CDD6C9E Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2050.outbound.protection.outlook.com [40.107.92.50]) by imf26.hostedemail.com (Postfix) with ESMTP id 30A1F140002 for ; Mon, 12 Jun 2023 04:54:19 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=CyTDyF8d; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.50 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686545660; 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=W7mzzYO76w209ixfz6AUBtX4bKrAJTU7/Cj1iL+ERcw=; b=Xqn2mQG6OLG4Q+o0JPtBfxuj+OypgqpWzKVBWFxEuaUeNihFPs5GR5SXUeoJZ6jSloEs/R 4HAUZPjXv6P6RHLStm6iWv8GArIrZ7TsFhB2+UBgi3EkY8zBJtXWn673OH3QoCv/3C0ZIq cn8h2IBLM+YIAYducn4um5MzOnHHeD4= ARC-Authentication-Results: i=2; imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=CyTDyF8d; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.50 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686545660; a=rsa-sha256; cv=pass; b=WhrH7AJu66PKwq1DP3DmwlNYc6WXOFDLm9ZNrLDf6Xd1Q94WeWDkiji3aWof54vL925iT8 gTDwWwHZ8k/CCjBrU6lS+2Xluj48970XHNMtB1ec1cUXVLt4Jnb64iU9UftdqzNfe4IRRG ItDwYg/ow35gxv0woSFovjkpnkYyyjA= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Lcw/0SkLEgUMhS2rbtls+xYPYwN7YJjzp+1X3+EmQg/ZEmAoOxHF0oOpyBSkYi62h0M/2yjVl7wuz2E4hZ5WQ8KZZFxV0ic98chYIbEBwGFJCi1ns+GEArqSxNIVi6cWmDM4psj7ch46Dl65hqUcGZFXZgNJ99R26B6CmNTTP/7P9UrPl8e3cF5EHx4/wWeTnpPQlFT4O6woOjzgnw06TZnl968geHyu8BkP+oW1alrG3NYDR1Cvy+f7CZSmqjMDoST17uOYUGZQR/XxR/a1Bpy2xzfe11LI2DcrPFGPmZZlk/Y1q7+OSoVt+Ff7x0pjHo7Gqob+nVPOPC1PHxHpXQ== 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=W7mzzYO76w209ixfz6AUBtX4bKrAJTU7/Cj1iL+ERcw=; b=Qd0v7n0P7UKHHtbZYiWrCvgx2i2P1Ec1zJ7hpzNQyV0/uahqK4nn0aX+gTq+CPCeQO4LrdvxAaWzKekFdzeUobRe8klT3Fe+K8SDGWOXRcYqR1KJt0Pm0jDMOvJCBF8AfaPcwokEx1uLVmgQ1n0ERTacgLI/qLBrU8Q40XgsDthLfEfNmlPy48Gcj7n+JRhV0iKgcPhHPO/pvjtZG1zIjqpZZMkey7TjEuoEqb8PmaQYEfc30R2PuMbkCgib/8brVlzZz2D3GZZMdsX+cApDs7FA02pcv7bd37RQlm15FF8dcdwDD47pTAiMEu6Q8iQd6tJeKjBiKWjG1EV0OJsTog== 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 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=W7mzzYO76w209ixfz6AUBtX4bKrAJTU7/Cj1iL+ERcw=; b=CyTDyF8dQbEeJP9GphGEJz8lMj+iqLesa4TWDm/RAr7qBfzq7tqJ4P3lOA1D0fUtEZI2H5lVgw8WLam47WT1ynS0uIJOg/ML2RRNe1ahC2kMikER9mDlapWarpAn0DVzL13O9I/8Gn1kTtSkxg7iF8O/I6d03zmq6nBzrHE3Atw= Received: from CYXPR03CA0084.namprd03.prod.outlook.com (2603:10b6:930:d3::8) by CH0PR12MB5330.namprd12.prod.outlook.com (2603:10b6:610:d5::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.29; Mon, 12 Jun 2023 04:54:17 +0000 Received: from CY4PEPF0000E9D6.namprd05.prod.outlook.com (2603:10b6:930:d3:cafe::dd) by CYXPR03CA0084.outlook.office365.com (2603:10b6:930:d3::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:54:17 +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 CY4PEPF0000E9D6.mail.protection.outlook.com (10.167.241.80) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:54:16 +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.23; Sun, 11 Jun 2023 23:54:16 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 47/51] iommu/amd: Add IOMMU_SNP_SHUTDOWN support Date: Sun, 11 Jun 2023 23:25:55 -0500 Message-ID: <20230612042559.375660-48-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CY4PEPF0000E9D6:EE_|CH0PR12MB5330:EE_ X-MS-Office365-Filtering-Correlation-Id: 24cc878b-6a36-43ee-53a1-08db6b01139a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: zId6Rx0KWJvdknOLotHWFbZMQccpKFohyIcqZHwOp2tL/AbmvAUWk0CyHdU7xkOi51lfzojxnE9gYdE30fWM5+RAxoAN/h6wY13rUFnFLg/mFQxQK/iG2mUFlRJvCMMIlSDbiszRVkkwGNoIzIO+VXWz9EqHqV0/pKn4aeycwsFbqI0I5uvZZ7dQP1KJhIzWP7ov0rDcFM5auMvQncdtJE6438WsMnWWKGrGNdkBfQywz8QrsjsdlTG/DRONluliUVIqMegWS823JeZ3F5Y8vEE840Blz47ExW22U5Xbwh/OMpnjqTDGvc4xahE9YT0ch9XQBukLvznBRLa1J2NxZZVIk1rDUL4kbcannC695gfBn/zWFzjO5hnXdBdZyO0qFspzmTTiBtEi6EMnZli+qHwNYtYJvD6E8CwiBlWa0Jl+1FZk2LRYph26qvI0T1jbrHQ/zc9LWSMdO5Qx+tLfpcHeUsZHu/CtpIRJvh8ASeQ6uq6vALjjdeMDLSPwdy/3pkhzgzOMKBQYGaQgpDlZKhU1EvZrwr3HLxo914fD1fKbtnFQwPFZIDPLXPtGNvGDm4bWdpnm0LL6UdlfFqSWwCnUYtLNlUOlz6G+yx1FUzAceVlqv7VRsuiSbwNlSLtGL5pnnda843qTR7XKLI4hk+4hQdEyQ/7hTxLRK7dg9heNkAuN7yftmOkK7E0mLXOMjZog16omGZ8eLraFikcNvLYv/5veo47d6zM6+XkoxcFaeMDVWWN1a5aU+8l9SXj4TpNvDB/SSdgM4yme8JbrZw== 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:(13230028)(4636009)(376002)(346002)(39860400002)(136003)(396003)(451199021)(46966006)(36840700001)(40470700004)(40460700003)(6666004)(47076005)(36756003)(426003)(336012)(83380400001)(2616005)(86362001)(82310400005)(82740400003)(81166007)(1076003)(26005)(356005)(40480700001)(36860700001)(186003)(16526019)(2906002)(54906003)(44832011)(4326008)(6916009)(316002)(7406005)(7416002)(70206006)(70586007)(41300700001)(5660300002)(478600001)(8936002)(8676002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:54:16.9827 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 24cc878b-6a36-43ee-53a1-08db6b01139a 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: CY4PEPF0000E9D6.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5330 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 30A1F140002 X-Stat-Signature: 89pih1hjaf1y978b8xnmowuqniu5ejzy X-Rspam-User: X-HE-Tag: 1686545659-613485 X-HE-Meta: U2FsdGVkX18swncu+l6Eoi5Tl/STFtlpYg/OR896mDxPyM7YFkrICoHANGazZfK8Tc17UBf/+cE35OItRNchKJPF7CJOwczbvpESAtule+M3Yee5u22JQehmbbRorpqaH1pmwQKY4MH83f/4WNIyBsGnQdCxvTU4dnj58t1vi+Ig42ycuNSrt+Toiz/FpdGamzgfzVZMzc7BVqqzQSGEAv7FC5UM106QV81RhuMTYijDNzj6Vj3x4J+76+90jx3l7eB/3unP4k1pMTh2VX3fHmVBFeprnStAUtUBIx+UzV6Woe8OBMWeqdfh0mBoOa6co497EJ9SVuYHjqSZsnyji6HeDZpY3n7GrcnnOR+KS8M4Snp4SPypZdYQV96wnfZTQsIsQ1DuerAhGU4wi68C6OuneCUc6UQf9wdL3QjMl2TEGveK6igt9CVQdZI3VpePx3j9imawLMBnGWpzp+36/51GAHLNESGnAK6nwybWIhgVvtor1Em4Hegn+TYc4ZijuXkabEiEFYgQkuXUx4LuCGmqD4toyF+Ucew4ZjFPEDZ3U9EydSgnnKmPQqWGaELj6Ktg6WXKZk8cm5lTxvQUGPnAfCb5raApYMpfbbUNo65q5KEZtK/bx2frxJTaHdEmHGGthw98wQJ7jrshQKg0Km1P5N7ffcIoSnyYtgkS1ujKB2nCNgTQlhGcY3hWZWtN3DMpmK076VypsmHoL0UV/xWmyjPBmojfrKpg3QdVzXTc5xFes5BGaCEnQABT176K06vQr7rl2nRxBiy9BxTtzTCDeB+yF1m08+6oMatd01ewrnX4cAnlZtLjJ42z4EDYXsNdQBAl35/IX63j1L/hJPNcNVLenSB6RB7jinoKztgEq9kcgGw0jb1y0+OcqIc8msAIDnDWhyl5CZzPwefjAGqZFegUNDdw9wMFR39YdctKfOm9jMruLNVBeI8kP7N7YpUuvoxqcJOOGLG+Fjq BNUW2bjt 5KmlspM4tSaIIPQAts2EImYnIwNDtB41pvcG3mhDTuvZo1Us1mCPuVFZESUqxP88hles0yEZuK3GTWz92SR/HhlUQrn0tDAgwXiYVYmwPtiW5BYzRuWLOe6DfAiR0LnEBTUvya+mAvShQyL6Q1EjyTyTgM9zIMbo2nr7iSg3nQXpne88Yn2DgSKiOEozfB1123d4/SPuQ6tSvTdnPTCQbeVKyE8TQ51G2h0OgJCEbCHrv6KlSRxawI3lshsB0GzKrYEiC3/DpPSzJOnx1ngMCoHJJmrs6LI1dTWVA/lAhv2ofGCsLrnEZPgMZz/zKtxfKlP7yLfrsB9z+9Wcb2IEep3D1ySRPWBS/9pd3RjyBZ1EWOhk= 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: From: Ashish Kalra Add a new IOMMU API interface amd_iommu_snp_disable() to transition IOMMU pages to Hypervisor state from Reclaim state after SNP_SHUTDOWN_EX command. Invoke this API from the CCP driver after SNP_SHUTDOWN_EX command. Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- drivers/crypto/ccp/sev-dev.c | 20 +++++++++++++ drivers/iommu/amd/init.c | 55 ++++++++++++++++++++++++++++++++++++ include/linux/amd-iommu.h | 1 + 3 files changed, 76 insertions(+) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 0bfe9721c977..b8e8c4da4025 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -1508,6 +1509,25 @@ static int __sev_snp_shutdown_locked(int *error) return ret; } + /* + * SNP_SHUTDOWN_EX with IOMMU_SNP_SHUTDOWN set to 1 disables SNP + * enforcement by the IOMMU and also transitions all pages + * associated with the IOMMU to the Reclaim state. + * Firmware was transitioning the IOMMU pages to Hypervisor state + * before version 1.53. But, accounting for the number of assigned + * 4kB pages in a 2M page was done incorrectly by not transitioning + * to the Reclaim state. This resulted in RMP #PF when later accessing + * the 2M page containing those pages during kexec boot. Hence, the + * firmware now transitions these pages to Reclaim state and hypervisor + * needs to transition these pages to shared state. SNP Firmware + * version 1.53 and above are needed for kexec boot. + */ + ret = amd_iommu_snp_disable(); + if (ret) { + dev_err(sev->dev, "SNP IOMMU shutdown failed\n"); + return ret; + } + sev->snp_initialized = false; dev_dbg(sev->dev, "SEV-SNP firmware shutdown\n"); diff --git a/drivers/iommu/amd/init.c b/drivers/iommu/amd/init.c index 33ea62d93540..a84ec81cfbb5 100644 --- a/drivers/iommu/amd/init.c +++ b/drivers/iommu/amd/init.c @@ -30,6 +30,7 @@ #include #include #include +#include #include @@ -3701,4 +3702,58 @@ int amd_iommu_snp_enable(void) return 0; } + +static int iommu_page_make_shared(void *page) +{ + unsigned long paddr, pfn; + + paddr = iommu_virt_to_phys(page); + /* Cbit maybe set in the paddr */ + pfn = __sme_clr(paddr) >> PAGE_SHIFT; + return rmp_make_shared(pfn, PG_LEVEL_4K); +} + +static int iommu_make_shared(void *va, size_t size) +{ + void *page; + int ret; + + if (!va) + return 0; + + for (page = va; page < (va + size); page += PAGE_SIZE) { + ret = iommu_page_make_shared(page); + if (ret) + return ret; + } + + return 0; +} + +int amd_iommu_snp_disable(void) +{ + struct amd_iommu *iommu; + int ret; + + if (!amd_iommu_snp_en) + return 0; + + for_each_iommu(iommu) { + ret = iommu_make_shared(iommu->evt_buf, EVT_BUFFER_SIZE); + if (ret) + return ret; + + ret = iommu_make_shared(iommu->ppr_log, PPR_LOG_SIZE); + if (ret) + return ret; + + ret = iommu_make_shared((void *)iommu->cmd_sem, PAGE_SIZE); + if (ret) + return ret; + } + + amd_iommu_snp_en = false; + return 0; +} +EXPORT_SYMBOL_GPL(amd_iommu_snp_disable); #endif diff --git a/include/linux/amd-iommu.h b/include/linux/amd-iommu.h index 8f0cde2d451c..7ba46118d0f1 100644 --- a/include/linux/amd-iommu.h +++ b/include/linux/amd-iommu.h @@ -208,6 +208,7 @@ struct amd_iommu *get_amd_iommu(unsigned int idx); #ifdef CONFIG_KVM_AMD_SEV int amd_iommu_snp_enable(void); +int amd_iommu_snp_disable(void); #endif #endif /* _ASM_X86_AMD_IOMMU_H */ From patchwork Mon Jun 12 04:25:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275565 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 5AC0FC7EE2E for ; Mon, 12 Jun 2023 04:55:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EFFAA6B0072; Mon, 12 Jun 2023 00:55:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EAF038E0002; Mon, 12 Jun 2023 00:55:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D28E46B0075; Mon, 12 Jun 2023 00:55:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id C41B66B0072 for ; Mon, 12 Jun 2023 00:55:13 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id A335D16010C for ; Mon, 12 Jun 2023 04:55:13 +0000 (UTC) X-FDA: 80892881706.25.16D7E96 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2086.outbound.protection.outlook.com [40.107.101.86]) by imf27.hostedemail.com (Postfix) with ESMTP id 8A8F94000A for ; Mon, 12 Jun 2023 04:55:09 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Fljl715e; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf27.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.101.86 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686545709; 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=Em/p9x32Hb3bLzmcXjcCKuGKc6huNvCGbDR3djc9uMo=; b=gTn5lfTn8c9nrCWmNojy17zQMt4KVkG8ibcWT1rbIph7Fz9rsf7yi4Ln8aoQMqFV2Bxiic NwWacwl2Q39MUQAUdo4vVamZNfdTfvMO1UVK0SaLWOtsekEr+pKy7zn1QeL5i4SxfHO3n0 B4/30v2b+e8ylBk8o9b8CoGHJnNHNaM= ARC-Authentication-Results: i=2; imf27.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Fljl715e; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf27.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.101.86 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686545709; a=rsa-sha256; cv=pass; b=CTzLm8UE+F+Uz0FIVplkfh7uGjaqIKqyZaU7VnZ0+87zfhZW1ukQUCvgjXoTpung7z7Y+n 9kikhuUeXmwIjX6F63dUDcXCG4VTxFJ5OSzokrc6SjBZ4sQaKIWEX8AAeSCyP+4yOVrJQW MhtKRVYa78L39h0WFY4Q4dqGhDuFVB8= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gihk28j4nlJGaBEvpAa1Hj3qp6RG9Yd6XHd2k5l+xJPcsMwLNOiocflDSP3twRmZLVakf9J+rQuvNBsul/BftsbVmStAe+Q9G5yF9bfeLmoRF/0F3hObTKwkpFYTqauTh1bQJQwGD/faZYuwLKH7TMnEf2DvR+IJo+ACZkzN4mZTHVuBXzzDosLFNCtHdv2eHgywBi+EAoXtTU7r5Hdk037K7mfmVFiUW/rwvaGXpqSF1mM9rGVpxR1ICl4elXRV2DULB/Y669blLgqQ9699Aa+Iw9fIrd0GVQZ0SvA/UAQYIbj6aGgTuBXMmbXuiuHvaD05kDljZBYCHCZzIwn9IA== 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=Em/p9x32Hb3bLzmcXjcCKuGKc6huNvCGbDR3djc9uMo=; b=kriR/W6+TjqLimipN0d+jkYB5m/bvTZD76GdkRxKzUVX73qLGHkamrNonPxqMYsG4TRrCVAwW/icvEcl9NmzVG7GNzXon4sDw9ChyM9tRXTNxrcloJopxBTOXkFNtt3snmVdkhi0KyKCkq4GdM3HiZwgL3KXzxdaQSZ1sKTjD0YtsjJF/qC4iDZAg07HQk41ADnRrFjA+3jKeUosYoEvN36+AeemWqy9+RUeGu4h5DcnnpzPwnQGQXGHaoIiEIHUrd8LwnGGOGuLTOXU8nY/6dxtZSJUNfMbnBBcCCIrwamdPzxud7bVyixe4TMsfJXvbJ9a3r4c+Go8AdPjaTHyBA== 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 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=Em/p9x32Hb3bLzmcXjcCKuGKc6huNvCGbDR3djc9uMo=; b=Fljl715eY0mSF1DjWxiyfEmAbYjMZ46WVo4c4w0YbYEL0pwvBV4wksgqqhZtKP10i3646cyDIWYUmx5xXN9ywBtynQLLKflauh9I/4jjI54YI1xKmBFTYULi+5h4scPZwWXod4fCFi0mRuI9paMhXf+7fAMRAxaEuKiEDA/ihQw= Received: from CYXPR03CA0095.namprd03.prod.outlook.com (2603:10b6:930:d3::6) by DS0PR12MB7557.namprd12.prod.outlook.com (2603:10b6:8:130::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.39; Mon, 12 Jun 2023 04:55:05 +0000 Received: from CY4PEPF0000E9D6.namprd05.prod.outlook.com (2603:10b6:930:d3:cafe::3b) by CYXPR03CA0095.outlook.office365.com (2603:10b6:930:d3::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.34 via Frontend Transport; Mon, 12 Jun 2023 04:55:05 +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 CY4PEPF0000E9D6.mail.protection.outlook.com (10.167.241.80) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:55:04 +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.23; Sun, 11 Jun 2023 23:54:58 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Alexey Kardashevskiy , Dionna Glaze Subject: [PATCH RFC v9 48/51] crypto: ccp: Add the SNP_{SET,GET}_EXT_CONFIG command Date: Sun, 11 Jun 2023 23:25:56 -0500 Message-ID: <20230612042559.375660-49-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CY4PEPF0000E9D6:EE_|DS0PR12MB7557:EE_ X-MS-Office365-Filtering-Correlation-Id: 1439d436-96b9-4ba9-0e0c-08db6b01302d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: IbMju/P4XXqkxJKDm5mlOOpvJe/nMzOQPC/YYKab8FAUWZ8ZdJOnHi6iqBQFESk0Cvlj0UZ+nPThdMXdfGoJNRwvUxPZMLypALZViFjiiF8WN46UKpC5aCJd8xV3/qZAEm+sy8RaAsoSCZM2vrJWV0V0evmame1KCzOb8VlXvkutOl8AeGyHJRJdrJrKb6c+5lnJs7mMXiyoaZ9+b0mjjOpd/8fIu7q/8aXbVMvbcE2hCeA0yxdSHu11LY5XmMxMuJkC3GM4lPsIdKean4PT0zu31IqhOrJpSTbLIgwtKrqKPz5SEALHdMF/gVBEkYngSRzaZ1xYTVKAMM9o5jBSsogNYflKEcmH1x5MCKKW1b93CF42NTiyMnw8j5mJYSS1gwvprTx9bBYBmzNouWS6b0hrRL8upFwjIWTriza1zBaWBSt5cfjJvjPgxFgeVq4s1stOfjvhYDU8sdBbAsqUYRH+hZEHmblaFwnhD8P+P6aoUfph6XdsyI39nhn4Xt3XVtsNiniRRLz0n4RPhh109KLGvHvHM61lpdeMPx/c/kZI5P3t+4qx6jcKTZEMw3wmzhQHwrH/+Zh/chm/lAkRv3Smk10vyKrj//P5KXynl6MzlGvFYR0VRB67VOTMUebY3vemDLyy1GcUumKTsAMTWBGoP4HF0XWAsOnMzkC2lkhF1HtrV4DHNiiUsPFXV38ysHVdNl6lvds2tZS6y6ofawGbELHHvP9o6QBCOUGtG8oU5CjVjKK+1OH95FM5ynuahbrLmhNDv7fC1DqgKWlzkA== 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:(13230028)(4636009)(376002)(396003)(346002)(39860400002)(136003)(451199021)(40470700004)(36840700001)(46966006)(40480700001)(40460700003)(81166007)(54906003)(82740400003)(478600001)(356005)(8676002)(70586007)(2616005)(5660300002)(316002)(4326008)(6916009)(8936002)(41300700001)(70206006)(336012)(426003)(16526019)(36860700001)(186003)(47076005)(83380400001)(26005)(1076003)(82310400005)(86362001)(7416002)(44832011)(36756003)(7406005)(2906002)(30864003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:55:04.9205 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1439d436-96b9-4ba9-0e0c-08db6b01302d 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: CY4PEPF0000E9D6.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB7557 X-Rspamd-Queue-Id: 8A8F94000A X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: qpdfffc4i7tnp1bskkr15mewe55stfsa X-HE-Tag: 1686545709-762902 X-HE-Meta: U2FsdGVkX1/mG65zx9/RqklFny2EXJZJCZfaLbtElpasROP3YsBDDQzpDFfv65XLbPb3e+D5uj5M2Wf8SdnT+RYnvBP1FyYj9qYKSf1GQklwWlHFB80CmykROfr7GXJ7HuXwk3rwQjyTRHmaQsIgppnKbUyuHolGkiYqvLJdVgx7IEb/NMOXJiOzeCQGTjI3p4xW1yM4dAy4y+ga/crbMRsGY22YRI+guBnOxofY4nuA7m4gFOVkDkrHU0fK21AUutAIca1IKyuaFh/UlBjMaSJZEONF3SR9lQRKZbNkR1c7yFZaCbH4wl8CQi0Gse0kapk+XpSXCBorzJpWWAnrzjYeC6ENNv01zzy94f0NJNGVV9Lu8sA2J1dn0R7HnPQ7GMaz3xD52i+KHn2q30jz+QAYpEHa3uThiDXxRLD7cHepOxTaAKGVikzaVCsm8IzzwWl9lhPzcm5UBtsoFPcxkfi9Uc4vzusEM9981pDJIaNHdvgalBhK4qE2ewlPa3XJ8X3cEMQpzb/5A+dHxr3l1apM0BfZWSp4XZrOP68lkNx+IsIS0baPPN92NkUN89uX9JSXTOxRC6nCbP5aDNdpe9Lxgsf2D+X5AlCU8NL4hi2xLVecitojJEPK68xFu+9mDQE1oSh+AaqvID3CFgaH5oLXdijPZDWpaEWpxDT9ypfsBKiO7ELwyJapbyCr+dWXSZZZfsSnqsB8ALLfyLr9kYS8zM7sxc83UET+ofrhn3QpYRj0LhSUIDUMhgYzH8H9m3PF+fS8Kpc0Fgrn/4ZhjXJE5GZ+GQXe1uSPJgSMmS5FWE6vQ9eWEJCsTKIqgu76h3nAhUFT3F0NJuHbWNvchA5AyUxEoGcwxqB610GiUN7+qKtgvIl6HUmvWHkjt3yzuLj6C7ufv/nrLFIZmh5B8l+7oLEPIFDiarD++MBzIjQTLzhrZN6ZTEpbDDfSTltClwYAmIOcDXUyDA+ZN7Q FpSir0Y8 HDmHFewsq63GVrBfqLXiVgS37KOlr+yG3bWSLFw+EEyJ37PceG9lzRB/yT1j8PRl07LgrDVRtMNH+8cHxTBirACjCll95T5H+Icoazibcdezb/YrC+KR5CRtOup4xXYMgKNQOWm5vl/Pky/7z7KMuhriFI2TEqcya9UC5gk5iazs5cF4Grb3C2HUNJKihnUnLoVsYm3jWgWzcVi1NZUmb+4QfhV0mPt3zhITuDHHdeYp/CK4hUgZuWInSrwouDPhGpDZ0z1gmedJr8a2tI0LKt2ik3NFrK+ga8WSP/4QDrmkc3L4+0eAMp2PVaEGKq2ccxh3zl62UGCUgodUV5KZ0NmMrkZICQaXPJOq9OUn1XUkSVuXkhAjT/hwQEiskemIZqcFw 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: From: Brijesh Singh The SEV-SNP firmware provides the SNP_CONFIG command used to set the system-wide configuration value for SNP guests. The information includes the TCB version string to be reported in guest attestation reports. Version 2 of the GHCB specification adds an NAE (SNP extended guest request) that a guest can use to query the reports that include additional certificates. In both cases, userspace provided additional data is included in the attestation reports. The userspace will use the SNP_SET_EXT_CONFIG command to give the certificate blob and the reported TCB version string at once. Note that the specification defines certificate blob with a specific GUID format; the userspace is responsible for building the proper certificate blob. The ioctl treats it an opaque blob. While it is not defined in the spec, but let's add SNP_GET_EXT_CONFIG command that can be used to obtain the data programmed through the SNP_SET_EXT_CONFIG. Co-developed-by: Alexey Kardashevskiy Signed-off-by: Alexey Kardashevskiy Co-developed-by: Dionna Glaze Signed-off-by: Dionna Glaze Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra [mdr: squash in doc patch from Dionna] Signed-off-by: Michael Roth --- Documentation/virt/coco/sev-guest.rst | 27 ++++ drivers/crypto/ccp/sev-dev.c | 178 ++++++++++++++++++++++++++ drivers/crypto/ccp/sev-dev.h | 2 + include/linux/psp-sev.h | 10 ++ include/uapi/linux/psp-sev.h | 17 +++ 5 files changed, 234 insertions(+) diff --git a/Documentation/virt/coco/sev-guest.rst b/Documentation/virt/coco/sev-guest.rst index 11ea67c944df..6cad4226c348 100644 --- a/Documentation/virt/coco/sev-guest.rst +++ b/Documentation/virt/coco/sev-guest.rst @@ -145,6 +145,33 @@ The SNP_PLATFORM_STATUS command is used to query the SNP platform status. The status includes API major, minor version and more. See the SEV-SNP specification for further details. +2.5 SNP_SET_EXT_CONFIG +---------------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (in): struct sev_data_snp_ext_config +:Returns (out): 0 on success, -negative on error + +The SNP_SET_EXT_CONFIG is used to set the system-wide configuration such as +reported TCB version in the attestation report. The command is similar to +SNP_CONFIG command defined in the SEV-SNP spec. The main difference is the +command also accepts an additional certificate blob defined in the GHCB +specification. + +If the certs_address is zero, then the previous certificate blob will deleted. +For more information on the certificate blob layout, see the GHCB spec +(extended guest request message). + +2.6 SNP_GET_EXT_CONFIG +---------------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (in): struct sev_data_snp_ext_config +:Returns (out): 0 on success, -negative on error + +The SNP_GET_EXT_CONFIG is used to query the system-wide configuration set +through the SNP_SET_EXT_CONFIG. + 3. SEV-SNP CPUID Enforcement ============================ diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index b8e8c4da4025..175c24163ba0 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -1491,6 +1491,10 @@ static int __sev_snp_shutdown_locked(int *error) data.length = sizeof(data); data.iommu_snp_shutdown = 1; + /* Free the memory used for caching the certificate data */ + sev_snp_certs_put(sev->snp_certs); + sev->snp_certs = NULL; + wbinvd_on_all_cpus(); retry: @@ -1829,6 +1833,126 @@ static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp) return ret; } +static int sev_ioctl_snp_get_config(struct sev_issue_cmd *argp) +{ + struct sev_device *sev = psp_master->sev_data; + struct sev_user_data_ext_snp_config input; + struct sev_snp_certs *snp_certs; + int ret; + + if (!sev->snp_initialized || !argp->data) + return -EINVAL; + + memset(&input, 0, sizeof(input)); + + if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) + return -EFAULT; + + /* Copy the TCB version programmed through the SET_CONFIG to userspace */ + if (input.config_address) { + if (copy_to_user((void * __user)input.config_address, + &sev->snp_config, sizeof(struct sev_user_data_snp_config))) + return -EFAULT; + } + + snp_certs = sev_snp_certs_get(sev->snp_certs); + + /* Copy the extended certs programmed through the SNP_SET_CONFIG */ + if (input.certs_address && snp_certs) { + if (input.certs_len < snp_certs->len) { + /* Return the certs length to userspace */ + input.certs_len = snp_certs->len; + + ret = -EIO; + goto e_done; + } + + if (copy_to_user((void * __user)input.certs_address, + snp_certs->data, snp_certs->len)) { + ret = -EFAULT; + goto put_exit; + } + } + + ret = 0; + +e_done: + if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) + ret = -EFAULT; + +put_exit: + sev_snp_certs_put(snp_certs); + + return ret; +} + +static int sev_ioctl_snp_set_config(struct sev_issue_cmd *argp, bool writable) +{ + struct sev_device *sev = psp_master->sev_data; + struct sev_user_data_ext_snp_config input; + struct sev_user_data_snp_config config; + struct sev_snp_certs *snp_certs = NULL; + void *certs = NULL; + int ret = 0; + + if (!sev->snp_initialized || !argp->data) + return -EINVAL; + + if (!writable) + return -EPERM; + + memset(&input, 0, sizeof(input)); + + if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) + return -EFAULT; + + /* Copy the certs from userspace */ + if (input.certs_address) { + if (!input.certs_len || !IS_ALIGNED(input.certs_len, PAGE_SIZE)) + return -EINVAL; + + certs = psp_copy_user_blob(input.certs_address, input.certs_len); + if (IS_ERR(certs)) + return PTR_ERR(certs); + } + + /* Issue the PSP command to update the TCB version using the SNP_CONFIG. */ + if (input.config_address) { + memset(&config, 0, sizeof(config)); + if (copy_from_user(&config, + (void __user *)input.config_address, sizeof(config))) { + ret = -EFAULT; + goto e_free; + } + + ret = __sev_do_cmd_locked(SEV_CMD_SNP_CONFIG, &config, &argp->error); + if (ret) + goto e_free; + + memcpy(&sev->snp_config, &config, sizeof(config)); + } + + /* + * If the new certs are passed then cache it else free the old certs. + */ + if (input.certs_len) { + snp_certs = sev_snp_certs_new(certs, input.certs_len); + if (!snp_certs) { + ret = -ENOMEM; + goto e_free; + } + } + + sev_snp_certs_put(sev->snp_certs); + sev->snp_certs = snp_certs; + + return 0; + +e_free: + kfree(certs); + return ret; +} + static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) { void __user *argp = (void __user *)arg; @@ -1883,6 +2007,12 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) case SNP_PLATFORM_STATUS: ret = sev_ioctl_snp_platform_status(&input); break; + case SNP_SET_EXT_CONFIG: + ret = sev_ioctl_snp_set_config(&input, writable); + break; + case SNP_GET_EXT_CONFIG: + ret = sev_ioctl_snp_get_config(&input); + break; default: ret = -EINVAL; goto out; @@ -1931,6 +2061,54 @@ int sev_guest_df_flush(int *error) } EXPORT_SYMBOL_GPL(sev_guest_df_flush); +static void sev_snp_certs_release(struct kref *kref) +{ + struct sev_snp_certs *certs = container_of(kref, struct sev_snp_certs, kref); + + kfree(certs->data); + kfree(certs); +} + +struct sev_snp_certs *sev_snp_certs_new(void *data, u32 len) +{ + struct sev_snp_certs *certs; + + if (!len || !data) + return NULL; + + certs = kzalloc(sizeof(*certs), GFP_KERNEL); + if (!certs) + return NULL; + + certs->data = data; + certs->len = len; + kref_init(&certs->kref); + + return certs; +} +EXPORT_SYMBOL_GPL(sev_snp_certs_new); + +struct sev_snp_certs *sev_snp_certs_get(struct sev_snp_certs *certs) +{ + if (!certs) + return NULL; + + if (!kref_get_unless_zero(&certs->kref)) + return NULL; + + return certs; +} +EXPORT_SYMBOL_GPL(sev_snp_certs_get); + +void sev_snp_certs_put(struct sev_snp_certs *certs) +{ + if (!certs) + return; + + kref_put(&certs->kref, sev_snp_certs_release); +} +EXPORT_SYMBOL_GPL(sev_snp_certs_put); + static void sev_exit(struct kref *ref) { misc_deregister(&misc_dev->misc); diff --git a/drivers/crypto/ccp/sev-dev.h b/drivers/crypto/ccp/sev-dev.h index 19d79f9d4212..22374f3d3e2e 100644 --- a/drivers/crypto/ccp/sev-dev.h +++ b/drivers/crypto/ccp/sev-dev.h @@ -66,6 +66,8 @@ struct sev_device { bool snp_initialized; struct snp_host_map snp_host_map[MAX_SNP_HOST_MAP_BUFS]; + struct sev_snp_certs *snp_certs; + struct sev_user_data_snp_config snp_config; }; int sev_dev_init(struct psp_device *psp); diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 5ae61de96e44..2191d8b5423a 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -24,6 +24,16 @@ #define SEV_FW_BLOB_MAX_SIZE 0x4000 /* 16KB */ +struct sev_snp_certs { + void *data; + u32 len; + struct kref kref; +}; + +struct sev_snp_certs *sev_snp_certs_new(void *data, u32 len); +struct sev_snp_certs *sev_snp_certs_get(struct sev_snp_certs *certs); +void sev_snp_certs_put(struct sev_snp_certs *certs); + /** * SEV platform state */ diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h index 4dc6a3e7b3d5..d1e6a0615546 100644 --- a/include/uapi/linux/psp-sev.h +++ b/include/uapi/linux/psp-sev.h @@ -29,6 +29,8 @@ enum { SEV_GET_ID, /* This command is deprecated, use SEV_GET_ID2 */ SEV_GET_ID2, SNP_PLATFORM_STATUS, + SNP_SET_EXT_CONFIG, + SNP_GET_EXT_CONFIG, SEV_MAX, }; @@ -201,6 +203,21 @@ struct sev_user_data_snp_config { __u8 rsvd1[52]; } __packed; +/** + * struct sev_data_snp_ext_config - system wide configuration value for SNP. + * + * @config_address: address of the struct sev_user_data_snp_config or 0 when + * reported_tcb does not need to be updated. + * @certs_address: address of extended guest request certificate chain or + * 0 when previous certificate should be removed on SNP_SET_EXT_CONFIG. + * @certs_len: length of the certs + */ +struct sev_user_data_ext_snp_config { + __u64 config_address; /* In */ + __u64 certs_address; /* In */ + __u32 certs_len; /* In */ +}; + /** * struct sev_issue_cmd - SEV ioctl parameters * From patchwork Mon Jun 12 04:25:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275566 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 4992FC7EE2E for ; Mon, 12 Jun 2023 04:55:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E3CF16B0072; Mon, 12 Jun 2023 00:55:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DEBFF6B0074; Mon, 12 Jun 2023 00:55:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CB3976B0075; Mon, 12 Jun 2023 00:55:39 -0400 (EDT) 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 BC10A6B0072 for ; Mon, 12 Jun 2023 00:55:39 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 975DA400B0 for ; Mon, 12 Jun 2023 04:55:39 +0000 (UTC) X-FDA: 80892882798.20.E44368B Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2065.outbound.protection.outlook.com [40.107.94.65]) by imf02.hostedemail.com (Postfix) with ESMTP id 5813780002 for ; Mon, 12 Jun 2023 04:55:35 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=lBmrpuM4; spf=pass (imf02.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.65 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686545736; 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=lQtuNY0mUB1m5WmhFOnk56WbAk95FDsuLAXhADtmTW0=; b=L0xk1prPfENFz3jnisZbc992ya7eTj3nZo07ttm0eYCUIx8Dje0JSHoDjIDl6z5LU60w4s 0m/Ce/H0SPMvOgVY6OhvZ+MPTNhHPDNa6Jzk+Gs3YqdPprR9287md/A+dx3Vu1sV0W0Q3O rBXG9GRUOWOe4kSo4ayynsm9pT6MT50= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686545736; a=rsa-sha256; cv=pass; b=z1/70nDoBAAC6Yli85GZFSFHKwhCVIvCTtHm9GCCkP14a1S55uCAksGCDEQGDLkOCFTst/ IrYSZ5XYl5jlPRuLjn1L7aBg3lIbMxi43ThH+ks8yVRE7qYDJ+HLSEflM7Rwfnl39SBv9P rPIPyNvyPlt7okWsCGbDQ7p1lN/ZybM= ARC-Authentication-Results: i=2; imf02.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=lBmrpuM4; spf=pass (imf02.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.65 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CSD00ODkgqyobCVqRS1vYYyrFCgTP8o4093ZMeDcaibuqeRvmwHumD5vzX6yezdpOO2mzhSfUZ1lrQEeDTktTY0HrEJckoUpzp045985lgbyw4/oQnzYGCz38za+9IX+wOGI5f9N+dR1L/Dvji2q+VNnyPOXpXkGXsT9ib/xBH7hEbKbS/yos7IMXZDqv0eiF8ilI0TUp56iirXN2dupTTqv5fH3o1a1KWB+KP2Za3BaJUCH7edAWV3PN57Fyz5x27GjXiId76NwWkZo7H5VzoSIXZUZg4xvLHvoyLZzHDjAbnmHZzU1ERflzL6JGkTxzPB8iaJMccNtzh7q/nNH3w== 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=lQtuNY0mUB1m5WmhFOnk56WbAk95FDsuLAXhADtmTW0=; b=kr3QIuBLsiU01pkN+Wtq0aN/mHAyP/xQ8lPl8QOnnrqgJ/7IuhwsRjSgqozcoVdwFXdA5fmWpAHnwxM+9mQM4X5a4cZNd64xr4FWFmCPeUmP+UB5+wgEUNYGFRqD0BfuWYd27zxVlTp5gi9e1MU9wWdc5NfQlc1QRIkkR9uHjx8IVQ4YZxH90P0rJEDKOgEPI6+oaCalqYhaC9/qQZK9y9xXHK2ewdVfa+sLap2tR03MOdD/90gz6Xp2coQPZaXbFkN/bhQ+JqO4RDqCpFBcAg/mEw+Q8RFyxLCuFtoGZwRrWL06xPLQvQ82vwQ2kJBbIdePSsMVHO9Oh6CZT/cuIg== 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 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=lQtuNY0mUB1m5WmhFOnk56WbAk95FDsuLAXhADtmTW0=; b=lBmrpuM4H3c1DDHbphwQ2dN2HGj1tNbjcqUB1HBVEYhMgSWGf9iJWA1u1R2a/cYSo3nSkwe+S2o4PlwjbrLZ5KUOafgao71AFG3+DkZTNGBUGek4NE5b21D/Gtet9EbE97jtw7oJpkDhgH5DxK+ZRvuzZ0X4LzZdOPj0CkWJcuk= Received: from DM5PR08CA0052.namprd08.prod.outlook.com (2603:10b6:4:60::41) by BN9PR12MB5146.namprd12.prod.outlook.com (2603:10b6:408:137::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.44; Mon, 12 Jun 2023 04:55:32 +0000 Received: from CY4PEPF0000E9DB.namprd05.prod.outlook.com (2603:10b6:4:60:cafe::11) by DM5PR08CA0052.outlook.office365.com (2603:10b6:4:60::41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:55:32 +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 CY4PEPF0000E9DB.mail.protection.outlook.com (10.167.241.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:55:31 +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.23; Sun, 11 Jun 2023 23:55:30 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Dionna Glaze , Tom Lendacky , Alexey Kardashevskiy Subject: [PATCH RFC v9 49/51] x86/sev: Add KVM commands for per-instance certs Date: Sun, 11 Jun 2023 23:25:57 -0500 Message-ID: <20230612042559.375660-50-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CY4PEPF0000E9DB:EE_|BN9PR12MB5146:EE_ X-MS-Office365-Filtering-Correlation-Id: 81a20d62-19de-453a-ae5a-08db6b014041 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 6gqrwROaIbuwPJnGKntokR4IBQyuRdnfX70ONMTqzwn3+buydpoI8Lfr4bUSvkDhWNj6lc8K127WhiiypW7dQWFvX8MN6sYcFtstLH/jUK8SpcH4RKAxCcpPHl4yqvgh4HNGdSV3Y1dw8qIwVbk6jMwyKSovWmBszo/oBTddu+J9Dq2TM6bwjlBDgZUfS/3JW1TzoZgfwRlDlYTaup1gCz1hqrnkPrSjFf7hw50LMHhO/p49j8vENN867EY+xa1u14fx03xDNAJDeIDBM/VsfAhWEfOgFEiD8K3edUr0KzpAL9Zs43O0yakzXzDkG1ET1nPc4LrNjbIxIRQcKuqExaZ8JcY8nVWum7Nnw/G38ZXu+7jFQT8yzAbQMUajLa5V1Q0/4KPoPZYT+vfW+Q29OIj3xuPN+YfWTwpvijhlVMuItGjAKT0PoZOQk4kv6hWR19/mFn9kne8bvXo+eBBwItxvqPai8be0kiiO3Y1iamrRJv96LSWu1dCDLixW1lFyuMPGdO9lje1GZmRZ8KDrkxrtqzA4m9XjhWh/V2WxGqv1HlN+iixM0ElmjvMuxuIqZH7V20fyyCJVQuX7SOHq0lmaTg3io35uzHksauhxE186DBkILoJ0alhRGipQrBaszMPM3NnOs1mA5pE6DwPxCTRdBoQVXEpTF8px4J8kJ5wr6KwCDCGTs+U2lp7sp87F6lvmngkA+B1UYTDbHWoxtsc4Bp7q1YTbjhzWpNxlEOCEpWsGP1QNXYpug5/6ev4jHVA5OfpcJnW8egjVcE9Mhg== 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:(13230028)(4636009)(39860400002)(376002)(346002)(396003)(136003)(451199021)(36840700001)(40470700004)(46966006)(54906003)(5660300002)(6916009)(4326008)(7406005)(7416002)(8936002)(8676002)(44832011)(41300700001)(316002)(2906002)(186003)(16526019)(478600001)(70586007)(70206006)(6666004)(40460700003)(1076003)(356005)(82740400003)(81166007)(40480700001)(26005)(83380400001)(426003)(336012)(47076005)(36756003)(36860700001)(86362001)(82310400005)(2616005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:55:31.8680 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 81a20d62-19de-453a-ae5a-08db6b014041 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: CY4PEPF0000E9DB.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5146 X-Rspamd-Queue-Id: 5813780002 X-Rspam-User: X-Stat-Signature: e9ttsz9hzjuk6t7fqy5fd91jgm765pod X-Rspamd-Server: rspam03 X-HE-Tag: 1686545735-477 X-HE-Meta: U2FsdGVkX1+/N2+As4RZWspyYl50WRE0ChRmvACZsKWpGqs2kLJ0WSZWM0ZEjuKBTA7/GtQrttusMw1JJm4H4qP0JnLy7gpIYhQCMK+zDO6t0BWV5qWaJGkTFR0+ZWrSBveVm8xLgZqV0g7qPyeI0YZS6s2U1hoYMJAHpkVshxojxcI8qw8+suKYZ+hRw1nHXKOWIOOv5vvhnhF2KwzFcUMX2KB9Ksj03OSV1wXE89bChEpIHGMl0dvrbjif62pI1Ahl7f/WcSTh7YBa9T/fw3tqsXWowRayjWV/z3Daxh9cI5J4MuILHS5f6pAodo/rl2Stfdon+b92U3fEitO/1Gl9tRtHvb4yty1hlt0yDY+2eVmWE+DvH+TigB+Ae8oSdo2sJDWqO9spCWtJfrzLn28Lg4W7Red/2eDlcsIgnvJelAb3pXKUd2eNtJXTpaXV7/UOw232eroOhT36OMYHoJ3Etd2l0pAZ0QYCgJBIL+DKyC4kQ6Kw0sDo1fRZqFUyb8T65FeZ0DaDYSsEVtQ5LZQgBTYCvivi978tmqgqsYPBc1JCJ3xPg0LR6T9t8W7y6bI9Gs4IC+WeLP+DActGfvC60AVLGsUXFL4+iYkNx6fXMoSWkxjR3RWeDXk/7oPJ6GqPpYGM7r3w6DIk5ln3cfCB5QasukdWDQQMpCaGeWkiNjCkmbIoghjCt166P//Cr7jTTb5E9CedSPDTGB2VEezdlovO2lxmVkVCm66m5gqLRhOna3CpBh38aXVMtrvwEbA6NDvelX+y38sbc5lcxTOMK1ii09PglkpHeC9C7VxSZL84IYhGpx46hqmAApjuA6Gfwv+fSxKAZ233R7ULmKcunq/xQrf/3/tybBRpWZ+jxd63iDVYAV9wBTDb0/vKiMrXHZ1m2ME+nueEo8zG0TE/8ankRbebgKJN1SwRthy4CbZ9FnBO7CsC6yZBpZhA2UxhWOktAkOqznEhv2o nfZOewA+ 72igRFM5NMEcutSVCajvPridKFxh5Q/odJa2Hz4o3d61vI/g8wvzvFGGulsJPJOKqnqUmkSIk3mlmkfrUMwQT0n0JY7UTBz8XORScub4jhqHJWQap2uwB/iS7sISbjrQtgvtVqrG59pAa2gxRw0R4GikiW9DYlPItWWWQ8ivWAIOKSVOTRBlbAuNxYbBQkPyhnrR9k14Wsx6v4wQC5VcrEP48f/nZF77hlX2m7RUbAc6ofJKfYnL1INXf92WMxvIEVBOvp0Vxfq9gtNDpZIUBFJrB20JSivH02V3DmJbj4LR1nIq70a+lY8nDupV9LCxlScHxozuMrJN5pLaOIZt74ARe+IT0oS75V2geqoOAVMHr1tSvLRaqGr0Xx1UrRyd4fFbgO75tRuHO4diat9co4PaJNawtlBJnwBHxbpTByTOruXSn6o0wSW2SnQ== 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: From: Dionna Glaze The /dev/sev device has the ability to store host-wide certificates for the key used by the AMD-SP for SEV-SNP attestation report signing, but for hosts that want to specify additional certificates that are specific to the image launched in a VM, a different way is needed to communicate those certificates. Add two new KVM ioctl to handle this: KVM_SEV_SNP_{GET,SET}_CERTS The certificates that are set with this command are expected to follow the same format as the host certificates, but that format is opaque to the kernel. The new behavior for custom certificates is that the extended guest request command will now return the overridden certificates if they were installed for the instance. The error condition for a too small data buffer is changed to return the overridden certificate data size if there is an overridden certificate set installed. Setting a 0 length certificate returns the system state to only return the host certificates on an extended guest request. Also increase the SEV_FW_BLOB_MAX_SIZE another 4K page to allow space for an extra certificate. Cc: Tom Lendacky Cc: Paolo Bonzini Signed-off-by: Dionna Glaze Signed-off-by: Ashish Kalra [mdr: remove used of "we" and "this patch" in commit log, squash in documentation patch] Signed-off-by: Michael Roth [aik: snp_handle_ext_guest_request() now uses the CCP's cert object without copying things over, only refcounting needed.] Signed-off-by: Alexey Kardashevskiy --- .../virt/kvm/x86/amd-memory-encryption.rst | 44 +++++++ arch/x86/kvm/svm/sev.c | 115 ++++++++++++++++++ arch/x86/kvm/svm/svm.h | 1 + include/linux/psp-sev.h | 2 +- include/uapi/linux/kvm.h | 12 ++ 5 files changed, 173 insertions(+), 1 deletion(-) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documentation/virt/kvm/x86/amd-memory-encryption.rst index cd77a19577fe..21c1894d78ef 100644 --- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst @@ -537,6 +537,50 @@ Returns: 0 on success, -negative on error See SEV-SNP specification for further details on launch finish input parameters. +22. KVM_SEV_SNP_GET_CERTS +------------------------- + +After the SNP guest launch flow has started, the KVM_SEV_SNP_GET_CERTS command +can be issued to request the data that has been installed with the +KVM_SEV_SNP_SET_CERTS command. + +Parameters (in/out): struct kvm_sev_snp_get_certs + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_snp_get_certs { + __u64 certs_uaddr; + __u64 certs_len + }; + +If no certs have been installed, then the return value is -ENOENT. +If the buffer specified in the struct is too small, the certs_len field will be +overwritten with the required bytes to receive all the certificate bytes and the +return value will be -EINVAL. + +23. KVM_SEV_SNP_SET_CERTS +------------------------- + +After the SNP guest launch flow has started, the KVM_SEV_SNP_SET_CERTS command +can be issued to override the /dev/sev certs data that is returned when a +guest issues an extended guest request. This is useful for instance-specific +extensions to the host certificates. + +Parameters (in/out): struct kvm_sev_snp_set_certs + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_snp_set_certs { + __u64 certs_uaddr; + __u64 certs_len + }; + +The certs_len field may not exceed SEV_FW_BLOB_MAX_SIZE. + References ========== diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index adbe8c242d81..bdf32aa971d8 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2277,6 +2277,113 @@ static int snp_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) return ret; } +static int snp_get_instance_certs(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct kvm_sev_snp_get_certs params; + struct sev_snp_certs *snp_certs; + int rc = 0; + + if (!sev_snp_guest(kvm)) + return -ENOTTY; + + if (!sev->snp_context) + return -EINVAL; + + if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, + sizeof(params))) + return -EFAULT; + + snp_certs = sev_snp_certs_get(sev->snp_certs); + /* No instance certs set. */ + if (!snp_certs) + return -ENOENT; + + if (params.certs_len < sev->snp_certs->len) { + /* Output buffer too small. Return the required size. */ + params.certs_len = sev->snp_certs->len; + + if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, + sizeof(params))) + rc = -EFAULT; + else + rc = -EINVAL; /* May be ENOSPC? */ + } else { + if (copy_to_user((void __user *)(uintptr_t)params.certs_uaddr, + snp_certs->data, snp_certs->len)) + rc = -EFAULT; + } + + sev_snp_certs_put(snp_certs); + + return rc; +} + +static void snp_replace_certs(struct kvm_sev_info *sev, struct sev_snp_certs *snp_certs) +{ + sev_snp_certs_put(sev->snp_certs); + sev->snp_certs = snp_certs; +} + +static int snp_set_instance_certs(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + unsigned long length = SEV_FW_BLOB_MAX_SIZE; + struct kvm_sev_snp_set_certs params; + struct sev_snp_certs *snp_certs; + void *to_certs; + int ret; + + if (!sev_snp_guest(kvm)) + return -ENOTTY; + + if (!sev->snp_context) + return -EINVAL; + + if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, + sizeof(params))) + return -EFAULT; + + if (params.certs_len > SEV_FW_BLOB_MAX_SIZE) + return -EINVAL; + + /* + * Setting a length of 0 is the same as "uninstalling" instance- + * specific certificates. + */ + if (params.certs_len == 0) { + snp_replace_certs(sev, NULL); + return 0; + } + + /* Page-align the length */ + length = ALIGN(params.certs_len, PAGE_SIZE); + + to_certs = kmalloc(length, GFP_KERNEL | __GFP_ZERO); + if (!to_certs) + return -ENOMEM; + + if (copy_from_user(to_certs, + (void __user *)(uintptr_t)params.certs_uaddr, + params.certs_len)) { + ret = -EFAULT; + goto error_exit; + } + + snp_certs = sev_snp_certs_new(to_certs, length); + if (!snp_certs) { + ret = -ENOMEM; + goto error_exit; + } + + snp_replace_certs(sev, snp_certs); + + return 0; +error_exit: + kfree(to_certs); + return ret; +} + int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -2376,6 +2483,12 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) case KVM_SEV_SNP_LAUNCH_FINISH: r = snp_launch_finish(kvm, &sev_cmd); break; + case KVM_SEV_SNP_GET_CERTS: + r = snp_get_instance_certs(kvm, &sev_cmd); + break; + case KVM_SEV_SNP_SET_CERTS: + r = snp_set_instance_certs(kvm, &sev_cmd); + break; default: r = -EINVAL; goto out; @@ -2591,6 +2704,8 @@ static int snp_decommission_context(struct kvm *kvm) snp_free_firmware_page(sev->snp_context); sev->snp_context = NULL; + sev_snp_certs_put(sev->snp_certs); + return 0; } diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 0d4c29a4300a..72be0c440b16 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -95,6 +95,7 @@ struct kvm_sev_info { u64 snp_init_flags; void *snp_context; /* SNP guest context page */ u64 sev_features; /* Features set at VMSA creation */ + struct sev_snp_certs *snp_certs; }; struct kvm_svm { diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 2191d8b5423a..7b65dd5808a1 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -22,7 +22,7 @@ #define __psp_pa(x) __pa(x) #endif -#define SEV_FW_BLOB_MAX_SIZE 0x4000 /* 16KB */ +#define SEV_FW_BLOB_MAX_SIZE 0x5000 /* 20KB */ struct sev_snp_certs { void *data; diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 175b958f103f..fa1c300303d6 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1937,6 +1937,8 @@ enum sev_cmd_id { KVM_SEV_SNP_LAUNCH_START, KVM_SEV_SNP_LAUNCH_UPDATE, KVM_SEV_SNP_LAUNCH_FINISH, + KVM_SEV_SNP_GET_CERTS, + KVM_SEV_SNP_SET_CERTS, KVM_SEV_NR_MAX, }; @@ -2084,6 +2086,16 @@ struct kvm_sev_snp_launch_finish { __u8 pad[6]; }; +struct kvm_sev_snp_get_certs { + __u64 certs_uaddr; + __u64 certs_len; +}; + +struct kvm_sev_snp_set_certs { + __u64 certs_uaddr; + __u64 certs_len; +}; + #define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0) #define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1) #define KVM_DEV_ASSIGN_MASK_INTX (1 << 2) From patchwork Mon Jun 12 04:25:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275582 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 40092C7EE25 for ; Mon, 12 Jun 2023 04:56:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D77DE6B0072; Mon, 12 Jun 2023 00:56:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D277A6B0074; Mon, 12 Jun 2023 00:56:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BF06F8E0002; Mon, 12 Jun 2023 00:56:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id AEC946B0072 for ; Mon, 12 Jun 2023 00:56:51 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 8580612010E for ; Mon, 12 Jun 2023 04:56:51 +0000 (UTC) X-FDA: 80892885822.10.B44153B Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2089.outbound.protection.outlook.com [40.107.100.89]) by imf21.hostedemail.com (Postfix) with ESMTP id 8334D1C0003 for ; Mon, 12 Jun 2023 04:56:48 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=LLGvTi+q; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf21.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.100.89 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; 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=1686545808; 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=MOjQ9OR6FXBKvOm3ej71JfND9jQgatXW3QKky35AJQE=; b=igidWBjGNfANZjOFIJEEIN09Xz7CyB+94BirrcGdhma25FcNQWDq7xlnCXTHyGaqTp6kR3 qjjIB6KcmucJlGefOLmoUivrKnDX7VYAAvZVtszIdy4dBcwkRZsNISHvaZfza3SA+sPKH1 y4+sBwcVFIveDrohIlG75AoBhtEfEN0= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686545808; a=rsa-sha256; cv=pass; b=ZQFOB+B/5FXQMa2VSuTpn0NdEHZZYtI0621/5FO2OXU/b1kySXHDt0tWZ4pqLy0JN/MRpK HHciV7r5iMnZgh2+XeB04fnCd7sdRgNTbVYGC9yOf04yjnBf5n3N/I55z3f8d/LnPnB9+Z MG3iQHXk4twIbTqS2zz7jd9cXLypEwo= ARC-Authentication-Results: i=2; imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=LLGvTi+q; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf21.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.100.89 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=g5ybTzg6orqF1L1+74qH7RXySg/JqLZ9uQrEzI2egNB/A7BpiUKI0oEZf2QDXD7Go0fZjWlI/FBTn7CFeVixVZH/VzBa5trRgchB6fsGaio8BFGgH0wpU90iFN5xCv2Hw2YViAM/E1JpiT4ECD0cSugKnlNNoITQ4y0iM1nxleo/1Y8KY4Npzk2SQ3yA7ghQOne0HcVvFOp5mzEwAxiOYThFcOmgo1WUsvUAqC8INMtQ5jOp8UBkPv2a8PHiJuoJW62b+zJHJLjbugZjzEKLKEtEui0kwPuPYJYwOXVjrug7WLFIrkhBC70aCW440P/9bJf71FDOyUK2jGNG2oK4Ag== 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=MOjQ9OR6FXBKvOm3ej71JfND9jQgatXW3QKky35AJQE=; b=QmnmY17XM0+Iv+D5RYndHp9UL7cJjFK0wGdcSuNP+qrtoepM/NZ/n5uA8FqMBRJcRg8IgCjaKfNBo+fNjFLtMxBy8VxMfNK4owVsAQ6o1HE+9GBQrm5JfD2rQX+PPX4Cw+J7Usi/5VDkU1E9VlnOsLXYIGlVzvg4gjiiR0hTlI4cL+qaG9qM3YXGfUGak/X15h111DqhKweXSf7kfO9OjN9jCW65GWkxKHhFpO44pblsK+K8EaJcuvAqXwlIvC3ekRRTwuwLLuYOVsVwClzJHCkboBEYj+td53NFb87hm/tDABs7C/zSqAwEbWRVcFVmdK3xrPuOwFyKxrAxKfQYPQ== 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 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=MOjQ9OR6FXBKvOm3ej71JfND9jQgatXW3QKky35AJQE=; b=LLGvTi+qj7M3SOC8/I9lZ1zOVzIAWlwMggN7gZ3F1UesWm9Ub8njHdEMcxvj0TXJwz4zolVaoniyScV5G1sS3K7rxF+LMt95yeRMdnhfc+uBHa0PVHhtIAptUEj9P1q1Yy4p/n/X1rV/5jjABHUv3icHmZcSPsiRg54dkhZrkbg= Received: from CYXPR03CA0092.namprd03.prod.outlook.com (2603:10b6:930:d3::18) by DM4PR12MB6040.namprd12.prod.outlook.com (2603:10b6:8:af::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.33; Mon, 12 Jun 2023 04:56:45 +0000 Received: from CY4PEPF0000E9D6.namprd05.prod.outlook.com (2603:10b6:930:d3:cafe::1d) by CYXPR03CA0092.outlook.office365.com (2603:10b6:930:d3::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:56:45 +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 CY4PEPF0000E9D6.mail.protection.outlook.com (10.167.241.80) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:56:45 +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.23; Sun, 11 Jun 2023 23:56:14 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Alexey Kardashevskiy Subject: [PATCH RFC v9 50/51] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event Date: Sun, 11 Jun 2023 23:25:58 -0500 Message-ID: <20230612042559.375660-51-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CY4PEPF0000E9D6:EE_|DM4PR12MB6040:EE_ X-MS-Office365-Filtering-Correlation-Id: 39def5c7-7280-43f6-5017-08db6b016c08 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 55sn1+nspxoKvDHHysY6nbWoCVhkRUsJLiUDe7i9mZ8MHZHGw0i2IE17i+h2XTXI2Cd9mJtz/K1UZvBp7NR2EdGfbZqJ7Z19a/raelWFguWJbJGJ3B1VYSc2saWjlQtoL8zxziX5i+4t7PE7fB6MBeLtJCzuz9TMTjBBkcyJYaEX+ZKoVXvAFM0W1cddWWjQd1Q8yRD3SSptLaxH7nsx3yu5poLcfOOtqFz2M3lPdmW16v+XgfzXa16R1L3iaQbWeR/rzhuW/+2z2WfD+QtnRnjoFq+zVHAkGozZqPG5AQuSCaAn23zh2oE/iI+I5z0EOoQp31z/Svtg5imwETInzOGAmg/uaMNypBTyw7mI3uVnUgqkwuT3F/UzrxK43vbt3HOayFuZTwqlfClTl/SAbzC3bpoFTou86uOowBydU+QOWVBSJNmbCM2v5tG5JUxNuHabtgMgWIY6OkWNHQv9zm7gjadZ2B1dHveiPl4zLbXh6BWEsheWk2Y3us6iNoJiKDqfLhhakdTH2f3x7KW1tgilam1+xGaMFRmzey9wHeZeU1wM58kdWiHlBhhYBR/NV/Vlk6jfRiuzkdnU9Tu0XJb5C6Pk/x0HAEHRmFBdXZ5v+xHGqAmZYA6cuVDqoT41llq3QnLcxXVxy/tZlyKdmRPRuUgmL8ftBfKFO5gLXYwS4lPKgVyVgJhgUq1YLKjU5WrEc/ADxF3VRwrKG896VtC1cdCfM2CRpdkSu5xU5dSnNNRB9vXQbQtSNFRf0E2i6cjMeL6oHUwrEQdMarrx/w== 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:(13230028)(4636009)(136003)(346002)(376002)(39860400002)(396003)(451199021)(36840700001)(40470700004)(46966006)(186003)(8676002)(8936002)(54906003)(478600001)(6666004)(5660300002)(41300700001)(316002)(16526019)(26005)(44832011)(1076003)(7416002)(7406005)(70586007)(70206006)(6916009)(4326008)(2616005)(83380400001)(40460700003)(2906002)(426003)(336012)(47076005)(82740400003)(81166007)(40480700001)(356005)(36860700001)(86362001)(82310400005)(36756003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:56:45.3274 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 39def5c7-7280-43f6-5017-08db6b016c08 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: CY4PEPF0000E9D6.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB6040 X-Stat-Signature: u7k3ijneyuob9yonaiacn7t1hngbfrea X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 8334D1C0003 X-Rspam-User: X-HE-Tag: 1686545808-362417 X-HE-Meta: U2FsdGVkX1+Fd7mf8cqGreU5NL1QcLyNOWJ6/XGClFTwHSnf+dHJVnCog9afJ2oCtM8vYDN1dfaf5w+2KEvTYlsNAyuaXqSWNmkPyvC+yxFvI/Dg1mHRnPkLRQH31hM0APpYy2hstBf6MFXe7lehiGtwfqCgrUNoueohkFCk18JUz3iMiit8eBVO8DKNqfcuIg2jD179pT+3+NRrR+lOLL6w7b+1BaqEBjX5VSbeU8XwNZbyk6ttcIPydYCIkRbKBaSE1EKxbvN0lQ0Jp5B+6AM3x6N1L51HACj0yT6GTmge4wHKpyClTJ5gVtHI8tVeyQ/uil5e39qh2NudW9j6YuA3sADAP+J7r78CuJZ5hEd1XuhUykzJteTLi4yFgjDZIZ8W9DntUMPS1nAzaPOLAIKaUE46TNzmZnlcFNfHBBECCXlwZFfC0KAaDsc6W8LzzQIkIF22Qtv3N3QhzNsqDSTyPyMO3vGv7poe7tVsUq9Rmmk3N1UFfnhoxinj7a7GwVC2LC775By82OG2l9gLwbUQ+y/hlOnjqdtt6Jg74Gag1rtRnXW83NBC4Gra9JQO14bqjs5UM1qwvCaoFGvLZ8dzuCzGoyXI18Z6Xc12bG+MPPIerD7Tdlb5ybfrBDvK0VTiFl/utnR1vtuFZVbZdHR/WrhTSooBi3WZwVl4Tl0dJqtv4m1sKGBLK/MgmTSxTlEbb6odPqLGSep++SIUaoT5EGkEg0ZhXEyWATIiw/12X1fI3+eB6MM3VqUQz/yJKTWc6HbQ6KcmjnIxkFQ3lG+d2Ow0huaq3xL94+U5vGl4Xkt75XMxtFbXWakCOzqkSpS0f1Acu9Yv2fd/vmIm2oRph2o3W/EUEKHU1vBg2oRyrjuxEpzJ+kQFXwjto3AJosA+hqk+MyfQnStyVcMRp2XGo7QhF/EnnvtGpWhC38Z1f5BdHtLPe8trTkAg4C8AcXXDpAst4W10K3bn1m7 8ioGVV9Z QuSmQFuhZXFhQLAcYXM7MSrFOQGXn9+brjxBvkFjTp+kjrCvcTO8oyfq0g+M3ZnaSDC//AKaDpuCMkgmSjUAwMYOcI59pLnLWnOXl3cPhpvfPfp0ksBiylR0eJcmSPUj3+CBaPpBmcn6xoHXpniL2O1GAWew1uWXkao2nnmU0/RBCaYaA7IF2wQ69mSzzP1g/7nuXaXhvQh/s650Oj9OM4Rx+V+jWFTn3WRDJ51uTFnlZg0NBAT7OI3cT/KC75a97tZHVt15+IMWPUMthzuimjDOGuNjVIPo5iKhT3RRhKJw/hfn5GEuxMd43Ykr9qsDPrbWriX/0mGlQLkojjGfa6qyPf0BG2dYQm0tcTf9hcBt/ovc= 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: From: Brijesh Singh Version 2 of GHCB specification added the support for two SNP Guest Request Message NAE events. The events allows for an SEV-SNP guest to make request to the SEV-SNP firmware through hypervisor using the SNP_GUEST_REQUEST API define in the SEV-SNP firmware specification. The SNP_EXT_GUEST_REQUEST is similar to SNP_GUEST_REQUEST with the difference of an additional certificate blob that can be passed through the SNP_SET_CONFIG ioctl defined in the CCP driver. The CCP driver provides snp_guest_ext_guest_request() that is used by the KVM to get both the report and certificate data at once. Co-developed-by: Alexey Kardashevskiy Signed-off-by: Alexey Kardashevskiy Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra [mdr: ensure FW command failures are indicated to guest] Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 175 +++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.h | 1 + drivers/crypto/ccp/sev-dev.c | 15 +++ include/linux/psp-sev.h | 1 + 4 files changed, 192 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index bdf32aa971d8..9f7defce1988 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -328,6 +328,8 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) ret = verify_snp_init_flags(kvm, argp); if (ret) goto e_free; + + mutex_init(&sev->guest_req_lock); } ret = sev_platform_init(&argp->error); @@ -2321,8 +2323,10 @@ static int snp_get_instance_certs(struct kvm *kvm, struct kvm_sev_cmd *argp) static void snp_replace_certs(struct kvm_sev_info *sev, struct sev_snp_certs *snp_certs) { + mutex_lock(&sev->guest_req_lock); sev_snp_certs_put(sev->snp_certs); sev->snp_certs = snp_certs; + mutex_unlock(&sev->guest_req_lock); } static int snp_set_instance_certs(struct kvm *kvm, struct kvm_sev_cmd *argp) @@ -3171,6 +3175,8 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) case SVM_VMGEXIT_UNSUPPORTED_EVENT: case SVM_VMGEXIT_HV_FEATURES: case SVM_VMGEXIT_PSC: + case SVM_VMGEXIT_GUEST_REQUEST: + case SVM_VMGEXIT_EXT_GUEST_REQUEST: break; default: reason = GHCB_ERR_INVALID_EVENT; @@ -3604,6 +3610,163 @@ static int sev_snp_ap_creation(struct vcpu_svm *svm) return ret; } +static unsigned long snp_setup_guest_buf(struct vcpu_svm *svm, + struct sev_data_snp_guest_request *data, + gpa_t req_gpa, gpa_t resp_gpa) +{ + struct kvm_vcpu *vcpu = &svm->vcpu; + struct kvm *kvm = vcpu->kvm; + kvm_pfn_t req_pfn, resp_pfn; + struct kvm_sev_info *sev; + + sev = &to_kvm_svm(kvm)->sev_info; + + if (!IS_ALIGNED(req_gpa, PAGE_SIZE) || !IS_ALIGNED(resp_gpa, PAGE_SIZE)) + return SEV_RET_INVALID_PARAM; + + req_pfn = gfn_to_pfn(kvm, gpa_to_gfn(req_gpa)); + if (is_error_noslot_pfn(req_pfn)) + return SEV_RET_INVALID_ADDRESS; + + resp_pfn = gfn_to_pfn(kvm, gpa_to_gfn(resp_gpa)); + if (is_error_noslot_pfn(resp_pfn)) + return SEV_RET_INVALID_ADDRESS; + + if (rmp_make_private(resp_pfn, 0, PG_LEVEL_4K, 0, true)) + return SEV_RET_INVALID_ADDRESS; + + data->gctx_paddr = __psp_pa(sev->snp_context); + data->req_paddr = __sme_set(req_pfn << PAGE_SHIFT); + data->res_paddr = __sme_set(resp_pfn << PAGE_SHIFT); + + return 0; +} + +static void snp_cleanup_guest_buf(struct sev_data_snp_guest_request *data, unsigned long *rc) +{ + u64 pfn = __sme_clr(data->res_paddr) >> PAGE_SHIFT; + int ret; + + ret = snp_page_reclaim(pfn); + if (ret) + *rc = SEV_RET_INVALID_ADDRESS; + + ret = rmp_make_shared(pfn, PG_LEVEL_4K); + if (ret) + *rc = SEV_RET_INVALID_ADDRESS; +} + +static void snp_handle_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_gpa) +{ + struct sev_data_snp_guest_request data = {0}; + struct kvm_vcpu *vcpu = &svm->vcpu; + struct kvm *kvm = vcpu->kvm; + struct kvm_sev_info *sev; + unsigned long rc; + int err; + + if (!sev_snp_guest(vcpu->kvm)) { + rc = SEV_RET_INVALID_GUEST; + goto e_fail; + } + + sev = &to_kvm_svm(kvm)->sev_info; + + mutex_lock(&sev->guest_req_lock); + + rc = snp_setup_guest_buf(svm, &data, req_gpa, resp_gpa); + if (rc) + goto unlock; + + rc = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &data, &err); + if (rc) + /* Ensure an error value is returned to guest. */ + rc = err ? err : SEV_RET_INVALID_ADDRESS; + + snp_cleanup_guest_buf(&data, &rc); + +unlock: + mutex_unlock(&sev->guest_req_lock); + +e_fail: + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, rc); +} + +static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_gpa) +{ + struct sev_data_snp_guest_request req = {0}; + struct sev_snp_certs *snp_certs = NULL; + struct kvm_vcpu *vcpu = &svm->vcpu; + struct kvm *kvm = vcpu->kvm; + unsigned long data_npages; + struct kvm_sev_info *sev; + unsigned long exitcode = 0; + u64 data_gpa; + int err, rc; + + if (!sev_snp_guest(vcpu->kvm)) { + rc = SEV_RET_INVALID_GUEST; + goto e_fail; + } + + sev = &to_kvm_svm(kvm)->sev_info; + + data_gpa = vcpu->arch.regs[VCPU_REGS_RAX]; + data_npages = vcpu->arch.regs[VCPU_REGS_RBX]; + + if (!IS_ALIGNED(data_gpa, PAGE_SIZE)) { + exitcode = SEV_RET_INVALID_ADDRESS; + goto e_fail; + } + + mutex_lock(&sev->guest_req_lock); + + rc = snp_setup_guest_buf(svm, &req, req_gpa, resp_gpa); + if (rc) + goto unlock; + + /* + * If a VMM-specific certificate blob hasn't been provided, grab the + * host-wide one. + */ + snp_certs = sev_snp_certs_get(sev->snp_certs); + if (!snp_certs) + snp_certs = sev_snp_global_certs_get(); + + /* + * If there is a host-wide or VMM-specific certificate blob available, + * make sure the guest has allocated enough space to store it. + * Otherwise, inform the guest how much space is needed. + */ + if (snp_certs && (data_npages << PAGE_SHIFT) < snp_certs->len) { + vcpu->arch.regs[VCPU_REGS_RBX] = snp_certs->len >> PAGE_SHIFT; + exitcode = SNP_GUEST_REQ_INVALID_LEN; + goto cleanup; + } + + rc = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &req, &err); + if (rc) { + /* pass the firmware error code */ + exitcode = err; + goto cleanup; + } + + /* Copy the certificate blob in the guest memory */ + if (sev->snp_certs && + kvm_write_guest(kvm, data_gpa, sev->snp_certs->data, sev->snp_certs->len)) + exitcode = SEV_RET_INVALID_ADDRESS; + +cleanup: + sev_snp_certs_put(snp_certs); + snp_cleanup_guest_buf(&req, &exitcode); + +unlock: + mutex_unlock(&sev->guest_req_lock); + +e_fail: + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, exitcode); +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3863,6 +4026,18 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) SVM_EVTINJ_VALID); } + ret = 1; + break; + case SVM_VMGEXIT_GUEST_REQUEST: + snp_handle_guest_request(svm, control->exit_info_1, control->exit_info_2); + + ret = 1; + break; + case SVM_VMGEXIT_EXT_GUEST_REQUEST: + snp_handle_ext_guest_request(svm, + control->exit_info_1, + control->exit_info_2); + ret = 1; break; case SVM_VMGEXIT_UNSUPPORTED_EVENT: diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 72be0c440b16..31cd8b3e6877 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -96,6 +96,7 @@ struct kvm_sev_info { void *snp_context; /* SNP guest context page */ u64 sev_features; /* Features set at VMSA creation */ struct sev_snp_certs *snp_certs; + struct mutex guest_req_lock; /* Lock for guest request handling */ }; struct kvm_svm { diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 175c24163ba0..096ba15d0740 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -2109,6 +2109,21 @@ void sev_snp_certs_put(struct sev_snp_certs *certs) } EXPORT_SYMBOL_GPL(sev_snp_certs_put); +struct sev_snp_certs *sev_snp_global_certs_get(void) +{ + struct sev_device *sev; + + if (!psp_master || !psp_master->sev_data) + return NULL; + + sev = psp_master->sev_data; + if (!sev->snp_initialized) + return NULL; + + return sev_snp_certs_get(sev->snp_certs); +} +EXPORT_SYMBOL_GPL(sev_snp_global_certs_get); + static void sev_exit(struct kref *ref) { misc_deregister(&misc_dev->misc); diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 7b65dd5808a1..1235eb3110cb 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -33,6 +33,7 @@ struct sev_snp_certs { struct sev_snp_certs *sev_snp_certs_new(void *data, u32 len); struct sev_snp_certs *sev_snp_certs_get(struct sev_snp_certs *certs); void sev_snp_certs_put(struct sev_snp_certs *certs); +struct sev_snp_certs *sev_snp_global_certs_get(void); /** * SEV platform state From patchwork Mon Jun 12 04:25:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275584 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 7F11EC7EE2E for ; Mon, 12 Jun 2023 04:57:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 22E046B0075; Mon, 12 Jun 2023 00:57:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2044F6B007B; Mon, 12 Jun 2023 00:57:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0A6228E0002; Mon, 12 Jun 2023 00:57:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id F0F9E6B0075 for ; Mon, 12 Jun 2023 00:57:19 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 616B614010C for ; Mon, 12 Jun 2023 04:57:19 +0000 (UTC) X-FDA: 80892886998.23.5E210C7 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2088.outbound.protection.outlook.com [40.107.92.88]) by imf25.hostedemail.com (Postfix) with ESMTP id 620C0A0009 for ; Mon, 12 Jun 2023 04:57:16 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=md+6pjLM; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf25.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.88 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; 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=1686545836; 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=RjrtDRnX4hUbBvCvwo1i1y/QPZ+dHBYmNvNtfJMhRak=; b=o1vrhHlGvtGQlvuYN5fMcoz44wPhwhhBA4jMSdZzUrP14QxwPncdZTxIYLpLG8nvR6DFET b7NRn87ikJOc26hx/RPCK8Dwvdbb02BEUwsoqSvzhaU9HUfSAkWrTZ7gqrwdFJgAVesnuS OEbhh2nKSEsudwu6wrSDCF7PcnNfu3c= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1686545836; a=rsa-sha256; cv=pass; b=HxKtYmZsg474CPr+Jcp2keynZm+yBAObE5ao6FwbQNfyocTqu/ndUFNfThtcYqYWYU5hmE B5ESzLYNlIFbYGt0I6LlOMIavIInUXz5RoGAjXhrRCj/DNj8/bTp5FIhUBuUdl//5ftjVC mpbL0tsmc2N7niqOaYd4xROa/78Zack= ARC-Authentication-Results: i=2; imf25.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=md+6pjLM; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf25.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.88 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=h7QwnoHsLQAel88XQqN7XTNHE5m4nCWOLANeKr5Qi+aGYKRz5NJNLVdA2YrW2YnWlyb9Vg3glh5KCiCvpJBoRYL/2z5IknPPhSnhiJE+ClY7h+VR3zIXKpOd/VvX+CZTAdciMwTMDFFaxEKn6URJ8q/HBky1KptBKrxevUrP1cbIN8Y9GpsciH403CfvgacW9xCkt6nHpqmXmlefmv74blUCRDJr7X0spG4c9vVaTpl9z6a0cXYL9/npk+ovLE73w+9rplzXwNbx2cMfT7HNmXD3F34IG757DsjqBa3R+R0ztOKiZ9IhXmHVuwXOCZNuZKA4xsM7r0A7AjLHN/4tCw== 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=RjrtDRnX4hUbBvCvwo1i1y/QPZ+dHBYmNvNtfJMhRak=; b=EkDCqPgU43oi3TPjnEN5S+ijm64HMCu/0+rYqzBFReN+N5+Nc31Ff7jy57+Gx2CHolXAfo/yTAq//S35Tn0/5kHDhBItxYZ8OVO44tmSI8Vdjj0sOqcmMQy4KUIxvwPCckTD4uqKFfWTOg7Qo5qLLRzjpL10iCupQfPotJYXrR8TCFBuN/8guNOUrlRq8PJqT90Ly+Em4doy38VOEWk3binJfDKeIceeLLfzv9wZGx4mOGbIr38AW1LLEgixZ3MF80KrHDdzvqOfCBEB7FAbXqe0HY3Na1TALXBBbptiotmjXHw+1z1Q9M5VxSKNILLNMm0HEdITngpQkipgxJAw/w== 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 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=RjrtDRnX4hUbBvCvwo1i1y/QPZ+dHBYmNvNtfJMhRak=; b=md+6pjLMK2ykrkM0a/b+89M7rIa2mvsvbVjUWPp4T4lkWySrLx3VYg636nFoMpZFdH2H9xE2PLvnMI2d2rz3SpEpZiwboB9yyPzxF6kXHNg9PYhBzICl1PgQFh9HTx6hpnZtMwWc1hJbhkonz+tl6hH+1V4NMOpMGl6DwJvkxX8= Received: from CYXPR03CA0077.namprd03.prod.outlook.com (2603:10b6:930:d3::19) by CH0PR12MB5249.namprd12.prod.outlook.com (2603:10b6:610:d0::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.42; Mon, 12 Jun 2023 04:57:10 +0000 Received: from CY4PEPF0000E9D6.namprd05.prod.outlook.com (2603:10b6:930:d3:cafe::d5) by CYXPR03CA0077.outlook.office365.com (2603:10b6:930:d3::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:57:09 +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 CY4PEPF0000E9D6.mail.protection.outlook.com (10.167.241.80) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:57:09 +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.23; Sun, 11 Jun 2023 23:57:07 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 51/51] crypto: ccp: Add debug support for decrypting pages Date: Sun, 11 Jun 2023 23:25:59 -0500 Message-ID: <20230612042559.375660-52-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] 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: CY4PEPF0000E9D6:EE_|CH0PR12MB5249:EE_ X-MS-Office365-Filtering-Correlation-Id: 499dbd1a-db19-494a-8345-08db6b017a88 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 71t+1axCyvSl/V6S59kONElavhM4jLvegT9GfJXF519MRl0ZhzWRf0RmHqL0U8U4jv/iq52DGbgm13fWzwMeLlhu3mB7SikkcGkuUhUm6l0pDcMcY0eR7hoys9oP3+hHbNtFRJKT63oDbzmDcqq32Qq2UaYQmxx1D4TWtChYzkmqK5A9bSWxQ1r/t7eePnSryFBcNiPxKX3bgP9pb0A0T7+3Gmnveqau5X+QRvhVyESyhzeIRK9kS87j8fyFGgxRKGOj0t+gEpDQ/sZQPxiRiM0xsx0KPSADx+fu+44Yir5r/W6Kot34PCo4TPG733wY3DIFF5P7YlvKiPiu5WyCRumeGXaU5mUHCSqZszyhdVb8RTICGh8le46ezuUebuTom1Xpo9tpDEsTMh9VmM3Rs23jdtXT/NT3QedmaviPX9MMGyl0l6TiDoUIAvAZYw8yYp1cpyD6HC7E5mV8Rul9SjcQneujZ7VwD5MHu7fCvI+0pRD40060pbBvgpOOFTD6ZNPLQKPdHBwpedaED+5+JAzSy8AKLFOLuxL9X1IWDlDWY3c2XR7ChDnMSCZXgOpJ0HkWooKevUmPFiefWi+CZSFTpS9QeyJLwrmrbp76nLwr6XShajkaNUiVpv+RLHD5ixEPFGbJZ9jlCJJSfXGfu6jggqWwAvftDe2pxUO+vzTjzsXPuaP2nNlVgXmeuoNtpHLEcd7C2VlpnvGeCzA0ZaUkGEvJbLsysPwUzbtcOVqh+wULmBvm2PIcJRLvSmicr4UTVjP0yQNS1ijQRcBGDnYo1UPZwr92wlHvbZRk0bI= 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:(13230028)(4636009)(396003)(39860400002)(136003)(346002)(376002)(451199021)(36840700001)(40470700004)(46966006)(2616005)(36756003)(2906002)(7406005)(82310400005)(86362001)(44832011)(7416002)(40480700001)(6666004)(47076005)(16526019)(36860700001)(186003)(83380400001)(40460700003)(5660300002)(336012)(26005)(426003)(1076003)(82740400003)(356005)(81166007)(478600001)(54906003)(70586007)(4326008)(70206006)(6916009)(41300700001)(316002)(8676002)(8936002)(36900700001)(134885004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:57:09.6400 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 499dbd1a-db19-494a-8345-08db6b017a88 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: CY4PEPF0000E9D6.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5249 X-Stat-Signature: xw9397a9ynd7cj8sbp9yf539odo4boys X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 620C0A0009 X-Rspam-User: X-HE-Tag: 1686545836-666483 X-HE-Meta: U2FsdGVkX19zbu0aeiUUVftx8UuPFSOaAZKQ9Z/Gu4lgGKgM9xsDEb5jIShdeW7giieiMva9K2KcrQ3uPpPjdy+eCDmktcdEwX3x6U6v4jBg7ranr7t90tlsFU9xNzvE+emsxcPPBEJjEWVY9nc9q2TCsQPaq8MG8hqmHrjV36wt/i1xXV1of5RnC5LTWaeraMMGjRiAI5OkS8s34VapaZ4gLNRTwTicevKqJ1jZO57QtiP4SLWRYCdkCT5VER6AD2cT0pydo0/NjKdWE6AsCarO+L+omkhZgN3ysMKaE5Rhyg+IXhJnt1VWYuuIKfcYjqoxRmRZYzZSbSO5uE8CdVGOgifyhIZbfwChU13McPrjvzh7NScBvp7JvNhzFELhy6GXAip0EEF5OgXk2lR6re+SNIMcjPQXQK0So5xmAdWFzDVYr1wRcNafvIvknFjXBmtgry6hfuacR2s+nVRIBqtQqMioP/PZbA517IQ9uLDUjl4LteLwKI2zEyOxiGpaxXybaIL5uk6jENm0Lgam0KUb1M0p3JkIp5fBBaYKCZiOXVbT8hA70Nv1L7xWZJP4aji5cKxhVrbQ91b/DxUypQsYpTBKpt6iRzx98TQ6MTohy71SNJyVCqB4h0aYisp8iIRUY/VoXDrKu9p1X6EnIuR1waw6N9vRHcPsAstUJ9PRiHluQsF9SAowiQTufqOvxQBtMkL+5ZLuV+VGtS+Wh32wump/WvxPWEURoYdRseWZS+v4+kZ/rxpfWiIfMF+5BcSwUpll0/hMpWYxeqLOvWO1HZyltFHogma4FWRnkdri5SnhgN/FzGNa325VRvmcrVGdKoohFopjbBFsbw7iPuEqGlAQ1tQEZNZPfmLH3CZ15wjky5rqSu+Yni4ODeKo0yPXRMOqJVLzn55ZmMz3i9vM3W05velvuFM+iRvb9/5Z6L2Dgp63t9kp3Bs6s6RtH689z4p/eKRNZ8BcbYL wsiHy1JQ 1Oe9Gp737urlKlKglNQCw26bIxudVXLXeeFUPKMf5/t1letGaBCP8cpGtDdIzCnACbKapL0UentpvGh+Nh68q2XRHBlLRcmliEwg0O50R8qHPopPPaaTSoVjmxhoZi6zqF/iXFi8+VGDJqQkcqsK6coSxoClitzLAbcTjuEdr433xHDLbWxg56s7eNGl/Ul0e2BX9s49MTQTjr3/iPDxjPFBzjERzxoTshun4Bl7i4hlbuB5mtG5lxOyd73mTICQAIZT2hFK4eczbd5oZiIxzovWmUFb9VseBqKSl+n6WOeGzQnkVUjfd2XiCcQ== 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: From: Brijesh Singh Add support to decrypt guest encrypted memory. These API interfaces can be used for example to dump VMCBs on SNP guest exit. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra [mdr: minor commit fixups] Signed-off-by: Michael Roth --- drivers/crypto/ccp/sev-dev.c | 32 ++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 19 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 096ba15d0740..3c8cd2d20016 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -2061,6 +2061,38 @@ int sev_guest_df_flush(int *error) } EXPORT_SYMBOL_GPL(sev_guest_df_flush); +int snp_guest_dbg_decrypt_page(u64 gctx_pfn, u64 src_pfn, u64 dst_pfn, int *error) +{ + struct sev_data_snp_dbg data = {0}; + struct sev_device *sev; + int ret; + + if (!psp_master || !psp_master->sev_data) + return -ENODEV; + + sev = psp_master->sev_data; + + if (!sev->snp_initialized) + return -EINVAL; + + data.gctx_paddr = sme_me_mask | (gctx_pfn << PAGE_SHIFT); + data.src_addr = sme_me_mask | (src_pfn << PAGE_SHIFT); + data.dst_addr = sme_me_mask | (dst_pfn << PAGE_SHIFT); + + /* The destination page must be in the firmware state. */ + if (rmp_mark_pages_firmware(data.dst_addr, 1, false)) + return -EIO; + + ret = sev_do_cmd(SEV_CMD_SNP_DBG_DECRYPT, &data, error); + + /* Restore the page state */ + if (snp_reclaim_pages(data.dst_addr, 1, false)) + ret = -EIO; + + return ret; +} +EXPORT_SYMBOL_GPL(snp_guest_dbg_decrypt_page); + static void sev_snp_certs_release(struct kref *kref) { struct sev_snp_certs *certs = container_of(kref, struct sev_snp_certs, kref); diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 1235eb3110cb..55f6dfc2580d 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -916,6 +916,20 @@ int sev_guest_decommission(struct sev_data_decommission *data, int *error); */ int sev_do_cmd(int cmd, void *data, int *psp_ret); +/** + * snp_guest_dbg_decrypt_page - perform SEV SNP_DBG_DECRYPT command + * + * @sev_ret: sev command return code + * + * Returns: + * 0 if the SEV successfully processed the command + * -%ENODEV if the SEV device is not available + * -%ENOTSUPP if the SEV does not support SEV + * -%ETIMEDOUT if the SEV command timed out + * -%EIO if the SEV returned a non-zero return code + */ +int snp_guest_dbg_decrypt_page(u64 gctx_pfn, u64 src_pfn, u64 dst_pfn, int *error); + void *psp_copy_user_blob(u64 uaddr, u32 len); void *snp_alloc_firmware_page(gfp_t mask); void snp_free_firmware_page(void *addr); @@ -946,6 +960,11 @@ sev_issue_cmd_external_user(struct file *filep, unsigned int id, void *data, int static inline void *psp_copy_user_blob(u64 __user uaddr, u32 len) { return ERR_PTR(-EINVAL); } +static inline int snp_guest_dbg_decrypt_page(u64 gctx_pfn, u64 src_pfn, u64 dst_pfn, int *error) +{ + return -ENODEV; +} + static inline void *snp_alloc_firmware_page(gfp_t mask) { return NULL;