From: Abdiel Janulgue <abdiel.janulgue@gmail.com>
To: rust-for-linux@vger.kernel.org, aliceryhl@google.com
Cc: dakr@redhat.com, linux-kernel@vger.kernel.org,
airlied@redhat.com, miguel.ojeda.sandonis@gmail.com,
boqun.feng@gmail.com, Abdiel Janulgue <abdiel.janulgue@gmail.com>
Subject: [PATCH v2 2/5] rust: page: Make ownership of the page pointer explicit.
Date: Wed, 23 Oct 2024 01:44:46 +0300 [thread overview]
Message-ID: <20241022224832.1505432-3-abdiel.janulgue@gmail.com> (raw)
In-Reply-To: <20241022224832.1505432-1-abdiel.janulgue@gmail.com>
Ensure only `Page::alloc_page` return pages that own the page allocation.
This requires that we replace the page pointer wrapper with Opaque instead
of NonNull to make it possible to cast to a Page pointer from a raw struct
page pointer.
Signed-off-by: Abdiel Janulgue <abdiel.janulgue@gmail.com>
---
rust/kernel/page.rs | 25 ++++++++++++++++---------
1 file changed, 16 insertions(+), 9 deletions(-)
diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
index fdac6c375fe4..a8288c15b860 100644
--- a/rust/kernel/page.rs
+++ b/rust/kernel/page.rs
@@ -8,8 +8,9 @@
error::code::*,
error::Result,
uaccess::UserSliceReader,
+ types::{Opaque, Owned, Ownable},
};
-use core::ptr::{self, NonNull};
+use core::ptr::{self};
/// A bitwise shift for the page size.
pub const PAGE_SHIFT: usize = bindings::PAGE_SHIFT as usize;
@@ -35,8 +36,9 @@ pub const fn page_align(addr: usize) -> usize {
/// # Invariants
///
/// The pointer is valid, and has ownership over the page.
+#[repr(transparent)]
pub struct Page {
- page: NonNull<bindings::page>,
+ page: Opaque<bindings::page>,
}
// SAFETY: Pages have no logic that relies on them staying on a given thread, so moving them across
@@ -71,19 +73,24 @@ impl Page {
/// let page = Page::alloc_page(GFP_KERNEL | __GFP_ZERO)?;
/// # Ok(()) }
/// ```
- pub fn alloc_page(flags: Flags) -> Result<Self, AllocError> {
+ pub fn alloc_page(flags: Flags) -> Result<Owned<Self>, 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)?;
+ if page.is_null() {
+ return Err(AllocError);
+ }
+ // CAST: Self` is a `repr(transparent)` wrapper around `bindings::page`.
+ let ptr = page.cast::<Self>();
// 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: According to invariant above ptr is valid.
+ Ok(unsafe { Owned::to_owned(ptr) })
}
/// Returns a raw pointer to the page.
pub fn as_ptr(&self) -> *mut bindings::page {
- self.page.as_ptr()
+ self.page.get()
}
/// Runs a piece of code with this page mapped to an address.
@@ -252,9 +259,9 @@ pub unsafe fn copy_from_user_slice_raw(
}
}
-impl Drop for Page {
- fn drop(&mut self) {
+unsafe impl Ownable for Page {
+ unsafe fn ptr_drop(ptr: *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 { bindings::__free_pages(ptr.cast(), 0) };
}
}
--
2.43.0
next prev parent reply other threads:[~2024-10-22 22:49 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-22 22:44 [PATCH v2 0/5] Introduce Owned type and Ownable trait (was: "rust: page: Add support for vmalloc_to_page") Abdiel Janulgue
2024-10-22 22:44 ` [PATCH v2 1/5] rust: types: add `Owned` type and `Ownable` trait Abdiel Janulgue
2024-10-23 8:10 ` Danilo Krummrich
2024-10-23 9:28 ` Alice Ryhl
2024-10-23 10:26 ` Abdiel Janulgue
2024-10-23 14:58 ` Boqun Feng
2024-10-23 17:52 ` Alice Ryhl
2024-10-23 18:07 ` Boqun Feng
2024-10-24 7:23 ` Alice Ryhl
2024-10-24 7:33 ` Alice Ryhl
2024-11-01 13:38 ` Abdiel Janulgue
2024-11-01 13:49 ` Alice Ryhl
2024-10-22 22:44 ` Abdiel Janulgue [this message]
2024-10-22 22:44 ` [PATCH v2 3/5] rust: page: Extend support to vmalloc_to_page Abdiel Janulgue
2024-10-23 8:42 ` Danilo Krummrich
2024-10-23 9:03 ` Danilo Krummrich
2024-10-23 10:26 ` Abdiel Janulgue
2024-10-23 11:30 ` Danilo Krummrich
2024-10-22 22:44 ` [PATCH v2 4/5] rust: page: Add page_slice_to_page Abdiel Janulgue
2024-10-22 22:44 ` [PATCH v2 5/5] rust: firmware: implement `Ownable` for Firmware Abdiel Janulgue
2024-10-23 9:35 ` Danilo Krummrich
2024-10-23 9:45 ` Danilo Krummrich
2024-10-27 22:20 ` Boqun Feng
2024-10-28 13:37 ` Danilo Krummrich
2024-10-23 8:03 ` [PATCH v2 0/5] Introduce Owned type and Ownable trait (was: "rust: page: Add support for vmalloc_to_page") Danilo Krummrich
2024-10-23 9:51 ` Miguel Ojeda
2024-10-23 12:01 ` Danilo Krummrich
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241022224832.1505432-3-abdiel.janulgue@gmail.com \
--to=abdiel.janulgue@gmail.com \
--cc=airlied@redhat.com \
--cc=aliceryhl@google.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=miguel.ojeda.sandonis@gmail.com \
--cc=rust-for-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®