summaryrefslogtreecommitdiff
path: root/rust/kernel/sizes.rs
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2026-11-21 17:22:21 -0800
committerLinus Torvalds <torvalds@linux-foundation.org>2026-11-21 17:22:21 -0800
commit4843f6873f7c4835e62f2c73ba0fd538e57a2f22 (patch)
treeee620501a1ffbad9a83141e3a70154e1fe641040 /rust/kernel/sizes.rs
downloadlinux-master.tar.gz
linux-master.zip
Merge tag 'sysctl-7.04-fixes-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/sysctl/sysctlgraftedHEADmaster
Pull sysctl fix from Joel Granados: "Fix sysctl jiffy / milliseconds conversion: - Writing a previously read value "X" shall keep the same "X" value - Remove truncation that was causing incorrect saturation of valid timeouts" * tag 'sysctl-7.04-fixes-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/sysctl/sysctl: sysctl: Do jiffy / ms conversion in ulong space
Diffstat (limited to 'rust/kernel/sizes.rs')
-rw-r--r--rust/kernel/sizes.rs179
1 files changed, 179 insertions, 0 deletions
diff --git a/rust/kernel/sizes.rs b/rust/kernel/sizes.rs
new file mode 100644
index 000000000..188e00c2b
--- /dev/null
+++ b/rust/kernel/sizes.rs
@@ -0,0 +1,179 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Commonly used sizes.
+//!
+//! C headers: [`include/linux/sizes.h`](srctree/include/linux/sizes.h).
+//!
+//! The top-level `SZ_*` constants are [`usize`]-typed, for use in kernel page
+//! arithmetic and similar CPU-side work.
+//!
+//! The [`SizeConstants`] trait provides the same constants as associated constants
+//! on [`u32`], [`u64`], and [`usize`], for use in device address spaces where
+//! the address width depends on the hardware. Device drivers frequently need
+//! these constants as [`u64`] (or [`u32`]) rather than [`usize`], because
+//! device address spaces are sized independently of the CPU pointer width.
+//!
+//! The trait is also implemented for [`Alignment`], providing each size as a
+//! compile-time validated alignment.
+//!
+//! [`Alignment`]: crate::ptr::Alignment
+//!
+//! # Examples
+//!
+//! ```
+//! use kernel::{
+//! page::PAGE_SIZE,
+//! sizes::{
+//! SizeConstants,
+//! SZ_1M, //
+//! }, //
+//! };
+//!
+//! // Module-level constants continue to work without a type qualifier.
+//! let num_pages_in_1m = SZ_1M / PAGE_SIZE;
+//!
+//! // Trait associated constants require a type qualifier.
+//! let heap_size = 14 * u64::SZ_1M;
+//! let small = u32::SZ_4K;
+//! ```
+
+macro_rules! define_sizes {
+ ($($type:ty),* $(,)?) => {
+ define_sizes!(@internal [$($type),*]
+ /// `0x0000_0001`.
+ SZ_1,
+ /// `0x0000_0002`.
+ SZ_2,
+ /// `0x0000_0004`.
+ SZ_4,
+ /// `0x0000_0008`.
+ SZ_8,
+ /// `0x0000_0010`.
+ SZ_16,
+ /// `0x0000_0020`.
+ SZ_32,
+ /// `0x0000_0040`.
+ SZ_64,
+ /// `0x0000_0080`.
+ SZ_128,
+ /// `0x0000_0100`.
+ SZ_256,
+ /// `0x0000_0200`.
+ SZ_512,
+ /// `0x0000_0400`.
+ SZ_1K,
+ /// `0x0000_0800`.
+ SZ_2K,
+ /// `0x0000_1000`.
+ SZ_4K,
+ /// `0x0000_2000`.
+ SZ_8K,
+ /// `0x0000_4000`.
+ SZ_16K,
+ /// `0x0000_8000`.
+ SZ_32K,
+ /// `0x0001_0000`.
+ SZ_64K,
+ /// `0x0002_0000`.
+ SZ_128K,
+ /// `0x0004_0000`.
+ SZ_256K,
+ /// `0x0008_0000`.
+ SZ_512K,
+ /// `0x0010_0000`.
+ SZ_1M,
+ /// `0x0020_0000`.
+ SZ_2M,
+ /// `0x0040_0000`.
+ SZ_4M,
+ /// `0x0080_0000`.
+ SZ_8M,
+ /// `0x0100_0000`.
+ SZ_16M,
+ /// `0x0200_0000`.
+ SZ_32M,
+ /// `0x0400_0000`.
+ SZ_64M,
+ /// `0x0800_0000`.
+ SZ_128M,
+ /// `0x1000_0000`.
+ SZ_256M,
+ /// `0x2000_0000`.
+ SZ_512M,
+ /// `0x4000_0000`.
+ SZ_1G,
+ /// `0x8000_0000`.
+ SZ_2G,
+ );
+ };
+
+ (@internal [$($type:ty),*] $($names_and_metas:tt)*) => {
+ define_sizes!(@consts_and_trait $($names_and_metas)*);
+ define_sizes!(@impls [$($type),*] $($names_and_metas)*);
+ define_sizes!(@impl_alignment $($names_and_metas)*);
+ };
+
+ (@consts_and_trait $($(#[$meta:meta])* $name:ident,)*) => {
+ $(
+ $(#[$meta])*
+ pub const $name: usize = bindings::$name as usize;
+ )*
+
+ /// Size constants for device address spaces.
+ ///
+ /// Implemented for [`u32`], [`u64`], and [`usize`] so drivers can
+ /// choose the width that matches their hardware. All `SZ_*` values fit
+ /// in a [`u32`], so all implementations are lossless.
+ ///
+ /// Also implemented for [`Alignment`], providing each size as a
+ /// compile-time validated alignment.
+ ///
+ /// [`Alignment`]: crate::ptr::Alignment
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use kernel::{
+ /// ptr::Alignment,
+ /// sizes::SizeConstants, //
+ /// };
+ ///
+ /// let gpu_heap = 14 * u64::SZ_1M;
+ /// let mmio_window = u32::SZ_16M;
+ /// let page_align = Alignment::SZ_4K;
+ /// ```
+ pub trait SizeConstants {
+ $(
+ $(#[$meta])*
+ const $name: Self;
+ )*
+ }
+ };
+
+ (@impls [] $($(#[$meta:meta])* $name:ident,)*) => {};
+
+ (@impl_alignment $($(#[$meta:meta])* $name:ident,)*) => {
+ impl SizeConstants for crate::ptr::Alignment {
+ $(
+ $(#[$meta])*
+ // A non-power-of-two constant will fail the build here if used.
+ const $name: Self = crate::ptr::Alignment::new::<{ self::$name }>();
+ )*
+ }
+ };
+
+ (@impls [$first:ty $(, $rest:ty)*] $($(#[$meta:meta])* $name:ident,)*) => {
+ impl SizeConstants for $first {
+ $(
+ const $name: Self = {
+ assert!((self::$name as u128) <= (<$first>::MAX as u128));
+ self::$name as $first
+ };
+ )*
+ }
+
+ define_sizes!(@impls [$($rest),*] $($(#[$meta])* $name,)*);
+ };
+}
+
+define_sizes!(u32, u64, usize);