AMD-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] drm/amdkfd: refuse XNACK for a secondary context
@ 2026-11-27 21:45 Yongqiang Sun
  2026-11-27 22:07 ` sashiko-bot
  0 siblings, 1 reply; 3+ messages in thread
From: Yongqiang Sun @ 2026-11-27 21:45 UTC (permalink / raw)
  To: amd-gfx; +Cc: Yongqiang Sun

A secondary kfd_process shares the caller's mm but registers no MMU
notifier and is not torn down when that mm exits. With XNACK enabled,
svm_range_restore_pages() still maps host pages for its PASID, and the
queue keeps those mappings after the pages are freed.

On an unpatched gfx942 kernel, a shader on the secondary context stored
to an unmapped malloc buffer, the process exec'd while that /dev/kfd fd
stayed open, and the GPU then wrote 0x5a5a5a5a into 64 KiB of memory
owned by the new image.

Do not enable XNACK for a non-primary context, reject
AMDKFD_IOC_SET_XNACK_MODE that asks for it, and drop a retry fault that
arrives for one before any host page is mapped.

Signed-off-by: Yongqiang Sun <Yongqiang.Sun@amd.com>
---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c |  4 +++-
 drivers/gpu/drm/amd/amdkfd/kfd_process.c |  8 ++++++--
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c     | 10 ++++++++++
 3 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
index ae5b430a00310..b589f8eaa0832 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
@@ -1775,7 +1775,9 @@ static int kfd_ioctl_set_xnack_mode(struct file *filep,
 		if (p->xnack_enabled == args->xnack_enabled)
 			goto out_unlock;
 
-		if (args->xnack_enabled && !kfd_process_xnack_mode(p, true)) {
+		if (args->xnack_enabled &&
+		    (p->context_id != KFD_CONTEXT_ID_PRIMARY ||
+		     !kfd_process_xnack_mode(p, true))) {
 			r = -EPERM;
 			goto out_unlock;
 		}
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process.c b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
index 5dc657f32d38c..0c153f9951b38 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_process.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
@@ -1782,8 +1782,12 @@ struct kfd_process *create_process(const struct task_struct *thread, bool primar
 	if (err != 0)
 		goto err_init_apertures;
 
-	/* Check XNACK support after PDDs are created in kfd_init_apertures */
-	process->xnack_enabled = kfd_process_xnack_mode(process, false);
+	/* Check XNACK support after PDDs are created in kfd_init_apertures.
+	 * Only a primary context is torn down with its address space (it owns
+	 * the MMU notifier), so XNACK retry faults must never map host pages
+	 * into a secondary context.
+	 */
+	process->xnack_enabled = primary && kfd_process_xnack_mode(process, false);
 
 	err = svm_range_list_init(process);
 	if (err)
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
index 32f2ca5b1fdfd..f48eea7cc9e92 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
@@ -3158,6 +3158,16 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,
 	}
 	svms = &p->svms;
 
+	/* A secondary context has no MMU notifier and is not torn down with
+	 * its address space, so it must never map host pages on a retry fault.
+	 */
+	if (p->context_id != KFD_CONTEXT_ID_PRIMARY) {
+		pr_debug("retry fault on non-primary kfd process, pasid 0x%x\n",
+			 pasid);
+		r = -EFAULT;
+		goto out;
+	}
+
 	pr_debug("restoring svms 0x%p fault address 0x%llx\n", svms, addr);
 
 	if (atomic_read(&svms->drain_pagefaults)) {
-- 
2.43.0


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

* Re: [PATCH] drm/amdkfd: refuse XNACK for a secondary context
  2026-11-27 21:45 [PATCH] drm/amdkfd: refuse XNACK for a secondary context Yongqiang Sun
@ 2026-11-27 22:07 ` sashiko-bot
  0 siblings, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-27 22:07 UTC (permalink / raw)
  To: Yongqiang Sun, amd-gfx

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] drm/amdkfd: missing primary context check in
  criu_restore_process()
--

commit 03c779d2c6f68cc91d9f9a720f82e6527cf327df
Author: Yongqiang Sun <Yongqiang.Sun@amd.com>

drm/amdkfd: refuse XNACK for a secondary context

This commit disables XNACK by default for non-primary KFD contexts,
rejects enabling XNACK via AMDKFD_IOC_SET_XNACK_MODE on them, and drops
retry page faults arriving for non-primary contexts in
svm_range_restore_pages().

> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> index 5a7f4f0cd0ce7..1aa2b8480bffc 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> @@ -1775,7 +1775,9 @@ static int kfd_ioctl_set_xnack_mode(struct file *filep,
>  		if (p->xnack_enabled == args->xnack_enabled)
>  			goto out_unlock;
>  
> -		if (args->xnack_enabled && !kfd_process_xnack_mode(p, true)) {
> +		if (args->xnack_enabled &&
> +		    (p->context_id != KFD_CONTEXT_ID_PRIMARY ||
> +		     !kfd_process_xnack_mode(p, true))) {
>  			r = -EPERM;
>  			goto out_unlock;
>  		}

[Severity: Medium]
Should criu_restore_process() also reject enabling XNACK when
p->context_id != KFD_CONTEXT_ID_PRIMARY (or should that check live in
kfd_process_xnack_mode())?

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c:criu_restore_process() {
    ...
	if (process_priv.xnack_mode && !kfd_process_xnack_mode(p, true)) {
		pr_err("xnack mode cannot be set\n");
		ret = -EPERM;
		goto exit;
	} else {
		pr_debug("set xnack mode: %d\n", process_priv.xnack_mode);
		p->xnack_enabled = process_priv.xnack_mode;
	}
    ...
}

Invoking AMDKFD_IOC_CRIU_OP (KFD_CRIU_OP_RESTORE) with
process_priv.xnack_mode != 0 on a secondary context still sets
p->xnack_enabled = true.  On per-process XNACK GPUs, update_qpd_v9() and
update_qpd_v12_1() then program the secondary context with hardware page-
fault retry enabled while svm_range_restore_pages() rejects its retry
faults with -EFAULT.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127214544.1058794-1-Yongqiang.Sun@amd.com?part=1

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

* [PATCH] drm/amdkfd: refuse XNACK for a secondary context
@ 2026-11-28  1:13 Yongqiang Sun
  0 siblings, 0 replies; 3+ messages in thread
From: Yongqiang Sun @ 2026-11-28  1:13 UTC (permalink / raw)
  To: amd-gfx; +Cc: Yongqiang Sun

A secondary kfd_process shares the caller's mm but registers no MMU
notifier and is not torn down when that mm exits. With XNACK enabled,
svm_range_restore_pages() still maps host pages for its PASID, and the
queue keeps those mappings after the pages are freed.

On an unpatched gfx942 kernel, a shader on the secondary context stored
to an unmapped malloc buffer, the process exec'd while that /dev/kfd fd
stayed open, and the GPU then wrote 0x5a5a5a5a into 64 KiB of memory
owned by the new image.

Do not enable XNACK for a non-primary context, reject
AMDKFD_IOC_SET_XNACK_MODE that asks for it, and drop a retry fault that
arrives for one before any host page is mapped.

Signed-off-by: Yongqiang Sun <Yongqiang.Sun@amd.com>
---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 13 ++++++++-----
 drivers/gpu/drm/amd/amdkfd/kfd_process.c |  8 ++++++--
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c     | 10 ++++++++++
 3 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
index ae5b430a00310..38ece99f70db9 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
@@ -1775,7 +1775,9 @@ static int kfd_ioctl_set_xnack_mode(struct file *filep,
 		if (p->xnack_enabled == args->xnack_enabled)
 			goto out_unlock;
 
-		if (args->xnack_enabled && !kfd_process_xnack_mode(p, true)) {
+		if (args->xnack_enabled &&
+		    (p->context_id != KFD_CONTEXT_ID_PRIMARY ||
+		     !kfd_process_xnack_mode(p, true))) {
 			r = -EPERM;
 			goto out_unlock;
 		}
@@ -2369,14 +2371,15 @@ static int criu_restore_process(struct kfd_process *p,
 	}
 
 	pr_debug("Setting XNACK mode\n");
-	if (process_priv.xnack_mode && !kfd_process_xnack_mode(p, true)) {
+	if (process_priv.xnack_mode &&
+	    (p->context_id != KFD_CONTEXT_ID_PRIMARY ||
+	     !kfd_process_xnack_mode(p, true))) {
 		pr_err("xnack mode cannot be set\n");
 		ret = -EPERM;
 		goto exit;
-	} else {
-		pr_debug("set xnack mode: %d\n", process_priv.xnack_mode);
-		p->xnack_enabled = process_priv.xnack_mode;
 	}
+	pr_debug("set xnack mode: %d\n", process_priv.xnack_mode);
+	p->xnack_enabled = process_priv.xnack_mode;
 
 exit:
 	return ret;
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process.c b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
index 5dc657f32d38c..0c153f9951b38 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_process.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
@@ -1782,8 +1782,12 @@ struct kfd_process *create_process(const struct task_struct *thread, bool primar
 	if (err != 0)
 		goto err_init_apertures;
 
-	/* Check XNACK support after PDDs are created in kfd_init_apertures */
-	process->xnack_enabled = kfd_process_xnack_mode(process, false);
+	/* Check XNACK support after PDDs are created in kfd_init_apertures.
+	 * Only a primary context is torn down with its address space (it owns
+	 * the MMU notifier), so XNACK retry faults must never map host pages
+	 * into a secondary context.
+	 */
+	process->xnack_enabled = primary && kfd_process_xnack_mode(process, false);
 
 	err = svm_range_list_init(process);
 	if (err)
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
index 32f2ca5b1fdfd..f48eea7cc9e92 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
@@ -3158,6 +3158,16 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,
 	}
 	svms = &p->svms;
 
+	/* A secondary context has no MMU notifier and is not torn down with
+	 * its address space, so it must never map host pages on a retry fault.
+	 */
+	if (p->context_id != KFD_CONTEXT_ID_PRIMARY) {
+		pr_debug("retry fault on non-primary kfd process, pasid 0x%x\n",
+			 pasid);
+		r = -EFAULT;
+		goto out;
+	}
+
 	pr_debug("restoring svms 0x%p fault address 0x%llx\n", svms, addr);
 
 	if (atomic_read(&svms->drain_pagefaults)) {
-- 
2.43.0


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

end of thread, other threads:[~2026-11-28  1:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 21:45 [PATCH] drm/amdkfd: refuse XNACK for a secondary context Yongqiang Sun
2026-11-27 22:07 ` sashiko-bot
  -- strict thread matches above, loose matches on Subject: below --
2026-11-28  1:13 Yongqiang Sun

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox