Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "Matthew Wilcox (Oracle)" <willy@infradead.org>
To: Christian Brauner <brauner@kernel.org>
Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>,
	Jan Kara <jack@suse.cz>, Chris Mason <clm@fb.com>,
	David Sterba <dsterba@suse.com>,
	Miklos Szeredi <miklos@szeredi.hu>,
	Trond Myklebust <trondmy@kernel.org>,
	Anna Schumaker <anna@kernel.org>,
	Mike Marshall <hubcap@omnibond.com>,
	Martin Brandenburg <martin@omnibond.com>,
	Alexander Viro <viro@zeniv.linux.org.uk>,
	linux-fsdevel@vger.kernel.org, linux-mm@kvack.org,
	linux-block@vger.kernel.org, linux-btrfs@vger.kernel.org,
	fuse-devel@lists.linux.dev, linux-nfs@vger.kernel.org,
	devel@lists.orangefs.org, Pavel Begunkov <asml.silence@gmail.com>
Subject: [PATCH 1/7] filemap: Export filemap_invalidate_pages() to modules
Date: Thu, 20 Aug 2026 20:33:34 +0100	[thread overview]
Message-ID: <20260820193343.3852967-2-willy@infradead.org> (raw)
In-Reply-To: <20260820193343.3852967-1-willy@infradead.org>

This is a better API for filesystems to use than
invalidate_inode_pages2() / invalidate_inode_pages2_range().
However, the 'nowait' argument is unnecessary for them.  It's also
wrongly implemented as it will call invalidate_inode_pages2_range()
even after filemap_range_has_page() returns false.

Move the filemap_range_has_page() call into the two existing callers
and add kernel-doc.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
 block/ioctl.c           | 14 ++++++++++----
 include/linux/pagemap.h |  2 +-
 mm/filemap.c            | 42 +++++++++++++++++++++++++++--------------
 3 files changed, 39 insertions(+), 19 deletions(-)

diff --git a/block/ioctl.c b/block/ioctl.c
index 3d4ea1537457..db5238b817b4 100644
--- a/block/ioctl.c
+++ b/block/ioctl.c
@@ -908,10 +908,16 @@ static int blkdev_cmd_discard(struct io_uring_cmd *cmd,
 	if (err)
 		return err;
 
-	err = filemap_invalidate_pages(bdev->bd_mapping, start,
-					start + len - 1, nowait);
-	if (err)
-		return err;
+	if (nowait) {
+		if (filemap_range_has_page(bdev->bd_mapping, start,
+					start + len - 1))
+			return -EAGAIN;
+	} else {
+		err = filemap_invalidate_pages(bdev->bd_mapping, start,
+					start + len - 1);
+		if (err)
+			return err;
+	}
 
 	while (true) {
 		bio = blk_alloc_discard_bio(bdev, &sector, &nr_sects, gfp);
diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index 2c3718d592d6..ed99c8ab196a 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -33,7 +33,7 @@ int invalidate_inode_pages2_range(struct address_space *mapping,
 int kiocb_invalidate_pages(struct kiocb *iocb, size_t count);
 void kiocb_invalidate_post_direct_write(struct kiocb *iocb, size_t count);
 int filemap_invalidate_pages(struct address_space *mapping,
-			     loff_t pos, loff_t end, bool nowait);
+		loff_t pos, loff_t end);
 
 int write_inode_now(struct inode *, int sync);
 int filemap_fdatawrite(struct address_space *);
diff --git a/mm/filemap.c b/mm/filemap.c
index d721986d5f46..fedb521d773f 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -2911,20 +2911,27 @@ int kiocb_write_and_wait(struct kiocb *iocb, size_t count)
 }
 EXPORT_SYMBOL_GPL(kiocb_write_and_wait);
 
+/**
+ * filemap_invalidate_pages - Invalidate pages from the page cache
+ * @mapping: Address space to invalidate
+ * @pos: First byte to invalidate
+ * @end: Last byte (inclusive) to invalidate
+ *
+ * Invalidates the folios containing @pos and @end from the page cache
+ * (as well as all folios between them), so may remove more pages from
+ * the page cache than you ask for.
+ *
+ * Context: May sleep.  Caller may wish to hold mapping_invalidate_lock to
+ * prevent new pages being instantiated in this range.
+ * Return: 0 on success or negative errno.
+ */
 int filemap_invalidate_pages(struct address_space *mapping,
-			     loff_t pos, loff_t end, bool nowait)
+		loff_t pos, loff_t end)
 {
-	int ret;
+	int ret = filemap_write_and_wait_range(mapping, pos, end);
 
-	if (nowait) {
-		/* we could block if there are any pages in the range */
-		if (filemap_range_has_page(mapping, pos, end))
-			return -EAGAIN;
-	} else {
-		ret = filemap_write_and_wait_range(mapping, pos, end);
-		if (ret)
-			return ret;
-	}
+	if (ret)
+		return ret;
 
 	/*
 	 * After a write we want buffered reads to be sure to go to disk to get
@@ -2935,14 +2942,21 @@ int filemap_invalidate_pages(struct address_space *mapping,
 	return invalidate_inode_pages2_range(mapping, pos >> PAGE_SHIFT,
 					     end >> PAGE_SHIFT);
 }
+EXPORT_SYMBOL_GPL(filemap_invalidate_pages);
 
 int kiocb_invalidate_pages(struct kiocb *iocb, size_t count)
 {
 	struct address_space *mapping = iocb->ki_filp->f_mapping;
+	loff_t end = iocb->ki_pos + count - 1;
+
+	if (iocb->ki_flags & IOCB_NOWAIT) {
+		/* we could block if there are any pages in the range */
+		if (filemap_range_has_page(mapping, iocb->ki_pos, end))
+			return -EAGAIN;
+		return 0;
+	}
 
