Re: [PATCH v2 4/8] dmaengine: idxd: Fix the device registration error paths
From: Dave Jiang
Date: Mon Nov 30 2026 - 12:03:41 EST
On 11/25/26 4:49 PM, Vinicius Costa Gomes wrote:
> idxd_setup_*() takes a reference on each wq, engine and group device,
> and idxd_cleanup_internals() drops it. When registering the devices
> fails, the error paths drop it first:
>
> idxd_pci_probe_alloc()
> |-idxd_register_devices()
> | \-idxd_register_{wq,engine,group}_devices()
> | put_device() or device_unregister() on each device.
> | The last reference is gone, the device is freed.
> |
> \-idxd_cleanup()
> \-idxd_cleanup_internals()
> put_device() on each device again, use-after-free.
>
> idxd_register_devices() does the same, with device_unregister(), for
> the devices registered before the failure.
>
> The rollback in the helpers is also off by one. With j = i - 1 and
> while (j--), it skips the last device added. When the first device
> fails, j starts at -1 and the loop walks off the front of the array.
>
> Only device_del() the devices that were added, and leave the references
> to idxd_cleanup_internals().
>
> Fixes: 7c5dd23e57c1 ("dmaengine: idxd: fix wq conf_dev 'struct device' lifetime")
> Fixes: 75b911309060 ("dmaengine: idxd: fix engine conf_dev lifetime")
> Fixes: defe49f96012 ("dmaengine: idxd: fix group conf_dev lifetime")
> Fixes: ddf742d4f3f1 ("dmaengine: idxd: Add missing cleanup for early error out in probe call")
> Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@xxxxxxxxx>
Reviewed-by: Dave Jiang <dave.jiang@xxxxxxxxx>
> ---
> drivers/dma/idxd/sysfs.c | 40 +++++++++++-----------------------------
> 1 file changed, 11 insertions(+), 29 deletions(-)
>
> diff --git a/drivers/dma/idxd/sysfs.c b/drivers/dma/idxd/sysfs.c
> index d5ffc641c856..87fa4f43e27c 100644
> --- a/drivers/dma/idxd/sysfs.c
> +++ b/drivers/dma/idxd/sysfs.c
> @@ -1864,7 +1864,7 @@ const struct device_type iax_device_type = {
> static int idxd_register_engine_devices(struct idxd_device *idxd)
> {
> struct idxd_engine *engine;
> - int i, j, rc;
> + int i, rc;
>
> for (i = 0; i < idxd->max_engines; i++) {
> engine = idxd->engines[i];
> @@ -1876,15 +1876,9 @@ static int idxd_register_engine_devices(struct idxd_device *idxd)
> return 0;
>
> cleanup:
> - j = i - 1;
> - for (; i < idxd->max_engines; i++) {
> + while (i--) {
> engine = idxd->engines[i];
> - put_device(engine_confdev(engine));
> - }
> -
> - while (j--) {
> - engine = idxd->engines[j];
> - device_unregister(engine_confdev(engine));
> + device_del(engine_confdev(engine));
> }
> return rc;
> }
> @@ -1892,7 +1886,7 @@ static int idxd_register_engine_devices(struct idxd_device *idxd)
> static int idxd_register_group_devices(struct idxd_device *idxd)
> {
> struct idxd_group *group;
> - int i, j, rc;
> + int i, rc;
>
> for (i = 0; i < idxd->max_groups; i++) {
> group = idxd->groups[i];
> @@ -1904,15 +1898,9 @@ static int idxd_register_group_devices(struct idxd_device *idxd)
> return 0;
>
> cleanup:
> - j = i - 1;
> - for (; i < idxd->max_groups; i++) {
> + while (i--) {
> group = idxd->groups[i];
> - put_device(group_confdev(group));
> - }
> -
> - while (j--) {
> - group = idxd->groups[j];
> - device_unregister(group_confdev(group));
> + device_del(group_confdev(group));
> }
> return rc;
> }
> @@ -1920,7 +1908,7 @@ static int idxd_register_group_devices(struct idxd_device *idxd)
> static int idxd_register_wq_devices(struct idxd_device *idxd)
> {
> struct idxd_wq *wq;
> - int i, rc, j;
> + int i, rc;
>
> for (i = 0; i < idxd->max_wqs; i++) {
> wq = idxd->wqs[i];
> @@ -1932,15 +1920,9 @@ static int idxd_register_wq_devices(struct idxd_device *idxd)
> return 0;
>
> cleanup:
> - j = i - 1;
> - for (; i < idxd->max_wqs; i++) {
> + while (i--) {
> wq = idxd->wqs[i];
> - put_device(wq_confdev(wq));
> - }
> -
> - while (j--) {
> - wq = idxd->wqs[j];
> - device_unregister(wq_confdev(wq));
> + device_del(wq_confdev(wq));
> }
> return rc;
> }
> @@ -1976,10 +1958,10 @@ int idxd_register_devices(struct idxd_device *idxd)
>
> err_group:
> for (i = 0; i < idxd->max_engines; i++)
> - device_unregister(engine_confdev(idxd->engines[i]));
> + device_del(engine_confdev(idxd->engines[i]));
> err_engine:
> for (i = 0; i < idxd->max_wqs; i++)
> - device_unregister(wq_confdev(idxd->wqs[i]));
> + device_del(wq_confdev(idxd->wqs[i]));
> err_wq:
> device_del(idxd_confdev(idxd));
> return rc;
>