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 80BE2606A33A for ; Tue, 1 Dec 2026 20:20: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=IumTmi30 Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 1E78310E0BA; Tue, 1 Dec 2026 20:20: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 6888A10E0BA for ; Tue, 1 Dec 2026 20:20:23 +0000 (UTC) Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id F0C264027B; Tue, 1 Dec 2026 20:20:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A39881F00898; Tue, 1 Dec 2026 20:20:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796156422; bh=xCleEzw7gL//+kLY66eve4DUvGCwQji+AoHMliRU0Os=; h=From:Subject:Reply-To:To:In-Reply-To:References:Date; b=IumTmi30o/BjPQprABZdmgi2wcPbBX0OL+mtcw0uHUkW/C/7LK4OytQCLi8JCUlkx 0l98SM+m15/wjAo3d5ORrNvTmmhJZMaFG/a4BggFfSb0OXhK+teCVXAG6nWPRIQIrR prfZXvwl9Zub0ShVOr04HpnD5SxUmNyRmUjmehY+JQqPjY3oAX4tfELiCA6StrtcEw JiGlrjuBdQVB9aYdv+MPMTdTl1nlf7OLu5+yoqawFMgm7FDeV4mjPZyyk10g2awTxR uM7nLbHwjlQvIYKykbqsdApgUxaBYcGjNPGd9+PnIl1xOgwhHjkQec5Oo8/aJ4bNj6 2FU/V/iiqn+zg== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH] drm/amdkfd: fix SVM GPU unmap bookkeeping on failed PTE invalidation To: "Eric Huang" , amd-gfx@lists.freedesktop.org In-Reply-To: <20261201200136.46039-1-jinhuieric.huang@amd.com> References: <20261201200136.46039-1-jinhuieric.huang@amd.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Tue, 01 Dec 2026 20:20:22 +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=-15.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,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: permanent queue hang and range free in svm_range_unmap_from_cpu() - [High] drm/amdkfd: -EBUSY masked in svm_range_unmap_from_gpu() during VM eviction - [High] drm/amdkfd: failed NO_ACCESS unmap re-maps GPU in svm_range_needs_unmap() - [Medium] drm/amdkfd: false-positive warning on -ESRCH in svm_range_unmap_from_cpu() - [Low] drm/amdkfd: missing Fixes: trailer for svm_range_unmap_from_gpus() bug fix It also noticed 1 related pre-existing issue this patch does not introduce: - [Critical] drm/amdkfd: NULL pointer dereference in svm_range_unmap_split() [1] -- commit a2c6053865ef6922ea03ebb16442de39f7070c6a Author: Eric Huang > drm/amdkfd: fix SVM GPU unmap bookkeeping on failed PTE invalidation > > svm_range_unmap_from_gpus() cleared bitmap_mapped for a GPU before > checking whether svm_range_unmap_from_gpu() actually succeeded. When > the GPU unmap failed (e.g. amdgpu_vm_ready() false because the VM was > being evicted), the bit was cleared anyway, so every later consumer of > bitmap_mapped (TLB flush decisions, svm_range_needs_unmap(), restore > and validate/map logic) believed this GPU had no mapping left, even > though its page table entries were still valid. The CPU pages behind > that SVM range can then be freed or reused while the GPU retains a > valid translation to them. > > The failure was also invisible: svm_range_unmap_from_cpu(), called > from the MMU_NOTIFY_UNMAP path of the mmu_interval_notifier callback, > is void and discarded the int returned by svm_range_unmap_from_gpus(). > > Fix this by: > - Only clearing bitmap_mapped after svm_range_unmap_from_gpu() > succeeds, so the bitmap keeps reflecting an unmap that didn't > actually happen. > - Making svm_range_unmap_from_cpu() return the first failure from its > svm_range_unmap_from_gpus() calls, and quiescing the process's > queues on failure (the same fallback already used a few lines above > for the "vital queue buffer" case), so GPU work can't run against > the known-stale mapping before the next validate/restore cycle > retries the unmap. > - Logging the failure from the notifier callback instead of silently > dropping it. > > Signed-off-by: Eric Huang [Severity: Low] This fixes a bug in how prange->bitmap_mapped is cleared on unmap. Should it include a Fixes: tag for the commit that introduced bitmap_mapped? Fixes: b96f26104946 ("drm/amdkfd: Unmap svm range from GPU set to no-acce= ss") > diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c b/drivers/gpu/drm/amd/a= mdkfd/kfd_svm.c > index a571fda5c713a..ed2a409a9f2c8 100644 > --- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c > +++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c > @@ -1460,12 +1460,6 @@ svm_range_unmap_from_gpus(struct svm_range *prange= , unsigned long start, > p =3D container_of(prange->svms, struct kfd_process, svms); > =20 > for_each_set_bit(gpuidx, bitmap_unmap, MAX_GPU_INSTANCE) { > - if (prange->start =3D=3D start && prange->last =3D=3D last) { > - pr_debug("unmap svms 0x%p prange 0x%p from gpu_idx 0x%x\n", > - prange->svms, prange, gpuidx); > - clear_bit(gpuidx, prange->bitmap_mapped); > - } > - > pdd =3D kfd_process_device_from_gpuidx(p, gpuidx); > if (!pdd) { > pr_debug("failed to find device idx %d\n", gpuidx); > @@ -1481,6 +1475,17 @@ svm_range_unmap_from_gpus(struct svm_range *prange= , unsigned long start, > if (r) > break; > =20 > + /* Only clear bitmap_mapped once the GPU unmap actually > + * succeeded. If svm_range_unmap_from_gpu() above failed > + * (e.g. VM not ready), the PTEs are still valid, so this > + * GPU must still be treated as mapped for this range. > + */ [Severity: High] Does svm_range_unmap_from_gpu() actually return an error when the VM is being evicted? drivers/gpu/drm/amd/amdkfd/kfd_svm.c:svm_range_unmap_from_gpu() { ... ret =3D amdgpu_vm_update_range(adev, vm, false, true, true, false, NULL, g= pu_start, gpu_end, init_pte_value, 0, 0, NULL, NULL, fence); /* EBUSY here means "vm not ready for updates because it in the process of= evicting." * If that is true, it's safe to not update the range now since the vm is = not in use. */ return ret =3D=3D -EBUSY ? 0 : ret; } When vm->evicting is true, amdgpu_vm_update_range() aborts with -EBUSY without invalidating the GPU PTEs, and svm_range_unmap_from_gpu() converts that -EBUSY into 0. As a result, svm_range_unmap_from_gpus() still sees r =3D=3D 0 during VM eviction, clears gpuidx in prange->bitmap_mapped, and returns 0 while the evicted page table BOs retain valid PTEs. Also, clear_bit(gpuidx, prange->bitmap_mapped) still runs before checking whether dma_fence_wait(fence, false) succeeded, and breaking out of the loop on the first error skips unmapping any remaining GPUs in bitmap_unmap. > + if (prange->start =3D=3D start && prange->last =3D=3D last) { > + pr_debug("unmap svms 0x%p prange 0x%p from gpu_idx 0x%x\n", > + prange->svms, prange, gpuidx); > + clear_bit(gpuidx, prange->bitmap_mapped); > + } [Severity: High] Can leaving gpuidx set in prange->bitmap_mapped on failure cause svm_range_set_attr() to re-map a GPU after KFD_IOCTL_SVM_ATTR_NO_ACCESS? When userspace sets KFD_IOCTL_SVM_ATTR_NO_ACCESS, svm_range_set_attr() calls svm_range_needs_unmap(), which runs: drivers/gpu/drm/amd/amdkfd/kfd_svm.c:svm_range_needs_unmap() { ... svm_range_unmap_from_gpus(prange, prange->start, prange->last, prange->bitmap_needs_unmap, KFD_SVM_UNMAP_TRIGGER_UNMAP_FROM_CPU); bitmap_clear(prange->bitmap_needs_unmap, 0, MAX_GPU_INSTANCE); return bitmap_empty(prange->bitmap_mapped, MAX_GPU_INSTANCE); } If svm_range_unmap_from_gpus() fails, prange->bitmap_needs_unmap is still wiped and svm_range_needs_unmap() returns false because gpuidx remains set in prange->bitmap_mapped. svm_range_set_attr() then falls through to svm_range_validate_and_map(), which under xnack_enabled computes bitmap_or(ctx->bitmap, prange->bitmap_mapped, prange->bitmap_aip, MAX_GPU_INSTANCE) and re-maps the range back onto the GPU whose access was just revoked. Similarly, in svm_range_evict() when xnack_enabled is true, the return value of svm_range_unmap_from_gpus() is ignored, so a failed unmap there neither quiesces the queues nor schedules svms->restore_work. > =20 > if (fence) { > r =3D dma_fence_wait(fence, false); > dma_fence_put(fence); > @@ -2633,7 +2638,7 @@ svm_range_unmap_split(struct svm_range *parent, str= uct svm_range *prange, unsign > } > } > =20 > -static void > +static int > svm_range_unmap_from_cpu(struct mm_struct *mm, struct svm_range *prange, > unsigned long start, unsigned long last) > { > @@ -2643,20 +2648,21 @@ svm_range_unmap_from_cpu(struct mm_struct *mm, st= ruct svm_range *prange, > struct kfd_process *p; > unsigned long s, l; > bool unmap_parent; > + int r =3D 0, ret; > =20 > if (atomic_read(&prange->queue_refcount)) { > - int r; > + int qr; > =20 > pr_warn("Freeing queue vital buffer 0x%lx, queue evicted\n", > prange->start << PAGE_SHIFT); > - r =3D kgd2kfd_quiesce_mm(mm, KFD_QUEUE_EVICTION_TRIGGER_SVM); > - if (r) > - pr_debug("failed %d to quiesce KFD queues\n", r); > + qr =3D kgd2kfd_quiesce_mm(mm, KFD_QUEUE_EVICTION_TRIGGER_SVM); > + if (qr) > + pr_debug("failed %d to quiesce KFD queues\n", qr); > } > =20 > p =3D kfd_lookup_process_by_mm(mm); > if (!p) > - return; > + return -ESRCH; [Severity: Medium] Won't returning -ESRCH here cause svm_range_cpu_invalidate_pagetables() to log a false-positive pr_warn() for every SVM range during normal process exit? In exit_mmap(), mmu_notifier_release() runs before unmap_vmas(). kfd_process_notifier_release() removes the process from kfd_processes_table (after stopping all user queues), while each prange->notifier stays registered until kfd_process_wq_release() runs. When unmap_vmas() then fires MMU_NOTIFY_UNMAP, kfd_lookup_process_by_mm() returns NULL and svm_range_cpu_invalidate_pagetables() will warn that SVM failed to unmap the range from the GPU. > svms =3D &p->svms; > =20 > pr_debug("svms 0x%p prange 0x%p [0x%lx 0x%lx] [0x%lx 0x%lx]\n", svms, > @@ -2670,17 +2676,37 @@ svm_range_unmap_from_cpu(struct mm_struct *mm, st= ruct svm_range *prange, > mutex_lock_nested(&pchild->lock, 1); > s =3D max(start, pchild->start); > l =3D min(last, pchild->last); > - if (l >=3D s) > - svm_range_unmap_from_gpus(pchild, s, l, prange->bitmap_mapped, trigge= r); > + if (l >=3D s) { > + ret =3D svm_range_unmap_from_gpus(pchild, s, l, prange->bitmap_mapped= , trigger); > + if (ret) > + r =3D ret; > + } > svm_range_unmap_split(prange, pchild, start, last); > mutex_unlock(&pchild->lock); > } > s =3D max(start, prange->start); > l =3D min(last, prange->last); > - if (l >=3D s) > - svm_range_unmap_from_gpus(prange, s, l, prange->bitmap_mapped, trigger= ); > + if (l >=3D s) { > + ret =3D svm_range_unmap_from_gpus(prange, s, l, prange->bitmap_mapped,= trigger); > + if (ret) > + r =3D ret; > + } > svm_range_unmap_split(prange, prange, start, last); [Severity: Critical] This problem wasn't introduced by this patch, but can svm_range_unmap_split() cause a NULL pointer dereference [1] if svm_range_split() fails with -ENOMEM? drivers/gpu/drm/amd/amdkfd/kfd_svm.c:svm_range_unmap_split() { ... head =3D tail =3D prange; if (start > prange->start) svm_range_split(prange, prange->start, start - 1, &tail); if (last < tail->last) svm_range_split(tail, last + 1, tail->last, &head); if (head !=3D prange && tail !=3D prange) { svm_range_add_child(parent, head, SVM_OP_UNMAP_RANGE); ... } When svm_range_new() fails inside svm_range_split(), *new is set to NULL and -ENOMEM is returned. Because svm_range_unmap_split() ignores the return value, a failure in the first split dereferences a NULL tail in last < tail->last, and a failure in the second split passes a NULL head to svm_range_add_child(). > + > + if (r) { > + /* GPU PTEs for this range could not be invalidated. Quiesce > + * the process's queues so GPU work can't run against the > + * stale mapping until the next validate/restore cycle > + * retries the unmap (bitmap_mapped is left set for any GPU > + * that failed above). > + */ > + pr_debug("failed %d to unmap svms 0x%p prange 0x%p [0x%lx 0x%lx] from = gpu, quiescing queues\n", > + r, svms, prange, start, last); > + ret =3D kgd2kfd_quiesce_mm(mm, KFD_QUEUE_EVICTION_TRIGGER_SVM); > + if (ret) > + pr_debug("failed %d to quiesce KFD queues\n", ret); > + } > + > if (unmap_parent) > svm_range_add_list_work(svms, prange, mm, SVM_OP_UNMAP_RANGE); > else > svm_range_add_list_work(svms, prange, mm, > SVM_OP_UPDATE_RANGE_NOTIFIER); > schedule_deferred_list_work(svms); [Severity: High] Doesn't calling kgd2kfd_quiesce_mm() here without a matching restore leave the process's queues permanently evicted, while still freeing prange on the deferred work list? kgd2kfd_quiesce_mm() increments the per-device qpd->evicted nesting counter in evict_process_queues_cpsch() and evict_process_queues_nocpsch(), and restore_process_queues_cpsch() only reactivates queues when qpd->evicted reaches 1. Unlike svm_range_evict(), which only quiesces on the atomic_inc_return(&svms->evicted_ranges) =3D=3D 1 transition and schedu= les svms->restore_work to call kgd2kfd_resume_mm(), this path never schedules a matching resume (and if prange->queue_refcount is non-zero, it calls kgd2kfd_quiesce_mm() twice in one invocation), so qpd->evicted is leaked and the process's queues can never resume. In addition, queuing SVM_OP_UNMAP_RANGE (or split children marked SVM_OP_UNMAP_RANGE) causes svm_range_handle_list_op() to unconditionally unlink the range, remove its notifier, and call svm_range_free(prange, true) to DMA-unmap and free prange, so no later validate/restore cycle can retry the GPU PTE unmap. [1] https://sashiko.dev/#/bug/linux-48eaf61c-de0f-4294-b7e6-728f8c66c802 --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261201200136.4603= 9-1-jinhuieric.huang@amd.com?part=3D1