Skip to contentSkip to conversation

dmaengine: idxd: fix memory leak in error handling path

PATCH Superseded Shuai Xue 3 participants 25 messages Feb 15, 2025 – Mar 3, 2025

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Shuai Xue

author
#0 lore raw

v2 cover dmaengine: idxd: fix memory leak in error handling path

changes since v1:
- add Reviewed-by tag for patch 1-5 from Dave Jiang
- add fixes tag
- add patch 6 and 7 to fix memory leak in remove call per Vinicius

Shuai Xue (7):
dmaengine: idxd: fix memory leak in error handling path of
idxd_setup_wqs
dmaengine: idxd: fix memory leak in error handling path of
idxd_setup_engines
dmaengine: idxd: fix memory leak in error handling path of
idxd_setup_groups
dmaengine: idxd: fix memory leak in error handling path of idxd_alloc
dmaengine: idxd: fix memory leak in error handling path of
idxd_pci_probe
dmaengine: idxd: Add missing idxd cleanup to fix memory leak in remove
call
dmaengine: idxd: Refactor remove call with idxd_cleanup() helper

 drivers/dma/idxd/init.c | 75 ++++++++++++++++++++++++++++-------------
 1 file changed, 51 insertions(+), 24 deletions(-)
Signature · 1 line
2.39.3
author
#1 lore raw

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

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")
---
 drivers/dma/idxd/init.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
drivers/dma/idxd/init.c +17 -3
index b946f78f85e1..b85736fd25bd 100644
@@ -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) {
 		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;
 }
 
Signature · 1 line
2.39.3
#2 lore raw

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

Quoted 5 lines

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")

…

Will a “stable tag” become relevant also for this patch series?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/stable-kernel-rules.rst?h=v6.14-rc2#n3

Quoted 2 lines

+++ b/drivers/dma/idxd/init.c

(quoted diff) +0 -0
@@ -169,8 +169,8 @@ static int idxd_setup_wqs(struct idxd_device *idxd)

…

drivers/dma/idxd/init.c static int idxd_setup_wqs(struct idxd_device *idxd) · line 211

Quoted 8 lines
drivers/dma/idxd/init.c +1 -0
@@ -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;
 		}

…

I got the impression that more common exception handling code could be moved to additional jump targets at the end of such function implementations. Will further adjustment opportunities be taken into account for the affected resource management?

Regards,
Markus

author
#3 lore raw

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

Quoted 10 lines · Markus Elfring
Quoted 5 lines

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")

…

Will a “stable tag” become relevant also for this patch series?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/stable-kernel-rules.rst?h=v6.14-rc2#n3

I don't know if this is a real serious issue for stable kernel. But I would like to add stable tag if the mantainer @Vinicius and @Dave ask.

Quoted 14 lines
Quoted 3 lines

+++ b/drivers/dma/idxd/init.c

(quoted diff) +0 -0
@@ -169,8 +169,8 @@ static int idxd_setup_wqs(struct idxd_device *idxd)

…

Quoted 8 lines
drivers/dma/idxd/init.c +1 -0
@@ -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;
  		}

…

Quoted 4 lines

I got the impression that more common exception handling code could be moved to additional jump targets at the end of such function implementations. Will further adjustment opportunities be taken into account for the affected resource management?

Sure, I will move them to the jump targets.

Quoted 3 lines

Regards,
Markus

Thanks for valuable comments.
Shuai

#4 lore raw

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

Quoted 4 lines
Quoted 2 lines

Will a “stable tag” become relevant also for this patch series?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/stable-kernel-rules.rst?h=v6.14-rc2#n3

I don't know if this is a real serious issue for stable kernel.

How many resource leaks would you like to avoid with your contributions?

Regards,
Markus

author
#5 lore raw

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

Quoted 5 lines · Markus Elfring
Quoted 4 lines
Quoted 2 lines

Will a “stable tag” become relevant also for this patch series?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/stable-kernel-rules.rst?h=v6.14-rc2#n3

I don't know if this is a real serious issue for stable kernel.

How many resource leaks would you like to avoid with your contributions?

For this patch:

- wqs = 8 * 8 bytes = 64 bytes
- wq = 8 * 2400 bytes = 19200 bytes
- opcap_bmap = 256 bit / 8 = 32 bytes

Around 18 KB.

Quoted 3 lines

Regards,
Markus

Thanks.
Shuai

#6 lore raw

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

In reply to Shuai Xue

…

Quoted 1 line
Fixes a8563a33a5e2 ("dmanegine: idxd: reformat opcap output to match bitmap_parse() input")

…

Would other commits be more appropriate for the desired reference (according to the affected function implementation)?

