[PATCH v2 2/2] drm/modeset: harden modeset_lock() against NULL ctx

From: george . d . sworo

Date: Wed Apr 29 2026 - 02:05:34 EST


From: George D Sworo <george.d.sworo@xxxxxxxxx>

modeset_lock() dereferences ctx unconditionally. Add a defensive NULL
guard to avoid NULL dereference if a buggy internal caller passes a NULL
acquire context.

For NULL ctx, fall back to plain ww_mutex locking semantics:
- interruptible path uses ww_mutex_lock_interruptible(..., NULL)
- non-interruptible path uses ww_mutex_lock(..., NULL)

This keeps wait behavior consistent with the helper arguments.

Signed-off-by: George D Sworo <george.d.sworo@xxxxxxxxx>
---
drivers/gpu/drm/drm_modeset_lock.c | 12 ++++++++++++
1 file changed, 12 insertions(+)

diff --git a/drivers/gpu/drm/drm_modeset_lock.c b/drivers/gpu/drm/drm_modeset_lock.c
index 2052bb9bb9e5..5bee424805c3 100644
--- a/drivers/gpu/drm/drm_modeset_lock.c
+++ b/drivers/gpu/drm/drm_modeset_lock.c
@@ -294,6 +294,18 @@ static inline int modeset_lock(struct drm_modeset_lock *lock,
bool interruptible, bool slow)
{
int ret;
+ /*
+ * Defensive fallback: this helper is expected to be called with a
+ * valid acquire context, but if a NULL ctx slips through, preserve
+ * the lock wait semantics and avoid NULL dereference.
+ */
+ if (unlikely(!ctx)) {
+ if (interruptible)
+ return ww_mutex_lock_interruptible(&lock->mutex, NULL);
+
+ ww_mutex_lock(&lock->mutex, NULL);
+ return 0;
+ }

if (ctx && WARN_ON(ctx->contended))
__drm_stack_depot_print(ctx->stack_depot);
--
2.34.1