Skip to contentSkip to conversation

rust: make various `alloc` functions `const fn`

PATCH Applied Onur Özkan 5 participants 12 messages Jul 20, 2025 – Aug 17, 2025

Subsystems (2)
lore
Share & tools
lore

Onur Özkan

author reviewer
#0 lore raw

cover rust: make various `alloc` functions `const fn`

This patch series makes various functions in the
alloc crate const fn.

Each patch corresponds to a different module within the same alloc crate.

Onur Özkan (3):
rust: make allocator::aligned_size a const fn
rust: make ArrayLayout::new_unchecked a const fn
rust: make kvec::Vec functions const fn

 rust/kernel/alloc/allocator.rs |  2 +-
 rust/kernel/alloc/kvec.rs      | 10 +++++-----
 rust/kernel/alloc/layout.rs    |  2 +-
 3 files changed, 7 insertions(+), 7 deletions(-)

--
2.50.0

author reviewer
#1 lore raw

PATCH 1/3 rust: make `allocator::aligned_size` a `const fn`

Makes allocator::aligned_size a const fn to allow compile-time evaluation.

---
 rust/kernel/alloc/allocator.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
rust/kernel/alloc/allocator.rs +1 -1
index aa2dfa9dca4c..aa2957fd585b 100644
@@ -44,7 +44,7 @@
 pub struct KVmalloc;
 
 /// Returns a proper size to alloc a new object aligned to `new_layout`'s alignment.
