Skip to contentSkip to conversation

x86: do not save callee-preserved registers around lockdep_sys_exit_thunk

PATCH In review Denys Vlasenko 3 participants 6 messages Feb 28, 2015 – Mar 2, 2015 x86

lore
Share & tools
lore
#0 lore raw

cover x86: do not save callee-preserved registers around lockdep_sys_exit_thunk

Recent change to struct pt_regs handling in entry.S, among other things, changed how callee-preserved registers are saved around call to lockdep_sys_exit_thunk:

 #    define LOCKDEP_SYS_EXIT_IRQ \
        TRACE_IRQS_ON; \
        sti; \
        SAVE_EXTRA_REGS; \    <======= HERE
        LOCKDEP_SYS_EXIT; \
        RESTORE_EXTRA_REGS; \ <======= HERE

The change is in fact a bit fragile:
now we assume that LOCKDEP_SYS_EXIT_IRQ macro is used only when there is a struct pt_regs on the stack.

So far this assumption is true, but it is probably a coincidence.

We can revert back to pushing registers, but this callee-preserved registers saving appears bogus. It is not necessary: they _are_ callee-preserved, and lockdep_sys_exit() does not touch them on the stack either.

First two patches clean up #define maze so that we can see
that LOCKDEP_SYS_EXIT macro call above is merely "call lockdep_sys_exit_thunk".

Last patch removes SAVE/RESTORE.

I tested this patch series and everything seems to run fine on a lockdep-enabled kernel with these lines removed.

Denys Vlasenko (3):
x86: move ARCH_LOCKDEP_SYS_EXIT[IRQ] defines closer to their users
x86: fold ARCH_LOCKDEP_SYS_EXIT defines into their users
x86: do not save callee-preserved registers around lockdep_sys_exit_thunk

 arch/x86/include/asm/irqflags.h | 45 +++++++++++++++++------------------------
 1 file changed, 19 insertions(+), 26 deletions(-)
CC Linus Torvalds <[email protected]>
CC Steven Rostedt <[email protected]>
CC Ingo Molnar <[email protected]>
CC Borislav Petkov <[email protected]>
CC "H. Peter Anvin" <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Oleg Nesterov <[email protected]>
CC Frederic Weisbecker <[email protected]>
CC Alexei Starovoitov <[email protected]>
CC Will Drewry <[email protected]>
CC Kees Cook <[email protected]>
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH 1/3 x86: move ARCH_LOCKDEP_SYS_EXIT[IRQ] defines closer to their users

This change simply moves defines around (even if it's not obvious in a patch form). Nothing is changed.

This is a preparation for folding ARCH_LOCKDEP_SYS_EXIT defines into their users.

CC Linus Torvalds <[email protected]>
CC Steven Rostedt <[email protected]>
CC Ingo Molnar <[email protected]>
CC Borislav Petkov <[email protected]>
CC "H. Peter Anvin" <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Oleg Nesterov <[email protected]>
CC Frederic Weisbecker <[email protected]>
CC Alexei Starovoitov <[email protected]>
CC Will Drewry <[email protected]>
CC Kees Cook <[email protected]>
---
 arch/x86/include/asm/irqflags.h | 37 +++++++++++++++++--------------------
 1 file changed, 17 insertions(+), 20 deletions(-)
arch/x86/include/asm/irqflags.h +17 -20
index 021bee9..55866c2 100644
@@ -163,12 +163,20 @@ static inline int arch_irqs_disabled(void)
 
 	return arch_irqs_disabled_flags(flags);
 }
+#endif /* !__ASSEMBLY__ */
 
+#ifdef __ASSEMBLY__
+#ifdef CONFIG_TRACE_IRQFLAGS
+#  define TRACE_IRQS_ON		call trace_hardirqs_on_thunk;
+#  define TRACE_IRQS_OFF	call trace_hardirqs_off_thunk;
 #else
