Skip to contentSkip to conversation

drm/amdgpu: fix possible fence leaks from job structure

PATCH In review Alex Deucher 3 participants 9 messages Oct 27, 2025 – Nov 3, 2025

Subsystems (2)
lore
Share & tools
lore

Alex Deucher

author maintainer
#0 lore raw

PATCH 1/1 drm/amdgpu: fix possible fence leaks from job structure

If we don't end up initializing the fences, free them when we free the job.

v2: take a reference to the fences if we emit them

Fixes db36632ea51e ("drm/amdgpu: clean up and unify hw fence handling")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c  |  2 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_job.c | 18 ++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  |  2 ++
 3 files changed, 22 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c +2 -0
index 39229ece83f83..0596114377600 100644
@@ -302,6 +302,8 @@ int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs,
 		return r;
 	}
 	*f = &af->base;
+	/* get a ref for the job */
+	dma_fence_get(*f);
 
 	if (ring->funcs->insert_end)
 		ring->funcs->insert_end(ring);
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +18 -0
index 55c7e104d5ca0..dc970f5fe601b 100644
@@ -295,6 +295,15 @@ static void amdgpu_job_free_cb(struct drm_sched_job *s_job)
 
 	amdgpu_sync_free(&job->explicit_sync);
 
+	if (job->hw_fence->base.ops)
+		dma_fence_put(&job->hw_fence->base);
+	else
+		kfree(job->hw_fence);
+	if (job->hw_vm_fence->base.ops)
+		dma_fence_put(&job->hw_vm_fence->base);
+	else
+		kfree(job->hw_vm_fence);
+
 	kfree(job);
 }
 
@@ -324,6 +333,15 @@ void amdgpu_job_free(struct amdgpu_job *job)
 	if (job->gang_submit != &job->base.s_fence->scheduled)
 		dma_fence_put(job->gang_submit);
 
+	if (job->hw_fence->base.ops)
+		dma_fence_put(&job->hw_fence->base);
+	else
+		kfree(job->hw_fence);
+	if (job->hw_vm_fence->base.ops)
+		dma_fence_put(&job->hw_vm_fence->base);
+	else
+		kfree(job->hw_vm_fence);
+
 	kfree(job);
 }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -0
index db66b4232de02..f8c67840f446f 100644
@@ -845,6 +845,8 @@ int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
 		if (r)
 			return r;
 		fence = &job->hw_vm_fence->base;
+		/* get a ref for the job */
+		dma_fence_get(fence);
 	}
 
 	if (vm_flush_needed) {
Signature · 1 line
2.51.0
#1 lore raw

Ping?

Quoted 80 lines · Alex Deucher

If we don't end up initializing the fences, free them when we free the job.

v2: take a reference to the fences if we emit them

Fixes db36632ea51e ("drm/amdgpu: clean up and unify hw fence handling")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c  |  2 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_job.c | 18 ++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  |  2 ++
 3 files changed, 22 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c +2 -0
index 39229ece83f83..0596114377600 100644
@@ -302,6 +302,8 @@ int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs,
                return r;
        }
        *f = &af->base;
+       /* get a ref for the job */
+       dma_fence_get(*f);
 
        if (ring->funcs->insert_end)
                ring->funcs->insert_end(ring);
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +18 -0
index 55c7e104d5ca0..dc970f5fe601b 100644
@@ -295,6 +295,15 @@ static void amdgpu_job_free_cb(struct drm_sched_job *s_job)
 
        amdgpu_sync_free(&job->explicit_sync);
 
+       if (job->hw_fence->base.ops)
+               dma_fence_put(&job->hw_fence->base);
+       else
+               kfree(job->hw_fence);
+       if (job->hw_vm_fence->base.ops)
+               dma_fence_put(&job->hw_vm_fence->base);
+       else
+               kfree(job->hw_vm_fence);
+
        kfree(job);
 }
 
@@ -324,6 +333,15 @@ void amdgpu_job_free(struct amdgpu_job *job)
        if (job->gang_submit != &job->base.s_fence->scheduled)
                dma_fence_put(job->gang_submit);
 
+       if (job->hw_fence->base.ops)
+               dma_fence_put(&job->hw_fence->base);
+       else
+               kfree(job->hw_fence);
+       if (job->hw_vm_fence->base.ops)
+               dma_fence_put(&job->hw_vm_fence->base);
+       else
+               kfree(job->hw_vm_fence);
+
        kfree(job);
 }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -1
