drm/modeset_lock: add NULL check for ctx before WARN_ON
Subsystems (2)
PATCH 1/1 drm/modeset_lock: add NULL check for ctx before WARN_ON
From: George D Sworo <[email protected]>
modeset_lock() and drm_modeset_drop_locks() do not validate the ctx pointer before dereferencing it in WARN_ON(ctx->contended), which can lead to a NULL pointer dereference if ctx is NULL.
Add a NULL check to prevent this.
--- drivers/gpu/drm/drm_modeset_lock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/drm_modeset_lock.c +1 -1
@@ -295,7 +295,7 @@ static inline int modeset_lock(struct drm_modeset_lock *lock, {int ret; -if (WARN_ON (ctx->contended)) +if (ctx &&WARN_ON (ctx->contended))__drm_stack_depot_print (ctx->stack_depot);if (ctx->trylock_only) {
Signature · 1 line
2.34.1
v2 cover drm/i915/modeset: fix NULL/ctx handling in lock paths
From: George D Sworo <[email protected]>
Hi,
This series addresses lock-context robustness seen in HDCP/MST paths, where modeset locking can be reached with invalid or stale acquire_ctx state and trigger WARN/Oops in modeset_lock().
Patch 1 keeps the original fix from v1.
Patch 2 adds a defensive fix for the additional ctx handling issue.
Changes in v2:
- Added patch 2 to handle NULL ctx defensively in modeset lock helper path.
- Kept patch 1 from v1 (no functional change) for complete series resend.
- Updated commit messages for clearer rationale and call-path context.
- Linked previous posting:
https://patchwork.freedesktop.org/patch/721791/
Patch overview:
drm/modeset_lock: add NULL check for ctx before WARN_ON drm/modeset: harden modeset_lock() against NULL ctx
drivers/gpu/drm/drm_modeset_lock.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
Signature · 1 line
2.34.1
PATCH v2 1/2 drm/modeset_lock: add NULL check for ctx before WARN_ON
From: George D Sworo <[email protected]>
modeset_lock() and drm_modeset_drop_locks() do not validate the ctx pointer before dereferencing it in WARN_ON(ctx->contended), which can lead to a NULL pointer dereference if ctx is NULL.
Add a NULL check to prevent this.
--- drivers/gpu/drm/drm_modeset_lock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/drm_modeset_lock.c +1 -1
@@ -295,7 +295,7 @@ static inline int modeset_lock(struct drm_modeset_lock *lock, {int ret; -if (WARN_ON (ctx->contended)) +if (ctx &&WARN_ON (ctx->contended))__drm_stack_depot_print (ctx->stack_depot);if (ctx->trylock_only) {
Signature · 1 line
2.34.1
Quoted 7 lines · On Tue, Apr 28, 2026 at 11:04:30PM -0700, [email protected] wrote:
From: George D Sworo <[email protected]>
modeset_lock()anddrm_modeset_drop_locks()do not validate the ctx pointer before dereferencing it inWARN_ON(ctx->contended), which can lead to a NULL pointer dereference if ctx is NULL.Add a NULL check to prevent this.
Why are you trying to pass garbage into the function?
Quoted 21 lines
Signed-off-by George D Sworo <[email protected]>--- drivers/gpu/drm/drm_modeset_lock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/gpu/drm/drm_modeset_lock.c +1
-1@@ -295,7 +295,7 @@ static inline int modeset_lock(struct drm_modeset_lock *lock, {int ret; -if (WARN_ON (ctx->contended)) +if (ctx &&WARN_ON (ctx->contended))__drm_stack_depot_print (ctx->stack_depot);if (ctx->trylock_only) {Signature · 1 line
2.34.1
Signature · 2 lines
Ville Syrjälä Intel
Thanks Ville for the review!
Fair point. Ideally, we shouldn't be calling modeset_lock() with NULL ctx but during a recent debug we uncovered an issue where the system kept printing the calltrace and I figured this was one instance where ctx is being dereferenced without a guard
03-26 11:33:03.261 0 0 F BUG : kernel NULL pointer
dereference, address: 0000000000000069
03-26 11:33:03.261 0 0 F #PF : supervisor read access in
kernel mode
03-26 11:33:03.261 0 0 F #PF : error_code(0x0000) - not-
present page
03-26 11:33:03.261 0 0 I : PGD 0 P4D 0 03-26 11:33:03.261 0 0 W Oops : Oops: 0000 [#1] PREEMPT SMP
NOPTI
03-26 11:33:03.261 0 0 W Tainted : [U]=USER, [W]=WARN,
[O]=OOT_MODULE
03-26 11:33:03.261 0 0 W Workqueue: i915_flipintel_atomic_commit_work [xe]
03-26 11:33:03.262 0 0 W RIP : 0010:modeset_lock+0x74/0xd0
Quoted 36 lines · Ville Syrjälä
Quoted 7 lines · On Tue, Apr 28, 2026 at 11:04:30PM -0700, [email protected] wrote:
From: George D Sworo <[email protected]>
modeset_lock()anddrm_modeset_drop_locks()do not validate the ctx pointer before dereferencing it inWARN_ON(ctx->contended), which can lead to a NULL pointer dereference if ctx is NULL.Add a NULL check to prevent this.
Why are you trying to pass garbage into the function?
Quoted 23 lines
Signed-off-by George D Sworo <[email protected]>---
drivers/gpu/drm/drm_modeset_lock.c | 2 +-1 file changed, 1 insertion(+), 1 deletion(-)drivers/gpu/drm/drm_modeset_lock.c +0
-0@@ -295,7 +295,7 @@ static inline int modeset_lock(structdrm_modeset_lock *lock, { int ret;drivers/gpu/drm/drm_modeset_lock.c +1
-1-if (WARN_ON (ctx->contended)) +if (ctx &&WARN_ON (ctx->contended))
__drm_stack_depot_print(ctx->stack_depot);
if (ctx->trylock_only) {Signature · 1 line
2.34.1
PATCH v2 2/2 drm/modeset: harden modeset_lock() against NULL ctx
In reply to [email protected]From: George D Sworo <[email protected]>
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.
--- drivers/gpu/drm/drm_modeset_lock.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
drivers/gpu/drm/drm_modeset_lock.c +12 -0
@@ -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);
Signature · 1 line
2.34.1