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

From: John Hubbard

Date: Fri Feb 20 2026 - 21:17:52 EST


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.

Add inline_const to rust_allowed_features in scripts/Makefile.build,
following the approach in [1].

[1] https://lore.kernel.org/rust-for-linux/20260206171253.2704684-2-gary@xxxxxxxxxx/

Suggested-by: Danilo Krummrich <dakr@xxxxxxxxxx>
Suggested-by: Miguel Ojeda <ojeda@xxxxxxxxxx>
Signed-off-by: John Hubbard <jhubbard@xxxxxxxxxx>
---
rust/kernel/ptr.rs | 27 +++++++++++++++++++++++++++
scripts/Makefile.build | 2 +-
2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/rust/kernel/ptr.rs b/rust/kernel/ptr.rs
index 5b6a382637fe..b3509caa5ad7 100644
--- a/rust/kernel/ptr.rs
+++ b/rust/kernel/ptr.rs
@@ -225,3 +225,30 @@ 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).
+///
+/// Panics on overflow, which becomes a compile-time error when called in a
+/// const context.
+///
+/// # Examples
+///
+/// ```
+/// use kernel::ptr::const_align_up;
+/// use kernel::sizes::SZ_4K;
+///
+/// assert_eq!(const_align_up::<16>(0x4f), 0x50);
+/// assert_eq!(const_align_up::<16>(0x40), 0x40);
+/// assert_eq!(const_align_up::<SZ_4K>(1), SZ_4K);
+/// ```
+#[inline(always)]
+pub const fn const_align_up<const ALIGN: usize>(value: usize) -> usize {
+ const { assert!(ALIGN.is_power_of_two(), "ALIGN must be a power of two") };
+ match value.checked_add(ALIGN - 1) {
+ Some(v) => v & !(ALIGN - 1),
+ None => panic!("const_align_up: overflow"),
+ }
+}
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index 32e209bc7985..a58a7d079710 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -319,7 +319,7 @@ $(obj)/%.lst: $(obj)/%.c FORCE
#
# Please see https://github.com/Rust-for-Linux/linux/issues/2 for details on
# the unstable features in use.
-rust_allowed_features := asm_const,asm_goto,arbitrary_self_types,lint_reasons,offset_of_nested,raw_ref_op,used_with_arg
+rust_allowed_features := asm_const,asm_goto,arbitrary_self_types,inline_const,lint_reasons,offset_of_nested,raw_ref_op,used_with_arg

# `--out-dir` is required to avoid temporaries being created by `rustc` in the
# current working directory, which may be not accessible in the out-of-tree
--
2.53.0