Skip to contentSkip to conversation

drm/ttm: fix ttm_mem_evict_first once more

PATCH In review Christian König 5 participants 8 messages Nov 15, 2017 – Dec 14, 2017 5 messages not in the archive

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

PATCH 1/2 drm/ttm: fix ttm_mem_evict_first once more

The code path isn't hit at the moment, but we need to take the lock to add the BO back to the LRU.

---
 drivers/gpu/drm/ttm/ttm_bo.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +5 -2
index 07d9c6e5b6ca..7c1eac4f4b4b 100644
@@ -793,10 +793,13 @@ static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
 	spin_unlock(&glob->lru_lock);
 
 	ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
-	if (locked)
+	if (locked) {
 		ttm_bo_unreserve(bo);
-	else
+	} else {
+		spin_lock(&glob->lru_lock);
 		ttm_bo_add_to_lru(bo);
+		spin_unlock(&glob->lru_lock);
+	}
 
 	kref_put(&bo->list_kref, ttm_bo_release_list);
 	return ret;
Signature · 6 lines
2.11.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw

PATCH 2/2 drm/ttm: completely rework ttm_bo_delayed_delete

There is no guarantee that the next entry on the ddelete list stays on the list when we drop the locks.

Completely rework this mess by moving processed entries on a temporary list.

---
 drivers/gpu/drm/ttm/ttm_bo.c | 77 ++++++++++++++------------------------------
 1 file changed, 25 insertions(+), 52 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +25 -52
index 7c1eac4f4b4b..ad0afdd71f21 100644
@@ -572,71 +572,47 @@ static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
  * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
  * encountered buffers.
  */
-
-static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
+static bool ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
 {
 	struct ttm_bo_global *glob = bdev->glob;
-	struct ttm_buffer_object *entry = NULL;
-	int ret = 0;
-
-	spin_lock(&glob->lru_lock);
-	if (list_empty(&bdev->ddestroy))
-		goto out_unlock;
+	struct list_head removed;
+	bool empty;
 
-	entry = list_first_entry(&bdev->ddestroy,
-		struct ttm_buffer_object, ddestroy);
-	kref_get(&entry->list_kref);
+	INIT_LIST_HEAD(&removed);
 
-	for (;;) {
-		struct ttm_buffer_object *nentry = NULL;
-
-		if (entry->ddestroy.next != &bdev->ddestroy) {
-			nentry = list_first_entry(&entry->ddestroy,
-				struct ttm_buffer_object, ddestroy);
-			kref_get(&nentry->list_kref);
-		}
-
-		ret = reservation_object_trylock(entry->resv) ? 0 : -EBUSY;
-		if (remove_all && ret) {
-			spin_unlock(&glob->lru_lock);
-			ret = reservation_object_lock(entry->resv, NULL);
-			spin_lock(&glob->lru_lock);
-		}
+	spin_lock(&glob->lru_lock);
+	while (!list_empty(&bdev->ddestroy)) {
+		struct ttm_buffer_object *bo;
 
-		if (!ret)
-			ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
-						  true);
-		else
-			spin_unlock(&glob->lru_lock);
+		bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
+				      ddestroy);
+		kref_get(&bo->list_kref);
+		list_move_tail(&bo->ddestroy, &removed);
+		spin_unlock(&glob->lru_lock);
 
-		kref_put(&entry->list_kref, ttm_bo_release_list);
-		entry = nentry;
+		reservation_object_lock(bo->resv, NULL);
 
-		if (ret || !entry)
-			goto out;
+		spin_lock(&glob->lru_lock);
+		ttm_bo_cleanup_refs(bo, false, !remove_all, true);
 
+		kref_put(&bo->list_kref, ttm_bo_release_list);
 		spin_lock(&glob->lru_lock);
-		if (list_empty(&entry->ddestroy))
-			break;
 	}
-
-out_unlock:
+	list_splice_tail(&removed, &bdev->ddestroy);
+	empty = list_empty(&bdev->ddestroy);
 	spin_unlock(&glob->lru_lock);
