[01/12] writeback: Factor out writeback_finish()
Commit Message
Instead of having a 'done' variable that controls the nested loops,
have a writeback_finish() that can be returned directly. This involves
keeping more things in writeback_control, but it's just moving stuff
allocated on the stack to being allocated slightly earlier on the stack.
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
include/linux/writeback.h | 6 ++++
mm/page-writeback.c | 74 +++++++++++++++++++++------------------
2 files changed, 45 insertions(+), 35 deletions(-)
Comments
On Mon, Jun 26, 2023 at 06:35:10PM +0100, Matthew Wilcox (Oracle) wrote:
> + struct folio_batch fbatch;
> + pgoff_t done_index;
> + int err;
> +
I think this really needs a comment that it should only be used for
the writeback iterator. In fact this whole structure could use a lot
more comments on what should / can be set by the caller, and what is
internal.
> + unsigned range_whole:1; /* entire file */
Same here.
@@ -11,6 +11,7 @@
#include <linux/flex_proportions.h>
#include <linux/backing-dev-defs.h>
#include <linux/blk_types.h>
+#include <linux/pagevec.h>
struct bio;
@@ -52,6 +53,10 @@ struct writeback_control {
loff_t range_start;
loff_t range_end;
+ struct folio_batch fbatch;
+ pgoff_t done_index;
+ int err;
+
enum writeback_sync_modes sync_mode;
unsigned for_kupdate:1; /* A kupdate writeback */
@@ -59,6 +64,7 @@ struct writeback_control {
unsigned tagged_writepages:1; /* tag-and-write to avoid livelock */
unsigned for_reclaim:1; /* Invoked from the page allocator */
unsigned range_cyclic:1; /* range_start is cyclic */
+ unsigned range_whole:1; /* entire file */
unsigned for_sync:1; /* sync(2) WB_SYNC_ALL writeback */
unsigned unpinned_fscache_wb:1; /* Cleared I_PINNING_FSCACHE_WB */
@@ -2360,6 +2360,24 @@ void tag_pages_for_writeback(struct address_space *mapping,
}
EXPORT_SYMBOL(tag_pages_for_writeback);
+static int writeback_finish(struct address_space *mapping,
+ struct writeback_control *wbc, bool done)
+{
+ folio_batch_release(&wbc->fbatch);
+
+ /*
+ * If we hit the last page and there is more work to be done:
+ * wrap the index back to the start of the file for the next
+ * time we are called.
+ */
+ if (wbc->range_cyclic && !done)
+ wbc->done_index = 0;
+ if (wbc->range_cyclic || (wbc->range_whole && wbc->nr_to_write > 0))
+ mapping->writeback_index = wbc->done_index;
+
+ return wbc->err;
+}
+
/**
* write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
* @mapping: address space structure to write
@@ -2395,18 +2413,12 @@ int write_cache_pages(struct address_space *mapping,
struct writeback_control *wbc, writepage_t writepage,
void *data)
{
- int ret = 0;
- int done = 0;
int error;
- struct folio_batch fbatch;
int nr_folios;
pgoff_t index;
pgoff_t end; /* Inclusive */
- pgoff_t done_index;
- int range_whole = 0;
xa_mark_t tag;
- folio_batch_init(&fbatch);
if (wbc->range_cyclic) {
index = mapping->writeback_index; /* prev offset */
end = -1;
@@ -2414,7 +2426,7 @@ int write_cache_pages(struct address_space *mapping,
index = wbc->range_start >> PAGE_SHIFT;
end = wbc->range_end >> PAGE_SHIFT;
if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
- range_whole = 1;
+ wbc->range_whole = 1;
}
if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) {
tag_pages_for_writeback(mapping, index, end);
@@ -2422,20 +2434,24 @@ int write_cache_pages(struct address_space *mapping,
} else {
tag = PAGECACHE_TAG_DIRTY;
}
- done_index = index;
- while (!done && (index <= end)) {
+
+ wbc->done_index = index;
+ folio_batch_init(&wbc->fbatch);
+ wbc->err = 0;
+
+ while (index <= end) {
int i;
nr_folios = filemap_get_folios_tag(mapping, &index, end,
- tag, &fbatch);
+ tag, &wbc->fbatch);
if (nr_folios == 0)
break;
for (i = 0; i < nr_folios; i++) {
- struct folio *folio = fbatch.folios[i];
+ struct folio *folio = wbc->fbatch.folios[i];
- done_index = folio->index;
+ wbc->done_index = folio->index;
folio_lock(folio);
@@ -2488,14 +2504,14 @@ int write_cache_pages(struct address_space *mapping,
folio_unlock(folio);
error = 0;
} else if (wbc->sync_mode != WB_SYNC_ALL) {
- ret = error;
- done_index = folio->index +
- folio_nr_pages(folio);
- done = 1;
- break;
+ wbc->err = error;
+ wbc->done_index = folio->index +
+ folio_nr_pages(folio);
+ return writeback_finish(mapping,
+ wbc, true);
}
- if (!ret)
- ret = error;
+ if (!wbc->err)
+ wbc->err = error;
}
/*
@@ -2505,26 +2521,14 @@ int write_cache_pages(struct address_space *mapping,
* we tagged for writeback prior to entering this loop.
*/
if (--wbc->nr_to_write <= 0 &&
- wbc->sync_mode == WB_SYNC_NONE) {
- done = 1;
- break;
- }
+ wbc->sync_mode == WB_SYNC_NONE)
+ return writeback_finish(mapping, wbc, true);
}
- folio_batch_release(&fbatch);
+ folio_batch_release(&wbc->fbatch);
cond_resched();
}
- /*
- * If we hit the last page and there is more work to be done: wrap
- * back the index back to the start of the file for the next
- * time we are called.
- */
- if (wbc->range_cyclic && !done)
- done_index = 0;
- if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
- mapping->writeback_index = done_index;
-
- return ret;
+ return writeback_finish(mapping, wbc, false);
}
EXPORT_SYMBOL(write_cache_pages);