drm/sched: Call drm_sched_fence_set_parent() from drm_sched_fence_scheduled()
Subsystems (3)
PATCH v3 1/1 drm/sched: Call drm_sched_fence_set_parent() from drm_sched_fence_scheduled()
Drivers that can delegate waits to the firmware/GPU pass the scheduled fence to drm_sched_job_add_dependency(), and issue wait commands to the firmware/GPU at job submission time. For this to be possible, they need all their 'native' dependencies to have a valid parent since this is where the actual HW fence information are encoded.
In drm_sched_main(), we currently call drm_sched_fence_set_parent() after drm_sched_fence_scheduled(), leaving a short period of time during which the job depending on this fence can be submitted.
Since setting parent and signaling the fence are two things that are kinda related (you can't have a parent if the job hasn't been scheduled), it probably makes sense to pass the parent fence to drm_sched_fence_scheduled() and let it call drm_sched_fence_set_parent() before it signals the scheduled fence.
Here is a detailed description of the race we are fixing here:
Thread A Thread B
- calls drm_sched_fence_scheduled()
- signals s_fence->scheduled which
wakes up thread B
- entity dep signaled, checking the next dep
- no more deps waiting
- entity is picked for job submission by drm_gpu_scheduler
- run_job() is called
- run_job() tries to collect native fence info from s_fence->parent, but it's NULL =>
BOOM, we can't do our native wait
- calls drm_sched_fence_set_parent()
v2:
* Fix commit message
v3:
* Add a detailed description of the race to the commit message
* Add Luben's R-b
--- drivers/gpu/drm/scheduler/sched_fence.c | 40 +++++++++++++++---------- drivers/gpu/drm/scheduler/sched_main.c | 3 +- include/drm/gpu_scheduler.h | 5 ++-- 3 files changed, 28 insertions(+), 20 deletions(-)
drivers/gpu/drm/scheduler/sched_fence.c +25 -15
@@ -48,8 +48,32 @@ static void __exit drm_sched_fence_slab_fini(void)kmem_cache_destroy (sched_fence_slab); } -void drm_sched_fence_scheduled (struct drm_sched_fence *fence) +static void drm_sched_fence_set_parent (struct drm_sched_fence *s_fence, +struct dma_fence *fence) { +/* +* smp_store_release() to ensure another thread racing us +* in drm_sched_fence_set_deadline_finished() sees the +* fence's parent set before test_bit() +*/ +smp_store_release (&s_fence->parent,dma_fence_get (fence)); +if (test_bit (DRM_SCHED_FENCE_FLAG_HAS_DEADLINE_BIT , + &s_fence->finished.flags)) +dma_fence_set_deadline (fence, s_fence->deadline); +} + +void drm_sched_fence_scheduled (struct drm_sched_fence *fence, +struct dma_fence *parent) +{ +/* Set the parent before signaling the scheduled fence, such that, +* any waiter expecting the parent to be filled after the job has +* been scheduled (which is the case for drivers delegating waits +* to some firmware) doesn't have to busy wait for parent to show +* up. +*/ +if (!IS_ERR_OR_NULL (parent)) +drm_sched_fence_set_parent (fence, parent); +dma_fence_signal (&fence->scheduled); }
@@ -179,20 +203,6 @@ struct drm_sched_fence *to_drm_sched_fence(struct dma_fence *f) }EXPORT_SYMBOL (to_drm_sched_fence); -void drm_sched_fence_set_parent (struct drm_sched_fence *s_fence, -struct dma_fence *fence) -{ -/* -* smp_store_release() to ensure another thread racing us -* in drm_sched_fence_set_deadline_finished() sees the -* fence's parent set before test_bit() -*/ -smp_store_release (&s_fence->parent,dma_fence_get (fence)); -if (test_bit (DRM_SCHED_FENCE_FLAG_HAS_DEADLINE_BIT , - &s_fence->finished.flags)) -dma_fence_set_deadline (fence, s_fence->deadline); -} -struct drm_sched_fence *drm_sched_fence_alloc (struct drm_sched_entity *entity,void *owner) {
drivers/gpu/drm/scheduler/sched_main.c +1 -2
@@ -1040,10 +1040,9 @@ static int drm_sched_main(void *param)trace_drm_run_job (sched_job, entity); fence = sched->ops->run_job (sched_job);complete_all (&entity->entity_idle); -drm_sched_fence_scheduled (s_fence); +drm_sched_fence_scheduled (s_fence, fence);if (!IS_ERR_OR_NULL (fence)) { -drm_sched_fence_set_parent (s_fence, fence);/* Drop for original kref_init of the fence */ dma_fence_put (fence);
include/drm/gpu_scheduler.h +2 -3
@@ -582,15 +582,14 @@ void drm_sched_entity_set_priority(struct drm_sched_entity *entity, enum drm_sched_priority priority);bool drm_sched_entity_is_ready (struct drm_sched_entity *entity); -void drm_sched_fence_set_parent (struct drm_sched_fence *s_fence, -struct dma_fence *fence);struct drm_sched_fence *drm_sched_fence_alloc (struct drm_sched_entity *s_entity,void *owner);void drm_sched_fence_init (struct drm_sched_fence *fence,struct drm_sched_entity *entity);void drm_sched_fence_free (struct drm_sched_fence *fence); -void drm_sched_fence_scheduled (struct drm_sched_fence *fence); +void drm_sched_fence_scheduled (struct drm_sched_fence *fence, +struct dma_fence *parent);void drm_sched_fence_finished (struct drm_sched_fence *fence);unsigned long drm_sched_suspend_timeout (struct drm_gpu_scheduler *sched);
Signature · 1 line
2.41.0
Quoted 37 lines · On Fri, 23 Jun 2023 09:52:04 +0200 Boris Brezillon…
Drivers that can delegate waits to the
firmware/GPUpass the scheduled fence todrm_sched_job_add_dependency(), and issue wait commands to thefirmware/GPUat job submission time. For this to be possible, they need all their 'native' dependencies to have a valid parent since this is where the actual HW fence information are encoded.In
drm_sched_main(), we currently calldrm_sched_fence_set_parent()afterdrm_sched_fence_scheduled(), leaving a short period of time during which the job depending on this fence can be submitted.Since setting parent and signaling the fence are two things that are kinda related (you can't have a parent if the job hasn't been scheduled), it probably makes sense to pass the parent fence to
drm_sched_fence_scheduled()and let it calldrm_sched_fence_set_parent()before it signals the scheduled fence.Here is a detailed description of the race we are fixing here:
Thread A Thread B
- calls
drm_sched_fence_scheduled()
- signalss_fence->scheduledwhich
wakes up thread B- entity dep signaled, checking the next dep
- no more deps waiting
- entity is picked for job submission bydrm_gpu_scheduler
-run_job()is called
-run_job()tries to collect native fence info froms_fence->parent, but it's NULL =>
BOOM, we can't do our native wait
Quoted 9 lines
- calls
drm_sched_fence_set_parent()v2:
* Fix commit messagev3:
* Add a detailed description of the race to the commit message
* Add Luben's R-b
FYI, I didn't put a Fixes tag because the various moves/modifications that happened on this file will make it hard to backport anyway, and no one complained about it so far. But if we want to have one, it would probably be:
Quoted 113 lines
Signed-off-by Boris Brezillon <[email protected]>Cc Frank Binns <[email protected]>Cc Sarah Walker <[email protected]>Cc Donald Robson <[email protected]>Cc Luben Tuikov <[email protected]>Cc David Airlie <[email protected]>Cc Daniel Vetter <[email protected]>Cc Sumit Semwal <[email protected]>Cc "Christian König" <[email protected]>Reviewed-by Luben Tuikov <[email protected]>--- drivers/gpu/drm/scheduler/sched_fence.c | 40 +++++++++++++++---------- drivers/gpu/drm/scheduler/sched_main.c | 3 +- include/drm/gpu_scheduler.h | 5 ++-- 3 files changed, 28 insertions(+), 20 deletions(-)drivers/gpu/drm/scheduler/sched_fence.c +25
-15@@ -48,8 +48,32 @@ static void __exit drm_sched_fence_slab_fini(void)kmem_cache_destroy (sched_fence_slab); } -void drm_sched_fence_scheduled (struct drm_sched_fence *fence) +static void drm_sched_fence_set_parent (struct drm_sched_fence *s_fence, +struct dma_fence *fence) { +/* +* smp_store_release() to ensure another thread racing us +* in drm_sched_fence_set_deadline_finished() sees the +* fence's parent set before test_bit() +*/ +smp_store_release (&s_fence->parent,dma_fence_get (fence)); +if (test_bit (DRM_SCHED_FENCE_FLAG_HAS_DEADLINE_BIT , + &s_fence->finished.flags)) +dma_fence_set_deadline (fence, s_fence->deadline); +} + +void drm_sched_fence_scheduled (struct drm_sched_fence *fence, +struct dma_fence *parent) +{ +/* Set the parent before signaling the scheduled fence, such that, +* any waiter expecting the parent to be filled after the job has +* been scheduled (which is the case for drivers delegating waits +* to some firmware) doesn't have to busy wait for parent to show +* up. +*/ +if (!IS_ERR_OR_NULL (parent)) +drm_sched_fence_set_parent (fence, parent); +dma_fence_signal (&fence->scheduled); }@@ -179,20 +203,6 @@ struct drm_sched_fence *to_drm_sched_fence(struct dma_fence *f) }EXPORT_SYMBOL (to_drm_sched_fence); -void drm_sched_fence_set_parent (struct drm_sched_fence *s_fence, -struct dma_fence *fence) -{ -/* -* smp_store_release() to ensure another thread racing us -* in drm_sched_fence_set_deadline_finished() sees the -* fence's parent set before test_bit() -*/ -smp_store_release (&s_fence->parent,dma_fence_get (fence)); -if (test_bit (DRM_SCHED_FENCE_FLAG_HAS_DEADLINE_BIT , - &s_fence->finished.flags)) -dma_fence_set_deadline (fence, s_fence->deadline); -} -struct drm_sched_fence *drm_sched_fence_alloc (struct drm_sched_entity *entity,void *owner) {drivers/gpu/drm/scheduler/sched_main.c +1
-2@@ -1040,10 +1040,9 @@ static int drm_sched_main(void *param)trace_drm_run_job (sched_job, entity); fence = sched->ops->run_job (sched_job);complete_all (&entity->entity_idle); -drm_sched_fence_scheduled (s_fence); +drm_sched_fence_scheduled (s_fence, fence);if (!IS_ERR_OR_NULL (fence)) { -drm_sched_fence_set_parent (s_fence, fence);/* Drop for original kref_init of the fence */ dma_fence_put (fence);include/drm/gpu_scheduler.h +2
-3@@ -582,15 +582,14 @@ void drm_sched_entity_set_priority(struct drm_sched_entity *entity, enum drm_sched_priority priority);bool drm_sched_entity_is_ready (struct drm_sched_entity *entity); -void drm_sched_fence_set_parent (struct drm_sched_fence *s_fence, -struct dma_fence *fence);struct drm_sched_fence *drm_sched_fence_alloc (struct drm_sched_entity *s_entity,void *owner);void drm_sched_fence_init (struct drm_sched_fence *fence,struct drm_sched_entity *entity);void drm_sched_fence_free (struct drm_sched_fence *fence); -void drm_sched_fence_scheduled (struct drm_sched_fence *fence); +void drm_sched_fence_scheduled (struct drm_sched_fence *fence, +struct dma_fence *parent);void drm_sched_fence_finished (struct drm_sched_fence *fence);unsigned long drm_sched_suspend_timeout (struct drm_gpu_scheduler *sched);
Quoted 50 lines · Boris Brezillon
Quoted 37 lines · On Fri, 23 Jun 2023 09:52:04 +0200 Boris Brezillon…
Drivers that can delegate waits to the
firmware/GPUpass the scheduled fence todrm_sched_job_add_dependency(), and issue wait commands to thefirmware/GPUat job submission time. For this to be possible, they need all their 'native' dependencies to have a valid parent since this is where the actual HW fence information are encoded.In
drm_sched_main(), we currently calldrm_sched_fence_set_parent()afterdrm_sched_fence_scheduled(), leaving a short period of time during which the job depending on this fence can be submitted.Since setting parent and signaling the fence are two things that are kinda related (you can't have a parent if the job hasn't been scheduled), it probably makes sense to pass the parent fence to
drm_sched_fence_scheduled()and let it calldrm_sched_fence_set_parent()before it signals the scheduled fence.Here is a detailed description of the race we are fixing here:
Thread A Thread B
- calls
drm_sched_fence_scheduled()
- signalss_fence->scheduledwhich
wakes up thread B- entity dep signaled, checking the next dep
- no more deps waiting
- entity is picked for job submission bydrm_gpu_scheduler
-run_job()is called
-run_job()tries to collect native fence info froms_fence->parent, but it's NULL =>
BOOM, we can't do our native waitQuoted 9 lines
- calls
drm_sched_fence_set_parent()v2:
* Fix commit messagev3:
* Add a detailed description of the race to the commit message
* Add Luben's R-b
Quoted 7 lines
FYI, I didn't put a Fixes tag because the various moves/modifications that happened on this file will make it hard to backport anyway, and no one complained about it so far. But if we want to have one, it would probably be:
Fixes 754ce0fa55c4 ("drm/amd: add parent for sched fence")
I agree with your assessment--the race fix doesn't seem to be pointing to or introduced by one particular change. Plus that fixes change is from 2016... So, we're good to go as is.
Signature · 2 lines
Regards, Luben
Boris Brezillon
AppliedQuoted 56 lines · On Fri, 23 Jun 2023 14:37:57 -0400 Luben Tuikov <[email protected]> wrote:
Quoted 55 lines · Boris Brezillon
Quoted 37 lines · On Fri, 23 Jun 2023 09:52:04 +0200 Boris Brezillon…
Drivers that can delegate waits to the
firmware/GPUpass the scheduled fence todrm_sched_job_add_dependency(), and issue wait commands to thefirmware/GPUat job submission time. For this to be possible, they need all their 'native' dependencies to have a valid parent since this is where the actual HW fence information are encoded.In
drm_sched_main(), we currently calldrm_sched_fence_set_parent()afterdrm_sched_fence_scheduled(), leaving a short period of time during which the job depending on this fence can be submitted.Since setting parent and signaling the fence are two things that are kinda related (you can't have a parent if the job hasn't been scheduled), it probably makes sense to pass the parent fence to
drm_sched_fence_scheduled()and let it calldrm_sched_fence_set_parent()before it signals the scheduled fence.Here is a detailed description of the race we are fixing here:
Thread A Thread B
- calls
drm_sched_fence_scheduled()
- signalss_fence->scheduledwhich
wakes up thread B- entity dep signaled, checking the next dep
- no more deps waiting
- entity is picked for job submission bydrm_gpu_scheduler
-run_job()is called
-run_job()tries to collect native fence info froms_fence->parent, but it's NULL =>
BOOM, we can't do our native waitQuoted 9 lines
- calls
drm_sched_fence_set_parent()v2:
* Fix commit messagev3:
* Add a detailed description of the race to the commit message
* Add Luben's R-bFYI, I didn't put a Fixes tag because the various moves/modifications that happened on this file will make it hard to backport anyway, and no one complained about it so far. But if we want to have one, it would probably be:
Quoted 6 lines
Quoted 2 lines
Fixes 754ce0fa55c4 ("drm/amd: add parent for sched fence")I agree with your assessment--the race fix doesn't seem to be pointing to or introduced by one particular change. Plus that fixes change is from 2016... So, we're good to go as is.
Queued to drm-misc-fixes.