Skip to contentSkip to conversation

drm/nouveau: wait for moving fence after pinning

PATCH In review Christian König 2 participants 10 messages Jun 21, 2021 – Jun 22, 2021

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

PATCH 1/3 drm/nouveau: wait for moving fence after pinning

We actually need to wait for the moving fence after pinning the BO to make sure that the pin is completed.

---
 drivers/gpu/drm/nouveau/nouveau_prime.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/nouveau/nouveau_prime.c +7 -1
index 347488685f74..591738545eba 100644
@@ -93,7 +93,13 @@ int nouveau_gem_prime_pin(struct drm_gem_object *obj)
 	if (ret)
 		return -EINVAL;
 
-	return 0;
+	if (nvbo->bo.moving) {
+		ret = dma_fence_wait(nvbo->bo.moving, true);
+		if (ret)
+			nouveau_bo_unpin(nvbo);
+	}
+
+	return ret;
 }
 
 void nouveau_gem_prime_unpin(struct drm_gem_object *obj)
Signature · 6 lines
2.25.1

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

PATCH 2/3 drm/radeon: wait for moving fence after pinning

We actually need to wait for the moving fence after pinning the BO to make sure that the pin is completed.

---
 drivers/gpu/drm/radeon/radeon_prime.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/gpu/drm/radeon/radeon_prime.c +13 -3
index 42a87948e28c..4a90807351e7 100644
@@ -77,9 +77,19 @@ int radeon_gem_prime_pin(struct drm_gem_object *obj)
 
 	/* pin buffer into GTT */
 	ret = radeon_bo_pin(bo, RADEON_GEM_DOMAIN_GTT, NULL);
-	if (likely(ret == 0))
-		bo->prime_shared_count++;
-
+	if (unlikely(ret))
+		goto error;
+
+	if (bo->tbo.moving) {
+		ret = dma_fence_wait(bo->tbo.moving, false);
+		if (unlikely(ret)) {
+			radeon_bo_unpin(bo);
+			goto error;
+		}
+	}
+
+	bo->prime_shared_count++;
+error:
 	radeon_bo_unreserve(bo);
 	return ret;
 }
Signature · 1 line
2.25.1
Reviewed-by on v1 2/3
#2 lore raw

Re: [PATCH 2/3] drm/radeon: wait for moving fence after pinning

Quoted 15 lines · Christian König

We actually need to wait for the moving fence after pinning the BO to make sure that the pin is completed.

---
 drivers/gpu/drm/radeon/radeon_prime.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/gpu/drm/radeon/radeon_prime.c +0 -0
index 42a87948e28c..4a90807351e7 100644
@@ -77,9 +77,19 @@ int radeon_gem_prime_pin(struct drm_gem_object *obj)
 

drivers/gpu/drm/radeon/radeon_prime.c int radeon_gem_prime_pin(struct drm_gem_object *obj) · line 84

Quoted 10 lines
drivers/gpu/drm/radeon/radeon_prime.c +5 -3
 	/* pin buffer into GTT */
 	ret = radeon_bo_pin(bo, RADEON_GEM_DOMAIN_GTT, NULL);
-	if (likely(ret == 0))
-		bo->prime_shared_count++;
-
+	if (unlikely(ret))
+		goto error;
+
+	if (bo->tbo.moving) {
+		ret = dma_fence_wait(bo->tbo.moving, false);

Here we wait whil holding the reservation, so we should be all fine. Maybe not the nicest to wait while locked, but also I don't think it'll matter.

Quoted 14 lines
drivers/gpu/drm/radeon/radeon_prime.c +8 -0
+		if (unlikely(ret)) {
+			radeon_bo_unpin(bo);
+			goto error;
+		}
+	}
+
+	bo->prime_shared_count++;
+error:
 	radeon_bo_unreserve(bo);
 	return ret;
 }
Signature · 1 line
2.25.1
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau
#3 lore raw

PATCH 3/3 drm/amdgpu: wait for moving fence after pinning

In reply to Christian König

