]> git.hungrycats.org Git - linux/commitdiff
bcache: fix race of writeback thread starting before complete initialization
authorEric Wheeler <git@linux.ewheeler.net>
Fri, 26 Feb 2016 22:39:06 +0000 (14:39 -0800)
committerSasha Levin <sasha.levin@oracle.com>
Mon, 18 Apr 2016 12:49:25 +0000 (08:49 -0400)
[ Upstream commit 07cc6ef8edc47f8b4fc1e276d31127a0a5863d4d ]

The bch_writeback_thread might BUG_ON in read_dirty() if
dc->sb==BDEV_STATE_DIRTY and bch_sectors_dirty_init has not yet completed
its related initialization.  This patch downs the dc->writeback_lock until
after initialization is complete, thus preventing bch_writeback_thread
from proceeding prematurely.

See this thread:
  http://thread.gmane.org/gmane.linux.kernel.bcache.devel/3453

Signed-off-by: Eric Wheeler <bcache@linux.ewheeler.net>
Tested-by: Marc MERLIN <marc@merlins.org>
Cc: <stable@vger.kernel.org>
Signed-off-by: Jens Axboe <axboe@fb.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
drivers/md/bcache/super.c

index 42522c8f13c6fd1d715b2eb99f491be9fe3a11b2..e83629a25af4ab69e31534e0d1a2b815ca193f81 100644 (file)
@@ -1046,8 +1046,12 @@ int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c)
         */
        atomic_set(&dc->count, 1);
 
-       if (bch_cached_dev_writeback_start(dc))
+       /* Block writeback thread, but spawn it */
+       down_write(&dc->writeback_lock);
+       if (bch_cached_dev_writeback_start(dc)) {
+               up_write(&dc->writeback_lock);
                return -ENOMEM;
+       }
 
        if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
                bch_sectors_dirty_init(dc);
@@ -1059,6 +1063,9 @@ int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c)
        bch_cached_dev_run(dc);
        bcache_device_link(&dc->disk, c, "bdev");
 
+       /* Allow the writeback thread to proceed */
+       up_write(&dc->writeback_lock);
+
        pr_info("Caching %s as %s on set %pU",
                bdevname(dc->bdev, buf), dc->disk.disk->disk_name,
                dc->disk.c->sb.set_uuid);