From: Nadav Amit <namit@vmware.com>
To: <linux-kernel@vger.kernel.org>, <x86@kernel.org>
Cc: <nadav.amit@gmail.com>, Nadav Amit <namit@vmware.com>,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, "H. Peter Anvin" <hpa@zytor.com>,
Peter Zijlstra <peterz@infradead.org>,
Josh Poimboeuf <jpoimboe@redhat.com>
Subject: [PATCH 2/6] x86: bug: prevent gcc distortions
Date: Thu, 17 May 2018 09:13:58 -0700 [thread overview]
Message-ID: <20180517161402.78089-3-namit@vmware.com> (raw)
In-Reply-To: <20180517161402.78089-1-namit@vmware.com>
GCC considers the number of statements in inlined assembly blocks,
according to new-lines and semicolons, as an indication to the cost of
the block in time and space. This data is distorted by the kernel code,
which puts information in alternative sections. As a result, the
compiler may perform incorrect inlining and branch optimizations.
The solution is to set an assembly macro and call it from the inlinedv
assembly block. As a result GCC considers the inline assembly block as
a single instruction.
This patch increases the kernel size:
text data bss dec hex filename
18126824 10067268 2936832 31130924 1db052c ./vmlinux before
18127205 10068388 2936832 31132425 1db0b09 ./vmlinux after (+1501)
But enables more aggressive inlining (and probably branch decisions).
The number of static text symbols in vmlinux is lower.
Before: 40015
After: 39860 (-155)
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: x86@kernel.org
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Nadav Amit <namit@vmware.com>
---
arch/x86/include/asm/bug.h | 56 +++++++++++++++++++++++++-------------
1 file changed, 37 insertions(+), 19 deletions(-)
diff --git a/arch/x86/include/asm/bug.h b/arch/x86/include/asm/bug.h
index 6804d6642767..1167e4822a34 100644
--- a/arch/x86/include/asm/bug.h
+++ b/arch/x86/include/asm/bug.h
@@ -30,33 +30,51 @@
#ifdef CONFIG_DEBUG_BUGVERBOSE
-#define _BUG_FLAGS(ins, flags) \
+/*
+ * Saving the bug data is encapsulated within an assembly macro, which is then
+ * called on each use. This hack is necessary to prevent GCC from considering
+ * the inline assembly blocks as costly in time and space, which can prevent
+ * function inlining and lead to other bad compilation decisions. GCC computes
+ * inline assembly cost according to the number of perceived number of assembly
+ * instruction, based on the number of new-lines and semicolons in the assembly
+ * block. The macro will eventually be compiled into a single instruction (and
+ * some data). This scheme allows GCC to better understand the inline asm cost.
+ */
+asm(".macro __BUG_FLAGS ins:req file:req line:req flags:req size:req\n"
+ "1:\t \\ins\n\t"
+ ".pushsection __bug_table,\"aw\"\n"
+ "2:\t "__BUG_REL(1b) "\t# bug_entry::bug_addr\n\t"
+ __BUG_REL(\\file) "\t# bug_entry::file\n\t"
+ ".word \\line" "\t# bug_entry::line\n\t"
+ ".word \\flags" "\t# bug_entry::flags\n\t"
+ ".org 2b+\\size\n\t"
+ ".popsection\n\t"
+ ".endm");
+
+#define _BUG_FLAGS(ins, flags) \
do { \
- asm volatile("1:\t" ins "\n" \
- ".pushsection __bug_table,\"aw\"\n" \
- "2:\t" __BUG_REL(1b) "\t# bug_entry::bug_addr\n" \
- "\t" __BUG_REL(%c0) "\t# bug_entry::file\n" \
- "\t.word %c1" "\t# bug_entry::line\n" \
- "\t.word %c2" "\t# bug_entry::flags\n" \
- "\t.org 2b+%c3\n" \
- ".popsection" \
- : : "i" (__FILE__), "i" (__LINE__), \
- "i" (flags), \
+ asm volatile("__BUG_FLAGS \"" ins "\" %c0 %c1 %c2 %c3" \
+ : : "i" (__FILE__), "i" (__LINE__), \
+ "i" (flags), \
"i" (sizeof(struct bug_entry))); \
} while (0)
#else /* !CONFIG_DEBUG_BUGVERBOSE */
+asm(".macro __BUG_FLAGS ins:req flags:req size:req\n"
+ "1:\t\\ins\n\t"
+ ".pushsection __bug_table,\"aw\"\n"
+ "2:\t" __BUG_REL(1b) "\t# bug_entry::bug_addr\n\t"
+ ".word \\flags" "\t# bug_entry::flags\n\t"
+ ".org 2b+\\size\n\t"
+ ".popsection\n\t"
+ ".endm");
+
#define _BUG_FLAGS(ins, flags) \
do { \
- asm volatile("1:\t" ins "\n" \
- ".pushsection __bug_table,\"aw\"\n" \
- "2:\t" __BUG_REL(1b) "\t# bug_entry::bug_addr\n" \
- "\t.word %c0" "\t# bug_entry::flags\n" \
- "\t.org 2b+%c1\n" \
- ".popsection" \
- : : "i" (flags), \
- "i" (sizeof(struct bug_entry))); \
+ asm volatile("__BUG_FLAGS \"" ins "\" %c0 %c1" \
+ : : "i" (flags), \
+ "i" (sizeof(struct bug_entry))); \
} while (0)
#endif /* CONFIG_DEBUG_BUGVERBOSE */
--
2.17.0
next prev parent reply other threads:[~2018-05-17 23:28 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-05-17 16:13 [PATCH 0/6] Macrofying inline assembly for better compilation Nadav Amit
2018-05-17 16:13 ` [PATCH 1/6] x86: objtool: use asm macro for better compiler decisions Nadav Amit
2018-05-17 16:13 ` Nadav Amit [this message]
2018-05-18 7:58 ` [PATCH 2/6] x86: bug: prevent gcc distortions Peter Zijlstra
2018-05-18 8:13 ` Ingo Molnar
2018-05-18 10:11 ` Borislav Petkov
2018-05-18 14:36 ` Nadav Amit
2018-05-18 15:40 ` Borislav Petkov
2018-05-18 15:46 ` Nadav Amit
2018-05-18 15:53 ` Borislav Petkov
2018-05-18 16:29 ` Nadav Amit
2018-05-18 17:41 ` Boris Petkov
2018-05-18 14:30 ` Nadav Amit
2018-05-18 14:22 ` Nadav Amit
2018-05-18 17:52 ` Joe Perches
2018-05-18 16:24 ` Linus Torvalds
2018-05-18 17:24 ` Nadav Amit
2018-05-18 18:25 ` Linus Torvalds
2018-05-18 18:33 ` hpa
2018-05-18 18:50 ` Linus Torvalds
2018-05-18 18:53 ` hpa
2018-05-18 19:02 ` Nadav Amit
2018-05-18 19:05 ` hpa
2018-05-18 19:11 ` Linus Torvalds
2018-05-18 19:18 ` Nadav Amit
2018-05-18 19:21 ` Linus Torvalds
2018-05-18 19:22 ` hpa
2018-05-18 19:36 ` Nadav Amit
2018-05-18 19:41 ` hpa
2018-05-17 16:13 ` [PATCH 3/6] x86: alternative: macrofy locks for better inlining Nadav Amit
2018-05-17 16:14 ` [PATCH 4/6] x86: prevent inline distortion by paravirt ops Nadav Amit
2018-05-17 16:14 ` [PATCH 5/6] x86: refcount: prevent gcc distortions Nadav Amit
2018-05-19 4:27 ` kbuild test robot
2018-05-17 16:14 ` [PATCH 6/6] x86: removing unneeded new-lines Nadav Amit
2018-05-18 9:20 ` [PATCH 0/6] Macrofying inline assembly for better compilation David Laight
2018-05-18 14:15 ` Nadav Amit
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180517161402.78089-3-namit@vmware.com \
--to=namit@vmware.com \
--cc=hpa@zytor.com \
--cc=jpoimboe@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=nadav.amit@gmail.com \
--cc=peterz@infradead.org \
--cc=tglx@linutronix.de \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox