drm/amdkfd: Handle lack of READ permissions in SVM mapping
Subsystems (3)
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_validate_and_map will continue to retry, silently, giving the impression of a hang.
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 | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +18 -0
@@ -1713,6 +1713,24 @@ 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; + +if (vma->vm_flags &VM_WRITE ) +pr_warn ("VM_WRITE without VM_READ is not supported" ); + s =max (start, prange->start); + e =min (end, prange->last); +if (e >= s) +svm_range_unmap_from_gpus (prange, s, e, +KFD_SVM_UNMAP_TRIGGER_UNMAP_FROM_CPU ); + addr = next; +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
Quoted 4 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 havePROT_WRITEor havePROT_NONE. When that happens,
Why read-only conflict with PROT_WRITE or have PROT_NONE? They are vma->vm_flags that specifies the vma protection. User can change its value at runtime. Is user not allowed to change it from read-only to PROT_NONE?
Quoted 33 lines
svm_range_validate_and_mapwill continue to retry, silently, giving the impression of a hang.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_NONEdoesn'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.Signed-off-by Kent Russell<[email protected]>--- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)drivers/gpu/drm/amd/amdkfd/kfd_svm.c +11
-0@@ -1713,6 +1713,24 @@ 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; + +if (vma->vm_flags &VM_WRITE ) +pr_warn ("VM_WRITE without VM_READ is not supported" ); + s =max (start, prange->start); + e =min (end, prange->last);
drivers/gpu/drm/amd/amdkfd/kfd_svm.c static int svm_range_validate_and_map(struct mm_struct *mm, · line 1736
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +7
-0+if (e >= s) +svm_range_unmap_from_gpus (prange, s, e, +KFD_SVM_UNMAP_TRIGGER_UNMAP_FROM_CPU ); + addr = next; +continue ; + } +WRITE_ONCE (p->svms.faulting_task, current); r = amdgpu_hmm_range_get_pages(&prange->notifier, addr, npages, readonly, owner, NULL,
It seems the real problem is at amdgpu_hmm_range_get_pages. It always set HMM_PFN_REQ_FAULT to hmm_range->default_flags. HMM_PFN_REQ_FAULT means the page is faultable and a future call with HMM_PFN_REQ_FAULT could succeed. When vma->vm_flags is PROT_NONE the vma is not faultable, so hmm_range->default_flags should be not set to HMM_PFN_REQ_FAULT to avoid hmm_range_fault fault this vma.
Regards
Xiaogang
[Public]
The big thing is that HMM doesn’t support a lack of read permissions. So does it make sense to make any HMM calls or set any HMM flags if it’s an unsupported configuration that it will fault on? That was why I went down this path, since trying to remove the READ permissions here while still using HMM calls could go sideways, if HMM will automatically fail if READ permissions are missing.
Kent
From: Chen, Xiaogang <[email protected]>
Sent: Tuesday, July 22, 2025 6:46 PM
To: Russell, Kent <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>
Subject: Re: [PATCH] drm/amdkfd: Handle lack of READ permissions in SVM mapping
On 7/22/2025 11:24 AM, Kent Russell wrote:
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,
Why read-only conflict with PROT_WRITE or have PROT_NONE? They are vma->vm_flags that specifies the vma protection. User can change its value at runtime. Is user not allowed to change it from read-only to PROT_NONE?
svm_range_validate_and_map will continue to retry, silently, giving the
impression of a hang.
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 | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -0
index e23b5a0f31f2..10b70b941b11 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
@@ -1713,6 +1713,24 @@ 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;
+
+ if (vma->vm_flags & VM_WRITE)
+ pr_warn("VM_WRITE without VM_READ is not supported");
+ s = max(start, prange->start);
+ e = min(end, prange->last);
+ if (e >= s)
+ svm_range_unmap_from_gpus(prange, s, e,
+ KFD_SVM_UNMAP_TRIGGER_UNMAP_FROM_CPU);
+ addr = next;
+ continue;
+ }
+
WRITE_ONCE(p->svms.faulting_task, current);
r = amdgpu_hmm_range_get_pages(&prange->notifier, addr, npages,
readonly, owner, NULL,It seems the real problem is at amdgpu_hmm_range_get_pages. It always set HMM_PFN_REQ_FAULT to hmm_range->default_flags. HMM_PFN_REQ_FAULT means the page is faultable and a future call with HMM_PFN_REQ_FAULT could succeed. When vma->vm_flags is PROT_NONE the vma is not faultable, so hmm_range->default_flags should be not set to HMM_PFN_REQ_FAULT to avoid hmm_range_fault fault this vma.
Regards
Xiaogang
Quoted 6 lines · Kent
[Public]
The big thing is that HMM doesn’t support a lack of read permissions. So does it make sense to make any HMM calls or set any HMM flags if it’s an unsupported configuration that it will fault on? That was why I went down this path, since trying to remove the READ permissions here while still using HMM calls could go sideways, if HMM will automatically fail if READ permissions are missing.
I do not see HMM can only work if read permission is specified. From code comments "If the vma does not allow read access, then assume that it does not allow write access either".
hmm_range_fault uses cpu page fault to valid pages. If it cannot do that it return error code specified the reasons. Driver needs check the error code to decide what to do after. Current driver checks if read is allowed, if not, driver assumes it is write permission. That does not consider VM_NONE that specifies the vma is not accessible, then the vms is not fault-able.
This issue not only exists in svm code. For user buffer registration and validation current driver does not consider VM_NONE either. I think part of the changes is: before let hmm_range_fault do page validation check vma->flags to decide if can set hmm_range->default_flags to HMM_PFN_REQ_FAULT or not. Do not request hmm fault on area that is not faultable.
Regards
Xiaogang
Quoted 119 lines
Kent
*From:*Chen, Xiaogang <[email protected]>
*Sent:* Tuesday, July 22, 2025 6:46 PM
*To:* Russell, Kent <[email protected]>; [email protected] *Cc:* Kuehling, Felix <[email protected]>
*Subject:* Re: [PATCH] drm/amdkfd: Handle lack of READ permissions in SVM mappingOn 7/22/2025 11:24 AM, Kent Russell wrote:
HMM assumes that pages have READ permissions by default. Inside
svm_range_validate_and_map, we add READ permissions then add WRITEpermissions if the VMA isn't read-only. This will conflict with regions
that only have
PROT_WRITEor havePROT_NONE. When that happens,Why read-only conflict with
PROT_WRITEor havePROT_NONE? They arevma->vm_flagsthat specifies the vma protection. User can change its value at runtime. Is user not allowed to change it from read-only toPROT_NONE?svm_range_validate_and_map will continue to retry, silently, giving the impression of a hang. 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. Signed-off-by: Kent Russell <[email protected]> <mailto:[email protected]> --- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c index e23b5a0f31f2..10b70b941b11 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c @@ -1713,6 +1713,24 @@ 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; + + if (vma->vm_flags & VM_WRITE) + pr_warn("VM_WRITE without VM_READ is not supported"); + s = max(start, prange->start); + e = min(end, prange->last); + if (e >= s) + svm_range_unmap_from_gpus(prange, s, e, + KFD_SVM_UNMAP_TRIGGER_UNMAP_FROM_CPU); + addr = next; + continue; + } + WRITE_ONCE(p->svms.faulting_task, current); r = amdgpu_hmm_range_get_pages(&prange->notifier, addr, npages, readonly, owner, NULL,It seems the real problem is at
amdgpu_hmm_range_get_pages. It always setHMM_PFN_REQ_FAULTtohmm_range->default_flags.HMM_PFN_REQ_FAULTmeans the page is faultable and a future call withHMM_PFN_REQ_FAULTcould succeed. Whenvma->vm_flagsisPROT_NONEthe vma is not faultable, sohmm_range->default_flagsshould be not set toHMM_PFN_REQ_FAULTto avoidhmm_range_faultfault this vma.Regards
Xiaogang