drm/amdgpu: drop the gang leader reference when freeing gang jobs
Subsystems (2)
PATCH v2 1/1 drm/amdgpu: drop the gang leader reference when freeing gang jobs
Every job of a gang except the leader holds a reference to the scheduled fence of the leader in job->gang_submit. Only amdgpu_job_free(), used for jobs which never ran, drops it, so for every gang submission which completes the scheduler fence of the leader, and with it its hardware fence, is leaked. amdgpu_job_free() in turn checks for the leader only after drm_sched_job_cleanup() cleared the scheduler fence, so a leader freed there drops a reference it never took.
On navi31, 100 submissions of a GFX + COMPUTE gang grow the slab cache drm_sched_fence is merged into by 60 to 180 objects each time, while 100 GFX only submissions don't.
Use amdgpu_job_free() for the jobs the scheduler frees too, and check for the gang leader there before drm_sched_job_cleanup() clears the scheduler fence. As the scheduler clears the entity of the jobs it picked up, check for the scheduler fence instead to tell whether the job needs cleaning up.
The IBs of jobs pushed to the scheduler are freed before that, so the IBs left to amdgpu_job_free() only ever ran when the job was submitted directly. Free them with the hardware fence or right away, never with the scheduler fence, which nobody signals for jobs aborted after drm_sched_job_arm(), like a CS returning -EAGAIN.
v2: unify amdgpu_job_free() and amdgpu_job_free_cb() (Christian)
--- drivers/gpu/drm/amd/amdgpu/amdgpu_job.c | 47 ++++++++++++++----------- 1 file changed, 27 insertions(+), 20 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +27 -20
@@ -313,22 +313,7 @@ void amdgpu_job_free_resources(struct amdgpu_job *job)static void amdgpu_job_free_cb (struct drm_sched_job *s_job) { -struct amdgpu_job *job =to_amdgpu_job (s_job); - -drm_sched_job_cleanup (s_job); - -amdgpu_sync_free (&job->explicit_sync); - -if (dma_fence_was_initialized (&job->hw_fence->base)) -dma_fence_put (&job->hw_fence->base); -else -kfree (job->hw_fence); -if (dma_fence_was_initialized (&job->hw_vm_fence->base)) -dma_fence_put (&job->hw_vm_fence->base); -else -kfree (job->hw_vm_fence); - -kfree (job); +amdgpu_job_free (to_amdgpu_job (s_job)); } void amdgpu_job_set_gang_leader(struct amdgpu_job *job,
@@ -349,14 +334,36 @@ void amdgpu_job_set_gang_leader(struct amdgpu_job *job,void amdgpu_job_free (struct amdgpu_job *job) { -if (job->base.entity) -drm_sched_job_cleanup (&job->base); +struct dma_fence *f =NULL ; +unsigned int i; -amdgpu_job_free_resources (job); -amdgpu_sync_free (&job->explicit_sync); +/* +* The IBs of jobs pushed to the scheduler were already freed with the +* scheduler fence before that. The remaining ones only got to the +* hardware when submitted directly. Don't use the scheduler fence +* here, it never signals for jobs aborted after drm_sched_job_arm(). +*/ +if (dma_fence_was_initialized (&job->hw_fence->base)) + f = &job->hw_fence->base; +for (i =0 ; i < job->num_ibs; ++i) +amdgpu_ib_free (&job->ibs[i], f); + +/* +* The gang leader doesn't hold a reference to itself. Check before +* drm_sched_job_cleanup() clears the scheduler fence. +*/ if (job->gang_submit != &job->base.s_fence->scheduled)dma_fence_put (job->gang_submit); +/* +* Not job->base.entity, the scheduler clears that for the jobs it +* picked up. +*/ +if (job->base.s_fence) +drm_sched_job_cleanup (&job->base); + +amdgpu_sync_free (&job->explicit_sync); +if (dma_fence_was_initialized (&job->hw_fence->base))dma_fence_put (&job->hw_fence->base);else
Signature · 1 line
2.50.1
Christian König
maintainerQuoted 68 lines · Jesse Zhang
Every job of a gang except the leader holds a reference to the scheduled fence of the leader in
job->gang_submit. Onlyamdgpu_job_free(), used for jobs which never ran, drops it, so for every gang submission which completes the scheduler fence of the leader, and with it its hardware fence, is leaked.amdgpu_job_free()in turn checks for the leader only afterdrm_sched_job_cleanup()cleared the scheduler fence, so a leader freed there drops a reference it never took.On navi31, 100 submissions of a GFX + COMPUTE gang grow the slab cache
drm_sched_fenceis merged into by 60 to 180 objects each time, while 100 GFX only submissions don't.Use
amdgpu_job_free()for the jobs the scheduler frees too, and check for the gang leader there beforedrm_sched_job_cleanup()clears the scheduler fence. As the scheduler clears the entity of the jobs it picked up, check for the scheduler fence instead to tell whether the job needs cleaning up.The IBs of jobs pushed to the scheduler are freed before that, so the IBs left to
amdgpu_job_free()only ever ran when the job was submitted directly. Free them with the hardware fence or right away, never with the scheduler fence, which nobody signals for jobs aborted afterdrm_sched_job_arm(), like a CS returning-EAGAIN.v2: unify
amdgpu_job_free()andamdgpu_job_free_cb()(Christian)Fixes 68ce8b242242 ("drm/amdgpu: add gang submit backend v2")Fixes f7d66fb2ea43 ("drm/amdgpu: cleanup scheduler job initialization v2")Signed-off-by Jesse Zhang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_job.c | 47 ++++++++++++++----------- 1 file changed, 27 insertions(+), 20 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +2
-18@@ -313,22 +313,7 @@ void amdgpu_job_free_resources(struct amdgpu_job *job)static void amdgpu_job_free_cb (struct drm_sched_job *s_job) { -struct amdgpu_job *job =to_amdgpu_job (s_job); - -drm_sched_job_cleanup (s_job); - -amdgpu_sync_free (&job->explicit_sync); - -if (dma_fence_was_initialized (&job->hw_fence->base)) -dma_fence_put (&job->hw_fence->base); -else -kfree (job->hw_fence); -if (dma_fence_was_initialized (&job->hw_vm_fence->base)) -dma_fence_put (&job->hw_vm_fence->base); -else -kfree (job->hw_vm_fence); - -kfree (job); +amdgpu_job_free (to_amdgpu_job (s_job)); } void amdgpu_job_set_gang_leader(struct amdgpu_job *job,@@ -349,14 +334,36 @@ void amdgpu_job_set_gang_leader(struct amdgpu_job *job,void amdgpu_job_free (struct amdgpu_job *job) { -if (job->base.entity) -drm_sched_job_cleanup (&job->base); +struct dma_fence *f =NULL ;
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c void amdgpu_job_set_gang_leader(struct amdgpu_job *job, · line 345
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +7
-2+unsigned int i; -amdgpu_job_free_resources (job); -amdgpu_sync_free (&job->explicit_sync); +/* +* The IBs of jobs pushed to the scheduler were already freed with the +* scheduler fence before that. The remaining ones only got to the +* hardware when submitted directly. Don't use the scheduler fence +* here, it never signals for jobs aborted after drm_sched_job_arm(). +*/
Drop that comment, it is more misleading than anything else.
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c void amdgpu_job_set_gang_leader(struct amdgpu_job *job, · line 354
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +9
-0+if (dma_fence_was_initialized (&job->hw_fence->base)) + f = &job->hw_fence->base; +for (i =0 ; i < job->num_ibs; ++i) +amdgpu_ib_free (&job->ibs[i], f); + +/* +* The gang leader doesn't hold a reference to itself. Check before +* drm_sched_job_cleanup() clears the scheduler fence. +*/
Same here. That should be documented on job->gang_submit if necessary.
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c void amdgpu_job_set_gang_leader(struct amdgpu_job *job, · line 361
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +4
-0if (job->gang_submit != &job->base.s_fence->scheduled)dma_fence_put (job->gang_submit); +/* +* Not job->base.entity, the scheduler clears that for the jobs it +* picked up. +*/
Hui? What do you mean here?
Regards,
Christian.
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +5
-0+if (job->base.s_fence) +drm_sched_job_cleanup (&job->base); + +amdgpu_sync_free (&job->explicit_sync); +if (dma_fence_was_initialized (&job->hw_fence->base))dma_fence_put (&job->hw_fence->base);else
AMD General
Quoted 110 lines
-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Monday, October 19, 2026 10:28 PM
To: Zhang, Jesse(Jie) <[email protected]>; amd- [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v2] drm/amdgpu: drop the gang leader reference when freeing gang jobsQuoted 7 lines · Jesse Zhang
Every job of a gang except the leader holds a reference to the scheduled fence of the leader in
job->gang_submit. Onlyamdgpu_job_free(), used for jobs which never ran, drops it, so for every gang submission which completes the scheduler fence of the leader, and with it its hardware fence, is leaked.amdgpu_job_free()in turn checks for the leader only afterdrm_sched_job_cleanup()cleared the scheduler fence, so a leader freed there drops a reference it nevertook.
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c void amdgpu_job_free_resources(struct
Quoted 34 lines
On navi31, 100 submissions of a GFX + COMPUTE gang grow the slab cache
drm_sched_fenceis merged into by 60 to 180 objects each time, while 100 GFX only submissions don't.Use
amdgpu_job_free()for the jobs the scheduler frees too, and check for the gang leader there beforedrm_sched_job_cleanup()clears the scheduler fence. As the scheduler clears the entity of the jobs it picked up, check for the scheduler fence instead to tell whether the job needs cleaning up.The IBs of jobs pushed to the scheduler are freed before that, so the IBs left to
amdgpu_job_free()only ever ran when the job was submitted directly. Free them with the hardware fence or right away, never with the scheduler fence, which nobody signals for jobs aborted afterdrm_sched_job_arm(), like a CS returning-EAGAIN.v2: unify
amdgpu_job_free()andamdgpu_job_free_cb()(Christian)Fixes 68ce8b242242 ("drm/amdgpu: add gang submit backend v2")Fixes f7d66fb2ea43 ("drm/amdgpu: cleanup scheduler job initializationv2")
Signed-off-by Jesse Zhang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_job.c | 47 ++++++++++++++----------- 1 file changed, 27 insertions(+), 20 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +0
-0@@ -313,22 +313,7 @@ void amdgpu_job_free_resources(struct
amdgpu_jobQuoted 29 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +3
-18*job)static void amdgpu_job_free_cb (struct drm_sched_job *s_job) { -struct amdgpu_job *job =to_amdgpu_job (s_job); - -drm_sched_job_cleanup (s_job); - -amdgpu_sync_free (&job->explicit_sync); - -if (dma_fence_was_initialized (&job->hw_fence->base)) -dma_fence_put (&job->hw_fence->base); -else -kfree (job->hw_fence); -if (dma_fence_was_initialized (&job->hw_vm_fence->base)) -dma_fence_put (&job->hw_vm_fence->base); -else -kfree (job->hw_vm_fence); - -kfree (job); +amdgpu_job_free (to_amdgpu_job (s_job)); } void amdgpu_job_set_gang_leader(struct amdgpu_job *job, @@ -349,14 +334,36 @@ void amdgpu_job_set_gang_leader(struct amdgpu_job *job, void amdgpu_job_free(struct amdgpu_job *job) { -if (job->base.entity) -drm_sched_job_cleanup (&job->base); + struct dma_fence *f = NULL;drivers/gpu/drm/amd/amdgpu/amdgpu_job.c void amdgpu_job_free_resources(struct amdgpu_job *job)
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +7
-2+unsigned int i; -amdgpu_job_free_resources (job); -amdgpu_sync_free (&job->explicit_sync); +/* +* The IBs of jobs pushed to the scheduler were already freed with the +* scheduler fence before that. The remaining ones only got to the +* hardware when submitted directly. Don't use the scheduler fence +* here, it never signals for jobs aborted after drm_sched_job_arm(). +*/ Drop that comment, it is more misleading than anything else.
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c void amdgpu_job_set_gang_leader(struct amdgpu_job *job, · line 354
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +9
-0+if (dma_fence_was_initialized (&job->hw_fence->base)) + f = &job->hw_fence->base; +for (i =0 ; i < job->num_ibs; ++i) +amdgpu_ib_free (&job->ibs[i], f); + +/* +* The gang leader doesn't hold a reference to itself. Check before +* drm_sched_job_cleanup() clears the scheduler fence. +*/ Same here. That should be documented on
job->gang_submitif necessary.Quoted 3 lines
if (job->gang_submit != &job->base.s_fence->scheduled) dma_fence_put(job->gang_submit);
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c void amdgpu_job_set_gang_leader(struct amdgpu_job *job, · line 361
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +4
-0+/* +* Not job->base.entity, the scheduler clears that for the jobs it +* picked up. +*/ Hui? What do you mean here?
drm_sched_entity_pop_job() sets sched_job->entity to NULL when the scheduler takes the job off the entity's queue.
So for the jobs freed through amdgpu_job_free_cb(), which now ends up in amdgpu_job_free(), job->base.entity is always NULL, and the old if (job->base.entity) check would skip drm_sched_job_cleanup() for them and leak the scheduler fence and the dependencies. job->base.s_fence is set by drm_sched_job_init() and only cleared by drm_sched_job_cleanup(),
so it tells whether the job still needs cleaning up for both the aborted jobs and the ones the scheduler ran.
I will drop the comments in v3.
Thanks
Jesse
Quoted 12 lines
Regards,
Christian.Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +5
-0+if (job->base.s_fence) +drm_sched_job_cleanup (&job->base); + +amdgpu_sync_free (&job->explicit_sync); +if (dma_fence_was_initialized (&job->hw_fence->base))dma_fence_put (&job->hw_fence->base);else