From: Huang Rui <ray.huang@amd.com>
To: "Christian König" <christian.koenig@amd.com>,
"Philip Yang" <Philip.Yang@amd.com>,
"Alex Deucher" <alexander.deucher@amd.com>,
"Felix Kuehling" <felix.kuehling@amd.com>,
"Simona Vetter" <simona@ffwll.ch>,
"Matthew Brost" <matthew.brost@intel.com>,
"Rodrigo Vivi" <rodrigo.vivi@intel.com>,
"Thomas Hellström" <thomas.hellstrom@linux.intel.com>,
"Danilo Krummrich" <dakr@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Aveline Noir" <jm5905938@gmail.com>,
amd-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org
Cc: Xiaogang Chen <xiaogang.chen@amd.com>,
Oak Zeng <Oak.Zeng@amd.com>, "Jenny Liu" <Jenny-Jing.Liu@amd.com>,
Zhu Lingshan <lingshan.zhu@amd.com>,
"Honglei Huang" <honglei1.huang@amd.com>,
Junhua Shen <Junhua.Shen@amd.com>, Yiru Ma <yiru.ma@amd.com>,
Huang Rui <ray.huang@amd.com>, Honglei Huang <honghuan@amd.com>,
Yu Huang <Yu.Huang@amd.com>
Subject: [PATCH v11 15/22] drm/amdgpu: use drm_gpusvm notifier lock for VM eviction
Date: Fri, 27 Nov 2026 16:41:38 +0800 [thread overview]
Message-ID: <20261127084149.925625-16-ray.huang@amd.com> (raw)
In-Reply-To: <20261127084149.925625-1-ray.huang@amd.com>
From: Honglei Huang <honghuan@amd.com>
Use the GPUSVM notifier semaphore as the eviction lock for an SVM
compute VM, so page-table updates, MMU invalidation and BO eviction
share one serialization mechanism.
Pass the caller-held lock to the VM update helpers. Keep range
validation, PTE/PDE updates and mapping-state publication under the
notifier lock; wait for update fences and perform the compute TLB
flush after releasing it. Publish the lock alias before the SVM
context, and restore the default lock before unpublishing the context
during teardown.
Suggested-by: Christian König <christian.koenig@amd.com>
Suggested-by: Huang Rui <ray.huang@amd.com>
Signed-off-by: Yu Huang <Yu.Huang@amd.com>
Signed-off-by: Honglei Huang <honghuan@amd.com>
---
drivers/gpu/drm/amd/amdgpu/amdgpu_svm.c | 37 ++++++++++++----
drivers/gpu/drm/amd/amdgpu/amdgpu_svm_range.c | 42 +++++++------------
2 files changed, 44 insertions(+), 35 deletions(-)
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_svm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_svm.c
index cf9c27cb0080f..8036ee189269f 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_svm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_svm.c
@@ -157,10 +157,13 @@ struct amdgpu_svm *amdgpu_svm_lookup_by_pasid(struct amdgpu_device *adev, u32 pa
amdgpu_pasid_lock(&irqflags);
fpriv = amdgpu_pasid_get_fpriv_locked(pasid);
vm = fpriv ? &fpriv->vm : NULL;
- if (vm && vm->svm) {
- svm = vm->svm;
+ /*
+ * eviction_lock changes when SVM is enabled, so acquire vm->svm
+ * after the new lock alias is published.
+ */
+ svm = vm ? smp_load_acquire(&vm->svm) : NULL;
+ if (svm)
kref_get(&svm->refcount);
- }
amdgpu_pasid_unlock(irqflags);
return svm;
@@ -465,6 +468,8 @@ static int amdgpu_svm_init_compute(struct amdgpu_device *adev, struct amdgpu_vm
struct amdgpu_svm *svm;
int ret;
+ dma_resv_assert_held(vm->root.bo->tbo.base.resv);
+
if (vm->svm)
return 0;
@@ -496,7 +501,9 @@ static int amdgpu_svm_init_compute(struct amdgpu_device *adev, struct amdgpu_vm
1UL << (svm->default_granularity + PAGE_SHIFT),
svm->xnack_enabled ? "enabled" : "disabled");
- vm->svm = svm;
+ vm->eviction_lock = &svm->gpusvm.notifier_lock;
+ /* Publish the context after its notifier lock alias. */
+ smp_store_release(&vm->svm, svm);
return 0;
err_free:
@@ -570,31 +577,43 @@ void amdgpu_svm_close(struct amdgpu_vm *vm)
* amdgpu_svm_fini() - Finalize and release a VM's SVM context
* @vm: The VM whose SVM context is being torn down.
*
- * Close the context, tear down the embedded drm_gpusvm under the SVM lock,
- * destroy the attribute tree and work queues, and drop the context
- * reference. Safe to call on a VM without an SVM context.
+ * Tear down the embedded drm_gpusvm, attribute tree and work queues, and drop
+ * the context reference.
*/
void amdgpu_svm_fini(struct amdgpu_vm *vm)
{
struct amdgpu_svm *svm = vm->svm;
+ struct amdgpu_bo *root;
if (!svm)
return;
amdgpu_svm_close(vm);
amdgpu_svm_lock(svm);
+
+ root = amdgpu_bo_ref(vm->root.bo);
+ amdgpu_bo_reserve(root, true);
+
drm_gpusvm_fini(&svm->gpusvm);
+
+ vm->eviction_lock = &vm->default_eviction_lock;
+ /* Publish the lock reset before making SVM unavailable. */
+ smp_store_release(&vm->svm, NULL);
+
amdgpu_svm_unlock(svm);
amdgpu_svm_attr_tree_destroy(svm->attr_tree);
amdgpu_svm_work_fini(svm);
- vm->svm = NULL;
amdgpu_svm_put(svm);
+
+ amdgpu_bo_unreserve(root);
+ amdgpu_bo_unref(&root);
}
bool amdgpu_svm_is_enabled(struct amdgpu_vm *vm)
{
- return vm->svm;
+ /* Pairs with publishing vm->svm after its lock alias is initialized. */
+ return smp_load_acquire(&vm->svm);
}
static int amdgpu_svm_copy_attrs(const struct drm_amdgpu_gem_svm *args,
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_svm_range.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_svm_range.c
index 6cc491cdc3ca5..adb9a0730ebf4 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_svm_range.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_svm_range.c
@@ -66,16 +66,13 @@ int amdgpu_svm_range_zap_ptes(struct amdgpu_svm *svm, struct amdgpu_svm_range *r
unsigned long start_page, unsigned long last_page)
{
struct dma_fence *fence = NULL;
- unsigned int flags;
int ret;
if (last_page < start_page)
return 0;
- flags = memalloc_noreclaim_save();
- ret = amdgpu_vm_unmap_range(svm->adev, svm->vm, true, NULL, start_page, last_page, NULL,
- &fence);
- memalloc_noreclaim_restore(flags);
+ ret = amdgpu_vm_unmap_range(svm->adev, svm->vm, true, NULL, start_page, last_page,
+ svm->vm->eviction_lock, &fence);
if (fence) {
dma_fence_wait(fence, false);
@@ -139,17 +136,9 @@ int amdgpu_svm_range_update_gpu_range(struct amdgpu_svm *svm, struct amdgpu_svm_
mapped_pages += seg_pages;
is_last_seg = mapped_pages == npages;
- /*
- * The struct hmm_range hmm_range field inside amdgpu_hmm_range
- * is drm_gpusvm internal state. It is not exposed to the
- * implementing driver and is managed entirely by the framework
- * as part of the framework logic. This amdgpu_vm_map_range call
- * always runs inside the drm_gpusvm notifier, so omitting
- * hmm_range here is safe.
- */
ret = amdgpu_vm_map_range(svm->adev, svm->vm, flush_tlb && is_last_seg, true, NULL,
start_page, last_page, pte_flags, 0, dst, NULL, NULL,
- NULL, NULL, NULL,
+ NULL, svm->vm->eviction_lock, base,
wait_fence && is_last_seg ? fence : NULL);
if (ret)
return ret;
@@ -231,7 +220,6 @@ int amdgpu_svm_range_update_mapping(struct amdgpu_svm *svm, struct amdgpu_svm_ra
struct drm_exec exec;
struct dma_fence *fence = NULL;
unsigned int exec_flags = DRM_EXEC_IGNORE_DUPLICATES;
- unsigned int flags;
int ret;
if (intr)
@@ -246,7 +234,6 @@ int amdgpu_svm_range_update_mapping(struct amdgpu_svm *svm, struct amdgpu_svm_ra
}
}
- flags = memalloc_noreclaim_save();
drm_gpusvm_notifier_lock(&svm->gpusvm);
if (!amdgpu_svm_range_pages_valid(svm, range)) {
@@ -257,24 +244,24 @@ int amdgpu_svm_range_update_mapping(struct amdgpu_svm *svm, struct amdgpu_svm_ra
wait, wait ? &fence : NULL);
}
- drm_gpusvm_notifier_unlock(&svm->gpusvm);
- memalloc_noreclaim_restore(flags);
-
- if (!ret && fence)
- dma_fence_wait(fence, intr);
- dma_fence_put(fence);
-
if (!ret)
- ret = amdgpu_vm_update_pdes(svm->adev, svm->vm, NULL);
+ ret = amdgpu_vm_update_pdes(svm->adev, svm->vm, svm->vm->eviction_lock);
if (!ret) {
- if (flush_tlb)
- ret = amdgpu_svm_flush_tlb(svm);
WRITE_ONCE(range->attr_flags, attrs->flags);
WRITE_ONCE(range->gpu_mapped, true);
range->validate_timestamp = ktime_get_boottime();
}
+ drm_gpusvm_notifier_unlock(&svm->gpusvm);
+
+ if (!ret && fence)
+ dma_fence_wait(fence, intr);
+ dma_fence_put(fence);
+
+ if (!ret && flush_tlb)
+ ret = amdgpu_svm_flush_tlb(svm);
+
drm_exec_fini(&exec);
return ret;
}
@@ -510,6 +497,8 @@ int amdgpu_svm_range_invalidate_interval(struct amdgpu_svm *svm, unsigned long s
"ATTR DESTROY" :
"ATTR ZAP PTE");
+ /* Use the unified eviction/MMU notifier lock for PTE zaps. */
+ down_write(svm->vm->eviction_lock);
ret = amdgpu_svm_range_zap_ptes(svm, svm_range,
drm_gpusvm_range_start(range) >>
PAGE_SHIFT,
@@ -518,6 +507,7 @@ int amdgpu_svm_range_invalidate_interval(struct amdgpu_svm *svm, unsigned long s
1);
if (!ret)
ret = amdgpu_svm_flush_tlb(svm);
+ up_write(svm->vm->eviction_lock);
if (ret < 0) {
drm_exec_fini(&exec);
return ret;
--
2.53.0
next prev parent reply other threads:[~2026-11-27 8:43 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 8:41 [PATCH v11 00/22] drm/amdgpu: AMDGPU SVM support based on DRM (Phase 1: single GPU, XNACK on) Huang Rui
2026-11-27 8:41 ` [PATCH v11 01/22] drm/amdgpu: decouple PTE encoding from buffer objects Huang Rui
2026-11-27 8:41 ` [PATCH v11 02/22] drm/amdgpu: add SVM ioctl UAPI definitions Huang Rui
2026-11-27 8:41 ` [PATCH v11 03/22] drm/amdgpu: add SVM core header and VM integration Huang Rui
2026-11-27 8:41 ` [PATCH v11 04/22] drm/amdgpu: implement SVM attribute tree and helper functions Huang Rui
2026-11-27 8:41 ` [PATCH v11 05/22] drm/amdgpu: implement SVM attribute set/get/clear operations Huang Rui
2026-11-27 8:41 ` [PATCH v11 06/22] drm/amdgpu: add SVM range types and work queue interface Huang Rui
2026-11-27 8:41 ` [PATCH v11 07/22] drm/amdgpu: implement SVM range GPU mapping core Huang Rui
2026-11-27 8:41 ` [PATCH v11 08/22] drm/amdgpu: implement SVM range notifier and GC helpers Huang Rui
2026-11-27 8:41 ` [PATCH v11 09/22] drm/amdgpu: add SVM notifier invalidate callback Huang Rui
2026-11-27 8:41 ` [PATCH v11 10/22] drm/amdgpu: implement SVM initialization and lifecycle Huang Rui
2026-11-27 8:41 ` [PATCH v11 11/22] drm/amdgpu: add SVM ioctl entry and fault handler module Huang Rui
2026-11-27 8:41 ` [PATCH v11 12/22] drm/amdgpu: integrate SVM into build system and VM fault path Huang Rui
2026-11-27 8:41 ` [PATCH v11 13/22] drm/amdgpu: make VM eviction lock read/write to match notifier lock Huang Rui
2026-11-27 8:41 ` [PATCH v11 14/22] drm/amdgpu: recheck range validity after page table allocation Huang Rui
2026-11-27 8:41 ` Huang Rui [this message]
2026-11-27 8:41 ` [PATCH v11 16/22] drm/pagemap: fix page undercount for compound src entries Huang Rui
2026-11-27 8:41 ` [PATCH v11 17/22] drm/amdgpu: add VRAM migration infrastructure for drm_pagemap Huang Rui
2026-11-27 8:41 ` [PATCH v11 18/22] drm/amdgpu: implement drm_pagemap SDMA migration callbacks Huang Rui
2026-11-27 8:41 ` [PATCH v11 19/22] drm/amdgpu: implement synchronous TTM eviction for SVM BOs Huang Rui
2026-11-27 8:41 ` [PATCH v11 20/22] drm/amdgpu: hook up ZONE_DEVICE registration in device init and reset Huang Rui
2026-11-27 8:41 ` [PATCH v11 21/22] drm/amdgpu: add SVM range migration helpers for drm_pagemap Huang Rui
2026-11-27 8:41 ` [PATCH v11 22/22] drm/amdgpu: integrate VRAM migration into SVM fault and prefetch paths Huang Rui
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261127084149.925625-16-ray.huang@amd.com \
--to=ray.huang@amd.com \
--cc=Jenny-Jing.Liu@amd.com \
--cc=Junhua.Shen@amd.com \
--cc=Oak.Zeng@amd.com \
--cc=Philip.Yang@amd.com \
--cc=Yu.Huang@amd.com \
--cc=alexander.deucher@amd.com \
--cc=aliceryhl@google.com \
--cc=amd-gfx@lists.freedesktop.org \
--cc=christian.koenig@amd.com \
--cc=dakr@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=felix.kuehling@amd.com \
--cc=honghuan@amd.com \
--cc=honglei1.huang@amd.com \
--cc=jm5905938@gmail.com \
--cc=lingshan.zhu@amd.com \
--cc=matthew.brost@intel.com \
--cc=rodrigo.vivi@intel.com \
--cc=simona@ffwll.ch \
--cc=thomas.hellstrom@linux.intel.com \
--cc=xiaogang.chen@amd.com \
--cc=yiru.ma@amd.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.