Skip to content

Commit 64afd9a

Browse files
axiqiagregkh
authored andcommitted
dmaengine: idxd: fix memory leak in error handling path of idxd_alloc
commit 46a5cca76c76c86063000a12936f8e7875295838 upstream. Memory allocated for idxd is not freed if an error occurs during idxd_alloc(). To fix it, free the allocated memory in the reverse order of allocation before exiting the function in case of an error. Fixes: a8563a3 ("dmanegine: idxd: reformat opcap output to match bitmap_parse() input") Cc: [email protected] Signed-off-by: Shuai Xue <[email protected]> Reviewed-by: Dave Jiang <[email protected]> Reviewed-by: Fenghua Yu <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Vinod Koul <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 68ac5a0 commit 64afd9a

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

drivers/dma/idxd/init.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -537,28 +537,34 @@ static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_d
537537
idxd_dev_set_type(&idxd->idxd_dev, idxd->data->type);
538538
idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
539539
if (idxd->id < 0)
540-
return NULL;
540+
goto err_ida;
541541

542542
idxd->opcap_bmap = bitmap_zalloc_node(IDXD_MAX_OPCAP_BITS, GFP_KERNEL, dev_to_node(dev));
543-
if (!idxd->opcap_bmap) {
544-
ida_free(&idxd_ida, idxd->id);
545-
return NULL;
546-
}
543+
if (!idxd->opcap_bmap)
544+
goto err_opcap;
547545

548546
device_initialize(conf_dev);
549547
conf_dev->parent = dev;
550548
conf_dev->bus = &dsa_bus_type;
551549
conf_dev->type = idxd->data->dev_type;
552550
rc = dev_set_name(conf_dev, "%s%d", idxd->data->name_prefix, idxd->id);
553-
if (rc < 0) {
554-
put_device(conf_dev);
555-
return NULL;
556-
}
551+
if (rc < 0)
552+
goto err_name;
557553

558554
spin_lock_init(&idxd->dev_lock);
559555
spin_lock_init(&idxd->cmd_lock);
560556

561557
return idxd;
558+
559+
err_name:
560+
put_device(conf_dev);
561+
bitmap_free(idxd->opcap_bmap);
562+
err_opcap:
563+
ida_free(&idxd_ida, idxd->id);
564+
err_ida:
565+
kfree(idxd);
566+
567+
return NULL;
562568
}
563569

564570
static int idxd_enable_system_pasid(struct idxd_device *idxd)

0 commit comments

Comments
 (0)