From patchwork Wed Jan 15 13:35:06 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13940446 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 58C08C02185 for ; Wed, 15 Jan 2025 13:36:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 11DDB6B0089; Wed, 15 Jan 2025 08:36:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 03022280002; Wed, 15 Jan 2025 08:36:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D9BC5280001; Wed, 15 Jan 2025 08:36:14 -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 B2F4F6B0089 for ; Wed, 15 Jan 2025 08:36:14 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 740FAB0293 for ; Wed, 15 Jan 2025 13:36:14 +0000 (UTC) X-FDA: 83009785068.20.78C096A Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf24.hostedemail.com (Postfix) with ESMTP id 7D023180005 for ; Wed, 15 Jan 2025 13:36:12 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=TRmfW6gb; spf=pass (imf24.hostedemail.com: domain of 3y7mHZwkKCKAALICERYHLGOOGLE.COMLINUX-MMKVACK.ORG@flex--aliceryhl.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3y7mHZwkKCKAALICERYHLGOOGLE.COMLINUX-MMKVACK.ORG@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=1736948172; 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=iAB5+MkDlqluj6B7QpzdYLJZpgBLqkqOo5ngR0sMgMc=; b=EELYvDprLTKt63h+34tL047qiheSEOmTfdpcwxLdPwR/jVsrhZv/D77tb22zcsnMXEbX6T Z2t72XQLjpKUhiI9Z47aarqJjVebAhSHDiJY4qao10z5M5NaSjr+xJnsIhpX0yK2sU6F7M v80B/dE33daQLEvhFoh/FOdXScGc2zk= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=TRmfW6gb; spf=pass (imf24.hostedemail.com: domain of 3y7mHZwkKCKAALICERYHLGOOGLE.COMLINUX-MMKVACK.ORG@flex--aliceryhl.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3y7mHZwkKCKAALICERYHLGOOGLE.COMLINUX-MMKVACK.ORG@flex--aliceryhl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736948172; a=rsa-sha256; cv=none; b=4kAlgI7bt2KMAi8wzalj+frL0SGvo2R3OR07pUFR1hGjmqI23PC99fOxJE4gah8ppCtVRm YxC34D7r5GciiJEqOmSHkJrKpU8DKktAZ19AefEzWvSCkJzwv/Wosbn5wHALJZuDm9GNFt Cgz4igR4Vqovmn6lrK06q1Ni5DQGh9s= Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4362f893bfaso36067155e9.1 for ; Wed, 15 Jan 2025 05:36:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736948171; x=1737552971; 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=iAB5+MkDlqluj6B7QpzdYLJZpgBLqkqOo5ngR0sMgMc=; b=TRmfW6gbI5S9OOCT0Y2SobG9IlRXfSQngDjd417TzouQmXFS3B2/qIj3uqc56SArGz /Bl2zWNSLZ5Of0wh0VtLcnp4wX7L7bVzuC+eFWbBvvcNiTdy0aXqTeneebwCmUx1a/Yh PCLTDc1/N0bspFmtjr2/0p7tewkegrLP83jmCB1f8EJ98STyn9K4L3tnbzNSOD2YGDUU DFW/WaqsA9uHlrXnhbu3gmA3hy2+wzFBZR4r1rDt7+H9yLJ+SwbW4JlIOWPLmMDcGTG6 JyBDsGdJ9jyWduzbngjzm4MOqPhXxXguKtOmnxK5yPoXaVBlPIfXECI/TRacn7pqoR4Z SSDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736948171; x=1737552971; 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=iAB5+MkDlqluj6B7QpzdYLJZpgBLqkqOo5ngR0sMgMc=; b=qDII745G7ir8nFhNfmonIgLPyA7ByTop/ltwNuGH2mRWqAt2vS7lGum9z3NBj+rp+9 EILnXS6XFbzfKyUfyfYehJ/5gSAutKJxlTIhdqyuelrF3B7bdRx7G4jffZz01sMQTptt aME2w+fkeFH48C7vs8O+GvIwDUha4BC5JVN+fg6CNgx4cUUXY07V/WWlI43D5Ybgk7Np bQjRsTqiF0BvMeqrJlSyBJqYb0zV9JoeWvXQH+MU/cXvaXfSnkEgvReLNl8q6Hj/VQvx LElpwu1gcJiGJAHQJJr995Z0Z+bQOgNgbLnYmLj6TsAM63pD5EFeQTiE0WysJLeKMdHz vjLA== X-Forwarded-Encrypted: i=1; AJvYcCVW0ul3rf3xgGA/24XJ2Lb3B3wnZNJf3SR0YeQzfV/XscBvKzVST/OvVhDajvsHhY6Pm8Zj33CFHg==@kvack.org X-Gm-Message-State: AOJu0Yym2ISvio0Bi93+x+Xyx+1DaiZ2O9NteonZHR9ZVTZvy5GQIN3G lXFlcMBcOikvoQtgcW7wWq+UXZgZwPZy7Kvdt0CkA4Rh30PaXVl0HHsPeO4napgWsjAl4wXUg/C hhXSdSobYdbRbVw== X-Google-Smtp-Source: AGHT+IH4/tFhizC0Tlbso0pfJWClZ8vsinv7fWPPpakyMd1YYPpuWimTTutI9DYbU8xxDHrbDqgJ8v0xT0gjcNQ= X-Received: from wmbhj18.prod.google.com ([2002:a05:600c:5292:b0:434:fd4d:ffad]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:314e:b0:434:a5bc:70fc with SMTP id 5b1f17b1804b1-436e269aad7mr260754245e9.8.1736948171131; Wed, 15 Jan 2025 05:36:11 -0800 (PST) Date: Wed, 15 Jan 2025 13:35:06 +0000 In-Reply-To: <20250115-vma-v12-0-375099ae017a@google.com> Mime-Version: 1.0 References: <20250115-vma-v12-0-375099ae017a@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=4166; i=aliceryhl@google.com; h=from:subject:message-id; bh=DkijNTCWjzL+GZ9A/ngSiwPnkdD9yqoZbhkXBZ1FzqQ=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnh7m+n+HRp6TVitWGzxTZmNyfMlBYn2nHJh85c gF39+HKacqJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ4e5vgAKCRAEWL7uWMY5 RkLSD/4hAr6ppSvTg25k4E1epqUVJqhwKwShK0kGDC2daG1PTdOf7HHIv2izh6j3qOiJU5pgh3e JBpHueh/ss+r9ARsw5Vp0y8VmJRhMphbTUgNJe6+f/TzTmBbwi5FWj36eKetwmB+6x32ApYd3bW b9zGR5ekfUIHCUKnn4ybbKv/+eO4qkmegPEzNpFE6ZemT/1bvOscQtuiU+1zEXtE6oMK6MhHrWM 0ntaU/0JSoLHdsCRv2kkmLI741cNl9wpplYF17izNbhn2hbCXHOtqpuzPXyfbqr9Frte5i6Ea8p FJTF4wJ3KRxwb52DoikPJXCzUprqFedDHjaPo9NB9eXQcXDTktrviZs74nlCx2IVINhEQW9NFqT lxiyxB059onff3iZax0SYVxFr+jH4w/YdiPd4rrpf0s3eMtOPQ3rYVPOK8dA5JP8uxiYhOJHFO5 XjQeCofzoPVvwKq5HLsa+/pMaIXrSQoDJ0SmdYtAAsQKCpI4XUrrXS9PVDisEsGcL+r9i3CrlCz jZwa2hoidoGaAip0/tCOyZPxeW9uO9X220T4m1wQeIzUS06ggzIWX3dbrSd7HpHg2/0U3OyZ4Zj l/0rq57Zp7TS9fPY2JWKtz2TEpyegxfM/AOuhYGKPzf6OTraPQnjWNhd8ncJL6WAWj0Zai8jcPC 0zsvqbfT7/0ElCg== X-Mailer: b4 0.13.0 Message-ID: <20250115-vma-v12-3-375099ae017a@google.com> Subject: [PATCH v12 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 , 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-Queue-Id: 7D023180005 X-Stat-Signature: 43pah81yus9rq63h8mjdzdptz4nj4yg4 X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1736948172-447917 X-HE-Meta: U2FsdGVkX19EPqxviGfPa8TekME6HUvu0zZSkyEvZqVY4SfOVrSvj1GJ5x1n6lVAZCRpL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qVtNHy+NpqsQvfABSn8nh/Z62gE1KyDjYXM8Ob98mAbBeFW4Gq0gnqcavQZewNeg2fdnVlXLsf2rbHYtkT2Si8qQulSq+/J4JeR0lFq48ys+kidhKsioR2t0d70mwJVe1OJyc23Nw+49jsWfF72gSwdccrqXdZqIxox+k+D985ZysCpDyxGlHvtj6otWfoFAKmU+y92RcMm99gvHVwz3JGn3Lw21bXIjXf5oyeN5S+1zYr+ekG5FP/LqXub19Z4eOuVYsF2J+FTAzVS1QdJe5kUjp+cm2fvRzaMes48ooX4YwX12m3ZEMGomw2/d1nYBM2mBs/c05aWFhrdl4Z4rRgTcmHLeuHZi3MUsx+G69PWxjE8onjhFo51Km9AiQiRXp1viNGFRMbXHr8G9xXTC2QFmXyRqB8JKPDw3c6wynCJfjeBK+ULW4od00AKJPwzY2lkpsoZS/wV3Ynke8Oxqr16BUSHwnslykKbrABIEDybn7AiVkn40R7hffdKQcUmOjnFy6RZy3Pl0ZSKxGqmLkU4= X-Bogosity: Unsure, tests=bogofilter, spamicity=0.493972, 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 | 79 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/rust/kernel/mm/virt.rs b/rust/kernel/mm/virt.rs index 2c7de0460e0a..ab89a526d3e4 100644 --- a/rust/kernel/mm/virt.rs +++ b/rust/kernel/mm/virt.rs @@ -14,7 +14,15 @@ //! ensures that you can't, for example, accidentally call a function that requires holding the //! write lock when you only hold the read lock. -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 access. /// @@ -124,6 +132,75 @@ pub fn zap_page_range_single(&self, address: usize, size: usize) { ) }; } + + /// If the [`VM_MIXEDMAP`] flag is set, returns a [`VmAreaMixedMap`] to this VMA, otherwise + /// returns `None`. + /// + /// This can be used to access methods that require [`VM_MIXEDMAP`] to be set. + /// + /// [`VM_MIXEDMAP`]: flags::MIXEDMAP + #[inline] + pub fn as_mixedmap_vma(&self) -> Option<&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. +/// +/// This struct is identical to [`VmAreaRef`] except that it must only be used when the +/// [`VM_MIXEDMAP`] flag is set on the vma. +/// +/// # Invariants +/// +/// The caller must hold the mmap read lock or the vma read lock. The `VM_MIXEDMAP` flag must be +/// set. +/// +/// [`VM_MIXEDMAP`]: flags::MIXEDMAP +#[repr(transparent)] +pub struct 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: By the type invariant of `Self` caller has read access and has verified that + // `VM_MIXEDMAP` is set. By invariant on `Page` the page has order 0. + to_result(unsafe { bindings::vm_insert_page(self.as_ptr(), address as _, page.as_ptr()) }) + } } /// The integer type used for vma flags.