-out:
-	if (entry)
-		kref_put(&entry->list_kref, ttm_bo_release_list);
-	return ret;
+
+	return empty;
 }
 
 static void ttm_bo_delayed_workqueue(struct work_struct *work)
 {
 	struct ttm_bo_device *bdev =
 	    container_of(work, struct ttm_bo_device, wq.work);
+	unsigned long delay = ((HZ / 100) < 1) ? 1 : HZ / 100;
 
-	if (ttm_bo_delayed_delete(bdev, false)) {
-		schedule_delayed_work(&bdev->wq,
-				      ((HZ / 100) < 1) ? 1 : HZ / 100);
-	}
+	if (!ttm_bo_delayed_delete(bdev, false))
+		schedule_delayed_work(&bdev->wq, delay);
 }
 
 static void ttm_bo_release(struct kref *kref)
@@ -1573,13 +1549,10 @@ int ttm_bo_device_release(struct ttm_bo_device *bdev)
 
 	cancel_delayed_work_sync(&bdev->wq);
 
-	while (ttm_bo_delayed_delete(bdev, true))
-		;
-
-	spin_lock(&glob->lru_lock);
-	if (list_empty(&bdev->ddestroy))
+	if (ttm_bo_delayed_delete(bdev, true))
 		TTM_DEBUG("Delayed destroy list was clean\n");
 
+	spin_lock(&glob->lru_lock);
 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
 		if (list_empty(&bdev->man[0].lru[0]))
 			TTM_DEBUG("Swap list %d was clean\n", i);
Signature · 6 lines
2.11.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

A message that is not in the archive (sent off-list or not yet received): 20171115123151.3038-2-christian.koenig-5C7GfCeVMHo@public.gmane.org

#3 lore raw

Re: [PATCH 2/2] drm/ttm: completely rework ttm_bo_delayed_delete

Quoted 7 lines · Christian König

There is no guarantee that the next entry on the ddelete list stays on the list when we drop the locks.

Completely rework this mess by moving processed entries on a temporary list.

[...]

drivers/gpu/drm/ttm/ttm_bo.c static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo, · line 616

Quoted 13 lines

static void ttm_bo_delayed_workqueue(struct work_struct *work) {

drivers/gpu/drm/ttm/ttm_bo.c +3 -4
 	struct ttm_bo_device *bdev =
 	    container_of(work, struct ttm_bo_device, wq.work);
+	unsigned long delay = ((HZ / 100) < 1) ? 1 : HZ / 100;
 
-	if (ttm_bo_delayed_delete(bdev, false)) {
-		schedule_delayed_work(&bdev->wq,
-				      ((HZ / 100) < 1) ? 1 : HZ / 100);
-	}
+	if (!ttm_bo_delayed_delete(bdev, false))
+		schedule_delayed_work(&bdev->wq, delay);
 }

Would be better to only add the ! here and leave the rest of this function unchanged in this patch. The cleanup can be done in a separate patch.

Other than that, both patches are

Signature · 6 lines
Earthling Michel Dänzer               |               http://www.amd.com
Libre software enthusiast             |             Mesa and X developer
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#4 lore raw

Re: [PATCH 2/2] drm/ttm: completely rework ttm_bo_delayed_delete

In reply to a message not in the archive

Hi, Christian,

While this has probably already been committed, and looks like a nice cleanup there are two things below I think needs fixing.

Quoted 61 lines · Christian König

There is no guarantee that the next entry on the ddelete list stays on the list when we drop the locks.

Completely rework this mess by moving processed entries on a temporary list.

---
  drivers/gpu/drm/ttm/ttm_bo.c | 77 ++++++++++++++------------------------------
  1 file changed, 25 insertions(+), 52 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +7 -30
index 7c1eac4f4b4b..ad0afdd71f21 100644
@@ -572,71 +572,47 @@ static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
   * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
   * encountered buffers.
   */
