[PATCH v2 2/2] drm/gpusvm: Clear inline DMA mapping state on unmap

From: Arvind Yadav

Date: Fri Dec 04 2026 - 01:53:48 EST


Unmap clears has_dma_mapping but leaves the inline DMA address and
inline_dma_mapping flag intact.

When a devmem callback is present, get_pages() continues through HMM
even with a valid DMA mapping. Invalidation during HMM can leave stale
inline state while HMM refreshes notifier_seq. The later sequence
check can then pass, but DMA array allocation is skipped. The mapper
treats the stored DMA address as an array pointer.

BUG: KASAN: user-memory-access in drm_gpusvm_get_pages+0xe12/0x1380 [drm_gpusvm_helper]
Write of size 16 at addr 000000027abfd000 by task kworker/u64:71/693678
Workqueue: xe_prefetch_work_queue prefetch_work_func [xe]

Clear the inline address and flag during unmap so the allocation pass
after HMM can allocate a DMA array. Update the helper documentation
to describe the inline entry's lifetime.

v2:
- Clear inline DMA mapping state in __drm_gpusvm_unmap_pages()
instead of adding a retry in drm_gpusvm_get_pages() (Matt).
- Update the inline DMA helper's lifetime documentation. (Sashiko)

Fixes: 8d242e452ed7 ("drm/gpusvm: keep a single DMA mapping inline for THP")
Cc: Matthew Brost <matthew.brost@xxxxxxxxx>
Cc: Thomas Hellström <thomas.hellstrom@xxxxxxxxxxxxxxx>
Cc: Himal Prasad Ghimiray <himal.prasad.ghimiray@xxxxxxxxx>
Suggested-by: Matthew Brost <matthew.brost@xxxxxxxxx>
Signed-off-by: Arvind Yadav <arvind.yadav@xxxxxxxxx>
---
drivers/gpu/drm/drm_gpusvm.c | 5 +++++
include/drm/drm_gpusvm.h | 14 ++++++++------
2 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/drivers/gpu/drm/drm_gpusvm.c b/drivers/gpu/drm/drm_gpusvm.c
index de2fa2684e84..1a61b13384a0 100644
--- a/drivers/gpu/drm/drm_gpusvm.c
+++ b/drivers/gpu/drm/drm_gpusvm.c
@@ -1286,6 +1286,11 @@ static void __drm_gpusvm_unmap_pages(struct drm_gpusvm *gpusvm,
i += 1 << addr->order;
}

+ if (flags.inline_dma_mapping) {
+ svm_pages->inline_addr = (struct drm_pagemap_addr){};
+ flags.inline_dma_mapping = false;
+ }
+
/* WRITE_ONCE pairs with READ_ONCE for opportunistic checks */
flags.has_devmem_pages = false;
flags.has_dma_mapping = false;
diff --git a/include/drm/drm_gpusvm.h b/include/drm/drm_gpusvm.h
index b43432af5637..9d55c34ffbb8 100644
--- a/include/drm/drm_gpusvm.h
+++ b/include/drm/drm_gpusvm.h
@@ -428,12 +428,14 @@ drm_gpusvm_pages_read_retry(struct drm_gpusvm *gpusvm,
* from the order. The order still states what one PTE may cover: the range
* length for a huge page, PAGE_SIZE for an IOVA mapped range of single pages.
*
- * Only get_pages() and the free path switch between the two union members.
- * Both hold the notifier lock for read, so taking that lock does not stop
- * them; callers need the driver lock that does, which every reader of the
- * addresses holds anyway. The notifier never touches the union, so the
- * pointer returned here stays good and can then be used under the notifier
- * lock.
+ * Callers must hold the driver lock that serializes get_pages() and the
+ * free path. Invalidation can clear inline storage under the notifier
+ * lock, so callers must also hold that lock while resolving and copying
+ * an inline entry.
+ *
+ * To use an inline entry after releasing the notifier lock, copy it while
+ * holding the lock and use the copy. The copy does not keep the DMA mapping
+ * valid. Callers must still validate it before committing GPU access.
*
* Return: Pointer to the first device address, NULL if none is populated.
*/
--
2.43.0