AMD-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1 0/2] drm/amdkfd: honor read-only intent for userptr and SVM ranges
@ 2026-11-17  4:20 Junhua Shen
  2026-11-17  4:20 ` [PATCH v1 1/2] drm/amdgpu: map KFD userptr read-only when the BO is not writable Junhua Shen
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Junhua Shen @ 2026-11-17  4:20 UTC (permalink / raw)
  To: Philip Yang, Felix Kuehling, Alex Deucher, Christian König,
	amd-gfx
  Cc: Chris Freehill, Kent Russell, Maneesh Gupta, Denny Iriawan,
	David Yat Sin, Huang Rui, Jenny Liu, Zhu Lingshan, Honglei Huang,
	Yiru Ma, Junhua Shen

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v1 1/2] drm/amdgpu: map KFD userptr read-only when the BO is not writable
  2026-11-17  4:20 [PATCH v1 0/2] drm/amdkfd: honor read-only intent for userptr and SVM ranges Junhua Shen
@ 2026-11-17  4:20 ` Junhua Shen
  2026-11-17  4:30   ` sashiko-bot
  2026-11-17  4:20 ` [PATCH v1 2/2] drm/amdkfd: honor KFD_IOCTL_SVM_FLAG_GPU_RO for read-only SVM ranges Junhua Shen
  2026-11-17 17:21 ` [PATCH v1 0/2] drm/amdkfd: honor read-only intent for userptr and " Philip Yang
  2 siblings, 1 reply; 6+ messages in thread
From: Junhua Shen @ 2026-11-17  4:20 UTC (permalink / raw)
  To: Philip Yang, Felix Kuehling, Alex Deucher, Christian König,
	amd-gfx
  Cc: Chris Freehill, Kent Russell, Maneesh Gupta, Denny Iriawan,
	David Yat Sin, Huang Rui, Jenny Liu, Zhu Lingshan, Honglei Huang,
	Yiru Ma, Junhua Shen

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.

Signed-off-by: Junhua Shen <Junhua.Shen@amd.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
index 1b79c9f182b1..04f221192768 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
@@ -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;
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v1 2/2] drm/amdkfd: honor KFD_IOCTL_SVM_FLAG_GPU_RO for read-only SVM ranges
  2026-11-17  4:20 [PATCH v1 0/2] drm/amdkfd: honor read-only intent for userptr and SVM ranges Junhua Shen
  2026-11-17  4:20 ` [PATCH v1 1/2] drm/amdgpu: map KFD userptr read-only when the BO is not writable Junhua Shen
@ 2026-11-17  4:20 ` Junhua Shen
  2026-11-17  4:42   ` sashiko-bot
  2026-11-17 17:21 ` [PATCH v1 0/2] drm/amdkfd: honor read-only intent for userptr and " Philip Yang
  2 siblings, 1 reply; 6+ messages in thread
From: Junhua Shen @ 2026-11-17  4:20 UTC (permalink / raw)
  To: Philip Yang, Felix Kuehling, Alex Deucher, Christian König,
	amd-gfx
  Cc: Chris Freehill, Kent Russell, Maneesh Gupta, Denny Iriawan,
	David Yat Sin, Huang Rui, Jenny Liu, Zhu Lingshan, Honglei Huang,
	Yiru Ma, Junhua Shen

When 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.

Signed-off-by: Junhua Shen <Junhua.Shen@amd.com>
---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
index ed5e3318877b..1b94b3b6d069 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
@@ -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;
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v1 1/2] drm/amdgpu: map KFD userptr read-only when the BO is not writable
  2026-11-17  4:20 ` [PATCH v1 1/2] drm/amdgpu: map KFD userptr read-only when the BO is not writable Junhua Shen
@ 2026-11-17  4:30   ` sashiko-bot
  0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-17  4:30 UTC (permalink / raw)
  To: Junhua Shen, amd-gfx

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 <Junhua.Shen@amd.com>

> 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.
>
> Signed-off-by: Junhua Shen <Junhua.Shen@amd.com>

[Severity: Low]
Should this include a Fixes: tag for the commit that introduced the
hardcoded 0 flags argument in init_user_pages()?

  Fixes: 5ae0283e831a ("drm/amdgpu: Add userptr support for KFD")

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117042051.768201-1-Junhua.Shen@amd.com?part=1

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v1 2/2] drm/amdkfd: honor KFD_IOCTL_SVM_FLAG_GPU_RO for read-only SVM ranges
  2026-11-17  4:20 ` [PATCH v1 2/2] drm/amdkfd: honor KFD_IOCTL_SVM_FLAG_GPU_RO for read-only SVM ranges Junhua Shen
@ 2026-11-17  4:42   ` sashiko-bot
  0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-17  4:42 UTC (permalink / raw)
  To: Junhua Shen, amd-gfx

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 <Junhua.Shen@amd.com>

> drm/amdkfd: honor KFD_IOCTL_SVM_FLAG_GPU_RO for read-only SVM ranges
>
> When 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.
>
> Signed-off-by: Junhua Shen <Junhua.Shen@amd.com>

[Severity: Low]
Since this fixes the read-only calculation in svm_range_validate_and_map(),
should it include a Fixes tag?

  Fixes: 2f617f4df8df ("drm/amdkfd: map SVM range with correct access permission")

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117042051.768201-1-Junhua.Shen@amd.com?part=2

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v1 0/2] drm/amdkfd: honor read-only intent for userptr and SVM ranges
  2026-11-17  4:20 [PATCH v1 0/2] drm/amdkfd: honor read-only intent for userptr and SVM ranges Junhua Shen
  2026-11-17  4:20 ` [PATCH v1 1/2] drm/amdgpu: map KFD userptr read-only when the BO is not writable Junhua Shen
  2026-11-17  4:20 ` [PATCH v1 2/2] drm/amdkfd: honor KFD_IOCTL_SVM_FLAG_GPU_RO for read-only SVM ranges Junhua Shen
@ 2026-11-17 17:21 ` Philip Yang
  2 siblings, 0 replies; 6+ messages in thread
From: Philip Yang @ 2026-11-17 17:21 UTC (permalink / raw)
  To: Junhua Shen, Philip Yang, Felix Kuehling, Alex Deucher,
	Christian König, amd-gfx
  Cc: Chris Freehill, Kent Russell, Maneesh Gupta, Denny Iriawan,
	David Yat Sin, Huang Rui, Jenny Liu, Zhu Lingshan, Honglei Huang,
	Yiru Ma

[-- Attachment #1: Type: text/plain, Size: 2933 bytes --]



On 2026-11-16 23:20, Junhua Shen wrote:
> 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
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

Reviewed-by: Philip Yang <philip.yang@amd.com>

[-- Attachment #2: Type: text/html, Size: 3487 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-17 17:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17  4:20 [PATCH v1 0/2] drm/amdkfd: honor read-only intent for userptr and SVM ranges Junhua Shen
2026-11-17  4:20 ` [PATCH v1 1/2] drm/amdgpu: map KFD userptr read-only when the BO is not writable Junhua Shen
2026-11-17  4:30   ` sashiko-bot
2026-11-17  4:20 ` [PATCH v1 2/2] drm/amdkfd: honor KFD_IOCTL_SVM_FLAG_GPU_RO for read-only SVM ranges Junhua Shen
2026-11-17  4:42   ` sashiko-bot
2026-11-17 17:21 ` [PATCH v1 0/2] drm/amdkfd: honor read-only intent for userptr and " Philip Yang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox