From nobody Fri Dec 19 17:31:11 2025 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4FB5D264A98 for ; Tue, 8 Apr 2025 09:24:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744104244; cv=none; b=eibNh479uvm648X5ql8lVykpHyNi4cR5X6VPXEJ9i9XxDmZcmTYBu/tixTrPhXD+fTfokRulhqEhuIqSu2wQ3qIuVJ3+x8VK4HpSzxVdrdWIMjMn+oRSOXKzmlm5So4CMI+N+uo2uIPGrCB4rVfP1wvHSnCO9b8rpGGO0taaWew= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744104244; c=relaxed/simple; bh=yxKBHgKEF0Pk92dXT07hzBYbzkqnu7N3MQCAKHqDQNA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=X3esDW8xTXWcAnTy4uOSOZqs2SGjwwqZmHJNKmJ8zcbti9ocNbqm5h+xtdU2+NnAPlltyUclj1iApIrjlknyvWWhbWyPQGi129d2acrk+XfZ636aibBmoPh9k2xdRBkq/+De5j5Z6xQDGZy15meOfZ3YnTGSfqE9T2q0EqtQRP4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=4Id5eSkR; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="4Id5eSkR" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43eea5a5d80so16370565e9.1 for ; Tue, 08 Apr 2025 02:24:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744104240; x=1744709040; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ki4H2x3bNdUZgaxD5TSjlFdpv0BBNOUt/zA8zBiktxo=; b=4Id5eSkRli4uMs29FFPdVPilE9/xjIMbyrA4ZRcDgR9lzFz+TMaNcuwr2+/ZiTeFgC zfe95dDcRFv6pFX3lrjtEtTBonELyBrTAFFdeyW7pVeOBmm9fuslvK5ndRX8BIZqVfP3 m71TcSwhBfzNR7uB/kK3+NxpdvA4/7vHGqtyY7q8vRXVmZ7uhywUfrfwus+n1pZPb+hl wWPvjQqyx4H+Pjs/wMIqa3EEs3mYgiMWgUuN3XSj5TTseGII1Q+sjpEU0OMBtAlvIMMl O+Pl+pICEylz5d5fLLrEpfB8ZcgzuCIYJ7NbzINlqisOSRwgfciM3pqfAPQTtRRDnpN2 oR2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744104240; x=1744709040; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ki4H2x3bNdUZgaxD5TSjlFdpv0BBNOUt/zA8zBiktxo=; b=Qu5U817n1YZrgmd+jp6l880KovXLU2PzEegC0QvVQeBWOPqMcVlPdm1FUFDK7JwQ1Z w2BLZ7KJ8dLVB19r5U73WZ6TPQA/0eFr7OD4Wgm/M0KNAMnI+W0L2blByfclyAWp7e6L MBD9KV2iJZYyqq6hNXzUGKYN8euNxTYLTe+gKbk3LoFa7kv6t7hN/KLqRY2LvP6Z3hWR iJlw82qwsHpkKXDcs7U4WHLcUu/gTwa4Yf/kzW+BdDrko/qU2yG8kT8KqqzEJKu38rPs 9wvOoG0/znr/eytFUH3lvdoFmxrdiq/zLQwhxK1Wt2yHiMIBQ8/+YFj5tZHmlnrzW3Gj pdyQ== X-Forwarded-Encrypted: i=1; AJvYcCXmNHiEEBsVPiiOJ1BHhZ0GsRqY4y40fayZvX2h9hu5qNsbrGX/5O3t76r2NaZdZKi3t6bcw7YfAxvez1I=@vger.kernel.org X-Gm-Message-State: AOJu0Yyq5qerNAC8MVag310HcP7QqT7PrzeVrCsioL1zWi4M1HG1H13s 5CDxUk3Mhz0jyhzxeD5AGCC6f2B/QdwS2JuFWpwQ0vVklnNyh9G9Wiw/AtCWBb5bXIY+A5N+1KI 6kVCMHHaTF3RQOQ== X-Google-Smtp-Source: AGHT+IHYfn2jFMPzZbozv1znd+quFlDYUIdmNio1DMNMO7NMjqNG77qP6RfgBjCyxquhSAqEKulRpzj7mSohx+c= X-Received: from wmbh6.prod.google.com ([2002:a05:600c:a106:b0:43d:9035:df36]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1385:b0:43d:46de:b0eb with SMTP id 5b1f17b1804b1-43ecf85f4e8mr141716885e9.12.1744104240706; Tue, 08 Apr 2025 02:24:00 -0700 (PDT) Date: Tue, 08 Apr 2025 09:22:38 +0000 In-Reply-To: <20250408-vma-v16-0-d8b446e885d9@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250408-vma-v16-0-d8b446e885d9@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=10913; i=aliceryhl@google.com; h=from:subject:message-id; bh=yxKBHgKEF0Pk92dXT07hzBYbzkqnu7N3MQCAKHqDQNA=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBn9OslaWj4W4zK8+NCEygrsxAUbocMAkAmSrImM xsssH4ZEoaJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ/TrJQAKCRAEWL7uWMY5 Rg9WEAC11iTd6isdQt5qWtvteId/5hP34edfgj8zeH/4CCCW00Sw4HXzTdwHcAegqA1muaKjn0f J9g0wzBZm1fKyRsYLajYxd5Z59jJLlnRrAE5QfbCH90AXUJdc6FftYwB/Jg/JubN3dPNeUYGScU 3ISw9I3xEMA11Px/O+Ai4VVQhN5aR6M44VEyeWxjUgIN2aj7S1pfgnhM+AfmSU2HAYhOHnIqqGe /sVoQtjoOWcfn4/4+Ghg4n5I9Fa3BB6UMd65UaRMBOm9sOzKe3pPw3zybomx1Pb48m/cr1jsJuC SF2dgkMwbO6tvcB5B9exQAD2f2mXfABlxB5KQMzrm9yUaNVYyYEeVU9bluKJIZpPgA/mXkL3NLu 2r0p7NITtj68BUpmG84+W+baaOb30suihSe42mN7lZ88HnlMBmjdk8X8wbt9hvqd5FuG+kYUmiO JJIXTk8EEz7zJ5QD8ZeS+M9vxo7Ut7u5dh9ysXcliHRqMkXioNnPn90W1qvICxl+Tmpz9ENOlwG khFeLyAnteQfbc1E2BSvhXJxziluA8Mq8jwVSWB8/EI7d/h2/a7AiIZJrV6DuwFBUVNaThNUegs A7dNpcPApjwoUJCn2UaYGBgaP3qBi8wES2xeBK2IpYNgpUA9i2MojX3EhyzyigwWiEoWdI7h5kx Ga+MA9pPHmxkk9w== X-Mailer: b4 0.14.2 Message-ID: <20250408-vma-v16-1-d8b446e885d9@google.com> Subject: [PATCH v16 1/9] mm: rust: add abstraction for struct mm_struct From: Alice Ryhl To: Miguel Ojeda , Matthew Wilcox , Lorenzo Stoakes , Vlastimil Babka , John Hubbard , "Liam R. Howlett" , Andrew Morton , Greg Kroah-Hartman , Arnd Bergmann , Jann Horn , Suren Baghdasaryan Cc: Alex Gaynor , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Alice Ryhl , Balbir Singh Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable These abstractions allow you to reference a `struct mm_struct` using both mmgrab and mmget refcounts. This is done using two Rust types: * Mm - represents an mm_struct where you don't know anything about the value of mm_users. * MmWithUser - represents an mm_struct where you know at compile time that mm_users is non-zero. This allows us to encode in the type system whether a method requires that mm_users is non-zero or not. For instance, you can always call `mmget_not_zero` but you can only call `mmap_read_lock` when mm_users is non-zero. The struct is called Mm to keep consistency with the C side. The ability to obtain `current->mm` is added later in this series. The mm module is defined to only exist when CONFIG_MMU is set. This avoids various errors due to missing types and functions when CONFIG_MMU is disabled. More fine-grained cfgs can be considered in the future. See the thread at [1] for more info. Acked-by: Lorenzo Stoakes Acked-by: Liam R. Howlett Acked-by: Balbir Singh Reviewed-by: Andreas Hindborg Reviewed-by: Gary Guo Link: https://lore.kernel.org/all/202503091916.QousmtcY-lkp@intel.com/ Signed-off-by: Alice Ryhl --- rust/helpers/helpers.c | 1 + rust/helpers/mm.c | 39 +++++++++ rust/kernel/lib.rs | 1 + rust/kernel/mm.rs | 210 +++++++++++++++++++++++++++++++++++++++++++++= ++++ 4 files changed, 251 insertions(+) diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c index e1c21eba9b15b672c5e4ab10c4e4c01ed407fae6..48b80bbc0645b2f4abc2b8e2897= 2b0550b5abfea 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -19,6 +19,7 @@ #include "io.c" #include "jump_label.c" #include "kunit.c" +#include "mm.c" #include "mutex.c" #include "page.c" #include "platform.c" diff --git a/rust/helpers/mm.c b/rust/helpers/mm.c new file mode 100644 index 0000000000000000000000000000000000000000..7201747a5d314b2b120b30c0b90= 6715c04ca77a5 --- /dev/null +++ b/rust/helpers/mm.c @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include + +void rust_helper_mmgrab(struct mm_struct *mm) +{ + mmgrab(mm); +} + +void rust_helper_mmdrop(struct mm_struct *mm) +{ + mmdrop(mm); +} + +void rust_helper_mmget(struct mm_struct *mm) +{ + mmget(mm); +} + +bool rust_helper_mmget_not_zero(struct mm_struct *mm) +{ + return mmget_not_zero(mm); +} + +void rust_helper_mmap_read_lock(struct mm_struct *mm) +{ + mmap_read_lock(mm); +} + +bool rust_helper_mmap_read_trylock(struct mm_struct *mm) +{ + return mmap_read_trylock(mm); +} + +void rust_helper_mmap_read_unlock(struct mm_struct *mm) +{ + mmap_read_unlock(mm); +} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index de07aadd1ff5fe46fd89517e234b97a6590c8e93..42ab6cf4053f2cadff0a512b864= 5699bfa6ec568 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -61,6 +61,7 @@ pub mod kunit; pub mod list; pub mod miscdevice; +pub mod mm; #[cfg(CONFIG_NET)] pub mod net; pub mod of; diff --git a/rust/kernel/mm.rs b/rust/kernel/mm.rs new file mode 100644 index 0000000000000000000000000000000000000000..eda7a479cff7e79760bb49eb4bb= 16209bbfc6147 --- /dev/null +++ b/rust/kernel/mm.rs @@ -0,0 +1,210 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2024 Google LLC. + +//! Memory management. +//! +//! This module deals with managing the address space of userspace process= es. Each process has an +//! instance of [`Mm`], which keeps track of multiple VMAs (virtual memory= areas). Each VMA +//! corresponds to a region of memory that the userspace process can acces= s, and the VMA lets you +//! 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, + types::{ARef, AlwaysRefCounted, NotThreadSafe, Opaque}, +}; +use core::{ops::Deref, ptr::NonNull}; + +/// A wrapper for the kernel's `struct mm_struct`. +/// +/// This represents the address space of a userspace process, so each proc= ess has one `Mm` +/// instance. It may hold many VMAs internally. +/// +/// There is a counter called `mm_users` that counts the users of the addr= ess space; this includes +/// the userspace process itself, but can also include kernel threads acce= ssing the address space. +/// Once `mm_users` reaches zero, this indicates that the address space ca= n be destroyed. To access +/// the address space, you must prevent `mm_users` from reaching zero whil= e you are accessing it. +/// The [`MmWithUser`] type represents an address space where this is guar= anteed, and you can +/// create one using [`mmget_not_zero`]. +/// +/// The `ARef` smart pointer holds an `mmgrab` refcount. Its destructo= r may sleep. +/// +/// # Invariants +/// +/// Values of this type are always refcounted using `mmgrab`. +/// +/// [`mmget_not_zero`]: Mm::mmget_not_zero +#[repr(transparent)] +pub struct Mm { + mm: Opaque, +} + +// SAFETY: It is safe to call `mmdrop` on another thread than where `mmgra= b` was called. +unsafe impl Send for Mm {} +// SAFETY: All methods on `Mm` can be called in parallel from several thre= ads. +unsafe impl Sync for Mm {} + +// SAFETY: By the type invariants, this type is always refcounted. +unsafe impl AlwaysRefCounted for Mm { + #[inline] + fn inc_ref(&self) { + // SAFETY: The pointer is valid since self is a reference. + unsafe { bindings::mmgrab(self.as_raw()) }; + } + + #[inline] + unsafe fn dec_ref(obj: NonNull) { + // SAFETY: The caller is giving up their refcount. + unsafe { bindings::mmdrop(obj.cast().as_ptr()) }; + } +} + +/// A wrapper for the kernel's `struct mm_struct`. +/// +/// This type is like [`Mm`], but with non-zero `mm_users`. It can only be= used when `mm_users` can +/// be proven to be non-zero at compile-time, usually because the relevant= code holds an `mmget` +/// refcount. It can be used to access the associated address space. +/// +/// The `ARef` smart pointer holds an `mmget` refcount. Its de= structor may sleep. +/// +/// # Invariants +/// +/// Values of this type are always refcounted using `mmget`. The value of = `mm_users` is non-zero. +#[repr(transparent)] +pub struct MmWithUser { + mm: Mm, +} + +// SAFETY: It is safe to call `mmput` on another thread than where `mmget`= was called. +unsafe impl Send for MmWithUser {} +// SAFETY: All methods on `MmWithUser` can be called in parallel from seve= ral threads. +unsafe impl Sync for MmWithUser {} + +// SAFETY: By the type invariants, this type is always refcounted. +unsafe impl AlwaysRefCounted for MmWithUser { + #[inline] + fn inc_ref(&self) { + // SAFETY: The pointer is valid since self is a reference. + unsafe { bindings::mmget(self.as_raw()) }; + } + + #[inline] + unsafe fn dec_ref(obj: NonNull) { + // SAFETY: The caller is giving up their refcount. + unsafe { bindings::mmput(obj.cast().as_ptr()) }; + } +} + +// Make all `Mm` methods available on `MmWithUser`. +impl Deref for MmWithUser { + type Target =3D Mm; + + #[inline] + fn deref(&self) -> &Mm { + &self.mm + } +} + +// These methods are safe to call even if `mm_users` is zero. +impl Mm { + /// 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 th= at it is not deallocated + /// during the lifetime 'a. + #[inline] + pub unsafe fn from_raw<'a>(ptr: *const bindings::mm_struct) -> &'a Mm { + // SAFETY: Caller promises that the pointer is valid for 'a. Layou= ts are compatible due to + // repr(transparent). + unsafe { &*ptr.cast() } + } + + /// Calls `mmget_not_zero` and returns a handle if it succeeds. + #[inline] + pub fn mmget_not_zero(&self) -> Option> { + // SAFETY: The pointer is valid since self is a reference. + let success =3D unsafe { bindings::mmget_not_zero(self.as_raw()) }; + + if success { + // SAFETY: We just created an `mmget` refcount. + Some(unsafe { ARef::from_raw(NonNull::new_unchecked(self.as_ra= w().cast())) }) + } else { + None + } + } +} + +// These methods require `mm_users` to be non-zero. +impl MmWithUser { + /// Obtain a reference from a raw pointer. + /// + /// # Safety + /// + /// The caller must ensure that `ptr` points at an `mm_struct`, and th= at `mm_users` remains + /// non-zero for the duration of the lifetime 'a. + #[inline] + pub unsafe fn from_raw<'a>(ptr: *const bindings::mm_struct) -> &'a MmW= ithUser { + // SAFETY: Caller promises that the pointer is valid for 'a. The l= ayout is compatible due + // to repr(transparent). + unsafe { &*ptr.cast() } + } + + /// Lock the mmap read lock. + #[inline] + pub fn mmap_read_lock(&self) -> MmapReadGuard<'_> { + // SAFETY: The pointer is valid since self is a reference. + unsafe { bindings::mmap_read_lock(self.as_raw()) }; + + // INVARIANT: We just acquired the read lock. + MmapReadGuard { + mm: self, + _nts: NotThreadSafe, + } + } + + /// Try to lock the mmap read lock. + #[inline] + pub fn mmap_read_trylock(&self) -> Option> { + // SAFETY: The pointer is valid since self is a reference. + let success =3D unsafe { bindings::mmap_read_trylock(self.as_raw()= ) }; + + if success { + // INVARIANT: We just acquired the read lock. + Some(MmapReadGuard { + mm: self, + _nts: NotThreadSafe, + }) + } else { + None + } + } +} + +/// A guard for the mmap read lock. +/// +/// # Invariants +/// +/// This `MmapReadGuard` guard owns the mmap read lock. +pub struct MmapReadGuard<'a> { + mm: &'a MmWithUser, + // `mmap_read_lock` and `mmap_read_unlock` must be called on the same = thread + _nts: NotThreadSafe, +} + +impl Drop for MmapReadGuard<'_> { + #[inline] + fn drop(&mut self) { + // SAFETY: We hold the read lock by the type invariants. + unsafe { bindings::mmap_read_unlock(self.mm.as_raw()) }; + } +} --=20 2.49.0.504.g3bcea36a83-goog From nobody Fri Dec 19 17:31:11 2025 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 89DA9264F9B for ; Tue, 8 Apr 2025 09:24:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744104246; cv=none; b=E2fknNoMR6LnsjoRiuzJEkH3vl8W9IEHZ1WFW64csTMpBiD+er2E/ZR0AwNDDD/1kWbI2bxlDTD7zRcGBm2P1mjtBPbcDg3c/EtvsGRHSYiKdtDKbA/WwsJ3Y62SIDO00+mj/TA6xdRShpXVosD1APiDJ+PGUC7KzzjO0GOmp5I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744104246; c=relaxed/simple; bh=GtH47wxbR+6d0aNTneO5pMigxZ/fp5PImw8k1ccf9Tg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=p62iJ4HeCLzf/231cgDaqsHT+ucVDTNpnhS/ZvKj6F+hCT1qpCzxGq7aHFYzS5wbHJdOc61CnQgmCPhCdANDq7p+yWH9kSuUK/KR8nScgpzKQQgOPLvqdyzrp66YDjwJ2smWoDcKrn4pE53t6u1oubSJtUpzO84/u4aP/ty9I/g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=hS4XC6CF; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="hS4XC6CF" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43eed325461so14243175e9.3 for ; Tue, 08 Apr 2025 02:24:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744104243; x=1744709043; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=9QyemcvIui2lVzxcGzSSLvwxhrDIAPa4Re4ibbE0iVw=; b=hS4XC6CFP/C5yFWMA2XlCssYWt9LoWKx8xYTL099tAxMdFARMsO73ZyiA2zsZlfVwB Lwp1iExQqNFNBJbb81wxlTkdy+znYN7ufD+wClloLgZucAc7lwCaDNxCmojnb53b8QSG ttED+FbiBz1BQWrhQGEFxSCIZZb4hWx1inZAb9odpCuQ3iSiRwLei6LZzG41OWnZfxnD AGQcxTO7yNr+JoYviyRPg10IUqU2nDIbZXDdH+IHib9yuY2j7YawOZmA2m0GPfm8dB9p kGBBkP4nIjhL0XXl2rUFcMxCKT0MS9DG3VILCmhwaoD5sbPbj8vIZAzykJQDBOGbAPHG BNpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744104243; x=1744709043; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=9QyemcvIui2lVzxcGzSSLvwxhrDIAPa4Re4ibbE0iVw=; b=MQG2GZ0fAnHAN8arBcg5OCBe74Gz/hz559l1poMEZNhq/8JEzzQyss8jcrrmTpoSpW 9pExqH1OSV4nZI1YfNMn0UpNT4rqkiRx29nO6yEQaE0jI4f9/8exNwwrY/csNaapSLVY rfhdMVqvqWDYBkp79NvWR8CETeRIqHzheeX9zaqffVzDJcCmN89Bs2h5l4GH0dIemVd8 hFfKLvvbrb8EfF8pgw47wo2/e6eOtU0/8sBTsTLlpeUh9j6DhiQ6fsGtDzekCE4Oy52u iCVbtfcSLQfJ/okysSxLB8WPAyZqQ/Jn8+i22p06oT9OSquo0YzfOl49JA9nnv70dwtm aHFA== X-Forwarded-Encrypted: i=1; AJvYcCUhWs1CXGl9fzw8sCW8xhfPdtNl2P5vwDh85+fTwH3bqyOwRtY/L8vk4TzJMMasrx82oo8epldoQtDqmw8=@vger.kernel.org X-Gm-Message-State: AOJu0YzYFd0QQHWOPqHjAVATIAbP4fH97uGvyLAo/Sezuzt4zezD42Ek c9y1DCpHUBgFQ3I+1jb02mzdeDSKzGYZ8tbJnh6ze6aO3QTHUs3V8ajO5s0Bu1O6P/4dqkRiVfd cMTl19UeeACZLxQ== X-Google-Smtp-Source: AGHT+IFxxFUu1qGOXyvkCU5XAz4kFicPKHdX0TfjTmS5tS8Ie5mgbTz+4dEhe4qW9IslRJpSem7hRyy9es0XXr0= X-Received: from wmbh17.prod.google.com ([2002:a05:600c:a111:b0:43d:8f:dd29]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3b94:b0:43d:649:4e50 with SMTP id 5b1f17b1804b1-43f0ab8c6d2mr38195215e9.13.1744104242867; Tue, 08 Apr 2025 02:24:02 -0700 (PDT) Date: Tue, 08 Apr 2025 09:22:39 +0000 In-Reply-To: <20250408-vma-v16-0-d8b446e885d9@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250408-vma-v16-0-d8b446e885d9@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=12083; i=aliceryhl@google.com; h=from:subject:message-id; bh=GtH47wxbR+6d0aNTneO5pMigxZ/fp5PImw8k1ccf9Tg=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBn9OsmVSvhUiA913nl+JAw+MlOHGWc8EvMXmmCp FwIUbqzQCWJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ/TrJgAKCRAEWL7uWMY5 RkWHEACjkc6iHWBOlrZetxMPhzthtkwd5NgmH0SN7rYXNpTp64E3opnX715hk0ef5PRZ5Nm/iIb OtkXcUfufrOdrLx+/qYdaVCOz06KYK7WD4uis3zU5B0Ri+aRTVulNIGlXRqepDmR3pYbp8PPF07 0Bu1WvumxR1Xim2M9XHDCOtyAd4G0vry0F+zjHGvWeleJmyha06PKWRZYj2bfpj+dtj7CHk+sE8 vaeAWQ91zCa0KwO8YUxDp2K1aQWMob4/+8GBkmrVq6XlGaWDd0cL6m6xqdcAaDXD9gBZ796xiaa tQyDvkO3tysYm+xTpw6NZ4pGfOIaFQugyjqhWbcLDuEPU7X6F67NZKIqf5ABJmqHcFniTyg8v9a 8uH2eJawIb6+7ZFgtzUdd2jcAmZxI58iVRiO2mzS5DMqX/KaCN+9nyf04fqol4eSsbJUGJDyxkw y387YiEA7VvHmYLbmmL0MqGs/u4koer7PHf2RPIANMWwarx4bceJPj2cB1L/JekMsMd6kC3uDNV i/5sM6BW2h+/uyu8vQMJBdH5xkUx0Rh73TCsADJOR22xGNHWo2v+9JFZU7kB8PwQBJrK3Px5GIc qBcE0dFheejnjo5NRQabW0CDywfmxR0dq969Q79nhVQDdkCNFzCNY4rCQ1YJ0IZvdgzRzXKqXkH cL7GVSoLH/eHi7w== X-Mailer: b4 0.14.2 Message-ID: <20250408-vma-v16-2-d8b446e885d9@google.com> Subject: [PATCH v16 2/9] mm: rust: add vm_area_struct methods that require read access From: Alice Ryhl To: Miguel Ojeda , Matthew Wilcox , Lorenzo Stoakes , Vlastimil Babka , John Hubbard , "Liam R. Howlett" , Andrew Morton , Greg Kroah-Hartman , Arnd Bergmann , Jann Horn , Suren Baghdasaryan Cc: Alex Gaynor , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This adds a type called VmaRef which is used when referencing a vma that you have read access to. Here, read access means that you hold either the mmap read lock or the vma read lock (or stronger). Additionally, a vma_lookup method is added to the mmap read guard, which enables you to obtain a &VmaRef in safe Rust code. This patch only provides a way to lock the mmap read lock, but a follow-up patch also provides a way to just lock the vma read lock. Acked-by: Lorenzo Stoakes Acked-by: Liam R. Howlett Reviewed-by: Jann Horn Reviewed-by: Andreas Hindborg Reviewed-by: Gary Guo Signed-off-by: Alice Ryhl --- rust/helpers/mm.c | 6 ++ rust/kernel/mm.rs | 23 ++++++ rust/kernel/mm/virt.rs | 210 +++++++++++++++++++++++++++++++++++++++++++++= ++++ 3 files changed, 239 insertions(+) diff --git a/rust/helpers/mm.c b/rust/helpers/mm.c index 7201747a5d314b2b120b30c0b906715c04ca77a5..7b72eb065a3e1173c920f02a440= 053cf6e93814e 100644 --- a/rust/helpers/mm.c +++ b/rust/helpers/mm.c @@ -37,3 +37,9 @@ void rust_helper_mmap_read_unlock(struct mm_struct *mm) { mmap_read_unlock(mm); } + +struct vm_area_struct *rust_helper_vma_lookup(struct mm_struct *mm, + unsigned long addr) +{ + return vma_lookup(mm, addr); +} diff --git a/rust/kernel/mm.rs b/rust/kernel/mm.rs index eda7a479cff7e79760bb49eb4bb16209bbfc6147..f1689ccb374078a3141489e487f= c32cd97c9c232 100644 --- a/rust/kernel/mm.rs +++ b/rust/kernel/mm.rs @@ -18,6 +18,8 @@ }; use core::{ops::Deref, ptr::NonNull}; =20 +pub mod virt; + /// A wrapper for the kernel's `struct mm_struct`. /// /// This represents the address space of a userspace process, so each proc= ess has one `Mm` @@ -201,6 +203,27 @@ pub struct MmapReadGuard<'a> { _nts: NotThreadSafe, } =20 +impl<'a> MmapReadGuard<'a> { + /// Look up a vma at the given address. + #[inline] + pub fn vma_lookup(&self, vma_addr: usize) -> Option<&virt::VmaRef> { + // SAFETY: By the type invariants we hold the mmap read guard, so = we can safely call this + // method. Any value is okay for `vma_addr`. + let vma =3D unsafe { bindings::vma_lookup(self.mm.as_raw(), vma_ad= dr) }; + + if vma.is_null() { + None + } else { + // SAFETY: We just checked that a vma was found, so the pointe= r references a valid vma. + // + // Furthermore, the returned vma is still under the protection= of the read lock guard + // and can be used while the mmap read lock is still held. Tha= t the vma is not used + // after the MmapReadGuard gets dropped is enforced by the bor= row-checker. + unsafe { Some(virt::VmaRef::from_raw(vma)) } + } + } +} + impl Drop for MmapReadGuard<'_> { #[inline] fn drop(&mut self) { diff --git a/rust/kernel/mm/virt.rs b/rust/kernel/mm/virt.rs new file mode 100644 index 0000000000000000000000000000000000000000..a66be649f0b8d3dfae8ce2d18b7= 0cb2b283fb7fe --- /dev/null +++ b/rust/kernel/mm/virt.rs @@ -0,0 +1,210 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2024 Google LLC. + +//! Virtual memory. +//! +//! This module deals with managing a single VMA in the address space of a= userspace process. Each +//! VMA corresponds to a region of memory that the userspace process can a= ccess, and the VMA lets +//! you control what happens when userspace reads or writes to that region= of memory. +//! +//! The module has several different Rust types that all correspond to the= C type called +//! `vm_area_struct`. The different structs represent what kind of access = you have to the VMA, e.g. +//! [`VmaRef`] is used when you hold the mmap or vma read lock. Using the = appropriate struct +//! ensures that you can't, for example, accidentally call a function that= requires holding the +//! write lock when you only hold the read lock. + +use crate::{bindings, mm::MmWithUser, types::Opaque}; + +/// A wrapper for the kernel's `struct vm_area_struct` with read access. +/// +/// It represents an area of virtual memory. +/// +/// # Invariants +/// +/// The caller must hold the mmap read lock or the vma read lock. +#[repr(transparent)] +pub struct VmaRef { + vma: Opaque, +} + +// Methods you can call when holding the mmap or vma read lock (or stronge= r). They must be usable +// no matter what the vma flags are. +impl VmaRef { + /// Access a virtual memory area given a raw pointer. + /// + /// # Safety + /// + /// Callers must ensure that `vma` is valid for the duration of 'a, an= d that the mmap or vma + /// read lock (or stronger) is held for at least the duration of 'a. + #[inline] + pub unsafe fn from_raw<'a>(vma: *const bindings::vm_area_struct) -> &'= a Self { + // SAFETY: The caller ensures that the invariants are satisfied fo= r the duration of 'a. + unsafe { &*vma.cast() } + } + + /// Returns a raw pointer to this area. + #[inline] + pub fn as_ptr(&self) -> *mut bindings::vm_area_struct { + self.vma.get() + } + + /// Access the underlying `mm_struct`. + #[inline] + pub fn mm(&self) -> &MmWithUser { + // SAFETY: By the type invariants, this `vm_area_struct` is valid = and we hold the mmap/vma + // read lock or stronger. This implies that the underlying mm has = a non-zero value of + // `mm_users`. + unsafe { MmWithUser::from_raw((*self.as_ptr()).vm_mm) } + } + + /// Returns the flags associated with the virtual memory area. + /// + /// The possible flags are a combination of the constants in [`flags`]. + #[inline] + pub fn flags(&self) -> vm_flags_t { + // SAFETY: By the type invariants, the caller holds at least the m= map read lock, so this + // access is not a data race. + unsafe { (*self.as_ptr()).__bindgen_anon_2.vm_flags } + } + + /// Returns the (inclusive) start address of the virtual memory area. + #[inline] + pub fn start(&self) -> usize { + // SAFETY: By the type invariants, the caller holds at least the m= map read lock, so this + // access is not a data race. + unsafe { (*self.as_ptr()).__bindgen_anon_1.__bindgen_anon_1.vm_sta= rt } + } + + /// Returns the (exclusive) end address of the virtual memory area. + #[inline] + pub fn end(&self) -> usize { + // SAFETY: By the type invariants, the caller holds at least the m= map read lock, so this + // access is not a data race. + unsafe { (*self.as_ptr()).__bindgen_anon_1.__bindgen_anon_1.vm_end= } + } + + /// Zap pages in the given page range. + /// + /// This clears page table mappings for the range at the leaf level, l= eaving all other page + /// tables intact, and freeing any memory referenced by the VMA in thi= s range. That is, + /// anonymous memory is completely freed, file-backed memory has its r= eference count on page + /// cache folio's dropped, any dirty data will still be written back t= o disk as usual. + /// + /// It may seem odd that we clear at the leaf level, this is however a= product of the page + /// table structure used to map physical memory into a virtual address= space - each virtual + /// address actually consists of a bitmap of array indices into page t= ables, which form a + /// hierarchical page table level structure. + /// + /// As a result, each page table level maps a multiple of page table l= evels below, and thus + /// span ever increasing ranges of pages. At the leaf or PTE level, we= map the actual physical + /// memory. + /// + /// It is here where a zap operates, as it the only place we can be ce= rtain of clearing without + /// impacting any other virtual mappings. It is an implementation deta= il as to whether the + /// kernel goes further in freeing unused page tables, but for the pur= poses of this operation + /// we must only assume that the leaf level is cleared. + #[inline] + pub fn zap_page_range_single(&self, address: usize, size: usize) { + let (end, did_overflow) =3D address.overflowing_add(size); + if did_overflow || address < self.start() || self.end() < end { + // TODO: call WARN_ONCE once Rust version of it is added + return; + } + + // SAFETY: By the type invariants, the caller has read access to t= his VMA, which is + // sufficient for this method call. This method has no requirement= s on the vma flags. The + // address range is checked to be within the vma. + unsafe { + bindings::zap_page_range_single(self.as_ptr(), address, size, = core::ptr::null_mut()) + }; + } +} + +/// The integer type used for vma flags. +#[doc(inline)] +pub use bindings::vm_flags_t; + +/// All possible flags for [`VmaRef`]. +pub mod flags { + use super::vm_flags_t; + use crate::bindings; + + /// No flags are set. + pub const NONE: vm_flags_t =3D bindings::VM_NONE as _; + + /// Mapping allows reads. + pub const READ: vm_flags_t =3D bindings::VM_READ as _; + + /// Mapping allows writes. + pub const WRITE: vm_flags_t =3D bindings::VM_WRITE as _; + + /// Mapping allows execution. + pub const EXEC: vm_flags_t =3D bindings::VM_EXEC as _; + + /// Mapping is shared. + pub const SHARED: vm_flags_t =3D bindings::VM_SHARED as _; + + /// Mapping may be updated to allow reads. + pub const MAYREAD: vm_flags_t =3D bindings::VM_MAYREAD as _; + + /// Mapping may be updated to allow writes. + pub const MAYWRITE: vm_flags_t =3D bindings::VM_MAYWRITE as _; + + /// Mapping may be updated to allow execution. + pub const MAYEXEC: vm_flags_t =3D bindings::VM_MAYEXEC as _; + + /// Mapping may be updated to be shared. + pub const MAYSHARE: vm_flags_t =3D bindings::VM_MAYSHARE as _; + + /// Page-ranges managed without `struct page`, just pure PFN. + pub const PFNMAP: vm_flags_t =3D bindings::VM_PFNMAP as _; + + /// Memory mapped I/O or similar. + pub const IO: vm_flags_t =3D bindings::VM_IO as _; + + /// Do not copy this vma on fork. + pub const DONTCOPY: vm_flags_t =3D bindings::VM_DONTCOPY as _; + + /// Cannot expand with mremap(). + pub const DONTEXPAND: vm_flags_t =3D bindings::VM_DONTEXPAND as _; + + /// Lock the pages covered when they are faulted in. + pub const LOCKONFAULT: vm_flags_t =3D bindings::VM_LOCKONFAULT as _; + + /// Is a VM accounted object. + pub const ACCOUNT: vm_flags_t =3D bindings::VM_ACCOUNT as _; + + /// Should the VM suppress accounting. + pub const NORESERVE: vm_flags_t =3D bindings::VM_NORESERVE as _; + + /// Huge TLB Page VM. + pub const HUGETLB: vm_flags_t =3D bindings::VM_HUGETLB as _; + + /// Synchronous page faults. (DAX-specific) + pub const SYNC: vm_flags_t =3D bindings::VM_SYNC as _; + + /// Architecture-specific flag. + pub const ARCH_1: vm_flags_t =3D bindings::VM_ARCH_1 as _; + + /// Wipe VMA contents in child on fork. + pub const WIPEONFORK: vm_flags_t =3D bindings::VM_WIPEONFORK as _; + + /// Do not include in the core dump. + pub const DONTDUMP: vm_flags_t =3D bindings::VM_DONTDUMP as _; + + /// Not soft dirty clean area. + pub const SOFTDIRTY: vm_flags_t =3D bindings::VM_SOFTDIRTY as _; + + /// Can contain `struct page` and pure PFN pages. + pub const MIXEDMAP: vm_flags_t =3D bindings::VM_MIXEDMAP as _; + + /// MADV_HUGEPAGE marked this vma. + pub const HUGEPAGE: vm_flags_t =3D bindings::VM_HUGEPAGE as _; + + /// MADV_NOHUGEPAGE marked this vma. + pub const NOHUGEPAGE: vm_flags_t =3D bindings::VM_NOHUGEPAGE as _; + + /// KSM may merge identical pages. + pub const MERGEABLE: vm_flags_t =3D bindings::VM_MERGEABLE as _; +} --=20 2.49.0.504.g3bcea36a83-goog From nobody Fri Dec 19 17:31:11 2025 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA2E3265612 for ; Tue, 8 Apr 2025 09:24:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744104249; cv=none; b=QIsf17GaJaI40Z8dfk7ceskRqNQf2WvJfDBLlqfxRZq6ICUG1mXx+qkFxrUndfcDS/01t8BcIZJYcJp/HcYENEHOZr+6zLTJ/3CSxVcvCIxnMIGhr1rKxl0GKfNlFWqfrx0OeMn3uUKgvZFDi7fvsGQg8sunjzdggEZZukEX3dY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744104249; c=relaxed/simple; bh=6cK8H0f1bjkfbBZECW+oXwSFALiwF8hLaYj708yZ9Vs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=KtZQ9ZKyhGc6L8BZ3+hufshRUKwwgTEhl5ThArUeEf3WmV0WmG7F1tFIJo9kVx6BWJMs13+3x6oodLUmCmaQLPtCteIXBaY5TENpn+kflY/G4nLUarFXXJncbgY0L2GeJsilQvfnNLmkMW0xxcBk95P+lMbQZTduYILmrFiJkko= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=v9pHGTwn; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="v9pHGTwn" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43ceeaf1524so29979945e9.1 for ; Tue, 08 Apr 2025 02:24:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744104245; x=1744709045; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=s5w8I1p3Tb9DO8JsnIo9VM8qLIkaDI8pOQrbVUWT6uI=; b=v9pHGTwnWt/CIeVyoBV0Gse996PE34inGSro+PcqxGZqHfU0QYruROSvExS5BWWXOD BfQGqOmAJwHMAW4eKtqkNP4p9xdqeYcZC8WQslYUmHIDSf1A6KxM3Q34GWem01Zi04a4 BLAG4J9G9XYY+R37KEXRQb6HCxWPwoO82NjJw7Gyvuxk1iAb88FkrBICuyjhapmkgl8F /5A1aMivzBSsCqWc7N8x1YnPlECclHUOQCeFDyphF1kDqZat2RaHCWkIVmeNG4cs6EDQ mwjtHncSavcb8+ggdvtQtLgR2lJVf8CJo3gadcSWAdfH9QM43rIatNykiSYo/q61HhCM V1Cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744104245; x=1744709045; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=s5w8I1p3Tb9DO8JsnIo9VM8qLIkaDI8pOQrbVUWT6uI=; b=FAC3uN2ZBPTTySD1J94MxAkvT0sG1D2X9761hzFMzc0/Q3kISgRypU7JoopKAMDg2+ 0KKcFEzG5EtXHYIguClfTd5+GzORgPEaz1RjepfSe/jfjSKeptOsNlYvw05avtmFOfOB l4BQPI6N0zKnbYcjFPnnLnFKOWgiaJQ2EhQMi94ma4SAFdWaUA4pFycN4ji5/XWtMCka T9AoLgqnUpbH2oFi7p0pKisgQOR8N+UEY1o/WMgKF/ewvu0hmgwfhG/kN1wuyzJzLo+X 6v4a22jTctO4TtaZobTnZYeN1VwgTMBwthiEGRJjlZvC1gaWYsl1vmltMtI6t7aD1qoZ Pcaw== X-Forwarded-Encrypted: i=1; AJvYcCVpSrhrpdvXT+hpgpKKj1bCPhbAPeU27I1EYG6LXmooZr0U2NnM4aCKTvlZB29vmMoeQhNrr3UYBtSqH9M=@vger.kernel.org X-Gm-Message-State: AOJu0YxLdqzufBwyUq4Ua/Qgj6nHJg/J/NNMzjuQ/pqvBzpVRdsrnxHs Qek/yq7j5Q9sde1e/6qifpbonKRZhK2tGB/A67m/gqeuMayu+AGrQjGVGfBjHDPuwbbDDtuvLvZ x8jm+kOIwSr4vZw== X-Google-Smtp-Source: AGHT+IHpYdwjp62GC42TgJRjq1KuRmzOAAEOqv7lAljye6ysUkygRaxUq/uM+rb+EggvJF0qu6sb2cFi0FK4uNE= X-Received: from wmbh6.prod.google.com ([2002:a05:600c:a106:b0:43c:eb09:3790]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:5845:b0:439:4c1e:d810 with SMTP id 5b1f17b1804b1-43f0e59c566mr15047995e9.9.1744104244985; Tue, 08 Apr 2025 02:24:04 -0700 (PDT) Date: Tue, 08 Apr 2025 09:22:40 +0000 In-Reply-To: <20250408-vma-v16-0-d8b446e885d9@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250408-vma-v16-0-d8b446e885d9@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=4393; i=aliceryhl@google.com; h=from:subject:message-id; bh=6cK8H0f1bjkfbBZECW+oXwSFALiwF8hLaYj708yZ9Vs=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBn9OsnO6hmFHtTeBdUk9KFgbrB2QrDLVOl+CpTX d6JGe8HUuKJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ/TrJwAKCRAEWL7uWMY5 RldLEACSoWh10crAZ99JPh2xNve1MWXfeXmmLSD1NzqPq91mZuyNyd7KxvS8MCgtkDxjI4lf/EK yl3m+I12OBvL54nMiHw6/jwjv8z/14XNQeRQx0aDtU2N66XpXG1O+tQ3Ow12J7cTrqmQaPNRBry lEjdcqaKJCly/EPPyJjRdLHd42EKtbyJN/OCoXQu1p/a/GvwLVbOVhGRTzC/i9XHCIzn0Tl5G2F kIAi6g93MU6TMnO3GGvKqDdS5exJQllY+P5pd9MxDU/4O/mMoxbfVxHtIbw3zavFNbI7ifI2w+g hc65G3O7tEkNtCwmy19QiNF/BkS/dnDnqKgco1rsL1enCAJSxUK87DZHaQ4cfvO08iABgGfa8tA fEQFM0ry4n1mDKBHHr34ICS9Tmrrj+zqh8ydMudFmwBYAVGu4A6YDBYs3whhMDeDxFT5gOb6IwW fTcdaAwIvwLNDqd/5iXGY8t/zcdrngDyChZB6VbZCwln/MEEyh1mhh0sCKs3oVO+ixfj4JrBG8E E3NaAjrWqbNzifbv+AII4tSHJFmW3ExuWdgRDNxSkrBhGFPjNDw4/VRsFlMnXVUUjhZsEpDjyjJ CO4gGUAFnizx/292hDoxB8xe9gzUaUycfj4uGObs+4NHG6wkZiS4K7YrktZkNEw7AcV0CdW5Yip h29xzmoJxJ6uJMg== X-Mailer: b4 0.14.2 Message-ID: <20250408-vma-v16-3-d8b446e885d9@google.com> Subject: [PATCH v16 3/9] mm: rust: add vm_insert_page From: Alice Ryhl To: Miguel Ojeda , Matthew Wilcox , Lorenzo Stoakes , Vlastimil Babka , John Hubbard , "Liam R. Howlett" , Andrew Morton , Greg Kroah-Hartman , Arnd Bergmann , Jann Horn , Suren Baghdasaryan Cc: Alex Gaynor , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The vm_insert_page method is only usable on vmas with the VM_MIXEDMAP flag, so we introduce a new type to keep track of such vmas. The approach used in this patch assumes that we will not need to encode many flag combinations in the type. I don't think we need to encode more than VM_MIXEDMAP and VM_PFNMAP as things are now. However, if that becomes necessary, using generic parameters in a single type would scale better as the number of flags increases. Acked-by: Lorenzo Stoakes Acked-by: Liam R. Howlett Reviewed-by: Andreas Hindborg Reviewed-by: Gary Guo Signed-off-by: Alice Ryhl --- rust/kernel/mm/virt.rs | 79 ++++++++++++++++++++++++++++++++++++++++++++++= +++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/rust/kernel/mm/virt.rs b/rust/kernel/mm/virt.rs index a66be649f0b8d3dfae8ce2d18b70cb2b283fb7fe..3e2eabcc21450497a02ffa1ed1f= 31f3e7e7e1b6b 100644 --- a/rust/kernel/mm/virt.rs +++ b/rust/kernel/mm/virt.rs @@ -14,7 +14,15 @@ //! ensures that you can't, for example, accidentally call a function that= requires holding the //! write lock when you only hold the read lock. =20 -use crate::{bindings, mm::MmWithUser, types::Opaque}; +use crate::{ + bindings, + error::{to_result, Result}, + mm::MmWithUser, + page::Page, + types::Opaque, +}; + +use core::ops::Deref; =20 /// A wrapper for the kernel's `struct vm_area_struct` with read access. /// @@ -119,6 +127,75 @@ pub fn zap_page_range_single(&self, address: usize, si= ze: usize) { bindings::zap_page_range_single(self.as_ptr(), address, size, = core::ptr::null_mut()) }; } + + /// If the [`VM_MIXEDMAP`] flag is set, returns a [`VmaMixedMap`] to t= his VMA, otherwise + /// returns `None`. + /// + /// This can be used to access methods that require [`VM_MIXEDMAP`] to= be set. + /// + /// [`VM_MIXEDMAP`]: flags::MIXEDMAP + #[inline] + pub fn as_mixedmap_vma(&self) -> Option<&VmaMixedMap> { + if self.flags() & flags::MIXEDMAP !=3D 0 { + // SAFETY: We just checked that `VM_MIXEDMAP` is set. All othe= r requirements are + // satisfied by the type invariants of `VmaRef`. + Some(unsafe { VmaMixedMap::from_raw(self.as_ptr()) }) + } else { + None + } + } +} + +/// A wrapper for the kernel's `struct vm_area_struct` with read access an= d [`VM_MIXEDMAP`] set. +/// +/// It represents an area of virtual memory. +/// +/// This struct is identical to [`VmaRef`] except that it must only be use= d when the +/// [`VM_MIXEDMAP`] flag is set on the vma. +/// +/// # Invariants +/// +/// The caller must hold the mmap read lock or the vma read lock. The `VM_= MIXEDMAP` flag must be +/// set. +/// +/// [`VM_MIXEDMAP`]: flags::MIXEDMAP +#[repr(transparent)] +pub struct VmaMixedMap { + vma: VmaRef, +} + +// Make all `VmaRef` methods available on `VmaMixedMap`. +impl Deref for VmaMixedMap { + type Target =3D VmaRef; + + #[inline] + fn deref(&self) -> &VmaRef { + &self.vma + } +} + +impl VmaMixedMap { + /// Access a virtual memory area given a raw pointer. + /// + /// # Safety + /// + /// Callers must ensure that `vma` is valid for the duration of 'a, an= d that the mmap read lock + /// (or stronger) is held for at least the duration of 'a. The `VM_MIX= EDMAP` flag must be set. + #[inline] + pub unsafe fn from_raw<'a>(vma: *const bindings::vm_area_struct) -> &'= a Self { + // SAFETY: The caller ensures that the invariants are satisfied fo= r the duration of 'a. + unsafe { &*vma.cast() } + } + + /// Maps a single page at the given address within the virtual memory = area. + /// + /// This operation does not take ownership of the page. + #[inline] + pub fn vm_insert_page(&self, address: usize, page: &Page) -> Result { + // SAFETY: By the type invariant of `Self` caller has read access = and has verified that + // `VM_MIXEDMAP` is set. By invariant on `Page` the page has order= 0. + to_result(unsafe { bindings::vm_insert_page(self.as_ptr(), address= , page.as_ptr()) }) + } } =20 /// The integer type used for vma flags. --=20 2.49.0.504.g3bcea36a83-goog From nobody Fri Dec 19 17:31:11 2025 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8691D26561C for ; Tue, 8 Apr 2025 09:24:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744104250; cv=none; b=tzdR8dYJ/1E9Uc531KML3tNhNr5VBD7VDHotIUFrA5fl5XuvwdLuT2lGvAZs0fpxBA3Mp5rW5yp+9j3uZAsFQghCDnLSRen4iPU6wB95G86I3uwiO5+TPK41jyQ/lhJGtNpckEJkODj078ti8Xk95aOpJtOBQLdytAL+z2n4EVQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744104250; c=relaxed/simple; bh=gF1ckMqCWHCjQXFknkNdHEiUzDYS1Ku+dCA1Sq6IckY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IJfXqHcnflcRxvXbB6ZnV14WjtKgRc2+UGfKoalZLjudZNJEnLFYddbMK0ItPmlqNmPQkfbdDQv35CzkMDaMmzADz9szMwORTQokM8tcjMuOaE2xeTyo1VErwpnrnyu+gsJWz1VUGwd+dCq7j+w2u+Utb3+HuZY9ZneYgKQBFGc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=pYn5j2U5; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="pYn5j2U5" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43cf3168b87so29596745e9.2 for ; Tue, 08 Apr 2025 02:24:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744104247; x=1744709047; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=fSV463Hzq4lcGHaBMKwzfTcjqbOdgyFN9gtovsbYOqc=; b=pYn5j2U5ZASKvI6FYYcER187Tjg6VBWXYoPS18s7YsbAmnymxfUSLLetRLD9pylEB+ DAv8ZKYYU59L+lAnf40tXD5IxD1+75LY4TM+3+21AXqKGrGPF02g+qlv8Ok4cj8195tV f6XzEVwhe75xNdsvlWYDA5fVCmDFDQgJs3mH6LXuTgpHpyVMI+tmyyZevqHG0y0I7Dds rW+uDjc0l9qTG4gEa+Ku4Uc+Y1V5vcMJmXvZMRfl2zz6o+Z/Sw+Za5wZReBBaAoEGnQp uwhEnxKJNUkNniNM/j2EkMoIBTRLZCJAaFRV3GvNch8PWICx50vAXye3GkKSC8qe7Q2w b32g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744104247; x=1744709047; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=fSV463Hzq4lcGHaBMKwzfTcjqbOdgyFN9gtovsbYOqc=; b=mWrZCR1418bzLmwRUmSeLlENv/3kjuE/jHPzF1e8HsqtOcLInow7YsDxfbM+639srC iIM3taajjFcX4zxGcqUQV88DRzCbGVksB/42LLTsJ/pxe/Y4S0E6umSPEPYyRt7OfNGc uxRPuSBjGYC4z+6ZoodVb216/0ITsEWAkdk7JNMBevIh17ekmNBXXXMG+eMk46InMWCY qAgzWw6z+DtSJK9FsjDVQmiacmUfhUe2xr2jwPEV6REYCvJ1KVDirGMhOoxt11HBe1hB 72hbzYFvCwUBElU+09IYnjPTs4CV4bPyOXpBYDbuWvPvnXVIpWtT+H+ApgslLep1kemL WPLQ== X-Forwarded-Encrypted: i=1; AJvYcCV9m3aEAZQak2s9tIPM0VLpGLoyw9/Y7bqXzAYeVZ9tRpDBOsOVmVmVrSBbFt3MwJWONzgzvoyMYGJ/RnY=@vger.kernel.org X-Gm-Message-State: AOJu0YyzL023vyO03ewwXS6NdTUFYXrCDJCDTIVVmS+/nXNzjZ5GgiL/ 4XJ2Wq9AlNoahOzOsqrWg9TnzNqW9RbHQGrefPP41Lgw/MgmbFqTRGWjV/FOBafuoZ/vRUojvEI iQgcgHlz+sdW0KQ== X-Google-Smtp-Source: AGHT+IGpYx8GY+CcPqdjvQeTUdadbSh+V6DOVo+9tIk10BZYKUulfM4YzPVOn2JdHxlRFKSX0lPoXYX9f1ZU4sI= X-Received: from wmbgx9.prod.google.com ([2002:a05:600c:8589:b0:43d:5264:3cf8]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1f07:b0:43c:f8fc:f687 with SMTP id 5b1f17b1804b1-43ecfa04a8emr120987995e9.27.1744104247078; Tue, 08 Apr 2025 02:24:07 -0700 (PDT) Date: Tue, 08 Apr 2025 09:22:41 +0000 In-Reply-To: <20250408-vma-v16-0-d8b446e885d9@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250408-vma-v16-0-d8b446e885d9@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=4299; i=aliceryhl@google.com; h=from:subject:message-id; bh=gF1ckMqCWHCjQXFknkNdHEiUzDYS1Ku+dCA1Sq6IckY=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBn9Osn3jxbpIs+xoCWpCoObHsxRS9D60krCSnYT liiyaW/aLiJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ/TrJwAKCRAEWL7uWMY5 RoLCEACYidsumhaA9miV4nPn5brjSwUFAg1VPkCXuULdVYpR98jJerXgW0o+C/JgnKudQsmOGPJ B7MEnQur02LcSgPW71ukNXp86bWc9dSnuMZIJRiK/wCi4UEJeVVaxOUu+id75fPxyCMPS+TzNWQ QuuKW3KOwpcwFr3fW601D3P4jKIJnca7mC8wuMxunrbFpz5pkVJ/bLGhrasFOUH70F1VjBussqq ZvnZWyn9z0Ij1HBgqvzUwNNtUVq27ul92aCNCiSMf7GTLqNV9EM6W1fIKAOC4s9bA9/usTrp02V T5BEZ5zGhMzQ1uOoAUgyH1229eWh5Un0A65JrF0XHe2ZJKS2Gsbv6iVSX5CCL4m+BtcRszMLJ5z wndBdwWT2DDDJyyshSoi3FOe9bfUFnCdoZ5cNTQxrD+Rz32PN9kJ5qiPBsQp+jS2HcqTFZ/mCKe xIpYVQcyhcnRstMU/N81m1h8c/SYGx2U46nPiQXpN3Pqb4wrIDQQpMxwsnh9ON7ewaaoYEI8/2C VBeETIom53ABUiJaI7b5R/ysuayAGNljta5PvwhvI94/KVaxcg5Ys2grjswEly5jp8O/36vVuE1 LstY/UxTleVJNGEycPlzy9gTvstP2u79n1tNOhKrVQ7IKwH84lR60sVjU1jArF8+cp9zjjBb4FL LkKtiYSl9CTjJ7w== X-Mailer: b4 0.14.2 Message-ID: <20250408-vma-v16-4-d8b446e885d9@google.com> Subject: [PATCH v16 4/9] mm: rust: add lock_vma_under_rcu From: Alice Ryhl To: Miguel Ojeda , Matthew Wilcox , Lorenzo Stoakes , Vlastimil Babka , John Hubbard , "Liam R. Howlett" , Andrew Morton , Greg Kroah-Hartman , Arnd Bergmann , Jann Horn , Suren Baghdasaryan Cc: Alex Gaynor , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Currently, the binder driver always uses the mmap lock to make changes to its vma. Because the mmap lock is global to the process, this can involve significant contention. However, the kernel has a feature called per-vma locks, which can significantly reduce contention. For example, you can take a vma lock in parallel with an mmap write lock. This is important because contention on the mmap lock has been a long-term recurring challenge for the Binder driver. This patch introduces support for using `lock_vma_under_rcu` from Rust. The Rust Binder driver will be able to use this to reduce contention on the mmap lock. Acked-by: Lorenzo Stoakes Acked-by: Liam R. Howlett Reviewed-by: Jann Horn Reviewed-by: Andreas Hindborg Reviewed-by: Gary Guo Signed-off-by: Alice Ryhl --- rust/helpers/mm.c | 5 +++++ rust/kernel/mm.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 65 insertions(+) diff --git a/rust/helpers/mm.c b/rust/helpers/mm.c index 7b72eb065a3e1173c920f02a440053cf6e93814e..81b510c96fd2692dcb7ab4705f7= 90bd3a41a630e 100644 --- a/rust/helpers/mm.c +++ b/rust/helpers/mm.c @@ -43,3 +43,8 @@ struct vm_area_struct *rust_helper_vma_lookup(struct mm_s= truct *mm, { return vma_lookup(mm, addr); } + +void rust_helper_vma_end_read(struct vm_area_struct *vma) +{ + vma_end_read(vma); +} diff --git a/rust/kernel/mm.rs b/rust/kernel/mm.rs index f1689ccb374078a3141489e487fc32cd97c9c232..c160fb52603f8d4b349000451e2= 200fb477d98a6 100644 --- a/rust/kernel/mm.rs +++ b/rust/kernel/mm.rs @@ -19,6 +19,7 @@ use core::{ops::Deref, ptr::NonNull}; =20 pub mod virt; +use virt::VmaRef; =20 /// A wrapper for the kernel's `struct mm_struct`. /// @@ -161,6 +162,36 @@ pub unsafe fn from_raw<'a>(ptr: *const bindings::mm_st= ruct) -> &'a MmWithUser { unsafe { &*ptr.cast() } } =20 + /// Attempt to access a vma using the vma read lock. + /// + /// This is an optimistic trylock operation, so it may fail if there i= s contention. In that + /// case, you should fall back to taking the mmap read lock. + /// + /// When per-vma locks are disabled, this always returns `None`. + #[inline] + pub fn lock_vma_under_rcu(&self, vma_addr: usize) -> Option> { + #[cfg(CONFIG_PER_VMA_LOCK)] + { + // SAFETY: Calling `bindings::lock_vma_under_rcu` is always ok= ay given an mm where + // `mm_users` is non-zero. + let vma =3D unsafe { bindings::lock_vma_under_rcu(self.as_raw(= ), vma_addr) }; + if !vma.is_null() { + return Some(VmaReadGuard { + // SAFETY: If `lock_vma_under_rcu` returns a non-null = ptr, then it points at a + // valid vma. The vma is stable for as long as the vma= read lock is held. + vma: unsafe { VmaRef::from_raw(vma) }, + _nts: NotThreadSafe, + }); + } + } + + // Silence warnings about unused variables. + #[cfg(not(CONFIG_PER_VMA_LOCK))] + let _ =3D vma_addr; + + None + } + /// Lock the mmap read lock. #[inline] pub fn mmap_read_lock(&self) -> MmapReadGuard<'_> { @@ -231,3 +262,32 @@ fn drop(&mut self) { unsafe { bindings::mmap_read_unlock(self.mm.as_raw()) }; } } + +/// A guard for the vma read lock. +/// +/// # Invariants +/// +/// This `VmaReadGuard` guard owns the vma read lock. +pub struct VmaReadGuard<'a> { + vma: &'a VmaRef, + // `vma_end_read` must be called on the same thread as where the lock = was taken + _nts: NotThreadSafe, +} + +// Make all `VmaRef` methods available on `VmaReadGuard`. +impl Deref for VmaReadGuard<'_> { + type Target =3D VmaRef; + + #[inline] + fn deref(&self) -> &VmaRef { + self.vma + } +} + +impl Drop for VmaReadGuard<'_> { + #[inline] + fn drop(&mut self) { + // SAFETY: We hold the read lock by the type invariants. + unsafe { bindings::vma_end_read(self.vma.as_ptr()) }; + } +} --=20 2.49.0.504.g3bcea36a83-goog From nobody Fri Dec 19 17:31:11 2025 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B2DB8265638 for ; Tue, 8 Apr 2025 09:24:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744104252; cv=none; b=O/wkY/xIlZ8Omq0M1mAglX83AoQPOpV/+4fHf6Vcl1QmBrHjYftoXO1sJP7ZvM6BbLBHwPCQRQsrHHfl/ort6cCS1JXx+fI2JfNsIeNnmFLaxch4FNlWQfjT52cHGGm0NUDPKs4pMaElBmIaZ8SvSQ7mhLHi+8fcslCd7klqBZ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744104252; c=relaxed/simple; bh=LmtcKlgkxP20LxSNq37k/eXJOg5jI9B+op34oo2QYlU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=K3K6LsgIZdgVRVcMf4zEPj3TFT1tLB5THLuoqtfGfp9fuOX/9OK1SCBT2vkyhpiVSgmZ2np9qY37Mg7ESoWrNpCYOxTBGwIH7w7+TGBFPtwbP6ANJTcgVNpxPhAMEqxB/5MWJDMGSFZ0sIwnpjSHkM0lBMcj22cbKL29eEf4kao= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=SFVhT604; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="SFVhT604" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43efa869b19so16419985e9.2 for ; Tue, 08 Apr 2025 02:24:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744104249; x=1744709049; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=bACckoQSwDfUzBFoYMbXvpmJP89UDR/iGZngv5w49FI=; b=SFVhT604XnTv254qop7i8nmpe8BlzcMSog1HqxKHCRCCtEbiDq36v+V32lFvOhMGWV FTbE3C9yYb6r1m4J0zkCwY3NNLn+zNxrdoxeyb3EIX5UiOcgzsuhw3/yR5X0rFq7nFJE zujVCgcXCBuR556NmTfSKUUgZ6m2AZff5yi1+kKZWdHgpO2li87x6moRJqhAOUmEJ+NL gbWXNmNgzdTWCLoY6mJ9bGWLi100+X4FfHMR/FfwmI7zKL/7bBWgaPLXKKmehN+EeGkz rzDxJun2S3jO2tX78BU6OXKFJuv6lyOqKqu6juuWxO0AC0gLN96mx4ktVy914OwI+r8P IGEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744104249; x=1744709049; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=bACckoQSwDfUzBFoYMbXvpmJP89UDR/iGZngv5w49FI=; b=nqRGgptUE7PmXXAyJotGv7x5Vt8vJlm9ac76XwnwASKokiLtnQwcB8U4fN+AXqRp3J iAZ56ndqMjFgjBxQOgD9V3M27KicW2mNT6ScX8lHVmRrG+W9MfLsvmF4Y8GJvgbBYgAr z6yd9QuTDs581T5ChCmL1xKDikn48+fOb4t5mkdV2H4WL9tUK9qBmJ3tS6HCrHIj2rbx Pv2GfjQ59qyYS6JL1b9RJ/6wEFsklwXRZAoQyUc+R0UkDLUqk8yeJXCFguw1HeUgR7nl Wdjfvrgoqk3vjysWVrtowGDxYzcwldrU0ccZBT/Ox3mXWlGtAbDHXNOFjQaAEEB9ehIw 9Jwg== X-Forwarded-Encrypted: i=1; AJvYcCV/gBCrjWJN8CBpBQ/bGydszLJbSAMdusLwPDf+oF2ylvQQNwdCx9i4VBBLWBvRWHPzOCkIsj7n/Git3fI=@vger.kernel.org X-Gm-Message-State: AOJu0Yz/LwllkrLovz207+fh8qYFU6eaWOY45iq8hemv7zLwb92g4dry hLr32IXa5KIzlwf8U4v/0XQ+p0dGpzJ9k+OTCP6CdygtMlMFXyqlSo137QsnDV5FTpC/TVb4mxk rWjHkOw2I/eQXEA== X-Google-Smtp-Source: AGHT+IHDWCtsJ/5jduSfjKCiGsbR920yirUwpI0CePyKkQEEcAeAv5QDUW+I6X/+MdTEL+UPb7oTVguYSXBKpx8= X-Received: from wmqe6.prod.google.com ([2002:a05:600c:4e46:b0:43b:c914:a2d9]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:35c7:b0:43c:e7ae:4bc9 with SMTP id 5b1f17b1804b1-43ecf842969mr131465855e9.1.1744104249155; Tue, 08 Apr 2025 02:24:09 -0700 (PDT) Date: Tue, 08 Apr 2025 09:22:42 +0000 In-Reply-To: <20250408-vma-v16-0-d8b446e885d9@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250408-vma-v16-0-d8b446e885d9@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3335; i=aliceryhl@google.com; h=from:subject:message-id; bh=LmtcKlgkxP20LxSNq37k/eXJOg5jI9B+op34oo2QYlU=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBn9OsoxyVRA9K3PlCKr3B/V+eq1q3gOWsdM/eiZ X6K6ZfFYLyJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ/TrKAAKCRAEWL7uWMY5 RhEYD/4kfuPcMl/0QAODznj6Z6rJZ5GbQA6paTos+nodEsXLiY5K7KF8sXZ9m9HJTOXfxUpr35t 9d3MYoHUG8X+nN+7y27bmoEl4iX4gIiMayP3g7ZdhstImQd7F066cNJcFBH1sNAOrVXuJxIyEY9 SoBJu8KFCx5vYsInFWNcy/zUY8h+JQTgNr1Vl9IBy1zSCjtNFHjUje+j4ntKN94ibZED5FQ2j8z kWu3QLhK5v4A0XpD8yt/ukwfDgKuUXwSs53w3MzZOgJtBy3eRmPapqXBfACVrKQz0PbKyzWPs2x i0f5UPpqZbWu9zmyRHmd0HqUUJnZvcUe38eNlOS241KVxKtb7eZSqiASAOGSK1DMV+XyFxM2WRN txmkDEKuYUrNMqeQi9yYhnknTvQbllMTtDzr0KeWmeP5shXPPOmfJETUe+qKjzKhNlfjZeLVu1e QdG51nh4dJk61DNeXYIq7gzxCMNk7bFLWJbezGnGlLYV2Gf0q6raYCAtwOI1iMn/qZeRJTm4J0f zLvHzfkLprVn689x4BVJ7PD5SNMmxS4BJVqiYhlfxLF/AVPW2LLg6y52gXb/ZckUu9csL3tZV2y XTi1fsOmVPx1sq7h/Cvwhfoomv9yQzJlgkzUgg0hUN9+m40owcDN8CNsW8duOTqJ68tXwJ/GwXT PGfD3DtHd9SDcgg== X-Mailer: b4 0.14.2 Message-ID: <20250408-vma-v16-5-d8b446e885d9@google.com> Subject: [PATCH v16 5/9] mm: rust: add mmput_async support From: Alice Ryhl To: Miguel Ojeda , Matthew Wilcox , Lorenzo Stoakes , Vlastimil Babka , John Hubbard , "Liam R. Howlett" , Andrew Morton , Greg Kroah-Hartman , Arnd Bergmann , Jann Horn , Suren Baghdasaryan Cc: Alex Gaynor , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Adds an MmWithUserAsync type that uses mmput_async when dropped but is otherwise identical to MmWithUser. This has to be done using a separate type because the thing we are changing is the destructor. Rust Binder needs this to avoid a certain deadlock. See commit 9a9ab0d96362 ("binder: fix race between mmput() and do_exit()") for details. It's also needed in the shrinker to avoid cleaning up the mm in the shrinker's context. Acked-by: Lorenzo Stoakes Acked-by: Liam R. Howlett Reviewed-by: Andreas Hindborg Reviewed-by: Gary Guo Signed-off-by: Alice Ryhl --- rust/kernel/mm.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/rust/kernel/mm.rs b/rust/kernel/mm.rs index c160fb52603f8d4b349000451e2200fb477d98a6..615907a0f3b487996ddc066083c= 4984a443f7146 100644 --- a/rust/kernel/mm.rs +++ b/rust/kernel/mm.rs @@ -111,6 +111,50 @@ fn deref(&self) -> &Mm { } } =20 +/// A wrapper for the kernel's `struct mm_struct`. +/// +/// This type is identical to `MmWithUser` except that it uses `mmput_asyn= c` when dropping a +/// refcount. This means that the destructor of `ARef` is= safe to call in atomic +/// context. +/// +/// # Invariants +/// +/// Values of this type are always refcounted using `mmget`. The value of = `mm_users` is non-zero. +#[repr(transparent)] +pub struct MmWithUserAsync { + mm: MmWithUser, +} + +// SAFETY: It is safe to call `mmput_async` on another thread than where `= mmget` was called. +unsafe impl Send for MmWithUserAsync {} +// SAFETY: All methods on `MmWithUserAsync` can be called in parallel from= several threads. +unsafe impl Sync for MmWithUserAsync {} + +// SAFETY: By the type invariants, this type is always refcounted. +unsafe impl AlwaysRefCounted for MmWithUserAsync { + #[inline] + fn inc_ref(&self) { + // SAFETY: The pointer is valid since self is a reference. + unsafe { bindings::mmget(self.as_raw()) }; + } + + #[inline] + unsafe fn dec_ref(obj: NonNull) { + // SAFETY: The caller is giving up their refcount. + unsafe { bindings::mmput_async(obj.cast().as_ptr()) }; + } +} + +// Make all `MmWithUser` methods available on `MmWithUserAsync`. +impl Deref for MmWithUserAsync { + type Target =3D MmWithUser; + + #[inline] + fn deref(&self) -> &MmWithUser { + &self.mm + } +} + // These methods are safe to call even if `mm_users` is zero. impl Mm { /// Returns a raw pointer to the inner `mm_struct`. @@ -162,6 +206,13 @@ pub unsafe fn from_raw<'a>(ptr: *const bindings::mm_st= ruct) -> &'a MmWithUser { unsafe { &*ptr.cast() } } =20 + /// Use `mmput_async` when dropping this refcount. + #[inline] + pub fn into_mmput_async(me: ARef) -> ARef= { + // SAFETY: The layouts and invariants are compatible. + unsafe { ARef::from_raw(ARef::into_raw(me).cast()) } + } + /// Attempt to access a vma using the vma read lock. /// /// This is an optimistic trylock operation, so it may fail if there i= s contention. In that --=20 2.49.0.504.g3bcea36a83-goog From nobody Fri Dec 19 17:31:11 2025 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9772F265CC3 for ; Tue, 8 Apr 2025 09:24:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744104254; cv=none; b=mgZyORsNXqMtPrR1ijbFVeVXlDW06z7+4AVq2g/oteH5MqHdcdArLuMWbpwRO2LP4n+bjlgX6sEJ2gPniwS8ZH+DxujKLowSMUDy+tKkeHPFSCpbQOGQQAC/XDRGXaXZkr6zPQc6Sw1JsBgB/CKCqCVmw39IcZenR/1b7qBIDWo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744104254; c=relaxed/simple; bh=y7o7ZyQ2dPEky50Ue+6FuWOqrOt6CBr0m5lqB46eSLI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jcIZwjyxgvJnln1mlqBurBT9SjzsFyqWRFaA2LTO2mNrAuc5JP5ioICWUprXREEDIogofJQnfv4wajO3f9RW5mwUrTSPuy1EVSnPE2UGvGyCHdEYfg33+IyWt7741MIrPfnJqAKOUyLBpl7LrkQpsoD9BJs1OywtfUt/6JclQeI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=NPkzp5KJ; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="NPkzp5KJ" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43ceb011ea5so35605645e9.2 for ; Tue, 08 Apr 2025 02:24:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744104251; x=1744709051; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=NbS7MAhMYKAe1fvQnX5I031aFw3fZNHXs82c8Rh/ZM8=; b=NPkzp5KJl1rteYHXmiPCKlp9XzTS6x4GJY+29RoOuYCMoaOwh8Jxti4hbLzunyriki s+Hv2UcniMkGnYYSWfvwlQCBi3eyLAGcMg1UJ6Em+P39f78VHC7ow2GROTZs70AiIe5O 87hxX2JmUuXfDGCdBGxVe6Ps8kDMrW7FZ6nSVtZNzg9sCSTrxmf19wj4Sc7akV0xAokp vLaKRmpLHInJAqJEjADEV5PXsjfvpPnBDuOgym4KWubVss1Z2skaBP7wlKZct2xR5VDC RwqNhKjcgYu4bQpC6+aqXlt1aKeggZd+PqhUfLuyYFyo423Phl8hNy+TewH9lNhb3lQh 8s5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744104251; x=1744709051; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=NbS7MAhMYKAe1fvQnX5I031aFw3fZNHXs82c8Rh/ZM8=; b=uYtVQTBCFvnYvIkasovztkmHgmdnWqzm3EspZ0TvsIoaI4fZQY1HjQfeeL2LuhqxjX Wp9hBOLz9oPd3GSGsR556sxdWnNjIEMTkpgWdGWKk/NurM8BB8JY6t9mGIcTHrgF+Gnr v1Kr00pWnWO2wVVQhGqH5Xylo8zCgpERL95hkKQF2giJib+AkNoZqIuaj/gljvjY7eS4 Z2PGs3GvQ6PNaLYS0IDFfIpDyDeHwiDirbifPeuQd2AfCbQ6HIANUC8yYpZ1cJ0fc+d1 Z2y2bZyYqyTQ7PzVxCcmXBRExpgBVg9hrm+/h8Y2RQnLAXs6tH/1yc6JqysF3DlM1Dx+ 4qhQ== X-Forwarded-Encrypted: i=1; AJvYcCWwUgDSdNspgjCwpswm5wAFzLu6cemMueIl6YwwsVruLONsCRMDJlxFZ7tj7fA11OFMfdvWsi+RdubNa/A=@vger.kernel.org X-Gm-Message-State: AOJu0YysjZr6XqY2Qelrz5bVhBFysrgDB51tzjQscXFq1XjSx7RKTy/G CF4V4m+QPK+DKauX44e8IeWmNuVhLXQw3kuDDxoZ0zgJshejHpNQ454zvdqbij8/yfNBr7wbGX/ ZQPFTqGUM7+98Uw== X-Google-Smtp-Source: AGHT+IE4W/KsQEJrmEgz9IsoTLynGVEAhAHKzspx/nbrSFmIxb6oHbD2e0Mgj+efPoxDwU0BvGGyyufB1Mi07JA= X-Received: from wma7.prod.google.com ([2002:a05:600c:8907:b0:43d:4038:9229]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:a0a:b0:43c:f689:dd with SMTP id 5b1f17b1804b1-43ecf8d0a37mr135840695e9.19.1744104251264; Tue, 08 Apr 2025 02:24:11 -0700 (PDT) Date: Tue, 08 Apr 2025 09:22:43 +0000 In-Reply-To: <20250408-vma-v16-0-d8b446e885d9@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250408-vma-v16-0-d8b446e885d9@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=8695; i=aliceryhl@google.com; h=from:subject:message-id; bh=y7o7ZyQ2dPEky50Ue+6FuWOqrOt6CBr0m5lqB46eSLI=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBn9OspqQy5WgY/Qd+8dWuqTUfQ3HnCwBb8dVD05 EA6Kry80NiJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ/TrKQAKCRAEWL7uWMY5 RgG+D/4imuYCLk7UElHu8hBZPmO6nTzNlNaQKt0LiTcGYywxsrABM/WJ8sMq5nF56+zZSR06nuc 56Kmkc7+8F2O1qfjff8l3mHCzT0J9cf6wAYYALBXOkz5KvBs7hWsjNTdnJzWZn++ZEjrRbTdrvq yg+RSfCLjZlIhNX6Qd/XZpMrLipc6MkzjLWxzEujf6AnkG17xmf4E8Kg02holVGJXsTKo+qM766 seqiZkbFJzPBajPN6woE0fyAqN8+JidK+UJmAYYNB/Xdfl4yGudLtE7gjgbrHNhlS1anVWwuFZC FdSApqsObE8OK9MNeI6RuiAjuro8QX9Qw/jocbWr0aBk1+Pn7TnOFobEil62TNdBYyA+8peUYAG jh93VskPs3P8of4BOr051oLK5kwcG25LOWkkStYuiYb3Emdi6ep3U/6tAcPb5MGnFCl5lBjr++/ UDPMjSqUQdpDlqWBx7rNRwDmCe2sMGlf7qo5K8Oly0cNdX+wmfJZ7/sKVg9sVXfAmH5RoKej18i LgUdXmjLd8L1exfbJ5E35i+jFmbusUl3F1xK+8ontqpt4uMc17eaMyDYaBc4ao+gqBIk2U3Nzgj LLd9Y59W9MRwdX2jJf8mifll5w1o9piYo9LgxbM7KFZHEPIIHAOfsvTSGIK/N3PmMbd89l33pKn WLK3RhVfl1Zx/4g== X-Mailer: b4 0.14.2 Message-ID: <20250408-vma-v16-6-d8b446e885d9@google.com> Subject: [PATCH v16 6/9] mm: rust: add VmaNew for f_ops->mmap() From: Alice Ryhl To: Miguel Ojeda , Matthew Wilcox , Lorenzo Stoakes , Vlastimil Babka , John Hubbard , "Liam R. Howlett" , Andrew Morton , Greg Kroah-Hartman , Arnd Bergmann , Jann Horn , Suren Baghdasaryan Cc: Alex Gaynor , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This type will be used when setting up a new vma in an f_ops->mmap() hook. Using a separate type from VmaRef allows us to have a separate set of operations that you are only able to use during the mmap() hook. For example, the VM_MIXEDMAP flag must not be changed after the initial setup that happens during the f_ops->mmap() hook. To avoid setting invalid flag values, the methods for clearing VM_MAYWRITE and similar involve a check of VM_WRITE, and return an error if VM_WRITE is set. Trying to use `try_clear_maywrite` without checking the return value results in a compilation error because the `Result` type is marked #[must_use]. For now, there's only a method for VM_MIXEDMAP and not VM_PFNMAP. When we add a VM_PFNMAP method, we will need some way to prevent you from setting both VM_MIXEDMAP and VM_PFNMAP on the same vma. Acked-by: Lorenzo Stoakes Acked-by: Liam R. Howlett Reviewed-by: Jann Horn Reviewed-by: Andreas Hindborg Signed-off-by: Alice Ryhl --- rust/kernel/mm/virt.rs | 186 +++++++++++++++++++++++++++++++++++++++++++++= +++- 1 file changed, 185 insertions(+), 1 deletion(-) diff --git a/rust/kernel/mm/virt.rs b/rust/kernel/mm/virt.rs index 3e2eabcc21450497a02ffa1ed1f31f3e7e7e1b6b..31803674aecc57408df7960def1= 7cfdc2cebcd6c 100644 --- a/rust/kernel/mm/virt.rs +++ b/rust/kernel/mm/virt.rs @@ -16,7 +16,7 @@ =20 use crate::{ bindings, - error::{to_result, Result}, + error::{code::EINVAL, to_result, Result}, mm::MmWithUser, page::Page, types::Opaque, @@ -198,6 +198,190 @@ pub fn vm_insert_page(&self, address: usize, page: &P= age) -> Result { } } =20 +/// A configuration object for setting up a VMA in an `f_ops->mmap()` hook. +/// +/// The `f_ops->mmap()` hook is called when a new VMA is being created, an= d the hook is able to +/// configure the VMA in various ways to fit the driver that owns it. Usin= g `VmaNew` indicates that +/// you are allowed to perform operations on the VMA that can only be perf= ormed before the VMA is +/// fully initialized. +/// +/// # Invariants +/// +/// For the duration of 'a, the referenced vma must be undergoing initiali= zation in an +/// `f_ops->mmap()` hook. +pub struct VmaNew { + vma: VmaRef, +} + +// Make all `VmaRef` methods available on `VmaNew`. +impl Deref for VmaNew { + type Target =3D VmaRef; + + #[inline] + fn deref(&self) -> &VmaRef { + &self.vma + } +} + +impl VmaNew { + /// Access a virtual memory area given a raw pointer. + /// + /// # Safety + /// + /// Callers must ensure that `vma` is undergoing initial vma setup for= the duration of 'a. + #[inline] + pub unsafe fn from_raw<'a>(vma: *mut bindings::vm_area_struct) -> &'a = Self { + // SAFETY: The caller ensures that the invariants are satisfied fo= r the duration of 'a. + unsafe { &*vma.cast() } + } + + /// Internal method for updating the vma flags. + /// + /// # Safety + /// + /// This must not be used to set the flags to an invalid value. + #[inline] + unsafe fn update_flags(&self, set: vm_flags_t, unset: vm_flags_t) { + let mut flags =3D self.flags(); + flags |=3D set; + flags &=3D !unset; + + // SAFETY: This is not a data race: the vma is undergoing initial = setup, so it's not yet + // shared. Additionally, `VmaNew` is `!Sync`, so it cannot be used= to write in parallel. + // The caller promises that this does not set the flags to an inva= lid value. + unsafe { (*self.as_ptr()).__bindgen_anon_2.__vm_flags =3D flags }; + } + + /// Set the `VM_MIXEDMAP` flag on this vma. + /// + /// This enables the vma to contain both `struct page` and pure PFN pa= ges. Returns a reference + /// that can be used to call `vm_insert_page` on the vma. + #[inline] + pub fn set_mixedmap(&self) -> &VmaMixedMap { + // SAFETY: We don't yet provide a way to set VM_PFNMAP, so this ca= nnot put the flags in an + // invalid state. + unsafe { self.update_flags(flags::MIXEDMAP, 0) }; + + // SAFETY: We just set `VM_MIXEDMAP` on the vma. + unsafe { VmaMixedMap::from_raw(self.vma.as_ptr()) } + } + + /// Set the `VM_IO` flag on this vma. + /// + /// This is used for memory mapped IO and similar. The flag tells othe= r parts of the kernel to + /// avoid looking at the pages. For memory mapped IO this is useful as= accesses to the pages + /// could have side effects. + #[inline] + pub fn set_io(&self) { + // SAFETY: Setting the VM_IO flag is always okay. + unsafe { self.update_flags(flags::IO, 0) }; + } + + /// Set the `VM_DONTEXPAND` flag on this vma. + /// + /// This prevents the vma from being expanded with `mremap()`. + #[inline] + pub fn set_dontexpand(&self) { + // SAFETY: Setting the VM_DONTEXPAND flag is always okay. + unsafe { self.update_flags(flags::DONTEXPAND, 0) }; + } + + /// Set the `VM_DONTCOPY` flag on this vma. + /// + /// This prevents the vma from being copied on fork. This option is on= ly permanent if `VM_IO` + /// is set. + #[inline] + pub fn set_dontcopy(&self) { + // SAFETY: Setting the VM_DONTCOPY flag is always okay. + unsafe { self.update_flags(flags::DONTCOPY, 0) }; + } + + /// Set the `VM_DONTDUMP` flag on this vma. + /// + /// This prevents the vma from being included in core dumps. This opti= on is only permanent if + /// `VM_IO` is set. + #[inline] + pub fn set_dontdump(&self) { + // SAFETY: Setting the VM_DONTDUMP flag is always okay. + unsafe { self.update_flags(flags::DONTDUMP, 0) }; + } + + /// Returns whether `VM_READ` is set. + /// + /// This flag indicates whether userspace is mapping this vma as reada= ble. + #[inline] + pub fn readable(&self) -> bool { + (self.flags() & flags::READ) !=3D 0 + } + + /// Try to clear the `VM_MAYREAD` flag, failing if `VM_READ` is set. + /// + /// This flag indicates whether userspace is allowed to make this vma = readable with + /// `mprotect()`. + /// + /// Note that this operation is irreversible. Once `VM_MAYREAD` has be= en cleared, it can never + /// be set again. + #[inline] + pub fn try_clear_mayread(&self) -> Result { + if self.readable() { + return Err(EINVAL); + } + // SAFETY: Clearing `VM_MAYREAD` is okay when `VM_READ` is not set. + unsafe { self.update_flags(0, flags::MAYREAD) }; + Ok(()) + } + + /// Returns whether `VM_WRITE` is set. + /// + /// This flag indicates whether userspace is mapping this vma as writa= ble. + #[inline] + pub fn writable(&self) -> bool { + (self.flags() & flags::WRITE) !=3D 0 + } + + /// Try to clear the `VM_MAYWRITE` flag, failing if `VM_WRITE` is set. + /// + /// This flag indicates whether userspace is allowed to make this vma = writable with + /// `mprotect()`. + /// + /// Note that this operation is irreversible. Once `VM_MAYWRITE` has b= een cleared, it can never + /// be set again. + #[inline] + pub fn try_clear_maywrite(&self) -> Result { + if self.writable() { + return Err(EINVAL); + } + // SAFETY: Clearing `VM_MAYWRITE` is okay when `VM_WRITE` is not s= et. + unsafe { self.update_flags(0, flags::MAYWRITE) }; + Ok(()) + } + + /// Returns whether `VM_EXEC` is set. + /// + /// This flag indicates whether userspace is mapping this vma as execu= table. + #[inline] + pub fn executable(&self) -> bool { + (self.flags() & flags::EXEC) !=3D 0 + } + + /// Try to clear the `VM_MAYEXEC` flag, failing if `VM_EXEC` is set. + /// + /// This flag indicates whether userspace is allowed to make this vma = executable with + /// `mprotect()`. + /// + /// Note that this operation is irreversible. Once `VM_MAYEXEC` has be= en cleared, it can never + /// be set again. + #[inline] + pub fn try_clear_mayexec(&self) -> Result { + if self.executable() { + return Err(EINVAL); + } + // SAFETY: Clearing `VM_MAYEXEC` is okay when `VM_EXEC` is not set. + unsafe { self.update_flags(0, flags::MAYEXEC) }; + Ok(()) + } +} + /// The integer type used for vma flags. #[doc(inline)] pub use bindings::vm_flags_t; --=20 2.49.0.504.g3bcea36a83-goog From nobody Fri Dec 19 17:31:11 2025 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 18398266581 for ; Tue, 8 Apr 2025 09:24:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744104257; cv=none; b=ub9zBnq+I3UM46SxsayfM4FswAsMSR2oRjMi0VVdhwtY2p6JxjHozlnT6xu0tvhjMii53glLdkxYF6HSnNbX1WACsRLslFZ6iDCRiDiyFHYL6+s7exDrjcIMFVAEuInVk8xg5hCyFK+1P9loVOtGcnvkZRGB6ItT10tkF0e+3hg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744104257; c=relaxed/simple; bh=Sfgq//T2QbdbqoMqrWL7BQLUnjSkh+kpgtWBCSQivcU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=I4iQZOWuIowkayp44KOK3XAEcSMN1kkm8lM20+gRYEB9iCudH8UW9rZGpJelmZkUB7Z44x8s4BKtZMc3n1KPS9wRj2r+SL95AB20FPORympXILtxtUyC+XzyRB9egYznKRbLSAEQERMdOiQ+p/Bw4h8nBznI8WNVP9P1RuBeASE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=cx4HGTyc; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cx4HGTyc" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43d51bd9b45so36714935e9.1 for ; Tue, 08 Apr 2025 02:24:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744104253; x=1744709053; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=BKgSHP0UtGt+oV0m7wKF/tLoW5odDMj8vC7ZBIsrjgA=; b=cx4HGTycjH4KeEr0pMv7vXk2vfEVLqn+WWn+LNGQ/tIY4o6W5FZbPCg86+LIy/OZjc aAFQ7FUmLwDvjjbFt7Cb/7vmGRE6XhFg32wOy6GM6KlNH7l9AAoY1Lexv5Zrs8FQdwSx vDvQp8tMo8SJJA0CjQvtrvb2tUKbdlpX+SVezJlklIixHQTzU+oJRz68IXPCdvNHie8E l0ipdVRyV0AEUb7D5lv3Tg7gp1yFp2S2/6zIJR8u74LDQvIkQ2rgPdZ7Kf+CiTUY37WQ qDbPD56dUcCxI60YI/xEALRVKR1qo7wgYo5pAittjbXjwyBOQFqdrx7QG/D6e5jn+b3+ psLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744104253; x=1744709053; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=BKgSHP0UtGt+oV0m7wKF/tLoW5odDMj8vC7ZBIsrjgA=; b=gqMyOlIOTVzVY5gGRRsjrDFDhWLpEx3i15pumb3VftTo4DxFTFXEGhtceH9D0GIENH LbnS4BXVFvmGxjovXXxAjlQ0FzAWMZ1Eilc4G1y4RUshApFX2wpQah4ErOqU26oW8o9Z imEiOrpY2NfzPmnEwzTQ8NifB1Q4aeGmc/i8qvN+08hnivi5o6An1LI9dNDiAzTKBejM XuTOLnomw+RPt5dIXjAppuBexl5oif8f19Qs3tZvCbR4svkqAN7olMUCI1cU+ilCGeDF grIv9Grndh4i/lS+JivLRCDVLEWzRqOXCoNh9hHHuh/ZQLTB7zBikav+JaZTj/4bZom1 Wtgw== X-Forwarded-Encrypted: i=1; AJvYcCV7FEcf90aA9UiBkp66gpksIvD1H/HlqJPOU3+W1uG9feE4iSHTWFGRoCGYMpV3IVX/pVEL21bnzk6Q+Wk=@vger.kernel.org X-Gm-Message-State: AOJu0YxObX6/ycJpZazb64/+Twwqkm3ywsp4CffRyCZcJ5L34G6GnTUZ 93zZiFf8ikDci3nzFLoamol//0eJMw1paNuhnLtdj0Ve8tXKXBuFYq5Vg5I13Yl8q3BLJBd8m62 7Ixd7WCo6EfWWCw== X-Google-Smtp-Source: AGHT+IGKxxFnPnuYQhaMn/C1VHYnvMuiIufsTL+sO+MIIaikeAKJnyM61wTXLBWoQ3PMLnGlIdAs32Q49e1duTw= X-Received: from wmbbg30.prod.google.com ([2002:a05:600c:3c9e:b0:43d:44cf:11f8]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3c8d:b0:43b:cc42:c54f with SMTP id 5b1f17b1804b1-43ecf85f20amr127759175e9.14.1744104253433; Tue, 08 Apr 2025 02:24:13 -0700 (PDT) Date: Tue, 08 Apr 2025 09:22:44 +0000 In-Reply-To: <20250408-vma-v16-0-d8b446e885d9@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250408-vma-v16-0-d8b446e885d9@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=4043; i=aliceryhl@google.com; h=from:subject:message-id; bh=Sfgq//T2QbdbqoMqrWL7BQLUnjSkh+kpgtWBCSQivcU=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBn9Osphtn+zBbO8bZFvj7pkZhABnr8VbaFiFEa8 vSFFXvSY0CJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ/TrKQAKCRAEWL7uWMY5 RuB5D/9RA7H3cjo5/vuxD/VzZyAqYJY0YR+6BZctDljbIzIBs0tHYjQGyX3i9/lx7Z7I24HZgG3 GYQdZnhqBnxIJeK+aTd3+I6/0Kxt/6IQR6Q12dP9St1jrQR8QY0NbbDlJIpZsh4EnSERgXH4VR2 Iv/dWP2koycKPHA53sb047tk00stSC8FBUFUrjU+fXIPEprHdC2O05Nn5ARdWJUF/teV+/VTF6O MjPU9y7a0TfFX9kS24LGjGguBGUuk04TbeSD3AJ4zI5ttYztwe4rDoy0G5JK0WAdqdOzGvUzjyA AqLPw3Ciycje/cSkNmjIHN4NgE5iCyRh6m6L/7vT4aE+XlSlK/fFXROSRYWDwRSP/MQc0QPPXCm NN3I1c3hL62W/8E3tveIBF+7V6kp8O8061h//5D2SScR/0OuE8ZVF+UvtYcgqS+ZHSWbog/rDqb LvsWYiaW8W+F501INqaTk2CdldRz8wUlYQ2xCRWzFZTlkuXChlDP4Zf8Uhv25SGDKHNwfGiWyoC cw7vMFfyvtUFsM0wxwdFhrB6PlXfugS1gsYCk4jrqO7Oq1WipRemsXxl5MPxVF9hjTQSISjPoT4 mlt0NTtEaoXDAUWMGbFJyHdFpelBc1wv8qCvZZmccvdR9ztCb6uhqJ185K2TK0waNVyj8MsR5Cz nJaPeEL8PTxVTAw== X-Mailer: b4 0.14.2 Message-ID: <20250408-vma-v16-7-d8b446e885d9@google.com> Subject: [PATCH v16 7/9] rust: miscdevice: add mmap support From: Alice Ryhl To: Miguel Ojeda , Matthew Wilcox , Lorenzo Stoakes , Vlastimil Babka , John Hubbard , "Liam R. Howlett" , Andrew Morton , Greg Kroah-Hartman , Arnd Bergmann , Jann Horn , Suren Baghdasaryan Cc: Alex Gaynor , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Add the ability to write a file_operations->mmap hook in Rust when using the miscdevice abstraction. The `vma` argument to the `mmap` hook uses the `VmaNew` type from the previous commit; this type provides the correct set of operations for a file_operations->mmap hook. Acked-by: Greg Kroah-Hartman Acked-by: Lorenzo Stoakes Acked-by: Liam R. Howlett Reviewed-by: Andreas Hindborg Reviewed-by: Gary Guo Signed-off-by: Alice Ryhl --- rust/kernel/miscdevice.rs | 45 +++++++++++++++++++++++++++++++++++++++++++= ++ 1 file changed, 45 insertions(+) diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs index fa9ecc42602a477328a25b5d357db90b59dc72ae..9d9771247c3865761c4387467de= 3f1d1a82691b7 100644 --- a/rust/kernel/miscdevice.rs +++ b/rust/kernel/miscdevice.rs @@ -14,6 +14,7 @@ error::{to_result, Error, Result, VTABLE_DEFAULT_ERROR}, ffi::{c_int, c_long, c_uint, c_ulong}, fs::File, + mm::virt::VmaNew, prelude::*, seq_file::SeqFile, str::CStr, @@ -119,6 +120,22 @@ fn release(device: Self::Ptr, _file: &File) { drop(device); } =20 + /// Handle for mmap. + /// + /// This function is invoked when a user space process invokes the `mm= ap` system call on + /// `file`. The function is a callback that is part of the VMA initial= izer. The kernel will do + /// initial setup of the VMA before calling this function. The functio= n can then interact with + /// the VMA initialization by calling methods of `vma`. If the functio= n does not return an + /// error, the kernel will complete initialization of the VMA accordin= g to the properties of + /// `vma`. + fn mmap( + _device: ::Borrowed<'_>, + _file: &File, + _vma: &VmaNew, + ) -> Result { + build_error!(VTABLE_DEFAULT_ERROR) + } + /// Handler for ioctls. /// /// The `cmd` argument is usually manipulated using the utilties in [`= kernel::ioctl`]. @@ -223,6 +240,33 @@ impl MiscdeviceVTable { 0 } =20 + /// # Safety + /// + /// `file` must be a valid file that is associated with a `MiscDeviceR= egistration`. + /// `vma` must be a vma that is currently being mmap'ed with this file. + unsafe extern "C" fn mmap( + file: *mut bindings::file, + vma: *mut bindings::vm_area_struct, + ) -> c_int { + // SAFETY: The mmap call of a file can access the private data. + let private =3D unsafe { (*file).private_data }; + // SAFETY: This is a Rust Miscdevice, so we call `into_foreign` in= `open` and + // `from_foreign` in `release`, and `fops_mmap` is guaranteed to b= e called between those + // two operations. + let device =3D unsafe { ::borrow(private= ) }; + // SAFETY: The caller provides a vma that is undergoing initial VM= A setup. + let area =3D unsafe { VmaNew::from_raw(vma) }; + // SAFETY: + // * The file is valid for the duration of this call. + // * There is no active fdget_pos region on the file on this threa= d. + let file =3D unsafe { File::from_raw_file(file) }; + + match T::mmap(device, file, area) { + Ok(()) =3D> 0, + Err(err) =3D> err.to_errno(), + } + } + /// # Safety /// /// `file` must be a valid file that is associated with a `MiscDeviceR= egistration`. @@ -291,6 +335,7 @@ impl MiscdeviceVTable { const VTABLE: bindings::file_operations =3D bindings::file_operations { open: Some(Self::open), release: Some(Self::release), + mmap: if T::HAS_MMAP { Some(Self::mmap) } else { None }, unlocked_ioctl: if T::HAS_IOCTL { Some(Self::ioctl) } else { --=20 2.49.0.504.g3bcea36a83-goog From nobody Fri Dec 19 17:31:11 2025 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8C40F264A72 for ; Tue, 8 Apr 2025 09:24:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744104261; cv=none; b=ZYquK9evGpnHUK3iwYxx8mt6N2FysBF2TUbfU1tykQB6WQ9IhqRmmHMPb+2kYkPFeRxEOcAJK8Z9FbcSJn+G1KMigOEJ4Cn9PAIJzJQXlq6GOswY6hcv2ISfWCciP2S7MBcvnjgYxVNs93DzVBOUQPSM7uGxC2DpyZVosONOqEU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744104261; c=relaxed/simple; bh=ZfEObmQkj1K3PKRhCEzycp7JYCVP4InOSHhkkaYW3MA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=epLPMj3zxfQWJPT6+hvM0QYz4AcoZctxuNnGd9u8PMn0zx4OQSrsWZTOIOFZPGnBaLcAe95B/ojRCMQcoIZD1vct2NuTfH5IPKiVigRAaFDvG4bmJ8uKXjhBk4GUOqzlyD5tTd3poRatHKEg+PfzGHwEm3SCBuSRzt7Wp7Wqr9Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=S/ngfQyP; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="S/ngfQyP" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43941ad86d4so29805195e9.2 for ; Tue, 08 Apr 2025 02:24:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744104256; x=1744709056; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=i/iS6j8sog2gWlkO8N4e9GnomGOnzRdojkcd1mY5y4s=; b=S/ngfQyP7HMlMaHI1tiXZOHDyt0bYQgRM5t7J5DJvuwOSTItMuAjjPzsoymuHled19 CLJ8IM4tgx0CFDA7HK7Z3U980koOXYruAZ+Z4x+V4ZLTbyCKeLfuipRTVccjfrJi3vYn LQbGJmNVy5Rx5VGKUwCNQ//fOf+qG27dJMVTogRbQXorAYEN/zOueaJn2m5wYQsD06/S +9jNAyCxy8QsmpY3RRYyQoRSVwG45r40JpUf1q6JBOdxvocrpgBrv+cnbiJa5x1AC17r piQcT0EFhfpw0WBYvdiMjaLYzGUj0rmSxxio7w26T5iIXBhA2+MX1Nu69x4y9JC7Gw+v bSsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744104256; x=1744709056; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=i/iS6j8sog2gWlkO8N4e9GnomGOnzRdojkcd1mY5y4s=; b=pu3cHGl7qAI9m1XSfJnKmHbXSOyTO3sXUCVe3Uf6Yaasl3j78eyn+6U5km2OR3n6Yc S/AU5UydP7ObwPoAdqh067sbFjYt6pE7JnH2+DEr71JR57VKzq5rYNiGUOf37NwRlJho gva9uYRCfxTYwqZ6Uh85GRToU+Wo9MSrQWVdi89Uz5uoHuBJ/djFVJEHqNnB7pacoYKc Uxk9vYmjDUDiCnDWVrxolxNCrVk4zExCHI5nVTId/Ml6UfXz764JRlwhFxLgVsYkOLuZ 00S2c6DgT71a9606/MVSIb7yqLRm6vH3hWMMrbAErX2tBDhKh0eSdn7i/dgwHnr31zJt fhGQ== X-Forwarded-Encrypted: i=1; AJvYcCXPO9PqFma+4aK7z+naGp8Hn+WkdcXmtw3bNy2kJHPqcJ+qOq7dfJgPZcJ2b7KH0P/cIe2FYV9j0NMls3U=@vger.kernel.org X-Gm-Message-State: AOJu0YzMqsYGiZxTtZ+EpCXhvrKz2Mg5kqedZGX7q+t7LS0N32zowV4W 3CCiexWUGFyjE1+30oq/YuGcMjQobvKIZT1urzw7lKzaoYFhjt1ij+sb8aJ6dapY0cuq8Hw8DcM R2LeHbkcKgKZdIA== X-Google-Smtp-Source: AGHT+IHF4NMSTY3mUG2B1pvCnIHlh681e95gNltNsfVVp4JefLhrym0jFmpHMw8kUvRQBBTjs304yWhH2X14FoA= X-Received: from wmbbh5.prod.google.com ([2002:a05:600c:3d05:b0:43c:eba5:f9b3]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:350b:b0:43c:e2dd:98ea with SMTP id 5b1f17b1804b1-43ee076d4e9mr86042425e9.22.1744104255877; Tue, 08 Apr 2025 02:24:15 -0700 (PDT) Date: Tue, 08 Apr 2025 09:22:45 +0000 In-Reply-To: <20250408-vma-v16-0-d8b446e885d9@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250408-vma-v16-0-d8b446e885d9@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=18345; i=aliceryhl@google.com; h=from:subject:message-id; bh=ZfEObmQkj1K3PKRhCEzycp7JYCVP4InOSHhkkaYW3MA=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBn9OsqEqNPldGcWxjAzols5gVQJsu4f41mtvuD3 Zwy4ljmPVyJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ/TrKgAKCRAEWL7uWMY5 RvBKD/9IFiNL5Ku7pGR+940xcFVTxVcQDOAb53+tx67czlPjUE2rZLyOy1w76quLKPBNzGhtoVE E9vBpKlcqUewiXeKHaJ7zzL6FMyqufbec3labawGa2/VCTb8OSXvunWYeiw9Kk6YqTPVUPE1JeV +/0XqN2MndSJ6DVtm2brRxpaI6Jol5s2kEllSTSe7josC+mZoagHh/hg2Vc+xkL4DqQmcECeFBb Z4LuobiDd2WRCLzxa7R9A5dlSP42//gJPGIWPWb++K4x1JXR+RJrFyLiEcB/XpPl9yU1G3PgupE bEatbY++29jb+VWV3Rb5fch6IXOhlARKcsfvOCetvcbUYwC2Fv8yOdYYTyIUgMeJqQxHIgJ9R3J D3gqTKpjvfYcWpAbjBJJMLQ2AtV672K+04hiFkMIRIXNNf60cetT3bTeURh2mq3+D9ciixsiQHq cpei/QOqXlc/v7+5Ac3Tsu2mtOAclC66hWSOV7QQaBoDG44hQe014DJ2SHz7bNgWInjudyMpw1G 1c7X4X4FCF8dHUUx9wlCe2RjIKJvixKkiJegIUcLmd2qgUcJog/fhqJf1yHAbJQ2nsi2I1bXChy +rwuCzJn8S6tXSEic8/UYTd6w+9D0tfJc5edsiDdL1mkBU3JshxzaH+RGxhkJ3DbwNyQ9Q5s9CF /iKw/aVIiNXmjgA== X-Mailer: b4 0.14.2 Message-ID: <20250408-vma-v16-8-d8b446e885d9@google.com> Subject: [PATCH v16 8/9] task: rust: rework how current is accessed From: Alice Ryhl To: Miguel Ojeda , Matthew Wilcox , Lorenzo Stoakes , Vlastimil Babka , John Hubbard , "Liam R. Howlett" , Andrew Morton , Greg Kroah-Hartman , Arnd Bergmann , Jann Horn , Suren Baghdasaryan Cc: Alex Gaynor , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Introduce a new type called `CurrentTask` that lets you perform various operations that are only safe on the `current` task. Use the new type to provide a way to access the current mm without incrementing its refcount. With this change, you can write stuff such as let vma =3D current!().mm().lock_vma_under_rcu(addr); without incrementing any refcounts. This replaces the existing abstractions for accessing the current pid namespace. With the old approach, every field access to current involves both a macro and a unsafe helper function. The new approach simplifies that to a single safe function on the `CurrentTask` type. This makes it less heavy-weight to add additional current accessors in the future. That said, creating a `CurrentTask` type like the one in this patch requires that we are careful to ensure that it cannot escape the current task or otherwise access things after they are freed. To do this, I declared that it cannot escape the current "task context" where I defined a "task context" as essentially the region in which `current` remains unchanged. So e.g., release_task() or begin_new_exec() would leave the task context. If a userspace thread returns to userspace and later makes another syscall, then I consider the two syscalls to be different task contexts. This allows values stored in that task to be modified between syscalls, even if they're guaranteed to be immutable during a syscall. Ensuring correctness of `CurrentTask` is slightly tricky if we also want the ability to have a safe `kthread_use_mm()` implementation in Rust. To support that safely, there are two patterns we need to ensure are safe: // Case 1: current!() called inside the scope. let mm; kthread_use_mm(some_mm, || { mm =3D current!().mm(); }); drop(some_mm); mm.do_something(); // UAF and: // Case 2: current!() called before the scope. let mm; let task =3D current!(); kthread_use_mm(some_mm, || { mm =3D task.mm(); }); drop(some_mm); mm.do_something(); // UAF The existing `current!()` abstraction already natively prevents the first case: The `&CurrentTask` would be tied to the inner scope, so the borrow-checker ensures that no reference derived from it can escape the scope. Fixing the second case is a bit more tricky. The solution is to essentially pretend that the contents of the scope execute on an different thread, which means that only thread-safe types can cross the boundary. Since `CurrentTask` is marked `NotThreadSafe`, attempts to move it to another thread will fail, and this includes our fake pretend thread boundary. This has the disadvantage that other types that aren't thread-safe for reasons unrelated to `current` also cannot be moved across the `kthread_use_mm()` boundary. I consider this an acceptable tradeoff. Acked-by: Lorenzo Stoakes Acked-by: Liam R. Howlett Reviewed-by: Boqun Feng Reviewed-by: Andreas Hindborg Reviewed-by: Gary Guo Signed-off-by: Alice Ryhl --- rust/kernel/task.rs | 247 +++++++++++++++++++++++++++---------------------= ---- 1 file changed, 129 insertions(+), 118 deletions(-) diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs index 9e6f6854948d9ef9bb203a3548c9b082df8280e2..927413d854846477578cbaf06e2= 7d1fc867d0682 100644 --- a/rust/kernel/task.rs +++ b/rust/kernel/task.rs @@ -7,6 +7,7 @@ use crate::{ bindings, ffi::{c_int, c_long, c_uint}, + mm::MmWithUser, pid_namespace::PidNamespace, types::{ARef, NotThreadSafe, Opaque}, }; @@ -33,22 +34,20 @@ #[macro_export] macro_rules! current { () =3D> { - // SAFETY: Deref + addr-of below create a temporary `TaskRef` that= cannot outlive the - // caller. + // SAFETY: This expression creates a temporary value that is dropp= ed at the end of the + // caller's scope. The following mechanisms ensure that the result= ing `&CurrentTask` cannot + // leave current task context: + // + // * To return to userspace, the caller must leave the current sco= pe. + // * Operations such as `begin_new_exec()` are necessarily unsafe = and the caller of + // `begin_new_exec()` is responsible for safety. + // * Rust abstractions for things such as a `kthread_use_mm()` sco= pe must require the + // closure to be `Send`, so the `NotThreadSafe` field of `Curren= tTask` ensures that the + // `&CurrentTask` cannot cross the scope in either direction. unsafe { &*$crate::task::Task::current() } }; } =20 -/// Returns the currently running task's pid namespace. -#[macro_export] -macro_rules! current_pid_ns { - () =3D> { - // SAFETY: Deref + addr-of below create a temporary `PidNamespaceR= ef` that cannot outlive - // the caller. - unsafe { &*$crate::task::Task::current_pid_ns() } - }; -} - /// Wraps the kernel's `struct task_struct`. /// /// # Invariants @@ -87,7 +86,7 @@ macro_rules! current_pid_ns { /// impl State { /// fn new() -> Self { /// Self { -/// creator: current!().into(), +/// creator: ARef::from(&**current!()), /// index: 0, /// } /// } @@ -107,6 +106,44 @@ unsafe impl Send for Task {} // synchronised by C code (e.g., `signal_pending`). unsafe impl Sync for Task {} =20 +/// Represents the [`Task`] in the `current` global. +/// +/// This type exists to provide more efficient operations that are only va= lid on the current task. +/// For example, to retrieve the pid-namespace of a task, you must use rcu= protection unless it is +/// the current task. +/// +/// # Invariants +/// +/// Each value of this type must only be accessed from the task context it= was created within. +/// +/// Of course, every thread is in a different task context, but for the pu= rposes of this invariant, +/// these operations also permanently leave the task context: +/// +/// * Returning to userspace from system call context. +/// * Calling `release_task()`. +/// * Calling `begin_new_exec()` in a binary format loader. +/// +/// Other operations temporarily create a new sub-context: +/// +/// * Calling `kthread_use_mm()` creates a new context, and `kthread_unuse= _mm()` returns to the +/// old context. +/// +/// This means that a `CurrentTask` obtained before a `kthread_use_mm()` c= all may be used again +/// once `kthread_unuse_mm()` is called, but it must not be used between t= hese two calls. +/// Conversely, a `CurrentTask` obtained between a `kthread_use_mm()`/`kth= read_unuse_mm()` pair +/// must not be used after `kthread_unuse_mm()`. +#[repr(transparent)] +pub struct CurrentTask(Task, NotThreadSafe); + +// Make all `Task` methods available on `CurrentTask`. +impl Deref for CurrentTask { + type Target =3D Task; + #[inline] + fn deref(&self) -> &Task { + &self.0 + } +} + /// The type of process identifiers (PIDs). pub type Pid =3D bindings::pid_t; =20 @@ -133,119 +170,29 @@ pub fn current_raw() -> *mut bindings::task_struct { /// /// # Safety /// - /// Callers must ensure that the returned object doesn't outlive the c= urrent task/thread. - pub unsafe fn current() -> impl Deref { - struct TaskRef<'a> { - task: &'a Task, - _not_send: NotThreadSafe, + /// Callers must ensure that the returned object is only used to acces= s a [`CurrentTask`] + /// within the task context that was active when this function was cal= led. For more details, + /// see the invariants section for [`CurrentTask`]. + pub unsafe fn current() -> impl Deref { + struct TaskRef { + task: *const CurrentTask, } =20 - impl Deref for TaskRef<'_> { - type Target =3D Task; + impl Deref for TaskRef { + type Target =3D CurrentTask; =20 fn deref(&self) -> &Self::Target { - self.task + // SAFETY: The returned reference borrows from this `TaskR= ef`, so it cannot outlive + // the `TaskRef`, which the caller of `Task::current()` ha= s promised will not + // outlive the task/thread for which `self.task` is the `c= urrent` pointer. Thus, it + // is okay to return a `CurrentTask` reference here. + unsafe { &*self.task } } } =20 - let current =3D Task::current_raw(); TaskRef { - // SAFETY: If the current thread is still running, the current= task is valid. Given - // that `TaskRef` is not `Send`, we know it cannot be transfer= red to another thread - // (where it could potentially outlive the caller). - task: unsafe { &*current.cast() }, - _not_send: NotThreadSafe, - } - } - - /// Returns a PidNamespace reference for the currently executing task'= s/thread's pid namespace. - /// - /// This function can be used to create an unbounded lifetime by e.g.,= storing the returned - /// PidNamespace in a global variable which would be a bug. So the rec= ommended way to get the - /// current task's/thread's pid namespace is to use the [`current_pid_= ns`] macro because it is - /// safe. - /// - /// # Safety - /// - /// Callers must ensure that the returned object doesn't outlive the c= urrent task/thread. - pub unsafe fn current_pid_ns() -> impl Deref { - struct PidNamespaceRef<'a> { - task: &'a PidNamespace, - _not_send: NotThreadSafe, - } - - impl Deref for PidNamespaceRef<'_> { - type Target =3D PidNamespace; - - fn deref(&self) -> &Self::Target { - self.task - } - } - - // The lifetime of `PidNamespace` is bound to `Task` and `struct p= id`. - // - // The `PidNamespace` of a `Task` doesn't ever change once the `Ta= sk` is alive. A - // `unshare(CLONE_NEWPID)` or `setns(fd_pidns/pidfd, CLONE_NEWPID)= ` will not have an effect - // on the calling `Task`'s pid namespace. It will only effect the = pid namespace of children - // created by the calling `Task`. This invariant guarantees that a= fter having acquired a - // reference to a `Task`'s pid namespace it will remain unchanged. - // - // When a task has exited and been reaped `release_task()` will be= called. This will set - // the `PidNamespace` of the task to `NULL`. So retrieving the `Pi= dNamespace` of a task - // that is dead will return `NULL`. Note, that neither holding the= RCU lock nor holding a - // referencing count to - // the `Task` will prevent `release_task()` being called. - // - // In order to retrieve the `PidNamespace` of a `Task` the `task_a= ctive_pid_ns()` function - // can be used. There are two cases to consider: - // - // (1) retrieving the `PidNamespace` of the `current` task - // (2) retrieving the `PidNamespace` of a non-`current` task - // - // From system call context retrieving the `PidNamespace` for case= (1) is always safe and - // requires neither RCU locking nor a reference count to be held. = Retrieving the - // `PidNamespace` after `release_task()` for current will return `= NULL` but no codepath - // like that is exposed to Rust. - // - // Retrieving the `PidNamespace` from system call context for (2) = requires RCU protection. - // Accessing `PidNamespace` outside of RCU protection requires a r= eference count that - // must've been acquired while holding the RCU lock. Note that acc= essing a non-`current` - // task means `NULL` can be returned as the non-`current` task cou= ld have already passed - // through `release_task()`. - // - // To retrieve (1) the `current_pid_ns!()` macro should be used wh= ich ensure that the - // returned `PidNamespace` cannot outlive the calling scope. The a= ssociated - // `current_pid_ns()` function should not be called directly as it= could be abused to - // created an unbounded lifetime for `PidNamespace`. The `current_= pid_ns!()` macro allows - // Rust to handle the common case of accessing `current`'s `PidNam= espace` without RCU - // protection and without having to acquire a reference count. - // - // For (2) the `task_get_pid_ns()` method must be used. This will = always acquire a - // reference on `PidNamespace` and will return an `Option` to forc= e the caller to - // explicitly handle the case where `PidNamespace` is `None`, some= thing that tends to be - // forgotten when doing the equivalent operation in `C`. Missing R= CU primitives make it - // difficult to perform operations that are otherwise safe without= holding a reference - // count as long as RCU protection is guaranteed. But it is not im= portant currently. But we - // do want it in the future. - // - // Note for (2) the required RCU protection around calling `task_a= ctive_pid_ns()` - // synchronizes against putting the last reference of the associat= ed `struct pid` of - // `task->thread_pid`. The `struct pid` stored in that field is us= ed to retrieve the - // `PidNamespace` of the caller. When `release_task()` is called `= task->thread_pid` will be - // `NULL`ed and `put_pid()` on said `struct pid` will be delayed i= n `free_pid()` via - // `call_rcu()` allowing everyone with an RCU protected access to = the `struct pid` acquired - // from `task->thread_pid` to finish. - // - // SAFETY: The current task's pid namespace is valid as long as th= e current task is running. - let pidns =3D unsafe { bindings::task_active_pid_ns(Task::current_= raw()) }; - PidNamespaceRef { - // SAFETY: If the current thread is still running, the current= task and its associated - // pid namespace are valid. `PidNamespaceRef` is not `Send`, s= o we know it cannot be - // transferred to another thread (where it could potentially o= utlive the current - // `Task`). The caller needs to ensure that the PidNamespaceRe= f doesn't outlive the - // current task/thread. - task: unsafe { PidNamespace::from_ptr(pidns) }, - _not_send: NotThreadSafe, + // CAST: The layout of `struct task_struct` and `CurrentTask` = is identical. + task: Task::current_raw().cast(), } } =20 @@ -328,6 +275,70 @@ pub fn wake_up(&self) { } } =20 +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 =3D unsafe { (*self.as_ptr()).mm }; + + if mm.is_null() { + return None; + } + + // SAFETY: If `current->mm` is non-null, then it references a vali= d mm with a non-zero + // value of `mm_users`. Furthermore, the returned `&MmWithUser` bo= rrows from this + // `CurrentTask`, so it cannot escape the scope in which the curre= nt pointer was obtained. + // + // This is safe even if `kthread_use_mm()`/`kthread_unuse_mm()` ar= e 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()` s= cope 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 t= he returned `&MmWithUser` + // also cannot escape that scope. + // In either case, it's not possible to read `current->mm` and kee= p using it after the + // scope is ended with `kthread_unuse_mm()`. + Some(unsafe { MmWithUser::from_raw(mm) }) + } + + /// Access the pid namespace of the current task. + /// + /// This function does not touch the refcount of the namespace or use = RCU protection. + /// + /// To access the pid namespace of another task, see [`Task::get_pid_n= s`]. + #[doc(alias =3D "task_active_pid_ns")] + #[inline] + pub fn active_pid_ns(&self) -> Option<&PidNamespace> { + // SAFETY: It is safe to call `task_active_pid_ns` without RCU pro= tection when calling it + // on the current task. + let active_ns =3D unsafe { bindings::task_active_pid_ns(self.as_pt= r()) }; + + if active_ns.is_null() { + return None; + } + + // The lifetime of `PidNamespace` is bound to `Task` and `struct p= id`. + // + // The `PidNamespace` of a `Task` doesn't ever change once the `Ta= sk` is alive. + // + // From system call context retrieving the `PidNamespace` for the = current task is always + // safe and requires neither RCU locking nor a reference count to = be held. Retrieving the + // `PidNamespace` after `release_task()` for current will return `= NULL` but no codepath + // like that is exposed to Rust. + // + // SAFETY: If `current`'s pid ns is non-null, then it references a= valid pid ns. + // Furthermore, the returned `&PidNamespace` borrows from this `Cu= rrentTask`, so it cannot + // escape the scope in which the current pointer was obtained, e.g= . it cannot live past a + // `release_task()` call. + Some(unsafe { PidNamespace::from_ptr(active_ns) }) + } +} + // SAFETY: The type invariants guarantee that `Task` is always refcounted. unsafe impl crate::types::AlwaysRefCounted for Task { fn inc_ref(&self) { --=20 2.49.0.504.g3bcea36a83-goog From nobody Fri Dec 19 17:31:11 2025 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8C472266B59 for ; Tue, 8 Apr 2025 09:24:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744104261; cv=none; b=N6gzLabbn0WANBZvTWH/Bom5MSbSmXd7wsf4CsusmZBK5chbiiy/+obE2ryvA/ilGBpCzGk5z0t4co4zUjD9LL1jPtZf/Qzxgr9++hpOsmXRJ9vmCBIksrbeSMer1sXinsLfJ/5ONv1PlU0AOAx1Xc2EYeYNMuiZOmccJX//478= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744104261; c=relaxed/simple; bh=m2/7sbOCWUMVifIeec7mgQC4DqxO7715a5mxnPiGobY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=j8mFdZwUZOrCzqYGfLj1N/STDmEiYJmd3twY9Jx631q9iOlaKaF/DbhBKahY6iI/UXlm87u8eyV1WRFfR2gqIfoWjBtfAQ57oVkH15HZB/0eZJSOFfLcF/0q3AeOjGw+mCm7JIemDg3XYcn5m4jQKFRNmpQW42SLpMiNCMugoDU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=FgsL14MU; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="FgsL14MU" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43cf172ff63so30326365e9.3 for ; Tue, 08 Apr 2025 02:24:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744104258; x=1744709058; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=iIuImScQv8Fi6Jr5gBxEmMobPhodk5GrITEtyDe/zuI=; b=FgsL14MUrSNNY/o70yitmv78lTdvOE8aQfzA4g81slbdxLorG9ZWzpYVwwowO6Nu4t YAkxRTZlgxa1GskJyWDHUtT2fIBd+mYNofRX2ZfM4oc5AMZaNkmeVnb/Qf8ByoSssgm2 oiFQ1yPOHDwHjIy5o49Oa5mqooxty3rT2HTJhmSANqX4VLnuvGoDzuqhmJFn488umBbV 2cSn5nnUL6zEcpq8oZn8Fk710VKJ6Klo5oVzzXLCVcMy7juR3cYh5uGNqa/vEgyD2zQ5 AKLqVOmAPZHXgthGqUirybWLnYD6LBdJFF95KKyG2YVDFllybKdEyPwJHNO0cuciEgZT 6IDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744104258; x=1744709058; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=iIuImScQv8Fi6Jr5gBxEmMobPhodk5GrITEtyDe/zuI=; b=Bg8U4QDATvfzb703/68i4KqHQk+x+Z2mw5x+/OPZCUD6UCr1pXbcBeBnVfxdBPgUR7 5QTYepX58ob2T1lbYWFEK+TiVIcnFWoo9kfpkb7OK/bq1HDpeLLkscxNxALkpWWb8Ze3 U/CC7TX17LyZRIqDmHb0VA66KYqT6TcSIrbQIyPAhCJqO3tV8v6uNp0JlB/RNoVVecnI lMvMRwpGahxPQo3tK+fUymOjBR1eK+OCDAxkikQdvU1X6adC0If9Lu6RRVUZREx7BZB/ N/eeF8GasyqMgHGwtIW7p2eZGY+bADiVC25cGB1lSiogclknEVXGTc8pVMw7z4l6ppBT VQNg== X-Forwarded-Encrypted: i=1; AJvYcCV3NyI5/L8emf1nk5OFFpfOECCB3x6ifrRZZVqaq/+h2H15OAj/xDW3dvh5swycVp+t2hPe2qi97wypqtc=@vger.kernel.org X-Gm-Message-State: AOJu0Yw5mq9zeyyZbnZEspnGB2VtLdeN2Lqbc7K2gnPq2InuY1pis/lX Zps+pTGpIj5WIjG545/g2cS9l2A4nUfgJvwAKoRitjuK49lZ/CHs/VzJdf16wUDRJKMuaG5IxO6 PdCpc0jEnCbhJ5Q== X-Google-Smtp-Source: AGHT+IFWc1pMQ2RJ9ij6FdvJcqN5KNDRiADu9dL8SpaDl5jyOXZP0BgOscC21FHTrRxiw3z04BlYLCIylAI56hA= X-Received: from wmsd15.prod.google.com ([2002:a05:600c:3acf:b0:43b:c9fc:b269]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1f86:b0:43d:fa58:81d3 with SMTP id 5b1f17b1804b1-43ecfa06ab0mr117784265e9.32.1744104258002; Tue, 08 Apr 2025 02:24:18 -0700 (PDT) Date: Tue, 08 Apr 2025 09:22:46 +0000 In-Reply-To: <20250408-vma-v16-0-d8b446e885d9@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250408-vma-v16-0-d8b446e885d9@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=1514; i=aliceryhl@google.com; h=from:subject:message-id; bh=m2/7sbOCWUMVifIeec7mgQC4DqxO7715a5mxnPiGobY=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBn9Osrq11PicKCteCb0qlte1JemOCFeETkH6Ll/ cnSpgmrbU+JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ/TrKwAKCRAEWL7uWMY5 RrTVEACTYV04bI/LPuLa9aALNkxEoDBC1dWns2op8JO02wLn1lwgfSVvYpy5ovBu+j7vTYD0Cfk KUPqzTfANnDnX6sBBckjHVgp/cP2bBFJFnVoad8EAP6vIWMqPkUSsNwI2la/g2vyhYzv7qnlWFM g2NZURTwzQq0Nti9cZ8V+IRWNHs8UqTxuf5exDhhF2CDeAj0ROwndmuSioqrSxy8knvimJ5HVrO dJUDn5oHKBvfmCsTMQtuhmuJId6JU53fD/sl2tZ539PAZ8jsfac57wvCxcrMs0HNtN4wdvqiJCn rTEU2gb91keNCTDfzqRHTRfPTlC5N6w8Op0vK/TtkdGOGV5llD4wjpDHc8i6ct7kvoTl1HD5yg4 JRfP8ZALtRQ0sOBWDRG4ixtU/Ura42hA39xatzz8eh1XTz93SRWIelsc/fVkNKShyNgkCyWPpvp gPdQsJhF53ztGLMGDq/if2wtl6qKgcRJPHLRWkkFoWjwb69a/OTOQ0mRopSVJxJxxoXVfwv6iFP oBVwj/tXju7f3HDpnlhvi3NZfB4/jTLdhRY+XEbAFM4OB7kLCDwocD78AylKcQGM4WLhCWURRSI f6/u0CZ1ihQ2vnm3wUGgHBg9TVIJH07635glB9apUJ3aiJZXV6QilE8pJDxsSnyO4NbSmdImtqn EsSKTX6/hCNw5oQ== X-Mailer: b4 0.14.2 Message-ID: <20250408-vma-v16-9-d8b446e885d9@google.com> Subject: [PATCH v16 9/9] mm: rust: add MEMORY MANAGEMENT [RUST] From: Alice Ryhl To: Miguel Ojeda , Matthew Wilcox , Lorenzo Stoakes , Vlastimil Babka , John Hubbard , "Liam R. Howlett" , Andrew Morton , Greg Kroah-Hartman , Arnd Bergmann , Jann Horn , Suren Baghdasaryan Cc: Alex Gaynor , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable We have introduced Rust bindings for core mm abstractions as part of this series, so add an entry in MAINTAINERS to be explicit about who maintains this. Patches are anticipated to be taken through the mm tree as usual with other mm code. Link: https://rust-for-linux.com/rust-kernel-policy#how-is-rust-introduced-= in-a-subsystem Link: https://lore.kernel.org/all/33e64b12-aa07-4e78-933a-b07c37ff1d84@luci= fer.local/ Acked-by: Lorenzo Stoakes Acked-by: Liam R. Howlett Signed-off-by: Alice Ryhl --- MAINTAINERS | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 96b82704950184bd71623ff41fc4df31e4c7fe87..9f661903723556a309434f83251= caba9dd3e0699 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -15529,6 +15529,19 @@ F: include/uapi/linux/userfaultfd.h F: mm/userfaultfd.c F: tools/testing/selftests/mm/uffd-*.[ch] =20 +MEMORY MANAGEMENT - RUST +M: Alice Ryhl +R: Lorenzo Stoakes +R: Liam R. Howlett +L: linux-mm@kvack.org +L: rust-for-linux@vger.kernel.org +S: Maintained +W: http://www.linux-mm.org +T: git git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm +F: rust/helpers/mm.c +F: rust/kernel/mm.rs +F: rust/kernel/mm/ + MEMORY MAPPING M: Andrew Morton M: Liam R. Howlett --=20 2.49.0.504.g3bcea36a83-goog