[PATCH v3] bus: fsl-mc: fix an error handling in fsl_mc_device_add()

Haoxiang Li posted 1 patch 1 week, 6 days ago
drivers/bus/fsl-mc/fsl-mc-bus.c | 6 +-----
1 file changed, 1 insertion(+), 5 deletions(-)
[PATCH v3] bus: fsl-mc: fix an error handling in fsl_mc_device_add()
Posted by Haoxiang Li 1 week, 6 days ago
In fsl_mc_device_add(), device_initialize() is called first.
put_device() should be called to drop the reference if error
occurs. And other resources would be released via put_device
-> fsl_mc_device_release. So remove redundant kfree() in
error handling path.

Fixes: bbf9d17d9875 ("staging: fsl-mc: Freescale Management Complex (fsl-mc) bus driver")
Cc: stable@vger.kernel.org
Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
Closes: https://lore.kernel.org/all/b767348e-d89c-416e-acea-1ebbff3bea20@stanley.mountain/
Signed-off-by: Su Hui <suhui@nfschina.com>
Suggested-by: Christophe Leroy (CS GROUP) <chleroy@kernel.org>
Signed-off-by: Haoxiang Li <lihaoxiang@isrc.iscas.ac.cn>
---
Changes in v2:
- fix a patch error. Thanks, Christophe.
- add specific changelog. Thanks, Dan.
Changes in v3:
- Also call put_device() in other error paths. Thanks, Ioana.
---
 drivers/bus/fsl-mc/fsl-mc-bus.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/drivers/bus/fsl-mc/fsl-mc-bus.c b/drivers/bus/fsl-mc/fsl-mc-bus.c
index 25845c04e562..6d132144ce25 100644
--- a/drivers/bus/fsl-mc/fsl-mc-bus.c
+++ b/drivers/bus/fsl-mc/fsl-mc-bus.c
@@ -905,11 +905,7 @@ int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc,
 	return 0;
 
 error_cleanup_dev:
-	kfree(mc_dev->regions);
-	if (mc_bus)
-		kfree(mc_bus);
-	else
-		kfree(mc_dev);
+	put_device(&mc_dev->dev);
 
 	return error;
 }
-- 
2.25.1
Re: [PATCH v3] bus: fsl-mc: fix an error handling in fsl_mc_device_add()
Posted by Christophe Leroy (CS GROUP) 1 week, 2 days ago
On Sat, 24 Jan 2026 18:20:54 +0800, Haoxiang Li wrote:
> In fsl_mc_device_add(), device_initialize() is called first.
> put_device() should be called to drop the reference if error
> occurs. And other resources would be released via put_device
> -> fsl_mc_device_release. So remove redundant kfree() in
> error handling path.
> 
> 
> [...]

Applied, thanks!

[1/1] bus: fsl-mc: fix an error handling in fsl_mc_device_add()
      commit: 52f527d0916bcdd7621a0c9e7e599b133294d495

Best regards,
-- 
Christophe Leroy (CS GROUP) <chleroy@kernel.org>
Re: [PATCH v3] bus: fsl-mc: fix an error handling in fsl_mc_device_add()
Posted by Ioana Ciornei 1 week, 2 days ago
On Sat, Jan 24, 2026 at 06:20:54PM +0800, Haoxiang Li wrote:
> In fsl_mc_device_add(), device_initialize() is called first.
> put_device() should be called to drop the reference if error
> occurs. And other resources would be released via put_device
> -> fsl_mc_device_release. So remove redundant kfree() in
> error handling path.
> 
> Fixes: bbf9d17d9875 ("staging: fsl-mc: Freescale Management Complex (fsl-mc) bus driver")
> Cc: stable@vger.kernel.org
> Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> Closes: https://lore.kernel.org/all/b767348e-d89c-416e-acea-1ebbff3bea20@stanley.mountain/
> Signed-off-by: Su Hui <suhui@nfschina.com>
> Suggested-by: Christophe Leroy (CS GROUP) <chleroy@kernel.org>
> Signed-off-by: Haoxiang Li <lihaoxiang@isrc.iscas.ac.cn>

Reviewed-by: Ioana Ciornei <ioana.ciornei@nxp.com>
Re: [PATCH v3] bus: fsl-mc: fix an error handling in fsl_mc_device_add()
Posted by Dan Carpenter 1 week, 6 days ago
On Sat, Jan 24, 2026 at 06:20:54PM +0800, Haoxiang Li wrote:
> In fsl_mc_device_add(), device_initialize() is called first.
> put_device() should be called to drop the reference if error
> occurs. And other resources would be released via put_device
> -> fsl_mc_device_release. So remove redundant kfree() in
> error handling path.
> 

