All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] x86: Return to kernel without IRET
@ 2014-05-02 23:53 Andy Lutomirski
  2014-05-03  4:03 ` H. Peter Anvin
  2014-05-03  4:32 ` Linus Torvalds
  0 siblings, 2 replies; 13+ messages in thread
From: Andy Lutomirski @ 2014-05-02 23:53 UTC (permalink / raw)
  To: Linus Torvalds, Thomas Gleixner, Linux Kernel Mailing List, x86,
	Steven Rostedt, Gleb Natapov, Paolo Bonzini
  Cc: Andy Lutomirski

On my box, this saves about 100ns on each interrupt and trap that
happens while running in kernel space.  This speeds up my kernel_pf
microbenchmark by about 17%.

Signed-off-by: Andy Lutomirski <luto@amacapital.net>
---

Changes from v1:
 - Comment fix *facepalm*

Changes from the RFC:
 - Much better comments
 - Rewritten to use popq_cfi directly instead of RESTORE_ARGS
 - Uses sti to restore IF so we get the interrupt shadow

 arch/x86/kernel/entry_64.S | 49 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 48 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/entry_64.S b/arch/x86/kernel/entry_64.S
index 1e96c36..504cec5 100644
--- a/arch/x86/kernel/entry_64.S
+++ b/arch/x86/kernel/entry_64.S
@@ -1033,9 +1033,56 @@ retint_swapgs:		/* return to user-space */
 retint_restore_args:	/* return to kernel space */
 	DISABLE_INTERRUPTS(CLBR_ANY)
 	/*
-	 * The iretq could re-enable interrupts:
+	 * The sti could re-enable interrupts:
 	 */
 	TRACE_IRQS_IRETQ
+
+	/*
+	 * Fast return to kernel.  The stack looks like:
+	 *
+	 * previous frame
+	 * possible 8 byte gap for alignment
+	 * SS RSP EFLAGS CS RIP
+	 * ORIG_RAX RDI ... R11
+	 *
+	 * We rewrite it to:
+	 *
+	 * previous frame
+	 * RIP (EFLAGS & ~IF) ...
+	 * pointer to the EFLAGS slot
+	 * RDI ... R11
+	 */
+	movq RSP-ARGOFFSET(%rsp), %rsi
+	subq $16, %rsi
+	movq EFLAGS-ARGOFFSET(%rsp), %rdi
+	movq RIP-ARGOFFSET(%rsp), %rcx
+	btr $9, %rdi
+	movq %rdi, (%rsi)
+	movq %rcx, 8(%rsi)
+	movq %rsi, ORIG_RAX-ARGOFFSET(%rsp)
+	popq_cfi %r11
+	popq_cfi %r10
+	popq_cfi %r9
+	popq_cfi %r8
+	popq_cfi %rax
+	popq_cfi %rcx
+	popq_cfi %rdx
+	popq_cfi %rsi
+	popq_cfi %rdi
+
+	popq %rsp
+	jc 1f
+	/* Interrupts were not enabled */
+	popfq_cfi
+	retq
+1:
+	CFI_ADJUST_CFA_OFFSET 8
+	/* Interrupts were enabled */
+	popfq_cfi
+	sti
+	/* Interrupts are still off because of the one-insn grace period. */
+	retq
+
 restore_args:
 	RESTORE_ARGS 1,8,1
 
-- 
1.9.0


^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2014-05-05 15:47 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-05-02 23:53 [PATCH v2] x86: Return to kernel without IRET Andy Lutomirski
2014-05-03  4:03 ` H. Peter Anvin
2014-05-03 11:24   ` Steven Rostedt
2014-05-03 22:19     ` H. Peter Anvin
2014-05-03 23:51       ` Andy Lutomirski
2014-05-04  0:31         ` Andy Lutomirski
2014-05-04  2:15           ` H. Peter Anvin
2014-05-04  2:14         ` H. Peter Anvin
2014-05-05 15:47       ` Andy Lutomirski
2014-05-03  4:32 ` Linus Torvalds
2014-05-03  6:12   ` H. Peter Anvin
2014-05-03 13:54     ` Linus Torvalds
2014-05-03 19:00       ` H. Peter Anvin

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.