We actually need to wait for the moving fence after pinning the BO to make sure that the pin is completed.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +13 -1
index baa980a477d9..37ec59365080 100644
@@ -214,9 +214,21 @@ static int amdgpu_dma_buf_pin(struct dma_buf_attachment *attach)
 {
 	struct drm_gem_object *obj = attach->dmabuf->priv;
 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
+	int r;
 
 	/* pin buffer into GTT */
-	return amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
+	r = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
+	if (r)
+		return r;
+
+	if (bo->tbo.moving) {
+		r = dma_fence_wait(bo->tbo.moving, true);
+		if (r) {
+			amdgpu_bo_unpin(bo);
+			return r;
+		}
+	}
+	return 0;
 }
 
 /**
Signature · 6 lines
2.25.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
Reviewed-by on v1 3/3
#4 lore raw

Re: [PATCH 3/3] drm/amdgpu: wait for moving fence after pinning

Quoted 16 lines · Christian König

We actually need to wait for the moving fence after pinning the BO to make sure that the pin is completed.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +0 -0
index baa980a477d9..37ec59365080 100644
@@ -214,9 +214,21 @@ static int amdgpu_dma_buf_pin(struct dma_buf_attachment *attach)
 {
 	struct drm_gem_object *obj = attach->dmabuf->priv;

drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c static int amdgpu_dma_buf_pin(struct dma_buf_attachment *attach) · line 224

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +6 -1
 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
+	int r;
 
 	/* pin buffer into GTT */
