Skip to contentSkip to conversation

drm/msm: UAPI error reporting

PATCH Superseded Rob Clark 3 participants 9 messages Nov 21, 2024 – Dec 31, 2024

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Rob Clark

author
#0 lore raw

PATCH 1/1 drm/msm: UAPI error reporting

From: Rob Clark <[email protected]>

Debugging incorrect UAPI usage tends to be a bit painful, so add a helper macro to make it easier to add debug logging which can be enabled at runtime via drm.debug.

---
 drivers/gpu/drm/msm/adreno/adreno_gpu.c | 21 ++++----
 drivers/gpu/drm/msm/msm_drv.c           |  2 +-
 drivers/gpu/drm/msm/msm_drv.h           |  7 +++
 drivers/gpu/drm/msm/msm_gem_submit.c    | 64 +++++++++++--------------
 4 files changed, 46 insertions(+), 48 deletions(-)
drivers/gpu/drm/msm/adreno/adreno_gpu.c +11 -10
index b96ce6fed649..ad7df8736eec 100644
@@ -310,10 +310,11 @@ int adreno_get_param(struct msm_gpu *gpu, struct msm_file_private *ctx,
 		     uint32_t param, uint64_t *value, uint32_t *len)
 {
 	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
+	struct drm_device *drm = gpu->dev;
 
 	/* No pointer params yet */
 	if (*len != 0)
-		return -EINVAL;
+		return UERR(EINVAL, drm, "invalid len");
 
 	switch (param) {
 	case MSM_PARAM_GPU_ID:
@@ -365,12 +366,12 @@ int adreno_get_param(struct msm_gpu *gpu, struct msm_file_private *ctx,
 		return 0;
 	case MSM_PARAM_VA_START:
 		if (ctx->aspace == gpu->aspace)
-			return -EINVAL;
+			return UERR(EINVAL, drm, "requires per-process pgtables");
 		*value = ctx->aspace->va_start;
 		return 0;
 	case MSM_PARAM_VA_SIZE:
 		if (ctx->aspace == gpu->aspace)
-			return -EINVAL;
+			return UERR(EINVAL, drm, "requires per-process pgtables");
 		*value = ctx->aspace->va_size;
 		return 0;
 	case MSM_PARAM_HIGHEST_BANK_BIT:
@@ -386,14 +387,15 @@ int adreno_get_param(struct msm_gpu *gpu, struct msm_file_private *ctx,
 		*value = adreno_gpu->ubwc_config.macrotile_mode;
 		return 0;
 	default:
-		DBG("%s: invalid param: %u", gpu->name, param);
-		return -EINVAL;
+		return UERR(EINVAL, drm, "%s: invalid param: %u", gpu->name, param);
 	}
 }
 
 int adreno_set_param(struct msm_gpu *gpu, struct msm_file_private *ctx,
 		     uint32_t param, uint64_t value, uint32_t len)
 {
+	struct drm_device *drm = gpu->dev;
+
 	switch (param) {
 	case MSM_PARAM_COMM:
 	case MSM_PARAM_CMDLINE:
@@ -401,11 +403,11 @@ int adreno_set_param(struct msm_gpu *gpu, struct msm_file_private *ctx,
 		 * that should be a reasonable upper bound
 		 */
 		if (len > PAGE_SIZE)
-			return -EINVAL;
+			return UERR(EINVAL, drm, "invalid len");
 		break;
 	default:
 		if (len != 0)
-			return -EINVAL;
+			return UERR(EINVAL, drm, "invalid len");
 	}
 
 	switch (param) {
@@ -434,11 +436,10 @@ int adreno_set_param(struct msm_gpu *gpu, struct msm_file_private *ctx,
 	}
 	case MSM_PARAM_SYSPROF:
 		if (!capable(CAP_SYS_ADMIN))
-			return -EPERM;
+			return UERR(EPERM, drm, "invalid permissions");
 		return msm_file_private_set_sysprof(ctx, gpu, value);
 	default:
-		DBG("%s: invalid param: %u", gpu->name, param);
-		return -EINVAL;
+		return UERR(EINVAL, drm, "%s: invalid param: %u", gpu->name, param);
 	}
 }
 
drivers/gpu/drm/msm/msm_drv.c +1 -1
index 8c13b08708d2..6416d2cb4efc 100644
@@ -537,7 +537,7 @@ static int msm_ioctl_gem_info_set_iova(struct drm_device *dev,
 
 	/* Only supported if per-process address space is supported: */
 	if (priv->gpu->aspace == ctx->aspace)
-		return -EOPNOTSUPP;
+		return UERR(EOPNOTSUPP, dev, "requires per-process pgtables");
 
 	if (should_fail(&fail_gem_iova, obj->size))
 		return -ENOMEM;
drivers/gpu/drm/msm/msm_drv.h +7 -0
index 2e28a1344636..7fe0c67a602c 100644
@@ -28,6 +28,7 @@
 
 #include <drm/drm_atomic.h>
 #include <drm/drm_atomic_helper.h>
+#include <drm/drm_print.h>
 #include <drm/drm_probe_helper.h>
 #include <drm/display/drm_dsc.h>
 #include <drm/msm_drm.h>
@@ -519,6 +520,12 @@ void msm_hrtimer_work_init(struct msm_hrtimer_work *work,
 			   clockid_t clock_id,
 			   enum hrtimer_mode mode);
 
+/* Helper for returning a UABI error with optional logging which can make
+ * it easier for userspace to understand what it is doing wrong.
+ */
+#define UERR(err, drm, fmt, ...) \
+	({ DRM_DEV_DEBUG_DRIVER((drm)->dev, fmt, ##__VA_ARGS__); -(err); })
+
 #define DBG(fmt, ...) DRM_DEBUG_DRIVER(fmt"\n", ##__VA_ARGS__)
 #define VERB(fmt, ...) if (0) DRM_DEBUG_DRIVER(fmt"\n", ##__VA_ARGS__)
 
drivers/gpu/drm/msm/msm_gem_submit.c +27 -37

181 lines not on this page · raw message

Signature · 1 line
2.47.0
#1 lore raw
Quoted 8 lines · Rob Clark

From: Rob Clark <[email protected]>

Debugging incorrect UAPI usage tends to be a bit painful, so add a helper macro to make it easier to add debug logging which can be enabled at runtime via drm.debug.

---

[...]

drivers/gpu/drm/msm/msm_drv.h void msm_hrtimer_work_init(struct msm_hrtimer_work *work, · line 530

Quoted 8 lines
drivers/gpu/drm/msm/msm_drv.h +6 -0
+/* Helper for returning a UABI error with optional logging which can make
+ * it easier for userspace to understand what it is doing wrong.
+ */
+#define UERR(err, drm, fmt, ...) \
+	({ DRM_DEV_DEBUG_DRIVER((drm)->dev, fmt, ##__VA_ARGS__); -(err); })
+
 #define DBG(fmt, ...) DRM_DEBUG_DRIVER(fmt"\n", ##__VA_ARGS__)
 #define VERB(fmt, ...) if (0) DRM_DEBUG_DRIVER(fmt"\n", ##__VA_ARGS__)

I'm generally not a fan of adding driver-specific debug prints..

Maybe that's something that could be pushed to the drm-common layer or even deeper down the stack?

Konrad

author
#2 lore raw
Quoted 24 lines · Konrad Dybcio
Quoted 8 lines · Rob Clark

From: Rob Clark <[email protected]>

Debugging incorrect UAPI usage tends to be a bit painful, so add a helper macro to make it easier to add debug logging which can be enabled at runtime via drm.debug.

---

[...]

drivers/gpu/drm/msm/msm_drv.h void msm_hrtimer_work_init(struct msm_hrtimer_work *work, · line 530

Quoted 8 lines
drivers/gpu/drm/msm/msm_drv.h +6 -0
+/* Helper for returning a UABI error with optional logging which can make
+ * it easier for userspace to understand what it is doing wrong.
+ */
+#define UERR(err, drm, fmt, ...) \
+     ({ DRM_DEV_DEBUG_DRIVER((drm)->dev, fmt, ##__VA_ARGS__); -(err); })
+
 #define DBG(fmt, ...) DRM_DEBUG_DRIVER(fmt"\n", ##__VA_ARGS__)
 #define VERB(fmt, ...) if (0) DRM_DEBUG_DRIVER(fmt"\n", ##__VA_ARGS__)

I'm generally not a fan of adding driver-specific debug prints..

Quoted 2 lines

Maybe that's something that could be pushed to the drm-common layer or even deeper down the stack?

Even if we had something like DRM_DBG_UABI_ERROR() I'd probably still just #define UERR() to be a wrapper for it, since line length/wrapping tends to be a bit of a challenge. And I have a fairly substantial patch stack on top of this adding sparse/vm_bind support. (Debugging that was actually the motivation for this patch.)

I noticed that xe has something similar, but slightly different shape, in the form of XE_IOCTL_DBG().. but that kinda just moves the line length problem into the if() conditional. (And doesn't provide the benefit of being able to display the incorrect param.)

BR,
-R

#3 lore raw
Quoted 26 lines · Rob Clark
Quoted 24 lines · Konrad Dybcio
Quoted 8 lines · Rob Clark

From: Rob Clark <[email protected]>

Debugging incorrect UAPI usage tends to be a bit painful, so add a helper macro to make it easier to add debug logging which can be enabled at runtime via drm.debug.

---

[...]

drivers/gpu/drm/msm/msm_drv.h void msm_hrtimer_work_init(struct msm_hrtimer_work *work, · line 530

Quoted 8 lines
drivers/gpu/drm/msm/msm_drv.h +6 -0
+/* Helper for returning a UABI error with optional logging which can make
+ * it easier for userspace to understand what it is doing wrong.
+ */
+#define UERR(err, drm, fmt, ...) \
+     ({ DRM_DEV_DEBUG_DRIVER((drm)->dev, fmt, ##__VA_ARGS__); -(err); })
+
 #define DBG(fmt, ...) DRM_DEBUG_DRIVER(fmt"\n", ##__VA_ARGS__)
 #define VERB(fmt, ...) if (0) DRM_DEBUG_DRIVER(fmt"\n", ##__VA_ARGS__)

I'm generally not a fan of adding driver-specific debug prints..

Quoted 8 lines
Quoted 2 lines

Maybe that's something that could be pushed to the drm-common layer or even deeper down the stack?

Even if we had something like DRM_DBG_UABI_ERROR() I'd probably still just #define UERR() to be a wrapper for it, since line length/wrapping tends to be a bit of a challenge. And I have a fairly substantial patch stack on top of this adding sparse/vm_bind support. (Debugging that was actually the motivation for this patch.)

Alright, let's not get in the way then

Quoted 4 lines

I noticed that xe has something similar, but slightly different shape, in the form of XE_IOCTL_DBG().. but that kinda just moves the line length problem into the if() conditional. (And doesn't provide the benefit of being able to display the incorrect param.)

Maybe rust comes one day and the lines will start growing vertically ;)

Konrad

author
#4 lore raw
Quoted 37 lines · Konrad Dybcio
Quoted 26 lines · Rob Clark
Quoted 24 lines · Konrad Dybcio
Quoted 8 lines · Rob Clark

From: Rob Clark <[email protected]>

Debugging incorrect UAPI usage tends to be a bit painful, so add a helper macro to make it easier to add debug logging which can be enabled at runtime via drm.debug.

---

[...]

drivers/gpu/drm/msm/msm_drv.h void msm_hrtimer_work_init(struct msm_hrtimer_work *work, · line 530

Quoted 8 lines
drivers/gpu/drm/msm/msm_drv.h +6 -0
+/* Helper for returning a UABI error with optional logging which can make
+ * it easier for userspace to understand what it is doing wrong.
+ */
+#define UERR(err, drm, fmt, ...) \
+     ({ DRM_DEV_DEBUG_DRIVER((drm)->dev, fmt, ##__VA_ARGS__); -(err); })
+
 #define DBG(fmt, ...) DRM_DEBUG_DRIVER(fmt"\n", ##__VA_ARGS__)
 #define VERB(fmt, ...) if (0) DRM_DEBUG_DRIVER(fmt"\n", ##__VA_ARGS__)

I'm generally not a fan of adding driver-specific debug prints..

Quoted 8 lines
Quoted 2 lines

Maybe that's something that could be pushed to the drm-common layer or even deeper down the stack?

Even if we had something like DRM_DBG_UABI_ERROR() I'd probably still just #define UERR() to be a wrapper for it, since line length/wrapping tends to be a bit of a challenge. And I have a fairly substantial patch stack on top of this adding sparse/vm_bind support. (Debugging that was actually the motivation for this patch.)

Quoted 8 lines

Alright, let's not get in the way then

Quoted 4 lines

I noticed that xe has something similar, but slightly different shape, in the form of XE_IOCTL_DBG().. but that kinda just moves the line length problem into the if() conditional. (And doesn't provide the benefit of being able to display the incorrect param.)

Maybe rust comes one day and the lines will start growing vertically ;)

Rust for the userspace facing rendernode side of the driver, in particular, would be interesting for me, tbh. Especially if handle related rust<->c layers are designed properly. I've lost track of how many handle lifetime race condition UAF's I've seen ;-)

Re-writing entire drivers is a big lift, especially when there is so much hw+features to enable. KMS is limited to drm master (generally a somewhat privileged process), so less of a concern from a security standpoint. Much of the GPU side of things is "boring" power related stuff (suspend/resume/devfreq). But the rendernode ioctls are open to any process that can use the GPU in a typical setup.

BR,
-R

#5 lore raw
Quoted 53 lines · Rob Clark
Quoted 37 lines · Konrad Dybcio
Quoted 26 lines · Rob Clark
Quoted 24 lines · Konrad Dybcio
Quoted 8 lines · Rob Clark

From: Rob Clark <[email protected]>

Debugging incorrect UAPI usage tends to be a bit painful, so add a helper macro to make it easier to add debug logging which can be enabled at runtime via drm.debug.

---

[...]

drivers/gpu/drm/msm/msm_drv.h void msm_hrtimer_work_init(struct msm_hrtimer_work *work, · line 530

Quoted 8 lines
drivers/gpu/drm/msm/msm_drv.h +6 -0
+/* Helper for returning a UABI error with optional logging which can make
+ * it easier for userspace to understand what it is doing wrong.
+ */
+#define UERR(err, drm, fmt, ...) \
+     ({ DRM_DEV_DEBUG_DRIVER((drm)->dev, fmt, ##__VA_ARGS__); -(err); })
+
 #define DBG(fmt, ...) DRM_DEBUG_DRIVER(fmt"\n", ##__VA_ARGS__)
 #define VERB(fmt, ...) if (0) DRM_DEBUG_DRIVER(fmt"\n", ##__VA_ARGS__)

I'm generally not a fan of adding driver-specific debug prints..

Quoted 8 lines
Quoted 2 lines

Maybe that's something that could be pushed to the drm-common layer or even deeper down the stack?

Even if we had something like DRM_DBG_UABI_ERROR() I'd probably still just #define UERR() to be a wrapper for it, since line length/wrapping tends to be a bit of a challenge. And I have a fairly substantial patch stack on top of this adding sparse/vm_bind support. (Debugging that was actually the motivation for this patch.)

Quoted 8 lines

Alright, let's not get in the way then

Quoted 4 lines

I noticed that xe has something similar, but slightly different shape, in the form of XE_IOCTL_DBG().. but that kinda just moves the line length problem into the if() conditional. (And doesn't provide the benefit of being able to display the incorrect param.)

Maybe rust comes one day and the lines will start growing vertically ;)

Rust for the userspace facing rendernode side of the driver, in particular, would be interesting for me, tbh. Especially if handle related rust<->c layers are designed properly. I've lost track of how many handle lifetime race condition UAF's I've seen ;-)

Quoted 6 lines

Re-writing entire drivers is a big lift, especially when there is so much hw+features to enable. KMS is limited to drm master (generally a somewhat privileged process), so less of a concern from a security standpoint. Much of the GPU side of things is "boring" power related stuff (suspend/resume/devfreq). But the rendernode ioctls are open to any process that can use the GPU in a typical setup.

The boring part would benefit greatly from automatic scope exit cleanup.. We've had lots of issues in the past with that (that are hopefully? sorted out now, or should I say, for now)

Konrad

author
#6 lore raw
Quoted 62 lines · Konrad Dybcio
Quoted 53 lines · Rob Clark
Quoted 37 lines · Konrad Dybcio
Quoted 26 lines · Rob Clark
Quoted 24 lines · Konrad Dybcio
Quoted 8 lines · Rob Clark

From: Rob Clark <[email protected]>

Debugging incorrect UAPI usage tends to be a bit painful, so add a helper macro to make it easier to add debug logging which can be enabled at runtime via drm.debug.

---

[...]

drivers/gpu/drm/msm/msm_drv.h void msm_hrtimer_work_init(struct msm_hrtimer_work *work, · line 530

Quoted 8 lines
drivers/gpu/drm/msm/msm_drv.h +6 -0
+/* Helper for returning a UABI error with optional logging which can make
+ * it easier for userspace to understand what it is doing wrong.
+ */
+#define UERR(err, drm, fmt, ...) \
+     ({ DRM_DEV_DEBUG_DRIVER((drm)->dev, fmt, ##__VA_ARGS__); -(err); })
+
 #define DBG(fmt, ...) DRM_DEBUG_DRIVER(fmt"\n", ##__VA_ARGS__)
 #define VERB(fmt, ...) if (0) DRM_DEBUG_DRIVER(fmt"\n", ##__VA_ARGS__)

I'm generally not a fan of adding driver-specific debug prints..

Quoted 8 lines
Quoted 2 lines

Maybe that's something that could be pushed to the drm-common layer or even deeper down the stack?

Even if we had something like DRM_DBG_UABI_ERROR() I'd probably still just #define UERR() to be a wrapper for it, since line length/wrapping tends to be a bit of a challenge. And I have a fairly substantial patch stack on top of this adding sparse/vm_bind support. (Debugging that was actually the motivation for this patch.)

Quoted 8 lines

Alright, let's not get in the way then

Quoted 4 lines

I noticed that xe has something similar, but slightly different shape, in the form of XE_IOCTL_DBG().. but that kinda just moves the line length problem into the if() conditional. (And doesn't provide the benefit of being able to display the incorrect param.)

Maybe rust comes one day and the lines will start growing vertically ;)

Rust for the userspace facing rendernode side of the driver, in particular, would be interesting for me, tbh. Especially if handle related rust<->c layers are designed properly. I've lost track of how many handle lifetime race condition UAF's I've seen ;-)

Quoted 6 lines

Re-writing entire drivers is a big lift, especially when there is so much hw+features to enable. KMS is limited to drm master (generally a somewhat privileged process), so less of a concern from a security standpoint. Much of the GPU side of things is "boring" power related stuff (suspend/resume/devfreq). But the rendernode ioctls are open to any process that can use the GPU in a typical setup.

Quoted 3 lines

The boring part would benefit greatly from automatic scope exit cleanup.. We've had lots of issues in the past with that (that are hopefully? sorted out now, or should I say, for now)

Maybe some of the cleanup.h stuff would be useful?

BR,
-R

#7 lore raw
Quoted 64 lines · Rob Clark
Quoted 62 lines · Konrad Dybcio
Quoted 59 lines · Rob Clark
Quoted 37 lines · Konrad Dybcio
Quoted 26 lines · Rob Clark
Quoted 24 lines · Konrad Dybcio
Quoted 8 lines · Rob Clark

From: Rob Clark <[email protected]>

Debugging incorrect UAPI usage tends to be a bit painful, so add a helper macro to make it easier to add debug logging which can be enabled at runtime via drm.debug.

---

[...]

Review

Quoted 8 lines
(quoted diff) +6 -0
+/* Helper for returning a UABI error with optional logging which can make
+ * it easier for userspace to understand what it is doing wrong.
+ */
+#define UERR(err, drm, fmt, ...) \
+     ({ DRM_DEV_DEBUG_DRIVER((drm)->dev, fmt, ##__VA_ARGS__); -(err); })
+
 #define DBG(fmt, ...) DRM_DEBUG_DRIVER(fmt"\n", ##__VA_ARGS__)
 #define VERB(fmt, ...) if (0) DRM_DEBUG_DRIVER(fmt"\n", ##__VA_ARGS__)

I'm generally not a fan of adding driver-specific debug prints..

Quoted 8 lines
Quoted 2 lines

Maybe that's something that could be pushed to the drm-common layer or even deeper down the stack?

Even if we had something like DRM_DBG_UABI_ERROR() I'd probably still just #define UERR() to be a wrapper for it, since line length/wrapping tends to be a bit of a challenge. And I have a fairly substantial patch stack on top of this adding sparse/vm_bind support. (Debugging that was actually the motivation for this patch.)

Quoted 8 lines

Alright, let's not get in the way then

Quoted 4 lines

I noticed that xe has something similar, but slightly different shape, in the form of XE_IOCTL_DBG().. but that kinda just moves the line length problem into the if() conditional. (And doesn't provide the benefit of being able to display the incorrect param.)

Maybe rust comes one day and the lines will start growing vertically ;)

Rust for the userspace facing rendernode side of the driver, in particular, would be interesting for me, tbh. Especially if handle related rust<->c layers are designed properly. I've lost track of how many handle lifetime race condition UAF's I've seen ;-)

Re-writing entire drivers is a big lift, especially when there is so much hw+features to enable. KMS is limited to drm master (generally a somewhat privileged process), so less of a concern from a security standpoint. Much of the GPU side of things is "boring" power related stuff (suspend/resume/devfreq). But the rendernode ioctls are open to any process that can use the GPU in a typical setup.

Quoted 5 lines
Quoted 3 lines

The boring part would benefit greatly from automatic scope exit cleanup.. We've had lots of issues in the past with that (that are hopefully? sorted out now, or should I say, for now)

Maybe some of the cleanup.h stuff would be useful?

Very possibly.

Though the main issue is that we're juggling two "real" power rails and two GDSCs that hang off them (with GX being juggled from both AP and GPU/GMU PoV), and it's simply confusing for the programmer..

I'd rather delay that until some next great cleanup (tm)

Konrad

#8 lore raw
In reply to Rob Clark
Quoted 35 lines · Rob Clark
Quoted 24 lines · Konrad Dybcio
Quoted 8 lines · Rob Clark

From: Rob Clark <[email protected]>

Debugging incorrect UAPI usage tends to be a bit painful, so add a helper macro to make it easier to add debug logging which can be enabled at runtime via drm.debug.

---

[...]

drivers/gpu/drm/msm/msm_drv.h void msm_hrtimer_work_init(struct msm_hrtimer_work *work, · line 530

Quoted 8 lines
drivers/gpu/drm/msm/msm_drv.h +6 -0
+/* Helper for returning a UABI error with optional logging which can make
+ * it easier for userspace to understand what it is doing wrong.
+ */
+#define UERR(err, drm, fmt, ...) \
+     ({ DRM_DEV_DEBUG_DRIVER((drm)->dev, fmt, ##__VA_ARGS__); -(err); })
+
  #define DBG(fmt, ...) DRM_DEBUG_DRIVER(fmt"\n", ##__VA_ARGS__)
  #define VERB(fmt, ...) if (0) DRM_DEBUG_DRIVER(fmt"\n", ##__VA_ARGS__)

I'm generally not a fan of adding driver-specific debug prints..

Quoted 2 lines

Maybe that's something that could be pushed to the drm-common layer or even deeper down the stack?

Even if we had something like DRM_DBG_UABI_ERROR() I'd probably still just #define UERR() to be a wrapper for it, since line length/wrapping tends to be a bit of a challenge. And I have a fairly substantial patch stack on top of this adding sparse/vm_bind support. (Debugging that was actually the motivation for this patch.)

Quoted 4 lines

I noticed that xe has something similar, but slightly different shape, in the form of XE_IOCTL_DBG().. but that kinda just moves the line length problem into the if() conditional. (And doesn't provide the benefit of being able to display the incorrect param.)

FWIW there is also a debug only builds hack in i915:

/* Catch emission of unexpected errors for CI! */
#if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)
#undef EINVAL

#define EINVAL ({ \
	DRM_DEBUG_DRIVER("EINVAL at %s:%d\n", __func__, __LINE__); \
	22; \
})

#endif

UERR functionality wise looks quite good to me. Better than the other two. The name is not scoped but I appreciate the readability line length challenges.

Regards,

Tvrtko