From patchwork Sat Dec 30 16:19:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13506855 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 62F0DC46CD2 for ; Sat, 30 Dec 2023 16:20:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BEDAE6B0146; Sat, 30 Dec 2023 11:20:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B9D766B0147; Sat, 30 Dec 2023 11:20:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A17936B0148; Sat, 30 Dec 2023 11:20:32 -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 8E9936B0146 for ; Sat, 30 Dec 2023 11:20:32 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 42B0A1A0A25 for ; Sat, 30 Dec 2023 16:20:32 +0000 (UTC) X-FDA: 81623997504.05.187BA0D Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2075.outbound.protection.outlook.com [40.107.95.75]) by imf11.hostedemail.com (Postfix) with ESMTP id 563E14000C for ; Sat, 30 Dec 2023 16:20:28 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Qi4IGBd0; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf11.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.95.75 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1703953228; 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=jT21oM7hF8GJ7QiA3VH/SoVsWv4M1q1DV5y907a+zac=; b=258EXsd9rLuAvQkPUi+Hd69Z1hfsnJUzWe4MMdvU/wNMB7YwJppqp3InnGdXdbllrAkXRg Mz0PuCxAOBUb7LmxvrlTS1Af+J45strIgmuFBfTNuPTgANFQgFxcRm8GUAQxlP0CM3CTjJ O/LkHNovADMsWRuZa0l4bMY3hIMLYTM= ARC-Authentication-Results: i=2; imf11.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Qi4IGBd0; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf11.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.95.75 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1703953228; a=rsa-sha256; cv=pass; b=Zto2gf9EVMI2laRqJ+JedJMwcwbBiLMlQvWb1+HIiph8Sd9RjHKxcOraZtpzKyVbXMEfCD sLxye+12A/CxfrwykcuX8WveIOIAtNnx2MNq/03x3dm5eQgbqoU+QJA8D5RpsTIRwIYjDj vJHrHfkPtlA9Ee/VIG0qgjejfSW1xAk= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BP/uX1FjkURr7t6F9B6rGTs+jDPvdYiAyizrsiebZQKGtHd9t/jtsEyT7z+BoJ7/cGUpYBiKP3p0/qtZ+S32ayLFJ5kxV1i2QhQPwN/cZGzsCI8QhPAFgZzEuI4FAGi3KnzSMUzNuMeuj6VVy0FtnRrsnYXRnL7hBApdmmwYWgFHq3UlD0VBQ34jyMbxYDjV0A6aKQhKOy8Ad7XqXxH/jbCXD2NpqyzHeXDP/u1z/PGXQNzeynT7qMqEVKS37l8gI7oYb5bjcpda8+aHrIyMsiXxrRH19AWRa9taDmO5ps2JKHdngXdQikB5ZVGnibgGkF5x+6VFbptSR8+NJn2s/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=jT21oM7hF8GJ7QiA3VH/SoVsWv4M1q1DV5y907a+zac=; b=SvjNB+mIE825dobN0JAAsQltliKM1Aj1av9S3jWnJHeDx4XaPn3/ev4+VVgPjswWjj5bxK6EMqdbHTbJhxMMF9pWRZuar4m8halEZkvv6Mdp2k4SHdq5Evap0H8JMZMN1RwMr26s599b2AEI2yKGtS9Td+sIiCGM6hA8IpcJAxs/Td3ETsGNiTNhwDnbjN6d2M6EsN06pZVJoAfhj9u+GVuTvnvObWMnsQs4us9MxUOWrV6ExTnmANRHodWWYN7Eu675MT1EzWqaqpQMKjH5higqHehEl258KFnSpkEwuhsY4w6NbfoqGZXONF95774SIauIDvHl2v1bXFeRHo5jNg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=jT21oM7hF8GJ7QiA3VH/SoVsWv4M1q1DV5y907a+zac=; b=Qi4IGBd0WpQjZU24ieP1V/z14eTUkMDpByeiPzGlgrt4AHCzlc//SKIK/W76WfrIkey6MqNpJWpAChX4h5GMEexUWIA5xE+ySeEjUWtMQ6yEQ0Gb2EDIkncz+COFdPgspYxIxnNce5Y7HYt0dIjQ7l+019pIE1UkMCmk2o6pzMM= Received: from DM6PR03CA0073.namprd03.prod.outlook.com (2603:10b6:5:333::6) by MW4PR12MB6951.namprd12.prod.outlook.com (2603:10b6:303:209::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7135.22; Sat, 30 Dec 2023 16:20:23 +0000 Received: from DS3PEPF000099DC.namprd04.prod.outlook.com (2603:10b6:5:333:cafe::63) by DM6PR03CA0073.outlook.office365.com (2603:10b6:5:333::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7113.27 via Frontend Transport; Sat, 30 Dec 2023 16:20: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 DS3PEPF000099DC.mail.protection.outlook.com (10.167.17.198) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7159.9 via Frontend Transport; Sat, 30 Dec 2023 16:20: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.2507.34; Sat, 30 Dec 2023 10:20:22 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH v1 06/26] x86/sev: Add RMP entry lookup helpers Date: Sat, 30 Dec 2023 10:19:34 -0600 Message-ID: <20231230161954.569267-7-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231230161954.569267-1-michael.roth@amd.com> References: <20231230161954.569267-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: DS3PEPF000099DC:EE_|MW4PR12MB6951:EE_ X-MS-Office365-Filtering-Correlation-Id: e71555b1-0b8e-4803-7a0f-08dc09533987 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: HjtvCXtXmA2OCtgiLfWFCJe8EwEruu/0Lb/QuZSRk6kLZEIO4qSnIVgPSxGDzhj+VaVPniBvjrSvXYhtBRBlXdfOEQgzSjHwNg40Ml3qv+eTVvtif71kQ6rntz4BWKZ35fHHbEwn7PZlpOWDClOjgtftJuFoiVg8ZiFeNSwjhtEf0r6yuDkX2LPYeiCO1saLK2K7qmbEgkgxalOR3SCCa8Q2x9vTQCXdfdo67KYYiSIKOj3UBedNA4dxqEpaTTAadHdy0iokBxUwNdLAc3kEj7/GrUMTZgsAeYEkXDE7HYd0D9RBeRaCfHszxDCFUO8YEoWEvO+c/kN/3I7SKRjcCWCYFSUN6ytgqXJ+MqDFKzRja2pHgffc/aRfaTY1QJHdLPVqzLkqnaTDLJrKOlX9ImBeLlOr24mh+r5irqSWSgqnTHbClufbhfwx6jOfLcixh279/yhlXuv2jwIZBwuy6jYxdxilgXTQTJUvf96YEAonMCbCBD5v3z+3/AAEWVndw87qbDach36TAvlAZLILKdMTDRKu/eMjMyPQWdjojM8MtKu4EIHJ/1lmCkmsfQQPSa9XO5z0kAdOWEqsQtKclkuzRzFn4DTxLneM5IVXfjV3Qqu/2mUwjRB5OYswmenZGXW/XN4Ervcu0UheIA93FC3YxFLBlxR3B+YggerOK6WE2LM1gkpO8pGwHGy9wqLSZ1lTk/pAhBy2r6M7z3gdGLTpEVRMv8QvyeAkvjys5h7d4juQNB7d8nP6ZUyfsyT0vcoYXSx5ss3nMGKvMfjENRmuvw2rJ/MB9YGo8Hz2/GA= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(136003)(39860400002)(396003)(376002)(346002)(230922051799003)(82310400011)(1800799012)(186009)(451199024)(64100799003)(46966006)(36840700001)(40470700004)(26005)(1076003)(54906003)(70586007)(70206006)(40460700003)(6916009)(40480700001)(2616005)(6666004)(83380400001)(4326008)(8676002)(8936002)(36756003)(316002)(336012)(426003)(478600001)(16526019)(44832011)(7416002)(7406005)(5660300002)(47076005)(86362001)(356005)(2906002)(81166007)(36860700001)(82740400003)(41300700001)(36900700001)(309714004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Dec 2023 16:20:23.1102 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e71555b1-0b8e-4803-7a0f-08dc09533987 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: DS3PEPF000099DC.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB6951 X-Rspamd-Queue-Id: 563E14000C X-Rspam-User: X-Rspamd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quxm5jTAcvMkGZaRaGJv6JUPklrKaav1Gyi/iwBWFY9kgZ8MjtMd9L7PfvLs9p4MmpFqan66lgHLXGJs3W3E039QuH8njIhcSnoq89J1Kj+RJWJd6m4wYVqemJmhFrqQ4ZEQ6jsgXKdB27sA/HH/HgiULHjJKSN6GYTPydi1BVge5CRlen8pb0em02J0jwKacjmn2in/JdRR2t8S6/FY+6lLhuCLVQQJWAkLl8D0H4R2fEkHQbZiX0Mr4sNragEO1JNLe66i4XMx5PL9nImxW2dC79yulNuNNV0ZgmK1lBCRL+//Hux2pZI1gISde6G1hVfefYRJn7YmV5CE0dkBqfo1CaL+Lbxp1YU6TKwCqg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Brijesh Singh Add a helper that can be used to access information contained in the RMP entry corresponding to a particular PFN. This will be needed to make decisions on how to handle setting up mappings in the NPT in response to guest page-faults and handling things like cleaning up pages and setting them back to the default hypervisor-owned state when they are no longer being used for private data. Signed-off-by: Brijesh Singh Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra [mdr: separate 'assigned' indicator from return code, and simplify function signatures for various helpers] Signed-off-by: Michael Roth --- arch/x86/include/asm/sev.h | 3 +++ arch/x86/virt/svm/sev.c | 49 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 1f59d8ba9776..01ce61b283a3 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -90,6 +90,7 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs); /* RMP page size */ #define RMP_PG_SIZE_4K 0 #define RMP_PG_SIZE_2M 1 +#define RMP_TO_PG_LEVEL(level) (((level) == RMP_PG_SIZE_4K) ? PG_LEVEL_4K : PG_LEVEL_2M) #define RMPADJUST_VMSA_PAGE_BIT BIT(16) @@ -245,8 +246,10 @@ static inline u64 sev_get_status(void) { return 0; } #ifdef CONFIG_KVM_AMD_SEV bool snp_probe_rmptable_info(void); +int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level); #else static inline bool snp_probe_rmptable_info(void) { return false; } +static inline int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) { return -ENODEV; } #endif #endif diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c index ce7ede9065ed..49fdfbf4e518 100644 --- a/arch/x86/virt/svm/sev.c +++ b/arch/x86/virt/svm/sev.c @@ -53,6 +53,9 @@ struct rmpentry { */ #define RMPTABLE_CPU_BOOKKEEPING_SZ 0x4000 +/* Mask to apply to a PFN to get the first PFN of a 2MB page */ +#define PFN_PMD_MASK GENMASK_ULL(63, PMD_SHIFT - PAGE_SHIFT) + static u64 probed_rmp_base, probed_rmp_size; static struct rmpentry *rmptable __ro_after_init; static u64 rmptable_max_pfn __ro_after_init; @@ -217,3 +220,49 @@ static int __init snp_rmptable_init(void) * This must be called after the IOMMU has been initialized. */ device_initcall(snp_rmptable_init); + +static struct rmpentry *get_rmpentry(u64 pfn) +{ + if (WARN_ON_ONCE(pfn > rmptable_max_pfn)) + return ERR_PTR(-EFAULT); + + return &rmptable[pfn]; +} + +static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level) +{ + struct rmpentry *large_entry, *entry; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return ERR_PTR(-ENODEV); + + entry = get_rmpentry(pfn); + if (IS_ERR(entry)) + return entry; + + /* + * Find the authoritative RMP entry for a PFN. This can be either a 4K + * RMP entry or a special large RMP entry that is authoritative for a + * whole 2M area. + */ + large_entry = get_rmpentry(pfn & PFN_PMD_MASK); + if (IS_ERR(large_entry)) + return large_entry; + + *level = RMP_TO_PG_LEVEL(large_entry->pagesize); + + return entry; +} + +int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) +{ + struct rmpentry *e; + + e = __snp_lookup_rmpentry(pfn, level); + if (IS_ERR(e)) + return PTR_ERR(e); + + *assigned = !!e->assigned; + return 0; +} +EXPORT_SYMBOL_GPL(snp_lookup_rmpentry);