Examples:
2022-09-29: de5819b994893197c71c86d21af10f85f50d6499 ("dmaengine: idxd: track enabled workqueues in bitmap") 2021-04-21: 700af3a0a26cbac87e4a0ae1dfa79597d0056d5f ("dmaengine: idxd: add 'struct idxd_dev' as wrapper for conf_dev") 2021-04-20: 7c5dd23e57c14cf7177b8a5e0fd08916e0c60005 ("dmaengine: idxd: fix wq conf_dev 'struct device' lifetime")

Regards,
Markus

author
#7 lore raw

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

Quoted 4 lines · Markus Elfring

…

Quoted 1 line
Fixes a8563a33a5e2 ("dmanegine: idxd: reformat opcap output to match bitmap_parse() input")

…

Quoted 8 lines

Would other commits be more appropriate for the desired reference (according to the affected function implementation)?

Examples:
2022-09-29: de5819b994893197c71c86d21af10f85f50d6499 ("dmaengine: idxd: track enabled workqueues in bitmap") 2021-04-21: 700af3a0a26cbac87e4a0ae1dfa79597d0056d5f ("dmaengine: idxd: add 'struct idxd_dev' as wrapper for conf_dev") 2021-04-20: 7c5dd23e57c14cf7177b8a5e0fd08916e0c60005 ("dmaengine: idxd: fix wq conf_dev 'struct device' lifetime")

Ok, I will add the fixes.

Quoted 3 lines

Regards,
Markus

Thanks.

#8 lore raw

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

In reply to Shuai Xue

Hi, Shuai,

Quoted 53 lines · Shuai Xue

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")
---
  drivers/dma/idxd/init.c | 20 +++++++++++++++++---
  1 file changed, 17 insertions(+), 3 deletions(-)
drivers/dma/idxd/init.c +6 -2
index b946f78f85e1..b85736fd25bd 100644
@@ -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;
  			}

drivers/dma/idxd/init.c static int idxd_setup_wqs(struct idxd_device *idxd) · line 232

Quoted 5 lines
drivers/dma/idxd/init.c +1 -1
@@ -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?

Quoted 7 lines
drivers/dma/idxd/init.c +4 -0
  		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);

drivers/dma/idxd/init.c static int idxd_setup_wqs(struct idxd_device *idxd) · line 249

Quoted 10 lines
drivers/dma/idxd/init.c +6 -0
+
  	}
+	bitmap_free(idxd->wq_enable_map);
+
+err_bitmap:
+	kfree(idxd->wqs);
+
  	return rc;
  }
 

Thanks.

-Fenghua

author
#9 lore raw

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

Quoted 51 lines · Fenghua Yu

Hi, Shuai,

Quoted 48 lines · Shuai Xue

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")

---
  drivers/dma/idxd/init.c | 20 +++++++++++++++++---

  1 file changed, 17 insertions(+), 3 deletions(-)
drivers/dma/idxd/init.c +6 -2
index b946f78f85e1..b85736fd25bd 100644
@@ -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;
Quoted 8 lines
Quoted 6 lines
drivers/dma/idxd/init.c +0 -0
               }
@@ -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.

Quoted 1 line

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

author
#10 lore raw

PATCH v2 2/7 dmaengine: idxd: fix memory leak in error handling path of idxd_setup_engines

In reply to Shuai Xue

Memory allocated for engines is not freed if an error occurs during idxd_setup_engines(). To fix it, free the allocated memory in the reverse order of allocation before exiting the function in case of an error.

Fixes 75b911309060 ("dmaengine: idxd: fix engine conf_dev lifetime")
---
 drivers/dma/idxd/init.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/dma/idxd/init.c +4 -0
index b85736fd25bd..4e47075c5bef 100644
@@ -277,6 +277,7 @@ static int idxd_setup_engines(struct idxd_device *idxd)
 		rc = dev_set_name(conf_dev, "engine%d.%d", idxd->id, engine->id);
 		if (rc < 0) {
 			put_device(conf_dev);
+			kfree(engine);
 			goto err;
 		}
 
@@ -290,7 +291,10 @@ static int idxd_setup_engines(struct idxd_device *idxd)
 		engine = idxd->engines[i];
 		conf_dev = engine_confdev(engine);
 		put_device(conf_dev);
+		kfree(engine);
 	}
+	kfree(idxd->engines);
+
 	return rc;
 }
 
Signature · 1 line
2.39.3
Reviewed-by on v2 2/7
#11 lore raw

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

Quoted 8 lines · Shuai Xue

Memory allocated for engines is not freed if an error occurs during idxd_setup_engines(). To fix it, free the allocated memory in the reverse order of allocation before exiting the function in case of an error.

