Skip to contentSkip to conversation

drm/amdgpu:fix race condition bug

PATCH NAK Monk Liu 2 participants 7 messages Apr 7, 2017 6 messages not in the archive

Subsystems (2)
lore
Share & tools
lore

Monk Liu

author
#0 lore raw

PATCH 1/1 drm/amdgpu:fix race condition bug

Change-Id Ib7a03f3cf5594deeb4ad333cc59b47a6bddfd1ad
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +3 -0
index 6d86eae..b8c11fe 100644
@@ -277,6 +277,9 @@ struct fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
 
 	spin_lock(&ctx->ring_lock);
 
+	if (!seq)
+		seq = ctx->rings[ring->idx].sequence - 1;
+
 	if (seq >= cring->sequence) {
 		spin_unlock(&ctx->ring_lock);
 		return ERR_PTR(-EINVAL);
Signature · 6 lines
2.7.4

_______________________________________________
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): 1491562320-10812-1-git-send-email-Monk.Liu-5C7GfCeVMHo@public.gmane.org

#2 lore raw

Am 07.04.2017 um 12:52 schrieb Monk Liu:

Quoted 2 lines
Change-Id Ib7a03f3cf5594deeb4ad333cc59b47a6bddfd1ad

NAK, that is a not backward compatible change to the IOCTL interface.

And BTW what's the background of it?

Christian.

Quoted 9 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 3 +++
  1 file changed, 3 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -0
index 6d86eae..b8c11fe 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c struct fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx, · line 285

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +3 -0
@@ -277,6 +277,9 @@ struct fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
 
  	spin_lock(&ctx->ring_lock);
 
+	if (!seq)
+		seq = ctx->rings[ring->idx].sequence - 1;
+
  	if (seq >= cring->sequence) {
  		spin_unlock(&ctx->ring_lock);
  		return ERR_PTR(-EINVAL);

_______________________________________________
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): 0d66797c-de99-75e4-bea3-c8dc725940d0-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org

#4 lore raw

Am 07.04.2017 um 13:26 schrieb Christian König:

Quoted 5 lines

Am 07.04.2017 um 12:52 schrieb Monk Liu:

Quoted 2 lines
Change-Id Ib7a03f3cf5594deeb4ad333cc59b47a6bddfd1ad

NAK, that is a not backward compatible change to the IOCTL interface.

To make it clear, something like "if(seq == ~0L)" should work.

Christian.

Quoted 20 lines

And BTW what's the background of it?

Christian.

Quoted 15 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 3 +++
  1 file changed, 3 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +2 -0
