Re: [PATCH v3] rust: mm: add abstractions for mm_struct and vm_area_struct
From: Danilo Krummrich
Date: Thu Aug 01 2024 - 10:02:43 EST
On Thu, Aug 01, 2024 at 12:58:45PM +0000, Alice Ryhl wrote:
> This is a follow-up to the page abstractions [1] that were recently
> merged in 6.11. Rust Binder will need these abstractions to manipulate
> the vma in its implementation of the mmap fop on the Binder file.
>
> This patch is based on Wedson's implementation on the old rust branch,
> but has been changed significantly. All mistakes are Alice's.
>
> Link: https://lore.kernel.org/r/20240528-alice-mm-v7-4-78222c31b8f4@xxxxxxxxxx [1]
> Co-developed-by: Wedson Almeida Filho <wedsonaf@xxxxxxxxx>
> Signed-off-by: Wedson Almeida Filho <wedsonaf@xxxxxxxxx>
> Signed-off-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
> ---
> Changes in v3:
> - Reorder entries in mm.rs.
> - Use ARef for mmput_async helper.
> - Clarify that VmArea requires you to hold the mmap read or write lock.
> - Link to v2: https://lore.kernel.org/r/20240727-vma-v2-1-ab3e5927dc3a@xxxxxxxxxx
>
> Changes in v2:
> - mm.rs is redesigned from scratch making use of AsRef
> - Add notes about whether destructors may sleep
> - Rename Area to VmArea
> - Link to v1: https://lore.kernel.org/r/20240723-vma-v1-1-32ad5a0118ee@xxxxxxxxxx
> ---
> rust/helpers.c | 61 +++++++++
> rust/kernel/lib.rs | 1 +
> rust/kernel/mm.rs | 337 +++++++++++++++++++++++++++++++++++++++++++++++++
> rust/kernel/mm/virt.rs | 204 ++++++++++++++++++++++++++++++
> rust/kernel/types.rs | 9 ++
> 5 files changed, 612 insertions(+)
>
> diff --git a/rust/kernel/mm.rs b/rust/kernel/mm.rs
> new file mode 100644
> index 000000000000..ed2db893fb79
> --- /dev/null
> +++ b/rust/kernel/mm.rs
> @@ -0,0 +1,337 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +// Copyright (C) 2024 Google LLC.
> +
> +//! Memory management.
> +//!
> +//! C header: [`include/linux/mm.h`](../../../../include/linux/mm.h)
NIT: srctree
> +
> + /// Returns a raw pointer to the inner `mm_struct`.
> + #[inline]
> + pub fn as_raw(&self) -> *mut bindings::mm_struct {
> + self.mm.get()
> + }
> +
> + /// Obtain a reference from a raw pointer.
> + ///
> + /// # Safety
> + ///
> + /// The caller must ensure that `ptr` points at an `mm_struct`, and that it is not deallocated
> + /// during the lifetime 'a.
> + #[inline]
> + pub unsafe fn from_raw_mm<'a>(ptr: *const bindings::mm_struct) -> &'a Mm {
I'd just call this `from_raw`, like you call the counterpart `as_raw` above.
Same goes for `MmWithUser` and `VmArea`.
> diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
> index bd189d646adb..143a2bf06941 100644
> --- a/rust/kernel/types.rs
> +++ b/rust/kernel/types.rs
> @@ -366,6 +366,15 @@ pub unsafe fn from_raw(ptr: NonNull<T>) -> Self {
> _p: PhantomData,
> }
> }
> +
> + /// Pass ownership of the refcount to a raw pointer.
> + pub fn into_raw(self) -> NonNull<T> {
> + let ptr = self.ptr;
> + // Skip the destructor.
> + core::mem::forget(self);
> +
> + ptr
> + }
I think this should be a separate patch.
> }
>
> impl<T: AlwaysRefCounted> Clone for ARef<T> {
>
> ---
> base-commit: 8400291e289ee6b2bf9779ff1c83a291501f017b
> change-id: 20240723-vma-f80119f9fb35
>
> Best regards,
> --
> Alice Ryhl <aliceryhl@xxxxxxxxxx>
>
>