-
-static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
+static bool ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
  {
  	struct ttm_bo_global *glob = bdev->glob;
-	struct ttm_buffer_object *entry = NULL;
-	int ret = 0;
-
-	spin_lock(&glob->lru_lock);
-	if (list_empty(&bdev->ddestroy))
-		goto out_unlock;
+	struct list_head removed;
+	bool empty;
 
-	entry = list_first_entry(&bdev->ddestroy,
-		struct ttm_buffer_object, ddestroy);
-	kref_get(&entry->list_kref);
+	INIT_LIST_HEAD(&removed);
 
-	for (;;) {
-		struct ttm_buffer_object *nentry = NULL;
-
-		if (entry->ddestroy.next != &bdev->ddestroy) {
-			nentry = list_first_entry(&entry->ddestroy,
-				struct ttm_buffer_object, ddestroy);
-			kref_get(&nentry->list_kref);
-		}
-
-		ret = reservation_object_trylock(entry->resv) ? 0 : -EBUSY;
-		if (remove_all && ret) {
-			spin_unlock(&glob->lru_lock);
-			ret = reservation_object_lock(entry->resv, NULL);
-			spin_lock(&glob->lru_lock);
-		}
+	spin_lock(&glob->lru_lock);
+	while (!list_empty(&bdev->ddestroy)) {
+		struct ttm_buffer_object *bo;
 
-		if (!ret)
-			ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
-						  true);
-		else

drivers/gpu/drm/ttm/ttm_bo.c static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo, · line 593

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo.c +6 -3
-			spin_unlock(&glob->lru_lock);
+		bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
+				      ddestroy);
+		kref_get(&bo->list_kref);
+		list_move_tail(&bo->ddestroy, &removed);
+		spin_unlock(&glob->lru_lock);
 
-		kref_put(&entry->list_kref, ttm_bo_release_list);
-		entry = nentry;
+		reservation_object_lock(bo->resv, NULL);

Reservation may be a long lived lock, and typically if the object is reserved here, it's being evicted somewhere and there might be a substantial stall, which isn't really acceptable in the global workqueue. Better to move on to the next bo. This function was really intended to be non-blocking, unless remove_all == true. I even think it's safe to keep the spinlock held on tryreserve?

drivers/gpu/drm/ttm/ttm_bo.c static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo, · line 598

Quoted 7 lines
drivers/gpu/drm/ttm/ttm_bo.c +3 -2
-		if (ret || !entry)
-			goto out;
+		spin_lock(&glob->lru_lock);
+		ttm_bo_cleanup_refs(bo, false, !remove_all, true);
 
+		kref_put(&bo->list_kref, ttm_bo_release_list);

Calling a release function in atomic context is a bad thing. Nobody knows what locks needs to be taken in the release function and such code is prone to lock inversion and sleep-while-atomic bugs. Not long ago vfree() was even forbidden from atomic context. But here it's easily avoidable.

/Thomas

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): fdaf68f8-de8d-948d-4cac-156d06817d76-4+hqylr40dJg9hUCZPvPmw@public.gmane.org

#6 lore raw

Re: [PATCH 2/2] drm/ttm: completely rework ttm_bo_delayed_delete

Quoted 81 lines · Thomas Hellstrom

Hi, Christian,

While this has probably already been committed, and looks like a nice cleanup there are two things below I think needs fixing.

Quoted 62 lines · Christian König

There is no guarantee that the next entry on the ddelete list stays on the list when we drop the locks.

Completely rework this mess by moving processed entries on a temporary list.

---
  drivers/gpu/drm/ttm/ttm_bo.c | 77
++++++++++++++------------------------------

  1 file changed, 25 insertions(+), 52 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +7 -27
index 7c1eac4f4b4b..ad0afdd71f21 100644
@@ -572,71 +572,47 @@ static int ttm_bo_cleanup_refs(struct 
 ttm_buffer_object *bo,
    * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
    * encountered buffers.
    */
-
-static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool 
 remove_all)