-fn aligned_size(new_layout: Layout) -> usize {
+const fn aligned_size(new_layout: Layout) -> usize {
     // Customized layouts from `Layout::from_size_align()` can have size < align, so pad first.
     let layout = new_layout.pad_to_align();
 

--
2.50.0

#2 lore raw

Re: [PATCH v1 1/3] rust: make `allocator::aligned_size` a `const fn`

Quoted 5 lines · Onur Özkan

Makes allocator::aligned_size a const fn to allow compile-time evaluation.

For future reference: we wouldn't be able to enable it as-is without the unstable feature anyway (which Danilo noticed when applied his version of this) -- if it is needed in the future, the patch is at:

https://lore.kernel.org/all/CANiq72mUXy6AYkwCW_kO3ikjNBc5pLzXw0+fXFGmYum0tGmw1g@mail.gmail.com/

Cheers,
Miguel

author reviewer
#3 lore raw

PATCH 2/3 rust: make `ArrayLayout::new_unchecked` a `const fn`

In reply to Onur Özkan

Makes ArrayLayout::new_unchecked a const fn to allow compile-time evaluation.

---
 rust/kernel/alloc/layout.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
rust/kernel/alloc/layout.rs +1 -1
index 93ed514f7cc7..52cbf61c4539 100644
@@ -80,7 +80,7 @@ pub const fn new(len: usize) -> Result<Self, LayoutError> {
     /// # Safety
     ///
     /// `len` must be a value, for which `len * size_of::<T>() <= isize::MAX` is true.
-    pub unsafe fn new_unchecked(len: usize) -> Self {
+    pub const unsafe fn new_unchecked(len: usize) -> Self {
         // INVARIANT: By the safety requirements of this function
         // `len * size_of::<T>() <= isize::MAX`.
         Self {

--
2.50.0

author reviewer
#4 lore raw

PATCH 3/3 rust: make `kvec::Vec` functions `const fn`

In reply to Onur Özkan

Makes various kvec::Vec functions const fn
to allow compile-time evaluation.

---
 rust/kernel/alloc/kvec.rs | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
rust/kernel/alloc/kvec.rs +5 -5
index 1a0dd852a468..af6e2564a194 100644
@@ -174,7 +174,7 @@ const fn is_zst() -> bool {
 
     /// Returns the number of elements that can be stored within the vector without allocating
     /// additional memory.
-    pub fn capacity(&self) -> usize {
+    pub const fn capacity(&self) -> usize {
         if const { Self::is_zst() } {
             usize::MAX
         } else {
@@ -184,7 +184,7 @@ pub fn capacity(&self) -> usize {
 
     /// Returns the number of elements stored within the vector.
     #[inline]
-    pub fn len(&self) -> usize {
+    pub const fn len(&self) -> usize {
         self.len
     }
 
@@ -195,7 +195,7 @@ pub fn len(&self) -> usize {
     /// - `additional` must be less than or equal to `self.capacity - self.len`.
     /// - All elements within the interval [`self.len`,`self.len + additional`) must be initialized.
     #[inline]
-    pub unsafe fn inc_len(&mut self, additional: usize) {
+    pub const unsafe fn inc_len(&mut self, additional: usize) {
         // Guaranteed by the type invariant to never underflow.
         debug_assert!(additional <= self.capacity() - self.len());
         // INVARIANT: By the safety requirements of this method this represents the exact number of
@@ -244,7 +244,7 @@ pub fn as_mut_ptr(&mut self) -> *mut T {
     /// Returns a raw pointer to the vector's backing buffer, or, if `T` is a ZST, a dangling raw
     /// pointer.
     #[inline]
-    pub fn as_ptr(&self) -> *const T {
+    pub const fn as_ptr(&self) -> *const T {
         self.ptr.as_ptr()
     }
 
@@ -260,7 +260,7 @@ pub fn as_ptr(&self) -> *const T {
     /// assert!(!v.is_empty());
     /// ```
     #[inline]
-    pub fn is_empty(&self) -> bool {
+    pub const fn is_empty(&self) -> bool {
         self.len() == 0
     }
 

--
2.50.0

reviewer Reviewed-by on the v1 series
#5 lore raw
In reply to Onur Özkan
Quoted 11 lines · Onur Özkan

This patch series makes various functions in the
alloc crate const fn.

Each patch corresponds to a different module within the same alloc crate.

Onur Özkan (3):
rust: make allocator::aligned_size a const fn
rust: make ArrayLayout::new_unchecked a const fn
rust: make kvec::Vec functions const fn

Quoted 4 lines
 rust/kernel/alloc/allocator.rs |  2 +-
 rust/kernel/alloc/kvec.rs      | 10 +++++-----
 rust/kernel/alloc/layout.rs    |  2 +-
 3 files changed, 7 insertions(+), 7 deletions(-)

This looks sensible, any particular reason for why you need them const?

For the entire series:

---
Cheers,
Benno

author reviewer
#6 lore raw
Quoted 20 lines · On Sun, 20 Jul 2025 16:43:49 +0200 "Benno Lossin" <[email protected]> wrote:
Quoted 11 lines · Onur Özkan

This patch series makes various functions in the
alloc crate const fn.

Each patch corresponds to a different module within the same alloc crate.

Onur Özkan (3):
rust: make allocator::aligned_size a const fn
rust: make ArrayLayout::new_unchecked a const fn
rust: make kvec::Vec functions const fn

Quoted 4 lines
 rust/kernel/alloc/allocator.rs |  2 +-
 rust/kernel/alloc/kvec.rs      | 10 +++++-----
 rust/kernel/alloc/layout.rs    |  2 +-
 3 files changed, 7 insertions(+), 7 deletions(-)

This looks sensible, any particular reason for why you need them const?

Quoted 7 lines

For the entire series:

---
Cheers,
Benno

Personally, I don't have a specific reason. I thought the change is harmless and might extend functionality for other people in the future. It could also (although less likely) help the compiler optimize things further.

Regards,
Onur

#7 lore raw
Quoted 5 lines · Onur Özkan

Personally, I don't have a specific reason. I thought the change is harmless and might extend functionality for other people in the future. It could also (although less likely) help the compiler optimize things further.

I think it is OK -- even if we promise they are const and we have to remove it in the future, it is fine, since there is no stable kernel API. So that flexibility is another advantage of no promises there.

However, I am curious, in which cases it would help the compiler optimize? The compiler already has the information on whether it could actually be const and whether it can be evaluated at compile-time and so on -- do you mean it has an effect on heuristics like inlining or similar?

Thanks!

Cheers,
Miguel

author reviewer
#8 lore raw
Quoted 11 lines · On Sun, 20 Jul 2025 17:42:43 +0200 Miguel Ojeda…
Quoted 5 lines · Onur Özkan

Personally, I don't have a specific reason. I thought the change is harmless and might extend functionality for other people in the future. It could also (although less likely) help the compiler optimize things further.

I think it is OK -- even if we promise they are const and we have to remove it in the future, it is fine, since there is no stable kernel API. So that flexibility is another advantage of no promises there.

Quoted 5 lines

However, I am curious, in which cases it would help the compiler optimize? The compiler already has the information on whether it could actually be const and whether it can be evaluated at compile-time and so on -- do you mean it has an effect on heuristics like inlining or similar?

I thought it had effects similar to inlining, but after digging into the assembly output of a simple program (with and without const expressions) and reading some related discussions [1], it seems I was wrong about it, sorry.

[1]: https://users.rust-lang.org/t/the-effects-of-const-fn/48303

Regards,
Onur

#9 lore raw
Quoted 5 lines · Onur Özkan

I thought it had effects similar to inlining, but after digging into the assembly output of a simple program (with and without const expressions) and reading some related discussions [1], it seems I was wrong about it, sorry.

No worries at all, and thanks for taking a look into it!

I think Danilo will take this eventually through alloc, but it sounds good to me.

Cheers,
Miguel

maintainer Reviewed-by on the v1 series
#10 lore raw
In reply to Onur Özkan
Quoted 11 lines · Onur Özkan

This patch series makes various functions in the
alloc crate const fn.

Each patch corresponds to a different module within the same alloc crate.

Onur Özkan (3):
rust: make allocator::aligned_size a const fn
rust: make ArrayLayout::new_unchecked a const fn
rust: make kvec::Vec functions const fn

Quoted 7 lines
 rust/kernel/alloc/allocator.rs |  2 +-
 rust/kernel/alloc/kvec.rs      | 10 +++++-----
 rust/kernel/alloc/layout.rs    |  2 +-
 3 files changed, 7 insertions(+), 7 deletions(-)

--
2.50.0