Skip to contentSkip to conversation

rust: page: Add support for vmalloc_to_page

PATCH In review Abdiel Janulgue 6 participants 13 messages Oct 7, 2024 – Oct 15, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

This series aims to add support for pages that are not allocated by an instance of the Page abstraction, specifically those returned by vmalloc_to_page().

This patch series is sent in the context of developing a Nova driver WIP feature where we load the the GSP firmware into an sg table [1].

[1] https://gitlab.freedesktop.org/abj/nova-drm

Abdiel Janulgue (3):
rust: page: replace the page pointer wrapper with Opaque
rust: page: Extend support to vmalloc_to_page
rust: page: Add page_slice_to_page

 rust/kernel/page.rs | 111 ++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 102 insertions(+), 9 deletions(-)

base-commit: 673d1648244c3840043e09a784164b38c2e2efb9

Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/3 rust: page: replace the page pointer wrapper with Opaque

Replace NonNull with Opaque to make it possible to cast to a Page pointer from a raw struct page pointer.

---
 rust/kernel/page.rs | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
rust/kernel/page.rs +13 -6
index 208a006d587c..08ff09a25223 100644
@@ -8,8 +8,9 @@
     error::code::*,
     error::Result,
     uaccess::UserSliceReader,
+    types::Opaque,
 };
-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;
@@ -25,8 +26,9 @@
 /// # 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
@@ -65,15 +67,20 @@ pub fn alloc_page(flags: Flags) -> Result<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 { ptr::read(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.
@@ -245,6 +252,6 @@ pub unsafe fn copy_from_user_slice_raw(
 impl Drop for Page {
     fn drop(&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(self.page.get(), 0) };
     }
 }
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH 1/3] rust: page: replace the page pointer wrapper with Opaque

Quoted 40 lines · Abdiel Janulgue

Replace NonNull with Opaque to make it possible to cast to a Page pointer from a raw struct page pointer.

---
 rust/kernel/page.rs | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
rust/kernel/page.rs +4 -3
index 208a006d587c..08ff09a25223 100644
@@ -8,8 +8,9 @@
     error::code::*,
     error::Result,
     uaccess::UserSliceReader,
+    types::Opaque,
 };
-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;
@@ -25,8 +26,9 @@
 /// # 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
@@ -65,15 +67,20 @@ pub fn alloc_page(flags: Flags) -> Result<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)?;

rust/kernel/page.rs pub fn alloc_page(flags: Flags) -> Result<Self, AllocError> { · line 78

Quoted 10 lines
rust/kernel/page.rs +7 -1
+        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 { ptr::read(ptr) })

Using ptr::read on the page is definitely not okay. That duplicates the contents of the struct page. You'll need some sort of pointer type around Page instead.

Alice

#3 lore raw

Re: [PATCH 1/3] rust: page: replace the page pointer wrapper with Opaque

Quoted 48 lines · Alice Ryhl
Quoted 46 lines · Abdiel Janulgue

Replace NonNull with Opaque to make it possible to cast to a Page pointer from a raw struct page pointer.

---
 rust/kernel/page.rs | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
rust/kernel/page.rs +9 -3
index 208a006d587c..08ff09a25223 100644
@@ -8,8 +8,9 @@
     error::code::*,
     error::Result,
     uaccess::UserSliceReader,
+    types::Opaque,
 };
-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;
@@ -25,8 +26,9 @@
 /// # 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
@@ -65,15 +67,20 @@ pub fn alloc_page(flags: Flags) -> Result<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
Quoted 9 lines

rust/kernel/page.rs pub fn alloc_page(flags: Flags) -> Result<Self, AllocError> { · line 78

Quoted 4 lines
rust/kernel/page.rs +2 -1
         // allocated page. We transfer that ownership to the new `Page` object.
-        Ok(Self { page })
+        // SAFETY: According to invariant above ptr is valid.
+        Ok(unsafe { ptr::read(ptr) })

Using ptr::read on the page is definitely not okay. That duplicates the contents of the struct page. You'll need some sort of pointer type around Page instead.

Agreed. So may I suggest we introduce Owned type and Ownable trait [1]? alloc_page() can be refactor to return a Result<Owned<Self>, AllocError>.

[1]: https://lore.kernel.org/rust-for-linux/ZnCzLIly3DRK2eab@boqun-archlinux/

Regards,
Boqun

Quoted 1 line

Alice

#4 lore raw

Re: [PATCH 1/3] rust: page: replace the page pointer wrapper with Opaque

Quoted 59 lines · Boqun Feng
Quoted 48 lines · Alice Ryhl
Quoted 46 lines · Abdiel Janulgue

Replace NonNull with Opaque to make it possible to cast to a Page pointer from a raw struct page pointer.

---
  rust/kernel/page.rs | 19 +++++++++++++------
  1 file changed, 13 insertions(+), 6 deletions(-)
rust/kernel/page.rs +9 -3
index 208a006d587c..08ff09a25223 100644
@@ -8,8 +8,9 @@
      error::code::*,
      error::Result,
      uaccess::UserSliceReader,
+    types::Opaque,
  };
-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;
@@ -25,8 +26,9 @@
  /// # 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
@@ -65,15 +67,20 @@ pub fn alloc_page(flags: Flags) -> Result<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
Quoted 9 lines

rust/kernel/page.rs pub fn alloc_page(flags: Flags) -> Result<Self, AllocError> { · line 78

Quoted 4 lines
rust/kernel/page.rs +2 -1
          // allocated page. We transfer that ownership to the new `Page` object.
-        Ok(Self { page })
+        // SAFETY: According to invariant above ptr is valid.
+        Ok(unsafe { ptr::read(ptr) })

Using ptr::read on the page is definitely not okay. That duplicates the contents of the struct page. You'll need some sort of pointer type around Page instead.

Quoted 5 lines

Agreed. So may I suggest we introduce Owned type and Ownable trait [1]? alloc_page() can be refactor to return a Result<Owned<Self>, AllocError>.

[1]: https://lore.kernel.org/rust-for-linux/ZnCzLIly3DRK2eab@boqun-archlinux/

Thanks for the feedback. How do you propose we move forward, do I take a stab at implementing Owned type and Ownable trait?

Regards,
Abdiel

Quoted 6 lines

Regards,
Boqun

Quoted 1 line

Alice

#5 lore raw

Re: [PATCH 1/3] rust: page: replace the page pointer wrapper with Opaque

Quoted 62 lines · Abdiel Janulgue
Quoted 59 lines · Boqun Feng
Quoted 57 lines · Alice Ryhl
Quoted 40 lines · Abdiel Janulgue

Replace NonNull with Opaque to make it possible to cast to a Page pointer from a raw struct page pointer.

---
  rust/kernel/page.rs | 19 +++++++++++++------
  1 file changed, 13 insertions(+), 6 deletions(-)
rust/kernel/page.rs +4 -3
index 208a006d587c..08ff09a25223 100644
@@ -8,8 +8,9 @@
      error::code::*,
      error::Result,
      uaccess::UserSliceReader,
+    types::Opaque,
  };
-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;
@@ -25,8 +26,9 @@
  /// # 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
@@ -65,15 +67,20 @@ pub fn alloc_page(flags: Flags) -> Result<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)?;

rust/kernel/page.rs pub fn alloc_page(flags: Flags) -> Result<Self, AllocError> { · line 78

Quoted 10 lines
rust/kernel/page.rs +7 -1
+        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 { ptr::read(ptr) })

Using ptr::read on the page is definitely not okay. That duplicates the contents of the struct page. You'll need some sort of pointer type around Page instead.

Quoted 8 lines
Quoted 5 lines

Agreed. So may I suggest we introduce Owned type and Ownable trait [1]? alloc_page() can be refactor to return a Result<Owned<Self>, AllocError>.

[1]: https://lore.kernel.org/rust-for-linux/ZnCzLIly3DRK2eab@boqun-archlinux/

Thanks for the feedback. How do you propose we move forward, do I take a stab at implementing Owned type and Ownable trait?

If you're interested, go ahead ;-)

Regards,
Boqun

Quoted 11 lines

Regards,
Abdiel

Quoted 6 lines

Regards,
Boqun

Quoted 1 line

Alice

#6 lore raw

Re: [PATCH 1/3] rust: page: replace the page pointer wrapper with Opaque

In reply to Abdiel Janulgue
Quoted 24 lines · Abdiel Janulgue

