]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - releases/6.7.12/loongarch-change-__my_cpu_offset-definition-to-avoid.patch
Linux 6.7.12
[thirdparty/kernel/stable-queue.git] / releases / 6.7.12 / loongarch-change-__my_cpu_offset-definition-to-avoid.patch
1 From 0be97594f41cf83eccc512a5a625d84178a8feeb Mon Sep 17 00:00:00 2001
2 From: Sasha Levin <sashal@kernel.org>
3 Date: Tue, 19 Mar 2024 15:50:34 +0800
4 Subject: LoongArch: Change __my_cpu_offset definition to avoid
5 mis-optimization
6
7 From: Huacai Chen <chenhuacai@loongson.cn>
8
9 [ Upstream commit c87e12e0e8c1241410e758e181ca6bf23efa5b5b ]
10
11 From GCC commit 3f13154553f8546a ("df-scan: remove ad-hoc handling of
12 global regs in asms"), global registers will no longer be forced to add
13 to the def-use chain. Then current_thread_info(), current_stack_pointer
14 and __my_cpu_offset may be lifted out of the loop because they are no
15 longer treated as "volatile variables".
16
17 This optimization is still correct for the current_thread_info() and
18 current_stack_pointer usages because they are associated to a thread.
19 However it is wrong for __my_cpu_offset because it is associated to a
20 CPU rather than a thread: if the thread migrates to a different CPU in
21 the loop, __my_cpu_offset should be changed.
22
23 Change __my_cpu_offset definition to treat it as a "volatile variable",
24 in order to avoid such a mis-optimization.
25
26 Cc: stable@vger.kernel.org
27 Reported-by: Xiaotian Wu <wuxiaotian@loongson.cn>
28 Reported-by: Miao Wang <shankerwangmiao@gmail.com>
29 Signed-off-by: Xing Li <lixing@loongson.cn>
30 Signed-off-by: Hongchen Zhang <zhanghongchen@loongson.cn>
31 Signed-off-by: Rui Wang <wangrui@loongson.cn>
32 Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
33 Signed-off-by: Sasha Levin <sashal@kernel.org>
34 ---
35 arch/loongarch/include/asm/percpu.h | 7 ++++++-
36 1 file changed, 6 insertions(+), 1 deletion(-)
37
38 diff --git a/arch/loongarch/include/asm/percpu.h b/arch/loongarch/include/asm/percpu.h
39 index 9b36ac003f890..8f290e5546cf7 100644
40 --- a/arch/loongarch/include/asm/percpu.h
41 +++ b/arch/loongarch/include/asm/percpu.h
42 @@ -29,7 +29,12 @@ static inline void set_my_cpu_offset(unsigned long off)
43 __my_cpu_offset = off;
44 csr_write64(off, PERCPU_BASE_KS);
45 }
46 -#define __my_cpu_offset __my_cpu_offset
47 +
48 +#define __my_cpu_offset \
49 +({ \
50 + __asm__ __volatile__("":"+r"(__my_cpu_offset)); \
51 + __my_cpu_offset; \
52 +})
53
54 #define PERCPU_OP(op, asm_op, c_op) \
55 static __always_inline unsigned long __percpu_##op(void *ptr, \
56 --
57 2.43.0
58