[PATCH 3/3] rust: page: Add page_slice_to_page
From: Abdiel Janulgue
Date: Mon Oct 07 2024 - 16:29:51 EST
Make it convenient to convert buffers into page-sized vmalloc'd chunks which can
then be used in vmalloc_to_page().
Signed-off-by: Abdiel Janulgue <abdiel.janulgue@xxxxxxxxx>
---
rust/kernel/page.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 58 insertions(+), 1 deletion(-)
diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
index a84f382daf5c..490685144694 100644
--- a/rust/kernel/page.rs
+++ b/rust/kernel/page.rs
@@ -3,7 +3,7 @@
//! Kernel page allocation and management.
use crate::{
- alloc::{AllocError, Flags},
+ alloc::{AllocError, Flags, VVec, flags::*},
bindings,
error::code::*,
error::Result,
@@ -106,6 +106,27 @@ pub fn vmalloc_to_page<'a>(
Ok(unsafe { &*page.cast() })
}
+ /// A convenience wrapper to vmalloc_to_page which ensures it takes a page-sized buffer
+ /// represented by `PageSlice`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use kernel::page::*;
+ ///
+ /// # fn dox() -> Result<(), kernel::alloc::AllocError> {
+ /// let data: [u8; PAGE_SIZE * 2] = [0; PAGE_SIZE * 2];
+ /// let buf: &[u8] = &data;
+ ///
+ /// let pages: VVec<PageSlice> = buf.try_into()?;
+ /// let page = Page::page_slice_to_page(&pages[0])?;
+ /// # Ok(()) }
+ /// ```
+ pub fn page_slice_to_page<'a>(page: &PageSlice) -> Result<&'a Self, AllocError>
+ {
+ Self::vmalloc_to_page(page.0.as_ptr() as _)
+ }
+
/// Returns a raw pointer to the page.
pub fn as_ptr(&self) -> *mut bindings::page {
self.page.get()
@@ -277,6 +298,42 @@ pub unsafe fn copy_from_user_slice_raw(
}
}
+/// A page-aligned, page-sized object. This is used for convenience to convert a large buffer into
+/// an array of page-sized chunks which can then be used in `Page::page_slice_to_page` wrapper.
+///
+// FIXME: This should be `PAGE_SIZE`, but the compiler rejects everything except a literal
+// integer argument for the `repr(align)` attribute.
+#[repr(align(4096))]
+pub struct PageSlice([u8; PAGE_SIZE]);
+
+impl TryFrom<&[u8]> for VVec<PageSlice> {
+ type Error = AllocError;
+
+ fn try_from(val: &[u8]) -> Result<Self, AllocError> {
+ let mut k = VVec::new();
+ let aligned_len: usize;
+
+ // Ensure the size is page-aligned.
+ match core::alloc::Layout::from_size_align(val.len(), PAGE_SIZE) {
+ Ok(align) => { aligned_len = align.pad_to_align().size() },
+ Err(_) => { return Err(AllocError) },
+ };
+ let pages = aligned_len >> PAGE_SHIFT;
+ match k.reserve(pages, GFP_KERNEL) {
+ Ok(_) => {
+ // SAFETY: from above, the length should be equal to the vector's capacity
+ unsafe { k.set_len(pages); }
+ // SAFETY: src buffer sized val.len() does not overlap with dst buffer since
+ // the dst buffer's size is val.len() padded up to a multiple of PAGE_SIZE.
+ unsafe { ptr::copy_nonoverlapping(val.as_ptr(), k.as_mut_ptr() as *mut u8,
+ val.len()) };
+ },
+ Err(_) => { return Err(AllocError) },
+ };
+ Ok(k)
+ }
+}
+
impl Drop for Page {
fn drop(&mut self) {
// SAFETY: By the type invariants, we have ownership of the page and can free it.
--
2.34.1