Skip to contentSkip to conversation

Revert "drm/amdgpu: get rid of amdgpu_drm_ioctl"

PATCH In review Shahyan Soltani 3 participants 5 messages Dec 3

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Revert "drm/amdgpu: get rid of amdgpu_drm_ioctl"

Commit 726d9fa92d8b ("drm/amdgpu: get rid of amdgpu_drm_ioctl") is broken as it forgot to account for non amdgpu IOCTLs that required the GPU to be active. Without the wrapper, these IOCTLs can no longer resume the device, so they can run while the GPU is suspended.

Revert commit 726d9fa92d8b ("drm/amdgpu: get rid of amdgpu_drm_ioctl") to allow the GPU to resume on all IOCTLs.

Fixes 726d9fa92d8b ("drm/amdgpu: get rid of amdgpu_drm_ioctl")
---
 drivers/gpu/drm/amd/amdgpu/Makefile       |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c   | 23 +++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.h   |  6 +++
 drivers/gpu/drm/amd/amdgpu/amdgpu_ioc32.c | 45 +++++++++++++++++++++++
 4 files changed, 73 insertions(+), 2 deletions(-)
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_ioc32.c
drivers/gpu/drm/amd/amdgpu/Makefile +1 -0
index 6a5ce2b89378..c1ef2e0af033 100644
@@ -340,6 +340,7 @@ include $(FULL_AMD_PATH)/acp/Makefile
 amdgpu-y += $(AMD_ACP_FILES)
 endif
 
+amdgpu-$(CONFIG_COMPAT) += amdgpu_ioc32.o
 amdgpu-$(CONFIG_VGA_SWITCHEROO) += amdgpu_atpx_handler.o
 amdgpu-$(CONFIG_ACPI) += amdgpu_acpi.o
 amdgpu-$(CONFIG_HMM_MIRROR) += amdgpu_hmm.o
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +21 -2
index 11fcf068b0a7..57bbb057108f 100644
@@ -3019,6 +3019,25 @@ static int amdgpu_drm_release(struct inode *inode, struct file *filp)
 	return drm_release(inode, filp);
 }
 
