All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] drm/ttm: Fix race condition in ttm_bo_delayed_delete
@ 2010-01-18 18:47 Luca Barbieri
  2010-01-18 19:40 ` Thomas Hellstrom
  0 siblings, 1 reply; 34+ messages in thread
From: Luca Barbieri @ 2010-01-18 18:47 UTC (permalink / raw)
  To: airlied-cv59FeDIM0c
  Cc: nouveau-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW, Thomas Hellstrom,
	dri-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f, Luca Barbieri

ttm_bo_delayed_delete has a race condition, because after we do:
kref_put(&nentry->list_kref, ttm_bo_release_list);

we are not holding the list lock and not holding any reference to
objects, and thus every bo in the list can be removed and freed at
this point.

However, we then use the next pointer we stored, which is not guaranteed
to be valid.

This was apparently the cause of some Nouveau oopses I experienced.

This patch rewrites the function so that it keeps the reference to nentry
until nentry itself is freed and we already got a reference to nentry->next.

It should now be correct and free of races, but please double check this.

Signed-off-by: Luca Barbieri <luca-Ukmtq+NC3rhBHFWNQifrYwC/G2K4zDHf@public.gmane.org>
---
 drivers/gpu/drm/ttm/ttm_bo.c |   58 +++++++++++++++++------------------------
 1 files changed, 24 insertions(+), 34 deletions(-)

diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
index 2920f9a..1daa2f1 100644
--- a/drivers/gpu/drm/ttm/ttm_bo.c
+++ b/drivers/gpu/drm/ttm/ttm_bo.c
@@ -523,52 +523,42 @@ static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo, bool remove_all)
 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
 {
 	struct ttm_bo_global *glob = bdev->glob;
-	struct ttm_buffer_object *entry, *nentry;
-	struct list_head *list, *next;
-	int ret;
+	struct ttm_buffer_object *entry;
+	int ret = 0;
 
 	spin_lock(&glob->lru_lock);
-	list_for_each_safe(list, next, &bdev->ddestroy) {
-		entry = list_entry(list, struct ttm_buffer_object, ddestroy);
-		nentry = NULL;
+	if (list_empty(&bdev->ddestroy)) {
+		spin_unlock(&glob->lru_lock);
+		return 0;
+	}
 
-		/*
-		 * Protect the next list entry from destruction while we
-		 * unlock the lru_lock.
-		 */
+	entry = list_first_entry(&bdev->ddestroy,
+		struct ttm_buffer_object, ddestroy);
+	kref_get(&entry->list_kref);
 
-		if (next != &bdev->ddestroy) {
-			nentry = list_entry(next, struct ttm_buffer_object,
-					    ddestroy);
+	for (;;) {
+		struct ttm_buffer_object *nentry = NULL;
+
+		if (!list_empty(&entry->ddestroy)
+			&& entry->ddestroy.next != &bdev->ddestroy) {
+			nentry = list_entry(entry->ddestroy.next,
+				struct ttm_buffer_object, ddestroy);
 			kref_get(&nentry->list_kref);
 		}
-		kref_get(&entry->list_kref);
 
 		spin_unlock(&glob->lru_lock);
 		ret = ttm_bo_cleanup_refs(entry, remove_all);
 		kref_put(&entry->list_kref, ttm_bo_release_list);
+		entry = nentry;
 
-		spin_lock(&glob->lru_lock);
-		if (nentry) {
-			bool next_onlist = !list_empty(next);
-			spin_unlock(&glob->lru_lock);
-			kref_put(&nentry->list_kref, ttm_bo_release_list);
-			spin_lock(&glob->lru_lock);
-			/*
-			 * Someone might have raced us and removed the
-			 * next entry from the list. We don't bother restarting
-			 * list traversal.
-			 */
-
-			if (!next_onlist)
-				break;
-		}
-		if (ret)
+		if (ret || !entry)
 			break;
+
+		spin_lock(&glob->lru_lock);
 	}
-	ret = !list_empty(&bdev->ddestroy);
-	spin_unlock(&glob->lru_lock);
 
+	if (entry)
+		kref_put(&entry->list_kref, ttm_bo_release_list);
 	return ret;
 }
 
-- 
1.6.3.3

^ permalink raw reply related	[flat|nested] 34+ messages in thread

end of thread, other threads:[~2010-01-25 20:51 UTC | newest]

Thread overview: 34+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-01-18 18:47 [PATCH] drm/ttm: Fix race condition in ttm_bo_delayed_delete Luca Barbieri
2010-01-18 19:40 ` Thomas Hellstrom
     [not found]   ` <4B54B949.9010906-pghWNbHTmq7QT0dZR+AlfA@public.gmane.org>
2010-01-18 22:33     ` Luca Barbieri
     [not found]       ` <ff13bc9a1001181433v1694e681l9f7ce9d880132dc3-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2010-01-20 11:28         ` Thomas Hellstrom
     [not found]           ` <4B56E8EE.8090706-4+hqylr40dJg9hUCZPvPmw@public.gmane.org>
2010-01-20 12:11             ` Thomas Hellstrom
2010-01-20 12:11               ` Thomas Hellstrom
     [not found]               ` <4B56F308.5090603-4+hqylr40dJg9hUCZPvPmw@public.gmane.org>
