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

* Re: [RFC PATCH] drm/gpusvm: reject device pages not owned by the caller
  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
  0 siblings, 1 reply; 3+ messages in thread
From: sashiko-bot @ 2026-12-05  7:03 UTC (permalink / raw)
  To: Praneeth Balasubramanian; +Cc: dri-devel

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] drm/gpusvm: -EOPNOTSUPP on foreign coherent pages livelocks Xe
  pagefaults
- [Low] drm/gpusvm: missing Fixes trailer for commit 99624bdff867
--

commit 662bc69b632b0f40716e04f89aa496467afcd5ff
Author: Praneeth Balasubramanian <praneethb2002@gmail.com>

> drm/gpusvm: reject device pages not owned by the caller
>
> 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>

[Severity: Low]
This fixes a type-confusion bug introduced when GPU SVM support was
added.  Should this include:

  Fixes: 99624bdff867 ("drm/gpusvm: Add support for GPU Shared Virtual Memory")

> diff --git a/drivers/gpu/drm/drm_gpusvm.c b/drivers/gpu/drm/drm_gpusvm.c
> index de2fa2684e846..a5fcf4de55497 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;
> +			}

[Severity: High]
Can returning -EOPNOTSUPP here livelock Xe GPU page fault handling on
foreign device coherent pages?

On a normal Xe GPU page fault (!ctx.devmem_only) hitting an SVM VMA
with a present foreign device coherent page, xe_pagefault_service()
holds down_read(&vm->lock) and calls xe_svm_handle_pagefault() ->
__xe_svm_handle_pagefault() -> xe_svm_range_get_pages().

xe_svm_range_get_pages() treats -EOPNOTSUPP as a transient condition
and calls drm_gpusvm_range_evict(), which invokes hmm_range_fault()
with dev_private_owner = NULL.  In hmm_vma_handle_pte(), owner-mismatch
faulting only runs for non-present PTEs, so present device coherent
pages stay mapped and drm_gpusvm_range_evict() is a no-op.

__xe_svm_handle_pagefault() then loops back to retry without bound
whenever !ctx.devmem_only:

drivers/gpu/drm/xe/xe_svm.c:__xe_svm_handle_pagefault() {
    ...
	if (err == -EOPNOTSUPP || err == -EFAULT || err == -EPERM) {
		ctx.timeslice_ms <<= 1;	/* Double timeslice if we have to retry */
		if (migrate_try_count > 0 || !ctx.devmem_only) {
			...
			goto retry;
		}
    ...
}

This spins the pagefault worker forever while holding
down_read(&vm->lock), wedging pagefault servicing and permanently
starving down_write(&vm->lock) waiters such as VM bind and teardown.

Returning a distinct terminal error code for non-evictable foreign
pages (or mapping them as system memory where supported) would avoid
the infinite retry loop.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205064202.2527445-1-praneethb2002@gmail.com?part=1

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

* Re: [RFC PATCH] drm/gpusvm: reject device pages not owned by the caller
  2026-12-05  7:03 ` sashiko-bot
@ 2026-12-05  8:21   ` Praneeth Balasubramanian
  0 siblings, 0 replies; 3+ messages in thread
From: Praneeth Balasubramanian @ 2026-12-05  8:21 UTC (permalink / raw)
  To: sashiko-reviews
  Cc: Matthew Brost, Maarten Lankhorst, Maxime Ripard,
	Thomas Zimmermann, David Airlie, Simona Vetter,
	Thomas Hellström, Himal Prasad Ghimiray, Honglei Huang,
	dri-devel, linux-kernel

On Sat, 05 Dec 2026 07:03:31 +0000, sashiko-bot@kernel.org wrote:
> [Severity: High]
> Can returning -EOPNOTSUPP here livelock Xe GPU page fault handling on
> foreign device coherent pages?

Yes. __xe_svm_handle_pagefault() retries -EOPNOTSUPP, -EFAULT and -EPERM
without a bound when devmem_only is not set, and
drm_gpusvm_range_evict() leaves a present device coherent page where it
is. So v2 returns -EINVAL for a foreign page, which ends the fault. The
userptr path returns it to the caller as before.

> Should this include:
>
>   Fixes: 99624bdff867 ("drm/gpusvm: Add support for GPU Shared Virtual Memory")

Yes, that commit already read zone_device_data as a zdd without an
owner check. Added in v2.

For the DRM SVM folks: is failing the mapping what you want here, or
should foreign device coherent pages be mapped like system memory? And
if failing is right, is -EINVAL the code you prefer?

^ permalink raw reply	[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.