Skip to content

Commit a44e84a

Browse files
jankaratytso
authored andcommitted
ext4: fix deadlock due to mbcache entry corruption
When manipulating xattr blocks, we can deadlock infinitely looping inside ext4_xattr_block_set() where we constantly keep finding xattr block for reuse in mbcache but we are unable to reuse it because its reference count is too big. This happens because cache entry for the xattr block is marked as reusable (e_reusable set) although its reference count is too big. When this inconsistency happens, this inconsistent state is kept indefinitely and so ext4_xattr_block_set() keeps retrying indefinitely. The inconsistent state is caused by non-atomic update of e_reusable bit. e_reusable is part of a bitfield and e_reusable update can race with update of e_referenced bit in the same bitfield resulting in loss of one of the updates. Fix the problem by using atomic bitops instead. This bug has been around for many years, but it became *much* easier to hit after commit 65f8b80 ("ext4: fix race when reusing xattr blocks"). Cc: [email protected] Fixes: 6048c64 ("mbcache: add reusable flag to cache entries") Fixes: 65f8b80 ("ext4: fix race when reusing xattr blocks") Reported-and-tested-by: Jeremi Piotrowski <[email protected]> Reported-by: Thilo Fromm <[email protected]> Link: https://lore.kernel.org/r/[email protected]/ Signed-off-by: Jan Kara <[email protected]> Reviewed-by: Andreas Dilger <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Theodore Ts'o <[email protected]>
1 parent b40ebaf commit a44e84a

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

fs/ext4/xattr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,7 @@ ext4_xattr_release_block(handle_t *handle, struct inode *inode,
12811281
ce = mb_cache_entry_get(ea_block_cache, hash,
12821282
bh->b_blocknr);
12831283
if (ce) {
1284-
ce->e_reusable = 1;
1284+
set_bit(MBE_REUSABLE_B, &ce->e_flags);
12851285
mb_cache_entry_put(ea_block_cache, ce);
12861286
}
12871287
}
@@ -2043,7 +2043,7 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
20432043
}
20442044
BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
20452045
if (ref == EXT4_XATTR_REFCOUNT_MAX)
2046-
ce->e_reusable = 0;
2046+
clear_bit(MBE_REUSABLE_B, &ce->e_flags);
20472047
ea_bdebug(new_bh, "reusing; refcount now=%d",
20482048
ref);
20492049
ext4_xattr_block_csum_set(inode, new_bh);

fs/mbcache.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ int mb_cache_entry_create(struct mb_cache *cache, gfp_t mask, u32 key,
100100
atomic_set(&entry->e_refcnt, 2);
101101
entry->e_key = key;
102102
entry->e_value = value;
103-
entry->e_reusable = reusable;
104-
entry->e_referenced = 0;
103+
entry->e_flags = 0;
104+
if (reusable)
105+
set_bit(MBE_REUSABLE_B, &entry->e_flags);
105106
head = mb_cache_entry_head(cache, key);
106107
hlist_bl_lock(head);
107108
hlist_bl_for_each_entry(dup, dup_node, head, e_hash_list) {
@@ -165,7 +166,8 @@ static struct mb_cache_entry *__entry_find(struct mb_cache *cache,
165166
while (node) {
166167
entry = hlist_bl_entry(node, struct mb_cache_entry,
167168
e_hash_list);
168-
if (entry->e_key == key && entry->e_reusable &&
169+
if (entry->e_key == key &&
170+
test_bit(MBE_REUSABLE_B, &entry->e_flags) &&
169171
atomic_inc_not_zero(&entry->e_refcnt))
170172
goto out;
171173
node = node->next;
@@ -284,7 +286,7 @@ EXPORT_SYMBOL(mb_cache_entry_delete_or_get);
284286
void mb_cache_entry_touch(struct mb_cache *cache,
285287
struct mb_cache_entry *entry)
286288
{
287-
entry->e_referenced = 1;
289+
set_bit(MBE_REFERENCED_B, &entry->e_flags);
288290
}
289291
EXPORT_SYMBOL(mb_cache_entry_touch);
290292

@@ -309,9 +311,9 @@ static unsigned long mb_cache_shrink(struct mb_cache *cache,
309311
entry = list_first_entry(&cache->c_list,
310312
struct mb_cache_entry, e_list);
311313
/* Drop initial hash reference if there is no user */
312-
if (entry->e_referenced ||
314+
if (test_bit(MBE_REFERENCED_B, &entry->e_flags) ||
313315
atomic_cmpxchg(&entry->e_refcnt, 1, 0) != 1) {
314-
entry->e_referenced = 0;
316+
clear_bit(MBE_REFERENCED_B, &entry->e_flags);
315317
list_move_tail(&entry->e_list, &cache->c_list);
316318
continue;
317319
}

include/linux/mbcache.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010

1111
struct mb_cache;
1212

13+
/* Cache entry flags */
14+
enum {
15+
MBE_REFERENCED_B = 0,
16+
MBE_REUSABLE_B
17+
};
18+
1319
struct mb_cache_entry {
1420
/* List of entries in cache - protected by cache->c_list_lock */
1521
struct list_head e_list;
@@ -26,8 +32,7 @@ struct mb_cache_entry {
2632
atomic_t e_refcnt;
2733
/* Key in hash - stable during lifetime of the entry */
2834
u32 e_key;
29-
u32 e_referenced:1;
30-
u32 e_reusable:1;
35+
unsigned long e_flags;
3136
/* User provided value - stable during lifetime of the entry */
3237
u64 e_value;
3338
};

0 commit comments

Comments
 (0)