Skip to content

Commit 34d5074

Browse files
hoshinolinajannau
authored andcommitted
rust: page: Convert to Ownable
This allows Page references to be returned as borrowed references, without necessarily owning the struct page. Signed-off-by: Asahi Lina <[email protected]>
1 parent fcbba36 commit 34d5074

1 file changed

Lines changed: 16 additions & 11 deletions

File tree

‎rust/kernel/page.rs‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{
77
bindings,
88
error::code::*,
99
error::Result,
10+
types::{Opaque, Ownable, Owned},
1011
uaccess::UserSliceReader,
1112
};
1213
use core::{
@@ -99,7 +100,7 @@ pub const fn page_align(addr: usize) -> Option<usize> {
99100
///
100101
/// [`VBox`]: kernel::alloc::VBox
101102
/// [`Vmalloc`]: kernel::alloc::allocator::Vmalloc
102-
pub struct BorrowedPage<'a>(ManuallyDrop<Page>, PhantomData<&'a Page>);
103+
pub struct BorrowedPage<'a>(ManuallyDrop<Owned<Page>>, PhantomData<&'a Page>);
103104

104105
impl<'a> BorrowedPage<'a> {
105106
/// Constructs a [`BorrowedPage`] from a raw pointer to a `struct page`.
@@ -109,7 +110,7 @@ impl<'a> BorrowedPage<'a> {
109110
/// - `ptr` must point to a valid `bindings::page`.
110111
/// - `ptr` must remain valid for the entire lifetime `'a`.
111112
pub unsafe fn from_raw(ptr: NonNull<bindings::page>) -> Self {
112-
let page = Page { page: ptr };
113+
let page = unsafe { Page::from_phys(bindings::page_to_phys(ptr.as_ptr())) };
113114

114115
// INVARIANT: The safety requirements guarantee that `ptr` is valid for the entire lifetime
115116
// `'a`.
@@ -142,8 +143,9 @@ pub trait AsPageIter {
142143
/// # Invariants
143144
///
144145
/// The pointer is valid, and has ownership over the page.
146+
#[repr(transparent)]
145147
pub struct Page {
146-
page: NonNull<bindings::page>,
148+
page: Opaque<bindings::page>,
147149
}
148150

149151
// SAFETY: Pages have no logic that relies on them staying on a given thread, so moving them across
@@ -177,19 +179,20 @@ impl Page {
177179
/// # Ok::<(), kernel::alloc::AllocError>(())
178180
/// ```
179181
#[inline]
180-
pub fn alloc_page(flags: Flags) -> Result<Self, AllocError> {
182+
pub fn alloc_page(flags: Flags) -> Result<Owned<Self>, AllocError> {
181183
// SAFETY: Depending on the value of `gfp_flags`, this call may sleep. Other than that, it
182184
// is always safe to call this method.
183185
let page = unsafe { bindings::alloc_pages(flags.as_raw(), 0) };
184186
let page = NonNull::new(page).ok_or(AllocError)?;
185-
// INVARIANT: We just successfully allocated a page, so we now have ownership of the newly
186-
// allocated page. We transfer that ownership to the new `Page` object.
187-
Ok(Self { page })
187+
// SAFETY: We just successfully allocated a page, so we now have ownership of the newly
188+
// allocated page. We transfer that ownership to the new `Owned<Page>` object.
189+
// Since `Page` is transparent, we can cast the pointer directly.
190+
Ok(unsafe { Owned::from_raw(page.cast()) })
188191
}
189192

190193
/// Returns a raw pointer to the page.
191194
pub fn as_ptr(&self) -> *mut bindings::page {
192-
self.page.as_ptr()
195+
Opaque::cast_into(&self.page)
193196
}
194197

195198
/// Get the node id containing this page.
@@ -364,10 +367,12 @@ impl Page {
364367
}
365368
}
366369

367-
impl Drop for Page {
370+
// SAFETY: See below.
371+
unsafe impl Ownable for Page {
368372
#[inline]
369-
fn drop(&mut self) {
373+
unsafe fn release(this: NonNull<Self>) {
370374
// SAFETY: By the type invariants, we have ownership of the page and can free it.
371-
unsafe { bindings::__free_pages(self.page.as_ptr(), 0) };
375+
// Since Page is transparent, we can cast the raw pointer directly.
376+
unsafe { bindings::__free_pages(this.cast().as_ptr(), 0) };
372377
}
373378
}

0 commit comments

Comments
 (0)