index 6d86eae..b8c11fe 100644
@@ -277,6 +277,9 @@ struct fence *amdgpu_ctx_get_fence(struct 
 amdgpu_ctx *ctx,
        spin_lock(&ctx->ring_lock);
  +    if (!seq)
+        seq = ctx->rings[ring->idx].sequence - 1;
+
      if (seq >= cring->sequence) {
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c struct fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx, · line 285

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -0
          spin_unlock(&ctx->ring_lock);
          return ERR_PTR(-EINVAL);

_______________________________________________
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): 57837990-fd0c-a74f-6844-ea1828332ac7-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org

author
#6 lore raw

IOCTL will pass a seq value, or 0
And 0 means it want to let KMD choose the latest seq value

So this patch is needed for the caller which input 0 as seq value

I found some code invoke ctx_get_fence() like:

Seq = parm.in.seq ? seq: ctx->rings[ring->idx].sequence -1 ; Fence = amdgpu_ctx_get_fence(ctx, seq);

BTW: above code is from hybrid kernel branch, not in upstream branch

You can see above logic actually wrong, because the consistence of the sequence is not guaranteed. Caller should not access sequence directly,
And sequence should be accessed during protection of the ring_lock

Any idea ?

-----邮件原件-----
发件人: Christian König [mailto:[email protected]] 发送时间: 2017年4月7日 19:28
收件人: Liu, Monk <[email protected]>; [email protected] 主题: Re: [PATCH] drm/amdgpu:fix race condition bug

Am 07.04.2017 um 13:26 schrieb Christian König:

Quoted 5 lines

Am 07.04.2017 um 12:52 schrieb Monk Liu:

Quoted 2 lines
Change-Id Ib7a03f3cf5594deeb4ad333cc59b47a6bddfd1ad

NAK, that is a not backward compatible change to the IOCTL interface.

To make it clear, something like "if(seq == ~0L)" should work.

Christian.

Quoted 21 lines

And BTW what's the background of it?

Christian.

Quoted 16 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 3 +++
  1 file changed, 3 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +2 -0
index 6d86eae..b8c11fe 100644
@@ -277,6 +277,9 @@ struct fence *amdgpu_ctx_get_fence(struct 
 amdgpu_ctx *ctx,
        spin_lock(&ctx->ring_lock);
  +    if (!seq)
+        seq = ctx->rings[ring->idx].sequence - 1;
+
      if (seq >= cring->sequence) {
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c struct fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx, · line 285

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -0
          spin_unlock(&ctx->ring_lock);
          return ERR_PTR(-EINVAL);

_______________________________________________
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): MWHPR12MB1615122AFAF4819617344E0E840C0-Gy0DoCVfaSVb47PzedaHsgdYzm3356FpvxpqHgZTriW3zl9H0oFU5g@public.gmane.org

#8 lore raw
Quoted 4 lines

IOCTL will pass a seq value, or 0
And 0 means it want to let KMD choose the latest seq value

So this patch is needed for the caller which input 0 as seq value

Yeah, I understand that. The problem is we already abused seq value 0 to be the "always" signaled fence number.

So your patch is accidentally redefining this. But we could use something like ~0ull instead.

Quoted 8 lines

Seq = parm.in.seq ? seq: ctx->rings[ring->idx].sequence -1 ; Fence = amdgpu_ctx_get_fence(ctx, seq);

BTW: above code is from hybrid kernel branch, not in upstream branch

You can see above logic actually wrong, because the consistence of the sequence is not guaranteed. Caller should not access sequence directly,
And sequence should be accessed during protection of the ring_lock

Yeah, I see that this is actually a race.

Please use ~0ull to signal that we want to wait for the latest fence instead of 0.

With that done the change looks fine to me.

Christian.

Am 07.04.2017 um 15:32 schrieb Liu, Monk:

Quoted 63 lines

IOCTL will pass a seq value, or 0
And 0 means it want to let KMD choose the latest seq value

So this patch is needed for the caller which input 0 as seq value

I found some code invoke ctx_get_fence() like:

Seq = parm.in.seq ? seq: ctx->rings[ring->idx].sequence -1 ; Fence = amdgpu_ctx_get_fence(ctx, seq);

BTW: above code is from hybrid kernel branch, not in upstream branch

You can see above logic actually wrong, because the consistence of the sequence is not guaranteed. Caller should not access sequence directly,
And sequence should be accessed during protection of the ring_lock

Any idea ?

-----邮件原件-----
发件人: Christian König [mailto:[email protected]] 发送时间: 2017年4月7日 19:28
收件人: Liu, Monk <[email protected]>; [email protected] 主题: Re: [PATCH] drm/amdgpu:fix race condition bug

Am 07.04.2017 um 13:26 schrieb Christian König:

Quoted 4 lines

Am 07.04.2017 um 12:52 schrieb Monk Liu:

Quoted 2 lines
Change-Id Ib7a03f3cf5594deeb4ad333cc59b47a6bddfd1ad

NAK, that is a not backward compatible change to the IOCTL interface.

To make it clear, something like "if(seq == ~0L)" should work.

Christian.

Quoted 19 lines

And BTW what's the background of it?

Christian.

Quoted 15 lines
---
   drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 3 +++
   1 file changed, 3 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +2 -0
index 6d86eae..b8c11fe 100644
@@ -277,6 +277,9 @@ struct fence *amdgpu_ctx_get_fence(struct
 amdgpu_ctx *ctx,
         spin_lock(&ctx->ring_lock);
   +    if (!seq)
+        seq = ctx->rings[ring->idx].sequence - 1;
+
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c struct fence *amdgpu_ctx_get_fence(struct · line 284

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -0
       if (seq >= cring->sequence) {
           spin_unlock(&ctx->ring_lock);
           return ERR_PTR(-EINVAL);

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

Quoted 4 lines

_______________________________________________
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): 5e3ba61a-2bb6-63f0-e4cb-79332d97d621-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org

author
#10 lore raw

Christian,

UMD already use 0 to tell KMD it want to use latest sequence value ...
And for KMD, we always start sequence value from 1, so I think this patch doesn't break anything

But if you want to use ~0 presenting latest sequence, that will demand many changes in UMD side ...

BR Monk

-----邮件原件-----
发件人: Christian König [mailto:[email protected]] 发送时间: 2017年4月7日 21:42
收件人: Liu, Monk <[email protected]>; [email protected] 主题: Re: 答复: [PATCH] drm/amdgpu:fix race condition bug

Quoted 4 lines

IOCTL will pass a seq value, or 0
And 0 means it want to let KMD choose the latest seq value

So this patch is needed for the caller which input 0 as seq value

Yeah, I understand that. The problem is we already abused seq value 0 to be the "always" signaled fence number.

So your patch is accidentally redefining this. But we could use something like ~0ull instead.

Quoted 8 lines

Seq = parm.in.seq ? seq: ctx->rings[ring->idx].sequence -1 ; Fence = amdgpu_ctx_get_fence(ctx, seq);

BTW: above code is from hybrid kernel branch, not in upstream branch

You can see above logic actually wrong, because the consistence of the sequence is not guaranteed. Caller should not access sequence directly, And sequence should be
accessed during protection of the ring_lock

Yeah, I see that this is actually a race.

Please use ~0ull to signal that we want to wait for the latest fence instead of 0.

With that done the change looks fine to me.

Christian.

Am 07.04.2017 um 15:32 schrieb Liu, Monk:

Quoted 63 lines

IOCTL will pass a seq value, or 0
And 0 means it want to let KMD choose the latest seq value

So this patch is needed for the caller which input 0 as seq value

I found some code invoke ctx_get_fence() like:

Seq = parm.in.seq ? seq: ctx->rings[ring->idx].sequence -1 ; Fence = amdgpu_ctx_get_fence(ctx, seq);

BTW: above code is from hybrid kernel branch, not in upstream branch

You can see above logic actually wrong, because the consistence of the sequence is not guaranteed. Caller should not access sequence directly, And sequence should be
accessed during protection of the ring_lock

Any idea ?

-----邮件原件-----
发件人: Christian König [mailto:[email protected]] 发送时间: 2017年4月7日 19:28
收件人: Liu, Monk <[email protected]>; [email protected] 主题: Re: [PATCH] drm/amdgpu:fix race condition bug

Am 07.04.2017 um 13:26 schrieb Christian König:

Quoted 4 lines

Am 07.04.2017 um 12:52 schrieb Monk Liu:

Quoted 2 lines
Change-Id Ib7a03f3cf5594deeb4ad333cc59b47a6bddfd1ad

NAK, that is a not backward compatible change to the IOCTL interface.

To make it clear, something like "if(seq == ~0L)" should work.

Christian.

Quoted 19 lines

And BTW what's the background of it?

Christian.

Quoted 15 lines
---
   drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 3 +++
   1 file changed, 3 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +2 -0
index 6d86eae..b8c11fe 100644
@@ -277,6 +277,9 @@ struct fence *amdgpu_ctx_get_fence(struct 
 amdgpu_ctx *ctx,
         spin_lock(&ctx->ring_lock);
   +    if (!seq)
+        seq = ctx->rings[ring->idx].sequence - 1;
+
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c struct fence *amdgpu_ctx_get_fence(struct · line 284

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -0
       if (seq >= cring->sequence) {
           spin_unlock(&ctx->ring_lock);
           return ERR_PTR(-EINVAL);

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

Quoted 4 lines

_______________________________________________
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): MWHPR12MB1615B6148A25B08BEADCE4B3840C0-Gy0DoCVfaSVb47PzedaHsgdYzm3356FpvxpqHgZTriW3zl9H0oFU5g@public.gmane.org

#12 lore raw
Quoted 1 line

UMD already use 0 to tell KMD it want to use latest sequence value ...

Well that was a bad idea. We already used 0 for the always signaled case IIRC.

Quoted 1 line

But if you want to use ~0 presenting latest sequence, that will demand many changes in UMD side ...

Please do so, using 0 clearly wasn't a good idea. And we should also add a define for this in amdgpu_drm.h.

We should probably send all interfaces to the public list for review, even when we aren't going to push them upstream.

Regards,
Christian.

Am 07.04.2017 um 15:59 schrieb Liu, Monk:

Quoted 106 lines

Christian,

UMD already use 0 to tell KMD it want to use latest sequence value ...
And for KMD, we always start sequence value from 1, so I think this patch doesn't break anything

But if you want to use ~0 presenting latest sequence, that will demand many changes in UMD side ...

BR Monk

-----邮件原件-----
发件人: Christian König [mailto:[email protected]] 发送时间: 2017年4月7日 21:42
收件人: Liu, Monk <[email protected]>; [email protected] 主题: Re: 答复: [PATCH] drm/amdgpu:fix race condition bug

Quoted 4 lines

IOCTL will pass a seq value, or 0
And 0 means it want to let KMD choose the latest seq value

So this patch is needed for the caller which input 0 as seq value

Yeah, I understand that. The problem is we already abused seq value 0 to be the "always" signaled fence number.

So your patch is accidentally redefining this. But we could use something like ~0ull instead.

Quoted 8 lines

Seq = parm.in.seq ? seq: ctx->rings[ring->idx].sequence -1 ; Fence = amdgpu_ctx_get_fence(ctx, seq);

BTW: above code is from hybrid kernel branch, not in upstream branch

You can see above logic actually wrong, because the consistence of the sequence is not guaranteed. Caller should not access sequence directly, And sequence should be
accessed during protection of the ring_lock

Yeah, I see that this is actually a race.

Please use ~0ull to signal that we want to wait for the latest fence instead of 0.

With that done the change looks fine to me.

Christian.

Am 07.04.2017 um 15:32 schrieb Liu, Monk:

Quoted 61 lines

IOCTL will pass a seq value, or 0
And 0 means it want to let KMD choose the latest seq value

So this patch is needed for the caller which input 0 as seq value

I found some code invoke ctx_get_fence() like:

Seq = parm.in.seq ? seq: ctx->rings[ring->idx].sequence -1 ; Fence = amdgpu_ctx_get_fence(ctx, seq);

BTW: above code is from hybrid kernel branch, not in upstream branch

You can see above logic actually wrong, because the consistence of the sequence is not guaranteed. Caller should not access sequence directly, And sequence should be
accessed during protection of the ring_lock

Any idea ?

-----邮件原件-----
发件人: Christian König [mailto:[email protected]] 发送时间: 2017年4月7日 19:28
收件人: Liu, Monk <[email protected]>; [email protected] 主题: Re: [PATCH] drm/amdgpu:fix race condition bug

Am 07.04.2017 um 13:26 schrieb Christian König:

Quoted 4 lines

Am 07.04.2017 um 12:52 schrieb Monk Liu:

Quoted 2 lines
Change-Id Ib7a03f3cf5594deeb4ad333cc59b47a6bddfd1ad

NAK, that is a not backward compatible change to the IOCTL interface.

To make it clear, something like "if(seq == ~0L)" should work.

Christian.

Quoted 18 lines

And BTW what's the background of it?

Christian.

Quoted 14 lines
---
    drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 3 +++
    1 file changed, 3 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -0
index 6d86eae..b8c11fe 100644
@@ -277,6 +277,9 @@ struct fence *amdgpu_ctx_get_fence(struct
 amdgpu_ctx *ctx,
          spin_lock(&ctx->ring_lock);
    +    if (!seq)
+        seq = ctx->rings[ring->idx].sequence - 1;
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c struct fence *amdgpu_ctx_get_fence(struct · line 284

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -0
+
        if (seq >= cring->sequence) {
            spin_unlock(&ctx->ring_lock);
            return ERR_PTR(-EINVAL);

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

Quoted 4 lines

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

Quoted 4 lines

_______________________________________________
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