Skip to contentSkip to conversation

drm/amdkfd: add missing return value check for range

PATCH In review Sunil Khatri 5 participants 7 messages Oct 24, 2025

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

PATCH v2 1/1 drm/amdkfd: add missing return value check for range

amdgpu_hmm_range_alloc could fails in case of low
memory condition and hence we should have a check
for the return value.

---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +5 -0
index f041643308ca..7f0ab73e2396 100644
@@ -1738,6 +1738,11 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
 
 			WRITE_ONCE(p->svms.faulting_task, current);
 			range = amdgpu_hmm_range_alloc(NULL);
+			if (unlikely(!range)) {
+				r = -ENOMEM;
+				goto free_ctx;
+			}
+
 			r = amdgpu_hmm_range_get_pages(&prange->notifier, addr, npages,
 						       readonly, owner,
 						       range);
Signature · 1 line
2.34.1
#1 lore raw

+shirish

Quoted 25 lines · Sunil Khatri

amdgpu_hmm_range_alloc could fails in case of low
memory condition and hence we should have a check
for the return value.

---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 5 +++++
  1 file changed, 5 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +5 -0
index f041643308ca..7f0ab73e2396 100644
@@ -1738,6 +1738,11 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
 
  			WRITE_ONCE(p->svms.faulting_task, current);
  			range = amdgpu_hmm_range_alloc(NULL);
+			if (unlikely(!range)) {
+				r = -ENOMEM;
+				goto free_ctx;
+			}
+
  			r = amdgpu_hmm_range_get_pages(&prange->notifier, addr, npages,
  						       readonly, owner,
  						       range);
Reviewed-by
#2 lore raw

[AMD Official Use Only - AMD Internal Distribution Only]

Regards,
Shirish

-----Original Message-----
From: Khatri, Sunil <[email protected]>
Sent: Friday, October 24, 2025 5:15 PM
To: Khatri, Sunil <[email protected]>; Koenig, Christian <[email protected]>; Kuehling, Felix <[email protected]>; Deucher, Alexander <[email protected]>; Paneer Selvam, Arunpravin <[email protected]>; Yang, Philip <[email protected]>; [email protected]; S, Shirish <[email protected]>
Subject: Re: [Patch v2] drm/amdkfd: add missing return value check for range

+shirish

Quoted 26 lines · Sunil Khatri

amdgpu_hmm_range_alloc could fails in case of low memory condition and hence we should have a check for the return value.

---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 5 +++++
  1 file changed, 5 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +5 -0
index f041643308ca..7f0ab73e2396 100644
@@ -1738,6 +1738,11 @@ static int svm_range_validate_and_map(struct
 mm_struct *mm,
 
                      WRITE_ONCE(p->svms.faulting_task, current);
                      range = amdgpu_hmm_range_alloc(NULL);
+                     if (unlikely(!range)) {
+                             r = -ENOMEM;
+                             goto free_ctx;
+                     }
+
                      r = amdgpu_hmm_range_get_pages(&prange->notifier, addr, npages,
                                                     readonly, owner,
                                                     range);
#3 lore raw
In reply to Khatri, Sunil
Quoted 12 lines · Sunil

+shirish

Quoted 9 lines · Sunil Khatri

amdgpu_hmm_range_alloc could fails in case of low
memory condition and hence we should have a check
for the return value.

---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 5 +++++

  1 file changed, 5 insertions(+)
Quoted 11 lines

drivers/gpu/drm/amd/amdkfd/kfd_svm.c static int svm_range_validate_and_map(struct · line 1743

Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +3 -0
index f041643308ca..7f0ab73e2396 100644
@@ -1738,6 +1738,11 @@ static int svm_range_validate_and_map(struct 
 mm_struct *mm,
                 WRITE_ONCE(p->svms.faulting_task, current);
               range = amdgpu_hmm_range_alloc(NULL);
+            if (unlikely(!range)) {
+                r = -ENOMEM;
+                goto free_ctx;

Can not goto end from here,  this skips the svm_range_unreserve_bos. Just set r = -ENOMEM, and the loop will exit and cleanup accordingly.

Regards,

Philip

Quoted 5 lines
Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -0
+            }
+
               r = amdgpu_hmm_range_get_pages(&prange->notifier, addr, npages,
                                  readonly, owner,
                                  range);
#4 lore raw
Quoted 14 lines · Philip Yang
Quoted 12 lines · Sunil

+shirish

Quoted 9 lines · Sunil Khatri

amdgpu_hmm_range_alloc could fails in case of low
memory condition and hence we should have a check
for the return value.

---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 5 +++++

  1 file changed, 5 insertions(+)
Quoted 13 lines
Quoted 11 lines

drivers/gpu/drm/amd/amdkfd/kfd_svm.c static int svm_range_validate_and_map(struct · line 1743

Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +3 -0
index f041643308ca..7f0ab73e2396 100644
@@ -1738,6 +1738,11 @@ static int svm_range_validate_and_map(struct 
 mm_struct *mm,
                 WRITE_ONCE(p->svms.faulting_task, current);
               range = amdgpu_hmm_range_alloc(NULL);
+            if (unlikely(!range)) {
+                r = -ENOMEM;
+                goto free_ctx;

Can not goto end from here,  this skips the svm_range_unreserve_bos. Just set r = -ENOMEM, and the loop will exit and cleanup accordingly.

That patch has already been merged now and i have to send another patch for review. While at it, cannot just set ENOMEM and leave as it will crash in next step where it access range which is NULL.

This is what i am thinking
range = amdgpu_hmm_range_alloc(NULL);
if (unlikely(!range)) {
                r = -ENOMEM;
                svm_range_unreserve_bos(ctx);
                goto free_ctx;
}

If that is fine i will send a new patch for this ?

Regards
Sunil Khatri

Quoted 10 lines

Regards,

Philip

Quoted 5 lines
Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -0
+            }
+
               r = amdgpu_hmm_range_get_pages(&prange->notifier, addr, npages,
                                  readonly, owner,
                                  range);
maintainer
#5 lore raw
Quoted 40 lines · Sunil
Quoted 14 lines · Philip Yang
Quoted 12 lines · Sunil

+shirish

Quoted 9 lines · Sunil Khatri

amdgpu_hmm_range_alloc could fails in case of low
memory condition and hence we should have a check
for the return value.

---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 5 +++++

  1 file changed, 5 insertions(+)
Quoted 13 lines
Quoted 11 lines

drivers/gpu/drm/amd/amdkfd/kfd_svm.c static int svm_range_validate_and_map(struct · line 1743

Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +3 -0
index f041643308ca..7f0ab73e2396 100644
@@ -1738,6 +1738,11 @@ static int svm_range_validate_and_map(struct 
 mm_struct *mm,
                 WRITE_ONCE(p->svms.faulting_task, current);
               range = amdgpu_hmm_range_alloc(NULL);
+            if (unlikely(!range)) {
+                r = -ENOMEM;
+                goto free_ctx;

Can not goto end from here,  this skips the svm_range_unreserve_bos. Just set r = -ENOMEM, and the loop will exit and cleanup accordingly.

That patch has already been merged now and i have to send another patch for review. While at it, cannot just set ENOMEM and leave as it will crash in next step where it access range which is NULL.

This is what i am thinking
range = amdgpu_hmm_range_alloc(NULL);
if (unlikely(!range)) {
                r = -ENOMEM;
                svm_range_unreserve_bos(ctx);
                goto free_ctx;
}

Quoted 1 line

If that is fine i will send a new patch for this ?

I'd recommend

	if (unlikely(!range)) {
		r = -ENOMEM;
		break;
	}

Regards,
  Felix

Quoted 14 lines

Regards
Sunil Khatri

Quoted 10 lines

Regards,

Philip

Quoted 5 lines
Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -0
+            }
+
               r = amdgpu_hmm_range_get_pages(&prange->notifier, addr, npages,
                                  readonly, owner,
                                  range);
#6 lore raw
Quoted 43 lines · Felix
Quoted 40 lines · Sunil
Quoted 14 lines · Philip Yang
Quoted 12 lines · Sunil

+shirish

Quoted 9 lines · Sunil Khatri

amdgpu_hmm_range_alloc could fails in case of low
memory condition and hence we should have a check
for the return value.

---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 5 +++++

  1 file changed, 5 insertions(+)
Quoted 13 lines
Quoted 11 lines

drivers/gpu/drm/amd/amdkfd/kfd_svm.c static int · line 1743

Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +3 -0
index f041643308ca..7f0ab73e2396 100644
@@ -1738,6 +1738,11 @@ static int 
 svm_range_validate_and_map(struct mm_struct *mm,
                 WRITE_ONCE(p->svms.faulting_task, current);
               range = amdgpu_hmm_range_alloc(NULL);
+            if (unlikely(!range)) {
+                r = -ENOMEM;
+                goto free_ctx;

Can not goto end from here,  this skips the svm_range_unreserve_bos. Just set r = -ENOMEM, and the loop will exit and cleanup accordingly.

That patch has already been merged now and i have to send another patch for review. While at it, cannot just set ENOMEM and leave as it will crash in next step where it access range which is NULL.

This is what i am thinking
range = amdgpu_hmm_range_alloc(NULL);
if (unlikely(!range)) {
                r = -ENOMEM;
                svm_range_unreserve_bos(ctx);
                goto free_ctx;
}

Quoted 8 lines
Quoted 1 line

If that is fine i will send a new patch for this ?

I'd recommend

	if (unlikely(!range)) {
		r = -ENOMEM;
		break;
	}

Yup, this would work too and is a better approach. Sent the new patch "[Patch v2] drm/amdkfd: fix the clean up when amdgpu_hmm_range_alloc fails"

Regards
Sunil khatri

Quoted 18 lines

Regards,
  Felix

Quoted 14 lines

Regards
Sunil Khatri

Quoted 10 lines

Regards,

Philip

Quoted 5 lines
Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -0
+            }
+
               r = amdgpu_hmm_range_get_pages(&prange->notifier, addr, npages,
                                  readonly, owner,
                                  range);