i3c: dw: Fix memory leak in dw_i3c_master_i3c_xfers()
Subsystems (2)
PATCH v2 1/1 i3c: dw: Fix memory leak in dw_i3c_master_i3c_xfers()
The dw_i3c_master_i3c_xfers() function allocates memory for the xfer structure using dw_i3c_master_alloc_xfer(). If pm_runtime_resume_and_get() fails, the function returns without freeing the allocated xfer, resulting in a memory leak.
Use the __free(kfree) cleanup attribute to automatically free the memory when the variable goes out of scope.
---
Changes in v2:
- Switch to use __free(kfree) to automatically free the memory.
- Link to v1: https://lore.kernel.org/r/[email protected]
--- drivers/i3c/master/dw-i3c-master.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/i3c/master/dw-i3c-master.c +2 -3
@@ -7,6 +7,7 @@#include <linux/bitfield.h> #include <linux/bitops.h> +#include <linux/cleanup.h> #include <linux/clk.h> #include <linux/completion.h> #include <linux/err.h>
@@ -924,7 +925,6 @@ static int dw_i3c_master_i3c_xfers(struct i3c_dev_desc *dev,struct i3c_master_controller *m =i3c_dev_get_master (dev);struct dw_i3c_master *master =to_dw_i3c_master (m);unsigned int nrxwords =0 , ntxwords =0 ; -struct dw_i3c_xfer *xfer;int i, ret =0 ;if (!i3c_nxfers)
@@ -944,7 +944,7 @@ static int dw_i3c_master_i3c_xfers(struct i3c_dev_desc *dev, nrxwords > master->caps.datafifodepth)return -EOPNOTSUPP ; - xfer =dw_i3c_master_alloc_xfer (master, i3c_nxfers); + struct dw_i3c_xfer *xfer __free(kfree) = dw_i3c_master_alloc_xfer(master, i3c_nxfers);if (!xfer)return -ENOMEM ;
@@ -995,7 +995,6 @@ static int dw_i3c_master_i3c_xfers(struct i3c_dev_desc *dev, } ret = xfer->ret; -dw_i3c_master_free_xfer (xfer);pm_runtime_put_autosuspend (master->dev);return ret;
---
base-commit: bd0f139e5fc11182777b81cefc3893ea508544ec
change-id: 20260402-dw-i3c-2-8f123def99b2
Best regards,
Signature · 1 line
Felix Gu <[email protected]>
Frank Li
reviewerQuoted 48 lines · Felix Gu
The
dw_i3c_master_i3c_xfers()function allocates memory for the xfer structure usingdw_i3c_master_alloc_xfer(). Ifpm_runtime_resume_and_get()fails, the function returns without freeing the allocated xfer, resulting in a memory leak.Use the
__free(kfree)cleanup attribute to automatically free the memory when the variable goes out of scope.Fixes 62fe9d06f570 ("i3c: dw: Add power management support")Signed-off-by Felix Gu <[email protected]>---
Changes in v2:
- Switch to use__free(kfree)to automatically free the memory.
- Link to v1: https://lore.kernel.org/r/[email protected]--- drivers/i3c/master/dw-i3c-master.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)drivers/i3c/master/dw-i3c-master.c +2
-2@@ -7,6 +7,7 @@#include <linux/bitfield.h> #include <linux/bitops.h> +#include <linux/cleanup.h> #include <linux/clk.h> #include <linux/completion.h> #include <linux/err.h> @@ -924,7 +925,6 @@ static int dw_i3c_master_i3c_xfers(struct i3c_dev_desc *dev,struct i3c_master_controller *m =i3c_dev_get_master (dev);struct dw_i3c_master *master =to_dw_i3c_master (m);unsigned int nrxwords =0 , ntxwords =0 ; -struct dw_i3c_xfer *xfer;int i, ret =0 ;if (!i3c_nxfers)@@ -944,7 +944,7 @@ static int dw_i3c_master_i3c_xfers(struct i3c_dev_desc *dev, nrxwords > master->caps.datafifodepth)return -EOPNOTSUPP ; - xfer =dw_i3c_master_alloc_xfer (master, i3c_nxfers); + struct dw_i3c_xfer *xfer __free(kfree) = dw_i3c_master_alloc_xfer(master, i3c_nxfers);if (!xfer)return -ENOMEM ;
drivers/i3c/master/dw-i3c-master.c static int dw_i3c_master_i3c_xfers(struct i3c_dev_desc *dev, · line 998
Quoted 4 lines
drivers/i3c/master/dw-i3c-master.c +0
-1@@ -995,7 +995,6 @@ static int dw_i3c_master_i3c_xfers(struct i3c_dev_desc *dev, } ret = xfer->ret; -dw_i3c_master_free_xfer (xfer);
You should mention dw_i3c_master_free_xfer() is simple wrap kfree() only in commit message.
You'd better create following patch to remove unnessary helper function dw_i3c_master_free_xfer() and change to other place by use cleanup.
Frank
Quoted 12 lines
drivers/i3c/master/dw-i3c-master.c +0
-1pm_runtime_put_autosuspend (master->dev);return ret; ---base-commit:
bd0f139e5fc11182777b81cefc3893ea508544ec
change-id: 20260402-dw-i3c-2-8f123def99b2Best regards,
--
Felix Gu <[email protected]>