-	return amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
+	r = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
+	if (r)
+		return r;
+
+	if (bo->tbo.moving) {

dma-buf.c guarantees we have the reservation here, so we're fine.

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +7 -0
+		r = dma_fence_wait(bo->tbo.moving, true);
+		if (r) {
+			amdgpu_bo_unpin(bo);
+			return r;
+		}
+	}
+	return 0;
 }
 
 /**
Signature · 1 line
2.25.1
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#5 lore raw
In reply to Christian König
Quoted 14 lines · Christian König

We actually need to wait for the moving fence after pinning the BO to make sure that the pin is completed.

---
 drivers/gpu/drm/nouveau/nouveau_prime.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/nouveau/nouveau_prime.c +0 -0
index 347488685f74..591738545eba 100644

drivers/gpu/drm/nouveau/nouveau_prime.c int nouveau_gem_prime_pin(struct drm_gem_object *obj) · line 96

Quoted 5 lines
drivers/gpu/drm/nouveau/nouveau_prime.c +1 -1
@@ -93,7 +93,13 @@ int nouveau_gem_prime_pin(struct drm_gem_object *obj)
 	if (ret)
 		return -EINVAL;
 
-	return 0;
+	if (nvbo->bo.moving) {

Don't we need to hold the dma_resv to read this? We can grab a reference and then unlock, but I think just unlocked wait can go boom pretty easily (since we don't hold a reference or lock so someone else can jump in and free the moving fence).
-Daniel

Quoted 12 lines
drivers/gpu/drm/nouveau/nouveau_prime.c +6 -0
+		ret = dma_fence_wait(nvbo->bo.moving, true);
+		if (ret)
+			nouveau_bo_unpin(nvbo);
+	}
+
+	return ret;
 }
 
 void nouveau_gem_prime_unpin(struct drm_gem_object *obj)
Signature · 1 line
2.25.1
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#6 lore raw

Am 21.06.21 um 16:54 schrieb Daniel Vetter:

Quoted 18 lines · Christian König
Quoted 17 lines

We actually need to wait for the moving fence after pinning the BO to make sure that the pin is completed.

---
  drivers/gpu/drm/nouveau/nouveau_prime.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/nouveau/nouveau_prime.c +0 -0
index 347488685f74..591738545eba 100644
@@ -93,7 +93,13 @@ int nouveau_gem_prime_pin(struct drm_gem_object *obj)
  	if (ret)
  		return -EINVAL;
 
Quoted 6 lines

drivers/gpu/drm/nouveau/nouveau_prime.c int nouveau_gem_prime_pin(struct drm_gem_object *obj) · line 96

Quoted 2 lines
drivers/gpu/drm/nouveau/nouveau_prime.c +1 -1
-	return 0;
+	if (nvbo->bo.moving) {

Don't we need to hold the dma_resv to read this? We can grab a reference and then unlock, but I think just unlocked wait can go boom pretty easily (since we don't hold a reference or lock so someone else can jump in and free the moving fence).

The moving fence is only modified while the BO is moved and since we have just successfully pinned it....

But in general I agree that it would be better to avoid this. I just didn't wanted to open a bigger can of worms by changing nouveau so much.

Christian.

Quoted 10 lines

-Daniel

Quoted 8 lines
drivers/gpu/drm/nouveau/nouveau_prime.c +6 -0
+		ret = dma_fence_wait(nvbo->bo.moving, true);
+		if (ret)
+			nouveau_bo_unpin(nvbo);
+	}
+
+	return ret;
  }
 
Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/nouveau/nouveau_prime.c +0 -0
  void nouveau_gem_prime_unpin(struct drm_gem_object *obj)
Signature · 1 line
2.25.1

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau

#7 lore raw
Quoted 27 lines · Christian König

Am 21.06.21 um 16:54 schrieb Daniel Vetter:

Quoted 18 lines · Christian König
Quoted 17 lines

We actually need to wait for the moving fence after pinning the BO to make sure that the pin is completed.

---
  drivers/gpu/drm/nouveau/nouveau_prime.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/nouveau/nouveau_prime.c +0 -0
index 347488685f74..591738545eba 100644
@@ -93,7 +93,13 @@ int nouveau_gem_prime_pin(struct drm_gem_object *obj)
     if (ret)
             return -EINVAL;
 
Quoted 6 lines

drivers/gpu/drm/nouveau/nouveau_prime.c int nouveau_gem_prime_pin(struct drm_gem_object *obj) · line 96

Quoted 2 lines
drivers/gpu/drm/nouveau/nouveau_prime.c +1 -1
-    return 0;
+    if (nvbo->bo.moving) {

Don't we need to hold the dma_resv to read this? We can grab a reference and then unlock, but I think just unlocked wait can go boom pretty easily (since we don't hold a reference or lock so someone else can jump in and free the moving fence).

Quoted 2 lines

The moving fence is only modified while the BO is moved and since we have just successfully pinned it....

Yeah ... so probably correct, but really tricky. Just wrapping a ttm_bo_reserve/unreserve around the code you add should be enough and get the job done?

Quoted 2 lines

But in general I agree that it would be better to avoid this. I just didn't wanted to open a bigger can of worms by changing nouveau so much.

Yeah, but I'm kinda thinking of some helpers to wait for the move fence (so that later on we can switch from having the exclusive fence to the move fence do that, maybe). And then locking checks in there would be nice.

Also avoids the case of explaining why lockless here is fine, but lockless wait for the exclusive fence in e.g. a dynami dma-buf importer is very much not fine at all. Just all around less trouble. -Daniel

Quoted 18 lines

Christian.

Quoted 14 lines

-Daniel

Quoted 12 lines
drivers/gpu/drm/nouveau/nouveau_prime.c +6 -1
+            ret = dma_fence_wait(nvbo->bo.moving, true);
+            if (ret)
+                    nouveau_bo_unpin(nvbo);
+    }
+
+    return ret;
  }
 
  void nouveau_gem_prime_unpin(struct drm_gem_object *obj)
--

2.25.1

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#8 lore raw
Quoted 30 lines · Daniel Vetter
Quoted 27 lines · Christian König

Am 21.06.21 um 16:54 schrieb Daniel Vetter:

Quoted 24 lines · Christian König
Quoted 14 lines

We actually need to wait for the moving fence after pinning the BO to make sure that the pin is completed.

---
  drivers/gpu/drm/nouveau/nouveau_prime.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/nouveau/nouveau_prime.c +0 -0
index 347488685f74..591738545eba 100644

drivers/gpu/drm/nouveau/nouveau_prime.c int nouveau_gem_prime_pin(struct drm_gem_object *obj) · line 96

Quoted 5 lines
drivers/gpu/drm/nouveau/nouveau_prime.c +1 -1
@@ -93,7 +93,13 @@ int nouveau_gem_prime_pin(struct drm_gem_object *obj)
     if (ret)
             return -EINVAL;
 
-    return 0;
+    if (nvbo->bo.moving) {

Don't we need to hold the dma_resv to read this? We can grab a reference and then unlock, but I think just unlocked wait can go boom pretty easily (since we don't hold a reference or lock so someone else can jump in and free the moving fence).

Quoted 6 lines
Quoted 2 lines

The moving fence is only modified while the BO is moved and since we have just successfully pinned it....

Yeah ... so probably correct, but really tricky. Just wrapping a ttm_bo_reserve/unreserve around the code you add should be enough and get the job done?

I think you distracted me a bit with the "it can't move", so yes there's a guarantee that no other fence can show up in ttm_bo->moving and confuse us. But it could get set to NULL because someone realized it signalled. We're not doing that systematically, but relying on fences never getting garbage-collected for correctness isn't great.

Sot the ttm_bo_reserve/unreserve is definitely needed here around this bit of code. You don't need to merge it with the reserve/unreserve in the pin function though, it's just to protect against the use-after-free.
-Daniel

Quoted 38 lines
Quoted 3 lines

But in general I agree that it would be better to avoid this. I just didn't wanted to open a bigger can of worms by changing nouveau so much.

Yeah, but I'm kinda thinking of some helpers to wait for the move fence (so that later on we can switch from having the exclusive fence to the move fence do that, maybe). And then locking checks in there would be nice.

Also avoids the case of explaining why lockless here is fine, but lockless wait for the exclusive fence in e.g. a dynami dma-buf importer is very much not fine at all. Just all around less trouble. -Daniel

Quoted 18 lines

Christian.

Quoted 14 lines

-Daniel

Quoted 12 lines
drivers/gpu/drm/nouveau/nouveau_prime.c +6 -1
+            ret = dma_fence_wait(nvbo->bo.moving, true);
+            if (ret)
+                    nouveau_bo_unpin(nvbo);
+    }
+
+    return ret;
  }
 
  void nouveau_gem_prime_unpin(struct drm_gem_object *obj)
--

2.25.1

--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#9 lore raw

Am 22.06.21 um 11:20 schrieb Daniel Vetter:

Quoted 30 lines · Daniel Vetter
Quoted 29 lines
Quoted 27 lines · Christian König

Am 21.06.21 um 16:54 schrieb Daniel Vetter:

Quoted 18 lines · Christian König
Quoted 17 lines

We actually need to wait for the moving fence after pinning the BO to make sure that the pin is completed.

---
   drivers/gpu/drm/nouveau/nouveau_prime.c | 8 +++++++-
   1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/nouveau/nouveau_prime.c +0 -0
index 347488685f74..591738545eba 100644
@@ -93,7 +93,13 @@ int nouveau_gem_prime_pin(struct drm_gem_object *obj)
      if (ret)
              return -EINVAL;
 
Quoted 6 lines

drivers/gpu/drm/nouveau/nouveau_prime.c int nouveau_gem_prime_pin(struct drm_gem_object *obj) · line 96

Quoted 2 lines
drivers/gpu/drm/nouveau/nouveau_prime.c +1 -1
-    return 0;
+    if (nvbo->bo.moving) {

Don't we need to hold the dma_resv to read this? We can grab a reference and then unlock, but I think just unlocked wait can go boom pretty easily (since we don't hold a reference or lock so someone else can jump in and free the moving fence).

The moving fence is only modified while the BO is moved and since we have just successfully pinned it....

Quoted 8 lines
Quoted 3 lines

Yeah ... so probably correct, but really tricky. Just wrapping a ttm_bo_reserve/unreserve around the code you add should be enough and get the job done?

I think you distracted me a bit with the "it can't move", so yes there's a guarantee that no other fence can show up in ttm_bo->moving and confuse us. But it could get set to NULL because someone realized it signalled. We're not doing that systematically, but relying on fences never getting garbage-collected for correctness isn't great.

Yeah, that's what I essentially meant with it would be better in general to take the lock.

Quoted 5 lines

Sot the ttm_bo_reserve/unreserve is definitely needed here around this bit of code. You don't need to merge it with the reserve/unreserve in the pin function though, it's just to protect against the use-after-free.

Ah, yes good point. That means I don't need to change the pin/unpin functions in nouveau at all.

BTW: What do you think of making dma_fence_is_signaled() and dma_fence_wait_timeout() save to passing in NULL as fence?

I think we have a lot of cases where we check "!fence || dma_fence_is_signaled(fence)" or similar.

Christian.

Quoted 37 lines

-Daniel

Quoted 33 lines
Quoted 2 lines

But in general I agree that it would be better to avoid this. I just didn't wanted to open a bigger can of worms by changing nouveau so much.

Yeah, but I'm kinda thinking of some helpers to wait for the move fence (so that later on we can switch from having the exclusive fence to the move fence do that, maybe). And then locking checks in there would be nice.

Also avoids the case of explaining why lockless here is fine, but lockless wait for the exclusive fence in e.g. a dynami dma-buf importer is very much not fine at all. Just all around less trouble. -Daniel

Quoted 16 lines

Christian.

Quoted 14 lines

-Daniel

Quoted 12 lines
drivers/gpu/drm/nouveau/nouveau_prime.c +6 -1
+            ret = dma_fence_wait(nvbo->bo.moving, true);
+            if (ret)
+                    nouveau_bo_unpin(nvbo);
+    }
+
+    return ret;
   }
 
   void nouveau_gem_prime_unpin(struct drm_gem_object *obj)
--

2.25.1

--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch