The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] f2fs: only redirty pinned folios in redirty_blocks
@ 2026-07-27 13:04 Wenjie Qi
  2026-08-03  8:38 ` Chao Yu
  2026-08-05 21:20 ` [f2fs-dev] " patchwork-bot+f2fs
  0 siblings, 2 replies; 3+ messages in thread
From: Wenjie Qi @ 2026-07-27 13:04 UTC (permalink / raw)
  To: jaegeuk, chao; +Cc: stable, linux-f2fs-devel, linux-kernel, qiwenjie, qwjhust

redirty_blocks() pins folios with read_cache_folio() and then walks the
same range again with filemap_lock_folio() to redirty them and drop the
references it took.

Commit 5951fee46bef ("f2fs: Use a folio in redirty_blocks()") changed
the second pass to a do/while loop. If read_cache_folio() fails before
anything is pinned, page_idx does not advance but the cleanup loop still
runs once.

If readahead has already populated the failed folio in page cache, that
extra iteration finds it and folio_put_refs(folio, 2) drops one
reference too many. Later drop_caches or reclaim can then report
"BUG: Bad page state".

Only redirty the range that was pinned successfully.

Fixes: 5951fee46bef ("f2fs: Use a folio in redirty_blocks()")
Cc: stable@kernel.org
Assisted-by: Codex:gpt-5.5
Signed-off-by: Wenjie Qi <qiwenjie@xiaomi.com>
---
Observed on the baseline after a FAULT_READ_IO-triggered
F2FS_IOC_COMPRESS_FILE:
  BUG: Bad page state in process bash  pfn:1190b0
  aops:f2fs_dblock_aops ino:5 dentry name(?):"redirty"
  page dumped because: non-NULL mapping
  Call Trace:
   dump_stack_lvl+0x53/0x70
   bad_page+0xd4/0x220
   free_unref_folios+0x72a/0x1320
   folios_put_refs+0x348/0x590
   mapping_try_invalidate+0x236/0x2d0
   drop_pagecache_sb+0x116/0x330
   __iterate_supers+0x183/0x200
   drop_caches_sysctl_handler+0x72/0x110
   proc_sys_call_handler+0x352/0x540
   vfs_write+0x5f8/0xf50
   ksys_write+0xf9/0x1d0
   do_syscall_64+0x5f/0x550
   entry_SYSCALL_64_after_hwframe+0x71/0x79

 fs/f2fs/file.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index c54897a25981..bb89dd738e96 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -4479,7 +4479,7 @@ static int redirty_blocks(struct inode *inode, pgoff_t page_idx, int len)
 		page_idx = folio_next_index(folio);
 	} while (page_len < len);
 
-	do {
+	while (redirty_idx < page_idx) {
 		folio = filemap_lock_folio(mapping, redirty_idx);
 
 		/* It will never fail, when folio has pinned above */
@@ -4492,7 +4492,7 @@ static int redirty_blocks(struct inode *inode, pgoff_t page_idx, int len)
 		redirty_idx = folio_next_index(folio);
 		folio_unlock(folio);
 		folio_put_refs(folio, 2);
-	} while (redirty_idx < page_idx);
+	}
 
 	return ret;
 }
-- 
2.43.0

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] f2fs: only redirty pinned folios in redirty_blocks
  2026-07-27 13:04 [PATCH] f2fs: only redirty pinned folios in redirty_blocks Wenjie Qi
@ 2026-08-03  8:38 ` Chao Yu
  2026-08-05 21:20 ` [f2fs-dev] " patchwork-bot+f2fs
  1 sibling, 0 replies; 3+ messages in thread
From: Chao Yu @ 2026-08-03  8:38 UTC (permalink / raw)
  To: Wenjie Qi, jaegeuk; +Cc: chao, stable, linux-f2fs-devel, linux-kernel, qiwenjie

On 7/27/26 21:04, Wenjie Qi wrote:
> redirty_blocks() pins folios with read_cache_folio() and then walks the
> same range again with filemap_lock_folio() to redirty them and drop the
> references it took.
> 
> Commit 5951fee46bef ("f2fs: Use a folio in redirty_blocks()") changed
> the second pass to a do/while loop. If read_cache_folio() fails before
> anything is pinned, page_idx does not advance but the cleanup loop still
> runs once.
> 
> If readahead has already populated the failed folio in page cache, that
> extra iteration finds it and folio_put_refs(folio, 2) drops one
> reference too many. Later drop_caches or reclaim can then report
> "BUG: Bad page state".
> 
> Only redirty the range that was pinned successfully.
> 
> Fixes: 5951fee46bef ("f2fs: Use a folio in redirty_blocks()")
> Cc: stable@kernel.org
> Assisted-by: Codex:gpt-5.5
> Signed-off-by: Wenjie Qi <qiwenjie@xiaomi.com>

Reviewed-by: Chao Yu <chao@kernel.org>

Thanks,

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [f2fs-dev] [PATCH] f2fs: only redirty pinned folios in redirty_blocks
  2026-07-27 13:04 [PATCH] f2fs: only redirty pinned folios in redirty_blocks Wenjie Qi
  2026-08-03  8:38 ` Chao Yu
@ 2026-08-05 21:20 ` patchwork-bot+f2fs
  1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+f2fs @ 2026-08-05 21:20 UTC (permalink / raw)
  To: Wenjie Qi; +Cc: jaegeuk, chao, linux-kernel, qiwenjie, stable, linux-f2fs-devel

Hello:

This patch was applied to jaegeuk/f2fs.git (dev)
by Jaegeuk Kim <jaegeuk@kernel.org>:

On Mon, 27 Jul 2026 21:04:29 +0800 you wrote:
> redirty_blocks() pins folios with read_cache_folio() and then walks the
> same range again with filemap_lock_folio() to redirty them and drop the
> references it took.
> 
> Commit 5951fee46bef ("f2fs: Use a folio in redirty_blocks()") changed
> the second pass to a do/while loop. If read_cache_folio() fails before
> anything is pinned, page_idx does not advance but the cleanup loop still
> runs once.
> 
> [...]

Here is the summary with links:
  - [f2fs-dev] f2fs: only redirty pinned folios in redirty_blocks
    https://git.kernel.org/jaegeuk/f2fs/c/85171332742e

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-08-05 21:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-27 13:04 [PATCH] f2fs: only redirty pinned folios in redirty_blocks Wenjie Qi
2026-08-03  8:38 ` Chao Yu
2026-08-05 21:20 ` [f2fs-dev] " patchwork-bot+f2fs

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox