Skip to contentSkip to conversation

x86/asm/entry/64: simplifications

PATCH In review Denys Vlasenko 4 participants 13 messages Mar 30, 2015 – Mar 31, 2015 x86

lore
Share & tools
lore
#0 lore raw

cover x86/asm/entry/64: simplifications

This patchset simplifies jump maze in entry_64.S a bit by moving "retint_kernel" code block, and follows up with simplifications which become obvious after the move.

Two labels and one define is gone
One label is no longer .global
One redundand DISABLE_INTERRUPTS is gone
A few jump and test instructions use shorter forms now

text data bss dec hex filename

  12706              0       0   12706    31a2 entry_64.o.before
  12658              0       0   12658    3172 entry_64.o

Patchset was run-tested (running it while writing this).

Denys Vlasenko (5):
x86/asm/entry/64: move retint_kernel code block closer to its user x86/asm/entry/64: simplify retint_kernel label usage, make
retint_restore_args label local
x86/asm/entry/64: remove redundant DISABLE_INTERRUPTS x86/asm/entry/64: do not GET_THREAD_INFO() too early x86/asm/entry/64: drop exit_intr label

 arch/x86/kernel/entry_64.S | 40 +++++++++++++++++-----------------------
 1 file changed, 17 insertions(+), 23 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]>
CC: [email protected]
CC: [email protected]

Signature · 1 line
1.8.1.4
#1 lore raw

PATCH 1/5 x86/asm/entry/64: move retint_kernel code block closer to its user

"retint_kernel" code block is misplaced. Since its logical continuation is "retint_restore_args", it is more natural to place it above that label. This also makes two jumps "short".

This change only moves code block around, without changing logic.

This enables the next simplification: making "retint_restore_args" label a local numeric one.

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/kernel/entry_64.S | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)
arch/x86/kernel/entry_64.S +13 -12
index dbfc8875..34d60c3 100644
@@ -740,7 +740,19 @@ opportunistic_sysret_failed:
 	SWAPGS
 	jmp restore_args
 
-retint_restore_args:	/* return to kernel space */
+/* Returning to kernel space */
+#ifdef CONFIG_PREEMPT
+	/* Interrupts are off */
+	/* Check if we need preemption */
+ENTRY(retint_kernel)
+	cmpl	$0,PER_CPU_VAR(__preempt_count)
+	jnz	retint_restore_args
+	bt	$9,EFLAGS(%rsp)	/* interrupts were off? */
+	jnc	retint_restore_args
+	call	preempt_schedule_irq
+	jmp	exit_intr
+#endif
+retint_restore_args:
 	DISABLE_INTERRUPTS(CLBR_ANY)
 	/*
 	 * The iretq could re-enable interrupts:
@@ -830,17 +842,6 @@ retint_signal:
 	GET_THREAD_INFO(%rcx)
 	jmp retint_with_reschedule
 
-#ifdef CONFIG_PREEMPT
-	/* Returning to kernel space. Check if we need preemption */
-	/* rcx:	 threadinfo. interrupts off. */
-ENTRY(retint_kernel)
-	cmpl $0,PER_CPU_VAR(__preempt_count)
-	jnz  retint_restore_args
-	bt   $9,EFLAGS(%rsp)	/* interrupts off? */
-	jnc  retint_restore_args
-	call preempt_schedule_irq
-	jmp exit_intr
-#endif
 	CFI_ENDPROC
 END(common_interrupt)
 
Signature · 1 line
1.8.1.4
#2 lore raw

[tip:x86/asm] x86/asm/entry/64: Move retint_kernel code block closer to its user

x86/asm/entry/64: Move retint_kernel code block closer to its user
Commit-ID:  627276cb55f33e2dc56eab5b973937c128b7704c
Gitweb:     http://git.kernel.org/tip/627276cb55f33e2dc56eab5b973937c128b7704c
Author:     Denys Vlasenko <[email protected]>
AuthorDate: Mon, 30 Mar 2015 20:09:31 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 31 Mar 2015 09:31:11 +0200

x86/asm/entry/64: Move retint_kernel code block closer to its user

The "retint_kernel" code block is misplaced. Since its logical continuation is "retint_restore_args", it is more natural to place it above that label. This also makes two jumps "short".

This change only moves code block around, without changing logic.

This enables the next simplification: making
"retint_restore_args" label a local numeric one.

Cc Alexei Starovoitov <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Kees Cook <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Will Drewry <[email protected]>
---
 arch/x86/kernel/entry_64.S | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)
