Skip to contentSkip to conversation

dmaengine: idxd: fix problems on initialization error path.

PATCH In review Steve Wahl 3 participants 5 messages May 20 – May 21

Subsystems (2)
lore
Share & tools
lore

Steve Wahl

author
#0 lore raw

PATCH 1/1 dmaengine: idxd: fix problems on initialization error path.

Some error paths within idxd_pci_probe_alloc and functions it calls did not keep proper track of what has already been allocated or freed, resulting in calling destroy_workqueue with a null pointer, and once that was fixed, attempting to free structures more than once. These conditions were hit running in a kexec'd kdump kernel with reduced resources, causing the "Device is HALTED!" branch in idxd_device_init_reset to be taken.

In idxd_conf_device_release, check that the workqueue has been allocated before trying to destroy it. And in idxd_free and idxd_alloc, do not attempt to free allocations that idxd_conf_device_release, called through put_device, will already have freed.

Fixes 3d33de353b1f ("dmaengine: idxd: Fix not releasing workqueue on .release()")
---
 drivers/dma/idxd/init.c  | 10 ++++++----
 drivers/dma/idxd/sysfs.c |  3 ++-
 2 files changed, 8 insertions(+), 5 deletions(-)
drivers/dma/idxd/init.c +6 -4
index f1cfc7790d95..227e323cc5a0 100644
@@ -607,9 +607,6 @@ static void idxd_free(struct idxd_device *idxd)
 		return;
 
 	put_device(idxd_confdev(idxd));
-	bitmap_free(idxd->opcap_bmap);
-	ida_free(&idxd_ida, idxd->id);
-	kfree(idxd);
 }
 
 static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data)
@@ -649,8 +646,13 @@ static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_d
 	return idxd;
 
 err_name:
+	/*
+	 * once device_initialize(conf_dev) is called,
+	 * put_device(conf_dev) will end up calling
+	 * idxd_conf_device_release() which will free the rest.
+	 */
 	put_device(conf_dev);
-	bitmap_free(idxd->opcap_bmap);
+	return NULL;
 err_opcap:
 	ida_free(&idxd_ida, idxd->id);
 err_ida:
drivers/dma/idxd/sysfs.c +2 -1
index 6d251095c350..d5ffc641c856 100644
@@ -1836,7 +1836,8 @@ static void idxd_conf_device_release(struct device *dev)
 {
 	struct idxd_device *idxd = confdev_to_idxd(dev);
 
-	destroy_workqueue(idxd->wq);
+	if (idxd->wq)
+		destroy_workqueue(idxd->wq);
 	kfree(idxd->groups);
 	bitmap_free(idxd->wq_enable_map);
 	kfree(idxd->wqs);
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Pre-existing issues:
- [High] Incomplete Fix / Double Free & Double Destroy
- [High] Immediate Double Free in child device cleanup functions
- [High] Use-After-Free and Double Free due to dangling pointers in registration rollback --

commit 36d73f2b6ed131031e13332b322a76ce56830ad7
Author: Steve Wahl <[email protected]>
Subject: dmaengine: idxd: fix problems on initialization error path.

This patch fixes double frees and NULL pointer dereferences in the IDXD initialization error paths by ensuring allocations are freed only once during the device release callback, and checking if the workqueue exists before destroying it.

Quoted 4 lines
drivers/dma/idxd/init.c +0 -0
index f1cfc7790d950..227e323cc5a0b 100644

[ ... ]

drivers/dma/idxd/init.c static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_d · line 654

Quoted 10 lines
drivers/dma/idxd/init.c +5 -0
@@ -649,8 +646,13 @@ static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_d
 	return idxd;
 
 err_name:
+	/*
+	 * once device_initialize(conf_dev) is called,
+	 * put_device(conf_dev) will end up calling
+	 * idxd_conf_device_release() which will free the rest.
+	 */
 	put_device(conf_dev);

[Severity: High]
This is a pre-existing issue, but does a similar pattern cause immediate double frees in the child device cleanup functions?

In functions like idxd_clean_wqs(), idxd_clean_groups(), and idxd_clean_engines() (as well as their setup error paths), cleanup is performed by calling put_device() on the child kobjects.

This drops the reference count to 0 and synchronously invokes the sysfs release callback (e.g., idxd_conf_wq_release(), idxd_conf_group_release()), which safely frees the structure (kfree(wq)) and its internal allocations.

However, immediately after put_device() returns, these cleanup functions explicitly call kfree() on the exact same device structure (and inner pointers like wq->wqcfg). Will this result in immediate double frees when unloading the driver or failing initialization?

[Severity: High]
This isn't a bug introduced by this patch, but are there dangling pointers in the registration rollback that cause a use-after-free?

If idxd_register_wq_devices() (or equivalent engine/group registration functions) fails, its error rollback drops references to all child devices via put_device() or device_unregister(), which invokes their release callbacks and frees them.

It does not remove the dangling pointers from the idxd->wqs (or groups/engines) arrays.

The error then bubbles up to idxd_pci_probe_alloc(), which jumps to err_dev_register and calls idxd_cleanup(). This eventually calls idxd_clean_wqs(), which iterates over the full idxd->wqs array and unconditionally calls put_device() and kfree() on the already-freed pointers.

Quoted 4 lines
drivers/dma/idxd/init.c +1 -0
+	return NULL;
 err_opcap:
 	ida_free(&idxd_ida, idxd->id);
 err_ida:

[ ... ]

drivers/dma/idxd/sysfs.c static void idxd_conf_device_release(struct device *dev) · line 1840

Quoted 11 lines
drivers/dma/idxd/sysfs.c +2 -1
index 6d251095c350f..d5ffc641c8567 100644
@@ -1836,7 +1836,8 @@ static void idxd_conf_device_release(struct device *dev)
 {
 	struct idxd_device *idxd = confdev_to_idxd(dev);
 
-	destroy_workqueue(idxd->wq);
+	if (idxd->wq)
+		destroy_workqueue(idxd->wq);

[Severity: High]
This isn't a bug introduced by this patch, but do the error paths from idxd_probe() still cause a double free and double destroy?

If an error occurs in idxd_probe() (e.g., idxd_device_load_config() fails), idxd_cleanup_internals() or idxd_setup_internals() error paths are invoked. These functions manually free arrays (idxd->groups, idxd->wqs, idxd->engines) and destroy the workqueue (destroy_workqueue(idxd->wq)) but leave the pointers non-NULL.

When the error propagates up to idxd_pci_probe_alloc(), it calls idxd_free() which calls put_device(). This triggers idxd_conf_device_release().

Because the pointers are still non-NULL, idxd_conf_device_release() unconditionally calls kfree() on the arrays a second time and executes destroy_workqueue(idxd->wq) again on the already-destroyed workqueue.

Quoted 3 lines
drivers/dma/idxd/sysfs.c +0 -0
 	kfree(idxd->groups);
 	bitmap_free(idxd->wq_enable_map);
 	kfree(idxd->wqs);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
Quoted 43 lines · [email protected] writes:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Pre-existing issues:
- [High] Incomplete Fix / Double Free & Double Destroy
- [High] Immediate Double Free in child device cleanup functions
- [High] Use-After-Free and Double Free due to dangling pointers in registration rollback --

commit 36d73f2b6ed131031e13332b322a76ce56830ad7
Author: Steve Wahl <[email protected]>
Subject: dmaengine: idxd: fix problems on initialization error path.

This patch fixes double frees and NULL pointer dereferences in the IDXD initialization error paths by ensuring allocations are freed only once during the device release callback, and checking if the workqueue exists before destroying it.

Quoted 4 lines
drivers/dma/idxd/init.c +0 -0
index f1cfc7790d950..227e323cc5a0b 100644

[ ... ]

drivers/dma/idxd/init.c static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_d · line 654

Quoted 10 lines
drivers/dma/idxd/init.c +5 -0
@@ -649,8 +646,13 @@ static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_d
 	return idxd;
 
 err_name:
+	/*
+	 * once device_initialize(conf_dev) is called,
+	 * put_device(conf_dev) will end up calling
+	 * idxd_conf_device_release() which will free the rest.
+	 */
 	put_device(conf_dev);

[Severity: High]
This is a pre-existing issue, but does a similar pattern cause immediate double frees in the child device cleanup functions?

In functions like idxd_clean_wqs(), idxd_clean_groups(), and idxd_clean_engines() (as well as their setup error paths), cleanup is performed by calling put_device() on the child kobjects.

Quoted 9 lines

This drops the reference count to 0 and synchronously invokes the sysfs release callback (e.g., idxd_conf_wq_release(), idxd_conf_group_release()), which safely frees the structure (kfree(wq)) and its internal allocations.

However, immediately after put_device() returns, these cleanup functions explicitly call kfree() on the exact same device structure (and inner pointers like wq->wqcfg). Will this result in immediate double frees when unloading the driver or failing initialization?

I believe this patch already solves this issue:

https://lore.kernel.org/dmaengine/[email protected]/

Quoted 10 lines

[Severity: High]
This isn't a bug introduced by this patch, but are there dangling pointers in the registration rollback that cause a use-after-free?

If idxd_register_wq_devices() (or equivalent engine/group registration functions) fails, its error rollback drops references to all child devices via put_device() or device_unregister(), which invokes their release callbacks and frees them.

Quoted 8 lines

It does not remove the dangling pointers from the idxd->wqs (or groups/engines) arrays.

The error then bubbles up to idxd_pci_probe_alloc(), which jumps to err_dev_register and calls idxd_cleanup(). This eventually calls idxd_clean_wqs(), which iterates over the full idxd->wqs array and unconditionally calls put_device() and kfree() on the already-freed pointers.

That patch from Yuho Choi may also fix this.

Quoted 42 lines
Quoted 4 lines
drivers/dma/idxd/init.c +1 -0
+	return NULL;
 err_opcap:
 	ida_free(&idxd_ida, idxd->id);
 err_ida:

[ ... ]

drivers/dma/idxd/sysfs.c static void idxd_conf_device_release(struct device *dev) · line 1840

Quoted 11 lines
drivers/dma/idxd/sysfs.c +2 -1
index 6d251095c350f..d5ffc641c8567 100644
@@ -1836,7 +1836,8 @@ static void idxd_conf_device_release(struct device *dev)
 {
 	struct idxd_device *idxd = confdev_to_idxd(dev);
 
-	destroy_workqueue(idxd->wq);
+	if (idxd->wq)
+		destroy_workqueue(idxd->wq);

[Severity: High]
This isn't a bug introduced by this patch, but do the error paths from idxd_probe() still cause a double free and double destroy?

If an error occurs in idxd_probe() (e.g., idxd_device_load_config() fails), idxd_cleanup_internals() or idxd_setup_internals() error paths are invoked. These functions manually free arrays (idxd->groups, idxd->wqs, idxd->engines) and destroy the workqueue (destroy_workqueue(idxd->wq)) but leave the pointers non-NULL.

When the error propagates up to idxd_pci_probe_alloc(), it calls idxd_free() which calls put_device(). This triggers idxd_conf_device_release().

Because the pointers are still non-NULL, idxd_conf_device_release() unconditionally calls kfree() on the arrays a second time and executes destroy_workqueue(idxd->wq) again on the already-destroyed workqueue.

Quoted 3 lines
drivers/dma/idxd/sysfs.c +0 -0
 	kfree(idxd->groups);
 	bitmap_free(idxd->wq_enable_map);
 	kfree(idxd->wqs);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Signature · 1 line
Vinicius
#3 lore raw
In reply to Steve Wahl

Hi Steve,

Quoted 41 lines · Steve Wahl

Some error paths within idxd_pci_probe_alloc and functions it calls did not keep proper track of what has already been allocated or freed, resulting in calling destroy_workqueue with a null pointer, and once that was fixed, attempting to free structures more than once. These conditions were hit running in a kexec'd kdump kernel with reduced resources, causing the "Device is HALTED!" branch in idxd_device_init_reset to be taken.

In idxd_conf_device_release, check that the workqueue has been allocated before trying to destroy it. And in idxd_free and idxd_alloc, do not attempt to free allocations that idxd_conf_device_release, called through put_device, will already have freed.

Fixes 3d33de353b1f ("dmaengine: idxd: Fix not releasing workqueue on .release()")
---
 drivers/dma/idxd/init.c  | 10 ++++++----
 drivers/dma/idxd/sysfs.c |  3 ++-
 2 files changed, 8 insertions(+), 5 deletions(-)
drivers/dma/idxd/init.c +1 -3
index f1cfc7790d95..227e323cc5a0 100644
@@ -607,9 +607,6 @@ static void idxd_free(struct idxd_device *idxd)
 		return;
 
 	put_device(idxd_confdev(idxd));
-	bitmap_free(idxd->opcap_bmap);
-	ida_free(&idxd_ida, idxd->id);
-	kfree(idxd);
 }
 
 static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data)
@@ -649,8 +646,13 @@ static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_d
 	return idxd;
 
 err_name:
+	/*

drivers/dma/idxd/init.c static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_d · line 658

Quoted 10 lines
drivers/dma/idxd/init.c +5 -1
+	 * once device_initialize(conf_dev) is called,
+	 * put_device(conf_dev) will end up calling
+	 * idxd_conf_device_release() which will free the rest.
+	 */
 	put_device(conf_dev);
-	bitmap_free(idxd->opcap_bmap);
+	return NULL;
 err_opcap:
 	ida_free(&idxd_ida, idxd->id);
 err_ida:

I think that this first part should be a separate patch.

Quoted 5 lines
drivers/dma/idxd/sysfs.c +0 -0
index 6d251095c350..d5ffc641c856 100644

drivers/dma/idxd/sysfs.c static void idxd_conf_device_release(struct device *dev) · line 1843

Quoted 9 lines
drivers/dma/idxd/sysfs.c +2 -1
@@ -1836,7 +1836,8 @@ static void idxd_conf_device_release(struct device *dev)
 {
 	struct idxd_device *idxd = confdev_to_idxd(dev);
 
-	destroy_workqueue(idxd->wq);
+	if (idxd->wq)
+		destroy_workqueue(idxd->wq);
 	kfree(idxd->groups);
 	bitmap_free(idxd->wq_enable_map);
 	kfree(idxd->wqs);

And this another.

Cheers,

Signature · 1 line
Vinicius
author
#4 lore raw
Quoted 66 lines · Vinicius Costa Gomes

Hi Steve,

Quoted 41 lines · Steve Wahl

Some error paths within idxd_pci_probe_alloc and functions it calls did not keep proper track of what has already been allocated or freed, resulting in calling destroy_workqueue with a null pointer, and once that was fixed, attempting to free structures more than once. These conditions were hit running in a kexec'd kdump kernel with reduced resources, causing the "Device is HALTED!" branch in idxd_device_init_reset to be taken.

In idxd_conf_device_release, check that the workqueue has been allocated before trying to destroy it. And in idxd_free and idxd_alloc, do not attempt to free allocations that idxd_conf_device_release, called through put_device, will already have freed.

Fixes 3d33de353b1f ("dmaengine: idxd: Fix not releasing workqueue on .release()")
---
 drivers/dma/idxd/init.c  | 10 ++++++----
 drivers/dma/idxd/sysfs.c |  3 ++-
 2 files changed, 8 insertions(+), 5 deletions(-)
drivers/dma/idxd/init.c +1 -3
index f1cfc7790d95..227e323cc5a0 100644
@@ -607,9 +607,6 @@ static void idxd_free(struct idxd_device *idxd)
 		return;
 
 	put_device(idxd_confdev(idxd));
-	bitmap_free(idxd->opcap_bmap);
-	ida_free(&idxd_ida, idxd->id);
-	kfree(idxd);
 }
 
 static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data)
