From mboxrd@z Thu Jan 1 00:00:00 1970 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 768F2E7719F for ; Mon, 13 Jan 2025 10:10:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 124CA6B0095; Mon, 13 Jan 2025 05:10:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0AEB56B0098; Mon, 13 Jan 2025 05:10:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E69F76B009D; Mon, 13 Jan 2025 05:10:28 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id C50876B0095 for ; Mon, 13 Jan 2025 05:10:28 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 6B789A1D42 for ; Mon, 13 Jan 2025 10:10:28 +0000 (UTC) X-FDA: 83002008936.08.0303717 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) by imf21.hostedemail.com (Postfix) with ESMTP id 6C2FF1C0009 for ; Mon, 13 Jan 2025 10:10:26 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=mjNZTolM; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of aliceryhl@google.com designates 209.85.128.51 as permitted sender) smtp.mailfrom=aliceryhl@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736763026; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ruMIfgt8XeymWn0Xb8GRY3LQRisyMq2NBZDcNhsouqk=; b=4UzROyuzmXqI/aVhgVznYYdUGQG3JirhloAQbq6Q7CFNGO+RMeqhG8HsCTcoexIPR6vPak PzP35fGjcp86418I/3gIYR5KAXeCJmRH1qWKCGoC46lBno7OzbdJ8+i44767SCse8mQU86 iEbU+Qwe2GgoobPW+i3JJFoaSe9Um4Y= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736763026; a=rsa-sha256; cv=none; b=pgaycq9hw9nlOLLc17/HHOsIEusMmq22wvS3xCB+C/IsM1ShJE6mMAGOCxvsPnuKTrUjPc M8Ru1+XtY2jcYj3lW7fla1IsnZxlKApq01LgOV2pe1UOwqJm/Nz6C01Bd8/DcbVnz7Lh9F MAVoAOQ8R9Ngfo6jxI0nkzLQ/3LxE1I= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=mjNZTolM; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of aliceryhl@google.com designates 209.85.128.51 as permitted sender) smtp.mailfrom=aliceryhl@google.com Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-4361f65ca01so39195295e9.1 for ; Mon, 13 Jan 2025 02:10:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736763025; x=1737367825; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=ruMIfgt8XeymWn0Xb8GRY3LQRisyMq2NBZDcNhsouqk=; b=mjNZTolMy0K5cV8c2f0Z3xMPm5kzileeQIL6vlcExbr4uXdQqadkWPBfpZtWF8Zm9U LosASCIT/HYj2RCEOgIoZKWi+rtEhdMxPIohzAXH95bB5uFS7c8X5dBxf0sQsi2a+cbv TGV1wDFFXEGaDfzk4rUOvMKeLZI5TTlsP1wEnqRglJXpOUBdjNsrNQEaf1LyuGf0apSi ME96xfgyUbJUV/s01tu+E7CUWbSErFq0oKaXeS0l/osfiESwMLmiFXw6nRPN3VSsXpdt 3UkGl7azPnORE6J6swbqaPRai5TtGeF8DpCGYrHk2mJN4uMpTdBxpDloAKQthsf2xei7 gtww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736763025; x=1737367825; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ruMIfgt8XeymWn0Xb8GRY3LQRisyMq2NBZDcNhsouqk=; b=Mb1Z5mQsw/b8DsM2iocwKjTpgt4FcAGmweHawYEw3lNiq4A3D+aZ3/d1b2jMALIvJL 7IIeUz9I7wwSfuDh9D6bYYpYh/awNwRXyt7WoprYzBcD327ktG5143ftMt/BdFY9cj8H 0sytCDY/4FfbkVqvLFIe/EMLWdxz2Wl30dKDTfuQmnR+PlTMqTPUwnGqCXW6wqI5/HFm SnXtty9wpubXQG++tSM1g6IzSv7Y/pYDeOHOzEYi61BhTtxlaNpaDuOm9VE9s8H+vfjX /L+Iv2iBQbkB2Mjk+j75BUCCDmcFleEp7FtJwft1Y0kVGa6XmZ1mT71nq7jqOrZPl782 uQ8g== X-Forwarded-Encrypted: i=1; AJvYcCXRM3Xd9Xl7U/nKIYpaWJ21LIbws740IjGHNT8O28HcahGpJCgjrS3GaP1aLHsAsPPLQRFHeYUWIQ==@kvack.org X-Gm-Message-State: AOJu0YxdR3xmMKdczcuMpleFB+BNjO/TxDen6K2eAfh6QZZOAzOPBqvZ e+oY13y6mwjj1T2HrsiQoeYnodOC9TwaC0s2rPotcs6By5wvDqF28xKERyJj5VsxUvu/YXWMbo4 Nfj1pElRtUI13geREXb1JnN1pS8Au8lCsArQvdQy2JvkBhXiTdw== X-Gm-Gg: ASbGncsaMMwoujS0+wf2bQYY+JOi/k6+2X22LDuwIIgeqY12cozeFBjbrQBJfvMxcVZ 21QuJ2wZbJBlblwVnscu8ykb+3ZmMhqCTMACKi1GBp/iGqKnMNE1u3iN5KYzioBRXujtp X-Google-Smtp-Source: AGHT+IGXYroCUwfW3rZ8JLd7wkjW88qfzhWvUhyaT8A7PZu/lBodxoUUkNKOlZdINTG+N767o/YqLm0vh6sHher8abs= X-Received: by 2002:a5d:5f44:0:b0:386:3e3c:efd with SMTP id ffacd0b85a97d-38a8730fa79mr20090451f8f.44.1736762556541; Mon, 13 Jan 2025 02:02:36 -0800 (PST) MIME-Version: 1.0 References: <20241211-vma-v11-0-466640428fc3@google.com> <20241211-vma-v11-3-466640428fc3@google.com> <87o71bbys7.fsf@kernel.org> In-Reply-To: <87o71bbys7.fsf@kernel.org> From: Alice Ryhl Date: Mon, 13 Jan 2025 11:02:24 +0100 X-Gm-Features: AbW1kvaU2mBVYi4r7g9TptFFdAf5JOsLMXkmiZb6_JeC32FLrxX_VQ5DF8tNs1s Message-ID: Subject: Re: [PATCH v11 3/8] mm: rust: add vm_insert_page To: Andreas Hindborg Cc: 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 , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Trevor Gross , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Stat-Signature: rnjxtkgzhturbgh449e3uzx7bdmkkfjd X-Rspamd-Queue-Id: 6C2FF1C0009 X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1736763026-160437 X-HE-Meta: U2FsdGVkX1+YzueQUxMI6zEFcb+MSQpYo/62iQ8aWRAzP73pIFjC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JJ30tNNXFzfHMAOoEnpPU4H9mH8rvTPwPsEJ5fCSgLRtqNAfZBgF4DjHsR5Eal9WqQUZvupP25ck766HwuhgT5E6OUaHtZTDXpCVIOaZBtcKEvYKOL1b/d3bmBaUi012NfTJyWZ2+FYBh2LO/GMSkYCp0Q3kDTRX1valojZwEEnhseH+jBj+tQoqRS8i32iC8X0lKzLFR/85ufmKsDfs0AiWx+oqjTAQRs0X5t8s3dAZ4w79IHGqE X-Bogosity: Unsure, tests=bogofilter, spamicity=0.487053, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Mon, Dec 16, 2024 at 3:51=E2=80=AFPM Andreas Hindborg wrote: > > "Alice Ryhl" writes: > > > 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 mor= e > > than VM_MIXEDMAP and VM_PFNMAP as things are now. However, if that > > becomes necessary, using generic parameters in a single type would scal= e > > better as the number of flags increases. > > > > Acked-by: Lorenzo Stoakes (for mm bits) > > Signed-off-by: Alice Ryhl > > --- > > rust/kernel/mm/virt.rs | 71 ++++++++++++++++++++++++++++++++++++++++++= +++++++- > > 1 file changed, 70 insertions(+), 1 deletion(-) > > > > diff --git a/rust/kernel/mm/virt.rs b/rust/kernel/mm/virt.rs > > index 68c763169cf0..3a23854e14f4 100644 > > --- a/rust/kernel/mm/virt.rs > > +++ b/rust/kernel/mm/virt.rs > > @@ -4,7 +4,15 @@ > > > > //! Virtual memory. > > > > -use crate::{bindings, mm::MmWithUser, types::Opaque}; > > +use crate::{ > > + bindings, > > + error::{to_result, Result}, > > + mm::MmWithUser, > > + page::Page, > > + types::Opaque, > > +}; > > + > > +use core::ops::Deref; > > > > /// A wrapper for the kernel's `struct vm_area_struct` with read acces= s. > > /// > > @@ -100,6 +108,67 @@ pub fn zap_page_range_single(&self, address: usize= , size: usize) { > > ) > > }; > > } > > + > > + /// Check whether the `VM_MIXEDMAP` flag is set. > > Perhaps "Check whether the `VM_MIXEDMAP` flag is set. If so, return > `Some`, otherwise `None` ? How about If the `VM_MIXEDMAP` flag is set, returns a `VmAreaMixedMap` to this VMA, otherwise returns `None`. This follows the example of slice::as_ascii > > + /// > > + /// 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 !=3D 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 acces= s and `VM_MIXEDMAP` set. > > +/// > > +/// It represents an area of virtual memory. > > Could we have a link to `VmAreaRef` and explain that this is a > `VmAreaRef` with an additional requirement? Ok. > > +/// > > +/// # 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 =3D 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 satisfie= d for the duration of 'a. > > + unsafe { &*vma.cast() } > > + } > > + > > + /// Maps a single page at the given address within the virtual mem= ory area. > > + /// > > + /// This operation does not take ownership of the page. > > + #[inline] > > + pub fn vm_insert_page(&self, address: usize, page: &Page) -> Resul= t { > > + // SAFETY: The caller has read access and has verified that `V= M_MIXEDMAP` is set. The page > > + // is order 0. The address is checked on the C side so it can = take any value. > > Maybe something like this: "By the type invariant of `Self` caller has re= ad > access and has verified that `VM_MIXEDMAP` is set. By invariant on > `Page` the page has order 0." Ok. Alice