[PATCH 6/6] x86/vdso: Enable sframe generation in VDSO

Steven Rostedt posted 6 patches 9 months, 2 weeks ago
There is a newer version of this series
[PATCH 6/6] x86/vdso: Enable sframe generation in VDSO
Posted by Steven Rostedt 9 months, 2 weeks ago
From: Josh Poimboeuf <jpoimboe@kernel.org>

Enable sframe generation in the VDSO library so kernel and user space
can unwind through it.

Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
Changes since v5: https://lore.kernel.org/20250422183722.919601983@goodmis.org

- Replace $(comma) with a comma in the Makefile
  (Jens Remus)

 arch/Kconfig                          |  3 +++
 arch/x86/entry/vdso/Makefile          | 10 +++++++---
 arch/x86/entry/vdso/vdso-layout.lds.S |  3 +++
 arch/x86/include/asm/dwarf2.h         |  5 ++++-
 4 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/arch/Kconfig b/arch/Kconfig
index b0adb665041f..6e7d739e853b 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -435,6 +435,9 @@ config HAVE_HARDLOCKUP_DETECTOR_ARCH
 	  It uses the same command line parameters, and sysctl interface,
 	  as the generic hardlockup detectors.
 
+config AS_SFRAME
+	def_bool $(as-instr,.cfi_sections .sframe\n.cfi_startproc\n.cfi_endproc)
+
 config HAVE_PERF_REGS
 	bool
 	help
diff --git a/arch/x86/entry/vdso/Makefile b/arch/x86/entry/vdso/Makefile
index 54d3e9774d62..2dc0e0ca19a7 100644
--- a/arch/x86/entry/vdso/Makefile
+++ b/arch/x86/entry/vdso/Makefile
@@ -47,13 +47,17 @@ quiet_cmd_vdso2c = VDSO2C  $@
 $(obj)/vdso-image-%.c: $(obj)/vdso%.so.dbg $(obj)/vdso%.so $(obj)/vdso2c FORCE
 	$(call if_changed,vdso2c)
 
+ifeq ($(CONFIG_AS_SFRAME),y)
+  SFRAME_CFLAGS := -Wa,-gsframe
+endif
+
 #
 # Don't omit frame pointers for ease of userspace debugging, but do
 # optimize sibling calls.
 #
 CFL := $(PROFILING) -mcmodel=small -fPIC -O2 -fasynchronous-unwind-tables -m64 \
        $(filter -g%,$(KBUILD_CFLAGS)) -fno-stack-protector \
-       -fno-omit-frame-pointer -foptimize-sibling-calls \
+       -fno-omit-frame-pointer $(SFRAME_CFLAGS) -foptimize-sibling-calls \
        -DDISABLE_BRANCH_PROFILING -DBUILD_VDSO
 
 ifdef CONFIG_MITIGATION_RETPOLINE
@@ -63,7 +67,7 @@ endif
 endif
 
 $(vobjs): KBUILD_CFLAGS := $(filter-out $(PADDING_CFLAGS) $(CC_FLAGS_LTO) $(CC_FLAGS_CFI) $(RANDSTRUCT_CFLAGS) $(GCC_PLUGINS_CFLAGS) $(RETPOLINE_CFLAGS),$(KBUILD_CFLAGS)) $(CFL)
-$(vobjs): KBUILD_AFLAGS += -DBUILD_VDSO
+$(vobjs): KBUILD_AFLAGS += -DBUILD_VDSO $(SFRAME_CFLAGS)
 
 #
 # vDSO code runs in userspace and -pg doesn't help with profiling anyway.
@@ -104,7 +108,7 @@ $(obj)/%-x32.o: $(obj)/%.o FORCE
 
 targets += vdsox32.lds $(vobjx32s-y)
 
-$(obj)/%.so: OBJCOPYFLAGS := -S --remove-section __ex_table
+$(obj)/%.so: OBJCOPYFLAGS := -g --remove-section __ex_table
 $(obj)/%.so: $(obj)/%.so.dbg FORCE
 	$(call if_changed,objcopy)
 
diff --git a/arch/x86/entry/vdso/vdso-layout.lds.S b/arch/x86/entry/vdso/vdso-layout.lds.S
index 60747905de2b..24b92687597b 100644
--- a/arch/x86/entry/vdso/vdso-layout.lds.S
+++ b/arch/x86/entry/vdso/vdso-layout.lds.S
@@ -59,6 +59,7 @@ SECTIONS
 	.eh_frame_hdr	: { *(.eh_frame_hdr) }		:text	:eh_frame_hdr
 	.eh_frame	: { KEEP (*(.eh_frame)) }	:text
 
+	.sframe		: { *(.sframe) }		:text	:sframe
 
 	/*
 	 * Text is well-separated from actual data: there's plenty of
@@ -87,6 +88,7 @@ SECTIONS
  * Very old versions of ld do not recognize this name token; use the constant.
  */
 #define PT_GNU_EH_FRAME	0x6474e550
+#define PT_GNU_SFRAME	0x6474e554
 
 /*
  * We must supply the ELF program headers explicitly to get just one
@@ -98,4 +100,5 @@ PHDRS
 	dynamic		PT_DYNAMIC	FLAGS(4);		/* PF_R */
 	note		PT_NOTE		FLAGS(4);		/* PF_R */
 	eh_frame_hdr	PT_GNU_EH_FRAME;
+	sframe		PT_GNU_SFRAME;
 }
diff --git a/arch/x86/include/asm/dwarf2.h b/arch/x86/include/asm/dwarf2.h
index 65d958ef1178..ce294e6c9017 100644
--- a/arch/x86/include/asm/dwarf2.h
+++ b/arch/x86/include/asm/dwarf2.h
@@ -12,8 +12,11 @@
 	 * For the vDSO, emit both runtime unwind information and debug
 	 * symbols for the .dbg file.
 	 */
-
+#if defined(__x86_64__) && defined(CONFIG_AS_SFRAME)
+	.cfi_sections .eh_frame, .debug_frame, .sframe
+#else
 	.cfi_sections .eh_frame, .debug_frame
+#endif
 
 #define CFI_STARTPROC		.cfi_startproc
 #define CFI_ENDPROC		.cfi_endproc
-- 
2.47.2
Re: [PATCH 6/6] x86/vdso: Enable sframe generation in VDSO
Posted by Jens Remus 3 months, 3 weeks ago
On 4/25/2025 4:37 AM, Steven Rostedt wrote:
> From: Josh Poimboeuf <jpoimboe@kernel.org>
> 
> Enable sframe generation in the VDSO library so kernel and user space
> can unwind through it.
> 
> Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>

> diff --git a/arch/x86/entry/vdso/vdso-layout.lds.S b/arch/x86/entry/vdso/vdso-layout.lds.S

> @@ -87,6 +88,7 @@ SECTIONS
>   * Very old versions of ld do not recognize this name token; use the constant.
>   */
>  #define PT_GNU_EH_FRAME	0x6474e550
> +#define PT_GNU_SFRAME	0x6474e554
>  
>  /*
>   * We must supply the ELF program headers explicitly to get just one
> @@ -98,4 +100,5 @@ PHDRS
>  	dynamic		PT_DYNAMIC	FLAGS(4);		/* PF_R */
>  	note		PT_NOTE		FLAGS(4);		/* PF_R */
>  	eh_frame_hdr	PT_GNU_EH_FRAME;

On s390 I found that the respective s390-change needs to be guarded to
prevent the vDSO from erroneously getting generated with a bogus
GNU_SFRAME program table entry, if CONFIG_AS_SFRAME is not enabled:

$ readelf -Wl arch/s390/kernel/vdso64/vdso64.so
...
Program Headers:
  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
  LOAD           0x000000 0x0000000000000000 0x0000000000000000 0x001508 0x001508 R E 0x1000
  DYNAMIC        0x001190 0x0000000000001190 0x0000000000001190 0x000100 0x000100 R   0x8
  NOTE           0x000420 0x0000000000000420 0x0000000000000420 0x00003c 0x00003c R   0x4
  GNU_EH_FRAME   0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000     0x8
  GNU_SFRAME     0x0014f0 0x00000000000014f0 0x00000000000014f0 0x000018 0x000018 RW  0x8