-	return filemap_invalidate_pages(mapping, iocb->ki_pos,
-					iocb->ki_pos + count - 1,
-					iocb->ki_flags & IOCB_NOWAIT);
+	return filemap_invalidate_pages(mapping, iocb->ki_pos, end);
 }
 EXPORT_SYMBOL_GPL(kiocb_invalidate_pages);
 
-- 
2.47.3



  reply	other threads:[~2026-08-20 19:34 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-20 19:33 [PATCH 0/7] Remove aops->launder_folio Matthew Wilcox (Oracle)
2026-08-20 19:33 ` Matthew Wilcox (Oracle) [this message]
2026-08-20 19:33 ` [PATCH 2/7] fuse: Use filemap_invalidate_pages() Matthew Wilcox (Oracle)
2026-08-20 20:37   ` Bernd Schubert
2026-08-24  9:05   ` Miklos Szeredi
2026-08-24 13:29     ` Matthew Wilcox
2026-08-24 13:49       ` Miklos Szeredi
2026-08-24 18:17         ` Matthew Wilcox
2026-08-24 19:33           ` Miklos Szeredi
2026-08-24 20:47             ` Matthew Wilcox
2026-08-25  7:08               ` Miklos Szeredi
2026-08-20 19:33 ` [PATCH 3/7] btrfs: " Matthew Wilcox (Oracle)
2026-08-24 21:57   ` Boris Burkov
2026-08-20 19:33 ` [PATCH 4/7] nfs: " Matthew Wilcox (Oracle)
2026-08-20 19:33 ` [PATCH 5/7] orangefs: " Matthew Wilcox (Oracle)
2026-08-20 19:33 ` [PATCH 6/7] orangefs: Remove launder_folio implementation Matthew Wilcox (Oracle)
2026-08-20 19:33 ` [PATCH 7/7] Remove folio_launder() Matthew Wilcox (Oracle)
2026-08-25 15:50 ` [PATCH 0/7] Remove aops->launder_folio Jan Kara

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260820193343.3852967-2-willy@infradead.org \
    --to=willy@infradead.org \
    --cc=anna@kernel.org \
    --cc=asml.silence@gmail.com \
    --cc=brauner@kernel.org \
    --cc=clm@fb.com \
    --cc=devel@lists.orangefs.org \
    --cc=dsterba@suse.com \
    --cc=fuse-devel@lists.linux.dev \
    --cc=hubcap@omnibond.com \
    --cc=jack@suse.cz \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=martin@omnibond.com \
    --cc=miklos@szeredi.hu \
    --cc=trondmy@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox