drm/ttm: Balance ttm_resource_cursor_init() and ttm_resource_cursor_fini()
authorThomas Hellström <thomas.hellstrom@linux.intel.com>
Tue, 17 Dec 2024 14:58:44 +0000 (15:58 +0100)
committerChristian König <christian.koenig@amd.com>
Mon, 13 Jan 2025 11:58:10 +0000 (12:58 +0100)
Make the interface more symmetric by providing and using a
ttm_resource_cursor_init().

v10:
- Fix a stray newline (Matthew Brost)
- Update kerneldoc (Matthew Brost)

Signed-off-by: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Reviewed-by: Matthew Brost <matthew.brost@intel.com>
Reviewed-by: Christian König <christian.koenig@amd.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20241217145852.37342-2-thomas.hellstrom@linux.intel.com
Signed-off-by: Christian König <christian.koenig@amd.com>
drivers/gpu/drm/ttm/ttm_bo.c
drivers/gpu/drm/ttm/ttm_bo_util.c
drivers/gpu/drm/ttm/ttm_resource.c
include/drm/ttm/ttm_resource.h

index 48c5365efca1cefbaf83747de9b0a585e5a8a314..06d6a452c4f4536af45d0992e7b6c60eb5d73257 100644 (file)
@@ -450,7 +450,8 @@ int ttm_bo_evict_first(struct ttm_device *bdev, struct ttm_resource_manager *man
        int ret = 0;
 
        spin_lock(&bdev->lru_lock);
-       res = ttm_resource_manager_first(man, &cursor);
+       ttm_resource_cursor_init(&cursor, man);
+       res = ttm_resource_manager_first(&cursor);
        ttm_resource_cursor_fini(&cursor);
        if (!res) {
                ret = -ENOENT;
index d939925efa81c8e90636dfe0531fd86c912009e3..917096bd5f683f537dec3ee7065a339c13d5af31 100644 (file)
@@ -865,7 +865,8 @@ s64 ttm_lru_walk_for_evict(struct ttm_lru_walk *walk, struct ttm_device *bdev,
        s64 lret;
 
        spin_lock(&bdev->lru_lock);
-       ttm_resource_manager_for_each_res(man, &cursor, res) {
+       ttm_resource_cursor_init(&cursor, man);
+       ttm_resource_manager_for_each_res(&cursor, res) {
                struct ttm_buffer_object *bo = res->bo;
                bool bo_needs_unlock = false;
                bool bo_locked = false;
index a87665eb28a62d1425e9e0d64c225c8a77b72f01..e19360cc793033bc3bf07cd9187f19dc10d14c97 100644 (file)
@@ -81,6 +81,23 @@ static void ttm_bulk_move_drop_cursors(struct ttm_lru_bulk_move *bulk)
                ttm_resource_cursor_clear_bulk(cursor);
 }
 
+/**
+ * ttm_resource_cursor_init() - Initialize a struct ttm_resource_cursor
+ * @cursor: The cursor to initialize.
+ * @man: The resource manager.
+ *
+ * Initialize the cursor before using it for iteration.
+ */
+void ttm_resource_cursor_init(struct ttm_resource_cursor *cursor,
+                             struct ttm_resource_manager *man)
+{
+       cursor->priority = 0;
+       cursor->man = man;
+       ttm_lru_item_init(&cursor->hitch, TTM_LRU_HITCH);
+       INIT_LIST_HEAD(&cursor->bulk_link);
+       INIT_LIST_HEAD(&cursor->hitch.link);
+}
+
 /**
  * ttm_resource_cursor_fini() - Finalize the LRU list cursor usage
  * @cursor: The struct ttm_resource_cursor to finalize.
@@ -593,7 +610,6 @@ ttm_resource_cursor_check_bulk(struct ttm_resource_cursor *cursor,
 /**
  * ttm_resource_manager_first() - Start iterating over the resources
  * of a resource manager
- * @man: resource manager to iterate over
  * @cursor: cursor to record the position
  *
  * Initializes the cursor and starts iterating. When done iterating,
@@ -602,17 +618,16 @@ ttm_resource_cursor_check_bulk(struct ttm_resource_cursor *cursor,
  * Return: The first resource from the resource manager.
  */
 struct ttm_resource *
-ttm_resource_manager_first(struct ttm_resource_manager *man,
-                          struct ttm_resource_cursor *cursor)
+ttm_resource_manager_first(struct ttm_resource_cursor *cursor)
 {
-       lockdep_assert_held(&man->bdev->lru_lock);
+       struct ttm_resource_manager *man = cursor->man;
 
-       cursor->priority = 0;
-       cursor->man = man;
-       ttm_lru_item_init(&cursor->hitch, TTM_LRU_HITCH);
-       INIT_LIST_HEAD(&cursor->bulk_link);
-       list_add(&cursor->hitch.link, &man->lru[cursor->priority]);
+       if (WARN_ON_ONCE(!man))
+               return NULL;
+
+       lockdep_assert_held(&man->bdev->lru_lock);
 
+       list_move(&cursor->hitch.link, &man->lru[cursor->priority]);
        return ttm_resource_manager_next(cursor);
 }
 
@@ -648,8 +663,6 @@ ttm_resource_manager_next(struct ttm_resource_cursor *cursor)
                ttm_resource_cursor_clear_bulk(cursor);
        }
 
-       ttm_resource_cursor_fini(cursor);
-
        return NULL;
 }
 
index be034be56ba1b39d171e79ab9ea22388c6045597..e1f3b95d73b6fe7588c0d6034c78358471ef8a47 100644 (file)
@@ -325,6 +325,9 @@ struct ttm_resource_cursor {
        unsigned int priority;
 };
 
+void ttm_resource_cursor_init(struct ttm_resource_cursor *cursor,
+                             struct ttm_resource_manager *man);
+
 void ttm_resource_cursor_fini(struct ttm_resource_cursor *cursor);
 
 /**
@@ -456,8 +459,7 @@ void ttm_resource_manager_debug(struct ttm_resource_manager *man,
                                struct drm_printer *p);
 
 struct ttm_resource *
-ttm_resource_manager_first(struct ttm_resource_manager *man,
-                          struct ttm_resource_cursor *cursor);
+ttm_resource_manager_first(struct ttm_resource_cursor *cursor);
 struct ttm_resource *
 ttm_resource_manager_next(struct ttm_resource_cursor *cursor);
 
@@ -466,14 +468,13 @@ ttm_lru_first_res_or_null(struct list_head *head);
 
 /**
  * ttm_resource_manager_for_each_res - iterate over all resources
- * @man: the resource manager
  * @cursor: struct ttm_resource_cursor for the current position
  * @res: the current resource
  *
  * Iterate over all the evictable resources in a resource manager.
  */
-#define ttm_resource_manager_for_each_res(man, cursor, res)            \
-       for (res = ttm_resource_manager_first(man, cursor); res;        \
+#define ttm_resource_manager_for_each_res(cursor, res) \
+       for (res = ttm_resource_manager_first(cursor); res;     \
             res = ttm_resource_manager_next(cursor))
 
 struct ttm_kmap_iter *