+long amdgpu_drm_ioctl(struct file *filp,
+		      unsigned int cmd, unsigned long arg)
+{
+	struct drm_file *file_priv = filp->private_data;
+	struct drm_device *dev;
+	long ret;
+
+	dev = file_priv->minor->dev;
+	ret = pm_runtime_get_sync(dev->dev);
+	if (ret < 0)
+		goto out;
+
+	ret = drm_ioctl(filp, cmd, arg);
+
+out:
+	pm_runtime_put_autosuspend(dev->dev);
+	return ret;
+}
+
 static const struct dev_pm_ops amdgpu_pm_ops = {
 	.prepare = pm_sleep_ptr(amdgpu_pmops_prepare),
 	.complete = pm_sleep_ptr(amdgpu_pmops_complete),
@@ -3051,12 +3070,12 @@ static const struct file_operations amdgpu_driver_kms_fops = {
 	.open = drm_open,
 	.flush = amdgpu_flush,
 	.release = amdgpu_drm_release,
-	.unlocked_ioctl = drm_ioctl,
+	.unlocked_ioctl = amdgpu_drm_ioctl,
 	.mmap = drm_gem_mmap,
 	.poll = drm_poll,
 	.read = drm_read,
 #ifdef CONFIG_COMPAT
-	.compat_ioctl = drm_compat_ioctl,
+	.compat_ioctl = amdgpu_kms_compat_ioctl,
 #endif
 #ifdef CONFIG_PROC_FS
 	.show_fdinfo = drm_show_fdinfo,
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.h +6 -0
index 6b06c0956e48..2d86cc6f7f4d 100644
@@ -43,4 +43,10 @@
 
 extern const struct drm_driver amdgpu_partition_driver;
 
+long amdgpu_drm_ioctl(struct file *filp,
+		      unsigned int cmd, unsigned long arg);
+
+long amdgpu_kms_compat_ioctl(struct file *filp,
+			     unsigned int cmd, unsigned long arg);
+
 #endif
drivers/gpu/drm/amd/amdgpu/amdgpu_ioc32.c new file +45 -0

45 lines not on this page · raw message

Signature · 1 line
2.56.0
#1 lore raw

PATCH 2/2 drm/amdgpu: implement IOCTL specific skip resume list

Following the revert of Commit 726d9fa92d8b ("drm/amdgpu: get rid of amdgpu_drm_ioctl"), every IOCTL resumes the device from BACO/BOCO on entry, including the IOCTLs which never required hardware access.

Introduce a new amdgpu_ioctls_skip_resume[] list which specifies which IOCTLs the driver should not take a runtime reference for upon entry. Any IOCTL not on this list will automatically resume the GPU as the default behaviour.

Edit the behaviour of the amdgpu_drm_ioctl wrappers to conditionally resume the GPU in regards to the new list.

Move the 32bit compat wrapper into amdgpu_drv.c, so that it can share the list.

---
 drivers/gpu/drm/amd/amdgpu/Makefile       |  1 -
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c   | 90 +++++++++++++++++++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.h   |  5 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ioc32.c | 45 ------------
 4 files changed, 87 insertions(+), 54 deletions(-)
 delete mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_ioc32.c
drivers/gpu/drm/amd/amdgpu/Makefile +0 -1
index c1ef2e0af033..6a5ce2b89378 100644
@@ -340,7 +340,6 @@ include $(FULL_AMD_PATH)/acp/Makefile
 amdgpu-y += $(AMD_ACP_FILES)
 endif
 
-amdgpu-$(CONFIG_COMPAT) += amdgpu_ioc32.o
 amdgpu-$(CONFIG_VGA_SWITCHEROO) += amdgpu_atpx_handler.o
 amdgpu-$(CONFIG_ACPI) += amdgpu_acpi.o
 amdgpu-$(CONFIG_HMM_MIRROR) += amdgpu_hmm.o
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +85 -5

110 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.h +2 -3
index 2d86cc6f7f4d..434bb3bf1c94 100644
@@ -43,10 +43,9 @@
 
 extern const struct drm_driver amdgpu_partition_driver;
 
+#define AMDGPU_IOCTL_SKIP_RESUME(ioctl)	[DRM_IOCTL_NR(ioctl)] = true
+
 long amdgpu_drm_ioctl(struct file *filp,
 		      unsigned int cmd, unsigned long arg);
 
-long amdgpu_kms_compat_ioctl(struct file *filp,
-			     unsigned int cmd, unsigned long arg);
-
 #endif
drivers/gpu/drm/amd/amdgpu/amdgpu_ioc32.c deleted +0 -45

45 lines not on this page · raw message

Signature · 1 line
2.56.0
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] drm/amdgpu: implement IOCTL specific skip resume list

Quoted 10 lines · Shahyan Soltani

Following the revert of Commit 726d9fa92d8b ("drm/amdgpu: get rid of amdgpu_drm_ioctl"), every IOCTL resumes the device from BACO/BOCO on entry, including the IOCTLs which never required hardware access.

Introduce a new amdgpu_ioctls_skip_resume[] list which specifies which IOCTLs the driver should not take a runtime reference for upon entry. Any IOCTL not on this list will automatically resume the GPU as the default behaviour.

Quoted 8 lines

Edit the behaviour of the amdgpu_drm_ioctl wrappers to conditionally resume the GPU in regards to the new list.

Move the 32bit compat wrapper into amdgpu_drv.c, so that it can share the list.

Series is:

Quoted 206 lines
---
 drivers/gpu/drm/amd/amdgpu/Makefile       |  1 -
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c   | 90 +++++++++++++++++++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.h   |  5 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ioc32.c | 45 ------------
 4 files changed, 87 insertions(+), 54 deletions(-)
 delete mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_ioc32.c
drivers/gpu/drm/amd/amdgpu/Makefile +0 -1
index c1ef2e0af033..6a5ce2b89378 100644
@@ -340,7 +340,6 @@ include $(FULL_AMD_PATH)/acp/Makefile
 amdgpu-y += $(AMD_ACP_FILES)
 endif
 
-amdgpu-$(CONFIG_COMPAT) += amdgpu_ioc32.o
 amdgpu-$(CONFIG_VGA_SWITCHEROO) += amdgpu_atpx_handler.o
 amdgpu-$(CONFIG_ACPI) += amdgpu_acpi.o
 amdgpu-$(CONFIG_HMM_MIRROR) += amdgpu_hmm.o
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +85 -5
index 57bbb057108f..9e6d6292a6b5 100644
@@ -3019,25 +3019,105 @@ static int amdgpu_drm_release(struct inode *inode, struct file *filp)
        return drm_release(inode, filp);
 }
 
