Skip to contentSkip to conversation

afs: Fix dangling folio ref counts in writeback

PATCH In review Vishal Moola (Oracle) 3 participants 8 messages Jun 7, 2023 – Jun 16, 2023 Afs filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 afs: Fix dangling folio ref counts in writeback

Commit acc8d8588cb7 converted afs_writepages_region() to write back a folio batch. If writeback needs rescheduling, the function exits without dropping the references to the folios in fbatch. This patch fixes that.

This has only been compile tested.

Fixes acc8d8588cb7 ("afs: convert afs_writepages_region() to use filemap_get_folios_tag()")
---
 fs/afs/write.c | 1 +
 1 file changed, 1 insertion(+)
fs/afs/write.c +1 -0
index c822d6006033..a724228e4d94 100644
@@ -764,6 +764,7 @@ static int afs_writepages_region(struct address_space *mapping,
 					if (skips >= 5 || need_resched()) {
 						*_next = start;
 						_leave(" = 0 [%llx]", *_next);
+						folio_batch_release(&fbatch);
 						return 0;
 					}
 					skips++;
Signature · 1 line
2.40.1
#1 lore raw

PATCH 1/1 afs: Fix waiting for writeback then skipping folio

Commit acc8d8588cb7 converted afs_writepages_region() to write back a folio batch. The function waits for writeback to a folio, but then proceeds to the rest of the batch without trying to write that folio again. This patch fixes has it attempt to write the folio again.

This has only been compile tested.

Fixes acc8d8588cb7 ("afs: convert afs_writepages_region() to use filemap_get_folios_tag()")
---
 fs/afs/write.c | 2 ++
 1 file changed, 2 insertions(+)
fs/afs/write.c +2 -0
index a724228e4d94..18ccb613dff8 100644
@@ -731,6 +731,7 @@ static int afs_writepages_region(struct address_space *mapping,
 			 * (changing page->mapping to NULL), or even swizzled
 			 * back from swapper_space to tmpfs file mapping
 			 */
+try_again:
 			if (wbc->sync_mode != WB_SYNC_NONE) {
 				ret = folio_lock_killable(folio);
 				if (ret < 0) {
@@ -757,6 +758,7 @@ static int afs_writepages_region(struct address_space *mapping,
 #ifdef CONFIG_AFS_FSCACHE
 					folio_wait_fscache(folio);
 #endif
+					goto try_again;
 				} else {
 					start += folio_size(folio);
 				}
Signature · 1 line
2.40.1
#2 lore raw

Re: [PATCH] afs: Fix waiting for writeback then skipping folio

Quoted 6 lines · On Wed, 7 Jun 2023 13:41:20 -0700 "Vishal Moola (Oracle)"…

Commit acc8d8588cb7 converted afs_writepages_region() to write back a folio batch. The function waits for writeback to a folio, but then proceeds to the rest of the batch without trying to write that folio again. This patch fixes has it attempt to write the folio again.

This has only been compile tested.

This seems fairly serious?

Quoted 11 lines
fs/afs/write.c +1 -0
@@ -731,6 +731,7 @@ static int afs_writepages_region(struct address_space *mapping,
 			 * (changing page->mapping to NULL), or even swizzled
 			 * back from swapper_space to tmpfs file mapping
 			 */
+try_again:
 			if (wbc->sync_mode != WB_SYNC_NONE) {
 				ret = folio_lock_killable(folio);
 				if (ret < 0) {

fs/afs/write.c static int afs_writepages_region(struct address_space *mapping, · line 764

Quoted 7 lines
fs/afs/write.c +1 -0
@@ -757,6 +758,7 @@ static int afs_writepages_region(struct address_space *mapping,
 #ifdef CONFIG_AFS_FSCACHE
 					folio_wait_fscache(folio);
 #endif
+					goto try_again;
 				} else {
 					start += folio_size(folio);
 				}

From my reading, we'll fail to write out the dirty data. Presumably not easily observable, as it will get written out again later on. But we're also calling afs_write_back_from_locked_folio() with an unlocked folio, which might cause mayhem.

So I'm suspecting that a cc:stable is needed. David, could you please take a look and perhaps retest?

Thanks.

maintainer
#3 lore raw

Re: [PATCH] afs: Fix waiting for writeback then skipping folio

Quoted 8 lines · Andrew Morton
Quoted 6 lines

Commit acc8d8588cb7 converted afs_writepages_region() to write back a folio batch. The function waits for writeback to a folio, but then proceeds to the rest of the batch without trying to write that folio again. This patch fixes has it attempt to write the folio again.

This has only been compile tested.

This seems fairly serious?

We will try to write the again later, but sync()/fsync() might now have skipped it.

Quoted 2 lines

From my reading, we'll fail to write out the dirty data. Presumably not easily observable, as it will get written out again later on.

As it's a network filesystem, interactions with third parties could cause apparent corruption. Closing a file will flush it - but if there's a simultaneous op of some other kind, a bit of a flush or a sync may get missed and the copy visible to another user be temporarily missing that bit.

Quoted 2 lines

But we're also calling afs_write_back_from_locked_folio() with an unlocked folio, which might cause mayhem.

Without this patch, you mean? There's a "continue" statement that should send us back to the top of the loop before we get as far as afs_write_back_from_locked_folio() - and then the folio_unlock() there would go bang.

David

#4 lore raw

Re: [PATCH] afs: Fix waiting for writeback then skipping folio

Quoted 22 lines · On Fri, 16 Jun 2023 23:43:02 +0100 David Howells <[email protected]> wrote:
Quoted 8 lines · Andrew Morton
Quoted 6 lines

Commit acc8d8588cb7 converted afs_writepages_region() to write back a folio batch. The function waits for writeback to a folio, but then proceeds to the rest of the batch without trying to write that folio again. This patch fixes has it attempt to write the folio again.

This has only been compile tested.

This seems fairly serious?

We will try to write the again later, but sync()/fsync() might now have skipped it.

Quoted 2 lines

From my reading, we'll fail to write out the dirty data. Presumably not easily observable, as it will get written out again later on.

As it's a network filesystem, interactions with third parties could cause apparent corruption. Closing a file will flush it - but if there's a simultaneous op of some other kind, a bit of a flush or a sync may get missed and the copy visible to another user be temporarily missing that bit.

Quoted 8 lines
Quoted 2 lines

But we're also calling afs_write_back_from_locked_folio() with an unlocked folio, which might cause mayhem.

Without this patch, you mean? There's a "continue" statement that should send us back to the top of the loop before we get as far as afs_write_back_from_locked_folio() - and then the folio_unlock() there would go bang.

Well, what I'm really asking is the thing I ask seven times a day:

- what are the end-user visible effects of the bug

- should be fix be backported into earlier kernels

maintainer
#5 lore raw

Re: [PATCH] afs: Fix waiting for writeback then skipping folio

Quoted 3 lines · Andrew Morton

Well, what I'm really asking is the thing I ask seven times a day:

- what are the end-user visible effects of the bug

A third party might see an incomplete flush after they've done a sync - which amounts to temporary file corruption.

Quoted 1 line

- should be fix be backported into earlier kernels

Yes.

David

maintainer
#6 lore raw

Re: [PATCH] afs: Fix waiting for writeback then skipping folio

In reply to Vishal Moola (Oracle)

fs/afs/write.c static int afs_writepages_region(struct address_space *mapping, · line 763

Quoted 3 lines · Vishal Moola (Oracle)
fs/afs/write.c +1 -0
+					goto try_again;
 				} else {
 					start += folio_size(folio);

The "else" is then redundant.

David

maintainer
#7 lore raw
In reply to Vishal Moola (Oracle)

fs/afs/write.c static int afs_writepages_region(struct address_space *mapping, · line 767

Quoted 4 lines · Vishal Moola (Oracle)
fs/afs/write.c +1 -0
 					if (skips >= 5 || need_resched()) {
 						*_next = start;
 						_leave(" = 0 [%llx]", *_next);
+						folio_batch_release(&fbatch);

This should go before the _leave().

fs/afs/write.c static int afs_writepages_region(struct address_space *mapping, · line 769

Quoted 2 lines
fs/afs/write.c +0 -0
 						return 0;
 					}

Looks okay otherwise.

David