]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
bcache: fixup multiple threads crash
authorMingzhe Zou <mingzhe.zou@easystack.cn>
Fri, 11 Feb 2022 06:39:15 +0000 (14:39 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 8 Apr 2022 12:39:57 +0000 (14:39 +0200)
commit 887554ab96588de2917b6c8c73e552da082e5368 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: 8e7102273f597 ("bcache: make bch_btree_check() to be multithreaded")
Signed-off-by: Mingzhe Zou <mingzhe.zou@easystack.cn>
Cc: stable@vger.kernel.org # v5.7+
Signed-off-by: Coly Li <colyli@suse.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/md/bcache/btree.c
drivers/md/bcache/writeback.c

index fe6dce125aba226e5f7c05cad39c78b38829389d..418914373a51319364ffd26a988b3f3d492ab9d1 100644 (file)
@@ -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) {
index 3c74996978dadef2642c3ac716abb4c46abda3c9..952253f24175a80cdcfc83e68f883474504a2660 100644 (file)
@@ -952,9 +952,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);