Re: [PATCH v2 -tip] x86/percpu: Use C for arch_raw_cpu_ptr()

From: Uros Bizjak
Date: Mon Oct 16 2023 - 14:53:04 EST


On Thu, Oct 12, 2023 at 5:19 PM Nadav Amit <namit@xxxxxxxxxx> wrote:
>
>
> > On Oct 12, 2023, at 12:54 AM, Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
> >
> > !! External Email
> >
> > On Wed, 11 Oct 2023 at 14:33, Uros Bizjak <ubizjak@xxxxxxxxx> 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@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/
> [3] https://lore.kernel.org/all/20190823224424.15296-5-namit@xxxxxxxxxx/
>
> --
>
> 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:

Unfortunately, it does not work and dies early in the boot with:

[ 4.939358] BUG: kernel NULL pointer dereference, address: 0000000000000000
[ 4.940090] #PF: supervisor write access in kernel mode
[ 4.940090] #PF: error_code(0x0002) - not-present page
[ 4.940090] PGD 0 P4D 0
[ 4.940090] Oops: 0002 [#1] PREEMPT SMP PTI
[ 4.940090] CPU: 1 PID: 52 Comm: kworker/u4:1 Not tainted
6.6.0-rc6-00365-g0c09c1d70838-dirty #7
[ 4.940090] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996),
BIOS 1.16.2-1.fc37 04/01/2014
[ 4.940090] RIP: 0010:begin_new_exec+0x8f2/0xa30
[ 4.940090] Code: 31 f6 e8 c1 49 f9 ff e9 3c fa ff ff 31 f6 4c 89
ef e8 b2 4a f9 ff e9 19 fa ff ff 31 f6 4c 89 ef e8 23 4a f9 ff e9 ea
fa ff ff <f0> 41 ff 0c 24 0f
85 55 fb ff ff 4c 89 e7 e8 4b 02 df ff e9 48 fb
[ 4.940090] RSP: 0000:ffff9c84c01e3d68 EFLAGS: 00010246
[ 4.940090] RAX: 0000000000000000 RBX: ffff9946e30c1f00 RCX: 0000000000000000
[ 4.940090] RDX: 0000000000000000 RSI: ffff9946e2ff0000 RDI: ffff9946e30c2718
[ 4.940090] RBP: ffff9946c03a7c00 R08: 00000000fffffffe R09: 00000000ffffffff
[ 4.940090] R10: 000001ffffffffff R11: 0000000000000001 R12: 0000000000000000
[ 4.940090] R13: 0000000000000000 R14: ffff9946e30c2718 R15: ffff9946e2ff0000
[ 4.940090] FS: 0000000000000000(0000) GS:ffff994763f00000(0000)
knlGS:0000000000000000
[ 4.940090] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 4.940090] CR2: 0000000000000000 CR3: 00000001003a8000 CR4: 00000000000406f0
[ 4.940090] Call Trace:
[ 4.940090] <TASK>
[ 4.940090] ? __die+0x1e/0x60
[ 4.940090] ? page_fault_oops+0x17b/0x470
[ 4.940090] ? search_module_extables+0x14/0x50
[ 4.940090] ? exc_page_fault+0x66/0x140
[ 4.940090] ? asm_exc_page_fault+0x26/0x30
[ 4.940090] ? begin_new_exec+0x8f2/0xa30
[ 4.940090] ? begin_new_exec+0x3ce/0xa30
[ 4.940090] ? load_elf_phdrs+0x67/0xb0
[ 4.940090] load_elf_binary+0x2bb/0x1770
[ 4.940090] ? __kernel_read+0x136/0x2d0
[ 4.940090] bprm_execve+0x277/0x630
[ 4.940090] kernel_execve+0x145/0x1a0
[ 4.940090] call_usermodehelper_exec_async+0xcb/0x180
[ 4.940090] ? __pfx_call_usermodehelper_exec_async+0x10/0x10
[ 4.940090] ret_from_fork+0x2f/0x50
[ 4.940090] ? __pfx_call_usermodehelper_exec_async+0x10/0x10
[ 4.940090] ret_from_fork_asm+0x1b/0x30
[ 4.940090] </TASK>
[ 4.940090] Modules linked in:
[ 4.940090] CR2: 0000000000000000
[ 5.017606] ---[ end trace 0000000000000000 ]---
[ 5.018957] RIP: 0010:begin_new_exec+0x8f2/0xa30
[ 5.020299] Code: 31 f6 e8 c1 49 f9 ff e9 3c fa ff ff 31 f6 4c 89
ef e8 b2 4a f9 ff e9 19 fa ff ff 31 f6 4c 89 ef e8 23 4a f9 ff e9 ea
fa ff ff <f0> 41 ff 0c 24 0f 85 55 fb ff ff 4c 89 e7 e8 4b 02 df ff e9
48 fb
[ 5.024765] RSP: 0000:ffff9c84c01e3d68 EFLAGS: 00010246
[ 5.026150] RAX: 0000000000000000 RBX: ffff9946e30c1f00 RCX: 0000000000000000
[ 5.027916] RDX: 0000000000000000 RSI: ffff9946e2ff0000 RDI: ffff9946e30c2718
[ 5.029714] RBP: ffff9946c03a7c00 R08: 00000000fffffffe R09: 00000000ffffffff
[ 5.031461] R10: 000001ffffffffff R11: 0000000000000001 R12: 0000000000000000
[ 5.033186] R13: 0000000000000000 R14: ffff9946e30c2718 R15: ffff9946e2ff0000
[ 5.034908] FS: 0000000000000000(0000) GS:ffff994763f00000(0000)
knlGS:0000000000000000
[ 5.036907] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 5.038341] CR2: 0000000000000000 CR3: 00000001003a8000 CR4: 00000000000406f0
[ 5.040044] Kernel panic - not syncing: Fatal exception
[ 5.040647] Kernel Offset: 0x22e00000 from 0xffffffff81000000
(relocation range: 0xffffffff80000000-0xffffffffbfffffff)

