From nobody Mon Dec 15 21:47:18 2025 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A7031246A23 for ; Wed, 15 Jan 2025 13:36:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736948175; cv=none; b=rgqJpYJwY0SO79IymZz6au2G5E0UrLIWYQWV+pZiCCgk648rszq4kaX+WeXoVv+QajtOg+v3DBFI75jJb5pgyKdfmphQ/1VUkfjEbaR8QAB9K7Ji50yyfjADmmopgUSBDlEpP+tViSlPyAGKWAN8jNmz85CAUJ2qH+SMXj8M/mc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736948175; c=relaxed/simple; bh=DkijNTCWjzL+GZ9A/ngSiwPnkdD9yqoZbhkXBZ1FzqQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ES6Hqoh2vEoS+cI+D8ywHbz7jKBXoBByFv4kvPc5EOkErnVlUG88NJdmrkT1oWJ0xk6UpO/jR9QKl+eYq6+7l8CIdcNeIPKFtBb9V8wm8ioJNAR0LChaWDDN5hriN0Q0rN4OA+qDROMoxq3CVgL96/r9RehkYfvYzF1g883vNLI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=opLg9mkD; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="opLg9mkD" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4362f893bfaso36067175e9.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=vger.kernel.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=opLg9mkDHwaQoqtDK2mwhQgsLfl0LQvgnE89WgEE6JcyIk5GjjdRohX+2euhBI2ExR YGHcNmvA6Or6imGH1Pz4/DPSR6P4oencSU7opvKwn7IUxLadCuBnFT/qkPbkTSsTy0m7 eurCE+oaHCYoxhdqi2ASpPmuubMYgIRrwc+a8udzjkyyw8a4NsaqZIjt4x7ESkj6GgIX 99KO0Qlr987Y9K3hgEAWRX+auH99IR3Myl7bd0e+omEKDu7zNWVFVAPDM1gMyWLciQTb HE61Rx/x7ZtLKisybQyyWJ3sm0v2U/Wh9cW3gkrZND1HfayoCgPhDGoESdZH1hn7Y1Bq x/Iw== 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=gwlVVX69rbBH1fb8DE1C70WBBsyiqZwsHMmwLz8tkwMqLd18B+HIwkZ14CuPewQ6OJ q0qvL3fu3taxyVPRRBeQmrPyjqjJdiKWITQSeKU+ZAmSrnqUGQVFjrw/rn61Oa9RoWBL kSRXfD3+shl+XsD0ZwRKHq5bZhJTjI5butjlQNU3CbLVnBulYAAC1N7AzzY3l7XMIT8R qCgOm/RD17Jpi4tMnqF9SgwV/abbddcTH15Ke8vMvCs6w39TS7/3PmRm9dPBq3bstycU Zsp59W4bjI1Axe+lCh0+KFnWGYTE85co+/ciUomliNMdhqSc0Zn5RWWOL/7ZT5fisFip oQYw== X-Forwarded-Encrypted: i=1; AJvYcCWivqvx8NMA1Ux4Fa2QSAsLBN1MtX7t/fQzIjHf9BXswrDoEEpILs+ubv8adbPpCEkE61LeyrBATv1xff0=@vger.kernel.org X-Gm-Message-State: AOJu0Yx+FYwqqS7mO8FDcUMcYDtouqVTfpjSxl+GxoFkzBj8jj5gqz93 1m0TNSWni5elQsSaaGfvWe9VfGs44Ngj3HGjGvZGOGXAD3258rKDxOiDi3Pc8rvBjBbi/HNKDP0 XtHodMhE2gRBdbA== 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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 Reviewed-by: Andreas Hindborg --- 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. =20 -use crate::{bindings, mm::MmWithUser, types::Opaque}; +use crate::{ + bindings, + error::{to_result, Result}, + mm::MmWithUser, + page::Page, + types::Opaque, +}; + +use core::ops::Deref; =20 /// 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, si= ze: usize) { ) }; } + + /// If the [`VM_MIXEDMAP`] flag is set, returns a [`VmAreaMixedMap`] t= o 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 !=3D 0 { + // SAFETY: We just checked that `VM_MIXEDMAP` is set. All othe= r 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 an= d [`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 =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, an= d that the mmap read lock + /// (or stronger) is held for at least the duration of 'a. The `VM_MIX= EDMAP` 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 fo= r 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()) }) + } } =20 /// The integer type used for vma flags. --=20 2.48.0.rc2.279.g1de40edade-goog