]> git.hungrycats.org Git - linux/commitdiff
bcache: fix for gc and write-back race
authorTang Junhui <tang.junhui@zte.com.cn>
Wed, 6 Sep 2017 06:25:59 +0000 (14:25 +0800)
committerSasha Levin <alexander.levin@verizon.com>
Wed, 4 Oct 2017 11:43:04 +0000 (07:43 -0400)
[ Upstream commit 9baf30972b5568d8b5bc8b3c46a6ec5b58100463 ]

gc and write-back get raced (see the email "bcache get stucked" I sended
before):
gc thread                               write-back thread
|                                       |bch_writeback_thread()
|bch_gc_thread()                        |
|                                       |==>read_dirty()
|==>bch_btree_gc()                      |
|==>btree_root() //get btree root       |
|                //node write locker    |
|==>bch_btree_gc_root()                 |
|                                       |==>read_dirty_submit()
|                                       |==>write_dirty()
|                                       |==>continue_at(cl,
|                                       |               write_dirty_finish,
|                                       |               system_wq);
|                                       |==>write_dirty_finish()//excute
|                                       |               //in system_wq
|                                       |==>bch_btree_insert()
|                                       |==>bch_btree_map_leaf_nodes()
|                                       |==>__bch_btree_map_nodes()
|                                       |==>btree_root //try to get btree
|                                       |              //root node read
|                                       |              //lock
|                                       |-----stuck here
|==>bch_btree_set_root()
|==>bch_journal_meta()
|==>bch_journal()
|==>journal_try_write()
|==>journal_write_unlocked() //journal_full(&c->journal)
|                            //condition satisfied
|==>continue_at(cl, journal_write, system_wq); //try to excute
|                               //journal_write in system_wq
|                               //but work queue is excuting
|                               //write_dirty_finish()
|==>closure_sync(); //wait journal_write execute
|                   //over and wake up gc,
|-------------stuck here
|==>release root node write locker

This patch alloc a separate work-queue for write-back thread to avoid such
race.

(Commit log re-organized by Coly Li to pass checkpatch.pl checking)

Signed-off-by: Tang Junhui <tang.junhui@zte.com.cn>
Acked-by: Coly Li <colyli@suse.de>
Cc: stable@vger.kernel.org
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Sasha Levin <alexander.levin@verizon.com>
drivers/md/bcache/bcache.h
drivers/md/bcache/super.c
drivers/md/bcache/writeback.c

index 04f7bc28ef832b6dded6d10e810ddbfbfada4fca..dfdd1908641c1d04cfce63b4da6ca5b0baf130f6 100644 (file)
@@ -348,6 +348,7 @@ struct cached_dev {
        /* Limit number of writeback bios in flight */
        struct semaphore        in_flight;
        struct task_struct      *writeback_thread;
+       struct workqueue_struct *writeback_write_wq;
 
        struct keybuf           writeback_keys;
 
index 38522cc6b26d133735471274c56df3c3ac08f75d..8e5666ac8a6afca82dacc0a065cbff4b9e3623f4 100644 (file)
@@ -1087,6 +1087,8 @@ static void cached_dev_free(struct closure *cl)
        cancel_delayed_work_sync(&dc->writeback_rate_update);
        if (!IS_ERR_OR_NULL(dc->writeback_thread))
                kthread_stop(dc->writeback_thread);
+       if (dc->writeback_write_wq)
+               destroy_workqueue(dc->writeback_write_wq);
 
        mutex_lock(&bch_register_lock);
 
index 43dc9cb14d284551e8e6d7ba1394b6de2f71d262..b0667b321a3f380a3d7e54bded5f901345c02754 100644 (file)
@@ -191,7 +191,7 @@ static void write_dirty(struct closure *cl)
 
        closure_bio_submit(&io->bio, cl, &io->dc->disk);
 
-       continue_at(cl, write_dirty_finish, system_wq);
+       continue_at(cl, write_dirty_finish, io->dc->writeback_write_wq);
 }
 
 static void read_dirty_endio(struct bio *bio, int error)
@@ -211,7 +211,7 @@ static void read_dirty_submit(struct closure *cl)
 
        closure_bio_submit(&io->bio, cl, &io->dc->disk);
 
-       continue_at(cl, write_dirty, system_wq);
+       continue_at(cl, write_dirty, io->dc->writeback_write_wq);
 }
 
 static void read_dirty(struct cached_dev *dc)
@@ -523,6 +523,11 @@ void bch_cached_dev_writeback_init(struct cached_dev *dc)
 
 int bch_cached_dev_writeback_start(struct cached_dev *dc)
 {
+       dc->writeback_write_wq = alloc_workqueue("bcache_writeback_wq",
+                                               WQ_MEM_RECLAIM, 0);
+       if (!dc->writeback_write_wq)
+               return -ENOMEM;
+
        dc->writeback_thread = kthread_create(bch_writeback_thread, dc,
                                              "bcache_writeback");
        if (IS_ERR(dc->writeback_thread))