index db66b4232de02..f8c67840f446f 100644
@@ -845,6 +845,8 @@ int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
                if (r)
                        return r;
                fence = &job->hw_vm_fence->base;
+               /* get a ref for the job */
+               dma_fence_get(fence);
        }
 
        if (vm_flush_needed) {
--

2.51.0

maintainer
#2 lore raw
In reply to Alex Deucher
Quoted 19 lines · Alex Deucher

If we don't end up initializing the fences, free them when we free the job.

v2: take a reference to the fences if we emit them

Fixes db36632ea51e ("drm/amdgpu: clean up and unify hw fence handling")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c  |  2 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_job.c | 18 ++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  |  2 ++
 3 files changed, 22 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c +0 -0
index 39229ece83f83..0596114377600 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs, · line 306

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c +2 -0
@@ -302,6 +302,8 @@ int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs,
 		return r;
 	}
 	*f = &af->base;
+	/* get a ref for the job */
+	dma_fence_get(*f);

I think it would be better to set the fence inside the job to NULL as soon as it is consumed/initialized.

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c +0 -0
 	if (ring->funcs->insert_end)
 		ring->funcs->insert_end(ring);
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +0 -0
index 55c7e104d5ca0..dc970f5fe601b 100644
@@ -295,6 +295,15 @@ static void amdgpu_job_free_cb(struct drm_sched_job *s_job)
 
 	amdgpu_sync_free(&job->explicit_sync);

drivers/gpu/drm/amd/amdgpu/amdgpu_job.c static void amdgpu_job_free_cb(struct drm_sched_job *s_job) · line 306

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +9 -0
 
+	if (job->hw_fence->base.ops)
+		dma_fence_put(&job->hw_fence->base);
+	else
+		kfree(job->hw_fence);
+	if (job->hw_vm_fence->base.ops)
+		dma_fence_put(&job->hw_vm_fence->base);
+	else
+		kfree(job->hw_vm_fence);
+

This way that here can just be a kfree(..).

Regards,
Christian.

Quoted 32 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +9 -0
 	kfree(job);
 }
 
@@ -324,6 +333,15 @@ void amdgpu_job_free(struct amdgpu_job *job)
 	if (job->gang_submit != &job->base.s_fence->scheduled)
 		dma_fence_put(job->gang_submit);
 
+	if (job->hw_fence->base.ops)
+		dma_fence_put(&job->hw_fence->base);
+	else
+		kfree(job->hw_fence);
+	if (job->hw_vm_fence->base.ops)
+		dma_fence_put(&job->hw_vm_fence->base);
+	else
+		kfree(job->hw_vm_fence);
+
 	kfree(job);
 }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -0
index db66b4232de02..f8c67840f446f 100644
@@ -845,6 +845,8 @@ int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
 		if (r)
 			return r;
 		fence = &job->hw_vm_fence->base;
+		/* get a ref for the job */
+		dma_fence_get(fence);
 	}
 
 	if (vm_flush_needed) {
#3 lore raw
Quoted 16 lines · Christian König
Quoted 14 lines · Alex Deucher

If we don't end up initializing the fences, free them when we free the job.

v2: take a reference to the fences if we emit them

Fixes db36632ea51e ("drm/amdgpu: clean up and unify hw fence handling")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c  |  2 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_job.c | 18 ++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  |  2 ++
 3 files changed, 22 insertions(+)
Quoted 12 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs, · line 306

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c +2 -0
index 39229ece83f83..0596114377600 100644
@@ -302,6 +302,8 @@ int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs,
              return r;
      }
      *f = &af->base;
+     /* get a ref for the job */
+     dma_fence_get(*f);

I think it would be better to set the fence inside the job to NULL as soon as it is consumed/initialized.

We need the pointer for the job timed out handling.

Alex

Quoted 60 lines
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c +0 -0
      if (ring->funcs->insert_end)
              ring->funcs->insert_end(ring);
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +0 -0
index 55c7e104d5ca0..dc970f5fe601b 100644
@@ -295,6 +295,15 @@ static void amdgpu_job_free_cb(struct drm_sched_job *s_job)
 
      amdgpu_sync_free(&job->explicit_sync);

drivers/gpu/drm/amd/amdgpu/amdgpu_job.c static void amdgpu_job_free_cb(struct drm_sched_job *s_job) · line 306

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +9 -0
 
+     if (job->hw_fence->base.ops)
+             dma_fence_put(&job->hw_fence->base);
+     else
+             kfree(job->hw_fence);
+     if (job->hw_vm_fence->base.ops)
+             dma_fence_put(&job->hw_vm_fence->base);
+     else
+             kfree(job->hw_vm_fence);
+

This way that here can just be a kfree(..).

Regards,
Christian.

Quoted 32 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +9 -0
      kfree(job);
 }
 
@@ -324,6 +333,15 @@ void amdgpu_job_free(struct amdgpu_job *job)
      if (job->gang_submit != &job->base.s_fence->scheduled)
              dma_fence_put(job->gang_submit);
 
+     if (job->hw_fence->base.ops)
+             dma_fence_put(&job->hw_fence->base);
+     else
+             kfree(job->hw_fence);
+     if (job->hw_vm_fence->base.ops)
+             dma_fence_put(&job->hw_vm_fence->base);
+     else
+             kfree(job->hw_vm_fence);
+
      kfree(job);
 }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -0
index db66b4232de02..f8c67840f446f 100644
@@ -845,6 +845,8 @@ int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
              if (r)
                      return r;
              fence = &job->hw_vm_fence->base;
+             /* get a ref for the job */
+             dma_fence_get(fence);
      }
 
      if (vm_flush_needed) {
maintainer
#4 lore raw
Quoted 31 lines · Alex Deucher
Quoted 16 lines · Christian König
Quoted 14 lines · Alex Deucher

If we don't end up initializing the fences, free them when we free the job.

v2: take a reference to the fences if we emit them

Fixes db36632ea51e ("drm/amdgpu: clean up and unify hw fence handling")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c  |  2 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_job.c | 18 ++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  |  2 ++
 3 files changed, 22 insertions(+)
Quoted 12 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs, · line 306

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c +2 -0
index 39229ece83f83..0596114377600 100644
@@ -302,6 +302,8 @@ int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs,
              return r;
      }
      *f = &af->base;
+     /* get a ref for the job */
+     dma_fence_get(*f);

I think it would be better to set the fence inside the job to NULL as soon as it is consumed/initialized.

Quoted 1 line

We need the pointer for the job timed out handling.

I don't think that is true. During a timeout we should have job->s_fence->parent for the HW fence.

But even when we go down that route here, you only grab a reference to the hw_fence but not the hw_vm_fence.

That looks broken to me.

Christian.

Quoted 63 lines

Alex

Quoted 60 lines
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c +0 -0
      if (ring->funcs->insert_end)
              ring->funcs->insert_end(ring);
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +0 -0
index 55c7e104d5ca0..dc970f5fe601b 100644
@@ -295,6 +295,15 @@ static void amdgpu_job_free_cb(struct drm_sched_job *s_job)
 
      amdgpu_sync_free(&job->explicit_sync);

drivers/gpu/drm/amd/amdgpu/amdgpu_job.c static void amdgpu_job_free_cb(struct drm_sched_job *s_job) · line 306

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +9 -0
 
+     if (job->hw_fence->base.ops)
+             dma_fence_put(&job->hw_fence->base);
+     else
+             kfree(job->hw_fence);
+     if (job->hw_vm_fence->base.ops)
+             dma_fence_put(&job->hw_vm_fence->base);
+     else
+             kfree(job->hw_vm_fence);
+

This way that here can just be a kfree(..).

Regards,
Christian.

Quoted 32 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +9 -0
      kfree(job);
 }
 
@@ -324,6 +333,15 @@ void amdgpu_job_free(struct amdgpu_job *job)
      if (job->gang_submit != &job->base.s_fence->scheduled)
              dma_fence_put(job->gang_submit);
 
+     if (job->hw_fence->base.ops)
+             dma_fence_put(&job->hw_fence->base);
+     else
+             kfree(job->hw_fence);
+     if (job->hw_vm_fence->base.ops)
+             dma_fence_put(&job->hw_vm_fence->base);
+     else
+             kfree(job->hw_vm_fence);
+
      kfree(job);
 }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -0
index db66b4232de02..f8c67840f446f 100644
@@ -845,6 +845,8 @@ int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
              if (r)
                      return r;
              fence = &job->hw_vm_fence->base;
+             /* get a ref for the job */
+             dma_fence_get(fence);
      }
 
      if (vm_flush_needed) {
#5 lore raw
Quoted 33 lines · Christian König
Quoted 31 lines · Alex Deucher
Quoted 28 lines · Christian König
Quoted 19 lines · Alex Deucher

If we don't end up initializing the fences, free them when we free the job.

v2: take a reference to the fences if we emit them

Fixes db36632ea51e ("drm/amdgpu: clean up and unify hw fence handling")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c  |  2 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_job.c | 18 ++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  |  2 ++
 3 files changed, 22 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c +0 -0
index 39229ece83f83..0596114377600 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs, · line 306

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c +2 -0
@@ -302,6 +302,8 @@ int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs,
              return r;
      }
      *f = &af->base;
+     /* get a ref for the job */
+     dma_fence_get(*f);

I think it would be better to set the fence inside the job to NULL as soon as it is consumed/initialized.

Quoted 7 lines
Quoted 1 line

We need the pointer for the job timed out handling.

I don't think that is true. During a timeout we should have job->s_fence->parent for the HW fence.

But even when we go down that route here, you only grab a reference to the hw_fence but not the hw_vm_fence.

That looks broken to me.

I also grab a reference to the vm fence. See the last hunk of the patch below.

Alex

Quoted 67 lines

Christian.

Quoted 63 lines

Alex

Quoted 60 lines
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c +0 -0
      if (ring->funcs->insert_end)
              ring->funcs->insert_end(ring);
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +0 -0
index 55c7e104d5ca0..dc970f5fe601b 100644
@@ -295,6 +295,15 @@ static void amdgpu_job_free_cb(struct drm_sched_job *s_job)
 
      amdgpu_sync_free(&job->explicit_sync);

drivers/gpu/drm/amd/amdgpu/amdgpu_job.c static void amdgpu_job_free_cb(struct drm_sched_job *s_job) · line 306

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +9 -0
 
+     if (job->hw_fence->base.ops)
+             dma_fence_put(&job->hw_fence->base);
+     else
+             kfree(job->hw_fence);
+     if (job->hw_vm_fence->base.ops)
+             dma_fence_put(&job->hw_vm_fence->base);
+     else
+             kfree(job->hw_vm_fence);
+

This way that here can just be a kfree(..).

Regards,
Christian.

Quoted 32 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +9 -0
      kfree(job);
 }
 
@@ -324,6 +333,15 @@ void amdgpu_job_free(struct amdgpu_job *job)
      if (job->gang_submit != &job->base.s_fence->scheduled)
              dma_fence_put(job->gang_submit);
 
+     if (job->hw_fence->base.ops)
+             dma_fence_put(&job->hw_fence->base);
+     else
+             kfree(job->hw_fence);
+     if (job->hw_vm_fence->base.ops)
+             dma_fence_put(&job->hw_vm_fence->base);
+     else
+             kfree(job->hw_vm_fence);
+
      kfree(job);
 }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -0
index db66b4232de02..f8c67840f446f 100644
@@ -845,6 +845,8 @@ int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
              if (r)
                      return r;
              fence = &job->hw_vm_fence->base;
+             /* get a ref for the job */
+             dma_fence_get(fence);
      }
 
      if (vm_flush_needed) {
#6 lore raw
In reply to Christian König
Quoted 33 lines · Christian König
Quoted 31 lines · Alex Deucher
Quoted 28 lines · Christian König
Quoted 19 lines · Alex Deucher

If we don't end up initializing the fences, free them when we free the job.

v2: take a reference to the fences if we emit them

Fixes db36632ea51e ("drm/amdgpu: clean up and unify hw fence handling")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c  |  2 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_job.c | 18 ++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  |  2 ++
 3 files changed, 22 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c +0 -0
index 39229ece83f83..0596114377600 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs, · line 306

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c +2 -0
@@ -302,6 +302,8 @@ int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs,
              return r;
      }
      *f = &af->base;
+     /* get a ref for the job */
+     dma_fence_get(*f);

I think it would be better to set the fence inside the job to NULL as soon as it is consumed/initialized.

Quoted 3 lines
Quoted 1 line

We need the pointer for the job timed out handling.

I don't think that is true. During a timeout we should have job->s_fence->parent for the HW fence.

We also need to keep it around for job_submit_direct() so we can free the IBs used for that.

Alex

Quoted 71 lines

But even when we go down that route here, you only grab a reference to the hw_fence but not the hw_vm_fence.

That looks broken to me.

Christian.