@@ -649,8 +646,13 @@ static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_d
 	return idxd;
 
 err_name:
+	/*

drivers/dma/idxd/init.c static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_d · line 658

Quoted 10 lines
drivers/dma/idxd/init.c +5 -1
+	 * once device_initialize(conf_dev) is called,
+	 * put_device(conf_dev) will end up calling
+	 * idxd_conf_device_release() which will free the rest.
+	 */
 	put_device(conf_dev);
-	bitmap_free(idxd->opcap_bmap);
+	return NULL;
 err_opcap:
 	ida_free(&idxd_ida, idxd->id);
 err_ida:

I think that this first part should be a separate patch.

Quoted 8 lines
drivers/dma/idxd/sysfs.c +0 -0
index 6d251095c350..d5ffc641c856 100644
@@ -1836,7 +1836,8 @@ static void idxd_conf_device_release(struct device *dev)
 {
 	struct idxd_device *idxd = confdev_to_idxd(dev);
 
Quoted 8 lines

drivers/dma/idxd/sysfs.c static void idxd_conf_device_release(struct device *dev) · line 1843

Quoted 6 lines
drivers/dma/idxd/sysfs.c +2 -1
-	destroy_workqueue(idxd->wq);
+	if (idxd->wq)
+		destroy_workqueue(idxd->wq);
 	kfree(idxd->groups);
 	bitmap_free(idxd->wq_enable_map);
 	kfree(idxd->wqs);

And this another.

I can split it as you desire.

--> Steve

Signature · 1 line
Steve Wahl, Hewlett Packard Enterprise