Re: [PATCH v5 21/38] rust: ptr: add const_align_up() and enable inline_const feature

From: John Hubbard

Date: Wed Mar 04 2026 - 20:32:27 EST


On 3/4/26 5:23 PM, Alexandre Courbot wrote:
> On Thu Mar 5, 2026 at 4:14 AM JST, John Hubbard wrote:
>> On 3/4/26 11:04 AM, Gary Guo wrote:
>>> On Wed Mar 4, 2026 at 6:53 PM GMT, John Hubbard wrote:
>>>> On 3/4/26 3:18 AM, Gary Guo wrote:
>>>>> On Wed Mar 4, 2026 at 3:47 AM GMT, John Hubbard wrote:
>>>> ...
>>>> +#[inline(always)]
>>>> +pub const fn const_align_up<const ALIGN: usize>(value: usize) -> Option<usize> {
>>>> + const { assert!(ALIGN.is_power_of_two(), "ALIGN must be a power of two") };
>>>> + match value.checked_add(ALIGN - 1) {
>>>> + Some(v) => Some(v & !(ALIGN - 1)),
>>>> + None => None,
>>>> + }
>>>> +}
>>>
>>> I think your signature should probably just be
>>>
>>> pub const fn const_align_up(value: usize, align: Alignment) -> Option<usize> {
>>> ...
>>> }
>>>
>>
>> OK yes that's a bit nicer. I've done that for v6, thanks!
>
> Hold on a bit - if we are purposing this new method for use in const
> contexts, what use do we have for a `None` return value? By definition
> we would know both `value` and `align` and thus the result is
> deterministic.
>
> We do have an alignment method for non-const contexts already. Gary's
> initial comment was:
>
>> Either this function is always used in const context, in which case
>> you take `ALIGN` as normal function parameter and use `build_assert` and
>> `build_error`
>
> So why not make both arguments generic in this new method, and fail at
> build in case of overflow?

At this point, it is completely impossible to write a patch that complies
with Gary, Danilo, and Alex. It's all over the map.

Here's what is staged in v6 so far. I don't care anymore what we end up
with, but let's pick something:

Author: John Hubbard <jhubbard@xxxxxxxxxx>
Date: Thu Feb 19 14:44:02 2026 -0800

rust: ptr: add const_align_up()

Add const_align_up() to kernel::ptr as the const-compatible equivalent
of Alignable::align_up().

Suggested-by: Danilo Krummrich <dakr@xxxxxxxxxx>
Suggested-by: Gary Guo <gary@xxxxxxxxxxx>
Suggested-by: Miguel Ojeda <ojeda@xxxxxxxxxx>
Signed-off-by: John Hubbard <jhubbard@xxxxxxxxxx>

diff --git a/rust/kernel/ptr.rs b/rust/kernel/ptr.rs
index 5b6a382637fe..1e15aac4faca 100644
--- a/rust/kernel/ptr.rs
+++ b/rust/kernel/ptr.rs
@@ -225,3 +225,27 @@ fn align_up(self, alignment: Alignment) -> Option<Self> {
}

impl_alignable_uint!(u8, u16, u32, u64, usize);
+
+/// Aligns `value` up to `align`.
+///
+/// This is the const-compatible equivalent of [`Alignable::align_up`].
+///
+/// Returns [`None`] on overflow.
+///
+/// # Examples
+///
+/// ```
+/// use kernel::ptr::{const_align_up, Alignment};
+/// use kernel::sizes::SZ_4K;
+///
+/// assert_eq!(const_align_up(0x4f, Alignment::new::<16>()), Some(0x50));
+/// assert_eq!(const_align_up(0x40, Alignment::new::<16>()), Some(0x40));
+/// assert_eq!(const_align_up(1, Alignment::new::<SZ_4K>()), Some(SZ_4K));
+/// ```
+#[inline(always)]
+pub const fn const_align_up(value: usize, align: Alignment) -> Option<usize> {
+ match value.checked_add(align.as_usize() - 1) {
+ Some(v) => Some(v & align.mask()),
+ None => None,
+ }
+}



thanks,
--
John Hubbard