Skip to contentSkip to conversation

Release xcp drm memory after unplug xcp device

PATCH Superseded Meng Li 3 participants 4 messages Jun 24, 2025 – Jun 27, 2025

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Meng Li

author
#0 lore raw

v3 cover Release xcp drm memory after unplug xcp device

Fix xcp drm memory release after executing PCIe remove. Fix xcp drm memory alloc after executing PCIe insert.

Changes from V2->V3:
- amdgpu:
- - Add Gerry as a Signed-off.
- - Unified commit author.

Changes from V1->V2:
- cover-letter:
- - Revise title version number.
- amdgpu:
- - Remove // comments.
- - Add Gerry as a co-developr.

Meng Li (1):
drm/amd/amdgpu: Release xcp drm memory after unplug

 drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c     |  1 +
 drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c | 60 +++++++++++++++++----
 drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.h |  1 +
 3 files changed, 53 insertions(+), 9 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v3 1/1 drm/amd/amdgpu: Release xcp drm memory after unplug

Add a new API amdgpu_xcp_drm_dev_free().
After unplug xcp device, need to release xcp drm memory etc.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c     |  1 +
 drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c | 60 +++++++++++++++++----
 drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.h |  1 +
 3 files changed, 53 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +1 -0
index 322816805bfb..70c44961af95 100644
@@ -394,6 +394,7 @@ void amdgpu_xcp_dev_unplug(struct amdgpu_device *adev)
 		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;
+		amdgpu_xcp_drm_dev_free(p_ddev);
 	}
 }
 
drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c +51 -9

93 lines not on this page · raw message

drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.h +1 -0
index c1c4b679bf95..580a1602c8e3 100644
@@ -25,5 +25,6 @@
 #define _AMDGPU_XCP_DRV_H_
 
 int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev);
+void amdgpu_xcp_drm_dev_free(struct drm_device *ddev);
 void amdgpu_xcp_drv_release(void);
 #endif /* _AMDGPU_XCP_DRV_H_ */
Signature · 1 line
2.43.0
maintainer Acked-by on v3 1/1
#2 lore raw

RE: [PATCH V3 1/1] drm/amd/amdgpu: Release xcp drm memory after unplug

[Public]

Quoted 97 lines

-----Original Message-----
From: Meng, Li (Jassmine) <[email protected]>
Sent: Monday, June 23, 2025 9:53 PM
To: amd-gfx <[email protected]>
Cc: Yuan, Perry <[email protected]>; Huang, Shimmer <[email protected]>; Koenig Christian <[email protected]>; Lazar, Lijo <[email protected]>; Deucher, Alexander <[email protected]>; Meng, Li (Jassmine) <[email protected]>; Jiang Liu <[email protected]>
Subject: [PATCH V3 1/1] drm/amd/amdgpu: Release xcp drm memory after unplug

Add a new API amdgpu_xcp_drm_dev_free().
After unplug xcp device, need to release xcp drm memory etc.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c     |  1 +
 drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c | 60 +++++++++++++++++----
drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.h |  1 +
 3 files changed, 53 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +1 -0
index 322816805bfb..70c44961af95 100644
@@ -394,6 +394,7 @@ void amdgpu_xcp_dev_unplug(struct amdgpu_device
 *adev)
              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;
+             amdgpu_xcp_drm_dev_free(p_ddev);
      }

}

drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c +18 -7
index faed84172dd4..3a8f3dd19a12 100644
@@ -45,18 +45,29 @@ static const struct drm_driver amdgpu_xcp_driver = {
 
 static int8_t pdev_num;
 static struct xcp_device *xcp_dev[MAX_XCP_PLATFORM_DEVICE];
+static DEFINE_MUTEX(xcp_mutex);
 
 int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev)  {
      struct platform_device *pdev;
      struct xcp_device *pxcp_dev;
      char dev_name[20];
-     int ret;
+     int ret, i;
+
+     guard(mutex)(&xcp_mutex);
 
      if (pdev_num >= MAX_XCP_PLATFORM_DEVICE)
              return -ENODEV;
 
-     snprintf(dev_name, sizeof(dev_name), "amdgpu_xcp_%d", pdev_num);
+     for (i = 0; i < MAX_XCP_PLATFORM_DEVICE; i++) {
+             if (!xcp_dev[i])
+                     break;
+     }
+
+     if (i >= MAX_XCP_PLATFORM_DEVICE)
+             return -ENODEV;
+
+     snprintf(dev_name, sizeof(dev_name), "amdgpu_xcp_%d", i);
      pdev = platform_device_register_simple(dev_name, -1, NULL, 0);
      if (IS_ERR(pdev))
              return PTR_ERR(pdev);
@@ -72,8 +83,8 @@ int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev)
              goto out_devres;
      }
 
