drm/amdgpu: defer XCP DRM device free until primary release
Subsystems (2)
PATCH 1/1 drm/amdgpu: defer XCP DRM device free until primary release
XCP unplug currently restores partition minors onto the synthetic p_ddev and frees that device while files opened through those minors may still be active. Those files took drm_device references on the primary ddev because the minors were redirected at XCP alloc time. After unplug reverses the redirection, close paths operate on the already-freed p_ddev, causing a use-after-free and p_ddev refcount underflow while leaking the matching primary-device references.
Split teardown into two phases. amdgpu_xcp_dev_unplug() only calls drm_dev_unplug() so new opens fail, and leaves minor->dev pointing at the primary device. amdgpu_xcp_dev_fini() restores the partition device pointers and frees p_ddev from amdgpu_driver_release_kms(), after the primary drm_device's last reference is dropped and every redirected drm_file has completed drm_minor_release().
--- drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 6 +++ drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c | 51 +++++++++++++++++++++---- drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.h | 1 + 3 files changed, 51 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +6 -0
@@ -1638,6 +1638,12 @@ void amdgpu_driver_release_kms(struct drm_device *dev) {struct amdgpu_device *adev =drm_to_adev (dev); +/* +* XCP minors redirect their device reference to this DRM device. +* Reaching its final release guarantees that no open files still +* reference those minors, so the backing XCP devices can now be freed. +*/ +amdgpu_xcp_dev_fini (adev);amdgpu_device_fini_sw (adev);pci_set_drvdata (adev->pdev,NULL ); }
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +44 -7
63 lines not on this page · raw message
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.h +1 -0
@@ -181,6 +181,7 @@ int amdgpu_xcp_get_inst_details(struct amdgpu_xcp *xcp,int amdgpu_xcp_dev_register (struct amdgpu_device *adev,const struct pci_device_id *ent);void amdgpu_xcp_dev_unplug (struct amdgpu_device *adev); +void amdgpu_xcp_dev_fini (struct amdgpu_device *adev);int amdgpu_xcp_open_device (struct amdgpu_device *adev,struct amdgpu_fpriv *fpriv,struct drm_file *file_priv);
Signature · 1 line
2.53.0
Public
Quoted 17 lines
From: Irshad, Luqmaan <[email protected]>
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +4
-0+/* +* XCP minors redirect their device reference to this DRM device. +* Reaching its final release guarantees that no open files still +* reference those minors, so the backing XCP devices can now be freed.
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +6
-0+*/ +amdgpu_xcp_dev_fini (adev);amdgpu_device_fini_sw (adev);pci_set_drvdata (adev->pdev,NULL ); } +/* +* Stop new opens, but keep the partition DRM device and its +* minors alive. Existing drm_file objects still point at these +* minors, whose ->dev was redirected to the primary DRM device
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c void amdgpu_xcp_dev_unplug(struct amdgpu_device *adev) · line 432
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +5
-0+* when the XCP device was allocated. +*/ +drm_dev_unplug (adev->xcp_mgr->xcp[i].ddev); + } +}
When looking at the final code, these comments seem to come out of nowhere. These would be a better fit in the commit message.
Quoted 1 line
+ * Normally this was done by
amdgpu_xcp_dev_unplug().
What does this mean?
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +3
-0+* The primary DRM device is in its final release callback, so +* every file that acquired a reference through these redirected +* minors has completed drm_minor_release(). It is now safe to
+ * restore the partition device and free it.
Maybe something like: Since all the XCP minor files are released at this point, It is now safe to restore the partition device and free it.
PATCH v2 1/1 drm/amdgpu: defer XCP DRM device free until primary release
In reply to Luqmaan IrshadXCP unplug currently restores the partition minors' device pointers to the synthetic p_ddev and frees that device while files opened through those minors may still be active. Those files acquired drm_device references on the primary ddev because the minors were redirected at XCP allocation time. After unplug reverses the redirection, close paths operate on the already-freed p_ddev, causing a use-after-free and p_ddev refcount underflow while leaking the matching primary-device references.
Split teardown into two phases. amdgpu_xcp_dev_unplug() only calls drm_dev_unplug() so new opens fail, leaving minor->dev pointing at the primary device. amdgpu_xcp_dev_fini() restores the partition device pointers and frees p_ddev from amdgpu_driver_release_kms(), when the primary drm_device reaches final release and all references held by redirected files have been dropped.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 1 + drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c | 40 ++++++++++++++++++++----- drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.h | 1 + 3 files changed, 35 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +1 -0
@@ -1638,6 +1638,7 @@ void amdgpu_driver_release_kms(struct drm_device *dev) {struct amdgpu_device *adev =drm_to_adev (dev); +amdgpu_xcp_dev_fini (adev);amdgpu_device_fini_sw (adev);pci_set_drvdata (adev->pdev,NULL ); }
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +33 -7
@@ -412,7 +412,6 @@ int amdgpu_xcp_dev_register(struct amdgpu_device *adev,void amdgpu_xcp_dev_unplug (struct amdgpu_device *adev) { -struct drm_device *p_ddev;int i;if (!adev->xcp_mgr)
@@ -422,12 +421,39 @@ void amdgpu_xcp_dev_unplug(struct amdgpu_device *adev)if (!adev->xcp_mgr->xcp[i].ddev)break ; - p_ddev = adev->xcp_mgr->xcp[i].ddev; -drm_dev_unplug (p_ddev); - p_ddev->render->dev = adev->xcp_mgr->xcp[i].rdev; - p_ddev->primary->dev = adev->xcp_mgr->xcp[i].pdev; - p_ddev->driver = adev->xcp_mgr->xcp[i].driver; - p_ddev->vma_offset_manager = adev->xcp_mgr->xcp[i].vma_offset_manager; +drm_dev_unplug (adev->xcp_mgr->xcp[i].ddev); + } +} + +void amdgpu_xcp_dev_fini (struct amdgpu_device *adev) +{ +struct amdgpu_xcp *xcp; +struct drm_device *p_ddev; +int i; + +if (!adev->xcp_mgr) +return ; + +for (i =1 ; i <MAX_XCP ; i++) { + xcp = &adev->xcp_mgr->xcp[i]; +if (!xcp->ddev) +break ; + + p_ddev = xcp->ddev; + +/* Unregister devices on paths that did not unplug them earlier. */ +if (p_ddev->registered) +drm_dev_unplug (p_ddev); + +/* +* All XCP minor files have been released, so it is now safe to +* restore the partition device and free it. +*/ + p_ddev->render->dev = xcp->rdev; + p_ddev->primary->dev = xcp->pdev; + p_ddev->driver = xcp->driver; + p_ddev->vma_offset_manager = xcp->vma_offset_manager; + xcp->ddev =NULL ;amdgpu_xcp_drm_dev_free (p_ddev); } }
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.h +1 -0
@@ -181,6 +181,7 @@ int amdgpu_xcp_get_inst_details(struct amdgpu_xcp *xcp,int amdgpu_xcp_dev_register (struct amdgpu_device *adev,const struct pci_device_id *ent);void amdgpu_xcp_dev_unplug (struct amdgpu_device *adev); +void amdgpu_xcp_dev_fini (struct amdgpu_device *adev);int amdgpu_xcp_open_device (struct amdgpu_device *adev,struct amdgpu_fpriv *fpriv,struct drm_file *file_priv);
Signature · 1 line
2.53.0
Public
Quoted 116 lines
From: Irshad, Luqmaan <[email protected]>
Subject: [PATCH v2] drm/amdgpu: defer XCP DRM device free until primary releaseXCP unplug currently restores the partition minors' device pointers to the synthetic
p_ddevand frees that device while files opened through those minors may still be active. Those files acquireddrm_devicereferences on the primary ddev because the minors were redirected at XCP allocation time. After unplug reverses the redirection, close paths operate on the already-freedp_ddev, causing a use-after-free andp_ddevrefcount underflow while leaking the matching primary-device references.Split teardown into two phases.
amdgpu_xcp_dev_unplug()only callsdrm_dev_unplug()so new opens fail, leavingminor->devpointing at the primary device.amdgpu_xcp_dev_fini()restores the partition device pointers and freesp_ddevfromamdgpu_driver_release_kms(), when the primarydrm_devicereaches final release and all references held by redirected files have been dropped.Signed-off-by Luqmaan Irshad <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 1 + drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c | 40 ++++++++++++++++++++----- drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.h | 1 + 3 files changed, 35 insertions(+), 7 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +1
-0@@ -1638,6 +1638,7 @@ void amdgpu_driver_release_kms(struct drm_device *dev) {struct amdgpu_device *adev =drm_to_adev (dev); +amdgpu_xcp_dev_fini (adev);amdgpu_device_fini_sw (adev);pci_set_drvdata (adev->pdev,NULL ); }drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +20
-7@@ -412,7 +412,6 @@ int amdgpu_xcp_dev_register(struct amdgpu_device *adev, void amdgpu_xcp_dev_unplug(struct amdgpu_device *adev) { -struct drm_device *p_ddev;int i;if (!adev->xcp_mgr)@@ -422,12 +421,39 @@ void amdgpu_xcp_dev_unplug(struct amdgpu_device *adev)if (!adev->xcp_mgr->xcp[i].ddev)break ; - p_ddev = adev->xcp_mgr->xcp[i].ddev; -drm_dev_unplug (p_ddev); - p_ddev->render->dev = adev->xcp_mgr->xcp[i].rdev; - p_ddev->primary->dev = adev->xcp_mgr->xcp[i].pdev; - p_ddev->driver = adev->xcp_mgr->xcp[i].driver; - p_ddev->vma_offset_manager = adev->xcp_mgr- >xcp[i].vma_offset_manager; +drm_dev_unplug (adev->xcp_mgr->xcp[i].ddev); + } +} + +void amdgpu_xcp_dev_fini (struct amdgpu_device *adev) { +struct amdgpu_xcp *xcp; +struct drm_device *p_ddev; +int i; + +if (!adev->xcp_mgr) +return ; + +for (i =1 ; i <MAX_XCP ; i++) { + xcp = &adev->xcp_mgr->xcp[i]; +if (!xcp->ddev) +break ; + + p_ddev = xcp->ddev; + +/* Unregister devices on paths that did not unplug them earlier. */
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +12
-0+if (p_ddev->registered) +drm_dev_unplug (p_ddev); + +/* +* All XCP minor files have been released, so it is now safe to +* restore the partition device and free it. +*/ + p_ddev->render->dev = xcp->rdev; + p_ddev->primary->dev = xcp->pdev; + p_ddev->driver = xcp->driver; + p_ddev->vma_offset_manager = xcp->vma_offset_manager; + xcp->ddev =NULL ;amdgpu_xcp_drm_dev_free (p_ddev); } }drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.h +1
-1@@ -181,6 +181,7 @@ int amdgpu_xcp_get_inst_details(struct amdgpu_xcp *xcp, int amdgpu_xcp_dev_register(struct amdgpu_device *adev,const struct pci_device_id *ent);void amdgpu_xcp_dev_unplug (struct amdgpu_device *adev); +void amdgpu_xcp_dev_fini (struct amdgpu_device *adev);int amdgpu_xcp_open_device (struct amdgpu_device *adev,struct amdgpu_fpriv *fpriv,struct drm_file *file_priv); --2.53.0
Public
Quoted 18 lines
-----Original Message-----
From: Irshad, Luqmaan <[email protected]>
Sent: Thursday, October 8, 2026 3:28 PM
To: [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Li, Yunxiang (Teddy) <[email protected]>; Irshad, Luqmaan <[email protected]>
Subject: [PATCH v2] drm/amdgpu: defer XCP DRM device free until primary releaseXCP unplug currently restores the partition minors' device pointers to the synthetic
p_ddevand frees that device while files opened through those minors may still be active. Those files acquireddrm_devicereferences on the primary ddev because the minors were redirected at XCP allocation time. After unplug reverses the redirection, close paths operate on the already-freedp_ddev, causing a use-after-free andp_ddevrefcount underflow while leaking the matching primary-device references.
Quoted 8 lines
Split teardown into two phases.
amdgpu_xcp_dev_unplug()only callsdrm_dev_unplug()so new opens fail, leavingminor->devpointing at the primary device.amdgpu_xcp_dev_fini()restores the partition device pointers and freesp_ddevfromamdgpu_driver_release_kms(), when the primarydrm_devicereaches final release and all references held by redirected files have been dropped.Signed-off-by Luqmaan Irshad <[email protected]>
Quoted 96 lines
--- drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 1 + drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c | 40 ++++++++++++++++++++----- drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.h | 1 + 3 files changed, 35 insertions(+), 7 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +1
-0@@ -1638,6 +1638,7 @@ void amdgpu_driver_release_kms(struct drm_device *dev) {struct amdgpu_device *adev =drm_to_adev (dev); +amdgpu_xcp_dev_fini (adev);amdgpu_device_fini_sw (adev);pci_set_drvdata (adev->pdev,NULL ); }drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +20
-7@@ -412,7 +412,6 @@ int amdgpu_xcp_dev_register(struct amdgpu_device *adev, void amdgpu_xcp_dev_unplug(struct amdgpu_device *adev) { -struct drm_device *p_ddev;int i;if (!adev->xcp_mgr)@@ -422,12 +421,39 @@ void amdgpu_xcp_dev_unplug(struct amdgpu_device *adev)if (!adev->xcp_mgr->xcp[i].ddev)break ; - p_ddev = adev->xcp_mgr->xcp[i].ddev; -drm_dev_unplug (p_ddev); - p_ddev->render->dev = adev->xcp_mgr->xcp[i].rdev; - p_ddev->primary->dev = adev->xcp_mgr->xcp[i].pdev; - p_ddev->driver = adev->xcp_mgr->xcp[i].driver; - p_ddev->vma_offset_manager = adev->xcp_mgr- >xcp[i].vma_offset_manager; +drm_dev_unplug (adev->xcp_mgr->xcp[i].ddev); + } +} + +void amdgpu_xcp_dev_fini (struct amdgpu_device *adev) { +struct amdgpu_xcp *xcp; +struct drm_device *p_ddev; +int i; + +if (!adev->xcp_mgr) +return ; + +for (i =1 ; i <MAX_XCP ; i++) { + xcp = &adev->xcp_mgr->xcp[i]; +if (!xcp->ddev) +break ; + + p_ddev = xcp->ddev; + +/* Unregister devices on paths that did not unplug them earlier. */
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +12
-0+if (p_ddev->registered) +drm_dev_unplug (p_ddev); + +/* +* All XCP minor files have been released, so it is now safe to +* restore the partition device and free it. +*/ + p_ddev->render->dev = xcp->rdev; + p_ddev->primary->dev = xcp->pdev; + p_ddev->driver = xcp->driver; + p_ddev->vma_offset_manager = xcp->vma_offset_manager; + xcp->ddev =NULL ;amdgpu_xcp_drm_dev_free (p_ddev); } }drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.h +1
-1@@ -181,6 +181,7 @@ int amdgpu_xcp_get_inst_details(struct amdgpu_xcp *xcp, int amdgpu_xcp_dev_register(struct amdgpu_device *adev,const struct pci_device_id *ent);void amdgpu_xcp_dev_unplug (struct amdgpu_device *adev); +void amdgpu_xcp_dev_fini (struct amdgpu_device *adev);int amdgpu_xcp_open_device (struct amdgpu_device *adev,struct amdgpu_fpriv *fpriv,struct drm_file *file_priv); --2.53.0