KMS LOCKING(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’… 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_lock
  • struct drm_modeset_acquire_ctx
For the basic principles of ww_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 ⭐


πŸŽ€ 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