mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andreas Hindborg <a.hindborg@kernel.org>
To: "Gary Guo" <gary@garyguo.net>, "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
Subject: Re: [PATCH] rust: add `CacheAligned` for easy cache line alignment of values
Date: Tue, 19 May 2026 11:16:14 +0200	[thread overview]
Message-ID: <8733zn92u9.fsf@t14s.mail-host-address-is-not-set> (raw)
In-Reply-To: <DG0ANMHJMJ40.3J7GZJ5H6WRMA@garyguo.net>

"Gary Guo" <gary@garyguo.net> writes:

> On Wed Jan 28, 2026 at 2:05 PM GMT, Andreas Hindborg wrote:
>> `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.
>
> Do you mean `CacheAligned<SpinLock<T>>`?
>

I missed this in my original reply.

I mean `SpinLock<CacheAligned<T>>` as I wrote. The `SpinLock` will be
aligned to some alignment, possibly the start of a cache line. Taking
the lock involves getting exclusive ownership of the cache line and
updating the lock state. When the lock is contended, the CPU core that
takes the lock will loose exclusive ownership of the cache line
immediately after taking the lock, as other CPU cores are trying to take
the lock.

The logical thing to do after taking a lock is looking at the value
protected by the lock. As the winning CPU tries to do this, it has to
obtain ownership of the cache line again, assuming the `T` lives on the
same cache line as the lock state. If we align the `T` to a cache line,
then that line will not be contended. Only the cache line holding the
lock state will be contended. And then observing the T does not incur
the overhead obtaining a highly contended cache line.


Best regards,
Andreas Hindborg



      parent reply	other threads:[~2026-05-19  9:16 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-28 14:05 Andreas Hindborg
2026-01-28 14:25 ` 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 [this message]

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=8733zn92u9.fsf@t14s.mail-host-address-is-not-set \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®