From: Andreas Hindborg <a.hindborg@kernel.org>
To: "Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>
Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
Andreas Hindborg <a.hindborg@kernel.org>,
Andreas Hindborg <a.hindborg@kernel.org>
Subject: [PATCH] rust: add `CacheAligned` for easy cache line alignment of values
Date: Wed, 28 Jan 2026 15:05:20 +0100 [thread overview]
Message-ID: <20260128-cache-aligned-v1-1-a4eb7c3ffd35@kernel.org> (raw)
`CacheAligned` allows to easily align values to a 64 byte boundary.
An example use case is the kernel `struct spinlock`. This struct is 4 bytes
on x86 when lockdep is not enabled. The structure is not padded to fit a
cache line. The effect of this for `SpinLock` is that the lock variable and
the value protected by the lock might share a cache line, depending on the
alignment requirements of the protected value. Wrapping the value in
`CacheAligned` to get a `SpinLock<CacheAligned<T>>` solves this problem.
Signed-off-by: Andreas Hindborg <a.hindborg@samsung.com>
---
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
rust/kernel/cache_aligned.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++
rust/kernel/lib.rs | 2 ++
2 files changed, 61 insertions(+)
diff --git a/rust/kernel/cache_aligned.rs b/rust/kernel/cache_aligned.rs
new file mode 100644
index 0000000000000..9c33b8613c077
--- /dev/null
+++ b/rust/kernel/cache_aligned.rs
@@ -0,0 +1,59 @@
+// SPDX-License-Identifier: GPL-2.0
+
+use kernel::try_pin_init;
+use pin_init::{
+ pin_data,
+ pin_init,
+ PinInit, //
+};
+
+/// Wrapper type that alings content to a 64 byte cache line.
+#[repr(align(64))]
+#[pin_data]
+pub struct CacheAligned<T: ?Sized> {
+ #[pin]
+ value: T,
+}
+
+impl<T> CacheAligned<T> {
+ /// Creates an initializer for `CacheAligned<T>` form an initalizer for `T`
+ pub fn new(t: impl PinInit<T>) -> impl PinInit<CacheAligned<T>> {
+ pin_init!( CacheAligned {
+ value <- t
+ })
+ }
+
+ /// Creates a fallible initializer for `CacheAligned<T>` form a fallible
+ /// initalizer for `T`
+ pub fn try_new(
+ t: impl PinInit<T, crate::error::Error>,
+ ) -> impl PinInit<CacheAligned<T>, crate::error::Error> {
+ try_pin_init!( CacheAligned {
+ value <- t
+ }? crate::error::Error )
+ }
+
+ /// Get a pointer to the contained value without creating a reference.
+ ///
+ /// # Safety
+ ///
+ /// - `ptr` must be dereferenceable.
+ pub const unsafe fn raw_get(ptr: *mut Self) -> *mut T {
+ // SAFETY: by function safety requirements `ptr` is valid for read
+ unsafe { &raw mut ((*ptr).value) }
+ }
+}
+
+impl<T: ?Sized> core::ops::Deref for CacheAligned<T> {
+ type Target = T;
+
+ fn deref(&self) -> &T {
+ &self.value
+ }
+}
+
+impl<T: ?Sized> core::ops::DerefMut for CacheAligned<T> {
+ fn deref_mut(&mut self) -> &mut T {
+ &mut self.value
+ }
+}
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index f812cf1200428..af6d48b078428 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -75,6 +75,7 @@
pub mod bug;
#[doc(hidden)]
pub mod build_assert;
+mod cache_aligned;
pub mod clk;
#[cfg(CONFIG_CONFIGFS_FS)]
pub mod configfs;
@@ -156,6 +157,7 @@
#[doc(hidden)]
pub use bindings;
+pub use cache_aligned::CacheAligned;
pub use macros;
pub use uapi;
---
base-commit: 63804fed149a6750ffd28610c5c1c98cce6bd377
change-id: 20260128-cache-aligned-c4c0acf870ff
Best regards,
--
Andreas Hindborg <a.hindborg@kernel.org>
next reply other threads:[~2026-01-28 14:05 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-28 14:05 Andreas Hindborg [this message]
2026-01-28 14:25 ` [PATCH] rust: add `CacheAligned` for easy cache line alignment of values Alexandre Courbot
2026-01-28 14:41 ` Gary Guo
2026-01-28 14:45 ` Miguel Ojeda
2026-01-28 18:19 ` Andreas Hindborg
2026-01-28 14:46 ` Alice Ryhl
2026-01-28 15:05 ` Gary Guo
2026-05-18 13:41 ` Andreas Hindborg
2026-05-18 16:22 ` Gary Guo
2026-05-19 8:18 ` Andreas Hindborg
2026-05-19 10:35 ` Gary Guo
2026-05-19 11:28 ` Andreas Hindborg
2026-01-28 14:26 ` Miguel Ojeda
2026-01-28 14:34 ` Miguel Ojeda
2026-01-28 18:23 ` Andreas Hindborg
2026-01-28 18:41 ` Miguel Ojeda
2026-01-28 14:32 ` Gary Guo
2026-01-28 18:27 ` Andreas Hindborg
2026-05-19 9:16 ` Andreas Hindborg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260128-cache-aligned-v1-1-a4eb7c3ffd35@kernel.org \
--to=a.hindborg@kernel.org \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.