From patchwork Thu Dec 12 06:36:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13904717 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 45925E7717F for ; Thu, 12 Dec 2024 06:38:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D58A16B008A; Thu, 12 Dec 2024 01:38:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D09416B008C; Thu, 12 Dec 2024 01:38:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BA9EB6B0092; Thu, 12 Dec 2024 01:38:33 -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 9D4ED6B008A for ; Thu, 12 Dec 2024 01:38:33 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 53B89B02EB for ; Thu, 12 Dec 2024 06:38:33 +0000 (UTC) X-FDA: 82885352550.30.7EFCFD0 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2061.outbound.protection.outlook.com [40.107.244.61]) by imf09.hostedemail.com (Postfix) with ESMTP id F27F2140005 for ; Thu, 12 Dec 2024 06:38:13 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Luiq8S4E; spf=pass (imf09.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.61 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733985499; 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=A9XYF0QJO5+ytlz+nk9KzbhWH+1hy5p4tpp3erNpAR4=; b=wgM3A6KoMTROHFw412mqLR6iziNiB6pVJCnNWmTtbSQhEin0EbmGOEWr5APD8BENEUMQIO mpg6tCma4tLNdXFEPWEI2NyrL65TartEj6JUQzBxMTr0tCrIW8Nh+ewyBQtH2+ncuja86f NbGZiMzYIxuUrCfO3uixQwfQLzAjGYY= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1733985499; a=rsa-sha256; cv=pass; b=lZ8ttEh7W/ReT7+eiQ1vwpAOKI7o+YLvLBkS/witWf3m+qt2290O+zPGotGFfgeD/CgDwB iCrLG3j93ZyU9tczQKnD38qpc4RZ0EkHrdEM3rJ9CnTl05pRgY5OTVtykoMPelNtup5PPz f+O0tOraW0Tp6ZyYR+h5vDqlFUDzbL0= ARC-Authentication-Results: i=2; imf09.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Luiq8S4E; spf=pass (imf09.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.61 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=xtau7Aqv+u6p3il7C31tUgPiAltOmXOp24rTfwJnaz38Azg2DZLSwJxjdaNm0gMcqWw94zRfxCJJnjP3fQzwRFrbLGN2B0/J8FZNiDIo2p+fbTn3E/Q3KAJzELBThWGeL5wqUxNWuBOEewVRlF8Db4h44WlCpy4VSGKMdXggpXlidfHHk7XOAjViuLsysjHgK6/g4F6PlcV20sKFCLmiMuGLlz/6ITqrYTMSzyOV6WBqrbc9+zfOHbVQF7INwlypY/5/G7czoBBuoiR3DCrBNdnlLt2zIhbPf53unZ81k/HltUsO3pKB97h6r9O7ymakJVrHcVTy4ve6a8WB6hKXsg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=A9XYF0QJO5+ytlz+nk9KzbhWH+1hy5p4tpp3erNpAR4=; b=yPJ7wQrsNtWgxPBEzBTLreYoBNt5tA60zxhMQkAMkFCiFfP9hWtGhQMKLaJOcvgJMrmS729xrclmn+k2Ykwd8T56Sxw4EA2tmv4Own37e5CaXZSpAOGv7ZqqD3hyTaYv0CySm18d/6ag337aWHvROooxuoNyBTCCYixTCkpH7NP7WeY6gzdtBeZxMdEecuM5XgPZOBhrCO+tOO3Lq3/vjLqWgopXLPWdoOx22gPJ+4WNuGPsQzKSrU4h7Kqp8gl075LtkMfjAV5Z6cTES4N88/53FqOwLOycfwINq8kS/qNsgfrel12Ue0tOCu0pr5GHE6Ebo3Df2jAsJkLETB85Mw== 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 (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=A9XYF0QJO5+ytlz+nk9KzbhWH+1hy5p4tpp3erNpAR4=; b=Luiq8S4EpjzAZbRSNr3OEZ6mYgWZqjTTmBGkkt4i2xaY0+3XY3V5QUY2bKMvW0HEV5brlzlTMOxjBhT+HjUm1EAcSSPRTTD0gWPGgm1sUqCmGcieovqwxDHTLIjna24b0q3bwIbrQPOnl2nWU83lUeNVy6irA997gAwuy1UIulw= Received: from CH0PR03CA0234.namprd03.prod.outlook.com (2603:10b6:610:e7::29) by CH2PR12MB4184.namprd12.prod.outlook.com (2603:10b6:610:a7::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8182.21; Thu, 12 Dec 2024 06:38:25 +0000 Received: from CH2PEPF0000009F.namprd02.prod.outlook.com (2603:10b6:610:e7:cafe::b8) by CH0PR03CA0234.outlook.office365.com (2603:10b6:610:e7::29) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8251.14 via Frontend Transport; Thu, 12 Dec 2024 06:38:25 +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 CH2PEPF0000009F.mail.protection.outlook.com (10.167.244.21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8251.15 via Frontend Transport; Thu, 12 Dec 2024 06:38:25 +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.39; Thu, 12 Dec 2024 00:38:24 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , Subject: [PATCH 3/5] KVM: gmem: Hold filemap invalidate lock while allocating/preparing folios Date: Thu, 12 Dec 2024 00:36:33 -0600 Message-ID: <20241212063635.712877-4-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20241212063635.712877-1-michael.roth@amd.com> References: <20241212063635.712877-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: CH2PEPF0000009F:EE_|CH2PR12MB4184:EE_ X-MS-Office365-Filtering-Correlation-Id: be83d3cd-20d5-40b0-210f-08dd1a7794df X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|7416014|376014|36860700013|1800799024|82310400026; X-Microsoft-Antispam-Message-Info: oN/MTV2i7zVlEcCb9zpOOX8bxmXnV/KyZYy70+wWj9RvacoX2P0g1wHrEllM09A4GWQENKNvqdoowKL5HXi8y/SY0y5ihia/j2anghIYALZIs8xq3ZHgp31IAt+0W0D8fIyjVc747STmbSrrPLNs9W/OE42NMrsR6lUWOfp21OHxwHSPtqudYpBf1jYwpi/e53TzVvqO+N3EsUkq08JGiB51lWZiYkLZFp0S4fuewQo/2BZ5EEQR3sZ/Qa/YYBFAAHzQaTQ7XBzfeiM7CX7NGQu2DCscdtqAWkYO4TxxeAja2mhIAzdJ/eZo03Cvz7NKOvmZ3E973IisafCtPBFgz4VoEgLFvBFZZH3eODXe4h240dPs95ppYgfxNY3acEJjyoZyPIcm/FRxlTLc08LGAQvvFiEXM1Vb14i+bh623cEcW/Bz3zuY+HZYoAu5HikcJIVlDXTghNEd73CBc0RqKALHKImDRddVFmC0kd9mVGb9dhxLytQRwTG+9zkBVR4KYX2EJXLwLa2y/ShE6AWqcsmb04kGTvh7IRe+cBNrw4MKrIRV49RYwcc6ZMcFnnYLO0Tq5oH3TiJjV0ig6NExCwGy4HIMI9e0DyrlM9bhKmSFDHYX+Hts1e8iT6nHtxwduxrQJoNuH+UjDHALjoL/lfs9TqRwtNMRIx/RoZSdeW+r1S7HCJbescChOSArYg1U1GigAjCfomo6wcEiImJ9iUTRPkeuhfq163FDRT2IhClAGKLzy0fPSnIUzE3GBi2E+gkWihLEYqSjsS6jcJJs/FAWwn1kDFzHN3zrt+v0jGtdDUPHnT/CbWvXOybYu3tbHSJKN+ZshCZGP5tKy4rkelz5Zjvr1n+7b2MdmC4e5oapIS+Ho+i85MyzhvYLFty7MGB5hlXeI+Gg+qOsBMMaoWYjGcA57KRjbpmE8KrsGyuim90+sYnZnhlnnQNEQo7c8SglhyP+XnZSWMy07WD9dmm6yxSFGcEEhXzqy4tBclswBmYI/hrJdnUNrNYWyyjNIgOiPgD71YC0181y7/0xI5c+B7BBDfv4kDTOMwHEe8+FEM4AblU918nbLrd1rb6XjE9wjwJJZTwIQCgD21/jE902nOVjjmGDL05bnkw/UziXSZfLBN2NYaCjdjk6KXuf4KwJbcU0gEJzw/APwBa8UZEIwRZ8h6Gvy/iEOS8kXgTnSZ/Cl5UWj4NmwhOZzT45MVxHU8G9jGaPbPVjzD524RPe4g2hMr7NHFkDn4CvzRtmfbr7hKK0QH1qrqySchBtYTIUS/wtdI3NKrRCb2d4Ok5X0jvfScyLkCKrSQBu5d0GtkJQEYUrKVA5btLGAJ/1BFGuhcv6V8qd9DN3AFjGAg+AGuJ4T0Nw64fVZyEqfydpebW/P1gNLhHu1sBtIowl8jJ9aN0kv0sPv+Sg04am68eGrWOuGprU91sLgW86Lrtsny/LVdrRljTRd5l3eYdl 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:(13230040)(7416014)(376014)(36860700013)(1800799024)(82310400026);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Dec 2024 06:38:25.6309 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: be83d3cd-20d5-40b0-210f-08dd1a7794df 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: CH2PEPF0000009F.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4184 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: F27F2140005 X-Stat-Signature: fht8zbizmuh9phccbib6bnae7ebh8zmt X-Rspam-User: X-HE-Tag: 1733985493-739916 X-HE-Meta: U2FsdGVkX1/8MDLscB0oKd68130P3Yv2VUWnckUYWAKj1dLsJtVNby5XGN1V9h/kvDU0HfSGs3Hk7KLWYK5oKNtMzshH8OFat2dBS/SpUthlbSTPpzPzo+MGcj5YPUKVBRjrifJVnIit4yGiF7SMJIQZjcFyUWvfzj7e+k5WuBtxxiQJnzgUa4g1bs+S3EOhzuPN+pvQef4S0jyp/PnS/Sr5HZPULFlLTEGL0Q+tXcRjTlqbh0MX3upx9DpdTCR87b8CdQ+EIoY3+uTER3KtFCy8mX/YIoXvT5rJTRTqNe6RalAWNHV0pSEc1XJbvq0Bsc1KmerVmmhED7YW7UJlGrNt4CN8BHTGh546LpLq5A9z6qy87OWmT7jL1QIIuki+j0jJBeTlTUhv7rBQlfLc2d4+furZKSMucc/iaM4prJGEmeCg9J/dlfkYqUj2n24FcSbj4hsEEXU+IK4rq9W8G0DU+4Mf7Dirdc7u0Oxi6rCHcIYJ9GGf72mWjDBcZEV1OroJk4MSMaAfohtnZNxMWOirflIQvM5DyQKwlbjrTFMnE6Rq/K6qhjY6x1WoZApg92bD2jor7FcaTNPfjW5RsAkkBno1pPlD0jMwHJwfq8IoW+RA5Im5XzxfI12oK7x2utmpW9gyVQ8YAZwidJ26t8U7qtiHFy9wT1tDgW3WpcTDyqpAM1D6cXDd/jNCleCkXcgcb8kMY0JboAG5aiWZ9c15FePpauPh6ca0LF93jocQfZ+X9g+/sRoA66VIGeSxWWZHzDLeWuJfXGCGNHARXLFfGyawWuxOk9Hi9stOl+C4m5jt5BI8nYRACBpCemHn4kXnwm1TMV9V8Z4mmZB7Qnpf1uAap7gVyrYzzYQ71921xIOcnAmD23ddzljYzy2FX8limYIBY7GVGJTlzKqNxrX8aVYWOZzFAtxwDqY9SmQjrNe/rkMrP59LxhO03ywmFyHV1Hbiq2A/RMSZgBf clZ7Cnbw 2k4g4c2ub8Dehn8PDz06qKZs1emMUOpoTV0un3/lePNAZHa8QPXNVt6/k8ZrcwouM2sahPGTUqTF80YeLILipDEiN2gBNoOEX+M8hDaz9yDcxRdhVfloZpp9JMwWTWCyomresvDh+XGYwmfqTVm2/HTyatQ2Uor6RHy5b26jakpgkvqMi8K8VIItaAvuM+pJu2i6sOPysX7YXFrcxHkfUb2xGxw4wXN15czYOUyWP3+DdaXXm/soc4GIAJ5JtZSJX7IySHXPrHqZHzMMaIjaweVDiv/ZGYwvGsCq1LjuTF3LZiU5DnDWHRyKWd09R3VSD+oiqEo/Btt7m6TbDeTedx0RcLfHUJQl+ume3qt2z3mK+PMAh0l6Gl6rG8CoVPPHRDOdkul/nGf5m10is/NHC/+lU74Vh/4pijTS435nBu1t5vUJqkFBxzlVvFEXidF3xnIDtBWp7fLB7uXfwmdsHPYmLiyoFURs8l4qcpcTrNcQAs0+XsihVb5oHlA== 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: Currently the preparedness tracking relies on holding a folio's lock to keep allocations/preparations and corresponding updates to the prepared bitmap atomic. However, on the invalidation side, the bitmap entry for the GFN/index corresponding to a folio might need to be cleared after truncation. In these cases the folio's are no longer part of the filemap, so nothing guards against a newly-allocated folio getting prepared for the same GFN/index, and then subsequently having its bitmap entry cleared by the concurrently executing invalidation code. Avoid this by ensuring that the filemap invalidation lock is held to ensure allocations/preparations and corresponding updates to the prepared bitmap are atomic even versus invalidations. Use a shared lock in the kvm_gmem_get_pfn() case so vCPUs can still fault in pages in parallel. Signed-off-by: Michael Roth --- virt/kvm/guest_memfd.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 6907ae9fe149..9a5172de6a03 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -154,6 +154,8 @@ static void kvm_gmem_mark_prepared(struct file *file, pgoff_t index, int order) unsigned long npages = (1ul << order); unsigned long *p; + rwsem_assert_held(&file->f_mapping->invalidate_lock); + /* The index isn't necessarily aligned to the requested order. */ index &= ~(npages - 1); p = i_gmem->prepared + BIT_WORD(index); @@ -174,6 +176,8 @@ static void kvm_gmem_mark_range_unprepared(struct inode *inode, pgoff_t index, p struct kvm_gmem_inode *i_gmem = (struct kvm_gmem_inode *)inode->i_private; unsigned long *p = i_gmem->prepared + BIT_WORD(index); + rwsem_assert_held(&inode->i_mapping->invalidate_lock); + index &= BITS_PER_LONG - 1; if (index) { int first_word_count = min(npages, BITS_PER_LONG - index); @@ -200,6 +204,8 @@ static bool kvm_gmem_is_prepared(struct file *file, pgoff_t index, int order) unsigned long *p; bool ret; + rwsem_assert_held(&file->f_mapping->invalidate_lock); + /* The index isn't necessarily aligned to the requested order. */ index &= ~(npages - 1); p = i_gmem->prepared + BIT_WORD(index); @@ -232,6 +238,8 @@ static int kvm_gmem_prepare_folio(struct kvm *kvm, struct file *file, pgoff_t index, aligned_index; int r; + rwsem_assert_held(&file->f_mapping->invalidate_lock); + index = gfn - slot->base_gfn + slot->gmem.pgoff; nr_pages = (1ull << max_order); WARN_ON(nr_pages > folio_nr_pages(folio)); @@ -819,12 +827,16 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, pgoff_t index = kvm_gmem_get_index(slot, gfn); struct file *file = kvm_gmem_get_file(slot); int max_order_local; + struct address_space *mapping; struct folio *folio; int r = 0; if (!file) return -EFAULT; + mapping = file->f_inode->i_mapping; + filemap_invalidate_lock_shared(mapping); + /* * The caller might pass a NULL 'max_order', but internally this * function needs to be aware of any order limitations set by @@ -838,6 +850,7 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, folio = __kvm_gmem_get_pfn(file, slot, index, pfn, &max_order_local); if (IS_ERR(folio)) { r = PTR_ERR(folio); + filemap_invalidate_unlock_shared(mapping); goto out; } @@ -845,6 +858,7 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, r = kvm_gmem_prepare_folio(kvm, file, slot, gfn, folio, max_order_local); folio_unlock(folio); + filemap_invalidate_unlock_shared(mapping); if (!r) *page = folio_file_page(folio, index);