drm/xe/bo: Introduce xe_bo_put_async
authorThomas Hellström <thomas.hellstrom@linux.intel.com>
Thu, 6 Mar 2025 01:26:30 +0000 (17:26 -0800)
committerMatthew Brost <matthew.brost@intel.com>
Thu, 6 Mar 2025 19:35:26 +0000 (11:35 -0800)
Introduce xe_bo_put_async to put a bo where the context is such that
the bo destructor can't run due to lockdep problems or atomic context.

If the put is the final put, freeing will be done from a work item.

v5:
 - Kerenl doc for xe_bo_put_async (Thomas)
v7:
 - Fix kernel doc (CI)

Signed-off-by: Matthew Brost <matthew.brost@intel.com>
Signed-off-by: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Reviewed-by: Matthew Brost <matthew.brost@intel.com>
Reviewed-by: Himal Prasad Ghimiray <himal.prasad.ghimiray@intel.com>
Tested-by: Gwan-gyeong Mun <gwan-gyeong.mun@intel.com>
Reviewed-by: Gwan-gyeong Mun <gwan-gyeong.mun@intel.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20250306012657.3505757-6-matthew.brost@intel.com
drivers/gpu/drm/xe/xe_bo.c
drivers/gpu/drm/xe/xe_bo.h
drivers/gpu/drm/xe/xe_device.c
drivers/gpu/drm/xe/xe_device_types.h

index 9adc63175e69b19e2732d7ab27785d04adab03a0..51cd226955924ca0c3777ba32110a330d5442181 100644 (file)
@@ -2660,6 +2660,31 @@ void xe_bo_put_commit(struct llist_head *deferred)
                drm_gem_object_free(&bo->ttm.base.refcount);
 }
 
+static void xe_bo_dev_work_func(struct work_struct *work)
+{
+       struct xe_bo_dev *bo_dev = container_of(work, typeof(*bo_dev), async_free);
+
+       xe_bo_put_commit(&bo_dev->async_list);
+}
+
+/**
+ * xe_bo_dev_init() - Initialize BO dev to manage async BO freeing
+ * @bo_dev: The BO dev structure
+ */
+void xe_bo_dev_init(struct xe_bo_dev *bo_dev)
+{
+       INIT_WORK(&bo_dev->async_free, xe_bo_dev_work_func);
+}
+
+/**
+ * xe_bo_dev_fini() - Finalize BO dev managing async BO freeing
+ * @bo_dev: The BO dev structure
+ */
+void xe_bo_dev_fini(struct xe_bo_dev *bo_dev)
+{
+       flush_work(&bo_dev->async_free);
+}
+
 void xe_bo_put(struct xe_bo *bo)
 {
        struct xe_tile *tile;
index a25340949415ac31416f9846760c771f236f8b34..9cab686dc8722caad51a8e7384d1ec8c01947511 100644 (file)
@@ -323,6 +323,25 @@ xe_bo_put_deferred(struct xe_bo *bo, struct llist_head *deferred)
 
 void xe_bo_put_commit(struct llist_head *deferred);
 
+/**
+ * xe_bo_put_async() - Put BO async
+ * @bo: The bo to put.
+ *
+ * Put BO async, the final put is deferred to a worker to exit an IRQ context.
+ */
+static inline void
+xe_bo_put_async(struct xe_bo *bo)
+{
+       struct xe_bo_dev *bo_device = &xe_bo_device(bo)->bo_device;
+
+       if (xe_bo_put_deferred(bo, &bo_device->async_list))
+               schedule_work(&bo_device->async_free);
+}
+
+void xe_bo_dev_init(struct xe_bo_dev *bo_device);
+
+void xe_bo_dev_fini(struct xe_bo_dev *bo_device);
+
 struct sg_table *xe_bo_sg(struct xe_bo *bo);
 
 /*
index 9454b51f7ad8e6cacb1ab79067d207955fc9c51b..a7dd9c7b95e522bf27ec8c7e20c90a6786b3ef30 100644 (file)
@@ -387,6 +387,8 @@ static void xe_device_destroy(struct drm_device *dev, void *dummy)
 {
        struct xe_device *xe = to_xe_device(dev);
 
+       xe_bo_dev_fini(&xe->bo_device);
+
        if (xe->preempt_fence_wq)
                destroy_workqueue(xe->preempt_fence_wq);
 
@@ -424,6 +426,7 @@ struct xe_device *xe_device_create(struct pci_dev *pdev,
        if (WARN_ON(err))
                goto err;
 
+       xe_bo_dev_init(&xe->bo_device);
        err = drmm_add_action_or_reset(&xe->drm, xe_device_destroy, NULL);
        if (err)
                goto err;
index 833c29fed3a37aff15237615b626cd04e46901fe..2dfe351b26a51417148273b66e8208f38f33e824 100644 (file)
@@ -525,6 +525,14 @@ struct xe_device {
                int mode;
        } wedged;
 
+       /** @bo_device: Struct to control async free of BOs */
+       struct xe_bo_dev {
+               /** @bo_device.async_free: Free worker */
+               struct work_struct async_free;
+               /** @bo_device.async_list: List of BOs to be freed */
+               struct llist_head async_list;
+       } bo_device;
+
        /** @pmu: performance monitoring unit */
        struct xe_pmu pmu;