Skip to contentSkip to conversation

drm/amdgpu: defer XCP DRM device free until primary release

PATCH Superseded Luqmaan Irshad 3 participants 5 messages Sep 22 – Oct 13

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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
index f2f179421c4d..403ca8fb1bc9 100644
@@ -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
index 33157409eda0..e2fd32af9932 100644
@@ -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
#1 lore raw

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.

#2 lore raw

PATCH v2 1/1 drm/amdgpu: defer XCP DRM device free until primary release

In reply to Luqmaan Irshad

XCP 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
index f2f179421c4d..f4bc9e6eb97b 100644
@@ -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
index a8c79a66df1d..c780d6794b89 100644
@@ -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
index 33157409eda0..e2fd32af9932 100644
@@ -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
Reviewed-by on v2 1/1
#3 lore raw

Public

Quoted 116 lines

From: Irshad, Luqmaan <[email protected]>
Subject: [PATCH v2] drm/amdgpu: defer XCP DRM device free until primary release

XCP 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
index f2f179421c4d..f4bc9e6eb97b 100644
@@ -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
index a8c79a66df1d..c780d6794b89 100644
@@ -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
index 33157409eda0..e2fd32af9932 100644
@@ -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

maintainer Reviewed-by on v2 1/1
#4 lore raw
In reply to Luqmaan Irshad

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 release

XCP 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.

Quoted 8 lines

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.

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
index f2f179421c4d..f4bc9e6eb97b 100644
@@ -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
index a8c79a66df1d..c780d6794b89 100644
@@ -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
index 33157409eda0..e2fd32af9932 100644
@@ -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