linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Nadav Amit <namit@vmware.com>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Uros Bizjak <ubizjak@gmail.com>,
	the arch/x86 maintainers <x86@kernel.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	Andy Lutomirski <luto@kernel.org>,
	Brian Gerst <brgerst@gmail.com>,
	Denys Vlasenko <dvlasenk@redhat.com>,
	"H . Peter Anvin" <hpa@zytor.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Josh Poimboeuf <jpoimboe@redhat.com>,
	Nick Desaulniers <ndesaulniers@google.com>
Subject: Re: [PATCH v2 -tip] x86/percpu: Use C for arch_raw_cpu_ptr()
Date: Thu, 12 Oct 2023 15:19:03 +0000	[thread overview]
Message-ID: <0617BB2F-D08F-410F-A6EE-4135BB03863C@vmware.com> (raw)
In-Reply-To: <CAHk-=wj2Co_g3RQ=JkDZC7PYbRqDPq7mePQ0=eYhhtpEgqJD0w@mail.gmail.com>


> On Oct 12, 2023, at 12:54 AM, Linus Torvalds <torvalds@linux-foundation.org> wrote:
> 
> !! External Email
> 
> On Wed, 11 Oct 2023 at 14:33, Uros Bizjak <ubizjak@gmail.com> wrote:
>> 
>> Reading the above, it looks to me that we don't want to play games
>> with "const aliased" versions of current_task [1], as proposed by
>> Nadav in his patch series.
> 
> Well, maybe I'd like it if I saw what the effect of it was, but that
> patch mentions "sync_mm_rss()" which doesn't actually exist
> (SPLIT_RSS_COUNTING is never defined, the split version is gone and
> hasn't existed since commit f1a7941243c1 "mm: convert mm's rss stats
> into percpu_counter")

So I added a new version of the current aliasing (well, actually pcpu_hot
in the new version) on top of Uros’s patches, and the effect can be seen
in many functions. I don’t want to bother with many examples so here is
a common and simple one:

Currently syscall_exit_work() that starts with:

   0xffffffff8111e120 <+0>: push   %rbp
   0xffffffff8111e121 <+1>: mov    %rdi,%rbp
   0xffffffff8111e124 <+4>: push   %rbx
   0xffffffff8111e125 <+5>: mov    %rsi,%rbx
   0xffffffff8111e128 <+8>: and    $0x20,%esi
   0xffffffff8111e12b <+11>: je     0xffffffff8111e143 <syscall_exit_work+35>
   0xffffffff8111e12d <+13>: mov    %gs:0x2ac80,%rax
   0xffffffff8111e136 <+22>: cmpb   $0x0,0x800(%rax)
   0xffffffff8111e13d <+29>: jne    0xffffffff8111e22a <syscall_exit_work+266>
   0xffffffff8111e143 <+35>: mov    %gs:0x2ac80,%rax
   0xffffffff8111e14c <+44>: cmpq   $0x0,0x7c8(%rax)

Using the const-alias changes the beginning of syscall_exit_work to:

   0xffffffff8111cb80 <+0>: push   %r12
   0xffffffff8111cb82 <+2>: mov    %gs:0x7ef0e0f6(%rip),%r12        # 0x2ac80 <pcpu_hot>
   0xffffffff8111cb8a <+10>: push   %rbp
   0xffffffff8111cb8b <+11>: mov    %rdi,%rbp
   0xffffffff8111cb8e <+14>: push   %rbx
   0xffffffff8111cb8f <+15>: mov    %rsi,%rbx
   0xffffffff8111cb92 <+18>: and    $0x20,%esi
   0xffffffff8111cb95 <+21>: je     0xffffffff8111cba6 <syscall_exit_work+38>
   0xffffffff8111cb97 <+23>: cmpb   $0x0,0x800(%r12)
   0xffffffff8111cba0 <+32>: jne    0xffffffff8111cc7a <syscall_exit_work+250>
   0xffffffff8111cba6 <+38>: cmpq   $0x0,0x7c8(%r12)

So we both see RIP-relative addressing is being used (hence the instruction is
one byte shorter) and the reload going away.

Now, I am not a compiler expert as for the rationale, but it googling around
I can see Nick explaining the rationale [1] - if you use “p” your read memory.
BTW: It is related to discussion you had [2], in which you encountered an issue
I also encountered before [3]. My bad for pushing it in.

Anyhow, I created a similar code on godbolt ( https://godbolt.org/z/dPqKKzPs4 )
to show this behavior - how compiler barriers cause reload. It seems that this
behavior happens on GCC and CLANG on various versions.

The idea behind the patch is that it communicates - in the compilation unit
granularity - that current is fixed. There is an issue of whether it works with
LTO, which I have never checked.


[1] https://reviews.llvm.org/D145416
[2] https://lore.kernel.org/lkml/20230306120106.GE1267364@hirez.programming.kicks-ass.net/
[3] https://lore.kernel.org/all/20190823224424.15296-5-namit@vmware.com/

--

Here’s the updated patch - but I didn’t really boot a machine with it so new
issues might have come since my last patch-set:

-- >8 --

Date: Thu, 12 Oct 2023 06:02:03 -0700
Subject: [PATCH] Const current

---
 arch/x86/include/asm/current.h | 17 ++++++++++++++++-
 arch/x86/kernel/cpu/common.c   |  4 ++++
 include/linux/compiler.h       |  2 +-
 3 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/arch/x86/include/asm/current.h b/arch/x86/include/asm/current.h
index a1168e7b69e5..d05fbb6a8bd7 100644
--- a/arch/x86/include/asm/current.h
+++ b/arch/x86/include/asm/current.h
@@ -36,9 +36,24 @@ static_assert(sizeof(struct pcpu_hot) == 64);
 
 DECLARE_PER_CPU_ALIGNED(struct pcpu_hot, pcpu_hot);
 
+/*
+ * Hold a constant alias for current_task, which would allow to avoid caching of
+ * current task.
+ *
+ * We must mark const_current_task with the segment qualifiers, as otherwise gcc
+ * would do redundant reads of const_current_task.
+ */
+DECLARE_PER_CPU(struct pcpu_hot const __percpu_seg_override, const_pcpu_hot);
+
 static __always_inline struct task_struct *get_current(void)
 {
-	return this_cpu_read_stable(pcpu_hot.current_task);
+
+	/*
+	 * GCC is missing functionality of removing segment qualifiers, which
+	 * messes with per-cpu infrastructure that holds local copies. Use
+	 * __raw_cpu_read to avoid holding any copy.
+	 */
+	return __raw_cpu_read(, const_pcpu_hot.current_task);
 }
 
 #define current get_current()
diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 382d4e6b848d..94590af11388 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -2052,6 +2052,10 @@ DEFINE_PER_CPU_ALIGNED(struct pcpu_hot, pcpu_hot) = {
 };
 EXPORT_PER_CPU_SYMBOL(pcpu_hot);
 
+DECLARE_PER_CPU_ALIGNED(struct pcpu_hot const __percpu_seg_override, const_pcpu_hot)
+	__attribute__((alias("pcpu_hot")));
+EXPORT_PER_CPU_SYMBOL(const_pcpu_hot);
+
 #ifdef CONFIG_X86_64
 DEFINE_PER_CPU_FIRST(struct fixed_percpu_data,
 		     fixed_percpu_data) __aligned(PAGE_SIZE) __visible;
diff --git a/include/linux/compiler.h b/include/linux/compiler.h
index d7779a18b24f..e7059292085e 100644
--- a/include/linux/compiler.h
+++ b/include/linux/compiler.h
@@ -212,7 +212,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
  */
 #define ___ADDRESSABLE(sym, __attrs) \
 	static void * __used __attrs \
-		__UNIQUE_ID(__PASTE(__addressable_,sym)) = (void *)&sym;
+		__UNIQUE_ID(__PASTE(__addressable_,sym)) = (void *)(uintptr_t)&sym;
 #define __ADDRESSABLE(sym) \
 	___ADDRESSABLE(sym, __section(".discard.addressable"))
 
-- 
2.25.1

  reply	other threads:[~2023-10-12 15:19 UTC|newest]

Thread overview: 106+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-10 16:42 [PATCH v2 -tip] x86/percpu: Use C for arch_raw_cpu_ptr() Uros Bizjak
2023-10-10 17:32 ` Linus Torvalds
2023-10-10 18:22   ` Uros Bizjak
2023-10-10 18:25     ` Nadav Amit
2023-10-10 18:42       ` Linus Torvalds
2023-10-10 18:37     ` Linus Torvalds
2023-10-10 18:41       ` Uros Bizjak
2023-10-10 18:52         ` Linus Torvalds
2023-10-11  7:27           ` Uros Bizjak
2023-10-11  7:45             ` Uros Bizjak
2023-10-11 19:40               ` Linus Torvalds
2023-10-11 18:42           ` Uros Bizjak
2023-10-11 19:51             ` Linus Torvalds
2023-10-11 19:52               ` Linus Torvalds
2023-10-11 20:00               ` Uros Bizjak
2023-10-11 22:37               ` Ingo Molnar
2023-10-11 23:15                 ` H. Peter Anvin
2023-10-12  1:35                   ` Josh Poimboeuf
2023-10-12  6:19                     ` Ingo Molnar
2023-10-12 16:08                       ` Josh Poimboeuf
2023-10-12 17:59                         ` Ingo Molnar
2023-10-12 21:30                           ` Josh Poimboeuf
2023-10-13 10:52                             ` Ingo Molnar
2023-10-11  7:41       ` Nadav Amit
2023-10-11 19:37         ` Linus Torvalds
2023-10-11 21:32           ` Uros Bizjak
2023-10-11 21:54             ` Linus Torvalds
2023-10-12 15:19               ` Nadav Amit [this message]
2023-10-12 16:33                 ` Uros Bizjak
2023-10-12 16:55                   ` Uros Bizjak
2023-10-12 17:10                     ` Linus Torvalds
2023-10-12 17:47                       ` Linus Torvalds
2023-10-12 18:01                         ` Uros Bizjak
2023-10-13  9:38                           ` Uros Bizjak
2023-10-13 11:53                             ` Uros Bizjak
2023-10-13 16:38                               ` Linus Torvalds
2023-10-12 17:52                       ` Uros Bizjak
2023-11-20  9:39                       ` Use %a asm operand modifier to obtain %rip-relative addressing Uros Bizjak
2023-10-12 16:56                   ` [PATCH v2 -tip] x86/percpu: Use C for arch_raw_cpu_ptr() Linus Torvalds
2023-10-12 17:16                 ` Linus Torvalds
2023-10-12 19:32                   ` Nadav Amit
2023-10-12 19:40                     ` Linus Torvalds
2023-10-16 18:52                 ` Uros Bizjak
2023-10-16 19:24                   ` Linus Torvalds
2023-10-16 20:35                     ` Nadav Amit
2023-10-16 20:59                       ` Linus Torvalds
2023-10-16 23:02                       ` Linus Torvalds
2023-10-16 23:14                         ` Linus Torvalds
2023-10-17  7:23                         ` Nadav Amit
2023-10-17 19:00                           ` Linus Torvalds
2023-10-17 19:11                             ` Uros Bizjak
2023-10-17 21:05                               ` Uros Bizjak
2023-10-17 21:53                                 ` Linus Torvalds
2023-10-17 22:06                                   ` Nadav Amit
2023-10-17 22:29                                     ` Nadav Amit
2023-10-18  7:46                                   ` Uros Bizjak
2023-10-18  9:04                                     ` Uros Bizjak
2023-10-18 10:54                                       ` Nadav Amit
2023-10-18 12:14                                         ` Uros Bizjak
2023-10-18 13:15                                           ` Uros Bizjak
2023-10-18 14:46                                             ` Nadav Amit
2023-10-18 15:17                                               ` Uros Bizjak
2023-10-18 16:03                                                 ` Nadav Amit
2023-10-18 16:26                                                   ` Linus Torvalds
2023-10-18 17:23                                                     ` Uros Bizjak
2023-10-18 18:11                                                       ` Linus Torvalds
2023-10-18 18:08                                                     ` Uros Bizjak
2023-10-18 18:15                                                       ` Linus Torvalds
2023-10-18 18:26                                                         ` Uros Bizjak
2023-10-18 19:33                                                           ` Uros Bizjak
2023-10-18 20:17                                                             ` Nadav Amit
2023-10-18 20:22                                                             ` Linus Torvalds
2023-10-18 20:34                                                               ` Linus Torvalds
2023-10-18 20:51                                                                 ` Uros Bizjak
2023-10-18 21:09                                                                   ` Uros Bizjak
2023-10-18 21:10                                                                   ` Linus Torvalds
2023-10-18 21:40                                                                     ` Uros Bizjak
2023-10-18 22:40                                                                       ` Linus Torvalds
2023-10-18 23:06                                                                         ` Linus Torvalds
2023-10-19  7:04                                                                         ` Uros Bizjak
2023-10-19 16:59                                                                           ` Linus Torvalds
2023-10-19 17:21                                                                             ` Uros Bizjak
2023-10-19 18:06                                                                               ` Linus Torvalds
2023-10-19 18:16                                                                                 ` Uros Bizjak
2023-10-19 18:49                                                                                   ` Linus Torvalds
2023-10-19 19:07                                                                                     ` Linus Torvalds
2023-10-20  7:57                                                                                       ` Uros Bizjak
2023-10-19 21:04                                                                                   ` Linus Torvalds
2023-10-19 22:39                                                                                     ` Linus Torvalds
2023-10-20  8:08                                                                                       ` Uros Bizjak
2023-10-19  8:44                                                                         ` Peter Zijlstra
2023-10-19  8:54                                                                         ` Peter Zijlstra
2023-10-19 17:04                                                                           ` Linus Torvalds
2023-10-19 18:13                                                                             ` Peter Zijlstra
2023-10-19 18:22                                                                               ` Linus Torvalds
2023-10-19 18:37                                                                                 ` Uros Bizjak
2023-10-19  9:07                                                                         ` Peter Zijlstra
2023-10-19  9:23                                                                           ` Uros Bizjak
2023-10-18 20:42                                                               ` Uros Bizjak
2023-10-19 16:32                                                               ` Uros Bizjak
2023-10-19 17:08                                                                 ` Linus Torvalds
2023-10-18 18:29                                                       ` Nadav Amit
2023-10-18 16:12                                             ` Linus Torvalds
2023-10-18 17:07                                               ` Uros Bizjak
2023-10-18 18:01                                                 ` Linus Torvalds
2023-10-16 21:09                   ` Uros Bizjak

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=0617BB2F-D08F-410F-A6EE-4135BB03863C@vmware.com \
    --to=namit@vmware.com \
    --cc=brgerst@gmail.com \
    --cc=dvlasenk@redhat.com \
    --cc=hpa@zytor.com \
    --cc=jpoimboe@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    --cc=ubizjak@gmail.com \
    --cc=x86@kernel.org \
    /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).