mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Stephane Eranian <eranian@google.com>
Cc: linux-kernel@vger.kernel.org, tony.luck@intel.com,
	reinette.chatre@intel.com, fenghua.yu@intel.com,
	peternewman@google.com, bp@suse.de, james.morse@arm.com,
	babu.moger@amd.com, ananth.narayan@amd.com, vschneid@redhat.com,
	Nick Desaulniers <ndesaulniers@google.com>,
	Nathan Chancellor <nathan@kernel.org>
Subject: Re: [PATCH] x86/resctrl: avoid compiler optimization in __resctrl_sched_in
Date: Mon, 6 Mar 2023 13:01:06 +0100	[thread overview]
Message-ID: <20230306120106.GE1267364@hirez.programming.kicks-ass.net> (raw)
In-Reply-To: <20230303231133.1486085-1-eranian@google.com>

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.

Compiler folks help?

  parent reply	other threads:[~2023-03-06 12:01 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-03 23:11 Stephane Eranian
2023-03-03 23:17 ` Luck, Tony
2023-03-06 12:01 ` Peter Zijlstra [this message]
2023-03-07  0:16   ` Nick Desaulniers
2023-03-07 11:35     ` Peter Zijlstra
2023-03-07 17:48       ` 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=20230306120106.GE1267364@hirez.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=ananth.narayan@amd.com \
    --cc=babu.moger@amd.com \
    --cc=bp@suse.de \
    --cc=eranian@google.com \
    --cc=fenghua.yu@intel.com \
    --cc=james.morse@arm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=peternewman@google.com \
    --cc=reinette.chatre@intel.com \
    --cc=tony.luck@intel.com \
    --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

all inboxes | Powered by JetHome®