-     xcp_dev[pdev_num] = pxcp_dev;
-     xcp_dev[pdev_num]->pdev = pdev;
+     xcp_dev[i] = pxcp_dev;
+     xcp_dev[i]->pdev = pdev;
      *ddev = &pxcp_dev->drm;
      pdev_num++;
 
@@ -88,16 +99,47 @@ int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev)
 }  EXPORT_SYMBOL(amdgpu_xcp_drm_dev_alloc);
 
-void amdgpu_xcp_drv_release(void)
+static void free_xcp_dev(int8_t index)
 {
-     for (--pdev_num; pdev_num >= 0; --pdev_num) {
-             struct platform_device *pdev = xcp_dev[pdev_num]->pdev;
+     if ((index < MAX_XCP_PLATFORM_DEVICE) && (xcp_dev[index])) {
+             struct platform_device *pdev = xcp_dev[index]->pdev;
 
              devres_release_group(&pdev->dev, NULL);

drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev) · line 115

Quoted 10 lines
drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c +8 -1
              platform_device_unregister(pdev);
-             xcp_dev[pdev_num] = NULL;
+
+             xcp_dev[index] = NULL;
+             pdev_num--;
+     }
+}
+
+void amdgpu_xcp_drm_dev_free(struct drm_device *ddev) {
+     int8_t i = MAX_XCP_PLATFORM_DEVICE;

No need to init i here. You aren't using the assigned value. With that fixed:

Quoted 43 lines
drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c +24 -1
+
+     guard(mutex)(&xcp_mutex);
+
+     for (i = 0; i < MAX_XCP_PLATFORM_DEVICE; i++) {
+             if ((xcp_dev[i]) && (&xcp_dev[i]->drm == ddev)) {
+                     free_xcp_dev(i);
+                     break;
+             }
+     }
+}
+EXPORT_SYMBOL(amdgpu_xcp_drm_dev_free);
+
+void amdgpu_xcp_drv_release(void)
+{
+     int8_t i = 0;
+
+     guard(mutex)(&xcp_mutex);
+
+     if (pdev_num > 0) {
+             for (i = 0; i < MAX_XCP_PLATFORM_DEVICE; i++) {
+                     free_xcp_dev(i);
+                     if (pdev_num == 0)
+                             break;
+             }
      }
-     pdev_num = 0;
 }
 EXPORT_SYMBOL(amdgpu_xcp_drv_release);
 
drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.h +1 -1
index c1c4b679bf95..580a1602c8e3 100644
@@ -25,5 +25,6 @@
 #define _AMDGPU_XCP_DRV_H_
 
 int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev);
+void amdgpu_xcp_drm_dev_free(struct drm_device *ddev);
 void amdgpu_xcp_drv_release(void);
 #endif /* _AMDGPU_XCP_DRV_H_ */
--

2.43.0

Reviewed-by on v3 1/1
#3 lore raw

Re: [PATCH V3 1/1] drm/amd/amdgpu: Release xcp drm memory after unplug

In reply to Meng Li
Quoted 101 lines · Meng Li

Add a new API amdgpu_xcp_drm_dev_free().
After unplug xcp device, need to release xcp drm memory etc.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c     |  1 +
 drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c | 60 +++++++++++++++++----
 drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.h |  1 +
 3 files changed, 53 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +1 -0
index 322816805bfb..70c44961af95 100644
@@ -394,6 +394,7 @@ void amdgpu_xcp_dev_unplug(struct amdgpu_device *adev)
 		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;
+		amdgpu_xcp_drm_dev_free(p_ddev);
 	}
 }
 
drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c +33 -8
index faed84172dd4..3a8f3dd19a12 100644
@@ -45,18 +45,29 @@ static const struct drm_driver amdgpu_xcp_driver = {
 
 static int8_t pdev_num;
 static struct xcp_device *xcp_dev[MAX_XCP_PLATFORM_DEVICE];
+static DEFINE_MUTEX(xcp_mutex);
 
 int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev)
 {
 	struct platform_device *pdev;
 	struct xcp_device *pxcp_dev;
 	char dev_name[20];
-	int ret;
+	int ret, i;
+
+	guard(mutex)(&xcp_mutex);
 
 	if (pdev_num >= MAX_XCP_PLATFORM_DEVICE)
 		return -ENODEV;
 
-	snprintf(dev_name, sizeof(dev_name), "amdgpu_xcp_%d", pdev_num);
+	for (i = 0; i < MAX_XCP_PLATFORM_DEVICE; i++) {
+		if (!xcp_dev[i])
+			break;
+	}
+
+	if (i >= MAX_XCP_PLATFORM_DEVICE)
+		return -ENODEV;
+
+	snprintf(dev_name, sizeof(dev_name), "amdgpu_xcp_%d", i);
 	pdev = platform_device_register_simple(dev_name, -1, NULL, 0);
 	if (IS_ERR(pdev))
 		return PTR_ERR(pdev);
@@ -72,8 +83,8 @@ int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev)
 		goto out_devres;
 	}
 
-	xcp_dev[pdev_num] = pxcp_dev;
-	xcp_dev[pdev_num]->pdev = pdev;
+	xcp_dev[i] = pxcp_dev;
+	xcp_dev[i]->pdev = pdev;
 	*ddev = &pxcp_dev->drm;
 	pdev_num++;
 
@@ -88,16 +99,47 @@ int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev)
 }
 EXPORT_SYMBOL(amdgpu_xcp_drm_dev_alloc);
 
-void amdgpu_xcp_drv_release(void)
+static void free_xcp_dev(int8_t index)
 {
-	for (--pdev_num; pdev_num >= 0; --pdev_num) {
-		struct platform_device *pdev = xcp_dev[pdev_num]->pdev;
+	if ((index < MAX_XCP_PLATFORM_DEVICE) && (xcp_dev[index])) {
+		struct platform_device *pdev = xcp_dev[index]->pdev;
 
 		devres_release_group(&pdev->dev, NULL);
 		platform_device_unregister(pdev);
-		xcp_dev[pdev_num] = NULL;
+
+		xcp_dev[index] = NULL;
+		pdev_num--;
+	}
+}
+
+void amdgpu_xcp_drm_dev_free(struct drm_device *ddev)
+{
+	int8_t i = MAX_XCP_PLATFORM_DEVICE;
+
+	guard(mutex)(&xcp_mutex);
+
+	for (i = 0; i < MAX_XCP_PLATFORM_DEVICE; i++) {
+		if ((xcp_dev[i]) && (&xcp_dev[i]->drm == ddev)) {
+			free_xcp_dev(i);

drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev) · line 133

Quoted 10 lines
drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c +10 -0
+			break;
+		}
+	}
+}
+EXPORT_SYMBOL(amdgpu_xcp_drm_dev_free);
+
+void amdgpu_xcp_drv_release(void)
+{
+	int8_t i = 0;
+

No need to init i.

drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev) · line 137

Quoted 4 lines
drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c +4 -0
+	guard(mutex)(&xcp_mutex);
+
+	if (pdev_num > 0) {
+		for (i = 0; i < MAX_XCP_PLATFORM_DEVICE; i++) {

You could avoid if checks and simplify by

for (i = 0; pdev_num && i < MAX_XCP_PLATFORM_DEVICE; i++)

With those, and comments from Alex addressed -

Thanks,
Lijo

Quoted 20 lines
drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c +4 -1
+			free_xcp_dev(i);
+			if (pdev_num == 0)
+				break;
+		}
 	}
-	pdev_num = 0;
 }
 EXPORT_SYMBOL(amdgpu_xcp_drv_release);
 
drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.h +1 -0
index c1c4b679bf95..580a1602c8e3 100644
@@ -25,5 +25,6 @@
 #define _AMDGPU_XCP_DRV_H_
 
 int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev);
+void amdgpu_xcp_drm_dev_free(struct drm_device *ddev);
 void amdgpu_xcp_drv_release(void);
 #endif /* _AMDGPU_XCP_DRV_H_ */