arch/x86/kernel/entry_64.S +13 -12
index dbfc8875..34d60c3 100644
@@ -740,7 +740,19 @@ opportunistic_sysret_failed:
 	SWAPGS
 	jmp restore_args
 
-retint_restore_args:	/* return to kernel space */
+/* Returning to kernel space */
+#ifdef CONFIG_PREEMPT
+	/* Interrupts are off */
+	/* Check if we need preemption */
+ENTRY(retint_kernel)
+	cmpl	$0,PER_CPU_VAR(__preempt_count)
+	jnz	retint_restore_args
+	bt	$9,EFLAGS(%rsp)	/* interrupts were off? */
+	jnc	retint_restore_args
+	call	preempt_schedule_irq
+	jmp	exit_intr
+#endif
+retint_restore_args:
 	DISABLE_INTERRUPTS(CLBR_ANY)
 	/*
 	 * The iretq could re-enable interrupts:
@@ -830,17 +842,6 @@ retint_signal:
 	GET_THREAD_INFO(%rcx)
 	jmp retint_with_reschedule
 
-#ifdef CONFIG_PREEMPT
-	/* Returning to kernel space. Check if we need preemption */
-	/* rcx:	 threadinfo. interrupts off. */
-ENTRY(retint_kernel)
-	cmpl $0,PER_CPU_VAR(__preempt_count)
-	jnz  retint_restore_args
-	bt   $9,EFLAGS(%rsp)	/* interrupts off? */
-	jnc  retint_restore_args
-	call preempt_schedule_irq
-	jmp exit_intr
-#endif
 	CFI_ENDPROC
 END(common_interrupt)
 
#3 lore raw

PATCH 2/5 x86/asm/entry/64: simplify retint_kernel label usage, make retint_restore_args label local

In reply to Denys Vlasenko

Get rid of #define obfuscation of retint_kernel in CONFIG_PREEMPT case by defining retint_kernel label always, not only for CONFIG_PREEMPT.

Strip retint_kernel of .global-ness (ENTRY macro) - it has no users outside of this file.

This looks like cosmetics, but it is not:
"je LABEL" can be optimized to short jump by assember only if LABEL is not global, for global labels jump is always a near one with relocation.

Convert retint_restore_args to a local numeric label, making it clearer that it is not used elsewhere in the file.

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/kernel/entry_64.S | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
arch/x86/kernel/entry_64.S +4 -8
index 34d60c3..4d723ed 100644
@@ -57,10 +57,6 @@
 	.section .entry.text, "ax"
 
 
-#ifndef CONFIG_PREEMPT
-#define retint_kernel retint_restore_args
-#endif
-
 #ifdef CONFIG_PARAVIRT
 ENTRY(native_usergs_sysret64)
 	swapgs
@@ -741,18 +737,18 @@ opportunistic_sysret_failed:
 	jmp restore_args
 
 /* Returning to kernel space */
+retint_kernel:
 #ifdef CONFIG_PREEMPT
 	/* Interrupts are off */
 	/* Check if we need preemption */
-ENTRY(retint_kernel)
 	cmpl	$0,PER_CPU_VAR(__preempt_count)
-	jnz	retint_restore_args
+	jnz	1f
 	bt	$9,EFLAGS(%rsp)	/* interrupts were off? */
-	jnc	retint_restore_args
+	jnc	1f
 	call	preempt_schedule_irq
 	jmp	exit_intr
+1:
 #endif
-retint_restore_args:
 	DISABLE_INTERRUPTS(CLBR_ANY)
 	/*
 	 * The iretq could re-enable interrupts:
Signature · 1 line
1.8.1.4
#4 lore raw

Re: [PATCH 2/5] x86/asm/entry/64: simplify retint_kernel label usage, make retint_restore_args label local

Quoted 3 lines · Denys Vlasenko

Strip retint_kernel of .global-ness (ENTRY macro) - it has no users outside of this file.

The reason for the ENTRY is to get it aligned, not because it's globally visible. It doesn't have any fallthroughs, and some cpu's care more than others.

See commit b06babac45e1 ("Add proper alignment to ENTRY").

Not that I'm sure the alignment matters all that much, but I thought I'd point it out. Removing the ENTRY() does a lot more than just remote the .globl.

Linus

#5 lore raw

PATCH 3/5 x86/asm/entry/64: remove redundant DISABLE_INTERRUPTS

In reply to Denys Vlasenko

At this location, we already have interrupts off, always. To be more specific, we already disabled them here:

ret_from_intr:
DISABLE_INTERRUPTS(CLBR_NONE)

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/kernel/entry_64.S | 1 -
 1 file changed, 1 deletion(-)
arch/x86/kernel/entry_64.S +0 -1
index 4d723ed..290ecb3 100644
@@ -749,7 +749,6 @@ retint_kernel:
 	jmp	exit_intr
 1:
 #endif
-	DISABLE_INTERRUPTS(CLBR_ANY)
 	/*
 	 * The iretq could re-enable interrupts:
 	 */
