* [PATCH] drm/amdkfd: fix SVM GPU unmap bookkeeping on failed PTE invalidation
@ 2026-12-01 20:01 Eric Huang
2026-12-01 20:20 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Eric Huang @ 2026-12-01 20:01 UTC (permalink / raw)
To: amd-gfx; +Cc: Philip.Yang, Eric Huang
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 <jinhuieric.huang@amd.com>
---
drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 63 ++++++++++++++++++++--------
1 file changed, 46 insertions(+), 17 deletions(-)
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
index 32f2ca5b1fdfd..62e54153acab2 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
@@ -1477,12 +1477,6 @@ svm_range_unmap_from_gpus(struct svm_range *prange, unsigned long start,
p = container_of(prange->svms, struct kfd_process, svms);
for_each_set_bit(gpuidx, bitmap_unmap, MAX_GPU_INSTANCE) {
- if (prange->start == start && prange->last == 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 = kfd_process_device_from_gpuidx(p, gpuidx);
if (!pdd) {
pr_debug("failed to find device idx %d\n", gpuidx);
@@ -1498,6 +1492,17 @@ svm_range_unmap_from_gpus(struct svm_range *prange, unsigned long start,
if (r)
break;
+ /* 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.
+ */
+ if (prange->start == start && prange->last == 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);
+ }
+
if (fence) {
r = dma_fence_wait(fence, false);
dma_fence_put(fence);
@@ -2660,7 +2665,7 @@ svm_range_unmap_split(struct svm_range *parent, struct svm_range *prange, unsign
}
}
-static void
+static int
svm_range_unmap_from_cpu(struct mm_struct *mm, struct svm_range *prange,
unsigned long start, unsigned long last)
{
@@ -2670,20 +2675,21 @@ svm_range_unmap_from_cpu(struct mm_struct *mm, struct svm_range *prange,
struct kfd_process *p;
unsigned long s, l;
bool unmap_parent;
+ int r = 0, ret;
if (atomic_read(&prange->queue_refcount)) {
- int r;
+ int qr;
pr_warn("Freeing queue vital buffer 0x%lx, queue evicted\n",
prange->start << PAGE_SHIFT);
- r = kgd2kfd_quiesce_mm(mm, KFD_QUEUE_EVICTION_TRIGGER_SVM);
- if (r)
- pr_debug("failed %d to quiesce KFD queues\n", r);
+ qr = kgd2kfd_quiesce_mm(mm, KFD_QUEUE_EVICTION_TRIGGER_SVM);
+ if (qr)
+ pr_debug("failed %d to quiesce KFD queues\n", qr);
}
p = kfd_lookup_process_by_mm(mm);
if (!p)
- return;
+ return -ESRCH;
svms = &p->svms;
pr_debug("svms 0x%p prange 0x%p [0x%lx 0x%lx] [0x%lx 0x%lx]\n", svms,
@@ -2697,17 +2703,37 @@ svm_range_unmap_from_cpu(struct mm_struct *mm, struct svm_range *prange,
mutex_lock_nested(&pchild->lock, 1);
s = max(start, pchild->start);
l = min(last, pchild->last);
- if (l >= s)
- svm_range_unmap_from_gpus(pchild, s, l, prange->bitmap_mapped, trigger);
+ if (l >= s) {
+ ret = svm_range_unmap_from_gpus(pchild, s, l, prange->bitmap_mapped, trigger);
+ if (ret)
+ r = ret;
+ }
svm_range_unmap_split(prange, pchild, start, last);
mutex_unlock(&pchild->lock);
}
s = max(start, prange->start);
l = min(last, prange->last);
- if (l >= s)
- svm_range_unmap_from_gpus(prange, s, l, prange->bitmap_mapped, trigger);
+ if (l >= s) {
+ ret = svm_range_unmap_from_gpus(prange, s, l, prange->bitmap_mapped, trigger);
+ if (ret)
+ r = ret;
+ }
svm_range_unmap_split(prange, prange, start, last);
+ 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 = 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
@@ -2716,6 +2742,7 @@ svm_range_unmap_from_cpu(struct mm_struct *mm, struct svm_range *prange,
schedule_deferred_list_work(svms);
kfd_unref_process(p);
+ return r;
}
/**
@@ -2767,7 +2794,9 @@ svm_range_cpu_invalidate_pagetables(struct mmu_interval_notifier *mni,
switch (range->event) {
case MMU_NOTIFY_UNMAP:
- svm_range_unmap_from_cpu(mni->mm, prange, start, last);
+ if (svm_range_unmap_from_cpu(mni->mm, prange, start, last))
+ pr_warn("SVM failed to fully unmap svms 0x%p prange 0x%p [0x%lx 0x%lx] from GPU\n",
+ prange->svms, prange, start, last);
break;
default:
svm_range_evict(prange, mni->mm, start, last, range->event);
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] drm/amdkfd: fix SVM GPU unmap bookkeeping on failed PTE invalidation
2026-12-01 20:01 [PATCH] drm/amdkfd: fix SVM GPU unmap bookkeeping on failed PTE invalidation Eric Huang
@ 2026-12-01 20:20 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-01 20:20 UTC (permalink / raw)
To: Eric Huang, amd-gfx
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 <jinhuieric.huang@amd.com>
> 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 <jinhuieric.huang@amd.com>
[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-access")
> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c b/drivers/gpu/drm/amd/amdkfd/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 = container_of(prange->svms, struct kfd_process, svms);
>
> for_each_set_bit(gpuidx, bitmap_unmap, MAX_GPU_INSTANCE) {
> - if (prange->start == start && prange->last == 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 = 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;
>
> + /* 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 = amdgpu_vm_update_range(adev, vm, false, true, true, false, NULL, gpu_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 == -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 == 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 == start && prange->last == 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.
>
> if (fence) {
> r = dma_fence_wait(fence, false);
> dma_fence_put(fence);
> @@ -2633,7 +2638,7 @@ svm_range_unmap_split(struct svm_range *parent, struct svm_range *prange, unsign
> }
> }
>
> -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, struct svm_range *prange,
> struct kfd_process *p;
> unsigned long s, l;
> bool unmap_parent;
> + int r = 0, ret;
>
> if (atomic_read(&prange->queue_refcount)) {
> - int r;
> + int qr;
>
> pr_warn("Freeing queue vital buffer 0x%lx, queue evicted\n",
> prange->start << PAGE_SHIFT);
> - r = kgd2kfd_quiesce_mm(mm, KFD_QUEUE_EVICTION_TRIGGER_SVM);
> - if (r)
> - pr_debug("failed %d to quiesce KFD queues\n", r);
> + qr = kgd2kfd_quiesce_mm(mm, KFD_QUEUE_EVICTION_TRIGGER_SVM);
> + if (qr)
> + pr_debug("failed %d to quiesce KFD queues\n", qr);
> }
>
> p = 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 = &p->svms;
>
> 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, struct svm_range *prange,
> mutex_lock_nested(&pchild->lock, 1);
> s = max(start, pchild->start);
> l = min(last, pchild->last);
> - if (l >= s)
> - svm_range_unmap_from_gpus(pchild, s, l, prange->bitmap_mapped, trigger);
> + if (l >= s) {
> + ret = svm_range_unmap_from_gpus(pchild, s, l, prange->bitmap_mapped, trigger);
> + if (ret)
> + r = ret;
> + }
> svm_range_unmap_split(prange, pchild, start, last);
> mutex_unlock(&pchild->lock);
> }
> s = max(start, prange->start);
> l = min(last, prange->last);
> - if (l >= s)
> - svm_range_unmap_from_gpus(prange, s, l, prange->bitmap_mapped, trigger);
> + if (l >= s) {
> + ret = svm_range_unmap_from_gpus(prange, s, l, prange->bitmap_mapped, trigger);
> + if (ret)
> + r = 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 = tail = 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 != prange && tail != 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 = 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) == 1 transition and schedules
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
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201200136.46039-1-jinhuieric.huang@amd.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-01 20:20 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 20:01 [PATCH] drm/amdkfd: fix SVM GPU unmap bookkeeping on failed PTE invalidation Eric Huang
2026-12-01 20:20 ` sashiko-bot
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.