Re: [PATCH v8 5/6] rust: ww_mutex: add Mutex, AcquireCtx and MutexGuard

From: Alice Ryhl

Date: Thu Dec 04 2025 - 08:33:11 EST


On Thu, Dec 04, 2025 at 07:26:25AM -0600, Daniel Almeida wrote:
>
>
> > On 4 Dec 2025, at 03:07, Alice Ryhl <aliceryhl@xxxxxxxxxx> wrote:
> >
> > On Wed, Dec 03, 2025 at 02:23:14PM -0300, Daniel Almeida wrote:
> >>
> >>
> >>> On 3 Dec 2025, at 10:26, Alice Ryhl <aliceryhl@xxxxxxxxxx> wrote:
> >>>
> >>> On Mon, Dec 01, 2025 at 01:28:54PM +0300, Onur Özkan wrote:
> >>>> Yeah :(. We could get rid of them easily by keeping the class that was
> >>>> passed to the constructor functions but that becomes a problem for the
> >>>> from_raw implementations.
> >>>>
> >>>> I think the best solution would be to expose ww_class type from
> >>>> ww_acquire_ctx and ww_mutex unconditionally (right now it depends on
> >>>> DEBUG_WW_MUTEXES). That way we can just access the class and verify
> >>>> that the mutex and acquire_ctx classes match.
> >>>>
> >>>> What do you think? I can submit a patch for the C-side implementation.
> >>>> It should be straightforward and shouldn't have any runtime impact.
> >>>
> >>> I think there is a better solution. We can create a different type for
> >>> every single class, like how rust/kernel/sync/lock/global.rs creates a
> >>> different type for every single mutex. Then, you know that the classes
> >>> are the same since the class is part of the type.
> >>
> >> I don’t think this would work with the from_raw() functions. What class
> >> would you assign then? I think this is precisely what sparked the current
> >> solution.
> >
> > There can be a way to create a type for a C-defined class, and
>
> That’s the problem, if we don’t have patch 2, we don’t know the class.
>
> What you’re suggesting seems unimplementable to me at first. Otherwise, can
> you expand some more?

For each class defined by C code, you invoke a macro:

ww_class_from_c_code(MY_C_CLASS, bindings::my_c_class);

Then when you call from_raw(), you call

Mutex::<T, MY_C_CLASS>::from_raw(ptr_to_mutex)

There is no need for a check, because from_raw() is unsafe.

Alice