Fixes 75b911309060 ("dmaengine: idxd: fix engine conf_dev lifetime")
Quoted 17 lines
---
  drivers/dma/idxd/init.c | 4 ++++
  1 file changed, 4 insertions(+)
drivers/dma/idxd/init.c +1 -0
index b85736fd25bd..4e47075c5bef 100644
@@ -277,6 +277,7 @@ static int idxd_setup_engines(struct idxd_device *idxd)
  		rc = dev_set_name(conf_dev, "engine%d.%d", idxd->id, engine->id);
  		if (rc < 0) {
  			put_device(conf_dev);
+			kfree(engine);
  			goto err;
  		}
 

drivers/dma/idxd/init.c static int idxd_setup_engines(struct idxd_device *idxd) · line 300

Quoted 10 lines
drivers/dma/idxd/init.c +3 -0
@@ -290,7 +291,10 @@ static int idxd_setup_engines(struct idxd_device *idxd)
  		engine = idxd->engines[i];
  		conf_dev = engine_confdev(engine);
  		put_device(conf_dev);
+		kfree(engine);
  	}
+	kfree(idxd->engines);
+
  	return rc;
  }
 

Thanks.

-Fenghua

author
#12 lore raw

PATCH v2 3/7 dmaengine: idxd: fix memory leak in error handling path of idxd_setup_groups

In reply to Shuai Xue

Memory allocated for groups is not freed if an error occurs during idxd_setup_groups(). To fix it, free the allocated memory in the reverse order of allocation before exiting the function in case of an error.

Fixes defe49f96012 ("dmaengine: idxd: fix group conf_dev lifetime")
---
 drivers/dma/idxd/init.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/dma/idxd/init.c +4 -0
index 4e47075c5bef..a2da68e6144d 100644
@@ -328,6 +328,7 @@ static int idxd_setup_groups(struct idxd_device *idxd)
 		rc = dev_set_name(conf_dev, "group%d.%d", idxd->id, group->id);
 		if (rc < 0) {
 			put_device(conf_dev);
+			kfree(group);
 			goto err;
 		}
 
@@ -352,7 +353,10 @@ static int idxd_setup_groups(struct idxd_device *idxd)
 	while (--i >= 0) {
 		group = idxd->groups[i];
 		put_device(group_confdev(group));
+		kfree(group);
 	}
+	kfree(idxd->groups);
+
 	return rc;
 }
 
Signature · 1 line
2.39.3
#13 lore raw

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

Hi, Shuai,

Quoted 24 lines · Shuai Xue

Memory allocated for groups is not freed if an error occurs during idxd_setup_groups(). To fix it, free the allocated memory in the reverse order of allocation before exiting the function in case of an error.

Fixes defe49f96012 ("dmaengine: idxd: fix group conf_dev lifetime")
---
  drivers/dma/idxd/init.c | 4 ++++
  1 file changed, 4 insertions(+)
drivers/dma/idxd/init.c +1 -0
index 4e47075c5bef..a2da68e6144d 100644
@@ -328,6 +328,7 @@ static int idxd_setup_groups(struct idxd_device *idxd)
  		rc = dev_set_name(conf_dev, "group%d.%d", idxd->id, group->id);
  		if (rc < 0) {
  			put_device(conf_dev);
+			kfree(group);
  			goto err;
  		}
 

drivers/dma/idxd/init.c static int idxd_setup_groups(struct idxd_device *idxd) · line 359

Quoted 7 lines
drivers/dma/idxd/init.c +3 -0
@@ -352,7 +353,10 @@ static int idxd_setup_groups(struct idxd_device *idxd)
  	while (--i >= 0) {
  		group = idxd->groups[i];
  		put_device(group_confdev(group));
+		kfree(group);
  	}
+	kfree(idxd->groups);
+

What happens to the memory areas previously allocated for wqs and engines after idxd_setup_groups() fails? They need to be freed as well, but currently they are not.

Maybe a separate patch cleans up the previously allocated mem areas for wqs/engines/groups if there is any failure after the allocations?

drivers/dma/idxd/init.c static int idxd_setup_groups(struct idxd_device *idxd) · line 362

Quoted 3 lines
drivers/dma/idxd/init.c +0 -0
  	return rc;
  }
 

Thanks.

-Fenghua

author
#14 lore raw

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

Quoted 36 lines · Fenghua Yu

Hi, Shuai,

Quoted 23 lines · Shuai Xue

Memory allocated for groups is not freed if an error occurs during idxd_setup_groups(). To fix it, free the allocated memory in the reverse order of allocation before exiting the function in case of an error.

Fixes defe49f96012 ("dmaengine: idxd: fix group conf_dev lifetime")

