]> git.ipfire.org Git - thirdparty/kernel/linux.git/commit
rust: sync: Add Lock::from_raw() for Lock<(), B>
authorLyude Paul <lyude@redhat.com>
Tue, 19 Nov 2024 23:11:03 +0000 (18:11 -0500)
committerBoqun Feng <boqun.feng@gmail.com>
Thu, 19 Dec 2024 22:04:42 +0000 (14:04 -0800)
commit15abc88057eeec052aefde897df277eca2340ac6
tree7a009439ae9a26aab4467a951a18dd6d1def5a0e
parent9793c9bb91f1b05473bb6d4a2323a259ef00ff2e
rust: sync: Add Lock::from_raw() for Lock<(), B>

The KMS bindings [1] have a few bindings that require manually acquiring
specific locks before calling certain functions. At the moment though,
the only way of acquiring these locks in bindings is to simply call the
C locking functions directly - since said locks are not initialized on
the Rust side of things.

However - if we add `#[repr(C)]` to `Lock<(), B>`, then given `()` is a
ZST - `Lock<(), B>` becomes equivalent in data layout to its inner
`B::State` type. Since locks in C don't have data explicitly associated
with them anyway, we can take advantage of this to add a
`Lock::from_raw()` function that can translate a raw pointer to
`B::State` into its proper `Lock<(), B>` equivalent. This lets us simply
acquire a reference to the lock in question and work with it like it was
initialized on the Rust side of things, allowing us to use less unsafe
code to implement bindings with lock requirements.

[Boqun: Use "Link:" instead of a URL and format the commit log]

Signed-off-by: Lyude Paul <lyude@redhat.com>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Link: https://patchwork.freedesktop.org/series/131522/
Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
Link: https://lore.kernel.org/r/20241119231146.2298971-2-lyude@redhat.com
rust/kernel/sync/lock.rs