Skip to content

Commit f522051

Browse files
Tang Junhuigregkh
authored andcommitted
bcache: fix for gc and write-back race
commit 9baf30972b5568d8b5bc8b3c46a6ec5b58100463 upstream. 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> Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent a6c5e7a commit f522051

3 files changed

Lines changed: 10 additions & 2 deletions

File tree

drivers/md/bcache/bcache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ struct cached_dev {
333333
/* Limit number of writeback bios in flight */
334334
struct semaphore in_flight;
335335
struct task_struct *writeback_thread;
336+
struct workqueue_struct *writeback_write_wq;
336337

337338
struct keybuf writeback_keys;
338339

drivers/md/bcache/super.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,8 @@ static void cached_dev_free(struct closure *cl)
10561056
cancel_delayed_work_sync(&dc->writeback_rate_update);
10571057
if (!IS_ERR_OR_NULL(dc->writeback_thread))
10581058
kthread_stop(dc->writeback_thread);
1059+
if (dc->writeback_write_wq)
1060+
destroy_workqueue(dc->writeback_write_wq);
10591061

10601062
mutex_lock(&bch_register_lock);
10611063

drivers/md/bcache/writeback.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ static void write_dirty(struct closure *cl)
191191

192192
closure_bio_submit(&io->bio, cl);
193193

194-
continue_at(cl, write_dirty_finish, system_wq);
194+
continue_at(cl, write_dirty_finish, io->dc->writeback_write_wq);
195195
}
196196

197197
static void read_dirty_endio(struct bio *bio)
@@ -211,7 +211,7 @@ static void read_dirty_submit(struct closure *cl)
211211

212212
closure_bio_submit(&io->bio, cl);
213213

214-
continue_at(cl, write_dirty, system_wq);
214+
continue_at(cl, write_dirty, io->dc->writeback_write_wq);
215215
}
216216

217217
static void read_dirty(struct cached_dev *dc)
@@ -523,6 +523,11 @@ void bch_cached_dev_writeback_init(struct cached_dev *dc)
523523

524524
int bch_cached_dev_writeback_start(struct cached_dev *dc)
525525
{
526+
dc->writeback_write_wq = alloc_workqueue("bcache_writeback_wq",
527+
WQ_MEM_RECLAIM, 0);
528+
if (!dc->writeback_write_wq)
529+
return -ENOMEM;
530+
526531
dc->writeback_thread = kthread_create(bch_writeback_thread, dc,
527532
"bcache_writeback");
528533
if (IS_ERR(dc->writeback_thread))

0 commit comments

Comments
 (0)