* [PATCH v1 0/3] rust: make various `alloc` functions `const fn`
@ 2025-07-20 9:48 Onur Özkan
2025-07-20 9:48 ` [PATCH v1 1/3] rust: make `allocator::aligned_size` a " Onur Özkan
` (5 more replies)
0 siblings, 6 replies; 12+ messages in thread
From: Onur Özkan @ 2025-07-20 9:48 UTC (permalink / raw)
To: rust-for-linux
Cc: dakr, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
a.hindborg, aliceryhl, tmgross, linux-kernel, 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`
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
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v1 1/3] rust: make `allocator::aligned_size` a `const fn`
2025-07-20 9:48 [PATCH v1 0/3] rust: make various `alloc` functions `const fn` Onur Özkan
@ 2025-07-20 9:48 ` Onur Özkan
2025-08-17 16:50 ` Miguel Ojeda
2025-07-20 9:48 ` [PATCH v1 2/3] rust: make `ArrayLayout::new_unchecked` " Onur Özkan
` (4 subsequent siblings)
5 siblings, 1 reply; 12+ messages in thread
From: Onur Özkan @ 2025-07-20 9:48 UTC (permalink / raw)
To: rust-for-linux
Cc: dakr, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
a.hindborg, aliceryhl, tmgross, linux-kernel, Onur Özkan
Makes `allocator::aligned_size` a `const fn` to allow
compile-time evaluation.
Signed-off-by: Onur Özkan <work@onurozkan.dev>
---
rust/kernel/alloc/allocator.rs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs
index aa2dfa9dca4c..aa2957fd585b 100644
--- a/rust/kernel/alloc/allocator.rs
+++ b/rust/kernel/alloc/allocator.rs
@@ -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
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH v1 2/3] rust: make `ArrayLayout::new_unchecked` a `const fn`
2025-07-20 9:48 [PATCH v1 0/3] rust: make various `alloc` functions `const fn` Onur Özkan
2025-07-20 9:48 ` [PATCH v1 1/3] rust: make `allocator::aligned_size` a " Onur Özkan
@ 2025-07-20 9:48 ` Onur Özkan
2025-07-20 9:48 ` [PATCH v1 3/3] rust: make `kvec::Vec` functions " Onur Özkan
` (3 subsequent siblings)
5 siblings, 0 replies; 12+ messages in thread
From: Onur Özkan @ 2025-07-20 9:48 UTC (permalink / raw)
To: rust-for-linux
Cc: dakr, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
a.hindborg, aliceryhl, tmgross, linux-kernel, Onur Özkan
Makes `ArrayLayout::new_unchecked` a `const fn` to allow
compile-time evaluation.
Signed-off-by: Onur Özkan <work@onurozkan.dev>
---
rust/kernel/alloc/layout.rs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/rust/kernel/alloc/layout.rs b/rust/kernel/alloc/layout.rs
index 93ed514f7cc7..52cbf61c4539 100644
--- a/rust/kernel/alloc/layout.rs
+++ b/rust/kernel/alloc/layout.rs
@@ -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
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH v1 3/3] rust: make `kvec::Vec` functions `const fn`
2025-07-20 9:48 [PATCH v1 0/3] rust: make various `alloc` functions `const fn` Onur Özkan
2025-07-20 9:48 ` [PATCH v1 1/3] rust: make `allocator::aligned_size` a " Onur Özkan
2025-07-20 9:48 ` [PATCH v1 2/3] rust: make `ArrayLayout::new_unchecked` " Onur Özkan
@ 2025-07-20 9:48 ` Onur Özkan
2025-07-20 14:43 ` [PATCH v1 0/3] rust: make various `alloc` " Benno Lossin
` (2 subsequent siblings)
5 siblings, 0 replies; 12+ messages in thread
From: Onur Özkan @ 2025-07-20 9:48 UTC (permalink / raw)
To: rust-for-linux
Cc: dakr, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
a.hindborg, aliceryhl, tmgross, linux-kernel, Onur Özkan
Makes various `kvec::Vec` functions `const fn`
to allow compile-time evaluation.
Signed-off-by: Onur Özkan <work@onurozkan.dev>
---
rust/kernel/alloc/kvec.rs | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
index 1a0dd852a468..af6e2564a194 100644
--- a/rust/kernel/alloc/kvec.rs
+++ b/rust/kernel/alloc/kvec.rs
@@ -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
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH v1 0/3] rust: make various `alloc` functions `const fn`
2025-07-20 9:48 [PATCH v1 0/3] rust: make various `alloc` functions `const fn` Onur Özkan
` (2 preceding siblings ...)
2025-07-20 9:48 ` [PATCH v1 3/3] rust: make `kvec::Vec` functions " Onur Özkan
@ 2025-07-20 14:43 ` Benno Lossin
2025-07-20 15:17 ` Onur Özkan
2025-07-21 11:31 ` Alice Ryhl
2025-08-15 18:01 ` Danilo Krummrich
5 siblings, 1 reply; 12+ messages in thread
From: Benno Lossin @ 2025-07-20 14:43 UTC (permalink / raw)
To: Onur Özkan, rust-for-linux
Cc: dakr, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, a.hindborg,
aliceryhl, tmgross, linux-kernel
On Sun Jul 20, 2025 at 11:48 AM CEST, Onur Özkan wrote:
> 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(-)
This looks sensible, any particular reason for why you need them const?
For the entire series:
Reviewed-by: Benno Lossin <lossin@kernel.org>
---
Cheers,
Benno
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v1 0/3] rust: make various `alloc` functions `const fn`
2025-07-20 14:43 ` [PATCH v1 0/3] rust: make various `alloc` " Benno Lossin
@ 2025-07-20 15:17 ` Onur Özkan
2025-07-20 15:42 ` Miguel Ojeda
0 siblings, 1 reply; 12+ messages in thread
From: Onur Özkan @ 2025-07-20 15:17 UTC (permalink / raw)
To: Benno Lossin
Cc: rust-for-linux, dakr, ojeda, alex.gaynor, boqun.feng, gary,
bjorn3_gh, a.hindborg, aliceryhl, tmgross, linux-kernel
On Sun, 20 Jul 2025 16:43:49 +0200
"Benno Lossin" <lossin@kernel.org> wrote:
> On Sun Jul 20, 2025 at 11:48 AM CEST, Onur Özkan wrote:
> > 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(-)
>
> This looks sensible, any particular reason for why you need them
> const?
>
> For the entire series:
>
> Reviewed-by: Benno Lossin <lossin@kernel.org>
>
> ---
> 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
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v1 0/3] rust: make various `alloc` functions `const fn`
2025-07-20 15:17 ` Onur Özkan
@ 2025-07-20 15:42 ` Miguel Ojeda
2025-07-21 6:13 ` Onur Özkan
0 siblings, 1 reply; 12+ messages in thread
From: Miguel Ojeda @ 2025-07-20 15:42 UTC (permalink / raw)
To: Onur Özkan
Cc: Benno Lossin, rust-for-linux, dakr, ojeda, alex.gaynor,
boqun.feng, gary, bjorn3_gh, a.hindborg, aliceryhl, tmgross,
linux-kernel
On Sun, Jul 20, 2025 at 5:17 PM Onur Özkan <work@onurozkan.dev> wrote:
>
> 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
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v1 0/3] rust: make various `alloc` functions `const fn`
2025-07-20 15:42 ` Miguel Ojeda
@ 2025-07-21 6:13 ` Onur Özkan
2025-07-21 20:13 ` Miguel Ojeda
0 siblings, 1 reply; 12+ messages in thread
From: Onur Özkan @ 2025-07-21 6:13 UTC (permalink / raw)
To: Miguel Ojeda
Cc: Benno Lossin, rust-for-linux, dakr, ojeda, alex.gaynor,
boqun.feng, gary, bjorn3_gh, a.hindborg, aliceryhl, tmgross,
linux-kernel
On Sun, 20 Jul 2025 17:42:43 +0200
Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> wrote:
> On Sun, Jul 20, 2025 at 5:17 PM Onur Özkan <work@onurozkan.dev> wrote:
> >
> > 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?
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
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v1 0/3] rust: make various `alloc` functions `const fn`
2025-07-20 9:48 [PATCH v1 0/3] rust: make various `alloc` functions `const fn` Onur Özkan
` (3 preceding siblings ...)
2025-07-20 14:43 ` [PATCH v1 0/3] rust: make various `alloc` " Benno Lossin
@ 2025-07-21 11:31 ` Alice Ryhl
2025-08-15 18:01 ` Danilo Krummrich
5 siblings, 0 replies; 12+ messages in thread
From: Alice Ryhl @ 2025-07-21 11:31 UTC (permalink / raw)
To: Onur Özkan
Cc: rust-for-linux, dakr, ojeda, alex.gaynor, boqun.feng, gary,
bjorn3_gh, lossin, a.hindborg, tmgross, linux-kernel
On Sun, Jul 20, 2025 at 12:48:35PM +0300, Onur Özkan wrote:
> 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
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v1 0/3] rust: make various `alloc` functions `const fn`
2025-07-21 6:13 ` Onur Özkan
@ 2025-07-21 20:13 ` Miguel Ojeda
0 siblings, 0 replies; 12+ messages in thread
From: Miguel Ojeda @ 2025-07-21 20:13 UTC (permalink / raw)
To: Onur Özkan, dakr
Cc: Benno Lossin, rust-for-linux, ojeda, alex.gaynor, boqun.feng,
gary, bjorn3_gh, a.hindborg, aliceryhl, tmgross, linux-kernel
On Mon, Jul 21, 2025 at 8:14 AM Onur Özkan <work@onurozkan.dev> wrote:
>
> 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
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v1 0/3] rust: make various `alloc` functions `const fn`
2025-07-20 9:48 [PATCH v1 0/3] rust: make various `alloc` functions `const fn` Onur Özkan
` (4 preceding siblings ...)
2025-07-21 11:31 ` Alice Ryhl
@ 2025-08-15 18:01 ` Danilo Krummrich
5 siblings, 0 replies; 12+ messages in thread
From: Danilo Krummrich @ 2025-08-15 18:01 UTC (permalink / raw)
To: Onur Özkan
Cc: rust-for-linux, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh,
lossin, a.hindborg, aliceryhl, tmgross, linux-kernel
On 7/20/25 11:48 AM, Onur Özkan wrote:
> rust: make `allocator::aligned_size` a `const fn`
I dropped this patch, since it would conflict with [1].
> rust: make `ArrayLayout::new_unchecked` a `const fn`
> rust: make `kvec::Vec` functions `const fn`
Applied to alloc-next, thanks!
[1] https://lore.kernel.org/all/20250731154919.4132-2-dakr@kernel.org/
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v1 1/3] rust: make `allocator::aligned_size` a `const fn`
2025-07-20 9:48 ` [PATCH v1 1/3] rust: make `allocator::aligned_size` a " Onur Özkan
@ 2025-08-17 16:50 ` Miguel Ojeda
0 siblings, 0 replies; 12+ messages in thread
From: Miguel Ojeda @ 2025-08-17 16:50 UTC (permalink / raw)
To: Onur Özkan
Cc: rust-for-linux, dakr, ojeda, alex.gaynor, boqun.feng, gary,
bjorn3_gh, lossin, a.hindborg, aliceryhl, tmgross, linux-kernel
On Sun, Jul 20, 2025 at 11:49 AM Onur Özkan <work@onurozkan.dev> wrote:
>
> Makes `allocator::aligned_size` a `const fn` to allow
> compile-time evaluation.
>
> Signed-off-by: Onur Özkan <work@onurozkan.dev>
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
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2025-08-17 16:51 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-20 9:48 [PATCH v1 0/3] rust: make various `alloc` functions `const fn` Onur Özkan
2025-07-20 9:48 ` [PATCH v1 1/3] rust: make `allocator::aligned_size` a " Onur Özkan
2025-08-17 16:50 ` Miguel Ojeda
2025-07-20 9:48 ` [PATCH v1 2/3] rust: make `ArrayLayout::new_unchecked` " Onur Özkan
2025-07-20 9:48 ` [PATCH v1 3/3] rust: make `kvec::Vec` functions " Onur Özkan
2025-07-20 14:43 ` [PATCH v1 0/3] rust: make various `alloc` " Benno Lossin
2025-07-20 15:17 ` Onur Özkan
2025-07-20 15:42 ` Miguel Ojeda
2025-07-21 6:13 ` Onur Özkan
2025-07-21 20:13 ` Miguel Ojeda
2025-07-21 11:31 ` Alice Ryhl
2025-08-15 18:01 ` Danilo Krummrich
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).