// SPDX-License-Identifier: GPL-2.012// Copyright (C) 2024 Google LLC.34//! Version of `MmWithUser` using `mmput_async`.5//!6//! This is a separate file from `mm.rs` due to the dependency on `CONFIG_MMU=y`.7#![cfg(CONFIG_MMU)]89use crate::{10bindings,11mm::MmWithUser,12sync::aref::{ARef, AlwaysRefCounted},13};14use core::{ops::Deref, ptr::NonNull};1516/// A wrapper for the kernel's `struct mm_struct`.17///18/// This type is identical to `MmWithUser` except that it uses `mmput_async` when dropping a19/// refcount. This means that the destructor of `ARef<MmWithUserAsync>` is safe to call in atomic20/// context.21///22/// # Invariants23///24/// Values of this type are always refcounted using `mmget`. The value of `mm_users` is non-zero.25#[repr(transparent)]26pub struct MmWithUserAsync {27mm: MmWithUser,28}2930// SAFETY: It is safe to call `mmput_async` on another thread than where `mmget` was called.31unsafe impl Send for MmWithUserAsync {}32// SAFETY: All methods on `MmWithUserAsync` can be called in parallel from several threads.33unsafe impl Sync for MmWithUserAsync {}3435// SAFETY: By the type invariants, this type is always refcounted.36unsafe impl AlwaysRefCounted for MmWithUserAsync {37#[inline]38fn inc_ref(&self) {39// SAFETY: The pointer is valid since self is a reference.40unsafe { bindings::mmget(self.as_raw()) };41}4243#[inline]44unsafe fn dec_ref(obj: NonNull<Self>) {45// SAFETY: The caller is giving up their refcount.46unsafe { bindings::mmput_async(obj.cast().as_ptr()) };47}48}4950// Make all `MmWithUser` methods available on `MmWithUserAsync`.51impl Deref for MmWithUserAsync {52type Target = MmWithUser;5354#[inline]55fn deref(&self) -> &MmWithUser {56&self.mm57}58}5960impl MmWithUser {61/// Use `mmput_async` when dropping this refcount.62#[inline]63pub fn into_mmput_async(me: ARef<MmWithUser>) -> ARef<MmWithUserAsync> {64// SAFETY: The layouts and invariants are compatible.65unsafe { ARef::from_raw(ARef::into_raw(me).cast()) }66}67}686970