[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <f44a4303-d106-408c-ba59-911fe7b9a290@nvidia.com>
Date: Tue, 18 Feb 2025 08:32:14 -0800
From: Fenghua Yu <fenghuay@...dia.com>
To: Shuai Xue <xueshuai@...ux.alibaba.com>, vinicius.gomes@...el.com,
dave.jiang@...el.com, vkoul@...nel.org
Cc: dmaengine@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH v2 1/7] dmaengine: idxd: fix memory leak in error handling
path of idxd_setup_wqs
Hi, Shuai,
On 2/14/25 21:44, Shuai Xue wrote:
> Memory allocated for wqs is not freed if an error occurs during
> idxd_setup_wqs(). To fix it, free the allocated memory in the reverse
> order of allocation before exiting the function in case of an error.
>
> Fixes: a8563a33a5e2 ("dmanegine: idxd: reformat opcap output to match bitmap_parse() input")
> Signed-off-by: Shuai Xue <xueshuai@...ux.alibaba.com>
> Reviewed-by: Dave Jiang <dave.jiang@...el.com>
> ---
> drivers/dma/idxd/init.c | 20 +++++++++++++++++---
> 1 file changed, 17 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/dma/idxd/init.c b/drivers/dma/idxd/init.c
> index b946f78f85e1..b85736fd25bd 100644
> --- a/drivers/dma/idxd/init.c
> +++ b/drivers/dma/idxd/init.c
> @@ -169,8 +169,8 @@ static int idxd_setup_wqs(struct idxd_device *idxd)
>
> idxd->wq_enable_map = bitmap_zalloc_node(idxd->max_wqs, GFP_KERNEL, dev_to_node(dev));
> if (!idxd->wq_enable_map) {
> - kfree(idxd->wqs);
> - return -ENOMEM;
> + rc = -ENOMEM;
> + goto err_bitmap;
> }
>
> for (i = 0; i < idxd->max_wqs; i++) {
> @@ -191,6 +191,7 @@ static int idxd_setup_wqs(struct idxd_device *idxd)
> rc = dev_set_name(conf_dev, "wq%d.%d", idxd->id, wq->id);
> if (rc < 0) {
> put_device(conf_dev);
> + kfree(wq);
> goto err;
> }
>
> @@ -204,6 +205,7 @@ static int idxd_setup_wqs(struct idxd_device *idxd)
> wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev));
> if (!wq->wqcfg) {
> put_device(conf_dev);
> + kfree(wq);
> rc = -ENOMEM;
> goto err;
> }
> @@ -211,7 +213,9 @@ static int idxd_setup_wqs(struct idxd_device *idxd)
> if (idxd->hw.wq_cap.op_config) {
> wq->opcap_bmap = bitmap_zalloc(IDXD_MAX_OPCAP_BITS, GFP_KERNEL);
> if (!wq->opcap_bmap) {
> + kfree(wq->wqcfg);
> put_device(conf_dev);
> + kfree(wq);
> rc = -ENOMEM;
> goto err;
> }
> @@ -225,11 +229,21 @@ static int idxd_setup_wqs(struct idxd_device *idxd)
> return 0;
>
> err:
> - while (--i >= 0) {
> + while (i-- > 0) {
Why changed to "i-- > 0" here? Before coming to here, the mem areas
allocated for wqs[i] are freed already and there is not need to free
them again here, right? And if i>1, mem areas for wqs[0] won't be freed
and will leak, right?
> wq = idxd->wqs[i];
> + if (idxd->hw.wq_cap.op_config)
> + bitmap_free(wq->opcap_bmap);
> + kfree(wq->wqcfg);
> conf_dev = wq_confdev(wq);
> put_device(conf_dev);
> + kfree(wq);
> +
> }
> + bitmap_free(idxd->wq_enable_map);
> +
> +err_bitmap:
> + kfree(idxd->wqs);
> +
> return rc;
> }
>
Thanks.
-Fenghua
Powered by blists - more mailing lists