From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AB1DC606A31A for ; Tue, 1 Dec 2026 11:48:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Hu0Jxr4S Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 4F1E110F824; Tue, 1 Dec 2026 11:48:30 +0000 (UTC) Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by gabe.freedesktop.org (Postfix) with ESMTPS id CC25410E050 for ; Tue, 1 Dec 2026 11:48:24 +0000 (UTC) Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 8F2FB4399F; Tue, 1 Dec 2026 11:48:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5A5171F00893; Tue, 1 Dec 2026 11:48:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796125704; bh=JgaGFjmJu+YM7ij5+wMVWLKcftRYD1n1Q3Ss27NxUnk=; h=From:Subject:Reply-To:To:In-Reply-To:References:Date; b=Hu0Jxr4SbmdN21eBT52q614h6VxtpWiMxiB5E94kVMtcKJT3rstSR1XgAVnjZHUHW CX8FltrjOiwdJAPgozZObNHy8aaEm/JPd9uIaXXcabU1RvelK56W9LKVQYw2ZiRHs+ jwO86ucmLJV/X5DWIASy36Acarv3XEljmPyE7bM/jSXG2v8Z7jQ/TKXqfiMrEKm1md AuHuNQpQGk/eGYK0DBI9u3JnJSYkgwuK3MdDZsWlA6Q0uNWwLNu4lGv60ZFiHcJoEj SFsbLN2h0uu93QT9ZIBcMYw0IlCfrLsUMmYC8QYA6tps0BPXG57Y/j75DFOaSNSADQ X9Md7aLNcMVdg== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH] drm/amdkfd: add in-place userptr write access upgrade To: "Junhua Shen" , amd-gfx@lists.freedesktop.org In-Reply-To: <20261201112332.44784-1-Junhua.Shen@amd.com> References: <20261201112332.44784-1-Junhua.Shen@amd.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Tue, 01 Dec 2026 11:48:23 +0000 X-BeenThere: amd-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Discussion list for AMD gfx List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: sashiko-reviews@lists.linux.dev Sender: "amd-gfx" X-Spam-Status: No, score=-10.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 X-Archived-By: 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 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/g= pu/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 *a= dev, > + struct kgd_mem *mem) > +{ > + struct amdkfd_process_info *process_info =3D mem->process_info; > + struct amdgpu_bo *bo =3D mem->bo; > + struct ttm_operation_ctx ctx =3D { > + .interruptible =3D false, > + .no_wait_gpu =3D false, > + }; > + struct kfd_mem_attachment *entry; > + struct amdgpu_hmm_range *range; > + u64 user_addr =3D 0; > + bool unbound =3D false; > + int ret =3D 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 =3D 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 =3D amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr, 0); > + if (ret) > + goto out_unlock; > + > + range =3D amdgpu_hmm_range_alloc(NULL); > + if (unlikely(!range)) { > + ret =3D -ENOMEM; > + goto out_restore_ro; > + } > + > + ret =3D amdgpu_ttm_tt_get_user_pages(bo, range); > + if (ret) { > + if (ret =3D=3D -EAGAIN || ret =3D=3D -EPERM || ret =3D=3D -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 =3D 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 !=3D 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 =3D 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 =3D 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 =3D 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 =3D 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 =3D=3D NULL. In addition, amdgpu_ttm_tt_set_user_pages() has alrea= dy 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 |=3D KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE; > + list_for_each_entry(entry, &mem->attachments, list) > + entry->pte_flags =3D 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 =3D=3D= 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 |=3D 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/a= md/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 f= ile *filep, > return err; > } > =20 > +/* 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 =3D amdgpu_amdkfd_gpuvm_upgrade_userptr_writable(pdd->dev->adev, m= em); > + > + 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 =3D p->pdds[i]; > + map_err =3D amdgpu_amdkfd_gpuvm_map_memory_to_gpu(peer_pdd->dev->adev, > + mem, peer_pdd->drm_priv); > + if (map_err && !err) > + err =3D map_err; > + } > + > + if (!bitmap_empty(bitmap_unmapped, p->n_pdds)) { > + map_err =3D amdgpu_amdkfd_gpuvm_sync_memory(pdd->dev->adev, mem, fals= e); > + if (map_err && !err) > + err =3D 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261201112332.4478= 4-1-Junhua.Shen@amd.com?part=3D1