From patchwork Fri Jan 26 04:11:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13532028 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 91D39C47422 for ; Fri, 26 Jan 2024 04:43:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2AD056B006E; Thu, 25 Jan 2024 23:43:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 25CD96B00A0; Thu, 25 Jan 2024 23:43:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 124A88D0001; Thu, 25 Jan 2024 23:43:02 -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 049DB6B006E for ; Thu, 25 Jan 2024 23:43:02 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id D6DE61604D1 for ; Fri, 26 Jan 2024 04:43:01 +0000 (UTC) X-FDA: 81720217362.23.E875231 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2085.outbound.protection.outlook.com [40.107.243.85]) by imf27.hostedemail.com (Postfix) with ESMTP id E195A40005 for ; Fri, 26 Jan 2024 04:42:58 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=v8hHeC2Z; spf=pass (imf27.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.243.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=1706244179; a=rsa-sha256; cv=pass; b=R8RLS5sXfZzvhvcC7hdiicj8WcLvoyu88Z92hVDxHo0UinQF9eZVggLzQhDMzZDJHJNgZO xoMceC2XKKamlbTbKmNUg4pvBRp5W3AK60aM5h9VTqEEhB5nzCepwQiGPahk7XiUCRtshr RU2VuFFEbWJHssJg4fyz9bqF/MxWi6M= ARC-Authentication-Results: i=2; imf27.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=v8hHeC2Z; spf=pass (imf27.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.243.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=1706244179; 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=szaFyuq6iYA2lOSQBax06TCzQzV99C7hyp/FnL8i7Hc=; b=PZYZv2P4jqgwsARjerwzVQ0kgOZvIlT9kvJKcqBHRoBBWBKBvq8UO6OLe2q91Q3i9KseaN ZxaTdt8cNYLewBchoynhcifxKQR9+XIkY6SDgGq1A5s+sN45B/U2CWfDVxIgm5aYbHDhLY CNkn9dNXo/8TdZ/QICWnggITnzLow38= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=K3KHWAjOLss0WZGMPcO93hZBx4wMrFIl5x6c8R4VZDaeZKvg7sM6WTI1Y1RhuGD4yx8W2U2gb6pm95jGsj4Ck0StcIj9nlNCcIGT3yh/KTToi21eJpgumttIvTG5sj4AuroS+2l9zufe8p2ArdGAi+rX2zFte1KWp7gQH7hvHFFIq4I0uLJL9M8CYALuA+TBxLAXPlztFfyd4QBOFeVkhvDsx+nmRNMfg9Y2rsN2U81Pnhn+2CeS33nnP36vfpNq5WNTyaidrrdxiNJ5WRtnKQq9R2t7HAABa9dT9/0AfZ3nCpGz3teXthMEJIV1HnV5ISXrIn5lRHLcC4h8Kq/fqg== 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=szaFyuq6iYA2lOSQBax06TCzQzV99C7hyp/FnL8i7Hc=; b=Syb6hp+01eXb9Kf8lTtpasYkhYeUv+BkC5eUJKc/KRODblm2lYxyoerlh6HEk1R2OAXQXN2qBKdtE6grnlMbdTplkT1jSJZGGLK4entwZ2Y4suxYOFJbOEtM2xVB5JbULKtZQ/MXk3Edsnc+7+6bWTwuzBgsDWhTSUYmJsbdnFAh1xCmYo/YDv8xEuCK39GxYh1nBz3k8DMpxjDdWpDVRAmP20J6bB4f+8dM1vjXqEP0IIeyr5ewTrCpk/FDHI1Qb5YEGoEKKryHh+f6QU17aMMq68s0Tc8ZXVVUtASsx/R7LTv0x0eQbr9/kaqPgRBNP/jtfOYS4hCGO6krVcjuZA== 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=szaFyuq6iYA2lOSQBax06TCzQzV99C7hyp/FnL8i7Hc=; b=v8hHeC2ZdIIanohNyWdlhUZjGeSkSMI5qQZ7fV/BdYDTMknwJ0ZeTPUplf68QasTPI+cyEB9FpjCD4JYsw5Bftu+Xhl9xR+iJRpNKaKX6dzA6Z2ABvX8uANb32EUz/3st4lUNINQg/cu4Ks+dVwckXBUKWBtgCljiK2RNwNLkoQ= Received: from MW4PR02CA0028.namprd02.prod.outlook.com (2603:10b6:303:16d::7) by SA0PR12MB4543.namprd12.prod.outlook.com (2603:10b6:806:9d::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7202.33; Fri, 26 Jan 2024 04:42:56 +0000 Received: from MWH0EPF000971E8.namprd02.prod.outlook.com (2603:10b6:303:16d:cafe::9) by MW4PR02CA0028.outlook.office365.com (2603:10b6:303:16d::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.26 via Frontend Transport; Fri, 26 Jan 2024 04:42:55 +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 MWH0EPF000971E8.mail.protection.outlook.com (10.167.243.68) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:42:55 +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; Thu, 25 Jan 2024 22:42:54 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v2 15/25] x86/sev: Introduce snp leaked pages list Date: Thu, 25 Jan 2024 22:11:15 -0600 Message-ID: <20240126041126.1927228-16-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MWH0EPF000971E8:EE_|SA0PR12MB4543:EE_ X-MS-Office365-Filtering-Correlation-Id: b8048db2-57b5-4e12-66fd-08dc1e2943cc X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hr7bMntL6Ht3VXhxEwbcLo8pEpeNGsPsvPPPpQJXzNGfYzxT0+SECARMwBUcFeGQ95bjUlnIGg4VQJo6GoAaG1qmQv+SKnrQ92zKzGAK7ND2qIutYu1D7dtgWTlBzfKbs+W+75EYumDz1vFrGntvjKTj/nkIcGf9+DDZEVkZELSUQ21Vqygh7Z2yxcXqKqHaQSs+O/zZmcRvnzP7j3/XBHt/HxX9WGDZzQYADL0zmQj4gLeDlPVVLgRnL9y66QPvyT6wrL37+IxoWcGShg0I3u7zqrO71xe1j2HUqzQ+U4aZBIuq4AP8Mv1qSKUO8lpfCWBMy0LO7fNFCCKTeno4hyKGX17YOnJI5DHsRA9Yn385qWXhdQn4O4trVT0/+MiTtGHMdt1zleltjwnSUYBJnKJRmnMRzfkIuJFOmbNDnDvnz6mupgFs8uw1Wb72+yncGfH54hB31TVQ49NI8N+CPPqy7vEic6wodPdjzl1ZmHxrU/Ns9bSCK78XNwJZtolMchgKt/7g3ug5kCN4gwxzt1PoBofW/UDS39B7cSY2j2KYnv8lK0ws2Qcwki5NS7OiZH+2yvcoDVjHBDC5hpHPH2STzWtI5zWw8QHSo/sf+vxHHrehp1EgEnphhYWldqE9UD9dO9qfN474SzAzyLbPvnLqtBUs8aQrzlg+80U7IYI6K0XxHQktZBUkexho/PzyhoUagOJeexZKciE9ITY0GEliXVrzsx8d7cUqO6l2QJHF6Rmt6VdiSA/0LWSIWMQT76BR2N40K/RlU573Pnv+4w== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(376002)(136003)(39860400002)(346002)(396003)(230922051799003)(82310400011)(64100799003)(451199024)(1800799012)(186009)(46966006)(40470700004)(36840700001)(356005)(81166007)(82740400003)(1076003)(41300700001)(2906002)(36756003)(6666004)(4326008)(40480700001)(336012)(40460700003)(426003)(8936002)(8676002)(83380400001)(26005)(86362001)(2616005)(70586007)(70206006)(54906003)(316002)(7416002)(16526019)(6916009)(47076005)(5660300002)(7406005)(478600001)(44832011)(36860700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:42:55.8323 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b8048db2-57b5-4e12-66fd-08dc1e2943cc 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: MWH0EPF000971E8.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4543 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: E195A40005 X-Stat-Signature: an6qg6t91j3n1nbo6mdsx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b7Xmc9aWIlKG2TX1jF/2mGg7pPf6kJ/JKVyMevJ2TMgsa2Z3iEO3abB6JIYonSG3az6kskx4K8alaYORbhW/RrCX1tzZZr3SY9P/+jfgFcyEpG824RrhwGjQ0yLbTI6rfQknuLJX8SFNCi3/evfuiHZSnDpH6Fyva1l+/OTnJdmgnLqqh7FFK3gw/SI/W4HQhEjbu8cmMSz79phkK3CPzcWV5ZNuu2cTNLYKV5Y6lOYA6yLN7nEPOrY8vCxS1pyPR4ufwF8o/QQit1fnmdf2WCWAe0K1wTygy7QtwgZ6VCMw8wL16EOjQB+rtFVSsZ09AM2OzSjFoNvOQ350A== 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: 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. Suggested-by: Vlastimil Babka Signed-off-by: Ashish Kalra [mdr: relocate to arch/x86/virt/svm/sev.c] Signed-off-by: Michael Roth Reviewed-by: Vlastimil Babka --- arch/x86/include/asm/sev.h | 2 ++ arch/x86/virt/svm/sev.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index d3ccb7a0c7e9..435ba9bc4510 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -264,6 +264,7 @@ void snp_dump_hva_rmpentry(unsigned long address); int psmash(u64 pfn); int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int asid, bool immutable); int rmp_make_shared(u64 pfn, enum pg_level level); +void snp_leak_pages(u64 pfn, unsigned int npages); #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; } @@ -275,6 +276,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 snp_leak_pages(u64 pfn, unsigned int npages) {} #endif #endif diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c index 1a13eff78c9d..649ac1bb6b0e 100644 --- a/arch/x86/virt/svm/sev.c +++ b/arch/x86/virt/svm/sev.c @@ -65,6 +65,11 @@ static u64 probed_rmp_base, probed_rmp_size; static struct rmpentry *rmptable __ro_after_init; static u64 rmptable_max_pfn __ro_after_init; +static LIST_HEAD(snp_leaked_pages_list); +static DEFINE_SPINLOCK(snp_leaked_pages_list_lock); + +static unsigned long snp_nr_leaked_pages; + #undef pr_fmt #define pr_fmt(fmt) "SEV-SNP: " fmt @@ -505,3 +510,32 @@ int rmp_make_shared(u64 pfn, enum pg_level level) return rmpupdate(pfn, &state); } EXPORT_SYMBOL_GPL(rmp_make_shared); + +void snp_leak_pages(u64 pfn, unsigned int npages) +{ + struct page *page = pfn_to_page(pfn); + + pr_warn("Leaking PFN range 0x%llx-0x%llx\n", pfn, 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. + */ + if (likely(!PageCompound(page)) || + (PageHead(page) && compound_nr(page) <= npages)) + /* + * Skip inserting tail pages of compound page as + * page->buddy_list of tail pages is not usable. + */ + list_add_tail(&page->buddy_list, &snp_leaked_pages_list); + dump_rmpentry(pfn); + snp_nr_leaked_pages++; + pfn++; + page++; + } + spin_unlock(&snp_leaked_pages_list_lock); +} +EXPORT_SYMBOL_GPL(snp_leak_pages);