From patchwork Fri Nov 29 16:32:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13888844 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 DC499D729E9 for ; Fri, 29 Nov 2024 16:33:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C991B6B0098; Fri, 29 Nov 2024 11:33:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C49136B0096; Fri, 29 Nov 2024 11:33:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AE9746B0098; Fri, 29 Nov 2024 11:33:02 -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 891256B0095 for ; Fri, 29 Nov 2024 11:33:02 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id F27664119D for ; Fri, 29 Nov 2024 16:33:01 +0000 (UTC) X-FDA: 82839676752.12.539CF68 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf07.hostedemail.com (Postfix) with ESMTP id 6140540007 for ; Fri, 29 Nov 2024 16:32:51 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=nlGyf1RM; spf=pass (imf07.hostedemail.com: domain of 3uuxJZwkKCPsdolfhu1kojrrjoh.frpolqx0-ppnydfn.ruj@flex--aliceryhl.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3uuxJZwkKCPsdolfhu1kojrrjoh.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=1732897976; 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=94NYmr5c9ppvPDgAeOeEOikiVfAFQj6mOJl+8qXFwoY=; b=J8+EMM9K4Rog5t3qGQFAimLbKR39nHtxW8Oi7129silhRVvFNA3KXqujtTYDhM34yvLoOX 7pz1SQfXEXxyjHllcWxPOKy1wumlmp7SbTbNk5Zx06EKA9GU6v/2+Y1rr5NPC/X/pXfoh+ JYuNgHbctfN7w8KWezMUMHrgIz1GwB0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732897976; a=rsa-sha256; cv=none; b=YfjV3RIwq8EdtXCVOt0Or0ljJ7K+fGIbMOQRp0YJqRO/06k6xpgfb//MfQiY33JYn707lj 6rWhlZU0s46GuGSxKZs/fxeC9Q1/OKyTqR9CU0fW+/DDov4BnVklRUBZ+SlProgT/HhZ0H xs5WqCHGva15UnywCYVzDpWQAfK9vw8= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=nlGyf1RM; spf=pass (imf07.hostedemail.com: domain of 3uuxJZwkKCPsdolfhu1kojrrjoh.frpolqx0-ppnydfn.ruj@flex--aliceryhl.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3uuxJZwkKCPsdolfhu1kojrrjoh.frpolqx0-ppnydfn.ruj@flex--aliceryhl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-434a90febb8so12014765e9.1 for ; Fri, 29 Nov 2024 08:32:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732897978; x=1733502778; 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=94NYmr5c9ppvPDgAeOeEOikiVfAFQj6mOJl+8qXFwoY=; b=nlGyf1RMSQwDmU0YU5I8qZOC7ou+EB4w7EXLF5/xK01xNSxr/i3eM92l+y6wm8ag6d ruZ7alT8O29QGyMPZFt9zJTnOTFPz7cTcMo3KgM3YII/Qo1rKDN8JtRIxTcC9IfcTL0a idD408jRo2MqDo9/mo97wyy4lKwKn1AsCprjXk3ROsWYOFH/IdocJUNzOC0iA04/rcPx okMr4iByTWRtK2yWg1pK+wsLP/xTJs9gucG8Yjd6MkvMc/Z42tJ5MoWI39YO/EPmdsWt BVHXm6GZTU8scLIbu2wcguknz5GODAaQoCpS/+kukVfQKuvxQUJobvEXactqUxUBtkZE YYDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732897978; x=1733502778; 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=94NYmr5c9ppvPDgAeOeEOikiVfAFQj6mOJl+8qXFwoY=; b=OHl7p+BdOXICvoVaeecuNJ5JH+PLXOeBGEHgB88u78JRnBSXfgiBrfYDdS2tIBk3Iu nRggVgayhlftrwOw22A/LopO1MARD8llPVgGFLxBSeQGHDhtbkFR26vgbsRSWDSk0CY8 +QjqFZSbkj4N0n4kdZPESMprBBsBYn7y3AsFFBlohrMLrXixcpmFLEykMYsH5zrg/wGn Am0y8Fru/ONkC4Yz3AMWX3tmEVn7BA46784Hv29a2O3qD5ZwT+doZcSg3L0+gfNPY5yw dVWs6zGj6YEnMwFQLoKfygTJu4FQ9puuYDBUqqAw86KB5EVExdJ53lrKiMbrrT9ZRFow sjKQ== X-Forwarded-Encrypted: i=1; AJvYcCW4Y4kwFlZdFIW9KfhHnSabnJ3nG9tGtEM2hdB73gmniowaVsZIduEI8cYBk0xTYcAuyl+PVWl4cA==@kvack.org X-Gm-Message-State: AOJu0YxZzdNs+Ibx+s9Br0e7V+5ci6LEvbYwKQHNudGqie/4bw1dF3Tb 9iljt1Kw2ggwrxrhBuxuq0QXLOA2FBKinnESgzCRBEPky/0xjMEhGl0Ym0UBqyFF1VuR5e+/eXb YCgtudjZazBIHdQ== X-Google-Smtp-Source: AGHT+IGrQvjMp3red6jWsR1GGii7AoihEDmHs9z4agdmTgk8GW6+jX/m+iq6oJEN70Cr+c0nxG1Xp0i1oBsbhdE= X-Received: from wmpq23.prod.google.com ([2002:a05:600c:3317:b0:42c:b336:8685]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4750:b0:431:5871:6c5d with SMTP id 5b1f17b1804b1-434a9dbbd1amr138602905e9.3.1732897978665; Fri, 29 Nov 2024 08:32:58 -0800 (PST) Date: Fri, 29 Nov 2024 16:32:36 +0000 In-Reply-To: <20241129-vma-v10-0-4dfff05ba927@google.com> Mime-Version: 1.0 References: <20241129-vma-v10-0-4dfff05ba927@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3694; i=aliceryhl@google.com; h=from:subject:message-id; bh=d5PVsjv5Dk2wDmJvfAYSWwtPELYfnKxOPm05AGfN2t4=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnSeyuYNpPwhGV89bUWs5GRH8aX7HH6P4Ztr1U0 bGuq73pEJ2JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ0nsrgAKCRAEWL7uWMY5 RqU2EACmE2VsGq4kuy2dH1HczWotSUGlrvZA17ocbWpWcLJh0JQ6N+LZjV0Xu6m6TTovT5IofyS XcN7yaQjwdI9oGmRkOFB1oRLjjAxEUwr46P/sjxC1UgiuiQaJv0auLW8LP5QiGKA+UaNqGQCcdO eGQMKqM4TikGluAykvFlxukfGrYNYTU6sQOVfaZLY2t+/SVMUva/uwFPQh1gpv9vTB9D/Sgi5Wo UCvQFh3JwkBvVaijNJoF6ONbRTdCbLiMDc9dVRNzZfDRw0uR4fQDzMEkSRkJ3v5+fScreYBljBK k0B1DXlJBNp212kQGxXIA0aC32E2UwuKNb9dHMPpBko8yZwn7Rrf3sbMU1fcfiXhneXaFOUprO9 H1v5/MRfiqsXXQpFiPQP1+SRjuHSlQ1wJnCOKaViw++5mXvDilf2gbMZO8HhXVGSB55HaS0a3U/ NjQPkNTFXTBDV8r76dThH5WrcX+Es5A2yJ9gVfTy+Busr0pH9IgPXxO6lorkt4eOoKXvgD1pto7 kfaSTKS+sHsPqpcgAJeI8RikZWbszcU+qzT2Z0y1akLeFVehqY5O7oMjESLS16mMpy4ui+8Dkc5 wDnJuzljFqHjFo7j68LGNQ7CVPHglJvW6OslNF4+CePBEE/j1kEWVjZl4+WoxVFvU+zMzj8t4cw YUSBsw9j8CvU+QQ== X-Mailer: b4 0.13.0 Message-ID: <20241129-vma-v10-3-4dfff05ba927@google.com> Subject: [PATCH v10 3/8] 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 , Jann Horn , Suren Baghdasaryan Cc: Alex Gaynor , Boqun Feng , Gary Guo , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Trevor Gross , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Alice Ryhl X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 6140540007 X-Stat-Signature: msf81gudakk1swm9fntqcynbbmhmppx9 X-Rspam-User: X-HE-Tag: 1732897971-618480 X-HE-Meta: U2FsdGVkX1/CaQYcqE8Pnkv8tv8IlVUi+YcCshQJhSxBl7AUrqMzER0xnDUL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GwnGFbpLNIxdetoA4RzGM4wAneM0HlGb3gnWFYTWYXKh4Xnnaj9laA9AMaCLqHcHJRb7/xSV586uwE1WX3enH6yRUVMy5sgxjG80wC6XaNHxq+KcDbCG176trCRVrtCJFfCUFcnoFPTs4M3CMVz/YMZpwInuDxqZCaW6ggcHLDk/FaSrbzirUbNwCqKIscr3bUnWF6+4+GwN2Ez4AOyYOXv6085opU9StfcvCzIjuTAv7VziY9rdyW2/deGWS1pOmg4+YetHks2igWHaCVef8mGUuQM+AQI7QF9b9GwTFxWsP+LtTx0GByVnppN4jZZtccy1OlBFIMCmDO+OENrVC1vxAoMzzrge0wBje1pA6iqBQ8Ew9OgSVkuV/boIuCBsElBJ478QclXDezEVdYTVo2Tvg74z9EukXn+uQfK1ccvhj4GP2yKhW6PsXfARkBTW9l3ZcgC8FCJ0xn0G9taRLMZkmyw9RSyUkb4XKFFi6Jupx7rotXXxWcRKKVIhByLnSTqgBRVmMxMnPjrHhcJEP39FOcSo7DTLW0jRcMGEjkzNjJVCNp6LzbrAhxRubgRsI= 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. Acked-by: Lorenzo Stoakes (for mm bits) Signed-off-by: Alice Ryhl --- rust/kernel/mm/virt.rs | 70 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/rust/kernel/mm/virt.rs b/rust/kernel/mm/virt.rs index 032eea4d4690..fa1fd6aa703c 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. /// @@ -91,6 +98,67 @@ pub fn zap_page_range_single(&self, address: usize, size: usize) { ) }; } + + /// Check whether the `VM_MIXEDMAP` flag is set. + /// + /// This can be used to access methods that require `VM_MIXEDMAP` to be set. + #[inline] + pub fn as_mixedmap_vma(&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.