RE: [PATCH V1 1/2] drm/gpusvm: add drm_gpusvm_pages unmapped and retry helpers

From: Huang, Honglei1

Date: Mon Nov 09 2026 - 03:57:01 EST


AMD General

Reviewed-by: Honglei Huang <honghuan@xxxxxxx>

-----Original Message-----
From: Lizhi Hou <lizhi.hou@xxxxxxx>
Sent: Saturday, November 7, 2026 2:42 AM
To: ogabbay@xxxxxxxxxx; quic_jhugo@xxxxxxxxxxx; dri-devel@xxxxxxxxxxxxxxxxxxxxx; Limonciello, Mario <Mario.Limonciello@xxxxxxx>; karol.wachowski@xxxxxxxxxxxxxxx; Zhen, Max <Max.Zhen@xxxxxxx>; thomas.hellstrom@xxxxxxxxxxxxxxx; himal.prasad.ghimiray@xxxxxxxxx; Huang, Honglei1 <Honglei1.Huang@xxxxxxx>
Cc: Matthew Brost <matthew.brost@xxxxxxxxx>; linux-kernel@xxxxxxxxxxxxxxx; Santan, Sonal <sonal.santan@xxxxxxx>; Hou, Lizhi <lizhi.hou@xxxxxxx>
Subject: [PATCH V1 1/2] drm/gpusvm: add drm_gpusvm_pages unmapped and retry helpers

From: Matthew Brost <matthew.brost@xxxxxxxxx>

Drivers which only use the drm_gpusvm_pages API (no drm_gpusvm_range) have no way to mark pages unmapped from their MMU notifier, as
drm_gpusvm_range_set_unmapped() requires a range. They also have to open code the mmu_interval_read_retry() check on the recorded notifier_seq to commit pages faulted with &drm_gpusvm_ctx.no_dma_map.

Split the pages part of drm_gpusvm_range_set_unmapped() into drm_gpusvm_pages_set_unmapped(), and add drm_gpusvm_pages_read_retry() which performs the commit side check with a lockdep assert on the notifier lock.

Assisted-by: LLM
Signed-off-by: Matthew Brost <matthew.brost@xxxxxxxxx>
Signed-off-by: Lizhi Hou <lizhi.hou@xxxxxxx>
---
drivers/gpu/drm/drm_gpusvm.c | 49 +++++++++++++++++++++++++-----------
include/drm/drm_gpusvm.h | 29 +++++++++++++++++++++
2 files changed, 64 insertions(+), 14 deletions(-)

