Skip to contentSkip to conversation

drm/amdgpu: free hw_vm_fence when fail in amdgpu_job_alloc

PATCH In review Jiqian Chen 3 participants 5 messages Jan 15

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 drm/amdgpu: free hw_vm_fence when fail in amdgpu_job_alloc

If drm_sched_job_init fails, hw_vm_fence is not freed currently, then cause memory leak.

---
v1->v2 changes:
* assign the return code of drm_sched_job_init and check that instead.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_job.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +5 -2
index 7f5d01164897..1daa9145b217 100644
@@ -219,8 +219,11 @@ int amdgpu_job_alloc(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	if (!entity)
 		return 0;
 
-	return drm_sched_job_init(&(*job)->base, entity, 1, owner,
-				  drm_client_id);
+	r = drm_sched_job_init(&(*job)->base, entity, 1, owner, drm_client_id);
+	if (!r)
+		return 0;
+
+	kfree((*job)->hw_vm_fence);
 
 err_fence:
 	kfree((*job)->hw_fence);
Signature · 1 line
2.34.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 17 lines · Jiqian Chen

If drm_sched_job_init fails, hw_vm_fence is not freed currently, then cause memory leak.

---
v1->v2 changes:
* assign the return code of drm_sched_job_init and check that instead.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_job.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +0 -0
index 7f5d01164897..1daa9145b217 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_job.c int amdgpu_job_alloc(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 220

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +0 -0
@@ -219,8 +219,11 @@ int amdgpu_job_alloc(struct amdgpu_device *adev, struct amdgpu_vm *vm,
        if (!entity)
                return 0;

I have a question that may not be related to the PATCH.

Why not check 'entity' at the beginning of function amdgpu_job_alloc()? Currently if the 'entity' is invalid, the allocated 3 structs won't be released.

drivers/gpu/drm/amd/amdgpu/amdgpu_job.c int amdgpu_job_alloc(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 226

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +5 -2
-       return drm_sched_job_init(&(*job)->base, entity, 1, owner,
-                                 drm_client_id);
+       r = drm_sched_job_init(&(*job)->base, entity, 1, owner, drm_client_id);
+       if (!r)
+               return 0;
+
+       kfree((*job)->hw_vm_fence);

The V2 looks good, it's already addressed the problem mentioned by Christian.

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +0 -1
 err_fence:
        kfree((*job)->hw_fence);
--

2.34.1

#2 lore raw
Quoted 23 lines · Amos Jianjun Kong
Quoted 17 lines · Jiqian Chen

If drm_sched_job_init fails, hw_vm_fence is not freed currently, then cause memory leak.

---
v1->v2 changes:
* assign the return code of drm_sched_job_init and check that instead.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_job.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +0 -0
index 7f5d01164897..1daa9145b217 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_job.c int amdgpu_job_alloc(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 220

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +0 -0
@@ -219,8 +219,11 @@ int amdgpu_job_alloc(struct amdgpu_device *adev, struct amdgpu_vm *vm,
        if (!entity)
                return 0;

I have a question that may not be related to the PATCH.

Quoted 2 lines

Why not check 'entity' at the beginning of function amdgpu_job_alloc()? Currently if the 'entity' is invalid, the allocated 3 structs won't be released.

What I can see is not all jobs need to be associated with an entity when they are allocated. It allows entity to be NULL when allocate a new job, then return the job, like amdgpu_amdkfd_submit_ib().

Quoted 10 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_job.c int amdgpu_job_alloc(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 226

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +5 -2
-       return drm_sched_job_init(&(*job)->base, entity, 1, owner,
-                                 drm_client_id);
+       r = drm_sched_job_init(&(*job)->base, entity, 1, owner, drm_client_id);
+       if (!r)
+               return 0;
+
+       kfree((*job)->hw_vm_fence);
Quoted 3 lines

The V2 looks good, it's already addressed the problem mentioned by Christian.

Thanks.

Quoted 6 lines
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +0 -1
 err_fence:
        kfree((*job)->hw_fence);
--

2.34.1

Signature · 2 lines
Best regards,
Jiqian Chen.
maintainer
#3 lore raw
In reply to Amos Jianjun Kong
Quoted 23 lines · Amos Jianjun Kong
Quoted 17 lines · Jiqian Chen

If drm_sched_job_init fails, hw_vm_fence is not freed currently, then cause memory leak.

---
v1->v2 changes:
* assign the return code of drm_sched_job_init and check that instead.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_job.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +0 -0
index 7f5d01164897..1daa9145b217 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_job.c int amdgpu_job_alloc(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 220

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +0 -0
@@ -219,8 +219,11 @@ int amdgpu_job_alloc(struct amdgpu_device *adev, struct amdgpu_vm *vm,
        if (!entity)
                return 0;

I have a question that may not be related to the PATCH.

Quoted 2 lines

Why not check 'entity' at the beginning of function amdgpu_job_alloc()? Currently if the 'entity' is invalid, the allocated 3 structs won't be released.

That you don't have an entity is not an error but normal condition.

It just means that the job is pushed directly to the HW without going through the scheduler.

Regards,
Christian.

Quoted 19 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_job.c int amdgpu_job_alloc(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 226

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +5 -2
-       return drm_sched_job_init(&(*job)->base, entity, 1, owner,
-                                 drm_client_id);
+       r = drm_sched_job_init(&(*job)->base, entity, 1, owner, drm_client_id);
+       if (!r)
+               return 0;
+
+       kfree((*job)->hw_vm_fence);

The V2 looks good, it's already addressed the problem mentioned by Christian.

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +0 -1
 err_fence:
        kfree((*job)->hw_fence);
--

2.34.1

maintainer Reviewed-by on v2 1/1
#4 lore raw
In reply to Jiqian Chen
Quoted 4 lines · Jiqian Chen

If drm_sched_job_init fails, hw_vm_fence is not freed currently, then cause memory leak.

Quoted 3 lines

---
v1->v2 changes:
* assign the return code of drm_sched_job_init and check that instead.

That's actually v3.

In general we keep the change log in the commit message for amdgpu changes, but doesn't really matter for such a trivial change.

Regards,
Christian.

Quoted 22 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_job.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +5 -2
index 7f5d01164897..1daa9145b217 100644
@@ -219,8 +219,11 @@ int amdgpu_job_alloc(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	if (!entity)
 		return 0;
 
-	return drm_sched_job_init(&(*job)->base, entity, 1, owner,
-				  drm_client_id);
+	r = drm_sched_job_init(&(*job)->base, entity, 1, owner, drm_client_id);
+	if (!r)
+		return 0;
+
+	kfree((*job)->hw_vm_fence);
 
 err_fence:
 	kfree((*job)->hw_fence);