Skip to contentSkip to conversation

md: bcache: check the return value of kzalloc() in detached_dev_do_request()

PATCH In review Jia-Ju Bai 2 participants 3 messages Feb 25, 2022 – Mar 3, 2022 bcache (block layer cache)

lore
Share & tools
lore

Jia-Ju Bai

author
#0 lore raw

PATCH 1/1 md: bcache: check the return value of kzalloc() in detached_dev_do_request()

The function kzalloc() in detached_dev_do_request() can fail, so its return value should be checked.

Fixes bc082a55d25c (bcache: fix inaccurate io state for detached bcache devices)
---
 drivers/md/bcache/request.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/md/bcache/request.c +2 -0
index d15aae6c51c1..1b5ccfa93b8c 100644
@@ -1107,6 +1107,8 @@ static void detached_dev_do_request(struct bcache_device *d, struct bio *bio,
 	 * which would call closure_get(&dc->disk.cl)
 	 */
 	ddip = kzalloc(sizeof(struct detached_dev_io_private), GFP_NOIO);
+	if (!ddip)
+		return;
 	ddip->d = d;
 	/* Count on the bcache device */
 	ddip->orig_bdev = orig_bdev;
Signature · 1 line
2.17.1
#1 lore raw
Quoted 15 lines · Jia-Ju Bai

The function kzalloc() in detached_dev_do_request() can fail, so its return value should be checked.

Fixes bc082a55d25c (bcache: fix inaccurate io state for detached bcache devices)
---
  drivers/md/bcache/request.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/md/bcache/request.c +0 -0
index d15aae6c51c1..1b5ccfa93b8c 100644

drivers/md/bcache/request.c static void detached_dev_do_request(struct bcache_device *d, struct bio *bio, · line 1109

Quoted 3 lines
drivers/md/bcache/request.c +0 -0
@@ -1107,6 +1107,8 @@ static void detached_dev_do_request(struct bcache_device *d, struct bio *bio,
  	 * which would call closure_get(&dc->disk.cl)
  	 */
  	ddip = kzalloc(sizeof(struct detached_dev_io_private), GFP_NOIO);

Yes this is a potential problem, may result NULL pointer deference.

drivers/md/bcache/request.c static void detached_dev_do_request(struct bcache_device *d, struct bio *bio, · line 1111

Quoted 2 lines
drivers/md/bcache/request.c +2 -0
+	if (!ddip)
+		return;

The fix here is incorrect. A proper way to fix might be something like this,

    if (!ddip) {

        bio->bi_status = BLK_STS_RESOURCE;

        bio->bi_end_io(bio);

        return;

    }

The orignal patch will make I/O task be in sleep forever.

Quoted 3 lines
drivers/md/bcache/request.c +0 -0
  	ddip->d = d;
  	/* Count on the bcache device */
  	ddip->orig_bdev = orig_bdev;
author
#2 lore raw
Quoted 33 lines · Coly Li
Quoted 15 lines · Jia-Ju Bai

The function kzalloc() in detached_dev_do_request() can fail, so its return value should be checked.

Fixes bc082a55d25c (bcache: fix inaccurate io state for detached bcache devices)

---
  drivers/md/bcache/request.c | 2 ++

  1 file changed, 2 insertions(+)
drivers/md/bcache/request.c +0 -0
index d15aae6c51c1..1b5ccfa93b8c 100644

drivers/md/bcache/request.c static void detached_dev_do_request(struct · line 1110

Quoted 4 lines
drivers/md/bcache/request.c +0 -0
@@ -1107,6 +1107,8 @@ static void detached_dev_do_request(struct 
 bcache_device *d, struct bio *bio,
        * which would call closure_get(&dc->disk.cl)
        */
       ddip = kzalloc(sizeof(struct detached_dev_io_private), GFP_NOIO);

Yes this is a potential problem, may result NULL pointer deference.

drivers/md/bcache/request.c static void detached_dev_do_request(struct bcache_device *d, struct bio *bio, · line 1111

Quoted 2 lines
drivers/md/bcache/request.c +2 -0
+    if (!ddip)
+        return;

The fix here is incorrect. A proper way to fix might be something like this,

    if (!ddip) {

        bio->bi_status = BLK_STS_RESOURCE;

Quoted 13 lines

        bio->bi_end_io(bio);

        return;

    }

The orignal patch will make I/O task be in sleep forever.

Quoted 3 lines
drivers/md/bcache/request.c +0 -0
       ddip->d = d;
       /* Count on the bcache device */

      ddip->orig_bdev = orig_bdev;

Hi Coly,

Thanks for the advice!
I will send a V2 patch.

Best wishes,
Jia-Ju Bai