Re: [PATCH v5 21/38] rust: ptr: add const_align_up() and enable inline_const feature
From: Gary Guo
Date: Wed Mar 04 2026 - 14:04:18 EST
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:
> ...
>> The implementation doesn't address any of my original comment and all my points
>> still apply.
>>
>
> OK, so that implies that you want to return an Option, I believe,
> like this?
>
> commit b41512390999f85bcb2a3809c68f392e936b09ab
> Author: John Hubbard <jhubbard@xxxxxxxxxx>
> Date: Thu Feb 19 14:44:02 2026 -0800
>
> rust: ptr: add const_align_up()
>
> Add const_align_up<ALIGN>() to kernel::ptr as the const-compatible
> equivalent of Alignable::align_up(). This uses inline_const to validate
> the alignment at compile time with a clear error message.
>
> 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..7e86429a9cb5 100644
> --- a/rust/kernel/ptr.rs
> +++ b/rust/kernel/ptr.rs
> @@ -225,3 +225,29 @@ fn align_up(self, alignment: Alignment) -> Option<Self> {
> }
>
> impl_alignable_uint!(u8, u16, u32, u64, usize);
> +
> +/// Aligns `value` up to `ALIGN` at compile time.
> +///
> +/// This is the const-compatible equivalent of [`Alignable::align_up`].
> +/// `ALIGN` must be a power of two (enforced at compile time).
> +///
> +/// Returns [`None`] on overflow.
> +///
> +/// # Examples
> +///
> +/// ```
> +/// use kernel::ptr::const_align_up;
> +/// use kernel::sizes::SZ_4K;
> +///
> +/// assert_eq!(const_align_up::<16>(0x4f), Some(0x50));
> +/// assert_eq!(const_align_up::<16>(0x40), Some(0x40));
> +/// assert_eq!(const_align_up::<SZ_4K>(1), Some(SZ_4K));
> +/// ```
> +#[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> {
...
}
Best,
Gary
>
>
> thanks,