-
-#ifdef CONFIG_X86_64
-#define ARCH_LOCKDEP_SYS_EXIT		call lockdep_sys_exit_thunk
-#define ARCH_LOCKDEP_SYS_EXIT_IRQ	\
+#  define TRACE_IRQS_ON
+#  define TRACE_IRQS_OFF
+#endif
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
+#  ifdef CONFIG_X86_64
+#    define ARCH_LOCKDEP_SYS_EXIT	call lockdep_sys_exit_thunk
+#    define ARCH_LOCKDEP_SYS_EXIT_IRQ \
 	TRACE_IRQS_ON; \
 	sti; \
 	SAVE_EXTRA_REGS; \
@@ -176,9 +184,8 @@ static inline int arch_irqs_disabled(void)
 	RESTORE_EXTRA_REGS; \
 	cli; \
 	TRACE_IRQS_OFF;
-
-#else
-#define ARCH_LOCKDEP_SYS_EXIT			\
+#  else
+#    define ARCH_LOCKDEP_SYS_EXIT \
 	pushl %eax;				\
 	pushl %ecx;				\
 	pushl %edx;				\
@@ -186,24 +193,14 @@ static inline int arch_irqs_disabled(void)
 	popl %edx;				\
 	popl %ecx;				\
 	popl %eax;
-
-#define ARCH_LOCKDEP_SYS_EXIT_IRQ
-#endif
-
-#ifdef CONFIG_TRACE_IRQFLAGS
-#  define TRACE_IRQS_ON		call trace_hardirqs_on_thunk;
-#  define TRACE_IRQS_OFF	call trace_hardirqs_off_thunk;
-#else
-#  define TRACE_IRQS_ON
-#  define TRACE_IRQS_OFF
-#endif
-#ifdef CONFIG_DEBUG_LOCK_ALLOC
+#    define ARCH_LOCKDEP_SYS_EXIT_IRQ
+#  endif
 #  define LOCKDEP_SYS_EXIT	ARCH_LOCKDEP_SYS_EXIT
 #  define LOCKDEP_SYS_EXIT_IRQ	ARCH_LOCKDEP_SYS_EXIT_IRQ
 # else
 #  define LOCKDEP_SYS_EXIT
 #  define LOCKDEP_SYS_EXIT_IRQ
 # endif
-
 #endif /* __ASSEMBLY__ */
+
 #endif
Signature · 1 line
1.8.1.4
#2 lore raw

PATCH 2/3 x86: fold ARCH_LOCKDEP_SYS_EXIT defines into their users

In reply to Denys Vlasenko

There is no need to have an extra level of macro indirection here.

CC Linus Torvalds <[email protected]>
CC Steven Rostedt <[email protected]>
CC Ingo Molnar <[email protected]>
CC Borislav Petkov <[email protected]>
CC "H. Peter Anvin" <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Oleg Nesterov <[email protected]>
CC Frederic Weisbecker <[email protected]>
CC Alexei Starovoitov <[email protected]>
CC Will Drewry <[email protected]>
CC Kees Cook <[email protected]>
---
 arch/x86/include/asm/irqflags.h | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
arch/x86/include/asm/irqflags.h +7 -9
index 55866c2..19355f3 100644
@@ -175,17 +175,17 @@ static inline int arch_irqs_disabled(void)
 #endif
 #ifdef CONFIG_DEBUG_LOCK_ALLOC
 #  ifdef CONFIG_X86_64
-#    define ARCH_LOCKDEP_SYS_EXIT	call lockdep_sys_exit_thunk
-#    define ARCH_LOCKDEP_SYS_EXIT_IRQ \
+#    define LOCKDEP_SYS_EXIT		call lockdep_sys_exit_thunk
+#    define LOCKDEP_SYS_EXIT_IRQ \
 	TRACE_IRQS_ON; \
 	sti; \
 	SAVE_EXTRA_REGS; \
-	LOCKDEP_SYS_EXIT; \
+	call lockdep_sys_exit_thunk; \
 	RESTORE_EXTRA_REGS; \
 	cli; \
 	TRACE_IRQS_OFF;
 #  else
-#    define ARCH_LOCKDEP_SYS_EXIT \
+#    define LOCKDEP_SYS_EXIT \
 	pushl %eax;				\
 	pushl %ecx;				\
 	pushl %edx;				\
@@ -193,14 +193,12 @@ static inline int arch_irqs_disabled(void)
 	popl %edx;				\
 	popl %ecx;				\
 	popl %eax;
