[PATCH v11 03/36] mm: Add a bulk end-writeback tool
From: David Howells
Date: Wed Sep 02 2026 - 14:09:50 EST
Add a tool to perform bulk folio_end_writeback() across a series of
contiguous folios. The function takes a beginning and an end file position
and ends any folio that lies wholly in the range, leaving the end position
of the last folio it considered in the iterator state for the filesystem to
use as a hint for when to call again.
As folio_end_writeback() has to access the xarray to clear the marks set on
the entries for the folios involved, this tool provides the opportunity to
merge the two xarray accesses (the iteration and the unmarking) and also do
statistics update in bulk.
Suggested-by: Matthew Wilcox <willy@xxxxxxxxxxxxx>
Signed-off-by: David Howells <dhowells@xxxxxxxxxx>
cc: Matthew Wilcox <willy@xxxxxxxxxxxxx>
cc: Paulo Alcantara <pc@xxxxxxxxxxxxx>
cc: Christoph Hellwig <hch@xxxxxxxxxxxxx>
cc: netfs@xxxxxxxxxxxxxxx
cc: linux-mm@xxxxxxxxx
cc: linux-fsdevel@xxxxxxxxxxxxxxx
---
fs/netfs/write_collect.c | 51 +++++++++++++++++++++++++++++++++++++++
include/linux/writeback.h | 10 ++++++++
mm/page-writeback.c | 45 ++++++++++++++++++++++++++++++++++
3 files changed, 106 insertions(+)
diff --git a/fs/netfs/write_collect.c b/fs/netfs/write_collect.c
index 210eb8f3958d..c5b07b3201d7 100644
--- a/fs/netfs/write_collect.c
+++ b/fs/netfs/write_collect.c
@@ -21,6 +21,57 @@
#define NEED_RETRY 0x10 /* A front op requests retrying */
#define SAW_FAILURE 0x20 /* One stream or hit a permanent failure */
+struct end_writeback_ctrl {
+ struct xa_state xas;
+ uoff_t fend;
+};
+
+/**
+ * end_writeback_iter - End writeback for the folios within the range
+ * @mapping: The pagecache to modify
+ * @from: Pointer to the starting position
+ * @to: The end position (exclusive)
+ * @ctrl: Iterator state
+ * @folio: The last return from this function or NULL if first call
+ *
+ * Remove the writeback mark on folios that are entirely within in the given
+ * range, where @from is included in the range, but @to is excluded from the
+ * range.
+ *
+ * Return: The folio to end writeback upon or NULL if the next folio isn't
+ * wholly within the range. Note that this does not necessarily imply that the
+ * ending is complete. @ctrl->fend is updated to point directly beyond the
+ * folio that was considered and may be negative if cast to loff_t.
+ */
+static
+struct folio *end_writeback_iter(struct address_space *mapping,
+ uoff_t from, uoff_t to,
+ struct end_writeback_ctrl *ctrl,
+ struct folio *folio)
+{
+ if (!folio) {
+ lockdep_assert_in_rcu_read_lock();
+ ctrl->xas = (struct xa_state)
+ __XA_STATE(&mapping->i_pages, from / PAGE_SIZE, 0, 0);
+ folio = xas_find(&ctrl->xas, (to - 1) / PAGE_SIZE);
+ } else {
+ folio_end_writeback(folio);
+ retry:
+ folio = xas_next_entry(&ctrl->xas, (to - 1) / PAGE_SIZE);
+ }
+
+ if (xas_retry(&ctrl->xas, folio))
+ goto retry;
+
+ if (!folio)
+ return NULL;
+
+ ctrl->fend = folio_next_pos(folio);
+ if (ctrl->fend > to)
+ folio = NULL;
+ return folio;
+}
+
static void netfs_dump_request(const struct netfs_io_request *rreq)
{
pr_err("Request R=%08x r=%d fl=%lx or=%x e=%ld\n",
diff --git a/include/linux/writeback.h b/include/linux/writeback.h
index b749a9a5a5ee..4f34cbc964f6 100644
--- a/include/linux/writeback.h
+++ b/include/linux/writeback.h
@@ -358,6 +358,16 @@ bool wb_over_bg_thresh(struct bdi_writeback *wb);
struct folio *writeback_iter(struct address_space *mapping,
struct writeback_control *wbc, struct folio *folio, int *error);
+/* end_writeback_iter() state. */
+struct end_writeback_ctrl {
+ struct xa_state xas; /* The xarray walker */
+ uoff_t fend; /* The next file pos after the folio last considered. */
+};
+
+struct folio *end_writeback_iter(struct address_space *mapping,
+ struct end_writeback_ctrl *ctrl,
+ uoff_t from, uoff_t to, struct folio *folio);
+
int do_writepages(struct address_space *mapping, struct writeback_control *wbc);
void writeback_set_ratelimit(void);
void tag_pages_for_writeback(struct address_space *mapping,
diff --git a/mm/page-writeback.c b/mm/page-writeback.c
index 6c9c7ba89b8a..632b0616eceb 100644
--- a/mm/page-writeback.c
+++ b/mm/page-writeback.c
@@ -2546,6 +2546,51 @@ struct folio *writeback_iter(struct address_space *mapping,
}
EXPORT_SYMBOL_GPL(writeback_iter);
+/**
+ * end_writeback_iter - End writeback for the folios within the range
+ * @mapping: The pagecache to modify
+ * @ctrl: Iterator state
+ * @from: Pointer to the starting position
+ * @to: The end position (exclusive)
+ * @folio: The last return from this function or NULL if first call
+ *
+ * Remove the writeback mark on folios that are entirely within in the given
+ * range, where @from is included in the range, but @to is excluded from the
+ * range.
+ *
+ * Return: The folio to end writeback upon or NULL if the next folio isn't
+ * wholly within the range. Note that this does not necessarily imply that the
+ * ending is complete. @ctrl->fend is updated to point directly beyond the
+ * folio that was considered and may be negative if cast to loff_t.
+ */
+struct folio *end_writeback_iter(struct address_space *mapping,
+ struct end_writeback_ctrl *ctrl,
+ uoff_t from, uoff_t to, struct folio *folio)
+{
+ if (!folio) {
+ lockdep_assert_in_rcu_read_lock();
+ ctrl->xas = (struct xa_state)
+ __XA_STATE(&mapping->i_pages, from / PAGE_SIZE, 0, 0);
+ folio = xas_find(&ctrl->xas, (to - 1) / PAGE_SIZE);
+ } else {
+ folio_end_writeback(folio);
+ retry:
+ folio = xas_next_entry(&ctrl->xas, (to - 1) / PAGE_SIZE);
+ }
+
+ if (xas_retry(&ctrl->xas, folio))
+ goto retry;
+
+ if (!folio)
+ return NULL;
+
+ ctrl->fend = folio_next_pos(folio);
+ if (ctrl->fend > to)
+ folio = NULL;
+ return folio;
+}
+EXPORT_SYMBOL_GPL(end_writeback_iter);
+
int do_writepages(struct address_space *mapping, struct writeback_control *wbc)
{
int ret;