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: [email protected]
> > Reported-by: Dan Carpenter <[email protected]>
> > Closes:
> > https://lore.kernel.org/all/[email protected]/
>
> 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/[email protected]/
> 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