From nobody Sun Feb 8 12:51:55 2026 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) (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 889AA1D5ACE; Sun, 2 Feb 2025 13:12:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.63.210.85 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738501945; cv=none; b=HqPgNvACb9rTTat3cv+WtGoTUDs/JYK3KfWvUjRCeOZoLJop4zyyetvNdc5oy0fuYjnLAqf8IsXMOrvxqQpgxAH48zv9z8pibL75kv857XgOt25QGTFdwuRG4wvHLjF9moOt4yJUqj/OK+0MnLw0Et+PG3qaNrCnsw6N398D2Kw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738501945; c=relaxed/simple; bh=db+CTl7vXN85Dcw+hwBoMAovv2p9XLFk8QMb0+h2eUk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=VrvSbsTBxpslD+0ADygqPe1CZSnn4d5DKJ1p4iTYY35hUfwVP/9rkcWVom0eSbW+hggB+tISPSLgcztq4Zs1Rroy8uXhfb8dvmKw8qH5DcZ1SXmmksHSakd2QnnzGxyg8/++OVXeW5xqGbKmllsFo5OWvSzMk96whQDUID56HMg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=asahilina.net; spf=pass smtp.mailfrom=asahilina.net; dkim=pass (2048-bit key) header.d=asahilina.net header.i=@asahilina.net header.b=a6FbgBjV; arc=none smtp.client-ip=212.63.210.85 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=asahilina.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=asahilina.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=asahilina.net header.i=@asahilina.net header.b="a6FbgBjV" Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: sendonly@marcansoft.com) by mail.marcansoft.com (Postfix) with ESMTPSA id C261F42601; Sun, 2 Feb 2025 13:06:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1738501598; bh=db+CTl7vXN85Dcw+hwBoMAovv2p9XLFk8QMb0+h2eUk=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=a6FbgBjVng3G981fDt43dF40MXlTw0rneaJMabBeyx+aIR56WSn59r40VougNzdn9 sAN00S22Hl9EimtHryyQ2WLebuOp1SFD1vHROBQQm6o5hLtff/bVxEfldzY26ZVKCs 90Iz6seFHpmJcG0RC9KmKpu9BAkJYuL4ZW+Ue+5w8gLxQ0berFYK9GcuremJ+f6LvT q8Kf9DMk8LmruH8sMGyzTRbNisX6FmMrS7gXSJFmY9l8H3XOCDyvP7fQDgR1XFX6jr Z+/7MUCQliMV8rJrfuUhi+igiiZ2uSyYJIZs8Cf6A0ZQqHkSnIj13WdS1w9EADuKLK I2PobK+BvOepA== From: Asahi Lina Date: Sun, 02 Feb 2025 22:05:43 +0900 Subject: [PATCH 1/6] rust: types: Add Ownable/Owned types Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250202-rust-page-v1-1-e3170d7fe55e@asahilina.net> References: <20250202-rust-page-v1-0-e3170d7fe55e@asahilina.net> In-Reply-To: <20250202-rust-page-v1-0-e3170d7fe55e@asahilina.net> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Cc: Jann Horn , Matthew Wilcox , Paolo Bonzini , Danilo Krummrich , Wedson Almeida Filho , Valentin Obst , Andrew Morton , linux-mm@kvack.org, airlied@redhat.com, Abdiel Janulgue , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev, Asahi Lina X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1738501594; l=5188; i=lina@asahilina.net; s=20240902; h=from:subject:message-id; bh=db+CTl7vXN85Dcw+hwBoMAovv2p9XLFk8QMb0+h2eUk=; b=v4PEmnpGzrguSNB9PT6BnZr6+ea6iCaG0tCqNXz3t0LXfHn0sXubw18gYdI27epEwjEXxH8Ou PmgIcPGn+PiB7ALp4QLTfYWJgBI28JDbm3gcQoszN3vZbwEzEW2Tw8a X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=tpv7cWfUnHNw5jwf6h4t0gGgglt3/xcwlfs0+A/uUu8= By analogy to AlwaysRefCounted and ARef, an Ownable type is a (typically C FFI) type that *may* be owned by Rust, but need not be. Unlike AlwaysRefCounted, this mechanism expects the reference to be unique within Rust, and does not allow cloning. Conceptually, this is similar to a KBox, except that it delegates resource management to the T instead of using a generic allocator. Signed-off-by: Asahi Lina --- rust/kernel/types.rs | 110 +++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 110 insertions(+) diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index ec6457bb3084ae327c38ba4ba79b1601aef38244..0bee56153dcea47fb1321162df6= b8765b5436e9f 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -479,6 +479,116 @@ fn drop(&mut self) { } } =20 +/// Types that may be owned by Rust code or borrowed, but have a lifetime = managed by C code. +/// +/// It allows such types to define their own custom destructor function to= be called when +/// a Rust-owned reference is dropped. +/// +/// This is usually implemented by wrappers to existing structures on the = C side of the code. +/// +/// # Safety +/// +/// Implementers must ensure that any objects borrowed directly as `&T` st= ay alive for the duration +/// of the lifetime, and that any objects owned by Rust as `Owned`) sta= y alive while that owned +/// reference exists, until the [`Ownable::release()`] trait method is cal= led. +pub unsafe trait Ownable { + /// Releases the object (frees it or returns it to foreign ownership). + /// + /// # Safety + /// + /// Callers must ensure that the object is no longer referenced after = this call. + unsafe fn release(this: NonNull); +} + +/// A subtrait of Ownable that asserts that an `Owned` Rust reference i= s not only unique +/// within Rust and keeps the `T` alive, but also guarantees that the C co= de follows the +/// usual mutable reference requirements. That is, the kernel will never m= utate the +/// `T` (excluding internal mutability that follows the usual rules) while= Rust owns it. +/// +/// When this type is implemented for an [`Ownable`] type, it allows `Owne= d` to be +/// dereferenced into a &mut T. +/// +/// # Safety +/// +/// Implementers must ensure that the kernel never mutates the underlying = type while +/// Rust owns it. +pub unsafe trait OwnableMut: Ownable {} + +/// An owned reference to an ownable kernel object. +/// +/// The object is automatically freed or released when an instance of [`Ow= ned`] is +/// dropped. +/// +/// # Invariants +/// +/// The pointer stored in `ptr` is non-null and valid for the lifetime of = the [`Owned`] instance. +pub struct Owned { + ptr: NonNull, + _p: PhantomData, +} + +// SAFETY: It is safe to send `Owned` to another thread when the underl= ying `T` is `Send` because +// it effectively means sending a unique `&mut T` pointer (which is safe b= ecause `T` is `Send`). +unsafe impl Send for Owned {} + +// SAFETY: It is safe to send `&Owned` to another thread when the under= lying `T` is `Sync` +// because it effectively means sharing `&T` (which is safe because `T` is= `Sync`). +unsafe impl Sync for Owned {} + +impl Owned { + /// Creates a new instance of [`Owned`]. + /// + /// It takes over ownership of the underlying object. + /// + /// # Safety + /// + /// Callers must ensure that the underlying object is acquired and can= be considered owned by + /// Rust. + pub(crate) unsafe fn from_raw(ptr: NonNull) -> Self { + // INVARIANT: The safety requirements guarantee that the new insta= nce now owns the + // reference. + Self { + ptr, + _p: PhantomData, + } + } + + /// Consumes the `Owned`, returning a raw pointer. + /// + /// This function does not actually relinquish ownership of the object. + /// After calling this function, the caller is responsible for ownersh= ip previously managed + /// by the `Owned`. + #[allow(dead_code)] + pub(crate) fn into_raw(me: Self) -> NonNull { + ManuallyDrop::new(me).ptr + } +} + +impl Deref for Owned { + type Target =3D T; + + fn deref(&self) -> &Self::Target { + // SAFETY: The type invariants guarantee that the object is valid. + unsafe { self.ptr.as_ref() } + } +} + +impl DerefMut for Owned { + fn deref_mut(&mut self) -> &mut Self::Target { + // SAFETY: The type invariants guarantee that the object is valid, + // and that we can safely return a mutable reference to it. + unsafe { self.ptr.as_mut() } + } +} + +impl Drop for Owned { + fn drop(&mut self) { + // SAFETY: The type invariants guarantee that the `Owned` owns the= object we're about to + // release. + unsafe { T::release(self.ptr) }; + } +} + /// A sum type that always holds either a value of type `L` or `R`. /// /// # Examples --=20 2.47.1 From nobody Sun Feb 8 12:51:55 2026 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) (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 8894C1CAA80; Sun, 2 Feb 2025 13:12:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.63.210.85 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738501944; cv=none; b=Ng2Fi20KlsCD8P9XKXiK4y1KT33rMzkvKOB48UE0URTHkt+geJ6oLAMSxD6Ih7UrQtUmULBf03hQ0v0qeK8SsSz7ESt1zQKZCursS7ZV616UWlvlHnqaGo3j1e5McpoEtN/0b8cJAKSW8kzlGyRV+ItAsvIOelWsuK6cPwrdepY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738501944; c=relaxed/simple; bh=i4Ab3HjgvvGhPot4YFk4jEpbbo5OwUVep0fKUcItVqY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=NZvg9qicarpxAFYyH8Yw/Dap385Acw/fn/Kxm4mlAlblUAbRLFdR3Q6dwpMuE4msnsRNS7KjNY/Eh0HMaIYZYn7rca0rnUSBTasw8yTbzwBNzBRlJCkAUhjaGX7u1xnMi3FdSe6h9wu5cZ01CSUtv8kpVr62tEaJuEix2eH3CbM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=asahilina.net; spf=pass smtp.mailfrom=asahilina.net; dkim=pass (2048-bit key) header.d=asahilina.net header.i=@asahilina.net header.b=j13+77B4; arc=none smtp.client-ip=212.63.210.85 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=asahilina.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=asahilina.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=asahilina.net header.i=@asahilina.net header.b="j13+77B4" Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: sendonly@marcansoft.com) by mail.marcansoft.com (Postfix) with ESMTPSA id 0AE3C4269B; Sun, 2 Feb 2025 13:06:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1738501601; bh=i4Ab3HjgvvGhPot4YFk4jEpbbo5OwUVep0fKUcItVqY=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=j13+77B4uDEiUiAv4VmriWW3KBa28+PJ+rGSbk9sG6oe+/Vc9YDMzmV7eNxU4Ovbe fcZvQetCX39EHEG00wlsexJZddtS+9Q0eYfuh+ZpU/Itzhea8VcX+R07bC5utcvT7v UL096wFWUozQwwAur7vKnZvzJ69/LuSXUmiP514WLvkNQb1OXmuodgNTxNI3s/WEjc QBbr6bR2qpm7Kpe92vLmd24v4QKLjhjKs8O/Xjobmulm5S/oEk9r7ipJKAoSLcAmq7 0ek/0u6XcL/YLJ1hpUkz3zcAgRBVmKPlWUB9p9RA2cO9tz98sdjH0jBx+6Yvkz2U6m 9FiA+JGRbSyow== From: Asahi Lina Date: Sun, 02 Feb 2025 22:05:44 +0900 Subject: [PATCH 2/6] rust: page: Convert to Ownable Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250202-rust-page-v1-2-e3170d7fe55e@asahilina.net> References: <20250202-rust-page-v1-0-e3170d7fe55e@asahilina.net> In-Reply-To: <20250202-rust-page-v1-0-e3170d7fe55e@asahilina.net> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Cc: Jann Horn , Matthew Wilcox , Paolo Bonzini , Danilo Krummrich , Wedson Almeida Filho , Valentin Obst , Andrew Morton , linux-mm@kvack.org, airlied@redhat.com, Abdiel Janulgue , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev, Asahi Lina X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1738501594; l=3247; i=lina@asahilina.net; s=20240902; h=from:subject:message-id; bh=i4Ab3HjgvvGhPot4YFk4jEpbbo5OwUVep0fKUcItVqY=; b=gUHcqq+q5mqcWHe6iq9f1yxIulhbcJU+/xJBVOGRrtI5rZfJKz3keo8IzgHn3owhyqq53MKvh /PP7nIbYuJVA+cau3fDKRYqaejphd+pg0QPqP6nq+mCd5L8wHvID/MN X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=tpv7cWfUnHNw5jwf6h4t0gGgglt3/xcwlfs0+A/uUu8= This allows Page references to be returned as borrowed references, without necessarily owning the struct page. Signed-off-by: Asahi Lina Reviewed-by: Alice Ryhl Reviewed-by: Fiona Behrens --- rust/kernel/page.rs | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs index fdac6c375fe46e1ba589f1640afeae3e001e39ae..0b6cbe02522ab6e6e1810288ad2= 3af4e4aa587d8 100644 --- a/rust/kernel/page.rs +++ b/rust/kernel/page.rs @@ -7,6 +7,7 @@ bindings, error::code::*, error::Result, + types::{Opaque, Ownable, Owned}, uaccess::UserSliceReader, }; use core::ptr::{self, NonNull}; @@ -30,13 +31,10 @@ pub const fn page_align(addr: usize) -> usize { (addr + (PAGE_SIZE - 1)) & PAGE_MASK } =20 -/// A pointer to a page that owns the page allocation. -/// -/// # Invariants -/// -/// The pointer is valid, and has ownership over the page. +/// An object representing a memory page in the kernel (`struct page`). +#[repr(transparent)] pub struct Page { - page: NonNull, + page: Opaque, } =20 // SAFETY: Pages have no logic that relies on them staying on a given thre= ad, so moving them across @@ -71,19 +69,20 @@ impl Page { /// let page =3D Page::alloc_page(GFP_KERNEL | __GFP_ZERO)?; /// # Ok(()) } /// ``` - 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 sl= eep. Other than that, it // is always safe to call this method. let page =3D unsafe { bindings::alloc_pages(flags.as_raw(), 0) }; let page =3D NonNull::new(page).ok_or(AllocError)?; - // INVARIANT: We just successfully allocated a page, so we now hav= e ownership of the newly - // allocated page. We transfer that ownership to the new `Page` ob= ject. - Ok(Self { page }) + // SAFETY: We just successfully allocated a page, so we now have o= wnership 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()) }) } =20 /// Returns a raw pointer to the page. pub fn as_ptr(&self) -> *mut bindings::page { - self.page.as_ptr() + Opaque::raw_get(&self.page) } =20 /// Runs a piece of code with this page mapped to an address. @@ -252,9 +251,12 @@ pub unsafe fn copy_from_user_slice_raw( } } =20 -impl Drop for Page { - fn drop(&mut self) { +// SAFETY: `Owned` objects returned by Page::alloc_page() follow the= requirements of +// the Ownable abstraction. +unsafe impl Ownable for Page { + unsafe fn release(this: NonNull) { // SAFETY: By the type invariants, we have ownership of the page a= nd can free it. - unsafe { bindings::__free_pages(self.page.as_ptr(), 0) }; + // Since Page is transparent, we can cast the raw pointer directly. + unsafe { bindings::__free_pages(this.cast().as_ptr(), 0) }; } } --=20 2.47.1 From nobody Sun Feb 8 12:51:55 2026 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) (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 88A021D63E9; Sun, 2 Feb 2025 13:12:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.63.210.85 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738501946; cv=none; b=H5nv/SdiqKv3SHrzwqshZsjc/lYeXrTI+jEHW54SFEwPGsMTdEyA1ZaAzC5zsDByDgNF1e+CK4Qi3O3huzeKA9hAc+Xm6W9WDMq9U2ssi2VmLsB6T3673VwyUa70mCwUQLnH/C1D24wh6NTNP+vkvZUmzSMABPOVOqcOqFyB46k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738501946; c=relaxed/simple; bh=wOJkjoDFrNnJZtmpG6nyyGH0hn+2uCbf5uZhqGJfbOE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=NXjzO1z/1U8yE8qVrHeg2kh0jl7fLnHqCYlzxMd33hYU9PH3HD3KZT2YfElopCjC4jjRYVaSoIrqzW79vF6PybO3JD3Nbp27KEYGTcPSE14ukg0fH8KLuOne2ycnz3Ep4bFPZwN2hS0tzTK6BfLR/izcNeSEHg8HPOfV1JabF/U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=asahilina.net; spf=pass smtp.mailfrom=asahilina.net; dkim=pass (2048-bit key) header.d=asahilina.net header.i=@asahilina.net header.b=Fvp6scyW; arc=none smtp.client-ip=212.63.210.85 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=asahilina.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=asahilina.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=asahilina.net header.i=@asahilina.net header.b="Fvp6scyW" Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: sendonly@marcansoft.com) by mail.marcansoft.com (Postfix) with ESMTPSA id 4777C426EE; Sun, 2 Feb 2025 13:06:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1738501603; bh=wOJkjoDFrNnJZtmpG6nyyGH0hn+2uCbf5uZhqGJfbOE=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=Fvp6scyWPHGaA2V+QtyuriDLEbcXsPS3f5VQzFRpnxbamCxSPxBedHEen0IFXWxI3 YpG4MxVaHryxse2Zqd/1ZE5kfg99dK0flO2MBOZwVwfLEIa8aIY9t/BtajndBSbJKP vXpovHQtURT/97O6uaWV91LU/u2gQkiSCOdP+mlZ16N76lYLszA9uFuuB3NSJMcoR3 eBc0Pwz76K/7PDiK61AjtwRc6LVbD6Zfxe7H2fG/uUEMNx401uBjatAwdXSc9FhxlB u35iD2Iy3pgteYWwwyJC5K0Am0M6ZTqwH9j72pgRPDMuWGuEyUswkTqgTPOnu+iml2 PmrctezEUpDEg== From: Asahi Lina Date: Sun, 02 Feb 2025 22:05:45 +0900 Subject: [PATCH 3/6] rust: page: Make with_page_mapped() and with_pointer_into_page() public Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250202-rust-page-v1-3-e3170d7fe55e@asahilina.net> References: <20250202-rust-page-v1-0-e3170d7fe55e@asahilina.net> In-Reply-To: <20250202-rust-page-v1-0-e3170d7fe55e@asahilina.net> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Cc: Jann Horn , Matthew Wilcox , Paolo Bonzini , Danilo Krummrich , Wedson Almeida Filho , Valentin Obst , Andrew Morton , linux-mm@kvack.org, airlied@redhat.com, Abdiel Janulgue , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev, Asahi Lina X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1738501594; l=1782; i=lina@asahilina.net; s=20240902; h=from:subject:message-id; bh=wOJkjoDFrNnJZtmpG6nyyGH0hn+2uCbf5uZhqGJfbOE=; b=VjxsvTln6z9TApE7nyNtlOcBYymzTzlwnEi76qRMUo4C7bukspMosskksBKgJRb59MWQFy45P mX7u6UyqsyPA8vfivipTf0JTGImVnVxXhDriizPvUyNcNyrkbI4lmXC X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=tpv7cWfUnHNw5jwf6h4t0gGgglt3/xcwlfs0+A/uUu8= Lets users do (unsafe) complex page read/write operations without having to repeatedly call into read_raw()/write_raw() (which may be expensive in some cases). The functions themselves are not unsafe, but they do take a closure that receives a raw pointer, so actually making the access requires unsafe code. Signed-off-by: Asahi Lina Reviewed-by: Alice Ryhl Reviewed-by: Andreas Hindborg Reviewed-by: Fiona Behrens --- rust/kernel/page.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs index 0b6cbe02522ab6e6e1810288ad23af4e4aa587d8..fe5f879f9d1a86083fd55c682fa= d9d52466f79a2 100644 --- a/rust/kernel/page.rs +++ b/rust/kernel/page.rs @@ -101,7 +101,7 @@ pub fn as_ptr(&self) -> *mut bindings::page { /// different addresses. However, even if the addresses are different,= the underlying memory is /// still the same for these purposes (e.g., it's still a data race if= they both write to the /// same underlying byte at the same time). - fn with_page_mapped(&self, f: impl FnOnce(*mut u8) -> T) -> T { + pub fn with_page_mapped(&self, f: impl FnOnce(*mut u8) -> T) -> T { // SAFETY: `page` is valid due to the type invariants on `Page`. let mapped_addr =3D unsafe { bindings::kmap_local_page(self.as_ptr= ()) }; =20 @@ -142,7 +142,7 @@ fn with_page_mapped(&self, f: impl FnOnce(*mut u8) -= > T) -> T { /// different addresses. However, even if the addresses are different,= the underlying memory is /// still the same for these purposes (e.g., it's still a data race if= they both write to the /// same underlying byte at the same time). - fn with_pointer_into_page( + pub fn with_pointer_into_page( &self, off: usize, len: usize, --=20 2.47.1 From nobody Sun Feb 8 12:51:55 2026 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) (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 778DD1D5ACE; Sun, 2 Feb 2025 13:06:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.63.210.85 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738501610; cv=none; b=M9RIOk7a0RU5bkn0I2qFXvzLNoFHZQqT/jxAmpDF80A4kCMYqlbT69mfkcPpq1ngi73Nw52NR81RblZYhOsqse3tmI3c/ChUGXLle6xhVKPmGDR/KkrQVXswvXW88bxDcoVxJWpMzgnhosqFlw+jrTTfzwuY2LKVrcGa4ob6wXs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738501610; c=relaxed/simple; bh=0hDjuQHDG3NyTLjzY3YRf98j+prtIAJUg+wcnreGVnU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=XrRlus5aMbgbTM4ISZemvY6vfuCtAuXK8Jz3B0j8+5uPkgnuIUs07VoTFmTCy2t6WzrCwGlTJI0/NaiJ8UgzgJT7C1JMh799iyD1ioZSIdbX9V0Lb7hu7FcV80zKz/t/j4lc41XK2MyiDDZadpSBaBkvuA+eI6hKKly+BJyuB58= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=asahilina.net; spf=pass smtp.mailfrom=asahilina.net; dkim=pass (2048-bit key) header.d=asahilina.net header.i=@asahilina.net header.b=T2h4y9OU; arc=none smtp.client-ip=212.63.210.85 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=asahilina.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=asahilina.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=asahilina.net header.i=@asahilina.net header.b="T2h4y9OU" Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: sendonly@marcansoft.com) by mail.marcansoft.com (Postfix) with ESMTPSA id 8453742769; Sun, 2 Feb 2025 13:06:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1738501605; bh=0hDjuQHDG3NyTLjzY3YRf98j+prtIAJUg+wcnreGVnU=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=T2h4y9OUV23hl0E27AGlIKbSuOI9FSr/Qfou1x4vaF+kpMEWyoB7lE2oXSt2ZC4W/ Vcaf5Jwqm3JTOsI85ryMwJoWD1yS1mJqti15Dezaeahhg22IbqOwd+iujr4UKk7aug H0lVZemGdZg71svn3NaFnIvjah5m2azg1qcWNwNdaXJ218VVIrliNcN6rfxeqOp7Tb iZ+YGqU0Kkrc1su5UsafpzWEtc9JWSeHMoheGQTvGzVkSc/LQ9cP/wuQkcoS6bp/2g XCjB9AVWWdiP50DvfftpFU+nBZXXL22EjJ36ptc8mlCu81Dg3K0r/izBMeiNI5rVMi 8ehO67zamuuvQ== From: Asahi Lina Date: Sun, 02 Feb 2025 22:05:46 +0900 Subject: [PATCH 4/6] rust: addr: Add a module to declare core address types Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250202-rust-page-v1-4-e3170d7fe55e@asahilina.net> References: <20250202-rust-page-v1-0-e3170d7fe55e@asahilina.net> In-Reply-To: <20250202-rust-page-v1-0-e3170d7fe55e@asahilina.net> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Cc: Jann Horn , Matthew Wilcox , Paolo Bonzini , Danilo Krummrich , Wedson Almeida Filho , Valentin Obst , Andrew Morton , linux-mm@kvack.org, airlied@redhat.com, Abdiel Janulgue , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev, Asahi Lina X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1738501594; l=1443; i=lina@asahilina.net; s=20240902; h=from:subject:message-id; bh=0hDjuQHDG3NyTLjzY3YRf98j+prtIAJUg+wcnreGVnU=; b=StouHNNdGl6LXXun36/Ktt4NhZHoitHEaj40BWBfZvKzmLuZ2Y4LDldrYuJmpt3jJeuKqqIwg B+kPBJA/qaKDiEavoI6E1b0JF0yulanuwYHgzUwBeL6uQl0npRPVkny X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=tpv7cWfUnHNw5jwf6h4t0gGgglt3/xcwlfs0+A/uUu8= Encapsulates the core physical/DMA address types, so they can be used by Rust abstractions. Signed-off-by: Asahi Lina Reviewed-by: Alice Ryhl --- rust/kernel/addr.rs | 15 +++++++++++++++ rust/kernel/lib.rs | 1 + 2 files changed, 16 insertions(+) diff --git a/rust/kernel/addr.rs b/rust/kernel/addr.rs new file mode 100644 index 0000000000000000000000000000000000000000..06aff10a0332355597060c5518d= 7fd6e114cf630 --- /dev/null +++ b/rust/kernel/addr.rs @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Kernel core address types. + +use bindings; +use core::ffi; + +/// A physical memory address (which may be wider than the CPU pointer siz= e) +pub type PhysicalAddr =3D bindings::phys_addr_t; +/// A DMA memory address (which may be narrower than `PhysicalAddr` on som= e systems) +pub type DmaAddr =3D bindings::dma_addr_t; +/// A physical resource size, typically the same width as `PhysicalAddr` +pub type ResourceSize =3D bindings::resource_size_t; +/// A raw page frame number, not to be confused with the C `pfn_t` which a= lso encodes flags. +pub type Pfn =3D ffi::c_ulong; diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index e1065a7551a39e68d6379031d80d4be336e652a3..eb1a80ba8ff83ab2d1b3b1d11fe= d4fb704c7a4f5 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -29,6 +29,7 @@ =20 pub use ffi; =20 +pub mod addr; pub mod alloc; #[cfg(CONFIG_BLOCK)] pub mod block; --=20 2.47.1 From nobody Sun Feb 8 12:51:55 2026 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) (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 891AD38384; Sun, 2 Feb 2025 13:06:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.63.210.85 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738501611; cv=none; b=qVDkk51aWwiTXH5SV7kxYztX2dLLpDTX0G2USXpf698H8sikm/9cCPY2v6S6bSEAGVBA5iW1njpjiO2gXmYq13yc+qk56m8M7SBWn/fnn1ujK5T4A5++a+As9mePGmWO2esrRjK3NOyNds0iSzfl4zmTbvaTl3S7kmxWGjQUw9M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738501611; c=relaxed/simple; bh=/l/yqYUI25JHcLzcwMcBrWhtbhlMTOGAR488Z8bons4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=TCT2cQT7ShR46q+/ds0frBhgbbKEqpq3L7cOChfJh8dZ428Vqezz+mn+5E0/ZoDseHijb3RyAcuDGyGMi4HQ/3Mazs+ojnFV2NCblc+eAEHrSjvUVOWLquFP4bvMfLoslF4lP5priIhILqMVv1Up+DGqJksDtDbOuPA6+7YLm1I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=asahilina.net; spf=pass smtp.mailfrom=asahilina.net; dkim=pass (2048-bit key) header.d=asahilina.net header.i=@asahilina.net header.b=Q1d6zRTZ; arc=none smtp.client-ip=212.63.210.85 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=asahilina.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=asahilina.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=asahilina.net header.i=@asahilina.net header.b="Q1d6zRTZ" Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: sendonly@marcansoft.com) by mail.marcansoft.com (Postfix) with ESMTPSA id C0B3242CC4; Sun, 2 Feb 2025 13:06:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1738501607; bh=/l/yqYUI25JHcLzcwMcBrWhtbhlMTOGAR488Z8bons4=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=Q1d6zRTZJ4O6vYmZeKYI/dMmURcPQcUkgzn7B0XvDog6QeDRPMPNjRMBp5Oh+dDWs fBKDBU6JMWUawsCrpr+SCfTXQnkcnfb5I3cwQbqLHu2mf+BRrHDcBcJiTDnDY9siL6 m6p+zuQZoIez31D62jvfEf0R9fnoa3LDPuhdrc18zVYA6dsnHDASD8vVK2finjxFXD 9+GkECLq4Vigayi1UGdSEgoy4ynEWax1Q/RkcUFIlmWVZMgVh23du/1cBc8QyRdqhD TMuB9MR8N2FLceLZtF6GbPWpGwVq+BarP2VPQSigR+sdaG+H5fFDHQ3bGFfzxgNb4L +8SDCfsLI9j6Q== From: Asahi Lina Date: Sun, 02 Feb 2025 22:05:47 +0900 Subject: [PATCH 5/6] rust: page: Add physical address conversion functions Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250202-rust-page-v1-5-e3170d7fe55e@asahilina.net> References: <20250202-rust-page-v1-0-e3170d7fe55e@asahilina.net> In-Reply-To: <20250202-rust-page-v1-0-e3170d7fe55e@asahilina.net> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Cc: Jann Horn , Matthew Wilcox , Paolo Bonzini , Danilo Krummrich , Wedson Almeida Filho , Valentin Obst , Andrew Morton , linux-mm@kvack.org, airlied@redhat.com, Abdiel Janulgue , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev, Asahi Lina X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1738501594; l=5914; i=lina@asahilina.net; s=20240902; h=from:subject:message-id; bh=/l/yqYUI25JHcLzcwMcBrWhtbhlMTOGAR488Z8bons4=; b=smOvr2ghPdeB8WGsuSZMnK+RdiZl/ggO1sjUJa9nF7n897PX4BZqpGtxrsCgRD0xgyLMFsN4C lONdvO0e8EiCCt7Wqof9tOCVkpiKbnePQ+jExk4ZKxJ5pM1CvRROa5X X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=tpv7cWfUnHNw5jwf6h4t0gGgglt3/xcwlfs0+A/uUu8= Add methods to allow code using the Page type to obtain the physical address of a page, convert to and from an (owned) physical address, and borrow a Page from a physical address. Most of these operations are, as you might expect, unsafe. These primitives are useful to implement page table structures in Rust, and to implement arbitrary physical memory access (as needed to walk arbitrary page tables and dereference through them). These mechanisms are, of course, fraught with danger, and are only expected to be used for core memory management code (in e.g. drivers with their own device page table implementations) and for debug features such as crash dumps of device memory. Signed-off-by: Asahi Lina --- rust/helpers/page.c | 26 +++++++++++++++++++++ rust/kernel/page.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 91 insertions(+) diff --git a/rust/helpers/page.c b/rust/helpers/page.c index b3f2b8fbf87fc9aa89cb1636736c52be16411301..1c3bd68818d77f7ce7806329b8f= 040a7d4205bb3 100644 --- a/rust/helpers/page.c +++ b/rust/helpers/page.c @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 =20 +#include #include #include =20 @@ -17,3 +18,28 @@ void rust_helper_kunmap_local(const void *addr) { kunmap_local(addr); } + +struct page *rust_helper_phys_to_page(phys_addr_t phys) +{ + return phys_to_page(phys); +} + +phys_addr_t rust_helper_page_to_phys(struct page *page) +{ + return page_to_phys(page); +} + +unsigned long rust_helper_phys_to_pfn(phys_addr_t phys) +{ + return __phys_to_pfn(phys); +} + +struct page *rust_helper_pfn_to_page(unsigned long pfn) +{ + return pfn_to_page(pfn); +} + +bool rust_helper_pfn_valid(unsigned long pfn) +{ + return pfn_valid(pfn); +} diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs index fe5f879f9d1a86083fd55c682fad9d52466f79a2..67cd7006fa63ab5aed4c4de2be6= 39ed8e1fbc2ba 100644 --- a/rust/kernel/page.rs +++ b/rust/kernel/page.rs @@ -3,6 +3,7 @@ //! Kernel page allocation and management. =20 use crate::{ + addr::*, alloc::{AllocError, Flags}, bindings, error::code::*, @@ -10,6 +11,7 @@ types::{Opaque, Ownable, Owned}, uaccess::UserSliceReader, }; +use core::mem::ManuallyDrop; use core::ptr::{self, NonNull}; =20 /// A bitwise shift for the page size. @@ -249,6 +251,69 @@ pub unsafe fn copy_from_user_slice_raw( reader.read_raw(unsafe { core::slice::from_raw_parts_mut(dst.c= ast(), len) }) }) } + + /// Returns the physical address of this page. + pub fn phys(&self) -> PhysicalAddr { + // SAFETY: `page` is valid due to the type invariants on `Page`. + unsafe { bindings::page_to_phys(self.as_ptr()) } + } + + /// Converts a Rust-owned Page into its physical address. + /// + /// The caller is responsible for calling [`Page::from_phys()`] to avo= id leaking memory. + pub fn into_phys(this: Owned) -> PhysicalAddr { + ManuallyDrop::new(this).phys() + } + + /// Converts a physical address to a Rust-owned Page. + /// + /// # Safety + /// The caller must ensure that the physical address was previously re= turned by a call to + /// [`Page::into_phys()`], and that the physical address is no longer = used after this call, + /// nor is [`Page::from_phys()`] called again on it. + pub unsafe fn from_phys(phys: PhysicalAddr) -> Owned { + // SAFETY: By the safety requirements, the physical address must b= e valid and + // have come from `into_phys()`, so phys_to_page() cannot fail and + // must return the original struct page pointer. + unsafe { Owned::from_raw(NonNull::new_unchecked(bindings::phys_to_= page(phys)).cast()) } + } + + /// Borrows a Page from a physical address, without taking over owners= hip. + /// + /// If the physical address does not have a `struct page` entry or is = not + /// part of a System RAM region, returns None. + /// + /// # Safety + /// The caller must ensure that the physical address, if it is backed = by a `struct page`, + /// remains available for the duration of the borrowed lifetime. + pub unsafe fn borrow_phys(phys: &PhysicalAddr) -> Option<&Self> { + // SAFETY: This is always safe, as it is just arithmetic + let pfn =3D unsafe { bindings::phys_to_pfn(*phys) }; + // SAFETY: This function is safe to call with any pfn + if !unsafe { bindings::pfn_valid(pfn) && bindings::page_is_ram(pfn= ) !=3D 0 } { + None + } else { + // SAFETY: We have just checked that the pfn is valid above, s= o it must + // have a corresponding struct page. By the safety requirement= s, we can + // return a borrowed reference to it. + Some(unsafe { &*(bindings::pfn_to_page(pfn) as *mut Self as *c= onst Self) }) + } + } + + /// Borrows a Page from a physical address, without taking over owners= hip + /// nor checking for validity. + /// + /// # Safety + /// The caller must ensure that the physical address is backed by a `s= truct page` and + /// corresponds to System RAM. This is true when the address was retur= ned by + /// [`Page::into_phys()`]. + pub unsafe fn borrow_phys_unchecked(phys: &PhysicalAddr) -> &Self { + // SAFETY: This is always safe, as it is just arithmetic + let pfn =3D unsafe { bindings::phys_to_pfn(*phys) }; + // SAFETY: The caller guarantees that the pfn is valid. By the saf= ety + // requirements, we can return a borrowed reference to it. + unsafe { &*(bindings::pfn_to_page(pfn) as *mut Self as *const Self= ) } + } } =20 // SAFETY: `Owned` objects returned by Page::alloc_page() follow the= requirements of --=20 2.47.1 From nobody Sun Feb 8 12:51:55 2026 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) (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 2DF841D8E06; Sun, 2 Feb 2025 13:06:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.63.210.85 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738501613; cv=none; b=a2TDwTV5GcnJx9XGVC6ZVWOSb21u+TjmUybz8DeYqeE+z2sxmMyo2d+xjFL2p26MtTYxweWkURxdtoZACc4dVAS2zqthW5Yy6+CBcI5zN7gE0Ro87/fgjvtONnW3Bin20aQCcsLEV/II9R+MX/hCrvvYcmSbeNLU2sBIxVpzpJI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738501613; c=relaxed/simple; bh=jAxlx0oKPYhbCLOndVQegKI/zkoqLO2seAFvco9COAE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=VxY/Nkt6lEYtXPdKUvGdE2rPvzKbBSAp8auUDGWZ4jcPC1d7Adp+BZ1jQInDoDC8UqkAXja1/Ci2IHa32URK9tykgEva7JMvB0tV3UvdkUCHMs1pJ0y2+/Dmb+vh/+vgkTYgc6epmqxXGet7JFbBaH/yK64B3YF+ug+cYUn8puY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=asahilina.net; spf=pass smtp.mailfrom=asahilina.net; dkim=pass (2048-bit key) header.d=asahilina.net header.i=@asahilina.net header.b=W68hCmAT; arc=none smtp.client-ip=212.63.210.85 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=asahilina.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=asahilina.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=asahilina.net header.i=@asahilina.net header.b="W68hCmAT" Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: sendonly@marcansoft.com) by mail.marcansoft.com (Postfix) with ESMTPSA id 0E6D142CF1; Sun, 2 Feb 2025 13:06:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1738501610; bh=jAxlx0oKPYhbCLOndVQegKI/zkoqLO2seAFvco9COAE=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=W68hCmATDKo9EiH/xMtAcdph4x+tpmPmNtxtu+tx82fwJBpSxeMQbd/aWh5QzcOKr G0ENO9GNbN8fsm1e0DDj/5x8+TN5kGJOiYdCd69beGzF/I1BDJ6P7Jq86/lwzga3cP Dujgi22bP8LU8ZB+fDML04o3s7g4Y+AHNSFxOosjo/msGdpUFqAtZypGs7yghtqruD TALKh0aV8LdMcXODde+LkiY6rV637Jn9uvFqd2LhpV135qKBe1dUi5H0eFWmpa+yIK JI2eDyF5njHMDptbSKSnURDe34XUmvYbXU/u0qe+0ttIPTE3LlL6M0rgArHX61eeX4 sf2dw0y2kuKzw== From: Asahi Lina Date: Sun, 02 Feb 2025 22:05:48 +0900 Subject: [PATCH 6/6] rust: page: Make Page::as_ptr() pub(crate) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250202-rust-page-v1-6-e3170d7fe55e@asahilina.net> References: <20250202-rust-page-v1-0-e3170d7fe55e@asahilina.net> In-Reply-To: <20250202-rust-page-v1-0-e3170d7fe55e@asahilina.net> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Cc: Jann Horn , Matthew Wilcox , Paolo Bonzini , Danilo Krummrich , Wedson Almeida Filho , Valentin Obst , Andrew Morton , linux-mm@kvack.org, airlied@redhat.com, Abdiel Janulgue , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev, Asahi Lina X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1738501594; l=869; i=lina@asahilina.net; s=20240902; h=from:subject:message-id; bh=jAxlx0oKPYhbCLOndVQegKI/zkoqLO2seAFvco9COAE=; b=Yx1gVXvO3w0R4qdGomFIq+0RwKj460R5wvIbngoMuX6wHDoesKQMqRxKU0o3unMwKL69zlqrR En16s28Jr/vB388LQY8FUK1/cVO+HCe4enKe0BKjctozf6HJnt9HVbf X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=tpv7cWfUnHNw5jwf6h4t0gGgglt3/xcwlfs0+A/uUu8= There's no good reason for drivers to need access to the raw `struct page` pointer, since that should be handled by Rust abstractions. Make this method pub(crate) so it is only visible to the kernel crate. Signed-off-by: Asahi Lina --- rust/kernel/page.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs index 67cd7006fa63ab5aed4c4de2be639ed8e1fbc2ba..b7bce033b53682eca6e61f47e4f= d3aa902da7900 100644 --- a/rust/kernel/page.rs +++ b/rust/kernel/page.rs @@ -83,7 +83,7 @@ pub fn alloc_page(flags: Flags) -> Result, Al= locError> { } =20 /// Returns a raw pointer to the page. - pub fn as_ptr(&self) -> *mut bindings::page { + pub(crate) fn as_ptr(&self) -> *mut bindings::page { Opaque::raw_get(&self.page) } =20 --=20 2.47.1