Re: "struct perf_sample_data" alignment

From: Peter Zijlstra
Date: Fri Mar 05 2021 - 03:37:39 EST


On Thu, Mar 04, 2021 at 07:45:44PM -0800, Linus Torvalds wrote:
> That ____cacheline_aligned goes back many years, this is not new, it
> seems to come from back in 2014: commit 2565711fb7d7 ("perf: Improve
> the perf_sample_data struct layout").

long time ago...

> But it really seems entirely and utterly bogus. That cacheline
> alignment makes things *worse*, when the variables are on the local
> stack. The local stack is already going to be dirty and in the cache,
> and aligning those things isn't going to - and I quote from the code
> in that commend in that commit - "minimize the cachelines touched".
>
> Quite the reverse. It's just going to make the stack frame use *more*
> memory, and make any cacheline usage _worse_.

IIRC there is more history here, but I can't seem to find references
just now.

What I remember is that since perf_sample_data is fairly large,
unconditionally initializing the whole thing is *slow* (and
-fauto-var-init=zero will hurt here).

So at some point I removed that full initialization and made sure we
only unconditionally touched the first few variables, which gave a
measurable speedup.

Then things got messy again and the commit 2565711fb7d7 referenced above
was cleanup, to get back to that initial state.

Now, you're right that __cacheline_aligned on on-stack (and this is
indeed mostly on-stack) is fairly tedious (there were a few patches
recently to reduce the amount of on-stack instances).

I'll put it on the todo list, along with that hotplug stuff (which I
tried to fix but ended up with an even bigger mess). I suppose we can
try and not have the alignment for the on-stack instances while
preserving it for the few off-stack ones.

Also; we're running on the NMI stack, and that's not typically hot.