Signature · 1 line
1.8.1.4
#6 lore raw

PATCH 4/5 x86/asm/entry/64: do not GET_THREAD_INFO() too early

In reply to Denys Vlasenko

At exit_intr, we GET_THREAD_INFO(%rcx) and then jump to retint_kernel if saved CS was from kernel. But code at retint_kernel doesn't need %rcx.

Move GET_THREAD_INFO(%rcx) down, after CS check and branch.

While at it, remove "has a correct top of stack" comment. After recent changes which eliminated FIXUP_TOP_OF_STACK, we always have correct pt_regs layout.

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/kernel/entry_64.S | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/x86/kernel/entry_64.S +2 -3
index 290ecb3..16bf357 100644
@@ -654,13 +654,12 @@ ret_from_intr:
 	CFI_ADJUST_CFA_OFFSET	RBP
 
 exit_intr:
-	GET_THREAD_INFO(%rcx)
 	testl $3,CS(%rsp)
 	je retint_kernel
-
 	/* Interrupt came from user space */
+
+	GET_THREAD_INFO(%rcx)
 	/*
-	 * Has a correct top of stack.
 	 * %rcx: thread info. Interrupts off.
 	 */
 retint_with_reschedule:
Signature · 1 line
1.8.1.4
#7 lore raw

[tip:x86/asm] x86/asm/entry/64: Do not GET_THREAD_INFO() too early

x86/asm/entry/64: Do not GET_THREAD_INFO() too early
Commit-ID:  a3675b32aac81c2c4733568844f8276527a37423
Gitweb:     http://git.kernel.org/tip/a3675b32aac81c2c4733568844f8276527a37423
Author:     Denys Vlasenko <[email protected]>
AuthorDate: Mon, 30 Mar 2015 20:09:34 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 31 Mar 2015 09:31:11 +0200

x86/asm/entry/64: Do not GET_THREAD_INFO() too early

At exit_intr, we GET_THREAD_INFO(%rcx) and then jump to retint_kernel if saved CS was from kernel. But the code at retint_kernel doesn't need %rcx.

Move GET_THREAD_INFO(%rcx) down, after CS check and branch.

While at it, remove "has a correct top of stack" comment. After recent changes which eliminated FIXUP_TOP_OF_STACK, we always have a correct pt_regs layout.

Cc Alexei Starovoitov <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Kees Cook <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Will Drewry <[email protected]>
---
 arch/x86/kernel/entry_64.S | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/x86/kernel/entry_64.S +2 -3
index 34d60c3..6f251a5 100644
@@ -658,13 +658,12 @@ ret_from_intr:
 	CFI_ADJUST_CFA_OFFSET	RBP
 
 exit_intr:
-	GET_THREAD_INFO(%rcx)
 	testl $3,CS(%rsp)
 	je retint_kernel
-
 	/* Interrupt came from user space */
+
+	GET_THREAD_INFO(%rcx)
 	/*
-	 * Has a correct top of stack.
 	 * %rcx: thread info. Interrupts off.
 	 */
 retint_with_reschedule:
#8 lore raw

PATCH 5/5 x86/asm/entry/64: drop exit_intr label

In reply to Denys Vlasenko

At this label, we test whether interrupt/exception was in kernel. If it did, we jump to preemption check. If preemption does happen (IOW if we call preempt_schedule_irq), we go back to exit_intr.

But it's pointless, we already know that test succeeded last time, preemption doesn't change the fact that interrupt/exception was in kernel. We can go back directly to checking PER_CPU_VAR(__preempt_count) instead.

This makes exit_intr label unused. Dropping it.