+static bool ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool 
 remove_all)
   {
       struct ttm_bo_global *glob = bdev->glob;
-    struct ttm_buffer_object *entry = NULL;
-    int ret = 0;
-
-    spin_lock(&glob->lru_lock);
-    if (list_empty(&bdev->ddestroy))
-        goto out_unlock;
+    struct list_head removed;
+    bool empty;
   -    entry = list_first_entry(&bdev->ddestroy,
-        struct ttm_buffer_object, ddestroy);
-    kref_get(&entry->list_kref);
+    INIT_LIST_HEAD(&removed);
   -    for (;;) {
-        struct ttm_buffer_object *nentry = NULL;
-
-        if (entry->ddestroy.next != &bdev->ddestroy) {
-            nentry = list_first_entry(&entry->ddestroy,
-                struct ttm_buffer_object, ddestroy);
-            kref_get(&nentry->list_kref);
-        }
-
-        ret = reservation_object_trylock(entry->resv) ? 0 : -EBUSY;
-        if (remove_all && ret) {
-            spin_unlock(&glob->lru_lock);
-            ret = reservation_object_lock(entry->resv, NULL);
-            spin_lock(&glob->lru_lock);
-        }
+    spin_lock(&glob->lru_lock);
+    while (!list_empty(&bdev->ddestroy)) {
+        struct ttm_buffer_object *bo;
   -        if (!ret)
-            ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
-                          true);
-        else

drivers/gpu/drm/ttm/ttm_bo.c static int ttm_bo_cleanup_refs(struct · line 597

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo.c +6 -2
-            spin_unlock(&glob->lru_lock);
+        bo = list_first_entry(&bdev->ddestroy, struct 
 ttm_buffer_object,
+                      ddestroy);
+        kref_get(&bo->list_kref);
+        list_move_tail(&bo->ddestroy, &removed);
+        spin_unlock(&glob->lru_lock);
   -        kref_put(&entry->list_kref, ttm_bo_release_list);
-        entry = nentry;
+        reservation_object_lock(bo->resv, NULL);

Reservation may be a long lived lock, and typically if the object is reserved here, it's being evicted somewhere and there might be a substantial stall, which isn't really acceptable in the global workqueue. Better to move on to the next bo. This function was really intended to be non-blocking, unless remove_all == true. I even think it's safe to keep the spinlock held on tryreserve?

Quoted 7 lines

drivers/gpu/drm/ttm/ttm_bo.c static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,

Quoted 5 lines
drivers/gpu/drm/ttm/ttm_bo.c +2 -1
   -        if (ret || !entry)
-            goto out;
+        spin_lock(&glob->lru_lock);
+        ttm_bo_cleanup_refs(bo, false, !remove_all, true);
   +        kref_put(&bo->list_kref, ttm_bo_release_list);

Calling a release function in atomic context is a bad thing. Nobody knows what locks needs to be taken in the release function and such code is prone to lock inversion and sleep-while-atomic bugs. Not long ago vfree() was even forbidden from atomic context. But here it's easily avoidable.

Hmm. It actually looks like ttm_bo_cleanup_refs unlocks the glob->lru_lock just loke ttm_bo_cleanup_refs_and_unlock did, so my latter comment actually isn't correct. Intuitively removing the "unlock" prefix from the function would also mean that the unlocking functionality went away, but that doesn't seem to be the case. Also the commit message "needed for the next patch" isn't very helpful when the next patch is actually commited much later...

The first comment about trylocking still holds, though.

/Thomas

Quoted 8 lines

/Thomas

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): f69c3624-4c2f-eef5-05ec-5a6e922beaa6-4+hqylr40dJg9hUCZPvPmw@public.gmane.org

#8 lore raw

Re: [PATCH 2/2] drm/ttm: completely rework ttm_bo_delayed_delete

Am 14.12.2017 um 08:24 schrieb Thomas Hellstrom:

Quoted 73 lines · Thomas Hellstrom
Quoted 72 lines

Hi, Christian,

While this has probably already been committed, and looks like a nice cleanup there are two things below I think needs fixing.

Quoted 67 lines · Christian König

There is no guarantee that the next entry on the ddelete list stays on the list when we drop the locks.

Completely rework this mess by moving processed entries on a temporary list.

---
  drivers/gpu/drm/ttm/ttm_bo.c | 77
++++++++++++++------------------------------

  1 file changed, 25 insertions(+), 52 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +10 -28
index 7c1eac4f4b4b..ad0afdd71f21 100644
@@ -572,71 +572,47 @@ static int ttm_bo_cleanup_refs(struct 
 ttm_buffer_object *bo,
    * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
    * encountered buffers.
    */
-
-static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool 
 remove_all)
+static bool ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool 
 remove_all)
   {
       struct ttm_bo_global *glob = bdev->glob;
-    struct ttm_buffer_object *entry = NULL;
-    int ret = 0;
-
-    spin_lock(&glob->lru_lock);
-    if (list_empty(&bdev->ddestroy))
-        goto out_unlock;
+    struct list_head removed;
+    bool empty;
   -    entry = list_first_entry(&bdev->ddestroy,
-        struct ttm_buffer_object, ddestroy);
-    kref_get(&entry->list_kref);
+    INIT_LIST_HEAD(&removed);
   -    for (;;) {
-        struct ttm_buffer_object *nentry = NULL;
-
-        if (entry->ddestroy.next != &bdev->ddestroy) {
-            nentry = list_first_entry(&entry->ddestroy,
-                struct ttm_buffer_object, ddestroy);
-            kref_get(&nentry->list_kref);
-        }
-
-        ret = reservation_object_trylock(entry->resv) ? 0 : -EBUSY;
-        if (remove_all && ret) {
-            spin_unlock(&glob->lru_lock);
-            ret = reservation_object_lock(entry->resv, NULL);
-            spin_lock(&glob->lru_lock);
-        }
+    spin_lock(&glob->lru_lock);
+    while (!list_empty(&bdev->ddestroy)) {
+        struct ttm_buffer_object *bo;
   -        if (!ret)
-            ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
-                          true);
-        else
-            spin_unlock(&glob->lru_lock);
+        bo = list_first_entry(&bdev->ddestroy, struct 
 ttm_buffer_object,
+                      ddestroy);
+        kref_get(&bo->list_kref);
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/ttm/ttm_bo.c static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo, · line 593

Quoted 5 lines
drivers/gpu/drm/ttm/ttm_bo.c +3 -1
+        list_move_tail(&bo->ddestroy, &removed);
+        spin_unlock(&glob->lru_lock);
   -        kref_put(&entry->list_kref, ttm_bo_release_list);
-        entry = nentry;
+        reservation_object_lock(bo->resv, NULL);

Reservation may be a long lived lock, and typically if the object is reserved here, it's being evicted somewhere and there might be a substantial stall, which isn't really acceptable in the global workqueue. Better to move on to the next bo. This function was really intended to be non-blocking, unless remove_all == true. I even think it's safe to keep the spinlock held on tryreserve?

This approach doesn't really work with shared reservation objects and was actually the originally reason why I stumbled over the function being a bit buggy.

The reservation object is a really busy lock because of all the command submission going on. So when you only have a single trylock every few ms it is rather unlikely that you can actually grab it. We ended up with tons of objects on the ddestroy list which couldn't be reaped because of this.

At least amdgpu tries to avoid to wait for any GPU operation while holding the reservation locks, so at least for us that shouldn't be an issue any more. And I'm going to pipeline delayed deletes as well rather soon.

What we could do here is to use a test like "bo->resv == &bo->ttm_resv" and only trylock if it isn't a shared reservation object. How about that?

Regards,
Christian.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
drivers/gpu/drm/ttm/ttm_bo.c +2 -1
   -        if (ret || !entry)
-            goto out;
+        spin_lock(&glob->lru_lock);
+        ttm_bo_cleanup_refs(bo, false, !remove_all, true);
   +        kref_put(&bo->list_kref, ttm_bo_release_list);
Quoted 20 lines
Quoted 1 line

Calling a release function in atomic context is a bad thing. Nobody knows what locks needs to be taken in the release function and such code is prone to lock inversion and sleep-while-atomic bugs. Not long ago vfree() was even forbidden from atomic context. But here it's easily avoidable.

Hmm. It actually looks like ttm_bo_cleanup_refs unlocks the glob->lru_lock just loke ttm_bo_cleanup_refs_and_unlock did, so my latter comment actually isn't correct. Intuitively removing the "unlock" prefix from the function would also mean that the unlocking functionality went away, but that doesn't seem to be the case. Also the commit message "needed for the next patch" isn't very helpful when the next patch is actually commited much later...

The first comment about trylocking still holds, though.

/Thomas

Quoted 8 lines

/Thomas

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 76cd677f-a245-e55c-4087-009b1ca4b051-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#10 lore raw

Re: [PATCH 2/2] drm/ttm: completely rework ttm_bo_delayed_delete

Quoted 83 lines · Christian König

Am 14.12.2017 um 08:24 schrieb Thomas Hellstrom:

Quoted 73 lines · Thomas Hellstrom
Quoted 72 lines

Hi, Christian,

While this has probably already been committed, and looks like a nice cleanup there are two things below I think needs fixing.

Quoted 67 lines · Christian König

There is no guarantee that the next entry on the ddelete list stays on the list when we drop the locks.

Completely rework this mess by moving processed entries on a temporary list.

---
  drivers/gpu/drm/ttm/ttm_bo.c | 77
++++++++++++++------------------------------

  1 file changed, 25 insertions(+), 52 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +10 -28
index 7c1eac4f4b4b..ad0afdd71f21 100644
@@ -572,71 +572,47 @@ static int ttm_bo_cleanup_refs(struct 
 ttm_buffer_object *bo,
    * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
    * encountered buffers.
    */
-
-static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool 
 remove_all)
+static bool ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool 
 remove_all)
   {
       struct ttm_bo_global *glob = bdev->glob;
-    struct ttm_buffer_object *entry = NULL;
-    int ret = 0;
-
-    spin_lock(&glob->lru_lock);
-    if (list_empty(&bdev->ddestroy))
-        goto out_unlock;
+    struct list_head removed;
+    bool empty;
   -    entry = list_first_entry(&bdev->ddestroy,
-        struct ttm_buffer_object, ddestroy);
-    kref_get(&entry->list_kref);
+    INIT_LIST_HEAD(&removed);
   -    for (;;) {
-        struct ttm_buffer_object *nentry = NULL;
-
-        if (entry->ddestroy.next != &bdev->ddestroy) {
-            nentry = list_first_entry(&entry->ddestroy,
-                struct ttm_buffer_object, ddestroy);
-            kref_get(&nentry->list_kref);
-        }
-
-        ret = reservation_object_trylock(entry->resv) ? 0 : -EBUSY;
-        if (remove_all && ret) {
-            spin_unlock(&glob->lru_lock);
-            ret = reservation_object_lock(entry->resv, NULL);
-            spin_lock(&glob->lru_lock);
-        }
+    spin_lock(&glob->lru_lock);
+    while (!list_empty(&bdev->ddestroy)) {
+        struct ttm_buffer_object *bo;
   -        if (!ret)
-            ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
-                          true);
-        else
-            spin_unlock(&glob->lru_lock);
+        bo = list_first_entry(&bdev->ddestroy, struct 
 ttm_buffer_object,
+                      ddestroy);
+        kref_get(&bo->list_kref);
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/ttm/ttm_bo.c static int ttm_bo_cleanup_refs(struct · line 597

Quoted 5 lines
drivers/gpu/drm/ttm/ttm_bo.c +3 -1
+        list_move_tail(&bo->ddestroy, &removed);
+        spin_unlock(&glob->lru_lock);
   -        kref_put(&entry->list_kref, ttm_bo_release_list);
-        entry = nentry;
+        reservation_object_lock(bo->resv, NULL);

Reservation may be a long lived lock, and typically if the object is reserved here, it's being evicted somewhere and there might be a substantial stall, which isn't really acceptable in the global workqueue. Better to move on to the next bo. This function was really intended to be non-blocking, unless remove_all == true. I even think it's safe to keep the spinlock held on tryreserve?

Quoted 2 lines

This approach doesn't really work with shared reservation objects and was actually the originally reason why I stumbled over the function being a bit buggy.

Actually I think it was correct, but very non-deterministic and difficult to follow. Both me and Maarten had our passes trying to make it look better but failed :(. It relied on the ddestroy list node either being on the ddestroy list or not on a list at all. So if the ->next pointer was on the list, we'd continue iteration, even though the object might have moved on the list. But it looks much better now.

Quoted 1 line

The reservation object is a really busy lock because of all the command submission going on. So when you only have a single trylock every few ms it is rather unlikely that you can actually grab it. We ended up with tons of objects on the ddestroy list which couldn't be reaped because of this.

OK, I see.

Quoted 5 lines

At least amdgpu tries to avoid to wait for any GPU operation while holding the reservation locks, so at least for us that shouldn't be an issue any more. And I'm going to pipeline delayed deletes as well rather soon.

What we could do here is to use a test like "bo->resv == &bo->ttm_resv" and only trylock if it isn't a shared reservation object. How about that?

Either that or a private workqueue separate from the global one is fine with me.

Thanks,
Thomas

Quoted 31 lines

Regards,
Christian.

Quoted 27 lines
Quoted 8 lines

drivers/gpu/drm/ttm/ttm_bo.c static int ttm_bo_cleanup_refs(struct · line 601

Quoted 6 lines
drivers/gpu/drm/ttm/ttm_bo.c +2 -1
   -        if (ret || !entry)
-            goto out;
+        spin_lock(&glob->lru_lock);
+        ttm_bo_cleanup_refs(bo, false, !remove_all, true);
   +        kref_put(&bo->list_kref, ttm_bo_release_list);

Calling a release function in atomic context is a bad thing. Nobody knows what locks needs to be taken in the release function and such code is prone to lock inversion and sleep-while-atomic bugs. Not long ago vfree() was even forbidden from atomic context. But here it's easily avoidable.

Hmm. It actually looks like ttm_bo_cleanup_refs unlocks the glob->lru_lock just loke ttm_bo_cleanup_refs_and_unlock did, so my latter comment actually isn't correct. Intuitively removing the "unlock" prefix from the function would also mean that the unlocking functionality went away, but that doesn't seem to be the case. Also the commit message "needed for the next patch" isn't very helpful when the next patch is actually commited much later...

The first comment about trylocking still holds, though.

/Thomas

Quoted 8 lines

/Thomas

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 20171115123151.3038-1-christian.koenig-5C7GfCeVMHo@public.gmane.org

Reviewed-by Roger He
#12 lore raw

Thanks
Roger(Hongbo.He)
-----Original Message-----
From: amd-gfx [mailto:[email protected]] On Behalf Of Christian K?nig
Sent: Wednesday, November 15, 2017 8:32 PM
To: [email protected]; [email protected]
Subject: [PATCH 1/2] drm/ttm: fix ttm_mem_evict_first once more

The code path isn't hit at the moment, but we need to take the lock to add the BO back to the LRU.

---
 drivers/gpu/drm/ttm/ttm_bo.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +5 -2
@@ -793,10 +793,13 @@ static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
 	spin_unlock(&glob->lru_lock);
 
 	ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
-	if (locked)
+	if (locked) {
 		ttm_bo_unreserve(bo);
-	else
+	} else {
+		spin_lock(&glob->lru_lock);
 		ttm_bo_add_to_lru(bo);
+		spin_unlock(&glob->lru_lock);
+	}
 
 	kref_put(&bo->list_kref, ttm_bo_release_list);
 	return ret;

--
2.11.0

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx