drm/amdkfd: honor read-only intent for userptr and SVM ranges
Subsystems (3)
cover drm/amdkfd: honor read-only intent for userptr and SVM ranges
KFD records whether an allocation or SVM range is writable (KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE for userptr, KFD_IOCTL_SVM_FLAG_GPU_RO for SVM) and already honors it when building the GPUVM page table. The userptr registration and the SVM mapping path, however, do not pass that intent down to the HMM fault, so hmm_range_fault() is asked for write access even for a read-only buffer.
For a read-only buffer backed by a writable private mapping (PROT_READ|PROT_WRITE, MAP_PRIVATE) that the GPU only reads, e.g. a host-to-device copy source, faulting for write breaks COW: every page is copied into anonymous memory, which doubles the footprint and, because the copy changes the CPU page tables, fires an MMU-notifier invalidation that evicts the GPU user-mode queues.
To keep the GPU mapping consistent with the requested permission, this series makes the HMM fault honor the same read-only intent:
- Patch 1 (userptr): register the userptr read-only when the allocation did not request KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE.
- Patch 2 (SVM): also honor KFD_IOCTL_SVM_FLAG_GPU_RO instead of deriving read-only solely from the VMA write permission.
This does not change what the GPU can access: the page table already maps a read-only allocation without write permission, so a GPU write to it already faults, with or without this series. The series only aligns the HMM fault with that page table and drops the needless write fault and COW. A buffer the GPU may write must still be allocated writable; there is no in-place upgrade, so making a buffer writable means freeing and re-allocating it.
The user-space side that marks such buffers read-only is tracked here:
Junhua Shen (2):
drm/amdgpu: map KFD userptr read-only when the BO is not writable drm/amdkfd: honor KFD_IOCTL_SVM_FLAG_GPU_RO for read-only SVM ranges
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 4 +++- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-)
base-commit: 067c5c24cef23d913b67eaaf71ae67e7169a6620
Signature · 1 line
2.53.0
PATCH 1/2 drm/amdgpu: map KFD userptr read-only when the BO is not writable
init_user_pages() registers every userptr writable (flags = 0), ignoring the KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE flag that get_pte_flags() already honors when mapping the BO.
Pass AMDGPU_GEM_USERPTR_READONLY for a read-only allocation, so the userptr registration matches the permission already enforced in the page table.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3 -1
@@ -1082,7 +1082,9 @@ static int init_user_pages(struct kgd_mem *mem, uint64_t user_addr,mutex_lock (&process_info->lock); - ret =amdgpu_ttm_tt_set_userptr (&bo->tbo, user_addr,0 ); + ret =amdgpu_ttm_tt_set_userptr (&bo->tbo, user_addr, + (mem->alloc_flags &KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE ) ? +0 :AMDGPU_GEM_USERPTR_READONLY );if (ret) {pr_err ("%s: Failed to set userptr: %d\n" , __func__, ret); goto out;
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v1 1/2] drm/amdgpu: map KFD userptr read-only when the BO is not writable
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] drm/amdgpu: missing Fixes trailer for KFD read-only userptr bug in init_user_pages() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] drm/amdgpu: missing Fixes trailer for KFD read-only userptr bug in init_user_pages()
--
commit 29a249e733b969cc649ffa369fb4f4e2387a5611
Author: Junhua Shen <[email protected]>
Quoted 11 lines
drm/amdgpu: map KFD userptr read-only when the BO is not writable
init_user_pages()registers every userptr writable (flags = 0), ignoring theKFD_IOC_ALLOC_MEM_FLAGS_WRITABLEflag thatget_pte_flags()already honors when mapping the BO.Pass
AMDGPU_GEM_USERPTR_READONLYfor a read-only allocation, so the userptr registration matches the permission already enforced in the page table.Signed-off-by Junhua Shen <[email protected]>
[Severity: Low]
Should this include a Fixes: tag for the commit that introduced the hardcoded 0 flags argument in init_user_pages()?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH 2/2 drm/amdkfd: honor KFD_IOCTL_SVM_FLAG_GPU_RO for read-only SVM ranges
In reply to Junhua ShenWhen mapping an SVM range, svm_range_validate_and_map() faults the pages with hmm_range_fault(), requesting write access unless the range is read-only. It decides read-only from the VMA write permission alone, so KFD_IOCTL_SVM_FLAG_GPU_RO is ignored and the range is faulted for write.
svm_range_get_pte_flags() already honors GPU_RO for the page table; honor it for the fault too.
--- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -1
@@ -1840,7 +1840,8 @@ static int svm_range_validate_and_map(struct mm_struct *mm, vma =vma_lookup (mm, addr);if (vma) { - readonly = !(vma->vm_flags &VM_WRITE ); + readonly = !(vma->vm_flags &VM_WRITE ) || + (prange->flags &KFD_IOCTL_SVM_FLAG_GPU_RO ); next =min (vma->vm_end, end); npages = (next - addr) >>PAGE_SHIFT ;
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v1 2/2] drm/amdkfd: honor KFD_IOCTL_SVM_FLAG_GPU_RO for read-only SVM ranges
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] drm/amdkfd: missing Fixes trailer for commit 2f617f4df8df --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] drm/amdkfd: missing Fixes trailer for commit 2f617f4df8df --
commit 7d7f0cff270e1b2b262588e7979494905efa821e
Author: Junhua Shen <[email protected]>
Quoted 11 lines
drm/amdkfd: honor
KFD_IOCTL_SVM_FLAG_GPU_ROfor read-only SVM rangesWhen mapping an SVM range,
svm_range_validate_and_map()faults the pages withhmm_range_fault(), requesting write access unless the range is read-only. It decides read-only from the VMA write permission alone, soKFD_IOCTL_SVM_FLAG_GPU_ROis ignored and the range is faulted for write.
svm_range_get_pte_flags()already honorsGPU_ROfor the page table; honor it for the fault too.Signed-off-by Junhua Shen <[email protected]>
[Severity: Low]
Since this fixes the read-only calculation in svm_range_validate_and_map(), should it include a Fixes tag?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Quoted 37 lines · Junhua Shen
KFD records whether an allocation or SVM range is writable (
KFD_IOC_ALLOC_MEM_FLAGS_WRITABLEfor userptr,KFD_IOCTL_SVM_FLAG_GPU_ROfor SVM) and already honors it when building the GPUVM page table. The userptr registration and the SVM mapping path, however, do not pass that intent down to the HMM fault, sohmm_range_fault()is asked for write access even for a read-only buffer.For a read-only buffer backed by a writable private mapping (PROT_READ|PROT_WRITE,
MAP_PRIVATE) that the GPU only reads, e.g. a host-to-device copy source, faulting for write breaks COW: every page is copied into anonymous memory, which doubles the footprint and, because the copy changes the CPU page tables, fires an MMU-notifier invalidation that evicts the GPU user-mode queues.To keep the GPU mapping consistent with the requested permission, this series makes the HMM fault honor the same read-only intent:
- Patch 1 (userptr): register the userptr read-only when the allocation did not request
KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE.
- Patch 2 (SVM): also honorKFD_IOCTL_SVM_FLAG_GPU_ROinstead of deriving read-only solely from the VMA write permission.This does not change what the GPU can access: the page table already maps a read-only allocation without write permission, so a GPU write to it already faults, with or without this series. The series only aligns the HMM fault with that page table and drops the needless write fault and COW. A buffer the GPU may write must still be allocated writable; there is no in-place upgrade, so making a buffer writable means freeing and re-allocating it.
The user-space side that marks such buffers read-only is tracked here:
Junhua Shen (2):
drm/amdgpu: map KFD userptr read-only when the BO is not writable drm/amdkfd: honorKFD_IOCTL_SVM_FLAG_GPU_ROfor read-only SVM ranges
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 4 +++- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-)base-commit:
067c5c24cef23d913b67eaaf71ae67e7169a6620
This change looks safe, svm ranges without the read-only attribute are unaffected, so I don’t expect regressions.
Using GPU_RO to request read-only access on a writable VMA is unusual. Ideally, userspace would use a read-only file-backed mapping, but this provides another way to request read-only GPU access and avoid triggering COW in hmm_range_fault().
Userspace is responsible for setting the read-only flag when appropriate. Failing to do so would be a userspace bug.
I don’t think the Fixes tag suggested by the bot is appropriate, since this supports new userspace functionality rather than fixing a regression. This patch series is