diff --git a/drivers/gpu/drm/drm_gpusvm.c b/drivers/gpu/drm/drm_gpusvm.c index 03dbe304e4b8f..63a26919f59db 100644
--- a/drivers/gpu/drm/drm_gpusvm.c
+++ b/drivers/gpu/drm/drm_gpusvm.c
@@ -1761,9 +1761,8 @@ static int drm_gpusvm_dma_map_pages(struct drm_gpusvm *gpusvm,
*
* With &drm_gpusvm_ctx.no_dma_map no mapping state is recorded, so
* drm_gpusvm_pages_valid() never returns true and success is only a snapshot:
- * the caller must recheck mmu_interval_read_retry() against the recorded
- * &drm_gpusvm_pages.notifier_seq under the notifier lock, and hold it until
- * its work is visible to invalidation.
+ * the caller must recheck drm_gpusvm_pages_read_retry() under the
+ notifier
+ * lock, and hold it until its work is visible to invalidation.
*
* Return: 0 on success, negative error code on failure.
*/
@@ -2021,6 +2020,38 @@ bool drm_gpusvm_has_mapping(struct drm_gpusvm *gpusvm, unsigned long start, } EXPORT_SYMBOL_GPL(drm_gpusvm_has_mapping);

+/**
+ * drm_gpusvm_pages_set_unmapped() - Mark GPU SVM pages as unmapped
+ * @gpusvm: Pointer to the GPU SVM structure.
+ * @pages: Pointer to the GPU SVM pages structure(s).
+ * @pages_count: Number of GPU SVM pages structure(s) passed in.
+ *
+ * Mark @pages as unmapped, after which drm_gpusvm_get_pages() fails
+with
+ * -EFAULT for them. Intended to be called from the driver's MMU
+notifier on
+ * MMU_NOTIFY_UNMAP, and usable without a &drm_gpusvm_range by drivers
+that
+ * only use the drm_gpusvm_pages API. The caller must hold the notifier
+lock
+ * in write mode.
+ */
+void drm_gpusvm_pages_set_unmapped(struct drm_gpusvm *gpusvm,
+ struct drm_gpusvm_pages *pages,
+ unsigned int pages_count)
+{
+ unsigned int i;
+
+ lockdep_assert_held_write(&gpusvm->notifier_lock);
+
+ for (i = 0; i < pages_count; ++i) {
+ struct drm_gpusvm_pages_flags flags = {
+ .__flags = pages[i].flags.__flags,
+ };
+
+ flags.unmapped = true;
+ /* WRITE_ONCE pairs with READ_ONCE for opportunistic checks */
+ WRITE_ONCE(pages[i].flags.__flags, flags.__flags);
+ }
+}
+EXPORT_SYMBOL_GPL(drm_gpusvm_pages_set_unmapped);
+
/**
* drm_gpusvm_range_set_unmapped() - Mark a GPU SVM range as unmapped
* @range: Pointer to the GPU SVM range structure.
@@ -2039,20 +2070,10 @@ void drm_gpusvm_range_set_unmapped(struct drm_gpusvm_range *range,
struct drm_gpusvm_range_flags range_flags = {
.__flags = range->flags.__flags,
};
- unsigned int i;

- lockdep_assert_held_write(&range->gpusvm->notifier_lock);
+ drm_gpusvm_pages_set_unmapped(range->gpusvm, pages, pages_count);

range_flags.unmapped = true;
- for (i = 0; i < pages_count; ++i) {
- struct drm_gpusvm_pages_flags flags = {
- .__flags = pages[i].flags.__flags,
- };
-
- flags.unmapped = true;
- /* WRITE_ONCE pairs with READ_ONCE for opportunistic checks */
- WRITE_ONCE(pages[i].flags.__flags, flags.__flags);
- }
if (drm_gpusvm_range_start(range) < mmu_range->start ||
drm_gpusvm_range_end(range) > mmu_range->end)
range_flags.partial_unmap = true;
diff --git a/include/drm/drm_gpusvm.h b/include/drm/drm_gpusvm.h index 4d31c6bba7455..b43432af56377 100644
--- a/include/drm/drm_gpusvm.h
+++ b/include/drm/drm_gpusvm.h
@@ -349,6 +349,10 @@ void drm_gpusvm_range_set_unmapped(struct drm_gpusvm_range *range,
unsigned int pages_count,
const struct mmu_notifier_range *mmu_range);

+void drm_gpusvm_pages_set_unmapped(struct drm_gpusvm *gpusvm,
+ struct drm_gpusvm_pages *pages,
+ unsigned int pages_count);
+
int drm_gpusvm_get_pages(struct drm_gpusvm *gpusvm,
struct drm_gpusvm_pages *svm_pages,
unsigned int num_pages,
@@ -383,6 +387,31 @@ static inline void drm_gpusvm_init_pages(struct drm_gpusvm_pages *svm_pages,
svm_pages->notifier_seq = LONG_MAX;
}

+/**
+ * drm_gpusvm_pages_read_retry() - Check whether GPU SVM pages went
+stale
+ * @gpusvm: Pointer to the GPU SVM structure.
+ * @svm_pages: Pointer to the drm_gpusvm_pages.
+ * @notifier: The notifier passed to drm_gpusvm_get_pages() for @svm_pages.
+ *
+ * Check whether @notifier was invalidated since the last successful
+ * drm_gpusvm_get_pages() for @svm_pages. This is the commit side check
+for
+ * pages faulted with &drm_gpusvm_ctx.no_dma_map, where no mapping
+state is
+ * tracked. Pages that were never faulted always report as stale. The
+caller
+ * must hold the notifier lock and keep holding it until its use of the
+pages
+ * is visible to the invalidation path.
+ *
+ * Return: True if the pages must be faulted again, false otherwise.
+ */
+static inline bool
+drm_gpusvm_pages_read_retry(struct drm_gpusvm *gpusvm,
+ struct drm_gpusvm_pages *svm_pages,
+ struct mmu_interval_notifier *notifier) {
+ lockdep_assert_held(&gpusvm->notifier_lock);
+
+ return mmu_interval_read_retry(notifier, svm_pages->notifier_seq); }
+
/**
* drm_gpusvm_pages_first_dma() - Resolve the device address array
* @svm_pages: Pointer to the drm_gpusvm_pages.

base-commit: d2a8cee91bfa8616f713c7c8f539a81e8e3554ea
--
2.34.1