Skip to content

Commit 9b29972

Browse files
Eric Wheeleraxboe
authored andcommitted
bcache: cleaned up error handling around register_cache()
Fix null pointer dereference by changing register_cache() to return an int instead of being void. This allows it to return -ENOMEM or -ENODEV and enables upper layers to handle the OOM case without NULL pointer issues. See this thread: http://thread.gmane.org/gmane.linux.kernel.bcache.devel/3521 Fixes this error: gargamel:/sys/block/md5/bcache# echo /dev/sdh2 > /sys/fs/bcache/register bcache: register_cache() error opening sdh2: cannot allocate memory BUG: unable to handle kernel NULL pointer dereference at 00000000000009b8 IP: [<ffffffffc05a7e8d>] cache_set_flush+0x102/0x15c [bcache] PGD 120dff067 PUD 1119a3067 PMD 0 Oops: 0000 [#1] SMP Modules linked in: veth ip6table_filter ip6_tables (...) CPU: 4 PID: 3371 Comm: kworker/4:3 Not tainted 4.4.2-amd64-i915-volpreempt-20160213bc1 #3 Hardware name: System manufacturer System Product Name/P8H67-M PRO, BIOS 3904 04/27/2013 Workqueue: events cache_set_flush [bcache] task: ffff88020d5dc280 ti: ffff88020b6f8000 task.ti: ffff88020b6f8000 RIP: 0010:[<ffffffffc05a7e8d>] [<ffffffffc05a7e8d>] cache_set_flush+0x102/0x15c [bcache] Signed-off-by: Eric Wheeler <bcache@linux.ewheeler.net> Tested-by: Marc MERLIN <marc@merlins.org> Cc: <stable@vger.kernel.org>
1 parent 07cc6ef commit 9b29972

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

drivers/md/bcache/super.c

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,11 +1835,12 @@ static int cache_alloc(struct cache_sb *sb, struct cache *ca)
18351835
return 0;
18361836
}
18371837

1838-
static void register_cache(struct cache_sb *sb, struct page *sb_page,
1838+
static int register_cache(struct cache_sb *sb, struct page *sb_page,
18391839
struct block_device *bdev, struct cache *ca)
18401840
{
18411841
char name[BDEVNAME_SIZE];
1842-
const char *err = "cannot allocate memory";
1842+
const char *err = NULL;
1843+
int ret = 0;
18431844

18441845
memcpy(&ca->sb, sb, sizeof(struct cache_sb));
18451846
ca->bdev = bdev;
@@ -1854,27 +1855,35 @@ static void register_cache(struct cache_sb *sb, struct page *sb_page,
18541855
if (blk_queue_discard(bdev_get_queue(ca->bdev)))
18551856
ca->discard = CACHE_DISCARD(&ca->sb);
18561857

1857-
if (cache_alloc(sb, ca) != 0)
1858+
ret = cache_alloc(sb, ca);
1859+
if (ret != 0)
18581860
goto err;
18591861

1860-
err = "error creating kobject";
1861-
if (kobject_add(&ca->kobj, &part_to_dev(bdev->bd_part)->kobj, "bcache"))
1862-
goto err;
1862+
if (kobject_add(&ca->kobj, &part_to_dev(bdev->bd_part)->kobj, "bcache")) {
1863+
err = "error calling kobject_add";
1864+
ret = -ENOMEM;
1865+
goto out;
1866+
}
18631867

18641868
mutex_lock(&bch_register_lock);
18651869
err = register_cache_set(ca);
18661870
mutex_unlock(&bch_register_lock);
18671871

1868-
if (err)
1869-
goto err;
1872+
if (err) {
1873+
ret = -ENODEV;
1874+
goto out;
1875+
}
18701876

18711877
pr_info("registered cache device %s", bdevname(bdev, name));
1878+
18721879
out:
18731880
kobject_put(&ca->kobj);
1874-
return;
1881+
18751882
err:
1876-
pr_notice("error opening %s: %s", bdevname(bdev, name), err);
1877-
goto out;
1883+
if (err)
1884+
pr_notice("error opening %s: %s", bdevname(bdev, name), err);
1885+
1886+
return ret;
18781887
}
18791888

18801889
/* Global interfaces/init */
@@ -1972,7 +1981,8 @@ static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
19721981
if (!ca)
19731982
goto err_close;
19741983

1975-
register_cache(sb, sb_page, bdev, ca);
1984+
if (register_cache(sb, sb_page, bdev, ca) != 0)
1985+
goto err_close;
19761986
}
19771987
out:
19781988
if (sb_page)

0 commit comments

Comments
 (0)