...

$ xxd arch/s390/kernel/vdso64/vdso64.so
...
000014f0: 0000 0000 0000 1190 0000 0000 0000 0000  ................
00001500: 0000 0000 0000 0000 4743 433a 2028 5562  ........GCC: (Ub
00001510: 756e 7475 2031 342e 322e 302d 3139 7562  untu 14.2.0-19ub
00001520: 756e 7475 3229 2031 342e 322e 3000 0000  untu2) 14.2.0...
...

Following would be the guard for x86 (same as the one used below in
arch/x86/include/asm/dwarf2.h):

#if defined(__x86_64__) && defined(CONFIG_AS_SFRAME)

> +	sframe		PT_GNU_SFRAME;

#endif

>  }

> diff --git a/arch/x86/include/asm/dwarf2.h b/arch/x86/include/asm/dwarf2.h

> @@ -12,8 +12,11 @@
>  	 * For the vDSO, emit both runtime unwind information and debug
>  	 * symbols for the .dbg file.
>  	 */
> -
> +#if defined(__x86_64__) && defined(CONFIG_AS_SFRAME)
> +	.cfi_sections .eh_frame, .debug_frame, .sframe
> +#else
>  	.cfi_sections .eh_frame, .debug_frame
> +#endif
>  
>  #define CFI_STARTPROC		.cfi_startproc
>  #define CFI_ENDPROC		.cfi_endproc

Regards,
Jens
-- 
Jens Remus
Linux on Z Development (D3303)
+49-7031-16-1128 Office
jremus@de.ibm.com

IBM

IBM Deutschland Research & Development GmbH; Vorsitzender des Aufsichtsrats: Wolfgang Wendt; Geschäftsführung: David Faller; Sitz der Gesellschaft: Böblingen; Registergericht: Amtsgericht Stuttgart, HRB 243294
IBM Data Privacy Statement: https://www.ibm.com/privacy/

Re: [PATCH 6/6] x86/vdso: Enable sframe generation in VDSO
Posted by Jens Remus 8 months, 2 weeks ago
On 25.04.2025 04:37, Steven Rostedt wrote:
> From: Josh Poimboeuf <jpoimboe@kernel.org>
> 
> Enable sframe generation in the VDSO library so kernel and user space
> can unwind through it.
> 
> Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
> ---
> Changes since v5: https://lore.kernel.org/20250422183722.919601983@goodmis.org
> 
> - Replace $(comma) with a comma in the Makefile
>   (Jens Remus)

> diff --git a/arch/x86/entry/vdso/Makefile b/arch/x86/entry/vdso/Makefile

> @@ -47,13 +47,17 @@ quiet_cmd_vdso2c = VDSO2C  $@
>  $(obj)/vdso-image-%.c: $(obj)/vdso%.so.dbg $(obj)/vdso%.so $(obj)/vdso2c FORCE
>  	$(call if_changed,vdso2c)
>  
> +ifeq ($(CONFIG_AS_SFRAME),y)
> +  SFRAME_CFLAGS := -Wa,-gsframe

Nit: The GNU assembler (GAS) option as of "as --help" and "man as" is
"--gsframe" (with two dashes).  But as GAS uses getopt_long_only() it
accepts long options prefixed with "--" as well as "-".  I don't have
any preference.

> +endif
> +

Regards,
Jens
-- 
Jens Remus
Linux on Z Development (D3303)
+49-7031-16-1128 Office
jremus@de.ibm.com

IBM

IBM Deutschland Research & Development GmbH; Vorsitzender des Aufsichtsrats: Wolfgang Wendt; Geschäftsführung: David Faller; Sitz der Gesellschaft: Böblingen; Registergericht: Amtsgericht Stuttgart, HRB 243294
IBM Data Privacy Statement: https://www.ibm.com/privacy/