Skip to contentSkip to conversation

drm/amdgpu: null check for hmm_pfns ptr before freeing it

PATCH In review Sunil Khatri 5 participants 6 messages Oct 23, 2025 – Oct 28, 2025

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

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
index d6f903a2d573..6ac206e2bc46 100644
@@ -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
Acked-by on v1 1/1
#1 lore raw

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_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
index d6f903a2d573..6ac206e2bc46 100644
@@ -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);
  }
#2 lore raw
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_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(-)
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
index d6f903a2d573..6ac206e2bc46 100644
@@ -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);

  }

maintainer
#3 lore raw
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_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(-)
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
index d6f903a2d573..6ac206e2bc46 100644
@@ -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,
  Felix

Quoted 5 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +1 -0
+
       amdgpu_bo_unref(&range->bo);
       kfree(range);
   }
#4 lore raw
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_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(-)
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
index d6f903a2d573..6ac206e2bc46 100644
@@ -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,
  Felix

Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +1 -0
+
       amdgpu_bo_unref(&range->bo);
       kfree(range);
   }
maintainer
#5 lore raw
Quoted 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_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(-)
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
index d6f903a2d573..6ac206e2bc46 100644
@@ -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_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.

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,
  Felix

Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +1 -0
+
       amdgpu_bo_unref(&range->bo);
       kfree(range);
   }