FAILED: patch "[PATCH] LoongArch: vDSO: Emit GNU_EH_FRAME correctly" failed to apply to 6.12-stable tree
Subsystems (2)
[email protected]
maintainerThe patch below does not apply to the 6.12-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.12.y
git checkout FETCH_HEAD
git cherry-pick -x e4878c37f6679fdea91b27a0f4e60a871f0b7bad
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026033044-kilometer-hefty-7423@gregkh' --subject-prefix 'PATCH 6.12.y' HEAD^..
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From e4878c37f6679fdea91b27a0f4e60a871f0b7bad Mon Sep 17 00:00:00 2001
From: Xi Ruoyao <[email protected]>
Date: Thu, 26 Mar 2026 14:29:09 +0800
Subject: [PATCH] LoongArch: vDSO: Emit GNU_EH_FRAME correctly
With -fno-asynchronous-unwind-tables and --no-eh-frame-hdr (the default of the linker), the GNU_EH_FRAME segment (specified by vdso.lds.S) is empty. This is not valid, as the current DWARF specification mandates the first byte of the EH frame to be the version number 1. It causes some unwinders to complain, for example the ClickHouse query profiler spams the log with messages:
clickhouse-server[365854]: libunwind: unsupported .eh_frame_hdr
version: 127 at 7ffffffb0000
Here "127" is just the byte located at the p_vaddr (0, i.e. the beginning of the vDSO) of the empty GNU_EH_FRAME segment. Cross- checking with /proc/365854/maps has also proven 7ffffffb0000 is the start of vDSO in the process VM image.
In LoongArch the -fno-asynchronous-unwind-tables option seems just a MIPS legacy, and MIPS only uses this option to satisfy the MIPS-specific "genvdso" program, per the commit cfd75c2db17e ("MIPS: VDSO: Explicitly use -fno-asynchronous-unwind-tables"). IIRC it indicates some inherent limitation of the MIPS ELF ABI and has nothing to do with LoongArch. So we can simply flip it over to -fasynchronous-unwind-tables and pass --eh-frame-hdr for linking the vDSO, allowing the profilers to unwind the stack for statistics even if the sample point is taken when the PC is in the vDSO.
However simply adjusting the options above would exploit an issue: when the libgcc unwinder saw the invalid GNU_EH_FRAME segment, it silently falled back to a machine-specific routine to match the code pattern of rt_sigreturn() and extract the registers saved in the sigframe if the code pattern is matched. As unwinding from signal handlers is vital for libgcc to support pthread cancellation etc., the fall-back routine had been silently keeping the LoongArch Linux systems functioning since Linux 5.19. But when we start to emit GNU_EH_FRAME with the correct format, fall-back routine will no longer be used and libgcc will fail to unwind the sigframe, and unwinding from signal handlers will no longer work, causing dozens of glibc test failures. To make it possible to unwind from signal handlers again, it's necessary to code the unwind info in __vdso_rt_sigreturn via .cfi_* directives.
The offsets in the .cfi_* directives depend on the layout of struct sigframe, notably the offset of sigcontext in the sigframe. To use the offset in the assembly file, factor out struct sigframe into a header to allow asm-offsets.c to output the offset for assembly.
To work around a long-term issue in the libgcc unwinder (the pc is unconditionally substracted by 1: doing so is technically incorrect for a signal frame), a nop instruction is included with the two real instructions in __vdso_rt_sigreturn in the same FDE PC range. The same hack has been used on x86 for a long time.
arch/loongarch/include/asm/linkage.h +36 -0
@@ -41,4 +41,40 @@ .cfi_endproc; \SYM_END (name,SYM_T_NONE ) +/* +* This is for the signal handler trampoline, which is used as the return +* address of the signal handlers in userspace instead of called normally. +* The long standing libgcc bug https://gcc.gnu.org/PR124050 requires a +* nop between .cfi_startproc and the actual address of the trampoline, so +* we cannot simply use SYM_FUNC_START. +* +* This wrapper also contains all the .cfi_* directives for recovering +* the content of the GPRs and the "return address" (where the rt_sigreturn +* syscall will jump to), assuming there is a struct rt_sigframe (where +* a struct sigcontext containing those information we need to recover) at +* $sp. The "DWARF for the LoongArch(TM) Architecture" manual states +* column 0 is for $zero, but it does not make too much sense to +* save/restore the hardware zero register. Repurpose this column here +* for the return address (here it's not the content of $ra we cannot use +* the default column 3). + */ +#define SYM_SIGFUNC_START (name) \ + .cfi_startproc; \ + .cfi_signal_frame; \ + .cfi_def_cfa 3, RT_SIGFRAME_SC; \ + .cfi_return_column 0; \ + .cfi_offset 0, SC_PC; \ + \ + .irp num, 1, 2, 3, 4, 5, 6, 7, 8, \ + 9, 10, 11, 12, 13, 14, 15, 16, \ + 17, 18, 19, 20, 21, 22, 23, 24, \ + 25, 26, 27, 28, 29, 30, 31; \ + .cfi_offset \num, SC_REGS + \num * SZREG; \ + .endr; \ + \ + nop; \ + SYM_START(name, SYM_L_GLOBAL, SYM_A_ALIGN) + +#define SYM_SIGFUNC_END (name) SYM_FUNC_END(name) +#endif
arch/loongarch/include/asm/sigframe.h new file +9 -0
@@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ + +#include <asm/siginfo.h> +#include <asm/ucontext.h> + +struct rt_sigframe { +struct siginfo rs_info; +struct ucontext rs_uctx; +};
arch/loongarch/kernel/asm-offsets.c +2 -0
@@ -16,6 +16,7 @@#include <asm/ptrace.h> #include <asm/processor.h> #include <asm/ftrace.h> +#include <asm/sigframe.h> #include <vdso/datapage.h> static void __used output_ptreg_defines (void )
@@ -220,6 +221,7 @@ static void __used output_sc_defines(void)COMMENT ("Linux sigcontext offsets." );OFFSET (SC_REGS , sigcontext, sc_regs);OFFSET (SC_PC , sigcontext, sc_pc); +OFFSET (RT_SIGFRAME_SC , rt_sigframe, rs_uctx.uc_mcontext);BLANK (); }
arch/loongarch/kernel/signal.c +1 -5
@@ -35,6 +35,7 @@#include <asm/cpu-features.h> #include <asm/fpu.h> #include <asm/lbt.h> +#include <asm/sigframe.h> #include <asm/ucontext.h> #include <asm/vdso.h>
@@ -51,11 +52,6 @@#define lock_lbt_owner () ({ preempt_disable(); pagefault_disable(); })#define unlock_lbt_owner () ({ pagefault_enable(); preempt_enable(); }) -struct rt_sigframe { -struct siginfo rs_info; -struct ucontext rs_uctx; -}; -struct _ctx_layout {struct sctx_info *addr;unsigned int size;
arch/loongarch/vdso/Makefile +2 -2
@@ -26,7 +26,7 @@ cflags-vdso := $(ccflags-vdso) \ $(filter -W%,$(filter-out -Wa$(comma)%,$(KBUILD_CFLAGS))) \ -std=gnu11 -fms-extensions -O2 -g -fno-strict-aliasing -fno-common -fno-builtin \ -fno-stack-protector -fno-jump-tables -DDISABLE_BRANCH_PROFILING \ - $(call cc-option, -fno-asynchronous-unwind-tables) \ + $(call cc-option, -fasynchronous-unwind-tables) \ $(call cc-option, -fno-stack-protector) aflags-vdso := $(ccflags-vdso) \ -D__ASSEMBLY__ -Wa,-gdwarf-2
@@ -41,7 +41,7 @@ endif # VDSO linker flags. ldflags-y := -Bsymbolic --no-undefined -soname=linux-vdso.so.1 \ - $(filter -E%,$(KBUILD_CFLAGS)) -shared --build-id -T + $(filter -E%,$(KBUILD_CFLAGS)) -shared --build-id --eh-frame-hdr -T # # Shared build commands.
arch/loongarch/vdso/sigreturn.S +3 -3
@@ -12,13 +12,13 @@ #include <asm/regdef.h> #include <asm/asm.h> +#include <asm/asm-offsets.h> .section .text - .cfi_sections .debug_frame -SYM_FUNC_START(__vdso_rt_sigreturn) +SYM_SIGFUNC_START(__vdso_rt_sigreturn) li.w a7, __NR_rt_sigreturn syscall 0 -SYM_FUNC_END(__vdso_rt_sigreturn) +SYM_SIGFUNC_END(__vdso_rt_sigreturn)
Sasha Levin
maintainerPATCH 1/2 LoongArch: vDSO: Use vdso/datapage.h to access vDSO data
From: Thomas Weißschuh <[email protected]>
[ Upstream commit dc32cb4ba6f408e05857fbbf0b2965deec1d5c92 ]
vdso/datapage.h provides symbols and functions to ease the access to shared vDSO data from both the kernel and the vDSO.
Make use of it to simplify the current code and also prepare for further changes unifying the vDSO data storage between architectures.
Stable-dep-of: e4878c37f667 ("LoongArch: vDSO: Emit GNU_EH_FRAME correctly")
--- arch/loongarch/include/asm/vdso/getrandom.h | 3 +-- arch/loongarch/include/asm/vdso/gettimeofday.h | 4 ++-- arch/loongarch/include/asm/vdso/vdso.h | 18 +----------------- arch/loongarch/kernel/asm-offsets.c | 9 +++++++++ arch/loongarch/vdso/vdso.lds.S | 8 +++++++- arch/loongarch/vdso/vgetcpu.c | 2 +- 6 files changed, 21 insertions(+), 23 deletions(-)
arch/loongarch/include/asm/vdso/getrandom.h +1 -2
@@ -30,8 +30,7 @@ static __always_inline ssize_t getrandom_syscall(void *_buffer, size_t _len, uns static __always_inline const struct vdso_rng_data *__arch_get_vdso_rng_data(void) { -return (const struct vdso_rng_data *)(get_vdso_data () +VVAR_LOONGARCH_PAGES_START * -PAGE_SIZE + offsetof(struct loongarch_vdso_data , rng_data)); +return &_loongarch_data.rng_data; }#endif /* !__ASSEMBLY__ */
arch/loongarch/include/asm/vdso/gettimeofday.h +2 -2
@@ -91,14 +91,14 @@ static inline bool loongarch_vdso_hres_capable(void) static __always_inline const struct vdso_data *__arch_get_vdso_data(void) { -return (const struct vdso_data *)get_vdso_data (); +return _vdso_data; }#ifdef CONFIG_TIME_NS static __always_inline const struct vdso_data *__arch_get_timens_vdso_data(const struct vdso_data *vd) { -return (const struct vdso_data *)(get_vdso_data () +VVAR_TIMENS_PAGE_OFFSET *PAGE_SIZE ); +return _timens_data; }#endif #endif /* !__ASSEMBLY__ */
arch/loongarch/include/asm/vdso/vdso.h +1 -17
@@ -48,23 +48,7 @@ enum vvar_pages {#define VVAR_SIZE (VVAR_NR_PAGES << PAGE_SHIFT) -static inline unsigned long get_vdso_base (void ) -{ -unsigned long addr; - - __asm__( -" la.pcrel %0, _start\n" - :"=r" (addr) - : - :); - -return addr; -} - -static inline unsigned long get_vdso_data (void ) -{ -return get_vdso_base () -VVAR_SIZE ; -} +extern struct loongarch_vdso_data _loongarch_data __attribute__((visibility("hidden")));#endif /* __ASSEMBLY__ */
arch/loongarch/kernel/asm-offsets.c +9 -0
@@ -16,6 +16,7 @@#include <asm/ptrace.h> #include <asm/processor.h> #include <asm/ftrace.h> +#include <vdso/datapage.h> static void __used output_ptreg_defines (void ) {
@@ -311,3 +312,11 @@ static void __used output_kvm_defines(void)OFFSET (KVM_GPGD , kvm, arch.pgd);BLANK (); } + +static void __used output_vdso_defines (void ) +{ +COMMENT ("LoongArch vDSO offsets." ); + +DEFINE (__VVAR_PAGES,VVAR_NR_PAGES ); +BLANK (); +}
arch/loongarch/vdso/vdso.lds.S +7 -1
@@ -3,6 +3,8 @@ * Author: Huacai Chen <[email protected]> * Copyright (C) 2020-2022 Loongson Technology Corporation Limited */ +#include <asm/page.h> +#include <generated/asm-offsets.h> OUTPUT_FORMAT("elf64-loongarch", "elf64-loongarch", "elf64-loongarch")
@@ -10,7 +12,11 @@ OUTPUT_ARCH(loongarch) SECTIONS { - PROVIDE(_start = .); + PROVIDE(_vdso_data = . - __VVAR_PAGES * PAGE_SIZE); +#ifdef CONFIG_TIME_NS + PROVIDE(_timens_data = _vdso_data + PAGE_SIZE); +#endif + PROVIDE(_loongarch_data = _vdso_data + 2 * PAGE_SIZE); . = SIZEOF_HEADERS; .hash : { *(.hash) } :text
arch/loongarch/vdso/vgetcpu.c +1 -1
@@ -21,7 +21,7 @@ static __always_inline int read_cpu_id(void) static __always_inline const struct vdso_pcpu_data *get_pcpu_data(void) { -return (struct vdso_pcpu_data *)(get_vdso_data () +VVAR_LOONGARCH_PAGES_START *PAGE_SIZE ); +return _loongarch_data.pdata; } extern
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/2 LoongArch: vDSO: Emit GNU_EH_FRAME correctly
From: Xi Ruoyao <[email protected]>
[ Upstream commit e4878c37f6679fdea91b27a0f4e60a871f0b7bad ]
With -fno-asynchronous-unwind-tables and --no-eh-frame-hdr (the default of the linker), the GNU_EH_FRAME segment (specified by vdso.lds.S) is empty. This is not valid, as the current DWARF specification mandates the first byte of the EH frame to be the version number 1. It causes some unwinders to complain, for example the ClickHouse query profiler spams the log with messages:
clickhouse-server[365854]: libunwind: unsupported .eh_frame_hdr
version: 127 at 7ffffffb0000
Here "127" is just the byte located at the p_vaddr (0, i.e. the beginning of the vDSO) of the empty GNU_EH_FRAME segment. Cross- checking with /proc/365854/maps has also proven 7ffffffb0000 is the start of vDSO in the process VM image.
In LoongArch the -fno-asynchronous-unwind-tables option seems just a MIPS legacy, and MIPS only uses this option to satisfy the MIPS-specific "genvdso" program, per the commit cfd75c2db17e ("MIPS: VDSO: Explicitly use -fno-asynchronous-unwind-tables"). IIRC it indicates some inherent limitation of the MIPS ELF ABI and has nothing to do with LoongArch. So we can simply flip it over to -fasynchronous-unwind-tables and pass --eh-frame-hdr for linking the vDSO, allowing the profilers to unwind the stack for statistics even if the sample point is taken when the PC is in the vDSO.
However simply adjusting the options above would exploit an issue: when the libgcc unwinder saw the invalid GNU_EH_FRAME segment, it silently falled back to a machine-specific routine to match the code pattern of rt_sigreturn() and extract the registers saved in the sigframe if the code pattern is matched. As unwinding from signal handlers is vital for libgcc to support pthread cancellation etc., the fall-back routine had been silently keeping the LoongArch Linux systems functioning since Linux 5.19. But when we start to emit GNU_EH_FRAME with the correct format, fall-back routine will no longer be used and libgcc will fail to unwind the sigframe, and unwinding from signal handlers will no longer work, causing dozens of glibc test failures. To make it possible to unwind from signal handlers again, it's necessary to code the unwind info in __vdso_rt_sigreturn via .cfi_* directives.
The offsets in the .cfi_* directives depend on the layout of struct sigframe, notably the offset of sigcontext in the sigframe. To use the offset in the assembly file, factor out struct sigframe into a header to allow asm-offsets.c to output the offset for assembly.
To work around a long-term issue in the libgcc unwinder (the pc is unconditionally substracted by 1: doing so is technically incorrect for a signal frame), a nop instruction is included with the two real instructions in __vdso_rt_sigreturn in the same FDE PC range. The same hack has been used on x86 for a long time.
--- arch/loongarch/include/asm/linkage.h | 36 +++++++++++++++++++++++++++ arch/loongarch/include/asm/sigframe.h | 9 +++++++ arch/loongarch/kernel/asm-offsets.c | 2 ++ arch/loongarch/kernel/signal.c | 6 +---- arch/loongarch/vdso/Makefile | 4 +-- arch/loongarch/vdso/sigreturn.S | 6 ++--- 6 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 arch/loongarch/include/asm/sigframe.h
arch/loongarch/include/asm/linkage.h +36 -0
@@ -41,4 +41,40 @@ .cfi_endproc; \SYM_END (name,SYM_T_NONE ) +/* +* This is for the signal handler trampoline, which is used as the return +* address of the signal handlers in userspace instead of called normally. +* The long standing libgcc bug https://gcc.gnu.org/PR124050 requires a +* nop between .cfi_startproc and the actual address of the trampoline, so +* we cannot simply use SYM_FUNC_START. +* +* This wrapper also contains all the .cfi_* directives for recovering +* the content of the GPRs and the "return address" (where the rt_sigreturn +* syscall will jump to), assuming there is a struct rt_sigframe (where +* a struct sigcontext containing those information we need to recover) at +* $sp. The "DWARF for the LoongArch(TM) Architecture" manual states +* column 0 is for $zero, but it does not make too much sense to +* save/restore the hardware zero register. Repurpose this column here +* for the return address (here it's not the content of $ra we cannot use +* the default column 3). + */ +#define SYM_SIGFUNC_START (name) \ + .cfi_startproc; \ + .cfi_signal_frame; \ + .cfi_def_cfa 3, RT_SIGFRAME_SC; \ + .cfi_return_column 0; \ + .cfi_offset 0, SC_PC; \ + \ + .irp num, 1, 2, 3, 4, 5, 6, 7, 8, \ + 9, 10, 11, 12, 13, 14, 15, 16, \ + 17, 18, 19, 20, 21, 22, 23, 24, \ + 25, 26, 27, 28, 29, 30, 31; \ + .cfi_offset \num, SC_REGS + \num * SZREG; \ + .endr; \ + \ + nop; \ + SYM_START(name, SYM_L_GLOBAL, SYM_A_ALIGN) + +#define SYM_SIGFUNC_END (name) SYM_FUNC_END(name) +#endif
arch/loongarch/include/asm/sigframe.h new file +9 -0
@@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ + +#include <asm/siginfo.h> +#include <asm/ucontext.h> + +struct rt_sigframe { +struct siginfo rs_info; +struct ucontext rs_uctx; +};
arch/loongarch/kernel/asm-offsets.c +2 -0
@@ -16,6 +16,7 @@#include <asm/ptrace.h> #include <asm/processor.h> #include <asm/ftrace.h> +#include <asm/sigframe.h> #include <vdso/datapage.h> static void __used output_ptreg_defines (void )
@@ -220,6 +221,7 @@ static void __used output_sc_defines(void)COMMENT ("Linux sigcontext offsets." );OFFSET (SC_REGS , sigcontext, sc_regs);OFFSET (SC_PC , sigcontext, sc_pc); +OFFSET (RT_SIGFRAME_SC , rt_sigframe, rs_uctx.uc_mcontext);BLANK (); }
arch/loongarch/kernel/signal.c +1 -5
@@ -35,6 +35,7 @@#include <asm/cpu-features.h> #include <asm/fpu.h> #include <asm/lbt.h> +#include <asm/sigframe.h> #include <asm/ucontext.h> #include <asm/vdso.h>
@@ -51,11 +52,6 @@#define lock_lbt_owner () ({ preempt_disable(); pagefault_disable(); })#define unlock_lbt_owner () ({ pagefault_enable(); preempt_enable(); }) -struct rt_sigframe { -struct siginfo rs_info; -struct ucontext rs_uctx; -}; -struct _ctx_layout {struct sctx_info *addr;unsigned int size;
arch/loongarch/vdso/Makefile +2 -2
@@ -21,7 +21,7 @@ cflags-vdso := $(ccflags-vdso) \ $(filter -W%,$(filter-out -Wa$(comma)%,$(KBUILD_CFLAGS))) \ -std=gnu11 -O2 -g -fno-strict-aliasing -fno-common -fno-builtin \ -fno-stack-protector -fno-jump-tables -DDISABLE_BRANCH_PROFILING \ - $(call cc-option, -fno-asynchronous-unwind-tables) \ + $(call cc-option, -fasynchronous-unwind-tables) \ $(call cc-option, -fno-stack-protector) aflags-vdso := $(ccflags-vdso) \ -D__ASSEMBLY__ -Wa,-gdwarf-2
@@ -36,7 +36,7 @@ endif # VDSO linker flags. ldflags-y := -Bsymbolic --no-undefined -soname=linux-vdso.so.1 \ - $(filter -E%,$(KBUILD_CFLAGS)) -shared --build-id -T + $(filter -E%,$(KBUILD_CFLAGS)) -shared --build-id --eh-frame-hdr -T # # Shared build commands.
arch/loongarch/vdso/sigreturn.S +3 -3
@@ -12,13 +12,13 @@ #include <asm/regdef.h> #include <asm/asm.h> +#include <asm/asm-offsets.h> .section .text - .cfi_sections .debug_frame -SYM_FUNC_START(__vdso_rt_sigreturn) +SYM_SIGFUNC_START(__vdso_rt_sigreturn) li.w a7, __NR_rt_sigreturn syscall 0 -SYM_FUNC_END(__vdso_rt_sigreturn) +SYM_SIGFUNC_END(__vdso_rt_sigreturn)
Signature · 1 line
2.53.0