linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] bcache: add a check for the return of kzalloc()
@ 2022-03-25  3:27 xkernel.wang
  2022-04-07 16:58 ` Coly Li
  0 siblings, 1 reply; 2+ messages in thread
From: xkernel.wang @ 2022-03-25  3:27 UTC (permalink / raw)
  To: colyli, kent.overstreet; +Cc: linux-bcache, linux-kernel, Xiaoke Wang

From: Xiaoke Wang <xkernel.wang@foxmail.com>

kzalloc() is a memory allocation function which can return NULL when
some internal memory errors happen. So it is better to check it to
prevent potential wrong memory access.

Signed-off-by: Xiaoke Wang <xkernel.wang@foxmail.com>
---
Note: I just roughly handled the error and this seems to affect the
original functionality. If this cannot fail, maybe the tag
`__GFP_NOFAIL` should be considered.
 drivers/md/bcache/request.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/drivers/md/bcache/request.c b/drivers/md/bcache/request.c
index 6d1de88..1dc9821 100644
--- a/drivers/md/bcache/request.c
+++ b/drivers/md/bcache/request.c
@@ -1107,14 +1107,16 @@ 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);
-	ddip->d = d;
-	/* Count on the bcache device */
-	ddip->orig_bdev = orig_bdev;
-	ddip->start_time = start_time;
-	ddip->bi_end_io = bio->bi_end_io;
-	ddip->bi_private = bio->bi_private;
-	bio->bi_end_io = detached_dev_end_io;
-	bio->bi_private = ddip;
+	if (ddip) {
+		ddip->d = d;
+		/* Count on the bcache device */
+		ddip->orig_bdev = orig_bdev;
+		ddip->start_time = start_time;
+		ddip->bi_end_io = bio->bi_end_io;
+		ddip->bi_private = bio->bi_private;
+		bio->bi_end_io = detached_dev_end_io;
+		bio->bi_private = ddip;
+	}
 
 	if ((bio_op(bio) == REQ_OP_DISCARD) &&
 	    !blk_queue_discard(bdev_get_queue(dc->bdev)))
-- 

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] bcache: add a check for the return of kzalloc()
  2022-03-25  3:27 [PATCH] bcache: add a check for the return of kzalloc() xkernel.wang
@ 2022-04-07 16:58 ` Coly Li
  0 siblings, 0 replies; 2+ messages in thread
From: Coly Li @ 2022-04-07 16:58 UTC (permalink / raw)
  To: xkernel.wang; +Cc: linux-bcache, kent.overstreet, linux-kernel

On 3/25/22 11:27 AM, xkernel.wang@foxmail.com wrote:
> From: Xiaoke Wang <xkernel.wang@foxmail.com>
>
> kzalloc() is a memory allocation function which can return NULL when
> some internal memory errors happen. So it is better to check it to
> prevent potential wrong memory access.
>
> Signed-off-by: Xiaoke Wang <xkernel.wang@foxmail.com>
> ---
> Note: I just roughly handled the error and this seems to affect the
> original functionality. If this cannot fail, maybe the tag
> `__GFP_NOFAIL` should be considered.

The error handling is incorrect indeed. And IIRC there is someone else 
already fixes it properly and the patch is not upstream yet.

Thanks.


Coly Li


>   drivers/md/bcache/request.c | 18 ++++++++++--------
>   1 file changed, 10 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/md/bcache/request.c b/drivers/md/bcache/request.c
> index 6d1de88..1dc9821 100644
> --- a/drivers/md/bcache/request.c
> +++ b/drivers/md/bcache/request.c
> @@ -1107,14 +1107,16 @@ 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);
> -	ddip->d = d;
> -	/* Count on the bcache device */
> -	ddip->orig_bdev = orig_bdev;
> -	ddip->start_time = start_time;
> -	ddip->bi_end_io = bio->bi_end_io;
> -	ddip->bi_private = bio->bi_private;
> -	bio->bi_end_io = detached_dev_end_io;
> -	bio->bi_private = ddip;
> +	if (ddip) {
> +		ddip->d = d;
> +		/* Count on the bcache device */
> +		ddip->orig_bdev = orig_bdev;
> +		ddip->start_time = start_time;
> +		ddip->bi_end_io = bio->bi_end_io;
> +		ddip->bi_private = bio->bi_private;
> +		bio->bi_end_io = detached_dev_end_io;
> +		bio->bi_private = ddip;
> +	}
>   
>   	if ((bio_op(bio) == REQ_OP_DISCARD) &&
>   	    !blk_queue_discard(bdev_get_queue(dc->bdev)))



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-04-07 16:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-25  3:27 [PATCH] bcache: add a check for the return of kzalloc() xkernel.wang
2022-04-07 16:58 ` Coly Li

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).