Received: by 2002:ab2:6991:0:b0:1f2:fff1:ace7 with SMTP id v17csp217847lqo; Wed, 27 Mar 2024 11:00:52 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXgXe7Z5mYUnF0pr+MH+EVaWDzzIPCfqsuBFY2XKOlgMpv1C1StRQNCY6mia8IgypCGFBWIOAtNFR33SD9A8AzDLDhiY2J779yIHGSsXg== X-Google-Smtp-Source: AGHT+IEbLYlfmJ/aZ4UO9oEzUm2UtGLd9SuO59Ypy9Soa6AxI7ELaYiEwzSsULX7ZIbxoZlTy2lo X-Received: by 2002:a05:6830:1302:b0:6e6:c3f3:a9a9 with SMTP id p2-20020a056830130200b006e6c3f3a9a9mr512854otq.11.1711562451695; Wed, 27 Mar 2024 11:00:51 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711562451; cv=pass; d=google.com; s=arc-20160816; b=DJBToSH3JCxEzRBoZeY9lko/on47yyThbWOqtpjgor/pkKRlfCmc9anxUFCPa/6gFA fjj9kl1dDXxXflEn/elaeIbkr9OGreUVnQWhKTrZR3/3aq2Ep8s14Ow+tef3gjxPMBgv 1VGHoFkdaK+mNoYTy1Ds9IWRYJYk+XOC3ogiqh98mFN3sc6znmKdlGFskOLcXMzaYtR/ 4PRUL6CgEWHbvcEBvIhnt2UhsDjSttpemZ06BEN4RIWCUKatGJgQ0jijgEsZ9N58JgOm 566c5bSxWrmOMrzKHFocaih03HNbPLBuZi17FmJC5GXEmIr1hLzoVyaB+ei3KPV3GtyA 6w1A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:content-transfer-encoding:content-id:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:subject:cc:to :references:in-reply-to:from:organization:dkim-signature; bh=SuhewvNUfoJvZQzmhzwfMCeMaHKTh082FqaMnijMnf4=; fh=y4nXOL324iDMB3Gz9kD6Fn4dkB58G2pqud5KriSOx3k=; b=At+GVqJDZEnmnJAAjT7wRhQeyybtsaV/7eb2OQR8emU41Jx35Z/1qqzKojvJqOt/2n VbW98Q5Yjo9ggMtkpjDLpau/c+DaPHcvX4keOv6eRIagp3kwK1+tLC9wlMbsPA7WjkhR zila9vZOdrMssbLvyb+fDBuLXUrlxuH/YX8MDECMjrQUnHUAb14E/cCAEzaUiC+J4GK3 jfCD9i5pO3En3uayU8SRqiCn39McCwCUeliCmEb2ZCbXIRJ4+18txQO/Jd5cKX7DqVxv FHHT3pS0vcZ0N4DzrzR9Ivf51Uyq7KVLPg5lDbjFQBTNhAFdEFxbCyhumhqUd3NnBMbr EIKA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=BsqsimYB; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-121769-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-121769-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id a19-20020a05620a02f300b0078a00f12cbesi10352370qko.274.2024.03.27.11.00.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 11:00:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-121769-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=BsqsimYB; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-121769-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-121769-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 41D4D1C32467 for ; Wed, 27 Mar 2024 18:00:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2938915217C; Wed, 27 Mar 2024 17:56:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="BsqsimYB" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 61E321514D8 for ; Wed, 27 Mar 2024 17:55:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711562160; cv=none; b=omHfFAjHn5/fe+WHr4T0GVD9RMqDGMJayNVuyQwVtViEUMP+JnWWCrw9f8RsJuUl1y2vEQEpqoHMVZCyDoqnqSl9lz4/axZp9o4ePshG/gurouwGTnuoojpwz/7BLf7LOHQBzeasEesHH+FqGX2OWEyHujIiNWQ8H1tgBI31V1E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711562160; c=relaxed/simple; bh=NPhvozRKEg9kfHBXKt48lnApC6KVUGFOSrrqbu9Hq/8=; h=From:In-Reply-To:References:To:Cc:Subject:MIME-Version: Content-Type:Date:Message-ID; b=uutz6umMunyrpIyGHWSx4bRg/xSEQwUbIH3MFANziwKP+i+Pr6Nz3YOpiqHZR+gBnB3JKuaKR+CR+Q0rSrPHwSxslSYehfjbdhqG4u130dPKw6RFz+Z7dR4pXKGeJH9tXnU/Lulg/K+34PUPhsB2Hsw5RxYLPiETuSmnBj1owBw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=BsqsimYB; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711562157; 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=BsqsimYBv1UAbll3H/FQAD2IrIGdXXYtNFnpuI+z2yXqJS6dgNaJvu7Ib4+NBJzPCFkb3k MI7vSuriTV4Jxgm4y6cxVfnNQSsLyBS0vgrO/q2wZPA2MkDs/3jYsglNTiPgpy9JtEEH+G 7Fn8VitYzbxaXuKv5bCGvuRyNARL7ps= 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 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2506006.1711562145.1@warthog.procyon.org.uk> Content-Transfer-Encoding: quoted-printable 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 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 folio= s cannot be added so easily into the laundry; thirdly, it's yet another op t= o 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 p= agecache + * @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 firs= t. + * Whilst the operation is undertaken, the invalidate lock is held to pre= vent + * new folios from being installed. + */ +int filemap_invalidate_inode(struct inode *inode, bool flush) +{ + struct address_space *mapping =3D 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 =3D { + .sync_mode =3D WB_SYNC_ALL, + .nr_to_write =3D LONG_MAX, + .range_start =3D 0, + .range_end =3D 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