Skip to contentSkip to conversation

edac: al_mc: free mem_ctl_info on error path in .probe()

PATCH Shenhar, Talel 2 participants 2 messages Dec 5, 2024

Subsystems (2)
lore
Share & tools
lore

Shenhar, Talel

author maintainer
#0 lore raw

Thanks, Joe, for reaching out.

I noticed that your patch explicitly calls edac_mc_free() in the error path, but it seems this is already handled by the devm framework. Specifically, in devm_add_action_or_reset():
static void devm_al_mc_edac_free(void *data)

{
    edac_mc_free(data);
}

This ensures that edac_mc_free() is called automatically if an error occurs during initialization or the device is removed.
Could you clarify the memory leak scenario you're addressing? Are there specific cases where the devm handling might not be triggered, leading to the observed leak? If such a scenario exists, understanding the exact path where devm_al_mc_edac_free() is bypassed would help evaluate whether the explicit call is necessary. Without such a case, the patch might introduce redundant cleanup logic.

Looking forward to your insights!

Best regards,
Talel

-----Original Message-----
From: Joe Hattori <[email protected]>
Sent: Thursday, December 5, 2024 12:58 PM
To: Shenhar, Talel <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; Joe Hattori <[email protected]>
Subject: [EXTERNAL] [PATCH] edac: al_mc: free mem_ctl_info on error path in .probe()

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.

In al_mc_edac_probe(), the mem_ctl_info allocated by edac_mc_alloc() is not freed in the error path, resulting in a memory leak. Add a edac_mc_free() call to fix this.

Fixes e23a7cdeb3da ("EDAC/al-mc-edac: Add Amazon's Annapurna Labs Memory Controller driver")
---
 drivers/edac/al_mc_edac.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/edac/al_mc_edac.c +3 -3
@@ -240,7 +240,7 @@ static int al_mc_edac_probe(struct platform_device *pdev)
 
        ret = devm_add_action_or_reset(&pdev->dev, devm_al_mc_edac_free, mci);
        if (ret)
-               return ret;
+               goto free;
 
        platform_set_drvdata(pdev, mci);
        al_mc = mci->pvt_info;
@@ -288,12 +288,12 @@ static int al_mc_edac_probe(struct platform_device *pdev)
                dev_err(&pdev->dev,
                        "fail to add memory controller device (%d)\n",
                        ret);
-               return ret;
+               goto free;
        }
 
        ret = devm_add_action_or_reset(&pdev->dev, devm_al_mc_edac_del, &pdev->dev);
        if (ret)
-               return ret;
+               goto free;
 
        if (al_mc->irq_ue > 0) {
                ret = devm_request_irq(&pdev->dev, @@ -306,7 +306,7 @@ static int al_mc_edac_probe(struct platform_device *pdev)
                        dev_err(&pdev->dev,
                                "failed to request UE IRQ %d (%d)\n",
                                al_mc->irq_ue, ret);
-                       return ret;
+                       goto free;
                }
        }

@@ -321,11 +321,15 @@ static int al_mc_edac_probe(struct platform_device *pdev)

                        dev_err(&pdev->dev,
                                "failed to request CE IRQ %d (%d)\n",
                                al_mc->irq_ce, ret);
-                       return ret;
+                       goto free;
                }
        }

        return 0;

+
+free:
+ edac_mc_free(mci);
+ return ret;
}

static const struct of_device_id al_mc_edac_of_match[] = {
--
2.34.1

#1 lore raw

Hi Talel,

Quoted 8 lines · Talel

Thanks, Joe, for reaching out.

I noticed that your patch explicitly calls edac_mc_free() in the error path, but it seems this is already handled by the devm framework. Specifically, in devm_add_action_or_reset():
static void devm_al_mc_edac_free(void *data)

{
     edac_mc_free(data);
}

Yes, you are absolutely right, I overlooked that the devm framework already handles the cleanup process. Please disregard my patch.

Quoted 85 lines

This ensures that edac_mc_free() is called automatically if an error occurs during initialization or the device is removed.
Could you clarify the memory leak scenario you're addressing? Are there specific cases where the devm handling might not be triggered, leading to the observed leak? If such a scenario exists, understanding the exact path where devm_al_mc_edac_free() is bypassed would help evaluate whether the explicit call is necessary. Without such a case, the patch might introduce redundant cleanup logic.

Looking forward to your insights!

Best regards,
Talel

-----Original Message-----
From: Joe Hattori <[email protected]>
Sent: Thursday, December 5, 2024 12:58 PM
To: Shenhar, Talel <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; Joe Hattori <[email protected]>
Subject: [EXTERNAL] [PATCH] edac: al_mc: free mem_ctl_info on error path in .probe()

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.

In al_mc_edac_probe(), the mem_ctl_info allocated by edac_mc_alloc() is not freed in the error path, resulting in a memory leak. Add a edac_mc_free() call to fix this.

Fixes e23a7cdeb3da ("EDAC/al-mc-edac: Add Amazon's Annapurna Labs Memory Controller driver")
---
  drivers/edac/al_mc_edac.c | 14 +++++++++-----
  1 file changed, 9 insertions(+), 5 deletions(-)
drivers/edac/al_mc_edac.c +3 -3
@@ -240,7 +240,7 @@ static int al_mc_edac_probe(struct platform_device *pdev)
 
         ret = devm_add_action_or_reset(&pdev->dev, devm_al_mc_edac_free, mci);
         if (ret)
-               return ret;
+               goto free;
 
         platform_set_drvdata(pdev, mci);
         al_mc = mci->pvt_info;
@@ -288,12 +288,12 @@ static int al_mc_edac_probe(struct platform_device *pdev)
                 dev_err(&pdev->dev,
                         "fail to add memory controller device (%d)\n",
                         ret);
-               return ret;
+               goto free;
         }
 
         ret = devm_add_action_or_reset(&pdev->dev, devm_al_mc_edac_del, &pdev->dev);
         if (ret)
-               return ret;
+               goto free;
 
         if (al_mc->irq_ue > 0) {
                 ret = devm_request_irq(&pdev->dev, @@ -306,7 +306,7 @@ static int al_mc_edac_probe(struct platform_device *pdev)
                         dev_err(&pdev->dev,
                                 "failed to request UE IRQ %d (%d)\n",
                                 al_mc->irq_ue, ret);
drivers/edac/al_mc_edac.c +6 -3
-                       return ret;
+                       goto free;
                 }
         }
@@ -321,11 +321,15 @@ static int al_mc_edac_probe(struct platform_device *pdev)
                         dev_err(&pdev->dev,
                                 "failed to request CE IRQ %d (%d)\n",
                                 al_mc->irq_ce, ret);
-                       return ret;
+                       goto free;
                 }
         }
 
         return 0;
+
+free:
+       edac_mc_free(mci);
+       return ret;
  }
 
  static const struct of_device_id al_mc_edac_of_match[] = {
--
Quoted 2 lines

2.34.1

Best,
Joe