It is true that we shouldn't free things directly after calling
device_initialize().  I don't know the impact of this bug in
real life.  Is it a leak?

> Fixes: bbf9d17d9875 ("staging: fsl-mc: Freescale Management Complex (fsl-mc) bus driver")
> Cc: stable@vger.kernel.org
> Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> Closes: https://lore.kernel.org/all/b767348e-d89c-416e-acea-1ebbff3bea20@stanley.mountain/

Heh.  What was I even talking about when I wrote this???

In my head I remember the code as looking like this:
https://lore.kernel.org/all/20251222074958.992911-1-lihaoxiang@isrc.iscas.ac.cn/
But that's not the version of the code that I copy and pasted into my
email.

The release function looks like this:

drivers/bus/fsl-mc/fsl-mc-bus.c
   757  static void fsl_mc_device_release(struct device *dev)
   758  {
   759          struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
   760  
   761          kfree(mc_dev->regions);
   762  
   763          if (is_fsl_mc_bus_dprc(mc_dev))
   764                  kfree(to_fsl_mc_bus(mc_dev));
   765          else
   766                  kfree(mc_dev);
   767  }

The problem is that if this function call fails:

	mc_dev->dev.type = fsl_mc_get_device_type(obj_desc->type);

Then the is_fsl_mc_bus_dprc() check might not work.  In the current
code the to_fsl_mc_bus() pointer math is a no-op because mc_dev is
the first struct member of mc_bus.  So it works for now, but it
feels wrong.

The fsl_mc_get_device_type() function can't really fail in real
life.

regards,
dan carpenter
Re: [PATCH v3] bus: fsl-mc: fix an error handling in fsl_mc_device_add()
Posted by Ioana Ciornei 1 week, 4 days ago
On Sat, Jan 24, 2026 at 01:47:24PM +0300, Dan Carpenter wrote:
> On Sat, Jan 24, 2026 at 06:20:54PM +0800, Haoxiang Li wrote:
> > In fsl_mc_device_add(), device_initialize() is called first.
> > put_device() should be called to drop the reference if error
> > occurs. And other resources would be released via put_device
> > -> fsl_mc_device_release. So remove redundant kfree() in
> > error handling path.
> > 
> 
> It is true that we shouldn't free things directly after calling
> device_initialize().  I don't know the impact of this bug in
> real life.  Is it a leak?
> 
> > Fixes: bbf9d17d9875 ("staging: fsl-mc: Freescale Management Complex (fsl-mc) bus driver")
> > Cc: stable@vger.kernel.org
> > Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> > Closes: https://lore.kernel.org/all/b767348e-d89c-416e-acea-1ebbff3bea20@stanley.mountain/
> 
> Heh.  What was I even talking about when I wrote this???
> 
> In my head I remember the code as looking like this:
> https://lore.kernel.org/all/20251222074958.992911-1-lihaoxiang@isrc.iscas.ac.cn/
> But that's not the version of the code that I copy and pasted into my
> email.
> 
> The release function looks like this:
> 
> drivers/bus/fsl-mc/fsl-mc-bus.c
>    757  static void fsl_mc_device_release(struct device *dev)
>    758  {
>    759          struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
>    760  
>    761          kfree(mc_dev->regions);
>    762  
>    763          if (is_fsl_mc_bus_dprc(mc_dev))
>    764                  kfree(to_fsl_mc_bus(mc_dev));
>    765          else
>    766                  kfree(mc_dev);
>    767  }
> 
> The problem is that if this function call fails:
> 
> 	mc_dev->dev.type = fsl_mc_get_device_type(obj_desc->type);
> 
> Then the is_fsl_mc_bus_dprc() check might not work.  In the current
> code the to_fsl_mc_bus() pointer math is a no-op because mc_dev is
> the first struct member of mc_bus.  So it works for now, but it
> feels wrong.
> 
> The fsl_mc_get_device_type() function can't really fail in real
> life.
> 

I agree, the fsl_mc_get_device_type() function will not fail in real
life circumstances. And let's say that it fails, then we most certainly
are not handling a DPRC device, aka a bus device as checked by the
is_fsl_mc_bus_dprc() function. This means that fsl_mc_device_release()
will execute the 'kfree(mc_dev)' from the else branch as it was also in
the initial code under the error_cleanup_dev label.

Ioana