Re: [PATCH v2 1/7] dmaengine: idxd: fix memory leak in error handling path of idxd_setup_wqs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





在 2025/2/19 00:32, Fenghua Yu 写道:
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@xxxxxxxxxxxxxxxxx>
Reviewed-by: Dave Jiang <dave.jiang@xxxxxxxxx>
---
  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?

Yes.

And if i>1, mem areas for wqs[0] won't be freed and will leak, right?

No, the two ways of writing are equivalent.

#include <stdio.h>

int main()
{
    int i = 1;
    while (i-- > 0)
        printf("freeing i %d\n", i);

    return 0;
}

// console output
// freeing i 0

I will drop this line to avoid confusion.

Thanks.
Shuai





[Index of Archives]     [Linux Kernel]     [Linux ARM (vger)]     [Linux ARM MSM]     [Linux Omap]     [Linux Arm]     [Linux Tegra]     [Fedora ARM]     [Linux for Samsung SOC]     [eCos]     [Linux PCI]     [Linux Fastboot]     [Gcc Help]     [Git]     [DCCP]     [IETF Announce]     [Security]     [Linux MIPS]     [Yosemite Campsites]

  Powered by Linux