Re: [PATCH v2 1/4] dmaengine: put a channel device whose registration failed
From: Frank Li
Date: Mon Nov 30 2026 - 16:13:26 EST
On Mon, Nov 30, 2026 at 11:52:00AM -0800, Daniel Byrne wrote:
> __dma_async_device_channel_register() frees a channel's device with
> kfree() when device_register() fails. device_register() has initialized
> the device by then, so its reference has to be dropped with
> put_device(), and the name set by dev_set_name() leaks otherwise.
>
> Drop the reference, and let the release free the device together with
> its name. The channel's id is freed first, since the release does not
> free it, and the ida label that no longer has a user goes away.
>
> The registration of a channel failed with -EEXIST when the channel was
> handed an id whose sysfs device still existed, which "dmaengine: Free
> channel ID after device_unregister()" [1] fixes. The registration can
> still fail for want of memory.
>
> Fixes: 257b17ca0303 ("dmaengine: fail device registration if channel registration fails")
> Signed-off-by: Daniel Byrne <daniel1.byrne@xxxxxxxxx>
> Link: https://lore.kernel.org/r/20261110101607.13784-1-zhejun_huang@xxxxxxxxxxxxxxxxx [1]
> ---
Reviewed-by: Frank Li <Frank.Li@xxxxxxx>
> New in v2.
>
> drivers/dma/dmaengine.c | 14 ++++++++++----
> 1 file changed, 10 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/dma/dmaengine.c b/drivers/dma/dmaengine.c
> index 2642abf105d7..b1cb466f376f 100644
> --- a/drivers/dma/dmaengine.c
> +++ b/drivers/dma/dmaengine.c
> @@ -1126,15 +1126,21 @@ static int __dma_async_device_channel_register(struct dma_device *device,
> else
> dev_set_name(dmaengine_chan_dev(chan), "%s", name);
> rc = device_register(dmaengine_chan_dev(chan));
> - if (rc)
> - goto err_out_ida;
> + if (rc) {
> + /*
> + * device_register() initialized the device. Its release
> + * frees it, together with the name set above.
> + */
> + ida_free(&device->chan_ida, chan->chan_id);
> + put_device(dmaengine_chan_dev(chan));
> + chan->chan_dev = NULL;
> + goto err_free_local;
> + }
> chan->client_count = 0;
> device->chancnt++;
>
> return 0;
>
> - err_out_ida:
> - ida_free(&device->chan_ida, chan->chan_id);
> err_free_dev:
> kfree(chan->chan_dev);
> err_free_local:
> --
> 2.43.0
>