---
  drivers/dma/idxd/init.c | 4 ++++

  1 file changed, 4 insertions(+)
drivers/dma/idxd/init.c +1 -0
index 4e47075c5bef..a2da68e6144d 100644
@@ -328,6 +328,7 @@ static int idxd_setup_groups(struct idxd_device *idxd)
           rc = dev_set_name(conf_dev, "group%d.%d", idxd->id, group->id);
           if (rc < 0) {
               put_device(conf_dev);
+            kfree(group);
               goto err;
           }

drivers/dma/idxd/init.c static int idxd_setup_groups(struct idxd_device *idxd) · line 359

Quoted 7 lines
drivers/dma/idxd/init.c +3 -0
@@ -352,7 +353,10 @@ static int idxd_setup_groups(struct idxd_device *idxd)
       while (--i >= 0) {
           group = idxd->groups[i];
           put_device(group_confdev(group));
+        kfree(group);
       }
+    kfree(idxd->groups);
+

What happens to the memory areas previously allocated for wqs and engines after idxd_setup_groups() fails? They need to be freed as well, but currently they are not.

Quoted 2 lines

Maybe a separate patch cleans up the previously allocated mem areas for wqs/engines/groups if there is any failure after the allocations?

Agreed, will add a new patch to fix it.

Thanks
Shuai

author
#15 lore raw

PATCH v2 4/7 dmaengine: idxd: fix memory leak in error handling path of idxd_alloc

In reply to Shuai Xue

Memory allocated for idxd is not freed if an error occurs during idxd_alloc(). 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")
---
 drivers/dma/idxd/init.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
drivers/dma/idxd/init.c +15 -9
index a2da68e6144d..dc34830fe7c3 100644
@@ -567,28 +567,34 @@ static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_d
 	idxd_dev_set_type(&idxd->idxd_dev, idxd->data->type);
 	idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
 	if (idxd->id < 0)
-		return NULL;
+		goto err_ida;
 
 	idxd->opcap_bmap = bitmap_zalloc_node(IDXD_MAX_OPCAP_BITS, GFP_KERNEL, dev_to_node(dev));
-	if (!idxd->opcap_bmap) {
-		ida_free(&idxd_ida, idxd->id);
-		return NULL;
-	}
+	if (!idxd->opcap_bmap)
+		goto err_opcap;
 
 	device_initialize(conf_dev);
 	conf_dev->parent = dev;
 	conf_dev->bus = &dsa_bus_type;
 	conf_dev->type = idxd->data->dev_type;
 	rc = dev_set_name(conf_dev, "%s%d", idxd->data->name_prefix, idxd->id);
-	if (rc < 0) {
-		put_device(conf_dev);
-		return NULL;
-	}
+	if (rc < 0)
+		goto err_name;
 
 	spin_lock_init(&idxd->dev_lock);
 	spin_lock_init(&idxd->cmd_lock);
 
 	return idxd;
+
+err_name:
+	put_device(conf_dev);
+	bitmap_free(idxd->opcap_bmap);
+err_opcap:
+	ida_free(&idxd_ida, idxd->id);
+err_ida:
+	kfree(idxd);
+
+	return NULL;
 }
 
 static int idxd_enable_system_pasid(struct idxd_device *idxd)
Signature · 1 line
2.39.3
author
#16 lore raw

PATCH v2 5/7 dmaengine: idxd: fix memory leak in error handling path of idxd_pci_probe

In reply to Shuai Xue

Memory allocated for idxd is not freed if an error occurs during idxd_pci_probe(). To fix it, free the allocated memory in the reverse order of allocation before exiting the function in case of an error.

Fixes bfe1d56091c1 ("dmaengine: idxd: Init and probe for Intel data accelerators")
---
 drivers/dma/idxd/init.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/dma/idxd/init.c +9 -1
index dc34830fe7c3..ac1cdc1d82bf 100644
@@ -550,6 +550,14 @@ static void idxd_read_caps(struct idxd_device *idxd)
 		idxd->hw.iaa_cap.bits = ioread64(idxd->reg_base + IDXD_IAACAP_OFFSET);
 }
 