+/**
+ * amdgpu_ioctls_skip_resume[] - List of IOCTLs that do not require hardware access.
+ *
+ * This list needs to be maintained against the most recent DRM IOCTLs.
+ *
+ * If an IOCTL is specified within this list, a runtime reference will not be
+ * held for the IOCTL, preventing the GPU from being resumed upon entry.
+ *
+ * TODO: Add the remaining DRM IOCTLs which don't require hardware access.
+ */
+static const bool amdgpu_ioctls_skip_resume[] = {
+       /* DRM IOCTLs */
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_VERSION),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_GET_CLIENT),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_GETPROPERTY),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_GETRESOURCES),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_LIST_LESSEES),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_GETPLANE),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_OBJ_GETPROPERTIES),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_GETPROPBLOB),
+       /* AMDGPU IOCTLs */
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_CREATE),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_CTX),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_VM),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_SCHED),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_BO_LIST),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_FENCE_TO_HANDLE),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_MMAP),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_WAIT_IDLE),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_CS),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_INFO),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_WAIT_CS),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_WAIT_FENCES),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_METADATA),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_VA),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_OP),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_USERPTR),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_USERQ),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_USERQ_SIGNAL),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_USERQ_WAIT),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_LIST_HANDLES),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_PROC_OPTIONS),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_UALINK_HANDLE),
+};
+
 long amdgpu_drm_ioctl(struct file *filp,
                      unsigned int cmd, unsigned long arg)
 {
        struct drm_file *file_priv = filp->private_data;
        struct drm_device *dev;
+       unsigned int nr = DRM_IOCTL_NR(cmd);
+       bool pm_ref = nr >= ARRAY_SIZE(amdgpu_ioctls_skip_resume) ?
+                     true : !amdgpu_ioctls_skip_resume[nr];
        long ret;
 
-       dev = file_priv->minor->dev;
-       ret = pm_runtime_get_sync(dev->dev);
-       if (ret < 0)
-               goto out;
+       if (pm_ref) {
+               dev = file_priv->minor->dev;
+               ret = pm_runtime_resume_and_get(dev->dev);
+               if (ret < 0)
+                       goto out;
+       }
 
        ret = drm_ioctl(filp, cmd, arg);
 
+       if (pm_ref)
+               pm_runtime_put_autosuspend(dev->dev);
 out:
-       pm_runtime_put_autosuspend(dev->dev);
        return ret;
 }
 
