Skip to contentSkip to conversation

drm/amdgpu: fix old fence check in amdgpu_fence_emit

PATCH In review Christian König 4 participants 6 messages Mar 29, 2019 – Apr 1, 2019 5 messages not in the archive

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

PATCH 1/1 drm/amdgpu: fix old fence check in amdgpu_fence_emit

We don't hold a reference to the old fence, so it can go away any time we are waiting for it to signal.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 24 ++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +17 -7
index ee47c11e92ce..4dee2326b29c 100644
@@ -136,8 +136,9 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **f,
 {
 	struct amdgpu_device *adev = ring->adev;
 	struct amdgpu_fence *fence;
-	struct dma_fence *old, **ptr;
+	struct dma_fence __rcu **ptr;
 	uint32_t seq;
+	int r;
 
 	fence = kmem_cache_alloc(amdgpu_fence_slab, GFP_KERNEL);
 	if (fence == NULL)
@@ -153,15 +154,24 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **f,
 			       seq, flags | AMDGPU_FENCE_FLAG_INT);
 
 	ptr = &ring->fence_drv.fences[seq & ring->fence_drv.num_fences_mask];
+	if (unlikely(rcu_dereference_protected(*ptr, 1))) {
+		struct dma_fence *old;
+
+		rcu_read_lock();
+		old = dma_fence_get_rcu_safe(ptr);
+		rcu_read_unlock();
+
+		if (old) {
+			r = dma_fence_wait(old, false);
+			dma_fence_put(old);
+			if (r)
+				return r;
+		}
+	}
+
 	/* This function can't be called concurrently anyway, otherwise
 	 * emitting the fence would mess up the hardware ring buffer.
 	 */
-	old = rcu_dereference_protected(*ptr, 1);
-	if (old && !dma_fence_is_signaled(old)) {
-		DRM_INFO("rcu slot is busy\n");
-		dma_fence_wait(old, false);
-	}
-
 	rcu_assign_pointer(*ptr, dma_fence_get(&fence->base));
 
 	*f = &fence->base;
Signature · 6 lines
2.17.1

_______________________________________________
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): 20190329183306.21873-1-christian.koenig-5C7GfCeVMHo@public.gmane.org

#2 lore raw
Quoted 35 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Christian K?nig
Sent: Saturday, March 30, 2019 2:33 AM
To: [email protected]
Subject: [PATCH] drm/amdgpu: fix old fence check in amdgpu_fence_emit

We don't hold a reference to the old fence, so it can go away any time we are waiting for it to signal.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 24 ++++++++++++++++-
------
 1 file changed, 17 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +2 -1