Quoted 63 lines

Alex

Quoted 60 lines
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c +0 -0
      if (ring->funcs->insert_end)
              ring->funcs->insert_end(ring);
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +0 -0
index 55c7e104d5ca0..dc970f5fe601b 100644
@@ -295,6 +295,15 @@ static void amdgpu_job_free_cb(struct drm_sched_job *s_job)
 
      amdgpu_sync_free(&job->explicit_sync);

drivers/gpu/drm/amd/amdgpu/amdgpu_job.c static void amdgpu_job_free_cb(struct drm_sched_job *s_job) · line 306

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +9 -0
 
+     if (job->hw_fence->base.ops)
+             dma_fence_put(&job->hw_fence->base);
+     else
+             kfree(job->hw_fence);
+     if (job->hw_vm_fence->base.ops)
+             dma_fence_put(&job->hw_vm_fence->base);
+     else
+             kfree(job->hw_vm_fence);
+

This way that here can just be a kfree(..).

Regards,
Christian.

Quoted 32 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +9 -0
      kfree(job);
 }
 
@@ -324,6 +333,15 @@ void amdgpu_job_free(struct amdgpu_job *job)
      if (job->gang_submit != &job->base.s_fence->scheduled)
              dma_fence_put(job->gang_submit);
 
+     if (job->hw_fence->base.ops)
+             dma_fence_put(&job->hw_fence->base);
+     else
+             kfree(job->hw_fence);
+     if (job->hw_vm_fence->base.ops)
+             dma_fence_put(&job->hw_vm_fence->base);
+     else
+             kfree(job->hw_vm_fence);
+
      kfree(job);
 }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -0
index db66b4232de02..f8c67840f446f 100644
@@ -845,6 +845,8 @@ int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
              if (r)
                      return r;
              fence = &job->hw_vm_fence->base;
+             /* get a ref for the job */
+             dma_fence_get(fence);
      }
 
      if (vm_flush_needed) {
maintainer
#7 lore raw
Quoted 35 lines · Alex Deucher
Quoted 33 lines · Christian König
Quoted 31 lines · Alex Deucher
Quoted 28 lines · Christian König
Quoted 19 lines · Alex Deucher

If we don't end up initializing the fences, free them when we free the job.

v2: take a reference to the fences if we emit them

Fixes db36632ea51e ("drm/amdgpu: clean up and unify hw fence handling")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c  |  2 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_job.c | 18 ++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  |  2 ++
 3 files changed, 22 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c +0 -0
index 39229ece83f83..0596114377600 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs, · line 306

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c +2 -0
@@ -302,6 +302,8 @@ int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs,
              return r;
      }
      *f = &af->base;
+     /* get a ref for the job */
+     dma_fence_get(*f);

I think it would be better to set the fence inside the job to NULL as soon as it is consumed/initialized.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

We need the pointer for the job timed out handling.

I don't think that is true. During a timeout we should have job->s_fence->parent for the HW fence.

We also need to keep it around for job_submit_direct() so we can free the IBs used for that.

Good point, but that handling here is really not straight forward.

Anyway feel free to add my rb for now, but we need to re-visite that at some point.

Regards,
Christian.

Quoted 74 lines

Alex

Quoted 71 lines

But even when we go down that route here, you only grab a reference to the hw_fence but not the hw_vm_fence.

That looks broken to me.

Christian.

Quoted 63 lines

Alex

Quoted 60 lines
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c +0 -0
      if (ring->funcs->insert_end)
              ring->funcs->insert_end(ring);
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +0 -0
index 55c7e104d5ca0..dc970f5fe601b 100644
@@ -295,6 +295,15 @@ static void amdgpu_job_free_cb(struct drm_sched_job *s_job)
 
      amdgpu_sync_free(&job->explicit_sync);

drivers/gpu/drm/amd/amdgpu/amdgpu_job.c static void amdgpu_job_free_cb(struct drm_sched_job *s_job) · line 306

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +9 -0
 
+     if (job->hw_fence->base.ops)
+             dma_fence_put(&job->hw_fence->base);
+     else
+             kfree(job->hw_fence);
+     if (job->hw_vm_fence->base.ops)
+             dma_fence_put(&job->hw_vm_fence->base);
+     else
+             kfree(job->hw_vm_fence);
+

This way that here can just be a kfree(..).

Regards,
Christian.

Quoted 32 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +9 -0
      kfree(job);
 }
 