+#ifdef CONFIG_COMPAT
+static long amdgpu_kms_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
+{
+       struct drm_file *file_priv = filp->private_data;
+       struct drm_device *dev;
+       unsigned int nr = DRM_IOCTL_NR(cmd);
+       bool pm_ref = nr >= ARRAY_SIZE(amdgpu_ioctls_skip_resume) ?
+                     true : !amdgpu_ioctls_skip_resume[nr];
+       long ret;
+
+       if (nr < DRM_COMMAND_BASE) {
+               if (pm_ref) {
+                       dev = file_priv->minor->dev;
+                       ret = pm_runtime_resume_and_get(dev->dev);
+                       if (ret < 0)
+                               goto out;
+               }
+
+               ret = drm_compat_ioctl(filp, cmd, arg);
+               if (pm_ref)
+                       pm_runtime_put_autosuspend(dev->dev);
+       } else {
+               ret = amdgpu_drm_ioctl(filp, cmd, arg);
+       }
+out:
+       return ret;
+}
+#endif
+
 static const struct dev_pm_ops amdgpu_pm_ops = {
        .prepare = pm_sleep_ptr(amdgpu_pmops_prepare),
        .complete = pm_sleep_ptr(amdgpu_pmops_complete),
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.h +2 -3
index 2d86cc6f7f4d..434bb3bf1c94 100644
@@ -43,10 +43,9 @@
 
 extern const struct drm_driver amdgpu_partition_driver;
 
+#define AMDGPU_IOCTL_SKIP_RESUME(ioctl)        [DRM_IOCTL_NR(ioctl)] = true
+
 long amdgpu_drm_ioctl(struct file *filp,
                      unsigned int cmd, unsigned long arg);
 
-long amdgpu_kms_compat_ioctl(struct file *filp,
-                            unsigned int cmd, unsigned long arg);
-
 #endif
drivers/gpu/drm/amd/amdgpu/amdgpu_ioc32.c deleted +0 -46
deleted file mode 100644
index a1cbd7c3deb2..000000000000
@@ -1,45 +0,0 @@
-/*
- * \file amdgpu_ioc32.c
- *
- * 32-bit ioctl compatibility routines for the AMDGPU DRM.
- *
- * \author Paul Mackerras <[email protected]>
- *
- * Copyright (C) Paul Mackerras 2005
- * All Rights Reserved.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice (including the next
- * paragraph) shall be included in all copies or substantial portions of the
- * Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
- * THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- * IN THE SOFTWARE.
- */
-#include <linux/compat.h>
-
-#include <drm/amdgpu_drm.h>
-#include <drm/drm_ioctl.h>
-
-#include "amdgpu_drv.h"
-
-long amdgpu_kms_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
-{
-       unsigned int nr = DRM_IOCTL_NR(cmd);
-
-       if (nr < DRM_COMMAND_BASE)
-               return drm_compat_ioctl(filp, cmd, arg);
-
-       return amdgpu_drm_ioctl(filp, cmd, arg);
-}
--

2.56.0

#3 lore raw

Re: [PATCH 2/2] drm/amdgpu: implement IOCTL specific skip resume list

In reply to Shahyan Soltani
Thank you for your contribution! Sashiko AI review found 9 potential issues to consider: - [High] drm/amdgpu: missing runtime PM resume for DRM_IOCTL_AMDGPU_VM - [High] drm/amdgpu: missing runtime…

Thank you for your contribution! Sashiko AI review found 9 potential issues to consider:
- [High] drm/amdgpu: missing runtime PM resume for DRM_IOCTL_AMDGPU_VM
- [High] drm/amdgpu: missing runtime PM resume for DRM_IOCTL_AMDGPU_CTX free path
- [High] drm/amdgpu: amdgpu_kms_compat_ioctl() breaks 32-bit x86 DRM_IOCTL_MODE_ADDFB2
- [Medium] drm/amdgpu: spurious runtime resume on non-DRM ioctls and unplugged devices
- [Medium] drm/amdgpu: DRM_IOCTL_AMDGPU_UALINK_HANDLE skips runtime PM resume
- [Medium] drm/amdgpu: amdgpu_ioctls_skip_resume[] omits syncobj and cap IOCTLs
- [Low] drm/amdgpu: invalid kernel-doc syntax on amdgpu_ioctls_skip_resume[]
- [Low] drm/amdgpu: unnecessary external linkage for amdgpu_drm_ioctl() and macro
- [Low] drm/amdgpu: missing array_index_nospec() in amdgpu_ioctls_skip_resume[] lookup
--

commit d5dc7cf8e3ca9fa610a736900cd13686834c705d
Author: Shahyan Soltani <[email protected]>

drm/amdgpu: implement IOCTL specific skip resume list

This patch introduces amdgpu_ioctls_skip_resume[] so amdgpu_drm_ioctl() and amdgpu_kms_compat_ioctl() only acquire a runtime PM reference for ioctls not on the list, and moves amdgpu_kms_compat_ioctl() into amdgpu_drv.c.

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c static int amdgpu_drm_release(struct inode *inode, struct file *filp) · line 3045

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
index 11c782a4914ed..0b1359c5125df 100644
@@ -3041,25 +3041,105 @@ static int amdgpu_drm_release(struct inode *inode, struct file *filp)
 	return drm_release(inode, filp);
 }
 
+/**
+ * amdgpu_ioctls_skip_resume[] - List of IOCTLs that do not require hardware access.

[Severity: Low]
Will this /** comment trigger a kernel-doc warning during make htmldocs and W=1 builds? Documentation/gpu/amdgpu/module-parameters.rst runs kernel-doc over amdgpu_drv.c, and the trailing [] without a kernel-doc keyword is rejected. Since amdgpu_ioctls_skip_resume[] is a static table, a regular /* comment avoids the warning.

[ ... ]

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c static int amdgpu_drm_release(struct inode *inode, struct file *filp) · line 3041

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +10 -0
+static const bool amdgpu_ioctls_skip_resume[] = {
+	/* DRM IOCTLs */
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_VERSION),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_GET_CLIENT),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_GETPROPERTY),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_GETRESOURCES),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_LIST_LESSEES),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_GETPLANE),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_OBJ_GETPROPERTIES),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_GETPROPBLOB),

[Severity: Medium]
Should the software-only DRM_IOCTL_SYNCOBJ_* ioctls along with DRM_IOCTL_GET_CAP, DRM_IOCTL_SET_CLIENT_CAP, and DRM_IOCTL_SET_CLIENT_NAME also be on this list?

Leaving them out means every syncobj ioctl takes dev->power.lock in __pm_runtime_resume() and wakes a suspended GPU, while blocking waits in drm_syncobj_array_wait_timeout() hold a runtime PM reference for the entire wait and prevent autosuspend. Similarly, DRM_IOCTL_GET_CAP during client probing wakes a suspended GPU even though drm_getcap() only reads in-memory state.

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c static int amdgpu_drm_release(struct inode *inode, struct file *filp) · line 3044

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +3 -0
+	/* AMDGPU IOCTLs */
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_CREATE),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_CTX),

[Severity: High]
Can skipping runtime resume for DRM_IOCTL_AMDGPU_CTX cause amdgpu_ctx_fini() to access SMU and CG/PG hardware while the GPU is suspended?

amdgpu_ctx_stable_pstate() drops its runtime PM reference immediately after assigning adev->pm.stable_pstate_ctx = ctx, so the GPU can autosuspend while that context is still alive. A subsequent AMDGPU_CTX_OP_FREE_CTX then runs amdgpu_ctx_fini() -> __amdgpu_ctx_set_stable_pstate() -> amdgpu_dpm_force_performance_level() without holding a runtime PM reference, risking SMU timeouts or GPU hangs.

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c static int amdgpu_drm_release(struct inode *inode, struct file *filp) · line 3045

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_VM),

[Severity: High]
Does skipping runtime resume for DRM_IOCTL_AMDGPU_VM allow direct GFXHUB MMIO writes and TLB flushes against a suspended GPU?

Neither amdgpu_vm_ioctl() nor its callees take a runtime PM reference, yet AMDGPU_VM_OP_RESERVE_VMID and AMDGPU_VM_OP_UNRESERVE_VMID call amdgpu_gmc_bind_vmid(), which programs VM page-table base registers via setup_vm_pt_regs() and flushes the GPU TLB via amdgpu_gmc_flush_gpu_tlb(). Running them while runtime-suspended risks PCIe bus errors or GPU hangs.

[ ... ]

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c static int amdgpu_drm_release(struct inode *inode, struct file *filp) · line 3065

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_UALINK_HANDLE),
+};

[Severity: Medium]
Can DRM_IOCTL_AMDGPU_UALINK_HANDLE issue LSDMA MMIO transfers or submit SDMA jobs while the GPU is runtime-suspended?

ualink_v1_0_ip_funcs has no .suspend callback, so accel_state stays AMDGPU_UALINK_ACCEL_STATE_ACTIVE across runtime suspend. When DRM_AMDGPU_UALINK_HANDLE_OP_IMPORT runs without a runtime PM reference, amdgpu_ualink_import_handle() reaches amdgpu_ualink_send_command() and calls amdgpu_lsdma_copy_mem() or amdgpu_job_submit() against a powered-down device, causing LSDMA errors or SDMA fence timeouts.

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c static int amdgpu_drm_release(struct inode *inode, struct file *filp) · line 3074

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +3 -0
 long amdgpu_drm_ioctl(struct file *filp,
 		      unsigned int cmd, unsigned long arg)
 {
 	struct drm_file *file_priv = filp->private_data;
 	struct drm_device *dev;
+	unsigned int nr = DRM_IOCTL_NR(cmd);
+	bool pm_ref = nr >= ARRAY_SIZE(amdgpu_ioctls_skip_resume) ?
+		      true : !amdgpu_ioctls_skip_resume[nr];

[Severity: Low]
Should this lookup (and the copy in amdgpu_kms_compat_ioctl()) use array_index_nospec() or size amdgpu_ioctls_skip_resume[] to [_IOC_NRMASK + 1]? Since nr is a user-controlled 8-bit value and the table has 200 entries, speculative execution past the bounds check can read out of bounds in .rodata.

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c static int amdgpu_drm_release(struct inode *inode, struct file *filp) · line 3082

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +6 -4
 	long ret;
 
-	dev = file_priv->minor->dev;
-	ret = pm_runtime_get_sync(dev->dev);
-	if (ret < 0)
-		goto out;
+	if (pm_ref) {
+		dev = file_priv->minor->dev;
+		ret = pm_runtime_resume_and_get(dev->dev);
+		if (ret < 0)
+			goto out;
+	}

[Severity: Medium]
Can non-DRM ioctls or calls on an unplugged device spuriously invoke pm_runtime_resume_and_get() before drm_ioctl() rejects them?

Both amdgpu_drm_ioctl() and amdgpu_kms_compat_ioctl() check only DRM_IOCTL_NR(cmd), not DRM_IOCTL_TYPE(cmd) == DRM_IOCTL_BASE or drm_dev_is_unplugged(dev). A routine probe such as TCGETS from isatty() will wake a suspended GPU only for drm_ioctl() to return -ENOTTY, and calls on a hot-unplugged device will attempt a runtime resume instead of immediately returning -ENODEV.

[ ... ]

Quoted 18 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +18 -0
+#ifdef CONFIG_COMPAT
+static long amdgpu_kms_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
+{
+	struct drm_file *file_priv = filp->private_data;
+	struct drm_device *dev;
+	unsigned int nr = DRM_IOCTL_NR(cmd);
+	bool pm_ref = nr >= ARRAY_SIZE(amdgpu_ioctls_skip_resume) ?
+		      true : !amdgpu_ioctls_skip_resume[nr];
+	long ret;
+
+	if (nr < DRM_COMMAND_BASE) {
+		if (pm_ref) {
+			dev = file_priv->minor->dev;
+			ret = pm_runtime_resume_and_get(dev->dev);
+			if (ret < 0)
+				goto out;
+		}
+

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c static int amdgpu_drm_release(struct inode *inode, struct file *filp) · line 3119

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +10 -0
+		ret = drm_compat_ioctl(filp, cmd, arg);
+		if (pm_ref)
+			pm_runtime_put_autosuspend(dev->dev);
+	} else {
+		ret = amdgpu_drm_ioctl(filp, cmd, arg);
+	}
+out:
+	return ret;
+}
+#endif

[Severity: High]
Does routing nr >= DRM_COMMAND_BASE to amdgpu_drm_ioctl() break 32-bit x86 DRM_IOCTL_MODE_ADDFB2 calls?

Core KMS ioctls live in 0xA0..0xFF, and drm_compat_ioctls[] registers compat_drm_mode_addfb2() at 0xB8 on CONFIG_X86. Bypassing drm_compat_ioctl() copies the packed 32-bit struct directly into the 64-bit struct drm_mode_fb_cmd2 without the alignment padding before modifier[4], corrupting the framebuffer modifiers.

Since drm_compat_ioctl() already falls through to drm_ioctl() for ioctls without a compat handler, this wrapper can call drm_compat_ioctl() directly for all ioctls and share the runtime PM helper with amdgpu_drm_ioctl().

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.h line 49

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.h +2 -0
index 2d86cc6f7f4dd..434bb3bf1c946 100644
@@ -43,10 +43,9 @@
 
 extern const struct drm_driver amdgpu_partition_driver;
 
+#define AMDGPU_IOCTL_SKIP_RESUME(ioctl)	[DRM_IOCTL_NR(ioctl)] = true
+
 long amdgpu_drm_ioctl(struct file *filp,
 		      unsigned int cmd, unsigned long arg);

[Severity: Low]
Now that amdgpu_ioc32.c is gone, can amdgpu_drm_ioctl() be made static and both its prototype and AMDGPU_IOCTL_SKIP_RESUME() be moved out of amdgpu_drv.h? Neither is used outside amdgpu_drv.c, and amdgpu_drv.h does not include drm/drm_ioctl.h for DRM_IOCTL_NR().

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#4 lore raw
In reply to Shahyan Soltani
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] drm/amdgpu: amdgpu_kms_compat_ioctl() breaks 32-bit x86 DRM_IOCTL_MODE_ADDFB2 - [Low] drm/amdgpu…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] drm/amdgpu: amdgpu_kms_compat_ioctl() breaks 32-bit x86 DRM_IOCTL_MODE_ADDFB2
- [Low] drm/amdgpu: Fixes tag cites non-ancestor commit 726d9fa92d8b --

