f2fs: drop pending discard commands before reserving device alias
Subsystems (2)
PATCH 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().
--- fs/f2fs/f2fs.h | 2 ++ fs/f2fs/file.c | 1 + fs/f2fs/segment.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+)
fs/f2fs/f2fs.h +2 -0
@@ -4122,6 +4122,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
@@ -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 +45 -0
51 lines not on this page · raw message
Signature · 1 line
2.55.0.897.gb25b4bd76c-goog
Hi Daeho,
There is a use-after-free in the new helper:
if (dc->state == D_PREP) {
start = dc->di.lstart + dc->di.len;
__remove_discard_cmd(sbi, dc);
continue;
}
...
if (dc)
__wait_one_discard_bio(sbi, dc);__remove_discard_cmd() frees a D_PREP command. If the last command in the requested range is D_PREP, dc remains non-NULL when the loop exits, so the following __wait_one_discard_bio() dereferences the freed object.
The range arithmetic also needs care. block_t end = start + len and start = dc->di.lstart + dc->di.len can wrap at the 32-bit block-address limit, and the RB-tree lookup still compares against a 32-bit command end. A range ending at 2^32 blocks would therefore be skipped.
Thanks,
Wenjie
_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Quoted 14 lines · Wenjie Qi
Hi Daeho,
There is a use-after-free in the new helper:
if (dc->state == D_PREP) { start = dc->di.lstart + dc->di.len; __remove_discard_cmd(sbi, dc); continue; } ... if (dc) __wait_one_discard_bio(sbi, dc);
Quoted 8 lines
__remove_discard_cmd()frees aD_PREPcommand. If the last command in the requested range isD_PREP, dc remains non-NULL when the loop exits, so the following__wait_one_discard_bio()dereferences the freed object.The range arithmetic also needs care.
block_tend = start + len and start =dc->di.lstart+dc->di.lencan wrap at the 32-bit block-address limit, and the RB-tree lookup still compares against a 32-bit command end. A range ending at 2^32 blocks would therefore be skipped.
Thanks for letting me know.
I will fix it.
Quoted 9 lines
Thanks,
Wenjie_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel