From patchwork Wed Dec 14 19:39:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073393 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 B4142C4332F for ; Wed, 14 Dec 2022 19:51:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4BA748E0006; Wed, 14 Dec 2022 14:51:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 46A6A8E0002; Wed, 14 Dec 2022 14:51:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 331AB8E0006; Wed, 14 Dec 2022 14:51:23 -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 2369D8E0002 for ; Wed, 14 Dec 2022 14:51:23 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id DDA501206DB for ; Wed, 14 Dec 2022 19:51:22 +0000 (UTC) X-FDA: 80241956004.07.A851C7A Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2078.outbound.protection.outlook.com [40.107.244.78]) by imf12.hostedemail.com (Postfix) with ESMTP id 2019240002 for ; Wed, 14 Dec 2022 19:51:19 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=zYTEtxrR; spf=pass (imf12.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.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=1671047480; 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=PoL7U9aX/ZvjtfMqXC9xO2mIT6ugo3HADJUjSNzOi8M=; b=0rtnTFFdK1ovVrL3XAV5QsbpP0ErEBo7NCis5SBzwEgTOrnN0Kh+HrnItwPwUcS7R20qmp /CJDG2rog0kjGEWBvIbfnNFxWcx674hXqAVF0oaGYClfSTBRs8ax1eKB2Po1cIo5ymbzBS jScMPmDayT603CbxhijKq4lDu0M8TJA= ARC-Authentication-Results: i=2; imf12.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=zYTEtxrR; spf=pass (imf12.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.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=1671047480; a=rsa-sha256; cv=pass; b=4XIj5UjALARMuP7CO3b7x2cvoFYjp8t4EovoT7IxvpgkQTE8Rumx4AfB5nOyTEFXtN5jz2 V+armG3tfY+z6ai9RaPQIyqsljctBTUdr4SZlO4zSoUwJp6hTxjuiwdVg1EL2It1R3SQSC 5znQPfbPNF2FNWLbe8gZYMBInWM5bAg= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=GTwXfETNoFwoz1+ZXziugdKfKKpQNYrcj9jUGIaNnQf5jPCC0gk78tp0X4SZAOSj8pD/KUtaJpmILBZulmZOKvlODqck4h8MDf0iGrPpoXcviZkGOMUJjn8KejkXSE1dpYPSMZCp/1pOUbd93rO5KDb2Ojv3FDdSSuZi4nO9nbFlg5Io5CNt3q038jjYq54/CPhJJvGUaG+POG1ZsmzADhVcezh3NPThfk8eP6tJ39OkFSF8Fav7mDDwkH7d+lpcjEor5SL4STGTSddUvjgwlNmQrPMU+jAtkdk/IeUAeAwO3DvLKQ8jKHvpN3D+YOGM9C1yv7+KPdgrPbZaMYSMbg== 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=PoL7U9aX/ZvjtfMqXC9xO2mIT6ugo3HADJUjSNzOi8M=; b=GvSaNkIPHydlqEE6TPkPQF5+B1JP+WuNrkmwZVkTkLWh0UQTa4MkCsK0MSWXku3pQisECHhig16pOeB3zmjEL5gpdKTYUNcNpn/H2pDQz5s/aHRjQZFJzGcYsPBLyAIUFAGKhJHKRz+BaSV6DTUP2nQ0XBupMKbs6xM6N4FAcu1vmVby02DiVxFrA9sjIOXjNVJqTn3M6AqHsNQ5hRcqCUGr8yGi6kq5Lcib1MzzJyrfLB737yPUiDXzcMuWQuqD4NgA6IXx9JLr86CoFAYRIFeAxUL9zL1nG1+7N1l3TJVnqc3lruC64EO70z0w25x8xkmf+TwK6UvNreUBMiKe1Q== 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=PoL7U9aX/ZvjtfMqXC9xO2mIT6ugo3HADJUjSNzOi8M=; b=zYTEtxrR3cZvihAXnCr8tT9Ec+nQUB8WQWQW1GTwLqTZrKiWTcwma6jTKgrgpF7QzMgJ3zP1W/1sRsTasEMEmwqqKN0MKsr4WaSqWCeJ7cu9Fi59tcu9C1HKcZBwe9Mt15rHDxxIJinUnc6xwY6dmr9jOSNvCE/UdKAN17SvvmY= Received: from CY5P221CA0084.NAMP221.PROD.OUTLOOK.COM (2603:10b6:930:9::14) by DS7PR12MB5840.namprd12.prod.outlook.com (2603:10b6:8:7b::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11; Wed, 14 Dec 2022 19:51:17 +0000 Received: from CY4PEPF0000C97B.namprd02.prod.outlook.com (2603:10b6:930:9:cafe::32) by CY5P221CA0084.outlook.office365.com (2603:10b6:930:9::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:51: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 CY4PEPF0000C97B.mail.protection.outlook.com (10.167.241.134) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.9 via Frontend Transport; Wed, 14 Dec 2022 19:51: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; Wed, 14 Dec 2022 13:51:16 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Nikunj A Dadhania Subject: [PATCH RFC v7 01/64] KVM: Fix memslot boundary condition for large page Date: Wed, 14 Dec 2022 13:39:53 -0600 Message-ID: <20221214194056.161492-2-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C97B:EE_|DS7PR12MB5840:EE_ X-MS-Office365-Filtering-Correlation-Id: b4a6e300-a89e-4609-a3c4-08dade0c90a4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: wwr/Osxya313jUmgCuc3sqjBWHW8EXeDnY7cdpayxzFLy1+VeZA/pCb5+Fsw+MsxrYsuhPLyeIibDIDJ374pgFCsAVvPS8twEp57yWCt8NG2eSuawP/nyYWIkbUFs0KZ4rMXhJpivO7pI02UH42m6jriJK99qxIxO3VSR/FFpEN0UuJVufXiqMWrFYnXeLKAVLM3IdOlZdlnJuMCl23EWeK6+sqAakZ0oi0323njRf0Rj0cfzhqv9ipBteuXHiSyB4Yt8Jthuq3Ht3DqTGg92a2CNPrxTzd7p7uCph1QRmvNsYXJcjpE0lnJIdKNr3UawzCP1lwPOL0VtM6v8vXk+pKUFe6v44Br4yEROFPgfCs3TBP7xAVGGMRQo0DhyA4/F+4sIZqyOxk0qvpiVPPaU36B8Fk8cQLVJARFCodY4fwTGYCU34wOjy4tX9LQ9DbTmFTjyQ0U+x5Umkm4X8H/0B6fwJTWMmPnMsHHLzjT7A7Ai8OdTDggcCNmn1rN+7UJvqlBjRZQTvNm4Tf918tX4JYBVBtvcKLXgkC0tNB15WNhVuF6nM7djAcanRDm+f/ZEl2oGQVSr4DjBEV07fHHgtwdn2OsdgTodg3BPjeeoG1jboNWWzRqlFKzYGhw9k/joynyspwWovUVIWR2ry37YV1HsbBM+nZ1uoMxPaVwQ4dmi4LkxZ0QXB9yLvbyYQeoj4OEXcAA5RIcLuzgnR3WhaIowRdhiWR1EWfDugamr1Y= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(346002)(376002)(39860400002)(136003)(396003)(451199015)(36840700001)(46966006)(40470700004)(83380400001)(336012)(2616005)(86362001)(47076005)(81166007)(82740400003)(8936002)(36860700001)(7416002)(356005)(426003)(41300700001)(44832011)(5660300002)(1076003)(4744005)(7406005)(40460700003)(40480700001)(26005)(82310400005)(16526019)(478600001)(6666004)(4326008)(70586007)(70206006)(8676002)(186003)(6916009)(316002)(54906003)(2906002)(36756003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:51:17.2466 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b4a6e300-a89e-4609-a3c4-08dade0c90a4 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: CY4PEPF0000C97B.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB5840 X-Rspamd-Queue-Id: 2019240002 X-Stat-Signature: pm9xkrzgkkxshki8p14ortm4oh3zqzzj X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1671047479-48923 X-HE-Meta: U2FsdGVkX1+3mv8NGLvvrW/xqV6uXMhVw0swr3gpwWl2rtJMfYLNwH+UQVI92U+t4n1QYVLVVAjr6xrXEBo0XirYE+FCoKygPzbTUlYqrbVI7j1b6oOCgBKroPiNg3BIXxWKb1e8ZJejNkMhHnQlvD1W7ZDdO/FDhyqPPhNiyIEfPYjN6906cmzwhjGmJaqCYMmQBtaliazLhXbhmvKyvcvsGvqvlhtk5C2fTUn4mVekIG3/LbmXBJFz1POXa+iKQOpvBe3579UWD9yQQk9fISgK17aS8Fteiyk4P/sLg00JGYci/Zq5W5lbF5jKw9ujTSnVduZmdPvl/ui1QwrVJSitloS3L9JmZ+dBXDXMHSlFEgk/uiynWLfbx37Vf0Xm/jgqYJZp/ng/LIENn52KnNkZ2FQ1F1cUEd2yOEKKIF/nQdWzJxcNUvIselsi+xTcIkop+fr6vcj5m3nyOYZ1ysMZ5yIeTP0J/3k5r0p7aTEpvCoqzPI0rWxrZ2Bq6ykYfp0W9dLDhEIv2i15ZR9xf3A+R43r5UQWSCCcoc6XEcPBezu6f8pwKdIqgi2BMB2zXEOYm8tty78z1XZsnAtZMVUsbKr7No0XA9eGMUr/msKKtB3PwERF5Tb5DlHRtxMcdVDgkR0eh5/Zr83cr0bDA1ie/k0ylpTL4QgezRlJHXDwL88axzjMQvZexfruGFkJor+eXvSr1UVJsNPvJyQz3sGqHOb/T6ExoDhku44oZXBsDj24IxXwuLdTsTV1md2Cgsm5qVTWamS69OOYEMfra7oKBUtyInM/qSrR2vzqSJJe6pMlrOdW+GG2sB8mMX34RTYCUNhb/J8WdAiFFBNEBRARnBl2OqCF7W4u0CsXieNh0qjDsdDxNbIuNgUnaINHLF7OWAiZq3XkaZya8RL1KiNtFDllVGyXjpZ4BtClnKl2p0LISbu8qwFINrxi44c7noJLk0f/63RgPGydh89 NI2A8482 GcyOQ 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 Aligned end boundary causes a kvm crash, handle the case. Signed-off-by: Nikunj A Dadhania Signed-off-by: Michael Roth --- arch/x86/kvm/mmu/mmu.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index b1953ebc012e..b3ffc61c668c 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -7159,6 +7159,9 @@ static void kvm_update_lpage_private_shared_mixed(struct kvm *kvm, for (gfn = first + pages; gfn < last; gfn += pages) linfo_set_mixed(gfn, slot, level, false); + if (gfn == last) + goto out; + gfn = last; gfn_end = min(last + pages, slot->base_gfn + slot->npages); mixed = mem_attrs_mixed(kvm, slot, level, attrs, gfn, gfn_end); From patchwork Wed Dec 14 19:39:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073423 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 38A47C4332F for ; Wed, 14 Dec 2022 19:55:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A5D418E0006; Wed, 14 Dec 2022 14:55:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9E5978E0002; Wed, 14 Dec 2022 14:55:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8384F8E0006; Wed, 14 Dec 2022 14:55:15 -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 6F3928E0002 for ; Wed, 14 Dec 2022 14:55:15 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 23BA0C0F59 for ; Wed, 14 Dec 2022 19:55:15 +0000 (UTC) X-FDA: 80241965790.21.A675833 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 620AC40006 for ; Wed, 14 Dec 2022 19:55:12 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=eJ22hGll; 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; 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=1671047712; a=rsa-sha256; cv=pass; b=VnZ3Ctnng/WlittKGWDTgvtzMKbitFthVGe+D+Ut7s4jL5vlll9GjBvz0tr58WIwnQjZXb /orcCmB+9rNw24dPEt3OiZbru4uEPlEu+FKw22a3JI4DJ3aV09uIjQ4tMn1WpUvj7jktVa 84baQcrZoIPopL9H5snNV+UfMb7Iy/A= ARC-Authentication-Results: i=2; imf04.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=eJ22hGll; 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; 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=1671047712; 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=j1/j0Qnnz3MPT8azUqqfnGGNMOaoM2mT2vdZ1g+r6s0=; b=XqlIr3urzyB8PyeF0oexY108OLDIPbEWciJT2/xRKKajGQYvMorqFt2kIts4KWIQMw5l12 AEo8bCG6Sd8IGkiDHWTu0tSASf6Nz62AD9tufN/1Yql0PQbQeVEKpAQqg0EGf+eueH7IIF 5082V+Lk86lss8NzyOBrXtxotBF7ENQ= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZeaZ+oNdBIxiXtWJr+dQx3cF4MKN8FuZ3b5rWFZYfMumDerK5fRYaBRTU1L+shXhxQdi0sJXFLkhasnoIwDhe6aqDKcCvBt7p2fe1gBBHzT4hV+6GyGIcrCt68BJs8sez4+qSq0Ui7iZdfFvXtuDhIgVR0snG1ENFqqsfkMVINEV4fHxYD9YZ5z3rvz3LCLTH8AY7ozV+MM2QG4nXIxh9HimLpqWGT24z7z1eCYjZOtcpet/wYiVmhDddPX5EAQHhbafFYrtd2HeCCjLkj8Un/7N5f0zK7QV+sZPA8xqdtWX+0/rSFUIdx2oLQ6f2CH6ZJf+CMjPvKTj655U5t2e1Q== 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=j1/j0Qnnz3MPT8azUqqfnGGNMOaoM2mT2vdZ1g+r6s0=; b=baRPEDtregsH1hTv03uyhcSBtpQqkRwhBHOqY0yWdbBcV99HJhvTWLjznughAn9lsdlCbyYJMuwqYxBufHpCH9b3PeAfz3kqxyLaH9v5QF86oSN8ziNbjrMv2RP4WCa4N1I1kb3qTBlmrQuwdWm3RAKkUKwEtNmndI+t6fZIQIbBqMaid0kpcR2Z+eeY833siL5Wm0o0w2xzkjL7m8Sjs4zDO+U7u4Wq6iMfBd9U66E237rLbQoNYJ4N35ib/JL0CuPjAmj7pb+WLej+AAPuBs0kGFPolRQ6ffFsRG53vrTPo4t2D02N38LTR/6YjXMU9XcBaiAtEDgdpRB29sKf/Q== 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=j1/j0Qnnz3MPT8azUqqfnGGNMOaoM2mT2vdZ1g+r6s0=; b=eJ22hGllH+u660sRa2M5tdZkZykX0xx5+ztW2AIoUoft452z6Q/mvCjJKUWV1wMBZJTJZE3gdLFCmsKLTRyMMnDpfCEGRQofucCyA9umMihi6T2ma9Z+uata6ci4/1VBTP4p+hCnMGqXx/I/0sNZBZT+b0ssBAm6qBc4IEN8I2g= Received: from MW4PR04CA0318.namprd04.prod.outlook.com (2603:10b6:303:82::23) by SN7PR12MB6909.namprd12.prod.outlook.com (2603:10b6:806:263::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11; Wed, 14 Dec 2022 19:55:08 +0000 Received: from CO1NAM11FT060.eop-nam11.prod.protection.outlook.com (2603:10b6:303:82:cafe::fd) by MW4PR04CA0318.outlook.office365.com (2603:10b6:303:82::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:55: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 CO1NAM11FT060.mail.protection.outlook.com (10.13.175.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:55: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; Wed, 14 Dec 2022 13:55:07 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v7 02/64] KVM: x86: Add KVM_CAP_UNMAPPED_PRIVATE_MEMORY Date: Wed, 14 Dec 2022 13:39:54 -0600 Message-ID: <20221214194056.161492-3-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT060:EE_|SN7PR12MB6909:EE_ X-MS-Office365-Filtering-Correlation-Id: fcd5f7ff-4cc0-45e8-0ebf-08dade0d1a8e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: QfTBjlSAW+BeHyMDd2Vio97+Gvm/0YUcxm9lBaEozxID2Io0D0KUd7/WK/uYV5KinKEQL5+BJhvGYHs085XPUrmQRqp7KV5hWguDmp5Qxp6Pekg0MMfHlSNf7q0MFjuDsOsZ9LfJrAv5YUuocJ/bIGfsn8bh/PAosc+rQTEC5929tcMDXizVCsPLD03q+7vbYN8MR9756Q8W8qb+pI9maGjrlL9+3PF5ilBCG27fS9My4AQ3vO3mjfdHDA3F2VzOjl6t4+JBqGzgR2ej8G78wu4ABhOPd5r2oJEQKBLBVkXqITemEibAjUfqD6JinLp1vlbEIWSfksDfvkxYABpmUlqkfduyOUtXL19dKLRyBOQgatgzXDmBx9gpr3hl239w+/qODqwVd2yE7Ib3S8PeHh2JLxeCNT0IkTJNHPXm8ajP9Tq7rHQASA2uYaD00jirX+mPiYvGGnzt4oG1A7fnk/cm+YJOExab8rEkGGyLh2cMLPfDv+o2BRMvTfiRoXlHthg0Sq9TZdvbZHTRfPk1BqWbDk08FDM/hg/li88zjvbRDvCNfeW4FSGxNyPp0Sk90lkvJzfnMyO+CRa+U/HLuReARPVuu4VoNFzllBxHG5mpK37MvYSvbUi3OldKzrjw55ETW7ZA/5tnHUekCtM8IZYcf+gH3VnxRDngxONcNMF+Mj80wMyt9k8BHFoxXS0rO+afztitoxjIu+w1jOD2rmfthpWLAYnmRqSS466TaBc= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(346002)(396003)(376002)(39860400002)(136003)(451199015)(40470700004)(46966006)(36840700001)(36756003)(40460700003)(41300700001)(478600001)(82310400005)(86362001)(81166007)(36860700001)(82740400003)(40480700001)(356005)(426003)(47076005)(336012)(1076003)(16526019)(66899015)(316002)(70206006)(83380400001)(6916009)(8676002)(5660300002)(6666004)(70586007)(54906003)(4326008)(26005)(44832011)(186003)(2616005)(8936002)(7406005)(7416002)(2906002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:55:08.6398 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fcd5f7ff-4cc0-45e8-0ebf-08dade0d1a8e 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: CO1NAM11FT060.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB6909 X-Rspam-User: X-Rspamd-Queue-Id: 620AC40006 X-Rspamd-Server: rspam01 X-Stat-Signature: bqcusxj59fybj5x1bru4cjmqj5wb1cxt X-HE-Tag: 1671047712-148737 X-HE-Meta: U2FsdGVkX1/0iFsbNPAMoDz6bDIGyHXXxqY0N3tkJt+TSDWviXoWwrRBvzwCvINkbnBlNTEp8O8pWH/6iCP7SpXBqxKlSiHlEyyJexkXasY8jrZEZPhvWzJeq5ElrOWcbAnwJWOeCZtu88mVmfdki7s8UZLBMgMyJ8WOHb01ORfMafGUiFrcY6NYZjYVCFXjT36V8u+jxUTC3jor5XYcCiD2S8JNpRjE9h4/r8Q8kpqe38XcJyatDs4SudORz6EStX9VTX2tF/9FXRA3xyMlNrjyHd8620efjzD0QBQWGlYIWyrzS0/i4MHO8DwtgU8ORTAfXAcmrA7sjsvBLheFaqVjOAtJmivpU3P2EFOVaDLB1aCXYrDUcVv+HnIwp4xMnlo/udi+VW/O/eFAJhaOmIQITAFM+p2KoZBr45tJwmdxMA0LNlHhxBuZSZbwPPg9PuMIq3ed0w4K3evGRcO6bC4JxkXPPDdPjzurE4sr2nGN7zdHH62As450V6dQXQ6fkbhjlVyj6XOnKtxUWbotH+Zjza1qcCkewttLnS9cBGs9UKNkWzDJBCrxEhzG8aZBookTDcf3OCrJ8UETg/AiD7GQHjPl38unFPB+xBXvrOfbxn28de7sKYuhBL+bhzMjriqPORtprIHw1BaTYeNVarNMuxEedqVmESm42Dw2wugUX/D8QXbX2gjUhK4WjcA+4vVOoVmaQVqM+Jgee9Myfe+VYgVs4otKnEllTlx9jZAEO1rQg6ccTdtgTRQ2HDv+bCKP5Jz+E/jD/0F7xnqOAYLHDftcrQxDKLou8vl3IIOjLReu5UqJnAtlwP3XPU6JgDK4+QGmMbqkyjMbe5mtazIQu9BYK+vmM7yN0Ftqm9uyt0IQflge12CvSCCUTtWnZwFnZMVm58BId3qAo1GoE0Ov90GobmiR/49Xkj5W3Jq/XJ6zEw5ZZDGHNb8H9CTbQ9KvRWG+w2luPfjTO0Y c1CcAwPM cycEJZ2J1XU14YtkipkZa/0q0npYwCovhBo+hDNyhbb38rylhYO85RoQpvw== 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 mainly indicates to KVM that it should expect all private guest memory to be backed by private memslots. Ideally this would work similarly for others archs, give or take a few additional flags, but for now it's a simple boolean indicator for x86. Signed-off-by: Michael Roth --- arch/x86/include/asm/kvm_host.h | 3 +++ arch/x86/kvm/x86.c | 10 ++++++++++ include/uapi/linux/kvm.h | 1 + 3 files changed, 14 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 27ef31133352..2b6244525107 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1438,6 +1438,9 @@ struct kvm_arch { */ #define SPLIT_DESC_CACHE_MIN_NR_OBJECTS (SPTE_ENT_PER_PAGE + 1) struct kvm_mmu_memory_cache split_desc_cache; + + /* Use/enforce unmapped private memory. */ + bool upm_mode; }; struct kvm_vm_stat { diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index c67e22f3e2ee..99ecf99bc4d2 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4421,6 +4421,11 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) case KVM_CAP_EXIT_HYPERCALL: r = KVM_EXIT_HYPERCALL_VALID_MASK; break; +#ifdef CONFIG_HAVE_KVM_MEMORY_ATTRIBUTES + case KVM_CAP_UNMAPPED_PRIVATE_MEM: + r = 1; + break; +#endif case KVM_CAP_SET_GUEST_DEBUG2: return KVM_GUESTDBG_VALID_MASK; #ifdef CONFIG_KVM_XEN @@ -6382,6 +6387,10 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, } mutex_unlock(&kvm->lock); break; + case KVM_CAP_UNMAPPED_PRIVATE_MEM: + kvm->arch.upm_mode = true; + r = 0; + break; default: r = -EINVAL; break; @@ -12128,6 +12137,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) kvm->arch.default_tsc_khz = max_tsc_khz ? : tsc_khz; kvm->arch.guest_can_read_msr_platform_info = true; kvm->arch.enable_pmu = enable_pmu; + kvm->arch.upm_mode = false; #if IS_ENABLED(CONFIG_HYPERV) spin_lock_init(&kvm->arch.hv_root_tdp_lock); diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index c7e9d375a902..cc9424ccf9b2 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1219,6 +1219,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_DIRTY_LOG_RING_ACQ_REL 223 #define KVM_CAP_S390_PROTECTED_ASYNC_DISABLE 224 #define KVM_CAP_MEMORY_ATTRIBUTES 225 +#define KVM_CAP_UNMAPPED_PRIVATE_MEM 240 #ifdef KVM_CAP_IRQ_ROUTING From patchwork Wed Dec 14 19:39:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073453 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 6E54FC4332F for ; Wed, 14 Dec 2022 19:59:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 131208E0005; Wed, 14 Dec 2022 14:59:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0E1C68E0002; Wed, 14 Dec 2022 14:59:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC55A8E0005; Wed, 14 Dec 2022 14:59:05 -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 DC79B8E0002 for ; Wed, 14 Dec 2022 14:59:05 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id C2803C0F68 for ; Wed, 14 Dec 2022 19:59:05 +0000 (UTC) X-FDA: 80241975450.01.D2A35B7 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2067.outbound.protection.outlook.com [40.107.100.67]) by imf04.hostedemail.com (Postfix) with ESMTP id F033D40007 for ; Wed, 14 Dec 2022 19:59:02 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=JgkA0n3S; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf04.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.100.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=1671047943; 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=c9OMyyUVR8eMaZT1tXrQgQK+Hcvc0WsRBQ75uDBC6AQ=; b=cr/yP+F6TwelY9pJwA4PfBSn20t3CHD+6Gk8p6iZplm7CXxcvqdpsHOS/buA1LcORhVoL5 0MwLg/jawJOKKGnya0snY4aynGbouX1xOVM/xJCqZEgm1/acXOPjJjk7UlWJmZzyx32/2H BUxIwzSyTTEAiNcbrQ++UZJXVew5ak0= ARC-Authentication-Results: i=2; imf04.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=JgkA0n3S; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf04.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.100.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=1671047943; a=rsa-sha256; cv=pass; b=HJuNHjtxqdJFwpnrAqsP/A2nlbZy7gpbs7B/U0ufNno9akn9a55mN6EV4086jEDgQi/TKa thz1hp+//fcEOW6du0wuRP/zZCzhp+QBz0w4zzhqapK/wWl0PEkiv3snAATwgSPFg/V7jD GgMQUQw+GkB9gnupAM3hHTEbCGGPpZk= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jooYwiNZmsaLaKLqUl7rYJpLA4A+Uz0YsJIGIvaYyaDNa4QKamvzuH/TdI83Bl+WMQxQTPR2til4UG4anGO6Fh/oRnBs9/hRfEa2gyZ9WqCu0BPwgkJgk6DORhH68CNJ8DhgYdY6ovmeS/sNAFHqAHpFSGGJdgGBhCFUgdJfAYIfeI7Wgh+CDnHrUN1+u3+8cSN/SdhMGgzr/Mjj0v+hyfjKbRRhbjaflXt2mFn4+LMWR7ZUWZ70m2i7C372YRIWhCznsJ1MGSBm87pUzhhe29zTPkZtr2tv+ClxJjvPSZ3xGv4n/wjO1A6tcbMC7nxLxyIiksmSjfElS0bKAjB19w== 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=c9OMyyUVR8eMaZT1tXrQgQK+Hcvc0WsRBQ75uDBC6AQ=; b=fm4lpT5Vsn2LXI00MJLqc9rSW07ljgbxHYbNEpAcIIsjxdhmf8brwF99RNxvlp8ihPhQJRlIe0dQkyOfdDI/CRyzVpsH8k1hRw3HDCJ4ieZwOSmMY2ftZLN8P2lVxs96I6ziWqVR9I8w9J0iITUacJJN/VLxsSv5IrxuwHRcU9TbmvnA1SVcmwChIV2X1pjrjy4QYCdPGP4a5niIqZGnhYe/+rlvTuXe/g54cDXo4uaGUB68dacgy81ASwub3WeW9/e3yS7VeVzEP+U4LoCvk3siKEmczWPjhNrciKsh4VUn4i/NK6QIn9AaloKroi/05t5YNw84RKezaoGuOw4UMg== 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=c9OMyyUVR8eMaZT1tXrQgQK+Hcvc0WsRBQ75uDBC6AQ=; b=JgkA0n3SIzbwKW9uA1deQkmrVDRQJ9Jmb5B4rm3nONVnNooXzF2b4VT8XIOPdfNUeoQgZars975Bv1ybYOGuI4lCnXI7sSx72ksze2gMTmv9Ct90r3KMtPK80RCJluix931tcrhvfqVxvG1jjIOzYPbcn4XU7LxB7gmKhme8z38= Received: from DS7PR05CA0092.namprd05.prod.outlook.com (2603:10b6:8:56::10) by BY5PR12MB4260.namprd12.prod.outlook.com (2603:10b6:a03:206::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:59:00 +0000 Received: from CY4PEPF0000C971.namprd02.prod.outlook.com (2603:10b6:8:56:cafe::23) by DS7PR05CA0092.outlook.office365.com (2603:10b6:8:56::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.5 via Frontend Transport; Wed, 14 Dec 2022 19:58: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 CY4PEPF0000C971.mail.protection.outlook.com (10.167.242.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.10 via Frontend Transport; Wed, 14 Dec 2022 19:58: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; Wed, 14 Dec 2022 13:58:58 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Nikunj A Dadhania Subject: [PATCH RFC v7 03/64] KVM: SVM: Advertise private memory support to KVM Date: Wed, 14 Dec 2022 13:39:55 -0600 Message-ID: <20221214194056.161492-4-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C971:EE_|BY5PR12MB4260:EE_ X-MS-Office365-Filtering-Correlation-Id: 9dfc367c-6ec4-4aea-1bca-08dade0da42b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 6Y02O8URML45FklxAygMIa+ZCM/fa5PynNut5ZWgKQTA6ztUAqY6Y76SyEX+V9QP6vwvXypz2PCx/aRXc0rsa9UrkVyQ6uGHeQSTJin+jFLkygvTrai9HcJA6e/NGK0ExAyiDJEK1kKXuf8YdNIpUsf2zVLdVCEH+y8tDnj01Sdg4+BS1o2qTysbAD2VrlGLBkb+WoMZD4glNfSQyNkM0fYBvkLXW+lQr4whROflha8qQm7ZRDJGJ5HZjKIqZqAzaMGQFQV6Tjw9VFBHJ8WsQG/nBijBCgvFUo9So7VwKHRHbfKnavFdkBNvNhqCtOmnjweRLtMdTB62ckrYxOppR+H7d62Nop75G14pS3efm8Gm4K2bcedo3CDTtna+10wylvYsuExGtc5+LiqvT5KVjqPLSI10aiCXd312ZKkUTeXblrJ6AJkq0rpNH7WxBICy8cQndx7/kgvgTkSy0Y9EsraZRi7PE11SOpz+qn4qjPxSLqo5/O1VSMakJrQTTjwvLcpg1XN33Wo3+IoG+a4z05VNd9l3sG9aY/eJwVKTlyC+nGgPRPss/w1hEgy+s7jX8B5hZaVgtc2fYUwPT0duiJbFmxnjZPE/D+R2q8wT6ATcfCdc8sEvyegv/bPiph/jjQ/ZcHIIVwgEqjWbc0jvRjufc4AGYHK3dMc2wwvH+S3Q30CQuu91CAQS5zGrabiU+tDttUJwY2CWnuZZ4g4D00oykrNeWYSTTdTbXJqJuLE= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(136003)(396003)(346002)(39860400002)(376002)(451199015)(36840700001)(46966006)(40470700004)(82740400003)(478600001)(2616005)(26005)(36756003)(16526019)(8936002)(47076005)(70586007)(40460700003)(1076003)(70206006)(82310400005)(8676002)(6916009)(54906003)(6666004)(4326008)(316002)(86362001)(40480700001)(2906002)(81166007)(356005)(44832011)(41300700001)(7406005)(83380400001)(336012)(5660300002)(186003)(426003)(7416002)(36860700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:58:59.5326 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9dfc367c-6ec4-4aea-1bca-08dade0da42b 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: CY4PEPF0000C971.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4260 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: F033D40007 X-Rspam-User: X-Stat-Signature: t3wmtafmaydhg4jaei1jcz59kysahbdz X-HE-Tag: 1671047942-143820 X-HE-Meta: U2FsdGVkX18/fbT5Mr0z0/evAiulWlLqrjZaWCRanBS4KEiiY0PWhtVMRL4sKSaGCfyclKZCE8Y6u8AYjTiNYLWW5aWQgn1+6oT0QhC39F+O4CNKJnZMLd8D/6KGlrhCt4cqy1Etkh8a5LPqG6H77U52DXeyM8plzJL0Q9mmodo9ABRnT13eYP5W9Qa+ZwKUANnfGfNcYJWJPzWeLxfEqsvn+DqzBqMrJcJ4ZdtcdNnWfjTmBGY1bgiRAgVEczlqoNEzjsRCT7CVSJZb7zlXGrd2NydxTa8vqLkNgdWlgxqY85uyMfskHtAY3Gu04i3qLVsCJG0QcSjtvPHhn146j8rkL9+O0hEmlPmYO50Aj+ETk0ZxAxXNCWar+5aiyV953qyJ35BJpfHcCGJqcJ3sSYXz2eNnGxec7RctlNaCZNpAxm6OP7YFff7MKLwe3mX0CuOf6zvUxTCJgjmSEBiohv5ZeEyiOa+SU11rfJ3cP1yHwfjCRJKIMnOWeEanpvS9MnB/thB00zbcx3/KWTMUwGaUKu9q7KRRttMaUUufpF0zt+MyFcRu1nFwrZqanxX3GrQk+befsIJo93wpMIs6sGnDyP7KDle65V1lqClhcr3Dt4UjowewjEXIUTaJ9N3nZWi67xymH59nBf0pD4al9ifV4Xh5CkD0KIY2hglMHr8PLJ55x4vkNwRaN+Du+CD37B+4LwXUSaLCFqwdKRj9tFJP8gnCBavq3+jKiIgbfVDgL7pFN5MwCLrpzXxP++VnISCHe14GSj2QVZAMN/fuOKt3dyU6tV0JyCxWGeiI/LXeOQNtgJua7fExvbdzYetvojJ2CD3ElI8saBJVngcgwTlawG7/NNOjDpnKVtVdaBMvUD0kjK2xaGI+EhtQ/Tcd75OrY0q4qFHmeO/CnMqO/SLUEs09hyd05kA63gnfhV9AJHBiOkyf/mx81V56sDxSIV4j+YP/UCrqnCTtfeS wuGv9Wt3 SDoOy 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 KVM should use private memory for guests that have upm_mode flag set. Add a kvm_x86_ops hook for determining UPM support that accounts for this situation by only enabling UPM test mode in the case of non-SEV guests. Signed-off-by: Nikunj A Dadhania [mdr: add x86 hook for determining restricted/private memory support] 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/svm/svm.c | 10 ++++++++++ arch/x86/kvm/x86.c | 8 ++++++++ 4 files changed, 20 insertions(+) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index abccd51dcfca..f530a550c092 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(private_mem_enabled); #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 2b6244525107..9317abffbf68 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1635,6 +1635,7 @@ struct kvm_x86_ops { void (*load_mmu_pgd)(struct kvm_vcpu *vcpu, hpa_t root_hpa, int root_level); + int (*private_mem_enabled)(struct kvm *kvm); bool (*has_wbinvd_exit)(void); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 91352d692845..7f3e4d91c0c6 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4694,6 +4694,14 @@ static int svm_vm_init(struct kvm *kvm) return 0; } +static int svm_private_mem_enabled(struct kvm *kvm) +{ + if (sev_guest(kvm)) + return kvm->arch.upm_mode ? 1 : 0; + + return IS_ENABLED(CONFIG_HAVE_KVM_PRIVATE_MEM_TESTING) ? 1 : 0; +} + static struct kvm_x86_ops svm_x86_ops __initdata = { .name = "kvm_amd", @@ -4774,6 +4782,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .vcpu_after_set_cpuid = svm_vcpu_after_set_cpuid, + .private_mem_enabled = svm_private_mem_enabled, + .has_wbinvd_exit = svm_has_wbinvd_exit, .get_l2_tsc_offset = svm_get_l2_tsc_offset, diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 99ecf99bc4d2..bb6adb216054 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12266,6 +12266,14 @@ void __user * __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, } EXPORT_SYMBOL_GPL(__x86_set_memory_region); +bool kvm_arch_has_private_mem(struct kvm *kvm) +{ + if (static_call(kvm_x86_private_mem_enabled)(kvm)) + return true; + + return false; +} + void kvm_arch_pre_destroy_vm(struct kvm *kvm) { kvm_mmu_pre_destroy_vm(kvm); From patchwork Wed Dec 14 19:39:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073464 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 11FBAC001B2 for ; Wed, 14 Dec 2022 20:02:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AA50A8E000C; Wed, 14 Dec 2022 15:02:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A547E8E0002; Wed, 14 Dec 2022 15:02:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8F6D28E000C; Wed, 14 Dec 2022 15:02:58 -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 800FF8E0002 for ; Wed, 14 Dec 2022 15:02:58 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 5AFFE80EF2 for ; Wed, 14 Dec 2022 20:02:58 +0000 (UTC) X-FDA: 80241985236.09.90B0F2E Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2041.outbound.protection.outlook.com [40.107.101.41]) by imf30.hostedemail.com (Postfix) with ESMTP id ADA828002D for ; Wed, 14 Dec 2022 20:02:53 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=XHy5U2T4; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.101.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=1671048175; 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=GV9jzxpLEX7mGVy+3N+X7Yta9vPU6GqxElewKLPQroA=; b=zzM/xkvNtKpF92xUgwwqvrA0AJULiJZeKxDAhGU1S7R2+MELDJOTMbwyozCqbRXJ9plK5e RkujeCIXgxNtq3kW7oVy8zxDdjq2NJ1LCkEzhogqFkzggnkCI6yksaP2aGQQaT0+PSVrqK bk3xkSXm89Pkeendva/R1mGvuhbgWfs= ARC-Authentication-Results: i=2; imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=XHy5U2T4; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.101.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=1671048175; a=rsa-sha256; cv=pass; b=jmXv7SFTnFaDwEHpk7k0wl/m5NXPBGx49InQ/bxyTki3hCw1/qWiYBt44uwxbLsqPwEuPg krK3j6io0uI/6M+URmnBoAtw1qiXxx0h5P7yuD2AFGjDBqq+HGoyuDaicV6QvMN9LdyYUV /OuH+8IvArTQF5LKUv680fsfLEzOrBU= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IBM3YpozxvDakSECPZjGpfo9DxLwteVKdxkIpc8aid+2Y3kOcdxobC38hProXV1H6k161wUhk+wWazjps9uoqmUTMbBaSUFBNvoyqqKW6X8R9jTSMEam09aq+yHrSOFxsSfPT0K0453Ky0pzGqtKyKZVBj1qqU66vAgeL3kVc2QXT/v5ERhQyqd96+8YjV+qd7MnucnWsbITyE9ZugjpilPoVCx5CgV0FXQhtb4zMnZVdYlAn+16bBbJN6QWmPm3ftJ+TssTYPRliCzyC9DalsRiEmt9m7xLfWGxkD7uMgeRSHeDZX2F4hcCiNvLxZ8Riarh4o+u+xitidol7dAGbQ== 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=GV9jzxpLEX7mGVy+3N+X7Yta9vPU6GqxElewKLPQroA=; b=VMSZXGXmdjHCCA2x2zXKXFvRQBMvb0fklBTaXVUc8AaKXeXSQvbJ/+vhtnXEoJ9RtAPlMG9S3Ujfcnu3duU1nx/6GbBFvAeSiM38gzJBR2N/OGD8gN5FlYCp7zAYnY1yd+42GRgSJtTL6KbFUV16JzB6dB5TQCa4H9J//Bu/edwhPGFDpXdM8ULspE+qekUaqgQmR9ahcd/qyFnYN6s6wJelED8WSrkSUq2guTGJuPXaQxgzRMSWAZI4DRz30CHkD8qMdXX05yF+Rakp0EVOlwyU/dCw+9HwNztomSOm8NIcyxvippROcBt7hrvIzdurLLsjLfDu5MmOZcuJFc9mZw== 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=GV9jzxpLEX7mGVy+3N+X7Yta9vPU6GqxElewKLPQroA=; b=XHy5U2T452CNPyfQxz59PrWtZlKUlwidHwikMLEf2hLEO+8kzberFjYdt93GkdR3LTe2F45LSyl+3zhH39uZk+bHa+mHJcz9/DiK9jyXqyQz7QH2e9CKQ+CbyP2ThGbst0cxi5gMGIygdmaiGf3KmIP8dtAU479EN+ORru8DlA4= Received: from CY5PR18CA0011.namprd18.prod.outlook.com (2603:10b6:930:5::20) by CY8PR12MB7436.namprd12.prod.outlook.com (2603:10b6:930:50::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 20:02:51 +0000 Received: from CY4PEPF0000C972.namprd02.prod.outlook.com (2603:10b6:930:5:cafe::9b) by CY5PR18CA0011.outlook.office365.com (2603:10b6:930:5::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:02:51 +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 CY4PEPF0000C972.mail.protection.outlook.com (10.167.242.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.10 via Frontend Transport; Wed, 14 Dec 2022 20:02:51 +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; Wed, 14 Dec 2022 14:02:49 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v7 04/64] KVM: x86: Add 'fault_is_private' x86 op Date: Wed, 14 Dec 2022 13:39:56 -0600 Message-ID: <20221214194056.161492-5-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C972:EE_|CY8PR12MB7436:EE_ X-MS-Office365-Filtering-Correlation-Id: a0b00f74-6898-4706-c809-08dade0e2e29 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: MYKHjiDhfjZgAIIO6CaDgEuzqlM+a0ijPgpwITnXYXNkZBIF8LUPvxXDiCXpYO0HmB7whPXoopeRUX2M88n9z9uc5kNpUiznygg7SqzWp6/0VRsoEKkcI8FWV5HjCwAisMaysrdgHyaj4/jwSyxyg6Jy7uYgsrwdJ6+rLxeoQCS08iesKTPUErjOPhru9Xj70gZz9EF4I0eHJKCluCdNefJhpPiqPJbl/SZVWVziX+yWdB5AteIdpJ88T4OzoXROmzyUI6JDQjeBKVtWNoFEotmbJvABsa5K6vY8HPEDBN99Uo5rzvYDZ/Is+vpoJmUteW2dqEP/Cmk/uWNTFplEGf8WCirig4khz73pbjTqlx8dhtepMEmuHhN7Esqq/b4PBPjjsNktOc+O6kx1P6rDaS0SJXDZc35bbLJeFIFSmkWt+8JFuEH1miIcgToOoI2RuQTJS6jfYR3OTq9JXDXD70wQTyBqNwXDSrJCJ1lDW1RsprmI2ZYyUe7nxUOZpNyMjgly5J0K60noML9BeOAKaAbWlbMwwfz91Yj6n5VzKQcR4TCtO77ifXHdMY5o/lNy4TURDXb2PgTVlhQZwMJJO742+adQo/IDM9djo8rw075TugyEfqZXnh5puG5gmf0RR1r9vXI+p//wXfKZK1eMG7EtoZzabd0llsOfdGG6rOZfGIiM+d+/IFYmFpToHn1BQ0E9wlnkR+0lGY2fzaflNBzoi8xvaJmgY6Io2uVo0Bk= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(346002)(39860400002)(396003)(136003)(376002)(451199015)(36840700001)(46966006)(40470700004)(86362001)(83380400001)(16526019)(6916009)(316002)(40480700001)(186003)(426003)(26005)(2616005)(1076003)(478600001)(356005)(40460700003)(2906002)(336012)(41300700001)(7406005)(81166007)(8936002)(44832011)(5660300002)(7416002)(70586007)(70206006)(4326008)(8676002)(82740400003)(36860700001)(47076005)(6666004)(82310400005)(54906003)(36756003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:02:51.0651 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a0b00f74-6898-4706-c809-08dade0e2e29 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: CY4PEPF0000C972.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7436 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: ADA828002D X-Stat-Signature: rzukyco5sf6ixxzdns4yofero8m5rn1b X-HE-Tag: 1671048173-265927 X-HE-Meta: U2FsdGVkX1/gxxIyueZu7urzslGttXzNPJSUgHbY7YSKu7S98MJqLvSKqqw++hw7nRAU0jOntid2KuuWyaUfNLJG1ryrvB6DzPTC8HGiKqvc5ulMHDDt0Oq/Vz4xHzhUz7wVgj2uiWQ5/zT8yfPovvpjt5sthmZnaSyvvISf3Oe15ID1LFcbUH5xe5Vf19CQ4SVHftJU79Rvjt16xD++sfeopBtfCOepAYKDkzWQOsha//74CuhE586s9/+oG2lJoe3J80ZiUwQ+AKNb/xmTGhA10ufiTU7q0g1CbtFIP51mMIC3SQFJ6jK+kqx4jx2Nt7cOWfrWwskreevavD7ZikvOM4/IzBx6IrQexExvAPQDrvO7zg2T6WQoqt6Xq5ItT3Zluq9VgE68gdE4IPt8mAp+821g4i++UybK3q2n3BSTXpusMGqyt4mC//Sg3Dllgy6IaTk2GEygH2//WFBETQTiYMmp2ajiQxrkBG1/sJRt+ywyAI6hx/Adfppdkfv3b4SfcayLdhScGNLDDEPVYDU3sBF+JTifz089Z+HD0Oq+efPW1HUf9ypl5ojpNO3YD4R46+CEdN5jaoZn1+IoD80tfbFTc8q5ztvWk1XzNswTBY5U3atgWXRuObkyiQ728mXBeFMNzOQo8VtKfz5PrE4MXKFX25+YX6dbMMAucn8WTHTH6+ir5iUEj2k1DfqzWb983mCTCwHAHsP+s2imKUmKaT2OllGnPinRCqqlyTzloClQ1dhZYlwCobug3AOVXDx9+LtjI0Sibz8PRnaHpPYkQEUkyGdVhqHUNKd2VQE3NpDPXU5dv5oDPYY6VTlUphhb9NPCQZYBRtHy0Hd6wH6HVMA5KcF32yy8hPYmkQJaPnJbFWG/oMpDmXbT/YLNCstPkdSkLFPTnGEWmkUhBC/RnfgF6NyKay40/fWI+j0tiyvjD9DI6Q/FW55IYfU10Kr4foVb3RnrIdexeRV Eifz1k2M EkW+vxRQF34byM/xJhCTJ8IwtxcKKV+BBTzkHVLfdPkMbVJBZM2o+9WtE3g== 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 or a private GPA or not. 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 | 3 +-- arch/x86/kvm/mmu/mmu_internal.h | 40 +++++++++++++++++++++++++++--- 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index f530a550c092..efae987cdce0 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(private_mem_enabled); +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 9317abffbf68..92539708f062 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1636,6 +1636,7 @@ struct kvm_x86_ops { void (*load_mmu_pgd)(struct kvm_vcpu *vcpu, hpa_t root_hpa, int root_level); int (*private_mem_enabled)(struct kvm *kvm); + int (*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 b3ffc61c668c..61a7c221b966 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -5646,8 +5646,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 e2f508db0b6e..04ea8da86510 100644 --- a/arch/x86/kvm/mmu/mmu_internal.h +++ b/arch/x86/kvm/mmu/mmu_internal.h @@ -230,6 +230,38 @@ 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) == 1) + goto out; + + /* + * Handling below is for UPM self-tests and guests that use + * slot->shared_bitmap for encrypted access tracking. + */ + if (IS_ENABLED(CONFIG_HAVE_KVM_PRIVATE_MEM_TESTING)) + 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(). @@ -261,13 +293,13 @@ 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) { bool is_tdp = likely(vcpu->arch.mmu->page_fault == kvm_tdp_page_fault); 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, @@ -281,8 +313,8 @@ 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 = IS_ENABLED(CONFIG_HAVE_KVM_PRIVATE_MEM_TESTING) && is_tdp && - kvm_mem_is_private(vcpu->kvm, cr2_or_gpa >> PAGE_SHIFT), + .is_private = is_tdp && kvm_mmu_fault_is_private(vcpu->kvm, + cr2_or_gpa, err), }; int r; From patchwork Wed Dec 14 19:39:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073498 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 6947BC4332F for ; Wed, 14 Dec 2022 20:06:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 105AF8E001A; Wed, 14 Dec 2022 15:06:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0DC7C8E0002; Wed, 14 Dec 2022 15:06:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EBED78E001A; Wed, 14 Dec 2022 15:06:47 -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 DD0288E0002 for ; Wed, 14 Dec 2022 15:06:47 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id A9BF2802AC for ; Wed, 14 Dec 2022 20:06:47 +0000 (UTC) X-FDA: 80241994854.11.2FCB251 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2072.outbound.protection.outlook.com [40.107.93.72]) by imf29.hostedemail.com (Postfix) with ESMTP id D0BF712000E for ; Wed, 14 Dec 2022 20:06:44 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=q3NnRn6p; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf29.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.72 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=1671048405; 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=oDqQcjWyY2cIFrvOsbgzHIjD9E0pfLSJhyuXKLlLr8s=; b=tJfDs4biDYOsc7zt7EtnJLxfyOpht5lMu6MNyPs3Pcsi9UH6FjWmGiBUtv+E+FnKfI5x3s GgX0qSchK4JaiVPuB4xKYelatNEq90ypWQ0s5TA1pkVpqPO49fzZLU89UieuTKaw5oNVDC fQnU4Xvsnwe9sIzH9YeiLxqV9l5E8+Q= ARC-Authentication-Results: i=2; imf29.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=q3NnRn6p; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf29.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.72 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=1671048405; a=rsa-sha256; cv=pass; b=iz3iHNHC0upFb32V6y6OfhGYCqaaaehbBxnyRKY9/GqLNVAtBTkyvVJcQ/SnjDo7/g2G35 RahVZ0wZv6QSzCw/zH3w7STLzwGhDwRkKTSAyVjQXGmB0//uAQqyBjFZ5IWcHub0VZGi7+ RSqMCT3DzYK2l38cerVLompUoQtIOiM= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=e0sunLe5fQ1Kdu9fSnWOX2E0oV9bFUA96BYOt5fQ0jZhIJ4yW8Ui5UQ/Pcw/Ynd87RKYetMDRRW7Lvp8o0WVslK0AmjQ7vrekaogE6ZzU8yzPyqtBAQX1ITwq+g7DaYUTKQ5CZa43B49G2gLJc+fwxT8cOjOnYHbxDErCm3nmURIPHFjNS2NCXORilfyMu3NVmktT3ORTKqrWtdLwpQPFGXhhNx1j8qrm3jV7RFO1tW0a/ZhdWjlmFe4mC2NJT8hASd2/WU8KxZBJcGolLAy0blhBskmCsMnlmouQZEKNT+98aZnuJq1ZO3aWDTheCO4Fkx203lk02aZNtWUeAqNjA== 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=oDqQcjWyY2cIFrvOsbgzHIjD9E0pfLSJhyuXKLlLr8s=; b=PiKnC8ndnZpHj/5Pd1MFOLMzV/DPzpVO6m6Z54fG68yIkYne9AvMlY4OSGe87Cbv0pfQ3LzU2DfzNvkbuPeWFAo5AiS7k0jc4/D0uHOnxnhLIhvGu2YSXe3hpCJLTq1i+uXOFoxQ5ZaW7Sc1S3RWkkJ1CCL840NGAyv/mfXnH++WyIzMqdMuyxZllBhpyV7q8TYIOWtV3hcwQ0fHsAbaIdo4QWREd/LUDDNXBjv3HaP/GgayPr/8Rhkq4EGOqsNlWV4zoDG4CRmf2Pi2L6fYpFVU889NlaeEYueNxB//Oa09w0JwX5rqMT03QpMZXl1AjgvOFKjmhJgSnh4lWBwmKA== 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=oDqQcjWyY2cIFrvOsbgzHIjD9E0pfLSJhyuXKLlLr8s=; b=q3NnRn6phP8ZYit6k9BrPTqI+hpyNWIKhMIG5KA4xgo2PTLvjAwNSEljd2fUfeTV/qSbz+R2w9p44EAccC6Z8MakBg5BhYDD5+kDC6R94UlcApBst+7Y2YqTf/8zsg/BQHVgPpRgEIjE9pu7YM1DFfFSds6mCWzftfmnsJ4n3OI= Received: from MW4PR04CA0164.namprd04.prod.outlook.com (2603:10b6:303:85::19) by BN9PR12MB5161.namprd12.prod.outlook.com (2603:10b6:408:11a::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11; Wed, 14 Dec 2022 20:06:42 +0000 Received: from CO1NAM11FT063.eop-nam11.prod.protection.outlook.com (2603:10b6:303:85:cafe::6b) by MW4PR04CA0164.outlook.office365.com (2603:10b6:303:85::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:06:42 +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 CO1NAM11FT063.mail.protection.outlook.com (10.13.175.37) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:06:42 +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; Wed, 14 Dec 2022 14:06:41 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v7 05/64] KVM: x86: Add 'update_mem_attr' x86 op Date: Wed, 14 Dec 2022 13:39:57 -0600 Message-ID: <20221214194056.161492-6-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT063:EE_|BN9PR12MB5161:EE_ X-MS-Office365-Filtering-Correlation-Id: 17ec48fb-14f8-41fb-83a8-08dade0eb801 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: j9GdpxQggZ8mc4xddKO8SXNdXrz9pFyCn494klrJeSsZe8mcxu0asGOfJvvyHrFvfwQ7pUNiTXZr5CrMIDTM5HAx83u75Rp5YvLyrxo60zqo7Ecp3wroOPSYOpfM6FBWipe6EWJUhRHwLuVFWkZHh3AxGQ9Q79jrdn0l0Byivk//+Qtjp+EOPqVBpvkt1cVTUgIm0lQf3wNKTGA0wgzrli8ENNbKa4atvS7yCEUc1VntAftxP3Kpygi7Vb1LBMVzulwPj8PBaqmztZgflqu37zwod+ob7YzyrB0XzESccxmLYsYGBYBpKXNRXRKa/Yjk1llPpkV8LbKoJZhN5OuLk/qMcLvJMfI4gkFCyobHvSmActk4vBDRuKzrLkZjxSV8437zCgh3NKJOPodFtSg9CcYiTgG+xKpXWHzNd9eaQowvIqiVEcsLLGqbWybl7IWx28pi86OwsfKgC13tr5UW1YfpZbXUo6glFIaGq+keLO1dA6nv07neg4s7NvUE3oniAdFnENhptGNsL7HiJvfJ7HQFEQLS/eXk5tfgOPvVLho6k/riw6OvPYmZ6YIL5nFRRR7tLsFRFqAoR4KOR4pW4ZfbbdcJSFWMHha2fciPZSjjE74PJDjPxm/AzHqMky5NkMmZ/ugPySp7KQaAIn7Xtj84ISY/uO0Curv5MEcvCqB6KIT57aYk9gv+sKFEzI9GaI4iBChxEHcPBtTCXHnwhWSPkpcStI1DTByPTei5fg0= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(39860400002)(136003)(396003)(346002)(376002)(451199015)(46966006)(40470700004)(36840700001)(36756003)(2906002)(7416002)(7406005)(40480700001)(36860700001)(40460700003)(5660300002)(336012)(8936002)(44832011)(86362001)(1076003)(2616005)(15650500001)(81166007)(356005)(426003)(47076005)(82740400003)(83380400001)(6916009)(54906003)(186003)(316002)(16526019)(6666004)(82310400005)(4326008)(41300700001)(26005)(8676002)(478600001)(70206006)(70586007)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:06:42.2799 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 17ec48fb-14f8-41fb-83a8-08dade0eb801 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: CO1NAM11FT063.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5161 X-Rspamd-Queue-Id: D0BF712000E X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: zm87a1tt9a3dpu8hcm1qwrzcocm4cz6s X-HE-Tag: 1671048404-132237 X-HE-Meta: U2FsdGVkX18iYE9rguSws9QHzsTRVzxgxLSlp0xQ/WzlhRJJsw4+p+3jErOHBYeMSd90XgBZZLTpYl4VeHNn5HJGu3A/XiP+l1rlYOAxNGKfpk6qbcU59UWpa9gxOEt/BRX4mnG1GfY0ZyyM5BcfAXDZd+CeTYB1tGMwd7MzVJGovpJ081GgAjUsmOhZmNQYfP34fetMfzoNTAfrupCg/GVcrCwl1l1s10zctyWwMzAm/Pa934dwe6eVWTgKX24fDlrFXHj8Evvvjja22wj1FhOOzB6ZxA6zHy0tzrjSr/tu9H1Vlf/Hp6Xy7R8AuxWbgcN2ftikmJmqT8eO6JGk6fIKeGd3Xa6k8XGUqK3M9h7N2MHg3jTgeC3pzJqzdB6jxG2TunI+Rw+g2S7deuMfDdTmuNtJYlEV/90ILhSl56hNuw0XMB4wSw/oHnLb0DBZWoKpXnOdsIjOLQmKqd9Cbg/W6YqQgL86qfce5weIcM1DGKm/Xl5k0W4wugP+ZMOUyEymRz1n6NC5wiY2xpmPGQq8Wq2Vfd1vr3vglGL1LNk1cinAurKFAkFGQrn6+ZDmH2u/oWVZ30/9NyKbhU/GwnWZN5rBUQuutg3Ymn9WREOBTJMOUKOzv4KAcvN4Y4dkRwujgOmfMZpNj6GrU2xZBh20C17xVIMGScU0yvfkUfCoQGeQW3fuUiCHRl/zcV5rDQQl/6xGwPT7A5RWWUFZvqajFIAHuz0u1GDXhC8JHnUCBSlYdpPVPqKMETuVirpiM2nBQkHwj3JHZ/KnQzFzwADX+T6gqyN+NRP9KtJYN44ZsQeVcoyQS0nrx0JVYwwNQCYcnwfAu8CY/CI2LcVVTOQFM8SMXu1TrD/VyTYPn5IbxAmQuhGsLf1cKjleJFSOC65IgcvsSYG98UUov4hRdyTJzmMb36f6ixXtjej8ccfXLQvi7pcqwAY5v4Ex1lfZUeVVL9sK7SNmTPALHlV p722wdC4 opf+ihcoLhiH6Bo3BYxygdwJl9eTlwlo2MQBK32jHU3fUUHcyJj+flYSgFA== 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 handle any platform-specific handling needed for converting pages between shared/private. 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/mmu/mmu.c | 10 ++++++++-- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index efae987cdce0..52f94a0ba5e9 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(private_mem_enabled); 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 92539708f062..13802389f0f9 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1637,6 +1637,8 @@ struct kvm_x86_ops { int root_level); int (*private_mem_enabled)(struct kvm *kvm); int (*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 61a7c221b966..a0c41d391547 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -7130,7 +7130,7 @@ static void kvm_update_lpage_private_shared_mixed(struct kvm *kvm, { unsigned long pages, mask; gfn_t gfn, gfn_end, first, last; - int level; + int level, ret; bool mixed; /* @@ -7153,7 +7153,7 @@ static void kvm_update_lpage_private_shared_mixed(struct kvm *kvm, linfo_set_mixed(gfn, slot, level, mixed); if (first == last) - return; + goto out; for (gfn = first + pages; gfn < last; gfn += pages) linfo_set_mixed(gfn, slot, level, false); @@ -7166,6 +7166,12 @@ static void kvm_update_lpage_private_shared_mixed(struct kvm *kvm, mixed = mem_attrs_mixed(kvm, slot, level, attrs, gfn, gfn_end); linfo_set_mixed(gfn, slot, level, mixed); } + +out: + 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); } void kvm_arch_set_memory_attributes(struct kvm *kvm, From patchwork Wed Dec 14 19:39:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073503 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 688F8C4332F for ; Wed, 14 Dec 2022 20:08:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 08EC98E001F; Wed, 14 Dec 2022 15:08:34 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 03E708E0002; Wed, 14 Dec 2022 15:08:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DF9FA8E001F; Wed, 14 Dec 2022 15:08:33 -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 D0E368E0002 for ; Wed, 14 Dec 2022 15:08:33 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id AB7EEAB659 for ; Wed, 14 Dec 2022 20:08:33 +0000 (UTC) X-FDA: 80241999306.20.74101FC Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2062.outbound.protection.outlook.com [40.107.94.62]) by imf26.hostedemail.com (Postfix) with ESMTP id D90B1140006 for ; Wed, 14 Dec 2022 20:08:30 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=1sNyg8++; spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.62 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=1671048511; 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=4PugxjeKUReufCsaUR5iD8KRKurR+tDaJmIsUTovgDI=; b=UrJK8eHq/mwFOEnnUX07V+qJ3RwaKjKM17bMZ4ASERZxvhWFeQdii+kI6zyxM8PZKAg3LG za3riTay+qcl14CX5l8dpuFb/ny3Opsg2ZFh4RjS5mo7BmQnCVIisLtY9VT559Mouh7I9B 4oppUCxk6qbmOp8C76HN6nMdHKDejsI= ARC-Authentication-Results: i=2; imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=1sNyg8++; spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.62 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=1671048511; a=rsa-sha256; cv=pass; b=6prg2Rjk+iPIdetVIvCBKgkqxZYWZFS/WyW+X0lj+mtHVJZywyzmT6dxGaaqGhoPh7aruj cYARHXnfY2qFGbeVeEQxqQtyC8FaGrdZ3FUXbbYmxKj7goGBu4ZWiMBTBUfoROGWrFAF73 2+s+8bzkwHnkLzM9z4n2MZcjoiRwVy0= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IW1LqFlxYaAZcf0tG59JNxLFJwnTJu7ESOecM4DkS5fpX884bakIZsnv0Wzrd3Wpzq+VuCyI88ulo11qM3cNamcV1z3pV9NMbQlcN5t5SMRCnQxDofPGkeIq/ERtCVuId9SPcQsaEhD6aqJGCHie4lU0CodkrIj1gpfLlYqNkemQWwtGHZ9GErLLT5luRE0rk111MNPowPwZXhgnEXJCbxYNEFcki18vSHgbKHfevD7y9XcOa6hcCqD1IIbgFIL2qvDHR76RFjJlQj0WR0MLWbmSXZHcsSm7QFZKxDLfrJvbifc153r3cLsU57bccc0Z0rmhYMBWSkLtsQt2f9u1iQ== 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=4PugxjeKUReufCsaUR5iD8KRKurR+tDaJmIsUTovgDI=; b=lp/r6lEs3PHiKF/N+d33K6MLeiMdlsnndLQ+tOieo85pWBIXpXiG1R8JiFWnE4Czj7wCMH2K/B8Yd14PUtE/bSjmcvypdD0hjr6Jh8ocwhYM9LqyjBOf/tLbm/mXmsrVHLL9Hxsl4rJQYA4Yqr+uOAZkB14JxOAUWBhR9ztSye7D1Ak+Nt/U5c8HpxUP1y/AjeWlMyRZU9x2/yvuvQwF/63sMgzsTqBt+auB176V5AyMrR41qQOJZGw5KAkJYrQ6KvRIIfrp6M6u5Zrha7/GBgIPSXRBPLXuk/WwRlZGom1kIlBRLB7n0ahu4kV21n076Fe2dRBVmuutO4IUDR2eNg== 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=4PugxjeKUReufCsaUR5iD8KRKurR+tDaJmIsUTovgDI=; b=1sNyg8++djEc+T5MQZld18lcz2OhVnwGmcOxKHz46yWWZ2Oo8FEoL3aoDHXZYGJiHCJ7v3XkCXtC6IX72f3q2p9RqEqewnowPErBeuT6TtqvIqNi4MYd6F3pSsSO2GPwcYpPidcZtLobK048Pd4iAZ7WjT+j3S5CEG8vL/rI1AM= Received: from MW4P220CA0020.NAMP220.PROD.OUTLOOK.COM (2603:10b6:303:115::25) by SJ0PR12MB7066.namprd12.prod.outlook.com (2603:10b6:a03:4ae::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 20:08:27 +0000 Received: from CO1NAM11FT044.eop-nam11.prod.protection.outlook.com (2603:10b6:303:115:cafe::82) by MW4P220CA0020.outlook.office365.com (2603:10b6:303:115::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:08:27 +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 CO1NAM11FT044.mail.protection.outlook.com (10.13.175.188) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:08:27 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Wed, 14 Dec 2022 14:08:26 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v7 06/64] KVM: x86: Add platform hooks for private memory invalidations Date: Wed, 14 Dec 2022 13:39:58 -0600 Message-ID: <20221214194056.161492-7-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT044:EE_|SJ0PR12MB7066:EE_ X-MS-Office365-Filtering-Correlation-Id: c3f862d7-ab99-45e8-cd93-08dade0ef6ae X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: g+y53TCxwyYfogXia3NgoNVSRPB5gpXUDeGtMMpLuFuVX0+hghRKLh7LJYPVurhKQFbm1cbkNerVh9y5RgwlBZ7IW+p68ZzhIeIMIJc9hrfVvrw3Lg0GUYfPwayMZ1NYJIJRNAhI7Pyk377XDq5shB5Sj8AMN2OtuoGvypoIXI9rQb/jEN46vPIWuqptorgONyq1n+3tHZsf0JnxxLcF5etB3R/skpyyNAz0mT7uwPiB6VYkuU23z/LHfB0M5rn/9arKU97bHlFyCsdbPFH48Lv1oH8NgYylyVSdMxjDSzNBZGqujp3RyB82olOxhYXYGJ3RpYh6FgF9y32ISswZoBELdexj07YKOLEYbI6BE41u+bXblqpbukUJnOVA8DrQI7O0LOi6qjtL4UIuetRafpliFcUJ5GDoBa0G9OFWa00Ntio8X885ACX8zTx/qggWZEyjfkCzEcEdX6DEm5IqvRsasERHNxt8GGtf5brQceQ8P2I6zI3bUmJeK5lSvn80FwEeNYMHkkoBSceXn/AAyg4HL8r6VW2PSQF44b2kpONI2ddbE2kczlaJ8k6r3JsDwjKiKUln/WwOyJ4MKT4mCmp3Z0zwFQLwtmiC2W9S2xRt1NcaAbQ/eU9SQkZnYntCggybhSl4Cq/KH9Gt9sVCeRwKJ4eOySh8wNdF55yTFbp9krr5E+szR0NMKzTP2C0eHhqvh66QumAk7RzyDAMtOStxunEs63Vc2zw7tnzL7fk= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(39860400002)(376002)(346002)(396003)(136003)(451199015)(36840700001)(46966006)(40470700004)(7406005)(81166007)(356005)(36860700001)(86362001)(5660300002)(40460700003)(478600001)(7416002)(40480700001)(41300700001)(44832011)(6666004)(6916009)(8936002)(70206006)(4326008)(83380400001)(54906003)(316002)(47076005)(82310400005)(186003)(26005)(336012)(1076003)(70586007)(2616005)(82740400003)(8676002)(16526019)(426003)(2906002)(36756003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:08:27.4499 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c3f862d7-ab99-45e8-cd93-08dade0ef6ae 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: CO1NAM11FT044.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB7066 X-Rspamd-Queue-Id: D90B1140006 X-Stat-Signature: g6mx8c73f9phdxyd7k83w3k1hj9hbhk9 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1671048510-312765 X-HE-Meta: U2FsdGVkX1/N6+ok0Uhu67u7r6ngId2sh90eNRnZxBL1d4eA9N77vCown16srFknChjE/FvDDBsQQzG0Fm28h/NA31BTUlRvuKuxPBEtD4ztmABpSUtNlr7ba0atorCGV9xt+klK8AfRJA/oIaJ9g6AZlhD8R/jrDkpRL3Qjt52DN5lsFziX3d4QGRnceG060nvzpjgbQ2iGf0kD59q02Jj5hKQsH7m6qfdFAPzSVUA8Mzlbm+uuvC0RgAtXvqVxlJvu6YeSxpIEzYSlsx9xc4XOEIQoolHWTFxpo2IYoDGs0J+Za3UCky2vv5rWGvwCJvCFpfIkfESZFLZO62gSr8RBIQOtzWMlVOXkul8c0vQ1NTKA2XyU3OCLHlt8LhTqxiFvwX8Yf/FiZSkMbFfwC0onz4rLIwY8/A0u2YRPMVuh0CWXRdKDR9+51CXqEshWbIPnEhYBV9E2z4vTxsz0zuM4tCx2gq9rvqOcruZtkGQC1RflidQLKebpB2uxra/Bo3hbfaZacSngBZnD/0UyiZFqBWnv+z1uvzGvGv0voHalz7fXZl0xI1Vnb4JJpt45xbp+6dSak8m0os/J6xAwBvNQkkmeoEaoEhB2eB+0z0Ich17R6AcV/fwKVCDwZ8Ai0reBIk4q8Vpdh1xMfFfK8n5INfKljoKa9MoySpacuIwhIr2xbuYICWaVTUmT+Dk+ORMFg0ZvXuOJyZ13FNmP9SzSH+91ic06zwxls9MZEP9MjATPqGgO5wGoCPBkic3ADO6SSMnctF+GwXZeGGCxJt/UjTgBObokA/4HcNHrGTGuf7P1Ec0RWnWO8tDazTURdx7tAeRzRybLHSSfdVNs7RvQ653z84WzZFwaBhPmZi0x/EMI9R4+kP87yJVn9qWYY3hslsnHXFLt1apfcHdukUV4vKvsHMjXQe23TlhEzW2uq9wk5wUpxyQ7N2RTXEB1rqKUskpW3+c6evtZWbs SiBk1RU9 Lf2/A 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 unregistration 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 | 2 ++ mm/restrictedmem.c | 16 ++++++++++++++++ virt/kvm/kvm_main.c | 5 +++++ 6 files changed, 30 insertions(+) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index 52f94a0ba5e9..c71df44b0f02 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -134,6 +134,7 @@ KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons); KVM_X86_OP_OPTIONAL_RET0(private_mem_enabled); 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 13802389f0f9..9ef8d73455d9 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1639,6 +1639,7 @@ struct kvm_x86_ops { int (*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 a0c41d391547..2713632e5061 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -7183,3 +7183,8 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm, kvm_update_lpage_private_shared_mixed(kvm, slot, attrs, start, end); } + +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); +} diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index f032d878e034..f72a2e0b8699 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2327,6 +2327,7 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm, struct kvm_memory_slot *slot, unsigned long attrs, gfn_t start, gfn_t end); + #else static inline void kvm_arch_set_memory_attributes(struct kvm *kvm, struct kvm_memory_slot *slot, @@ -2366,6 +2367,7 @@ static inline int kvm_restricted_mem_get_pfn(struct kvm_memory_slot *slot, } void kvm_arch_memory_mce(struct kvm *kvm); +void kvm_arch_invalidate_restricted_mem(struct kvm_memory_slot *slot, gfn_t start, gfn_t end); #endif /* CONFIG_HAVE_KVM_RESTRICTED_MEM */ #endif diff --git a/mm/restrictedmem.c b/mm/restrictedmem.c index 56953c204e5c..74fa2cfb8618 100644 --- a/mm/restrictedmem.c +++ b/mm/restrictedmem.c @@ -54,6 +54,11 @@ static int restrictedmem_release(struct inode *inode, struct file *file) { struct restrictedmem_data *data = inode->i_mapping->private_data; + pr_debug("%s: releasing memfd, invalidating page offsets 0x0-0x%llx\n", + __func__, inode->i_size >> PAGE_SHIFT); + restrictedmem_invalidate_start(data, 0, inode->i_size >> PAGE_SHIFT); + restrictedmem_invalidate_end(data, 0, inode->i_size >> PAGE_SHIFT); + fput(data->memfd); kfree(data); return 0; @@ -258,6 +263,17 @@ void restrictedmem_unregister_notifier(struct file *file, struct restrictedmem_notifier *notifier) { struct restrictedmem_data *data = file->f_mapping->private_data; + struct inode *inode = file_inode(data->memfd); + + /* TODO: this will issue notifications to all registered notifiers, + * but it's only the one being unregistered that needs to process + * invalidations for any ranges still allocated at this point in + * time. For now this relies on KVM currently being the only notifier. + */ + pr_debug("%s: unregistering notifier, invalidating page offsets 0x0-0x%llx\n", + __func__, inode->i_size >> PAGE_SHIFT); + restrictedmem_invalidate_start(data, 0, inode->i_size >> PAGE_SHIFT); + restrictedmem_invalidate_end(data, 0, inode->i_size >> PAGE_SHIFT); mutex_lock(&data->lock); list_del(¬ifier->list); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index d2d829d23442..d2daa049e94a 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -974,6 +974,9 @@ static void kvm_restrictedmem_invalidate_begin(struct restrictedmem_notifier *no &gfn_start, &gfn_end)) return; + pr_debug("%s: start: 0x%lx, end: 0x%lx, roffset: 0x%llx, gfn_start: 0x%llx, gfn_end: 0x%llx\n", + __func__, start, end, slot->restricted_offset, gfn_start, gfn_end); + gfn_range.start = gfn_start; gfn_range.end = gfn_end; gfn_range.slot = slot; @@ -988,6 +991,8 @@ static void kvm_restrictedmem_invalidate_begin(struct restrictedmem_notifier *no if (kvm_unmap_gfn_range(kvm, &gfn_range)) kvm_flush_remote_tlbs(kvm); + kvm_arch_invalidate_restricted_mem(slot, gfn_start, gfn_end); + KVM_MMU_UNLOCK(kvm); srcu_read_unlock(&kvm->srcu, idx); } From patchwork Wed Dec 14 19:39:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073522 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 1B5E6C4332F for ; Wed, 14 Dec 2022 20:08:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B14FD8E0020; Wed, 14 Dec 2022 15:08:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AC4E98E0002; Wed, 14 Dec 2022 15:08:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 93F2A8E0020; Wed, 14 Dec 2022 15:08:54 -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 85B5D8E0002 for ; Wed, 14 Dec 2022 15:08:54 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 6A6D7A0977 for ; Wed, 14 Dec 2022 20:08:54 +0000 (UTC) X-FDA: 80242000188.07.6CADFE2 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2050.outbound.protection.outlook.com [40.107.223.50]) by imf26.hostedemail.com (Postfix) with ESMTP id 7B349140013 for ; Wed, 14 Dec 2022 20:08:51 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=uthl4Eu3; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.50 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1671048531; 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=SzMxwYpfyh7XFqtLB97O0RIzVpulsDfAAiXFKloaZjM=; b=vsoJyzUj9aRd1EX5AzxZJ/Z415aJpACl3Fnig87fBPaqXpzI0bfeZyTe+OUioxYXRyZ797 U4BM3ifjwMNxT9LoaiYevmIl+XQIrvOOQvudHBLvDddIw2aoG6EPYgHdVNOqApoKSRV1OT cX6fLuapbK3UNYipZY9ijjEKQ0mZs0o= ARC-Authentication-Results: i=2; imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=uthl4Eu3; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.50 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1671048531; a=rsa-sha256; cv=pass; b=pI+LKgEHJS9uVoYkGn41NphIUgGMY08DZO1m/Q6FE5G+FS4DWlD6h5IRKgmuFSV6grs25s pJgofU2R/ym3OqbWFmZdA2vHXVf3aGleuFk35DDhMtREkyh9tCZDjC+M4wrkBOTT2Df/1X nOHj/NWeMuJPwpJ4aFpKiFiN5677OhY= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=GphtGA4gOyGS0xHy7Jz9c2jBm211YnRlti0Bjzh6rhVZWFph8cVR0dSnDA+zqwUBu4innzWZYXhzA+3lKDOgREaPdQBCGWm5OU4tPjkvRGtA3R7P6dxmNC12c9IorGa3WSZy5GHqAgUh0iaFrJQr5QVdfvLC5eY2fSyMpjdmPRASCv5VTg4Pbv/9XFhHAYd5/i+WQBqJUwhVgnYK0yyqgU3k8GXWnXglxyiuZYH0OyRnZrV8IXuCSL+ntFu+0/GLHC0o+EWOsnCrd1HjkO/JkRAUM5ZkU68DkfbUUe+v4rS6mGB4d3idsWoS6mJV2ID4gvIRTFkLAdV1NWJOpwbOEA== 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=SzMxwYpfyh7XFqtLB97O0RIzVpulsDfAAiXFKloaZjM=; b=g9kLDrfaaB+4Ym83OPzEfb0b5PB7y2BjVX+esu98sqvOpd2d2MglXHYc/EhAM19VSJkIyMyHz/kSeZ/ZKZ7dCnnNl/4KB73/U325HEnuYFbpYrhJst9EdWN1KCd8ajEnECQvyCnDXzj7udZPujvWI7DBw7uPG/+uJ724Vjqo2SHPsW8x5NsNt4xJbyUbRDO1kWjglQhVndjZELLR74OSCQ/Kulc1LBquEMy3x0QuqJJ+nNnt9jT1bhdVOqe6XEubE7LLQu7ExikJSJ1ZMeF4KedouRql3qovR/SPMRlyqHd9jT2WNmLbzvbIDh1CZuO3G+sWB9bm/DUbyIUprqRIyQ== 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=SzMxwYpfyh7XFqtLB97O0RIzVpulsDfAAiXFKloaZjM=; b=uthl4Eu3LMMMLNn7XH591fraZvSbUrasyB1sJGFToQAidqp9IldXjkIjnxNMHneb2RLNR33gA6eYDWfXIX8Cmh84nOBsBXlq3vvVNwNZzgGw/1Uz4YR0VmPW2Ve9lf8EneWGpZ/FNXLQFv7vqTsBTqNp80K8/MAub5YTb7T3cW4= Received: from MW4P220CA0009.NAMP220.PROD.OUTLOOK.COM (2603:10b6:303:115::14) by MW4PR12MB7141.namprd12.prod.outlook.com (2603:10b6:303:213::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 20:08:48 +0000 Received: from CO1NAM11FT028.eop-nam11.prod.protection.outlook.com (2603:10b6:303:115:cafe::14) by MW4P220CA0009.outlook.office365.com (2603:10b6:303:115::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:08: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 CO1NAM11FT028.mail.protection.outlook.com (10.13.175.214) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:08:48 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Wed, 14 Dec 2022 14:08:47 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Nikunj A Dadhania Subject: [PATCH RFC v7 07/64] KVM: SEV: Handle KVM_HC_MAP_GPA_RANGE hypercall Date: Wed, 14 Dec 2022 13:39:59 -0600 Message-ID: <20221214194056.161492-8-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT028:EE_|MW4PR12MB7141:EE_ X-MS-Office365-Filtering-Correlation-Id: 7789cd11-542a-4e61-74c6-08dade0f0342 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JxE58TbV2E+WNdeUgEh6tyiJlKDbEUSBe77xbaJRTZwknrvt96OxXLiReLBAzTtnFOie/h2pSRT1fpt+DFKuqqwjK7c3FnuzyLAs8IafYkYT6A8HNyt0UsYROuBx+9FnyIUPYBd3adqej8/TRNxGjavVOtOwlCADwPu1xwGCsseTeQjJtonZac5WcEiSKasjZQz3OVbcME4bOsqq9t3oO/2God62VYAG63mEtkjGxZmRmZMDbgb6+AG/0UOAmG5DWBWGjWMjhGZmz03bcYHaky+9EDxcKiNgAz7twCplYUME2r/OR2oE3iyfEtfzBL3Kcsd8jvl9zqBw0bWnPXgGPxvuG1VUCkrDag72qQF7bYqYh9mao9LA13Wvcdw+E+X2f9J9tp9EIXl+092AJVaQ4pxo6qrbgduOHvxOPwczsYcCZpwPEeEhsMjFSpAZmm+fObeUEB3XlBNKCPph+Bo9uONDQp2xsmj0MiCEyTdqtJwIHr9OYCugyMH4niK7NAmgEtW1W1vpKWHozbO9WIxzI/McW6/b3qAyJurowjy5F47yLIH8XX0CUupUt6NKy1oPragHITkRahcNC49yna8p2H9ipoPJvDic5LEU4oFl8MUOC0BrgEdIRkOsSZ9Z3nIhDVJsoc5/9JnIIyyOiZEVEHok6N5jS2kn9wBm/XaLks0te3qsTQ40Pw5aBAPBmRIngVkWb6uB70Smx2VUiowybJaQqX40lBAQHilMMQH6Hds= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(136003)(376002)(396003)(346002)(39860400002)(451199015)(36840700001)(40470700004)(46966006)(8936002)(6916009)(5660300002)(70586007)(70206006)(316002)(8676002)(6666004)(7406005)(7416002)(54906003)(41300700001)(36860700001)(26005)(36756003)(86362001)(82310400005)(4326008)(2616005)(336012)(16526019)(426003)(40460700003)(186003)(478600001)(47076005)(1076003)(2906002)(356005)(81166007)(44832011)(82740400003)(40480700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:08:48.5346 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7789cd11-542a-4e61-74c6-08dade0f0342 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: CO1NAM11FT028.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB7141 X-Rspamd-Queue-Id: 7B349140013 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: mc1uwo9argncduhs1ts9t7bx5qhc3um8 X-HE-Tag: 1671048531-33937 X-HE-Meta: U2FsdGVkX1+CqEvMxs40X8CZB/AquYw9NM5rDSVRbyJ0Dxvpd1t1iV77Z1hPd7woyUJllBlLzsOlVi/TTkplxvkIZavbLudLrg8lkTDC4+KtRs5cldrXvqUNPYt/Q0Sy1IJIvDdBlai455KXP7B1fQnburknHfMA8xf85/E049qu+JRtSpXSHyugMGnFz3fP3eXcIH+gOV2pCUHhT8nrFfU44QlgG3mGL8X0wscbnDystHXL0onAeP3yRy01VDlJK/09v9hmqXyRfi73WGN+HtztiEyYqHakwQLEeftnPZvvHtapmCsONci2jLiZq1dnXFQJTa08TE1ZFB6GDQF5vy2jaUR2/8WanzVtitLlyRl7FU+C0X/VWohDrbtH3GWNmmHqluZlrgUHZ47iaXboZmna5Md9/ZDhfi61sFs7aAWRg2zm0dol/gmSoqMzCMc9LEmvqIhW305BKc0jgmODmkU5GZdL6UsuSzdiwVws8qrpuVdUscGd83XiHwrsznPcLWgBcgpnqno13RQyXtXvGeGvdxzSq8Jfk8Fcmy/+iChvml+l9XRxMuY5hIhPzXOEfFirUBdz+BJ+keFOiZjGfgddUATUyeIXJAaJbvqHBldpXTnoLxF7VIsXsnaUZglZsNKyos1Li0kImt7sNgySQCrpLig1H2LXv6WR5Iz+YyjeF9b2haUdbdaWwAFZ0OLwfK0f//6z/sfv795XwKmD8l2ogpJLHnX+zZkykllFGjSjXJXrJw3ompLIS0TlX59CCewW2/2gj68yBPYC6ruQ9rSYZgaoVvZqo45vw+yk1eLewW/xZmgKYa5YUhFkUFXk62GpFD7zA7gHNxn3JL4DpqR+wQ7IACc+hjKt5LFsU00JN9cJExPGiMBNkS2fffQ2Bh4XLxejlS86qhn55w+eh2sn4OPSMOEzM0yzpv4DxlcUzpp10ZZpbTaHP+35f7+A1xUP+zs4deyVLUtLVj0 +KSw8WLO snCsQ4xnEBoNKcqk34WagnD/bAzj9W4xPRw6fL5zh8ltGMlDK2iHYymreVA== 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 KVM_HC_MAP_GPA_RANGE hypercall is used by the SEV guest to notify a change in the page encryption status to the hypervisor. The hypercall exits to userspace with KVM_EXIT_HYPERCALL exit code, currently this is used for explicit memory conversion between shared/private for memfd based private memory. Signed-off-by: Nikunj A Dadhania Signed-off-by: Michael Roth --- arch/x86/kvm/x86.c | 8 ++++++++ virt/kvm/kvm_main.c | 1 + 2 files changed, 9 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index bb6adb216054..732f9cbbadb5 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -9649,6 +9649,7 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) break; case KVM_HC_MAP_GPA_RANGE: { u64 gpa = a0, npages = a1, attrs = a2; + struct kvm_memory_slot *slot; ret = -KVM_ENOSYS; if (!(vcpu->kvm->arch.hypercall_exit_enabled & (1 << KVM_HC_MAP_GPA_RANGE))) @@ -9660,6 +9661,13 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) break; } + slot = kvm_vcpu_gfn_to_memslot(vcpu, gpa_to_gfn(gpa)); + if (!vcpu->kvm->arch.upm_mode || + !kvm_slot_can_be_private(slot)) { + ret = 0; + break; + } + vcpu->run->exit_reason = KVM_EXIT_HYPERCALL; vcpu->run->hypercall.nr = KVM_HC_MAP_GPA_RANGE; vcpu->run->hypercall.args[0] = gpa; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index d2daa049e94a..73bf0bdedb59 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2646,6 +2646,7 @@ struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn return NULL; } +EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_memslot); bool kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn) { From patchwork Wed Dec 14 19:40:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073523 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 0AB6BC4332F for ; Wed, 14 Dec 2022 20:09:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9DCA08E0021; Wed, 14 Dec 2022 15:09:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9B37A8E0002; Wed, 14 Dec 2022 15:09:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 854588E0021; Wed, 14 Dec 2022 15:09:20 -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 760798E0002 for ; Wed, 14 Dec 2022 15:09:20 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 4B08DC0F9D for ; Wed, 14 Dec 2022 20:09:20 +0000 (UTC) X-FDA: 80242001280.30.A4FCCA2 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2087.outbound.protection.outlook.com [40.107.244.87]) by imf12.hostedemail.com (Postfix) with ESMTP id 78A5A40014 for ; Wed, 14 Dec 2022 20:09:17 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=e2JrRsKd; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf12.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.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=1671048557; 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=UoTE+fiW10Y+o7IhpGUW/8psStYG4PWgFyeDWrTWepE=; b=RTkC/+QxRyXLss0//ToaDQZIl3yD95szGOYeouEAXScD9u4IZvn3mnd+hee+GxIwiIbSOa 19w9gVpub6FVphTdjW96LT7/YfnV0yQP9rWuf6x9vf5edCsYnMaufjTLE8h3TU/DIJbZ9+ n9vJ4bSkp04cjb3iITgTGWlVmrSMWMU= ARC-Authentication-Results: i=2; imf12.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=e2JrRsKd; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf12.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.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=1671048557; a=rsa-sha256; cv=pass; b=1GYmirUwDhioQ8O38oYU+GK6480yHveLLPJ9AC3Le2GvZRywoK7FrPI0RzwUT3af+8dfow 62jJaFpHVdXOVeTCJ4/3+wzLD+O44y9ka/WXMzM7tRzWYSKxkUZ50JCR25mckWtgEsAm3b gvmksuhKJ73zU4lu6WPyYLVpcd1FckE= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=k3Rel/P9Z7OJE5kuAliew2w7FhMk+r8EKcRnU8EL43mhB33HiGwL4d5x5QQlEAnyPM/Sbjim7h427JWmha45fsFJzRAjCAWLrbrMWRS3nc2jCZOzlTueNoWKe2C5tgO4i80z+pKpTysClweyFgwnluf6dsf+KITDF/zMGiL1Sw5cMM6+LKKQaVhk3mucDloQT6hcsCTaWiW+7q21tMKZj/VFPoiHUHVEuMS48RJAkIoC3hpO/decTb8lxsjn6jzugMXpkn4OAJ4z21gaUmMdV9fx8OG6twQ4UpyaImGMJ8IT7txvRCAf9mxRu7T+P5TbQQjXuvcQ91iqxYcKZgHV1w== 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=UoTE+fiW10Y+o7IhpGUW/8psStYG4PWgFyeDWrTWepE=; b=EjXpi+4MgfZPBrGGgDPg6xKlV16lmK71ax7vF6jPh4K+/YMCPgN2KDeOEedEfmUoz35ZH/b5kDLa5lu5D9QhAs+s9yntugnusN7X8tPeJ63CXE1TCzvJI3u56eLsQ13F+PdEG1VjrYfIl5N9uONT9qqPj/QnFh0XYG5Vphxnp3hClswLAts19KMRV85A/k70HAa9UBL0gIZi5NeHVlJO1CL14eTB+C7B3VPCray3x9XUjnT7xovZKJh/BMxjNVBHaRAhsK6orR13Np8Xb8IO7iQMk7Gd1YCsF2TovZ9urTup3vAaUcqoissfifh6xsvI3WlHSvSIZ/1CCqlXJh6ZaQ== 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=UoTE+fiW10Y+o7IhpGUW/8psStYG4PWgFyeDWrTWepE=; b=e2JrRsKdrZ+X4ETw96wMApa/EFuSQ6W9rmfP3g0vSfbTIZ3mdYj1gwIN3I11d75Zg6WCS3GVIdo8wtk8Z8NXlHKC99CsMHEEv3LwM297kYt64AMqXQpJfD3WHMY4IIv/xaUq12MF/D0AOi2nm+2oclgr+uRNsv4XwKzzcGF3UPU= Received: from MW4PR03CA0303.namprd03.prod.outlook.com (2603:10b6:303:dd::8) by PH7PR12MB7236.namprd12.prod.outlook.com (2603:10b6:510:207::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 20:09:13 +0000 Received: from CO1NAM11FT061.eop-nam11.prod.protection.outlook.com (2603:10b6:303:dd:cafe::67) by MW4PR03CA0303.outlook.office365.com (2603:10b6:303:dd::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:09: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 CO1NAM11FT061.mail.protection.outlook.com (10.13.175.200) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:09:13 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Wed, 14 Dec 2022 14:09:08 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Nikunj A Dadhania , "Maciej S . Szmigiero" Subject: [PATCH RFC v7 08/64] KVM: Move kvm_for_each_memslot_in_hva_range() to be used in SVM Date: Wed, 14 Dec 2022 13:40:00 -0600 Message-ID: <20221214194056.161492-9-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT061:EE_|PH7PR12MB7236:EE_ X-MS-Office365-Filtering-Correlation-Id: 1e62d8c1-447c-4880-10a6-08dade0f11da X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Lg3YP3RpLL5TvffTChCRCg1QLEheUULM+s3Z6rqU1KnzhHb4mXNifSg74d6JT94BoJTVWQAQrmA3GaxcKkIq+w5xfZW0Hhe+obxXUYIUtzvBvymat+biqLpAeb/t4huem+uUfkhx1Fn9t1y2lj67Pr7OrUt8IctrWQhzq/INworfkf7EGmDH5WHo/IqpRo0fKYbzlRYo6Bw0jQbPK0FY5j8RbcyPDUDYA3jpSwgKFliq2aOzprMfX2Z5BAyw3qfaeoX5HK5qnPAkclGD1ZqPYuGaea6ECqtZrl47rlyN4D++E4BXQOjVNjojYilLGHwpMYPiN8sbU9S4D1abPr8KH08r3hl9lSAu3UJ26wvox5rEpEFLsaNwYuESHSHM3hXx6mFfalb001VdGqB8VbceKtHYsgTWTCk/hzXsqYdjDpl+nIuOnLvPRJ1f3je9GD13sXUU6Uzj1hUzJD3Gv1azLKVTdztGlg7Kk7NCdgC20/rCtEMZZ7aNXIarWx/zglNiI7FGzif6JncStwCiOJYBhlEG3X/FcXx2XkzIBjRIPPj0uTvKGV9Jhp8Gt0VEieWFJeuu92BDRfInjt8qSizFJxgEC5NU0asRbPQjq+821ADS68lxM/+Lp638+uauEZazAsNMeB2qktvpGNLslfrgfO+aQ04QYPw63W1IiAw5gk6RpkdXW2hCe22WoDbS1pipaoBdn8FzldenPm0qmYR7GcQ9K0CSkalDshWQr+SWGAU= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(39860400002)(376002)(346002)(136003)(396003)(451199015)(36840700001)(46966006)(40470700004)(70586007)(6916009)(70206006)(82740400003)(8676002)(316002)(4326008)(5660300002)(44832011)(356005)(81166007)(7416002)(54906003)(7406005)(8936002)(40480700001)(186003)(1076003)(83380400001)(82310400005)(16526019)(2616005)(36756003)(41300700001)(36860700001)(2906002)(40460700003)(47076005)(426003)(86362001)(336012)(26005)(6666004)(478600001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:09:13.0319 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1e62d8c1-447c-4880-10a6-08dade0f11da 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: CO1NAM11FT061.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7236 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 78A5A40014 X-Stat-Signature: pyd184fiwrw5br9j46uxxqh1ug7o1fst X-HE-Tag: 1671048557-66301 X-HE-Meta: U2FsdGVkX19TPpS4Z+Vha5H90vTj4gm8BwxlWPPHdTMVXPq/VceiaFxS4wgLbg1qDDyiHgiU3OzhAW6p2fQtupw/T8llY8IZFlNSs1Mz920E644wgEX3JrbvCVnYP4RqdPm38mMDlA8tSZFYWHsv18HwRZtagug5KsDIRYcLc5Vyhd+CEno+IDDHRaBVRFV4PNr2TC+IFoA1vnrB9+nn28aNVaA4D/4zG4SxxII4uX1Zq5zVCZ7/F0/enU75X4vXW2AOJVF5Opsjami3JUjUK0rEgxnBxFfFUYlJ3VH6lIHYMNBmgKWYN/zR+9Gpuum676FCmjafPBwe0WbTqePrzpyxYLoHa7Yhtxzms7N+F82eWiZWkmdB+zwdXGPbKipqwOnbCmglrqnNfDRxnpZ8iSxOqLv7KxLrdFBHDrZE1taa+tv8nad+o2rwERJstkJapASuK0z/4/9Oe+UKw5NJVpDpm+1kZ90iFEF9Tw1EsjCh1LZFcY2Cg6ndtqKC/l4YuTJROwF2FJnedE5tRCmBMRewbrxmE2/t5p5y+hjSc3QQMw4+STV+e5fVgq7YYORCo0XpxEqr7Jo/73/ObOFBj9uGJYuL4w3jJlMv7KBuYMMyNDG2dssl8+h8FXJI/CPSyCwoldbbA12gsynqBl4/AzZSL2OyDNGxs07WL0yGjwQQHWQvaFQ73rKswpZ3CnT0b/+vehkRUQXtTahXHMRD+86AXvLbLi5o/45DIkW6UzbvnXn0n7UX6sVtOO5pJOhYo4q6CQbcPqEumL+IzpHm0qhP48fJP8bbYX+ZA7o41HXeq8gAFkMFD2chteD+G2c7wb+jABVJ09nbbMSfMcpKFcbKzAvj0O23QiI8IIXcJz29DIBvSKCJDfk0n09Hbq+BLV6TqazDTVXst47Ikj07QpxeJrAyfDlzC5X73MDUFNk8rRfHSKMF/4Ov1ZLm7clVYJI3d1erG6mkKwnAhb8 YT8K/xtP V6BcTQdLsvI4754JzX4iUA/2zmRj6NhJOPg5O3ayFoqj/gR6CCvV4EwznLA== 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 Move the macro to kvm_host.h and make if visible for SVM to use. No functional change intended. Suggested-by: Maciej S. Szmigiero Signed-off-by: Nikunj A Dadhania Signed-off-by: Michael Roth --- include/linux/kvm_host.h | 6 ++++++ virt/kvm/kvm_main.c | 6 ------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index f72a2e0b8699..43b5c5aa8e80 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1116,6 +1116,12 @@ static inline bool kvm_memslot_iter_is_valid(struct kvm_memslot_iter *iter, gfn_ kvm_memslot_iter_is_valid(iter, end); \ kvm_memslot_iter_next(iter)) +/* Iterate over each memslot intersecting [start, last] (inclusive) range */ +#define kvm_for_each_memslot_in_hva_range(node, slots, start, last) \ + for (node = interval_tree_iter_first(&slots->hva_tree, start, last); \ + node; \ + node = interval_tree_iter_next(node, start, last)) + /* * KVM_SET_USER_MEMORY_REGION ioctl allows the following operations: * - create a new memory slot diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 73bf0bdedb59..a2306ccf9ab1 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -623,12 +623,6 @@ static void kvm_null_fn(void) } #define IS_KVM_NULL_FN(fn) ((fn) == (void *)kvm_null_fn) -/* Iterate over each memslot intersecting [start, last] (inclusive) range */ -#define kvm_for_each_memslot_in_hva_range(node, slots, start, last) \ - for (node = interval_tree_iter_first(&slots->hva_tree, start, last); \ - node; \ - node = interval_tree_iter_next(node, start, last)) \ - static __always_inline int __kvm_handle_hva_range(struct kvm *kvm, const struct kvm_hva_range *range) { From patchwork Wed Dec 14 19:40:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073366 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 27B97C4332F for ; Wed, 14 Dec 2022 19:46:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 692958E0003; Wed, 14 Dec 2022 14:46:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 61B6E8E0002; Wed, 14 Dec 2022 14:46:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 46EA58E0003; Wed, 14 Dec 2022 14:46:51 -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 30AD58E0002 for ; Wed, 14 Dec 2022 14:46:51 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id F0ECAC06CF for ; Wed, 14 Dec 2022 19:46:50 +0000 (UTC) X-FDA: 80241944580.18.C3ABB42 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2041.outbound.protection.outlook.com [40.107.237.41]) by imf19.hostedemail.com (Postfix) with ESMTP id 2CF811A0018 for ; Wed, 14 Dec 2022 19:46:46 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=OeMfWvF9; spf=pass (imf19.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.41 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1671047207; 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=KrawmOQcUfOvXjQ5967mZ3Vgq8NBVT7yFWCyAF6k3c8=; b=jiU+KpqpvTrtWWqMzUTLJ8S42GWBJnSgSD29JP86i9S9PH3/Gg98bIcDj9/iXl8jQrkrGQ 34MOjHwiqrWSe0S5C4HZ7VQ8TgQdTbV2e731gHXhr1pHCsfc6IXuBGZWv6WtUEespOwVwS 2a+CuFCUsZg/GMjMB3v+cnxI6q9IIbg= ARC-Authentication-Results: i=2; imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=OeMfWvF9; spf=pass (imf19.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.41 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1671047207; a=rsa-sha256; cv=pass; b=ms1Q1fbiZmh3XL1QnKTi/miFexTjfdh+lZpx7/9ZS4//QibaM19CjmX/DlCZRClzl9W+kb ByNVQQr/Fe1C7B4JjJmQPHPRwe8HWaBhKMzpCrW+V1OujmmKu2NgyQkJ3nTUqx4JPLb0s7 G2kGCp90sWKkhdt/VMmqcZRG4klKTGc= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=It1ZnRUqJiqFmjSYP23twjCuXhLlF43t3nd7Tyn5LKgIBVmqnKKZstjEhXtTokR0Nlr3Q8CuaIJY0vBNkToiY3iF1HUFqHw6Iuy7MInPubCUhF4prJIEe2YfHTKlu5BLZ8fe6+8Rvzju1ZQuZ5Lv3/sweSPgAUHrgQ6IPSbUbar3BtPemytuX3Kx34ScbuafD/z8vwdsruvMQ9p2A4oQgG0zNOBnO05ISXKQDL1lPXdGeT+k/AkP55xBq5HE3t0qhZXMZtqNdBYqfq9f64hS4BjJeH9uEhh4ZmRGgPteuJbnocWFlDSaEyAXg7nStfvg/3bGHIYL8EZCMJSjOXUACw== 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=KrawmOQcUfOvXjQ5967mZ3Vgq8NBVT7yFWCyAF6k3c8=; b=lIHSnzOUtMmIC0D7RLxzsXUwti5A329DVoMaazYwH2rvItFIre/51GRz5PMDxAePdfi+Q42SYG9XcIuae0EwjiVkCTvr/1594G+EGFME/E/TfxW3HQnnlGvZvVLU2yPcSyRKCcfICquokN9r/qIUcpkUinxjnfT4RwWuyjUqfR+qLyVVWsv6IdgWk2hMinmafnUXU1Hcm9xBncnxCcO5wwQ3Dh7Wmu/BBlI3ioM1LZqYbrIWYJfvtHFJgtwDgtEPWugUBU3eufus90Xqn5oU8ORVisR3hUe5cQgU13PARmH4udX79F+Qlk62VNIHoFUdUyjr4vfrzxfzYthSDQwlOg== 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=KrawmOQcUfOvXjQ5967mZ3Vgq8NBVT7yFWCyAF6k3c8=; b=OeMfWvF9ubeqKey0wnQBXOt5M9XduoYkOw90bAnMW3wYvEE4y0OJWgRPPEL7QYvgHPqhx83CYMu52mJ90r1Ayyif7JAj9823ALMZLPZHmeTkko74xsY2LtoZi5CMm/izPsFYNO5D3Yx58yuQI2d7zfoVSzlmz8VT5lcBQUE4NJ0= Received: from BL1PR13CA0098.namprd13.prod.outlook.com (2603:10b6:208:2b9::13) by BL1PR12MB5302.namprd12.prod.outlook.com (2603:10b6:208:31d::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:46:44 +0000 Received: from BL02EPF0000EE3D.namprd05.prod.outlook.com (2603:10b6:208:2b9:cafe::de) by BL1PR13CA0098.outlook.office365.com (2603:10b6:208:2b9::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:46: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 BL02EPF0000EE3D.mail.protection.outlook.com (10.167.241.134) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5880.8 via Frontend Transport; Wed, 14 Dec 2022 19:46: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; Wed, 14 Dec 2022 13:46:43 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Vishal Annapurve Subject: [PATCH RFC v7 09/64] KVM: Add HVA range operator Date: Wed, 14 Dec 2022 13:40:01 -0600 Message-ID: <20221214194056.161492-10-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: BL02EPF0000EE3D:EE_|BL1PR12MB5302:EE_ X-MS-Office365-Filtering-Correlation-Id: 38003544-7ec6-48ef-62ea-08dade0bee01 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: NackQYfnOA84fpGzzOnRkBVpOip+ZZl5oXHHpNEk6hqquHntX/F5SMSmLPTFihkCegh1Lpg1WKCPMOcDwbSJ5ah1c9hKJwnUWGvmdgWxsnWX9voGera+h0dMsyvRClpCgOL1T7fDLguEjxfAGDK4lmYlbnZhOL8yqZVxEQJTs3r2CE+z0FAdap6j4Abn4x9KhTgOVe1SWCb5z69JBAUmt7yfBYTusyJvvh7JKlksIA+a5ERom6LFak0cInj048e6eLaY7UCedCKq3NFdDEThOrizEL/1CvZyyksjX5Hv1LLMKKmJCRGGEp52XVkoB+yG+4tv3GYjAU3eo9Yy/XUGx029NRKdmMglt2shsWv3iu3Or/YOwavTw4s1JluqttfmlP+6zAW1j5N3ljQbEebc6u0SQQMRt8jvYvhtGrZnlkuFV9lrWDxpDdeX1PiqJIlc1eXJBInUivnpd9wfoLWOULnseljBeL5k1zml3Pe7welAODqcdWsAAA41/E6Srtd6u6cua3hQNmDGgR+8InQhb1MyckrSlQgHfCbjx+7qoHtjRwjvaS55JA3AYTkl9N2agps+OM0R7yhHHK9Jac6SW5z7WuTIuex9YepkXiaRSfU6mezPab3Ji9Wo7ut0S64MaQn2t+205SxOkbpZYz2Dixa981Ydh7Lv3RThGPYdKScyJU6lpFiMVR/l0sk6aj/zdVduYR+0yv2H3cRjwqvltbneawFwbOpZsPWf7SPRGIE= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(376002)(346002)(396003)(136003)(39860400002)(451199015)(36840700001)(46966006)(40470700004)(82740400003)(81166007)(44832011)(83380400001)(5660300002)(478600001)(36756003)(186003)(2616005)(47076005)(356005)(40460700003)(8936002)(70206006)(8676002)(1076003)(41300700001)(82310400005)(7416002)(16526019)(4326008)(86362001)(70586007)(7406005)(336012)(26005)(40480700001)(36860700001)(316002)(6916009)(54906003)(2906002)(426003)(6666004)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:46:44.4483 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 38003544-7ec6-48ef-62ea-08dade0bee01 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: BL02EPF0000EE3D.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5302 X-Rspamd-Queue-Id: 2CF811A0018 X-Stat-Signature: ws8im1k5uxha3t3tfwfpgiyks3wbqxxr X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1671047206-371112 X-HE-Meta: U2FsdGVkX1+UtrI1PTrxLTsBJIkxNwidBGwrDO4rp/dk/bX2ULQ9jnNFY4GyU424X1gndr49U7YeWERgblcZGaXlKYjpkk5toQMIibYYNHDz8BmhKqo8NTvQ1IZpmn1nIW1M0N5S4Ya4K+hI3uIoeB3uPN7jliOF0Hn+hus9gEaCV0/M84bXPaSRfrY0Stk38gxjCI5CSApNL23kcJbToAU+wXprxce8JZ7XTTuk3d2gHRJAe4ZEuckjpM5LZYxpdaZeL6u/lxTpwRwpj9avlIY86TfVAfyQr3VemVVg3JmBx55ZFjY1upPDWk33FD+jZjq1qHnCO5fY0umrBFD+ss7q6YqFdsy+Tff7ILQLirWzSRl85TGRy1p5c9WCfZlQs700vcJf5iz/uiaIhrN4T/Z5rPf43uMX29BsskWra2ezX+TPCJRrqIJnQNTj37e+JpzxZcKvTwMxNdG0Yz3m3w0vTkApr2GcICh8ACUQP7yv88Kgd/xStY7OyiRv0ccyodg/AegSbzfKYbUn6CkDs1blWxgenNtN4pd9T3KM7K4GSQJQER1VsFf3x1bLabDlBJ01UO4UlBoySwGxEFf8f7xGRp/PBM//jJ5Gk8LVfkH3ojvpzAmQtTRVL4whzxv34vcPbnZZUn3sPy41rI/dGzRx3aC1UE4VR0vL5a3hefrewxcMQSE8eL01dpP8Nz77EJhli0u2TU5AyUT5AikFTWQWCInOG6y5ZuZ3S5wyXrsYeDBwZUfoftkUpQ+hseZvSAzB4In6AkCVpehA7hQOYpYG64wgZvDMIHtE0zwk56axq3Y9rHp9IsneiPsgWVm4y0CH9Z1oj5POJkh1k9ChKCP1Sgr9+OcyyH+kI+KEx9j71v/cMm/F9mV9Ydt9Y8X63Xw6I+pnxCySz/uIjFsq9Xntw+aSlUIywFLCtQFL+Dv7ckWsb8HpIrxFFYxeA7aCwAoDC6gVmkghDiHHd+z 3BCr6Wnv A9PEQ 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 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 43b5c5aa8e80..21a539ab17f6 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1407,6 +1407,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 a2306ccf9ab1..4ff7adaf6c56 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -700,6 +700,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_ADDRESS_SPACE_NUM; 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 Wed Dec 14 19:40:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073367 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 B24EDC4332F for ; Wed, 14 Dec 2022 19:47:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DBDC68E0003; Wed, 14 Dec 2022 14:47:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D46538E0002; Wed, 14 Dec 2022 14:47:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B73C98E0003; Wed, 14 Dec 2022 14:47:11 -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 9D86E8E0002 for ; Wed, 14 Dec 2022 14:47:11 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 57D1114038A for ; Wed, 14 Dec 2022 19:47:11 +0000 (UTC) X-FDA: 80241945462.05.CBD5538 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2082.outbound.protection.outlook.com [40.107.102.82]) by imf24.hostedemail.com (Postfix) with ESMTP id 8801718000D for ; Wed, 14 Dec 2022 19:47:08 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=aD4iT9z+; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf24.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.102.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=1671047228; 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=9w/cFCjO4bsQFA/Rhr3wtXGAVyDniVXFXOz9FKivmoQ=; b=NTIG8Q8QYJL16XJCu/8HayRrqC4i+Z9ohbQXv5vMcrBXfjYROk9xgZ+bQ2TENrq+eSK9xE sdJrhnYJDuw08lz0xUKkHL9I5vgujaCTT1sXRk5YFs7e9FW/vQKFFWLabTHiZ5lXuKGelo zyqw2B4Onc4s29vPl6iGw3xq00jjgDc= ARC-Authentication-Results: i=2; imf24.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=aD4iT9z+; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf24.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.102.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=1671047228; a=rsa-sha256; cv=pass; b=LvWgQvYQ7zPpZj/Lu3IL380aXlHdKr2aOUu5bmIuIlQ5QVK/jq7VPYeyD2xnROBbPQHWnp uo8DLGcmi7oajuoNMwcdk0zT22TYDWj74aqt99sdoR6Hlkk+ggj14R3V/7H/jUpRF6F5vh KcCfVvBrNmi1l9UVhFnVIZy51awZ26w= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QFXp2ura0SniPy/1FT0hTaveLU8lPt79DfuLbx/ZmlS7LuyHVEOwY8yhtngXdPQ1B4/GEWScZHs/3PhFwoPIHm/Y8u65e0w9x+je/iitBf/ceHPhO8ymIUkwkgy8uwGCh81lCr4uydo2DjPc8KwjYcUt8+5gZvkllD5pr0gYeGTQY1CqJ3uHKa7rfIIJ1hw8jd2mcHZB6GFM9lwWF7Xc2ad/VRZJidrDzwIVjMr6Q6HJCZV2JXvM8uNIgUn8bZmTCRnfKv1iE5+dBdf9nIaohQVIWiss+HUKx2j1moGd0AnGoLurc38tj6U2uQPw0JahHtA35/PEwsOOjxaET/QYrg== 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=9w/cFCjO4bsQFA/Rhr3wtXGAVyDniVXFXOz9FKivmoQ=; b=ZlYWEJA6QNYL7w4maLau6oQ7gSjpSG65dbnIlAUAFMwTtOrZPBOcQF9ewOLWJgmk6kp4tlX1kEsFYTBpvN1z1bx7p249amlEmWglYDBTDmbHk8SR/ODn0jmMQaa6TnfXCaywkiofx38qZ3Kuh7eRDv3aKEPTonS2QwOnVmM1r4gVE0eeonG68h3hS8h7f404nRNApFN6fpWPowfRpjEweexK13LPsBcCz1nT1c3Oym2PUmyOk6sVkAbuAgB2VTiPpUVQw4mnDlKLDp0tlg5TzR7rXfMdIidvnMq7SPGKNTf9IoofO2AlK85gj2MTEty3r+TDK6ubTfXGVP13Qz5ZnQ== 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=9w/cFCjO4bsQFA/Rhr3wtXGAVyDniVXFXOz9FKivmoQ=; b=aD4iT9z+C3JGhKVwwsdRrKuYEi9Ba2c/ktqic1JLQPOkOG2fYEP90wktUMh3WszpESkBYMtDGATTPaD8piuB+7qMtCGl+1FouuJTQCvDa997XiEv3Kg5D+RPRcCBeHzQw+hbV3MQ6V52yPZO+e2kfXEBfvIyah3Q0j3Y2KH6ahU= Received: from BN9PR03CA0274.namprd03.prod.outlook.com (2603:10b6:408:f5::9) by IA1PR12MB8192.namprd12.prod.outlook.com (2603:10b6:208:3f9::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:47:05 +0000 Received: from BL02EPF0000EE3C.namprd05.prod.outlook.com (2603:10b6:408:f5:cafe::48) by BN9PR03CA0274.outlook.office365.com (2603:10b6:408:f5::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:47: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 BL02EPF0000EE3C.mail.protection.outlook.com (10.167.241.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5880.8 via Frontend Transport; Wed, 14 Dec 2022 19:47: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; Wed, 14 Dec 2022 13:47:04 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Vishal Annapurve Subject: [PATCH RFC v7 10/64] KVM: SEV: Populate private memory fd during LAUNCH_UPDATE_DATA Date: Wed, 14 Dec 2022 13:40:02 -0600 Message-ID: <20221214194056.161492-11-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: BL02EPF0000EE3C:EE_|IA1PR12MB8192:EE_ X-MS-Office365-Filtering-Correlation-Id: 2431e434-eaba-4956-3d4a-08dade0bfa6f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: zvh6NY72+zxvwfjghmxvaPw+2+acsgsBjjCDutF210E0NpjY8xiP2OgNdGpOg9o1mmNa+4q99k3X7iVDTFy7kvjvf36lCc9X9Dc9QFnxl5ncX7UjGc+BPGeO+Ics7ODFTVa/pvWQB426K4gWsaqei7UncSHN4izOurmmiZ+FvJXkUVhIVWD/nSKFHkGYT20slSLb/wI/Qj9kOlEN/3xrv+bRkqSbuUf2r65Mlehg8v6Qh7Jkmw2Tq5mET2sk01SEtVHcZhacYJrMcLxT1nlXMUqu5RUA/8a82vUiku769M56Ijldo4ljN++WC7A+YZ6c/L47Z2itdMf0K93Sh1w5JUqETYbIG3drq20E11MptHRrh8E7dKjk5BgDHKw0jZdUb+WOhjCqVg0+3vVLoX6du+90j9RDbF2CQHiU2cJX5vGW6sCne9dnecQ0J5RpsbOZid5X2N2OXZAXDTQVQCVO4wC8BRpse9DVDskVYVyuAnGOP6adFrdD0dhxEXxLMQ6/Y491Jurb+xITApKEnUZ0gA8uQomCzmeqNYlIGnwPATB4mPnpuA+V6PQ3HwDvLMZy45PamMNFv8Q4lwJ9J0feehtfptNpIB8KRHRsEgw++fbO42Fc0LQlfc1nfZ+iG8tDmX3fC5MqN5dTHrf1i+ionRVlV6H6ptkRiG2dWBUEHqcaxOw1LzCgp7+DuAKRc4W4C2C8aoPikM/p5Jw2q9aqmj/s75Lx0gD3u2N9jRzrZRc= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(136003)(346002)(376002)(39860400002)(396003)(451199015)(40470700004)(46966006)(36840700001)(36756003)(6916009)(82740400003)(356005)(7416002)(70586007)(70206006)(2906002)(54906003)(316002)(7406005)(81166007)(86362001)(36860700001)(40480700001)(8676002)(83380400001)(66899015)(26005)(47076005)(4326008)(2616005)(40460700003)(5660300002)(8936002)(336012)(44832011)(1076003)(82310400005)(478600001)(426003)(186003)(16526019)(41300700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:47:05.3009 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2431e434-eaba-4956-3d4a-08dade0bfa6f 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: BL02EPF0000EE3C.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB8192 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 8801718000D X-Rspam-User: X-Stat-Signature: 5uhdp5qxostn4ahwgon9xourug3cjht3 X-HE-Tag: 1671047228-186669 X-HE-Meta: U2FsdGVkX19jkO3l9li1Wbjtvk1peg1/F9UMz/yYdwVAYeD6x2tbX//43TS16ZLQYr8AhTHjEo2OxJJPso3PNTHC4JUsStWxk6NyaITC8in0L1r5nZZosvYuEb/WbTIg3XQYMx8i6ksv1c8W0Wy8dRNulyGQlp51qwdn6OGB4UMw1y8kLnm1LFPhKFcnmlifv5BYdDwO4hzQVegGoXk2dzpSjp5Ur8wUEzJOyv9wkBb3W4CMEQc6xvmuG7Nk5w+4cU1eg7ISWRvOrwAe6/3UY0laPG1jn2aL6WsPTIf3QFxa4U6dfSBlW6Uzs41AXg2P1Kcmjy5wbHINaq/mxXsm6NLywdXr/2jpGRlnNwlt0hC9K6mQjEwR+JDWjwY6lm4s/9rTOOnSJBpguf9bSrXBeq+9EZgFwTQStWG8Oe/br9mHitCXnqJpoGVdSTGm5Slb20ljpsrWMtwlQaeU9FBnAf/3GsR4C7/LnomECQf546/Z5cnp7YLDtgOmO1vH9q8lK3e65XYtcLm3tjGqXzdmd3wa1MNZPs4OxqlsjRJ0ERmnr1lOf0uM6hnsXX8IvxLghEBjbqr+ZdQlBFd79unEjK2bqFMmIogvDf6Oxfl81imx9E9GrHUVWpWuSdCSFHV845kEffNxDSv0ZVmXiJJGvqUOhgDgp2vj3hE+XmA5EIgJWbQRIQl8zhUQYmbrwezxjlujXoHNb0/SO/FUW9EY8cr2VSmEi1M66TG9PQCUDRVjvJIjDMMP6C4VcSWIX+7rqK7vVulsLE27DAc+oTrn9BYQ4kLxC1w3NmJLFcPaogzmL9XTa+fwFeqW9hSmErxub5nXQlww+zHKJ0sN5O+j48J2lA4kmUaq8fHOTLVkeen+/n9dLZFBsVzSUNfSXd6A0gDvglYJJYqvWeF6OYuhC3R0pyeGYx2Ll38fIqoAhdZe64Zp8w/zMPBxFQzUozDJHg4Ofvs8rT9PuqgF1fM Z1WF8LoL MtY7waNjoYs/FubmtDZp/nLn7VxMw6a4XU8j0L3twIc+4v0FoQdCkqnaSUQ== 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] Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 99 ++++++++++++++++++++++++++++++++++++---- include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 27 ++++++++--- 3 files changed, 111 insertions(+), 16 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 69dbf17f0d6a..a7e4e3005786 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -493,23 +493,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. */ @@ -561,6 +564,84 @@ 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_restricted_mem_get_pfn(memslot, gfn, &pfn, &order); + if (ret) + return ret; + + kvaddr = pfn_to_kaddr(pfn); + if (!virt_addr_valid(kvaddr)) { + pr_err("Invalid kvaddr 0x%llx\n", (uint64_t)kvaddr); + ret = -EINVAL; + goto e_ret; + } + + ret = kvm_read_guest_page(kvm, gfn, kvaddr, 0, PAGE_SIZE); + if (ret) { + pr_err("guest read failed 0x%x\n", ret); + goto e_ret; + } + + if (!this_cpu_has(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); + } + kvm_vm_set_region_attr(kvm, range->start, range->end, + true /* priv_attr */); + +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/include/linux/kvm_host.h b/include/linux/kvm_host.h index 21a539ab17f6..33fa0b1435d3 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -973,6 +973,7 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, 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 4ff7adaf6c56..1343070657d1 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -747,6 +747,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, @@ -2595,12 +2596,28 @@ static void kvm_unmap_mem_range(struct kvm *kvm, gfn_t start, gfn_t end, kvm_flush_remote_tlbs(kvm); } +int kvm_vm_set_region_attr(struct kvm *kvm, gfn_t start, gfn_t end, + u64 attributes) +{ + gfn_t index; + void *entry; + + entry = attributes ? xa_mk_value(attributes) : NULL; + + for (index = start; index < end; index++) + if (xa_err(xa_store(&kvm->mem_attr_array, index, entry, + GFP_KERNEL_ACCOUNT))) + break; + + return index; +} +EXPORT_SYMBOL_GPL(kvm_vm_set_region_attr); + static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, struct kvm_memory_attributes *attrs) { gfn_t start, end; unsigned long i; - void *entry; int idx; u64 supported_attrs = kvm_supported_mem_attributes(kvm); @@ -2617,8 +2634,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; - if (kvm_arch_has_private_mem(kvm)) { KVM_MMU_LOCK(kvm); kvm_mmu_invalidate_begin(kvm); @@ -2627,10 +2642,7 @@ static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, } mutex_lock(&kvm->lock); - 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); mutex_unlock(&kvm->lock); if (kvm_arch_has_private_mem(kvm)) { @@ -2793,6 +2805,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 Wed Dec 14 19:40:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073369 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 6EA80C10F1E for ; Wed, 14 Dec 2022 19:47:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CABB78E0005; Wed, 14 Dec 2022 14:47:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C35408E0002; Wed, 14 Dec 2022 14:47:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AAE878E0005; Wed, 14 Dec 2022 14:47:53 -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 95FEB8E0002 for ; Wed, 14 Dec 2022 14:47:53 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 5DE15140381 for ; Wed, 14 Dec 2022 19:47:53 +0000 (UTC) X-FDA: 80241947226.12.968EEE4 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by imf23.hostedemail.com (Postfix) with ESMTP id 70FC1140019 for ; Wed, 14 Dec 2022 19:47:50 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=koQk3jhM; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf23.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=1671047270; 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=JUgAP9c6+ee2LMTX4sIVCW+FM9ZjLBQ77Cw5MM6nbMc=; b=c+zC+LYaSsBuLWtLIQluuDekDjs2nx3PwKRX2yi/Aj0G+DlRNPmtYaaM/aWsrbiE3mPVRj NGfJn7IsYWWrsBHUZHVV0MwYK39/2JC8CXTFupvZsReDnkS2/kJKL2bOVGNEoKaKUTbfrh P/eHab7UB8K7BYz2qexNXaOnQrmuPSg= ARC-Authentication-Results: i=2; imf23.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=koQk3jhM; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf23.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=1671047270; a=rsa-sha256; cv=pass; b=i7d5gkF4Xz3Y3fzqdNnjCjuPAGT0RG2unw9egAa/vqXrkkDoSN9pN3W8akB6x3VFAqtAsi rbSarz2mv4ASG+VA61INYHU3idRATf6ojIKb/XYShcFUHA/0vOP4btyoN5Wso2N0Jv9Wag nagTghxvrvJlFTwK3lgQRqzgHj8hJpU= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=T7YSf7N09G1GFr+wEm8XDMZSXq1SJmIOlIzoDXh2iBV3hZp3JzuaIcy2A+NrvOo8ve53iTxqHMAlAerG+H0oPEytv0LaxNBq2SUCJX9yIGZDxt6Frv9u+JaicWCXmx2gRvurWQ5BsuDrt/0y1KW7etdy5qmrMCA4lr1shKZd+qGlgsZh6cOVg/BJLO+q3+9kElV2sRJGimCRq3cer8dY/mokpES/IfC8mH+HD3/5w3EMhbScTlfrNkPaVDQbWMz0N/FapYoj59IIWYW9ftGOD8Z7N1grnyxzuwnDzUu1s7v9xjU7l+I1EvJpr82rW4N5OuTYayOvXmV/dgINFp0U/A== 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=JUgAP9c6+ee2LMTX4sIVCW+FM9ZjLBQ77Cw5MM6nbMc=; b=UEFjwtdANyx+JLRGs9zA0QOrU5eLpAU2/Vg7FL43qSaQ0NYxsny79662fhdgDjpusa4YYLSJCbZwZlVvhTJtSsPGyZl8gF84CzQRW7omHQ6najr1VQIf/SAmy5Rq4Ivh7Y52pj7GXtE9cK0iWxrR7kn67LjneCv3c1kjApygb8lGGorJTfXyIY4dIcBnsW1zCJYFxVkgnEh169QKWZyvYQXNJ0Npbvtzu4nnqUIul2x1SH40CfdiBuPH42N7+C2cTc8oAK1JBBagkKDeK2vm9B5zoS51x1mq3sijPfLBbfOhsC20WbR/nF5iiyyaxTE2uYu2FB43cW3kWjwMMOuH6w== 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=JUgAP9c6+ee2LMTX4sIVCW+FM9ZjLBQ77Cw5MM6nbMc=; b=koQk3jhMsrh919UaeAilJRpNP/IwnhEOykqsru/bH1o6yFsVW1PIA01XY6anes17LzqiilP/tWOanMU/uS3e9iCvsOUZUjQhNvaNTfbia2SmkcPyqbBhRiEAQbKGm5TsOaLbA5PeunmEuZe9sP+NMebCY3cAUCC/ULxCpdgGbfI= Received: from BL1PR13CA0117.namprd13.prod.outlook.com (2603:10b6:208:2b9::32) by SJ2PR12MB7894.namprd12.prod.outlook.com (2603:10b6:a03:4c6::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:47:47 +0000 Received: from BL02EPF0000EE3D.namprd05.prod.outlook.com (2603:10b6:208:2b9:cafe::52) by BL1PR13CA0117.outlook.office365.com (2603:10b6:208:2b9::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:47: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 BL02EPF0000EE3D.mail.protection.outlook.com (10.167.241.134) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5880.8 via Frontend Transport; Wed, 14 Dec 2022 19:47: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; Wed, 14 Dec 2022 13:47:46 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Nikunj A Dadhania Subject: [PATCH RFC v7 11/64] KVM: SEV: Support private pages in LAUNCH_UPDATE_DATA Date: Wed, 14 Dec 2022 13:40:03 -0600 Message-ID: <20221214194056.161492-12-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: BL02EPF0000EE3D:EE_|SJ2PR12MB7894:EE_ X-MS-Office365-Filtering-Correlation-Id: ebbfc3ad-dfe7-455f-9697-08dade0c136f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: PLaeAO7TYsE9o2XaoIiKwk8zTmAl/zzqIh0eqH2LwPJoY++2mS/RAk8Q+Bx48m76okg5+ZS2B36u7fgp06Oqa6toOXDMODFXGLKhdNibUKSZfDJb3i4h+zT7vFV69eXeBUTQZfpDtZAAxFWS8LMJ2B8ZmTv+PQfUPNEY0WafPXmoDC7AwZMXP7Q0xMmgngHoLzo8vSOoFU3uSQvvLtBHIH+tSmJzbijkGSvBhHELxglTSwVcWR0t8gSLUhWORKnfLdSEdX92g5boVydYyCNvDIS8t4egMmC4lRZo6T+w79pL+Dm/T/31k9asqcnsa2UZrM+Oi19dJ9dLS564JeUjb3Y9k7p9+y8RRUE0nRjWtY1OYhSogAZL1u/FRxzv30j6Rhx6eQuyimjNODiRpFRIsUp/IDDoT7c3P3hW5Q5jlLdKNIVjGc985Ag9ewFTIcnFciItGrhCvrj3RWK6JlDl8FhGzZqmWvNRuzCWMkxAPlKHLyqLGDVNNoxSOMrMSkOHlwEwhpXES2j1aClhhGwB/fJn/eC9QwBGHguvL0q7J3LtlgMLC+QXOcLUU7aoChrRtmMjla9VARnpyBBb4M6Ha9hD7cYpSXWa2pKxanxbxjw7LSwjUk39AbdhSeZJqpp/a4/pxvENJTUQUOtkKdNkC+ZchpGroK7gDFAr+TA82GhdjDsyOmfcI7KxV3gxEmg6jccdvG4tZvdsTn+IPLKS45ooTAf7ZJzucDuoUwbbDNdvfhzdxUebPZfR2AngehlOe29DUfzPly9ZWenlfepTXA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(376002)(39860400002)(136003)(396003)(346002)(451199015)(40470700004)(46966006)(36840700001)(7406005)(7416002)(6916009)(54906003)(336012)(4326008)(70586007)(8676002)(5660300002)(41300700001)(6666004)(316002)(8936002)(86362001)(82310400005)(70206006)(26005)(83380400001)(426003)(47076005)(186003)(1076003)(16526019)(40460700003)(2616005)(36860700001)(478600001)(36756003)(2906002)(40480700001)(81166007)(44832011)(356005)(82740400003)(36900700001)(309714004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:47:47.2775 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ebbfc3ad-dfe7-455f-9697-08dade0c136f 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: BL02EPF0000EE3D.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB7894 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 70FC1140019 X-Stat-Signature: 49xe19kss1drka5up3i8srnbzx15z99f X-HE-Tag: 1671047270-421796 X-HE-Meta: U2FsdGVkX19iQW/fPRwa1KT09MWWuPqx/YpdRDrjhohaT7H3COMpO5wXAgTJPwdnrBBNoLhJ35em7/TuyvRmet0GLf6lLx7mWHzXiLA/rnAVFlFdIqVeTivX31uArhW6xNgevEw1q2EKAMOTvSV8UB83JyRUVSkU87NiVNZ3ePNFx1yf31f6PILKsyQQ66f3dfTJ40X+VufLm9tI6jP5u8odV3x4X4M8yelaMBux+6fq3131rncnjSTixcPW1XIaVR7ehqJDurZHyL7kv6/8+Qihz/3u2dPLLaMciu4aGmvSZalWdYzoJAFDOe4xQ2hi8ijf2MsbwNwjD8tQq0CHqbEvyKovg23OWmEyNVJPhtapfljLLoaAG+cZ5xWz06u2o/Wh5ylpk49wH7fUOOyqMCm3KcitZ2sa27bs8xmTX5WjqPU8dJg3AWGUhPf+3G7buDaFS/1CXGORO7VYRWuRqYU/HxPTNW2L7BUMzqa9X3N1YO/I4mFATaOipTYawz49m7lxqG+jCYZMCFT1+CzECXoTZqppmIOidE3aEIqfDSHyBJoctHc51ep5FGTAyvNSMgB84lTOrA7G9VJi3m1v2EQu0jYKPJKsR8cOjrwl2//w0FVBuoBgcBM4d/AQiZbQv010gSEap9rDpZ43ddoa/y8TePisHsgc+y3FSahtRc+os5p+aOOjGWNlD4YmhBS2MFgXotLN+suK4MimJ6KsFfkm3Qui2Rj1ajHNGS2MbifElI0RChmbfIQ7kraBFARGvgi4Hi1+skfs8VGUGAdEeu/xGC5FgGNVOirW+xSRFyRMwg1aPCAG2JEPRQPgWcqpmJyOuA2+RUL7MSokqieCiI+vuWRfnkv+3ITh9+CTCQYkz50RD1DYEdQdmJ8y3mfzLxJ8g4gV2i7jMT2RKrgU2hqybT+i24U8QR2/M1HTG6McGEhB1GhDHAXvyP8jEpz+kzCS64fBfin8KDUXPAz BuuiZZa9 +o7XXQyIWYk0kDzp7otLGyLpTVSoG4OeAzIbE7KNf7fc2v0OD48oiO7qr9w== 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 Pre-boot guest payload needs to be encrypted and VMM has copied it over to the private-fd. Add support to get the pfn from the memfile fd for encrypting the payload in-place. Signed-off-by: Nikunj A Dadhania Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 79 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 15 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index a7e4e3005786..ae4920aeb281 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -107,6 +107,11 @@ static inline bool is_mirroring_enc_context(struct kvm *kvm) return !!to_kvm_svm(kvm)->sev_info.enc_context_owner; } +static bool kvm_is_upm_enabled(struct kvm *kvm) +{ + return kvm->arch.upm_mode; +} + /* Must be called with the sev_bitmap_lock held */ static bool __sev_recycle_asids(int min_asid, int max_asid) { @@ -382,6 +387,38 @@ static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) return ret; } +static int sev_get_memfile_pfn_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_restricted_mem_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_get_memfile_pfn(struct kvm *kvm, unsigned long addr, + unsigned long size, unsigned long npages, + struct page **pages) +{ + return kvm_vm_do_hva_range_op(kvm, addr, size, + sev_get_memfile_pfn_handler, pages); +} + static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr, unsigned long ulen, unsigned long *n, int write) @@ -424,16 +461,25 @@ static struct page **sev_pin_memory(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_is_upm_enabled(kvm)) { + /* Get the PFN from memfile */ + if (sev_get_memfile_pfn(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; @@ -514,6 +560,7 @@ static int sev_launch_update_shared_gfn_handler(struct kvm *kvm, size = (range->end - range->start) << PAGE_SHIFT; vaddr_end = vaddr + size; + WARN_ON(size < PAGE_SIZE); /* Lock the user memory. */ inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1); @@ -554,13 +601,16 @@ static int sev_launch_update_shared_gfn_handler(struct kvm *kvm, } e_unpin: - /* 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]); + if (!kvm_is_upm_enabled(kvm)) { + /* 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); } - /* unlock the user pages */ - sev_unpin_memory(kvm, inpages, npages); + return ret; } @@ -609,9 +659,8 @@ static int sev_launch_update_priv_gfn_handler(struct kvm *kvm, goto e_ret; kvm_release_pfn_clean(pfn); } - kvm_vm_set_region_attr(kvm, range->start, range->end, - true /* priv_attr */); + kvm_vm_set_region_attr(kvm, range->start, range->end, KVM_MEMORY_ATTRIBUTE_PRIVATE); e_ret: return ret; } From patchwork Wed Dec 14 19:40:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073370 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 10B8BC4332F for ; Wed, 14 Dec 2022 19:48:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 94F8F8E0006; Wed, 14 Dec 2022 14:48:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8FFC78E0002; Wed, 14 Dec 2022 14:48:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7531F8E0006; Wed, 14 Dec 2022 14:48:16 -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 6475B8E0002 for ; Wed, 14 Dec 2022 14:48:16 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 2CD05804FF for ; Wed, 14 Dec 2022 19:48:16 +0000 (UTC) X-FDA: 80241948192.17.BCCC17C Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2063.outbound.protection.outlook.com [40.107.220.63]) by imf30.hostedemail.com (Postfix) with ESMTP id 520D780015 for ; Wed, 14 Dec 2022 19:48:13 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=wMCYzGlE; spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.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=1671047293; 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=bvB+CkWgMZwu/G1uEl2VsVd+ebjzmKgx1a7uwFu23Fo=; b=LOEW1sUCDmznDJFqqCsbZVHM9jxphkng1qgIweJ6tBqp8s0v0RAk0GZ++sirxeFj8pVl4e g1W1BTaulSM9UWjezgup+f0bHy0hEuUHjgCET9yhjAzxovuEPTaIP7BS9ZKejI1kNKz5Xd F+Vo9LnGWpL1h6XqxeVocpt4mG/XttM= ARC-Authentication-Results: i=2; imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=wMCYzGlE; spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.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=1671047293; a=rsa-sha256; cv=pass; b=flji211z3IQW8hTh8qhGl1R/IgJ8MkgB6OjWoS560m0Mr4Tvck6LfIl7ZoG6CPhyQl4rjs Y/zITlMKfDgDGftF3OQsUUVMwAVVy9kRSTSE8ZvJCd6ldPY2MSe3I7fzpxZJH36ejvPAm1 GyrNhc7faQYXF9rB7HWH+lg0akl6R+c= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nNYgM4jKtD6mSHe802fG/uBL3josC4nq5IsMjw9gKyIfbCMdfwGp6M/dG07GmgQrgmWWrcmspDkQZ9DfkzG9Hq68eciYtHZY7dACYOYX3a8pFeDn7RW/UgWi3dzKf6d4TJrw1+tDZM9W2IiALPz526nSY+Lq3kHermfw1ypO5ApFu3lBUxPuiMAiRhqqZbfaDmecs7n8jN666sAE03epZCNeUbYInmlZOH7JLq+AKHE5tioiljtyx8eEvEpUNK6mndfgQQI6Aftle7N+iMiyct4QBPX5SQKxv+ewE7HiT0Zc+TBdOqK8i3z4UGw01l3YgzhTQGN4IBNVA7UeoF7kiQ== 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=bvB+CkWgMZwu/G1uEl2VsVd+ebjzmKgx1a7uwFu23Fo=; b=U3+SMpquxa/ms2WEmLQ+/28sfaQZVRCxiHz1esLRSHrDPnTtZHQlK75UJ7PWtlHxQKOoWIq8MKWKxMp21RWS8xUgyn3XqbU4jtrM53S6l8UioFruxkmCoMn4C345DW9h2meVtnMdMDntSSWjF+oq1PislNhPUoKLYJ48LQwMM5C411tCLPJ9XHAxDHVJ9IkOr2Yg4oi+OgYgwerMGAxYxS1QYcS2ZJIiWk1nE1J7jE7wYEUqcktipWN/Hsdg/PCgKuHrjpYPK4iFyALqCcdU8bf669Wm1iipltpxm8J9A9/p64wePHTRyNQQvcgAbVKKPKQ6TZm9yiZatKxKxJ+T/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=bvB+CkWgMZwu/G1uEl2VsVd+ebjzmKgx1a7uwFu23Fo=; b=wMCYzGlEkNAmAOfTxWcOgeZ+c6S/yOGUw55OkgyBzWzL+P83maWjcDuA6oMm3/fswc2NNzmxVOJdDrpwANg5CwOsAU84sB03Bm81CQTHU6iWC9mQTGpa2Z9YCu8B0msjbie3LasSAAwTVZiQ3HdnLnsJsOAicwjP7fE+9fYn+x4= Received: from MN2PR15CA0002.namprd15.prod.outlook.com (2603:10b6:208:1b4::15) by DM4PR12MB5868.namprd12.prod.outlook.com (2603:10b6:8:67::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:48:08 +0000 Received: from BL02EPF0000EE3E.namprd05.prod.outlook.com (2603:10b6:208:1b4:cafe::16) by MN2PR15CA0002.outlook.office365.com (2603:10b6:208:1b4::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.10 via Frontend Transport; Wed, 14 Dec 2022 19:48: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 BL02EPF0000EE3E.mail.protection.outlook.com (10.167.241.135) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5880.8 via Frontend Transport; Wed, 14 Dec 2022 19:48: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; Wed, 14 Dec 2022 13:48:07 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v7 12/64] KVM: SEV: Implement .fault_is_private callback Date: Wed, 14 Dec 2022 13:40:04 -0600 Message-ID: <20221214194056.161492-13-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: BL02EPF0000EE3E:EE_|DM4PR12MB5868:EE_ X-MS-Office365-Filtering-Correlation-Id: 169c56e7-7792-4b86-c9ad-08dade0c1fe1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 0rbxraxPtDOWCByVgnfIbq8jdZLNqlzmDssuqU1uERFzIEah+DD1EZlKHt0YFddi3klzPC8YufYVegkczH0/OoLAovcGGWpX6un7nwaBjQeD+NBh+qAR0Xis9A6HXa/6tcfPdrs4/RtKBAX4qQaoXLzDbhXcTIxS096xBe96WpIKaRc7WqB4tl4gXXtkaNvoA7HsP2V6EULZm5jnbOAnU9iFRpCzr4cHESv3/OwC0PblOTqMQnWHIabEf1qPiqeHubdJAo8bbb4vEQFPDZDHSV/klCI6orxIxPJnrf3O9LpatoRY4lerVZ3U+Lc3Az30Gp+YJPlBQPR2Giz2qg4fJEGme/n4o8MKQwb84dMitq/UlxlY6kxe5WWUhLkmlYoUBQmn788MIKlwB71EaklUkwwBSzWaVAjaHHMjmvvDgRzuU8MnQnTknz6yRU6E6NjZr+2vnga/r3x3jUxqyTz05p9Mo/iNjHhEM1vxlCC/dGVMsTHCN61DPTWjnvLWL1jaQwIVbFwtPzoM+sGC6vThAyAGcrpwsbpMkjTQJ6e5J9y7y7ZfCm0nHhhukxOzzGB+1NdJnZDCudHbznfbb8hDDUGRnta+sbFCmaeUTb6XsGrDWM8dOHFsxPmww5nL+NWRyhLNP/3oBkHvRZ1LA1BzVWiMDV88ejNVNi4nyZI0c6c0lTkIHRbYgJoXybYvfvHqpW014xCKZ/MZIL6lWnJxu4ezLtPhMV3Uv+ZrofeVrY6h251qdCXAEurvSP6aOmu7nFOCxOLh12jrdWUo6QIF6w== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(346002)(376002)(136003)(396003)(39860400002)(451199015)(46966006)(40470700004)(36840700001)(36756003)(356005)(7406005)(41300700001)(8936002)(6666004)(478600001)(40460700003)(86362001)(82310400005)(81166007)(82740400003)(40480700001)(36860700001)(2616005)(5660300002)(16526019)(44832011)(1076003)(336012)(70206006)(186003)(6916009)(8676002)(426003)(4326008)(70586007)(7416002)(316002)(83380400001)(26005)(2906002)(47076005)(54906003)(142923001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:48:08.1273 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 169c56e7-7792-4b86-c9ad-08dade0c1fe1 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: BL02EPF0000EE3E.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5868 X-Stat-Signature: urqyzfag78peq6m61ecqdu784gfqkakb X-Rspam-User: X-Rspamd-Queue-Id: 520D780015 X-Rspamd-Server: rspam06 X-HE-Tag: 1671047293-117884 X-HE-Meta: U2FsdGVkX1+3HbHj7XgHs+FXzTd0kp9yT3fhHFmhA7iq0pWZlgD7Pt8lpuum3mI7jnVb17hgQqnE9ONyJbkD/ycNRE7v3gvFO1qo4beTefYSAAgFW978f6M/uL55zl3Izg+ZkWPZaAlqIrK1TgF78CLYJTsdCRuqc8/RnSkm1WdZVc1O6/pfW9MwxAhY9k7JXr1LWNKNkT4S3ESZnU64fzAxfm6B+3jd+tpCb2mhtZNZRwQ6wPc2NVDBZahFuNWwizlSxWWdbw+T+ixXHwh43v6VJoXbJnFfevbJI8CJjOojkt0g2gCV4NGgz5uIAHI/tGMsy7vRcU5BtNrdJpGkJaeNAgt+uazDbn57YzZ/49brlBB9QbLxFIQz5fsmkVWh36nLFfqMK6J3wnYzjYl0Mb8uiUiealpNj08u9GNy0FIAaEzm061tX6QoJ4Jo5w32qbhgS8IfzRxSbaqP8E5iZVobzKXGN+Dv7yBJy8hLjqgZ730ea7xRCDlTEWBPnW5zfom79BMOz43lGAAG1sk4ZI/O6dwCGMQr535MpbMcSL0cnzGa/EPKq+V3zdPAxihyFsCVp8sWQ5mm/dfqnOSuSkdaxAc7IstMWjxnxXA6XKOMvJbfxcb8fTxs3JfDBDtQdebyKwQogxcVc1NnnFR8i58UcqZt+ncIrn/fHi2KAc7wBs25I2eXW4xJ1tDizqWAJGXg0yRupLbTEfmlT8XMgILbZ21cFEe6b13dj8CFYWmS6XAs0Ca/FBFTW536Ndq42ph/evaNzpAEx2vtQTQHb7RVlvPGg5q90XJKQwDKeAZGIKaAWrZR4uNQ5Rt3pwaGqcWWxb6cvW8gEegQzFAOfcB8IMT+ydMiuy7EICoHhjhJ5l2DKlMcjQMfVBJlbxFWvXtI5BEhbjG+Fon/p/JHeuD+49HbB7RNHLN1uWbWkg3BJ8SF9VfIU0BWdSqfxfyCx88FJ4Vu9DbmN5jrFMp BB0IJEvk Hl0jz5RXznsWf4aUx2KKq4BQx9ZOha4Vd2oCV/JGpZBmTo3XvVYgvlKvkeA== 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 | 23 +++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 2 ++ arch/x86/kvm/svm/svm.h | 2 ++ 3 files changed, 27 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index ae4920aeb281..6579ed218f6a 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3179,3 +3179,26 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, 1); } + +int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault) +{ + gfn_t gfn = gpa_to_gfn(gpa); + + if (!kvm_is_upm_enabled(kvm) || !sev_guest(kvm)) + goto out_unhandled; + + /* + * For SEV, the hypervisor is not aware of implicit conversions in the + * guest, so it relies purely on explicit conversions via + * KVM_EXIT_HYPERCALL, so the resulting handling by userspace should + * update the backing memory source accordingly. Therefore, the backing + * source is the only indicator of whether the fault should be treated + * as private or not. + */ + *private_fault = kvm_mem_is_private(kvm, gfn); + + return 1; + +out_unhandled: + return 0; +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 7f3e4d91c0c6..fc7885869f7e 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4830,6 +4830,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, + + .fault_is_private = sev_fault_is_private, }; /* diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 4826e6cc611b..c760ec51a910 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -683,6 +683,8 @@ 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); +int 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 Wed Dec 14 19:40:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073385 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 4B987C4332F for ; Wed, 14 Dec 2022 19:48:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C0F368E0005; Wed, 14 Dec 2022 14:48:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B97CF8E0002; Wed, 14 Dec 2022 14:48:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9EA818E0005; Wed, 14 Dec 2022 14:48:41 -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 8AD2B8E0002 for ; Wed, 14 Dec 2022 14:48:41 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 57CFE1C67E6 for ; Wed, 14 Dec 2022 19:48:41 +0000 (UTC) X-FDA: 80241949242.22.E1D3D86 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2062.outbound.protection.outlook.com [40.107.244.62]) by imf29.hostedemail.com (Postfix) with ESMTP id B05F4120012 for ; Wed, 14 Dec 2022 19:48:37 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=E47+Jt7q; spf=pass (imf29.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.62 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=1671047318; 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=983qr7kV4HVkhdJdw8Aarr87tNHNQaHp+YAL3C1s1I4=; b=aD5JsLC3mZt5zoVyskEcLZ49T4mxupelLm7fMPj6R6bxd3RqQpeCx82EAYWZNaCilhgkKj DZo2FKicrcZj5jaNjJJYNOU2KhSe4TRL3Iw5tu2PFgIzIOKDnGzgIT+kmBm5GHWM3gRSsY /SMOPExWjVvDspo+rYVR5jVz+3X2uHg= ARC-Authentication-Results: i=2; imf29.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=E47+Jt7q; spf=pass (imf29.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.62 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=1671047318; a=rsa-sha256; cv=pass; b=l6JmWZo+RiKsBRiYPzOeWqetngvozaOjook+DS0S0zYI9OFF2K9ymMjfKNVnHs6TMhU0s3 W/I35j9MCMlluANPspybEfN620SQBZcX8XOCbavleW2Kr+n/NpghixcQWMfeDSdmLoD3bD n1zu3rovrzOQAH0sxsntAOmnAsHlTrs= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bCm9YrXcX3m9lNJ5FWuxRXQ1gER2NxhsNqYdtjTBq4F6DiYUlIO8X9TJmJJYb+FNdiirQCHBh8SOAQ0ie3LKyc6lGPB96Z0Hj3Mc77T8rQ28BIsqW173Rm+0q5TMx2/kaVs7fbIjnNa47Kc1j6MC0a4S/Srq5sGUXCaHGia9K5Ut94uFJaSOi3Xv8xH4qH4DjOzNv9vGJyyITdqP+d9aoBjg1ICg9GDvzvu5y9paGTBUvPsoaLG6LUGYwy1JkQO8aMlm1ZJqu8YakF0Ihst1bL4v128+IScS7TaL2zrxh39iJcvXgbZ8dXYuG9Ldtcju0/BMzW7HPVfUjjo6JyQXJQ== 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=983qr7kV4HVkhdJdw8Aarr87tNHNQaHp+YAL3C1s1I4=; b=AcqpnMPy+gINK4owICMtVRjvYReCf0nWoNIOxVrujL4pjnkW4SpuRlJktbZdvJFppVT7Z28OLgvjGZNx4foWARXRmSlhLtbMOJgB2mbdX3JdUlvq3KGlHTWdTQPOw790czWSM7trNI5rbLAdYkp0Shd/o0ZjH+ywOGJtl1gSQKlG6V3us9zFRDluZsNEuzw5IjloGwIqY4ta/DMK3KMJkbSdhj83GYn0WLo2trBxV+kio3m+KU6qycN7cKG1oDuWED173YPP6lqp9u9eD/IMPt7h05uIL/0qTB3eFkwqHeTEQbwpxyoVJ3VQYI44yT7POflvHD4I9ZR2YfBzUkLfGw== 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=983qr7kV4HVkhdJdw8Aarr87tNHNQaHp+YAL3C1s1I4=; b=E47+Jt7q/qDwLoOGWYV3iRNujtqrIj0YCSPevpHVoaU3OYN1TgVjmhJ1apmFHzm2XneNejJJGhsM7XNUfVLCrHh0vuvYaSjJaes15ntRRZBohGf6g4V3Z3Z5CutCG5a/VgNLsxYMPD3Q01wiq9CqRnzeRiT+jY/zcJB+5Of5DxE= Received: from BL1PR13CA0101.namprd13.prod.outlook.com (2603:10b6:208:2b9::16) by DS0PR12MB8344.namprd12.prod.outlook.com (2603:10b6:8:fe::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:48:31 +0000 Received: from BL02EPF0000EE3D.namprd05.prod.outlook.com (2603:10b6:208:2b9:cafe::87) by BL1PR13CA0101.outlook.office365.com (2603:10b6:208:2b9::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:48: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 BL02EPF0000EE3D.mail.protection.outlook.com (10.167.241.134) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5880.8 via Frontend Transport; Wed, 14 Dec 2022 19:48: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; Wed, 14 Dec 2022 13:48:28 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Jarkko Sakkinen , Ashish Kalra Subject: [PATCH RFC v7 13/64] x86/cpufeatures: Add SEV-SNP CPU feature Date: Wed, 14 Dec 2022 13:40:05 -0600 Message-ID: <20221214194056.161492-14-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: BL02EPF0000EE3D:EE_|DS0PR12MB8344:EE_ X-MS-Office365-Filtering-Correlation-Id: a99d12b9-a925-4ca9-300f-08dade0c2c9c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: HElM6bhJpfNO3PYjnq1Wd2gUmXVIfRnKJj1Lnq0YvdVxc1tCHG+GDXM/5HGZpfO11wXSo97QWbuUsvgyXGffsVh8V2uluz3sZsO6M2HjVNwHEPR3hlnhWGv5ydsLgIT5q5Tws5GUR6uafV4Be1cDNyqS8TOAGdfV6l0nT/KREJh4Wt0G1fpiKrGRZo80P3WbpXZLQcvM3PHGsNWJl2sQUWUAOUmQjC4X6FYKcYVrbOaUodMVPk7Poz2PGQ52aXRYqc/TNVPWavTtvV1yfhy9RY2NuPVEbqPMxPyx6s/FHX7E9h6Y4zhMnIp5lYmavleSIaxWCxHm9HVnzVPme/72Bnz3M9jJonOEcpRU+DDN3FtT5xFkl9S/kUIppKBZovyguFkZCNMnG/CdrFNny840gzHGagpOZhKgevMm9DRcAa5WCqkTU5AufDGGq4mWXtOhVhMjnzzv+hHpcyPLMc15R7W4DQhAwt9dTMY3RCV8fEoSMGYTd/UgHoRF1pmt5pEQb6IP3YG8ZIPfaq9/ZFwaeU/4hnA/ZWQLf9tivYN/FymXr+D2nUgQX62q40TmQaj6VNMTBYB79Z4aP5z8dSanisBdKkBU3mt3oCrnDwxgGhkEgT5jgZ9Lt6N5df2Dbx+d17rSZYwTXDdr3IX7ybqvBLp/60zNTDeqr+eRjWY4ggHyTW1RdfSfNw1i2SW8CaSTcINQWOHQgeqBz3iK9hasUqiQ5oZZwsLjVFjgcPQm7/0e9512X1WGu1ITPah/OkTGOfV6pRcXFSIfgi5tyNP6ig== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(136003)(346002)(376002)(39860400002)(396003)(451199015)(40470700004)(46966006)(36840700001)(82310400005)(478600001)(44832011)(8936002)(83380400001)(81166007)(16526019)(2616005)(7406005)(41300700001)(86362001)(8676002)(40460700003)(70206006)(70586007)(1076003)(26005)(426003)(36756003)(5660300002)(47076005)(4326008)(356005)(82740400003)(186003)(336012)(7416002)(36860700001)(6666004)(966005)(40480700001)(6916009)(316002)(2906002)(54906003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:48:29.5126 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a99d12b9-a925-4ca9-300f-08dade0c2c9c 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: BL02EPF0000EE3D.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8344 X-Rspamd-Queue-Id: B05F4120012 X-Stat-Signature: uckyx1pn6tgpbtz5mx8j9o9h1euzy1uj X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1671047317-66739 X-HE-Meta: U2FsdGVkX1+DQWZTN9JNXmQJxtRVhHms2cqBFcofom4vNjnWzllLyeHOcCywt0JsKzHALRFcQxHKXsND3fgtyp7W/kWBG+MKZetO72I9CCBWB9HiO8sYUpQH1hUn5TVowVFYm1FTx4/8FK6IXoBvSB4BNMXfcS2yr23nADBaXRzKsn6wWuyOaCn+0mksgRq/wzY39Dz7Gbm+4OsP29k7043luKHVNOZ/Gdy3XvyXdLVFGatn29OoR7qJj/sb3ZK5/MMsJcGBQ1gi9Ty6TzVEr8sP63nL2iFtE9XMpDKE5tsHI7jEWRsueI1ddSVMbwoCRIBMpUWqCKCMdwQqF8RG7mbBv08cGSpEE6Duc95HQ6AFlcyn16oi6Mln3G+oncy/63GwDf96QZCFSwj7TLtMUfeIXliEa2pIvX2oUrLhj/Yl6F//HQJQfGL88kncXCnjb9whkimT8+tpeHRxJgGMO8XNU74zs6upDXw1toHYmN7n+uQrZGDmyHQ+KI8uASciDuo3brjBELX45YIQn7MaQvBPcbmo2poee62TAUONWd7q5ssYybfNYFsCfbenRotuv0BC0+id7hH7ObC7m00ChTLGNahCVE6+WKQKtgERSjUMWa4IbA4RfolKHtGQMzd493/VjTb9B4JxTFfoLxct/EczyYm+OeFe92cKLHMI+QWHvIRWdc1QC8iG/6RoBUH2TlSRNQzd7X2TqoeSKLivMKZMn0KWFEeCdq0lKjuAiRTHkj6KQNIHNlAdwFjnbzfKsfLnJm6yC2l2QfrBpi3McaX5Da5HbFJpU9OMNT0WaMEa8Lup951BcUzEr7KlAMi8hTrZ/o9/W+sDarDyYuWdiGh0XjTh3RS7szjcYqSpeK+wfwNVyLM+IVhHOonphjBndkB9ChRdJBtOaomBkslHmhciIiaUj3hJN3b+t0zIBhweRELMSS2xtD4hFOMhHob6CtcsccbA4tFG6CIqcon ADcKz86y RcN5g 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. Link: https://lore.kernel.org/all/YrGINaPc3cojG6%2F3@zn.tnic/ 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 Wed Dec 14 19:40:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073386 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 6EC1BC4332F for ; Wed, 14 Dec 2022 19:49:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B73B48E0007; Wed, 14 Dec 2022 14:49:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B22598E0002; Wed, 14 Dec 2022 14:49:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 99BB18E0007; Wed, 14 Dec 2022 14:49:02 -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 841858E0002 for ; Wed, 14 Dec 2022 14:49:02 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 59ADEAB612 for ; Wed, 14 Dec 2022 19:49:02 +0000 (UTC) X-FDA: 80241950124.09.C150342 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2077.outbound.protection.outlook.com [40.107.220.77]) by imf20.hostedemail.com (Postfix) with ESMTP id 814E21C000F for ; Wed, 14 Dec 2022 19:48:59 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=rEKAk+3+; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf20.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.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=1671047339; 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=KphY0nexziHJ8xx+b9qdlFw6j59/C+D0P79fmBASfcE=; b=qf8YVVkmPesn/phpNtCp5dsf+uUdUz0xyugdHflF8ZKeh8j6a0PjXdqgwsApnJtgKe61kG QZNv7iuGmr9a056nKFgy1ZtXezoDquLpMjbhsds7zXz+k6Q1D4ibesvEtg/n4tznf3+4gZ a7p4PS4xIK4OXUlEgYITihuIII1wJjk= ARC-Authentication-Results: i=2; imf20.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=rEKAk+3+; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf20.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.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=1671047339; a=rsa-sha256; cv=pass; b=IaNmfU5PYqpk6D7ekRDzQlQ9RlRMujAq4yu5J1TTzQxDgJ9Yo49z3Ee9Xs/uYCdtSm51hY 8RSSksF38SzzUbl80iFHDnYEcfdxxWr+1g5smI9MyDrwcsQyrYRD/BJOce7mntPTAhphtm 7v3DPXlIsngN56ES1mEdVV6iZyQgLyw= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TOhNFYptC2xhjLrxDkv8dC1tML0YumzjEK365ET1F1Mzr0VuK7Aer9+ykzsil0THd0C9xkBu70qRRw17NQv6rc8B7UYNYQiWCYkOw6FMYF2gjraslQCD7ADG0SqvQ0IaM1Nss4f8bY5IuqmNrbZBZcijOPc0R1abCRP8356/O20dZaYY9PhACDEl0Xp6mBPv2PZNH3LXBnSBJ9+8tyEWDQDwCyv16TEpVvuT+DsNCjxZzuMt0FaMyexX1RzbdPN7oEi/0AVFEqC5QZsty47zNkO6si2/Cz40ruQQ8YU/7U/e/VIbHcar7XEk0qXWT5DNx1rd6OypFZyD1A6ZQm4yLw== 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=KphY0nexziHJ8xx+b9qdlFw6j59/C+D0P79fmBASfcE=; b=UYkOdChObdAMxoB8RI7H94+1M8iHB9QSbKwy2zV6ttDjYPgHpMzeB/SrUf7woylKDUgSW2CpEndd8ktXwv7l0oeiJQTDd1+zbEdwLg41uIecjl8qq22hcvgJPQxa2i+z6fP1Ylob+iyeoqkqDnjkpmKME8m/lngdp6VhNycb/Xf8HDbc/f+lRbn4AS7Ye9Ysblbp6ogUrTURMoT93Ivytsfy0dfRF0KpyYzf/zFpkaYQVkcB6fct4QFuc9jyC8ex1lgmBkj4cTiPoEwD9kiamrsZHw0qAwQrffSCZSh7hJ0ofsOGJCdrGgk2XHMyQIR05DPgeXipFLcwweheTx2BUg== 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=KphY0nexziHJ8xx+b9qdlFw6j59/C+D0P79fmBASfcE=; b=rEKAk+3+cS44NPCg1kBBo+RRRzaVUL9F4v0TICV4jksidX36QyiJZ6H41W7cqiEEOPWF5KX2gz4EEma1mIdO50gwOARZANRnvezlnuQ7dYdp0t4nqDbebJQPhtK1X+fLpxc7t5S2MG+tiLVyp4enmkF0OfL1ZFkMKvvxb9rIeGw= Received: from CY5PR22CA0060.namprd22.prod.outlook.com (2603:10b6:930:1d::23) by MW3PR12MB4492.namprd12.prod.outlook.com (2603:10b6:303:57::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:48:50 +0000 Received: from CY4PEPF0000C978.namprd02.prod.outlook.com (2603:10b6:930:1d:cafe::46) by CY5PR22CA0060.outlook.office365.com (2603:10b6:930:1d::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:48: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 CY4PEPF0000C978.mail.protection.outlook.com (10.167.241.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.10 via Frontend Transport; Wed, 14 Dec 2022 19:48: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; Wed, 14 Dec 2022 13:48:49 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 14/64] x86/sev: Add the host SEV-SNP initialization support Date: Wed, 14 Dec 2022 13:40:06 -0600 Message-ID: <20221214194056.161492-15-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C978:EE_|MW3PR12MB4492:EE_ X-MS-Office365-Filtering-Correlation-Id: c4d3f694-9ede-493e-0e1f-08dade0c3906 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2xFZFCMWmoc7enASiKwAQimkzH6Emc3BN1f+lhT3OttRWp78gGdSCyTwZck75gxZPSEMaK43WkevD1quulNeWJkUuxBuVyVWN7vZ+uPtmo11WF5R1gZeoY6yH+Q4HMUv1LtmyckLbhOyvTbBtseHvpjKEEgR9TRqv8hDbSkjfHBhFoB8OG1m58qCB6HqrEH6aJDAaSNfcDy/CaH0o/0paPbjIzpNMw05/Bd9px/t2jTdjy73qFi1zE5cPJ1Z4odKU8DrzXuORxPF/hUYO9mQmdOSn/HopDJlN6SjV8C0IdsmcCI2sUpeUPM+5WT1q+8BccYtRpm4IR/TA1ET2/P7rQy1mAJQK72WxDPex1aCM1xk1MpPzbJhirvbJgU3RFDnvPKWOY3EP4brL7hPwgYQX5vIvLHp+leW6cU4bgsAYsMEBcFJIHILvv0z+FX3b+ECFkN8JYt/G7G+fe6FrK448st0T3sOkw16k5KmtqwjeVw6iVQf11qSOHe2OWWKD9Rzb0QTluRyL+zubGW+gEfiohx5i8H4aEGw/nKpaRKzjeNm4HXiuldn5cZ94+d2yA+TuqQ6M4oDCOXc5bxHNxyVgxCnpuIGo/j6LEJX51E8Finq12xighVpuYvFin/K6ViOinFDwY2TveTIh8mwyqL4yAPK74i+BS4RZmsY1I89EPW1y4d85GnXr01H/7i5uzD7c7OB+QdCr2jPtVUeiLM5QYiQp25+tlr8WKkHvprvsG4= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(346002)(376002)(136003)(396003)(39860400002)(451199015)(40470700004)(36840700001)(46966006)(36756003)(81166007)(7406005)(41300700001)(8936002)(6666004)(478600001)(356005)(40460700003)(86362001)(82310400005)(82740400003)(40480700001)(36860700001)(2616005)(5660300002)(16526019)(44832011)(1076003)(336012)(70206006)(186003)(6916009)(8676002)(426003)(4326008)(316002)(70586007)(7416002)(83380400001)(26005)(2906002)(47076005)(54906003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:48:50.2924 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c4d3f694-9ede-493e-0e1f-08dade0c3906 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: CY4PEPF0000C978.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4492 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 814E21C000F X-Rspam-User: X-Stat-Signature: q1nb3dbi4xjjqq7prc1jk8xx6pzwaw9j X-HE-Tag: 1671047339-546106 X-HE-Meta: U2FsdGVkX1/rCXPGtTzl8rwCEtFwYvSP608T3KPTHV4KB3yoIbOaj2jT9dP+mDGiI318vcZXCgX8N7PBdMZ6VFXDm7wIrEuKLCHV0AhWfPT7kNCjP0M/ss4R8OKd0Swws7/0tKPLWLUHCyatnpOPILnrPbCq3eUXibjL6f2lZeB5u04LfMpxwivULxE76KvcU6/Ist5oPpDMx4A1J8UOic+Ek9lNvbTf2XLux809H8rknpDXA0A6EwFwX4yodlOeYdTVYCEitY5eXLqHL/7y9ZGzXinXmOKAPewnSNkc/7asUgTvSqTRH4K7z9Wxu5hiUPvYQzFJsh3akyh4nwXGqqAoxZSgNFcB+us7tRvaxWNC9wsWx2FC7Pk7krIhR92vgoJ6azSnK9RdHtolP/qKvwdWER85LAmDzgpatETtNuPQfQo51JqDr2LtKo0c5bTN85H8awDUYqNvPByB5h7nqF4ZHrL4lGREQLahC5iCTpP9Pau+XMe3CNFDjL6AW6Ey841TFcf4rqcp5/O4fZNZ5WIcxFKqktF/TtDDBqpKXxq7AG/vK1sCVEee0HW2kLJoGOaL74tGV9gz0v0v1gqiDs/SkKr1uvOXMY9bmvs1E9DLqUGql7m/OG7JNHqLVtE3ZrzT2mh7LRhVmivQol/zBizfqlX2DcpvvSg92SHXif7OamK1OBmpb4e1FIleOLvQxmG+yghBrVmzLjvDFJJTl9b9bRJzyO5tBfEO/oKxcgZB2tQbnc9iiOZFJgKqxDGTg4lzHBP19Kg0ZYxj8j38TTjk0vAsRVDNdqGf4WPNgbZYdh66tkEkpf5eYpz+qzuLclZS/ZWFfGH836yJtuZcA8CUuMK1ogDPTFngFVloIqPFfmJng67YAp2gtzFnGNG2scR/ELAEdHWKWRNDaEvk7YJENcW5croxdATMg4rqn1+r0fBUcV4sK9gP6GfHxqKrMsl8pRTJzK1zQQD9Uw6 OgkCzsbQ x6xZC 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-b: 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 | 180 +++++++++++++++++++++++ 3 files changed, 197 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..687a91284506 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,170 @@ 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; + + /* Initialize the RMP table to zero */ + memset(start, 0, sz); + + /* Flush the caches to ensure that data is written before SNP is enabled. */ + wbinvd_on_all_cpus(); + + /* MFDM must be enabled on all the CPUs prior to enabling SNP. */ + on_each_cpu(mfd_enable, NULL, 1); + + /* Enable SNP on all CPUs. */ + on_each_cpu(snp_enable, NULL, 1); + +skip_enable: + rmptable_start = (unsigned long)start; + rmptable_end = rmptable_start + sz - 1; + + return 0; +} + +static int __init snp_rmptable_init(void) +{ + int family, model; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return 0; + + family = boot_cpu_data.x86; + model = boot_cpu_data.x86_model; + + /* + * RMP table entry format is not architectural and it can vary by processor and + * is defined by the per-processor PPR. Restrict SNP support on the known CPU + * model and family for which the RMP table entry format is currently defined for. + */ + if (family != 0x19 || model > 0xaf) + goto nosnp; + + if (amd_iommu_snp_enable()) + goto nosnp; + + if (__snp_rmptable_init()) + goto nosnp; + + cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/rmptable_init:online", __snp_enable, NULL); + + return 0; + +nosnp: + setup_clear_cpu_cap(X86_FEATURE_SEV_SNP); + return -ENOSYS; +} + +/* + * This must be called after the PCI subsystem. This is because amd_iommu_snp_enable() + * is called to ensure the IOMMU supports the SEV-SNP feature, which can only be + * called after subsys_initcall(). + * + * NOTE: IOMMU is enforced by SNP to ensure that hypervisor cannot program DMA + * directly into guest private memory. In case of SNP, the IOMMU ensures that + * the page(s) used for DMA are hypervisor owned. + */ +fs_initcall(snp_rmptable_init); From patchwork Wed Dec 14 19:40:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073387 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 6EEF8C4332F for ; Wed, 14 Dec 2022 19:49:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 138B68E0006; Wed, 14 Dec 2022 14:49:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0C27D8E0002; Wed, 14 Dec 2022 14:49:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E7DDC8E0006; Wed, 14 Dec 2022 14:49:16 -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 D76798E0002 for ; Wed, 14 Dec 2022 14:49:16 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 9E0CD40509 for ; Wed, 14 Dec 2022 19:49:16 +0000 (UTC) X-FDA: 80241950712.25.D99DE34 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2065.outbound.protection.outlook.com [40.107.223.65]) by imf24.hostedemail.com (Postfix) with ESMTP id AECF7180008 for ; Wed, 14 Dec 2022 19:49:13 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=dwSrjTpT; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf24.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.65 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=1671047353; 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=p7kNHsOKDKxIu0jjw+KLsxb4fgN6qewANaORLbviVCg=; b=6W9zc0TV2n0kola/W44cCxFB7mcMySS8+7KqQ5/0d+27UEOjppHcnnSVHt7wpApvtX/qP2 ZQslS413b3Gx3mOeSn1QxOrB1mQrjPAetNQN2T3fvAQKo5CsEYChkVdJvBUbo0++agMrzN 3etlVXVD9mGfgWmqsQbH7CjeCFtbpws= ARC-Authentication-Results: i=2; imf24.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=dwSrjTpT; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf24.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.65 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=1671047353; a=rsa-sha256; cv=pass; b=uVWZvNRwULcRdxzmL4tzHcM9yunz635STwxK7mmblrtda/6BhNeNVwvvOFCy2iDCIs+htx JeuqPLt+FgtEAPln+CzROnLXmkLiW4Pe4/lfmceXf5RYJG59Kq02hjq0WyL9Vu3Wk+zJVC xYLcqtouamvTsNrXSjPjQrNhgMkoYnk= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Cn2Ae1LQU+Vn0XFK4F8HLBvNwEmLCNFvi0LYNUAmLLvWMYNWBHXEoB2/YrDHOqjhKuWt0lzIuytTi4UfoeEjs29+YMZb6Krd/MIb4hr2A/CIEL7qpReFg2zs+8kZJNvlcQXycss+Ne8UJ7hrAu/Mg3S5++zUn5QBK9arI4FVP9Wfgk1IkM4fVTWz07kSKA7oEzvr46RoCrv5Hk7funmCcFT4/czBn405qmALaQaS9xcWeq7Z2BwSrr1fU2Sh/0BSRdesbuf3r6ZEnfJActdJbxcUNf6dg2/pnxzcje4JfgTXbaHID/XhRGFZGO9zWR1TfX1/w9tof0qcqLrpbLB9Gg== 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=p7kNHsOKDKxIu0jjw+KLsxb4fgN6qewANaORLbviVCg=; b=Av/qiHeJwhZWwrHzhL9Ge+Ywc6A3oNstkxHmr+4/gkHYVFcZOh4yJODi5J3u01pjl5AW1JBBORG3G6ISdpvJb+mukOjeSSL1KNNlv8DvNllYXURQpzs0KBYuIzefpiQrPOkr5kmBrBkIs4+Y1EVHBJzq9dGlnEJkYHOkK+YOmQaBCGVkht8tsN34cCKchcnUgC1JO7e14Q0TbsFguCHw13Sn3WjwuUT5WF9D+tVSTpaaxk4WTV7y4717/WLuBJsksmMjH2r7hm39xGplj61RoCJ1GHBuSh9AGVRSm/YD+aECCliBe4xiMFhmo+CWMpTRqjDCOxOWkipfq+RhGibseg== 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=p7kNHsOKDKxIu0jjw+KLsxb4fgN6qewANaORLbviVCg=; b=dwSrjTpTD+cncx+8Ct5UUL6oBw46pwSq8dFaui46Ghbu9QPCUZnuoAN6t0w/wVTKuzIPlQHQy+GvtiXirB1UHfZOrIISpzenYHU5e5YF5j4RDoMX/Ef/fL4OxDMF1YKMgtLLdxoN8logvs/7ELP1/IietOWFq+FMT93VGUjyDJo= Received: from CY5PR20CA0012.namprd20.prod.outlook.com (2603:10b6:930:3::16) by DM6PR12MB5520.namprd12.prod.outlook.com (2603:10b6:5:208::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:49:11 +0000 Received: from CY4PEPF0000C97A.namprd02.prod.outlook.com (2603:10b6:930:3:cafe::54) by CY5PR20CA0012.outlook.office365.com (2603:10b6:930:3::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:49: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 CY4PEPF0000C97A.mail.protection.outlook.com (10.167.241.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.10 via Frontend Transport; Wed, 14 Dec 2022 19:49: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; Wed, 14 Dec 2022 13:49:10 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 15/64] x86/sev: Add RMP entry lookup helpers Date: Wed, 14 Dec 2022 13:40:07 -0600 Message-ID: <20221214194056.161492-16-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C97A:EE_|DM6PR12MB5520:EE_ X-MS-Office365-Filtering-Correlation-Id: 60d16971-69c7-42f6-d3d9-08dade0c458e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: +t2pWl4C43ruuLfvcGn2WkSW+Sz3bUyHw0fA+f7PF6R1xtG9xlHDPclQ4h7P0jqe5T++ae2Mh190din6M+TyrosQnYuvO0UjHwv9LnC9AtzS9FSZNbGzP/p0OiZpDoJczrE1MCR2JWuG6b4E2ZeWNcidUVh5luJZ/ydbe0Z8Yoenk95KaIO1zg4ysMKeGtYasQ3MUVLudJPCX2LVTaMy2B/JI2+BpF/F3VhaeTUjQB+IAE6EsnzHbJD/3wVo00IW0LxpY3k2tmhOzQD4CRTnt35a/QgzDXMcd+mRpxjoK34/fjTkFPpyahHO9rY6s1W32d+J2MKWGuvHQtTEaY2mwTW7NF2D/DGlWyGCADpdvjd8ugC/zlPp2P5eQNGHwOef1qWulLpuWo36vc9Vdi66F77BxkVABZc3czxC7wFOmPOE3xRb+KFsR1RrDCyxW7i2BvK+ZxrEBn8Zb35r+McB9T662+JJ8cDmkhvUIeUSav5WsSTWmHDe06/VooLERkQAVJGkodWENp157EsEMfra0Y7WQtCIhPexcgovZbB2v5VkaBDK6ZpA4JuOpEgqC8le9ThQWmsDLGigctp5MimI9S6tpO2zO6nfi5ACdlaXiL4zjZI7KVFehEdwGxw4wZWvdYZ1iZ0boOjp6RE8oKEo6/3ghlz1ej+dYYjw/1HOMjuUTtdm7NpE2NgxaTu1/sFvLG/4tPhIhnO1BfeD930Hsymo1i4XenETTRqKfb9KkzuJlipKW/t0Lyg0o9qMTt1WBiCMAGMPDvMiadFqgm2NPM6B322JRG66ncP36u5nP+E= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(39860400002)(136003)(376002)(346002)(396003)(451199015)(40470700004)(46966006)(36840700001)(86362001)(83380400001)(40480700001)(36860700001)(316002)(356005)(81166007)(54906003)(82740400003)(7406005)(6916009)(70206006)(70586007)(8676002)(336012)(7416002)(6666004)(2906002)(1076003)(82310400005)(5660300002)(44832011)(2616005)(26005)(47076005)(8936002)(16526019)(41300700001)(426003)(186003)(4326008)(478600001)(966005)(40460700003)(36756003)(36900700001)(309714004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:49:11.3214 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 60d16971-69c7-42f6-d3d9-08dade0c458e 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: CY4PEPF0000C97A.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB5520 X-Rspamd-Queue-Id: AECF7180008 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: fymdqk8ha8etd9bqc4oioxeqy8byemx5 X-HE-Tag: 1671047353-812613 X-HE-Meta: U2FsdGVkX19zmyyfSCwJUp2RGqk1D3nhxo0WrLzM4f35et1ezqGrv3wLo/VAuVY39YLMwyrcnCw+CGBikwnj+hy/LvIpQnl2KtQXxggUZ/a9JIbZC6nwGbmpY3yZjg2KDT2Xo6XVYPE/Ukx1vH0k7AqiX0x6rMQWHWUZVL0iqqKkUgs+/n/odzma1UROAH5kc0o56AddgIfFuZIeWe9zsFx6Ah7F9boPAB9FZMWIB+gExmUUDM8xbf8VEuzURFptdlhji3m/kXaYr2mEXub1v4hHjryKBcc4MO1gIz83U9zVIetLuYO2tuUYJvbSuQ7CVesiTty6LJYw+7Z40DvSvuxhsSNdTyOQNxo+bg4dDK6fFJwqI3sPKTgPYR/lsJX8aXH+I6uxolNW0LbCK2oo1Iul5cBUxNLnbFoB7IifXTJ5s6Cxh0mIYBSQ1/TBHn8PfV8TLn22Cv7/xNxKeQeXWC+aibbLDkAhke63Mu9j1qGvEvMj4ZqP2Q2LijG5YNtMvR7fAdbA0V8+oOvyfyp4eITbTiNqCcQCWm+IxhVhR9Vdaer8u6l/ayJyyDQuK57CU6pXHd3xjeRqYAQ8t6+9/Goa97yzcfBMmyZIsyutjVtEZpRAUP8m7YWgDmMp0nosL5P5mfeFaOBCzV+fL4Sy9WCrNy+l3g+8kOyCqhArzvNSPd/gCEPrHX8s8KEwYUKqhU1zy2axGSloPzrgRBDj/7YmjZo/BuBMmFXfEuH25LJzcuLO0+S6VLoErhMumsPvb7Ae1VsqrVYbuimfjsw8yfpDtj+v/yKocpEV6ZLz+ijcvvdV8A8fsUXSXScQhVImFXjwVBclSUlHxgAvgitIAM7fY+R/kAPP4ZxQMNaHNO414Lq5S42Pi82b7L7hTUKtgFrupUJ8grP8altUZB/E3KAq+c/I0gjxHoKWIwc43n1e1xD8v45up87vzZw9rPe0d6c8Vst1hdNl0coPZXr +JqeSatC mZ2ms 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_rmpentry() 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 | 83 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 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 687a91284506..706675561f49 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -61,11 +61,35 @@ #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); @@ -2440,3 +2464,62 @@ static int __init snp_rmptable_init(void) * the page(s) used for DMA are hypervisor owned. */ fs_initcall(snp_rmptable_init); + +static inline unsigned int rmpentry_assigned(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 Wed Dec 14 19:40:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073388 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 CA947C4332F for ; Wed, 14 Dec 2022 19:49:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 668728E0005; Wed, 14 Dec 2022 14:49:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 619778E0002; Wed, 14 Dec 2022 14:49:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 492548E0005; Wed, 14 Dec 2022 14:49:38 -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 37FFF8E0002 for ; Wed, 14 Dec 2022 14:49:38 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 18B30140A0D for ; Wed, 14 Dec 2022 19:49:38 +0000 (UTC) X-FDA: 80241951636.20.E507C61 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2066.outbound.protection.outlook.com [40.107.243.66]) by imf29.hostedemail.com (Postfix) with ESMTP id 292F312000E for ; Wed, 14 Dec 2022 19:49:34 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=3u8SHPVF; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf29.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.243.66 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=1671047375; a=rsa-sha256; cv=pass; b=kSH+QCZJ09lv0dqTk2irqpZrcPUxNcZ/ylt0tU4eBl20+bf6XYQMCXhM+sFo+aFZ9yYeyh 0zoZwbUWf2c9rusin0uBRlwf5SUsYFTQ2FowB6iEkTcNReUW/ppjLzvC5rxgKGCBh9a0PU Z3qtC92JjlOvFwfi4pA9pWZKHcjlZBc= ARC-Authentication-Results: i=2; imf29.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=3u8SHPVF; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf29.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.243.66 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=1671047375; 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=0DDUpcAbZbIX2Z0x25kQh8te3lopnStDcycaGYXFBvw=; b=0K1Ike++6ASn/UDBvJhCQwv4wZQ++sPi5ZHdx9uURt8Hm4k+SwGlpb4Vv354xCYM/Ql9Jh l+LDn7IFCxAJHw1OxelXLlkEe6oZgZILuXSgWMnEUcoAdS+tOe1JaCDIM+q7/k29mmm5JL 0d6h0t+UEo8To+FNhCxYVReVWKywGyc= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZnCdoyFXiQXbmWX1uYJ9GyHiNG54F+bWFDOF+TLVfU/MmD5mUD7SmvlFndFTaIJvk1hxBn3TZ8UPzLxXQepSqgYoIXPaU3/k8Q+JMWEsjUR8W2Nky2cEVQTDI7AKO0XaK058WizX9Patgb+j0wBEfaabQOUaLAnKOuH6j3CAKBn44dTNz4Nk/mn3gt37lFeShulRX0uZulIA7JRmxS1kiPiF8a7gdvqr44HCPiR7skyw3ANvPSaDc9ZtS2C+D6cyZemaze/mW0vqSFsIF+3W+54tbyc6PkzNCENCjeMfAzzEAw3kUi0m+HGY1bfIa/Bk2Qw26dZ4bAROFd1k+IRcOg== 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=0DDUpcAbZbIX2Z0x25kQh8te3lopnStDcycaGYXFBvw=; b=HW2aPewNgwuIa6/QCHPMfgeIjjehZpespm7/EWWNEoCwCcwe7DcsK1AXvfM2JYLAHIkS/rEROlHmu35KS26gZnvkc3WWlYtbCObNqylldBOcbxakNpJRvhI/PbIyWzCfBdcMVV4G3Q+6YMQpH2CEe6ukLou0GtL74Hie7kj3tXBqw0tZfOeey/lLwCNNkqcASjSkaNhq4PegEo9p9+axGJJh6SF60HVvvB+ZNmJk1uNV1bCVe24KxL4g2NCFh8orLzETpcy1VBw+qxGVE+lL+3ql4hk+TKtvKgnhDSVWtBtRqD1Yx3EKsdgwdlq+Sj64MmvQ2g1EXuUKv6lcLpdYKA== 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=0DDUpcAbZbIX2Z0x25kQh8te3lopnStDcycaGYXFBvw=; b=3u8SHPVFA0rIlLi82kMbqVu7j+t+SAkb4OiUZwDrCv3sn2SDnF+4h3n5NxQfESS5vSlRfzND12r7DrsZSugrGaqas7jYoMtKUIdcGnZc6m9XDrolasBgwDliEBlkJAz/W0vhtqKxz6CVy82F35yF5OE/LCdGe42+6ollk5hcLV8= Received: from CY5P221CA0019.NAMP221.PROD.OUTLOOK.COM (2603:10b6:930:b::32) by PH0PR12MB7886.namprd12.prod.outlook.com (2603:10b6:510:26e::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11; Wed, 14 Dec 2022 19:49:32 +0000 Received: from CY4PEPF0000C978.namprd02.prod.outlook.com (2603:10b6:930:b:cafe::8e) by CY5P221CA0019.outlook.office365.com (2603:10b6:930:b::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:49: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 CY4PEPF0000C978.mail.protection.outlook.com (10.167.241.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.10 via Frontend Transport; Wed, 14 Dec 2022 19:49: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; Wed, 14 Dec 2022 13:49:31 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 16/64] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction Date: Wed, 14 Dec 2022 13:40:08 -0600 Message-ID: <20221214194056.161492-17-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C978:EE_|PH0PR12MB7886:EE_ X-MS-Office365-Filtering-Correlation-Id: 2f1acf69-b517-4b5a-a134-08dade0c5213 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: qNGZadhHH0CrbDWAAKPvG3KYsvUouIn1eqYuEL9EZOKRMjovK7VzfttHgoBqMor0230tEEqp6OIkeTKj4UHLNzHfaKibQRZC2wGskc6ElNtE2+EClsz3ZXMwI4452yJ2JJCEMa4EtpG2VYlRZ9QbML0Y7FEY8rJe27YqztGZTKWfy6xnxa5q6nCxs34IzCGrZdCYLM6Rmo8Bvcs4gAdHPoI477ib5+ULBI+kHLV7tMkARlDzCZZfeBrJucHojMywZWadrkVlBmNrUMxseaX1LwC7nekPdrlEBNt1i0BRS6cfKn7zg+eAyyWuOo7Mz82jOry6sOZJ+3s+fTp6M/WI+99279bEdmgPstJ2oADZ+n1C0HrdxqjEOt7w92BW9kApS653luPBTNfY+iP6FR25ukzrHsegFHeMw8bNio0x4Vh4Yxrfahg8576IHVGJ4BhV/Ap0m4mH+9ux7pP4NA49mfsutCCl/nkgqLNZXkei20yOniyftqhMiX/2ojr6JEQqQuojISsZVYPdALdLrw6Mg0YUhKyPNjxyOmjitQre4IULLoIpVBsRvrszWWORmUxv6wtRdAA1a5LLc1NEZomtkK74sKpGSfmebBv015OROEip5Ju6MNsuHtuvYOLyjdY89BHZCGf8YHUJU6D3CritevlT/PjyVKqieSazT3pNSDpEZyYiHFn8f0gsRZpGB2NAT55rc2ln7iHSeTrZQ7VLV/e5QJKndkIkl6gr9+AX1m69U1mGV8Ca7ceaZkEMoa6xSKZdjsvbZ0nB3fhkL3SWTA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(39860400002)(136003)(346002)(376002)(396003)(451199015)(36840700001)(40470700004)(46966006)(36860700001)(82310400005)(83380400001)(336012)(1076003)(426003)(86362001)(2616005)(47076005)(40460700003)(40480700001)(36756003)(82740400003)(81166007)(356005)(316002)(54906003)(70206006)(2906002)(70586007)(8676002)(8936002)(4326008)(5660300002)(7416002)(44832011)(7406005)(41300700001)(26005)(186003)(6666004)(6916009)(16526019)(478600001)(36900700001)(309714004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:49:32.3245 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2f1acf69-b517-4b5a-a134-08dade0c5213 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: CY4PEPF0000C978.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB7886 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 292F312000E X-Stat-Signature: 9rx6p5qn9nr1zfo1znb7ympjnqjm59wn X-HE-Tag: 1671047374-237544 X-HE-Meta: U2FsdGVkX1+Ko1sNksh4xYQsThBCNnMvBwIEYD6mFBRNIZVEcQtbeXsJRSPx0FBcKUPKZoExLg763AC9EyfGLwelE9cU4CFx0f4KRP13NwM+zybRmhcGmPkTgSWNqj98W95H3zsVoVR1RGTMiZMSJCWpT4mV+JdHmeIcOrcLABzyC5hB0z6m9AcC+W4NU4Z288jMRWfD6GVF9rzqjgs7Ma97FIwgUQvLh4Xl2DBk93L6kBUifWXDHN3ZfcsHWQ8y825Z9XEUAvYSxq1MJ5MsXDOPOUzuPw7gbrwjHqrRc9IG18Ng9jT1SgoeBzWg1ptv8WBIqmMQMvi7wVQPc+CTrsTzhbtf3+M/fHWGUFOyuxK/PLVsRXT3XtzztEwPgR7SzlyADO3OG1VRDiFmW59TMpCAtMQOq4zn9MTHi3yv0C2M7Wvmfd5bzbmj9jn+eToizcZvQk6Op2jVrjNG/goHazT8fGp4O4QRJF27etVMmaUYHvXQX3rUvcGDn0xkCb9GbIdiX+6prAi0YX309lcz7EJtH00DLcg+aJoeJYYi5c7TCMOGeFEdYt2jS/Uq7HOe6nlX/TenHvsoJ8o9ZvHOBwPnIEYjeVzZTf6CoUjxKC/YsjxuBjtm3bzl5WsjcCeMNNeG+j996xxZPbmUa/CCCMSVoWzT0fcYIxkhs0hZ/Du+ZQwnjyp6EMSUR2mfrq6BYxbDypIc9KR82Arnd8kp1uafFGr3eMCbV2KRPMim9n+sZmL3raQ3lRI4dOyIO0RAgJLJOPJrd8yh23sJocwBdvDZadYCc0BJMSybj/+xnZaFDmzLZHSZH/UOzvijId7BoASRcqMDJpWFDqUm7UVe0qDaSyf6GK+pVGCw+CS9rIPqp25A3Aux5UfKZKaQBdGdiu16+T4Ib8Yq8pWnUpxzUVMF1HUCvXdZTe5hOUlef8M/qs2dV6iqIlrlb0+0Gi0IWdaIy2gNTMFNuLI4UL2 /ZJuTEDP DAisg 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: int psmash(u64 pfn); psmash is used to smash a 2MB aligned page into 4K pages while preserving the Validated bit in the RMP. int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int asid, bool immutable); Used to assign a page to guest using the RMPUPDATE instruction. int rmp_make_shared(u64 pfn, enum pg_level level); 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 | 95 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 8d3ce2ad27da..4eeedcaca593 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); @@ -198,6 +212,9 @@ 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); +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) { } @@ -223,6 +240,13 @@ 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 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 706675561f49..67035d34adad 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -2523,3 +2523,98 @@ 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; + + if (!pfn_valid(pfn)) + return -EINVAL; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return -ENXIO; + + /* Binutils version 2.36 supports the PSMASH mnemonic. */ + asm volatile(".byte 0xF3, 0x0F, 0x01, 0xFF" + : "=a"(ret) + : "a"(paddr) + : "memory", "cc"); + + return ret; +} +EXPORT_SYMBOL_GPL(psmash); + +static int rmpupdate(u64 pfn, struct rmp_state *val) +{ + unsigned long paddr = pfn << PAGE_SHIFT; + int retries = 0; + int ret; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return -ENXIO; + +retry: + /* 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"); + + if (ret) { + if (!retries) { + pr_err("RMPUPDATE failed, ret: %d, pfn: %llx, npages: %d, level: %d, retrying (max: %d)...\n", + ret, pfn, npages, level, 2 * num_present_cpus()); + dump_stack(); + } + retries++; + if (retries < 2 * num_present_cpus()) + goto retry; + } else if (retries > 0) { + pr_err("RMPUPDATE for pfn %llx succeeded after %d retries\n", pfn, retries); + } + + return ret; +} + +/* + * 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; + + 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; + + 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 Wed Dec 14 19:40:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073389 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 84212C4332F for ; Wed, 14 Dec 2022 19:50:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1FBDC8E0006; Wed, 14 Dec 2022 14:50:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1AC378E0002; Wed, 14 Dec 2022 14:50:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0259A8E0006; Wed, 14 Dec 2022 14:49:59 -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 E3F4B8E0002 for ; Wed, 14 Dec 2022 14:49:59 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id B6A59160458 for ; Wed, 14 Dec 2022 19:49:59 +0000 (UTC) X-FDA: 80241952518.11.1DBE2D0 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2077.outbound.protection.outlook.com [40.107.101.77]) by imf18.hostedemail.com (Postfix) with ESMTP id F14C61C0005 for ; Wed, 14 Dec 2022 19:49:56 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=x7m2MRCb; spf=pass (imf18.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.101.77 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=1671047397; 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=jm/QvkaIBQJRPPdHWTGEkZBgJM39n0PSJvku/UCbtsA=; b=fZwozRTuXLVEH95ZQN7g3VL4/FyHzorIUVXL4U1RaZi3126DKOSaXzDCJITpMdeoO/unzn fsmsPe2XTr2M8TPjAHJ0vcmXMfT1JHWATxv60Dc0w2M/cqOw7lc840NEHp6o5GEsZwqzG0 3JRQ04IDKXdG41aG/0YNZckgM0l70cY= ARC-Authentication-Results: i=2; imf18.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=x7m2MRCb; spf=pass (imf18.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.101.77 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=1671047397; a=rsa-sha256; cv=pass; b=mHHH4pPjWtQPZi/Ub94dwWSk3paYRE465TUv99ZQQGDCCHwyIsTeC5QdBzcmEBz7HDfZ5B 7cnKAZe5CcgPkBeLFYZdIWsaWkRbBJhbS0X7L4RogyvWWJaI9L2XK+El+2x/vg5Q25gwOW ZJKki7aE9JMyTjEqY7mHKWD1xK6Sv/Y= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=L/LHjBRAMWE7Lluf8M+RSyU4G3tTIiiKGpHXME25PsJjAxxAfqrptMWwFCEaRWb+lzrOFLCPpibBSCqBBNmfDDXqX1e/ficS/Pa6HdDShKKnRYOwFKKy/wfszeLsIccUDyok0s7wHLKqdjFatI457l4Nxg1xqNTgrPJvxDRonXZwe9vT8czldIWSZRYJJFSeC63f8d3nKKh0ohab+QaSHtCokOWG38kbAxI+BOk39tyCen8OUkuZc747afTv9jthxrBJjIYR7o4ey29obfQPVDR02jCr6SoE7EY6wFdNCmkeToI0Nnp7GLhFytqdUkZ63QDx1iandGcaQ2EZjCWhLw== 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=jm/QvkaIBQJRPPdHWTGEkZBgJM39n0PSJvku/UCbtsA=; b=LXEOcEZG3dDf92gTed/skioinXqZi/uF9Eq7IklSjgF6hYfiKAG8YwEWKqYCuJygDAL9f9PNmZVGagVfH35L32ip9aJuByo8KNMbyn1QfzKM1hb2hB9GECBrjK5jStnnvcZLWjci8mfvwpkPJI5RZl0AmKfVCv31WpmyxPjkrb9pZoJ8aVKFTZ/CdrR7rkhgjLwjKwF8jU/ycge2bnpP1Pflb/noDXgdO2xGEBqZue7PQErlxjVZEfmez4+4jkxrLs8PnHqKOvjU1hJOsH8RpF561wYyFOTwWIwXudJi/AQqa3OgD+Orr4tPmEKCU/r2ffHBEVA2wt9YTbucAOKkCw== 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=jm/QvkaIBQJRPPdHWTGEkZBgJM39n0PSJvku/UCbtsA=; b=x7m2MRCb66MyUjKw6o/WG2MwE2QHXiQ2tWRgBPOvo1A3MuyaoXDA1Dl6yymCpEeGa71RPzsN+3qP93P2cC3Og7GbgaKvEEzNWstWpETClCGyxntCytb7nA0zWOq6k8FSzl7BU8AL1LQbZ8HS5IOoje/Z9xcs5ROu9cAaCgsdSio= Received: from CY5P221CA0019.NAMP221.PROD.OUTLOOK.COM (2603:10b6:930:b::32) by CH2PR12MB4104.namprd12.prod.outlook.com (2603:10b6:610:a4::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.14; Wed, 14 Dec 2022 19:49:53 +0000 Received: from CY4PEPF0000C978.namprd02.prod.outlook.com (2603:10b6:930:b:cafe::c0) by CY5P221CA0019.outlook.office365.com (2603:10b6:930:b::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:49: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 CY4PEPF0000C978.mail.protection.outlook.com (10.167.241.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.10 via Frontend Transport; Wed, 14 Dec 2022 19:49: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; Wed, 14 Dec 2022 13:49:52 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v7 17/64] x86/mm/pat: Introduce set_memory_p Date: Wed, 14 Dec 2022 13:40:09 -0600 Message-ID: <20221214194056.161492-18-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C978:EE_|CH2PR12MB4104:EE_ X-MS-Office365-Filtering-Correlation-Id: ac7eaa51-59c2-46ad-9f35-08dade0c5e8e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: G0jRK5W87/t82xBZlSDAkg18bzwSFG7FYgZLG/T1zrXjVNB+ednlSNn0dR0/u3N4qtpPcORWfFV9QVhbccKnmenjaTxx+y47V1XWhKhsgI6WGlmSWqURYIQ+hqYMAv7YUIE+uS3oLNAl+Zk7+lS7X2lSrgXoge5FNWlaN6xiw2SAqoyJR82AEJo7A7gVGDl0dTza6cfF41kM5a1ulFX4KVt19CT9raMzmG3F2lI/J0GHGRnBRt7B7EkGJi7COm4OoAyiWL7epESO/e6XBB+x+jfjMW5TW1YHxU69NbmjUnS9YtYXg6DbsopyZ7CssBatdWZgYhmK3VAiRpva0wZUshx4UQ0swuksNK9Z5ZN2gD7TcDPZLpvUa03BynV7wP00dyR+aLFAh8pym6DAk36gIt1SRcsfOKUl4gI29n/Ako8YN/o7HLc0lbMLvJlMLK+TL2WDan6ZOFjCRnBpfoTF85jSCW7gmKDqaZXmYVQDWjSVrdEqCIcWnixApKvueIH+8JXUJxkssMX08SwNSRbLvrVzac3kgobMN8vocsRBrNQwsYWAxupTJqiq8nZiBdq0lykn6eIXePTXbVlsSQRGA3x47B1kA0MvzHxjI3mGl70clFiGtTZbl8LF1apiGxSpn/Og1NVE2ELvyTbcPdmpJuBnOOk8oeYTw4EydFjXAB/HMtNxDQ4OTIxeMPbg4MhlbYNJKRy0ss+gVuUsvjrolPOja4pQqfSYVIDzZlerF5o= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(346002)(376002)(39860400002)(136003)(396003)(451199015)(36840700001)(40470700004)(46966006)(2616005)(83380400001)(336012)(41300700001)(8676002)(2906002)(16526019)(1076003)(5660300002)(7416002)(8936002)(7406005)(36860700001)(36756003)(426003)(82310400005)(40460700003)(47076005)(81166007)(44832011)(356005)(82740400003)(40480700001)(6666004)(6916009)(54906003)(70586007)(4326008)(478600001)(316002)(70206006)(26005)(86362001)(186003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:49:53.2475 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ac7eaa51-59c2-46ad-9f35-08dade0c5e8e 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: CY4PEPF0000C978.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4104 X-Stat-Signature: gdh3kg8sftjfwqqyqsjhqs15amm8qi4w X-Rspam-User: X-Rspamd-Queue-Id: F14C61C0005 X-Rspamd-Server: rspam06 X-HE-Tag: 1671047396-46832 X-HE-Meta: U2FsdGVkX192cv91HKBiA4t1rKwa3eg2ho9dIojedTtiQewqdIAc15ii31dF9NWGP0ZqmV9XjzYS8Id+HIKEALfsaSKrB2G/m/hEst446FDNrqPHVIXmAYtOgVJdkeyd1XOBanTOABGqi+53F7GrHIj1g4fAg1rasphH2k0X2gLV8rjotQCcp3yZ/myZuZ3xD4TfGfm3R2ttMT2DBHzMQnR/pkGx2GwY0P10NS9cWy5GP6BbF7w5IgHYTnAMGhdz0Dz3pCQbpXVCTVr+Z4SojZLNeUllFnALat4scPgsWqeJoFVGwL0Pi3ngXJxsYpZGAOreJNAX19QOtTKsV/bEBBY2fhLPXxjVHklS9yyiTJphBSTXBp/FWFxBb/e/Vji6b+TKvxpFxbU4oOICO95AkVuPEaXA6IMCK70dWPb0AYz1oPrH7HmXmfXawZZZQ8udncIh/7jLIDJtjS4ltRvS3Vaiq2VZoVR77F5GH0DzJV2gVXNS4RX3fFN+xG5KmijiIniPvBRAz5PS3Y6q86ti2ItVtpgBpPEdfcw8CAW6nFEb+GnrqAmC+PIu14/RbCUXRQB283VJggkf1aqdnrnOGGIuK2oYrZJZQMvL0RMjgF0dj80+VOOJNk8Q2Xmga/NXjmZD0fe2QgNwV/igkdvfzD/KtNpXtZeCq0bknTn+XDp35Pvw0ip2Zf0+9GvvCMwKPBiHyWawe6EdmBa/hNsBTqAUoqIaR4GjgndPQKcyWZTa6fR5MTtApDlgIA4z3Q7uN7MV8urvtBSpY8rYwQ9TBT1aB9C1HRN5glLbfX0nCW5A8H7d3L4M1e+mUJmqPZX33CEfHSITCTpMCneKrygYMTSwOcOIo7g9ZXdFiNBbEAlC++KTNuOP6jlqtQiHezRp1OU8ip08jOu0FRYATzWyvvx+Tyc6K2aXpjXabFsn2iS8v0ZC6tYoI160+grxwoLdpgUh2Wra6Dr90ubF1Dz DBWuQtXY VyvKfYYfthLLc6dWWYztklNYDCg8o1xWA5ooarIZqj5tPkGP3+YyzbRjoJg== 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 set_memory_p() provides a way to change atributes of a memory range to be marked as present. Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/include/asm/set_memory.h | 3 ++- arch/x86/mm/pat/set_memory.c | 12 ++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/arch/x86/include/asm/set_memory.h b/arch/x86/include/asm/set_memory.h index b45c4d27fd46..56be492eb2d1 100644 --- a/arch/x86/include/asm/set_memory.h +++ b/arch/x86/include/asm/set_memory.h @@ -12,7 +12,7 @@ * Cacheability : UnCached, WriteCombining, WriteThrough, WriteBack * Executability : eXecutable, NoteXecutable * Read/Write : ReadOnly, ReadWrite - * Presence : NotPresent + * Presence : NotPresent, Present * Encryption : Encrypted, Decrypted * * Within a category, the attributes are mutually exclusive. @@ -44,6 +44,7 @@ int set_memory_uc(unsigned long addr, int numpages); int set_memory_wc(unsigned long addr, int numpages); int set_memory_wb(unsigned long addr, int numpages); int set_memory_np(unsigned long addr, int numpages); +int set_memory_p(unsigned long addr, int numpages); int set_memory_4k(unsigned long addr, int numpages); int set_memory_encrypted(unsigned long addr, int numpages); int set_memory_decrypted(unsigned long addr, int numpages); diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index 2e5a045731de..b1f79062c4a5 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -1993,17 +1993,12 @@ int set_mce_nospec(unsigned long pfn) return rc; } -static int set_memory_p(unsigned long *addr, int numpages) -{ - return change_page_attr_set(addr, numpages, __pgprot(_PAGE_PRESENT), 0); -} - /* Restore full speculative operation to the pfn. */ int clear_mce_nospec(unsigned long pfn) { unsigned long addr = (unsigned long) pfn_to_kaddr(pfn); - return set_memory_p(&addr, 1); + return set_memory_p(addr, 1); } EXPORT_SYMBOL_GPL(clear_mce_nospec); #endif /* CONFIG_X86_64 */ @@ -2039,6 +2034,11 @@ int set_memory_np(unsigned long addr, int numpages) return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT), 0); } +int set_memory_p(unsigned long addr, int numpages) +{ + return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_PRESENT), 0); +} + int set_memory_np_noalias(unsigned long addr, int numpages) { int cpa_flags = CPA_NO_CHECK_ALIAS; From patchwork Wed Dec 14 19:40:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073390 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 AEFFCC4332F for ; Wed, 14 Dec 2022 19:50:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4BB608E0007; Wed, 14 Dec 2022 14:50:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 46B148E0002; Wed, 14 Dec 2022 14:50:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2E4778E0007; Wed, 14 Dec 2022 14:50:20 -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 1E52D8E0002 for ; Wed, 14 Dec 2022 14:50:20 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D096F1C67E6 for ; Wed, 14 Dec 2022 19:50:19 +0000 (UTC) X-FDA: 80241953358.12.1B6D39E 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 08EE74000C for ; Wed, 14 Dec 2022 19:50:16 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Jw+D4WFv; 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"); 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=1671047417; 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=xr9WbvJ0VYPoXlLW+mpQJnYb1YLVqrmHGPL/cfLSrWI=; b=wcJlZ3xnCUswp7ntXXHulgtZsvKzSDDzDiYSmDTwGPtD+ief2xHWvl2AfbDJnkq6zR0ZDJ 3MrFt52zZ1n7Wbmwi1OMbR75ZKEQPnmvDtcjuRtvJpGXmqWPUdq1eFsoajnxJz7wNyVWOc bhDe1rdllvGZnLXpFa0DXGdcoKUcBcA= ARC-Authentication-Results: i=2; imf04.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Jw+D4WFv; 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"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1671047417; a=rsa-sha256; cv=pass; b=41VMjx64QXcezwM9+eRtBp+FDQut162v1kxu2bvmk1iKq2mS0gYuC+LzM3f/cKH868aYrn HlISpgXb+fUjKetMex53/m6OhObxNkU3OY6Gua1hHXo06iAi3V3WEOpo/4oY3HAasIWrh+ iDb4RCpjFv2GCKbnP/XMmcITpTlATY0= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YdB1eR9TpCnHdFMks1CqIbzwKpBbSNN2pvdJbkOWQi74ZJxSFo+urEzBJUf+/4KdG/bdYO6D+Pc3pEPgaW3eH2YOFldVUqObQnkdGRRwtkt+hG97dEO/4oMYHUZC1VQ6V+rts1oqjupWjpBURF6peecw7ggIoeiaZxwAyH3E5HS0S7hXcHj6ac3TSnPFVweS9PsxVk81P++xwBzvuw3PWPvo+7GNOTqy8uWSV1q2ZRznjLxkO0nRFVTYbZH0iDBoErr94zV1PwfNGWl5ZWxHV49ylsTZfNWH99J6ad0b6XQEVZgzQAGI8tJ/W0Chj5DGh1mLri2mqkGvBq9kCQjCXQ== 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=xr9WbvJ0VYPoXlLW+mpQJnYb1YLVqrmHGPL/cfLSrWI=; b=JvvUiGuWyRCV/d6m/zLVPdSP2MoRgYoWFhCXWbDuGu/KCE/2coh2q2a4EieAarP5yFX9hrJ2an8PEVQfLDIHyjEFdBj/VfsktZzAEvTPGDD7puYVWgAN+1BI3O1YN03bSHPOT9DSWXvM7qEit6wlEBSF7YXItLrMsuaXwq1Cb+ZH/LlYaKgdZg+lvcesYbu1l+6K95X0Vrnyjbp4N8RCmPnBahbh1vxHl+NrbLb1PxJBwF73/7DxbDCbyFFkA5V+SkmuAi3STbj0dMGaVqsQewnKIkptCvMcQY2rf6tpNYyozkg18Eht/pfHDiAAkq+X/GoSj9RJisBoZbkhhO+V8w== 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=xr9WbvJ0VYPoXlLW+mpQJnYb1YLVqrmHGPL/cfLSrWI=; b=Jw+D4WFv9XAcV3ddRB16D1lc1TmfOuFIkyjAXIjGegAZrEigZhEdv18Q1TiiRG0z8lIjuGtt4saxUfx7cU27AcwUtu+71ma2BMEYa77OzwNWfEqeCcwpoDlj60DCpqTxVudyJ+4M5O/I4ymWby/FIdbS/V4Az6NvsCLAgYotckc= Received: from CY5P221CA0022.NAMP221.PROD.OUTLOOK.COM (2603:10b6:930:b::23) by CO6PR12MB5475.namprd12.prod.outlook.com (2603:10b6:5:354::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11; Wed, 14 Dec 2022 19:50:14 +0000 Received: from CY4PEPF0000C978.namprd02.prod.outlook.com (2603:10b6:930:b:cafe::79) by CY5P221CA0022.outlook.office365.com (2603:10b6:930:b::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:50: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 CY4PEPF0000C978.mail.protection.outlook.com (10.167.241.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.10 via Frontend Transport; Wed, 14 Dec 2022 19:50: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; Wed, 14 Dec 2022 13:50:13 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 18/64] x86/sev: Invalidate pages from the direct map when adding them to the RMP table Date: Wed, 14 Dec 2022 13:40:10 -0600 Message-ID: <20221214194056.161492-19-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C978:EE_|CO6PR12MB5475:EE_ X-MS-Office365-Filtering-Correlation-Id: 3d79f132-52b7-4d84-b876-08dade0c6b04 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: CINFwuHUrf+qXKJFcBtDD1EA7g1DtHRzzXorcqC7/ltyMhn3JjCaJqWXuX4dMCaIPLWeJmtAITv/C26Tft1pokla+mfexCY5IZyiEf1An20JTmpjRwWvwRjCKfhd+ydA35YePOU03kzU9LltPrGDGzjrW3GXPDbC6UbVUjoo45yiO3z93GC4sVpMWqbkyRBvCNctv95D0jp0x8LUNe+qlJ0CV5ee2lk/cvdlTpbzfplm0isRsePWIGLavjJP/qJOBB12sEhiKvFYCJGeDpvwhO8eVdkOEiyxYKULT3+BCK5YmeFrDHSKk8N3cq0VwOL9YSKww6SVz4KtCqoqusLEb0ikEqKgYON1HXq5o2Xae0aJKdhFkX+6eUUxPrmaPp05D3QHyxIcN0l6mFtfb+j5nCtBXTPhqVT5XxHoDwvL4fAH+kwEJgZaQXpKe+5aMP3lO84F32ydsR59hu7UsTeYpuvNPyPYD62l6GUVTx852Z05Cpf4dkd1uF4WxHYfIz5IjXx7y2i4QK+D/eSQoggjb0EGhSoswffYFRB11nZfmY6wQ9Lx24y8mXUGUNrzod7dFgmBICXxxUgMuFdGFSwsUaxzkZXjmVLeX0AlBBKrV1MpamOICjoiMa2hVLZau3xaQXn7P9m5wog+QvPsYfHrkAiv4n9y6qMQjSVuy1K8rTtTYz2ESik1sIZg5LEuy6avL9iK9kQyFkcCBRh95ZnQVTiLWTYOGx4bm0eBGtZ3Gqs= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(39860400002)(136003)(396003)(346002)(376002)(451199015)(46966006)(40470700004)(36840700001)(36756003)(2906002)(7416002)(7406005)(40480700001)(36860700001)(40460700003)(5660300002)(336012)(8936002)(44832011)(86362001)(1076003)(2616005)(81166007)(356005)(426003)(47076005)(82740400003)(83380400001)(6916009)(54906003)(186003)(316002)(16526019)(82310400005)(4326008)(41300700001)(26005)(8676002)(478600001)(70206006)(70586007)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:50:14.1855 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3d79f132-52b7-4d84-b876-08dade0c6b04 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: CY4PEPF0000C978.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CO6PR12MB5475 X-Rspamd-Queue-Id: 08EE74000C X-Stat-Signature: rhiod76gm6sxqrsetnzukgi4zu6n76yd X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1671047416-11817 X-HE-Meta: U2FsdGVkX19N7o3nP/Acl7eI4JmH3B7hjwvLqoirWqHZvxSUXjC3h3VeptW0Y74xnldDFFbRhb8mY9+0OrTkvqcIYxfDXSEe6BIhnKN+098otAQtKXR6DdXHdd4iXNPiBwIrY6q32ubute+SqUJ4G1K3qtLD+TA+xZTGQ0W6DanwhRIfo4tTv2paWCedJrNPItnlENUGEUbi/sKQsUv1b9kKx68k4DEzzj2u1ssOkzZfKyDM+GAc1uilts1vXvvq3gdZCdVg4R+Zy71rJ1ylXVvdiPg+dnj2pOXbqtIOFf7FQU8SoT3/Uvwr/+cB3SsfaBi3qhUhulyGu2zaqGFyPds8N7RpEYzEy6C3UmeoYsXpvBDuGWSwLDQP/FkWt+LTySx8+3UqahqUbr5GwmY7H0OZUrZLwtheiIH/qOdtcq/72rSgsFD/UehH+K3BSSTyRab2vweD8JRxeNocghVTbw6LVouXGp2P+IcxQNjkqwSjhM8utFJLRwcEbxp8NFpD36dA2rWB+anIvg1fIYx3dElXyL/xvoqSyz1FrBfVxN5AwmWch0MloPVFwD/KneCT6aHsvgJQtgu7k2PLUqKINMyII+3aKHHwyzhp986wIXbnJk5OrSSsxlrhuzCSkdZ8Khga3n3IJhukm55E1JdWjhGH2e7nInVyR1rjbFhkS3SSrL1ei7WWsXBrjaUa1n1Q5/otFciaWhlBaNcTKAVTJNVhHnBYUQngP1rhrPPwKrktu63ox8VM11eU2JSlfC1Pwwe2e8ttLm1e4tiqFYpAY8gyrasgWDhMVhLFdCSd4oksJdN3xzO9rYmJBYJxHY8nsXm/xoLadeUC+SKCHj8q3L0llC7G43vDaAlF4rMJF4iO2xtKf7WPmZ8Si8xcDi7rZh5Yhjvv6bPV2S+VSZdFpt2L38RX7spZOi0ILOnaQwS0p7W/jujX0xdS6m4ffCWH6WM+Cuy43H5qDeFPGgD rmniGVZE 3+NgT 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 | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 67035d34adad..e2b38c3551be 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -2549,15 +2549,40 @@ int psmash(u64 pfn) } EXPORT_SYMBOL_GPL(psmash); +static int restore_direct_map(u64 pfn, int npages) +{ + return set_memory_p((unsigned long)pfn_to_kaddr(pfn), npages); +} + +static int invalidate_direct_map(unsigned long pfn, int npages) +{ + return set_memory_np((unsigned long)pfn_to_kaddr(pfn), npages); +} + static int rmpupdate(u64 pfn, struct rmp_state *val) { unsigned long paddr = pfn << PAGE_SHIFT; + int ret, level, npages; int retries = 0; - int ret; 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; + } + } + retry: /* Binutils version 2.36 supports the RMPUPDATE mnemonic. */ asm volatile(".byte 0xF2, 0x0F, 0x01, 0xFE" @@ -2578,6 +2603,17 @@ static int rmpupdate(u64 pfn, struct rmp_state *val) pr_err("RMPUPDATE for pfn %llx succeeded after %d retries\n", pfn, retries); } + /* + * Restore the direct map after the page is removed from the RMP table. + */ + if (!ret && !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 ret; } From patchwork Wed Dec 14 19:40:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073391 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 A0204C4332F for ; Wed, 14 Dec 2022 19:50:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 444F08E0008; Wed, 14 Dec 2022 14:50:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3F55E8E0002; Wed, 14 Dec 2022 14:50:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 270578E0008; Wed, 14 Dec 2022 14:50: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 17E028E0002 for ; Wed, 14 Dec 2022 14:50:41 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id E1FF4406EF for ; Wed, 14 Dec 2022 19:50:40 +0000 (UTC) X-FDA: 80241954240.14.644BE0D Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2053.outbound.protection.outlook.com [40.107.223.53]) by imf30.hostedemail.com (Postfix) with ESMTP id 1290880006 for ; Wed, 14 Dec 2022 19:50:37 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=5OWEKjdS; spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.53 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1671047438; 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=mOkkiMJhY429VlttOvbQWqRRPP2fy+dAh2Krpocr4JdxBhuiu4M2p47gfaPWSdXefkST3a eKi5R/JE9C67rMc9572mRKV2IzAee4n7YkiD2u1+qYzrCKvTXQQJmF7GgcUFKGmNGje17p 3YlFh/71Yw/2Mkev4DFbj0Alhj9+QJI= ARC-Authentication-Results: i=2; imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=5OWEKjdS; spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.53 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1671047438; a=rsa-sha256; cv=pass; b=5Ct8Wa4BogRjgEJQ+fepNBHoWqmyrEEQngDoIyFtNrjcCl3j2Z0djNhI0b7BdqR3XwlDnq BZa5SMBW2Ro07mNd/w6BVkDVumSbaYcmS5COCWEKsV1DCVCfgMPyHuSkZmcNFbi6GOj6RN Y+yyLhM+woBVDLU06CZbAiL5q6S9pmo= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KEMR9k3bWggy3zLcG+Uxs5Qi2IQxSxsmOQTspnew0tuLFktWmeLHqHrItoFri44FLPkjIfcREk5E0rcmcaZQ2OpI67Ox5ZQkQs0ZnUalATHSLNUB0ESUN6HvH7oeIHdRr+TDKns4aMDxSA03eZiMRhfD2TiMFLFHiMJCeJo1CYNR2lL1reQDKVY49U1t+wOP6QaSJ6d4zluEinYDEDT847wGKrYg/TBXveABSXCZtzMpyydUV4LqbhLbSPINGMa8Uco4LgLkoPVYVSBP3RezcCmIVcN6wJ29vmHTauEgOrgbbFevcCzv52j83qIYaAuGtQVW70E6q0yvtwxitZrN/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=LaE99ZyE9fPNXP1+sCNkKPB9OJSZnnK2iy4k/8TUgSg=; b=WFRYR4lGWdXP/K5hoVSmr+hzDGmtiQ8pZ0BFY2QXmSWrXrVzctTEQXHoKlK4B72QiEz0JBSU8Qh2jUNuC4+bhNq2KkU2ula+zPn9MIgP2RAbZ/gnLxFaEcz2Tw9SLPEChtu4/9pmRZDzVrfatz91C4J+g7opb7lToA238/AKwSosYkjUmzyDUV65Mi9iLBaG6t7alGLD0MgNIwNL+AdnMhO7taw9ID7ZKF/Uv7dNVDbAsD01/7sZcjd/pQ78DC+JtiWi0XJ8rh7phqP3ZuZOddMIqPhs/Lt5IYUZ96pPWlZuzhDy+4Es3rVPGTUgG0jnrv+DVJ8PEjW6o0fefqTx4w== 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=5OWEKjdS/EQKZbw3YVCW7utdhryuOCmQZOIWxDpD3nkPNELnih/L9umKdzQbgoCkktJQGSLCm2GlPsv10ciChl9uKqu0CwnEE55RVPyn7DgJCCdsQhClvB54BiWo0wKVkrCZnXgHs2ES1iFU2zLyCwQzaZrevVZz4vLWTDmQUik= Received: from CY5P221CA0085.NAMP221.PROD.OUTLOOK.COM (2603:10b6:930:9::41) by CH3PR12MB7739.namprd12.prod.outlook.com (2603:10b6:610:151::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11; Wed, 14 Dec 2022 19:50:35 +0000 Received: from CY4PEPF0000C97B.namprd02.prod.outlook.com (2603:10b6:930:9:cafe::8e) by CY5P221CA0085.outlook.office365.com (2603:10b6:930:9::41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:50: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 CY4PEPF0000C97B.mail.protection.outlook.com (10.167.241.134) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.9 via Frontend Transport; Wed, 14 Dec 2022 19:50: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; Wed, 14 Dec 2022 13:50:34 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 19/64] x86/traps: Define RMP violation #PF error code Date: Wed, 14 Dec 2022 13:40:11 -0600 Message-ID: <20221214194056.161492-20-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C97B:EE_|CH3PR12MB7739:EE_ X-MS-Office365-Filtering-Correlation-Id: 07312df0-0df1-4361-20a1-08dade0c77a3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: OJTUvFSfCY3nCSvwEHoQlZesbGpDSU6DqU9Llr5VhC4XQQrp0usk+9d3z1ic4mLhufQVVrbqr8e+XJxtLIRAbaXOyLxQEG2mYqLWeM8P9ph+iuHXeOZHHeFr6nvl+DaRvIEqSecAZBvI8bLIHjvunFSbL1CkcSg3SpYSIKIBrPIxrM2TS30i/ICum6JcqcwzhiTR73iNg71xR2FMyZ4aHjOKAWkdrlV0fh7saCuS/aJls1HjTZ4QafKcN8qmKGYmQcwXj2W2IK2CAn1y/s89ijUqcZTS2khdsn5VlQRzl7ZFg1A/ZbKrOx1ISylPx/+FfypmGyfRDKUOlUTeK/AwIi5PWUXQ6gRFdgFrolZbJDxbNSmOLFoM2BmMctaZizbF8Vz6ha9LouVLV1EC6g1wFW712fgT2sR8sfjYVLQHNGItzvk5MQR+t5JqkNdSqaNPP+mHNNXclMibOKU5hUgzhU4dspMG05Gf37r7JmHSIZzVi22hz1toAxWx7mT2tU360edSNgg0L18FoN3ueXexu6hskk3r0k6BDzCyWczgEvYB0adgSc5OUuzu7x2hd0i0fXct2ShI0yvWgX5TXAWEu81Lfdtw4jrlR0j6vIPVyXZk6pp3JSNVdbcOvgQ3Tt00D3OXjvZbd4gwtj4QXGsHA/ZhYMLmodV1vPpwN58O3FbNLNL6rGCI/RLrjwavnD7Si1M/O0dVel4iN2n3s58W1a9Wih7mtGlRqxycWWQj6FY= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(396003)(39860400002)(346002)(136003)(376002)(451199015)(40470700004)(36840700001)(46966006)(316002)(54906003)(6916009)(70206006)(4326008)(1076003)(2616005)(6666004)(16526019)(478600001)(186003)(70586007)(8676002)(26005)(8936002)(41300700001)(336012)(426003)(47076005)(83380400001)(5660300002)(7416002)(44832011)(7406005)(2906002)(36756003)(36860700001)(82740400003)(40480700001)(81166007)(356005)(86362001)(40460700003)(82310400005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:50:35.2781 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 07312df0-0df1-4361-20a1-08dade0c77a3 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: CY4PEPF0000C97B.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB7739 X-Rspamd-Queue-Id: 1290880006 X-Stat-Signature: 436i4y4zckibp75gc5m34861u5hxz7xy X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1671047437-427967 X-HE-Meta: U2FsdGVkX18mpEv/E+lPkKntgi5JallQbm1bagXkqaQdHDFwveeV3USELPf2Ht+V2n6w2X7jI+GPwgQp5maO1SUHQOrZYo4r9JBwGcMNSwS+2k2vWJGQDd5KU5/YMe9YrfH10Cz19REl1ef8SWLjbPuA36eE75ua6FQkBQyvP9YPr1PBdM/LTw/BF49Ad021XpwaEIuF3/O3GhOnjloE0e0pCxu0B0m4xRRFhWgUBeuMOLljuQXOjfrwiB8Mnvyb/HC58oPZYcXgC1KqhUcSjh8UFwGBnXJrIzZgvhh1jnuI7CHevTbr2RBDGdgTzpUGVHMxKHfAPODq2DvL3R9xg6UEJSIkbWLKo8n/YP9DZNoGhsqlwRtL/zf2PPl31oR5CETubrdb2P07w5KwnrZJ/68f4yKS1nDfFfFYEa+LyrwZLEG59m99EXWV6xS4net5NC3trGg6QiHBVhcLxGN/tci7UsaY2rxQBltJn0IZ+KrpTglCt0X1tIG2190suog9CFcke1UIWq63hLkClgnfMry0oAd9axbe2vEYC8a1g/+Df5b94Hvkn8CKxwbzrQa8bEHClegSVHENzWaZ+6y8TvEjT6DhKzxiY7pxdWWj2dEuUiyICH9bk+tjvCcgYA1DuJx2GlOi5JkwqSKsaD+q1OIPnQPiyvwAr4JuHTQirT1KwFI81Kh10BtX357k87Lq+yLAVv2y2yOoteL42rr/7Y3tyfcSJD0EpCJbLBD2FaxWEwnopJX4HP72c7HMSDP4ANN9SJgeT5EMfii7nwWujvkKNtcgTHx19podJtRQNvNjHrtGrO76/D7NWXIaoFzCaA85HFDFBO1jrqm3irN8QK8p9VQLI3ldZ4CEpNTmgh8w+Aydl/YNnB4pjszoSRJM3gMM8XQ475l3iypU6rk7b5uEUI0aq3p3gAYnUW32r49i9lUMfqCjaw9lFrQhBYTpw0zH7mQMIMwabnF9mpe RoceD6cT WWgkz 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 Wed Dec 14 19:40:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073392 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 06AF0C001B2 for ; Wed, 14 Dec 2022 19:51:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 99BA58E0005; Wed, 14 Dec 2022 14:51:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 94C548E0002; Wed, 14 Dec 2022 14:51:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7C5BD8E0005; Wed, 14 Dec 2022 14:51:03 -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 6D4018E0002 for ; Wed, 14 Dec 2022 14:51:03 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 49817C0F5E for ; Wed, 14 Dec 2022 19:51:03 +0000 (UTC) X-FDA: 80241955206.15.87DFDD5 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2067.outbound.protection.outlook.com [40.107.94.67]) by imf30.hostedemail.com (Postfix) with ESMTP id 69BAE8000F for ; Wed, 14 Dec 2022 19:51:00 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=4s8MVY5P; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.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=1671047460; 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=9LtoKmzOFERPW2EbZIkR/ImCBYp760e0sF60z6DPADA=; b=lgCJHX+DWN+uMXG70vPsY44K1gshiilkv0SKmcDwbYqXavGdjV0zYTsNemIR7cqy4JbmmR +HWaeraWPYOJGZjRut5b3054HE02xrWwNd0tQwPtuA7E32iTkUnHEqCQYHc0qHx/x3VMoA tTbEku8oXolt5Gp1s3S9yUieGubdYRM= ARC-Authentication-Results: i=2; imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=4s8MVY5P; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.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=1671047460; a=rsa-sha256; cv=pass; b=2i387lfHn0q7PHykcHulnUG5dJXlZBRX2gqSck7wJBM12dRNtsK4vBHjpNPxCylE2o0bnv K+EfDlqhm3t7tFTr8lHjDmhu3X6YrgsCALffAsWuRa1Fs4i7tLQ/I1DLM7T2a9BbSVMMKT PQGI91rxMJM4lItJDSaG6G5G7icdOSQ= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fb8b8FqMgguh/APREXyxGsv3Ha2kDAHaAqkMnuiEcAFj7oUjeOrO3YbGndkqQcoEMr8F/vvSs3sEWL1VSMQnJB3404ABxVZV2N5PnqfhorQT+ZqX7kf5uQ+ppwfNqFU9Wweq6oZuGKgMwFjVheceL92PlrpTBhddbbAlwEgBw4/6o4irg0HJNphtyuN+7HHxZPmqloSPxZJugdXaWod6JVoKml7Hh22AqU8DAv0d2VSZhEGrwK6X/rbcJudoyUEMbxUbxqGpCu5TWwEo2/WTD+CL3se0C5Eyne6sKdolH1QxOxIFNS5KpMx74A1zzIqp+GIglCRWubkeOVRD+VbMgg== 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=9LtoKmzOFERPW2EbZIkR/ImCBYp760e0sF60z6DPADA=; b=HDvtZYwD11/sBpIcyk+dNNmPNeCVk0GNdRmlmsSxCHduRi6FyUX+vQZc7dXvTQA6YZ9wcKKGYhA5vxJQdF9v9GNbqbajFDj5BbsnbucGKtprlIFBM6xqhpBzwjo045SGUMCyykOb960WA66WxIYSdh8ks4Uly031VPK7qBhysfuH/05iV+cW7IXw31Ni4FeOv/ikvxHgLcmLXBbqxhpatY0pcCm1iUgVJ2XAYxuihnd38P/pVytyVDy2An64B2GvEUIukm1LNIH9mZ5zIkAzfghTBs4XnOkwXHCVxLvzxU0P+R0tqfuIafQDdeh9eHrP40Ei4JBKQukXzMZvzIXJ4A== 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=9LtoKmzOFERPW2EbZIkR/ImCBYp760e0sF60z6DPADA=; b=4s8MVY5PZNNhw/WTUWK8AW3gFucs+OdDGORBRv9DSFliGhJg7bY2F5LWFLRSi9VxU/jX2fHLoRTW5wqa9xrK65qr+Ectoo4VQrpoZcRmrQVXMcwlLkH2HDuMgOtOXvjI0TAAIm//WZY0E1h07InZ01+cjexZMXHYY99DiPIZW6s= Received: from CY5P221CA0001.NAMP221.PROD.OUTLOOK.COM (2603:10b6:930:b::20) by PH8PR12MB7447.namprd12.prod.outlook.com (2603:10b6:510:215::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11; Wed, 14 Dec 2022 19:50:56 +0000 Received: from CY4PEPF0000C978.namprd02.prod.outlook.com (2603:10b6:930:b:cafe::fe) by CY5P221CA0001.outlook.office365.com (2603:10b6:930:b::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:50: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 CY4PEPF0000C978.mail.protection.outlook.com (10.167.241.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.10 via Frontend Transport; Wed, 14 Dec 2022 19:50: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; Wed, 14 Dec 2022 13:50:55 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Jarkko Sakkinen Subject: [PATCH RFC v7 20/64] x86/fault: Add support to handle the RMP fault for user address Date: Wed, 14 Dec 2022 13:40:12 -0600 Message-ID: <20221214194056.161492-21-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C978:EE_|PH8PR12MB7447:EE_ X-MS-Office365-Filtering-Correlation-Id: 12742c80-aadb-48ab-e919-08dade0c840f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: jeY4V549PtE4lpiCqMR4/eq5M9IKng/LFBEM5sRL0sT7zBeL2xx1BhGhSB+CRtqAWNqiiUt+AK3HtVm1jaq+vUFRG0RG4NnASKcV99uxq27z+CLk37x22Vh/5b0Mzch3PZ8tIFkDypJUx81Hla6/PXHMJAe/VgTAjoyYB3WmyKPWmsyxta64I35E293+ErEVD1VGZR4amwN2IEQXcbPpsDLsC9KXHmCDw1cnKXeOP1oS2FV7W1oEHz3YbOWz5y8ZB6sDj6oyrT6TcolrLov4L42258ux91C3iQfoXONZ/vAI8p7uv2t3iKxojNx6hqzPIX3iDuPPdl6BiJCZ7TTe05ifyAKhwkv7VVZRIkjXrIFYU6fSjfZBHMcHYSGkKN8igeiO6mgW0TboqZd8b40OxTOpftYOZ/F3mf1NSxohO6a4WGjhtUBO5YDUdxDSiAfjeUh27wW0J1C5UBup8aVSB5BmkA+qduV9PykjMRqNU6UkeQM/ab/GjyKywAtlFMfv0fMVFscTM4zAXwG2A8w7brXJTJs0Wn+Q44kgQlDPa+zyg4Ii1RI5AXl+OAie8TdAUQS3T2XSGGMAW3N1jOGb+1AaLoinmE0o19ZYW2ea+HxB1Xk4PggMBM3gvoVYR3xWlzDSoW+QRN74+agimW5X1RCvPeQdvmHQZ+PVZ0asoYN8qFy+j94PtOqDK73WZi1L/g8S1JGUddMqYqS4kiev8Sj//HwBhwYho/ywHo31Eug= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(396003)(39860400002)(346002)(376002)(136003)(451199015)(40470700004)(36840700001)(46966006)(86362001)(82310400005)(478600001)(6666004)(16526019)(81166007)(356005)(186003)(82740400003)(26005)(8936002)(336012)(2906002)(1076003)(41300700001)(54906003)(6916009)(316002)(83380400001)(40460700003)(44832011)(47076005)(426003)(36756003)(7416002)(7406005)(2616005)(40480700001)(70206006)(70586007)(4326008)(8676002)(36860700001)(5660300002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:50:56.1861 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 12742c80-aadb-48ab-e919-08dade0c840f 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: CY4PEPF0000C978.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB7447 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 69BAE8000F X-Stat-Signature: fso6nrf48nttir6hbst963mg357kxiwz X-HE-Tag: 1671047460-996642 X-HE-Meta: U2FsdGVkX18a2o1DFasaoXjuB3O2RZWWh1IQ5S89nSXRiVpc0xB7ETen03bplQHAvdTHAu+xLahC//pKpZiM9x2xv4aaNffTj+tSQ+DcvtT/a51TAgyPmP4hxnblAU0ipQAGfcZQ+3Jrj7uo8FJB9gka1Gy56jtdgQPyplPl/LFbzFzJPlpvXdaUsFvZjTPhP9K2OPaf7Vc8nFk0Ufgn9PWToofU3xOdvl14FGwCuqVzG3SYcWRcoZt0vB9RazEgEMGNe1LDaze0OIpzQbmPuM1hCHDr+Mlqrd8OAY4RjGn5vOwi1I7ZIWyl5fv4u3/U0Yz0+bVN48CnKsC5+bM8Be+/pz8NfEKcnElUQkuz9tiQuKPs7MOtfHw9G6tQ6KPaY+e0HUItPkQjnS4kJPJm02hqNqIUezWXVh4IOpmw2ZoPxkjz+II2dRdMOqbK+Vxn/CDMWr0tS+CP9GnqWro4RVG04I2Sp5zShgtVNomdXwLoXwOry/yOgFu2KwA6fsxp+LFr7OCljn6Xj2+JJn0vy3Janp5uPPAG83gpjTrsrMRO65nT6gdzcIJgd/3JZ5Fhcnl6P499cemFuxi+d0lD22BoG5MVGYN6wsZDPz6wl7q9qgmNEyFnROhOdzLwBbyCjpHgm7+C0JAzqBy0TLminpelg7CLT/ZOfz0hOPfEbXeLEjZQHmFf2Lypc9U8qA/c4L8Z3I2HDV/0OhwUDLELACC5EGSdgQzZiYU/mtYSCgPrtqm5ilzyo/gdmG7q4d3L/R4X0HwC3pkGwf4j70l9IEd7EjA4BlyiJGZg+HRkstm9pnHF3nVMG+4WgLRz0XLomkCK48Iuqi2LjyfRF2HU0VlP/u1Rh597/sROJqZaCCHjMrZtHWjNPtviLwAXU3NY7LqviENtsxDHUW/qHdmmBNN0PILkUBWxc3ldc83sG9IhWb5KsuqfRwZgEz13cAKJeojAu4as3y5Lyxkxsh0 eSYl6A8h z8Zp6 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 | 97 ++++++++++++++++++++++++++++++++++++++++ include/linux/mm.h | 3 +- include/linux/mm_types.h | 3 ++ mm/memory.c | 10 +++++ 4 files changed, 112 insertions(+), 1 deletion(-) diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index f8193b99e9c8..d611051dcf1e 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"); @@ -1240,6 +1242,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 +1423,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 Wed Dec 14 19:40:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073394 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 31C01C4332F for ; Wed, 14 Dec 2022 19:51:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CA8EF8E0008; Wed, 14 Dec 2022 14:51:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C7FE08E0002; Wed, 14 Dec 2022 14:51:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B21168E0008; Wed, 14 Dec 2022 14:51:44 -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 A194D8E0002 for ; Wed, 14 Dec 2022 14:51:44 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 6B0FA1C5BFE for ; Wed, 14 Dec 2022 19:51:44 +0000 (UTC) X-FDA: 80241956928.23.1B0B2B9 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2061.outbound.protection.outlook.com [40.107.94.61]) by imf28.hostedemail.com (Postfix) with ESMTP id 730D9C0004 for ; Wed, 14 Dec 2022 19:51:41 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=AbT63HwZ; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf28.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.61 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=1671047501; 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=MKYaly9eYd7mxPsrdxpN5xtIw5VZXBt1VqUaXZ5EYDKHvaFB9SQKjBeZ2MGgrehIKTBBQm O1Wo3QiUdyhI1R35YEXOGYwjBQFEyUY3ONUUvQ0fhRt+pRGm4iaPCscSALC6YCIl2oNc+K a6qqcH7ubpPhyyE8tfri3oc50ZgQfOU= ARC-Authentication-Results: i=2; imf28.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=AbT63HwZ; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf28.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.61 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=1671047501; a=rsa-sha256; cv=pass; b=0x6l4zlhELT/Vo5NUUdyJTt0dyjHGgEO46CsA/5K3qPH8bWhf2YqrnigLbegya06O6PQiu TsKshhz8W+iaMeWgNKEgqwpgDGBUcFLjHAQyoM2kUr7b5qWY4/+f8cJMpWW3CllY/S2b5g V+XFidAq/VXJYfUvctVGsxvDl65/cWU= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UchcarU+H9/ZmcAR7+LfGzKny1oSjj9QUSWLi1NUtx1zvF1yeklFEfBS7M8yR4cMFZJg4Xsi1JYAuSK9jD57bAMYVOjRsh2/xLEtGqrE+57ChXKNaK0hEdR0Js79x1b9fccjMHg+FeRGiElcXF7zBrqcwPo0y+ap/bXz4SU8I1Zba2YHIy7qte304S/U2djgwyajEASxwNDBZc8l2zgyY7SRe0Ya0k15nnJ8S18BCqMQ4d/qp0iqZgTbLYJ/YPo40xsw7Jd2JemkROjag6WtRkrS8sxTMf4hqGb2nFWQyEXbe9sz3GrD+pv5ijQjrfiktgJkU22TRbwhyQ8Nz2AQDA== 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=GqDHuGh/jhffD0ZveL0Q9gXFuARcIh8GmJLg3AQIJFJCvOxifOOg6it6Ksc6HIG9HLKOB/ChPLC8TjeVGaSeTg2+bM87CjyeCDNyFm5b9NFatOFLBxM96JgbHSPDBV5nkZeO8X6xUEgIBp9wMwBvAxXtAKmhKzDr43cOChejZnVutbzd7MSdmXaRJ2sU41cA9coWq74mSLUXvJE6n6a8v7dQ9LILggs0QyVNwOXLVOss58J1Xeh/xIaHoJYdMWE6OpklGN9XumEYAJF3QvyC04qaZk0JyC1HdKojQ/EeiH9HMd/FsqSUibkkhSkYhEOmljsCdewQhXdd+M7SUMw/OQ== 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=AbT63HwZtpfHA/VWHqJswLGecO43bGaBgxCZroLGMFmAgy18tDD2LUqVt1HsKrdHIH97ewUv6HNZe9nr5fKMUKQZaBHUeFFupdDJEaG3bQviij3AxYaMU8mR//b0iVYk5swkqJm5teeRCIrE2UBouR78Pa2YYTK93DKVrBltKA0= Received: from CY5P221CA0004.NAMP221.PROD.OUTLOOK.COM (2603:10b6:930:b::12) by DM4PR12MB5056.namprd12.prod.outlook.com (2603:10b6:5:38b::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:51:38 +0000 Received: from CY4PEPF0000C978.namprd02.prod.outlook.com (2603:10b6:930:b:cafe::4f) by CY5P221CA0004.outlook.office365.com (2603:10b6:930:b::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:51: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 CY4PEPF0000C978.mail.protection.outlook.com (10.167.241.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.10 via Frontend Transport; Wed, 14 Dec 2022 19:51: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; Wed, 14 Dec 2022 13:51:37 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Hugh Dickins Subject: [PATCH RFC v7 21/64] x86/fault: fix handle_split_page_fault() to work with memfd backed pages Date: Wed, 14 Dec 2022 13:40:13 -0600 Message-ID: <20221214194056.161492-22-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C978:EE_|DM4PR12MB5056:EE_ X-MS-Office365-Filtering-Correlation-Id: a4bde643-db12-4646-ca78-08dade0c9d2b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: mFaTg346g8lcyFMW1cwlnoV+jQvmzSLRzmnUOb12LkhCFmmFRY9JlREPKL+VtpbgS7hvHwfechiK87qJMGnQHvGOvIM3vn+XIW+zztu8OHkm7eqgucuv3d3RdmY0Mag3WueRkWw2FAH0UrrlNDF47GsL52WI3AuQc6b5U9eSOFmMw4QtY62c2AbIZfeKzJPu3PFevA4+vK+H8IZDXqxLRcAnVuNrYBJFEcPISA1FYcmnfw4hCgzfY38ErWwh2ipsrDwqE+CNTNYJEHTgDMDslv2PayJmhZuLhVHMOKqViu5oIYXo6w4ekXuD/CpIyKItBpV4u7vNALAUMapHth5Jg/+5mmltYO3SFbmRs7fqfh8axDHGH5O5UnmE2KyjL79RCz4h+3qIT+HiCi2uW23VXayxBC6bfRouM9aHFLSL1FtSYaN0IM2coJYIJieriSk7vce99+xAvN+5gTr+/Pj3cQFmAedbJxuCZjWbNRpF6oe1g1vjqH3FUn/fCjpJsiR/xhIGa/tdgDfLGyWAiAV8FrJK1kcCRX7jDNG6fp4NzfaXt0P5vK39sN/O8sl8hUTX7sljQKR+RlQn4Kd91CWWGgijk+mVJXjg8+znJwQ+o5RB2P1lkoyqudulVSxw0fpKlKZejD4OTT6qO3/odlK6+3dD4oYkCIK/bJD/ZJsIEJJxZxPQX3wTYBEhp9OX/JT6BTnGtpzOExmrgqtlzbHH7e9VYsB43XG0RFVtnumqnV8= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(39860400002)(346002)(396003)(376002)(136003)(451199015)(40470700004)(36840700001)(46966006)(7406005)(8936002)(41300700001)(4326008)(5660300002)(70586007)(44832011)(70206006)(8676002)(7416002)(36756003)(16526019)(1076003)(478600001)(54906003)(6916009)(2906002)(316002)(40460700003)(186003)(336012)(86362001)(2616005)(6666004)(426003)(26005)(40480700001)(83380400001)(82740400003)(81166007)(47076005)(356005)(82310400005)(36860700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:51:38.2806 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a4bde643-db12-4646-ca78-08dade0c9d2b 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: CY4PEPF0000C978.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5056 X-Rspamd-Queue-Id: 730D9C0004 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: pbwqe6heto5xdq7z5un1mwg4351bh7sf X-HE-Tag: 1671047501-760138 X-HE-Meta: U2FsdGVkX1+GQiHN2vPDIvPTywDHaCJe+GcsBcUXyYRiOnGb43JhB3TUKgE+5nCBaMcB+RWj7FbQSqgfLTGQfOGn0hPcHm+h/+9ny+Vfqj9f7F8+JbJMdVCSh5zXXZSMJjb/CfMqTPTF6Y2Oo7Ox7gmDi+1tf9t4+hhoN2c1s5HwSatU2YJ9Ybsbmc45NSKkAQ4qClPgWBHPdE0SphUfjpkIxoEBsxmf9Twq2KMc87QAXBA3F54OsV8M9w/e3qUarM07mVavvKJdE+adlXWrWjxOBBC+SmsAJ7odfTkEDU5NfWCa4GpHcM9sYXYHd5FzIEKn3x1nW4ZgIpMBoe08lTU5JKAfroQCZA3/2NwYyYY7yh1roIh2tCtca3lWYqwKOERpcnQFrYIocpS2v+nwAnj1Im78PUXdWtPUDl72uAqrpirLfipYN1V5inezx9HIcw897oZYY36HGcCFe/fPkq9VVkxGtAmVjn6yAGWuYOSF+icGvLeLNwchZU9a7cqz7oWWCG9/TDsIPCLWTSCNjiLSFltDXrHNyrN6F/8UeZuoavxK9aHC1NBmTFA7ucCMJwdK5mEQZxmq5Vd6oyKtR8Fn6DZ3HTxLAQCsYqxwKMxOsXRSYfA+LltVUIpL68tNLtkx1JqzfAAElt2/mn1iDjflbx2J5yR47nB8rREjI+aA2XpqEnHbMhPk/s21wzW0ZkJpmO7cKqUjw3AUmC3UL6yScVuh03jsPPOWn62xswfSEvmzlJoVjm/muQ9Vwp9akPjJf+thipnBKhhX7qicrS0GXiE5a+27CBDEWuqy4WhVeC0NvaI90PK3A/LpAl3l2nKWGFH08rbQkJ1XJ9IRnAr9I9Z1Mcy0YTCJ44B1HFI2l8vyefkBSzgJe/+2m5LoRrgxkEXDhSLil2sVhszoGSw+A7AiKcHpFMWJhjxhiwXygPnEtEZMwgukueZU6TicS4SpJFgsC2UkthEVWDb sFGs44nz t8J2m3vIGXCydilR/o4nvOPG6CYyA9A5gcFF/8CSiWRce6+CZFbxK8C1Xng== 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 Wed Dec 14 19:40:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073395 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 5B9B8C4332F for ; Wed, 14 Dec 2022 19:52:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ECB168E0005; Wed, 14 Dec 2022 14:52:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E7B348E0002; Wed, 14 Dec 2022 14:52:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D1BF48E0005; Wed, 14 Dec 2022 14:52:12 -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 C0B788E0002 for ; Wed, 14 Dec 2022 14:52:12 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 95EEBA0F9C for ; Wed, 14 Dec 2022 19:52:12 +0000 (UTC) X-FDA: 80241958104.17.1F436FF Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2069.outbound.protection.outlook.com [40.107.244.69]) by imf28.hostedemail.com (Postfix) with ESMTP id BDB0DC001A for ; Wed, 14 Dec 2022 19:52:09 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=bfc2JPN9; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf28.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.69 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=1671047529; 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=ehh3zB8AkT3HR+8GiQG3Qs2aejsevicxPUxFJxZv7mk=; b=3cSrPnnNeAiyss7FCbcUQK39okvZz5ChlgeRS7uPIS3zp1MTbrKRIFa7L2sJU3KzNIl4kR Iyzk1tmJTmJkML+x5c3Dquwh5taqwTmhDIqWFyjIP7vMDVR2QOSsajeWHOUgYLSML/gCmZ 4Z4QZY7m+MpUXWjWSzKgAGEeFswwvjk= ARC-Authentication-Results: i=2; imf28.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=bfc2JPN9; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf28.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.69 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=1671047529; a=rsa-sha256; cv=pass; b=d4pW/CmLW7IcgVfRZ25bKsGdL75NPYzvbInrcZsfOZ4mBuMU0ZE5gW8g0zxa4gsGeMhd/F NwbBBFVKHbUsyqM0RWV8dLb8fD97wmcLTZqFxWT10Q9Jzg6XKDisZFzWya2oooAyvzr5E6 qAPjPUX1bZJGL3xUAJhStDmBZ28A0Jw= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LSoX6i0K+8TvDyeDCIU+eauBLa3w5W7GITVE+MH1cOcG8TQkVT1G/sTjyFxHkvn+7DQKbfZjQVmo39kiIBZV8Kw2KowIN2DmT2DKGOt9aCf7vMJsU4mAf0i4Y18lsbcoyQzs3ce7ZJ2sBcZTT5j3/KhqyPBsCY/Ofd0v19SirzyjXYbaS1YFBuSUoojFK6VGrmn/xY+p5SYL9geikWiVIuuPz2ymEmemXyX+HZVkJhiNGgvLMyMyDCT6nWcXmY5d+Pc39uKGERnIrgVOCun9VhtAk5rod/PqBBSEQDBdnEdoyiCQNEggQI6PFIkdWyYl5qqKRkEC217FDTFeJ5CZuw== 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=ehh3zB8AkT3HR+8GiQG3Qs2aejsevicxPUxFJxZv7mk=; b=WxTUCK05snWn5ISW+9YqecRp/3xoMJQrZMSrxlZNmbSkne4eSYDDEsOvmDyrbl2PeI+spy5vpTpIXKbjKwV5DCH/bo+faFwGJw6SFMZywEhhfXLrg7XKQcVKyLYjTm3EFY+R42gi4F0Kaj0OX3TlZy6AS37zUEdqj+IyT1XqRDfo9sgkeV38V8KND6FEZ6ZChcTEJjZtm0udqlugzy6tyava2mUCcC6lWJw3P/N+uNGdawWGsNDOMsT2+jhHuCUNIONndFqdtPS6hU3L7T4EcBB+eRtGX1wVhysd61miNsmUjJkDZEV06fXPyd+wQVYpAes5TGitB/4Ndw8Kg+t1RA== 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=ehh3zB8AkT3HR+8GiQG3Qs2aejsevicxPUxFJxZv7mk=; b=bfc2JPN9gP3mSTzNqzMYj4ip9rOxntwTL+pmj+X9MFLIn6DFx7HvGP4wJ1LWclh1h5fjg002rxdIyXqwHVPVrBW9n8UocRGIJdKsSWDwxQ0vYjOeQIDHWi/CtlEfTIIy0IlwY2purrm/UeWCJTbQt+ub3mpWetTdSc3W2wyl5Ng= Received: from CY5PR18CA0041.namprd18.prod.outlook.com (2603:10b6:930:13::19) by PH0PR12MB7864.namprd12.prod.outlook.com (2603:10b6:510:26c::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11; Wed, 14 Dec 2022 19:52:00 +0000 Received: from CY4PEPF0000C979.namprd02.prod.outlook.com (2603:10b6:930:13:cafe::85) by CY5PR18CA0041.outlook.office365.com (2603:10b6:930:13::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:51: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 CY4PEPF0000C979.mail.protection.outlook.com (10.167.241.137) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.10 via Frontend Transport; Wed, 14 Dec 2022 19:51: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; Wed, 14 Dec 2022 13:51:58 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v7 22/64] x86/fault: Return pfn from dump_pagetable() for SEV-specific fault handling. Date: Wed, 14 Dec 2022 13:40:14 -0600 Message-ID: <20221214194056.161492-23-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C979:EE_|PH0PR12MB7864:EE_ X-MS-Office365-Filtering-Correlation-Id: c4c126d3-a80a-4373-8dde-08dade0ca9db X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: KVjYk4w/HnfWbVhfEPsdQS8zDAZhPUCkgVLygV6jjgjm22Qb1ZMH4LPC03cNp0YMQ8acCe+1vrZ6K/QOgFAWsDLWO3G1V7mDV+Ais7OoiJt2urNLPJrHNMVafJllpcDHVoimWqEduOO9DAb1dJ1ksziCRFG3l86KEydDBnv9PCDt8a3uhpC0iKvqva8mSgEhaXL8zzaGnRg41iHa7JqNrSuBAaHY9FZW3yQrhnBoT191UCFxXyXiJ2bt6OahQEl2arCj3aYFIw/sUj4RLOOe10pXSBviNwK13MhhakDQWDBRpu8OHDkOYtQQpdZt/I44mv+ReI1Ftlie85A1S0plJ24ly2QxZpI/uLxoSJJ8Lz6EHOZBSlvN4dG+2gbcXySTfhQeMCBbzKwmwZ9ZfiJ8kgksLVknuXXCo4cojqcoGPQHQ5KOOwW74kYk0YK1PSzscGg3pgw1xL8OXtDBH5lkSyTDjXVX5UlJ6PxN2dx6N3MyAKMMiKxT+fMdoYMBW/B1UXdUqqqQ4SyvEhIVETQ/xJm+y0uodq9wvn7rQhtbZw/qkKV01DEa+j1RZlywAoY4YyzqYWsPQvE+dhBfXYqolmrKnepkc0s32fkR5YNAHDRinSJAVJvpUzV/YJqPe7O13eRZnqdGtM49CiNauw4ynGjH18kMtnjDYuvUdQlYnILfEWzmVkAAb/fS387b605DYwcWFHe7SQi/GmsXgX6/IrxW/8s1dUR6PN+N/HDSRg0= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(39860400002)(396003)(376002)(136003)(346002)(451199015)(40470700004)(36840700001)(46966006)(82310400005)(478600001)(6666004)(86362001)(16526019)(356005)(81166007)(82740400003)(26005)(186003)(8936002)(1076003)(336012)(41300700001)(2906002)(54906003)(6916009)(83380400001)(36756003)(44832011)(40460700003)(426003)(47076005)(316002)(7416002)(7406005)(2616005)(70206006)(8676002)(70586007)(4326008)(40480700001)(5660300002)(36860700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:51:59.6120 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c4c126d3-a80a-4373-8dde-08dade0ca9db 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: CY4PEPF0000C979.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB7864 X-Rspamd-Queue-Id: BDB0DC001A X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: u63946ridydwrgndgzw4y8u6ctcs7waa X-HE-Tag: 1671047529-642145 X-HE-Meta: U2FsdGVkX18NF2zj/gNwIbVdRbvanl5Hc7Zsrv1YwOXmKQITgjUz07P6xHlKXgn4WhTRyddfiCBLSRxEjMsp/ycNZ240aZf6s1oOiQ14GJJFglauM+Pk+AcL8Lb5UTLmgt7S4Q5kz9YTIxPUwBALt1uZFoIDSrIN29q7Z6636bD/0p+xZbX6+030M+osWR4M6syTfVR6CIsRYb4VAOKOzsGLI764LqiBVTyyOROS/5wfSbIb12p1vbmQl8Ok6Y0vvk1loaXxolpRk9C8uVMkNqDo0Kg44DU2Gu1orXqoiHO9kUrl8SvcQ7FkvdUlLKMONfOfg5RU+Hi4P9W88wdX6AaaXbpMd/1arK7ytzv4hhJiN+Pllgv23lzkpdhHVu4+DBdANPJyodduUMGWZYpezMeaK+uRlnoaRLWTQbEAGUoGXsSscAkcLeLZVzD2Sp60ywtSMNQOsRQI/R6FSj7BbOkZN5mxTnHHekCBIZHfuR+uAKbA5LTGvxn+MGmKL1HOflMDGtoNbhxeIL2RHuIp9gEYI7WpOnttUtAbVTsM3wdlW1PiEUV0ExR7zEaBW2cVGM9Tg8FVzMRECtJT40s3dyrwCuWRvgbuxeeZ02ZQFBVM0cHmqsI4lnbVcts5XLE7C0AvpShgAC7TU9UHWSt6AZVVAjRdA7j1nvPl6iTKGexh6j7rcHGY0aFtNGC+DSd/UQQe8nmeYiWfQBnQBjihuIZWYr5LYh5aOPiEldDwBGS8ESVlbRA5I8zA1NtrS/8IcFGODBytuk7jxPFiEKLQCb7pEVJMA18J6WZORGLq7Qf4jVgJcMevSpGHit9S6aakEbP0Bej1kplBwbvDQ9FS3tcZ08aH3u8fG4rMFqI0sE8SKkheLBewRJr4AYFivm+VWiBJEdXDzdP97sWwGNhQVuZ9z8t6znJfi5UBIS9S7fKy67tRSKKagx/RK+r2InmSKkuLKpu38nO0XmDBknx MYDmmWFo ultHbSZD7cPUh4aLenhAjp6dcxuCEfr6+O1Km+rU9aQjZ1KS66um7MBbjqQ== 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 d611051dcf1e..ded53879f98d 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 Wed Dec 14 19:40:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073396 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 C60D2C4332F for ; Wed, 14 Dec 2022 19:52:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 640068E0006; Wed, 14 Dec 2022 14:52:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5EF768E0002; Wed, 14 Dec 2022 14:52:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 469658E0006; Wed, 14 Dec 2022 14:52:27 -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 384588E0002 for ; Wed, 14 Dec 2022 14:52:27 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id EE70E1602DD for ; Wed, 14 Dec 2022 19:52:26 +0000 (UTC) X-FDA: 80241958692.08.FC93392 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2065.outbound.protection.outlook.com [40.107.220.65]) by imf03.hostedemail.com (Postfix) with ESMTP id 28EF420005 for ; Wed, 14 Dec 2022 19:52:23 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=GXPMg1Sl; spf=pass (imf03.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.65 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1671047544; a=rsa-sha256; cv=pass; b=l5IkBzBpu6I30b+2lls4KS5M6UC6NJplAu/o8TXWN34H3SRjBBSHX8KyJxBDj1s4n1xpiR Z8Kjz+MXRAQQAuLKaOXWPvO3hoXm3K3S0MXXIbG0Iy4GBp7sx8KfppAV0q03pbHPCQU+Zk ZvX2lDfrHG4sl087irb0YZhk05oPGfc= ARC-Authentication-Results: i=2; imf03.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=GXPMg1Sl; spf=pass (imf03.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.65 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1671047544; 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=GLx0x+amPIYqW7BaWhik7eNiLUWDfU0/h7EA7b98jRw=; b=YBxfCH4cUaZKURAgI+PveZ9PzxpRwzMt8z+f7d4b7x7mjQXuD5nh4UD8uE+lxbp6nNIpsM JXPgSCpaxyU75TZos0yarfELzaJ4GTf+rHcZ/UaQ3Cs7TwOsy8Dmt4CMN+dDOm67nZVPfQ JHu6jZtQeeN2picSUylljmg2RZS8QWs= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DeJrjchzstX+34F60lSkC/h/M3fnjKEnKBOdjIxBB9jdYqXqKUqNQuck8qw8hRVcSSs1pMOZu+lhEoXqkp3gmo8cYJKY/Q/Ik5GFdhlas35XFq4iAIfrHLBYVeouc/8eYQaAibGGaRimZVb/b/TPNEWIOyHn6FGpEZdUIsM02jBFIrYQmKnLmOm57tpRmcDF1oRAV/LiNcwnJkidz8gkRmrvfDrkoS648dblsskM7iBfBsj8//cz+x6twM6gdnkAT3AgwX3WC3AKT6ffs+KfaTegtIhkHoAyXz4Xjo/UwNGTeK0Fs6QBuwxZejIq2fF0YyYkTo3QVAa5h6ql9Ujiuw== 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=GLx0x+amPIYqW7BaWhik7eNiLUWDfU0/h7EA7b98jRw=; b=aXbb15zlV5XK8J19kKciSQOtm8YAL5uVKmrujJtDaWQUJ/+KF2UxM7NM/C/UnzQyxG5bBpCS07y2SaVXsuZJLrQv9/4W5KfsCAY9RO/rr1HI31XuZL/yK+NktwxFvkp6Amumz8yEgOzbM5mBDns//+/yo60js4P3xhVeAyofKoFL+S2FmFJ/nyWBKgsaELx0PUpsh9BRgeDcRNYPpyWIQTUYwpt21XXuU1ItsiNnRXtJXjBdPeLFXyUv9ora61w6j+2Gz1KeIkKO1/aH+2sYv8CvsA1+PPD9fqMmRjzsgVHDg6JR7vPGhN6htAdLxsAWiOJQsb/sIa3Yy8WwzrL7ag== 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=GLx0x+amPIYqW7BaWhik7eNiLUWDfU0/h7EA7b98jRw=; b=GXPMg1SltUKH5rMWaqkffdWnDX9U5z29XguTGYER1o8KyYcNvdKhWyqdnUlTJJNPsnyD7wW0lQePmmvuVOFv40z0k3uyQY2LOleBpZSybRkwRfASE3/Mx2RkOCL0Qsp3tthzoTkZrgGZb6kc+he6ueHXdZgmZMZ9g+qEczaGrzw= Received: from CY5P221CA0119.NAMP221.PROD.OUTLOOK.COM (2603:10b6:930:1f::26) by IA1PR12MB6185.namprd12.prod.outlook.com (2603:10b6:208:3e7::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11; Wed, 14 Dec 2022 19:52:21 +0000 Received: from CY4PEPF0000C979.namprd02.prod.outlook.com (2603:10b6:930:1f:cafe::27) by CY5P221CA0119.outlook.office365.com (2603:10b6:930:1f::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:52: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 CY4PEPF0000C979.mail.protection.outlook.com (10.167.241.137) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.10 via Frontend Transport; Wed, 14 Dec 2022 19:52: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; Wed, 14 Dec 2022 13:52:19 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 23/64] x86/fault: Add support to dump RMP entry on fault Date: Wed, 14 Dec 2022 13:40:15 -0600 Message-ID: <20221214194056.161492-24-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C979:EE_|IA1PR12MB6185:EE_ X-MS-Office365-Filtering-Correlation-Id: 00e693a5-0038-499f-da17-08dade0cb66b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: a8Vt/lxhQOcj00ofq1kEjyvUayt+iCeXuA/WKe8DB1urotvUecchOA74klJY5NtJ97pNZVMD4ctnXHofZcovbz+R/aj3PCkzIzf9C8wFAvY3hnrcPLoRa0moPfyBAHfuxyK2HvooN3kYAh8acZZDRnj94qI5/j8M0rF09G/yJgNeXp9gum+602kCmuu9pPr6RpSIaOOiH4YdEwAECW/qY9OffCg1ya4a8MXAaE0EqsVA1iE6yubF+9/zMIyamSyDxBp9K/JxNBNdA4FPg53iM5xn15ENKEqLreogHvHkwL42W25Sh3SVgsFp+o3jQAXdQNCibB5h5rU9fitSZ2lPn8y+9YISXWDsRKXsNRTYk9y4kynl3mVHxslghcgt9hrfSbfNlTo7D91g/RU2P5VJX/ZzVpLYvaTyhCIPV+/7Za5EzVSpWtmNT4ITDsUzZoQluVbKi2mOSmHnMq/is/W86nfaKmTN9n9UzSK0BAyYdWS/1JuRlovkgQivnzwj8NwBBlxD8GRp6DI2AgywPmpvjZt5QwbZfznfRNqYVVR8smc5bfNQY3JiUOBJncguIV0sSByrag5pHH8SEQzSwqWjHkWCK1ECHeCTtVnGIG8eB/XNJdaE+kXwE8ymjjg3n7sJIQFFaoRNpPTT1mAMzE5//jeJHHSBlsBWtY6c2QOksDwe4mWPrHPkBWVfkNO5/cfzhKfTA5fuTv/XZkl5RvdMWV+2b83Jv1riho7xm1OVrHA= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(136003)(376002)(396003)(39860400002)(346002)(451199015)(46966006)(36840700001)(40470700004)(70206006)(70586007)(41300700001)(8676002)(7416002)(4326008)(7406005)(5660300002)(316002)(2906002)(8936002)(186003)(26005)(6916009)(54906003)(6666004)(478600001)(426003)(47076005)(86362001)(83380400001)(40480700001)(36860700001)(82310400005)(2616005)(336012)(16526019)(1076003)(36756003)(81166007)(82740400003)(44832011)(356005)(40460700003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:52:20.6748 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 00e693a5-0038-499f-da17-08dade0cb66b 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: CY4PEPF0000C979.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6185 X-Rspam-User: X-Rspamd-Queue-Id: 28EF420005 X-Rspamd-Server: rspam01 X-Stat-Signature: hbmpz456ta3ystk6uazi49gdgjjhk7a7 X-HE-Tag: 1671047543-37278 X-HE-Meta: U2FsdGVkX1/3WWQSKejkfDS02FRqK2fu6ggCGWfdeZvoWPjs1QPD3GrzmsbX8Zmx3eIAYUJIhnfiK3Iy1IkiPOxrThTW2kLvzWta/hJxhgdrHsplHWwVBOM373cLkmXtzhIQcmam4SB6Qb/E7J8KxWG8GAGNvMnFoQo+2hXDg/rZngQb7Qw9v3Xv9NHShqfBoLgEu4CYIe3ShIAFpi3S8av5+J2bAHTiu6RwluIye7+3G3sEWMsgwzhIWBORgcx3H5qsdamox0dtITNKI0T150m8cdH8Fb9dQOvWoSxKlq72ZrtmYW2Ml4oY1zIUdF1VSgEPnee1pEKiFqZSPb9vubnjaQRB/zu5qdU98vJJVEz2ljOXb6x1H1swZZmd7r6BR3mPRQCQjbKo9tni9NVnGxRGUuPocc7WtfrGkMyGuWHFnDuix2sTD7jhjKHvmAZX+YiYulA1K+96FiDEBwvsvDiOro9UZIOzstkR6QTGP7PefAuvbF5d5qP1jgElDssFba0wONe2TQc8FEM4dNTAblby9pNtfKFFj4240kIquk5rYUK3xssyrDNA65lCnI7FOooioE6KBczWDuWglO2eDBweTRbu00Vi22PVtPLYQapzCXaVC85qrgpL+F9TWeT09RgN1pJSZmTAo+4vR0+lFo/h2fqYfctHTayOJqHqyq3TiJkbYMzVYcCkxGQKPyLM+ZzUS9nmQQnJ9aoavabLQ+ZVRcDK8OJJCD2KFZcSE4gaENt+NetRBN/h43BEvaQPKchqzo1diJVqFmyx+6LvE2sMwqbD6B58Fu3Pjr1975HPlu92gWMSXlxN+zvjOhXS63MRWguGUsq+n5weivoqYHveT2Cf+zSt7jVkaXMiZ3uK5uq+G52mjSJ4VIHJvZVr1fiPl0nbddJubX0id9ARQKETNzxNEJz7CNYUFkBEFTWb8dmm9Wa6OO6Clc8Mx73rapTZVJzkBTfLZPPYHuk MzarVlkN jGij8 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. If the hardware encounters the check failure, then it raises the #PF (with RMP set). Dump the RMP entry at the faulting pfn to help the debug. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/include/asm/sev.h | 2 ++ arch/x86/kernel/sev.c | 43 ++++++++++++++++++++++++++++++++++++++ arch/x86/mm/fault.c | 7 ++++++- 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 4eeedcaca593..2916f4150ac7 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -215,6 +215,7 @@ int snp_lookup_rmpentry(u64 pfn, int *level); int psmash(u64 pfn); int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int asid, bool immutable); int rmp_make_shared(u64 pfn, enum pg_level level); +void 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) { } @@ -247,6 +248,7 @@ static inline int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int as return -ENODEV; } static inline int rmp_make_shared(u64 pfn, enum pg_level level) { return -ENODEV; } +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 e2b38c3551be..1dd1b36bdfea 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -2508,6 +2508,49 @@ 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 (!e) { + pr_info("failed to read RMP entry 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); + 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 (!e) + return; + + 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. diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index ded53879f98d..f2b16dcfbd9a 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -536,6 +536,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; @@ -608,7 +610,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 From patchwork Wed Dec 14 19:40:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073397 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 EDB9BC4332F for ; Wed, 14 Dec 2022 19:52:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 88B318E0008; Wed, 14 Dec 2022 14:52:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 838AE8E0002; Wed, 14 Dec 2022 14:52:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6B38D8E0008; Wed, 14 Dec 2022 14:52:48 -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 5A1568E0002 for ; Wed, 14 Dec 2022 14:52:48 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 32DE51A0358 for ; Wed, 14 Dec 2022 19:52:48 +0000 (UTC) X-FDA: 80241959616.06.487D702 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2066.outbound.protection.outlook.com [40.107.102.66]) by imf27.hostedemail.com (Postfix) with ESMTP id 5785E40018 for ; Wed, 14 Dec 2022 19:52:45 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=twudiF35; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf27.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.102.66 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=1671047565; 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=WyDxm+ZEkUceopQe2fchB1xwX5XRjOyVc1qz9kE/Nac=; b=lOICL7Egw+XMsMVDJp2UhpClF/Ovn8BlMX+wO5sCBkRzEeflTyawOJPscfnh2dYQS355Om xfB6nZ8gu/TGOjREsG8csWtHpYBo3MzEoEQH2qj8j9zxOiQPYfuYPNW1PBXrnYWdbVV3TP w0DqWYqfozzL537T3ZTfrCgI+BhQu5I= ARC-Authentication-Results: i=2; imf27.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=twudiF35; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf27.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.102.66 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=1671047565; a=rsa-sha256; cv=pass; b=Hj55ailP6JU0nIpDIEHIln8iMV+2zDNaD8QIYSe2WxgP6e5wWTXXpTUHuNZJ9MNMrWQgeC 3SFgwWKCuBwFwK2xTsJmcs1oDDGZD2h6JSNe8HqqSdGKZBoDvu2NMlbm+scaPUba1rXCwT a2Tivsu947K4eXxfthRA6DqQ7xnhGpI= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=GrvnhCFl/kIRuRu4UYHl/+ORfmk8qqr6DiVxxk0H1fP7HQBlGPxjz/02Qn47KSwcU5JiNG9kHcGRkMj4qPmAoTcexXA1Bu+fdyKaeuqIAbnm+pCWqIFCF0ev+t91MQ6s0WQed49S5yqszySmRMRMRvLxuVwLh9xLWFlQvWE48iLaC8k2BKZ5vsCcVOs4b8p+ybb5IMstDm5eXfDcy+pVJMy2WykahZbNySoD5O0EBQbVjUnDMEYMZvQwQzrfVJzTY1EAPP41Am+17tPNXcBHKrQE9ZLdDDKJgOhKLDR0mMIBMjGIQ9tQwchbiYPevgTC/G/9bv15PJE53bZ8aYF5jw== 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=WyDxm+ZEkUceopQe2fchB1xwX5XRjOyVc1qz9kE/Nac=; b=Tv22aHq8WCF4PHRbM3/y0bLDiHWQPFohUPXq9JhQ3S+yk9vXvj9U2eu3fxdwQgG39fIjbvAaulq5l/PQDMQuHYtDLdBNw4gc2mZj4SL3imS6wfACm7tiEMb813mkefTBmb3YHEGe637hc5JEiZHq2OPaiEmA7/C2yjOpPWEbZjWS7SoeXsVVi/1llRLPLcp5ti9IThYYwzagt+roU08o5CiCPCDWDyhorYQKhrj3n4R+UtLRTvSBQ0Skh3WupULYneCm9fZbjQ2huEJDyN7plHPQzR0N7iCL25Axru5gJ5SmR0bsa/TrrsYJgrgJ0EvBzBKLJGYfR15MvkMCNADHGg== 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=WyDxm+ZEkUceopQe2fchB1xwX5XRjOyVc1qz9kE/Nac=; b=twudiF35FBS2fwHgljSJXvhIvywxJFeLzFDGzEzgChmbSjt5ltZyvyyscN3NSnaH3NRpsje6hl/ktQGhU+aGKs25K0A3NrVxvHt6+ZFcqJAZvl9PLpi+r9kgkjmCpOtsQk4G+md73WNXTljkv6R7lozsHwcEwSW6FzAlQiqSJlg= Received: from CY5PR15CA0028.namprd15.prod.outlook.com (2603:10b6:930:14::24) by PH7PR12MB7115.namprd12.prod.outlook.com (2603:10b6:510:1ee::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:52:42 +0000 Received: from CY4PEPF0000C97B.namprd02.prod.outlook.com (2603:10b6:930:14:cafe::42) by CY5PR15CA0028.outlook.office365.com (2603:10b6:930:14::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:52: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 CY4PEPF0000C97B.mail.protection.outlook.com (10.167.241.134) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.9 via Frontend Transport; Wed, 14 Dec 2022 19:52: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; Wed, 14 Dec 2022 13:52:40 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 24/64] crypto:ccp: Define the SEV-SNP commands Date: Wed, 14 Dec 2022 13:40:16 -0600 Message-ID: <20221214194056.161492-25-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C97B:EE_|PH7PR12MB7115:EE_ X-MS-Office365-Filtering-Correlation-Id: 806e9ea1-3ba0-47dd-58fa-08dade0cc2f6 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hE8A04p7erqN3aF3Dav0CWqGktNqxYFcPMe6qu3LfhudNI8QE6EJwCLClvZUrIhbuPOilIcEMlQmNAsKvmnSGdxjbSNkw2mPALZLIpUhfe+nNw4HXpbBqmGhJGGHlrWSeT33gCbzlrClY6lSPAEqvBVQmkHZbozv+zSpc96ffGegmeCuURwWQ0d6e0X0537OlMQVGRkHo/AqV7DUpP24EezxgZCaAjeKrvSzrCy53rtPaz0ruVaVMSLulWPvSP1fgai7jGWY0TZAPhYi4LKLqk0EyYIwDjIS5FfpAlPhxSo03Bons3xJKXY9FOcDOzlCq228ODfa/kmb5fzG0qBob+5hTT4rZd1ciepWrSWOFdbq4tvIbOKYlQ2Mu1u4c5G05A35RPaFX1bFFarU1tPa3B44lFBphizkYyz6t0Eos7A48STx3PemCG+LSGrzI0x8Lp6vHnRONkZ/8LRKYoMQg3t/YOuVlCyLoevqKRmcowYzDjB6oGtpzH5hadIzIfMWVGUh5XhabjmHXnc3lc1z2l3Y/mp5vpNNc8XM7kDe+Rfe22VS6+/+a6iSe/ZG4LRwHnaib53OBAKRHLW5UjHV2dPW3DNA1+kpNN9SsipFBI3qSZ4AEr0IAnzv8xASQKxZVQ8fKab5TlB7X+bTsrTVOwR7DULn5Fyoyk1Vzp+UUbgQqkNRTnbk4WfNTlgnRWdl6I31RVBK+rhF1b4avW1LZNp8e7APto5hkCojHvPI0r0YU1KVl3QJo5saLHTh3GcWwsJyxR2qpBjnyIyckYUnbw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(346002)(376002)(39860400002)(136003)(396003)(451199015)(36840700001)(46966006)(40470700004)(54906003)(26005)(86362001)(6916009)(44832011)(8936002)(41300700001)(316002)(5660300002)(47076005)(36756003)(81166007)(82740400003)(356005)(4326008)(30864003)(426003)(7416002)(70586007)(1076003)(36860700001)(2616005)(83380400001)(336012)(40460700003)(82310400005)(6666004)(2906002)(7406005)(478600001)(16526019)(186003)(40480700001)(70206006)(84970400001)(8676002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:52:41.6681 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 806e9ea1-3ba0-47dd-58fa-08dade0cc2f6 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: CY4PEPF0000C97B.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7115 X-Rspamd-Queue-Id: 5785E40018 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: su7iq1acagf16qdwkokipr35p1jc9ipr X-HE-Tag: 1671047565-18864 X-HE-Meta: U2FsdGVkX1/jWXoBBDL4fdsmPaZMNL92vmj+wu3bnMMFZOZSkbsNbAg/dxekDYBeKA/h+ojSkxDUaDh+Ucgy9sOonpLSWmPK3tfuZdo7I9RGzdEOnf377GFC5v9WTLZojUXhMVu25Llvz+v1vAtHEbh7nPeB4Iti7GEIW1tOAMBy2zirT8ayEqXOt8D/H1I1zCUhcFBMJ7mwYuU8QI0efc00YaNQqOwHhu/obhWhdhsimOaxRXKAtlZ7N9PSPMLov2du62KKrG/wpl243SQt3nY6ki/LO/+vMBF1mx6k5UFAImPk012QY/BUgsLGE+t5gKVtR4OpqKFoNVUV90u0jmngaG1m5IRCDXnFneuX+BjgbVZ71nEg3XXkvsFrmxoDAfYaNnMBjgNmrdCPlHe0OoVY6WMH01u7VcN0ZRxV9wXji5ZZQgjCXH1mxByse32Qo9rVX+4RrZGn4pjAI6Dd1fk3PFN//0VowW0ifrbhzub8+k1UbDHch/l9ZoWylg2oIP7J18ytSJpMHoLMkdeVKMBiRfKoIEJIyFETajdp7dCa8ubhj6ZIjxlEGWjD/yHkPw899nGOT9IqNxjoZm6/mhogJFCxTu8KhrYvx17Osib7z0ZC+ycT5WAOpzB/dQEHM+9Ho9R4mn20a+eqrr8ltrEWRzZdCwkAFDP7lJkN0Bpp9pGFF4YK4qE+/Z+wCPzQmrgarHkirV44o1LEs1zhJQ9XrmEa2g5gRFsK1sX/9qmcSzqvtYIPuunCziZEHMSaGMMvd7zz2TasVRmPE1JAGgRvJnEJzpLUhuJVPm2cC7m+CUzwZ9A17NKwXBWQO7n5D6pJOM8592ndlcg0yquvgaS7ozY3/T22LlvMWIKfq2YyF//4pyfTbrEnZG8A+cOe/0F+C7CIFU0vTwG0RsQH6kU9BomFeY0jjxw9c+X0loMy1KpO45/HVMJN6cCeUP/opFmGiLnQ5vLVyhvAnS9 L3a1Jlzv prwJQ 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 | 42 ++++++ 3 files changed, 305 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..bed65a891223 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,41 @@ 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; /* In */ + __u8 rsvd[52]; +} __packed; + /** * struct sev_issue_cmd - SEV ioctl parameters * From patchwork Wed Dec 14 19:40:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073398 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 53DEAC4332F for ; Wed, 14 Dec 2022 19:53:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CF59A8E0005; Wed, 14 Dec 2022 14:53:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CA5998E0002; Wed, 14 Dec 2022 14:53:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B46BA8E0005; Wed, 14 Dec 2022 14:53:08 -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 A393B8E0002 for ; Wed, 14 Dec 2022 14:53:08 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 75F2B80557 for ; Wed, 14 Dec 2022 19:53:08 +0000 (UTC) X-FDA: 80241960456.06.41572EA Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2087.outbound.protection.outlook.com [40.107.94.87]) by imf17.hostedemail.com (Postfix) with ESMTP id 77C7C40016 for ; Wed, 14 Dec 2022 19:53:05 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=edzldjzU; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf17.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.87 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1671047585; 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=PfZ1KhVgDMijscRMWev5aFEgyN8rrHRd4FqBACedOItzkuxkYPWoIJ1wGHlm19J4jwPgXL +4VgAsVTvFqv59GfqlYyzNTEMkE5Iuvq5UgCkGGf2/YMvHPaKfz9SedBa22IuETld6CVgg hBxxY6pE+hqSR4KarDAhHF4+bLmN808= ARC-Authentication-Results: i=2; imf17.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=edzldjzU; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf17.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.87 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=1671047585; a=rsa-sha256; cv=pass; b=gW66Pcb7Gp9p/DwpblZ/FNSe5aX+n1AXdafLhBJTKxIIp5c4hBj0jEGoBbjxJ1k+szvO19 ZtVNUejto/dhC0t+MyxQctgDEMXJcxbyYyhQvVofDUggngdx3Ou8MC/caIDLk/diEXO+hT AsjlW3Q1c1iYUS6P4imSM0JemzKqioA= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gzhSewsKPdxyI2rU9xBFhBAZs/7+8THWHE98jtbFhZ+e1qGurii3ZGnWRCE4Sr8Ngj+Q5hf4VdYKkX/IuggkTVF+YgX5hPfDnLunv5nj/ENq+6b0HZMOUc7VXMr7cvb5heq7uSQpgsIGpVTrjzMiEb7VGrjGL+px842N7FP1nx0Zkx7zstPfmzrUmCPlO8zAvOGV9pWOkUHMCZVOqQlCnVpnszEty9NM90gaBIN7ri1cPP4xkOfK9uH5CR8qcWgp8+1Lpikfseh+oEKVNn7r9Vxn3M4OH+ThNVV8Ckeg51tREUS+fmFTphKjrrL2zeH/buAyUO/X2OvboZxAt5l/CQ== 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=McnhUVbklKQEOhqhaKy9NDaiZaWYF6Ig6/q7UagEEPZWRnGY5/nLyb+uBy8tU7Pm9nyaDahgX2x7OLq64BHUnU42ABTMVmnRldDOws6Z1FZCsyGT6fUnX6YySoPUpTH0JErf0NhHEEOnUKCInqFeGs9F9A2JLucYrDnRT57Sc6heDt0aWO2sB7UoEnPfdsILHmyhQSytubjqk+m9lBDnTKN/CudcFSoUkvZVKC99Hykl6OjYKz3BB8zkPrZdByCB3tS96BcFEqSlq/nDnNle+6wqRWO17PkFa/piUi81mc0Q6aaht9Rt+b2kGqbfKbHwGR4pxJOtwd9uNlyUmLWy7Q== 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=edzldjzU355iQOFNxML4Ql+YpkW2t60xGotIqHrcvBH4e1LIQgv82F+UZWaMX5ZdeXyCI8Zrsfluj7iuiIuzh6y/t296J10hiy1P/9wFQqa6x18Ar630tR2Km0f+IgB4+6WXfsdb7VqF9kzwQZRgWH0i/eD2IC2r7ZXuBWrx9Cs= Received: from CY5PR17CA0014.namprd17.prod.outlook.com (2603:10b6:930:17::23) by CY8PR12MB7291.namprd12.prod.outlook.com (2603:10b6:930:54::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:53:02 +0000 Received: from CY4PEPF0000C97A.namprd02.prod.outlook.com (2603:10b6:930:17:cafe::8d) by CY5PR17CA0014.outlook.office365.com (2603:10b6:930:17::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:53: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 CY4PEPF0000C97A.mail.protection.outlook.com (10.167.241.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.10 via Frontend Transport; Wed, 14 Dec 2022 19:53: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; Wed, 14 Dec 2022 13:53:01 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 25/64] crypto: ccp: Add support to initialize the AMD-SP for SEV-SNP Date: Wed, 14 Dec 2022 13:40:17 -0600 Message-ID: <20221214194056.161492-26-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C97A:EE_|CY8PR12MB7291:EE_ X-MS-Office365-Filtering-Correlation-Id: 231bf967-f584-4902-1c0a-08dade0ccf6a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9T24pyQYxMAnsOkRc2EebtH8nyE6coC4ppYSepKBkzU8dp2vcYjFz0oRCQPGDj0I90hjXu0lZhXFILqssCkv2EvVWXnlWsZHTCwKpAVKq/fsRG5R5TWM8EBEIdDYVXdU2rYCLmxyH2yk/in5adHrFmMNgWsCCBFp/3bBlaI+bbO3BVdk+MSLIe1hi/tVUqqo7+gTzvWqWRbe5/ylxcqYVd97RTHKVDQjKBqOUFTVNjiwv7C6/1wYpXwpsRxkGBl39Nd2rUWtIjdW0LaNUuwCPqNAMNYjsUJ31/aHCSXyhxDrEYOkVD7BM8WIrrFie9TkLkKI3H89nQ1FWbFwaEf+Ma1VC5SpFKHrN0ohAJDJWTKJJQf7QZawwnY1F8FtYsNcmZnhlwVHPSsVhCYcgvue5EAppGQKB7uaCaV6WsboKIs44KS7MMIo3oXVViw4Fnxp74hh2QxwJ2/JsLuMgyIUvYV6by0QGpc/JX9Q2qu78I/3viXYtw4kNyDGiO+mH1Od2glzRdioH0aWMo0gl413wWVyxCrN07SHowbCnxqjIlB9qlrmEgJCsHNbWEOaZJ+53pMV47CgU4k/xheEGTKX2rjlHb3ywYJpqZLZ0Mo/U9VfB7LKWaxUkRwEGnGz2LX3wdZA0cDo9jMvsLeFjjc7FgdjARwucg2dq5W5TZzELAR63WR8yHMbU5HGWrXCcjXaPyZA6/FvBn/bqXWSRRVo+O5ZtxM6wnGTRcXopTNa+NdPupwXMinnHtknqN/MSC/KdAub/jzKexilz+9vpPB4xA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(376002)(136003)(39860400002)(346002)(396003)(451199015)(36840700001)(46966006)(40470700004)(478600001)(26005)(6666004)(186003)(82310400005)(7416002)(426003)(316002)(1076003)(336012)(40480700001)(5660300002)(7406005)(16526019)(2906002)(54906003)(83380400001)(70206006)(36756003)(70586007)(36860700001)(4326008)(47076005)(30864003)(8676002)(2616005)(356005)(81166007)(6916009)(86362001)(44832011)(8936002)(41300700001)(40460700003)(82740400003)(36900700001)(134885004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:53:02.6220 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 231bf967-f584-4902-1c0a-08dade0ccf6a 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: CY4PEPF0000C97A.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7291 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 77C7C40016 X-Stat-Signature: x5psqfco6bxjq7oxnt6kphzmfxu993e3 X-Rspam-User: X-HE-Tag: 1671047585-995302 X-HE-Meta: U2FsdGVkX18R2i5rLlM9PR8l2mSUFJUpCxGmJpOPMxTqBx7EZzwkOV6NCDtWg6VaIfNalk50lj1ucAELRqkiBFqWJxW4IpB+mz9oKQlVDaLFcBNB/gc00cYfs4poK7r/sH/DHK6pGU0LLjPP3yDri6RgQtE/+fEn/HVPUsocxMnbbdJAO/VIreCmcnZhV6rKcj1FHTjvhTRwmkXIOIctD7R1Af5A0rS2c3l48703MBdu8v7GoGHgXlcRdHlNURfCeso59OSE2S79lyjxtH4oeEyZ29K5h42GOmpvQXsphm8sm/O2V34bS2WnX/Mn/Zun4cdK0dBfE3fWY4Uy0Zru0Lp793/f3mthjbsaXRujB7MYo31iWHiE0WZDE6+OC0yue/hwICPZBEdnMIm1tmVrkufeUZmRinyMAguYubEu/srjZdJM2vQKS6pwQ6iSvCapZrcA5v9hqPRL/h6RwX8OI4cY95vXYKw4rB9IcZejAsJTl/cO5/hmkmwmSWuCZ1TW5dwQ1G9jLBk0cT2wG9siWWett8BwlQMZBPswAnebI2c6zf9T1w4oXWsuiZPBmLpsxQOMK0dyw6Nt0+RdmsJljrvTUEL6juhOe1JQopMFjcgQzn4+WhV2zt5GYNvSt6aiqC0kpkZwfZre1LlxvmWtCJtmAQakT9NazmFliqV2qsvoRS7UYwYp89nh3/fUHaRJDy8FJW6CuwncriUv2BjEWThmNvEMrOeOpZDCzF54UORxBwwjiAfYmbS73+rGPlP5joX82H1YrDUmc++EN8giMWb96p7pT7sD9mTXbux80dSOzZZDDm22y67veQjFQW1m+SzMj/1avAIqikoLkKe7v+h787nybwx7hO6QPq/cUwO5M2DdEiubf+LiCcQS6lnVeKqDKViigB+rvNcpd0iwGDmZAkQgD4XvaM4Ps1SKP+ffYkBvRiHH6TSSSwZJea+Cemr7QvpHzCVyZw9njmC 9stOed7A TFXff 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 Wed Dec 14 19:40:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073399 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 3F179C4332F for ; Wed, 14 Dec 2022 19:53:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D43818E0009; Wed, 14 Dec 2022 14:53:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CF4148E0002; Wed, 14 Dec 2022 14:53:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B94CA8E0009; Wed, 14 Dec 2022 14:53: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 AB07F8E0002 for ; Wed, 14 Dec 2022 14:53:29 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id BB30AAB43D for ; Wed, 14 Dec 2022 19:53:28 +0000 (UTC) X-FDA: 80241961296.18.36A1048 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2048.outbound.protection.outlook.com [40.107.223.48]) by imf16.hostedemail.com (Postfix) with ESMTP id F1760180002 for ; Wed, 14 Dec 2022 19:53:25 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=sXvSsfEf; spf=pass (imf16.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.48 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=1671047606; a=rsa-sha256; cv=pass; b=yRd+f/8Ek6VC3/yuXPAe4DYa1CyIKfr/V+v+2JxRxdCfbyRk/+wEE/oiim1nwskh7TuC00 J0+PEj1lD9bVWzg6ImQR8e7IyIRz2Ow2OCjzfaG7zdVUUAG+X+fCDFHATpogFvu4uZhseX vHD9g83ioobZxxfH6voDCKa/KD0J9Uw= ARC-Authentication-Results: i=2; imf16.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=sXvSsfEf; spf=pass (imf16.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.48 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=1671047606; 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=xr1ythBJLaLS1r729zZ+2uLky5x8YLyyZgs+zbq+wjfFS3+Fn5VGF90K5f89V44dvCqMPj TbbGSEFzRBHsVWocFDC9gCE0EIJRHsri/UzYDZfS1U85SOiZmApcDthzp8O0UnpD7mBVu/ xIdAltfzTgd6beOYPJx8rPi7u6bGvNs= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=iRrLR345INPvxFlhSdSeOpzxiGnpak9+Z/c1uGWs0DYwp0ICxvXEXCd3A6DhYZpVBrPPJrj1MIMHNMgE1DjPC4Tv1M78JD2/OLz3Re+J5zEZsyPbVVBtVCZeHS0Vkmw9RzY6YIrHD1ebhMlUwbnIKheQHCWQwMaNsT0XBgkoGNkb3HV7LN9vJGHAh7uuOFmIAr+i4LfaNGnl5zdlz4E702ByXbwSLOla1a3IoMgpskXK+wm6dpgJLWUyNUHt9NBjiZSiu+kor04kW5U3URTGr+WK9A6TPGR+LXuoDoMH8zA5ejuojg8DGeg0P00eT6ByBPb6LBCz0hif/KQP6Bw+YA== 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=mzCUmD7l7eW0AQ9THCpCmRxX90Z6RVFICk2wGOMqexoVNpSmjYLKO8PZMzviJzZjmQrJeQEa9iyySU57NsWK8vreejo40y4ahq3mmmC9zslEXNFEleN0nzwPDnAC7nTOHGm/ycDXNzTdWDvmGvv05XkKCu4jUxCN3FT3a3pQRb6BCYO/FUCyvE+6NZMXUsEzAGSm4p3yRCwkdWsboAPoFiPIH9R7CNCMLAg37PTEDmlnRIhu0ZF2i+bkWc7Ldh2bCtS3DZAI8jeXu7m2g7Xs9z2ZQxZJjJ3eIDZAI+giu6VDEh21PbsRa2w9v0D4f1GwnELYVrbB5qFjNe1RKsxDfQ== 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=sXvSsfEfC1h83fuOrdHkYJphbRlhDzw/cgN7vUUEUHq9CGWNhaL7tY2Lbber8U81nova0y/pAVf7wJRG2r/HVjqeQRRWPDnyPWDLK6CtahyLoyqkxVjbh6j7rC97Em2rnVHC3xYLZfs1WpFINaKuUmYwZd5w+s0+f9Qj4kEzWm0= Received: from CY5P221CA0115.NAMP221.PROD.OUTLOOK.COM (2603:10b6:930:1f::12) by SN7PR12MB7274.namprd12.prod.outlook.com (2603:10b6:806:2ad::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:53:23 +0000 Received: from CY4PEPF0000C979.namprd02.prod.outlook.com (2603:10b6:930:1f:cafe::c7) by CY5P221CA0115.outlook.office365.com (2603:10b6:930:1f::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19 via Frontend Transport; Wed, 14 Dec 2022 19:53: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 CY4PEPF0000C979.mail.protection.outlook.com (10.167.241.137) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.10 via Frontend Transport; Wed, 14 Dec 2022 19:53: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; Wed, 14 Dec 2022 13:53:22 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 26/64] crypto:ccp: Provide API to issue SEV and SNP commands Date: Wed, 14 Dec 2022 13:40:18 -0600 Message-ID: <20221214194056.161492-27-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C979:EE_|SN7PR12MB7274:EE_ X-MS-Office365-Filtering-Correlation-Id: 05d4b19e-a2f3-4043-3d9e-08dade0cdbe1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: tM6Q1mGgnRGZlaP1aoFsB2/kzBMnIguwWv3fBO6jnGcQXHdWiCgtsqgXauO1VOFhkbtR1uO4FhPXIpN6IxpuDAiIKFj2vtMED8LnkeWomf4zyzTjKOuHE6RLzLIyH8yL0w+UxGechGev7X4gcUA7beV5COneye89HsLKj5WA8s8e18fBgGjChADSHXVi39MAtBpTORh2p1rCeEa0egvaTSwkGsWarSbWxCVRFpRe/3PoLpPmMHJKNevQe/j7z2t2XZyUKHzf3SZVd322MCo9HW2GbhyBTc9/Sa6y+fNlzGn/8svr4FHzemx1HSQOeV0cvym6Sn8CpBqgdXgXBGTZ+1tQtU0o3UD5lA4M4zijrz4bjaCy/R1KEfgM/ZdobaYXJOfJHfJIBztnBwtc8yZmhpvNSW9+kC5rVLSH6AfmCLcNE63FGD4fASFAwh6+PsvgQvX+jycM4YAVL1O76IFOdeqyMvM2pUgVQqnlNtsz0O2J7+qFKPlb3UScbItsn9QyoJTR2sBlcZtJhz6U+u2KwMRr2iautfHHN15h3+UsHjrC54Qm3OtMMXcqcyQ8Zpa4iZUih1T82E05jsJ4F6qqRNrULdr5l2YmzTHhjsMMmTJ57YM9EpWPXfT/Gt9JG7t/Bg0sN+S8B5lSK3GyF03LrKnNIANFdWVZAqCz43W4iLc43ixlIkjZSPbu/t/KiWj2nU0gRZExDnylrjJR6WBwlcDH+kCfUczHpv6u4HKncpkDDVqyVqElcw3phoNGPaQjwDP58GMT1tstyejEozTPkA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(376002)(39860400002)(136003)(396003)(346002)(451199015)(46966006)(36840700001)(40470700004)(7416002)(44832011)(7406005)(186003)(2906002)(70206006)(4326008)(8936002)(8676002)(41300700001)(70586007)(54906003)(316002)(6916009)(16526019)(2616005)(82310400005)(6666004)(36756003)(83380400001)(47076005)(40480700001)(26005)(1076003)(426003)(478600001)(336012)(82740400003)(81166007)(356005)(5660300002)(86362001)(40460700003)(36860700001)(36900700001)(134885004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:53:23.5354 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 05d4b19e-a2f3-4043-3d9e-08dade0cdbe1 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: CY4PEPF0000C979.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7274 X-Rspam-User: X-Rspamd-Queue-Id: F1760180002 X-Rspamd-Server: rspam01 X-Stat-Signature: nemj8jjypgm5q7y6wsdctxesmpxk54md X-HE-Tag: 1671047605-789798 X-HE-Meta: U2FsdGVkX1+UsfkZ1fn5joCSHEiUEILJwl24bZYxwbbdmh6SU7rM+YS9RskQfJMg+MjZ3SU7BxVAquxh/uVpunnXkvalWpFxCmWyH+k/Ug8bAZQDGPDoTSMDLxcw/3N/bzqp6gC2X0hbyXcxuulh9UOMTZJiZ0cAW6JY3zFIcWfhp8FPgLRQjuDGSslq1KOxs/D617gEWunUz8yyR9uoqxGKHvwtuF8DcEwkJe4TFyprUW6boT2yskLsX/HiefRRUZYtSS4jCp019lVOvYCGpAxM6f8Uf2MfVpHRVBPgd4f9zoGB3giUlk8tcQSiqCPtkTUYGsHb5xD1vodT6sutnY3DLM+0d9IUweF1dKd2y0psSdi2o7+Yc8viDcNQAZLxAqpoeDYjzh9ayhZCd5X9q6ogD0ef268r9FdDUnJgZ3YwposZDDlOVyC1Nt1NxyeqbkFMWl0wz8ep5gHl6svqpjPU2JSVchjK2tOaCuG7R+zYTcycLfgKZgriC3Dv9XHJQWQ7qFp0ZP7/jul9Y6wpA7giyz4XUgks2i6HEyR3bWxtOjMliZpNnQXGZvCmihNlYjnGRqxKB6GIwBPWdGEwV6Mn3wheUTbf3YRiU5NiQoJY1h6t7hKGj5bxRB7IwH/aaxLXgJTdWCM6rgzoX424ufJUFzSKYxSnLb8qHJlbK6Qdz4ZpgMlit0kx0oaGTF1A0vmPrQBfHwBDHY17hZzhlzaXjigRauiJjXutX4pGXZOVIGsV8s739X5Rl8eS0UWS5cg/qCtPuJviCMps/+9i4BqtQ+YhFdZ8CtW3Jqt6WITC9DG3o3xIHMXr2m0eYwFmir8CuWvaMYS6H7fJstfIdKJBKtqB91xTdA6HRoY5BSYv2Lkld7Lc0ocOTdYHgkV8lHQBICs0C289Qha9ZUJ/3oQ4D0hDOcMlBPwFGndTCeI5eUnN4af3f7RJ5DnMQFA7LgtvAxtr+QSyuQhnnBv k19Mfmn0 a1765 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 Wed Dec 14 19:40:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073419 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 3AA24C4332F for ; Wed, 14 Dec 2022 19:53:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C802C8E0006; Wed, 14 Dec 2022 14:53:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C574D8E0002; Wed, 14 Dec 2022 14:53:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AF8748E0006; Wed, 14 Dec 2022 14:53:50 -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 9F9C78E0002 for ; Wed, 14 Dec 2022 14:53:50 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 7634C12031C for ; Wed, 14 Dec 2022 19:53:50 +0000 (UTC) X-FDA: 80241962220.25.79B158E Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2058.outbound.protection.outlook.com [40.107.92.58]) by imf13.hostedemail.com (Postfix) with ESMTP id A1E0C20014 for ; Wed, 14 Dec 2022 19:53:47 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=i3U1dAUj; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf13.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.58 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=1671047627; 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=h+D8vT833xDv79zvlMvnKBuo2u6DS1pW4OrtICa5py4LqgTl5qy4/PlOMdFrYtBF+p68Aq 2qTsITod7LiwfgGEK8y6U173W2mrR4wqGVVgzujYRrHCbtW2OJrahazqNs/ZH+yKA+ZCZ6 gHo4CQw0hWTOTW+vb/+CWT47VdEoz1w= ARC-Authentication-Results: i=2; imf13.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=i3U1dAUj; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf13.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.58 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=1671047627; a=rsa-sha256; cv=pass; b=zUgGr0Isl6b2DXZF7W0wwNUd5mwUA39aIC58Ibfq5NmnsOn3atU/iAWIt79OUVl6eJVCZt aiRPl6SdlYSLpQgFtNfPM2oI5JAvq2DLILXFHjGY7bF0x/HW8MnWDfT9t61RfyDtnqvavz BquFo9LdCx+5Mn/VreUpGzXQigVUcuE= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IFLsjPqMaAEfNX2KlAaIkyj9ysofX8OSCR35KqCsUVS/wcO9M6E1/tcanvFaaT4OiWUNwN9hFRyuQ4srsEwrJCEhKxF7aAEL3SDSjTwIIJ/SOv2U1t1ju7az1d8zEp3eGW6fAToet25kIUX3JS1FjGFU+UP9gHoKX98K2xCy136kAPVdvLkVx0iJR838NhKtiu/ur816vrgWpN0zudFBWw1Q+wOx7XvNIYMrUE1CdoAAlBWOeB9ugO6m/yGgI8PfN7v/2IqHzmsBRQi3YtInz3v4yHX5Cec41CHh1w3XN3YG+ykjJbq4m5F/mJ5UgNInjADkFODQu7NxHPCLaK6Aww== 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=h7x0osrR+X9DLSmjDZ21oJAtHHPAm9TvXoNeKV0hDSsR29SKWLdb1Igv1y9apv/vVeAS3m8/xEh5G8IIMzL+oaZ8mfGta2sZqyXQcjf0Wmt8/WUJMv31WxNVcoHhB6Gk3IQ60RtgOVMpdjCkBftNKXOdO5U07vz2O5Hc6eja1Zu7+37mvobeghsEA3j3FFdJ58YlPBr8+S3p7pI7RdfCr6FWcyL1Tu68C95NrAidja36i8bLZgWRu5boKduh7G0HUsErF1Cgo1ukuv4c06ogwuMPHNilFO2ZBSZVGlW/nzWC7mTrB02wMyDGwZedft9oRqMGYWeL+bbqVCvUe3fyeQ== 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=i3U1dAUjYpBMm9E4gVveUWzivtUSkuXPOqbpZB3Jq0zU2EDEM2T5D68EAuaSNokbckinZAaZw5jqHX0m7/JM3SHt5LkspBrIoPFKTggv9bW17Uc1Kfg+EHpiE0FbgKzDJKJ/JfSBCdl7G6+YGmdqxVQtx4fHFpGjEU/cFYXor3g= Received: from MW4PR04CA0298.namprd04.prod.outlook.com (2603:10b6:303:89::33) by PH0PR12MB5498.namprd12.prod.outlook.com (2603:10b6:510:d6::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:53:45 +0000 Received: from CO1NAM11FT058.eop-nam11.prod.protection.outlook.com (2603:10b6:303:89:cafe::6a) 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.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:53:45 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CO1NAM11FT058.mail.protection.outlook.com (10.13.174.164) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:53: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; Wed, 14 Dec 2022 13:53:43 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v7 27/64] crypto: ccp: Introduce snp leaked pages list Date: Wed, 14 Dec 2022 13:40:19 -0600 Message-ID: <20221214194056.161492-28-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT058:EE_|PH0PR12MB5498:EE_ X-MS-Office365-Filtering-Correlation-Id: 3ceaea20-884d-4181-ee7a-08dade0ce8b2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: gP9aD4ws7DSQm6KeWTkzAanosuEgd6BdgytIwVYwYp3wxqBDsNio9LIBEPfsMU2gBh9hUtrxePLTtcKHyMZL+eELz7gyfcJO1+TlMz0PMLjQ+ER9f/gZJNpel1jG77pwQ7Jno9mMRO/3QRbSbJUbUV4GjVJPnNzp7jx3EsNrF75tVPLiNWc9PZ5/yuoYPZ4w+5310sd2TWj9PJJtWyGXP05NorN4tz8PHhg38SFYEatO9aRda/krhJe/2tBIiTDN0Ungjan+AbJCUmFi7z+mS/SsiaNLVglu6sb3JuIFSEDdK7FPQJ7lN23efLWYqkzNICdgHjUUQkW9VAWLFTC4TuxopNdsvDXCsybrtrlWbqHV5rF74T4Fq17a+flpgJmqx6ZRgzL1wFUMH89e1kceftOCQ55Mho5HWKZpZyh7xDNj+KqtAhx48SkXB7TyS3Kw7oOa5ltAb0+xgc8eQPXvvJKKsQt656rqPbJv+Y57dyuC8m2FS9LMFh349hhyGaBZpljtzx84hmmbCA9vOnY43Myz+Arj++achJUyGSOUHmW2BDMUObIuXK8LRTMm57SttITu2L6bN9kNr6Ck4M79NWfceyOFDaLCxXCyheCaUjq0ofYfNJ3H2odYkbXTNsb+6E4uyhQ7FtFfkhj2yW6JbtOxg+nc80wItuX300DuWXfKGGssWtOO+KCzIMurnTM3Wo49+1jRmu91RJB/YBhONRxql5ChJYOV7IdS04ugvaY= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(396003)(346002)(39860400002)(136003)(376002)(451199015)(36840700001)(40470700004)(46966006)(36860700001)(70586007)(36756003)(16526019)(40480700001)(8676002)(426003)(4326008)(336012)(47076005)(70206006)(1076003)(7406005)(54906003)(6916009)(316002)(2616005)(86362001)(7416002)(5660300002)(2906002)(44832011)(82740400003)(41300700001)(356005)(40460700003)(8936002)(81166007)(83380400001)(6666004)(478600001)(186003)(26005)(82310400005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:53:44.9642 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3ceaea20-884d-4181-ee7a-08dade0ce8b2 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: CO1NAM11FT058.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB5498 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: A1E0C20014 X-Stat-Signature: fc5wwpsq43sm5y1nubfyrax133geatb5 X-Rspam-User: X-HE-Tag: 1671047627-693754 X-HE-Meta: U2FsdGVkX1/xoydeSAn7MNlGNKvan+zsnabE7M8bqCryq70LFkyHAF1cEFhBqN+aTTWh3myui+dEG0204nReM4eTYvRvsdCzQdNC4S/ANw2po5sRU6eNyjVct11wseXik0caIh/YUqU8gvxefnspJuJiIsDcGywqwlBSaylV6k0pW/F0iSTTR6BuBgfuJDvW/Mzlk6PEXEkckIhizAkPQfIPZmjbFvvpYZcoByVQp89etfWAdl1c/VdkiA7ElEvVYvdnwP3C2f+TU/RAwhmfesstnz91lBHOP72JUxeKgDDU8r3rMjOgUGWp6kFf4+M9FAPPXvVl1GpMXK51DUOAp+aEt1+lLSjH8xv8SHh6CQ8iS1YUbaFH6agMjZL9ODchWso1ZqtsadEIVcp8XgNb9sqaAzO3g3hglSpPyCQilVHqiMTj7Ck9GtDg6tzKEMCv02sUwes8cmTi287o41vn+tR89xADM/88jtv+QGWuvTwT5H09aoolo8gv15yNh6UXVRHEpGkvesa05i+PvzQTaoDHMKYs2okRos2TY/9oceyUP3Nbjww/cyqKdQtV9/wBCkVymsLmG/Ix2CaRc/bhDlR9rM+3DdeTPgGhWWiuJRommn0Vy5SonO9325p+75mnGslE936shhPdmHe8BA0vCML1XANCFEznoOYFVG/EUk3Qk53NZ7pXAAIejuvwTLGRy+8LN2Cqt4egB/2/OztRgwdN5/BlGj8xttQW71x7p+XVSgg040Zw7/gjRpkWlRDwQWFO4SPuETKR1w0HQDFFcF+pRsVyf/VAH7BhLtCMZQ391vnq3yZIT+mLtuujaFHBQYGxXyYeHrJsGeljlvvzULjkLLRSMRjA8ToZbCy8snF7arkFhvveDqnazfH2DfpXwcNnsme73T70EkzVZr/buliEogu+BsU1ekAoyU5EmbJHNGpXFUZkvZq3jVR6LKgcj8BI0pItJslbkJ5FmrE 5HjdYfZQ TYpM8 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 Wed Dec 14 19:40:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073420 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 C4973C4332F for ; Wed, 14 Dec 2022 19:54:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6B3348E0005; Wed, 14 Dec 2022 14:54:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 663AE8E0002; Wed, 14 Dec 2022 14:54:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4DD3F8E0005; Wed, 14 Dec 2022 14:54:14 -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 3BCF98E0002 for ; Wed, 14 Dec 2022 14:54:14 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 13DAA140274 for ; Wed, 14 Dec 2022 19:54:14 +0000 (UTC) X-FDA: 80241963228.29.5E318D6 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2083.outbound.protection.outlook.com [40.107.100.83]) by imf22.hostedemail.com (Postfix) with ESMTP id 475AAC0003 for ; Wed, 14 Dec 2022 19:54:11 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=HKTQL6ch; spf=pass (imf22.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.100.83 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=1671047651; 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=wP3UtI8QMx61pvxjoUJy88PJPd+DqVNYLd6gY1oK/uAXadX17REnxOnyLXV2ttOcTLza+6 oQxzGFezs0hi7q4y0dP9iivKxyfoU1+N6uDa1EXxSm+ieG12zy0YH0UMf8sGgcOGU7gsuB rzlWNcNLm+6bqIXIuuls91s54iEWS+0= ARC-Authentication-Results: i=2; imf22.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=HKTQL6ch; spf=pass (imf22.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.100.83 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=1671047651; a=rsa-sha256; cv=pass; b=fZKsVP7aydQC82eGaKG7jY9TLeSkWSVzV3H+o4UjqKQtjtgmkw/Xybg6NGG/CwMp9K1ty4 I70uVkI3kp5ahUTaBKxLL4dTcnFDk6WpAcr62vF3pS5+RFh3mc0967sF8de45V558GQ2Kw tPStE3CNE58TPeV+ZsUtCTos/OThgnY= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kbGDUzyNjXyyCgkSGbrWFic914DdFjw+NTh/YTi9MyfPfjyY8S9XgSVQ/iV2acudYHixF6dBOiAXQOZ3BIgGKwRRExFYdvwqKhZkgpl29LorPWH2sHuXsKXDDjyPaO9Xo6CYecU+eo3ctaPMsLH5wTIlAuvlr2OSKr/FgJBNUF5Y8gfa8ykxrqRvWCd8pP3Z5kIVcx99Uc4oYT29C0IJli2Q4urodwn1MlGioHoNS82//2wZL5MrcYfTnWBvfIR3N+fK3xZ+nxCzjbGmakfGtzA81OShbpmkpiRy+oWiQW72rx54XaxYsAVpqp162qIjAJzWso4mLFuiLcGC1r1bwA== 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=oVJxgkc4MQ2WNFyMXtimAb+i6gGixegr057g/I147+H1nVLW+GA4cHtqAN6sok0npVuZXkjd0N8VIRiYQhB1Yz2g51Dh7Qg8Zddk8SC/27dHUda3Py8tX6GkeCHdrf5lK9ZtvNANPbLMXtX/CbLzfKQVQyPFMmiQjfn2ibeOTBhG+soStOt6CBeu3Ikc7AQSMn0jjA6r1RoW7ODZCr/cK0li1S7odEaF4ydfb/XHbOkqwPHQbbV09n+hBlya9j+/UHq9ipA5iaSvWNOTI5xGKp8gHozSEE69q97PXK127JalWO+Oh8Go4bJ+J2gtOnzEDsitWBujtEC5a/ElDbDBVg== 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=HKTQL6chZNHsA0ubsBGX2Z+rXhvaM95JEKK8KuTH/9W4gFfBcXP0sbDx8CIlCEZzAgu85yBZ36Je+Gp6l9HFLQemQ9JYhO8tuuJwFtmll/qbsApswAAVN2HJOlg/+sC18uQVttn5rgIKTy+7UQI0U/lzIZOIGI/r+gU4HiK9MCw= Received: from MW4PR03CA0078.namprd03.prod.outlook.com (2603:10b6:303:b6::23) by DM6PR12MB4562.namprd12.prod.outlook.com (2603:10b6:5:2aa::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:54:08 +0000 Received: from CO1NAM11FT007.eop-nam11.prod.protection.outlook.com (2603:10b6:303:b6:cafe::12) by MW4PR03CA0078.outlook.office365.com (2603:10b6:303:b6::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:54: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 CO1NAM11FT007.mail.protection.outlook.com (10.13.174.131) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:54:07 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Wed, 14 Dec 2022 13:54:04 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 28/64] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled Date: Wed, 14 Dec 2022 13:40:20 -0600 Message-ID: <20221214194056.161492-29-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT007:EE_|DM6PR12MB4562:EE_ X-MS-Office365-Filtering-Correlation-Id: 10d5cec4-ca44-4cdf-b144-08dade0cf665 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: aI8MH51FYOoP2fyx2++/kyck0HOUacr+EyNcPzZYnWkhfpjreFqCtwajhUcT8B6dmtdbkmy84glyvR7oWdjQUz6DLZGNa5v5TqKKI8tmkt7eFQy4uwZuddhrlj2NjNqLHsJbT77y8cnZvOA3NK5zbavZAABBH50OOtUviROfsvpcEWkZvKy6xD+PpR1Zp23V/GeXwLrPLw2+b8ITJaBNn3ngV6zbnsZXQ0ut7PnZobBbfjfnaKXmd0vaEggNWw+8vdOK7GUgfkLVli0/9+tHyRYgoCMHMyCOGnE8RBLUz+3YaHdyaSuGLIDlsbZucaS7Akjdoipg+ite6tVkdZZOXwsyVWMefwK+B6/A6ydxPf/d4tY+UCjakTnDHwAcugqn2/a7L1XHtCTXQ9KLmZaR+FxaRVywxc1JkyZ2y5LRrxd0Yi9ZuFCjDdRGTmM0PqB/qelW9qJ/JHYJHOo6D06+IF1DBIMve46QvKKO7c/7g39IBQFHBHL1Rn3Z+8jvB1Hw61CN5SMDV93q/CtzXbsz2AniCzG2qyswZJJ6Tlid8bE/apl9rUdJZW4ixkhN6AWx92hsyi28MHQoTB0+HeoFfJq/bFMcfisaPBvpz41+4zJYMMA+W+EUFiSHFRH7eAETgFoLUIuaNF4Vn9s372KdTN89XITO3iX0eewKiXxp/OokJ0CUU1XFH526CrWfcULrRIEw7ZR/4mpnDo9BQAlQyXDSbamO5S2jRafLCjA0aws= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(396003)(376002)(346002)(39860400002)(136003)(451199015)(46966006)(36840700001)(40470700004)(2906002)(44832011)(36756003)(8676002)(7406005)(81166007)(70206006)(356005)(41300700001)(8936002)(70586007)(7416002)(36860700001)(86362001)(83380400001)(478600001)(426003)(54906003)(47076005)(1076003)(316002)(6916009)(2616005)(16526019)(40460700003)(5660300002)(82740400003)(4326008)(6666004)(82310400005)(336012)(26005)(40480700001)(186003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:54:07.9773 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 10d5cec4-ca44-4cdf-b144-08dade0cf665 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: CO1NAM11FT007.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4562 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 475AAC0003 X-Stat-Signature: 1fkx7nqt641zzy8oxmeo98gt8noqrt5j X-HE-Tag: 1671047651-201009 X-HE-Meta: U2FsdGVkX1/AvrSMYVImF97zznHbpjw6R3kHnj2WT/t42HGhCPgXMg967DDejThhRhglF27ayGHbP68QrNpBc1V/3DRIYCdkKf/TD3SlKjUfw5165TAnv2fJ8o8US1n2aWa7tT2w5vNniVxrkfca7Q3NXbd/RDMaRsVN3ibTYgRgGcTrPeYxfWGSOik3pVpH08fsNC5avv+rWJvdGJKnCI8LVMdXqQIiaZloPZ6elVbbqBVKjF5EIEVP3Y9VED5BbY3xXPe7+gVBjmDwbkqHaFJ7LPBr45z9SQeZqhBunmNCN8C7k04tg92OfpAgBicnOvdNyj0MshvbJFpW7n2WMef7cksOBR4UEJbWCR027t/Y9hYtkxK34BLQXXUuIdj3C0TkPqgtyR2pUaZoDwOvMaGIO56mRq2ZXuHfL8OhGf92xObeyQi1a9JA/LkVwVi16S0Hr5L+sYve8Q1mlToCEBrSfglHThkpRTdzC03R/LgG92BDPpLFZEutp/6fobfPoheVwulMboW9lezbj4eRgg071n7uJWRlNgyLtjNXqbSANaaqgR0Q907HsKjqgbVnLm0rkFb/vr6FbRDSFpoXPvNPcGTMnUAFk3DO/qYkZ4YOLPTo93dASoybFsZmabhVWKlbbQ+CivNOqPkhURFQAmCLpZlILlhVMY/Zzp5I/yf4PB6bGNT9S7PBy+NG7nery/r6lEm3Pg2qA2V176oDZ1mPeA4Qo8+U8qDZoWEANb6YKmzg8Z4h+Zg26J18XOFdZ6PUrXA8pwKGl2StqipR9eGgurE85v/6kPIZYcUok5IsvJCA2YXsH3kyt4x4jbP0UXg4+gYMHV9w3Av+9DP2Gcx2tel2iYvLJt2cg/Z1prScx+KgFMgDL3Jnb57MQwvk2F0/N/tLP5bgk5i+xjxF8kVm7Hqv+BS56UCz/48442zyNkDtw871a+Gt5vzod0oHjeIfcznzcx8nP97id/z 5KWGyMqb frMRW 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 Wed Dec 14 19:40:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073421 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 C568AC4332F for ; Wed, 14 Dec 2022 19:54:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6B38F8E0005; Wed, 14 Dec 2022 14:54:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 63CBD8E0002; Wed, 14 Dec 2022 14:54:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 48F9E8E0005; Wed, 14 Dec 2022 14:54:33 -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 30A2B8E0002 for ; Wed, 14 Dec 2022 14:54:33 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E29DAA0F9D for ; Wed, 14 Dec 2022 19:54:32 +0000 (UTC) X-FDA: 80241963984.30.78AB684 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2077.outbound.protection.outlook.com [40.107.92.77]) by imf23.hostedemail.com (Postfix) with ESMTP id DA715140006 for ; Wed, 14 Dec 2022 19:54:29 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=LKuauPaU; spf=pass (imf23.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.77 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=1671047670; 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=ElAldfZLGPcU/rNHWtctMMov4dLZSL+ESPt383t1qiY=; b=ZzQCQphtN6SPr62MRXX7Uavhz1jGQmKowaCJqOp4wULi+3M2ZqTWDg2gPn7haJEPxw7+c+ dBqI5u7/o+SQFOYuCwmUD81mH39fwFbo45HdtPpEa4sRAUgK8U0ZTmUWkrXFYAeeRjzTrn 3fZMw059hI4T3eaD+EBJvm+W8HVfXII= ARC-Authentication-Results: i=2; imf23.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=LKuauPaU; spf=pass (imf23.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.77 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=1671047670; a=rsa-sha256; cv=pass; b=OVL2tRGlt5qO0BkJmiXTsMacR/3hW4MBnouG1zXpRP9deAtf+YN08rcr4tMSQGb9qNedoP wI291ywAmmSJt3xkic0/zpHCy7CKJ1gjz9vKNvyanYUuLnKEpkeS56gp9i6Qbac4NA4U4p GYfRTNJCHygocOy6CR4SDMQMLdPdbwM= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=R2PdguFxVzo3NQMZEf6bc7xhUpuvwxv5MqckWRb086YJ4mTYD77pIXz2+s+RPgeTcFbBt9ybU39a9TS8tD7lGYm3X0uv8HVaB5+I8Gfy4H9zgc8LM7sX41qVeHqG2SvYsX/J5o1Ihd9d9KxHG/79A8Ix/ghL8IIR3eO9Li4BUIsr3VoUIkAR/IqqgraoiLVAwbA1mOTYsZpAMy8lpohsPfG+ZBxQhmYHXMukqGL8qm1B7FbQq5k1CbtvQ6kbBZzjt92dalSRAW60maKj0AROlmjPTU+UaWLepy2eJCOkw+0jrQgZJMKmAJ3BFgQ0exo8Tjy48z+z3LewbXEioWu24A== 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=ElAldfZLGPcU/rNHWtctMMov4dLZSL+ESPt383t1qiY=; b=l42UQo5NWB3ZZOiLBc+5FjF/piY9OsORngEjpFUgCxgpcybkUQ7sg21iBrHHLhHd6CaPkyknwUo3xfOozst0G8zjITTSqu5gxQZSe2az7wf3kM1U3ZrtxNviJUMZAJGaFBnW6cDMyusDdpFt3wUX/kDuqhDIQrY6nkBLeh1ijphKGu7vlCNTaGPUQfVnLKr5gFVr7+fLTy3TiC0dYxGbzf/bXz92QL6/gdaXHUolEliBbD7/VfzWEucf5c1OenxkR/qBWiznoN971DMgsjnS6E1t97FTmK1fchzTEvuwOlxyvTCosh0PGJUw+aZ4511NWDu47/+vCLLBYJlu/iyP5g== 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=ElAldfZLGPcU/rNHWtctMMov4dLZSL+ESPt383t1qiY=; b=LKuauPaUAW+0jpL0M/N+m5MWUYLoOFYuYeQ5CPvJXbkkRrBzyyMsHfee1Unu26bNdw9IXJE+FDenAGcHJckACGqbrxcjUJNZxBKNYOsj/ICiQkZSbMnfLeKjYNYW7gsSRx0NS3gVfv8slsJbzGff1ejh6itvMcUM55RwHVitG9k= Received: from MW4PR03CA0232.namprd03.prod.outlook.com (2603:10b6:303:b9::27) by CY5PR12MB6455.namprd12.prod.outlook.com (2603:10b6:930:35::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:54:27 +0000 Received: from CO1NAM11FT110.eop-nam11.prod.protection.outlook.com (2603:10b6:303:b9:cafe::e5) by MW4PR03CA0232.outlook.office365.com (2603:10b6:303:b9::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:54:27 +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 CO1NAM11FT110.mail.protection.outlook.com (10.13.175.125) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:54:27 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Wed, 14 Dec 2022 13:54:25 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 29/64] crypto: ccp: Handle the legacy SEV command when SNP is enabled Date: Wed, 14 Dec 2022 13:40:21 -0600 Message-ID: <20221214194056.161492-30-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT110:EE_|CY5PR12MB6455:EE_ X-MS-Office365-Filtering-Correlation-Id: 113efb0f-d98d-4f5c-7a21-08dade0d01cc X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: SdM7ErY81ZD54rCfIEVC5FlQE+BIa7re3KmA3c+8Qf7IF5/YliC6ItOlEh5WLdZHHf31JvTqOCulVfZEwidvG4l6P5mKQyLMddsGA1v/flKatmNqUxbiLl8uGMKvWVngjoJlI2GbUwOFoNRoZwlsSxKQKjNc1qwc9n1xQy+R3DqPOJZ7A5Y9h5gjWmMNiHMnQYakL2h2WAvuYNffXt3BC+8sHYWs//mGSL+oa5QTCSEZU2weI6Gv4QVuuy9ktSNYfhX5p+4tORHPeBI1Kkgt2AR3fRdH8Hcs+jlFDr1Cu2fZGmQezJIPsT978pU5GCpBpPiq4QSuSB/nucPmzlgBTijHjzjPHRvAH51+J84ROxGi4IzmH4axkizQ6EV/FM9cX140tJg2AUDpaD8tVFlZo7QdeiAtb3HogjkFp0DVoW5iSb+AVUXJf+ulkycmXTfMUewOSXNClGrrgK1yzx6tbWlp3aLOK0lhJWyzW4sN+/CRXfCK9JUlmFov8MctAt0n5eIq10YuH3NMmB72Gj5C0dkutBXgjJ0nwkEOulqGdEko5hi908tXP6cCns1o1GGN/hRKLElvIjwFW6YLcK2Z0E2ju8/QFeYSQwK/TZ3Na+8UOD/FLeqe+7l3Hms6PI3EL41lXTUgUg8DnDWmeTJ5CA2/WvYCygnq1+I1NxEZqCpfeyisCXMaomQTwIDuy0vMY2ILoB2jpUPYKOHRkBHm3K7sw793GKUmp2kSAmWmCoo= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(136003)(39860400002)(346002)(396003)(376002)(451199015)(36840700001)(46966006)(40470700004)(2906002)(82310400005)(2616005)(1076003)(6666004)(36756003)(47076005)(316002)(26005)(6916009)(7406005)(40480700001)(478600001)(7416002)(16526019)(8936002)(83380400001)(70206006)(54906003)(336012)(186003)(426003)(70586007)(8676002)(40460700003)(4326008)(5660300002)(41300700001)(81166007)(86362001)(30864003)(356005)(44832011)(82740400003)(36860700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:54:27.1059 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 113efb0f-d98d-4f5c-7a21-08dade0d01cc 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: CO1NAM11FT110.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY5PR12MB6455 X-Rspamd-Queue-Id: DA715140006 X-Stat-Signature: jn3xc3akmg547tiwq1yyyozy3x91t9pn X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1671047669-699548 X-HE-Meta: U2FsdGVkX1/NUrFUYiJt3zk5e0nQ84Bcb/8fJcSh1eE6scIW5/WWQAcPTKNS/6+aIf3dgFd1sr01jodrWV8tp/H0VXQ+1qyc6pJ1nUSf6ewwrHS8gKPBZ6LSV5yVaf62KbkZO810l0E7L4w2NkCnp18TPhngtXJ4RBGxw0KU8T6qQ9ow+dskhltGHPKWnWrAWWj5wxKU7ouxoUdlYIoCLxMzmFIV7M6td+a/n8jM2IY+SCc48lG7URsB+DNUpYMdmeWCB+ciE1uTygqB6EYz3lRCis95/o5F1c7QktttUEKLL2X4t+SrTxzxD/NqAqfcAm2YCBi8Zl0dJUEgKNAdCznpGV8IUhDp7h/hlSQfCI//IlthlboEaKhVXirLhBUES6nZUoxuRB6B3hfSAUtwwnpoLMluwv0u971yRgX+gyWk98QGQOzGPgVrv3gWDvQ0gSCs0UqYEqQC3kjm0Uv2n3Km4IVXr6beLEfWcUxLCjPf8GBhzKLyldRLX64wBBcv+o7wAriQF0I9o0O2sIhAoLpDXky6t1oMcAp4KgvdeWpdpcnOTIlVaVzvjqls88q2LxYxL3OMJZ7cWqiBtsUdEj8UJPbQeJXtMm4YjCFkyX/T37YX9VFG6BaqgX8354G9F8FH0zRQhcE/Dlevw8JrGQQ3vUOGoURVFLNQRyQ22KH4xlyACBlkHg6UUBk1QzEkh8KmbrO8/lqouaA7c4f/6Gke+E7MMPXgQxXMMFEZcdD0EKyBR94lCaJkH9Sq4p6/aOANF7lb6gO5IjJGwTWm8F6LpcaCJDq5e0HP8x/KYJ8bL8SWbWzzC71a4MnkeavEeGEDQhvefh+09sdoz2AoohObO/mckkZiM57BbQg5G/N9usVk1WfdXET+GVfJF7LHv0jOAs/fArjWLb1MQX90DBj64ehjYrH49FUbkX86ywoS0h9vpUHeEect+wEd/GiDaBhzOa4wT9+jHWAxU7N e6TE0dzw b4Lb6 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 | 370 ++++++++++++++++++++++++++++++++++- drivers/crypto/ccp/sev-dev.h | 12 ++ 2 files changed, 372 insertions(+), 10 deletions(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 4c12e98a1219..5eb2e8f364d4 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -286,6 +286,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 +511,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 +819,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, sev->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 +883,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 +1911,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 +1982,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 +2062,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 +2087,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 Wed Dec 14 19:40:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073422 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 3512FC4332F for ; Wed, 14 Dec 2022 19:54:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C8B068E0005; Wed, 14 Dec 2022 14:54:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C07E88E0002; Wed, 14 Dec 2022 14:54:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A34308E0005; Wed, 14 Dec 2022 14:54:53 -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 8D8CA8E0002 for ; Wed, 14 Dec 2022 14:54:53 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 633AAAB4D4 for ; Wed, 14 Dec 2022 19:54:53 +0000 (UTC) X-FDA: 80241964866.14.81F0E08 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2081.outbound.protection.outlook.com [40.107.93.81]) by imf30.hostedemail.com (Postfix) with ESMTP id 96B1B80006 for ; Wed, 14 Dec 2022 19:54:50 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="htnQpS/i"; spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.81 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=1671047690; 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=vcft2Rd+FceGboMQ891metKsRjEQzg2Y4zDqbdImk5c=; b=HlxxHE9rm/NIY9N7zYVRDx5ZXlTyTnM54F+gax7Uwq9R2IQF+bcHaN8ft2iKwBlu12Zgj9 8rg4SWW7DKXsAsnjppKDVD0KiX0c+1Keg11q/pu95dcOE5uTub2EfxIcWrj7m6zAY3Z6/5 npdZTgLbvSIEaulKOh3YYpWdLW8DNVQ= ARC-Authentication-Results: i=2; imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="htnQpS/i"; spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.81 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=1671047690; a=rsa-sha256; cv=pass; b=3q4YT5ufsTl/hZ3/P/PeRLD4vD22/4TuN+/2KHoQQ5Gj9mFFGSx7Wag8GkRAmS8vZj0uot oo10bEm0i0vsSHyZM9Gp8D/SLk1PWcJ8LmBaX+0Z2EePYVNAjoT9gPK0eSY90SEMhzdWTh T9kBfiZ1IIqmubFn+jRytblGVJk/W3Q= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aYHpuQ+XKgqsjOsGApBv1H1P74BAookyNSSAQxaGrlxZR8YN6mzY+hMYxnc9Z7seeucHWQI3YDEnpTs0E39JpqeS+RSM2Y+PiPmkBInjXRdJbxXywFz0zKps7inDnVq3xWbj3AcEeKNwpaqfpyitV30kINNpLL302GzdEdqH/kWeoqHkdbD18ao35VMHN4lHLjc5OXCjUAepIYMvcJltillgJS2mgrxYhMa15TV2QH3ORbDQqquEXFJ5eIA8HRToL50Xx3GdVTLJMzSExnDeTmPwZQ5VIpWVvp2xZ09wVthPB9AoT92Febaxhp3P8//JhNHnt6xxzaQZJy+Jn3sQLw== 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=vcft2Rd+FceGboMQ891metKsRjEQzg2Y4zDqbdImk5c=; b=LiQ+DLi/VstbvbLb/fZ2mQYo1Wi4o9xFTpZPx7MwfrterC4RB3b6YPV6c3CKPKlz318fXqgPDHfta6bmz9UjMr+GUo+E9YmQwEixln9Q/drcTvnBXTDgNa75ci4sqKidlT20v5HaD8PPIJB5t5d0ZJlesaU9brvEKFpvs01ZTgJjRePYbJolowmRU8g8apjSpBlTUrzGE7VOp6vNXlKNZY6zUWINJMxGQ5bZyRm3GwWoOa4kO+Qm+1dL1HgZaJ3GuRWVbGlzSJKl8+u17ryhyEn7B7NmVrYNXJHXjp83+Tg0fJr28wdze1e6Kv+Bi1iDDP3VsGgNzloZX/Sq5TUUMg== 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=vcft2Rd+FceGboMQ891metKsRjEQzg2Y4zDqbdImk5c=; b=htnQpS/iZG9L72T3ZuQvA1IXq0j3fHd4EkETCc/ar9sIL2GrIl8T6bR9O7ylkSkINzOwUyLud7Tz9BUamdt8AYDckYN9wXzfSbmaGY1rOzw4vz0Hr7YrYXgbQld9IEVHjKFFvRoiEVu+D5ZcSZDv1SWqHTjPZ1ymcfSuIYmF1ZU= Received: from MW4PR03CA0326.namprd03.prod.outlook.com (2603:10b6:303:dd::31) by DS7PR12MB6334.namprd12.prod.outlook.com (2603:10b6:8:95::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11; Wed, 14 Dec 2022 19:54:48 +0000 Received: from CO1NAM11FT101.eop-nam11.prod.protection.outlook.com (2603:10b6:303:dd::4) by MW4PR03CA0326.outlook.office365.com (2603:10b6:303:dd::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:54: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 CO1NAM11FT101.mail.protection.outlook.com (10.13.175.164) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:54: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; Wed, 14 Dec 2022 13:54:46 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 30/64] crypto: ccp: Add the SNP_PLATFORM_STATUS command Date: Wed, 14 Dec 2022 13:40:22 -0600 Message-ID: <20221214194056.161492-31-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT101:EE_|DS7PR12MB6334:EE_ X-MS-Office365-Filtering-Correlation-Id: 5d5db834-61f1-4f2d-22ae-08dade0d0e0d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Uwov9Vcs/wD6TKOtwHMwQqM2KiH5hz7Fk8gA5IeVdFKefJ6l6UB8FN7kn9fXNhgA64t+iJ+RpuNRtzNABNWXhr/gQwARgR7Oix5jVQytRl0X5NLfJkbSdcKjH5ykT/NQRrw0oDdGgJcV9bimYkfrzMT9tVikIXINIf9lOpFUlHVnvziZ+cJLvL3/P8pAkhXomEz/u5P+Fsigk/rsSAtTOAV0uibsnRvFm31vXB/qE4xhlEabEkspKUg7swXXYBSBTQLcZQJxowZCS+SFiE0tHrja63YxgkJ2u8D4pBqJD6D6P5nw6L/g89OTcTmYgnPY9Qd2Etx/W7SqnMNx7XXWhSVopMzWGwuwQbZ2kbFo4DiZ9KYLsTEKGYCJQQ4qUn4P4bzWDC6mJzGdtq7NOz7O1dBzpcyPsdgcmRT2WeVFVLwwIIFlGaYGpfTUJyZ1/Pyx8kepJREYqNhdQl7IlbIhaitQiqz3dTCdyRIdcyWrNTySiUnchFsgIcnysha4C0C8y7q3KjYbpb/Qnolie98jSvirNVP6ug9az2okXllqk9gZX60q4RW3Ga+C7pJuwZn1jqM67P2BMv8iNZHNnMfVl8AVsBMHTGo+EARnZwTXhIlN2J64FBWNdYGVnE1yqZp5QuU4xOXVcWo0tI4j8YyO1563VmhHUF8SHAb1gXf20Q/ihX0LCXvCmgDmiil4B2HhwAc7JX3fmiGMgu+aBHplnzo4dbcPcC6ddzIynbdx1EE= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(376002)(136003)(396003)(39860400002)(346002)(451199015)(46966006)(36840700001)(40470700004)(54906003)(2906002)(316002)(6916009)(81166007)(36860700001)(82310400005)(86362001)(7416002)(8936002)(356005)(7406005)(478600001)(44832011)(83380400001)(40480700001)(5660300002)(8676002)(82740400003)(40460700003)(70586007)(26005)(186003)(1076003)(6666004)(426003)(2616005)(41300700001)(336012)(4326008)(36756003)(70206006)(16526019)(47076005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:54:47.6467 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 5d5db834-61f1-4f2d-22ae-08dade0d0e0d 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: CO1NAM11FT101.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB6334 X-Rspamd-Queue-Id: 96B1B80006 X-Stat-Signature: um44hwdomx9qb56g3e94ffz6ohcjibpy X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1671047690-345028 X-HE-Meta: U2FsdGVkX1/oGdARK0/k29vX+HhZk+SrK1iIYMcKgDVmJ42dSg8yZoWuAke/QW9CGvLN7hXll0RS0wooHXNqWFd9Bzyq/KLFS94CWGjq4EnIlFFE+MWCYtOQsuaqr0B+wOh65Ie/241SYRwBjLX9Ll6cvwpYxsqN+C8Uw0HL4X8QVrmFxPrkdynM5S1uq0uJaxwq0KGbhapa0psVlZUIEqYWD96jQBMgzHxYD12hkFXV1CZbYSdAFQoRSYy0xzye5OipBXkVp6t9WYtv/2kYzNHNcC/MreFv5DwyllfArSOT7387kR9D0wN/lrjQW4WOc0MUCI5iuuVAhhgtvcPX/IkQmcdV81xfThIptf46ZottB0CsY7xgSS6pYyCVmHJxQiewGZkAP1/dX1GbPd/Ur6D1OAeuR3NPvbR3GpkhT6BCivnPip7cAt3U7o23BmTBEUpumCt5Cun8cncuRjvVD/lxFFNOsB0zcjupXTCIDgz5h9I1JbU60uDbCX/gM7bh4iyjOnhokmaqWy7DcgFM0o9XdvqByPNFlBY+47mJzixwvf73fVLaXTX0i1rqKEwCgRxcZuYkG3lo7yLJbyZ4Xj9DDv7Y9D/tUCNqqLd6xNRk9eun6zKWKq3C7KhNWi4aQPa9UyvOECmpY3+yW7FXdAC0tOzGBJ2c9PO6DQDK6Q+vhFbp0N3+XJxwLJ9do+9YRoS9yxiOES3WrkhWfWgIUlDvjvyW9cDQ6ADiVm0ce5Z5pIQL1AN54Hpy1IvU5yGPesc5X6Tsz1MFWJgutU6aGSanC7hjBrh+U849feVfLC7ELcY3Cnv/wE2IG/13hpKefB727NQus18L0emOt49H16Fml62qBQjD6i3g8kWX6+Cd0QxViSQ1lZcrATZbNPKvcQqJsTQd5bi71a5zR10F/Fug1aKxVg4Rp8GIFouNMn/GoxoVXiNkhaLoh7uXY1gcT7yjYj4FLF23Wn/EGG5 369tojcE s/pNv8xj/9Kzg3wOOBSK95K0YmNKoxHbnQArhHUpWUu53x9DKB/Gu/TANXg== 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 5eb2e8f364d4..10b87ec339aa 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -1750,6 +1750,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; @@ -1801,6 +1843,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 bed65a891223..ffd60e8b0a31 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 Wed Dec 14 19:40:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073424 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 64569C4332F for ; Wed, 14 Dec 2022 19:55:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 097B48E0007; Wed, 14 Dec 2022 14:55:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0216D8E0002; Wed, 14 Dec 2022 14:55:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D8FA88E0007; Wed, 14 Dec 2022 14:55: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 C18278E0002 for ; Wed, 14 Dec 2022 14:55:36 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 7D8421C6856 for ; Wed, 14 Dec 2022 19:55:36 +0000 (UTC) X-FDA: 80241966672.13.2C56A20 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2072.outbound.protection.outlook.com [40.107.244.72]) by imf21.hostedemail.com (Postfix) with ESMTP id 851E61C0010 for ; Wed, 14 Dec 2022 19:55:33 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=qPQUmIXU; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf21.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.72 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=1671047733; a=rsa-sha256; cv=pass; b=ylp/aI38yM+Eti296SUQ4PQjhhKp6X9n+mr1qP7lSRa3PQxexA4bwq6SivY85FEaGcKQpK HIVc79ogj1Ad7AxC8UheA2Qt7SVYrEwmm/9rYG8IESwvpPAQ6xzMKwKd2zYRAfpslT7eVD ACpZrY8+0HNMNDRV4k+NAbQ1exZQ8mU= ARC-Authentication-Results: i=2; imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=qPQUmIXU; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf21.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.72 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=1671047733; 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=7fd60JzeQ8GtFsoFEV9MQELV863uk5jBPLSPX35RE/Q=; b=Ig7yBP+ShJGyx7Wv2VnnMesbDdcryELngzak3ROt+m6Ji8BlzRBRJF7OQHCdwT1mHbveM+ j/ghr7nOjaWyiUZj4JWBqixWt1ALKAdt70MW9jzKU/Rud/GhQ7Jh3eqBFabxVekGuFSLNp OR4ESlIvbsu5uRGoTn9s7+yvEF3pTSE= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eL5SQDb50Gd/7sAuGW0EplB+eZ6ZkxuKpRbs6t2eFx1iUQkbpSK8srWbwBVJUYVzXUp8imnL4v3E6H633Os85iJMX60zNp9jZqiNsyrMwKOiaR99FdxhsrCwOqPeziZhuYdeIWq35FMPHUWg/AtwivbRm5/dFHjvWXPerqpx11TpxFFsklAUPTLUFOEAdW7ybOpQrw6WOgF8Cps3GG+yi9REIADeQA/OqWHzmITtf8fRSOElJnjfndFaEYgijc7/yLhO76bbPy33QyAvwUTpQiQ7yJLgihfMMwwxSdcP1fuNsTG+8B8xC5is1nUiptCn4Qdt8jkotGkp3sDqfmIKXQ== 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=7fd60JzeQ8GtFsoFEV9MQELV863uk5jBPLSPX35RE/Q=; b=UfaLgRDfCwsNP3NELJTVHTp5X8iukbxWvmdiGNmrvnBBpb+WZWGmKWHNUu9iHCWyiEMG8J0h6xvFcfOuP2DBL8LyX+da6wmcJDPg9HPFdPNgzCCeGBUd4MEj2FcWDg9ynjZRKJyMb/qjZBxO4ZaJS/+RhVdIuftL+gP2hGQAJ6AUAyGLW6V2CLP3s3wL9zg4U7bSHdHW7unzc3DPtDn2qHGNjNyqwH421IW1HxfPTmiiTD+yBUk/oBcRN4UNt6NWJYjVOdNfw8U5R5/qT7lG2mrgrAW2nD6wNd2edEDE44RiOaTKtmhD76iOssHkmETTlj68CMI6k7/g/bQl5hmZ6g== 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=7fd60JzeQ8GtFsoFEV9MQELV863uk5jBPLSPX35RE/Q=; b=qPQUmIXU2KDgO+TaO8tCVZjaGvHGQ3NsZnfaVVUUCvuqDV2dH12soVgMf4Fj8lrceJH+BTp1f6arlZO/kwXoE9Wve5m03RTxPxYQWLFaqKuJNhcwE3by9Vic7U56YR2Fm8tdnlvwxNxmCHhlrSFI1B630jjej7jrzD/pRAtI9Oo= Received: from MW4P222CA0011.NAMP222.PROD.OUTLOOK.COM (2603:10b6:303:114::16) by PH7PR12MB7915.namprd12.prod.outlook.com (2603:10b6:510:27c::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11; Wed, 14 Dec 2022 19:55:30 +0000 Received: from CO1NAM11FT097.eop-nam11.prod.protection.outlook.com (2603:10b6:303:114:cafe::91) by MW4P222CA0011.outlook.office365.com (2603:10b6:303:114::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:55:30 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CO1NAM11FT097.mail.protection.outlook.com (10.13.175.185) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:55:30 +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; Wed, 14 Dec 2022 13:55:28 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 31/64] crypto: ccp: Add the SNP_{SET,GET}_EXT_CONFIG command Date: Wed, 14 Dec 2022 13:40:23 -0600 Message-ID: <20221214194056.161492-32-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT097:EE_|PH7PR12MB7915:EE_ X-MS-Office365-Filtering-Correlation-Id: 8ac8da9b-8e99-4898-71ee-08dade0d2752 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: F9WhJwslUyxybdYnzQcQ8I/eHKfS1j2qaWAAuHs/LoGb5AfV/7mQLxLvMdYV/4IwJsvj0BAc70F8du16CD8tu+1d9zOLEAwOw4t8VjFSXEP+RjUmZFMY4JsWINMjeL1lBztYYZ3gJSyeWFQn7zjsPyXvhWPGRBiAF6Eq3/SSYgxuQDpigoRKxUCtfXfvBa14MXDgP3kuXm7w+Nw1l2B8fG5Mxe682OuZehn3iM6y6U1izrL0pqiPcDMoNt/bpz0aeoFEpRF6yyqUIuCCYSF8g0+2CcPOWakJpjOUozLzSrBRc237aZggTnooyPHipOPNZVToHdIcxWD2MaiEgzujSRvkN9H7dOEI4BVgllQxCdVm7QSmjh7n1A5oM8GB47lJuPCYl31PayDDlp+fZ1I+d/6YExkYi+MKqgF8VB2jVXwqfopbq+OHZsicN4cAMMlbufjmQBdqA5rYJcWjjomuXZCjgkYnsQPpyZNMWN8pf++TX+lmIsBTd5YTm6+0pzr54YuP8dAk2qMrNNQHwnT9Hm5bcBzIstbMoyRKAZ67j6FRl9IEoQqkJQyD37IZA9H9L3O0jUUmdDAIyWTpnOX4KDQEguHdHqUM8J9evrg5oUcT5WCU8PeGZ8hhDZTr+cc2hnX1jMZQpSikRz3QC/ET4ChoY5f3gDZmS3l1vtCteL86n41a/PVdVdyOYqzqQeNKqQCgNVv47zwoQFw4S2FzspLS7PA71q1+Gx1pVPRPKVo= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(346002)(39860400002)(376002)(136003)(396003)(451199015)(46966006)(36840700001)(40470700004)(36756003)(41300700001)(356005)(44832011)(8936002)(7416002)(5660300002)(7406005)(82740400003)(81166007)(36860700001)(40460700003)(86362001)(426003)(83380400001)(186003)(26005)(47076005)(478600001)(40480700001)(54906003)(6916009)(6666004)(82310400005)(4326008)(8676002)(70206006)(70586007)(316002)(1076003)(2616005)(336012)(16526019)(2906002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:55:30.0596 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 8ac8da9b-8e99-4898-71ee-08dade0d2752 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: CO1NAM11FT097.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7915 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 851E61C0010 X-Stat-Signature: 7qxpfewrtg4z3cixx3psw1kz88q39sb5 X-HE-Tag: 1671047733-568277 X-HE-Meta: U2FsdGVkX1+IYGGXGEAenilZKQnUm1Huxkzc4O0/ei/tpvZwTqTKt3XRQr7kjCAD2NhNgx9NbPlUjnlXgmozXVDUURnosc78+5iqcP68ykls6eD7m1XEPsZNdd2gsThvpBZ3elkq1BxjmCLtqJARkzJMBEC+tbxuUJCRFc6sywbWT/fFDGJ7EHQIRZ3ZUxzi8e/PGniVzeL70bpxGSoZ4UnvQL+id+z8yqrYpl9oF3+4IcvJhg6yygRudDM8+S9oxGSdN2ZK18fV/D9+AmlsW0bKJHOu3OBBxfzOkxI/B0ykJQ6XC67+X/tHoTBrPHTFugO/8tJi/6aeNWiXpKYbPrUN8qyUr7WwyYtI/IB2rlq/mrsIfAD8U1WHgX4Co7aK2ukEcL463VfECfeP143Urb/b0i66g41RVSOsIH6OEMB8Q9micRlje9jpD/DZtBbjT+SNTn79UCYTXUqmrFPjtpIQIvrLxWRKtcdtYn3he4+Geypo8afC5eTZgx1GRANxjl5WWv+2bqmLqnIUcNWGdmphxaSrgT4yn7MMDvlEiAW2UzbysslneHtPgHzIolD7t+MM7vJLVGQJccKISx5/TAfGuWI5e4ijgEGaSLpawpxvdFdK5TGw5bTs1XzsipGj5nGFzPiA7wXYZIsivY6bbzoaftgW5DdfpJrtU3NtCLLZFutA2Boe/FTRL8juEyC8NwRAiPifsjcAwlyWa7888R3At0MRdR68fECvKGvMfCr00v522/uMSNLCRzlf0iBoDUfOMIXL2l+Y39I4OjsRJdUXc1BAyUPkOgy/s2NbkodrZigF1i9w1lWe8P+nBg8rIvyqPllNPV4cu8wnuz7ScF3cNLNsZUYxAyH3y38KE4jWtFvXxIEFMDKVoDMfjJ7UT2kQJJ1ASYW0nkoP9k3UVvi96zYsgZOAXGkqA809Ut3oO35HtXHVMETH98Y2aDe1sLYzo2NPDptUGIgQ1zP oY8Ibb4z Yc/AViMNuYuE03eMIFcOSuRJSfQ6/Uxqxy9Jvdkb6fqgyZ4pfihrBkVXY9Q== 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..fad1e5639dac 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_SET_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 10b87ec339aa..d59727ac2bdd 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -1480,6 +1480,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: @@ -1792,6 +1796,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; @@ -1846,6 +1962,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; @@ -1961,6 +2083,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 ffd60e8b0a31..60e7a8d1a18e 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, }; @@ -190,6 +192,21 @@ struct sev_user_data_snp_config { __u8 rsvd[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 Wed Dec 14 19:40:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073425 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 5857AC4332F for ; Wed, 14 Dec 2022 19:55:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EF0128E0005; Wed, 14 Dec 2022 14:55:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E78BD8E0002; Wed, 14 Dec 2022 14:55:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CF27C8E0005; Wed, 14 Dec 2022 14:55:58 -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 B81CE8E0002 for ; Wed, 14 Dec 2022 14:55:58 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 921C0C0F7C for ; Wed, 14 Dec 2022 19:55:58 +0000 (UTC) X-FDA: 80241967596.08.F1ADC49 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2085.outbound.protection.outlook.com [40.107.92.85]) by imf07.hostedemail.com (Postfix) with ESMTP id E17A940017 for ; Wed, 14 Dec 2022 19:55:55 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=R7BqV4VJ; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf07.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.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=1671047755; 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=eyPA4dikpCpaB//lg8u/dlw5GszNJ+XYmnU5P9RFUVc=; b=CFSxTnUeQF6jUtBporNEebJrEX5eCU9NnQQ7PHunxMpGRaCEfdlfEhw/7rT2F0wH4oNVN5 r8o2R97bHv9bkOnjDgqlckepSiT+ovuSpdMEMhQ9JMeCmDrkZVsB3z/5IB0HIDtk6nrPF4 oTUa3qlQ0tFBrNAyAGnNP4LgOzk60as= ARC-Authentication-Results: i=2; imf07.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=R7BqV4VJ; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf07.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.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=1671047755; a=rsa-sha256; cv=pass; b=lf+T0o7ChsJoanH2x3hEe5i5OND4CnJD8svYDaCfAtwPEGxYK706GW4DwRSjo3FwuVZD+K RUCS4oVio0uclMwLy7U3QTs6bT5+N55f7xlDJhABfP55KiRjSY2j26p0KIGbqMjoK5VBNv vWZXoj4qOG4pWd+HPTcmS0qNGOru9XI= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cK11vb6meTxeW+oVjJuBRbv38+t33FkmJTeq84XPKeIFIX76vwcuCLwtCjR4dzR6J+j/gyhfrz7js+hZ+gvOqYc2sbbmWCRiV+eCmg+xaVDnzjxcNH/6PQkdB+UFzQ07WQI+5LhhxNWrmkHO6tVRZkCVnDa/TQJuEuKZ2qmsCruS0jtMDVZPotBfK0b/6Cl6WG6uZHwfI2OrA/eHCbzX9YvT2jetaup6Nk0ucjCvSUlZGi/SlxFs/SYtwZ4/dgoySAV43fYu9MTe1EjtuvfluUEM/eu8A3N7yFefsNDzu9FOdSYdjjpQ9Mjzba1VOhhnzOH23D3KLZKwQquGdJK9SA== 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=eyPA4dikpCpaB//lg8u/dlw5GszNJ+XYmnU5P9RFUVc=; b=AymKQ712l2f78FTTDcE0i46rwSBk6lrsw9mO93kCGYCMcPiGIwPvSXC1qRFcwr3yX6S5KtmqdEGNVWeaO7Gpg2N+0mY2hsPcGGMhmsf4nwS5HNz5v6tEgc7BxzAR8ZsMmskNAmRqhukabL+Lczj1xfPpkxJ3NvULDMWggmIOIIxyeMFfpg/fqMHLGt9goBsCoB5Dk8wn9gKoOnP5Vg1f5+hmu7w4myJq5ux1u+1H8lCyWZA0xioNxLDHO8/XrNaIem/ehWvyK3EE+NTtIf+f6oXvhu6emr+7jblBV4CU+oy9vENA3pBs95/GDWV2/bFHX/yqKF2ztzpkXuxZ1IcHjQ== 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=eyPA4dikpCpaB//lg8u/dlw5GszNJ+XYmnU5P9RFUVc=; b=R7BqV4VJdhT9K745gODLDUMpZRQKz5n/8a3SmDuiNt9OCgsRrkkv//XJC0kgP0rl4l+Wc6W+bE5I2j8+x3OZBCykti42NMF/sSrI52s1K1NUFwa9QWEcrqQaQFUdxXvLTte+L0RLITwsD4s6SJ24J0PAX6XSygVIQSej8XbvoQc= Received: from MW3PR05CA0027.namprd05.prod.outlook.com (2603:10b6:303:2b::32) by BL1PR12MB5047.namprd12.prod.outlook.com (2603:10b6:208:31a::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:55:53 +0000 Received: from CO1NAM11FT056.eop-nam11.prod.protection.outlook.com (2603:10b6:303:2b:cafe::e9) by MW3PR05CA0027.outlook.office365.com (2603:10b6:303:2b::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.7 via Frontend Transport; Wed, 14 Dec 2022 19:55: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 CO1NAM11FT056.mail.protection.outlook.com (10.13.175.107) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:55: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; Wed, 14 Dec 2022 13:55:49 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 32/64] crypto: ccp: Provide APIs to query extended attestation report Date: Wed, 14 Dec 2022 13:40:24 -0600 Message-ID: <20221214194056.161492-33-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT056:EE_|BL1PR12MB5047:EE_ X-MS-Office365-Filtering-Correlation-Id: 9c5f475e-4351-47e9-d608-08dade0d3510 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: CKV0dUR15uZvChzEiCNt59C7iFdFrM4IZ4IiyJPq3Y91qrfqS9cbH0XgBCIObWA6X9qmbUd4TFQKcL1oOfapQOzjl9l2779wMEKbfKklSTIlt+2GVeOYktnB65vv3+6U8XLjBsuQHGaMFDNmE8YpD/UzXdnKPsT50+wxbFCy98IEgM514pAw6mHqNHAnxwqfKMMgA9Wprcf+l3UAY/ZWJ2XaGlsPeCUKpnlcidfmK/QYDXCnzMlVQY3DLOOK/Rst84jLqCXsjvHGG1ekI0lWvF8j6ikQPXekqWeGoPkXqM7/TSE28s8776U7Ddpw+76xrRghgclsai0npfvEmXxgCLJvGKg1io1g1ARfl2SxhtmKSsxM1oIh6/FBzBA8WIDV5TrNLAMlVXN74m5YvPAOTmvJe1oWtWMhrBp//tFDY3kcDe0PGOF8zqjkJOy0Lrs22+gH+IvoyOH8pEKHRd/LwTUEUVXfQLCRY22qNQagXnEVuv5TM7Mqjcl7UGrNrjkzBQSJNqt16+jPc/qLI1bIJZWoROSwjkVfiiBFKNBNLF7TZIOxNoWCKX63lCRx5w/wkLLQZoNGc1ZKCPL35GPfmf0kkSZkicBNrxZTQsW9HjtBsohpq1pM950/B7Ge5AVzFKJXgf0zCIIqLKclxvAUyDeG57K50ktUAGPqalhq4RDyjjxztpPJntKYdiQAl+me+6nH0ovXXI1uugRQMN1nNh0DI1Rnvn+/Lhh4m84+xx5qxR0UzDWOzwQS+uhovruAY3tyUQzzb60A4+5He5QXGw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(376002)(396003)(39860400002)(346002)(136003)(451199015)(40470700004)(46966006)(36840700001)(6666004)(316002)(6916009)(54906003)(478600001)(336012)(2906002)(47076005)(70586007)(186003)(7406005)(70206006)(4326008)(1076003)(8676002)(41300700001)(2616005)(16526019)(426003)(83380400001)(5660300002)(7416002)(44832011)(8936002)(40480700001)(82310400005)(36860700001)(26005)(86362001)(82740400003)(36756003)(40460700003)(81166007)(356005)(36900700001)(134885004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:55:53.1177 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9c5f475e-4351-47e9-d608-08dade0d3510 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: CO1NAM11FT056.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5047 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: E17A940017 X-Rspam-User: X-Stat-Signature: z9si4rx6iosgur8c5npwte5q3xatowhp X-HE-Tag: 1671047755-965540 X-HE-Meta: U2FsdGVkX194ZMWHVvIomdop/Wq/a4jSrHXNstnyj79BbQq0BdIb/VUQlRA0ESk9JtRjf24GXfDpWoH3K6TUNWDLfPA90vvkSFiW9JXLs+L08OCRFtKnOyJgFUrAYnEPLligal4MuNT8D4UbrzjDiLsX19FRxyEUwbGIu5X7To9m2BguJQudisjQ++5g0SaAOIeHB9h7JQcwKRnnvBq8f+dRP2g1MPgHlR5ek00P7mdhsmZP5DuBEoGd++fcFCY5197Wx7eFln9OrJ1oPH21C0LKUjgCHgrswKdG8v8+LufjIEhktFTtWr6PFCkprKU6xTFCOYppoq/NSRswnueZy23yMB0g1gneoMrrM1ucX0npQbM5ADyZLtWUb1wV0RoKSw7c9Om4hN7dVJf9xdbkB1lzut7CUpD9XYEkFbt4+/X6fmiqUrxeaM4b2NMP2brTW9uv+r3QdBd8ZNEK9Ub4F23wfNkPsJ0fTFlbvk/O4+60OrYhmSMIEIwZPjrWQ+9nZeBHg4bWbGSSvhQnXEx87uBE5G/h/CH/O7MKCMq53LoCQvgrwg705Njf+C+vaHz/EL+euB6LI6k4HEWhAbVI7Fwd2wAAPZ07fYeh7zwiGWMRgW8u8HjL8osVi5p1ewKbZdArowymUMiC5Z5ODWebs5A/PdIeEz5wLEDxT47yFCkoGbbzJKYm5xE0r6jyMl7hE5SQfTmmmFAaFIGQGXulMIikr2ftyE1RimCv/VdDHqpSP33piB7w9wUzHt4ATEuo5hB6+p20nx7tpHLT2AdfJEM4M+XiGuaEOMGP5Dc+rR9YUOSong+jcnR9w7Hah3/fAJ37ZfKBmpVMEc9o3sk9eAuzbPpLiPp61QChn99DRaEXwQ7l0/ndY5bZZpBeg9uU8w1/xAJoSGzjr155u3wf1GL6XEsRKuXPIeUoRXfYd+FVhHZb3e/e4GMgdZbMuWkw13eXVkoUA+3cW+zzMGI k+/tmABn CtzYM 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 | 48 ++++++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 33 +++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index d59727ac2bdd..d4f13e5a8dde 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" @@ -2016,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 Wed Dec 14 19:40:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073426 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 8BE60C4332F for ; Wed, 14 Dec 2022 19:56:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 32F848E0005; Wed, 14 Dec 2022 14:56:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2B8B88E0002; Wed, 14 Dec 2022 14:56:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 10B6C8E0005; Wed, 14 Dec 2022 14:56:19 -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 F33B78E0002 for ; Wed, 14 Dec 2022 14:56:18 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id ABD48140274 for ; Wed, 14 Dec 2022 19:56:18 +0000 (UTC) X-FDA: 80241968436.10.A1452F8 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by imf09.hostedemail.com (Postfix) with ESMTP id EA27D140007 for ; Wed, 14 Dec 2022 19:56:15 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=GiyBOtfp; spf=pass (imf09.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.40 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=1671047776; a=rsa-sha256; cv=pass; b=VBqEzeRHaoYiV9Uz0Fj1MeQNTWZS4S8ieuZizEN9wK7/SErkAt8dIRRdKBz3AbGB+ol8Iq SDMwFM7aP+NCk2GfnsS5AWDhuqUu8AqVIJAvhlXguHQPH/XRNI408xaSFW1s+N28rL6wjr Tk4K56fvRlAwhwH7Az/P+AjC7ThbwwY= ARC-Authentication-Results: i=2; imf09.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=GiyBOtfp; spf=pass (imf09.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.40 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=1671047776; 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=88WHvW9ADwXvsGBtRWmkyKTWFmgVWRi3HBKItCn1SNw=; b=4i0dGqF8hzv5D8adT0AZY3f7x0qdHfBgUoCEshJrbfhxkHpD2vscPbSElW0OvTvmOkA304 TumuOmPAMRGee6ciDLDq2Mj2ElfJSez5m+2Ea126dgv5xqT2ffCGMQ0BdkGrMyTspnsYTX awpcIKwODpF621oCbeMFzIC+smHxFlM= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dAr3vpwryknawdfv26MjyM1IFvVy8CD9jUuss1PqPZUwLPdbcHmWj+3afBrVEY2KXjzf6tZPscH7PGoA2QFPrnV4ntGlMY3k1KpXY8oPdzPJr3JmrNE7IHdWWmyRwvDvznDkA9MDKekotdUiV5Y7Jltkc+i6S+QuUjn54QA/fB1in7lrxrpR1YzNkZuk2lhU65ZBtLI8ShFli3ocvw+K4ensbI3VW/+EWCxraxDLut7uNhHB+eFHjmzan39oBmsWIRim2vxdj6SCURipjlVJ+uH1Tu8gbBgLC8IN8P1D+wV1QmRC426uv9ZUk0aV4QWFES6V1Ewxr4qUp9acMzpDaA== 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=88WHvW9ADwXvsGBtRWmkyKTWFmgVWRi3HBKItCn1SNw=; b=AQ6lmeXcpWw5XLxk1B7oKUmzXUSt4eYt1OZaXL3fBkK/Y0LZjgggUthe2G8BD9DGEYJtiVJPBgnQHG7DqL54f3O2Oics5t6nFm5tpaaBQ2uFBiwxHxFhO65vtUk7N6J2qxjQS2GorHCb4kFK5NJVDCZ2LDXacqN/rvnw+prXI6AO7H6tkbv3wdsmzNDjWDh+enXIB650i1O8l3xoSmbl4qb/9hgZWgP3biIUFcN2xg/Ra5BzlsWeV9pX5/nUrTgE8Ip7yowQ1o4YGpm5xMvtk/X81fQ2rKytyzGzXTxKRy54syZ0AQj4tcEmp9ueLIm18XdyVkiDDapQwzepvxp4yw== 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=88WHvW9ADwXvsGBtRWmkyKTWFmgVWRi3HBKItCn1SNw=; b=GiyBOtfphDNB+Ap5T1ka6WhgU7A6mIPhycEseBQqEGBKQi1joRzcQNmB8PvxKK/X0gabGaNqAQeHv9UOX0VIkxbUYBR8ZWNg1ZqWI0p1OvHz9jLs4MiizGikv0SHC5p26i13zNr/6H5NuPHdKveBEm/o0Jrt5jGFfWN9MW8TyxA= Received: from MW4PR04CA0295.namprd04.prod.outlook.com (2603:10b6:303:89::30) by DM6PR12MB4545.namprd12.prod.outlook.com (2603:10b6:5:2a3::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:56:12 +0000 Received: from CO1NAM11FT098.eop-nam11.prod.protection.outlook.com (2603:10b6:303:89:cafe::bb) by MW4PR04CA0295.outlook.office365.com (2603:10b6:303:89::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19 via Frontend Transport; Wed, 14 Dec 2022 19:56:12 +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 CO1NAM11FT098.mail.protection.outlook.com (10.13.174.207) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:56: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; Wed, 14 Dec 2022 13:56:10 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 33/64] KVM: SVM: Add support to handle AP reset MSR protocol Date: Wed, 14 Dec 2022 13:40:25 -0600 Message-ID: <20221214194056.161492-34-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT098:EE_|DM6PR12MB4545:EE_ X-MS-Office365-Filtering-Correlation-Id: 4faf35c1-dc41-47cd-e9c7-08dade0d403c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: K+eekyF1/M9vXMssGj0Os608tV/uINsw71NB5lmNxYziPQMo7Kkl2jxT0mBCQ9BwXc751QGb3KveOln4Ab9ppZAEQjS0yoMpyg9mVJpA17eSFtmMEzrTfx3TfxN13a22CmyIm7uwJ9p8AZy8QHpC4Dd+qPrIohd6a00Y5BvnQvrgAEyD5GG2pFlXL35pXYCJCNoDjl0tHhxx09f6h2BlMfVmNDBITz9Hn+LJnRIq3C0xgGtHRyH4so6bAAGBOPKTn14JpKvS6aymmbo0ucqkolsfgAFgUf7sK7P/XLL73i56tIAOe6ENUh0GMODAJk6EVtKxhbbduiTcIv3/LTWLjM0Au8lKh+YES/TjOvc1w3Egvg1F9qOu1KSPrVCVslldLQn8GhMOiD4Q0kJFCd58y6436DF0kD3p2TiAxj1weU3/o++3185AtpZeryy28/KZwgbayQaK+/hru3hj3bNxLg81Gupgw7iA8kC9hslAO5Jkyno1OpJguRDp/WTv+mNd5viPdCyaUgM6R1GguQ4btGsMLfXP8QaX9An71Jdt8HRs1GC/vu3F3u6At6t8/hUmUHi8XoUxNOBtOt7mikRaAbkGsJ0BIwX8ACbkk8o10zOf170PoRZEooq2uYL+E3xmGV3jOBrYrzmxKcE+MPi+ZGZiZoXO3mb0rkiX+7KgW4Ahk9a2MVMLqvK/M+MnSKC5VIgUp1HVSIHzCM6qB0iWyOTsIDCjThsxuxLSbv/mI8A= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(376002)(136003)(39860400002)(396003)(346002)(451199015)(40470700004)(46966006)(36840700001)(47076005)(336012)(81166007)(26005)(1076003)(2616005)(83380400001)(16526019)(6666004)(82740400003)(478600001)(356005)(36756003)(86362001)(186003)(426003)(82310400005)(8676002)(41300700001)(4326008)(8936002)(70206006)(70586007)(40460700003)(54906003)(2906002)(40480700001)(7416002)(44832011)(5660300002)(36860700001)(7406005)(316002)(6916009)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:56:11.8615 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4faf35c1-dc41-47cd-e9c7-08dade0d403c 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: CO1NAM11FT098.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4545 X-Rspam-User: X-Rspamd-Queue-Id: EA27D140007 X-Rspamd-Server: rspam01 X-Stat-Signature: 3oce6om9imb1cf4jyg8bjyukquu7ufoj X-HE-Tag: 1671047775-23223 X-HE-Meta: U2FsdGVkX1/3hsasesJAeay2u13m5RnOTFJVCUf0sCb4rKTpJmAlNKVEDh2+tcBXR3EU4/Vg/sG/rypBvlqXiBeo/WZRqBcBN8kdJQIcGtDIu4E0ilGsjc+8Fnj2gIESvj0d4ThTYP+7TDrhAJF6Kb1iox9sROEL3wWSSq+ds7O6TmICc8K6SztVXp3gHKGXnEW5vBHN+eMYqYg03uriZk86SOsmUw/fFohU9nnPio5J/vbd7TBNnI35Zen+42HwdlIAIkJSH1HcihKQkuMiAXbR3FDJlLDxoSa6ap+/ZZs/nUCnJN1kcPlesM2KMMcebqsZuaZFB5unGCyk7mGy61FIHpV5RUYzEq/OhJnjQ40o4svOgho5rIaE4dh+Bo6I1cKRD3y2aQvoGC+BfjRwH+7RmSwswwiS+PMdhX3QYYyBQy1WWRsfGj+DhyC3ZccMhScMqRvb0UPcNjhm9Raijku0AodUyxlwb3AsZSsS6PEbWDYWA+lobHU8EY+OdAS59fY7IX/A3N29mq/rmquG+K5F0A7sXRGLC43pUxKd0m2RWb3NEp5XhidZQPINnN7h40tF/1zKmkY6VeyKpgEq1WJT9bt7eEFCg0IexXj/QqmSkpbIwBQEg0zTRDCdljPNgd8zKE3DrZwfrJeKYF3g8LVtiOBSYvDhrVdu26WOUSBXQYnjSbylpGIvEkzDOs2UeUv2QOrme8hHJEqH7dJAgiC0/wVOwHVeORt8hajteB8BrMax+42fc5Gzt67UcLMP1VpJJZt1du9h0ofGb2kIl55b7MKl5ey1Uj6HJSYZMKqg3s+2CwQiNOZyyMmw0ltl5u//H42BNSvZs0E2vkVP8X+jvTdgCZkLVBQiKjXl7YHgRHpG2PAq+FyXzglUPOu0KHfdS/7kBy2F4BgblUCJqxskMJDo+119bMS8FHzQbAoHKa98dO1JwMkPXG6RqmIO8RKqouQBEMAvGh9Njas Dafm1UQz MjlEW 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 6579ed218f6a..244c58bd3de7 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -57,6 +57,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); @@ -2698,6 +2702,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; @@ -2910,6 +2917,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; @@ -3009,6 +3032,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: { @@ -3169,15 +3193,31 @@ 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; + } } int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault) diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index c760ec51a910..cb9da04e745a 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 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 Wed Dec 14 19:40:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073427 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 6CD76C4332F for ; Wed, 14 Dec 2022 19:56:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0E5838E0005; Wed, 14 Dec 2022 14:56:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 06ED38E0002; Wed, 14 Dec 2022 14:56:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E2A018E0005; Wed, 14 Dec 2022 14:56:38 -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 D1F8C8E0002 for ; Wed, 14 Dec 2022 14:56:38 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B61DBC0F74 for ; Wed, 14 Dec 2022 19:56:38 +0000 (UTC) X-FDA: 80241969276.23.16E3F6B Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by imf21.hostedemail.com (Postfix) with ESMTP id 0A5201C000A for ; Wed, 14 Dec 2022 19:56:35 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Jbr58qUq; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf21.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.40 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=1671047796; a=rsa-sha256; cv=pass; b=U0oz86F+kMxFKzsNryzsPQJ0ne8Sl/D1AJDXd4JnYruTqeZ9MZeh2YpBMzKYmyJ/QCbtrX bxXEhYHFwDXaD58IZ44VpWcMi/8pOCMvb+VVPf5q55hf/8cbqgDtVTXK7r3JPNEYM0PHtQ eUcZieR0Oh1OZtxSxs5SmpavnxBhTto= ARC-Authentication-Results: i=2; imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Jbr58qUq; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf21.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.40 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=1671047796; 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=L3Ryree9hpK7wmsj8Cmnp5OW6S0cIpM4e4m1hjfhEpw=; b=L2zjAGuFTnHpGpgeE2ZbbNaWmffSzHwy/wpqSkVOr9HQXiJPppMyJPDxQvXAyg69ucM17O 8YGaIRvpt96/5iI3zHNIPqsI+CnJuUZBVERfp0lEowZ94JoobXn/O0jSAbUUkEzmUTno2Y F9vFrX1bm6uxu3UXGq7CeICXQyrYXKo= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mOQCV37goZZCAOTG9koypOxO5JfZ7m2k/sSCYkcb4e3ggQq/xi+X6FzIWWSjowHdCcxE1OolhM271qtxAqpxpif3BC5sAbHf0H9HH+RMHvlzORjPSEEpD1gWqmOFsjagO1LXUUsUpy9f53eybiTWXRo2wiowsS3MbjoZXsRsKHGc5OaLb3hm11w343/yrNTBz7DneGgzbjWva6sQOV3g03GLSBQuQynwL3ghNbUrWynciMHAt64MdBuvroLfDAT+MfVJRyxsSuFXJvtU6+y66sAKqKnk5/CjSbxEHu/Pl/q0iOUo9UjypcDgamxhXKineotqXoU443QsoY4YNsBLVA== 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=L3Ryree9hpK7wmsj8Cmnp5OW6S0cIpM4e4m1hjfhEpw=; b=Jh0K9ol7GcTMsYdDeH1ViSaipL4eVGi7nF/Ky7MULlenhph/4rr9ye/VavxKhzS9IX69FiimiBxp+OAxjYIfnwnav7WkUUlU2kgquz3apf67dhts+Hn2fYBM4sKinO3V5FKVaby18ZrrgkF/lpmff5IfgfosOPjQBDp1vXulU4SUioJAkWfkBg/wW2Y7ZhEtoxR2GCOJolzi4EwMod+h0eJWs1I0YwK5LpbZgCqbmen3Id8vKSOhWph0ikuE/f2TVa3gMK+d+Y0Xi5LHW70eYMgaHY4v7p6LbK6LhVZ6s5LdlZWSt73MV6neYJLnieLn3wSdHvfnT0AN6pLy8jGw+Q== 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=L3Ryree9hpK7wmsj8Cmnp5OW6S0cIpM4e4m1hjfhEpw=; b=Jbr58qUqG8ReQtJFf78Pa5npdAXdqxCr5rHBCorOV4BWJNvEqm2nURKA6XyCvYQX9O6j7NjrcaLpqxc8bUSu9tABPuVPj6sDE4Q54jNx7/qswm6a5BPIO5tGlJUfpW6eIQYnmc13ILyXOYe9VnqF0lFKLt7tUFBU9Q/g3lR8xQI= Received: from MW4PR03CA0350.namprd03.prod.outlook.com (2603:10b6:303:dc::25) by DS7PR12MB5791.namprd12.prod.outlook.com (2603:10b6:8:76::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:56:33 +0000 Received: from CO1NAM11FT079.eop-nam11.prod.protection.outlook.com (2603:10b6:303:dc:cafe::c) by MW4PR03CA0350.outlook.office365.com (2603:10b6:303:dc::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:56:33 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CO1NAM11FT079.mail.protection.outlook.com (10.13.175.134) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:56: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; Wed, 14 Dec 2022 13:56:31 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 34/64] KVM: SVM: Provide the Hypervisor Feature support VMGEXIT Date: Wed, 14 Dec 2022 13:40:26 -0600 Message-ID: <20221214194056.161492-35-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT079:EE_|DS7PR12MB5791:EE_ X-MS-Office365-Filtering-Correlation-Id: 9f41c332-1e03-4a2a-de9e-08dade0d4cc5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: X9TGCmotSEQWS7JgXPr55j/UvtgVE/sOQYryrLs8BzQ+DQsvo8UjjWH1AuuOoUKze/L2tVehcsRjAdvyjBsfIL5WGZk/vIcUIAubZpPuSoHv4U2K4uIS2MFuBYNy3L8FQevq3lWnQFiQpalS+WhEed8Ie/zumaIf0HnGeo5i5zVbL4Q7aEHB0KlEZcaSoCMaKKXChRHDLSKhXPi2Ag0CSh2APNgwOuklqKahR84a+tUjD7Bx3BhFHab0DqXkKgOQgn/SzWUeCozPNUGYgzlAoHn0sbivcxJPoqJ8GZLPWX/LlnOJkQC3h6GqPqyRSjJUFbc0d40Qq44X9Ll93ZoeNCrrc2Q8MquNjkr4SEX+SBOOO+gHzRRvpWiLKktevVyzUxJ/cWmIu0DT94mYTPgPzXA243eHPKG0BdfklMNPTEtUGwbeQdEyvS002STdk66nWe8lGDhSfcQ3Al732ehqDXTxjyKTbbJQQFrp0MFd4mmYKNQlyFjvbRv6MOaM5YdFcWLXfpYAw+eVfqspOnCln90R36LXzab02fYWZffjAo45CX3rNAzM8xace6SAYvrVKVns/3JWPuwv6YPULmjo+SuoJs2I2uxUtsDOEz6VI6kvrFDQW6TG36bdw+V6BJ8HUMpbQV0VKu45bbLuDg1ORGK5dRNfXk248j6xh57B1jPGp5DVQWXb8++xCW/+2hAifzNs1P7IaYh/QDCK/83qdZsbTQhMh23kWzpR0tRFQFw= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(136003)(376002)(346002)(39860400002)(396003)(451199015)(46966006)(40470700004)(36840700001)(82310400005)(36756003)(86362001)(40460700003)(8676002)(4326008)(316002)(70586007)(356005)(82740400003)(81166007)(36860700001)(26005)(426003)(336012)(54906003)(2906002)(44832011)(83380400001)(16526019)(40480700001)(6666004)(7416002)(70206006)(478600001)(7406005)(186003)(6916009)(8936002)(1076003)(2616005)(5660300002)(41300700001)(47076005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:56:32.8761 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9f41c332-1e03-4a2a-de9e-08dade0d4cc5 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: CO1NAM11FT079.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB5791 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 0A5201C000A X-Stat-Signature: 5omnsfhrdwsi9y6af8p8nbx6w5qg7au8 X-HE-Tag: 1671047795-187570 X-HE-Meta: U2FsdGVkX1+vgX4nl/eRbrSgEbEoqCAyfDWTfSMP8U/wy8H3IacD41RUGfqmH18XsQ0FrYWV86WqS4Lnus0vbWUaEzZAvrk1NWqby6NDAr4SXPwMJNv474hizzc08aMfphhi6Il7f9os4rmE3DDmQBTnvU8SOfjpoxopVPBPoYxdWKe7gnH8XfOOyUYoK+pJPEHHBrUo85JINwPbJHsKuISAXqob+N2Ap4X2QcvWamM/g1wxC6f2TFnk/NUg8WaeLo7WflgVZZwLuidKMjCcpwsQ9QTXnG2GoZ4+VLO+VRNaHSy85qloMpqDVK4Lbe99trpzAJyOpyUuWjm+8RpTP8YKWeuLLPdsVrzu0uLpEJD4imImyw8LRMXpLXbfgBaSGz1XP1fEvyNLjE8ZAnOVId6kkYbFqCLoFxK6YMd1x0rDx8gx1/YF/bF59RsnQozNFxBUtDBpthK54NvqTkatQh5x/UA9wvYdttqo7CbzDh3S7jo2C59vbJBNFLBQsm3zVnvAUFhEDgIz6vpv19wVp/CBBqnuq85Bf/MtoiuXehhF4q4Po9bAWCiy97oS0OYKkEul/0858VuryVdcaTF8P2XIHM9pvCmX+8+l3qo0iNltxbafIW894HH8UGAx95QUp//vFuGrnlyuAlnBsXj5V/yVc/sgV7a3PPn2OsihJakedNao9plhOcanLdzIzWrEveriPrlo+xPV9DQhSNnZeYt7NfIVN/Kj6wONG19WYl0bJ0cmP7qhXBNGjN4VgE0R1/agTN4Siy5vwHBIaxdofC+oBH4UuD+MhdgF/wKGSmjskHTkr3pp08HCyT0ZVY5oOhqB/Y1uz135S1KJQ0+zhBLgcZ3Q6vV2tzKD0mjPQ2Jt8rDkCQwWsGnIWHyPKrDy2Y5vP101WF4zCYbTTPGGBgx1e7Tu4FcT9HiSzkPZ5zo3KhhZCkNGIFgCvgQlsa8jujz/CDmemaqOrNLr0HD TffBd0Cr 82lww 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 244c58bd3de7..82ff96b4f04a 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2667,6 +2667,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; @@ -2933,6 +2934,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; @@ -3057,6 +3065,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 cb9da04e745a..1f3098dff3d5 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -654,9 +654,10 @@ void avic_set_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 Wed Dec 14 19:40:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073428 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 6BF8CC4332F for ; Wed, 14 Dec 2022 19:57:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0E5198E0006; Wed, 14 Dec 2022 14:57:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 06EB98E0002; Wed, 14 Dec 2022 14:57:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E03218E0006; Wed, 14 Dec 2022 14:56:59 -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 CF21F8E0002 for ; Wed, 14 Dec 2022 14:56:59 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id B7275120A6F for ; Wed, 14 Dec 2022 19:56:59 +0000 (UTC) X-FDA: 80241970158.28.F08293D Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2069.outbound.protection.outlook.com [40.107.100.69]) by imf06.hostedemail.com (Postfix) with ESMTP id E6011180010 for ; Wed, 14 Dec 2022 19:56:56 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=ro9+8Fj8; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf06.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.100.69 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=1671047817; a=rsa-sha256; cv=pass; b=cXyVfzimAnxDSyrF8/j6QhPosGwFeEBq/Tx0FvvdkyWEsu//vLFCpzH31pKYKD/rR48mRL 8nh8uMHoo952vkc6vkcZZ3R00La+83c+OMVHdUYbTeEu5pTT8bpKys8GoXLo0+9/1yRHWq fTWnP1BEEQXTQlVcu2wZiGqQDPg02UI= ARC-Authentication-Results: i=2; imf06.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=ro9+8Fj8; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf06.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.100.69 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=1671047817; 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=LeifTBbDa1KVpZpgd13JQktLoRSv+C6/qJfNPJKBMxw=; b=3BrFkhrnxF13lAUuGFWGPw+trBUNXxmQ1Hi61oOTHBBpp4I2neQE8V9stK6Wmn6Fk64j92 SBv0qbCdxaSSUGGaCmcJ9uyUKmwHoV167+VrGiyE3/V3v2fidb4yUWti7mSWC8fN9NrmnN bV8snQ5fOYT4InCNe++OEsOtY5iH7ik= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CIzMGwPzYK+wxJPzf2X7RTAgMwI9xZORPNpWKR/eYJBgCXcAYB3U9lFXe9oVtJQJ+mtzG35v/VqlFo4YpQ7RjHDl/jqAOwXdPpw6Sc3hsIQ+8BTJ1V8iLF8WiOJwjESBFxtCE0JlFn8MUqtSlcVn3/uJZVi7aStLvIzRkmDRgp/QJMMy+zC7u3p72VOJd41s/rfKOrzfysIdm3OOSeJzPb04YQSJygvKyGVYUY3aQD4YQbbEiMufNf7MRthcSo9mOlNsvRBXKRa8XABd4KSaMnXrrX3xdscjOZy0qk9haOA6zKVCeeBGCcw0ik3QJSCvZJaMJAdScRAWmLJqWf1bDQ== 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=LeifTBbDa1KVpZpgd13JQktLoRSv+C6/qJfNPJKBMxw=; b=inxXymeqFVphvtEvsNLfFCFe2KWfkWWcpVJN+HBbjoghjGYvG1TWn/gt6P8rTll2TA3978/WrM/hq5qnVJdo1Rj7NKagRkh4i52EFTgib6SYMha9/8Rk81CThkDD78OcXDsJuO+z7J++XvpBPnxUlj71vUTz597358K50xetZHM7yhO0EcJWMDossXjITtuh6iF0u1Ll825YeB2adD+NAnWha678AAz/zHFVecIgOu3oP+qbuFIkrWqr7CH61wIwo/YFzke+/Cw9Ra1MBed+lIX2PDorQ5c5gzLuCA3m9GFR12Y91sfSZDkT1uRVbwDO0ExzPprSj+0oW1IgKqTxOQ== 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=LeifTBbDa1KVpZpgd13JQktLoRSv+C6/qJfNPJKBMxw=; b=ro9+8Fj8vFrJ/s3xkwXKDhyHniY1TX5VRQJydn+mDv6GwaKefeesA1ySlBFcNZepYGd6jZ0q5wrwlaTYfjYvPyQjiv6WclUPpZ7D2G9/UA/5n6txwrQIANdxWH1tNNjl9cEz0XrzrShsTdozugq7uxmlb9wBEhybzzxmMJ7p87g= Received: from MW2PR16CA0057.namprd16.prod.outlook.com (2603:10b6:907:1::34) by DM4PR12MB5200.namprd12.prod.outlook.com (2603:10b6:5:397::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:56:54 +0000 Received: from CO1NAM11FT057.eop-nam11.prod.protection.outlook.com (2603:10b6:907:1:cafe::94) by MW2PR16CA0057.outlook.office365.com (2603:10b6:907:1::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:56:54 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CO1NAM11FT057.mail.protection.outlook.com (10.13.174.205) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:56:54 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Wed, 14 Dec 2022 13:56:52 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 35/64] KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe Date: Wed, 14 Dec 2022 13:40:27 -0600 Message-ID: <20221214194056.161492-36-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT057:EE_|DM4PR12MB5200:EE_ X-MS-Office365-Filtering-Correlation-Id: 41248a71-0f29-461a-1390-08dade0d596b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Bcbt0EJUz8JZjJNyTejuvLrAHv8AQ5iilq0ampzsGH1kEXhJAzXncxnAYiNlutINfZUscuw3VX+JncLKqfFETNL4RFZMLCV/MpdQpMXSV6himu30ITWCJf1AdrT/hnwbdMU6EihyCb37MFWS4kFEvT7Mct4tf4fc+xVks6hdOu3p4Vz07lY3R1DAvJSxA7acPUX91h8UTQU+x9vZYG6wXkLRvNvrBKwWA+5ETGYbOPdG2vgyZ1J221UxICZrFMDUchD8eYnEZ22HaIMCbDZhz3pTr3Ly9rVaNfV2dT57Y5846PL9C4g4m0/va0cYzk9hdw9gNJzM3zXCWweMqZJtxpLT+rbcP48fL4YjXFNstxBduGduAjp+e7+yU5QUMRymhMj3BsPMw3GYtJN5y11JVqIIQqtBvFySwOeJnSEcHTlpwc2+ScrXldwuerUDWEis3TX8/E8P0Jm3HcNnVTKO30pDTT+rAozc838NZSbxw+xBlWathsXMRkDMXd2n0uQR4ztaFNhe48DqIV6ijZmIB4kjkaROtJxgIS5vR9oi/gWt/NLq231dByyCEiHp9qB5tpqysvgTs/uSNtb1pQHuQMvPhGdLcvv/UHbwyq+XNeT6u5y7VL9AYugmd8ocXKWrDTF8v6tCJYoQ+vqi4rghttUomYL6fxCgCmU7jla2FUBWEISodt0tp3WlD7C1U7hCgl0n6DWsSj58X2QwUfMCdaYi4zY3HzbcvWhX+l6DOv8= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(346002)(396003)(376002)(136003)(39860400002)(451199015)(36840700001)(40470700004)(46966006)(36756003)(40480700001)(6666004)(8936002)(478600001)(82310400005)(86362001)(82740400003)(356005)(81166007)(36860700001)(40460700003)(41300700001)(316002)(7416002)(1076003)(336012)(7406005)(2616005)(5660300002)(16526019)(44832011)(186003)(54906003)(2906002)(4326008)(6916009)(70206006)(8676002)(26005)(70586007)(426003)(83380400001)(47076005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:56:54.1071 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 41248a71-0f29-461a-1390-08dade0d596b 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: CO1NAM11FT057.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5200 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: E6011180010 X-Stat-Signature: cor7u8zpgrng81cyuuydd1nxg49w9g4q X-HE-Tag: 1671047816-653059 X-HE-Meta: U2FsdGVkX19/GPG3csfN6XDthQFB+1p4r47lhDfMFNUv2wpPD2SzhyjrtlF3gtru36VFQr2s1yL6Au/CoCJ7vxSCwN61suifKPeUC/HZhQt1QUQjlclIj6b7/+X8RsJBfio4gslE8g10K9+3GCFRhQ5pzbm+B+Bl4jWfbkUObBAim8URYultPR6E9Kvirm8sl0guFDQuj3SER+oO/vfc3+Xsbzu74XAp7QVVXetXpA4eEualWypWfKsUVGSEB5mBYf//5CwVF8Mjzvn0suUSu88SffZOG5sEJ8noTEtxr7kI02CTocwBkudy8+1T2hAlWZ8f4kQRPjuu6bIbPiJzaVIEZKo4Q94gLX8N609XgioJapTY4yIBQ2tv7B/UpDAD+WIV7DCmH8QvfG5QHfYF1rHw/IjLSCEBRNDo9ksW0ZHw0adYs0whvttGHDQCc18rbKVUMbRR9WFhIhs2TtQEtlvUHUTNYqKMxZyN4IwXUUQjaYleJObDJsMvG5vc6UtopzqWmjVYT26CXcxidixLvNxeOA3L1RflMgp/WkkaHXzeGtON0acn16QVdJ3SBigsUUD8b7PfyPCkm9/HbbqMSPcXheGryBap68tD4VcVZDoW/D+B3D3vL2uYwXOIR5M3Z5TNr8N4uG4NfaT0j3nZ9G0YhXbiKhQiSNTsKa6vKwDM7eVK1peBIgB80+mL+SNlb1UbbJtu1pnao2xS8Fga863qot/zhv1H8QhtMK5+vT6/KOFZH9ej5DD7t2M05Vxa+lAKnxO78jfUPsch2SulyOAKgO8F5Ap7jt3ifg+QhDrmDMRDwxCMxsGDpoJSLsbV5s4bAkLGmZxpyfE4+fv+orVTWvX08zJ9xlfEeJ6BkIPY8UuKZ4+2gD3tCZbKpyIZavnsvtKEV1Rg/ZGI1Idz1MszW/WTiet1JyH9L9zwD4YvrorAKxkEST3X91VCw7VVb0namDxIcwbY6/qmt7q 1TuiERDy BzmQ1 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 c71df44b0f02..e0015926cdf4 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(private_mem_enabled); KVM_X86_OP_OPTIONAL_RET0(fault_is_private); KVM_X86_OP_OPTIONAL_RET0(update_mem_attr) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 9ef8d73455d9..e2529415f28b 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1722,6 +1722,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 1bb63746e991..8500d1d54664 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2581,7 +2581,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 82ff96b4f04a..0e93b536dc34 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3234,6 +3234,39 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) } } +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; +} + int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault) { gfn_t gfn = gpa_to_gfn(gpa); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index fc7885869f7e..013f811c733c 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1360,7 +1360,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; @@ -1369,7 +1369,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; @@ -4694,6 +4694,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 int svm_private_mem_enabled(struct kvm *kvm) { if (sev_guest(kvm)) @@ -4830,6 +4840,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, .fault_is_private = sev_fault_is_private, }; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 1f3098dff3d5..ea9844546e8a 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -684,6 +684,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); int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault); From patchwork Wed Dec 14 19:40:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073429 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 279A5C4332F for ; Wed, 14 Dec 2022 19:57:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C08448E0007; Wed, 14 Dec 2022 14:57:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BB8918E0002; Wed, 14 Dec 2022 14:57:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A0B778E0007; Wed, 14 Dec 2022 14:57:21 -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 912468E0002 for ; Wed, 14 Dec 2022 14:57:21 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 68944A0FA9 for ; Wed, 14 Dec 2022 19:57:21 +0000 (UTC) X-FDA: 80241971082.26.D84EB72 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2085.outbound.protection.outlook.com [40.107.94.85]) by imf13.hostedemail.com (Postfix) with ESMTP id 6B92C2000D for ; Wed, 14 Dec 2022 19:57:18 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=EIa3CCEk; spf=pass (imf13.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.85 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=1671047838; 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=b3oFK5H6oMp/qtECE/tp0Qc0iKoUNBA67OdiIc9I814=; b=7jh0nx+ZNox6UmrUMeI8EB8+W9oZ5Dhm7BqczEKf7ggNX9/4P6ehOLE/i4lnrJRCJGeyLl +7rfdsskcZPe5CTfPignnTFDuCkw0tak13eHL3LvUJpmKwOOTfsyXgf3AnJvZhRtTgr2oW ANnZIxEX0v1fkGOv4ixQj2VgFgrgajE= ARC-Authentication-Results: i=2; imf13.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=EIa3CCEk; spf=pass (imf13.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.85 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=1671047838; a=rsa-sha256; cv=pass; b=M6SzEs3584drPXpYzUfQcjTiU3V7ZtFhK1sta7xIXuk2nxHJkzskKJslHTvWW8QxK0L1G7 cZYMb1pmhatSTOo2CR5/4OwSEDKNjVPPs7UE2egcJJdyJOutcfWj2uBh7EamFqvEy8vJrR +bw/vpBJEYuAw3i+4wC/xzjOOv0ew+E= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jC63+ppZ9dPdWNHB9gmHX/CMvEh1hvSTN6i6mjeIajEBQE0AEe16MpSOsRpicKBQDwsy7W/t56QczcMIl8QPLcb8EYPflTi2gyu0jwgDq57HMTQAwTFomE/lAX+pOm8IIrrQIBozPRcJ2wOEczHjs8rKjjhuj/pJe++GTS4g2/0Fliu9gdkhQ+Yr7+3Z57iIB7dz9WZGKeN7FyOaR9BgkzlOHACHA+D+rDzp6UbpVyYcCTN29aHp/NxwwrbzqDoCH82US0kjU7oim1njsnANm3EMv/nygiqIwXkL/l+gSd2NXYppK2FHYrdtzf2UQkTsj+FR+8/hGg/56/dN+aRkOA== 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=b3oFK5H6oMp/qtECE/tp0Qc0iKoUNBA67OdiIc9I814=; b=j4Cp5lHBvd9gsQDzIuXuT0kzNO+ZEFBnTUk66Mk61BnUN4Dv+8UBIp4OkkeFREDN2i2AQ7wfPTtPgTPdEQpWtGjYDkuDz3qx+lwXRpM9ueztCG3iYyeAV9WB1HAOIfAAdTEd1Rs37tOQpitg/urMU39Ug8sQ8JiXjwkwUDtEMdTN0uCVs6SZt2TfEyqz6GtG8paHRwtyPUYPg13HYC7oqQpJEbCfbY3zYQe6bGpF/fQju23SmW/e21Wuog/Y+VWFjiAf1GWA/DWLCx0Gwi9FRwY6WFFmB7wgUBz2Ng6kyYVqI250a8OaCuCnAyKNsBS9JGP1pW1b4q8QDQdb63ChAA== 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=b3oFK5H6oMp/qtECE/tp0Qc0iKoUNBA67OdiIc9I814=; b=EIa3CCEkgmSapx+POOE6WCgpcpK9/7XYfWyUU7H9ANOgllZzkGegBdfDDL8xe4KQUDDpjZV3xI9K9j0nResujnMm6v0oBYfgb82z9Kd6zzb76f2hR6YFOZ3GZ7s/feaEmJuKt8J3ut3FuVxYlQurL9IEhGWNjUubYFvoLBNXXCY= Received: from MW4PR04CA0032.namprd04.prod.outlook.com (2603:10b6:303:6a::7) by MN0PR12MB6247.namprd12.prod.outlook.com (2603:10b6:208:3c1::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:57:15 +0000 Received: from CO1NAM11FT104.eop-nam11.prod.protection.outlook.com (2603:10b6:303:6a:cafe::d9) by MW4PR04CA0032.outlook.office365.com (2603:10b6:303:6a::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:57:15 +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 CO1NAM11FT104.mail.protection.outlook.com (10.13.174.220) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:57: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; Wed, 14 Dec 2022 13:57:13 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 36/64] KVM: SVM: Add initial SEV-SNP support Date: Wed, 14 Dec 2022 13:40:28 -0600 Message-ID: <20221214194056.161492-37-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT104:EE_|MN0PR12MB6247:EE_ X-MS-Office365-Filtering-Correlation-Id: 0a5b99e5-40ab-43a8-bd7d-08dade0d65c7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: l8aTcKZg+y88+cZMNS2xwhwnqoAkNwcsiQCD9BTGzvYDpshnlINFJN3NundMLD2mdIrmhEFXKC8aQfHIiGDHNEze94QcjkPdyOoNMVAQZtUrgfReA4Mf86N5O+DrYuhZV1gmZXAK+z6bnhOBwYqFEPD21ziPSQaZX6NLeaYtIwa/3oQLvNFWyV/Ar+FPCXvbOs0s+zzqzoXDQiTRDIi77WVIakAXKTo23sFlpSEw1W4WGJlbv6ssI89j7AwJgkLd8dF8H9t5kJM+kzarLUVqJ72ESo+Z8/iGVEFKT54db+A4j585Wv32FDJtGO0ZxJIr4sm0EOtPrZE+wrAhz8t62/HHvrt3xpIpF+t2XIx8fxtxfs/YQwdcyfNpVQfy+6rxZ6A/teNMhWxWCZsxloB9NNo+zmceL3ocJ5LVBbw4jAPhGoYdMUMgr4KRLQnBufQl1qyblYo/jgR3bsBjmG8vkqwe6D1ScJY+Kw97KHjkTBo3IVrEfCBjx+ypYptOYV4QFgWqqTwwyFx9HZJpv/qhWYhLyS4MvsbEpOS4rid4/oPPvqqKfGidNVk5/pthZkeHSeQOwekYOGqwvJexYcC+bRD3XPGK1jukiMqBm/b6DzuwSXXV5QQVQIsI9MJ22TQpNBGaVn3AKkEM+PXBCRpcVXRc//lTcGjv3bKNj6cdZ9JOUI91mIPW5k5fpXafppzDrnej7A5RhD1xj48wtVb/7RJDW9xsJVMzR7wLVjnf1rI= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(39860400002)(136003)(396003)(346002)(376002)(451199015)(36840700001)(40470700004)(46966006)(316002)(36860700001)(36756003)(40480700001)(40460700003)(81166007)(356005)(82310400005)(26005)(82740400003)(86362001)(478600001)(54906003)(8936002)(6916009)(6666004)(47076005)(83380400001)(44832011)(426003)(7406005)(7416002)(5660300002)(2906002)(336012)(4326008)(41300700001)(70206006)(186003)(16526019)(8676002)(2616005)(1076003)(70586007)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:57:14.8470 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0a5b99e5-40ab-43a8-bd7d-08dade0d65c7 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: CO1NAM11FT104.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6247 X-Rspamd-Queue-Id: 6B92C2000D X-Stat-Signature: ss35h4dsep1bkd6cj7bwki8aojetbcq6 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1671047838-111947 X-HE-Meta: U2FsdGVkX19uJOfRkMkS8qMn6QP1UZsYaY6dZapghYjxIVHRaGMIXylandQyYrzqtE50SYjMR/qwsD0U2Wx/FElJ/ZUN7bmn189KZTNZyI2Pz2rRYp5OmCLzQWxc6bA5ssobdTz7IJCfGr65jwtxRCJueeBXjEPFzvP9Qvh/R9rWtV/AjfQleuxygsjbbEhC+UEFqMFHZrcUT1DMJY0+G5TCEaMQOitz/aDIzq4miTKUHaOu3PVoK25I1XJ41OIcuyMiRd34PT06vN4bItsm7w6ZCW82bfhbDOq5TqDccEzSz5x6eCRgjlnnK6TEiZRVFL3e5cqso9woLOmZLs/vpxJIVqUOX/rW3wdRgrbSnYTQYxtWv0IL9f8Hz1QxQEfuPF1LJe38FfnETXVYPgyUm/Q/a6d0s5BCYJVqLLUgm/6nz1uWBwDgQnRGrnqnbRTNDbi4qqHbLwDKpprZU3mZI5L/y07jkLQFwypIReQJ815xLy89dmfxnouLD4x5i7avPcxTRnlnbhshPz7CReaxgmEpXFJd/Zfk2VuLnQydwA3UjZo6YUrYJAx10HiDK1FjyH/9kJ5+i0xUDreD6YU5BEJ4Uwhp16E93jAsFGBqapSe1DqRLlU8cRxmLPnTxWk6pzJUXD3VZTl/ZfK/jF8ikcS3XmY5JhNAUhtrD6gdEJVoirSkk+TUfWASm9FcKiViptQd/QuQ5sSTqnKLT8yFNCaltYqsPj565syvJk09mc1R4bQ4jun3Kmz3clwFtZNh15ZPt2yPwdt7bWcKep+aeaLnWKazGeGDz/y3f+kxIsiPNOYXVA46W1bQEUz3LkKgqVXQPosvt64fIb7UZxDkhhHG+YnIRLMT3zsa9E62Lc/6biS0LQLzDSWU1E7cAP89zLgV7yphIRLRhJkknyQHREnOIljypKrjNF0cfe6OEvZyyBcOzQh3XA6wbz2J47CiaC/FATtPitRoX7T5nT/ V/ykmCSt Wz/Rk 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 0e93b536dc34..f34da1203e09 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -57,6 +57,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 @@ -2298,6 +2301,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; @@ -2377,12 +2381,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 ea9844546e8a..a48fe5d2bea5 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -83,6 +83,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 */ @@ -330,6 +331,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 Wed Dec 14 19:40:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073430 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 C6A24C4332F for ; Wed, 14 Dec 2022 19:57:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 63B4F8E0005; Wed, 14 Dec 2022 14:57:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5EBCA8E0002; Wed, 14 Dec 2022 14:57:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 48CB78E0005; Wed, 14 Dec 2022 14:57:43 -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 37C1E8E0002 for ; Wed, 14 Dec 2022 14:57:43 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 136B71C66AD for ; Wed, 14 Dec 2022 19:57:43 +0000 (UTC) X-FDA: 80241972006.17.E871A5A Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2082.outbound.protection.outlook.com [40.107.92.82]) by imf20.hostedemail.com (Postfix) with ESMTP id 28D7F1C000B for ; Wed, 14 Dec 2022 19:57:39 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=lBBP4kjv; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf20.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.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=1671047860; a=rsa-sha256; cv=pass; b=ZpTvengweUhnKNnq0PQP/SYiHxscYi2HyF/Ui0mpQOcTV4slKVzKDTy3LC6YWt1D8w6/NT RmZCQXW75tC+4cNdxeMgSUBy/ktgRRodctkHVoCBgNq1YDVEYN6nldlt2ZgPn9SQ090gbH ppNY7eZSXmsgVhO6b+G0tb5eCVDRNW0= ARC-Authentication-Results: i=2; imf20.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=lBBP4kjv; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf20.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.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=1671047860; 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=D60tz+ftdXdZiCmZ7XbBtHBe2fPSUEmTcsDkq2v56QU=; b=hNKYTBrjCQtKC1mD7++eSnsa/CFtWJs/789ZkNm2yuWBDAtZSWOLUz7PQq2tOGZdyxlCUe dF2rYXBEGUwvrdVSn8I8SgeEodybBeSGef87Ecf6zcAxuhjArz4B65XbMr5JZlsLFw0HBJ f25mBmQxaSZixoehPMcxwPN1s5hwrXE= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=avyqhqcVe7+7HNwtEW7KC95g5ohvWhbLLtnfwDv7cC5hjQqpsCV5m7yMAxUwL+DoSxVfdA8QmZIdvsVCcJlbquEe8qno7XudLMLsQ1rYEE9r2NJdIFrQ/hv31UqAlRyjZwt3b1kx0GEcFbToGg8ZcoX6vMn7TjwHw6L6BzM2KL4qHDw5mJsMkR6WnJPssRbN94QgUtpmeJpIHOA2/+26Uwvo+k7ax/hal7IZ8qpikVqxhjU4cqeuqhBkr0XfC7GdIoYrcroFajfxfGT7WXylpsGz/L2wZ3AIM2MzoKVGD9rI6IkTM+upfKij8sitOmB28YL16kj8Ey5wF29dM7A9Rw== 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=D60tz+ftdXdZiCmZ7XbBtHBe2fPSUEmTcsDkq2v56QU=; b=kwtMpCc+OXnJu0LQ97Hqmb7hVl16ZSUvzOwfAJtl6LW6GT0VzqC2UzWzys1u27QL1w+dyV1ia4vy3eixFeG3ReZatch26171REcbSajUuOmove76jQhLyJjNQlvBa15fMEWlbVE2GEwQepD3Zv4C3B1ngt/5fYvA8qMzNwiwt69c0E17NbEZpYtuSWgzkZi6I0u3aZ5QoVAmTMmjKwcFtst+qfc9ZxL3D/S3NSRV+9dHYw01oyjLpFZfAlWulnvwxkuQf1d/zEt9hkhm2XHTt4UM/YzTy7OpXJ8pGsyL8GDmo09NvD8JeiAYR1Hl/pumhymUP8FSslScTudTe16BxA== 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=D60tz+ftdXdZiCmZ7XbBtHBe2fPSUEmTcsDkq2v56QU=; b=lBBP4kjvASSYRgzmIPbCB1Jh/ZPRZqHLyxTEuIGwR7yiSzHm3zY2K0jH2f1ZckB6Fc/63JotuSUxFv8ZSGJ6YnS5p/VxFEP49lBTbC73gUz4geimkvUJq/4bK7Ioj0FSC+VyMWZSKxqK6PWYVrl3/Y4mMn7PSBT12mGkas2nEx0= Received: from MW4PR03CA0273.namprd03.prod.outlook.com (2603:10b6:303:b5::8) by SA3PR12MB7860.namprd12.prod.outlook.com (2603:10b6:806:307::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:57:37 +0000 Received: from CO1NAM11FT114.eop-nam11.prod.protection.outlook.com (2603:10b6:303:b5:cafe::3f) by MW4PR03CA0273.outlook.office365.com (2603:10b6:303:b5::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:57:36 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CO1NAM11FT114.mail.protection.outlook.com (10.13.174.103) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:57: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; Wed, 14 Dec 2022 13:57:34 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Pavan Kumar Paluri Subject: [PATCH RFC v7 37/64] KVM: SVM: Add KVM_SNP_INIT command Date: Wed, 14 Dec 2022 13:40:29 -0600 Message-ID: <20221214194056.161492-38-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT114:EE_|SA3PR12MB7860:EE_ X-MS-Office365-Filtering-Correlation-Id: f66660e0-9955-4cb8-d5e5-08dade0d7246 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hAPO3W+/GASilihmenEdCtr//6r4ylCYSuXyL804DuSg6KNozv4FNu7Y12gn/F3rcj9qrxWHC9j+9oPAJlU7yoGYIyOdFj5J9juh/58g0hUz0cvNCHHI5sjVsBgOiCPS56zu/Ax0UYOAL01vz760RHNP5hW4aAEMAWFxVVk2EhGiYJqq+vWvpsBUgKzzKIXwyrBK4HB8yJJEPDJndvmFnusXuWo+rXRf1yk3+nA2QzJehx5xxs/eijAf0MVhy0I9Gb+TfDqutiInJt+ljnkCjnJkTMdJyjrEF6oWqIfuq6qHDMF7EqLj/LLzJtltebI/+etR9wo/PNOSWjuai1vgQaSTAQYtCAoQLGHIqB/83UmE3RTUOO59GGO4zIIrDotRhGHO9+M6bkKPH5U78M7arwCswidaemheNui8EbaPGkGHn165QhV4IjlxcOctUxawg3uxu73jlExkAUOJZRerzlFd3TMdyVBeYoCV6iL2Erh8QOXLMerWJpe2WMP2m6/Ly3c2icgAliY/DPwc1OPXmmCkqGYNGFYfFy0/00I2oK2UEgNavApycNqzS6vSnLcTo+xsJYUveFDz3W7nJmqhQ8LPVLBLtsKIfda4JsdlreNgmVfkg4IAoy3swmIg+UI5geyhHg0lDTDjMGjJO+IaDlvsNcNEFbZzgXk07sJ10k2uL7Yg3mB5MNZfrHw4cV250llBrCwQHXHdWHF1UfzqwMcOUOE4VK6rc/W71k+PdwI= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(39860400002)(396003)(136003)(346002)(376002)(451199015)(46966006)(36840700001)(40470700004)(40460700003)(70586007)(2616005)(2906002)(186003)(426003)(36756003)(47076005)(36860700001)(86362001)(336012)(16526019)(44832011)(54906003)(83380400001)(1076003)(6916009)(6666004)(356005)(478600001)(81166007)(82310400005)(82740400003)(41300700001)(8936002)(8676002)(40480700001)(5660300002)(4326008)(70206006)(316002)(7416002)(7406005)(26005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:57:35.8126 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f66660e0-9955-4cb8-d5e5-08dade0d7246 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: CO1NAM11FT114.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA3PR12MB7860 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 28D7F1C000B X-Stat-Signature: xq964s9yrdejmxyqmdruumz4f41c5pt8 X-HE-Tag: 1671047859-819411 X-HE-Meta: U2FsdGVkX1+AEJUb7Jj9Z7lrPrgDOIIygbX9Hf31cJRWeW6i2d+pp/kSzzpDzizMbV9GyPtXmjCVDvuI4NXGcUfD+r//P4xhMz73b0NwlykHgW/t5zITU6UND2e1+sr1/wHLCtqlk01bBOT3mPG5+iDJz4S8elNWvY5G0pA1j5EL2hTDGuGlEms5pgMmIG3h9DsAsIXCPLbeCTMzryqlvmS1eCcusGTH+NR5KN6P3aasLz/dfrQ9FvaB/tBmDJpTr7J/fZ7Y+KN7i6FeyOx0CloSVXnqnm6Uq0hJv7hZ0Uu5Zlb5cZisf8S2s8/j6NF7HzpGkG4xEJY1o06ub7zNcDGa7h5LPbiCFH+Lcul0Ne9p76erJwPMRKWzS3SiHai7fbvyKSRaPFsosjz2XDXCh3STejSN+V3AxUhcx0R9E5K5Ps0YPDjLYQmYoKtvh8VlRNbWy4JyYIJF97Akl+7OT09aQPGqCNC+ok3PDoifkG8ZCrXr7NLGwXEbtkGuTYmOjoWaday3I1UxNzZyiDjNhYrb5F2+NM53LBClqwVS+fqrz0FfzKkP/Dm8wlFXToqj8hYRQceMVpN6oC+5gra/mnjiwy+JDInY4GNbIIdcMwgV+5585zSltcAXU+yYRab9gy6U2qYZ8jidANnJg71aNMYSOwz7MegFIaiaeAbR0xBnHGQ3gicuuEXX0JwuWsubuqZPLu/Mn8DXswrSC3caj0rLs2TSGQFGydKpa6kkhsZ/7srK3+2ZlphYMi/AEKROp32pFmi5E136L35uy6gxR7cTLv0Sjei0VSBxZcQdnO8M89YuH70+sH9GXyKqFLwWy8vqA/sj4SPuxKTamuIOyEz7moFjRPDvzKqk6upWja7O17zZ7s5RUNA1vT2ZHcdb+jyv6b0B8+Ulr7IUjGTyu7KWMAeTBBMmGCGQ1HDTpQSJeqCZEUKINj2LeVXYvxnaZZWbb47FB8XD9ps8brK VB5aoS4x pBiGTejsJru6yUsd63q45aIbHwJaiVwrwIRCflbTR6yF6TwW689ItyAkmcA== 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 Signed-off-by: Jarkko Sakkinen Signed-off-by: Jarkko Sakkinen --- .../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 f34da1203e09..e3f857cde8c0 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -247,6 +247,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; @@ -260,13 +279,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; @@ -281,6 +310,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; @@ -741,6 +771,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); @@ -1993,6 +2027,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 a48fe5d2bea5..379b253d2464 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -80,6 +80,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 */ @@ -95,6 +98,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 cc9424ccf9b2..a6c73297a62d 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1938,6 +1938,9 @@ enum sev_cmd_id { /* Guest Migration Extension */ KVM_SEV_SEND_CANCEL, + /* SNP specific commands */ + KVM_SEV_SNP_INIT, + KVM_SEV_NR_MAX, }; @@ -2034,6 +2037,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 Wed Dec 14 19:40:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073431 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 815FEC4332F for ; Wed, 14 Dec 2022 19:58:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 243648E0007; Wed, 14 Dec 2022 14:58:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 21BBA8E0002; Wed, 14 Dec 2022 14:58:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 06D9A8E0007; Wed, 14 Dec 2022 14:58: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 EAA3D8E0002 for ; Wed, 14 Dec 2022 14:58:02 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id A392CA0F9E for ; Wed, 14 Dec 2022 19:58:02 +0000 (UTC) X-FDA: 80241972804.01.5CE7B17 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2075.outbound.protection.outlook.com [40.107.223.75]) by imf14.hostedemail.com (Postfix) with ESMTP id CD310100007 for ; Wed, 14 Dec 2022 19:57:59 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=G3MWOjYx; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf14.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.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=1671047879; 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=FzBw1ZxlfRv0VZ1/467rS9sM/N4vuXpCESj/AlJzwR8=; b=iwqgpsvLjn1UKvHZczo4CM6aLuXiA5VxPmPHyDJDU3WETeEY7R7mytI2zqWsHnJ8+6K+9p r1STPewVoNUpXtaY99j9A6JCyEC5ShzQxshtNKggbmYh36xDU7RqAJSOMyPpeiqTibyUg5 jthZTheQqGC00yflFfY2IbJqSaPXICU= ARC-Authentication-Results: i=2; imf14.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=G3MWOjYx; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf14.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.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=1671047879; a=rsa-sha256; cv=pass; b=Pf0nm4w4SqdERpXoVz7WJSPF8LIwpZvuLpQcWfhrb5s11RCmLbP9JIjmAHqZycqOFEbt1k auCDjExEJIswWgA4FmmQCkBCkmE3dacULsKsnTYdpZRrRQ3yOx6D+IEDBpuqFT9ny00GRJ poKAAfUas2JU0hhVovonm729bTSzfBU= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QSRh0p1/9l8oexpPyskKG57hYEIC6TPZ/I1eLHlXFEwAMNiOqdifSxvFF6BOwLROpEpyWBqOoXzlS1s96/HhczgiYZDwegAoYgSNtpn+7U0cHPK1LEWosQr6GLhYfaUvr0qFrOTxxLJt1i33TwDcYMhtn1wb1oSC15sBAkYOgrWYuH3KR8npy9+61HLnIgMuNtkVwo87cIDMbYWS9u5xhA0T4QVNvk5LDlLfl3FryXxRyjWkAct2fKN+9ru19FXisv0eeix3wkxEyB3PGOzAj6+U3xWLyNtSEOT87dOBrVSkj5wzxSTqtHQxu6iaN+Utrq7j1wofjiu/7f3Zguma8w== 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=FzBw1ZxlfRv0VZ1/467rS9sM/N4vuXpCESj/AlJzwR8=; b=GTK99xLVDl+S4fnXOOov7T0E1mZkOMiRzXqS1+X0zWiJbUl/SRSc8bGrdwXah+jkLFou6CIkWVfPFUQArKUUPTsIyKNt2Ecx/ZYwdysUcHlp/IU4jnAQ55FZHLCYstOM29Wjs0g1BIL69xqoubYHsNKfkPzG7om0pixM2BjWF5Hfkw5ta2/b9s06a2GezOdn6bvg5bpUiHN/ht12l/r6zqzGEDhnGNGnxHPuIcgB7XAngjpsqyhV6AO0CWZlKHv7oTYZbjgfuyxqVPc6eCqjAziJev2y7WK+vV+rk3zOFsCUwRdia7w4HirjO2TvNzuZAbsAFFRA329+NvqB1e4YWw== 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=FzBw1ZxlfRv0VZ1/467rS9sM/N4vuXpCESj/AlJzwR8=; b=G3MWOjYx3YGL4IA3Y/3SOPcaio0XPEipKhIUIW7174vEYvhLV4cPxSfocVTrCe3vNofdDieINvsVa4ct/rsAIJ5v0YfKTyKC3PcBKJ5qp/R0irB0RciIpOcqgrnyswVt3UlRLwsnfFqs98l6YfkxIlMDfE2b4uikceuClVAmvBs= Received: from MW4PR03CA0337.namprd03.prod.outlook.com (2603:10b6:303:dc::12) by CH2PR12MB5018.namprd12.prod.outlook.com (2603:10b6:610:6e::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:57:57 +0000 Received: from CO1NAM11FT077.eop-nam11.prod.protection.outlook.com (2603:10b6:303:dc:cafe::76) by MW4PR03CA0337.outlook.office365.com (2603:10b6:303:dc::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:57: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 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.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:57: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; Wed, 14 Dec 2022 13:57:55 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 38/64] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_START command Date: Wed, 14 Dec 2022 13:40:30 -0600 Message-ID: <20221214194056.161492-39-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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_|CH2PR12MB5018:EE_ X-MS-Office365-Filtering-Correlation-Id: c18de988-126a-4a01-c955-08dade0d7ebb X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: WJqoKST9Mu88VEzimgfBDYtgqY6MZNnL7fXXI0NohOr7g8TTlrw5L1EJck+pVqlsLpJdYGDKZ0HWF2liqEqGGpunyiC1URC94ibo5rGBsfYggE5mIbFyRU2mP+B81H9FCXfPhEMpo0j9G0e9qNpL1AinGTwZG5vEiHtonmLVrO/iHMtkwoAUFcpv7FiYC9sAz4FGZlZIwz/y1bGFarIWIkxVg1Ak/AJqJD853HNo6qAKD9UrPBdiXou+I9NSM2enEL0ddN7ukDmlff9/MTJkohpaLHKAx6ABSZXobVzoXAdoDEDiLLqZD27WvIIiGjHuc164gTcQE/iX/wy4tcYm6qgJzRg3GBCw9DmOBoENpFbPqgba9vQDBYKNMGmO2SyA/7KuPDmDRfVjw3ZFMTWKmTHuDvQg07N+uw7nPeJZNlW8+/TBTCPpjbIvflFUrNrp2Q9OZb9J+Dmwy+MPfZ3RaqFDRINkv8zFuonqvp6HLPa8yLltHJPQHffe8vEkXP9y0FlJM3hrdfCtMbukOytFrf5G2zXujkrmej+ceaIFURMHSqBW78iBBmhmSeoLkrZFHWgFgelCVSZynnCE+nb1q6bLpYh2oki1ZPl6jpGYdzGz6vLNDV8NwLMDEeU8LMkzHrb8CXQ02VRrq/nA0CUGuBpjes0Nggrs9iOeZNrZWzN8k4KHnRq3IJtStGsPpvUWulT9gcGoe8kFYXqjRfYobN4JnH0d/ynYaAODLe3sIHA= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(376002)(136003)(39860400002)(346002)(396003)(451199015)(36840700001)(46966006)(40470700004)(478600001)(26005)(6666004)(186003)(82310400005)(7416002)(426003)(316002)(1076003)(336012)(40480700001)(5660300002)(7406005)(16526019)(2906002)(54906003)(83380400001)(70206006)(36756003)(70586007)(36860700001)(4326008)(47076005)(8676002)(2616005)(356005)(81166007)(6916009)(86362001)(44832011)(8936002)(41300700001)(40460700003)(82740400003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:57:56.7117 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c18de988-126a-4a01-c955-08dade0d7ebb 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: CH2PR12MB5018 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: CD310100007 X-Rspam-User: X-Stat-Signature: 1ufxtkiwfmhcbtw954gp4fsdc3p3e67q X-HE-Tag: 1671047879-615112 X-HE-Meta: U2FsdGVkX1+IHsSD5jpcEgByg8fzMQrkjABXxWam/j5y3BjJ1+Xef4NqJZMgqWqfxTay3h403cI1w4QYnmuzybUYm5xM3FQjCYNar3OkM2rM6mObQHMQ65Up16sRJVOD5lUSW6t+JlP4bqsl7BhtTZtz7kPe2D656c0yaPmJmuNcfeDfliVTdRtq2wFXdpLgmsNuKqVvj6XRqHsCz3HbCxMHiuVYdx9AYQeqp6KlQXWzDZ1ojOjlbA3MMPQOB4Z+OAn/MSwaEpj7uFWIBxwUYl6w0vI7Wqu8VgQhRRXEi3CeqI/57L+2/P1CbMyvEZ23aywD9P4vgYrgTLmITlcUviPmGchmIOrweOwUEA9m0sitbSQrawS2WaK9dS6xECsrYs3Y3oSp/+HO7xvfkKoCzk8+E1CjeN3cl1HWS2EqnYM8NeyxZ5GinSn659HbfNqByE0xpWoUr57u6iBby0Q2lyjDz9F5Qhwr1Le5v5fclawg189wsUOls1tLrw+OY+4Nj4MTPfPBOarSVxdT+oaOd729LUG5qvj+zfbi4RXA2p9AcQqZJUyCHOKHmwj4eUi2FiZ1otEUg2rptUZGfhHp31f1pB1BGaI4zNczJBkESTg9atSbo+BqWObcyVs5rUzEjEnQTGjVi+W5/XztLGt4EEQuyyoQehWPWxGrsL8hv/l412gG0x9iC73PJ02n3ALHgtc5hPrnLmzKhCel6IZ84Dfpqc+RaKsKmHAAzjrO969wjz1pzIBckMOZbGvvH3xfLQ980YVsGLcE4WCOacYUGV8IvhbFJCj0dMM2HUA2SSuvt/0+uu5tD3veOyTn35YBZJweSQ+w9JKz0FIDYtOW2aqeUBF3+ysBfvTMXa9Pu2eqCQn9A2cKvwuKDwTRAEhBzJeU63cP74NRUwe0F/F0u796yfYHiEFIkLxMsRj+nF8XXdLYONH6yQwUPLlV+Ea+zBCtevhXX7born7o2e9 HqxH16XX an+oW24RXcll8b0P23Tu06erLUzX1kmYlaN1ctHAV9IVQPuSg+tOiSCVgWw== 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 e3f857cde8c0..6d1d0e424f76 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -21,6 +21,7 @@ #include #include #include +#include #include "mmu.h" #include "x86.h" @@ -74,6 +75,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; @@ -99,12 +102,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; } @@ -2003,6 +2011,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; @@ -2093,6 +2175,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; @@ -2284,6 +2369,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; @@ -2325,7 +2432,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 379b253d2464..17200c1ad20e 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -99,6 +99,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 a6c73297a62d..b2311e0abeef 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1940,6 +1940,7 @@ enum sev_cmd_id { /* SNP specific commands */ KVM_SEV_SNP_INIT, + KVM_SEV_SNP_LAUNCH_START, KVM_SEV_NR_MAX, }; @@ -2047,6 +2048,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 Wed Dec 14 19:40:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073432 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 A7DA2C4332F for ; Wed, 14 Dec 2022 19:58:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4669D8E0008; Wed, 14 Dec 2022 14:58:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4166D8E0002; Wed, 14 Dec 2022 14:58:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 290368E0008; Wed, 14 Dec 2022 14:58:25 -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 169A18E0002 for ; Wed, 14 Dec 2022 14:58:25 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id E209C1C66EB for ; Wed, 14 Dec 2022 19:58:24 +0000 (UTC) X-FDA: 80241973728.23.73B8A5D Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2072.outbound.protection.outlook.com [40.107.244.72]) by imf17.hostedemail.com (Postfix) with ESMTP id F0A6C40017 for ; Wed, 14 Dec 2022 19:58:21 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=kged6BGC; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf17.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.72 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=1671047902; 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=h4FSUS3/B0Rm9xArYguEyqZKPzjYjRY9XqH/r8iRhzM=; b=irpO77+2ZY9KsB5o/Hsm/D29vuOCjw7Zi8Kd6fWOadM8ybl0RA4B9G1gpLVN9zF3sPwPkc q30cRPysU2p/oq5+RhleCV6kP+YgQbxSdDu5p8V0CC/RGXbAQLRzkAvLwrrhJOhQIFwLog 4zET62UW9STYck2CcR2arG6nILVyVv4= ARC-Authentication-Results: i=2; imf17.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=kged6BGC; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf17.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.72 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=1671047902; a=rsa-sha256; cv=pass; b=rC61ZR32bb7C+/BXFluWTxc0U6VU33XjroU2ND3Y3lqz+HiUN62yyIkqvtkQ0WXuH/UTOU O5W1VvoHSwC0zQBioI7cNBSbn/jlVgn83Bk3/tT+l5EGIZiUaiLMN8igwWaIQ15g7AU3i3 cpIjtaTkEfjheUo4hzEgfSYdKkVs5UE= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=FnzufEQkG7tStmHliW3OTf5PwDc2K+Pvi9gqW4CGHNitcW5FbgzTS30Th/a/8WnbqosD6pjP5J5QiE2LHWCBpuKaufHkoo2butcmXqSUdgTK3HOvE71beEvGgC0O41lnTYlUcKmzmnf+UV1TlpY4cZWXCnClmzdLoj102v0uq6c7bQ3e0IK57N46OvHmbkpIfbZz/gD+gcp3r7F4MulPPKWRKdoiyP0X7AIQv7uY8AqgbPJ4sx9NvwdOfqBTc5IKC9azoIXF5RwNZFYh8XG11R0hoOv1VMj5kxWTye/NoAIjIy/mGdS8ysjs6ci2gNaX7teBJdFhhGP4HpO257Wypw== 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=h4FSUS3/B0Rm9xArYguEyqZKPzjYjRY9XqH/r8iRhzM=; b=ilL+0ajOjofVEvnXY5PdoXJrSHsB5bn9zn0/5xoPKEZADzOFHJLlqylPac25hfczjjV/SZddhfZ2NvOB4kYnhQyRyJHWmyaJIriNNpvDzmafraZZp+UZH+iydiePz2Ukny5URktQ+YMc1vtWTvR930i8hCp2QcDMqUeg/5FXqlizh+3JIhtVcxOdG0X3kul9SJX102MAfn833JStcbsOvgveBY79Euo76N4cHoPfkBxx5t8GuaHx4ktHldHUuMinn7nomFgioN+fGkrTZg4H1CROykuJsgIlr4J8hmXBbouv3mvWG6IxsHSzlsb+zUM17zb4670yXodu5o8byteWeA== 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=h4FSUS3/B0Rm9xArYguEyqZKPzjYjRY9XqH/r8iRhzM=; b=kged6BGC3qmgZ81K51GjJmxdAIOhf00z9pWj12w1OLgqf1U4zcFrMqKS0IDjsyiS/qb1waL4MtasWW5fBawmDIMfiLKsY6dRajO+ZAGozkXF8bacCBJvVwaw1BCCrlmQ0L/UPL0e2k5wYb3NVz935xW/oQgACVKa5szsrk97M0I= Received: from MW4PR04CA0253.namprd04.prod.outlook.com (2603:10b6:303:88::18) by IA1PR12MB8309.namprd12.prod.outlook.com (2603:10b6:208:3fe::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:58:18 +0000 Received: from CO1NAM11FT089.eop-nam11.prod.protection.outlook.com (2603:10b6:303:88:cafe::dd) by MW4PR04CA0253.outlook.office365.com (2603:10b6:303:88::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:58:18 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CO1NAM11FT089.mail.protection.outlook.com (10.13.175.179) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:58: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; Wed, 14 Dec 2022 13:58:16 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 39/64] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_UPDATE command Date: Wed, 14 Dec 2022 13:40:31 -0600 Message-ID: <20221214194056.161492-40-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT089:EE_|IA1PR12MB8309:EE_ X-MS-Office365-Filtering-Correlation-Id: bcd8080d-ef9e-4911-4f20-08dade0d8b51 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: eXfYqhqjbZb3s1cNUl7Kc59vIqSca64MEvhg8hhow6b/3xKRFysoksYi/N0KsS5WfeBrKS+uzDv6s40xapckkyoxvLZRkq23urvo6pik6WdtUU9/jwJF55wS4StNEkHHf9N14S3tnmyCxF1f5R3UZP/jVQ3dN1q0+V1Ar7sWXyh/pAkH2gGTU3HsiGeLcPcouMVqpD+buItCvB1+Kw7JROucpnHk2LV0HOluJ03mOL4JD3q8hl0ZwvnbN5jjSRCt6ATpAHII88V/PpvggBJEWAttyIO4UZLm864/y9NtylakyBbcIoPwr8yBI0OOieQ/0O7Axee0JIILW4JBurCn9O8d2kTHRYUH4QyFaX6r49+YJduxpANNL1i54lUdTfqtHB0NzPY/hmohdhkaBxwFB8nfWO31T09NNndaBotb4DxMyKeaupc1dpqtjM+ZC7MGGMJJGQrZ3vkWnr3u+n+QeD02cdYXNdKc2FVg/Gl20AzlwNOKBJM9lrCortRxZqCOshaPnPVfva1RUGTxQAbN8p/ORBiQIAfDDI31Pmfi/hcxoC3Tix+l15yyFgllrF41N0D01F+RVC03C+dqOH9AdZbuQemwWF59b7yjmWgj79mblnn3qnScDVo+QAVmBVLuZG3FZ0fWgV1PJn5eassmM4MuxrNGZnObcy9ZXPAHq0bKnCngTBmJuKmeiwNm5heSZr7LQbStTpg7XKXL0VjTcVUlM4MHT0GQMrrZu5HHhxw= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(346002)(376002)(136003)(396003)(39860400002)(451199015)(46966006)(36840700001)(40470700004)(36756003)(81166007)(8936002)(40460700003)(82310400005)(40480700001)(82740400003)(356005)(36860700001)(6666004)(478600001)(86362001)(26005)(16526019)(44832011)(5660300002)(336012)(1076003)(7416002)(41300700001)(4326008)(2616005)(54906003)(426003)(6916009)(70586007)(186003)(8676002)(70206006)(7406005)(316002)(47076005)(83380400001)(2906002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:58:17.8243 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: bcd8080d-ef9e-4911-4f20-08dade0d8b51 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: CO1NAM11FT089.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB8309 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: F0A6C40017 X-Stat-Signature: rpjwt3srp4mm31nmeqjhgnrogu65ob1f X-Rspam-User: X-HE-Tag: 1671047901-696157 X-HE-Meta: U2FsdGVkX1+5HJcYumT3kw4YX7fTJ3h7s2ddhwNib9//sr6MHbYevNXC3wG5Bu79eXoRyxW20qc+118EXSxy+y7lNJHeiX3QZPNbVR30SZbZVHQ+vOdIucuRe4weRQUQ29xbROAvoaNsujmNVndOaKEzE530tU9MsWuWjUukU9s+0FdqY7LJqo1StrU3hGdq6x/3jjeh+lI4X15pjgxAmA0CDXl8dKsDTipj+6AOGoapFOfFn/dd9bASsu0xEbnLhtIfZUp03brEm/G/GNshwUjs4qy6OGfny/1hQnhLqJaAASX7K7subcXtuJCYw1bIeAW2ziEW2Eka6VN8Vfnd2RnZFo02Gr9Y30WltSVp4wzxeLUsg0PTjeR9JGGg/PTehEI717uQnwLN+VPkiw2Uaax0jtr2TQR695Asd9Rco7JbnDvIb0jO+4o0XdKoSxH1jG5MgelEe+C3B3097eAPk/zlhOpoxt8Mws0NvCK1xMEpo+xERNHzKhzDcotKY1YYhZXU02Hu+qHP085UC8Rz4nuK24Sk8mK6aC6PzcEXy/pSVDNr8PlDgLcIhWqXJD1y4WphxChyeWaAIZqRZ6m/K849qnu3T7Ri0VNj/SBSD1k/2b2BVdGtygDwqbPlPBzjoBKZsurxZ95n5nHlZ9HpXvC9W0GYjtvyZOXfwJfds+JoD7fpolNEqYjGzYiJrolycDe8QLMNFpO2bdW5a6YhSYjUa7UE7Kn1axUQmSn+b5d8pXCdsusUsIjKhnXl+YTiqT//3v0IBxkikNFMr/+ZwOTeT/KWouIz5OVyrV/TFqvW8m7NEguQUlfrRj3pVLwZra2yKO+usyEmwMPttqf2tXk0WvyIvcgYANkJ5DO+Qain4AxUlCXBOgKmv+uhiWmKCZyyzSnfhsAghySxc6jzxm51kNZjYKKKci15pKZf7sWLZJE8jd93SyZ2NTcCbSIeD3dxxcPyKJk3JJQ3bd7 FQW6h9Ed pIDhsc/Um9B/T65g9TUQqOA3umswq/Ra2LQBNd/I58f+ZRbgW+Ozy56Hb1g== 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 | 161 ++++++++++++++++++ include/uapi/linux/kvm.h | 19 +++ 3 files changed, 209 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 6d1d0e424f76..379e61a9226a 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -238,6 +238,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; @@ -2085,6 +2116,133 @@ 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_restricted_mem_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]); + goto e_release; + } + } + + kvm_vm_set_region_attr(kvm, range->start, range->end, KVM_MEMORY_ATTRIBUTE_PRIVATE); + +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; @@ -2178,6 +2336,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 b2311e0abeef..9b6c95cc62a8 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1941,6 +1941,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, }; @@ -2057,6 +2058,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 Wed Dec 14 19:40:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073452 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 B5A87C4332F for ; Wed, 14 Dec 2022 19:58:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 59EAA8E0009; Wed, 14 Dec 2022 14:58:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 54ECB8E0002; Wed, 14 Dec 2022 14:58:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3C8808E0009; Wed, 14 Dec 2022 14:58:45 -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 2DC2E8E0002 for ; Wed, 14 Dec 2022 14:58:45 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id A5A071C685A for ; Wed, 14 Dec 2022 19:58:44 +0000 (UTC) X-FDA: 80241974568.14.9F929BF Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2049.outbound.protection.outlook.com [40.107.102.49]) by imf30.hostedemail.com (Postfix) with ESMTP id BF2178001A for ; Wed, 14 Dec 2022 19:58:41 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=ai2nGK2z; spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.102.49 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=1671047921; 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=Hb9bqLc1Dfyknq/y0NpeA5+ye2zRjKVdqAZG3m6ii5w=; b=m6/mp7+sfYZRbV3GlfegSh1vHcznNpMr1aWLNN+kswQ8pv9ufpG1gLXo/jIglSaT2oq36y Mq4vcf5q87Mm+v85+S3dMmjXFMBdss5x6uSt2dWlyZGH4BbpN5nf5lHb94iqe5m2Eq8v65 wkeEp/zaUwlOIgXkLIgMUtcPZVYOP8A= ARC-Authentication-Results: i=2; imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=ai2nGK2z; spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.102.49 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=1671047921; a=rsa-sha256; cv=pass; b=l+PZpSHTAntAq9EA11XyI87NLQgd/uAa3vWpLmDGiR+L66ogoM2UM9OvqT9A3I0hnomfaT GXJdNioR3UmTOJhcMcEsBOiQ5BmIVhk5sEPrp69vFyW2jenytHZ1nZRMg2Vro97pP6TPKg kbDCARfkmQ2vIGxEhZRq7NaOuph2EiQ= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QWq7/IBWdRTSGUaJgZRSGdqdn9tOCqIL1E4KJh3LSCyebM73ATm6msf2IcioPyZY2Bhjs/UiEYKaUbDNxIg+HzyhXQf+TDes21xB1ymiM2MsInL4KnnReEvPT8EnKQnWgItFNviA1hUoydBq6FbbqHUJU4sBzP3WF6AZoMvT5BYxlrieri7yZWT9bW8ine0OXOhmxdbwjbt6qx6h2HyhAO5Je5B9lxk/35nb1PIeusUYqlf7bN5Nj0a3KqUkXRzO38i4klMJx29h3AbOB2lntC3Y6b42b4+jFYZwtZ+QDjcpDPuBQ/Gxa2YVGrsAhbMVdTZb9NllwJzh75TMSixZPg== 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=Hb9bqLc1Dfyknq/y0NpeA5+ye2zRjKVdqAZG3m6ii5w=; b=fr7HOkPlx6cQThBQkKYT/a9p7cSUa5fmpOJbu6UfuM0PTAoVi5J2fGy3GnEPjQhgSH4K6FbQI4aqPzQ6JpKPRwJtwfQBlxiPwMS4Ge4eOQKEpLFQdp/7N7hFLb1lmrPRVgKgTF8VqvhKZdva8TAL+RkubGLG4heag17aYXasHA2oYui5dex6zjV3QY0W9bBceomkO50c5ZfAaKvCR7icxe/AQAPKY6/i5oYPfT/XXEgzh08CLgLU4UGxxo/Ydia04+fr8WjTRet47YNAnMJqwWjJYjgL7EKYaD02/QveZ5LiWyjgoapHr545fh8Ex4wokHl0Ow3kCqPpPvW3YHwjUw== 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=Hb9bqLc1Dfyknq/y0NpeA5+ye2zRjKVdqAZG3m6ii5w=; b=ai2nGK2zbzxXsflslEFYW0rxViJS8X3O8CYq4pjriJvWXbXyLp8i4JFZNo4hEqoZKvXGM3Ul91iKEFNdRR0FB7+QiTbfUq4Gi4R9MlfI8j28gT4pAx7+xHn5McqpUhgvHJMh8f2giqxcW09cabbZ19nHNPHQPbPvKZ32Aj2Na30= Received: from CY5P221CA0050.NAMP221.PROD.OUTLOOK.COM (2603:10b6:930:4::19) by IA1PR12MB8359.namprd12.prod.outlook.com (2603:10b6:208:3fc::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:58:39 +0000 Received: from CY4PEPF0000C973.namprd02.prod.outlook.com (2603:10b6:930:4:cafe::14) by CY5P221CA0050.outlook.office365.com (2603:10b6:930:4::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:58:39 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000C973.mail.protection.outlook.com (10.167.242.11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.10 via Frontend Transport; Wed, 14 Dec 2022 19:58: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; Wed, 14 Dec 2022 13:58:37 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 40/64] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_FINISH command Date: Wed, 14 Dec 2022 13:40:32 -0600 Message-ID: <20221214194056.161492-41-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C973:EE_|IA1PR12MB8359:EE_ X-MS-Office365-Filtering-Correlation-Id: f5019bcc-18fd-4653-51f0-08dade0d97dd X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: RAjM1ywA2qAyUgdI9MuXrHNN+msHJDzmOnMQxMFEBCg02ZemUaG1/Gb/lbL2Gq56LsbPQr8jlAoSfRVgesa3R5/P+GIujx8p7uLwDYxdxGE6IyKE2kzKKWe5LjKPpdGI4sIsbuQiSwxNJy/PUlt2Ulybaiq+ywDK9DsL+t91N9gvYnZCoq0baop2xjkC0J1bVvYsAa1h9h4DfMeyelIP/5HXKu43A19YKnAGbOpAUinx2M+kHpDIfikiG1unKJmSi9mYws/stT/NIP8dLkxpurtNQz4UHUM6jfcYV/nmBNyvjGlHFWghyCXKnj6NrlXSAXP55+3PIQF1NCJ2vbMu1Upxu/AqmvKYXBI6S6DgPRru6GOAf7gDUiFHmA2sKAF2Uq4ZkSdh+mYpO+hB47vdJ3yvQlT4Jut3Gn/9SrU19SEo4TlroNV2N8/D2CngX+MgbnzgdOMAn4ldwCNkDVwWQ17Ss2hSLCoR4Jx33qnAViSlplW8xDSRav0GgJYKTw+b5/HtPTK8uWrVaQUPrxJfJeySxYgFCrmVbvfqpmEP1k9nOwJuT3YFHMwj6T/L5ZjP598wC7fcB8bOE0v9OyMiiWspfNvcRrJmo9skqmSAmZrKB6ujxNqrE935MpKnhCxecy4OFtezJu0psuHY9Fr1MSXhTE84JRZsl18nb5cwvB1bTdio5DTMjcp+4EzEi26uxhjELrj+jVXY+RLcXnPqkDYnCm5aAbO5dDAd3ZW/QRQ= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(396003)(346002)(376002)(136003)(39860400002)(451199015)(40470700004)(36840700001)(46966006)(478600001)(8936002)(426003)(47076005)(2906002)(4326008)(8676002)(6666004)(82310400005)(36756003)(82740400003)(2616005)(186003)(5660300002)(7406005)(44832011)(26005)(40460700003)(40480700001)(356005)(41300700001)(7416002)(1076003)(81166007)(86362001)(36860700001)(6916009)(83380400001)(54906003)(336012)(16526019)(316002)(70206006)(70586007)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:58:38.9058 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f5019bcc-18fd-4653-51f0-08dade0d97dd 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: CY4PEPF0000C973.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB8359 X-Stat-Signature: keaoj9pxawmzwrpfo8hw9mh8qr5a3qqu X-Rspam-User: X-Rspamd-Queue-Id: BF2178001A X-Rspamd-Server: rspam06 X-HE-Tag: 1671047921-108236 X-HE-Meta: U2FsdGVkX19qULZnGbjx/WHxXTlkgpQMtNI+Rws0gF3ThCixrY6jGofhfHSBKlhCaXBY+AtML56ba3UVZMXU2977PmREGCAIel726dwSTizb6/sXtrC/rHr9LEfe1aCkzN/ELkkC/WJZ9FA8HKD44cB/2LKK0q72HGp1fEhUIRBW1bqrgg6HQTNodKEbJ20CUPotei0KUk4VY8uM2FZxKRLeFjACmVtuC160GH0SOQgYgesMHuM6DlNzY5P9sBBqnqNX3lfmQnqBM/cNBhRlh0XVI69ogUpWSZC33ypyBRsJ7vkXjnHrGzGad1pmciUvKEtDpColOwCZMqA1W25YY3YsX6v416o6TkhNKmUrcud/KDAedzI293gHDA2CWp2aEM0c/aSQoE7SPEmvYOs5P8zV5x0hV2XsWZtkG9TPTke74xUDwMRmMJrivp9lU7F2RZTbLtkb38RlIn6xJKXl+xN5TCaLf6dktsT32IAauzq0ZLBR4DhhWa+FJdumBCSF6dLRDLRTSxxuHKL86oeXk8I3nS3eorK1eRTgniSqyTE2zb3X1yHv3BTULSvDhqPamKoRB9FQWURs6QVI/wntcqhYQ8PZSXj1E4C+i0ZFpMaqCAxBH5G3fkZhKMZRq/ahu52izWVzpzWVUegTCuX3XT0//Jyty8WVO1rX9DDI4KSWPEOydlpHiyHcQ/RVWKg84N2vwucC3tcmaMIscgPVGH8MtGYh+7oCM2hcCj9NSfzGY69tjVr3NbViM+kcZCR4GUgj5q3Nw0hLyGEy8XDdjAhvamMJ8eXH+/LivojdAOAkBBKIC2BqINK9EWRlNts5NDAwq+dPeVByo5dDXhhjRZOLC9Svg/CX4m/UVNp033GiOH/H40N6nLu7700JOaR6MewuuYeuy5aG1mkyNUeCYa/G6JbEHatFS8NoozK18oxE4EYBafYGVG37OAeAIoWmgAp+JtmuvbyoZuiSQqm 7Dw2FGCs 7TXsJsmmkDSBvwCLDCAlSkYoy1IQq04CjFZQDQ99qHT23NMMn3PqZ2rgy6Q== 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 | 22 ++++ arch/x86/kvm/svm/sev.c | 119 ++++++++++++++++++ include/uapi/linux/kvm.h | 14 +++ 3 files changed, 155 insertions(+) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documentation/virt/kvm/x86/amd-memory-encryption.rst index c94be8e6d657..e4b42aaab1de 100644 --- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst @@ -513,6 +513,28 @@ 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]; + }; + + +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 379e61a9226a..6f901545bed9 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2243,6 +2243,106 @@ 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 = {}; + int i, ret; + + data.gctx_paddr = __psp_pa(sev->snp_context); + data.page_type = SNP_PAGE_TYPE_VMSA; + + for (i = 0; i < kvm->created_vcpus; i++) { + struct vcpu_svm *svm = to_svm(xa_load(&kvm->vcpu_array, i)); + 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; + } + + 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; @@ -2339,6 +2439,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; @@ -2794,11 +2897,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 9b6c95cc62a8..c468adc1f147 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1942,6 +1942,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, }; @@ -2076,6 +2077,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 Wed Dec 14 19:40:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073454 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 7219CC4332F for ; Wed, 14 Dec 2022 19:59:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1590E8E0007; Wed, 14 Dec 2022 14:59:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 109178E0002; Wed, 14 Dec 2022 14:59:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC4558E0007; Wed, 14 Dec 2022 14:59:27 -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 DBD3C8E0002 for ; Wed, 14 Dec 2022 14:59:27 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id B8F9CA04B1 for ; Wed, 14 Dec 2022 19:59:27 +0000 (UTC) X-FDA: 80241976374.19.7B57A87 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2056.outbound.protection.outlook.com [40.107.220.56]) by imf09.hostedemail.com (Postfix) with ESMTP id CA9CE140005 for ; Wed, 14 Dec 2022 19:59:24 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=nhhAPQHv; spf=pass (imf09.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.56 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1671047964; 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=lIMjY7hiNsb9k1AzgaOD++2qcJNETyyjvIDWnwvrG1s=; b=EG1jI8T6u47GTs3ZsT8ia4gfKItsfDNaI4dRnsHTRVL5ie1SvpUnYPKX3mWvniyc6KWMH4 t+o2rxunb4wGDVCzvigwOg7y/NUCghR6aG+BfYCLTjiI7fpyr6+LIaSTQB1yhrer3BLccf J9218FFBHZJTwYAkhX3Be9Bf/4kbils= ARC-Authentication-Results: i=2; imf09.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=nhhAPQHv; spf=pass (imf09.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.56 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1671047964; a=rsa-sha256; cv=pass; b=lsK8y2pFu5ibfihfP0JlUQsV+xXDjmfCSKnbVDlDFJJVaItaWWaNXecZpcPCwIChtBpV3O FGcq6gAq7numRmErjLU7j5huNFMRIl2TvGNWgd0GhaT6XSTawYtSOVxvKp9SyV5Tih8iyL +CFa2q1uIuVn+g1lo2AtOtj6cGuHb8s= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=iBPph2rYsW63K/ZH6sKjt+BJ3vAJspwRZyxaNnDfkmTBqBQPsI4uChA+gq4Dn9YPexV81yFYtB1q3W39cIKRFAzFM8CDUVmIWJuzmYwDIN1DGiJd1tChV3KJWp6dmGdc/UARtE/3d+syttQwbxRmNNGWfmMQf9qYRMwsf5pnZtJypTizkdYKgKyEZajDgMM9Ixp047pGXiFQtAPX1H76w56asE6E1K9RBm9l/o+Ygt4A7mtiU88r8lJeF6xC1BFbTIE3HqeoYL3hGtNGoMxJWvpcg0YimLz7AjVdg5m3L6JWyA11vF2Pr05iL7byVwevpBt+zXztwN+huGa0v35gJg== 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=lIMjY7hiNsb9k1AzgaOD++2qcJNETyyjvIDWnwvrG1s=; b=eswmslqJ3L0E56MqKxCYOAskGBimLSdRMfNBYptbr+z+DdQCUIaGt5BNIeTlxpYGSES81d+viqGu2xSHZfQp+Y8ih1J+B+AbtTM9earv/cHnJXpotnhuKdTq/x0wngn+wRIN+V7F/LLEj7wpAFQXOo6ASzE8yiIhKBrmX0GlkLTDuRiUacr+vENIkEK2bMRwk1A9NWdU/o739ufQFzrCcCSGAQm2LhCTnB4CrWUijc4Qpmg5BU8oLcwxm+ahmuJi1PlDeZI+Itx0BSKTQx3KlKK8QogMVZqmVwqj2cPqmFKAzq/hCpP4/WPcwX2jWd4Mt5wGpBDyNSxVAArYwR6AMw== 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=lIMjY7hiNsb9k1AzgaOD++2qcJNETyyjvIDWnwvrG1s=; b=nhhAPQHvtMKk7g7ebClLtOfMOY+phoRMsj375cGCk8YtJiggH3S6kxjV29bE41jZSM+tZWPSM69VFnoEvYLUz+X4PT655eWQslB+gRooi9GZB02kjOP/0myQce5L2UgULdeq7KhWZlu8+fd57ufEPWA9Z8e3quZzG/GCsCWZTUs= Received: from CY8PR12CA0002.namprd12.prod.outlook.com (2603:10b6:930:4e::26) by CH2PR12MB4040.namprd12.prod.outlook.com (2603:10b6:610:ac::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:59:21 +0000 Received: from CY4PEPF0000C96C.namprd02.prod.outlook.com (2603:10b6:930:4e:cafe::ab) by CY8PR12CA0002.outlook.office365.com (2603:10b6:930:4e::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:59:21 +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 CY4PEPF0000C96C.mail.protection.outlook.com (10.167.242.4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.10 via Frontend Transport; Wed, 14 Dec 2022 19:59:21 +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; Wed, 14 Dec 2022 13:59:19 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Jarkko Sakkinen , Ashish Kalra Subject: [PATCH RFC v7 41/64] KVM: X86: Keep the NPT and RMP page level in sync Date: Wed, 14 Dec 2022 13:40:33 -0600 Message-ID: <20221214194056.161492-42-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C96C:EE_|CH2PR12MB4040:EE_ X-MS-Office365-Filtering-Correlation-Id: 351b3bf2-4a7b-4d72-75ca-08dade0db110 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hXjW5yhjSp2ViycKw3iv682ex/wd+aEGbQPF7HXHe2guvTW835pbYqEK6KeviVTS9pmlzWCGBgDg3dxxYfxmzOb7bK7f48dDvK+jXzWIuZSFTvvGuS8b6UyPhAXv2ZUtMIrwBpoyFz5iCYZyd97dZddiGocdT2ryKAtomJMVmWI87weu255k+TswsUfINfJpt6L0pwWYzbIAOVO5LnZLXJRsrDebtt6pxVBUCkVDkIfuHOXbavFm268R2W6BW53cCu+oaeG8xCkkTERMlaPXW3hAzNVsogCDDYktGv1XbRC904zWG/wrgNfH4M3X+644vshBaItMVpQXqt91DKRrhEQE/iut56Snz4WbRBV+kNILWLp63OjmjF3oQKLFRbvCQAgDTAhYmx+vgnsojiOLBGIhvsKXcZKM31at2Fgq7WxRL9IMdbNoH/bQ5cuKi4G696VR2TJu/Vpbcoy9qlbcvh7eG8KBTpWYBm9kbUHej90dvM63wk8O3dQkBg9ne/UqM53zwCZEuYKJje5u/9ALXoBqgCuuR/6GxQaTR4onGa5/+1yUreU0mp3CzSfI95417iA8BTxDdxwzyByVlKPRrTqPxk05XCz8tWSYNUAb0RQP42Vv8jCcmIA2JrHTpwOV12e/SYPspD6mPSdjY0XnJ2L434WsNzxgMS6dTEszLs6YiIotcwX0ihcWITphbyuERCt5ovwC9KO+fhZmGUgsMnW+f9O9jw81TdyzMdUR/h0= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(136003)(376002)(39860400002)(396003)(346002)(451199015)(36840700001)(40470700004)(46966006)(2906002)(82740400003)(44832011)(356005)(40480700001)(8936002)(36756003)(36860700001)(26005)(6666004)(70586007)(82310400005)(316002)(4326008)(86362001)(7416002)(7406005)(5660300002)(81166007)(426003)(70206006)(54906003)(6916009)(8676002)(478600001)(41300700001)(16526019)(186003)(40460700003)(83380400001)(47076005)(2616005)(336012)(1076003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:59:21.1683 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 351b3bf2-4a7b-4d72-75ca-08dade0db110 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: CY4PEPF0000C96C.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4040 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: CA9CE140005 X-Stat-Signature: ac7jhrwpaje8bwaqwbxnqugotx3intsm X-HE-Tag: 1671047964-517573 X-HE-Meta: U2FsdGVkX1/VYOH93mXnNwC1aiqkEx4k1poQi7iy22BV1y53b3TvkHYrvWSMNEE/1TA3ekfRVuYYcdkMChZzGohIa0KA/LFbPJCw8wjYo3V18LOLeyPr2wHV5qI1hXlj8jLGS+27t4jvdYimyDhC7LovR8OLoal1r58zwJPWQY59oGw6n3gP/gtLOq15ugnRhJEyjupg4XhNgtwa78+0lVtaSYZ0IOR89dZ6+bD0C/S8kGtpF/cAk5XmF97oZNXcVlf/e9AWtv1eRSXBxbt89CXDc4Q0ZjoXNWebIOIqhVPlMeeNex9jhuQ3S20C1b09vJIbqEvGa9x2V8WK/bT5H+cQ+HFqCwVki3m20zjgaemPsHgkRj8q1qIKQqgaJn6/RTDxQVD09UqppHVWx7BmEtjp2xBXh4NYepeuE7zIJkd6E0pHet+unEelfdbtdV+243APYeU/gAr5mXjRTWoV2yCMz1lF9p/aGkhu4psKDwEvwE78BGf+HhB91cIR2DZUL3yLGAWlETIhXWwV4P67/j7gyXJ8mi/Ej7GbWGQDNboh/oPliXbqWuQ59pWicFRgDRoPKizZiy6w+SINB1QWW2sEr4JV6U0pqP08foLTCeJwd3gD1Kpgo57na9OEYodU5AofarL0OYKo8srGm6xgwqpYbyTLpY1XqZ4BPi8KiQz6UxYAMubiq88ho4AlAr/jtz7Qhx/xCID7HxkCKVqCMK3SRjtwEPvfj4vehLs6DuFmNLY2iQpMVqbbRTh+b/8+hv7hSxQEnwEL9CXcq3tw8pVZVDXE0E7iUA9bzOXcs+iRb58OS831LO23xtVrcaFSRHIjAaGGFurx/usCXHRvvgYFAcogYbG9d9t5B6ifBWmoAnfjkj3+gvor85i9lmbPPzbdDQh21FwvCi2kTmxsRKxf75g7dO5SOp1465+bZJpRdfAO3Jck23qp3a3IhwDeKjBd0H7TP4jLx/Ceuyt dPWZxkoY gzJgp 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 | 12 +++++- arch/x86/kvm/svm/sev.c | 66 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 2 + arch/x86/kvm/svm/svm.h | 1 + 6 files changed, 83 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index e0015926cdf4..61e31b622fce 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -136,6 +136,7 @@ KVM_X86_OP_OPTIONAL_RET0(private_mem_enabled); 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(rmp_page_level_adjust) #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 e2529415f28b..b126c6ac7ce4 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1724,6 +1724,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 (*rmp_page_level_adjust)(struct kvm *kvm, gfn_t gfn, int *level); }; struct kvm_x86_nested_ops { diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 2713632e5061..25db83021500 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3053,6 +3053,11 @@ static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn, out: local_irq_restore(flags); + + /* Adjust the page level based on the SEV-SNP RMP page level. */ + if (kvm_x86_ops.rmp_page_level_adjust) + static_call(kvm_x86_rmp_page_level_adjust)(kvm, gfn, &level); + return level; } @@ -3070,8 +3075,13 @@ int kvm_mmu_max_mapping_level(struct kvm *kvm, break; } - if (is_private) + pr_debug("%s: gfn: %llx max_level: %d max_huge_page_level: %d\n", + __func__, gfn, max_level, max_huge_page_level); + if (kvm_slot_can_be_private(slot) && is_private) { + if (kvm_x86_ops.rmp_page_level_adjust) + static_call(kvm_x86_rmp_page_level_adjust)(kvm, gfn, &max_level); return max_level; + } if (max_level == PG_LEVEL_4K) return PG_LEVEL_4K; diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 6f901545bed9..443c5c8aaaf3 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3710,6 +3710,72 @@ struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) return p; } +static bool is_pfn_range_shared(kvm_pfn_t start, kvm_pfn_t end) +{ + int level; + + while (end > start) { + if (snp_lookup_rmpentry(start, &level) != 0) + return false; + start++; + } + + return true; +} + +void sev_rmp_page_level_adjust(struct kvm *kvm, gfn_t gfn, int *level) +{ + struct kvm_memory_slot *slot; + int ret, order, assigned; + int rmp_level = 1; + kvm_pfn_t pfn; + + slot = gfn_to_memslot(kvm, gfn); + if (!kvm_slot_can_be_private(slot)) + return; + + ret = kvm_restricted_mem_get_pfn(slot, gfn, &pfn, &order); + if (ret) { + pr_warn_ratelimited("Failed to adjust RMP page level, unable to obtain private PFN, rc: %d\n", + ret); + *level = PG_LEVEL_4K; + 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) { + kvm_pfn_t huge_pfn; + + /* + * If all the pages are shared then no need to keep the RMP + * and NPT in sync. + */ + huge_pfn = pfn & ~(PTRS_PER_PMD - 1); + if (is_pfn_range_shared(huge_pfn, huge_pfn + 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: + put_page(pfn_to_page(pfn)); + pr_debug("%s: GFN: 0x%llx, level: %d, rmp_level: %d, ret: %d\n", + __func__, gfn, *level, rmp_level, ret); +} + int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault) { gfn_t gfn = gpa_to_gfn(gpa); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 013f811c733c..2dfa150bcb09 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4843,6 +4843,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .alloc_apic_backing_page = svm_alloc_apic_backing_page, .fault_is_private = sev_fault_is_private, + + .rmp_page_level_adjust = sev_rmp_page_level_adjust, }; /* diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 17200c1ad20e..ae733188cf87 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -698,6 +698,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_rmp_page_level_adjust(struct kvm *kvm, gfn_t gfn, int *level); int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault); From patchwork Wed Dec 14 19:40:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073455 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 1CDBCC4332F for ; Wed, 14 Dec 2022 19:59:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B43178E0005; Wed, 14 Dec 2022 14:59:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AF3108E0002; Wed, 14 Dec 2022 14:59:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 96CFC8E0005; Wed, 14 Dec 2022 14:59:48 -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 85E7A8E0002 for ; Wed, 14 Dec 2022 14:59:48 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 604F6160F38 for ; Wed, 14 Dec 2022 19:59:48 +0000 (UTC) X-FDA: 80241977256.22.0B31E97 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by imf01.hostedemail.com (Postfix) with ESMTP id 885DC40009 for ; Wed, 14 Dec 2022 19:59:45 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=pPBGszfS; spf=pass (imf01.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=1671047985; 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=AIkJ9ONvV/9ix4xqluzIRXKLZEkOKPUvKC22i1Q56YE=; b=Oe/S9fDp81c5CXSWKe+J4/CK6r21k9Bk1S1V2kjnclDhPzVku9fbDJCI1NsF7YQDUDCOsp 56xXRofM9uClXJhun+RLcZiFekM4WDE48zxJu0WCycaJJjRxhtam/v53plBUMG2mFvDJnQ z3XWZA5utd0orx/b2dh5dqyl7b297uE= ARC-Authentication-Results: i=2; imf01.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=pPBGszfS; spf=pass (imf01.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=1671047985; a=rsa-sha256; cv=pass; b=EOFlOgDvWIgygcXRubaQxXXrCz91/8ugdNtWldt3I0Hkq72HeyfoTYnS7KnIVILoJKElX0 /rntW07wnkmsQaXn23MyEZVeTrytld6WbMa54cHSJibaA7t+mJitiEEgJsbcJnYODLkvTf SZ+5tCu5li8Scd59tVnaCFGXju7Lprw= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NiDCMfWK3Ylu5L+ixmIx1+2Ikpjwz6A+HX2IxCbxq2TMRFTrGYYG1+U3BPdHgJ4Z4jvS6MqIXlR4Xa3yVdkTfnOl/zga8wNuGAIXcd/0XuUpAP7ucCdA4X4CiiPBVqiDnJYrtxKT2WzdiCefIsmIKOQGPfemrz+n33lJflMDKzSLrOzvJhdy2MhW0VHFhHcO9pX6E9wBFLATD5d0gu4wHJ/aYjNcF3Q/B967skH5BAiIkewJ+XzIMU3wkGoT4sx72lOrWQszHDFcvOM+B806OaZWV9wNsL3Y/1uuKIbr3QH9hnbRosOazWDVApNDxzwFzf0zU9wE9+Rq+pq7++/19w== 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=AIkJ9ONvV/9ix4xqluzIRXKLZEkOKPUvKC22i1Q56YE=; b=YpRMT8ew1IzBRsvBHFi3FI9G/tncC/nBo66gJ2cMpmzzmEasChceOY4xIS2aN8QS6pI3pw2/Rg2ixyAHVUuklI3vr9+KxjaNZzoEeejZRk2psK6HcbxnILBEQybu33ywhvQ/1G6JoWHpKdtBLydibne9MHPefxn35CT3z2PFluE6TuTQDY0HCZYLXKtu9eflzgqcWwPj/9jM+bqAnoh0I256I+U6MgZ8rqYoeRV8OoOybGmO8hqFUlRduYBeeBJ2CE13JG778upa2vI+suciQ7Qt9knUKLslaWrAQx1VKOvrzDNef+DGvKsJ+DwTbqgb3E1CRNCWnTrlaoaiYgD0TQ== 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=AIkJ9ONvV/9ix4xqluzIRXKLZEkOKPUvKC22i1Q56YE=; b=pPBGszfS1mUrMvR4o6IwiWOlmeufLnSqk1bQvngvcG7qHCj9b6SOnesrQ8ADx9OlRIxWbxPt3oj5cHPuXFJOl08WOkV9ihN20PHEvGf7UGX26VftPJtiM5TuJS1zwfE50NKB21ADUIXTY+GWbxFYK8LxPCUt+uOyH9L4dwW7Tww= Received: from DS7PR05CA0089.namprd05.prod.outlook.com (2603:10b6:8:56::6) by SJ2PR12MB7919.namprd12.prod.outlook.com (2603:10b6:a03:4cc::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:59:41 +0000 Received: from CY4PEPF0000C971.namprd02.prod.outlook.com (2603:10b6:8:56:cafe::c2) by DS7PR05CA0089.outlook.office365.com (2603:10b6:8:56::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.5 via Frontend Transport; Wed, 14 Dec 2022 19:59: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 CY4PEPF0000C971.mail.protection.outlook.com (10.167.242.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.10 via Frontend Transport; Wed, 14 Dec 2022 19:59: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; Wed, 14 Dec 2022 13:59:40 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 42/64] KVM: x86: Define RMP page fault error bits for #NPF Date: Wed, 14 Dec 2022 13:40:34 -0600 Message-ID: <20221214194056.161492-43-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C971:EE_|SJ2PR12MB7919:EE_ X-MS-Office365-Filtering-Correlation-Id: 2aae1591-435f-454f-afa6-08dade0dbd19 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2YJqlKfiI2M6lO95CKZKW6KVBGS0SIHcZEH/cy8JuCeH7/VhGgGfsa1AuhtM3H3ivQ6t+SmQgPhp0ZjfGVFrFuwrr54OGebui+SORxFpH/5CziZ7/E/++26B9Nf3wGyuFFnhevqcbwerPG8tCU1kHlujnWB9VC3UxbK1uzZrugRVYnUZs4aQzdzyzcf60nYNQeTgHj3k4He0VAif+LK6GxCC6SuF9yc22n9peBuCjfhL7447gNmPbatZazsboIvHAWUZT7A0SLH0tT3Sr1GXsUUYS9Ah0TYNeVcJTUyPE/CJpK7KADLMa6rYZNmF5XXYiBoxN4DtRhCF7IFtfy8m9yggqMCFKI7R5wfnfX8OWIxJ4gG96TT0p0GG9LqyJ3tniH4UiiIOtFIjxLlHQlWknzKAF+VZE6CYkfXepLdCDUq4rn+xyut4LdMsBMlWv/CpEeEGj/kduvS8tlcvC8rm9BQ3v35WCsCO1gotC+SKyRmi8Ph7DReJ+9428MzzW5eFTlPFJzXBmBvECkrYt5Cc3NKzv9EwaJu1e4Bj3mm57eAnLvAPBbuoYi4vJdtZw6QLO0j3VOf6sgiwycEvM2wYh+tb3bLwlTmsAuUK+ZaR8KKqfQQtcip4NxbIsGh5i2JYbChwCJMN4i0ESHW+8/hGZq6MD/DD8pP96nqRBkVuJrHonkDe50SfKV4PJ+FSPrQQwK6t6/EyNk71EtSL3zkIrNqnifmgupafbDNk7e9/gyA= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(396003)(39860400002)(136003)(376002)(346002)(451199015)(46966006)(36840700001)(40470700004)(40480700001)(356005)(8936002)(36860700001)(47076005)(82740400003)(83380400001)(70206006)(8676002)(40460700003)(41300700001)(70586007)(4326008)(86362001)(81166007)(5660300002)(336012)(316002)(7416002)(26005)(2906002)(44832011)(186003)(6666004)(1076003)(6916009)(426003)(7406005)(16526019)(2616005)(54906003)(82310400005)(478600001)(36756003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:59:41.3926 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2aae1591-435f-454f-afa6-08dade0dbd19 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: CY4PEPF0000C971.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB7919 X-Rspamd-Queue-Id: 885DC40009 X-Stat-Signature: dfhymmqzfmx84rnqqnsx1nt47mxgubfw X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1671047985-804918 X-HE-Meta: U2FsdGVkX18EV1XlT4SvArja77i7DI+kM2tKuGL6sI69EkuVSyKqOVBI7EUyolQRZbH3XgHE/yiLwBEjpxRimLcnMWuNvsB7N6P1enLzaXyfjInmLCu6YgpDKFMxxLHIOdmrqBATFsVRXQVkr5HNp8dXAOUuQR90letUkIGu7h1zysGv5Ahb8QbGHdWv6H2yh3WKELZYVa+dJUrNdsmSBRiSBEzUkuNeiryCq1xXEZMGKO02QHVeksw2mJ3tRJnLff9zHta2NZWp7qfLYrq3WVTwXHs6DRwtBaQbneh/NpOKdC2Xo1L2TKTxAIVQIyX0NAcGAf94HGwtH/gPXRCuZ1KLUx+9lp5YGW3pgM6uOM8SMrTrpcKQdoLICQUHOC+tk/B0TBxgLK6rNtrkw7Sd/cxp7fXkRa6oWdgEeOO8VcYTjrz+PVVwhBvdsNxRE55V/1YWxydD5C+hpF47qNHyELhGKyLkJL++G7tDbhTkLtqffD/4m/2yXWUt8r19HFe2BPnyfMfxUSSZanmmU4nDkgCxwx6Wc4zstI9mx3gHZpZsLzY6Na5ImFkw2bz3LiaSdrFnfKOmyUkWK8rW9OSWhZAquqhsGdjwfguPXSustJihTlxbPmb68IlrGjikr+X8r690lLlQVZRFNoN6qKHeyT67N4ahxugYjA1W5tqKsuF8wIHyUxJu9QD3Gw0vpsmG8xQadHEIckrV+eFMqohN7SPtfIdHMVrsaV1bBU5ryS0acn2u99ExSzDJquzEXmOJHhZyvrjteTSjrgeJ2g3sK3bQeXEGkWSRgkbQ6NWGvxAzaExjGyF3rwcFBKRExGXzLRcgCfX0nrCJbzwG4pyt18Bs1QSWBlGlJYc0sZmCOz3d6p68Fbu6RvwlOy2qd3dJ4O7NnH7jC3AvQ8pDB67PhlByXocODP06i+MnelBEVWUMsiwQP2AmPFBQKJ/6wuYxrQCabfNuPX5UtON2t5G Hf3X5z1j 5/C/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: 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 b126c6ac7ce4..f4bb0821757e 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -257,9 +257,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) @@ -271,6 +275,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 Wed Dec 14 19:40:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073456 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 1A8F4C001B2 for ; Wed, 14 Dec 2022 20:00:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 835FF8E0005; Wed, 14 Dec 2022 15:00:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7E5B98E0002; Wed, 14 Dec 2022 15:00:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 65F8F8E0005; Wed, 14 Dec 2022 15:00:11 -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 562BF8E0002 for ; Wed, 14 Dec 2022 15:00:11 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 05CC840F77 for ; Wed, 14 Dec 2022 20:00:11 +0000 (UTC) X-FDA: 80241978222.01.B6E9155 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2052.outbound.protection.outlook.com [40.107.220.52]) by imf21.hostedemail.com (Postfix) with ESMTP id 0C9A91C0020 for ; Wed, 14 Dec 2022 20:00:07 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=t126Cxc5; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf21.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.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=1671048008; 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=VCxIbVtnyduFHme2CiqX7FQyEIL4D6z/zq075hUxUxc=; b=1TlJOyON4kHNklqw4UFHWq79GSmGMKAamtKlmchClGrXdY73jO/zz7G2X2KnKM4Wfj3xKR 31cB/grKLC4QPqstnmNlfSg/k3qsliCYPpG+7KF4NGcRGv9vuGYysuV/PpiYDeExuX/yz6 bRthxUvLjjN8f4Tr626Yqaz+v3oXHyA= ARC-Authentication-Results: i=2; imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=t126Cxc5; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf21.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.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=1671048008; a=rsa-sha256; cv=pass; b=QjmLUiAhLWEG3qzhDWWUFU+V0ToVsz8DQ1j4bhohZAw5T14Kuc9OKqo0WFCpiAkL9otbpB XuX81UVXWwtkMaLVK+u9R4xuwl50VZUd8KJ+P5VwPbGT4Gn531dgeASnShx+ig89Bd96Xp FulYLBW4L3qGHu4ooTPpCPunOfk5aFs= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=k9jkOBj5cr1cVto/bdHE+v3hmjA9Mu5jyV+GJr6D7zw7dSqVO1UqpMngts226gCy2WkljSMmPjHx2Np+l962b7Tvs/4oDxmV/ihtZIc2iRGbqk9BVsWWCgEnXcTCt+RsLawCWexF4vxev3AItbygNanZJJ0VriQvUKmKeaN6kL67cX3O45xmoUVI6GwlKYZgH3DUlpXr8rmQPTtcqIVYAZ0gl7fGcbsZnsPSx4KEdL45s98ovRt2p331vpRba1iT08yl/1C4mfjiLHVHKvCkPYXb1POT/CS53Z0HmrWgMUJKaeSXN9wjubbo/xh3O9iArMCMys8869aE4gKjAEnfbQ== 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=VCxIbVtnyduFHme2CiqX7FQyEIL4D6z/zq075hUxUxc=; b=FKYNKftZ1TpaMUTUrVuNE6Ta+z1LhbNmZv3E/Wvd7z4L5KNDYNRipKttXsry+9m4bCeeZJkGJz/DKWbubJDigLOQ8qkcZyM1C5k4yBoJnOJ+RRqgrfZfqsLDaCZAfxjCoYmDEXYKGmLtdr5zsTkYYe5QcgVoxNnHEMvftkWaENBlpnzNpDI7zzs1848QEFP7KXeKQzWm2i/fmmFLkSIF0kI/nbS8eE6g1gNWNnq3nZsA7t4i1PErBXpNdoqhESfCgmkjTQfE2YB0+289uGF0rzlNShlRA5ek3tEYcjRzmeoTI/yHLGuRpUpeGG3umijdkoVwbS9xsGeLqWR9ugGG6Q== 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=VCxIbVtnyduFHme2CiqX7FQyEIL4D6z/zq075hUxUxc=; b=t126Cxc5Xu/k1oljfLZI7BCc3Fmsd7zBTZM1xJsC7RKWF9Bd5KBKejeT1SMUtcT870u+KL2Ya/WM9yhlT1JK1cPwPOeuck4g4sobWw4esPzPBoUAdAc5LNJf+X+IiSLZGzKvNsoAXXd8mZTIeYDV31XeyKSBxlpapkWti+LeACY= Received: from CY8PR12CA0012.namprd12.prod.outlook.com (2603:10b6:930:4e::10) by CH3PR12MB8403.namprd12.prod.outlook.com (2603:10b6:610:133::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11; Wed, 14 Dec 2022 20:00:05 +0000 Received: from CY4PEPF0000C96C.namprd02.prod.outlook.com (2603:10b6:930:4e::4) by CY8PR12CA0012.outlook.office365.com (2603:10b6:930:4e::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:00:04 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000C96C.mail.protection.outlook.com (10.167.242.4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.10 via Frontend Transport; Wed, 14 Dec 2022 20:00:04 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Wed, 14 Dec 2022 14:00:01 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 43/64] KVM: SVM: Do not use long-lived GHCB map while setting scratch area Date: Wed, 14 Dec 2022 13:40:35 -0600 Message-ID: <20221214194056.161492-44-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C96C:EE_|CH3PR12MB8403:EE_ X-MS-Office365-Filtering-Correlation-Id: a7c49e37-5d7a-42fd-8f35-08dade0dcb00 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: VY6oyxhYEyUZlZRtOO5CzC46Fhsku/C6FTmY5afWzVsL/93v7MJ5SSrPvPVFfQMPB7H11nsQb4hB8MGLvNTGbNzUxd9ZJhLAt8S8Lext1Yt1RUu+tyxdcVhwOjlVUAk1aYCn54XN8x3DiEBRfhToJXQpp+fUSKXpLFaIGgid06nBpybERr1n0K5fe7aw5zaxyas18k7DsT3ifS6BedmyHehxx8IpFiYThJx+kSApMoHjyYdRN79gR0niZ1QWOr8PJlEUtfNt39XJIIvw3xyQY1Z/or+ytH0DMWNo+SHqgbLE3E0DctGRtelAbo/i5CYOZSQSCvkDBpPHtO7x1MXCUwMnAbbCBhsw6mZcQXNRkUhjtnWRKRSyHpfaBHkDIg0+qQM5iUmTv+bQ5IKlT+xCir7hAJr82Cgj3BwS6bKxIsL+LiFYcKeLUu2l+mhAvtntjfWZcbr402U+lELmVhKPF8e+k9rjeAzx8y6qgP1kM53RWcUy6NonGkKIZPu3o5zo4QB7IvCKdzJ+I0JyIjoTQshbGQoQGJkQXY2gm6Fw8dxD3eua2nPHUPJukzmENwiW4vUUajqt725wP6PR0xxSe1go4zZt0xt892gjmY3CCwu5R9n1bqXHjLPJ2tt2xFbPpoGBDQ2tiKcInTsOs/LTvduFyghid5ZoYYsGThNYOfRfGfsbzd2PK9R2x0JWEiLqEeu1JAbi3rQgAzS+Vru5EJ+g5gWXtUK4h6dnKtgfoHE= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(376002)(346002)(396003)(39860400002)(136003)(451199015)(36840700001)(40470700004)(46966006)(6666004)(478600001)(82310400005)(82740400003)(86362001)(186003)(26005)(356005)(81166007)(54906003)(47076005)(36756003)(8936002)(6916009)(2906002)(426003)(316002)(41300700001)(336012)(16526019)(36860700001)(1076003)(83380400001)(7406005)(44832011)(5660300002)(40460700003)(7416002)(2616005)(4326008)(8676002)(70206006)(70586007)(40480700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:00:04.7004 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a7c49e37-5d7a-42fd-8f35-08dade0dcb00 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: CY4PEPF0000C96C.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB8403 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 0C9A91C0020 X-Stat-Signature: sxex9zhteuyf7nybgkrxjuett7sd7t9a X-Rspam-User: X-HE-Tag: 1671048007-913541 X-HE-Meta: U2FsdGVkX19+hIK9YvbeZAosp5sp72R8powsP2k4FuaLj2mbTRnTmLmsae/X/Fw6saRNPUt8cZkqrD/lI4Cn7xM93fCsE1OTmlDURtD1nm+Vk7NoAjbJOvdm53cvk18j1vMlcxpYhG0/0vMJP4UngUA6TwIn001SC8A1iaeH7FdYP13Eq9Yx8+uTLT9pB/g4BRDr4+n8xc/eEXOgXfEycCaoKRVIwJtvLVwkCDkgiEMibCOslkGrYdILmzWGNHML9xRXhiZH+D8A2GVMv6DTS1uIi/ezMe3dcVJVH9xnuUVAGxeN7FYua2qYVagEk+2rnkU8Mu3sAXsj4Rrjh8I4ZtK9uO1Fs3zh1dzQ74ervvmL5TqkiYcZaHtYhm8PXSG5v2DzrnZ3/S4kM1XOy/epEYggG8Dcyu5Eskgr1BUAdaVKB/0Km7C9q1ElYnCNxWyLJotNMZby5fIj8OEnXfps1Zd9+lS/lab0JZOPoiVqlnw6CtOp+FwmnWPTwnd+0coq8da/IhzUabh8MZd8kFaMQkcpzbpd44m94WMcZtugVuEQ1UH91+Ix4KfYRtzTlDDK6LL3Bw5FHzXx533PKMiwscN2tQ6DnxIIBGs2gFKtfGIiKGLcFLE9vtWYk3fspDGMhbOrBa6v8omTJMFH4huwheSxpfWGhgiuBHoJnJ7YcD14vi0O+gVQI+16p8q9GlG65BD06WorO3aTeySanyxYL7fpEYCsj3wH6v5duvaKAt4gSJvmB1pVQBLHnBhDuHyRB+G09r7XRCknqJaXYIejeuD/6FsaZn7tNZ7deNxYd5Kr6DKeIkealyG3WIQPHE9ai/1JEnSszZEMWYDG6qDMO2mgVx4TyuVk+REiZNW4CqPQ0DBa9ZKwZ546ScZS1Xkwu84QTtXr7y66TKwIp+njEECrkb1KKz1lvzebxQGov414XctyKQ87g6SRJeLXFL2N6mhSEPBb/KlJ3y7Ptd5 1GxmfNyx bnQ6i 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 setup_vmgexit_scratch() function may rely on a long-lived GHCB mapping if the GHCB shared buffer area was used for the scratch area. In preparation for eliminating the long-lived GHCB mapping, always allocate a buffer for the scratch area so it can be accessed without the GHCB mapping. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 74 +++++++++++++++++++----------------------- arch/x86/kvm/svm/svm.h | 3 +- 2 files changed, 36 insertions(+), 41 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 443c5c8aaaf3..d5c6e48055fb 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2918,8 +2918,7 @@ void sev_free_vcpu(struct kvm_vcpu *vcpu) __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); + kvfree(svm->sev_es.ghcb_sa); } static void dump_ghcb(struct vcpu_svm *svm) @@ -3007,6 +3006,9 @@ static void sev_es_sync_from_ghcb(struct vcpu_svm *svm) control->exit_info_1 = ghcb_get_sw_exit_info_1(ghcb); control->exit_info_2 = ghcb_get_sw_exit_info_2(ghcb); + /* Copy the GHCB scratch area GPA */ + svm->sev_es.ghcb_sa_gpa = ghcb_get_sw_scratch(ghcb); + /* Clear the valid entries fields */ memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap)); } @@ -3152,23 +3154,12 @@ void sev_es_unmap_ghcb(struct vcpu_svm *svm) if (!svm->sev_es.ghcb) return; - if (svm->sev_es.ghcb_sa_free) { - /* - * The scratch area lives outside the GHCB, so there is a - * buffer that, depending on the operation performed, may - * need to be synced, then freed. - */ - if (svm->sev_es.ghcb_sa_sync) { - kvm_write_guest(svm->vcpu.kvm, - ghcb_get_sw_scratch(svm->sev_es.ghcb), - svm->sev_es.ghcb_sa, - svm->sev_es.ghcb_sa_len); - svm->sev_es.ghcb_sa_sync = false; - } - - kvfree(svm->sev_es.ghcb_sa); - svm->sev_es.ghcb_sa = NULL; - svm->sev_es.ghcb_sa_free = false; + /* Sync the scratch buffer area. */ + if (svm->sev_es.ghcb_sa_sync) { + kvm_write_guest(svm->vcpu.kvm, + ghcb_get_sw_scratch(svm->sev_es.ghcb), + svm->sev_es.ghcb_sa, svm->sev_es.ghcb_sa_len); + svm->sev_es.ghcb_sa_sync = false; } trace_kvm_vmgexit_exit(svm->vcpu.vcpu_id, svm->sev_es.ghcb); @@ -3209,9 +3200,8 @@ static int setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len) struct ghcb *ghcb = svm->sev_es.ghcb; u64 ghcb_scratch_beg, ghcb_scratch_end; u64 scratch_gpa_beg, scratch_gpa_end; - void *scratch_va; - scratch_gpa_beg = ghcb_get_sw_scratch(ghcb); + scratch_gpa_beg = svm->sev_es.ghcb_sa_gpa; if (!scratch_gpa_beg) { pr_err("vmgexit: scratch gpa not provided\n"); goto e_scratch; @@ -3241,9 +3231,6 @@ static int setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len) scratch_gpa_beg, scratch_gpa_end); goto e_scratch; } - - scratch_va = (void *)svm->sev_es.ghcb; - scratch_va += (scratch_gpa_beg - control->ghcb_gpa); } else { /* * The guest memory must be read into a kernel buffer, so @@ -3254,29 +3241,36 @@ static int setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len) len, GHCB_SCRATCH_AREA_LIMIT); goto e_scratch; } - scratch_va = kvzalloc(len, GFP_KERNEL_ACCOUNT); - if (!scratch_va) - return -ENOMEM; + } - if (kvm_read_guest(svm->vcpu.kvm, scratch_gpa_beg, scratch_va, len)) { - /* Unable to copy scratch area from guest */ - pr_err("vmgexit: kvm_read_guest for scratch area failed\n"); + if (svm->sev_es.ghcb_sa_alloc_len < len) { + void *scratch_va = kvzalloc(len, GFP_KERNEL_ACCOUNT); - kvfree(scratch_va); - return -EFAULT; - } + if (!scratch_va) + return -ENOMEM; /* - * The scratch area is outside the GHCB. The operation will - * dictate whether the buffer needs to be synced before running - * the vCPU next time (i.e. a read was requested so the data - * must be written back to the guest memory). + * Free the old scratch area and switch to using newly + * allocated. */ - svm->sev_es.ghcb_sa_sync = sync; - svm->sev_es.ghcb_sa_free = true; + kvfree(svm->sev_es.ghcb_sa); + + svm->sev_es.ghcb_sa_alloc_len = len; + svm->sev_es.ghcb_sa = scratch_va; } - svm->sev_es.ghcb_sa = scratch_va; + if (kvm_read_guest(svm->vcpu.kvm, scratch_gpa_beg, svm->sev_es.ghcb_sa, len)) { + /* Unable to copy scratch area from guest */ + pr_err("vmgexit: kvm_read_guest for scratch area failed\n"); + return -EFAULT; + } + + /* + * The operation will dictate whether the buffer needs to be synced + * before running the vCPU next time (i.e. a read was requested so + * the data must be written back to the guest memory). + */ + svm->sev_es.ghcb_sa_sync = sync; svm->sev_es.ghcb_sa_len = len; return 0; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index ae733188cf87..f53a41e13033 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -209,8 +209,9 @@ struct vcpu_sev_es_state { /* SEV-ES scratch area support */ void *ghcb_sa; u32 ghcb_sa_len; + u64 ghcb_sa_gpa; + u32 ghcb_sa_alloc_len; bool ghcb_sa_sync; - bool ghcb_sa_free; }; struct vcpu_svm { From patchwork Wed Dec 14 19:40:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073457 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 2A3ECC4332F for ; Wed, 14 Dec 2022 20:00:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C2DB48E0007; Wed, 14 Dec 2022 15:00:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BDD4E8E0002; Wed, 14 Dec 2022 15:00:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A56A58E0007; Wed, 14 Dec 2022 15:00:31 -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 9452B8E0002 for ; Wed, 14 Dec 2022 15:00:31 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 5F289AB64C for ; Wed, 14 Dec 2022 20:00:31 +0000 (UTC) X-FDA: 80241979062.06.EE91879 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2078.outbound.protection.outlook.com [40.107.94.78]) by imf19.hostedemail.com (Postfix) with ESMTP id 2119A1A0022 for ; Wed, 14 Dec 2022 20:00:27 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="uNFYN/az"; spf=pass (imf19.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=1671048028; 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=XZPzzzS3Y+e+/+jr7Ajwo0zlhoYfxqq2qCs0alPROMw=; b=p9/eQWq47hBWxqwxbz1efMySAQJtesrGeMuFM8w0G6pmLOgoqNDg0AKaGe+H94adcbzsRb x/VQHcWEP2hHWc5aUWE8sMOiOCCilA3BOp66vhfg4MKfb53VQ+sry6WZ7Anq0pE2kjdSxE N/1IJomEFPrAPJuCzaA/OQy5SNo8NlM= ARC-Authentication-Results: i=2; imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="uNFYN/az"; spf=pass (imf19.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=1671048028; a=rsa-sha256; cv=pass; b=ih8GOHh4ldPe5k2n9AlN6XTyi6fkfLSzB3PzdBhbGyeFOtr87d+4u1++vsLdxRB2ejmxJf Hf2OwM0RjoLVMXlLNeKIgm0r5NiC7b//ThYJgeQdw8LkjyOtqxGllXl3sY1Sb/G3ZPIn8B s2uRo85yUmRV4n79tg9xqOQml3WpMGQ= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KmaElLjjiAwlNCrghI81tY5yGxUhBH4Z9zzacvVykyMncHxnAgq7+NC9Ov5BMVtce/AZNyD0hTKopqphIx4cGujotOdhNv0R2Vhuj3Xx5W/irl56o0N22mGWu7jZVEolW//kDsua41yIwUwAt7GphGf62NeRaTskzRzqvZJWlDx3qrXdPjInEsPr71Tib6olilIpa/RU3jKoFnrOA1Nsy8NAupbiHZ47Yt698gD4ePCEQupVyiS8j2gnfUyt1cFnB6G5+Yzd45rwT5mRbtT5QeQAm64P2xofAD7ZtnX5C4LAzyf0E7eAh7XO2DpSl9x6cBPpLIUlRW4t9AIPviEAqg== 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=XZPzzzS3Y+e+/+jr7Ajwo0zlhoYfxqq2qCs0alPROMw=; b=mSZdRHkewFcTwbiURQCYcgNEkFiOlHlnJrPHVcZOUKWp28YtKeb75ExhDB31sYd+C5TXOzTHQlUwV874E1XzaOyJ/eOnWn8aMfQ0P0mt8uAlstDnmh+sZZegojMQfoENGov5e11r5OssBlwBSiZLByMDeP8PMXVAGYhO1AOO215uTsGGLcqaAMLHRLpJCAaNMCY+xqU23fp6tP0hs9i7L7kyyOKHdZG6xGt+Siwd2sBcEbXjEMipWtxqZr7++5i142HXltF84gbeqtVExP4MLiP4JMygbH3DWpzjP6+33uSKneANRD4fGXsMLt+fyPHHIgYeiv2ii2es6ndUMnyy4Q== 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=XZPzzzS3Y+e+/+jr7Ajwo0zlhoYfxqq2qCs0alPROMw=; b=uNFYN/azD3N6h3VjCo+2iEvfNaY3p1HIEfPBLDn79YvJNBdAWObtgXM1HFeF4Y+hgzC2v5rohxLClSK4VTzB8tzTcECXXp8rdhnR7ufXy59mU6nfhEXTAWibrNQK6FsvJ5iTXf4o8EHIAp2dDniDbYp+O5ePyKwJFeqpSYLQ5nc= Received: from CY5PR22CA0066.namprd22.prod.outlook.com (2603:10b6:930:80::13) by IA1PR12MB7640.namprd12.prod.outlook.com (2603:10b6:208:424::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 20:00:24 +0000 Received: from CY4PEPF0000C96D.namprd02.prod.outlook.com (2603:10b6:930:80:cafe::f) by CY5PR22CA0066.outlook.office365.com (2603:10b6:930:80::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:00: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 CY4PEPF0000C96D.mail.protection.outlook.com (10.167.242.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.9 via Frontend Transport; Wed, 14 Dec 2022 20:00: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; Wed, 14 Dec 2022 14:00:22 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 44/64] KVM: SVM: Remove the long-lived GHCB host map Date: Wed, 14 Dec 2022 13:40:36 -0600 Message-ID: <20221214194056.161492-45-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C96D:EE_|IA1PR12MB7640:EE_ X-MS-Office365-Filtering-Correlation-Id: f221bda5-9c6c-4199-5cfe-08dade0dd654 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: qAcdgR5OShZQBebAPOzf50AorePaDhHuB9OglEEfy75jMexxbIFoXGBj2iGbHpgcaOC9aGyCHV7+xbMiH9YhUjDB0CEfbtFb03o+NOyGmzlj4TUx8Z3Lf5+xsqCA9BlIEip21HF8zRUbrymXY8bLdPzMjLbFD93lRhmxV1QqFLywTZc0pGvoI/294BWu3C6e0dfWXQp1yfO0Kb4DhI3lZeY9yoVe0mM09qDonwtTaA6hD8gsPh8lRUvkDybHVkKfo+mfT4BlrOP268+/SA9VtKrvHrgi0C5t8zcwYTtRxsMUjsa4nRJeQOYtsW6QsTr8b+JI1cBc2Pg3UeWmZAFwlCh7I1lv+8bHyIRXd1io4JATzBBKaZDFKKnOrSiu9aHwGsNoYGcAM0XUDj58P6iQmKoPGn0E0yx3bA+Uvsjur12xeWOxPNd0/4KrBb30jn+Dtfmrh8kNeipDi//RHY3kvwbeWRYOkj7f1xHhejyzQiBxBRVz9VU3eI4ytqnWd6DPgStARU7S/3kHLbrQ9kun9LGMQOyam1jDSBFI23fjFWqLs/gbTeF6spWRyrI5F+850xctvTZoeSc1HeaoPufENVSqOj/dHlq1kBqkMfAAvcU/RleFFfNDZldJqrGWbd3rnXVjPUzaxXANAgBmhVv1j0sy17IpNIXt8KuVQhUx+YOToZrnLSTNKy+0WW5zjiAourC7k3EdmtZzWvrMu5EHFmqpJTNQDAmgzzq9ocghuApYlM92QV/9GaR64xueNh42Wp3DtDpQg32J/fVijj1YLw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(136003)(346002)(376002)(396003)(39860400002)(451199015)(40470700004)(46966006)(36840700001)(81166007)(82740400003)(36860700001)(356005)(86362001)(316002)(40460700003)(4326008)(8676002)(70586007)(2616005)(6916009)(6666004)(7416002)(16526019)(5660300002)(8936002)(41300700001)(44832011)(47076005)(2906002)(336012)(1076003)(478600001)(186003)(54906003)(26005)(426003)(70206006)(83380400001)(30864003)(7406005)(40480700001)(82310400005)(36756003)(21314003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:00:23.6890 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f221bda5-9c6c-4199-5cfe-08dade0dd654 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: CY4PEPF0000C96D.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB7640 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 2119A1A0022 X-Stat-Signature: t38rq589ae9icox838zyiq18ejajuxdm X-HE-Tag: 1671048027-603147 X-HE-Meta: U2FsdGVkX18Gs+oki/IeBNDbJ+e7mvhCH1dCpTheqAGnS0mCnzQNXjGlPpLvpLV5IBHyyMkzD0uE7G9IMt1zG9qGgwtx8h6V4URgx41+C7Xm0XBMCHgRHGps1+dQYS40C3hE13IUIDg1SckzEZ+4XZhWc+LWhnqMSQWI19cO6we6o71s/bHMWICSXzVJtIsAxEfUmpFGruxmA8gzoQFM/rYAyoTsuGKon9o/URypOLf0BaQ0pH4Qj9PONV2dz97iTf/FVS7qs/YgGQ24Z5YSg+x4jRhlNKTWNvPN0g5uqpUcZlqIxfVerVBPZIYuc7HYdTmoDkrqPmbxr/BnGVNqFmtUgC/i4/2xHJk12n5Sxajpdn0MMsX3fpMlpF3olH5iRe9mSN2jh3d1tBUSiLEvMszcifA4awgEEFBYArrjcK5+SeZ7KdStwKE+x++TkG6/ZDSytSkx6iF4QPhdyTSH4dXSY7SV0yX4xQ4yV/vp3N2tcxySVb8w8wSxxuhDZT/sCSPmzGF27HfrjYslkzmnfItf4Oh7z2i+GVnqa1eNEWnNbwrKiJjIF8P12r6rqmL/5OYdl8Njp4UGDYAvky561gDLWkEQ04GkhGXpnX1OCX4rwvCcl7YCZKYzAqwDApZdwebYi7j0E7D3vsi5Bq8zboWAlE2+vmKDAn50XvrUXSfTBYELeHkofkiIY3q/L1Sfzk00+GJrpfCX1lrajKa86UlFcSRH2AQfsE/4IJQIApGkm74IeZiyIu8N5FX1OQ4zQgsJB+pEwaPBmkPCo31cogf1rI/8ANAu42fkc51NSn6/w24N6itEzvSvAgZVRDd6mnyfbcTQStzQ7JYSt1p2s5yUMhZStklgJVargQfwcbh5dMUXw2vkjioegwGR1ezk842c2fnXqKME8X9WcVJIQGzq7YAABz/uD75KTrvQfReQ6IM/4OAuT4DtPliOXkhiFFzhZKp923prTtzMaaY S7oJoZhF 3zq2H 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 On VMGEXIT, sev_handle_vmgexit() creates a host mapping for the GHCB GPA, and unmaps it just before VM-entry. This long-lived GHCB map is used by the VMGEXIT handler through accessors such as ghcb_{set_get}_xxx(). A long-lived GHCB map can cause issue when SEV-SNP is enabled. When SEV-SNP is enabled the mapped GPA needs to be protected against a page state change. To eliminate the long-lived GHCB mapping, update the GHCB sync operations to explicitly map the GHCB before access and unmap it after access is complete. This requires that the setting of the GHCBs sw_exit_info_{1,2} fields be done during sev_es_sync_to_ghcb(), so create two new fields in the vcpu_svm struct to hold these values when required to be set outside of the GHCB mapping. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra [mdr: defer per_cpu() assignment and order it with barrier() to fix case where kvm_vcpu_map() causes reschedule on different CPU] Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 131 ++++++++++++++++++++++++++--------------- arch/x86/kvm/svm/svm.c | 18 +++--- arch/x86/kvm/svm/svm.h | 24 +++++++- 3 files changed, 116 insertions(+), 57 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index d5c6e48055fb..6ac0cb6e3484 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2921,15 +2921,40 @@ void sev_free_vcpu(struct kvm_vcpu *vcpu) kvfree(svm->sev_es.ghcb_sa); } +static inline int svm_map_ghcb(struct vcpu_svm *svm, struct kvm_host_map *map) +{ + struct vmcb_control_area *control = &svm->vmcb->control; + u64 gfn = gpa_to_gfn(control->ghcb_gpa); + + if (kvm_vcpu_map(&svm->vcpu, gfn, map)) { + /* Unable to map GHCB from guest */ + pr_err("error mapping GHCB GFN [%#llx] from guest\n", gfn); + return -EFAULT; + } + + return 0; +} + +static inline void svm_unmap_ghcb(struct vcpu_svm *svm, struct kvm_host_map *map) +{ + kvm_vcpu_unmap(&svm->vcpu, map, true); +} + static void dump_ghcb(struct vcpu_svm *svm) { - struct ghcb *ghcb = svm->sev_es.ghcb; + struct kvm_host_map map; unsigned int nbits; + struct ghcb *ghcb; + + if (svm_map_ghcb(svm, &map)) + return; + + ghcb = map.hva; /* Re-use the dump_invalid_vmcb module parameter */ if (!dump_invalid_vmcb) { pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n"); - return; + goto e_unmap; } nbits = sizeof(ghcb->save.valid_bitmap) * 8; @@ -2944,12 +2969,21 @@ static void dump_ghcb(struct vcpu_svm *svm) pr_err("%-20s%016llx is_valid: %u\n", "sw_scratch", ghcb->save.sw_scratch, ghcb_sw_scratch_is_valid(ghcb)); pr_err("%-20s%*pb\n", "valid_bitmap", nbits, ghcb->save.valid_bitmap); + +e_unmap: + svm_unmap_ghcb(svm, &map); } -static void sev_es_sync_to_ghcb(struct vcpu_svm *svm) +static bool sev_es_sync_to_ghcb(struct vcpu_svm *svm) { struct kvm_vcpu *vcpu = &svm->vcpu; - struct ghcb *ghcb = svm->sev_es.ghcb; + struct kvm_host_map map; + struct ghcb *ghcb; + + if (svm_map_ghcb(svm, &map)) + return false; + + ghcb = map.hva; /* * The GHCB protocol so far allows for the following data @@ -2963,13 +2997,24 @@ static void sev_es_sync_to_ghcb(struct vcpu_svm *svm) ghcb_set_rbx(ghcb, vcpu->arch.regs[VCPU_REGS_RBX]); ghcb_set_rcx(ghcb, vcpu->arch.regs[VCPU_REGS_RCX]); ghcb_set_rdx(ghcb, vcpu->arch.regs[VCPU_REGS_RDX]); + + /* + * Copy the return values from the exit_info_{1,2}. + */ + ghcb_set_sw_exit_info_1(ghcb, svm->sev_es.ghcb_sw_exit_info_1); + ghcb_set_sw_exit_info_2(ghcb, svm->sev_es.ghcb_sw_exit_info_2); + + trace_kvm_vmgexit_exit(svm->vcpu.vcpu_id, ghcb); + + svm_unmap_ghcb(svm, &map); + + return true; } -static void sev_es_sync_from_ghcb(struct vcpu_svm *svm) +static void sev_es_sync_from_ghcb(struct vcpu_svm *svm, struct ghcb *ghcb) { struct vmcb_control_area *control = &svm->vmcb->control; struct kvm_vcpu *vcpu = &svm->vcpu; - struct ghcb *ghcb = svm->sev_es.ghcb; u64 exit_code; /* @@ -3013,20 +3058,25 @@ static void sev_es_sync_from_ghcb(struct vcpu_svm *svm) memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap)); } -static int sev_es_validate_vmgexit(struct vcpu_svm *svm) +static int sev_es_validate_vmgexit(struct vcpu_svm *svm, u64 *exit_code) { - struct kvm_vcpu *vcpu; + struct kvm_vcpu *vcpu = &svm->vcpu; + struct kvm_host_map map; struct ghcb *ghcb; - u64 exit_code; u64 reason; - ghcb = svm->sev_es.ghcb; + if (svm_map_ghcb(svm, &map)) + return -EFAULT; + + ghcb = map.hva; + + trace_kvm_vmgexit_enter(vcpu->vcpu_id, ghcb); /* * Retrieve the exit code now even though it may not be marked valid * as it could help with debugging. */ - exit_code = ghcb_get_sw_exit_code(ghcb); + *exit_code = ghcb_get_sw_exit_code(ghcb); /* Only GHCB Usage code 0 is supported */ if (ghcb->ghcb_usage) { @@ -3119,6 +3169,9 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) goto vmgexit_err; } + sev_es_sync_from_ghcb(svm, ghcb); + + svm_unmap_ghcb(svm, &map); return 0; vmgexit_err: @@ -3129,10 +3182,10 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) ghcb->ghcb_usage); } else if (reason == GHCB_ERR_INVALID_EVENT) { vcpu_unimpl(vcpu, "vmgexit: exit code %#llx is not valid\n", - exit_code); + *exit_code); } else { vcpu_unimpl(vcpu, "vmgexit: exit code %#llx input is not valid\n", - exit_code); + *exit_code); dump_ghcb(svm); } @@ -3142,6 +3195,8 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) ghcb_set_sw_exit_info_1(ghcb, 2); ghcb_set_sw_exit_info_2(ghcb, reason); + svm_unmap_ghcb(svm, &map); + /* Resume the guest to "return" the error code. */ return 1; } @@ -3151,23 +3206,20 @@ 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) + if (!svm->sev_es.ghcb_in_use) return; /* Sync the scratch buffer area. */ if (svm->sev_es.ghcb_sa_sync) { kvm_write_guest(svm->vcpu.kvm, - ghcb_get_sw_scratch(svm->sev_es.ghcb), + svm->sev_es.ghcb_sa_gpa, svm->sev_es.ghcb_sa, svm->sev_es.ghcb_sa_len); svm->sev_es.ghcb_sa_sync = false; } - trace_kvm_vmgexit_exit(svm->vcpu.vcpu_id, svm->sev_es.ghcb); - sev_es_sync_to_ghcb(svm); - kvm_vcpu_unmap(&svm->vcpu, &svm->sev_es.ghcb_map, true); - svm->sev_es.ghcb = NULL; + svm->sev_es.ghcb_in_use = false; } void pre_sev_run(struct vcpu_svm *svm, int cpu) @@ -3197,7 +3249,6 @@ void pre_sev_run(struct vcpu_svm *svm, int cpu) static int setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len) { struct vmcb_control_area *control = &svm->vmcb->control; - struct ghcb *ghcb = svm->sev_es.ghcb; u64 ghcb_scratch_beg, ghcb_scratch_end; u64 scratch_gpa_beg, scratch_gpa_end; @@ -3276,8 +3327,8 @@ static int setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len) return 0; e_scratch: - ghcb_set_sw_exit_info_1(ghcb, 2); - ghcb_set_sw_exit_info_2(ghcb, GHCB_ERR_INVALID_SCRATCH_AREA); + svm_set_ghcb_sw_exit_info_1(&svm->vcpu, 2); + svm_set_ghcb_sw_exit_info_2(&svm->vcpu, GHCB_ERR_INVALID_SCRATCH_AREA); return 1; } @@ -3413,7 +3464,6 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) struct vcpu_svm *svm = to_svm(vcpu); struct vmcb_control_area *control = &svm->vmcb->control; u64 ghcb_gpa, exit_code; - struct ghcb *ghcb; int ret; /* Validate the GHCB */ @@ -3428,29 +3478,14 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) return 1; } - if (kvm_vcpu_map(vcpu, ghcb_gpa >> PAGE_SHIFT, &svm->sev_es.ghcb_map)) { - /* Unable to map GHCB from guest */ - vcpu_unimpl(vcpu, "vmgexit: error mapping GHCB [%#llx] from guest\n", - ghcb_gpa); - - /* Without a GHCB, just return right back to the guest */ - return 1; - } - - svm->sev_es.ghcb = svm->sev_es.ghcb_map.hva; - ghcb = svm->sev_es.ghcb_map.hva; - - trace_kvm_vmgexit_enter(vcpu->vcpu_id, ghcb); - - exit_code = ghcb_get_sw_exit_code(ghcb); - - ret = sev_es_validate_vmgexit(svm); + ret = sev_es_validate_vmgexit(svm, &exit_code); if (ret) return ret; - sev_es_sync_from_ghcb(svm); - ghcb_set_sw_exit_info_1(ghcb, 0); - ghcb_set_sw_exit_info_2(ghcb, 0); + svm->sev_es.ghcb_in_use = true; + + svm_set_ghcb_sw_exit_info_1(vcpu, 0); + svm_set_ghcb_sw_exit_info_2(vcpu, 0); switch (exit_code) { case SVM_VMGEXIT_MMIO_READ: @@ -3490,20 +3525,20 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) break; case 1: /* Get AP jump table address */ - ghcb_set_sw_exit_info_2(ghcb, sev->ap_jump_table); + svm_set_ghcb_sw_exit_info_2(vcpu, sev->ap_jump_table); break; default: pr_err("svm: vmgexit: unsupported AP jump table request - exit_info_1=%#llx\n", control->exit_info_1); - ghcb_set_sw_exit_info_1(ghcb, 2); - ghcb_set_sw_exit_info_2(ghcb, GHCB_ERR_INVALID_INPUT); + svm_set_ghcb_sw_exit_info_1(vcpu, 2); + svm_set_ghcb_sw_exit_info_2(vcpu, GHCB_ERR_INVALID_INPUT); } ret = 1; break; } case SVM_VMGEXIT_HV_FEATURES: { - ghcb_set_sw_exit_info_2(ghcb, GHCB_HV_FT_SUPPORTED); + svm_set_ghcb_sw_exit_info_2(vcpu, GHCB_HV_FT_SUPPORTED); ret = 1; break; @@ -3651,7 +3686,7 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) * 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); + svm_set_ghcb_sw_exit_info_2(vcpu, 1); break; case AP_RESET_HOLD_MSR_PROTO: /* diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 2dfa150bcb09..1826946a2f43 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1445,7 +1445,7 @@ static void svm_vcpu_free(struct kvm_vcpu *vcpu) static void svm_prepare_switch_to_guest(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm = to_svm(vcpu); - struct svm_cpu_data *sd = per_cpu_ptr(&svm_data, vcpu->cpu); + struct svm_cpu_data *sd; if (sev_es_guest(vcpu->kvm)) sev_es_unmap_ghcb(svm); @@ -1453,6 +1453,10 @@ static void svm_prepare_switch_to_guest(struct kvm_vcpu *vcpu) if (svm->guest_state_loaded) return; + /* sev_es_unmap_ghcb() can resched, so grab per-cpu pointer afterward. */ + barrier(); + sd = per_cpu_ptr(&svm_data, vcpu->cpu); + /* * Save additional host state that will be restored on VMEXIT (sev-es) * or subsequent vmload of host save area. @@ -2818,14 +2822,14 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) static int svm_complete_emulated_msr(struct kvm_vcpu *vcpu, int err) { struct vcpu_svm *svm = to_svm(vcpu); - if (!err || !sev_es_guest(vcpu->kvm) || WARN_ON_ONCE(!svm->sev_es.ghcb)) + if (!err || !sev_es_guest(vcpu->kvm) || WARN_ON_ONCE(!svm->sev_es.ghcb_in_use)) return kvm_complete_insn_gp(vcpu, err); - ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, 1); - ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, - X86_TRAP_GP | - SVM_EVTINJ_TYPE_EXEPT | - SVM_EVTINJ_VALID); + svm_set_ghcb_sw_exit_info_1(vcpu, 1); + svm_set_ghcb_sw_exit_info_2(vcpu, + X86_TRAP_GP | + SVM_EVTINJ_TYPE_EXEPT | + SVM_EVTINJ_VALID); return 1; } diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index f53a41e13033..c462dfac0a0d 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -201,8 +201,7 @@ struct svm_nested_state { struct vcpu_sev_es_state { /* SEV-ES support */ struct sev_es_save_area *vmsa; - struct ghcb *ghcb; - struct kvm_host_map ghcb_map; + bool ghcb_in_use; bool received_first_sipi; unsigned int ap_reset_hold_type; @@ -212,6 +211,13 @@ struct vcpu_sev_es_state { u64 ghcb_sa_gpa; u32 ghcb_sa_alloc_len; bool ghcb_sa_sync; + + /* + * SEV-ES support to hold the sw_exit_info return values to be + * sync'ed to the GHCB when mapped. + */ + u64 ghcb_sw_exit_info_1; + u64 ghcb_sw_exit_info_2; }; struct vcpu_svm { @@ -640,6 +646,20 @@ void nested_sync_control_from_vmcb02(struct vcpu_svm *svm); void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm); void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb); +static inline void svm_set_ghcb_sw_exit_info_1(struct kvm_vcpu *vcpu, u64 val) +{ + struct vcpu_svm *svm = to_svm(vcpu); + + svm->sev_es.ghcb_sw_exit_info_1 = val; +} + +static inline void svm_set_ghcb_sw_exit_info_2(struct kvm_vcpu *vcpu, u64 val) +{ + struct vcpu_svm *svm = to_svm(vcpu); + + svm->sev_es.ghcb_sw_exit_info_2 = val; +} + extern struct kvm_x86_nested_ops svm_nested_ops; /* avic.c */ From patchwork Wed Dec 14 19:40:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073458 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 0C0B7C4332F for ; Wed, 14 Dec 2022 20:00:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A09B18E0005; Wed, 14 Dec 2022 15:00:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9B9F88E0002; Wed, 14 Dec 2022 15:00:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 833F18E0005; Wed, 14 Dec 2022 15:00:52 -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 7358C8E0002 for ; Wed, 14 Dec 2022 15:00:52 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 4819D120F96 for ; Wed, 14 Dec 2022 20:00:52 +0000 (UTC) X-FDA: 80241979944.07.06065F8 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2077.outbound.protection.outlook.com [40.107.96.77]) by imf09.hostedemail.com (Postfix) with ESMTP id BB2E8140015 for ; Wed, 14 Dec 2022 20:00:48 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=nm2ZYbZK; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf09.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.96.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=1671048048; a=rsa-sha256; cv=pass; b=EgETLHT7g1Ki/ecEeTpMM92UhzYnVeGJOlA54OHF9HtsROjVmWTPh/UJ8MQjXlr/2W2Yo4 5POF68v3N1liSg0k28IyXhIc/czSDmyxgG1ZTvBmKMYMRwTdBkz9XF4PYfMqFHfQHJhAh4 JjeznVp2gxe1jkX9qylcRV3RNkbmseQ= ARC-Authentication-Results: i=2; imf09.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=nm2ZYbZK; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf09.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.96.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=1671048048; 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=87+dXM0XJY3uTF69sg7L6q8yRudG+raaIDTkuVaDBt4=; b=VJzx7WXIRw3lBc2HQd295RFMZoSDr59NL4s/rper6QozQVYNb6c1F71FVfyqneXEFm/tGv yQ7/NswqyET3xV3fSoySZ+YyiyOfPWZUMIaHyL6ZfKzjsylNBMsjen8m+jZnRqfchZDXXT kdzJaIGtDz7QGJkWmoqGtWblp6PuKjg= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Nc2tc8jiyeXPud6Mh8oX/izEEiaGOmmRY/4oN3NsCeTTbe5E8Fe1tmU4s68uN6wU4Tp0b+brhbBxj4qB9MgTualU20kCXVTLwVuYpcHzRjToqeZ9qQtVezYEF6ArpYjxeBQUPYgHrpqVXttZmhj/hwJyymMP17E7Ug7OYpOVq8eOjg70TyNBwu6UOpxbD314cw95oF4gkK7rzN3WSlXSd0lrTx8WtGXWDK+3K3ci5bO3tZ9B2ug7kMUz3CFShUDstFMw1I+UNQNB/h8PZQuyzcqiX7J6gh2iH9zwRfhRSVg6j72q3BXWaYh7C1QNimbVxvVbalEem7Bxo7Md8LhJKQ== 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=87+dXM0XJY3uTF69sg7L6q8yRudG+raaIDTkuVaDBt4=; b=MCJcm7NO3WgsIQSshtYSAXbsjFS3YkQOQUqtfEJ2DN7qwi+RI4f6Uqe4gTotPmeRUcLnrHYzLw2qYAMHJ5Gx2WRjCOgCWAWYsMhZ/fhhiZU5vUTKFJCSiKhPB+kNWJJXysIrQ966yOurr7bBwDge7DuRLmsBOWvU8rZt9vFTzo0HCv6ReOxAVrUdAm5hsX0/lmJTFZv9vvn0V5/1qOB3Zg0Ufvr0wfNriUC8hrnkbdn9XEqjHfkDBuCldXsth+++xXr+KUL6k/pOOzLXti250z5mIiDVpe27pun9NZv1ZAO7W1yFTTQKdv59/0px7jlbqiqxo14+DdvRaCUPrScp/A== 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=87+dXM0XJY3uTF69sg7L6q8yRudG+raaIDTkuVaDBt4=; b=nm2ZYbZKneyIj3uL8wt1xkoWQ5m0iNXOFLRKN7QPu2JfRsr96y0vfucHs7WoMudmphDbFi6Oo4047C8Tu5DEamOczWNq1rDj12WzVFW74+lpZTK9fbWyyW5NKCX2nnyVqHyjNOOO+dU8LYS1QW9zzahlGx+k1pw9MIi+EyAR+i4= Received: from CY5PR22CA0066.namprd22.prod.outlook.com (2603:10b6:930:80::13) by LV2PR12MB5797.namprd12.prod.outlook.com (2603:10b6:408:17b::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 20:00:45 +0000 Received: from CY4PEPF0000C96D.namprd02.prod.outlook.com (2603:10b6:930:80:cafe::21) by CY5PR22CA0066.outlook.office365.com (2603:10b6:930:80::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:00:45 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000C96D.mail.protection.outlook.com (10.167.242.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.9 via Frontend Transport; Wed, 14 Dec 2022 20:00:45 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Wed, 14 Dec 2022 14:00:43 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 45/64] KVM: SVM: Add support to handle GHCB GPA register VMGEXIT Date: Wed, 14 Dec 2022 13:40:37 -0600 Message-ID: <20221214194056.161492-46-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C96D:EE_|LV2PR12MB5797:EE_ X-MS-Office365-Filtering-Correlation-Id: 49d796b4-a364-49ae-6882-08dade0de320 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 1vz/LsYYJY7rhJtSVVQROwtXNMjatmhIrasHecNs6Hjpf1OVcn3ibv24TgEIMq0Fm7PVd7yjLRHIv74p3iaz/uI48HdXsZ/34QHB/DrWLvaP5zhYY2528y3bmjubEHNF4LEGRnLPbHPkm+VpmAt46Ca85yzNh5PqpJT68An7dZhG530qqum4JBbKMOlq+XTD8YjBT6TxIBDTYKQ/SN5FhwKODOjUoDjazFn0xuAQRSAHrqESDLr0unFBMKkZgDsm0gShO66jvRUGZ0ZMfu6F3EfpcT5bFxSzWaD98MUVT5YJCop0Zm5fdUy9l1RiHAwog1Hc426UBW/ZH/a/tJipIthnn+LWigLctp0+vMW5g5ArOOfe0KBl/b8ymYraKJpiM8UNiovceT5dW49jOCGcHMYvKC9IVvryfqtxaC6StsYNOfwsoOEK85a/HLJ2cdBbnZpe8Jr+xVuMmRKqW208BHe9cmirBQHYbe+PiDCMIp7pvWwwRZo3RJQwun2JZfk9r8lK9OEO1FsrPazoLEFjr+x6n93gD9Ry0PVaSg2ll39dj5VSN6RLEzlvvaXz/z1Gi7C066RNaHjhpTnAeElEDSkOFGnt9G7ywjIYcdRRAvvT8qfcKBb336YshpWoCm5i4hlz2Xp8bWZifHgkeeY7WTzN+jIcWJ1EGpC+1UocUpRI2I5Ond1Ny5/rE+jPv+wJXOYpueeDTYBbWVEDzZI46DS2KQSX78zUYxuV+Odg3Wg= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(346002)(376002)(136003)(396003)(39860400002)(451199015)(36840700001)(40470700004)(46966006)(36756003)(26005)(41300700001)(83380400001)(36860700001)(186003)(7416002)(478600001)(6666004)(54906003)(6916009)(316002)(7406005)(5660300002)(8936002)(86362001)(82310400005)(70586007)(8676002)(70206006)(4326008)(426003)(47076005)(44832011)(356005)(81166007)(336012)(40480700001)(82740400003)(1076003)(2906002)(16526019)(40460700003)(2616005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:00:45.1433 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 49d796b4-a364-49ae-6882-08dade0de320 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: CY4PEPF0000C96D.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV2PR12MB5797 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: BB2E8140015 X-Stat-Signature: e5est36gxmsawryhu4pmbcgmd6szt5ae X-HE-Tag: 1671048048-225733 X-HE-Meta: U2FsdGVkX18+IqyrBD1XNQzxvxTntQzmM1UjDivYxaLGHBhQyHI+6Yq9DkuHMpXV7Ma0Dgy0cceZmCA6+8oGaxEjXWdH7w7y+QTvMRDpy25KEp2RIesXdYmSxqbM5ClukrXhe+A8i3aawpgVBQ/hJcUIpsTUJOytRFTHJVY6pCEuo/7q68o+bv5DrLGCUPMavuwfy+bIwrRsH7MufklsX8wUiFroS1o7o0eAwfqKEfLnYcY1LDu58yy3WvPShZbnmDQz8PPhS42PVfsSvlBnomzRZXtza20hvV6PHBM6RnxlxqeZobIDagOQtj729Tzy5/iKu++OHdEP9V6TXxwZQe0xJCJeBy7iXQNvzLfV5rny2FC1gJqHeZ5FI7tWuVM9HSHSfiaC2h5M4gfehOTr+MpP2vh7Qd/iVPjcTqNUpCpD85ryQfZujrm+7szokP/lvT5dt8dWwpyM2J0I0365TyMMng7gYvgfO+jHhv+Fb9T2U0Me4IZFKSYXBgvWk2sEcVqYi863MDT3QDDpx82U0A1swMgWt2mHBsW0CuNaSzs+q18uFtn6D592Evb9d9nVk6R0M1y2sgahCo1nCXY+U8nbnw4acVadvwpcfIXZt8kXLByUXyR9iWfGN1Qy822Cen9x3tUUzwt1q8+CltMxyoqCsj39udelpjtu4wo9o3bqwYcujgHGkloBsLtrpchGfyBk47wRYsFw93GzM/J0eHW7m/WPFoGpuhQ499UQMl5SPtFDFRjVSLGnJkX5FgjY2+NKe6ZvDEy6BgIzACQCVpeVQr9yVMc7G+JL6FRGm8AHPZs/IPcPzxwSaoORkuOBi307flPg1fptL4o55oz0ysk4jAZBzc7R0P228s1RAbsMGzSjsybHBQlybyj7O7MRGTd3GkpqtKvjL44soUGVKUYoFpI08fXubvrfjcHAh0Y0FQf/zWhXgU7HuXpY+grwc7VdlJ5z7mxrW78OIzB yDoAk2ux mb1ea 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 6ac0cb6e3484..d7b467b620aa 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3429,6 +3429,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; @@ -3478,6 +3499,12 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) return 1; } + /* 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, &exit_code); if (ret) return ret; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index c462dfac0a0d..a4d48c3e0f89 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -218,6 +218,8 @@ struct vcpu_sev_es_state { */ u64 ghcb_sw_exit_info_1; u64 ghcb_sw_exit_info_2; + + u64 ghcb_registered_gpa; }; struct vcpu_svm { @@ -350,6 +352,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 Wed Dec 14 19:40:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073459 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 B7568C4332F for ; Wed, 14 Dec 2022 20:01:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 54ED68E0007; Wed, 14 Dec 2022 15:01:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 52EBA8E0002; Wed, 14 Dec 2022 15:01:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3EEC48E0007; Wed, 14 Dec 2022 15:01:11 -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 305398E0002 for ; Wed, 14 Dec 2022 15:01:11 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id CA409120F7E for ; Wed, 14 Dec 2022 20:01:10 +0000 (UTC) X-FDA: 80241980700.10.172F441 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by imf10.hostedemail.com (Postfix) with ESMTP id C5BF3C001B for ; Wed, 14 Dec 2022 20:01:07 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="0ieU/p15"; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf10.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=1671048067; 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=cmwpFRrETQPvZ04fJ8irqa0gEHOKsSxIthHrtbtX3fA=; b=LzDayqON37PvrspVIqZWUh9nN9/uIuVw7Vi2kGRNLJhhHv/ilhQ16uZCO0aCXkN1sAZcRt OxdfAAHpCmgyl/uZ0fqgOItpcfaPKaJlP78Hwa3v05EdhXygDQsPHnBNHRQx6mGQEtvLX3 uYebFfZdMQGZuPuIqVJidKv5WXClaCU= ARC-Authentication-Results: i=2; imf10.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="0ieU/p15"; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf10.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=1671048067; a=rsa-sha256; cv=pass; b=rsX62P3H7cTP4PlWzkUtOIucQeX/m7i5auLfaZ7UR95XEOIsWcvInR7JpQhUVXv5Ez59Id hU18WfvAn6kQN0WBClzpadjgkStr/ekoQwVG+Aa0cFvA/nU3JZB6sBlH6PN4NDMDZGruN/ e4FUTbIaZTKBBpS+xhb0kaH60B07/+s= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=e+LQr/sq/5kxOkZlqYhg2UmENgJW9yUsH/NSSHvqhp7Pu9A951k+qpcjCll+aNXD9sqFpADfJu7joXnj5ydfALiENN17Ie4vSHixCfS9Az7/t3Bv1jcG/Ais7a9/AwTHC/RIL0Wo3/VUOZYZ8IusUzpgXyKDFtki95MIh1hZLu8La2enadAUduvTDfI8f0W5TCGxyZ1MVPpwypfjjtGPnnUeS2IV8vzRLcDOZULH6IYYE/NemdtGnJtYZju/5PGI8yPPsEbCiQpaDX23kx/jkGJTUJD3zlcs0XCLEfQ7PcUMlMeO04z8wVXQNTBtIaVGhS5IfQ944kQKZIfHiazZ7Q== 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=cmwpFRrETQPvZ04fJ8irqa0gEHOKsSxIthHrtbtX3fA=; b=fxsiwWsp9KesXpdtxB5TB+M8m6Jyra9L/GUkiOYCsAKpE7zWglMK/IKdaiXv84gKBL61I/hW3XpD2WQCOnERVUectGOscMFgrzJxWr8eLELHNoeWwjc57eaP6KzP5fPX7ksEohjiH72Q+zcRVd2O1b7ggu20YekRtCunNdpRTlioLSVYHqnvO8xH4yYA77qlGDKGelES7AVkkuWOx0Z1r8JXKeflrCvTWhx9kKjOTrBnJUMhvk7+pf3wA6I8A2M2+Qp+6UXh0VaguQ1E9K7EbDiooFHb0zS3G5MHrvxUoLQotdCkGeo1CrA1eCINubAmi2oLkdXyFY1WJq2dUcm60w== 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=cmwpFRrETQPvZ04fJ8irqa0gEHOKsSxIthHrtbtX3fA=; b=0ieU/p153QUdR0G4RHpyn9cuT437mF+HWq7udXtEudaJV3HZ725Si0JalrQWdE5D3cD5+DjhUTgsAAqW+jezc5f5falgLtBW1qKFS2NYiHxqYW3wxKOrT+fzbtfA4W4weG0RH2J93VjDT/+5/b94IvJfqv7uos7pK/fu1JChNDs= Received: from CY5P221CA0087.NAMP221.PROD.OUTLOOK.COM (2603:10b6:930:9::30) by CY5PR12MB6105.namprd12.prod.outlook.com (2603:10b6:930:2a::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 20:01:05 +0000 Received: from CY4PEPF0000C973.namprd02.prod.outlook.com (2603:10b6:930:9:cafe::53) by CY5P221CA0087.outlook.office365.com (2603:10b6:930:9::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:01: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 CY4PEPF0000C973.mail.protection.outlook.com (10.167.242.11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.10 via Frontend Transport; Wed, 14 Dec 2022 20:01: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; Wed, 14 Dec 2022 14:01:04 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v7 46/64] KVM: SVM: Add KVM_EXIT_VMGEXIT Date: Wed, 14 Dec 2022 13:40:38 -0600 Message-ID: <20221214194056.161492-47-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C973:EE_|CY5PR12MB6105:EE_ X-MS-Office365-Filtering-Correlation-Id: e3f97380-cc2c-4a8e-2e78-08dade0def5b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: RLIAeRG3jFUMYUCStQ2am9kZ8uCuwm4eVwjBeikSbJo77z+YiQhzNT7q5Wwigx30C1LZ61Lsd0ojph7gF3bJCcs5YjMIS2j+lQjIE9nEXsjBXbXPWLbcrhXOa5DzQGueXMb7vFYKkbJGV9kPWURUfRmgT0TsnSUT8c+9oC03k1sbeFRLEDQ03ppO+0G7J8H13LTqEn5rUE0TBlaMRmDAQEIgryon3ncszP9SrNwuPWbhrVFayUtxMgPMhTDBsWq/xzgnb6xIBUgJ+juUE7YrPc8o4V6UFXsNB1RxnXQRLicDVmKOzJJdDiuz/ByoT4NfGRKihHvUb7rZ8nINXaYLUHt63WBOIMSlPuRwqIobNmZY1jQ53UCfU+MjLQgFwX3+GjA5lqkXyVtsl78g+ad2Dv9Q7Kjvi3VsVEx2N44RQ1zZeNCRk9/m5LfoJFPGHzLrQNjluOcMJfj1MIfNoV84jYYAbRlrez38tIlw7KDua6FTe5cPDgZT1dl/DyNHTUf36YAaRf42fZvL6MRDpyZ+LoavpdGIi97T8wfuKAoPFc9k0XIq8wMfAO53lU0ozvjyP2DU2+XDKETmGA8eRkXSeWAa6cnDJZNV6yXancyV88A6g9blKKmEXBri8iFntRoMeLqUvyjqQ3tDzjbAIF10NWWazn/Ev1PLWRs875Vm1Nf6cqJxEvfjGY5T1g7fbOVA1VOwDQLxjJEQNE47CO8y5o05YBuShZOc24IS2vHWMTk= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(136003)(396003)(39860400002)(376002)(346002)(451199015)(36840700001)(40470700004)(46966006)(44832011)(26005)(2616005)(36756003)(86362001)(186003)(426003)(47076005)(6916009)(36860700001)(4744005)(336012)(16526019)(83380400001)(54906003)(40460700003)(1076003)(2906002)(41300700001)(6666004)(82310400005)(356005)(82740400003)(478600001)(7406005)(81166007)(40480700001)(8936002)(8676002)(316002)(70206006)(4326008)(70586007)(7416002)(5660300002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:01:05.6922 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e3f97380-cc2c-4a8e-2e78-08dade0def5b 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: CY4PEPF0000C973.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY5PR12MB6105 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: C5BF3C001B X-Stat-Signature: xczjw1if44xh7dn6pyohqstj3ciyiino X-HE-Tag: 1671048067-627634 X-HE-Meta: U2FsdGVkX18002XFtlwps623qPgc3TrhmZ19V7t/lhjZvKeB5vbYW0BIebMkHfBJD/MSegq8lWjdm7OuLOdX22XLKvA2kOK5jyPCc/9PAoK6lvcTGBTBCV702BOb3s9QgHLk+YTK7xh44hZ4+woK1xn7wqNHEhet+COjM2ynuXXKejOJxuqSH9BkVk340gI2bxnsjRNj3Qu1YsP8ACLhgheJvqkcWAaTajO7x8SgSqZNlSXTXDWKSwantlkraF7a76NLYdtEgFKazw0qKt3p2QFRdIGm37Agtj3S39XA9n7xQQBsmcYEbxDUr1s0Tvl8GMg+CnVRhQrEquEB50Peqd4DMce8OoKe9goR1q9l6IRnwsIMKPnEGvuhlfUBWkvSRoZR5Uakw4yCBw2wrbyAb5qBz50WopYZ5XyPUWMuPM35hJ5bxhW2KCI5ne/rXtAE7X+C1el+KGjgH1SxvVxZEM2F7XKtJNFxVZ3fwj459FjTn5HtLbEsBa1B9e3CuwROKt4ZErIQ4/ZT3rJPG3ppfxQ7KelznGlE7N0iRhZN9dzIYJXCfPjYNiFpVhSBShJ136KFizNxUMJ6XYhcKOpOtavyGgH/KwZ5wlnXlWQXXVYJAqymM0CwT6z/AMz9mo2dtd97Z92GJ18RlfwC6T+oA9Q/h5PnE8tLWI/qiUOfwpOOfQxPWW4SXq3aSD8pCNKoyDKJZWTeDMpLFIrAzEkdUa5JuXuJv/GZ0LDGl6WBEHKsYtcRoxhmcaWmD8YMRtQ8OVAannImD7fAW3Pyg0wS+tpOMUAPC2qWBRkn07/B0UASWHg2/b3BMxIwh6aNtt4bzwxHIAkCZqV2m6+nz+kz4JIv+RaI82jCYU1yY/vTEXeHTnAg0MP95Bi5pBY2urDaULYVv+8KC08ag7kPfN3CdQlQegcwnk2JhizPObTBPGfKQoRu43zA32n4zRi+PqE/ABDn9EbvfR5P4e6tQM/ F+JF78Ow l/DJkWc14XkzuAMmRUityG4uGhuOTACCg7zQ2Ws5UTC/D220GczLDxHknPA== 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 c468adc1f147..61b1e26ced01 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -301,6 +301,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. */ @@ -549,6 +550,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 Wed Dec 14 19:40:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073460 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 D8D5EC4332F for ; Wed, 14 Dec 2022 20:01:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 835768E0008; Wed, 14 Dec 2022 15:01:34 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7E4898E0002; Wed, 14 Dec 2022 15:01:34 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 65EAA8E0008; Wed, 14 Dec 2022 15:01:34 -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 53C008E0002 for ; Wed, 14 Dec 2022 15:01:34 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id EB8DB120F8A for ; Wed, 14 Dec 2022 20:01:33 +0000 (UTC) X-FDA: 80241981666.20.F60075A Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2054.outbound.protection.outlook.com [40.107.244.54]) by imf08.hostedemail.com (Postfix) with ESMTP id 06F0816000E for ; Wed, 14 Dec 2022 20:01:30 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=y2yuMNXd; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf08.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.54 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1671048091; 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=GlVqa2qmkaaiQMe4N6o7Bdn+VWWRYcbCRi5kfE+AEcs=; b=NInnIm5WM4qDNKAEZY5qsIz1y2xuVccJNUdgnkVZspUMTZZEWTjBr4xc5we+XsL4MR4JE4 ejlgnFor+y1Zgtqiqcisp+WnH749+Lrpd1ghzAGQ44DCp+EvaqraSGugLfdrVYZaPm5C5m wRysGAMJtIes057tTvGVuAAJVF9D4BU= ARC-Authentication-Results: i=2; imf08.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=y2yuMNXd; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf08.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.54 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=1671048091; a=rsa-sha256; cv=pass; b=537tTa1YQBEbBE2VFMpu5Dzq4fy6m6ySs3FU3ZT24k0A+iL05xxKIuu4IyX7Fct59DqmNr CQLNhTEA3KXj4zqu9ZNFp1uSRvYh16q6ARUAuuZJgLHhEokicvfF6Lba8PNDW6nKcvwt/9 y5MD5czocdMjWocomXVgm5v4XfZQBSM= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=et/pSCXO1Pu7zUui0Dt88nBwf8QcIBM0mH7v1AL/XONJc26rvBcDEzKpTmk+4ppNhb4lhnSFmtd3ROCDdr0AMYRs4czsdEIejZbl+f+erE+lgTxWLMR8WxJL1eshOD1k4BmAkuZajdscsAc06lyb2pJ1zQi8Wz7ivrCVOHGTXEzVwNvUCwhF4I4u1A9dzQs34hEFHa7DYyccxfdKcxUMOz9sl7v/J73jkHAngWEzTB5ghZGoDLy7kFmvwz0yZkqV6RtE+HRR15bAsGMjxFempjQ/YNhF1KtuaZzgvuNfC9j5nqzSm/OBMcSHKXZGoDSTVG0yJSknQnMd5r5pscIPcQ== 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=GlVqa2qmkaaiQMe4N6o7Bdn+VWWRYcbCRi5kfE+AEcs=; b=VOFO1kFg9cIn1nbmsYnyptDGWujJMbH9CddmAWGDCzTNHashUJP63+E0IIkzon0S9h/sth0Laz6IMSjAoXMypNJ5LxKQhtZTkavGXaKBQjV6cszccK+dL21I3zLL9hCfA+Vxa8PnIzlw0kwNTGo1zvSfx9hUBu8pCvdcdYWW9q0OgShHTjL0Ul6NTdV9Nfe7PG7VLKCcoD5DcGdMvDeeM+7JbEahpIZOxLRMv35tqSp4ByiMxjyZ5Vg9KsPT3x0LRm2BbU6Us29WmwyC1tv/dEF5MX9n8hgNX77CpHqDgu3LLNpreN3faAdTKiSwusp+duPJlWBF8K++sDKMq0tcoA== 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=GlVqa2qmkaaiQMe4N6o7Bdn+VWWRYcbCRi5kfE+AEcs=; b=y2yuMNXdA8MtNSwuQ+OFJ6s+1acdCDTG8U1Ve+cHOduen30yoNZAlsOhM7PIVvsalhclr+xgju3bljR1NSFgSX9eXVg9s8ZF0xu2XBERtknFNNzflA1eiePWtoPcdenG26TvIHc18w++L+D6DWM2sxtygZgl6rTi/RoXyt9cEdI= Received: from DS7PR05CA0094.namprd05.prod.outlook.com (2603:10b6:8:56::11) by MN0PR12MB6223.namprd12.prod.outlook.com (2603:10b6:208:3c1::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 20:01:27 +0000 Received: from CY4PEPF0000C971.namprd02.prod.outlook.com (2603:10b6:8:56:cafe::cd) by DS7PR05CA0094.outlook.office365.com (2603:10b6:8:56::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.6 via Frontend Transport; Wed, 14 Dec 2022 20:01: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 CY4PEPF0000C971.mail.protection.outlook.com (10.167.242.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.10 via Frontend Transport; Wed, 14 Dec 2022 20:01: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; Wed, 14 Dec 2022 14:01:25 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 47/64] KVM: SVM: Add support to handle MSR based Page State Change VMGEXIT Date: Wed, 14 Dec 2022 13:40:39 -0600 Message-ID: <20221214194056.161492-48-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C971:EE_|MN0PR12MB6223:EE_ X-MS-Office365-Filtering-Correlation-Id: 8663c683-ff99-467f-5989-08dade0dfbdf X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ZveNpknXwjrnDQoLsSKWEAhz/NYf2kdEvJ1IE3GEcX8Hf8i0u8He+dteQrFLohtLbQztcQVYbyppdEUF79rupi20ct4PlotG4MQxaFaUCXeAfLPKoHfZ+fJ5S8Osw9b63Q/Q7KWAhQAMSTLf0c/QTw9vr2h5xmHX+QJ6ln2ax0hVmgw392Jz5erztSez7g8OAS+AW0gJYrBhgAV6YFJVnKjxKmUloq4QtGiXDKwkTcIAcrh0t3L/MA3g36LG+iqyJMz0EIij0Fhg1qqepZRVv+aYnNkhxsotn0udiwWSzG2JGkK6J7OspFsWfTs36O2z44NDnRpuOuxmS4Gzl6o/p9H/7JgDwSpIw6mkIA/ru0BRTefqvwqsk0OqdXa3revYQP7Wh9ky9YjUafq751G4tnT3+hNoRoI6GdiiYv/pWfIah0uUK0j/O5iSSko8u3+l2C77FcPB1PnsXy66cyRncxANdbsev7cD898kUrhUTR7b/IQXxg45KxcLbI9ATWhMahL/jhcrxA9x9sBAD5xlE9rRMNTuw219IFa3sJOfbTrSkYAJpqbK3Iik+wYuMpHpEvLxAXYvNSqM6g9bilC38FC/PRtEn4Rp0VtcfMOetQu8/UJJInxzqvKX49F+jLff882RdyFYAPjXAy7UbA1eSM+iT7X+Jcpi6aoqoIlyO+A1SCisQnrj9zWps1nJXJWRwFNxcLZM7WwZm1APylQr+Sn4nI1HEqGjaPtcb9zsxRE= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(376002)(346002)(396003)(136003)(39860400002)(451199015)(40470700004)(46966006)(36840700001)(36756003)(40460700003)(316002)(44832011)(8676002)(7406005)(8936002)(7416002)(41300700001)(5660300002)(16526019)(426003)(47076005)(356005)(70586007)(2616005)(186003)(70206006)(336012)(82310400005)(1076003)(4326008)(83380400001)(26005)(40480700001)(36860700001)(54906003)(6916009)(478600001)(86362001)(6666004)(82740400003)(81166007)(2906002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:01:26.6912 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 8663c683-ff99-467f-5989-08dade0dfbdf 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: CY4PEPF0000C971.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6223 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 06F0816000E X-Rspam-User: X-Stat-Signature: be7depx87zatwpk9xbufcmrrobfb81xc X-HE-Tag: 1671048090-24113 X-HE-Meta: U2FsdGVkX19guoWTMOJXxEGm8CFfnAK3PWfvLgBkwuCdFxwOIVAKiXAF0fWd8OSV01ctVFJRgFXZiV87Serme4SJuZKo708tJ9ioSppLzIH1y9Ii66YvBCZ1oIMSG0NyXh8XN6CLTcgFuXOFK9g6nHMbZC+XKT/NmNhK+mN62lkl9dRQiPb0nLKQBUxfo+nl1Z+ol6+b2tgTD4Od28K2+vlLpIfwx7lI5Z2/BEFDS06aRhgxuKExuiAP8pcu7JeeD1PWmyzF3xj7f2iP8nbLTiXMC6wU8zlI6LDyfQDk/9zS5LL1ZnvZKkzuFTEJ0ICBq7cAykdt098fJytIIk10kKAbGGkm1dAXb6gkoPopJGpna+cJpt5d2uBTEXd9kOUQNUpahxcZ1lPkEqidekMhsS/lDxfjxYRZA5CMmEatVA0rtANXUPmgpxK6Aop0JMAFv3e5LUrjKtH745UJ2+LZIGhF4nD64kCuzHXpNZCzFLxVc0WokhQjh/9Vu1FNn68SDuWw9FNAL5AyffqZBZPIM2/W9U+Y4f6/84fstSFhXTngO+8O9Qhjzi/qTrx94FAu7JdsH60z2Hl5yrMlm9TrG/pv/p5N+37baY9kWX3VDFtqFwFPC+mciwkpOEHIHMUGVc/GT20cnhiHR2Zmcw2UKFy83DkE8w/X+q+7cgf6j8qegwayojVr7nM50TA/0j2ZsdQOB/dUU8C/w1d8MtnOFMqWMwKSNC2iA6gIX3g8Y15/Zj2b3vbEPNypQdmSILsvRr0nenfL3s/4jUOCIFfkXOPXkLQMtOKy3Ln4O8x+BamIXSZbVvOEAe4743gGu7wuH9jqnKKA5M3uNOq1OR4EQDuTuHhLNC5CeZDj+46GWHLTsEPprjikclK78il24JxUbFrttqnVHY/xzJKhuFN4C0wuFQWHiY3uK7Qgx+ZPA35fAO3RkF9EkbmRaNP0dnlDrbf+DVFtpn3XEg5fhH7 J2hRM5W5 +ucUD 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 d7b467b620aa..d7988629073b 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -29,6 +29,7 @@ #include "svm_ops.h" #include "cpuid.h" #include "trace.h" +#include "mmu.h" #ifndef CONFIG_KVM_AMD_SEV /* @@ -3350,6 +3351,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; @@ -3450,6 +3468,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 732f9cbbadb5..08dd1ef7e136 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13481,6 +13481,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 Wed Dec 14 19:40:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073461 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 DEB58C4332F for ; Wed, 14 Dec 2022 20:01:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 80D508E0009; Wed, 14 Dec 2022 15:01:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7E46D8E0002; Wed, 14 Dec 2022 15:01:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 685C58E0009; Wed, 14 Dec 2022 15:01:54 -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 585498E0002 for ; Wed, 14 Dec 2022 15:01:54 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 0BF75160F4D for ; Wed, 14 Dec 2022 20:01:54 +0000 (UTC) X-FDA: 80241982548.14.37CCE50 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2043.outbound.protection.outlook.com [40.107.101.43]) by imf04.hostedemail.com (Postfix) with ESMTP id 0B5A140023 for ; Wed, 14 Dec 2022 20:01:50 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=gwC+03GN; spf=pass (imf04.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.101.43 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=1671048111; 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=9kFUIUAb0hyW9i3i+pIzOyi+dQb0DpLo0gJKM8xz7HE=; b=5t3Jssnib4i0NVxHuMXJDw2Bw9RgyL2ExNtVRrHD0eEVqA92ac4FHS3701ptOKSCSXQbkc GeBUGbgPqDbwwvyl8AszcYjyskzacFpVL65qtwV5hTHVEF2fAM04Lwv2ltcRga/2w65Osa 8PaBSTg1hGOiOoziD6Lo5i7jqWf5D/s= ARC-Authentication-Results: i=2; imf04.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=gwC+03GN; spf=pass (imf04.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.101.43 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=1671048111; a=rsa-sha256; cv=pass; b=kfw4UA+idCqZDMAYVIUa/KidlhysRAix7S/HCYkVrpvnAL4xIUfm5G2YXjsTlKqxpaR/Zp AMAy0G6sMGHG+jOBQNNZ16X3LJmfoL49w439bqNUp7elEdGsXQpkiteChQ6VKuQNwENnOO w9f4Cw2x6+STdhwU8ns0XTu4bEPYFZQ= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=H9tShW/mbNybmPxfm6mLgcjWi41B1iMGwE67JibC7sn6nqJ5rJKC33d44BWRhCQFKsjVPGs/PF1+NrBd9oGkuza3LIL4djo76Edoaa5CpJSc+z0dVdnycbrU0ZcjpmPe2lZOxK+lXpLlaZRrTvyl0UsRZ//0jqxZdoiN58oCRXrRjmPvG4NJQKevBxowAk1liWknbocv0+HcOnAIbINVGDvB4Y9+HOzQKuiDLSvNINZFppipEXiXiGz51xez5YTwp4Dj8IiZdsUHgLVKlNA9N5C5QBDyLSIl3puMIG3NJ1/2IhQ/8X1jVGyKMU9QOWjQA/d41Sck7kwPNZXb03dsJQ== 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=9kFUIUAb0hyW9i3i+pIzOyi+dQb0DpLo0gJKM8xz7HE=; b=O5QbqDM+MOzIWMbsUfFFFjjVqykGwEk1sX9p30oP0n0qgOE+B9FhNsNdFf0LTQ++gUiG9HirNHWuJTP/RpytJBs2eInEe3OUeAuw8RxnVLoR+SnR9poKr5p8ircjSHnTb8XNlY5Se7f3PfzXWapqmghOU7P1R0f3GE83EAffMen7R7OsZ0kZhAzTkfMvTOe7APvhsjw5oH5dFB9iLeu/SsPTv5iF2a7eycp0WJYkUu88zQqhh1evYSWpYOQeGCYF+wRVDLK3bfwLfaTQGf7dLx7fSvCRvUWZ/qAG3lDMbQV3hxQ7NwXGI4vK6stXer3yghXR4Vhv3fnne/VXsxwrVw== 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=9kFUIUAb0hyW9i3i+pIzOyi+dQb0DpLo0gJKM8xz7HE=; b=gwC+03GNXitbim1DNmZ6+6VIvEUIuoOhKdt6/xKosp42ppJSBUVWD6wzpVzkjtiH3jxCECcVAaF0O+avISsaig1ZgOty6Rrt6rIPRjpJZGYeKyjE1amtbxctaDCxdS4byJ/wRseYWWuMkuv4kNzqLmTgQZIJ8HpPzLkyRxq6UIc= Received: from DM6PR18CA0017.namprd18.prod.outlook.com (2603:10b6:5:15b::30) by DS0PR12MB8199.namprd12.prod.outlook.com (2603:10b6:8:de::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 20:01:47 +0000 Received: from CY4PEPF0000C96F.namprd02.prod.outlook.com (2603:10b6:5:15b:cafe::79) by DM6PR18CA0017.outlook.office365.com (2603:10b6:5:15b::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:01: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 CY4PEPF0000C96F.mail.protection.outlook.com (10.167.242.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.8 via Frontend Transport; Wed, 14 Dec 2022 20:01: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; Wed, 14 Dec 2022 14:01:46 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 48/64] KVM: SVM: Add support to handle Page State Change VMGEXIT Date: Wed, 14 Dec 2022 13:40:40 -0600 Message-ID: <20221214194056.161492-49-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C96F:EE_|DS0PR12MB8199:EE_ X-MS-Office365-Filtering-Correlation-Id: ed048386-2c81-4f8b-3cf3-08dade0e0866 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Y1keZEAKZfOkN9afkJYpKHOnOz+AMze9tGVi52NefP4qJ1V3nX+h8oEnhwfjdqTy3lHvWofSG8O5M5hjMVJoakEQSLuIHXVsfi2pclsbo+XLQ9POexkv/wo1EEOkbw3XU6Ukl40SMzsrw1HKfSIR8HZdA+dsnFObTmTP+W5+QdmR1F3CciVy+aeJu2wai+hTWXuMkJY4vWfEz6VXARPHOaf7EnjCc1LxBuJDssongzU5cNHPR44IZHqCrFFstj/vK3kqINP+wc9+QbQhkks1krL0JD2wNrHqALhsQmdD5bF2Av992JhwCxbj8mzZ0yKVdfNRuSoZ3rmsXzNUWEVLvRT/wteRG3tUGYMQV+rffSYiKChXqguXqJHgMgGZ8HPgDz17xl5Nyv+iiL60SXhRJY1hCnwPWdFjO2FkofAsuFpF9edEb1SRzUff/lxd1NS0xz6lfpjoC/fdCDepmKbScz0dX1drRQcVQrzPK22ySCRyqzOmQu4RE17MdfGoZCvMbg2IMEjiFhJgCQ96RybCiv2KvoNQZHohk98RVW7lvq6Tf4XpuM9xH3xh67buwMVlSGUqYKJdahRuig3uDXdrKJOx7GjFnSK5HjQn9dNGGrngkJ3UlhOdgxtBGqF2/09PdcRmUVHEAEVPBHF58z+RAAI43YGiuS/hyJ4XP/xz3ftURKcVShSAeXR0udkIutXB6x3cdjJYlXxzaWbOjKaRfkwSjWcN+zE1wrs24WqV7MQ= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(39860400002)(376002)(346002)(396003)(136003)(451199015)(46966006)(36840700001)(40470700004)(356005)(7406005)(82740400003)(2906002)(81166007)(7416002)(36860700001)(5660300002)(86362001)(44832011)(478600001)(41300700001)(40460700003)(40480700001)(6666004)(70586007)(4326008)(8676002)(26005)(83380400001)(426003)(82310400005)(186003)(8936002)(47076005)(16526019)(316002)(1076003)(2616005)(70206006)(54906003)(66899015)(336012)(6916009)(36756003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:01:47.7078 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ed048386-2c81-4f8b-3cf3-08dade0e0866 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: CY4PEPF0000C96F.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8199 X-Stat-Signature: 9exhjzktoi91qqramjcojw9n4r7is5gh X-Rspam-User: X-Rspamd-Queue-Id: 0B5A140023 X-Rspamd-Server: rspam06 X-HE-Tag: 1671048110-539467 X-HE-Meta: U2FsdGVkX1+MPgEw9ecXXlXM6EP9z5tDt5DhNBR/72jgUZByEOVFdZVPE8qZuG86pIx34JVtexAHCoNyVu1aTXXDnQFCd2jrFNR9Qd1sN8PAO/1ce2OtLt9sMI9IFU+Z9BsOVUPtK9+uN6j4TZy22jfG3Mi4BNWj7eHSo0raT0LyjqmeT1/Bm5sg4SK9qOemFfW8DqR4vWJJO0wmWd/O0PXDLKX0gJqL6nD3MfApvnUtxpr0igKBSvuAblsz6QFvU5fXd0CqwQ7BdcbMMP5G+csSbhxYgxdkR0rBRkagbKK1GST8SuMVqNbHH42cbXqkoF1PK8138sR/lN0STlq7o8gCK5gTdtqVnN1kX2+s4oVHRFJ0hFj+aE/w7qlwMdibbi48b4ydFtCxLIk0GFb3jm9b8W7dA9kg+hvMndyouqZoF8LHUAK+3w9HGZ0x/yPSuVTbgnXDrLIevOtXvOAWsa/LusdnQZ3HphvVViK+bBO4r22ut11912MjCKxYlSpTo+zYLSkPfDTaUlAelAVdkyap+ZG/i9vXRFO5AkuK0X1tCngVNRAEN+w+ryLPKkEgNe9dhII+giDd29qFPn/D2OUCWVfOGDZcJIMRE6tu0akdMX9ckTgMc1pcTVnrz12Bl5dErsDfi72pRMsQ9MGigGXf1Lt5sfVSeCsQ9xUqLpqw/C3B4toW15U7PKr3U+Uc2qJXLKabGuAVtUzeLJdM+3jbU86fGwzCjjtLdrOEFTJ6VMeagGj35T4gpBa6Y5UEcmZbA9cJeA2LLXQbooG44iGVRuBOUq6hTBWpdRo6ZRAzAjyKL55q+ieTvDStovgRXU+BAEsaEtsmhEFsJVY++hFyN+urrSHhcX7ghP+h+YQyru0PP9DP0OZJxv8EtcaDKhfi7XTE1KXC7zM/mLr+Qrw4P10uZOz6CT5sG6MAOGBIv6/ixzG0atzGtlqRPLbzGIWKHUe0w0cx65gzo1l IRI0PBmr shbrK 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 | 18 ++++++++++++++++++ 2 files changed, 25 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 d7988629073b..abe6444bf5d4 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3164,6 +3164,7 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm, u64 *exit_code) 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; @@ -3368,6 +3369,17 @@ 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) +{ + svm_set_ghcb_sw_exit_info_2(vcpu, 0); + + return 1; +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3595,6 +3607,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 Wed Dec 14 19:40:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073462 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 7C7D6C001B2 for ; Wed, 14 Dec 2022 20:02:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1F26E8E000A; Wed, 14 Dec 2022 15:02:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1A29F8E0002; Wed, 14 Dec 2022 15:02:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 01DC78E000A; Wed, 14 Dec 2022 15:02:17 -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 E363F8E0002 for ; Wed, 14 Dec 2022 15:02:17 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 9A8AFA419F for ; Wed, 14 Dec 2022 20:02:17 +0000 (UTC) X-FDA: 80241983514.11.80AC928 Received: from outbound.mail.protection.outlook.com (mail-bn7nam10on2075.outbound.protection.outlook.com [40.107.92.75]) by imf27.hostedemail.com (Postfix) with ESMTP id 447B340027 for ; Wed, 14 Dec 2022 20:02:13 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=ITt3SqFP; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf27.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.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=1671048133; a=rsa-sha256; cv=pass; b=FKRZKVTD/YyuYxcqUj6UgIcHnUbHCnmGlfkj46AOF4SkQDg1x9TzF62Pto5rUOrjJFtH/S hUmDKgCsT6ZlhRfjT3hKcgi9FbfuNlPTrLaQ6KfA4bghEQxqyqD6+Zrbkm2MT3b6c3+sC4 LCF+Wt50ok9ZA/OgKtUkXrZL/Scl3U8= ARC-Authentication-Results: i=2; imf27.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=ITt3SqFP; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf27.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.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=1671048133; 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=iI52L3YVAxI6dNkrnUQzy49EWWUwko1VEEKryV9QQEA=; b=0K3gQxYSSQhZlS+oMixOtrIvCLuJ1nrqvrrjRhHRLVqAUS6oaZ+jITNw0SN7vDojdR3nTx tLor2uDILdE1xUwYNuhhQB8ZGx34AWmmaa6+8gTMJv9aVcRTz/C0PEo2iwV52xvs7LG9aw gOrpUD3FCiS37MLpP5vGvYB+aPXNP0g= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OzGJ0T7GRKgPB/i1vX3rYSXrEUvyIz/BTQdHdVV0G45Id5RqBtjKeaSOuHApDvvfzkCokcNAUjlIHKjZ5rGF7HbKVdHFLx7AwgzmzsVMEgUVHM1szbcRzHAkHTFmJ9//31+Quc0GoqX1jgCknFiLas003k4ywRMKca3ufx1zVhgPFATExzojWeAm/6EIBaYnswF3tUqig0e/Ep4Y7So6BtZXKAi2DJrKBW5A+ssQQuPXaM+7tD4tIT/ebWiTfo/ryRN1GmloA05PR71vomIFBLr0k71+fqrA5sMPiWSy072dIkWpExWaeOyPKsQLoYjIcEpztq2OovWpnPpCRcT23g== 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=iI52L3YVAxI6dNkrnUQzy49EWWUwko1VEEKryV9QQEA=; b=AmU2+RL9nACRyGJf7APnTs5iOfN3gix9JD/TP0cZPlJgM0IYoTBCTqqQKbOlL7zVlbNyThzfEKl+Gc8GlqIhpNgxGTxn28Lgwt7k8TXeEKYceDd8daqI5swCFttLxXVQFDfq2kj5V76Esv4z/RuF8B8Yvc2GudfVKFz38KFMdBWlZ/Puu5d4RxucnvMngmoGTYT3QkwPOeF+kPIfTT/4H7dn3zUEE/LTuYLYjnEsvjPYr5lIxew7a0ozLILTPwKE8a6Rj8sfE+KMc5Dg6o7kpctNdail+x0LNLDP16g7Fw8jFdw4126yIyxHIEreF2/d4K578NoVQBfC2v/wNa2T8Q== 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=iI52L3YVAxI6dNkrnUQzy49EWWUwko1VEEKryV9QQEA=; b=ITt3SqFPXwh7S5SEutMLIgWUHoIK70+kmCotvD0liKrcdXZFXpkhwiWBTsqJaTDF6Q5jn66phuND7LGwQ1UhHCdvuMw8yW3ZKuiZnFdJjdu/T8RAFSfQ4pNhYbtKY0TBGKmCUCTcfVSO/j8zONtyKpfxFYoWwwcTb+dpWHBYT3w= Received: from CY5PR18CA0053.namprd18.prod.outlook.com (2603:10b6:930:13::29) by SA3PR12MB7879.namprd12.prod.outlook.com (2603:10b6:806:306::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 20:02:11 +0000 Received: from CY4PEPF0000C96E.namprd02.prod.outlook.com (2603:10b6:930:13:cafe::2a) by CY5PR18CA0053.outlook.office365.com (2603:10b6:930:13::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:02:10 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000C96E.mail.protection.outlook.com (10.167.242.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.10 via Frontend Transport; Wed, 14 Dec 2022 20:02: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; Wed, 14 Dec 2022 14:02:07 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Jarkko Sakkinen Subject: [PATCH RFC v7 49/64] KVM: SVM: Introduce ops for the post gfn map and unmap Date: Wed, 14 Dec 2022 13:40:41 -0600 Message-ID: <20221214194056.161492-50-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C96E:EE_|SA3PR12MB7879:EE_ X-MS-Office365-Filtering-Correlation-Id: 92d0b10f-74ce-49a8-5f0a-08dade0e1623 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: LJLySv4lX4OdfArM4C8JCtj66cKwiGvv85XtGzM56m7Le/Nrwu0b4z+kB3JKjPHIqxiz7I+LJ0+6gG+BtMf2RkiQS1yj/fGzICEaBrf1GEO4raTkvfwm/WZG3uuovESwc5CV9KO6jDdanyVG+MWugU5rq6W9AcVJE4t5vcOnHr3h0qiC/FNjebEaWT88giQ8HJVJbGupJUlrS0K81nJui0pzJeAqdTaNIhJ2VBnwhr9xK6nGsOJYrwqDN4qpsb5IQLcl0dSwgXRoTmokzE9Q3B8jS4jocZ5lr49vBFGKKvAxLVaz3SqywPy/aQlp2PkkSo04s8knS6jPse/CQpQy6c3E9nyxGx90Z+lJvsWOCGG0jk2myZe121NJugxX6x/eM9dEwIKgAkNMTBoZcpfwFLY6pT0cDLSIR7jGoE3VPKiRjf6zCZHKP7OzepmUqwXVIUIRbvHADqIaytXnKR2bs3SLV7pbIKLbC2Eycmrq2k5TsPiCasqsfRIId+56WN3z+pYbXxzT6RzXhh0ZCjJAFsCAk8yZ3/7CYgoUrBT4m++MZxdisPyuFjMMpLdVW0JrZD8kaZBpNPeTee+DrSdIMq1vBZAklZr3txYztd4hhwhpF01jD/T2sSKSGcTiRGOnNPfUF2iDvyF2ty3Bp9G2aN+AGrVpft1eBnMVd/VgzZEipcbPaFVDl8jS7FJUUFDGnO7nF8HjulWHkT+zKzW1x7LYzvsTkDDY0zBOHKFLjygK5qmKZokUjayrh8UHZcbynpEMm6dOE5KyUcwbuq6iGQ== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(376002)(39860400002)(136003)(396003)(346002)(451199015)(46966006)(36840700001)(40470700004)(966005)(478600001)(26005)(6666004)(54906003)(316002)(8936002)(40460700003)(8676002)(41300700001)(4326008)(70206006)(7406005)(36756003)(7416002)(70586007)(44832011)(336012)(5660300002)(47076005)(16526019)(86362001)(426003)(2906002)(40480700001)(2616005)(186003)(36860700001)(1076003)(81166007)(6916009)(83380400001)(82310400005)(82740400003)(356005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:02:10.7578 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 92d0b10f-74ce-49a8-5f0a-08dade0e1623 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: CY4PEPF0000C96E.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA3PR12MB7879 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 447B340027 X-Stat-Signature: qtprnohfjwqmf7c369e79rru8a8ug4ns X-HE-Tag: 1671048133-630154 X-HE-Meta: U2FsdGVkX18AgUIiSFFyBNmIrpEUt1D7G6qw1iuHbTzWbl9ZZ0prqpA4eqWT9tKuyeVeTzgPyWyme9gAETa1YRS15jaqWDikIX92HYhpAqVzQruC8OKI50SqH5mrHGNOZgCtpPnYlZsfEEWJ/iya01h52K1SDJeVDmcotO0lzChKBq5PTY30kPHgW95KAxGTzlAzFYoTrEnKZUV8ZpKpBPmV5USbgPlQyiUo7cHY4mCU2TVq50QF4BrVGxY0kXUoMMIFXt6EHKDxNGCfcWHajKfsUV4pBRGcMsil9RbcKFH7dt9+/ar0NnWoex4gW0GXShTetTzhLuhIfrkayOA3AEgAIgd6uql1ClqzZLEsGfjCTK9dmWQRHnFQM7UH3DWiC/PMxT5JRO2bqCwlNUWFM0RqnlbLTorY3jlKmWr6u5x0ptrSthezZYPNZD0HjSrXa/vXAl3MDwCWzp6G4HISqoI5O5ogGzYbSFWoWdlMnwJI2KOHhOcLTXuewpDklzkFpX35l7lPUJ8TFhFflQzIVE/8zmcx4doUb13G5vCtnIfy/4Fu6+HZorP/pzwfD+lTAJ+df9jFU6/3yv3MM8pA4ps3XEhj3sB9ndFYa3ZpsSbpfVfynT1FodqSNbMoo8+S/AV4iqdz+xw28eKtZifGNEnhKDLgQMVEFY3R5Zecl1tugdG4lNQ81pzKcGGQ9h3aHilBWtbv8CMuUKbdAz5wFx55Dlt1GPFzlWT+WS/M9qPS2mNzeBpNf+iFgO343elCOuSiPGT2daK7/35962i+OX8I3nXxpJtL7tCu5QP6TxrWE7n9jPxMfZBqpSQqKIpZ4e+hD03oW7ia4B1P69FKrN+WZAWAJG3saAoWyoG0kbN5XSMpxHUJaIHySs0UkuXI+2qhIVHrHDnwVeaCHY3pjfXT+Bg54zKe1sQVe1uJXF27t4s9dikyWSPQKfQ36vhj8Ky5kYNMFhH01ts4wXo PhzOpRpP ixpRQ 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 VM, the guest memory pages can either be a private or shared. A write from the hypervisor goes through the RMP checks. If the CPU sees that hypervisor is attempting to write to a guest private page, then it will trigger an RMP violation #PF. To avoid the RMP violation with GHCB pages, added new post_{map,unmap}_gfn functions to verify if its safe to map GHCB pages. Use kvm->mmu_lock to guard the GHCB against invalidations while being accessed. Need to add generic post_{map,unmap}_gfn() ops that can be used to verify that it's safe to map a given guest page in the hypervisor. Link: https://lore.kernel.org/all/CABpDEukAEGwb9w12enO=fhSbHbchypsOdO2dkR4Jei3wDW6NWg@mail.gmail.com/ Signed-off-by: Brijesh Singh Signed-off-by: Jarkko Sakkinen Signed-off by: Ashish Kalra [mdr: use kvm->mmu_lock instead of a new spinlock, this should guard GHCB page against invalidations] Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 40 ++++++++++++++++++++++++++++++++++++++-- arch/x86/kvm/svm/svm.h | 3 +++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index abe6444bf5d4..90b509fe1826 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2926,19 +2926,28 @@ static inline int svm_map_ghcb(struct vcpu_svm *svm, struct kvm_host_map *map) { struct vmcb_control_area *control = &svm->vmcb->control; u64 gfn = gpa_to_gfn(control->ghcb_gpa); + struct kvm_vcpu *vcpu = &svm->vcpu; - if (kvm_vcpu_map(&svm->vcpu, gfn, map)) { + if (kvm_vcpu_map(vcpu, gfn, map)) { /* Unable to map GHCB from guest */ pr_err("error mapping GHCB GFN [%#llx] from guest\n", gfn); return -EFAULT; } + if (sev_post_map_gfn(vcpu->kvm, map->gfn, map->pfn)) { + kvm_vcpu_unmap(vcpu, map, false); + return -EBUSY; + } + return 0; } static inline void svm_unmap_ghcb(struct vcpu_svm *svm, struct kvm_host_map *map) { - kvm_vcpu_unmap(&svm->vcpu, map, true); + struct kvm_vcpu *vcpu = &svm->vcpu; + + kvm_vcpu_unmap(vcpu, map, true); + sev_post_unmap_gfn(vcpu->kvm, map->gfn, map->pfn); } static void dump_ghcb(struct vcpu_svm *svm) @@ -3875,6 +3884,33 @@ void sev_rmp_page_level_adjust(struct kvm *kvm, gfn_t gfn, int *level) __func__, gfn, *level, rmp_level, ret); } +int sev_post_map_gfn(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn) +{ + int level; + + if (!sev_snp_guest(kvm)) + return 0; + + read_lock(&(kvm)->mmu_lock); + + /* If pfn is not added as private then fail */ + if (snp_lookup_rmpentry(pfn, &level) == 1) { + read_unlock(&(kvm)->mmu_lock); + pr_err_ratelimited("failed to map private gfn 0x%llx pfn 0x%llx\n", gfn, pfn); + return -EBUSY; + } + + return 0; +} + +void sev_post_unmap_gfn(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn) +{ + if (!sev_snp_guest(kvm)) + return; + + read_unlock(&(kvm)->mmu_lock); +} + int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault) { gfn_t gfn = gpa_to_gfn(gpa); diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index a4d48c3e0f89..aef13c120f2d 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -100,6 +100,7 @@ struct kvm_sev_info { atomic_t migration_in_progress; u64 snp_init_flags; void *snp_context; /* SNP guest context page */ + spinlock_t psc_lock; }; struct kvm_svm { @@ -727,6 +728,8 @@ 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_rmp_page_level_adjust(struct kvm *kvm, gfn_t gfn, int *level); +int sev_post_map_gfn(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn); +void sev_post_unmap_gfn(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn); int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault); From patchwork Wed Dec 14 19:40:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073463 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 4977FC4332F for ; Wed, 14 Dec 2022 20:02:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C524A8E000B; Wed, 14 Dec 2022 15:02:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C021A8E0002; Wed, 14 Dec 2022 15:02:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A7CF58E000B; Wed, 14 Dec 2022 15:02:35 -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 9AA618E0002 for ; Wed, 14 Dec 2022 15:02:35 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 744051C6762 for ; Wed, 14 Dec 2022 20:02:35 +0000 (UTC) X-FDA: 80241984270.16.509FDF9 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2050.outbound.protection.outlook.com [40.107.94.50]) by imf01.hostedemail.com (Postfix) with ESMTP id 9843D4001A for ; Wed, 14 Dec 2022 20:02:32 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=LzwwiaBR; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf01.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.50 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=1671048152; 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=2cUxxQ98/Df0ZZ4W4Ql1QYz2Nc518nF/wDeCqB4P90M=; b=w68s6K3t143hgxqL4h7V4zooM+jX6QRQSwv+jBwPIfdmnNLj05iIPAGHiDkm7WUcxGhhCn hQIXdwj0yfZkMUzFZ3X6a0dCXuoFL9WDv8v/Uu6YqbyatT02h26fbW0d1JO51fvNlJtV/R ymvYa5Ds1srfXrA84HqIVlCwvz9PjS8= ARC-Authentication-Results: i=2; imf01.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=LzwwiaBR; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf01.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.50 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=1671048152; a=rsa-sha256; cv=pass; b=ayQte/Cqczj9k4x0bcEFf+qEk90NaCaRO40mgpoS0tiTTXit6zDoHuYaaaYJ+ftlw0mo91 iA2sXPa3g/KwNx2fhiO+whRU70Tv5IuEcWiKl8ljwgeX5mvotXiew60zEsPKC3+fitcHJf 5tzriwP7ughFMugGB5m0ZpzTCnQYa1I= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MZf2eOmzkNHGp5jgnXa4ZFKd62SLB6lZaOgmxHwiPurbe4tCd3UjAEPMzSSdLOK9siIQ/jR7U+2E8/WXKktMjqS+FFxzAyuqfRqj+8XJX3s4Us+DyRYBLogSvxNcjrB1stIj3CH2x1zrjcgv3Z+8RdvJt/gv3mjg4xZVAi2JgqlhdIWHmjwh76j8Xtb5MVwIXWHkiqVSOsFOnXR3VpbPMuwdc3l5gj7VStALW2HscCrJWwZFcgiQlYfHCIQyuySUrr45cVerolBZblq94JOGcEX3E3zSGwPwol2qJVMG9kgZD85NVXg1+A/FfaezRH3gHa5SOlTjFwtvVAre/j7vRw== 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=2cUxxQ98/Df0ZZ4W4Ql1QYz2Nc518nF/wDeCqB4P90M=; b=DiSOlPHMG8TOIReoOVz/yI/NBqTl6v0BZ/n6UZpClshAS+EoD+79ZLpwkXv/5v0rGvScD+0h4SZSwmOVJdvXTxPCAWYOGNiJK/RoiCwCCz7c/4v/QTDBjaNkTt7wY3diET+mv77Tp3taQm4zDiy4w4pR9wzaWHORbnP7d8iQFsrIuOKfkBJltF44k/29ptqfRSzGOA8yoVA+n0PxiQlqgkjU5EvIqkmi9nXybsMpX99AZ2LDzw2kvw+Y6kDX8hgPK0iOs/+N+4+w7EQv1fgPiv9EfrOJGypja5RMb7k6D3aeuYDOIsKiCsryiamJjbhoUYAt9Gqq5FkLw4WB9KEMBw== 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=2cUxxQ98/Df0ZZ4W4Ql1QYz2Nc518nF/wDeCqB4P90M=; b=LzwwiaBR93+2qQ1nvGff6INTs/ZwXe1D23x8OjXI6hh5ZG7vBx2xfYQQEozg6rXl0LM/g3HJLK67zdmHSwv0BStW+tIAGlaKqpa7NYr3bvwfJXDdwxjhB21ZwDmUyQaoE18xIaTc17geUZLklyUoEBvYwX49tgGuMoORkSkjWDU= Received: from CY5PR19CA0082.namprd19.prod.outlook.com (2603:10b6:930:69::28) by DM4PR12MB6133.namprd12.prod.outlook.com (2603:10b6:8:ae::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 20:02:29 +0000 Received: from CY4PEPF0000C970.namprd02.prod.outlook.com (2603:10b6:930:69:cafe::f4) by CY5PR19CA0082.outlook.office365.com (2603:10b6:930:69::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:02: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 CY4PEPF0000C970.mail.protection.outlook.com (10.167.242.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.10 via Frontend Transport; Wed, 14 Dec 2022 20:02: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; Wed, 14 Dec 2022 14:02:28 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 50/64] KVM: x86: Export the kvm_zap_gfn_range() for the SNP use Date: Wed, 14 Dec 2022 13:40:42 -0600 Message-ID: <20221214194056.161492-51-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C970:EE_|DM4PR12MB6133:EE_ X-MS-Office365-Filtering-Correlation-Id: 0f434472-a91e-4234-cf5c-08dade0e2163 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: VtIzu9W9Pq3pOk4vqJ3WZK3b7uP0pTIlyDtBq4Zwa3Q/s70Y6Ggn8DuqcuMZxUfr7Xfwe5NraJxjQVizG8/6PqOAtqj58E7qirqbtvG0vmHhVnXkOQROuHLouOvDw+8byuWgGtizZWZ2DhJ6QNwIhOiV/dO/5u7dV3WJnHROR6GiYHPboPh7I5UIHVsK7dsH24Rq74xH0mt/Q9alkbX8LZNghBbb9BdhPrvslNcFl+QlkJwrMlaOU9j7/AI8iQXehkej8b6bq4ZOIJegVFPHTuvLNqMcXGQle1dQmlqi/DXpOuMPuuc6ipXiBaOlZn6A0nnaFDj2h469AhpF57l7Aou8d1XxIiCfoNdzKA+yNJXZSh4pkqmiGG+bOh1IE44VgH/WjSISAmIYHtCVbVswKzwwvc9cno7TWqcxfiLFYDdvGZ78IsbAUrzJ+P9obIM0iEuSh/1zHJn6d9PKxbpn4QF+V52OoNw96iB+e4XmlKA36LhVPq5lT0yg0GwhPH9eptuhWdcs86jopUE/7qnlY1TEV6Br9du5QSde3NVL9Y83DrJ6PWbsl8GNuuQCp2HI+6wgOUo0NG12w8zgWSe0awZtgtUWfs/+ODA97qqiyjE2RImoLHA4NqZNI68ZqKN8yGYhRliNTBvAwh4PYNqy4HTWjrk2JwBcBSV9R3Nw3m3HQXyg62KlZCFiRfFTifKV++CWrRvxMOjLgC66DhwN21jCN88dy8B2TeGyk+ZyxBs= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(396003)(346002)(39860400002)(136003)(376002)(451199015)(36840700001)(40470700004)(46966006)(36860700001)(70586007)(36756003)(16526019)(40480700001)(8676002)(426003)(4326008)(336012)(47076005)(70206006)(1076003)(7406005)(54906003)(6916009)(316002)(2616005)(86362001)(7416002)(5660300002)(2906002)(44832011)(82740400003)(41300700001)(356005)(40460700003)(8936002)(81166007)(83380400001)(6666004)(478600001)(186003)(26005)(82310400005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:02:29.6037 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0f434472-a91e-4234-cf5c-08dade0e2163 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: CY4PEPF0000C970.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB6133 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 9843D4001A X-Stat-Signature: p5bbfiqpukxuyqofjngcobqjumji7zop X-Rspam-User: X-HE-Tag: 1671048152-307088 X-HE-Meta: U2FsdGVkX18ySzH7yYiJcGHAUJzoREVhF1cSHj0fy9reKE+J0cL+0EIA/fKuh3LLIB8WTXpnMuT7P701Ar9459Iqj8RHXZd4svkGyXPl4smGW2jOYSERoJ0azNYlUZDHB2ywc44RJpm9nj62SXq5soeJGXXh3Y5V4M7/sOf/Sy1Hug3IxWD7pbyyqUVST8T551rcE9Twky9Dolk8ic1zYAwJtICWT0+BZz2CJz9TYP5MzQsHpqc9TF0Ng03PHSp1TuyFzh0qg70U33lUAcJpLjje0dymNzHimxgMBavYChMqHliB9cv+7Y1sdYeOTINe2Yf2YKtoq2Ag4PR54WWfTW79hThIahFXnBFmrgFPrKjk79NRxYvYgRWVNiI04NQ9XudZPWmsc6YKw+FxPze1G3qLQdGkV6U14LoO4Ub6FlYnMfoFUOdxrUFK0mM3xW64RUE9+Kk8+yJ73G61+4FNSAVKpfPLK5EsNJcPSjXUDvB24DUJkfjkAKsy9Hzhc+X9HUoNUTJz+LgRNz8/YCiVhvPvFAz0wILfcSYQZCcJ8uJP/pxfyG6E+UXc7ZI5p5p/6eqVHxJiDQY9CQKxj1GJzitCE/KrGk3TXm/Tee6Lthb8ex8btMlgo+N4qeS5v2O9NUUkh64xB+tEKiyANhmjasb8sgrtY4Cx7Vi8cJbSz8Rw2mv+D+h8XDFBLb+rKKpCvpYNEB57ff3TsDDJcKxiLSuYuSSap1Np6WWZqA09kHNDDax0gVgCDbC6obuWuFwBWsyn4fq/2xGelud/IqecFncDy8BIOrrsuPaDVve0WOXXMWl4KKRxIDPVCVSvnJpxMdOn/SwKFg7QoiPdGAnucJMedP2SZu/G832pfX26KDHfS8aWW1xGATWF/orROzofZ67NR2p3XRvLvVST5sOBwBe5FHxeb0Pa7tOdoycgeJgdHE2DFmEmAL0raKw2PzawiS10QKryao0R5gOKyZr Rab1y11z yMcduZWAOZOLInCVAdL11Gx5j8snXEvnY8KYO6KPbpiNHr7juskIUjONTeQ== 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 f4bb0821757e..15b9c678b281 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1838,6 +1838,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 6bdaacb6faa0..c94b620bf94b 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 25db83021500..02c7fb83a669 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -6533,6 +6533,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 Wed Dec 14 19:40:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073465 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 A83CCC4332F for ; Wed, 14 Dec 2022 20:03:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 489A08E000D; Wed, 14 Dec 2022 15:03:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 413B68E0002; Wed, 14 Dec 2022 15:03:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 23E748E000D; Wed, 14 Dec 2022 15:03:18 -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 0DD1A8E0002 for ; Wed, 14 Dec 2022 15:03:18 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id CBB31C032D for ; Wed, 14 Dec 2022 20:03:17 +0000 (UTC) X-FDA: 80241986034.15.E6C7CDD Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2047.outbound.protection.outlook.com [40.107.244.47]) by imf02.hostedemail.com (Postfix) with ESMTP id F2E9980008 for ; Wed, 14 Dec 2022 20:03:14 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=JBKlu0ik; spf=pass (imf02.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=1671048195; 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=kOe60nzGdlMzQXs5TK7zEKZesPLwJGtRx9qJrFWezak=; b=pUrriKLozeta2lPPYdk0f8uqFyG3dvNlrkVyo6qHVC2+hE///a6SVT1GWT8M0++zJpCYIe eM7eDVXaL1q1O13LGlijvYH3L7b5zrAPohqt6zZ0AdWyXX4m+Tl2165H4/7DgQBdOJpdHb 8CWwb+CCrqXvkvPGkjFUcFxE91PIYAw= ARC-Authentication-Results: i=2; imf02.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=JBKlu0ik; spf=pass (imf02.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=1671048195; a=rsa-sha256; cv=pass; b=iD7zLLiyhUK4TVQnl6sV5q9vTan9GECJDmwfQErwvINm2QB+l3q3oCHiF42L3sPyA/y0sF 0o1+iYuMeZJmWsmAtqTB9ljm001uWDNhCkw1C7iy/NnsoLrPW4QtejD43+s4uW8dCUaLaC Uw2r3Cc5ajqqN2uWFD6bJbTb/0SqwHU= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=E5dqqnCwMA9gVsTAkOCqErXGgtfzAf9WMt9nd212itrMBFBW8CoOikLu0dmP/3EBvsnT/073ZE/57dcgd2dEO9FNqIKdwFh//3yvEm04q0j/A+fCYXOhRcFGeewTwQlkQKL3H0spooIbnw43lt7cxpIQF/h+QeY867rA6qjAdTwJcDgmG2CbqHUyhfB8DDaMZuiJGLBsRwCWACD+HA6GW/G/bscNZn38SktVt5IrXTnWYNjRrDZtfs5/0dKY3KyttJVmAdErPgOGmCPkGXrfhRu1iJITEp6aXFmiRY/0h9d+l6fUMaw2NWMYgyiSYnjqMSJmEjv0hxCiWsOZHmk7MA== 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=kOe60nzGdlMzQXs5TK7zEKZesPLwJGtRx9qJrFWezak=; b=R9gT8HwAeylbAUtgA1UqHhgBz1Fh+bGrO28+kYk0BeIELYZDru5dcoZygGPUJt5eXyaTcI5eT3UF3DctR5RRS5y3avnVaTtWJKPYvE17S+07T0l/fZMtVUjClBCeOUCK3Fpe5/MRUDxP0hblkxLt6+tUyc+RCCGOxh5RHyPneLbQ4YAaLy+Z89S9wBf1nWB+H98Z7MbaYzELmutn7VW970Rc5czoxks7vS0MDKVH7GaEcxhtHfcD38w87kUHiJ01qQ8QSvCti4aWhKhTpHbaT+xFIylDpWxVvMLQjBNuWaiziGWl+VfxnpRzQwHkCkmwPdFXLAghkD8cD7EMqr1cCA== 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=kOe60nzGdlMzQXs5TK7zEKZesPLwJGtRx9qJrFWezak=; b=JBKlu0ikIUSm19/oD1bzOWKmfViXnrbem6CDW8lNK9XEA6IfWF8lwDH3WOSIyrH18Ny2W5K4R79IWHszYFAWqHYbnsUVN/N9Y2iLYXg5xYLLOqIoQQVk0RHVx+M4L7z3B6w9rIS66xBT6QvdaIIZ6BObGO2+qvv7ikn61zlPTco= Received: from CY5PR22CA0076.namprd22.prod.outlook.com (2603:10b6:930:80::23) by PH7PR12MB6539.namprd12.prod.outlook.com (2603:10b6:510:1f0::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 20:03:11 +0000 Received: from CY4PEPF0000C96E.namprd02.prod.outlook.com (2603:10b6:930:80:cafe::d3) by CY5PR22CA0076.outlook.office365.com (2603:10b6:930:80::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:03: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 CY4PEPF0000C96E.mail.protection.outlook.com (10.167.242.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.10 via Frontend Transport; Wed, 14 Dec 2022 20:03: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; Wed, 14 Dec 2022 14:03:10 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 51/64] KVM: SVM: Add support to handle the RMP nested page fault Date: Wed, 14 Dec 2022 13:40:43 -0600 Message-ID: <20221214194056.161492-52-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C96E:EE_|PH7PR12MB6539:EE_ X-MS-Office365-Filtering-Correlation-Id: 208bccc9-227f-4a5e-3059-08dade0e3a52 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: oFD4Fru9kyvxTEUwaEd0qmp6c5aBjkV8GNHkCFsOoDWFbJDciYM6Sn+G7yyEo017kCiiqAfx011VuV0V9m5t2VBSUHJi0Lk+vhusEWmbGvFDoyXkT1FWQliIWi+yp7OvMYMvgAhpRNWw+xstHc9BX3QTdys+ecD3Gt9zjUBiTEOQIjr/3lGDLgbHVnIDxkz06xppAMoRXpb8oUAyDDFdNql6aoQCgilBSRPCOp7f0MRATKjXG9xLcuTTk1r+Ol+LJYlex+GSZu7gEA/OuGjcEjMDXFZt6vj8+MBnyOF/6D2URVZPQ5ICWvvRS4gFcsa4tr4xnr8QWKbtA3CPUxLjZdcpa3czUtaKOm9/7zJiTehivjb6ALhrlMBhd9/xNjewFuT73yGal5WuUJiz/H6MaGG/G3iaJEGVO6U2boHGP/LqFEDEBQmY35ImzgZzEIz2tu+B1LV9UcErvcMscfX1RxG0CTH9d2cCjuPrnqmKn3Za65VN6/HGkTTxuYsvBHcIiN6yGSsHIry1OBGpbrWjECt8gnlyOH/bbz9r+zaFmtk/Sxpr1WogA186P7LRx17spx30XmmFqOmpVD2iMXpD1LUCi2qsjK7ab5r1jMYJAAIsg7zaB6ixZwNGwqouEQ9LyRRCQHSRsw1nGa4q46HCb8jm8rgqqwzCwNyNYAJcub52ss/DmL4Pp1/Cf3UiA5k7qbmB79c0jZ6GNhGvSpszVH8dpZU/XVopfavfp19xy9E= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(136003)(39860400002)(346002)(376002)(396003)(451199015)(40470700004)(46966006)(36840700001)(316002)(54906003)(6916009)(26005)(40480700001)(36860700001)(2906002)(426003)(6666004)(186003)(478600001)(36756003)(356005)(47076005)(40460700003)(70206006)(8936002)(2616005)(8676002)(44832011)(82740400003)(81166007)(5660300002)(83380400001)(16526019)(7416002)(7406005)(336012)(86362001)(4326008)(70586007)(41300700001)(1076003)(82310400005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:03:11.4313 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 208bccc9-227f-4a5e-3059-08dade0e3a52 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: CY4PEPF0000C96E.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB6539 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: F2E9980008 X-Stat-Signature: n95he1azjp8cneuqdhrxbdjh47girxfq X-HE-Tag: 1671048194-679528 X-HE-Meta: U2FsdGVkX1+9Y+sjlD2fjl5s1RagXnzQYUGUsGIq2GarBmYUQ417RcwN17Eq+8gb5xKVsb5g0YJqY5OJSzJMi9usGmtOX2A1pV81pHbAg7joSES6fIZ8sSypToGKz2ma56hyeBW14AsjamqnpDNyOyxfKL9LGIZR+QfJLHLglpdGLz2Gd6PyhNIEow8M5eXxuADEBwNUP/7l9EgPWLC0QxNFx3IERx9r9rhNYlTyi6vpdE/ceM+HvLNJtN4nIiJpNOoKTRJZ9LML3hF+3MFVmHZTxt6bRzmAx6t4lGE3QoCXkrFxsFo4fQPmFnNYC/GgU2eQ6AsnJyPeXv8QHQM40ZHUbFScF/mqj5tI6U2tPd+2Qtc9ImOfSkDgPCDNeWMpQWl8ro1vobCqMrqbVOFwC4/LfLuJnLZK27UJJZF0n5KJpGbQjawqdGtK/aMe2zdsiO7dOWOZQxHo7X8XHFHflEgRNuot5KHJbCeKOGxLiHKlflmItzprv29NwAmVGfTxEybeUkEek1otdpvnMqi7x0lX66EyQrKojfOv6YkLGkEzGegiUpz01j8V5WAmHFX8sgzMOwmbmYDEkRMQuBbMlIqyoUau2HmbCsmVigK+P/rz5SOHz6Fg4oCQ+dtTMr7+cxw2WDXc4K0ggR4ceBQoD+NMNDuH6UnJeliUN7MROtsQntbau1AJo0PpcFD2gpwB8qyXeYlVm6joRYz4hrU6lGWrbkvsoJD0W6mr5CLCMk5Vp8edi6mGwQHiQCdEdjpm46ikKcbVJBTLl0ticaVzRxgyGv0GmKOiM8T2puGyJhmzz6NoUq+pajC/K+PDr7O2PlEQPjXxYgqHlIuEZyrho/pAIJTIgkbb57Soke78b5CzrLymbDLgZbooysH82txpfqtn/eOVzB7VTXdRlEILS71rXJNauEzMTwI6wLOfn+8J6nDot4wlW3HA7a49EXDaActVvTU3cQ69Jq7pQqQ hbxMyuhA Tik6r 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 | 78 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 21 +++++++++--- arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 96 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 90b509fe1826..5f2b2092cdae 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3361,6 +3361,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. @@ -3911,6 +3918,77 @@ void sev_post_unmap_gfn(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn) read_unlock(&(kvm)->mmu_lock); } +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("Unexpected RMP fault for GPA 0x%llx, error_code 0x%llx", + gpa, error_code); + + 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("Unexpected RMP fault, size-mismatch for non-private GPA 0x%llx", gpa); + return; + } + + slot = gfn_to_memslot(kvm, gfn); + if (!kvm_slot_can_be_private(slot)) { + pr_warn("Unexpected RMP fault, non-private slot for GPA 0x%llx", gpa); + return; + } + + ret = kvm_restricted_mem_get_pfn(slot, gfn, &pfn, &order); + if (ret) { + pr_warn("Unexpected RMP fault, no private backing page for GPA 0x%llx", gpa); + return; + } + + assigned = snp_lookup_rmpentry(pfn, &rmp_level); + if (assigned != 1) { + pr_warn("Unexpected RMP fault, no assigned RMP entry for GPA 0x%llx", 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)); +} + int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault) { gfn_t gfn = gpa_to_gfn(gpa); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 1826946a2f43..43f04fc95a0a 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1968,15 +1968,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 aef13c120f2d..12b9f4d539fb 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -730,6 +730,7 @@ struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu); void sev_rmp_page_level_adjust(struct kvm *kvm, gfn_t gfn, int *level); int sev_post_map_gfn(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn); void sev_post_unmap_gfn(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn); +void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code); int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault); From patchwork Wed Dec 14 19:40:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073486 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 E04CBC4332F for ; Wed, 14 Dec 2022 20:03:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7F0048E000E; Wed, 14 Dec 2022 15:03:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 76E148E0002; Wed, 14 Dec 2022 15:03:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5C1FB8E000E; Wed, 14 Dec 2022 15:03:39 -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 478948E0002 for ; Wed, 14 Dec 2022 15:03:39 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id F1D15160F47 for ; Wed, 14 Dec 2022 20:03:38 +0000 (UTC) X-FDA: 80241986916.29.402D403 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2072.outbound.protection.outlook.com [40.107.220.72]) by imf24.hostedemail.com (Postfix) with ESMTP id E141C180007 for ; Wed, 14 Dec 2022 20:03:35 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=bcF89XXa; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf24.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.72 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=1671048216; 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=kcHgxzjqYwjC063icms3jxvHup+Y3pTqF5BVDiK80f4=; b=AhrBYC18H2LsM4XC1zeQ5Ro/C5Y//FMumezeorV6bGh7H26kBcbVTAOjVnDnrefJuidjnC LOzHd9/8sW3uBo7Jarm3icmENmyOVsBVIzgklpx6KKca4ghxKFx9GordpeUeUk6yaoIIwg nFiy/OiZYyl3Ax+4fWOZbleFEnWgEPQ= ARC-Authentication-Results: i=2; imf24.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=bcF89XXa; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf24.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.220.72 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=1671048216; a=rsa-sha256; cv=pass; b=7W1SrkB/OzeGIVZmx4Jy3yNMzqkD7kl660pLuW0OQc9EZwUjR30sHD6/H5BTsis1ahyDyE jBUlEQRpwLQLJ/nMhBfGDL6pdyRM7POiif5DHuDpr5cLRDHYV8kE9mVL/031tSwnLCGhka V/9/m7J1EwSitHplixlguPgiVHngzuE= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=M98Y7AplrkZsHnvB91hWD4a39RnwvUI5bkuYq/Jb9t3fATclMxTI5zB/Ura3/0EACMjQm7UEFsdm3dfthEWOso2/WNxrAKYPp5IdubW9Tu8AWlly1ZmiSmLsE12VPOmcOVzcQ6BwU8T/rHXj2WeowYA9rIocmacZy0P0WLGwDspAIdoxJ5fmRQ9Q5MuFcmpRMz7uSPidZg5VeA4p0yiw+TBf4/rkxmsoeLT2K2k35EiGZOewXRiqc853OSZguUytZo89iLF9WjlMphfw8C2RFFIL6PSX65CKbRrgA7U+Zo6ro6hF1nq5K5uFweezG2syecySw5BZekEgU7qJwwcxdA== 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=kcHgxzjqYwjC063icms3jxvHup+Y3pTqF5BVDiK80f4=; b=ONy9dZnF3z2gbJWrnDAO63LTbV0JOhyZxNTz8YEIHGLiaQb3kTwl43cTfub6SUueRomxk2NFz2ISZg48S+QV7uYrN7CB/xcmqM0RDmL1ZoCFv7rvgJU6iZzBGT7BH7ZDqx2ip6lkQQ/lVtydB+sA3PNG6GHVfsuPFS/hAMu1rVjCXJW6RD0kdviz0yy4JK6hzj2z5t/YIAaP/OisMmelBxoLO5QvPMc4dKjYrioccw2mO1oyeAARbe/00iHZKXFI+aeolakUhcwoFE8Gj45UPPiLTeLX9ATqptUYT76mKwvAVexCLWM9/PerhD2kryqdbvGXafM1VN6c21azbv7eBw== 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=kcHgxzjqYwjC063icms3jxvHup+Y3pTqF5BVDiK80f4=; b=bcF89XXaLqwBkeCKZKHeTLgbhMdQU8MQUHUsbXibTGLnS2GM/BdxIL9NgDutibh+2DgND9Q97rQCUUiY2+i5aAy/PJ1/eoayZJJsYvIEG2DcXRRNwvIeh2K/Jx0ZZCwDSOh+hTUhOuiuDzDF2nAJWX2bcNzOHB0XAXNk3v0gTaY= Received: from CY5PR22CA0074.namprd22.prod.outlook.com (2603:10b6:930:80::22) by PH7PR12MB6612.namprd12.prod.outlook.com (2603:10b6:510:210::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 20:03:32 +0000 Received: from CY4PEPF0000C96E.namprd02.prod.outlook.com (2603:10b6:930:80:cafe::a5) by CY5PR22CA0074.outlook.office365.com (2603:10b6:930:80::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:03: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 CY4PEPF0000C96E.mail.protection.outlook.com (10.167.242.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.10 via Frontend Transport; Wed, 14 Dec 2022 20:03: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; Wed, 14 Dec 2022 14:03:31 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 52/64] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event Date: Wed, 14 Dec 2022 13:40:44 -0600 Message-ID: <20221214194056.161492-53-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C96E:EE_|PH7PR12MB6612:EE_ X-MS-Office365-Filtering-Correlation-Id: 5f3ba924-27fd-4211-d22c-08dade0e46d1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: tllGmG84N4CHJw+DCzJko1kxNWqO20XTAgqJukrcMJPbs4xUsQvrD/1kgZUuvdaSgcoSQp8lIwSlU1iWL6cyNaS4QxvK64eSNRJXnAhgTUTJyVKoEIjN99wsFNathQhGE0IMBcwKsGeiYTCE8z5JDNyFC3EnDIu0N1ynXdh2hqu20PTN/0BpCDzLpqTrZXTX5xb6uVpydKgPIK9aUy1t194HOFnG2x44Jadx9y5mzmISAhkHJ92GLA10QanDlaQMJsc9fs066joR4RMDN8eWMHVIb6+Qsq+LrVPJKxDsLWZzdGL7vPt7BdU34HLp2TRE1tOMGuGGB7kGDf8drQYEQEVIEn5svugbHnCBOO1wjz9YZ3nIA0ZWv8nffdV7mwDTCvBg3GIRIRWvh5tnfUJd+pzPrmWBDXYDOwMS6L4H+lnHOEK0vz9LkPNfalucBIQrGpUvRQO9j9bbQ7s4G0UUvJiIvo5VSbp9ZETqm4I569+8+G1vAuL5EkyEVM/BtcKjyaP3tdkwZvxz/XyoLmfFIDR4LLPjpB9Tz5+agyC6z06/hrfXBClr/AURezz1V4cNXbjuoD/7PZ9XLjOQk/uDY5oRr2jXr2QcEJ1zYzoTnl+sz+URycisO018J1/yp3xY/EulBkBgMFOh3qizLlV33yMr7QI73cPBIbTW8SF1czJ/sbIuLESlWYst5I78kN3vknsREzY9+IZYnNuYus53tY+TknQfYWHqYlWj+pDVU2g= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(346002)(376002)(39860400002)(136003)(396003)(451199015)(36840700001)(40470700004)(46966006)(316002)(2616005)(426003)(6916009)(70586007)(4326008)(47076005)(26005)(36756003)(8936002)(54906003)(70206006)(478600001)(356005)(6666004)(36860700001)(336012)(1076003)(83380400001)(82740400003)(81166007)(186003)(8676002)(82310400005)(40480700001)(16526019)(40460700003)(86362001)(41300700001)(44832011)(2906002)(7416002)(5660300002)(7406005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:03:32.4004 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 5f3ba924-27fd-4211-d22c-08dade0e46d1 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: CY4PEPF0000C96E.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB6612 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: E141C180007 X-Stat-Signature: o97zjnmugp1e8exgrcs9kyod91u6ncar X-HE-Tag: 1671048215-498585 X-HE-Meta: U2FsdGVkX1+g7yKCAVzFk6awo4ugusbZiGFuMNBpNCFVB4IK1/R9/qxj8TuiM0i/HXu/xsKRToRTbHAS9gIeLhhj1Ff36di/HgIqYy0bNzuJtZKmCF2jB9nTepiFLY4ju5sm3UG/rt1uLknyvbanhPigY+xF0h8gc0yezLCHTMyKx8a/ObgKe7rFozU2BoQfgRycMBBmUqwWC6xcI1Q/EBia+WRADglptdsuL6Jgxp8KUf7CgXpK9BCTndrKNeJqrJn2A/XZ/gg0cq+3oWwRz1op3J0MiAkTafdiZTGxTVpFmK7dXt6OdQddBFf5NKBuRhHpki4S867UXhzfvHCstOFPwgFrMdrbZwvx1oY+Dwyvd9XGir88+pV0AQ/cqT/8uiEkTsSfvVbperVJU8eFuR970rXUIIqrtANccS3epWwpuTSvL308Y1kTVvz+/6QAGfA7LNjVewyj5AZQFlSd2BpKhHp+5sVAVnZJ4q0xQf9ceXEROGbJJqTgNALClW0kxAaRRLsWszdw9pmoFX+WaihMCQAvWBgzJfh6hPDt7FkPHbPmvkB3WOKMJxU0zfwZRxuykYQLvRMMgw3x0pZlVzN7NmGvQoRKBd0523aYeO1unzXwHpkyW8CDowJ7LH55CJNZrAof7FwQGERdOwguwmm+kXmGhBAXZUJb85A4ZXc9sccfMsgZjCjkuKk+l7oVBBD3uEpwEtFP+S7Ah8LAcj2NM0A/5RccYkEbmNT08b7KTY/qYViOFDgbhGEC1cdoKE6KVTd+71tUOGuLTzJiKcEm3mgSpaq+QCRXTs1ExUfemyWcuGFM/h5rDvYACPJZxxdYgvC5r0GXPYI89HxqqMDBoSylSFhRL1Je0XzTRp39F/rHvNUhruNzcSev2m77PL+fVqYq694LNe2C/hYQ6m3yvuANDAfbZzc9nptxOV1QHzF2pEh+bsROjrThZILjeBGF9JEWx0uZ5AmtE/+ jYuAmdHS y/0qHpl7xNMRp7yn0cW9bt8D+B+JgAA/3ubmywjMjIfDhM75kvlOQekysZw== 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 5f2b2092cdae..18efa70553c2 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -331,6 +331,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); @@ -2051,23 +2052,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) @@ -2653,6 +2665,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; } @@ -3174,6 +3188,8 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm, u64 *exit_code) 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; @@ -3396,6 +3412,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: + svm_set_ghcb_sw_exit_info_2(vcpu, 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: + svm_set_ghcb_sw_exit_info_2(vcpu, rc); +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3629,6 +3788,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 12b9f4d539fb..7c0f9d00950f 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -101,6 +101,8 @@ struct kvm_sev_info { u64 snp_init_flags; void *snp_context; /* SNP guest context page */ spinlock_t psc_lock; + void *snp_certs_data; + struct mutex guest_req_lock; }; struct kvm_svm { From patchwork Wed Dec 14 19:40:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073487 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 12417C4167B for ; Wed, 14 Dec 2022 20:04:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A7CFB8E000F; Wed, 14 Dec 2022 15:03:59 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A066F8E0002; Wed, 14 Dec 2022 15:03:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 87F658E000F; Wed, 14 Dec 2022 15:03:59 -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 7124C8E0002 for ; Wed, 14 Dec 2022 15:03:59 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 4D39E120F65 for ; Wed, 14 Dec 2022 20:03:59 +0000 (UTC) X-FDA: 80241987798.30.D531058 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2061.outbound.protection.outlook.com [40.107.237.61]) by imf29.hostedemail.com (Postfix) with ESMTP id 619D0120010 for ; Wed, 14 Dec 2022 20:03:56 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=MXr0u5Fy; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf29.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.61 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=1671048236; 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=SO5f6YuudHhXpf/qz3ENT7uZjSyAEhvUiU7bzgVCfHg=; b=PUr1Vtr7BBZvEFmlAN7r+uhwSgZc5ODKpT+JhEMhys7BaMzAbMaDVvXDsn/IL/RcHE8Z0G udSofQpRu2rU6pWCFX0zmgRESxv3oA8i/CoNWfuYVYtsowotaYeBtjaQVphd4snKCZLrh1 ZqJ8lvBcjbi8KCG/T+pquYG0S/2CnQQ= ARC-Authentication-Results: i=2; imf29.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=MXr0u5Fy; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf29.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.61 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=1671048236; a=rsa-sha256; cv=pass; b=RzD0+uy1DY6tr2DnMkQqR732U7kdded5p8DkTdaAVT+QjOYO7hz0frqqGkZNvFg8Y39sni gPZSYYTWnSVWxlAAhCY776Fm3AfTGxoVGUr7iRtut4H5Vv4jm7aifoSFsl2XfUoq/N32nP 5KmtQOwllBORkNWPbrTq4s4uy+zUBX4= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=oVpk/LqB3g85UGBxU/mRebJTj6ICc35mS+OiGYM0ffm/GztYZac9ykKMeKg41rQbJFQ9RF9J6vrGAOXuRCEezjabtyihSGBCU+3+9gfI8g4XyACC1136nSn01hL3RkbfGKHgo5MPMTqiZHEWIytiCtwDmRR+tp+tQ23EBKVDPIl7X+Xe7koAqbw/qwSCTkeGrT0B09ok93oNb9P5BvwHL7vaKzTnd/3k+qzBd+6l6Htw6QpiLgVbhr8SCaTBCguvBxR7MGzONiNdyaUZt5ymSVrrveLdCaN0XEq4q1nFRmPBwdMrnawGz/drPVn4qPBeQwSi2o9OQnhGtDKNZXFnuw== 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=SO5f6YuudHhXpf/qz3ENT7uZjSyAEhvUiU7bzgVCfHg=; b=SINVei6ujc7W9/n97LiBZujDblPUku3ctbqgr3u90CzFLBZPGTx0jZepidKnv3mNRUgP7Y01xaNSxcf/kI8yKmV29vjyzgu1kbsXFkJN9fT3mTvnXNDFkozYA8bkXSXWx4AQqaCjglsqFbtO/8a51ALzk+kdjojjGpMJNwe8OIaG2dQDQd3FYDE3we0BQ/8se3LI8WetijeRxpGUzIt8M2R/lDZVdE0ND/mslI5aSzI+IZV/0bOQSI321e09IGlmTo7tvvHhOoDYM5bURZ4W43jgT0Kyx4ncVPT/+USqWO5GT8Ymt3e+xJ3APtYsDBFkj9KcjT9HID+f9Xbeg2sWbw== 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=SO5f6YuudHhXpf/qz3ENT7uZjSyAEhvUiU7bzgVCfHg=; b=MXr0u5Fy+g+RC9iDkjoFbGzkAu0L00R2qIj90lUMXUvAd3wc353WIfBNyOqZELyh5OjgsXe4GiBtt2MKT8mHVKXNnp6Y4Z9NY8XuQ+VBQY2hfp007LZO6n05+P21WFi68/3OyVKfybYFl1ZAwq7UI3Oo3kdi/G5zJNKbnJtgjjw= Received: from MW4PR04CA0114.namprd04.prod.outlook.com (2603:10b6:303:83::29) by MN0PR12MB6029.namprd12.prod.outlook.com (2603:10b6:208:3cf::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 20:03:54 +0000 Received: from CO1NAM11FT081.eop-nam11.prod.protection.outlook.com (2603:10b6:303:83:cafe::95) by MW4PR04CA0114.outlook.office365.com (2603:10b6:303:83::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:03: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 CO1NAM11FT081.mail.protection.outlook.com (10.13.174.80) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:03: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; Wed, 14 Dec 2022 14:03:52 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v7 53/64] KVM: SVM: Use a VMSA physical address variable for populating VMCB Date: Wed, 14 Dec 2022 13:40:45 -0600 Message-ID: <20221214194056.161492-54-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT081:EE_|MN0PR12MB6029:EE_ X-MS-Office365-Filtering-Correlation-Id: d73c9c9a-2fbf-49bc-e434-08dade0e537a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: fck6FJC740/KMecRB/nOVbRYYvX7TJ5BSHujgj9KhlPSC1qcoLQVjgfj7h5o/svo4W3RmL34h/ortWC15/9DPrEwuPnnynryqLQyhUzIziU8Z0cQQbPfvcN4CSWMBV6HSsQ5XBGjIK776OmhIOU0/H/ma2/L3jbncEhEbn3L74WIUQY1jKvWmphZgygtrsnheumnLz4KBPR3A4Xj7OhJBzdag8KmcfbegUT//zufaekN91MKZ46aPqAY55pQgXxbXvK72aE+ohm9C2VfrIUjiZ+352kh0IDm/x0ascT27MHcwGIIwCcj+pnuyfnpEmxc40+6BcNu8fAME0k/PUjkL1h0uTCKzAcdjMvH/JXyPY4cKPCvq7/WWnKZlvHBCLSeKVjuOwz5TZHjYgSEnxxRNKZt0mNR8QdL0BVc6uXih7CcjlGqSKk/yinvK7NCFOupwVeKbM0fSGrhVx1sFduTcSRe4A0lbcaNUJt1I6hJqQW6ySr/w1Uy2Ox2lCVUU2AIYAU7/6vWk8OcrpOPHR7qdg+JgP9Jim4JlqW85nMqbu+p3GaFuMKxuSeMQEAsf//YGI+vOa8z9Wow5hlHj7tQD6ZItWJv/tQCIQ+t/i0k8qI1dZ1unYoE0P8tWfwCCjdU40i9qFSlUI+ifkUkmwqRFHUAS9CXrxqBFAgv/jXBMPqPsHZ+ISYljm207ucCoeBcZcwFZ0Y3XeNvaWbRmc7n7Bk/Im7HXghApD4H+4SzqMM= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(136003)(346002)(376002)(396003)(39860400002)(451199015)(36840700001)(40470700004)(46966006)(41300700001)(36860700001)(36756003)(5660300002)(8936002)(82310400005)(86362001)(7416002)(7406005)(478600001)(54906003)(70206006)(70586007)(4326008)(8676002)(316002)(426003)(47076005)(6666004)(6916009)(26005)(356005)(81166007)(83380400001)(40480700001)(336012)(44832011)(82740400003)(1076003)(2906002)(16526019)(2616005)(186003)(40460700003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:03:53.6255 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d73c9c9a-2fbf-49bc-e434-08dade0e537a 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: CO1NAM11FT081.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6029 X-Rspamd-Queue-Id: 619D0120010 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: 8pkr91iq7ik4ueituzxx53ey5c7we5g1 X-HE-Tag: 1671048236-192044 X-HE-Meta: U2FsdGVkX1/jfyBF0CWqtag8hOrFPEOsWG54cFq1tgc5g7Bg7cDoq4bDfwXzbxtmBqyYi3ADAHZuDBimp2ZtR68akOrhjr/ipha5U3feq92F5+i/iWMy9ircp+DUM4oVu5qIIIa7PpkAXjGOhXex8gyvuPifZQhwddqwvoZgfErBeC1MucI4JfH8V9AzR9JzhxUWN1aLJlpHKODNNSLLGHTf5DeCuZa2M7jfg9PTXZ0wPyMoAHiGsNKMGdgpGXnt52GO7+FSd48AKvdsqSrWEUtX3Ix9U4RG3DQdPMFTQaezJIW9T79rt9ah9fQnDY27fEwYqrXn8GCgaA7xWVlMUuL7975ypT2tSqyYB6luJMaf5pBDBTMwB/V7RGPmlcOw6fd93FmS2p6FemLriGVeKBUfxCGf6SKGjupbq9MXclvAEL0FVKmuK43Gg+dCBn286t6zwws0ZjOsGimwchj5RGOS/MdqzawzKsNweBEMHOmOefkaGOGzzwKTknobp4s6mXTG8DfuNlhI214Px0c99mAS+302C1a6Lg8uyxZHeOcQ0R4xS6ecs6AB1opa3b7/mgd2MLfOV46D/ihl8xbW2cjK7jFXCkJod/5H9/X1uJsk45WBLuNpzE41RbCyJR0Q0rA8wJyqzf9Ig3r+dw7SHLpT/L4+99uk08FRyV2+tQZDt7rUlzMZIrMVTCpFk8Nsl7Mafm4CG32PPJzAjbmcE4O6MV8zW18BfTHbeYLgtJE82Ikae2Vc1Magrjjip68I07vFiQ/Kcpp5/QYdk3aD+0iHlBwIvJID64IlE63LGJIzInWPL0VOxpeeLfGQRktq6efSwoA/vL5EEnf5zwqK4Q1Wq7oHAM5JS8lpK3BssAU/vCFZb8awWkKR4eYMAqWq5vZ665y90U3jdbqKSV3tpGc2+htlo7iQjpWeBdZsIE4UlbY8T+XSE+bpPqzvFRP7krkc8FPb067eYif3nxy PaHUsL32 wGX1o 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 18efa70553c2..36c312143d12 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3845,10 +3845,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 43f04fc95a0a..e9317d27a01d 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1398,9 +1398,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 7c0f9d00950f..284902e22dce 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -204,6 +204,7 @@ struct svm_nested_state { struct vcpu_sev_es_state { /* SEV-ES support */ struct sev_es_save_area *vmsa; + hpa_t vmsa_pa; bool ghcb_in_use; bool received_first_sipi; unsigned int ap_reset_hold_type; From patchwork Wed Dec 14 19:40:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073488 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 C846FC4332F for ; Wed, 14 Dec 2022 20:04:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 636938E0010; Wed, 14 Dec 2022 15:04:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5C0288E0002; Wed, 14 Dec 2022 15:04:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3EBBC8E0010; Wed, 14 Dec 2022 15:04:20 -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 2822F8E0002 for ; Wed, 14 Dec 2022 15:04:20 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 0462F1A0FA9 for ; Wed, 14 Dec 2022 20:04:19 +0000 (UTC) X-FDA: 80241988680.25.69345D9 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2046.outbound.protection.outlook.com [40.107.223.46]) by imf21.hostedemail.com (Postfix) with ESMTP id 3B5051C0003 for ; Wed, 14 Dec 2022 20:04:17 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=ODC8ZGfH; spf=pass (imf21.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.46 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=1671048257; 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=N3c3SYfOpfx8KmHSqA0Zsswq1NidwqBDgUXBOwOQeoU=; b=HZKh0b6r+6euM8HoiUz64/JEi+hhmIrl44iMbwgybOw9FITDpwwCOEb5MEMaB0ja9rEOLc v2UJfRXyenSJ7qJY9WyW2cyC9FiSU8LuQ9eJN7Yjq/v8Zv5Bd0IJyQ0FPyp/5k6IM7zJDD 3B2TVlajGJ15hp70TCcMyDUagChOKtU= ARC-Authentication-Results: i=2; imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=ODC8ZGfH; spf=pass (imf21.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.46 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=1671048257; a=rsa-sha256; cv=pass; b=8DYfNsJM6T029pk1JksLb99whob7tQiWDUHAFdTdE50d2mxa+prW4CAgPMPEAKU0IFEQGg slIpboV+PJqZnHYATa4C5Vmgvch2vva82uzo/oD7NYPhRWBG5RIWHt4eXahQI5pS9rWPXe E0x7GxXI0/3EgyCQZH2aJEvTlUe3odU= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mBSvASUWLU9PMiN0UNspfbxMBUelJzmVVYgMC7cWyBQilNkJlzXzYWUrLn1JnJpI1P4VUtocYAaH586JcUFabn9/I4cjXrWr4yR5JlpC0uNsOlSBAo66VdZmhmBKt18QV3ent0xh6WlDPAHBHus6ea0g5VtIsnMk2sdtqcYjZlMdbkQkNN0HngIP3OkRRjecUhJl6lvB9Tvr5bYzzlvGWe9P21gPuXiGioQa98RjktfYzTY3zvqqolZ0YWg0vVFOd5oo2ncXXCkQXE99dIuenWz9Y0eNAuLsZXpwdeJGXB3SiCyiBl7UTqKNH7jdeXgQ4OKje3ZvVmA3c7CFg1NdYg== 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=N3c3SYfOpfx8KmHSqA0Zsswq1NidwqBDgUXBOwOQeoU=; b=NHbZPwBmj/ONPFgJLZFB7ir73DZQPtoxsEWU+p7LEa/YosFTgxKoOkWlCqQQHo+HWiNJB8dHzjxZNlayU/JrPwL67OZgFrm7ZKEElDARtyCZljLJWKq3mzVOA/wESN2hN/3Z9VGpwFc6gFTqWXoCS4ksLbR0udFKjiwK+JxIUhfkXZTvLJW2c9COBgyt5yxNrSk+ZBzEW8Iv8KNT8c1Q3llClGB2b8kO4HYTpcqfN6TWN5PqVXoeXYYVmVDy+SKYzuzRfAT4t80wBoql5YrYtuRj2ph7qe4rSkneqcasmXKY5naFhigKFx0+4rwQh0eaJ0+7Ami4BMC9KPFf4HLL1w== 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=N3c3SYfOpfx8KmHSqA0Zsswq1NidwqBDgUXBOwOQeoU=; b=ODC8ZGfHicraJLBc4i7AsQH2yaMeDZsttI5VLtPMJy40UtBYF0UGm63w28QP+fqc7ssxWQlsGS8Imo+YrbHLxeBJ/MXsIlI+tcEsNwu+P7J9actUoSeQvPUJekQ9rS0Zovveca2ONK8gEC3SZF/1ab12uBsSAO02S3/P1EeTLXI= Received: from MW4PR04CA0248.namprd04.prod.outlook.com (2603:10b6:303:88::13) by DM6PR12MB4927.namprd12.prod.outlook.com (2603:10b6:5:20a::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 20:04:14 +0000 Received: from CO1NAM11FT013.eop-nam11.prod.protection.outlook.com (2603:10b6:303:88:cafe::67) by MW4PR04CA0248.outlook.office365.com (2603:10b6:303:88::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:04: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 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.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:04: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; Wed, 14 Dec 2022 14:04:13 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 54/64] KVM: SVM: Support SEV-SNP AP Creation NAE event Date: Wed, 14 Dec 2022 13:40:46 -0600 Message-ID: <20221214194056.161492-55-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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_|DM6PR12MB4927:EE_ X-MS-Office365-Filtering-Correlation-Id: 5df8857f-c983-4660-1f47-08dade0e5fbf X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 4MydYN3bKdE+TL0cyJYnOoHDX5rfNWZZDay/B3WXKh22Dvlq8oKVVbLv7rAr+Jlm6MxYPkFHqFeQY6jB0HuXhXjniEcEdCMeftgT3vKBhEcA9xcqx+gEzZcduMesKLozfaG/+ZJK6E1gPQdqD1X41tg2IonxXVIfeThaRUt4jm/s3iyx/tla0u1huAmqOP7BEZTxGN+4uxzyYKNAt+gi4n5iZ2CxZYuIJozLCsCcHAYBTLOPUsRVdd8tdyJbonH/ZNjULoUgseABx5knKE0AtWj2CLnXlrqfgzqnXMdFOL2614lTfLECAKCmNaD6/RswhdBHDoaB9Awg1XEqCcRMQ4PF4oWiPFV3MzMZjNV4Nev+zitX+2Cfg5Jnvb1siOkSap5bA/swarJ5IB/Q8aptMFvFMzH3l9oNKgu/3XnZkpHiDmFAsT+hZwT42+EUDNsx7owRs92btVb2hZ5UFGSKjtv2F+qntyP80Qfnc/6+8/nhtyDwjAfZ/ZSDSCVfmIEuAI70zGMCuhezO+cz6l2wIGsRTmr+W642mgZXDpR/dVAaY/rAhPCqfCLogyxE3WdaTzL8IMuroPBbSUUY7qmfEs8F+0zHYGd2WoP3YQTik81PkQ7Fb2OXiNEr9aTya/8toJIvJAS4qYUjWycxw1kjyvT8C262AWgWmLkeDHVpnXQluEBKsQD6C9iQzEtfyOya1VQ1f3OZuVfKR/suA72pSQi2+iR+4xi5TAklkU+QMKA= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(376002)(136003)(39860400002)(346002)(396003)(451199015)(36840700001)(46966006)(40470700004)(478600001)(26005)(6666004)(186003)(82310400005)(7416002)(426003)(316002)(66899015)(1076003)(336012)(40480700001)(5660300002)(7406005)(16526019)(2906002)(54906003)(83380400001)(70206006)(36756003)(70586007)(36860700001)(4326008)(47076005)(30864003)(8676002)(2616005)(356005)(81166007)(6916009)(86362001)(44832011)(8936002)(41300700001)(40460700003)(82740400003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:04:14.2053 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 5df8857f-c983-4660-1f47-08dade0e5fbf 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: DM6PR12MB4927 X-Stat-Signature: hhix1ksm61fqrk5k7zzxcxs5z73cb6eq X-Rspam-User: X-Rspamd-Queue-Id: 3B5051C0003 X-Rspamd-Server: rspam06 X-HE-Tag: 1671048257-108804 X-HE-Meta: U2FsdGVkX1//A5o8FTeOiMNRwTHgPQ+bcbLcJzC1uTHVe0IcajrXVi5dIz4hTGvrHIvhaDbcJboNXZrL1THGEpI+UF6w0hdzgSzfOFsWNHY5OixYy8ju63OlyafUMuYWoJiggw+qteGr9jw+9Cegk51hY6VYsjZwHmbWbAcFtgIXEZTfpVkebCZcsOiBfeu29auzeTpXVObNNfg0dtRPJpOdq8Ttv5oAkh09eGcbjxJnr/qQ7aYzRcRQJHrSLhKfwO+NhWyutZ5eIDRimveS1dnzaMc5JPJHIHT+Qo6Zu438j4lUVk70fuTt+eGIGBJPiHayUjRXFaaL8ZiDqhM3L1wuKdfozxN4oDlKSO+q62QiSO0qmyY6YI13o79Bsedy6vGxGEl+AdSMNTOazcb4rPI/8lMRwYVCKzOwBpEQWfRh16mkKQFCh3lqsueWADdV3/U7HQB2zBmM83qhZl9U9lOjWT3hJXJcfc7RDQamzS2E3FlZiTVPhyVY2b4gytN1eL7ngwdrsyJzVSqqOaveRjkLJNUvcFHfq09iL0fRKA0M/rIHq+/9JgyVJAn4gM8Ds4GYZEHQtvRngcnjQtS0P9Gq46aRxW7yftBZNimbcWWPplIP7VGmGof0CaYUOUehCE2oNc5lmtQt3E8b0T3NpQ3GWBPqeFtCX7B8kpELoADXdmB4Jgl6YV9ru9ExKiyjebM+HAmBP9T7o4XRj1dVZTxsdoNXCNk9KFTc5HbwH/JylXYUA9qvMKnq7YTcWXZyg20nNDjrpF+RZKoTnmgUB8BYRZgPib4Dc8RX5pi5hkYdsacKevi0zWQPSalTVMhJjTQo+8pZrv/GqwYBPUjP7pgNhlW18NfaeucozMJcvOoG5OjDOlUb8x6YtHGZICirsLGsQYa8BcvGI65VnGB1HMEjnAhNE9LqMZVqslpqWBjjQFtR3pikhTHdCYfL2xzUPJlKq+kKLFu7rCf/AXF yjXm28VE XJXxZ 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 15b9c678b281..5958cd93e5e6 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -115,6 +115,7 @@ #define KVM_REQ_HV_TLB_FLUSH \ KVM_ARCH_REQ_FLAGS(32, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) #define KVM_REQ_MEMORY_MCE KVM_ARCH_REQ(33) +#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 36c312143d12..2f4c9f2bcf76 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -771,6 +771,7 @@ static int sev_launch_update_data(struct kvm *kvm, 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 */ @@ -816,6 +817,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); @@ -3182,6 +3189,10 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm, u64 *exit_code) 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: @@ -3555,6 +3566,226 @@ static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp svm_set_ghcb_sw_exit_info_2(vcpu, 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_restricted_mem_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; @@ -3802,6 +4033,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) { + svm_set_ghcb_sw_exit_info_1(vcpu, 1); + svm_set_ghcb_sw_exit_info_2(vcpu, + 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", @@ -3906,6 +4149,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 e9317d27a01d..7f8c480dfa5e 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1337,6 +1337,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 284902e22dce..5e7cb0260dc3 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -103,6 +103,8 @@ struct kvm_sev_info { spinlock_t psc_lock; void *snp_certs_data; struct mutex guest_req_lock; + + u64 sev_features; /* Features set at VMSA creation */ }; struct kvm_svm { @@ -224,6 +226,10 @@ struct vcpu_sev_es_state { u64 ghcb_sw_exit_info_2; u64 ghcb_registered_gpa; + + struct mutex snp_vmsa_mutex; + gpa_t snp_vmsa_gpa; + bool snp_ap_create; }; struct vcpu_svm { @@ -734,6 +740,7 @@ void sev_rmp_page_level_adjust(struct kvm *kvm, gfn_t gfn, int *level); int sev_post_map_gfn(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn); void sev_post_unmap_gfn(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn); 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_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 08dd1ef7e136..a08601277497 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10387,6 +10387,12 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) r = 0; goto out; } + + 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 || @@ -12667,6 +12673,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 Wed Dec 14 19:40:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073489 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 15BD7C4332F for ; Wed, 14 Dec 2022 20:04:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ACAD78E0011; Wed, 14 Dec 2022 15:04:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A53A78E0002; Wed, 14 Dec 2022 15:04:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8A59C8E0011; Wed, 14 Dec 2022 15:04:41 -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 778558E0002 for ; Wed, 14 Dec 2022 15:04:41 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 3B443120F65 for ; Wed, 14 Dec 2022 20:04:41 +0000 (UTC) X-FDA: 80241989562.28.E8CA057 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2086.outbound.protection.outlook.com [40.107.237.86]) by imf15.hostedemail.com (Postfix) with ESMTP id 72AEAA0002 for ; Wed, 14 Dec 2022 20:04:38 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=q189s+Wa; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf15.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.86 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=1671048278; 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=1Jd4sSlG45whTkhGTHWXjOr2AQ4CCq8eVlab5mMdIrw=; b=1K70FtxldUH+IvW09tYhwKqeHEDyIvFdJx4EKNuohqXCnoPU8ZLr3Da6zhPm1Ju7GRdz6r NNF7ip2Yx/pP23R+SDCiEEraUEGf/UoW6kRJveJp7nXLDOxkfaUyT66FdjcaxxFKCUAoRO ArVP+XPLxRiShkTqpeyiEwI52g3Gxrs= ARC-Authentication-Results: i=2; imf15.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=q189s+Wa; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf15.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.86 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=1671048278; a=rsa-sha256; cv=pass; b=XqFPsStGe4zBWaSU0DHjFXZwCPcx9jOvlfoFCioDFISYaHSWk8bBFo8s0181ay+Uzz8f1N 2d8AV7Ys4fW5FqrKHin6tD0JtuDodryiGQNylwHKlHO/qZSZQJWCvQ2du0BCvDipyh//qE L9FxS83MC1hWPPlyMJqA3/NETKr3WNc= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=V4hdvMacOIAnfET+zFtZl15GOC+atFcvXM6MNpv2q4DAM7tC+RN/49X6tliHJCsdfk9gyb9GcNvtH+ekd6bTXwKv5rCU462nN3C/jiS98PoXLGKg5JdVemJ57yoj0Zk0w3yQl3d5s+4+AypEXrw8PHU5eR7RnUCaMyWvRoGmeQRODseg6MXcq7VcfSI33agqi+JO8veFx2iECc0NeT6PNvMwL58gE41aAcGNaVPBRoD62+AHwY6JaLCF8bhmlFiuziPGs8kp0LT+eHpFGl7q7acfzLiniYZk0vryOnYulE3qq813rIuVZ9id/Ry7P62TVahhIthAiAMcJwE4VC9b8g== 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=1Jd4sSlG45whTkhGTHWXjOr2AQ4CCq8eVlab5mMdIrw=; b=RmNNC3rGnbpE1qrBUz4UG4RDQPH0Gc/ejw7sdxXlGUd/oSPX5lCcWSzmp3vzI26ptmIrBvrpmz3q9HmML39g4o3UQU0pZ6z8QVYB/uPFnGTgNs60t5MW50QMsv1Et+e3tPfVNcnebVeQ2EB8yW6Ce1MT9PMr1h+ndCq9Ox31icoIvvMcdf3JjpRi86jO78VJ83FM0/rSRVDt4fgOBOIFjSSUTZ/Trt+qthwofsA62bYzWtw52LD/JHcGd3XTztscsnPg59l2eETC1XdW5ya62T+D3wj0jQ4bByyCKLXz+SbtdYUZq1kTre9231Nkpe8gE6SSxpPj0bpRTfKL2nhB3A== 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=1Jd4sSlG45whTkhGTHWXjOr2AQ4CCq8eVlab5mMdIrw=; b=q189s+WaF2JnBMQqBmakZs8ZQZVa6gHkQTTjq82ABox97xEODVRjsmy57rGSvO6MqLdiIiGznj9/3ysV/vjg2j4EvK5pbEH91xQrbTb9IJdEq5dbinwPl3GWw9hxDnAA3VcWcnm/zRjQViH0WUdwU0B2mcbnNpGDbT03j/ucVcw= Received: from MW3PR05CA0022.namprd05.prod.outlook.com (2603:10b6:303:2b::27) by DM6PR12MB4122.namprd12.prod.outlook.com (2603:10b6:5:214::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11; Wed, 14 Dec 2022 20:04:36 +0000 Received: from CO1NAM11FT073.eop-nam11.prod.protection.outlook.com (2603:10b6:303:2b:cafe::a8) by MW3PR05CA0022.outlook.office365.com (2603:10b6:303:2b::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.7 via Frontend Transport; Wed, 14 Dec 2022 20:04: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 CO1NAM11FT073.mail.protection.outlook.com (10.13.174.196) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:04: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; Wed, 14 Dec 2022 14:04:34 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v7 55/64] KVM: SVM: Add SNP-specific handling for memory attribute updates Date: Wed, 14 Dec 2022 13:40:47 -0600 Message-ID: <20221214194056.161492-56-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT073:EE_|DM6PR12MB4122:EE_ X-MS-Office365-Filtering-Correlation-Id: 10020b2e-3b6f-44e4-9419-08dade0e6c81 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Z3a4lW/+Qg1wUTpH0t53K0b3eeXn9mBf0LapCpB2B0Br8warerGU9ogIDW2eVVzVNaBnfCHWnvemo7qY5vPx7LbOL6sByUM3z2g6PMYovZgfXAvvldeyQGtEggbx2Fjq4WqFzVdrLjvMRtuI97GHIY35/vG+E3b8dC1IkRP8LSKYO12aVXUQNsiKJf26qLpitJJDXdcWYy8b1vuw6QYoAItooXBic6fryufNVxb8+uiuO6L7SUPx+2S2/ehO2afagmitvSLtYaJGOP+XEziZHLdBTWZjZr537TWQ2k+IkEcOv0vGRXVmEroyJJX4YZjNBxQJsiT/LsZBzjp0qwMK+jjEtVvW8bWoPsue0qkAA1CS/q9bVL+05VtkE1miXGqNlL+cZDF3dvBS6ioftZLWhXzO2dNwGbIB8Th9dHY72O9S62UHWJIw38EhOCapn8/Qx695qs/mY9zFC8D/A2R9G4KsK0NoLn+neomyaURzbJxygsoQUCe+GbYqW4Pupcjk4h2JDjcyMEyRBuXjk652DIIvy8wobJiqE9RRQAac4waw5xDA/4R4Q7TPPSZ3BauTPs5nTyyhdeurYJd3cX8+n5138C5gq+IVYkwhCD7BAsSOgel+UabaQ8e2s/Fq65o/UPvnKFc2QbPcBRyx+zUl4LQHvUpeWYgATkqQFAooXC4aUcdDmAG1pIghQixyRFMb7V8FpS9LRvHJVpvP1a8B4Wq3sEiTLgeKAqmB4Q62LOM= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(136003)(346002)(39860400002)(376002)(396003)(451199015)(36840700001)(40470700004)(46966006)(86362001)(40480700001)(40460700003)(26005)(186003)(70206006)(8676002)(336012)(41300700001)(70586007)(1076003)(4326008)(2616005)(478600001)(16526019)(54906003)(44832011)(7406005)(6916009)(316002)(6666004)(36860700001)(356005)(81166007)(82740400003)(2906002)(426003)(8936002)(47076005)(83380400001)(15650500001)(5660300002)(7416002)(66899015)(82310400005)(36756003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:04:35.6123 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 10020b2e-3b6f-44e4-9419-08dade0e6c81 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: CO1NAM11FT073.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4122 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 72AEAA0002 X-Stat-Signature: 9asbhufb6gn1ey4ocnohhxw1o4gq85od X-Rspam-User: X-HE-Tag: 1671048278-403128 X-HE-Meta: U2FsdGVkX1+2o6w7RAyN5x59gp/6qphGD/GfL/FBy3BNJgQ6+vK2rpOhC8abI4Gpg5gm+iAssnkf1sWoCAMro2+/sd7glcONg9xNeceihE1zECHXlRxGFxkjvg/++NPl/3f2jMuhJD4Di5lUye5WE9sHNLJEDcttsleMPSAFeWEzj4AWKU2gBX1OfqDG9jGkxpDJS/1q42nlhmr5kaxa2b5Ub5NkgBsi8kjhGnkpXn22TnK7barEM6RyEdygPJ7ynJbpX+qveVz1ohwiIResAFh6dW5YXMdGElcwt4IWHogSN17j/IRW+MbEPBQDmdPMq1Nz+A4/PGBYsC3WUeWtmmX7JlyADNF7hOsn1pCqhbPU82SSFykT9jMBuAFcAtivSrIsQijVlJauCc3mUrZB5NDM4KL+Re8S5YVnHBko4Pr64hZY5WD7x1Lh3dHupl1GU4VT6z151WO92bbO09JvsCFSfrbWJctNOD87GOepquWqfsAYZOXoV/HqU4iPW9V74N47ZJ8qTeRzbSFAyGu+WN0Ug27QYk6kftG4OJ5Sl8HjAbysUX0a8iQQ4bmLqPpH0qaf+319djqPAy6qtaPOuXzJ56gLFlWyplDUv0XunvQAc6dUPNzeX4FZ426U51HCu2ZpN+nucuSJhwNpiI5WrCwLdwiGVZIgIwSAvDaZuBDIuqcZ4LEokpKvRnGLLZAHfwvOq2KqpyHZmX9oFPb0mbBQMMp/ci8TwLU4I9Y7dD68pwYQkCPZrF9RhmYuS7mfHiflWb8aHzE4w6fnlQLrQRhhopRwOUoGfaiSv6zOaYIHY72l28SD0vBm6YtDYEJMMqt45O1sMF/mqvn3QBnMvdtiwwDzuHeHLrUrY9f6llxfWnqrUiF8psIfhAXXBvFp6Cfp7pO7Za5pp+HiRWP80iPSWwK23o0LP1XYoiBeIMLVDpJK/Qf/2kmH6E4GfR+msrTxSsdNWr7/nKTEpiS 71rzkZ8H mODnj 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 2f4c9f2bcf76..885a3f1da910 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3395,6 +3395,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. @@ -4428,3 +4453,104 @@ int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *priva out_unhandled: return 0; } + +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_restricted_mem_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 7f8c480dfa5e..6cf5b73f74c1 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4872,6 +4872,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .fault_is_private = sev_fault_is_private, .rmp_page_level_adjust = sev_rmp_page_level_adjust, + .update_mem_attr = sev_update_mem_attr, }; /* diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 5e7cb0260dc3..5f315225ae4d 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -741,6 +741,8 @@ int sev_post_map_gfn(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn); void sev_post_unmap_gfn(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn); 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); int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault); From patchwork Wed Dec 14 19:40:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073491 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 95746C4332F for ; Wed, 14 Dec 2022 20:05:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 350238E0013; Wed, 14 Dec 2022 15:05:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2FFC98E0002; Wed, 14 Dec 2022 15:05:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 12C9B8E0013; Wed, 14 Dec 2022 15:05:03 -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 F1C238E0002 for ; Wed, 14 Dec 2022 15:05:02 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 98A73AB642 for ; Wed, 14 Dec 2022 20:05:02 +0000 (UTC) X-FDA: 80241990444.18.87007FE Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2050.outbound.protection.outlook.com [40.107.94.50]) by imf08.hostedemail.com (Postfix) with ESMTP id AEACB160016 for ; Wed, 14 Dec 2022 20:04:59 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Ds3YocRQ; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf08.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.50 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=1671048299; 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=VdFV0whZydjkEkUMB3fvnv3WDNrdWPRikxIjb+Jw6Mo=; b=MMvIaSbhCrUzU76icfH13HASIOG+w8syckAm9zERWmqedTpfa+PaEPKC8tf9zfsG5FghfJ qkAWHq7KXIMLtyLSrchse0PPfI6Y2Jkb1SO7cjCsYfFNsx4sXkN7043iRnvq5ywJEN8Wsx rcl0UViX0rUEKTet737CCx6uwq+pzho= ARC-Authentication-Results: i=2; imf08.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Ds3YocRQ; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf08.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.50 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=1671048299; a=rsa-sha256; cv=pass; b=CNjy2+DZjQv41c+yciHbDlL0Cj26jNvw0KQXalVDCqPYmtcLJoHLvHhEH0VBNYpKcD/tZ8 9abC6ElVT0EaRDyXZluonHI9pFoTpjRKTjj/Yjvr2mt8olR7u58ka85rVUZWH1THWODGfE uekl3ApMZa79KGmbUb2gukTzu43uEEw= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OcKuM5j5YFDCdqLl92inJmcuyfRUfl+v2sfewlyfr9A6/MMAJjL7pUgKu3eP58I2Cq11NCR9IgUnT9oLk5u61wc5uwoyUqoztZ1TYYLRQ2/0kXV8llcX8zitihrkwGbiFdTpwjUWgylK8RCdYjQUz1Hwd7rkD2cS25S98+99rwkVS8bilEWXRWlVRqpbN16LJWr5jA0r9fkks5NC5XUluaGgYrzevNuD1+k9tqUfHTc7pE6rKZyvnqZmVom5VLiLs6utzqgSpsTy2Zy4fNaCUO/LtQb88jLqrEHlx+Z4ReJMCjKAFcDHcwyt99Q4HJVFw64bVDpR+DFNaZ8hL5A5Wg== 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=VdFV0whZydjkEkUMB3fvnv3WDNrdWPRikxIjb+Jw6Mo=; b=moqoiGlNCA/m7nJ5hlhiqxYvP/Zc/Py2eBAADvdtqoCQiSvENk39YvXjQwyg813rl7bfEYDc+d/4RFOamRVSlfeYaVi+7IQ5eH4cme2rJ7iVtDQ99kpkcfdQLNdrn+m20TOHNBXnVPUwKv9I+u3MXav9o82wwxZq8flGdK2a7y9LVZE5BPWE7pRCOXdwy3P+8cdMCVsV8rWem63zR8ptkwsBGeW6pvA12nxhNp9jwFSod4nkA7UHKChHhPGOW8fj5mfT7+YRIHcIsS4K4NChT8kjcKmMSLwrMtueIrsX12WveHcUcIIzNi98Mu+oLloNUbOW2kiiA09MMvA44DgdKQ== 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=VdFV0whZydjkEkUMB3fvnv3WDNrdWPRikxIjb+Jw6Mo=; b=Ds3YocRQWIfYpbAEHhD8te0koL9WdvXfvVhslOM448dLZHI3ZbPahwQC8r9TlZKvKR+C34KgEi4Gyq/zsJg1jSTeBU+Qd7S7+n0lioYaGok5xsetHs7WF7BTsOyPcR07iLytIEexuWJEEEzFZXylEMqnuaFyFjhJW8kxSi1PjtQ= Received: from MW4P221CA0021.NAMP221.PROD.OUTLOOK.COM (2603:10b6:303:8b::26) by PH7PR12MB5904.namprd12.prod.outlook.com (2603:10b6:510:1d8::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 20:04:57 +0000 Received: from CO1NAM11FT107.eop-nam11.prod.protection.outlook.com (2603:10b6:303:8b:cafe::55) by MW4P221CA0021.outlook.office365.com (2603:10b6:303:8b::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:04: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 CO1NAM11FT107.mail.protection.outlook.com (10.13.175.97) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:04: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; Wed, 14 Dec 2022 14:04:55 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v7 56/64] KVM: x86/mmu: Generate KVM_EXIT_MEMORY_FAULT for implicit conversions for SNP Date: Wed, 14 Dec 2022 13:40:48 -0600 Message-ID: <20221214194056.161492-57-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT107:EE_|PH7PR12MB5904:EE_ X-MS-Office365-Filtering-Correlation-Id: c4a67f0b-5239-46a7-ec86-08dade0e793a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Zk3MkAaPPMkQ+VUyCTDwmYxwIBeFVs1VuJYW8UkrvbPQKJT4Ec8uXWa+ndJWwQD6H1JLcZ6nNPQfuZA133g3v7o3BkG0NIRxlXD0jIiL31FmVej6AGIggJNofojZNsp9kRduFJj9TSAWdjJiX2p0bswZNq2N3MQ3KPPRrj/bJUjMKSVE4Bmv2CIbgR24cFNyFMZR8qlTmRNzYCf9t5KriTICu3su2qNINB3REr9A4kBsjucuRX6XTzKG7GDY1dWkwYbDoKsXVGnWchMGMxifJ4UJeNxo9vZUIQDNUzIvLHf6b6s7LlF1sj1n5dKzceSQWAV+3jTiqPJkvS+Q94i2ew4RVygP9MtfoyyXb4aWaewiojFReYMYvToKZelNTWLRTNDZ0WtIRG8jE66CLIO/anbImS2U4iQhKfTprFEgmupBBLk7bla8+j5F9ZHFZbAYhRkY4s1TDiSbGJlOg1+JSA/PisdOsU4qhtKCbwdws/5QSUUV5BjxNZpVaziG1EuQyhkPZoRsVGyR3u0q5BUWI9kF3uU6RbNaaHC8x1as1oCQ/0DpsRytsgwFCk8OPRt2DFwabjcAYKt8pFd3yjZr9A7JKSFoQQUtIwXv4AlpO0K4XThPx73EUvjWjnUPdaGHuaLMXbafagFGSgbDu3BMoqXdI6pQjaJtaOaCWYdtvhgTd7NLWmJPwOP8rdMYplSkIGpOV0j1pYRcpyd6jE23BBxw0rem0O6vugfRxGYpMZ4= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(396003)(39860400002)(346002)(376002)(136003)(451199015)(40470700004)(36840700001)(46966006)(40480700001)(7416002)(356005)(81166007)(2906002)(44832011)(36860700001)(478600001)(186003)(26005)(6666004)(8936002)(5660300002)(70586007)(70206006)(8676002)(4326008)(36756003)(7406005)(47076005)(336012)(40460700003)(426003)(82740400003)(2616005)(41300700001)(82310400005)(54906003)(86362001)(83380400001)(16526019)(316002)(6916009)(1076003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:04:56.9284 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c4a67f0b-5239-46a7-ec86-08dade0e793a 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: CO1NAM11FT107.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB5904 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: AEACB160016 X-Stat-Signature: e5fiumfuzpwcck7p58xisoq6e543uhdo X-Rspam-User: X-HE-Tag: 1671048299-662753 X-HE-Meta: U2FsdGVkX18pbQgsYape2GF8CyAS9YfkcaL1KOTzS2tVTNSelv7B5jJdcI6ReO7o6Br/4AfMOaiDPYtHRqNdEgCjYq/k5pHYZGKOgLHIf1Q//M6P4m44MHg0HKtii0QN/pPa+U4TVwCVdfS3+p3rYbfiXRDHGW9WVNhN183B1FLud1YXCGXa8xgJnXGf+P5EC4W3g+JxNLmo1HAD4KGw4CGHc+a+nok5+bLMwdE7alllMj3ubp4U2i3UncO14YU8eVxrza/KbvHqjLi4xY+Tc8oW/yXYIlRERUSND+njjo2ZvAh/xsRUtqAKHo4hFk6CzkbxVQ21kG0aNPlqPv0msxhQeBT3I4lWma2SdHJysWGTlKTxYLCv6uBg51bRNGNXovmZtCufE43K6i9KZgsjvu0WKZU0Tll9lpflkF/2wcu8Jj6dAssqG39YpFjVmmfWKtfH648ewTWVgah84wFEDH76cdcMSqS8mgPxf7nR9PGk9l1fu2WkjEaDZ/pvrfM3OyX7ybuhBT7D9Na+rdI/AAzSKhJl59QRUmLouooEB+S3kqHp1BZqrMPM8jJfaN0aaUXj1pP/QPRkT2K7G0BNhRMyaFpOTyUixPiJuqwkBujeQjGDxWkI7n4fvIEqm4yjE/qBbt9sM+TOOx9zAsFQuGfaPAMuskYdK/ZY8/kRe3y39/qIxrYFTq0TscIfkAMx0LPWdNeW6hdPEBEPNVCdsasH2Bo7WVfmHStA8LQpBt2nrq+CO+75Datc5I4g7+ctsqyfWaDIC0IeEUnKv7IZVkZ4X0ec6UfeXfBkNgKh/OoPD6K+R02u2lza3DZUyFhM1hncQaoa3huyq9yaEfKW6B2SPBDiw8nzNeSsMORZzSuvb9EsS/jHvLv5alWPtPhYVFxR/sQkM8+0tEQ872bPkzQSW8uPaBGwo4neKsjjhX4fE9R4+q8GB6awbAbimgDYXqO6f1dFV+Omw/6FTz3 Pnz8m7Cp u/n6jbeJLdBTMGzGe6j1mCvMM1Q+LLdIlX/UtwvdbzJO3+8Iqnw8uSGU3BA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000330, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: SEV-SNP will set PFERR_GUEST_ENC_MASK for NPT faults for encrypted/private memory. Generally such accesses will be preceded at some point by a GHCB request to the hypervisor to put the page in the expected private/shared state, so the KVM MMU wouldn't normally need to generate KVM_EXIT_MEMORY_FAULTs to handle the updates at access time. However, implicit conversions are also supported for SNP guests, and in those cases an KVM_EXIT_MEMORY_FAULT will be needed to put the page in the expected private/shared state. Check for this PFERR_GUEST_ENC_MASK bit when determining whether a #NPF should be handled with restrictedmem pages or not. Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 885a3f1da910..0dd3d9debe48 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -4446,7 +4446,10 @@ int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *priva * source is the only indicator of whether the fault should be treated * as private or not. */ - *private_fault = kvm_mem_is_private(kvm, gfn); + if (sev_snp_guest(kvm)) + *private_fault = (error_code & PFERR_GUEST_ENC_MASK) ? true : false; + else + *private_fault = kvm_mem_is_private(kvm, gfn); return 1; From patchwork Wed Dec 14 19:40:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073494 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 2FDA3C4332F for ; Wed, 14 Dec 2022 20:05:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C77878E0016; Wed, 14 Dec 2022 15:05:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C02F08E0002; Wed, 14 Dec 2022 15:05:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A2CD48E0016; Wed, 14 Dec 2022 15:05:25 -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 924128E0002 for ; Wed, 14 Dec 2022 15:05:25 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 64E2DAB664 for ; Wed, 14 Dec 2022 20:05:25 +0000 (UTC) X-FDA: 80241991410.07.4A5D2A9 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2057.outbound.protection.outlook.com [40.107.94.57]) by imf17.hostedemail.com (Postfix) with ESMTP id 75A464000F for ; Wed, 14 Dec 2022 20:05:22 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=zy7KyVxW; spf=pass (imf17.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.57 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=1671048322; 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=/OzKsfTHOxu0vZX+JKMUe8Rr1JlYN051/tKqUBDz3IY=; b=SlZmTqK5UIDm0TL5hSb2WePAgW2kPfrbQQbCnB8w/fo0JmVmmi9M1lAHfKjtLTW0e6M+Fc PnU8Ld3NxsampsgS7t2P2yMH9xqFQyS2pShy02Xfspe2/Kzd710jM3DrAdb7hfqtgbdTIX Gofe5nDqPZ6+KdLiNqhLZCxfJL8IDwA= ARC-Authentication-Results: i=2; imf17.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=zy7KyVxW; spf=pass (imf17.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.57 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=1671048322; a=rsa-sha256; cv=pass; b=2WcO7WbzVn7bi/fMzU3K3HYca8vLG4XC0VInNdNIZHmzzq5ni3Rz/nA9a2QTZQzokTUPoh pTTSuNJYM36Rke6SYfV71xSiJtIFzBekG3vr5/RHkG8dpIVBzz2XtnISPWePSukqMYeVZw 9AP0Yd/T70/oZiwtTWfo0OX0VS1lE8E= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Gigt4+OIWss4F3sckjVWigrt72JQJotc74gOljeW0gQqoP/hLGdGdqKPm/rrU0TaHEerlL1Z58Xq9BrUqu5RnyvgXTc5dNFix0CdLx4CRZuXpO4i382qYPudoa8OokSUOKSspzDNHuO4SS9TYOVEjs/lQXcDdGNn7Gc13qQ3CGJeVEMgY/RdK+IMZhhfrWb/JvtVWbG9aQj+ddSvQEMNlSTaos5GVhpORhR7CFgzBrNgat+dkDOVWzt19N+wy9dnVwKcvJuCm7goGk52C7hG3UGshKct/u5Py4oTsjgw4iG0WPlzkDyVdZWbZ97OWM7cNh5O5DpiqtX37oMLMzFXcQ== 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=/OzKsfTHOxu0vZX+JKMUe8Rr1JlYN051/tKqUBDz3IY=; b=NGGL5/JQwESKhM2HneSqME7bmqQE3CkV1Xx6XRKhgavrM5GMofC09RGToOQMQQ6gqyXF4Cstgi0RhkPAA8JpSca9ZHzH4A/t8Gd6pm8lQkz7XXbrmdu3/W5+qNacEjXtmaVGG4gpcVSMK5563k0XWAuIRMQcwb3qGyyJZYhDawkroIzbmvszphgBKYfDeWCYiIVRYLeCWXMtn87pcSUtW53H6JeWp5FsR5UaiwiG14JezjSZfDBCcGkBMf+HnXwBpdOAPsbUJKPigegCTyoxkEbsu8bqTuxr7ZIubTQTYLnzdWbf1LMh4bRox3wTZO1UXxrSL5P1baYZYxcaXMiR8w== 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=/OzKsfTHOxu0vZX+JKMUe8Rr1JlYN051/tKqUBDz3IY=; b=zy7KyVxWzEu86uKX/u6TQ0jcyLO2kfojrlVWBMOod2JBV226AQ0ITemRBgFRUYE8F2WFq8rEOw5d2/KIsnbVT5+QYIBXceLM2G5YQjfCFslF/zpNNvlD6hEc1Xns8nx4z96Xpm03415vaIi1IShIkcR6RBQ05fCW4nZ1IPgAMuI= Received: from MW4PR03CA0113.namprd03.prod.outlook.com (2603:10b6:303:b7::28) by MN0PR12MB5810.namprd12.prod.outlook.com (2603:10b6:208:376::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 20:05:18 +0000 Received: from CO1NAM11FT061.eop-nam11.prod.protection.outlook.com (2603:10b6:303:b7:cafe::99) by MW4PR03CA0113.outlook.office365.com (2603:10b6:303:b7::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:05:18 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CO1NAM11FT061.mail.protection.outlook.com (10.13.175.200) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:05: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; Wed, 14 Dec 2022 14:05:16 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v7 57/64] KVM: SEV: Handle restricted memory invalidations for SNP Date: Wed, 14 Dec 2022 13:40:49 -0600 Message-ID: <20221214194056.161492-58-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT061:EE_|MN0PR12MB5810:EE_ X-MS-Office365-Filtering-Correlation-Id: cf9aa6fd-dbca-4888-59ac-08dade0e85a4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: lSuHogvfbQhYnoZB0cs2NqUtiSUv8xndLHEOwrbfFJMuPdA700rGJsuWEVb/2n89tEdYoxpVHbepcF2HFXd6EGJ11/3MSsW6XSMcROjGZlCv80uJTz4AoGZBw7TUpnPBXylGruXNE/piQa1SAFyRV2iNbB2pkl9IIxpYBEBbcoQvc8GA1NtRPR/Xp+4FklAhKF3/h+HaOdh4SFQL7CVjlDY4b+j12JfxCmV2QVyxx3NZgX7UiPeiJ188mEHDJp3Tq43SpxmebIRErYzFGjejd+pXQCxLZSvZ9VUlBxf5Mkw97IV8Wcaw4TmzSoVsBclSPdeqAmLGx6Y8G4yetI1ztALDGxTxmZkelifFD128J/KWZbV9g5l12nTUwp2KjhejgFzf1Wul4p1ZUolw6X+IHprLMv0IelrwzXZIRWrqFMj6r+/11EBp4a2R0R1Ty5EqBSoRXFcYDfIXBQDf/DtT0wtQElkqazF5wRla7OcwGhvQWDs+bV9MVnXXtCvRx+hxSH/ZjGt7w/PPFN19Vj68oPNiz8v3E440zweh+5GtDEE3SMWRWPwneJ/tZDj0YG990YNC+Dm3fD0CgzyZ2ayW9lm3A1xE5YNRzDS5KQd792sYnXNCL3kOZ6Pmf+4uBjsc7n0HataOO/1kT2jbeFwkY0/S/YhWaI4NvkrM+hEwOIy/mpi3sn+4PTUs59TILHHHGQa0NMb9en6PBJcZFYdt4qo9iR/gyl7ktRc/9LDQfRw= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(396003)(376002)(346002)(136003)(39860400002)(451199015)(46966006)(40470700004)(36840700001)(36756003)(6666004)(40460700003)(6916009)(54906003)(316002)(81166007)(8936002)(478600001)(82740400003)(82310400005)(356005)(1076003)(336012)(186003)(2616005)(26005)(40480700001)(16526019)(83380400001)(5660300002)(70586007)(86362001)(44832011)(7406005)(70206006)(4326008)(2906002)(426003)(7416002)(8676002)(47076005)(36860700001)(41300700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:05:17.7989 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: cf9aa6fd-dbca-4888-59ac-08dade0e85a4 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: CO1NAM11FT061.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB5810 X-Rspamd-Queue-Id: 75A464000F X-Stat-Signature: pgwmo735m38azdpbrywayqe794pm8noi X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1671048322-416737 X-HE-Meta: U2FsdGVkX18kT8gIbxIS4tA4lI8x2todxFUPN6PVdDYXGJZ5H6x5cw3yjFqsEZmvrPm4ShU1QsMClwEq3xkNCKq4H9h15Sm3mzCaQC3Jr6o1NM4ywu7UvQAG/lZivpGXbxIwW4wSpCDQB+QVs5niteHgfrOro1lA7eFdYsRrcWkrb2KYok3k1D05EGdEX6Ai1COrpnHz0OmBusfqeyq+5hnexzBE+t5/Ussex/CCXIwZEcbeJvJrxP/uYAABJ9zFUVt94rT0C2yeIKvWxcphEzUt/IpQAM72Vs+EY0c0f98n9DatF1YTVhhCGwfxnwe+I2Q6pULJ+CbBX6MLQD6CsgZWEGFAmcV2+BJ+mmIq/VEx5gVqlh+/AzZdH/azyZRdLZw17G52G+xIcAoamCfYHm4BuwtbZ4PdimZxcgEfPme4TyBhX0agpzOiD+p6kFOT4he2wJeNc42yr416AvxoE9EuRUH2K8vWBxRCYXyGWqMaD0d1IAjXGv+lYVjGuaYztG9bNL+G188Uigi6ZTMGHXZyG23FMHY6pYea0ge1M9FA4Ddkp7fTFap1aRCGIrzQer/g84R1u2L3CV5jMWY0gEPQWtq7c0kdyEd7spUaIm58aux3uKItOcyJ1bni4Wf3xM6xo3bRaAkX3Fokcd7IiyM4Z4uYW8BC+rN+vXrfnkDDC6PPKKoYXN+Xv9bsDegnnitMsRj440rYgppAldrmlpiMPok8CH5ALXviXWQTLAZq0+DnKXwnER038rTe7Zqbmay94ytru5mMrSTPbtHI56IUuIQdLX8QiFnf6KwMsNfwmxYnqQR1bXUz4srk24qYOX6SvZ7zxEma6l6+hYB/y+ceKxw5/pe4fl8uGxQAmdAgJs9YHPxDYl153u8eGyvlAvJ0wdrFpNX9flXDaeBwoRT8LooCzu7QeMyTjD80aUYZJwtgPy8W2x/hEHMRBXx3M2NHTj3bgw/EywfAD8X cBks3XJU HXj1q 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 | 64 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 66 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 0dd3d9debe48..8783b64557e5 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -4557,3 +4557,67 @@ int sev_update_mem_attr(struct kvm_memory_slot *slot, unsigned int attr, return 0; } + +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; + + if (!kvm_mem_is_private(slot->kvm, gfn)) { + gfn++; + continue; + } + + rc = kvm_restricted_mem_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 6cf5b73f74c1..543261c87eb3 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4873,6 +4873,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .rmp_page_level_adjust = sev_rmp_page_level_adjust, .update_mem_attr = sev_update_mem_attr, + .invalidate_restricted_mem = sev_invalidate_private_range, }; /* diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 5f315225ae4d..277f53c903c2 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -743,6 +743,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); int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault); From patchwork Wed Dec 14 19:40:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073495 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 75955C4167B for ; Wed, 14 Dec 2022 20:05:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 12C6E8E0017; Wed, 14 Dec 2022 15:05:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0B6308E0002; Wed, 14 Dec 2022 15:05:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E716E8E0017; Wed, 14 Dec 2022 15:05:44 -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 D7BA08E0002 for ; Wed, 14 Dec 2022 15:05:44 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id AA2DA1A0FA1 for ; Wed, 14 Dec 2022 20:05:44 +0000 (UTC) X-FDA: 80241992208.14.AB11944 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2069.outbound.protection.outlook.com [40.107.102.69]) by imf04.hostedemail.com (Postfix) with ESMTP id A28D940005 for ; Wed, 14 Dec 2022 20:05:41 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Vh6XBIBi; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf04.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.102.69 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=1671048341; a=rsa-sha256; cv=pass; b=K94i9JdHlGUeka+YzwvXe5n5UHjXDBdqcHD0pS0lhHWzJ+2lv4SZzU1g9W+RSwE5pbUzD1 ECJBX1XWR2/XQ//QjZ0IACLkw4Gm/kVEB2JwdpHr8aTxfOknA2QrujyZJsfvJNYtWN/HQu mT/clIiesHx/LHk2NtnyNlfugl8/hZU= ARC-Authentication-Results: i=2; imf04.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Vh6XBIBi; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf04.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.102.69 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=1671048341; 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=sCFtQWTk77TMaIV8QgrLv1S9hcmAs7Blyn0qO9scZro=; b=0TMQ7CBLexQcn8bdfaUlQpQ6KakqwmQyHfUqc8hurW9QNfpepIBxMDORMWUfxx39MUSfQJ 8WCOK7Cx1DT4a1rz/QCii58s5EZs3oNPI02Jjz5FMDga4mdDh7W83q+N3Ah1DuRZJj5giC 1tMzt/ngQe1HDt/cy/NaXCtR6jXjjB4= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=i2uAHIvBVxA8OmsGjOda3ZWI/9PbBSNEvl264Onf9fmgJGx0P85La7+8+zHcb1x6ywlpxre8uCeUrQ+frMm1hhnDQVmc/hziS52LWsq7s8UuanflXJ5E9NzTR10q55+EsjSneAoqjazuW4vqMR/v8oGeB/2kB5LMYXZs0NLawJbX4cafCKO2ZT8/hdIxJcjOgYmHyHR4g9hPf0b6qOtBWsZUW21k7a/cL/6z/PnRNzFoH6x8rd5sugdO79B4VEDIvREpCLtMnMaLrP7xlo+v7jHCK4UVCFeNS3OvKKat6O0GEL2xX8Uvnduqj1LSduuhjvSgHGC22JozyX0627kj/A== 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=sCFtQWTk77TMaIV8QgrLv1S9hcmAs7Blyn0qO9scZro=; b=nt1iODPWM/ZXWPA9nWgYU6V+xQc0/8ryQsdLye2Vtt4BIFjIJ7KZkddX6gx84f+M3nEkEffvD+cULh0VSLUBbYIyCyOQFf2aNEp4EWqx5ANjHSAn4sSMuciFi24m+Bv83CRIwihovyaZS9GIxCq2ggQdylIJI8IW5CWDhNVZOjAAzLZ589jYdR+ajab36kIqcIsJQZk7G557S8udfi0njBZvMyIalNebLT5unUqJzhmD9ixOAEpHmAOGxYduGGsxh6pUoBXUI7w2HQ5aDhPik1a5y+9JCdXblcyRscoBd9zuNMj4F0BsbSoBBIbtcHLJwfnfelKu5hMGDndciafy+w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=sCFtQWTk77TMaIV8QgrLv1S9hcmAs7Blyn0qO9scZro=; b=Vh6XBIBiJ6WLuk5liJyjyeB/hw3+q56IpDEqldTzaUjLz2oXGN28gMOhDtrCtKBvV8de7Ne5ep6cEB3+OTuYLcKhhUoIpT4t2yzxOtS3wJYBUW+BYNTDsxdIfKVKGlZfQoIEvgppn8UR5XzDaI4z+nVUAa92FvK4/Lkat4glzQE= Received: from MW3PR05CA0008.namprd05.prod.outlook.com (2603:10b6:303:2b::13) by CY5PR12MB6178.namprd12.prod.outlook.com (2603:10b6:930:25::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 20:05:39 +0000 Received: from CO1NAM11FT073.eop-nam11.prod.protection.outlook.com (2603:10b6:303:2b:cafe::6e) by MW3PR05CA0008.outlook.office365.com (2603:10b6:303:2b::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.5 via Frontend Transport; Wed, 14 Dec 2022 20:05:39 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CO1NAM11FT073.mail.protection.outlook.com (10.13.174.196) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:05:39 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Wed, 14 Dec 2022 14:05:37 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 58/64] KVM: SVM: Add module parameter to enable the SEV-SNP Date: Wed, 14 Dec 2022 13:40:50 -0600 Message-ID: <20221214194056.161492-59-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT073:EE_|CY5PR12MB6178:EE_ X-MS-Office365-Filtering-Correlation-Id: b94b5378-05a6-49e0-6f4e-08dade0e924e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9hK8S6WDAp46n+eq3J3LyrYc1/Fp1Fz/ZfDmq3axijXBMhoVnlYZ76WXY125XrLhAq6BN44sMr5VeAcFGoenGvWn6ZTAjukJYStjEKrHJzyQzrsC8w+j01Jv4Eh7E1hrEJiR6u0Ra+zy6ME4ikztINZCeiNcNQjsS9rLgOjyKrThltWYOHu1yMxkAGtfEytvapDQzHNx26TJlgf4NmiI7BOyqSggKS0upqwGBTOCwcjjk4EgHX2PDbeEsZiA5sugIdE8VlHjXc5u2qApYSAJcEqEydyRNVKU8QGWPN7MyD1ZpL0UPubfaG0tWZ485U9z6xaIoaPjK4fZWoG9TmccJk1GT17UnLKu+MeAyYgur1nb8HCz/YhDMGV/qKLH3OXL0+ioCODWDzGO59p2mPqPpCphcL9UOu6+P0Apu6iWqfsTee3R/Ocas//ILsaFf9A+70KTJqINX2wbehVBCUqL/uxTChTyEVnB15xORPIDqEekjWBF4YvQ1WpG/7ZvFCKozH2boHUtld2yDQgFIbkx+P9E6h5lnxtRzVcLVYxQGH+Mu3620keW7B1uY97fWMZgiosOXeaeuRKszbeLHgc84Y3hZzEumK5EgpH/hw8chVpl9ywXSJgXIlCLKReLlL86g6B0cW8/g/s8eUDFjMnAB48XouDvrfWo4BWwa2hr06Fmuw9cbh+A4Cq9vFYi2Y2Ufkh8Nu1ZLnsc3thCiMveL89LLdxHWD/9CHS7sQhmJHY= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(396003)(376002)(39860400002)(136003)(346002)(451199015)(36840700001)(46966006)(40470700004)(83380400001)(356005)(26005)(36756003)(81166007)(478600001)(40460700003)(36860700001)(40480700001)(86362001)(82310400005)(1076003)(47076005)(336012)(186003)(82740400003)(2616005)(426003)(16526019)(44832011)(70206006)(7416002)(8676002)(6916009)(6666004)(5660300002)(7406005)(54906003)(70586007)(4326008)(2906002)(8936002)(316002)(41300700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:05:39.0150 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b94b5378-05a6-49e0-6f4e-08dade0e924e 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: CO1NAM11FT073.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY5PR12MB6178 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: A28D940005 X-Stat-Signature: cfqo75t4qfk8iw6ci7okfs7rq7k8na8x X-HE-Tag: 1671048341-810475 X-HE-Meta: U2FsdGVkX1+vPrw2WnzHwjqs6c+uhCQWzJHvsipkOcGB0jehrSu8clTonLz7qcLE+K2N74eO5c4fatsFObRw6vc4dSarFpSqfygkoDHv+f3lmYb5ERlZEGQBaWFNR71R1mFZzfMw+gaDUoppbhEp2fg94HSEV/8uUPjJppWJg4Xw0WUifhs5KQ2EOPHC0Xczydeee43qgF9W/5a8qBOx5Vn+FUAHIAdNcJSzdrEsdVj5Zfy3aSrbMba5sHaa016pVaOjRmAx4YHNJ2n5JyHoAoB2ygHJ25DLCBV1fznn8ldIKUL4MWglJVe1RxkcV1Ts2A3BJiqJaDfwYzxee7Yoyka4SRdNgT5gOyzFdy18NgMHX91TW628A/s/CrSxTIZkvM1aNHeodUPyzmYkNhBuId7zL45JIwaSwo2kJM4D9DVW9AepHF+AcNh7L5P2lxeyE5M2WssVsvTlx+cZD7T+Wv1w4XHD7CuQBDtX3192CXGYV/jjgbZn4UFWaDIPSeGlJ/Ybpa0iujgt1jwROiIKjoLLv49TqKEHYf+GjMF7UlSXbw8ajSQGLY/1xp5G59rNJuQt7Z1IzItOsy6VD2G/aTQUGsNhUT4Kt7lsxHFHaXlDGOyLKSQFVHgujinRzS3y+8UZtNW8DuUlvRPFwrtxKg3+u8+MmLd6f5STsVfWE0giPyObrrM+7TVJSXZoATlisNtk7M85zPODVzA+9hPFXZYETZGUoiOc7BbjWp2AlgS0J0svuzpprWSnbAMh4QzpLJKNYc+gZ9cPQdqdr2cExchC/bX04ubob695zkFmW5mcK6pbzDIVeOmYYG/EWz3pBPXo/VnwfTORyjE9B9hP5P1ol3trsrG5RO/feNYkNsurjOgVT7fofcgYeRGHhps6J14Xn73/cUBRRVbkAtDwcZ/uZL1+ZAmfMuA93y/mcjYx5+0SM6K7p8oau0PickUpZhA2F0QdzIYhDyl3Vuh Sw32Vi/m STPai 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 8783b64557e5..b0f25ced7bcf 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -54,14 +54,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 277f53c903c2..4692ada13f02 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -708,7 +708,7 @@ void avic_set_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 Wed Dec 14 19:40:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073496 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 B793BC001B2 for ; Wed, 14 Dec 2022 20:06:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 598998E0018; Wed, 14 Dec 2022 15:06:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5490A8E0002; Wed, 14 Dec 2022 15:06:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 39C348E0018; Wed, 14 Dec 2022 15:06:05 -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 294478E0002 for ; Wed, 14 Dec 2022 15:06:05 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 06C7DA0F9C for ; Wed, 14 Dec 2022 20:06:05 +0000 (UTC) X-FDA: 80241993090.16.8F2D68B Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by imf13.hostedemail.com (Postfix) with ESMTP id 2E62420012 for ; Wed, 14 Dec 2022 20:06:01 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="xIpwy/Ns"; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf13.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.41 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1671048362; 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=nnUcfOdvqA6p9KXxdh9PKvpo3g+ZgbpQiQwrBwMqyM8=; b=HOFhf8PeZKMwuY7+MZjfLOCWxIGx+0xibp6y7mycpYMRIwcusmjfk1VQudGKkYkHnF4ecL zip4xsEb59DI0lU142NashAE4cGmKQ3SyGz3EkJTspMD1aQAgMvDIAzclRtEobxwAi1u2d 5olG5PlIKOIJDQ4kTkHv7mTGXMj4oHE= ARC-Authentication-Results: i=2; imf13.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="xIpwy/Ns"; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf13.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.41 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1671048362; a=rsa-sha256; cv=pass; b=cBZudK1LDUpcwS4MQ4mOtdwUujrF3pty80gZt7L/X+QPh/WunNdJB0+2z0ZTLWM02M6a6V ByblJStXv8msndS6grtIcy8AFSzgy4lXPMGPruRH7hxpoTNOPewpwNWE/DE5+sdHinWWZ8 N+Jse03m4HL8kVZaCze1FgJfHPIJ5q8= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lRSUB617zbllF7Ijn0ZrsPUAPQ/ii9HwnbAlL+o2VBHA847MXkFNEnM+0BVbpD22EbqrfCLybiGCi/4hKqrFAil9jQcifIzXq/DQgb5dhe0y13f7IfsUnWKFm8BICpF47d8r2+1VPf2mHldthY6siYo13ytOK+MUizg44dq9S1AG/qxSoNKsqfzLMm1sHRF0gJFzgIhkDc4v97pDdloIIdYTPtPG+0BMDqpYjbDPz3kxPQmm2x5IkqMUyOUlAg0OoBIxh8INNFT77AFJfAvUA12uwaO6Oid7BVloPGwZomMwaYZZzLTgDBZhQn+SAFAXsVPS+4JHqtwMoYXovnAWxw== 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=nnUcfOdvqA6p9KXxdh9PKvpo3g+ZgbpQiQwrBwMqyM8=; b=VoXjUFm1R8mQzfr9HMckynh45DW1Kplp+eerxZd9LORDlPbQxFV8JmvErOiWeLFIUf9siQbD5fl5eMI/XJZt9Thhf5up76EyI+xBJ5jlxMNj4YeTKhQuh6tcPLsOBJ1U2ZLGaETtQQV8PcGqWMRimYRGPtqHKR6O/I/XSr1FMi/LIb1FKRZFOVS+u2iMNHGezPAkLdXlj1RvJEX5NbA7NYnAomJ8FitlqiVSdxs1j0pfXgxMRbbUV8ES927yn2IrH+vDyQcliL8TAcp2QUtcBXhovSacTV0YloWeAOICKqtFT1WMc0o2DcsDReILhQo75w9iYMkl8z9mSD0mvSNP9w== 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=nnUcfOdvqA6p9KXxdh9PKvpo3g+ZgbpQiQwrBwMqyM8=; b=xIpwy/Nsckst9fJNbEcZVPs7V2Zn0mNOf4VDhdaQNg8Sq0+mfQIbKJpP2HjRIJlEdLrOUCPsxHYWhgWbdyov7h4v1cOBxdZvwisF66GVCk+Uc8DRdzV9gaUw3UMjOk0zeeQW2BJH/DBr5TGugvwI6Qfvy21npvReJzDoyaQvPe0= Received: from MW3PR06CA0006.namprd06.prod.outlook.com (2603:10b6:303:2a::11) by MN0PR12MB5739.namprd12.prod.outlook.com (2603:10b6:208:372::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 20:05:59 +0000 Received: from CO1NAM11FT099.eop-nam11.prod.protection.outlook.com (2603:10b6:303:2a:cafe::4b) by MW3PR06CA0006.outlook.office365.com (2603:10b6:303:2a::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19 via Frontend Transport; Wed, 14 Dec 2022 20:05: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 CO1NAM11FT099.mail.protection.outlook.com (10.13.175.171) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:05: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; Wed, 14 Dec 2022 14:05:59 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 59/64] ccp: Add support to decrypt the page Date: Wed, 14 Dec 2022 13:40:51 -0600 Message-ID: <20221214194056.161492-60-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT099:EE_|MN0PR12MB5739:EE_ X-MS-Office365-Filtering-Correlation-Id: 219723c7-44f6-4d18-5e57-08dade0e9e78 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: SHqC9p+4pajcS18vmQ7MiaQ+0+ddF9sAVF6w2R8gIPat4kiWnb6wht7Q51hX1qZ2CXIx1Hu9IaoqttAUXZQcpMGLwmvvW5Hi6Kmrx8Y+OO9cZ/UA4kVv5hiO8fREcbTlI4Ak9NuZoJ2qI6aEs/N42ExgHWM2mxw8QLEjHLKbjZ1PGhF6/RfIyKRlpnaa0leIVhhHxo896L2rgVMLxqxpq1Xheb11zy4MikhYx48RHdebfzPRi2x5cBvQfIyxQ32u+DuoZ95yoNBmW3RWZ9fi59PoEa/QZf0VikfR9s2T/ueTEyOfW3keLxFxGNdtp/EZ7B0XRHIEbKWlSABgOXsHR/DC/VXQQvJ6XpDsDZX0DdzsYLOhiFBzEHtp2MzzKA3WMngOAibwRIR7UH7hUhDvlUZRXRk3M5defpAsXOHK6/zIhfGxnhXfY5/nHsGt0GR9zpEQyUq/XGILX7fsuqGytMShIdJ5WsuH/0kvUBIdf5JKO52bAmWdoUFqxa/EFmZpQtLXy6lu3QwwWFdYNoaCEmvYRZg9VGnct0PRHuCaMLvVH0+m8iBIU2VMj9NQMRS/jc8j5o0a4r5jg7s0DBdNPWNYGSApxAkiiZl/UmyaQ+H2LLdr9v5jz8iBppqVdQrF5ZyoVkrn7mJhgfztCQPlFgnNeIhtAxmXV10P2ehMnRjisDSuPCQwF549XaCvyfoECkpyy4lxPWc8OfzTA36r4TY3tUddC/dAEtLTEGIN/C3uEGKw3WTpIzCUGCAamAlXD48A/z5rwlGG3NvW6ldbVA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(346002)(396003)(376002)(39860400002)(136003)(451199015)(46966006)(36840700001)(40470700004)(70206006)(41300700001)(4326008)(5660300002)(8676002)(70586007)(54906003)(16526019)(316002)(47076005)(1076003)(186003)(336012)(2906002)(426003)(82310400005)(2616005)(6916009)(7416002)(36756003)(7406005)(8936002)(44832011)(86362001)(478600001)(26005)(81166007)(40460700003)(40480700001)(356005)(6666004)(36860700001)(82740400003)(83380400001)(36900700001)(134885004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:05:59.4537 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 219723c7-44f6-4d18-5e57-08dade0e9e78 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: CO1NAM11FT099.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB5739 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 2E62420012 X-Stat-Signature: o4ioriixzk16g3pckk5am8i3nc77scoo X-Rspam-User: X-HE-Tag: 1671048361-455941 X-HE-Meta: U2FsdGVkX18vQqrp8Tl1DOVmXQUda/PKZW2bFAAjE7mpFZLXCGWubbREhv9Ka7dwxA2MBlSNTfpMQwQwXdA20FRQ42Gnf/uxvRUzcvY6EcATZEfy06D9+ruiEYDfCCBJX6yzQg4Vqixd8y773bf7V6q2KYTGXFhf/K8GBoKmHc/HsqAEslDNKIARyaA1unYyDhJ/UFvhsey+u+VYJKPrWNDmdTVJyo6tESkyutdP6tSCuldJTCL15I1dOnb+nzfphUkARvcHBww9AoEb1i917LcfpdFi2ah+J2Q2Fw7ly2uHMZxpbydW21zKwkao0EttYmI/h0qJcpWop6tViJWVRfg8guiUibIPav18Zgro+KEt2jqRuAH5oi/ekJ0S4+PFe1NERY22H/s596gVVqLKTr6vZ8xsoFLktGBJBhMi9z7PUWIG785p+L0M/7NTvLlmY/8mbiIpiyANzhSi4FClhMXH0HbL/nydnSoLbFAgGv4PZ8r08Ik1whoogDa2o6hRUTbRZQtDmTMNrTsWxv9TbhXUVmQt5VRNDx7+CKC43RLE6PbreQ0UGYOIaG9/Yo69UHxoaMUAVW7mNBUFdty0Pz1LoNsrOy9enMTt2Z8NWcaWRhO4pd5yoTV3ZpBkbJpawVwkt8HqRKKEmQ6QnkZy0f02pCxQ3K8g93hc6pE69E9q6lmZoFz7+KDQG9OPcFkISIgjn0cqZ3DwPWLpYKcArwOY79yMJdE/xBfIF9Yg07oycQ/ifwuUjVjDb8LhdZ6YvwUCtFx2ScwU6fY7CIHgrR+So3CGkxD/HUNTLiopXPlsFY9NiU7gXk8agOc/+yT5diot/xHnKHNkS4x6aqtKdVCa87mAensMpQwzJw5DbVE95dNUhCNU1nCAW1tOocbAHde/GRR5OT8igEGVR5lNaeNAYVEfCdiXZsCfP/XAEYV/dx1N6tctv3ZJQhPT7rixKbPFutPxlWNpUZOe/Sx s3o1n+ZA 3dVLQ 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 | 33 +++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 21 ++++++++++++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index d4f13e5a8dde..052190bdb8a6 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -2017,6 +2017,39 @@ 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); + data.len = PAGE_SIZE; + + /* 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..a1e6624540f3 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -913,13 +913,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 +1001,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 Wed Dec 14 19:40:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073497 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 1F6AAC4332F for ; Wed, 14 Dec 2022 20:06:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B16928E0019; Wed, 14 Dec 2022 15:06:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AC71D8E0002; Wed, 14 Dec 2022 15:06:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 940958E0019; Wed, 14 Dec 2022 15:06:29 -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 83FAD8E0002 for ; Wed, 14 Dec 2022 15:06:29 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 5DFD9120F51 for ; Wed, 14 Dec 2022 20:06:29 +0000 (UTC) X-FDA: 80241994098.14.E1FFB2D Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2060.outbound.protection.outlook.com [40.107.93.60]) by imf01.hostedemail.com (Postfix) with ESMTP id 6422240003 for ; Wed, 14 Dec 2022 20:06:25 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=dNVKq2Td; spf=pass (imf01.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.60 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=1671048385; 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=6UfBSbTttNEq3CmS6i/IUU8habEU1ekDUawFULn11fY=; b=nNVcOhUkguIga2osh0STaC/oV7ZKsREbn/v1rxdtah3BY/HuEaJQLeb2ql7kKHExkos9po mJdaeyALuQycxe+aYpx86sNgfCoWMv/MGN3v15gKUWaiAmjfyWPVx2uM+gKAVIJMkVIXwb mFKgb0c8p8wzeXSHP/WOeyUT+4KJcIo= ARC-Authentication-Results: i=2; imf01.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=dNVKq2Td; spf=pass (imf01.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.60 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=1671048385; a=rsa-sha256; cv=pass; b=42t37oPnD/yp5URk7OB1ZloJeC7cZn29MS+Uin9e774jGhe1QBySARBVZk8LYJArYQVrqI 8o2uBHwVeBGSPPjkmxbhv63cFBmWbUGzr5lrQM1wB89jFMKA2jK4Gms7kpMyTNAgb2oXg6 xrAF+gAUAn7NlGANFBxRcMTyETsy31E= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RZikPoEYQaT3G8092RXf3lGvElWf4UHfSd1tYb4+dpvaAHpJsSQjeZFD+o0mHInB/zfPIWlynSfCraDYvTlLugvVVHE7LoacxoyDKKOCIZE3FxoCGz943bONFEmpcTXsf1LEZhJoS0hMlrlzpVLjjKAJj5EVWz9Lk1nudsLXMl2p9I0kcPRu4vqljGUcrD95PypF6e+rOM/FzgW2FU32R5esIHtO73881MUZLP2U5A1pnGPB4pGx/mMcKLf8B7a60m6l5/ObcZf8Uno1SHz8+VIzFDtHCnIHG3AuoLxINKSbi+xDrZ0YyD0fZf/HEWeUNqDz8kXUADxL7LR+j+iWnQ== 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=6UfBSbTttNEq3CmS6i/IUU8habEU1ekDUawFULn11fY=; b=W+V+HfoaAXLzA4F0qckRgnTdMxcjoOLb2Iwo90yaxy3b4Is7koY1IPiWvYyQZJgFQcI0wWb3C7m9EOCUkuxRBGqgD+wJd7DZJgwjMKKD+2G3ZAGVF/gBLJnvPDF5NXpM8dq/vsICwr64eWaMc5SSKNw/1+lznlj8YZ5V1rW465+X3Nk6XtaEJcaJ4AgeG8jQv9JetmthjZLukhGG3nZk/HYtAsWL7Qj73SQQqpMzDrcE1o4OpdMy003iam2+WCb4K03yBVOuLm+SmGP+gdMxv01r9ClQJMfV1tO3UQSfDUvUSYuPe961KkyvXZTy4eq1ZXux0dSvxX7d8vfsWt3utA== 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=6UfBSbTttNEq3CmS6i/IUU8habEU1ekDUawFULn11fY=; b=dNVKq2Td6H9UYRntQVRyMGpBzrH74Twm6PYPP36TCTeo4533heOCihLJuV3bfc9u8X5tvTd/XM/d7nKLFBdnkBKtA04it7feEykPbGknbyBVgHQZVg+DT/hu0m0Y2A3NrIQhUB7RiuvpfR1/EqZE888HeJ93W2eMECtW6T1R2PY= Received: from MW3PR05CA0019.namprd05.prod.outlook.com (2603:10b6:303:2b::24) by MN2PR12MB4272.namprd12.prod.outlook.com (2603:10b6:208:1de::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 20:06:22 +0000 Received: from CO1NAM11FT016.eop-nam11.prod.protection.outlook.com (2603:10b6:303:2b:cafe::58) by MW3PR05CA0019.outlook.office365.com (2603:10b6:303:2b::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.5 via Frontend Transport; Wed, 14 Dec 2022 20:06: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 CO1NAM11FT016.mail.protection.outlook.com (10.13.175.141) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:06:21 +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; Wed, 14 Dec 2022 14:06:19 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v7 60/64] KVM: SVM: Sync the GHCB scratch buffer using already mapped ghcb Date: Wed, 14 Dec 2022 13:40:52 -0600 Message-ID: <20221214194056.161492-61-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT016:EE_|MN2PR12MB4272:EE_ X-MS-Office365-Filtering-Correlation-Id: 08bbcafc-89c7-4ff1-6afc-08dade0eabd4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: EtlAn/Lfv6MzDLaW04qs5PfR1NaqneupPNeKRbY86wDfn6eGAh0WqEqxYYt5fQh6u13+Q0rTSD4EuAFDi+R/uDTfnm4qaEwHSn6dTXNT8pIGeSMhEsSzDgyhefKYEsZLQB8/nMszR50JOGB1bY1iCRPd6nEyMajfYA9GmSz0Oxa8wSIhE/cBakOMO6UtRoWS9FOBzB9QTDOF5OuJE2fxZoym1MmFCtpPGfbnrTEc46Rx1DvhidkrifzpsfwUnKeqk048tllTjVHeaf3DzM/1FZVcGhbqYf52dDi7aS9K8zYjjsyCBnGf2FZol0pXg76s/txj41evtxEa0sYLh0Sc04BqklPSF2Uq4iMRKj+xcWtNfjrMFGL3jRpanQK+tcWCAwdWPRKiGfcaUUEx4aziEP3rb1FIl12bt6P8ztj5QVXcIrkcIRamNRapBgmQXeL7aHdngZIt0CRX5SA6d2w1CpQn49XegV0xFcgi5QrpOvX24kz4iJ3Br4Rsc6Qn3c9A0UqnHOBpmCzcbXrm/Jm/VciPObRBVoE/gu6eLnaGoFuxHHbi+e3L0nbWj1FPuT8sHS85XSgeGG/q+VezSpLHjBSx/szYzMfK+7y5eK+tQRU6jDIvXPCjFpL8SIHPWPToxcX+Ljvq1mhrm5LTMtZmBKj2umH8XJyjzoTfECEiGHM6wrVTbtEJ329Hl1sWGD3Zyy+jhlBIkvmqc3/ZP4W4vwits4UD/53RzpF/VZuBLDY= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(376002)(346002)(396003)(136003)(39860400002)(451199015)(40470700004)(46966006)(36840700001)(83380400001)(36756003)(40460700003)(40480700001)(478600001)(86362001)(47076005)(81166007)(82740400003)(26005)(36860700001)(6666004)(356005)(16526019)(186003)(1076003)(336012)(4326008)(2616005)(426003)(44832011)(8676002)(70586007)(7416002)(2906002)(5660300002)(54906003)(6916009)(41300700001)(8936002)(316002)(82310400005)(7406005)(70206006)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:06:21.8361 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 08bbcafc-89c7-4ff1-6afc-08dade0eabd4 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: CO1NAM11FT016.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB4272 X-Stat-Signature: jz9cyofzxrtm6mahcnsm9dii54or8o3q X-Rspam-User: X-Rspamd-Queue-Id: 6422240003 X-Rspamd-Server: rspam06 X-HE-Tag: 1671048385-855297 X-HE-Meta: U2FsdGVkX1+rh9ie0XMR67HblNN+hQ0RvQvtMnWpudJeR0GPcNJSQH9IdYqIi5w+z8vAb/RDVMJhXEGsYig5fbVMw/abxeVCqRxKUWAMHXqdAW/Bb1FyURu7hStuncXfa+P1Gq7nagz3Nf4KCWWaCeRhYapyNrTcomlcPbE0qZWRpSZqSIBHqKQbFCUDBb71gSy1wWRisGZ+el6x7NniDh+GCy+S2bR7sBfA9lh1CzAqKnyM8u0nDTVRoMCrG1prLvMD2wBCBItjl31hPvHWKKoFXrNhkupS5mzSG/hdgMa97JII7V8WRBADgCvpqxoQaCOlAOS5RheJi99jWvVZ2ztRT8yhnlRVRIiRZi0w2k+TxolYXQ49k1KS+WKlvghvoUP3KM36wAk6JzOXLVEBk7q+ZLybo7aB3nFYLd17vmz7bYGw+aIniHzL61WX0kFOXvUe6VbMliRVLwyfyffjY5nDZUwiTAC6AO1IwFGqDXjbglsxzG78900DHPYhENePlmNpqDsk5RH3HNHpUxqy4XLTf+J4hDjc5pbzqVetqSmDFmtWOkKkdhqPwY40THAicIV8VpTvL5aXa3abij3JWj72x1cfL/ycwwDFAH8A/NU/kFRK0SUm1Rn/5qIXc7B4XIjOE5h9LZrMUSKsNsnGSqBNxXeihiL/dcc4eHn9t8sLnVUNZogXjPwyZG1g8UrVAgktkgy6YZtRA0iWkXYUnaTvQ2HP1Ry+ekcBhvttk9a5KJzlVn8E+KtaJmRGToJ4D/FT68BaJfleAhtLJZy5vO3EhCtns5oVXLjjmTVrGs2z4vhPziROGC3yZoEP2xWvNUAWSbISO30o4+Bt+3haBT+3M4YRYq7+ndYRQqBrzjHBvjyKy8U9MA6VX9+rePUOtD4IIRyDng4wYOPPVaZE5fGe7e96yds6KtKG8DmWoXj25sAyojPVRhEf/QHqSbjINyslBCJheuMFCNLgxjq C4+2zvtD gQEoG 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 Using kvm_write_guest() to sync the GHCB scratch buffer can fail due to host mapping being 2M, but RMP being 4K. The page fault handling in do_user_addr_fault() fails to split the 2M page to handle RMP fault due to it being called here in a non-preemptible context. Instead use the already kernel mapped ghcb to sync the scratch buffer when the scratch buffer is contained within the GHCB. Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 29 +++++++++++++++++++++-------- arch/x86/kvm/svm/svm.h | 2 ++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index b0f25ced7bcf..4de952d1d446 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3036,6 +3036,24 @@ static bool sev_es_sync_to_ghcb(struct vcpu_svm *svm) ghcb_set_sw_exit_info_1(ghcb, svm->sev_es.ghcb_sw_exit_info_1); ghcb_set_sw_exit_info_2(ghcb, svm->sev_es.ghcb_sw_exit_info_2); + /* Sync the scratch buffer area. */ + if (svm->sev_es.ghcb_sa_sync) { + if (svm->sev_es.ghcb_sa_contained) { + memcpy(ghcb->shared_buffer + svm->sev_es.ghcb_sa_offset, + svm->sev_es.ghcb_sa, svm->sev_es.ghcb_sa_len); + } else { + int ret; + + ret = kvm_write_guest(svm->vcpu.kvm, + svm->sev_es.ghcb_sa_gpa, + svm->sev_es.ghcb_sa, svm->sev_es.ghcb_sa_len); + if (ret) + pr_warn_ratelimited("unmap_ghcb: kvm_write_guest failed while syncing scratch area, gpa: %llx, ret: %d\n", + svm->sev_es.ghcb_sa_gpa, ret); + } + svm->sev_es.ghcb_sa_sync = false; + } + trace_kvm_vmgexit_exit(svm->vcpu.vcpu_id, ghcb); svm_unmap_ghcb(svm, &map); @@ -3248,14 +3266,6 @@ void sev_es_unmap_ghcb(struct vcpu_svm *svm) if (!svm->sev_es.ghcb_in_use) return; - /* Sync the scratch buffer area. */ - if (svm->sev_es.ghcb_sa_sync) { - kvm_write_guest(svm->vcpu.kvm, - svm->sev_es.ghcb_sa_gpa, - svm->sev_es.ghcb_sa, svm->sev_es.ghcb_sa_len); - svm->sev_es.ghcb_sa_sync = false; - } - sev_es_sync_to_ghcb(svm); svm->sev_es.ghcb_in_use = false; @@ -3321,6 +3331,8 @@ static int setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len) scratch_gpa_beg, scratch_gpa_end); goto e_scratch; } + svm->sev_es.ghcb_sa_contained = true; + svm->sev_es.ghcb_sa_offset = scratch_gpa_beg - ghcb_scratch_beg; } else { /* * The guest memory must be read into a kernel buffer, so @@ -3331,6 +3343,7 @@ static int setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len) len, GHCB_SCRATCH_AREA_LIMIT); goto e_scratch; } + svm->sev_es.ghcb_sa_contained = false; } if (svm->sev_es.ghcb_sa_alloc_len < len) { diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 4692ada13f02..38aa579f6f70 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -217,6 +217,8 @@ struct vcpu_sev_es_state { u64 ghcb_sa_gpa; u32 ghcb_sa_alloc_len; bool ghcb_sa_sync; + bool ghcb_sa_contained; + u32 ghcb_sa_offset; /* * SEV-ES support to hold the sw_exit_info return values to be From patchwork Wed Dec 14 19:40:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073499 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 8DA2FC4332F for ; Wed, 14 Dec 2022 20:07:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 326B78E001B; Wed, 14 Dec 2022 15:07:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2D6E58E0002; Wed, 14 Dec 2022 15:07:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 150F38E001B; Wed, 14 Dec 2022 15:07:09 -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 01F088E0002 for ; Wed, 14 Dec 2022 15:07:09 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id CDE8C120E9E for ; Wed, 14 Dec 2022 20:07:08 +0000 (UTC) X-FDA: 80241995736.07.A1788A1 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by imf24.hostedemail.com (Postfix) with ESMTP id 84CF018000D for ; Wed, 14 Dec 2022 20:07:05 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=kOxijL+y; spf=pass (imf24.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=1671048425; 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=ioS3JfXIHvi53uaP/Fbu2qHHPrFAosADMMkceWONob0=; b=SaIaNR0ocqcWO+ldY2Q3nK2ShOpEmESgIjChclouA558sqjhoNgu8BhI+cmDfMaqp8V+hQ qZBDMwx4AWCe/a2Y5NP4MhxAbnbVyKrh0HFeV0adsifgAKmBStjfKNWkgiB8mUths6iOJC kccqXzvveT9Yc/71KNK0feToPV+S0zw= ARC-Authentication-Results: i=2; imf24.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=kOxijL+y; spf=pass (imf24.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=1671048425; a=rsa-sha256; cv=pass; b=RVrZj9MTNiEocM4ZKey/+LzVBf26DRkL1N4MFiVYa3/ld3jbE+j031ovGAbzH8EHnOPMwc eJasFPhexV8hmZYYXzH61vad4VEh5L7O9zpv0Y0O6ycvAZ8q0sQ0jlNrLl2rBp5nIgTXz4 64KD9VrOgV8KEGXOyBO7yBh+zxhSd8c= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cbQLn+VJnHIjl9WVgBRxskwSZndsejssP2IWZp5LuMB5kB8ncDyoq+F+DbbQTaeoZ5oCyvNrtYHzI7KszPR16LLP6YE2eUvZfOBKCJyJuDoDi9H4z485BvB9GAjyQNVTXAYC1SN+qkBWkPLbRWTppZIDRJY74Jmfz03kc5ZxKuHkG1smu5bCwVxicnvbMojxsrgFrrSAJQcWb21Oq416DPVH0AF7tDHL1y4hRTr7e6WaCJOnXh27Fr1K2nFdgCGKWXY5zjcBoDr3/ZmJ2skMdFaOkPlbNlDDgoMjO+CIJ5bTQALp/ttFcBSojUiMMRhoCv+54EAhBPyYE5xrbU1Cqg== 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=ioS3JfXIHvi53uaP/Fbu2qHHPrFAosADMMkceWONob0=; b=TjH9FYCbaNzDbRX94foNLuCxy72qpM+HbBMjNrnsUawqek4MT8mBUChhM/8YdvT8xnhfCkfiB+unHoEnFgnI1nFK7cAsfVaSxTiiTEWg55CFActPjAKx5vRznIdxnHSeoLncx3yHyS4/CBu6VeEe1OMvOGvWZ3o4ooW03T/inAiaRbJZ2hlyo0yhUx3PyKM1sYWuIzOnZpoMUIAit/SzNa2wTGX4Y02YImVsOj9GsdbA2oMmux95b/oIgRr5SZRZk9sh1WYzHAS4Iik1l2EnSzTGsAoB0m1/NyErNThsIH38NLo1qP+JThgfX5yeCJPaDnWjarPr832QizGlw9FX9A== 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=ioS3JfXIHvi53uaP/Fbu2qHHPrFAosADMMkceWONob0=; b=kOxijL+ysI31uYr3jlgxDE+wAeZ5wxatP97v7RjdEBWcqWb7o0YD3QjYmlIk0WxXLvvAy+z2kYL+lYfE6NeLpbS7nZJBZmwdkoCECmGvolzL9mHfGu8OiunZ5UTrPWMR70VDxxXaUau14u8aH2JdRSBXj7wytBPFX06xD0wmmSE= Received: from MW4PR04CA0083.namprd04.prod.outlook.com (2603:10b6:303:6b::28) by SA0PR12MB4590.namprd12.prod.outlook.com (2603:10b6:806:93::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 20:07:03 +0000 Received: from CO1NAM11FT085.eop-nam11.prod.protection.outlook.com (2603:10b6:303:6b:cafe::2) by MW4PR04CA0083.outlook.office365.com (2603:10b6:303:6b::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:07:03 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CO1NAM11FT085.mail.protection.outlook.com (10.13.174.137) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:07:03 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Wed, 14 Dec 2022 14:07:02 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v7 61/64] KVM: SVM: Make VMSAVE target area memory allocation SNP safe Date: Wed, 14 Dec 2022 13:40:53 -0600 Message-ID: <20221214194056.161492-62-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT085:EE_|SA0PR12MB4590:EE_ X-MS-Office365-Filtering-Correlation-Id: 0cebb3d1-104b-47e1-3ac2-08dade0ec461 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: lP7wMEXnyaSdlL8IKDf50YW5/Nma7+flujihY8HEpHwwOQDVgzIeS5OMSo5R04LDbH26iYBZUiY94TxvRWWCIZwv3Sh5bzqouPkpPnF5VGE6MnAkgAWUSVOoWVHP7d5qiCxT21hS4CyZ8tpGITb/tv54na/eTsaEfII+D/kx+sX+u8MAkda4lauQZzdnLvySsvaSl6YwCeB+NKcfCqmoz70RZcgGiOM2zmHnH6J1acFTnDsSuMDPGMKLYO0qWoiKudCbNr7u8/CuEcdMGV3LQ639wGmmbeK/lk8Oy7akxGh9DS5gZZTBkcsiNrGaVXB+k4+K8TM6TGTeNs3etFtNhrhxP/otSiv/nUG+CUZrEd+ei+Tkijk8o6E5xEh73FHVISMdOgcmKWDUz2p1nbeZceS7vzPa1XiKcT72jB8BtGMsnuKHBiX5r0XsXwC1cUGGL6nYuGtHwC1oZg0sJGUPDBe2suV/JHvhGBjBh2BAT1fzFDYomZ0pGwJkasHTqz6yNCNDleO3lD2n2Q32n931R11Q38SQjgbQk8v0EE4O1Mtfp/Giofb2E2W6FP+LQUgnJ4L7DHC/HHwT0zDexuuZZqNuLLBqe8c/HE2g0C2qoCmf+UHQWmh+u9L0fHva+nHmfkTGSr/z4ackX9fxeVHeuAosqBHsueqIr3x78MveizGeSyNE31YPP8WQp1UxyO2nMiQ/GPLHCDPlay5sDu/q+cyTXMr+9WvxCXj7/WzgxzM= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(376002)(396003)(39860400002)(346002)(136003)(451199015)(40470700004)(46966006)(36840700001)(6666004)(316002)(6916009)(54906003)(478600001)(336012)(2906002)(47076005)(70586007)(186003)(7406005)(70206006)(4326008)(1076003)(8676002)(41300700001)(2616005)(16526019)(426003)(83380400001)(5660300002)(7416002)(44832011)(8936002)(40480700001)(82310400005)(36860700001)(26005)(86362001)(82740400003)(36756003)(40460700003)(81166007)(356005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:07:03.0398 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0cebb3d1-104b-47e1-3ac2-08dade0ec461 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: CO1NAM11FT085.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4590 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 84CF018000D X-Stat-Signature: obropzh5zasn9pruexfyieset9pod1uh X-HE-Tag: 1671048425-969794 X-HE-Meta: U2FsdGVkX18ljJ1xQjUey7ANGUyQMbsvegrnXph4XXmApox5VLO7cf5VbbxermW80uCjaSuYJbe+54qs06kP5MhutJXfTqqbWquATBX+RqaQsbVNyxZWleyqnZ4USh5YwL4bbBvLQmOz37AF6m4tWDbTqiRCxEQ2iqlvbxw88XbubvVWPkAqWANGJzvu63PXX4mBxkSMFh2iNY5DorRI1rUA3Be66ac/Y5Yr8bRvlU+Q31p3Yj/nvEP7JGkTK2flaa0I2D1IZoHusjL69BNnZ3qJFv8T9RfiLvWuFpSrfAmFHyS3cm3AM4o29Qt2Xdf9wBg/MCJR8mTjKmatA4sSUDhQm1N/lyo5PrDc9zkDpqye6Mjwz3VIiTGdDZpHkQO3H7yt7v5g2ny93UtMypH0R9cGmAimCeAezDeadkwfm8+WurvFZmJkiaDIb1wSNnLYma6bKEVE26S8Wsib/+Af/WHa+GDZ6rsG7laDM9BWJB3CfsuYaRCyylZWUTnaaWa3NBsUdB75MlkbYgBvo8r5fSdY/kYAm24kqNGpdTRF4X/ouS7lqlW7SDg3e3db6ZjLGj7KpGUHQPD5W8uEu8T3ibZh9l7BrJrMvH7U1Xj8VNlpJYmErlXXo+7CE3vrLmXTHziNOVIOwBEvokRjp78otgTUJnHkW4fMxKfUa9u1odMXLXMs4DACoYGegRQVs1LWikenBbQGv+gZeX8Z6yj4q+pEMUPdCOCUj2DE5r7H8DD2F7cUyYqPNjzUSJv6cc+hkZGYkV6gAjxbks0S5YDF6y/jsBrXgMHzvl9md/fUlDtEL/hC0SynLEzj34lAq6EbxwemSJKGFXcmRJZ7OBzwbp3iInqJUkue97t70272ibnlUbzx0jBaG9iUojF/s40jNCpbp0pzi3YlvvwZ0KpXltJ4NbZhArY//pEPMUHIuYHjIPbT4lG8hYnXqzbZ5lttBDJpgqVIc3fCjyvvMKa aJ074URv bZ/o6 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 543261c87eb3..1d4a723d5def 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -653,7 +653,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 Wed Dec 14 19:40:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073500 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 2C65FC4167B for ; Wed, 14 Dec 2022 20:07:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AC8D18E001C; Wed, 14 Dec 2022 15:07:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A787D8E0002; Wed, 14 Dec 2022 15:07:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 918C88E001C; Wed, 14 Dec 2022 15:07:31 -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 824768E0002 for ; Wed, 14 Dec 2022 15:07:31 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 5E24B1A01AE for ; Wed, 14 Dec 2022 20:07:31 +0000 (UTC) X-FDA: 80241996702.26.7EE01C1 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2069.outbound.protection.outlook.com [40.107.244.69]) by imf17.hostedemail.com (Postfix) with ESMTP id 74E8040005 for ; Wed, 14 Dec 2022 20:07:27 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=UBimAGV7; spf=pass (imf17.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.69 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=1671048447; 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=WZz9P8qmQHvOs6Uopb7Yc5wo1GarzMrIlTPJE6hd5bM=; b=zevAky579iP8PhcD0ooBQOOUJUcurnDxdszG1n74UNczbobi2zLXW28ki0AbrrpwqP4pyl fJtQrR/eK7nQJWWCfHs3Drcyg3vF3EFz0FP/L1hYbf/SmxsfDwXhgsgyvr9bgBYRX3jmZD F4AwcjKWC7EDQZwh7sW16Ue8TWjHkdw= ARC-Authentication-Results: i=2; imf17.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=UBimAGV7; spf=pass (imf17.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.69 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=1671048447; a=rsa-sha256; cv=pass; b=tVA7Jzcz55XSRvzQ+wlcfjYTPWXjxHBNGEwubF6bhQdh4L26KImbC7vROHmcQX64yYtgYs K15omVeFDNPmBxDy/LjbkNrOZJiSpHgI3qaOAFmGUJ+rvEJ6gPGCU+iViQPjdeGBnaoiNr TRyPOsJ2Hr3M70iltwrrrN+Ls6ATLlg= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KLUU7EeRmMR3p8waKF3UpHxz5/KI9Ya0oUNz50N0e6vA2nUkSXbGEa+i9knTlnJF+qOBizcUGuLKXWSxtoFnwo2VSgAgZMbnlRHy/D5NLDqUFp3ar10O+o73I/c9OG3F2xFmyI8mSOLrzVOqPUwmIl9TaS7uZ0fDdREC6WkmHeh0rZrWXHHcfuZINrtM99XbrJwSdov/sZ4Bo73garvrMkVfbmTUizcqmJAJql6M9RtbIkqTT/XxNtbZZN2UKEKg2JZgtvm4kEdaxNw7a5QIj3i7uOuXRHhVTN2jy3cv/JzBYXTrs+0Q3VrCkdayHlfiifcpxGzgGjJ7CqJl2LX/fg== 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=WZz9P8qmQHvOs6Uopb7Yc5wo1GarzMrIlTPJE6hd5bM=; b=YgRS4mEkHZRUr9O2MSEDShWtWZGW3AC2fkY+qp4cN3GWNrhn3qj4aEb3GNKD8iZBBc4/72xM02HzAUmoJkII3i06bgR97y9mhYzGlPWludJJgxOVzAAf/xCCN4hgQydsjBqTX7PsO2QQVS2BLjsO5hW+yID9pW2J0DHwqtCaJtQQNNg3T3nn/reJR9X80dGCCtlBpWIkB2yqM5Repq9ERBP5kdhuzlH0mQOwlgM1o9T6saGst6jlTBb4RJKnwG3kjU7BJ68YPELs/PqXKyCTDXAOgfDK2k1zSqrOytpWGDbbWAkwhSHUcvFnm2RKsNZ0bqJqmGUatWip+94iqqnfig== 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=WZz9P8qmQHvOs6Uopb7Yc5wo1GarzMrIlTPJE6hd5bM=; b=UBimAGV7OuAJFuc1Y9Iin42E4mtyM2xLqHfMit3FVdFSpJs5/hqhXpQP3BKZMGTB/dnLIiSTedinoqnEWeXhjg0z2jup/53pCcXaBXWImeCOKaFGgtRir/04yLmyskavhQbg2T0NwUjql6l4qP+ayeHY1df9VJ7NTKD7qAp2+Zo= Received: from MW4PR03CA0090.namprd03.prod.outlook.com (2603:10b6:303:b6::35) by SA3PR12MB7973.namprd12.prod.outlook.com (2603:10b6:806:305::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 20:07:24 +0000 Received: from CO1NAM11FT036.eop-nam11.prod.protection.outlook.com (2603:10b6:303:b6:cafe::93) by MW4PR03CA0090.outlook.office365.com (2603:10b6:303:b6::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:07:24 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by 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.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:07:24 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Wed, 14 Dec 2022 14:07:23 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Dionna Glaze , Tom Lendacky Subject: [PATCH RFC v7 62/64] x86/sev: Add KVM commands for instance certs Date: Wed, 14 Dec 2022 13:40:54 -0600 Message-ID: <20221214194056.161492-63-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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_|SA3PR12MB7973:EE_ X-MS-Office365-Filtering-Correlation-Id: 1d01ac9e-efd6-4ea7-2f3a-08dade0ed109 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: g09cJr1qUVgRLbcdJs2W3PpjccxXzB4XIsyVh7NmjE+dI3ngIJJMjoi0Ya5/MUdDs6tlhxtpG5GnoqkZt9kt9P7+aq4yPshHVWFTjxpQIKyZ/FrsJIkt7MjkNyRV0RzbPkspFyNwqQLLNMEPtUOnDQuLrOetHQbEi6Jp6eFgnBC835FcTynipKLs3OavKSrdR6ZedR/IFj9g+pB2R9gTzkqb9BKyY259pSr6dk1aju2YfOL7vphB0CjIk1Ez72jIvPvlUZ2GzF62b9aQZTrbVecyc8wl0Rpp8pIjpMsJtpdoRLqLld1iD0Me/2q/5cDY2Dq7Ogw0IUIdFxTIK1/sDr1s/CI5SS2dp+V5XVIHsq6rR/xRp1LHenQEp8UKQ000Q0Er3WycD1BnJ91nzyHrxOdCmSHLKNIZPb2Mcn5+cF2JzalUGbVabcnDfapcqpyxltyokRYT2gzr15qtv+2L2sLMb7oDFQNFMqDl5O4GiYlDVUOqJ2Fz5uK6FCNGLpIavYLY48itI/1uZHdu0tzNgJNN2FjLOokLOJDFmED62CG/I1WT2H1iw1z574SJjCCJJKWzsZOBppoS705iHHrf9vkL27Qe4PRej2LpAJxQUZ+AIbuDxlth6IbnaJaWG1btsnGZo9Dk/RL6iHxh4aU3xxlAPepz/DOcSRive9bM7OszOusLUaXPH+nK64l6uBY9cdf7n3ucLcEYG1Dqn3DTnuEbRPMM8Wi69knYtQLEigM= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(39860400002)(346002)(396003)(376002)(136003)(451199015)(40470700004)(36840700001)(46966006)(7406005)(8936002)(41300700001)(4326008)(5660300002)(70586007)(44832011)(70206006)(8676002)(7416002)(36756003)(16526019)(1076003)(478600001)(54906003)(6916009)(2906002)(316002)(40460700003)(186003)(336012)(86362001)(2616005)(6666004)(426003)(26005)(40480700001)(83380400001)(82740400003)(81166007)(47076005)(356005)(82310400005)(36860700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:07:24.2576 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1d01ac9e-efd6-4ea7-2f3a-08dade0ed109 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: SA3PR12MB7973 X-Rspamd-Queue-Id: 74E8040005 X-Stat-Signature: o3biapp5hfwfcnf7bymgu4ukoxfx6c1s X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1671048447-954941 X-HE-Meta: U2FsdGVkX1+/NPRH5tUtb//FbyOOqU0tG+MNhF3HRVDtY61BNu4MZou2xpV0e+rem3QlzlauUwhwlZwUOPYD7rkbwUtfLjAqNmWBHYvNhz9xg0eVU+MR9ACFEMg3EqH/EYnL2sUTSkWoq71HmiHssui4gxM5IfTG55oFp6pVuGGuhpVrICMmMiWR/bUULXmWDWJMWsvxguaV6opytB94UqaKzjfdBWxtZoDcgBA6/GABvb/EjPdrstYeYvZNQb7BIuKITXjrRjg6DB4vS3A9JhWKcHvZKmsK8pYz5H+MSMIBrmzlJZgb8b3sp2Xx3ABTgpITTXMLvw6ulIFLgXWBkZO7c2kcl4W7ZqIZu7TXr2wVeFtgzBLiFh2R2zSMJqZ8/9IOr3T6DiYoPfY+IHkaTdbHlKJEQzlrpXfK9KGn7SNAfTmKw9EXg4l1H+943Vf+Zhom/fL3aFI3Il+GPXYAKTniC4D13oPdq1SeIahUMGdfB/NLA0z80Yt7QdGX1qdUCv5OGVkbiqVa4e8tIxPv2TOhXjVdD7aJ2qFBnGrf3VxDk8TrzPSDJWZUwgwa1GBAMihHTPOlciRFAqJwVvcFJW1Nn+nzm0ziUA/r9p3QvxWWJOi6Z1ZRTHnPWbgPQQ+BEbfS2RgXty7KQBx+2lMQCo2UToAWXhX8pOTJHyqUy6BWwX3KDI3w6fjpg7vOyiFVmZmQaj0BEYLgN/PxROwIJPexFtuNVvqYMannc3WAhS1hzWMRsUPi2pW3kiZYAf+RvCqSg5p6gX1oenqXyfgfbEvJlsfU7wfWYDr9HfQxECU+vXngBYpou50BbaS6liLao3ny4QmgF+Rr2WfVa3XVBmSj+jNmiTH0LDAjvVXtrG441IwJEfPAkU37v5Jc/Jgj0aGs1hQEU4e4pWjhHeVbYO34CiJALG9TKK2b/H+tt+ds9B8b+LHsWl+DqPs9QpMwYi3L8Xqz4JRdPbS0ghS 19HQchzO 5vUpN 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. This patch adds two new KVM ioctl commands: 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. We 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 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 4de952d1d446..d0e58cffd1ed 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2081,6 +2081,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; @@ -2364,6 +2365,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; @@ -2463,6 +2544,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; @@ -3575,8 +3662,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 38aa579f6f70..8d1ba66860a4 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -102,6 +102,7 @@ struct kvm_sev_info { void *snp_context; /* SNP guest context page */ spinlock_t psc_lock; void *snp_certs_data; + unsigned int snp_certs_len; /* Size of instance override for certs */ struct mutex guest_req_lock; u64 sev_features; /* Features set at VMSA creation */ diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index a1e6624540f3..970a9de0ed20 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 61b1e26ced01..48bcc59cf86b 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1949,6 +1949,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, }; @@ -2096,6 +2098,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 Wed Dec 14 19:40:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073501 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 DBB18C4332F for ; Wed, 14 Dec 2022 20:07:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7BA2B8E001D; Wed, 14 Dec 2022 15:07:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 76AC68E0002; Wed, 14 Dec 2022 15:07:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5E4E38E001D; Wed, 14 Dec 2022 15:07: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 4B09F8E0002 for ; Wed, 14 Dec 2022 15:07:51 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 1C7C3120F90 for ; Wed, 14 Dec 2022 20:07:51 +0000 (UTC) X-FDA: 80241997542.14.AFC4592 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2060.outbound.protection.outlook.com [40.107.237.60]) by imf20.hostedemail.com (Postfix) with ESMTP id 55B151C001D for ; Wed, 14 Dec 2022 20:07:48 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=j5C3rc3k; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf20.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.60 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=1671048468; 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=MQUMBExNxhs8U7TItiZxvWkZDCRmiRb0tzOnKzQnm4k=; b=vhaKXp8sQfAnVAsPTgvLAHOcAFLAVfRFYBIoGQ2m+P7u6FBa69zYAtCloxdkuCgVhblvdi jf7f7BhMhPHK95MO2PsXT01EVL+4d0u+HEcqhyAreIcQ4CHZGErOxgCsunk5s2p5QS3Nxp xIH5xZaQO+DIiCTMnBbY9kc9Evj6o1U= ARC-Authentication-Results: i=2; imf20.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=j5C3rc3k; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf20.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.60 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=1671048468; a=rsa-sha256; cv=pass; b=fGYBcs1wmAOGc0eHFOJV0askLH9ukgLQVhm4XwV5OQm/TSqoHd855o8n/lA264AuDBWDKJ XzvJB1P2N6Z8oVDrKUbcaHe6EvNXxHHuqcZmDctDsHEBYQ4jKaVSUrf/0IM3FaG63k3ecM dVD42bC1okefkmLOCmtIODePotohWds= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PffBtNm14sTdFQJm5LQhkHiV4jLou0ajrCU3GRbrlYityg1zooHioCPnxUcUQgbph71MgdRpyPmCLc1NFa8p/kYubAAPCXS1lcRFh+Jb0TIwWfi7ncwTtWc5Ff1JgeaSiV0ZI2//3ekktpN4i1OG8HdIYtd9kjfiiAVo91vV2pioWArF92LpBymYVE5KTiT8OqiNK+wGWBm+d5xHCXS0IXY/b71LGdVHmrZ2GwBimql6q4dOy+KQA5TCIuvrT7zeMOsWGQjQUsHXF6EO0nzFdoUGWo30Tzr7hN8QzR1ce3s4htd1sHRajk+e3i178n14SHBGn/Pp3/XG4VlpJycpBw== 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=MQUMBExNxhs8U7TItiZxvWkZDCRmiRb0tzOnKzQnm4k=; b=bRpQd2YOWjl137T+SBBTODR5Lq1kCp5BpSEyL4btoHBLGWKqT/mHk8q81Yy/YHg3Ds/gdNReaIPdg64sULd4ubEGhY7bt1JrQomf7eU6IHSjgvl25M7Bbm6vsU4VHfEgaGqcpUhQBxauTVt1TmNS3FyW/Unnd+nceOHOR/hV2pwq3GckX0cNb78TXmWTIdLXQvuHu+XZUM9/ID9X1D8q4tC8jjLhvv3Lf6Eawh+ihHb6Kta/Lv0n2hELkMUpAxO7Fg30Ppw80gWycEmQRn2fD0Y6E3b+1ObpuQhhZZ0YApdN2y6q9Ge4kjE0M9YJ+s+cU0poxoit6HAqh8UXfOEh7A== 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=MQUMBExNxhs8U7TItiZxvWkZDCRmiRb0tzOnKzQnm4k=; b=j5C3rc3kx24ZrHkmWWJC+5eGKhHob3uCIQIbSBGLVa5OrFhV64+hfPhWcEwjFk/M396jEInScpIOeiFPeHiEujOp0/TpwYKPGWiJN2nXJpltZYag2L5gjfChOCsCmJA8AIaAZ+Ph7er/uMrp+nXt3JkeXM67G7W2gHy5nGMAMYw= Received: from MW4PR04CA0386.namprd04.prod.outlook.com (2603:10b6:303:81::31) by CH2PR12MB4311.namprd12.prod.outlook.com (2603:10b6:610:a8::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 20:07:45 +0000 Received: from CO1NAM11FT030.eop-nam11.prod.protection.outlook.com (2603:10b6:303:81:cafe::af) by MW4PR04CA0386.outlook.office365.com (2603:10b6:303:81::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:07:45 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CO1NAM11FT030.mail.protection.outlook.com (10.13.174.125) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:07:45 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Wed, 14 Dec 2022 14:07:44 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Dionna Glaze , Thomas Lendacky Subject: [PATCH RFC v7 63/64] x86/sev: Document KVM_SEV_SNP_{G,S}ET_CERTS Date: Wed, 14 Dec 2022 13:40:55 -0600 Message-ID: <20221214194056.161492-64-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CO1NAM11FT030:EE_|CH2PR12MB4311:EE_ X-MS-Office365-Filtering-Correlation-Id: 057bc176-fb98-4e50-7694-08dade0edd8e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: DSGd9aBr3Ge2D0VzQnxViKZf0aa9KRxd6mZo0UQbPf+XscEoTQ4t2UJLD92EWj62gVTphF616ESobRSl4U2//dmzZFhiDG5jlYjYN04NvboCJ99ryndKo6mT/lGA67Ow+criYRaCP060x5nFa7Z+ikBM5SnEFyZv4s+mdMPUMInQIesO6OQgaQc8jWDHiOG9on/2wka3B+rN1pfkqF2GHGj86hNu4XEBDj0tqsRRlV4x1sR6ZvuJ+n1l2J5nyJtAHLaZbBoOfnkq4BXFXWkNqP/g7PcfRCqlpT6HOUWZLWCMt2bJoWPt2IOJxFdLjMKYbcJYsbI3OORlI/ZCDLvWN9p9ZtyZaaTOrKZHyCvdkYlUm4jn5uyrrS6qjtD5SlnHP9SWcAXmw+UiYOEzq+Yyl4jU4O/6UUFb3fIvq5iMpmLrxTeX5On8mPa/xTvOZynsm/pD0cjH881x9fJTX3dWYiD1+IDdy7wesgFO4HwTFcUnqX3vMNR5toOEzIn3hiWUtT/Wh89GcT6f+Ha8BpiFPQl0Mgrau4MbVil/fqjp3atcO6puSHZs6G/pu/Am+H0SVNt71L/DFttncbDERM9T5kCVI0YvFCOX/JpXrp71XkazDAcZq28RHhldvW7rvXMaD8Kk13U9KyY1cBY8VZn+veKgrKWVI76soeQ89XInLbu46j25DpIWP2cCZQu7Su0rmDlNFmi1+ra2XhP8ae0qLSrH+tnAYJEW8QMXviS7kjc= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(136003)(396003)(376002)(39860400002)(346002)(451199015)(40470700004)(46966006)(36840700001)(36756003)(36860700001)(4326008)(70206006)(1076003)(40460700003)(70586007)(8676002)(16526019)(47076005)(2616005)(7406005)(8936002)(41300700001)(426003)(83380400001)(336012)(5660300002)(316002)(54906003)(6916009)(478600001)(186003)(26005)(82310400005)(86362001)(6666004)(7416002)(82740400003)(2906002)(81166007)(356005)(40480700001)(44832011)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:07:45.2637 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 057bc176-fb98-4e50-7694-08dade0edd8e 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: CO1NAM11FT030.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4311 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 55B151C001D X-Stat-Signature: 97did3cscwg71eccwwdtrck4hgjysdp1 X-HE-Tag: 1671048468-774175 X-HE-Meta: U2FsdGVkX19BrI6WWAiPfHAYpPrSOobNGkKRbe7yQkArbJZyupmrSRVNVOlCvoPQrUvv2I2VgciVDF0djkowzu+O5cQ+ItdQNLrchLXFKrGfjXHCvEVjrsU+DF1KnJtfbY3w7xJJbW1f4Ig7QiuqKzKJEHMzE2BX+X9faC2PAR3w35lschzp1+sc5AspRQSC9a66Ve07Upi3AdOQu69CqAS0GPxO2IY+xvIsENj4tTyH3RqtITBkIOCIJPq+lDdR2Hd2FBqhJQ3lY2Qzi7UwExP4BoKtPcEI+j56ebUzwnXqgbWJT5u8V8+k8KNBDedGKMQJ03KRrNFy02YU24CW/uJhzt5tU+rZ6bjWXNGtvHKRGRr7VatmTrb4n5x01vvhHHU7U1mwJpd/fndYrphTOIK6N+qmtDrthDdiOkWcNQDnkV/KggkBXT5Uvg3pRDZ0NRi0+iihnXDJc8a5EglRFuUnHT46VWPQFQe1oPEXy6FdK8CPQWqBu8ZAIRod4kr1ZYM4aw/V8FWu/57A7lK4gtbyjC8EN83NzkfGVH0mhBkW8DDw2zzvndc3R1muf8DbKLDCw6t4wFnK62s4K6wM3BxyA3Y+C9STmFwVeIvQZ4uGGpbu5iUpYAmNFAns0SALeLw9q7Czb3FmOmpE1nPShVJrzdUp82K+GM48SDsJdEdUJK/dn1N30Kw41krqIH3g1FuKm6F6EDhvB3Heq2lwzDI+GVMPauKU003nmiDoCYNiR+zyhrsBfuvxpKkIUVrUREQY4+gqWmTxdgqXM38moipq521Ohply8f+8ZhYyN6KjJpsMr0FSJ3FWs7hugPeWvO7pVsSuUn4ffGWr6eHcuDZKQZHGURwd8RRDperys4H3UV+poX/FbSrA9rv5rdK3qN1j7ZAmrTA7G9qku6lGrroUCbomFCCPtX10E/Sw16MBaXSqX6z+8mqzmTebXy780JSIIEBAGWqEbuUr8C3 mZ6uDU2z ukZiO948v1qG5pX3ACRHzg9Acd858k4Eu00RBjXYDQHRbhQrI9hO8qbPICA== 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 e4b42aaab1de..3cfe7b7cef61 100644 --- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst @@ -536,6 +536,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 Wed Dec 14 19:40:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073502 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 952CEC4332F for ; Wed, 14 Dec 2022 20:08:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 374F48E001E; Wed, 14 Dec 2022 15:08:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 34C2C8E0002; Wed, 14 Dec 2022 15:08:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1C5718E001E; Wed, 14 Dec 2022 15:08:12 -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 0C17B8E0002 for ; Wed, 14 Dec 2022 15:08:12 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id E4085AB618 for ; Wed, 14 Dec 2022 20:08:11 +0000 (UTC) X-FDA: 80241998382.09.0AF1768 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2046.outbound.protection.outlook.com [40.107.244.46]) by imf12.hostedemail.com (Postfix) with ESMTP id 1430340005 for ; Wed, 14 Dec 2022 20:08:08 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="L1O/IH/I"; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf12.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.46 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=1671048489; a=rsa-sha256; cv=pass; b=okSB1kbTvHWf6ofab+06Di6+cgevwQGJ5YKC5et+g3KWKJvC8yuBFdMAaNaq09WpH1XT0e fgpHIZpJp/eBoI+Ud3CA2afWH5zt3BSBrp5GyZkPmnjsHBd8QVbYxaWTb3IhcORBH62Bbc fNxXQOVI22neoOBdk5AjU6TQItzNd3M= ARC-Authentication-Results: i=2; imf12.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="L1O/IH/I"; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf12.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.46 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=1671048489; 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=usdDmXcJMmcLw3KYUul5HJCtUTgF2Nhc7TpOXVijSeg=; b=xgnBMwzcIDkHo7bbzQ4SGtUOPSSRHUbDIbX1OZI/sBF3CEEhW1jPLv/+A8fDgnp/GNVzlO 5datvyhP3XslxCKa/Z6zi7CFatVusl0t1tsmC+Cgy/1ToZINONZWw5sWAE9oZ+KfqXmJU9 9vFWvuQD3rUqO6Wl/VM+rd8gvjkE2ho= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aqAwigH2I0kR7P7hkeoHLS7Y/nzwmn4vvLNm8PkW8Ta8sjEA1c4ol2YBQ0Z3MgyzvN2YbjClEDJo3ulTqG9xJVclTXirqm1i5+r0r5bupkmlLlA9zhuQ+8/vrGTX0Skz1kZSRrI/1wchEmy95s5JgQhypVWypbZ8l4AoVQHx6SMmGsXMq5Y6WOFm3alNptLSMTioEIQWeET7BAYr7jZ/RMirg6Zj8iWMTGUX1E0C3hnL83vsx0usRJsdL4lnCzFlPREU/NQOVaYtz8xDbfnI3btOxrlhAiYzCOj/ShWDJkWnBbgxMq8Ojrsj90xWuoZ566AKlkvS1Gn2lz+1odCnbQ== 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=usdDmXcJMmcLw3KYUul5HJCtUTgF2Nhc7TpOXVijSeg=; b=hmhg9KeHlOk4HewIOT0+2oJCX11LVaXoChqKH0Twu0Y8OXFcaO3lGuVLSUgF4yVt7clSob6Tr8QITG3NbSWPkAm9Ut+OnuM1Zs4GLX2cPYdKERg0RxU2d0UpgUD9QHW0qi4frgd8MmZPIh/SM4e1PIrWTAn4ihZfZ7TCI9vd1Y9gBkKg0G1J8wDh7svJCcQ6oS+Xrku6HcC7HSK2hflletEJ9xtpAAIIK0RLB6Q1Oq+PAuXCYFJuxYBIYiuIGGwOfb3Y3e27S1MXuysFTjQ3+Gfjy77oE5+xK9gnjSIGpkS93/gDt9QR29wykrzVWtgdlLbgrRhk0V7TQtRz7xYD9w== 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=usdDmXcJMmcLw3KYUul5HJCtUTgF2Nhc7TpOXVijSeg=; b=L1O/IH/Ix7vxJnbehYDLQ9liOghjkQD1Az218hcQCYXP+X+0NwiJ/my3/WShMp8h8B8JQhxqnUJe2yjy+kKlEd4GS4MP6jT4rnbChN2iw9bMS5vVYDDnuHKB7RVhMpIhcNw4vO2VjLxuqvyhPk9D9q83PX7EoZiHF0gE8A1oY4w= Received: from MW4PR03CA0103.namprd03.prod.outlook.com (2603:10b6:303:b7::18) by MW4PR12MB7285.namprd12.prod.outlook.com (2603:10b6:303:22e::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11; Wed, 14 Dec 2022 20:08:06 +0000 Received: from CO1NAM11FT035.eop-nam11.prod.protection.outlook.com (2603:10b6:303:b7:cafe::64) by MW4PR03CA0103.outlook.office365.com (2603:10b6:303:b7::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:08:06 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by 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.5924.11 via Frontend Transport; Wed, 14 Dec 2022 20:08: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; Wed, 14 Dec 2022 14:08:05 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v7 64/64] iommu/amd: Add IOMMU_SNP_SHUTDOWN support Date: Wed, 14 Dec 2022 13:40:56 -0600 Message-ID: <20221214194056.161492-65-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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_|MW4PR12MB7285:EE_ X-MS-Office365-Filtering-Correlation-Id: 7dce4a32-8379-4710-13b0-08dade0ee9cf X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: b2+L6GruvxCW4T89GjBwMM36vRjU5Su1Jjgi8uIGi06/wCUych8U+VvxqDLNMgwo54ExX8uOMubAWwuGWY/Fa0CCS5wt4Lc6k5UisUj8H4Idil8lmVmHzo0RoS2kAsWZL+u703FAuCo9zAEkxZVr5H34iVqLOs8smmfiTFD82PJHhwo0P+Q+PbcC9XB4NqadC8HnU1WBMvK9BtG2gehPBzIz5p/E2mC7hujrtn363ul1Fcrtu7qg40Zbjc+7iI2BFFXiQVkAALHouHg13JvqGgJcduB/EwVma5lY9WtU8dsDfGPzduIOsR9XFhF+Wz0WEPG85ksYQjBFIee8Fi35k66sO6C/b+q84kKFSUdo3Ka42G59z+ErTaNgOpAXF9s7EFdzXtCsznR2gGbN0/0laId6G6vVygpaYi35LIrJSz16iUtcOwbV25CwEqeECsLVc1pRR6xPATarK7ilN8DQ3duFHwysIzN1i4xM/B0Fa/dxajZ78RLMIHZrlPqjEd3nhe26GzG0aAiatwd0nzzgr5ZoNVMR7Qvm6/iY/MyDoxATqA/HsdszfAUBBAgO7GnF6bugR+ZwR3EN7sbpPbDnWxenySjjOqT+WsCfEZwy+9CC3EX0BH0AbncAufskOLgqf1S/309vb2EQZa1EUv4Xmp7qriBIlXJkDAWk8/ZsCncqly7I46KPXLr3nbKh6md2dcpz267l3TTsrhhk+fPwB670LdE0aMVvQjDf2MM+4W0= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(396003)(39860400002)(136003)(376002)(346002)(451199015)(46966006)(40470700004)(36840700001)(426003)(47076005)(1076003)(8936002)(336012)(36756003)(8676002)(316002)(6916009)(7406005)(2616005)(4326008)(70586007)(70206006)(41300700001)(36860700001)(81166007)(40480700001)(54906003)(82740400003)(7416002)(83380400001)(86362001)(5660300002)(356005)(44832011)(186003)(2906002)(26005)(40460700003)(16526019)(478600001)(82310400005)(6666004)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:08:05.8354 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7dce4a32-8379-4710-13b0-08dade0ee9cf 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: MW4PR12MB7285 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 1430340005 X-Stat-Signature: 8zijsy3jeg6ikt37jwuzhs5uqz3ihj6m X-HE-Tag: 1671048488-695570 X-HE-Meta: U2FsdGVkX1/vOrgw7sTE/lUW4rlDINWOOYf7V9eRdqcK7g1W7ZDZBfmr+PrFuvvPuCx1Y2dhH32Ci/kbP8rL/H+oNMZeVRreWV8XUxKxqolUkf4GLaKT0BeCNN2w4jojTOYn8wR02ws8etH5M7J6gUqOkDNXlwiZ6BpLAEQ6Ki0wcYSxNOhga/msE/xyaTGaCaCKOrWw6n4kYwakYAIhYCHmFX8hn33gTHD8HTEPLySEKyLsVanj+6Rsy+Alp/PQkfvNQHJ2+IN9KURhMCz1P34CzQF6+fh02QUy3O9rGdEI5wES69v5NDsUXoebjNANVCm8bNQVKzWhs4Hbz3wWlSM2wQPLUZzc5njBu41eqlW+64FPfGwaD7hzqlS6paVwoNG3dFyXwCLudLBgLAGIgoMXqK9/JtVyWTs5eiBFXbjIVhO1xiDZEs5rnSV1EEaJg7EozCXtZXhwJ3NZJQ1FKl7dvhEIhugoqtoCYssZGyRldE9tI/uVRRU/zz2P5uw0wVCq4rJz3A1vl+WT0CWXmODWA282kRH/5oEgfrv1278tUka9Pd99aFbWJxD3M14R/0iH1aWo4U97pvQGoZI3Gv3/9eXUfmgpQLsN8DmG2JRldfy4EhHRRTPHPYOmpO/DFwQ606t/xebmESn82SRSmnMNozfayAv9HcLx/mIz1YlVIGY5PllzIjbw6BBnD3ZvF2yQQfrsLvX5GqiFIvVJ0XMQWn+/mJY0FCljcfguOBJovOQ1WeXTaC43bpKRd6WUgqYAUiqBiYndQ6YCqPBjtk3BGxFtGFhnVTxEvpIBA2PUrYFwxyKtotOgv9aq7ojuqfBQ3/KTq+tAzpzrHm+tmLW5BCMQAUl/2qlAEq6RhTXc2GNXEX5YrrlBsh8d4XuBJTFDPPl++raFo9yMsGZiLzvROKT9TRf+yF/wAHk2J7abHQKqY7QfB7VQTJaDO80/CN7ytGsQsEcKJfImgg6 CpwbPkBk ZwYVuRhezdKixdX9OGUM0DQAKkbqoAtA4PFP1FFodQovvdlx1mlhZQsAqTw== 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 052190bdb8a6..6c4fdcaed72b 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 */