Skip to contentSkip to conversation

drm/amdkfd: Handle lack of READ permissions in SVM mapping

PATCH In review Kent Russell 2 participants 4 messages Aug 5, 2025 – Aug 8, 2025

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

PATCH 1/1 drm/amdkfd: Handle lack of READ permissions in SVM mapping

HMM assumes that pages have READ permissions by default. Inside svm_range_validate_and_map, we add READ permissions then add WRITE permissions if the VMA isn't read-only. This will conflict with regions that only have PROT_WRITE or have PROT_NONE. When that happens, svm_range_restore_work will continue to retry, silently, giving the impression of a hang if pr_debug isn't enabled to show the retries..

If pages don't have READ permissions, simply unmap them and continue. If they weren't mapped in the first place, this would be a no-op. Since x86 doesn't support write-only, and PROT_NONE doesn't allow reads or writes anyways, this will allow the svm range validation to continue without getting stuck in a loop forever on mappings we can't use with HMM.

---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +22 -0
index e23b5a0f31f2..449595aab433 100644
@@ -1713,6 +1713,28 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
 
 			next = min(vma->vm_end, end);
 			npages = (next - addr) >> PAGE_SHIFT;
+			/* HMM requires at least READ permissions. If provided with PROT_NONE,
+			 * unmap the memory. If it's not already mapped, this is a no-op
+			 * If PROT_WRITE is provided without READ, warn first then unmap
+			 */
+			if (!(vma->vm_flags & VM_READ)) {
+				unsigned long e, s;
+
+				svm_range_lock(prange);
+				if (vma->vm_flags & VM_WRITE)
+					pr_debug("VM_WRITE without VM_READ is not supported");
+				s = max(start, prange->start);
+				e = min(end, prange->last);
+				if (e >= s)
+					r = svm_range_unmap_from_gpus(prange, s, e,
+						       KFD_SVM_UNMAP_TRIGGER_UNMAP_FROM_CPU);
+				addr = next;
+				svm_range_unlock(prange);
+				if (r)
+					return r;
+				continue;
+			}
+
 			WRITE_ONCE(p->svms.faulting_task, current);
 			r = amdgpu_hmm_range_get_pages(&prange->notifier, addr, npages,
 						       readonly, owner, NULL,
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 32 lines · Kent Russell

HMM assumes that pages have READ permissions by default. Inside svm_range_validate_and_map, we add READ permissions then add WRITE permissions if the VMA isn't read-only. This will conflict with regions that only have PROT_WRITE or have PROT_NONE. When that happens, svm_range_restore_work will continue to retry, silently, giving the impression of a hang if pr_debug isn't enabled to show the retries..

If pages don't have READ permissions, simply unmap them and continue. If they weren't mapped in the first place, this would be a no-op. Since x86 doesn't support write-only, and PROT_NONE doesn't allow reads or writes anyways, this will allow the svm range validation to continue without getting stuck in a loop forever on mappings we can't use with HMM.

---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 22 ++++++++++++++++++++++
  1 file changed, 22 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +6 -0
index e23b5a0f31f2..449595aab433 100644
@@ -1713,6 +1713,28 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
 
  			next = min(vma->vm_end, end);
  			npages = (next - addr) >> PAGE_SHIFT;
+			/* HMM requires at least READ permissions. If provided with PROT_NONE,
+			 * unmap the memory. If it's not already mapped, this is a no-op
+			 * If PROT_WRITE is provided without READ, warn first then unmap
+			 */
+			if (!(vma->vm_flags & VM_READ)) {
+				unsigned long e, s;

drivers/gpu/drm/amd/amdkfd/kfd_svm.c static int svm_range_validate_and_map(struct mm_struct *mm, · line 1731

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +10 -0
+
+				svm_range_lock(prange);
+				if (vma->vm_flags & VM_WRITE)
+					pr_debug("VM_WRITE without VM_READ is not supported");
+				s = max(start, prange->start);
+				e = min(end, prange->last);
+				if (e >= s)
+					r = svm_range_unmap_from_gpus(prange, s, e,
+						       KFD_SVM_UNMAP_TRIGGER_UNMAP_FROM_CPU);
+				addr = next;

Maybe move this as the last statement before continue below.

drivers/gpu/drm/amd/amdkfd/kfd_svm.c static int svm_range_validate_and_map(struct mm_struct *mm, · line 1734

Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +3 -0
+				svm_range_unlock(prange);
+				if (r)
+					return r;

This will skip some cleanup, including svm_range_unreserve_bos and kfree(ctx). I think you can just continue in any case. If r != 0 the loop will terminate.

Regards,
  Felix

Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +3 -0
+				continue;
+			}
+
  			WRITE_ONCE(p->svms.faulting_task, current);
  			r = amdgpu_hmm_range_get_pages(&prange->notifier, addr, npages,
  						       readonly, owner, NULL,
#2 lore raw

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 54 lines

-----Original Message-----
From: Kuehling, Felix <[email protected]>
Sent: Friday, August 8, 2025 3:34 PM
To: Russell, Kent <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdkfd: Handle lack of READ permissions in SVM mapping

Quoted 13 lines · Kent Russell

HMM assumes that pages have READ permissions by default. Inside svm_range_validate_and_map, we add READ permissions then add WRITE permissions if the VMA isn't read-only. This will conflict with regions that only have PROT_WRITE or have PROT_NONE. When that happens, svm_range_restore_work will continue to retry, silently, giving the impression of a hang if pr_debug isn't enabled to show the retries..

If pages don't have READ permissions, simply unmap them and continue. If they weren't mapped in the first place, this would be a no-op. Since x86 doesn't support write-only, and PROT_NONE doesn't allow reads or writes anyways, this will allow the svm range validation to continue without getting stuck in a loop forever on mappings we can't use with HMM.

Review

Quoted 6 lines
---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 22 ++++++++++++++++++++++
  1 file changed, 22 insertions(+)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c

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

Quoted 4 lines

index e23b5a0f31f2..449595aab433 100644

drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -0
@@ -1713,6 +1713,28 @@ static int svm_range_validate_and_map(struct

mm_struct *mm,

drivers/gpu/drm/amd/amdkfd/kfd_svm.c static int svm_range_validate_and_map(struct mm_struct *mm,

Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +1 -0
                    next = min(vma->vm_end, end);
                    npages = (next - addr) >> PAGE_SHIFT;
+                   /* HMM requires at least READ permissions. If provided with

PROT_NONE,

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

Quoted 1 line
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +1 -0
+                    * unmap the memory. If it's not already mapped, this is a no-

op

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

Quoted 1 line
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +1 -0
+                    * If PROT_WRITE is provided without READ, warn first then

unmap

drivers/gpu/drm/amd/amdkfd/kfd_svm.c static int svm_range_validate_and_map(struct mm_struct *mm,

Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +7 -0
+                    */
+                   if (!(vma->vm_flags & VM_READ)) {
+                           unsigned long e, s;
+
+                           svm_range_lock(prange);
+                           if (vma->vm_flags & VM_WRITE)
+                                   pr_debug("VM_WRITE without VM_READ is

not supported");

drivers/gpu/drm/amd/amdkfd/kfd_svm.c static int svm_range_validate_and_map(struct mm_struct *mm,

Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +4 -0
+                           s = max(start, prange->start);
+                           e = min(end, prange->last);
+                           if (e >= s)
+                                   r = svm_range_unmap_from_gpus(prange, s,
Quoted 6 lines

e,

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

Quoted 1 line
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +1 -0
+

KFD_SVM_UNMAP_TRIGGER_UNMAP_FROM_CPU);

drivers/gpu/drm/amd/amdkfd/kfd_svm.c static int svm_range_validate_and_map(struct mm_struct *mm, · line 1731

Quoted 1 line
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +1 -0
+                           addr = next;

Maybe move this as the last statement before continue below.

Do you mean just the addr=next line? IE Not worrying about setting addr while holding the prange lock?

Quoted 9 lines

drivers/gpu/drm/amd/amdkfd/kfd_svm.c static int svm_range_validate_and_map(struct mm_struct *mm, · line 1734

Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +3 -0
+                           svm_range_unlock(prange);
+                           if (r)
+                                   return r;

This will skip some cleanup, including svm_range_unreserve_bos and kfree(ctx). I think you can just continue in any case. If r != 0 the loop will terminate.

Thanks, I missed the !r in the for loop conditions.

Kent

Quoted 12 lines

Regards,
Felix

drivers/gpu/drm/amd/amdkfd/kfd_svm.c static int svm_range_validate_and_map(struct mm_struct *mm,

Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +3 -0
+                           continue;
+                   }
+
                    WRITE_ONCE(p->svms.faulting_task, current);
                    r = amdgpu_hmm_range_get_pages(&prange->notifier, addr,

npages,

Quoted 1 line
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -0
                                                   readonly, owner, NULL,
maintainer
#3 lore raw
Quoted 55 lines · Kent

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 53 lines

-----Original Message-----
From: Kuehling, Felix <[email protected]>
Sent: Friday, August 8, 2025 3:34 PM
To: Russell, Kent <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdkfd: Handle lack of READ permissions in SVM mapping

Quoted 13 lines · Kent Russell

HMM assumes that pages have READ permissions by default. Inside svm_range_validate_and_map, we add READ permissions then add WRITE permissions if the VMA isn't read-only. This will conflict with regions that only have PROT_WRITE or have PROT_NONE. When that happens, svm_range_restore_work will continue to retry, silently, giving the impression of a hang if pr_debug isn't enabled to show the retries..

If pages don't have READ permissions, simply unmap them and continue. If they weren't mapped in the first place, this would be a no-op. Since x86 doesn't support write-only, and PROT_NONE doesn't allow reads or writes anyways, this will allow the svm range validation to continue without getting stuck in a loop forever on mappings we can't use with HMM.

Review

Quoted 6 lines
---
   drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 22 ++++++++++++++++++++++
   1 file changed, 22 insertions(+)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c

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

Quoted 4 lines

index e23b5a0f31f2..449595aab433 100644

drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -0
@@ -1713,6 +1713,28 @@ static int svm_range_validate_and_map(struct

mm_struct *mm,

drivers/gpu/drm/amd/amdkfd/kfd_svm.c static int svm_range_validate_and_map(struct mm_struct *mm,

Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +1 -0
                     next = min(vma->vm_end, end);
                     npages = (next - addr) >> PAGE_SHIFT;
+                   /* HMM requires at least READ permissions. If provided with

PROT_NONE,

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

Quoted 1 line
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +1 -0
+                    * unmap the memory. If it's not already mapped, this is a no-

op

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

Quoted 1 line
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +1 -0
+                    * If PROT_WRITE is provided without READ, warn first then

unmap

drivers/gpu/drm/amd/amdkfd/kfd_svm.c static int svm_range_validate_and_map(struct mm_struct *mm,

Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +7 -0
+                    */
+                   if (!(vma->vm_flags & VM_READ)) {
+                           unsigned long e, s;
+
+                           svm_range_lock(prange);
+                           if (vma->vm_flags & VM_WRITE)
+                                   pr_debug("VM_WRITE without VM_READ is

not supported");

Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +4 -0
+                           s = max(start, prange->start);
+                           e = min(end, prange->last);
+                           if (e >= s)
+                                   r = svm_range_unmap_from_gpus(prange, s,
Quoted 6 lines
Quoted 5 lines

e,

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

Quoted 1 line
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +1 -0
+

KFD_SVM_UNMAP_TRIGGER_UNMAP_FROM_CPU);

drivers/gpu/drm/amd/amdkfd/kfd_svm.c static int svm_range_validate_and_map(struct mm_struct *mm, · line 1731

Quoted 1 line
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +1 -0
+                           addr = next;

Maybe move this as the last statement before continue below.

Do you mean just the addr=next line? IE Not worrying about setting addr while holding the prange lock?

Yes. Setting addr=next sets things up for the next loop iteration. Therefore it seems logical to see this just before the continue, rather than "hiding" it between locking and error handling.

Regards,
  Felix

Quoted 22 lines
Quoted 7 lines

drivers/gpu/drm/amd/amdkfd/kfd_svm.c static int svm_range_validate_and_map(struct mm_struct *mm, · line 1734

Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +3 -0
+                           svm_range_unlock(prange);
+                           if (r)
+                                   return r;

This will skip some cleanup, including svm_range_unreserve_bos and kfree(ctx). I think you can just continue in any case. If r != 0 the loop will terminate.

Thanks, I missed the !r in the for loop conditions.

Kent

Quoted 11 lines

Regards,
Felix

drivers/gpu/drm/amd/amdkfd/kfd_svm.c static int svm_range_validate_and_map(struct mm_struct *mm,

Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +3 -0
+                           continue;
+                   }
+
                     WRITE_ONCE(p->svms.faulting_task, current);
                     r = amdgpu_hmm_range_get_pages(&prange->notifier, addr,

npages,

Quoted 1 line
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -0
                                                    readonly, owner, NULL,