Skip to contentSkip to conversation

ext4: fix extent status tree race in writeback error recovery path

PATCH Applied Eric Whitney 2 participants 2 messages Jun 15, 2022 – Jul 14, 2022 ext4 file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ext4: fix extent status tree race in writeback error recovery path

A race can occur in the unlikely event ext4 is unable to allocate a physical cluster for a delayed allocation in a bigalloc file system during writeback. Failure to allocate a cluster forces error recovery that includes a call to mpage_release_unused_pages(). That function removes any corresponding delayed allocated blocks from the extent status tree. If a new delayed write is in progress on the same cluster simultaneously, resulting in the addition of an new extent containing one or more blocks in that cluster to the extent status tree, delayed block accounting can be thrown off if that delayed write then encounters a similar cluster allocation failure during future writeback.

Write lock the i_data_sem in mpage_release_unused_pages() to fix this problem. Ext4's block/cluster accounting code for bigalloc relies on i_data_sem for mutual exclusion, as is found in the delayed write path, and the locking in mpage_release_unused_pages() is missing.

---
 fs/ext4/inode.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/ext4/inode.c +7 -0
index 3dce7d058985..95a7a90b3942 100644
@@ -1571,7 +1571,14 @@ static void mpage_release_unused_pages(struct mpage_da_data *mpd,
 		ext4_lblk_t start, last;
 		start = index << (PAGE_SHIFT - inode->i_blkbits);
 		last = end << (PAGE_SHIFT - inode->i_blkbits);
+
+		/*
+		 * avoid racing with extent status tree scans made by
+		 * ext4_insert_delayed_block()
+		 */
+		down_write(&EXT4_I(inode)->i_data_sem);
 		ext4_es_remove_extent(inode, start, last - start + 1);
+		up_write(&EXT4_I(inode)->i_data_sem);
 	}
 
 	pagevec_init(&pvec);
Signature · 1 line
2.30.2
maintainer Applied
#1 lore raw
Quoted 11 lines · Eric Whitney

A race can occur in the unlikely event ext4 is unable to allocate a physical cluster for a delayed allocation in a bigalloc file system during writeback. Failure to allocate a cluster forces error recovery that includes a call to mpage_release_unused_pages(). That function removes any corresponding delayed allocated blocks from the extent status tree. If a new delayed write is in progress on the same cluster simultaneously, resulting in the addition of an new extent containing one or more blocks in that cluster to the extent status tree, delayed block accounting can be thrown off if that delayed write then encounters a similar cluster allocation failure during future writeback.

Quoted 1 line

[...]

Applied, thanks!

[1/1] ext4: fix extent status tree race in writeback error recovery path

      commit: 8e469e57340049d4735b71660d29bd4fd3ae1607

Best regards,

Signature · 1 line
Theodore Ts'o <[email protected]>