While at it, tidy up TEST insn width to use shorter insn form, use logically correct JZ mnemonic instead of JE (this doesn't change code), use X86_EFLAGS_IF_BIT instead of literal 9.

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/kernel/entry_64.S | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
arch/x86/kernel/entry_64.S +6 -7
index 16bf357..bc90f63 100644
@@ -653,9 +653,8 @@ ret_from_intr:
 	CFI_DEF_CFA_REGISTER	rsp
 	CFI_ADJUST_CFA_OFFSET	RBP
 
-exit_intr:
-	testl $3,CS(%rsp)
-	je retint_kernel
+	testb $3,CS(%rsp)
+	jz retint_kernel
 	/* Interrupt came from user space */
 
 	GET_THREAD_INFO(%rcx)
@@ -740,12 +739,12 @@ retint_kernel:
 #ifdef CONFIG_PREEMPT
 	/* Interrupts are off */
 	/* Check if we need preemption */
-	cmpl	$0,PER_CPU_VAR(__preempt_count)
-	jnz	1f
-	bt	$9,EFLAGS(%rsp)	/* interrupts were off? */
+	bt	$X86_EFLAGS_IF_BIT,EFLAGS(%rsp)	/* interrupts were off? */
 	jnc	1f
+0:	cmpl	$0,PER_CPU_VAR(__preempt_count)
+	jnz	1f
 	call	preempt_schedule_irq
-	jmp	exit_intr
+	jmp	0b
 1:
 #endif
 	/*
Signature · 1 line
1.8.1.4
#9 lore raw

Re: [PATCH 5/5] x86/asm/entry/64: drop exit_intr label

arch/x86/kernel/entry_64.S retint_kernel: · line 742

Quoted 4 lines · Denys Vlasenko
arch/x86/kernel/entry_64.S +1 -3
-       cmpl    $0,PER_CPU_VAR(__preempt_count)
-       jnz     1f
-       bt      $9,EFLAGS(%rsp) /* interrupts were off? */
+       bt      $X86_EFLAGS_IF_BIT,EFLAGS(%rsp) /* interrupts were off? */

Since you're changing this anyway, just change it to use "testb" the way you did the other place.

Yeah, yeah, it might make it less readable, but

        testb $2,EFLAGS+1(%rsp) /* interrupts were off? */

should be smaller and faster than "bt", and the point of this commit was to make code smaller and faster, no?

Linus

#10 lore raw

PATCH v2 5/5 x86/asm/entry/64: simplify looping around preempt_schedule_irq

In reply to Denys Vlasenko

At this label, we test whether interrupt/exception was in kernel. If it did, we jump to preemption check. If preemption does happen (IOW if we call preempt_schedule_irq), we go back to exit_intr.

But it's pointless, we already know that test succeeded last time, preemption doesn't change the fact that interrupt/exception was in kernel. We can go back directly to checking PER_CPU_VAR(__preempt_count) instead.

This makes exit_intr label unused. Dropping it.

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]>

---

Changes in v2:
* split insn cleanups into a separate patch
* improve commit log message

 arch/x86/kernel/entry_64.S | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
arch/x86/kernel/entry_64.S +3 -4
index 16bf357..356266c 100644
@@ -653,7 +653,6 @@ ret_from_intr:
 	CFI_DEF_CFA_REGISTER	rsp
 	CFI_ADJUST_CFA_OFFSET	RBP
 
-exit_intr:
 	testl $3,CS(%rsp)
 	je retint_kernel
 	/* Interrupt came from user space */
@@ -740,12 +739,12 @@ retint_kernel:
 #ifdef CONFIG_PREEMPT
 	/* Interrupts are off */
 	/* Check if we need preemption */
-	cmpl	$0,PER_CPU_VAR(__preempt_count)
-	jnz	1f
 	bt	$9,EFLAGS(%rsp)	/* interrupts were off? */
 	jnc	1f
+0:	cmpl	$0,PER_CPU_VAR(__preempt_count)
+	jnz	1f
 	call	preempt_schedule_irq
-	jmp	exit_intr
+	jmp	0b
 1:
 #endif
 	/*
Signature · 1 line
1.8.1.4
#11 lore raw
In reply to Denys Vlasenko
Quoted 3 lines · Denys Vlasenko

This patchset simplifies jump maze in entry_64.S a bit by moving "retint_kernel" code block, and follows up with simplifications which become obvious after the move.

So I got conflicts with latest tip:master with 3 of the 5 patches, so I probably missed one of your older patches.

I pushed out the other two - mind merging all your pending patches on top of latest tip:master and sending out all of them as a single series, so that I can pick up all the uncontroversial bits?

Thanks,

Ingo

#12 lore raw
Quoted 7 lines · Ingo Molnar
Quoted 3 lines · Denys Vlasenko

This patchset simplifies jump maze in entry_64.S a bit by moving "retint_kernel" code block, and follows up with simplifications which become obvious after the move.

Quoted 6 lines

So I got conflicts with latest tip:master with 3 of the 5 patches, so I probably missed one of your older patches.

I pushed out the other two - mind merging all your pending patches on top of latest tip:master and sending out all of them as a single series, so that I can pick up all the uncontroversial bits?

Will do in ~15 minutes.