From patchwork Wed Nov 20 14:49:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13881312 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C6651D711A7 for ; Wed, 20 Nov 2024 14:56:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5030F6B0095; Wed, 20 Nov 2024 09:56:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4B2FE6B0096; Wed, 20 Nov 2024 09:56:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 306F36B0098; Wed, 20 Nov 2024 09:56:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 0D44C6B0095 for ; Wed, 20 Nov 2024 09:56:22 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 9FEB5C0CE4 for ; Wed, 20 Nov 2024 14:56:21 +0000 (UTC) X-FDA: 82806773616.02.D1F7E7F Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) by imf05.hostedemail.com (Postfix) with ESMTP id 0EABF100011 for ; Wed, 20 Nov 2024 14:54:41 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Dcb7Meui; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of 3kfg9ZwkKCLofqnhjw3mqlttlqj.htrqnsz2-rrp0fhp.twl@flex--aliceryhl.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3kfg9ZwkKCLofqnhjw3mqlttlqj.htrqnsz2-rrp0fhp.twl@flex--aliceryhl.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732114394; a=rsa-sha256; cv=none; b=W+KcJ1DPsedb2DZGz4M5TpL/UgTKYkV6JqBUtbp9nks5rbjgkVVtbDksGRohJPIMp4EuO9 vthqBkf4kcr5AX54O+1B0MKM0oCmqjE/hsHBPdR3ib10rUe6tVlfGkqQrWILlhhXsCq650 PiKQGWKvaIrhA5Wa9l90ZTqe/NmUbJY= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Dcb7Meui; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of 3kfg9ZwkKCLofqnhjw3mqlttlqj.htrqnsz2-rrp0fhp.twl@flex--aliceryhl.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3kfg9ZwkKCLofqnhjw3mqlttlqj.htrqnsz2-rrp0fhp.twl@flex--aliceryhl.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732114394; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=iRAvnS0IaCWBMzdRyDGxGfoX2WP3nXVUoH5LBPSc5Qs=; b=J+bDuZLzco3wGRc4x4qGrGRUsodjUGqfnSlGPP/uZULy6K9R0DCsat8K+ivXQUM0o/mDx0 QDgNVYHAUiTWpebnLdBKSPPxDFGFMKcl3Gc20ma5efaHu6zIHJwETQeE3Evh8QhR2jfBGx ZDziwHrHCN87hCqLqVadx/Be3GzULAo= Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-38243a4ba5fso3103710f8f.2 for ; Wed, 20 Nov 2024 06:56:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732114578; x=1732719378; darn=kvack.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=iRAvnS0IaCWBMzdRyDGxGfoX2WP3nXVUoH5LBPSc5Qs=; b=Dcb7MeuiBQ5qWwZ15Scm1bFCa8Se+62MXIhVvOyCUCTEiGrkRQRXoAHFrYUlEbsj3V ooiWaoNExgTJbZjJ1EVRE6LE8mUOFvnIvE9U0LZuIj698pkjar7eLyFzog5F4kV5af9b c6w4LWgsD+gyfzW42fV73cb3axVe6rnci4+4jVMb4t8eK/JRiFpdXAHzgg+bmTpqO6rY ZAodf8Utg03dgLSWjndXcJ9lmkwgSMCVygpsLHfWOHZ+TtD/apMZ5AzB5K1Ge/HowI5i TB1WYCyW9d6CTIhNe3QFrTGoOSHo5RcNbqSpNdC4GGSEAGbMVKWOzdcL9AJhph6Jk6V9 5glw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732114578; x=1732719378; 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=iRAvnS0IaCWBMzdRyDGxGfoX2WP3nXVUoH5LBPSc5Qs=; b=QQ08kpn6ZFh7K0AuY+3rpyoX9x+R75mu92JkZC0MHZbvFHXf3YLLpMuV9eVcYUImJs z3vj8nAiuIH+4A6cnqhD3jMIUxRUPdHL6bCWHOVP+STIKYZsGJzb/RIL8HDjQpCrGF/P bG8P2Zgzh6bM9Vrh/jB2TmHKJXzBX+WQfBZJ017Q35bAylqJAHOFAmFqYbA92qFwtt5j D6i+xpbr+758mPq4my8H88SGfu5ZNAtNu9gdFfA+1xbQWLfirQC4rPtUw5I69VDFruo4 Tf5ka3DNZS43WVeLE+LEvHZGMt1zegB3rHBhmLe2YSjjwt/nOCwpzNt0/tWgkNgbe5g3 tPew== X-Forwarded-Encrypted: i=1; AJvYcCVS4kow23QELHVA/jU8L4eHL43aWc3tS4AfECCc8D0L5qUgmx5LYweOzdZtLC6jZtzcbnwKgRo0Tg==@kvack.org X-Gm-Message-State: AOJu0Yz6Dt0N2viVpBwmSRi1MUrLFJZvhqDS53aMvcSEeuN7bxwff0JE 7Al+cDIFa9Kba4YsnPXbjll986HdGiIySMBzT1sZHQnzOBTK9/EKr9wXAVjzQeyHkjP6oNFAQSz N3luwLUCaBWC3JQ== X-Google-Smtp-Source: AGHT+IGq/r3HGbbtFoFVRvHr1EOlzN/Wt8+om4kWd42V+YspQZes17Yps3LGccfMBIlhtc72415J8+GJZmwAm9s= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:68:fe9:ac10:f29e]) (user=aliceryhl job=sendgmr) by 2002:a05:6000:cf:b0:37d:5467:4008 with SMTP id ffacd0b85a97d-38254b18ad3mr818f8f.5.1732114577963; Wed, 20 Nov 2024 06:56:17 -0800 (PST) Date: Wed, 20 Nov 2024 14:49:55 +0000 In-Reply-To: <20241120-vma-v8-0-eb31425da66b@google.com> Mime-Version: 1.0 References: <20241120-vma-v8-0-eb31425da66b@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=9857; i=aliceryhl@google.com; h=from:subject:message-id; bh=thHW2hYjZXfFchwxaSHY31jS6Gxjf/8txx6XeVPPA40=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnPfiIPfGxtRmuM/beITcG2729I1pZZuD2y71Hm a5hLFdiTyWJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZz34iAAKCRAEWL7uWMY5 RrM9EAChZwI5X5J59zvpP7My8BG3gVmNliYXLaMF7DdF13YYqlfRr6Nw3FK7e+/5u8oyjxwjLDq i3Aerdaf3lQlSYU3T8YKbVn4UKhByCAgIggWEbwvt8y0OPSLqBJ8gzZ8qYHcluaqHVB6En/S47l 7lsU2P1ozQStRB699rZdudrnMkzcxUw7oPTdb4oj33RV6AN3B7WuiPf/9FXT+L99Mz3fDdjhyu/ DZFg5TzMI7PXpxVysdNfctskP27Y+Z/4W/wr534zWMVNHWwURuyfzMFgxpA68y6YgmeIp7jBp80 h2+3Y4/WFCfH7dtvc2a3W7vdheRq8OqP3hIU7j85so4RpYZrLRFWJTsI6ksb8Tx6YatClhaN8xs QqZWzy67zmvq6CD9bNdc8iSGKicFWYqL3CRAK0dPh1iKzItCU/VYo6NDfKDVPKcJGCoeRa1mny2 klw3Ap8CxgCgpmRPo9MKuHZre8uhJo7HBYPJMqNjVqTzWUGkHHap3fIa/pW/02sBXfteKVY4BsY ixHgtqTqQsvDkUGNy7QnWKGvdDAWJel/bBeazpquvJIDptb99myFDl581bVHCit1O/7vW+4egEw CeMAkJ7joMJHpeK7O9KH0ek1UkNOypcg1cM4iLNqxpYvpGAVDO0IA41NZEWF3DyHOy4fl4KJhlt xSzwOusQ+/1n5aA== X-Mailer: b4 0.13.0 Message-ID: <20241120-vma-v8-1-eb31425da66b@google.com> Subject: [PATCH v8 1/7] 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 , Christian Brauner Cc: Alex Gaynor , Boqun Feng , Gary Guo , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Alice Ryhl , Andreas Hindborg X-Stat-Signature: 6qhbhmg3oqar6as8xekcw6dgzh7biq7z X-Rspamd-Queue-Id: 0EABF100011 X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1732114481-96268 X-HE-Meta: U2FsdGVkX18Dun4iHuCp462bi9A/Kw6P1KyU/mriVP7rcGk3O6yjA10eE89xiMowjQuGMmLvnuhtHvvJLNzGVnDYDBRKkVIWsvdZSKw9ixewgNfpOn0jQYY6cYHYDBjkSVOyCRc9RPSs3Iq6c26tVzyBsCTnYBLUhhC06can+QJxI2R49zAgxAB/pIvxgcEMrCjWf3XgfHu4ZfnSLHEptTFGPfYockD7afwXO+SHiK1bXhLECYDBg5/qy3tIpt1WuuGuGLN6PA0kdrorhAle0OqdQJLROMjkzQOWJBxiFISbgiuQ0/2e36CMO241+Yp1VZLyxerZ5CLV4WHtmJIIFRTiqQ+ObbUcgXGXrDlIqfn4mSc0vcNhwi3rV4swwdcuIGGmfjCyjQIhRfT4mOKHuwQ3R5pM5hdhxo19UmSvi2Dwy8ASTayOE2UfnoLUbviR2wnuCv1FCZ2ffSeGDWyfpOHGipuLVKuUMxm+g7lQT86RoHesWPadkArNCnR3PCBzTGvudTbBS4TaNIYNe9f0JZYhQSe483FlTAtWW86MKEezZd0s8Hosd7CRpBqKPMBupbsZp1+tsCzVOWphw8QXxeBV0ywEOoPVudkVO9XQ59e93KaQp1feuvAMqv9dGQb0vZfsomFCBgpgwCT1ZVU5jmh7hN2IZfY3w/chMo81OX9AkjA/BCVhUgea2jacciBNshNM5kdwLJ8HjjrfEHXGOIWGUJDLdWZltpo+0v2r5cTyTlW2wJUsXROAyEHcQb9jGyfjlQqmPSOKR0VoWmnKDmK4dBSuE/8G8qkG6zvAEKDtFRz2lAMywKU1QbwLcFOVt2XJOK58OyRhpp/i06UGjl/ZzlmkINVaTvCka+f10X5HXsSTIfQJwLL9U9Xai+va8wbyeqfFQK18tYF7yfgWUtOEDcorwGi0OUmpoh7cRPewLfJ3f8v0lBAwHwBD1c8/Ttjk7Wzg6t+t8OSBTq4 oPVEfcMM QOJCGNEf94SrwwgTq3osXEBNDx2GxVvbgLwBMzwaojFLUUw2UZ4W8hNVJFE3ZOl/3fI3OCUOVhIBw5Bz0RCjNH54SR5scPByrvaf75m/Y/5immEOlYloJLQhetbdevI2OJQhTijMdX0av3JjYDerPAg+s4GQxv/mA8GDJE7zBMEmGtgnkGLuFUYUjt+rb6I7wvSAEgBolvZAFt6eOplYAMxqo1qLD0j1YFsztpNOCX4gVVTV0DcCdeu/Lf01lg9Aax2UMZxF0y0l0OavAZKwn2NcZyeRoKSyAlD/HShuHXFdaMmnakbNVVcvbA4lwSOD+IdqJ417iaDZrhxnL+q2y71GZwnT+JvOJZ0Z6Piu5HUC2jytig2kfy8fntlcMjiek2cpQSifPeHpnay3v7C4BmSHaZVGdEO7ciRfB+nngvCG5+jBMIhyYCPcSCMfLa0Tgvwoo/eO/x273phJQ5rbJJlVwTPKbjftlJi2lLn/bPQmiwRAI9Emnnt/n17UTbQkLk+Hm/ll3pBZBvb9gx9eDJBBE4+RqFXFzyfmRgr6MoTIXYAGxkxFQjWBJhyeYj4DfbPm1AeicvpL7x1vQUL+rR87WVj68vJIkCiUEhvuNSoYb7fkcIHi8okgv9V19K+K1eBeBYrKF6/2wTtxHGh3ZAhMPQW7aZTCPx6LVuBN346vHqcNQ85dotY6N1Q== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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. Signed-off-by: Alice Ryhl --- rust/helpers/helpers.c | 1 + rust/helpers/mm.c | 39 +++++++++ rust/kernel/lib.rs | 1 + rust/kernel/mm.rs | 219 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 260 insertions(+) diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c index 20a0c69d5cc7..60a488eb4efe 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -13,6 +13,7 @@ #include "build_bug.c" #include "err.c" #include "kunit.c" +#include "mm.c" #include "mutex.c" #include "page.c" #include "rbtree.c" diff --git a/rust/helpers/mm.c b/rust/helpers/mm.c new file mode 100644 index 000000000000..7201747a5d31 --- /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 66f5cde7f322..cc1963510cdf 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -43,6 +43,7 @@ pub mod kunit; pub mod list; pub mod miscdevice; +pub mod mm; #[cfg(CONFIG_NET)] pub mod net; pub mod page; diff --git a/rust/kernel/mm.rs b/rust/kernel/mm.rs new file mode 100644 index 000000000000..84cba581edaa --- /dev/null +++ b/rust/kernel/mm.rs @@ -0,0 +1,219 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2024 Google LLC. + +//! Memory management. +//! +//! C header: [`include/linux/mm.h`](srctree/include/linux/mm.h) + +use crate::{ + bindings, + types::{ARef, AlwaysRefCounted, NotThreadSafe, Opaque}, +}; +use core::{ops::Deref, ptr::NonNull}; + +/// A wrapper for the kernel's `struct mm_struct`. +/// +/// Since `mm_users` may be zero, the associated address space may not exist anymore. You can use +/// [`mmget_not_zero`] to be able to access the address space. +/// +/// The `ARef` smart pointer holds an `mmgrab` refcount. Its destructor 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 `mmgrab` was called. +unsafe impl Send for Mm {} +// SAFETY: All methods on `Mm` can be called in parallel from several threads. +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 destructor 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 several 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 = Mm; + + #[inline] + fn deref(&self) -> &Mm { + &self.mm + } +} + +// These methods are safe to call even if `mm_users` is zero. +impl Mm { + /// Call `mmgrab` on `current.mm`. + #[inline] + pub fn mmgrab_current() -> Option> { + // SAFETY: It's safe to get the `mm` field from current. + let mm = unsafe { + let current = bindings::get_current(); + (*current).mm + }; + + if mm.is_null() { + return None; + } + + // SAFETY: The value of `current->mm` is guaranteed to be null or a valid `mm_struct`. We + // just checked that it's not null. Furthermore, the returned `&Mm` is valid only for the + // duration of this function, and `current->mm` will stay valid for that long. + let mm = unsafe { Mm::from_raw(mm) }; + + // This increments the refcount using `mmgrab`. + Some(ARef::from(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 that 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. Layouts 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 = 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_raw().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 that `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 MmWithUser { + // SAFETY: Caller promises that the pointer is valid for 'a. The layout 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 = 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()) }; + } +} From patchwork Wed Nov 20 14:49:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13881313 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 37657D711A8 for ; Wed, 20 Nov 2024 14:56:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B93056B0096; Wed, 20 Nov 2024 09:56:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B1DF46B0098; Wed, 20 Nov 2024 09:56:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8F9866B0099; Wed, 20 Nov 2024 09:56:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 6ACF16B0096 for ; Wed, 20 Nov 2024 09:56:24 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 53D8F160D04 for ; Wed, 20 Nov 2024 14:56:23 +0000 (UTC) X-FDA: 82806772272.25.EFE8EC0 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf09.hostedemail.com (Postfix) with ESMTP id C266E14001C for ; Wed, 20 Nov 2024 14:55:45 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=DAxYbkw2; spf=pass (imf09.hostedemail.com: domain of 3lPg9ZwkKCL0dolfhu1kojrrjoh.frpolqx0-ppnydfn.ruj@flex--aliceryhl.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3lPg9ZwkKCL0dolfhu1kojrrjoh.frpolqx0-ppnydfn.ruj@flex--aliceryhl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732114489; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hUP5pKIRXgXTZOS13MJMWQ9DFpxOfGZ7r+UgNJvVuSI=; b=dR4UZmZi9Q2seQ5dZX3k6djAo2CVn/dimhBwMWstP7RRMDAhE704jTFZTXNaRnPOgeSCLm HBOQe+lKYQ5PS7bqhnKki48jeimpXB2FJSwCAVBNO+cd4GaetNtF31MN7DjAtLj+Vhn8V4 OnXPT/bDq9gNkm6YITFOdqkFXuXMVQs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732114489; a=rsa-sha256; cv=none; b=mkcozUlOJvp0DxF4Je6yESZKnNQyYadwil85bUgbdcTXbsNBi7foMih4d+CiPgCH8Gng6W N2X/+fURVPnnhYJVx+Sff1l+ll7ChX45VssLd67nzsA1Owbu8jBpTcqVS4doM6CbjXoJGQ GC0Jusj84I/eDTzdme/S/WauKVBDqZ8= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=DAxYbkw2; spf=pass (imf09.hostedemail.com: domain of 3lPg9ZwkKCL0dolfhu1kojrrjoh.frpolqx0-ppnydfn.ruj@flex--aliceryhl.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3lPg9ZwkKCL0dolfhu1kojrrjoh.frpolqx0-ppnydfn.ruj@flex--aliceryhl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6ea863ecfe9so41305657b3.3 for ; Wed, 20 Nov 2024 06:56:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732114580; x=1732719380; darn=kvack.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=hUP5pKIRXgXTZOS13MJMWQ9DFpxOfGZ7r+UgNJvVuSI=; b=DAxYbkw2FtuVCMB68ukxp7uf2X4mKHN9tWktowanAUhVtS/yUe7Y1tr/oi6ntRuoev nLPOh6Y9OqGbOsYPfwE03DfmL1fLlCETKXRb9nQwzB24+37qvQVN0JOA41ydtfe1t7M3 066yLKCDL0p+oanSWn/VFokgsDJ8yLku0r5V0iO0zmMqIUFfHTLsMYVsfQJ8fWuIumDG 1jIj16fnyXcOyA5Tuxk5uaC+OgXWnA5DJxQQ5ZP588jMpJaNV2y2pbHJO2Nj4SuPK8xq CXW3KUTLZUI7W7Ar2S0O/gV1gr9a6VP8qHun88jS4H3EVlz/t3uktGXwhnaiyqRMpfWF LACQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732114580; x=1732719380; 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=hUP5pKIRXgXTZOS13MJMWQ9DFpxOfGZ7r+UgNJvVuSI=; b=DzVx1uXQJ1uAgnnATC0T8/Xwh+L30dsqI/wxTn9p8YwhoBcq9ur4z0MrbGO8H4hqsD OrNkt0dMRc2BVRVrM2x3TJ2TeSc2YMWGIt7gGNhiD/OApzqcTMvSDi07qDJcpdJFIrxn PXwohv2MC7f9EV7ogIL3fH4qZoe9/10INqM9CPAhjF/BPDyIpItqK/zfneip5Vk1hGlt J5/hpxR/Fi0n0Utzhi1gFzKjfam5X92WSZ29oyq0fB0UYWhbRVhuf+w1CvniUs6EXHkB M0ps5hXBM0kwnflJYJLVcy/rZRlUsEW4DFYRS1oX1NpVd4mYD0LX9L/izkIselSsAX6X mOhA== X-Forwarded-Encrypted: i=1; AJvYcCVDyVYixIkMlJ5fg1qiMTa59rPjJeBeU9qXz68qLcB5b3vpVJBMlJFMdEsO8GLgo/pqSeiAQ3VQVg==@kvack.org X-Gm-Message-State: AOJu0YxyXU7hmJCiIR7MCoVMGHTvcIFYiW73jHSuw2g3v/e42IRxCsv7 zSKkeEwedLaY7G7X07sJ4Sca38XRaNncwtNCKzNrdDzSHkds9NS/y/tU0jx45UY9ShovJWXpZu3 93c3mjG3tUOJySA== X-Google-Smtp-Source: AGHT+IEG9WEVO32XNJycTeJbJu48vAwd8yZZDgjUr+05hjT6U66t/ondGMQmQEA/GIQ+PsqbB1I6Xqu/GnabS+U= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:68:fe9:ac10:f29e]) (user=aliceryhl job=sendgmr) by 2002:a05:690c:5781:b0:6e2:ad08:4924 with SMTP id 00721157ae682-6eebd296195mr14787b3.4.1732114580372; Wed, 20 Nov 2024 06:56:20 -0800 (PST) Date: Wed, 20 Nov 2024 14:49:56 +0000 In-Reply-To: <20241120-vma-v8-0-eb31425da66b@google.com> Mime-Version: 1.0 References: <20241120-vma-v8-0-eb31425da66b@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=8694; i=aliceryhl@google.com; h=from:subject:message-id; bh=Xqhb993p62ucfXpgqmTAzwF8zVU/iTXacAc64i+hvXs=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnPfiJtZ8e3RVRA+k2lDQWtngvEf9QEVRA3KEln +zY498BKQmJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZz34iQAKCRAEWL7uWMY5 RpOaD/4yxq12q8wgIfSEmCve+/+Os+hqeA6WaIrFWW5ionzJ0gIAJ9cftQLGZTKFI8+rvupF8Lp dR1pbVHaU8sw+3jpSLHyp0jNLFTOL5gbGW1yFUHsGIjzxw9ZghQPcX+3vola7oY+NRtH0UmDDSf 8SHI4Dgs+ykt3Kxa09hTFwnho5mhAtnRKj2ngOcwoqWbTMeGi7vSEbENMrrIyTRmnN4JMfdtNSM w/yYL/NWvnBcoEgwTBvK3Nb1UOFaXc0Qbqs3Bjv0y9q/0WFl6Pflj7UA/Fnoy7ic1VZsxQUDmvs TqskcSB6vHZBhIne4/8r2NYal0rmG9qtI0u1O51X9qF+udqdWZtRJBrFDnWI9oEIWUaNE+THgG0 5/kSBR40GPZYjpwSdwM4HiyBlgxx74DpP9La+cyScGLvbsTOqG/8VkQLtlSus1MsYrElbH1Owqh fFYUr9RuGgxgFZVRxAnc0bIBTam6l6qUBZkUWO/Oa9wbwgq9laxBOwXXDAjr18IFTGblGKLyeCW 1YOxkDN0nbIADD4637WxbTL4l2wYEaDiGA1W5ZMdFdJugTmNZodJxoye7vDLMjEOx5d+dvf2/t7 gFm838Bm4GUkNA1Dm/Pjvht0d8hg0e9hRZx3uqJnRZIXU2Lt6DznOKYGva8smnLR0ySkWip8guU k0h7uXIHYMJyYWA== X-Mailer: b4 0.13.0 Message-ID: <20241120-vma-v8-2-eb31425da66b@google.com> Subject: [PATCH v8 2/7] 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 , Christian Brauner Cc: Alex Gaynor , Boqun Feng , Gary Guo , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Alice Ryhl , Andreas Hindborg X-Rspamd-Server: rspam10 X-Stat-Signature: gjhwmaxyajm1xts6cia6chwpb6upda63 X-Rspamd-Queue-Id: C266E14001C X-Rspam-User: X-HE-Tag: 1732114545-741940 X-HE-Meta: U2FsdGVkX1/L2Kk7FwAeGWBbMd6ekMJGZJc2+MqcAj7CpN9P6RCqJiJOoceeFzZI6RvWtCcixFPggqvd/rs8BEujdginM8yAsj5qqosAk+w51u1LZwzeVcjrdOcvKP8ZvaF2Uwfjq4qmpeB8E+6grmRtvomihUr9HmuDy3SZbN839HFoWRpGEMwhjGa+jtsdNmzh/ptA0Spji57H2JIHjb4axzVbCbuZba9/D9ulQbl0+fpsW8Fcmm9lfk3C6u4HcUbCpchdkuaZas1Aepuh9lwWuYwdAcpXDrQorNXlBmDMwTJ9ZwOgKl3J5LD6T2UOFgEP15/7gBO4NXkCK62RyuFmWGafKwOKCLpKWFpco8HihpQilI6qJ5vo1Y29DDNiEKldGSiJtac1cZUSkISVwpErIbajew8Y0mM0ZWG2eYz4BipXzjtg/vcS54R9ru+f53WvAtweeMDzfWbYy85gy8fEQ5zQdRzikF2erkF9TRfYxHA14c2qugAokRI6YmrLeOWLPVYBHZGfEDhAad2ks+rWOYgWSRebNfQ5pnnvkJeNa076vJGRh4F2lrOC3YQsUQkXxqh4Spd/yzRhfclYHSkaewuNoj/iRTeTyiY8qNHTbQkwGbz/ikbNB46onuWNmf7zF5rjBOe7+a/pxB27F9jyg7UjqUnWilYWsY3jXmOfUi83zHulwE8YTsDdzxkR1oxzwO6ALfUWHhbzvTkBs+dDUhidF1+Azm0en2QgEbAXH2KebLH/nrThSDi8p70jNf/OFS1+8fMcG5lFKZDQTRPlc4eVByFQiQKtqF8q52lyWwt9x66KM+/FoFZ8pJHsXMrUjLSZ38tPXr4RevWj6ca3ONQdlHYmcW00gbS0whZKCyjTGW2co/vL5DJT262INvXHArYYm3a7RhTY4X1IyWAr8YNngrll31m13B6rlWMbM4TWJgRe7WzFEq4LHa/WTKqh73RAWD7Si5t5a7D DBExphu4 Y7sVqBzXMcVLI6/2M83Fu11mshu13TZtzXPFS+mHfOP7nVCpPEwZUg1AZMLuXx4quHjSo8OQlmRmKd2kii5f4lyInWoczPpwBbSKZG7oTqZAF47XbBiWxdVNnCuzxr/sZFLk05ajh4FykMwiSxL8utMkoq1aJJdRwy6FqRx36KT2Y805XABMoYQnOYLH8E6DU43A8JfViFovlgVTlpwF+LqStUPl/38dJLM65e/V7Jy3RIHlFgc8Pp/l6Nm/eSmECx5ffG2eQ6XiKFOq4idGmhWUxylwDTBm4GX9nHevuIfOG63pLDGReN5FY9Ipq06BU2Bmte42NCeKBSq6WHi/JjxR76biiA/EOzQY4SjrovSB0Ns94Yi+aC4fBWSI0H3LOyf3mQPPyNr983qWmPzPDKSyU+ttldSsY6wt2QBSWjLSEQD3t+MkMQRyFpdvDWe1WY1VNghR8cuHkab0Mp83eQ484ayEidmwgkGTloGxk4nQ5Qmf2KWfGxiQOGB1ZDlayA2hIe8ESDD96CJMPh1d6idWnKK3VQ0lFltosLIz9ih688MPSh3Mo/gOHCTY8yURtLd6q2UPUh3g77CorSBwCijJNYDP8pz3J4k6JA7jEblH08FB7CsPUHKipGj1f4fOt+Da97PCCqhwGOuNDXkPAYTe+4EZIEj5jY0+h5GMbvojVS1YWAkbrmTcISw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000073, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This adds a type called VmAreaRef 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 &VmAreaRef in safe Rust code. Signed-off-by: Alice Ryhl --- rust/helpers/mm.c | 6 ++ rust/kernel/mm.rs | 21 ++++++ rust/kernel/mm/virt.rs | 171 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 198 insertions(+) diff --git a/rust/helpers/mm.c b/rust/helpers/mm.c index 7201747a5d31..7b72eb065a3e 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 84cba581edaa..ace8e7d57afe 100644 --- a/rust/kernel/mm.rs +++ b/rust/kernel/mm.rs @@ -12,6 +12,8 @@ }; use core::{ops::Deref, ptr::NonNull}; +pub mod virt; + /// A wrapper for the kernel's `struct mm_struct`. /// /// Since `mm_users` may be zero, the associated address space may not exist anymore. You can use @@ -210,6 +212,25 @@ pub struct MmapReadGuard<'a> { _nts: NotThreadSafe, } +impl<'a> MmapReadGuard<'a> { + /// Look up a vma at the given address. + #[inline] + pub fn vma_lookup(&self, vma_addr: usize) -> Option<&virt::VmAreaRef> { + // SAFETY: We hold a reference to the mm, so the pointer must be valid. Any value is okay + // for `vma_addr`. + let vma = unsafe { bindings::vma_lookup(self.mm.as_raw(), vma_addr as _) }; + + if vma.is_null() { + None + } else { + // SAFETY: We just checked that a vma was found, so the pointer is valid. Furthermore, + // the returned area will borrow from this read lock guard, so it can only be used + // while the mmap read lock is still held. + unsafe { Some(virt::VmAreaRef::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 000000000000..1e755dca46dd --- /dev/null +++ b/rust/kernel/mm/virt.rs @@ -0,0 +1,171 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2024 Google LLC. + +//! Virtual memory. + +use crate::{bindings, 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 VmAreaRef { + vma: Opaque, +} + +// Methods you can call when holding the mmap or vma read lock (or strong). They must be usable no +// matter what the vma flags are. +impl VmAreaRef { + /// Access a virtual memory area given a raw pointer. + /// + /// # Safety + /// + /// Callers must ensure that `vma` is valid for the duration of 'a, and that the mmap 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 for 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() + } + + /// 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 mmap read lock, so this + // access is not a data race. + unsafe { (*self.as_ptr()).__bindgen_anon_2.vm_flags as _ } + } + + /// Returns the start address of the virtual memory area. + #[inline] + pub fn start(&self) -> usize { + // SAFETY: By the type invariants, the caller holds at least the mmap read lock, so this + // access is not a data race. + unsafe { (*self.as_ptr()).__bindgen_anon_1.__bindgen_anon_1.vm_start as _ } + } + + /// Returns the end address of the virtual memory area. + #[inline] + pub fn end(&self) -> usize { + // SAFETY: By the type invariants, the caller holds at least the mmap read lock, so this + // access is not a data race. + unsafe { (*self.as_ptr()).__bindgen_anon_1.__bindgen_anon_1.vm_end as _ } + } + + /// Unmap pages in the given page range. + #[inline] + pub fn zap_page_range_single(&self, address: usize, size: usize) { + // SAFETY: By the type invariants, the caller has read access to this VMA, which is + // sufficient for this method call. This method has no requirements on the vma flags. Any + // value of `address` and `size` is allowed. + unsafe { + bindings::zap_page_range_single( + self.as_ptr(), + address as _, + size as _, + core::ptr::null_mut(), + ) + }; + } +} + +/// The integer type used for vma flags. +#[doc(inline)] +pub use bindings::vm_flags_t; + +/// All possible flags for [`VmAreaRef`]. +pub mod flags { + use super::vm_flags_t; + use crate::bindings; + + /// No flags are set. + pub const NONE: vm_flags_t = bindings::VM_NONE as _; + + /// Mapping allows reads. + pub const READ: vm_flags_t = bindings::VM_READ as _; + + /// Mapping allows writes. + pub const WRITE: vm_flags_t = bindings::VM_WRITE as _; + + /// Mapping allows execution. + pub const EXEC: vm_flags_t = bindings::VM_EXEC as _; + + /// Mapping is shared. + pub const SHARED: vm_flags_t = bindings::VM_SHARED as _; + + /// Mapping may be updated to allow reads. + pub const MAYREAD: vm_flags_t = bindings::VM_MAYREAD as _; + + /// Mapping may be updated to allow writes. + pub const MAYWRITE: vm_flags_t = bindings::VM_MAYWRITE as _; + + /// Mapping may be updated to allow execution. + pub const MAYEXEC: vm_flags_t = bindings::VM_MAYEXEC as _; + + /// Mapping may be updated to be shared. + pub const MAYSHARE: vm_flags_t = bindings::VM_MAYSHARE as _; + + /// Page-ranges managed without `struct page`, just pure PFN. + pub const PFNMAP: vm_flags_t = bindings::VM_PFNMAP as _; + + /// Memory mapped I/O or similar. + pub const IO: vm_flags_t = bindings::VM_IO as _; + + /// Do not copy this vma on fork. + pub const DONTCOPY: vm_flags_t = bindings::VM_DONTCOPY as _; + + /// Cannot expand with mremap(). + pub const DONTEXPAND: vm_flags_t = bindings::VM_DONTEXPAND as _; + + /// Lock the pages covered when they are faulted in. + pub const LOCKONFAULT: vm_flags_t = bindings::VM_LOCKONFAULT as _; + + /// Is a VM accounted object. + pub const ACCOUNT: vm_flags_t = bindings::VM_ACCOUNT as _; + + /// should the VM suppress accounting. + pub const NORESERVE: vm_flags_t = bindings::VM_NORESERVE as _; + + /// Huge TLB Page VM. + pub const HUGETLB: vm_flags_t = bindings::VM_HUGETLB as _; + + /// Synchronous page faults. + pub const SYNC: vm_flags_t = bindings::VM_SYNC as _; + + /// Architecture-specific flag. + pub const ARCH_1: vm_flags_t = bindings::VM_ARCH_1 as _; + + /// Wipe VMA contents in child.. + pub const WIPEONFORK: vm_flags_t = bindings::VM_WIPEONFORK as _; + + /// Do not include in the core dump. + pub const DONTDUMP: vm_flags_t = bindings::VM_DONTDUMP as _; + + /// Not soft dirty clean area. + pub const SOFTDIRTY: vm_flags_t = bindings::VM_SOFTDIRTY as _; + + /// Can contain `struct page` and pure PFN pages. + pub const MIXEDMAP: vm_flags_t = bindings::VM_MIXEDMAP as _; + + /// MADV_HUGEPAGE marked this vma. + pub const HUGEPAGE: vm_flags_t = bindings::VM_HUGEPAGE as _; + + /// MADV_NOHUGEPAGE marked this vma. + pub const NOHUGEPAGE: vm_flags_t = bindings::VM_NOHUGEPAGE as _; + + /// KSM may merge identical pages. + pub const MERGEABLE: vm_flags_t = bindings::VM_MERGEABLE as _; +} From patchwork Wed Nov 20 14:49:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13881314 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D693BD711A7 for ; Wed, 20 Nov 2024 14:56:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D68156B0099; Wed, 20 Nov 2024 09:56:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D16F76B009A; Wed, 20 Nov 2024 09:56:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B91EE6B009B; Wed, 20 Nov 2024 09:56:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 958986B0099 for ; Wed, 20 Nov 2024 09:56:26 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 51AFB80D8E for ; Wed, 20 Nov 2024 14:56:26 +0000 (UTC) X-FDA: 82806772818.26.17D34FB Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf02.hostedemail.com (Postfix) with ESMTP id B211C80017 for ; Wed, 20 Nov 2024 14:54:46 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="kC6KDma/"; spf=pass (imf02.hostedemail.com: domain of 3lvg9ZwkKCL8fqnhjw3mqlttlqj.htrqnsz2-rrp0fhp.twl@flex--aliceryhl.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3lvg9ZwkKCL8fqnhjw3mqlttlqj.htrqnsz2-rrp0fhp.twl@flex--aliceryhl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732114523; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/SZjxNKVJkWvdCYwLH30w19SxbpLmf+zpgo7doX1isE=; b=5M+H0kWxLCt3yH/0wcG2w/YRMS1I8ab6p8JtkAa3fyij2cJFCk5eEeBqitdn60oqm28NNv hFnQMEHikXJ4RW21YWD0HV1SOLSJ/UbkJFv5uxveuuMG2g1gwj0+qSxwv7QOVPUBBGIA+9 0MzIYatQrCWSVVBXcJo35qW57xm445Y= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732114523; a=rsa-sha256; cv=none; b=kl4aJyNdr4+qlm7dpcUNuJ5OlUl9aoFw53shmvs+VeF26hVLd2zY3+H+Rkt2qtUVFLLbX/ x5w3Mxq1T+IOVlQGNPCfOWcqJUrcMb1jsr7XQ1cPZJ682XkbX4jGLHUUVMUQ4k5g6OjYOG FavYg5PfeefH83RWBRU+ZDP9s3lTflk= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="kC6KDma/"; spf=pass (imf02.hostedemail.com: domain of 3lvg9ZwkKCL8fqnhjw3mqlttlqj.htrqnsz2-rrp0fhp.twl@flex--aliceryhl.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3lvg9ZwkKCL8fqnhjw3mqlttlqj.htrqnsz2-rrp0fhp.twl@flex--aliceryhl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-4316e350d6aso32255315e9.3 for ; Wed, 20 Nov 2024 06:56:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732114583; x=1732719383; darn=kvack.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=/SZjxNKVJkWvdCYwLH30w19SxbpLmf+zpgo7doX1isE=; b=kC6KDma/LDNNg0pKSGb5tXnE1RSnehk6TAsD8aONouCzC/5fJ3Gn8wxZcjvbubNb8m d5KV3iPLExC3JEeCsewMZXieOET9JgP6KKZfDshO1gHwENapjw7foB64Q+krsbdceDQT 2Ts/7uaMeRrWnH+dKLf1uK+5edXYGaskL11i9ExCM3iR1Kj6Bqionfhq8pQL4mjj7EAt t6pw/zY/1mfXXeC3z1dGVVH+Qc9Nj/ecoUiTq/X01Jm7XIXMi3k7wQih/9DmNZHq+Yk2 XXAaawS5BTIF2M7ICDRb8m+6lL+B/NWFUNlO+9dI0mR9tnEZjy0H/8MCitE+1BrJ/QS9 YYew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732114583; x=1732719383; 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=/SZjxNKVJkWvdCYwLH30w19SxbpLmf+zpgo7doX1isE=; b=c3hNcplCIldE91DswbNA49GuP6DH54HGkhvDAYSUxs8Iblm4MHmAYKdu2F93/PTlWx S+kjWotSWvMQxJ1O4VDFfQvpW0Rlr5Eqq18z6KV+G1CVjdXSL+JxMkPbz9Zi7QRXgfP/ G8cE25g01Uqr31uoqjeyQsKUNGmWIOB1Hmr0vUgrGHQYvKaSo/vn0KcCbMndkFcs/SO+ awNaDUmsqt//NDAyrmrcmjOvVJiPClXS9Hcku0Ggj7xc0dm/uUe7del9hQJTqEEL/V4c iNtdPRIMNecxLZ3FJ6yWHJzSDZxXZ4C+ZzLKzG0iOKABmtlYpecBYy+bcX2gdOBXHg7f 4HqQ== X-Forwarded-Encrypted: i=1; AJvYcCWLfX+25I+C0jIDOyF1HZO1idv/AEvKT6HBg3/8DmCO9QbUz/PnI5P1tUxpwZsmQcFjfG6MQCqUkg==@kvack.org X-Gm-Message-State: AOJu0Yw8Ekl77oP/ycQAc0W1n03KQaEecUB2Qe+gNJo0xOPq4RgbOH5G WmyzL7JBafhYeVvi8wtJa6AVcj5/J0StkU/KeRarkUiUe9eOm1JS1ZB3wZovt07ql2NkrRLut7e qJRwRKOnrlp/gAg== X-Google-Smtp-Source: AGHT+IGmCoJh8WJ6CU35nHCj5+WORiCwN3sPF8aj+A+iomLA9SMFPrHheiwvgUQFi0TxoH+VHwHP38URGnqozCI= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:68:fe9:ac10:f29e]) (user=aliceryhl job=sendgmr) by 2002:a05:600c:291:b0:430:576d:fa96 with SMTP id 5b1f17b1804b1-4334f030b13mr22415e9.8.1732114582861; Wed, 20 Nov 2024 06:56:22 -0800 (PST) Date: Wed, 20 Nov 2024 14:49:57 +0000 In-Reply-To: <20241120-vma-v8-0-eb31425da66b@google.com> Mime-Version: 1.0 References: <20241120-vma-v8-0-eb31425da66b@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3530; i=aliceryhl@google.com; h=from:subject:message-id; bh=gZT3Euh+ZEaMLQWKLqbms6nTrYufOG/3Vrmil72vUnw=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnPfiKOwSyo7JZVYh7YZMMMLf6ReyTAFr9kcbzi p/IU6JooYKJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZz34igAKCRAEWL7uWMY5 Rp/hEACkypxgG72GpLru5x4sBEqUUSeMGsryVWgYsYHX2gV20DtgtGdbhI1rzgBZqgt+qeNUxKa +ZqyVHTkBTnPYPLFAr9lPYrudJCUj8BDDafGZb+R3ZEAQm3GgJIj3brJJaWnZ2gcpJ1xte+7JW7 4wXomI/PzSy8EbV4uymi+uJnrbBW4R5lGKnbhLpm+jKGrT+a8UygKm2/leDm6uWVtXcHSnNJGBo hrCEeUQDK4mrPR26HDv9wPPyjMyxEx7c+RdZGUgsqoX+hHs70KkQG4gL1AIaFdKtukvC6rGeo1Y X+TDHMayeCyz/XicGqQlwtFFirz/q38j3ujbouHa1D8Tsnsjc/7Tb852JpeYOMseuqAu0GWjUjX HtnkNoHi27j51fs7YgQ9epYxBO60wsqoM9Expkj8UmYvqm1t0oySIQQd1MU3MkMue17DuL7Jy9b NR3zufrHk5jbpQUFeWV1HfVbUMPFGvVtI3/2gqlKHN7fP7l9s6KJqTMpwVcNE8ygUl4lbHX1o5o 2RnaPaR0WzuP4QRUTJlMWM2Weu0unpvmONfEAW0DwvB1QbnJjalRjlcylwhi6RhZn1ELTX/9/xr qpYh6hRmWsct3nWWD9jBaJqiGAZvEWm1rFG5H9qPHIKjYQPogidLCRRS1NKy/Gu/foPpu3RjHhD uzSBNpYqoDIgXVw== X-Mailer: b4 0.13.0 Message-ID: <20241120-vma-v8-3-eb31425da66b@google.com> Subject: [PATCH v8 3/7] 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 , Christian Brauner Cc: Alex Gaynor , Boqun Feng , Gary Guo , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Alice Ryhl , Andreas Hindborg X-Stat-Signature: txbbkwqe7zrdrsydkritr6kgbmrw4gw1 X-Rspam-User: X-Rspamd-Queue-Id: B211C80017 X-Rspamd-Server: rspam02 X-HE-Tag: 1732114486-196252 X-HE-Meta: U2FsdGVkX1+qKkNIa+gpCT0Qc8Rcu5JxI1ZwD6PSfuRclg8lkWtiT0mubrix1es2e/LN7mE5do/JgFf0ZxovTpku7jItDo6TIHaNp0hCiNmDBGjJS2G0vCxzUMVKjw/L6fMoxd4MSNxVR3NTs0tHcuv5+VKlM+gvjY9539AejYmtCC0xHBa6Huve/VojkiHwNJ63HXD6IVhIjgAAgGKL78lQMT4X7NDN/IuuBmTQMSohH9JQ6+qOG6KTmOVC774mJaBY6Qf+a8tv3FQH/nZ2Htic+ooQHcldCoCgztPvg246YaLU/Afq1WWxCC3MDlYKhKfu4yCotyssOuyhanuNuAr+D4uU8Vsfr3IH++XDGI88r38VUuda8wIVEBJ9UY/7BLwoXIpzX9hSmU+Y8C9WwVt/0LBRA8Ex6ru/u3qFiNj/5FjyTBnDMHINWztXsNNatdyuBD6XPWTR1MfggDCLLUBVzB3gcyRsf//Wnyx/yugxKmNB2cfaJRvFknRItyBdUS+6guqiP3KWFxfuydFWIh0ZpQl0OZntV2hPWy5QGC/ox+cAv3B5jV1rrQK2rXdGa9S/wCTePj2Dt9q8A4kGj/Kw0zHpRL8NezuoGnVNjOO57kR4ORS1l49JZ9SVE3bq8g+YrBXkY+Mbu14/w8SMMgakFzVT33NB/oav3Op/qg7T+ZmkzG4FCm5bhQkGo3Hkpm67YjoXJ063fXIMEuwqDFbyWhZcWax+ejwdKTuGh4FQbVmgKbDCZCuBa4i0rwBO8bjlp+LAXvt2cMChBUGbY+ZWd5dY2ud5kQFyLsvxNg1VUv0GUgj+frl+k2R4Hz27Z59ryITb/lVdVBVZ2pIi7Si3hBpWXkd4O9QbcdguL6+NskIimGnvLk9vpVU8e3BEwS6YobnoVcEgi3mbnKB2VrUwCaew9uPF47jtfRmf5xkDvdU82gC8BIfLnKmOhT+z2/dYIfVUBSMHF209+Tk bGxVY0g3 boRNZS46LTwoCNzie3m0jMvBGEIR/plZEQnWm4o0uCrkPDn59ww/hAxp2iGV3YkxT84b6CBB1l70Hn8sk4Lj6KMEZKG6NgkrbN4u8GD6r6y05BpM3DCC3fDfpV2z1LnjFDQUNIeDLOSLIE+ImV8MFSiUgjWgZsilVcJ40zclpHS0HHGDOg9WYoZs/mLg3jOVgkCQFVUONGDLhwwH1WK5wAskrrKAURgJqDSltMf/80AIhjScuxIkmj0OQq8HeZEa7VFiaCmdXecJGg+bOq2pgLZ9Scdbmowdo7jl4IUiL309QXGETE4q4Jur09yBGdYszd/Xarp/WAuk1Rb+sacbQ8D9YFCviBVEloAVu7/s2HWqdeH46ArXcs/8iKXvxVLgrRSygFPPkUND/8/CU+yUZZt6gX6srOdm7/Q/LXHQD3EDOXPAfCmtKhoYU6nqnbWU48XaqRvCd2u6YjRvNnadHVEj6eb2zUH1v6oLOf0sIoomPO5YtSZr5S1Yt68WRTqBwqSnfTn+JlyWP4uoR+wUM11l5A3ELdenChV52zITfdDJ0kEc4ipygK8YNSfM1LvyhXKXaJZjAAhERp4mccGXbIN8im+vcEO6LUgND75kyRbem2dFPoiJJmXXmm0WRx3Ck97mMZveM/Aqa88X4mKFJy1irNFt4v2ak4et+RhddwuupB7Kb4QZjZeP1Gw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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. Signed-off-by: Alice Ryhl --- rust/kernel/mm/virt.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/rust/kernel/mm/virt.rs b/rust/kernel/mm/virt.rs index 1e755dca46dd..de7f2338810a 100644 --- a/rust/kernel/mm/virt.rs +++ b/rust/kernel/mm/virt.rs @@ -4,7 +4,14 @@ //! Virtual memory. -use crate::{bindings, types::Opaque}; +use crate::{ + bindings, + error::{to_result, Result}, + page::Page, + types::Opaque, +}; + +use core::ops::Deref; /// A wrapper for the kernel's `struct vm_area_struct` with read access. /// @@ -80,6 +87,65 @@ pub fn zap_page_range_single(&self, address: usize, size: usize) { ) }; } + + /// Check whether the `VM_MIXEDMAP` flag is set. + #[inline] + pub fn check_mixedmap(&self) -> Option<&VmAreaMixedMap> { + if self.flags() & flags::MIXEDMAP != 0 { + // SAFETY: We just checked that `VM_MIXEDMAP` is set. All other requirements are + // satisfied by the type invariants of `VmAreaRef`. + Some(unsafe { VmAreaMixedMap::from_raw(self.as_ptr()) }) + } else { + None + } + } +} + +/// A wrapper for the kernel's `struct vm_area_struct` with read access and `VM_MIXEDMAP` set. +/// +/// It represents an area of virtual memory. +/// +/// # Invariants +/// +/// The caller must hold the mmap read lock or the vma read lock. The `VM_MIXEDMAP` flag must be +/// set. +#[repr(transparent)] +pub struct VmAreaMixedMap { + vma: VmAreaRef, +} + +// Make all `VmAreaRef` methods available on `VmAreaMixedMap`. +impl Deref for VmAreaMixedMap { + type Target = VmAreaRef; + + #[inline] + fn deref(&self) -> &VmAreaRef { + &self.vma + } +} + +impl VmAreaMixedMap { + /// Access a virtual memory area given a raw pointer. + /// + /// # Safety + /// + /// Callers must ensure that `vma` is valid for the duration of 'a, and that the mmap read lock + /// (or stronger) is held for at least the duration of 'a. The `VM_MIXEDMAP` 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 for 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: The caller has read access and has verified that `VM_MIXEDMAP` is set. The page + // is order 0. The address is checked on the C side so it can take any value. + to_result(unsafe { bindings::vm_insert_page(self.as_ptr(), address as _, page.as_ptr()) }) + } } /// The integer type used for vma flags. From patchwork Wed Nov 20 14:49:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13881315 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id F0134D711A7 for ; Wed, 20 Nov 2024 14:56:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 37C366B009B; Wed, 20 Nov 2024 09:56:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 32C756B009C; Wed, 20 Nov 2024 09:56:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 131D16B009D; Wed, 20 Nov 2024 09:56:29 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id DD3C36B009B for ; Wed, 20 Nov 2024 09:56:28 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 92199A0BF5 for ; Wed, 20 Nov 2024 14:56:28 +0000 (UTC) X-FDA: 82806772104.08.A4846D4 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf05.hostedemail.com (Postfix) with ESMTP id 1F397100005 for ; Wed, 20 Nov 2024 14:54:48 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=FIsAvbjw; spf=pass (imf05.hostedemail.com: domain of 3mfg9ZwkKCMIitqkmz6ptowwotm.kwutqv25-uus3iks.wzo@flex--aliceryhl.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3mfg9ZwkKCMIitqkmz6ptowwotm.kwutqv25-uus3iks.wzo@flex--aliceryhl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732114383; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=aoiANS11czrxWVbduQfnhGruCAXv4HeO9pq8nZpI/0M=; b=BzUEWL0tqYD6KLjaGLqyJf+kZ1TGmL8cfLA2FHxr0ja6O+Fx/Ed77kcS4/Rn+WDyryVTLD YgZjwNtkhNqnx+XpEAP+mThdjyQuiiHhQYXjRcqSm85gbk5mXv1QocwHQG/3a2Kf3kRubR uttb3nRJHfk8rKSDXBDcbiRCwxtfjYk= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=FIsAvbjw; spf=pass (imf05.hostedemail.com: domain of 3mfg9ZwkKCMIitqkmz6ptowwotm.kwutqv25-uus3iks.wzo@flex--aliceryhl.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3mfg9ZwkKCMIitqkmz6ptowwotm.kwutqv25-uus3iks.wzo@flex--aliceryhl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732114383; a=rsa-sha256; cv=none; b=4DvvcEetA9DqcTTh0gWcYlZHYkkKaz21per64COpMtTMVIlQGYZVeoR+q+ts86ujvtnGn+ spvIZSNpOxqlUdAwmwDPkDXvaGoR3zukYGoJpyP+OGK6y+2LeKJjLoY9//vc7EWf1gUcdd 2HvMs1X4CMJKdKAV2M120jXu2trVLdo= Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4315c1b5befso16618125e9.1 for ; Wed, 20 Nov 2024 06:56:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732114585; x=1732719385; darn=kvack.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=aoiANS11czrxWVbduQfnhGruCAXv4HeO9pq8nZpI/0M=; b=FIsAvbjwpxO94DCSEz6kAdtv4sVuylY7kAmpUsZRQ9OolSZ2iVmk9A3UttRwLCwrPE WWhfzR/feu0hgdKyqZKjUTAG3SjDaJ8qT9vt7c3CLeuLGPoPf9KXa4tTQqbX2TWH6trV Kdczz/JlYgOm9zdBSnqAT93fv4WkTtnh7avG5TVLVZuHFcaJtYHsndn8zE7scMEtZUNb BEP1sgdXoE6O77vJBvfRtIDIDDMxEEEJXaI0x11a0R4Jr5MzX8/5JQbEKEKKPkzkU6I0 CW1zqKyPxwS1vpBk/CMlKx66wnMF0ebGxv0kYPP+RP7Y8DmMsVacEL0D64Z1PhfJPb56 YiyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732114585; x=1732719385; 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=aoiANS11czrxWVbduQfnhGruCAXv4HeO9pq8nZpI/0M=; b=giZn1UCXmpYUq2GgLzzVqLIu7MeoiRtPGQQVJ6dWZk6dT01Xz6iJC6opa2+UfY/iqZ OeSQtvXWe8APFto2UFYrZNUKhKaXPC456yrrbrLy4rdU7wZnxL+QfmjnrQiRoRzzBsDk 6aIv8XcXDP26vNyh7sYVuFqKc8mELMZzMCEmzonsTE4pU+Uzj9/QUT0Yecri/claIuiy 7l2cQ+lGfV+yFtJWR21l9eMusTn2uvX23rvWc9D6BcwKAFyVX+3taj2TvoRSQnK4hC/+ Hqq12s8Uv1X8w/S6OAzgum836Yk8odm+r9lERHyV2muZycR9ddnM7ziCnr/ll1S2hHrD rOgA== X-Forwarded-Encrypted: i=1; AJvYcCUqXZZRDaiDGW/UsAbEWGxXgY+MZgquX8dKGxvcNK/y5TPD6pEVy5sJpSu5DltCRCr2rRxFGvcwjQ==@kvack.org X-Gm-Message-State: AOJu0Yxlcy/o0Mh8MiASmBUS5G3foypc5nEVBD56g6LuaGawIV+mQtWy jba/TTCgQJe20Ud4q24w+GxmkJOKGSWyEcT7Eu/oob6hS6Bab2rCc9+n1lOcg6t2cZFWqiRYGAm 9m8higN9GNRcX3w== X-Google-Smtp-Source: AGHT+IE6KZ4EvG2+AyBS7X0AsuANcRIXcFSazw3+PxVfaxNaYxLQx8x02Y7MetgM2m0JyIIKmjeIwnAHQKtRbjc= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:68:fe9:ac10:f29e]) (user=aliceryhl job=sendgmr) by 2002:a05:600c:46d0:b0:431:4162:6adf with SMTP id 5b1f17b1804b1-4334890546fmr124315e9.0.1732114585303; Wed, 20 Nov 2024 06:56:25 -0800 (PST) Date: Wed, 20 Nov 2024 14:49:58 +0000 In-Reply-To: <20241120-vma-v8-0-eb31425da66b@google.com> Mime-Version: 1.0 References: <20241120-vma-v8-0-eb31425da66b@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3357; i=aliceryhl@google.com; h=from:subject:message-id; bh=B1j9P1BH0bxfU/zafa6FVQxd9ysL+wMPMIqevg6Rsd4=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnPfiKB1ELULsXYa5P1N0NhjBWmvy5M6u0nsExs po7gAjq23aJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZz34igAKCRAEWL7uWMY5 RtPwD/kBvIn5klKFHqApPc2S/WCAcomjz5SYvwUslE6FeJGQvZfkTdXTuEoZJq0lsxJmNbs6Hp5 Q9oxbRPey0lnHYyCJQ/nry7A6FNxk3n9Y8GhK4ctlNZr0glylb7YifGxTYNJhuEoFjnoYOvnlfb RZ5yfmziAthnvy/QooPSnoRzSB9L+hyK/vtg+ReOZ06qfImvN6lBFkaAyNklhP/zbjpFSClY2sy vNCOingHCvMKFKWxfGQhiVkonR4nQYVisrQZQRpkq2OJu+V9CMKBsR4tYGX/8bfJKXABOyYKV2X 7cxgf984gDAZfBzM/7VQAret1qyVxQh/R4Pb1Dc4hrXanMJy7Y1yp4nzlP3Q7P0iU6hfY/L01eQ YL6kMdNVuU+FIFSUEIP4qVOlcWZcQ9c+nJANEx1d6x4M7gXDzvusU6N1pBRXaqMS1Ll3aP1CvC5 sfIAd93vWwYZNuZ2yfoIxT/uXDmoVCG04mfKN1RpM8vGzcnonzACkX7DimqwXqGtZZhF/tHSsyu ze2wduW0hjSyD49/eZMEaNrlmxBFaxs9uZqjJUim+WJzRPNbzhgcbtSPUp3iw4RrqTVREicitdr 41uVHMtohgbKannriawy40MiB6pUjr6QUYmB+ljpC2gJ54cbV8uFiZ2Dl2vm7BAEwxR2NRTgesh 2OzPc8WMU/3D7Lg== X-Mailer: b4 0.13.0 Message-ID: <20241120-vma-v8-4-eb31425da66b@google.com> Subject: [PATCH v8 4/7] 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 , Christian Brauner Cc: Alex Gaynor , Boqun Feng , Gary Guo , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Alice Ryhl , Andreas Hindborg X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 1F397100005 X-Stat-Signature: zanonorpci1e4q1gamgoq54ubua3fcs9 X-Rspam-User: X-HE-Tag: 1732114488-213971 X-HE-Meta: U2FsdGVkX18w7iCje0KauYp7zGYbF2aimb6il0z7u/3tNWS6GlQ+DKKMxTeA/3QU9ok/mGwsGtBJAsugAVANZC9HqHqw1Zfu+XSXvoMXBZQB7HdJ330E8O/9KNPQ//T2kNBuO2Gn1stxzkADsckvTE1Vm+EzvtfFlIylS7gz6zxxLpUfxB2Qfj3uYqEKtazd1bZI0V20Wh2wVb9wtC0HbZf9rkH9aPFKIbZJPRYD7cU5+xaCnRG14QkbPmQQesrLBhUPG+Bz23uV2/PJDhWURc/FN6yKK/Qo5HPebc7sJHFErBtXkmUcpftC2Lz5Kz0UThjUnuHkLyLYJP65w5/qOJvqUpxIyL4N0T1RaGBR6bF6GzCNfBEcCti5iFKf6yfp93qS1q6iLTMKXG3R+BxlfE17PyK4Mk5hxEg0c5V8dtmclvJRrGP41yuVbhu01mwE+P2FmA/e4nYq8kikYt+PHnDysO2PKq2f8HYv/gwRaUrrY0C4Cujo0eSfp/7xMuZgeXWlE0RT7rZWND/dpCh/3xf3ZxRO7qYh91TmsYs8NsXiQRC4cHQHxExmZwyiX52TQJljmxUWRWHB9OJZM66m718caXFhj5fJ61QiN4FLyBJGsT7pMLFgYTFEkOuqAvC/HTWuiYwZt2rpd6gYtSb9Dsu79FNoNodkavkXFnnsOcoMppg0c5noqF9Bl5vCIEBkP6M9AuIbUHm/ntrh0QohwzAppdheCeZFxuEEtzXhxXIwNv/UB0OUf/7DJl/lc30BZE8oR7B/kh6xSqZeJKt/m+huti+sHlKgnS4DKI29lZAKCKKK8S0EEokaY0Kk3hKOhnyFL0BRc8uiUQzGBDFTdmXPOv6SRhgU4KylEYw2PPs+d6xUYttVg3gOmyGdwlagOJlWRvGB5eGK6jUc+p5tR2GG92NJBCu/lvwh7x4/VM25NaElTeMXpZwS3y9HEIlBxCbehh/h0hvIR19n5N3 Ho5YTKKI /ONBxDrGWX4Ek6WrWDwMqRReDX/Dj7p7ER30Zm3exlzLTW3C6Wi2/neTJ0KAi9Xv4lUUNT51Uifs3PHxCL6N6+aMnQoye/3TeWPjCm55aBUCB+JvsCZ3ajp619VF+e2IRPGoaMU9SyOGkHHrb5+A10gaGXvVubNTEL4xvPvnyoCtCvztefBJJLlRPuwLil0KiNndxlBKUAYlNW3BH++trbLPVGp0hkqTxQidRDVfMEAd4/305TtLsN5+KekYDSwY9wouxGLtg8oHifCJaD+1s3rHhvJgGFYYIfW3IHgMkMhEKgji1IUyPjsmzGesLhZxdYW4mFlwEkj25pp6/t06KOe/W0Jnege7zJCIpANALt+zv9+A0xr1FqIoZTL4PBbCikrqFLdttPGenFCaSt4Aa6R3mYxpVGlrx6ECoiJkBKLwVmTTqAizXQEJ2voUX28YoWpHVi/WB0xjHZf3YHa8+H6bBiwq00YT8Ul7fFTz02Xb0xUFQMotzADcdSvppXTnnjz032ccob/9bfy/FDUsUeyb0xCuR5h49jW5US+WLXZEqKvRgktLPRS7j0j15NjvMlet+i6DSLHruvp4s4t5ewBVFG4KtSvAvjYG2b7GRACQg6qMPptNVtsa79lkokQ2gJOTCApJFZyGZlw732fttlefYfvHxkC35vvqLSyHeqZN/0qXplmmZhDaISQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: All of Rust Binder's existing calls to `vm_insert_page` could be optimized to first attempt to use `lock_vma_under_rcu`. This patch provides an abstraction to enable that. Signed-off-by: Alice Ryhl --- rust/helpers/mm.c | 5 +++++ rust/kernel/mm.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/rust/helpers/mm.c b/rust/helpers/mm.c index 7b72eb065a3e..81b510c96fd2 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_struct *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 ace8e7d57afe..a15acb546f68 100644 --- a/rust/kernel/mm.rs +++ b/rust/kernel/mm.rs @@ -13,6 +13,7 @@ use core::{ops::Deref, ptr::NonNull}; pub mod virt; +use virt::VmAreaRef; /// A wrapper for the kernel's `struct mm_struct`. /// @@ -170,6 +171,32 @@ pub unsafe fn from_raw<'a>(ptr: *const bindings::mm_struct) -> &'a MmWithUser { unsafe { &*ptr.cast() } } + /// Try to lock the vma read lock under rcu. + /// + /// If this operation fails, the vma may still exist. In that case, you should take the mmap + /// read lock and try to use `vma_lookup` instead. + /// + /// 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 okay given an mm where + // `mm_users` is non-zero. + let vma = unsafe { bindings::lock_vma_under_rcu(self.as_raw(), vma_addr as _) }; + 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 { VmAreaRef::from_raw(vma) }, + _nts: NotThreadSafe, + }); + } + } + + None + } + /// Lock the mmap read lock. #[inline] pub fn mmap_read_lock(&self) -> MmapReadGuard<'_> { @@ -238,3 +265,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 VmAreaRef, + // `vma_end_read` must be called on the same thread as where the lock was taken + _nts: NotThreadSafe, +} + +// Make all `VmAreaRef` methods available on `VmaReadGuard`. +impl Deref for VmaReadGuard<'_> { + type Target = VmAreaRef; + + #[inline] + fn deref(&self) -> &VmAreaRef { + 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()) }; + } +} From patchwork Wed Nov 20 14:49:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13881316 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id CB242D711A5 for ; Wed, 20 Nov 2024 14:56:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5510B6B009C; Wed, 20 Nov 2024 09:56:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 528186B009D; Wed, 20 Nov 2024 09:56:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 37E986B009E; Wed, 20 Nov 2024 09:56:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 1294A6B009C for ; Wed, 20 Nov 2024 09:56:31 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id C4D48A06C8 for ; Wed, 20 Nov 2024 14:56:30 +0000 (UTC) X-FDA: 82806772188.08.C22F6FE Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf09.hostedemail.com (Postfix) with ESMTP id 46377140006 for ; Wed, 20 Nov 2024 14:55:53 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=m9Tpf92s; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf09.hostedemail.com: domain of 3m_g9ZwkKCMQkvsmo18rvqyyqvo.mywvsx47-wwu5kmu.y1q@flex--aliceryhl.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3m_g9ZwkKCMQkvsmo18rvqyyqvo.mywvsx47-wwu5kmu.y1q@flex--aliceryhl.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732114522; a=rsa-sha256; cv=none; b=BwlooIim23JV85bXsn64kJ8FhVHXpRnNS6sq+BvOl55henesk7a7GxP/HksUiW68Xl3uyB Dc7fRDY1etZRN19VkKi29SmvhQaX7SvusSz9dYamazTgr2ZFAci6j6wS9IB6hCFc6wsTHV 0qmoW7nOXXNVsdLNefKpSSUl8TwhhuM= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=m9Tpf92s; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf09.hostedemail.com: domain of 3m_g9ZwkKCMQkvsmo18rvqyyqvo.mywvsx47-wwu5kmu.y1q@flex--aliceryhl.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3m_g9ZwkKCMQkvsmo18rvqyyqvo.mywvsx47-wwu5kmu.y1q@flex--aliceryhl.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732114522; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=B1qhmYZFewino7xIAVBLotqrei4FahWNdMxHocqLM/I=; b=smyO4TBHtPIyS3hFuWyJmych+o6ML2qjqxWGJHqgaWzWSBbfR6yBd1iZfyvfSczdhGUYbh okHV36wuPWOJF7tNZwITID8a8mNisrunqZ9cH8QDXut24XvnqKb5gECPvxMTScME0RFsQ8 kgC9WRBYpDgbwq3tduyDDjUJ5qYKTM8= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6ee61c44c69so42232327b3.3 for ; Wed, 20 Nov 2024 06:56:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732114588; x=1732719388; darn=kvack.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=B1qhmYZFewino7xIAVBLotqrei4FahWNdMxHocqLM/I=; b=m9Tpf92s9hBbupL6gE0XP/1zlgJx4ZF6E7moz1Ce5YQOKBwzlqyGbWWJvDxoqtPJSl vp6dHAFf4sqjnmNX/W6rVrbCngDDyN4Vh9sG3ulYmG8s/FK5+oZwmxIuB4M4Tl7Tu0td HG8oB2BrPB8jnrgo3YAbuipI5RY1iNiV1iQ9mtzksOKY4p51ya1lZNsbP8Psr4/c7BYe 09+qar8nuwpXsOKLCdcYdZIzNZIgzkmxRe0ZOt7yskDCTV37rOIH9NbhBtiMkMLtOyyf xf99/FYk5wu2zy+2dgJ4vJhmAtcG167Jm9L8fOCnOXJuthesnKxuoAUl9Gb0LvO6Fqpi U6Lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732114588; x=1732719388; 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=B1qhmYZFewino7xIAVBLotqrei4FahWNdMxHocqLM/I=; b=ahdtZinKpAWztzYpRnhCLQ4401NVXLPo/AdTYlXH7hQbMwidzAVTISXgniG/vxrHU8 GS0KH1VOtYzR200sfz3awDvhw1Uq/rnHaTYm03aWkFmUsgsevedu5N5sEdqpM9SuU5/W bVS9gFi9n1+vSQXhBDn+pPhgBVphq5SS2QHnZ0orRpnf4rnGCerH50NEPz2/CHJnjjTv 5vnLvOtZfL2QfpHpwW7WM1TVQZHfmymBneV4CTggytC6YWrBDaKzyB216uHJoV71ZFPz LwqxiwbbIzDAoxcSsHDwSCb1W46xXzPPHp18eowW+culLSlVrJNm+Uws5WXZRJ3l7K9y mWVA== X-Forwarded-Encrypted: i=1; AJvYcCX9YEvkMOSo1au6DpL6EyN+bQGu/E+wVMHEkPVc6D6WWBsDrSPod0iwvAoiWpHDfm/Y9kHpowhAjw==@kvack.org X-Gm-Message-State: AOJu0YzR1ESgZbNEDjsxeXu4pwDJbALijMawIu+qQZO4Y/fR2KaU1MdD QYF0ty17Dw8jIgdYXRrhYgmEZ8w7O2sXtyG85iV/x9m9ebD1BIjEOKRZNn6H7Ra3G39OwiJyAuT I9Q1j2nx9h9/s5g== X-Google-Smtp-Source: AGHT+IF/fEeF7RZo4n1G/IhIgluwurThfGhJ8HHwALNv+tngK6vv1K4wFovn4nEL5pwq1i5uR+Y80sYHZ33r9Rg= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:68:fe9:ac10:f29e]) (user=aliceryhl job=sendgmr) by 2002:a25:abee:0:b0:e38:bdf2:17f2 with SMTP id 3f1490d57ef6-e38cb73348emr3963276.10.1732114587698; Wed, 20 Nov 2024 06:56:27 -0800 (PST) Date: Wed, 20 Nov 2024 14:49:59 +0000 In-Reply-To: <20241120-vma-v8-0-eb31425da66b@google.com> Mime-Version: 1.0 References: <20241120-vma-v8-0-eb31425da66b@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3012; i=aliceryhl@google.com; h=from:subject:message-id; bh=zMBEVvavLox9MacZywaW5TO54AiJm41R1sgIdY4Cj4c=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnPfiLa5bb5Royej/gvacnIe/2ZvajVUxGyAgMa dNsj+onQTmJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZz34iwAKCRAEWL7uWMY5 RumNEACU95kezGcsO/Br+P6owGa2xELObLK9zCf/u/XtIn3c8FNg85uhnzJziaGAaAd2cYqUpzy RwmIPH+7ZyFBtfJpVKn7NLRLtdVo+klyy9JVT2z6vKZBJr9VJUJSDEG6gC3gRxqE6UgrRlWd2Ys wIWtHMXp4KouQyAZWtSablEJ5spRaujZDuYaA8KD9UICei4ceVZAkgpluSWANS9W7P/465YuKd6 kV+/4zDJVskpsK+bllZ8ONgS6OImPjDavA+ZBLXqGtcpBGVPfVQ71N25J8y0YjUgksmvXzoB6/7 93aKzR2oaK6rWaCZbzQvh1UWQoJZe606PiqvMHGKvngM1Z8RA+d71ce0Cf2n4r1EyHyzAeU6WJL cOW3Ru9xOnLlGfECNblgdzVjOzhFqAuukhuzo54ZhUVmkpCwOdmXRUlOke5rCDEE5cJ5Qqd7Wak tAvR5mkY4rG3zy2C174XQRFs95SBBQmOs4YrEDCXnIUgB6ZB3hVwUmLlU8TJNsnFRxKLa/zgdI5 6cQislthRO7rqBODUOhHBjizlWYGcoFwRkxjjS+nT7oRo8DpxZcCQ2tGV5z9cQbX3QEp1KuxDe4 dGYcwM0fgLH1M09SIqdoGE3bIMWuplVaerFAXZKqKpYgC4by/xXMkwXkE0WGAKavS4c4MVBip4d lLCXPgjMMTYXRCg== X-Mailer: b4 0.13.0 Message-ID: <20241120-vma-v8-5-eb31425da66b@google.com> Subject: [PATCH v8 5/7] 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 , Christian Brauner Cc: Alex Gaynor , Boqun Feng , Gary Guo , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Alice Ryhl , Andreas Hindborg X-Rspam-User: X-Rspamd-Queue-Id: 46377140006 X-Rspamd-Server: rspam11 X-Stat-Signature: 9trs88c6a4amh1xbtzi5x1mi5g6yzhti X-HE-Tag: 1732114553-288997 X-HE-Meta: U2FsdGVkX19uCXO6vhBx84tNhOebdIaTHV/RDl5a0Y3tX2MJCn3HcKzni6lMi1JgTPK5AWmP6FnQAdlYHKSNj668OQ1EcPOwVlIVVRW/3gukHMJKeHwUxSvUvkpAmPHn9dhYOAgksV9TgkD9mY/TlV3gBC103y/SRMbKV3tSs/iBbZMJbeC/grllbN4U/m7R1dfgt6WKbAzf/xIQPlLj+xjq46HQjQNAdEFn6c4YuDbLjKrPnxRcsTWK6jrcOvdmfBvMyhBS1CijOiQJEgYx79v92uuRpMg/y6pVy8RItnCLkjyki53sP4cNUvl3so9xwUIAp6qf9/wxz57PqvF7v3n1qBS8O4DXUMqWLrMPOAkFL/7kGOH1WVqsGMPr0fPSo4dQ6JCJQnDw6x1omIXDDF9d+vI4YWGTlv9NoDj5TI+VzfIafKIFPP8vDUE78oazfDQPlXC7qG9b5oQBoI98RGOVQiwTiMjOgNSeVdHZGxLHQHNXFYSjV2eU9szaO3Q4dOBTv5MhDKmB36OwbOyQ3N7SWPi3Yja4nBX6pKEI1cZzG0e/HFOf/61nQVNiuwZh0qb03MMuhOjy648ZxmF2q7AyI+sqcz26EJ3s6FCQPsOSFrYUGOHqr3Ga88VKPh3qRFhwNJm18nD6QUQPzmAj62OyEZnZCdF8ew/2ThmLQt6SPSl7pqlSDDg98a3tUH129RqvOnw+PjS/mwwy4lDONe5GWmgzVBZn7U1WYnhelZSLF89zllKA1RJZIurmePNmKB/yghr8fbLg8FVHzQmu3VE7VQu7g5X5O0dLKIjoGWKA5i/OB63bL8zHjid0swcvAyRKD5YU0LirqmXWtg6qNQ+geU7dAghtmyPn+1knZubgV9AQjRIV3Fr5LB+X+x7vUthyehuEzmu9EHBJoA0MMN/mJQpCnrBICAljWjF+aDEIT6ihyCjLBBiKzqCKPEXPXQ8+2z0UVK14hOSsCzg r/DPJw3P rd8Ek1LYUq2J6cBP8Y03ZTVNHQkmcc8Oy7I1IWWbTXrwJkBLeARLxp92TogN+UsKPHZgHMwQ6yhqchW2TGHPUgO8Rsm6P50pIfUxrBCf69neIrhbOMyO713iBfFYzk8C8i7wrvtsizt5wyvD5M+DWvXF5f+Aa8i7D0FwL+C0BVYg6XdZpzDCgtM1lzRFhCnE42vrEcVa5n3awdZ9aeC2wrxjhDgXxzSjKKZgKljRsAKY3WwMso+8sj+YuIOBU/mJeEucu050m/fSzNh3OE16aa7Oa6YmlO1Pl3Za0jdIpx94DmqU/p5vuNm8W8HDk4vz/ubTeutKfbZoqS8nRjBa8zomsYu1Uu3cahQgd59oKcEv4o2mCw9C8qPah6D+yvNGLvciEAWbFky6Q8HtunNaUD/2j7hbS+eTrOHhaNWXY5NjF3fwYzWbdhDnA3T0uqNo5/blv3NLZkpryvC4wxQjhfTVN6D87i7qS/os4RdCji3bcm0WiwGgA4lFUaa9nNOxtNgQgL1vAsNrvh1Tkpq7MTJ+D4ZjZ/obBaGC3Z2+suvus1/h3Cwy9N/BwdH1N8tKlmhkC/5OeaWdhbOnfWDgP+7wVEXe5z6hLVL+HPO7IpDUZHy1jYbK+OhAuBLZsVrGRRlDTm9BcOnfZ7IlFdgChLklSNlj1OTGR1zpOZq8CgxKNymcR6hC6c91L+W8z9HeCG/qG/pWhGVwFK+4DkbvsyR//Ow== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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. Signed-off-by: Alice Ryhl --- rust/kernel/mm.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/rust/kernel/mm.rs b/rust/kernel/mm.rs index a15acb546f68..f800b6c244cd 100644 --- a/rust/kernel/mm.rs +++ b/rust/kernel/mm.rs @@ -98,6 +98,48 @@ fn deref(&self) -> &Mm { } } +/// A wrapper for the kernel's `struct mm_struct`. +/// +/// This type is identical to `MmWithUser` except that it uses `mmput_async` 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 { + fn inc_ref(&self) { + // SAFETY: The pointer is valid since self is a reference. + unsafe { bindings::mmget(self.as_raw()) }; + } + + 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 = MmWithUser; + + #[inline] + fn deref(&self) -> &MmWithUser { + &self.mm + } +} + // These methods are safe to call even if `mm_users` is zero. impl Mm { /// Call `mmgrab` on `current.mm`. @@ -171,6 +213,13 @@ pub unsafe fn from_raw<'a>(ptr: *const bindings::mm_struct) -> &'a MmWithUser { unsafe { &*ptr.cast() } } + /// Use `mmput_async` when dropping this refcount. + #[inline] + pub fn use_mmput_async(me: ARef) -> ARef { + // SAFETY: The layouts and invariants are compatible. + unsafe { ARef::from_raw(ARef::into_raw(me).cast()) } + } + /// Try to lock the vma read lock under rcu. /// /// If this operation fails, the vma may still exist. In that case, you should take the mmap From patchwork Wed Nov 20 14:50:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13881317 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 19DC0D711A7 for ; Wed, 20 Nov 2024 14:56:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 700FE6B009F; Wed, 20 Nov 2024 09:56:34 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6B22D6B00A1; Wed, 20 Nov 2024 09:56:34 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 48D0E6B00A0; Wed, 20 Nov 2024 09:56:34 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 266FC6B009E for ; Wed, 20 Nov 2024 09:56:34 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id D3BEDC0CDC for ; Wed, 20 Nov 2024 14:56:33 +0000 (UTC) X-FDA: 82806773154.24.B87992A Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) by imf11.hostedemail.com (Postfix) with ESMTP id C14774001E for ; Wed, 20 Nov 2024 14:55:27 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=EvQxDuLr; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3nvg9ZwkKCMcnyvpr4Buyt11tyr.p1zyv07A-zzx8npx.14t@flex--aliceryhl.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3nvg9ZwkKCMcnyvpr4Buyt11tyr.p1zyv07A-zzx8npx.14t@flex--aliceryhl.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732114500; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=UwChOJ9zAIfe8IedX9D+YlgaCTQORbfAqxOhT4faXfc=; b=BVTx/AyVdPlj099ScmYvDP/txAYThRpbCHt6BDGaTV+HW5c3rdsGzebDa+HWV82SLqFhI/ HNlIga+iPfOygW7sk7aAdb3AH5s4s2H2a/GgJKmsI6c5J8kbgdKyF1sBO9gwcSW3l7mA84 2pXz/2xVBDunpvYrmasR/cbt4R4hMMk= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=EvQxDuLr; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3nvg9ZwkKCMcnyvpr4Buyt11tyr.p1zyv07A-zzx8npx.14t@flex--aliceryhl.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3nvg9ZwkKCMcnyvpr4Buyt11tyr.p1zyv07A-zzx8npx.14t@flex--aliceryhl.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732114500; a=rsa-sha256; cv=none; b=8n4N+OKy1/fAhTiatKCZ+sA9kk3GkSgjL3LjxRyfFM1eegQ1cAregC6eJWhhpUjA2BENuX oFDGHYKB+DB9fQdODD/HDbWf6oQXkYwXkEdfXcu9v1iRemp69Q6LD7IsQYV2B6lqFl1bHj voD+2CNWWcr4rcTD9ZDNaPmT4Nbhv0s= Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-38256bf4828so310507f8f.1 for ; Wed, 20 Nov 2024 06:56:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732114590; x=1732719390; darn=kvack.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=UwChOJ9zAIfe8IedX9D+YlgaCTQORbfAqxOhT4faXfc=; b=EvQxDuLrzTC/kegjiW3s5HaZH2pXUiKqGgUxP2eZvuw89W/79HkRCROEKcQHd7ZGxZ 3E3J/isgzUamEG0TdZRVWWtfvKKjLV9kO0VRlaik05UyVXORyM8Yr0SdxYuzM8s91fbK 1QpbyWmlk37PVZ5Q+s8YJNbTb1FOzWwNBbTsZgH4kfijge/d7rGuX+7OrLt3G8oHZr2x U27JJi/f+mesEhVGkQ5HtbBo5aR5whj02dVV8imbQ92QCewKCjlyMM0tT4vBm9VchS/+ jbCID7oYMkUvlWh6PbACmipFEVAIw7jzBoLemp5dbCctLEX6GK2CiXkNwbg1RlRn+SHw xEfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732114590; x=1732719390; 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=UwChOJ9zAIfe8IedX9D+YlgaCTQORbfAqxOhT4faXfc=; b=kgLXkN2FmRuAWGuRknG/u5S735Rv2ZRhWHibkDoMYyqy979JWa/3wHtQpC+u5Y4xMe XvLz8eNcz4xQgak7dNni2cKuVbh/NAw9307mD/88ta2uBbqxwoAP5LsBbq8lSL/W98Ec lL5gGPCritv4ObN4dKfnIDyCAnmsEV8fmWYQnSTUghiK7pDWKl0iacGDc0nUdxRBTnQr DLZAxmXElJUue4tZH5Vw3dO8u5MfP9UqnheRFVC/gcXtlhp+WNPqZIoDnhhB2kgcjBC1 ZzOxIEdTNf1nKlpcLiQo3k/LYL8fxOy7S4nTTt3adgPPGomZ47rjaAFJih7MMjIp5gGI sQaA== X-Forwarded-Encrypted: i=1; AJvYcCWbJakJeYEfyhJbsw28LfxMStj6cZXLpgDCyVy2JQMyjDATWInKSKVVuY5ETniqit4hq6Gr3kfF7A==@kvack.org X-Gm-Message-State: AOJu0YySuomljHbJhko3gkeouhZmAgG9PuNPX6fS6PT1VkkdYEIe0szx uKZDKDY3kj4QVK+A+OUad2IKN00U4YanKxhnjnkHhxvUBJo4hjV25Wvji+Pg3JXenJXUgb6aJd0 7zAVVivZxRLI47w== X-Google-Smtp-Source: AGHT+IHXbdV8HAQ4+Suef2mPnmtDgGLMdzhu1f1M8BZUgtuk+G0K3LivSZuvpPIt1xjcvlwyptBDB6mRFKUQlaU= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:68:fe9:ac10:f29e]) (user=aliceryhl job=sendgmr) by 2002:a5d:444b:0:b0:382:3b1a:1870 with SMTP id ffacd0b85a97d-382544b5d4bmr1125f8f.2.1732114590361; Wed, 20 Nov 2024 06:56:30 -0800 (PST) Date: Wed, 20 Nov 2024 14:50:00 +0000 In-Reply-To: <20241120-vma-v8-0-eb31425da66b@google.com> Mime-Version: 1.0 References: <20241120-vma-v8-0-eb31425da66b@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=7325; i=aliceryhl@google.com; h=from:subject:message-id; bh=GxeBG0VLgiYY7aoOP2RaPrBmc8r79aoXtEJ4vi7OY7A=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnPfiLpdbSAArUzD356S1Rdhs5xf34Ng/4olKS2 zZLQlqEVN6JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZz34iwAKCRAEWL7uWMY5 RihJEACjPpazX+vyEm1GGkDxAIjC2Z0e2x4wQhJDGkGfba15BJ7OdktI8JOyxO4017dJv7XCJE2 OK+70me6wb/vPIGBpARjasmBRsFqaWj33HTt1PEH+AHdNRkVxswHxI5VMyAqQptj05zDgBFlLA+ pELHaN5ryoaVBTyMbpNCl5l3L3mMgagvMJupYAsGhl/NuPbLAzrAcTJpklnovug1YMbz2qXblbO kZQq818LxO5ybuOlDxESimOZvwO/0X8dqnBHHB6RSgzsbH2WBwpTUjasTqFEFJAAdcjYwYGRPyy yDGqkIDT4zK+A1mvjaTbYEexaoFbkmVufawcJ+sJlPsyfJOJmaW1zqteMwMUHZTpgHqWaDoU2El 6/Cnz20ExMDZSdITnhC4No7JB2fyu6VSmp2Aor9F7uLIdVIU2DeBjq8dkTovFnTS+TGtrMlSMj/ Ci9+qIFkfEOc1w8OBPZFbg1CVnuL3zWzP5c3Z7E845AFM557AtWKQqKNFglOOn1CXmskiZEPWsM y2lk0A+cmA/1wgD04mcczeJqeAL733Oyea4JeEwhAlod49osPNU5t2OxdO/MUB+wWy7BDcEKpyj /CZlEDcP/JoXoioDlwssN3+EJNhN88t5exP5y3//5S1kP4i1ip5KuZWp9yhkc9cLDxjERVAQHCB eDHl3Ean9iBRSrQ== X-Mailer: b4 0.13.0 Message-ID: <20241120-vma-v8-6-eb31425da66b@google.com> Subject: [PATCH v8 6/7] mm: rust: add VmAreaNew From: Alice Ryhl To: Miguel Ojeda , Matthew Wilcox , Lorenzo Stoakes , Vlastimil Babka , John Hubbard , "Liam R. Howlett" , Andrew Morton , Greg Kroah-Hartman , Arnd Bergmann , Christian Brauner Cc: Alex Gaynor , Boqun Feng , Gary Guo , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Alice Ryhl , Andreas Hindborg X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: C14774001E X-Stat-Signature: o376czd7ipgg6sckjqgq1z31ag6htxc5 X-HE-Tag: 1732114527-71946 X-HE-Meta: U2FsdGVkX18UUv7JS9AbKfLD9MAyNeuIW9X+oVruSXYcdrskDHbNDcIyXvTQ6th3OOSgX9rPQkjpqwcdx3pVqfQ+IFoZLPzpKn9/TL1h7N+a0q5p1voGykI4VtcdzllrLgdF7CRHmxMo6qZyLn1Vrqxxj4QbKSGWTckhS+nklWFMf03YzjDPDqyXtcmQOc7AF4g/nHlgx4vXU5lbHz94lotTKXlIgpMwxHd7jG2GF9rh6Ju7EWKwa8wDHqdxx9UWNxSknv5jDn8LoqXmfvSi4qlg96b1fGuKTXWm9xn2xofyRqcJM6dWtX7VoBboW0FaOYOi+fLfiOJkUxp5iWTcG8OVaq3o/XHT6g1rukxUnpikL5yKGbsXo0raAWp6u08NhMG0vkA+KOw+dvVkeGDAY7NqAgfPCa4n2mkWIFXSX3u0SOmNKEN0zBAF765knXT3XuTCoQaIITl0iOBoA0/GEhxF5WpidZxkUD8gvuBxWbnGBzpLpJYsmFjeFdHEXzEYCyvEB07GGHTpASp7oJ7guVp9Q58WhlYqHR7+qT2A39mU/EwU3jficp60juLyy0+s22AoX1o6BDHvd+ei761HARNe2cIPXuEsg49TFprCduO253E4KxpVNtetpZSS+CJl2lHWITzpc5XCDihUQ9dP8u7wxR2fnn6wkOvun1Cy8MZRvuZzSqWTuzhOsvCxprLjPWKCKMVIpOfR3mvojNJNIz4k8EZ0NenSK/ei4HqOYjtySOHrfedrJYaVF4PyrBgbp+hE8bur1oDLEH18/4IVuwECr1HSfK1WYT8Nw0C5vQjnTX7nmSKVD8gGfCeyDqNfC4t2fNCfaaG7+euk9/ZnpImsTFvoSDNaa43Ka9HlxnQg3wPK2A78f67w2ye9NqAiqcwxtqH6E7pKI00nD8PXK38prk2vd42M/Bbkbj37AwhhaurVlTQ8jH6hYBRFgX/gF/cM2u4cgisGy1+DziK 24anHizf QbwsQ3niQbyvYyVQi/mNC8dmpyNoiGN3YGOAXNbqgWDUh9fBUaWObDmhKklBnOK33BeV7ZsBSvzDAJFOix7JYxT4fVg0AbuogtGnwbqlB28nR9XGZCMmeP/40dZmaA0pA19I91QOI0WOWmUJJhyHNyHDjjZpu9JK7oDHP6GLScZ8PnDqZqwriJTgHBBKdLeWGDXvfDcDVuN4mxj66NugtS8m71+ElsjvkQD9ERlXULtHFGAMKsrUtQ1WD0X7uz19gUcsKSwY1Pipt7M/rcrBj0et7L3ZzmKbQ6tLwoe6rvsd+x6eTiEp1ZW8FLa0z1izW/NbtA4OoA0Ur8oviKSJDlj78F3Qf9IJhzcH8Mrg18sIrS+TmI9di/VsyKI/FLDf7cA0D7fjXykHbttXblDgeOOH6hByanWguObZ23vEFKWi5PZV4ITFjSKoP5Dyup5ys6awwAumDiwREbowWN2FYhPkq9yJKVP+tef85DSIiSVA6r29i3/xn8ipp1tVsfLDXSHKT5PronRDwkkmU1tqCQuUJrXxxWpTLD6VM2otYday98rpOkt17qJQMXTdkEHVVilaDGDZZRuE0sJctS/B6rDi8XZ2gtyWqzwxc14AETlCBxrI9mb5T7IA67O1KWOHomrYNZ5P6mDZDabw= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: When setting up a new vma in an mmap call, you have a bunch of extra permissions that you normally don't have. This introduces a new VmAreaNew type that is used for this case. 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. Signed-off-by: Alice Ryhl --- rust/kernel/mm/virt.rs | 169 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 168 insertions(+), 1 deletion(-) diff --git a/rust/kernel/mm/virt.rs b/rust/kernel/mm/virt.rs index de7f2338810a..22aff8e77854 100644 --- a/rust/kernel/mm/virt.rs +++ b/rust/kernel/mm/virt.rs @@ -6,7 +6,7 @@ use crate::{ bindings, - error::{to_result, Result}, + error::{code::EINVAL, to_result, Result}, page::Page, types::Opaque, }; @@ -148,6 +148,173 @@ pub fn vm_insert_page(&self, address: usize, page: &Page) -> Result { } } +/// A builder for setting up a vma in an `mmap` call. +/// +/// # Invariants +/// +/// For the duration of 'a, the referenced vma must be undergoing initialization. +pub struct VmAreaNew { + vma: VmAreaRef, +} + +// Make all `VmAreaRef` methods available on `VmAreaNew`. +impl Deref for VmAreaNew { + type Target = VmAreaRef; + + #[inline] + fn deref(&self) -> &VmAreaRef { + &self.vma + } +} + +impl VmAreaNew { + /// 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: *const bindings::vm_area_struct) -> &'a Self { + // SAFETY: The caller ensures that the invariants are satisfied for 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 = self.flags(); + flags |= set; + flags &= !unset; + + // SAFETY: This is not a data race: the vma is undergoing initial setup, so it's not yet + // shared. Additionally, `VmAreaNew` is `!Sync`, so it cannot be used to write in parallel. + // The caller promises that this does not set the flags to an invalid value. + unsafe { (*self.as_ptr()).__bindgen_anon_2.vm_flags = flags }; + } + + /// Set the `VM_MIXEDMAP` flag on this vma. + /// + /// This enables the vma to contain both `struct page` and pure PFN pages. Returns a reference + /// that can be used to call `vm_insert_page` on the vma. + #[inline] + pub fn set_mixedmap(&self) -> &VmAreaMixedMap { + // SAFETY: We don't yet provide a way to set VM_PFNMAP, so this cannot put the flags in an + // invalid state. + unsafe { self.update_flags(flags::MIXEDMAP, 0) }; + + // SAFETY: We just set `VM_MIXEDMAP` on the vma. + unsafe { VmAreaMixedMap::from_raw(self.vma.as_ptr()) } + } + + /// Set the `VM_IO` flag on this vma. + /// + /// This marks the vma as being a memory-mapped I/O region. + #[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 only 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 option 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 readable. + #[inline] + pub fn get_read(&self) -> bool { + (self.flags() & flags::READ) != 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()`. + #[inline] + pub fn try_clear_mayread(&self) -> Result { + if self.get_read() { + 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 writable. + #[inline] + pub fn get_write(&self) -> bool { + (self.flags() & flags::WRITE) != 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()`. + #[inline] + pub fn try_clear_maywrite(&self) -> Result { + if self.get_write() { + return Err(EINVAL); + } + // SAFETY: Clearing `VM_MAYWRITE` is okay when `VM_WRITE` is not set. + unsafe { self.update_flags(0, flags::MAYWRITE) }; + Ok(()) + } + + /// Returns whether `VM_EXEC` is set. + /// + /// This flag indicates whether userspace is mapping this vma as executable. + #[inline] + pub fn get_exec(&self) -> bool { + (self.flags() & flags::EXEC) != 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()`. + #[inline] + pub fn try_clear_mayexec(&self) -> Result { + if self.get_exec() { + 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; From patchwork Wed Nov 20 14:50:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13881318 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A083FD711A7 for ; Wed, 20 Nov 2024 14:56:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 923606B00A0; Wed, 20 Nov 2024 09:56:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8D1316B00A1; Wed, 20 Nov 2024 09:56:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 74B216B00A2; Wed, 20 Nov 2024 09:56:36 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 53E086B00A0 for ; Wed, 20 Nov 2024 09:56:36 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 16E5280D8E for ; Wed, 20 Nov 2024 14:56:36 +0000 (UTC) X-FDA: 82806772524.27.C62BA90 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) by imf21.hostedemail.com (Postfix) with ESMTP id 4FDD71C0006 for ; Wed, 20 Nov 2024 14:54:56 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=euVXFQDj; spf=pass (imf21.hostedemail.com: domain of 3oPg9ZwkKCMkp0xrt6Dw0v33v0t.r310x29C-11zAprz.36v@flex--aliceryhl.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3oPg9ZwkKCMkp0xrt6Dw0v33v0t.r310x29C-11zAprz.36v@flex--aliceryhl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732114533; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=aBJMJ0qYrky91mwQ0u5syokhvobq81rvT/hL+9f8jt4=; b=7YfCnYkrAobjt+4m5TPd0ULAq5lZP1V58vAEZs1Vmf6AfeZqWiBgOi2OktvjYaDnHZP0fo eGwO7piAso+5ggw07oqiqLZ1gAukzO2YfW4tTJ0zMVc3CrTCvcIJ5KO5MxyWUSy6M+l7mV rbzhloqOJ7d1u3xpewQQMqnuckJGtoY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732114533; a=rsa-sha256; cv=none; b=y0SZBEkjOZ8Z2CQIkpLP2s4eudxc2Wju1KLW7RdCa+m3ZJ2hoDm/BewGZZ/WUFBbwONJ7n v8R5jcPII/mvK29wnlvPN+DhxvQqXDVERjKIEPwxfdaqsscIZoJoIBv+ujcaE9EiLD8pYT oxq6wuPaf78talz1VcDKbQeLuiTuNzQ= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=euVXFQDj; spf=pass (imf21.hostedemail.com: domain of 3oPg9ZwkKCMkp0xrt6Dw0v33v0t.r310x29C-11zAprz.36v@flex--aliceryhl.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3oPg9ZwkKCMkp0xrt6Dw0v33v0t.r310x29C-11zAprz.36v@flex--aliceryhl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-382412f3e62so2043041f8f.3 for ; Wed, 20 Nov 2024 06:56:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732114593; x=1732719393; darn=kvack.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=aBJMJ0qYrky91mwQ0u5syokhvobq81rvT/hL+9f8jt4=; b=euVXFQDjH6aVhUtIm8FkzXBu9ific+pCNlUKdz3rN+V7GrucQUSTRB7YAaeByBzF4l QTy7bbGBJ33pm+MQCv58vOOfUtlq7tf/cJzfvnEsJOuhF1sN5JhSjCJnEgNuWqtQ27UG bP32/io8dqMoseVz8mYfT8Q9Y3sfo/AD1IhPDHUs/WLSny8UyB/HA8K/4XUXsp22jiMT tOHfEqQo70Fo04mp46gLPH/Rg996shlhvFZWFB54XepPuc+JnMmTUafezKKyiAhMmajf zB6hN0q283S/TTQOJNzl/RpxBhT1AW7wtLpj0F5zygSN+Vuosk0h+/hghy1tGC5ZegQ9 gtGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732114593; x=1732719393; 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=aBJMJ0qYrky91mwQ0u5syokhvobq81rvT/hL+9f8jt4=; b=KHLfrfeMrkF1RU8m7wbhNSnQ0etnrepJmOmVg4i62eNGJiUzJroEt5/2wEvPRTityY ppDQRkuWhAwNbYhxEPYF9lEm28MgnuP0mbaXkXtJ0G82r6hHlg5fO3/IP7XEtWMlVPUn 6czcFCFAKVoJfMsnvhRHDNCZi/2UAHcbDNstYONCyW+UNMR7QpqYnh96EkAHs8NC3uNP +hy/4QdeR2WkGREAG40js8dUuobG7JekESuoQ8oUqeIYUl5cOmK4KOj7xpGkImboMKG6 oibo8zmMW+kLfztxRpJLH6b3crqN14GxLHGsAH7bg0Gz+XQrVojrSmY8egiO04DPZAC8 S+Mw== X-Forwarded-Encrypted: i=1; AJvYcCXddQPieEIfpaQfFmFzeFC83pu+KN+PwEchoHZ7qvqm82EPZ7afWJs48WDUB2B5TB00z1yfwzp0uw==@kvack.org X-Gm-Message-State: AOJu0Yw2uQx0QklfH1SeHJAi14X1tJWLe3jlylzdEZZAjNqvO5+IfHab hBHjKSGkMzYv6aGrMMgNFsOqDHHVykW4Sbk9DPLaMvWSQHuOkOIIXFDMMCzvirotrPzcRCapYY9 xStZGZiRPaTpZ5A== X-Google-Smtp-Source: AGHT+IHg6gC2fHXTfoa8HybdbCejL4Hw2wCXFWVyfLSy/FMj3wKsyFBkjC9iLS8+VOnYGq57+fL7fZy03dC8naI= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:68:fe9:ac10:f29e]) (user=aliceryhl job=sendgmr) by 2002:a05:6000:148:b0:382:3745:e843 with SMTP id ffacd0b85a97d-38254a8593dmr859f8f.0.1732114592756; Wed, 20 Nov 2024 06:56:32 -0800 (PST) Date: Wed, 20 Nov 2024 14:50:01 +0000 In-Reply-To: <20241120-vma-v8-0-eb31425da66b@google.com> Mime-Version: 1.0 References: <20241120-vma-v8-0-eb31425da66b@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2667; i=aliceryhl@google.com; h=from:subject:message-id; bh=GSw09bqyA6K1PcCK8gagOT2XwF7uH8Q/0z1wyixcHDs=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnPfiM46FQ43oKCfz0YbqQO2Vr66Kdgk66Jo5tx JPUulsyfy+JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZz34jAAKCRAEWL7uWMY5 RkXqEACEB7yn2mFW0lgsKxYBQ4qe2+54UutRvkGiIwc8wfu0WgQ+1KUd04XtaEmpyHADZ+itLu4 w1PUr+Qf3HccuAeWtsE12ckyvdIacsnpDEl73ajj1jGwJsZwDgM27DJB3Pe9OaB8FTOZAPbaLEZ JOmLqMLXCULYc26n3q9/Yb/FMwV41XYcxFPovNlq16vpbsQ0PTY9A//QKAhhINkxA+HD6etiNf+ bMN7aL4D5wJW9KHNeA+ubufEouX8yDKZF21ft/+ZyGHSs2bL6qlhwG2ZUly0H8wtI6G0XHciaNh xe2agSElixu2sn+THcpulk9QebWT0nAW3alVucVH8TVZPzXB04MQdA1cMzSxg8yHCSlRdWdCtAi jJfO979h+KcrnKE2+KMi6YV1lr9SEcLHt8q12IeUWIZDUD7Bb8JAC9zi5B67xB11hl2HA4kfdZm jo5jJp9K3lAgAmFrxIhr5PhOpPUauxswnhtFQLl40gmlHRUFh6eOngHO/oMoCLRqKY3hRlw8nv2 MIK9KFyNCYT2SFWifeBNJ575bVLcsnE9ESTUpSgsQU1V1SfWI+UENNWsPrxUd+sIY4rYkGrSTn4 1ptpfZuhqYA6ZNOY9B2j2hhTh0rf1jOBrWFkm0AuSio6VJCdcqB3J4dmzMR4zSY6VslhXm/+yV+ q10Bd9/rJyXui7A== X-Mailer: b4 0.13.0 Message-ID: <20241120-vma-v8-7-eb31425da66b@google.com> Subject: [PATCH v8 7/7] 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 , Christian Brauner Cc: Alex Gaynor , Boqun Feng , Gary Guo , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Alice Ryhl , Andreas Hindborg X-Stat-Signature: s47bgz7ypgkhim1q1hsrbrfjq5wgdf5a X-Rspam-User: X-Rspamd-Queue-Id: 4FDD71C0006 X-Rspamd-Server: rspam02 X-HE-Tag: 1732114496-524807 X-HE-Meta: U2FsdGVkX18fhLISGth+l27D1UbVMKiCpe129NoIR5gxOK9f3oFycylrDMXf9KxhSsE1XGKTRLn3tOga+l+XU15tgx12AKsysGfEm/LLsQUGKYPdtY9Rp6mtAg1KV/W/ULAjUhyNTRFN58DYQE2WvIg7bh3fwa+IWsJz/PB+IgS47N/ws/MoJTs93QTZ+Difb39Xn1srFZADT26Okh/rH4/KbIdKqOe7XLTMQBz6b70YdkJ4mbTW4qgQLHLM1WPE0BLR2gZdCGw9SaFMzx3O9ZffY46/8pNtDimABZgAm0mcs4J14ElcjEsiHVf5Iylm1dj8VDl5R2A5+CgKzodAeEVtnLkci7CUWvyWMCUKp+JImgCV34SvYtYXqVQtx4HcdiAfK6Bs286EyQPil5f0oIneFQzKB7oauzSxP3QVc8LUWPS4PVx/IsUo0x+Pnmwvyrj/TzQaVvqcTRKwEw/hfkJKcm80ucCmg1dkZql/LKBKU1hDIuPw8A9UaTCQK4PWuzchuZlIflCWrZ+PuJoX67nCM+YPuk32Bctf1f1W6BTCtbmD+8ehyQmC3ecu9ufFBlH6OLlhodVjXo/CllBgryiW76oUn4zXdpoKMlMwFfkAkhwxPAug5A9YyXko5zzkw2o3EFHAqfB6523eqa7Lf+/+e4/gp2aXRSYQlmOyaZRoSzMVUYClX3U2CBpOpgrd1LphlQohSBGhWeZ2EbiN3ghPHamsvUVdD9ZlkWTCuLFu9wocuwuyT7dbOutkP+81D2SAgcmv7VF8cpFIZff9gRs4uOw4sZ9Z+RuILAIrThQZ5b4S4HnV0n07dghV/OYg3bvibRLq4ANgh3Zi+ptKTAFtfzMv4UWq1som5xMpWonaV0KotwD2nZzLznWiqUvIa/wfF8BU6ZKH8IIDYElh5zgjnX3zFmbpzyyVmwVAFmhrlkLZ65eS5TofgKadMSQhziqjGyNX/vBZIw0Ze52 iheA/0Bl PqMWWfm4sANdHntgsHrs8OTBRP29fCCjB/T70Z7SRSPSlSOf3bzyA1eX0yvjJp2tZuUXaAMfOoyniiNXfn3+vCN7ps571c46ObcAptz3qXRHZT8JoqMJd9hkISwJ96mhzyFppHT/yCh7pceP8/nxL0BQfMlBrfYfa3COy67WSGERvFim+KSlTDxtCJ5MYj+0H0VQkwPnm3jvUnCheN/tbkpA4VCR20RFjZaMp7LaKrzOfOqvE0QeOc+UmwmrRKzD6qkrkBOlA6zgIrxbTCaL2uK6VACSnjxu84Lb99AOGvbr1A/QoYSBMZ2URPjEw1uP6qdQ2uPgo+q1lEXWTTmLOas0h9CYnA3dEZB2sVdbKhCTm5WB1dPmfJjWd0QT54DqiFB/9K+gVv6C7ZYJGn8ZYNs2Cf3djQNccPBs3jqp0B+sY2wr+jWpck/JQESIzUBItKQ9rTOnrXn9xmMimjTohyP5pWvXycZqu/13xvTgOvGioNGFCo22D7hNUUXanxog9gqRhiHu7qiX3pUxkq9bgdqk5re4yEdWhLRF5xxze7ae5b3WYwNeRBAysO7J1s2IVnTa+/+N97B+5/dme+vW7hIWpPYooSNhvFNgrxdX1MENHKGJZKZcKEtm3yfCba46fk+ICMk61HNHl20DkWXSY+9fwV60apsuXmhyOCd1d7VBLy4M= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Using the vma support introduced by the previous commit, introduce mmap support for miscdevices. The mmap call is given a vma that is undergoing initial setup, so the VmAreaNew type is used. Signed-off-by: Alice Ryhl --- rust/kernel/miscdevice.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs index 7e2a79b3ae26..4e4b9476e092 100644 --- a/rust/kernel/miscdevice.rs +++ b/rust/kernel/miscdevice.rs @@ -11,6 +11,7 @@ use crate::{ bindings, error::{to_result, Error, Result, VTABLE_DEFAULT_ERROR}, + mm::virt::VmAreaNew, prelude::*, str::CStr, types::{ForeignOwnable, Opaque}, @@ -110,6 +111,11 @@ fn release(device: Self::Ptr) { drop(device); } + /// Handle for mmap. + fn mmap(_device: ::Borrowed<'_>, _vma: &VmAreaNew) -> Result { + kernel::build_error(VTABLE_DEFAULT_ERROR) + } + /// Handler for ioctls. /// /// The `cmd` argument is usually manipulated using the utilties in [`kernel::ioctl`]. @@ -156,6 +162,7 @@ impl VtableHelper { const VTABLE: bindings::file_operations = bindings::file_operations { open: Some(fops_open::), release: Some(fops_release::), + mmap: maybe_fn(T::HAS_MMAP, fops_mmap::), unlocked_ioctl: maybe_fn(T::HAS_IOCTL, fops_ioctl::), #[cfg(CONFIG_COMPAT)] compat_ioctl: if T::HAS_COMPAT_IOCTL { @@ -216,6 +223,27 @@ impl VtableHelper { 0 } +/// # Safety +/// +/// `file` must be a valid file that is associated with a `MiscDeviceRegistration`. +/// `vma` must be a vma that is currently being mmap'ed with this file. +unsafe extern "C" fn fops_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 = unsafe { (*file).private_data }; + // SAFETY: Mmap calls can borrow the private data of the file. + let device = unsafe { ::borrow(private) }; + // SAFETY: The caller provides a vma that is undergoing initial VMA setup. + let area = unsafe { VmAreaNew::from_raw(vma) }; + + match T::mmap(device, area) { + Ok(()) => 0, + Err(err) => err.to_errno() as c_int, + } +} + /// # Safety /// /// `file` must be a valid file that is associated with a `MiscDeviceRegistration`.