From patchwork Mon Feb 20 18:37: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: 13146866 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 43913C6379F for ; Mon, 20 Feb 2023 18:47:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D4BA96B0071; Mon, 20 Feb 2023 13:47:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CFA746B0072; Mon, 20 Feb 2023 13:47:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BC2486B0073; Mon, 20 Feb 2023 13:47:50 -0500 (EST) 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 AEA646B0071 for ; Mon, 20 Feb 2023 13:47:50 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 6C097140602 for ; Mon, 20 Feb 2023 18:47:50 +0000 (UTC) X-FDA: 80488554300.26.08E3A85 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2044.outbound.protection.outlook.com [40.107.95.44]) by imf13.hostedemail.com (Postfix) with ESMTP id 7F17F2000E for ; Mon, 20 Feb 2023 18:47:47 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=o8OBNomi; spf=pass (imf13.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.95.44 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=1676918867; 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=rrQnaVQh2mcS503DV65lMv6uDTUoE4QTksfruqEgVic=; b=l3lbiIwnZFOo1dfqKCtqNhAP+Yr4ZQqt+SwLp/cO3T7uVq/nxwMFL6BcP5Ih36xvnSTB16 JtToPz11URuXq0ctdBqKSgvYM2CpCDUGgvtR6cSV+W+rpamQMZARzBWJXqVqY0ZEWuR98F +Vl4DBDToiktM7cBbMdNqITFdUlCyBs= ARC-Authentication-Results: i=2; imf13.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=o8OBNomi; spf=pass (imf13.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.95.44 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=1676918867; a=rsa-sha256; cv=pass; b=SeZJULnwY7TzVuFMCw2+lddUc9Ohn+ZHhw1qMrYJVH5/BuDaZyArWUqi5duvyek2bEt57e a0pAFmQBtXjOjMtprHSRpOqxA241lE6Ium1fRvJJy/htGx9NxKpdN5vQEWAZvkweuZ6gx8 4GccNiIoOLC9PKBYjAATDB8T4if0A20= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VMX2wOGkJlySpYru0yrtc8ipW4M9U7j+pjwE6eN/rBBf7XxyU7S73TZPZmNjrsxMDhrfiembiR6ENAzdQXh7OdTxR5HyQ4f72hcN7k7J84EJl/xNLW3sm5og8wN+DRJGXWjC0wp2CBtbswJ2AO650a+6KivuwdRm+Yh627mfwpIIYjunykMD0ixftOyGOYEqQOVl9n7AZmWKcDmo17Z7SpgI5fDqJheN8t5DgO4TiNpLcaWmyXfF0WWbSs5jeoGeeJPdqktj0WB1PD/cNeOc0rv0v+e7GCW2l9Vin2h4MAPwmoM1XQIv1VaAiNBk2Asn04MaBRa6K8BvzaJsfwcoww== 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=rrQnaVQh2mcS503DV65lMv6uDTUoE4QTksfruqEgVic=; b=PCoRF+bGEfwx+c9JYKmCWUZOZKsN3Nx1nn479TbMXSYoT7E/J0tlE2UNqlxai+u7CXYYp0AoQE81yhKNZlk+p33nhZQQXRqTCIhjfxJURPVixt/G7bVA//KIKNSNzhDP09f7CHgPmG3l6F+dwucaUgYk10rlQzEXaL27xBW7UvUzYhQ1HRdfYEcPvbXm2zV107Nu6/B/aY54WxCW8LsaXb7RHrqysW7SiOZV6MlJkmavVSDukrQpj3/tjXBLASkRWNys6m98UrKsFqy8wbdqxvMk1MGocfCjyhM+lTbn1AXno3T4f/Ci97IyJloYklFZT0s2ZWkbRyf31vVVga+2pw== 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=rrQnaVQh2mcS503DV65lMv6uDTUoE4QTksfruqEgVic=; b=o8OBNomizSWbxJ9cYw29Pe3QA2JSH1JVHiLDYBPK1XGHnsAbPHglnKY2+jTwJbWc835BIpYqSSJ3TAffDjwH7QkUs5g6+/uR4pt3EYXIugwpkxZGXzrFwg1XQLisPP5vBLOKw+Ecj/knJsD1b3Axw4r31cN/Q4pU4RPClVLaOII= Received: from CY5PR15CA0044.namprd15.prod.outlook.com (2603:10b6:930:1b::27) by MW3PR12MB4361.namprd12.prod.outlook.com (2603:10b6:303:5a::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20; Mon, 20 Feb 2023 18:47:44 +0000 Received: from CY4PEPF0000C966.namprd02.prod.outlook.com (2603:10b6:930:1b:cafe::fc) by CY5PR15CA0044.outlook.office365.com (2603:10b6:930:1b::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19 via Frontend Transport; Mon, 20 Feb 2023 18:47:44 +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 CY4PEPF0000C966.mail.protection.outlook.com (10.167.241.70) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:47: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.2375.34; Mon, 20 Feb 2023 12:47:43 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v8 01/56] KVM: x86: Add 'fault_is_private' x86 op Date: Mon, 20 Feb 2023 12:37:52 -0600 Message-ID: <20230220183847.59159-2-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000C966:EE_|MW3PR12MB4361:EE_ X-MS-Office365-Filtering-Correlation-Id: bb36044f-08b9-4aa1-a24d-08db1372f426 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: uYMAuGPQWb5w8UiI1X/BvhHTugnqZR7o/1jUDsOuXiX2wANlUfgr/UgE9wpIh4TJR1hewWn9bZo39BmfP3BAZVmBUmIaoRguyx+rQyda6D2xGzdFcPLvCq7Q6C7OTk8ZCbw0ZsdvjinM+R4SzRHfAXjsfesv1q++w7S5BSa6IrsRxMn3jngvH78xUWX4VU4/0ju+GpAEjhDPchN+uBpOU8rSqfborH9d3z0zSugLZatBns8V6T0JSmWCDehM/9cTF90EH831AbFz26OZOhBXV13ZTacIfS5kQNB7f9NK0i94Y86BEtQT5UNsXyS9dSJ8N21tmAYWzUTMjpzYXwddnDIGixi8KUA2AwlDBTzdzSEb6Nz95wOpg7GvDAiW8G5phUb7kmS85NMYMKaUdjpQMRPjvaftqrKJLop2VwuQtmmxpHjNAdoKLHPS0U+jVWoMRQ43Z5JWFop6Z5v8U8lJzXMjRkzYiNBKIVFZ8EghY79NzRWv4XH2jWdTBZN1f1YQneojfq6pi8B7iYU4zS7AgdgyQgshPlNZ9m9Uj7fx47lyuUJklkdeHjw2rjgwHw/BqIvPb/9PxIEHY4dmNTNw+y27EDpLhx16PsEmviSrk4EE9oGRyMmH8yEMHujnFbaBZmx+urJmasLkavCuT4CNW4S8cSdiU8Ee4XqeROmpLF7/6MTK9qB/6bKwpdmkm1FNDzEvLoUbx9Ua9ZiklQtRUrfxOjlc29Q4xP2W18aiSYg= 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:(13230025)(4636009)(396003)(376002)(346002)(136003)(39860400002)(451199018)(46966006)(36840700001)(40470700004)(5660300002)(8936002)(426003)(47076005)(36860700001)(83380400001)(40480700001)(40460700003)(86362001)(82740400003)(186003)(356005)(81166007)(336012)(316002)(82310400005)(54906003)(478600001)(4326008)(8676002)(41300700001)(70586007)(6916009)(2616005)(16526019)(1076003)(36756003)(6666004)(26005)(70206006)(2906002)(7416002)(7406005)(44832011)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:47:44.5335 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: bb36044f-08b9-4aa1-a24d-08db1372f426 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: CY4PEPF0000C966.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4361 X-Stat-Signature: comk3gf7nkyp8c19mhx6uh585z5keq6b X-Rspam-User: X-Rspamd-Queue-Id: 7F17F2000E X-Rspamd-Server: rspam06 X-HE-Tag: 1676918867-393881 X-HE-Meta: U2FsdGVkX18pGoVHTOhoJ7c7a45NPKg0T5Im/dFo4bIbcEK4cVFu6wAC0eHUXTXxSuXQXeCoZemtlSs+b0LAXvpGUf3AlKlSG3ePQem5ztwInwuVR6bqCMH7GjIXLHbIi+cG34nweDNhiDudVhFk+nBzdbhy/yA0vFlB22IlgQRxBxmD2xMcxw4ktu+qGESi+oOyPnDUS+hzmx+NpqsAr+Zm2sv/AzmKa1W8dNHTwVhm5cL1Ti4VA5tUg71CWr7YnE+7d4od1icuwjzbMNqF0Rsfe+srS0CnR3AiT0+WnNVfd8bf9FjejZbIL7qYW8FCG0VFkkLB/sz3K3Ytc+UeYiOEh2tk2iIKkEDBRumqjqq1B/cjsSSM0AkYoaZ2FBeJ0qCMIE0q1/DyQs+dqlNgxTYWafG6IhfRqIAKr6qGl90xb1EvMIL3DmkVupQLdGloBpCueNPk+Kl2kaBnzZkOi1JV6CmLMGZwDRE1MWqDFyzGBub6gWTp7a0UXGH7z1eqijnKRCSmlJJ1eGtkbpFL/Pg9z9+W7jQ2MM/BGq1HuK5Ls/6ZgtHsnUJOWTAYixoKWeX9R+tpPZJJV/VSGVuILbiMQxzXB38iaQhftDQv300/KG3z0BRSCDM3kIi+4b4X6xAOcQtG4leyOzcjYHw+ZYBy6Tl9sokHK97/2oo5+PGWdj38iZiA9NyrUNZ3161tRoOkq1TkFWP7OErTwAdFgeqBxxIKikjguVjrn/zrJsaUbg0mrXtmeiQYY9T/g0UBHB6O10s4bJfkGxRvmft5o1D1h0tVmErn1L3Dlr5vAEHEYn34f/g720YbVYUDHpukNv9gqIQxj2wKxa24Z9RN3TncCaTOjHRtk6gQ1BWxJmpq+IUO8mE3tlWRvIBqhfrqpP+keu7gZuST/ErC2T0ngj1NV8dtfVaGt8kZsQFQj0bGevTSfxQM5mhjMP9f6xGearwdfpPnNTqr45VAZJS p1D0KFUy dcgffLhbzQWi9VqUejPuc//5Oglc47FDkUYO9n5A1txDOXZNegyHo5NnI4ye+Jzz+69cMcHvPjn1tllG3UCVhJl7TRJB4cg2ui3P7irALNPTvP+GxsZeDKew1o3XHv0mh3MfkXjLd2cIqfOjvbnuUuABjzyxppzeoSTsqiD8Za85fMh3boJx9yw+ZbyOoeqER8nQAlXkX+Atn/rqSZ+Y0ERMyFr/XVot+xwVxHpY/W9xZTFZGisUe9SyPewWRiXdPgvgmGIMQoifEQunUzKmjvvKmzzL1s0lJ8ItmUUzRzHv4ElDLZp/pCYIE0vLK+YJMYBj72VeMpxtrvYemPwEjaGmoLgYb4i9ngbnIEYmh/s9KLof16JEU0OLniw== 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 callback is used by the KVM MMU to check whether a #NPF was for a private GPA or not. In some cases the full 64-bit error code for the #NPF will be needed to make this determination, so also update kvm_mmu_do_page_fault() to accept the full 64-bit value so it can be plumbed through to the callback. Signed-off-by: Michael Roth Signed-off-by: Isaku Yamahata Signed-off-by: Isaku Yamahata --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 3 +-- arch/x86/kvm/mmu/mmu_internal.h | 37 +++++++++++++++++++++++++++--- 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index 8dc345cc6318..72183da010b8 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -131,6 +131,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(fault_is_private); #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 e552374f2357..f856d689dda0 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1643,6 +1643,7 @@ struct kvm_x86_ops { void (*load_mmu_pgd)(struct kvm_vcpu *vcpu, hpa_t root_hpa, int root_level); + bool (*fault_is_private)(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault); bool (*has_wbinvd_exit)(void); diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index eda615f3951c..fb3f34b7391c 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -5724,8 +5724,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); 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 e642d431df4b..557a001210df 100644 --- a/arch/x86/kvm/mmu/mmu_internal.h +++ b/arch/x86/kvm/mmu/mmu_internal.h @@ -231,6 +231,37 @@ 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 (static_call(kvm_x86_fault_is_private)(kvm, gpa, err, &private_fault)) + 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(). @@ -262,11 +293,11 @@ enum { }; static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, - u32 err, bool prefetch) + u64 err, bool prefetch) { 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, @@ -280,7 +311,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 Feb 20 18:37: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: 13146905 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 216C5C6379F for ; Mon, 20 Feb 2023 18:51:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AFD466B0073; Mon, 20 Feb 2023 13:51:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AAD876B0075; Mon, 20 Feb 2023 13:51:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 929396B0078; Mon, 20 Feb 2023 13:51:41 -0500 (EST) 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 845B86B0073 for ; Mon, 20 Feb 2023 13:51:41 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 50816C0701 for ; Mon, 20 Feb 2023 18:51:41 +0000 (UTC) X-FDA: 80488564002.10.357A03C Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2082.outbound.protection.outlook.com [40.107.92.82]) by imf26.hostedemail.com (Postfix) with ESMTP id 5682D140011 for ; Mon, 20 Feb 2023 18:51:38 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="aTeGgX/2"; spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.82 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=1676919098; 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=3nl9ReAu52r8NoHSvqQtO/vhnozQrmzY3lpXAFSgGIA=; b=uypzxq+XGadqlmdILDtMXQtQjjM+G3T6qeD0O23blfPGgCV2ler1D5ragShOIlfs3B9VLf iApgaCo7/dxJPOntsqJehZsZ+u7rS2tdmER3/KxXxfUwdXc+f/vI8k67Oa4sHZR9ArneQV FBWQsJz/fDVO9E0px/EtFwjW4Q+qSLk= ARC-Authentication-Results: i=2; imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="aTeGgX/2"; spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.82 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=1676919098; a=rsa-sha256; cv=pass; b=VJhviTFXxlV/syqj75yTIDdJbLAVVxnAYGiJEXKw4MH/w8ps45bWKQgq5PhkBO1zSbP840 n0fwMfR3kwyB2DGSj9RFGNuUzt/N2X0BVgHhq7KtWHgA5Jp6gUg5v8575/AX8nPbavE+gZ kkKyau14L6aEo+HnkjScmF8lHySA3gw= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SBaFVvxGVzAxIuGvMR7RPhwVAi4M+8L+o6DsaKo5Wt+2sBwUUOeLOsfwm/uXVpCYcJiWOirpfm/F+X343772CAPMdpmG6jYR/EEOld423YP4znDXQ6J7HWlSpjfQSZVVa4BBiERkJ2iM/N/gKLCntCmWHbNMNVBrIgT945PWxWht3DppBLjSbuB2YFg+UYyOi/fy5F9IWQ0kvY2buFuJP1Pg7CmSo7nbU3jyoB38aAYgryTRKoAtNUOLy9villAo10EJpTRLM/2NITfh1FKBYiDD6VufeIS18DQB2y+CHj6Hr1VGJnzd0/r3Hg6q+uH/MJJ5WBqKFWyn0eqQxKZWXw== 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=3nl9ReAu52r8NoHSvqQtO/vhnozQrmzY3lpXAFSgGIA=; b=A4ICQ2Egw4E9h1FsFNSL85Xt2SOf0jSeUaWmzWMti0BhKtbbwi9SVtGBTpcVSs/8wgfLJItow86W2S+F3tIdNe7i/iEPGcjr3D7/CgvPtidItjacY7fYGYH7vgTyYhehgLG/idsII4RsM/mOixSUflL1TlNOt4k6JALkSb4hwJ/0LrP8DaMAsINh7tjcKZGUbdpSqoHlqcsXjMnLWHuSC831SVwUtLtZ5eSJMclHxBDYJ78o5upnLDe7hMNk0mf9r9Fnnja1FU/By/5ixUYQ1aG7XzAgDKuPKs+jxoKRTN5tbc5ML7rcRdjlAR2ioTqWcOlEoKUsDLC01zbYfx/rsw== 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=3nl9ReAu52r8NoHSvqQtO/vhnozQrmzY3lpXAFSgGIA=; b=aTeGgX/2/DFuWExk1bptM3xwtYiFj6A73o1BzlLo0jFSHja/si0L/kBUQQZJjzEp1p3F8m6Qj0YzTjpvXO7+hQVs8Lr+fO+T0jTGUYYcCVYrsK+zaHkq9W1htYSpylyxbtrJbfPd9dbplahT+xPTf2gP69ouFV8wzsyY79iuCYQ= Received: from DM5PR07CA0064.namprd07.prod.outlook.com (2603:10b6:4:ad::29) by BL1PR12MB5190.namprd12.prod.outlook.com (2603:10b6:208:31c::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6043.38; Mon, 20 Feb 2023 18:51:35 +0000 Received: from DM6NAM11FT014.eop-nam11.prod.protection.outlook.com (2603:10b6:4:ad:cafe::db) by DM5PR07CA0064.outlook.office365.com (2603:10b6:4:ad::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:51:35 +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 DM6NAM11FT014.mail.protection.outlook.com (10.13.173.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.19 via Frontend Transport; Mon, 20 Feb 2023 18:51:35 +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.2375.34; Mon, 20 Feb 2023 12:51:34 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v8 02/56] KVM: x86: Add 'update_mem_attr' x86 op Date: Mon, 20 Feb 2023 12:37:53 -0600 Message-ID: <20230220183847.59159-3-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT014:EE_|BL1PR12MB5190:EE_ X-MS-Office365-Filtering-Correlation-Id: 829f19a8-d636-4e6a-cf32-08db13737db1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 4sTWKOgJR4B/cQaq4zDslFBI0FW/VH/CBEeNBvUhe78h8cSEVm+KP+U2KnFQ0wURfOp5ivivfDU2eoAOdyY9hUCC4nlrJydjmxQwGAl0af36spOfJcawVmlki0V/IKU1K2N4ponknT7yJM4RXAv5jbPIASyDkJ6kNPk1ZND0zcLsMDirMqvh18V517BPGk9NhodxnxrTDo+Dk7/U5iBlH3deTRQ0l/O+OA9dJ5riRDiFb8WsbA82BFmJ9nkQLCOv9qaFQIqNXiVH+a4OGtIfvKJ/B+/UNT6kucfBx9gXXQFXPRxXd/pRGHhE7MVeMcy1+d/FmFJNjGxBGmnU92m3VbqZHLVq5EKaiGfD2Ih94m4Klbg42mBgwU4XTFQNFBbOZa9gVnUtMcUYHVn3m8LOlQc2g/9C+tB04Ej/ZwTUfrnsBijEg8AS+rpAvJrkpNwonyBMYV1pimtNAPU4s5coHsFUcZwA9d3Pn0nzxgjcLTi1wkgFV6GJhaZn4XQCvD3u0Zkfr4I7iQdNeX1bOamWuuOSjtBo3Z/1xUmASw+EBr0VKdzjRBsd0o9OXdpbgJyNH2Gs6Zol8h6lAhuci8nbPsh0jASMXms+AR10wFQUbeV5LbuR+ygjIGyT+y3G8/yhQA2B8CA/rQo1V+P7sPnUZ5tuUY9LjWKeyET/YtLC7TzDum2Di0hR08itnh0iTSO9WWruLy3MTcBYwqRnbQKksmCYpYZBCW0joQEhOJADLOc= 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:(13230025)(4636009)(376002)(39860400002)(396003)(136003)(346002)(451199018)(46966006)(40470700004)(36840700001)(8936002)(41300700001)(7406005)(70586007)(86362001)(5660300002)(36860700001)(7416002)(316002)(356005)(70206006)(8676002)(4326008)(36756003)(44832011)(6916009)(186003)(81166007)(336012)(82740400003)(40460700003)(2906002)(16526019)(2616005)(6666004)(26005)(40480700001)(426003)(82310400005)(47076005)(15650500001)(54906003)(1076003)(83380400001)(478600001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:51:35.3247 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 829f19a8-d636-4e6a-cf32-08db13737db1 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: DM6NAM11FT014.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5190 X-Rspamd-Queue-Id: 5682D140011 X-Stat-Signature: qiqx41imkff581ygggstxj4sss6eb5ek X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1676919098-383419 X-HE-Meta: U2FsdGVkX197m/i/TjT1u817DBej47fddRkS2z3MQOhq43GraGX4Yh+/qWgWuEm0B+uoacfRg1xzz+pcV6D9+X5uLXM9WfIdJByJ4tScxXKljg9yloH7JrVjwHCzN3le6WXEHgtunjvZHUOo8WmE6MtynyryfOVaN8O6OWQgT81MoRMRsUv06Y6IXtiIi60eJqZPYioGho3IJKoqMav3INnrXLo6OQlq0A8QoPyp7sf5LkyKNqBa3BHpyqm4IA3SWJow0Let9YsEwU5hhWwjd35M13QY8MVt1hGRycy5TTts4CYpk6MNo+a3kGcJ14RUMlkXhz/y8XbC8VSxeuUSD+8qUpW9DgslZs7kUG8sCoN+fm26u3HUwFgRBcFZyXn4EIASU+bAs2/i8zIarur3GUiGMZLQ62C2G8hs17J+vM17lQTAAhts9kP1NqfycSN90A/JdvWgsCIGSPjMU8ggmC9HsvS2wMN45dlmCd3yvnWmT1bWpf3/E3sG2vj8QSG2DjYxJgkscfA39hKmmJoYXba/UKTg+i/PwYXRgr108YYlnotnzbOs4Ke0yClUnxup1gwb+KEN+Hj+N6IVmlqu/jTskJjaLIK6i5kmnO7iypJaCh+3WxESakBrN56ek37Ho/8SrUtt/DLnLw0opgSp//wVfJtBzimVtveT5e/+rg9aL546RxskofUoflDqQBLxaeevFRXtYmcVUskWkpYYN4baP0oFH61Ndnfy238OmEPSmO+P6p6vhKghOs+0Agh3QL/6qSOa8QSfFSEet0N40lg6XU9Y+kzIOKonZKlfsn5io/UDxaOBpKRzAQE7kQIpcm0NnmIhpInCqV2fDggSKAm/J1SbqO9RaA7M3fbodsajcmDRV2OsX/lCE8fHG4EzCAzVERImBPKQmKhxmMbPKxHFr/5WLL6YJAPQqL/YCcPznHTyB7epigFe44qpldU3Gkyquj4wC86O/wLoa7w cWadDZYz I1/Fnuj2i+vbUmTZJ+Bn0s6kyLYRGfhNRT3Ya0J4Md01ftEcm1R3XFRwRoZVitLXedGRDZ84VfEYAW5pr6srBygHzRwY95SK2iLYUtj+csBntc5ghtlj7q9BJbRlUFm/SOKDGaJY9QE9FGU/b0djndX4f4pxRPJaPBPeL0JvC3S1RfcOdAv/DN6GgYMn7qqUEbYVhAkYmGhVT/UAAVK9vSr4j/JrHH+BFV9DqwPE6nkDV9LFZdkxvzgLj/COy0jtdBwNwCiLj440MDzN77+4qQ0Db7BNk/d+QdfrnYpEuRDkU9A4UkYe4XfUObRqt60AR2KhasHuf7CqT0GXf8eF/yXOEKby1NkNF5Ibf9+ffrrKXTt8tJHQhDR0DDQ== 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 callback will do any platform-specific handling needed for converting pages between shared/private. Signed-off-by: Michael Roth Signed-off-by: Isaku Yamahata --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 13 +++++++++++++ include/linux/kvm_host.h | 4 ++++ virt/kvm/kvm_main.c | 29 +++++++++++++++++++++++++++++ 5 files changed, 49 insertions(+) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index 72183da010b8..a8aaf532c2ab 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -132,6 +132,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(fault_is_private); +KVM_X86_OP_OPTIONAL_RET0(update_mem_attr) #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 f856d689dda0..2da3fb2d5d1b 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1644,6 +1644,8 @@ struct kvm_x86_ops { void (*load_mmu_pgd)(struct kvm_vcpu *vcpu, hpa_t root_hpa, int root_level); bool (*fault_is_private)(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault); + int (*update_mem_attr)(struct kvm_memory_slot *slot, unsigned int attr, + gfn_t start, gfn_t end); bool (*has_wbinvd_exit)(void); diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index fb3f34b7391c..053bd77bbf52 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -7251,4 +7251,17 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm, linfo_update_mixed(gfn, slot, level, mixed); } } + +void kvm_arch_post_set_memory_attributes(struct kvm *kvm, + struct kvm_memory_slot *slot, + unsigned long attrs, + gfn_t start, gfn_t end) +{ + int ret; + + ret = static_call(kvm_x86_update_mem_attr)(slot, attrs, start, end); + if (ret) + pr_warn_ratelimited("Failed to update GFN range 0x%llx-0x%llx with attributes 0x%lx. Ret: %d\n", + start, end, attrs, ret); +} #endif diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index fdc59479b3e2..d200b8f45583 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2330,6 +2330,10 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm, struct kvm_memory_slot *slot, unsigned long attrs, gfn_t start, gfn_t end); +void kvm_arch_post_set_memory_attributes(struct kvm *kvm, + struct kvm_memory_slot *slot, + unsigned long attrs, + gfn_t start, gfn_t end); static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) { diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index b68574ff6c30..8ec985f1c57d 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2561,6 +2561,32 @@ static void kvm_mem_attrs_changed(struct kvm *kvm, unsigned long attrs, kvm_flush_remote_tlbs(kvm); } +static void kvm_post_mem_attrs_changed(struct kvm *kvm, unsigned long attrs, + gfn_t start_orig, gfn_t end_orig) +{ + struct kvm_memory_slot *slot; + struct kvm_memslots *slots; + struct kvm_memslot_iter iter; + int i; + + for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) { + slots = __kvm_memslots(kvm, i); + + kvm_for_each_memslot_in_gfn_range(&iter, slots, start_orig, end_orig) { + gfn_t start, end; + + slot = iter.slot; + start = max(start_orig, slot->base_gfn); + end = min(end_orig, slot->base_gfn + slot->npages); + + if (start >= end) + continue; + + kvm_arch_post_set_memory_attributes(kvm, slot, attrs, start, end); + } + } +} + static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, struct kvm_memory_attributes *attrs) { @@ -2602,6 +2628,9 @@ static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, kvm_mmu_invalidate_end(kvm); KVM_MMU_UNLOCK(kvm); + if (i > start) + kvm_post_mem_attrs_changed(kvm, attrs->attributes, start, i); + mutex_unlock(&kvm->slots_lock); attrs->address = i << PAGE_SHIFT; From patchwork Mon Feb 20 18:37: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: 13146943 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 E8EF1C6379F for ; Mon, 20 Feb 2023 18:55:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8B13A6B0073; Mon, 20 Feb 2023 13:55:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 860AA6B0074; Mon, 20 Feb 2023 13:55:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 750396B007B; Mon, 20 Feb 2023 13:55:32 -0500 (EST) 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 680D46B0073 for ; Mon, 20 Feb 2023 13:55:32 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 3786116025A for ; Mon, 20 Feb 2023 18:55:32 +0000 (UTC) X-FDA: 80488573704.07.D5C39FD Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2042.outbound.protection.outlook.com [40.107.237.42]) by imf25.hostedemail.com (Postfix) with ESMTP id 3766FA0028 for ; Mon, 20 Feb 2023 18:55:29 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=MMw+vPhe; spf=pass (imf25.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.42 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=2; s=arc-20220608; d=hostedemail.com; t=1676919329; a=rsa-sha256; cv=pass; b=Ee6p3iVReUAzQ4Yaw44bqaP7zHO5xbk162Nhiha7WPvksnrPd9G5JrDdAlEfhTgsKrCOWK t0Ofh6DmtFo1FaQGSEgMYMWS6+mcxTEAgn08d37q8wft12rTHrPQP33ao1tIAIyViU79p/ cVN2zkUPQPlZo45k18nYI1ZysMtEttw= ARC-Authentication-Results: i=2; imf25.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=MMw+vPhe; spf=pass (imf25.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.42 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=1676919329; 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=M9Ew5tn5x1C1/p9qsujs1TZPHeenne1rQSYJ332sceQ=; b=xczcTH+GWoP7DVk2kAsuZMwy+8ttC9G2yBM7knQWqvsV0f7Dx3w78XokmIj/NRAC9VnVXR WPOj5Jsni0kKAZBNIswumgCHYO6W3SiO2Gd0YUQ1Bs/LBtRU3n8FDBhSavuBMty9zc+uq0 R9PavaJutjcyMFUn2TNcNKLwNJj8CB8= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cdMyIrtokde7rFpD3655Z/LXYE+bJT2m8GWWXohR3NHQ+tEAstjZ2dyveoadZzLi+NtU0vH07s8T9HzdYSEjZJM6LXAxH8J9DYknGr3Gryc4hr5v0YzsVUou3ntXPHVeQes87fi/OfIq7FdtfcYiTbJnaIT4Zr15QiLrIm/HfI/bZx2NABd13SeArO3Q+iRORIHp+ly1L45tVy8xZBRsAl5V1JDtctED2Lqe6eckw1A2fyeNXVzpIiQNKNq/KEfu+4s2AVaB0CKA0PNNLnUZbO57eOEJRCCmOWxuXZHk1B6tdEO2cSmIOMHv8i8hTM92HF+oLqNpt6LFfBy7yp0Znw== 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=M9Ew5tn5x1C1/p9qsujs1TZPHeenne1rQSYJ332sceQ=; b=XnSq6lr/2aKcQ8N5SMIxgW9jf+PWoJ1ZzODYGAwt/GRpfOeA0mH1cpRniPgocPsFG3udsunoS7H8ymQUBmX6b4Y6ICJNJpVLqMYRnw97x8jOHaOFcB1Rlkrth9QxoHjrS9ViRXXgUKtu+hc/QwhjJ9NI0TigIKh62spJyOnsIc1DRDN3sfDdaNP2ogguvI2ffr9NRlGMXpME3ZkKJi8Da7vSQtkdbbd3BW+fm83PXQz8iv5PkYjKeUUllc82OeNjKq4/1P0TLCVaCwKT8LWHyqJcId6DOaXMDN65OouMFCh8dFZH7sTtvmEG6iOobaZoKGrtdJKKylxjZICgIruJpw== 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=M9Ew5tn5x1C1/p9qsujs1TZPHeenne1rQSYJ332sceQ=; b=MMw+vPhe2vPLkRpE+yqtkkDNGw7KTNF1gjQnEQn1Gu7ZnLgsbL3Yu04IPk9F54rKXW9HfmZ+/D/WdUGJa/wkLCjCKcu5Wpv7S+ikPbdiuAUoMCIYYPdopqjoZv/MMYWTxVdNP24PdMmB0h92gbP3unqCzsYM5UBnASZ8TxoPr8Q= Received: from MN2PR14CA0013.namprd14.prod.outlook.com (2603:10b6:208:23e::18) by CH0PR12MB5170.namprd12.prod.outlook.com (2603:10b6:610:b9::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20; Mon, 20 Feb 2023 18:55:26 +0000 Received: from BL02EPF000108EA.namprd05.prod.outlook.com (2603:10b6:208:23e:cafe::d1) by MN2PR14CA0013.outlook.office365.com (2603:10b6:208:23e::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19 via Frontend Transport; Mon, 20 Feb 2023 18:55: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 BL02EPF000108EA.mail.protection.outlook.com (10.167.241.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:55: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.2375.34; Mon, 20 Feb 2023 12:55:25 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v8 03/56] KVM: x86: Add platform hooks for private memory invalidations Date: Mon, 20 Feb 2023 12:37:54 -0600 Message-ID: <20230220183847.59159-4-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF000108EA:EE_|CH0PR12MB5170:EE_ X-MS-Office365-Filtering-Correlation-Id: 8fc5272c-1559-4dfb-7e7c-08db13740739 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 8Qw9KZVx53ga4s5Te7QdTZHnBA6xKPdCOp27MFFqX8P5SVkI24nIq2zsoFpZ5ouqQJ5uNLFncqaLMLWXOGCXdqZ+FsvKINC6T2G3/aCagX+ByKvta3xm1tVPMkmBpCD/3on/KbXzn4uW/sLPTOc/VC4gGBfwI0OO7ogHx9I5J2K9zdjrc5qSEsB5qvwP40I6EfSpWS2Xy/jlLkCfJVUDaH5HEMKy7cWfEGdlhaVmehEKoQNLvQ2KGQE3CXkVbf+dF2PCEbLqulo8qFBaAps/dAeLM5a/ZptDhoMYm3joh31hDP48Fbn+H0uCNmrSUZ27VOgp5GCjhIvOTgVAmyFRyDu7bOb5OOkmLph9N1kGwWjk6M7YzLKfkBfHOw6SsachueF17hqalsxFe9elU7FFUtRLKsPb31zsKchxS1MHnBxKNjwrc4pPG3I2HfNbc8Y7jO2LJiCcCwn3uVjnp/0YkUnFxJwOQtKO92cDpxleoG7ot+o9SSq/hlPgeZX33lXcQW+RpKa25gWjAr4ti+BZtmD53Nx8vUI4ENrpGi7V99TBLroNsdyn9Cl9PofGyA7CpMijIT74ZEb7ROFvvw9GfMWfmcGc92CmG/+/gTV7UClOanyH3R8gzXUTAmRCcr2i9DkFDvdGF6cnauD9moDQruu5fv6KKRaIDo9X/FC6962Em5lAXqC917il3EbbUinP/bM1DkIBUf78Hs4PjNvOapHdxvDANK0PwBull5gvd8A= 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:(13230025)(4636009)(346002)(376002)(136003)(396003)(39860400002)(451199018)(40470700004)(46966006)(36840700001)(81166007)(40480700001)(356005)(36860700001)(82740400003)(83380400001)(426003)(336012)(40460700003)(2616005)(86362001)(47076005)(316002)(54906003)(7406005)(4326008)(36756003)(8676002)(6916009)(5660300002)(82310400005)(8936002)(7416002)(478600001)(70586007)(44832011)(41300700001)(70206006)(26005)(16526019)(6666004)(2906002)(186003)(1076003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:55:26.0462 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 8fc5272c-1559-4dfb-7e7c-08db13740739 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: BL02EPF000108EA.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5170 X-Rspam-User: X-Rspamd-Queue-Id: 3766FA0028 X-Rspamd-Server: rspam01 X-Stat-Signature: htiftcr5rkd5uu34b9tjfcfhum8fhoun X-HE-Tag: 1676919329-424905 X-HE-Meta: U2FsdGVkX18D39bZxWX51yMudjnLl2GM8g6VIRoTnJ9vmzFQ20ZCyrShj5UAfsBTdYaDWDJ0A7HqI8oP4oqIuSrh6ZL0P1/GzqWg7nEbj5bRwwOJdSodwc2KvZVKIjmVecL+hzNzo5k+9W9TzC1ynLTUpOH2vN8ecj0dpRd3FT4L7r+Z077DagfuPBt3JI0ljN0iVN1jbCivv/hCViHLCJSkq4+mwABQpSTYUTHBNBAOHaXppBoawwsxr3kUYeVtWmg9Ir40KVdgyBhTBkZvCJ0vNpcmYQIKbKRd4zaDm5btBA0UE33UltLwtHqcHDxY5mBUenAWB6QqK2bxM1rVZUbN/3vYQG/K+CYgM7S6l65g+0V2juv6sDsnqiDdvQpG2dQXFxunvOqe2xRoD0431XBapW12egZM7oMLr4z0oRmO6sI+8twcE1dniXuBEp68t1a5oUmTsr7ClF0Zl6Gy95aMJPUpKoDxNeNEUEylNWF74XoC7CMbuYAov0vTaeV23DMiIMnbYzsmevFLWNxzWPw6eeifL0z26i7bnvw7/ixQfbVAKj/jOea7LxuQU4ZB/g035GRgmrKCB3665+f/44EV/Oq50VtILWZ2qUYkW7PYTofl+zJvSwfa2nf5QQU8mNj9A8Wveb6Xc+Pr8sk6ACgpAvnfH0LuI0auZyEkllSn3B1G93EB24Oc0qiQOwUA7in3fizrLkLDNuhA442ewaFYuomG66GvJUwXU2lkGB8Q3Fm3LenI+LDCtDBnNnOr6+B/gRGxAA5AvjWqTuIjJNJyvX2E75LM9fjNVGSiedXtELZPAViLqwE36gR9er9jc6fe4OLtLUnAy3aWEZdM70fv/4sQDKW6NULwJzyz7rAeN+W0N7dCNrOjY9ZZgJtZxSSzlC0s8HsK/Xu6x55mJ2UkC3c2gC9cAY7WX9p1Z+LTKEIVsE1m+49V4vg3cM9dadq7Po6qenfWiQC10Hg gVB3mUKU 6FYj3rt2NP+1YTIz7/9pJSAxAU4hduUBMDs1SF2k2ChYabufd3ddGHNEaFGFMsA/mwyIlC/q6brHapGjNw34guiA3P3tEHF6tlvnKM+Y1EMXjg5IY2wasqIEeggUldN+c/4JwGjIziGxqAgzMdoB8kjmHi5SMX9en2B6fD6gHZUMwZtwyDGoC9RhU19x2r8lKUx1ffyGJjgEkN+CDF1EYTsES8olZrpbYbO5exolcgLEYtuEY41Tgx+YEq6k5zWrL/AXp1X1sBVB40qm1EwebOjDu7L9nU3AvHs6CIS7K9eSjVzerk6ry4zfPXmILsyv1Y4gx+4RWrxoBVFw= 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: 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 to the invalidation notifiers for restricted memory. Also issue invalidations of all allocated pages during notifier/memslot unbinding so that the pages are not left in an unusable state when they eventually get freed back to the host upon FD release. 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/mmu/mmu.c | 5 +++++ include/linux/kvm_host.h | 3 +++ mm/restrictedmem.c | 12 +++++++++++- virt/kvm/kvm_main.c | 12 +++++++++++- 6 files changed, 32 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index a8aaf532c2ab..6a885f024a00 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(vcpu_deliver_sipi_vector) KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons); KVM_X86_OP_OPTIONAL_RET0(fault_is_private); KVM_X86_OP_OPTIONAL_RET0(update_mem_attr) +KVM_X86_OP_OPTIONAL(invalidate_restricted_mem) #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 2da3fb2d5d1b..37c92412035f 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1646,6 +1646,7 @@ struct kvm_x86_ops { bool (*fault_is_private)(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault); int (*update_mem_attr)(struct kvm_memory_slot *slot, unsigned int attr, gfn_t start, gfn_t end); + void (*invalidate_restricted_mem)(struct kvm_memory_slot *slot, gfn_t start, gfn_t end); bool (*has_wbinvd_exit)(void); diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 053bd77bbf52..360af0c9997e 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -7264,4 +7264,9 @@ void kvm_arch_post_set_memory_attributes(struct kvm *kvm, pr_warn_ratelimited("Failed to update GFN range 0x%llx-0x%llx with attributes 0x%lx. Ret: %d\n", start, end, attrs, ret); } + +void kvm_arch_invalidate_restricted_mem(struct kvm_memory_slot *slot, gfn_t start, gfn_t end) +{ + static_call_cond(kvm_x86_invalidate_restricted_mem)(slot, start, end); +} #endif diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index d200b8f45583..4d542060cd93 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2341,6 +2341,9 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE; } + +void kvm_arch_invalidate_restricted_mem(struct kvm_memory_slot *slot, gfn_t start, gfn_t end); + #else static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) { diff --git a/mm/restrictedmem.c b/mm/restrictedmem.c index fd6f3c66033f..c8353c592cfe 100644 --- a/mm/restrictedmem.c +++ b/mm/restrictedmem.c @@ -17,7 +17,7 @@ struct restrictedmem { static int restrictedmem_release(struct inode *inode, struct file *file) { - struct restrictedmem *rm = inode->i_mapping->private_data; + struct restrictedmem *rm = file->f_mapping->private_data; xa_destroy(&rm->bindings); fput(rm->memfd); @@ -305,10 +305,20 @@ void restrictedmem_unbind(struct file *file, pgoff_t start, pgoff_t end, struct restrictedmem_notifier *notifier) { struct restrictedmem *rm = file->f_mapping->private_data; + unsigned long index; + pr_debug("%s: unregistering notifier, invalidating page offsets 0x%lx-0x%lx\n", + __func__, start, end); down_write(&rm->lock); + + xa_for_each_range(&rm->bindings, index, notifier, start, end) + notifier->ops->invalidate_start(notifier, start, end); + xa_for_each_range(&rm->bindings, index, notifier, start, end) + notifier->ops->invalidate_end(notifier, start, end); + xa_store_range(&rm->bindings, start, end, NULL, GFP_KERNEL); synchronize_rcu(); + up_write(&rm->lock); } EXPORT_SYMBOL_GPL(restrictedmem_unbind); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 8ec985f1c57d..f7e00593cc5d 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -960,8 +960,15 @@ static void kvm_restrictedmem_invalidate_begin(struct restrictedmem_notifier *no struct kvm *kvm = slot->kvm; int idx; - if (restrictedmem_get_gfn_range(slot, start, end, &gfn_range)) + if (restrictedmem_get_gfn_range(slot, start, end, &gfn_range)) { + pr_debug("%s: Invalidation skipped, slot: %d, start: 0x%lx, end: 0x%lx, restrictedmem.index: 0x%lx\n", + __func__, slot->id, start, end, slot->restrictedmem.index); return; + } + + pr_debug("%s: slot: %d, start: 0x%lx, end: 0x%lx, restrictedmem.index: 0x%lx, gfn_start: 0x%llx, gfn_end: 0x%llx\n", + __func__, slot->id, start, end, slot->restrictedmem.index, gfn_range.start, + gfn_range.end); idx = srcu_read_lock(&kvm->srcu); KVM_MMU_LOCK(kvm); @@ -972,7 +979,10 @@ static void kvm_restrictedmem_invalidate_begin(struct restrictedmem_notifier *no kvm_flush_remote_tlbs(kvm); KVM_MMU_UNLOCK(kvm); + srcu_read_unlock(&kvm->srcu, idx); + + kvm_arch_invalidate_restricted_mem(slot, gfn_range.start, gfn_range.end); } static void kvm_restrictedmem_invalidate_end(struct restrictedmem_notifier *notifier, From patchwork Mon Feb 20 18:37: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: 13146977 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 28421C636D6 for ; Mon, 20 Feb 2023 18:59:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B937E6B007D; Mon, 20 Feb 2023 13:59:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B1D0A6B007E; Mon, 20 Feb 2023 13:59:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 996AF6B0080; Mon, 20 Feb 2023 13:59:25 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 8530F6B007D for ; Mon, 20 Feb 2023 13:59:25 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 634B2AAD16 for ; Mon, 20 Feb 2023 18:59:25 +0000 (UTC) X-FDA: 80488583490.25.7F7ABCE Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2047.outbound.protection.outlook.com [40.107.244.47]) by imf27.hostedemail.com (Postfix) with ESMTP id 4CC7140009 for ; Mon, 20 Feb 2023 18:59:22 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="E4/m7VpO"; spf=pass (imf27.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.47 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=1676919562; 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=6t/7MChFwUyyN5VUBnMXSkXPBAhKTwfDsPWfcTbxIpk=; b=zp0+2DIPrxf20jZakhhtKvIKam94R41aF/jAll0fNNARd9Oor0p5QU8raYp1XoqlUY+XhK XOhXjqBam5p/ord4zmQhtHpYX6yT4QbUIPa70XURKu+EaOXz2bosMtmRP8yqUb2sgyfWnk Majfw2g2AAaaBbbx7o5wwXkceZ/YqOI= ARC-Authentication-Results: i=2; imf27.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="E4/m7VpO"; spf=pass (imf27.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.47 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=1676919562; a=rsa-sha256; cv=pass; b=CsIigH2O7LG5QiJ0YWKJ18lGgMM57xekyksuvsN28j7+YmNWZiGE8f2faFPOKwyHQ0HW8t J/X4Wldtpu0N33K4o70HOToTVTUmFF1XV7Fv0DSTIZ1/Ty0eZP4aA/OuxJTmYihU7ajMLB YTA2N/a2DnqBGqh7LVR2KelMM+4b/5E= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YPitQX6gDSmqWlWvXdX2eJFfC+6cQ5XhdkL0cbdcgmS9tWyaWEFMGHICcnyFqhJ5GWUZqICPF58gIPFLQhtFaBJqryygL3H7myLsk2ZwtaaeMqs4+Q5YsNmKzN/OxNUwnbm5rBKp5CE8QtW1Rl27SUzxovUnOTWEuCqIRdkM8U7K22289tQfGIcIT+RF7ZwjZMGklhIu6CwHwZHrdGtcvZcC6s7b6CUGKitbcUq52gyhsIp3yRIS5PfMRfF4XCDTpoj0ButGgBS9YWd/TQW6WstY73KJ1FOsNZ8NeWHAtj4rwSrKL4fhmiXPzI2ynuCndPjTYPh9k53zpabX6Py3Xg== 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=6t/7MChFwUyyN5VUBnMXSkXPBAhKTwfDsPWfcTbxIpk=; b=oH85ROPDiZpiMMEQj1UjAroCMmHzxxcQ5PjE/CvArFQgqreMZTKjxYyn4AeAZuedzo2pZ+iG9nT9E0cRwprsw1gxTm7a6lIhdcm/qfO8mNKgwjKKuwW6c7u2fg7QFgXyWpf44ZGQAV0x3P7qlcufNLWt6UXb+7hfb84XjN+887KamBcZSXQs5efLunQZQJEI9tgyubwtsfKFpCluWAZuQv5lWhhNYdSJGXYT4JkivIwIQ7ST/YhBfTWwZ/32bed0N92f6H1fz8yRqy0ZnFKVf37YqpgkVRK9Gi0v/aGotUihGHnLRzM2RslK36Zq9VY0qAfc8qvCQ3oUcpwWh5kDuQ== 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=6t/7MChFwUyyN5VUBnMXSkXPBAhKTwfDsPWfcTbxIpk=; b=E4/m7VpOW/Y7wDCHcV89YgAv/D9G8DparRV5J+1NqjbiFxvjOeRnRavQFrwFe6d54kDjmKUoWzQrBnU/znqEC6y2Us+wRJpb/hL29mAwnc99TJ/dx9CMT/Pda0owReCzryZvVtwer0wjFiEmE4W5+2J6Rqgq4oEUA17br1dNN88= Received: from MW4PR03CA0277.namprd03.prod.outlook.com (2603:10b6:303:b5::12) by DM4PR12MB5376.namprd12.prod.outlook.com (2603:10b6:5:39f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20; Mon, 20 Feb 2023 18:59:17 +0000 Received: from CO1NAM11FT080.eop-nam11.prod.protection.outlook.com (2603:10b6:303:b5:cafe::1e) by MW4PR03CA0277.outlook.office365.com (2603:10b6:303:b5::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:59: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 CO1NAM11FT080.mail.protection.outlook.com (10.13.174.99) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.19 via Frontend Transport; Mon, 20 Feb 2023 18:59:17 +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.2375.34; Mon, 20 Feb 2023 12:59:16 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Vishal Annapurve Subject: [PATCH RFC v8 04/56] KVM: Add HVA range operator Date: Mon, 20 Feb 2023 12:37:55 -0600 Message-ID: <20230220183847.59159-5-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT080:EE_|DM4PR12MB5376:EE_ X-MS-Office365-Filtering-Correlation-Id: 7c479bb9-eb77-4ba0-f8c2-08db13749115 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hETg4WBeDpvBCTOlLB3Sjxsx0OINKjiUCj/grgUiBMpB3ZSzcgCuSSXjU7gNJv3G36sA3gEWsqvZhKYbuaiO1tK/lsZQe2hmab9rsWR0MM2gCA/cTM5+dYrAbRCUTH+qRCkQJ5R8IIq7dLlMf3lKPzL/lZmfZIAQv6faQ0aYDMdOhjBds+4aIe7dQALDb5Ge8k9iypWjYBpfNNNFIsxTpfLCKpt5MkssniaoHFmf/FeD5ePuC2hw1AOwGnOLgonWfOf/op+HswbS4Rt7V1xfrr3I5eCH9BvPE0+oN5Y2F39O315MGWYU6ljKgSDKgEm7LUc/lNeRG4ZRiltPabOfz9pnNZSZq8h7qrFoSVktXKVCHvjdP2DBOmNuJXf56kNxpfdkT1NYlczGsD+ksO1K2V2/+cYrunxlzp3o4VmgQAtZOsGPNBz/Yb8AVa+QY7fhxOCblpGG2BGmXU5FR+Z03sxPcm3RbHa+ObdHi5uGGejCg946K0OFNlxiyEqiMzjgMexmJfgGKOsealqB0pR7+5xakXODUU2+jHKeOaNSLLnUxiIkBzlPgXBsYJk0TLWwMf5xmakuz8lQ4Cd37/MsYTGkuv1y9fBEjMCPLKNjO3kJ37ngH+7UtEwwos7T9YvS8AGIJ3WbsRuZSOiV40cuWN/M5d2KJunCjJgsrtE4qw7dClIUWD0P9K1CTD7Mq2TxFtKi7/YgQUd1WEp2RjQC2RyZ7EpdyZ+EdWRB87sLsFA= 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:(13230025)(4636009)(396003)(136003)(346002)(39860400002)(376002)(451199018)(40470700004)(46966006)(36840700001)(36860700001)(356005)(82740400003)(86362001)(36756003)(81166007)(7406005)(7416002)(5660300002)(6916009)(2906002)(4326008)(8936002)(8676002)(70586007)(70206006)(40480700001)(41300700001)(1076003)(82310400005)(2616005)(186003)(26005)(83380400001)(16526019)(336012)(47076005)(426003)(40460700003)(54906003)(316002)(44832011)(478600001)(6666004)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:59:17.2608 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7c479bb9-eb77-4ba0-f8c2-08db13749115 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: CO1NAM11FT080.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5376 X-Rspamd-Queue-Id: 4CC7140009 X-Stat-Signature: sg9yrkffwesz51n6ckhzitxofrctqpno X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1676919562-643507 X-HE-Meta: U2FsdGVkX1/aa4dz21qIbok77AOBZi3kpr5IAYXrjo2rSYkWb5F17OH8s1WDssxDaTaF9K52ud0xTP1FctmOr0/KcefpKmHOcWHq4DsugoTWXHynp44c1cdAQWaJYFBGyaQQGDqIjo2UjGYafbaLumHQ7nQMfcE2/0kY0UlyXOSHz3kKrgGUj5youQMYISJSG0RY+w213QXHB3d1q8k9xw3t5xBJde3EBSFs3RkdPDTup9C01jNf/WKOKmmnL26x79yiQpvtB3/PJVonirGinAD3G9BlODm64GBjNo2HcxKl9n6HevFuSFOHRGNgKV4ta6xUHVOl7+BpyWFos0+uw/n4yuPAD3IDGWWuiTA+2Of2QgC2PYSr4ohfYhvCJNv6cfSPl5cPtWWfRHTGVUu/ycvlNZF4MSYjVaMfkyRFlVjfo8zGcQewIat/7sYm01/5Ggfh7P/fpKskyCa8B9Hd58VKlWio9KcD+HeAcx4mVSYlHLTUjyAWtUqjyCKSuKgBUHALC+NrG8BQ342UpCgzjkbKuitR3fLlfxTT5P+p2dWy4pzkc9eU6EqBRPNA9AfJawqn1kg/cZHntGelTziqobmKBUNP3wsPZgDdsVze04cxyxOnjdSOJSGZs+1icg5WMTMGJ/VTwhGWWlF0UYHcDNFleLqBlT2cdMosQri1HrhAmJhGDze5x29qsk/6XyZioYKan+ChAf9i36CeDGvUvK3MU4geBobT6v2N1d72U2jPgi5+tsyBVhJmBS5M+BWue2yV9UdK6kBtScpdaQhLYzfuuG17pgv/+dSL0ja/AHTPtvi/qd4Nf1fyNKsEWFHAUKuv4FWdkb5442Z4ibYtXIQLgWvUfJGuHWVIQcp/f+394w9sdOXphQpDoSz5uku8kV+8frPjqydSS0GFCz7OSVEJdGeNptiJdiX3lXJaID9ocTtf1b5ax5A6yzZy+2maAbldUI/K1auO38710e4 Og0VB6Dt LeTnO82w4bpR025d354asbTgZAmfRPTMtjPlkwbxi30cur6zd39VM53BC2zbZRPZbo5396VLMd9IWe/7xSVvuRq80VqU2fbh/bXEs+UojKt4eKjbRr523uSwW18++OjsB3By0b7IVHAI27xd9tC/6fE+tAJh/yQQH9K+rlQoEjryvXzdJJUkdGmfQpBLzjjUN8bjWEsrfaXXcN6vvdoIjidumUAD0W5iEIZmevgQJ4HNO03JbWw/eSEjH0WIdmHZCivABzjL1+EXZemW+0nYRFm3pA4FQg0o2zrw7KBNpkHvPudNC30mA+dyqJc1+EyEXad6pfbduXT+l8IbszokZoi9cVR7ttzOK8OUX 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 | 48 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 4d542060cd93..c615650ed256 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1402,6 +1402,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 f7e00593cc5d..4ccd655dd5af 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -642,6 +642,54 @@ 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; +} + static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn, unsigned long start, unsigned long end, From patchwork Mon Feb 20 18:37: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: 13146984 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 4CB35C6379F for ; Mon, 20 Feb 2023 19:01:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DDC036B0098; Mon, 20 Feb 2023 14:01:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D8BF26B0099; Mon, 20 Feb 2023 14:01:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C2CB56B009A; Mon, 20 Feb 2023 14:01:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id B21C36B0098 for ; Mon, 20 Feb 2023 14:01:50 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 92C6B1602DA for ; Mon, 20 Feb 2023 19:01:50 +0000 (UTC) X-FDA: 80488589580.27.7EED60D Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2040.outbound.protection.outlook.com [40.107.96.40]) by imf27.hostedemail.com (Postfix) with ESMTP id 8AA8440022 for ; Mon, 20 Feb 2023 19:01:47 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Cb2UsjgQ; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf27.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.96.40 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=1676919707; 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=TUy6W8vUVLW4RDBTDQXfJAfbbqMreiqOl2cG+jRqR+Y=; b=fVWcpYoCRbwOPT+zLeZZdE8sz21G5rSIg17FvpShsTXoH8pM8vbv5ifUsT3xGu6TeNo4v9 kNjUEGzSUtAewtR5+cAt+3o++ArT/nDaBLZWvURrGR1YCt4Ehvk8guYDct7wLg2mzYDR8Q 6CEy0AeAipo+qlNIW8fA4EuGLwmOX/s= ARC-Authentication-Results: i=2; imf27.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Cb2UsjgQ; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf27.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.96.40 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=1676919707; a=rsa-sha256; cv=pass; b=QImXbhhUWyXKLu5fGWoK4+3nBI0Uiy4kyfHTaYXX6hl8/w8c1vnXucNGUjtEnWVP6wse1e 29UfWgOxfaleiI0R4gaXft6f3HBPBnYS+3OhpOy+h/8/kaVMPe1s6CtSd9xAa363RpBUcR UdvvDe9q6lgY9kdu5RrRU0Bm1a9Bn6M= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=n3QeESdQSv0mkXhHnGxlbaCVMgRnDuNnPvZoRgi7DS9ufUcPAHgHRErXCeX1Zz5aHqQnPhv7t3aQUCcF4byUsdZtNHkbKpZjhXlJ6OZJgIpNcGzeLQ3e9cSFCtWRuNh3h+dZ12rxYNcy4AMzRy25aF1KVdkyCHjMIO8u49Jrag6YSe8AXa22UOS62NB4NKQmn6JG76F7G1lWyFYT7rwMYhovz/rZxCDIxJ1WekDnTsf0SpRjJ5CZ8XJZR8mDTaFc8b3dVZFWti2wwnM4PL8p9TaPK59HNamGd8/Y0i9T+I2sPYIEsZm1DZS4VoJHdzdu3lvvOyx36S70mZyMa8tbfA== 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=TUy6W8vUVLW4RDBTDQXfJAfbbqMreiqOl2cG+jRqR+Y=; b=mer7Kli3WQ5EXhGJ3Okgjt3cMExdqH79+fUYOmT9+lr7JRLCBnI2G1olqWnPX8Dkci3Q0wlRiNS3pfGSa5bQ/hfBpkQ1F9MPb1FE8sBFz361cyUx54hj4keMnrb6al/H6zFP2DKmWhMK5iN/lVQuX9zRZxSEFDjmmJ6Omot6eprdPTYmKmTOKiRmGl3l3Qk8NTQ8H3OwJZc8OwZyQNU1tzbhJkhpy6g7osn32c/1gdUy+SQBDfoJGDH0di8dQ3CPGZmPNj1WJByKrbtnQqyCQb58j64mDy/MFXH4MbkHhBJTZHa6EMTzFc9D58iQIwSoTMC9X+UzX5ME7fBFFVnixw== 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=TUy6W8vUVLW4RDBTDQXfJAfbbqMreiqOl2cG+jRqR+Y=; b=Cb2UsjgQ/RBWvqQt5eI2XM4BxnS/AIHnHdkTDUBDAVDC7sRNKu4e2mFowcrzuleu7m0FqjywzJmYLP9VaYKa5OSnsXuiVhpC9Zyx6ulYIQBz+GzXe9yINMDO3+Dy5rsHfvHZ922LNLHKuS6BhgjeWw9ZfSb8au27UPSr3jatGn8= Received: from MW4PR04CA0330.namprd04.prod.outlook.com (2603:10b6:303:82::35) by DM4PR12MB5214.namprd12.prod.outlook.com (2603:10b6:5:395::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19; Mon, 20 Feb 2023 19:01:44 +0000 Received: from CO1NAM11FT027.eop-nam11.prod.protection.outlook.com (2603:10b6:303:82:cafe::3b) by MW4PR04CA0330.outlook.office365.com (2603:10b6:303:82::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19 via Frontend Transport; Mon, 20 Feb 2023 19:01:44 +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 CO1NAM11FT027.mail.protection.outlook.com (10.13.174.224) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 19:01: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.2375.34; Mon, 20 Feb 2023 13:01:43 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v8 05/56] KVM: SEV: Require KVM_PROTECTED_VM when AMD_MEM_ENCRYPT is enabled Date: Mon, 20 Feb 2023 12:37:56 -0600 Message-ID: <20230220183847.59159-6-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT027:EE_|DM4PR12MB5214:EE_ X-MS-Office365-Filtering-Correlation-Id: 3eb1b2ac-50fa-4e08-c53d-08db1374e8ef X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xojPAxrcNBewpydI4YiEKDyFbZhtWPhJ1J22Sh1ig6V+Y5PkLYDtldjbkMH4Xe9ML1HTqIP8gO9mkBRYFsAOpfFdld5jl6gDdIjcNhV+MtWbp8IKy5C0iCPMuoIj/2+Ii1jDHy/DHkza/pnyR0S00pKNj2/aExFEfBB5m4083ZXOI6mQPwyK8DEe/XTpW76wBwX7sHZhlHmmri12maIr2sJh/e1EjXqszbb9kUxZsRAjPprkCpnQF6ps40FpvBvgdUtLeVYAqlY1mhS42mbKKqaXmTnpOMZ/61NRnoroLY5x84O4qmBpOdUQ0fGMD+d7sc3Vc90V+9pYPGyANoqKdlCVGcZ2c15eZ3g8mTv2LFxVUoFEw+gA2I4E9zc77mgSl9iD7Xhig8hcJPavW+RMU5tPrSNyVwoeTnKWFy+Md3hHuxWM01+IgtsZTJ2D/OMz6SqYY3QW7LblahO7MP7WZerkKt4oaymju6yauG7Votk9/1w+EL4U49lwdXIhA3FzCU6oNtYlXA74vKL0keJZG3nfRvcMU4Qa9LmGzU1pmYZuaGlsTW5Vjyhpqhb72nQPKfjU6nmWnx7OpHiJHmGvYiNw0IgM1isd/YaK/ye9qCenATKgzYTIRlKuC9ByS/TR+IgJ72M3vXkagT3PJmIqFxKT2HIxYCUsp3UJqafokL68CkgNShJKR/ZCWOEeGw0MHUOyU0bCtXHjl5lkSgbQW+Hd710sYQuaUmu/D5silUw= 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:(13230025)(4636009)(396003)(376002)(136003)(346002)(39860400002)(451199018)(40470700004)(46966006)(36840700001)(36860700001)(5660300002)(44832011)(7416002)(4744005)(7406005)(82310400005)(86362001)(82740400003)(2906002)(336012)(81166007)(40480700001)(36756003)(478600001)(186003)(26005)(47076005)(16526019)(426003)(40460700003)(356005)(316002)(70586007)(54906003)(4326008)(41300700001)(1076003)(70206006)(6666004)(8676002)(6916009)(8936002)(2616005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 19:01:44.6812 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3eb1b2ac-50fa-4e08-c53d-08db1374e8ef 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: CO1NAM11FT027.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5214 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 8AA8440022 X-Stat-Signature: qak1jf9hi7m1a1a1hj47jgkphwxk9nmm X-HE-Tag: 1676919707-646914 X-HE-Meta: U2FsdGVkX19x5ds2Psn01icbTYKmiDUrhr0b8Myv8s6nCzrbJtFb6klFO8jw4OFuzcEppTpkvTMs/yaglh6I5BMd55/6ltehXgyl5UK1AFBt+R5YHA502hdOwU3w6AC7w35zOzz5ICQlnkSF7ywLxGMoalUvWwS4p/Dl6ezZPaQOM4tvjwjDGP9WqJ8zxtGSwfRiP7py12C7RhhP2+bi+2K8zLdM8Ro+0wczdpjIHZ1iGeGH0l09eFvJATxQQIiS2TDanKsfodxfbykD0Fynpb/rwYeTjALp9vICFLygQc3T5jVDouJd6WyWwp7aKoXABaCoy5RvVTZgvhOiYbBvzElJgPwFId9W1iqW0hFS9bjY4yZLMJvCZB/g+Faso7sy/Q/FmtgVEJkLUgEFcSHbLtkfdiN0J4948A8yJgNgXqSVQdsEvYCtxWGGfdx+2ivJlM0qKyLtRIiFabvBnKkvzrfvIxpIPkZAOuW1sYwLMm0fg/MHh2ko79J3WyKFnkckR/XOyb3jVwJ61TzYGuNaFV2fo9qJ5pYtZFhEHIakVLkti0MZqGnDp86VqOO1R+4GkApvf/I0qTPV/45sb92z7OuDZcTP7A1K1EX/JVr6p6SeY7H3KUG78VnG3HmfxHpng7d64lKW6Ddkn1g71jtHwWpmZSv+p6efwHwi5xoj75IjaeByUQRmktxnHqBceebyg57pVUh7Bua6wjHx4VVP/LVO2CO5C0owFfwkZw/TdNYtPl6GbdtsWwTT0nWhPenocSvLyBn0yGEl4RngTGP2NNBYvJar+5hmVqhYpP0WPbbp10PUvP3Kg/2rp+dJnsLdqqgf8nP5WfPJJrrKOGmqLo4x1UItJmqZbgC000IvxD2ZW73DUGeAVu25PoL1XJgdZ8Hm9rsen9T0oxdnz78IYYTO19HIO2F5N2UNZiBtZGepa4M58moPSMdV2CjZEiD23sXHqc4zix8aq0oEvhJ gn6tapKz JtjEs0lS3MPw92/4nUcltQBGPSJT8TdaW90BPhgoNDKLg0hfKL0G+XPxiWRcznR4KqwYY0CFZDhKyERprA0k7rAPeyFL2xqhtMCqbl87Q4SojLWFXSZqmHEvURtMWbN5jmgJ/vAApNUDjAfpX0vL7yFwnN+jt7H5UurkxG15WzKPJ/eKce8+OZ9iVnp8AcAdwayJuhGgO/BGpR7H/mHiE99Y8O2AlZkoIqjHQswsaKoDYmk9VkynnyAbWy6k6honhmsnNFAexWIhzb8/hOtERmSVY/q4hIRHZMh3hJib97GHnDeNSsRTnr48UhCBGu0Z0WAmaJ52IOfjS6Cvax61t1XhS+tRd96nDKxGRAvUzYmxXz27Fuv7GsP03Pg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000003, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: AMD_MEM_ENCRYPT implies SEV support, which now relies on support provided by the KVM_PROTECTED_VM config option. An argument can be made that SEV running in non-protected-VM-mode is still possible, and so this should be configurable, but AMD_MEM_ENCRYPT will also imply SEV-SNP, for which KVM_PROTECTED_VM is required in all cases. Signed-off-by: Michael Roth --- arch/x86/Kconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 67745ceab0db..f0d8f6bbc1a7 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -1546,6 +1546,7 @@ config AMD_MEM_ENCRYPT select INSTRUCTION_DECODER select ARCH_HAS_CC_PLATFORM select X86_MEM_ENCRYPT + select KVM_PROTECTED_VM help Say yes to enable support for the encryption of system memory. This requires an AMD processor that supports Secure Memory From patchwork Mon Feb 20 18:37: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: 13146985 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 600C4C6379F for ; Mon, 20 Feb 2023 19:02:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 06E926B009A; Mon, 20 Feb 2023 14:02:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 01E136B009B; Mon, 20 Feb 2023 14:02:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E01916B009C; Mon, 20 Feb 2023 14:02:12 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id D18626B009A for ; Mon, 20 Feb 2023 14:02:12 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 9DA951C5E76 for ; Mon, 20 Feb 2023 19:02:12 +0000 (UTC) X-FDA: 80488590504.11.1799130 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02on2083.outbound.protection.outlook.com [40.107.212.83]) by imf18.hostedemail.com (Postfix) with ESMTP id 5F2481C001B for ; Mon, 20 Feb 2023 19:02:08 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Ab2OZ72F; spf=pass (imf18.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.212.83 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=2; s=arc-20220608; d=hostedemail.com; t=1676919729; a=rsa-sha256; cv=pass; b=q8/vNbCSJWM9goWGoRkri/qva8fccrStzT5vJrMUvm6G730BLiJOBLFJTtFc7ENl35O6/p 3FxHehnEULiGzRovfDXQzV7ZKVCIJmPJdbXtNP5m8Lje+3YyiMGbD8e2eQioXU2Cc+IMyb Y3R5HSb2fHih/qcF2IzLMftyvH0pXGc= ARC-Authentication-Results: i=2; imf18.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Ab2OZ72F; spf=pass (imf18.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.212.83 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=1676919729; 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=q3nf73drwaLvEnfy8UVO8myJaOVWglmANxn8btDUER8=; b=PJ6uaScqXqQbTy1ZF6pUltqOZqaloUbFTMYf+G/C5UNlNyY8j7H6KrYey5dA44RWXTO/Wf x+jMjaONorNcvQVtpTEgvsN0TMVTOZDpRdvo9Zv81G36q+F5OMZ4nNEtH9cJUvs+qPFapT f12cShElo8uFgkj1In5A/d0KFHZmzL0= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LesiwY9wpa7r9Ap9qkVzdAFkPEdizHTsmMxq3aK1eu828F2kXo0o3ewj1RJH0M9HLfXAvJaiGRk8hfWQj5YydvJNrxBgy5wf+KsNdIUpIQxyIfMTRj+amNiEeouSK7yD+KaYpjvfYQwbMXkXMbiGqD9vd1z031Ku0kqJSIuoP3n3u11bd4Di+4qWgZyDRf08mAu2CiCbefDzfMSTvzpZ4RdEwub06yvzr19yuWvXX81sil51ToiAmeMRSSE2WWVZ2QvnJr8I2P/1u6Iopau0ytUCB3CGvTVewDXxBHpMNLFGoVOT5RzExec5tsneFMm0serFOOsBomHB1m+rHa+cPg== 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=q3nf73drwaLvEnfy8UVO8myJaOVWglmANxn8btDUER8=; b=XVY9hpW8EB1ty4unAKiA0XxKpFtjQ3xaF8gyPTlvdvRXjpxti1baRE/eGk8wNZweg2K1O1SX7N0CxoVPBGnie8DlEohUw1WjIgtbh8IdMTNCIEZFlr5dxEH/77TN6y++2mWWAuyqfzhCfgaR+FIJfYSlNcHWcsOlI7wf4V89GbGEo6wakBxyJJzHZF97HejfWRYhMfl/cgIn0O36bFiwSJYLF7+d/Ad8jcwQkMYT6I2sfP19U6tmpV7mf+3AvSMSpLsdB78EUwI6ebDdrwAhlCRWrt3W6PlIkB0i9gk1OrAhrD2vSTiS2f0qouTBdqfAvN34pxuewco+kG/JqhZjSA== 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=q3nf73drwaLvEnfy8UVO8myJaOVWglmANxn8btDUER8=; b=Ab2OZ72FdQ/JPayIcGtzQZD2blDZZRubTdMYMjF3xGOtBrVszkTU2sQOguah3bwqANVU+4854QSSkR61WeL+U3ukbxtCiV0zb8qfCnS2UBpuUund0usm+DTecBSTf2jlQ7rfugFGDztSyQbHxb3u3g/ticBc6l6CXg5Lfy8e6Bs= Received: from MW4PR04CA0298.namprd04.prod.outlook.com (2603:10b6:303:89::33) by SJ2PR12MB8009.namprd12.prod.outlook.com (2603:10b6:a03:4c7::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.18; Mon, 20 Feb 2023 19:02:05 +0000 Received: from CO1NAM11FT080.eop-nam11.prod.protection.outlook.com (2603:10b6:303:89:cafe::8c) by MW4PR04CA0298.outlook.office365.com (2603:10b6:303:89::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 19:02: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 CO1NAM11FT080.mail.protection.outlook.com (10.13.174.99) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.19 via Frontend Transport; Mon, 20 Feb 2023 19:02: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.2375.34; Mon, 20 Feb 2023 13:02:04 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v8 06/56] KVM: Split out memory attribute xarray updates to helper function Date: Mon, 20 Feb 2023 12:37:57 -0600 Message-ID: <20230220183847.59159-7-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT080:EE_|SJ2PR12MB8009:EE_ X-MS-Office365-Filtering-Correlation-Id: 5eaa5440-d3bc-470f-2352-08db1374f541 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ZFMXZt6POvfTIKz07tkXAkp6GtKZCxbDSIK5Alq+8AsCTBxdZyvAQIK7+btA9m+IDdZez5NdfUYpxAzE1+xKXQFYGlfnD3jdVyGmguJ/l3V0MkHWDCy1UTr6iRJj3lJ1oqZIxxsPCdqO+g24RqVPRiaosD91MTDb15UdKAwEwaKdlZmOrcyJ9c4e1RAax2CxGF/rPZl2mD7wU3ko5p0ufzPOmZSmBOmjmodIKtNoAS9pJ2gm6tgD38lFEgmUzcndZN5fxEaUntQ2bkg5/v1P1Llycrm5vlz+IJ6Byr9yZpnO51zrmyv0BFpT5JLojH/0FbQmjOgRKaqKmsUcSCzbO9jJHMIBHDF90XWfrrMvTN1kd1eKLt6BwkxxBsDeuV3WaaRs9353fly5rk6x1mqv+t0e7zB72dZxgnTjeGf0UiXiiYJCHnWatIY5aUCoMC4OnxiDIpYXJv8wydWWrLKHiGuVo8dVhMolFKJ+NpgA9OOedv5YvFH14G8Xg9kBx1OZvAcL6JOC2vgRf45r0jSXMOwg3BXSWtzJHsyzlXxFu9aAUrHjWzz1TK+TVdAqHbKyoiRMPZZPfw7il9Z433V+7W62bFtF+dMfO1QBoJFFQLvD1luPo9tP2QF9GPN5/GWLIJOau+VYci0jaDwgJN9AzAz45H6kbsrV24d6RcTFYFp5dzmvUBY3jKhu8MDZWHTZ+4zL51hyb6IN7roE4m+rz619qrPJciNC+208fz1RgbM= 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:(13230025)(4636009)(136003)(346002)(396003)(39860400002)(376002)(451199018)(36840700001)(46966006)(40470700004)(86362001)(54906003)(8936002)(7406005)(36860700001)(336012)(426003)(41300700001)(2616005)(316002)(7416002)(70586007)(2906002)(70206006)(5660300002)(44832011)(36756003)(81166007)(15650500001)(6916009)(8676002)(4326008)(356005)(478600001)(16526019)(6666004)(40480700001)(26005)(186003)(1076003)(40460700003)(47076005)(82310400005)(82740400003)(83380400001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 19:02:05.3471 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 5eaa5440-d3bc-470f-2352-08db1374f541 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: CO1NAM11FT080.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB8009 X-Rspam-User: X-Rspamd-Queue-Id: 5F2481C001B X-Rspamd-Server: rspam01 X-Stat-Signature: qw7qjzcbupqenhjw1nh41b8xwnni5384 X-HE-Tag: 1676919728-841767 X-HE-Meta: U2FsdGVkX1/SpLsVigsmJfGHPALrH1U1zDTEU3lMT9f32zy7+A/zqrm5Oub8nhKGZDcjCLmayEOl8bsNLg9e9U/8wuKwMlgcdJGW+H1v+GiU9zA4/d0SRILsdKhkx2AVnw33LgAFEucHiUgohiv2mQ5RrBki4oxeZTZt355oJ8kKvq7t9urnhytV3wKGoC6bIB9pcv4K7wM3jltB763xU0GgTqXNnByKCf1KF2ON6RvWYtCoijImVPFOOV9sX5zFIDMG/srx6uX+Wl0ghDvlxCIZtfnoN9eW+5/zLo4rAOuAMmQy8Ao28TJ1/CIevzZx10l8Ez4m3v8IDatIS1hCJ8RF7J60+m+IZ+t3CM1wpJLgEpltVcX5gJ8ean0QrxzktFIPy95UqaJuQhmgDLDCd1uV5RYXDqc1/31mZa2bpqtacgLylMOMGyTb0W/4mzrGdSHXBwDXo+7S/FVNHj16EyN85NAfUdTrp/fWgLr6DodeqWmAXXcKzyNYJoNsrjtUc9ckhicNvewRQpWW2W/ukDFVLzr3+XHm3F0rmRFBbg319P7K0glkVJU5TnCNOHJNDAFcjYu435SEGrsu08tsx1jKbbT90zwPiBLMJ052emPH6pLtkaD1fwttO0g6zAwC42PvnuIevmsMmRyEAg6R7yPWNQjT4WzZssY7WAI6dxwxu1mgLiHH97AVAK43Q5gopiAD7KKH9DY+TWafxPvcPwmxpUsAI5E4HcDt8lJk8UkmLcUpf/kaKf/2xTnSfpz4CGbaKUM279xlnk7WJJ3cqzJ94LCUAN1Awoq+Rgh7QYt/14NVGLVN9U72i9Pc7NT01F/nDHjdqabddooGk76cInfabIGBIHt2rDjnlVWEmnv36t4MmjSC7t4yxs0Ei1MyrEPhmwvf1QJ9cBpxW3tlvvQXmuC+PnOSIeF5jR/igu6p4jJBm6U6gMoptltFQHcX2HDJTxLK489UqNg+F5K j4pup/ha uk6wkBTFqmsnz+T7K1vKvZJ0Ns2s50/T3h0fmmLOIXJRRzzMQmsETyk/DmGniiGwcuPJkbfdq37drWF54QBVnSEsM0gceGU4+uZsTXg1IFXzTuiJ2m6pwK1ShJ+vKCPL0Gnuk6G/JmcCOxKIoPLepx39m4D+JxavpjwiAKml7p9M064y5Z6wrIeY59U0b3Y+uZncnqPSWRQD3HnyAnr5k2DJrOY2B6ZbMKDG0JpSFa81w9m+Gl8bDEZsAuoNFpMztX4K+U2iYqo9z2mHWQBmfYz22+J1cfahsehKDv3N1JwSF1zQTfJuGzo/dUg1bmBvG4wkS5JNsiv39P5JXuYHCYYDqGqrdRAQf6AB71oVjlbx0Ie8= 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 c615650ed256..57d56cd09a61 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -993,6 +993,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 4ccd655dd5af..c740b56d6ba4 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2645,12 +2645,28 @@ static void kvm_post_mem_attrs_changed(struct kvm *kvm, unsigned long attrs, } } +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) @@ -2665,8 +2681,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); @@ -2674,11 +2688,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 Feb 20 18:37: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: 13146986 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 A24FBC636D6 for ; Mon, 20 Feb 2023 19:02:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 418796B009C; Mon, 20 Feb 2023 14:02:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3C76C6B009D; Mon, 20 Feb 2023 14:02:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2684E6B009E; Mon, 20 Feb 2023 14:02:32 -0500 (EST) 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 172226B009C for ; Mon, 20 Feb 2023 14:02:32 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id A31794090F for ; Mon, 20 Feb 2023 19:02:31 +0000 (UTC) X-FDA: 80488591302.05.681F735 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2059.outbound.protection.outlook.com [40.107.102.59]) by imf28.hostedemail.com (Postfix) with ESMTP id 973D8C0016 for ; Mon, 20 Feb 2023 19:02:28 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=rplaPNrE; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf28.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.102.59 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=1676919748; 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=21G95oFZzHWgiSnbQKm59oJ3QLRud3+Mni9EVFmud3w=; b=RCzmoJwkX27eU1sFNWMPwgD+/4sm/Kr2mIRVM1vT1z6jgpbVu0ITc9id+7lMEC7otH3xwE a4vUCC1+/mmWDs5qwWX/yieQ7J1cQcq0X2NbcyC/pQh9O530e1ASl6MM1a73Xpimzk9e6r 9/xzEGa53KcilIgIWPzZUZfgnYMyhDw= ARC-Authentication-Results: i=2; imf28.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=rplaPNrE; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf28.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.102.59 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1676919748; a=rsa-sha256; cv=pass; b=PWpZ1hdyWzv8iLLk6b95PekoAd4QKvJ9eAwd2x0WHx3Ypgf5yuEv7xZkcbFdiTf62U0eRl j64PGEEmuY0epMtNmmeWc5aHBzn+6p/p64pe4CSaLEOq0yHiEyZiuIxLJ5XniissBw1Igz jptlBgkyIS4HYC+xE7yCPabN9TKDdbY= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HUETiSK42d5gUhv1x4apCytrxOfnTFmbPFjWCjBRcrROksGqyjBFh1OXwpQtCnHJXhbLWKRHe4q9VFyHVxKIBpOLucSYfMSLDtEZCnnPt0BGGj5RZm1i74nkKZlmhpKdxZX42brQj2dktfivKGEfKJJVE4BkLl7mDN13M0cXQAC3SrueU+oPYa6jGX27z0EWi2F3lGXI1MEq5H1wbMp+zPFqISLjhfTnsS50IY+FsVdjyIuTJhL/X/AjSkyJBRiM74bwZfh7ZGng4oZQpQjaWKxrnqjzx32HYq8FoCYHQLSuFUjS7pDClZzlP4m0WYMch/BTFP2hnwucMJ6wTD26IA== 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=21G95oFZzHWgiSnbQKm59oJ3QLRud3+Mni9EVFmud3w=; b=PUX54sXYzUagm42NfcR8f7cmELGg5ruCRSY1dSKL/UCNsgWgbF5D7b/bbSl55QFWUGXhEjDNZ4Ns//U1Crfwbp010ROBR1aenVaWLxIjRSWivp1Zq1Guw47QTPgWATPnu+fh1SUERGf/w2D51VQCFa0upJ2sa7UqhdeRFpqImnyCAypVGfM1HxnGbbJ0fY/68cLrmsZIrSFbzz/F3JL5X3aN37P80+F7wbEuTTfdIiPQHYCALAU1xQwdokcWBVL9ae9EVuTEtxbbO2f1eipGam4fzwYGlcFWid5pBNufMW+jvqY7+/yUpjSJ1ka8EgKiDL/SoWgH++7FYYIrqFgM1g== 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=21G95oFZzHWgiSnbQKm59oJ3QLRud3+Mni9EVFmud3w=; b=rplaPNrE2ZI6utZeW8e8B3qeLI7B/6ZKj5M7aN/vh3NjfJILvzyzynfeTKdqO1qzpQdR6sfPndhEJ0YWLpUxBzAqQLKlka5LEArBq6gguG3e8zVV3vMiIybm7Kqsfn5s7F/FtaWFNzI+KaCLooAU8YRniO4XaR/fFEGfYG32PFU= Received: from MW4PR03CA0035.namprd03.prod.outlook.com (2603:10b6:303:8e::10) by DS0PR12MB8561.namprd12.prod.outlook.com (2603:10b6:8:166::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20; Mon, 20 Feb 2023 19:02:26 +0000 Received: from CO1NAM11FT035.eop-nam11.prod.protection.outlook.com (2603:10b6:303:8e:cafe::ab) by MW4PR03CA0035.outlook.office365.com (2603:10b6:303:8e::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 19:02: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 CO1NAM11FT035.mail.protection.outlook.com (10.13.175.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.21 via Frontend Transport; Mon, 20 Feb 2023 19:02: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.2375.34; Mon, 20 Feb 2023 13:02:25 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Vishal Annapurve Subject: [PATCH RFC v8 07/56] KVM: SEV: Populate private memory fd during LAUNCH_UPDATE_DATA Date: Mon, 20 Feb 2023 12:37:58 -0600 Message-ID: <20230220183847.59159-8-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT035:EE_|DS0PR12MB8561:EE_ X-MS-Office365-Filtering-Correlation-Id: 607fe171-2f6f-4e08-2f07-08db137501ba X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JWmjgonN+G9HW7svEnDa+LBz4VSTz+Ske3mLX5GhlexJf5QsJNP1+6oR6nWqAN9PTz1fPYocoD8X8mkfeUDcz51lN5xoFM2XUXQA5o4clVN6QbyNjlkx2Kibx/of6UYXpoiNFaRcizQ+LoHpJKP1ObH+RzqYG4DTMj1JBxFjamZ0J5d4fajXHOMKNOrokXq0+21Sy1UVgaj66VK5Mo0s7T/MqyOKH3xgwT6EvFBui/dLteroIu/LRMco1uFGcJrUz4xCxkCPW2mhatRzr64WeAkLLZYh7wzNTQ+m2rWpmxP1ZmN2tTx5T8xmypA+ZrsmN+RK3NvY/jGGMpsnFLelx97HxgUALE9J8a0IEGhtnbWhLZWhf9RRy1UXnp7GS2xIToXfrwmkJP3J61NlZ06KStdkNzDwP59dUpNMK4pq81/p8xuz/BfVW0HGHOxyLdowZ5jQlfXCSkOoQq3D3rhaGMTOAcpB4quKD8Beue9c8xKYNwxQ4AvnFJyIG/yW/1GHp6CNMxC6mO+/AXBf6toSjIrz+kcAk8Iw7fHSZzpXOcdvG8DWug9ijSGZyL55ZNj6umxtiKhs01UnvQ+64thTrc8PqKvt4wuTW+mxz2Y+JyvvXgypTzSWitcmVVtZT1na7PGI9kl9Ac/s8QrAfDPWbtkw1vMvSk8RpoDINqGaGAkkIoZUXMGxc+sDaip6eJrhT2R1ItyN2dsTZQOppRlll5HI6jZNT+J23OshgT4zCdE= 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:(13230025)(4636009)(346002)(396003)(39860400002)(136003)(376002)(451199018)(36840700001)(46966006)(40470700004)(356005)(36756003)(40480700001)(40460700003)(86362001)(82310400005)(47076005)(426003)(336012)(83380400001)(316002)(478600001)(54906003)(1076003)(6666004)(16526019)(26005)(186003)(2616005)(7416002)(44832011)(36860700001)(7406005)(5660300002)(82740400003)(81166007)(66899018)(4326008)(2906002)(8676002)(41300700001)(6916009)(8936002)(70586007)(70206006)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 19:02:26.2418 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 607fe171-2f6f-4e08-2f07-08db137501ba 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: CO1NAM11FT035.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8561 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 973D8C0016 X-Rspam-User: X-Stat-Signature: hgkac166p8mx4upx3hsggha7ji8iobfm X-HE-Tag: 1676919748-429062 X-HE-Meta: U2FsdGVkX1+Ms557r8GvnMYxTjP4uMps56QU2u/ea+/ZrUTwOGpZql06OWtkPSnB55IJw/QYqEaH+lbgQLbxTyyezaAxNqXk9jn3gl/3BLLkhXDDZcEgsOCld8uuD/36R7isqD1UriVAwjTn8Znns79PMZ2bPhiFsUfrzi8lHh21t2T+2n6RYq+XJeR65MkcHuceqvztWimaexeXHuroA/pLPXeCaWuoSqGg49g6FtMEPJIWP8LXqs+bQrL/7FJyt8z58eee/nJKajhZ/vBoerQnRZ22EtLWxRQxU44WbwukJ9HLCRln8q4cYHd0OeMJRGpIyApv2HYoRpSplz5/aTv6JwOdxA77gSaAG/zWwgAStjnA8bppErMmq/Ja2MkXLnkmWeafZa7Mm2HkccosD7LhjM2a7AGWLL3gHYZaZcHMO1kKoONwcee06SdK4vDTd9OntyZKPa2JxTd3NOHd1aI9MVS3l/NPW8DQ1lQf1wA6CULEBC9vm+2rwaB8jALrnjyg4RFzr+HYur158ms/hmJ5Bm+4PfQAIrcKM2tkLsWxTutkq1VcmESJck+hPFxQiHXv0KV3OAHEbS6n6ScddrU2FjZPskzWMgsr703iP7PomF17k/97Lil6YPcGzPJqKVM+tj3rsOqn50eSSQ6F6IzH9yTyMObCkbSMewjWt7dXzbH0dPrnmsq82Ti0npR1WnT8CTB+ZrfbW2XdIYsFFSlYArHbhrMYCx0wH/5lmr2aUObpCn/WxqP1g7hYqNzhBTaLqTb8bBsm7sIqPHYuXAla6oI+fLmCsmVe92loMrDTqlbvh/N8shIJOXrAACRvUrXPGHGuc5AIHY7pBU51CPJCtkZgCkjITI+Nb1Rpzb2GC/3sltIGXb5WDtz+7xIbJIUdYBMRS6sQ9n9rW4Cm8tl1gqC1mQlbqqh7MqgOUBoybGUbMeaPgd9fDftXFwPxRwJcykCWMQEsN1P9L/y 6dqRNxOh HSH3LRDCXnxD/VNp4b1rwnt84uBug7XgI8FfNsStLNBgHg+zW8QkneObPjVBC3JIycVidv7D0sFYSH9GLACi+cGkIGZoqLrwOLwlh5BwTSLyBWOS8eAZVxMqSAXbgQGWOfjPX89m88MgtkkhL2xdVtMI+t/CmFGx7VmRgpPAD+lGqlTCtJoYHU2mALjMzG6qX8mp/My9rslSIxmQSK4uYsaxLzS/Un3tiYNcW5bGeS3LTtesI8JrandQjqkgwOYS+fFtnF1DiJ3M+Sba62tXbsGass5movGBZ9aOnJmT8oSwRMhccJCdWdgWbgVKXfzF8CN9UdObRioENv2RShwLB085Ga1A3Z3k0rVYln3IEtj9MpkwG66+RGQhWt7aAxH6OzeC0XsrVhdy122U= 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 This change adds handling of HVA ranges to copy contents to private memory while doing sev launch update data. mem_attr array is updated during LAUNCH_UPDATE_DATA to ensure that encrypted memory is marked as private. Signed-off-by: Vishal Annapurve [mdr: Use gfn_to_hva_memslot_prot() for shared GFN handler to deal with read-only slots for ROMs. Split kvm_vm_set_region_attr into separate patch.] Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 103 +++++++++++++++++++++++++++++++++++++---- virt/kvm/kvm_main.c | 2 + 2 files changed, 96 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 273cba809328..fad7fb34ef9e 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -494,23 +494,26 @@ static unsigned long get_num_contig_pages(unsigned long idx, return pages; } -static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) +static int sev_launch_update_shared_gfn_handler(struct kvm *kvm, + struct kvm_gfn_range *range, + struct kvm_sev_cmd *argp) { unsigned long vaddr, vaddr_end, next_vaddr, npages, pages, size, i; struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; - struct kvm_sev_launch_update_data params; struct sev_data_launch_update_data data; struct page **inpages; int ret; - if (!sev_guest(kvm)) - return -ENOTTY; - - if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) - return -EFAULT; + vaddr = gfn_to_hva_memslot_prot(range->slot, range->start, NULL); + pr_debug("%s: shared GFN: %llx, slot.id: %d, slot.base_gfn: %llx, slot.userspace_addr: %lx, slot.flags: %x, vaddr: %lx\n", + __func__, range->start, range->slot->id, range->slot->base_gfn, + range->slot->userspace_addr, range->slot->flags, vaddr); + if (kvm_is_error_hva(vaddr)) { + pr_err("vaddr is erroneous 0x%lx\n", vaddr); + return -EINVAL; + } - vaddr = params.uaddr; - size = params.len; + size = (range->end - range->start) << PAGE_SHIFT; vaddr_end = vaddr + size; /* Lock the user memory. */ @@ -562,6 +565,88 @@ static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) return ret; } +static int sev_launch_update_priv_gfn_handler(struct kvm *kvm, + struct kvm_gfn_range *range, + struct kvm_sev_cmd *argp) +{ + struct sev_data_launch_update_data data; + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + gfn_t gfn; + kvm_pfn_t pfn; + struct kvm_memory_slot *memslot = range->slot; + int ret = 0; + + data.reserved = 0; + data.handle = sev->handle; + + for (gfn = range->start; gfn < range->end; gfn++) { + int order; + void *kvaddr; + + ret = kvm_restrictedmem_get_pfn(memslot, gfn, &pfn, &order); + if (ret) + goto e_ret; + + kvaddr = pfn_to_kaddr(pfn); + if (!virt_addr_valid(kvaddr)) { + pr_debug("%s: Invalid kvaddr 0x%llx\n", __func__, (uint64_t)kvaddr); + ret = -EINVAL; + goto e_ret; + } + + ret = kvm_read_guest_page(kvm, gfn, kvaddr, 0, PAGE_SIZE); + if (ret) { + pr_debug("%s: Guest read failed 0x%x\n", __func__, ret); + goto e_ret; + } + + if (!cpu_feature_enabled(X86_FEATURE_SME_COHERENT)) + clflush_cache_range(kvaddr, PAGE_SIZE); + + data.len = PAGE_SIZE; + data.address = __sme_set(pfn << PAGE_SHIFT); + ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, &data, &argp->error); + if (ret) + goto e_ret; + kvm_release_pfn_clean(pfn); + } + + /* + * 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_ret: + return ret; +} + +static int sev_launch_update_gfn_handler(struct kvm *kvm, struct kvm_gfn_range *range, + void *data) +{ + struct kvm_sev_cmd *argp = (struct kvm_sev_cmd *)data; + + if (kvm_slot_can_be_private(range->slot)) + return sev_launch_update_priv_gfn_handler(kvm, range, argp); + + return sev_launch_update_shared_gfn_handler(kvm, range, argp); +} + +static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_launch_update_data params; + + if (!sev_guest(kvm)) + return -ENOTTY; + + 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, + sev_launch_update_gfn_handler, argp); +} + static int sev_es_sync_vmsa(struct vcpu_svm *svm) { struct sev_es_save_area *save = svm->sev_es.vmsa; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index c740b56d6ba4..003cb199ba4b 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -689,6 +689,7 @@ int kvm_vm_do_hva_range_op(struct kvm *kvm, unsigned long hva_start, 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, @@ -2850,6 +2851,7 @@ unsigned long gfn_to_hva_memslot_prot(struct kvm_memory_slot *slot, return hva; } +EXPORT_SYMBOL_GPL(gfn_to_hva_memslot_prot); unsigned long gfn_to_hva_prot(struct kvm *kvm, gfn_t gfn, bool *writable) { From patchwork Mon Feb 20 18:37: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: 13146987 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 63D1AC6379F for ; Mon, 20 Feb 2023 19:02:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 06EF86B009E; Mon, 20 Feb 2023 14:02:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 01D2D6B009F; Mon, 20 Feb 2023 14:02:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E00FB6B00A0; Mon, 20 Feb 2023 14:02:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id D0F726B009E for ; Mon, 20 Feb 2023 14:02:54 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 8A13FC02D6 for ; Mon, 20 Feb 2023 19:02:54 +0000 (UTC) X-FDA: 80488592268.11.4940598 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2049.outbound.protection.outlook.com [40.107.93.49]) by imf07.hostedemail.com (Postfix) with ESMTP id 6A77D40020 for ; Mon, 20 Feb 2023 19:02:50 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=gcl8KJLB; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf07.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.49 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=1676919770; 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=zs2dlCK3kpBp+DHzyao4L2xJnXTs0oWXM++0nWxwyeo=; b=uAx7yd36Mao7B+5jUyhEsqlLAteRhBtbTpcgkVsrN4oQJVa5ZIRaVE6CTnEQ9o7tBMhi+S wsoo4W+fkAIBSZqomfZgMZ0Az5w7fzr14Wfv13ABLd6o20J6NnkbE8xJ25BlNWHaOaVukE UrbEB/SK4D7j+kzvFed01ZeCLzcmaF0= ARC-Authentication-Results: i=2; imf07.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=gcl8KJLB; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf07.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.49 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=1676919770; a=rsa-sha256; cv=pass; b=YLZmSgb/870wMaoA2RR6f61CGCXBTa2Sa/kLAAMoKDk/jeYtdA9gaqXuTVAxnL6TDK4qgj ddIW076L7uGl/bt/RzcyzJVUzQmpjqM2MiXfqxPdP34jvgmzyN3C1r3BBqnj6zh0INR++I EcaUVO6NbtAgV706gwY+XcMMpN30dMM= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ksXqVaG6dcqy+bSUh2sWfOqFJmPxKJttm1ZQ8SF1v0vlBTBp58gfU8gPX61KFPljORHCPoWSXFYdjY8KVg1ERCdPj+0fbEKs5DssgLYRLsixzj+cMmtLa6oBYaM76bHU+bFK5JweU/qJ/WgzIaKvSftbiNe18J2aZGd0mXnjTn2ao3xSF93idSAZhFxr8bW7+gG6sr2O3j1X8VjjSlg3TLGdPJXBaa3NPqF61RQ+/pAlqybTrFmHCarXntqf2O+Qt4vfDOGp/h4SVXeMtD5n6TDkpjoz/ALW9BZXOAwYN2V95TxraQA/C3BV1USxlV+BiEVZeCQTnTcCx+ZMDl7Row== 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=zs2dlCK3kpBp+DHzyao4L2xJnXTs0oWXM++0nWxwyeo=; b=DjkN/nvcQ6xFOa5js+kbLAMEAmUpq9e2Rl2JTdHDo887Ysh8Cg0O7qcdkvk860+qsJVgbBxj3vnLUrzm7i0cDv0QzuvdYj5cY+Im2lHOEVH42SUgrUlnniEJGZ4DnORg5x2QqvcMkxr/cQZUPlQpKVx1nUwHr/gxIXjRxmNMtaQhxvlxZ3kwm0uHNhxoPurdH8mlJiN6dTJqqRDt/+5ML6dGhRSCqjDG+N7iT9u/oUoNcIu6pTZhC0S8235gjH9NdFIVdLnEfFK5M8MmLEWQUrr5ChfLaXyvtGWI3D7AIAOziW9zN6svo8bNtB0sTw5bCF6UqgBhb07Vt0J3WOYBpg== 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=zs2dlCK3kpBp+DHzyao4L2xJnXTs0oWXM++0nWxwyeo=; b=gcl8KJLBnJifPUe/cgPkkyaC+XdvUxI9YdFiNUpIFRrYauEmdpZu9w3h0KJNbNHi1SSJgkzJl678RY50Dz/YwEz76BwcGgEN8nh/kTQwOoqv17+Hk+YWMw3daWNpkTVj35LXDvv3RjypvxFO+kcnrWjmGqDHQRqXYh8XD901BFk= Received: from MW4PR03CA0263.namprd03.prod.outlook.com (2603:10b6:303:b4::28) by SJ2PR12MB8181.namprd12.prod.outlook.com (2603:10b6:a03:4f6::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19; Mon, 20 Feb 2023 19:02:48 +0000 Received: from CO1NAM11FT071.eop-nam11.prod.protection.outlook.com (2603:10b6:303:b4:cafe::f9) by MW4PR03CA0263.outlook.office365.com (2603:10b6:303:b4::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 19:02: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 CO1NAM11FT071.mail.protection.outlook.com (10.13.175.56) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 19:02:47 +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.2375.34; Mon, 20 Feb 2023 13:02:46 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Nikunj A Dadhania Subject: [PATCH RFC v8 08/56] KVM: SEV: Rename sev_{pin,unpin}_memory Date: Mon, 20 Feb 2023 12:37:59 -0600 Message-ID: <20230220183847.59159-9-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT071:EE_|SJ2PR12MB8181:EE_ X-MS-Office365-Filtering-Correlation-Id: 931c97d0-2a5a-4d25-f2b1-08db13750e93 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: LoIj0soWdNR/1b+30MySf9izMeGQlhDgtu4AgXT2YAz19Pyo8D9r4PHbnGA9VGxQhBRQWl1PGm9N6OoeyuFZUlL7fT+MC4GEq1G5NvGixjRbz8r1UajMmTAW3YulaHUZtThHuI08Cn4pmYklfoCZO46U4bf8Alxs9dBzAKEB3lZrTxw/xB51Y5kSa87//u5lwu6MwktIqLSFOZISv6nF9IcWUUaP45oQf42nHmUd4uUbw+DUlX3ZXcLjgO6vDlnv6YMd1H7ATbQDq0QVpwGlhXFmjA5bO+lGTIXoKqMO7fxMIEr+m7H6oxPPvu+0SvRK8hVmtjq46cy+VsK6IEVr+SGHPHmpn8G/zdyTN6Cns3UA5bnJzI3WKuh1jlb7EehfDQgPv9MZqiHwonTHpwJoKnbkdZwkwqvttf0BTm35lT2tG2h5Gwx/7PuZ77GqejWDlZcvuILcA+1dxf/TpO/SKwyiQjwYQqqZhvuMgQueezmdLLyL6RZoFa5ZsZlUSETnLQYq3MMoZGKPhzN6vZmMx0jxuwqg2KIuQxgOxfnx7XztW4+0EjEbSSQLg/NEy9IN+yCeU8t1oFEIYrXX5mxKXwLHyRZWYhkgWVT4ABrYJCwVi8r43PqsAjy2slspJSwjz/ZQS8eHITN+RfQ95e46Iv8/byIaXts34c8WbVKfjoe8nWqCjG89uKmXgSLn1hAZQ5jQKnXrgP3vVLX0eVLW/MevC0bXpVhFHIouONeCE6w= 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:(13230025)(4636009)(346002)(39860400002)(376002)(396003)(136003)(451199018)(40470700004)(36840700001)(46966006)(82740400003)(44832011)(81166007)(36860700001)(5660300002)(7406005)(86362001)(82310400005)(2906002)(7416002)(356005)(36756003)(478600001)(336012)(47076005)(186003)(16526019)(26005)(40480700001)(426003)(70586007)(316002)(54906003)(83380400001)(40460700003)(70206006)(8676002)(1076003)(2616005)(6916009)(6666004)(41300700001)(4326008)(8936002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 19:02:47.7235 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 931c97d0-2a5a-4d25-f2b1-08db13750e93 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: CO1NAM11FT071.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB8181 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 6A77D40020 X-Stat-Signature: yj6he5ad4fy9wcj1d9mj1szep3rekjan X-HE-Tag: 1676919770-254249 X-HE-Meta: U2FsdGVkX1/Kb1XcaffwDX/qAcVu4IuSMkpduQAJNZvABFwA3EYbVtASBDjU6L/jFRMKz5sO06AzfhIOxlGwVdSBeI108xYD3KIR4c2Wso7cdUis5dcKtIn+89QnrZnD9rB1Xtm5Oz/IGKRDF7TFGUbHAFTMFw3CMh9fFNbJKE9nq01MisKTryyyzB6xMSIY+NnB8ZwLaLqdRef+snkqMCT5/Td/1jEIby/U0MAMPoBeNzuTk5hX02y6yGLYDebfj5Iz9nOz5lR8hlpFSAxIYDMh3D74eiWRHkq82W0764cJOFgLj+3CFP86AwR7i02gbD7LzrbjOD/m/P2ejVzxKtXC1hB0300p1Y3M24ysMhEu+fdYf77O6ZJkiAJnK/w5paHsDJVp0RJWikIoSz0QTxNrYtfEWFG1S7o5O5zaaVk+uwhcaJlqPWaZcvPh6KtBfNZxvDCwc1rUwg2cyJTRt38s//VshDuCH81/5BUz/dmaI859MB0FMMfWZ6c/ZeyLx2YLmn3RMy/Xg7fahIoaUt3lZndbs3WPA3j4tGtty4HfSWPni5QHfbmDJ9PiwZJpzwW6b3tnjDnuociLYsquGNVa0j8pSP4oz+7W4VG0OSMsVFkEyf5k801PE8prhzDyIekXFbRZBLoAsENz3Qkf+Cy3NJiZk2/+OrlUJrTrifz6yRl1c0BpCwzDldx5fTOkiv5ql3jwyf1ZtkXAC5VCwnHNIUkBbicMRd0i1GuQkdnJ1ZkBtYSkq8rQ8cYguD73I9bFKnqu0Sj347HZXP0Pt8vL2WTblCZijCRcOu0DrPERfE2Y75pmAsrexNTsK9CNW0XqpRZD16hZtaFVrXownHwBTro42GYKoG1IVhqYPuTmit3P5sfTs1oKLTlxvJZX/iuT6Sewux2VmiJnh7yPdYOQvvWNZd26ocrfelaYv4vwMAN2dCPrn0/bbQyOdZrE4C6O0qB/Ag3HHaGamW7 AiJfCJ7l pDH5rkxV3lt0200R04rzy0L1XNgfw6OdgeNP8FejZkdSumazpcy266CZQzML+zFm+rZGcCdCZY5h7lZMf5HSgD3w3LY5qlLBU4EL0IxN0ovYSmjgujGWLx6OfdsbdzG8EOFRb+Uhl4dIFDey53TUNITH1yMQdxk0DY6a8EGrdzA5wbbusTceVmohqsK21PXlkSH4IUfYdfoUgs+7OpEvDGIFeoGkg3+N23uHiZvjX6HafM5hYZnW3LeCH52tqlLwFvTHs87o26sfn9zAHZIN/Ba+9JLJn0iCEsoWL050KdWmhMSfn5EycKTIszz1t8PsQVJ7Ttwxy+8jBNaCxGkTXD0COvQoVwaJMc51PbK6HH1259Wvi/5ynoi6y9w== 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: Nikunj A Dadhania Rename sev_{pin|unpin}_memory to sev_memory_{get|put}_pages. Apart from pinning the pages, sev_pin_memory also populates the pages array which is used by its callers. SEV guest using restricted memfd do not to pin the memory but will require the pages array to be populated. Rename the function appropriately. No functional change intended. Signed-off-by: Nikunj A Dadhania Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 62 ++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index fad7fb34ef9e..523c78bbff3f 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -383,9 +383,13 @@ static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) return ret; } -static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr, - unsigned long ulen, unsigned long *n, - int write) +/* + * Legacy SEV guest pin the pages and return the array populated with pinned + * pages. + */ +static struct page **sev_memory_get_pages(struct kvm *kvm, unsigned long uaddr, + unsigned long ulen, unsigned long *n, + int write) { struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; unsigned long npages, size; @@ -446,8 +450,8 @@ static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr, return ERR_PTR(ret); } -static void sev_unpin_memory(struct kvm *kvm, struct page **pages, - unsigned long npages) +static void sev_memory_put_pages(struct kvm *kvm, struct page **pages, + unsigned long npages) { struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; @@ -517,7 +521,7 @@ static int sev_launch_update_shared_gfn_handler(struct kvm *kvm, vaddr_end = vaddr + size; /* Lock the user memory. */ - inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1); + inpages = sev_memory_get_pages(kvm, vaddr, size, &npages, 1); if (IS_ERR(inpages)) return PTR_ERR(inpages); @@ -548,20 +552,20 @@ static int sev_launch_update_shared_gfn_handler(struct kvm *kvm, data.address = __sme_page_pa(inpages[i]) + offset; ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, &data, &argp->error); if (ret) - goto e_unpin; + goto e_put_pages; size -= len; next_vaddr = vaddr + len; } -e_unpin: +e_put_pages: /* content of memory is updated, mark pages dirty */ for (i = 0; i < npages; i++) { set_page_dirty_lock(inpages[i]); mark_page_accessed(inpages[i]); } /* unlock the user pages */ - sev_unpin_memory(kvm, inpages, npages); + sev_memory_put_pages(kvm, inpages, npages); return ret; } @@ -1028,13 +1032,13 @@ static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec) int len, s_off, d_off; /* lock userspace source and destination page */ - src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0); + src_p = sev_memory_get_pages(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0); if (IS_ERR(src_p)) return PTR_ERR(src_p); - dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1); + dst_p = sev_memory_get_pages(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1); if (IS_ERR(dst_p)) { - sev_unpin_memory(kvm, src_p, n); + sev_memory_put_pages(kvm, src_p, n); return PTR_ERR(dst_p); } @@ -1068,8 +1072,8 @@ static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec) (void __user *)dst_vaddr, len, &argp->error); - sev_unpin_memory(kvm, src_p, n); - sev_unpin_memory(kvm, dst_p, n); + sev_memory_put_pages(kvm, src_p, n); + sev_memory_put_pages(kvm, dst_p, n); if (ret) goto err; @@ -1098,7 +1102,7 @@ static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp) if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) return -EFAULT; - pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1); + pages = sev_memory_get_pages(kvm, params.guest_uaddr, params.guest_len, &n, 1); if (IS_ERR(pages)) return PTR_ERR(pages); @@ -1114,7 +1118,7 @@ static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp) */ if (get_num_contig_pages(0, pages, n) != n) { ret = -EINVAL; - goto e_unpin_memory; + goto e_put_pages; } memset(&data, 0, sizeof(data)); @@ -1126,7 +1130,7 @@ static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp) blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len); if (IS_ERR(blob)) { ret = PTR_ERR(blob); - goto e_unpin_memory; + goto e_put_pages; } data.trans_address = __psp_pa(blob); @@ -1147,13 +1151,13 @@ static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp) e_free_blob: kfree(blob); -e_unpin_memory: +e_put_pages: /* content of memory is updated, mark pages dirty */ for (i = 0; i < n; i++) { set_page_dirty_lock(pages[i]); mark_page_accessed(pages[i]); } - sev_unpin_memory(kvm, pages, n); + sev_memory_put_pages(kvm, pages, n); return ret; } @@ -1383,8 +1387,8 @@ static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) return -EINVAL; /* Pin guest memory */ - guest_page = sev_pin_memory(kvm, params.guest_uaddr & PAGE_MASK, - PAGE_SIZE, &n, 0); + guest_page = sev_memory_get_pages(kvm, params.guest_uaddr & PAGE_MASK, + PAGE_SIZE, &n, 0); if (IS_ERR(guest_page)) return PTR_ERR(guest_page); @@ -1392,7 +1396,7 @@ static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) ret = -ENOMEM; hdr = kzalloc(params.hdr_len, GFP_KERNEL_ACCOUNT); if (!hdr) - goto e_unpin; + goto e_put_pages; trans_data = kzalloc(params.trans_len, GFP_KERNEL_ACCOUNT); if (!trans_data) @@ -1431,8 +1435,8 @@ static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) kfree(trans_data); e_free_hdr: kfree(hdr); -e_unpin: - sev_unpin_memory(kvm, guest_page, n); +e_put_pages: + sev_memory_put_pages(kvm, guest_page, n); return ret; } @@ -1579,8 +1583,8 @@ static int sev_receive_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) data.trans_len = params.trans_len; /* Pin guest memory */ - guest_page = sev_pin_memory(kvm, params.guest_uaddr & PAGE_MASK, - PAGE_SIZE, &n, 1); + guest_page = sev_memory_get_pages(kvm, params.guest_uaddr & PAGE_MASK, + PAGE_SIZE, &n, 1); if (IS_ERR(guest_page)) { ret = PTR_ERR(guest_page); goto e_free_trans; @@ -1602,7 +1606,7 @@ static int sev_receive_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) ret = sev_issue_cmd(kvm, SEV_CMD_RECEIVE_UPDATE_DATA, &data, &argp->error); - sev_unpin_memory(kvm, guest_page, n); + sev_memory_put_pages(kvm, guest_page, n); e_free_trans: kfree(trans); @@ -2037,7 +2041,7 @@ int sev_mem_enc_register_region(struct kvm *kvm, return -ENOMEM; mutex_lock(&kvm->lock); - region->pages = sev_pin_memory(kvm, range->addr, range->size, ®ion->npages, 1); + region->pages = sev_memory_get_pages(kvm, range->addr, range->size, ®ion->npages, 1); if (IS_ERR(region->pages)) { ret = PTR_ERR(region->pages); mutex_unlock(&kvm->lock); @@ -2084,7 +2088,7 @@ find_enc_region(struct kvm *kvm, struct kvm_enc_region *range) static void __unregister_enc_region_locked(struct kvm *kvm, struct enc_region *region) { - sev_unpin_memory(kvm, region->pages, region->npages); + sev_memory_put_pages(kvm, region->pages, region->npages); list_del(®ion->list); kfree(region); } From patchwork Mon Feb 20 18:38:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13146823 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 B1AACC27C7C for ; Mon, 20 Feb 2023 18:39:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2A2126B0071; Mon, 20 Feb 2023 13:39:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 252236B0072; Mon, 20 Feb 2023 13:39:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0F35F6B0073; Mon, 20 Feb 2023 13:39:27 -0500 (EST) 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 0113B6B0071 for ; Mon, 20 Feb 2023 13:39:26 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id A22CC80449 for ; Mon, 20 Feb 2023 18:39:26 +0000 (UTC) X-FDA: 80488533132.21.1A12735 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2073.outbound.protection.outlook.com [40.107.95.73]) by imf27.hostedemail.com (Postfix) with ESMTP id 96B0640007 for ; Mon, 20 Feb 2023 18:39:22 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=zUJcm6Nk; spf=pass (imf27.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.95.73 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=1676918362; 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=lbDYvgnjibN6bbVX25Ges497pkvaF/I6r86gcyyzOcs=; b=pQhasIxO0BF4idQWJksjUzz61O9L1ObTknGkrambSEUKfU09x5Xv7+YHtdBHK6oE2imCG/ 430O0ccwu4LQAVqa4PCNSiEPwXYmAKvzu7AEYBCdMyMUuG4ZaDmyt96M66luKqKUBjXiBc ySE0n5VfSA2awol2xZmMqt+7B7HqE6s= ARC-Authentication-Results: i=2; imf27.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=zUJcm6Nk; spf=pass (imf27.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.95.73 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=1676918362; a=rsa-sha256; cv=pass; b=ulx8gGujhBJlPym/VBDjaGrvHc5ARJ2/S0kTfXGggq9p2lcxwkGGy4okcV0kz+hCqbWX+8 h0G+qAwRxgGBaK9xpBZYtZSPmXLwGzUwzcC/5ad+ZQ4IEQWDKma55TtcD+usSWb5N7KKsS 5K0OFR6gZRuQPXCKyJHtEmbGdb/uTMo= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=iElN+USAgO3JjSOrkjCCg/fCVBJtcrFDAmWOYxo03XXvv72go15Sp/0Xuh4aWuF/TXNLFldoMkYwmZEb07vMpzqKJvG8Q35YRzF8EIm+9JWdL91NLGq68CWe9mryTgd4EZlXd3EnKZE66/iDghF1yHbJUa75JmQcx2pDwa/JAQcX7YZjag2yy2jjgXQdmQBr7+4QIrKbuFKIyvj4VxwH57HND4zgQIqMkxZg3HC1kjBqJ9n5EaIpTYutT3Jg1q0uagfTdwgg2F65BnuUAfG8Xo1lxRxPKNDGbqx1SI8JC9WKUWeGI/UtKEsEDbw6MsupfxEZbdwyMR1sy6NgkdpWnQ== 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=lbDYvgnjibN6bbVX25Ges497pkvaF/I6r86gcyyzOcs=; b=dLI9ZdHulMotsFmhYPTwRpqRmYfObhMlAc8b5e42b9jAOFhmsYXv3v23W+Nv3rHxoF8KJ+g/q5OmbvdExEc2mmR/6qsW2rqT6RONc2Kyi1JIN2lrTEuejmBLtEsvsu7tisGIkGJjObHw4uhHOmaQBvEamggZCC8J8ApqP9TXSSPraULqdXZjA7XUleL9U7N4OFkNxP+Zt3XB5AmpYedkP37I95253UrfrnaMmoGQqKF+dl3eBqYosTPbxShnLL9pXFolP+GEq6hSfSx0V726/VrJQbD2a7sziK501wNOtccIs7D0dTBxzoidRdSRc6kKFwQ9w1Rfpm3aHb//blintQ== 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=lbDYvgnjibN6bbVX25Ges497pkvaF/I6r86gcyyzOcs=; b=zUJcm6NkCzRG5KKPRLozHXH0OJjKonvNo20u96nx96YDDChKBJMBX26iG9csuXrIARW3wKDnv34WSZBnzU8blZVXClvYEtbOXugcJQ4d/2+sDNQUx8i+facscmSuGQQ6loRAFMbCE6Y/bdfIIDX6ln7xvWIobbw5QBPjAx5Gwz8= Received: from DM6PR07CA0099.namprd07.prod.outlook.com (2603:10b6:5:337::32) by PH7PR12MB8040.namprd12.prod.outlook.com (2603:10b6:510:26b::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19; Mon, 20 Feb 2023 18:39:19 +0000 Received: from DM6NAM11FT048.eop-nam11.prod.protection.outlook.com (2603:10b6:5:337:cafe::49) by DM6PR07CA0099.outlook.office365.com (2603:10b6:5:337::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:39: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 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.6111.18 via Frontend Transport; Mon, 20 Feb 2023 18:39: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.2375.34; Mon, 20 Feb 2023 12:39:18 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Nikunj A Dadhania Subject: [PATCH RFC v8 09/56] KVM: SEV: Handle memory backed by restricted memfd Date: Mon, 20 Feb 2023 12:38:00 -0600 Message-ID: <20230220183847.59159-10-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-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_|PH7PR12MB8040:EE_ X-MS-Office365-Filtering-Correlation-Id: 64aba1fb-d7d5-4a20-7235-08db1371c71c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: p1HFcRzjC8UsCHxJH/eHZjH9l7RPw1YuAzuH0HhTQp47HKk08alhnJsK3ViHtpDVuy+gA2AyYvfDF7cRzEfVuMRm5CCRv5H1JuqyIKi6DkkkVYlkzsdP/XhUkbj9AMv+uOwxc7603QkiokutWeTecEaPR8I3gc7LAKeucIQ77/TNKw9FrCHhJaRB83BBV5poUVMkHEDAFQwJ5rIg9kRzDgWyhGv4fV5wwKz5aXGTB30pQp6E5C8aLeISepvo+dcjs8Bt00zHl/M61Vl5pna3rg1IDkp98SQt0DKGvm17F1MNBjIbvo4QkfyIMNK6E2GmxJw5XRjPKo9hQ4YC9T8wmDuaqxI+854BaFplMD765EPDFw+jRPFvahL4ViHDMJaCks4dOk/MPNi4T44l/miAtVlLCogYFe/o49RYbnV69Xlp4/f30/eStCWfUjR4VuO/aZp6dh4Zx44DX8Fcao+JmlOjNhdt5j+ImNsotVaPnRTAxTpB89Q3Lqv6wwFQkvKhfrJFFGeV6zZqduuHbHFCOOcCOdsRTc+cAiQwbMBzBk/E7VJeye4R+dt2FGejhjDdmCklGKef7zVacxjOwlaHqAO6EQB+8UWX9EJ9pITBI6zGFC7QLXRs2wzemvOGCK56HKUogv9FTKwTbhnmf84C8W7ycdNAX/Iz3FV7m93lJ/jhKH8mOUthL2bany1/uh0Oqus5dPzvRIFhdbJ7lW6/h/eZqkJBFSzINXmia4s9Sm8= 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:(13230025)(4636009)(136003)(396003)(39860400002)(376002)(346002)(451199018)(46966006)(36840700001)(40470700004)(356005)(336012)(40460700003)(6916009)(83380400001)(54906003)(316002)(70586007)(70206006)(6666004)(8676002)(1076003)(2616005)(4326008)(41300700001)(8936002)(186003)(47076005)(26005)(16526019)(426003)(478600001)(36756003)(40480700001)(82310400005)(86362001)(82740400003)(2906002)(7416002)(7406005)(5660300002)(36860700001)(44832011)(81166007)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:39:19.5038 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 64aba1fb-d7d5-4a20-7235-08db1371c71c 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: PH7PR12MB8040 X-Rspamd-Queue-Id: 96B0640007 X-Stat-Signature: 4ceqhkd4mgggb873e4jhgqgm3i8itpu9 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1676918362-299616 X-HE-Meta: U2FsdGVkX18AI+uZ/Y+dZg3AKFrJGzBSzLfjKRIBp6ymxzQGa0j83e6Z53VNLJdxa0BdeHJL3ZeprjI3Yf0KMnQ48p2sZbI4BVlJgnKtnBL2GpsocRFMLltODJ4a+EtXQDS9Q7ntoYvR5qqGnANuTZGR8JVZ5VnD0dbYE4PNzX1/XW567E6ViPNEgDV3/b17esQ9T2/aj73QhYlAxmHUmSyTgQQQIYUl2xOL12okeGD2Bv68m+O46kB5bXE2qWKhhYefym/QolqDpfKVxtgiGee6wkR7XmooGRAIO9jHvJij2mXSNC96/1lgJwfB7tpI4Mev0Rdt8dKydvyBABekKYOSEOlHcxNmdKOfaK08rD3vI15zlGZAlglurGEGOvF4zv7MiuIAopRUx4n3Qe7bYd56XqPknTbG0ifIwuNUolLFFG+UF9wQ7KFno/MYBCg/caw3AoM14w+zahoNK3LBZj42nepYOE8cmNg1RjHohpWwF5i9JEhA8sEIb+hZkjQGJ7s4jPRIz6AwZWS7xv6TqP9tBSxquQ4G93d0dzx8C4y9eYS/NJWdxkXa1JFzRIrleockVmpvTCGPkRx/Sgki8hFWWOh03ZgCShT50rHn+bK/AIvM61ftbkUj/aCzAo8yeRlxDdYfpiOupiIXhAp+x5XVg7/BT13BDJsjcwkvRiMHGXaL9i2zOpXoYNU+5jl/wu9iEgtpE/b5AATUixiUqibJ2TEIUHeZ0rn/QuQzodi9muv9plYgdltK3nFpQjuc2C+4hOKBWfNDTNh67KA+rczaLYHY6EpuBxfintX8hOINnEKxujIlt8y9nzouq/qlxGEQVZ5SEYvtdyXzfTVSaWu460610olamjWOqIrLg1RmRnVeCyEM0nUB4XcE9IojvVicKa8ZYgEPtGhu1q9X2X2fGPNRoyS9O3bi5u5et/g1QVSuJZ3WFxj+uRkarC0lRrWza4hvTbpkoRJkhvN 869aMq/E /yWwBunf2WZKPlP2J0kxu+3pt75qNWTgObUip4BqSyqPVaa1P/4vVpTlGtO5fQPVDcCs1GP52b0jp4t8i+0vH826USaJB+ysZjcNM0/AvgPkDBmxCDSQu0pBEuNaVlyGSXLmiBU/MnobB5pBKMFciFAFrjClGFtASWUB1SSMx3vZx15nVMTai8wm4Lh2qPDMtAKP18dxLudMfbO8ZTPRiPnXQi/cG1K+Oy8VVAMaf8v5ednWEyFdBMOLUaAaftY2k3CSlp3/gPlOsbADuFKNcjq3qyYoaprh7A+HpHGpgEmLWFbJNhA5NdNcoxR7av9h4UWktydyeCPTyXL2q9dKvWepDlA== 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: Nikunj A Dadhania Do not pin the guest memory backed by a restrictedmem backend, as pages in the restrictedmem are already pinned. Instead, populate the pages array for these guests using the already-pinned pages provided by restrictedmem backend. Signed-off-by: Nikunj A Dadhania Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 68 +++++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 523c78bbff3f..ad9b29ff4590 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -383,9 +383,46 @@ static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) return ret; } +static int sev_private_mem_get_pages_handler(struct kvm *kvm, struct kvm_gfn_range *range, + void *data) +{ + struct kvm_memory_slot *memslot = range->slot; + struct page **pages = data; + int ret = 0, i = 0; + kvm_pfn_t pfn; + gfn_t gfn; + + for (gfn = range->start; gfn < range->end; gfn++) { + int order; + + ret = kvm_restrictedmem_get_pfn(memslot, gfn, &pfn, &order); + if (ret) + return ret; + + if (is_error_noslot_pfn(pfn)) + return -EFAULT; + + pages[i++] = pfn_to_page(pfn); + } + + return ret; +} + +static int sev_private_mem_get_pages(struct kvm *kvm, unsigned long addr, + unsigned long size, unsigned long npages, + struct page **pages) +{ + return kvm_vm_do_hva_range_op(kvm, addr, addr + size, + sev_private_mem_get_pages_handler, pages); +} + /* * Legacy SEV guest pin the pages and return the array populated with pinned * pages. + * + * SEV guests using restricted memfd backend, pages are already marked as + * unmovable and unevictable. Populate the pages array for these guests using + * restrictedmem get_pfn. */ static struct page **sev_memory_get_pages(struct kvm *kvm, unsigned long uaddr, unsigned long ulen, unsigned long *n, @@ -393,7 +430,7 @@ static struct page **sev_memory_get_pages(struct kvm *kvm, unsigned long uaddr, { struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; unsigned long npages, size; - int npinned; + int npinned = 0; unsigned long locked, lock_limit; struct page **pages; unsigned long first, last; @@ -429,16 +466,25 @@ static struct page **sev_memory_get_pages(struct kvm *kvm, unsigned long uaddr, if (!pages) return ERR_PTR(-ENOMEM); - /* Pin the user virtual address. */ - npinned = pin_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages); - if (npinned != npages) { - pr_err("SEV: Failure locking %lu pages.\n", npages); - ret = -ENOMEM; - goto err; + if (kvm_arch_has_private_mem(kvm)) { + /* Get the PFN from memfile */ + if (sev_private_mem_get_pages(kvm, uaddr, ulen, npages, pages)) { + pr_err("%s: ERROR: unable to find slot for uaddr %lx", __func__, uaddr); + ret = -ENOMEM; + goto err; + } + } else { + /* Pin the user virtual address. */ + npinned = pin_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages); + if (npinned != npages) { + pr_err("SEV: Failure locking %lu pages.\n", npages); + ret = -ENOMEM; + goto err; + } + sev->pages_locked = locked; } *n = npages; - sev->pages_locked = locked; return pages; @@ -455,9 +501,11 @@ static void sev_memory_put_pages(struct kvm *kvm, struct page **pages, { struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; - unpin_user_pages(pages, npages); + if (!kvm_arch_has_private_mem(kvm)) { + unpin_user_pages(pages, npages); + sev->pages_locked -= npages; + } kvfree(pages); - sev->pages_locked -= npages; } static void sev_clflush_pages(struct page *pages[], unsigned long npages) From patchwork Mon Feb 20 18:38:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13146824 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 B5076C636D6 for ; Mon, 20 Feb 2023 18:39:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3DA346B0072; Mon, 20 Feb 2023 13:39:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 38AB66B0073; Mon, 20 Feb 2023 13:39:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2522F6B0074; Mon, 20 Feb 2023 13:39:49 -0500 (EST) 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 1872C6B0072 for ; Mon, 20 Feb 2023 13:39:49 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id ED85E404BC for ; Mon, 20 Feb 2023 18:39:48 +0000 (UTC) X-FDA: 80488534056.03.9F84663 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2059.outbound.protection.outlook.com [40.107.94.59]) by imf30.hostedemail.com (Postfix) with ESMTP id 0542A80029 for ; Mon, 20 Feb 2023 18:39:44 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=cWbCDjzM; spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.59 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=2; s=arc-20220608; d=hostedemail.com; t=1676918385; a=rsa-sha256; cv=pass; b=qGpjX3qVWGWtLXeejB7LiKMmEsjvdlejlmnIhMg+Smv3LXcmuzhAkmpPS7GH4WuIQCtDwP pvlYBU3ys9s5J1R9XreuSLZGr+zkCe66Bl5IEKO7+nTetb5yXt6C5KjHFh8PKfC15JRS/O 7xzLattCS6kYvmqXf7EdH2bKU19SqhE= ARC-Authentication-Results: i=2; imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=cWbCDjzM; spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.59 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=1676918385; 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=ra8CnUkw+2suxZchTpOORUA7BAGVGNIxYd/fXKIHi3M=; b=lc8N/7J19t0aif5ksv1Q4kv/z/pkoigQAHgoumbEK2rop7N3LrnQLj1l7/RDLTy3Oqhkzc jOugSzmarEhKnFA6Q1vbo1+AT57n7qro8KqP6G572TJhBvVbUJlaiANLyw7naz/Kr5NkDI nkp4CHfsZl6rGuwIN5uWagFagBfxjTk= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=arEPh193fPXgsvYvBEQCLQgNx6y5jXTpTzjQ3xLLXWbGK385s851WtndRuXxGTpYylDtSUZAyUB7x7yjlfDuUOR3MXAjeXY7DdT96V7sLH7pywSGSuGcYJF/70uJWPLhMVG1w8jo6Djri0A7nxA8oANuoInYTURqvKLzkT9U9eKdaQMjFn/MRJPYYz/yDx2+mP7vKPg1+Ie96lyoW2jdb8w6Rull+va2PjiHmr/ZlhiZFv1/IlYKVcYe06tjQ74coxeJcFCXKfUuipwAJl1c9BLVSUqtpoHoNCiy4W0mvthwj/SVW6UdELpi95aBT2EWWFYQWOEJjLeaYCaRDoIcjA== 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=ra8CnUkw+2suxZchTpOORUA7BAGVGNIxYd/fXKIHi3M=; b=E1V0m2GIURU1EofIgTVerTrM6ffangeTXlFKto94R1Ie8dLkGFOjH5pniN44uHb6S9Um+YdCAXaG8PvyVv9YBHj3XW7uJfOrafnRG56jI4kcACD5CgZdY+wv8L9E66sm88GvVYANHyULp5lH2U5K6ao0OQcwKPMxUwyzSkjkyKYCLlZWvBoADrllBQDVyYVz2V+O0IY+1W8YvvO1IJCUPS0GuBqvftZl0aKW9vnGN8lhyMO/Xx6zpAZxRMAJ052E4bmwWsZllneE5os3N13cp67E4fkgxsySpIzw2v+wIrg/M+awpbmX6A3h+aFFie+jWu86KSQzovbtxt0/aOHQJg== 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=ra8CnUkw+2suxZchTpOORUA7BAGVGNIxYd/fXKIHi3M=; b=cWbCDjzMeSDN/M3kpbS0RSgePhAOFIc2NfhJWtfsxhLXvVmeLtSuihWsY9BlKi/ti5Xn+pKlu8kBFaWB1yZDhZGGPXQ1HYUnue85EnmH+MQPWwm343M5U2ujhAEB21f+MyN7UBK1yJZ8KgmiVTG9VWb7JwhTloVgO/VV/m2Uwp4= Received: from DM6PR06CA0022.namprd06.prod.outlook.com (2603:10b6:5:120::35) by PH7PR12MB5688.namprd12.prod.outlook.com (2603:10b6:510:130::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.18; Mon, 20 Feb 2023 18:39:41 +0000 Received: from DM6NAM11FT061.eop-nam11.prod.protection.outlook.com (2603:10b6:5:120:cafe::45) 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.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:39: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 DM6NAM11FT061.mail.protection.outlook.com (10.13.173.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:39:40 +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.2375.34; Mon, 20 Feb 2023 12:39:39 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , "Jarkko Sakkinen" , Ashish Kalra Subject: [PATCH RFC v8 10/56] x86/cpufeatures: Add SEV-SNP CPU feature Date: Mon, 20 Feb 2023 12:38:01 -0600 Message-ID: <20230220183847.59159-11-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-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: DM6NAM11FT061:EE_|PH7PR12MB5688:EE_ X-MS-Office365-Filtering-Correlation-Id: 8f594371-5424-413e-0ee1-08db1371d3a3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: oF9pCQMVF1Lr0k7eEiVoJ6GePUsqeDRJfyU7UIQ5u5yVNUWKxc8z3O2KCYb9qAXpPorpqIqd/WsfgWKMX1B/TU4C367WmaO0XhPHmQpqmgxeu09P5p8uXUxNs6A/B/SaN5/J49BPKNKkeWhU+YJWO6E/cK+cOJCuxOpoUEqVV0jowDma0XRqQC3s4SlhLjvZ4okAYaYUq38JG6WC3YhDMIJAGTr2LN5kXxPSevALhW8UpzplWKE5sDSoNrpCEMMmgHlqD1G/qNkqhFbmP51M75g6MZ65EnPqfb0UpimyPLwfDxogS0YuebeIBxgns3j7DlJJJkN8mcY9VpO+Ix7sJaMheF6ZAWxwf7o5nLCZj2gR/BCo9Y779gSZpbDlTf8FvJAX/B+1Lt/xWuoAE6b8Hc1sINw8p3bMwMeKkE19+9tDYsEqNpoZ/KwuJ2vpD+nfGwW71wXIApx3EX4y7T4HhUmoWr3c7FI9M3OGDt4pXjWp2VNhS2jsSO/ks/YeuzJY/zT0yeIKg4Tfpx5bF/btktJsv5a1gB8sJ8bvmA7mqYk/tMFyZKtPZDd0hjS9BKJkWNPGKgVi0AKgvsjjhaQUmg4by4LdmyWb4wTLXkqXS5l/jvEbKzJwjUQ9HKdbnm4ca1U6IriMqglOE2XIskcg2nTXVult301WkXEJSXg++AwKoas5ZkfhqUyj1NxAbSUkUC9IDh0x6R5c5u8odSkGDZfvudY/NC8i2FeXujqNnZw= 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:(13230025)(4636009)(376002)(39860400002)(396003)(346002)(136003)(451199018)(46966006)(40470700004)(36840700001)(356005)(82310400005)(40480700001)(40460700003)(36756003)(86362001)(83380400001)(478600001)(47076005)(54906003)(316002)(426003)(26005)(6666004)(186003)(16526019)(2616005)(36860700001)(44832011)(5660300002)(7406005)(7416002)(1076003)(82740400003)(336012)(2906002)(70206006)(81166007)(8676002)(6916009)(41300700001)(4326008)(70586007)(8936002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:39:40.5186 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 8f594371-5424-413e-0ee1-08db1371d3a3 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: DM6NAM11FT061.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB5688 X-Rspam-User: X-Rspamd-Queue-Id: 0542A80029 X-Rspamd-Server: rspam01 X-Stat-Signature: xibejmwkyd8f1os4ktaxp9gsje3iz9u1 X-HE-Tag: 1676918384-5959 X-HE-Meta: U2FsdGVkX1/YevRDzTTsoOIed/Drbg2tFjFZSM9TepiIjQwa1Cd/miHDpoiZ1GkdKVSft+7xfA6GNakqjfEoLshas9ed7e19a1AXopaCPmeLNMFGmrLoyTMIA5OBfmYpwc95B7J09He7LSCraQLdZML0R/iewvBd6QnufWIhXookPffC0pZeWp6N/HmGbPfB2jOGEypjtjF/UUb1sOyKNHoNuqon39DbkVdcbjU6CViesHlwVGfVtyie6sw1pL43g30QRF1ciDtqnzOM9fCtoTJi4muAig/Rsflevesr9e50vi2n5Pz80+Vgd1tRQsXCUz/rsj7+Qh5IwAlv4pxmIrvwRY0Hvtex6mN1oCRI7BD+nJO1xl8yOyOutHNAGVDx6XE7qk72eWUkGNYj3owIxYh0as3pnN/OaacvPBp1dcFUIwFyRNkoAp3HcRu0BWhPpZnBCdt5YAly50+eq+csvYjbrMqY/kkETnrYcCCv/0/u84DMQSIRalo7h5jAcYquxplr8QBJSaaDv8hi/0N8KGcR1s98IEvj/DptEhSdv+kKihEuI4z6lF5QCSd7HHMmGzUHR4k/gGZGqRTylTw9JETOZMzFuiv/9QmJfgT6h/82ixftSALWsThwy18kIOrsPTMiTltLw5hD2uWsZojnLdPcbF58TImR+7OUEtO4dFeoY9AEbQOtu85Iw4a8Lq/KmmsEFnSTlVb+qfp6WZE2NMSK+2JkE/sNvePaKvVV3+HfEZGXoJVheFoPzPoXHWVPtmbLhe5pVOsLxl5Z4+1at9ZoabP/zOT1L5+OKMz+IaAwb4+tMIbA/5FDRkBU4JazAVah9ADvtEx6PyOtYmPd/XGdWUoj4tZyiS7v7XGQbalmYu/p5VhQ3yEzPcHBkV3sdkyFGNg9nG+6nLGjJVe0b21lZ3qJaSBWtVoKHpTXh+bj8mDPKtw0ciZBj4VVYBZv8/7sV7y16oIf4L/n8R1 K/jAu8QK aCfCHvTg+pExmIUxxFF2GhqzANqG27RqTJ/evZoxV5i5iKQn/nzTPAGJcSyEtQubfWxVkxYVES4X+OpnioDqPPAHvFdeYVSq7P4MeYeHDCEVV3zXmcbOJYuPTXf4W5eVWg8W6wvZAhtMRjRUEfBeJSuQ6Xa0KaIrjui/vq2gG2eC+rkhELk117FfzLgPNjIa928KuDRbNoqVC+J1QVYZXijqUXIWfJqooKYbSiF8B4ziw6cdsyXZdycjLxlckPexBtmMIcJtpG/elhxGY/wF6evBpx4h0RzvdFkVw8Z+wylq7ZnISqiQtR1SWIEB3yO5/ll5rnOr/HsyTheOqzt/9+djabdWfzB+NNdHxEH1b765P7bwSPnUu4EOa3FyI48/KPu0TGImlbdP/BqI= 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 1419c4e04d45..480b4eaef310 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -420,6 +420,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 860b60273df3..c7884198ad5b 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 b71f4f2ecdd5..e81606fcd2ab 100644 --- a/tools/arch/x86/include/asm/cpufeatures.h +++ b/tools/arch/x86/include/asm/cpufeatures.h @@ -417,6 +417,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 Feb 20 18:38:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13146826 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 EFE07C27C7C for ; Mon, 20 Feb 2023 18:40:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 931F66B0071; Mon, 20 Feb 2023 13:40:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8BA876B0072; Mon, 20 Feb 2023 13:40:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 70D166B0074; Mon, 20 Feb 2023 13:40:31 -0500 (EST) 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 5860B6B0071 for ; Mon, 20 Feb 2023 13:40:31 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 26EB4AACF1 for ; Mon, 20 Feb 2023 18:40:31 +0000 (UTC) X-FDA: 80488535862.16.31AEEBD Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2055.outbound.protection.outlook.com [40.107.94.55]) by imf14.hostedemail.com (Postfix) with ESMTP id 092D1100021 for ; Mon, 20 Feb 2023 18:40:27 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=rJy62BWV; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf14.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.55 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=1676918428; 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=1VCT0diDUuBiNxbQj4irtCaHwl+/vl5tfhPxGNP0oDs=; b=q5bQI41801vX5cO1wA85mP9ZZ3gNKiaV1FWaTwhgZ79sVBVsr2ircJe+4fjJJgh4+fU6d6 OKQKYIgVb5A0briZ98cy5inYuzlU/3owJNUoDZF1NJZ1oNhL7+53QBwwy7iWe/xv98lAOQ GDlmwDG7JW87qov9kt8TfnRhRs1YKLU= ARC-Authentication-Results: i=2; imf14.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=rJy62BWV; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf14.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.55 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=1676918428; a=rsa-sha256; cv=pass; b=SVaGdoE7TqojbZwhEGSUiU0LN+9D8eeSHPNrOom+chXUT9ZLGUtQZHUGGdxNbeAWqNwn1k FYHH3Z6ecgYtr/9P7nwvrF7JD2MAOHQo1VXPjpK66WOoKPODrrg05rzA/SrcL3VsU1r7Xg 1IllNGgoR7Si1UpTrsiQa+GR4XurHDY= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NlUNlkge1EIgxPPpvhKbOAmPUspaieyUmK0B0rDURF0FnRV5rywRzqVtLUEbdLVk6W5r7wTdPjwvvSWHeJGO1LYA31cXErz9vdIhOVr1aIvU8Fr/IY64GV9W3AuqNYhN32it3KcAg+9ATR5Jh6CjylUif3ZFCIhGYy2CE6dtsWl6paGojyMW6u6MRH0FkPAfugeFZW7CbgyA6QxFwE67Y+uk1yP+eAMXbYbski+sOFukBmEkZiSWmcOglBRMDYMPExvb9F5w5kji+qUJWDa3X5NvLFI4LknuXiy+EcMIRwA7sHvhA/hum14U9XsdD/qXeDsAvGPZZz0oV0pee6ABUg== 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=1VCT0diDUuBiNxbQj4irtCaHwl+/vl5tfhPxGNP0oDs=; b=WPxPjn5cCNTUJuH5Ybp1XuTSvNmmPf1CpCyN58t1dDVcZcM4sq5WZdkLVIMQEM7vXeSfZi912enpbul0k1INLTvglFybITjVs9S3xd6OHj/ebtVhzlZTeChNZNIxbhDCqmIRs1nuLtJMM4wFr5ebI+p/7Tkp9RwKZkeEhAOu106y/H1cQR/4hfpuc9EkTrqQDWFXRWk2+L4yWNXR5sOdHCYPjTXwKNyOv+qNceIv/nsOySfp/y/rpEIvB8sYTGiN0oAOC892W4m48f5RTIQLUtQEv4VKHiZ5pgRIOjCV+5S8TcZ8K5ENloBLzu1yX/I7D/vwrxvfzWSdlaioMP+0WQ== 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=1VCT0diDUuBiNxbQj4irtCaHwl+/vl5tfhPxGNP0oDs=; b=rJy62BWVS/4DNR4nMSvcxZEkS61bIBxgm0d7KgSJ5zrRXngl4Z6SzzZRvg/qN8lQyR28GR1DOW3zPq9w7V8zVSyLcyXxmkhxZKFugDyZd2/ywBgP2RIO9TdcUclXV4f/NfUs59hGrjVdpBaxESflOnCGptMFtlr/FXzWfhVYcLs= Received: from DM6PR03CA0087.namprd03.prod.outlook.com (2603:10b6:5:333::20) by PH8PR12MB7208.namprd12.prod.outlook.com (2603:10b6:510:224::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19; Mon, 20 Feb 2023 18:40:22 +0000 Received: from DM6NAM11FT016.eop-nam11.prod.protection.outlook.com (2603:10b6:5:333:cafe::64) by DM6PR03CA0087.outlook.office365.com (2603:10b6:5:333::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:40: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 DM6NAM11FT016.mail.protection.outlook.com (10.13.173.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.19 via Frontend Transport; Mon, 20 Feb 2023 18:40: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.2375.34; Mon, 20 Feb 2023 12:40:21 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 11/56] x86/sev: Add the host SEV-SNP initialization support Date: Mon, 20 Feb 2023 12:38:02 -0600 Message-ID: <20230220183847.59159-12-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-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: DM6NAM11FT016:EE_|PH8PR12MB7208:EE_ X-MS-Office365-Filtering-Correlation-Id: e0a1d5b7-efbc-41a0-6f29-08db1371ecaa X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: AorAVHYxkAKOEYDG6S8s/Oq1bZm6MfoKO5Gabd51zyjIGK//ljU+ywaPeR3USqAwzSBqxEAcZWjXh9yaZ42+Lxj7UPAAl6L7DxG4Ix8+LAyYkV5vXsH7WbovSfPrBNYj17/Q8r99PuNx4CBzt/FbE1HbWya/o+CmZoNelmzU/m9g5lUIgnSJTQ5Q1SRhGfW78t1L5EZxVEWPe5VxBkGb1N0/vQGQaTae79khb5Fe+7NhU5PVAoI9+ULHnUl5HQwo4AOZYipvj7y4QJLeGfWmUoFOl2VgryUtCxb8rn9ABHnI568QgMKf66s/V8ZXO9bL3p0w5fGjXL0+hWLRp/ksqTkwqVMm/dSBcvCnAcnwtkL7xKY5rWct6+n6pYO1GiUXYX8DJhEuIlo1naLTOX03u3vrbTj7tybztPuaG+hYPaMdthvqnu8RrO+LDPFelXkl2IAOWYiGIVp7BrLEyzuzwlHlMzBvBRIOt7HgUr4AIQEm879pNhkXAn3A9D4n828FIrOxIyYFuEngNgsFs6R+RQ77dY84GzGV229U82eu7f02xVSSLVjEEKJK69D2jXu2XBGPrUO7styNw3Yq95a8/eL3nyqpFb9c1ygZ5IVAdxgtJJZmEzSnVilCNMvTcJRK6wx/QRzGVD0rB+JWW8E/ILDsoHkJmkHnGYBEIalDwvHqygTNCBZbBN3ETLXyftxGWeCBc8rrz0MWOmhIwmAA1iLrKBtA/IACDHJ8s6UJ0jc= 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:(13230025)(4636009)(396003)(39860400002)(136003)(346002)(376002)(451199018)(46966006)(36840700001)(40470700004)(40460700003)(47076005)(36756003)(6666004)(8676002)(1076003)(478600001)(2616005)(336012)(186003)(54906003)(26005)(316002)(41300700001)(70586007)(4326008)(44832011)(16526019)(70206006)(5660300002)(81166007)(7406005)(7416002)(82740400003)(36860700001)(8936002)(82310400005)(2906002)(356005)(40480700001)(86362001)(426003)(83380400001)(6916009)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:40:22.5100 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e0a1d5b7-efbc-41a0-6f29-08db1371ecaa 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: DM6NAM11FT016.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB7208 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 092D1100021 X-Stat-Signature: gyoy3aiabr35gn7upigxsg11bxuojg4n X-HE-Tag: 1676918427-994493 X-HE-Meta: U2FsdGVkX186Ia+8GKQ3f2s360mIwR+HWrfH/7CW+X08+nDhH0aauvAF+/VCxEF9XEY5D/Cn1VzSGZV0YDfzO1zYBdDIRMiO3tqp3QyZ1KJpzyPpW+4QqOgHP62TPaczkcA9/dMBKWdwy2uhB2qGgh6jo0DK+B2BIUtRg3CIdRP1ENj0KL1aF7te5ZqXiBudGYlvDDm0T1vFnnNCWy/QPuFZ0k1wOU13R675djAoqnSOOGnW36j/1trZv6kSF69GF0AEoZFi43aFo4rslBJ0taMxDrJnP+ndAPYZMS4KtgsYn6cBrBFXhXU3secCmfPQcsUphZa+P2vOB0sWp1rmGcpyMkXYXf3f7R5l1xqGUnG0ER6LFy89k6/EnP8ezRFyZqSyKZn5CClF3JzeZLy/xOE7e5SDYlTma2mhkg3NSSQ4ZUOucj9sWpmR7eeXyKixI3QTfyNmO9n9tmV9yDdCHdiBkeoFE0J/ibbV18gCUdqZMbE+kcnkqNnZGcgTcJvbUerlDk+lpGylqurDIAZaLa9a8O3QTxJ9fSBT1sZC7FiKoZyl/pBM5rDBLtcs1zWRIxyNDwq8c/XV5/B0HXJAPjhbEbJsfxZY4RAiGRUQlYfNnrDe+wyTdOl1xMDJLWUeg4K6R2Ter2Rcz7myMrX/qCVYQvhl31haXQoSrTvtu+t8bCf46jrpU4JuPkSJeGuX1W0yMJLazkRm64YztldUPLg7eHR24BYZHVz+tzs/xwMzqGo7dbuX+hctuuKtBS+K3fDJD46NWMTiS5rgSKF0CRCcuwffRn7BpbDt8VIGStpc1Py2qzLO3piazMN31a8i11AXG0MJz/0q1R0cpZR57hmwmiwFtpeBzDarMzd024fXHoos6DmVACdA/f0rgyJKOzkxEJYqBuUrrfyHNG0j8aSFigULl/b1ZMZP1YltTiwYi5s8Eo3+q2bB0TjlG9leXEG2WSt319GgNGgAZdT +rPO3PtD dqz2qtd0hkpxDjwUJijWgwcqIQYsbCMN3d2h7PyfO67cHtFUTM7xlqM5d0Dve8mKY7tWqd7ObYxRb0vQi2zGKq40v5plOayOY6xqB33gEB+p6iLHnyxey4KQPIE+7ghqpBgmxuliTCqC3cdPKh580Kl8P2fLZRnnsFaiUZipLDuwWJ9GshbduU1IGmiK/N2LozLNPFjAnXMXq8oXziCZB9USWQbtyPxFErMs5doo5gdyWnTOAuACFm/kgSNomlGg1lToo1VWINfItiRQqH1mbvehnWlTXhrYKUOpfPXXQaARcOzLvBKrqKF6gZQvkWbMlCtnvXrCXHJFYlOM= 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. The goal of RMP is to track the owner of each page of memory. Pages of memory can be owned by the hypervisor, owned by a specific VM or owned by the AMD-SP. See APM2 section 15.36.3 for more detail on RMP. The RMP table is used to enforce access control to memory. The table itself is not directly writable by the software. New CPU instructions (RMPUPDATE, PVALIDATE, RMPADJUST) are used to manipulate the RMP entries. Based on the platform configuration, the BIOS reserves the memory used for the RMP table. The start and end address of the RMP table must be queried by reading the RMP_BASE and RMP_END MSRs. If the RMP_BASE and RMP_END are not set then disable the SEV-SNP feature. The SEV-SNP feature is enabled only after the RMP table is successfully initialized. Also set SYSCFG.MFMD when enabling SNP as SEV-SNP FW >= 1.51 requires that SYSCFG.MFMD must be se RMP table entry format is non-architectural and it can vary by processor and is defined by the PPR. Restrict SNP support on the known CPU model and family for which the RMP table entry format is currently defined for. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/msr-index.h | 11 +- arch/x86/kernel/sev.c | 175 +++++++++++++++++++++++ 3 files changed, 192 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/disabled-features.h b/arch/x86/include/asm/disabled-features.h index 33d2cd04d254..9b5a2cc8064a 100644 --- a/arch/x86/include/asm/disabled-features.h +++ b/arch/x86/include/asm/disabled-features.h @@ -87,6 +87,12 @@ # define DISABLE_TDX_GUEST (1 << (X86_FEATURE_TDX_GUEST & 31)) #endif +#ifdef CONFIG_AMD_MEM_ENCRYPT +# 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 */ @@ -110,7 +116,7 @@ DISABLE_ENQCMD) #define DISABLED_MASK17 0 #define DISABLED_MASK18 0 -#define DISABLED_MASK19 0 +#define DISABLED_MASK19 (DISABLE_SEV_SNP) #define DISABLED_MASK_CHECK BUILD_BUG_ON_ZERO(NCAPINTS != 20) #endif /* _ASM_X86_DISABLED_FEATURES_H */ diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h index 10ac52705892..35100c630617 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -565,6 +565,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 #define MSR_AMD64_VIRT_SPEC_CTRL 0xc001011f @@ -649,7 +651,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/kernel/sev.c b/arch/x86/kernel/sev.c index a428c62330d3..e54e412c9916 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -22,6 +22,9 @@ #include #include #include +#include +#include +#include #include #include @@ -38,6 +41,7 @@ #include #include #include +#include #define DR7_RESET_VALUE 0x400 @@ -57,6 +61,12 @@ #define AP_INIT_CR0_DEFAULT 0x60000010 #define AP_INIT_MXCSR_DEFAULT 0x1f80 +/* + * 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 + /* For early boot hypervisor communication in SEV-ES enabled guests */ static struct ghcb boot_ghcb_page __bss_decrypted __aligned(PAGE_SIZE); @@ -69,6 +79,9 @@ static struct ghcb *boot_ghcb __section(".data"); /* Bitmap of SEV features supported by the hypervisor */ static u64 sev_hv_features __ro_after_init; +static unsigned long rmptable_start __ro_after_init; +static unsigned long rmptable_end __ro_after_init; + /* #VC handler runtime per-CPU data */ struct sev_es_runtime_data { struct ghcb ghcb_page; @@ -2260,3 +2273,165 @@ static int __init snp_init_platform_device(void) return 0; } device_initcall(snp_init_platform_device); + +#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()); +} + +static bool get_rmptable_info(u64 *start, u64 *len) +{ + u64 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. The reserved memory should also cover the + * RMP table itself. + */ + calc_rmp_sz = (((rmp_sz >> PAGE_SHIFT) + totalram_pages()) << 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; + + pr_info("RMP table physical address [0x%016llx - 0x%016llx]\n", rmp_base, rmp_end); + + return true; +} + +static __init int snp_rmptable_init(void) +{ + u64 rmp_base, sz; + void *start; + u64 val; + + if (!get_rmptable_info(&rmp_base, &sz)) + return 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; + + 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_host_init(void) +{ + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return 0; + + /* + * 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 (boot_cpu_data.x86 != 0x19 || boot_cpu_data.x86_model > 0xaf) + 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 -ENODEV; +} + +/* + * 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_host_init); From patchwork Mon Feb 20 18:38:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13146857 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 C82BEC27C7C for ; Mon, 20 Feb 2023 18:44:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4E9156B0071; Mon, 20 Feb 2023 13:44:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4989D6B0072; Mon, 20 Feb 2023 13:44:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 339066B0073; Mon, 20 Feb 2023 13:44:44 -0500 (EST) 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 23CD06B0071 for ; Mon, 20 Feb 2023 13:44:44 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id F173CA01AD for ; Mon, 20 Feb 2023 18:44:43 +0000 (UTC) X-FDA: 80488546446.10.09C2A1C Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by imf30.hostedemail.com (Postfix) with ESMTP id 0B1C280021 for ; Mon, 20 Feb 2023 18:44:40 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=lM6emfzz; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.40 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=1676918681; 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=PyENLFIa0rQ2Wtg5Iei7J7RXR143JffNAsYZ2SlRxIs=; b=j8lQx35LXTeFRV01Sqv6byLUM691TeBWI0pD4+Qe6RmS6WgVwTn3EN7yh3h4zJ3LBmBNER bsKOwvIP0YDO6z1KxHvNlv0yg6xRqWeNUc7WKiHsKMZHBJJ8vKBz6m3xp4aNS4ZcdlZmIX 5uRjEPmLjkJS1ZE5BXQDp42ih1TzPHw= ARC-Authentication-Results: i=2; imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=lM6emfzz; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.40 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=1676918681; a=rsa-sha256; cv=pass; b=jj9vdRHqn7OHpPQuG3Gq0ZA/gl2yFQUWmbVVLlCzrK4MVOQq06iJj2s60/7KrMhmPZzW2k Vt64Gr0PJmdkN0EwaBNj3J+YjLdgChZlQTJ6JwUdVbJ53zCXvpKDiEBeHTvd03By45c9SE 1xbK5Q5hTHyi2VWPbdcp2o+rz9toMRk= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eCbQyQqF6a+KBoc1ykyWOG211hV4H1ndN41W5pOqJhLBIJj6eRHSsR04flIYPylO/dH8yvVutktMGxE8cG/lpS0/RXW5ZLtrEPhIp56xbrVdvW6cjF6UHUZ0XD+ozhI+L05ep0h/AW+UIjEI8rDYeMNl+qRxc2dnGlKAh3JcsJClwUc1sXZu+/tpuV1UhXUC0xYBE7igXwUrPL3kifs6zNlDy9M+2/5hI/bm05g3Bp74vCKZwI/0/DMbkgm5mi1+IopHI2+BuBRERJjh4QfYJIBglpZIfogo7O0aYOEEpR0atZlx3tzXMf9sJG/ubkqq4vqhAlZQHTdXPXvQudbJVg== 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=PyENLFIa0rQ2Wtg5Iei7J7RXR143JffNAsYZ2SlRxIs=; b=OlSQvEO93n80019jyLJ6lYtZ0urD31oAprmPIT1xD+nJqwYoB+TblLlez4xh72+Zb99WzUhW75ZMx37hM204CINgwE8U/K5BKY7wvAocoHrzQ8mOv8+C4ibMQyON2cKsjjQatu8DpA9BRBUsHCFGGp/EbfAC+h47LG/ZwD/kNFPZE7sXAebaFJqZY2sLTDF3/eRAqMnsBpzo0LWnb6F1qLrDlsQagH4TJ7gKacL+AEhU6p+qt+RTPs7S0/e2Nd2jC3GpTC/1Gkvjz9EjFvwwRLlE64nC+f4VxOkSu/zboOEdRhmehTZ3UWALlv6a/tbNDE7SspREjiOnRi4OfqqUEg== 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=PyENLFIa0rQ2Wtg5Iei7J7RXR143JffNAsYZ2SlRxIs=; b=lM6emfzzp7OKNY/Jwv5svpD1nZMjgdkGKkRU8LndYmpk/iOnhCWj3YklsH6+c1wdN6avC/oggMd22ZNBFD96xtsyY3R6woDN4lMHsWagCmd3l1XEsErb5yVMCK/OEHvrn+WIzKjeE0J9Vu3Pmvfz4Bns9/z1v21KMY4q9c4msA0= Received: from DS7PR03CA0055.namprd03.prod.outlook.com (2603:10b6:5:3b5::30) by DM6PR12MB4140.namprd12.prod.outlook.com (2603:10b6:5:221::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19; Mon, 20 Feb 2023 18:44:35 +0000 Received: from DM6NAM11FT101.eop-nam11.prod.protection.outlook.com (2603:10b6:5:3b5:cafe::f8) by DS7PR03CA0055.outlook.office365.com (2603:10b6:5:3b5::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:44:35 +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 DM6NAM11FT101.mail.protection.outlook.com (10.13.172.208) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.21 via Frontend Transport; Mon, 20 Feb 2023 18:44:35 +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.2375.34; Mon, 20 Feb 2023 12:44:34 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 12/56] x86/sev: Add RMP entry lookup helpers Date: Mon, 20 Feb 2023 12:38:03 -0600 Message-ID: <20230220183847.59159-13-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT101:EE_|DM6PR12MB4140:EE_ X-MS-Office365-Filtering-Correlation-Id: 0f06a04f-cc09-473b-2865-08db1372838b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: EFT2kjw0bm+eGlPQP6oylLpv4iMAnT9m2wHTjNcei05ZfJv7vE5JVZqFywn7rVQ7JsLC4eV/5dUpriH5H2K6B91tqgUL2ArSxHcIZczA1GmvGMYKDglmHUVJBqPGecusMHGN2LbTMKQAGiNla6joXWmMB3o8GGBWt67YBdND94cVBdNaisSTz2YdFfifvdbLKCbciEAwY8euRLm7KUwxfkNvM9jc0MtZSuVGP4ZDpR/4Jky/AUpqJq041PLApaW6GOftJBG2UjY6q14/06bwY68+YBaiGD78gDq2MZriuaNtF5WZpVW/hpi7RKjdZ0UN+IPU2NLlOURh6d06s7fpZAAARomx9e5UE1O9a/TjhyhWIuJQFIrQdw6a5bo2jDIRDM6kwfBQLiNG3hJC/QN/g/g2XNe529MXDND4ujrhE6ctVlo3QsrZavsvOpv7uXRFV/jpLCIFxhjyNw3aOdZbGcvLqPC+4QvEDizrDBXC4n43SGV/TZzpCLBEjAKIYfU8BrLM9aFTYkwxfMg4K3wVYnYfKORgVjlCAvv/0ONtfX1gUL/edbu1E6OIrV9kqlkWaFBLR8vyOWuKxigTHf9AzDyu3iCiHJ/xCXAgos+bIHoTnuPF2m5gTyQUCRpOmqrNXE0SKzdFU3yq5nDIpsHvlL1oMk0RREqkl7zqdnbMeTUHhAGy8cSSzVFZlKxPliuNrkJ83MjNB6sp+IEmqLpIBzcN0huK1L7nEF7GRLMNl89UDKOVF5Zyeh+8MM2rHkJX 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:(13230025)(4636009)(136003)(346002)(376002)(396003)(39860400002)(451199018)(36840700001)(46966006)(40470700004)(70206006)(4326008)(70586007)(6916009)(8676002)(316002)(7416002)(54906003)(44832011)(966005)(8936002)(41300700001)(5660300002)(356005)(40460700003)(36756003)(86362001)(478600001)(26005)(16526019)(1076003)(186003)(6666004)(47076005)(426003)(2616005)(336012)(7406005)(2906002)(82310400005)(81166007)(82740400003)(40480700001)(36860700001)(83380400001)(36900700001)(309714004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:44:35.6410 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0f06a04f-cc09-473b-2865-08db1372838b 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: DM6NAM11FT101.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4140 X-Rspamd-Queue-Id: 0B1C280021 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: gxkf1tq8yt1heubxo1ye5e6hukcqcy91 X-HE-Tag: 1676918680-784392 X-HE-Meta: U2FsdGVkX195tXMB7ow6fQTSEZgj9DtbI7zjvPPdv2q7zxOdfemnim9ugx8meAfv+Z3mdHtHQyLiMmaQOufqjXM/ePt2v6w/7F6zjjhgwrTfKvH9rljG87QXjLj2Sl25g17w44ok0xVtO6p3WVZaufP2CppJRqRRjrKJMo8RgUQuw/UsSU63pDbmeQUk0EEM7Vhp/ZWeTZ1KWyPlMqFUpLrPzyM/grOSbcdkVjLh6mNK3DI1nmSPeo5OT3JQeaz2TnrdwjChXypATM0ZjfXZ5EJWY437tn6CGqxKgvcEQP7JSyZKOv+g3quOqbsVkhDr+iUzUgrb3uvuLRhCa6REf23/DKHt3oq3UznIj9Xo61wlwGAr4gQj9yS35phauohzZ/k5OBzbWI2w9q5H66dL9EifyxIOAWBO/69GP8+kzeeVeKr2wESVyvsFc5JKPatGUZ6J82Qj9F1cHuqe07IyNvbhLQ9R0b5hwwULUPcJJv31wu3B+QG7RjPbuMJTpDnK9y0SNGOu6XSMoQUSRjA1DslWvpHyJkFVHI4YmWCFgcgfmJ2RIu3/tVji5Z9UHjOX+DZHBROOS1hFMyqJdAXqbQon/KhAzvKyziCI9xdwdfL/UN/iqPEwMVjG+MGQg0NS4cAj/O5LjciknO1YvXgE4NzFO1Cw99fpaboemItlOm6WbZXWKH80QQzLPTleUCxe1W8QmoPZYKWWGxsWROmDdfcxdF/pQ2FnxcJ31pS4HlHEdCBROLJWUpbjwIlQhX0qz2Iseul9b+N1BOFfG38OQUbrw/pQYRDriTc2HiVZeVjD3zXRjo4hgeTPDf6rdL8Wu0yk1M2WK0978oGnwnAHy4zn7H3eyvd0lU+Ed8hqYNqSkyGJUolJMaQfC4AfRuW3MyKXcqNTr2qkIi1tvefoh/YOtN9VeIjG25XKcF0HApadHaHmpnWFqJFNFBavWH4V94vEo86kxN6txO6jheh gezQ1jvH pMhhGomArjXUXi6ZlwefZEmw0w7GiY2cfryEsu+Ydf9GrLhc5Pwl0dNlRp1SWUmPxd9yLF3FgBIJx40UvrjqxTfxuEz3UUdIKBwCdOOKP/ekIR9Uvde5W1Cf60DrJ9s3dMVc+bsFbAJK8vyiz4cYGXMKYua0nYuRGe7IObVqJMUMEUAI+trq5Hau+ngyQLT80JRklpGaLOYiOiS4TLf0NKC0BquTViOg0L9jFDOBiDY3Pi+ue1g75+TT3u+GiHbQ8rjp86xkIm6yakVxX9O/Mijz5gBvMoKNiUFcsSUtzWKkCpT2PFcPloOB2I3kJkGmvjCqZsmFE8uELl1tzPcIwGlVUTiMA3FrFsUgKUcq5EdvyscLIzqGH52Smwwduy8Tc9pz0CknPlRPa9e4= 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 Signed-off-by: Michael Roth --- arch/x86/include/asm/sev.h | 4 +- arch/x86/kernel/sev.c | 84 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index ebc271bb6d8e..8d3ce2ad27da 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -83,7 +83,7 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs); /* 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) #define RMPADJUST_VMSA_PAGE_BIT BIT(16) /* SNP Guest message request */ @@ -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); +int snp_lookup_rmpentry(u64 pfn, int *level); #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 int snp_lookup_rmpentry(u64 pfn, int *level) { return 0; } #endif #endif diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index e54e412c9916..a063c1b98034 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -61,11 +61,36 @@ #define AP_INIT_CR0_DEFAULT 0x60000010 #define AP_INIT_MXCSR_DEFAULT 0x1f80 +/* + * 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)) /* For early boot hypervisor communication in SEV-ES enabled guests */ static struct ghcb boot_ghcb_page __bss_decrypted __aligned(PAGE_SIZE); @@ -2435,3 +2460,62 @@ static int __init snp_host_init(void) * the page(s) used for DMA are hypervisor owned. */ fs_initcall(snp_host_init); + +static inline unsigned int rmpentry_assigned(struct rmpentry *e) +{ + return e->info.assigned; +} + +static inline unsigned int rmpentry_pagesize(struct rmpentry *e) +{ + return e->info.pagesize; +} + +static struct rmpentry *rmptable_entry(unsigned long paddr) +{ + unsigned long vaddr; + + vaddr = rmptable_start + rmptable_page_offset(paddr); + if (unlikely(vaddr > rmptable_end)) + return ERR_PTR(-EFAULT); + + return (struct rmpentry *)vaddr; +} + +static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level) +{ + unsigned long paddr = pfn << PAGE_SHIFT; + struct rmpentry *entry, *large_entry; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return ERR_PTR(-ENXIO); + + if (!pfn_valid(pfn)) + return ERR_PTR(-EINVAL); + + entry = rmptable_entry(paddr); + if (IS_ERR(entry)) + return entry; + + /* Read a large RMP entry to get the correct page level used in RMP entry. */ + large_entry = rmptable_entry(paddr & PMD_MASK); + *level = RMP_TO_X86_PG_LEVEL(rmpentry_pagesize(large_entry)); + + return entry; +} + +/* + * Return 1 if the RMP entry is assigned, 0 if it exists but is not assigned, + * and -errno if there is no corresponding RMP entry. + */ +int snp_lookup_rmpentry(u64 pfn, int *level) +{ + struct rmpentry *e; + + e = __snp_lookup_rmpentry(pfn, level); + if (IS_ERR(e)) + return PTR_ERR(e); + + return !!rmpentry_assigned(e); +} +EXPORT_SYMBOL_GPL(snp_lookup_rmpentry); From patchwork Mon Feb 20 18:38:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13146858 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 B6947C6379F for ; Mon, 20 Feb 2023 18:45:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1657D6B0072; Mon, 20 Feb 2023 13:45:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 115E36B0073; Mon, 20 Feb 2023 13:45:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EF8486B0074; Mon, 20 Feb 2023 13:45:02 -0500 (EST) 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 E0C9F6B0072 for ; Mon, 20 Feb 2023 13:45:02 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 8E28C1607D8 for ; Mon, 20 Feb 2023 18:45:02 +0000 (UTC) X-FDA: 80488547244.02.4145A29 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2058.outbound.protection.outlook.com [40.107.96.58]) by imf06.hostedemail.com (Postfix) with ESMTP id 8B75D180012 for ; Mon, 20 Feb 2023 18:44:59 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=UkpYSvrn; spf=pass (imf06.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.96.58 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=1676918699; 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=F2kvs1P/l4ZozOxsEC0d23b+JIIcSAMRctM1la8Mn4o=; b=syhFeDaZFDQ+GG6gaQKgnLWv3LG/R3KkxxGUTlf4oHgWs8oj6wcDn5bT6wJjdjEr6UhsUy H02DIN+o5gDGypNmpOaU1picW7KF99BSQMLsx3LSv99geWjou3yXP/qmZ9S87Xs7Uxkb9T joxFAPLFWMzKwZz09AHY5/vT07slnMk= ARC-Authentication-Results: i=2; imf06.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=UkpYSvrn; spf=pass (imf06.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.96.58 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=1676918699; a=rsa-sha256; cv=pass; b=Ye7o0Vb4KTGRt/X02d9fDYTintnYnD3J6IZ5N2Gex/d3lBTMINoqMfTsN2SxNRqXFGvujp nUWNOG6rJ9LwRU+M+D0eAWOcAITkhY7p53Vfrvff+Ht0z7yGevZA9Pk9VqLjymi5JqvM5u eRR7ysWzVX4EYYJaoRAmO2FznCcfZyw= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YbVjny7Alf9EebGhN9aVJxLbPByfm8J6u+JfSKOVDSSVidiIgfRPcEtrRQlOb3SqKBhgM81OlZ8xmdAh5DQ5G4yz20d8B3TtvJhxkTDrrPmmXvjWVplIANeRcDNHkSBQ60uwvEjSUd1Qfidp3KddBpMMi1oCb21tvX3DdF4w34OUxtLRcSaHEHNK7dAiSXmtsoWlhQBWoix88tCANzPAYKBfzJ7Py3VdLz0lHFMI7JBQZpHCi7Z/A/6vtAoTuSfcrkymSqhTRsVVbhNtsOxIiseDIzD8Z0J5H7Z9kdOojkks/KYOe76bnvYtJsAa5WB3JWc8OkhB4CtJxczxwfmrnA== 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=F2kvs1P/l4ZozOxsEC0d23b+JIIcSAMRctM1la8Mn4o=; b=IxGZ5YT2jwwxB6OvNDv6aNoytVhqjEpAukNGcF7Oj/wcRea1w1X4n5Kpeae+3g8C5RSlXQpWonoyEG8NkNeJhd/1rACLbgXpbi7yczHfRKJusd2/CH0yApOYem7/PwMpBACFHeNFkV69bXgztJ6CXdTSpF/Zu+oRV6LQ6XSONeEs0rw2pHkehoqMxfbYakgo8eRXrvKmKTrZrjTswiGTlum2A0ok1SI4Px7NqRXc+wAvlayfgHJrQUF4mKjTYaYFGmCEDJpKnJMWPDuZ71BYc/4SHkbfLsdrvP3y8RQ3YxTZQulPrH/O67K/TAVM6j4ukeq2WrWGhZF/fdi/asTuCQ== 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=F2kvs1P/l4ZozOxsEC0d23b+JIIcSAMRctM1la8Mn4o=; b=UkpYSvrnwaSykb28m6Gtf6DYlINfGtBgD0VVYyKWZq4zJt5TIlpXaYZSPQm2+lqYs14ZzHnMaKJkf9P86uJZvzFURRxeKpAzNLacMGsX2QlKAonTlsurxzRfKeFSCLuwfUTro8PIsvzcmHMN/Zlwjmie927Ms0bsOPnLPbnz/rI= Received: from DM6PR07CA0062.namprd07.prod.outlook.com (2603:10b6:5:74::39) by CY8PR12MB7754.namprd12.prod.outlook.com (2603:10b6:930:86::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.13; Mon, 20 Feb 2023 18:44:57 +0000 Received: from DM6NAM11FT090.eop-nam11.prod.protection.outlook.com (2603:10b6:5:74:cafe::cb) by DM6PR07CA0062.outlook.office365.com (2603:10b6:5:74::39) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:44:57 +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 DM6NAM11FT090.mail.protection.outlook.com (10.13.172.184) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:44:56 +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.2375.34; Mon, 20 Feb 2023 12:44:55 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 13/56] x86/fault: Add helper for dumping RMP entries Date: Mon, 20 Feb 2023 12:38:04 -0600 Message-ID: <20230220183847.59159-14-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT090:EE_|CY8PR12MB7754:EE_ X-MS-Office365-Filtering-Correlation-Id: 26894fc4-1c33-4779-bf79-08db1372903a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: k5MHBK56RFlQC5aKaQjyU5GLAIjushZvHab6ynl6l11i3+wRrEl2YX02kTaow3mM3+Lsd06bQyQVAok4+l1mp3vk1KWnLmvFOebF0IgKl8vQb9zGTG/MXrq6+MHQoObiLfc3m16tju6Uaih9xKzs6s22s0O0MBErbQETRzCUsm7OwJkPzrLaBMhaFnzGURjUt2diAoazru6g+U3NITYG9xpI8WU3yoDZhoi6otijm+u4QNBc1ypnBFzaqUgEpdg2/kn2Dug6iLiYhJbzyH/YghFy6AzIpIrslIZpGiUiEnpSTWwVvS9NdMIvV7Oksxhr9GbPt4vT793MY52qXcSEMYzb+9CTqxZRbUT9RAbAqMPWC4GxD7U04/PTWHvitr/IQTxIoGxeFctkFlyVve20UUGZS3w3rGFgXky8Boo+LnWpuPDUQVPAxJzu+ZDFUjp7qr3fGaOSahlDr/lhPjBDiGGve9L+ADITEspKfa0p3JP1Fx82BbColqjKSLz/yNyQmX5uXiT4t6qXWvDFkDJxOJqjWEFk23qzqRgy02nIbkK1sAuWh0gRTl7rO3Zw4fSZEHLom5TbsLpB+pJLdh1BiLznMr75RfewTvkHy5AJIipWnbsZ/OOsS+X0D1FHSdH/ad5s8OTzHGN0Cy9x7goApcuLG4zKnvAaJsRG3QY2opbeJd9x+r5KvJtZKK5vI2/DXoQ7z0Sn0WZBXhkhDzQ727g1M8FalQ+qKWMUrVxC6ZM= 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:(13230025)(4636009)(346002)(39860400002)(136003)(376002)(396003)(451199018)(36840700001)(46966006)(40470700004)(40460700003)(1076003)(40480700001)(83380400001)(47076005)(82740400003)(426003)(36860700001)(86362001)(81166007)(82310400005)(356005)(6666004)(16526019)(336012)(478600001)(36756003)(186003)(2616005)(26005)(5660300002)(7416002)(4326008)(70206006)(54906003)(70586007)(41300700001)(8936002)(6916009)(8676002)(2906002)(316002)(44832011)(7406005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:44:56.9088 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 26894fc4-1c33-4779-bf79-08db1372903a 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: DM6NAM11FT090.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7754 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 8B75D180012 X-Stat-Signature: g74mgpunfchxacishbb1txniwei8xj4b X-HE-Tag: 1676918699-656586 X-HE-Meta: U2FsdGVkX1/ZGd+V8xXhKt5CuNJWOoAbwKDXHU9LckAso6ooOy6KvdF0V0K8+wFSVkbj2wy4rFV/MVe9TFtAulrO3b6vcJH1qPLFiPyVPQC5Oc6bBJYc818jcReT2Cr1ttSAezbjanJjJiIFsJZ/nIVGLPtgSfm22MKiIR39KTg0u58uF60vVWRP2/lauTsSKV2JbJgPkCc4AM/GCi+9KKWSNBTjf6cC9gqRmfLUJ7V5d8RkhAqGkqLsXU7OL/M8DNc+X+XCphGHJgDvk1FQukZ7FlrwGUmlnjIK0ZkqV7RShM69mvmSPEA0WXDPeXNevKSxVNjVYTADeWOdzBp6BPmVcNXvJDbmL2vD1VxQXKLkkahRVm1xBOXNwIOOsiPwNjjbUsbl8KVZjuTaCgMk7XNsheHSstObS94k/iWPSwTOj9QaKJVlP4ADaVGx64bBsK6I06OPzpcfJK4mqibpXPCJO2B6Nxy8DRqBIgOcLM7uaExUPv8oXLv/EJFOwuuL+h6PQJQUhZbLa4jscL7f8jSZZeNTegc520hyWfJZ2A0iap2AMsAM+2f8pak9O8HZ7+qit0FR63ekWtC0dRYI+DRceOzzA9K+iIPzqZ2uZhXTgqx1d7ENaV1w6/KY+ZJa7cpXw2PLzhxFhGb4TNtdDwbBoSnSEIADnE/l357fdm/Tk2FQHqBcWEJfJsKz1YRRD3wffLNj7pOpW/uJ9zPzK3U06uRfwoEMIgszPuRmqytdONfcjTbTfSz1IxpRQojifgyt2pNc23Lm13V+jtF5te/Q5Z4seGk/Mbf4Rt+J0vZScC5fnm/pvzeiRSl0xZqEkvqtpWMpAWEf/DXwFawo/U0lnsIlbx9BOz115q4n2t1yYXNRRocA7OZEgO4/TWau3AmK/EJwg71K2uBfQc52EGMqVNGO6dF+0/z6kFjUKEHA3rJhVnCIbox8IgAKiu2pkrB33qTB3OE1XSTqxH9 ltblKMz5 lIrwH4wPU/PNKCDZHrXXDak/em5Lf/m7NBEoCcwo+gaRr/XDjGCDMKkl2IvMLcomDj9PVDiV09nczoKbRd5N+/bWxbTtfwnSZ7PIq/V1nIg5kTzMfXFyuyerAeB1wgY8H+kEIgMr7IcR2bXLMpfmNCW1NUr4qiLfrumT2DLadjgYCNi0dkk6b58WTldkQrR3CzQGi1abgJpKcXmoXmb/89Y1CbVqRoqHZ1Qh8XRLdZj7K9TzeOWBOG4lANBdAkDhr40GXeOTjuHhTtL0CfNqjT5fEPst+pjBvXtUvzCIfCFP5NHliXyNNN2gCsIC/Kw5XnBpoiz82if/HHw8= 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 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/include/asm/sev.h | 2 ++ arch/x86/kernel/sev.c | 48 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 8d3ce2ad27da..edbb7fa488af 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -198,6 +198,7 @@ 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); int snp_lookup_rmpentry(u64 pfn, int *level); +void sev_dump_rmpentry(u64 pfn); #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } static inline void sev_es_ist_exit(void) { } @@ -223,6 +224,7 @@ static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *in return -ENOTTY; } static inline int snp_lookup_rmpentry(u64 pfn, int *level) { return 0; } +static inline void sev_dump_rmpentry(u64 pfn) {} #endif #endif diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index a063c1b98034..a01741c4a1b8 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -2504,6 +2504,54 @@ static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level) return entry; } +void sev_dump_rmpentry(u64 pfn) +{ + unsigned long pfn_end; + struct rmpentry *e; + int level; + + e = __snp_lookup_rmpentry(pfn, &level); + if (IS_ERR(e)) { + pr_info("Failed to read RMP entry for PFN 0x%llx\n", pfn); + return; + } + + if (rmpentry_assigned(e)) { + pr_info("RMPEntry paddr 0x%llx [assigned=%d immutable=%d pagesize=%d gpa=0x%lx asid=%d vmsa=%d validated=%d]\n", + pfn << PAGE_SHIFT, rmpentry_assigned(e), e->info.immutable, + rmpentry_pagesize(e), (unsigned long)e->info.gpa, e->info.asid, + e->info.vmsa, e->info.validated); + + /* Dump all the non-zero entries if debug enabled */ + if (!sev_cfg.debug) + 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) { + e = __snp_lookup_rmpentry(pfn, &level); + if (IS_ERR(e)) { + 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); + /* * Return 1 if the RMP entry is assigned, 0 if it exists but is not assigned, * and -errno if there is no corresponding RMP entry. From patchwork Mon Feb 20 18:38:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13146859 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 2FC39C636D6 for ; Mon, 20 Feb 2023 18:45:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C888C6B0072; Mon, 20 Feb 2023 13:45:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C38776B0073; Mon, 20 Feb 2023 13:45:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AD98F6B0074; Mon, 20 Feb 2023 13:45:25 -0500 (EST) 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 9FF1C6B0072 for ; Mon, 20 Feb 2023 13:45:25 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 70200AAB76 for ; Mon, 20 Feb 2023 18:45:25 +0000 (UTC) X-FDA: 80488548210.08.00B75B6 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2070.outbound.protection.outlook.com [40.107.92.70]) by imf02.hostedemail.com (Postfix) with ESMTP id 90C8D80006 for ; Mon, 20 Feb 2023 18:45:22 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=S8f43poq; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf02.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.70 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=1676918722; 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=6j+nSgs/DGvg/kRPK1aMPVbYjA2l54XSMEcjve6IfB0=; b=iV82gvge7fhHrY1PcJ5hO3eXL/eUIhnU+iSTpRorSii+tzmX+IrEMlhFkvHWWBN0uUyTDP 03IaVABRVSZOs5nGQIHe1eZkOXY9yBfM5TrupKiZUb0Vr8URbH3YCM9uXofmSxCNAmj38O JG46IpddPAEIaIM0a11Ecde5W2nZJ/Y= ARC-Authentication-Results: i=2; imf02.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=S8f43poq; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf02.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.70 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1676918722; a=rsa-sha256; cv=pass; b=XPl3KZavD3G03DI6guevL/NJsHYcxPPh3fc+sS3bsPDHAuZCzBnwGmB5Vex0jWeArRQH8f J9vH3dcNe5CR9gBnM8rMPKsWHgd+zDvk16zrJW8pr9a/945i1E0hp+GDdAQYDIEwDxZyz4 GbYkJMrocR84U/5DF41WgSdEctHZIbk= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Q7HWQHHJW/IMSA6hEFRJzptrlKZhPrVs7NOyo4oVnw5kC0nxz0Vs519oN6Qj0p/cmGOB3DPHbDndak9CoHVHhyCWQGRaMt9Cczq/VsGlHAyj86ZApAJaY8w484grokDpfBO5n5kzEzV5f6dVQ0g5CsS6ahwp7G3v55Cqtj0Hthh9WZDz9xi/HE/hHthjPytXiQ20m5L5L25h+bwDeSqLuu8wK5z7uBkNOZvyNdaU/9fs3sugcHen865auZpVHhZZeXsk3CQvna4OMe98acj0ePX6wWYebJ5AoK2A0OjtE8nKxZAFFQSrMga5QaeSIXpmMWdvVoTG0daGtEzeRWQnqQ== 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=6j+nSgs/DGvg/kRPK1aMPVbYjA2l54XSMEcjve6IfB0=; b=V6N4qiYCoAUXvq9vc7Lw2gKq0Pao0XA9ykOtxb3GFpSx6PzoqkAD+609O5AuDRD/E9JTxRJU6uYcU8ZIVAVuAtLiqQjboMMQ6qcoUvcL6bzipshGlsyCsJ/iSOwF4vIsUzAVbB8eg0NBmhAqhk0jeTEZ7sRpm8YbF8wtCRbPfw/fk7dJ0i3s/Y6pKrhIrjBS3w2xo+NZaYL4JQOC7aljNYl/+IB15kVNs/IquvqYisk8UHXV9kKRwRWUmj2TbChljaWvflE0KDi05MBykQefsmus19o7SXwgJ6d/xmf0QIQjz7n/n2OxC8NYetNYeRXVVxiMsl5DPUDi/VrxIVyzGA== 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=6j+nSgs/DGvg/kRPK1aMPVbYjA2l54XSMEcjve6IfB0=; b=S8f43poqY6Nyp6QnkCEQYzf8yFPtylMKIqDNjT0+BY1i/EZGom4RVwMioH+Im+cOPwMD3i09QEGCIwZ9ezp2kGBQBOWb27C7z2/uYQ3QtGhlELX2GzSJkxTuBcTEvcPp1nZvNHx1O3oXrR4tcPVz+4kgC7GBLeRh6twPIGCojsI= Received: from CY8PR22CA0006.namprd22.prod.outlook.com (2603:10b6:930:45::10) by DM6PR12MB4548.namprd12.prod.outlook.com (2603:10b6:5:2a1::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19; Mon, 20 Feb 2023 18:45:17 +0000 Received: from CY4PEPF0000C968.namprd02.prod.outlook.com (2603:10b6:930:45:cafe::d8) by CY8PR22CA0006.outlook.office365.com (2603:10b6:930:45::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:45: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 CY4PEPF0000C968.mail.protection.outlook.com (10.167.241.72) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:45:17 +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.2375.34; Mon, 20 Feb 2023 12:45:16 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 14/56] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction Date: Mon, 20 Feb 2023 12:38:05 -0600 Message-ID: <20230220183847.59159-15-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000C968:EE_|DM6PR12MB4548:EE_ X-MS-Office365-Filtering-Correlation-Id: 4c7b35f8-8bbb-4c44-7e6d-08db13729ca6 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: fVXsoiXv4t1w41x5qa0C7Di15m7ZevA1abEVi1IH48Myl98XNk4RHw9YlY8o+yKPDuCPMTKeRK35yGzKZK8hvV+BOWzuCb3wlpI8LOaLFTtRIb5iRJc9kH9srYpPL2F5tmNGMyWw+s9IfoNuryac4kPI33Df3mbwTuErbIiJQrVGofmO7Ef+25VZhgekoQwgtVMuz2JnY72cyuLnHMGtX4wSDCxaz4LY04EM6v9LUj0Ag8G+zlAseKtK7BnCug9GHwfSNHtLBUy6pfN+8pgyMxx/ZBhMDzrOCVhLm5Za/uSvwaF1/moWst0Zk3R5eobQ2MtOem0rKrUXhyr34uX02KwePqvBP/cC8RWXgSz1JHIhASujdmDiZ1A0edqkLe/WKA+CT7o+msWXza9x4WFBOHY0RBAbHsfofTlRG3jVMA/ypNvClqSkeSqLByGBwgt8HKnu0DsqXMr84ineVxlUiTGkJAXb8o0COenPLr+xoAMSGi1DGGx47ThoHAAoEbYyEZTmy92YTbZip9n7BmjADB+Mp+wk585AzzXXMWz2PWr+SY3Ii4F8jk4NbijgguY1v4NdE7SSLvutRmpEVIBJ1SzjxzuHrBajQxXyiRcRr/PSFvSyeiX5vLOKRhNPHJ4K0D0d23PTu48d1iZhQpSMY2WWEcbdr/SElAkH+a9bl+JE+6pSDfK+9pyNepGl4gHvKFTYmj1RBjikbRgzXvtdMhFs09NcaotK9RowmzjnJk2Ad3KTbYb1ITvK/PZ9+wdFrGxzT7fcwpnFdsZZ3H006A== 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:(13230025)(4636009)(376002)(136003)(396003)(346002)(39860400002)(451199018)(40470700004)(46966006)(36840700001)(5660300002)(7406005)(7416002)(36860700001)(44832011)(82310400005)(82740400003)(86362001)(2906002)(81166007)(478600001)(356005)(336012)(16526019)(47076005)(426003)(186003)(26005)(36756003)(40480700001)(70586007)(54906003)(40460700003)(316002)(70206006)(2616005)(6666004)(6916009)(1076003)(83380400001)(4326008)(41300700001)(8936002)(8676002)(36900700001)(309714004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:45:17.7319 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4c7b35f8-8bbb-4c44-7e6d-08db13729ca6 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: CY4PEPF0000C968.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4548 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 90C8D80006 X-Rspam-User: X-Stat-Signature: bc9dmq3xtep8f9wr6gj6ofzn3jo1cscx X-HE-Tag: 1676918722-121528 X-HE-Meta: U2FsdGVkX19ZMUKzWvcG3idZOy1BMuUw2pDJOYrSutaF/jfKa1dBGW6/6chH43DTHWn5/InEvWPpzyqvDNe2uKdsOEKnGPcYzmv0HEHx95kqIvlhjcQYDw/uCJhUcbklmqd9xt12O0KHguLgPdrre7Rmk/WDfG46EjdSv+oJ5X9D5LvtS6cQ4+ezL/F6vPwXnadJKA9TEMleYN3AN1wnS/hUM2sI/2YmJyCfXkJRIPPZ4UelZmRAKl/1dWoE4kkbMHeR9JjvD11NZqyFwzlf7HqWfDexxlkAIwtA1UiBVLCUnOPOrM2YfvW+RJ0ztc4Rklahi6pYQ4fZMn/vpSgMP9AWY0v732iaJ+Dq4bsAFzQg9BKtCGlIORzrlHVrmTjLMY0DiNgVmMtrDOPsEUaQlUr/Qb8AqmVcSu0WVLu5c/Er2uYypZ2CDPIKZJUdKSRC3fwJxXJPuAIfLYwUADFCWPczpwNa+hcAGZhp6su+7Jd4tfqs7mpr5kzYiS6gmBBWCDHQ4w0R6mPW46inByyDLpgVtzdEjCWYY+dVVCb1DEnUOWFC0PJLnF/ssCwu0EE5B/g4adGGcctGaZ0aSCr+DALxoI5WqrOd3cF3ZjxNtbQcEOFnqFFPyGCCVBL4VTK67TAk4Drpf2AR2z2Q39U2PSpOFDlNge2EM0n2rn/mg4zxlTcp7yEO9Z073Rz8ZOVEA/5kSw9GwDFu5Hh6qWlMUQguCni2xaZYQjGo7RGh+KSdjncgKYAiXepg6qDCiE8gcKvXyKIl3NK+IKiTInW+cPBSjwHYh4m9mVrKjVT9x7LPP6mPWSNpNKUsTAbk/cyB1JyF9Ir3Xgq6oqS/iFau7nsm2fqWkFnAs09eL6sqWldgEeU1NUGUfWlBJ3loQECeM6zDCFMwALjZyiDMQ+2a2l1UhRNQAfmHC6IuY59sLbKqvTOmaz9GRzFJ2TbvFn+YMPhOc8wP/ufPGiildFA 2VKfTBJn qS8uN+wquvJQTP5tVdqaMKRauop4Zze5xMDOoC8MDgPE4+GrinSfzUm9mquOaw186CpsyfSIL/Qk8hVMkL/S51hlOxrUvY5/7R1MyWQEH7eV9WDMT4NWghzXIbYwK43TcGK/sr+w9HwrnPvwj7dClgR1HhWPQNY9ntWSTPZErL2jydqZ2l8MlscoCKb56r5Ml9F9rD4WEo4fHkxuQFvOGlFpJ9DECY1iRVbz7B+plEOkRZfXefckeQ8qo3yIL8gZhPnNWsFEU+4ChRrCUTS6Ib9H0fB5gvFYLakS/kLgOZ8ndPcyMjuZ5MqtSb9R+xWyn4Op9p6bLSiPIw2E= 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/include/asm/sev.h | 24 +++++++++ arch/x86/kernel/sev.c | 108 +++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index edbb7fa488af..7d728b30319c 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -80,10 +80,15 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs); /* Software defined (when rFlags.CF = 1) */ #define PVALIDATE_FAIL_NOUPDATE 255 +/* RMUPDATE detected 4K page and 2MB page overlap. */ +#define RMPUPDATE_FAIL_OVERLAP 7 /* 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) + #define RMPADJUST_VMSA_PAGE_BIT BIT(16) /* SNP Guest message request */ @@ -133,6 +138,15 @@ struct snp_secrets_page_layout { u8 rsvd3[3840]; } __packed; +struct rmp_state { + u64 gpa; + u8 assigned; + u8 pagesize; + u8 immutable; + u8 rsvd; + u32 asid; +} __packed; + #ifdef CONFIG_AMD_MEM_ENCRYPT extern struct static_key_false sev_es_enable_key; extern void __sev_es_ist_enter(struct pt_regs *regs); @@ -199,6 +213,9 @@ void __init __noreturn snp_abort(void); int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, unsigned long *fw_err); int snp_lookup_rmpentry(u64 pfn, 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 void sev_es_ist_enter(struct pt_regs *regs) { } static inline void sev_es_ist_exit(void) { } @@ -225,6 +242,13 @@ static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *in } static inline int snp_lookup_rmpentry(u64 pfn, 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 diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index a01741c4a1b8..a49f30c10dc1 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -2567,3 +2567,111 @@ int snp_lookup_rmpentry(u64 pfn, int *level) return !!rmpentry_assigned(e); } EXPORT_SYMBOL_GPL(snp_lookup_rmpentry); + +/* + * psmash is used to smash a 2MB aligned page into 4K + * pages while preserving the Validated bit in the RMP. + */ +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) +{ + int max_attempts = 4 * num_present_cpus(); + 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++; + if (ret) + pr_debug("RMPUPDATE retry needed, ASID: %d, ret: %d, pfn: %llx, npages: %d, level: %d, assigned: %d, attempts: %d (max: %d)\n", + ret, val->asid, pfn, npages, level, val->assigned, + attempts, max_attempts); + } while (ret && attempts < max_attempts); + + 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; + } else if (attempts > 1) { + pr_debug("RMPUPDATE succeeded after %d attempts, ASID: %d, ret: %d, pfn: %llx, npages: %d", + attempts, val->asid, ret, pfn, npages); + } + + 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); + + if (!pfn_valid(pfn)) + return -EINVAL; + + 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); + + if (!pfn_valid(pfn)) + return -EINVAL; + + memset(&val, 0, sizeof(val)); + val.pagesize = X86_TO_RMP_PG_LEVEL(level); + + return rmpupdate(pfn, &val); +} +EXPORT_SYMBOL_GPL(rmp_make_shared); From patchwork Mon Feb 20 18:38:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13146860 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 6FCA8C27C7C for ; Mon, 20 Feb 2023 18:45:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0EF476B0073; Mon, 20 Feb 2023 13:45:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0A0166B0074; Mon, 20 Feb 2023 13:45:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E82D16B0075; Mon, 20 Feb 2023 13:45:44 -0500 (EST) 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 D6AF26B0073 for ; Mon, 20 Feb 2023 13:45:44 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 700A2A0339 for ; Mon, 20 Feb 2023 18:45:44 +0000 (UTC) X-FDA: 80488549008.08.BDF95BA Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2077.outbound.protection.outlook.com [40.107.92.77]) by imf19.hostedemail.com (Postfix) with ESMTP id 6B43C1A000A for ; Mon, 20 Feb 2023 18:45:41 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=iTRJU2R8; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf19.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.77 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=1676918741; 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=YbUe5MPV5IcipfQc+HmmLC3mcPpmVfsdPBM6AWSlV6s=; b=ohUOwOdBWc27allH1Y74wjB9F2qhLzRP7klhCIvx8xNBgLX8LQf7zMwZirENsKpGgf+Hsn +dRKbO6Ys4G8/OnH4KqdXU51ONX1tSmKnHyrNShWB2+2F1i3c5OiD3jzqdmvkr0bhoMeL7 nwgSOej0NGshrPqBmmOlEm8iC0F2qYM= ARC-Authentication-Results: i=2; imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=iTRJU2R8; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf19.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.77 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1676918741; a=rsa-sha256; cv=pass; b=YK3Hb6MwkQ8Csy6YWUq8vEOAtucyL/d0NLBIR/G32Mhd8pWaD1cXoDYo7KbsR6LgUb8hHn JiviGsw+NNy01cHjrs1Evz8nnRmAe5EGw4exkbd9sCXVKq+7M3Qkr3d6SeDuEQtjFGp14C Tkw7LFzPycQP6Hvz/Toc0WZ5m91Lr/c= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=M1L6SGKpp9ocXlZdXp0N3Us/tZIlA6KIrYrwWnF92htZIqOJZF4vtspXPSMe1EauxRmKP1BW0SUVDmZ0+WPd55iH4u7yiibHzTK1blVPioMqON0OdL6LXMVRwpuc4ATknu/tEg1Z+IRKzRtjboq3UzuX+hG4YQ8o44gb4uiPTkptzy4zSkvBz+gmOIbtw+gi6r50Xaf6N50CTruN30O5JHLHGstsfsA8dwpqVqdXLS4rbQDdobMPN0N0HCL+7m5kw18JeBPijm3rgcWyYlwt2iMMXXXYMv9d+jxrgBBoiS8jmTUrNET8xU4/XAae2YmTJ29KaFWPywVNjhc6y5saAw== 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=YbUe5MPV5IcipfQc+HmmLC3mcPpmVfsdPBM6AWSlV6s=; b=IRSDhQPsurvL+0oEmU1QqbLhgeyOe4GmR25ISNi4U/HrFYxBOwhepntf8MBTzKiHDjL4L+eBHcJlaefI578i+8Np4Lmq4re6I6v9HbevQEz6crOY6/2eM9S18SCsOmuQV3O/ootKrCV8qoG81cOUL0mZoKcPImkuLa3FN/8d3ZHGHh2fQypEnPDtaqkALf4KDiRbcZvAGVvYmKWvKBaOGCDdbT6fmE8Zxy5bqHoZYLNs8hZMGeXKgQWg30zzpEEzGtkDor+GU8sWBgpTsbSFP1x8v/qVcE0ZW+BlbY/eIZl9pZMG3EtAr2OOVVFXDUcSZ5O0vIWtfEhagyN+cbPV0Q== 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=YbUe5MPV5IcipfQc+HmmLC3mcPpmVfsdPBM6AWSlV6s=; b=iTRJU2R8wg/ieKSHtq75FVVJyby9r3WrsNP2qRqVGZdCQ0ReWqFvt0KTdUUYA43EuQp5JdqTnHSRZsaxQE6MPW0TED4mfR1AgirJgPVodz4W/JLJuguMs7AawD9g/VodkUWU4inkI4v1cW88to9uDZMKYdd1TanGWKfyE0KoxVU= Received: from CY8PR19CA0002.namprd19.prod.outlook.com (2603:10b6:930:44::11) by IA0PR12MB8277.namprd12.prod.outlook.com (2603:10b6:208:3de::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20; Mon, 20 Feb 2023 18:45:39 +0000 Received: from CY4PEPF0000C96A.namprd02.prod.outlook.com (2603:10b6:930:44:cafe::43) by CY8PR19CA0002.outlook.office365.com (2603:10b6:930:44::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:45:38 +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 CY4PEPF0000C96A.mail.protection.outlook.com (10.167.241.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:45:38 +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.2375.34; Mon, 20 Feb 2023 12:45:37 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 15/56] x86/sev: Invalidate pages from the direct map when adding them to the RMP table Date: Mon, 20 Feb 2023 12:38:06 -0600 Message-ID: <20230220183847.59159-16-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000C96A:EE_|IA0PR12MB8277:EE_ X-MS-Office365-Filtering-Correlation-Id: 4fb6eb3b-8dec-40c1-12d5-08db1372a920 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: jvQ48W5gzOiaKIfldMU6eOipFaHDQEN93lXnmKlUHh2g+hjw59RYzpTOl6yfxSpM658lv8Ak+1WPE1wxnjpLbn28GPc7vS5XhlrbbGWs7N2R13OXOVmMIpCSSE/myAqZHlLpRMYLDUFPumysJQN8JwMfyoyyE7yFd7OxdrOsQ0WZUz+jz2KFluvVSib8PyCmd8JdBmxK0VrOmQGCtF2xcxg++jcWuEKLOuZgNTCvqEvORV9HFc0ocAjmUjFn0I9ydZhW59kUf+khoIyv2+wWuo95cBcxSO6QVb1zveo/1jjWBLbIVfMCSPgJ8IZcH4WKzqZmYU8chcKbbcwbWG6DrpA+XDvUTxqR8VFjBILp14HveGQCa9JmcGJLkbY/NY7APZJzJphNZKTbqtixelV4MXK+EPb43mdnMuQCtWfPX5F0aqTx72hb0FxMmeVbNGwgy63bsPhabZIfMay0GJaeTzNLEjlpZ7LvHCGOR8HAqeYzZhrozdvbQ870MzEOLlO3eGP/yxbk/Ewxffw8ltg4BEInhXQkDtO+43nvNTmzkiz3STsMveutICFcfQ93gztjx7T6A0ImgeOLkHItdCKFx8sDMTtbSD+Y/ueg/leOBSsb5sEmoRswfsOyMwTMP5lxa20Z5j/+lV67QNzN//iJK9dh2uwg9eBAz20oD1lfptk5niZhE7+sxv9MffqDRwcYrzjic32iBdN7/qV0RCmRtzw97WUxUVvHfeFEatL2PqY= 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:(13230025)(4636009)(396003)(136003)(346002)(376002)(39860400002)(451199018)(40470700004)(46966006)(36840700001)(70206006)(40480700001)(40460700003)(6666004)(8936002)(83380400001)(7416002)(7406005)(5660300002)(41300700001)(47076005)(44832011)(426003)(86362001)(16526019)(26005)(186003)(1076003)(8676002)(2906002)(70586007)(6916009)(316002)(4326008)(336012)(54906003)(478600001)(2616005)(356005)(36860700001)(36756003)(82310400005)(81166007)(82740400003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:45:38.6770 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4fb6eb3b-8dec-40c1-12d5-08db1372a920 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: CY4PEPF0000C96A.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB8277 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 6B43C1A000A X-Stat-Signature: rskjnie871ukx14owcas84w1yn9rau65 X-Rspam-User: X-HE-Tag: 1676918741-904601 X-HE-Meta: U2FsdGVkX18cqaXyKwueVhUwyDaqGgY7vexkBteupfwjkNT6EiMHQgwO2SqEoOKg0vySF9ARylsy44kRCk9/rpy/C1+eedmxy3Geuxf/NZiXwwnilMdVd1k5s1tMkXv7VGwW1PNc/J09Kk5niNCIvfvgMSKJIL8sLRVPAzmtkch6/48mj84WQlaAsH4FJHaekUewPz1bOA2LUtd/lSoca2Smy2gwm8QUN7tIiQ5rkxrVSTvCU1PTpS7arGbF1HH2GxYA9HINkOGVZ06VIYCfysriD3+CG45mwZnW5tPvc39uGSoM5lRRe4MwbaCIWT137sHYt0TAdo+5Q+gOid8sjJzaqSr6N/qO/TgBYHCcZUvwpKkM7r7gFa8tHpAR5cyDczajSd3273OrJxoJvKbf0yMKBNt9+WL5SwePqW/61i8nmd6M/nD05xFgv1hENNzwBK+Wy0xA9a7o1SVout4AJqBlCZxoOOTB2bM4ey5FqRcJRsXqext+dm36C+gQXXaNjcbWFm3QOV/N4w0PFMnYKCt3tm9Xt15YX/oSzZ4t0jUjdDroizmtJsClFytScqWuMwXocWkrkcoV1buWC029tLGeX19biUiUrtVN2JBQHftbyNBwG5ydtaezNCsvrsD2GtFelX3FDFR1o12NwDSBBPXNgpeg98FQ78NkK2/w5wq6B5ZqV6JFu4frd9+MEyAkvPQaCcxdLht3uZh/f+5BeVDdPIYKbRYwcRMxMqTLSU2pi1X4P4ojdNpjKKlf1h6qX0jgU7C8bgExV5KBtZa89W/yMXXGSePXk0zRDq50t8aX7z/lx5/D9/VvWXX2jRODzyx3ZzotAzQ9gqCF6frC+Q/5mhFUhQOpZ6pGv0bQ+/uf2RIg5FQidx3gI1CKYdCp8V1GQ4RBxxTkgTENLPrVE4Ijse0vSqaDjUJrDN7bqF1KQizPCrpxnB+cnN5ah3l6H2wYkXaFRvd8h5b4daE QH36EGIC I1VoVhApsTWaw7kjkexUSggsoMvFb/vt17u5m5wSjDV71/EE/yZRRiDGMz9RjewFCt4YXsBP0DxJzJXbS6fLg1UlSWXcHYhEM+5Ze4QPVWtJ1K1k/0OltgVFWm95hf2l8ABIdyDeSyS9+E44TGCQcEDon5MMOsTMOoPk9wy6VPclydkqnnlW0MLJ0BS7M9hxMNm5rOrlDtmbHvU3aoUa5dRIyaA8rjIjqUhO7Ga2dMFeEpwjYRjaPlzjXutwoXyoG0/hmsEzrc8JrvxFDGvY0VCYjKsbcxv89OEcBpnSLUj5PHDLjO3/CLqpSRD9ZCGfCSXNp2rVViCKhAeg= 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/kernel/sev.c | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index a49f30c10dc1..3e5ff5934e83 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -2595,6 +2595,37 @@ 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) + goto cleanup; + } + +cleanup: + WARN(ret > 0, "Failed to restore direct map for pfn 0x%llx\n", pfn + i); + 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) + goto cleanup; + } + +cleanup: + WARN(ret > 0, "Failed to invalidate direct map for pfn 0x%llx\n", pfn + i); + restore_direct_map(pfn, i); + return ret; +} + static int rmpupdate(u64 pfn, struct rmp_state *val) { int max_attempts = 4 * num_present_cpus(); @@ -2605,6 +2636,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" @@ -2630,6 +2676,17 @@ static int rmpupdate(u64 pfn, struct rmp_state *val) attempts, val->asid, ret, pfn, npages); } + /* + * 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 Feb 20 18:38:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13146861 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 CDC33C27C7C for ; Mon, 20 Feb 2023 18:46:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 360156B0071; Mon, 20 Feb 2023 13:46:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 310516B0073; Mon, 20 Feb 2023 13:46:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1B1886B0074; Mon, 20 Feb 2023 13:46:07 -0500 (EST) 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 0E1CD6B0071 for ; Mon, 20 Feb 2023 13:46:07 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id D9CC514077E for ; Mon, 20 Feb 2023 18:46:06 +0000 (UTC) X-FDA: 80488549932.23.DEF4C8F Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2055.outbound.protection.outlook.com [40.107.220.55]) by imf26.hostedemail.com (Postfix) with ESMTP id DFEA414000F for ; Mon, 20 Feb 2023 18:46:03 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=5ScSu2nP; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.55 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=1676918764; 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=LaE99ZyE9fPNXP1+sCNkKPB9OJSZnnK2iy4k/8TUgSg=; b=xiltyttqYyQQh7Q6AmemWMlO8tSqVpeSUbnb147P7EjneyhiqVvt6920tQzr8LpRbMvbMe Q5as/XXm+6oQCUAMpIg/8XUO5Upg29EwlIYENUeZACmGJfq/rEKeLukIDJcTtvB5XVf1/U rjmH0pcUsREJNbyI1VAg34uMiUQU97M= ARC-Authentication-Results: i=2; imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=5ScSu2nP; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.55 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1676918764; a=rsa-sha256; cv=pass; b=LcmmHV/wQr69+K3mnqF9mqndI9Tar9WEyQML48EQoaLnFsDmxFFOS9tgg53U7E4B33yGXJ 65QBVuBuaoLOUGc9SN2eGlFLklLPfRc/zVJbeO8AQtRH5duqAsY2USsAT68XGCkkBaSpIc Dw0COp0F7VTyi1lxnRULzWVxsiHRKw8= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ib+QpV4dtiCPvitAowhXJoqt6AuFnZRb9Cc94pbBK9x7hTGF6yECS/qxVCdatV/8+GtnzkFdjiWXZWjZnZmYzg8WNfRVP/6w3MybTuufT2oAKTW1LNj7lgTtpILW61meN9CnpScb/O8ZjgbiaxQgiJZqvmv01eZ4Oleq4kCfpw1TeQ1oDZAUVhtlTSNsB+U3G4UwvGOlvTmtXaRmkjiaFh2aC3zrdsFJr8Wkq0lDA8hE7Z6KrP5GblzNOQOvOE0l5WkYZwbs7QFpG+loeQghwe9R4+VRe+nN4tefxps/AbEcKj0OdxIFJHgiGH4AKnJ3GoHK/jHLZTYZlwqdirbjkg== 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=LaE99ZyE9fPNXP1+sCNkKPB9OJSZnnK2iy4k/8TUgSg=; b=I6Xjea3pj2aKHKhdSkxjR6gikxurOur4IP+GjkVtmHO8SLiA8rmGq0TrBsNCS58TGEcnnJc9lAa8vSBi1jRJaUq5KycgJhaUNPOf3XfCoAlc5irq1GOhCprk++gei0UlpssKFSZyoQjfbAr9DpzqVfqmFkJlQW5fkwldFmhbWTyf0Ll6Kt3hTG+U9mccWcTsAYa7PpAMBg0C4UYhF0/rlavHlajIlZa3x/s3PsRceZEs79/ILZyyXQcraixFu6SYBjDcbubq1Vf4E3MEomVpftTSPIfQimk1i/XbvPq1nNRVWTZQpjicCP+grlaHkLkaKQZWWnrP489o8m2L9Df1xg== 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=LaE99ZyE9fPNXP1+sCNkKPB9OJSZnnK2iy4k/8TUgSg=; b=5ScSu2nP8vwx48ps6wK9Q7/C+pleM5XY3oVqWseQHPXeeNBGgZ0Ai+qhFcAuWAn+0iuU0ONOadc7mj1JQq+U77QpbHWjaFCDcZFdJGOhfbRFPHXp5coXavZYRtFRnSnOs8UVELiuQRwGJD8nyhJTePhtlu2vZwk/XI5L3f2eDIc= Received: from CY8PR22CA0016.namprd22.prod.outlook.com (2603:10b6:930:45::27) by SJ0PR12MB7068.namprd12.prod.outlook.com (2603:10b6:a03:4ae::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20; Mon, 20 Feb 2023 18:45:59 +0000 Received: from CY4PEPF0000C968.namprd02.prod.outlook.com (2603:10b6:930:45:cafe::3a) by CY8PR22CA0016.outlook.office365.com (2603:10b6:930:45::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:45:59 +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 CY4PEPF0000C968.mail.protection.outlook.com (10.167.241.72) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:45:59 +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.2375.34; Mon, 20 Feb 2023 12:45:58 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 16/56] x86/traps: Define RMP violation #PF error code Date: Mon, 20 Feb 2023 12:38:07 -0600 Message-ID: <20230220183847.59159-17-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000C968:EE_|SJ0PR12MB7068:EE_ X-MS-Office365-Filtering-Correlation-Id: 33661cb8-979b-4837-8754-08db1372b597 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: FAAeYIHurIps2K/JbO09YdHrBe1AjJsKNqb7GYwz2aBSj7eW6BanckDmdj6Cys2OwC7BKay+bs1YmbEM2zxGgq2yMEAEaOGnSbsUEDuv3mEwI252KjeRpRpVD+VayI3RHaEa1IpNJ5KMnN35J1yQeZuqln9KzGKGSEdIoJhVoLbqrS87itrEiGZA+/rj8Vu3Kqo6ngIQoc3Y7C7CWOU+Kxtt9Aq+4TU+vyqbtQboA/RvexFOnNJwktWT9fRvK5T76FQCnIWoKPvy1gsWZpcDFN8Q1FkQms7c+Kj6/Ij4idDw90JJC6YkatRVI7EznC1L1lac++F/Qc30pEdmuTfkxb8aReuI9cvqqSEeyner+ieaHMG4yIzpz144ZdgRSOVzB6CVLr3Sv7QkuyO7kOf/LiSEfb3JsBNFif3mSbtNM61UQz5Sd53vCcugP4SyQaggJNHYDuNADFwFQ6pEpehRCrPYzdnhswBw8CKmUKRfbNZG1n5q58G/cQJxPoDwXSmFLHPnw/XEz1QAnKuoh3DxUuzffgysoWnQ/LAtZdOPmvT6xHskWptFOw9Cv9UcylqKFR69CIpOgXM3y99z3QGq5q7vzGTe3/YB85Ze4OW3a5Vmtc2QUlhVbNxyAsMnDEEwgXl54MduP0w4tfOqshOk/cPQfBl2aSKsZ6mm2HlYJ5WuoWGuvyCfHo8K7xJP136skBVFXPwO95u1d1/KbXUj3ZBx1QKa7vIZnS8m2Zv2TlE= 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:(13230025)(4636009)(396003)(136003)(376002)(39860400002)(346002)(451199018)(36840700001)(40470700004)(46966006)(82740400003)(81166007)(82310400005)(36756003)(356005)(36860700001)(16526019)(40460700003)(40480700001)(6666004)(26005)(186003)(70586007)(1076003)(70206006)(4326008)(336012)(2616005)(478600001)(54906003)(316002)(2906002)(8676002)(6916009)(83380400001)(5660300002)(7406005)(8936002)(44832011)(86362001)(7416002)(41300700001)(426003)(47076005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:45:59.5769 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 33661cb8-979b-4837-8754-08db1372b597 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: CY4PEPF0000C968.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB7068 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: mguqyeh66ysrmo8gks8ct9wyqsezfh15 X-Rspamd-Queue-Id: DFEA414000F X-HE-Tag: 1676918763-818623 X-HE-Meta: U2FsdGVkX1/uFYp8yjIvAvzhpTXzz1JYmp/gq3LdZBgi+arW2IOVVkz/3kdwlLc9WKxs04mFg2QxYXveiAhxfwKGaAjCArC0aXO+OXibeComDe814Pb7FHzIMQSlUe7G7UfSNrRusMZyjEasbagC+KjIvs8tqDCruDfykJFgSQ6UNxb+xjtmHVSEKTcuKb2xESpbR7+O5CfghVBxVnRZHMB6hzC5Zr33NieHAZNE0LxQLsVHmmAYdBJmXSlNH49PnDv6TlqN48BDHRnT6AtdqrjFPGj0WMzHBjhkCONW2fYTT90e1S2sdh34bXGcLLD1n4LiJ7RiSNnOaL1x5MC7RsOiC6VoOz23FjZAttl1JDLgpDyr4hfBuBFbX8zwZvZgZS9rABwqZUA3iAk9fEmmRMGqO9/5quS4JT9HDQU3SG9GsnuKdwAehN50VUl9k90b/d8UfXdlT1ERReRcAOB8yl906AAkqJkIc3EM/6CJm+Cm5+/TTbO+wZ/dMI37k6x0V03IbJRIyVQyVtgtN4SVz9p4lWjpaGhFiA9yIHF6SqrmmWUytRZG9rxqWR3qGtsxQaKJ2c7uS0InRpN1sHgd+yV1pnSOC5oSG9HLQWvmqprm84GJ6ueXn6e+u2QNW0KzqLvA5M7S0Xq3rieaomEI37XZhcRzHa32VTIaaGKdS059w4ch9GoAK6lLjr4bK8g1Dk5Pd7fcRDfFjr3TtDOM3YxTrsTN9LyBfKb7BIoycpLDLN1hH7UeSFQp8WDYB3hxkrJ9uzELZS/hhAZaHWlMuqT1JfTnilKgJ3uzhhZ35NRingD8CXHlUil/dOZEeQpmPdG6qvAVWMVhWxhndhUMsCgkQ4Hp46KbHholPk/eNMakhkFRzODtJHLtUOjOSNFxuD7KgF1oCeAiK5Dgrc37b45BWTr4dKTB85GAdacYW+ACdWaHU64C6UMp5mQyKPeeHtgpRRdowdFbgFrihwG /vXKmytw oISc2Lz9McrNikJkkeuBfS3YaTOkPLjHjP5cXiVenwlqrQKcrD4JJuj/07lv/yPgDeR21e4gQ0dpQ7DEIMxdZOL1xqWg+8VUjuwlAAvsk6WQNMv28Lu4OlzPh+iPpf785i8WNIjgAFC/xK5wsIv9csk5ZlHi3FpO5bV0bsErT46GI5WrMXji+V3MLdOtBm+gIsNvypkZT+wgXzmUz0K1QihqvhhGZ9Upv+k7EyuufNcmVFkD8uNN/mIU+jMAHDlJjjMBt2tWX2IhU3joWzlFR8aG/uSULzLwdaYZq1nOooecM0GXju7h1btUoRIixa9iPYAeCnzFT4SomOQ0= 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 --- 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 7b0d4ab894c8..f8193b99e9c8 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -567,6 +567,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 Feb 20 18:38:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13146862 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 EC0F8C64ED9 for ; Mon, 20 Feb 2023 18:46:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 55BE16B0071; Mon, 20 Feb 2023 13:46:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 533466B0072; Mon, 20 Feb 2023 13:46:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3D47C6B0073; Mon, 20 Feb 2023 13:46:31 -0500 (EST) 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 2BD5A6B0071 for ; Mon, 20 Feb 2023 13:46:31 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 04CAB1C5CE9 for ; Mon, 20 Feb 2023 18:46:30 +0000 (UTC) X-FDA: 80488550982.04.A5FC164 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2052.outbound.protection.outlook.com [40.107.101.52]) by imf19.hostedemail.com (Postfix) with ESMTP id 002521A001E for ; Mon, 20 Feb 2023 18:46:27 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=PaKvaCtb; spf=pass (imf19.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.101.52 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=1676918788; 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=D0uOF+T4jZHmuqcz209Qddae1NNTWTRVV73luMTut3g=; b=T8AD3YA0NON0+c6i6bPGCNCPdDt/t04mGfCojMR4CD/RTgzOzhaYv0bPNzmV2qNaXesXpz ByU8JoL5AgaItlkYzkRdMidbXsd8b5c8b+YiOD71xeQnMtxm18QqAV1PmOSSbVVCnkCZPy OaUlzlabj2AJnViP6swpmknqptdblqo= ARC-Authentication-Results: i=2; imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=PaKvaCtb; spf=pass (imf19.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.101.52 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=1676918788; a=rsa-sha256; cv=pass; b=NBQy6xKgIa0BIn0S1f27CvIefZH7BoYpdKnNVK0viZ8AB2drqqAGlgmKv50CGu+D4BaRbR YrHS9V4yy5IjXPJQalQP4sJ0A92yoFEapr7lE48VFKy80utNW0DANhYbB5ZgxgyjeYTLYw BR3NtIORKpi1SPV6WE80Ga0ROwVLrtQ= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ijAMP9dOwsm1No4MWGXlYrk1HbG7w41UFZyAfUrqQYzJcQawWoSQjhyiGiYxPMWE5Iyw6pRM0t3L20dyVzZzOKp2ZreBRXDoyCMXKPUgvD1wDjbLhJHDih9jW0pjzY6vu/EWP3JcAHF/4zf0DPDBHUuN6dxDvyKj1Y14JDT6acdM3U4Bl4bmz8C4q+5aIIPLURfZVQVAvOlXFcR1lVOp9VwoqaJAuJNXB7gNIMB2DAWI5NPer14aTop503DE9FBQpjIMZforu9icwAL6PILHbtjgKOSFOujTxy9S8kiZwvE/xmtiwwIt/WJz/OaGjBA7Toq6ifdVdYecfFOzIoLphw== 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=D0uOF+T4jZHmuqcz209Qddae1NNTWTRVV73luMTut3g=; b=lafWjtZybMxuXjgQouaJBdObEhfHM5xAcD9JQIiJ9Sv5ZSJUsY9VQRDKGUs4JrcMMvOhVLYyyX0kMbKtaHbAoL7NRWC/GTkb3RfGqjWMoGb/KliQC894dlvnVuY/E49FTpu1LKrT6+IbGcQ78Hc20mQixHyiVFGbVqGR/8VI3wT+aE1bvfxHBoZIVtSciRnMHTo/w4pznY8hKlshXP4yBUiPByO11D9RQA1w+C5gp2Tet17CjoyyhvL0rgftIRByRboXvwZL6bKJ2Eo2yl8Qt9n6FNAXi04bKlqqmY7TvRSFqLH9PlMz3E8GjF7Lb/LgXzcmFTLkW9XceQH278f4Gg== 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=D0uOF+T4jZHmuqcz209Qddae1NNTWTRVV73luMTut3g=; b=PaKvaCtb+mFMGg8TOMFnrW27/YAARnIbfllvEMZMGd+HC7UBk96gLgV0n5Gz9dD6n+QapLNUIP05kaLabC+riB/IiHEtIiAZPBS+PAnYgrsiVlfioWzh0jw7X2u9FBXNw23pUDqGmXhaHOC5bLuJQ/WCK2Jo+t0GN1jm422u9II= Received: from CY5PR15CA0046.namprd15.prod.outlook.com (2603:10b6:930:1b::7) by BL1PR12MB5254.namprd12.prod.outlook.com (2603:10b6:208:31e::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19; Mon, 20 Feb 2023 18:46:21 +0000 Received: from CY4PEPF0000C96B.namprd02.prod.outlook.com (2603:10b6:930:1b:cafe::1c) by CY5PR15CA0046.outlook.office365.com (2603:10b6:930:1b::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:46: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 CY4PEPF0000C96B.mail.protection.outlook.com (10.167.241.75) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:46: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.2375.34; Mon, 20 Feb 2023 12:46:19 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , "Jarkko Sakkinen" Subject: [PATCH RFC v8 17/56] x86/fault: Add support to handle the RMP fault for user address Date: Mon, 20 Feb 2023 12:38:08 -0600 Message-ID: <20230220183847.59159-18-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000C96B:EE_|BL1PR12MB5254:EE_ X-MS-Office365-Filtering-Correlation-Id: 9daee4c6-b661-4543-da8d-08db1372c21f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: VyHcgg771pKNaQaZ66TOwQjJx+5a1DDEHtgpaRzGyoXjyiaE2CigwirpZLR9q+6QT8Ar/smJsi244WePfNfXuVgaXMeb9sD9VOSfAT2BoHMp2VTQc2uKJ19Q00W7v+QcinnBYWBwERT4GWkWDdys3BNGYjUEh8KyHx4lM/A1jcs5fVSWUyUKIibRIwnCeRvucm/Z/kYmbuF0lOyx0j4Jfil0gtcPE9OiEoWS+8Ob1v8DIBAMlBOeSJlO9ETwo61JpG7Fy/RaSj/bh4R0Q6FYGAkSf83agPNcOOSAZOFzV02oDZ0Xbc1mu88HquKV+wGhLuOJ6SfMdXeAGRh0XohaTTNWzPHBHRWgUbUWbUEg6LUJcV4USBRseylRU0m9xXTKMA+lBlAcgLD7R9Lu1oBdPERUjDa1Gvbwtt8IEwmnWin2UrMiKa91ZqitlCx8q0dKaPLnKfu/DZ1BWn+MdQCSQFyhZ/SMSofeVF+5VYdRiGEaTAf5gbFrJP1cQrciPVv3eLcVYZgZs2ECa6rVXO8RtKx8XYASGA4KUig1FdrgWjEnDI637lgWE7YeBVdARtnEC0RSqDNGvAslH28iQOB9MVhgJLcweAqXlcrVBQWXfPQehFfe/HlS/ErdneWP09nBfJCbPNKoELu3p5101E1Oun0LRXnP2T0A0pokdjUv9PikdmfIvqlrilPT00M1AziR6V7e5aRgIRZtWVrNkcF4ctpy7FlzpAQtVZyP25jLzeY= 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:(13230025)(4636009)(136003)(376002)(346002)(396003)(39860400002)(451199018)(40470700004)(46966006)(36840700001)(40460700003)(54906003)(316002)(8936002)(83380400001)(8676002)(70206006)(70586007)(4326008)(41300700001)(6666004)(1076003)(2616005)(6916009)(47076005)(16526019)(40480700001)(186003)(26005)(426003)(356005)(478600001)(336012)(36756003)(82310400005)(86362001)(2906002)(82740400003)(44832011)(7406005)(7416002)(36860700001)(5660300002)(81166007)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:46:20.6030 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9daee4c6-b661-4543-da8d-08db1372c21f 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: CY4PEPF0000C96B.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5254 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 002521A001E X-Stat-Signature: exjts6eeg34996ncsqt9f3dcuywgimmx X-HE-Tag: 1676918787-95823 X-HE-Meta: U2FsdGVkX18+PouWrGxhYPmSOyfNKHdfaDCYxnpIVmgp9X54SqCJzhX1SoHU3uOIXOZwrTxfvaKu1Ln0hyL+XLn0h5+ZNIRO54QVeF/tA4B4zCg6T2LbuGVbHYYDIUdf7M5U1itfVHIVo/msTpUUFpqiXnuH+APiFIXxEsCshh8gJAUP8/Mpy9S5/MOpTFHV3sdsiiPRUeK32fEYiSjnirbLC10jvgfB/AiPNcaVnCjzNWCahGmFD3JeClpKPGaE0Dig+HcOxLFGWCR4BLEQK7MnWATPwqwtDhKoqHqYNUifD/vsO8mDLUmzVr6CQEOmHzI8PcTBoEQfiH+DXkcz2e8OKsQNYDqPFtbA3noVrPuc2EnZ7gODJmi0Hq78YMQeiuzFrRSkGx5Pcz8g4IIrgWwddzHuvT26VpHPsdmegnJMdPmYECBuDyiSweABTvjPH5CvV8wn6XU7K2vfH+LpqgZvvALxbjdCzMglVQODrnxGFeYxcHeyHJSwWTZyGHABtWI9M6XIudL1WivWPd3W3ObDfADIrc8jZHn6NgoMPsTt795GlA81ZfO/5ZV0zIoitIaATfBoWSvIee1HouTq40mudYz/qQIRrY/dd7ECijmUq3PdVibBxLYEfrZR9uGaKwY0oJMKWJDiOiSiyuj0MGTd0onDMqnb3+ZdHNO0rBp1fn+s9hV8qWFRwwHI8dl8pL7MQSTrfh6UTzC0/fwlhdTm6Ebzep2gcO4wd3uwOInhYt/gFvJIpKRzeTPHoa0jlshLCevxBtwAfc9PRyd3k9205gY6m43RfVtxJMitBmkdh3PJ3sKwy0sDlkOFlEk1Lpkj1MlcUvDUp1zPCp2vF+oPgc1AHjBTQRHyEMEIHzW50xNuUgAMAcZu8v+GucE/ekbCp2RolKHiuyH/jUWqbTe96Ot7ySoAG33+/ltpWkaKIc7DX+ZkqT4NlIqge8dC4iyNLfxcoNmFsxaR2ej nPLQoeUW RrFFM3TZqP/blL7mnXXNSy6HjhBFcSHi3IMxkTgf2tpn7Bb8nE7INcZCr6heSfOuUL9NDW2PzVwuaVoBtmm13xx9p3WKd7yZsO35prRw08fb5022rG1H3xQdOl4lnGlMftzJ4Cm1XSThh+hPQa7YWFWNptBjbqIlmnH33P+KrjaPMtz5KW8jN265QgVE4gAr+/l/ElCMcCpXXhaxNFla6oOsoFhDbfbbzP5Usoo1JMAZb8HpBhqpqSMIfZUJrCWiowcXEZJArQNLP8Nyl6IFfIoiidVIGR8uLgOh/0bxqZ1JMA43p3fXs3JQozUYu9xvLVz5gx29voRUE9lt9DpB6XbEd7ETDRkhv47qQe5rfIRAS3eriio/RicNeOzMXhnqyZfJMotVAGrsIsQ8= 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 goes through the RMP check. When the host writes to pages, hardware checks the following conditions at the end of page walk: 1. Assigned bit in the RMP table is zero (i.e page is shared). 2. 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. 3. Immutable bit in the RMP table is not zero. The hardware will raise page fault if one of the above conditions is not met. Try resolving the fault instead of taking fault again and again. If the host attempts to write to the guest private memory then send the SIGBUS signal to kill the process. If the page level between the host and RMP entry does not match, then split the address to keep the RMP and host page levels in sync. 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 Signed-off-by: Michael Roth --- arch/x86/mm/fault.c | 104 ++++++++++++++++++++++++++++++++++++++- include/linux/mm.h | 3 +- include/linux/mm_types.h | 3 ++ mm/memory.c | 10 ++++ 4 files changed, 118 insertions(+), 2 deletions(-) diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index f8193b99e9c8..afd4cde17001 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 /* snp_lookup_rmpentry() */ #define CREATE_TRACE_POINTS #include @@ -414,6 +415,7 @@ static void dump_pagetable(unsigned long address) pr_cont("PTE %lx", pte_val(*pte)); out: pr_cont("\n"); + return; bad: pr_info("BAD\n"); @@ -527,6 +529,8 @@ static void show_ldttss(const struct desc_ptr *gdt, const char *name, u16 index) static void show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long address) { + unsigned long pfn; + if (!oops_may_print()) return; @@ -599,7 +603,10 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long ad show_ldttss(&gdt, "TR", tr); } - dump_pagetable(address); + pfn = dump_pagetable(address); + + if (error_code & X86_PF_RMP) + sev_dump_rmpentry(pfn); } static noinline void @@ -1240,6 +1247,90 @@ do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, } NOKPROBE_SYMBOL(do_kern_addr_fault); +enum rmp_pf_ret { + RMP_PF_SPLIT = 0, + RMP_PF_RETRY = 1, + RMP_PF_UNMAP = 2, +}; + +/* + * The goal of RMP faulting routine is really to check whether the + * page that faulted should be accessible. That can be determined + * simply by looking at the RMP entry for the 4k address being accessed. + * If that entry has Assigned=1 then it's a bad address. It could be + * because the 2MB region was assigned as a large page, or it could be + * because the region is all 4k pages and that 4k was assigned. + * In either case, it's a bad access. + * There are basically two main possibilities: + * 1. The 2M entry has Assigned=1 and Page_Size=1. Then all 511 middle + * entries also have Assigned=1. This entire 2M region is a guest page. + * 2. The 2M entry has Assigned=0 and Page_Size=0. Then the 511 middle + * entries can be anything, this region consists of individual 4k assignments. + */ +static int handle_user_rmp_page_fault(struct pt_regs *regs, unsigned long error_code, + unsigned long address) +{ + int rmp_level, level; + pgd_t *pgd; + pte_t *pte; + u64 pfn; + + pgd = __va(read_cr3_pa()); + pgd += pgd_index(address); + + pte = lookup_address_in_pgd(pgd, address, &level); + + /* + * It can happen if there was a race between an unmap event and + * the RMP fault delivery. + */ + if (!pte || !pte_present(*pte)) + return RMP_PF_UNMAP; + + /* + * RMP page fault handler follows this algorithm: + * 1. Compute the pfn for the 4kb page being accessed + * 2. Read that RMP entry -- If it is assigned then kill the process + * 3. Otherwise, check the level from the host page table + * If level=PG_LEVEL_4K then the page is already smashed + * so just retry the instruction + * 4. If level=PG_LEVEL_2M/1G, then the host page needs to be split + */ + + pfn = pte_pfn(*pte); + + /* If its large page then calculte the fault pfn */ + if (level > PG_LEVEL_4K) + pfn = pfn | PFN_DOWN(address & (page_level_size(level) - 1)); + + /* + * If its a guest private page, then the fault cannot be resolved. + * Send a SIGBUS to terminate the process. + * + * As documented in APM vol3 pseudo-code for RMPUPDATE, when the 2M range + * is covered by a valid (Assigned=1) 2M entry, the middle 511 4k entries + * also have Assigned=1. This means that if there is an access to a page + * which happens to lie within an Assigned 2M entry, the 4k RMP entry + * will also have Assigned=1. Therefore, the kernel should see that + * the page is not a valid page and the fault cannot be resolved. + */ + if (snp_lookup_rmpentry(pfn, &rmp_level)) { + pr_info("Fatal RMP page fault, terminating process, entry assigned for pfn 0x%llx\n", + pfn); + do_sigbus(regs, error_code, address, VM_FAULT_SIGBUS); + return RMP_PF_RETRY; + } + + /* + * The backing page level is higher than the RMP page level, request + * to split the page. + */ + if (level > rmp_level) + return RMP_PF_SPLIT; + + return RMP_PF_RETRY; +} + /* * Handle faults in the user portion of the address space. Nothing in here * should check X86_PF_USER without a specific justification: for almost @@ -1337,6 +1428,17 @@ void do_user_addr_fault(struct pt_regs *regs, if (error_code & X86_PF_INSTR) flags |= FAULT_FLAG_INSTRUCTION; + /* + * If its an RMP violation, try resolving it. + */ + if (error_code & X86_PF_RMP) { + if (handle_user_rmp_page_fault(regs, error_code, address)) + return; + + /* Ask to split the page */ + flags |= FAULT_FLAG_PAGE_SPLIT; + } + #ifdef CONFIG_X86_64 /* * Faults in the vsyscall page might need emulation. The diff --git a/include/linux/mm.h b/include/linux/mm.h index 3c84f4e48cd7..2fd8e16d149c 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -466,7 +466,8 @@ static inline bool fault_flag_allow_retry_first(enum fault_flag flags) { FAULT_FLAG_USER, "USER" }, \ { FAULT_FLAG_REMOTE, "REMOTE" }, \ { FAULT_FLAG_INSTRUCTION, "INSTRUCTION" }, \ - { FAULT_FLAG_INTERRUPTIBLE, "INTERRUPTIBLE" } + { FAULT_FLAG_INTERRUPTIBLE, "INTERRUPTIBLE" }, \ + { FAULT_FLAG_PAGE_SPLIT, "PAGESPLIT" } /* * vm_fault is filled by the pagefault handler and passed to the vma's diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 500e536796ca..06ba34d51638 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -962,6 +962,8 @@ typedef struct { * mapped R/O. * @FAULT_FLAG_ORIG_PTE_VALID: whether the fault has vmf->orig_pte cached. * We should only access orig_pte if this flag set. + * @FAULT_FLAG_PAGE_SPLIT: The fault was due page size mismatch, split the + * region to smaller page size and retry. * * About @FAULT_FLAG_ALLOW_RETRY and @FAULT_FLAG_TRIED: we can specify * whether we would allow page faults to retry by specifying these two @@ -999,6 +1001,7 @@ enum fault_flag { FAULT_FLAG_INTERRUPTIBLE = 1 << 9, FAULT_FLAG_UNSHARE = 1 << 10, FAULT_FLAG_ORIG_PTE_VALID = 1 << 11, + FAULT_FLAG_PAGE_SPLIT = 1 << 12, }; typedef unsigned int __bitwise zap_flags_t; diff --git a/mm/memory.c b/mm/memory.c index f88c351aecd4..e68da7e403c6 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4996,6 +4996,12 @@ static vm_fault_t handle_pte_fault(struct vm_fault *vmf) return 0; } +static int handle_split_page_fault(struct vm_fault *vmf) +{ + __split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL); + return 0; +} + /* * By the time we get here, we already hold the mm semaphore * @@ -5078,6 +5084,10 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, pmd_migration_entry_wait(mm, vmf.pmd); return 0; } + + if (flags & FAULT_FLAG_PAGE_SPLIT) + return handle_split_page_fault(&vmf); + if (pmd_trans_huge(vmf.orig_pmd) || pmd_devmap(vmf.orig_pmd)) { if (pmd_protnone(vmf.orig_pmd) && vma_is_accessible(vma)) return do_huge_pmd_numa_page(&vmf); From patchwork Mon Feb 20 18:38: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: 13146863 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 65F10C6379F for ; Mon, 20 Feb 2023 18:46:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0A1876B0071; Mon, 20 Feb 2023 13:46:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0507C6B0072; Mon, 20 Feb 2023 13:46:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E340E6B0073; Mon, 20 Feb 2023 13:46:46 -0500 (EST) 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 D627B6B0071 for ; Mon, 20 Feb 2023 13:46:46 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 9C92CAABBF for ; Mon, 20 Feb 2023 18:46:46 +0000 (UTC) X-FDA: 80488551612.03.1BEEE34 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by imf25.hostedemail.com (Postfix) with ESMTP id 8F08BA0006 for ; Mon, 20 Feb 2023 18:46:43 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=p3yYnq7N; spf=pass (imf25.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.41 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676918803; 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=bwGNNhr0BV8XU5gNur70iQYprV9zVRyFOKALIClyqYk=; b=JJ7mvXs67EJ0aE4TtzERlbU26qCRRM7fEygmoXMzAmiQ/JFmy1q2p9cLL/CwB7PDq/Atk2 LAHDG4uPSLgFUWdB1u2k0jSSCqtNOKNjYAbj3fyowWwk8xmuGSxKWisBMqOxH0ixjX7zai uPom5saaP5sLjjtLu0kXjqRGzFybEqE= ARC-Authentication-Results: i=2; imf25.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=p3yYnq7N; spf=pass (imf25.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.41 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1676918803; a=rsa-sha256; cv=pass; b=jmisKeo44RYZk7S3HpvOeZL+ozvhgmYBd183alw6QLjJZwwkJ2nxuUBoE+5x69Sz7RhLS7 0YvJSnHEVvJO4CQYpIcVkvGBW19wBTTPes6YQEa/UjL3Mm6ffMPQxOtvf60QA0dhMT04Lc +3g9MbOD1SnEaAjdQ7a/z3qpsYElCQg= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MVRjs2k+sxXg5GY29MPMYn3AhV7TtUEMEc+Eov3Fq1flr4YlFQp4e71aQHJH7B354wDn6IJfvbNr5Vy54jHzITFRRQfL3AARhes5/NTgS1YFc2A73TMBuBlzK3w+bI2v2nsjIH1ViT552WjGGaFlietZE+4Agn5CwadX7UMtGZTGpCf2DiGASN/+ai3Cm0GtWqCnHRY9lMj9g8bCjIGYpNRgH70zhUIz9UpLi4UvrT/oZ/rUeVIkgdY9jowo2gsdAfMHEteGfZW0b5s7+QPTr26MIqUt9OL5g/PjpEhPWY4jAJu5filRKGBob3HHxvy29pqKgqDrpGw0gpklKBhk2g== 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=bwGNNhr0BV8XU5gNur70iQYprV9zVRyFOKALIClyqYk=; b=dZxBmpghRAzFGqYnF13dvp+FEhJJxgFbjYsmitOriqEXm6hIJD4AhJXMAU4h5UwHzFXabEJox3QjUtmPJS6ZIMp7sx/GUzX2cXgrUeNum9cw9yvaUs37QHOrycs0LpipBVOphY3SDWvU8LSzQKXvqydhwATuGSmNByo63H+JH2fuLXMIm7prbE2/uqWcTtSOKbP1KYCKgQOai3NNj4Q6WA+Ob6zZnXzEarzOTRZFt4wuLGbT0nSmX7gBC5Svu0TNsv8++RJodGBelqQ7lixW8wKtbZ5xTzaRzVM7TzjB50LL/00B183RpCM4IxMBnMtYkCiLJ7TMjaJGJf3Uf0E/ww== 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=bwGNNhr0BV8XU5gNur70iQYprV9zVRyFOKALIClyqYk=; b=p3yYnq7NW2BQ6QCQ1WAVRrLCAI0g/VIjE0oTcmF5RJKXruLyMHR6k2O5zsqySzBpo5tqS6UQenn8JKcZ5QX9vCwEwUEr//BPsnpkqQzF2Z0h3jGMzHEqQRhuN5OBMuT6Y40iJQlN90gXCwgWEp89wHJy60RLHXK/Lv69yvMWUdU= Received: from CY5PR15CA0054.namprd15.prod.outlook.com (2603:10b6:930:1b::20) by DM4PR12MB5264.namprd12.prod.outlook.com (2603:10b6:5:39c::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20; Mon, 20 Feb 2023 18:46:41 +0000 Received: from CY4PEPF0000C966.namprd02.prod.outlook.com (2603:10b6:930:1b:cafe::19) by CY5PR15CA0054.outlook.office365.com (2603:10b6:930:1b::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:46: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 CY4PEPF0000C966.mail.protection.outlook.com (10.167.241.70) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:46: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.2375.34; Mon, 20 Feb 2023 12:46:40 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Hugh Dickins Subject: [PATCH RFC v8 18/56] x86/fault: fix handle_split_page_fault() to work with memfd backed pages Date: Mon, 20 Feb 2023 12:38:09 -0600 Message-ID: <20230220183847.59159-19-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000C966:EE_|DM4PR12MB5264:EE_ X-MS-Office365-Filtering-Correlation-Id: ba5fa676-5082-4d16-0e82-08db1372ce65 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: B7d/72wrsRpK+69U11y8f7soos0SsuWP+xMyNq08E54psX1/nQ4WL0L6VC9MUTK3rXt7gXXHCJfXuW0Xjwa+Dt2Osio0EoWKZv8leNvtsXtmc4+lOgBHjxVqbTOxN7tDQMD1rSo48LfsTk7Ua/WCVihXVNnbHZoshykNRgvrfaotk4Is05Fl7Op91tB7DXutFOflbN73S+Cy0c1w6Wa8AthKSewebo2L9xXduCiBfkIHVMnPk5GvE8QkXcP0/GGWUdiwcR3VGtgYv6LM2hmRD4G+TtNFyHG8GY1AtPTa91Jn0Qki9ShzymXykJWBHWRzF1V+Np6Ua7FcsRNRMBkl/+TfTasQxD2Kz3xuDoKk9MUB/jB5XdaSC94BRM8LS2Oq9t+Xp4v695l5PdV5cy/5hSlmTecCs7C6t8gvVN4QYvUWyrl9gVqEXW9q1vxcJ49NqZf+XgapBmL8ysg4qEhExE459i9v2B81wBVPt2dVj06/9F3aakZbsPVaCdEljCyEgirNCW1qzMY3hm2F5HgH5XJwRQxjeeWHLaquRGpBcFfz7AT7xC7CJMP59jlhvIxC1XWGFB0bcFoahPy1RoH8JUxnBfjjX38J3rWr+kPEUU0MN3ReAt7YLvncj7fEInKSzywbCGuwSrmvec9Oa9dHziCzxbqS57LPHkaYh39uhmzrMPHlB5QfUxtzfV/QIxPefi5n7stR7z0TYtAqLWQ1Ca65SlNSFKc0EVthNn/FJUw= 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:(13230025)(4636009)(136003)(39860400002)(376002)(346002)(396003)(451199018)(36840700001)(40470700004)(46966006)(82740400003)(81166007)(82310400005)(36756003)(356005)(36860700001)(16526019)(40460700003)(40480700001)(6666004)(186003)(26005)(70206006)(1076003)(316002)(2906002)(2616005)(478600001)(54906003)(70586007)(336012)(8676002)(6916009)(4326008)(8936002)(5660300002)(83380400001)(86362001)(44832011)(7416002)(41300700001)(7406005)(426003)(47076005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:46:41.1892 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ba5fa676-5082-4d16-0e82-08db1372ce65 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: CY4PEPF0000C966.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5264 X-Stat-Signature: brb9umby6hyrfiwdfz4srp1nxqakrxk8 X-Rspam-User: X-Rspamd-Queue-Id: 8F08BA0006 X-Rspamd-Server: rspam06 X-HE-Tag: 1676918803-168809 X-HE-Meta: U2FsdGVkX19Qm0ejBjY27CMln8nAXL+dsNhcnBH5fUCqhVDU25dcgMBmlMT0XuVhwFxDFlbmXDAsVdjBWzjwkM9JvXuCy6jpNVdky9vZW0yxLn+Ix1EJunYDjzZVGD/9h4/t9sx98hjJVXorOB1B1QBn8vX4E4VdM7pGQAAYD3wX0XI92TCCwMkmGQMS/5WQ0Mc6343UUz9Nfx6oSQQmfwzACmQk0tGq4WB5dcwRGBuzG2De1bw9qEa9OGOmcet4eZdP+8MeYJcZFncgbd9YnNxUNn5+BGT7qnBgTiVycsicuMGOHhjfTmrmqWO4BByxnP7FqB6CSJMeokzr86hriTlnVmq6ipdYS/2Ekl9Gfg0znZ9CDR4tMnb7LhfwIm6mt1Bgp4ry2UPhowz579J8SlGnc64Bx18273tXYrBlzTlyNe3MmHA5s9OH6ic9eCBbK2AfRY7J2zyqzCnH6W13WGYhNuskzse/0x+0UG+lGZ11jP2syjiOCO7T8dyYBRrBkIjA4xBkzWy6FHUp0QL/2YzczOJvdtUos2jFw+Z8S1g11R79Md7hYO4UB1uFJuMGZH3mxa8DYQ6N2pgJ0Szq70CAIod97IY3+ID72bKRyHJMN2yZsBWLhvzUmLET2CAFWUun33t4jT+jFqD8LvNKw77CscVWf7K6lU+u+fh0DTFOEUfiYa8eBGm3+WftISGendS9nsCzWVVCzZAZA+b8l+2Og7q4Qz1QGEa9AJqNWNE4qjdrMAH84rBO2MUZI+snSAxWhkCTbUVkRjCPuSdlKsM+GZB3cKLj3D3A7oGE7/oxq+Fn3ZMta26UPY3r0i6xF2P/DcMD7RmW2qvhGbl/KrRTct7VZZbn/3VYSgNnlEXGB1CjmQFhOI/ryvugSAk1lNE37GKGyvH254i6pgBpqYgjySHC1vNXh3i9P2n0aV4kEwCdEq7VtuB5fbxhE05LT0m6TGPZZeRL/kKXv+B HOn8SKV/ /dBTPsKvchRWQ3lfw1lvws8Mrnp3goFwZFVwfS/y0dCpAhOJtWGOQSQgrulJGy0IxD3Z+V/5NktA6kZkx6L6YFNaHu2SJuTpAlkGmHGKtj2QcVlNzMjYuwxlhgMZAymMxgwLOWhLpF9oR+L8jeEblHsbLazowStRHOpwMDuZMZMtH4klTCq3VoN8Khqhyq/9czE4xIC7nal/S34P+gmOYvZ0xFHgpqdsYI/eB6JS4BLuOaSUuHIWjYuoGdm4GRMCa3bRcRsU0KKzQfqU4UuynuKJ9bSIeTkNL8aLL4e2nwi9mLd4++N+U/Enpbhb1q3k4sdUqIMG9+8T3vs3EWeVyKurXLpPILzxot6QRqr5O5vV6VzLOUIjWTmSE6zYTv8L0cW6jVNY5RCy78uU= 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: Hugh Dickins When the address is backed by a memfd, the code to split the page does nothing more than remove the PMD from the page tables. So immediately install a PTE to ensure that any other pages in that 2MB region are brought back as in 4K pages. Signed-off-by: Hugh Dickins Cc: Hugh Dickins Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- mm/memory.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mm/memory.c b/mm/memory.c index e68da7e403c6..33c9020ba1f8 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4999,6 +4999,11 @@ static vm_fault_t handle_pte_fault(struct vm_fault *vmf) static int handle_split_page_fault(struct vm_fault *vmf) { __split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL); + /* + * Install a PTE immediately to ensure that any other pages in + * this 2MB region are brought back in as 4K pages. + */ + __pte_alloc(vmf->vma->vm_mm, vmf->pmd); return 0; } From patchwork Mon Feb 20 18:38: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: 13146864 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 E1028C27C7C for ; Mon, 20 Feb 2023 18:47:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7F3486B0071; Mon, 20 Feb 2023 13:47:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7A3606B0072; Mon, 20 Feb 2023 13:47:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 644856B0073; Mon, 20 Feb 2023 13:47:08 -0500 (EST) 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 56F1F6B0071 for ; Mon, 20 Feb 2023 13:47:08 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 1C405160AA5 for ; Mon, 20 Feb 2023 18:47:08 +0000 (UTC) X-FDA: 80488552536.18.0DB6377 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2082.outbound.protection.outlook.com [40.107.237.82]) by imf26.hostedemail.com (Postfix) with ESMTP id 11DDD140008 for ; Mon, 20 Feb 2023 18:47:04 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=gp6doT3d; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.82 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=1676918825; 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=8hQgEt4zfuohLaU4xMAC3aVGdlY7+WMaFzYsllKiGIY=; b=USS6wJrFah2JgO2/VZTq5K7p/Zoy7Aj7ZwzspsFIRpKOuig1f0UrZk0F1mZRxjQ/wBb+IT cWW5yKBvkzMiNp2MAwp4xr+WYwpokLUXQX4WBgzx5b2ymuPpg2VTf1TWuJ0kL/x1sK3OKB 06IRlHCRGh6eThawORy1xU2ZFgsyZfE= ARC-Authentication-Results: i=2; imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=gp6doT3d; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.82 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1676918825; a=rsa-sha256; cv=pass; b=XltFCljptqZEP8SIpoZ9A2lkrF+5FBbXSMmguKxA0T5nWmOV1I1AkqbtcF5WzTJ13IdXmq NU7h6Q4qRQIxWAIwtmdirfcw8ydMYVQMj1wWT0vjhVcbv3CmHaxVvwaXzoa1sNKajpK7uA FvjsxdhsgVxjX/R/NlsG8Zw4WpcwiW8= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=T+JJla0olOxnhbHBPL9D0pNGBGWu1/JtPM5ULIJvnxosIgI+PsaY0sBfAECsQlOItmjckcQj/4J/RvLxcteje2yIRkm8ji51iCINmNDnvBrvTFstw5EmmthEhlF50z3LikmJj5fheDds56ktxFNfwffliO635E+EC6utPy7vvjOS4tnDOammBEdy3boVa0JKwXKqT99Lgr7EBnx2G3qbc0eXKlzfb3rI449bYrEdusa0hChjhbEGuI8PehWUyzLmK6XdLjpM9qLvoFnejypIKmOI2RtppazTBZgOY8b4bTq8qS7U+9hos4hqM3RvinOu4LMna/6uzVdRC1r798CJJw== 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=8hQgEt4zfuohLaU4xMAC3aVGdlY7+WMaFzYsllKiGIY=; b=iiILRzl3Ym+bS05JBKW/wnLSDUav0w5nogKzyyUdsbgvGyU8ZX48NOwBRBfo4PuUSPHjls6LkV3hdNr+tyTq7EnfyJp0AYSDut776EyfxymKyCfhMYsTdK2ARYZsBcF2X0IkYeDQ8haUNPa/sx4XbwzQx2pQhPiQxIS2/s7HS9e39L++l+6k/jFuclLgeqHm9P4MqwluH6r4HikaRppF8p/a8onSma48XSyHbWSXPEzLs1Pbnq7ZwMc0R8J8JBX1c7PcjwfaV1cg2BjNsQBu3nNyUBv4tyRrU0JXsxdSB12BlXJZAxu7CZzKB02xwcHBlKZJhtRfpeOhcuPYsHj4Iw== 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=8hQgEt4zfuohLaU4xMAC3aVGdlY7+WMaFzYsllKiGIY=; b=gp6doT3dHJYY8E9gGKjumXpv5LdwdRcCoDckGMKReqljFOVTP3JiwfebMQX3jO2NMN5BiDt3al+TESvoYWnNjDsOIaAlp1Fd8HH7zSL/oq+ORf20SGMSOpQD8b2V0KCd7dzQToUWz5ablhXhuCxKaD6JGTUSARyoDAqkGInk/ho= Received: from CY5P221CA0113.NAMP221.PROD.OUTLOOK.COM (2603:10b6:930:1f::6) by MN6PR12MB8513.namprd12.prod.outlook.com (2603:10b6:208:472::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19; Mon, 20 Feb 2023 18:47:02 +0000 Received: from CY4PEPF0000C96A.namprd02.prod.outlook.com (2603:10b6:930:1f:cafe::20) by CY5P221CA0113.outlook.office365.com (2603:10b6:930:1f::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:47: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 CY4PEPF0000C96A.mail.protection.outlook.com (10.167.241.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:47: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.2375.34; Mon, 20 Feb 2023 12:47:01 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v8 19/56] x86/fault: Return pfn from dump_pagetable() for SEV-specific fault handling. Date: Mon, 20 Feb 2023 12:38:10 -0600 Message-ID: <20230220183847.59159-20-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000C96A:EE_|MN6PR12MB8513:EE_ X-MS-Office365-Filtering-Correlation-Id: 9b2c1e85-2006-460b-f2d6-08db1372db01 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: sfiaFB4QrkAGuUpc9aYDo8hy7b2Tra5UUhwn+OhNIo53ij4AQLAHfrvoMTux2P8/TEa8mMsfxRPpaMAdDbhsmA2oY3zmSWPe7hoRDNc01R170JClPW8A9Vpueas1m5YaiIw4CLbIQ3TW8Gte5awLrk45nEoifCkvnB5kagH75Mufqoc282vTTg+B/co7YwTC1ndv/BYlMrqeH+ljhxR3/o1kvqfV4AiT3Ec+MDZleuIIEeACB7i/dN045+g3Y2FFkTNK89akTy17AiapDBaeh9D0e+6tcwe9B0MnDJQHM4ROMgBZt+nA5bNU/38u6mEd7uj6Jd/4LtdgSpE4LzwNUGQIQJsQ/W1S1o3m5wyxkfj6ddDT0eUyHjjK+TZ4S6pJ3xyP/7hoPmJwZjlYtFqLbl0fvylazK7KFMcY2cSHUES/9Zd3lu+Tq6lnKohZ3GNsekdMKH7Xzwl/7/Tr/FyLSp8IGD0lqfgOkHNp4NRlk5p5ZME/pn3eoTaKi5GZHWBO6oP/8tptiTd0Ai1c/Pe5ZCV/6ftdrUCPc7/QOwCWo3djpvf32B215zUnroMc3AV4Ym7e/0AZkUgf6fMeAryU3VGAiXQkbDIygQtQK1V9PPcS24nnXjwpyI9kfW++ZUlucZSFDzOevOrrB+bpa7+3iXkk5/Opds9GtI1cURI+V63FwX/YZdg7NXh1WlaSQahLWajjE1NSAOIlfH9dDh7KKC3JcoXYCsd6efT9Bd8c1LA= 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:(13230025)(4636009)(136003)(346002)(376002)(396003)(39860400002)(451199018)(40470700004)(36840700001)(46966006)(6666004)(47076005)(478600001)(426003)(4326008)(6916009)(83380400001)(2616005)(336012)(70206006)(1076003)(16526019)(70586007)(8676002)(41300700001)(7416002)(5660300002)(7406005)(8936002)(36860700001)(44832011)(2906002)(82740400003)(81166007)(40480700001)(356005)(26005)(54906003)(316002)(186003)(36756003)(82310400005)(86362001)(40460700003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:47:02.3500 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9b2c1e85-2006-460b-f2d6-08db1372db01 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: CY4PEPF0000C96A.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN6PR12MB8513 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 11DDD140008 X-Stat-Signature: r7fe43dxq9abzw3r9z9zgy7z5szybgon X-Rspam-User: X-HE-Tag: 1676918824-489374 X-HE-Meta: U2FsdGVkX19q/m0DDT0ppaLQlIfXWIC2k2hmwURVAcOKDwi4NmB16PyI+c74DaOQ+R6E0gM6pRcC0hQQ24rFnG+vExPxaNXkP5v5zXmb4cvxEpbB+fQQsEMljT4id7B8116K4KhuMzosT58/mOirWieT5YzenbEcxYOCxOzEn/hw1hkL9vSs64goLqaDoCvcUiVHarCs0b1ZY269odyTyWOCNT+EB8/5AasgKMTveXQIzyr7YUL1rjfsA9HG8fUEJC3KIzvZPrKaYkupygvAjkUlHnR7ryA1aL28vkKinQbYNo8HxJfFSwdn3X+nzjSKNR2mzMqPTNRjxRKQVpbDcmUNdFVxLrFTKyBCCoXKBQmytIqHKNqWIN9h7WEsUph6WZZl0qy7fHVk03ci1A4Mp24rw7pbYlwoNw49fnMJ1jKZZbAB3PDJWK0dAzugcdoZwUmpkSBw1yMEPgAaW2qEe+X5ThK1LZBvjGv080e+w1IU37aHYHomasguaAmo7AgG8mIHZ99s8bpTO4lLBrgl1sfMZ6vkk1rVT5ap9eL3Sps0IcIEEaaT0UTsEmJFfLV7msO7kYASSjj1SuRBxjicgkKN8n02CGb+iIi2/HrdlsDwvBmmzrpdwQ53omu7Z0Al4welb6Wnp6yOwmh8ETOsFscvxetE+XYyT0XzUqgnI8PS7LOMNgK9OsbZx0CMmWaHu0JRxk8IJaF4ls6F21wIds7lbHx5UUMheVb9c7Krh3QwsSmM1XFlOYBqlJHbiULHmvAS6aYxCYEe3Toa2IdOXZVOHV8GWpcbGFGQN2ObcQQz6vw7Uw1Q75sjMdHBa7WJmvRyeOL2zcZJDSWxAbVg7b5N8dxBaniGDEQ/XbZ5iRTSaUuApbach+DBcEMe/2NPODXvzsM/Wsenmvxhjtq1FzGfc0+z+oQlxEKVZ5Kix8O+SzzNasRKilKLjKL1SGUymOJvCYgOsSHPXjsKIQw v17XxlHc 2rnrALedZf2yYUuY+QsD/lJLXjbBkh9beAMtLl5q1cQMXzAlt6Xwd9gj0Ush9XvMCls+QFrkZPNo5zxwn3+oc+gcXtEPegeVt36eHovEyfFAKiDNUVzIJTLUnDHX9LFCs0N2/oJJ57sQp8DqgK6A+uW8oxz0vo7zMIVl2UWS9K1HoIGPu9MNVfw+BvCmEZSHjeduKchn+sEmkQzGsRTLPY9mHZ2Zl7EX6Rg9mlWBqOWuFYire/6APgozMvvRHUMg2nhXnrPF6V6vV+reNDCtlHJsIb0n+M4h+/Pg8ALMiQcVoST00tjYF3Bmj7+bq0GskwgkdCa85Hjv6/COHSjM1N8/ekiXTKiaVhCCqr9YKyRpspH8jtERL/0ak7Q== 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 Return pfn from dump_pagetable() to do SEV-specific fault handling. Used for handling SNP RMP page fault. Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/mm/fault.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index afd4cde17001..f2b16dcfbd9a 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -311,7 +311,7 @@ static bool low_pfn(unsigned long pfn) return pfn < max_low_pfn; } -static void dump_pagetable(unsigned long address) +static unsigned long dump_pagetable(unsigned long address) { pgd_t *base = __va(read_cr3_pa()); pgd_t *pgd = &base[pgd_index(address)]; @@ -345,8 +345,10 @@ static void dump_pagetable(unsigned long address) pte = pte_offset_kernel(pmd, address); pr_cont("*pte = %0*Lx ", sizeof(*pte) * 2, (u64)pte_val(*pte)); + return 0; out: pr_cont("\n"); + return 0; } #else /* CONFIG_X86_64: */ @@ -367,10 +369,11 @@ static int bad_address(void *p) return get_kernel_nofault(dummy, (unsigned long *)p); } -static void dump_pagetable(unsigned long address) +static unsigned long dump_pagetable(unsigned long address) { pgd_t *base = __va(read_cr3_pa()); pgd_t *pgd = base + pgd_index(address); + unsigned long pfn; p4d_t *p4d; pud_t *pud; pmd_t *pmd; @@ -388,6 +391,7 @@ static void dump_pagetable(unsigned long address) if (bad_address(p4d)) goto bad; + pfn = p4d_pfn(*p4d); pr_cont("P4D %lx ", p4d_val(*p4d)); if (!p4d_present(*p4d) || p4d_large(*p4d)) goto out; @@ -396,6 +400,7 @@ static void dump_pagetable(unsigned long address) if (bad_address(pud)) goto bad; + pfn = pud_pfn(*pud); pr_cont("PUD %lx ", pud_val(*pud)); if (!pud_present(*pud) || pud_large(*pud)) goto out; @@ -404,6 +409,7 @@ static void dump_pagetable(unsigned long address) if (bad_address(pmd)) goto bad; + pfn = pmd_pfn(*pmd); pr_cont("PMD %lx ", pmd_val(*pmd)); if (!pmd_present(*pmd) || pmd_large(*pmd)) goto out; @@ -412,13 +418,14 @@ static void dump_pagetable(unsigned long address) if (bad_address(pte)) goto bad; + pfn = pte_pfn(*pte); pr_cont("PTE %lx", pte_val(*pte)); out: pr_cont("\n"); - - return; + return pfn; bad: pr_info("BAD\n"); + return -1; } #endif /* CONFIG_X86_64 */ From patchwork Mon Feb 20 18:38: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: 13146865 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 EBA19C636D6 for ; Mon, 20 Feb 2023 18:47:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 81A246B0071; Mon, 20 Feb 2023 13:47:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7C9CB6B0072; Mon, 20 Feb 2023 13:47:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 66B516B0073; Mon, 20 Feb 2023 13:47:29 -0500 (EST) 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 54D936B0071 for ; Mon, 20 Feb 2023 13:47:29 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 0FBC1160A9F for ; Mon, 20 Feb 2023 18:47:29 +0000 (UTC) X-FDA: 80488553418.26.D806F6A Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2049.outbound.protection.outlook.com [40.107.223.49]) by imf23.hostedemail.com (Postfix) with ESMTP id 18C4C140005 for ; Mon, 20 Feb 2023 18:47:25 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=ayiff+DE; spf=pass (imf23.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.49 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=2; s=arc-20220608; d=hostedemail.com; t=1676918846; a=rsa-sha256; cv=pass; b=BueTuBMShCkM9Ln1LzgnoiFI3uEXfslZqgEowmTlVg8w36FoldgrFDic/VQJyllYcX5kcv 1vHdmIsGD8dLjibcbmpco9oWdbr+TIQrpFW+tZjV6aI8qEupNOvo6sYIZRURfwEuptdUIJ uVXxVnoPEUn8rBBHRzoE//7mko5hN1Y= ARC-Authentication-Results: i=2; imf23.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=ayiff+DE; spf=pass (imf23.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.49 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=1676918846; 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=ZlUFp+VwVtbLnwCRmDAMFBybG0+YXNebrHVCVCJfOus=; b=FT8Nu/OyWuTY6yoMM98u5IBxcuFySVarCbC3naY3nIKArQVazui/tnZIPUSx80VmaI6yVQ M266BwpR51C84B1Z+g8iACN+k5Haz3K5PaqrJ1x14qCGfWDa3Wmb83EnhZZhI+yLIUDDH0 rlwQg4VNumEKP4SrzZcS7Er4eNyNONc= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=g8boJHumb+DaZBzCNWppzHWo6meDqU0lCuaAo7dtvcs4xb7jzuNPTI+MSNILK9cRgn+um7BBSRagjBuGbrNgZEEayOwSJ12LGSZR2T3wt8EcnBVzY8CeNDH6a5Um9MQiqKUIpcv8C7K/1CFKKldE1SpjZ9XIKTyWwfzMsMlibOnmDnIOj23hjJmDHIf6yk8MbfOX/gr0ZDvcotB/RKdlfqPC0hRVvozInQ3x0DQ9106AbD9eUzFND6Am8FBxrWPHqB4mQ2w0wt9//8bRuB9Wd6+BJVwSGGe0W5yEgs1vWWQGq/En1I5nvlcGqTeWKVqEwwuziLDnUSqkqIgolEmH1Q== 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=ZlUFp+VwVtbLnwCRmDAMFBybG0+YXNebrHVCVCJfOus=; b=Z9c8t1YtJmDIw+HhRX7VriAh65WwxXxaOlJYJvt+/Fqryt66va1/j0LHgvRhBD0PSQPSKh7NTaGYE+NIHOB3q1IrQjAx1kY1lA2wWgiZVP91PJGxLR9QitmgqGXjDCQQLbeolZJ/o8UZxStQK8r9p2WmwhYYum3kSbY6LfLNEHVhrSNTBtjbSix56+E9wiNM3KBOYwOQHGdPyoeu9D3WfMiev430ZK/vWOU6afQ5BbH+/AME3LTJAMYXQaBsHcE8ZcgBxRlE5Nsh3ARMnsvud5XZMNThPSIN1ruJOXOktYF+qEG5wN0nDRRN+gk2i02Je92/Y2ZbyZQRdBN8D3xiAA== 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=ZlUFp+VwVtbLnwCRmDAMFBybG0+YXNebrHVCVCJfOus=; b=ayiff+DECJTY3D5fFe9Kbr9lSQmDitAsSzOKt0elqgIzUCeJ4kRCDIiVdSNSrR9NYcQmrNFS8wGs5DKPatXvwwQ4cZJQt6+o3ThLleGuk10DWJoA2/f2mOuJraBdJlG3tcOriDidpYksv+tQWGo1Wqnc8bHq6vvGGhn45EJyJaA= Received: from CY5P221CA0117.NAMP221.PROD.OUTLOOK.COM (2603:10b6:930:1f::11) by DM4PR12MB5311.namprd12.prod.outlook.com (2603:10b6:5:39f::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19; Mon, 20 Feb 2023 18:47:23 +0000 Received: from CY4PEPF0000C96A.namprd02.prod.outlook.com (2603:10b6:930:1f:cafe::ce) by CY5P221CA0117.outlook.office365.com (2603:10b6:930:1f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:47: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 CY4PEPF0000C96A.mail.protection.outlook.com (10.167.241.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:47:23 +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.2375.34; Mon, 20 Feb 2023 12:47:22 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 20/56] crypto:ccp: Define the SEV-SNP commands Date: Mon, 20 Feb 2023 12:38:11 -0600 Message-ID: <20230220183847.59159-21-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000C96A:EE_|DM4PR12MB5311:EE_ X-MS-Office365-Filtering-Correlation-Id: af72fbc2-da5f-4b67-5585-08db1372e7a5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: gihk1fF0d39RLCAJy12wf42b7YPcROlyDi8Qw6qMJcpjuVJdsAxWYlryEirs/qUhNI96YQFdK5UJ6fPuiOjaW82P3DWd0RlXanuHED/BnF0j466wcsvEitbo5eOnP3dAZzE55AWyoTyIBO42t48yHTeWvHGzMqzvVdV5LBmSJ6TltGhrZCo7ZAfvRJX5A7i9eXzI3FPk4tHna+Vp/eS9X6k/hWhGL4Gsy0VpNbJZtQCaeaVXfXiKSXP7wM42pgbHhgsDkcryzuLueJRiS8W/Dy1CeiIbD0t13sTRtgxVtOON5S4b7WdmPEYd1ms5HVHOXirgMsRxWdx9YVWwhcOl/HPnsDQVbab1E0vz1V7QKjp+DI0VRKXKFo12AZu1k7jgIuV3ypA1QPr8IZq5+b0avHkXM4pDEluhb5li4i+z/EE9taX5SIhrPuBXQ6pptLKloCLHSeK3Fnsn8uU61COqyrLcvdGmbasqSuQQU8RbGAhLtxuM5K842GuYztLDth/0yueQ81u/DqVS0Wefm1BFq597RbMSAC9uHNj6P22EQEC+Hc0FTzNHBmNC62IsHh4PfB/wynb9qirrgvRZauEyyGKf34Tu016V4JjL0nAryuOpFPPb1uYY57JROYp+hfIBeNZwB2/DxawoDCdR7X8x+iVvji/wVitTym419mB696qRjdT3wcdontp/yCriXt5ji0L0P+zTaeZhB6s+6DG860zkdRkAEqPYHQaMSE5fh8g= 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:(13230025)(4636009)(136003)(396003)(376002)(346002)(39860400002)(451199018)(40470700004)(46966006)(36840700001)(40460700003)(41300700001)(70586007)(70206006)(83380400001)(7406005)(5660300002)(7416002)(8676002)(54906003)(1076003)(2616005)(6916009)(6666004)(8936002)(316002)(4326008)(186003)(478600001)(426003)(26005)(47076005)(16526019)(356005)(82310400005)(36756003)(40480700001)(336012)(2906002)(82740400003)(86362001)(30864003)(36860700001)(44832011)(81166007)(84970400001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:47:23.5378 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: af72fbc2-da5f-4b67-5585-08db1372e7a5 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: CY4PEPF0000C96A.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5311 X-Rspam-User: X-Rspamd-Queue-Id: 18C4C140005 X-Rspamd-Server: rspam01 X-Stat-Signature: 7sx1nd46oxrozi8c6z3oogdy14g5daz8 X-HE-Tag: 1676918845-50431 X-HE-Meta: U2FsdGVkX19gpkFRG6aNBBgsquNgMo3m7D1+Z9fJQA76WZk+1DZPSuaxJjPGGxyuB0eJOM5ki0/pS0kt20hAUJix85xjLcApC90B1fFXwJ4fQ0J2zzrB7OuuxzFziDaNMDVkkybjGrcbj7+KmxvaEr6Zl8GV1oVXyU+GdPbSfLTFJlCwOHV0BQ2wEv7GOFLrKoElJUxZ5X9Tgee4hIYhacHWHPUUjoJL9BFVpB6aX7cFGmDeyI8BKZB8/080y/PgOnocx4L7kKtqdcmET/BHmjOYyh6lIUvLQw10HJKqarkPRbuwPvqdY7YJ6d0mtWMLvPHscvj7imbjmMOxs+am527itDpoCgy4vc62c+z+QXgPC3Q4m7CzQbgAeHQTb78QXbaZsRY09SJOatg36E5+//cAjm+FBw2GT/fSzvnZg/qFojNmpxfRUddfLBkfpk7H9eXkiE7Zfm9UahEwKr6LMze3l5hOVXxXRDVivAdnCk2cZw3bhKKkXD9Qw9aw0Rl50rKaGb/4yo7S2/sFjHg3jGLEdnwX7YVlFvVXX3i9dF7WvTL7mN2ovUloE3M7WEtyHsM0th0oharKnBJ10WiyR60ulkHj9Ckm+nY3AnWd9Hgx495Yqmm+Fw591cUPtJpqgl1j4uMVkXpN6oth98RlMSS+Bc/IeGA+rw/RtBz6Hx2NXYPtHkwqISENputHvbYEKNHYEum/XlRDLJbtk3litW8+dZyDJ/teI6/shap3l+vVUvmCD+to0m43R+s+AkwOrqC5uJa4D7E16tA770JpTSUpUgOb5NROfHOkfd/bvNhqSRbRV/PK4MPLKuKJ7OqdJAHnc4YcuMD0l2txK3LR8KQrVy6CnvcqhnKk+wqwwmx8birgw59uE1coFpuNOYFVrFRz6wINLWDv3OB5rc4EMM+rtuFwB39Bvxfo4wI6XhzGXlP2LOb1d8tqBxNSXDdsyqCv1UJSXzcsPwDPJ23 y7F2WGmc GQ6nA5VdX5WIoDFE996mp3Yr5kSanCq/ueoIqm8PBwMjC1dXswq4HbUOq2CiSYPA2Z/osBszEjZibFCLOEj1DB0QxuER1X24Zm8/ZGnrI9uIeUACrs2YNcEtfWoM6m2DHKPZGIX71PjqMcTv9MHrPceIuCASye0svsPO7gH2u3Mhpwszj5OeokyKtVtvIos/VsOUxIgK/DjFOi46w6d6NXRbsiJlLXQd94OVhsTZ1OBLtYOlzL6/BS9W+/iqDHvyLKbGGSK9BQkzzu2XnckC8kG/XeXEIhxTs+ThqmiilvH6py1Ssrcc/EVTN6DmaDGglIp/7Z2+I/Fsl5kQ= 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 Signed-off-by: Michael Roth --- drivers/crypto/ccp/sev-dev.c | 16 +++ include/linux/psp-sev.h | 247 +++++++++++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 44 +++++++ 3 files changed, 307 insertions(+) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 06fc7156c04f..9d84720a41d7 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -126,6 +126,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); @@ -154,6 +156,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..31b045e1926f 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -86,6 +86,35 @@ 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_MD_RECLAIM = 0xC4, + SEV_CMD_SNP_PAGE_RO_RECLAIM = 0xC5, + SEV_CMD_SNP_PAGE_RO_RESTORE = 0xC6, + SEV_CMD_SNP_PAGE_RECLAIM = 0xC7, + SEV_CMD_SNP_PAGE_UNSMASH = 0xC8, + SEV_CMD_SNP_CONFIG = 0xC9, + SEV_CMD_MAX, }; @@ -531,6 +560,224 @@ 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 + * @len: len of data to operate on + */ +struct sev_data_snp_dbg { + u64 gctx_paddr; /* In */ + u64 src_addr; /* In */ + u64 dst_addr; /* In */ + u32 len; /* 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..c66f7c372645 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,43 @@ 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 + * @build: firmware build id for the API version + * @guest_count: the number of guest currently managed by the firmware + * @tcb_version: current TCB version + */ +struct sev_user_data_snp_status { + __u8 api_major; /* Out */ + __u8 api_minor; /* Out */ + __u8 state; /* Out */ + __u8 rsvd; + __u32 build_id; /* Out */ + __u32 rsvd1; + __u32 guest_count; /* Out */ + __u64 tcb_version; /* Out */ + __u64 rsvd2; +} __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 Feb 20 18:38: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: 13146867 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 C7BF1C636D6 for ; Mon, 20 Feb 2023 18:48:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6458E6B0071; Mon, 20 Feb 2023 13:48:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5F5316B0072; Mon, 20 Feb 2023 13:48:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 495F56B0073; Mon, 20 Feb 2023 13:48:14 -0500 (EST) 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 3A8656B0071 for ; Mon, 20 Feb 2023 13:48:14 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 135A21C5CE9 for ; Mon, 20 Feb 2023 18:48:14 +0000 (UTC) X-FDA: 80488555308.24.8069CDF Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2051.outbound.protection.outlook.com [40.107.94.51]) by imf19.hostedemail.com (Postfix) with ESMTP id E98C61A0011 for ; Mon, 20 Feb 2023 18:48:10 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=stz3a7Ee; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf19.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.51 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=1676918891; 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=RNuCOPLhGkj2ZKgj57cn7yE9zrZbhXJooEIkMA/0dAg=; b=2+HWDVDtJ2SbU/KnAwRFzt+rEkkGEWx7wN/R6PCchPql4VRW6jwW0PfjJ65xiFf48B8uu6 5o54OKSoWocuTohJJUZbH8pJiHCv0t4+24WyXnoADUlzg+n6N66Hwj5xADP0MrpUCGd1dW n8P/tWu/UxiqLmShSDOnqqcbcwIW9i8= ARC-Authentication-Results: i=2; imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=stz3a7Ee; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf19.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.51 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1676918891; a=rsa-sha256; cv=pass; b=uzyhhH3/QteC4Vtmb3ldKwYqZLn3xkATtiuXi2ETLtNt7u7O2MIkB/Zm5Um/MUI2hDA49Z +B3GgtApP12krwZ4DM+jTm36E9NSF2CBAacvTQzGUB/gkZMMWPI5RuhHImuEACuUAjWOhZ BnTno3plHPWopjl1RKGeKIms0BVm4KQ= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bCgkGt85boMT7aHGqRRaG1j1g8PLMiR15H3Ft5raJJLZmdGJ/0Q/bxof3/cwvM6Kb5iZ8IS6JyW0LsWuWscXxBpqJ9tmOUv3/NGTXNmDe/gEdpBzR7KMH8MLjJgHkipbnEzxwz0aI9jBVGbWNIZxf9xHvJ9c8VhMe49CtAkbN9Go1B9EQY0qzuK14iTe0uUzBbf4+arQQayss7JHbbjvKDvk8dhyAmmyA2maeAptqKeWHAxNy9kNfMxHgiSGXwuomfUlZPGculf3r7ZLw8wkM7b4lOJAtuEIVievccfszyTXv9qXu04BYYaaHTHDIyf98edsQB4LeL5vYs+oZ5sVzA== 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=RNuCOPLhGkj2ZKgj57cn7yE9zrZbhXJooEIkMA/0dAg=; b=D/3JFmrKOha6yeax2HQo40aeGLvijcgYxk8TPm5Zs76Apdz15ZxQnzE7mACXsf5jfZ5fvHc4FLBI7r3WkrygC9zo23gal8EbNqm4nR1z2XNrxK6rbsCo83IPbxFtctih5opQF9B4MP6AaRhmDO/69gg1Pi5C7SV6Xsi5ToSF54sNfCqvUHWgWmBdMLBd9wkpmCee74tN+kCJH8TqtKjXUXnzxPHxXWLbUvGjK6e+vGIRfkjArKU3uGxI0Kq7yvxoQzUPZ9wONu3OvVWhRwwoj4IJwIlCF56OzmYb9XNoZpw4ARv6fV0OaDZMgmmRn3qbROt2QTaAG76SQCdMgLLPqw== 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=RNuCOPLhGkj2ZKgj57cn7yE9zrZbhXJooEIkMA/0dAg=; b=stz3a7EeDrJFKy4BmGS1X6ZzV4I06l2ySO0mD9FSSOfBGqPefAMX8h3zLrx9KtdR7md0bB5OtjEGCJnMjuwCH5K+veGAVIIYlJCa+YO0S3v6wQQrM1R+JrOECB8rzaIUqH4MlaEfFrAfSrvE0mUb6OiP3reQByhz7shYZNBTHv8= Received: from CY5PR15CA0042.namprd15.prod.outlook.com (2603:10b6:930:1b::26) by MW4PR12MB7481.namprd12.prod.outlook.com (2603:10b6:303:212::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20; Mon, 20 Feb 2023 18:48:05 +0000 Received: from CY4PEPF0000C966.namprd02.prod.outlook.com (2603:10b6:930:1b:cafe::8a) by CY5PR15CA0042.outlook.office365.com (2603:10b6:930:1b::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19 via Frontend Transport; Mon, 20 Feb 2023 18:48: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 CY4PEPF0000C966.mail.protection.outlook.com (10.167.241.70) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:48: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.2375.34; Mon, 20 Feb 2023 12:48:04 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 21/56] crypto: ccp: Add support to initialize the AMD-SP for SEV-SNP Date: Mon, 20 Feb 2023 12:38:12 -0600 Message-ID: <20230220183847.59159-22-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000C966:EE_|MW4PR12MB7481:EE_ X-MS-Office365-Filtering-Correlation-Id: 3160cc51-63cb-460d-9fb3-08db137300a8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: uYBdY8dJcrj1z9kWqdJYQMKD08Ij0lqrf3QFEoL+/9ijmeRT94pIchIXo1d+InQyP5cyS6N/KuvglvkhN924lUPZU18pJt6UXy+mTUaXP+W5CrdvnQ+9JKcOzeCHt95FTdf9ItzBplpsdiWqp9FdEdRHE57lPTa/pf1s6YW0oEDOy9tJ9SY2XhImOxdrdFiSRq3e12RYpbhsIjHN4OnXhF/PoZ+B8L7Csowq/mdi0eIpJ2ImVuhYz1EWZ3c5rj/rsbkRgzvSCm/gBQkiTNeNDouycm6YNO951V8e9DTIpqBdgWJnsgjdCoNCuD5bsopPEtiwud99ftXyjZe1oBfZSFmkLSTddyaY3JKSpBwYKa7Ir5TCMAViTPF8NKzVmVofktwQOlmNdNE8ExGTvHBSS80QJ9feAGum2eBC/KCrQANCIb854+g6BclqRu8VvlrBtzPBPJDPDsMA6BcOld3AxtSbjcumxBlypV7K+ZlAY7jqtT3gt3LeZ0CsFXIKM89qpS0/JqlD55yYE5Qm7K0RC/NKv4reOnek92hf32JvMSEZ+B+uIHuu27YbBDX2w6gorgDRRREwf72JJ1xqfougHtsZuWjKRKROii5QcUbIDsUmyqpNsYjTPIq0E3HirI2d0h5Yuz1zR5N56V9vIJ4/vkMrrG0J4AXZX1ZkKA/7Ti2L28bBvfqJRnEMwjeucgijikbODa03VN1vh9gzucTe9TpBARQhCN90SVKsStkwFn6mqLsAFKkPU45gBxqThTJ6jJ28e7rDaRKEv5I+vFvSSWBGa3Z3gsO80ZANkLmette112kGJVVszZTWCZKmgR3Z 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:(13230025)(4636009)(39860400002)(136003)(396003)(346002)(376002)(451199018)(40470700004)(36840700001)(46966006)(478600001)(6666004)(16526019)(83380400001)(6916009)(36756003)(70206006)(70586007)(8676002)(8936002)(26005)(40480700001)(316002)(2616005)(41300700001)(1076003)(186003)(4326008)(336012)(40460700003)(426003)(47076005)(356005)(54906003)(86362001)(5660300002)(2906002)(36860700001)(81166007)(82310400005)(82740400003)(7416002)(44832011)(30864003)(7406005)(36900700001)(134885004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:48:05.5024 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3160cc51-63cb-460d-9fb3-08db137300a8 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: CY4PEPF0000C966.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB7481 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: E98C61A0011 X-Rspam-User: X-Stat-Signature: 9pxzfat9nykrozqdffttytipo8m743zh X-HE-Tag: 1676918890-354751 X-HE-Meta: U2FsdGVkX1/uR3xagoT9Z94XBL6uRsRrJZivZcYM70QBvo/YhW7AustCJCXxGXY39d0Z+uTVtflO4rpTEz5G7/1Xd2bORpDV8FMUJQa9b7VJZV5tzitZrFt+b1q7X3C5X+4ir3a2iwuxV9ZDaWtsW2/zWrqdMRfY13w5P5FkO0tKpuYoWt6P6wnqBnnXk6Wq99VGXyDhegOD8Kj2f1xuA6F98YV77xFuM2dsrWvbyL1eRTHVihGr9Ganj5AS5qwBDhNjnPk6gbMmuPWOwN0CQ687fFfjhvG1vs72LMaxAVBVLtllkc0d/YZKCPAe3BCHa9eGzlC7IuiTttFyMc2xNxVaOcGFEAgqywScPCWx/tr4NKzrJn/wuIIuYGGAP05yeiD1EPvQZ4r2SHbaiUYeBsl1xZcoJoIIhpRelIXHEmE6cbYrKESNepbxRbD14G6OPMkroldCwEEX4SV0zBre2VrGLj1O1pXM3IEljwP3xgXNe2vNB/cAYe8Di2xgzLkV81zUwmaZIIaPiLF4UmIqJNdmJd+LCbS6SWiiVYfy3eUZAUqTS0LfjpEEkAM1puL2cmYt0pFyatHrAPGFo6YCNtk+PzN0UcxqyU2pU65L8k8Jio4psQ3dixnmfaEwrYFcf/fnUe7jJNSlKASj4KWN3q2KJayl2TkxLHtCCEmhs5JhjehPmcJd2r8c17eNJktxNzQA4y9qk0+a/dq8LdeSvBiOV8pjJR7CyiKnNYxVBw6oCaD57/bpQBEaVymKCGIwidApO7/cBBLNiB8yXcTRZZDUfyxuZJ1MWu1SBjjsIlsyybf7x4Ux61gC7gsXbuFoRXJyPlKDwh8V+AzwYxrNhFDYGzfRVeZZrrx8/2jZx4ZkaKfaSuQqo2LqKSciig9fDvA0bONRPebO8m2W3mTz4RCSkkQZhMtM9rlmoPAsaJPeRSUV5Groe8RzoAffGm4KGb2Ek+0Q+8GxK3iOdqy Q6SgzzF2 xrZpcPbsnf+rL0VV+YZj85UUkY6Mn4gSQn2goGNZXM2eKxOln3zTsjFHrnPAi2pYWLFyWGlR3WkXjlvctG0L0go9cmghSU2U+ygvhKlAHcKCfKebpR7TnrRq+f4du3jhbZAzhLZF5Bf7tRG6buWPUw17eJh4vZX0sLUnAGI2D7EXeWsItFdC7jPWyfGaK31Fs8PCrs2CWbZtjk8bahpcjnpFD2ziO4o/VA5QyuWpPdlE7tTPNCnOfdlF/R4t6+G226b2peWjpd9sweeUhNLdEXHzamrDzpZ5EnCzsRww/ONGoU9eMEAlnrIKlqzOdtMHfSApA/CqyDtQoNWc= 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 Signed-off-by: Brijesh Singh Signed-off-by: Michael Roth --- drivers/crypto/ccp/sev-dev.c | 225 +++++++++++++++++++++++++++++++++++ drivers/crypto/ccp/sev-dev.h | 2 + include/linux/psp-sev.h | 17 +++ 3 files changed, 244 insertions(+) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 9d84720a41d7..af20420bd6c2 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -26,6 +26,7 @@ #include #include +#include #include "psp-dev.h" #include "sev-dev.h" @@ -34,6 +35,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; @@ -76,6 +81,13 @@ 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 inline bool sev_version_greater_or_equal(u8 maj, u8 min) { struct sev_device *sev = psp_master->sev_data; @@ -830,6 +842,186 @@ 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 (!psp || !psp->sev_data) + return -ENODEV; + + sev = psp->sev_data; + + if (sev->snp_initialized) + 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 = sev_fw_alloc(PAGE_SIZE); + if (!snp_range_list) { + dev_err(sev->dev, + "SEV: SNP_INIT_EX range list memory allocation failed\n"); + return -ENOMEM; + } + + memset(snp_range_list, 0, PAGE_SIZE); + + /* + * 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 = __pa(snp_range_list); + + rc = __sev_do_cmd_locked(SEV_CMD_SNP_INIT_EX, &data, error); + if (rc) + return rc; + } else { + 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; +} + +int sev_snp_init(int *error, bool init_on_probe) +{ + int rc; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return -ENODEV; + + if (init_on_probe && !psp_init_on_probe) + return 0; + + mutex_lock(&sev_cmd_mutex); + rc = __sev_snp_init_locked(error); + mutex_unlock(&sev_cmd_mutex); + + return rc; +} +EXPORT_SYMBOL_GPL(sev_snp_init); + +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; @@ -1270,6 +1462,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) { @@ -1286,6 +1480,14 @@ static void sev_firmware_shutdown(struct sev_device *sev) get_order(NV_LENGTH)); sev_init_ex_buffer = NULL; } + + if (snp_range_list) { + free_pages((unsigned long)snp_range_list, + get_order(PAGE_SIZE)); + snp_range_list = NULL; + } + + sev_snp_shutdown(&error); } void sev_dev_destroy(struct psp_device *psp) @@ -1341,6 +1543,26 @@ void sev_pci_init(void) } } + /* + * If boot CPU supports SNP, then first attempt to initialize + * the SNP firmware. + */ + if (cpu_feature_enabled(X86_FEATURE_SEV_SNP)) { + if (!sev_version_greater_or_equal(SNP_MIN_API_MAJOR, SNP_MIN_API_MINOR)) { + dev_err(sev->dev, "SEV-SNP support requires firmware version >= %d:%d\n", + SNP_MIN_API_MAJOR, SNP_MIN_API_MINOR); + } else { + rc = sev_snp_init(&error, true); + if (rc) { + /* + * 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 error %#x\n", error); + } + } + } + /* Obtain the TMR memory area for SEV-ES use */ sev_es_tmr = sev_fw_alloc(SEV_ES_TMR_SIZE); if (!sev_es_tmr) @@ -1356,6 +1578,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); diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 31b045e1926f..8cfe92e82743 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -794,6 +794,21 @@ struct sev_data_snp_shutdown_ex { */ int sev_platform_init(int *error); +/** + * sev_snp_init - perform SEV SNP_INIT command + * + * @error: SEV command return code + * @init_on_probe: indicates if called during module probe/init + * + * 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_snp_init(int *error, bool init_on_probe); + /** * sev_platform_status - perform SEV PLATFORM_STATUS command * @@ -901,6 +916,8 @@ sev_platform_status(struct sev_user_data_status *status, int *error) { return -E static inline int sev_platform_init(int *error) { return -ENODEV; } +static inline int sev_snp_init(int *error, bool init_on_probe) { return -ENODEV; } + static inline int sev_guest_deactivate(struct sev_data_deactivate *data, int *error) { return -ENODEV; } From patchwork Mon Feb 20 18:38: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: 13146868 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 C5F9AC636D6 for ; Mon, 20 Feb 2023 18:48:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 693A86B0072; Mon, 20 Feb 2023 13:48:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 642F86B0073; Mon, 20 Feb 2023 13:48:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 50AC36B0074; Mon, 20 Feb 2023 13:48:32 -0500 (EST) 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 429B36B0072 for ; Mon, 20 Feb 2023 13:48:32 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 242818032F for ; Mon, 20 Feb 2023 18:48:32 +0000 (UTC) X-FDA: 80488556064.30.4412984 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by imf06.hostedemail.com (Postfix) with ESMTP id 3E051180009 for ; Mon, 20 Feb 2023 18:48:29 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=aqKl34TR; spf=pass (imf06.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.40 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=1676918909; 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=rRY9S+poCqn/XGHRQP5gYUrdAtRxMsAtv6+gdzhKppY=; b=mXqy2gIaJudLoAOIAPqDAZrTAAAwibRiiVP0cjdzX+PD5T048hM7urKMXARHdVAQNXjYJf 0WE5uXzN1GTJe6yYxf3NHBuEANNyM7KPeds0QfgtqDJB1RtZVZUUiKqcrepWU4CKls9fkg rq5tTGxWh/ey3GmtNsErhrzxsh4aa74= ARC-Authentication-Results: i=2; imf06.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=aqKl34TR; spf=pass (imf06.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.40 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=1676918909; a=rsa-sha256; cv=pass; b=TRJEIGz8ucYdiIg//4vIGqW6N2Eab7lzeCC18Mx/9NKKe57lIPrgB0ZYH0Yd4AFRaEcTJm ycGpgADuXVYQ09fe8cV9/QVuXjUlLYKFUjwb0T9UPzLtSUKiY3wzx91zLpSxLhVHxgqdjo 8YrjJKdTwIV4gpExgQ1L73Q+cxqG3nU= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=e4WRyFoqpowrNCmAvcBEOaTwVqCkS5Ypqsn4gb38RX8P0f6PzeLVf5NI6XeuXVMK2AHlpy5AZ1DNU1/AbA0oVrEk4PHPBD8auQIIwfdOL6UXhGwCrnUq2s+2VUSF01d+LKNLCnkMYGs3Ox3MRm6Z9jurkLvOzyEYsi8iTjS6PjoLR5bKSXmVZD2MRQlNIfmZZqy77u8Y9rfpAV0zdcOXLS9lW/VfDv65QnyWeW+wsqwoO4Q+VxSsJSWGHakPSL8HrQMvUgO1FpDwS4VRGDKZI2hls1Vq2klXptmCxWsB0qYTA11VaYe45cCUYFFAIjgA+uh3id1HAEvQf4yuH5aGqA== 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=rRY9S+poCqn/XGHRQP5gYUrdAtRxMsAtv6+gdzhKppY=; b=S639+u09irAeLBT3E7PmVG6QyYmWQnpPOiIz3k9Gg7PM/jv95TzoUDUJ9Hr5Q7W/lGiZDXdrCdiJEXcE/kPq9d1NZp57qMQLrEHJV4YBi/a8UeicNoVkkwYa7TI/DBpsKJOS4ECMEtlpye2UM/UF+PC/5HuDkijb8PGh7l8x8grGbu6IzLgHK4uWZ2bi9pUYTH6ArPyWyBphdOJFpoK0IvLcWEGbSnBTkpuoeoN8KrCZyqho1+cea1gT966q4QOGlXoBh/hl+AEhYib9aBU+2siRXyhk/Tsnv+AqHHh4iKo+wbdLRMyLyjICWp+zZ2OmYLZSohlMV6VM/f/6IbS+vg== 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=rRY9S+poCqn/XGHRQP5gYUrdAtRxMsAtv6+gdzhKppY=; b=aqKl34TRfitl6kPjGIpLHPd3bbZlcFzAolP8XprAUROMje9WJZMx+hSG1LZqCloBHLsaLFzXWlB6SNg7v/t/SEqaR0u4NcbQxNY1oRh3A18MZwJXkiNFyYfLgrPnoXSux+Ce+NalmdCGFU7mchHwrgi6qjiZ3S4qB0WBF+24J50= Received: from CY5PR15CA0022.namprd15.prod.outlook.com (2603:10b6:930:14::30) by BL3PR12MB6428.namprd12.prod.outlook.com (2603:10b6:208:3b7::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6086.24; Mon, 20 Feb 2023 18:48:27 +0000 Received: from CY4PEPF0000C96B.namprd02.prod.outlook.com (2603:10b6:930:14:cafe::7c) by CY5PR15CA0022.outlook.office365.com (2603:10b6:930:14::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:48: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 CY4PEPF0000C96B.mail.protection.outlook.com (10.167.241.75) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:48: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.2375.34; Mon, 20 Feb 2023 12:48:25 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 22/56] crypto:ccp: Provide API to issue SEV and SNP commands Date: Mon, 20 Feb 2023 12:38:13 -0600 Message-ID: <20230220183847.59159-23-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000C96B:EE_|BL3PR12MB6428:EE_ X-MS-Office365-Filtering-Correlation-Id: 7c5e00d7-69eb-427f-5169-08db13730d22 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ROLlBfG8ETRc2hPOlTR95vXUNCQojg43+RtuuS0+z+uh/NaBTz8V4XOAVNuBzYhjc4SzJCCUCJhZOhBsfMKIxr/mCRp583BgCE0CuFKKixmFTDrj30ntX6ZB5CGVukcmeZI514egIC8CYuxw7azfByKM+uhDPeMTc+fG6bqMvCxGaXj5w6J9KhodP0EQUFGPTYuNHpmK9J5ociVT4Ok1KTDccznH6pH5Tc+6lbtHxUQb1SnV1scBgucQtreuJpq4hRVCw0b37/YPZDGpNJiJzh29mqSMKej3A550RZtmPZ3RPv37jfYX2JiLr3/scu2QLVa92rJ8ciKyjj1Q3K6C9rxyvYX0nPg1jZhq2LSrg+gQGT/c/LF9S/IEnBudAhLRYu0O6EFdGepb+XndXKCzlIREulZpfHkNOf52FD99gtlcFeeYC4esODCkBzMawg8PzgT3gDa/2G/C6PfxFLIpcIOgpH15qlL4ETkCr+Jwgy0veepbhDEWohNxlOM5qbsMCT6rYj1J5eu3UU6NlEdxKB9asgVYBypQ6YMui4b+sNFibR1BUNIhJn4UPiOk1TiW1cB9U2L0ca18/hRYmzu0Ki+BPNKKvzkwp/vVKStosM2FGgeooHmLS75XSVgjJiMNi684ikwCk+Z4zB718h05B4F4xj76UdOcIrxxqkVb7XIAJwT62XtnCcQo8+WCbWLDjoSkPr86+J3ClBJywiczhQ6lr1TjW1WJZQ3Nl7o+Ubklg5/WAxiMUWZ1DNBFBTwIH1QVEmbyxsNuEUEteqPU1w== 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:(13230025)(4636009)(136003)(376002)(346002)(39860400002)(396003)(451199018)(36840700001)(40470700004)(46966006)(86362001)(6916009)(8676002)(7406005)(47076005)(41300700001)(54906003)(44832011)(70206006)(40460700003)(8936002)(36756003)(356005)(70586007)(316002)(1076003)(2906002)(2616005)(16526019)(186003)(5660300002)(26005)(478600001)(81166007)(6666004)(4326008)(7416002)(36860700001)(336012)(426003)(82310400005)(82740400003)(83380400001)(40480700001)(36900700001)(134885004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:48:26.4483 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7c5e00d7-69eb-427f-5169-08db13730d22 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: CY4PEPF0000C96B.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6428 X-Stat-Signature: p3tog6qb4g8jtact9i9onaw56pdityt8 X-Rspam-User: X-Rspamd-Queue-Id: 3E051180009 X-Rspamd-Server: rspam06 X-HE-Tag: 1676918909-689299 X-HE-Meta: U2FsdGVkX1932YLY7PEXiOxfL1oWkJbZ+Kw36htue2S7w3J2QTNRSlhjG2vflXbz87tNpIrkGUwO/qwCPvhSb7+m4PwLTWxELvtU2uMoOTna9RDezr9TSDKYvMFlw7wiaDA1QthOvc8TKH856eksIaBnIKNMsbjhhwk2dRjQ7TarZY9lYjG4tocD31I0vr3q0647x9/PIB3vyHnYL9Y6PKiKr35mLAQUv8IjWaNrvFpWzspCtYBZtiWxVyPH+34njHN30dhMviP38sfKWohvTOkc25s6Af+QrnhuaWzdzOS5o6zgqVcpRrABOHi/VcpsbwblQlKC5KhyXmvYvk1aBwaLmzLEOcJYKpNoMRM28vVv8Qu+5eeRLdQqa2URlI1IEUfShNVCzwMIAHxpLC8ksphN+JV5nLuQtLsQ02Fs7JPCO1sCbUYXyi9jQ6jTqYJ3nceaRNJnEWRiUUCB3D4sRB2+Vabc5uA4mKnwcXtTxUn9iEVCYEXnkNoAHEQwaU9hee3s6rhG+0djBIUBPJgSrUaJtCRhkHAQarlaedwaf2hWus3XhAn4+qbtWYOcsIE4FGZ4xbmYlQPSh+II2FjPYpiHD8JQw28VkGILvodjwJpEP2rD7qzQI4QRzpbsTyxIL3taFue9vfvs6UInQwozW7G0xX57XGoG9c/zDAROt6Ij7GFMlfaYd8qfhlG84uij2Gq8O3cVHE2Jayx+GtgQXRJfFmCJuHtUywILsksmjn9Qvbslh6xsgxW2QqmrV5DevoZpF1zrANbR8yutWQGnUgJHkgDEsshVXD/K0ygGMgLyTXLlQb9t8VGkf0nEbwMwhCT2qzAetzO9s4UHEhaIHnDO5x5iuBhNunzdsQ6ky17Ux3W6khZzuWwSMyXfn0qtD82yE+rCsH2rrlehbU0EtddwkM+lAvtWtBcxm5xbc7QbfUu5kKcNWgzTp6XD9lNc8LhvuUQSFJXgDw9Jy96 Jhyn5BUt wR6CzEuBSbqtsBbeEkyw7qZHbaS9c19qYgn8DPJSxzMbefuTZbo456gXx2SA0Le4MKUxhS+ScNfeREzQq8iRpexFaQZMNdOong2cIGikrbRhawtq4QJLyEdaiN3DqUc9g4BzlZ9hVz+YfOpGDXWn+Zn79ZTtVLwxKp5FtyPLnl+u/9rWB/9NxexfDBjdpIDuwo+Ry2XtIlWRbeXyb40D0LA4ZIzTZC+ICipaR5XfbQBLmwEXjQnBHIsYXhZJo7I5JsVNvuefpYO/jqMU/5FMV4HfDXKcwnYAPW6KWKF6ylhd/srsrQFcrLVQq9jdVB9fh1Q2xHL5nHtNijCbu67/lscguJHwhwIC2jsr2uLuWtbsph30= 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 af20420bd6c2..35f605936f1b 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -415,7 +415,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; @@ -425,6 +425,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 8cfe92e82743..46f61e3ae33b 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -907,6 +907,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 */ @@ -924,6 +938,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 Feb 20 18:38: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: 13146869 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 5A78DC64ED9 for ; Mon, 20 Feb 2023 18:48:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DBEC06B0073; Mon, 20 Feb 2023 13:48:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D9F0C6B0074; Mon, 20 Feb 2023 13:48:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C5D896B0075; Mon, 20 Feb 2023 13:48:54 -0500 (EST) 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 B9E506B0073 for ; Mon, 20 Feb 2023 13:48:54 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 964BDA804F for ; Mon, 20 Feb 2023 18:48:54 +0000 (UTC) X-FDA: 80488556988.25.7B1395F Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2063.outbound.protection.outlook.com [40.107.94.63]) by imf20.hostedemail.com (Postfix) with ESMTP id 8DDE31C0008 for ; Mon, 20 Feb 2023 18:48:51 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=n1NEJiIu; spf=pass (imf20.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.63 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=1676918931; 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=seEEnfJ+CbxvtpbqyiuTUKriJBTOcgWKYjy5NAOfINI=; b=fWHX308ANr6IAMpY5YK6g2HzlJoDn+lQ76x8uGg2AB/SxYU9ZnMeKV3RSbifAN/cidpjq0 diELb8bi5Pm4UkEW0of1i3AFrmZnbL617bm3KEt6Q8b+vh/TAhK8Et2Xgy80qlGCKfK8Ss 7MfILJb63UMbz8EOIavIHDEOldeLEYo= ARC-Authentication-Results: i=2; imf20.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=n1NEJiIu; spf=pass (imf20.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.63 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=1676918931; a=rsa-sha256; cv=pass; b=qBSkumcCroiLrBgAxmq5B7YR+v63nS45KG8J/9I3H8CovkJWuiWiG89LAueV7oJD8+zMob 5M7AMgm/CMnPUgxZ8VqhLL1iEidLaU+hTLQcL/AVTpsd5x23MbhA8RlOCj1IoAm7C9GiNJ ybDtDgbX0plJsetsU/9X13lPEI/CEW8= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ole9GU0zWR3/b+S4gYdeyVkdUo/cMFnNZaILpCDDxbXPNgzNFTjB7CQ72t3pTDmRi+eclw+PLdx2/WOkLfArgjyPjCvXSO9lX2YTTDyVVTdhAC4922aE/Ar68Ypj9eI5Aaqp/S+Zq7DlyyoJzqTzN6y+xAWcJqJmu6kY3hxhscXz2/g2FFCi0xcV60hDBNgt4ArcGhwNlG2w96Ci69LHrOpxsEEXUloEft5HBvxNyrP+4clmXarIrSTbSKk3EPGBggYWqivvu4sNP6TrqA+CfmVCBsx79aZJLLSEe0tq69U4Qp7bXm/3MjeGj7GmkQCb3vNwOjbYM1hh4yVa/6PcrA== 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=seEEnfJ+CbxvtpbqyiuTUKriJBTOcgWKYjy5NAOfINI=; b=akJ9eaS11qk3imPQFZ3n2DRgLgrq4BAws1+6NFqSjqhWwzpHl/tw1SLwFFAeQfEWRqdGmGQUPqZEbSyGrwF9arTybMSSpNceKKxWIU7ofZI9vTS/QO2XRseXl9HHgAZDtJQj81rkqxYbukiVxQt9Pb8+wBmbzW06h5f1oQsxIVnGikwiPz7wUpU8BjtU7YZfKg5OshHw2DZSn/iJROtXSGut4HVQ0lTDnvcfXwtKBQxLf98omToj8MX6GBo/vchrH+bJt+o0EGISlgAMSgXkf2FfY04WSuU/ZXwjftn3eHjCVG5tSFj/vca6jzkePcvcLaRlUTZrrll36bohDJ7vIg== 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=seEEnfJ+CbxvtpbqyiuTUKriJBTOcgWKYjy5NAOfINI=; b=n1NEJiIu9sy4pdj/cbLikX+AtOBHwb345q8eu0MtzHxLrKnr1ct/3hyV0sYKgqhOEshMyLyOmHkw01EEWxvXujTsrbWRkXOql/AnQT8CYaP34xWJm2abcGFDWu33jEWpeRkaR7bwzO9ZWxKLjlU00lw0noaAy7Fl+Q4bqX0035g= Received: from CY5PR15CA0025.namprd15.prod.outlook.com (2603:10b6:930:14::34) by BL1PR12MB5972.namprd12.prod.outlook.com (2603:10b6:208:39b::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.18; Mon, 20 Feb 2023 18:48:47 +0000 Received: from CY4PEPF0000C96B.namprd02.prod.outlook.com (2603:10b6:930:14:cafe::4) by CY5PR15CA0025.outlook.office365.com (2603:10b6:930:14::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:48:47 +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 CY4PEPF0000C96B.mail.protection.outlook.com (10.167.241.75) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:48:47 +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.2375.34; Mon, 20 Feb 2023 12:48:46 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v8 23/56] crypto: ccp: Introduce snp leaked pages list Date: Mon, 20 Feb 2023 12:38:14 -0600 Message-ID: <20230220183847.59159-24-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000C96B:EE_|BL1PR12MB5972:EE_ X-MS-Office365-Filtering-Correlation-Id: f230206c-2aeb-446a-2a31-08db1373199d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: by2b9i7Dx5H1HZZbyroDY7gIQGOuRUiDqZfbmUXkXW4Ohr8JRFbGci/RAF3GQ1mLv0z6IFV6RytZd3Zpm6l2iEZVKMK8Mapx1duFmSa7jAfijfAuTrFk4iNgmYmWjuCPjmey3H+yfXSbVx0s/WnBoS5qjFS1/qLC1b5SbY+hv3qODW6XQkVuhBBO8TNAHrtuWnciTlR7VgLpYJDeOdVyGIu+WC/JNSN0y2AwK5OwoqP3DBXr3vv6T/B2nyfTnehJmcAXfcjlFa8jZUjEmLJwfunS1hjw9cMdACDGqRRLozCC9VfYJt2UJ32sjc6upCrBRbgu1t4cZx3zp2VHFANuKUMJ4Fqxsut648NuCRaAlIzpE0e2kgzzBDEakbdxZDHKIfTtzlGJJWz7by2kb/SE2ZzZVGB7OevVlxgIiOzdtO6K7Ps20UX7KxeAZCuNciv6m7w1OJIDudtUmoB/1SUYhQsY97A45hqDqQlkt5/IZrUBhRVvFkN77al8igmU1XIdXKpJbL7ZrxKpIyeG4IN3mzQe0eFnZ1aen4M2756m4f7cjI/5T9AwmJbsu8biP9SlTaAx7IOVp28gSJ5/FTaEwdPMAWsTAellO/g8jR/Z0hGYyoWuGEckEqixosR9huzNo5DnxuTkqhb8LHCsw3tc5npAxgWHhH8QbyR6poNggYR4Msn7SMJ5Q8osOVb4I0Sk3MS+qkZBoOXBhD5WcFyVd+CmO7VyjbhCgbA2r1BRoqg= 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:(13230025)(4636009)(376002)(346002)(396003)(39860400002)(136003)(451199018)(46966006)(36840700001)(40470700004)(54906003)(316002)(82740400003)(40460700003)(41300700001)(16526019)(478600001)(8676002)(70206006)(186003)(26005)(1076003)(4326008)(356005)(70586007)(82310400005)(6916009)(8936002)(6666004)(81166007)(44832011)(7416002)(7406005)(5660300002)(47076005)(2906002)(336012)(86362001)(36756003)(2616005)(40480700001)(426003)(83380400001)(36860700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:48:47.4016 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f230206c-2aeb-446a-2a31-08db1373199d 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: CY4PEPF0000C96B.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5972 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 8DDE31C0008 X-Stat-Signature: 186bq3s6d54ar593iyxbxs8knih9mjpx X-HE-Tag: 1676918931-147263 X-HE-Meta: U2FsdGVkX1/3YbcpX2Dnt5CQ0WvL/SZ48mIixAq+zZPr+zxQi3f7ARDcNLxMRjSYpp2wGRxIPsGJ9a5SK9YwSfAZLLf6FQKU1xrcCuttVvlo7S2+UAPT7awz7CN/7Yw1wrcGKqFxFwMVFAfS9zqLsXqxveqysCpVVpMcGcah0c6hfB6WSqLBBY2l9nLAGV80vdRSdGEvyqe25g7xY1y2zd65/rdWxNIptQsAVWi9X9vClugozNDYv7nx/xU8Zm/0HRSaORFq13bbq6uBNsmhHpaAa4ysFFSnGdE6jp8jbqxXamqpsVRltweLKSJe9qGRnwGeQMe8aud8iF4sqP4a8AK0/8srUwTKJKFweb+MpzRIbb5n3k0YsPOARG6gICDHH+HVrWra+IkGY6oodwhi9aSC3uvmFZyqiwn3hfXyQpLkMqC2Xv1G1YfvwPehoSwmxo6bHhw8msDwywsU/q4B+YsdWYYOAbXDIz1kOBivrYZAMq64LUJjTTcX2EG0MVwOQOXkGq930ZOMr7pbUjGwgt0qzw2VNiHL/8J8zyufOhvQjQs4tlneY6x0ro3zkiYGylQUUkPKMwj4Z4Ej6tSecqcDQyeDwFT6k29bsOYdWowPIsD74ZhzXHAZhQDKQefHZ1INKS/HOmlMWPNfmgd6yOITvulewIungj9UXAjlR/oo1z4x1UXBUxzH5QPGv1oKMWZ4+2raFolS6S34dfP9VzDG3MzQ/xUpiOIrxVoKE2V77F7/pL4bftcctJFMrKfIYKzwdnrq1Z/vkcW/YiPb6o0NH6yyMyVcqrw6ekjBwjCpURlamq8ZBOq4eVA9Cmo9+jKtgo/qAUOfu0pgOYgcogYt7HaVKP2DXtRinEe429XPZwGIEE8edb1eC/FsAIIrrkPeFGRd4i3LSSmJ7/QxBgmoiIbJmMg4L+gDUNKtjc6cKj00wUbqQynUb3AlJcaiTgJZPqXnLtkNIXELnxg K7uSl/UP b34bLZS+pib0xDawyD7eGgQcVIdqk2jnDatzR+Vio6VNdYNCDppJl59wdNTBhFIHkjBRYNpamC4wboPwkYLnjP36PwGcl91v+S+jgz52tywQ9yvPEgGE0uGLbZCZTbCzKU9HBd4ybMVXh3AExZ+FLvlFPlcuugZNz68O3I0+5E2XNPsvmLq0GQkqmkkiUezztod+XyW5QK/CBPFvBfRaw/iwnKombqOG39On4fkwpJd41mZly5qGPzu+0ccR50FE36xi/eB8OZ5EfFUgTRz+ghC0hXftVcsf/a0oFega6kSbPkZFmRnFoaTuqh0ldho2S08ivDIB+lpbXwBU= 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 Signed-off-by: Michael Roth --- drivers/crypto/ccp/sev-dev.c | 28 ++++++++++++++++++++++++++++ include/linux/psp-sev.h | 8 ++++++++ 2 files changed, 36 insertions(+) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 35f605936f1b..eca4e59b0f44 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -42,6 +42,12 @@ static DEFINE_MUTEX(sev_cmd_mutex); static struct sev_misc_dev *misc_dev; +/* 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); + static int psp_cmd_timeout = 100; module_param(psp_cmd_timeout, int, 0644); MODULE_PARM_DESC(psp_cmd_timeout, " default timeout value, in seconds, for PSP commands"); @@ -188,6 +194,28 @@ static int sev_cmd_buffer_len(int cmd) return 0; } +void snp_mark_pages_offline(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_mark_pages_offline); + static void *sev_fw_alloc(unsigned long len) { struct page *page; diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 46f61e3ae33b..8edf5c548fbf 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -923,6 +923,12 @@ int sev_do_cmd(int cmd, void *data, int *psp_ret); void *psp_copy_user_blob(u64 uaddr, u32 len); +/** + * sev_mark_pages_offline - insert non-reclaimed firmware/guest pages + * into a leaked pages list. + */ +void snp_mark_pages_offline(unsigned long pfn, unsigned int npages); + #else /* !CONFIG_CRYPTO_DEV_SP_PSP */ static inline int @@ -951,6 +957,8 @@ 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); } +void snp_mark_pages_offline(unsigned long pfn, unsigned int npages) {} + #endif /* CONFIG_CRYPTO_DEV_SP_PSP */ #endif /* __PSP_SEV_H__ */ From patchwork Mon Feb 20 18:38: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: 13146870 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 7AF76C6379F for ; Mon, 20 Feb 2023 18:49:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1B1156B0071; Mon, 20 Feb 2023 13:49:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 139806B0074; Mon, 20 Feb 2023 13:49:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EF5766B0075; Mon, 20 Feb 2023 13:49:15 -0500 (EST) 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 DCB7A6B0071 for ; Mon, 20 Feb 2023 13:49:15 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id C4698A0360 for ; Mon, 20 Feb 2023 18:49:15 +0000 (UTC) X-FDA: 80488557870.23.496DD0B Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2085.outbound.protection.outlook.com [40.107.94.85]) by imf04.hostedemail.com (Postfix) with ESMTP id A226540006 for ; Mon, 20 Feb 2023 18:49:12 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=TjAb+zXa; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf04.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.85 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=1676918952; 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=cqs+ScbwS9SaGm6GdjYtLpsrVAyQkW77Y2/rbuGN220=; b=8NK2Gsje8Mb0AeiWShn0BejzW3kzzaipKTsaZ5apQQdd6MreoD95GMl2CntHFuQurZvUOo w+WwB+10tNhpIAzKFrdcii86YdjCzgDpYIoMcl0kqd3l9goNQN4iq/ElASmY9ikX6UmP+2 gBxvs32I/wgESU+ESgGdggCnSI9+Fms= ARC-Authentication-Results: i=2; imf04.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=TjAb+zXa; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf04.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.85 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1676918952; a=rsa-sha256; cv=pass; b=62sMwLPBW0mC+VXBlCH6AuwB2s3gNV8+ulEuzQeTvPddm6yrfRA0WHuxeLB1fmSsWfvcm8 PpuS0oslGSJy12l5w2lFXnij4mVDksPZ9lh/YdKCkE4baTeAhfrsf8NDKUUwZWIclLTrqb ItAF33nlOMTuD+xr0ay7X71MKBASo0Q= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ba2sPBd6+TvO80X9LCAwVcJUYUC/iSF0XjskWsZfRdXx8/1vFHnoAUGqFmelJU06IHEVrcQZ+o9mBpYaeyre/Q1zk/mXJZwOL6v+NxA48DuyHpkkPXU1TTTu+clcKolKcngQyqFqKIfBBXLxr9w5wgur/P8IOTeh7aZTv12XebpN7yffqszZLaOiW/nel10/pnRrOZYF4ciUSt2C2V4DRejp8AE0BEC7/8eqvnKxkUXRfEhWupvGfFwnTnYj/dFu+DZ5QNZsbfQlYLqXnpwOuzTQhk1eUi5Uh7t20/y1cPdGOs+Db+w0CDw8URZ/uE5sIb7PsHgFPF+7+9YNBGY1Ww== 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=cqs+ScbwS9SaGm6GdjYtLpsrVAyQkW77Y2/rbuGN220=; b=I9qTrKGkNzQgKmMWp6z5KOd3uOJCL0eopg+/vuTR9J0XMwnH4TOV04vLvCDMXCMw0vtKh6UQWd0bOqaFFjr5d61B9HPv9BA9J5a3PV0jYVGcYyJ0DMLd9PFtPF7ZSeRPUTQ5/Yrqdi0OO4vIm4dDqnF4McWS/Yy1vTZBxJLQOGQO9qWpg7l8KIYMYJJVImN+EV3KF99j1k1Lqy2bNwAmlG8IPwIFr7CQu1tTpF8j/X22rqhtyRDOqU8HbwGcBZbEyqKagfOHpnUatnWlIKjdoy3ofav9KXqJMlNGb6wVN2FP+N7ScPNF3IEun7xK0VhLULnGkwzeSkNcKR8RCEbPpA== 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=cqs+ScbwS9SaGm6GdjYtLpsrVAyQkW77Y2/rbuGN220=; b=TjAb+zXalch/7DwFUedEqazxoO10/TnxHgh/exabkwtw4gOPqrbRfUaWKqvxjHHYnALQq8s54xpn2iXEkaSQmXmJMIdT8gdOrn7nVxClTz7L7qZ5Bq2c4sz9SqvzdCRIJnyRlWZ3s1wj0jUVpzGuuYcSYEl/Bs9jZaQ1z9MVUdw= Received: from CY5PR10CA0024.namprd10.prod.outlook.com (2603:10b6:930:1c::24) by DM6PR12MB5518.namprd12.prod.outlook.com (2603:10b6:5:1b9::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20; Mon, 20 Feb 2023 18:49:08 +0000 Received: from CY4PEPF0000C966.namprd02.prod.outlook.com (2603:10b6:930:1c:cafe::39) by CY5PR10CA0024.outlook.office365.com (2603:10b6:930:1c::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:49: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 CY4PEPF0000C966.mail.protection.outlook.com (10.167.241.70) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:49: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.2375.34; Mon, 20 Feb 2023 12:49:07 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 24/56] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled Date: Mon, 20 Feb 2023 12:38:15 -0600 Message-ID: <20230220183847.59159-25-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000C966:EE_|DM6PR12MB5518:EE_ X-MS-Office365-Filtering-Correlation-Id: 31847463-33b7-4e3a-bf70-08db13732635 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: nlLV6aJFHDivvWcwmq+ZfAt4q2dSS0zhQ4RzQ6y40tb1Uleli7t7lm5Or670bkWby4tSSGHH6GaKdgtcDFZ5cRUgWmf7b0icuUcHrgbTnOs3cJKTRDmEcqrB9g37JoIlBmKJIAR3jV2HdOpNGcHt9yNpFn23A0+aAZsoWFSBGs8pZAdip6wHyskf59D6XKxHfmFxLPbvjUozFfkgnMjVLyhFb/mCa4AhIFxL5T0JLE5ai8yDc/+Wdo8tBVclWlarb4RclFz21K1qEazXlX6nQbIScUXoTF+OoBmPNhglTCCvqiyi/938YMEZ9jVP+5JZZDRkDCUt+1xJzt/HzzqWTDppkiRq1B7sprzx/33U6s9FRUCAgBgj2Hu6/iOoHxdzevRZpz4e/nsHwl/5WXWxclBHc6U3SsWyB5CKC9HDRoHRWLZdAX4SpUhraDFyPEWssSRt4d49MVwULI+6Baypw1ZenEV+Cjo8yKo5DWWa1WsoExbj9mUtUeJIkzm8T/lP6iFCwklqmmI1ppAEwgAEIDYlFPgJrBvLtU1VV7K2S4HFDj98MtOhYArZoIpDlqjqYbcs2WCvN9ettBYP7KMMB9JWzoDEfP6JXnkKz9SlSi8h2qJXWwq6TGr7Z8GGDCO3ekczPK+GODVlaYx9j+VBIYsfgb/Sdj9+PWNEnrKt6Dvy1SKFpdq/77QghxecTF7yZihdVofNqG+2dvKOkT2pg+tdlZMKHOYVj9Zzz4vliy8= 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:(13230025)(4636009)(396003)(136003)(376002)(39860400002)(346002)(451199018)(36840700001)(40470700004)(46966006)(82740400003)(81166007)(82310400005)(36756003)(356005)(36860700001)(16526019)(40460700003)(40480700001)(6666004)(26005)(186003)(70586007)(1076003)(70206006)(4326008)(336012)(2616005)(478600001)(54906003)(316002)(2906002)(8676002)(6916009)(83380400001)(5660300002)(7406005)(8936002)(44832011)(86362001)(7416002)(41300700001)(426003)(47076005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:49:08.5186 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 31847463-33b7-4e3a-bf70-08db13732635 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: CY4PEPF0000C966.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB5518 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: 1r7ekohjyq7ce9ygoa6qpqjjzsz3dwwf X-Rspamd-Queue-Id: A226540006 X-HE-Tag: 1676918952-659793 X-HE-Meta: U2FsdGVkX1/q8gOVMx/6z0/CqyM/4OaepDj8/EPmPgbMunNHqRjngZtMBG41fnzbxZ0uYpvPZLxGjDbGBYZXcYskfuUwiwAGBbX3NeMOmw2xxvWDBkskMGcvDu9sSTbLS1T/I4gDgwGTONnuYrk+0bYCRO4FB5dYKjL4ts2knUioQOtq8BE6E9UFR7uDLqG6nCrXrkeM1u8z+wgCPAwlA2UyoN6fvMOFTVZKP45kRjScMPQKJD+ror5/Q34o7Dga7/ecCiKfrAoy9tSUG7ryfvvsABdVH2JlSMH3eq1o1SUq02SQoK3JXSdYFyGoOmdCRA1V94TZPzmbSHNWcImlD9Vw5e24SRGS6M8GiaPI4PCrbK4LjohqqHVkH5FJ2KCZaQbT7dPEEgjSU4p5x5xu+r6jUByo27wQpxxypIwDfjLc/4dy6HKeFRB9qiKOMtlknfj/HYJx6+sYCYFaY93kB0cUN/+FExN4sXypFb8HtA/CWxxxeRrOVv+rlIiCi9+bmRFoIUhKSRA+ChkpYbw3EPF5DiKPT3R6XdjsuqLgmvdbcKeLRF4b9OPitrxJrRSytp3eRwQkGlwpRL+Yr3Vt344s0H9ABFxRx+DzA3L8nncnpdWuBOKL1jYTpVk8CMginrerwPf8spyMGO99menI/BoakQceC6mPEFKLBrMkgHpYPa/CuZbKZmgSnZi2Xin9Z5P5IVLMkVyh4CEYdaLmi5fpmKg52lUx/g4u+kjfPSGMoyLpKzGJyvGpZf8L0LIEIFQRc/F0C34Kgq6mmAHfrsK76fYQqoG3HEd/+beS6gSUwLLqCsPkvu5qwwSH83ZSg5z9EpGzn8AFrizWeHb+3tALVgIuzgwRnGgWVqXuC5TiV1ARYsgru0qu1QHdLqGRbrfUeF66bNfnrPzKw68LNwrxrs0DDBV9mYFFq1Vsi+hy2kxQ1JrlR6muPmMJU8aGR4BhSDj0cTj9PomRqtP m7xCRBde /3Fc9Rxu/dnFg2nL1VwMaIyOTK8oTACwYmQZMRgaqrfnPTvOKH5lCYJkAj24zkdc6PbZw3aEQ3L5tVqUR8oS1nINUlGmmnW6jhWAeFjx/c100wKMrEK7QzrA76BagRXn/8I6zMZO8Zjehvn2lD4Z8V92h6ZnfBb+1qV6KzfMYAYUQtnHPoMi/DKoEv0Y7y09JqlS48tjohKJ/DcTBN3eScTavgtkt6aFBukslUo1QOG1agHy39X+TTrTER3uAYNY9gZ18QHuexs42Jm5HbhC1arbP+u8mKjdBxU+Xhdidu543qjrhtuv6uauC25O2GLLCRDJwnbyPfgGnMOk= 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 Signed-off-by: Michael Roth --- drivers/crypto/ccp/sev-dev.c | 148 +++++++++++++++++++++++++++++++++-- include/linux/psp-sev.h | 9 +++ 2 files changed, 149 insertions(+), 8 deletions(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index eca4e59b0f44..4c12e98a1219 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -94,6 +94,13 @@ static void *sev_init_ex_buffer; */ struct sev_data_range_list *snp_range_list; +/* 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; @@ -216,11 +223,134 @@ void snp_mark_pages_offline(unsigned long pfn, unsigned int npages) } EXPORT_SYMBOL_GPL(snp_mark_pages_offline); +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; + + if (!pfn_valid(pfn)) { + pr_err("%s: Invalid PFN %lx\n", __func__, pfn); + return 0; + } + + for (i = 0; i < npages; i++, pfn++, n++) { + paddr = pfn << PAGE_SHIFT; + + if (locked) + ret = __sev_do_cmd_locked(SEV_CMD_SNP_PAGE_RECLAIM, &paddr, &err); + else + ret = sev_do_cmd(SEV_CMD_SNP_PAGE_RECLAIM, &paddr, &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_mark_pages_offline(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; @@ -468,7 +598,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); @@ -491,7 +621,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); @@ -982,6 +1112,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; } @@ -1499,8 +1631,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; } @@ -1511,8 +1644,7 @@ static void sev_firmware_shutdown(struct sev_device *sev) } if (snp_range_list) { - free_pages((unsigned long)snp_range_list, - get_order(PAGE_SIZE)); + snp_free_firmware_page(snp_range_list); snp_range_list = NULL; } @@ -1593,7 +1725,7 @@ void sev_pci_init(void) } /* Obtain the TMR memory area for SEV-ES use */ - sev_es_tmr = sev_fw_alloc(SEV_ES_TMR_SIZE); + sev_es_tmr = sev_fw_alloc(sev_es_tmr_size); if (!sev_es_tmr) dev_warn(sev->dev, "SEV: TMR allocation failed, SEV-ES support unavailable\n"); diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 8edf5c548fbf..d19744807471 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -922,6 +922,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); /** * sev_mark_pages_offline - insert non-reclaimed firmware/guest pages @@ -959,6 +961,13 @@ static inline void *psp_copy_user_blob(u64 __user uaddr, u32 len) { return ERR_P void snp_mark_pages_offline(unsigned long pfn, unsigned int npages) {} +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 Feb 20 18:38: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: 13146899 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 018E5C6379F for ; Mon, 20 Feb 2023 18:49:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8DBED6B0072; Mon, 20 Feb 2023 13:49:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8659C6B0074; Mon, 20 Feb 2023 13:49:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6B8116B0075; Mon, 20 Feb 2023 13:49:38 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 54FB26B0072 for ; Mon, 20 Feb 2023 13:49:38 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 2EF058048B for ; Mon, 20 Feb 2023 18:49:38 +0000 (UTC) X-FDA: 80488558836.22.6C69F10 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2083.outbound.protection.outlook.com [40.107.92.83]) by imf05.hostedemail.com (Postfix) with ESMTP id 2C6B2100007 for ; Mon, 20 Feb 2023 18:49:34 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=mhJK6duo; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf05.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.83 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=1676918975; 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=oyEis+QxgkD0rrC2Cj501D7OqFc3o6stBa7O9h/r2NU=; b=YReY2/Sukwjz4s7Rrv2cQv+zDOCIlKBEJEdV0ia9qp+45Ni3Aml4aXTyY4tvVZOLrioDXn mPAXMaLSSmtxAB1lll3i3+3q7ceDIB3ZJ5MEcZolS3ZLDhNrNuJyVj0ik0M9I2CcMr0wIL 7Rkubi22+GmrFiJaqqMNY3W6UDDUVMM= ARC-Authentication-Results: i=2; imf05.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=mhJK6duo; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf05.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.83 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=1676918975; a=rsa-sha256; cv=pass; b=7FNG9f+JGUhGnVyMQSWR//u06SLXG89IgzVMhvQFPd8mbxOQMrX/2ysM+SmESMXeLo6j8C NOH1yOI9uqM7zyXJP3exTXopq4UFlAZrojfJaPfrlKew9ykk6vlqiMReVjvDu57y7a21Ox yuaRaZIkOAVLJP7UxcSPU+3v284XGAE= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=iDnfICBIXlN9ASRP8VHbRS3vXeOj94vCzIW0rncMT6q0sfA4PuC7HRQcWM5JlMIQ5+ST8pjijjK0x7DAVACL7hU7nbJFPca2v6vgirR/nbuJ3h6QBKhkxFGNKhVGMm1fQjUhjRCJYQj+pY6XL2knSiRuN0KIAQExej1U9rkg/0rEO1fBwBnDaOsUKAztY2ugcEQxJVHtVytdEtms/HExOpan1IfbCKu47Na9XNkdtTystKI8z5q8EewJnUaYye4odjdQG8rdbBSWdVzwj3OpvQR+aarCH7LukT5Z8O88xnaeAPzDK51lea1FJTCT1CiXbx144zFaftQnCGylMns7mg== 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=oyEis+QxgkD0rrC2Cj501D7OqFc3o6stBa7O9h/r2NU=; b=SFtV3u2ApcJL2dg4632BXmoleleF4gY3X0ym7qWWLbiS9MB5NLuai8v4ONISkaL2tVcLMVDXrGkVzt1CbYIVi8PIMrtLTb59ISPqecSdqjxzxwIPI99UlmqHaEZwLpgfHAMxASz3Jd53rq0ds1QuwiyEqKcpUzkwDd+uzlKHIyzSt1ukldOoDvVUZPXjF9pW/SKewd7O6IjeCuhizaKyNwiQEHj6ga3C0t9I9KnNzZDIZVtRoPvFZE9rjZdfH3yPTZzJT69Z1uxzYG3BFmrqzWsUSdZ9WFlTOLQSpL897J0NdUumP3ua2kbMeX7CZgSJ99vx6E0QwYpSzl1pDWppYA== 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=oyEis+QxgkD0rrC2Cj501D7OqFc3o6stBa7O9h/r2NU=; b=mhJK6duoTRQBrkXjkLPhq1B3yJT4Mm0YS1deKvJ3ClgJbRwadxhbzc1oGHpoGin1M577QTnXGQ/BrdmlzHgHoo84jUjy5eIrRA3Y7bll5+MQulqHk+MBQy6LuUPtbWpTiWwo6PbivxVpKSg98nr4cY1f/x50Xq1g45zwmy+lZnE= Received: from CY5PR15CA0030.namprd15.prod.outlook.com (2603:10b6:930:14::28) by SA1PR12MB6776.namprd12.prod.outlook.com (2603:10b6:806:25b::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19; Mon, 20 Feb 2023 18:49:30 +0000 Received: from CY4PEPF0000C96B.namprd02.prod.outlook.com (2603:10b6:930:14:cafe::e0) by CY5PR15CA0030.outlook.office365.com (2603:10b6:930:14::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:49:29 +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 CY4PEPF0000C96B.mail.protection.outlook.com (10.167.241.75) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:49: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.2375.34; Mon, 20 Feb 2023 12:49:28 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 25/56] crypto: ccp: Handle the legacy SEV command when SNP is enabled Date: Mon, 20 Feb 2023 12:38:16 -0600 Message-ID: <20230220183847.59159-26-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000C96B:EE_|SA1PR12MB6776:EE_ X-MS-Office365-Filtering-Correlation-Id: d6700ea7-600d-4a8e-0275-08db137332d8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: qFLieTtn3sAVoNA5ru6EEJqN9GTWFaazxkf7QMR+9lyLhNG/hDoWk7TkQDpNpQP0WiIbEBS/SHVOLNIMkfhpra7RAGIuKmeUarbqLj4mQkrB7SKqPUWfsuuv9NCGHzknBi7VGCyy5RPdtXHjOO0ybARNZsXPE4zXtSF8vbyhAJ5CNPOFIpST2T+Knn85zGeSrWbAxurqXq3uDh5pIWIMsVkOZCMp7eMdDxkXThCQCGD1v0MqKiw3qHhbk+n1bqVPfkPRUitoOhVLhR2gRKpuK6hPA9gzmZpt56So6voXYyO2hNk7doWZHOPXB2xtfAt3tooOkt2j06grS9fyMk4PBvaLdKY2A5uJtrnEboltvuKKoBlCQbp/jO0Uk/508aDYEfs9YpkUl304mYNALdZ9QUAG8wDAPP9/qwyvYrHO11TT6dFrI/JEXDLlbfOEJtRnQWTpzi9MkkU3byCBQAH41zopf3c/pMJruwChqXn030nF4ARerpB3VsRke06fMtYqzzJNrcfITH9BTwom4KSM29Xou+DgdArmuBXM7qi8ATu10gZsTgOETfzSwRl2Q0xMMlkm7cyZnMPmiv4MCb9hZ//gP6EWwpH06QqqUN3aclyvrLTKXQUy7dkOwYSyYOPg1Zqd08MIufkjXaqdqQ9SkSrikyTM2Zkjqxm9yYr6dw4goCziNsnPol446H/c4rYSm8U3kgYamo+oDwyNAmUBkTA1YeusEPUygvhepp0C+n0= 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:(13230025)(4636009)(346002)(39860400002)(376002)(396003)(136003)(451199018)(36840700001)(40470700004)(46966006)(82740400003)(44832011)(81166007)(36860700001)(5660300002)(7406005)(30864003)(86362001)(82310400005)(2906002)(7416002)(356005)(36756003)(478600001)(336012)(47076005)(186003)(16526019)(26005)(40480700001)(426003)(70586007)(316002)(54906003)(83380400001)(40460700003)(70206006)(8676002)(1076003)(2616005)(6916009)(6666004)(41300700001)(4326008)(8936002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:49:29.7146 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d6700ea7-600d-4a8e-0275-08db137332d8 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: CY4PEPF0000C96B.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB6776 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 2C6B2100007 X-Stat-Signature: z46wfaaweutomyoufh4wyqeic5f3mk74 X-HE-Tag: 1676918974-925458 X-HE-Meta: U2FsdGVkX1+7WW+q2zsAxL1wfMdTkDBJEqnnFrxgkA7hhWMCVihPZoejZ7LzTYAPaw8hoq75Jx911WEufhzXvnXFfm4DPp9vFzN0LN1uJ4RgvuvqYAeCvtZGF7EK23W4FWL6N1SgaWjR2bL3VsLldBYqYOnVyefhFAHJc3AadDzzEgdCqKLgTaN4n3z3HjSKjJKO+G/x32VJBZw74NOMhsOJXMxN718Dqz5XJYvQfLEh8YySu1Xm/+ZfjEZROsgJA8neteHvJz0sNfgdBVjOqGIOSrXQ/jnZxQXBJiDWam/N1xzNY4a7bljKgpsa5bkfFpsNBxJrcvoGAlYd6F1AICQa40tuOxlTpx0mLrfN0yEHDiLRW1wqxQU4YfMd3IA+IvA0YikoZqFZL1hjg4tk8kVnYYm6DcV0yoAUSkctXC7tUF19cs6RpqCOiSLNzfKLmbtl/yEpprcMAKniMSPo9kTWyA8I4HESqbdMrUVeblzDVy/4iP4v/qk6g9NmQvYJfNA0uQqGM4nQmOKWgbvyjY267KoYGkPf8SiBDnAsWA92HTDIYOIvYWnW5hPAlwM+qaQYqnxuM8lVyrGm+ECXJknuoX5+LXZkkLHrFRrELRdzdzXh8CMx6JAHcrV5UXMivudNJrfwIHtf+I4Tjma9L8cKViaIEbjSb6Yb+Zc16wm+HYnVZwMc/zTWdrPIuD3thprkjW8+Mq59AY39zU9T5SuptfhX84/lxZPkKTIZYL8rxBfAHUP1AP0KYyLqEa8ogMw/4G+EfVwIm6mg8PfwxYSoqFsDddPQta940f4dpqLVRGKS4h6x39Ii0sIzYNUkGz49958uTw3fn+8n4/bASMNoh5CWsD6gouUyujPLaih+FSxOWjd/wDL9V+jLU+hj118b5RvU6vReLHQyCDbudCyUYgjJLMfnnbtbJLs6SsGRwp+kK+GDUt+1NWCir/UvPTyYUafqlk6G+IbE9hu ToaYieN6 lMsQSa3SpMOuVuE8fJxcJiYoWjlMDadsX/ax/mWMNJjWrsRU7ROgM1b1Q+EnTfp1J4I0T1RyiM2MX4UttzyUSRxYePYdvaasrWdTyHX4m4aOaTQa/aKc/fvWmVQNNVRxKpfsEnLn4zYCv8DBrDgqSIaNJDT3CUlGdE5szolTCVU6zk+2c3gqwvFRhPLliH9Zs0sDSZhS4cc4bI2L+JwY3A5+hM6v7IS195aoadAYliVTwf5Ff3MSPYcV1MSkvU4n9ns2zVbkxP382InD4HuyEtxnyI+L/0xuZcNCFk1OGXEBvfXfLRYPg2NW5vxIivNMPrftE6Sbh3S1QjwU= 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 4c12e98a1219..fd8893af6ed7 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" @@ -286,6 +287,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_mark_pages_offline(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; @@ -487,12 +512,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) @@ -512,12 +820,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); @@ -560,15 +884,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; } @@ -1579,10 +1912,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; @@ -1648,6 +1983,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); } @@ -1722,6 +2063,14 @@ void sev_pci_init(void) dev_err(sev->dev, "SEV-SNP: failed to INIT error %#x\n", error); } } + + /* + * Allocate the intermediate buffers used for the legacy command handling. + */ + if (alloc_snp_host_map(sev)) { + dev_notice(sev->dev, "Failed to alloc host map (disabling legacy SEV)\n"); + goto skip_legacy; + } } /* Obtain the TMR memory area for SEV-ES use */ @@ -1739,12 +2088,14 @@ void sev_pci_init(void) dev_err(sev->dev, "SEV: failed to INIT error %#x, rc %d\n", error, rc); +skip_legacy: 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: + 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 Feb 20 18:38: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: 13146900 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 2C532C6379F for ; Mon, 20 Feb 2023 18:49:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C36DD6B0072; Mon, 20 Feb 2023 13:49:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BE71F6B0073; Mon, 20 Feb 2023 13:49:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A12FD6B0074; Mon, 20 Feb 2023 13:49:58 -0500 (EST) 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 8CA806B0072 for ; Mon, 20 Feb 2023 13:49:58 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 5ECE51602DA for ; Mon, 20 Feb 2023 18:49:58 +0000 (UTC) X-FDA: 80488559676.18.E2A8718 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2040.outbound.protection.outlook.com [40.107.92.40]) by imf08.hostedemail.com (Postfix) with ESMTP id 6F657160003 for ; Mon, 20 Feb 2023 18:49:55 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=myXUbkaG; spf=pass (imf08.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.40 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=1676918995; 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=x6nUTrm8qRU265S2b9i11RIXRilPZluYGpyCcCYUbZI=; b=xK1hWxIa2qguj74aVfr3OH2QHRcPkAVj2Vr0D4EsN0g6ubx/HeOaGoUIjWVOlfLL8sP06U WxCecd/KmUilImBFeek9q/eQRcMbgbZPfIfXYRyBGDdKvvtlPEGmtbCgVGSdbyYJJCC+6/ RQvpCiC6e5YpORd/8FtQqTKNMWG85oU= ARC-Authentication-Results: i=2; imf08.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=myXUbkaG; spf=pass (imf08.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.40 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=1676918995; a=rsa-sha256; cv=pass; b=7BJGAEytd3cEgCELD+9755NEs9R3fwmhQim+PdmK00Ed6wAsRML1/bQ1znhqLo7pxKLLxE Qizl6Zsu6r1TqzDHWn2Df0iibY3ZDlNxvH4riiT78juF4uEJZfXS55zpOAy3JmhpenGBHY 3DlU9vD20Rnv+wEU1np8tbbdN1OaJUk= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=H7rUmFhUGWCoS0j2blag4pWFWPnTKKALKdu31EBSwXwhfL43Gwq7EbvG7z+qY5YbtUC5aqe/fcCQ3id3UjLwW4f7gq2KZkf2TlwYNibGZw3t+t5edpXJ3EJFM8lOIeIub+AQVQiENnh6i/PiDAgBfc1GKflSijpt6tWnCe1ui2dxTPfAGeMIImCiwUaBtjNlkwi39vZceD7YOcX3IXY/WRZUuFB5WOqTvXlkfUlpLb0nNhWNgDs/Ex71Jkww/z2zYZd93Tis4fZq+d5VTDlXHLVyDYYLqqOKYX5ZDmG9/6wCvRrRjbVswonfspq98N/h079C+gJGX2zxbSwIgM1i4Q== 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=x6nUTrm8qRU265S2b9i11RIXRilPZluYGpyCcCYUbZI=; b=DT55adSncfuYZAvNtwtsCK21oPfuqDUYbCh3hF3/OOibDkvA9YQdG5+Yv69tXNPj1JDKf/j8oRDucqcgTCugeHkaELAH/lxXtO5wJPQwsRLAVoFLf1JdhfKMZ/bvlpLuKJseT0kJLRamkIoOTqplT6uU7eu/wdpwFG0KKTRJP0KJGLdcufhelgweite5k46EtwB15omzTHQPGyuY0hTynyzZGNN6DTmlnkmq48ikSmamba7SIN1ixkyAOzEF4qTrJDPF4Wq5TqSjI691uC74yQTDBsA8RH1Po0C7b1/6C9ydJvUSjosZvxIE7LBxcv03hvpeAZcum34N4CKvSapjbQ== 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=x6nUTrm8qRU265S2b9i11RIXRilPZluYGpyCcCYUbZI=; b=myXUbkaGLcZZEp+GqoboL3oEnzYReDwkCGQfcwHZd0VSjOYIDIRGwUxyEJ4n49+qwTiwVl0ZrqgY1C+gMfjjlrPi7WG+SIVDSNLh0XgUMW+rMSEBtVnf/E+4eEo/mv9hNrDSpo/CQLjW9OD20cC+ehWQaU5ldsA1h5XgOp+0n3Q= Received: from CY5PR16CA0020.namprd16.prod.outlook.com (2603:10b6:930:10::16) by CY8PR12MB8339.namprd12.prod.outlook.com (2603:10b6:930:7e::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20; Mon, 20 Feb 2023 18:49:50 +0000 Received: from CY4PEPF0000C968.namprd02.prod.outlook.com (2603:10b6:930:10:cafe::bc) by CY5PR16CA0020.outlook.office365.com (2603:10b6:930:10::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:49:50 +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 CY4PEPF0000C968.mail.protection.outlook.com (10.167.241.72) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:49:50 +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.2375.34; Mon, 20 Feb 2023 12:49:49 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 26/56] crypto: ccp: Add the SNP_PLATFORM_STATUS command Date: Mon, 20 Feb 2023 12:38:17 -0600 Message-ID: <20230220183847.59159-27-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000C968:EE_|CY8PR12MB8339:EE_ X-MS-Office365-Filtering-Correlation-Id: 572a6838-83f4-445f-5a8f-08db13733f35 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: m4Pwn1EA8k8uQpJy4IQguQMuRi3vhQrKhjXkxXiY/voFmKskqwK5J2EA/gpYQEdd5CGnnh8ZRx9+iZ5IB8n8Sdapo4Z6RogYno4GSchyHn5SLhPPWqloAHA3ZAHUbY3Pe2dN4fblHhF9Nsm+zQla3FK+D5D4YtWYwcCmIuVOfrjQk4AhdEscXRKyMnQ1MrQ/fe9rFt2EAoPrLfbL4nDp5GBjvnBCKLgNalRLdvr84shbJ+lhncK54lCnDM9kZj6nuLiAOqkOsvpW+X1zXSqam1H+Glg2/updeSfPfHTfnaATQ69s2S0BwVN7hHcJNKK+1+GsPjhjZfISm2IeVmZkNNQ8BzIiwdaC4leprQH1dQLGiyBO7VJdJvTitWgA0UjEaob5a9Faxe+/ofU2gZrK8ti5JPVDTExerrqXZabCdqt7owLSjFhkDqvDf0ow7sZlIG+1L9H/FKXgb8sq0tffjjShmOTuykFJJNXCe2yYXBvohL6lULbHWTXnfLSxz7U0sEZTVCqgbfRCK5MuztqX825x9qDkKMSiIST69CLJeNvWW4dK0qFm/5kwWf2+20VMj9pSddFdFchQpgAl1uZamQPff/Tjyn0xO5HK3yAJYvxD+TY3SrNOnnHG3GQhZV4fLtyAqV1PrE96SEU8//yl+i0btj4nNY14QBryS1BadnIbp4NGdqW0cknd4uozOfGj+JvQKnnd++vK7dVqKeAxCJn1usAUcY57Ybi+8EFFiYA= 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:(13230025)(4636009)(39860400002)(346002)(136003)(376002)(396003)(451199018)(46966006)(36840700001)(40470700004)(2906002)(26005)(186003)(41300700001)(356005)(16526019)(82740400003)(1076003)(6666004)(44832011)(81166007)(2616005)(7406005)(5660300002)(7416002)(8936002)(36860700001)(40460700003)(316002)(86362001)(70586007)(70206006)(8676002)(4326008)(6916009)(336012)(82310400005)(478600001)(426003)(40480700001)(47076005)(83380400001)(54906003)(36756003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:49:50.4591 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 572a6838-83f4-445f-5a8f-08db13733f35 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: CY4PEPF0000C968.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB8339 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 6F657160003 X-Stat-Signature: po545n4bygqnamoxrtp4s7o3tda5it7a X-HE-Tag: 1676918995-952485 X-HE-Meta: U2FsdGVkX1+0Rx6NmF9KFBCniQXaVxHvM0SEJl5OMivLVfQyKh+mA+9cwF0AirnTnvlFZprm6QgHTuwN/F3Q2EQa15Tc0yir9djfX6C87tI2YFKxp1roKbdyYmVLkmWN/p3f/GUAMxIJHiCEmwkbGM0ymgzKo8Gr31FtTrmlpKXzwvqPx4AveZM6kmtgdibv2Wx5bSl4OlwqHR/TuWQU4wmnS3Fo+vxOejm5PRjlx5rgxqGPRBsVKef2nrp2mxJs5bzvMKDl58uVhUaRi8VXrH6NTjSzJv3e4ysOYGfTGje8VNBr1J4ZdmIH05OYQQu1P9pXQzvBwbCQknuzAjH97KZBs2Z5Y8X23mM3+M45ng5bLPJLgdvrD3P39MS3bvUz0sttOS+04BKVgd/KSFzd9ef3+fWA5SEFkKp+AKYBlTe2SK0xr9kS4UTWu8umqZfe3BJcSflSsLgtuQ+JWUD+K2JqIe2nAEs8DRRK3IjOBopXlYSYQv2MGzGKJMBBG06/XbgXviGzsjouGlTwAoki03KveWbRLphqIHeagG9s5TFedFo5jjTGDsDHTHzZ9+j9RR985+keKKz38W1kxJTS5reF6hZ4qULtiaqi2L4MOCPfS4Z4PUDnZ0w91/tvGzm6zME00Y19RzIfvuGrvFY2GlJn4py/a2fpcJWw1WcyYAHE1JFCIvefEec4lF0EvQ30RNLOOKl7u3QK/BzNL8en1yQ7w+BNGHeXGXfAHXpkKbX8YB7Tk52zhozjhroWMXzxAlWPxzSMF1vhiFxcRLXmaXgGirGK+MXDgkCsGJ+iEAmUIt5AThLVSYxY/hCOJ35NsMPrsgrQ4jUmmjHPYzXh3vqyxIcqfIFdLe9VeDNM4Z2PGOCTxlmlJwYS+mkOW3nB6ZUGvXxhuX/Sa2plHdaYjtI35XxKKjMbfmofhA4OWxvf/Fbj/BValn5wo7Kay1unQhHbxIkRjUgk9L9p616 OMYNPHYU xAd7Ii95H3C36suIyRGQx3d95+J5ku09XDez/1sflknO2/vAISdESJPPkLh+xZQKPSQYDa4VUcBY0EhLziJJzQ1Gg2A7zJC75qJkbIvkToiGorhHQ5cZwBZ1Ld8XIigca+tWp+g79Y/pHhj522jF0o5eZtdtbKZbpE6ggryzhA7Me+dOPQghCuQMQf8aLkXaGpbNnG6MXCpKmGEiVlsRvwba1fCaDtwmfXlpGEF/fpwLXzcOQhyc6gUgnEgsynGpQS6OF2hs+A0HHhLGTgpTsMUllwe2k29erVcp/7fFqpiAQar3hE1x7PLKw3t/En/n7GQSobFVp08e18Zy3j/dhha0+2XSdPGJw/214ypKwwg7RzsJfjLKF5pYRqw== 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 fd8893af6ed7..65e13a562f3b 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -1751,6 +1751,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_mark_pages_offline(__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; @@ -1802,6 +1844,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 c66f7c372645..5adfaea7df97 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 Feb 20 18:38: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: 13146901 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 C8186C27C7C for ; Mon, 20 Feb 2023 18:50:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 691EB6B0073; Mon, 20 Feb 2023 13:50:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 641AE6B0074; Mon, 20 Feb 2023 13:50:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 493F76B0075; Mon, 20 Feb 2023 13:50:18 -0500 (EST) 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 30E306B0073 for ; Mon, 20 Feb 2023 13:50:18 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id EEA0812011E for ; Mon, 20 Feb 2023 18:50:17 +0000 (UTC) X-FDA: 80488560474.15.77A8568 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2053.outbound.protection.outlook.com [40.107.96.53]) by imf28.hostedemail.com (Postfix) with ESMTP id CCBDDC000E for ; Mon, 20 Feb 2023 18:50:14 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Ui0Z0zav; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf28.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.96.53 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=1676919015; 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=bu8MBPOdpq1dXgtI8ha3W3qrfw7vuEbfpB8WIhNoZrc=; b=iDDIBACv3XP8eFGh1fRWzX0duo9ycSN59mz9O5VTdBDImTnBLiLmXnGZvCk78L2U5oOY+h MSRugjljlZS8ai95sfM07p/l3Beu++bLVEARpPk1l784LwJgbtwHr33oAw0VK3K0/DmfYK TDLbtgAJ7A0lbZwyRS0cjQQFD5w0Et8= ARC-Authentication-Results: i=2; imf28.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Ui0Z0zav; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf28.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.96.53 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=1676919015; a=rsa-sha256; cv=pass; b=ragUpArHJGlk4h0+EWO2fngbLPLnT7tRIILmq+RioP1VopUHAm7dB8MR6gtI26fiZt3hPx +hFPJJtEow2pWANo1Qv2NnbYbhLd9SLXYaxJGpl4rdasel+k+r7BsQyLhF4HWUCVxGChfj nROV2XuwMlSoPIVlxRqHcKXxOFL3DvA= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VK2u2gU7jFnYi5+iOA26l2NtAnkVVXv4JC1jqsHo7JxW0MYzKeTiL/icOZFRz4wi7Kflu8CQ0QgtlcL/SMRB9YN8sE9rRvEVITCShTE8YOZAZqG/eDEjFf1z51+C1xC+PoGWGTlHc1tjCMl5JFDJaaQQxq8dWxXO4sfb6YEX9hjead2G/GLNuEeMLJBE1vD6P5oaYOh20feGjROmlTLwLg2DYwZeR2LzQ6e5BZwAXiB8PBb4kzjdqTBygLMdMgbbeyI0s5Gpk50uf/y5kkCihWTMyeYN18W+mlbLascbPoti3TfTza/n5X+PFaXTx0gXpJI+3p+8zXNtD95fFdoblg== 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=bu8MBPOdpq1dXgtI8ha3W3qrfw7vuEbfpB8WIhNoZrc=; b=mfSTPRX7ypF2MYo+5GnL8zViRwpF0WdN9WNGvnqQm4+B+dlP9ix3Apyf5D+GldBlJgl3M72U3pHCLudo5WITXDi5PS47UdnZhk8zyfJFSV3IZcgiJoIcdF3W5tuXR050PbCNG0qsZpXNFkvRs+50EmmE4kLzYcUwgnsip3on7GaexqppHX1TJzAZi2OVF1hje0eS/CuZOMHNQlQF2Or2Qc2PvKuXKHb/eYwOJoibJJN/DuwglPWTWN/pWM4xlm7aD5Ha6HBn2VXspz12rxrepea3u9ZFDWlA3b4UUAYnleXDEncmcHapyf3Wda5EzqWRhuGKkeJDxdmXQbZS9UqqDQ== 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=bu8MBPOdpq1dXgtI8ha3W3qrfw7vuEbfpB8WIhNoZrc=; b=Ui0Z0zav+fkwyymdrqnt+bFZ4URC+FLH5kk85GEXB8YUmo9oc/n8y0vqCtcB3Nj3doVhVSDYLv/UiXbXUjXuDxvz9BK2YwWxSRkSFHbx1PlyUgY4eNpjBHxn7w0c/WGHwrl85rIOdrw2DLXPiaa415TC/BS6nwFthO3QsfB79D0= Received: from CY5PR10CA0004.namprd10.prod.outlook.com (2603:10b6:930:1c::19) by CH3PR12MB7617.namprd12.prod.outlook.com (2603:10b6:610:140::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.15; Mon, 20 Feb 2023 18:50:12 +0000 Received: from CY4PEPF0000C966.namprd02.prod.outlook.com (2603:10b6:930:1c:cafe::5d) by CY5PR10CA0004.outlook.office365.com (2603:10b6:930:1c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:50:11 +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 CY4PEPF0000C966.mail.protection.outlook.com (10.167.241.70) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:50:11 +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.2375.34; Mon, 20 Feb 2023 12:50:10 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 27/56] crypto: ccp: Add the SNP_{SET,GET}_EXT_CONFIG command Date: Mon, 20 Feb 2023 12:38:18 -0600 Message-ID: <20230220183847.59159-28-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000C966:EE_|CH3PR12MB7617:EE_ X-MS-Office365-Filtering-Correlation-Id: f64c5fe2-11b0-4e2e-8ffd-08db13734bd3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: kRNlhby/dnP/ajLaG2sOvEZHNl5BhnzK85xYePjSU/Lzxob1uvS7JOxCAA1r4UFAxw4mCe5NUCwV9Ac+Au6dsWNl+Muf+KvOs7ghV/b7GIrrZrIU/e+lhcQ7XGmDhns3xz0K0dYc4HiA7i6FOeolWIww/Jod/F3FK6Rp2ddP9ZC7FVoMBjXa+ceUNoZD0RvR0l6oYgoAYHhgF0lzj1Ks0UMGrD1H1u84nVpRPI6862B9wDd89ce0vmADwxzDshsBgXqbDNQMxvQRFZGeI7MVf1rIvJYKyjgfcv/Pws/N8W9WJEMxNttkMn2qwR0WTT1ne5bTFKl+AwAprgO5fTUZYa4tX/3QX5KV2w13ksTneD31iTL4T3ITjj96exhacVZPMuXVnzGrs9GyZzpEWs5tIfy/V6BEB/jvo9wdDQ+rCw5NbcGQUUkz9ShbjyVk2PDihfDM6pF1D1bzFHglbRE8xwsqTjcFiA8H9Yv+MH+raXzLHd6j7lW1hRua1ZcE+D5k2Jj7s0CMKpiD/IodI+pU+aHIQv2zgtO1ePbo9xPGyIDCGO+sO9o09fFekW6NgLhu+ucD166/sMRFUXAdGeiVvHK3JOaLm4z/dgbIAyjV3tQJWS52hqsYlDQlL7iGhSa99xL+DjE6OjqddgO1AddtXG1zNRVWrtcJdW0U9EFEE01118UcwweuxoVHSyrTpWwXoq/4RvJYNQ9VUaIoD+xY8MGlOyzHMGaGuZCQ4o7i3Ok= 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:(13230025)(4636009)(346002)(396003)(136003)(39860400002)(376002)(451199018)(40470700004)(36840700001)(46966006)(86362001)(81166007)(44832011)(82740400003)(36860700001)(2906002)(336012)(16526019)(478600001)(186003)(26005)(54906003)(82310400005)(6916009)(36756003)(47076005)(356005)(40480700001)(426003)(8676002)(83380400001)(5660300002)(70206006)(70586007)(40460700003)(4326008)(2616005)(41300700001)(316002)(8936002)(1076003)(7406005)(7416002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:50:11.6286 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f64c5fe2-11b0-4e2e-8ffd-08db13734bd3 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: CY4PEPF0000C966.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB7617 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: CCBDDC000E X-Stat-Signature: izjxxt4spdkwh9dnkmcbwyfe7cb4t9bu X-HE-Tag: 1676919014-634924 X-HE-Meta: U2FsdGVkX19WPIHtfYJQftkgogzNw/w7mYGU7KELIh5QE3kALHGc+FovLq4uc3TWdG7gdm+lT2U25hij8PkWDh21tAqVqNPu58Grf+QhXGKfNdyLC3ZSu4xxF8TEJHtq6An4MXSCPcVIi1Jm2q+5T2s0w9XKLEXwQ4SiV/bdtOaQXKwZyacczk4+eue0gx0JiYnqpzgWS1vQk6caYT9i825sR7OKcS5fmrfOOpJZT82PuinLomrwqqkIZAcXbDGOXwE1J3rpUCtvT3BXj+BqDMehJJHMLakV1Gm7WKMubgkjF0fOd86HgbdTTn9KKGIJ8xl/P33SDhOD+FmV1+HlTU/iTZyF3UhYHr9RfjwMDVNFO7gMQhTqfhtoVlHWwylIaFK5Pp+klnlZs75XV0uaRpU8XNIruZvq9qr6dWzFhD9Qc/OVAuHZWkSkm6ZzJisX4WUECJ/X3/zf1MshTqC5aJ+xBteizc1UJzF4O/Rc2g6bcZ9+etr52/nMxmXDDUKdeHEM6dQtDPTvdWwV8fftMkGR/egYLFsqRsRnFbwvMfOjxN/xZzD9gIgkA6PT4p2u0dUAmK0NiwQXlf+0jlgJspOjjeI2LVBnNu4WwJUe8PFIPIa07jZ58wYmH2MBwdQVc3i54a9c3CRHRxf5WF44//MR5c2xb1Bm0q7s2Rkf7+hNoyxh1KNyok4R2D4TiCFFwHZv20DnLRGa1UamRtgkjC4e+jl5AOy0MzPeV1/dNq2PHAmQHJ4tI17dv0ZWN42oS/Tg0cJOvHjEFs01FlmmTle2n0YcK3sikJvfeCotzezxKDDtkL8AWUEA2CZU98OcdMb7CMSlENGaoetwb8TSWJRO23GQ5rT8bpK07C2wYnrreq2fU1S4UKzuBZhgCFFwhWbgg05XJXeQe2sSVhjky/aP+wUOP85sEsdKOXAYiUPs+KRX5GX3cwNghykACY9cz14Ul0elxXN9AYjBWdI oHBDvptZ H6PDrRobgwWzFb5/GS4e1O2qcmIYjgcZtXvzid8tX8wGk9B3AP/qaeX+l/PmjRV6bOB1Lu2yI0dTxlalES7Zoo7lT9bQGyDCLijPVCytF80E/ZP7r7JhXzO4/ZIhzrYHum0jtHclQjzLeoHWqVgbmxx9V4axiNZ9DDSdYFobXd4yvb2l8MsbtCkI9NXrHgfBXEGi/Oz6o3wjTJz2s9JiYSXwjT12FHNL7PWBaWi+2HGH8+DoEQCM2+jXC9w+inVtJ8E6Uh913j88zCTtXaR3K0KEF+ZHnRjQLiF0Bz0VAMP3cKTvsAE8+g0Qscg6mBry90uq0/rPR5sJarelcdmTfcjF3lmKNVrPpNioeNUtq0aphPxwqMCIK5fpE3g== 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. 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 | 123 ++++++++++++++++++++++++++ drivers/crypto/ccp/sev-dev.h | 4 + include/uapi/linux/psp-sev.h | 17 ++++ 4 files changed, 171 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 65e13a562f3b..b56b00ca2cd4 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -1481,6 +1481,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 */ + kfree(sev->snp_certs_data); + sev->snp_certs_data = NULL; + wbinvd_on_all_cpus(); retry: @@ -1793,6 +1797,118 @@ 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; + 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; + } + + /* Copy the extended certs programmed through the SNP_SET_CONFIG */ + if (input.certs_address && sev->snp_certs_data) { + if (input.certs_len < sev->snp_certs_len) { + /* Return the certs length to userspace */ + input.certs_len = sev->snp_certs_len; + + ret = -ENOSR; + goto e_done; + } + + if (copy_to_user((void * __user)input.certs_address, + sev->snp_certs_data, sev->snp_certs_len)) + return -EFAULT; + } + + ret = 0; + +e_done: + if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) + ret = -EFAULT; + + 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; + 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. + */ + mutex_lock(&sev->snp_certs_lock); + if (certs) { + kfree(sev->snp_certs_data); + sev->snp_certs_data = certs; + sev->snp_certs_len = input.certs_len; + } else { + kfree(sev->snp_certs_data); + sev->snp_certs_data = NULL; + sev->snp_certs_len = 0; + } + mutex_unlock(&sev->snp_certs_lock); + + 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; @@ -1847,6 +1963,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; @@ -1962,6 +2084,7 @@ int sev_dev_init(struct psp_device *psp) goto e_sev; sev->cmd_buf_backup = (uint8_t *)sev->cmd_buf + PAGE_SIZE; + mutex_init(&sev->snp_certs_lock); psp->sev_data = sev; diff --git a/drivers/crypto/ccp/sev-dev.h b/drivers/crypto/ccp/sev-dev.h index 19d79f9d4212..41d5353d5bab 100644 --- a/drivers/crypto/ccp/sev-dev.h +++ b/drivers/crypto/ccp/sev-dev.h @@ -66,6 +66,10 @@ struct sev_device { bool snp_initialized; struct snp_host_map snp_host_map[MAX_SNP_HOST_MAP_BUFS]; + void *snp_certs_data; + u32 snp_certs_len; + struct mutex snp_certs_lock; + struct sev_user_data_snp_config snp_config; }; int sev_dev_init(struct psp_device *psp); diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h index 5adfaea7df97..c20d37586d21 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, }; @@ -192,6 +194,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 Feb 20 18:38: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: 13146902 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 4B4B6C27C7C for ; Mon, 20 Feb 2023 18:50:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DBE9C6B0071; Mon, 20 Feb 2023 13:50:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D464F6B0074; Mon, 20 Feb 2023 13:50:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B991D6B0075; Mon, 20 Feb 2023 13:50:38 -0500 (EST) 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 A341E6B0071 for ; Mon, 20 Feb 2023 13:50:38 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 7D0E8C0570 for ; Mon, 20 Feb 2023 18:50:38 +0000 (UTC) X-FDA: 80488561356.02.281FD71 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2045.outbound.protection.outlook.com [40.107.223.45]) by imf18.hostedemail.com (Postfix) with ESMTP id 836D81C0009 for ; Mon, 20 Feb 2023 18:50:35 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=nToGGUcr; spf=pass (imf18.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.45 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=1676919035; 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=VjpNkINyZyAw+Ocb5Ohq8zU+vpUdm3APUpg0cCkBwOo=; b=gESQrH7UyBepiOpthi7sZdvy1y1Fnisx0R3K2zu5+MWlHnj1F/oyicYKvoSUk6AgOxotMi WoqACsQzgu5i9af14BW8IxQmTevSrSGFElrTp2OALGC/d38y3E5CBN5eQPrLJcBGiphwhG m1veNYxQvhgdXK7CKrtn0lEbQcyiLSw= ARC-Authentication-Results: i=2; imf18.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=nToGGUcr; spf=pass (imf18.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.45 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=1676919035; a=rsa-sha256; cv=pass; b=w/OOt5DhK0hLDoUN+Jlliyc8gFHUf9CrmHLrgyJSSGA45F2fLiPCW8JiNWDuL1FSNDNLKI 6LSWv27249PA16BMreiTYQTCUZJTs65tIiqZxiMfwKhT9qv02AJMiPLksQW7yU8mSxwncN XtVYOoWTKd5ubSvv6A75Ct79LRqBXcU= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IzfNFo0skAl9ryEW7VmRogVluaIfIX3HMt2YCg1/A+NiP11xkya6o4d0rT11Wf3l8inBnhoooMl1Kb8cxbvX6r0Iyn3VhkOnER6jGxXaGwkYp4Nf+JGvpjaB5faTuhPkHdbH687uTgOh3IwllKCcCnFMpO9PXLGWatnR6cUdm5Oyb4NStyxzfGSCXADCtJdBKfjC257tXGGVyoaqkx8Km9LCNKEHWAKM1Bm21pG2M9RpznggoWJrcL8X2CcOB1760lPuvtGk8GHzLft2kaeoz1AuNEXcu1Xdk5CguB2dOWpZsK/jCYjBm2yLEE0BKw4wAipirxesLJCm8KwO5BzkCA== 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=VjpNkINyZyAw+Ocb5Ohq8zU+vpUdm3APUpg0cCkBwOo=; b=GT6TLuRyYXmDWuzKsB/W/LgVm5U6c37sx6xZD4H9ZqhnoJmpAKjexICdf+G2axA7HX43auEa0TJSgrIlAZnR97p9t7/WibYobGyWFehRLISe+uwg5IpfZg5N5osiT1etnLL0dQm3zRiV/MGv4m4fvPmY/u0DZMMoyedZA1KtMxifbaBmvv5GCJJA3G5RVq9l4Nk2gIf1LruNfzX39qm5KoL5fUDEl2zBg9QJkqWBaGOZz5WLYez9D082vd37d9yThC1T3L1Y1fF2gDtXgP7TtSs1O9M75bQo0lNhJ0CcQfSFebjbTFRuYCWRYxsk3hY+mlaT13MsCrruhW7buRO7Ug== 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=VjpNkINyZyAw+Ocb5Ohq8zU+vpUdm3APUpg0cCkBwOo=; b=nToGGUcrYxj4uc5JaTMuXWlwsnhb2vFuB1n7M7H8YrVdt2hguh4HkO0SQyK936Evd204wXOnUpgWBIG9q8ZESlFHntc6JLh8gI6Gn/MPxA7bKM97eGwXKNejN0cUYdNqXnNkNo6ply7zZFgN0rb1ANhNbMcoa+VqtVmfJ9n6oN4= Received: from DS7PR03CA0140.namprd03.prod.outlook.com (2603:10b6:5:3b4::25) by SJ1PR12MB6362.namprd12.prod.outlook.com (2603:10b6:a03:454::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19; Mon, 20 Feb 2023 18:50:32 +0000 Received: from DM6NAM11FT037.eop-nam11.prod.protection.outlook.com (2603:10b6:5:3b4:cafe::a6) by DS7PR03CA0140.outlook.office365.com (2603:10b6:5:3b4::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19 via Frontend Transport; Mon, 20 Feb 2023 18:50: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 DM6NAM11FT037.mail.protection.outlook.com (10.13.172.122) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.19 via Frontend Transport; Mon, 20 Feb 2023 18:50: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.2375.34; Mon, 20 Feb 2023 12:50:31 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 28/56] crypto: ccp: Provide APIs to query extended attestation report Date: Mon, 20 Feb 2023 12:38:19 -0600 Message-ID: <20230220183847.59159-29-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT037:EE_|SJ1PR12MB6362:EE_ X-MS-Office365-Filtering-Correlation-Id: f23e4168-cfca-4023-1009-08db1373583e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: oPlKutr988IhZ/sgWNcgrSwNoZX1IDwAm8zzME/FIAexsxr8JqaTEgFeUrul11rn/Q88NVgwUnnLJ9Sjt+2AxJOY3rM/J1OHVBx8PMlA/wCeA3bAld63buhMKj2FDeyakQenN6eHsX3MqWNZX6EjeQXGFdv1qyasrpVXKh6iVBuiHpnSaL3On3gf/36iuoC470bVSbdStAP5PR7pAJq56+YloOCanEElybqTuggYOL+yva5CbASXTRJ1fYg78chL02c3rPqG2+hsZE1izg8pfHxkmTWOCs2gDx5Te8LMYpbipRPLdvDKSGaFElzjNkguslBq8gL4KdzYNlW/kBKL5YA/Q9ECBDF5loA+xLSL2SApwGKFbeNVEM7oyxeOQoSmRODc4UzEpjK8ismJWoiEVrYVfvGI8QSy5jgWWvCe5N9z6zzi7Z8N5WQIBwE+E20Xm53j1jCgi2eLzEn4wgsSzsr6QPBn3FgXNlifKz9AAQuBEBThaE2Ul7B1v47R/xy45vKvOAjO2mMVULlp7prrB37NW7mEDl7/gs5qJ9AluRW60Ef5GWvdJLg+NQ021StUYn43gZjusP5KGE6Qffq1k47jrdDEHdKrxfNDL5Y++3Qv94e/LJKGXyHNQHguKm6RfokflEbrBPaXVpIRtYZl4xBMc4jWYe2o0tJAA+E2VkYxU1KE9ZLU/+GtvTAeNTSSwB5WlV6vzhX3FCwUpLRuj56Tu4YemQTS+lDgNtDsDLHMYMQxdKkDbHfKCf3ngSDPf8UbDWoKJU9xcJ+aPoYwcA== 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:(13230025)(4636009)(376002)(346002)(136003)(39860400002)(396003)(451199018)(36840700001)(46966006)(40470700004)(6916009)(83380400001)(36756003)(40460700003)(86362001)(36860700001)(8936002)(5660300002)(7416002)(7406005)(81166007)(426003)(356005)(47076005)(82740400003)(82310400005)(40480700001)(2906002)(478600001)(336012)(2616005)(70586007)(26005)(1076003)(4326008)(6666004)(44832011)(8676002)(54906003)(16526019)(316002)(70206006)(186003)(41300700001)(36900700001)(134885004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:50:32.4905 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f23e4168-cfca-4023-1009-08db1373583e 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: DM6NAM11FT037.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ1PR12MB6362 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 836D81C0009 X-Stat-Signature: ntqaejb9j11kj8ja97nhy6cxiukky1cg X-HE-Tag: 1676919035-844928 X-HE-Meta: U2FsdGVkX19q9Eybn5F5S3VuyhsPqCtdpzwtQ5rD0KC29atAcQvyuh4R20lxg+n8RPc7EJNERnEV/3fp/DD9uQUgOi/uiypWk5+ILKO9U7CWi0IkElbgUFdCcnjnW33UZAI15q9eDc8kAWRa9ZTb/sL5qkjB0O2KV2Bm3qgP4Q94aUBHf/FlP2Xp5ZHTdziI1Bxy+ReCFGxV0D2cGwPBSeGayNdNPT4+vhSlHX8xx48eMlQ4RXPZfH+9xvIFW6WFPh2zzmyXmVS+hBfSBmA5Ewwh4azVizSeyyjq8QUn2eTWfFHP98sBLBatpnRZYI3fPA+GsLcF57T4mv6uLlvBvVm+s4loMI+ZLaa3yf7KfO5lufjdcv0KDGZEZq521Y8Lx0PGfX9C61wRSAnH2LakXs4ErJ7+dmvOsNNySpdhWSPePP6AfX9A7XMmZIemnMG++2o9MbTMKEE40MtVEO7uDIwv9YMX0jXisy9Gi5E9xQvfxVhArB8A9P0IVzcV27hYHmAmgJ1FQaTHJ7hD92aOOx16RvJ47NTNloejDegekqpWpM91Iz45FuCcleKruP3JUkFGnQJoJi4+AT81Vwg5W2wZMucNiSnMfROWW7AUyIW2k/iUq2eP5wYE6vZRjLn14mxPX/LDq3fSzDfMfnkbGdNynD4m+hzoF0h7Y+cXlRTDP8xiAetGCgCwDhylUTr2MbJgsWdAoMeaaH+m1eJWssz1bfS1fuCcIstkfcj+c+ySIgrJ2BkSs8Dma8XRdQI0QBjS06IkICdrFLH26SOP+fsKrBsNAZGOM7pPJOIah0fYLBcbCcSK923yzbP0c6VTEagfV3QuN7mUNNx8hnzTHR+/Nmv4/CJow4LsjdO0yZ9KskKevey5vlg6XkBOCvk2gzjqzuSzQ+QKTstiGamaJEzGguzH1c5n7qfgZWelavrbslQaY2Y8XRdJX9q2XYmp1tLRi6aYyalOlgrq4mR oZt9FLOK Ikvh0W/Jkn3fr7HzSvOIpoftDwIY45yveesSSdHRW1V6YfYQqjMn55lVFaIbfRZrDJewEtM+emJilgY99rGm6W4Sc7mrf+eWIOdyTPGEZB3+LuMtd8tWMfFqInYc6dR4B1PXbdYY/ecEJuHYpga9rpT9EYxFMEzKt0+Iyq2kD2ykLD0ueH7zZP2t9pNpcWWamJOkz6DxvUTO4W088Y1sytihIIYTVmTVwr2+ZeIDi/0RNekSBX1qMoOqMKzA+woXdlRx9i+n0Ix9vQ7gxGpaUPiJGqAJGgBg8RZAm9J4K7eQCUqU86eoT2KZlw/46il0FnC9oyKO695PsjTE= 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 defines VMGEXIT that is used to get the extended attestation report. The extended attestation report includes the certificate blobs provided through the SNP_SET_EXT_CONFIG. The snp_guest_ext_guest_request() will be used by the hypervisor to get the extended attestation report. See the GHCB specification for more details. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- drivers/crypto/ccp/sev-dev.c | 47 ++++++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 33 +++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index b56b00ca2cd4..e65563bc8298 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -2017,6 +2017,53 @@ int sev_guest_df_flush(int *error) } EXPORT_SYMBOL_GPL(sev_guest_df_flush); +int snp_guest_ext_guest_request(struct sev_data_snp_guest_request *data, + unsigned long vaddr, unsigned long *npages, unsigned long *fw_err) +{ + unsigned long expected_npages; + struct sev_device *sev; + int rc; + + if (!psp_master || !psp_master->sev_data) + return -ENODEV; + + sev = psp_master->sev_data; + + if (!sev->snp_initialized) + return -EINVAL; + + mutex_lock(&sev->snp_certs_lock); + /* + * Check if there is enough space to copy the certificate chain. Otherwise + * return ERROR code defined in the GHCB specification. + */ + expected_npages = sev->snp_certs_len >> PAGE_SHIFT; + if (*npages < expected_npages) { + *npages = expected_npages; + *fw_err = SNP_GUEST_REQ_INVALID_LEN; + mutex_unlock(&sev->snp_certs_lock); + return -EINVAL; + } + + rc = sev_do_cmd(SEV_CMD_SNP_GUEST_REQUEST, data, (int *)fw_err); + if (rc) { + mutex_unlock(&sev->snp_certs_lock); + return rc; + } + + /* Copy the certificate blob */ + if (sev->snp_certs_data) { + *npages = expected_npages; + memcpy((void *)vaddr, sev->snp_certs_data, *npages << PAGE_SHIFT); + } else { + *npages = 0; + } + + mutex_unlock(&sev->snp_certs_lock); + return rc; +} +EXPORT_SYMBOL_GPL(snp_guest_ext_guest_request); + 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 d19744807471..81bafc049eca 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -931,6 +931,32 @@ void snp_free_firmware_page(void *addr); */ void snp_mark_pages_offline(unsigned long pfn, unsigned int npages); +/** + * snp_guest_ext_guest_request - perform the SNP extended guest request command + * defined in the GHCB specification. + * + * @data: the input guest request structure + * @vaddr: address where the certificate blob need to be copied. + * @npages: number of pages for the certificate blob. + * If the specified page count is less than the certificate blob size, then the + * required page count is returned with error code defined in the GHCB spec. + * If the specified page count is more than the certificate blob size, then + * page count is updated to reflect the amount of valid data copied in the + * vaddr. + * + * @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_ext_guest_request(struct sev_data_snp_guest_request *data, + unsigned long vaddr, unsigned long *npages, + unsigned long *error); + #else /* !CONFIG_CRYPTO_DEV_SP_PSP */ static inline int @@ -968,6 +994,13 @@ static inline void *snp_alloc_firmware_page(gfp_t mask) static inline void snp_free_firmware_page(void *addr) { } +static inline int snp_guest_ext_guest_request(struct sev_data_snp_guest_request *data, + unsigned long vaddr, unsigned long *n, + unsigned long *error) +{ + return -ENODEV; +} + #endif /* CONFIG_CRYPTO_DEV_SP_PSP */ #endif /* __PSP_SEV_H__ */ From patchwork Mon Feb 20 18:38: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: 13146903 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 208CFC64EC7 for ; Mon, 20 Feb 2023 18:51:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A61776B0074; Mon, 20 Feb 2023 13:51:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9EA2F6B0075; Mon, 20 Feb 2023 13:51:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 83E976B0078; Mon, 20 Feb 2023 13:51:00 -0500 (EST) 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 72F116B0074 for ; Mon, 20 Feb 2023 13:51:00 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 3BBBE40726 for ; Mon, 20 Feb 2023 18:51:00 +0000 (UTC) X-FDA: 80488562280.30.03B204F Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2042.outbound.protection.outlook.com [40.107.244.42]) by imf30.hostedemail.com (Postfix) with ESMTP id 397068001E for ; Mon, 20 Feb 2023 18:50:56 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=e7cc+4Yd; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.42 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=1676919057; 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=ReVNTA1h+/h2+VtpWRFQO8/nEpuoqiSemE+EZgXTIyA=; b=FvLh1vWZBEX0lu7E+BzE1/PzKJMBLxxxFLn6Sh4H5vvoc7/6OhE9HHIc+Azbua95ddgf67 gXaDFnd++if1TEGMnyKQzieb/7qeqkJcazMn2rQRUyO5gdP0upOK7ZaLlIJ8KTiilKlGOU wr4js/WmYU0KelB4f6s6qdwptUgwbn8= ARC-Authentication-Results: i=2; imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=e7cc+4Yd; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.42 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1676919057; a=rsa-sha256; cv=pass; b=PDmbQY77FmqSiIsfXGu0I5MW8Qx1whlWz110a1Nsw5Yjqmp7LeG9NfCYWlNd9JcWCu0V8Q P2G55VliEWjZC36smgd8q0fXZkkRaHyn9ZbBVG44yAHEXKQqfE4Mh1O4jc6LOUJ4LHDHFq QapER79P3UJ2HW4bwqJ56MmVCVLFMFk= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RZetvvUwuas7ePHwiQYkhtxOhh84l2WHWaaSFTUGyC72IXmpmA8dSGGLF5kKgixkUXoXFtRbqd155hRLMYSECv+cFxvJCrqweRFHzjU9EKoYaMzdwLB8di2wvpx5PLL+AlZBNxJpCvfPYO2YNuEgF5AzDj5y0gdKTGMc2oXhXuEcocwMqP69Kglj7cMKWqx5WqOPCh4t94lId/eLfU4SZbzYHgworMe+DQ6ms1J53I8alrk0BbxI2GaYGGy5Qqk+lhzA2lamCVIUra6TpCb5NvYNaSL4oV3OJvrlTYjd8eAnCIRqS+rHcLFenKFhtcPN8zN5FcRLckCNvLDAFJFi7w== 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=ReVNTA1h+/h2+VtpWRFQO8/nEpuoqiSemE+EZgXTIyA=; b=RWaDtWjhkSnwuzB+D6Gt5E1bbNq8TxIeiTwEp2BYf4QzQDstzgotltIwFVRdjasvwtug7TZWA7ExWTA1F1DeP8poDucHwSSbn4YdL5TQYnKylk6ceGWprWsom0mb9LdTR9DGbNprINPQ+ad5a3hhHaSaQnoD/zCMub5nxPuVY3FLdKqsu4GMOUKBFuGOov/L/h4Hq3WNKpbizjNpbZAo9hcszpukSXXj1KGVu63hU0J2IIJIvqAe3GU+S5Hopt83+cfmkdA2t5R+ujkh+3OFUzdDuWbNIcrT2oL2YEEitUtrCgVvLyJ71JPlIajt9OGoLUx+Ac0BVbxQYJ8w0HpveA== 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=ReVNTA1h+/h2+VtpWRFQO8/nEpuoqiSemE+EZgXTIyA=; b=e7cc+4Yd3VXgNoNWqGWfQUwN1CmdH1zkRhV7QCe1Wszt1QjlrrtE/SfrGXYxclhF/OjNiAIU9N8UxEsICWRuYvmj3+5ot0JaOHjhtoD0zL0CigIQdS1pSbeN6V0M/e3KhHsiRorq5EeKq8VrJMgLA0BItRE4NNCihu4xaXped24= Received: from DM6PR02CA0153.namprd02.prod.outlook.com (2603:10b6:5:332::20) by SN7PR12MB8129.namprd12.prod.outlook.com (2603:10b6:806:323::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20; Mon, 20 Feb 2023 18:50:53 +0000 Received: from DM6NAM11FT041.eop-nam11.prod.protection.outlook.com (2603:10b6:5:332:cafe::29) by DM6PR02CA0153.outlook.office365.com (2603:10b6:5:332::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19 via Frontend Transport; Mon, 20 Feb 2023 18:50: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 DM6NAM11FT041.mail.protection.outlook.com (10.13.172.98) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.17 via Frontend Transport; Mon, 20 Feb 2023 18:50: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.2375.34; Mon, 20 Feb 2023 12:50:52 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 29/56] KVM: SVM: Add support to handle AP reset MSR protocol Date: Mon, 20 Feb 2023 12:38:20 -0600 Message-ID: <20230220183847.59159-30-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT041:EE_|SN7PR12MB8129:EE_ X-MS-Office365-Filtering-Correlation-Id: 0d92763a-4afd-4cd0-86c0-08db137364b4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: XOTaE+RcD5LtAz0I2XsBsGXZq4KjZREXZM68OfE18lLEVbeD25tdzPslAVxZAE7v5L9CADSLpmJSdY8zuI2Dh55EIx7zxO1Xf0fxMbo1lvsvp6xA6tw1O9D6eOooTLWSmwu7g1k8RWwArtGinGhPbU2PoW9GL6aA5D93DmzUKSuDEih/gS5Oczw38EKFzdLcetJNGs3DBf5gnU+8EgD5fqwd246WYSuXRIfEhMV/6jMhAQtZEUQD+WD99aW3wIzQ9EyZsIzRlsnLaVqy8B8dgvKrS/WTigbEv7CYrkIIhnTMyD9Wu+ctrtjuuDbyhEyDWUYrsPsE7uaCTqbM6D7MqljR/KWGbFygcfFyl2CFduoyXPHNtny6Y0+olU9uPs4oGBh3//MXMQBgo3wbzXatIphd45/SVEnq1Vo4q0bBDDitbw2eiQTPrClNsIcT/xTRgb4GNCmHKTFPhqpReT18bEWLyOKUfCl95f0Yy5Z6WPVoqqTeMWMardGLvww3VwK4kIOE0zTKeOgYgQPp7OQdq0aBuInbcnOOzoC8O2p2tEi+P0APKG2Zd8kgiUt8jdl2LXePyj6ytETCFfyY5+WpfvtPCqUDpvhjasp88VsyJXyHnr9uvVV1I5+qNtAB1R4bi2TgbNjua4lYK9myMhgBTvjrOwxqYbOBRvs66ijXc5GaDJNuA8JqZ14CwllR3vL7Ym+DjojIZ3mr/csQeVhfte7xv5I1symGR8KgoSkpk3g= 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:(13230025)(4636009)(346002)(376002)(136003)(39860400002)(396003)(451199018)(36840700001)(40470700004)(46966006)(36860700001)(40480700001)(356005)(82740400003)(81166007)(83380400001)(426003)(336012)(40460700003)(2616005)(86362001)(47076005)(316002)(54906003)(7406005)(4326008)(36756003)(8676002)(6916009)(5660300002)(82310400005)(8936002)(7416002)(478600001)(70586007)(41300700001)(44832011)(70206006)(26005)(16526019)(6666004)(2906002)(186003)(1076003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:50:53.3995 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0d92763a-4afd-4cd0-86c0-08db137364b4 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: DM6NAM11FT041.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB8129 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 397068001E X-Stat-Signature: kid7ymprb8tqrt6zgs9rwptjgc6r37hy X-Rspam-User: X-HE-Tag: 1676919056-872615 X-HE-Meta: U2FsdGVkX1+E+MdOJqXhLT+FkYOpoEPAMu9afuuHEeGV4Hu79acNuw5AhZClgHFHdwS+M+faOlVzKX8hKMJD07gEAKgLNKwy4AEjSfxMVHZfbz75G631ppBW25y7MvM8MzUAN2JFErHZpZI3VrBIuAW0gkk5jyWftK1jXo0gvNN9aCRwY6kkTRIJclfV+WYLucqo8RLSYXbMtVdngnBv/OuPxx4DmXiTwGy1S9w0ZvcDHeBpMi0PWF0CjuWssD7a6xPR/54ufLW1SPysH1QuYu1OGy9RqiP1vzkGNEdiQOufsUcJ/6V425mp1F82zZE3vRK2Nvpu6YqtC6WQhV8hbsw5S0j9fCtZMv8YlOxcQIGIun+a2yxkc1tV/oJhxFoYCybwDGHJMth8YEpGZMPBBam89jve0Wdzit+81E6A9rz7eJb4sI74PKiJj2HfkoZn4HSaxf72x8IjVZMaF9HpNgZ+1ukXAEvGF5N40Fztm4jePyhVmRU6sLKl9sLZANj2lcTHjMk5xAcKAZhe6HMpQY0g+lxoc+B1W7yUMYN7yqFXEJYfTx1CeAv5hHfOY4kr3Vcjd/GJ0Vi7mYhrPftJiIAZgdtSu9BbVm3zYqocus5KW1BANCHmQTTUk4NmHWjSh+1aN/hqY+Aaef5z8vzhj8bOySSWRi4xv7TgT4fzqEzACn0kXUAA3s7CWc3n/4EPV1cwVqwOnHB34Z/hvipTc/hQeISsq29U0I1ZzqNlpoXZCznFVQ1aoldQ8sih2YznWMI4A/28esEEtXGOSCEKVSSm9Yc8IT64ONpUrGp2decsN5f08UZc40SsDKYD+Gis8bs71KJU9dXhxupbXy3QhKhmHXnQk1HLzyj7/VlnQPx3XECrXgqHcEbKINRFriCZuBtZzRiaWi+uFIjDsKPn/I1nOI6jKfR55Wd8hA+94/SsMx0nLiHCBJUZPd/VSTCTrX4glq9xC/yz5ep67nj 8LmKYgHy uPZZd48Ca8jA44ikwXzTxrQ1YYZQVnvWm8UGI0krxxaK6fDl+laQuAuhzReItTgJdtK3j/C8/b50yMPoY+P3aXVrl4J+YdzIYnz1pi2Rpw0cA4emnGX6P1Ee9SOlfNmgpTv17tzkw2rQkS+n0XxRjrQWMl4m46QNGYUmNUyxJdqwOWumBs1H5dgU4MbOGE0yDdW7b/VQpTE6WRJiHvkIrhQjIyyT9KjbjtztV+rEfzjSV3FDKomPv8mAjoACteU9q+0A5c3EQ0gELdSlUd0ZnloyZfrSYTjgXH//9XLAPUKvFhDQWBGQwdXAmj8Xb6XkknfcuSZZNe/Njrg4= 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 b8357d6ecd47..e15548d88f2a 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 ad9b29ff4590..05eda0940e22 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); @@ -2706,6 +2710,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; @@ -2918,6 +2925,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; @@ -3017,6 +3040,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: { @@ -3177,13 +3201,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 56e306a1f0c7..f4848e6aba28 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -191,6 +191,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 Feb 20 18:38: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: 13146904 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 A3AF7C6379F for ; Mon, 20 Feb 2023 18:51:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3F94B6B0074; Mon, 20 Feb 2023 13:51:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 382316B0075; Mon, 20 Feb 2023 13:51:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1D4906B0078; Mon, 20 Feb 2023 13:51:21 -0500 (EST) 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 0E3396B0074 for ; Mon, 20 Feb 2023 13:51:21 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id DBF35160419 for ; Mon, 20 Feb 2023 18:51:20 +0000 (UTC) X-FDA: 80488563120.21.239FAE9 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2057.outbound.protection.outlook.com [40.107.220.57]) by imf28.hostedemail.com (Postfix) with ESMTP id DC306C0002 for ; Mon, 20 Feb 2023 18:51:17 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=sRnSYHgc; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf28.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.57 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=1676919078; 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=pK6F+pvFORzmpnS48WkfqKT9Msdt2eLtlnHN8hpVCdw=; b=SyxuzRODkLm8x5cASthF8/DRuEhM26zvXMs0HqvO/uICMC7APStismzMpM9lty5P78QJ7W mz6RXNCKKGw077gBI2UglyBHyq7k7ijKPFjswN95eso8kuCwfRdguS2wfNu24ZFJqFVckV zng+eXrarDysu4ud7rPETwq2FRXolyA= ARC-Authentication-Results: i=2; imf28.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=sRnSYHgc; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf28.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.57 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=1676919078; a=rsa-sha256; cv=pass; b=3ob56kyLmPPmni+08vEdTWZD4Y98tLBm+C6r+IUMzJks5T6lsxxzC+7oeYxkNHFyNU+x54 pMEnlOPhDglW5VvxTli8zCD9rtbdtDlTeBzVrZ7Qnhnc6Pklr90+HDSGin8ou5/6EM/IAS aMLZmJYD9KcUphZZ/LBtmR7nGyLzHSg= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZdHW4U+TYdBoR1NKRGdLaZEiN/0pMr2upaJJxo09Ffl9PgpmZnoIof9T8sL30VNozDrQiso/Gvmghj2glvTERsW4b/WDAFWE9A6E3LNan0gDlAvPTGGwi665iOGVoPuV5kz0XlfAS02rcl/MBpuvxXmI7o+hXWtMxU6AbZB5ai9r9PWtgOaDGKHMdVQ+QnJkEcbPg4SQCf5lOCSITPNALHuoxIuJwUPfNVl4y2czB4WJ70XpEBPAKO20AAD9jjcwEQL8XRw4Gj1Id8vmnaNCvyp129dLOi8qyg8II9AgTLrOpwQ9cp9QLZI8nCnTc2MhbxBk/N+cFnrm2RLhhmMiFA== 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=pK6F+pvFORzmpnS48WkfqKT9Msdt2eLtlnHN8hpVCdw=; b=JqN2ucfZTa+5uqcPuJKe4eAFCCF0jS5a8n2r6ZnwkLufjL+iBMWoSJGP8aH+dIir3sh5bQf+ExxQy7QcRVH4FEXTzMBDaGcoIhnb/8kQwkP4gmNxjva6/dMTAqhGZRkiHNg3q5xGzfim5If7LasViu8XrLQBkC8aoSsh0x05PDsQDhUS+JdvQdaM4YUf6nLopmxvpBfrKf4aOjnqANJmdHWTizj8ia9rZS9wLZroPoSzS3okNWCyvzG3gfz7ccUbpRFPMRoVHAifPyyZkb1LOjhMrPQ599XYFrk4nNMwjvxrx4KEtjk45Ny6pnUGNjfmhcG/MjiZUBCSXLswiTPyVg== 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=pK6F+pvFORzmpnS48WkfqKT9Msdt2eLtlnHN8hpVCdw=; b=sRnSYHgcMD9peg+NPPUlZOxLJ8Rasutg0+ECBG10haIfLQxCMevj/VLYBgbdcOZ+5LRhrKDgc7AIhIth5co2QKW1tVuUKZaizaA1eqdASOhOIEqKg5PFHFmcC8tM/DuMEPLhEHdJJEODhA37PP3AZLC6IDnoITWp6iywi+0Tu+M= Received: from DS7PR05CA0048.namprd05.prod.outlook.com (2603:10b6:8:2f::19) by CY8PR12MB7683.namprd12.prod.outlook.com (2603:10b6:930:86::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19; Mon, 20 Feb 2023 18:51:14 +0000 Received: from DM6NAM11FT098.eop-nam11.prod.protection.outlook.com (2603:10b6:8:2f:cafe::e9) by DS7PR05CA0048.outlook.office365.com (2603:10b6:8:2f::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.16 via Frontend Transport; Mon, 20 Feb 2023 18:51:14 +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 DM6NAM11FT098.mail.protection.outlook.com (10.13.173.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.21 via Frontend Transport; Mon, 20 Feb 2023 18:51:14 +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.2375.34; Mon, 20 Feb 2023 12:51:13 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 30/56] KVM: SVM: Provide the Hypervisor Feature support VMGEXIT Date: Mon, 20 Feb 2023 12:38:21 -0600 Message-ID: <20230220183847.59159-31-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT098:EE_|CY8PR12MB7683:EE_ X-MS-Office365-Filtering-Correlation-Id: 27061840-6531-409e-7a37-08db1373713b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ihBvJMNj23d4gRAJHW+CDN2Q9f0LLMQFvjksOcwdXg8Ilb5ts2oe7CHAsINweQkyjfS72GgTltG91q8tsyyJI93lMvNDNsHYkLrVwnMoDKsajR1AxR0eXtuJ6ObRvsbmtXQHYJEKmdjEWJLdv7DWFzpItvBxP+U1hSmBo5uZBr7t4Jjs6KXnwOOguk5CwodrZkcnMsaQ9eTGA9fBQbTy9w3qvFuGYABYiuyvTp1XYFTRy8sdnUpcjvTrBH79tGaes16F1uwV1fIKVPmNABA6kc0TJhVepIM/VkFm2ZXor8ju3NnLFjgmaJVUGjFHUI/A9+/ISz9N1gT05PnytX+zP2fVoWE6BABJ9Bkyd4mMDZpNh77tPQNhnpxF514QvXLMqt1WIr+YAFfaI4O86VkJH0OsLpA+qDs6XzUkJm7UxWvg7AWmdeyBoFSNJekot5lUtWbpSocFznAwzNTCoVS8yVrN6kiY5rT3GKzUVO3rshPK8B5brYxD8sP7LiGdYpKxMJN7V+IoD1YxrhbYYZBffx+GAv1CfnXJyzQNeFIBP4P2Yifsssbtw9PPtuvImji6fklK7RIsBjNPC/BgZU9ntzItiyyUksRDEDK4gPikpj+FqKeL/uQY6mG1zneb3CEsTR8QkAQNd0a1F41JtVVUECF8WZA1riq7+Kp1bWGVsG04Jl8wiAhGKvPpR83khrpFfxxEYccozmooCkxwLk3LEBX/VQQvjJGuLTjc9RIirLk= 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:(13230025)(4636009)(346002)(39860400002)(376002)(396003)(136003)(451199018)(36840700001)(46966006)(40470700004)(82740400003)(44832011)(81166007)(36860700001)(5660300002)(7406005)(86362001)(82310400005)(2906002)(7416002)(356005)(40460700003)(36756003)(478600001)(336012)(47076005)(186003)(16526019)(26005)(40480700001)(426003)(70586007)(316002)(54906003)(83380400001)(70206006)(8676002)(1076003)(2616005)(6916009)(6666004)(41300700001)(4326008)(8936002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:51:14.3697 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 27061840-6531-409e-7a37-08db1373713b 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: DM6NAM11FT098.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7683 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: DC306C0002 X-Stat-Signature: rrkrj8uzhrtads3d18yp33dqcufj34nh X-HE-Tag: 1676919077-12783 X-HE-Meta: U2FsdGVkX19J4p98AztD6zLBoCpUGfo27wpU5MxNgVJFBkRcfoCZRz3gbi2zMLBqD0XpQv+LE082T5FCQzv8LfIQ2KrJt9foO7E6imNvE7+sjmdYOynynXhOJSP7ko+SbLZTxw1aEhpZUPA0hTaFt9yhdz7k8ja+ojOlQb3cLA/D+1Jt+kfbaRT3p81zNIfKkPOI4DHhKAQhmrbQsKBQ6P4HV0881D4s3eLrHuV2JOkG+EbV66D+zz1ij3foFw/tk+AkIwMsNDIRT92A+bA7b0u2L8PsaUi18ckm0DjyGjRFb5m9ieR8UdhMn6UZGSJsitfN/zU4IGm/c1ROMtDJ99i2oseoHyJQ3lLE6JFkCNPInOq/232jq6xbvpXn+HJ6N6ARXpDHbA1poafActYtnCsyPnfpSW2qsA6+NiLdfO2RDutXBbrMd2LWvhqOfyccUBs/XtAKkCfIY1TVv/RQhwUjmBpXNbI7YMp29DMLPp9wFNvokPfH+RtI4dC8ASipmGxakw3LvBMlq/L2qRUjYQ5xtwieSrH6u0id+5m9H5GS4tZbeUQ/QORW9DftW5AWynoid3D4ZZ9aXE74TpOiJFWVJncrLaqlWkdNCRGsl7T/9YRfPxjNzmpFsLcGowUFEoaCdEdSmYT2syX/SMjYHW9F9I1s9giYX2BdJCwySBPhaEZCL45xsctZ0JXDID2ls28C0Ye5e8Gt8Ns9Hnwm5Cz9ubSO1ZikFHTi2c386N+SobbZ/ACRiDnBTTyQvnbtvJlw1frYSrPipwZXQQ13cOEYfmqGZwbz/aevtrF7uRrHOO7m7z5DRkqgRqG3g3ipT2LwLPufn+wvUYmlSLl2YgnU8fV3lTryDDCMHw0KDkfC2lyUkRg/gA760+l6wg7XOfqGoITtTcIJzA7TYwFeJ+f3X9P2Waw815v0oDpo6JYE/TQ0sc+zGGU1GnTXAmIW2IOHBQdHUUeccUHGLyY ixchP9vD jfOrK+lqj/gpYRg8pWNmssClh0myJMb4FDRz5d54BdN/i8A1dADUsG9FGVK1TeaHgwwhD4qnnmvWic5Bs2Y5QX0BaoMhSqZ3Q1NoqmkLU0LXly0HBUGWEYr3kK/E5FTogA/gEEuQ3Dan38pZUDmpbTyMgbMso1UGUdId/V/a1W9YOXPXVcBT9TOv5IP3G96WFiGrzT5jUnzuMraq2ir+kR3G6OFCXygoJyZyDuCr8KWhhlEYz5fOa16d/D2VJbXRpBb0VeUcBI2vdnEfpI0+4/gB/Ndi3g7/6cbHBzVPpcDAvh0M3ZUVBO+mlsi9FUet0dWXZoxU5q0bqQACib4QpMYEdmgQL6WKhbOyLy8iIYIbELcE= 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 e15548d88f2a..539de6b93420 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 05eda0940e22..c1f0d4898ce3 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2675,6 +2675,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; @@ -2941,6 +2942,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; @@ -3065,6 +3073,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 f4848e6aba28..c249c360fe36 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -662,9 +662,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 0 extern unsigned int max_sev_asid; From patchwork Mon Feb 20 18:38: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: 13146906 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 E333DC27C7C for ; Mon, 20 Feb 2023 18:52:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 817716B0075; Mon, 20 Feb 2023 13:52:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7C7BD6B0078; Mon, 20 Feb 2023 13:52:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 640FD6B007B; Mon, 20 Feb 2023 13:52:04 -0500 (EST) 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 53D216B0075 for ; Mon, 20 Feb 2023 13:52:04 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 3952D1202E2 for ; Mon, 20 Feb 2023 18:52:04 +0000 (UTC) X-FDA: 80488564968.22.F33087D Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2071.outbound.protection.outlook.com [40.107.244.71]) by imf13.hostedemail.com (Postfix) with ESMTP id 5042C20008 for ; Mon, 20 Feb 2023 18:52:01 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=4fRC1AjR; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf13.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.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=1676919121; 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=EiNmv50v1k6xWo6LXu4CGQp8xzQCx8kEUJHMu+8q+GY=; b=k4AzbImhbTpCRE+SJxjv+eSxJx3R1smL/NJ9ZJ1p6t1rtv3BdRwiRlCCvAOCRqnfz11qjG w+1Nl1O2QCVehwArnaSNPrOotnDfpPO+wkaaWdk6ie8+bCXbngghbNnlxswaZHl7VfXK2J dUmZrhvUhNGj0TcfhPnbdgirZ5FwaRE= ARC-Authentication-Results: i=2; imf13.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=4fRC1AjR; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf13.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.71 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1676919121; a=rsa-sha256; cv=pass; b=rA67vQmsAW+Pq+92CW1jMQw6xe8ZYBNFPg+0Q9m9jERgca1RSO/x6lvaorh2NS4U8WnOz0 3UZDiDf27LiTkdZDP69ebecwPEt9odRQC427pGVVI/BMPCyTNTmvwysBhJ+KTvzJ5vFt2v VIWbJcvYWAQ/msOSG4s5w2gHlkcohvA= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=c1677IsrNIci7studV3s+nL61csVqTyy9ziqqc9SRUon2IoL/Hbn7vPekWxVjZxCbUBu81/gkKNQheNULdDB48c8GN1yykARFGZCqSzih3nbQO6n+eL5SEWQ0dWTKc0BYLr9Uw4vxnqtSglWwB3GNBMO89cRk1zlbTGb1u8kztjuiMa8HF4uSHTdU09YK6PUnUAg7tcpnrqtoXL2YgARmoJktZr+2kPtnJl0LQsSnS7qtj6m3PjDbUoaTulv+3qQU/KEUhpa2Kv/XWRSROyrZzP9vuFYbRpeZSYElM86EXxmyMFX3l32fniPrerRfncyFC5CbPnNf/+6vbFDKJdOvQ== 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=EiNmv50v1k6xWo6LXu4CGQp8xzQCx8kEUJHMu+8q+GY=; b=T30EAm2En7rbUiiYkb16Lcvv9MfE+PnchOtLvMgPYmP3jnw3JKvolZIfPsA+mw5jzRWp1MusY1mcFGDt10oKDnDieiYRPSUcazYVpp66cxOkEfTdNUXzsPzbCtDLSrQTU+IE0qJZo36NdINlaMGcI4CPYHezSt5whkiaD+Im2IWqmBsI0Eg9LWlBdOZAaWIMlMVIE81hFcTxDr4fhQ25CP14KhTDUr2jpGEF+gpouGoEOw0B79UYq6L0MREIA1tdhZp/BplYIw4Ato7QMLpJQyhs765RZyTKD6D2UM6lFzo3GlcnXtFMCXGlCZnVen4wdfdYEt+rD10fPAj/J08cbg== 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=EiNmv50v1k6xWo6LXu4CGQp8xzQCx8kEUJHMu+8q+GY=; b=4fRC1AjRVQ09W/vbXRBYP/LcQf6DbyYtyTW4QiU22p0h2vjDcelylI0PosrHo9+COK/z5Ba6yEmGcGS+MPeEnpOhildkEgLIQul5WgoAF/6M6vBIWqvM/Ftz6PxM2VIEeZ+8fzYkTFhUQh9/8fQZ0NYi6lBk4sIQgBnSb9kZfdY= Received: from DM6PR04CA0007.namprd04.prod.outlook.com (2603:10b6:5:334::12) by MW4PR12MB7286.namprd12.prod.outlook.com (2603:10b6:303:22f::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20; Mon, 20 Feb 2023 18:51:56 +0000 Received: from DM6NAM11FT061.eop-nam11.prod.protection.outlook.com (2603:10b6:5:334:cafe::23) by DM6PR04CA0007.outlook.office365.com (2603:10b6:5:334::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:51:56 +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 DM6NAM11FT061.mail.protection.outlook.com (10.13.173.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:51:56 +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.2375.34; Mon, 20 Feb 2023 12:51:55 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 31/56] KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe Date: Mon, 20 Feb 2023 12:38:22 -0600 Message-ID: <20230220183847.59159-32-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT061:EE_|MW4PR12MB7286:EE_ X-MS-Office365-Filtering-Correlation-Id: 79800487-d9db-43d0-644a-08db13738a3b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: W8lZTb0pqpbErP1JqdvrCRvUYEXBlWNkL8TQZsJJXMkI07Tw9mwwPGSGOOMS1gy8/KwJf4azFcuv1TkC1f5nnDJsKo+x1WtM+Pw0507vEd0WPtajldbukCs+3S7VZxZdq7vrXxJomOYpQWRp6cqmR/4AgONXsIyGKCUBGbOYtauohe287kFNaj7d7dMV7i0qJjAtXDu0WZNCOKgOnOMxExWVhZPfVaHVX05xStlLBXBCwRAwalWNQZgyLTQ8z5CcyWnafOOHzBf2dfnfVjcqWpNbNobqx1CxNdsth9oAq/roTxpk7mLG3Val+6zvHqsVwH0/y7Wr8o/D4dtfa+pU+Qt/v0j4reTu0Gb57MX3XX45es4iMnG1H3QWyzPvEg9tY5zmvr0t6P6A/Qtyq28VUxd08N109+mZu9hmK764vE6C1/jEIOqbwOGYh2shaxbojX/YC76N2U1UGpPBBSukA17FFB4upGrCOu2Va72x0uLtW2d34Zo6wRsMvO46bwVnrIwutgDVkPbrVzIBhVk3ntCSA5eA2HssT1O4Z2jHv0aHVEZyXjZgE+iZ529Q6BAeFu6rWiv2tGaeR/jBaozrjwHZKc2/HMynbLRiAGnlNvPZWgazYFM8HtiDI7FE2WiubX74Qu8HTTSzbYBqpizpVndmiYn9N8hc9Q9cbLJvRY/q5WQW+610RDo2bWLCJgnY+OAw3EnTguIhVExn6D/ibb7759vcPvgSpD4J7Rjnoaw= 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:(13230025)(4636009)(396003)(376002)(136003)(39860400002)(346002)(451199018)(46966006)(36840700001)(40470700004)(82740400003)(81166007)(36756003)(82310400005)(356005)(36860700001)(16526019)(40460700003)(40480700001)(6666004)(26005)(186003)(1076003)(4326008)(54906003)(478600001)(2616005)(70206006)(70586007)(336012)(2906002)(6916009)(8676002)(316002)(7406005)(8936002)(83380400001)(5660300002)(7416002)(44832011)(86362001)(426003)(41300700001)(47076005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:51:56.3590 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 79800487-d9db-43d0-644a-08db13738a3b 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: DM6NAM11FT061.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB7286 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: 6is9483xnf9saw6cysxyb4bcihdgbfw4 X-Rspamd-Queue-Id: 5042C20008 X-HE-Tag: 1676919121-168396 X-HE-Meta: U2FsdGVkX1/eLnx21bWMyQxb+TivLBZkYAFZa7zplwWL+OT050v7cG87XD+b3CBQvIIs/fTVWS8aNhPn0yQwwAECQG5c92Kz5f9btK1W0zDm/IgOb2BmgIp9ZeZ+aDmX0Q/LLLTytIdTvg8Z9r5KGc4jPccqedQaKO3Dyqp2jkPt2OCMpMv53msx/vngHjy5FtkTC8JjL+YABXrjDQY2HWJPQYxqoqB3SiiD8b20BoFSxBBUtqKlrJZKGn1ybe2J8WYH67HzenC3cqHlWYj94Aw6hf4y7AKi7jM6fJlBtfqT+JXEwUzhHAkn+mpvHESi0BaNBm5ybQ+iUJMYcwenhQW5Qnire33AZ0Dx4JEvLtQtKHmLda1BzpjRn7710dlwiXMz5bWLwxIQuNrZP2Jh0iRxRXkdd2axdTEHGqlSEk+yQgbqBIBk/T8iDRWXaEXqM/eqJTuU1IJFixaBRHcV7zSBVzOUXOr0lTmFjztV8EPaMGwEjhFtVZuIGNQy/YGT9i1spPTgcG0EtiEpJT55EKAzn1kthQR4hLwpfH5WMmZApF1p6Vbxs+5tdnJvFBdzgCFjcQxGwxgB66lO3De/aTrKeCpSHnxvJIL/1EVlDXIaFryCFOoAPTXMbNsryQn65oDEQX0OS6G02aGAVqwtx+kczPXpK/xDg82EbGAhrG8agO9MdwraO4lUcL1iMEFeEoqwdpfMQFjQ4mpSJP8XU6CcruteADZv6fCg+cSEEMGX/R/EtsHhFE7RPvXVIcyysHbQ6lU5kErSpC8w22S/2zv5GhWgQzJbNl5280TO91/FUbqu5WkA8ibzsEHExZ34t2X8iA/bRgjhXpAmyaUmcRgMvZSQyJ76+tFAfUd+zZjXhWc3Tdunm01umo/JQojXLxxu1BbNlmee8t6u+w9Ngi09zdEPiMEIDAsjUns7KNjUIrfTUCNGeBNzYpvnx6ppSnyg+bFQSdqOgAjnl4S Tru3zwFn aHWkIxkIvpVbQhfaGaaxwmH+etcKIktVM5mVKoMg84Fp8U4nw3ACUvPWd83d/ZaIml0gEqpqWpQyfB6r6nFDElZA4b09RBGEG9A+BiwgPmddAD+WCFkuHzPwEwaKu3aMz+jivo/UYQwgLoeBQP9VabpdiT2CMDpLQcP6wMdKy577cgiT/FW8NyirgtGgh6R/RKdZfWDurAfX+EtKqsY3HfOWDQSNmKin2Ch/WVuCPj+juayXfLSobRhPfb5dqCm73XMw3UsxcIHpuzfqsCa+q17Ts/wvKld04hWNrcgtASdJvuavucxkjmo/5lvKIZf7ZAW8+mjHYcsifqvTRxjqBjPWEW9aA0N48W+qY 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" in the RMP 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. 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. Co-developed-by: Marc Orr Signed-off-by: Marc Orr Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/lapic.c | 5 ++++- arch/x86/kvm/svm/sev.c | 33 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 15 ++++++++++++-- arch/x86/kvm/svm/svm.h | 1 + 6 files changed, 54 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index 6a885f024a00..e116405cbb5f 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -131,6 +131,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(alloc_apic_backing_page) KVM_X86_OP_OPTIONAL_RET0(fault_is_private); KVM_X86_OP_OPTIONAL_RET0(update_mem_attr) KVM_X86_OP_OPTIONAL(invalidate_restricted_mem) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 37c92412035f..a9363a6f779d 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1729,6 +1729,8 @@ struct kvm_x86_ops { * Returns vCPU specific APICv inhibit reasons */ unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu); + + 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 80f92cbc4029..72e46d5b4201 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2740,7 +2740,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/sev.c b/arch/x86/kvm/svm/sev.c index c1f0d4898ce3..9e9efb42a766 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3241,3 +3241,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 213593dbd7a1..1061aaf66f0a 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1372,7 +1372,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; @@ -1381,7 +1381,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; @@ -4696,6 +4696,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, @@ -4824,6 +4834,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 c249c360fe36..5efcf036ccad 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -692,6 +692,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 Feb 20 18:38: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: 13146907 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 15400C6379F for ; Mon, 20 Feb 2023 18:52:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A87796B0074; Mon, 20 Feb 2023 13:52:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A378A6B0075; Mon, 20 Feb 2023 13:52:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8B1736B0078; Mon, 20 Feb 2023 13:52:23 -0500 (EST) 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 7C5466B0074 for ; Mon, 20 Feb 2023 13:52:23 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 469E21C5F4A for ; Mon, 20 Feb 2023 18:52:23 +0000 (UTC) X-FDA: 80488565766.22.84592E6 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2049.outbound.protection.outlook.com [40.107.220.49]) by imf24.hostedemail.com (Postfix) with ESMTP id 456B4180013 for ; Mon, 20 Feb 2023 18:52:20 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=mKXvigIB; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf24.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.49 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=1676919140; 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=6itsZa5HGOy70yEThEpdgNmkx82wXLkHdNDZZ+J4+sA=; b=1PcBJJl8ItkZ2GDjy3U2BCl7o/XYRsJTB+DPnMgK0FWidPEY/O6gWa/yS3ixu47UAicngm koI9Uzdo9PsPkk1FJvX5TpTeWjCgEHbs4VSmj13Wi3q+GONIM8aNWurAwVHhy2GeK0eNqg WyShntK7GL2kDJ1Q1ixapm6jswsRY1A= ARC-Authentication-Results: i=2; imf24.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=mKXvigIB; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf24.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.49 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1676919140; a=rsa-sha256; cv=pass; b=5iRSLi+x7m/tFa72HL7gbOchgVTlBvZ2YbJnTBzzHYAlw5MwFzm2OG2CfIP8v7FT7MUDQ2 Gi65RJyas9gfMc576Ft9e0vyBQCRqmB7GjBKqvUzaY1gec4GPKIVY94GAgyQR+BuqBSJZp zA/3JEvbPdDWz5T12SLSsnu0PK7kVrI= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fmxO8VWWUPNlTQgO8OjLwCHU1yqFpJp4FN2Q79RvaLhuCU2N/zc9o2EmI1pyD5U0c18kyjoGhsQM1NVCrdHTljzzdvMlwAKUiFXMcvKk5nQCe74EmU6p+wQiyOydg0PvBQnHqdAbvug/tu+v7CHhaJRMKOziBCdvVp5Ns9uAGGMGpcNjAOGjcnuDuELplxqroeXDurvoKbKJx0r+QQhPJanbHh9MynU9BEGRJtba3pCLBd1IgLyaACgmES1H1OodRwzSZ4fZ+JxetNr350fp/3NKaH8fIzwzU+qcEkF8oXJYq6Dw+tl3GbCnu7bkd0T5NbTzN9213A9ld2V4fq/LxQ== 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=6itsZa5HGOy70yEThEpdgNmkx82wXLkHdNDZZ+J4+sA=; b=CgDqk24oFRfnCmZrZ7kAn8GumK6vUmlll2RzGP2RAhSK5ZpjY+k/DDg2wYy0WxwSQMvF1ftcoel1t5Da87Tp0yk1LeeouwM/OVlZ9S00pokERSg4qq0mxArUwJTrtzGnb8VtiNPAXaZQ195DAHBzYSUgXHSBWt6f/NxPLR4F7q91kodqp3G5LGuwmG494QlnGBkQtsWnV1zsnQpS3GJpMzq4U1WdVnShIGxSNdP5PanSHc9Hld4DQiHZo/R+wGMaqszZfZhpK1PSjK2dyg5VpG9GuKBXG4JWR9RxS10YQLJf2so5QehL5NWSctnC8sSTzwEemI8kbFJ/89n8XnoWIQ== 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=6itsZa5HGOy70yEThEpdgNmkx82wXLkHdNDZZ+J4+sA=; b=mKXvigIBcNfNnFLV0wGn2Bb8hl4I5dTcGTPKxAhgIGj7JurTTwi9NNC4H3zU6+1bayo/jkWSopdgm5WOZdYNvUQNNECtgeZM8lGW2ewoJPh5SDkDFwWyJobq7Fl7DRY9h0Isp4e8ANuBrtWkAKWmXrd9RhTM9fui3R3EN3Adeho= Received: from DM6PR11CA0012.namprd11.prod.outlook.com (2603:10b6:5:190::25) by PH7PR12MB6441.namprd12.prod.outlook.com (2603:10b6:510:1fb::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20; Mon, 20 Feb 2023 18:52:17 +0000 Received: from DM6NAM11FT052.eop-nam11.prod.protection.outlook.com (2603:10b6:5:190:cafe::7b) by DM6PR11CA0012.outlook.office365.com (2603:10b6:5:190::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:52: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 DM6NAM11FT052.mail.protection.outlook.com (10.13.172.111) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:52:17 +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.2375.34; Mon, 20 Feb 2023 12:52:16 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 32/56] KVM: SVM: Add initial SEV-SNP support Date: Mon, 20 Feb 2023 12:38:23 -0600 Message-ID: <20230220183847.59159-33-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT052:EE_|PH7PR12MB6441:EE_ X-MS-Office365-Filtering-Correlation-Id: ce23ba12-6e27-4f47-ddcc-08db137396ac X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: GFirWRmKKyLS3Uwa3xtqJnfO+uhdtR2CLFWIFmcZjMvMvL3qtfRinCMhRbfYfHMIakWEoZz4h0AGnTda+D34oIPooOSd3JG975yl0zUlG01r0dus5ClBwBm/GxKdnd1tLhT/ofyJZo7jX/E8598zoOqLtQxk6oOm1rK2B6hZi7sPBpGzptxn+PQpdthbe8+yDvHzSHXuZWFKwxaf5Mot1+p4PAPsMbfm87wCWJc4KdKchyq1dBUPcMq0YLv67dHgGUCPSxaMXSZEMtR0hdSgVivdC5MZC2ES2Ben0UyqS20yLc8M0GkNltCVlJ86Q0gfy7SDEf4l2A40tsfdOxWoq1Q48E5K/L4iFg9+4pPLaQJ3nBoaolbSuVw4Osm8NbiYYnSCRLdUv1Y5+wuulbw2mX758duUptBqB49qyz+pDmMrhuqN0Z26bahbScVtCCFwNXqHMDz5iWIwhpE3quWSF7EbpqdD99b8b/aM/Yd+lbgophtIxIGX6SSzgYkHrmNkeuqBbglFGi+7baqW0icxn8uVh6QfJ+hIs/gGwis58EU6wxV+iEYhb+wznmwZ9beau5DTSELxKRrEg9Veuz9eyBKv6N8QKI6TOpCEb6+3JQCOfLDqR+tTU3DIQpHAjJK+tYGuqwnAzt+f1kfSoE2BupsOVbSklfm3PXns0iMemMzoJW5gSCol8qUaEgUaeQVLhRp3tWLNA5i+mVr/FRaS795xvQhwa8ciCMPObUNNTZ4= 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:(13230025)(4636009)(346002)(396003)(136003)(39860400002)(376002)(451199018)(40470700004)(46966006)(36840700001)(2906002)(44832011)(7406005)(7416002)(5660300002)(47076005)(83380400001)(426003)(36860700001)(8936002)(6666004)(36756003)(16526019)(2616005)(1076003)(186003)(70206006)(26005)(81166007)(40480700001)(356005)(40460700003)(86362001)(6916009)(82740400003)(41300700001)(70586007)(478600001)(8676002)(4326008)(316002)(336012)(54906003)(82310400005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:52:17.2197 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ce23ba12-6e27-4f47-ddcc-08db137396ac 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: DM6NAM11FT052.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB6441 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 456B4180013 X-Stat-Signature: 9frrk6dyr39y19xxomqg4b6dfzw9rxii X-Rspam-User: X-HE-Tag: 1676919140-548528 X-HE-Meta: U2FsdGVkX1+k4EpAI+XRpi5eoGfDZJUx+dqQIouKfAgBQkpQ8L0VgqwVZYhH06Fiu7jSMVz+YN3VUzS6bKUvDIAQvYgvOxzscljramFQRNLFUrYYGKgvUYh7ary4kQNCgYaHVkYwtqdFSQj3An/yW8iQB9jjNUmnFRHgY156O2hw6Y0GGv+OZMLDIoAwPipBDkwUM9EwdO9+Tm8/yTqKQOz8YM8qVr5zXXGEAnpXCToY2ZLCgLS7NNb2qoZurIVMUcdLTgn/fQs8XYFMCqSFpFMHAuMtCAeHgeLfYJ8UIn3CCzInB09jDUYpGuJn8DWISVfZR5hKP7BUgWQAMl4eGXbhrPlMyYelml/hV+qwaOyT8O1xTRQ/NTSdczgh0z03JawXAxGUDAGzkh9uhC/I5xldnaNJKZCftzY6LB/4533lWe6OQDhhvgXi/ryAy1seALha4XCUWqra35aOiE9Et3bbTY/l6AgYAxKrYSbtZ8owHqSBMg8TVTwR+l6ffH9gGk7G6PGgdH5sgK76acX+2KQsSBNMbaufASNePZbw1R1zLGwIUoB6M9GPnWizTo1CvRqEJMQXVinYSL0qbZKkPV5qVBLYzy4H+ddgal4m9R1WmzQRHBzChdiub0OLRWnRGjg1DnzHDm0eY5BbGB7ltcH5LDj+em7xNbbecuSmHtz4RRUsIR3T18Sv6FokW9JqzlXgzoKZxmbpVI8523/S9+cBmFTFobg7OIQgOzNoNGwMm8hvT0MldgWLWAmkn5G+k248wERqUtmPDFz1zFUWdHI3pjg3xckeO1oy23mWD+9ox6ogThh2tnBhfKb1wUO8ldygGrgOYxKHumi5+gFG2YLjsLCy4KI1iizFz4iHyeQLKySj94NAqNQbonGWOxBQ4HdVjYcIWfV3DuZKCh5oQ783Lul4ssnWbI/OMeHNSvB/ejPdkf8QSggy+KwNzD80zG3u1U0ZdvXKi0H9kW/ AbLj0YzW FLMZHfszA8OHi6iP6gpnTolFw+1EdvVhhU858gv7JW7qWH7AngA9+Kx6JbeNfZY2h5jVIaHfHjwHtNk6LButjjAEhseYi2PlJfQNnJqNsxCHenOvHFSAtYsPZgiDwUE3q9HlXfNq6aJOfdJwbnj1SJ2vhRO2+u8PQTw9YpgF+oOKfbTU88Ei8vt6as5vKxx6Pzixf4ueIr+wgRbIa3DA/qgqFeb1JLt/CCwGoNxvG0/7no1OMfn+vQHKpmarQbquNK45DLaPYcZ0cZxW7VBvSKXdo/cvJxNgz3Yem860BydL0WOe/Yz4aGvvyeRPJN3splqldvPvshPW2jUICwokKutXdvjI4cA3X+WitVEyUQTEuLXI= 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 9e9efb42a766..51db01b282eb 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 @@ -2306,6 +2309,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; @@ -2385,12 +2389,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 5efcf036ccad..8eb1b51e92f5 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -76,6 +76,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 */ @@ -323,6 +324,13 @@ static __always_inline bool sev_es_guest(struct kvm *kvm) #endif } +static 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 Feb 20 18:38: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: 13146908 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 8EEA4C6379F for ; Mon, 20 Feb 2023 18:52:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2E5C46B0073; Mon, 20 Feb 2023 13:52:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 296236B0075; Mon, 20 Feb 2023 13:52:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1104E6B0078; Mon, 20 Feb 2023 13:52:44 -0500 (EST) 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 00AE16B0073 for ; Mon, 20 Feb 2023 13:52:43 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id BA47A140430 for ; Mon, 20 Feb 2023 18:52:43 +0000 (UTC) X-FDA: 80488566606.20.D447404 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2063.outbound.protection.outlook.com [40.107.237.63]) by imf19.hostedemail.com (Postfix) with ESMTP id C22921A000A for ; Mon, 20 Feb 2023 18:52:40 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=BvDayb++; spf=pass (imf19.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.63 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=1676919160; 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=lzZtYSsYtaJMwvMB8+nYvnXncTA2MsPdhi/xqq416ss=; b=frYG4Ok0M/M+14Qj0Z5zsFbRkckLAIOGZA9QdmFI8tuM6WoG4nU0rz73kY0/lxFtA7E4l8 quIchFAR7lA50rVDCVFJjX/8O7kxBP50WrucvEEUjQQorm+SC9r/BgRbOAvz081s/7huc6 z0H2LMxMHlNK2UnXbnPyZ4NncVvl3X8= ARC-Authentication-Results: i=2; imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=BvDayb++; spf=pass (imf19.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.63 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=1676919160; a=rsa-sha256; cv=pass; b=Kdd8g6Fly9clbTQbXShLJj6A6ky+GaoiYJ6TZNR0hiFukjcw9r7v8Zy4CTAG/ajuLcpebz 1o2iq7YgsSDveh2GjegzxViTUw4CF3Cma0wg5PjvAW9Uxzn3MyVsyC820YTahC46pHhpIK ITSr3D0I+wSma2+n2jDoP1cNx7LXwzE= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aQ7BxpfBSKFJ+jskdVYkUFChbpmyB4dkd2ToJHlZ0FhigJwx2n+izYoU1ius1Zx/lnTW8eazA8k2cFV3tTP8MoGengY9qzRj3UDhhjzRUJefcjfb3nMtH7jkhd2WdmIOHNFQJxjygydycPr3ad7n9XXIC8j/kazVj/lJEDeEg4ysn7uhon/xm065ojxJJGRbnevs2gnH6aFJ+k9VdYlBnDnEu7rNbsUsl3HENfcJWSD/t1Aha7HXNbR4OLVh2lQJn6eMI4PG1wP2TYMFDVJhTB0avCdngvOZSindC2EG2DisiqKXXzHQ/KiWv/67bXnrGTuAiudd4DEN0Go5Kok+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=lzZtYSsYtaJMwvMB8+nYvnXncTA2MsPdhi/xqq416ss=; b=PmOuJDHwccDnftVUF2gG9bAVjZbDJtV4/O0Qm6wYdRIoxxbJNYi7/WW8rHx+HVeT0Y7aKziKNIvLOY4ZTMeEBYqTtBZcszV4s7DPVE4qko+J98+PkB2DJ9ZKW3eM8F97EPGNX1tfacS63aA2A1ThazcXqFnrZQSYAb2kVwbShuVpUYaiUsqlNHfauIbIsJk2MpL/t3pdErZoE2I/UN13FbvwIrHn4iXe6v57ONTVxbAFbczRXJg2Y9DL93WA/G7VanBx3h3ps++2pIg3mdwACzrsjeJM7NCmz58oeAChFOE6eQiteUhX4m0vz3fGSmpCFZuMlE5X+88wf7pZ5Pp1Og== 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=lzZtYSsYtaJMwvMB8+nYvnXncTA2MsPdhi/xqq416ss=; b=BvDayb++Yyi1QP5M0y4E3Xnshfvq1yk6E6X6GoLQi2QHvIocRsjyOe7ujovmCQqfC/XiFaEqTipLpNtwDua3Xe7gSePS3I2S7TYInK18V9oXvjT2FHyKMDe8oYdZAcYFacE+fp9QaHz0nu4jvgqajF5yJfOHo2NIuSr3vOvhhrk= Received: from DS7PR03CA0299.namprd03.prod.outlook.com (2603:10b6:5:3ad::34) by SN7PR12MB6790.namprd12.prod.outlook.com (2603:10b6:806:269::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19; Mon, 20 Feb 2023 18:52:38 +0000 Received: from DM6NAM11FT102.eop-nam11.prod.protection.outlook.com (2603:10b6:5:3ad:cafe::97) by DS7PR03CA0299.outlook.office365.com (2603:10b6:5:3ad::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19 via Frontend Transport; Mon, 20 Feb 2023 18:52:38 +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 DM6NAM11FT102.mail.protection.outlook.com (10.13.173.172) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.21 via Frontend Transport; Mon, 20 Feb 2023 18:52:38 +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.2375.34; Mon, 20 Feb 2023 12:52:37 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , "Pavan Kumar Paluri" Subject: [PATCH RFC v8 33/56] KVM: SVM: Add KVM_SNP_INIT command Date: Mon, 20 Feb 2023 12:38:24 -0600 Message-ID: <20230220183847.59159-34-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT102:EE_|SN7PR12MB6790:EE_ X-MS-Office365-Filtering-Correlation-Id: a03bb785-94b5-4cd6-ddf6-08db1373a330 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: FQIHKNEX5pmo00zCrEUktzboyf+dX77Y2yI09CUgwGkOo9FN7YfTuUq6OTusI1IFsY5LXg6UNbixiOdLhX/qHgbhix88lV93pF5NJqz7uahHQHcxhjzooj4FbUoN9jNjiaxGHLmECbUhFFSoKuOQPgpuFRABiqZu7vBOxd5nAu9jKdE3JjJ9AxIIm45CSWHo1ow9FkAngi4+UrTeaMZUUGfr39KkuASjsAaLdW1z7WXSMA1ZLedGYsfAbtCzg9o50BWtQYfndKPGI/AIzLRIjbGYhyj+qP/HZIm/r1uZ5YUJBjzAVI+HbESaGOt0rsZJJkLkRnk17rZbUNUyYrgkCPmkq/TWFsPak8C882ovyLul+iMfw2hLzRROlA/2jP28zyOz/TLwEN9tNwnIVCl/p4vKKG239F94gxplmuh3mSW0Th5fiQ9iv+S/sAyBEwSNmzHPoGxE2jsNpMoQTKeDwVdcaGYC8VX4/yg1qtjNiCbzUgaj7YEcLzNBGrGa6AxkLr2ekN0I8lykAkcojr/ear1HA9e52gUbeXmrTjq8kSF0q6sGqC3XRrgD6o5h+4F1NYFmfuD/3f05X3jFJn2afukQbUxDM6c1FlPkRU8Xg5Dfjh6HeSClGvt/u38X7v9QNIVIpVEbl+tStvSN8oPDHD14Yf5Spc+5iQMHI9s0TUylIBngrzT7niJnQ0YGNJuG7Kg+2whzFmlgpL5GIEAssgUACyOjzvGf5fkjVkJd9K0= 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:(13230025)(4636009)(346002)(39860400002)(376002)(136003)(396003)(451199018)(46966006)(36840700001)(40470700004)(47076005)(40460700003)(478600001)(426003)(54906003)(336012)(81166007)(82310400005)(82740400003)(86362001)(44832011)(2906002)(36860700001)(356005)(316002)(41300700001)(5660300002)(7416002)(7406005)(83380400001)(70586007)(40480700001)(8676002)(6916009)(4326008)(70206006)(8936002)(36756003)(2616005)(186003)(16526019)(1076003)(6666004)(26005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:52:38.2331 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a03bb785-94b5-4cd6-ddf6-08db1373a330 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: DM6NAM11FT102.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB6790 X-Stat-Signature: gra5cdh31aj51e5so3d4ftuo64e485a4 X-Rspam-User: X-Rspamd-Queue-Id: C22921A000A X-Rspamd-Server: rspam06 X-HE-Tag: 1676919160-901928 X-HE-Meta: U2FsdGVkX1/Um5hDjoZf8oIphd+5wNYuDgAJSKHSZ3uckKs+OQHgVpeFP+1Skdrq9nm+IoBVZR7wbvPieps5pdiLAdjVURHe3gJorYKFxuwKc5QxLW/o7rKheM6ZQWOxwX/KpMYy0uoYU6xKOTBN1Qp4uSDVgj5HVE0HBvR7TcjzbQEo5+VkpbtfM6NObVlS9esuPVFVVHEVgBWVgeOfW/EOlw88cuw7hqsk3R6LpbZLcjJO2xjpSfIy8E7b6Kv559DRBrRXJhX8LytJm0DntRPHrDZXshGCInj4h9K3nS+pm8cJv9mwWJFdFqlu39cPffJfR6pPMGLyEth5q49dehr4Bt3TExBzGi+JBSfgCLMQ0IRTod7nTUR/rxsfPSGn5+Y5+OvBK4KdTDe3pcasZjQgqdvw8p+HLi8343/lqemSkVC4tw4y3zfOu7fujBokfs+5Bg+i8E6vF2ltatAjES6DDqeYiMBX6vjSRua2yFtI5SI/OMIvcpvKPXs/B0O0VhfXSR8rdl96MY+GpQux1LLOzZ5xRB9U/dO7B9jM2JYbKcDjhJ0tqI9vu4nMa8jWQrbfqPSXHzU+MPn53LiNqg3cmNC+Ykc3v1ssEnlIPkCOG9xfjuNPUkGslLOgnXiA5oZnCNXgpUdP+g2amPir15uCWIpdFm067ZmCJKPFOmAVsAiC2YALnW07azLY/yODDwDVAsF+2tEROQz7nwthaiPYaFI13cDsGJiW0rOQ8K1hm7FfB8QDihpIikr2AeD3u7OiGtRXwUma1Kezpg1iiMgRLg5PZLpuxwy6HzDW6k6PydM/0oS2Wd5NcOjOI0hr3hpUgWakFmXTf/dUM6xQItU63gVYY+VaZaC5eZmIJUQSYGNdeXtKjsrnEl0bGcH3yiZt1t5qYftbab8H962EbORR+R5TnpU/X0cbGrR3lTq+W1e/kx9Vw/5RO8/nPnOYyFyWY6LMnfoXq3cU9eZ mEofMhZV arXl+jy1/Djm83hAgrY6kaZQXA8J/7dVpXjFjO672oY3YGjYCYsx2Sc/SG8rTj796npcWMLeGDgMwjZ/6rgxdPyjCJtUokRd73JY+F8GyvpkEIJlvQBypfQVUQ6HAdpUThaOLcsG7shU9jNnEzL1gh4wMTntxHiJpJpmyWFxt/NetjgUznYjfrE+RTz5iM8ReCFHPqE8r/3S8gVN4146ynUFxHAC2vrLaVTUTh48AEU06TcVPCUIqrKzVClPoyCRMlpAIfxaPjpq6FZDxnCiVpboJHL4onhSyvaBHy6+I7y4nqH4dJsFgNXyvX9vJ+3G0N4K8lZU9sghTgTjMrKCGM5wRI8Qfker8qlgtcMZyZwoSd4jFfyyg9Mv4HQ== 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 | 44 ++++++++++++++++++- arch/x86/kvm/svm/svm.h | 4 ++ include/uapi/linux/kvm.h | 13 ++++++ 5 files changed, 87 insertions(+), 2 deletions(-) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documentation/virt/kvm/x86/amd-memory-encryption.rst index 935aaeb97fe6..2432213bd0ea 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 cb1ee53ad3b1..c18d78d5e505 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -278,6 +278,7 @@ enum avic_ipi_failure_cause { #define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF) #define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL +#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 51db01b282eb..a8efe1f6bf77 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,13 +275,23 @@ 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; - ret = sev_platform_init(&argp->error); + if (sev->snp_active) { + ret = verify_snp_init_flags(kvm, argp); + if (ret) + goto e_free; + + ret = sev_snp_init(&argp->error, false); + } else { + ret = sev_platform_init(&argp->error); + } + if (ret) goto e_free; @@ -277,6 +306,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; @@ -749,6 +779,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); @@ -2001,6 +2035,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 8eb1b51e92f5..56a5c96d8a36 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -73,6 +73,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 */ @@ -88,6 +91,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 2fba29125ec2..499cc323f793 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1917,6 +1917,9 @@ enum sev_cmd_id { /* Guest Migration Extension */ KVM_SEV_SEND_CANCEL, + /* SNP specific commands */ + KVM_SEV_SNP_INIT, + KVM_SEV_NR_MAX, }; @@ -2013,6 +2016,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 Feb 20 18:38: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: 13146909 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 A3CECC64EC7 for ; Mon, 20 Feb 2023 18:53:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 41F716B007B; Mon, 20 Feb 2023 13:53:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3CEB36B0078; Mon, 20 Feb 2023 13:53:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2483F6B007B; Mon, 20 Feb 2023 13:53:07 -0500 (EST) 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 124716B0075 for ; Mon, 20 Feb 2023 13:53:07 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id C133580501 for ; Mon, 20 Feb 2023 18:53:06 +0000 (UTC) X-FDA: 80488567572.03.1EBBFAD Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2087.outbound.protection.outlook.com [40.107.243.87]) by imf20.hostedemail.com (Postfix) with ESMTP id AC0291C0018 for ; Mon, 20 Feb 2023 18:53:03 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="zY/3TP1P"; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf20.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.243.87 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=1676919183; 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=FB2N6JkgN/Cjkvv6VZ4HEN31LDCqlLOev6/9qU2o1SI=; b=WoyoHiZGBQ+Rhr49DjY32S1BQC9B7V42SzXVPEyuQboil7zeyHzZ7ZGX3R092v9Prcwshs JppELDTiiyx2JvWurCz3SP/kBT1pVxHBSh2sDZBAcAOQIYIJe9mWSOFKhMH70MsYX0GTF8 sNvASo201s81fW51rOLwWGCVcxCivnk= ARC-Authentication-Results: i=2; imf20.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="zY/3TP1P"; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf20.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.243.87 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=1676919183; a=rsa-sha256; cv=pass; b=G917s0tK2A77S/5Ws2mBHoScl9MGyLtIRyyfP9DNVvun/EUVEk34uTkfgYL7LHBC5zs8rw m/IBtVpiP5SwBpdlb8DmLzl1ukKh6rUDtPMrbkCXpy6sIldSiRpvmj5XxBD3gZp5u7z1a6 DXfEV4seh2BKf0Cww8R7eGLnGJSr0OM= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Bn0I8tRk74veKXoi+NS7P+CkSafrwCA2XaRi11ywb+SnDNqix4eGk4F2ywZm6gtME4/FnvBThtn+lp7eOjXhgbhZCKhstynzu0Ycgg6aqLNGlYgd/wPx3rVdv6UD+2LXj3KGaunsL9n3Q6i3o5pYWMj385PPJ8on1hYl6dbmYPyQPZX/nhwuqoJjRkj37fzBEcVXPAWZsk0P33+XzyHQsFBXXwHdOFHV+gZ6tEzOZRMT2H62zfpDEilGiCCPya8uOlMWSXv/N1+rr+3tRkFZwArIVpmCGnXjDDn3V20fM218XzGMgx6tT70+1LZ1qrqFWmX5dKBi+pivDB0ywnZsRA== 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=FB2N6JkgN/Cjkvv6VZ4HEN31LDCqlLOev6/9qU2o1SI=; b=bWXUgKZRDbxjKngBSfRT4scBV0OnIPLu2tGSjiMHG5HYD6ONnvEXAgsBFXkwktVHiL6S1Iy61svQVpI7BpqD0MHmnthuOynHfnmkDu+P8zFa+Tfdb+SDlNQmrQXOcPKcMe2WRg6OeOmau9AmpHCmFx3E6TsnW5NF419glNeXF6RH7BcLtzP4qphR9IC5Dax89XcSyhkALU6H39ipEcvxUthz0mToGmJ5NJzmz35+68KGL1rqUrxe7HweSwqsv3j78YaoSYQcvmbxCK3Mc/W5OJlGGxeso1UfNzSj4L4OZWOPQDHYH9yX4t4u7E6CE/y8JcLPQo/inVeTXblaTS0PWA== 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=FB2N6JkgN/Cjkvv6VZ4HEN31LDCqlLOev6/9qU2o1SI=; b=zY/3TP1PUkR8zHnjqGaKe3E3sE8nj8Cm9Mmc5LASblRG4RDEtxPUFfAhS9z5an3P7F36lM5jZpgdFbfscQXNVqYomIa/TGaH3kXcmjVwFlNpvTdVGHt+9pOKYjt+xgH599RnZ4vQ1HwoqCfgLZXr8yvXVCOsm/aCO4h8HnTvLvA= Received: from DM6PR21CA0024.namprd21.prod.outlook.com (2603:10b6:5:174::34) by DS0PR12MB6557.namprd12.prod.outlook.com (2603:10b6:8:d3::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19; Mon, 20 Feb 2023 18:52:59 +0000 Received: from DM6NAM11FT027.eop-nam11.prod.protection.outlook.com (2603:10b6:5:174:cafe::72) by DM6PR21CA0024.outlook.office365.com (2603:10b6:5:174::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.6 via Frontend Transport; Mon, 20 Feb 2023 18:52:59 +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 DM6NAM11FT027.mail.protection.outlook.com (10.13.172.205) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.21 via Frontend Transport; Mon, 20 Feb 2023 18:52:59 +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.2375.34; Mon, 20 Feb 2023 12:52:58 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 34/56] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_START command Date: Mon, 20 Feb 2023 12:38:25 -0600 Message-ID: <20230220183847.59159-35-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT027:EE_|DS0PR12MB6557:EE_ X-MS-Office365-Filtering-Correlation-Id: b1cec592-d609-42e5-1671-08db1373afc0 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ntnxtuGE2g1MvPHDtIVgh2XrCH4n4HCQS8ioegSPzAf3+gVG3OTAW1foIsXXcAA+4mRVrQZGQ3tDIY3v1nhaDuaKn9I2Lj5Bez5iBKgV4ad0nrYKXz/RMVdwRdFeEpRMD9P1TdP1T3bD1uhVnwHnGWpC505Am/6AkAcsgp1utPaLwskoXNks2HAdSvqaCyYpABoWT9ytOztASNaZI10efnik2Zvm6wJbo2HomysJkCN6PexNprGbfkNWDUh2geUuKDFZ9u+iWp2ejNt9N9tp9yenjMq4dEK9aDMKhtx1AM80ZkriL7DINghcI7kJvai/pbbfCGGCQbBqTAhg1BYmBwrgClGJQUn0n/orD/PzYqYMre//x3YO2p0INvFim9CKNu4RRJ/3d6EHc0vfZUYCceJqKSL7Fb0orLeBaezmXnjgAQSjUUHa4ZAEJIe8LtkGad8ffn9wG7Uj7+hnARn24udXZJeRrrPgBjeus3a3Tr7m/ry3Z5h3vC9tTlPYM7dUitfdH5BnJ+hnPbdjdmGTrIf9lUJJnM+207sAI0cwrX1hF+UChxSLtPoyMpjljRZWcR4tzXNLRPdR1RAyqlCdgqXNJBvJfZfswCzmpI6yqx3EDerN/pWpoHKXCuchUnpiblkwPuC3xnF+XnwO5ovZCGzJbKjV/tALySFAqGkGtarh1SGFKFreOq0JUHD2dqURhJwcWx4c+XHl4h49LZrzEcobplfZvXdykQ9h10g6Zu8= 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:(13230025)(4636009)(136003)(396003)(346002)(376002)(39860400002)(451199018)(36840700001)(46966006)(40470700004)(1076003)(41300700001)(4326008)(40480700001)(6916009)(8676002)(70586007)(70206006)(8936002)(5660300002)(7406005)(7416002)(83380400001)(2616005)(26005)(186003)(16526019)(6666004)(36756003)(54906003)(336012)(478600001)(47076005)(40460700003)(426003)(356005)(316002)(44832011)(86362001)(82740400003)(81166007)(82310400005)(36860700001)(2906002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:52:59.2887 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b1cec592-d609-42e5-1671-08db1373afc0 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: DM6NAM11FT027.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB6557 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: AC0291C0018 X-Stat-Signature: rt5u1oumxadjj8e1as5snc7r93tqpuns X-HE-Tag: 1676919183-426353 X-HE-Meta: U2FsdGVkX1+MZdR2OvOqbDSdWkI3y/zuhLvzn2Y9rggMxilEj82Yo9EQ/maVuwVyfOfLV1n8i5AuHIN/oOKvQbN5X2NG+8jl30pYwzDeP9BeWw9p82NN7aeDfyefKnOncdOw3Ujvw4jXluNqvTbqkdNZN0LfbfY3QVIlejGSusA5B5Mf10NnbyGc3nrgCEwVaY+71mBpnUJ0kEaf0pR7TyTWFlS9RFiku9r5NNSStMNi447sglDk7nP719sLEqJLTZb0lHGs+HxnaVRycghYWETR27npcXtuc5KoMchyQgGZTmT57ofMNdq+TuWKPhhglxfy7lQ9G84+7BZaELWC0f8QdPglcKhA2OwGWtHMyYlKL3iUOZUqevMyfWqRjIdehbiqfVzVitX2a0ezvVh7ace9r2rcWBNYwB1Q7lsCxV75uL3p7ioohEYkZrKTOLIxnlubLOpFRMaDU1XLuhFhQLjCMT4hVPCfXVSfv0wgOhSZSg4j6uu8FRnQjk1uzuRC5Du1QYvivXfrxbZ2A3Vo/nwClhCmKXoa0vAh5sOz1JCMu2VJxCckgOtlgbitXRzSqGe4gSrc36WbYucqtQSlh4V8jiuPp3dSng7zGa+tfY+viBJFt5acM2YBBqcUvueFV2zRVU+c52F1RKsUyljrdEU2RtlloixMBjwk9RyX+0ag7GqKs9L4kF4U9LXElVQXuA0DqjPe93XRt4xPbpsceYl/Gw1QBt+vCUhelKOIf76hZOJ9q5laUIrlYw7WnwDC2C6dvdRpI09rm3OW8XEA2JH0d/bXaP0L1M3Gdv65zF9xm6RrYJ2Y0iip4VtTs7okbQef5nzZCHL9Q8iAKibsIfXY6HCobHFSJV2ZK4kYqZXifG1pjkbZLl0Kt6nmZQ91nf+LxYubJaTVIPqBby3Wb5GDWS6oEJEREvQfVpI/JBJKre0WMABcs8g/9SlvUXvJMPW3EEkcEG6+azI0tYG D4ejFaHV /adPPxN5SmeHsNAEI/5tg11bH5hPHa5C9+7R9aQHvM2Sq5ic5gX15PjYjbETx+Tf0FXHm3zoun6sl59Yd7q45fcD+TAW8E3KfLmCztZKVaF3GOv/F3eKQKtNxa2kmnvLEaoxE7qTw8c/1jqkOtVsQqpzQiMVwWLqjLIB/dDZAd4sc8nRdncH+SbDcEAwewJVtOk3zLA4UYWgJm/T4/4leKbR/vMhDcN0qglaYUOUa98WrM3TQ2ZQmA7/gE9VC+2ZEDpqoooTkg5rF5C3UJV9SWF0a7auzBz5Nzb6771DJ6p/uwpihJJSG4ZCjImGRGKDv9N+P/Fw8LcBIWU5dk03susYx/JN/dPlmGkS6qSRJ/FnkR2ecw61cmlXNXg== 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 Signed-off-by: Michael Roth --- .../virt/kvm/x86/amd-memory-encryption.rst | 24 ++++ arch/x86/kvm/svm/sev.c | 121 +++++++++++++++++- arch/x86/kvm/svm/svm.h | 1 + include/uapi/linux/kvm.h | 10 ++ 4 files changed, 153 insertions(+), 3 deletions(-) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documentation/virt/kvm/x86/amd-memory-encryption.rst index 2432213bd0ea..58971fc02a15 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 a8efe1f6bf77..097bb2138360 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; } @@ -2011,6 +2019,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; @@ -2101,6 +2183,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; @@ -2292,6 +2377,28 @@ 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); + ret = sev_do_cmd(SEV_CMD_SNP_DECOMMISSION, &data, NULL); + if (WARN_ONCE(ret, "failed to release guest context")) + return ret; + + /* 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; @@ -2333,7 +2440,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 56a5c96d8a36..740969b57425 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -92,6 +92,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 499cc323f793..cf19799ca5ce 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1919,6 +1919,7 @@ enum sev_cmd_id { /* SNP specific commands */ KVM_SEV_SNP_INIT, + KVM_SEV_SNP_LAUNCH_START, KVM_SEV_NR_MAX, }; @@ -2026,6 +2027,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 Feb 20 18:38: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: 13146910 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 1A66CC6379F for ; Mon, 20 Feb 2023 18:53:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B4A1D6B0074; Mon, 20 Feb 2023 13:53:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AFA356B0075; Mon, 20 Feb 2023 13:53:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 973AC6B0078; Mon, 20 Feb 2023 13:53:26 -0500 (EST) 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 854C76B0074 for ; Mon, 20 Feb 2023 13:53:26 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 5284C160419 for ; Mon, 20 Feb 2023 18:53:26 +0000 (UTC) X-FDA: 80488568412.06.1B117A5 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2067.outbound.protection.outlook.com [40.107.100.67]) by imf14.hostedemail.com (Postfix) with ESMTP id 4EBDD100002 for ; Mon, 20 Feb 2023 18:53:23 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=kHHFPQ+i; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf14.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.100.67 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=1676919203; 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=gMVr3aU9Xdh5TboWF18psQEDEvA2TIg/npTEcVgWI7E=; b=NpGG7/uxj3mFgiIS9xIwZi7903iiuLWMtpQdXRXxBY6+I3Rsrk+waU8ystRkjMzx2vhMXf ygs+zhRlKklPdO+e4vm3VT/qvacyzBKUT1GTp2ECnQx11j3UyGq+M7/2VueNOTP5PovuPu t7MD5hvk98C5YcXnBN8jI7J6yN3a4JE= ARC-Authentication-Results: i=2; imf14.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=kHHFPQ+i; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf14.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.100.67 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=1676919203; a=rsa-sha256; cv=pass; b=TyfmjKkDZ9QGBjNdAefLiF+zvU36QR9xvUqxenrHch1GRo5BBNEvACYfKIi1UdgRVq/ux7 /TFNMMrjbQv4r9M7kklySkSSPinn4EX2tbNXj1HkQTkKPgzJRekDfqQjxmw1YWvLQVToV4 MhT7Q/JsqDnudpWbs4Ra8ymabMoQSOE= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=h/YL6USh9J39sKixLOvv0qQPkIrpUsXsOId/+75hrZh2fVtwcmMycieZ55P9BT3caojB+VUwq6wczOIxkXtKGnvmZeQCByfuLu+cHF1imjPQ3r4CXCYFj6TMbqZ54/pVDvXZiV89U2/7JqCXyrD74ox3HsXoNDmak0wz+FEwAqijgspAzEXM5JmO5Hn7ozSAvQ/90IrSaaDI8counXB0Ff7GTlGEuZ8HGgjUi+LwnVml17HPtPX3GukA25DathinkomP8E3BvOuLLiCTJNHz2v4iTT2p9yFqqqbykapwu9UbNVQPJ5TqxWPHsQQSsruwNgx5MI4WGi7TfgIGSs9rpA== 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=gMVr3aU9Xdh5TboWF18psQEDEvA2TIg/npTEcVgWI7E=; b=F+fD/6TZmwOCMN0H5YajHa8xTcM9Ayydt31usm+xRVUtLFHPAxJgrPO794NBRN1vMpF9CTn5stgFIP6C3bfst1ZTaJcGr5OE3rjPg0mgALDjdJUxp5F50Eg0lepbHX3zlwOlQkK4r/r0wqRwj6Gq6+PJ+P5BCtdYP5InOMhicSaBzCG9JSjoS0ACBONlRCKAP7mDe422N9RL7A3f+N/gV1QQ3QrzbjhFz2GrxdfpE+/t3+WFUyHXBhp/5Id4T2gcVFAjTCmpxf2U1BG9i5wvJgkF3jLwt4LVkCtFxaVMnOg7s53Eah9cd0eHpUVWGV/rAfrf9Thbr94QefZRruWb8A== 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=gMVr3aU9Xdh5TboWF18psQEDEvA2TIg/npTEcVgWI7E=; b=kHHFPQ+iVISMOwb8ZyXG3tXGYhzuVMVOZJ01qwfYm20lyE3kYsK3DkjNzsVH1uhiBCMUZTxAz/SJxLVC1C68KprXbp5J14+nx9z6tVX+uXYsHCUvalpSfOGTDovb+8gSC9tHLh5o/axbuq0spy5bo+vuMAv+KQrwzUVySXII1Ws= Received: from DM6PR10CA0004.namprd10.prod.outlook.com (2603:10b6:5:60::17) by DS0PR12MB6583.namprd12.prod.outlook.com (2603:10b6:8:d1::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.18; Mon, 20 Feb 2023 18:53:20 +0000 Received: from DM6NAM11FT046.eop-nam11.prod.protection.outlook.com (2603:10b6:5:60:cafe::7b) by DM6PR10CA0004.outlook.office365.com (2603:10b6:5:60::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:53: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 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.6111.18 via Frontend Transport; Mon, 20 Feb 2023 18:53: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.2375.34; Mon, 20 Feb 2023 12:53:19 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 35/56] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_UPDATE command Date: Mon, 20 Feb 2023 12:38:26 -0600 Message-ID: <20230220183847.59159-36-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT046:EE_|DS0PR12MB6583:EE_ X-MS-Office365-Filtering-Correlation-Id: f4e21b02-a376-44ea-cfc7-08db1373bc55 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: uX02knEIaCBltUtmvBE1z4/RIHkuAKEdLA6EdC8FgBY4o1xOnsQ4iQY/r+y23+E0FlhVNkUHd99vdz8HhSjQMy5ZVJ8N1JH2s2dXUEenVG4B0B3nKy56amODoChXWL0lgDZ5w6mUbxrpl63LhvOfL+aayyV/sg0KSDfenpL2zdXwSVzm/T1e5hPnOLW3uZvCcCy15NUiqegbh6PfQBtEdF0XWbcELz4NccFDLzIqTh7SSSej5BlxLnkRYL+szdRR9qrrlCciY1xQgM05SG6Ma65nyiTd/7wlbKn0g9/BicoSYYtjdo05kVjIfZrjvf6ucXMtdt6GtRq7hRoEr1ClrvHKvHT1Y+uSeLGTkZOtDPCSUQ5EsEKbjJFSw6GGQX5AN+v0wS3PduKJzEbzxJm9hg4zrwLKjd5pDYzpzjplalbI1VbZ/iBG0QwalbZ3I1cuR77/vwreWWMCLKEbe+SB0wR3DNLSe0KfIjdo7SMugFDeGApis2lS6SudcJBcRQR7C7WbbmQ7QbiUeUFpe63enw3DdMTbWtRgb5kY/F6RCnvxWQz403E1yScBjVpNrHZB6p/gA0L25RiF0QcTu7LybmtYCveXB3qoc2HrsQJ1T8dL9Q+W5RMJuFN8oEzPrCfI4o4kCsFhVeUAqVnG/ynovC88++IiUKprt6kFWrXZAezVMZspyg4H1Vu6/TYwTSqeVFxukQNEuFkonXIjMyDB3akIlWN3RzbBaS4WVJiFQsQ= 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:(13230025)(4636009)(346002)(376002)(136003)(39860400002)(396003)(451199018)(40470700004)(46966006)(36840700001)(7416002)(44832011)(5660300002)(8936002)(316002)(7406005)(2906002)(81166007)(54906003)(70586007)(82310400005)(4326008)(6916009)(8676002)(41300700001)(70206006)(86362001)(478600001)(82740400003)(16526019)(36756003)(26005)(36860700001)(186003)(1076003)(2616005)(47076005)(426003)(336012)(40480700001)(40460700003)(83380400001)(6666004)(356005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:53:20.4183 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f4e21b02-a376-44ea-cfc7-08db1373bc55 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: DS0PR12MB6583 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 4EBDD100002 X-Stat-Signature: p1nox1pm4ychiccmz9uyxak3kr1dgeyf X-HE-Tag: 1676919203-739551 X-HE-Meta: U2FsdGVkX191Z9ScaQ4eh9Kzb3Ko5XJkXI2GfRG+l7zCC0MgR7b7nLomXlFGSL8I3E5svcYiA2op1lhWLFAGAtTFDR52OE7KGvk2XyRTrUkrqwzRN31n7xX63T3N828VMBr5EluzhI4VZ3WAbJUv/DAkcNGiULDuuPuYYNANze/tMiedFPxj7b/kLmZikqHoeXhv8sG3dXxklWXgdCp3BeSi7pAotYssEWiTwFxr7a1qQyb/sVcBlk11XXS8/SJQO0VU8JuMWyzdhRK88dsxGSCv8H8ksNxns0o3pTZnbvGunv4IOtxTroPvCzKnCcyYnHJHWVQGWm5ZNKoTIBg1dXWNSX9CQ3R5/d57K+CmOm1B1BGhlxlSAcudRQxMtaFD8v0KpAleX8DiqFnAMrDcVBDTHaMY0OUqxRb4AQ/emaHoQ+dDH8CcU4KL/2fUjm1qBftSome1rovZSatW+K9WYbWnrbEqCq8QgCO97DvRDR/nyguTSdDvoxESp8WyDkEHTv/1hpG1wpj3E7gYMX5hKhVvNJijnEIK1Zx2XcPaqSOaYGWdej/BnvGcc2pYbQzM6Zo5MsWTcUPfsEksIs8QuONoxymP4ld/5YoUflXXE3S8bt2X/C437EOzgKxWNYzA12vem6K7T3yXnOZ7xIedwcQKPZ0gefOQntu2XnUhivcSU7K8L0AtZKMIpKY9X7u6hsdDj/YoO4eFONw2iphgJ2Q4sitHCFwxeOZ8jvPCfLv4wNt+VysXnvqwD7xrCmKvWnFdDcISU+NXr+gcQWj4j1nf8fU4OW0Hy2QUjwtdGdTg0hI3BX2pIIHpCurV16hmFbhl+gTx3Dn7zssdmMUEKAQjrQnmB9oJnr7SkIr7wJ2Gm7PMRuS44WajwAnthhGXKIIEnd+H5g1NNeaMaNg2uK5/wyyW9EyR3XwjOAlWdkJwtHTZgJ2jFsxugPtZxwN0FO/K7QQOeQKp2Wdielo xNPW852r +7q/TFlTXNhjVUZoxdAIx5IZBjosZqfqZO/VnMmywNAgwuccVy7+vLaJzaA5a2Hu6Ju6UB+/Me77wW3smQz4qiwjBt71SHv5bAfLa3JSybxVk6RzCIzXyZ2Wtgo1DW7CT/mYH7iPSVVfU0plfkfBR20L0JShAXSdLJc5C6487RDH+hUHKxoHYberSw+2gmsG2KwhWWUuG/fSPDINPInjkXI15SNrJJQiueblWiFSf8uKOnusGRG54spj4RgqsigS7fqX/cI9eqm9VS6V6ChH5hrUr/do26LrZPpXA2semDczTqBv91cGpa/D7j9it0BDwa+v8EUvSmdb5rOrKnI+I0hKvobgrNHA3EcmfZWsNnz0RDO1ruZ76s+w3Kw== 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 | 29 +++ arch/x86/kvm/svm/sev.c | 190 ++++++++++++++++++ include/uapi/linux/kvm.h | 19 ++ 3 files changed, 238 insertions(+) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documentation/virt/kvm/x86/amd-memory-encryption.rst index 58971fc02a15..c94be8e6d657 100644 --- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst @@ -485,6 +485,35 @@ 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 097bb2138360..03dd227f6090 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -234,6 +234,37 @@ 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_mark_pages_offline(pfn, + page_level_size(PG_LEVEL_4K) >> PAGE_SHIFT); + } + + 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_mark_pages_offline(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; @@ -2093,6 +2124,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; + void *kvaddr; + + ret = kvm_restrictedmem_get_pfn(memslot, gfn, &pfns[i], &order); + if (ret) + goto e_release; + + n++; + ret = snp_lookup_rmpentry((u64)pfns[i], &level); + if (ret) { + pr_err("Failed to ensure GFN 0x%llx is in initial shared state, ret: %d\n", + gfn, ret); + 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; @@ -2186,6 +2373,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 cf19799ca5ce..4098bba17aa4 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1920,6 +1920,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, }; @@ -2036,6 +2037,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 Feb 20 18:38: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: 13146911 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 0A04DC27C7C for ; Mon, 20 Feb 2023 18:53:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A3A436B0073; Mon, 20 Feb 2023 13:53:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9E8206B0074; Mon, 20 Feb 2023 13:53:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8893E6B0075; Mon, 20 Feb 2023 13:53:50 -0500 (EST) 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 7B8E56B0073 for ; Mon, 20 Feb 2023 13:53:50 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 4FED940222 for ; Mon, 20 Feb 2023 18:53:50 +0000 (UTC) X-FDA: 80488569420.19.893ECBB Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2079.outbound.protection.outlook.com [40.107.94.79]) by imf07.hostedemail.com (Postfix) with ESMTP id 3B94340011 for ; Mon, 20 Feb 2023 18:53:47 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=S0Ap34RO; spf=pass (imf07.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.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=1676919227; 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=fQ5XEYYKQSPDy3XEDppFDt/H106z3yOc84xd6pNrjdc=; b=GFKVQIz+a88tAv9MsEFCy9EgL3i1OZcwiwBp5TBmVpBf11tUVDY8UnYYp7PlXh47d9irjg jQcLtkskKdpR9wckLnJfzWW4JZS7n2BricktcpjR9z8OUzQ/QB+aXXsAAMdYi34/sSJqvI 1aMDGGt2tRNGbl3+tduesl4ldr5NU1Q= ARC-Authentication-Results: i=2; imf07.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=S0Ap34RO; spf=pass (imf07.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.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=1676919227; a=rsa-sha256; cv=pass; b=MTk3kqkT85QxLTLruP9hb+7SKst+9SIvBVBUOnPzM7x7R3LWLsPr4zjonf3pMRnWaMQa3i 8Wy41ONE2C/iUoBOwn16UlBnV+81MTcl2iiy6lUGcgmXZzP5gbYD86zVJGZuBj7CfJc+R3 mlKOQWk9VYPjwztZ/OxrQRsJqBxfUiI= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Gmt6ZUhs/YZgwYwcMlaO0KGTIL8lIymTI/4kjByjZhd4yLJpnHCa3NeRx3RH/IqMEui9z/5db75YPQsHg6wdhsWZ/Ro1Vq8rd5HSF5Sqma2AXEn2PZwHZUg0KUD2jlB7qoIDCgFwlei03vUMi1CALnjHmL5+D5yPZqeFNokjThie06mBU4k2QicIcPhmweEh5AjTDDHSLjLSCIbx/qGghl9J5QYVepDlv7dCISo7hk5cnJkvChP4hgbCFT2Pycpupkv9s1jPPobXrMH1cZWxlpHr52Zw4yge7a5xgvebX5JKwYQ3pVfFEsQNZ90ZhBv6OeErPcEwhdSOXYmJ7Sf19Q== 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=fQ5XEYYKQSPDy3XEDppFDt/H106z3yOc84xd6pNrjdc=; b=NhEd+7XSPpuz850oMnk+BPUzgvkkRDSqUfAOSNVsJQNPiXpYVyt5hIoOVup7Mm/sX1m5n2zCMsFPKYeezrLathbLxaBPZVXoJq3xgYynuOWniXYmlrkLsvHJabjfyCGjgPL4bn9fbKYeXCsDTUVOrEmmbvTYQDBtO3Ww0V7dX6MMsgxTkyV66IChgLeefxMfvcFMldJy8a+q2/SLAH8dvXU2PJ4gzXkzyq7TqCWkaLkmBTRJlHEnna14N76YPOIJiCL3/lmRZ1/W+N4MupVTS4jfbCyaBrdBuNOseWnIa4bE1I+Qg16c1QcIEK4ON7QhxXxb7cdqT8TMHY1xCij4dA== 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=fQ5XEYYKQSPDy3XEDppFDt/H106z3yOc84xd6pNrjdc=; b=S0Ap34RODGLjJ53/WOs1ZLXWcni2RUMULQA5c3QIdgAlvs/xYOpF865IJipCGYonvDEqm/NAutazqhyhcgNfVXPfFKALViCW4xICsSXl+NnTpQ7NHlhtx/veXHA2yAueuq9orDXKDLHIF/1OCHKmSlOcU2uYPIDHkY1hmkPkcUg= Received: from DS7PR05CA0041.namprd05.prod.outlook.com (2603:10b6:8:2f::8) by MW4PR12MB7335.namprd12.prod.outlook.com (2603:10b6:303:22b::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19; Mon, 20 Feb 2023 18:53:41 +0000 Received: from DM6NAM11FT034.eop-nam11.prod.protection.outlook.com (2603:10b6:8:2f:cafe::2f) 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.6134.16 via Frontend Transport; Mon, 20 Feb 2023 18:53: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 DM6NAM11FT034.mail.protection.outlook.com (10.13.173.47) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.19 via Frontend Transport; Mon, 20 Feb 2023 18:53: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.2375.34; Mon, 20 Feb 2023 12:53:40 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , "Harald Hoyer" Subject: [PATCH RFC v8 36/56] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_FINISH command Date: Mon, 20 Feb 2023 12:38:27 -0600 Message-ID: <20230220183847.59159-37-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT034:EE_|MW4PR12MB7335:EE_ X-MS-Office365-Filtering-Correlation-Id: 8871b370-24dd-47a4-576d-08db1373c8c6 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 8rjVIeCUfp5eh/IwrO15JOeXx514jGtlcyE8q7hCqKx+dyznbHXisi3Fea10k6N4lpyHjvkA04rrb2bffpG6BFuXs1MNU8228ppLvxybyFAQ5hdxy2T4eccgfGtq0BKM7AcqtsQz4IYpyiyC+gIz10mkWyzffAXqooVnmf/zLHiH4Gx5RcW7tC2bQZNqdD3zhmWKj+7z3T9KTjMhoOJtROpS8CEKBqEXFYRn9iCfjhGPOOwX5jR6pzJm8HB/dzqW2FyZ8vtSOhKr+cvDuTEWvagkM2DCeQpmF9cLDyg1UqJSK1hHnt5UIlfX6AnVBD5ENkFS+RMWpMVH9fEHZju095FPf9sgrECmqksIJY3M3UB7EcCTlMSxhgNy1GJNvUTjDU2JIF0PVNeu3mgWGd8IsvVUja3kUp6BP4pHc6erW730GPR9fgmtVtYDS/k4+IQ4GkWNZZBOat7Arb0ozdT7qHhrJEsWl7Sa9K42qd6mZPCTtxIpN9MQ8XT/gdY7XkLPVNk99fYErtNlTscOSCX7gSH83rQrzuvB0m8/kUqGF6QE4k1kq/08Zp6bE3yZpVJAvJunZZwRMJvblNQYHP9iZ9dPMcURJtzi04ATF4H0yy3jxqPeN+uA4On2SHJEZqTajFsUbeRq0zpR8j/9TP9ojbTgrxWthjlgHQpRAtsyRlCh9iXSawt0Cs+aT4/ZfWA/2e+TJRcdEHYehLM865I4w2cFCnC1/DQkof9Cd3mOEeQ= 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:(13230025)(4636009)(39860400002)(396003)(346002)(376002)(136003)(451199018)(40470700004)(46966006)(36840700001)(44832011)(36860700001)(2906002)(86362001)(82740400003)(81166007)(40460700003)(336012)(26005)(47076005)(16526019)(478600001)(186003)(426003)(82310400005)(36756003)(40480700001)(356005)(70586007)(83380400001)(70206006)(1076003)(8936002)(54906003)(2616005)(6916009)(4326008)(41300700001)(6666004)(8676002)(7416002)(5660300002)(316002)(7406005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:53:41.2884 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 8871b370-24dd-47a4-576d-08db1373c8c6 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: DM6NAM11FT034.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB7335 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 3B94340011 X-Stat-Signature: m4daqhx9tt8ow3wwosufky7akxegugn8 X-HE-Tag: 1676919227-250230 X-HE-Meta: U2FsdGVkX1/qEzzLTuZFbJjGMGrY9usZhx/EPdQzESwSPeQp1+U3rHkSalnzksv+VLHHKcRjMK8E6EAFdKnt5IEywOzCK11z6spL96ZsmdA2wHR6NajcaNoLTdrkzxcCnEWjMEjsB+rUDBWF3bz+G00LxaZ7g5Cb5CgglQHHmUJCjZ61ZhncO+nuvoDKTKGhJOaf/+8j4c5b5SorzkoJwmA7T4Ql+7pLVIxGAVRV+7WRlhtynTpo8MRC7JKSoZ8414gw184EMsyCPeliBo4jnIfpdfzK82iSbTUyevxKYMStB2XpXfO4OY3jreop5qJvgcMJa2G6z9jL09GAVuu0CXEp1yN9Ldanf2/UXaZHqAsJBJY4djnSjSj/b+EGpLbC1GYr3RQK3sws/djw520lrSAgU1VhIuS0cUFIycAmdbYJ10gftDeIH9KBAIKf52gzfqVE17Emo8uLNyZFzp2t7oi3v4LKp/a6z8dnXLNmZrWtRNwVnhILlhGbTtWo5U7dK+FnxVeFfGUaYLcaUUjpQ/ElcTKCdKa8kF7sjxNC0uHbDgOSZxWxGFZj6obKtUTmq0YHWfH5e0AqtmDmZ9XdDjfkLf2TjqN//DZSotdn/uqmyf04VRIQeZ7T0onRMP4XIKDerK5CjwwZjIiLObnTJCgh/IyHoIbvy46DK8vxao5mTEeq5oHh+iedxkapHm4PYvEq0DCvS+UAiyvpnCV1OCrlY2G4gzn4GewWx1XSTx3JJXS3TPXTQkeolFmV+GOLtYSLHKVKSMaQnAu3QVwd2PJxXOnxq9pzSaEXb+1mAtOlnWsbUrw1IE2b5olCncgxAuK7Bz6eST2jW0wVQbvGn9V0rwR/VrnOf/suQDHFQeij93qrnxYeFp6gMUizjvOCXumr370GbAId4FFYuHfafG57pBM0x0XRD5996Xa8o+U1kXK4YthZq5pWqw/wfDqxiglVF6gf4pllsRehnJD iAbwkvsb FHcTAEo63yBax+xmH7x7Cefb3PZmlPoUw11LLCfTYpKYr0ZGQLpsjVSPiV2IG8WO0J1jqgPhECsyDrr7SlENUkRfMnc+263gx7PtVW6Z4NnfMfAGld1O3a8POS7abiUPpmt+oDFa7vVwW7+LzSupnr1vvWHq3DQqE4K2YyWRP1x85VgkOboa+b9lxG5eZ53cjaM7xoxlMCDd7UqBmnVNgSuJJStrak8acRrz5IvSVBGKzCo+tHuKYPHAZlqgFmpRj3LJDQvE31QmFhUKe9bQB9HtQfDgVNSXarytLoahdbCGPA9uswam9zYnPqQ2QRinuju0CbJklat/ki4+ChxqcXTcPDxwylbHtfIMI5mBekRez3KmMprcc7yss8A1Xrg4bSlpvPFguq4vLEq1ip4aOv7rbDC8ubI0JNJ17mxQCDfmYQvGiHGWWfIFDhg== 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 Signed-off-by: Michael Roth --- .../virt/kvm/x86/amd-memory-encryption.rst | 23 ++++ arch/x86/kvm/svm/sev.c | 122 ++++++++++++++++++ include/uapi/linux/kvm.h | 14 ++ 3 files changed, 159 insertions(+) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documentation/virt/kvm/x86/amd-memory-encryption.rst index c94be8e6d657..dafb0c9984f1 100644 --- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst @@ -513,6 +513,29 @@ 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 03dd227f6090..515e22d0dc30 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2280,6 +2280,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, -1, 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; @@ -2376,6 +2479,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; @@ -2831,11 +2937,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 4098bba17aa4..2bab08a5b5d7 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1921,6 +1921,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, }; @@ -2055,6 +2056,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 Feb 20 18:38: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: 13146912 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 324FAC27C7C for ; Mon, 20 Feb 2023 18:54:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C33F66B0074; Mon, 20 Feb 2023 13:54:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BE4596B0075; Mon, 20 Feb 2023 13:54:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A5E0C6B0078; Mon, 20 Feb 2023 13:54:08 -0500 (EST) 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 983E16B0074 for ; Mon, 20 Feb 2023 13:54:08 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 75588140430 for ; Mon, 20 Feb 2023 18:54:08 +0000 (UTC) X-FDA: 80488570176.27.CD8A01C Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by imf04.hostedemail.com (Postfix) with ESMTP id 6437840021 for ; Mon, 20 Feb 2023 18:54:05 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=ETy3Pzi0; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf04.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.41 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=1676919245; 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=kokLMy8NhHv0IiJAtB8w/Wv0Gpgl+gURTTSI+M31Zk8=; b=iIEmTd9/E3Bj3IMdpIziCSpyiqUK3xG1S+Di0m4oQyi+ktBzR+EOnDPDQ2SIJOJKFJL3Xf CEtpMefMZovMdtfkAg7oGvLZsjwgi9yJjo5pTjRf7jfWFOip63XJBpgtGULsBaZ0rmCakR kmVi4BLfDqncC8vU6/nYA+9FwC6jFbM= ARC-Authentication-Results: i=2; imf04.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=ETy3Pzi0; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf04.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.41 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=1676919245; a=rsa-sha256; cv=pass; b=ySK8EwgsGstCvsYAP7d8BDHjHdhrNNiCRii2/5caQAlZHdKxM3IZcAmiy1zUQrC4r8P1WN hFdBc2WdIYMRq7/n7ZbKayk2CGsjskZznX6Xg4Bt+TD5dUQi9WZ3HypDrne7CdLdBP/0op mMiTAIH7rgPE3os0MGnsjSW7dmyBrA0= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bT0YmtVe0ahmPKj5F2BPGB+AeBwB2/Qd7XQStEuNCfEyx5tCi05qxQmZEvjhuASQwkYI48Jjlc3Tq6cvsL0sFF6u6weCogX7KSA1Lo2w3bECDpJTMrHVj2jzMWjE+McYnqbjwHK/VFo70FgBtPe1tGvrIjJPJ0NPJKMGdw10U4FR/JQtM+DCsjJvLCf765K89HgwXiUGNUODidIyQyIzP+DWyx3/Zri0PHQ+NdiK1RIh1ghmjL5v+3mT4qhGL6Xqkul+hUxdaoRTqqlcRQ3eguEBXh35BXVz3ppWD96nhe0JGVFTYkSajIrTYxK7XXdYaIw9DqgDeWJ1JOkQJLKZDw== 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=kokLMy8NhHv0IiJAtB8w/Wv0Gpgl+gURTTSI+M31Zk8=; b=dh/Px4JAOiHHHoo9dxGQG38OnAByH+qTXM9OUmAiErE3l0FGOm2odo79zpAoH4Ms4rSEA87uwkc+wOjfa9r3oinaGcR9kSvnw77qI1QilhQob9z/1nJlcIeW79t5l8wlNaPFs+IMFGNn3A8biBH5hFKO+4Xz7/RETXfbNmyAwGUl4H7o8qAvSN0rWyV+3628z3ow+u7a/0izl7Pf0f4Z2FyBu2NQaZxhZEprm/Hv250iji7yxtZsX2GTcJDBzZR2BQM7gLnglqQXH70DZGI4XT6SqnJaCejRs60H3y+CKlWgx0U4trUM+WsKzqyTfyKLjEj0XaLElnyMkGlcFN7z+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=kokLMy8NhHv0IiJAtB8w/Wv0Gpgl+gURTTSI+M31Zk8=; b=ETy3Pzi0XuQwsZguKeSHOwKBY6Is7l5+tzpSsnog26BuRPadZzmfDTBYwg6ZQ8oGZy/Cgep6X2q/YEKUH2YnYFLA/XNUoZXou7EXwzhl2S7HHxnKrmzJiKh6E2sm6DYsU5tpoZ/7NtyX9qYg6BHHE5tpoFv+/Y97QAsI+IN+T3Y= Received: from DM5PR07CA0117.namprd07.prod.outlook.com (2603:10b6:4:ae::46) by IA0PR12MB8748.namprd12.prod.outlook.com (2603:10b6:208:482::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19; Mon, 20 Feb 2023 18:54:02 +0000 Received: from DM6NAM11FT012.eop-nam11.prod.protection.outlook.com (2603:10b6:4:ae:cafe::d5) by DM5PR07CA0117.outlook.office365.com (2603:10b6:4:ae::46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:54: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 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.6111.19 via Frontend Transport; Mon, 20 Feb 2023 18:54: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.2375.34; Mon, 20 Feb 2023 12:54:01 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , "Jarkko Sakkinen" , Ashish Kalra Subject: [PATCH RFC v8 37/56] KVM: X86: Keep the NPT and RMP page level in sync Date: Mon, 20 Feb 2023 12:38:28 -0600 Message-ID: <20230220183847.59159-38-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT012:EE_|IA0PR12MB8748:EE_ X-MS-Office365-Filtering-Correlation-Id: 17e376b1-4bdf-44b5-9e12-08db1373d557 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 8KwLgoSvn+rq0RN8eAFGfO8DJrkAUHiQkSI9pezqXNlTVPl/BgcroD7KTdadz4J7Ouj2C7bw571dIg7a4oxFx0zbJnPuGThqQvL+k5sgyduuMwJEz5rn90o0aqHT/pIOrandIOfqud5o6V1m7O1cGm/hY7Pr8EYRs2SdvueW0UGzXruiWrXAlydKI8lllAlCF6TiBQxOmBlwbd3XdoWoZ71FB64FBVIgmpiPPxIaMb+fRa5DwTjLlPu1w1kDcJSI3pYiGjfELNie4aG5cWbw/G6M1u6hw43w/7oPUQmMWUEZ0yLeUeUHNuZXndU6MQbv6JgGbgkzR7WLf8a1ZGvx7ENOWTt/Xsmm5bkG3YtSqmxY4hkv1jxOwQUl4QZ3H/+fTRedSbQYRWPVif4s2u0EpKJ2ooHwb/IzPxPlGN2rEZ8g1SPXZtDajC3/CH3Lkvj79hjRCxZ7kFf3B+y7nxpBmPxPgn9UYqGUWg1rQVNlOZIMGWaJ9qn02rAfuVbPK2eE1vn1Jzut18EUdjZAuVoINegxVqh2kIXNBYGorR5KscSYa52gwKObTqanX4bdEXWdsxueXgNJbW0s/GmSKotrRaq3ZZ8r+lzkbFKkbnJUtM6xJCnqFEq/9ZBeU60te9xy7XvjJOPp9GOI1KoRYMauHxOeIV199ytL0xcStNJJ5q4VCZCj2kK+dBG5LL9iyjoRAXp+7FNEPe1C+7YuUFwvVcfFzz+OmcfA6G/aXRu/71Q= 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:(13230025)(4636009)(136003)(396003)(376002)(346002)(39860400002)(451199018)(46966006)(36840700001)(40470700004)(82740400003)(36860700001)(5660300002)(44832011)(7416002)(7406005)(86362001)(82310400005)(2906002)(81166007)(40480700001)(356005)(336012)(36756003)(478600001)(426003)(26005)(186003)(47076005)(16526019)(70206006)(70586007)(54906003)(83380400001)(40460700003)(316002)(6666004)(6916009)(2616005)(1076003)(8676002)(41300700001)(4326008)(8936002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:54:02.3731 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 17e376b1-4bdf-44b5-9e12-08db1373d557 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: IA0PR12MB8748 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 6437840021 X-Stat-Signature: gz945g9jgz39hnw167mhm9bjas9ue6k3 X-HE-Tag: 1676919245-834821 X-HE-Meta: U2FsdGVkX19ni7/tzW6rWFfroh4/CP5Pqdahkp7kJ4wrxtoEhq/JGWG4X3C4aBRj0kD6DgJJdR2/U4CMz7Xu3QT8RBMcylEkpTA859lBrUOS+QpwTxI4VuNcgjLp7AKIbvEcquVFVaOTtTTbIRr0t0/4HOU5GyqSu93e95vWHfCBcHtveDoe3J+TToKdfN1PlSSm8YZ/ZgSbNgmpwn4pPcPbpsJwb2I0qCzs7Q61Sq+bDd6YAS0aQaG0ijeKveRUf7y9T4JCYM38CqGQ/gqYOevOcbsBYiM5430wAHkmHju3Zk/eFtVT3gG5cmip16eco4+5+ikchRcvxuAU4saV2KHIBsISh0gweUhUsPC/cI1B7yh/AGebQO7Ynhch/t5aQh/9C7V7xPV7F+t9fC670uxDTOx26PTIB8ai26MXMmivQ2CuNWJ6mwlMIMxCKhZIjVgmOD1eYN1+0uciii6kU2OmU5jwZyK5mqHyFLybyZB7wOuSiGEFmNNAyBMXBWJdFPYP1Hb3YgOV7KHLTGdnIiFOdZkxqiQ2JN4huxMwn7XTUd9yXFOBWaFp/WBvIS1PFqsXvPPs4w5EvpZHCMrhNvsiYRMyU9RVk35tkC7uleGiSEKPFVw99OtMoOYB5XWyRLwTn5mL8pO++S9BHOTduoGpvb62UA5vsn+70p9jQZj21vTiMUexRHEO8OCjJ1E5i6LHEFLRNdh8msMHmLW3m2CE8YU4fWgDvLpcYFChXokM9ayeJawDHKzREyFX834Q7peUzGABN2N2kSZlshAKXixst+fJRs+y1wqPrAobGRlEpALBOOsvHdAvLq3pZ0ae7mMzT4cKpwOaMZv8pPtN03Zel4JLKgpHQm+qZ1YKjDI41pzPB07D47Gm0HNCiJmH1Sfm0KUXGYE6QawWSd9CJAzBRrbkZkjBXJ/dKFrWGrgsiM4O6AOM+Lq/n19V+lw5nXc9gZNsr4uXWWfG4Zf av210mvR rCm60RMiVoUxcY47XJCuoGRfDIPd5BYvivXrWdh28XB0CzSx40tKknpgGOLJRr9i6YM7IfFzzWZxjtLb3M/lXRNl6FzSjARYWvum75kKNJAyjJkjQevm49gkfKhYBONihS1TqKrGYFw4uYjukRmAsC3slTEn8Z4B1tflhtiAF+DrTb2OgntE36erzUG9Vumc6lmve5GeDD9MpMGTdFXuj/3Dvops8He5bh/wZtS6yxWqqahMeaVA93WwVGruOLQ4nxy0Z4OC7nbOmk+0SxstWNcgL/MFwUxKnTUocSWE8lQqy5c9EGNKHkf9MWamrMlWHCOdH8ImLjIyngfDsvN5vKUONeOuFp1zeNw9sX4G4SPy7hjZM/729zZUnicc/pbc7FdoUSbdw/BcgIG8= 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 running an SEV-SNP VM, the sPA used to index the RMP entry is obtained through the NPT translation (gva->gpa->spa). The NPT page level is checked against the page level programmed in the RMP entry. If the page level does not match, then it will cause a nested page fault with the RMP bit set to indicate the RMP violation. Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Brijesh Singh Signed-off-by: Jarkko Sakkinen Signed-off-by: Ashish Kalra --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 9 ++++++ arch/x86/kvm/svm/sev.c | 51 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 2 ++ arch/x86/kvm/svm/svm.h | 1 + 6 files changed, 66 insertions(+) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index e116405cbb5f..87a087ec3277 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_OPTIONAL(alloc_apic_backing_page) KVM_X86_OP_OPTIONAL_RET0(fault_is_private); KVM_X86_OP_OPTIONAL_RET0(update_mem_attr) KVM_X86_OP_OPTIONAL(invalidate_restricted_mem) +KVM_X86_OP_OPTIONAL(adjust_mapping_level) #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 a9363a6f779d..456b42cb167b 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1731,6 +1731,8 @@ struct kvm_x86_ops { unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu); void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); + + void (*adjust_mapping_level)(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, int *level); }; struct kvm_x86_nested_ops { diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 360af0c9997e..d8e5254f314d 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3081,6 +3081,7 @@ static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn, out: local_irq_restore(flags); + return level; } @@ -3141,6 +3142,14 @@ void kvm_mmu_hugepage_adjust(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault fault->req_level = __kvm_mmu_max_mapping_level(vcpu->kvm, slot, fault->gfn, fault->max_level, fault->is_private); + if (kvm_slot_can_be_private(slot)) { + int req_level = fault->req_level; + + static_call_cond(kvm_x86_adjust_mapping_level)(vcpu->kvm, fault->gfn, fault->pfn, + &req_level); + fault->req_level = req_level; + } + if (fault->req_level == PG_LEVEL_4K || fault->huge_page_disallowed) return; diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 515e22d0dc30..e8740c35be39 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3749,3 +3749,54 @@ struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) return p; } + +static bool is_gfn_range_shared(struct kvm *kvm, gfn_t start, gfn_t end) +{ + while (start++ < end) + if (kvm_mem_is_private(kvm, start)) + return false; + + return true; +} + +void sev_adjust_mapping_level(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, int *level) +{ + int assigned; + int rmp_level = 1; + int level_orig = *level; + + if (!sev_snp_guest(kvm)) + return; + + /* If there's an error retrieving RMP entry, stick with 4K mappings */ + assigned = snp_lookup_rmpentry(pfn, &rmp_level); + if (unlikely(assigned < 0)) + goto out_adjust; + + if (!assigned) { + gfn_t huge_gfn; + + /* + * If all the pages are shared then no need to keep the RMP + * and NPT in sync. + */ + huge_gfn = gfn & ~(PTRS_PER_PMD - 1); + if (is_gfn_range_shared(kvm, huge_gfn, huge_gfn + PTRS_PER_PMD)) + goto out; + } + + /* + * The hardware installs 2MB TLB entries to access to 1GB pages, + * therefore allow NPT to use 1GB pages when pfn was added as 2MB + * in the RMP table. + */ + if (rmp_level == PG_LEVEL_2M && (*level == PG_LEVEL_1G)) + goto out; + +out_adjust: + /* Adjust the level to keep the NPT and RMP in sync */ + *level = min_t(size_t, *level, rmp_level); +out: + pr_debug("%s: GFN: 0x%llx, PFN: 0x%llx, level: %d, rmp_level: %d, level_orig: %d, assigned: %d\n", + __func__, gfn, pfn, *level, rmp_level, level_orig, assigned); +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 1061aaf66f0a..9eb750c8b04c 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4835,6 +4835,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, + + .adjust_mapping_level = sev_adjust_mapping_level, }; /* diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 740969b57425..cbd4594f1cca 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -706,6 +706,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 sev_adjust_mapping_level(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, int *level); /* vmenter.S */ From patchwork Mon Feb 20 18:38: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: 13146940 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 3C882C636D6 for ; Mon, 20 Feb 2023 18:54:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CA9F66B0075; Mon, 20 Feb 2023 13:54:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C59646B0078; Mon, 20 Feb 2023 13:54:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AD3896B007B; Mon, 20 Feb 2023 13:54:31 -0500 (EST) 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 9ED2F6B0075 for ; Mon, 20 Feb 2023 13:54:31 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 726351C5586 for ; Mon, 20 Feb 2023 18:54:31 +0000 (UTC) X-FDA: 80488571142.08.C428A20 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2078.outbound.protection.outlook.com [40.107.94.78]) by imf30.hostedemail.com (Postfix) with ESMTP id 669A280022 for ; Mon, 20 Feb 2023 18:54:28 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="HUL/97eO"; spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.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=1676919268; 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=Lm86L9ncTBUZoZWM3MdpTq8/bCwf/O3ttdk7LmSABfU=; b=crSPvJd2WwoyegXTREzM82XQtasEAi0osyXsicnDae5Fdydc9WLBQtgpjeOQGPBRXDzLt7 xRqBaGhxHMyg+iFZ8ADk1L4299vscL/RxNTnJYJUAwVgpB6NklTrvjC2S8xWrtxXbey4K6 EEK44rB8KNfI8snQYFuZAHnrDhWLZbk= ARC-Authentication-Results: i=2; imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="HUL/97eO"; spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.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=1676919268; a=rsa-sha256; cv=pass; b=r9zxh/WSF/egPDCSVfhuvBNIZ3QwCyxQjv4tEzWP3EVsGKOp7sFAEzy/FK0P4Y6fwW0ATv 1z0i6k5Z1Of5TiIXBCeShqXSBOz4OD1XCV9oKNBG7lU5I1AYcsGuScvAGmypMJYr4Txm4h dATl05/meRbDX7lkgbXHqsSgdZdR/28= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ghx3ehWB0js2VmZn44l8QVK0yA9+j02fLt8jafMYm639tJyLQe7aWQkbFkU3XMbB8V34lP1uyatkS55MRhrhaQvrdr//dsTO8jTUjiKjBHbe4dVJjoF7ApILe7BZEs8+BG4T0SJyimuaw/8tVbN/rzRqhrAiJBrN4h7l0BLT+pH8cBKCQC1jkmhR21po7u/q5pr9Z4io9HtO9M4Qsj+8cy8tyEcjuBTz2CYV9E0UG39sDpqntkF+Ryb7RPIu9+wGBksKBeuVNhCUlCmmWHGz7QhrZm0MqTqPe0f2HI05n5liaR9bu30AsQTTLkUmmglzlhas9YAMldI5GvvJlgtMbw== 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=Lm86L9ncTBUZoZWM3MdpTq8/bCwf/O3ttdk7LmSABfU=; b=C8frD0rVUfRPlVOo96bVIMrLQb7XZ4c7D44LbWFeWQ9aOFL6YsXGJspz0C6apMrlsN6ToJBOWHEOPJd2gaqbF2Em1LGIulYbxIcFgz9fkHm5iQaSHMwJnr5P7if5UreMYCY81XAO6/jGjnXSwStWcI4DtEt7G73A58JkDGg9UrdxHkjiskzDdlhsJucZ6vI+knzi+rLJ3hyvZFn1srf4LzIJYXSm/41B0m8p3oSAGUZaLPVJuNVVdrX/3l4CJ8qjaHnXn8L/m6hNV8Owm+rTOph3g75dpXahN+4Z8uXHEEWLlfeWEsAaHZ6dXYknbCVZpX9xRk1oVJnnjT4zVoPmGg== 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=Lm86L9ncTBUZoZWM3MdpTq8/bCwf/O3ttdk7LmSABfU=; b=HUL/97eOJp6I02r5BGkZCQjDAeZilxQ1rv4WAdKboU8ixWcYMnOPmJsXzC8xchSt05UDJHIEa3DbjJFWdhMrmdrW1QmPZ5E7XbgHcmhbx42l1Id/hwPLgPZL36aTggFtZZxpcZ2XpTVKP/qIIjEKPvTp2VQ4wUETHM1ZOPcGpeM= Received: from DM6PR02CA0085.namprd02.prod.outlook.com (2603:10b6:5:1f4::26) by SA1PR12MB8697.namprd12.prod.outlook.com (2603:10b6:806:385::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20; Mon, 20 Feb 2023 18:54:23 +0000 Received: from DM6NAM11FT077.eop-nam11.prod.protection.outlook.com (2603:10b6:5:1f4:cafe::36) by DM6PR02CA0085.outlook.office365.com (2603:10b6:5:1f4::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:54: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 DM6NAM11FT077.mail.protection.outlook.com (10.13.173.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.21 via Frontend Transport; Mon, 20 Feb 2023 18:54:23 +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.2375.34; Mon, 20 Feb 2023 12:54:22 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 38/56] KVM: x86: Define RMP page fault error bits for #NPF Date: Mon, 20 Feb 2023 12:38:29 -0600 Message-ID: <20230220183847.59159-39-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT077:EE_|SA1PR12MB8697:EE_ X-MS-Office365-Filtering-Correlation-Id: f66167d1-0096-492f-6c1b-08db1373e1c4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: kM6mydxn8sLFOK4VAscqKWOkdc6mbPhyJKPAuwm+LYGkfwKB9AdYQlnC8ZbXCuc0GkK7RmTUvIJc66QZ6rGj728hO+sKauU0PTd2ruGIBtzhPubxkJ+5iWlhIImvBpw98uIMhrlXrgA7q82QoY/j8C6u7GWeMdR79ABRRwaTlyRezQIfaoZY37LT9l/7At1YFckcNAIFxidCtjLpCzmthbfnEavKDfuJkYfdaZtF99dsOm3HtlMBiOyorx0/GgEYDCGBisJG2gt+Uc/pKO/iZQvJ5HF87Ax1K1k14aLUfHgptSgXVftJ+1NDXDvBusoIBi+oBGb4LWijg/BYFbt9ork2XTo48lklcJgVBxXXym3cjRKfgny7WsUS83bcKzApyFw5VldupU12sc5Ty75d/IH2nuN70w0Us5hV91AfR50QFF0Vf6Umb506RTV8QGXqdCmWUBL/aIIYxIvBHgSwmD7f0rCVSp407vqz+TRGqEOR3Mukn7Ou61Kb6eM6kQhSUg4dr9vDtCTpGRIv2uE2V91vL2FEM7zXynW6Q1lovCuR87vcv9zCb8Psat42BhMl5ypBb1q86YwLVTx2sS/WC9C/gwMqlZGIRBS09dY47LmDTPeircLTKlNqMcA9nFkCExE3FLJonFdDzEfq40XZbOj3nKd5Lwzg1VFtZioKS7RfAy7AjoTMlvpPZL/I99WMqPiJikRhIFXGJJtoArUc8cDmvvrzdJLp8aX9i3X+4Zo= 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:(13230025)(4636009)(396003)(376002)(346002)(39860400002)(136003)(451199018)(46966006)(40470700004)(36840700001)(356005)(36860700001)(82740400003)(81166007)(8676002)(36756003)(86362001)(7416002)(5660300002)(7406005)(44832011)(70586007)(4326008)(2906002)(70206006)(8936002)(6916009)(40480700001)(1076003)(82310400005)(40460700003)(186003)(2616005)(26005)(83380400001)(16526019)(47076005)(426003)(336012)(54906003)(316002)(41300700001)(478600001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:54:23.1876 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f66167d1-0096-492f-6c1b-08db1373e1c4 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: DM6NAM11FT077.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB8697 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 669A280022 X-Stat-Signature: cm9auunz71za58n3x6nrpt93pnwpmwxt X-HE-Tag: 1676919268-418150 X-HE-Meta: U2FsdGVkX195YdiVagLRFfW5LvAjfzuJ/mWea1uiq3t/g6XPO+qZyal+xfGdwCp0RBCFA+jLOCMRuMXzgohcFYVS0hnAdnoHzGr8MWXUjcQl3ojI09U5po2PG00jtlHQlxLR2rXWRN+7sQiPulnEkQbfBxdoiPEVN9VjRMX4eq2n6AwNbCUAulTz2a3SJt3CYgCpAsVdvM/fHHlY3ECu3YPiUDoUf5HdVKdIxY5JqG6DRAl/ZkuCzfPSxRDoeKKOO4eKCv/uPMOClH3T8Xv1UTYPkCfrnOtXORonuLmB9iBPkg9rUkLejIzZ8LpxBoUC0aYHwffMZkdA+hiB2izp021sWBI7ZdY/ZaAmxIqYnIfmYWtlRQZVdKlVQ4h6Efuyny34SawAXgn0de8hw7XYE7ZyGozaHvx1wHHIgfTGcTrD1N/1fIAyFBj5+bDwVHIKSszjN0w0/4XnPdakazW8xW29L4XmqtIN47ctciSKB23a9hPUG/7nV8MHmGsEXo4Lp7vVrUT6YOGs+4UcfNSEUs2v/+X286vr1XCQ4pqu4331kGKIbNomIq9x1ultBakGAkg7T2xWu63WIt1sRM5a61fK2BOTZnH+xS/WNuy/CMvrPREDzNwhwBel1KpHmuJGzXZ2TRtQQRxmy64PloSPN885aKyv5BRWpZF7MmDUlS3cL2Yn0DbSe8nl4fkQzHwJU7Yfg5zHbzlNEHA4xb2nRcUeeErLxzTniAGTgPpPJl6lVOGLzmeYuyZrGZzY4hs9OgMT+9w3sG7D1zIQaShEdQqJsgXPajWX9ReEXa2QgPyxGmfQGjDFT7LR4AtuVVQVG68/F3IH4ZGWaNKw/iI8W0jnPj/Hk15GBsts0WduuygzMCeKvwLt3gJacU7DzN7gN4DGTZr1fp3QBEsInyA2gJfgrQE1IDflMaoL8zJIGy41IstX5hrtFtdCx2x0zjT1YDaw9NkQ7qP6RteKi9V ufCeXVeJ sz2CnI1lmUX8EChdDKBxTBTDAKsBeq1SNtgboWVa0U256cWqkMDrGfSIQLnyZgWcl93Spnq8i9YjauM+Uc/f5cvinASX4g2KDDEo8IqG0GC4ftw6xDh11gLjuimscRTGahLjV1rZKwJkc56zxRiOIxDGjw2I8ot+V38opcoLSE/3F4e16OQqCA2l/ulTX2OUQcVO0LCzDtQxt38SH/hYBO51iTPlo7GNfXJusHnJdEDDbcBngKtRdzEpqcmeKdYbFEtj7M46mjZ1D5SmCdgFsRk+Z6NlUDGJD9oBBNF9jS7iJ0Re3tTKuyyqJmfzh62rlL+MgjUd4ArXXpZmXwJsd6EPoaOGVkbOHu2YbHN3JUMsmquc= 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 456b42cb167b..d2e1c109dde5 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 Feb 20 18:38: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: 13146941 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 3F4EFC636D6 for ; Mon, 20 Feb 2023 18:54:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D1DCD6B0078; Mon, 20 Feb 2023 13:54:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CCDC36B007B; Mon, 20 Feb 2023 13:54:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B6EED6B007D; Mon, 20 Feb 2023 13:54:50 -0500 (EST) 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 A75466B0078 for ; Mon, 20 Feb 2023 13:54:50 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 702871A01F7 for ; Mon, 20 Feb 2023 18:54:50 +0000 (UTC) X-FDA: 80488571940.23.7BE2B51 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2089.outbound.protection.outlook.com [40.107.102.89]) by imf06.hostedemail.com (Postfix) with ESMTP id 8F266180010 for ; Mon, 20 Feb 2023 18:54:47 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Qy73IVK5; spf=pass (imf06.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.102.89 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=2; s=arc-20220608; d=hostedemail.com; t=1676919287; a=rsa-sha256; cv=pass; b=glhvXmUPpxBTzeNrFdnYX+DX/O5WBsdwXjvGgYZZAqyNemGvTut1D8vPbBU9zLmkVbaXOR 61Mz1Cipf3E9LRDG7x2aOC/RTDo7mwahql+ehiDAq2mZdkOQetSj9mIb2MNDjjcpws1o1G uJm9JmLJBo6MxpPGpAMHo73s63/e7a8= ARC-Authentication-Results: i=2; imf06.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Qy73IVK5; spf=pass (imf06.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.102.89 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=1676919287; 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=bfbbeYCbDjRFikAqVByP3v8vGvoyVHSwAy9gWugZHqw=; b=lHuOoPcbVQQUiczIimFPq3Sk05MwaV+YSnZHfyE+4kQlRHZO45EncdCb+E6KSPICyX6eck 8rQywkkJbujTKGSsKdgW0LwelzH1g1/GJOSP7gs3Sg2J9Dsqlttm5Q4irQmCLnnxamDPWQ /RTThmNz5+mhJK5NXKqvhMR0D5hwNV0= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BMjdDOy7d+TGYiSCPoRPo35y/JJ2qyZxQe0obeJnNphm+bHKC+9noC1dy+TNw/u7ZNPLMcrTrcqPvgnR7wJztZwkTOcYuqtuZYYvh9KuIHLEhITFHFwjORWc+8ZTto1vuxphZfDgsJwq/rB7m5EDy3uf4oVvlAfTRk10DZAuXSszI9O13r3BnZNNXC8lynH4DYCAHFooUOERu9KQa3VFE3sML9b+KRyJirjU+23ZBQBGRdHDlpzNsRRpW3lrgn+bAbWG8bIgUZRdqgxq/7If84WawhE2Sut4ukbeCVqakpTJs5dv0ZDy1U/0/6IjI16dp5Nai6cqCVtzQXjyhLJU/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=bfbbeYCbDjRFikAqVByP3v8vGvoyVHSwAy9gWugZHqw=; b=mmYnq/OVDJkalwxZeYfAN3IjTsVqEHDhs5T9DD010YZMVelQ2WXK18Z57gJJESHAHcn7cuwEvpGBAgDGWq4H3UI4mMuBH+ZgVdsc0tLfpjLuuyjOvNXuogS2V1u1vdMA9EB8On1I/Rkfl/BO13ijigVWs+wJV+aTzvMBE4EUBY4ILfqiNkx+K3q6/aP/YzU0IjSmAmvos410dcBcjnqQSkjskywtc/J/eqawqKoQHmpyjfliI0qnnqbsqV7cByjgKIGh1IT6K2tXvHZ2b7Fm0RUUVtPpQNuH9MViwQq980GyT2cApoeim31FFbEgtQfTed+trZ3cpL3hRT8n4C4Gqw== 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=bfbbeYCbDjRFikAqVByP3v8vGvoyVHSwAy9gWugZHqw=; b=Qy73IVK5J+rW4qYNtPBVxqNDSFQxh6fHM/zyJYzPElvAMJhM63QkTCYQijBSY36UQZgVUdf75cRnQKrwQL275VSEY4U1VF1yBAdIwst+ttU/mh4zwh2CzuYh5+TZ6vkd7rRC890S1Epn0a7GFEhpnOGk4S6rwWhHs4JeBPiSoiY= Received: from DM6PR07CA0041.namprd07.prod.outlook.com (2603:10b6:5:74::18) by DS7PR12MB8252.namprd12.prod.outlook.com (2603:10b6:8:ee::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19; Mon, 20 Feb 2023 18:54:44 +0000 Received: from DM6NAM11FT072.eop-nam11.prod.protection.outlook.com (2603:10b6:5:74:cafe::84) by DM6PR07CA0041.outlook.office365.com (2603:10b6:5:74::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:54:44 +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 DM6NAM11FT072.mail.protection.outlook.com (10.13.173.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.19 via Frontend Transport; Mon, 20 Feb 2023 18:54: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.2375.34; Mon, 20 Feb 2023 12:54:43 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 39/56] KVM: SVM: Add support to handle GHCB GPA register VMGEXIT Date: Mon, 20 Feb 2023 12:38:30 -0600 Message-ID: <20230220183847.59159-40-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT072:EE_|DS7PR12MB8252:EE_ X-MS-Office365-Filtering-Correlation-Id: 7221e5be-bcf1-4593-4950-08db1373ee1c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xpE3iW1to2rfFR02Hs7J3oU3JFQMw/vvjU8mpcQIwsY2g2HIU+zZrpJvzR9uLiqsQW/oIQkaosDhtXurnhZ4hYNnTW3ytPeQ1OdQFhjUzuO02X5vPp9sjITZKYe541rwy73t7vUX/PTeuAccj5UZVZy2DMCcj2CMdVBqbaZa/OMFoJvc5MkUE1kQ4DuanP06t2DRQyUSgQeNYWrRRddgGITg55zTmIcS3MFNGbwUiTHwItYmtQZP3GXSGkuerp6b6x9lSVbzBPgiG4iAxZrfSz1p4rnzketUmHp19ft/teFVs5VpyDo7/wzelCtkltwjbco1HsW0wTZrBbMI6Pwd/qq0Z3eUQdhzc2uSmBlhFVrfv6yvu8i1IWtL7xL70M0IcbhfKGYArYObpVtOv5pNwDLpXeRqhOkBCQBm244YS7WGlemSEeSKmsDzBRvPEx7WZBFQZck3623OwYTobVf2X17cZaO5wnE8VXIz+Cd6saVNiOd+t+ea/u/wSJ1FNswHOTHL7EXVCVUK+f6J/qHv8CDeFDuILgR0vUJrG88JcdPhc46MjYqgqZ3no0gFe+2d6R4faZxEwnb4MFVauAImZfrt2t4bbsD9ytwlrGUt7Fg3rvKdokCmq+E10ZQoe2yKXIXi7/8YDm/iN3T4BZE9hPYCqDMxUOubVa7MJfUPW4giKkzRGC5sqL1EYzSQeh3QVIS4yzbnGrUHlVmx3Yo0Ov9dOUyHPQurnponB5D6xdg= 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:(13230025)(4636009)(136003)(396003)(376002)(346002)(39860400002)(451199018)(40470700004)(46966006)(36840700001)(40460700003)(41300700001)(70586007)(70206006)(83380400001)(7406005)(5660300002)(7416002)(8676002)(54906003)(1076003)(2616005)(6916009)(6666004)(8936002)(316002)(4326008)(186003)(478600001)(426003)(26005)(47076005)(16526019)(356005)(82310400005)(36756003)(40480700001)(336012)(2906002)(82740400003)(86362001)(36860700001)(44832011)(81166007)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:54:43.9273 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7221e5be-bcf1-4593-4950-08db1373ee1c 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: DM6NAM11FT072.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB8252 X-Rspam-User: X-Rspamd-Queue-Id: 8F266180010 X-Rspamd-Server: rspam01 X-Stat-Signature: 6wsrxcqrh6cj71btki1zxt4ahkf53odd X-HE-Tag: 1676919287-510464 X-HE-Meta: U2FsdGVkX18GA/wsHZpgLuaZ8CmDBYF9vG3jrgwCkOxkWUo6rNnK5+19oI88L8UNfaZfOi6RpYyXjdMq2tSg2icKxKTrlyWuT51WtCinhlPuaEgwNHsxgP94BEyXHoNJQ3HhreKwDPz2KwWbPgLGNgbln9wXXYsye7DvVtzLGLewnMD/Dj0LIA8Fw589Dw3WWk/dnHDK0sh6ecjP1JdqGUTrQ7jjUjVecYq6uAkSDCOwv9GK3JV71YH1cKyXR1DxHnYs/kwIuKQLrAqugsigTZsqvdyKGBcAjvlc+tR28KOsnSAPZshuMtrquJck91nDtjqYhdSJvJBApiUEu6RPgA83MgKHe1Abcbjtra9YDRmsjtCL9HjzU1qT3wU6+EVtHqXxwW77MNJ6yHKRACOd5ua9stM5HDy4yjVjTMDLsggFeZjIzzPjNzwdO7/fvco3P28vw2wrMruy7+KMd0xoynFan+VaJ/CegFCO7TyX4KfUolgNtKRUC4t1nrvpzSytzbLknzicFrvwALDJzd93omEAJ0t87gWKWc09cCUul8h2RPtd1iiavjZT/XI3D1SNglRK186biFSBAgUxhAl/b5ckxm3WJJtJNgkCjLcHlhFgrNs9WwQLPQ79+4ZtkychDcZM34Zvd8hTPxU9MVIqozKp2fGucCEDJboAVlwqtar2xnBlS3GBl8pkxdROWviPKbiYgPlgVGPkVe127w8wJwfUef3kCk37+PaSzFyscpxXfLx/0j83nV56iwqF63B3GlCqYVKcC9V/kY7blElC1TsRxdBc5GwrLAiw/wraI5Nxg2A/5BKD6fujnR/MNa1bDnMx28oqaDr+O6AjOnRVNcgPifMcCsfjJUO5ili/guSkSw+1ER0oI7kmWWuAzAsF7sc6HaurkFtGo/7LgEX5C1VrMRVe8CnZRwXZGSDg2EbDsuwZJvgcHMp+7GcVIONrzj1rnIKZ6+K1Cjqikyn Hnkr4Ndo rNLLyWj8t86Sna1yjoHVrfxH0IgtUZNzcaHN31yTzqtZ0ONC14UHFWD2whhk7KqIVg1ivrlejJopy3Sq0Z5qh2qaOF/Q6EubyeNtPR3sxM/a2RULyeoDvdCVrlcTENVP6I2TmXLCHZgviKEWFmKhhot0LCZhHSnjpcyE6LdKJc/XJFwygZQTSbpphWlfFXlBl8/gK8SWUvTQa9eGnQlNbfr4kr5QFQkbEAIMCwBndVbF6mRJpyJxDjQHAethDZLPgPDDXSgRbigE7EJYArzyPDjIcbWmiZ6P0Pw7Qv/Gyl48iA2xs3cIQtavK1aUIo5HaaMz1ux3n9GTfLFhM+5FYRdDEc63Z8Z3yrxEnww1ZbUU/qlI= 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 539de6b93420..0a9055cdfae2 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 e8740c35be39..2613311f4fcc 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3424,6 +3424,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; @@ -3490,6 +3511,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 cbd4594f1cca..0c655a4d32d5 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -204,6 +204,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 { @@ -336,6 +338,11 @@ static 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 Feb 20 18:38: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: 13146942 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 C0293C64EC7 for ; Mon, 20 Feb 2023 18:55:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5CA766B0073; Mon, 20 Feb 2023 13:55:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 57AA86B0074; Mon, 20 Feb 2023 13:55:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3F53E6B007B; Mon, 20 Feb 2023 13:55:11 -0500 (EST) 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 316896B0073 for ; Mon, 20 Feb 2023 13:55:11 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id F0CE840222 for ; Mon, 20 Feb 2023 18:55:10 +0000 (UTC) X-FDA: 80488572780.03.52ECF2B Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by imf19.hostedemail.com (Postfix) with ESMTP id 03FEE1A001E for ; Mon, 20 Feb 2023 18:55:07 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=hRjW2oR+; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf19.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.40 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=1676919308; 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=UMuiftp5hLD4XlVZp8/8ZxOwpPrSyoxlNaZIJkx8b7M=; b=qXu1P6ghoBdC+kSsBl6Xw4elKo+kW3+rF5ouKDoyCeB8dJF8T02PaffoMXASmfz/CWWTAB mOecbXf/zSOqujGrvNBM7eQ5B1N3pCcg+SjxXPiteFIvG8lAqeWnyiRR4Z/1UgWdD2dxXQ sOWicRi/r2BvI/sDyKlXmdmXdMCNWqc= ARC-Authentication-Results: i=2; imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=hRjW2oR+; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf19.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.40 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=1676919308; a=rsa-sha256; cv=pass; b=oB69cDNtfdpokM4oRuUOx+TI2kCtBGHxFUqHjua9U4PGCB42CaCsCD5c5YDicLGONP99CT GgsAyisA3GfG0y+5LBVc8OZT/b1R696iHQvBOrOEj8A/0z1VEoU08XpmuoVvTPbCIshFUp ZNnjIwjQKY30oj15cUCXlQtLEEdZUz4= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Vqw3cl05T87AbQHDyU17PUrnQ8gByS7Msj8yjTjLfmlnEwfwzYPpdk+hYyyfPANzCkitMYlL84Jwi98E651vJj8hYfubhhjvCpkwuFgz2nIf0O7bd4ucrh8uUvkZRspzZJsqusFRoT5HIKVmXK9FTMckcedU9SIrXS037NnB0/2SUlGyDQIXX+T0wPZuGrO6YsrEYNy+ujq70auXAvq9TDuQ76bZT1g1Sk0z3UsLOHMRynUFlugz2lkEpbZpGXmZmk8JLxhwBSX5JTI0zCn4aaj8MS/kYM3sSQJwN7Mn7RXSNSbQaQsjtLI36HlgyEtndY49Z0k1CICSGQ0I4OX47w== 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=UMuiftp5hLD4XlVZp8/8ZxOwpPrSyoxlNaZIJkx8b7M=; b=kTdcdVIHFG9oZhLcMVhkG39KVVpywKm655eGbJ+BqsNWtNwst0kz+xDf8wSfm4EyFLnNbYsOMNon+C/RWJ30QBHDdBO0XSTpne8Xb4SmAOnHn2jcFc2jWBTggd0rxG3TRUE3QQFgp0HaQ+2x8iRZcT07Xp4L6oxOGSvIFFGb1kpXBtsMoIiVx6DzQLaaUV/ELw94+VFXrX8EFEb9ER8VA32OmF9vTl6TGTiN62wr2PcbkqZcWbGdmcWvLtgnZ2+y5xO8nmBy7/g+vxsuNHIu4j8aXfJbdrYJKYKzLODwmc0eWWS6hG3CECdWyBYEVOfPct3R2dX9N4gTIZLe8a5J2w== 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=UMuiftp5hLD4XlVZp8/8ZxOwpPrSyoxlNaZIJkx8b7M=; b=hRjW2oR+acSQFeXz3mjy4950jU8Y0MeCTw47n5OMKVOtYV+FNfEB/c7QwYdTwDB5t7faQf8yyAWX6W2ZGG1fExkWyU+FISJQTPo1M4M+TcoT5pg9rbT6PrRDFpiZJQYHoszxzL1QqyMeK50VjugNJcEBxRf53u86u8baIUppeJw= Received: from DS7PR03CA0107.namprd03.prod.outlook.com (2603:10b6:5:3b7::22) by DM8PR12MB5448.namprd12.prod.outlook.com (2603:10b6:8:27::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20; Mon, 20 Feb 2023 18:55:05 +0000 Received: from DM6NAM11FT011.eop-nam11.prod.protection.outlook.com (2603:10b6:5:3b7:cafe::54) by DS7PR03CA0107.outlook.office365.com (2603:10b6:5:3b7::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18: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 DM6NAM11FT011.mail.protection.outlook.com (10.13.172.108) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.19 via Frontend Transport; Mon, 20 Feb 2023 18:55: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.2375.34; Mon, 20 Feb 2023 12:55:04 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v8 40/56] KVM: SVM: Add KVM_EXIT_VMGEXIT Date: Mon, 20 Feb 2023 12:38:31 -0600 Message-ID: <20230220183847.59159-41-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT011:EE_|DM8PR12MB5448:EE_ X-MS-Office365-Filtering-Correlation-Id: 05a551da-a106-4f5f-910a-08db1373faea X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: VxR3T1TC5MHjMIrUsQ5/xuem8nkUHTZce9t568ha7mvcTv+iX+FmWSheWdcg6fDbcCYxQ5kVHvse5fMsCfryrgyYT28XZmLEgNIhxjFcei2NSke36zqeheCN5j6q5FAFDJUls5UuCeRtxTNfy2f1dFdA5ZoMEYTDVvSruVDWJQ8bdMKUQfRGCZPXEORIkHQF+KEmMhfu+9S5PxmsFHzGt2++T5N0SJw9uvp/+G34e7OUfijvBep/BSYmBDKQE8enJR1JxtF+yhBueEcv6LQgaMMy64mrXiK7Jgpnf8z0Vsv4u5DYV6Yh5KKKta1iAXRLlDxX7bjp4cAb7Gz560BZjzEb87iWEVw6UtJespzCrGy/rReL9sgCLBrU/P+j0mMiceQ7oug6+GhRrengZxx5IqUZe5abBbnc929AItSrTge7VkNKlSCLcZcqaxKmseXhSo0BGgt5acdN4eYb2u02vrbEbVsIdy1CVtwTsn+E4Q4vw2nYE/aagRNG7b/GQk6A5+uUbQea3zpc06mMUNc5BMSniBOcSY9iXdxpuVpSGXpLTZOImdNM7v29zjQ8Jr3F/l1H6eKyEZAQqoAfAFiuz1Dmy995KyGEnMj90L2XtbNonGuOTaGVONSgHXUSaHAxtOdPaLHzfpOOOdHWb/86DG2iVShLfXz+8Lw9c4DUnETaaVlY5eOn4oq4Y5s6tGkYvQ+rhCyMv/Ux6A5/kyTc6ICa5myBPPz2KTJB7kufoPA= 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:(13230025)(4636009)(346002)(39860400002)(136003)(376002)(396003)(451199018)(46966006)(36840700001)(40470700004)(7406005)(4744005)(2906002)(44832011)(7416002)(8936002)(36860700001)(5660300002)(47076005)(83380400001)(426003)(36756003)(6666004)(16526019)(1076003)(2616005)(70206006)(186003)(26005)(81166007)(356005)(86362001)(40480700001)(40460700003)(4326008)(82740400003)(6916009)(70586007)(8676002)(41300700001)(316002)(478600001)(336012)(54906003)(82310400005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:55:05.4119 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 05a551da-a106-4f5f-910a-08db1373faea 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: DM6NAM11FT011.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM8PR12MB5448 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 03FEE1A001E X-Stat-Signature: o6enromsg4wida8s49cap5ttwethi5fw X-HE-Tag: 1676919307-445140 X-HE-Meta: U2FsdGVkX18578KbhjTiPwoIneZJXeD9tOrJ6Fatrke5dHowuw4IKCL4tS2npQbviFv7DWqBpLLt2AIhSKgyLut+zVtxiLK+hHIeeuI1rPvBD2u7tWfMbpGEGaEKSGAgC+plcJs+Pplyrkbj0PUdm0UAOtUtcyrWEdutgb58oBc8rXBUwz37laA/WuwAVgi9QNlJI2KxF9z4mwJTa+p8Rucy+YBUrTJVY3rdvnc7/PWMYyYA98agx+G0k8HzCnIAjIbJscR4Xp/n1T1jtdwFNKomnjEoYeMw2wvBNJlEqm1VSVX49YIYIWuSNDoa7jGk/elHnJBbCOMa8D0NXHGuBqpyCHh67XxzU0KI4IOuo4RfzmiSO/9GBFydvBqrSdsEWjVsSiYuzHQTIiobJoc4LuL4aquK/xNI9NgwCddbXLBtKUmtUeVf1NB8yVFXn6d6/B5AS9ViJ95ROsDcUGm589xOrcZl8m5W3w2o517//sGA4/rz6mmUfxz3KVhn+2fLQgqm3w7UwwOyK2P8kQFw/mgJLMYLAYE55lSDXc0x46ZABWLeMQcM6CwWzLCXgjHPqwa9aITuTcOdvBJN/IezPeJ7IFlbkyyrdbVoOO7hLuL8nWCSDPby6MfooGpMAznTRHU+Xrzmepsh6GjGyrCBbayS7rSnYD0kBVqaQ5ri1qyr/onMQE0SYykgEiXIinJ5HcEg5Mhd3a9NvlJaMJyj4b/uheSADvO0Ykw8zHd3cPH88xQlvZsfzqcPZ8cjyUyk5Kbhy8ktwXS927BkHocA6cuZPn0yGfKqt9C81eUmXoDZ3AaiNnFX3ayuciuSDSKPxZm9ofm31E8AzGCt+Zbnqjn7Gbo6hQO+4JNbXCarAIPc5NeOXnqRHMRorNtPqLfFg9PDNy34NtcifEq/ruSvGe5FjSs5ErJIX8C4hMXa0ZTEfi98cyLWKS8XtGp1R+VcA7uVpeCLjCRotQg/hrk /vbCD2L0 5/RbA6G4yIdPGQalFMgTOGV1mjxPhvS/1jO+cT+hD970GwArN2uyss0w/pJbP7rutAEXH8Ll7IBdqocjtZoRD0OZDG2ZVvCQxvnCXT5b6PmUXUdPlXsVgUJ2onidj0DgOSYsaBtCvmzgl29CeqYvgn0ey4wSMtw43vo63i3aWi3ojSUBUK1KQoO4geHT2iglyChoooMSzvSNJFfHkZYawIYATBSiMLJZNBHUX4/hJ2QZ4qrDzskfJaasppaxUy422IytG+9Wl1dS0Gj6JmYw9IXe1tmB9kASkbVKbPjsl1A6wXT57LXcEJ8Wkk2ky9KS3w1ohZe/AbqiLLTqB/0byvJS2zFn6wYuTCtvSkvaJtqtlq/0/iKtWiuLbTQ== 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. Signed-off-by: Michael Roth --- include/uapi/linux/kvm.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 2bab08a5b5d7..6e684bf5f723 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 */ + __u8 error; /* user -> kernel */ + } vmgexit; /* Fix the size of the union. */ char padding[256]; }; From patchwork Mon Feb 20 18:38: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: 13146944 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 20D70C27C7C for ; Mon, 20 Feb 2023 18:55:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9C3FB6B0073; Mon, 20 Feb 2023 13:55:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 972096B0074; Mon, 20 Feb 2023 13:55:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7EB9F6B0075; Mon, 20 Feb 2023 13:55:53 -0500 (EST) 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 7045A6B0073 for ; Mon, 20 Feb 2023 13:55:53 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 398F012011E for ; Mon, 20 Feb 2023 18:55:53 +0000 (UTC) X-FDA: 80488574586.13.3C177B8 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2067.outbound.protection.outlook.com [40.107.95.67]) by imf05.hostedemail.com (Postfix) with ESMTP id 47F5110000C for ; Mon, 20 Feb 2023 18:55:50 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=ljOVuFjz; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf05.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.95.67 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=1676919350; 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=EwBov8mAFc4rrnqr1KAE9hELe45EaYrVBKIRrG+aCxU=; b=JXRemhRNcqoPB2sSt2uSQ/TWdXky6FS3mehDTCl2mWcYxqbMjAMjsfvIPJDz6671MOykfT 6eVr6MkT7Bw87JPxBJFTrlSJdUVb6LeiAbkNF/5bDcJcAQ+awerFq8f8VtzovqLmIU2y3E uKLYQ300waKRGZYiXsyUBsgWOyRHIbQ= ARC-Authentication-Results: i=2; imf05.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=ljOVuFjz; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf05.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.95.67 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1676919350; a=rsa-sha256; cv=pass; b=I4RTiYtDu2aQ+Wsf5zQXfCF2FIjJGoYBMHE7jM8jBILCqX69EGhi+7UR4xZQHbrKHApg8J fvQGy2V0vFnHp0yx3RoJ750mHBT5mMmSg2xRKF3mA7BjzGdFgBZJeAp9PjpekWkt6fMAyL L3QUM5VvohC9y+7r5+Rt8BMKH0abd44= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Oty/0k97/2sXklFEt4E1qbfNT6u0BPJjIU+E7gs++s2hInTN7R0mkDVEp58EeZcyL0sVjifIGCAjji/GRbMxVlx+v+Bno80eQ4i4dZuMOIBHSdiILKeboweGpxNXltH9PVSAS/3jYSwNkN1wQWYNk+VNWsvT0fIaAPFYgrH/Gyi6vReqlPe//k6PyNDXE/chzFhyPK/PCz8DzZowW4yXkxmuBk56iLRd9riFghqSC3nxbFckXQFfRy1XO9ouMKWi5LIhNxqoCC3Wd13G4tO/bE6UkMcuvLpvh1VBirahG/LiWjVNBqnPWzf4TRYpLNh9lD9z1l0ldt1yVLDEJkj3rQ== 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=EwBov8mAFc4rrnqr1KAE9hELe45EaYrVBKIRrG+aCxU=; b=f6d0XajbeCngJgkKn3r3FOQ/FiW1lNhJwt2YbdEdP4Be6EoPq3nw2KCcRh7yxpSwvlsXj45cjOEKb/56JZs2wSk4rHi6+VvlABH7sLgzNnt7YX1R3id8f36EjMUv9MxIcRwQHHd9kHN5Gr/EgHl3sDiK0iTI27DELU+hsKauE7tmIyj9WejquLgH4ZmhhjWjkr1rvVFGb3gHe9CxPhOmpNiWhi5HgnUvdSwAs6fR4b26tbERYRMGfihbvJYCavYgGPIfnfkVIAP8rBwV3S+qRp7wBOmoan1+WS9F383FJCIXQ8e3rBLxbmOFoveBODALOpVetO7+GKAsS7VtmxtsLQ== 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=EwBov8mAFc4rrnqr1KAE9hELe45EaYrVBKIRrG+aCxU=; b=ljOVuFjzzYASCrCea6lweTMywjNu1/hFlgobFFsIvGZwqTLvjQ9ONJQUqFrdcuwykHLBsiCj1TCAMlsbGoga4wp54VUP5SSCXKtP13WpRE9O7FDu2CHvYqieTzdCCdaiItPH5xJR0tQ6vxPrj5850Rv1AoRaUfWb47G9TAna9B4= Received: from MN2PR14CA0024.namprd14.prod.outlook.com (2603:10b6:208:23e::29) by BL1PR12MB5063.namprd12.prod.outlook.com (2603:10b6:208:31a::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20; Mon, 20 Feb 2023 18:55:47 +0000 Received: from BL02EPF000108EA.namprd05.prod.outlook.com (2603:10b6:208:23e:cafe::98) by MN2PR14CA0024.outlook.office365.com (2603:10b6:208:23e::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:55:47 +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 BL02EPF000108EA.mail.protection.outlook.com (10.167.241.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:55:47 +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.2375.34; Mon, 20 Feb 2023 12:55:46 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 41/56] KVM: SVM: Add support to handle MSR based Page State Change VMGEXIT Date: Mon, 20 Feb 2023 12:38:32 -0600 Message-ID: <20230220183847.59159-42-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF000108EA:EE_|BL1PR12MB5063:EE_ X-MS-Office365-Filtering-Correlation-Id: fbf6540a-1c30-43b7-58a6-08db137413d3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: c62kbVw9zE4fIBkaCtccrEtX1RIli5gtFwVktJlGxRK9FSy7BFui0tXfp3Zt//aetJlfvNzu0aSuWqnMlshiDgi5s00EuLkPvyKVQZFzZylNQE9oSVo23T8GZ07ehwn2e6PQDJzSyf8/qE5+m8NhI5YExSdUEyQs26JDVsnu7ETwL+Zmh1J82hZsgiI7AxFmXnFWqOLIfMsfpsxECob79aOsuUHcn1gnuRsLu7J8sl3A8hdhQHS7ybds+/tTLvkPNUxLFBYGlVgzDRXNot9c+YaCBQvSWSqG+x1/zbvLj6ubwXfXUhquoEmxWwn9kTH4n7kkn4NY1MZpY7RKA69cdUatKcXeTXWSI9O1crgeJxBbeQS5V5y/7e3jgwCE2DNJpYdbVpqrkbMRm+d8joZJP75htmtjqwCj2CCNSEbC4rcYIPlS5eJMWVKUrU4kA4ykhVv1metnhDjg4zOcuMlorhVqoV+rYwnbSub/Kz6f9mZV1QJH38NOc/6+WT+EuGSdjSnV/rQvl4syFV1VAEhy/TU9mOtAQdo3ufElFkp89HkryFygm7SPP6DT5sveTP4PH7suKxLFqgZM5INKaecNNjJDyY4puJWxN4EfzNu8COAxsOVh9zOh2pPey2vV2XKMivvRgVwEMaKOseo5EJb0CL46PH1jditgMABNvKJbP4PIf91wgJxHBVR8yTmYqh4cTVL9T76kiXUo238w8URqBcHDXo8VwA4HDq0M6w+Inuo= 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:(13230025)(4636009)(396003)(136003)(376002)(39860400002)(346002)(451199018)(36840700001)(40470700004)(46966006)(82740400003)(81166007)(82310400005)(36756003)(356005)(36860700001)(16526019)(40460700003)(40480700001)(6666004)(26005)(186003)(70586007)(1076003)(70206006)(4326008)(336012)(2616005)(478600001)(54906003)(316002)(2906002)(8676002)(6916009)(83380400001)(5660300002)(7406005)(8936002)(44832011)(86362001)(7416002)(41300700001)(426003)(47076005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:55:47.1715 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fbf6540a-1c30-43b7-58a6-08db137413d3 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: BL02EPF000108EA.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5063 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: zp7etddpnajk9uob6dbxa9jed6frjjqp X-Rspamd-Queue-Id: 47F5110000C X-HE-Tag: 1676919350-92464 X-HE-Meta: U2FsdGVkX1/RedMrci7lQ6/FRbS3sB4PFtP3KSxnj+Yg7NpPTjYG5pKtyJ1nud853cNmRMODvGFohWZQOs6BG5XpuxGoWOyntbuZfJGr3WdPY5ZqSPo7yfsQmaks2DCGR/0wnTeSu2MFu743ZBEfpi3weVpDTauEiLQW2H72enpKNr0dfPt1Ry3FYgs+n1gOKodoobcYxS6SlfFkuQ9uLPdi1HvBpXs7fa4IdnMsKk4VYFhKXK5GDors6yKkr22aY+HtZ9xcHOclmsehYx5cqCFGcNgdmU+nZR9O5OSoeB658CSyU9ppYaXCIHlCsWXSCBOvVVf2o9faGE5em3txdw8xdEPDu74ql0eDcd83e8kki+wy4TF2K5cLguNh2cKkA9khtfj2WjYLhzs7A963TKE+RX6n7AJnldnGskcwsGTPq7ZxfTN9yJMxYKSISWyvoHou6R2mNMzNmKcX6zI+6sn1/v81N2UPiGCFvuTuEgDDW9BNRT+6B7KO231+WsedB3BivDZkcOA9FvwS0xEatfDg7LRJ3i96JFn1s4hLy3uakPtPzcaJjPX0JdnE58ZhcgVacKtXry3e2/qT5YdCG+bUOgjVPd8iS10rcknCMs93c/wIMOoZOa4xE2BJyx52XAIssnJwRThjc4EhOemVBaBzPqe4iO2ye4LBlYl6C/QHIk4YLLZHCk3gLQ42VXsvguPnc6IQELSFR6/fPYUfQNkg1wK8Hd58gDERJwiTLCUrt9qhndQNLOnqmpYrbPmGw2Bbn8OwFhZMDdwpdgj6mi8ks+idJ6i+gdyQEkWP2nu2inKvDqmfrvrNi/RfH0CiMeO9AwrO62/JGMjZUR7u8ID+YBMJ5OXyHkLZ++0x0+C8QTLaaV7QlqZEVpjRMi/Y8kUhLsm/LUysJD1mxi+W8rWmO74bRnby4dcUAsnw96NyLicexPFDGycNqp1SCviy0eSUHIgsZAkFD7vGGoA 52LhKiF1 oS3uv48KlJTEpLgV+BZUpZ1fPATbxSj133oUVi4AqRWxDwYXJmZJGphUY5dwR86cWaohW1EJ74MGnBrkwyC5H+i3iXEzqn8JJSj6nlGQXvW9CcTSea+EPX8xy+5Sg4xoOdWtZKSSm66VDSoyrS0NbdFUZnLmAU7pqUAutdqjKYukDrjJPV8rlLXMyQ6Ktq3tpv1i8lZhQ0lzpGDHLZauFBI7XSOW4C+V7LpAm7lCb7u/vC9BmTYtApS9npIAiGuXSB20x2eJ10STtkL36CJ1nQwDblrlp+ufzfxidmQoiDZsEQPlI1keSAdkVa6KJxZkFiD3oirSO2ONrrRo= 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. Forward these requests to userspace via KVM_EXIT_VMGEXIT so the VMM can issue the KVM ioctls to update the page state accordingly. Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- arch/x86/include/asm/sev-common.h | 9 ++++++++ arch/x86/kvm/svm/sev.c | 25 +++++++++++++++++++++++ arch/x86/kvm/trace.h | 34 +++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 1 + 4 files changed, 69 insertions(+) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index 0a9055cdfae2..ee38f7408470 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -93,6 +93,10 @@ enum psc_op { }; #define GHCB_MSR_PSC_REQ 0x014 +#define GHCB_MSR_PSC_GFN_POS 12 +#define GHCB_MSR_PSC_GFN_MASK GENMASK_ULL(39, 0) +#define GHCB_MSR_PSC_OP_POS 52 +#define GHCB_MSR_PSC_OP_MASK 0xf #define GHCB_MSR_PSC_REQ_GFN(gfn, op) \ /* GHCBData[55:52] */ \ (((u64)((op) & 0xf) << 52) | \ @@ -102,6 +106,11 @@ enum psc_op { GHCB_MSR_PSC_REQ) #define GHCB_MSR_PSC_RESP 0x015 +#define GHCB_MSR_PSC_ERROR_POS 32 +#define GHCB_MSR_PSC_ERROR_MASK GENMASK_ULL(31, 0) +#define GHCB_MSR_PSC_ERROR GENMASK_ULL(31, 0) +#define GHCB_MSR_PSC_RSVD_POS 12 +#define GHCB_MSR_PSC_RSVD_MASK GENMASK_ULL(19, 0) #define GHCB_MSR_PSC_RESP_VAL(val) \ /* GHCBData[63:32] */ \ (((u64)(val) & GENMASK_ULL(63, 32)) >> 32) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 2613311f4fcc..a1a2686dde7b 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -30,6 +30,7 @@ #include "svm_ops.h" #include "cpuid.h" #include "trace.h" +#include "mmu.h" #ifndef CONFIG_KVM_AMD_SEV /* @@ -3345,6 +3346,23 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) svm->vmcb->control.ghcb_gpa = value; } +/* + * TODO: need to get the value set by userspace in vcpu->run->vmgexit.ghcb_msr + * and process that here accordingly. + */ +static int snp_complete_psc_msr_protocol(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + + set_ghcb_msr_bits(svm, 0, + GHCB_MSR_PSC_ERROR_MASK, GHCB_MSR_PSC_ERROR_POS); + + set_ghcb_msr_bits(svm, 0, GHCB_MSR_PSC_RSVD_MASK, GHCB_MSR_PSC_RSVD_POS); + set_ghcb_msr_bits(svm, GHCB_MSR_PSC_RESP, GHCB_MSR_INFO_MASK, GHCB_MSR_INFO_POS); + + return 1; /* resume */ +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3445,6 +3463,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; diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h index 83843379813e..65861d2d086c 100644 --- a/arch/x86/kvm/trace.h +++ b/arch/x86/kvm/trace.h @@ -7,6 +7,7 @@ #include #include #include +#include #undef TRACE_SYSTEM #define TRACE_SYSTEM kvm @@ -1831,6 +1832,39 @@ TRACE_EVENT(kvm_vmgexit_msr_protocol_exit, __entry->vcpu_id, __entry->ghcb_gpa, __entry->result) ); +/* + * Tracepoint for the SEV-SNP page state change processing + */ +#define psc_operation \ + {SNP_PAGE_STATE_PRIVATE, "private"}, \ + {SNP_PAGE_STATE_SHARED, "shared"} \ + +TRACE_EVENT(kvm_snp_psc, + TP_PROTO(unsigned int vcpu_id, u64 pfn, u64 gpa, u8 op, int level), + TP_ARGS(vcpu_id, pfn, gpa, op, level), + + TP_STRUCT__entry( + __field(int, vcpu_id) + __field(u64, pfn) + __field(u64, gpa) + __field(u8, op) + __field(int, level) + ), + + TP_fast_assign( + __entry->vcpu_id = vcpu_id; + __entry->pfn = pfn; + __entry->gpa = gpa; + __entry->op = op; + __entry->level = level; + ), + + TP_printk("vcpu %u, pfn %llx, gpa %llx, op %s, level %d", + __entry->vcpu_id, __entry->pfn, __entry->gpa, + __print_symbolic(__entry->op, psc_operation), + __entry->level) +); + #endif /* _TRACE_KVM_H */ #undef TRACE_INCLUDE_PATH diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 268c3d16894d..0154fc7a28c1 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13515,6 +13515,7 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_enter); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_exit); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_msr_protocol_enter); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_msr_protocol_exit); +EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_snp_psc); static int __init kvm_x86_init(void) { From patchwork Mon Feb 20 18:38: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: 13146945 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 14F9AC636D6 for ; Mon, 20 Feb 2023 18:56:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A5D856B0074; Mon, 20 Feb 2023 13:56:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A0DBF6B0075; Mon, 20 Feb 2023 13:56:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 860FA6B0078; Mon, 20 Feb 2023 13:56:19 -0500 (EST) 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 779B26B0074 for ; Mon, 20 Feb 2023 13:56:19 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 572F3C086F for ; Mon, 20 Feb 2023 18:56:19 +0000 (UTC) X-FDA: 80488575678.09.8BAE346 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2052.outbound.protection.outlook.com [40.107.243.52]) by imf01.hostedemail.com (Postfix) with ESMTP id 6D8CC40005 for ; Mon, 20 Feb 2023 18:56:16 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=SYA80Vcy; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf01.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.243.52 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=1676919376; 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=/oTnKs+99hfYPNpv6B5e7Bf0gJ60+2+o6y89UaJhRi0=; b=HWFS82vLEnAG1cYDCWHrU8yLLS4QaTOv1j6VujfCTYGLO8ZoS5mW5esEnTiVn0FCFt154y 19oXAX5fSBpiIDbLQRGEwIffXBUZqTZ2xqtcIlqxuB/cBqHyQmX690jJC3wLI42bG+fdp4 Gz1197i9uKyYjS27xviqtEwMJhby9RY= ARC-Authentication-Results: i=2; imf01.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=SYA80Vcy; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf01.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.243.52 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1676919376; a=rsa-sha256; cv=pass; b=gO2jFjqTLsKiHqQ8L/jMskmkSw5lvRzOm3DIvRYBusBg24mBojd3JETbE2T8WrzbxoWdH8 jsNXO8HuSaU21H9moEmRMjUYdPnPj3Xe41QLK0PDzXYgx0OC2w6cy/6yk5+VWlZXX0FHb7 0Cp/2gUoBWNdpfft8KRWO0CZiufXwSs= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=N+tSWxncI5QjIemmHL8V6b1wiJ82mlW3qYQncKyTUIJmiaKoSfBruT1yYqJqqucPtGAPTlmBJMJPIEArZxGUAdktNpQgUm3fOwMCksjdjO89HE0g9A9k6EdY6LIDvPwkHJJGn5uxDsLDYX8ROrNJTxTsNlELhjf6f+gFI13IBb1oadVXRRJwagtBdL53Bp/KS4WBoCFF5x4H4JirVzaBDW4rAqq1GdZ1xwDz1Lc3lk8U1qL5I5KiNOrp4z6+s29lkFAphMLePxoaMza7U8pVuTkOb1XXQIoXZuAJCT+rKJIdLw/TQA8K6tQ1o+HKr62KVcjB5lHsTYZn3SJcoKhZkQ== 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=/oTnKs+99hfYPNpv6B5e7Bf0gJ60+2+o6y89UaJhRi0=; b=T1000cryZUO7UVqURtpp8yHpMdqCd5iO7Rd16ubE9CppmJ9Q4PsiCK9yvwKmwVAN6KlxJQVoBMxNKQpEzD/hvmnGDQXmgA4K51m7FKzDY/jjNQCii0J9RieQrQQ0NFGz99L70LCteGhF6YjsVJ1biUGDcvV1oNCzZZd6oeI9yPbOtax9UkerBDHK8DtyH894BsBSIRw8pL9cSXNtLDe6UTS1JNVCZdhuagnsp434b1ETuEkk9fq+XvVEd7oHElJq/dv4NN9IwzolpwLeIzZTTfzU+r898XwBbv1H5ztcYlMDXMvjRmnRFPPm8LoAl3QUBc7acEkZq2qOXvfblX8Lsw== 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=/oTnKs+99hfYPNpv6B5e7Bf0gJ60+2+o6y89UaJhRi0=; b=SYA80Vcy3OQ2J70pquhjzMmSoHrUuVAHKBOMqvOjOLU38xdQ540CsORrGYw+pA2dYKoYgQ8EGV08OjvZkj1exEWEkVdKHzdA7DalEOdnJiKw4qCZTox2eneUtFJl8CCHqdmdeTWaPyharScoc9XuOZSLroKy5MI7VNsTOoRMBoU= Received: from MN2PR14CA0009.namprd14.prod.outlook.com (2603:10b6:208:23e::14) by PH0PR12MB7960.namprd12.prod.outlook.com (2603:10b6:510:287::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19; Mon, 20 Feb 2023 18:56:13 +0000 Received: from BL02EPF000108EA.namprd05.prod.outlook.com (2603:10b6:208:23e:cafe::6b) by MN2PR14CA0009.outlook.office365.com (2603:10b6:208:23e::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:56: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 BL02EPF000108EA.mail.protection.outlook.com (10.167.241.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:56: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.2375.34; Mon, 20 Feb 2023 12:56:07 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 42/56] KVM: SVM: Add support to handle Page State Change VMGEXIT Date: Mon, 20 Feb 2023 12:38:33 -0600 Message-ID: <20230220183847.59159-43-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF000108EA:EE_|PH0PR12MB7960:EE_ X-MS-Office365-Filtering-Correlation-Id: c0a16c03-50c3-46aa-4f8b-08db13742331 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: zXiZRPF0W4XDM4q3RnB7OD7rElj42Oz11iFGYErcPpQP2JOqW2HPabKdDhDsgWFy1H+m4zhVfhdeTmfyQBNwlZldemr/KpdZr2ReUcyCC0n+Pk3zVpgKd1UBdkCtAsYKlXQO2Z+2ffbl2rcTxyJEWLanBXaN4+XFhEgdJc+2bGbw5EgTdn0qnAGQjZ20JSpU7AE+1v2kYAnnubMo2hOCLwybDky5/A3bHQwy0IjbSf5K5Jji48qiorQvx40NojUgG5aa7PxkCENF18ZZEkOHI/WqeuPmTGpfxPTRJmJd6cYL93KrQqXcKoOSnigEJsgprnfC+gnIyepOGfDJBEMFLoHrogYbMCNOVNaVqgDzAP9JeMdjYCy7ELhTTTs9zDxVK4gjpgU8ovYySN7DE+l3DuyEHQjpdO4p2NCIyqpuDCumDz+OyDRzivblOfel9A79o8b9lwdgj/Ov51DT84JFUADI6JySVlmnB/3N/UWjov/x7SUxAGE7bmBskwuAKXuSsJcmtebN5t7sNVbKB/mVgSfG0fD9t8kOpSeF91X+xc/f6o0VJmPzKAkM6G0b8aqx7BhanPUDxiaLgwjz0kxynU4icK7wqDd0Y9rVtnVWCdeTI09RzT/c1HKexd/3aI5D6E3m2wu8tsyYQDjj/SkdFp1rlDgB2ceNjNzDj+ARW336aBNmKBvIAfNZWPVPbeid9qrAQasinPi5F1goRoK4gw/J58oXj3DLpy/fbKt7Hzw= 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:(13230025)(4636009)(376002)(136003)(346002)(39860400002)(396003)(451199018)(40470700004)(46966006)(36840700001)(66899018)(26005)(186003)(8676002)(6916009)(4326008)(40480700001)(7416002)(36756003)(7406005)(8936002)(316002)(83380400001)(70586007)(2616005)(70206006)(16526019)(6666004)(1076003)(5660300002)(336012)(478600001)(47076005)(40460700003)(54906003)(426003)(356005)(41300700001)(44832011)(86362001)(82740400003)(82310400005)(81166007)(36860700001)(2906002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:56:10.7187 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c0a16c03-50c3-46aa-4f8b-08db13742331 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: BL02EPF000108EA.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB7960 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: u89oskjnnuh3xkhazwezmujzfhhes3kq X-Rspamd-Queue-Id: 6D8CC40005 X-HE-Tag: 1676919376-314419 X-HE-Meta: U2FsdGVkX199Dj4IAKZWa3oJbjhyiRdLV0CwuMikTtNCxJ967iLGIo2SJdFSGdToqJFUJ6LsHk8BwZYXpGyhaaoS/X0DcGqPCZ9KQqlq8hiVmbuYNYZHu9OQcJk8QWLtTSajECemO8zD/kPemPaR9EbVa1T8YTwq4RazYkMbXg3SWPrh9gwmGtIZaBPuVhQB+PzaM3ZJAOlFNCPL/q/HwAyHisCpPhzp19DjqE67JJhgWH2Esq2DsM48RVrOLMWFZsS2GxAOfIMZTpaYWNA632kum3bBqyC3d3/Dsle1cde4iPSGtRjjLVdihmU+1H5xtMRbLMXJtarKd82TIQZQWpMCrsYi362q56dIvLrfzUVfk36eLrZSwDsJPZIv4oo4GpoDXjmZUjVdixK2dsZfAgzvZ1y+UgLP2xniqnHxbcR4Rsy8GQxUl1W97fF6sUdPY2B/+3qxFAJRESLH1BGtJu82QFTDForqnl8ZJ8OeiMCStHONTdvx36I8G4wvt1KbaHdGD6OLI7QT1ODFaAr28f4XgT56Rsqcog9TFCxKByJRVtbNtV6rQm2kzabqTi4J8QC2UP37Fq8ruHWG5ivXZ0oS1MMICpCszeigcfOvCZDuZq8H0OgzMs9fxl5aZEB2wywgZPX+kjil6x7FK7leN40oNK9IRm/s0PoJ0A5A8M8r3EnJ4m6z+KF5s08GwPZ1z0kLgKM3+Kp9MY2YoyIp8SnoYoPFqbYuiPiapnGxsRRKvDWXZ1dgBmn6vBErM68zQXz5lczZNxW9d3+sX1bvoa8EHFz5vxk3Znq2oAqDUvFtv6M87KEw8on+KUyzOkB5r2o3kwh4UQazb3a5Ush5kStUHKXNpeCw8KOAvitNKnDkjvU/fVkZ+i4bPfboNNVekaK4x1ag2e540axkHVNcyf2CN1PPJRokIOhLG3JZN+zZO22aLw/VCsr/i9uDLyWPUb7ylWQA9TQo0JW9+KU qiG47QQz vUOvejjOXWUGJe1EMfXHWN5aHXPjjIQry/2XMZl2wpaTDdISG0Otmt2C3BomIwnorGhZashxQb//ydXjW68gIoc15sVLrCJVmSEe0wubAglSz2lzqLkCzUSGQl4YLaQZctnvE0CmvpgTEbCObFQmN9Owo+S74M/KBbBW7bDOi7mIv66g3m2M1yFbWOp9OgCeLdhdkvB3i2MStfRwrOzX02CuX9KLr2rEx8TqTLqz3q9BKWNcHBWyk2Xs4uNXUSGpVOdsFztsVtSiTGJHMbNW2YFvQpxNI9+J+B3MTMuAXqoFZYXVSmm3ilK9zdO2DhumYUHFWcx3Is1JmFpC2UhbdmVlrXVbGRzQg0WQ3De2WCeOxo6U= 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/include/asm/sev-common.h | 7 +++++++ arch/x86/kvm/svm/sev.c | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index ee38f7408470..1b111cde8c82 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -130,6 +130,13 @@ enum psc_op { /* SNP Page State Change NAE event */ #define VMGEXIT_PSC_MAX_ENTRY 253 +/* The page state change hdr structure in not valid */ +#define PSC_INVALID_HDR 1 +/* The hdr.cur_entry or hdr.end_entry is not valid */ +#define PSC_INVALID_ENTRY 2 +/* Page state change encountered undefined error */ +#define PSC_UNDEF_ERR 3 + struct psc_hdr { u16 cur_entry; u16 end_entry; diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index a1a2686dde7b..102966c43e28 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3152,6 +3152,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; @@ -3363,6 +3364,19 @@ static int snp_complete_psc_msr_protocol(struct kvm_vcpu *vcpu) return 1; /* resume */ } +/* + * TODO: need to process the GHCB contents and report the proper error code + * instead of assuming success. + */ +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, 0); + + return 1; +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3606,6 +3620,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 Feb 20 18:38: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: 13146946 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 14AD2C64EC7 for ; Mon, 20 Feb 2023 18:56:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A5D966B0074; Mon, 20 Feb 2023 13:56:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A0D7D6B0075; Mon, 20 Feb 2023 13:56:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8AE7B6B0078; Mon, 20 Feb 2023 13:56:36 -0500 (EST) 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 79B366B0074 for ; Mon, 20 Feb 2023 13:56:36 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 4CD91AADC8 for ; Mon, 20 Feb 2023 18:56:36 +0000 (UTC) X-FDA: 80488576392.28.27AD2F0 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2059.outbound.protection.outlook.com [40.107.244.59]) by imf28.hostedemail.com (Postfix) with ESMTP id 54F7DC0011 for ; Mon, 20 Feb 2023 18:56:33 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=B68GRleY; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf28.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.59 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=1676919393; 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=ogrA7qde+AG0fiChCB4rPy03CBPWzzR4SjMBo/lrZOs=; b=z42pNhHYRoMEjC43BvCCiy5L1N7vqN/sX5llfljdqMmRxrP33IEVBONFt6Nrgomnut5b61 zKpDEOXmPjh7ko/phVMcDyzd+f7UvDMxSaGA9CxvuimDizMyQeDKTaWnhp5jJGZEwTclLi JiEfZSUT2IJ2NWDNGCtQpG2vADbvzws= ARC-Authentication-Results: i=2; imf28.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=B68GRleY; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf28.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.59 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1676919393; a=rsa-sha256; cv=pass; b=stwkLoJ97wwXYi5UV0bvD6ZoZiVntVghyOyGscQ16Tq14LZOpTSs72YzooJvGKPpRbRcGn 66Yhj0Zm6HXbrJPPmNL575bOnRmwptckEEw1Rn/lH/GnvxLUhjbrrj8hgf7C1kjvDgFBfy l/ulMolcymebRQ6Eu/u21RDFFX7qAac= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PAgAjzV4VMeH0iOqWX5SBZs+tYoAFaVhM3kd/EuLG0Ps3qWfVeQh2nIXX/q3gia6tClovNmk+7bjGF8PdNfri+VVqwcowR1SOYOWpOkIdJlqJdBFXJbp/qsGaMjE/bDE8Tf4swNkk5yexq6hStMOt6ZMJYzEif4BhSpTg9sM7ANlFcbGUogHOTgSkCXm4fTgfi+YgrPc9uu2cepABT5TD4ND2bYMCQK9It1JPxm3uCJ9U7zhnJZqipWgyY74THa2GBdoeGiuKFsNZ8gSayJUQyNYo0+RsdshYmDqj2qYSKJ0ISXtUFtOEgBp8xp9D90sR6NzvsDZq1/C7xJ2rd/cYg== 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=ogrA7qde+AG0fiChCB4rPy03CBPWzzR4SjMBo/lrZOs=; b=cTgsdCXqJ+Y+YwsEkQGm1CojIwQkH90aSL7ugi+yDMeKOUb24KKj9wG8b7uBqYN6hp88MkDIgigpJLmu7sBZ+bpdt93godtEee+gwbtjTq80RosJkj7BHAMZipN8BjP+DhEBuxJmiOkAt6WHIkrp0Y6s9+xtuNtZUmk8HCDRHpn1m8A/+/PeNFYD/WsALleVKrHd7zHdNjmqTcPVttPqt5AzyoxtzVOqC+gMfJhcsxatxjUGCKNWnkdaXmEBlTUwJ/z+0aECS1bszXl4lEaxjorzxeOZcGD4Hvq62b4nWDxS2qeVU+5gN4VF4mrmiH2PY199DibjE6wfAGwmG9mObQ== 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=ogrA7qde+AG0fiChCB4rPy03CBPWzzR4SjMBo/lrZOs=; b=B68GRleYMzew51RJPazcT6+NVSwt8DSDI8rMVbKjyxQ+30+U7mg8M98tqFYSwyUjDXza+YcpZa7Az/6eSSCsm7rO1DZ5QtJleu4LljWWwceOFwqQb+atOT0p1MMZ6itKDOKVDd+Oo6K2zhx93XGI4ak43PeXwd5jL0WnxnRRHRQ= Received: from BL0PR01CA0018.prod.exchangelabs.com (2603:10b6:208:71::31) by BL3PR12MB6428.namprd12.prod.outlook.com (2603:10b6:208:3b7::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6086.24; Mon, 20 Feb 2023 18:56:29 +0000 Received: from BL02EPF000108EA.namprd05.prod.outlook.com (2603:10b6:208:71:cafe::f2) by BL0PR01CA0018.outlook.office365.com (2603:10b6:208:71::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19 via Frontend Transport; Mon, 20 Feb 2023 18:56:29 +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 BL02EPF000108EA.mail.protection.outlook.com (10.167.241.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:56: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.2375.34; Mon, 20 Feb 2023 12:56:28 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 43/56] KVM: x86: Export the kvm_zap_gfn_range() for the SNP use Date: Mon, 20 Feb 2023 12:38:34 -0600 Message-ID: <20230220183847.59159-44-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF000108EA:EE_|BL3PR12MB6428:EE_ X-MS-Office365-Filtering-Correlation-Id: 1887d59b-f590-47c6-5bf2-08db13742cf6 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: zhuUzXdSZdd6QrYrAPkyMFqRvj7kIvk1eIoqcvi69Rlg08ImjdJ7ksG4zlXNc3NWMUtremouWHuxkxyN7ljVnnMMN4jYIgni1FKQOAGntwTc+du+2wSTlhQakz/kT1sbLdhFjOplj/cpzCOosXRvXNAjQUwzN/yaZl2Ne9TvyBYIA7FTf1MdpDirqEmBmkjthgeDmpxa0YEpd+HSwh7RE6mvaHFyN65c69lYQn0BMnnfvj3wYCDrICOS6Zy9abpEU+UhkfbC6732UbOTjwrdvvf2y1NIa5lrbEwtChg3HPo9FAqZ82Mg4t6KiZaB6VRTI09NbHQqNq59zQWcc9m9/h0UdYii+0LNRfqcNDDYLwTRQUQ74CzYLXUhh2x4YLucr/1xH6OkigjXgkHeE3/0z3qEmWPjYhJQrpyPsom1Y0gmc/5bTxbjgLkirN1rdM6IdUSpU/Z2tueDTp6l4zttG6gM8XZfpH306BTrtqqYYp+iomfzzi0QR3jZOczjUhjAyQOyvuO/QeHljOPMezuH+lRhiVR0ROQYjmanvsB2rpnNwDIaIEp7cH0GSgaNQSNVcoLzWE0jb+zrA0KUTMkWpAxSGuhL7fnEiXnhWqlb+sxbXNvesy31zohRA1GHwRr3VYJSPHzMeHlbAUYs8YYV32M0VdLLVQqgIfZlD+Qpt/XYX9zTxMZ7JrckDWhQXjB6DhlFzGF636alC3fKOcMs6bAbLgeAikiwoAdI7Q+Lf00= 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:(13230025)(4636009)(136003)(376002)(346002)(39860400002)(396003)(451199018)(36840700001)(40470700004)(46966006)(86362001)(6916009)(8676002)(7406005)(47076005)(41300700001)(54906003)(44832011)(70206006)(40460700003)(8936002)(36756003)(356005)(70586007)(316002)(1076003)(2906002)(2616005)(16526019)(186003)(5660300002)(26005)(478600001)(81166007)(6666004)(4326008)(7416002)(36860700001)(336012)(426003)(82310400005)(82740400003)(83380400001)(40480700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:56:29.3440 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1887d59b-f590-47c6-5bf2-08db13742cf6 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: BL02EPF000108EA.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6428 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 54F7DC0011 X-Stat-Signature: sfbcftdx3jhkryauemec11c55b4baqn8 X-Rspam-User: X-HE-Tag: 1676919393-56409 X-HE-Meta: U2FsdGVkX1+2DWGX0nwAQHQ+4y4wDDnPD6UNNB/xYHsYRCBHSCu+RPH1P2pyVVDOjTdZnHq6uTSupMvnWi8yF8DelnaIsGzRt/rdujgCNFtqD1WqxBh7hqcA40BW6OuJ9Wd35D0rBbxnOg5GDLO1XGB6vaW2ttl8O/vnxW+U3irgJpycV9DWap7wsMxYdu2513F1JsGVVDu6qby9Cdv9U+mAd3fiSPjXWGxVnXO4zKh6NPLfjIOpLbOevjUiH2B3oLd2Aq72yxa3lFw+Epxr1pUTbA77GVQAh1VoDhxAsRhy9g6AfkEawr/pHWU6/uXfsU5GiIlGQR/K9krpLHkPsDJcidYe2mbZ6SiBLRFTqFuQu0f6+P9oBw9mYMQVvaVJ7qTc0CRA4E8llmh5LX/ZfcvNmIsHG3EidkBhR2FgFb7Mfy3MbXKsyA8KHWPavD9mtynah5edGAhO0iT4ZVnaiFYkFaotUAx13CMK/Vva8PKPaxuUiELJ8AzNzlVJle5rB/E8gUxM2GZW6MIrhaO5Ppke5z/Yy7qfCEdrhQRLrUkA/v6TY6uzmgX4jcY4F8uepRk465bYGSaOuqTvQ0GUuXRcchFZta9k/4xwaUgH4dhDmlT6eMPUwrHpgn9Q/qmJYlgiR3aFKBqUtCZCS+kEVT7E5qYq+AwvM5Ia8UkpzJeBuQRGAteucqVoQ/0D1PhKBG1SbLI4ZV7bu3zc/UKhvmJIDUM1Fb2cOSFYKGlOmC8eLIKYas9NHxzTKcyoeW8+/UsOnvYr8qRUsLOYBXByN3jZfaD2/DkrM5J54hRAFhL1sTnMaMxDQcrVlorJFcsq6pAwAjWfoHUFOEqR9HQiScNwqOHFoIYjVnuOUaHvz9Dgwp3DlFvcMDIeCpJ4ABc4XcZI7mGKf1PO21v2dkL4GnYZOY8RJNlazphwm4WXz9CLO4L4euuyRyA5Vbt9hPQasi2auGb7PBXcUzoTTES BvWuqpRx 1Y3xYsenl7f1sSMdHNgh0MnFCfL9uaR50LXTr4TSskgFNmqljeSjRoyxyB3Ew6y2XAzbF++JQU+rm+vYKrKVEmoDx3TJleOxaOyDmUpzGc78mJZcY1Q2mrltczteAa2i4xSE33RX2zoFE/Yxn+Afxu4wto9uY0J3WUp5WICYpj2+XFnJrZ3ZXzMWFS0OPe1guXNOG/ddUSsgeESeoDRYbSu7H0F95cpwg2QGkMhMLqeELQO0dChtbKG9UybFT9F1gzZBNh2af8DRE2pJpPT442LQNSXr+HIgxSCajlgicWnwwgeu8YOI9DqSs2Xz4MN2mdiWwgvPQn8GTAsNxCXBe5/3KxW18u0oY+X996Q6f0YZKDVmRCs7kkHrOhmpXUzl0WUSG97Gn/zDFe2gFK73CXiW7yeOB+sueD5DM 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 d2e1c109dde5..28b01cc7f64d 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1845,6 +1845,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 168c46fd8dd1..0afaf8ff2bb8 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -211,8 +211,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 d8e5254f314d..d7847af3e177 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -6615,6 +6615,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 Feb 20 18:38: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: 13146947 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 8215CC636D6 for ; Mon, 20 Feb 2023 18:56:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1F8E96B0074; Mon, 20 Feb 2023 13:56:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1A95B6B0075; Mon, 20 Feb 2023 13:56:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 04A1C6B0078; Mon, 20 Feb 2023 13:56:56 -0500 (EST) 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 E8B9B6B0074 for ; Mon, 20 Feb 2023 13:56:55 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id BD5901A0247 for ; Mon, 20 Feb 2023 18:56:55 +0000 (UTC) X-FDA: 80488577190.23.BE70802 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2087.outbound.protection.outlook.com [40.107.102.87]) by imf10.hostedemail.com (Postfix) with ESMTP id 87E88C0009 for ; Mon, 20 Feb 2023 18:56:52 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="ReJS4KP/"; spf=pass (imf10.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.102.87 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=1676919412; 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=vDe8g5CKLNfnRX5BP3OujC8LV1qgCmPw2a0wdR51M1w=; b=sxKIzr8DQrDKAJk92M4QIFj6bMxSFQnnDBPqtpNrA6om23E167hne1W7gFAAEgcxDcioLX 99nOEKp2KO7rvrmXd120K2G9PVxMRYWDJ+lLHUAbdYW/1xpSHkVM88PZ77Hm1UJQ6I93W2 AIQhA57ECxkS75Fn+K6zCVQG8BYP+lk= ARC-Authentication-Results: i=2; imf10.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="ReJS4KP/"; spf=pass (imf10.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.102.87 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=1676919412; a=rsa-sha256; cv=pass; b=64jEhdpRpips8x32cXWIJnhcbs4gRvAASdLDPntNZQtYsQIDfhqUi2woUcI4PXLwnhEP2f juLkweb/y8vW0PHn+rjfsV5YCwQoJiWo/p38xnkwhkp/9NtJD0voii1mb/mf4b51ZM9/Eh A9HknFAWEs70W0Z5HhmlPgmyqDgpELQ= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DWR+H9k7NaEkDNDswlWqytzOyFuxgzHkRrnjdx6RVkdkBCItfCW4CB5YReE7thhQWyN87qwwuylCq2XkZ0p2dLqXphyFzcCDdU8paRFtrw5fLgg43hXASTjBv3aY1OPq2ti0AXYsKfUrbO9Uo62mYZSZa3Icx9zX9TJJ24kZMGHNPwbiomX9XBoW2QQ13qeJonFhFblBGdLtLcYpbPtxB8RLgt3DnrVGNlauCLF5CE5MFW1WgZOqOzdhiX9AnhELzQKs4coPc94FnMqHR0eS18oYzoWRbhrHmqOh7Yzg/9WeS1WQKx1N2chEf8VtnmhKgvEF35T7dOIRNc0ZfGFZNw== 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=vDe8g5CKLNfnRX5BP3OujC8LV1qgCmPw2a0wdR51M1w=; b=WqrKqVIgzAKh+WxIrBX6y5r8jZxD6CbyXGRjwKoFl+hHGqqvtmRQrcWE+Q6v+OzWcMo+FKj6JGY5aRm416aOSeS0hE6halefBEyXyAdQZ8FUFPIOY1of9IPKrn9dRM+CZThkOZb8E9Y3OtoLjm4v+LC8Bj+BHz3zH7Dx/R+4USCTUPsnBc1qUvJtgjENPa+xaZqUbM3VyC3FdZ73yE/gpg8UwX2BdR7LxJg+VvbeCmHaW9aiLA87iuaHKzIVi5KT+lHAZ0sLJ52era5PBsTFkoy76uNeUwdhPujhCGezK+e4lRZb/a1HoGVBqRPZyArF31bsZkTqJh6RVRlCz/bQnw== 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=vDe8g5CKLNfnRX5BP3OujC8LV1qgCmPw2a0wdR51M1w=; b=ReJS4KP/a2QGuPe/xbRYg/CD+J5NNNucqadI2yiehHDO5KtGGTz7w3eVvdVwivVNlOSl2ztm4HuXsRHTBj2mBQNkSKYzoD9dKCA+qpB/dEjSBN+pBv7kmYh0vIKx7FCZb+OWbuQNgWxceReOpyOPU1yck1Dic/glpDN3qlDg7HU= Received: from BL0PR01CA0030.prod.exchangelabs.com (2603:10b6:208:71::43) by SN7PR12MB8001.namprd12.prod.outlook.com (2603:10b6:806:340::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.18; Mon, 20 Feb 2023 18:56:50 +0000 Received: from BL02EPF000108EA.namprd05.prod.outlook.com (2603:10b6:208:71:cafe::d0) by BL0PR01CA0030.outlook.office365.com (2603:10b6:208:71::43) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:56:50 +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 BL02EPF000108EA.mail.protection.outlook.com (10.167.241.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:56:49 +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.2375.34; Mon, 20 Feb 2023 12:56:49 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 44/56] KVM: SVM: Add support to handle the RMP nested page fault Date: Mon, 20 Feb 2023 12:38:35 -0600 Message-ID: <20230220183847.59159-45-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF000108EA:EE_|SN7PR12MB8001:EE_ X-MS-Office365-Filtering-Correlation-Id: e6faf4d8-d37f-4176-87f3-08db13743941 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hQZ9nMwnrwE0FZrlTBbgCPI8tzQqzUcYb1Adl0EuvzMdgKarcL8D89YkX8G9aaiX9GX3NZHzzHiER0WDmhcDL183KpfIm4bQkGfKqTdDfWV6kiCT8e40zZeqpJ/JXfS5BCbTNfvBRVujhIbJkSyKuZPIEWhSEizvOpXf3ETIe6MiUUNQhIGFKHet6DKTZP6krpjNhxWbuWXhhADhvBn8PQT0K/PrxZ1rQuEgthQX/D4Qi5DBAtGS/RLLUpwP6HJhj3x0yRbua94ADXX2oXrS6iFaobdWrBRtGOp26dvKvc99qKcGCzULWnq7xzzUqsJ6RZcTGWC7i8B7w87IL5DnO985RBE0jWKAE4MMg9eUxE+nrk1Rx4GimdvFELhHfokBwaLeEZq7za35b8qL+dzxhyFUrw3C6gnClgExxNQrK99cHnlyR5QsRMuZ16gFs8IMRLpdNZX52xjllD1CnKk3TdH4FI8RyAw5Ezxz37ltARrCQbG+34+clveN5zhGXMASRzuik44nDXv08pMqOftzUwWvmunC9Mq45iV0J5t4LR0ltBNFjAUjZL7K0LKLPdqpiBgpP8+usUjofMVeNUBX4psutURQ9QHRMfSOmpkxz0/+/t34s+7ZrZCd2D3uEZvwLVnjxt1cRr8OAeexcOHF7VNgh+z9/TVgLaOryCo2N7SfupoFwdKYXIS/rSVzydJ+Vty6T9apxgY6Dtxh9f78ZHrRTF29RqV842k5AUYJWFg= 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:(13230025)(4636009)(39860400002)(136003)(396003)(346002)(376002)(451199018)(46966006)(40470700004)(36840700001)(40460700003)(81166007)(356005)(82310400005)(6666004)(2616005)(16526019)(336012)(426003)(26005)(36860700001)(47076005)(186003)(40480700001)(83380400001)(7416002)(70586007)(8936002)(6916009)(70206006)(2906002)(44832011)(4326008)(41300700001)(5660300002)(82740400003)(316002)(86362001)(7406005)(36756003)(478600001)(1076003)(54906003)(8676002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:56:49.9537 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e6faf4d8-d37f-4176-87f3-08db13743941 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: BL02EPF000108EA.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB8001 X-Rspamd-Queue-Id: 87E88C0009 X-Stat-Signature: mymrrxojbh95jbpc6rmqd6z31odeg6ht X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1676919412-523352 X-HE-Meta: U2FsdGVkX1+71m6LIWNDvQIxALba+zKGPyIaSdZeGRSPQK6LEk7N450rQZz465Thj9aKQNZWBJxi5faRr7qgkkQiJ6F2lrTSN7HKqpG/jtPHEUQ2lqc83UPMsHyZkyj/R3HPPZggejzFC+0s/zInkw9sbnev3HocHAJfpvRsKZScTpQe5crOyWtlbQ5t4fudAC2UbzUmYIWbIIQFVCxlJ+StgRt2HcA+dPu+CVXF6WQuFRomXgEOQ9/KyPl4n5lTMeLo4I4OAkAzm69uGTs6yuWicoXEJrLY1u/5I8iDv8V0d42aamzkvIF4pBJtkl95KbKArNQopxZbhlVhoO8UNoo6mkeD8DTJVVLI46Pz9rrJY4I8JO/V07L8+A2TVfEBlfIL495koPcMsEz3nXq2uMEhBoKVr86q1PjF1nylnk4zJq31HObvDC3wWE3fZXLA2NbEhgKdIwepRdCq155mb8VOVH5ETKOHqbR2yEmu+NOACUHt8eelUbfu12lXtlEDhTADlqhtBwQ048mp0I53i6vvCGSrK9vlrzjbsiwZWBTS9wrPFjGcYuZcilZuXcTdDZ624OTqflZ25OCkobHSpvWobo33Q2c5Y+eAedKvm4YmSx7UY2tLFntbNAZLtQfw2ORZOl/HfMUvjp7xG03jHff+aUo5sf0Xm/TpjduAKrah/0fdJp6AJP+FdzRTjJei5SYaEQZpuqebXM1zp4AuMkq7dgtpOhv0y1xo1fDRhgEmSVBkiMvnyFfwuDTvSTq6kccwzrK7bFpuQVf4cSQX1H6fbEAKZ9UH02VHSkYSyFhqwRpkelMdR3NldGOcCvJrBwqpAhGrvc/F4ernhEmRUn0JDzu/QIDe8D8cr3pq1ZipfNcWQPOKeAb5XLKZn2nvAR3dcX+Hn+n0V64qRQ57L91iyMqH5CCFblGtDaJkJGy5E04paPsboAFpZ1YYy56YWAMAbMnIhJ2Uqr0Z2Nw HiRKJUSP chefGInhZn/3lKy0SkH3pJNlrf0C+O8yN4vmbONIrZL7yu2Ij9RGH1NpXf0QF8/K4yFrhObu2UvUCeve2LZ/YFL9ajOHpZsIRqJJFn6PQDBrytoZyR1XF2e5irT389b3SG2CR+q12nOsCphyjVVpxszpENKDJa4N+uB9UdOh/YvEuKV5Rt2dx5zRKmm8aITfz/Hd/1bnt6BogL9pScobSbYE6GOF7WEYcCzGpMT4nMOW7TfR+cGGhpC2vzb0t9lIs5KHa3QXEXjyVkbkAb94b3laIT2+7vY8SmJ5VNQ2IxIdumlx8jD6fYt+9aQ78ucVkv8164mL5h4/zU5w= 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. Page state changes are handled by userspace, so if an RMP fault is triggered as a result of an RMP NPT fault, exit to userspace just like with explicit page-state change requests. RMP NPT faults can also occur if the guest pvalidates a 2M page as 4K, in which case the RMP entries need to be PSMASH'd. Handle this case immediately in the kernel. 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 | 84 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 21 +++++++++-- arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 102 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 102966c43e28..197b1f904567 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3347,6 +3347,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); +} + /* * TODO: need to get the value set by userspace in vcpu->run->vmgexit.ghcb_msr * and process that here accordingly. @@ -3872,3 +3879,80 @@ void sev_adjust_mapping_level(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, int *le pr_debug("%s: GFN: 0x%llx, PFN: 0x%llx, level: %d, rmp_level: %d, level_orig: %d, assigned: %d\n", __func__, gfn, pfn, *level, rmp_level, level_orig, assigned); } + +void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) +{ + int order, rmp_level, assigned, ret; + struct kvm_memory_slot *slot; + struct kvm *kvm = vcpu->kvm; + kvm_pfn_t pfn; + gfn_t gfn; + + /* + * Private memslots punt handling of implicit page state changes to + * userspace, so the only RMP faults expected here for + * PFERR_GUEST_SIZEM_MASK. Anything else suggests that the RMP table has + * gotten out of sync with the private memslot. + * + * TODO: However, this case has also been noticed when an access occurs + * to an NPT mapping that has just been split/PSMASHED, in which case + * PFERR_GUEST_SIZEM_MASK might not be set. In those cases it should be + * safe to ignore and let the guest retry, but log these just in case + * for now. + */ + if (!(error_code & PFERR_GUEST_SIZEM_MASK)) { + pr_warn_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_restrictedmem_get_pfn(slot, gfn, &pfn, &order); + if (ret) { + pr_warn_ratelimited("Unexpected RMP fault, no private backing page for GPA 0x%llx\n", + gpa); + return; + } + + assigned = snp_lookup_rmpentry(pfn, &rmp_level); + if (assigned != 1) { + pr_warn_ratelimited("Unexpected RMP fault, no assigned RMP entry for GPA 0x%llx\n", + gpa); + 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 9eb750c8b04c..f9ab4bf6d245 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1976,15 +1976,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 0c655a4d32d5..13b00233b315 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -714,6 +714,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 sev_adjust_mapping_level(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, int *level); +void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code); /* vmenter.S */ From patchwork Mon Feb 20 18:38: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: 13146948 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 43A8BC636D6 for ; Mon, 20 Feb 2023 18:57:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D42A26B0073; Mon, 20 Feb 2023 13:57:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CF3456B0074; Mon, 20 Feb 2023 13:57:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B943F6B0075; Mon, 20 Feb 2023 13:57:17 -0500 (EST) 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 AB49D6B0073 for ; Mon, 20 Feb 2023 13:57:17 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 7E8AF1A062C for ; Mon, 20 Feb 2023 18:57:17 +0000 (UTC) X-FDA: 80488578114.07.8670097 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2075.outbound.protection.outlook.com [40.107.101.75]) by imf28.hostedemail.com (Postfix) with ESMTP id 786BDC000A for ; Mon, 20 Feb 2023 18:57:14 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=ANa+rBtO; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf28.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.101.75 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=1676919434; 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=n1RRgjOiQpLZ01liN8EP3I2nWld/+bK90dZ8xsXbfg0=; b=MSM0+C8WCpidxJmjHSEKgkQPhjQ8iBGydQ5fadpVXwjQtc6IFfxJlHZImzWkHAXs/DUXDP nKHDkw9VYoZnojxGyrzVcO/ixj0TT0f1Q5fJZ7XCl8KFSHVd9kRLc/2TztQ1YhCeB82HO6 9fJNmSBqY2Teiz4j24Eb0mZh31QcEtY= ARC-Authentication-Results: i=2; imf28.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=ANa+rBtO; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf28.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.101.75 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=1676919434; a=rsa-sha256; cv=pass; b=C6VokSAlEV5v8v59es3xYAWWe7QClVL062W5VPnlRAsfweCHMUWpSc6ZObSTeOWI3lgu2k eNgCKIVCWMNaaSNuaOH0LakdMcQ3HVOlu/dEqW6GmKO3EvUAtD/YVXyaCapmsrJqAP3n+L /M01A/S9ZCWqrHvXx+uknudYLLjvuyY= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EgVTAgi+BvMjNS8x7+bNAsLn5LsX6cjQNXh+5hnm+Wa2fn8GKKuIFg+KsRvICrsi27+6IiB95RWsbp/Nf8Dn00ju0Sitq7VwbSwpGTwvzVPlr8iYF0vdjKnbal+c+UyeskpxU/7DYOTzMmNGUDIlCWwN0Sxy6aSr0onG+LVlKAVXKs7V7ujVyUQVvpIkn9C3gYqpTzo9L2S0Kkb18r2X0yNUfqPPejxYiX/PF9c0C5FDDDZUgJVLi9NDuM6be4k1eU4poKxCHQmFbqB+ws4n6SzzTrW2nNtDl+RDH81x2xDJjoRNSLQVT2jYcRwooNRhhBlL3HU4KxAPCPSR1yLeug== 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=n1RRgjOiQpLZ01liN8EP3I2nWld/+bK90dZ8xsXbfg0=; b=TXx94yvyHe00XP83qM9vmdf069XK8xgKi3mu1T0X3c/6QQYJJ4dAqoFdVyp7HegKhlswaEJ92Pn5R9+0uMiE0JjKPpJUprUuKQxgM3tn+2TQbd/XSdxl9LVyvQnTWWcZEnSLDEvdl0bKEDQzQjdey1nFz9icYdfwj3f62KAxsX4+5ibQrEGx6Bvpl/5WnjCKi4j73SAyEvddzGpzu4Zkfjpxup8JNpr9L3q+LAZP9qewL8xalNUDrDIRJ1SYzUczMBXcfwQ2K1uUvSyND1pW9955ufj6VAM8P7rhs6jIVCiSFvQruZXoIS6Ue+3SmIi7AOgcL9AJ5hrFTrBSFoYQzw== 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=n1RRgjOiQpLZ01liN8EP3I2nWld/+bK90dZ8xsXbfg0=; b=ANa+rBtOjgJFCvgYhsNra63laCUt0BcdCUrUgm7cgdJg42hCv8pgJe+MU4wouA7hslGkEV4yUKNhCrlimR7jSaBYZBfgMcreK8Sa2rAA/RQlWgUVCbspWhhaDRoqlSIrd8RbRuw/Kh9zT9XeCIdM+ekdZHcFnWNbN+rXFpZpfv8= Received: from BL0PR01CA0022.prod.exchangelabs.com (2603:10b6:208:71::35) by DS0PR12MB8269.namprd12.prod.outlook.com (2603:10b6:8:fd::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19; Mon, 20 Feb 2023 18:57:11 +0000 Received: from BL02EPF000108EA.namprd05.prod.outlook.com (2603:10b6:208:71:cafe::d5) by BL0PR01CA0022.outlook.office365.com (2603:10b6:208:71::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19 via Frontend Transport; Mon, 20 Feb 2023 18:57:11 +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 BL02EPF000108EA.mail.protection.outlook.com (10.167.241.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:57: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.2375.34; Mon, 20 Feb 2023 12:57:10 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 45/56] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event Date: Mon, 20 Feb 2023 12:38:36 -0600 Message-ID: <20230220183847.59159-46-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF000108EA:EE_|DS0PR12MB8269:EE_ X-MS-Office365-Filtering-Correlation-Id: 54221cbc-b5c3-4812-b00c-08db137445e9 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: gLmuCbEKzaTuth+0yRZxIwlvWr+NdutP/188OE1HqbeEZ6Vs33rM7IXyRxl/+75fTpA9U5A1sZXOjabmH/d9cNxM51O4E6qErXlt5jWhh4ZxKSaAjqIVWGJOvld5Fgiy95rbzymc4S93aQVi4Hd6vUMSRdJLZXNFEStmte9/YgWYbUOlarPnlrZZ4G76QZG4FkIGNH+jyoZ2RlUn8W0XsH3oI7MrVeUDMc6EBUuFWcRTRshbaa+biHoulSPPz3NokJVlmd5RCSUEfPbGt8/gW2o4yYJii78qek1pL2oYQwOf5IaK55Lr9xsx7jTS2ErTO9a9EjjAngFntFRaUWh4WlVT0EsCicok+z0zun2AhXm30OMGgT46BnsPIPFhxgOPZiNbDfyhDwHkXMgAIBT2klZx6jJ+k+Yul658c8C7qWFke1KVdMLXMTyVNhAxLuCLvCzVCwH9P0KoKral+7kQ7RpauNpN++9uEjeonh01zB5+8JkY4AbFmkHUpVaX0yiM84//4yDkns0gh+UEW+MK6NBk8QourTaz8yKUkqXM6X6QME+mmhlUTw35TUit7C+sunYRtH3NMKy+kTbUvwj5iWHagMW1b/vJnC7aOIL07aX9FXe5qWyA8uj028qliFMhpUVidhTNgO/495f3+aR8hh8SHSrz0USDPXGu56P9PACfwhjwiO3WFVH2ERJjoA7LxUdCUAm+qAlly6ykNdmUEI1wXTyPJnW+oNhmWL3p4ak= 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:(13230025)(4636009)(39860400002)(396003)(136003)(346002)(376002)(451199018)(46966006)(40470700004)(36840700001)(44832011)(81166007)(5660300002)(36860700001)(7406005)(7416002)(86362001)(82310400005)(2906002)(82740400003)(40460700003)(356005)(336012)(36756003)(478600001)(47076005)(186003)(426003)(16526019)(26005)(40480700001)(70206006)(54906003)(41300700001)(83380400001)(316002)(4326008)(70586007)(2616005)(1076003)(8676002)(6666004)(8936002)(6916009)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:57:10.9540 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 54221cbc-b5c3-4812-b00c-08db137445e9 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: BL02EPF000108EA.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8269 X-Rspamd-Queue-Id: 786BDC000A X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: pxtky1jgtzhp7qzih4jfws8twyqjmn88 X-HE-Tag: 1676919434-889733 X-HE-Meta: U2FsdGVkX1+WW2CaGDucE8uI8b2kde6ozcH564mTDt2jujn7tAj9U1Ag/I11lmmHojyf8NvSSnJ6cOnOe5KcSSvneJrfoiW+n+vI0W1LMAWWWcmw9RHpD8gWwqz5Wmc0nUKS4mm1ojVXs08vIhSRgKXNPwpanMhzjQZ7gyVRIBg1QB2liWHiaLYucQLia+zJQTHevKhFhZRjo3/7a8wOP6r+rJ07dAJxUDbWS+/VOovu/FpGHx+8hYGQ615J4m3poEILfCx+97qfj37DAe0iGPK0Biw3q1l8cm/xiipr8CNajf7wnW0Alti6JuTJNQ4W9dEqDyT5l9a8MEgkNTqfA8me46ORIrNUonHDzgseWHovoulBRQOLfImlERHsyCF2zKXBUah2FbHXCEui/NY+6x68H3y8UG9/igh7qerIskCgf2Vgj+wCSYpglZ3bGXCnNE32CPGfN3UJBdTXRtb/tDVr0c/8MLHz2iEJR7kY5DwKniuQv/1hN/gKI25Y0PXu6a/7avO6MTccGjG0BTLKirNsnSCaWTOI0pSLkaWbl5sxdjuA40H6lePSAsvKSXehtYnJB3K3khPsrKGhIKlJCHnj8CJv3Jt5boJOtbR/fSHkXEhlPHR9t7EUiF8MDLg6504+rBarN2KxmPByE8QqBR7CgbfuEM3M/j28Yy9yGdQ1NvsHtXocnMt2GsFR5+SWJ1Oqk4VXJBX9MvPp55Z8TZDmIBuP+zZ2Y7eNbIpSRP8sIP+/xsh00cc7WTVh8sH1/9jHw+BXQEpHT52Z1PGFlbDZH5lD824OY+7OL3ds2xHfh8W6aS19Ino1u8+mrMyU3FyGYvEWFCT6Fqh4EZCQ95454de/IvHQYffbVOtitcmossUmz9I5WIf4b8CIslX2PBsMa4PiR6iiX39LpwMqbJgRwjYGQy8aSwvvNizsQEk7IBWyFhghRjRTGmrO0iGU0YvcnykEyGgfYxfUdkM JZlsN93z mOgST5u/QWOGKEdQJ5BvaVjIqztreHzwZUWgIkurNkIP1gmpetA12jHtz5TEecliunEybbf8Q8oTUpe9TKmarAXMmzn3QcYY2rM7y/KrSOtXhZ0Z9hAHg/cYNF7gjc0S/cXmMzCXpE6AHX2VjLOhxtXiHEmqWdndm/9Y8/E/iZv/e/C27iI5WTa0tE096nB7DCGeTcg+DHuqpi9H2xF1nTeWi0d3gZAczg06zthCdtHCM1TgLoaTerKnGh5Yj25FY0DkwCX32VaiA0CBP/dZhQ7M6Uygu62JR9aEOb6P71Z+Gr2uMzEylLQI/jbNhkEgwFZyy813v3zlhMww9x8aVRcTFSse9j83JxDaKYSZln0ROw6d3Gv2fBmdXIg== 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. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 185 +++++++++++++++++++++++++++++++++++++++-- arch/x86/kvm/svm/svm.h | 2 + 2 files changed, 181 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 197b1f904567..92179614102e 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -327,6 +327,7 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) if (ret) goto e_free; + mutex_init(&sev->guest_req_lock); ret = sev_snp_init(&argp->error, false); } else { ret = sev_platform_init(&argp->error); @@ -2059,23 +2060,34 @@ int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd) */ static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp) { + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; struct sev_data_snp_addr data = {}; - void *context; + void *context, *certs_data; int rc; + /* Allocate memory used for the certs data in SNP guest request */ + certs_data = kzalloc(SEV_FW_BLOB_MAX_SIZE, GFP_KERNEL_ACCOUNT); + if (!certs_data) + return NULL; + /* Allocate memory for context page */ context = snp_alloc_firmware_page(GFP_KERNEL_ACCOUNT); if (!context) - return NULL; + goto e_free; 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; - } + if (rc) + goto e_free; + + sev->snp_certs_data = certs_data; return context; + +e_free: + snp_free_firmware_page(context); + kfree(certs_data); + return NULL; } static int snp_bind_asid(struct kvm *kvm, int *error) @@ -2693,6 +2705,8 @@ static int snp_decommission_context(struct kvm *kvm) snp_free_firmware_page(sev->snp_context); sev->snp_context = NULL; + kfree(sev->snp_certs_data); + return 0; } @@ -3153,6 +3167,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; @@ -3384,6 +3400,149 @@ static int snp_complete_psc(struct kvm_vcpu *vcpu) return 1; } +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) + /* use the firmware error code */ + rc = err; + + 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 kvm_vcpu *vcpu = &svm->vcpu; + struct kvm *kvm = vcpu->kvm; + unsigned long data_npages; + struct kvm_sev_info *sev; + unsigned long rc, err; + u64 data_gpa; + + 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)) { + rc = 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; + + rc = snp_guest_ext_guest_request(&req, (unsigned long)sev->snp_certs_data, + &data_npages, &err); + if (rc) { + /* + * If buffer length is small then return the expected + * length in rbx. + */ + if (err == SNP_GUEST_REQ_INVALID_LEN) + vcpu->arch.regs[VCPU_REGS_RBX] = data_npages; + + /* pass the firmware error code */ + rc = err; + goto cleanup; + } + + /* Copy the certificate blob in the guest memory */ + if (data_npages && + kvm_write_guest(kvm, data_gpa, sev->snp_certs_data, data_npages << PAGE_SHIFT)) + rc = SEV_RET_INVALID_ADDRESS; + +cleanup: + snp_cleanup_guest_buf(&req, &rc); + +unlock: + mutex_unlock(&sev->guest_req_lock); + +e_fail: + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, rc); +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3633,6 +3792,20 @@ 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_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: 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 13b00233b315..4a9ffb7e5139 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -93,6 +93,8 @@ struct kvm_sev_info { atomic_t migration_in_progress; u64 snp_init_flags; void *snp_context; /* SNP guest context page */ + void *snp_certs_data; + struct mutex guest_req_lock; /* Lock for guest request handling */ }; struct kvm_svm { From patchwork Mon Feb 20 18:38: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: 13146949 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 EF562C27C7C for ; Mon, 20 Feb 2023 18:57:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 94CA76B0074; Mon, 20 Feb 2023 13:57:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9236A6B0075; Mon, 20 Feb 2023 13:57:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7C3F46B0078; Mon, 20 Feb 2023 13:57:40 -0500 (EST) 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 6D75D6B0074 for ; Mon, 20 Feb 2023 13:57:40 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 42ECD1A04D8 for ; Mon, 20 Feb 2023 18:57:40 +0000 (UTC) X-FDA: 80488579080.15.5B4FE77 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2075.outbound.protection.outlook.com [40.107.220.75]) by imf10.hostedemail.com (Postfix) with ESMTP id 556F7C0007 for ; Mon, 20 Feb 2023 18:57:37 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=CpdKZCb3; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf10.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.75 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=1676919457; 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=BwVXnDi9jaA4a8BdNgxHpkNnSMn7CrcmD4LD5ZDYAv4=; b=xEl0W2fR+4TgINIGOCuDSWLiPq3rVOilekCAEAyHzAReOMScXfF8NVrN0GOFquziOF//mg MNT0rcR5+V0/npMNpU6MeE2H8iEGN2+/0+bmctA7OCAhGdWgkFfJszGf8BGjcbqAWSas+5 nK2ZDxhWfJyUMaENYLjBrgubo3pjKKk= ARC-Authentication-Results: i=2; imf10.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=CpdKZCb3; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf10.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.75 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1676919457; a=rsa-sha256; cv=pass; b=uBfIspHxp/y81LK3oziXBBqcWaCVUxc9FlROKy0eUnb8fUUjdHruqV75NZyoUI1i9E1Y7r hrPKNuN9dPjWZtJUPA4HzNAmKC+dSb6vK4KJtk9pkFA9eNxaF69ZBDtw1QGXn7MlPQlLVL O0oBKewcdM2Hbyn1Jk3+IAUTP9hBY10= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PDrNglJ0xX+zgLTD59mSx1wmnALZDybesPSuIBlYzitQGXvAdvvCCcdYFxI/6Dr2lhVnqZPn1cE8lnkH/WIES8eTvBbZ3aQicnOKaHB+xiX5vExO+Q/IZMn0q3m2kmXBczxLXBwZHFxY9qjUJ8hGzsd5ft6U7k3ybKEdRqsDqqmBRqjKkrYiif03lKsWS+IagQtZ+ILCgZHdqa3/a9Txa0DbCI87jZDUxDPp/DI7mguVgaqqUFInPv5v9NtSScUQPWkFOcpkd+N0sst7KiezWOU9j7ttkvQgSui8SpvighvQIDorhtntfCLG0wlMgJA7XqGnLwsOM4DjuDiVfbJV8A== 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=BwVXnDi9jaA4a8BdNgxHpkNnSMn7CrcmD4LD5ZDYAv4=; b=jycO1AGj8yOntyPS8sC7YAhw+aOEiOKNvcSTuMBRzmUFoWOlEK9zywiP1QlpU9jbnmn4I5SyDxVxP0NwUav8UkFD12fRAqC4cbP/NsdzJp9yEYqCLHDDHeHTswY0h/uh1mXheVTdP22bw2mVA2XsAfTh7301UM1wR2PpukZpaDfTYqJ8Tt0h0Wa7SPqLjSMbkomwVmYMftqaP4HcY1KrzLDfoakBQmPg+vb8fkXLK8cdFk7RsohR5klcFyPMotqHPTrBG4kjCi0ZoW5ggohE7gb8CfXlzVpiYn/WYtDC0kdxGBSiqniNESxWhHy87TjHOUZdWQF69XYAWdMAU54nUQ== 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=BwVXnDi9jaA4a8BdNgxHpkNnSMn7CrcmD4LD5ZDYAv4=; b=CpdKZCb3MWz0LJjObZyBRJdvEZt0gYkfYJlOWMtpTp4Y9glzScmTUGFCELmTJOUWaYIYnf0wG8YDWfHrx4wID8R25GvKevV37WOqYz/ncrYkqoYuwMUX/or5Xf1wM3au42yMMz7smX4NHB76n6tOgpnBIqp2blubyTTuQNsaDhw= Received: from BL0PR01CA0019.prod.exchangelabs.com (2603:10b6:208:71::32) by SA0PR12MB4557.namprd12.prod.outlook.com (2603:10b6:806:9d::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19; Mon, 20 Feb 2023 18:57:34 +0000 Received: from BL02EPF000108EA.namprd05.prod.outlook.com (2603:10b6:208:71:cafe::99) by BL0PR01CA0019.outlook.office365.com (2603:10b6:208:71::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:57: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 BL02EPF000108EA.mail.protection.outlook.com (10.167.241.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:57: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.2375.34; Mon, 20 Feb 2023 12:57:31 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v8 46/56] KVM: SVM: Use a VMSA physical address variable for populating VMCB Date: Mon, 20 Feb 2023 12:38:37 -0600 Message-ID: <20230220183847.59159-47-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF000108EA:EE_|SA0PR12MB4557:EE_ X-MS-Office365-Filtering-Correlation-Id: 84fdfd86-3f56-46c7-f60d-08db13745371 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ntxKMRTEXGB1icnLd1eH5EPjxNCt3N1sncm2ALlg0usUPaCz4o5e6q/dljRC/kJ7m58BJmThAGPaQr8hZ2adFYBSXfTuDxrqQH39lxIWMmOyC/Rp/40NoDOdJ6JdkSIVhsP2NGvYyPY35Dn0x4YtdWQv+Gb6+RyRpjTZwWxw+ofeYb8zLV2MAcmFWCEbAhgRCUlHKXt7UPFBBWE7dL3qHpY2iu6TvygwTx6r95UBCkWJqxOFYLfk7b0pKjg6vwO46oY9Iy91qjQ1I24prgSNQhcZkmI1/z6TH9p6O9MsFTQPjE5+vudBzdBn04ylArJ3rIMmMmgMczyTJO1QuZkgwKtZ6hgtbFMJpe3T7y8/CVz5g91wAHAucKPEYi1zqvAf+0R/0O3PX3RCHCeFw5X/TC05H2+xtcwTvm2PzOfMaa5HKa1vI2TbXTZzwPO0h065eLCqCwr2snsv3E4OFj7zssfaeP8dW6kO9U5JFd8hgXAeoMIthbsBVuadSS562zE+DgeE7wARCzxEyv1fQwkFWXAWYfA/rpfS3kkoRVHsDa+/9Cl4+Nd9KH4o5leRSRwpy6UsTXAtWmaVpTjuNzMkobCHnri94yNaJeXteHEUeJOWDIMZr7lT9l0it79nuxfGHDUehDHO50+k/0OJ+mUUqbckXfS0MDABGfRnTUg2JlsF2UtY6sDyhSUEUvhBzLfjRpSWSbFgguuU6AwpiXLheVD17QSL7ZFdxxAlPCqbX/Y= 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:(13230025)(4636009)(136003)(376002)(396003)(39860400002)(346002)(451199018)(36840700001)(46966006)(40470700004)(36756003)(40480700001)(8936002)(7416002)(82310400005)(44832011)(356005)(5660300002)(7406005)(86362001)(1076003)(82740400003)(41300700001)(186003)(4326008)(26005)(36860700001)(2906002)(8676002)(83380400001)(316002)(54906003)(70206006)(6916009)(40460700003)(70586007)(16526019)(47076005)(2616005)(478600001)(81166007)(426003)(336012)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:57:33.8137 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 84fdfd86-3f56-46c7-f60d-08db13745371 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: BL02EPF000108EA.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4557 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 556F7C0007 X-Stat-Signature: bfdo1exrtmnf7uimuyocwxu1yyrqhrsm X-Rspam-User: X-HE-Tag: 1676919457-533138 X-HE-Meta: U2FsdGVkX1/czhyQRLN+TGI/ppVPGUQM60zrl7rfh+e6j9EZCXQSR6TcuA/q38Qy5EqRUODp031V50pLQMhPe5S9l3Pg9F+xDiGu8h7hhyIybGXN1Foc91ZXMFwn5EDXxXmUSxc4L9el9w0czrsOkZv0N4W1rNvm46C6fkiHFIkdPQPfyfQhEFmQGCy13AQ/VRak77lbeZL90DEywOer0C6nAOD18fbo0EntybFriz6KjPJgEpTgne9PLATjlokwdmkv65tTwWVsgXS96TPOxCA9jIbpPZApdKctI+jSQc6/brcUpuSVC+ynZulCYjq+kZSTdO+skpn0J0m8pSeAqTvFnJPAlKQBJCNldIJ0+yfXVxOrIJteTl9sRrG55jGQMhjCbVhIN33wx+QGd26+kLYTW5aYwRnnm1x1a6cHZ87kk71qn8EMbHF9ipAYIgxybd+ycaU92ArdXAs3Tbx9QY/dZOObk4XgzdXsMMF9Ka+dTopyBs+DorZvUdedu/WhaphjxgcRoH3Q4qrMXZ5rqMawBOuvOVMpQ9cJ2NoIKICYCJAtC+mcFVtWsUgA0elniR3t4RiSsxJNF6XRKibQz0pmDLQ7QjrNI8fFeaSjz6nAbVB3ptuzm+FMygAkw9R2ZWDr9XW2WG+9dh6QO2Fda9ZX8U2477WUFiqw9U534LcCFTtUBwIqmZOLNC/goxZdJXsdPaMpaEB/XY67+/uJnt8diKM1nLRneT/uO3qMsf/OVsRSPV4hhX4expFo1sDIna5hJHRRc21j3ga7NlMJpK32W8XXUL6Bz+vysbgZ5tW6UQOglEcSIDi3ZPZLr6zCND6jY5k6r0Wge5cRDKnaBJVTgyX00NmjhquiFmBbuRC69BnzYOM3ETGXwu7AxBYFw+NnvOKSOqhjaDMyqJ4m3fx6aeeSfhq9fZJagbtuNROO3E0s0RtbN/55ZDIo4a2lbSvps6bfIC2CT9h8bXn YCSV+wIr lSy+JGWxGJ+5GqK+tgrEmSxwed7tL4cO5tDtvMhB4ESRQlt5DUu/c6HCv6RTtLie9/QZYt3tH7bOCMiQoK46MUU/eoh3MbeX80W03yAzwrC1kcQYsIdqjviGq5donHsrBYGKZXPib+1StvUdeGqgc01bGN5Ca0inuwQdGblie8dkZcgpVL4JLVsIJm5Ezx7bYW94Sf3XwMCEdVURQGRXkvWV8TzhhwVoZi0C+j1BWxRi6N6MhpVONJ24ZhaooMrV8GZXb43J6OGMilmAtYNh7mLH9t3qVhexj/DIa0BDVlRM3TTjuoe23iJoTTRMT5542JG9MP+vWNJuxQbY= 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 92179614102e..6bec2712ecc6 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3849,10 +3849,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 f9ab4bf6d245..745f736d9c98 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1410,9 +1410,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 4a9ffb7e5139..b6ca6657aa6c 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -198,6 +198,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 Feb 20 18:38: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: 13146950 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 6811EC27C7C for ; Mon, 20 Feb 2023 18:58:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 072276B0074; Mon, 20 Feb 2023 13:58:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F3CCF6B0075; Mon, 20 Feb 2023 13:58:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D8F516B0078; Mon, 20 Feb 2023 13:58:01 -0500 (EST) 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 C1B736B0074 for ; Mon, 20 Feb 2023 13:58:01 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 8AAD512011E for ; Mon, 20 Feb 2023 18:58:01 +0000 (UTC) X-FDA: 80488579962.19.A052A42 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2080.outbound.protection.outlook.com [40.107.94.80]) by imf08.hostedemail.com (Postfix) with ESMTP id 61CF9160008 for ; Mon, 20 Feb 2023 18:57:58 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=RZO07Tz7; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf08.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=1676919478; 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=Z/JaN4o4GcF6leK1nTsDeWGcfPK3zSjcGLL5oef7NBQ=; b=3jKbtakUVhodvOKfhzJvERLtm2YOmZfmrYd81idS1WglifPo0CwGXpLIEL/7JmnyYjVm/i q/jlU0ak2mRxYnxDkhGTLkKdEUhv9u5U7tmyO+CmWfH2eBXjnfx4jIYm/Vsq9zht9ukxwf 7mT1YJbS+421ocKx4v9/OmJRZmi0af4= ARC-Authentication-Results: i=2; imf08.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=RZO07Tz7; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf08.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=2; s=arc-20220608; d=hostedemail.com; t=1676919478; a=rsa-sha256; cv=pass; b=sSECUUV9JmU9XmytFkJNrKFl/ZF5WyOYO0ekGobad2WnsOaouMNqXcauw5H352l4RRzgph qbB/ThxEiUCazXqhCNjpcNzbmzmFsTl2a+9CxSAxPAo0m7kwnjiKlwQM+NjPFLmJhSfl/p /YgsJsWVZqkRknt9KRfITiFvYwIF9cU= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=G3/6td9cekTxOFdFxInRjniqfDUphz82Ckwi0T9opZ2Ku2QGL7h0deIDEl/lGl/Yb7QtBlZE5AYu6bYRDlfhuWOnLmvebs6OH3SepFoZmJMKwgSZdhersucsL1fr4iJjfFgYNQKp3cC6+9PT15HoUIq4/v2UuQRvUGCWIuzCevyDkniYR/0D7zhRnmS3dp4l3TF7/bYzrEEUPWyvsfzbtJ2Hc3P67kg4d6L2A6s7WNgIj8bQV4WY9Ul9wDLOgwrBsEiCr7WAJvb2eWPne3+HSmUmg3mtpUSKrDel1HH/PKXJENT65eMJCltNhtVsYJ8ZYmGOn9kbOnc0/GC5dhSZsg== 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=Z/JaN4o4GcF6leK1nTsDeWGcfPK3zSjcGLL5oef7NBQ=; b=MuAHSK6W+GX/Jool9aczJk+rvkUmtR/4P9agd+fhgwPXShq380PCfR/0xRUFTYqqJbJbg3L2n1j/gQf4ARqaKPcPr8ZkmyGOL6WBpm7kCTPQGFO2cYRTKcEkC+1nuwEpIMqxrNRboPyBX8ar0TQYhonozuFvDa3xH/O0fBWYnBebhDUi0BwDVxi9b38LFGbOWkdaNSCc5SFnvaVepM8px//JMyq2/UHOCTLdxMF2vuPXpZIe/PyZwuTwgoo/7G09cadVw0z1RDbS0BJLZK/YxNgmgDI6XI9Zh9e4m2NHdnoXFTOxFBTnqUI5XEtJxe7pQH4cJohgvo2FvqyoRfMU3Q== 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=Z/JaN4o4GcF6leK1nTsDeWGcfPK3zSjcGLL5oef7NBQ=; b=RZO07Tz7XYDsbnyPuhvTYo9OFHY3PXKrR5Fm0XYT3aRHn9TcXJyRwHCfAQdVfTLS6z3w4wA5e1QTIz1YM62kTtMwUAkqLOf7/JxwZzYXqHfo2vHv5jL9NVCeO6q2JBrwwKzyrWhHdDXjNgAlQEjMPWH8mBaFzP5TUxc0zF1xT/0= Received: from BL0PR01CA0007.prod.exchangelabs.com (2603:10b6:208:71::20) by PH8PR12MB7349.namprd12.prod.outlook.com (2603:10b6:510:217::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20; Mon, 20 Feb 2023 18:57:54 +0000 Received: from BL02EPF000108EA.namprd05.prod.outlook.com (2603:10b6:208:71:cafe::f7) by BL0PR01CA0007.outlook.office365.com (2603:10b6:208:71::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:57: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 BL02EPF000108EA.mail.protection.outlook.com (10.167.241.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:57: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.2375.34; Mon, 20 Feb 2023 12:57:52 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 47/56] KVM: SVM: Support SEV-SNP AP Creation NAE event Date: Mon, 20 Feb 2023 12:38:38 -0600 Message-ID: <20230220183847.59159-48-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF000108EA:EE_|PH8PR12MB7349:EE_ X-MS-Office365-Filtering-Correlation-Id: f9561e8a-d814-403f-9df9-08db13745f37 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: /sivMQrKvsJTGUmIG3kvcy41ETPLPR6Uq+SFttuVWbzASIG0JgPYQe9dV688qAISLV7efVIxyG0hgunIWYGldpHhI9K+w5zphkXZwYBKoz+h7nJG6elKK+thAkpcZeUcsOSfixxb1OC1RWvZb3VlTfeD15I242EsABNfC1Sk3Q+fR10xZpb011iEbbgF/IZ6tV91mQIfrZAFbDAFcR5jQE4wLGVE64Gb20qRTrL5NTp7YJc59o/DxiV4EeX1ljaayLjoUAwMqMzDEgxVIZlIhKOYDiAGCXTeNIxIQDxOQlC9MgOkcUtxeKck77VBdJ0N5xk0zbCoBah9D99+2/RiltozSdamE8PCkw67X1X6uIJBQUGDiEpVVb3tm/wgK3s6l+MsSEdeJ2lYnYuEFvUxiXBFVVKmsRj2JNBcV5hpE8nKewXj1d0EjstFbMUUl2pRdXjBX3NG9S+IptscBRAoHFBfzCbFbaQo0WfTDIODLx9RgxyskjR09V6WrSsZkHbf+PShuUQOhUUJFd33Zn6XV+P08XFjdpiknpZkTvpw+OwYxXQRNtPAUs5QMxdcBkWRTK1hW+vvVYdH6D6oFmqy1PIwE3x3ytVBY5+CAxgWukLryuJE1s17pLa7SNvvHKP9VX+NIrf7Ml95rtzWSzkirg+27/mD8GvQdOiKULTR1sRpWN0op1kFDd4a7yiCN/FIzy4Fvg2sgLB8MRw2H8UWLlQX/7jG5VAEa15ZM82yBUA= 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:(13230025)(4636009)(396003)(136003)(346002)(376002)(39860400002)(451199018)(40470700004)(46966006)(36840700001)(70206006)(40480700001)(40460700003)(6666004)(8936002)(30864003)(83380400001)(7416002)(7406005)(5660300002)(41300700001)(47076005)(44832011)(426003)(86362001)(16526019)(26005)(186003)(1076003)(8676002)(2906002)(70586007)(6916009)(316002)(4326008)(336012)(54906003)(478600001)(2616005)(356005)(36860700001)(36756003)(82310400005)(81166007)(82740400003)(66899018)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:57:53.2984 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f9561e8a-d814-403f-9df9-08db13745f37 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: BL02EPF000108EA.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB7349 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: af4dk6sw4duw8oinncj6xfdmntdtomyc X-Rspamd-Queue-Id: 61CF9160008 X-HE-Tag: 1676919478-603231 X-HE-Meta: U2FsdGVkX18oX3eMkGK3E97VEXdJkCyZpVV1VjgDjpL5cCpT6cHYei733BCu2jsUO+ip8BzkL2VnXJPciRD4fDwHRurHfl9dgF7IskugWErzsGUHSZpG/MzJLEjeErLIEMf9CywHGQiwC6FF0I71sEPryL3mNu3/POtOS8SDSs2OKCodauehPi7yWhQCCgCAxtaerY2UzYeqoMDTvGPpaECahwzrKON/r6qssVMFrcixjwPbyEh81mtWcFcvPK6fU7pZEBGsduXg6/D/NEYGXTLySRK6FdZa8xt3t4GGtVV7PQiTSEQLcKgPEDuSM8UsWp8Qr42tpnXYeLXiRDeUIzGvELXEezthb3DfGaTfikh1EwXnF9Cfr7ig+LC31nYTtCpcJkeep1noDKHpLRBe8+hlcR6D/07GgOgexGHQI9+fIVst5Oa89THeEKyY6K5lCu9L0AWv5ffu9cjpaKFtGP8HZGxDZoHwBQqXwOEK0S/OeTmfI4UGsEtpj1ZzOUcdy+BNWBqwISkg1GmHneVqmSdTdKlZ/lugs3doJNWLHYDtciCX7L8fujiy7EkX79ZImZTLLAZjOboYsYe8QxvJIQaTXmLnqKF0/f4bBBB6OHY0DpmKSqwYugO+qTTWKf8RYM+I6kzEsaxJvE/ACYMq4Vb5mwhJgWBc2ligSrHKr1vTLEZc+2Y8mNUf7t8Y9nmIlD2ZBOsHpFrGgQuSBqxWTDpw5ppnVCX988Nf8RfDaJTx062Mejhe3TuUKm5ybAGB8SQ4aLluu0MY70WOgzmK6qNimKxNw9RAmU94I1Bc3NxWGYz7bLgnY+zFR3byh5hCrl9PFBrK1cuaTBZc6L/UZwi4EFHCD/g084ueuLPJZKIgBlby/SXNdY5reDnN0OMoV64CN/71exBHkE8iULgwnjfVMV2rphI5IDSB0z5n+o6d0LwiXfI9GdDHUgWIO8SZepjTT8NOgUUfckVMAvy d2vRpncJ dZDajcyBspwnmdmf5STefuWoZEc0j7gosIjOdYTCT0sOjszumVXgpsPB2hLsoZ06gz1B3K7O0612mfXRBOo5bcKy5PDg0z9kW6F5AyFQ26hbjK1Uz1SgwRJK/iHntYUELZ8YdSn/CcuYeSiqTShxsn+A/IhX+GAdl1aeNNRU/nyRxK7vuJojss3kTvSfqqSpxJwk/QkaCpsg4LuZQNMyjWMDH7cpLHOwj9w/DHsFQ066xVg7JN4kRHWwmZEWglQGcDEsTA2YH8Ksv8xxpSNtVyc2dJdgh5PQS3TE5Ryc0sI6OvVpPm+ABnvhxK8NJrUh2fhmz10HLGT4UJyw= 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 | 245 ++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 3 + arch/x86/kvm/svm/svm.h | 7 + arch/x86/kvm/x86.c | 9 ++ 6 files changed, 271 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 28b01cc7f64d..09b36462582c 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 c18d78d5e505..e76ad26ba64f 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -278,7 +278,12 @@ enum avic_ipi_failure_cause { #define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF) #define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL -#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 6bec2712ecc6..b2f1a12685ed 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -779,6 +779,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 */ @@ -824,6 +825,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); @@ -3161,6 +3168,10 @@ 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 (!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: @@ -3543,6 +3554,226 @@ static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, rc); } +static kvm_pfn_t gfn_to_pfn_restricted(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_restrictedmem_get_pfn(slot, gfn, &pfn, &order)) { + pr_err("SEV: Failure retrieving restricted PFN for GFN 0x%llx\n", gfn); + return INVALID_PAGE; + } + + put_page(pfn_to_page(pfn)); + + 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_STOPPED; + + /* Clear use of the VMSA */ + svm->sev_es.vmsa_pa = INVALID_PAGE; + svm->vmcb->control.vmsa_pa = INVALID_PAGE; + + if (cur_pa != __pa(svm->sev_es.vmsa) && VALID_PAGE(cur_pa)) { + /* + * The svm->sev_es.vmsa_pa field holds the hypervisor physical + * address of the about to be replaced VMSA which will no longer + * be used or referenced, so un-pin it. However, restricted + * pages (e.g. via AP creation) should be left to the + * restrictedmem backend to deal with, so don't release the + * page in that case. + */ + if (!VALID_PAGE(gfn_to_pfn_restricted(vcpu->kvm, + gpa_to_gfn(svm->sev_es.snp_vmsa_gpa)))) + 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_restricted(vcpu->kvm, gpa_to_gfn(svm->sev_es.snp_vmsa_gpa)); + if (!VALID_PAGE(pfn)) + return pfn; + + /* 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; @@ -3806,6 +4037,18 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) ret = 1; 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", @@ -3910,6 +4153,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 745f736d9c98..539926b07ee5 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1349,6 +1349,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 b6ca6657aa6c..37bd7b728d52 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -95,6 +95,8 @@ struct kvm_sev_info { void *snp_context; /* SNP guest context page */ void *snp_certs_data; struct mutex guest_req_lock; /* Lock for guest request handling */ + + u64 sev_features; /* Features set at VMSA creation */ }; struct kvm_svm { @@ -209,6 +211,10 @@ struct vcpu_sev_es_state { bool ghcb_sa_free; u64 ghcb_registered_gpa; + + struct mutex snp_vmsa_mutex; + gpa_t snp_vmsa_gpa; + bool snp_ap_create; }; struct vcpu_svm { @@ -718,6 +724,7 @@ void sev_es_unmap_ghcb(struct vcpu_svm *svm); struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu); void sev_adjust_mapping_level(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, int *level); 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 0154fc7a28c1..9872217e3a06 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10501,6 +10501,12 @@ 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) + goto out; + } } if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win || @@ -12698,6 +12704,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 Feb 20 18:38: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: 13146951 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 864DBC636D6 for ; Mon, 20 Feb 2023 18:58:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 293AB6B0073; Mon, 20 Feb 2023 13:58:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 21D956B0075; Mon, 20 Feb 2023 13:58:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 070226B0078; Mon, 20 Feb 2023 13:58:22 -0500 (EST) 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 E207C6B0073 for ; Mon, 20 Feb 2023 13:58:21 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id B5C4D1202E2 for ; Mon, 20 Feb 2023 18:58:21 +0000 (UTC) X-FDA: 80488580802.29.6B76B7A Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2085.outbound.protection.outlook.com [40.107.95.85]) by imf13.hostedemail.com (Postfix) with ESMTP id C65FB2000C for ; Mon, 20 Feb 2023 18:58:18 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=GZfHlX5E; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf13.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.95.85 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=1676919498; 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=CwZqp+t05+zsG1Vq05f7ztxd34mOR92w8AyhJ0DMD40=; b=omdpcXchGYn0VEdEMiRka3tMVk0e37uTPeh58dIi3yPPGNGc/yvNTT3attA+oIiomM/j2F vVX1upDZADMwKI9tjsqZ1M0uH16GQpz9SLHt5XeK37u2fFZ7gxBBrJLAP2MhUaBB0pefU5 ouFGMnUKEMUszMxQI1oqc1INGWR5XSk= ARC-Authentication-Results: i=2; imf13.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=GZfHlX5E; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf13.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.95.85 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=1676919498; a=rsa-sha256; cv=pass; b=B+XfWg1orb3MDrqQUqlyYn1oBv3Ae9Zr4wNZpiq1/s58GdxWEvo0+RPUP/Nd48HqrF+7R2 upKBCPKU/Pl1XPQpBzEfnc8/jtrrwf7w5j3JQFz1hQNjdYFpVVthtAba3Sjkfn0amBi2zU 0WnNTvobk6U21ihz9gvPN8Ap8Ht6N+8= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CO4iwPjKE80tPuL9lmKCM5mmLO6HYbno7n/ituOTvT2gVHkQANMd8w46IryWQS5luuzAGm/Hr4zoRbDWCRXt/zCGDgrAVI0DT7UPYvjqlThakXsJrBNuwEeuyBHh/RY8mu6/QItTj1cqrAcvdVFMRBAryGfb4hQhnTz8JBw+SazuXS7N0H0H3F2jQ9qyNY6Hne1hMvjVtHxiQ1MAyOTQxEjGwQb+k2O+BntlpxLysa9WSngkJHn6B+CFB/3EswOVrlK+73To80VgIaZezWkqKbs0itded8AMav9+nkSjkIQT97VZIf23CfK05d6QGYQ2zkzs9ovdwFK10ydUhKaT8A== 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=CwZqp+t05+zsG1Vq05f7ztxd34mOR92w8AyhJ0DMD40=; b=SWuwQchQyY5W0H+mg/T1qnHk7tpH9CEr1CWsrTVIjFRTDrzrqpPf4YS7IDdT08qPP2z0cC7NW5woF8PLcfbLx1UE06mgFpX+euWGnFXvY4d+szj/7qB6x1EpXjtcjvEPUA7t6s4TCJ3TbHF/aVoaEoV9P/mByCd22Tz1Hht2xnrmLqH6NI2zRSCNjlvRVMRZL5Va7npz+rLrlyJL2YxoHM1uEviLs1YdQDMTSUCTGZbKElbiTtVBSxpci29BtzQCpUbSq79ZUkOUdT1EaIcmGwg4SiKwhcgqFynF+S2b06fPSOLZHOP5tK1cHub8nxSkyHxPJASmzkqO/pEwqVVKxw== 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=CwZqp+t05+zsG1Vq05f7ztxd34mOR92w8AyhJ0DMD40=; b=GZfHlX5EvrGWt1vMCFpgEiJjJxf6LZYxW+1F/tvbbPgKtUOQIsqNCwieNfv1UUboCovFFuSyxW3KpExpm3OAYW8lY7hYjX77rWncS14YvaOQTEA+e0+aKP1+nC33Q8JwsdNXbOz+AWGYLTg21ZgILH/VZtP/p5khO9Bvbzs/+jY= Received: from BL0PR01CA0022.prod.exchangelabs.com (2603:10b6:208:71::35) by DM4PR12MB5939.namprd12.prod.outlook.com (2603:10b6:8:6a::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19; Mon, 20 Feb 2023 18:58:16 +0000 Received: from BL02EPF000108EA.namprd05.prod.outlook.com (2603:10b6:208:71:cafe::18) by BL0PR01CA0022.outlook.office365.com (2603:10b6:208:71::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19 via Frontend Transport; Mon, 20 Feb 2023 18:58:16 +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 BL02EPF000108EA.mail.protection.outlook.com (10.167.241.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:58: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.2375.34; Mon, 20 Feb 2023 12:58:13 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v8 48/56] KVM: SVM: Add SNP-specific handling for memory attribute updates Date: Mon, 20 Feb 2023 12:38:39 -0600 Message-ID: <20230220183847.59159-49-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF000108EA:EE_|DM4PR12MB5939:EE_ X-MS-Office365-Filtering-Correlation-Id: bcc55256-0910-4096-d0fb-08db13746cac X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: WBp7NsOjtQRr9r2fumEpdAu2TR9WF4vodunw4ENlL2ILID28X/WPfusVJ37xj3+ktwvUzJe5DP/655UmLFKdVJSlIcWsF16TjWtHGKUVEkWny043koH9hJDr6d0IdnvPe0SHoLbHw4BfNhEyAhSgp39ArmsrNKKfD6EEPMupYkpHZOv7PXCPua6PHZYAu/5T3MtK8b4sqNvMofHg0d/S11Vu8TEbKeLOkihXXz0I86wF71DNWbzYjh1LVijQlGR9cjPZKw1niDGRoDqIEGY+W8lVrROW7SRwz3oOjTDF1WScGjY11i59xfHPitSLgYnfJDjN3o6M1x2cN0JeXjPreqD5A12aOkPavg/TeU9WiLMiQR0DD2PABDKd58TAs1DvOiRoEZa1e9n7G3mTkFOq/XWkZPTMgSdDmbpegjK7SOqlhCnPonTAa488rub51NY1FkoI0S4NCSiNfYTNRUXmtqIMj/3ug1zctR5kHplSAU0wutWTqn6idstZHMIr3z+fZemKhN1J4f5KQinbfdCFwGAnbAljLEUcfW9nDj7RDN3lu6gmOUorYjBCz8ZRPOUs6Y7dKXhCSFUDxSOjl+Nwnflcih3DkM0EUDHMvix+Ai787rblJAxG0ukUCicsCJPEG79NCnEg2G2Veo3dJk3q8zD5r/ryyoLLO4RVG4IK6JpR0B9SmXXXk6NJFT9X2OGDVqbTk3om9EbIWDS/AZVgRRN9p251yCKuK34HB/ZXlNg= 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:(13230025)(4636009)(346002)(136003)(376002)(39860400002)(396003)(451199018)(40470700004)(46966006)(36840700001)(40460700003)(83380400001)(316002)(70586007)(70206006)(54906003)(2616005)(8676002)(8936002)(6666004)(4326008)(6916009)(1076003)(41300700001)(426003)(16526019)(186003)(47076005)(26005)(478600001)(336012)(356005)(40480700001)(36756003)(82310400005)(86362001)(15650500001)(82740400003)(7406005)(44832011)(7416002)(5660300002)(36860700001)(2906002)(81166007)(66899018)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:58:16.0331 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: bcc55256-0910-4096-d0fb-08db13746cac 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: BL02EPF000108EA.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5939 X-Rspamd-Queue-Id: C65FB2000C X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: j97gto498okwdm5r3mchwpn5zeqzh14b X-HE-Tag: 1676919498-965830 X-HE-Meta: U2FsdGVkX186qB4Mzmq16JcVWJ4Do+7etOWHvauVoma1ZozoYsy5VIIlA5jNIQCJFpt36mQMR7mcblSaFNzXmsQ9wofaM4wT5B+HlCo0Dvx3lZAJeOndKXnFevnGj8N156fFP3iyCmaWzE3+45KX9lnqHApehTsP3HgXHnju6kBfu/WWmnDw/5jgwh1nNP45PTxuR+B6udNGLvsHtmG+ne7wfxFVu5YF6OYUNtbkZDwHIgfhMVbyxIatAAtVjjC1YCO3tqc62PoFlcwUtgewdRWb78kAzWIa2fQJVtueWamj6nxKWMdgfV7bat/yQ2rDycR6Et+Q+Wjne2GYMBADtq5kHonRvZqvIVzSYQSrWxAnlAfhEd7UrJfoOBW4kHL2T3pAq9MpezFmhmGXoj3ie9n/7uwzL/EgZ6fKJUN/eO2YTe8/tVdeP0idsOIGJVsHVIPGxqQDlBtx4xXdbTxlILRAi0asrD5ki8zI+UdMdEHBJPdz5wLFmSo/5B8uQPOo3tfOkcgzAOBAh1ZCey6IUsRHXaAa7bwm8t288dPKdoI/C02vxmyTrmNjlsZ6wOPzGea2YhnLD6LU7NsknUYXSyf8597KGDe5DK0APnjHzXeMyhgkFiS8/l7jrg30z01nKBwJGgZRETD0JmCdYraFDKsT3xMfTy4xFBLwwm9rO5cI7OjcI5o6+dlpvq1WDRcyqvv0bRL5sz8uihVavRqIytxafCTg5lOXG5aCMgROQdP+r5JXaAymxR1pM4Pdk8JT0X5Z1HtNpY6sjMwN5FG5F/VaaQwJ3ess5jwKNajQMVBYTmu9AnJuUPFGBOAqx/OQFHNqLccBblCW7o1o9MkQel1iNh9fobn4Lr20nA/CjMrNAru2OTHQ/4/77tA5RVOM5ww3QLVGA29G7dobfs8Vd5X9gf4ARFOn6Gw0gFup8p3TGGnh+5jwOQeWrdwkKBld2Fe4PPh0lB9KTj/hEfT DWWfdxsG OwXoFYJG1Ykq+pql9/j8OHKYHax2bKHjE7F2YSe5JEYHKXgjWaOK+N8D1PLVN/bzkEgvD2LPDpLLmE/aGVMcCkjFDRJf4I2Ulmhmxl3ENLSHwmE/BrvsxmSlmGbaN6sdr2JVlrZKWlAo2n1ouYP1QcVnTUBxHuxO3hQZd8DiZ2fxh8tUZEt7kH5J6Ig3vMDyQuQG07gshZjX/zlNdALXUPxQl2JuqfvsvJLanIZwGFKqgEMxJsjbU0G6Pc+UbSBhwmX+Ij0oQInawK89zoNXHdDEjljUqFseM1vQB91WvjVuuMDCnaM584AjJSwhwAwFUjb56KJUsUkRhE/4= 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 for page state conversions requested by userspace. Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 126 +++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 2 + 3 files changed, 129 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index b2f1a12685ed..73d614c538da 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3381,6 +3381,31 @@ static int snp_rmptable_psmash(struct kvm *kvm, kvm_pfn_t pfn) return psmash(pfn); } +static int snp_make_page_shared(struct kvm *kvm, gpa_t gpa, kvm_pfn_t pfn, int level) +{ + int rc, rmp_level; + + rc = snp_lookup_rmpentry(pfn, &rmp_level); + if (rc < 0) + return -EINVAL; + + /* If page is not assigned then do nothing */ + if (!rc) + return 0; + + /* + * Is the page part of an existing 2MB RMP entry ? Split the 2MB into + * multiple of 4K-page before making the memory shared. + */ + if (level == PG_LEVEL_4K && rmp_level == PG_LEVEL_2M) { + rc = snp_rmptable_psmash(kvm, pfn); + if (rc) + return rc; + } + + return rmp_make_shared(pfn, level); +} + /* * TODO: need to get the value set by userspace in vcpu->run->vmgexit.ghcb_msr * and process that here accordingly. @@ -4373,3 +4398,104 @@ 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)); } + +static inline u8 order_to_level(int order) +{ + BUILD_BUG_ON(KVM_MAX_HUGEPAGE_LEVEL > PG_LEVEL_1G); + + if (order >= KVM_HPAGE_GFN_SHIFT(PG_LEVEL_1G)) + return PG_LEVEL_1G; + + if (order >= KVM_HPAGE_GFN_SHIFT(PG_LEVEL_2M)) + return PG_LEVEL_2M; + + return PG_LEVEL_4K; +} + +int sev_update_mem_attr(struct kvm_memory_slot *slot, unsigned int attr, + gfn_t start, gfn_t end) +{ + struct kvm_sev_info *sev = &to_kvm_svm(slot->kvm)->sev_info; + enum psc_op op = (attr & KVM_MEMORY_ATTRIBUTE_PRIVATE) ? SNP_PAGE_STATE_PRIVATE + : SNP_PAGE_STATE_SHARED; + gfn_t gfn = start; + + pr_debug("%s: GFN 0x%llx - 0x%llx, op: %d\n", __func__, start, end, op); + + if (!sev_snp_guest(slot->kvm)) + return 0; + + if (!kvm_slot_can_be_private(slot)) { + pr_err_ratelimited("%s: memslot for gfn: 0x%llx is not private.\n", + __func__, gfn); + return -EPERM; + } + + while (gfn < end) { + kvm_pfn_t pfn; + int level = PG_LEVEL_4K; /* TODO: take actual order into account */ + gpa_t gpa = gfn_to_gpa(gfn); + int npages = 1; + int order; + int rc; + + /* + * No work to do if there was never a page allocated from private + * memory. If there was a page that was deallocated previously, + * the invalidation notifier should have restored the page to + * shared. + */ + rc = kvm_restrictedmem_get_pfn(slot, gfn, &pfn, &order); + if (rc) { + pr_warn_ratelimited("%s: failed to retrieve gfn 0x%llx from private FD\n", + __func__, gfn); + gfn++; + continue; + } + + /* + * TODO: The RMP entry's hugepage bit is ignored for + * shared/unassigned pages. Either handle looping through each + * sub-page as part of snp_make_page_shared(), or remove the + * level argument. + */ + if (op == SNP_PAGE_STATE_PRIVATE && order && + IS_ALIGNED(gfn, 1 << order) && (gfn + (1 << order)) <= end) { + level = order_to_level(order); + npages = 1 << order; + } + + /* + * Grab the PFN from private memslot and update the RMP entry. + * It may be worthwhile to go ahead and map it into the TDP at + * this point if the guest is doing lazy acceptance, but for + * up-front bulk shared->private conversions it's not likely + * the guest will try to access the PFN any time soon, so for + * now just take the let KVM MMU handle faulting it on the next + * access. + */ + switch (op) { + case SNP_PAGE_STATE_SHARED: + rc = snp_make_page_shared(slot->kvm, gpa, pfn, level); + break; + case SNP_PAGE_STATE_PRIVATE: + rc = rmp_make_private(pfn, gpa, level, sev->asid, false); + break; + default: + rc = PSC_INVALID_ENTRY; + break; + } + + put_page(pfn_to_page(pfn)); + + if (rc) { + pr_err_ratelimited("%s: failed op %d gpa %llx pfn %llx level %d rc %d\n", + __func__, op, gpa, pfn, level, rc); + return -EINVAL; + } + + gfn += npages; + } + + return 0; +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 539926b07ee5..e2edc4700e55 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4860,6 +4860,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .alloc_apic_backing_page = svm_alloc_apic_backing_page, .adjust_mapping_level = sev_adjust_mapping_level, + .update_mem_attr = sev_update_mem_attr, }; /* diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 37bd7b728d52..50a2bcaf3fd7 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -725,6 +725,8 @@ struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu); void sev_adjust_mapping_level(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, int *level); 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_update_mem_attr(struct kvm_memory_slot *slot, unsigned int attr, + gfn_t start, gfn_t end); /* vmenter.S */ From patchwork Mon Feb 20 18:38: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: 13146952 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 EF140C636D6 for ; Mon, 20 Feb 2023 18:58:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8D59A6B0075; Mon, 20 Feb 2023 13:58:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 85ED26B0078; Mon, 20 Feb 2023 13:58:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6D8206B007B; Mon, 20 Feb 2023 13:58:41 -0500 (EST) 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 562016B0075 for ; Mon, 20 Feb 2023 13:58:41 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 2837180430 for ; Mon, 20 Feb 2023 18:58:41 +0000 (UTC) X-FDA: 80488581642.18.D5B99D9 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2083.outbound.protection.outlook.com [40.107.94.83]) by imf03.hostedemail.com (Postfix) with ESMTP id 2841E20005 for ; Mon, 20 Feb 2023 18:58:37 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=MCpI3e1W; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf03.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.83 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=1676919518; 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=d9dPtb0J/B6n406AfrJg339OIKYCnCNwo7m/uhWDMfY=; b=O5UQbepPdbwGjPHV/neGV7SM3Itifda98fQwTaf4u5gQbLP4Yl1jFqLRxoZ9xWpZ7jqdky amou1QXbzaHTuvKz270VChlnnu2LLVFlADiLyZw4YSdA8ROXG8SYL6cVN3/+GR8AOMzRr5 MNvHyDcw+N278aDTCRt6D/u1qaAZP6M= ARC-Authentication-Results: i=2; imf03.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=MCpI3e1W; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf03.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.83 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=1676919518; a=rsa-sha256; cv=pass; b=txklzL/jDaDmWGCnXNitAPOuI6c3FkikZzRe8KYtCouvuzEDevT49OLUZqNTKH4wfBmA9r h3V7NxriI8aMDFJnZYKQImGRIws3fWfk5//5B/P2Am+hvBMW9+bcJ5KH6zO7vGIEQKpj9N 4zBeUy8/3LCmIV2Yyoz6bMFKpX6bQqA= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TLV3QQsEGZSu8710w2osmcV4RHcw2UgPGml7vwAorec4dyGcVoMFz+ERVqw5ryGa1PkMTr7znLv5dlBEKGoI4HW2X6FCmPsctFlpygzMwApuV/Qc5gj27/p1GzRh8U056X5xEW8yKq3r9FB3QtLoLtIaGgA0WUzTTvkUzbODKjGikN7yudCF5qIrPSU/Kk4rMQdPKZuG/jg/y05yfk5wzcmYcqVcNOr58BaPbWaX+pCEYbqYKmVYg8mhSyA9pJ9Qu2nTkwJU7uKWV9va4MkAKqC6JRXVkCy+2klAmHTGKqofPlsrEvo7147e3SV79167D3o/RE9SAjvZMCuiZueBag== 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=d9dPtb0J/B6n406AfrJg339OIKYCnCNwo7m/uhWDMfY=; b=UmAd5w/a+zj2LcI890mdC8xgGIqNs8NcNryEFeldtQXc7VbCEAaoVoXW4mIUb7goc97S6fU34S2kXx2vzRtkwJvu8K5P9nYiU+bQIg+a1+SfjMlpPxvSwXNwoZCKalrz0n9tNpGCSK3zabE0gW8/kDbsZJwAFoJlX9RgTOIbDoNvaOoSseYcDplLcWPOqX/TqQTETKSpE4Q+vvuR8/JgMdHNklfouuMF/y5gwwNsvzzhW5JRtNhcorYalAvEHWy5T+oZu+cAQSWwGzShGdWCv6I2CAeMaZBdyLNypkdMAnX7ZI77tmqUFv4vb2T2Cl9V1/Lf1OiyOPV05RVjpuF11A== 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=d9dPtb0J/B6n406AfrJg339OIKYCnCNwo7m/uhWDMfY=; b=MCpI3e1WmlGp5o3G5hOLQwYy58HQ81kP6avyRb3xtqyJFOLwuKr71uasLywu7+Ok6Bxa7BKQ0jjxe7+4SZbhXGZAUi+Fyj8GghV5tr7+VhqZ7JNuZ6SihzpvAbFlmVMu2B8tfrnDQdBW2HrftK+iVezGJhEJu1ogiOgX1Kfl2fg= Received: from MW4PR02CA0013.namprd02.prod.outlook.com (2603:10b6:303:16d::13) by PH8PR12MB6963.namprd12.prod.outlook.com (2603:10b6:510:1be::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20; Mon, 20 Feb 2023 18:58:35 +0000 Received: from CO1NAM11FT013.eop-nam11.prod.protection.outlook.com (2603:10b6:303:16d:cafe::1) by MW4PR02CA0013.outlook.office365.com (2603:10b6:303:16d::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19 via Frontend Transport; Mon, 20 Feb 2023 18:58:35 +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 CO1NAM11FT013.mail.protection.outlook.com (10.13.174.227) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.21 via Frontend Transport; Mon, 20 Feb 2023 18:58:35 +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.2375.34; Mon, 20 Feb 2023 12:58:34 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v8 49/56] KVM: SVM: Implement .fault_is_private callback for SNP Date: Mon, 20 Feb 2023 12:38:40 -0600 Message-ID: <20230220183847.59159-50-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT013:EE_|PH8PR12MB6963:EE_ X-MS-Office365-Filtering-Correlation-Id: a626403f-4abd-4245-6a54-08db13747814 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 3xsTmTtwZ3Q/QLmceQLkWyD+sMYUrSej4eqJpbsjCTCgYmGIgp+UT3HnpbWytpaIUWo1AZQHbUaRX/Ou2fe383JrK9NCpYa8ku0G70oT2lIPw0J/pkh4d5QjVOfpVHd2UqrTGAI8uu2l6P0zYYAXtGGl1lPmozORWBwiqaCwOJ4vwQj5Z08ebbLm/oa4pqHWWGVsOlb8FMRskLyWQBhveWtISfHInADI0vXuex8FDw5TupECKbhnXLsGX2qgN2PQqXjAIbwfdRBpT+bEyj7RKYxIr3p2VWu+Eub3MGsEH9MsoCr6MXmgeckOr7aLFA/kraaWuydECTARYWE0w8jWkn9Dzb0hpQyORYI+ox1KpFDu0Wtq/L6PjX2Mghj5QIdDCVE1RGfmQ83Qr9E4oiHIRnEh1RIbO0f8VQWF/ix3M+DKU2xWrPJBZv8XhJ9ryicyPTlt/Lj+2ik4euHDtojYujG7c3onX11F9Y0fqL/lRHJBiYYG70zJ5Q02p2p3w++mS3QYpiE198zRARphm16Ok9xRKrPfPCszVzeIBZLnDCQSohvksTFKYjDaCixw5gol0xWk0CnjT/0XlsO6IPO9pTFzVN9aAmxxixw6f3nUTZ+xFN5oUpAdlfi5DSIWYqon+IQtDeLSc/IR5Df9t4I0d4tFu1tlf6PQr9E+mkWdm0xAqmQ5HqsHmQErrCt9GK7qK48ywfFfBRCWDuNcVyzfrSJDuiqYIj2tMG0gzryqeQouDaiharWPfn79/tyHbGUmTs9UOgC+A9LcpT4MPQfi2A== 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:(13230025)(4636009)(136003)(346002)(396003)(39860400002)(376002)(451199018)(46966006)(36840700001)(40470700004)(7406005)(7416002)(2906002)(44832011)(8936002)(5660300002)(47076005)(36860700001)(426003)(83380400001)(36756003)(6666004)(16526019)(26005)(2616005)(1076003)(70586007)(186003)(81166007)(40480700001)(356005)(86362001)(70206006)(40460700003)(41300700001)(82740400003)(6916009)(8676002)(4326008)(478600001)(316002)(336012)(54906003)(82310400005)(142923001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:58:35.3076 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a626403f-4abd-4245-6a54-08db13747814 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: CO1NAM11FT013.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB6963 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 2841E20005 X-Stat-Signature: 3e8jso1n4k1ekgie558rxesudxssrp7c X-HE-Tag: 1676919517-389083 X-HE-Meta: U2FsdGVkX180fxBZsVX5JOZChkOZeQgyNs14Ev2/exSngqKggHM5Ux2eXSsW37uHfY22lc93kFnVeFrgt18pd2TR7SMAdlY9OLaaKHmniP5UEcHdAMt0a6fnE9agtVUOWYHFPSzykd8MFLDUemcF6ox65koT6KtOaJig1DzIBljZPfQUal/xeHbNfRwRpvz9XpnGygDVMH8DGm948QWJGWh2nZzTMmQZpaH2f3CH4jGj4IWfZxJkLro2a+jl2Pv7ky5n9YCizRSLqbnN03AO5uiAWrudFg9mIrTpgtDNBRzejPH9GqufPbSgda+6NoIgZugAGRz4+D70Iwf2MJOTFsKKHr/xIOW0rlB9ntBjUXgzKvM1K+rJy3pkm/Ob4RMRRKG2dziXIYTR7pJW3NfOhGFacrlIZjVkFxqKcyILopeSiQcrNPpObo15I4lnpDYj5M9TgHpNr25e3iKjYKTEA8f0+4dGP6bRjP22DQ0Ag5sM6DH/zuVwzpMPIW1AkmHnaGDdzrmHK2ExsBdYenofk9eJOv0Bra/Y0leVbP4WWN2SxI6MiErumWmiDzMztfXxNfqFJPAqVSaxBmQ810ASQlPaLwj/jwMF8G/X07K5+mq0DJORLY4M4NM28zyyNX8yMnDT/fI4Ch9oFqnXwnre34Xo//E2b3duE/L9iqnyXE74H61GC3Rviqkn0woUAGePJacm9mI0tn1V+aLpY7Fq3kEPT9hYFyjiQ6g2QLjQMDFl/ZRlb2Shq4ZuzfKAG1Cj/Puomwgyr6XAbUbDI//qeKOh+7Y9c/bfxmZ+TS5IX4AvCkHc9N9/IGzG4ebKGDhIDQ8/oyPCh7H8imfyxgcIPd5KtpWEbKyVjJRXfIgP+Xabhqa5mBnDB/JW4CZdkLSCEDsl/vUs6E5agtzQMcRJQFwktdOkGQ5bLVwS4wDytfs+7jbFXedNSuAmO1qtWIPDJqRYDQZPEGbsfvDWXfi U8A3znLn w7AUFm+7Q+Qh8yKXdQxWtvK8a6uKsnZ+Ckloo9Mk4hz/cIK9Z3w5ff+PItGOn6wthZ20EBQk6jdujIBHbR+VtOkDKeLoAXEtlxKVp90T72TsBV63detCyOLH4kg3/+qSqG4ZeN5uZovwoXG8QKulllS6etglQdP6JbtjowZwXtpKIvCU1S63KyNT6lWwiH81oaE3uPleYKkAeNe8LKPutzYoIRn1JkfIF7+Yi5DjaGw9Fhh5efM2uQG3+pCW2ceseRnqOhr8daf/neRHvHbOrMRG3AsoLI8tW7KQ72K9Aioqj7udFKNLuvm/pJh+6BEBTuuT6+Hz4pvxKnI7l3M/8vZysmc8mRLL4ItsuTqUqnTt9inF+Kr0oydSIrw== 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: KVM MMU will use this to determine whether an #NPF should be serviced with restricted memory or not. Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 10 ++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 2 ++ 3 files changed, 13 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 73d614c538da..7a74a92cb39a 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -4499,3 +4499,13 @@ int sev_update_mem_attr(struct kvm_memory_slot *slot, unsigned int attr, return 0; } + +bool sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault) +{ + if (!sev_snp_guest(kvm)) + return false; + + *private_fault = (error_code & PFERR_GUEST_ENC_MASK) ? true : false; + + return true; +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index e2edc4700e55..18e4a6c17d11 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4861,6 +4861,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .adjust_mapping_level = sev_adjust_mapping_level, .update_mem_attr = sev_update_mem_attr, + .fault_is_private = sev_fault_is_private, }; /* diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 50a2bcaf3fd7..97038afa8020 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -728,6 +728,8 @@ void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu); int sev_update_mem_attr(struct kvm_memory_slot *slot, unsigned int attr, gfn_t start, gfn_t end); +bool sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault); + /* vmenter.S */ void __svm_sev_es_vcpu_run(struct vcpu_svm *svm, bool spec_ctrl_intercepted); From patchwork Mon Feb 20 18:38: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: 13146953 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 F223AC636D6 for ; Mon, 20 Feb 2023 18:59:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8FC406B0074; Mon, 20 Feb 2023 13:59:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 885026B007B; Mon, 20 Feb 2023 13:59:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6FEF56B007D; Mon, 20 Feb 2023 13:59:03 -0500 (EST) 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 595BC6B0074 for ; Mon, 20 Feb 2023 13:59:03 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 3D46F80431 for ; Mon, 20 Feb 2023 18:59:03 +0000 (UTC) X-FDA: 80488582566.10.8464434 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by imf18.hostedemail.com (Postfix) with ESMTP id 567651C0017 for ; Mon, 20 Feb 2023 18:58:59 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=PjZObEk9; spf=pass (imf18.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.41 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676919539; 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=KnSphTWBNbwGmWsTY+YjMyKJE+TqSxm46nIWpnun/yg=; b=qUsYlGEnYGQmUTFBQxpfyF29RuMEeSUe5+9iQo8WwjAAzDEEGlBxbyKTN6sYGv1DsIi3Ve gdeY1gf7wrRdd6h/I5LrRanpxWlxAzfVX55YaEq+bhCBmIQRdcBoIUIudYenwkYRmy3WNc l4ulghAovxLcYhqrKoHw1B4rFzdZcTc= ARC-Authentication-Results: i=2; imf18.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=PjZObEk9; spf=pass (imf18.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.41 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1676919539; a=rsa-sha256; cv=pass; b=tb7nSlN6mECxBlIYKb9rMOhVSl/LvgZjcxrdQH0/MZ88nzxKHrtAhsWRCCgW/OIxp9N85/ uJi3Y+lPLI/GCcwpsqs4D3tXjWQfRjTpEKS2fNLIdr574tG/JbdFJdjXzWAkEnX+QnkQ1C 9A/H//PIrzVzOMsut/bL1HJ9u9wGJVk= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Bx/oVMYN50rwy3n67GbfW0YlvimcydZ7aJJLylq1zAnU0fCFuqutyFn7EM34bT2z3bP0M/5y1RCDtbQK1CJ0uRYNoNF1F12zsL9nIxTjZXHyMm9r8cpI2rcfOr2hXI5iYG7KXDN0xY6l/Uxzl4aOzHhceoZ2Py0axp19HyGgMYNkAUAoG34Cm+sq8GguQOeFKVeKFm47qPJYoeUh+k0DHo8PXCWm1yz3Omqw1iKLmzxLF2msp98jwXTGmSYN6hqTb6nO34VjmMoQQZclDuswI6JUxSxADBSEnfc1fqgCWCSPm0npSBkdi32NAlZjoVh4dTkNsBwKXEABAoGOXowmXg== 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=KnSphTWBNbwGmWsTY+YjMyKJE+TqSxm46nIWpnun/yg=; b=gq9IIMntuMVcdGcvKHW/WnpqXmniggtgmlNqcRkoZNF58usVdSWeqHQwrVGYothUjWuImuqpN0bPnts4IlF2A12TCT38WvjOgQLWn0oJ9pHFlMyiLU/+XVLxGlhqxm8gQZ6YMKN8BJunYyIBrU27kxj9VxNyPq6f4n6KnwWDkV9qjvNu7u7tUVBBoaGnZJYwo0l6+D7yRMqjbDexs0f0nDg+l+Ahg35biTLAyDmTckt185ImJBUefmsJJlDn3YKMTehrmyZoceCumVE7sbIvLZQmDRLDsX55qh++gk6iMMI0T8beDU0gThyRvdWtE++d3ivVnUuQm5JGgQwG7xk3TA== 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=KnSphTWBNbwGmWsTY+YjMyKJE+TqSxm46nIWpnun/yg=; b=PjZObEk981RiCaSHEjid0Bo4fifOIg4o1ZlsQ9aK+4RFjEvhnSMsbEvM2ldcOHwkhDWCtINK52Y1vwtLa4scdMNPb8emWi0GQM0PSWGDpJahfiGjA7b8vpt/r+MxliDjlp0wpXfVPEjop/fP4AnW43is0p4UcsbSml+6OEJ2R3E= Received: from MW4PR04CA0135.namprd04.prod.outlook.com (2603:10b6:303:84::20) by BN9PR12MB5036.namprd12.prod.outlook.com (2603:10b6:408:135::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20; Mon, 20 Feb 2023 18:58:57 +0000 Received: from CO1NAM11FT086.eop-nam11.prod.protection.outlook.com (2603:10b6:303:84:cafe::61) by MW4PR04CA0135.outlook.office365.com (2603:10b6:303:84::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:58:56 +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 CO1NAM11FT086.mail.protection.outlook.com (10.13.175.73) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:58:56 +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.2375.34; Mon, 20 Feb 2023 12:58:55 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v8 50/56] KVM: SEV: Handle restricted memory invalidations for SNP Date: Mon, 20 Feb 2023 12:38:41 -0600 Message-ID: <20230220183847.59159-51-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT086:EE_|BN9PR12MB5036:EE_ X-MS-Office365-Filtering-Correlation-Id: 2077a6bc-7a03-445b-49e1-08db13748497 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: tciTx6DkaeYjeG1TlzwJW1Ucx4P+B9OZk3Kq7MbMYnhNfXViEKkZT/wGz/LwFgjB/vZvZ8LuKOU91vFqBBZfz1eFIQkKfcTsnGTOcWHZvJ/Jtgo6aoJJRq0S468GJN+YwwGApRoeSfPo9K6OlXmiHzHAgyirNJeS4gb97MBi0QsfzqhNTWhVhX+lrw30kWXBiGXrg6+e7mSKnJ6CiOw9V8w7d0fM3hkADuNZgHnhhqYgLDE/nf+2Aow8sp1XnFiU1YE1mYNlG8rcN6/eU8UbqNmpiMQZr/BfjSIcFSx2ZGNplhAoYF3fpDZnUPRoTUUB2vc1VwtTCRJXNOtZnBXRQbWOWY56LHiYOeznHE2Z9g67kpWv4+9GdJiimf1IcJCKjXn+LkpgphNPANq4NKtojuaLA00AU/UMoftWR+H8Yyc7m6uqKQOnagWC8heNISNMF6LtXT/mUixCwSTI6IRlw9DpOKB8ESMbHmoRdnTq+7o39xfPhueqtsw9RxopMv51sTPevOrgt4cRsqX1wRV8Bh6FgdSohMc5VMmH0Po22GZFzL67i+3+vkzyGRq66kO9+30afs4sA5pXycg9dTwhgAPF5sFz7nduY+12QE85oNvky4QgDw37N7lHcMRn5HdSEK+EZHlNsXL8ganRRy2Airw9k9OZNHd6KsTRgyM4CNWB/CskurNJRe6M0qKTHw/+cJ2QnoE2BtAgDhejfLAxVw3WpCm3IPwBvNZ/ildkIf8= 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:(13230025)(4636009)(39860400002)(136003)(346002)(396003)(376002)(451199018)(40470700004)(36840700001)(46966006)(8936002)(2616005)(36860700001)(7406005)(7416002)(5660300002)(336012)(4326008)(47076005)(82310400005)(8676002)(86362001)(70586007)(70206006)(6916009)(83380400001)(36756003)(54906003)(426003)(40480700001)(478600001)(316002)(40460700003)(41300700001)(26005)(186003)(16526019)(82740400003)(356005)(2906002)(81166007)(6666004)(44832011)(1076003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:58:56.3136 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2077a6bc-7a03-445b-49e1-08db13748497 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: CO1NAM11FT086.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5036 X-Stat-Signature: yo3ufe5y7r7ms9qy3z99uznhhf7x3ufz X-Rspam-User: X-Rspamd-Queue-Id: 567651C0017 X-Rspamd-Server: rspam06 X-HE-Tag: 1676919539-122335 X-HE-Meta: U2FsdGVkX18VwLTOylTo17KP6zAGcEeG8Ee6RMLCWkrltD8oJCeWRnn5CwqNWjzgHch8srs3qnpdm0Lb2+TGwTfxlWhFfFa9JKAaOWl90Rt5F5ujhJHr837t2XTTpZLFz1E6iZN36AVCppz70PFdUDg/9ZClAar9wWJ21dsp+Fw7VzFLHuA/mM+W0ht7u77h1X36XP8ZDSeuQbkaiFnb/MWuHEBAfjUtTeSjyXrzAZ8i8fCWoLj/XgaAArHLDv9ctRxLlZ6DnqzGzUsROW4QxfMvxIxS477NFDvDcYC48a5AxuhaiTQwFU0UcU2M4YRCfnTz8onycp9RDkIa7eCrIR+wnCRTuT944MHxP/vkTSV4ECXU1Tq5VuHO5z1HCUdFUuBUyCsRA40C+979GRpNH79BuognQON78zLGpaT5QFWTYzsjNYW+9VAhEeE5Q5oPytMe0fVDpE1IXKxsb+jvo1O9GRfV02L5AQbPQT8VhW384O0zY9OQTnWeMbrANka1XbnX4YElXCp5xCcgbj7PSCBywJFMrDu2LWbOfPzyA3VpqwG6r7aj/v4ltdh58o0QlgroPMrX1+qtgAjehHphgW1SWvaaHIL3NS3oSGwDwhYj2muSrKaX5ryhmaS9dPr8EeE7g9WeQ6+l9u+ymODeET10vPWGiRgGYE98QassJqQ2ztnz6TV8/WThXepQT+Uv0U7qUe8VXIwV5/GvqjrdDOGfJWMlqJ+Pm+hHlDG3wK+vuAUnTEgimJTysDTxVejs91Yywge+BO6NqRiwECYRjVem9GY1r3mcyD/jpg73DpDQVOP9p5Kx5+c2j5tfcBsOrd+BQ4mGtJMpxX+6ULXBLFIrA7v9N8Gl5KioXshgMLghwlCSHhRBsGhxOnAfx1aFiHl05uBKSVGByrSrf9wKmaha3MCN6bH5/lAnsCMHWNQbqD+GUsuAeQab2hSeRRsW1h7ZqH/M3UXYXbz45Nr 8U30TI7f eHE/wX/vJuWp5AZDggtbPaosdCFZwuu6JMDI8JmC7iFSM6kF/sx9boiDzqw1d2sAjW443m7oe1ojEiJ/79F/VvvEjX+p8cBn7WMH3i2d0r0qkIdH8cu+nELdI33vwRrMPYq25xg/j70MbwJywLzkH12WPZl4q9ArITVwKjJq3o0BH0hMchDlJanW8oJSagxQPqEDhyxOOhLntps3W8EJcXId8Jzzm8M9iltHxLaWClvMp4WEq9ZtXIxu1it+5hTK4lQ0re2AUvjr5jSamVd5M61UxGykHi8OkJgEz+QIXBwxn4G3LCLevx18/IqMAvDuxoDARfRueP8+bC3g= 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 and cleaning up RMP table entries for restricted memory that is being freed back to the host. Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 62 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 64 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 7a74a92cb39a..bedec90d034f 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -4509,3 +4509,65 @@ bool sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *priv return true; } + +void sev_invalidate_private_range(struct kvm_memory_slot *slot, gfn_t start, gfn_t end) +{ + gfn_t gfn = start; + + if (!sev_snp_guest(slot->kvm)) + return; + + if (!kvm_slot_can_be_private(slot)) { + pr_warn_ratelimited("SEV: Memslot for GFN: 0x%llx is not private.\n", + gfn); + return; + } + + while (gfn <= end) { + gpa_t gpa = gfn_to_gpa(gfn); + int level = PG_LEVEL_4K; + int order, rc; + kvm_pfn_t pfn; + + rc = kvm_restrictedmem_get_pfn(slot, gfn, &pfn, &order); + if (rc) { + pr_warn_ratelimited("SEV: Failed to retrieve restricted PFN for GFN 0x%llx, rc: %d\n", + gfn, rc); + gfn++; + continue; + } + + if (order) { + int rmp_level; + + if (IS_ALIGNED(gpa, page_level_size(PG_LEVEL_2M)) && + gpa + page_level_size(PG_LEVEL_2M) <= gfn_to_gpa(end)) + level = PG_LEVEL_2M; + else + pr_debug("%s: GPA 0x%llx is not aligned to 2M, skipping 2M directmap restoration\n", + __func__, gpa); + + /* + * TODO: It may still be possible to restore 2M mapping here, + * but keep it simple for now. + */ + if (level == PG_LEVEL_2M && + (!snp_lookup_rmpentry(pfn, &rmp_level) || rmp_level == PG_LEVEL_4K)) { + pr_debug("%s: PFN 0x%llx is not mapped as 2M private range, skipping 2M directmap restoration\n", + __func__, pfn); + level = PG_LEVEL_4K; + } + } + + pr_debug("%s: GPA %llx PFN %llx order %d level %d\n", + __func__, gpa, pfn, order, level); + rc = snp_make_page_shared(slot->kvm, gpa, pfn, level); + if (rc) + pr_err("SEV: Failed to restore page to shared, GPA: 0x%llx PFN: 0x%llx order: %d rc: %d\n", + gpa, pfn, order, rc); + + gfn += page_level_size(level) >> PAGE_SHIFT; + put_page(pfn_to_page(pfn)); + cond_resched(); + } +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 18e4a6c17d11..3fe5f13b5f3a 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4862,6 +4862,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .adjust_mapping_level = sev_adjust_mapping_level, .update_mem_attr = sev_update_mem_attr, .fault_is_private = sev_fault_is_private, + .invalidate_restricted_mem = sev_invalidate_private_range, }; /* diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 97038afa8020..857b674e68f0 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -727,6 +727,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_update_mem_attr(struct kvm_memory_slot *slot, unsigned int attr, gfn_t start, gfn_t end); +void sev_invalidate_private_range(struct kvm_memory_slot *slot, gfn_t start, gfn_t end); bool sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault); From patchwork Mon Feb 20 18:38: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: 13146978 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 E7E2FC6379F for ; Mon, 20 Feb 2023 18:59:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 886E86B0080; Mon, 20 Feb 2023 13:59:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 80F3B6B0081; Mon, 20 Feb 2023 13:59:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 688D36B0082; Mon, 20 Feb 2023 13:59:50 -0500 (EST) 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 568616B0080 for ; Mon, 20 Feb 2023 13:59:50 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 26B32120704 for ; Mon, 20 Feb 2023 18:59:50 +0000 (UTC) X-FDA: 80488584540.10.214F0FF Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2089.outbound.protection.outlook.com [40.107.243.89]) by imf13.hostedemail.com (Postfix) with ESMTP id 2DBC520014 for ; Mon, 20 Feb 2023 18:59:46 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=3CBYj510; spf=pass (imf13.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.243.89 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=1676919587; 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=AnmyPryN/heSuGOFY8VRmqMvbLRbxeGy7ersAZFqHrs=; b=YAk5COwUF+fhDCiZn2exAb9sU4jFcViindA9Y6CZiuMR31YiCXOqFqoEc8VKyCZSCc8xcF VD8/151EK1nZFnItx1zq5zs1Sw2Axbr1uQtQDLIIortQ+iMsXbDvDJbOqWwyamzKv+JMJS sMMuUsgOEBPpiNjKs/Z+er55EEz/xRg= ARC-Authentication-Results: i=2; imf13.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=3CBYj510; spf=pass (imf13.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.243.89 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=1676919587; a=rsa-sha256; cv=pass; b=4FEvQo9N7O9LARGgO491wmzXcKWXY34Y6yFRF5++45SL+ALOMAHwY+pRuWJmMl0ZfuC7a/ 06VfCR4KmVd0xYve0qneLd3lqbe899E2LfFyibFUQBQUJfUhUJs2YN/thLQP7sWENSWDZN 18g2hL66hU2hP3OvLH7e3KQcW13dPo4= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=W2aenU3s6cZD5hVCPjgOAoPTP2W6rpwaGeyZr83O6oQmUVFM4kuTcJt6HKSjhWX19hO2dOBfdpmMSgPqyTn1t9RLJfARFZkvE0tUH0IgceK6WT8/8FSNFBTDM4zf/0vSqxfTZPxzRjBT2vD7VbgpMhddkYyojjzbKs/v02IDznlB/U0zvsHWvdjuIsg18WXdDlMuFE+a87dYKnwvSGToljI8gdvNPXYaaE35T/eOdMlxsh+DioN0OvoR3H0l0wspm8veTfneXVA+L5N0Yldz8MLjTNkAfdA/i7IJ9kna610n9upu7pSsuTebYMNf7X+Dzev8Zi7jYu8lLZ3X8EaNdw== 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=AnmyPryN/heSuGOFY8VRmqMvbLRbxeGy7ersAZFqHrs=; b=cyK72mdA3W40+GoElJkksYkV12Nto+lYHLiMJ/ICUDyoEJ3KVjyZg/NuIXRcNbHoaUuYSxNC3+mBGGy8D1gC7glvcPb765MhP4FyhqAlJR9daQ37SRiCFf2iRrkO8gwuhtWypydJZDHimaFDeTwAG4VwAXG7w0oc8Z7C7A0TpA9T03/AvGIxpmqHqA5J5k92Zbtm16uGEtIMccecPPbKsWjD/jUUeK5mQqbGtxfC/jC5u0HdBGdjrfF79MVf5SlE0w1UwbiSA4qaOrUZEUS74Vr1o0hEaP3dmKtR8Xq979/6OjULjQ+j1q15EstqVgmHLviXERNvZeGS7hN8615DUg== 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=AnmyPryN/heSuGOFY8VRmqMvbLRbxeGy7ersAZFqHrs=; b=3CBYj510JB/Xi9kZ6Rl3hpwED5tzOJQUeTWTpyiRgMGVC9uF5d1tQ9FoovDjnYQG9HiisJNLUy1rFsklT2loKSWjAYNcQOh7pWeh/EL6jWbxpvvI70zyxICUQbEl4tuv6/k+MZrf4Rr5YyL+RTa9h2Udlzd/jh8F5zwGLo6Br4Q= Received: from BL0PR01CA0014.prod.exchangelabs.com (2603:10b6:208:71::27) by SJ2PR12MB8112.namprd12.prod.outlook.com (2603:10b6:a03:4f8::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19; Mon, 20 Feb 2023 18:59:44 +0000 Received: from BL02EPF000108EA.namprd05.prod.outlook.com (2603:10b6:208:71:cafe::65) by BL0PR01CA0014.outlook.office365.com (2603:10b6:208:71::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:59:44 +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 BL02EPF000108EA.mail.protection.outlook.com (10.167.241.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:59:40 +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.2375.34; Mon, 20 Feb 2023 12:59:37 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 51/56] KVM: SVM: Add module parameter to enable the SEV-SNP Date: Mon, 20 Feb 2023 12:38:42 -0600 Message-ID: <20230220183847.59159-52-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF000108EA:EE_|SJ2PR12MB8112:EE_ X-MS-Office365-Filtering-Correlation-Id: 52d75de6-2d76-454e-aea0-08db1374a0ee X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: sTe4xAMoq5qs8YNKgQysFoV6mcA4pIKYsNbhB2cFfiIcPw1x9RKRny+Z0hBatdIf5FTLLRcg5W3wDysgVBdYh1lfTi7F6Fs1nIN+tNkGEI7oppP90DgN0+cbjwYXpGkHSyL2IuypupiJIqHW/vn4uTd26PcIBsW1dW/BIeuj47sIAeDxq7r07GE39CrjJY5dE4KA6u6gSpX3EISmF4BJj1OlZIzsOH+J7dEAKgy0Rz2QpDHH8BLdg1EkuWLVuQ5YkyYqzo0TH8tjs7Hq1/STP0quYMEHxvXJMe4eGnympgGX4dsIbHhDzgR0PtjQAhYsBwRKzqmEj+tqYYF8OrxmXgVM4Fav1RC5OGt1CvMVveBnmonxPe7lo3p9idQx/jjiuDONs9bFaoEEI9SB1hDsXG1u0eCT3sqz32timjJHqipHfV2AH1w5sUaxhmBut+pCCtWe+Sb7JnSRSKg8NxD6qhjL3lRuT5CVA+/bRTPJn6JKzXK6ZpU//6c4rMCnaOZ/dPRmhojKEBc89kR01n8Wa4OD3vFk7PAYmkIyTqiGzfeJBbxg40YN7aasn8Kt1rMk/rAvpv0ta39uKFqPKcBmCVAoJTMbbJbo0mnyvKq6cV1JsdQ/L1X8r/GuzwRZHSsWESr13zeqafFB42q2Pnn2cjiDsCg12Rew/TRfpSqA3D9/3Ta4Cl7EMw5FbbmtXTnw3QhjPKqJH7tiEBJPmNXrqOeain0aINXfviAd7s6VDFo= 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:(13230025)(4636009)(39860400002)(136003)(396003)(376002)(346002)(451199018)(36840700001)(40470700004)(46966006)(40460700003)(41300700001)(83380400001)(54906003)(70586007)(316002)(2616005)(6916009)(4326008)(8936002)(6666004)(1076003)(8676002)(70206006)(426003)(47076005)(26005)(336012)(186003)(16526019)(478600001)(36756003)(40480700001)(2906002)(82310400005)(82740400003)(356005)(7406005)(44832011)(5660300002)(36860700001)(7416002)(81166007)(86362001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:59:40.8781 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 52d75de6-2d76-454e-aea0-08db1374a0ee 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: BL02EPF000108EA.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB8112 X-Rspamd-Queue-Id: 2DBC520014 X-Stat-Signature: qwtzpw5jut3m7upqoupr6qkyiaw9f1b9 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1676919586-849618 X-HE-Meta: U2FsdGVkX19EgVBF4VbonQ6gQpNTP6bXjg4M6+3LxGSrgrxFTnFRMVkfch2f6aqMfX/cdhg9xTs3WhjoV++fnT/CCjZ7lV1cttAnGQ919481THAZN0s5w+uaNi5mZa1hyDNfPOT1C9qG2MpAfVJr+NrlPXp+teMs+wmD5MuZzPIxiZjyJEguyXaIqHIITBKwwxC6Wi+/mNO7J4W69jMKV+Ub+mjON+fU2cbbFGyq9mRhLfhNb+lJooDrNUePFE1Z90gO0eBgONpCLpmluw+PKGZG6RnptNnaqBOsLvstHby2mqabcys/x382DQX8PlDuIKw9nm17Ei1qocwRP7oogt4ekmcagL4vdJOmtquolTDN8AX10RrGzpwxWSvUHaQ28ev7e/jjzE8QdUU6o8sqWEU4g2DkfGBGiq8fE3xc87VsVYZh5plnuDakANMy22eoukx1o+OAFeh+Typyd+Tir9VWc2ZPHXXMgQ6SGv4Rbo7PC/n4H1dwSV+r3dsxccHwyl5AbfsU0prhWGNBJ2Bmj5+mJ2jSNWby0ue5vJtm6DpzIA6jGH/SzCncrVhM1wnnQL4GIqIIAfYAiQ4LmU6coUNTzN8zqccSoH8ap1qufnAQo3mqXZnvD9sSngMgdJS/3E2WFOax6EcN2w7eUCV1p0Ns8W0tHxbgQLWaNccGrgJfZDXp+AAJGpCGOc8W6GMFEw3yEmG8/kvtFe4i2iv4tXJHQT2zOCDAf1gk2LHXUjS0Klwwbw9kBIkt+MUC9jS0LtpEvC+2CMEspBk/R7zA/VJ6/ASh2dF7WnCVsKRAv9mPCRYiSoS9olt3PZWr+Olioz7HWzaeJw21pqZVY7rWqmAnlF2f1BBu3TZ3zfus9LeWOxU1RtZPnu44taMBlZzWOa0qqTfZKA3vwOOVP9sjbNWb1SApsfq3BEeUseOjoOM32lzd9RmMZdo7Y+BNA4HNrQmhHpkJU10cHkDdKNH U2Fy5VpI jWUxweyqi0rmz7kREX1KjYtEc8OEqsmpLqFWKx9uUplfi0qbS8/yEgkAanDvUJhZnxArSiVF7GXbWh/UgFFvEc6MMirz6OPVSayruIj7I43Ndjf+YHaRd6mKbtlmmPIDkV/Yva9ezAj45I0BRDmfhU3i1S5+8q4yI8YIcMh00yZkAJw5d8IlK7YhFAw2y48AMpUzpuZLPZqJfsd15la1NlCUEU38F9DxVB05UlXMoS08oIIysY2Uu5EaWc2sKivCxa/ZIzfSulU5df9KobmjxrsNHtHJnXR9BwW4WgQIbzaSbG8kjz0i3rS/9c2W0fumnm41CD9zzhMvjk27bBbcfs7XEbWsgqQX4/d5ptivfygmFSZk= 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 | 7 ++++--- arch/x86/kvm/svm/svm.h | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index bedec90d034f..70d5650d8d95 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -55,14 +55,15 @@ 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 #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 diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 857b674e68f0..221b38d3c845 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -694,7 +694,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 0 +#define GHCB_HV_FT_SUPPORTED (GHCB_HV_FT_SNP | GHCB_HV_FT_SNP_AP_CREATION) extern unsigned int max_sev_asid; From patchwork Mon Feb 20 18:38: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: 13146979 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 6E0CAC636D6 for ; Mon, 20 Feb 2023 19:00:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F3C716B0082; Mon, 20 Feb 2023 14:00:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EECEC6B0083; Mon, 20 Feb 2023 14:00:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D3F686B0085; Mon, 20 Feb 2023 14:00:07 -0500 (EST) 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 C2D8E6B0082 for ; Mon, 20 Feb 2023 14:00:07 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 9AE1FC0691 for ; Mon, 20 Feb 2023 19:00:07 +0000 (UTC) X-FDA: 80488585254.09.AF65BED Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2068.outbound.protection.outlook.com [40.107.100.68]) by imf21.hostedemail.com (Postfix) with ESMTP id 8CADC1C0009 for ; Mon, 20 Feb 2023 19:00:04 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=d++de2Gh; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf21.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.100.68 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=1676919604; 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=kRUxSUaNWiRvHuEScxe5r3QlLNMXENAPIQKIYkpvNqE=; b=yyrlZXCVqAuK1Bzcad7OSDdbUI9LioK4rt5kPM6tnrPs6e1RsBSd7cVNJ86fWYZw219Yb8 tqB647GiG1svuQ26jEpSQNeXpc+wDywUbMSSES8xzDvZyQpM/FZWYFk0VfXFuDY8i23id8 CugfLQv50EGiB8hJuImIAKGvcqHSctk= ARC-Authentication-Results: i=2; imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=d++de2Gh; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf21.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.100.68 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1676919604; a=rsa-sha256; cv=pass; b=g0oeggC4/1UlptxeGeSXp61h5EEsiG7VesXOAXPrDOl3jJmpEfVL88Z63Zqi6acsKIdYg4 eYLQiyehBwrp4Uu9/TGS1EVzSC9r3IH88ka1bl0eerAgd32WckukZDcxaXxbpJFC3MP9uK vYcctIGgma38ewm4NHv4B+e0Bk0AIHg= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KFixzlaHQRPn/4tJOZdLKyRN3pn9JFfZc5aRJDLzcyP28sysiegffJfMu5ggOhz8+ZrvHMuLNyqBJATz5KN4Paly8WNgrsG6R3oG/z7QQJG9GSa9tv6nxutG39BdKghNg1tBMEENPKxuJ0mU0uyGJwpSgs9CyUjjASVwyKuCjoc+evs0aSaiai5XH4bv/z7JFJgHYsA5vZNRhtkDoxNXBZ0Q1Y35PdQjTmtv8QRcOt/B6sF3k0ptV1ueA/UrJtrp4SW6pHATj7mj3pYQYDoehxK6w5/+S+xHZNKa6Pk/3KVS54ZmoXi+2ZBSoc7iZ8VBKqWdHQG1p2ZBYTOY1b+dCw== 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=kRUxSUaNWiRvHuEScxe5r3QlLNMXENAPIQKIYkpvNqE=; b=cb4y0Fz6oWSuC5zBEGtZk4J5IEPj5lA4EMBKrWpSKY8sAwSmzUHkiL4wKc4jqxe4K6sSz192gi/nNZOfERml8OsnxgRW5P0USzYbfXCKo+dC6wfqLzN4XYNp+bqzPnzXlrqPEJ12uEE9Y+qEKL2/pgPigfY+AOFu8ish7GX2xc33i+A0ZBASNZ6cBwNAVb/spSzBHhajbvIoBUufGLeqrPHM0AD80Rd0PaeJTk5t8qIpjRmdqeHSDP9UtuGBIn7M//bUjN59isHl0J8IsWOdWzcj+GInZLj2k1hzqtSe0CXFzHeWijGDg3XsrMFHpKY38Se4gkhH+xT+nBxKfGhAuw== 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=kRUxSUaNWiRvHuEScxe5r3QlLNMXENAPIQKIYkpvNqE=; b=d++de2GhzQ7DGgzYFkDuNI7LnoQ2Qb9ilJJiUFHZEwLqsQ1vJydUL2zsj/wZlM4FBNKKhv61VIE0e/Sf/mlgpE0pF1CgIqTTS/06FjVOw6TfAJcBb2TJ66DvQll92JXN5Aojh58cKLTXYTf8cgIW671PsPCl3ah25DQTkllvZso= Received: from BL0PR01CA0032.prod.exchangelabs.com (2603:10b6:208:71::45) by IA1PR12MB6138.namprd12.prod.outlook.com (2603:10b6:208:3ea::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20; Mon, 20 Feb 2023 19:00:00 +0000 Received: from BL02EPF000108EA.namprd05.prod.outlook.com (2603:10b6:208:71:cafe::71) by BL0PR01CA0032.outlook.office365.com (2603:10b6:208:71::45) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 19:00:00 +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 BL02EPF000108EA.mail.protection.outlook.com (10.167.241.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6134.14 via Frontend Transport; Mon, 20 Feb 2023 18:59:59 +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.2375.34; Mon, 20 Feb 2023 12:59:58 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v8 52/56] ccp: Add support to decrypt the page Date: Mon, 20 Feb 2023 12:38:43 -0600 Message-ID: <20230220183847.59159-53-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF000108EA:EE_|IA1PR12MB6138:EE_ X-MS-Office365-Filtering-Correlation-Id: 71a72801-7bda-466e-4307-08db1374aadc X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: khXlFckNrLOpwfl8dDBBL4I2gjhAv1HO81ff3sUWr9lKzhkrtrVoyVxNJvLiivKvcfMp6k2RnxcNfgY4QxktVmiQvOA8N8RNz8g43+s1t2zgtM9n5I/7RwqPtBG7qe55C/L50vzOmp1MORm5Uuk+3J/s7rjAQRsQSRcv8a9oy6WXJEcnuDdxF8ejEjNOpuEbhrFq0m4pyI96yayaMjNbJVakdXNLwnY2ZxKpNVOAcvwNNKAR9Dhc2qpJyW6cfE9HX82LmwYkOjdh7EWXfu08vNSwcCDNaBuk4xpWKv3jxkFlnMf1rwi3canX7TmnkfsNbgWDk5HFsRTuVgRVvN8UKMYpTrmOGDRm1JsTewxlvROO0sCBzqbbr0SrnKMHa+JOC68PmFwEFLt3GokQ5+1oah9hkwiX2nZaBZrPdpIvsGQM83KxFP/gY8+Ly+KpPzroScHK8IxKG/0bky5QJ/prChzDyW7aipXcyLxIZl47siUsg2Skc7SAHRMohf5r5gmivgzvY+XGSsZG/R8pnwTGY5nYQMteEAGq8GenDo+YgUd3HTexFbo4Yfg79YV0RBdCDqaWYVgnnpXhAIrgsXTi4Sdx12B45MVBm3U81wk4rvotnNhCZy5X+Jjxm9P6gpj3w07wbvqYeUzKkYOutoI0fXdJecJWDfqpe/qdZsjsdcAcQ238aXXhbF6BLGG4iYEyS5PK/KAh95PsC1/qEpbGez3AL184ZvoHALJn1fbtapJgsyKObK9TJg9Ddaaaol/fTUdiA7dsFrefeHQT7mzrvw== 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:(13230025)(4636009)(396003)(346002)(39860400002)(136003)(376002)(451199018)(40470700004)(36840700001)(46966006)(86362001)(2616005)(426003)(40460700003)(336012)(47076005)(83380400001)(7406005)(478600001)(6666004)(1076003)(186003)(26005)(2906002)(16526019)(70206006)(70586007)(44832011)(41300700001)(82310400005)(6916009)(36756003)(8676002)(5660300002)(4326008)(316002)(54906003)(8936002)(7416002)(40480700001)(36860700001)(82740400003)(81166007)(356005)(36900700001)(134885004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:59:59.5815 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 71a72801-7bda-466e-4307-08db1374aadc 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: BL02EPF000108EA.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6138 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 8CADC1C0009 X-Stat-Signature: 3wizhdpp76o99a1wdw4aiiwwhagcxsec X-Rspam-User: X-HE-Tag: 1676919604-885121 X-HE-Meta: U2FsdGVkX1+k+yA6JXA4F/L03FhjmorjRnhZaO6oU1Bsf7q/Bblq+koar5ru02higP+J6aF3+NBBliYR2jY9abcFzsFKwWedfzEhcWc9QYh1fRj0gQUHeTi0nTpDvqLkIoG5iSR1m0qWqLZZGRTe65MwxOIM2gQ4DF8A2Y+GpFCxJpnmitWbz5kTdYB70ShwviEOOJF9QQ/Y1tlM6H6Du7KccmUEE2VVZyR82TYf+TSR6oStwoc4Uln1VKiro7KWGcgQ3GqxoI8tIM7k+7Zxe3NYb1mtlxS0Agk1TJJhb4CXXo4O9gRxKzBFdo4n2NSmJ5quFQaOOX27OU4OthQ70tQcKnAjxwPWKNjDeyyeiAvmbO2fTZVQzPy2QClHiIYoidD6TI0m52Y4XGzR9QDLFlpqqtWbWEM7CASnE/UGuZUzCYmmeVzS2DVtCdha7cdUXj32TqS34qqBNAhBygPa8cvmCA4WBmmzdBUpl6v3WM6BUHyYP6b2hSR9HHhDMASSr5MNYzej7LYJiT7Sgyeu35JLVgsgrkX44EKtYY3SC3Dg+Pk9k3+ebf+vC5xSKaxHjmmE3tMlrTiyBUczo1anD0fSD1Cb02CA1AuH1CQQnDAAdp/L96xpnI57duc1ouDTlfR4M3wfIv0lHHlD3XpFKStk9MmbneYiVw8s3YRgwWtWWPPx0z9achOEi9z//qdZmWw3dCh1UF/LSgfpb1Ax1DzA/HGiWZfqiz0KToy0F57DED95t3EZ5gEwIdR+TvamomtmmuSBndTPDqV1Ofua1SCPJXb8YGpdTlfuo9uOLUhSjMuzhM3S5+k7K7GMMKeeXY9bhboRWSMvGvCWpk+/4slfw27DnNqEA/0U3nMeweugw+Yi/u0mkVI7nlVDsUJt2geSwcSUd7GyEdKJq1SKvrlEy9z09dKOrJsg2r+77Xf05uWvVWPMqT3hczn7SFb8MIbo+dW0cPqHTR79z0V 0fGe2VlZ CcCXRy6xX5lDWueigKRbMPzJVce7Xl4zNO3i/1isbhH4lJKSCVmem6TiMZmCyqvX5nHn3kBru+WIfi9izj4MZgJuWU5iBkqD1PnFO+XMshJWDKA3KjkF6bmbzum5GDMb53tHZEMsrcAySHdjJB4ZbUs1ikiPKzsb4mHwsclQNydeOFTjJJUmCKFdx728KiB5NeRIXxce1BrLB++hE3remUs0wsemNz2vV3SJTk+SMpf8FqJHt8Aet75ADY+o/2WD4fgiNZli/Mg2uRXgut8+CFYzhkKB4gj1lCtewxpJfhZsMX/s9I0hqTXcPh7J7SuvnU9Wuvq85ekiZx8Q= 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 | 22 ++++++++++++++++++++-- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index e65563bc8298..bf5167b2acfc 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -2017,6 +2017,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); + int snp_guest_ext_guest_request(struct sev_data_snp_guest_request *data, unsigned long vaddr, unsigned long *npages, unsigned long *fw_err) { diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 81bafc049eca..92116e2b74fd 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -710,7 +710,6 @@ struct sev_data_snp_dbg { u64 gctx_paddr; /* In */ u64 src_addr; /* In */ u64 dst_addr; /* In */ - u32 len; /* In */ } __packed; /** @@ -913,13 +912,27 @@ int sev_guest_decommission(struct sev_data_decommission *data, int *error); * @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); + +/** + * 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 sev_do_cmd(int cmd, void *data, int *psp_ret); +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); @@ -987,6 +1000,11 @@ static inline void *psp_copy_user_blob(u64 __user uaddr, u32 len) { return ERR_P void snp_mark_pages_offline(unsigned long pfn, unsigned int npages) {} +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; From patchwork Mon Feb 20 18:38: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: 13146980 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 397E5C6379F for ; Mon, 20 Feb 2023 19:00:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CCBF16B0085; Mon, 20 Feb 2023 14:00:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C54D46B0089; Mon, 20 Feb 2023 14:00:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ACE616B008A; Mon, 20 Feb 2023 14:00:26 -0500 (EST) 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 973F46B0085 for ; Mon, 20 Feb 2023 14:00:26 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 78329A0348 for ; Mon, 20 Feb 2023 19:00:26 +0000 (UTC) X-FDA: 80488586052.17.72970B5 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2040.outbound.protection.outlook.com [40.107.96.40]) by imf14.hostedemail.com (Postfix) with ESMTP id 5BCA410000B for ; Mon, 20 Feb 2023 19:00:23 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=sH3qZDr5; spf=pass (imf14.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.96.40 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=1676919623; 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=LopXYiYyykv0LOA+8gvAgfurQClPV2ZL2OUbKE6L/Ic=; b=0jVv4zDHI6ScPvzCIgsCmq510jeXR8jMaz4qgOnb3CkJMLaFsKwPROawM7ChGatIx5MwIN YnbpyTp+v1VKw7buiIYKXwuD2Fhv+eKCLmr6c1qcATUOSS8YK9p4ECdSvrP0G2PNyn4Cfq dJSJRCaugBTFNixPQsGe67XjaXgJdTQ= ARC-Authentication-Results: i=2; imf14.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=sH3qZDr5; spf=pass (imf14.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.96.40 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=1676919623; a=rsa-sha256; cv=pass; b=B16sYKAJN8gkR+0k8gfPt0Cy+r57cDKJ4t28eSD+SdxZiWnedAZnQKSzM2nerOmbk17BE5 KDwwesA5N7U7rYT3IZAKjWMXRM7Srkwcx02TPw4t6tkSe8uaYsdz2CHJHqgAoDeq5v+z7h 5o4N6xmPgP9erKBnaCAAUN57dbdqF/Y= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Us5oc+c0EjiEBa80509rTHcthGEwyqHLk6lKfA2CmCm+GF3bdAP9GqimCNw5w1N1k4zMVzU1cFFebw4+rNahCo8HdNdN/IgcyOXurp3QuK28DcHQDx4i+YHLEyPAFCeudbE1R5ziSIHpzyj0YUYHpzVN3b/zzS8WySYn7h1cZAISEsyoQtfZTWnLWWe9zMKeMRLKnqIIQ0zNybc2gyJZjIqgEqvAediOPXuFAYHL4qadkeWEYAHakr3ZP726NNb70R2T4O1tedOaoCL9uPoLnRXECYptcDKLBroSM+Gx+46DG4tsAaLVJnv1RmzGfNTSqms0LW/JQrllebH3BZFw+g== 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=LopXYiYyykv0LOA+8gvAgfurQClPV2ZL2OUbKE6L/Ic=; b=CFPbLXvZBN/tZM5QZI2AoleBlb70IloHefT7SMzI34JMaNhitFmsDAPcosRc13mSAXidsOjdccTb/R9wd2HfyLNmpnCG++ORDbgd6x6g84VYGfxLGA1432/ZagWhwbWzSEDPIFpC47UHsKm5S2t2FvJ+0guDPKZxWCTIjZPVEywuEkPQWNIredhw+oyTW6yU9Q7ax89N7ARneeDA18Hi/2xuGOJ11W2SyVWAVHBYaBnY2Vs7wAPcnOlcaJKEgd8vdIDnAUWNJDmb6li1xXo+XYlTuMnjvFhA0I+zLwWAg/gyGNmsJw6gMqEqlNEXq7uk09qWBd5cB+TMM09OWzESXQ== 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=LopXYiYyykv0LOA+8gvAgfurQClPV2ZL2OUbKE6L/Ic=; b=sH3qZDr5v+nqc6H9IC9VmZRpvGVel9X7mvS7ym8dKX683tsrVnY33yMVNyrw/Ze1CYEPsQIjrHjv2jMeuCzOAAX4deq3kDlcqNOKs5XMrniEaaFGJkyNjtpbjOkAaMbJDhlpmuZOpisaGUMYkya4yvQWJFghPegUYxVK+dd/caU= Received: from MW2PR16CA0049.namprd16.prod.outlook.com (2603:10b6:907:1::26) by SA1PR12MB8642.namprd12.prod.outlook.com (2603:10b6:806:383::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19; Mon, 20 Feb 2023 19:00:20 +0000 Received: from CO1NAM11FT036.eop-nam11.prod.protection.outlook.com (2603:10b6:907:1:cafe::b) by MW2PR16CA0049.outlook.office365.com (2603:10b6:907:1::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 19:00: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 CO1NAM11FT036.mail.protection.outlook.com (10.13.174.124) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.21 via Frontend Transport; Mon, 20 Feb 2023 19:00: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.2375.34; Mon, 20 Feb 2023 13:00:19 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v8 53/56] KVM: SVM: Make VMSAVE target area memory allocation SNP safe Date: Mon, 20 Feb 2023 12:38:44 -0600 Message-ID: <20230220183847.59159-54-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT036:EE_|SA1PR12MB8642:EE_ X-MS-Office365-Filtering-Correlation-Id: b711566e-bba1-4401-6bce-08db1374b6b0 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: wBDpVUHt03FwxxTaPXUM5WKI/7ajoFeBvmHUw9lALTFPQmWsVobxRpfWFo4S7EgzDgI3GZgh3SQQ7FviTTEMzeIzlCNjAhuunbV4efECxzlvdj1OBp1PebjOC1lopUUjDc4hNUO7u0gqMFAUcFyCp43wz/Ag2M9T0bRSclGLODiXn2m+QWcj0M4EHoGj/qeCXA2tOA567v07/WME+lkXKLT8/1ZxSJHf+sn0ZT36cGQeiDbp8Ed8FHwkpziQZsafGRIp0FO6Uy2T5aFLYnCTJnj5RuI3NT/YO7JCXxA8XpFJjJRuLOV9fYCd5jk0ZVG9ea3Q8Y0LcOVTXNQ6Mv18U9M4hoT0WInFdBQ9rq1U6mVAG6w7EkTfegh1syBDJX9zzJIuC/UY4rkEoHUM2A0EqGm+xANSipgo4ldprikIHOYpDyUWxtnlogjyYMOrjhjcPqK2mrBB+78KTY3luBKh1koJZyOwz3dimqXxtvQoRNuWlSPAfMskVu31KTIrqeq+iXq+xdGlvg270aUHWUt/+udj8wJ2hTX2PuH3wC6p1XkGQRSVHzeCJ6vMCiG3Jg7qsqyQ7bO483ir2QxShrILUCVNlpmRL/h/QWdIlm2TajAyHCN2Oa5Xbg6zWljvroaE2nUbgRAXe3WFGeCVZGXYqpPy0gUpDa+87l8G9j6MqzhZLYOdtAEKFEO9raC+GgfbbD2KsPJzBrbH0d1mqf/DTcF1W7stFSWvhr8SXNlij10= 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:(13230025)(4636009)(396003)(39860400002)(376002)(346002)(136003)(451199018)(40470700004)(36840700001)(46966006)(40460700003)(54906003)(70206006)(70586007)(316002)(83380400001)(8936002)(8676002)(41300700001)(6666004)(6916009)(4326008)(2616005)(1076003)(186003)(426003)(16526019)(26005)(47076005)(356005)(478600001)(336012)(40480700001)(36756003)(82310400005)(86362001)(2906002)(82740400003)(7416002)(7406005)(5660300002)(36860700001)(44832011)(81166007)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 19:00:20.3644 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b711566e-bba1-4401-6bce-08db1374b6b0 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: CO1NAM11FT036.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB8642 X-Stat-Signature: a87tgyh4of3bx4sdpydi1h9od5zx46jc X-Rspam-User: X-Rspamd-Queue-Id: 5BCA410000B X-Rspamd-Server: rspam06 X-HE-Tag: 1676919623-605773 X-HE-Meta: U2FsdGVkX1+8sWAFrnRvV3JBxtjLmTCI0qEFwP9YM6TQFtmeP0Z2h8yQf5phEEEsABPd4AIdEDuCyKddfxlj681y1qJCuAualCCJbgjJNHNzWkyQCifB93R0XRc8gzbl/iLG8NPW4aY7P5KGRUpt+g7hNqtReC4Gl4uQRBluNqyXj+4Ifc20KP9mtcgCIalIM/sfN+a9xBj65nVhpwcFhXOrQvN++UUp3EOzApfqriCBZpRk7i0wzGstDuhgXQ285mvk+MrJtEeVphZBiL8Q8051Cg1ZlAWCNaeqSEVaNQr9gouXg4Zg4GBSSreqoNICVvcL2YZ62efBiOPMakbQalo7PqVNOZtfR4ttN9wxRU/IMJ5/Qx1rCJbZfc6YpWj3j9z5/zvfR61lWkHpYSFs/mgHans1Avk4Bpzi23dM8/TM+GgzTKOaSoinwGsrkEbySYJKMbTHgkuv+pvzUiv8sgqN5xQOJe25MVGAqJXcbCO/qlSJQZ6Os5KuA29jkcp9vbo+eTZ9d3RVJRfVgYF6dGd2WvxI8zLUev0ARG3zmhCnkavptybK7GF4OcKe/ryGBShh1UEIG23IMzLKbIPJjZU0/xlspIeg5hN6iCH7qA0V+Cyo/evLlWY5cbGGOCVd3ki1k6KmCyalFCF+twUQ+RKqQz2ZEHrqrz8fURDNzpE1GEqwY9Y4qdwb/7s9CApx+I8Dkg7s5w3b903iQ/oJTsRm4/vnJXcc+DIyZzj4Lv1/5EA2pUf1MkCGt55QTexyJ/1n9AhmHAPcSpVgqGzWnTOMZktXTFleXNHxZGqp7V6TpNAJjQeQZ5cQ3LQKQN7pJj1heWlDFvScdVxPqFA0XEvI1NCDjhWRC6gybNKhcCckGOU3baCLyeyeSRexe4y5aTfpdUACWgqvD5lho8EHF/BxFd6G5VUckzixa2GRUp7OEnu3AUqwIT8UZKhqzyDiG0ifU+fUniXxkWPvsSC RWNEc0vN CQcPXX6jjtxrLHrTfjs/dYLX1hSB1R99qXDjMMTGLfBMxtU8fEAyiRX/D0LFry1V8E6tpdaqH37U6UlkYyCJgwZhZvw7QdFmhMHQOBEv6zg9GJCISgxGDdqDo4TPBM2J7KJWJiw6NDtv7MbwiKfZ5Y6fG/rDO0EERTAlb0JZCMDaqJHMQw+yJZRnju/nZePq3Q4CjdQ+42T68W8dHyDs7i0q4t3GxespXA9KiI5+8AcfSIVQDFrqmnc06J5wtzMrGIhAsMhABcPIGmrBTkcrtfqUSiAl82MbNB2HgaUN/2krTGsMXUjH6ZZfVAJwK+btQb0nx+cZFVMghrwyOrt/trwZcIWgYrbHt8UK9 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 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 the VMSAVE target page. When SEV-SNP is globally enabled, the CPU marks the VMSAVE target page as "InUse" while the VMSAVE instruction is executing. If another CPU writes to a different page in the same 2MB region while the VMSAVE is executing, the CPU will throw an RMP violation #PF. Use the snp safe generic allocator for allocating the VMSA target page which will ensure that the page returned is not a hugepage, as it is already being used for the allocating the VMCB, VMSA and AVIC backing page. Co-developed-by: Marc Orr Signed-off-by: Marc Orr Reported-by: Alper Gun Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/kvm/svm/svm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 3fe5f13b5f3a..8bda31a61757 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -665,7 +665,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; From patchwork Mon Feb 20 18:38: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: 13146981 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 8FE4CC6379F for ; Mon, 20 Feb 2023 19:00:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 306DB6B008A; Mon, 20 Feb 2023 14:00:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2906C6B008C; Mon, 20 Feb 2023 14:00:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0E3216B0092; Mon, 20 Feb 2023 14:00:51 -0500 (EST) 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 F23076B008A for ; Mon, 20 Feb 2023 14:00:50 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id B2E51160842 for ; Mon, 20 Feb 2023 19:00:50 +0000 (UTC) X-FDA: 80488587060.06.813BEB6 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2065.outbound.protection.outlook.com [40.107.237.65]) by imf27.hostedemail.com (Postfix) with ESMTP id 40B174001A for ; Mon, 20 Feb 2023 19:00:45 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=KWQmPL8b; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf27.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.65 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=1676919646; 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=2M6hrAR8Ctx+uOCboGePLQAUWptiNchqX1bNzjVjiko=; b=ycPW3xp/LjwGz2TBRlCF+WNuHb4MJ3xxQKM7OQZlBjaDhuf9MyNpZVCvBqVLNCgZKxooLD +k7R/PKVbaTkGInW4akIshZZC/4MZ19tqF+WFqUQH2EuPU/tAfKTD7oGDDtcksVY6Gsxf2 Y4iG3ye0Z0wlnrOq3RYb14N5J13B4OE= ARC-Authentication-Results: i=2; imf27.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=KWQmPL8b; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf27.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.65 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1676919646; a=rsa-sha256; cv=pass; b=a52pCe5sLJHjjK1HDkvrFUCiRN08YsO6tWIwouIehzPsKhjtgOmeKt3//mYzaAZtfqgwjJ RrbDSY/agz8LTd4vE16j+vGbObQym/NczC19hi/DJrTaaOgul3Q91WEkWBpkFUqc3Q211v n3Wn1rb1IsZNQVKq0Sd6/xlA4WLxNns= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=djcn8ThIoyrU41bEYDWLwFYChKAW8DTjTqlwK0dDvJkV4m94JIgWSL31ckuhe01MBG06R2O3thrUUYbSmpSiK5b5tBcucu6jBLmbtV0du5w0tur2dbOOU6TB+sfIUi7Zu6xp15qouWz5nH925yTzrWhj1N4vKOqgx/VDlDCWsZ6tvMiyNfK3DEZUKT418pjyd65Oy/7yF+brcJybNXPwiCYGA7RgA8YeJKApSfWMaZj6RrpwQNFA+3UiHoEwQ6zsd4EQyQiuK7Gyzp0bPv3788W1FLM1ba5X6agjPwyQPWdvRmJh0T8M+L2kEAZiCXxHSja5jqSyMrP3iKEbqUX7Lg== 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=2M6hrAR8Ctx+uOCboGePLQAUWptiNchqX1bNzjVjiko=; b=CjKLVTE2cYJQAqkZBrXNecAvg3H5/4Z+D3YBt/9EERpk7C8S4QVMBhYoKWHWfZnOu8RwgjGKWU7/MGWImyeCfUzT8DFCLqdy/NVgSJ6EttIS2yOnYosG1GomiHuOMrpijONwbs/9NQDBEh1JXlhdwIC3OAyOwJlboemEN1V9fr6UqXHYi0n+cmYtuKUGwK8OAEDzA16mA1TpHoOSffU5LtWN5U+3pMtnPetxhcwwbkiOlUDm+p6N3vyrxLnWQ1DIGtx3FaOLuWSYxpYwWXfOlhFslJj4zK7v4NV3J4zliUQpSOHH6yoPcsqNpojHAzuNwLEpc63gzQDtyZn0LoOslQ== 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=2M6hrAR8Ctx+uOCboGePLQAUWptiNchqX1bNzjVjiko=; b=KWQmPL8bu1lqQm+OY+FOyE0MbFhzno8IgkEoNzsWMh3guGApBwDcApX/QwAh1pad0AdVm5YW//qj1WySSx0wf7y7DYSay6AZkY2dHHlFa+iwXRf40Nv0R4BGvl0Rpnw1YLIrGIb5Wqm0uSoMhtPvwgNSm6duSw7JhjRNBnrATSg= Received: from MW4PR03CA0056.namprd03.prod.outlook.com (2603:10b6:303:8e::31) by DM4PR12MB6208.namprd12.prod.outlook.com (2603:10b6:8:a5::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19; Mon, 20 Feb 2023 19:00:41 +0000 Received: from CO1NAM11FT077.eop-nam11.prod.protection.outlook.com (2603:10b6:303:8e:cafe::54) by MW4PR03CA0056.outlook.office365.com (2603:10b6:303:8e::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19 via Frontend Transport; Mon, 20 Feb 2023 19:00: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 CO1NAM11FT077.mail.protection.outlook.com (10.13.175.55) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.19 via Frontend Transport; Mon, 20 Feb 2023 19:00: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.2375.34; Mon, 20 Feb 2023 13:00:40 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Dionna Glaze , "Tom Lendacky" Subject: [PATCH RFC v8 54/56] x86/sev: Add KVM commands for instance certs Date: Mon, 20 Feb 2023 12:38:45 -0600 Message-ID: <20230220183847.59159-55-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT077:EE_|DM4PR12MB6208:EE_ X-MS-Office365-Filtering-Correlation-Id: c4025751-8b34-4a24-1238-08db1374c32d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: qERWmM6kAcyem68EePiJhnI2MMVSFNyShOfMpsEtP20IEyzXsj2L4q46HzSC4z9D/EMB+9MFQHhN6cHg+Z+WhHDL1zVVtEYKO0o6wunc540vElz/3e3g/ZPAay/1RXdCA6x5hcspBK1JUH3FMB0vTQcHFzm/2VTNxMRmt6TbrM4Z22j1AeK2I0QPULdwrI1foiOXuqdvwJKxUDno297eRmaCJm4ys5IclnRaAm0LBZ0RfRI3E/mqb+fIhOODJdHmKvRRGRFUmd5M5oT5ptNXyU2XhjjgaTK6faa5zBPMgdNfjMXWaFN/QxNlTTyzzxsB6Q5k3V+wySyof/tetC7HCWgJyZR+qK2evF/C1Tf7t1lyPMwarzFIHOVlHduANBH4akO13yWSH+dqsKupcMBi5sCMblFalCBWpBvTPNGzzPjMpsjXa8X7j7lbmVPu/mFpksft2zeOSX5sbt9MwFRYalF/3ABQxuezgD9p2dnj+CTxTec8R2e1gLxp0H6CpOQ3kR8SoRAu1WomECWwLZ14RXJlBGKLAC/Jbm2MMyd4FX8EaziQRdfNS1fuBzqD1uatt3YmPJsSrlyNZSMqCVlmGNGaDGMs5lKJ/3UrVQd2cQ+sqByoZJGpVVtveA0Rulwj4SNgB7truWRE31jESp+1W8YCNCDPYbdZprG9qpYcLoTnWTcmmNc1Y7hy+/JEMa0E31B87plsUX6LCG4I3hFVal2V8nGtemsKT1dKgbx2wYY= 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:(13230025)(4636009)(39860400002)(346002)(396003)(136003)(376002)(451199018)(36840700001)(40470700004)(46966006)(336012)(478600001)(36860700001)(2616005)(47076005)(81166007)(426003)(70206006)(1076003)(2906002)(83380400001)(186003)(6916009)(316002)(40460700003)(16526019)(8676002)(70586007)(54906003)(36756003)(7416002)(82310400005)(40480700001)(356005)(8936002)(86362001)(26005)(4326008)(5660300002)(44832011)(41300700001)(7406005)(82740400003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 19:00:41.3139 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c4025751-8b34-4a24-1238-08db1374c32d 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: CO1NAM11FT077.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB6208 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 40B174001A X-Stat-Signature: rkcfsdkwipcn14jgdmaadwc381iusxyp X-Rspam-User: X-HE-Tag: 1676919645-847105 X-HE-Meta: U2FsdGVkX199EM6uryZ7A3nu1yq/HzxcSPPWTaAS7rF9tF+aDId0qaxiCufpqwQlDK313eRqZdHYYDQy96OETAlGlp0kuMkNgLxSggZ8Brgl4KXwTW+Cc20fM+UUngZ94vqn1+T7DO0UomIj2CG3RDOIfRR3aaOFmvMWvpqImX201CJh7ZOfyrFUt2BYgDMvEZBk/RltgZe4PWWWVsd5wcGy8wwNEvcjkdmcshkz8F65P+S8T0MShcAMDDsUzWalSPcrftmXdxrim3FcdHH5mm1V4ql3XStVU669Z/mEbidkzTvM7IIWBUHSu534qePk/x7Huv2XU2HlWAynYew2vDX6du+n2LCROXfQYIhzan4dhWe7HQt95ubaawH9VMQryoESRThm7iDuwVc4osd4/kLkWYaO8UQECIckIh+H7A1lwVFYjmc1TJ6fEx8/K1YSeXUl03aH9Zyg9McgQacZT7NpOh4FfdabbMVfFhbrOUQLo5PdRzVyDIIYWMhNiTu4fMGhywWfsTTuSsYiLYtruNTZaOV/QOCGmfvP2QYlHQ7jLhjA4CQXKAQdgkvejY91ZvbVjtueqYD5ZXG40H7Hr2FMaGrywfw77RdogaUPUFJh9PpGJ3bsf7+jyekE78RSWPOymEVCg4hCDlATVy1zvEM6YUQTDd83ZDt51ghDobJyMj8L97aYdzxn7M62FVF208iEci85pZZw4V7/BX2w7mTY2qbSR61vUEIJ0Ip7YH20r4v4Q1A2wtJTgzDADwvRRm6QOnetHDwbPzDT0B1fykX5v8C1RrrkJ6x+T52lha2aq6TexUBkyoWzKxvhZaHRWB6qlCjcI6+05Kvuqd8ZU2f+/+13mzFeUDN95WF4AjKcCKrZvNUEMH1DWcjyC/sg7wDn4lBj4UADVBvlzBGWO4pSLBXbAxTNEvHHIt5lYIuRcAG092nQHp2H3m13eogS4AlHJLDOE+BdQnEluHF 6O0pYzr3 3CTN7LGIqHJPZwVSDZ+Mj6RaRI+JFlJoi6ua8Od5cRH9NG/Kt4o4GxYsAoECvlAIwstliP3p5jITMZ21hkx2FtLcNmLoHO05InXMKSbu4tuykDgKV5+UWuA+PFvwdFq2nGp/Sz1f+wiLIHrj9LldZ3EEjAEsGeEY4w2BcJ3RollmVVf+sottTiWeDKA7o2eNharsuBgyeBy7gXAJLJZQzhB7SOsFlZhS3OkuCgSUpIdbhGQRc0Atq+o3qccVvkheS5rrw9jvA47zSBC2qLfuZ1bjwdeeRPUvQkG1CB0GwGnnm1SQJ8gifrPMGISkh02P6VWRrTN4oR9yWTJ3vKfd9DN+ha196LyLMGK9emBfHn6T7ZF8Z5Eog6i3tYQ== 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] Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 111 ++++++++++++++++++++++++++++++++++++++- arch/x86/kvm/svm/svm.h | 1 + include/linux/psp-sev.h | 2 +- include/uapi/linux/kvm.h | 12 +++++ 4 files changed, 123 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 70d5650d8d95..18b64b7005e7 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2089,6 +2089,7 @@ static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp) goto e_free; sev->snp_certs_data = certs_data; + sev->snp_certs_len = 0; return context; @@ -2404,6 +2405,86 @@ 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; + + 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; + + /* No instance certs set. */ + if (!sev->snp_certs_len) + 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))) + return -EFAULT; + + return -EINVAL; + } + + if (copy_to_user((void __user *)(uintptr_t)params.certs_uaddr, + sev->snp_certs_data, sev->snp_certs_len)) + return -EFAULT; + + return 0; +} + +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; + void *to_certs = sev->snp_certs_data; + struct kvm_sev_snp_set_certs 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; + + 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) { + sev->snp_certs_len = 0; + return 0; + } + + /* Page-align the length */ + length = (params.certs_len + PAGE_SIZE - 1) & PAGE_MASK; + + if (copy_from_user(to_certs, + (void __user *)(uintptr_t)params.certs_uaddr, + params.certs_len)) { + return -EFAULT; + } + + sev->snp_certs_len = length; + + return 0; +} + int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -2503,6 +2584,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; @@ -3550,8 +3637,28 @@ static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp if (rc) goto unlock; - rc = snp_guest_ext_guest_request(&req, (unsigned long)sev->snp_certs_data, - &data_npages, &err); + /* + * If the VMM has overridden the certs, then change the error message + * if the size is inappropriate for the override. Otherwise, use a + * regular guest request and copy back the instance certs. + */ + if (sev->snp_certs_len) { + if ((data_npages << PAGE_SHIFT) < sev->snp_certs_len) { + rc = -EINVAL; + err = SNP_GUEST_REQ_INVALID_LEN; + goto datalen; + } + rc = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &req, + (int *)&err); + } else { + rc = snp_guest_ext_guest_request(&req, + (unsigned long)sev->snp_certs_data, + &data_npages, &err); + } +datalen: + if (sev->snp_certs_len) + data_npages = sev->snp_certs_len >> PAGE_SHIFT; + if (rc) { /* * If buffer length is small then return the expected diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 221b38d3c845..dced46559508 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -94,6 +94,7 @@ struct kvm_sev_info { u64 snp_init_flags; void *snp_context; /* SNP guest context page */ void *snp_certs_data; + unsigned int snp_certs_len; /* Size of instance override for certs */ struct mutex guest_req_lock; /* Lock for guest request handling */ u64 sev_features; /* Features set at VMSA creation */ diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 92116e2b74fd..3b28b78938f6 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 */ /** * SEV platform state diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 6e684bf5f723..ad7e24e43547 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1928,6 +1928,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, }; @@ -2075,6 +2077,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 Feb 20 18:38: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: 13146982 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 A9BD2C636D6 for ; Mon, 20 Feb 2023 19:01:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 466DE6B0092; Mon, 20 Feb 2023 14:01:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4166F6B0093; Mon, 20 Feb 2023 14:01:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 290656B0095; Mon, 20 Feb 2023 14:01:10 -0500 (EST) 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 18E396B0092 for ; Mon, 20 Feb 2023 14:01:10 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id D3D4814080B for ; Mon, 20 Feb 2023 19:01:09 +0000 (UTC) X-FDA: 80488587858.18.E452177 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2076.outbound.protection.outlook.com [40.107.244.76]) by imf25.hostedemail.com (Postfix) with ESMTP id 5B4C8A0026 for ; Mon, 20 Feb 2023 19:01:05 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=DtUz3FYM; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf25.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.76 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=1676919665; 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=bhdLB+9fGVwgfwLdW0pGauuauXN7czdXY6hjutnQW1g=; b=N0jI68EkT0/8pK8+P3YiB+rX4S/WmnXr5yjxipNQ2kgNWQvr8YeNNqZ2o6fLV3LjI1R2Io /JV2r5Xy97/fxCP1B+RKgKUIfMG8oaG9nBVmddP++6M4IszOlETII4nCd06RHRz9VPy+Wj lBVoDO+PQwVrlEA0nsP05X0VbT6hcjo= ARC-Authentication-Results: i=2; imf25.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=DtUz3FYM; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf25.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.76 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1676919665; a=rsa-sha256; cv=pass; b=IJlD3WEQGZmD7QKEJMv4euaECEBx5w1jOTiO+V+7FMvglA90Gtf8mT6kafTO757uXcBskw CElUSRLUwRlnZ0fWvFzcEu3+p2lKKE2yy0oxxTOJaQlyvfZTMyhtm/jdM+DWIHqFWFP2G5 dGTR0hJu/ZwV9T54Inyi4TPbdIKdFmE= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DLDu94Nqnh9/vD1yUqt8QADAm08mbhkWREprrsfNLwl9tzkufTtVpAKG0/VVtxi0Eqw49eyQViT8tVrvYcHIyiP+DYTTd32g/nMcSUu6zp0x7G+0hj0WwhZG++MjMcicRDFOegv6veIdLgNS9nq6q7S0GJGru+UOofZIjetqHRtIbQZ3OsZmqpyZORRVejRuKFrjCJKqc5gcpJCd9cP5qsewj8JmXzptQetb5/XkEmtBIK+6LFBYnmOLLLxU3yJ9tJMH7H813NS6aEVGzTqmb9NP5GIC71GmSuQBkLRQsvVnvt7AXplMpmKfUOrdLfXSvX+RgWgVsoL066wnACvdXg== 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=bhdLB+9fGVwgfwLdW0pGauuauXN7czdXY6hjutnQW1g=; b=Zr+Bi4jFXsFxOQ4Hz5nZRBzJx3f7wEIR/b59kp27uG2n7MZPwo0e5XNeVxw5JMyyGSp2QeBc/F7NFbZKYojJ3cigJQgk4y+PBXRP0uccqbLjbfFHnW95oMb3J75HKKqbSB9zS+S/sdQK9pVsZvTgYM0bEjXiB0w8LMB1T/1YZcTbJRYgGuMHMl1smvaXPAYr3pJ+bpP9S8Tv0sUR0MzT1B3ixvBLeAvuxZT21KRJQt6csA+iuVTseZ1sUDiF3S7lZ/69RQYK7VILnZpTNJyoQCV1rlaYmQ2BLwkiuF1yB1tBLlAoFsIerIyYi9FsFXnJFzUbRONJIbkJGnG0jGL5mw== 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=bhdLB+9fGVwgfwLdW0pGauuauXN7czdXY6hjutnQW1g=; b=DtUz3FYMKqlsy4VdJNvf6rrjZ5X0RkEy5lOJSTU2T3awTQh3xKom3q/bh2slCozG0aet3xBiCz2ZJ4+zkBMXfo7CxH2CvzzbHXbFWV/m+HBbbTOwWzQS+UkF/cIekSBWSFROPKSylAxjXYWGHvtG3KQ49VvRBIum+9UFdp4uW+Q= Received: from MW4P222CA0023.NAMP222.PROD.OUTLOOK.COM (2603:10b6:303:114::28) by SN7PR12MB7810.namprd12.prod.outlook.com (2603:10b6:806:34c::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20; Mon, 20 Feb 2023 19:01:02 +0000 Received: from CO1NAM11FT018.eop-nam11.prod.protection.outlook.com (2603:10b6:303:114:cafe::64) by MW4P222CA0023.outlook.office365.com (2603:10b6:303:114::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19 via Frontend Transport; Mon, 20 Feb 2023 19:01: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 CO1NAM11FT018.mail.protection.outlook.com (10.13.175.16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.21 via Frontend Transport; Mon, 20 Feb 2023 19:01: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.2375.34; Mon, 20 Feb 2023 13:01:01 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Dionna Glaze , "Thomas Lendacky" Subject: [PATCH RFC v8 55/56] x86/sev: Document KVM_SEV_SNP_{G,S}ET_CERTS Date: Mon, 20 Feb 2023 12:38:46 -0600 Message-ID: <20230220183847.59159-56-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT018:EE_|SN7PR12MB7810:EE_ X-MS-Office365-Filtering-Correlation-Id: f41b3fd6-8231-470a-4532-08db1374cfb3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: e65c5ls0TTnpXU4cZJQW4nNcRpTjRarYf1eErEGrO6OXnOWPpla+eNKBdyqaSpWOEyCDvWA/4W9lhsYdoF7yvLyEwIeo/PR9dTxUFXEK14QMWGj+MVV8obywmfNFFKd9Z81/EP4GC7HWy0ZUF5YC1gN4pst9KV3h4RzGR5FX41gbwISznl6G2zrzaVqllQuKWuI389zL1Q8R9/HKPOmeuTQvurAO8mw4KJCWZEpylcY6AVeqCYpHuFy8Cax9b8VUO5zMiujQ8k78cZGi6oKyNW3wgDev/5NHv0xPL/APoYRRyqmczau6DOGW3y7dQW/gcyTZSWDDYaL+Jnm72OH7jnSHw0klAkFCU+I+ozTGslmwSvalE8O51aJpIzu9/SI4VWcQNhwmc8rRdmwhb9yLPb5QjBY3S61h2qkyV8xXY68agEXdbKIpz3WC6250dw6HzY9Agqwo5e6n4QIfMgN1D45dEa1pNNXGZgWsfUixOGnfza7LU8FMUt9ynJzVejswFNZd8I9RryxTrrTBhaYhDpa3LxGyZ2LcTH8giMugPKAlyPaXXbVhyLwawpLPbMJxXnFkmWgFK4cGKJ++3YYGNyhPwzyyiBsSfYZGk+lcsjq2JGnl7VWH2TdPWGWdo/2OSsq5SEETJXplsYDltAAJ4QBZAtJnHE3BRBNGIsWbhub+u9EXpKs+g66eM3mScwBMKRvh3aXatC2X4TIKdE4G8D67Px5dNj3dAi4x8lJSHqE= 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:(13230025)(4636009)(396003)(346002)(376002)(136003)(39860400002)(451199018)(36840700001)(46966006)(40470700004)(2906002)(26005)(186003)(16526019)(41300700001)(356005)(82740400003)(44832011)(6666004)(1076003)(81166007)(2616005)(7416002)(5660300002)(36860700001)(7406005)(8936002)(478600001)(316002)(40460700003)(86362001)(70206006)(70586007)(6916009)(8676002)(4326008)(336012)(82310400005)(47076005)(40480700001)(426003)(36756003)(83380400001)(54906003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 19:01:02.3104 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f41b3fd6-8231-470a-4532-08db1374cfb3 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: CO1NAM11FT018.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7810 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 5B4C8A0026 X-Stat-Signature: c5tm67p9sk89tye9p9adiunk3t8yp1we X-Rspam-User: X-HE-Tag: 1676919665-831232 X-HE-Meta: U2FsdGVkX1+Jf0vFlHTH1H02/U+mcx/w+UdALph0T22xc38404Vp9OjkiWJ9XxCNnSs6FPATrsE+QruLKOYnLwOajMrnB0tpORy9CCs6q8V29PJCXJFpwza7IQCM8d6zXYZz140FWOg7DY5RB/ADzLJWMdjYau7kbDopC+D+AVDswAHp0YesPTE3Us4svV0/rQatQg9iZtI8tVLZEU+uWBG/WMnKMfyr+BLduSCyyweh8YRJdwprXAcLP/j7+7vU6x6qPTP30JfRLiCARfUQiZJ5RiljSkM8QHjF27YOkD673UFiAMJ3pp6xPzYDcJkPvlHHHpF9QdJXJmfutvVIDkD5TZsFGUlwGJHLYwpSq9/Ac04U79RoDtO8RkG3j8bEgs6h20hSljZaPluNhG2O/R2IFOaz0pdYJtF9pCor3bcD8nYObcmSA4itGXwhK8i4YIX7tjL01TxRLZMgI2985trtRjznAWqw5Wc8uP+uP4dO6BQZTncobKXCpkZA7d7A7bBPzqujsz0Ao/Whr/3QwGPGQu5mz0nZ+yg3TqsXIQVjE5Ebatl6fDDUe2qpN4yVtgfXqwB18MXDxvN+4qKwGrkc/6pF2ev9hxV7UM74ETNYthES+oCLKaOgRdosH5TLNUpeAe4gaq/ulqROdWVAm8gNiGV3HFj/YtWIQNMsxK3DFYGsh9+qb1AtVJOthCUyc6Pey8+I8ritdkmsDX+ek/acwR+S+Ndtw8AkIknQuyQd1BKIH/1s/pqXBCEdTEcX11tFfc+92S+K6fcCFH55TxLuzvoBDhrUuTXg30rL3NWHfEmf9g6jnUFNE1OdZfAe6iaiSmkX2QocATXGKUj4aSF8liNTQxXvRVMoISVRZzHuqjKzwNXAKf1PZqPX4WhO5XFqgGhT+WtdboxqRzJW6U8raK2kRJG/F/R410HFT1gxDj40nNtZtwNHKT2tQOGeRZzZB0Qxw+zrEPtSYBd 2gidzLuS hQd5n2r9lsWIWRz8eruJaEUMVF3T9TFuufs73sDvXbw+dG7BX3+MNO6FZ1128iyeRc1WERXZYnUmzIt7km/qz7KguyIXhMMxaxNKFkTdoVXu+2eq71kaxGeSTjU56eBpifNv4R+KhVTEA/+RRkJsCDOAaYgnO2HRfRV5F2aWOQqS74YHSYwqUjq3A5RzSDv0mFlXMAaV7nVVmHSTQiC3yPFKyC0wPw8s9XHDFB6pj4BzbtZTCaK1zbUthb/zwlXSP/Sa9zPxji7rhLqT9/qb+Zfxd2a688WupmYwSxdOEstm2AyNFhZKQRR1sJiHzPiGqAxCuQRitMJNpmKquBWKojgsYQZK7+95ZUcVnnfLji6VadNLUZ3BRMgSKOfVpVU/D8ZOLRZ9PpeC4OXioZ/sbj+3D/fMYZQDTM7P1 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 Update the KVM_MEMORY_ENCRYPT_OP documentation to include the new commands for overriding the host certificates that the guest receives from an extended guest request. Cc: Thomas Lendacky Cc: Paolo Bonzini Signed-off-by: Dionna Glaze Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- .../virt/kvm/x86/amd-memory-encryption.rst | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documentation/virt/kvm/x86/amd-memory-encryption.rst index dafb0c9984f1..153003ff2c51 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 ========== From patchwork Mon Feb 20 18:38: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: 13146983 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 54A39C6379F for ; Mon, 20 Feb 2023 19:01:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E2A516B0095; Mon, 20 Feb 2023 14:01:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DDAA16B0096; Mon, 20 Feb 2023 14:01:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C7B686B0098; Mon, 20 Feb 2023 14:01:29 -0500 (EST) 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 B85736B0095 for ; Mon, 20 Feb 2023 14:01:29 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 88259A0893 for ; Mon, 20 Feb 2023 19:01:29 +0000 (UTC) X-FDA: 80488588698.10.347CBAA Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2052.outbound.protection.outlook.com [40.107.237.52]) by imf11.hostedemail.com (Postfix) with ESMTP id 08E924002B for ; Mon, 20 Feb 2023 19:01:25 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=sCzOp8ro; spf=pass (imf11.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.52 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=1676919686; 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=xfgvNgPkzXxiD3rUfJwSEsnsZDwFqlVnWGrip+638u0=; b=EDV1pTEtQVIig3mppOyA7bZUZ6txPXeLonx431PIYiTyfrS/on93DPge7muscvoSLbo03i E4+1c9GNX1GQGF/UNYfa4sRPIP7YSbZ7+9CriaCDLuH8jNMLz/cU6plFIPYUacxAK30rxX RDT5GtUbcNNdQFvBykIpVBzwtd+yJ6w= ARC-Authentication-Results: i=2; imf11.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=sCzOp8ro; spf=pass (imf11.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.52 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=1676919686; a=rsa-sha256; cv=pass; b=h3Pwt0quWsMI51t3x68Fi8eYeTHVrVMnPrvqIgj0CK4zUISpSrrZv47ruffbD2KuZ1mq0O UOLZJQ7k1qPxBdvR0MRdja7ufdD68XL6OB9PHcHqnoPydtxwbA68PVj2MxKyN0gQxnxkq7 zq/emZ1JM5C2uda2J8ATSQKCcZ56/Rc= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Z/Zt95TYguED0j75IMIetOmHnYQyZDjUneQsiw/UtNo+/1/LFd0iGB4Jb6hA4EUY/tIOYSWGFcJnxgAysB6TRUo5o/jN2ROgz6mtJr4g8o5fthDxhPtCSs4/R9B74gsB1VTAIR0chLuF5DBD/jZxGF/mGrn9XjZVwO+tqWB6e+1plYOvzYfChsQxGv3Fb3TC4YIa9tdXH8CloAHaMqtKjHeRs6E4s5FvyHtIk5IuvmvjafnGvhySMQStzeXYFG3PUACBMF/f+P+JnqrU4e6wMUIlOAylDveCievsQmHlJZurdRACyMQkh9ThsmXOKy0RnWpTPB+1UIZdrhcceZejhw== 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=xfgvNgPkzXxiD3rUfJwSEsnsZDwFqlVnWGrip+638u0=; b=CdXECGTskGdUt58NZqxEZg3D0Bd9XbtEUzzArPSedjhUqpkDjCI8FLlvBAkqL7ZM8MLwBQoBW1xRGrtAjaPdrnICSWdrT4Q22isicUOEmYN2+ZzPPj0ANTzqk/dKoEDHz72occEP3UGlvbI6BXoHgu4qelN2wNVlhZvsHlaZtc+vCyTGJgKoJL5fI8m9FtP8TDDbE7O08oMcuEjKk46MtWfaUCWXKbxkwqSKmjk8PdvFqqplHh5TouuQidiajZTvK8ByZrpQlKXVVNxr93I4vf+vnwNq0QHp/0VUWttL3O+4GNDTvDxjwrWrd1urA1Z6g/c68vemr3NnmEOi0X/FLA== 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=xfgvNgPkzXxiD3rUfJwSEsnsZDwFqlVnWGrip+638u0=; b=sCzOp8ro4SFWXtWwIr143m2wbwzFpF050yzqxY2bzyhR3kRQ3HL4X+XUgAohQ92O4Vw2qZjSwUcwLc+ek7MPGY/hGlMpj+dl0nWFNgogWUojUI4WJYemHiI0S8AvvZ+q8g30e5FC2DfbuW7OSR7fINPOCnq9zJviZ2aLX0+/+kw= Received: from MW4PR04CA0049.namprd04.prod.outlook.com (2603:10b6:303:6a::24) by SN7PR12MB7936.namprd12.prod.outlook.com (2603:10b6:806:347::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20; Mon, 20 Feb 2023 19:01:23 +0000 Received: from CO1NAM11FT003.eop-nam11.prod.protection.outlook.com (2603:10b6:303:6a:cafe::48) by MW4PR04CA0049.outlook.office365.com (2603:10b6:303:6a::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 19:01: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 CO1NAM11FT003.mail.protection.outlook.com (10.13.175.93) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 19:01:23 +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.2375.34; Mon, 20 Feb 2023 13:01:22 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v8 56/56] iommu/amd: Add IOMMU_SNP_SHUTDOWN support Date: Mon, 20 Feb 2023 12:38:47 -0600 Message-ID: <20230220183847.59159-57-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT003:EE_|SN7PR12MB7936:EE_ X-MS-Office365-Filtering-Correlation-Id: b369f4ba-f405-4719-1d71-08db1374dc2d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: InLlUBpeVAYIg/MCVuuByIk89FbaZCdlRaVm9cviSpw20tuDsXRgA7avVWo8QYnwlZN17YjjLQ1/KsRHm/aWU9M2vHCrf+TMo+f/h8R5z4zMBvoU8O5PzvJ/IV5J9yGQ6TdnTP+5lbhdvXvkamd66QLV2bwaQbxZ6ZeLK+4UUuffBQQwZoTcQhigB2WF7AhbRIGD3S45WUzjPqc0pH2ETKCfWaGzTIogSRvkXrYIjdM/d6wu4JDUnQ+WJMYO3Eygl/Y1zlxyzGNVuOxRiZm2kBOr7l0OI9GadNKqI2b2Sdl5hfCEuA55R9gfAwiZz2V/agopa/l18f0gMm0uMC7VWndPfUx7xPtrxrS8zIPyGbUbREjJnqyrvUKVYYzYN4quVnyzQycZz+nY63ZjjjblwF1eAC1EGXGGRYnMjVm+o+l6XTluFOEHakbmPLpaJPCbj2EwQeQlhys/KEcP6EOKGnNHXDudZelvxmIlWZbzz5MqFV/uZ95pgzq/egpJTrHrZJEbbCSV7VlphcF1HFJM712E6TjqvPCubfaXk+L7hNyX0N2Q2gvSeazIgNPqqqtwV9KcaUj/ZyekjquDo8B+Chu4wdKoMveoIiAlz+OGrenHpQ4W721ACZPDcEGQTsX42kJmYeMm2YDmtML5buV6gx9qa13bxcMJT2ICIdkdIn/Rt24stqrklW8X5b1tSzp6uy7cC7VD+Sv7CV98WrVoQCYEROplhYxd4OpIZlKxZlY= 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:(13230025)(4636009)(376002)(346002)(136003)(39860400002)(396003)(451199018)(46966006)(40470700004)(36840700001)(2906002)(186003)(26005)(16526019)(5660300002)(7406005)(6666004)(7416002)(44832011)(2616005)(36860700001)(47076005)(426003)(40480700001)(86362001)(40460700003)(1076003)(336012)(8676002)(6916009)(70206006)(356005)(70586007)(4326008)(81166007)(41300700001)(54906003)(82310400005)(316002)(83380400001)(478600001)(8936002)(82740400003)(36756003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 19:01:23.2729 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b369f4ba-f405-4719-1d71-08db1374dc2d 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: CO1NAM11FT003.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7936 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 08E924002B X-Stat-Signature: adcwfxcf7xi77hupkh3ff7z5icmyqg6n X-HE-Tag: 1676919685-208506 X-HE-Meta: U2FsdGVkX1+gmM+uWIJg0MxL3QIR7yIfOw2G3vlsMhOChBiy1WddIJhro+YfeR7js2z+9e8DhuPBNFq6msHdEoxO9jtErH59WKUf4cJxLtnHX5aaHgtqF0NNlGdeXV/Oai6Hvlla7TajEygDk+JQ2pef7WfmmyAeg9fUjhbxcEmhDZOv/yLA2Z/YB+UrbZUh6M4DUOeKDYvQgTOfflkKsmWnOgY57x7yEP+wmMZWeOM/0gZMgZZ95xKoNTXN1ah1IqDoLPjSWgPvz1G9kGTdUH6p/rIYkbNhoWi9r7gGgN13IWpqtqWeNplBd5wsHzFCmnpbC/KtKhGrcwI3k4sZeZ02gRB6T2WfpoQTZSmBzUVyl4dLkUe6T8lKWvq/39rFOxz6YWkm8tBG50c3CKfqd2fnht0FOwy0Wa9myy2K8IShShJIIH5GC9TjbCPJA33QgZ7zTSBn327pQ3n2YwUqXnaOvuPsIrYZ9xcEhcZ57QxkEq7RPE5NJ1z2rXl/HoB99QOSmcgFNk/LgRHLTqREDCKbzotdlBduhrlC6EH7OLnsOl82s8COgnO+cXSTVmiTBySH37JhIj3rH6hHVlihwoF5gEiDXJhQkHBtDdooF7AC2wmtw0shW+af0aHYG+Z8CunaTVdGzhFDlXKwyh8a/mEhczSnSTzJHVdbj1QwKyyW+z8+hV0Ug7qjidR66cVxTMkTFeEqb1BhzMVoluCVKclGaEosg72h2lKycbax1uAIa05u56q0fBwaJLA8fH93Bfpf1fRRNAZkbh7aZ3+5vkKonN2YzfGLinUTfnoahO7G5/YbRwHIvd1fLNaYtkm5QjtIJwVYcckeApNe9o4tPYxDQIqJbew7KCQ51H2igPPgl9av9pITQ4YgMzHAN6jP8wKScA9e7kQ0mIDoYMMdQwMy8wQTS/S/lzaBGMBwMKPecMLv6v0UXsf3dXpOmB1Gub5Y+Z/BJ/5dIvihEIf gByCLqUV xgEkSTmK6K/Q02JWal16bWvlOSyHKlzud1zJwbcsRdRnsu+Z1WeDtocTsDTPeqsIW52uht0Ku1iaJ1a8F0TB81kdD1JOFwuy55IROBdQSwl29aE4roANL8MKW3wRx79Fvm7anXBwNmjqEhwyy9spb8oa/7buodccYAEVh8mo6AwP6r3Lrsp2u6W0s0YdDuCZcYGKfvx27MzdM2FLX5H1YI++Dt4BuD/eQFvxjT2H8mC12vr+XF4mSxb97KnNgG6v6d3Oljz8Jm9LhnOmFvUMThN7cUZcMXAxmCtxszWxAReK3VwASHjAkaWgYkNz9+sw+2tjqyYNfc4T9y7NxiDxmndQGnX8JBgoXJFGZkrGo7IduTwh/ugg6KODKJg== 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 | 53 ++++++++++++++++++++++++++++++++++++ include/linux/amd-iommu.h | 1 + 3 files changed, 74 insertions(+) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index bf5167b2acfc..7ded2f9111e0 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 @@ -1503,6 +1504,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 1a2d425bf568..d1270e3c5baf 100644 --- a/drivers/iommu/amd/init.c +++ b/drivers/iommu/amd/init.c @@ -30,6 +30,7 @@ #include #include #include +#include #include @@ -3651,4 +3652,56 @@ int amd_iommu_snp_enable(void) return 0; } + +static int iommu_page_make_shared(void *page) +{ + unsigned long pfn; + + pfn = iommu_virt_to_phys(page) >> 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 953e6f12fa1c..a1b33b838842 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_AMD_MEM_ENCRYPT int amd_iommu_snp_enable(void); +int amd_iommu_snp_disable(void); #endif #endif /* _ASM_X86_AMD_IOMMU_H */