bcache: check ca->alloc_thread initialized before wake up it
authorColy Li <[email protected]>
Fri, 13 Oct 2017 23:35:29 +0000 (16:35 -0700)
committerJens Axboe <[email protected]>
Mon, 16 Oct 2017 15:07:26 +0000 (09:07 -0600)
In bcache code, sysfs entries are created before all resources get
allocated, e.g. allocation thread of a cache set.

There is posibility for NULL pointer deference if a resource is accessed
but which is not initialized yet. Indeed Jorg Bornschein catches one on
cache set allocation thread and gets a kernel oops.

The reason for this bug is, when bch_bucket_alloc() is called during
cache set registration and attaching, ca->alloc_thread is not properly
allocated and initialized yet, call wake_up_process() on ca->alloc_thread
triggers NULL pointer deference failure. A simple and fast fix is, before
waking up ca->alloc_thread, checking whether it is allocated, and only
wake up ca->alloc_thread when it is not NULL.

Signed-off-by: Coly Li <[email protected]>
Reported-by: Jorg Bornschein <[email protected]>
Cc: Kent Overstreet <[email protected]>
Cc: [email protected]
Reviewed-by: Michael Lyle <[email protected]>
Signed-off-by: Jens Axboe <[email protected]>
drivers/md/bcache/alloc.c

index cacbe2dbd5c31757229e8e27e77845e59777d6ea..e1a4205caa2e191976aa2de0c41d346215f2d06a 100644 (file)
@@ -406,7 +406,8 @@ long bch_bucket_alloc(struct cache *ca, unsigned reserve, bool wait)
 
        finish_wait(&ca->set->bucket_wait, &w);
 out:
-       wake_up_process(ca->alloc_thread);
+       if (ca->alloc_thread)
+               wake_up_process(ca->alloc_thread);
 
        trace_bcache_alloc(ca, reserve);