Compile-time stack validation
Subsystems (14)
v7 cover Compile-time stack validation
This is v7 of the compile-time stack validation patch set, based on the tip/master branch.
v6, which had a lot of major changes, can be found here:
https://lkml.kernel.org/r/[email protected]
For more information about the motivation behind this patch set, and more details about what it does, please see the changelog in patch 2.
To reduce churn, I didn't bother posting any patches to fix warnings. If there's agreement on this approach, I can start proposing fixes.
Posting a listing of reported warnings in a reply to this email.
v7:
- sibling call support
- document proposed solution for inline asm() frame pointer issues
- say "kernel entry/exit" instead of "context switch"
- clarify the checking of switch statement jump tables
- discard __stackvalidate_ignore_* sections in linker script
- use .Ltemp_\@ to get a unique label instead of static 3-digit number
- change STACKVALIDATE_IGNORE_FUNC variable to a static
- move STACKVALIDATE_IGNORE_INSN to arch-specific .h file
v6:
- rename asmvalidate -> stackvalidate (again)
- gcc-generated object file support
- recursive branch state analysis
- external jump support
- fixup/exception table support
- jump label support
- switch statement jump table support
- added documentation
- detection of "noreturn" dead end functions
- added a Kbuild mechanism for skipping files and dirs
- moved frame pointer macros to arch/x86/include/asm/frame.h
- moved ignore macros to include/linux/stackvalidate.h
v5:
- stackvalidate -> asmvalidate
- frame pointers only required for non-leaf functions
- check for the use of the FP_SAVE/RESTORE macros instead of manually analyzing code to detect frame pointer usage
- additional checks to ensure each function doesn't leave its boundaries
- make the macros simpler and more flexible
- support for analyzing ALTERNATIVE macros
- simplified the arch interfaces in scripts/asmvalidate/arch.h
- fixed some asmvalidate warnings
- rebased onto latest tip asm cleanups
- many more small changes
v4:
- Changed the default to CONFIG_STACK_VALIDATION=n, until all the asm code can get cleaned up.
- Fixed a stackvalidate error path exit code issue found by Michal Marek.
v3:
- Added a patch to make the push/pop CFI macros arch-independent, as suggested by H. Peter Anvin
v2:
- Fixed memory leaks reported by Petr Mladek
Josh Poimboeuf (4):
x86/asm: Frame pointer macro cleanup x86/stackvalidate: Compile-time stack validation x86/stackvalidate: Add file and directory ignores x86/stackvalidate: Add ignore macros
Documentation/stack-validation.txt | 193 ++++++++ MAINTAINERS | 8 + arch/Kconfig | 6 + arch/x86/Kconfig | 1 + arch/x86/Makefile | 6 +- arch/x86/boot/Makefile | 3 +- arch/x86/boot/compressed/Makefile | 3 +- arch/x86/entry/vdso/Makefile | 5 +- arch/x86/include/asm/frame.h | 37 +- arch/x86/include/asm/stackvalidate.h | 28 ++ arch/x86/kernel/vmlinux.lds.S | 5 +- arch/x86/purgatory/Makefile | 2 + arch/x86/realmode/Makefile | 4 +- arch/x86/realmode/rm/Makefile | 3 +- drivers/firmware/efi/libstub/Makefile | 1 + include/linux/stackvalidate.h | 21 + lib/Kconfig.debug | 11 + scripts/Makefile | 1 + scripts/Makefile.build | 34 +- scripts/stackvalidate/Makefile | 24 + scripts/stackvalidate/arch-x86.c | 148 ++++++ scripts/stackvalidate/arch.h | 44 ++ scripts/stackvalidate/elf.c | 422 ++++++++++++++++ scripts/stackvalidate/elf.h | 85 ++++ scripts/stackvalidate/list.h | 217 +++++++++ scripts/stackvalidate/special.c | 177 +++++++ scripts/stackvalidate/special.h | 40 ++ scripts/stackvalidate/stackvalidate.c | 881 ++++++++++++++++++++++++++++++++++ 28 files changed, 2382 insertions(+), 28 deletions(-) create mode 100644 Documentation/stack-validation.txt create mode 100644 arch/x86/include/asm/stackvalidate.h create mode 100644 include/linux/stackvalidate.h create mode 100644 scripts/stackvalidate/Makefile create mode 100644 scripts/stackvalidate/arch-x86.c create mode 100644 scripts/stackvalidate/arch.h create mode 100644 scripts/stackvalidate/elf.c create mode 100644 scripts/stackvalidate/elf.h create mode 100644 scripts/stackvalidate/list.h create mode 100644 scripts/stackvalidate/special.c create mode 100644 scripts/stackvalidate/special.h create mode 100644 scripts/stackvalidate/stackvalidate.c
Signature · 1 line
2.1.0
PATCH v7 1/4 x86/asm: Frame pointer macro cleanup
The FRAME/ENDFRAME asm macros for setting up and restoring the frame pointer aren't currently being used. However, they will be needed soon to help asm functions to comply with stackvalidate.
Make the code more readable and improve the comments.
--- arch/x86/include/asm/frame.h | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-)
arch/x86/include/asm/frame.h +20 -17
@@ -1,23 +1,26 @@ +#ifndef _ASM_X86_FRAME_H +#define _ASM_X86_FRAME_H +#ifdef __ASSEMBLY__#include <asm/asm.h> -/* The annotation hides the frame from the unwinder and makes it look -like a ordinary ebp save/restore. This avoids some special cases for -frame pointer later */ -#ifdef CONFIG_FRAME_POINTER - .macro FRAME - __ASM_SIZE(push,) %__ASM_REG(bp) - __ASM_SIZE(mov) %__ASM_REG(sp), %__ASM_REG(bp) - .endm - .macro ENDFRAME - __ASM_SIZE(pop,) %__ASM_REG(bp) - .endm -#else - .macro FRAME - .endm - .macro ENDFRAME - .endm -#endif +/* +* These are stack frame creation macros. They should be used by every +* callable non-leaf asm function to make kernel stack traces more reliable. +*/ +.macro FRAME + .if CONFIG_FRAME_POINTER + push %_ASM_BP + _ASM_MOV %_ASM_SP, %_ASM_BP + .endif +.endm + +.macro ENDFRAME + .if CONFIG_FRAME_POINTER + pop %_ASM_BP + .endif +.endm#endif /* __ASSEMBLY__ */ +#endif /* _ASM_X86_FRAME_H */
Signature · 1 line
2.1.0
PATCH v7 2/4 x86/stackvalidate: Compile-time stack validation
In reply to Josh PoimboeufThis adds a CONFIG_STACK_VALIDATION option which enables a host tool named stackvalidate which runs at compile time. It analyzes every .o file and ensures the validity of its stack metadata. It enforces a set of rules on asm code and C inline assembly code so that stack traces can be reliable.
Currently it checks frame pointer usage. I plan to add DWARF CFI validation as well.
For each function, it recursively follows all possible code paths and validates the correct frame pointer state at each instruction.
It also follows code paths involving special sections, like .altinstructions, __jump_table, and __ex_table, which can add alternative execution paths to a given instruction (or set of instructions). Similarly, it knows how to follow switch statements, for which gcc sometimes uses jump tables.
To achieve the validation, stackvalidate enforces the following rules:
1. Each callable function must be annotated as such with the ELF function type. In asm code, this is typically done using the ENTRY/ENDPROC macros. If stackvalidate finds a return instruction outside of a function, it flags an error since that usually indicates callable code which should be annotated accordingly.
2. Conversely, each section of code which is *not* callable should *not* be annotated as an ELF function. The ENDPROC macro shouldn't be used in this case.
3. Each callable function which calls another function must have the correct frame pointer logic, if required by CONFIG_FRAME_POINTER or the architecture's back chain rules. This can by done in asm code with the FRAME/ENDFRAME macros.
4. Dynamic jumps and jumps to undefined symbols are only allowed if:
a) the jump is part of a switch statement; or
b) the jump matches sibling call semantics and the frame pointer has the same value it had on function entry.
5. A callable function may not execute kernel entry/exit instructions. The only code which needs such instructions is kernel entry code, which shouldn't be in callable functions anyway.
It currently only supports x86_64. I tried to make the code generic so that support for other architectures can hopefully be plugged in relatively easily.
As a first step, CONFIG_STACK_VALIDATION is disabled by default, and all reported non-compliances result in warnings. Once we get them all cleaned up, we can change the default to CONFIG_STACK_VALIDATION=y and change the warnings to errors to keep the stack metadata clean.
On my Lenovo T430s 4-core laptop, building the kernel with it enabled adds about 3.6 seconds (14.4 seconds of total CPU). It hasn't been optimized for performance yet, so there are probably some opportunities for better build performance.
--- Documentation/stack-validation.txt | 193 ++++++++ MAINTAINERS | 8 + arch/Kconfig | 6 + arch/x86/Kconfig | 1 + arch/x86/Makefile | 6 +- lib/Kconfig.debug | 11 + scripts/Makefile | 1 + scripts/Makefile.build | 34 +- scripts/stackvalidate/Makefile | 24 + scripts/stackvalidate/arch-x86.c | 148 ++++++ scripts/stackvalidate/arch.h | 44 ++ scripts/stackvalidate/elf.c | 422 ++++++++++++++++ scripts/stackvalidate/elf.h | 85 ++++ scripts/stackvalidate/list.h | 217 +++++++++ scripts/stackvalidate/special.c | 177 +++++++ scripts/stackvalidate/special.h | 40 ++ scripts/stackvalidate/stackvalidate.c | 881 ++++++++++++++++++++++++++++++++++ 17 files changed, 2293 insertions(+), 5 deletions(-) create mode 100644 Documentation/stack-validation.txt create mode 100644 scripts/stackvalidate/Makefile create mode 100644 scripts/stackvalidate/arch-x86.c create mode 100644 scripts/stackvalidate/arch.h create mode 100644 scripts/stackvalidate/elf.c create mode 100644 scripts/stackvalidate/elf.h create mode 100644 scripts/stackvalidate/list.h create mode 100644 scripts/stackvalidate/special.c create mode 100644 scripts/stackvalidate/special.h create mode 100644 scripts/stackvalidate/stackvalidate.c
Documentation/stack-validation.txt new file +193 -0
@@ -0,0 +1,193 @@ +Compile-time stack validation +============================= + + +Overview +-------- + +The CONFIG_STACK_VALIDATION option enables a host tool named +stackvalidate which runs at compile time. It analyzes every .o file and +ensures the validity of its stack metadata. It enforces a set of rules +on asm code and C inline assembly code so that stack traces can be +reliable. + +Currently it only checks frame pointer usage, but there are plans to add +DWARF CFI validation as well. + +For each function, it recursively follows all possible code paths and +validates the correct frame pointer state at each instruction. + +It also follows code paths involving special sections, like +.altinstructions, __jump_table, and __ex_table, which can add +alternative execution paths to a given instruction (or set of +instructions). Similarly, it knows how to follow switch statements, for +which gcc sometimes uses jump tables. + + +Rules +----- + +To achieve the validation, stackvalidate enforces the following rules: + +1. Each callable function must be annotated as such with the ELF + function type. In asm code, this is typically done using the + ENTRY/ENDPROC macros. If stackvalidate finds a return instruction + outside of a function, it flags an error since that usually indicates + callable code which should be annotated accordingly. + +2. Conversely, each section of code which is *not* callable should *not* + be annotated as an ELF function. The ENDPROC macro shouldn't be used + in this case. + +3. Each callable function which calls another function must have the + correct frame pointer logic, if required by CONFIG_FRAME_POINTER or + the architecture's back chain rules. This can by done in asm code + with the FRAME/ENDFRAME macros. + +4. Dynamic jumps and jumps to undefined symbols are only allowed if: + + a) the jump is part of a switch statement; or + + b) the jump matches sibling call semantics and the frame pointer has + the same value it had on function entry. + +5. A callable function may not execute kernel entry/exit instructions. + The only code which needs such instructions is kernel entry code, + which shouldn't be be in callable functions anyway. + + +Errors in .S files +------------------ + +If you're getting an error in a compiled .S file which you don't +understand, first make sure that the affected code follows the above +rules. + +Here are some examples of common problems and suggestions for how to fix +them. + + +1. stackvalidate: asm_file.o: func()+0x128: call without frame pointer save/setup + + The func() function made a function call without first saving and/or + updating the frame pointer. + + If func() is indeed a callable function, add proper frame pointer + logic using the FP_SAVE and FP_RESTORE macros. Otherwise, remove its + ELF function annotation by changing ENDPROC to END. + + If you're getting this error in a .c file, see the "Errors in .c + files" section. + + +2. stackvalidate: asm_file.o: .text+0x53: return instruction outside of a callable function + + A return instruction was detected, but stackvalidate couldn't find a + way for a callable function to reach the instruction. + + If the return instruction is inside (or reachable from) a callable + function, the function needs to be annotated with the PROC/ENDPROC + macros. + + If you _really_ need a return instruction outside of a function, and + are 100% sure that it won't affect stack traces, you can tell + stackvalidate to ignore it. See the "Adding exceptions" section + below. + + +3. stackvalidate: asm_file.o: func()+0x9: function has unreachable instruction + + The instruction lives inside of a callable function, but there's no + possible control flow path from the beginning of the function to the + instruction. + + If the instruction is actually needed, and it's actually in a + callable function, ensure that its function is properly annotated + with PROC/ENDPROC. + + If it's not actually in a callable function (e.g. kernel entry code), + change ENDPROC to END. + + +4. stackvalidate: asm_file.o: func(): can't find starting instruction + or + stackvalidate: asm_file.o: func()+0x11dd: can't decode instruction + + Did you put data in a text section? If so, that can confuse + stackvalidate's instruction decoder. Move the data to a more + appropriate section like .data or .rodata. + + +5. stackvalidate: asm_file.o: func()+0x6: kernel entry/exit from callable instruction + + This is a kernel entry/exit instruction like sysenter or sysret. + Such instructions aren't allowed in a callable function, and are most + likely part of the kernel entry code. + + If the instruction isn't actually in a callable function, change + ENDPROC to END. + + +6. stackvalidate: asm_file.o: func()+0x26: sibling call from callable instruction with changed frame pointer + + This is a dynamic jump or a jump to an undefined symbol. + Stackvalidate assumed it's a sibling call and detected that the frame + pointer wasn't first restored to its original state. + + If it's not really a sibling call, you may need to move the + destination code to the local file. + + If the instruction is not actually in a callable function (e.g. + kernel entry code), change ENDPROC to END. + + +7. stackvalidate: asm_file: func()+0x5c: frame pointer state mismatch + + The instruction's frame pointer state is inconsistent, depending on + which execution path was taken to reach the instruction. + + Make sure the function pushes and sets up the frame pointer (for + x86_64, this means rbp) at the beginning of the function and pops it + at the end of the function. Also make sure that no other code in the + function touches the frame pointer. + + +Errors in .c files +------------------ + +If you're getting a stackvalidate error in a compiled .c file, chances +are the file uses an asm() statement which has a "call" instruction. An +asm() statement with a call instruction must declare the use of the +stack pointer in its output operand. For example, on x86_64: + + register void *__sp asm("rsp"); + asm volatile("call func" : "+r" (__sp)); + +Otherwise the stack frame may not get created before the call. + +Another possible cause for errors in C code is if the Makefile removes +-fno-omit-frame-pointer or adds -fomit-frame-pointer to the gcc options. + +Also see the above section for .S file errors for more information what +the individual error messages mean. + + + +Adding exceptions +----------------- + +If you _really_ need stackvalidate to ignore something, and are 100% +sure that it won't affect kernel stack traces, you can tell +stackvalidate to ignore it: + +- To skip validation of an instruction, use the + STACKVALIDATE_IGNORE_INSN macro immediately before the instruction. + +- To skip validation of a function, use the STACKVALIDATE_IGNORE_FUNC + macro. + +- To skip validation of a file, add "STACKVALIDATE_filename.o := n" to + the Makefile. + +- To skip validation of a directory, add "STACKVALIDATE := n" to the + Makefile.
MAINTAINERS +8 -0
@@ -9666,6 +9666,14 @@ L: [email protected] S: Supported F: Documentation/stable_kernel_rules.txt +STACK VALIDATION +M: Josh Poimboeuf <[email protected]> +S: Supported +F: scripts/stackvalidate/ +F: include/linux/stackvalidate.h +F: arch/x86/include/asm/stackvalidate.h +F: Documentation/stack-validation.txt + STAGING SUBSYSTEM M: Greg Kroah-Hartman <[email protected]> T: git git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging.git
arch/Kconfig +6 -0
@@ -515,6 +515,12 @@ config HAVE_COPY_THREAD_TLS normal C parameter passing, rather than extracting the syscall argument from pt_regs. +config HAVE_STACK_VALIDATION + bool + help + Architecture supports the stackvalidate host tool, which adds + compile-time stack metadata validation. + # # ABI hall of shame #
arch/x86/Kconfig +1 -0
@@ -150,6 +150,7 @@ config X86 select VIRT_TO_BUS select X86_DEV_DMA_OPS if X86_64 select X86_FEATURE_NAMES if PROC_FS + select HAVE_STACK_VALIDATION if X86_64 config INSTRUCTION_DECODER def_bool y
arch/x86/Makefile +5 -1
@@ -174,9 +174,13 @@ KBUILD_CFLAGS += $(call cc-option,-mno-avx,) KBUILD_CFLAGS += $(mflags-y) KBUILD_AFLAGS += $(mflags-y) -archscripts: scripts_basic +archscripts: scripts_basic $(objtree)/arch/x86/lib/inat-tables.c $(Q)$(MAKE) $(build)=arch/x86/tools relocs +# this file is needed early by scripts/stackvalidate +$(objtree)/arch/x86/lib/inat-tables.c: + $(Q)$(MAKE) $(build)=arch/x86/lib $@ + ### # Syscall table generation
lib/Kconfig.debug +11 -0
@@ -332,6 +332,17 @@ config FRAME_POINTER larger and slower, but it gives very useful debugging information in case of kernel bugs. (precise oopses/stacktraces/warnings) +config STACK_VALIDATION + bool "Enable compile-time stack metadata validation" + depends on HAVE_STACK_VALIDATION + default n + help + Add compile-time checks to validate stack metadata, including frame + pointers and back chain pointers. This helps ensure that runtime + stack traces are more reliable. + + For more information, see Documentation/stack-validation.txt. + config DEBUG_FORCE_WEAK_PER_CPU bool "Force weak per-cpu definitions" depends on DEBUG_KERNEL
scripts/Makefile +1 -0
@@ -37,6 +37,7 @@ subdir-y += mod subdir-$(CONFIG_SECURITY_SELINUX) += selinux subdir-$(CONFIG_DTC) += dtc subdir-$(CONFIG_GDB_SCRIPTS) += gdb +subdir-$(CONFIG_STACK_VALIDATION) += stackvalidate # Let clean descend into subdirs subdir- += basic kconfig package
scripts/Makefile.build +30 -4
@@ -241,9 +241,26 @@ cmd_record_mcount = \ fi; endif +ifdef CONFIG_STACK_VALIDATION + +stackvalidate = $(objtree)/scripts/stackvalidate/stackvalidate + +ifndef CONFIG_FRAME_POINTER +nofp = --no-frame-pointer +endif + +# Set STACKVALIDATE_foo.o=n to skip stack validation for a file. +# Set STACKVALIDATE=n to skip stack validation for a directory. +cmd_stackvalidate = $(if $(patsubst n%,, \ + $(STACKVALIDATE_$(basetarget).o)$(STACKVALIDATE)y), \ + $(stackvalidate) $(nofp) "$(@)";) + +endif # CONFIG_STACK_VALIDATION + define rule_cc_o_c $(call echo-cmd,checksrc) $(cmd_checksrc) \ $(call echo-cmd,cc_o_c) $(cmd_cc_o_c); \ + $(cmd_stackvalidate) \ $(cmd_modversions) \ $(call echo-cmd,record_mcount) \ $(cmd_record_mcount) \
@@ -253,14 +270,23 @@ define rule_cc_o_c mv -f $(dot-target).tmp $(dot-target).cmd endef +define rule_as_o_S + $(call echo-cmd,as_o_S) $(cmd_as_o_S); \ + $(cmd_stackvalidate) \ + scripts/basic/fixdep $(depfile) $@ '$(call make-cmd,as_o_S)' > \ + $(dot-target).tmp; \ + rm -f $(depfile); \ + mv -f $(dot-target).tmp $(dot-target).cmd +endef + # Built-in and composite module parts -$(obj)/%.o: $(src)/%.c $(recordmcount_source) FORCE +$(obj)/%.o: $(src)/%.c $(recordmcount_source) $(stackvalidate) FORCE $(call cmd,force_checksrc) $(call if_changed_rule,cc_o_c) # Single-part modules are special since we need to mark them in $(MODVERDIR) -$(single-used-m): $(obj)/%.o: $(src)/%.c $(recordmcount_source) FORCE +$(single-used-m): $(obj)/%.o: $(src)/%.c $(recordmcount_source) $(stackvalidate) FORCE $(call cmd,force_checksrc) $(call if_changed_rule,cc_o_c) @{ echo $(@:.o=.ko); echo $@; } > $(MODVERDIR)/$(@F:.o=.mod)
@@ -290,8 +316,8 @@ $(obj)/%.s: $(src)/%.S FORCE quiet_cmd_as_o_S = AS $(quiet_modtag) $@ cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $< -$(obj)/%.o: $(src)/%.S FORCE - $(call if_changed_dep,as_o_S) +$(obj)/%.o: $(src)/%.S $(stackvalidate) FORCE + $(call if_changed_rule,as_o_S) targets += $(real-objs-y) $(real-objs-m) $(lib-y) targets += $(extra-y) $(MAKECMDGOALS) $(always)
scripts/stackvalidate/Makefile new file +24 -0
@@ -0,0 +1,24 @@ +hostprogs-y := stackvalidate +always := $(hostprogs-y) + +stackvalidate-objs := stackvalidate.o elf.o special.o + +HOSTCFLAGS += -Werror +HOSTLOADLIBES_stackvalidate := -lelf + +ifdef CONFIG_X86 + +stackvalidate-objs += arch-x86.o + +HOSTCFLAGS_arch-x86.o := -I$(objtree)/arch/x86/lib/ \ + -I$(srctree)/arch/x86/include/ \ + -I$(srctree)/arch/x86/lib/ + +$(obj)/arch-x86.o: $(srctree)/arch/x86/lib/insn.c \ + $(srctree)/arch/x86/lib/inat.c \ + $(srctree)/arch/x86/include/asm/inat_types.h \ + $(srctree)/arch/x86/include/asm/inat.h \ + $(srctree)/arch/x86/include/asm/insn.h \ + $(objtree)/arch/x86/lib/inat-tables.c + +endif
scripts/stackvalidate/arch-x86.c new file +148 -0
148 lines not on this page · raw message
scripts/stackvalidate/arch.h new file +44 -0
44 lines not on this page · raw message
scripts/stackvalidate/elf.c new file +422 -0
422 lines not on this page · raw message
scripts/stackvalidate/elf.h new file +85 -0
85 lines not on this page · raw message
scripts/stackvalidate/list.h new file +217 -0
217 lines not on this page · raw message
scripts/stackvalidate/special.c new file +177 -0
177 lines not on this page · raw message
scripts/stackvalidate/special.h new file +40 -0
@@ -0,0 +1,40 @@ +/* +* Copyright (C) 2015 Josh Poimboeuf <[email protected]> +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef _SPECIAL_H +#define _SPECIAL_H + +#include <stdbool.h> +#include "elf.h" + +struct special_alt { +struct list_head list; + +bool group; + +struct section *orig_sec; +unsigned long orig_off; + +struct section *new_sec; +unsigned long new_off; + +unsigned int orig_len, new_len;/* group only */ +}; + +int special_get_alts (struct elf *elf,struct list_head *alts); + +#endif /* _SPECIAL_H */
scripts/stackvalidate/stackvalidate.c new file +881 -0
881 lines not on this page · raw message
Signature · 1 line
2.1.0
Peter Zijlstra
maintainerRe: [PATCH v7 2/4] x86/stackvalidate: Compile-time stack validation
Quoted 8 lines · Josh Poimboeuf
This adds a
CONFIG_STACK_VALIDATIONoption which enables a host tool named stackvalidate which runs at compile time. It analyzes every .o file and ensures the validity of its stack metadata. It enforces a set of rules on asm code and C inline assembly code so that stack traces can be reliable.Currently it checks frame pointer usage. I plan to add DWARF CFI validation as well.
Validation or Annotation, as in the generation of DWARF CFI info?
Re: [PATCH v7 2/4] x86/stackvalidate: Compile-time stack validation
Quoted 11 lines · Peter Zijlstra
Quoted 8 lines · Josh Poimboeuf
This adds a
CONFIG_STACK_VALIDATIONoption which enables a host tool named stackvalidate which runs at compile time. It analyzes every .o file and ensures the validity of its stack metadata. It enforces a set of rules on asm code and C inline assembly code so that stack traces can be reliable.Currently it checks frame pointer usage. I plan to add DWARF CFI validation as well.
Validation or Annotation, as in the generation of DWARF CFI info?
My current thinking is I'll do both:
- CFI generation for asm code
- CFI validation of C code (and possibly asm code too)
It's TBD whether the generation will be done by this tool or by a separate tool.
Signature · 1 line
Josh
Peter Zijlstra
maintainerRe: [PATCH v7 2/4] x86/stackvalidate: Compile-time stack validation
In reply to Josh PoimboeufQuoted 37 lines · Josh Poimboeuf
This adds a
CONFIG_STACK_VALIDATIONoption which enables a host tool named stackvalidate which runs at compile time. It analyzes every .o file and ensures the validity of its stack metadata. It enforces a set of rules on asm code and C inline assembly code so that stack traces can be reliable.Currently it checks frame pointer usage. I plan to add DWARF CFI validation as well.
For each function, it recursively follows all possible code paths and validates the correct frame pointer state at each instruction.
It also follows code paths involving special sections, like .altinstructions,
__jump_table, and__ex_table, which can add alternative execution paths to a given instruction (or set of instructions). Similarly, it knows how to follow switch statements, for which gcc sometimes uses jump tables.To achieve the validation, stackvalidate enforces the following rules:
1. Each callable function must be annotated as such with the ELF function type. In asm code, this is typically done using the ENTRY/ENDPROC macros. If stackvalidate finds a return instruction outside of a function, it flags an error since that usually indicates callable code which should be annotated accordingly.
2. Conversely, each section of code which is *not* callable should *not* be annotated as an ELF function. The ENDPROC macro shouldn't be used in this case.
3. Each callable function which calls another function must have the correct frame pointer logic, if required by
CONFIG_FRAME_POINTERor the architecture's back chain rules. This can by done in asm code with the FRAME/ENDFRAME macros.4. Dynamic jumps and jumps to undefined symbols are only allowed if:
Quoted 8 lines
a) the jump is part of a switch statement; or
b) the jump matches sibling call semantics and the frame pointer has the same value it had on function entry.
5. A callable function may not execute kernel entry/exit instructions. The only code which needs such instructions is kernel entry code, which shouldn't be in callable functions anyway.
How (if it does at all) deal with function-trace / -pg -fprofile-arcs things? Does it silently ignore the __mcount calls and assumes ftrace knows wtf its doing? ;-)
Re: [PATCH v7 2/4] x86/stackvalidate: Compile-time stack validation
Quoted 43 lines · Peter Zijlstra
Quoted 42 lines · Josh Poimboeuf
This adds a
CONFIG_STACK_VALIDATIONoption which enables a host tool named stackvalidate which runs at compile time. It analyzes every .o file and ensures the validity of its stack metadata. It enforces a set of rules on asm code and C inline assembly code so that stack traces can be reliable.Currently it checks frame pointer usage. I plan to add DWARF CFI validation as well.
For each function, it recursively follows all possible code paths and validates the correct frame pointer state at each instruction.
It also follows code paths involving special sections, like .altinstructions,
__jump_table, and__ex_table, which can add alternative execution paths to a given instruction (or set of instructions). Similarly, it knows how to follow switch statements, for which gcc sometimes uses jump tables.To achieve the validation, stackvalidate enforces the following rules:
1. Each callable function must be annotated as such with the ELF function type. In asm code, this is typically done using the ENTRY/ENDPROC macros. If stackvalidate finds a return instruction outside of a function, it flags an error since that usually indicates callable code which should be annotated accordingly.
2. Conversely, each section of code which is *not* callable should *not* be annotated as an ELF function. The ENDPROC macro shouldn't be used in this case.
3. Each callable function which calls another function must have the correct frame pointer logic, if required by
CONFIG_FRAME_POINTERor the architecture's back chain rules. This can by done in asm code with the FRAME/ENDFRAME macros.4. Dynamic jumps and jumps to undefined symbols are only allowed if:
a) the jump is part of a switch statement; or
b) the jump matches sibling call semantics and the frame pointer has the same value it had on function entry.
Quoted 8 lines
Quoted 3 lines
5. A callable function may not execute kernel entry/exit instructions. The only code which needs such instructions is kernel entry code, which shouldn't be in callable functions anyway.
How (if it does at all) deal with function-trace / -pg -fprofile-arcs things? Does it silently ignore the
__mcountcalls and assumes ftrace knows wtf its doing? ;-)
Adding Steven to CC to keep me honest.
In the case of "-pg -mfentry", which is what ftrace has relied on for the past few years for newer versions of gcc, stackvalidate silently ignores __fentry__ calls and assumes that ftrace indeed knows wtf it's doing. I don't see a problem there as long as the ftrace handler doesn't sleep.
I haven't run stackvalidate on the old "-pg" mcount non-fentry stuff, but I think it creates a stack frame before calling mcount, so it should be fine.
I don't know much about -fprofile-arcs, but as far as I can tell, it's only used for gcov.
Signature · 1 line
Josh
Peter Zijlstra
maintainerRe: [PATCH v7 2/4] x86/stackvalidate: Compile-time stack validation
Quoted 7 lines · Josh Poimboeuf
Quoted 3 lines
How (if it does at all) deal with function-trace / -pg -fprofile-arcs things? Does it silently ignore the
__mcountcalls and assumes ftrace knows wtf its doing? ;-)Adding Steven to CC to keep me honest.
In the case of "-pg -mfentry", which is what ftrace has relied on for
Ah -mfentry is the magic word, I couldn't find it and a grep led me astray.
Quoted 4 lines
the past few years for newer versions of gcc, stackvalidate silently ignores
__fentry__calls and assumes that ftrace indeed knows wtf it's doing. I don't see a problem there as long as the ftrace handler doesn't sleep.
They should not indeed, however it would be very nice if backtraces would still be 'good'.
Quoted 3 lines
I haven't run stackvalidate on the old "-pg" mcount non-fentry stuff, but I think it creates a stack frame before calling mcount, so it should be fine.
Ok.
Quoted 2 lines
I don't know much about -fprofile-arcs, but as far as I can tell, it's only used for gcov.
Right, nobody much uses that I think.
Re: [PATCH v7 2/4] x86/stackvalidate: Compile-time stack validation
Quoted 8 lines · Peter Zijlstra
Quoted 4 lines · Josh Poimboeuf
the past few years for newer versions of gcc, stackvalidate silently ignores
__fentry__calls and assumes that ftrace indeed knows wtf it's doing. I don't see a problem there as long as the ftrace handler doesn't sleep.They should not indeed, however it would be very nice if backtraces would still be 'good'.
Agreed, though I don't know if it's possible for stackvalidate to reasonably understand what ftrace is doing. I tend to doubt it, since ftrace does some code modification at runtime.
It does spit out some warnings for mcount_64.S. I'll need to look at that code in more detail to figure out if the warnings should be whitelisted as false positives, or if there's some way to annotate the code to help stackvalidate understand it and validate it.
Signature · 1 line
Josh
Re: [PATCH v7 2/4] x86/stackvalidate: Compile-time stack validation
In reply to Josh PoimboeufHi,
Just nitpicks below..
Quoted 54 lines · Josh Poimboeuf
scripts/stackvalidate/elf.c new file +54
-0+struct elf *elf_open (const char *name) +{ +struct elf *elf; + +elf_version (EV_CURRENT ); + + elf =malloc (sizeof (*elf)); +if (!elf) { +perror ("malloc" ); +return NULL ; + } +memset (elf,0 ,sizeof (*elf)); + +INIT_LIST_HEAD (&elf->sections); + + elf->name =strdup (name); +if (!elf->name) { +perror ("strdup" ); + goto err; + } + + elf->fd =open (name,O_RDONLY ); +if (elf->fd ==-1 ) { +perror ("open" ); + goto err; + } + + elf->elf =elf_begin (elf->fd,ELF_C_READ_MMAP ,NULL ); +if (!elf->elf) { +perror ("elf_begin" ); + goto err; + } + +if (!gelf_getehdr (elf->elf, &elf->ehdr)) { +perror ("gelf_getehdr" ); + goto err; + } + +if (read_sections (elf)) + goto err; + +if (read_symbols (elf)) + goto err; + +if (read_relas (elf)) + goto err; + +return elf; + +err: +elf_close (elf); +return NULL ; +} +
scripts/stackvalidate/elf.c line 411
Quoted 10 lines
scripts/stackvalidate/elf.c new file +10
-0+void elf_close (struct elf *elf) +{ +struct section *sec, *tmpsec; +struct symbol *sym, *tmpsym; + +list_for_each_entry_safe (sec, tmpsec, &elf->sections, list) { +list_for_each_entry_safe (sym, tmpsym, &sec->symbols, list) { +list_del (&sym->list); +free (sym); + }
It seems that it needs to free entries in sec->relas too here.
Quoted 11 lines
scripts/stackvalidate/elf.c new file +11
-0+list_del (&sec->list); +free (sec); + } +if (elf->name) +free (elf->name); +if (elf->fd >0 ) +close (elf->fd); +if (elf->elf) +elf_end (elf->elf); +free (elf); +}
[SNIP]
Quoted 19 lines
scripts/stackvalidate/stackvalidate.c new file +19
-0+int main (int argc,char *argv[]) +{ +struct elf *elf; +int ret =0 , warnings =0 ; + +argp_parse (&argp, argc, argv,0 ,0 , &args); + + elf =elf_open (args.args[0 ]); +if (!elf) { +fprintf (stderr,"error reading elf file %s\n" , args.args[0 ]); +return 1 ; + } + + ret =decode_sections (elf); +if (ret <0 ) + goto out; + warnings += ret; + + ret =validate_functions (elf);
scripts/stackvalidate/stackvalidate.c line 876
Quoted 10 lines
scripts/stackvalidate/stackvalidate.c new file +10
-0+if (ret <0 ) + goto out; + warnings += ret; + + ret =validate_uncallable_instructions (elf); +if (ret <0 ) + goto out; + warnings += ret; + +out:
elf_close(elf); ??
Thanks,
Namhyung
Quoted 13 lines
scripts/stackvalidate/stackvalidate.c new file +5
-1+/* ignore warnings for now until we get all the code cleaned up */ +if (ret || warnings) +return 0 ; +return 0 ; +} --2.1.0
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Re: [PATCH v7 2/4] x86/stackvalidate: Compile-time stack validation
Quoted 3 lines · Namhyung Kim
Hi,
Just nitpicks below..
Hi Namhyung, thanks for the review!
Quoted 5 lines · Josh Poimboeuf
Quoted 4 lines
scripts/stackvalidate/elf.c new file +4
-0+void elf_close (struct elf *elf) +{ +struct section *sec, *tmpsec; +struct symbol *sym, *tmpsym;
Quoted 8 lines
scripts/stackvalidate/elf.c line 411
Quoted 6 lines
scripts/stackvalidate/elf.c new file +6
-0+ +list_for_each_entry_safe (sec, tmpsec, &elf->sections, list) { +list_for_each_entry_safe (sym, tmpsym, &sec->symbols, list) { +list_del (&sym->list); +free (sym); + }It seems that it needs to free entries in
sec->relastoo here.
I must confess that this program has some known memory leaks. I actually have a comment at the top of the file attempting to explain why, but I'll try to improve the comment a little bit.
Many of the leaks are actually intentional, since it's a short running program that needs to run fast (though I haven't yet optimized it for speed). And most of the data structures are needed until it exits anyway.
It's perhaps distasteful, but it improves performance. And I'm a pragmatist at heart ;-)
That said, I will make your suggested change here, since maybe the elf.c code might someday be reused elsewhere, and freeing memory is actually the goal of this function.
Quoted 23 lines
Quoted 23 lines
scripts/stackvalidate/stackvalidate.c new file +23
-0+int main (int argc,char *argv[]) +{ +struct elf *elf; +int ret =0 , warnings =0 ; + +argp_parse (&argp, argc, argv,0 ,0 , &args); + + elf =elf_open (args.args[0 ]); +if (!elf) { +fprintf (stderr,"error reading elf file %s\n" , args.args[0 ]); +return 1 ; + } + + ret =decode_sections (elf); +if (ret <0 ) + goto out; + warnings += ret; + + ret =validate_functions (elf); +if (ret <0 ) + goto out; + warnings += ret; +
Quoted 8 lines
scripts/stackvalidate/stackvalidate.c line 876
Quoted 6 lines
scripts/stackvalidate/stackvalidate.c new file +6
-0+ ret =validate_uncallable_instructions (elf); +if (ret <0 ) + goto out; + warnings += ret; + +out:
elf_close(elf); ??
I intentionally left out the call to elf_close() here, since this is the exit path and the kernel will free the memory anyway.
Signature · 1 line
Josh
Re: [PATCH v7 2/4] x86/stackvalidate: Compile-time stack validation
Quoted 32 lines · Josh Poimboeuf
Quoted 23 lines
Quoted 23 lines
scripts/stackvalidate/stackvalidate.c new file +23
-0+int main (int argc,char *argv[]) +{ +struct elf *elf; +int ret =0 , warnings =0 ; + +argp_parse (&argp, argc, argv,0 ,0 , &args); + + elf =elf_open (args.args[0 ]); +if (!elf) { +fprintf (stderr,"error reading elf file %s\n" , args.args[0 ]); +return 1 ; + } + + ret =decode_sections (elf); +if (ret <0 ) + goto out; + warnings += ret; + + ret =validate_functions (elf); +if (ret <0 ) + goto out; + warnings += ret; +Quoted 8 lines
scripts/stackvalidate/stackvalidate.c line 876
Quoted 6 lines
scripts/stackvalidate/stackvalidate.c new file +6
-0+ ret =validate_uncallable_instructions (elf); +if (ret <0 ) + goto out; + warnings += ret; + +out:
elf_close(elf); ??
Quoted 2 lines
I intentionally left out the call to
elf_close()here, since this is the exit path and the kernel will free the memory anyway.
OTOH it makes Valgrind runs harder to interpret, as real leaks won't be visible.
So I'd only do intentional leaks only if it's truly, measurably improves performance.
Thanks,
Ingo
Re: [PATCH v7 2/4] x86/stackvalidate: Compile-time stack validation
Quoted 35 lines · Ingo Molnar
Quoted 32 lines · Josh Poimboeuf
Quoted 31 lines
Quoted 19 lines
scripts/stackvalidate/stackvalidate.c new file +19
-0+int main (int argc,char *argv[]) +{ +struct elf *elf; +int ret =0 , warnings =0 ; + +argp_parse (&argp, argc, argv,0 ,0 , &args); + + elf =elf_open (args.args[0 ]); +if (!elf) { +fprintf (stderr,"error reading elf file %s\n" , args.args[0 ]); +return 1 ; + } + + ret =decode_sections (elf); +if (ret <0 ) + goto out; + warnings += ret; + + ret =validate_functions (elf);scripts/stackvalidate/stackvalidate.c line 876
Quoted 10 lines
scripts/stackvalidate/stackvalidate.c new file +10
-0+if (ret <0 ) + goto out; + warnings += ret; + + ret =validate_uncallable_instructions (elf); +if (ret <0 ) + goto out; + warnings += ret; + +out:
elf_close(elf); ??
Quoted 7 lines
Quoted 2 lines
I intentionally left out the call to
elf_close()here, since this is the exit path and the kernel will free the memory anyway.OTOH it makes Valgrind runs harder to interpret, as real leaks won't be visible.
So I'd only do intentional leaks only if it's truly, measurably improves performance.
Ok. I'll fix the leaks.
Signature · 1 line
Josh
Re: [PATCH v7 2/4] x86/stackvalidate: Compile-time stack validation
In reply to Josh PoimboeufOn Mon, 2015-07-20 at 12:50 -0500, Josh Poimboeuf wrote:
[...]
Quoted 2 lines
It's perhaps distasteful, but it improves performance. And I'm a pragmatist at heart ;-)
And you measured the time gain guaranteeing that it actually saves that much time. Usually that isn't actually measurable ....
And the usual solution is to have a preprocessor symbol to allow people with lots of time and/or "valgrind" and similar tools to clean up cleanly and the speed gang can compile that code out.
Kind regards,
Bernd
Signature · 4 lines
"I dislike type abstraction if it has no real reason. And saving
on typing is not a good reason - if your typing speed is the main
issue when you're coding, you're doing something seriously wrong."
- Linus TorvaldsRe: [PATCH v7 2/4] x86/stackvalidate: Compile-time stack validation
Quoted 7 lines · Bernd Petrovitsch
On Mon, 2015-07-20 at 12:50 -0500, Josh Poimboeuf wrote:
[...]Quoted 2 lines
It's perhaps distasteful, but it improves performance. And I'm a pragmatist at heart ;-)
And you measured the time gain guaranteeing that it actually saves that much time. Usually that isn't actually measurable ....
You're probably right. s/pragmatist/lazy/ ;-) I'll fix the leaks.
Signature · 1 line
Josh
PATCH v7 3/4 x86/stackvalidate: Add file and directory ignores
In reply to Josh PoimboeufTell stackvalidate to skip validation of the following code:
- boot image
- vdso image
- kexec purgatory
- realmode
- efi libstub
They all run outside the kernel's normal mode of operation and they don't affect runtime kernel stack traces, so they're free to skirt the stackvalidate rules.
--- arch/x86/boot/Makefile | 3 ++- arch/x86/boot/compressed/Makefile | 3 ++- arch/x86/entry/vdso/Makefile | 5 ++++- arch/x86/purgatory/Makefile | 2 ++ arch/x86/realmode/Makefile | 4 +++- arch/x86/realmode/rm/Makefile | 3 ++- drivers/firmware/efi/libstub/Makefile | 1 + 7 files changed, 16 insertions(+), 5 deletions(-)
arch/x86/boot/Makefile +2 -1
@@ -14,7 +14,8 @@ # Set it to -DSVGA_MODE=NORMAL_VGA if you just want the EGA/VGA mode. # The number is the same as you would ordinarily press at bootup. -KASAN_SANITIZE := n +KASAN_SANITIZE := n +STACKVALIDATE := n SVGA_MODE := -DSVGA_MODE=NORMAL_VGA
arch/x86/boot/compressed/Makefile +2 -1
@@ -16,7 +16,8 @@ # (see scripts/Makefile.lib size_append) # compressed vmlinux.bin.all + u32 size of vmlinux.bin.all -KASAN_SANITIZE := n +KASAN_SANITIZE := n +STACKVALIDATE := n targets := vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2 vmlinux.bin.lzma \ vmlinux.bin.xz vmlinux.bin.lzo vmlinux.bin.lz4
arch/x86/entry/vdso/Makefile +4 -1
@@ -3,7 +3,9 @@ # KBUILD_CFLAGS += $(DISABLE_LTO) -KASAN_SANITIZE := n + +KASAN_SANITIZE := n +STACKVALIDATE := n VDSO64-$(CONFIG_X86_64) := y VDSOX32-$(CONFIG_X86_X32_ABI) := y
@@ -15,6 +17,7 @@ vobjs-y := vdso-note.o vclock_gettime.o vgetcpu.o # files to link into kernel obj-y += vma.o +STACKVALIDATE_vma.o := y # vDSO images to build vdso_img-$(VDSO64-y) += 64
arch/x86/purgatory/Makefile +2 -0
@@ -1,3 +1,5 @@ +STACKVALIDATE := n + purgatory-y := purgatory.o stack.o setup-x86_$(BITS).o sha256.o entry64.o string.o targets += $(purgatory-y)
arch/x86/realmode/Makefile +3 -1
@@ -6,7 +6,9 @@ # for more details. # # -KASAN_SANITIZE := n +KASAN_SANITIZE := n +STACKVALIDATE := n + subdir- := rm obj-y += init.o
arch/x86/realmode/rm/Makefile +2 -1
@@ -6,7 +6,8 @@ # for more details. # # -KASAN_SANITIZE := n +KASAN_SANITIZE := n +STACKVALIDATE := n always := realmode.bin realmode.relocs
drivers/firmware/efi/libstub/Makefile +1 -0
@@ -20,6 +20,7 @@ KBUILD_CFLAGS := $(cflags-y) \ GCOV_PROFILE := n KASAN_SANITIZE := n +STACKVALIDATE := n lib-y := efi-stub-helper.o lib-$(CONFIG_EFI_ARMSTUB) += arm-stub.o fdt.o
Signature · 1 line
2.1.0
PATCH v7 4/4 x86/stackvalidate: Add ignore macros
In reply to Josh PoimboeufAdd new stackvalidate ignore macros: STACKVALIDATE_IGNORE_INSN and STACKVALIDATE_IGNORE_FUNC. These can be used to tell stackvalidate to skip validation of an instruction or a function, respectively.
--- arch/x86/include/asm/stackvalidate.h | 28 ++++++++++++++++++++++++++++ arch/x86/kernel/vmlinux.lds.S | 5 ++++- include/linux/stackvalidate.h | 21 +++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 arch/x86/include/asm/stackvalidate.h create mode 100644 include/linux/stackvalidate.h
arch/x86/include/asm/stackvalidate.h new file +28 -0
@@ -0,0 +1,28 @@ +#ifndef _ASM_X86_STACKVALIDATE_H +#define _ASM_X86_STACKVALIDATE_H + +#include <asm/asm.h> + +#ifdef __ASSEMBLY__ + +/* +* This asm macro tells the stack validation script to ignore the instruction +* immediately after the macro. It should only be used in special cases where +* you're 100% sure it won't affect the reliability of frame pointers and +* kernel stack traces. +* +* For more information, see Documentation/stack-validation.txt. +*/ +.macro STACKVALIDATE_IGNORE_INSN + .if CONFIG_STACK_VALIDATION + .Ltemp_\@: + .pushsection __stackvalidate_ignore_insn, "a" + _ASM_ALIGN + .long .Ltemp_\@ - . + .popsection + .endif +.endm + +#endif /* __ASSEMBLY__ */ + +#endif /* _ASM_X86_STACKVALIDATE_H */
arch/x86/kernel/vmlinux.lds.S +4 -1
@@ -332,7 +332,10 @@ SECTIONS /* Sections to be discarded */ DISCARDS - /DISCARD/ : { *(.eh_frame) } + /DISCARD/ : { + *(.eh_frame) + *(__stackvalidate_ignore_*) + } }
include/linux/stackvalidate.h new file +21 -0
@@ -0,0 +1,21 @@ +#ifndef _LINUX_STACKVALIDATE_H +#define _LINUX_STACKVALIDATE_H + +#include <asm/stackvalidate.h> + +#ifndef __ASSEMBLY__ + +/* +* This C macro tells the stack validation script to ignore the function. It +* should only be used in special cases where you're 100% sure it won't affect +* the reliability of frame pointers and kernel stack traces. +* +* For more information, see Documentation/stack-validation.txt. +*/ +#define STACKVALIDATE_IGNORE_FUNC (_func) \ + static void __used __section(__stackvalidate_ignore_func) \ + *__stackvalidate_ignore_func_##_func = _func + +#endif /* !__ASSEMBLY__ */ + +#endif /* _LINUX_STACKVALIDATE_H */
Signature · 1 line
2.1.0
Quoted 1 line · Josh Poimboeuf
Posting a listing of reported warnings in a reply to this email.
These are the reported stackvalidate warnings on tip/master with my Fedora-based config. There were 1399 warnings in 26 .c files and 16 .S files. It was mostly repeat warnings so I removed a lot of the repeats to keep it short.
stackvalidate: arch/x86/crypto/aesni-intel_asm.o: aesni_set_key()+0x128: call without frame pointer save/setup
...removed 29 lines...
stackvalidate: arch/x86/crypto/aesni-intel_asm.o: aesni_set_key()+0xb9: call without frame pointer save/setup
stackvalidate: arch/x86/crypto/aesni-intel_asm.o: aesni_enc()+0xa: call without frame pointer save/setup
stackvalidate: arch/x86/crypto/aesni-intel_asm.o: aesni_dec()+0x11: call without frame pointer save/setup
stackvalidate: arch/x86/crypto/aesni-intel_asm.o: aesni_ecb_enc()+0x57: call without frame pointer save/setup
...removed 5 lines...
stackvalidate: arch/x86/crypto/aesni-intel_asm.o: aesni_cbc_dec()+0x43: call without frame pointer save/setup
stackvalidate: arch/x86/crypto/aesni-intel_asm.o: _aesni_inc_init(): can't find starting instruction
stackvalidate: arch/x86/crypto/aesni-intel_asm.o: aesni_ctr_enc()+0x15: call without frame pointer save/setup
...removed 6 lines...
stackvalidate: arch/x86/crypto/aesni-intel_asm.o: aesni_ctr_enc()+0x51: call without frame pointer save/setup
stackvalidate: arch/x86/crypto/aesni-intel_asm.o: aesni_xts_crypt8()+0xda: call without frame pointer save/setup
stackvalidate: arch/x86/crypto/aesni-intel_asm.o: aesni_xts_crypt8()+0x1f1: call without frame pointer save/setup
stackvalidate: arch/x86/crypto/aesni-intel_asm.o: _aesni_inc_init()+0x24: return instruction outside of a callable function
stackvalidate: arch/x86/crypto/ghash-clmulni-intel_asm.o: clmul_ghash_mul()+0x13: call without frame pointer save/setup
stackvalidate: arch/x86/crypto/ghash-clmulni-intel_asm.o: clmul_ghash_update()+0x28: call without frame pointer save/setup
stackvalidate: arch/x86/crypto/crc32c-pcl-intel-asm_64.o: crc_pcl()+0x11dd: can't decode instruction
stackvalidate: arch/x86/entry/entry_64.o: native_usergs_sysret64()+0x3: kernel entry/exit from callable instruction
stackvalidate: arch/x86/entry/entry_64.o: .entry.text+0x329: return instruction outside of a callable function
stackvalidate: arch/x86/entry/entry_64.o: .entry.text+0x1fe9: return instruction outside of a callable function
stackvalidate: arch/x86/entry/entry_64.o: .entry.text+0x2015: return instruction outside of a callable function
stackvalidate: arch/x86/entry/entry_64.o: .entry.text+0x2514: return instruction outside of a callable function
stackvalidate: arch/x86/entry/entry_64.o: .entry.text+0x2628: return instruction outside of a callable function
stackvalidate: arch/x86/entry/thunk_64.o: .text+0x53: return instruction outside of a callable function
stackvalidate: arch/x86/entry/entry_64_compat.o: native_usergs_sysret32()+0x3: kernel entry/exit from callable instruction
stackvalidate: arch/x86/entry/entry_64_compat.o: entry_SYSENTER_compat()+0xd: call without frame pointer save/setup
stackvalidate: arch/x86/entry/entry_64_compat.o: .entry.text+0x45f: sibling call from callable instruction with changed frame pointer
stackvalidate: arch/x86/entry/entry_64_compat.o: .entry.text+0x487: sibling call from callable instruction with changed frame pointer
stackvalidate: arch/x86/entry/entry_64_compat.o: entry_SYSENTER_compat()+0xd7: sibling call from callable instruction with changed frame pointer
stackvalidate: arch/x86/entry/entry_64_compat.o: entry_SYSENTER_compat()+0x179: sibling call from callable instruction with changed frame pointer
stackvalidate: arch/x86/entry/entry_64_compat.o: .entry.text+0x5de: return instruction outside of a callable function
stackvalidate: arch/x86/kernel/mcount_64.o: .entry.text+0x0: return instruction outside of a callable function
stackvalidate: arch/x86/kernel/mcount_64.o: .entry.text+0xbb: return instruction outside of a callable function
stackvalidate: arch/x86/kernel/mcount_64.o: .entry.text+0x2b7: return instruction outside of a callable function
stackvalidate: arch/x86/kernel/tsc.o: unsynchronized_tsc()+0x5f: call without frame pointer save/setup
stackvalidate: arch/x86/kernel/process.o: set_tsc_mode()+0x55: call without frame pointer save/setup
stackvalidate: arch/x86/kernel/acpi/wakeup_64.o: do_suspend_lowlevel()+0x6: call without frame pointer save/setup
stackvalidate: arch/x86/kernel/acpi/wakeup_64.o: do_suspend_lowlevel()+0x95: call without frame pointer save/setup
stackvalidate: arch/x86/kernel/cpu/common.o: debug_stack_reset()+0x70: call without frame pointer save/setup
stackvalidate: arch/x86/kernel/cpu/common.o: debug_stack_reset()+0x61: call without frame pointer save/setup
stackvalidate: arch/x86/kernel/cpu/common.o: debug_stack_reset()+0x31: call without frame pointer save/setup
stackvalidate: arch/x86/kernel/cpu/amd.o: .text+0x0: return instruction outside of a callable function
stackvalidate: arch/x86/kernel/cpu/mcheck/mce.o: mce_wrmsrl.constprop.30()+0x3f: call without frame pointer save/setup
stackvalidate: arch/x86/kernel/kprobes/core.o: kretprobe_trampoline_holder()+0x1d: duplicate frame pointer save
stackvalidate: arch/x86/kernel/kprobes/core.o: kprobe_exceptions_notify()+0x2d: call without frame pointer save/setup
stackvalidate: arch/x86/kernel/kprobes/core.o: kretprobe_trampoline_holder()+0x53: function has unreachable instruction
stackvalidate: arch/x86/kernel/kprobes/core.o: kretprobe_trampoline_holder()+0x54: function has unreachable instruction
stackvalidate: arch/x86/kernel/reboot.o: machine_real_restart()+0x5d: sibling call from callable instruction with changed frame pointer
stackvalidate: arch/x86/kernel/relocate_kernel_64.o: .text+0xd6: can't find call dest symbol at offset 0xdb
stackvalidate: arch/x86/kernel/paravirt.o: paravirt_end_context_switch()+0x3f: call without frame pointer save/setup
stackvalidate: arch/x86/kernel/paravirt.o: paravirt_end_context_switch()+0x37: call without frame pointer save/setup
stackvalidate: arch/x86/kernel/vsmp_64.o: .text+0x1d: return instruction outside of a callable function
stackvalidate: arch/x86/kernel/vsmp_64.o: .text+0x3b: return instruction outside of a callable function
stackvalidate: arch/x86/kernel/vsmp_64.o: .text+0x59: return instruction outside of a callable function
stackvalidate: arch/x86/kernel/vsmp_64.o: .text+0x77: return instruction outside of a callable function
stackvalidate: arch/x86/kernel/head_64.o: .text: unexpected end of section
stackvalidate: arch/x86/kernel/head_64.o: start_cpu0()+0x13: kernel entry/exit from callable instruction
stackvalidate: arch/x86/kernel/head_64.o: early_idt_handler_common()+0x59: call without frame pointer save/setup ...removed 5 lines...
stackvalidate: arch/x86/kernel/head_64.o: early_idt_handler_common()+0x42: call without frame pointer save/setup
stackvalidate: arch/x86/kernel/head_64.o: early_idt_handler_array()+0x9: function has unreachable instruction
...removed 140 lines...
stackvalidate: arch/x86/kernel/head_64.o: early_idt_handler_array()+0x11f: function has unreachable instruction
stackvalidate: arch/x86/mm/tlb.o: leave_mm()+0x5c: call without frame pointer save/setup
stackvalidate: arch/x86/mm/tlb.o: do_flush_tlb_all()+0x25: call without frame pointer save/setup
stackvalidate: arch/x86/mm/tlb.o: do_flush_tlb_all()+0x11: call without frame pointer save/setup
stackvalidate: arch/x86/net/bpf_jit.o: .text+0x18: return instruction outside of a callable function
...removed 8 lines...
stackvalidate: arch/x86/net/bpf_jit.o: .text+0x16d: return instruction outside of a callable function
stackvalidate: arch/x86/platform/efi/efi_stub_64.o: efi_call()+0x7c: call without frame pointer save/setup
stackvalidate: arch/x86/platform/uv/tlb_uv.o: ptc_seq_next()+0x1e: call without frame pointer save/setup
stackvalidate: arch/x86/xen/enlighten.o: xen_cpuid()+0x41: can't find jump dest instruction at .text+0x108
stackvalidate: arch/x86/xen/mmu.o: .text+0x1d: return instruction outside of a callable function
...removed 6 lines...
stackvalidate: arch/x86/xen/mmu.o: .text+0xef: return instruction outside of a callable function
stackvalidate: arch/x86/xen/irq.o: xen_halt()+0x0: call without frame pointer save/setup
stackvalidate: arch/x86/xen/irq.o: xen_halt()+0x1d: call without frame pointer save/setup
stackvalidate: arch/x86/xen/irq.o: .text+0x1d: return instruction outside of a callable function
stackvalidate: arch/x86/xen/irq.o: .text+0x3b: return instruction outside of a callable function
stackvalidate: arch/x86/xen/irq.o: .text+0x59: return instruction outside of a callable function
stackvalidate: arch/x86/xen/irq.o: .text+0x77: return instruction outside of a callable function
stackvalidate: arch/x86/xen/time.o: xen_vcpuop_set_mode()+0x36: call without frame pointer save/setup
stackvalidate: arch/x86/xen/time.o: xen_vcpuop_set_mode()+0x49: call without frame pointer save/setup
stackvalidate: arch/x86/xen/time.o: xen_vcpuop_set_mode()+0x1d: call without frame pointer save/setup
stackvalidate: arch/x86/xen/time.o: xen_timerop_set_mode()+0x26: call without frame pointer save/setup
stackvalidate: arch/x86/xen/xen-asm.o: .text+0x7f: return instruction outside of a callable function
stackvalidate: arch/x86/xen/xen-asm_64.o: .text+0xa: return instruction outside of a callable function
stackvalidate: kernel/softirq.o: __tasklet_hi_schedule_first()+0x5: call without frame pointer save/setup
stackvalidate: kernel/bpf/core.o: __bpf_prog_run()+0x5c: sibling call from callable instruction with changed frame pointer
stackvalidate: kernel/bpf/core.o: __bpf_prog_run()+0x60: function has unreachable instruction
...removed 1092 lines...
stackvalidate: kernel/bpf/core.o: __bpf_prog_run()+0x12dc: function has unreachable instruction
stackvalidate: kernel/locking/lockdep.o: trace_hardirqs_off_caller()+0x31: call without frame pointer save/setup
stackvalidate: kernel/sched/core.o: __schedule()+0x3e7: duplicate frame pointer save
stackvalidate: kernel/sched/core.o: __schedule()+0x424: sibling call from callable instruction with changed frame pointer
stackvalidate: kernel/sched/core.o: __schedule()+0x431: call without frame pointer save/setup
stackvalidate: kernel/sched/core.o: __schedule()+0x8b8: frame pointer state mismatch
stackvalidate: kernel/sched/core.o: __schedule()+0x447: frame pointer state mismatch
stackvalidate: kernel/sched/core.o: preempt_schedule_irq()+0x17: call without frame pointer save/setup
stackvalidate: kernel/sched/rt.o: set_cpus_allowed_rt()+0x1b: call without frame pointer save/setup
stackvalidate: kernel/irq_work.o: irq_work_run_list()+0x0: call without frame pointer save/setup
stackvalidate: mm/slub.o: check_slab()+0x5: call without frame pointer save/setup
stackvalidate: mm/huge_memory.o: split_huge_page_address()+0x30: call without frame pointer save/setup
stackvalidate: mm/huge_memory.o: split_huge_page_address()+0x6d: call without frame pointer save/setup
stackvalidate: fs/buffer.o: __find_get_block()+0x5: call without frame pointer save/setup
stackvalidate: drivers/xen/sys-hypervisor.o: pagesize_show()+0xf: call without frame pointer save/setup
stackvalidate: drivers/xen/sys-hypervisor.o: minor_show()+0xc: call without frame pointer save/setup
stackvalidate: drivers/xen/sys-hypervisor.o: major_show()+0xc: call without frame pointer save/setup
stackvalidate: arch/x86/power/hibernate_asm_64.o: .text+0x69: return instruction outside of a callable function
stackvalidate: arch/x86/power/hibernate_asm_64.o: .text+0x16d: return instruction outside of a callable function
stackvalidate: lib/percpu_counter.o: __percpu_counter_compare()+0x1e: call without frame pointer save/setup
stackvalidate: arch/x86/lib/rwsem.o: call_rwsem_down_read_failed()+0xf: call without frame pointer save/setup
stackvalidate: arch/x86/lib/rwsem.o: call_rwsem_down_write_failed()+0xe: call without frame pointer save/setup
stackvalidate: arch/x86/lib/rwsem.o: call_rwsem_wake()+0x12: call without frame pointer save/setup
stackvalidate: arch/x86/lib/rwsem.o: call_rwsem_downgrade_wake()+0xf: call without frame pointer save/setup
Signature · 1 line
Josh
Quoted 6 lines · Josh Poimboeuf
Quoted 1 line · Josh Poimboeuf
Posting a listing of reported warnings in a reply to this email.
These are the reported stackvalidate warnings on tip/master with my Fedora-based config. There were 1399 warnings in 26 .c files and 16 .S files. It was mostly repeat warnings so I removed a lot of the repeats to keep it short.
Are most of these warnings legit, what's the false positive rate in your impression?
You might want to start fixing a few typical types, just to see what it involves exactly and whether we want to fix it that way.
Thanks,
Ingo
Quoted 4 lines · Ingo Molnar
Quoted 1 line · Josh Poimboeuf
On Tue, Jul 14, 2015 at 12:14:06PM -0500, Josh Poimboeuf wrote:
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Posting a listing of reported warnings in a reply to this email.
These are the reported stackvalidate warnings on tip/master with my Fedora-based config. There were 1399 warnings in 26 .c files and 16 .S files. It was mostly repeat warnings so I removed a lot of the repeats to keep it short.
Are most of these warnings legit, what's the false positive rate in your impression?
Examining the warnings per-file (since that's a much easier analysis than per-warning):
Of the 26 .c files:
- 21 have real frame pointer bugs, most of them caused by a handful of inline asm() macros
- 5 have false positives (but several of these have questionable usage of asm() which might be convertible to C code)
Of the 16 .S files:
- 12 have real frame pointer bugs*, typically caused by not having FRAME/ENDFRAME (note: many of these files also have false positive warnings)
- 4 have no bugs and only false positives
Most of the false positives in the .S files are actually incorrect ELF annotations. I still consider them false positives because they're not "real" bugs, per se. But they're still fixable.
Overall I think there will end up being only be a handful of locations that need to use one of the whitelist macros (STACKVALIDATE_IGNORE_{INSN,FILE}).
Quoted 2 lines
You might want to start fixing a few typical types, just to see what it involves exactly and whether we want to fix it that way.
I already have a good idea of what needs to be done for most of the warnings. I'll post some patches and try to give a representive sampling of what the different types of fixes look like.
[*] But note that the definition of a frame pointer bug is open to
interpretation, especially for "special" asm code like suspend, hibernate, relocate, reboot, ftrace, bpf, entry code, etc. In general, when looking at whether frame pointers are needed, I asked myself "what would gcc do?"
Signature · 1 line
Josh
cover x86: Proposed fixes for stackvalidate warnings
In reply to Ingo MolnarThese patches fix many of the warnings reported by stackvalidate. They're based on top of the "Compile-time stack validation" v7 patch set [1].
They've been compile-tested and boot tested in a VM, but I haven't attempted any meaningful testing for most of them. This should give an idea of what kinds of changes I think are needed.
[1] https://lkml.kernel.org/r/[email protected]
Josh Poimboeuf (21):
stackvalidate: Process ignores earlier and add more ignore checks
stackvalidate: Add C version of STACKVALIDATE_IGNORE_INSN x86/asm: Add C versions of FRAME and ENDFRAME macros x86/hweight: Add stack frame dependency for __arch_hweight*() x86/xen: Add stack frame dependency to hypercall inline asm calls x86/paravirt: Add stack frame dependency to PVOP inline asm calls x86/paravirt: Fix frame pointer usage in PV_CALLEE_SAVE_REGS_THUNK x86/paravirt: Align paravirt thunk functions at 16-byte boundaries x86/amd: Set ELF function type for vide()
x86/reboot: Add ljmp instructions to stackvalidate whitelist x86/xen: Add xen_cpuid() and xen_setup_gdt() to stackvalidate
whitelists
sched: Add __schedule() to stackvalidate whitelist x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S x86/asm/crypto: Move .Lbswap_mask data to .rodata section x86/asm/crypto: Move jump_table to .rodata section x86/asm/crypto: Fix frame pointer usage in clmul_ghash_mul/update() x86/asm/entry: Fix frame pointer usage in thunk functions x86/asm/acpi: Fix frame pointer usage in do_suspend_lowlevel() x86/asm: Fix frame pointer usage in rwsem functions x86/asm/efi: Fix frame pointer usage in efi_call() x86/asm/power: Fix frame pointer usage in hibernate_asm_64.S
arch/x86/crypto/aesni-intel_asm.S | 21 +++++++++ arch/x86/crypto/crc32c-pcl-intel-asm_64.S | 8 ++-- arch/x86/crypto/ghash-clmulni-intel_asm.S | 5 ++ arch/x86/entry/thunk_64.S | 4 ++ arch/x86/include/asm/arch_hweight.h | 6 ++- arch/x86/include/asm/frame.h | 20 +++++++- arch/x86/include/asm/paravirt.h | 10 +++- arch/x86/include/asm/paravirt_types.h | 18 ++++---- arch/x86/include/asm/stackvalidate.h | 9 ++++ arch/x86/include/asm/xen/hypercall.h | 5 +- arch/x86/kernel/acpi/wakeup_64.S | 3 ++ arch/x86/kernel/cpu/amd.c | 5 +- arch/x86/kernel/reboot.c | 7 ++- arch/x86/lib/rwsem.S | 11 ++++- arch/x86/platform/efi/efi_stub_64.S | 3 ++ arch/x86/power/hibernate_asm_64.S | 7 +++ arch/x86/xen/enlighten.c | 4 +- kernel/sched/core.c | 2 + scripts/stackvalidate/stackvalidate.c | 77 ++++++++++++++++++++++--------- 19 files changed, 179 insertions(+), 46 deletions(-)
Signature · 1 line
2.1.0
RFC PATCH 1/21 stackvalidate: Process ignores earlier and add more ignore checks
Process the ignore macros earlier and associate them with their corresponding instructions. This allows ignores to be checked in get_jump_destinations() which is needed for some funky xen code which introduces fake instructions (XEN_EMULATE_PREFIX).
--- scripts/stackvalidate/stackvalidate.c | 77 +++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 21 deletions(-)
scripts/stackvalidate/stackvalidate.c +56 -21
@@ -49,7 +49,7 @@ struct instruction {unsigned int len, state;unsigned char type;unsigned long immediate; -bool alt_group, visited; +bool alt_group, visited, ignore;struct symbol *call_dest;struct instruction *jump_dest;struct list_head alts;
@@ -106,8 +106,7 @@ static struct argp argp = { options, parse_opt, args_doc, 0 };/* * Check for the STACKVALIDATE_IGNORE_INSN macro. */ -static bool ignore_insn (struct elf *elf,struct section *sec, -unsigned long offset) +static bool ignore_insn (struct elf *elf,struct instruction *insn) {struct section *macro_sec;struct rela *rela;
@@ -118,8 +117,8 @@ static bool ignore_insn(struct elf *elf, struct section *sec,list_for_each_entry (rela, ¯o_sec->rela->relas, list)if (rela->sym->type ==STT_SECTION && - rela->sym == sec->sym && - rela->addend == offset) + rela->sym == insn->sec->sym && + rela->addend == insn->offset)return true;return false;
@@ -138,9 +137,11 @@ static bool ignore_func(struct elf *elf, struct symbol *func)return false;list_for_each_entry (rela, ¯o_sec->rela->relas, list) -if (rela->sym == func) +if (rela->sym->sec == func->sec && + rela->addend == func->offset)return true; +return false; }
@@ -266,6 +267,44 @@ static int decode_instructions(struct elf *elf) }/* +* Warnings shouldn't be reported for ignored instructions. Set insn->ignore +* for each ignored instruction and each instruction contained in an ignored +* function. +*/ +static void get_ignores (struct elf *elf) +{ +struct instruction *insn; +struct section *sec; +struct symbol *func; + +list_for_each_entry (insn, &insns, list) + insn->ignore =ignore_insn (elf, insn); + +list_for_each_entry (sec, &elf->sections, list) { +list_for_each_entry (func, &sec->symbols, list) { +if (func->type !=STT_FUNC ) +continue ; + +if (!ignore_func (elf, func)) +continue ; + + insn =find_instruction (sec, func->offset); +if (!insn) +continue ; + +list_for_each_entry_from (insn, &insns, list) { +if (insn->sec != func->sec || + insn->offset >= func->offset + func->len) +break ; + + insn->ignore = true; + insn->visited = true; + } + } + } +} + +/* * Find the destination instructions for all jumps. */ static int get_jump_destinations (struct elf *elf)
@@ -300,7 +339,7 @@ static int get_jump_destinations(struct elf *elf) } insn->jump_dest =find_instruction (dest_sec, dest_off); -if (!insn->jump_dest) { +if (!insn->jump_dest && !insn->ignore) {/* * This is a special case where an alt instruction
@@ -591,6 +630,8 @@ static int decode_sections(struct elf *elf)if (ret)return ret; +get_ignores (elf); + ret =get_jump_destinations (elf);if (ret)return ret;
@@ -734,7 +775,8 @@ static int validate_branch(struct elf *elf, struct instruction *first,break ;case INSN_JUMP_DYNAMIC : -if (list_empty (&insn->alts) && insn->state) { +if (list_empty (&insn->alts) && insn->state && + !insn->ignore) {WARN ("%s: sibling call from callable instruction with changed frame pointer" ,offstr (sec, insn->offset)); warnings++;
@@ -743,9 +785,11 @@ static int validate_branch(struct elf *elf, struct instruction *first,return warnings;case INSN_CONTEXT_SWITCH : -WARN ("%s: kernel entry/exit from callable instruction" , -offstr (sec, insn->offset)); - warnings++; +if (!insn->ignore) { +WARN ("%s: kernel entry/exit from callable instruction" , +offstr (sec, insn->offset)); + warnings++; + }return warnings;
@@ -786,15 +830,6 @@ static int validate_functions(struct elf *elf)continue ; } -if (ignore_func (elf, func)) { -list_for_each_entry_from (insn, &insns, list) { -if (insn->sec != func->sec || - insn->offset >= func->offset + func->len) -break ; - insn->visited = true; - } - } - ret =validate_branch (elf, insn,0 ); warnings += ret; }
@@ -835,7 +870,7 @@ static int validate_uncallable_instructions(struct elf *elf)list_for_each_entry (insn, &insns, list) {if (!insn->visited && insn->type ==INSN_RETURN && - !ignore_insn (elf, insn->sec, insn->offset)) { + !insn->ignore) {WARN ("%s: return instruction outside of a callable function" ,offstr (insn->sec, insn->offset)); warnings++;
Signature · 1 line
2.1.0
RFC PATCH 2/21 stackvalidate: Add C version of STACKVALIDATE_IGNORE_INSN
In reply to Josh PoimboeufAdd a C inline asm string version of the STACKVALIDATE_IGNORE_INSN macro which tells stackvalidate to ignore the subsequent instruction.
--- arch/x86/include/asm/stackvalidate.h | 9 +++++++++ 1 file changed, 9 insertions(+)
arch/x86/include/asm/stackvalidate.h +9 -0
@@ -23,6 +23,15 @@ .endif .endm +#else /* !__ASSEMBLY__ */ + +#define STACKVALIDATE_IGNORE_INSN \ + ".Ltemp" __stringify(__LINE__) ":;" \ + ".pushsection __stackvalidate_ignore_insn, \"a\";" \ + _ASM_ALIGN ";" \ + ".long .Ltemp" __stringify(__LINE__) " - .;" \ + ".popsection;" +#endif /* __ASSEMBLY__ */ #endif /* _ASM_X86_STACKVALIDATE_H */
Signature · 1 line
2.1.0
Borislav Petkov
maintainerRe: [RFC PATCH 02/21] stackvalidate: Add C version of STACKVALIDATE_IGNORE_INSN
Quoted 13 lines · Josh Poimboeuf
Add a C inline asm string version of the
STACKVALIDATE_IGNORE_INSNmacro which tells stackvalidate to ignore the subsequent instruction.Signed-off-by Josh Poimboeuf <[email protected]>--- arch/x86/include/asm/stackvalidate.h | 9 +++++++++ 1 file changed, 9 insertions(+)arch/x86/include/asm/stackvalidate.h +0
-0
arch/x86/include/asm/stackvalidate.h line 27
Quoted 5 lines
arch/x86/include/asm/stackvalidate.h +2
-0@@ -23,6 +23,15 @@ .endif .endm +#else /* !__ASSEMBLY__ */ +
#ifdef CONFIG_STACK_VALIDATION
arch/x86/include/asm/stackvalidate.h line 34
Quoted 7 lines
arch/x86/include/asm/stackvalidate.h +7
-0+#define STACKVALIDATE_IGNORE_INSN \ + ".Ltemp" __stringify(__LINE__) ":;" \ + ".pushsection __stackvalidate_ignore_insn, \"a\";" \ + _ASM_ALIGN ";" \ + ".long .Ltemp" __stringify(__LINE__) " - .;" \ + ".popsection;" +
#endif
Also, you should end your lines with "\n" so that the .s output looks a bit more readable, not like now:
#APP
# 30 "./arch/x86/include/asm/arch_hweight.h" 1
661:
.Ltemp32:;.pushsection __stackvalidate_ignore_insn, "a"; .balign 8 ;.long .Ltemp32 - .;.popsection;call __sw_hweight32662:
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--Re: [RFC PATCH 02/21] stackvalidate: Add C version of STACKVALIDATE_IGNORE_INSN
Quoted 33 lines · Borislav Petkov
Quoted 13 lines · Josh Poimboeuf
Add a C inline asm string version of the
STACKVALIDATE_IGNORE_INSNmacro which tells stackvalidate to ignore the subsequent instruction.Signed-off-by Josh Poimboeuf <[email protected]>--- arch/x86/include/asm/stackvalidate.h | 9 +++++++++ 1 file changed, 9 insertions(+)arch/x86/include/asm/stackvalidate.h +0
-0arch/x86/include/asm/stackvalidate.h line 27
Quoted 5 lines
arch/x86/include/asm/stackvalidate.h +2
-0@@ -23,6 +23,15 @@ .endif .endm +#else /* !__ASSEMBLY__ */ +#ifdef
CONFIG_STACK_VALIDATIONarch/x86/include/asm/stackvalidate.h line 34
Quoted 7 lines
arch/x86/include/asm/stackvalidate.h +7
-0+#define STACKVALIDATE_IGNORE_INSN \ + ".Ltemp" __stringify(__LINE__) ":;" \ + ".pushsection __stackvalidate_ignore_insn, \"a\";" \ + _ASM_ALIGN ";" \ + ".long .Ltemp" __stringify(__LINE__) " - .;" \ + ".popsection;" +#endif
Quoted 8 lines
Also, you should end your lines with "\n" so that the .s output looks a bit more readable, not like now:
#APP
# 30 "./arch/x86/include/asm/arch_hweight.h" 1661: .Ltemp32:;.pushsection __stackvalidate_ignore_insn, "a"; .balign 8 ;.long .Ltemp32 - .;.popsection;call __sw_hweight32662:
Ok, will fix both issues. Thanks.
Signature · 1 line
Josh
A message that is not in the archive (sent off-list or not yet received): CA+55aFyoO75n-mQBrB_YBLx9yNpAjisFAqkO8+YsphD-xmgY+w@mail.gmail.com
Re: [RFC PATCH 02/21] stackvalidate: Add C version of STACKVALIDATE_IGNORE_INSN
Quoted 18 lines · Linus Torvalds
arch/x86/include/asm/stackvalidate.h line 29
Quoted 4 lines · Josh Poimboeuf
arch/x86/include/asm/stackvalidate.h +3
-0+ +#define STACKVALIDATE_IGNORE_INSN \ + ".Ltemp" __stringify(__LINE__) ":;" \This is wrong. It won't work for people who do multiple of these on the same line (think macros etc).
For temporary labels, you should just use numeric labels. Think Pascal style ones. Then use "b" or "f" after the number when referring to it to say "back" or "forward" reference.
So the code for an endless loop with a true temporary label looks like:
1: code
jmp 1b
Quoted 8 lines
and a branch over would look like
jne 7f .. code ... 7: ...and now you can have multiple of these truly temp labels without ever getting errors from having a label redefined.
Yeah, I think I'll change the C macro to do that.
However for asm macros you have to worry about collisions. For example:
.macro FOO
1: ...
.endm
1: ...
FOO
jmp 1b
It would jump to "1" in the macro instead of the "1" before the macro.
So for the asm version of the macro I ended up using Andy's idea of using "\@" to get a unique identifier:
.macro STACKVALIDATE_IGNORE_INSN
.if CONFIG_STACK_VALIDATION .Ltemp_\@: .pushsection __stackvalidate_ignore_insn, "a" _ASM_ALIGN .long .Ltemp_\@ - . .popsection .endif
.endm
Signature · 1 line
Josh
RFC PATCH 3/21 x86/asm: Add C versions of FRAME and ENDFRAME macros
In reply to Josh PoimboeufAdd C versions of the FRAME and ENDFRAME macros which can be used to create a stack frame in inline assembly.
--- arch/x86/include/asm/frame.h | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)
arch/x86/include/asm/frame.h +18 -2
@@ -1,10 +1,10 @@#ifndef _ASM_X86_FRAME_H#define _ASM_X86_FRAME_H -#ifdef __ASSEMBLY__ -#include <asm/asm.h> +#ifdef __ASSEMBLY__ +/* * These are stack frame creation macros. They should be used by every * callable non-leaf asm function to make kernel stack traces more reliable.
@@ -22,5 +22,21 @@ .endif .endm +#else /* !__ASSEMBLY__ */ + +#ifdef CONFIG_FRAME_POINTER + +#define FRAME \ + "push %" _ASM_BP ";" \ + _ASM_MOV "%" _ASM_SP ", %" _ASM_BP ";" + +#define ENDFRAME "pop %" _ASM_BP ";" + +#else /* !CONFIG_FRAME_POINTER */ + +#define FRAME "" +#define ENDFRAME "" + +#endif /* CONFIG_FRAME_POINTER */ #endif /* __ASSEMBLY__ */ #endif /* _ASM_X86_FRAME_H */
Signature · 1 line
2.1.0
RFC PATCH 4/21 x86/hweight: Add stack frame dependency for __arch_hweight*()
In reply to Josh PoimboeufIf __arch_hweight32() or __arch_hweight64() is inlined at the beginning of a function, gcc can insert the call instruction before setting up a stack frame, which breaks frame pointer convention if CONFIG_FRAME_POINTER is enabled and can result in a bad stack trace.
Force a stack frame to be created if CONFIG_FRAME_POINTER is enabled by listing the stack pointer as an output operand for the inline asm statement.
--- arch/x86/include/asm/arch_hweight.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
arch/x86/include/asm/arch_hweight.h +4 -2
@@ -23,10 +23,11 @@*/ static inline unsigned int __arch_hweight32 (unsigned int w) { + registervoid *__sp asm("esp" );unsigned int res =0 ; asm (ALTERNATIVE("call __sw_hweight32", POPCNT32, X86_FEATURE_POPCNT) - : "="REG_OUT (res) + : "="REG_OUT (res), "+r" (__sp) : REG_IN (w));return res;
@@ -44,6 +45,7 @@ static inline unsigned int __arch_hweight8(unsigned int w)static inline unsigned long __arch_hweight64 (__u64 w) { + registervoid __maybe_unused *__sp asm("rsp" );unsigned long res =0 ;#ifdef CONFIG_X86_32
@@ -51,7 +53,7 @@ static inline unsigned long __arch_hweight64(__u64 w)__arch_hweight32 ((u32)(w >>32 ));#else asm (ALTERNATIVE("call __sw_hweight64", POPCNT64, X86_FEATURE_POPCNT) - : "="REG_OUT (res) + : "="REG_OUT (res), "+r" (__sp) : REG_IN (w));#endif /* CONFIG_X86_32 */
Signature · 1 line
2.1.0
Borislav Petkov
maintainerRe: [RFC PATCH 04/21] x86/hweight: Add stack frame dependency for __arch_hweight*()
Quoted 40 lines · Josh Poimboeuf
If
__arch_hweight32()or__arch_hweight64()is inlined at the beginning of a function, gcc can insert the call instruction before setting up a stack frame, which breaks frame pointer convention ifCONFIG_FRAME_POINTERis enabled and can result in a bad stack trace.Force a stack frame to be created if
CONFIG_FRAME_POINTERis enabled by listing the stack pointer as an output operand for the inline asm statement.Signed-off-by Josh Poimboeuf <[email protected]>--- arch/x86/include/asm/arch_hweight.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)arch/x86/include/asm/arch_hweight.h +3
-1@@ -23,10 +23,11 @@*/ static inline unsigned int __arch_hweight32 (unsigned int w) { + registervoid *__sp asm("esp" );unsigned int res =0 ; asm (ALTERNATIVE("call __sw_hweight32", POPCNT32, X86_FEATURE_POPCNT) - : "="REG_OUT (res) + : "="REG_OUT (res), "+r" (__sp) : REG_IN (w));return res;@@ -44,6 +45,7 @@ static inline unsigned int __arch_hweight8(unsigned int w)static inline unsigned long __arch_hweight64 (__u64 w) { + registervoid __maybe_unused *__sp asm("rsp" );unsigned long res =0 ;#ifdef CONFIG_X86_32
arch/x86/include/asm/arch_hweight.h static inline unsigned long __arch_hweight64(__u64 w) · line 58
Quoted 7 lines
arch/x86/include/asm/arch_hweight.h +1
-1@@ -51,7 +53,7 @@ static inline unsigned long __arch_hweight64(__u64 w)__arch_hweight32 ((u32)(w >>32 ));#else asm (ALTERNATIVE("call __sw_hweight64", POPCNT64, X86_FEATURE_POPCNT) - : "="REG_OUT (res) + : "="REG_OUT (res), "+r" (__sp) : REG_IN (w));#endif /* CONFIG_X86_32 */
Eeew, useless code so that some compile-time validation is done. Let's not add this clutter please.
In this particular case, the majority of CPUs out there will get POPCNT patched in and that CALL is gone. And for the remaining cases where we do end up using the __sw_* variants, I'd prefer to rather not do the validation instead of polluting the code with that fake rsp dependency.
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--Re: [RFC PATCH 04/21] x86/hweight: Add stack frame dependency for __arch_hweight*()
Quoted 52 lines · Borislav Petkov
Quoted 40 lines · Josh Poimboeuf
If
__arch_hweight32()or__arch_hweight64()is inlined at the beginning of a function, gcc can insert the call instruction before setting up a stack frame, which breaks frame pointer convention ifCONFIG_FRAME_POINTERis enabled and can result in a bad stack trace.Force a stack frame to be created if
CONFIG_FRAME_POINTERis enabled by listing the stack pointer as an output operand for the inline asm statement.Signed-off-by Josh Poimboeuf <[email protected]>--- arch/x86/include/asm/arch_hweight.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)arch/x86/include/asm/arch_hweight.h +3
-1@@ -23,10 +23,11 @@*/ static inline unsigned int __arch_hweight32 (unsigned int w) { + registervoid *__sp asm("esp" );unsigned int res =0 ; asm (ALTERNATIVE("call __sw_hweight32", POPCNT32, X86_FEATURE_POPCNT) - : "="REG_OUT (res) + : "="REG_OUT (res), "+r" (__sp) : REG_IN (w));return res;@@ -44,6 +45,7 @@ static inline unsigned int __arch_hweight8(unsigned int w)static inline unsigned long __arch_hweight64 (__u64 w) { + registervoid __maybe_unused *__sp asm("rsp" );unsigned long res =0 ;#ifdef CONFIG_X86_32 arch/x86/include/asm/arch_hweight.h static inline unsigned long __arch_hweight64(__u64 w) · line 58
Quoted 7 lines
arch/x86/include/asm/arch_hweight.h +1
-1@@ -51,7 +53,7 @@ static inline unsigned long __arch_hweight64(__u64 w)__arch_hweight32 ((u32)(w >>32 ));#else asm (ALTERNATIVE("call __sw_hweight64", POPCNT64, X86_FEATURE_POPCNT) - : "="REG_OUT (res) + : "="REG_OUT (res), "+r" (__sp) : REG_IN (w));#endif /* CONFIG_X86_32 */ Eeew, useless code so that some compile-time validation is done. Let's not add this clutter please.
Quoted 4 lines
In this particular case, the majority of CPUs out there will get POPCNT patched in and that CALL is gone. And for the remaining cases where we do end up using the __sw_* variants, I'd prefer to rather not do the validation instead of polluting the code with that fake rsp dependency.
Well, but this isn't some whitelist code to make stackvalidate happy.
It's actually a real runtime frame pointer bug, and the rsp dependency is real. If it does the call without first creating the stack frame then it breaks frame pointer based stack traces.
Signature · 1 line
Josh
Borislav Petkov
maintainerRe: [RFC PATCH 04/21] x86/hweight: Add stack frame dependency for __arch_hweight*()
Quoted 5 lines · Josh Poimboeuf
Well, but this isn't some whitelist code to make stackvalidate happy.
It's actually a real runtime frame pointer bug, and the rsp dependency is real. If it does the call without first creating the stack frame then it breaks frame pointer based stack traces.
I think we can live with the stack trace being a little wrong in those __sw_* variants. And besides, we're talking about the very very small percentage of machines (which keeps getting smaller) which don't support POPCNT. And from those, only for the cases where the arg is not __builtin_constant_p() because there we do the __const_hweight* thing.
I'd prefer to not clutter the code more in that case.
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--Re: [RFC PATCH 04/21] x86/hweight: Add stack frame dependency for __arch_hweight*()
Quoted 7 lines · Borislav Petkov
Quoted 5 lines · Josh Poimboeuf
Well, but this isn't some whitelist code to make stackvalidate happy.
It's actually a real runtime frame pointer bug, and the rsp dependency is real. If it does the call without first creating the stack frame then it breaks frame pointer based stack traces.
Quoted 7 lines
I think we can live with the stack trace being a little wrong in those __sw_* variants. And besides, we're talking about the very very small percentage of machines (which keeps getting smaller) which don't support POPCNT. And from those, only for the cases where the arg is not
__builtin_constant_p()because there we do the __const_hweight* thing.I'd prefer to not clutter the code more in that case.
Ok, so would you rather adding a whitelist to tell stackvalidate to ignore it? Something like this?
arch/x86/include/asm/arch_hweight.h +8 -2
@@ -1,6 +1,8 @@#ifndef _ASM_X86_HWEIGHT_H#define _ASM_X86_HWEIGHT_H +#include <linux/stackvalidate.h> +#ifdef CONFIG_64BIT /* popcnt %edi, %eax -- redundant REX prefix for alignment */ #define POPCNT32 ".byte 0xf3,0x40,0x0f,0xb8,0xc7"
@@ -25,7 +27,9 @@ static inline unsigned int __arch_hweight32(unsigned int w) {unsigned int res =0 ; - asm (ALTERNATIVE("call __sw_hweight32", POPCNT32, X86_FEATURE_POPCNT) + asm (ALTERNATIVE(STACKVALIDATE_IGNORE_INSN + "call __sw_hweight32", + POPCNT32, X86_FEATURE_POPCNT) : "="REG_OUT (res) : REG_IN (w));
@@ -50,7 +54,9 @@ static inline unsigned long __arch_hweight64(__u64 w)return __arch_hweight32 ((u32 )w) +__arch_hweight32 ((u32)(w >>32 ));#else - asm (ALTERNATIVE("call __sw_hweight64", POPCNT64, X86_FEATURE_POPCNT) + asm (ALTERNATIVE(STACKVALIDATE_IGNORE_INSN + "call __sw_hweight64", + POPCNT64, X86_FEATURE_POPCNT) : "="REG_OUT (res) : REG_IN (w));#endif /* CONFIG_X86_32 */
Signature · 1 line
Josh
Borislav Petkov
maintainerRe: [RFC PATCH 04/21] x86/hweight: Add stack frame dependency for __arch_hweight*()
Quoted 2 lines · Josh Poimboeuf
Ok, so would you rather adding a whitelist to tell stackvalidate to ignore it? Something like this?
I tried it and maybe I'm missing something but that doesn't work:
$ make drivers/gpu/drm/i915/intel_ringbuffer.o CHK include/config/kernel.release CHK include/generated/uapi/linux/version.h CHK include/generated/utsrelease.h CHK include/generated/timeconst.h CHK include/generated/bounds.h CHK include/generated/asm-offsets.h CALL scripts/checksyscalls.sh CC drivers/gpu/drm/i915/intel_ringbuffer.o ./arch/x86/include/asm/arch_hweight.h: Assembler messages: ./arch/x86/include/asm/arch_hweight.h:31: Error: symbol `.Ltemp32' is already defined ./arch/x86/include/asm/arch_hweight.h:31: Error: symbol `.Ltemp32' is already defined ./arch/x86/include/asm/arch_hweight.h:31: Error: symbol `.Ltemp32' is already defined scripts/Makefile.build:258: recipe for target 'drivers/gpu/drm/i915/intel_ringbuffer.o' failed make[1]: *** [drivers/gpu/drm/i915/intel_ringbuffer.o] Error 1 Makefile:1528: recipe for target 'drivers/gpu/drm/i915/intel_ringbuffer.o' failed make: *** [drivers/gpu/drm/i915/intel_ringbuffer.o] Error 2
Also, that label temp32 could be more descriptive.
Regardless of the above, I don't like the idea of adding some compile-time checking and thus obfuscating what is already non-obvious code.
And since your tool is already parsing ELF files and all that other fun, what I'd do is make that checking out-of-line *without* adding any new code to the kernel.
In this particular case, you have:
#APP
# 28 "./arch/x86/include/asm/arch_hweight.h" 1
661:
call __sw_hweight32662:
.skip -(((6651f-6641f)-(662b-661b)) > 0) * ((6651f-6641f)-(662b-661b)),0x90 663:
.pushsection .altinstructions,"a"
.long 661b - .
.long 6641f - .
.word ( 4*32+23)
.byte 663b-661b
.byte 6651f-6641f
.byte 663b-662b
.popsection
.pushsection .altinstr_replacement, "ax"
6641:
.byte 0xf3,0x40,0x0f,0xb8,0xc7
6651:
.popsection
# 0 "" 2
so you see that a CALL instruction gets replaced with a POPCNT and the feature bit used is 4*32+23 which is X86_FEATURE_POPCNT. This information is enough to detect that particular case and add the offset ".long 661b - ." to the list of instructions which stackvalidate should ignore.
Anyway, this is what I'd do.
IMNSVHO, we must be very conservative and not add some markers/helpers/etc to code only so that tools can do their job. Not if it can be helped. Instead, tools should do the hard work and we should keep kernel code clean.
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--Re: [RFC PATCH 04/21] x86/hweight: Add stack frame dependency for __arch_hweight*()
Quoted 15 lines · Borislav Petkov
Quoted 2 lines · Josh Poimboeuf
Ok, so would you rather adding a whitelist to tell stackvalidate to ignore it? Something like this?
I tried it and maybe I'm missing something but that doesn't work:
$ make drivers/gpu/drm/i915/intel_ringbuffer.o CHK include/config/kernel.release CHK include/generated/uapi/linux/version.h CHK include/generated/utsrelease.h CHK include/generated/timeconst.h CHK include/generated/bounds.h CHK include/generated/asm-offsets.h CALL scripts/checksyscalls.sh CC drivers/gpu/drm/i915/intel_ringbuffer.o
Quoted 8 lines
./arch/x86/include/asm/arch_hweight.h: Assembler messages: ./arch/x86/include/asm/arch_hweight.h:31: Error: symbol `.Ltemp32' is already defined ./arch/x86/include/asm/arch_hweight.h:31: Error: symbol `.Ltemp32' is already defined ./arch/x86/include/asm/arch_hweight.h:31: Error: symbol `.Ltemp32' is already defined scripts/Makefile.build:258: recipe for target 'drivers/gpu/drm/i915/intel_ringbuffer.o' failed make[1]: *** [drivers/gpu/drm/i915/intel_ringbuffer.o] Error 1 Makefile:1528: recipe for target 'drivers/gpu/drm/i915/intel_ringbuffer.o' failed make: *** [drivers/gpu/drm/i915/intel_ringbuffer.o] Error 2
Yeah, it doesn't actually support this particular example yet. I was just trying to figure out if that's what you were proposing.
Quoted 1 line
Also, that label temp32 could be more descriptive.
Yeah, that's from:
".Ltemp" __stringify(__LINE__) ":;"
Which was intended to give a unique ID for each use of the macro, but apparently that didn't work as planned here.
Quoted 5 lines
so you see that a CALL instruction gets replaced with a POPCNT and the feature bit used is 4*32+23 which is
X86_FEATURE_POPCNT. This information is enough to detect that particular case and add the offset ".long 661b - ." to the list of instructions which stackvalidate should ignore.
Currently, when stackvalidate sees an ALTERNATIVE, it assumes that either code path is possible, so it follows both paths in parallel.
If I understand right, you're proposing that stackvalidate should only follow the POPCNT path and never follow the !POPCNT path?
Quoted 6 lines
Anyway, this is what I'd do.
IMNSVHO, we must be very conservative and not add some markers/helpers/etc to code only so that tools can do their job. Not if it can be helped. Instead, tools should do the hard work and we should keep kernel code clean.
In general, I agree, and I like the original patch much better. IMO, it achieved the goal of keeping the kernel code clean, while fixing the frame pointer bug.
If you insist on breaking stack traces on !POPCNT, I can probably add some intelligence to stackvalidate to look for !POPCNT and ignore it. It seems less "clean" to me, though.
Signature · 1 line
Josh
Borislav Petkov
maintainerRe: [RFC PATCH 04/21] x86/hweight: Add stack frame dependency for __arch_hweight*()
Quoted 5 lines · Josh Poimboeuf
Currently, when stackvalidate sees an ALTERNATIVE, it assumes that either code path is possible, so it follows both paths in parallel.
If I understand right, you're proposing that stackvalidate should only follow the POPCNT path and never follow the !POPCNT path?
Actually, you don't even need to follow the POPCNT case either because it is a single instruction - no stack operations there.
So yeah, either that or special-case the case where the original insn is CALL and the replacement is a POPCNT and ignore those CALL locations.
The advantage is that the burden is put on the tool and not by adding markers to kernel code paths.
Quoted 3 lines
In general, I agree, and I like the original patch much better. IMO, it achieved the goal of keeping the kernel code clean, while fixing the frame pointer bug.
And I think that in that case, adding that rSP dependency is too much because even though it fixes the "bug", it is very very unlikely any stack trace will have __sw_hweight* in it for reasons pointed out earlier and also because those functions can't fail and they get integral types as args which can't fail when deref-fing either. And even if they do, they don't call any other functions so rIP pointing to them is already enough.
So even if we're not 100% correct wrt stack traces in this case, I think that's ok.
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--Re: [RFC PATCH 04/21] x86/hweight: Add stack frame dependency for __arch_hweight*()
Quoted 21 lines · Borislav Petkov
Quoted 5 lines · Josh Poimboeuf
Currently, when stackvalidate sees an ALTERNATIVE, it assumes that either code path is possible, so it follows both paths in parallel.
If I understand right, you're proposing that stackvalidate should only follow the POPCNT path and never follow the !POPCNT path?
Actually, you don't even need to follow the POPCNT case either because it is a single instruction - no stack operations there.
So yeah, either that or special-case the case where the original insn is CALL and the replacement is a POPCNT and ignore those CALL locations.
The advantage is that the burden is put on the tool and not by adding markers to kernel code paths.
Quoted 3 lines
In general, I agree, and I like the original patch much better. IMO, it achieved the goal of keeping the kernel code clean, while fixing the frame pointer bug.
Quoted 7 lines
And I think that in that case, adding that rSP dependency is too much because even though it fixes the "bug", it is very very unlikely any stack trace will have __sw_hweight* in it for reasons pointed out earlier and also because those functions can't fail and they get integral types as args which can't fail when deref-fing either. And even if they do, they don't call any other functions so rIP pointing to them is already enough.
Enough for oopses, perhaps, but maybe not enough for perf.
It sounds like you want CFI unwinding :)
--Andy
Borislav Petkov
maintainerRe: [RFC PATCH 04/21] x86/hweight: Add stack frame dependency for __arch_hweight*()
Quoted 3 lines · Andy Lutomirski
Enough for oopses, perhaps, but maybe not enough for perf.
It sounds like you want CFI unwinding :)
What would you want to unwind in those __sw_hweight* almost-trivial, tail functions?
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--Re: [RFC PATCH 04/21] x86/hweight: Add stack frame dependency for __arch_hweight*()
Quoted 7 lines · Borislav Petkov
Quoted 3 lines · Andy Lutomirski
Enough for oopses, perhaps, but maybe not enough for perf.
It sounds like you want CFI unwinding :)
What would you want to unwind in those __sw_hweight* almost-trivial, tail functions?
So your shiny perf profile shows cumulative time in whatever called them. Sure, this is arguably silly if we're stuck with frame pointers.
--Andy
Borislav Petkov
maintainerRe: [RFC PATCH 04/21] x86/hweight: Add stack frame dependency for __arch_hweight*()
Quoted 3 lines · Andy Lutomirski
So your shiny perf profile shows cumulative time in whatever called them. Sure, this is arguably silly if we're stuck with frame pointers.
You can count 20ish cycles tops for any one of them.
At least that was from the last time when comparing POPCNT perf to the __sw_hweight* ones. IOW, POPCNT didn't show any improvement vs those sw versions.
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--RFC PATCH 5/21 x86/xen: Add stack frame dependency to hypercall inline asm calls
In reply to Josh PoimboeufIf a hypercall is inlined at the beginning of a function, gcc can insert the call instruction before setting up a stack frame, which breaks frame pointer convention if CONFIG_FRAME_POINTER is enabled and can result in a bad stack trace.
Force a stack frame to be created if CONFIG_FRAME_POINTER is enabled by listing the stack pointer as an output operand for the hypercall inline asm statements.
--- arch/x86/include/asm/xen/hypercall.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/include/asm/xen/hypercall.h +3 -2
@@ -110,9 +110,10 @@ extern struct { char _entry[32]; } hypercall_page[]; registerunsigned long __arg2 asm (__HYPERCALL_ARG2REG ) = __arg2; \ registerunsigned long __arg3 asm (__HYPERCALL_ARG3REG ) = __arg3; \ registerunsigned long __arg4 asm (__HYPERCALL_ARG4REG ) = __arg4; \ - registerunsigned long __arg5 asm (__HYPERCALL_ARG5REG ) = __arg5; + registerunsigned long __arg5 asm (__HYPERCALL_ARG5REG ) = __arg5; \ + register void *__sp asm(_ASM_SP); -#define __HYPERCALL_0PARAM "=r" (__res) +#define __HYPERCALL_0PARAM "=r" (__res), "+r" (__sp)#define __HYPERCALL_1PARAM __HYPERCALL_0PARAM, "+r" (__arg1)#define __HYPERCALL_2PARAM __HYPERCALL_1PARAM, "+r" (__arg2)#define __HYPERCALL_3PARAM __HYPERCALL_2PARAM, "+r" (__arg3)
Signature · 1 line
2.1.0
RFC PATCH 6/21 x86/paravirt: Add stack frame dependency to PVOP inline asm calls
In reply to Josh PoimboeufIf a PVOP call macro is inlined at the beginning of a function, gcc can insert the call instruction before setting up a stack frame, which breaks frame pointer convention if CONFIG_FRAME_POINTER is enabled and can result in a bad stack trace.
Force a stack frame to be created by listing the stack pointer as an output operand for the PVOP inline asm statements.
--- arch/x86/include/asm/paravirt_types.h | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)
arch/x86/include/asm/paravirt_types.h +10 -8
@@ -497,8 +497,9 @@ int paravirt_disable_iospace(void);* makes sure the incoming and outgoing types are always correct. */ #ifdef CONFIG_X86_32 -#define PVOP_VCALL_ARGS \ - unsigned long __eax = __eax, __edx = __edx, __ecx = __ecx +#define PVOP_VCALL_ARGS \ + unsigned long __eax = __eax, __edx = __edx, __ecx = __ecx; \ + register void *__sp asm("esp")#define PVOP_CALL_ARGS PVOP_VCALL_ARGS#define PVOP_CALL_ARG1 (x) "a" ((unsigned long)(x))
@@ -516,9 +517,10 @@ int paravirt_disable_iospace(void);#define VEXTRA_CLOBBERS #else /* CONFIG_X86_64 */ /* [re]ax isn't an arg, but the return val */ -#define PVOP_VCALL_ARGS \ - unsigned long __edi = __edi, __esi = __esi, \ - __edx = __edx, __ecx = __ecx, __eax = __eax +#define PVOP_VCALL_ARGS \ + unsigned long __edi = __edi, __esi = __esi, \ + __edx = __edx, __ecx = __ecx, __eax = __eax; \ + register void *__sp asm("rsp")#define PVOP_CALL_ARGS PVOP_VCALL_ARGS#define PVOP_CALL_ARG1 (x) "D" ((unsigned long)(x))
@@ -557,7 +559,7 @@ int paravirt_disable_iospace(void); asm volatile(pre \ paravirt_alt(PARAVIRT_CALL) \ post \ - : call_clbr \ + : "+r" (__sp), call_clbr \ :paravirt_type (op), \paravirt_clobber (clbr), \ ##__VA_ARGS__ \
@@ -567,7 +569,7 @@ int paravirt_disable_iospace(void); asm volatile(pre \ paravirt_alt(PARAVIRT_CALL) \ post \ - : call_clbr \ + : "+r" (__sp), call_clbr \ :paravirt_type (op), \paravirt_clobber (clbr), \ ##__VA_ARGS__ \
@@ -594,7 +596,7 @@ int paravirt_disable_iospace(void); asm volatile(pre \ paravirt_alt(PARAVIRT_CALL) \ post \ - : call_clbr \ + : "+r" (__sp), call_clbr \ :paravirt_type (op), \paravirt_clobber (clbr), \ ##__VA_ARGS__ \
Signature · 1 line
2.1.0
RFC PATCH 7/21 x86/paravirt: Fix frame pointer usage in PV_CALLEE_SAVE_REGS_THUNK
In reply to Josh PoimboeufA function created with the PV_CALLEE_SAVE_REGS_THUNK macro doesn't set up a new stack frame before the call instruction, which breaks frame pointer convention if CONFIG_FRAME_POINTER is enabled and can result in a bad stack trace. Also, the thunk functions aren't annotated as ELF callable functions.
Create a stack frame when CONFIG_FRAME_POINTER is enabled and add the ELF function type.
--- arch/x86/include/asm/paravirt.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
arch/x86/include/asm/paravirt.h +7 -2
@@ -13,6 +13,7 @@#include <linux/bug.h> #include <linux/types.h> #include <linux/cpumask.h> +#include <asm/frame.h> static inline int paravirt_enabled (void ) {
@@ -772,15 +773,19 @@ static __always_inline void __ticket_unlock_kick(struct arch_spinlock *lock,* call. The return value in rax/eax will not be saved, even for void * functions. */ +#define PV_THUNK_NAME (func) "__raw_callee_save_" #func#define PV_CALLEE_SAVE_REGS_THUNK (func) \ extern typeof(func) __raw_callee_save_##func; \ \ asm(".pushsection .text;" \ - ".globl __raw_callee_save_" #func " ; " \ - "__raw_callee_save_" #func ": " \ + ".globl " PV_THUNK_NAME(func) ";" \ + ".type " PV_THUNK_NAME(func) ", @function;" \ + PV_THUNK_NAME(func) ":" \ + FRAME \ PV_SAVE_ALL_CALLER_REGS \ "call " #func ";" \ PV_RESTORE_ALL_CALLER_REGS \ + ENDFRAME \ "ret;" \ ".popsection")
Signature · 1 line
2.1.0
RFC PATCH 8/21 x86/paravirt: Align paravirt thunk functions at 16-byte boundaries
In reply to Josh PoimboeufParavirt thunk functions aren't aligned, which can impact performance and is inconsistent with gcc-generated functions.
Align them at 16-byte boundaries to be consistent with gcc functions.
--- arch/x86/include/asm/paravirt.h | 1 + 1 file changed, 1 insertion(+)
arch/x86/include/asm/paravirt.h +1 -0
@@ -778,6 +778,7 @@ static __always_inline void __ticket_unlock_kick(struct arch_spinlock *lock, extern typeof(func) __raw_callee_save_##func; \ \ asm(".pushsection .text;" \ + ".align 16;" \ ".globl " PV_THUNK_NAME(func) ";" \ ".type " PV_THUNK_NAME(func) ", @function;" \ PV_THUNK_NAME(func) ":" \
Signature · 1 line
2.1.0
RFC PATCH 9/21 x86/amd: Set ELF function type for vide()
In reply to Josh Poimboeufvide() is a callable function, but is missing the ELF function type, which confuses tools like stackvalidate.
Properly annotate it to be a callable function. The generated code is unchanged.
--- arch/x86/kernel/cpu/amd.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/amd.c +4 -1
@@ -74,7 +74,10 @@ static inline int wrmsrl_amd_safe(unsigned msr, unsigned long long val)*/ extern __visible void vide(void); -__asm__(".globl vide\n\t.align 4\nvide: ret" ); +__asm__(".globl vide;" +".type vide, @function;" +".align 4;" +"vide: ret;" );static void init_amd_k5 (struct cpuinfo_x86 *c) {
Signature · 1 line
2.1.0
RFC PATCH 10/21 x86/reboot: Add ljmp instructions to stackvalidate whitelist
In reply to Josh Poimboeufstackvalidate reports a false positive warning for the ljmp instruction in machine_real_restart(). Normally, ljmp isn't allowed in a function, but this is a special case where it's jumping into real mode.
Add the jumps to a whitelist which tells stackvalidate to ignore them.
--- arch/x86/kernel/reboot.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
arch/x86/kernel/reboot.c +5 -2
@@ -9,6 +9,7 @@#include <linux/sched.h> #include <linux/tboot.h> #include <linux/delay.h> +#include <linux/stackvalidate.h> #include <acpi/reboot.h> #include <asm/io.h> #include <asm/apic.h>
@@ -97,11 +98,13 @@ void __noreturn machine_real_restart(unsigned int type)/* Jump to the identity-mapped low memory code */ #ifdef CONFIG_X86_32 - asmvolatile ("jmpl *%0" : : + asmvolatile (STACKVALIDATE_IGNORE_INSN +"jmpl *%0;" : :"rm" (real_mode_header->machine_real_restart_asm),"a" (type));#else - asmvolatile ("ljmpl *%0" : : + asmvolatile (STACKVALIDATE_IGNORE_INSN +"ljmpl *%0" : :"m" (real_mode_header->machine_real_restart_asm),"D" (type));#endif
Signature · 1 line
2.1.0
RFC PATCH 11/21 x86/xen: Add xen_cpuid() and xen_setup_gdt() to stackvalidate whitelists
In reply to Josh Poimboeufstackvalidate reports the following false positive warnings:
stackvalidate: arch/x86/xen/enlighten.o: xen_cpuid()+0x41: can't find jump dest instruction at .text+0x108
stackvalidate: arch/x86/xen/enlighten.o: xen_setup_gdt.constprop.23()+0x2e: kernel entry/exit from callable instruction
The first warning is due to xen_cpuid()'s use of XEN_EMULATE_PREFIX to insert some fake instructions which stackvalidate doesn't know how to decode.
The second warning is due to xen_setup_gdt()'s use of an lret instruction, which stackvalidate normally doesn't allow in callable functions. But this seems to be a valid use of the instruction.
Add both functions to the stackvalidate whitelist. This results in no actual changes to the generated code.
--- arch/x86/xen/enlighten.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/xen/enlighten.c +3 -1
@@ -32,6 +32,7 @@#include <linux/gfp.h> #include <linux/memblock.h> #include <linux/edd.h> +#include <linux/stackvalidate.h> #include <xen/xen.h> #include <xen/events.h>
@@ -345,8 +346,8 @@ static void xen_cpuid(unsigned int *ax, unsigned int *bx, *cx &= maskecx; *cx |= setecx; *dx &= maskedx; - } +STACKVALIDATE_IGNORE_FUNC (xen_cpuid);static bool __init xen_check_mwait (void ) {
@@ -1403,6 +1404,7 @@ static void __ref xen_setup_gdt(int cpu) pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry; pv_cpu_ops.load_gdt = xen_load_gdt; } +STACKVALIDATE_IGNORE_FUNC (xen_setup_gdt);#ifdef CONFIG_XEN_PVH /*
Signature · 1 line
2.1.0
RFC PATCH 12/21 sched: Add __schedule() to stackvalidate whitelist
In reply to Josh Poimboeufstackvalidate reports the following warnings for __schedule():
stackvalidate: kernel/sched/core.o: __schedule()+0x3e7: duplicate frame pointer save
stackvalidate: kernel/sched/core.o: __schedule()+0x424: sibling call from callable instruction with changed frame pointer
stackvalidate: kernel/sched/core.o: __schedule()+0x431: call without frame pointer save/setup
stackvalidate: kernel/sched/core.o: __schedule()+0x8b8: frame pointer state mismatch
stackvalidate: kernel/sched/core.o: __schedule()+0x447: frame pointer state mismatch
__schedule() is obviously a special case which is allowed to do unusual things with the frame pointer. Add it to the stackvalidate whitelist. This results in no actual changes to the generated code.
--- kernel/sched/core.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/sched/core.c +2 -0
@@ -74,6 +74,7 @@#include <linux/binfmts.h> #include <linux/context_tracking.h> #include <linux/compiler.h> +#include <linux/stackvalidate.h> #include <asm/switch_to.h> #include <asm/tlb.h>
@@ -3059,6 +3060,7 @@ static void __sched __schedule(void)balance_callback (rq); } +STACKVALIDATE_IGNORE_FUNC (__schedule);static inline void sched_submit_work (struct task_struct *tsk) {
Signature · 1 line
2.1.0
Peter Zijlstra
maintainerRe: [RFC PATCH 12/21] sched: Add __schedule() to stackvalidate whitelist
Quoted 10 lines · Josh Poimboeuf
stackvalidate reports the following warnings for
__schedule():stackvalidate:
kernel/sched/core.o: __schedule()+0x3e7: duplicate frame pointer save
stackvalidate:kernel/sched/core.o: __schedule()+0x424: sibling call from callable instruction with changed frame pointer
stackvalidate:kernel/sched/core.o: __schedule()+0x431: call without frame pointer save/setup
stackvalidate:kernel/sched/core.o: __schedule()+0x8b8: frame pointer state mismatch
stackvalidate:kernel/sched/core.o: __schedule()+0x447: frame pointer state mismatch
__schedule()is obviously a special case which is allowed to do unusual things with the frame pointer.
Yes, but is the code actually correct? We can't dismiss the warnings just on that basis alone.
Re: [RFC PATCH 12/21] sched: Add __schedule() to stackvalidate whitelist
Quoted 9 lines · Peter Zijlstra
Quoted 8 lines · Josh Poimboeuf
stackvalidate reports the following warnings for
__schedule():stackvalidate:
kernel/sched/core.o: __schedule()+0x3e7: duplicate frame pointer save
stackvalidate:kernel/sched/core.o: __schedule()+0x424: sibling call from callable instruction with changed frame pointer
stackvalidate:kernel/sched/core.o: __schedule()+0x431: call without frame pointer save/setup
stackvalidate:kernel/sched/core.o: __schedule()+0x8b8: frame pointer state mismatch
stackvalidate:kernel/sched/core.o: __schedule()+0x447: frame pointer state mismatch
Quoted 5 lines
Quoted 2 lines
__schedule()is obviously a special case which is allowed to do unusual things with the frame pointer.Yes, but is the code actually correct? We can't dismiss the warnings just on that basis alone.
It's really only __switch_to that does weird things, right? I kinda want to rework that thing anyway to have a well-defined saved state format anyway, which would have the nice benefit of letting us get rid of all the ret_from_fork crap.
That is, we'd context switch like:
static inline void __switch_to(...) {
switch extra stuff;
switch everything except gpregs;
asm volatile ("call __switch_stack_and_ip" : [__sp thing goes here]
: "S" (&prev->bottom_of_stack), "D" (&next->bottom_of_stack) :
"basically all regs and flags");
}
Then the low level bit would be:
__switch_stack_and_ip:
pushq %rbp
mov %rsp, (%rsi)
mov (%rdi), %rsp
popq %rbp
ret
Now, when we create a new task, we can set up its stack directly rather than setting some TI flag, because we actually know the layout.
Hmm?
--Andy
Peter Zijlstra
maintainerRe: [RFC PATCH 12/21] sched: Add __schedule() to stackvalidate whitelist
Quoted 10 lines · Andy Lutomirski
Quoted 9 lines · Peter Zijlstra
Quoted 8 lines · Josh Poimboeuf
stackvalidate reports the following warnings for
__schedule():stackvalidate:
kernel/sched/core.o: __schedule()+0x3e7: duplicate frame pointer save
stackvalidate:kernel/sched/core.o: __schedule()+0x424: sibling call from callable instruction with changed frame pointer
stackvalidate:kernel/sched/core.o: __schedule()+0x431: call without frame pointer save/setup
stackvalidate:kernel/sched/core.o: __schedule()+0x8b8: frame pointer state mismatch
stackvalidate:kernel/sched/core.o: __schedule()+0x447: frame pointer state mismatch
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
__schedule()is obviously a special case which is allowed to do unusual things with the frame pointer.Yes, but is the code actually correct? We can't dismiss the warnings just on that basis alone.
It's really only
__switch_tothat does weird things, right?
Yep, but context_switch() gets inlined and __switch_to() is a macro, so it all ends up being __schedule().
Quoted 24 lines
I kinda
want to rework that thing anyway to have a well-defined saved state format anyway, which would have the nice benefit of letting us get rid of all theret_from_forkcrap.That is, we'd context switch like:
static inline void
__switch_to(...){
switch extra stuff;
switch everything except gpregs;
asm volatile ("call__switch_stack_and_ip" : [__spthing goes here]
: "S" (&prev->bottom_of_stack), "D" (&next->bottom_of_stack) :
"basically all regs and flags");
}Then the low level bit would be:
__switch_stack_and_ip:
pushq %rbp
mov %rsp, (%rsi)
mov (%rdi), %rsp
popq %rbp
ret
Quoted 4 lines
Now, when we create a new task, we can set up its stack directly rather than setting some TI flag, because we actually know the layout.
Hmm?
I've never really looked at the x86 details, but yes it would be good to get rid of that fork special case.
Re: [RFC PATCH 12/21] sched: Add __schedule() to stackvalidate whitelist
In reply to Andy LutomirskiQuoted 40 lines · Andy Lutomirski
Quoted 9 lines · Peter Zijlstra
Quoted 8 lines · Josh Poimboeuf
stackvalidate reports the following warnings for
__schedule():stackvalidate:
kernel/sched/core.o: __schedule()+0x3e7: duplicate frame pointer save
stackvalidate:kernel/sched/core.o: __schedule()+0x424: sibling call from callable instruction with changed frame pointer
stackvalidate:kernel/sched/core.o: __schedule()+0x431: call without frame pointer save/setup
stackvalidate:kernel/sched/core.o: __schedule()+0x8b8: frame pointer state mismatch
stackvalidate:kernel/sched/core.o: __schedule()+0x447: frame pointer state mismatchQuoted 5 lines
Quoted 2 lines
__schedule()is obviously a special case which is allowed to do unusual things with the frame pointer.Yes, but is the code actually correct? We can't dismiss the warnings just on that basis alone.
It's really only
__switch_tothat does weird things, right? I kinda want to rework that thing anyway to have a well-defined saved state format anyway, which would have the nice benefit of letting us get rid of all theret_from_forkcrap.That is, we'd context switch like:
static inline void
__switch_to(...){
switch extra stuff;
switch everything except gpregs;
asm volatile ("call__switch_stack_and_ip" : [__spthing goes here]
: "S" (&prev->bottom_of_stack), "D" (&next->bottom_of_stack) :
"basically all regs and flags");
}Then the low level bit would be:
__switch_stack_and_ip:
pushq %rbp
mov %rsp, (%rsi)
mov (%rdi), %rsp
popq %rbp
ret
Quoted 4 lines
Now, when we create a new task, we can set up its stack directly rather than setting some TI flag, because we actually know the layout.
Hmm?
I think that would make stackvalidate much happier. I'll drop this patch for now so stackvalidate will keep reminding me to bug you to implement this ;-)
Signature · 1 line
Josh
Re: [RFC PATCH 12/21] sched: Add __schedule() to stackvalidate whitelist
In reply to Andy LutomirskiQuoted 40 lines · Andy Lutomirski
Quoted 9 lines · Peter Zijlstra
Quoted 8 lines · Josh Poimboeuf
stackvalidate reports the following warnings for
__schedule():stackvalidate:
kernel/sched/core.o: __schedule()+0x3e7: duplicate frame pointer save
stackvalidate:kernel/sched/core.o: __schedule()+0x424: sibling call from callable instruction with changed frame pointer
stackvalidate:kernel/sched/core.o: __schedule()+0x431: call without frame pointer save/setup
stackvalidate:kernel/sched/core.o: __schedule()+0x8b8: frame pointer state mismatch
stackvalidate:kernel/sched/core.o: __schedule()+0x447: frame pointer state mismatchQuoted 5 lines
Quoted 2 lines
__schedule()is obviously a special case which is allowed to do unusual things with the frame pointer.Yes, but is the code actually correct? We can't dismiss the warnings just on that basis alone.
It's really only
__switch_tothat does weird things, right? I kinda want to rework that thing anyway to have a well-defined saved state format anyway, which would have the nice benefit of letting us get rid of all theret_from_forkcrap.That is, we'd context switch like:
static inline void
__switch_to(...){
switch extra stuff;
switch everything except gpregs;
asm volatile ("call__switch_stack_and_ip" : [__spthing goes here]
: "S" (&prev->bottom_of_stack), "D" (&next->bottom_of_stack) :
"basically all regs and flags");
}Then the low level bit would be:
__switch_stack_and_ip:
pushq %rbp
mov %rsp, (%rsi)
mov (%rdi), %rsp
popq %rbp
ret
Quoted 4 lines
Now, when we create a new task, we can set up its stack directly rather than setting some TI flag, because we actually know the layout.
Hmm?
Sounds good to me!
Thanks,
Ingo
RFC PATCH 13/21 x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
In reply to Josh Poimboeufaesni-intel_asm.S has several callable non-leaf functions which don't honor CONFIG_FRAME_POINTER, which can result in bad stack traces.
Create stack frames for them when CONFIG_FRAME_POINTER is enabled.
--- arch/x86/crypto/aesni-intel_asm.S | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
arch/x86/crypto/aesni-intel_asm.S +19 -0
@@ -31,6 +31,7 @@ #include <linux/linkage.h> #include <asm/inst.h> +#include <asm/frame.h> /* * The following macros are used to move an (un)aligned 16 byte value to/from
@@ -1800,6 +1801,7 @@ ENDPROC(_key_expansion_256b) * unsigned int key_len) */ ENTRY(aesni_set_key) + FRAME #ifndef __x86_64__ pushl KEYP movl 8(%esp), KEYP # ctx
@@ -1905,6 +1907,7 @@ ENTRY(aesni_set_key) #ifndef __x86_64__ popl KEYP #endif + ENDFRAME ret ENDPROC(aesni_set_key)
@@ -1912,6 +1915,7 @@ ENDPROC(aesni_set_key) * void aesni_enc(struct crypto_aes_ctx *ctx, u8 *dst, const u8 *src) */ ENTRY(aesni_enc) + FRAME #ifndef __x86_64__ pushl KEYP pushl KLEN
@@ -1927,6 +1931,7 @@ ENTRY(aesni_enc) popl KLEN popl KEYP #endif + ENDFRAME ret ENDPROC(aesni_enc)
@@ -2101,6 +2106,7 @@ ENDPROC(_aesni_enc4) * void aesni_dec (struct crypto_aes_ctx *ctx, u8 *dst, const u8 *src) */ ENTRY(aesni_dec) + FRAME #ifndef __x86_64__ pushl KEYP pushl KLEN
@@ -2117,6 +2123,7 @@ ENTRY(aesni_dec) popl KLEN popl KEYP #endif + ENDFRAME ret ENDPROC(aesni_dec)
@@ -2292,6 +2299,7 @@ ENDPROC(_aesni_dec4) * size_t len) */ ENTRY(aesni_ecb_enc) + FRAME #ifndef __x86_64__ pushl LEN pushl KEYP
@@ -2342,6 +2350,7 @@ ENTRY(aesni_ecb_enc) popl KEYP popl LEN #endif + ENDFRAME ret ENDPROC(aesni_ecb_enc)
@@ -2350,6 +2359,7 @@ ENDPROC(aesni_ecb_enc) * size_t len); */ ENTRY(aesni_ecb_dec) + FRAME #ifndef __x86_64__ pushl LEN pushl KEYP
@@ -2401,6 +2411,7 @@ ENTRY(aesni_ecb_dec) popl KEYP popl LEN #endif + ENDFRAME ret ENDPROC(aesni_ecb_dec)
@@ -2409,6 +2420,7 @@ ENDPROC(aesni_ecb_dec) * size_t len, u8 *iv) */ ENTRY(aesni_cbc_enc) + FRAME #ifndef __x86_64__ pushl IVP pushl LEN
@@ -2443,6 +2455,7 @@ ENTRY(aesni_cbc_enc) popl LEN popl IVP #endif + ENDFRAME ret ENDPROC(aesni_cbc_enc)
@@ -2451,6 +2464,7 @@ ENDPROC(aesni_cbc_enc) * size_t len, u8 *iv) */ ENTRY(aesni_cbc_dec) + FRAME #ifndef __x86_64__ pushl IVP pushl LEN
@@ -2534,6 +2548,7 @@ ENTRY(aesni_cbc_dec) popl LEN popl IVP #endif + ENDFRAME ret ENDPROC(aesni_cbc_dec)
@@ -2598,6 +2613,7 @@ ENDPROC(_aesni_inc) * size_t len, u8 *iv) */ ENTRY(aesni_ctr_enc) + FRAME cmp $16, LEN jb .Lctr_enc_just_ret mov 480(KEYP), KLEN
@@ -2651,6 +2667,7 @@ ENTRY(aesni_ctr_enc) .Lctr_enc_ret: movups IV, (IVP) .Lctr_enc_just_ret: + ENDFRAME ret ENDPROC(aesni_ctr_enc)
@@ -2677,6 +2694,7 @@ ENDPROC(aesni_ctr_enc) * bool enc, u8 *iv) */ ENTRY(aesni_xts_crypt8) + FRAME cmpb $0, %cl movl $0, %ecx movl $240, %r10d
@@ -2777,6 +2795,7 @@ ENTRY(aesni_xts_crypt8) pxor INC, STATE4 movdqu STATE4, 0x70(OUTP) + ENDFRAME ret ENDPROC(aesni_xts_crypt8)
Signature · 1 line
2.1.0
Re: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
arch/x86/crypto/aesni-intel_asm.S ENTRY(aesni_set_key) · line 1912
Quoted 12 lines · Josh Poimboeuf
ENTRY(aesni_set_key)arch/x86/crypto/aesni-intel_asm.S +2
-0+ FRAME #ifndef __x86_64__ pushl KEYP movl 8(%esp), KEYP # ctx@@ -1905,6 +1907,7 @@ ENTRY(aesni_set_key) #ifndef __x86_64__ popl KEYP #endif + ENDFRAME ret ENDPROC(aesni_set_key)
So cannot we make this a bit more compact and less fragile?
Instead of:
ENTRY(aesni_set_key) FRAME ... ENDFRAME ret ENDPROC(aesni_set_key)
How about writing this as:
FUNCTION_ENTRY(aesni_set_key) ... FUNCTION_RETURN(aesni_set_key)
which does the same thing in a short, symmetric construct?
One potential problem with this approach would be that what 'looks' like an entry declaration, but it will now generate real code.
OTOH if people find this intuitive enough then it's a lot harder to mess it up, and I think 'RETURN' makes it clear enough that there's a real instruction generated there.
Thanks,
Ingo
Re: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
Quoted 34 lines · Ingo Molnar
arch/x86/crypto/aesni-intel_asm.S ENTRY(aesni_set_key) · line 1912
Quoted 12 lines · Josh Poimboeuf
ENTRY(aesni_set_key)arch/x86/crypto/aesni-intel_asm.S +2
-0+ FRAME #ifndef __x86_64__ pushl KEYP movl 8(%esp), KEYP # ctx@@ -1905,6 +1907,7 @@ ENTRY(aesni_set_key) #ifndef __x86_64__ popl KEYP #endif + ENDFRAME ret ENDPROC(aesni_set_key)So cannot we make this a bit more compact and less fragile?
Instead of:
ENTRY(aesni_set_key) FRAME ... ENDFRAME ret ENDPROC(aesni_set_key)How about writing this as:
FUNCTION_ENTRY(aesni_set_key) ... FUNCTION_RETURN(aesni_set_key)
Quoted 9 lines
which does the same thing in a short, symmetric construct?
One potential problem with this approach would be that what 'looks' like an entry declaration, but it will now generate real code.
OTOH if people find this intuitive enough then it's a lot harder to mess it up, and I think 'RETURN' makes it clear enough that there's a real instruction generated there.
How about FUNCTION_PROLOGUE and FUNCTION_EPILOGUE?
-Andy
Re: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
Quoted 40 lines · Andy Lutomirski
Quoted 39 lines · Ingo Molnar
arch/x86/crypto/aesni-intel_asm.S ENTRY(aesni_set_key) · line 1912
Quoted 12 lines · Josh Poimboeuf
ENTRY(aesni_set_key)arch/x86/crypto/aesni-intel_asm.S +2
-0+ FRAME #ifndef __x86_64__ pushl KEYP movl 8(%esp), KEYP # ctx@@ -1905,6 +1907,7 @@ ENTRY(aesni_set_key) #ifndef __x86_64__ popl KEYP #endif + ENDFRAME ret ENDPROC(aesni_set_key)So cannot we make this a bit more compact and less fragile?
Instead of:
ENTRY(aesni_set_key) FRAME ... ENDFRAME ret ENDPROC(aesni_set_key)How about writing this as:
FUNCTION_ENTRY(aesni_set_key) ... FUNCTION_RETURN(aesni_set_key)which does the same thing in a short, symmetric construct?
One potential problem with this approach would be that what 'looks' like an entry declaration, but it will now generate real code.
Quoted 6 lines
Quoted 4 lines
OTOH if people find this intuitive enough then it's a lot harder to mess it up, and I think 'RETURN' makes it clear enough that there's a real instruction generated there.
How about
FUNCTION_PROLOGUEandFUNCTION_EPILOGUE?
Perhaps the macro name should describe what the epilogue does, since frame pointers aren't required for _all_ functions, only those which don't have call instructions.
What do you think about ENTRY_FRAME and ENDPROC_FRAME_RETURN? The ending macro is kind of long, but at least it a) matches the existing ENTRY/ENDPROC convention for asm functions; b) gives a clue that frame pointers are involved; and c) lets you know that the return is there.
Signature · 1 line
Josh
Re: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
Quoted 48 lines · Josh Poimboeuf
Quoted 40 lines · Andy Lutomirski
Quoted 39 lines · Ingo Molnar
arch/x86/crypto/aesni-intel_asm.S ENTRY(aesni_set_key) · line 1912
Quoted 12 lines · Josh Poimboeuf
ENTRY(aesni_set_key)arch/x86/crypto/aesni-intel_asm.S +2
-0+ FRAME #ifndef __x86_64__ pushl KEYP movl 8(%esp), KEYP # ctx@@ -1905,6 +1907,7 @@ ENTRY(aesni_set_key) #ifndef __x86_64__ popl KEYP #endif + ENDFRAME ret ENDPROC(aesni_set_key)So cannot we make this a bit more compact and less fragile?
Instead of:
ENTRY(aesni_set_key) FRAME ... ENDFRAME ret ENDPROC(aesni_set_key)How about writing this as:
FUNCTION_ENTRY(aesni_set_key) ... FUNCTION_RETURN(aesni_set_key)which does the same thing in a short, symmetric construct?
One potential problem with this approach would be that what 'looks' like an entry declaration, but it will now generate real code.
Quoted 6 lines
Quoted 4 lines
OTOH if people find this intuitive enough then it's a lot harder to mess it up, and I think 'RETURN' makes it clear enough that there's a real instruction generated there.
How about
FUNCTION_PROLOGUEandFUNCTION_EPILOGUE?
Quoted 9 lines
Perhaps the macro name should describe what the epilogue does, since frame pointers aren't required for _all_ functions, only those which don't have call instructions.
What do you think about
ENTRY_FRAMEandENDPROC_FRAME_RETURN? The ending macro is kind of long, but at least it a) matches the existing ENTRY/ENDPROC convention for asm functions; b) gives a clue that frame pointers are involved; and c) lets you know that the return is there.
This really is about frame pointers, right? How about ENTRY_FRAMEPTR_xyz where xyz can be prologue, epilogue, return, whatever?
Quoted 2 lines
--
Josh
Signature · 2 lines
Andy Lutomirski AMA Capital Management, LLC
Re: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
Quoted 59 lines · Andy Lutomirski
Quoted 48 lines · Josh Poimboeuf
Quoted 40 lines · Andy Lutomirski
Quoted 39 lines · Ingo Molnar
arch/x86/crypto/aesni-intel_asm.S ENTRY(aesni_set_key) · line 1912
Quoted 12 lines · Josh Poimboeuf
ENTRY(aesni_set_key)arch/x86/crypto/aesni-intel_asm.S +2
-0+ FRAME #ifndef __x86_64__ pushl KEYP movl 8(%esp), KEYP # ctx@@ -1905,6 +1907,7 @@ ENTRY(aesni_set_key) #ifndef __x86_64__ popl KEYP #endif + ENDFRAME ret ENDPROC(aesni_set_key)So cannot we make this a bit more compact and less fragile?
Instead of:
ENTRY(aesni_set_key) FRAME ... ENDFRAME ret ENDPROC(aesni_set_key)How about writing this as:
FUNCTION_ENTRY(aesni_set_key) ... FUNCTION_RETURN(aesni_set_key)which does the same thing in a short, symmetric construct?
One potential problem with this approach would be that what 'looks' like an entry declaration, but it will now generate real code.
Quoted 6 lines
Quoted 4 lines
OTOH if people find this intuitive enough then it's a lot harder to mess it up, and I think 'RETURN' makes it clear enough that there's a real instruction generated there.
How about
FUNCTION_PROLOGUEandFUNCTION_EPILOGUE?Quoted 9 lines
Perhaps the macro name should describe what the epilogue does, since frame pointers aren't required for _all_ functions, only those which don't have call instructions.
What do you think about
ENTRY_FRAMEandENDPROC_FRAME_RETURN? The ending macro is kind of long, but at least it a) matches the existing ENTRY/ENDPROC convention for asm functions; b) gives a clue that frame pointers are involved; and c) lets you know that the return is there.
Quoted 3 lines
This really is about frame pointers, right? How about
ENTRY_FRAMEPTR_xyzwhere xyz can be prologue, epilogue, return, whatever?
Wouldn't the "ENTRY" in ENTRY_FRAMEPTR_RETURN be confusing at the end of a function?
Signature · 1 line
Josh
Re: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
Quoted 60 lines · Josh Poimboeuf
Quoted 59 lines · Andy Lutomirski
Quoted 57 lines · Josh Poimboeuf
Quoted 40 lines · Andy Lutomirski
Quoted 39 lines · Ingo Molnar
arch/x86/crypto/aesni-intel_asm.S ENTRY(aesni_set_key) · line 1912
Quoted 12 lines · Josh Poimboeuf
ENTRY(aesni_set_key)arch/x86/crypto/aesni-intel_asm.S +2
-0+ FRAME #ifndef __x86_64__ pushl KEYP movl 8(%esp), KEYP # ctx@@ -1905,6 +1907,7 @@ ENTRY(aesni_set_key) #ifndef __x86_64__ popl KEYP #endif + ENDFRAME ret ENDPROC(aesni_set_key)So cannot we make this a bit more compact and less fragile?
Instead of:
ENTRY(aesni_set_key) FRAME ... ENDFRAME ret ENDPROC(aesni_set_key)How about writing this as:
FUNCTION_ENTRY(aesni_set_key) ... FUNCTION_RETURN(aesni_set_key)which does the same thing in a short, symmetric construct?
One potential problem with this approach would be that what 'looks' like an entry declaration, but it will now generate real code.
Quoted 6 lines
Quoted 4 lines
OTOH if people find this intuitive enough then it's a lot harder to mess it up, and I think 'RETURN' makes it clear enough that there's a real instruction generated there.
How about
FUNCTION_PROLOGUEandFUNCTION_EPILOGUE?Perhaps the macro name should describe what the epilogue does, since frame pointers aren't required for _all_ functions, only those which don't have call instructions.
What do you think about
ENTRY_FRAMEandENDPROC_FRAME_RETURN? The ending macro is kind of long, but at least it a) matches the existing ENTRY/ENDPROC convention for asm functions; b) gives a clue that frame pointers are involved; and c) lets you know that the return is there.
Quoted 6 lines
Quoted 3 lines
This really is about frame pointers, right? How about
ENTRY_FRAMEPTR_xyzwhere xyz can be prologue, epilogue, return, whatever?Wouldn't the "ENTRY" in
ENTRY_FRAMEPTR_RETURNbe confusing at the end of a function?
I meant ENTRY_FRAMEPTR_xyz and the beginning and ENDPROC_FRAMEPTR_xyz (ENTRY is debatable, but that's what we currently have). ENDPROC could easily be replaced with anything else.
--Andy
Quoted 3 lines
--
Josh
Signature · 2 lines
Andy Lutomirski AMA Capital Management, LLC
Re: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
Quoted 65 lines · Andy Lutomirski
Quoted 64 lines · Josh Poimboeuf
Quoted 62 lines · Andy Lutomirski
Quoted 48 lines · Josh Poimboeuf
Quoted 40 lines · Andy Lutomirski
Quoted 39 lines · Ingo Molnar
Review ENTRY(aesni_set_key) · line 1912
Quoted 12 lines · Josh Poimboeuf
ENTRY(aesni_set_key)(quoted diff) +2
-0+ FRAME #ifndef __x86_64__ pushl KEYP movl 8(%esp), KEYP # ctx@@ -1905,6 +1907,7 @@ ENTRY(aesni_set_key) #ifndef __x86_64__ popl KEYP #endif + ENDFRAME ret ENDPROC(aesni_set_key)So cannot we make this a bit more compact and less fragile?
Instead of:
ENTRY(aesni_set_key) FRAME ... ENDFRAME ret ENDPROC(aesni_set_key)How about writing this as:
FUNCTION_ENTRY(aesni_set_key) ... FUNCTION_RETURN(aesni_set_key)which does the same thing in a short, symmetric construct?
One potential problem with this approach would be that what 'looks' like an entry declaration, but it will now generate real code.
Quoted 6 lines
Quoted 4 lines
OTOH if people find this intuitive enough then it's a lot harder to mess it up, and I think 'RETURN' makes it clear enough that there's a real instruction generated there.
How about
FUNCTION_PROLOGUEandFUNCTION_EPILOGUE?Quoted 9 lines
Perhaps the macro name should describe what the epilogue does, since frame pointers aren't required for _all_ functions, only those which don't have call instructions.
What do you think about
ENTRY_FRAMEandENDPROC_FRAME_RETURN? The ending macro is kind of long, but at least it a) matches the existing ENTRY/ENDPROC convention for asm functions; b) gives a clue that frame pointers are involved; and c) lets you know that the return is there.This really is about frame pointers, right? How about
ENTRY_FRAMEPTR_xyzwhere xyz can be prologue, epilogue, return, whatever?
Quoted 6 lines
Quoted 2 lines
Wouldn't the "ENTRY" in
ENTRY_FRAMEPTR_RETURNbe confusing at the end of a function?I meant
ENTRY_FRAMEPTR_xyzand the beginning andENDPROC_FRAMEPTR_xyz(ENTRY is debatable, but that's what we currently have). ENDPROC could easily be replaced with anything else.
So do you mean ENTRY_FRAMEPTR_PROLOGUE and ENDPROC_FRAMEPTR_EPILOGUE? Or something else?
Signature · 1 line
Josh
Re: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
Quoted 69 lines · Josh Poimboeuf
Quoted 68 lines · Andy Lutomirski
Quoted 66 lines · Josh Poimboeuf
Quoted 59 lines · Andy Lutomirski
Quoted 48 lines · Josh Poimboeuf
Quoted 40 lines · Andy Lutomirski
Quoted 39 lines · Ingo Molnar
Review ENTRY(aesni_set_key) · line 1912
Quoted 12 lines · Josh Poimboeuf
ENTRY(aesni_set_key)(quoted diff) +2
-0+ FRAME #ifndef __x86_64__ pushl KEYP movl 8(%esp), KEYP # ctx@@ -1905,6 +1907,7 @@ ENTRY(aesni_set_key) #ifndef __x86_64__ popl KEYP #endif + ENDFRAME ret ENDPROC(aesni_set_key)So cannot we make this a bit more compact and less fragile?
Instead of:
ENTRY(aesni_set_key) FRAME ... ENDFRAME ret ENDPROC(aesni_set_key)How about writing this as:
FUNCTION_ENTRY(aesni_set_key) ... FUNCTION_RETURN(aesni_set_key)which does the same thing in a short, symmetric construct?
One potential problem with this approach would be that what 'looks' like an entry declaration, but it will now generate real code.
Quoted 6 lines
Quoted 4 lines
OTOH if people find this intuitive enough then it's a lot harder to mess it up, and I think 'RETURN' makes it clear enough that there's a real instruction generated there.
How about
FUNCTION_PROLOGUEandFUNCTION_EPILOGUE?Quoted 9 lines
Perhaps the macro name should describe what the epilogue does, since frame pointers aren't required for _all_ functions, only those which don't have call instructions.
What do you think about
ENTRY_FRAMEandENDPROC_FRAME_RETURN? The ending macro is kind of long, but at least it a) matches the existing ENTRY/ENDPROC convention for asm functions; b) gives a clue that frame pointers are involved; and c) lets you know that the return is there.Quoted 3 lines
This really is about frame pointers, right? How about
ENTRY_FRAMEPTR_xyzwhere xyz can be prologue, epilogue, return, whatever?Wouldn't the "ENTRY" in
ENTRY_FRAMEPTR_RETURNbe confusing at the end of a function?
Quoted 7 lines
Quoted 3 lines
I meant
ENTRY_FRAMEPTR_xyzand the beginning andENDPROC_FRAMEPTR_xyz(ENTRY is debatable, but that's what we currently have). ENDPROC could easily be replaced with anything else.So do you mean
ENTRY_FRAMEPTR_PROLOGUEandENDPROC_FRAMEPTR_EPILOGUE? Or something else?
I like it. I think this bikeshed might be well painted now!
--Andy
Quoted 2 lines
--
Josh
Signature · 2 lines
Andy Lutomirski AMA Capital Management, LLC
Re: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
Quoted 74 lines · Andy Lutomirski
Quoted 73 lines · Josh Poimboeuf
Quoted 71 lines · Andy Lutomirski
Quoted 60 lines · Josh Poimboeuf
Quoted 59 lines · Andy Lutomirski
Quoted 57 lines · Josh Poimboeuf
Quoted 40 lines · Andy Lutomirski
Quoted 39 lines · Ingo Molnar
Review ENTRY(aesni_set_key) · line 1912
Quoted 12 lines · Josh Poimboeuf
ENTRY(aesni_set_key)(quoted diff) +2
-0+ FRAME #ifndef __x86_64__ pushl KEYP movl 8(%esp), KEYP # ctx@@ -1905,6 +1907,7 @@ ENTRY(aesni_set_key) #ifndef __x86_64__ popl KEYP #endif + ENDFRAME ret ENDPROC(aesni_set_key)So cannot we make this a bit more compact and less fragile?
Instead of:
ENTRY(aesni_set_key) FRAME ... ENDFRAME ret ENDPROC(aesni_set_key)How about writing this as:
FUNCTION_ENTRY(aesni_set_key) ... FUNCTION_RETURN(aesni_set_key)which does the same thing in a short, symmetric construct?
One potential problem with this approach would be that what 'looks' like an entry declaration, but it will now generate real code.
Quoted 6 lines
Quoted 4 lines
OTOH if people find this intuitive enough then it's a lot harder to mess it up, and I think 'RETURN' makes it clear enough that there's a real instruction generated there.
How about
FUNCTION_PROLOGUEandFUNCTION_EPILOGUE?Perhaps the macro name should describe what the epilogue does, since frame pointers aren't required for _all_ functions, only those which don't have call instructions.
What do you think about
ENTRY_FRAMEandENDPROC_FRAME_RETURN? The ending macro is kind of long, but at least it a) matches the existing ENTRY/ENDPROC convention for asm functions; b) gives a clue that frame pointers are involved; and c) lets you know that the return is there.Quoted 6 lines
Quoted 3 lines
This really is about frame pointers, right? How about
ENTRY_FRAMEPTR_xyzwhere xyz can be prologue, epilogue, return, whatever?Wouldn't the "ENTRY" in
ENTRY_FRAMEPTR_RETURNbe confusing at the end of a function?I meant
ENTRY_FRAMEPTR_xyzand the beginning andENDPROC_FRAMEPTR_xyz(ENTRY is debatable, but that's what we currently have). ENDPROC could easily be replaced with anything else.
Quoted 5 lines
Quoted 3 lines
So do you mean
ENTRY_FRAMEPTR_PROLOGUEandENDPROC_FRAMEPTR_EPILOGUE? Or something else?I like it. I think this bikeshed might be well painted now!
Actually I'm not done painting. Personally it seems a little too verbose. I still like ENTRY_FRAME and ENDPROC_FRAME_RETURN :p
Signature · 1 line
Josh
Borislav Petkov
maintainerRe: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
Quoted 2 lines · Josh Poimboeuf
Actually I'm not done painting. Personally it seems a little too verbose. I still like
ENTRY_FRAMEandENDPROC_FRAME_RETURN:p
Let's balance it out even more:
ENTRY_FRAME(..)
...
ENDPROC_FRAME(..)
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--Re: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
Quoted 11 lines · Borislav Petkov
Quoted 2 lines · Josh Poimboeuf
Actually I'm not done painting. Personally it seems a little too verbose. I still like
ENTRY_FRAMEandENDPROC_FRAME_RETURN:pLet's balance it out even more:
ENTRY_FRAME(..)...
ENDPROC_FRAME(..)
I like the balance, but the "ret" is still non-obvious.
How about:
FRAME_ENTRY(...)
FRAME_RETURN(...)
Signature · 1 line
Josh
Borislav Petkov
maintainerRe: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
Quoted 1 line · Josh Poimboeuf
I like the balance, but the "ret" is still non-obvious.
Does it have to be obvious?
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--Re: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
Quoted 4 lines · Borislav Petkov
Quoted 1 line · Josh Poimboeuf
I like the balance, but the "ret" is still non-obvious.
Does it have to be obvious?
I feel that making "ret" obvious is better.
But if somebody messes up and adds a second "ret", I suppose stackvalidate would warn about the fact that it returned without restoring the frame pointer. So if there are no other objections, your suggestion of ENTRY_FRAME and ENDPROC_FRAME is fine with me.
Signature · 1 line
Josh
Re: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
In reply to Josh PoimboeufQuoted 48 lines · Josh Poimboeuf
Quoted 40 lines · Andy Lutomirski
Quoted 39 lines · Ingo Molnar
arch/x86/crypto/aesni-intel_asm.S ENTRY(aesni_set_key) · line 1912
Quoted 12 lines · Josh Poimboeuf
ENTRY(aesni_set_key)arch/x86/crypto/aesni-intel_asm.S +2
-0+ FRAME #ifndef __x86_64__ pushl KEYP movl 8(%esp), KEYP # ctx@@ -1905,6 +1907,7 @@ ENTRY(aesni_set_key) #ifndef __x86_64__ popl KEYP #endif + ENDFRAME ret ENDPROC(aesni_set_key)So cannot we make this a bit more compact and less fragile?
Instead of:
ENTRY(aesni_set_key) FRAME ... ENDFRAME ret ENDPROC(aesni_set_key)How about writing this as:
FUNCTION_ENTRY(aesni_set_key) ... FUNCTION_RETURN(aesni_set_key)which does the same thing in a short, symmetric construct?
One potential problem with this approach would be that what 'looks' like an entry declaration, but it will now generate real code.
Quoted 6 lines
Quoted 4 lines
OTOH if people find this intuitive enough then it's a lot harder to mess it up, and I think 'RETURN' makes it clear enough that there's a real instruction generated there.
How about
FUNCTION_PROLOGUEandFUNCTION_EPILOGUE?
Quoted 8 lines
Perhaps the macro name should describe what the epilogue does, since frame pointers aren't required for _all_ functions, only those which don't have call instructions.
What do you think about
ENTRY_FRAMEandENDPROC_FRAME_RETURN? The ending macro is kind of long, but at least it a) matches the existing ENTRY/ENDPROC convention for asm functions; b) gives a clue that frame pointers are involved; and c) lets you know that the return is there.
So the thing I like about these:
FUNCTION_ENTRY(aesni_set_key) ... FUNCTION_RETURN(aesni_set_key)
is the symmetry - it's a lot harder to misplace/miswrite these than two completely separately named things:
ENTRY_FRAME(aesni_set_key) ... ENDPROC_FRAME_RETURN(aesni_set_key)
Also, the 'FRAME' part will be pointless and somewhat misleading once we do dwarves, right?
Another valid variants would be:
FUNCTION_ENTER(aesni_set_key) ... FUNCTION_RET(aesni_set_key)
or:
FUNCTION_START(aesni_set_key) ... FUNCTION_RET(aesni_set_key)
or:
ASM_FUNCTION_START(aesni_set_key) ... ASM_FUNCTION_RET(aesni_set_key)
Note that the name has two parts:
- The symmetric 'FUNCTION_' prefix tells us that this is a callable function that we are defining. That is a very significant property of this construct, and should be present in both the 'start' and the 'end' markers.
- The '_RET' stresses the fact that it always generates a 'ret' instruction.
Note what the names _don't_ contain: that we generate debug info! That fact is not present in the naming, and that's very much intentional, because the precise form of debug info is conditional:
- if CONFIG_FRAME_POINTERS=y then we push/pop a stack frame
- if (later on) we do CFI annotations we don't push/pop a stack frame but emit CFI debuginfo
In that sense 'FRAME' should never be in these names I think, nor 'PROC' (which is not symmetric).
Plus all 3 variants I suggested are very easy to remember, why I'd always have to look up any non-symmetric macro name called 'PROC'...
Thanks,
Ingo
Re: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
Quoted 8 lines · Ingo Molnar
Note what the names _don't_ contain: that we generate debug info! That fact is not present in the naming, and that's very much intentional, because the precise form of debug info is conditional:
- if
CONFIG_FRAME_POINTERS=ythen we push/pop a stack frame- if (later on) we do CFI annotations we don't push/pop a stack frame but emit CFI debuginfo
According to current plan, the macro won't add CFI annotations. That will be done instead by a separate tool. So the macro really is frame pointer specific.
Quoted 6 lines
In that sense 'FRAME' should never be in these names I think, nor 'PROC' (which is not symmetric).
Plus all 3 variants I suggested are very easy to remember, why I'd always have to look up any non-symmetric macro name called 'PROC'...
The reason I suggested to put FRAME in the macro name is to try to prevent it from being accidentally used for leaf functions, where it isn't needed.
Also the naming of FUNCTION_ENTRY and FUNCTION_RETURN doesn't do anything to distinguish them from the already ubiquitous ENTRY and ENDPROC. So as a kernel developer it seems confusing to me, e.g. how do I remember when to use FUNCTION_ENTRY vs ENTRY?
Signature · 1 line
Josh
Re: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
Quoted 5 lines · Josh Poimboeuf
Quoted 4 lines · Ingo Molnar
Note what the names _don't_ contain: that we generate debug info! That fact is not present in the naming, and that's very much intentional, because the precise form of debug info is conditional:
Quoted 7 lines
Quoted 4 lines
- if
CONFIG_FRAME_POINTERS=ythen we push/pop a stack frame- if (later on) we do CFI annotations we don't push/pop a stack frame but emit CFI debuginfo
According to current plan, the macro won't add CFI annotations. That will be done instead by a separate tool. So the macro really is frame pointer specific.
Still the same argument applies: it's a debug info detail we should hide as much as sensibly possible. In the CONFIG_FRAME_POINTERS=y case it will be code, in the future !CONFIG_FRAME_POINTERS case it will be nothing.
Quoted 8 lines
Quoted 5 lines
In that sense 'FRAME' should never be in these names I think, nor 'PROC' (which is not symmetric).
Plus all 3 variants I suggested are very easy to remember, why I'd always have to look up any non-symmetric macro name called 'PROC'...
The reason I suggested to put FRAME in the macro name is to try to prevent it from being accidentally used for leaf functions, where it isn't needed.
Well, we could use LEAF_FUNCTION to mark that fact.
Wether a function written in assembly is a leaf function or not is a higher level (and thus more valuable) piece of information whether we generate frame pointer debuginfo or not.
Quoted 4 lines
Also the naming of
FUNCTION_ENTRYandFUNCTION_RETURNdoesn't do anything to distinguish them from the already ubiquitous ENTRY and ENDPROC. So as a kernel developer it seems confusing to me, e.g. how do I remember when to useFUNCTION_ENTRYvs ENTRY?
'ENDPROC' is really leftover from older debuginfo cruft, it's not a valuable construct IMHO, even if it's (sadly) ubiquitious.
We want to create new, clean, as minimal as possible and as clearly named as possible debuginfo constructs from first principles.
Thanks,
Ingo
Re: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
Quoted 15 lines · Ingo Molnar
Quoted 3 lines
The reason I suggested to put FRAME in the macro name is to try to prevent it from being accidentally used for leaf functions, where it isn't needed.
Well, we could use
LEAF_FUNCTIONto mark that fact.Wether a function written in assembly is a leaf function or not is a higher level (and thus more valuable) piece of information whether we generate frame pointer debuginfo or not.
Quoted 4 lines
Also the naming of
FUNCTION_ENTRYandFUNCTION_RETURNdoesn't do anything to distinguish them from the already ubiquitous ENTRY and ENDPROC. So as a kernel developer it seems confusing to me, e.g. how do I remember when to useFUNCTION_ENTRYvs ENTRY?
Quoted 5 lines
'ENDPROC' is really leftover from older debuginfo cruft, it's not a valuable construct IMHO, even if it's (sadly) ubiquitious.
We want to create new, clean, as minimal as possible and as clearly named as possible debuginfo constructs from first principles.
Ok. So if I understand right, the proposal is:
Replace *all* x86 usage of ENTRY/ENDPROC with either:
FUNCTION_ENTRY(func)FUNCTION_RETURN(func)
or
LEAF_FUNCTION_ENTRY(func)LEAF_FUNCTION_RETURN(func)
Those sound fine to me.
I should point out that there are still a few cases where the more granular FRAME/ENDFRAME and ENTRY/ENDPROC macros would still be needed.
For example, if the function ends with a jump instead of a ret. If the jump is a sibling call, the code would look like:
FUNCTION_ENTRY(func)
... ENDFRAME jmp another_func
ENDPROC(func)
Or if it's a jump within the function to an internal ret:
FUNCTION_ENTRY(func)
...
1: ...
ENDFRAME
ret
2: ...
jmp 1b
ENDPROC(func)
Or if it jumps to some shared code before returning:
FUNCTION_ENTRY(func_1)
... jmp common_return
ENDPROC(func_1)
FUNCTION_ENTRY(func_2)
... jmp common_return
ENDPROC(func_2)
common_return:
... ENDFRAME ret
So in some cases we'd still need the more granular macros, unless we decided to make special macros for these cases as well.
Signature · 1 line
Josh
Re: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
Quoted 29 lines · Josh Poimboeuf
Quoted 15 lines · Ingo Molnar
Quoted 3 lines
The reason I suggested to put FRAME in the macro name is to try to prevent it from being accidentally used for leaf functions, where it isn't needed.
Well, we could use
LEAF_FUNCTIONto mark that fact.Wether a function written in assembly is a leaf function or not is a higher level (and thus more valuable) piece of information whether we generate frame pointer debuginfo or not.
Quoted 4 lines
Also the naming of
FUNCTION_ENTRYandFUNCTION_RETURNdoesn't do anything to distinguish them from the already ubiquitous ENTRY and ENDPROC. So as a kernel developer it seems confusing to me, e.g. how do I remember when to useFUNCTION_ENTRYvs ENTRY?Quoted 5 lines
'ENDPROC' is really leftover from older debuginfo cruft, it's not a valuable construct IMHO, even if it's (sadly) ubiquitious.
We want to create new, clean, as minimal as possible and as clearly named as possible debuginfo constructs from first principles.
Ok. So if I understand right, the proposal is:
Replace *all* x86 usage of ENTRY/ENDPROC with either:
FUNCTION_ENTRY(func)FUNCTION_RETURN(func)
Quoted 6 lines
or
LEAF_FUNCTION_ENTRY(func)LEAF_FUNCTION_RETURN(func)Those sound fine to me.
Yeah - but keep the old constructs as well and don't necessarily do the full migration straight away, only once the dust has settled - to reduce churn.
Quoted 37 lines
I should point out that there are still a few cases where the more granular FRAME/ENDFRAME and ENTRY/ENDPROC macros would still be needed.
For example, if the function ends with a jump instead of a ret. If the jump is a sibling call, the code would look like:
FUNCTION_ENTRY(func)... ENDFRAME jmp another_funcENDPROC(func)
Or if it's a jump within the function to an internal ret:
FUNCTION_ENTRY(func)...1: ...
ENDFRAME
ret
2: ...
jmp 1b
ENDPROC(func)Or if it jumps to some shared code before returning:
FUNCTION_ENTRY(func_1)... jmp common_return
ENDPROC(func_1)
FUNCTION_ENTRY(func_2)... jmp common_return
ENDPROC(func_2)
Quoted 8 lines
common_return:... ENDFRAME retSo in some cases we'd still need the more granular macros, unless we decided to make special macros for these cases as well.
Ok, I see how the naming scheme I proposed won't work with all that very well, but I'd still suggest using consistently named patterns.
Let me suggest yet another approach. How about open-coding something like this:
FUNCTION_START(func)
push_bp mov_sp_bp ... pop_bp ret
FUNCTION_END(func)
This is just two easy things:
- a redefine of the FUNCTION_ENTRY and ENDPROC names
- the introduction of three quasi-mnemonics: push_bp, mov_sp_bp, pop_bp - which all look very similar to a real frame setup sequence, except that we can easily make them go away in the !CONFIG_FRAME_POINTERS case.
The advantage of this approach would be:
- it looks pretty 'natural' and very close to how the real disassembly looks like in CONFIG_FRAME_POINTERS=y kernels. So while it's not as compact as some of the other variants, it's close to what the real instruction sequence looks like and that is a positive quality in itself.
- it also makes it apparent 'on sight' that it's probably a bug to have unbalanced push/pop sequences in a regular function, to any reasonably alert assembly coder.
- if we ever unsupport framepointer kernels in the (far far) future, we can get rid of all lines with those 3 mnemonics and be done with it.
- it's finegrained enough so that we can express all the special function/tail variants you listed above.
What do you think?
I'd still keep existing frame setup functionality and names and only use these in fixes, new code and new annotations - and do a full rename and cleanup once the dust has settled.
Thanks,
Ingo
Re: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
Quoted 84 lines · Ingo Molnar
Quoted 38 lines · Josh Poimboeuf
On Mon, Jul 20, 2015 at 09:56:11AM +0200, Ingo Molnar wrote:
I should point out that there are still a few cases where the more granular FRAME/ENDFRAME and ENTRY/ENDPROC macros would still be needed.For example, if the function ends with a jump instead of a ret. If the jump is a sibling call, the code would look like:
FUNCTION_ENTRY(func)... ENDFRAME jmp another_funcENDPROC(func)
Or if it's a jump within the function to an internal ret:
FUNCTION_ENTRY(func)...1: ...
ENDFRAME
ret
2: ...
jmp 1b
ENDPROC(func)Or if it jumps to some shared code before returning:
FUNCTION_ENTRY(func_1)... jmp common_return
ENDPROC(func_1)
FUNCTION_ENTRY(func_2)... jmp common_return
ENDPROC(func_2)Quoted 8 lines
common_return:... ENDFRAME retSo in some cases we'd still need the more granular macros, unless we decided to make special macros for these cases as well.
Ok, I see how the naming scheme I proposed won't work with all that very well, but I'd still suggest using consistently named patterns.
Let me suggest yet another approach. How about open-coding something like this:
FUNCTION_START(func)push_bp mov_sp_bp ... pop_bp ret
FUNCTION_END(func)This is just two easy things:
- a redefine of the
FUNCTION_ENTRYand ENDPROC names- the introduction of three quasi-mnemonics:
push_bp,mov_sp_bp,pop_bp- which all look very similar to a real frame setup sequence, except that we can easily make them go away in the !CONFIG_FRAME_POINTERS case.The advantage of this approach would be:
- it looks pretty 'natural' and very close to how the real disassembly looks like in
CONFIG_FRAME_POINTERS=ykernels. So while it's not as compact as some of the other variants, it's close to what the real instruction sequence looks like and that is a positive quality in itself.- it also makes it apparent 'on sight' that it's probably a bug to have unbalanced push/pop sequences in a regular function, to any reasonably alert assembly coder.
Quoted 7 lines
- if we ever unsupport framepointer kernels in the (far far) future, we can get rid of all lines with those 3 mnemonics and be done with it.
- it's finegrained enough so that we can express all the special function/tail variants you listed above.
What do you think?
I agree that the edge cases make FUNCTION_ENTRY and FUNCTION_RETURN less attractive. Slowly we are circling around to where we started :-)
Personally, I prefer FRAME/ENDFRAME instead of push_bp/mov_sp_bp/pop_bp, because it more communicates *what* it's doing rather than how. IMO, it's easier to grok with a quick glance.
Quoted 3 lines
I'd still keep existing frame setup functionality and names and only use these in fixes, new code and new annotations - and do a full rename and cleanup once the dust has settled.
That sounds good.
Signature · 1 line
Josh
Re: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
Quoted 44 lines · Josh Poimboeuf
Quoted 43 lines · Ingo Molnar
* Josh Poimboeuf <[email protected]> wrote:
Ok, I see how the naming scheme I proposed won't work with all that very well, but I'd still suggest using consistently named patterns.Let me suggest yet another approach. How about open-coding something like this:
FUNCTION_START(func)push_bp mov_sp_bp ... pop_bp ret
FUNCTION_END(func)This is just two easy things:
- a redefine of the
FUNCTION_ENTRYand ENDPROC names- the introduction of three quasi-mnemonics:
push_bp,mov_sp_bp,pop_bp- which all look very similar to a real frame setup sequence, except that we can easily make them go away in the !CONFIG_FRAME_POINTERS case.The advantage of this approach would be:
- it looks pretty 'natural' and very close to how the real disassembly looks like in
CONFIG_FRAME_POINTERS=ykernels. So while it's not as compact as some of the other variants, it's close to what the real instruction sequence looks like and that is a positive quality in itself.- it also makes it apparent 'on sight' that it's probably a bug to have unbalanced push/pop sequences in a regular function, to any reasonably alert assembly coder.
- if we ever unsupport framepointer kernels in the (far far) future, we can get rid of all lines with those 3 mnemonics and be done with it.
- it's finegrained enough so that we can express all the special function/tail variants you listed above.
Quoted 8 lines
Quoted 1 line
What do you think?
I agree that the edge cases make
FUNCTION_ENTRYandFUNCTION_RETURNless attractive. Slowly we are circling around to where we started :-)Personally, I prefer FRAME/ENDFRAME instead of push_bp/mov_sp_bp/pop_bp, because it more communicates *what* it's doing rather than how. IMO, it's easier to grok with a quick glance.
Ingo, any chance this last paragraph was a convincing argument to continue to use FRAME/ENDFRAME over push_bp/mov_sp_bp/pop_bp?
(I think this is the last outstanding issue from the reviews, so I'm all set to send out a new version of the patches once there's agreement on this issue.)
Signature · 1 line
Josh
Re: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
In reply to Josh PoimboeufQuoted 4 lines · Josh Poimboeuf
The reason I suggested to put FRAME in the macro name is to try to prevent it from being accidentally used for leaf functions, where it isn't needed.
Could someone remind me why it isn't needed for leaf functions?
--Andy
Re: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
Quoted 7 lines · Andy Lutomirski
Quoted 4 lines · Josh Poimboeuf
The reason I suggested to put FRAME in the macro name is to try to prevent it from being accidentally used for leaf functions, where it isn't needed.
Could someone remind me why it isn't needed for leaf functions?
If a function doesn't call any other functions, then it won't ever show up in a stack trace unless:
a) the function itself walks the stack, in which case the frame pointer isn't necessary; or
b) The function gets hit by an interrupt/exception, in which case frame pointers can't be 100% relied upon anyway.
I've noticed that gcc *does* seem to create stack frames for leaf functions. But it's inconsistent, because the early exit path of some functions will skip the stack frame creation and go straight to the return.
We could probably get a good performance boost with the -momit-leaf-frame-pointer flag. Though it would make stack traces less reliable when a leaf function gets interrupted.
Signature · 1 line
Josh
Peter Zijlstra
maintainerRe: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
Quoted 8 lines · Josh Poimboeuf
If a function doesn't call any other functions, then it won't ever show up in a stack trace unless:
a) the function itself walks the stack, in which case the frame pointer isn't necessary; or
b) The function gets hit by an interrupt/exception, in which case frame pointers can't be 100% relied upon anyway.
In case the interrupt happens whilst setting up the frame, right?
Quoted 8 lines
I've noticed that gcc *does* seem to create stack frames for leaf functions. But it's inconsistent, because the early exit path of some functions will skip the stack frame creation and go straight to the return.
We could probably get a good performance boost with the -momit-leaf-frame-pointer flag. Though it would make stack traces less reliable when a leaf function gets interrupted.
So the information we'd loose in that case would be the location in the calling function, right? Which isn't a problem, if the current function (as obtained through RIP) is only ever called once. However if there's multiple call sites this might be a wee bit confusing.
Re: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
Quoted 11 lines · Peter Zijlstra
Quoted 8 lines · Josh Poimboeuf
If a function doesn't call any other functions, then it won't ever show up in a stack trace unless:
a) the function itself walks the stack, in which case the frame pointer isn't necessary; or
b) The function gets hit by an interrupt/exception, in which case frame pointers can't be 100% relied upon anyway.
In case the interrupt happens whilst setting up the frame, right?
Right.
Quoted 11 lines
Quoted 8 lines
I've noticed that gcc *does* seem to create stack frames for leaf functions. But it's inconsistent, because the early exit path of some functions will skip the stack frame creation and go straight to the return.
We could probably get a good performance boost with the -momit-leaf-frame-pointer flag. Though it would make stack traces less reliable when a leaf function gets interrupted.
So the information we'd loose in that case would be the location in the calling function, right?
Right.
Quoted 3 lines
Which isn't a problem, if the current function (as obtained through RIP) is only ever called once. However if there's multiple call sites this might be a wee bit confusing.
Agreed, though the stack dump code always prints '?' for any kernel address it finds on the stack. So there would still be a good clue.
Signature · 1 line
Josh
Re: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
In reply to Josh PoimboeufQuoted 18 lines · Josh Poimboeuf
Quoted 7 lines · Andy Lutomirski
Quoted 4 lines · Josh Poimboeuf
The reason I suggested to put FRAME in the macro name is to try to prevent it from being accidentally used for leaf functions, where it isn't needed.
Could someone remind me why it isn't needed for leaf functions?
If a function doesn't call any other functions, then it won't ever show up in a stack trace unless:
a) the function itself walks the stack, in which case the frame pointer isn't necessary; or
b) The function gets hit by an interrupt/exception, in which case frame pointers can't be 100% relied upon anyway.
Quoted 7 lines
I've noticed that gcc *does* seem to create stack frames for leaf functions. But it's inconsistent, because the early exit path of some functions will skip the stack frame creation and go straight to the return.
We could probably get a good performance boost with the -momit-leaf-frame-pointer flag. Though it would make stack traces less reliable when a leaf function gets interrupted.
So in theory we could resolve this during the stack walk: when we pass from the IRQ stack to the process stack we actually know the RIP of the interrupted context, and could include that.
Visualizing context boundaries in the stack dump would also be pretty useful independently of the performance boost. We already demark them to a certain degree:
[ 37.287036] Call Trace: [ 37.287042] <IRQ> [<ffffffffb5161eee>] dump_stack+0x4c/0x65 [ 37.287045] [<ffffffffb3d40288>] ? down_trylock+0x28/0x33 [ 37.287048] [<ffffffffb3cfa74d>] warn_slowpath_common+0x7b/0xb1 [ 37.287050] [<ffffffffb3cfa7f7>] warn_slowpath_fmt+0x50/0x6e [ 37.287053] [<ffffffffb3cfe0ba>] ? __do_softirq+0x224/0x289 [ 37.287055] [<ffffffffb3cfe047>] ? __do_softirq+0x1b1/0x289 [ 37.287057] [<ffffffffb3d71b4c>] can_stop_full_tick+0x69/0xa1 [ 37.287059] [<ffffffffb3d72273>] tick_nohz_irq_exit+0x72/0x98 [ 37.287061] [<ffffffffb3cfe3b5>] irq_exit+0xbf/0x104 [ 37.287064] [<ffffffffb3c6b01a>] smp_apic_timer_interrupt+0x42/0x51 [ 37.287067] [<ffffffffb51821d9>] apic_timer_interrupt+0x89/0x90 [ 37.287071] <EOI> [<ffffffffb3d283ab>] ? ___might_sleep+0x150/0x232 [ 37.287075] [<ffffffffb3d4b684>] ? lock_torture_reader+0x102/0x102
including the RIP of interrupted nested contexts would be helpful.
Thanks,
Ingo
Re: [RFC PATCH 13/21] x86/asm/crypto: Fix frame pointer usage in aesni-intel_asm.S
Quoted 25 lines · Ingo Molnar
Quoted 22 lines · Josh Poimboeuf
Quoted 7 lines · Andy Lutomirski
Quoted 4 lines · Josh Poimboeuf
The reason I suggested to put FRAME in the macro name is to try to prevent it from being accidentally used for leaf functions, where it isn't needed.
Could someone remind me why it isn't needed for leaf functions?
If a function doesn't call any other functions, then it won't ever show up in a stack trace unless:
a) the function itself walks the stack, in which case the frame pointer isn't necessary; or
b) The function gets hit by an interrupt/exception, in which case frame pointers can't be 100% relied upon anyway.
I've noticed that gcc *does* seem to create stack frames for leaf functions. But it's inconsistent, because the early exit path of some functions will skip the stack frame creation and go straight to the return.
Quoted 7 lines
Quoted 3 lines
We could probably get a good performance boost with the -momit-leaf-frame-pointer flag. Though it would make stack traces less reliable when a leaf function gets interrupted.
So in theory we could resolve this during the stack walk: when we pass from the IRQ stack to the process stack we actually know the RIP of the interrupted context, and could include that.
The problem is with the *caller* of the leaf function. Without the leaf's frame pointer there's no way to find the call site pointer on the stack, so the leaf's caller gets skipped.
Signature · 1 line
Josh
RFC PATCH 14/21 x86/asm/crypto: Move .Lbswap_mask data to .rodata section
In reply to Josh Poimboeufstackvalidate reports the following warning:
stackvalidate: arch/x86/crypto/aesni-intel_asm.o: _aesni_inc_init(): can't find starting instruction
stackvalidate gets confused when it tries to disassemble the following data in the .text section:
.Lbswap_mask:
.byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
Move it to .rodata which is a more appropriate section for read-only data.
--- arch/x86/crypto/aesni-intel_asm.S | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/crypto/aesni-intel_asm.S +2 -0
@@ -2553,9 +2553,11 @@ ENTRY(aesni_cbc_dec) ENDPROC(aesni_cbc_dec) #ifdef __x86_64__ +.pushsection .rodata .align 16 .Lbswap_mask: .byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 +.popsection /* * _aesni_inc_init: internal ABI
Signature · 1 line
2.1.0
RFC PATCH 15/21 x86/asm/crypto: Move jump_table to .rodata section
In reply to Josh Poimboeufstackvalidate reports the following warning:
stackvalidate: arch/x86/crypto/crc32c-pcl-intel-asm_64.o: crc_pcl()+0x11dd: can't decode instruction
It gets confused when trying to decode jump_table data. Move jump_table to the .rodata section which is a more appropriate home for read-only data.
--- arch/x86/crypto/crc32c-pcl-intel-asm_64.S | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/crypto/crc32c-pcl-intel-asm_64.S +4 -4
@@ -170,8 +170,8 @@ continue_block: ## branch into array lea jump_table(%rip), bufp movzxw (bufp, %rax, 2), len - offset=crc_array-jump_table - lea offset(bufp, len, 1), bufp + lea crc_array(%rip), bufp + lea (bufp, len, 1), bufp jmp *bufp ################################################################
@@ -310,7 +310,9 @@ do_return: popq %rdi popq %rbx ret +ENDPROC(crc_pcl) +.section .rodata, "a", %progbits ################################################################ ## jump table Table is 129 entries x 2 bytes each ################################################################
@@ -324,13 +326,11 @@ JMPTBL_ENTRY %i i=i+1 .endr -ENDPROC(crc_pcl) ################################################################ ## PCLMULQDQ tables ## Table is 128 entries x 2 words (8 bytes) each ################################################################ -.section .rotata, "a", %progbits .align 8 K_table: .long 0x493c7d27, 0x00000001
Signature · 1 line
2.1.0
RFC PATCH 16/21 x86/asm/crypto: Fix frame pointer usage in clmul_ghash_mul/update()
In reply to Josh Poimboeufclmul_ghash_mul() and clmul_ghash_update() are callable non-leaf functions which don't honor CONFIG_FRAME_POINTER, which can result in bad stack traces.
Create stack frames for them when CONFIG_FRAME_POINTER is enabled.
--- arch/x86/crypto/ghash-clmulni-intel_asm.S | 5 +++++ 1 file changed, 5 insertions(+)
arch/x86/crypto/ghash-clmulni-intel_asm.S +5 -0
@@ -18,6 +18,7 @@ #include <linux/linkage.h> #include <asm/inst.h> +#include <asm/frame.h> .data
@@ -94,6 +95,7 @@ ENDPROC(__clmul_gf128mul_ble) /* void clmul_ghash_mul(char *dst, const u128 *shash) */ ENTRY(clmul_ghash_mul) + FRAME movups (%rdi), DATA movups (%rsi), SHASH movaps .Lbswap_mask, BSWAP
@@ -101,6 +103,7 @@ ENTRY(clmul_ghash_mul) call __clmul_gf128mul_ble PSHUFB_XMM BSWAP DATA movups DATA, (%rdi) + ENDFRAME ret ENDPROC(clmul_ghash_mul)
@@ -109,6 +112,7 @@ ENDPROC(clmul_ghash_mul) * const u128 *shash); */ ENTRY(clmul_ghash_update) + FRAME cmp $16, %rdx jb .Lupdate_just_ret # check length movaps .Lbswap_mask, BSWAP
@@ -128,5 +132,6 @@ ENTRY(clmul_ghash_update) PSHUFB_XMM BSWAP DATA movups DATA, (%rdi) .Lupdate_just_ret: + ENDFRAME ret ENDPROC(clmul_ghash_update)
Signature · 1 line
2.1.0
RFC PATCH 17/21 x86/asm/entry: Fix frame pointer usage in thunk functions
In reply to Josh PoimboeufThunk functions are callable non-leaf functions that don't honor CONFIG_FRAME_POINTER, which can result in bad stack traces. Also they aren't annotated as ELF callable functions which can confuse tooling.
Create stack frames for them when CONFIG_FRAME_POINTER is enabled and add the ELF function type.
--- arch/x86/entry/thunk_64.S | 4 ++++ 1 file changed, 4 insertions(+)
arch/x86/entry/thunk_64.S +4 -0
@@ -8,11 +8,14 @@ #include <linux/linkage.h> #include "calling.h" #include <asm/asm.h> +#include <asm/frame.h> /* rdi: arg1 ... normal C conventions. rax is saved/restored. */ .macro THUNK name, func, put_ret_addr_in_rdi=0 .globl \name + .type \name, @function \name: + FRAME /* this one pushes 9 elems, the next one would be %rIP */ pushq %rdi
@@ -62,6 +65,7 @@ restore: popq %rdx popq %rsi popq %rdi + ENDFRAME ret _ASM_NOKPROBE(restore) #endif
Signature · 1 line
2.1.0
RFC PATCH 18/21 x86/asm/acpi: Fix frame pointer usage in do_suspend_lowlevel()
In reply to Josh Poimboeufdo_suspend_lowlevel() is a callable non-leaf function which doesn't honor CONFIG_FRAME_POINTER, which can result in bad stack traces.
Create a stack frame for it when CONFIG_FRAME_POINTER is enabled.
--- arch/x86/kernel/acpi/wakeup_64.S | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kernel/acpi/wakeup_64.S +3 -0
@@ -5,6 +5,7 @@ #include <asm/page_types.h> #include <asm/msr.h> #include <asm/asm-offsets.h> +#include <asm/frame.h> # Copyright 2003 Pavel Machek <[email protected]>, distribute under GPLv2
@@ -39,6 +40,7 @@ bogus_64_magic: jmp bogus_64_magic ENTRY(do_suspend_lowlevel) + FRAME subq $8, %rsp xorl %eax, %eax call save_processor_state
@@ -109,6 +111,7 @@ ENTRY(do_suspend_lowlevel) xorl %eax, %eax addq $8, %rsp + ENDFRAME jmp restore_processor_state ENDPROC(do_suspend_lowlevel)
Signature · 1 line
2.1.0
RFC PATCH 19/21 x86/asm: Fix frame pointer usage in rwsem functions
In reply to Josh Poimboeufrwsem.S has several callable non-leaf functions which don't honor CONFIG_FRAME_POINTER, which can result in bad stack traces.
Create stack frames for them when CONFIG_FRAME_POINTER is enabled.
--- arch/x86/lib/rwsem.S | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
arch/x86/lib/rwsem.S +10 -1
@@ -15,6 +15,7 @@ #include <linux/linkage.h> #include <asm/alternative-asm.h> +#include <asm/frame.h> #define __ASM_HALF_REG(reg) __ASM_SEL(reg, e##reg) #define __ASM_HALF_SIZE(inst) __ASM_SEL(inst##w, inst##l)
@@ -84,24 +85,29 @@ /* Fix up special calling conventions */ ENTRY(call_rwsem_down_read_failed) + FRAME save_common_regs __ASM_SIZE(push,) %__ASM_REG(dx) movq %rax,%rdi call rwsem_down_read_failed __ASM_SIZE(pop,) %__ASM_REG(dx) restore_common_regs + ENDFRAME ret ENDPROC(call_rwsem_down_read_failed) ENTRY(call_rwsem_down_write_failed) + FRAME save_common_regs movq %rax,%rdi call rwsem_down_write_failed restore_common_regs + ENDFRAME ret ENDPROC(call_rwsem_down_write_failed) ENTRY(call_rwsem_wake) + FRAME /* do nothing if still outstanding active readers */ __ASM_HALF_SIZE(dec) %__ASM_HALF_REG(dx) jnz 1f
@@ -109,15 +115,18 @@ ENTRY(call_rwsem_wake) movq %rax,%rdi call rwsem_wake restore_common_regs -1: ret +1: ENDFRAME + ret ENDPROC(call_rwsem_wake) ENTRY(call_rwsem_downgrade_wake) + FRAME save_common_regs __ASM_SIZE(push,) %__ASM_REG(dx) movq %rax,%rdi call rwsem_downgrade_wake __ASM_SIZE(pop,) %__ASM_REG(dx) restore_common_regs + ENDFRAME ret ENDPROC(call_rwsem_downgrade_wake)
Signature · 1 line
2.1.0
RFC PATCH 20/21 x86/asm/efi: Fix frame pointer usage in efi_call()
In reply to Josh Poimboeufefi_call() is a callable non-leaf function which doesn't honor CONFIG_FRAME_POINTER, which can result in bad stack traces.
Create a stack frame for it when CONFIG_FRAME_POINTER is enabled.
--- arch/x86/platform/efi/efi_stub_64.S | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/platform/efi/efi_stub_64.S +3 -0
@@ -11,6 +11,7 @@ #include <asm/msr.h> #include <asm/processor-flags.h> #include <asm/page_types.h> +#include <asm/frame.h> #define SAVE_XMM \ mov %rsp, %rax; \
@@ -74,6 +75,7 @@ .endm ENTRY(efi_call) + FRAME SAVE_XMM mov (%rsp), %rax mov 8(%rax), %rax
@@ -88,6 +90,7 @@ ENTRY(efi_call) RESTORE_PGT addq $48, %rsp RESTORE_XMM + ENDFRAME ret ENDPROC(efi_call)
Signature · 1 line
2.1.0
RFC PATCH 21/21 x86/asm/power: Fix frame pointer usage in hibernate_asm_64.S
In reply to Josh Poimboeufswsusp_arch_suspend() and restore_registers() are callable non-leaf functions which don't honor CONFIG_FRAME_POINTER, which can result in bad stack traces. Also they aren't annotated as ELF callable functions which can confuse tooling.
Create a stack frame for them when CONFIG_FRAME_POINTER is enabled and give them proper ELF function annotations.
--- arch/x86/power/hibernate_asm_64.S | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/power/hibernate_asm_64.S +7 -0
@@ -21,8 +21,10 @@ #include <asm/page_types.h> #include <asm/asm-offsets.h> #include <asm/processor-flags.h> +#include <asm/frame.h> ENTRY(swsusp_arch_suspend) + FRAME movq $saved_context, %rax movq %rsp, pt_regs_sp(%rax) movq %rbp, pt_regs_bp(%rax)
@@ -50,7 +52,9 @@ ENTRY(swsusp_arch_suspend) movq %rax, restore_cr3(%rip) call swsusp_save + ENDFRAME ret +ENDPROC(swsusp_arch_suspend) ENTRY(restore_image) /* switch to temporary page tables */
@@ -107,6 +111,7 @@ ENTRY(core_restore_code) */ ENTRY(restore_registers) + FRAME /* go back to the original page tables */ movq %rbx, %cr3
@@ -147,4 +152,6 @@ ENTRY(restore_registers) /* tell the hibernation core that we've just restored the memory */ movq %rax, in_suspend(%rip) + ENDFRAME ret +ENDPROC(restore_registers)
Signature · 1 line
2.1.0
Re: [RFC PATCH 00/21] x86: Proposed fixes for stackvalidate warnings
In reply to Josh PoimboeufQuoted 10 lines · Josh Poimboeuf
These patches fix many of the warnings reported by stackvalidate. They're based on top of the "Compile-time stack validation" v7 patch set [1].
They've been compile-tested and boot tested in a VM, but I haven't attempted any meaningful testing for most of them. This should give an idea of what kinds of changes I think are needed.
Nothing here looks all that bad, but I think the extra frame pointers staring us in the face (as opposed to the ones that gcc adds transparently) might serve as added incentive to suck it up and get CFI unwinding working.
--Andy
Re: [RFC PATCH 00/21] x86: Proposed fixes for stackvalidate warnings
Quoted 9 lines · Andy Lutomirski
Quoted 8 lines · Josh Poimboeuf
These patches fix many of the warnings reported by stackvalidate. They're based on top of the "Compile-time stack validation" v7 patch set [1].
They've been compile-tested and boot tested in a VM, but I haven't attempted any meaningful testing for most of them. This should give an idea of what kinds of changes I think are needed.
Quoted 7 lines
Quoted 2 lines
Nothing here looks all that bad, but I think the extra frame pointers staring us in the face (as opposed to the ones that gcc adds transparently) might serve as added incentive to suck it up and get CFI unwinding working.
So there are two aspects to frame pointers staring us in the face:
- Syntactically there's extra debuginfo cruft added to the source code. Not good but very fixable: I already made a couple of suggestions of how to trim all that in a way that improves general readability as well over what we have today.
- Instruction wise in the generated code. I'm afraid the 'transparent' frame pointers added by GCC are staring me in the face in perf top/report disassembly output just as much! ;-)
But naming details aside, I like the direction of these patches a lot better than that of any previous debuginfo approach, because the approach is very proactive.
( Which it really has to be, given that deep down these patches are motivated by
enabling more complex models of live kernel patching. But that's a win-win. )
Once the cleanliness of the annotations is improved and we have a reasonable path towards having no warnings on a regular kernel build, I plan on starting to apply the patches.
So if anyone has any deep objections that were kept close to the vest so far, please holler now.
Thanks,
Ingo