Skip to content

Commit be23e83

Browse files
ZhiqiangLiu26axboe
authored andcommitted
bcache: fix potential deadlock problem in btree_gc_coalesce
coccicheck reports: drivers/md//bcache/btree.c:1538:1-7: preceding lock on line 1417 In btree_gc_coalesce func, if the coalescing process fails, we will goto to out_nocoalesce tag directly without releasing new_nodes[i]->write_lock. Then, it will cause a deadlock when trying to acquire new_nodes[i]-> write_lock for freeing new_nodes[i] before return. btree_gc_coalesce func details as follows: if alloc new_nodes[i] fails: goto out_nocoalesce; // obtain new_nodes[i]->write_lock mutex_lock(&new_nodes[i]->write_lock) // main coalescing process for (i = nodes - 1; i > 0; --i) [snipped] if coalescing process fails: // Here, directly goto out_nocoalesce // tag will cause a deadlock goto out_nocoalesce; [snipped] // release new_nodes[i]->write_lock mutex_unlock(&new_nodes[i]->write_lock) // coalesing succ, return return; out_nocoalesce: btree_node_free(new_nodes[i]) // free new_nodes[i] // obtain new_nodes[i]->write_lock mutex_lock(&new_nodes[i]->write_lock); // set flag for reuse clear_bit(BTREE_NODE_dirty, &ew_nodes[i]->flags); // release new_nodes[i]->write_lock mutex_unlock(&new_nodes[i]->write_lock); To fix the problem, we add a new tag 'out_unlock_nocoalesce' for releasing new_nodes[i]->write_lock before out_nocoalesce tag. If coalescing process fails, we will go to out_unlock_nocoalesce tag for releasing new_nodes[i]->write_lock before free new_nodes[i] in out_nocoalesce tag. (Coly Li helps to clean up commit log format.) Fixes: 2a28568 ("bcache: btree locking rework") Signed-off-by: Zhiqiang Liu <[email protected]> Signed-off-by: Coly Li <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent b3a9e3b commit be23e83

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

drivers/md/bcache/btree.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,7 +1389,7 @@ static int btree_gc_coalesce(struct btree *b, struct btree_op *op,
13891389
if (__set_blocks(n1, n1->keys + n2->keys,
13901390
block_bytes(b->c)) >
13911391
btree_blocks(new_nodes[i]))
1392-
goto out_nocoalesce;
1392+
goto out_unlock_nocoalesce;
13931393

13941394
keys = n2->keys;
13951395
/* Take the key of the node we're getting rid of */
@@ -1418,7 +1418,7 @@ static int btree_gc_coalesce(struct btree *b, struct btree_op *op,
14181418

14191419
if (__bch_keylist_realloc(&keylist,
14201420
bkey_u64s(&new_nodes[i]->key)))
1421-
goto out_nocoalesce;
1421+
goto out_unlock_nocoalesce;
14221422

14231423
bch_btree_node_write(new_nodes[i], &cl);
14241424
bch_keylist_add(&keylist, &new_nodes[i]->key);
@@ -1464,6 +1464,10 @@ static int btree_gc_coalesce(struct btree *b, struct btree_op *op,
14641464
/* Invalidated our iterator */
14651465
return -EINTR;
14661466

1467+
out_unlock_nocoalesce:
1468+
for (i = 0; i < nodes; i++)
1469+
mutex_unlock(&new_nodes[i]->write_lock);
1470+
14671471
out_nocoalesce:
14681472
closure_sync(&cl);
14691473

0 commit comments

Comments
 (0)