drm/amdgpu: null check for hmm_pfns ptr before freeing it
Subsystems (2)
PATCH 1/1 drm/amdgpu: null check for hmm_pfns ptr before freeing it
Due to low memory or when num of pages is too big to be accomodated, allocation could fail for pfn's.
Chekc hmm_pfns for NULL before calling the kvfree for the it.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +5 -1
@@ -286,7 +286,11 @@ void amdgpu_hmm_range_free(struct amdgpu_hmm_range *range)if (!range)return ; -kvfree (range->hmm_range.hmm_pfns); +if (range->hmm_range.hmm_pfns) { +kvfree (range->hmm_range.hmm_pfns); + range->hmm_range.hmm_pfns =NULL ; + } +amdgpu_bo_unref (&range->bo);kfree (range); }
Signature · 1 line
2.34.1
Regards,
Arun.
Quoted 27 lines · Sunil Khatri
Due to low memory or when num of pages is too big to be accomodated, allocation could fail for pfn's.
Chekc
hmm_pfnsfor NULL before calling the kvfree for the it.Signed-off-by Sunil Khatri <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +5
-1@@ -286,7 +286,11 @@ void amdgpu_hmm_range_free(struct amdgpu_hmm_range *range)if (!range)return ; -kvfree (range->hmm_range.hmm_pfns); +if (range->hmm_range.hmm_pfns) { +kvfree (range->hmm_range.hmm_pfns); + range->hmm_range.hmm_pfns =NULL ; + } +amdgpu_bo_unref (&range->bo);kfree (range); }
Quoted 15 lines · Arunpravin Paneer Selvam
Regards,
Arun.Quoted 10 lines · Sunil Khatri
Due to low memory or when num of pages is too big to be accomodated, allocation could fail for pfn's.
Chekc
hmm_pfnsfor NULL before calling the kvfree for the it.Signed-off-by Sunil Khatri <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c| 6 +++++-1 file changed, 5 insertions(+), 1 deletion(-)
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c void amdgpu_hmm_range_free(struct · line 293
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +4
-0@@ -286,7 +286,11 @@ void amdgpu_hmm_range_free(struct amdgpu_hmm_range *range) if (!range) return; - kvfree(range->hmm_range.hmm_pfns); + if (range->hmm_range.hmm_pfns) { + kvfree(range->hmm_range.hmm_pfns); + range->hmm_range.hmm_pfns = NULL; + }
NULL-checks before kfree and friends are unnecessary. There are actually static checkers that complain about such unnecessary NULL-checks. For example, see https://lkml.org/lkml/2024/8/11/168.
The same is also true for the standard libc free in usermode: https://stackoverflow.com/questions/1912325/checking-for-null-before-calling-free.
Finally, setting range->hmm_range.hmm_pfns = NULL is also unnecessary because you're about to free the whole range structure anyway.
Regards,
Felix
Quoted 5 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +1
-0+ amdgpu_bo_unref(&range->bo); kfree(range);}
Christian König
maintainerQuoted 32 lines · Felix
Quoted 15 lines · Arunpravin Paneer Selvam
Regards,
Arun.Quoted 10 lines · Sunil Khatri
Due to low memory or when num of pages is too big to be accomodated, allocation could fail for pfn's.
Chekc
hmm_pfnsfor NULL before calling the kvfree for the it.Signed-off-by Sunil Khatri <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c| 6 +++++-1 file changed, 5 insertions(+), 1 deletion(-)Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c void amdgpu_hmm_range_free(struct amdgpu_hmm_range *range) · line 292
Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +4
-0@@ -286,7 +286,11 @@ void amdgpu_hmm_range_free(struct amdgpu_hmm_range *range) if (!range) return; - kvfree(range->hmm_range.hmm_pfns); + if (range->hmm_range.hmm_pfns) { + kvfree(range->hmm_range.hmm_pfns); + range->hmm_range.hmm_pfns = NULL; + }NULL-checks before kfree and friends are unnecessary. There are actually static checkers that complain about such unnecessary NULL-checks. For example, see https://lkml.org/lkml/2024/8/11/168.
Quoted 3 lines
The same is also true for the standard libc free in usermode: https://stackoverflow.com/questions/1912325/checking-for-null-before-calling-free.
Finally, setting
range->hmm_range.hmm_pfns= NULL is also unnecessary because you're about to free the whole range structure anyway.
Agree completely with Felix.
Sunil why do you think that this is necessary and blocking KFD for some reason?
Regards,
Christian.
Quoted 10 lines
Regards,
FelixQuoted 5 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +1
-0+ amdgpu_bo_unref(&range->bo); kfree(range); }
Quoted 34 lines · Christian König
Quoted 32 lines · Felix
Quoted 15 lines · Arunpravin Paneer Selvam
Regards,
Arun.Quoted 10 lines · Sunil Khatri
Due to low memory or when num of pages is too big to be accomodated, allocation could fail for pfn's.
Chekc
hmm_pfnsfor NULL before calling the kvfree for the it.Signed-off-by Sunil Khatri<[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c void amdgpu_hmm_range_free(struct amdgpu_hmm_range *range) · line 292
Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +4
-0@@ -286,7 +286,11 @@ void amdgpu_hmm_range_free(struct amdgpu_hmm_range *range) if (!range) return; - kvfree(range->hmm_range.hmm_pfns); + if (range->hmm_range.hmm_pfns) { + kvfree(range->hmm_range.hmm_pfns); + range->hmm_range.hmm_pfns = NULL; + }NULL-checks before kfree and friends are unnecessary. There are actually static checkers that complain about such unnecessary NULL-checks. For example, seehttps://lkml.org/lkml/2024/8/11/168.
The same is also true for the standard libc free in usermode:https://stackoverflow.com/questions/1912325/checking-for-null-before-calling-free.
Quoted 7 lines
Quoted 1 line
Finally, setting
range->hmm_range.hmm_pfns= NULL is also unnecessary because you're about to free the whole range structure anyway.Agree completely with Felix.
Sunil why do you think that this is necessary and blocking KFD for some reason?
Regards,
Christian.
KFD side reported the error of NULL dereference
pfns = kvmalloc_array(npages, sizeof(*pfns), GFP_KERNEL); //fails if the size is too big.
Now when we free the memory in function amdgpu_hmm_range_free and try to do a kvfree of the range->hmm_range.hmm_pfns which is NULL and we were seeing the NULL dereference. So i added a check to check for the memory to be valid ptr first before calling kvfree.
This actually fixed the issue but i do agree that *"setting range->hmm_range.hmm_pfns = NULL could be avoided and that why i did not added that check in the final patch that i merged" This is the final code after this merge.*
voidamdgpu_hmm_range_free(structamdgpu_hmm_range*range) {
if(!range)
return;if(range->hmm_range.hmm_pfns) kvfree(range->hmm_range.hmm_pfns); amdgpu_bo_unref(&range->bo);
kfree(range);
}
Regards Sunil Khatri
Quoted 8 lines
Quoted 8 lines
Regards,
FelixQuoted 4 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +1
-0+ amdgpu_bo_unref(&range->bo); kfree(range); }
Christian König
maintainerQuoted 50 lines · Sunil
Quoted 33 lines · Christian König
Quoted 32 lines · Felix
Quoted 15 lines · Arunpravin Paneer Selvam
Regards,
Arun.Quoted 10 lines · Sunil Khatri
Due to low memory or when num of pages is too big to be accomodated, allocation could fail for pfn's.
Chekc
hmm_pfnsfor NULL before calling the kvfree for the it.Signed-off-by Sunil Khatri <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c| 6 +++++-1 file changed, 5 insertions(+), 1 deletion(-)Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c void amdgpu_hmm_range_free(struct amdgpu_hmm_range *range) · line 292
Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +4
-0@@ -286,7 +286,11 @@ void amdgpu_hmm_range_free(struct amdgpu_hmm_range *range) if (!range) return; - kvfree(range->hmm_range.hmm_pfns); + if (range->hmm_range.hmm_pfns) { + kvfree(range->hmm_range.hmm_pfns); + range->hmm_range.hmm_pfns = NULL; + }NULL-checks before kfree and friends are unnecessary. There are actually static checkers that complain about such unnecessary NULL-checks. For example, see https://lkml.org/lkml/2024/8/11/168.
The same is also true for the standard libc free in usermode: https://stackoverflow.com/questions/1912325/checking-for-null-before-calling-free.
Quoted 7 lines
Quoted 1 line
Finally, setting
range->hmm_range.hmm_pfns= NULL is also unnecessary because you're about to free the whole range structure anyway.Agree completely with Felix.
Sunil why do you think that this is necessary and blocking KFD for some reason?
Regards,
Christian.KFD side reported the error of NULL dereference
pfns =
kvmalloc_array(npages, sizeof(*pfns), GFP_KERNEL); //fails if the size is too big.Now when we free the memory in function
amdgpu_hmm_range_freeand try to do a kvfree of therange->hmm_range.hmm_pfnswhich is NULL and we were seeing the NULL dereference. So i added a check to check for the memory to be valid ptr first before calling kvfree.
Quoted 8 lines
This actually fixed the issue but i do agree that *"setting range->hmm_range.hmm_pfns = NULL could be avoided and that why i did not added that check in the final patch that i merged" This is the final code after this merge.*
voidamdgpu_hmm_range_free(structamdgpu_hmm_range*range){
if(!range)
return;if(range->hmm_range.hmm_pfns)kvfree(range->hmm_range.hmm_pfns);
That makes absolutely no sense kvfree() should be able to accept a NULL pointer as parameter. So clear NAK to that change.
What exactly does the backtrace look like?
Regards,
Christian.
Quoted 15 lines
amdgpu_bo_unref(&range->bo);
kfree(range);
}Regards Sunil Khatri
Quoted 8 lines
Quoted 8 lines
Regards,
FelixQuoted 4 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +1
-0+ amdgpu_bo_unref(&range->bo); kfree(range); }