The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Praneeth Balasubramanian <praneethb2002@gmail.com>
To: Matthew Brost <matthew.brost@intel.com>,
	Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
	Maxime Ripard <mripard@kernel.org>,
	Thomas Zimmermann <tzimmermann@suse.de>,
	David Airlie <airlied@gmail.com>, Simona Vetter <simona@ffwll.ch>
Cc: "Praneeth Balasubramanian" <praneethb2002@gmail.com>,
	"Thomas Hellström" <thomas.hellstrom@linux.intel.com>,
	"Himal Prasad Ghimiray" <himal.prasad.ghimiray@intel.com>,
	"Honglei Huang" <honghuan@amd.com>,
	dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org
Subject: [RFC PATCH] drm/gpusvm: reject device pages not owned by the caller
Date: Fri,  4 Dec 2026 22:42:01 -0800	[thread overview]
Message-ID: <20261205064202.2527445-1-praneethb2002@gmail.com> (raw)

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


                 reply	other threads:[~2026-12-05  6:42 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261205064202.2527445-1-praneethb2002@gmail.com \
    --to=praneethb2002@gmail.com \
    --cc=airlied@gmail.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=himal.prasad.ghimiray@intel.com \
    --cc=honghuan@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=matthew.brost@intel.com \
    --cc=mripard@kernel.org \
    --cc=simona@ffwll.ch \
    --cc=thomas.hellstrom@linux.intel.com \
    --cc=tzimmermann@suse.de \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox