ext4: defer iput() in ext4_xattr_block_set() to avoid deadlock with writepages
PATCH v4 1/1 ext4: defer iput() in ext4_xattr_block_set() to avoid deadlock with writepages
ext4_xattr_block_set() calls iput() on ea_inode while its callers hold xattr_sem. If this iput() drops the last reference, it can trigger write_inode_now() -> ext4_writepages() -> s_writepages_rwsem, which violates the lock ordering since ext4_writepages() already establishes s_writepages_rwsem -> jbd2_handle ordering:
CPU0 (writeback worker) CPU1 (file create)
---- ----
ext4_writepages()
s_writepages_rwsem (read) ext4_create()
ext4_do_writepages() __ext4_new_inode()
ext4_journal_start() [holds jbd2 handle]
wait_transaction_locked() ext4_xattr_set_handle()
[WAIT for jbd2_handle] xattr_sem (write)
CPU2 (xattr set or isize expand)
----
ext4_xattr_set_handle() or ext4_try_to_expand_extra_isize()
xattr_sem (write)
ext4_xattr_block_set()
iput(ea_inode)
write_inode_now()
ext4_writepages()
s_writepages_rwsem (read) [DEADLOCK]
This forms a circular dependency on lock classes:
s_writepages_rwsem --> jbd2_handle --> xattr_sem --> s_writepages_rwsem
Fix by deferring all ea_inode iput() calls until after locks are released:
1. Convert all iput(ea_inode) calls within ext4_xattr_set_entry(), ext4_xattr_ibody_set(), and ext4_xattr_block_set() to deferred iput via ext4_expand_inode_array(). Thread the ea_inode_array through ext4_xattr_set_handle(), ext4_xattr_move_to_block(), and ext4_expand_extra_isize_ea() as an output parameter.
2. Callers that own the journal handle (ext4_xattr_set, ext4_expand_extra_isize) free the array AFTER ext4_journal_stop(). For ext4_try_to_expand_extra_isize (called from __ext4_mark_inode_dirty with the caller's handle), free after xattr_sem release -- safe because EXT4_STATE_NO_EXPAND blocks the !SB_ACTIVE path.
3. Callers that cannot control the handle lifetime (ext4_initxattrs, __ext4_set_acl, ext4_set_context, inline data ops) pass NULL. ext4_expand_inode_array() falls back to synchronous iput() when ea_inode_array is NULL -- safe because these callers only run with SB_ACTIVE where iput cannot trigger write_inode_now().
4. Set EXT4_STATE_NO_EXPAND in ext4_evict_inode() before ext4_mark_inode_dirty() to block the only code path where !SB_ACTIVE (mount-time orphan cleanup) could reach ext4_try_to_expand_extra_isize.
5. Use __GFP_NOFAIL in ext4_expand_inode_array() to guarantee the deferred-iput array never fails to grow, eliminating any fallback to synchronous iput() under locks.
6. Pass ea_inode_array directly to ext4_xattr_release_block() in ext4_xattr_block_set() instead of using a local array freed synchronously under xattr_sem.
--- fs/ext4/acl.c | 2 +- fs/ext4/crypto.c | 4 +- fs/ext4/inline.c | 8 ++-- fs/ext4/inode.c | 16 +++++-- fs/ext4/xattr.c | 93 ++++++++++++++++++++++++---------------- fs/ext4/xattr.h | 10 +++-- fs/ext4/xattr_security.c | 3 +- 7 files changed, 85 insertions(+), 51 deletions(-)
fs/ext4/acl.c +1 -1
@@ -215,7 +215,7 @@ __ext4_set_acl(handle_t *handle, struct inode *inode, int type, } error =ext4_xattr_set_handle (handle, inode, name_index,"" , - value, size, xattr_flags); + value, size, xattr_flags,NULL );kfree (value);if (!error)
fs/ext4/crypto.c +2 -2
@@ -173,7 +173,7 @@ static int ext4_set_context(struct inode *inode, const void *ctx, size_t len, res =ext4_xattr_set_handle (handle, inode,EXT4_XATTR_INDEX_ENCRYPTION ,EXT4_XATTR_NAME_ENCRYPTION_CONTEXT , - ctx, len,XATTR_CREATE ); + ctx, len,XATTR_CREATE ,NULL );if (!res) {ext4_set_inode_flag (inode,EXT4_INODE_ENCRYPT ); ext4_clear_inode_state(inode,
@@ -202,7 +202,7 @@ static int ext4_set_context(struct inode *inode, const void *ctx, size_t len, res =ext4_xattr_set_handle (handle, inode,EXT4_XATTR_INDEX_ENCRYPTION ,EXT4_XATTR_NAME_ENCRYPTION_CONTEXT , - ctx, len,0 ); + ctx, len,0 ,NULL );if (!res) {ext4_set_inode_flag (inode,EXT4_INODE_ENCRYPT );/*
fs/ext4/inline.c +4 -4
@@ -309,7 +309,7 @@ static int ext4_create_inline_data(handle_t *handle, goto out; } - error =ext4_xattr_ibody_set (handle, inode, &i, &is); + error =ext4_xattr_ibody_set (handle, inode, &i, &is,NULL );if (error) {if (error == -ENOSPC ) ext4_clear_inode_state(inode,
@@ -386,7 +386,7 @@ static int ext4_update_inline_data(handle_t *handle, struct inode *inode, i.value = value; i.value_len = len; - error =ext4_xattr_ibody_set (handle, inode, &i, &is); + error =ext4_xattr_ibody_set (handle, inode, &i, &is,NULL );if (error) goto out;
@@ -469,7 +469,7 @@ static int ext4_destroy_inline_data_nolock(handle_t *handle,if (error) goto out; - error =ext4_xattr_ibody_set (handle, inode, &i, &is); + error =ext4_xattr_ibody_set (handle, inode, &i, &is,NULL );if (error) goto out;
@@ -1917,7 +1917,7 @@ int ext4_inline_data_truncate(struct inode *inode, int *has_inline) i.value = value; i.value_len = i_size >EXT4_MIN_INLINE_DATA_SIZE ? i_size -EXT4_MIN_INLINE_DATA_SIZE :0 ; - err =ext4_xattr_ibody_set (handle, inode, &i, &is); + err =ext4_xattr_ibody_set (handle, inode, &i, &is,NULL );if (err) goto out_error; }
fs/ext4/inode.c +12 -4
@@ -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,
@@ -6408,7 +6409,8 @@ ext4_reserve_inode_write(handle_t *handle, struct inode *inode,static int __ext4_expand_extra_isize (struct inode *inode,unsigned int new_extra_isize,struct ext4_iloc *iloc, -handle_t *handle,int *no_expand) +handle_t *handle,int *no_expand, +struct ext4_xattr_inode_array **ea_inode_array) {struct ext4_inode *raw_inode;struct ext4_xattr_ibody_header *header;
@@ -6453,7 +6455,7 @@ static int __ext4_expand_extra_isize(struct inode *inode,/* try to expand with EAs present */ error =ext4_expand_extra_isize_ea (inode, new_extra_isize, - raw_inode, handle); + raw_inode, handle, ea_inode_array);if (error) {/* * Inode size expansion failed; don't try again
@@ -6475,6 +6477,7 @@ static int ext4_try_to_expand_extra_isize(struct inode *inode, {int no_expand;int error; +struct ext4_xattr_inode_array *ea_inode_array =NULL ;if (ext4_test_inode_state (inode,EXT4_STATE_NO_EXPAND ))return -EOVERFLOW ;
@@ -6496,8 +6499,10 @@ static int ext4_try_to_expand_extra_isize(struct inode *inode,return -EBUSY ; error =__ext4_expand_extra_isize (inode, new_extra_isize, &iloc, - handle, &no_expand); + handle, &no_expand, + &ea_inode_array);ext4_write_unlock_xattr (inode, &no_expand); +ext4_xattr_inode_array_free (ea_inode_array);return error; }
@@ -6509,6 +6514,7 @@ int ext4_expand_extra_isize(struct inode *inode,handle_t *handle;int no_expand;int error, rc; +struct ext4_xattr_inode_array *ea_inode_array =NULL ;if (ext4_test_inode_state (inode,EXT4_STATE_NO_EXPAND )) {brelse (iloc->bh);
@@ -6534,7 +6540,8 @@ int ext4_expand_extra_isize(struct inode *inode, } error =__ext4_expand_extra_isize (inode, new_extra_isize, iloc, - handle, &no_expand); + handle, &no_expand, + &ea_inode_array); rc =ext4_mark_iloc_dirty (handle, inode, iloc);if (!error)
@@ -6543,6 +6550,7 @@ int ext4_expand_extra_isize(struct inode *inode, out_unlock:ext4_write_unlock_xattr (inode, &no_expand);ext4_journal_stop (handle); +ext4_xattr_inode_array_free (ea_inode_array);return error; }
fs/ext4/xattr.c +57 -36
276 lines not on this page · raw message
fs/ext4/xattr.h +7 -3
@@ -179,7 +179,9 @@ extern ssize_t ext4_listxattr(struct dentry *, char *, size_t);extern int ext4_xattr_get (struct inode *,int ,const char *,void *,size_t );extern int ext4_xattr_set (struct inode *,int ,const char *,const void *,size_t ,int ); -extern int ext4_xattr_set_handle (handle_t *,struct inode *,int ,const char *,const void *,size_t ,int ); +extern int ext4_xattr_set_handle (handle_t *,struct inode *,int ,const char *, +const void *,size_t ,int , +struct ext4_xattr_inode_array **ea_inode_array);extern int ext4_xattr_set_credits (struct inode *inode,size_t value_len,bool is_create,int *credits); extern int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
@@ -192,7 +194,8 @@ extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,extern void ext4_xattr_inode_array_free (struct ext4_xattr_inode_array *array);extern int ext4_expand_extra_isize_ea (struct inode *inode,int new_extra_isize, -struct ext4_inode *raw_inode,handle_t *handle); +struct ext4_inode *raw_inode,handle_t *handle, +struct ext4_xattr_inode_array **ea_inode_array);extern void ext4_evict_ea_inode (struct inode *inode);extern const struct xattr_handler *const ext4_xattr_handlers[];
@@ -204,7 +207,8 @@ extern int ext4_xattr_ibody_get(struct inode *inode, int name_index, void *buffer, size_t buffer_size);extern int ext4_xattr_ibody_set (handle_t *handle,struct inode *inode,struct ext4_xattr_info *i, -struct ext4_xattr_ibody_find *is); +struct ext4_xattr_ibody_find *is, +struct ext4_xattr_inode_array **ea_inode_array);extern struct mb_cache *ext4_xattr_create_cache (void );extern void ext4_xattr_destroy_cache (struct mb_cache *);
fs/ext4/xattr_security.c +2 -1
@@ -44,7 +44,8 @@ ext4_initxattrs(struct inode *inode, const struct xattr *xattr_array, err =ext4_xattr_set_handle (handle, inode,EXT4_XATTR_INDEX_SECURITY , xattr->name, xattr->value, - xattr->value_len,XATTR_CREATE ); + xattr->value_len,XATTR_CREATE , +NULL );if (err <0 )break ; }
Signature · 1 line
2.43.0