2010-01-20 12:16                 ` Thomas Hellstrom
2010-01-20 12:16                   ` Thomas Hellstrom
     [not found]                   ` <4B56F401.8070700-pghWNbHTmq7QT0dZR+AlfA@public.gmane.org>
2010-01-20 19:22                     ` Luca Barbieri
     [not found]                       ` <ff13bc9a1001201122y110fb003k704bc6d05d2aea07-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2010-01-20 20:16                         ` Thomas Hellstrom
     [not found]                           ` <4B5764BA.7080801-pghWNbHTmq7QT0dZR+AlfA@public.gmane.org>
2010-01-20 20:45                             ` Luca Barbieri
     [not found]                               ` <ff13bc9a1001201245g6ee25219q851b7989968f4c7b-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2010-01-20 20:58                                 ` Luca Barbieri
     [not found]                                   ` <ff13bc9a1001201258i37ee7354gb305aa98afae3716-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2010-01-20 21:11                                     ` Thomas Hellstrom
2010-01-20 21:04                                 ` Thomas Hellstrom
     [not found]                                   ` <4B576FF5.9040907-4+hqylr40dJg9hUCZPvPmw@public.gmane.org>
2010-01-21  3:49                                     ` Luca Barbieri
     [not found]                                       ` <ff13bc9a1001201949l4691f202v2a2874b9cef86f37-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2010-01-21 12:29                                         ` Jerome Glisse
     [not found]                                           ` <20100121122920.GB3837-bi+AKbBUZKY6gyzm1THtWbp2dZbC/Bob@public.gmane.org>
2010-01-21 12:59                                             ` Thomas Hellstrom
     [not found]                                               ` <4B584FAE.8040801-4+hqylr40dJg9hUCZPvPmw@public.gmane.org>
2010-01-25  8:14                                                 ` Jerome Glisse
     [not found]                                                   ` <20100125081444.GA23124-bi+AKbBUZKY6gyzm1THtWbp2dZbC/Bob@public.gmane.org>
2010-01-25 20:51                                                     ` Thomas Hellstrom
2010-01-21 15:14                                             ` Luca Barbieri
     [not found]                                               ` <ff13bc9a1001210714m34c3976etc7680f056cb55453-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2010-01-25  8:20                                                 ` Jerome Glisse
2010-01-21 12:53                                         ` Thomas Hellstrom
     [not found]                                           ` <4B584E48.8020806-4+hqylr40dJg9hUCZPvPmw@public.gmane.org>
2010-01-21 13:40                                             ` Luca Barbieri
     [not found]                                               ` <ff13bc9a1001210540t7dc2b7a7p38359ca82b8b3eb4-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2010-01-21 14:07                                                 ` Francisco Jerez
     [not found]                                                   ` <87vdeveekk.fsf-sGOZH3hwPm2sTnJN9+BGXg@public.gmane.org>
2010-01-21 14:17                                                     ` Luca Barbieri
     [not found]                                                       ` <ff13bc9a1001210617qe37ab7at949d545216693608-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2010-01-21 14:44                                                         ` Francisco Jerez
     [not found]                                                           ` <87pr53ecwd.fsf-sGOZH3hwPm2sTnJN9+BGXg@public.gmane.org>
2010-01-21 15:36                                                             ` Luca Barbieri
     [not found]                                                               ` <ff13bc9a1001210736k71740417r7b129c70374fece3-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2010-01-21 16:18                                                                 ` Francisco Jerez
     [not found]                                                                   ` <87y6jrbfef.fsf-sGOZH3hwPm2sTnJN9+BGXg@public.gmane.org>
2010-01-21 16:30                                                                     ` Luca Barbieri
     [not found]                                                                       ` <ff13bc9a1001210830g2653f672r918f8cb90cbf6170-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2010-01-21 17:39                                                                         ` Francisco Jerez
2010-01-21 15:39                                                             ` Maarten Maathuis
     [not found]                                                               ` <6d4bc9fc1001210739r2bb8b4c4i18590376a1628d82-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2010-01-21 15:56                                                                 ` Luca Barbieri
     [not found]                                                                   ` <ff13bc9a1001210756r1e627146w89fb2138ca77e6b5-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2010-01-21 16:02                                                                     ` Maarten Maathuis
2010-01-21 14:23                                                 ` Thomas Hellstrom

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.