Skip to contentSkip to conversation

f2fs: drop pending discard commands before reserving device alias

PATCH Applied Daeho Jeong 4 participants 4 messages Sep 3 – Sep 8

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 f2fs: drop pending discard commands before reserving device alias

From: Daeho Jeong <[email protected]>

When reserving a device alias via f2fs_ioc_reserve_dev_alias(), f2fs_reserve_device_alias() bulk-marks all blocks in the target device range as valid in SIT.

However, if the device was previously in the released state, stale pending discard commands covering that range may still exist in dcc->root. When f2fs_issue_discard_thread later processes those commands, __check_sit_bitmap() detects valid blocks in the discard range and triggers a kernel BUG().

To fix this:
1. Introduce f2fs_drop_discard_cmd_range() to traverse the discard rbtree, drop all pending D_PREP discard commands in the range,
and wait for any in-flight discard bios under dcc->cmd_lock.
2. Call f2fs_drop_discard_cmd_range() in f2fs_ioc_reserve_dev_alias() before f2fs_reserve_device_alias().

Fixes eae3faf210bd ("f2fs: support dynamic reserve/release for device aliasing")

---
v4: limit discard commands to drop per lock round and call cond_resched().
v3: introduce cur to prevent 32-bit overflow.
v2: fix use-after-free and 32-bit overflow issues.

---
 fs/f2fs/f2fs.h    |  3 +++
 fs/f2fs/file.c    |  1 +
 fs/f2fs/segment.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+)
fs/f2fs/f2fs.h +3 -0
index 511286432483..dda70234bf2d 100644
@@ -466,6 +466,7 @@ struct discard_entry {
 #define MAX_PLIST_NUM		512
 #define plist_idx(blk_num)	((blk_num) >= MAX_PLIST_NUM ?		\
 					(MAX_PLIST_NUM - 1) : ((blk_num) - 1))
+#define MAX_DISCARD_DROP_COUNT	512
 
 enum {
 	D_PREP,			/* initial */
@@ -4122,6 +4123,8 @@ void f2fs_reserve_device_alias(struct f2fs_sb_info *sbi, block_t addr,
 bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
 int f2fs_start_discard_thread(struct f2fs_sb_info *sbi);
 void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
+void f2fs_drop_discard_cmd_range(struct f2fs_sb_info *sbi,
+					block_t start, block_t len);
 void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
 bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi, bool need_check);
 void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
fs/f2fs/file.c +1 -0
index 29cf82d02c77..0b2d173d9597 100644
@@ -3855,6 +3855,7 @@ static int f2fs_ioc_reserve_dev_alias(struct file *filp)
 	write_unlock(&et->lock);
 	clear_inode_flag(inode, FI_NO_EXTENT);
 
+	f2fs_drop_discard_cmd_range(sbi, ei.blk, ei.len);
 	f2fs_reserve_device_alias(sbi, ei.blk, ei.len);
 
 	i_size_write(inode, (loff_t)ei.len << sbi->log_blocksize);
fs/f2fs/segment.c +54 -0

60 lines not on this page · raw message

Signature · 1 line
2.55.0.970.g62bdec98f9-goog
maintainer Reviewed-by on v4 1/1
#1 lore raw
Quoted 19 lines · Daeho Jeong

From: Daeho Jeong <[email protected]>

When reserving a device alias via f2fs_ioc_reserve_dev_alias(), f2fs_reserve_device_alias() bulk-marks all blocks in the target device range as valid in SIT.

However, if the device was previously in the released state, stale pending discard commands covering that range may still exist in dcc->root. When f2fs_issue_discard_thread later processes those commands, __check_sit_bitmap() detects valid blocks in the discard range and triggers a kernel BUG().

To fix this:
1. Introduce f2fs_drop_discard_cmd_range() to traverse the discard rbtree, drop all pending D_PREP discard commands in the range,
and wait for any in-flight discard bios under dcc->cmd_lock.
2. Call f2fs_drop_discard_cmd_range() in f2fs_ioc_reserve_dev_alias() before f2fs_reserve_device_alias().

Quoted 4 lines
Fixes eae3faf210bd ("f2fs: support dynamic reserve/release for device aliasing")

Thanks,

#3 lore raw
In reply to Daeho Jeong
This patch was applied to jaegeuk/f2fs.git (dev) by Jaegeuk Kim <[email protected]>:

Hello:

This patch was applied to jaegeuk/f2fs.git (dev)
by Jaegeuk Kim <[email protected]>:

Quoted 6 lines · On Thu, 3 Sep 2026 09:10:31 -0700 you wrote:

From: Daeho Jeong <[email protected]>

When reserving a device alias via f2fs_ioc_reserve_dev_alias(), f2fs_reserve_device_alias() bulk-marks all blocks in the target device range as valid in SIT.

Quoted 7 lines

However, if the device was previously in the released state, stale pending discard commands covering that range may still exist in dcc->root. When f2fs_issue_discard_thread later processes those commands, __check_sit_bitmap() detects valid blocks in the discard range and triggers a kernel BUG().

[...]

Here is the summary with links:
- [f2fs-dev,v4] f2fs: drop pending discard commands before reserving device alias

    https://git.kernel.org/jaegeuk/f2fs/c/5200186c8cf1

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html