From: Peter Zijlstra <peterz@infradead.org>
To: Nick Desaulniers <ndesaulniers@google.com>
Cc: Stephane Eranian <eranian@google.com>,
linux-kernel@vger.kernel.org, tony.luck@intel.com,
reinette.chatre@intel.com, fenghua.yu@intel.com,
peternewman@google.com, james.morse@arm.com, babu.moger@amd.com,
ananth.narayan@amd.com, vschneid@redhat.com,
Nathan Chancellor <nathan@kernel.org>,
clang-built-linux <llvm@lists.linux.dev>,
Borislav Petkov <bp@alien8.de>,
Linus Torvalds <torvalds@linux-foundation.org>,
"H. Peter Anvin" <hpa@zytor.com>,
linux-toolchains@vger.kernel.org
Subject: Re: [PATCH] x86/resctrl: avoid compiler optimization in __resctrl_sched_in
Date: Tue, 7 Mar 2023 12:35:45 +0100 [thread overview]
Message-ID: <20230307113545.GB2017917@hirez.programming.kicks-ass.net> (raw)
In-Reply-To: <CAKwvOdnRvd5KK01awAyeyt5S36TPPW4_8Z6YL1r4gB-pBrHTbg@mail.gmail.com>
On Mon, Mar 06, 2023 at 04:16:52PM -0800, Nick Desaulniers wrote:
> Start of Lore thread:
> https://lore.kernel.org/lkml/20230303231133.1486085-1-eranian@google.com/
>
> On Mon, Mar 6, 2023 at 4:01 AM Peter Zijlstra <peterz@infradead.org> wrote:
> >
> > On Fri, Mar 03, 2023 at 03:11:33PM -0800, Stephane Eranian wrote:
> >
> > > The problem is located in the __resctrl_sched_in() routine which rewrites
> > > the active closid via the PQR_ASSOC register. Because this is an expensive
> > > operation, the kernel only does it when the context switch involves tasks
> > > with different CLOSID. And to check that, it needs to access the current
> > > task's closid field using current->closid. current is actually a macro
> > > that reads the per-cpu variable pcpu_hot.current_task.
> > >
> > > After an investigation by compiler experts, the problem has been tracked down
> > > to the usage of the get_current() macro in the __resctrl_sched_in() code and
> > > in particular the per-cpu macro:
> > >
> > > static __always_inline struct task_struct *get_current(void)
> > > {
> > > return this_cpu_read_stable(pcpu_hot.current_task);
> > > }
> > >
> > > And as per percpu.h:
> > >
> > > /*
> > > * this_cpu_read() makes gcc load the percpu variable every time it is
> > > * accessed while this_cpu_read_stable() allows the value to be cached.
> > > * this_cpu_read_stable() is more efficient and can be used if its value
> > > * is guaranteed to be valid across cpus. The current users include
> > > * get_current() and get_thread_info() both of which are actually
> > > * per-thread variables implemented as per-cpu variables and thus
> > > * stable for the duration of the respective task.
> > > */
> > >
> > > The _stable version of the macro allows the value to be cached, meaning it
> > > does not force a reload.
> >
> > Right, so afaict the difference between this_cpu_read() and
> > this_cpu_read_stable() is the volatile qualifier.
> >
> > this_cpu_read() is asm volatile(), while this_cpu_read_stable() and
> > raw_cpu_read() are both an unqualified asm().
> >
> > Now, afaiu we're inlining all of this into __switch_to(), which has
> > raw_cpu_write(pcpu_hot.current_task, next_p).
> >
> > And I suppose what the compiler is doing is lifting the 'current' load
> > over that store, but how is it allowed that? I thought C was supposed to
> > have PO consistency, That raw_cpu_write() should be seen as a store to
> > to pcpu_hot.current_task, why can it lift a load over the store?
> >
> > Specifically, percpu_to_op() has a "+m" output constaint while
> > percpu_stable_op() has a "p" input constraint on the same address.
>
> I definitely think the issue is specific to "p" constraints.
> https://godbolt.org/z/34YeG6WbY is the test case I reduced which I
> think demonstrates the issue.
>
> https://reviews.llvm.org/D145416
> -> click "Show Older Changes" for the ongoing discussion.
So per that summary, I'm going to nit-pick and state we very much want
CSE. CSE good. What we don't want it violating store-load ordering.
> I don't have a satisfactory answer yet, but am looking into this.
Oh, geez, what a twisty tale that... So Linus knew back in '09 that "p"
was icky, but it sorta was the only thing and it 'worked' -- until now
:/
Is there a way to explicitly order these things? barrier() obviously
isn't going to help here.
So ideally we'd get something that respects the whole store-load
ordering but still allows agressive CSE. And works for both toolchains.
Small ask, I know :-)
next parent reply other threads:[~2023-03-07 11:36 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20230303231133.1486085-1-eranian@google.com>
[not found] ` <20230306120106.GE1267364@hirez.programming.kicks-ass.net>
[not found] ` <CAKwvOdnRvd5KK01awAyeyt5S36TPPW4_8Z6YL1r4gB-pBrHTbg@mail.gmail.com>
2023-03-07 11:35 ` Peter Zijlstra [this message]
2023-03-07 17:48 ` [PATCH] x86/resctrl: avoid compiler optimization in __resctrl_sched_in Linus Torvalds
2023-03-07 18:43 ` Segher Boessenkool
2023-03-07 20:43 ` Jakub Jelinek
2023-03-07 20:54 ` Linus Torvalds
2023-03-07 21:06 ` Linus Torvalds
2023-03-07 21:35 ` Luck, Tony
2023-03-07 21:58 ` Nick Desaulniers
2023-03-08 6:13 ` Stephane Eranian
2023-03-08 23:25 ` Linus Torvalds
2023-03-08 16:02 ` Moger, Babu
2023-03-07 21:11 ` Luck, Tony
2023-03-07 21:14 ` Linus Torvalds
2023-03-07 21:23 ` Luck, Tony
2023-03-08 0:36 ` Luck, Tony
2023-03-07 21:16 ` Nick Desaulniers
2023-03-07 21:19 ` Linus Torvalds
2023-03-07 21:22 ` Nick Desaulniers
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=20230307113545.GB2017917@hirez.programming.kicks-ass.net \
--to=peterz@infradead.org \
--cc=ananth.narayan@amd.com \
--cc=babu.moger@amd.com \
--cc=bp@alien8.de \
--cc=eranian@google.com \
--cc=fenghua.yu@intel.com \
--cc=hpa@zytor.com \
--cc=james.morse@arm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-toolchains@vger.kernel.org \
--cc=llvm@lists.linux.dev \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=peternewman@google.com \
--cc=reinette.chatre@intel.com \
--cc=tony.luck@intel.com \
--cc=torvalds@linux-foundation.org \
--cc=vschneid@redhat.com \
/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;
as well as URLs for NNTP newsgroup(s).