Skip to contentSkip to conversation

netfs: fix ENOMEM handling in netfs_writepages() to drain all dirty folios

PATCH In review Yun Zhou 4 participants 8 messages Jul 7 – Jul 23

Subsystems (2)
lore
Share & tools
lore

Yun Zhou

author
#0 lore raw

PATCH 1/1 netfs: fix ENOMEM handling in netfs_writepages() to drain all dirty folios

When netfs_create_write_req() fails with -ENOMEM in netfs_writepages(), the couldnt_start error path redirties and unlocks the first folio, then calls writeback_iter() expecting it to return NULL. However, if the mapping contains multiple dirty folios, writeback_iter() returns the next one, triggering WARN_ON_ONCE(folio != NULL).

This can be reproduced via 9p (cache=loose) with shared mmap writes and fault injection (fail_nth), where v9fs_mmap_vm_close() triggers writeback on a mapping with multiple dirty folios during mmap overlap.

Fix this by looping over all remaining dirty folios in the ENOMEM path, redirtying and unlocking each one. This ensures all folios taken by the writeback iterator are properly released, and they will be retried on the next writeback cycle when memory is available.

Reported-by [email protected]
Fixes ac5f95ac5d6d ("netfs: Fix writeback error handling")
---
 fs/netfs/write_issue.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
