Re: [PATCH] rust: mm: add abstractions for mm_struct and vm_area_struct

From: Alice Ryhl
Date: Fri Jul 26 2024 - 04:15:14 EST


On Fri, Jul 26, 2024 at 10:11 AM Benno Lossin <benno.lossin@xxxxxxxxx> wrote:
>
> On 23.07.24 16:32, Alice Ryhl wrote:
> > +pub struct MmGet {
> > + mm: NonNull<bindings::mm_struct>,
> > +}
> > +
> > +impl MmGet {
> > + /// Lock the mmap read lock.
> > + #[inline]
> > + pub fn mmap_write_lock(&self) -> MmapWriteLock<'_> {
> > + // SAFETY: The pointer is valid since we hold a refcount.
> > + unsafe { bindings::mmap_write_lock(self.mm.as_ptr()) };
> > +
> > + // INVARIANT: We just acquired the write lock, so we can transfer to this guard.
> > + //
> > + // The signature of this function ensures that the `MmapWriteLock` will not outlive this
> > + // `mmget` refcount.
> > + MmapWriteLock {
> > + mm: self.mm,
> > + _lifetime: PhantomData,
> > + }
> > + }
> > +
> > + /// When dropping this refcount, use `mmput_async` instead of `mmput`.
>
> I don't get this comment.

The C side provides two ways to decrement the mmget refcount. One is
mmput and the other is mmput_async. The difference is that when the
refcount hits zero, mmput_async cleans up the mm_struct on the
workqueue, whereas mmput cleans it up immediately. This means that
mmput_async is safe in atomic context, but mmput is not.

Alice