]> git.ipfire.org Git - thirdparty/kernel/linux.git/commit
rust: sizes: add SizeConstants trait for device address space constants
authorJohn Hubbard <jhubbard@nvidia.com>
Sat, 4 Apr 2026 02:12:02 +0000 (19:12 -0700)
committerMiguel Ojeda <ojeda@kernel.org>
Tue, 7 Apr 2026 10:57:50 +0000 (12:57 +0200)
commitf4231eb25cd215882d4a5da9110d3772c2644b6c
tree46822b1f24fe75252bc55bc83872791694edb8dd
parent354c085299defe9e49b21af77fdc715cc969ed36
rust: sizes: add SizeConstants trait for device address space constants

The SZ_* constants are usize, matching the CPU pointer width. But
device address spaces have their own widths (32-bit MMIO windows,
64-bit GPU framebuffers, etc.), so drivers end up casting these
constants with SZ_1M as u64 or helper functions. This adds
boilerplate with no safety benefit.

Add a SizeConstants trait with associated SZ_* constants, implemented
for u32, u64, and usize. With the trait in scope, callers write
u64::SZ_1M or u32::SZ_4K to get the constant in their device's
native width. All SZ_* values fit in a u32, so every implementation
is lossless. Each impl has a const assert to catch any future
constant that would overflow.

A define_sizes! macro generates everything from a single internal
list of names. The macro takes the target types as arguments, so
adding a new target type requires changing only the call site.

Suggested-by: Danilo Krummrich <dakr@kernel.org>
Link: https://lore.kernel.org/all/DGB9G697GSWO.3VBFGU5MKFPMR@kernel.org/
Link: https://lore.kernel.org/all/DGHI8WRKBQS9.38910L6FIIZTE@kernel.org/
Reviewed-by: Eliot Courtney <ecourtney@nvidia.com>
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
Acked-by: Gary Guo <gary@garyguo.net>
Signed-off-by: John Hubbard <jhubbard@nvidia.com>
Link: https://patch.msgid.link/20260404021204.339779-2-jhubbard@nvidia.com
[ Applied the "kernel vertical" imports style. - Miguel ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
rust/kernel/sizes.rs