Replace NonNull with Opaque to make it possible to cast to a Page pointer from a raw struct page pointer.

---
 rust/kernel/page.rs | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
rust/kernel/page.rs +2 -1
index 208a006d587c..08ff09a25223 100644
@@ -8,8 +8,9 @@
     error::code::*,
     error::Result,
     uaccess::UserSliceReader,
+    types::Opaque,
 };
-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;

rust/kernel/page.rs line 31

Quoted 7 lines
rust/kernel/page.rs +2 -1
@@ -25,8 +26,9 @@
 /// # Invariants
 ///
 /// The pointer is valid, and has ownership over the page.
+#[repr(transparent)]
 pub struct Page {
-    page: NonNull<bindings::page>,
+    page: Opaque<bindings::page>,

Still not to sure where to encode pinning in the type api. Looking into the C struct I see a union that sometimes holds a list head, should this then be a pinned thing in this type?

Fiona

Quoted 37 lines
rust/kernel/page.rs +9 -4
 }
 
 // SAFETY: Pages have no logic that relies on them staying on a given thread, so moving them across
@@ -65,15 +67,20 @@ pub fn alloc_page(flags: Flags) -> Result<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 { ptr::read(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.
@@ -245,6 +252,6 @@ pub unsafe fn copy_from_user_slice_raw(
 impl Drop for Page {
     fn drop(&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(self.page.get(), 0) };
     }
 }
Signature · 1 line
2.34.1
maintainer
#7 lore raw

Re: [PATCH 1/3] rust: page: replace the page pointer wrapper with Opaque

Quoted 26 lines · Fiona Behrens
Quoted 21 lines · Abdiel Janulgue

Replace NonNull with Opaque to make it possible to cast to a Page pointer from a raw struct page pointer.

---
 rust/kernel/page.rs | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
rust/kernel/page.rs +2 -1
index 208a006d587c..08ff09a25223 100644
@@ -8,8 +8,9 @@
     error::code::*,
     error::Result,
     uaccess::UserSliceReader,
+    types::Opaque,
 };
-use core::ptr::{self, NonNull};
+use core::ptr::{self};
 
Quoted 12 lines

rust/kernel/page.rs line 31

Quoted 10 lines
rust/kernel/page.rs +2 -1
 /// A bitwise shift for the page size.
 pub const PAGE_SHIFT: usize = bindings::PAGE_SHIFT as usize;
@@ -25,8 +26,9 @@
 /// # Invariants
 ///
 /// The pointer is valid, and has ownership over the page.
+#[repr(transparent)]
 pub struct Page {
-    page: NonNull<bindings::page>,
+    page: Opaque<bindings::page>,

Still not to sure where to encode pinning in the type api. Looking into the C struct I see a union that sometimes holds a list head, should this then be a pinned thing in this type?

As long as you only hand out immutable references to it, nothing further is necessary.

Alice

#8 lore raw

PATCH 2/3 rust: page: Extend support to vmalloc_to_page

In reply to Abdiel Janulgue

Extend Page to support pages that are not allocated by the constructor, for example, those returned by vmalloc_to_page(). Since we don't own those pages we shouldn't Drop them either. Hence we take advantage of the switch to Opaque so we can cast to a Page pointer from a struct page pointer and be able to retrieve the reference on an existing struct page mapping. In this case Drop will not be called since we are not instantiating a new Page instance.

Note that we maintain ownership semantics for self-allocated pages (e.g. Drop still works as usual in this case).

---
 rust/kernel/page.rs | 32 ++++++++++++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)
rust/kernel/page.rs +30 -2
index 08ff09a25223..a84f382daf5c 100644
@@ -21,11 +21,12 @@
 /// A bitmask that gives the page containing a given address.
 pub const PAGE_MASK: usize = !(PAGE_SIZE - 1);
 
-/// A pointer to a page that owns the page allocation.
+/// A pointer to a page that may own the page allocation.
 ///
 /// # Invariants
 ///
-/// The pointer is valid, and has ownership over the page.
+/// The pointer is valid, and has ownership over the page if the page is allocated by this
+/// abstraction.
 #[repr(transparent)]
 pub struct Page {
     page: Opaque<bindings::page>,
@@ -78,6 +79,33 @@ pub fn alloc_page(flags: Flags) -> Result<Self, AllocError> {
         Ok(unsafe { ptr::read(ptr) })
     }
 
+    /// This is just a wrapper to vmalloc_to_page which returns an existing page mapping, hence
+    /// we don't take ownership of the page. Returns an error if the pointer is null or if it
+    /// is not returned by vmalloc().
+    pub fn vmalloc_to_page<'a>(
+        cpu_addr: *const core::ffi::c_void
+    ) -> Result<&'a Self, AllocError>
+    {
+        if cpu_addr.is_null() {
+            return Err(AllocError);
+        }
+        // SAFETY: We've checked that the pointer is not null, so it is safe to call this method.
+        if unsafe { !bindings::is_vmalloc_addr(cpu_addr) } {
+            return Err(AllocError);
+        }
+        // SAFETY: We've initially ensured the pointer argument to this function is not null and
+        // checked for the requirement that the buffer passed to it should be allocated by vmalloc,
+        // so it is safe to call this method.
+        let page = unsafe { bindings::vmalloc_to_page(cpu_addr) };
+        if page.is_null() {
+            return Err(AllocError);
+        }
+        // CAST: `Self` is a `repr(transparent)` wrapper around `bindings::page`.
+        // SAFETY: We just successfully allocated a page, therefore dereferencing
+        // the page pointer is valid.
+        Ok(unsafe { &*page.cast() })
+    }
+
     /// Returns a raw pointer to the page.
     pub fn as_ptr(&self) -> *mut bindings::page {
         self.page.get()
Signature · 1 line
2.34.1
#9 lore raw

PATCH 3/3 rust: page: Add page_slice_to_page

In reply to Abdiel Janulgue

Make it convenient to convert buffers into page-sized vmalloc'd chunks which can then be used in vmalloc_to_page().

---
 rust/kernel/page.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 58 insertions(+), 1 deletion(-)
rust/kernel/page.rs +58 -1
index a84f382daf5c..490685144694 100644
@@ -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.
Signature · 1 line
2.34.1
#10 lore raw

Re: [PATCH 3/3] rust: page: Add page_slice_to_page

kernel test robot noticed the following build errors:

Hi Abdiel,

kernel test robot noticed the following build errors:

[auto build test ERROR on rust/rust-next]
[also build test ERROR on linus/master v6.12-rc2 next-20241011] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Abdiel-Janulgue/rust-page-Extend-support-to-vmalloc_to_page/20241008-052805
base:   https://github.com/Rust-for-Linux/linux rust-next
patch link:    https://lore.kernel.org/r/20241007202752.3096472-4-abdiel.janulgue%40gmail.com
patch subject: [PATCH 3/3] rust: page: Add page_slice_to_page
config: x86_64-randconfig-014-20241011 (https://download.01.org/0day-ci/archive/20241011/[email protected]/config)
compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241011/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> error[E0432]: unresolved import `crate::alloc::VVec`

--> rust/kernel/page.rs:6:32
|
6 | alloc::{AllocError, Flags, VVec, flags::*},
| ^^^^ no VVec in alloc

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#11 lore raw
In reply to Abdiel Janulgue
Quoted 2 lines · Abdiel Janulgue

base-commit: 673d1648244c3840043e09a784164b38c2e2efb9

Quick note: this is a rust-next commit, but this looks like it is meant to be on top of Danilo's Allocator series -- e.g. we don't have e.g. VVec there yet so it cannot build (but we will have that soon).

By the way, cover letters normally go with [PATCH 0/...]. Since you have the base commit, I guess you used --cover-letter but changed the title maybe?

Thanks!

Cheers,
Miguel

#12 lore raw
Quoted 5 lines · Miguel Ojeda
Quoted 2 lines · Abdiel Janulgue

base-commit: 673d1648244c3840043e09a784164b38c2e2efb9

Quoted 9 lines

Quick note: this is a rust-next commit, but this looks like it is meant to be on top of Danilo's Allocator series -- e.g. we don't have e.g. VVec there yet so it cannot build (but we will have that soon).

By the way, cover letters normally go with [PATCH 0/...]. Since you have the base commit, I guess you used --cover-letter but changed the title maybe?

Sorry about that, my bad, I edited the cover by mistake. Will fix this in next update.

Abdiel

Quoted 4 lines

Thanks!

Cheers,
Miguel