From patchwork Wed Mar 27 17:55:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13607189 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 C58D6C47DD9 for ; Wed, 27 Mar 2024 17:56:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3106F6B0083; Wed, 27 Mar 2024 13:56:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2C08E6B0092; Wed, 27 Mar 2024 13:56:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 161236B0096; Wed, 27 Mar 2024 13:56:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id E69A06B0083 for ; Wed, 27 Mar 2024 13:55:59 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 4FD04804BB for ; Wed, 27 Mar 2024 17:55:59 +0000 (UTC) X-FDA: 81943572438.28.A5EE70D Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf01.hostedemail.com (Postfix) with ESMTP id 6A65E40023 for ; Wed, 27 Mar 2024 17:55:57 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=dDBAYAsB; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf01.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711562157; 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=SuhewvNUfoJvZQzmhzwfMCeMaHKTh082FqaMnijMnf4=; b=qqiBwTa++1NhkEzmxq8DfH7iFA2ESvCb8qD6g53hWTV90cbUuWzj2QZpINZfUu7QDRJl86 uVoKHykojYKDKOUChgWDnK64R/ZvbjweiwCExE/kN36vDhW40msR5T8Lbd2wwv9Qwc+EzR to2wu+iAejJ1G0LhOA3ll4AwlWR7W+c= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=dDBAYAsB; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf01.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711562157; a=rsa-sha256; cv=none; b=kFZXVGyFke9S5cE8r4s7de0v3BDHJYnlZAa6ZEndEM3jC4VwiJXROotqCkBVEEk9Bmogon GD/A0bRuSfCT4hK5bcMxNz8XxwxIX7UnZInABabXXt6/ktcwswhMqHx1Ca1mETFBEp/xoE TVvuw+fmHTzDgRyU7ZwSi7vJtM+yMDk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711562156; h=from:from: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; bh=SuhewvNUfoJvZQzmhzwfMCeMaHKTh082FqaMnijMnf4=; b=dDBAYAsBCRahQyJD2HrJPMzg1OA/amjRpRVKIX/g4uupoX8yjiyz+lzpe1ldQm7PEtlB4p j9Woy5KUliUZG9CkBqE6xXLiGBYNyD0QKSsaXJInrBwVrBZYzi+7tzmsSwJJsrl8LR6gTD MXAfhEdUFPhPuzeyqCEvFZELtBpTjoc= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-187-3HLvNSycN1CY-_0DA3XqSg-1; Wed, 27 Mar 2024 13:55:53 -0400 X-MC-Unique: 3HLvNSycN1CY-_0DA3XqSg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 440148007A2; Wed, 27 Mar 2024 17:55:52 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.42.28.146]) by smtp.corp.redhat.com (Postfix) with ESMTP id F0D7F2024517; Wed, 27 Mar 2024 17:55:49 +0000 (UTC) Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 From: David Howells In-Reply-To: <2318298.1711551844@warthog.procyon.org.uk> References: <2318298.1711551844@warthog.procyon.org.uk> To: dhowells@redhat.com Cc: Matthew Wilcox , Miklos Szeredi , Trond Myklebust , Christoph Hellwig , Andrew Morton , Alexander Viro , Christian Brauner , Jeff Layton , linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, netfs@lists.linux.dev, v9fs@lists.linux.dev, linux-afs@lists.infradead.org, ceph-devel@vger.kernel.org, linux-cifs@vger.kernel.org, linux-nfs@vger.kernel.org, devel@lists.orangefs.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH v2] mm, netfs: Provide a means of invalidation without using launder_folio MIME-Version: 1.0 Content-ID: <2506006.1711562145.1@warthog.procyon.org.uk> Date: Wed, 27 Mar 2024 17:55:45 +0000 Message-ID: <2506007.1711562145@warthog.procyon.org.uk> X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-Rspam-User: X-Stat-Signature: xmbdw9zripm5ykbnxx9wu4pangoxxprw X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 6A65E40023 X-HE-Tag: 1711562157-461494 X-HE-Meta: U2FsdGVkX19uvXbeHWzNk6C7AclV3YDeD8wFMcKYDEkWUurQLavZhpUWADt3+3drAZHHGsR9kHYedtPNh1KWe7LAxonqYu+o/r3kQQP5IDyy/24LoEeAz/pJ0G8ODNi5miUoaqYUt+uhBdM3I1haezeeOyXBjuMtejjXxiDBaJ92xZELnepOdBaBmbOxu0hSgoGqG7el1PZ+e9fBF3UFPi7tOH5HKXCx0gVYBM3V60VUX+Cxd1y9XQzwSbjgjlJfm4e6kv4FyV0t+uKOTnpJ1VW2LAdBchO7A9oovco/vP5QKRwypZUNnW+qfnv6hEPN0qUuvoQ4Wfk+qwagCO40aeRNjp+3Bw+T/2MMQDwrnYRbHciBVihHOZ+5MGHGjIIBdvJGkICiQvDXdnEDjdX1lNkXtj+LdGaD5CPHl1t6VtMrz7KJ8j83KM33BioH7TkNOAoJ7X6JOZzvfejO3mKNbC0Xxdxplq2DEhk74kpqyBG7U90H6HY5Br4Ci6tvgBEmqaBJTHiud3ftyPuKaj20GIkz7kGS1d2AALxkqDzGLxkTNr8qcoU27Bu8bNBusC9Z1VmSt2PadLwdyswP2xijq8eI3QuUyU/+FuX1RL0JotDfc+yp036kLbDs1yBY0TcPe60y9p5IWlNv7lkaPhLlqPsDQ8BjBzodF7OzVZnzgW09xi+JPsFCQ+zc+RoRwS9k69Nc90Nqxs0nybKS9kvD7+2z/E565RySXauhGS2K1DjvJHcntUNpZgScWWNoIVcXtZftgBdZyqIgacWXzDMJXyXyFxTOhzpXpX9AUr5bvH6w8bB2mvfktVg3s5XUpRlt9xhc4p8gg7jJSEdivUKDbapdDjXTLN8vQuidE6sw0qB/SYiuFbsJINffA4m3jUQOr/cC0Ct8R0lALQwKkCp1SFDeErYofdORQKOtvHc+y2pdaWvvJXiQeV+cClpbYbrcByR0y3YJBKooh4VXXPT Nf73wFoJ 8hJ94JEl+c69GwMg3/LXByQjnhRWMYNJn2SWwTKTbNUajwgQmyY/tM4mnxZb08fKSqxTD0lPGLiT7EZhjO4rXlvfWzwuWiWApDkj1JzO+cAJGrDX4UN8anLiQifmQm/wIWkYoemhxMlXsi//zG0gUePSjHeMRp0VRGizCYC17dHY79hx6XyhMDQ7eMtQzthgJcp65bs+navOsbJC7/yDJhoowtuG59P4BXRABFkCqhJxTmb6Q43IqWBOhtUpTM+WS45UpJRkGsbzXsTNdhjNCngHcotkYndLdMIALKPs1nIUZ/n2Hht9X5PBTQGiEamNdAHpNlXuC4JlaLHR6h98TO8PWMGhI/gly20T/ggMwoxXiN2WrtbbnLeC6Ce/LoKPfrGSfpEvc+0wBrNBYAcEmHty5wVnujQ4Ekm2reQxA290NpKPWUrl0pnsYVvUA+yl6t+i0+Z3GD1FuW+DFUGY1MOaD8ykeHeArJJ0CvbbwZZwllXhUkwLtX+9GIkSNEV/oDB+DNPcv6rd7XewcxZVKE6UrOHU85Un40a06R9Ryg6p88COmzKP7a/MLY6ANvJxjsXYte/Zr1XkdSLn63Uwx8OsAKTuPAOqgG1piTyT2mYF74m4uYutzAEYXEuMBopNH5JQHzSmWYMqBVpE= 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: mm, netfs: Provide a means of invalidation without using launder_folio Implement a replacement for launder_folio. The key feature of invalidate_inode_pages2() is that it locks each folio individually, unmaps it to prevent mmap'd accesses interfering and calls the ->launder_folio() address_space op to flush it. This has problems: firstly, each folio is written individually as one or more small writes; secondly, adjacent folios cannot be added so easily into the laundry; thirdly, it's yet another op to implement. Instead, use the invalidate lock to cause anyone wanting to add a folio to the inode to wait, then unmap all the folios if we have mmaps, then, conditionally, use ->writepages() to flush any dirty data back and then discard all pages. The invalidate lock prevents ->read_iter(), ->write_iter() and faulting through mmap all from adding pages for the duration. Signed-off-by: David Howells cc: Matthew Wilcox cc: Miklos Szeredi cc: Trond Myklebust cc: Christoph Hellwig cc: Andrew Morton cc: Alexander Viro cc: Christian Brauner cc: Jeff Layton cc: linux-mm@kvack.org cc: linux-fsdevel@vger.kernel.org cc: netfs@lists.linux.dev cc: v9fs@lists.linux.dev cc: linux-afs@lists.infradead.org cc: ceph-devel@vger.kernel.org cc: linux-cifs@vger.kernel.org cc: linux-nfs@vger.kernel.org cc: devel@lists.orangefs.org --- include/linux/pagemap.h | 1 + mm/filemap.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index 2df35e65557d..4eb3d4177a53 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -40,6 +40,7 @@ int filemap_fdatawait_keep_errors(struct address_space *mapping); int filemap_fdatawait_range(struct address_space *, loff_t lstart, loff_t lend); int filemap_fdatawait_range_keep_errors(struct address_space *mapping, loff_t start_byte, loff_t end_byte); +int filemap_invalidate_inode(struct inode *inode, bool flush); static inline int filemap_fdatawait(struct address_space *mapping) { diff --git a/mm/filemap.c b/mm/filemap.c index 25983f0f96e3..98f439bedb44 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -4134,6 +4134,54 @@ bool filemap_release_folio(struct folio *folio, gfp_t gfp) } EXPORT_SYMBOL(filemap_release_folio); +/** + * filemap_invalidate_inode - Invalidate/forcibly write back an inode's pagecache + * @inode: The inode to flush + * @flush: Set to write back rather than simply invalidate. + * + * Invalidate all the folios on an inode, possibly writing them back first. + * Whilst the operation is undertaken, the invalidate lock is held to prevent + * new folios from being installed. + */ +int filemap_invalidate_inode(struct inode *inode, bool flush) +{ + struct address_space *mapping = inode->i_mapping; + + if (!mapping || !mapping->nrpages) + goto out; + + /* Prevent new folios from being added to the inode. */ + filemap_invalidate_lock(mapping); + + if (!mapping->nrpages) + goto unlock; + + /* Assume there are probably PTEs only if there are mmaps. */ + if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))) + unmap_mapping_pages(mapping, 0, ULONG_MAX, false); + + /* Write back the data if we're asked to. */ + if (flush) { + struct writeback_control wbc = { + .sync_mode = WB_SYNC_ALL, + .nr_to_write = LONG_MAX, + .range_start = 0, + .range_end = LLONG_MAX, + }; + + filemap_fdatawrite_wbc(mapping, &wbc); + } + + /* Wait for writeback to complete on all folios and discard. */ + truncate_inode_pages_range(mapping, 0, LLONG_MAX); + +unlock: + filemap_invalidate_unlock(mapping); +out: + return filemap_check_errors(mapping); +} +EXPORT_SYMBOL(filemap_invalidate_inode); + #ifdef CONFIG_CACHESTAT_SYSCALL /** * filemap_cachestat() - compute the page cache statistics of a mapping