index ee47c11e92ce..4dee2326b29c 100644
@@ -136,8 +136,9 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring,
 struct dma_fence **f,  {
 	struct amdgpu_device *adev = ring->adev;
 	struct amdgpu_fence *fence;
-	struct dma_fence *old, **ptr;
+	struct dma_fence __rcu **ptr;
 	uint32_t seq;
+	int r;
 
 	fence = kmem_cache_alloc(amdgpu_fence_slab, GFP_KERNEL);
 	if (fence == NULL)

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c int amdgpu_fence_emit(struct amdgpu_ring *ring, · line 159

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +1 -0
@@ -153,15 +154,24 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring,
 struct dma_fence **f,
 			       seq, flags | AMDGPU_FENCE_FLAG_INT);
 
 	ptr = &ring->fence_drv.fences[seq & ring-
 >fence_drv.num_fences_mask];
+	if (unlikely(rcu_dereference_protected(*ptr, 1))) {

Isn't this line redundant with dma_fence_get_rcu_safe? I think it's unnecessary. Otherwise looks ok to me.

-David

Quoted 25 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +14 -6
+		struct dma_fence *old;
+
+		rcu_read_lock();
+		old = dma_fence_get_rcu_safe(ptr);
+		rcu_read_unlock();
+
+		if (old) {
+			r = dma_fence_wait(old, false);
+			dma_fence_put(old);
+			if (r)
+				return r;
+		}
+	}
+
 	/* This function can't be called concurrently anyway, otherwise
 	 * emitting the fence would mess up the hardware ring buffer.
 	 */
-	old = rcu_dereference_protected(*ptr, 1);
-	if (old && !dma_fence_is_signaled(old)) {
-		DRM_INFO("rcu slot is busy\n");
-		dma_fence_wait(old, false);
-	}
-
 	rcu_assign_pointer(*ptr, dma_fence_get(&fence->base));
Quoted 8 lines

*f = &fence->base;
--
2.17.1

_______________________________________________
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

A message that is not in the archive (sent off-list or not yet received): MN2PR12MB2910D258E4E97FDD0C4D8980B4550-rweVpJHSKTr1t3MqfsnKMAdYzm3356FpvxpqHgZTriW3zl9H0oFU5g@public.gmane.org

#4 lore raw

Am 01.04.19 um 04:54 schrieb Zhou, David(ChunMing):

Quoted 37 lines
Quoted 37 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Christian K?nig
Sent: Saturday, March 30, 2019 2:33 AM
To: [email protected]
Subject: [PATCH] drm/amdgpu: fix old fence check in amdgpu_fence_emit

We don't hold a reference to the old fence, so it can go away any time we are waiting for it to signal.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 24 ++++++++++++++++-
------
  1 file changed, 17 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +2 -1
index ee47c11e92ce..4dee2326b29c 100644
@@ -136,8 +136,9 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring,
 struct dma_fence **f,  {
  	struct amdgpu_device *adev = ring->adev;
  	struct amdgpu_fence *fence;
-	struct dma_fence *old, **ptr;
+	struct dma_fence __rcu **ptr;
  	uint32_t seq;
+	int r;
 
  	fence = kmem_cache_alloc(amdgpu_fence_slab, GFP_KERNEL);
  	if (fence == NULL)
@@ -153,15 +154,24 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring,
 struct dma_fence **f,
  			       seq, flags | AMDGPU_FENCE_FLAG_INT);
 
Quoted 5 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c int amdgpu_fence_emit(struct amdgpu_ring *ring, · line 159

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +1 -0
  	ptr = &ring->fence_drv.fences[seq & ring-
 > fence_drv.num_fences_mask];
+	if (unlikely(rcu_dereference_protected(*ptr, 1))) {

Isn't this line redundant with dma_fence_get_rcu_safe? I think it's unnecessary. Otherwise looks ok to me.

The key point is lock()+dma_fence_get_rcu_safe(ptr)+unlock() is rather expensive for something which is really unlikely.

So we check here if we already see the variable as NULL and if that is true, then we can just skip the whole expensive dance.

Christian.

Quoted 27 lines

-David

Quoted 25 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +14 -6
+		struct dma_fence *old;
+
+		rcu_read_lock();
+		old = dma_fence_get_rcu_safe(ptr);
+		rcu_read_unlock();
+
+		if (old) {
+			r = dma_fence_wait(old, false);
+			dma_fence_put(old);
+			if (r)
+				return r;
+		}
+	}
+
  	/* This function can't be called concurrently anyway, otherwise
  	 * emitting the fence would mess up the hardware ring buffer.
  	 */
-	old = rcu_dereference_protected(*ptr, 1);
-	if (old && !dma_fence_is_signaled(old)) {
-		DRM_INFO("rcu slot is busy\n");
-		dma_fence_wait(old, false);
-	}
-
  	rcu_assign_pointer(*ptr, dma_fence_get(&fence->base));
Quoted 8 lines
Quoted 8 lines

*f = &fence->base;
--
2.17.1

_______________________________________________
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

A message that is not in the archive (sent off-list or not yet received): 1a87cbdd-1797-3e25-4588-a1c265d5d4c3-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#6 lore raw
Quoted 46 lines · Christian König

Am 01.04.19 um 04:54 schrieb Zhou, David(ChunMing):

Quoted 38 lines
Quoted 38 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Christian K?nig
Sent: Saturday, March 30, 2019 2:33 AM
To: [email protected]
Subject: [PATCH] drm/amdgpu: fix old fence check in amdgpu_fence_emit

We don't hold a reference to the old fence, so it can go away any time we are
waiting for it to signal.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 24 ++++++++++++++++-
------

  1 file changed, 17 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +2 -1
index ee47c11e92ce..4dee2326b29c 100644
@@ -136,8 +136,9 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring,
 struct dma_fence **f,  {
       struct amdgpu_device *adev = ring->adev;
       struct amdgpu_fence *fence;
-    struct dma_fence *old, **ptr;
+    struct dma_fence __rcu **ptr;
       uint32_t seq;
+    int r;
 
       fence = kmem_cache_alloc(amdgpu_fence_slab, GFP_KERNEL);
       if (fence == NULL)
@@ -153,15 +154,24 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring,
 struct dma_fence **f,
                      seq, flags | AMDGPU_FENCE_FLAG_INT);
 
Quoted 6 lines
Quoted 3 lines

      ptr = &ring->fence_drv.fences[seq & ring-

Quoted 1 line

fence_drv.num_fences_mask];

+    if (unlikely(rcu_dereference_protected(*ptr, 1))) {

Isn't this line redundant with dma_fence_get_rcu_safe? I think it's unnecessary.
Otherwise looks ok to me.

Quoted 5 lines

The key point is lock()+dma_fence_get_rcu_safe(ptr)+unlock() is rather expensive for something which is really unlikely.

So we check here if we already see the variable as NULL and if that is true, then we can just skip the whole expensive dance.

but that is most unlikely case, isn't it?  That ptr is NULL seems only when before first fence emitted.

-David

Quoted 30 lines

Christian.

Quoted 27 lines

-David

Quoted 25 lines

+        struct dma_fence *old;

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +13 -0
+
+        rcu_read_lock();
+        old = dma_fence_get_rcu_safe(ptr);
+        rcu_read_unlock();
+
+        if (old) {
+            r = dma_fence_wait(old, false);
+            dma_fence_put(old);
+            if (r)
+                return r;
+        }
+    }
+

      /* This function can't be called concurrently anyway, otherwise
       * emitting the fence would mess up the hardware ring buffer. */
-    old = rcu_dereference_protected(*ptr, 1);
-    if (old && !dma_fence_is_signaled(old)) {
-        DRM_INFO("rcu slot is busy\n");
-        dma_fence_wait(old, false);
-    }
-
      rcu_assign_pointer(*ptr, dma_fence_get(&fence->base));

Quoted 9 lines
Quoted 8 lines
Quoted 8 lines

      *f = &fence->base;

Signature · 6 lines
2.17.1

_______________________________________________
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

A message that is not in the archive (sent off-list or not yet received): 1bc93ee8-748b-9935-6749-d7f4f6c2ebe6-5C7GfCeVMHo@public.gmane.org

#8 lore raw

Am 01.04.19 um 16:04 schrieb Zhou, David(ChunMing):

Quoted 45 lines · Christian König
Quoted 44 lines

Am 01.04.19 um 04:54 schrieb Zhou, David(ChunMing):

Quoted 43 lines
Quoted 34 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Christian K?nig
Sent: Saturday, March 30, 2019 2:33 AM
To: [email protected]
Subject: [PATCH] drm/amdgpu: fix old fence check in amdgpu_fence_emit

We don't hold a reference to the old fence, so it can go away any time we are
waiting for it to signal.

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 24 ++++++++++++++++-
------
   1 file changed, 17 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +2 -1
index ee47c11e92ce..4dee2326b29c 100644
@@ -136,8 +136,9 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring,
 struct dma_fence **f,  {
       struct amdgpu_device *adev = ring->adev;
       struct amdgpu_fence *fence;
-    struct dma_fence *old, **ptr;
+    struct dma_fence __rcu **ptr;
       uint32_t seq;
+    int r;
 
       fence = kmem_cache_alloc(amdgpu_fence_slab, GFP_KERNEL);
       if (fence == NULL)

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c int amdgpu_fence_emit(struct amdgpu_ring *ring, · line 159

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +1 -0
@@ -153,15 +154,24 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring,
 struct dma_fence **f,
                      seq, flags | AMDGPU_FENCE_FLAG_INT);
 
       ptr = &ring->fence_drv.fences[seq & ring-
 > fence_drv.num_fences_mask];
+    if (unlikely(rcu_dereference_protected(*ptr, 1))) {

Isn't this line redundant with dma_fence_get_rcu_safe? I think it's unnecessary.
Otherwise looks ok to me.

Quoted 7 lines
Quoted 5 lines

The key point is lock()+dma_fence_get_rcu_safe(ptr)+unlock() is rather expensive for something which is really unlikely.

So we check here if we already see the variable as NULL and if that is true, then we can just skip the whole expensive dance.

but that is most unlikely case, isn't it?  That ptr is NULL seems only when before first fence emitted.

No, the pointer is set to NULL when the fence is processed. See amdgpu_fence_process.

Christian.

Quoted 32 lines

-David

Quoted 28 lines

Christian.

Quoted 26 lines

-David

Quoted 25 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +14 -6
+        struct dma_fence *old;
+
+        rcu_read_lock();
+        old = dma_fence_get_rcu_safe(ptr);
+        rcu_read_unlock();
+
+        if (old) {
+            r = dma_fence_wait(old, false);
+            dma_fence_put(old);
+            if (r)
+                return r;
+        }
+    }
+
       /* This function can't be called concurrently anyway, otherwise
        * emitting the fence would mess up the hardware ring buffer.
        */
-    old = rcu_dereference_protected(*ptr, 1);
-    if (old && !dma_fence_is_signaled(old)) {
-        DRM_INFO("rcu slot is busy\n");
-        dma_fence_wait(old, false);
-    }
-
       rcu_assign_pointer(*ptr, dma_fence_get(&fence->base));
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

      *f = &fence->base;

Signature · 6 lines
2.17.1

_______________________________________________
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

A message that is not in the archive (sent off-list or not yet received): c59d5cd5-6062-874e-f228-1577cc0807aa-5C7GfCeVMHo@public.gmane.org

#10 lore raw
Quoted 49 lines · Christian

Am 01.04.19 um 16:04 schrieb Zhou, David(ChunMing):

Quoted 48 lines · Christian König
Quoted 47 lines

Am 01.04.19 um 04:54 schrieb Zhou, David(ChunMing):

Quoted 37 lines
Quoted 37 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Christian K?nig
Sent: Saturday, March 30, 2019 2:33 AM
To: [email protected]
Subject: [PATCH] drm/amdgpu: fix old fence check in amdgpu_fence_emit

We don't hold a reference to the old fence, so it can go away any time we are
waiting for it to signal.

---
    drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 24 ++++++++++++++++-
------
    1 file changed, 17 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +2 -1
index ee47c11e92ce..4dee2326b29c 100644
@@ -136,8 +136,9 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring,
 struct dma_fence **f,  {
        struct amdgpu_device *adev = ring->adev;
        struct amdgpu_fence *fence;
-    struct dma_fence *old, **ptr;
+    struct dma_fence __rcu **ptr;
        uint32_t seq;
+    int r;
 
        fence = kmem_cache_alloc(amdgpu_fence_slab, GFP_KERNEL);
        if (fence == NULL)
@@ -153,15 +154,24 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring,
 struct dma_fence **f,
                       seq, flags | AMDGPU_FENCE_FLAG_INT);
 
Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c int amdgpu_fence_emit(struct amdgpu_ring *ring, · line 159

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +1 -0
        ptr = &ring->fence_drv.fences[seq & ring-
 > fence_drv.num_fences_mask];
+    if (unlikely(rcu_dereference_protected(*ptr, 1))) {

Isn't this line redundant with dma_fence_get_rcu_safe? I think it's unnecessary.
Otherwise looks ok to me.

The key point is lock()+dma_fence_get_rcu_safe(ptr)+unlock() is rather expensive for something which is really unlikely.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

So we check here if we already see the variable as NULL and if that is true, then we can just skip the whole expensive dance.

but that is most unlikely case, isn't it?  That ptr is NULL seems only when before first fence emitted.

No, the pointer is set to NULL when the fence is processed. See amdgpu_fence_process.

Yeah, I see that RCU__INIT again for every singal fence.

Sorry for noise, pathc is Reviewed-by: Chunming Zhou <[email protected]>

-David

Quoted 34 lines

Christian.

Quoted 31 lines

-David

Quoted 28 lines

Christian.

Quoted 26 lines

-David

Quoted 25 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +14 -6
+        struct dma_fence *old;
+
+        rcu_read_lock();
+        old = dma_fence_get_rcu_safe(ptr);
+        rcu_read_unlock();
+
+        if (old) {
+            r = dma_fence_wait(old, false);
+            dma_fence_put(old);
+            if (r)
+                return r;
+        }
+    }
+
        /* This function can't be called concurrently anyway, otherwise
         * emitting the fence would mess up the hardware ring buffer.
         */
-    old = rcu_dereference_protected(*ptr, 1);
-    if (old && !dma_fence_is_signaled(old)) {
-        DRM_INFO("rcu slot is busy\n");
-        dma_fence_wait(old, false);
-    }
-
        rcu_assign_pointer(*ptr, dma_fence_get(&fence->base));
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

      *f = &fence->base;

Signature · 6 lines
2.17.1

_______________________________________________
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