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/LN7m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gjWgZsilVcJ40zclpHS0HHGDOg9WYoZs/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.