Skip to contentSkip to conversation

ext4: skip extra isize expansion during mount to prevent deadlock

PATCH Applied Yun Zhou 2 participants 4 messages Jun 23 – Jul 23 ext4 file system

lore
Share & tools
lore

Yun Zhou

author
#0 lore raw

PATCH 1/2 ext4: skip extra isize expansion during mount to prevent deadlock

ext4_try_to_expand_extra_isize() is called from __ext4_mark_inode_dirty() while holding an active jbd2 handle. During mount (!SB_ACTIVE), the expand path may move xattrs to external blocks and release ea_inodes via iput(). When !SB_ACTIVE, iput() calls write_inode_now() which acquires s_writepages_rwsem, creating a circular lock dependency:

s_writepages_rwsem --> jbd2_handle --> xattr_sem --> s_writepages_rwsem

This can be triggered via:

ext4_process_orphan() -> ext4_truncate() -> ext4_mark_inode_dirty()

    -> ext4_try_to_expand_extra_isize()

or:

ext4_evict_inode() -> ext4_mark_inode_dirty()

    -> ext4_try_to_expand_extra_isize()

Skip expansion when !SB_ACTIVE. This is a minor loss of functionality (extra isize won't grow for these inodes during mount), which e2fsck can resolve later if needed.

Reported-by [email protected]
Fixes c8585c6fcaf2 ("ext4: fix races between changing inode journal mode and ext4_writepages")
---
 fs/ext4/inode.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
fs/ext4/inode.c +10 -0
index ce99807c5f5b..a5409324d965 100644
@@ -6510,6 +6510,16 @@ static int ext4_try_to_expand_extra_isize(struct inode *inode,
 	if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND))
 		return -EOVERFLOW;
 
+	/*
+	 * Skip expansion during mount (!SB_ACTIVE).  Expanding extra isize
+	 * may move xattrs to external blocks and release ea_inodes via iput.
+	 * When !SB_ACTIVE, iput triggers write_inode_now() which acquires
+	 * s_writepages_rwsem, causing a deadlock with the caller's active
+	 * jbd2 handle (lock order: s_writepages_rwsem -> jbd2_handle).
+	 */
+	if (unlikely(!(inode->i_sb->s_flags & SB_ACTIVE)))
+		return -EBUSY;
+
 	/*
 	 * In nojournal mode, we can immediately attempt to expand
 	 * the inode.  When journaled, we first need to obtain extra
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 2/2 ext4: set EXT4_STATE_NO_EXPAND in ext4_evict_inode

An inode being evicted will never need its extra isize expanded. Set EXT4_STATE_NO_EXPAND before ext4_mark_inode_dirty() in ext4_evict_inode() to make this explicit and prevent any unnecessary work in ext4_try_to_expand_extra_isize().

This also provides defense-in-depth for the s_writepages_rwsem deadlock during mount-time orphan cleanup, ensuring the expand path is blocked for inodes under eviction regardless of how they are reached.

---
 fs/ext4/inode.c | 1 +
 1 file changed, 1 insertion(+)
fs/ext4/inode.c +1 -0
index a5409324d965..0d131371ad3d 100644
@@ -264,6 +264,7 @@ void ext4_evict_inode(struct inode *inode)
 	if (ext4_inode_is_fast_symlink(inode))
 		memset(EXT4_I(inode)->i_data, 0, sizeof(EXT4_I(inode)->i_data));
 	inode->i_size = 0;
+	ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
 	err = ext4_mark_inode_dirty(handle, inode);
 	if (err) {
 		ext4_warning(inode->i_sb,
Signature · 1 line
2.43.0
author
#2 lore raw

Re: [PATCH 2/2] ext4: set EXT4_STATE_NO_EXPAND in ext4_evict_inode

Friendly ping

Quoted 27 lines · Yun Zhou

An inode being evicted will never need its extra isize expanded. Set EXT4_STATE_NO_EXPAND before ext4_mark_inode_dirty() in ext4_evict_inode() to make this explicit and prevent any unnecessary work in ext4_try_to_expand_extra_isize().

This also provides defense-in-depth for the s_writepages_rwsem deadlock during mount-time orphan cleanup, ensuring the expand path is blocked for inodes under eviction regardless of how they are reached.

---
  fs/ext4/inode.c | 1 +
  1 file changed, 1 insertion(+)
fs/ext4/inode.c +1 -0
index a5409324d965..0d131371ad3d 100644
@@ -264,6 +264,7 @@ void ext4_evict_inode(struct inode *inode)
  	if (ext4_inode_is_fast_symlink(inode))
  		memset(EXT4_I(inode)->i_data, 0, sizeof(EXT4_I(inode)->i_data));
  	inode->i_size = 0;
+	ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
  	err = ext4_mark_inode_dirty(handle, inode);
  	if (err) {
  		ext4_warning(inode->i_sb,
maintainer Applied
#3 lore raw
In reply to Yun Zhou
Quoted 9 lines · Yun Zhou

ext4_try_to_expand_extra_isize() is called from __ext4_mark_inode_dirty() while holding an active jbd2 handle. During mount (!SB_ACTIVE), the expand path may move xattrs to external blocks and release ea_inodes via iput(). When !SB_ACTIVE, iput() calls write_inode_now() which acquires s_writepages_rwsem, creating a circular lock dependency:

s_writepages_rwsem --> jbd2_handle --> xattr_sem --> s_writepages_rwsem

[...]

Applied, thanks!

[1/2] ext4: skip extra isize expansion during mount to prevent deadlock

      commit: 7461c60b9c6a839b13ad4c3490681a0cf5aa0637
[2/2] ext4: set EXT4_STATE_NO_EXPAND in ext4_evict_inode
      commit: 2c0e4fb511f97467dafc91b6afb3414100289081

Best regards,

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