From patchwork Sat Dec 30 16:19:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13506883 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 3A1E7C46CD2 for ; Sat, 30 Dec 2023 16:23:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CD9D58D0019; Sat, 30 Dec 2023 11:23:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C8ACD8D0012; Sat, 30 Dec 2023 11:23:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B2CB18D0019; Sat, 30 Dec 2023 11:23:58 -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 A34718D0012 for ; Sat, 30 Dec 2023 11:23:58 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 74383C03CC for ; Sat, 30 Dec 2023 16:23:58 +0000 (UTC) X-FDA: 81624006156.06.ACF3714 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2059.outbound.protection.outlook.com [40.107.244.59]) by imf30.hostedemail.com (Postfix) with ESMTP id 7322280010 for ; Sat, 30 Dec 2023 16:23:55 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="nzLcd3/P"; spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.59 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1703953435; 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=AiVvp+np7A+xrED5Xr7DhTlkRvWTwZgGfMO/veOUKsM=; b=4yo2EGDKBPXD/5GvewEjMTgKlfonnCJ6KNJaqExRCjan7KDAFgS5mK1hgHoWgS+mYWmCc0 2p3oxfTzBPFVrFNhv4dAu+VneTf/xLVYJ5heFvO6EPksInS/+hEzZnS6QFT1megtDP62Wh 9rdMZ+SPUmwtDvQuDf+l7x2QX+LBN70= ARC-Authentication-Results: i=2; imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="nzLcd3/P"; spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.59 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1703953435; a=rsa-sha256; cv=pass; b=WmUa3pi6z2SR3J+7D9cOxgcSCd4wfsBSTbIeEnw91pvlun1DYvt9kaFkEW5UfaZnfREU8P dcIrEWq/h6VouttUY4LfZ0qgNKo/9CirvfhF0SuxjMRzK2jU74J3Sv++/u+Ok7nYIAuSdf 65byW+msvGhDx1+Qq2Knj1q9j9HKm2c= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IsGC/hPGNEFBhq09hVKjCcs15yOjTfDY8ZPaYRKDjpJRt56LUCf2xhNPxKbWJ9fwEB/UHROT7qr1S9hhEbiMy+3mfVYE3DklXZIbREhVMNn0IOvgHFQi5cF0K2eDGScodqe/QkLtSR+O8sXA1LPaB45D2/9/gdrfMgd+npGWIMoaK2SocleTra4mWUjeWuy18gWIALbYCfdHILQ8bTN33n+Eg9Ls6mS9cBVv9FjVo7RQw9Q1pP4GDmA+//B281MZOy/ZoO7NPkEnuHV3ZlBgPvS2zKAsGbh6ZLb0da7oS5kWjb5eygLWlyeGwlJ4K/o94VyBaEM+K/GsQDT0xh3RhQ== 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=AiVvp+np7A+xrED5Xr7DhTlkRvWTwZgGfMO/veOUKsM=; b=FNk13BP113SR0fjHAxaOSSm4V+dCaoAAY6IMw66nHW0NxgxQzIV45kbwSKB+VwV+Qj8u45K/5EVPzoVurbJOA+3j8H/nEUWMrYKXHfOL4VtLbMBWYQEnvFOUV9eTtoxY6WhT4gCJdHdLUVVQa4H5DPPzNyFseksiPJk7dW/gTGquxj4ypLq2QI/HNlKn5OCTQZv73HqYGu6uF0zwItXWvU9WosyCkvsf/rx4veNzcIO1mV6tzsT5MMs6YV84SrJo1r1XPwXpjqqgtaW0lrkZRESDGSI5/S2RZ/hXepGXBQp6bQIJtktUe8gk/y+Xxj7J5Uh9Ok2rnb/6W3rOSLVX7g== 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=AiVvp+np7A+xrED5Xr7DhTlkRvWTwZgGfMO/veOUKsM=; b=nzLcd3/P30gySK0fWmLGafwBAFElCemdelDCU4GUXVMz60SkdjCFGOJA4XtkKiHydiqiugFzt02oEO8vEuvtWAcrWqAEZzr9SWD+J4Ow0tO7OvTK5TFYCyXffyYGNHLZmTO7lxiMScyucOdb0M0iSuc+BF+WyCtZfJBR6BeV1qw= Received: from DM6PR13CA0016.namprd13.prod.outlook.com (2603:10b6:5:bc::29) by CY8PR12MB8411.namprd12.prod.outlook.com (2603:10b6:930:6e::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7113.27; Sat, 30 Dec 2023 16:23:51 +0000 Received: from DS3PEPF000099E2.namprd04.prod.outlook.com (2603:10b6:5:bc:cafe::be) by DM6PR13CA0016.outlook.office365.com (2603:10b6:5:bc::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7159.7 via Frontend Transport; Sat, 30 Dec 2023 16:23: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 DS3PEPF000099E2.mail.protection.outlook.com (10.167.17.201) 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:23: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.2507.34; Sat, 30 Dec 2023 10:23:50 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v1 15/26] x86/sev: Introduce snp leaked pages list Date: Sat, 30 Dec 2023 10:19:43 -0600 Message-ID: <20231230161954.569267-16-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: DS3PEPF000099E2:EE_|CY8PR12MB8411:EE_ X-MS-Office365-Filtering-Correlation-Id: 09363be5-f9e1-44a1-c6a8-08dc0953b5a3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: dJcooFPE4Nx3IM4yDP3YucBYaZDT6vCL3yXzJgh1HOfYG4x1KxnGWWX/1iZ7QNH5BkH5cWpmFmtvYvjG2ZIHCZljWZf/inSu3vTmufS4799ywsI+qSzRVKfKie4juSxRaEer5MYdQudlBa6Y71mnJDxJdwR+Wg9NWoE+px6o7eJfYVk60oRbvzZicVw8N+H0Xr27EqMPF+nyn5IDYc8dgmJpoBgM2hkNhkYI95x7VBvz9X0s6sz2XN56rl3ibzcxHuS2pb+YeVFHvp31LYgPkronuRYEfzDZunWKI4gT5Isow2hrTg0iQRPlqRh36hjq2cihucPYKQ8iS9wOBaGZpsIG31qjNGnsvURsuOQh9nkzZULCFHRRRVe6Z1Rea49iEIVYKS5y3eKzYzs4PgiuybJYku24Kv30KQOz9S5c0RQCmRQR9fAH5PRW6dYn7F+8+y07+n2d0yc+283WhZ0/ZftUN0JZAHFRUm44oy/EAtoN1i//4eMeegAAjnoaIELjyDulcMeeWrdCAXsxtcikcnCZkT6thcUIQckurKPKgjy4mOi/6lY4UeO2PeNHSQujlg/gWOW0VIPbPQumXRN9CKs6fft46PoZHbEtGhgZwZDcntujbzaFXbUA2jimf+eAZ7ULtM0zDqITe+hFgtDHbJl7NjhPA2swO4zgqTPXd+SQJO7ETPvQVhNx4UGPDXGVNPfm6RZjtcvYyVh15gpp+jmT3pWTVj2PRWfBmjXIBtx5KeF1uX9FRLdh36mvd/caU4xhsM9kOv5n8SjVHxTUig== 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)(346002)(396003)(376002)(230922051799003)(82310400011)(451199024)(64100799003)(186009)(1800799012)(46966006)(40470700004)(36840700001)(40460700003)(16526019)(426003)(26005)(336012)(1076003)(2616005)(44832011)(83380400001)(5660300002)(4326008)(7416002)(8676002)(36860700001)(8936002)(6666004)(41300700001)(2906002)(70586007)(6916009)(316002)(478600001)(54906003)(70206006)(86362001)(82740400003)(47076005)(7406005)(356005)(81166007)(36756003)(40480700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Dec 2023 16:23:51.3331 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 09363be5-f9e1-44a1-c6a8-08dc0953b5a3 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: DS3PEPF000099E2.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB8411 X-Rspamd-Queue-Id: 7322280010 X-Rspam-User: X-Stat-Signature: d9b8gpr1hi3qc4ajfbkmxcs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PxCNT8+OczvSuWvQmxjEtPkbYq0cH+Qo47P64jC+vF6PnJ/WZYEaHH5Qe9RoVnha+ya2JeLXtmJ7ii/yif0urdTW6KmdsqNfKt2L2+ld6dUkR7HbnaixHLlS6cfhz4eKPZCIU2UNd91h0XwTKpcdOCrpj9XYmUgnlK/Du299sOarG4zPPDBmtaOP/1/Oa43wzUkFZK3kpiVZXYp607YXRnQNzSH9RxQ2davNebWRoRIi2feg0B5iPTKSsf83j0AcUXu2Ja1pjGLfE+6gB4tXKaTlLJ6ZTYTkq3/75c= 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. Signed-off-by: Ashish Kalra [mdr: relocate to arch/x86/virt/svm/sev.c] Signed-off-by: Michael Roth --- arch/x86/include/asm/sev.h | 2 ++ arch/x86/virt/svm/sev.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 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 ee182351d93a..0f2e1ce241b5 100644 --- a/arch/x86/virt/svm/sev.c +++ b/arch/x86/virt/svm/sev.c @@ -60,6 +60,17 @@ static u64 probed_rmp_base, probed_rmp_size; static struct rmpentry *rmptable __ro_after_init; static u64 rmptable_max_pfn __ro_after_init; +/* List of pages which are leaked and cannot be reclaimed */ +struct leaked_page { + struct page *page; + struct list_head list; +}; + +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 @@ -476,3 +487,27 @@ 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); + struct leaked_page *leak; + + pr_debug("%s: leaking PFN range 0x%llx-0x%llx\n", __func__, pfn, pfn + npages); + + spin_lock(&snp_leaked_pages_list_lock); + while (npages--) { + leak = kzalloc(sizeof(*leak), GFP_KERNEL_ACCOUNT); + if (!leak) + goto unlock; + leak->page = page; + list_add_tail(&leak->list, &snp_leaked_pages_list); + dump_rmpentry(pfn); + snp_nr_leaked_pages++; + pfn++; + page++; + } +unlock: + spin_unlock(&snp_leaked_pages_list_lock); +} +EXPORT_SYMBOL_GPL(snp_leak_pages);