On Fri, Sep 26, 2025 at 10:48:06AM +0200, Philipp Stanner wrote:
[..]
> > >
> > > Regarding the manually created spinlock of mine: I so far never need
> > > that spinlock anywhere in Rust and wasn't sure what's then the best way
> > > to pass a "raw" spinlock to C.
> > >
> >
> > You can use `SpinLock<()>` for this purpose, no need to add new
> > bindings.
>
> The dma_fence C backend needs a spinlock pointer, given to it by the
> driver (so Rust code).
>
> How do I pass a SpinLock<()> to a C function? AFAICS SpinLock doesn't
> implement as_raw(), so I'd have to implement it, wouldn't I?
>
Technically you can use a `&raw` to get the address of the spinlock and
just use it, since `SpinLock<()>` is `repr(C)`, so it's transparent to a
spinlock_t. So you don't have to ;-)
However, while we are at it, it makes sense that we do it right.
> Or rather, as it looks, I'd have to implement it for SpinLockBackend?
>
I think the below should work, we already have a special from_raw() for
Lock<()>, it makes sense to have a special as_raw() as well.
Let me know if you want me to send a proper patch, or feel free to add
one in your patchset ;-)
diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
index 27202beef90c..78f3287a1372 100644
--- a/rust/kernel/sync/lock.rs
+++ b/rust/kernel/sync/lock.rs
@@ -160,6 +160,13 @@ pub unsafe fn from_raw<'a>(ptr: *mut B::State) -> &'a Self
{
// `B::State`.
unsafe { &*ptr.cast() }
}
+
+ /// Obtains the raw pointer from a [`Lock`].
+ ///
+ /// This can be useful for working with a lock user outside Rust.
+ pub fn as_raw(&self) -> *mut B::State {
+ self.state.get()
+ }
}
impl<T: ?Sized, B: Backend> Lock<T, B> {
Regards,
Boqun
>
> P.
>
> >
> > [1]:
> > https://lore.kernel.org/rust-for-linux/[email protected]/
> >
> > Regards,
> > Boqun
> >
> > >
> > > So much from my side. Hope to hear from you.
> > >
> > > (I've compiled and tested this with the unit test on the current -rc3)
> > >
> > > Philipp
> > > ---
> > [...]
>