fs/netfs/write_issue.c +5 -4
index f2761c99795a..5aa47128f7e7 100644
@@ -597,10 +597,11 @@ int netfs_writepages(struct address_space *mapping,
 
 couldnt_start:
 	if (error == -ENOMEM) {
-		folio_redirty_for_writepage(wbc, folio);
-		folio_unlock(folio);
-		folio = writeback_iter(mapping, wbc, folio, &error);
-		WARN_ON_ONCE(folio != NULL);
+		/* Redirty all dirty folios and let writeback retry later. */
+		do {
+			folio_redirty_for_writepage(wbc, folio);
+			folio_unlock(folio);
+		} while ((folio = writeback_iter(mapping, wbc, folio, &error)));
 	} else {
 		netfs_kill_dirty_pages(mapping, wbc, folio);
 	}
Signature · 1 line
2.43.0
author Tested-by [email protected] on v1 1/1
#1 lore raw

https://lore.kernel.org/all/[email protected]/

Quoted 43 lines · Yun Zhou

When netfs_create_write_req() fails with -ENOMEM in netfs_writepages(), the couldnt_start error path redirties and unlocks the first folio, then calls writeback_iter() expecting it to return NULL. However, if the mapping contains multiple dirty folios, writeback_iter() returns the next one, triggering WARN_ON_ONCE(folio != NULL).

This can be reproduced via 9p (cache=loose) with shared mmap writes and fault injection (fail_nth), where v9fs_mmap_vm_close() triggers writeback on a mapping with multiple dirty folios during mmap overlap.

Fix this by looping over all remaining dirty folios in the ENOMEM path, redirtying and unlocking each one. This ensures all folios taken by the writeback iterator are properly released, and they will be retried on the next writeback cycle when memory is available.

Reported-by [email protected]
Fixes ac5f95ac5d6d ("netfs: Fix writeback error handling")
---
  fs/netfs/write_issue.c | 9 +++++----
  1 file changed, 5 insertions(+), 4 deletions(-)
fs/netfs/write_issue.c +5 -4
index f2761c99795a..5aa47128f7e7 100644
@@ -597,10 +597,11 @@ int netfs_writepages(struct address_space *mapping,
 
  couldnt_start:
  	if (error == -ENOMEM) {
-		folio_redirty_for_writepage(wbc, folio);
-		folio_unlock(folio);
-		folio = writeback_iter(mapping, wbc, folio, &error);
-		WARN_ON_ONCE(folio != NULL);
+		/* Redirty all dirty folios and let writeback retry later. */
+		do {
+			folio_redirty_for_writepage(wbc, folio);
+			folio_unlock(folio);
+		} while ((folio = writeback_iter(mapping, wbc, folio, &error)));
  	} else {
  		netfs_kill_dirty_pages(mapping, wbc, folio);
  	}
author
#2 lore raw
In reply to Yun Zhou

Friendly ping.

Quoted 43 lines · Yun Zhou

When netfs_create_write_req() fails with -ENOMEM in netfs_writepages(), the couldnt_start error path redirties and unlocks the first folio, then calls writeback_iter() expecting it to return NULL. However, if the mapping contains multiple dirty folios, writeback_iter() returns the next one, triggering WARN_ON_ONCE(folio != NULL).

This can be reproduced via 9p (cache=loose) with shared mmap writes and fault injection (fail_nth), where v9fs_mmap_vm_close() triggers writeback on a mapping with multiple dirty folios during mmap overlap.

Fix this by looping over all remaining dirty folios in the ENOMEM path, redirtying and unlocking each one. This ensures all folios taken by the writeback iterator are properly released, and they will be retried on the next writeback cycle when memory is available.

Reported-by [email protected]
Fixes ac5f95ac5d6d ("netfs: Fix writeback error handling")
---
  fs/netfs/write_issue.c | 9 +++++----
  1 file changed, 5 insertions(+), 4 deletions(-)
fs/netfs/write_issue.c +5 -4
index f2761c99795a..5aa47128f7e7 100644
@@ -597,10 +597,11 @@ int netfs_writepages(struct address_space *mapping,
 
  couldnt_start:
  	if (error == -ENOMEM) {
-		folio_redirty_for_writepage(wbc, folio);
-		folio_unlock(folio);
-		folio = writeback_iter(mapping, wbc, folio, &error);
-		WARN_ON_ONCE(folio != NULL);
+		/* Redirty all dirty folios and let writeback retry later. */
+		do {
+			folio_redirty_for_writepage(wbc, folio);
+			folio_unlock(folio);
+		} while ((folio = writeback_iter(mapping, wbc, folio, &error)));
  	} else {
  		netfs_kill_dirty_pages(mapping, wbc, folio);
  	}
maintainer
#3 lore raw

Is writeback_iter() missing some error handling? -- was Re: [PATCH] netfs: fix ENOMEM handling in netfs_writepages() to drain all dirty folios

In reply to Yun Zhou
Quoted 24 lines · Yun Zhou

When netfs_create_write_req() fails with -ENOMEM in netfs_writepages(), the couldnt_start error path redirties and unlocks the first folio, then calls writeback_iter() expecting it to return NULL. However, if the mapping contains multiple dirty folios, writeback_iter() returns the next one, triggering WARN_ON_ONCE(folio != NULL).

This can be reproduced via 9p (cache=loose) with shared mmap writes and fault injection (fail_nth), where v9fs_mmap_vm_close() triggers writeback on a mapping with multiple dirty folios during mmap overlap.

Fix this by looping over all remaining dirty folios in the ENOMEM path, redirtying and unlocking each one. This ensures all folios taken by the writeback iterator are properly released, and they will be retried on the next writeback cycle when memory is available.

...

fs/netfs/write_issue.c +0 -2
@@ -597,10 +597,11 @@ int netfs_writepages(struct address_space *mapping,
 
 couldnt_start:
 	if (error == -ENOMEM) {
-		folio_redirty_for_writepage(wbc, folio);
-		folio_unlock(folio);

fs/netfs/write_issue.c int netfs_writepages(struct address_space *mapping, · line 607

Quoted 10 lines
fs/netfs/write_issue.c +5 -2
-		folio = writeback_iter(mapping, wbc, folio, &error);
-		WARN_ON_ONCE(folio != NULL);
+		/* Redirty all dirty folios and let writeback retry later. */
+		do {
+			folio_redirty_for_writepage(wbc, folio);
+			folio_unlock(folio);
+		} while ((folio = writeback_iter(mapping, wbc, folio, &error)));
 	} else {
 		netfs_kill_dirty_pages(mapping, wbc, folio);
 	}

This seems like the wrong thing to do - or, at least, a bug in the writeback_iter() API. Getting something like ENOMEM would seem to indicate that all subsequent writeback_iter() calls in this loop are pointless as it looks like the sequence will just go { lock, undirty, dirty, unlock } for each folio.

David

author
#4 lore raw

Re: Is writeback_iter() missing some error handling? -- was Re: [PATCH] netfs: fix ENOMEM handling in netfs_writepages() to drain all dirty folios

Quoted 17 lines · David Howells
Quoted 4 lines · Yun Zhou
  couldnt_start:
       if (error == -ENOMEM) {
fs/netfs/write_issue.c +0 -2
-             folio_redirty_for_writepage(wbc, folio);
-             folio_unlock(folio);

fs/netfs/write_issue.c int netfs_writepages(struct address_space *mapping, · line 607

Quoted 10 lines
fs/netfs/write_issue.c +5 -2
-             folio = writeback_iter(mapping, wbc, folio, &error);
-             WARN_ON_ONCE(folio != NULL);
+             /* Redirty all dirty folios and let writeback retry later. */
+             do {
+                     folio_redirty_for_writepage(wbc, folio);
+                     folio_unlock(folio);
+             } while ((folio = writeback_iter(mapping, wbc, folio, &error)));
       } else {
               netfs_kill_dirty_pages(mapping, wbc, folio);
       }
Quoted 6 lines

This seems like the wrong thing to do - or, at least, a bug in the writeback_iter() API. Getting something like ENOMEM would seem to indicate that all subsequent writeback_iter() calls in this loop are pointless as it looks like the sequence will just go { lock, undirty, dirty, unlock } for each folio.

Thanks for the feedback. I understand the concern — the loop is indeed doing mechanical { redirty, unlock } work that ideally writeback_iter() could handle internally when the caller has nothing meaningful to do with the folios.

However, given the current writeback_iter() API contract ("callers must keep calling until it returns NULL"), I'm not sure there's a cleaner way to handle this without a VFS/mm level change. And in the meantime, syzbot is hitting this — the WARN fires and any folio returned beyond the first is left locked.

Would it be acceptable to keep this as a minimal stopgap fix for the immediate bug, with the understanding that it can be cleaned up later if writeback_iter() gains an early-abort mechanism? Happy to adjust the approach if you have something else in mind.

BR,
Yun

#5 lore raw

Re: Is writeback_iter() missing some error handling? -- was Re: [PATCH] netfs: fix ENOMEM handling in netfs_writepages() to drain all dirty folios

In reply to David Howells
Quoted 5 lines · David Howells

This seems like the wrong thing to do - or, at least, a bug in the writeback_iter() API. Getting something like ENOMEM would seem to indicate that all subsequent writeback_iter() calls in this loop are pointless as it looks like the sequence will just go { lock, undirty, dirty, unlock } for each folio.

I'd find it hard to call a bug, when it's intended behaviour:

* For integrity writeback we have to keep going until we have
* written all the folios we tagged for writeback above, even if
* we run past wbc->nr_to_write or encounter errors.
* We stash away the first error we encounter in wbc->saved_err
* so that it can be retrieved when we're done. This is because
* the file system may still have state to clear for each folio. *
* For background writeback we exit as soon as we run past
* wbc->nr_to_write or encounter the first error.

Why are you getting ENOMEM for a sync() writeback anyway? Is this some stupid error injection?

#6 lore raw
In reply to Yun Zhou
Quoted 2 lines · Yun Zhou

When netfs_create_write_req() fails with -ENOMEM in netfs_writepages(), the couldnt_start error path redirties and unlocks the first folio,

writeback must use mempools to back resoures, or __GFP_NOFAIL if it can't for some reason. -ENOMEM must not happend in writeback paths or your toast. So please fix the cause of this error and not the symptoms.

author
#7 lore raw
Quoted 9 lines · Christoph Hellwig
Quoted 2 lines · Yun Zhou

When netfs_create_write_req() fails with -ENOMEM in netfs_writepages(), the couldnt_start error path redirties and unlocks the first folio,

writeback must use mempools to back resoures, or __GFP_NOFAIL if it can't for some reason. -ENOMEM must not happend in writeback paths or your toast. So please fix the cause of this error and not the symptoms.

Thanks for the review. You're right - writeback should not fail with ENOMEM.

I'll follow your suggestion and use __GFP_NOFAIL for the allocation in the writeback path. The ENOMEM here comes from rolling_buffer_init() which uses plain GFP_NOFS for its folio_queue allocation.

Using mempools would be the more robust approach, but it requires adding a new mempool, modifying the rolling_buffer_init() signature and all its callers, plus the init/destroy lifecycle - a much larger change. __GFP_NOFAIL is a minimal fix that achieves the same goal for the writeback path.

Will send v2 shortly.