Skip to content

Commit f7a62c3

Browse files
jankarasmb49
authored andcommitted
ext4: fix deadlock due to mbcache entry corruption
BugLink: https://bugs.launchpad.net/bugs/2003914 [ Upstream commit a44e84a ] 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]> Signed-off-by: Sasha Levin <[email protected]> Signed-off-by: Kamal Mostafa <[email protected]> Signed-off-by: Stefan Bader <[email protected]>
1 parent ea0ecba commit f7a62c3

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
@@ -1293,7 +1293,7 @@ ext4_xattr_release_block(handle_t *handle, struct inode *inode,
12931293
ce = mb_cache_entry_get(ea_block_cache, hash,
12941294
bh->b_blocknr);
12951295
if (ce) {
1296-
ce->e_reusable = 1;
1296+
set_bit(MBE_REUSABLE_B, &ce->e_flags);
12971297
mb_cache_entry_put(ea_block_cache, ce);
12981298
}
12991299
}
@@ -2054,7 +2054,7 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
20542054
}
20552055
BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
20562056
if (ref == EXT4_XATTR_REFCOUNT_MAX)
2057-
ce->e_reusable = 0;
2057+
clear_bit(MBE_REUSABLE_B, &ce->e_flags);
20582058
ea_bdebug(new_bh, "reusing; refcount now=%d",
20592059
ref);
20602060
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
@@ -94,8 +94,9 @@ int mb_cache_entry_create(struct mb_cache *cache, gfp_t mask, u32 key,
9494
atomic_set(&entry->e_refcnt, 1);
9595
entry->e_key = key;
9696
entry->e_value = value;
97-
entry->e_reusable = reusable;
98-
entry->e_referenced = 0;
97+
entry->e_flags = 0;
98+
if (reusable)
99+
set_bit(MBE_REUSABLE_B, &entry->e_flags);
99100
head = mb_cache_entry_head(cache, key);
100101
hlist_bl_lock(head);
101102
hlist_bl_for_each_entry(dup, dup_node, head, e_hash_list) {
@@ -162,7 +163,8 @@ static struct mb_cache_entry *__entry_find(struct mb_cache *cache,
162163
while (node) {
163164
entry = hlist_bl_entry(node, struct mb_cache_entry,
164165
e_hash_list);
165-
if (entry->e_key == key && entry->e_reusable &&
166+
if (entry->e_key == key &&
167+
test_bit(MBE_REUSABLE_B, &entry->e_flags) &&
166168
atomic_inc_not_zero(&entry->e_refcnt))
167169
goto out;
168170
node = node->next;
@@ -318,7 +320,7 @@ EXPORT_SYMBOL(mb_cache_entry_delete_or_get);
318320
void mb_cache_entry_touch(struct mb_cache *cache,
319321
struct mb_cache_entry *entry)
320322
{
321-
entry->e_referenced = 1;
323+
set_bit(MBE_REFERENCED_B, &entry->e_flags);
322324
}
323325
EXPORT_SYMBOL(mb_cache_entry_touch);
324326

@@ -343,9 +345,9 @@ static unsigned long mb_cache_shrink(struct mb_cache *cache,
343345
entry = list_first_entry(&cache->c_list,
344346
struct mb_cache_entry, e_list);
345347
/* Drop initial hash reference if there is no user */
346-
if (entry->e_referenced ||
348+
if (test_bit(MBE_REFERENCED_B, &entry->e_flags) ||
347349
atomic_cmpxchg(&entry->e_refcnt, 1, 0) != 1) {
348-
entry->e_referenced = 0;
350+
clear_bit(MBE_REFERENCED_B, &entry->e_flags);
349351
list_move_tail(&entry->e_list, &cache->c_list);
350352
continue;
351353
}

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)