bcache: fix a lost wake-up problem caused by mca_cannibalize_lock
authorGuoju Fang <fangguoju@gmail.com>
Wed, 13 Nov 2019 08:03:16 +0000 (16:03 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 1 Oct 2020 18:40:03 +0000 (20:40 +0200)
[ Upstream commit 34cf78bf34d48dddddfeeadb44f9841d7864997a ]

This patch fix a lost wake-up problem caused by the race between
mca_cannibalize_lock and bch_cannibalize_unlock.

Consider two processes, A and B. Process A is executing
mca_cannibalize_lock, while process B takes c->btree_cache_alloc_lock
and is executing bch_cannibalize_unlock. The problem happens that after
process A executes cmpxchg and will execute prepare_to_wait. In this
timeslice process B executes wake_up, but after that process A executes
prepare_to_wait and set the state to TASK_INTERRUPTIBLE. Then process A
goes to sleep but no one will wake up it. This problem may cause bcache
device to dead.

Signed-off-by: Guoju Fang <fangguoju@gmail.com>
Signed-off-by: Coly Li <colyli@suse.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/md/bcache/bcache.h
drivers/md/bcache/btree.c
drivers/md/bcache/super.c

index 7fe7df56fa334b91b18abf817c1a91d690e75a1d..f0939fc1cfe55a7036d56f343a41252d17f9acdf 100644 (file)
@@ -547,6 +547,7 @@ struct cache_set {
         */
        wait_queue_head_t       btree_cache_wait;
        struct task_struct      *btree_cache_alloc_lock;
+       spinlock_t              btree_cannibalize_lock;
 
        /*
         * When we free a btree node, we increment the gen of the bucket the
index 764d519a7f1c6d3fa5b7e7e491567a330e0b465c..26e56a9952d09ccdc39b44e59c074ee76c1beb75 100644 (file)
@@ -836,15 +836,17 @@ out:
 
 static int mca_cannibalize_lock(struct cache_set *c, struct btree_op *op)
 {
-       struct task_struct *old;
-
-       old = cmpxchg(&c->btree_cache_alloc_lock, NULL, current);
-       if (old && old != current) {
+       spin_lock(&c->btree_cannibalize_lock);
+       if (likely(c->btree_cache_alloc_lock == NULL)) {
+               c->btree_cache_alloc_lock = current;
+       } else if (c->btree_cache_alloc_lock != current) {
                if (op)
                        prepare_to_wait(&c->btree_cache_wait, &op->wait,
                                        TASK_UNINTERRUPTIBLE);
+               spin_unlock(&c->btree_cannibalize_lock);
                return -EINTR;
        }
+       spin_unlock(&c->btree_cannibalize_lock);
 
        return 0;
 }
@@ -879,10 +881,12 @@ static struct btree *mca_cannibalize(struct cache_set *c, struct btree_op *op,
  */
 static void bch_cannibalize_unlock(struct cache_set *c)
 {
+       spin_lock(&c->btree_cannibalize_lock);
        if (c->btree_cache_alloc_lock == current) {
                c->btree_cache_alloc_lock = NULL;
                wake_up(&c->btree_cache_wait);
        }
+       spin_unlock(&c->btree_cannibalize_lock);
 }
 
 static struct btree *mca_alloc(struct cache_set *c, struct btree_op *op,
index 95e9a33de06a266cf63facdf45d1cdfbce9784a3..263c0d987929e8c98429f9ec9146e1d5c60c81c3 100644 (file)
@@ -1510,6 +1510,7 @@ struct cache_set *bch_cache_set_alloc(struct cache_sb *sb)
        sema_init(&c->sb_write_mutex, 1);
        mutex_init(&c->bucket_lock);
        init_waitqueue_head(&c->btree_cache_wait);
+       spin_lock_init(&c->btree_cannibalize_lock);
        init_waitqueue_head(&c->bucket_wait);
        init_waitqueue_head(&c->gc_wait);
        sema_init(&c->uuid_write_mutex, 1);