From patchwork Thu Mar 28 16:34:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13608976 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 9BAC6CD11DD for ; Thu, 28 Mar 2024 16:36:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 318776B00AA; Thu, 28 Mar 2024 12:36:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2A0306B00AB; Thu, 28 Mar 2024 12:36:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 11AD26B00AC; Thu, 28 Mar 2024 12:36:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id E38C46B00AA for ; Thu, 28 Mar 2024 12:36:55 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id B37E41402CA for ; Thu, 28 Mar 2024 16:36:55 +0000 (UTC) X-FDA: 81947001990.03.BA0FE27 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf27.hostedemail.com (Postfix) with ESMTP id BDBD240020 for ; Thu, 28 Mar 2024 16:36:52 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Mp9m+Eoe; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf27.hostedemail.com: domain of dhowells@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711643812; a=rsa-sha256; cv=none; b=r5MpkJdkWiZHyHkNoWg7imaPWq4Dm7QdqReaXBtYmdSjzIXrd35UrL3EkHrHdZ8fGa1pf2 qAg1Gin2dHfGPKmLq1THpnU5hWrbFoe+z0fkB2Nu529CWGoLhtYI/TGCWAeuM6AeI8RpkZ Qb29oY/jnb2SuOAp2jNdKwRtz3Yz6fc= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Mp9m+Eoe; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf27.hostedemail.com: domain of dhowells@redhat.com designates 170.10.133.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=1711643812; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=lykrnbha/6S87zBZnY0wz/f7lqvJK6Se2csvQWUZWMQ=; b=Vf/qbWyOaIFExa/CpidCBkiCx+IbGmeIUCN5pMXbeQhgVi6KOXVCg/reUsdRiQO0F4MJMP 8f9V3UYk3QQOJ9SLkA2BzZk+Yt+KrNRkFhzwaM+RfLJTWk3TfxwYu4UzZzzVb4Wc2GdeaX 8tgH4mjQKzZDVrFpreM0nEzrVY5utj0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711643812; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lykrnbha/6S87zBZnY0wz/f7lqvJK6Se2csvQWUZWMQ=; b=Mp9m+EoehUrAHH3rnJcEmYeMTlMMOwHhvfyw6P4yk6qxe6qn675R5z+t7sVMQn2rpecLfw e5sZ8sh2FLP1QIZwrxnOPnBErbva/qibo4ciV/bcGA4wsFrIieXS4d2I0S2YpfEjQXj/MY ihrMPlnEDjjnDMvqQq24bLQDtWnhI2s= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-616-zNPlVeWXO8uUH_TirPk-BA-1; Thu, 28 Mar 2024 12:36:44 -0400 X-MC-Unique: zNPlVeWXO8uUH_TirPk-BA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (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 37CBF1C05147; Thu, 28 Mar 2024 16:36:43 +0000 (UTC) Received: from warthog.procyon.org.com (unknown [10.42.28.146]) by smtp.corp.redhat.com (Postfix) with ESMTP id F2813C37A87; Thu, 28 Mar 2024 16:36:38 +0000 (UTC) From: David Howells To: Christian Brauner , Jeff Layton , Gao Xiang , Dominique Martinet Cc: David Howells , Matthew Wilcox , Steve French , Marc Dionne , Paulo Alcantara , Shyam Prasad N , Tom Talpey , Eric Van Hensbergen , Ilya Dryomov , netfs@lists.linux.dev, linux-cachefs@redhat.com, linux-afs@lists.infradead.org, linux-cifs@vger.kernel.org, linux-nfs@vger.kernel.org, ceph-devel@vger.kernel.org, v9fs@lists.linux.dev, linux-erofs@lists.ozlabs.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Miklos Szeredi , Trond Myklebust , Christoph Hellwig , Andrew Morton , Alexander Viro , Christian Brauner , devel@lists.orangefs.org Subject: [PATCH 09/26] mm: Provide a means of invalidation without using launder_folio Date: Thu, 28 Mar 2024 16:34:01 +0000 Message-ID: <20240328163424.2781320-10-dhowells@redhat.com> In-Reply-To: <20240328163424.2781320-1-dhowells@redhat.com> References: <20240328163424.2781320-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: BDBD240020 X-Stat-Signature: bjxdcf4sbycrabi8a7uayghwnz4bc8cw X-HE-Tag: 1711643812-929038 X-HE-Meta: U2FsdGVkX1+YoiiD8OmY3BT3oNxwV3Gb2p1b+z0UJheY+IA7s3Pp6p503dkWW02Nek3UB/6ixIWJyR058FxKMG4MXMrhh78HZeK6p7Pp1+3n1Tek3WvkNtLIZ0tNverOFzJEu9RT8qlBaVVJnIsA4QkWLtR1AASPps0LrnxPlNgtjwjilW7VSv+UsieQFbSBwnQn0j8UlSQtmC0ZHoitL73ArVt8wXKCxMZmTIIcHlvFljpnAfngF/4KCuqrav7iVgv4x8qRWMGkpUIJlMnQcaiy/SBorE0t2+BuETCAO0rVd3oe3R4QmV0ENQsSXwEjNlxgP/t9VHliYFzOcFF4MBnNjRSLVtglUfd9zCKQIFlj6ykPuCys7u/VAJ3l16wTpW4CbwAtyKiXPgxlfHSCTtsKC7e1T5KTKmCakLDOCkzL69IQSHEH3xLMu0JKLAYnRRAB248Z5cMeVTRGAvGZDgfZiRFrsOjiHkTnRizp76OJoyDRFnBf2o3T7YzKiVFDPPXp/19HGt/SB5SCUg2vkxlUD+U4QHWYxxvz2WIRg2yi/U60f+Gkhh1XPVRnHZKFkRrSjSSl7BBKdtD5rGSwOgCLqz1W46ozi3M/yK+kcMZzCahgR7/dyrogEVwYFQM69HISR0j40UoiZgLjQfMrFanDMD5JR/LPXW/hB4z8oy3YYNT6xpthZcnwVn2Q9XzTrl9n+unEWKQTguHCHyyp2urz+OIdQTOhlCnVfUmBtraWavZOf/8Nb98epjbsUYXEvwJ3VBDDkMiliOZvZr2DGaW6IrAX9BDSNmzDo0cJ6bI7VhtP9vHsVJ486J107mKWS5wTWhcBj+t4OTKb49aL0kgvb4+rR0/rpWnqfhnyZQ+q3mK284DXzz2Rgae7nbIjolq8c3Xj1+E9a4AzWpw4kCsHQjyndwtYHDdsFPJMCN7+l8m6I84V5H0fe/2dZPLlvqXglegXfkTpyiEL0yR zEKU/PSv 4xRpRAt+bFCtWDhV7+DX1Kvf7OBJIZY/GU8tt11nb6nt44qTVHaNPJ7TgU9ae4YBFA2yI2ehBHj06wj0wyKssdM4QrwtWAmqVyH9u65GboVCJtjZp3nlCzc59ZF9KukTwoRNLpioYml4K2JmoXzV9hd6qVYPZexWK8kx4WZ0qWFtXFSM0WO68kl7fGb2Y9twynfGolMrdnU9ihsTbZGTwaKipTUOGx5aITqqxAKJyCYrzXzKtTFZFqe4QUlxvtaK4/rdJq3q+Nf8b4bxfuKFF2iYf9AZwLhR/hC0/dv/+5u31IvQHG3oTfbGW0fWBOmK0np4CZDTTKr08Ifv1DrZQQlWkUksKcTCmN1832bhdaN3793e56JZtCSGwUzttnZb6CCeR3dHJkQRf+yKxAZBRt+yoxJ6ZDybghDTALeYKywpEWk++Nl2QOFCGM0pcguvbf9HM0dK2h2Fmb75KGDWDSosJOjSH80lfaC7ErUfF5kggY8A6H5wxL4NZjabuhNrNQkIHqPD7Vbw4EUowu7BGD9QQfqKcg8AKGqMKTgdH95+3e/0/kJyzUYo76saDwvmiEWlHUxdOfYtx22YGlHUeoeEgHc5oI8F+R4M0jrlfur6IhhYs+C9sZMvn6MV4A7qRT8Uz 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: 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 | 46 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 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..087f685107a5 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -4134,6 +4134,52 @@ 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; + + 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