Skip to contentSkip to conversation

drm/nouveau: fix ref leak in nouveau_gem_pushbuf_validate()

PATCH In review Pekka Paalanen 1 participant 2 messages Sep 7, 2009 1 message not in the archive

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

PATCH 1/1 drm/nouveau: fix ref leak in nouveau_gem_pushbuf_validate()

If ttm_bo_reserve() in nouveau_gem_pushbuf_validate() failed, the GEM object reference is leaked, since the object is not yet in the list for cleanup.

Create a new function nouveau_gem_pushbuf_lookup_and_reserve() that does the GEM object lookup and ttm_bo_reserve() together, or fails and undos them both, eliminating GEM object reference leaks.

I believe this bug lead to a permanent validation failure, which was worked around in the earlier commit "drm/nouveau: bail out if validation fails repeatedly". I also believe this bug later triggered a BUG_ON() in TTM cleanup during module unload, making nouveau.ko impossible to unload. The relevant bug report:
https://bugs.freedesktop.org/show_bug.cgi?id=23741

This patch also includes Ben's cleanup fix in nouveau_gem_pushbuf_validate(), where the current object ref and reservervation would have not been cancelled, if cpu_writers test failed.

However, this patch does NOT fix nouveau_gem_pushbuf_validate() completely. The scenario described in the bug report now leads to X server being stuck in uninterruptible sleep in ttm_bo_wait_unreserved().

---
 drivers/gpu/drm/nouveau/nouveau_gem.c |   65 +++++++++++++++++++++------------
 1 files changed, 42 insertions(+), 23 deletions(-)
drivers/gpu/drm/nouveau/nouveau_gem.c +42 -23
index f0c78a8..55e7988 100644
@@ -268,6 +268,40 @@ nouveau_gem_pushbuf_fence(struct list_head *list, struct nouveau_fence *fence)
 }
 
 static int
+nouveau_gem_pushbuf_lookup_and_reserve(struct nouveau_channel *chan,
+				struct drm_file *file_priv,
+				struct drm_nouveau_gem_pushbuf_bo *pbbo,
+				struct nouveau_bo **bop)
+{
+	struct drm_gem_object *gem;
+	struct nouveau_bo *nvbo;
+	int ret;
+
+	gem = drm_gem_object_lookup(chan->dev, file_priv, pbbo->handle);
+	if (!gem) {
+		NV_ERROR(chan->dev, "Unknown handle 0x%08x\n", pbbo->handle);
+		return -EINVAL;
+	}
+	nvbo = gem->driver_private;
+
+	ret = ttm_bo_reserve(&nvbo->bo, false, false, true,
+			     chan->fence.sequence);
+	switch (ret) {
+	case 0:
+		*bop = nvbo;
+		return 0;
+	case -EAGAIN:
+		ret = ttm_bo_wait_unreserved(&nvbo->bo, false);
+		if (ret == 0)
+			ret = -EAGAIN;
+		/* fall through */
+	default:
+		drm_gem_object_unreference(gem);
+	}
+	return ret;
+}
+
+static int
 nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
 			     struct drm_file *file_priv,
 			     struct drm_nouveau_gem_pushbuf_bo *pbbo,
@@ -279,7 +313,7 @@ nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
 	struct drm_nouveau_gem_pushbuf_bo __user *user_pbbos =
 				(void __force __user *)(uintptr_t)user_buffers;
 	struct nouveau_fence *prev_fence;
-	struct nouveau_bo *nvbo;
+	struct nouveau_bo *nvbo = NULL;
 	struct list_head *entry, *tmp;
 	int ret = -EINVAL;
 	int i;
@@ -296,28 +330,15 @@ retry:
 	}
 
 	for (i = 0, b = pbbo; i < nr_buffers; i++, b++) {
-		struct drm_gem_object *gem;
-
-		gem = drm_gem_object_lookup(dev, file_priv, b->handle);
-		if (!gem) {
-			NV_ERROR(dev, "Unknown handle 0x%08x\n", b->handle);
-			ret = -EINVAL;
-			goto out_unref;
-		}
-		nvbo = gem->driver_private;
-
-		ret = ttm_bo_reserve(&nvbo->bo, false, false, true,
-				     chan->fence.sequence);
-		if (ret) {
+		ret = nouveau_gem_pushbuf_lookup_and_reserve(chan, file_priv,
+								b, &nvbo);
+		if (ret == -EAGAIN) {
 			nouveau_gem_pushbuf_backoff(list);
-			if (ret == -EAGAIN) {
-				ret = ttm_bo_wait_unreserved(&nvbo->bo, false);
-				if (unlikely(ret))
-					goto out_unref;
-				goto retry;
-			} else
-				goto out_unref;
+			goto retry;
 		}
+		if (ret)
+			goto out_unref;
+		list_add_tail(&nvbo->entry, list);
 
 		if (unlikely(atomic_read(&nvbo->bo.cpu_writers) > 0)) {
 			nouveau_gem_pushbuf_backoff(list);
@@ -326,8 +347,6 @@ retry:
 				goto out_unref;
 			goto retry;
 		}
-
-		list_add_tail(&nvbo->entry, list);
 	}
 
 	b = pbbo;
Signature · 1 line
1.6.3.3

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw

From ee49e66551a28550b69a1a0fc7f45ec2ab24c295 Mon Sep 17 00:00:00 2001
From: Pekka Paalanen <[email protected]>
Date: Tue, 8 Sep 2009 00:05:24 +0300
Subject: [PATCH] drm/nouveau: do not call ttm_bo_wait_unreserved()

In nouveau_gem_puhsbuf_validate() path, ttm_bo_reserve() already calls ttm_bo_wait_unreserved() internally. Calling it again is not only racy and needless, but it makes the X process hang in uninterruptible sleep.

Remove the call to ttm_bo_wait_unreserved() from
nouveau_gem_pushbuf_validate().

---

NOTE!

This is a tentative patch, I didn't even have the time to compile-test it. On top of the previous.

 drivers/gpu/drm/nouveau/nouveau_gem.c |   17 ++++++-----------
 1 files changed, 6 insertions(+), 11 deletions(-)
drivers/gpu/drm/nouveau/nouveau_gem.c +6 -11
index 55e7988..b339de2 100644
@@ -286,19 +286,14 @@ nouveau_gem_pushbuf_lookup_and_reserve(struct nouveau_channel *chan,
 
 	ret = ttm_bo_reserve(&nvbo->bo, false, false, true,
 			     chan->fence.sequence);
-	switch (ret) {
-	case 0:
-		*bop = nvbo;
-		return 0;
-	case -EAGAIN:
-		ret = ttm_bo_wait_unreserved(&nvbo->bo, false);
-		if (ret == 0)
-			ret = -EAGAIN;
-		/* fall through */
-	default:
+
+	if (ret) {
 		drm_gem_object_unreference(gem);
+		return ret;
 	}
-	return ret;
+
+	*bop = nvbo;
+	return 0;
 }
 
 static int
Signature · 1 line
1.6.3.3