It looks that aliasing a structure from another namespace is a no-go,
since the patch (attached, slightly changed your patch) without
__percpu_seg_override decorations bootstraps OK. The working patch
(without __percpu_seg_override) is not effective (no effect in
syscall_exit_work) and increases the number of current_task reads from
3841 to 4711.

Uros.
diff --git a/arch/x86/include/asm/current.h b/arch/x86/include/asm/current.h
index a1168e7b69e5..5b46505ff99e 100644
--- a/arch/x86/include/asm/current.h
+++ b/arch/x86/include/asm/current.h
@@ -36,10 +36,23 @@ static_assert(sizeof(struct pcpu_hot) == 64);

DECLARE_PER_CPU_ALIGNED(struct pcpu_hot, pcpu_hot);

+/*
+ *
+ */
+DECLARE_PER_CPU_ALIGNED(const struct pcpu_hot __percpu_seg_override,
+ const_pcpu_hot);
+
+#ifdef CONFIG_USE_X86_SEG_SUPPORT
+static __always_inline struct task_struct *get_current(void)
+{
+ return this_cpu_read_const(const_pcpu_hot.current_task);
+}
+#else
static __always_inline struct task_struct *get_current(void)
{
return this_cpu_read_stable(pcpu_hot.current_task);
}
+#endif

#define current get_current()

diff --git a/arch/x86/include/asm/percpu.h b/arch/x86/include/asm/percpu.h
index 54746903b8c3..268d1ceaa4b2 100644
--- a/arch/x86/include/asm/percpu.h
+++ b/arch/x86/include/asm/percpu.h
@@ -453,6 +453,8 @@ do { \
#define this_cpu_write_8(pcp, val) __raw_cpu_write(volatile, pcp, val)
#endif

+#define this_cpu_read_const(pcp) __raw_cpu_read(, pcp)
+
#else /* CONFIG_USE_X86_SEG_SUPPORT */

#define raw_cpu_read_1(pcp) percpu_from_op(1, , "mov", pcp)
diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index b14fc8c1c953..f284c08aaeca 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -2050,6 +2050,10 @@ DEFINE_PER_CPU_ALIGNED(struct pcpu_hot, pcpu_hot) = {
};
EXPORT_PER_CPU_SYMBOL(pcpu_hot);

+DECLARE_PER_CPU_ALIGNED(const struct pcpu_hot __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..bf9815eaf4aa 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"))