+static void idxd_free(struct idxd_device *idxd)
+{
+	put_device(idxd_confdev(idxd));
+	bitmap_free(idxd->opcap_bmap);
+	ida_free(&idxd_ida, idxd->id);
+	kfree(idxd);
+}
+
 static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data)
 {
 	struct device *dev = &pdev->dev;
@@ -1219,7 +1227,7 @@ int idxd_pci_probe_alloc(struct idxd_device *idxd, struct pci_dev *pdev,
  err:
 	pci_iounmap(pdev, idxd->reg_base);
  err_iomap:
-	put_device(idxd_confdev(idxd));
+	idxd_free(idxd);
  err_idxd_alloc:
 	pci_disable_device(pdev);
 	return rc;
Signature · 1 line
2.39.3
#17 lore raw

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

Hi, Shuai,

Quoted 16 lines · Shuai Xue

Memory allocated for idxd is not freed if an error occurs during idxd_pci_probe(). To fix it, free the allocated memory in the reverse order of allocation before exiting the function in case of an error.

Fixes bfe1d56091c1 ("dmaengine: idxd: Init and probe for Intel data accelerators")
---
  drivers/dma/idxd/init.c | 10 +++++++++-
  1 file changed, 9 insertions(+), 1 deletion(-)
drivers/dma/idxd/init.c +0 -0
index dc34830fe7c3..ac1cdc1d82bf 100644

drivers/dma/idxd/init.c static void idxd_read_caps(struct idxd_device *idxd) · line 558

Quoted 9 lines
drivers/dma/idxd/init.c +6 -0
@@ -550,6 +550,14 @@ static void idxd_read_caps(struct idxd_device *idxd)
  		idxd->hw.iaa_cap.bits = ioread64(idxd->reg_base + IDXD_IAACAP_OFFSET);
  }
 
+static void idxd_free(struct idxd_device *idxd)
+{
+	put_device(idxd_confdev(idxd));
+	bitmap_free(idxd->opcap_bmap);
+	ida_free(&idxd_ida, idxd->id);
+	kfree(idxd);

opcap_bmap, idxd_ida, idxd are NOT allocated during FLR re-init idxd device. In the FLR case, they should not be freed.

Quoted 6 lines
drivers/dma/idxd/init.c +2 -0
+}
+
  static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data)
  {
  	struct device *dev = &pdev->dev;

drivers/dma/idxd/init.c int idxd_pci_probe_alloc(struct idxd_device *idxd, struct pci_dev *pdev, · line 1233

Quoted 8 lines
drivers/dma/idxd/init.c +1 -1
@@ -1219,7 +1227,7 @@ int idxd_pci_probe_alloc(struct idxd_device *idxd, struct pci_dev *pdev,
   err:
  	pci_iounmap(pdev, idxd->reg_base);
   err_iomap:
-	put_device(idxd_confdev(idxd));
+	idxd_free(idxd);
   err_idxd_alloc:
  	pci_disable_device(pdev);
  	return rc;

Thanks.

-Fenghua

author
#18 lore raw

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

Quoted 14 lines · Fenghua Yu

Hi, Shuai,

Quoted 11 lines · Shuai Xue

Memory allocated for idxd is not freed if an error occurs during idxd_pci_probe(). To fix it, free the allocated memory in the reverse order of allocation before exiting the function in case of an error.

Fixes bfe1d56091c1 ("dmaengine: idxd: Init and probe for Intel data accelerators")

---
  drivers/dma/idxd/init.c | 10 +++++++++-

  1 file changed, 9 insertions(+), 1 deletion(-)
Quoted 15 lines

drivers/dma/idxd/init.c static void idxd_read_caps(struct idxd_device *idxd) · line 557

Quoted 13 lines
drivers/dma/idxd/init.c +6 -0
index dc34830fe7c3..ac1cdc1d82bf 100644
@@ -550,6 +550,14 @@ static void idxd_read_caps(struct idxd_device *idxd)
           idxd->hw.iaa_cap.bits = ioread64(idxd->reg_base + IDXD_IAACAP_OFFSET);
   }
+static void idxd_free(struct idxd_device *idxd)
+{
+    put_device(idxd_confdev(idxd));
+    bitmap_free(idxd->opcap_bmap);
+    ida_free(&idxd_ida, idxd->id);
+    kfree(idxd);

opcap_bmap, idxd_ida, idxd are NOT allocated during FLR re-init idxd device. In the FLR case, they should not be freed.

Great catch, thanks.

Will fix it in next version.

Shuai

author
#19 lore raw

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

In reply to Fenghua Yu
Quoted 14 lines · Fenghua Yu

Hi, Shuai,

Quoted 11 lines · Shuai Xue

Memory allocated for idxd is not freed if an error occurs during idxd_pci_probe(). To fix it, free the allocated memory in the reverse order of allocation before exiting the function in case of an error.

Fixes bfe1d56091c1 ("dmaengine: idxd: Init and probe for Intel data accelerators")

---
  drivers/dma/idxd/init.c | 10 +++++++++-

  1 file changed, 9 insertions(+), 1 deletion(-)
Quoted 16 lines

drivers/dma/idxd/init.c static void idxd_read_caps(struct idxd_device *idxd) · line 557

Quoted 13 lines
drivers/dma/idxd/init.c +6 -0
index dc34830fe7c3..ac1cdc1d82bf 100644
@@ -550,6 +550,14 @@ static void idxd_read_caps(struct idxd_device *idxd)
           idxd->hw.iaa_cap.bits = ioread64(idxd->reg_base + IDXD_IAACAP_OFFSET);
   }
+static void idxd_free(struct idxd_device *idxd)
+{
+    put_device(idxd_confdev(idxd));
+    bitmap_free(idxd->opcap_bmap);
+    ida_free(&idxd_ida, idxd->id);
+    kfree(idxd);

opcap_bmap, idxd_ida, idxd are NOT allocated during FLR re-init idxd device. In the FLR case, they should not be freed.

After relook the code, idxd_free() will not be called in FLR case, because all error lable in idxd_pci_probe_alloc() is called from alloc_idxd=true, not the FLR case.

Anyway, I will add a protection in idxd_free().

Thanks.
Shuai

author
#20 lore raw

PATCH v2 6/7 dmaengine: idxd: Add missing idxd cleanup to fix memory leak in remove call

In reply to Shuai Xue

The remove call stack is missing idxd cleanup to free bitmap, ida and the idxd_device. Call idxd_free() helper routines to make sure we exit gracefully.

Fixes bfe1d56091c1 ("dmaengine: idxd: Init and probe for Intel data accelerators")
---
 drivers/dma/idxd/init.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dma/idxd/init.c +1 -1
index ac1cdc1d82bf..f40f1c44a302 100644
@@ -1295,7 +1295,7 @@ static void idxd_remove(struct pci_dev *pdev)
 	pci_disable_device(pdev);
 	destroy_workqueue(idxd->wq);
 	perfmon_pmu_remove(idxd);
-	put_device(idxd_confdev(idxd));
+	idxd_free(idxd);
 }
 
 static struct pci_driver idxd_pci_driver = {
Signature · 1 line
2.39.3
author
#21 lore raw

PATCH v2 7/7 dmaengine: idxd: Refactor remove call with idxd_cleanup() helper

In reply to Shuai Xue

The idxd_cleanup() helper clean up perfmon, interrupts, internals and so on. Refactor remove call with idxd_cleanup() helper to avoid code duplication. Note, this also fixes the missing put_device() for idxd groups, enginces and wqs.

Fixes bfe1d56091c1 ("dmaengine: idxd: Init and probe for Intel data accelerators")
---
 drivers/dma/idxd/init.c | 13 ++-----------
 1 file changed, 2 insertions(+), 11 deletions(-)
drivers/dma/idxd/init.c +2 -11
index f40f1c44a302..0fbfbe024c29 100644
@@ -1282,20 +1282,11 @@ static void idxd_remove(struct pci_dev *pdev)
 	get_device(idxd_confdev(idxd));
 	device_unregister(idxd_confdev(idxd));
 	idxd_shutdown(pdev);
-	if (device_pasid_enabled(idxd))
-		idxd_disable_system_pasid(idxd);
 	idxd_device_remove_debugfs(idxd);
-
-	irq_entry = idxd_get_ie(idxd, 0);
-	free_irq(irq_entry->vector, irq_entry);
-	pci_free_irq_vectors(pdev);
+	idxd_cleanup(idxd);
 	pci_iounmap(pdev, idxd->reg_base);
-	if (device_user_pasid_enabled(idxd))
-		idxd_disable_sva(pdev);
-	pci_disable_device(pdev);
-	destroy_workqueue(idxd->wq);
-	perfmon_pmu_remove(idxd);
 	idxd_free(idxd);
+	pci_disable_device(pdev);
 }
 
 static struct pci_driver idxd_pci_driver = {
Signature · 1 line
2.39.3
#22 lore raw

Re: [PATCH v2 7/7] dmaengine: idxd: Refactor remove call with idxd_cleanup() helper

Hi, Shuai,

Quoted 1 line · Shuai Xue

The idxd_cleanup() helper clean up perfmon, interrupts, internals and so

s/clean/cleans/

Quoted 1 line

on. Refactor remove call with idxd_cleanup() helper to avoid code

s/idxd_cleanup()/the idxd_cleanup()/

Quoted 15 lines

duplication. Note, this also fixes the missing put_device() for idxd groups, enginces and wqs.

Fixes bfe1d56091c1 ("dmaengine: idxd: Init and probe for Intel data accelerators")
---
  drivers/dma/idxd/init.c | 13 ++-----------
  1 file changed, 2 insertions(+), 11 deletions(-)
drivers/dma/idxd/init.c +0 -0
index f40f1c44a302..0fbfbe024c29 100644

drivers/dma/idxd/init.c static void idxd_remove(struct pci_dev *pdev) · line 1282

Quoted 1 line
drivers/dma/idxd/init.c +0 -0
@@ -1282,20 +1282,11 @@ static void idxd_remove(struct pci_dev *pdev)
  	get_device(idxd_confdev(idxd));

get_device() is called here.

Quoted 7 lines
drivers/dma/idxd/init.c +0 -4
  	device_unregister(idxd_confdev(idxd));
  	idxd_shutdown(pdev);
-	if (device_pasid_enabled(idxd))
-		idxd_disable_system_pasid(idxd);
  	idxd_device_remove_debugfs(idxd);
-
-	irq_entry = idxd_get_ie(idxd, 0);

drivers/dma/idxd/init.c static void idxd_remove(struct pci_dev *pdev) · line 1288

Quoted 10 lines
drivers/dma/idxd/init.c +1 -7
-	free_irq(irq_entry->vector, irq_entry);
-	pci_free_irq_vectors(pdev);
+	idxd_cleanup(idxd);
  	pci_iounmap(pdev, idxd->reg_base);
-	if (device_user_pasid_enabled(idxd))
-		idxd_disable_sva(pdev);
-	pci_disable_device(pdev);
-	destroy_workqueue(idxd->wq);
-	perfmon_pmu_remove(idxd);
  	idxd_free(idxd);

put_device() is called inside idxd_free(). Seems not easy to read code to match the pair.

Plus idxd_free() is called only in non FLR case.

Maybe it's better to change this code to:

1. call put_device() outside idxd_free() so that it's easy to match the get_device() and put_deivce in the same level of function.

2. idxd_free() called here is OK because this is not in FLR handler. But only call it in non FLR path in idxd_pci_probe_alloc()

?

drivers/dma/idxd/init.c static void idxd_remove(struct pci_dev *pdev) · line 1292

Quoted 4 lines
drivers/dma/idxd/init.c +1 -0
+	pci_disable_device(pdev);
  }
 
  static struct pci_driver idxd_pci_driver = {

Thanks.

-Fenghua

author
#23 lore raw

Re: [PATCH v2 7/7] dmaengine: idxd: Refactor remove call with idxd_cleanup() helper

Quoted 45 lines · Fenghua Yu

Hi, Shuai,

Quoted 1 line · Shuai Xue

The idxd_cleanup() helper clean up perfmon, interrupts, internals and so

s/clean/cleans/

Quoted 1 line

on. Refactor remove call with idxd_cleanup() helper to avoid code

s/idxd_cleanup()/the idxd_cleanup()/

Quoted 15 lines

duplication. Note, this also fixes the missing put_device() for idxd groups, enginces and wqs.

Fixes bfe1d56091c1 ("dmaengine: idxd: Init and probe for Intel data accelerators")
---
  drivers/dma/idxd/init.c | 13 ++-----------
  1 file changed, 2 insertions(+), 11 deletions(-)
drivers/dma/idxd/init.c +0 -0
index f40f1c44a302..0fbfbe024c29 100644

drivers/dma/idxd/init.c static void idxd_remove(struct pci_dev *pdev) · line 1282

Quoted 1 line
drivers/dma/idxd/init.c +0 -0
@@ -1282,20 +1282,11 @@ static void idxd_remove(struct pci_dev *pdev)
      get_device(idxd_confdev(idxd));

get_device() is called here.

Quoted 7 lines
drivers/dma/idxd/init.c +0 -4
      device_unregister(idxd_confdev(idxd));
      idxd_shutdown(pdev);
-    if (device_pasid_enabled(idxd))
-        idxd_disable_system_pasid(idxd);
      idxd_device_remove_debugfs(idxd);
-
-    irq_entry = idxd_get_ie(idxd, 0);

drivers/dma/idxd/init.c static void idxd_remove(struct pci_dev *pdev) · line 1288

Quoted 10 lines
drivers/dma/idxd/init.c +1 -7
-    free_irq(irq_entry->vector, irq_entry);
-    pci_free_irq_vectors(pdev);
+    idxd_cleanup(idxd);
      pci_iounmap(pdev, idxd->reg_base);
-    if (device_user_pasid_enabled(idxd))
-        idxd_disable_sva(pdev);
-    pci_disable_device(pdev);
-    destroy_workqueue(idxd->wq);
-    perfmon_pmu_remove(idxd);
      idxd_free(idxd);
Quoted 1 line

put_device() is called inside idxd_free(). Seems not easy to read code to match the pair.

IMHO, idxd_free() is paired with idxd_alloc() which grap a reference count by device_initialize(). So, we should match that right pair.

Quoted 2 lines

* When ->release() is called for the idxd->conf_dev, it frees all the memory related
* to the idxd context.

I did not figure out why you explictly grab reference count of idxd_confdev(idxd).

idxd_unregister_devices() is paired with idxd_register_devices(), it only decrease reference through wqs, engines, and groups. So a refcnt of idxd->conf_dev is still hold by idxd_alloc().

Please correct me, if I missed anything.

Quoted 6 lines

Plus idxd_free() is called only in non FLR case.

Maybe it's better to change this code to:

1. call put_device() outside idxd_free() so that it's easy to match the get_device() and put_deivce in the same level of function.

See my comments above.

Quoted 3 lines

2. idxd_free() called here is OK because this is not in FLR handler. But only call it in non FLR path in idxd_pci_probe_alloc()

Exactly, so, shoud I add a protection in idxd_free() in a fact that non FLR case will not call it.

Thanks.
Shuai

author
#24 lore raw

Re: [PATCH v2 7/7] dmaengine: idxd: Refactor remove call with idxd_cleanup() helper

Quoted 74 lines · Shuai Xue
Quoted 45 lines · Fenghua Yu

Hi, Shuai,

Quoted 1 line · Shuai Xue

The idxd_cleanup() helper clean up perfmon, interrupts, internals and so

s/clean/cleans/

Quoted 1 line

on. Refactor remove call with idxd_cleanup() helper to avoid code

s/idxd_cleanup()/the idxd_cleanup()/

Quoted 15 lines

duplication. Note, this also fixes the missing put_device() for idxd groups, enginces and wqs.

Fixes bfe1d56091c1 ("dmaengine: idxd: Init and probe for Intel data accelerators")

---
  drivers/dma/idxd/init.c | 13 ++-----------

  1 file changed, 2 insertions(+), 11 deletions(-)
drivers/dma/idxd/init.c +0 -0
index f40f1c44a302..0fbfbe024c29 100644

drivers/dma/idxd/init.c static void idxd_remove(struct pci_dev *pdev) · line 1282

Quoted 1 line
drivers/dma/idxd/init.c +0 -0
@@ -1282,20 +1282,11 @@ static void idxd_remove(struct pci_dev *pdev)
       get_device(idxd_confdev(idxd));

get_device() is called here.

Quoted 7 lines
drivers/dma/idxd/init.c +0 -4
       device_unregister(idxd_confdev(idxd));
       idxd_shutdown(pdev);
-    if (device_pasid_enabled(idxd))
-        idxd_disable_system_pasid(idxd);
       idxd_device_remove_debugfs(idxd);
-
-    irq_entry = idxd_get_ie(idxd, 0);

drivers/dma/idxd/init.c static void idxd_remove(struct pci_dev *pdev) · line 1288

Quoted 10 lines
drivers/dma/idxd/init.c +1 -7
-    free_irq(irq_entry->vector, irq_entry);
-    pci_free_irq_vectors(pdev);
+    idxd_cleanup(idxd);
       pci_iounmap(pdev, idxd->reg_base);
-    if (device_user_pasid_enabled(idxd))
-        idxd_disable_sva(pdev);
-    pci_disable_device(pdev);
-    destroy_workqueue(idxd->wq);
-    perfmon_pmu_remove(idxd);
       idxd_free(idxd);
Quoted 1 line

put_device() is called inside idxd_free(). Seems not easy to read code to match the pair.

IMHO, idxd_free() is paired with idxd_alloc() which grap a reference count by device_initialize(). So, we should match that right pair.

Quoted 2 lines

* When ->release() is called for the idxd->conf_dev, it frees all the memory related
* to the idxd context.

I did not figure out why you explictly grab reference count of idxd_confdev(idxd).

idxd_unregister_devices() is paired with idxd_register_devices(), it only decrease reference through wqs, engines, and groups. So a refcnt of idxd->conf_dev is still hold by idxd_alloc().

Please correct me, if I missed anything.

Quoted 6 lines

Plus idxd_free() is called only in non FLR case.

Maybe it's better to change this code to:

1. call put_device() outside idxd_free() so that it's easy to match the get_device() and put_deivce in the same level of function.

See my comments above.

Quoted 1 line
Quoted 7 lines
Quoted 2 lines

2. idxd_free() called here is OK because this is not in FLR handler. But only call it in non FLR path in idxd_pci_probe_alloc()

Exactly, so, shoud I add a protection in idxd_free() in a fact that non FLR case will not call it.

Thanks.
Shuai

Hi, all,

Any feedback?

Thanks.
Shuai