Register the VRAM manager with the dmem cgroup reclaim infrastructure
so that lowering dmem.max below current VRAM usage triggers TTM
eviction rather than failing with -EBUSY.

v4:
- Rebased on drm-tip; dropped the XE_PL_STOLEN guard as stolen memory
  uses a separate TTM manager and never calls __xe_ttm_vram_mgr_init().

v5:
- Rebased on the introduction of struct dmem_cgroup_init.
- Register the fini drmm action before drmm_cgroup_register_region() so
  that devres LIFO teardown runs unregister_region() first (draining any
  in-flight reclaim callbacks via the rwsem) and xe_ttm_vram_mgr_fini()
  second, ensuring the manager is never accessed by a reclaim callback
  after teardown. (Sashiko-bot)
- Wrap the reclaim callback in xe_ttm_vram_mgr_dmem_reclaim() using
  drm_dev_enter()/drm_dev_exit() to prevent TTM reclaim from running
  after driver unbind.

Assisted-by: GitHub_Copilot:claude-sonnet-4.6
Signed-off-by: Thomas Hellström <[email protected]>
---
 drivers/gpu/drm/xe/xe_ttm_vram_mgr.c | 54 +++++++++++++++++++++++-----
 1 file changed, 45 insertions(+), 9 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c 
b/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c
index 308fda4248eb..b2500344cd57 100644
--- a/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c
+++ b/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c
@@ -276,6 +276,28 @@ static const struct ttm_resource_manager_func 
xe_ttm_vram_mgr_func = {
        .debug  = xe_ttm_vram_mgr_debug
 };
 
+static const struct dmem_cgroup_ops xe_ttm_vram_mgr_dmem_ops;
+
+static int xe_ttm_vram_mgr_dmem_reclaim(struct dmem_cgroup_pool_state *pool,
+                                        u64 target_bytes, void *priv)
+{
+       struct ttm_resource_manager *man = priv;
+       struct xe_device *xe = ttm_to_xe_device(man->bdev);
+       int ret, idx;
+
+       if (!drm_dev_enter(&xe->drm, &idx))
+               return -ENODEV;
+
+       ret = ttm_resource_manager_dmem_reclaim(pool, target_bytes, priv);
+
+       drm_dev_exit(idx);
+       return ret;
+}
+
+static const struct dmem_cgroup_ops xe_ttm_vram_mgr_dmem_ops = {
+       .reclaim = xe_ttm_vram_mgr_dmem_reclaim,
+};
+
 static void xe_ttm_vram_mgr_fini(struct drm_device *dev, void *arg)
 {
        struct xe_device *xe = to_xe_device(dev);
@@ -301,17 +323,10 @@ int __xe_ttm_vram_mgr_init(struct xe_device *xe, struct 
xe_ttm_vram_mgr *mgr,
                           u64 default_page_size)
 {
        struct ttm_resource_manager *man = &mgr->manager;
+       struct dmem_cgroup_region *cg;
        const char *name;
        int err;
 
-       name = mem_type == XE_PL_VRAM0 ? "vram0" : "vram1";
-       man->cg = drmm_cgroup_register_region(&xe->drm, name,
-                                             &(struct dmem_cgroup_init){
-                                               .size = size,
-                                             });
-       if (IS_ERR(man->cg))
-               return PTR_ERR(man->cg);
-
        man->func = &xe_ttm_vram_mgr_func;
        mgr->mem_type = mem_type;
        err = drmm_mutex_init(&xe->drm, &mgr->lock);
@@ -330,7 +345,28 @@ int __xe_ttm_vram_mgr_init(struct xe_device *xe, struct 
xe_ttm_vram_mgr *mgr,
        ttm_set_driver_manager(&xe->ttm, mem_type, &mgr->manager);
        ttm_resource_manager_set_used(&mgr->manager, true);
 
-       return drmm_add_action_or_reset(&xe->drm, xe_ttm_vram_mgr_fini, mgr);
+       /*
+        * Register the fini action before the cgroup region so that devres
+        * LIFO teardown runs unregister_region first (draining any in-flight
+        * reclaim callbacks) and the manager fini second.
+        */
+       err = drmm_add_action_or_reset(&xe->drm, xe_ttm_vram_mgr_fini, mgr);
+       if (err)
+               return err;
+
+       name = mem_type == XE_PL_VRAM0 ? "vram0" : "vram1";
+       cg = drmm_cgroup_register_region(&xe->drm, name,
+                                        &(struct dmem_cgroup_init){
+                                               .size = size,
+                                               .ops = 
&xe_ttm_vram_mgr_dmem_ops,
+                                               .reclaim_priv = man,
+                                        });
+       if (IS_ERR(cg))
+               return PTR_ERR(cg);
+
+       ttm_resource_manager_set_dmem_region(man, cg);
+
+       return 0;
 }
 
 /**
-- 
2.54.0

Reply via email to