drm/amdgpu: Fix pipelining jobs with timeline syncobj dependencies
Subsystems (2)
PATCH 1/1 drm/amdgpu: Fix pipelining jobs with timeline syncobj dependencies
drm_syncobj_find_fence returns fence chain for timeline syncobjs. Scheduler expects normal fences as job dependencies to be able to determine whether the fences come from the same entity or sched and skip waiting on them.
With fence chain as job dependency, the fence will always be waited on forcing CPU round-trip before starting the job.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +8 -2
@@ -29,6 +29,7 @@#include <linux/pagemap.h> #include <linux/sync_file.h> #include <linux/dma-buf.h> +#include <linux/dma-fence-unwrap.h> #include <drm/amdgpu_drm.h> #include <drm/drm_syncobj.h>
@@ -450,7 +451,8 @@ static int amdgpu_syncobj_lookup_and_add(struct amdgpu_cs_parser *p, uint32_t handle, u64 point, u64 flags) { - struct dma_fence *fence; + struct dma_fence *fence, *f; +struct dma_fence_unwrap iter;int r; r =drm_syncobj_find_fence (p->filp, handle, point, flags, &fence);
@@ -460,7 +462,11 @@ static int amdgpu_syncobj_lookup_and_add(struct amdgpu_cs_parser *p,return r; } - r =amdgpu_sync_fence (&p->sync, fence,GFP_KERNEL ); +dma_fence_unwrap_for_each (f, &iter, fence) { +if (!r) + r =amdgpu_sync_fence (&p->sync, f,GFP_KERNEL ); + } +dma_fence_put (fence);return r; }
Signature · 1 line
2.43.0
Hi,
Quoted 6 lines · David Rosca
drm_syncobj_find_fencereturns fence chain for timeline syncobjs. Scheduler expects normal fences as job dependencies to be able to determine whether the fences come from the same entity or sched and skip waiting on them.
With fence chain as job dependency, the fence will always be waited on forcing CPU round-trip before starting the job.
Interesting! I was sending patches to fix this differently last year or so, by making the scheduler use dma_fence_array for tracking dependencies and relying on dma_fence_unwrap_merge to unwrap, coalesce contexts and only keep the latest fence for each. But I did not have a good story to show for which use cases it helped. So I am curious if you could share which scenario you found gets an improvement from your patch?
Regards,
Tvrtko
Quoted 40 lines
Signed-off-by David Rosca <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +8
-2@@ -29,6 +29,7 @@#include <linux/pagemap.h> #include <linux/sync_file.h> #include <linux/dma-buf.h> +#include <linux/dma-fence-unwrap.h> #include <drm/amdgpu_drm.h> #include <drm/drm_syncobj.h> @@ -450,7 +451,8 @@ static int amdgpu_syncobj_lookup_and_add(struct amdgpu_cs_parser *p, uint32_t handle, u64 point, u64 flags) { - struct dma_fence *fence; + struct dma_fence *fence, *f; +struct dma_fence_unwrap iter;int r; r =drm_syncobj_find_fence (p->filp, handle, point, flags, &fence);@@ -460,7 +462,11 @@ static int amdgpu_syncobj_lookup_and_add(struct amdgpu_cs_parser *p,return r; } - r =amdgpu_sync_fence (&p->sync, fence,GFP_KERNEL ); +dma_fence_unwrap_for_each (f, &iter, fence) { +if (!r) + r =amdgpu_sync_fence (&p->sync, f,GFP_KERNEL ); + } +dma_fence_put (fence);return r; }
Hi,
Quoted 4 lines · Tvrtko Ursulin
Hi,
On 17/09/2025 10:59, David Rosca wrote:
Quoted 8 lines
Quoted 6 lines
drm_syncobj_find_fencereturns fence chain for timeline syncobjs. Scheduler expects normal fences as job dependencies to be able to determine whether the fences come from the same entity or sched and skip waiting on them.
With fence chain as job dependency, the fence will always be waited on forcing CPU round-trip before starting the job.Interesting! I was sending patches to fix this differently last year or so, by making the scheduler use
dma_fence_arrayfor tracking dependencies and relying ondma_fence_unwrap_mergeto unwrap, coalesce contexts and only keep the latest fence for each. But I did not have a good story to show for which use cases it helped. So I am curious if you could share which scenario you found gets an improvement from your patch?
The scenario I am trying to fix is very simple to reproduce with Vulkan when using timeline semaphore to sync submissions on the same queue (eg. each submit waiting on value signaled by previous submit). I have noticed this issue with FFmpeg Vulkan video code, but it will happen with any Vulkan app using this pattern.
Regards,
David
Quoted 45 lines
Regards,
Tvrtko
Quoted 39 lines
Signed-off-by David Rosca <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c| 10 ++++++++--1 file changed, 8 insertions(+), 2 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +8
-1@@ -29,6 +29,7 @@ #include <linux/pagemap.h> #include <linux/sync_file.h> #include <linux/dma-buf.h> +#include <linux/dma-fence-unwrap.h> #include <drm/amdgpu_drm.h> #include <drm/drm_syncobj.h>@@ -450,7 +451,8 @@ static int amdgpu_syncobj_lookup_and_add(structamdgpu_cs_parser *p, uint32_t handle, u64 point, u64 flags) { - struct dma_fence *fence; + struct dma_fence *fence, *f; + struct dma_fence_unwrap iter; int r; r = drm_syncobj_find_fence(p->filp, handle, point, flags, &fence);@@ -460,7 +462,11 @@ static int amdgpu_syncobj_lookup_and_add(structamdgpu_cs_parser *p, return r; } - r = amdgpu_sync_fence(&p->sync, fence, GFP_KERNEL); + dma_fence_unwrap_for_each(f, &iter, fence) { + if (!r) + r = amdgpu_sync_fence(&p->sync, f, GFP_KERNEL); + } + dma_fence_put(fence); return r;}
Tvrtko Ursulin
Reviewed-byQuoted 14 lines · David Rosca
Hi,
Quoted 11 lines · Tvrtko Ursulin
Hi,
Quoted 6 lines · David Rosca
drm_syncobj_find_fencereturns fence chain for timeline syncobjs. Scheduler expects normal fences as job dependencies to be able to determine whether the fences come from the same entity or sched and skip waiting on them.
With fence chain as job dependency, the fence will always be waited on forcing CPU round-trip before starting the job.
Quoted 3 lines
Quoted 1 line
Interesting! I was sending patches to fix this differently last year or so, by making the scheduler use
dma_fence_arrayfor tracking dependencies and relying ondma_fence_unwrap_mergeto unwrap, coalesce contexts and only keep the latest fence for each. But I did not have a good story to show for which use cases it helped. So I am curious if you could share which scenario you found gets an improvement from your patch?The scenario I am trying to fix is very simple to reproduce with Vulkan when using timeline semaphore to sync submissions on the same queue (eg. each submit waiting on value signaled by previous submit). I have noticed this issue with FFmpeg Vulkan video code, but it will happen with any Vulkan app using this pattern.
Still out of curiosity, is the performance loss from the CPU round-trip something you are able to measure?
Btw your patch is I think fine, so:
But you will probably need Christian to ack it.
Regards,
Tvrtko
Quoted 40 lines
Quoted 40 lines
Quoted 39 lines
Signed-off-by David Rosca <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c| 10 ++++++++--1 file changed, 8 insertions(+), 2 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +8
-1@@ -29,6 +29,7 @@ #include <linux/pagemap.h> #include <linux/sync_file.h> #include <linux/dma-buf.h> +#include <linux/dma-fence-unwrap.h> #include <drm/amdgpu_drm.h> #include <drm/drm_syncobj.h>@@ -450,7 +451,8 @@ static int amdgpu_syncobj_lookup_and_add(structamdgpu_cs_parser *p, uint32_t handle, u64 point, u64 flags) { - struct dma_fence *fence; + struct dma_fence *fence, *f; + struct dma_fence_unwrap iter; int r; r = drm_syncobj_find_fence(p->filp, handle, point, flags, &fence);@@ -460,7 +462,11 @@ static int amdgpu_syncobj_lookup_and_add(structamdgpu_cs_parser *p, return r; } - r = amdgpu_sync_fence(&p->sync, fence, GFP_KERNEL); + dma_fence_unwrap_for_each(f, &iter, fence) { + if (!r) + r = amdgpu_sync_fence(&p->sync, f, GFP_KERNEL); + } + dma_fence_put(fence); return r;}
Quoted 16 lines · Tvrtko Ursulin
Quoted 14 lines · David Rosca
Hi,
Quoted 11 lines · Tvrtko Ursulin
Hi,
Quoted 6 lines · David Rosca
drm_syncobj_find_fencereturns fence chain for timeline syncobjs. Scheduler expects normal fences as job dependencies to be able to determine whether the fences come from the same entity or sched and skip waiting on them.
With fence chain as job dependency, the fence will always be waited on forcing CPU round-trip before starting the job.
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Interesting! I was sending patches to fix this differently last year or so, by making the scheduler use
dma_fence_arrayfor tracking dependencies and relying ondma_fence_unwrap_mergeto unwrap, coalesce contexts and only keep the latest fence for each. But I did not have a good story to show for which use cases it helped. So I am curious if you could share which scenario you found gets an improvement from your patch?The scenario I am trying to fix is very simple to reproduce with Vulkan when using timeline semaphore to sync submissions on the same queue (eg. each submit waiting on value signaled by previous submit). I have noticed this issue with FFmpeg Vulkan video code, but it will happen with any Vulkan app using this pattern.
Still out of curiosity, is the performance loss from the CPU round-trip something you are able to measure?
Yes, I'm seeing measurable improvement with this patch applied. I have tested three cases (decoding three different videos) now and the result was +2%, +4% and +6%.
Regards,
David
Quoted 52 lines
Btw your patch is I think fine, so:
Reviewed-by Tvrtko Ursulin <[email protected]>But you will probably need Christian to ack it.
Regards,
Tvrtko
Quoted 40 lines
Quoted 40 lines
Quoted 39 lines
Signed-off-by David Rosca <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c| 10 ++++++++--1 file changed, 8 insertions(+), 2 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +8
-1@@ -29,6 +29,7 @@ #include <linux/pagemap.h> #include <linux/sync_file.h> #include <linux/dma-buf.h> +#include <linux/dma-fence-unwrap.h> #include <drm/amdgpu_drm.h> #include <drm/drm_syncobj.h>@@ -450,7 +451,8 @@ static int amdgpu_syncobj_lookup_and_add(structamdgpu_cs_parser *p, uint32_t handle, u64 point, u64 flags) { - struct dma_fence *fence; + struct dma_fence *fence, *f; + struct dma_fence_unwrap iter; int r; r = drm_syncobj_find_fence(p->filp, handle, point, flags, &fence);@@ -460,7 +462,11 @@ static int amdgpu_syncobj_lookup_and_add(struct amdgpu_cs_parser *p, return r; } - r = amdgpu_sync_fence(&p->sync, fence, GFP_KERNEL); + dma_fence_unwrap_for_each(f, &iter, fence) { + if (!r) + r = amdgpu_sync_fence(&p->sync, f, GFP_KERNEL); + } + dma_fence_put(fence); return r;}
Quoted 20 lines · David Rosca
Quoted 18 lines · Tvrtko Ursulin
Quoted 16 lines · David Rosca
Hi,
Quoted 12 lines · Tvrtko Ursulin
Hi,
Quoted 6 lines · David Rosca
drm_syncobj_find_fencereturns fence chain for timeline syncobjs. Scheduler expects normal fences as job dependencies to be able to determine whether the fences come from the same entity or sched and skip waiting on them.
With fence chain as job dependency, the fence will always be waited on forcing CPU round-trip before starting the job.Interesting! I was sending patches to fix this differently last year or so, by making the scheduler use
dma_fence_arrayfor tracking dependencies and relying ondma_fence_unwrap_mergeto unwrap, coalesce contexts and only keep the latest fence for each. But I did not have a good story to show for which use cases it helped. So I am curious if you could share which scenario you found gets an improvement from your patch?
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
The scenario I am trying to fix is very simple to reproduce with Vulkan when using timeline semaphore to sync submissions on the same queue (eg. each submit waiting on value signaled by previous submit). I have noticed this issue with FFmpeg Vulkan video code, but it will happen with any Vulkan app using this pattern.
Still out of curiosity, is the performance loss from the CPU round-trip something you are able to measure?
Yes, I'm seeing measurable improvement with this patch applied. I have tested three cases (decoding three different videos) now and the result was +2%, +4% and +6%.
Regards,
David
Cc Christian
Quoted 53 lines
Quoted 53 lines
Btw your patch is I think fine, so:
Reviewed-by Tvrtko Ursulin <[email protected]>But you will probably need Christian to ack it.
Regards,
Tvrtko
Quoted 40 lines
Quoted 40 lines
Quoted 39 lines
Signed-off-by David Rosca <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c| 10 ++++++++--1 file changed, 8 insertions(+), 2 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +8
-1@@ -29,6 +29,7 @@ #include <linux/pagemap.h> #include <linux/sync_file.h> #include <linux/dma-buf.h> +#include <linux/dma-fence-unwrap.h> #include <drm/amdgpu_drm.h> #include <drm/drm_syncobj.h>@@ -450,7 +451,8 @@ static int amdgpu_syncobj_lookup_and_add(struct amdgpu_cs_parser *p, uint32_t handle, u64 point, u64 flags) { - struct dma_fence *fence; + struct dma_fence *fence, *f; + struct dma_fence_unwrap iter; int r; r = drm_syncobj_find_fence(p->filp, handle, point, flags, &fence);@@ -460,7 +462,11 @@ static int amdgpu_syncobj_lookup_and_add(struct amdgpu_cs_parser *p, return r; } - r = amdgpu_sync_fence(&p->sync, fence, GFP_KERNEL); + dma_fence_unwrap_for_each(f, &iter, fence) { + if (!r) + r = amdgpu_sync_fence(&p->sync, f, GFP_KERNEL); + } + dma_fence_put(fence); return r;}
Christian König
maintainerQuoted 18 lines · David Rosca
Quoted 16 lines · Tvrtko Ursulin
Quoted 14 lines · David Rosca
Hi,
Quoted 11 lines · Tvrtko Ursulin
Hi,
Quoted 6 lines · David Rosca
drm_syncobj_find_fencereturns fence chain for timeline syncobjs. Scheduler expects normal fences as job dependencies to be able to determine whether the fences come from the same entity or sched and skip waiting on them.
With fence chain as job dependency, the fence will always be waited on forcing CPU round-trip before starting the job.
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Interesting! I was sending patches to fix this differently last year or so, by making the scheduler use
dma_fence_arrayfor tracking dependencies and relying ondma_fence_unwrap_mergeto unwrap, coalesce contexts and only keep the latest fence for each. But I did not have a good story to show for which use cases it helped. So I am curious if you could share which scenario you found gets an improvement from your patch?The scenario I am trying to fix is very simple to reproduce with Vulkan when using timeline semaphore to sync submissions on the same queue (eg. each submit waiting on value signaled by previous submit). I have noticed this issue with FFmpeg Vulkan video code, but it will happen with any Vulkan app using this pattern.
Still out of curiosity, is the performance loss from the CPU round-trip something you are able to measure?
Yes, I'm seeing measurable improvement with this patch applied. I have tested three cases (decoding three different videos) now and the result was +2%, +4% and +6%.
Mhm, I'm nearly 100% sure that we unwrapped the fences when we added timeline support. That must have been broken at some point without anybody noticing it.
Potentially when we moved the dependency handling from driver specific into common code. Probably a good idea to add CC stable to the patch before pushing it.
Going to review it on the original.
Regards,
Christian.
Quoted 54 lines
Regards,
DavidQuoted 50 lines
Btw your patch is I think fine, so:
Reviewed-by Tvrtko Ursulin <[email protected]>But you will probably need Christian to ack it.
Regards,
Tvrtko
Quoted 38 lines
Quoted 38 lines
Quoted 37 lines
Signed-off-by David Rosca <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c| 10 ++++++++--1 file changed, 8 insertions(+), 2 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +8
-1@@ -29,6 +29,7 @@ #include <linux/pagemap.h> #include <linux/sync_file.h> #include <linux/dma-buf.h> +#include <linux/dma-fence-unwrap.h> #include <drm/amdgpu_drm.h> #include <drm/drm_syncobj.h>@@ -450,7 +451,8 @@ static int amdgpu_syncobj_lookup_and_add(struct amdgpu_cs_parser *p, uint32_t handle, u64 point, u64 flags) { - struct dma_fence *fence; + struct dma_fence *fence, *f; + struct dma_fence_unwrap iter; int r; r = drm_syncobj_find_fence(p->filp, handle, point, flags, &fence);@@ -460,7 +462,11 @@ static int amdgpu_syncobj_lookup_and_add(struct amdgpu_cs_parser *p, return r; } - r = amdgpu_sync_fence(&p->sync, fence, GFP_KERNEL); + dma_fence_unwrap_for_each(f, &iter, fence) { + if (!r) + r = amdgpu_sync_fence(&p->sync, f, GFP_KERNEL); + } + dma_fence_put(fence); return r; }
Quoted 35 lines · David Rosca
drm_syncobj_find_fencereturns fence chain for timeline syncobjs. Scheduler expects normal fences as job dependencies to be able to determine whether the fences come from the same entity or sched and skip waiting on them.
With fence chain as job dependency, the fence will always be waited on forcing CPU round-trip before starting the job.Signed-off-by David Rosca <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3
-1@@ -29,6 +29,7 @@#include <linux/pagemap.h> #include <linux/sync_file.h> #include <linux/dma-buf.h> +#include <linux/dma-fence-unwrap.h> #include <drm/amdgpu_drm.h> #include <drm/drm_syncobj.h> @@ -450,7 +451,8 @@ static int amdgpu_syncobj_lookup_and_add(struct amdgpu_cs_parser *p, uint32_t handle, u64 point, u64 flags) { - struct dma_fence *fence; + struct dma_fence *fence, *f; +struct dma_fence_unwrap iter;int r; r =drm_syncobj_find_fence (p->filp, handle, point, flags, &fence);
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c static int amdgpu_syncobj_lookup_and_add(struct amdgpu_cs_parser *p, · line 467
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3
-1@@ -460,7 +462,11 @@ static int amdgpu_syncobj_lookup_and_add(struct amdgpu_cs_parser *p,return r; } - r =amdgpu_sync_fence (&p->sync, fence,GFP_KERNEL ); +dma_fence_unwrap_for_each (f, &iter, fence) { +if (!r) + r =amdgpu_sync_fence (&p->sync, f,GFP_KERNEL );
You should probably add an dma_fence_unwrap_cleanup() function which drops iter->chain and sets it to NULL and then code this like:
r = ...
if (r) {
dma_fence_unwrap_cleanup(..);
return r;
}drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c static int amdgpu_syncobj_lookup_and_add(struct amdgpu_cs_parser *p, · line 470
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +2
-0+ } +dma_fence_put (fence);
That is superfluous now, fence was consumed by dma_fence_unwrap_for_each().
Regards,
Christian.
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0
-0return r; }
Quoted 51 lines · Christian König
Quoted 35 lines · David Rosca
drm_syncobj_find_fencereturns fence chain for timeline syncobjs. Scheduler expects normal fences as job dependencies to be able to determine whether the fences come from the same entity or sched and skip waiting on them.
With fence chain as job dependency, the fence will always be waited on forcing CPU round-trip before starting the job.Signed-off-by David Rosca<[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3
-1@@ -29,6 +29,7 @@#include <linux/pagemap.h> #include <linux/sync_file.h> #include <linux/dma-buf.h> +#include <linux/dma-fence-unwrap.h> #include <drm/amdgpu_drm.h> #include <drm/drm_syncobj.h> @@ -450,7 +451,8 @@ static int amdgpu_syncobj_lookup_and_add(struct amdgpu_cs_parser *p, uint32_t handle, u64 point, u64 flags) { - struct dma_fence *fence; + struct dma_fence *fence, *f; +struct dma_fence_unwrap iter;int r; r =drm_syncobj_find_fence (p->filp, handle, point, flags, &fence);drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c static int amdgpu_syncobj_lookup_and_add(struct amdgpu_cs_parser *p, · line 467
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3
-1@@ -460,7 +462,11 @@ static int amdgpu_syncobj_lookup_and_add(struct amdgpu_cs_parser *p,return r; } - r =amdgpu_sync_fence (&p->sync, fence,GFP_KERNEL ); +dma_fence_unwrap_for_each (f, &iter, fence) { +if (!r) + r =amdgpu_sync_fence (&p->sync, f,GFP_KERNEL );You should probably add an
dma_fence_unwrap_cleanup()function which dropsiter->chainand sets it to NULL and then code this like:r = ...
if (r) { dma_fence_unwrap_cleanup(..); return r; }
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c static int amdgpu_syncobj_lookup_and_add(struct amdgpu_cs_parser *p, · line 470
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +2
-0+ } +dma_fence_put (fence);That is superfluous now, fence was consumed by
dma_fence_unwrap_for_each().
dma_fence_unwrap_first() gets own reference to head, so I think that should not be the case?
Regards,
David
Quoted 5 lines
Regards,
Christian.Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0
-0return r; }