accel/amdxdna: Fix amdxdna_client lifetime race during device removal
Lizhi Hou
maintainerPATCH 1/1 accel/amdxdna: Fix amdxdna_client lifetime race during device removal
In amdxdna_remove(), all amdxdna_client structures are freed after calling drm_dev_unplug(). However, drm_dev_unplug() does not force existing file descriptors to be closed, so amdxdna_drm_close() may be called after amdxdna_remove() has completed.
As a result, accessing client->pid for debug output in amdxdna_drm_close() can lead to a use-after-free, since the access is not protected by drm_dev_enter().
Fix this by decoupling hardware teardown from client cleanup. amdxdna_remove() only performs hardware-related cleanup, while per-client resources are released from amdxdna_drm_close() when the corresponding file is closed.
--- drivers/accel/amdxdna/amdxdna_pci_drv.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-)
drivers/accel/amdxdna/amdxdna_pci_drv.c +2 -14
@@ -174,18 +174,12 @@ static void amdxdna_drm_close(struct drm_device *ddev, struct drm_file *filp) {struct amdxdna_client *client = filp->driver_priv;struct amdxdna_dev *xdna =to_xdna_dev (ddev); -int idx;XDNA_DBG (xdna,"closing pid %d" , client->pid); -if (!drm_dev_enter (&xdna->ddev, &idx)) -return ; -mutex_lock (&xdna->dev_lock);amdxdna_client_cleanup (client);mutex_unlock (&xdna->dev_lock); - -drm_dev_exit (idx); }static int amdxdna_drm_get_info_ioctl (struct drm_device *dev,void *data,struct drm_file *filp)
@@ -443,14 +437,8 @@ static void amdxdna_remove(struct pci_dev *pdev)amdxdna_sysfs_fini (xdna);mutex_lock (&xdna->dev_lock); - client =list_first_entry_or_null (&xdna->client_list, - struct amdxdna_client, node); -while (client) { -amdxdna_client_cleanup (client); - - client =list_first_entry_or_null (&xdna->client_list, - struct amdxdna_client, node); - } +list_for_each_entry (client, &xdna->client_list, node) +amdxdna_hwctx_remove_all (client); xdna->dev_info->ops->fini (xdna);mutex_unlock (&xdna->dev_lock);
Signature · 1 line
2.34.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
New issues:
- [High] Dropping dev_lock during amdxdna_remove() list iteration causes a Use-After-Free if a file is closed concurrently.
- [High] Deferring IOMMU SVA unbind to amdxdna_drm_close() causes a NULL pointer dereference in the IOMMU core.
Pre-existing issues:
- [High] Deferred GEM object freeing accesses destroyed IOMMU domain structures, causing a Use-After-Free.
- [High] Deferred GEM object freeing calls flush_workqueue() on a destroyed workqueue pointer. --
commit ec7bb08ec6d7a0837cd580d24a3f76e7ed06a6c6
Author: Lizhi Hou <[email protected]>
accel/amdxdna: Fix amdxdna_client lifetime race during device removal
This patch changes the client cleanup logic during device removal. It decouples hardware teardown from client cleanup to avoid a use-after-free if amdxdna_drm_close() is called concurrently with amdxdna_remove().
Quoted 7 lines
drivers/accel/amdxdna/amdxdna_pci_drv.c +0
-0@@ -167,18 +167,12 @@ static void amdxdna_drm_close(struct drm_device *ddev, struct drm_file *filp) {struct amdxdna_client *client = filp->driver_priv;
drivers/accel/amdxdna/amdxdna_pci_drv.c static void amdxdna_drm_close(struct drm_device *ddev, struct drm_file *filp) · line 174
Quoted 10 lines
drivers/accel/amdxdna/amdxdna_pci_drv.c +0
-4struct amdxdna_dev *xdna =to_xdna_dev (ddev); -int idx;XDNA_DBG (xdna,"closing pid %d" , client->pid); -if (!drm_dev_enter (&xdna->ddev, &idx)) -return ; -mutex_lock (&xdna->dev_lock);amdxdna_client_cleanup (client);
[Severity: High]
Since amdxdna_client_cleanup() is now deferred until file close, could this trigger a NULL pointer dereference during SVA teardown?
When a device is removed, the PCI subsystem unlinks it from its IOMMU group. Later, when this file is closed, amdxdna_sva_fini() calls iommu_sva_unbind_device().
Inside the IOMMU core, iommu_detach_device_pasid() dereferences the group pointer:
struct iommu_group *group = dev->iommu_group; mutex_lock(&group->mutex);
If the device was already removed, dev->iommu_group would be NULL, causing a crash. Does the SVA unbind need to happen earlier during device removal?
Quoted 9 lines
drivers/accel/amdxdna/amdxdna_pci_drv.c +0
-2mutex_unlock (&xdna->dev_lock); - -drm_dev_exit (idx); }@@ -436,14 +430,8 @@ static void amdxdna_remove(struct pci_dev *pdev)amdxdna_sysfs_fini (xdna);
drivers/accel/amdxdna/amdxdna_pci_drv.c static void amdxdna_remove(struct pci_dev *pdev) · line 433
Quoted 10 lines
drivers/accel/amdxdna/amdxdna_pci_drv.c +1
-8mutex_lock (&xdna->dev_lock); - client =list_first_entry_or_null (&xdna->client_list, - struct amdxdna_client, node); -while (client) { -amdxdna_client_cleanup (client); - - client =list_first_entry_or_null (&xdna->client_list, - struct amdxdna_client, node); - } +list_for_each_entry (client, &xdna->client_list, node)
[Severity: High]
Does this list_for_each_entry() loop have a use-after-free risk if the lock is dropped internally?
The loop calls amdxdna_hwctx_remove_all(), which invokes aie2_hwctx_fini(). Inside aie2_hwctx_fini(), xdna->dev_lock is temporarily dropped to wait for hardware idle:
mutex_unlock(&xdna->dev_lock);
Since this patch removes the drm_dev_enter() safeguard from amdxdna_drm_close(), a concurrent file close can acquire dev_lock while it is dropped here. The concurrent close would remove the client from the list via list_del() and free the client structure.
When the lock is re-acquired and list_for_each_entry() attempts to fetch the next element, won't it dereference the freed client->node.next pointer?
drivers/accel/amdxdna/amdxdna_pci_drv.c static void amdxdna_remove(struct pci_dev *pdev) · line 444
Quoted 4 lines
drivers/accel/amdxdna/amdxdna_pci_drv.c +1
-0+amdxdna_hwctx_remove_all (client); xdna->dev_info->ops->fini (xdna);mutex_unlock (&xdna->dev_lock);
[Severity: High]
This is a pre-existing issue, but does this function leave xdna->domain dangling, leading to a use-after-free on deferred file close?
During device removal, amdxdna_remove() calls amdxdna_iommu_fini(), which frees the domain but leaves the pointer intact:
iommu_domain_free(xdna->domain);
If the application keeps the file descriptor open, drm_gem_object_put() will later invoke amdxdna_gem_obj_free() during cleanup. This calls amdxdna_dma_unmap_bo():
if (amdxdna_iova_on(xdna)) {
size = iova_align(&xdna->iovad, abo->mem.size);
iommu_unmap(xdna->domain, abo->mem.dma_addr, size);Since xdna->domain is not NULL, amdxdna_iova_on() returns true, and iommu_unmap() is called on the freed domain. Should the domain pointer be set to NULL after being freed?
[Severity: High]
This is a pre-existing issue, but could deferred GEM cleanup trigger a use-after-free on the notifier workqueue?
Earlier in amdxdna_remove(), destroy_workqueue(xdna->notifier_wq) frees the workqueue structure.
When a process retains an open file descriptor past device removal, the DRM core defers GEM cleanup to the eventual file close. When drm_gem_object_put() calls amdxdna_gem_obj_free(), it unconditionally does:
flush_workqueue(xdna->notifier_wq);
Since the workqueue was already destroyed and freed, won't this result in a use-after-free crash?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Lizhi Hou
maintainerQuoted 5 lines · On 6/5/26 11:18, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
New issues:
- [High] Droppingdev_lockduringamdxdna_remove()list iteration causes a Use-After-Free if a file is closed concurrently.
- [High] Deferring IOMMU SVA unbind toamdxdna_drm_close()causes a NULL pointer dereference in the IOMMU core.
Agreed. I will provide a simpler fix in v2 patch.
Lizhi
Quoted 129 lines
Pre-existing issues:
- [High] Deferred GEM object freeing accesses destroyed IOMMU domain structures, causing a Use-After-Free.
- [High] Deferred GEM object freeing callsflush_workqueue()on a destroyed workqueue pointer. --commit
ec7bb08ec6d7a0837cd580d24a3f76e7ed06a6c6
Author: Lizhi Hou <[email protected]>accel/amdxdna: Fix
amdxdna_clientlifetime race during device removalThis patch changes the client cleanup logic during device removal. It decouples hardware teardown from client cleanup to avoid a use-after-free if
amdxdna_drm_close()is called concurrently withamdxdna_remove().Quoted 7 lines
drivers/accel/amdxdna/amdxdna_pci_drv.c +0
-0@@ -167,18 +167,12 @@ static void amdxdna_drm_close(struct drm_device *ddev, struct drm_file *filp) {struct amdxdna_client *client = filp->driver_priv;drivers/accel/amdxdna/amdxdna_pci_drv.c static void amdxdna_drm_close(struct drm_device *ddev, struct drm_file *filp) · line 174
Quoted 10 lines
drivers/accel/amdxdna/amdxdna_pci_drv.c +0
-4struct amdxdna_dev *xdna =to_xdna_dev (ddev); -int idx;XDNA_DBG (xdna,"closing pid %d" , client->pid); -if (!drm_dev_enter (&xdna->ddev, &idx)) -return ; -mutex_lock (&xdna->dev_lock);amdxdna_client_cleanup (client);[Severity: High]
Sinceamdxdna_client_cleanup()is now deferred until file close, could this trigger a NULL pointer dereference during SVA teardown?When a device is removed, the PCI subsystem unlinks it from its IOMMU group. Later, when this file is closed,
amdxdna_sva_fini()callsiommu_sva_unbind_device().Inside the IOMMU core,
iommu_detach_device_pasid()dereferences the group pointer:struct iommu_group *group = dev->iommu_group; mutex_lock(&group->mutex);If the device was already removed,
dev->iommu_groupwould be NULL, causing a crash. Does the SVA unbind need to happen earlier during device removal?Quoted 9 lines
drivers/accel/amdxdna/amdxdna_pci_drv.c +0
-2mutex_unlock (&xdna->dev_lock); - -drm_dev_exit (idx); }@@ -436,14 +430,8 @@ static void amdxdna_remove(struct pci_dev *pdev)amdxdna_sysfs_fini (xdna);drivers/accel/amdxdna/amdxdna_pci_drv.c static void amdxdna_remove(struct pci_dev *pdev) · line 433
Quoted 10 lines
drivers/accel/amdxdna/amdxdna_pci_drv.c +1
-8mutex_lock (&xdna->dev_lock); - client =list_first_entry_or_null (&xdna->client_list, - struct amdxdna_client, node); -while (client) { -amdxdna_client_cleanup (client); - - client =list_first_entry_or_null (&xdna->client_list, - struct amdxdna_client, node); - } +list_for_each_entry (client, &xdna->client_list, node)[Severity: High]
Does thislist_for_each_entry()loop have a use-after-free risk if the lock is dropped internally?The loop calls
amdxdna_hwctx_remove_all(), which invokesaie2_hwctx_fini(). Insideaie2_hwctx_fini(),xdna->dev_lockis temporarily dropped to wait for hardware idle:mutex_unlock(&xdna->dev_lock);Since this patch removes the
drm_dev_enter()safeguard fromamdxdna_drm_close(), a concurrent file close can acquiredev_lockwhile it is dropped here. The concurrent close would remove the client from the list vialist_del()and free the client structure.When the lock is re-acquired and
list_for_each_entry()attempts to fetch the next element, won't it dereference the freedclient->node.nextpointer?drivers/accel/amdxdna/amdxdna_pci_drv.c static void amdxdna_remove(struct pci_dev *pdev) · line 444
Quoted 4 lines
drivers/accel/amdxdna/amdxdna_pci_drv.c +1
-0+amdxdna_hwctx_remove_all (client); xdna->dev_info->ops->fini (xdna);mutex_unlock (&xdna->dev_lock);[Severity: High]
This is a pre-existing issue, but does this function leavexdna->domaindangling, leading to a use-after-free on deferred file close?During device removal,
amdxdna_remove()callsamdxdna_iommu_fini(), which frees the domain but leaves the pointer intact:iommu_domain_free(xdna->domain);If the application keeps the file descriptor open,
drm_gem_object_put()will later invokeamdxdna_gem_obj_free()during cleanup. This callsamdxdna_dma_unmap_bo():if (amdxdna_iova_on(xdna)) { size = iova_align(&xdna->iovad, abo->mem.size); iommu_unmap(xdna->domain, abo->mem.dma_addr, size);Since
xdna->domainis not NULL,amdxdna_iova_on()returns true, andiommu_unmap()is called on the freed domain. Should the domain pointer be set to NULL after being freed?[Severity: High]
This is a pre-existing issue, but could deferred GEM cleanup trigger a use-after-free on the notifier workqueue?Earlier in
amdxdna_remove(),destroy_workqueue(xdna->notifier_wq)frees the workqueue structure.When a process retains an open file descriptor past device removal, the DRM core defers GEM cleanup to the eventual file close. When
drm_gem_object_put()callsamdxdna_gem_obj_free(), it unconditionally does:flush_workqueue(xdna->notifier_wq);Since the workqueue was already destroyed and freed, won't this result in a use-after-free crash?