* [PATCH] drm/amdkfd: add in-place userptr write access upgrade
@ 2026-12-01 11:23 Junhua Shen
2026-12-01 11:48 ` sashiko-bot
2026-12-01 14:16 ` Christian König
0 siblings, 2 replies; 4+ messages in thread
From: Junhua Shen @ 2026-12-01 11:23 UTC (permalink / raw)
To: amd-gfx, Felix Kuehling, Philip Yang, Alex Deucher,
Christian König
Cc: Chris Freehill, Kent Russell, Maneesh Gupta, Denny Iriawan,
David Yat Sin, Huang Rui, Jenny Liu, Zhu Lingshan, Honglei Huang,
Yiru Ma, Junhua Shen
Userptr BOs can be registered read-only so that pinning host memory for
device access does not request write permission, which would, for
example, break the copy-on-write sharing of a private file mapping.
When the same host range later needs write access, the only option today
is to unregister and re-register it, which recreates the BO and can move
its GPU virtual address.
Add AMDKFD_IOC_UPGRADE_USERPTR_WRITABLE to upgrade an existing read-only
userptr BO to read/write in place, without changing its VA or recreating
the BO. On failure the BO keeps its original read-only state.
Signed-off-by: Junhua Shen <Junhua.Shen@amd.com>
---
Background:
In ROCm rocm-systems PR #15545 [1], the runtime changes host-to-device
source pins from writable to read-only. Previously, those writable pins
requested write permission even though the device only reads the buffer.
On a MAP_PRIVATE file mapping, e.g. mmapped model weights, this breaks
copy-on-write: every page is copied into anonymous memory, and for
userptr the resulting MMU notifier evicts the process queues, repeatedly
during a single model load.
These read-only pins will be made internally by the runtime and avoid
the COW copies, their memory overhead and the resulting queue evictions.
However, the same host range can concurrently be registered writable by
another user, such as hipHostRegister() or a device-to-host staging pin.
User space shares one BO per range and reference-counts it. With SVM, a
writable request just clears GPU_RO on the existing range. With
userptr, the read-only BO can only be freed and re-allocated, which
recreates the BO and can move its GPU VA while other registrants still
use it, so user space has to fail the writable request. This ioctl lets
userptr behave like SVM.
This builds on the read-only userptr registration series:
https://lore.kernel.org/all/20261117042051.768201-1-Junhua.Shen@amd.com/
Without it no KFD userptr is registered read-only and this ioctl is a no-op.
Upgrade flow:
- Reject non-writable or unmapped VMAs up front, before touching queues.
- Evict the process queues, unmap the BO on every GPU where it is mapped,
sync, flush TLBs and drop peer DMA mappings (still DMA_TO_DEVICE).
- Re-fault the pages for write, unbind the old read-only GTT binding and
rebind with DMA_BIDIRECTIONAL, then publish WRITABLE and refresh the
cached attachment PTE flags.
- Remap on the same GPUs, sync, flush TLBs and restore the queues.
On any failure the BO keeps its read-only state and its original mappings
are restored. -EAGAIN is propagated and retried by libhsakmt.
User space and tests (ROCm rocm-systems [1]):
- libhsakmt: use the ioctl when a read-only registered range is
re-registered writable (fmm.c).
- kfdtest: KFDMemoryTest.MemoryRegisterReadOnly and
MemoryRegisterUserPtrUpgrade{,Unmapped,ProtReadFail,CowZeroPage,
RemapAfterUnmap,ActiveQueue,MultiGPU}.
[1] https://github.com/ROCm/rocm-systems/pull/15545
Tested on gfx1151 (single GPU): all of the above pass with HSA_USE_SVM=0;
MultiGPU is skipped on single-GPU systems, so the multi-GPU path has not
been exercised on hardware.
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 3 +
.../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 200 ++++++++++++++++++
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 130 ++++++++++++
include/uapi/linux/kfd_ioctl.h | 17 +-
4 files changed, 348 insertions(+), 2 deletions(-)
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h
index 74dc307e5140..f1b899c18505 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h
@@ -337,6 +337,9 @@ int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(struct amdgpu_device *adev,
struct kgd_mem *mem, void *drm_priv);
int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
struct amdgpu_device *adev, struct kgd_mem *mem, void *drm_priv);
+int amdgpu_amdkfd_gpuvm_check_userptr_writable(struct kgd_mem *mem);
+int amdgpu_amdkfd_gpuvm_upgrade_userptr_writable(struct amdgpu_device *adev,
+ struct kgd_mem *mem);
int amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void *drm_priv);
int amdgpu_amdkfd_gpuvm_sync_memory(
struct amdgpu_device *adev, struct kgd_mem *mem, bool intr);
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
index 342a7cefaacd..51de1ba19d60 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
@@ -1937,6 +1937,206 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
return ret;
}
+/**
+ * amdgpu_amdkfd_gpuvm_check_userptr_writable - Fast pre-check that all VMAs
+ * backing a userptr BO are currently mapped and writable.
+ * @mem: KFD userptr BO to check
+ *
+ * Allows callers to reject non-writable (e.g. PROT_READ) or unmapped ranges
+ * before evicting user queues and tearing down GPU mappings.
+ *
+ * Return: 0 if the whole range is writable, -EPERM if a VMA is not writable,
+ * -EFAULT if part of the range is unmapped, or another negative error code.
+ */
+int amdgpu_amdkfd_gpuvm_check_userptr_writable(struct kgd_mem *mem)
+{
+ struct amdgpu_bo *bo = mem->bo;
+ struct vm_area_struct *vma;
+ struct mm_struct *mm;
+ unsigned long start, end, addr;
+ u64 user_addr = 0;
+ int ret;
+
+ if (!bo || !amdgpu_ttm_tt_is_userptr(bo->tbo.ttm))
+ return -EINVAL;
+
+ ret = amdgpu_ttm_tt_get_userptr(&bo->tbo, &user_addr);
+ if (ret)
+ return ret;
+
+ mm = bo->notifier.mm;
+ if (unlikely(!mm))
+ return -EFAULT;
+
+ if (!mmget_not_zero(mm))
+ return -ESRCH;
+
+ start = user_addr;
+ if (check_add_overflow(start,
+ (unsigned long)bo->tbo.ttm->num_pages << PAGE_SHIFT,
+ &end)) {
+ mmput(mm);
+ return -EFAULT;
+ }
+
+ mmap_read_lock(mm);
+ addr = start;
+ do {
+ vma = vma_lookup(mm, addr);
+ if (!vma) {
+ ret = -EFAULT;
+ break;
+ }
+ if (!(vma->vm_flags & VM_WRITE)) {
+ ret = -EPERM;
+ break;
+ }
+ addr = min(end, vma->vm_end);
+ } while (addr < end);
+ mmap_read_unlock(mm);
+ mmput(mm);
+
+ return ret;
+}
+
+/**
+ * amdgpu_amdkfd_gpuvm_upgrade_userptr_writable - In-place read-only -> read/write
+ * upgrade of a userptr BO.
+ * @adev: amdgpu device
+ * @mem: KFD BO to upgrade (must be an unmapped userptr BO)
+ *
+ * Caller must have quiesced user queues, unmapped and DMA-unmapped all GPU
+ * mappings of @mem, and will remap them after this returns. On failure, the
+ * BO's read-only state is preserved so the caller can restore the original
+ * read-only mappings.
+ *
+ * Return: 0 on success or if the BO is already writable, negative error code
+ * on failure.
+ */
+int amdgpu_amdkfd_gpuvm_upgrade_userptr_writable(struct amdgpu_device *adev,
+ struct kgd_mem *mem)
+{
+ struct amdkfd_process_info *process_info = mem->process_info;
+ struct amdgpu_bo *bo = mem->bo;
+ struct ttm_operation_ctx ctx = {
+ .interruptible = false,
+ .no_wait_gpu = false,
+ };
+ struct kfd_mem_attachment *entry;
+ struct amdgpu_hmm_range *range;
+ u64 user_addr = 0;
+ bool unbound = false;
+ int ret = 0;
+
+ if (!bo || !amdgpu_ttm_tt_is_userptr(bo->tbo.ttm))
+ return -EINVAL;
+
+ /* Already writable: nothing to do. */
+ if (!amdgpu_ttm_tt_is_readonly(bo->tbo.ttm))
+ return 0;
+
+ ret = amdgpu_ttm_tt_get_userptr(&bo->tbo, &user_addr);
+ if (ret)
+ return ret;
+
+ mutex_lock(&process_info->lock);
+
+ /* Clear the read-only flag and re-fault the user pages for write
+ * before touching the BO's placement, so a failure (e.g. -EPERM on a
+ * PROT_READ VMA or -EFAULT) leaves the existing read-only BO intact.
+ */
+ ret = amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr, 0);
+ if (ret)
+ goto out_unlock;
+
+ range = amdgpu_hmm_range_alloc(NULL);
+ if (unlikely(!range)) {
+ ret = -ENOMEM;
+ goto out_restore_ro;
+ }
+
+ ret = amdgpu_ttm_tt_get_user_pages(bo, range);
+ if (ret) {
+ if (ret == -EAGAIN || ret == -EPERM || ret == -EFAULT)
+ pr_debug("%s: Failed to get user pages: %d\n",
+ __func__, ret);
+ else
+ pr_err("%s: Failed to get user pages: %d\n",
+ __func__, ret);
+ goto out_free_range;
+ }
+
+ ret = amdgpu_bo_reserve(bo, true);
+ if (ret) {
+ pr_err("%s: Failed to reserve BO\n", __func__);
+ goto out_free_range;
+ }
+
+ /* Unbind the old GTT mapping with READONLY still set so TTM unmaps
+ * the SG table with its original DMA_TO_DEVICE direction, then rebind
+ * the writable pages with DMA_BIDIRECTIONAL.
+ */
+ if (bo->tbo.resource->mem_type != TTM_PL_SYSTEM) {
+ amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr,
+ AMDGPU_GEM_USERPTR_READONLY);
+ amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU);
+ ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
+ if (ret) {
+ pr_err("%s: failed to unbind RO userptr BO\n", __func__);
+ amdgpu_bo_unreserve(bo);
+ amdgpu_hmm_range_free(range);
+ goto out_unlock;
+ }
+ unbound = true;
+ amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr, 0);
+ }
+
+ amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm, range);
+ amdgpu_bo_placement_from_domain(bo, mem->domain);
+ ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
+ if (ret) {
+ pr_err("%s: failed to validate BO: %d\n", __func__, ret);
+ amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr,
+ AMDGPU_GEM_USERPTR_READONLY);
+ if (unbound) {
+ /* Re-bind read-only so the BO is not stranded in
+ * TTM_PL_SYSTEM when the caller remaps it.
+ */
+ amdgpu_bo_placement_from_domain(bo, mem->domain);
+ if (ttm_bo_validate(&bo->tbo, &bo->placement, &ctx))
+ pr_err("%s: failed to restore RO userptr BO\n",
+ __func__);
+ }
+ amdgpu_bo_unreserve(bo);
+ amdgpu_hmm_range_free(range);
+ goto out_unlock;
+ }
+ amdgpu_bo_unreserve(bo);
+ amdgpu_hmm_range_free(range);
+
+ /* Publish the writable state and refresh cached attachment PTE flags
+ * so the caller's remap installs writable PTEs.
+ */
+ mutex_lock(&mem->lock);
+ mem->alloc_flags |= KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE;
+ list_for_each_entry(entry, &mem->attachments, list)
+ entry->pte_flags = get_pte_flags(entry->adev,
+ entry->bo_va->base.vm, mem);
+ mutex_unlock(&mem->lock);
+
+ mutex_unlock(&process_info->lock);
+ return 0;
+
+out_free_range:
+ amdgpu_hmm_range_free(range);
+out_restore_ro:
+ amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr,
+ AMDGPU_GEM_USERPTR_READONLY);
+out_unlock:
+ mutex_unlock(&process_info->lock);
+ return ret;
+}
+
int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
struct amdgpu_device *adev, struct kgd_mem *mem, void *drm_priv,
uint64_t *size)
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
index 3d2136adb62b..05898e8b5064 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
@@ -1277,6 +1277,133 @@ static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep,
return err;
}
+/* Upgrade an already registered userptr BO from read-only to read/write in
+ * place.
+ */
+static int kfd_ioctl_upgrade_userptr_writable(struct file *filep,
+ struct kfd_process *p, void *data)
+{
+ struct kfd_ioctl_upgrade_userptr_writable_args *args = data;
+ struct kfd_process_device *pdd, *peer_pdd;
+ DECLARE_BITMAP(bitmap_mapped, MAX_GPU_INSTANCE);
+ DECLARE_BITMAP(bitmap_unmapped, MAX_GPU_INSTANCE);
+ bool has_mapped = false;
+ struct kgd_mem *mem;
+ long err = 0;
+ int i;
+
+ mutex_lock(&p->mutex);
+
+ pdd = kfd_process_device_data_by_id(p, GET_GPU_ID(args->handle));
+ if (!pdd) {
+ err = -EINVAL;
+ goto err_unlock;
+ }
+
+ mem = kfd_process_device_translate_handle(pdd, GET_IDR_HANDLE(args->handle));
+ if (!mem) {
+ err = -EINVAL;
+ goto err_unlock;
+ }
+
+ if (!(mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_USERPTR)) {
+ err = -EINVAL;
+ goto err_unlock;
+ }
+
+ if (mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE)
+ goto err_unlock;
+
+ /* Fast pre-check: reject non-writable or unmapped VMAs before
+ * evicting user queues and tearing down GPU mappings.
+ */
+ err = amdgpu_amdkfd_gpuvm_check_userptr_writable(mem);
+ if (err)
+ goto err_unlock;
+
+ bitmap_zero(bitmap_mapped, MAX_GPU_INSTANCE);
+ bitmap_zero(bitmap_unmapped, MAX_GPU_INSTANCE);
+ if (mem->mapped_to_gpu_memory) {
+ for (i = 0; i < p->n_pdds; i++) {
+ if (p->pdds[i]->drm_priv &&
+ amdgpu_amdkfd_bo_mapped_to_dev(p->pdds[i]->drm_priv,
+ mem))
+ set_bit(i, bitmap_mapped);
+ }
+ has_mapped = !bitmap_empty(bitmap_mapped, p->n_pdds);
+ }
+
+ if (has_mapped) {
+ /* Stop all user queues before tearing down the read-only
+ * mappings so no in-flight GPU access faults on unmapped PTEs.
+ */
+ err = kfd_process_evict_queues(p, KFD_QUEUE_EVICTION_TRIGGER_USERPTR);
+ if (err)
+ goto err_unlock;
+
+ for_each_set_bit(i, bitmap_mapped, p->n_pdds) {
+ peer_pdd = p->pdds[i];
+ err = amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(peer_pdd->dev->adev,
+ mem, peer_pdd->drm_priv);
+ if (err)
+ break;
+ set_bit(i, bitmap_unmapped);
+ }
+
+ /* Sync, flush TLBs, and drop peer DMA mappings for every GPU
+ * actually unmapped above before either upgrading or remapping,
+ * so dmaunmap uses the original DMA_TO_DEVICE direction and
+ * remap will not leak an existing peer SG table.
+ */
+ if (!bitmap_empty(bitmap_unmapped, p->n_pdds)) {
+ long sync_err;
+
+ sync_err = amdgpu_amdkfd_gpuvm_sync_memory(pdd->dev->adev, mem, false);
+ if (sync_err && !err)
+ err = sync_err;
+
+ for_each_set_bit(i, bitmap_unmapped, p->n_pdds) {
+ peer_pdd = p->pdds[i];
+ kfd_flush_tlb(peer_pdd);
+ amdgpu_amdkfd_gpuvm_dmaunmap_mem(mem, peer_pdd->drm_priv);
+ }
+ }
+ }
+
+ if (!err)
+ err = amdgpu_amdkfd_gpuvm_upgrade_userptr_writable(pdd->dev->adev, mem);
+
+ if (has_mapped) {
+ long map_err;
+
+ /* Restore mappings on every GPU that was unmapped: writable
+ * PTEs on success, or the original read-only PTEs on rollback.
+ */
+ for_each_set_bit(i, bitmap_unmapped, p->n_pdds) {
+ peer_pdd = p->pdds[i];
+ map_err = amdgpu_amdkfd_gpuvm_map_memory_to_gpu(peer_pdd->dev->adev,
+ mem, peer_pdd->drm_priv);
+ if (map_err && !err)
+ err = map_err;
+ }
+
+ if (!bitmap_empty(bitmap_unmapped, p->n_pdds)) {
+ map_err = amdgpu_amdkfd_gpuvm_sync_memory(pdd->dev->adev, mem, false);
+ if (map_err && !err)
+ err = map_err;
+
+ for_each_set_bit(i, bitmap_unmapped, p->n_pdds)
+ kfd_flush_tlb(p->pdds[i]);
+ }
+
+ kfd_process_restore_queues(p);
+ }
+
+err_unlock:
+ mutex_unlock(&p->mutex);
+ return err;
+}
+
static int kfd_ioctl_free_memory_of_gpu(struct file *filep,
struct kfd_process *p, void *data)
{
@@ -3682,6 +3809,9 @@ static const struct amdkfd_ioctl_desc amdkfd_ioctls[] = {
AMDKFD_IOCTL_DEF(AMDKFD_IOC_PROFILER,
kfd_ioctl_profiler, 0),
+
+ AMDKFD_IOCTL_DEF(AMDKFD_IOC_UPGRADE_USERPTR_WRITABLE,
+ kfd_ioctl_upgrade_userptr_writable, 0),
};
#define AMDKFD_CORE_IOCTL_COUNT ARRAY_SIZE(amdkfd_ioctls)
diff --git a/include/uapi/linux/kfd_ioctl.h b/include/uapi/linux/kfd_ioctl.h
index 3d608d6b0ab8..d08166fe98fd 100644
--- a/include/uapi/linux/kfd_ioctl.h
+++ b/include/uapi/linux/kfd_ioctl.h
@@ -50,9 +50,10 @@
* - 1.22 - Add queue creation with metadata ring base address
* - 1.23 - Add profiler control ioctl to enable/disable profiler on a process
* - 1.24 - Enable SVM no-access attributes
+ * - 1.25 - Add userptr read-only -> read/write upgrade ioctl
*/
#define KFD_IOCTL_MAJOR_VERSION 1
-#define KFD_IOCTL_MINOR_VERSION 24
+#define KFD_IOCTL_MINOR_VERSION 25
struct kfd_ioctl_get_version_args {
__u32 major_version; /* from KFD */
@@ -449,6 +450,15 @@ struct kfd_ioctl_alloc_memory_of_gpu_args {
__u32 flags;
};
+/* Upgrade an already registered userptr BO from read-only to read/write in place
+ *
+ * Rebuilds the GPU mapping with write permission without a free/re-alloc; user
+ * space does not unmap. Only the read-only -> read/write direction is supported.
+ */
+struct kfd_ioctl_upgrade_userptr_writable_args {
+ __u64 handle; /* to KFD */
+};
+
/* Free memory allocated with kfd_ioctl_alloc_memory_of_gpu
*
* @handle: memory handle returned by alloc
@@ -1723,7 +1733,10 @@ struct kfd_ioctl_profiler_args {
#define AMDKFD_IOC_PROFILER \
AMDKFD_IOWR(0x28, struct kfd_ioctl_profiler_args)
+#define AMDKFD_IOC_UPGRADE_USERPTR_WRITABLE \
+ AMDKFD_IOW(0x29, struct kfd_ioctl_upgrade_userptr_writable_args)
+
#define AMDKFD_COMMAND_START 0x01
-#define AMDKFD_COMMAND_END 0x29
+#define AMDKFD_COMMAND_END 0x2A
#endif
--
2.53.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] drm/amdkfd: add in-place userptr write access upgrade
2026-12-01 11:23 [PATCH] drm/amdkfd: add in-place userptr write access upgrade Junhua Shen
@ 2026-12-01 11:48 ` sashiko-bot
2026-12-01 14:16 ` Christian König
1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-01 11:48 UTC (permalink / raw)
To: Junhua Shen, amd-gfx
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] drm/amdkfd: COW self-invalidation in
amdgpu_amdkfd_gpuvm_upgrade_userptr_writable()
- [High] drm/amdkfd: missing amdgpu_hmm_range_valid() check on userptr
write upgrade
- [High] drm/amdkfd: NULL ttm->sg dereference on rollback in
amdgpu_amdkfd_gpuvm_upgrade_userptr_writable()
- [High] drm/amdkfd: early return in
amdgpu_amdkfd_gpuvm_upgrade_userptr_writable() skips PTE flag update
- [Medium] drm/amdkfd: remap failure in
kfd_ioctl_upgrade_userptr_writable() loses GPU mappings
--
commit 0c4aba580e875d99623cbf13b7e161af5fbf3260
Author: Junhua Shen <Junhua.Shen@amd.com>
drm/amdkfd: add in-place userptr write access upgrade
This commit adds the AMDKFD_IOC_UPGRADE_USERPTR_WRITABLE ioctl to upgrade
an existing read-only userptr BO to read/write in place without changing
its GPU virtual address or recreating the BO.
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
> index 1b79c9f182b1d..f3df5a5ff1ae9 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
[ ... ]
> +int amdgpu_amdkfd_gpuvm_upgrade_userptr_writable(struct amdgpu_device *adev,
> + struct kgd_mem *mem)
> +{
> + struct amdkfd_process_info *process_info = mem->process_info;
> + struct amdgpu_bo *bo = mem->bo;
> + struct ttm_operation_ctx ctx = {
> + .interruptible = false,
> + .no_wait_gpu = false,
> + };
> + struct kfd_mem_attachment *entry;
> + struct amdgpu_hmm_range *range;
> + u64 user_addr = 0;
> + bool unbound = false;
> + int ret = 0;
> +
> + if (!bo || !amdgpu_ttm_tt_is_userptr(bo->tbo.ttm))
> + return -EINVAL;
> +
> + /* Already writable: nothing to do. */
> + if (!amdgpu_ttm_tt_is_readonly(bo->tbo.ttm))
> + return 0;
[Severity: High]
Does this early return leave mem->alloc_flags and entry->pte_flags
read-only when bo->tbo.ttm does not have AMDGPU_GEM_USERPTR_READONLY set?
init_user_pages() initializes userptr BOs with
amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr, 0) regardless of whether
KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE is set in mem->alloc_flags, while
get_pte_flags(), kfd_mem_dmamap_userptr() and
kfd_ioctl_upgrade_userptr_writable() check
mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE. When this check
returns 0 early, kfd_ioctl_upgrade_userptr_writable() remaps the BO with
the unchanged read-only entry->pte_flags and DMA_TO_DEVICE peer mappings
and returns 0 to user space while GPU page tables stay read-only.
> +
> + ret = amdgpu_ttm_tt_get_userptr(&bo->tbo, &user_addr);
> + if (ret)
> + return ret;
> +
> + mutex_lock(&process_info->lock);
> +
> + /* Clear the read-only flag and re-fault the user pages for write
> + * before touching the BO's placement, so a failure (e.g. -EPERM on a
> + * PROT_READ VMA or -EFAULT) leaves the existing read-only BO intact.
> + */
> + ret = amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr, 0);
> + if (ret)
> + goto out_unlock;
> +
> + range = amdgpu_hmm_range_alloc(NULL);
> + if (unlikely(!range)) {
> + ret = -ENOMEM;
> + goto out_restore_ro;
> + }
> +
> + ret = amdgpu_ttm_tt_get_user_pages(bo, range);
> + if (ret) {
> + if (ret == -EAGAIN || ret == -EPERM || ret == -EFAULT)
> + pr_debug("%s: Failed to get user pages: %d\n",
> + __func__, ret);
> + else
> + pr_err("%s: Failed to get user pages: %d\n",
> + __func__, ret);
> + goto out_free_range;
> + }
> +
> + ret = amdgpu_bo_reserve(bo, true);
> + if (ret) {
> + pr_err("%s: Failed to reserve BO\n", __func__);
> + goto out_free_range;
> + }
> +
> + /* Unbind the old GTT mapping with READONLY still set so TTM unmaps
> + * the SG table with its original DMA_TO_DEVICE direction, then rebind
> + * the writable pages with DMA_BIDIRECTIONAL.
> + */
> + if (bo->tbo.resource->mem_type != TTM_PL_SYSTEM) {
> + amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr,
> + AMDGPU_GEM_USERPTR_READONLY);
> + amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU);
> + ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
> + if (ret) {
> + pr_err("%s: failed to unbind RO userptr BO\n", __func__);
> + amdgpu_bo_unreserve(bo);
> + amdgpu_hmm_range_free(range);
> + goto out_unlock;
> + }
> + unbound = true;
> + amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr, 0);
> + }
> +
> + amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm, range);
> + amdgpu_bo_placement_from_domain(bo, mem->domain);
> + ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
> + if (ret) {
> + pr_err("%s: failed to validate BO: %d\n", __func__, ret);
> + amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr,
> + AMDGPU_GEM_USERPTR_READONLY);
> + if (unbound) {
> + /* Re-bind read-only so the BO is not stranded in
> + * TTM_PL_SYSTEM when the caller remaps it.
> + */
> + amdgpu_bo_placement_from_domain(bo, mem->domain);
> + if (ttm_bo_validate(&bo->tbo, &bo->placement, &ctx))
> + pr_err("%s: failed to restore RO userptr BO\n",
> + __func__);
> + }
> + amdgpu_bo_unreserve(bo);
> + amdgpu_hmm_range_free(range);
> + goto out_unlock;
> + }
[Severity: High]
Can the rollback ttm_bo_validate() dereference a NULL bo->tbo.ttm->sg when
rebinding the BO to mem->domain fails inside amdgpu_ttm_tt_pin_userptr()?
When the first ttm_bo_validate() to mem->domain fails in
amdgpu_ttm_tt_pin_userptr() (for example in sg_alloc_table_from_pages() or
dma_map_sgtable()), its error path frees ttm->sg and sets it to NULL while
TTM_TT_FLAG_PRIV_POPULATED remains set:
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c:amdgpu_ttm_tt_pin_userptr() {
...
release_sg_table:
sg_free_table(ttm->sg);
release_sg:
kfree(ttm->sg);
ttm->sg = NULL;
return r;
}
Because ttm_tt_is_populated(bo->tbo.ttm) is still true, the rollback
ttm_bo_validate() skips amdgpu_ttm_tt_populate() and calls
amdgpu_ttm_tt_pin_userptr() -> sg_alloc_table_from_pages() with
ttm->sg == NULL. In addition, amdgpu_ttm_tt_set_user_pages() has already
overwritten bo->tbo.ttm->pages with pages from range before validation, so
if the rollback validation also fails, freeing range leaves dangling page
pointers in ttm->pages with the BO stranded in TTM_PL_SYSTEM.
> + amdgpu_bo_unreserve(bo);
> + amdgpu_hmm_range_free(range);
> +
> + /* Publish the writable state and refresh cached attachment PTE flags
> + * so the caller's remap installs writable PTEs.
> + */
> + mutex_lock(&mem->lock);
> + mem->alloc_flags |= KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE;
> + list_for_each_entry(entry, &mem->attachments, list)
> + entry->pte_flags = get_pte_flags(entry->adev,
> + entry->bo_va->base.vm, mem);
> + mutex_unlock(&mem->lock);
> +
> + mutex_unlock(&process_info->lock);
> + return 0;
[Severity: High]
When upgrading a read-only userptr BO backed by copy-on-write pages, does
faulting the pages for write in amdgpu_ttm_tt_get_user_pages() fire the
BO's own MMU interval notifier and leave mem->invalid non-zero?
During hmm_range_fault(), write-faulting a read-only COW PTE runs
do_wp_page() -> wp_page_copy(), which invokes
mmu_notifier_invalidate_range_start() -> amdgpu_amdkfd_evict_userptr() on
bo->notifier. That increments mem->invalid and process_info->evicted_bos,
calls kgd2kfd_quiesce_mm() (taking an extra nested queue eviction reference
when the BO was mapped), schedules restore_userptr_work, and causes
hmm_range_fault() to abort with -EBUSY (-EAGAIN) after one 2 MiB PMD.
When user space retries the ioctl before restore_userptr_work runs and all
COW pages are now writable, amdgpu_amdkfd_gpuvm_upgrade_userptr_writable()
succeeds without clearing mem->invalid or moving mem off
userptr_inval_list. In kfd_ioctl_upgrade_userptr_writable(), the subsequent
amdgpu_amdkfd_gpuvm_map_memory_to_gpu() then sees is_invalid_userptr == true
(!!mem->invalid) and skips updating GPU PTEs and peer DMA mappings, while
kfd_process_restore_queues() drops only one eviction reference and returns 0
to user space with unmapped PTEs and evicted queues. In addition,
kfd_ioctl_upgrade_userptr_writable() does not hold process_info->lock across
unmap, sync_memory, kfd_flush_tlb() and dmaunmap, allowing a scheduled
restore_userptr_work to unbind and DMA-unmap mem->bo in
update_invalid_user_pages() before unmap fences complete and TLBs are
flushed.
[Severity: High]
Can pages invalidated after amdgpu_ttm_tt_get_user_pages() drops
mmap_read_lock(mm) still be bound into GTT and committed as writable
because amdgpu_hmm_range_valid(range) is never checked under
process_info->notifier_lock?
CPU0 CPU1
amdgpu_amdkfd_gpuvm_upgrade_userptr_writable()
amdgpu_ttm_tt_get_user_pages(bo, range)
mmap_read_lock(mm)
amdgpu_hmm_range_get_pages(...)
mmap_read_unlock(mm)
munmap() / mprotect(PROT_READ)
amdgpu_amdkfd_evict_userptr()
mutex_lock(¬ifier_lock)
mmu_interval_set_seq(...)
mem->invalid++
mutex_unlock(¬ifier_lock)
amdgpu_ttm_tt_set_user_pages(..., range)
ttm_bo_validate(...) <-- DMA-maps stale pages
mem->alloc_flags |= KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE
Because hmm_range_fault() does not pin page references and
amdgpu_amdkfd_evict_userptr() only takes process_info->notifier_lock, pages
freed or write-protected after mmap_read_unlock(mm) are still DMA-mapped
with DMA_BIDIRECTIONAL by ttm_bo_validate(), and range is freed instead of
being stored in mem->range. If the concurrent operation was
mprotect(PROT_READ), committing the upgrade also leaves
AMDGPU_GEM_USERPTR_READONLY cleared on bo->tbo.ttm, causing
restore_userptr_work -> update_invalid_user_pages() to fail with -EPERM and
reschedule itself every 1 ms indefinitely with queues evicted.
> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> index 5a7f4f0cd0ce7..b6cede98d3de5 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> @@ -1277,6 +1277,133 @@ static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep,
> return err;
> }
>
> +/* Upgrade an already registered userptr BO from read-only to read/write in
> + * place.
> + */
> +static int kfd_ioctl_upgrade_userptr_writable(struct file *filep,
> + struct kfd_process *p, void *data)
> +{
[ ... ]
> + if (mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE)
> + goto err_unlock;
[ ... ]
> + if (!err)
> + err = amdgpu_amdkfd_gpuvm_upgrade_userptr_writable(pdd->dev->adev, mem);
> +
> + if (has_mapped) {
> + long map_err;
> +
> + /* Restore mappings on every GPU that was unmapped: writable
> + * PTEs on success, or the original read-only PTEs on rollback.
> + */
> + for_each_set_bit(i, bitmap_unmapped, p->n_pdds) {
> + peer_pdd = p->pdds[i];
> + map_err = amdgpu_amdkfd_gpuvm_map_memory_to_gpu(peer_pdd->dev->adev,
> + mem, peer_pdd->drm_priv);
> + if (map_err && !err)
> + err = map_err;
> + }
> +
> + if (!bitmap_empty(bitmap_unmapped, p->n_pdds)) {
> + map_err = amdgpu_amdkfd_gpuvm_sync_memory(pdd->dev->adev, mem, false);
> + if (map_err && !err)
> + err = map_err;
> +
> + for_each_set_bit(i, bitmap_unmapped, p->n_pdds)
> + kfd_flush_tlb(p->pdds[i]);
> + }
> +
> + kfd_process_restore_queues(p);
> + }
[Severity: Medium]
If amdgpu_amdkfd_gpuvm_map_memory_to_gpu() fails during the remap loop (for
example with -ERESTARTSYS when a pending signal interrupts
reserve_bo_and_vm() or kfd_mem_dmamap_userptr()), does this leave the BO
unmapped while user queues are restored?
kfd_process_restore_queues(p) resumes the queues even when remapping failed
on one or more GPUs in bitmap_unmapped, so active queues fault on the
missing mappings. Retrying AMDKFD_IOC_UPGRADE_USERPTR_WRITABLE cannot
recover those mappings either: if the upgrade step succeeded,
KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE is already set in mem->alloc_flags so the
retry returns 0 immediately, and if the upgrade step failed,
entry->is_mapped is now false on the unmapped GPUs so a retry omits them
from bitmap_mapped.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201112332.44784-1-Junhua.Shen@amd.com?part=1
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] drm/amdkfd: add in-place userptr write access upgrade
2026-12-01 11:23 [PATCH] drm/amdkfd: add in-place userptr write access upgrade Junhua Shen
2026-12-01 11:48 ` sashiko-bot
@ 2026-12-01 14:16 ` Christian König
2026-12-01 19:04 ` Felix Kuehling
1 sibling, 1 reply; 4+ messages in thread
From: Christian König @ 2026-12-01 14:16 UTC (permalink / raw)
To: Junhua Shen, amd-gfx, Felix Kuehling, Philip Yang, Alex Deucher
Cc: Chris Freehill, Kent Russell, Maneesh Gupta, Denny Iriawan,
David Yat Sin, Huang Rui, Jenny Liu, Zhu Lingshan, Honglei Huang,
Yiru Ma
On 12/1/26 12:23, Junhua Shen wrote:
> Userptr BOs can be registered read-only so that pinning host memory for
> device access does not request write permission, which would, for
> example, break the copy-on-write sharing of a private file mapping.
> When the same host range later needs write access, the only option today
> is to unregister and re-register it, which recreates the BO and can move
> its GPU virtual address.
>
> Add AMDKFD_IOC_UPGRADE_USERPTR_WRITABLE to upgrade an existing read-only
> userptr BO to read/write in place, without changing its VA or recreating
> the BO. On failure the BO keeps its original read-only state.
Yeah that is clearly not somethign we can do.
> Signed-off-by: Junhua Shen <Junhua.Shen@amd.com>
> ---
> Background:
> In ROCm rocm-systems PR #15545 [1], the runtime changes host-to-device
> source pins from writable to read-only. Previously, those writable pins
> requested write permission even though the device only reads the buffer.
> On a MAP_PRIVATE file mapping, e.g. mmapped model weights, this breaks
> copy-on-write: every page is copied into anonymous memory, and for
> userptr the resulting MMU notifier evicts the process queues, repeatedly
> during a single model load.
>
> These read-only pins will be made internally by the runtime and avoid
> the COW copies, their memory overhead and the resulting queue evictions.
> However, the same host range can concurrently be registered writable by
> another user, such as hipHostRegister() or a device-to-host staging pin.
That is an illegal operation and needs to be rejected somehow.
> User space shares one BO per range and reference-counts it. With SVM, a
> writable request just clears GPU_RO on the existing range. With
> userptr, the read-only BO can only be freed and re-allocated, which
> recreates the BO and can move its GPU VA while other registrants still
> use it, so user space has to fail the writable request. This ioctl lets
> userptr behave like SVM.
That this doesn't work is perfectly intentional behavior, mappings must be made either readonly or read/write but not mixed by the application.
I think we rather need to fix the SVM side to return an error when a read-only range is made read-write without unmapping it in between.
So clear NAK from my side to this approach here.
Regards,
Christian.
>
> This builds on the read-only userptr registration series:
> https://lore.kernel.org/all/20261117042051.768201-1-Junhua.Shen@amd.com/
> Without it no KFD userptr is registered read-only and this ioctl is a no-op.
>
> Upgrade flow:
> - Reject non-writable or unmapped VMAs up front, before touching queues.
> - Evict the process queues, unmap the BO on every GPU where it is mapped,
> sync, flush TLBs and drop peer DMA mappings (still DMA_TO_DEVICE).
> - Re-fault the pages for write, unbind the old read-only GTT binding and
> rebind with DMA_BIDIRECTIONAL, then publish WRITABLE and refresh the
> cached attachment PTE flags.
> - Remap on the same GPUs, sync, flush TLBs and restore the queues.
> On any failure the BO keeps its read-only state and its original mappings
> are restored. -EAGAIN is propagated and retried by libhsakmt.
>
> User space and tests (ROCm rocm-systems [1]):
> - libhsakmt: use the ioctl when a read-only registered range is
> re-registered writable (fmm.c).
> - kfdtest: KFDMemoryTest.MemoryRegisterReadOnly and
> MemoryRegisterUserPtrUpgrade{,Unmapped,ProtReadFail,CowZeroPage,
> RemapAfterUnmap,ActiveQueue,MultiGPU}.
>
> [1] https://github.com/ROCm/rocm-systems/pull/15545
>
> Tested on gfx1151 (single GPU): all of the above pass with HSA_USE_SVM=0;
> MultiGPU is skipped on single-GPU systems, so the multi-GPU path has not
> been exercised on hardware.
>
> drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 3 +
> .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 200 ++++++++++++++++++
> drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 130 ++++++++++++
> include/uapi/linux/kfd_ioctl.h | 17 +-
> 4 files changed, 348 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h
> index 74dc307e5140..f1b899c18505 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h
> @@ -337,6 +337,9 @@ int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(struct amdgpu_device *adev,
> struct kgd_mem *mem, void *drm_priv);
> int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
> struct amdgpu_device *adev, struct kgd_mem *mem, void *drm_priv);
> +int amdgpu_amdkfd_gpuvm_check_userptr_writable(struct kgd_mem *mem);
> +int amdgpu_amdkfd_gpuvm_upgrade_userptr_writable(struct amdgpu_device *adev,
> + struct kgd_mem *mem);
> int amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void *drm_priv);
> int amdgpu_amdkfd_gpuvm_sync_memory(
> struct amdgpu_device *adev, struct kgd_mem *mem, bool intr);
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
> index 342a7cefaacd..51de1ba19d60 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
> @@ -1937,6 +1937,206 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
> return ret;
> }
>
> +/**
> + * amdgpu_amdkfd_gpuvm_check_userptr_writable - Fast pre-check that all VMAs
> + * backing a userptr BO are currently mapped and writable.
> + * @mem: KFD userptr BO to check
> + *
> + * Allows callers to reject non-writable (e.g. PROT_READ) or unmapped ranges
> + * before evicting user queues and tearing down GPU mappings.
> + *
> + * Return: 0 if the whole range is writable, -EPERM if a VMA is not writable,
> + * -EFAULT if part of the range is unmapped, or another negative error code.
> + */
> +int amdgpu_amdkfd_gpuvm_check_userptr_writable(struct kgd_mem *mem)
> +{
> + struct amdgpu_bo *bo = mem->bo;
> + struct vm_area_struct *vma;
> + struct mm_struct *mm;
> + unsigned long start, end, addr;
> + u64 user_addr = 0;
> + int ret;
> +
> + if (!bo || !amdgpu_ttm_tt_is_userptr(bo->tbo.ttm))
> + return -EINVAL;
> +
> + ret = amdgpu_ttm_tt_get_userptr(&bo->tbo, &user_addr);
> + if (ret)
> + return ret;
> +
> + mm = bo->notifier.mm;
> + if (unlikely(!mm))
> + return -EFAULT;
> +
> + if (!mmget_not_zero(mm))
> + return -ESRCH;
> +
> + start = user_addr;
> + if (check_add_overflow(start,
> + (unsigned long)bo->tbo.ttm->num_pages << PAGE_SHIFT,
> + &end)) {
> + mmput(mm);
> + return -EFAULT;
> + }
> +
> + mmap_read_lock(mm);
> + addr = start;
> + do {
> + vma = vma_lookup(mm, addr);
> + if (!vma) {
> + ret = -EFAULT;
> + break;
> + }
> + if (!(vma->vm_flags & VM_WRITE)) {
> + ret = -EPERM;
> + break;
> + }
> + addr = min(end, vma->vm_end);
> + } while (addr < end);
> + mmap_read_unlock(mm);
> + mmput(mm);
> +
> + return ret;
> +}
> +
> +/**
> + * amdgpu_amdkfd_gpuvm_upgrade_userptr_writable - In-place read-only -> read/write
> + * upgrade of a userptr BO.
> + * @adev: amdgpu device
> + * @mem: KFD BO to upgrade (must be an unmapped userptr BO)
> + *
> + * Caller must have quiesced user queues, unmapped and DMA-unmapped all GPU
> + * mappings of @mem, and will remap them after this returns. On failure, the
> + * BO's read-only state is preserved so the caller can restore the original
> + * read-only mappings.
> + *
> + * Return: 0 on success or if the BO is already writable, negative error code
> + * on failure.
> + */
> +int amdgpu_amdkfd_gpuvm_upgrade_userptr_writable(struct amdgpu_device *adev,
> + struct kgd_mem *mem)
> +{
> + struct amdkfd_process_info *process_info = mem->process_info;
> + struct amdgpu_bo *bo = mem->bo;
> + struct ttm_operation_ctx ctx = {
> + .interruptible = false,
> + .no_wait_gpu = false,
> + };
> + struct kfd_mem_attachment *entry;
> + struct amdgpu_hmm_range *range;
> + u64 user_addr = 0;
> + bool unbound = false;
> + int ret = 0;
> +
> + if (!bo || !amdgpu_ttm_tt_is_userptr(bo->tbo.ttm))
> + return -EINVAL;
> +
> + /* Already writable: nothing to do. */
> + if (!amdgpu_ttm_tt_is_readonly(bo->tbo.ttm))
> + return 0;
> +
> + ret = amdgpu_ttm_tt_get_userptr(&bo->tbo, &user_addr);
> + if (ret)
> + return ret;
> +
> + mutex_lock(&process_info->lock);
> +
> + /* Clear the read-only flag and re-fault the user pages for write
> + * before touching the BO's placement, so a failure (e.g. -EPERM on a
> + * PROT_READ VMA or -EFAULT) leaves the existing read-only BO intact.
> + */
> + ret = amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr, 0);
> + if (ret)
> + goto out_unlock;
> +
> + range = amdgpu_hmm_range_alloc(NULL);
> + if (unlikely(!range)) {
> + ret = -ENOMEM;
> + goto out_restore_ro;
> + }
> +
> + ret = amdgpu_ttm_tt_get_user_pages(bo, range);
> + if (ret) {
> + if (ret == -EAGAIN || ret == -EPERM || ret == -EFAULT)
> + pr_debug("%s: Failed to get user pages: %d\n",
> + __func__, ret);
> + else
> + pr_err("%s: Failed to get user pages: %d\n",
> + __func__, ret);
> + goto out_free_range;
> + }
> +
> + ret = amdgpu_bo_reserve(bo, true);
> + if (ret) {
> + pr_err("%s: Failed to reserve BO\n", __func__);
> + goto out_free_range;
> + }
> +
> + /* Unbind the old GTT mapping with READONLY still set so TTM unmaps
> + * the SG table with its original DMA_TO_DEVICE direction, then rebind
> + * the writable pages with DMA_BIDIRECTIONAL.
> + */
> + if (bo->tbo.resource->mem_type != TTM_PL_SYSTEM) {
> + amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr,
> + AMDGPU_GEM_USERPTR_READONLY);
> + amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU);
> + ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
> + if (ret) {
> + pr_err("%s: failed to unbind RO userptr BO\n", __func__);
> + amdgpu_bo_unreserve(bo);
> + amdgpu_hmm_range_free(range);
> + goto out_unlock;
> + }
> + unbound = true;
> + amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr, 0);
> + }
> +
> + amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm, range);
> + amdgpu_bo_placement_from_domain(bo, mem->domain);
> + ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
> + if (ret) {
> + pr_err("%s: failed to validate BO: %d\n", __func__, ret);
> + amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr,
> + AMDGPU_GEM_USERPTR_READONLY);
> + if (unbound) {
> + /* Re-bind read-only so the BO is not stranded in
> + * TTM_PL_SYSTEM when the caller remaps it.
> + */
> + amdgpu_bo_placement_from_domain(bo, mem->domain);
> + if (ttm_bo_validate(&bo->tbo, &bo->placement, &ctx))
> + pr_err("%s: failed to restore RO userptr BO\n",
> + __func__);
> + }
> + amdgpu_bo_unreserve(bo);
> + amdgpu_hmm_range_free(range);
> + goto out_unlock;
> + }
> + amdgpu_bo_unreserve(bo);
> + amdgpu_hmm_range_free(range);
> +
> + /* Publish the writable state and refresh cached attachment PTE flags
> + * so the caller's remap installs writable PTEs.
> + */
> + mutex_lock(&mem->lock);
> + mem->alloc_flags |= KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE;
> + list_for_each_entry(entry, &mem->attachments, list)
> + entry->pte_flags = get_pte_flags(entry->adev,
> + entry->bo_va->base.vm, mem);
> + mutex_unlock(&mem->lock);
> +
> + mutex_unlock(&process_info->lock);
> + return 0;
> +
> +out_free_range:
> + amdgpu_hmm_range_free(range);
> +out_restore_ro:
> + amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr,
> + AMDGPU_GEM_USERPTR_READONLY);
> +out_unlock:
> + mutex_unlock(&process_info->lock);
> + return ret;
> +}
> +
> int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
> struct amdgpu_device *adev, struct kgd_mem *mem, void *drm_priv,
> uint64_t *size)
> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> index 3d2136adb62b..05898e8b5064 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> @@ -1277,6 +1277,133 @@ static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep,
> return err;
> }
>
> +/* Upgrade an already registered userptr BO from read-only to read/write in
> + * place.
> + */
> +static int kfd_ioctl_upgrade_userptr_writable(struct file *filep,
> + struct kfd_process *p, void *data)
> +{
> + struct kfd_ioctl_upgrade_userptr_writable_args *args = data;
> + struct kfd_process_device *pdd, *peer_pdd;
> + DECLARE_BITMAP(bitmap_mapped, MAX_GPU_INSTANCE);
> + DECLARE_BITMAP(bitmap_unmapped, MAX_GPU_INSTANCE);
> + bool has_mapped = false;
> + struct kgd_mem *mem;
> + long err = 0;
> + int i;
> +
> + mutex_lock(&p->mutex);
> +
> + pdd = kfd_process_device_data_by_id(p, GET_GPU_ID(args->handle));
> + if (!pdd) {
> + err = -EINVAL;
> + goto err_unlock;
> + }
> +
> + mem = kfd_process_device_translate_handle(pdd, GET_IDR_HANDLE(args->handle));
> + if (!mem) {
> + err = -EINVAL;
> + goto err_unlock;
> + }
> +
> + if (!(mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_USERPTR)) {
> + err = -EINVAL;
> + goto err_unlock;
> + }
> +
> + if (mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE)
> + goto err_unlock;
> +
> + /* Fast pre-check: reject non-writable or unmapped VMAs before
> + * evicting user queues and tearing down GPU mappings.
> + */
> + err = amdgpu_amdkfd_gpuvm_check_userptr_writable(mem);
> + if (err)
> + goto err_unlock;
> +
> + bitmap_zero(bitmap_mapped, MAX_GPU_INSTANCE);
> + bitmap_zero(bitmap_unmapped, MAX_GPU_INSTANCE);
> + if (mem->mapped_to_gpu_memory) {
> + for (i = 0; i < p->n_pdds; i++) {
> + if (p->pdds[i]->drm_priv &&
> + amdgpu_amdkfd_bo_mapped_to_dev(p->pdds[i]->drm_priv,
> + mem))
> + set_bit(i, bitmap_mapped);
> + }
> + has_mapped = !bitmap_empty(bitmap_mapped, p->n_pdds);
> + }
> +
> + if (has_mapped) {
> + /* Stop all user queues before tearing down the read-only
> + * mappings so no in-flight GPU access faults on unmapped PTEs.
> + */
> + err = kfd_process_evict_queues(p, KFD_QUEUE_EVICTION_TRIGGER_USERPTR);
> + if (err)
> + goto err_unlock;
> +
> + for_each_set_bit(i, bitmap_mapped, p->n_pdds) {
> + peer_pdd = p->pdds[i];
> + err = amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(peer_pdd->dev->adev,
> + mem, peer_pdd->drm_priv);
> + if (err)
> + break;
> + set_bit(i, bitmap_unmapped);
> + }
> +
> + /* Sync, flush TLBs, and drop peer DMA mappings for every GPU
> + * actually unmapped above before either upgrading or remapping,
> + * so dmaunmap uses the original DMA_TO_DEVICE direction and
> + * remap will not leak an existing peer SG table.
> + */
> + if (!bitmap_empty(bitmap_unmapped, p->n_pdds)) {
> + long sync_err;
> +
> + sync_err = amdgpu_amdkfd_gpuvm_sync_memory(pdd->dev->adev, mem, false);
> + if (sync_err && !err)
> + err = sync_err;
> +
> + for_each_set_bit(i, bitmap_unmapped, p->n_pdds) {
> + peer_pdd = p->pdds[i];
> + kfd_flush_tlb(peer_pdd);
> + amdgpu_amdkfd_gpuvm_dmaunmap_mem(mem, peer_pdd->drm_priv);
> + }
> + }
> + }
> +
> + if (!err)
> + err = amdgpu_amdkfd_gpuvm_upgrade_userptr_writable(pdd->dev->adev, mem);
> +
> + if (has_mapped) {
> + long map_err;
> +
> + /* Restore mappings on every GPU that was unmapped: writable
> + * PTEs on success, or the original read-only PTEs on rollback.
> + */
> + for_each_set_bit(i, bitmap_unmapped, p->n_pdds) {
> + peer_pdd = p->pdds[i];
> + map_err = amdgpu_amdkfd_gpuvm_map_memory_to_gpu(peer_pdd->dev->adev,
> + mem, peer_pdd->drm_priv);
> + if (map_err && !err)
> + err = map_err;
> + }
> +
> + if (!bitmap_empty(bitmap_unmapped, p->n_pdds)) {
> + map_err = amdgpu_amdkfd_gpuvm_sync_memory(pdd->dev->adev, mem, false);
> + if (map_err && !err)
> + err = map_err;
> +
> + for_each_set_bit(i, bitmap_unmapped, p->n_pdds)
> + kfd_flush_tlb(p->pdds[i]);
> + }
> +
> + kfd_process_restore_queues(p);
> + }
> +
> +err_unlock:
> + mutex_unlock(&p->mutex);
> + return err;
> +}
> +
> static int kfd_ioctl_free_memory_of_gpu(struct file *filep,
> struct kfd_process *p, void *data)
> {
> @@ -3682,6 +3809,9 @@ static const struct amdkfd_ioctl_desc amdkfd_ioctls[] = {
>
> AMDKFD_IOCTL_DEF(AMDKFD_IOC_PROFILER,
> kfd_ioctl_profiler, 0),
> +
> + AMDKFD_IOCTL_DEF(AMDKFD_IOC_UPGRADE_USERPTR_WRITABLE,
> + kfd_ioctl_upgrade_userptr_writable, 0),
> };
>
> #define AMDKFD_CORE_IOCTL_COUNT ARRAY_SIZE(amdkfd_ioctls)
> diff --git a/include/uapi/linux/kfd_ioctl.h b/include/uapi/linux/kfd_ioctl.h
> index 3d608d6b0ab8..d08166fe98fd 100644
> --- a/include/uapi/linux/kfd_ioctl.h
> +++ b/include/uapi/linux/kfd_ioctl.h
> @@ -50,9 +50,10 @@
> * - 1.22 - Add queue creation with metadata ring base address
> * - 1.23 - Add profiler control ioctl to enable/disable profiler on a process
> * - 1.24 - Enable SVM no-access attributes
> + * - 1.25 - Add userptr read-only -> read/write upgrade ioctl
> */
> #define KFD_IOCTL_MAJOR_VERSION 1
> -#define KFD_IOCTL_MINOR_VERSION 24
> +#define KFD_IOCTL_MINOR_VERSION 25
>
> struct kfd_ioctl_get_version_args {
> __u32 major_version; /* from KFD */
> @@ -449,6 +450,15 @@ struct kfd_ioctl_alloc_memory_of_gpu_args {
> __u32 flags;
> };
>
> +/* Upgrade an already registered userptr BO from read-only to read/write in place
> + *
> + * Rebuilds the GPU mapping with write permission without a free/re-alloc; user
> + * space does not unmap. Only the read-only -> read/write direction is supported.
> + */
> +struct kfd_ioctl_upgrade_userptr_writable_args {
> + __u64 handle; /* to KFD */
> +};
> +
> /* Free memory allocated with kfd_ioctl_alloc_memory_of_gpu
> *
> * @handle: memory handle returned by alloc
> @@ -1723,7 +1733,10 @@ struct kfd_ioctl_profiler_args {
> #define AMDKFD_IOC_PROFILER \
> AMDKFD_IOWR(0x28, struct kfd_ioctl_profiler_args)
>
> +#define AMDKFD_IOC_UPGRADE_USERPTR_WRITABLE \
> + AMDKFD_IOW(0x29, struct kfd_ioctl_upgrade_userptr_writable_args)
> +
> #define AMDKFD_COMMAND_START 0x01
> -#define AMDKFD_COMMAND_END 0x29
> +#define AMDKFD_COMMAND_END 0x2A
>
> #endif
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] drm/amdkfd: add in-place userptr write access upgrade
2026-12-01 14:16 ` Christian König
@ 2026-12-01 19:04 ` Felix Kuehling
0 siblings, 0 replies; 4+ messages in thread
From: Felix Kuehling @ 2026-12-01 19:04 UTC (permalink / raw)
To: Christian König, Junhua Shen, amd-gfx, Philip Yang,
Alex Deucher
Cc: Chris Freehill, Kent Russell, Maneesh Gupta, Denny Iriawan,
David Yat Sin, Huang Rui, Jenny Liu, Zhu Lingshan, Honglei Huang,
Yiru Ma
On 2026-12-01 09:16, Christian König wrote:
> On 12/1/26 12:23, Junhua Shen wrote:
>> Userptr BOs can be registered read-only so that pinning host memory for
>> device access does not request write permission, which would, for
>> example, break the copy-on-write sharing of a private file mapping.
>> When the same host range later needs write access, the only option today
>> is to unregister and re-register it, which recreates the BO and can move
>> its GPU virtual address.
>>
>> Add AMDKFD_IOC_UPGRADE_USERPTR_WRITABLE to upgrade an existing read-only
>> userptr BO to read/write in place, without changing its VA or recreating
>> the BO. On failure the BO keeps its original read-only state.
> Yeah that is clearly not somethign we can do.
>
>> Signed-off-by: Junhua Shen <Junhua.Shen@amd.com>
>> ---
>> Background:
>> In ROCm rocm-systems PR #15545 [1], the runtime changes host-to-device
>> source pins from writable to read-only. Previously, those writable pins
>> requested write permission even though the device only reads the buffer.
>> On a MAP_PRIVATE file mapping, e.g. mmapped model weights, this breaks
>> copy-on-write: every page is copied into anonymous memory, and for
>> userptr the resulting MMU notifier evicts the process queues, repeatedly
>> during a single model load.
>>
>> These read-only pins will be made internally by the runtime and avoid
>> the COW copies, their memory overhead and the resulting queue evictions.
>> However, the same host range can concurrently be registered writable by
>> another user, such as hipHostRegister() or a device-to-host staging pin.
> That is an illegal operation and needs to be rejected somehow.
>
>> User space shares one BO per range and reference-counts it. With SVM, a
>> writable request just clears GPU_RO on the existing range. With
>> userptr, the read-only BO can only be freed and re-allocated, which
>> recreates the BO and can move its GPU VA while other registrants still
>> use it, so user space has to fail the writable request. This ioctl lets
>> userptr behave like SVM.
> That this doesn't work is perfectly intentional behavior, mappings must be made either readonly or read/write but not mixed by the application.
>
> I think we rather need to fix the SVM side to return an error when a read-only range is made read-write without unmapping it in between.
AFAICT this has nothing to do with SVM. This is about the legacy userptr
API. ROCr shouldn't even use this on GPU+kernel combinations that
support SVM. Therefore I strongly suspect that this kind of hack isn't
needed in the upstream driver in the first place.
>
> So clear NAK from my side to this approach here.
I agree.
Regards,
Felix
>
> Regards,
> Christian.
>
>> This builds on the read-only userptr registration series:
>> https://lore.kernel.org/all/20261117042051.768201-1-Junhua.Shen@amd.com/
>> Without it no KFD userptr is registered read-only and this ioctl is a no-op.
>>
>> Upgrade flow:
>> - Reject non-writable or unmapped VMAs up front, before touching queues.
>> - Evict the process queues, unmap the BO on every GPU where it is mapped,
>> sync, flush TLBs and drop peer DMA mappings (still DMA_TO_DEVICE).
>> - Re-fault the pages for write, unbind the old read-only GTT binding and
>> rebind with DMA_BIDIRECTIONAL, then publish WRITABLE and refresh the
>> cached attachment PTE flags.
>> - Remap on the same GPUs, sync, flush TLBs and restore the queues.
>> On any failure the BO keeps its read-only state and its original mappings
>> are restored. -EAGAIN is propagated and retried by libhsakmt.
>>
>> User space and tests (ROCm rocm-systems [1]):
>> - libhsakmt: use the ioctl when a read-only registered range is
>> re-registered writable (fmm.c).
>> - kfdtest: KFDMemoryTest.MemoryRegisterReadOnly and
>> MemoryRegisterUserPtrUpgrade{,Unmapped,ProtReadFail,CowZeroPage,
>> RemapAfterUnmap,ActiveQueue,MultiGPU}.
>>
>> [1] https://github.com/ROCm/rocm-systems/pull/15545
>>
>> Tested on gfx1151 (single GPU): all of the above pass with HSA_USE_SVM=0;
>> MultiGPU is skipped on single-GPU systems, so the multi-GPU path has not
>> been exercised on hardware.
>>
>> drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 3 +
>> .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 200 ++++++++++++++++++
>> drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 130 ++++++++++++
>> include/uapi/linux/kfd_ioctl.h | 17 +-
>> 4 files changed, 348 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h
>> index 74dc307e5140..f1b899c18505 100644
>> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h
>> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h
>> @@ -337,6 +337,9 @@ int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(struct amdgpu_device *adev,
>> struct kgd_mem *mem, void *drm_priv);
>> int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
>> struct amdgpu_device *adev, struct kgd_mem *mem, void *drm_priv);
>> +int amdgpu_amdkfd_gpuvm_check_userptr_writable(struct kgd_mem *mem);
>> +int amdgpu_amdkfd_gpuvm_upgrade_userptr_writable(struct amdgpu_device *adev,
>> + struct kgd_mem *mem);
>> int amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void *drm_priv);
>> int amdgpu_amdkfd_gpuvm_sync_memory(
>> struct amdgpu_device *adev, struct kgd_mem *mem, bool intr);
>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
>> index 342a7cefaacd..51de1ba19d60 100644
>> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
>> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
>> @@ -1937,6 +1937,206 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
>> return ret;
>> }
>>
>> +/**
>> + * amdgpu_amdkfd_gpuvm_check_userptr_writable - Fast pre-check that all VMAs
>> + * backing a userptr BO are currently mapped and writable.
>> + * @mem: KFD userptr BO to check
>> + *
>> + * Allows callers to reject non-writable (e.g. PROT_READ) or unmapped ranges
>> + * before evicting user queues and tearing down GPU mappings.
>> + *
>> + * Return: 0 if the whole range is writable, -EPERM if a VMA is not writable,
>> + * -EFAULT if part of the range is unmapped, or another negative error code.
>> + */
>> +int amdgpu_amdkfd_gpuvm_check_userptr_writable(struct kgd_mem *mem)
>> +{
>> + struct amdgpu_bo *bo = mem->bo;
>> + struct vm_area_struct *vma;
>> + struct mm_struct *mm;
>> + unsigned long start, end, addr;
>> + u64 user_addr = 0;
>> + int ret;
>> +
>> + if (!bo || !amdgpu_ttm_tt_is_userptr(bo->tbo.ttm))
>> + return -EINVAL;
>> +
>> + ret = amdgpu_ttm_tt_get_userptr(&bo->tbo, &user_addr);
>> + if (ret)
>> + return ret;
>> +
>> + mm = bo->notifier.mm;
>> + if (unlikely(!mm))
>> + return -EFAULT;
>> +
>> + if (!mmget_not_zero(mm))
>> + return -ESRCH;
>> +
>> + start = user_addr;
>> + if (check_add_overflow(start,
>> + (unsigned long)bo->tbo.ttm->num_pages << PAGE_SHIFT,
>> + &end)) {
>> + mmput(mm);
>> + return -EFAULT;
>> + }
>> +
>> + mmap_read_lock(mm);
>> + addr = start;
>> + do {
>> + vma = vma_lookup(mm, addr);
>> + if (!vma) {
>> + ret = -EFAULT;
>> + break;
>> + }
>> + if (!(vma->vm_flags & VM_WRITE)) {
>> + ret = -EPERM;
>> + break;
>> + }
>> + addr = min(end, vma->vm_end);
>> + } while (addr < end);
>> + mmap_read_unlock(mm);
>> + mmput(mm);
>> +
>> + return ret;
>> +}
>> +
>> +/**
>> + * amdgpu_amdkfd_gpuvm_upgrade_userptr_writable - In-place read-only -> read/write
>> + * upgrade of a userptr BO.
>> + * @adev: amdgpu device
>> + * @mem: KFD BO to upgrade (must be an unmapped userptr BO)
>> + *
>> + * Caller must have quiesced user queues, unmapped and DMA-unmapped all GPU
>> + * mappings of @mem, and will remap them after this returns. On failure, the
>> + * BO's read-only state is preserved so the caller can restore the original
>> + * read-only mappings.
>> + *
>> + * Return: 0 on success or if the BO is already writable, negative error code
>> + * on failure.
>> + */
>> +int amdgpu_amdkfd_gpuvm_upgrade_userptr_writable(struct amdgpu_device *adev,
>> + struct kgd_mem *mem)
>> +{
>> + struct amdkfd_process_info *process_info = mem->process_info;
>> + struct amdgpu_bo *bo = mem->bo;
>> + struct ttm_operation_ctx ctx = {
>> + .interruptible = false,
>> + .no_wait_gpu = false,
>> + };
>> + struct kfd_mem_attachment *entry;
>> + struct amdgpu_hmm_range *range;
>> + u64 user_addr = 0;
>> + bool unbound = false;
>> + int ret = 0;
>> +
>> + if (!bo || !amdgpu_ttm_tt_is_userptr(bo->tbo.ttm))
>> + return -EINVAL;
>> +
>> + /* Already writable: nothing to do. */
>> + if (!amdgpu_ttm_tt_is_readonly(bo->tbo.ttm))
>> + return 0;
>> +
>> + ret = amdgpu_ttm_tt_get_userptr(&bo->tbo, &user_addr);
>> + if (ret)
>> + return ret;
>> +
>> + mutex_lock(&process_info->lock);
>> +
>> + /* Clear the read-only flag and re-fault the user pages for write
>> + * before touching the BO's placement, so a failure (e.g. -EPERM on a
>> + * PROT_READ VMA or -EFAULT) leaves the existing read-only BO intact.
>> + */
>> + ret = amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr, 0);
>> + if (ret)
>> + goto out_unlock;
>> +
>> + range = amdgpu_hmm_range_alloc(NULL);
>> + if (unlikely(!range)) {
>> + ret = -ENOMEM;
>> + goto out_restore_ro;
>> + }
>> +
>> + ret = amdgpu_ttm_tt_get_user_pages(bo, range);
>> + if (ret) {
>> + if (ret == -EAGAIN || ret == -EPERM || ret == -EFAULT)
>> + pr_debug("%s: Failed to get user pages: %d\n",
>> + __func__, ret);
>> + else
>> + pr_err("%s: Failed to get user pages: %d\n",
>> + __func__, ret);
>> + goto out_free_range;
>> + }
>> +
>> + ret = amdgpu_bo_reserve(bo, true);
>> + if (ret) {
>> + pr_err("%s: Failed to reserve BO\n", __func__);
>> + goto out_free_range;
>> + }
>> +
>> + /* Unbind the old GTT mapping with READONLY still set so TTM unmaps
>> + * the SG table with its original DMA_TO_DEVICE direction, then rebind
>> + * the writable pages with DMA_BIDIRECTIONAL.
>> + */
>> + if (bo->tbo.resource->mem_type != TTM_PL_SYSTEM) {
>> + amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr,
>> + AMDGPU_GEM_USERPTR_READONLY);
>> + amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU);
>> + ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
>> + if (ret) {
>> + pr_err("%s: failed to unbind RO userptr BO\n", __func__);
>> + amdgpu_bo_unreserve(bo);
>> + amdgpu_hmm_range_free(range);
>> + goto out_unlock;
>> + }
>> + unbound = true;
>> + amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr, 0);
>> + }
>> +
>> + amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm, range);
>> + amdgpu_bo_placement_from_domain(bo, mem->domain);
>> + ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
>> + if (ret) {
>> + pr_err("%s: failed to validate BO: %d\n", __func__, ret);
>> + amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr,
>> + AMDGPU_GEM_USERPTR_READONLY);
>> + if (unbound) {
>> + /* Re-bind read-only so the BO is not stranded in
>> + * TTM_PL_SYSTEM when the caller remaps it.
>> + */
>> + amdgpu_bo_placement_from_domain(bo, mem->domain);
>> + if (ttm_bo_validate(&bo->tbo, &bo->placement, &ctx))
>> + pr_err("%s: failed to restore RO userptr BO\n",
>> + __func__);
>> + }
>> + amdgpu_bo_unreserve(bo);
>> + amdgpu_hmm_range_free(range);
>> + goto out_unlock;
>> + }
>> + amdgpu_bo_unreserve(bo);
>> + amdgpu_hmm_range_free(range);
>> +
>> + /* Publish the writable state and refresh cached attachment PTE flags
>> + * so the caller's remap installs writable PTEs.
>> + */
>> + mutex_lock(&mem->lock);
>> + mem->alloc_flags |= KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE;
>> + list_for_each_entry(entry, &mem->attachments, list)
>> + entry->pte_flags = get_pte_flags(entry->adev,
>> + entry->bo_va->base.vm, mem);
>> + mutex_unlock(&mem->lock);
>> +
>> + mutex_unlock(&process_info->lock);
>> + return 0;
>> +
>> +out_free_range:
>> + amdgpu_hmm_range_free(range);
>> +out_restore_ro:
>> + amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr,
>> + AMDGPU_GEM_USERPTR_READONLY);
>> +out_unlock:
>> + mutex_unlock(&process_info->lock);
>> + return ret;
>> +}
>> +
>> int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
>> struct amdgpu_device *adev, struct kgd_mem *mem, void *drm_priv,
>> uint64_t *size)
>> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
>> index 3d2136adb62b..05898e8b5064 100644
>> --- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
>> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
>> @@ -1277,6 +1277,133 @@ static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep,
>> return err;
>> }
>>
>> +/* Upgrade an already registered userptr BO from read-only to read/write in
>> + * place.
>> + */
>> +static int kfd_ioctl_upgrade_userptr_writable(struct file *filep,
>> + struct kfd_process *p, void *data)
>> +{
>> + struct kfd_ioctl_upgrade_userptr_writable_args *args = data;
>> + struct kfd_process_device *pdd, *peer_pdd;
>> + DECLARE_BITMAP(bitmap_mapped, MAX_GPU_INSTANCE);
>> + DECLARE_BITMAP(bitmap_unmapped, MAX_GPU_INSTANCE);
>> + bool has_mapped = false;
>> + struct kgd_mem *mem;
>> + long err = 0;
>> + int i;
>> +
>> + mutex_lock(&p->mutex);
>> +
>> + pdd = kfd_process_device_data_by_id(p, GET_GPU_ID(args->handle));
>> + if (!pdd) {
>> + err = -EINVAL;
>> + goto err_unlock;
>> + }
>> +
>> + mem = kfd_process_device_translate_handle(pdd, GET_IDR_HANDLE(args->handle));
>> + if (!mem) {
>> + err = -EINVAL;
>> + goto err_unlock;
>> + }
>> +
>> + if (!(mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_USERPTR)) {
>> + err = -EINVAL;
>> + goto err_unlock;
>> + }
>> +
>> + if (mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE)
>> + goto err_unlock;
>> +
>> + /* Fast pre-check: reject non-writable or unmapped VMAs before
>> + * evicting user queues and tearing down GPU mappings.
>> + */
>> + err = amdgpu_amdkfd_gpuvm_check_userptr_writable(mem);
>> + if (err)
>> + goto err_unlock;
>> +
>> + bitmap_zero(bitmap_mapped, MAX_GPU_INSTANCE);
>> + bitmap_zero(bitmap_unmapped, MAX_GPU_INSTANCE);
>> + if (mem->mapped_to_gpu_memory) {
>> + for (i = 0; i < p->n_pdds; i++) {
>> + if (p->pdds[i]->drm_priv &&
>> + amdgpu_amdkfd_bo_mapped_to_dev(p->pdds[i]->drm_priv,
>> + mem))
>> + set_bit(i, bitmap_mapped);
>> + }
>> + has_mapped = !bitmap_empty(bitmap_mapped, p->n_pdds);
>> + }
>> +
>> + if (has_mapped) {
>> + /* Stop all user queues before tearing down the read-only
>> + * mappings so no in-flight GPU access faults on unmapped PTEs.
>> + */
>> + err = kfd_process_evict_queues(p, KFD_QUEUE_EVICTION_TRIGGER_USERPTR);
>> + if (err)
>> + goto err_unlock;
>> +
>> + for_each_set_bit(i, bitmap_mapped, p->n_pdds) {
>> + peer_pdd = p->pdds[i];
>> + err = amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(peer_pdd->dev->adev,
>> + mem, peer_pdd->drm_priv);
>> + if (err)
>> + break;
>> + set_bit(i, bitmap_unmapped);
>> + }
>> +
>> + /* Sync, flush TLBs, and drop peer DMA mappings for every GPU
>> + * actually unmapped above before either upgrading or remapping,
>> + * so dmaunmap uses the original DMA_TO_DEVICE direction and
>> + * remap will not leak an existing peer SG table.
>> + */
>> + if (!bitmap_empty(bitmap_unmapped, p->n_pdds)) {
>> + long sync_err;
>> +
>> + sync_err = amdgpu_amdkfd_gpuvm_sync_memory(pdd->dev->adev, mem, false);
>> + if (sync_err && !err)
>> + err = sync_err;
>> +
>> + for_each_set_bit(i, bitmap_unmapped, p->n_pdds) {
>> + peer_pdd = p->pdds[i];
>> + kfd_flush_tlb(peer_pdd);
>> + amdgpu_amdkfd_gpuvm_dmaunmap_mem(mem, peer_pdd->drm_priv);
>> + }
>> + }
>> + }
>> +
>> + if (!err)
>> + err = amdgpu_amdkfd_gpuvm_upgrade_userptr_writable(pdd->dev->adev, mem);
>> +
>> + if (has_mapped) {
>> + long map_err;
>> +
>> + /* Restore mappings on every GPU that was unmapped: writable
>> + * PTEs on success, or the original read-only PTEs on rollback.
>> + */
>> + for_each_set_bit(i, bitmap_unmapped, p->n_pdds) {
>> + peer_pdd = p->pdds[i];
>> + map_err = amdgpu_amdkfd_gpuvm_map_memory_to_gpu(peer_pdd->dev->adev,
>> + mem, peer_pdd->drm_priv);
>> + if (map_err && !err)
>> + err = map_err;
>> + }
>> +
>> + if (!bitmap_empty(bitmap_unmapped, p->n_pdds)) {
>> + map_err = amdgpu_amdkfd_gpuvm_sync_memory(pdd->dev->adev, mem, false);
>> + if (map_err && !err)
>> + err = map_err;
>> +
>> + for_each_set_bit(i, bitmap_unmapped, p->n_pdds)
>> + kfd_flush_tlb(p->pdds[i]);
>> + }
>> +
>> + kfd_process_restore_queues(p);
>> + }
>> +
>> +err_unlock:
>> + mutex_unlock(&p->mutex);
>> + return err;
>> +}
>> +
>> static int kfd_ioctl_free_memory_of_gpu(struct file *filep,
>> struct kfd_process *p, void *data)
>> {
>> @@ -3682,6 +3809,9 @@ static const struct amdkfd_ioctl_desc amdkfd_ioctls[] = {
>>
>> AMDKFD_IOCTL_DEF(AMDKFD_IOC_PROFILER,
>> kfd_ioctl_profiler, 0),
>> +
>> + AMDKFD_IOCTL_DEF(AMDKFD_IOC_UPGRADE_USERPTR_WRITABLE,
>> + kfd_ioctl_upgrade_userptr_writable, 0),
>> };
>>
>> #define AMDKFD_CORE_IOCTL_COUNT ARRAY_SIZE(amdkfd_ioctls)
>> diff --git a/include/uapi/linux/kfd_ioctl.h b/include/uapi/linux/kfd_ioctl.h
>> index 3d608d6b0ab8..d08166fe98fd 100644
>> --- a/include/uapi/linux/kfd_ioctl.h
>> +++ b/include/uapi/linux/kfd_ioctl.h
>> @@ -50,9 +50,10 @@
>> * - 1.22 - Add queue creation with metadata ring base address
>> * - 1.23 - Add profiler control ioctl to enable/disable profiler on a process
>> * - 1.24 - Enable SVM no-access attributes
>> + * - 1.25 - Add userptr read-only -> read/write upgrade ioctl
>> */
>> #define KFD_IOCTL_MAJOR_VERSION 1
>> -#define KFD_IOCTL_MINOR_VERSION 24
>> +#define KFD_IOCTL_MINOR_VERSION 25
>>
>> struct kfd_ioctl_get_version_args {
>> __u32 major_version; /* from KFD */
>> @@ -449,6 +450,15 @@ struct kfd_ioctl_alloc_memory_of_gpu_args {
>> __u32 flags;
>> };
>>
>> +/* Upgrade an already registered userptr BO from read-only to read/write in place
>> + *
>> + * Rebuilds the GPU mapping with write permission without a free/re-alloc; user
>> + * space does not unmap. Only the read-only -> read/write direction is supported.
>> + */
>> +struct kfd_ioctl_upgrade_userptr_writable_args {
>> + __u64 handle; /* to KFD */
>> +};
>> +
>> /* Free memory allocated with kfd_ioctl_alloc_memory_of_gpu
>> *
>> * @handle: memory handle returned by alloc
>> @@ -1723,7 +1733,10 @@ struct kfd_ioctl_profiler_args {
>> #define AMDKFD_IOC_PROFILER \
>> AMDKFD_IOWR(0x28, struct kfd_ioctl_profiler_args)
>>
>> +#define AMDKFD_IOC_UPGRADE_USERPTR_WRITABLE \
>> + AMDKFD_IOW(0x29, struct kfd_ioctl_upgrade_userptr_writable_args)
>> +
>> #define AMDKFD_COMMAND_START 0x01
>> -#define AMDKFD_COMMAND_END 0x29
>> +#define AMDKFD_COMMAND_END 0x2A
>>
>> #endif
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-01 19:04 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 11:23 [PATCH] drm/amdkfd: add in-place userptr write access upgrade Junhua Shen
2026-12-01 11:48 ` sashiko-bot
2026-12-01 14:16 ` Christian König
2026-12-01 19:04 ` Felix Kuehling
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.