> On Mar 30, 2025, at 1:14 AM, Beesdeckar <beesdec...@softinengines.com> wrote:
> 
> Hello,
> I found that global variable of type kmutex_t is on many places defined like:
> kmutex_t variable_name __cacheline_aligned;
> 
> Two questions,
> 1. Is __cacheline_aligned mandatory for global/static variables of type 
> kmutex in kernel space ?

No, it is not mandatory.  Explainer coming below.

> 2. Regarding to alignment what are requirements for kmutex placed in kernel 
> dynamically allocated memory ?

Again, not mandatory.

Explainer:

Aligning a kutex_t to a cacheline is a performance optimization, designed to 
avoid false sharing.  False sharing is the situation where more than one CPU 
core is accessing a given cache line and at least one of those CPU accesses is 
a write, which results in large amounts of bus activity and stalling due to the 
cache coherency logic having to coordinate access to the falsely-shared cache 
line.

A mutex, by definition, is something that is always accessed using writes 
(hand-waving some details and corner cases here). If your mutex isn’t going to 
be acquired very often, it doesn’t matter as much.  If your mutex is “hot”, 
then it can matter quite a bit more.  By aligning the mutex to a cache line, 
you are reducing the chances of it sharing a cache footprint with something 
else that is also accessed frequently which could result in poor performance 
due to false sharing.

Related topic: this is also what the __read_mostly attribute is all about.  If 
the access pattern for a global variable is that it’s “mostly read, modified 
seldomly”, then gathering up as many of these globals and putting them closely 
together in memory can increase the efficiency of the data cache due to “the 
good kind” of cache line sharing behavior.

-- thorpej

Reply via email to