@@ -324,6 +333,15 @@ void amdgpu_job_free(struct amdgpu_job *job)
      if (job->gang_submit != &job->base.s_fence->scheduled)
              dma_fence_put(job->gang_submit);
 
+     if (job->hw_fence->base.ops)
+             dma_fence_put(&job->hw_fence->base);
+     else
+             kfree(job->hw_fence);
+     if (job->hw_vm_fence->base.ops)
+             dma_fence_put(&job->hw_vm_fence->base);
+     else
+             kfree(job->hw_vm_fence);
+
      kfree(job);
 }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -0
index db66b4232de02..f8c67840f446f 100644
@@ -845,6 +845,8 @@ int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
              if (r)
                      return r;
              fence = &job->hw_vm_fence->base;
+             /* get a ref for the job */
+             dma_fence_get(fence);
      }
 
      if (vm_flush_needed) {
#8 lore raw
Quoted 39 lines · Christian König
Quoted 37 lines · Alex Deucher
Quoted 35 lines · Christian König
Quoted 32 lines · Alex Deucher
Quoted 16 lines · Christian König
Quoted 14 lines · Alex Deucher

If we don't end up initializing the fences, free them when we free the job.

v2: take a reference to the fences if we emit them

Fixes db36632ea51e ("drm/amdgpu: clean up and unify hw fence handling")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c  |  2 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_job.c | 18 ++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  |  2 ++
 3 files changed, 22 insertions(+)
Quoted 12 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs, · line 306

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c +2 -0
index 39229ece83f83..0596114377600 100644
@@ -302,6 +302,8 @@ int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs,
              return r;
      }
      *f = &af->base;
+     /* get a ref for the job */
+     dma_fence_get(*f);

I think it would be better to set the fence inside the job to NULL as soon as it is consumed/initialized.

We need the pointer for the job timed out handling.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

I don't think that is true. During a timeout we should have job->s_fence->parent for the HW fence.

We also need to keep it around for job_submit_direct() so we can free the IBs used for that.

Good point, but that handling here is really not straight forward.

Anyway feel free to add my rb for now, but we need to re-visite that at some point.

Thanks. I found a leak of the non-job fence. Please see the latest revision of the patch.

Alex

Quoted 79 lines

Regards,
Christian.

Quoted 74 lines

Alex

Quoted 71 lines

But even when we go down that route here, you only grab a reference to the hw_fence but not the hw_vm_fence.

That looks broken to me.

Christian.

Quoted 63 lines

Alex

Quoted 60 lines
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c +0 -0
      if (ring->funcs->insert_end)
              ring->funcs->insert_end(ring);
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +0 -0
index 55c7e104d5ca0..dc970f5fe601b 100644
@@ -295,6 +295,15 @@ static void amdgpu_job_free_cb(struct drm_sched_job *s_job)
 
      amdgpu_sync_free(&job->explicit_sync);

drivers/gpu/drm/amd/amdgpu/amdgpu_job.c static void amdgpu_job_free_cb(struct drm_sched_job *s_job) · line 306

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +9 -0
 
+     if (job->hw_fence->base.ops)
+             dma_fence_put(&job->hw_fence->base);
+     else
+             kfree(job->hw_fence);
+     if (job->hw_vm_fence->base.ops)
+             dma_fence_put(&job->hw_vm_fence->base);
+     else
+             kfree(job->hw_vm_fence);
+

This way that here can just be a kfree(..).

Regards,
Christian.

Quoted 32 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +9 -0
      kfree(job);
 }
 
@@ -324,6 +333,15 @@ void amdgpu_job_free(struct amdgpu_job *job)
      if (job->gang_submit != &job->base.s_fence->scheduled)
              dma_fence_put(job->gang_submit);
 
+     if (job->hw_fence->base.ops)
+             dma_fence_put(&job->hw_fence->base);
+     else
+             kfree(job->hw_fence);
+     if (job->hw_vm_fence->base.ops)
+             dma_fence_put(&job->hw_vm_fence->base);
+     else
+             kfree(job->hw_vm_fence);
+
      kfree(job);
 }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -0
index db66b4232de02..f8c67840f446f 100644
@@ -845,6 +845,8 @@ int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
              if (r)
                      return r;
              fence = &job->hw_vm_fence->base;
+             /* get a ref for the job */
+             dma_fence_get(fence);
      }
 
      if (vm_flush_needed) {