From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 25104379974; Tue, 24 Feb 2026 11:20:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771932034; cv=none; b=eIjYjEpJX8Guo5AmbS5O9/jnIkVJ5sdLq79wo/jNKFB9ccoyUKoTBtQle2SBXscenBr5qRghDLuBcnxIe4OAkZV1K8mOu/Uwt8+KnoOxQ8+0Ppa27dyiT1C/lGQ00n9n8+WmVnHNGS9qmzRb+/BRdHVyXSjC8J4F6ZjbhvCTrQ4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771932034; c=relaxed/simple; bh=8blkgRGad5yywyXctIGzlJVTqzYWum47B0ORZDHUOHs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=VfceK/lxzDGwrbLaEmVZeolPzJjydbq8XUz9qJjO+DpoPZL3dN7R6CtvlvdYtVsFfTMLI1LDbMJuLA1/o/GZoAbWWD4yZqAxhdw3UxIlOnKGXT3Z+W1HnWe8JuymGwcdC825Xtl4rErCDlBQCDxZCU07h4PzNM76Vnks3NBTlNM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=diiRtWQG; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="diiRtWQG" Received: by smtp.kernel.org (Postfix) with ESMTPSA id ECCD5C116D0; Tue, 24 Feb 2026 11:20:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771932034; bh=8blkgRGad5yywyXctIGzlJVTqzYWum47B0ORZDHUOHs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=diiRtWQGbNsdc0h1Enpi/pVNcSAaCWR19qvAIJoesAE0mu5EUaR8gi6E2meijCd3l 97m65gcugi/MEpeLuoE/El+ebtQwyFcqdyp15jnsHJkkBIDPu1Kgk4LkUufqBenC1c UR3PJJ+hys5NdZ2nbMk43WX7u9weWHc2W4hh20fReiF4KkuRcqpf232RVYMMUpfBOT sufS5qXIN0A3yfDCqynIeneINu4gSc2NH6Xbl3ER2z7OAPwIkka8a27jugFup+0bRf MOW4Xg3d4YH05H0TJJ4S2hVRhnqjSVnOV3XT3525keF7NACqpz7ed4h4T18APxCRpI sB5T08oMsNdpQ== From: Andreas Hindborg Date: Tue, 24 Feb 2026 12:18:03 +0100 Subject: [PATCH v16 08/10] rust: page: convert to `Ownable` Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260224-unique-ref-v16-8-c21afcb118d3@kernel.org> References: <20260224-unique-ref-v16-0-c21afcb118d3@kernel.org> In-Reply-To: <20260224-unique-ref-v16-0-c21afcb118d3@kernel.org> To: Miguel Ojeda , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Greg Kroah-Hartman , Dave Ertman , Ira Weiny , Leon Romanovsky , Paul Moore , Serge Hallyn , "Rafael J. Wysocki" , David Airlie , Simona Vetter , Alexander Viro , Christian Brauner , Jan Kara , Igor Korotin , Daniel Almeida , Lorenzo Stoakes , "Liam R. Howlett" , Viresh Kumar , Nishanth Menon , Stephen Boyd , Bjorn Helgaas , =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= , Boqun Feng , Vlastimil Babka , Uladzislau Rezki , Boqun Feng Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-block@vger.kernel.org, linux-security-module@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-pm@vger.kernel.org, linux-pci@vger.kernel.org, Andreas Hindborg , Asahi Lina , Asahi Lina X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=4409; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=7QArePJ/pjN7cnWoG0Oe4tA+Fg7KV4ZTWL57uMIfL+M=; b=owEBbQKS/ZANAwAKAeG4Gj55KGN3AcsmYgBpnYj6lI/S2fx31p6OdqYp54cwV/ZbM95Hj1Ip+ BqHsBArEFuJAjMEAAEKAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaZ2I+gAKCRDhuBo+eShj d+jAEADCyxHHAeQ9w9chDL5by1fVQ/N2FPpmL6SwLswTZkGUfXNDnAk/H82ilVO62oy5hZ8KaRu OsXJgrKn/02LDtIGs2dErMz/Xm62Qw5RikfQFgd9ILpgPHQVjUd0Ca+NiFC0tMiwf1M3TzX10oy 2yJ9a35fdQZVecuk8gGwNztfDXC23g11XjYUzukzr8/UWRzywluZ14HPZn9o6W5mhLO1LYimTiH VlMKHqHyUtOyQ6p4LP/hQInN6A7D47XFx6TIgiMk7ZVKHIQJQQY/ViRDDdrAeAKPp1gZo+xW/Xl 4bOBdBlhIm48CYKt1te/GksCvwE2aAcqV8I5r9hMUrrkDYNxK7UEYiKWGPsQ53aALJMQJSA5XDi GxVnZA+oOSsx9RzjngNovD1jQjVKTOGiL86/RdGb4XjSoqtZBX949dgVrJgW8bWMUvo9a3r5uy+ ldnSR7x9fOoPEo2o1Cge0zxUDTim5d/ze7bCnukyq16eD+03/qtca55aDF45XNeEjR6hpoqERA7 9JRoT/7cP19VcrIwkeZZwE6R8oqhoQUcNdxjMVkbxpWASF76Ml63W3g58rOQphacvZGhixvI2tO gwE7QuRGxSCGpSYFerpnwT0LxJnhUo+JtZUCQR7AlZC7pGrkFNCOJChGHYLZQfCP/45/0U7y8AC OtZo8io7bnV4TnQ== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 From: Asahi Lina This allows Page references to be returned as borrowed references, without necessarily owning the struct page. Signed-off-by: Asahi Lina [ Andreas: Fix formatting and add a safety comment. ] Signed-off-by: Andreas Hindborg --- rust/kernel/page.rs | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs index bf3bed7e2d3fe..e21f02ae47b72 100644 --- a/rust/kernel/page.rs +++ b/rust/kernel/page.rs @@ -10,6 +10,11 @@ bindings, error::code::*, error::Result, + types::{ + Opaque, + Ownable, + Owned, // + }, uaccess::UserSliceReader, // }; use core::{ @@ -83,7 +88,7 @@ pub const fn page_align(addr: usize) -> usize { /// /// [`VBox`]: kernel::alloc::VBox /// [`Vmalloc`]: kernel::alloc::allocator::Vmalloc -pub struct BorrowedPage<'a>(ManuallyDrop, PhantomData<&'a Page>); +pub struct BorrowedPage<'a>(ManuallyDrop>, PhantomData<&'a Page>); impl<'a> BorrowedPage<'a> { /// Constructs a [`BorrowedPage`] from a raw pointer to a `struct page`. @@ -93,7 +98,9 @@ impl<'a> BorrowedPage<'a> { /// - `ptr` must point to a valid `bindings::page`. /// - `ptr` must remain valid for the entire lifetime `'a`. pub unsafe fn from_raw(ptr: NonNull) -> Self { - let page = Page { page: ptr }; + let page: NonNull = + // SAFETY: By function safety requirements `ptr` is non null. + unsafe { NonNull::new_unchecked(ptr.as_ptr().cast()) }; // INVARIANT: The safety requirements guarantee that `ptr` is valid for the entire lifetime // `'a`. @@ -105,7 +112,8 @@ impl<'a> Deref for BorrowedPage<'a> { type Target = Page; fn deref(&self) -> &Self::Target { - &self.0 + // SAFETY: By type invariant `self.0` is convertible to a reference for `'a`. + unsafe { self.0.as_ref() } } } @@ -126,8 +134,9 @@ pub trait AsPageIter { /// # Invariants /// /// The pointer is valid, and has ownership over the page. +#[repr(transparent)] pub struct Page { - page: NonNull, + page: Opaque, } // SAFETY: Pages have no logic that relies on them staying on a given thread, so moving them across @@ -161,19 +170,20 @@ impl Page { /// # Ok::<(), kernel::alloc::AllocError>(()) /// ``` #[inline] - pub fn alloc_page(flags: Flags) -> Result { + pub fn alloc_page(flags: Flags) -> Result, AllocError> { // SAFETY: Depending on the value of `gfp_flags`, this call may sleep. Other than that, it // is always safe to call this method. let page = unsafe { bindings::alloc_pages(flags.as_raw(), 0) }; let page = NonNull::new(page).ok_or(AllocError)?; - // INVARIANT: We just successfully allocated a page, so we now have ownership of the newly - // allocated page. We transfer that ownership to the new `Page` object. - Ok(Self { page }) + // SAFETY: We just successfully allocated a page, so we now have ownership of the newly + // allocated page. We transfer that ownership to the new `Owned` object. + // Since `Page` is transparent, we can cast the pointer directly. + Ok(unsafe { Owned::from_raw(page.cast()) }) } /// Returns a raw pointer to the page. pub fn as_ptr(&self) -> *mut bindings::page { - self.page.as_ptr() + Opaque::cast_into(&self.page) } /// Get the node id containing this page. @@ -348,10 +358,12 @@ pub unsafe fn copy_from_user_slice_raw( } } -impl Drop for Page { +impl Ownable for Page { #[inline] - fn drop(&mut self) { - // SAFETY: By the type invariants, we have ownership of the page and can free it. - unsafe { bindings::__free_pages(self.page.as_ptr(), 0) }; + unsafe fn release(&mut self) { + let ptr: *mut Self = self; + // SAFETY: By the function safety requirements, we have ownership of the page and can free + // it. Since Page is transparent, we can cast the raw pointer directly. + unsafe { bindings::__free_pages(ptr.cast(), 0) }; } } -- 2.51.2