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:
Link: https://github.com/ROCm/rocm-systems/pull/15545

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
-- 
2.53.0

Reply via email to