-#    define ARCH_LOCKDEP_SYS_EXIT_IRQ
+#    define LOCKDEP_SYS_EXIT_IRQ
 #  endif
-#  define LOCKDEP_SYS_EXIT	ARCH_LOCKDEP_SYS_EXIT
-#  define LOCKDEP_SYS_EXIT_IRQ	ARCH_LOCKDEP_SYS_EXIT_IRQ
-# else
+#else
 #  define LOCKDEP_SYS_EXIT
 #  define LOCKDEP_SYS_EXIT_IRQ
-# endif
+#endif
 #endif /* __ASSEMBLY__ */
 
 #endif
Signature · 1 line
1.8.1.4
#3 lore raw

PATCH 3/3 x86: do not save callee-preserved registers around lockdep_sys_exit_thunk

In reply to Denys Vlasenko

Internally, lockdep_sys_exit_thunk saves callee-clobbered registers, and calls a C function, lockdep_sys_exix. Thus, callee-preserved won't be mangled, there is no need to save them.

Patch was run-tested.

CC Linus Torvalds <[email protected]>
CC Steven Rostedt <[email protected]>
CC Ingo Molnar <[email protected]>
CC Borislav Petkov <[email protected]>
CC "H. Peter Anvin" <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Oleg Nesterov <[email protected]>
CC Frederic Weisbecker <[email protected]>
CC Alexei Starovoitov <[email protected]>
CC Will Drewry <[email protected]>
CC Kees Cook <[email protected]>
---
 arch/x86/include/asm/irqflags.h | 2 --
 1 file changed, 2 deletions(-)
arch/x86/include/asm/irqflags.h +0 -2
index 19355f3..9a63eae 100644
@@ -179,9 +179,7 @@ static inline int arch_irqs_disabled(void)
 #    define LOCKDEP_SYS_EXIT_IRQ \
 	TRACE_IRQS_ON; \
 	sti; \
-	SAVE_EXTRA_REGS; \
 	call lockdep_sys_exit_thunk; \
-	RESTORE_EXTRA_REGS; \
 	cli; \
 	TRACE_IRQS_OFF;
 #  else
Signature · 1 line
1.8.1.4
#4 lore raw
Quoted 5 lines · Denys Vlasenko

Internally, lockdep_sys_exit_thunk saves callee-clobbered registers, and calls a C function, lockdep_sys_exix. Thus, callee-preserved won't be mangled, there is no need to save them.

Patch was run-tested.

Peterz, I think this was your code.

--Andy

Quoted 36 lines
CC Linus Torvalds <[email protected]>
CC Steven Rostedt <[email protected]>
CC Ingo Molnar <[email protected]>
CC Borislav Petkov <[email protected]>
CC "H. Peter Anvin" <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Oleg Nesterov <[email protected]>
CC Frederic Weisbecker <[email protected]>
CC Alexei Starovoitov <[email protected]>
CC Will Drewry <[email protected]>
CC Kees Cook <[email protected]>
---
 arch/x86/include/asm/irqflags.h | 2 --
 1 file changed, 2 deletions(-)
arch/x86/include/asm/irqflags.h +0 -3
index 19355f3..9a63eae 100644
@@ -179,9 +179,7 @@ static inline int arch_irqs_disabled(void)
 #    define LOCKDEP_SYS_EXIT_IRQ \
        TRACE_IRQS_ON; \
        sti; \
-       SAVE_EXTRA_REGS; \
        call lockdep_sys_exit_thunk; \
-       RESTORE_EXTRA_REGS; \
        cli; \
        TRACE_IRQS_OFF;
 #  else
--

1.8.1.4

Signature · 2 lines
Andy Lutomirski
AMA Capital Management, LLC
#5 lore raw
Quoted 8 lines · Andy Lutomirski
Quoted 5 lines · Denys Vlasenko

Internally, lockdep_sys_exit_thunk saves callee-clobbered registers, and calls a C function, lockdep_sys_exix. Thus, callee-preserved won't be mangled, there is no need to save them.

Patch was run-tested.

Peterz, I think this was your code.

It was, I'm forever unsure on when what register are saved by which macros/functions.

Thanks!