All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH] drm/gpusvm: reject device pages not owned by the caller
@ 2026-12-05  6:42 Praneeth Balasubramanian
  2026-12-05  7:03 ` sashiko-bot
  0 siblings, 1 reply; 3+ messages in thread
From: Praneeth Balasubramanian @ 2026-12-05  6:42 UTC (permalink / raw)
  To: Matthew Brost, Maarten Lankhorst, Maxime Ripard,
	Thomas Zimmermann, David Airlie, Simona Vetter
  Cc: Praneeth Balasubramanian, Thomas Hellström,
	Himal Prasad Ghimiray, Honglei Huang, dri-devel, linux-kernel

drm_gpusvm_dma_map_pages() treats every device private and device
coherent page returned by hmm_range_fault() as populated from a
drm_pagemap. It calls drm_pagemap_page_to_dpagemap(), which reads
zone_device_data as a struct drm_pagemap_zdd and dereferences
zdd->devmem_allocation->dpagemap.

That assumption does not hold for foreign pages. hmm_range_fault()
filters non-present device private entries by owner. But a present
device coherent page is reported as valid regardless of owner. A
coherent VRAM page migrated by amdkfd SVM holds a struct svm_range_bo
in zone_device_data, not a drm_pagemap_zdd. When such a page reaches
drm_gpusvm_dma_map_pages() through drm_gpusvm_get_pages(), for
example from the Xe userptr path which sets allow_mixed,
drm_pagemap_page_to_dpagemap() misreads the object type and follows a
bogus dpagemap pointer into drm_pagemap_get() and
dpagemap->ops->device_map().

Compare page_pgmap(page)->owner against
ctx->device_private_page_owner before interpreting zone_device_data.
Fail with -EOPNOTSUPP on mismatch, the same error already used for
unsupported page mixes. Device private pages keep working unchanged
since hmm_range_fault() already filters them by that owner.

This was found by an LLM-based audit that checks code against the rules
stated in kernel comments.

Signed-off-by: Praneeth Balasubramanian <praneethb2002@gmail.com>
---

Notes:
    This was found by an LLM-based audit, part of research at UCSB SecLab.
    An LLM turned a kernel comment into a rule, and three LLM votes checked
    the code against it. The fix and changelog were drafted with GLM-5.3,
    then checked against the source and partly rewritten with Claude Opus
    5.5. I reviewed the patch and take responsibility for it.
    
    Testing: builds without new warnings with W=1 (x86_64 allmodconfig),
    clang 23. checkpatch.pl is clean. Not runtime-tested, and there is no
    reproducer. I do not have this hardware.
    
    RFC: rejecting foreign device coherent pages is one option. Mapping them
    as plain system memory may be what you want instead. This needs a system
    with both an amdkfd coherent memory device and an Xe device, which I do
    not have.

 drivers/gpu/drm/drm_gpusvm.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/drm_gpusvm.c b/drivers/gpu/drm/drm_gpusvm.c
index de2fa2684e84..a5fcf4de5549 100644
--- a/drivers/gpu/drm/drm_gpusvm.c
+++ b/drivers/gpu/drm/drm_gpusvm.c
@@ -1614,8 +1614,20 @@ static int drm_gpusvm_dma_map_pages(struct drm_gpusvm *gpusvm,
 		order = drm_gpusvm_hmm_pfn_to_order(pfns[i], i, npages);
 		if (is_device_private_page(page) ||
 		    is_device_coherent_page(page)) {
-			struct drm_pagemap_zdd *__zdd =
-				drm_pagemap_page_zone_device_data(page);
+			struct drm_pagemap_zdd *__zdd;
+
+			/*
+			 * hmm_range_fault() reports present device coherent
+			 * pages of any owner. Only pages of our owner come
+			 * from a drm_pagemap and carry a drm_pagemap_zdd.
+			 */
+			if (page_pgmap(page)->owner !=
+			    ctx->device_private_page_owner) {
+				err = -EOPNOTSUPP;
+				goto err_unmap;
+			}
+
+			__zdd = drm_pagemap_page_zone_device_data(page);
 
 			if (!ctx->allow_mixed &&
 			    zdd != __zdd && i > 0) {

base-commit: 29beb21c21ada7207692b2d69fc58dcd15dab6e9
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-05  8:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05  6:42 [RFC PATCH] drm/gpusvm: reject device pages not owned by the caller Praneeth Balasubramanian
2026-12-05  7:03 ` sashiko-bot
2026-12-05  8:21   ` Praneeth Balasubramanian

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.