KMS LOCKING(9)
π kms locking(9) β a Kernel API Manual moment
okay so bestie, real talk: this is the kms locking page from the Kernel API Manual, and it's basically the group chat of KMS locking drama, so let's get into it β¨
πΈ kms locking
So here's the vibe: KMS is moving toward more fine grained locking, plus there's atomic ioctl where userspace can indirectly control locking order π and honestly? That means it becomes necessary to use ww_mutex and acquire-contexts to avoid deadlocks. No cap, deadlocks are the enemy.
But since the locking is way more distributed around the driver code, we want a little extra utility/tracking out of our acquire-ctx. That's literally what these two icons provide:
struct drm_modeset_lockstruct drm_modeset_acquire_ctx
For the basic principles ofww_mutex, go read:Documentation/locking/ww-mutex-design.rstπ«Ά
β¨ the basic usage pattern β¨
The basic usage pattern is to:
drm_modeset_acquire_init(ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE)
retry:
foreach (lock in random_ordered_set_of_locks) {
ret = drm_modeset_lock(lock, ctx)
if (ret == -EDEADLK) {
ret = drm_modeset_backoff(ctx);
if (!ret)
goto retry;
}
if (ret)
goto out;
}
... do stuff ...
out:
drm_modeset_drop_locks(ctx);
drm_modeset_acquire_fini(ctx);
For convenience (because we're nice like that π) this whole control flow is implemented in DRM_MODESET_LOCK_ALL_BEGIN and DRM_MODESET_LOCK_ALL_END, for the case where all modeset locks need to be taken through drm_modeset_lock_all_ctx.
π¦ just one lock? we got you
If all that's needed is a single modeset lock</>, then the struct drm_modeset_acquire_ctx is not needed, and the locking can be simplified by passing a NULL instead of ctx in the drm_modeset_lock call, or by calling drm_modeset_lock_single_interruptible. To unlock afterwards, call drm_modeset_unlock. Periodt. π
π and on top of all that...
On top of these per-object locks using ww_mutex, there's also an overall drm_mode_config.mutex, for protecting everything else. Mostly this means:
- probe state of connectors
- preventing hotplug add/removal of connectors
Finally, there's a bunch of dedicated locks to protect drm core internal lists and lookup data structures. iykyk π
β SEE ALSO β
- Kernel file
./drivers/gpu/drm/drm_modeset_lock.c drm_modeset_lock_all(9)drm_modeset_unlock_all(9)drm_warn_on_modeset_not_all_locked(9)drm_modeset_acquire_init(9)drm_modeset_acquire_fini(9)drm_modeset_drop_locks(9)drm_modeset_backoff(9)drm_modeset_lock_init(9)drm_modeset_lock(9)drm_modeset_lock_single_interruptible(9)drm_modeset_unlock(9)drm_modeset_lock_all_ctx(9)
π and that's the whole lore, bestie β this one lives at ./drivers/gpu/drm, dated October 2026. go forth and lock responsibly, mwah π
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms