From patchwork Fri Nov 22 15:40:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13883304 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 3F5E7E6916D for ; Fri, 22 Nov 2024 15:41:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BC1676B00A2; Fri, 22 Nov 2024 10:41:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B6F506B00A4; Fri, 22 Nov 2024 10:41:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A115E6B00B3; Fri, 22 Nov 2024 10:41:12 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 83BD46B00A2 for ; Fri, 22 Nov 2024 10:41:12 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 3C161AF00B for ; Fri, 22 Nov 2024 15:41:12 +0000 (UTC) X-FDA: 82814143776.11.1CCE3D2 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf13.hostedemail.com (Postfix) with ESMTP id CEC6120008 for ; Fri, 22 Nov 2024 15:40:10 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=UoJZofie; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3FaZAZwkKCKQEPMGIVcLPKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--aliceryhl.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3FaZAZwkKCKQEPMGIVcLPKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--aliceryhl.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732290002; a=rsa-sha256; cv=none; b=WgyviDE5HyKH32AknXl0EIpCtqM5TnXgDyT0TPFuXsPFVFSpmwk46cC1eguUxicxfWAYht Xe7BGFoUvvFyISKe56h7cXqAlx+1gVRtO3zMclZ1cjFjI3HDC7tNslNtN8kA4t9Dm0akEW OzwUcYFyb6nwWGpLqCRmXx0g4Xq8/AI= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=UoJZofie; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3FaZAZwkKCKQEPMGIVcLPKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--aliceryhl.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3FaZAZwkKCKQEPMGIVcLPKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--aliceryhl.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732290002; 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=hYxLUop5CFzWW+VlEDd2o5nmfczomOBA2awiJyr0ME8=; b=xfqbmeXtQyEWmOr7PnZm1mT2wzHX4rmOFkMbzjRZXSQThA+f0pZrLtrKF5q1hDW7vKc+4t TQL2snuiM7LpJYNBuh+fKW5irbmCCe7KGBO8kgrH0KevlK/eqwiqA/lSKubMWR+PsSCEF8 MA9K31lLM8CJ75VQgAwj8X/VpX8BmiY= Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4315eaa3189so19779285e9.1 for ; Fri, 22 Nov 2024 07:41:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732290069; x=1732894869; 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=hYxLUop5CFzWW+VlEDd2o5nmfczomOBA2awiJyr0ME8=; b=UoJZofieHf4Vh+2IRqLqiXdfCAcT6ln3zSBYjKTtGQNVG5yyKsLHX7p+YFKNM5h8ab XYsbx0betti0cPC/vu6+e8BWonSxVj80dxhVD871sB8MF0TO/Ncie0BzQ296qVQjr6mA 6LniRML9Odj1S4GSxyVrD21xcu1tPMstFih9VQ7UtBwYoXU8KSjccLIEqol8qSQZ+HJ6 D3IrCYnU/TKrEz32+BQt+33GqSVwlmiUrnEcLxjddd2uh1l+tT212H73++K4MoL7nymo hjfQ8DXOK1z2Wpe8bMgf2BGPd9LU3KQt+ENRkqme7Mi1Tj1y7BHIQSdrYeWEXbZU4kPU sc6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732290069; x=1732894869; 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=hYxLUop5CFzWW+VlEDd2o5nmfczomOBA2awiJyr0ME8=; b=HwEWiFPamtTIgIeiv9h35eucr63S8BUzpe/52b+EDx+BAoEUJH0QKL5XS1msgxNIxx xpoPu8meRyLOep37a2Y2Fw4JRDdimUU1u5VTEdO8ByvL5iJEPvUxIFlh6/cCFa4sQ3F0 1kv1aSMRzzk04Niy89GwW6Hbwv8FUTgnX2t3drEndS4yAD9La+CwfiLFyH9H+BNH4LBq QG7GS513XMdDoVEQ7Tseb9/wTo8pTBN7fEztGu8/shSGhKOCGme6DdXOezX5GHwMVgT6 XrqMMJ8QSYNzbVmjgSRdzdyqYbFZU/sdodbKthfHI376YD7U4QDKomZ93eYPQvijK2RQ kY3g== X-Forwarded-Encrypted: i=1; AJvYcCWb9jEH1hsriLCHI51z71drxcmZH4J4TnePJUxsoMe6Ed+dt4w62dUfrgPkvPSD19ZxnhXV8vN6oQ==@kvack.org X-Gm-Message-State: AOJu0YyLo7BfVwVBcFUBNRPb1cmztyvGwDnclUjvSLYsqVtIiEGjwDze Z1QySKCulDXywWCidFID/BzPQT6/57YQDNAI5xj+AM6jua6GuT+6DBxu0v0+SE1mr9HJYVKZcdW awPUqrBGD3XItFw== X-Google-Smtp-Source: AGHT+IEIKcYcdx6MVWct3b+zC6qUnAKNrcyvu2pxBZsUhCYdS8JAEE1PK1hh3wfuatjl0mJTsbCFOi6Ydty6B7I= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:68:fe9:ac10:f29e]) (user=aliceryhl job=sendgmr) by 2002:a05:600c:35c4:b0:42e:d463:3ea7 with SMTP id 5b1f17b1804b1-433ce4b2e61mr290325e9.3.1732290069096; Fri, 22 Nov 2024 07:41:09 -0800 (PST) Date: Fri, 22 Nov 2024 15:40:28 +0000 In-Reply-To: <20241122-vma-v9-0-7127bfcdd54e@google.com> Mime-Version: 1.0 References: <20241122-vma-v9-0-7127bfcdd54e@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=0kJbDQ2R2LaDZDdpCyVYpygYX3kC3nlpmnRFMMkub60=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnQKYIvyS3vYvaLl2hnXmTUoNnpgWbUaM0XAaBO 5Hd920hx1WJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ0CmCAAKCRAEWL7uWMY5 RpyREACin3dfUAa6PzIT6e2p6DXDZa92oR0hkUItIreeOaz+aDB7QYUz1TQHXS+0/ggVZfuQ5qM PW6oK14yS9QIiu6J0zqJlQ+h9CcPk6ycDjrkQKKQQNlnLaoA1KUSopOA0/dXkvOqDZx7eh0rczd yRoj+CT4F/YkIYDETENVy6KMVZIMklx4enehQes6M1v/AftWAvbN7ZluoxYjzTtcsgsw2dpLQyx 7qtm2TIoTqNlAKvdXoxetMePlgiR9OmK4DFj9zSoknu222UrRAZLZrEQ8Us3nMTCd6rFI9dsVrh SCqn+C7ZJgAOZP0UDU7gBbCXtL0TdYszA+ja48fRu7t9eb8GZZZmuDMM6i2jHKgKcw5HKVmPW2U H4YawniEx4YivnbG2E58oYT2t6bhlDrlBu4OZ2PFnSo0O0TvsiX8919xV8I3dfRkiGz7bpC4icL Eeyu4sxt/8cUq/Bdr52weSKg05oKqakBtcrCfbQ0SFx0QyO/X+ZPpuoUjw5l6SIRSglewAePJui rlIdtOTOVSpaMVVvRJiCG1X7KKQLnOa9o/znQ2gVJjMOYupgju8s87HWBq/AKVyzZPO+9LI9JxH b7afReIFODoqSqosl36aTz7325JcyO+7kKY7zNDD5KnoFhgQa/Hj/u1nSsxHHiPTc/MT1lNXiLA Zar/2JKK9H6pk4Q== X-Mailer: b4 0.13.0 Message-ID: <20241122-vma-v9-3-7127bfcdd54e@google.com> Subject: [PATCH v9 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 , 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: CEC6120008 X-Rspamd-Server: rspam01 X-Stat-Signature: 4y733xnmc6nu35owpj5c64sreee84a1i X-HE-Tag: 1732290010-577171 X-HE-Meta: U2FsdGVkX1++F7kvOsXGEfN1IPu8XGblZvzrj8INydLuq8JYWSObI4r3fj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C5+mKm2H90vgkFcV6J4UVz/pzOjGQxAC8PzrJggJQ9tc5VWU+xvwwdpUfUaWhjdeNgBieV/rWHRUo3QXyVAvSEr91lRUzpplRtqRhuDml9P2Zid7Y6u7qWmY8maAVKZurd+LDI8b4zkqe0emhrobcU1l+lUlQYdszv7/Z82a0LhE3FJGPwWlNZvgViAX5IIr6IJ+cJ1anL7XZVIJNhbGomaEaL0JNxe7VEk4jYnpN9qpbXfzenbSVTIo+v0teWACd7sDq4ev9qBRCNYsNJPV2Xz1Ibgm7S6e7m7kXVs4JIGPB0r4asbKBV/Nc7Sq12VtZZ5MuN2d+S1l3M1G2Cd8oTBryBx8ivXTsgXFBHIfGf6axW4sHST9y2R1VYjY2wTq8v+WirWbQak1tSIQhlXD7ejcyCW+79pjkX4HUxTDNKK9Zyy0PMzkGRcdG9LkzENZxr4Rt5ngm81KxRBiAdgxFSsMpa7acebADgFzNcsfAEL51J+fPlkXMSfPElMi9Wa2z6WGuqFn+8J9xPqv7k8t6ra9AOs+SzVdYU7gcQWv4kXt7F8alLP8nTb0so2murFPs= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000004, 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 6df145fea128..3e494e40b530 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. /// @@ -85,6 +92,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.