Skip to content

Commit 467971a

Browse files
author
Danilo Krummrich
committed
rust: page: implement BorrowedPage
Currently, a Page always owns the underlying struct page. However, sometimes a struct page may be owned by some other entity, e.g. a vmalloc allocation. Hence, introduce BorrowedPage to support such cases, until the Ownable solution [1] lands. This is required by the scatterlist abstractions. Acked-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Alexandre Courbot <acourbot@nvidia.com> Tested-by: Alexandre Courbot <acourbot@nvidia.com> Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com> Link: https://lore.kernel.org/rust-for-linux/ZnCzLIly3DRK2eab@boqun-archlinux/ [1] Link: https://lore.kernel.org/r/20250820145434.94745-2-dakr@kernel.org Signed-off-by: Danilo Krummrich <dakr@kernel.org>
1 parent 93296e9 commit 467971a

2 files changed

Lines changed: 75 additions & 1 deletion

File tree

rust/bindings/bindings_helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include <linux/jiffies.h>
5858
#include <linux/jump_label.h>
5959
#include <linux/mdio.h>
60+
#include <linux/mm.h>
6061
#include <linux/miscdevice.h>
6162
#include <linux/of_device.h>
6263
#include <linux/pci.h>

rust/kernel/page.rs

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ use crate::{
99
error::Result,
1010
uaccess::UserSliceReader,
1111
};
12-
use core::ptr::{self, NonNull};
12+
use core::{
13+
marker::PhantomData,
14+
mem::ManuallyDrop,
15+
ops::Deref,
16+
ptr::{self, NonNull},
17+
};
1318

1419
/// A bitwise shift for the page size.
1520
pub const PAGE_SHIFT: usize = bindings::PAGE_SHIFT as usize;
@@ -30,6 +35,74 @@ pub const fn page_align(addr: usize) -> usize {
3035
(addr + (PAGE_SIZE - 1)) & PAGE_MASK
3136
}
3237

38+
/// Representation of a non-owning reference to a [`Page`].
39+
///
40+
/// This type provides a borrowed version of a [`Page`] that is owned by some other entity, e.g. a
41+
/// [`Vmalloc`] allocation such as [`VBox`].
42+
///
43+
/// # Example
44+
///
45+
/// ```
46+
/// # use kernel::{bindings, prelude::*};
47+
/// use kernel::page::{BorrowedPage, Page, PAGE_SIZE};
48+
/// # use core::{mem::MaybeUninit, ptr, ptr::NonNull };
49+
///
50+
/// fn borrow_page<'a>(vbox: &'a mut VBox<MaybeUninit<[u8; PAGE_SIZE]>>) -> BorrowedPage<'a> {
51+
/// let ptr = ptr::from_ref(&**vbox);
52+
///
53+
/// // SAFETY: `ptr` is a valid pointer to `Vmalloc` memory.
54+
/// let page = unsafe { bindings::vmalloc_to_page(ptr.cast()) };
55+
///
56+
/// // SAFETY: `vmalloc_to_page` returns a valid pointer to a `struct page` for a valid
57+
/// // pointer to `Vmalloc` memory.
58+
/// let page = unsafe { NonNull::new_unchecked(page) };
59+
///
60+
/// // SAFETY:
61+
/// // - `self.0` is a valid pointer to a `struct page`.
62+
/// // - `self.0` is valid for the entire lifetime of `self`.
63+
/// unsafe { BorrowedPage::from_raw(page) }
64+
/// }
65+
///
66+
/// let mut vbox = VBox::<[u8; PAGE_SIZE]>::new_uninit(GFP_KERNEL)?;
67+
/// let page = borrow_page(&mut vbox);
68+
///
69+
/// // SAFETY: There is no concurrent read or write to this page.
70+
/// unsafe { page.fill_zero_raw(0, PAGE_SIZE)? };
71+
/// # Ok::<(), Error>(())
72+
/// ```
73+
///
74+
/// # Invariants
75+
///
76+
/// The borrowed underlying pointer to a `struct page` is valid for the entire lifetime `'a`.
77+
///
78+
/// [`VBox`]: kernel::alloc::VBox
79+
/// [`Vmalloc`]: kernel::alloc::allocator::Vmalloc
80+
pub struct BorrowedPage<'a>(ManuallyDrop<Page>, PhantomData<&'a Page>);
81+
82+
impl<'a> BorrowedPage<'a> {
83+
/// Constructs a [`BorrowedPage`] from a raw pointer to a `struct page`.
84+
///
85+
/// # Safety
86+
///
87+
/// - `ptr` must point to a valid `bindings::page`.
88+
/// - `ptr` must remain valid for the entire lifetime `'a`.
89+
pub unsafe fn from_raw(ptr: NonNull<bindings::page>) -> Self {
90+
let page = Page { page: ptr };
91+
92+
// INVARIANT: The safety requirements guarantee that `ptr` is valid for the entire lifetime
93+
// `'a`.
94+
Self(ManuallyDrop::new(page), PhantomData)
95+
}
96+
}
97+
98+
impl<'a> Deref for BorrowedPage<'a> {
99+
type Target = Page;
100+
101+
fn deref(&self) -> &Self::Target {
102+
&self.0
103+
}
104+
}
105+
33106
/// A pointer to a page that owns the page allocation.
34107
///
35108
/// # Invariants

0 commit comments

Comments
 (0)