linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: Alice Ryhl <aliceryhl@google.com>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
	"Matthew Wilcox" <willy@infradead.org>,
	"Lorenzo Stoakes" <lorenzo.stoakes@oracle.com>,
	"Vlastimil Babka" <vbabka@suse.cz>,
	"John Hubbard" <jhubbard@nvidia.com>,
	"Liam R. Howlett" <Liam.Howlett@oracle.com>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Arnd Bergmann" <arnd@arndb.de>, "Jann Horn" <jannh@google.com>,
	"Suren Baghdasaryan" <surenb@google.com>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	linux-kernel@vger.kernel.org, linux-mm@kvack.org,
	rust-for-linux@vger.kernel.org
Subject: Re: [PATCH v16 8/9] task: rust: rework how current is accessed
Date: Wed, 7 May 2025 09:55:29 -0700	[thread overview]
Message-ID: <aBuQgY4wMv1CsVdo@Mac.home> (raw)
In-Reply-To: <aBuOX0hTLZed3JND@Mac.home>

On Wed, May 07, 2025 at 09:46:23AM -0700, Boqun Feng wrote:
> On Wed, May 07, 2025 at 09:06:01AM -0700, Boqun Feng wrote:
> [...]
> > > +impl CurrentTask {
> > > +    /// Access the address space of the current task.
> > > +    ///
> > > +    /// This function does not touch the refcount of the mm.
> > > +    #[inline]
> > > +    pub fn mm(&self) -> Option<&MmWithUser> {
> > > +        // SAFETY: The `mm` field of `current` is not modified from other threads, so reading it is
> > > +        // not a data race.
> > > +        let mm = unsafe { (*self.as_ptr()).mm };
> > > +
> > > +        if mm.is_null() {
> > > +            return None;
> > > +        }
> > > +
> > > +        // SAFETY: If `current->mm` is non-null, then it references a valid mm with a non-zero
> > > +        // value of `mm_users`. Furthermore, the returned `&MmWithUser` borrows from this
> > > +        // `CurrentTask`, so it cannot escape the scope in which the current pointer was obtained.
> > > +        //
> > > +        // This is safe even if `kthread_use_mm()`/`kthread_unuse_mm()` are used. There are two
> > > +        // relevant cases:
> > > +        // * If the `&CurrentTask` was created before `kthread_use_mm()`, then it cannot be
> > > +        //   accessed during the `kthread_use_mm()`/`kthread_unuse_mm()` scope due to the
> > > +        //   `NotThreadSafe` field of `CurrentTask`.
> > > +        // * If the `&CurrentTask` was created within a `kthread_use_mm()`/`kthread_unuse_mm()`
> > > +        //   scope, then the `&CurrentTask` cannot escape that scope, so the returned `&MmWithUser`
> > > +        //   also cannot escape that scope.
> > > +        // In either case, it's not possible to read `current->mm` and keep using it after the
> > > +        // scope is ended with `kthread_unuse_mm()`.
> > > +        Some(unsafe { MmWithUser::from_raw(mm) })
> > > +    }
> > > +
> > 
> > Due to this:
> > 
> > 	https://lore.kernel.org/oe-kbuild-all/202505072116.eSYC8igT-lkp@intel.com/
> > 
> > , I think we should move this impl block into rust/kernel/mm.rs for now,
> > i.e. (in rust/kernel/mm.rs)
> > 
> >     impl crate::task::CurrentTask {
> >         pub fn mm(&self) -> Option<&MmWithUser> { ... }
> >     }
> > 
> > Thoughts?
> > 
> 
> Hmm.. this alone won't be enough, because miscdevice also uses mm. Maybe
> you could most of mm defined even when CONFIG_MMU=n but keep
> MmWithUserAsync only available when CONFIG_MMU=y?
> 

Something like this, probably? But your choice ;-) Make CONFIG_RUST
select CONFIG_MMU is fine but the question is who is going to unselect
that at when?

Regards,
Boqun

------------------>8
diff --git a/rust/kernel/mm.rs b/rust/kernel/mm.rs
index 615907a0f3b4..8a1c8158f2b3 100644
--- a/rust/kernel/mm.rs
+++ b/rust/kernel/mm.rs
@@ -10,7 +10,6 @@
 //! control what happens when userspace reads or writes to that region of memory.
 //!
 //! C header: [`include/linux/mm.h`](srctree/include/linux/mm.h)
-#![cfg(CONFIG_MMU)]
 
 use crate::{
     bindings,
@@ -130,6 +129,7 @@ unsafe impl Send for MmWithUserAsync {}
 // SAFETY: All methods on `MmWithUserAsync` can be called in parallel from several threads.
 unsafe impl Sync for MmWithUserAsync {}
 
+#[cfg(CONFIG_MMU)]
 // SAFETY: By the type invariants, this type is always refcounted.
 unsafe impl AlwaysRefCounted for MmWithUserAsync {
     #[inline]
@@ -207,6 +207,7 @@ pub unsafe fn from_raw<'a>(ptr: *const bindings::mm_struct) -> &'a MmWithUser {
     }
 
     /// Use `mmput_async` when dropping this refcount.
+    #[cfg(CONFIG_MMU)]
     #[inline]
     pub fn into_mmput_async(me: ARef<MmWithUser>) -> ARef<MmWithUserAsync> {
         // SAFETY: The layouts and invariants are compatible.


  reply	other threads:[~2025-05-07 16:55 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-08  9:22 [PATCH v16 0/9] Rust support for mm_struct, vm_area_struct, and mmap Alice Ryhl
2025-04-08  9:22 ` [PATCH v16 1/9] mm: rust: add abstraction for struct mm_struct Alice Ryhl
2025-04-08  9:22 ` [PATCH v16 2/9] mm: rust: add vm_area_struct methods that require read access Alice Ryhl
2025-04-08  9:22 ` [PATCH v16 3/9] mm: rust: add vm_insert_page Alice Ryhl
2025-04-08  9:22 ` [PATCH v16 4/9] mm: rust: add lock_vma_under_rcu Alice Ryhl
2025-04-08  9:22 ` [PATCH v16 5/9] mm: rust: add mmput_async support Alice Ryhl
2025-04-08  9:22 ` [PATCH v16 6/9] mm: rust: add VmaNew for f_ops->mmap() Alice Ryhl
2025-04-08  9:22 ` [PATCH v16 7/9] rust: miscdevice: add mmap support Alice Ryhl
2025-04-08  9:22 ` [PATCH v16 8/9] task: rust: rework how current is accessed Alice Ryhl
2025-05-07 16:06   ` Boqun Feng
2025-05-07 16:46     ` Boqun Feng
2025-05-07 16:55       ` Boqun Feng [this message]
2025-05-08  9:50         ` Alice Ryhl
2025-05-08 20:25         ` John Hubbard
2025-04-08  9:22 ` [PATCH v16 9/9] mm: rust: add MEMORY MANAGEMENT [RUST] Alice Ryhl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aBuQgY4wMv1CsVdo@Mac.home \
    --to=boqun.feng@gmail.com \
    --cc=Liam.Howlett@oracle.com \
    --cc=a.hindborg@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=arnd@arndb.de \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=jannh@google.com \
    --cc=jhubbard@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lorenzo.stoakes@oracle.com \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=surenb@google.com \
    --cc=tmgross@umich.edu \
    --cc=vbabka@suse.cz \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).