From patchwork Tue Dec 17 05:12:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13911110 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A1801E7716A for ; Tue, 17 Dec 2024 05:21:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:MIME-Version:Content-Type: Content-Transfer-Encoding:References:In-Reply-To:Message-ID:Date:Subject:Cc: To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=Dt16aTHQWa/9x7dM8NaNzVhZVxyBkJOcFpMjfFKYq5Q=; b=Y4qCds3MON0iX9KLdHHinRR80j ypjyX4OaetZY82EzsP7D3zMzoYCX3u0LWQyw4VH+P6pYX3R3HL6zfd2Mmtw+amBzqm5uC5K5iFBea i8LTTklvzCtxG3U6uvGHm5VeTyIizuEVWZHISPW5ONv2DEFvOaw2TgEpPx93jJLlWci1sv/5IWNl7 i/fLZIGKJgh955KCZ6jn5xOCCjgfWMT6BRwiv0odzCsKCPpKxuY1698lJnjDPCxtNQm8SaZLcINdt 2Z6/a/iPicaTMuxtAAYREiFCx/IrfND05+lsGXXwXGEbsZuBKYSBaEO9LdiLOuje37PXhf6PDqXZK uz/NhfGw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tNQ1E-0000000CFsa-2GBy; Tue, 17 Dec 2024 05:21:16 +0000 Received: from mail-dm6nam11on20608.outbound.protection.outlook.com ([2a01:111:f403:2415::608] helo=NAM11-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tNPuD-0000000CD6V-2R8s for linux-arm-kernel@lists.infradead.org; Tue, 17 Dec 2024 05:14:02 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=r7UbssMyxrkp6dlor1G9yJWYPlfVNGnEfPHuv9cbhuJGpITF19pRBMu42wgabztFIJYsbu2XwhlU5u635L4t5mm6pdkZW9ubSRQI/L0p0dN25nvUuiqVUh8phcoEzJSUoF7/9jDfpELaWouAf4ZyoaxkJxZw4WOh5F5taqy1rORjNkLioTsgubpooNHvvgm5g2yjdUEwrrxh01qfI1HxTH9jCURbZziWSZCf86EXDlO4BHFgzVvNnWrBt2s/txsGKQXYGzKlnKp+XRsACc+jLuceNJOpu1POOvHPjEzj51R9bHoZbxuh2EGqRA+QOT94BC2iBHXzpfz3sPucu4WT5g== 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=Dt16aTHQWa/9x7dM8NaNzVhZVxyBkJOcFpMjfFKYq5Q=; b=i1+KdwK+geXm14vWQfURYu2oM+Mix51funTBDLeOmFua2+qpNC/DPOyLjY72EPWILMWGylQHWsI0Vv82hX3L4Nv22hdMWMW531kFbhsO2KpYi5hwKJmk4veBpOBMdX8L8Qh2WREzondG65pDZfuXyGfPVtDGdqmdItqDoElPvwhHfcuikccwhH0kHtsNeQvNG0CMDZ1X8NOoe9CyiaZPeLijGBCXhCB6T5JAnzL0H1EQDWno0lclTZUC5JBcDiE4kLJKcGC/MaCB8b6jMC0V+XxtYNLH8zECYO9tOHzZdbmhVlMvrCESBl47ei202CVEHMOfi8Yv3WQ3gkieC0k1bw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Dt16aTHQWa/9x7dM8NaNzVhZVxyBkJOcFpMjfFKYq5Q=; b=MoEJwevMEMZLA4cUJ6B5q4gOtWRs7dveFE3lRGXDbvb1pPu6//+0lOAXbl6VZzlNSrDdpQBMRWXlJO33RpXm8floW4plRUVa1Xeu8+QuCA82bmv+Heaa3WC1FQsa3OFAUUpOUrQk6lCysGvrgQFdIjYjWz4KzBo53lUx6PXYhd8MnOlf4UVyOa9gPTxUgOKPF+XmMH+H+qAsNC8YZSsKkAAPLZX4irhtIYEK59nYeZtHBMX6LmRjk6zMcFlMMhsw0wTrBMJ0b/vCnv3VAP6MnHVVhlpqrRREEJR2I9iOSjwO7hXf0zPCsI5SEM8bA3Lzxsvt2pb/f3RAw5GG7GUUuw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DS0PR12MB7726.namprd12.prod.outlook.com (2603:10b6:8:130::6) by CH3PR12MB8936.namprd12.prod.outlook.com (2603:10b6:610:179::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8251.22; Tue, 17 Dec 2024 05:13:59 +0000 Received: from DS0PR12MB7726.namprd12.prod.outlook.com ([fe80::953f:2f80:90c5:67fe]) by DS0PR12MB7726.namprd12.prod.outlook.com ([fe80::953f:2f80:90c5:67fe%4]) with mapi id 15.20.8251.015; Tue, 17 Dec 2024 05:13:59 +0000 From: Alistair Popple To: akpm@linux-foundation.org, dan.j.williams@intel.com, linux-mm@kvack.org Cc: Alistair Popple , lina@asahilina.net, zhang.lyra@gmail.com, gerald.schaefer@linux.ibm.com, vishal.l.verma@intel.com, dave.jiang@intel.com, logang@deltatee.com, bhelgaas@google.com, jack@suse.cz, jgg@ziepe.ca, catalin.marinas@arm.com, will@kernel.org, mpe@ellerman.id.au, npiggin@gmail.com, dave.hansen@linux.intel.com, ira.weiny@intel.com, willy@infradead.org, djwong@kernel.org, tytso@mit.edu, linmiaohe@huawei.com, david@redhat.com, peterx@redhat.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, nvdimm@lists.linux.dev, linux-cxl@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-ext4@vger.kernel.org, linux-xfs@vger.kernel.org, jhubbard@nvidia.com, hch@lst.de, david@fromorbit.com Subject: [PATCH v4 06/25] fs/dax: Always remove DAX page-cache entries when breaking layouts Date: Tue, 17 Dec 2024 16:12:49 +1100 Message-ID: X-Mailer: git-send-email 2.45.2 In-Reply-To: References: X-ClientProxiedBy: SY6PR01CA0056.ausprd01.prod.outlook.com (2603:10c6:10:ea::7) To DS0PR12MB7726.namprd12.prod.outlook.com (2603:10b6:8:130::6) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS0PR12MB7726:EE_|CH3PR12MB8936:EE_ X-MS-Office365-Filtering-Correlation-Id: 8c042e64-ca26-4b0c-ba6b-08dd1e599cc8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|1800799024|7416014|366016; X-Microsoft-Antispam-Message-Info: vOezp3DYsZnu8ke8eB03nSukQUlqBelVOOOUBC6egyP6zgbMomeZ0QSS3VNCpfIWSTDfQoSkXhNu3g+PimbNYPBdT9QgOC6CWTVGw/97l+Y5MsDlQVBHtvleTO3WVHA9fdB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Xc73taaStuznq8kq7x8wZnZMYqq99CRIBE9kvELgT8c0GYd3SQX9XSSMTk2vPZsdVZKbpoxjBrOAFJYLFOkdKWYeq2IbVZZV8OJq1Z2yN2L61WvmNChtKXhC8n/dQz1ZgMFqCDjrU+3oGsjLRvEb5aeV9mVPNC/H0f0LHsdl4Vm7AYEkUFcfzjfTr9aRzCjj3UsMd+43957 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DS0PR12MB7726.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(376014)(1800799024)(7416014)(366016);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 8gVl47iUmFUPwo0eqPLmxy28OhBtI2zEwTG+riiycA9fcyENWelBdSCtcdekcspcgFrPNWSGo+hTDzq1ShB6oWyf5QeiGFDWQHlrOeWmfOo5sEQ4t5Bfaa/aHpVVToCFO9ygvdIezu0Vkx+9Kx7yborAswdo0Hwvs4m2I+z4SN3c/EChEMNYFOjluJ+aTDrTy3bvxF33c6iv0aMv9UV6HX81Mq4SD7o9Ktp0aJvSkqCMqiYwPk/7fMg27X0EKOL+fk0DZU/Tfpqp/7jFx0550WXrrtjp16zamqJ2YrltwC35bbWfwjY3k5+QlGjEoD5j2AX4cwnH0ErlDWb+7Aeo/u/I87ReyMsVxB/QkOriDHy/pBRaQcG7ZglETELl534EzzwjLc/V/l1E217U1BkkDFjh1zuyYqcaxyJqjW9xB6ReCL8dacEP9IYrea3/lfl/5NuyD9M9bvkLTstToyrRcSGO1YirURxpppLjsvdF9QZGcbrM8cd4ox8kxvgf5PpQ/lzXyhxuXO/2l7ZrygTbppxxRYXvltQ45qCcubbs144kgXkDJBEVAg178JktgdGVAZj0FpBGEu1BPxianAhRvX3+F/2F70eVHLlo64zkuCMVkpWJ+EkXluheO+CplcwC2Sna/bNmtRW2OP+eseYhF+sEp6R8Vf43T4d1RSmUddZ6R/VeK4bVEfwMPNhO9tHYDD7QQKieLI47+Gu0z0SOrHa4lH/qd7QPyUthnENzHAjb4/kQdR33BORhNztD2IX4bHWmMUMDIYvSWVF4/ZlXyMsKielRtQaGRBp8I0A4Q2cCTmKkeKYyt8xUZJa5EsglsOgYT53RDUMk6JLXA628yEHQfZzuA1oUlDKslr2Ky6MOA9BycsEGmzpzQDKWnLD9ZBz7WvZNlcOStAvOoj8MTG0o9EXPFv2tMLnEiQoBLPhsEXIr+DtsUbswjahBMpCxWVV8yruS6kr0PQfMNMiAjirxhc24hzjVmFhwZYtW5mCAMatifaHRRw2G7io4r7YOhBBdTHVUwfPf6s7kvSZckE49DHO/WX5i9RyfNvvcuNq6x5gaCEv99VhhCXV/sNVvAGfRtHFQOMOAYtgwKJOO3F35KuMWFf/V2U+RX4OdbD+GoNnKQ01+4l/kGbB6sZ/JCFZWhRj+duvGi8VSNGrMqRLCZPcBocuwF5bA3Q/O90CEAq2OeVMuYAGgppcAY/ZGKzABKBKd+8b+UUn3ptqzGBpcbQ517jlfSeyu8aiSUSCp6Slqenrbm/b9B+LMEtCSg/V+PYYVEEfFlbrU20n/1n+pcZA54Eqe7a+sd/papF1kOCH//QC9ZNrRmn3FcsihMNWSSnxM1ImqeHfA+1I9GGDwbyka8qB0clEnpFG2LLNeljFs+byascYGN3T9zJ3Gqtcv0CPO58RKZT8KxktZQ5tbTUIjHqWXh00LfxeDmj9fT+4JEl7UykzlL9pPsNxToFdoC4cVyNnmwTPSwk1VhH29+XuC+MjjJj4MzPSWGr8bI/K9tzaIzLvtmnz0krdNRcEHFh+5eREM2pOlQ4bRAzwk5RFG0Q3aox/iBMO+90q17N9rU7Rho4ricRbduNFv X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8c042e64-ca26-4b0c-ba6b-08dd1e599cc8 X-MS-Exchange-CrossTenant-AuthSource: DS0PR12MB7726.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Dec 2024 05:13:58.9699 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: UhftufAN+wx2FiE+2lnoGrR1ceyzuPxl4QBEQjMt+WzXfdhrVAPJhwCUGWpLrsJNYOBdrdiupC81SxlPikQvDQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB8936 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241216_211401_624667_88808056 X-CRM114-Status: GOOD ( 18.03 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Prior to any truncation operations file systems call dax_break_mapping() to ensure pages in the range are not under going DMA. Later DAX page-cache entries will be removed by truncate_folio_batch_exceptionals() in the generic page-cache code. However this makes it possible for folios to be removed from the page-cache even though they are still DMA busy if the file-system hasn't called dax_break_mapping(). It also means they can never be waited on in future because FS DAX will lose track of them once the page-cache entry has been deleted. Instead it is better to delete the FS DAX entry when the file-system calls dax_break_mapping() as part of it's truncate operation. This ensures only idle pages can be removed from the FS DAX page-cache and makes it easy to detect if a file-system hasn't called dax_break_mapping() prior to a truncate operation. Signed-off-by: Alistair Popple --- Ideally I think we would move the whole wait-for-idle logic directly into the truncate paths. However this is difficult for a few reasons. Each filesystem needs it's own wait callback, although a new address space operation could address that. More problematic is that the wait-for-idle can fail as the wait is TASK_INTERRUPTIBLE, but none of the generic truncate paths allow for failure. So it ends up being easier to continue to let file systems call this and check that they behave as expected. --- fs/dax.c | 33 +++++++++++++++++++++++++++++++++ fs/xfs/xfs_inode.c | 6 ++++++ include/linux/dax.h | 2 ++ mm/truncate.c | 16 +++++++++++++++- 4 files changed, 56 insertions(+), 1 deletion(-) diff --git a/fs/dax.c b/fs/dax.c index 5462d9d..cd6cca8 100644 --- a/fs/dax.c +++ b/fs/dax.c @@ -845,6 +845,36 @@ int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index) return ret; } +void dax_delete_mapping_range(struct address_space *mapping, + loff_t start, loff_t end) +{ + void *entry; + pgoff_t start_idx = start >> PAGE_SHIFT; + pgoff_t end_idx; + XA_STATE(xas, &mapping->i_pages, start_idx); + + /* If end == LLONG_MAX, all pages from start to till end of file */ + if (end == LLONG_MAX) + end_idx = ULONG_MAX; + else + end_idx = end >> PAGE_SHIFT; + + xas_lock_irq(&xas); + xas_for_each(&xas, entry, end_idx) { + if (!xa_is_value(entry)) + continue; + entry = wait_entry_unlocked_exclusive(&xas, entry); + if (!entry) + continue; + dax_disassociate_entry(entry, mapping, true); + xas_store(&xas, NULL); + mapping->nrpages -= 1UL << dax_entry_order(entry); + put_unlocked_entry(&xas, entry, WAKE_ALL); + } + xas_unlock_irq(&xas); +} +EXPORT_SYMBOL_GPL(dax_delete_mapping_range); + static int wait_page_idle(struct page *page, void (cb)(struct inode *), struct inode *inode) @@ -871,6 +901,9 @@ int dax_break_mapping(struct inode *inode, loff_t start, loff_t end, error = wait_page_idle(page, cb, inode); } while (error == 0); + if (!page) + dax_delete_mapping_range(inode->i_mapping, start, end); + return error; } EXPORT_SYMBOL_GPL(dax_break_mapping); diff --git a/fs/xfs/xfs_inode.c b/fs/xfs/xfs_inode.c index 295730a..4410b42 100644 --- a/fs/xfs/xfs_inode.c +++ b/fs/xfs/xfs_inode.c @@ -2746,6 +2746,12 @@ xfs_mmaplock_two_inodes_and_break_dax_layout( goto again; } + /* + * Normally xfs_break_dax_layouts() would delete the mapping entries as well so + * do that here. + */ + dax_delete_mapping_range(VFS_I(ip2)->i_mapping, 0, LLONG_MAX); + return 0; } diff --git a/include/linux/dax.h b/include/linux/dax.h index f6583d3..ef9e02c 100644 --- a/include/linux/dax.h +++ b/include/linux/dax.h @@ -263,6 +263,8 @@ vm_fault_t dax_iomap_fault(struct vm_fault *vmf, unsigned int order, vm_fault_t dax_finish_sync_fault(struct vm_fault *vmf, unsigned int order, pfn_t pfn); int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index); +void dax_delete_mapping_range(struct address_space *mapping, + loff_t start, loff_t end); int dax_invalidate_mapping_entry_sync(struct address_space *mapping, pgoff_t index); int __must_check dax_break_mapping(struct inode *inode, loff_t start, diff --git a/mm/truncate.c b/mm/truncate.c index 7c304d2..b7f51a6 100644 --- a/mm/truncate.c +++ b/mm/truncate.c @@ -78,8 +78,22 @@ static void truncate_folio_batch_exceptionals(struct address_space *mapping, if (dax_mapping(mapping)) { for (i = j; i < nr; i++) { - if (xa_is_value(fbatch->folios[i])) + if (xa_is_value(fbatch->folios[i])) { + /* + * File systems should already have called + * dax_break_mapping_entry() to remove all DAX + * entries while holding a lock to prevent + * establishing new entries. Therefore we + * shouldn't find any here. + */ + WARN_ON_ONCE(1); + + /* + * Delete the mapping so truncate_pagecache() + * doesn't loop forever. + */ dax_delete_mapping_entry(mapping, indices[i]); + } } goto out; }