commit 3b9f83db39db8a3806aa0b6d95b926ad913db516
Author: Shahyan Soltani <[email protected]>

Quoted 7 lines

Revert "drm/amdgpu: get rid of amdgpu_drm_ioctl"

Commit 726d9fa92d8b ("drm/amdgpu: get rid of amdgpu_drm_ioctl") is broken as it forgot to account for non amdgpu IOCTLs that required the GPU to be active. Without the wrapper, these IOCTLs can no longer resume the device, so they can run while the GPU is suspended.

Quoted 7 lines

Revert commit 726d9fa92d8b ("drm/amdgpu: get rid of amdgpu_drm_ioctl") to allow the GPU to resume on all IOCTLs.

Fixes 726d9fa92d8b ("drm/amdgpu: get rid of amdgpu_drm_ioctl")

[Severity: Low]
Commit 726d9fa92d8b ("drm/amdgpu: get rid of amdgpu_drm_ioctl") is not an ancestor in this tree. Should the commit message and Fixes tag reference the merged commit instead?

Fixes 3d3140b5b0d0 ("drm/amdgpu: get rid of amdgpu_drm_ioctl")

[ ... ]

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ioc32.c new file +0 -0
new file mode 100644
index 0000000000000..a1cbd7c3deb2c

[ ... ]

drivers/gpu/drm/amd/amdgpu/amdgpu_ioc32.c line 45

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ioc32.c new file +9 -0
+long amdgpu_kms_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
+{
+	unsigned int nr = DRM_IOCTL_NR(cmd);
+
+	if (nr < DRM_COMMAND_BASE)
+		return drm_compat_ioctl(filp, cmd, arg);
+
+	return amdgpu_drm_ioctl(filp, cmd, arg);
+}

[Severity: High]
Does this break 32-bit x86 DRM_IOCTL_MODE_ADDFB2 by bypassing drm_compat_ioctl()?

Core DRM KMS ioctls resume at DRM_COMMAND_END (0xa0), including DRM_IOCTL_MODE_ADDFB2 (0xb8). Since only nr < DRM_COMMAND_BASE (0x40) goes to drm_compat_ioctl(), DRM_IOCTL_MODE_ADDFB2 goes through amdgpu_drm_ioctl() -> drm_ioctl(), skipping compat_drm_mode_addfb2().

On x86_64, struct drm_mode_fb_cmd2 has 4 bytes of padding between offsets[4] and modifier[4] that 32-bit x86 omits. Copying the 32-bit struct directly shifts all four modifier[] entries by 4 bytes, corrupting framebuffer modifiers when a 32-bit x86 client uses DRM_MODE_FB_MODIFIERS.

Since amdgpu has no driver-private compat ioctl handlers, wrapping drm_compat_ioctl() directly with runtime PM handling would avoid bypassing compat_drm_mode_addfb2().

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1