Skip to content

Commit 34cf865

Browse files
jankaratytso
authored andcommitted
ext4: fix deadlock when writing in ENOSPC conditions
Akira-san has been reporting rare deadlocks of his machine when running xfstests test 269 on ext4 filesystem. The problem turned out to be in ext4_da_reserve_metadata() and ext4_da_reserve_space() which called ext4_should_retry_alloc() while holding i_data_sem. Since ext4_should_retry_alloc() can force a transaction commit, this is a lock ordering violation and leads to deadlocks. Fix the problem by just removing the retry loops. These functions should just report ENOSPC to the caller (e.g. ext4_da_write_begin()) and that function must take care of retrying after dropping all necessary locks. Reported-and-tested-by: Akira Fujita <[email protected]> Reviewed-by: Zheng Liu <[email protected]> Signed-off-by: Jan Kara <[email protected]> Signed-off-by: "Theodore Ts'o" <[email protected]> Cc: [email protected]
1 parent a67c848 commit 34cf865

File tree

1 file changed

+0
-12
lines changed

1 file changed

+0
-12
lines changed

fs/ext4/inode.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,6 @@ static int ext4_journalled_write_end(struct file *file,
12061206
*/
12071207
static int ext4_da_reserve_metadata(struct inode *inode, ext4_lblk_t lblock)
12081208
{
1209-
int retries = 0;
12101209
struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
12111210
struct ext4_inode_info *ei = EXT4_I(inode);
12121211
unsigned int md_needed;
@@ -1218,7 +1217,6 @@ static int ext4_da_reserve_metadata(struct inode *inode, ext4_lblk_t lblock)
12181217
* in order to allocate nrblocks
12191218
* worse case is one extent per block
12201219
*/
1221-
repeat:
12221220
spin_lock(&ei->i_block_reservation_lock);
12231221
/*
12241222
* ext4_calc_metadata_amount() has side effects, which we have
@@ -1238,10 +1236,6 @@ static int ext4_da_reserve_metadata(struct inode *inode, ext4_lblk_t lblock)
12381236
ei->i_da_metadata_calc_len = save_len;
12391237
ei->i_da_metadata_calc_last_lblock = save_last_lblock;
12401238
spin_unlock(&ei->i_block_reservation_lock);
1241-
if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
1242-
cond_resched();
1243-
goto repeat;
1244-
}
12451239
return -ENOSPC;
12461240
}
12471241
ei->i_reserved_meta_blocks += md_needed;
@@ -1255,7 +1249,6 @@ static int ext4_da_reserve_metadata(struct inode *inode, ext4_lblk_t lblock)
12551249
*/
12561250
static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock)
12571251
{
1258-
int retries = 0;
12591252
struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
12601253
struct ext4_inode_info *ei = EXT4_I(inode);
12611254
unsigned int md_needed;
@@ -1277,7 +1270,6 @@ static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock)
12771270
* in order to allocate nrblocks
12781271
* worse case is one extent per block
12791272
*/
1280-
repeat:
12811273
spin_lock(&ei->i_block_reservation_lock);
12821274
/*
12831275
* ext4_calc_metadata_amount() has side effects, which we have
@@ -1297,10 +1289,6 @@ static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock)
12971289
ei->i_da_metadata_calc_len = save_len;
12981290
ei->i_da_metadata_calc_last_lblock = save_last_lblock;
12991291
spin_unlock(&ei->i_block_reservation_lock);
1300-
if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
1301-
cond_resched();
1302-
goto repeat;
1303-
}
13041292
dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1));
13051293
return -ENOSPC;
13061294
}

0 commit comments

Comments
 (0)