Skip to content

Commit

Permalink
bcache: fixup multiple threads crash
Browse files Browse the repository at this point in the history
commit 887554a upstream.

When multiple threads to check btree nodes in parallel, the main
thread wait for all threads to stop or CACHE_SET_IO_DISABLE flag:

wait_event_interruptible(check_state->wait,
                         atomic_read(&check_state->started) == 0 ||
                         test_bit(CACHE_SET_IO_DISABLE, &c->flags));

However, the bch_btree_node_read and bch_btree_node_read_done
maybe call bch_cache_set_error, then the CACHE_SET_IO_DISABLE
will be set. If the flag already set, the main thread return
error. At the same time, maybe some threads still running and
read NULL pointer, the kernel will crash.

This patch change the event wait condition, the main thread must
wait for all threads to stop.

Fixes: 8e71022 ("bcache: make bch_btree_check() to be multithreaded")
Signed-off-by: Mingzhe Zou <[email protected]>
Cc: [email protected] # v5.7+
Signed-off-by: Coly Li <[email protected]>
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
zoumingzhe authored and Sasha Levin committed Apr 7, 2022
1 parent e997e9e commit a0f9d28
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions drivers/md/bcache/btree.c
Original file line number Diff line number Diff line change
Expand Up @@ -2060,9 +2060,11 @@ int bch_btree_check(struct cache_set *c)
}
}

/*
* Must wait for all threads to stop.
*/
wait_event_interruptible(check_state->wait,
atomic_read(&check_state->started) == 0 ||
test_bit(CACHE_SET_IO_DISABLE, &c->flags));
atomic_read(&check_state->started) == 0);

for (i = 0; i < check_state->total_threads; i++) {
if (check_state->infos[i].result) {
Expand Down
6 changes: 4 additions & 2 deletions drivers/md/bcache/writeback.c
Original file line number Diff line number Diff line change
Expand Up @@ -998,9 +998,11 @@ void bch_sectors_dirty_init(struct bcache_device *d)
}
}

/*
* Must wait for all threads to stop.
*/
wait_event_interruptible(state->wait,
atomic_read(&state->started) == 0 ||
test_bit(CACHE_SET_IO_DISABLE, &c->flags));
atomic_read(&state->started) == 0);

out:
kfree(state);
Expand Down

0 comments on commit a0f9d28

Please sign in to comment.