* [PATCH v6 0/9] x86: macrofying inline asm for better compilation
@ 2018-06-22 17:22 Nadav Amit
2018-06-22 17:22 ` [PATCH v6 2/9] x86: objtool: use asm macro for better compiler decisions Nadav Amit
2018-07-11 1:59 ` [PATCH v6 0/9] x86: macrofying inline asm for better compilation Nadav Amit
0 siblings, 2 replies; 5+ messages in thread
From: Nadav Amit @ 2018-06-22 17:22 UTC (permalink / raw)
To: linux-kernel, x86
Cc: Nadav Amit, Masahiro Yamada, Sam Ravnborg, Alok Kataria,
Christopher Li, Greg Kroah-Hartman, H. Peter Anvin, Ingo Molnar,
Jan Beulich, Josh Poimboeuf, Juergen Gross, Kate Stewart,
Kees Cook, linux-sparse, Peter Zijlstra, Philippe Ombredanne,
Thomas Gleixner, virtualization, Linus Torvalds
This patch-set deals with an interesting yet stupid problem: kernel code
that does not get inlined despite its simplicity. There are several
causes for this behavior: "cold" attribute on __init, different function
optimization levels; conditional constant computations based on
__builtin_constant_p(); and finally large inline assembly blocks.
This patch-set deals with the inline assembly problem. I separated these
patches from the others (that were sent in the RFC) for easier
inclusion. I also separated the removal of unnecessary new-lines which
would be sent separately.
The problem with inline assembly is that inline assembly is often used
by the kernel for things that are other than code - for example,
assembly directives and data. GCC however is oblivious to the content of
the blocks and assumes their cost in space and time is proportional to
the number of the perceived assembly "instruction", according to the
number of newlines and semicolons. Alternatives, paravirt and other
mechanisms are affected, causing code not to be inlined, and degrading
compilation quality in general.
The solution that this patch-set carries for this problem is to create
an assembly macro, and then call it from the inline assembly block. As
a result, the compiler sees a single "instruction" and assigns the more
appropriate cost to the code.
To avoid uglification of the code, as many noted, the macros are first
precompiled into an assembly file, which is later assembled together
with the C files. This also enables to avoid duplicate implementation
that was set before for the asm and C code. This can be seen in the
exception table changes.
Overall this patch-set slightly increases the kernel size (my build was
done using my Ubuntu 18.04 config + localyesconfig for the record):
text data bss dec hex filename
18140829 10224724 2957312 31322865 1ddf2f1 ./vmlinux before
18163608 10227348 2957312 31348268 1de562c ./vmlinux after (+0.1%)
The number of static functions in the image is reduced by 379, but
actually inlining is even better, which does not always shows in these
numbers: a function may be inlined causing the calling function not to
be inlined.
I ran some limited number of benchmarks, and in general the performance
impact is not very notable. You can still see >10 cycles shaved off some
syscalls that manipulate page-tables (e.g., mprotect()), in which
paravirt caused many functions not to be inlined. In addition this
patch-set can prevent issues such as [1], and improves code readability
and maintainability.
[1] https://patchwork.kernel.org/patch/10450037/
v5->v6: * Removing more code from jump-labels (PeterZ)
* Fix build issue on i386 (0-day, PeterZ)
v4->v5: * Makefile fixes (Masahiro, Sam)
v3->v4: * Changed naming of macros in 2 patches (PeterZ)
* Minor cleanup of the paravirt patch
v2->v3: * Several build issues resolved (0-day)
* Wrong comments fix (Josh)
* Change asm vs C order in refcount (Kees)
v1->v2: * Compiling the macros into a separate .s file, improving
readability (Linus)
* Improving assembly formatting, applying most of the comments
according to my judgment (Jan)
* Adding exception-table, cpufeature and jump-labels
* Removing new-line cleanup; to be submitted separately
Cc: Masahiro Yamada <yamada.masahiro@socionext.com>
Cc: Sam Ravnborg <sam@ravnborg.org>
Cc: Alok Kataria <akataria@vmware.com>
Cc: Christopher Li <sparse@chrisli.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jan Beulich <JBeulich@suse.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Juergen Gross <jgross@suse.com>
Cc: Kate Stewart <kstewart@linuxfoundation.org>
Cc: Kees Cook <keescook@chromium.org>
Cc: linux-sparse@vger.kernel.org
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Philippe Ombredanne <pombredanne@nexb.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: virtualization@lists.linux-foundation.org
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: x86@kernel.org
Nadav Amit (9):
Makefile: Prepare for using macros for inline asm
x86: objtool: use asm macro for better compiler decisions
x86: refcount: prevent gcc distortions
x86: alternatives: macrofy locks for better inlining
x86: bug: prevent gcc distortions
x86: prevent inline distortion by paravirt ops
x86: extable: use macros instead of inline assembly
x86: cpufeature: use macros instead of inline assembly
x86: jump-labels: use macros instead of inline assembly
Makefile | 9 ++-
arch/x86/Makefile | 11 ++-
arch/x86/entry/calling.h | 2 +-
arch/x86/include/asm/alternative-asm.h | 20 ++++--
arch/x86/include/asm/alternative.h | 11 +--
arch/x86/include/asm/asm.h | 61 +++++++---------
arch/x86/include/asm/bug.h | 98 +++++++++++++++-----------
arch/x86/include/asm/cpufeature.h | 82 ++++++++++++---------
arch/x86/include/asm/jump_label.h | 77 ++++++++------------
arch/x86/include/asm/paravirt_types.h | 56 +++++++--------
arch/x86/include/asm/refcount.h | 74 +++++++++++--------
arch/x86/kernel/macros.S | 16 +++++
include/asm-generic/bug.h | 8 +--
include/linux/compiler.h | 56 +++++++++++----
scripts/Kbuild.include | 4 +-
scripts/mod/Makefile | 2 +
16 files changed, 331 insertions(+), 256 deletions(-)
create mode 100644 arch/x86/kernel/macros.S
--
2.17.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v6 2/9] x86: objtool: use asm macro for better compiler decisions
2018-06-22 17:22 [PATCH v6 0/9] x86: macrofying inline asm for better compilation Nadav Amit
@ 2018-06-22 17:22 ` Nadav Amit
2018-07-11 1:59 ` [PATCH v6 0/9] x86: macrofying inline asm for better compilation Nadav Amit
1 sibling, 0 replies; 5+ messages in thread
From: Nadav Amit @ 2018-06-22 17:22 UTC (permalink / raw)
To: linux-kernel, x86; +Cc: Nadav Amit, Christopher Li, linux-sparse
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.
In the case of objtool, this distortion is extreme, since anyhow the
annotations of objtool are discarded during linkage.
The solution is to set an assembly macro and call it from the inline
assembly block. As a result GCC considers the inline assembly block as
a single instruction.
This patch slightly increases the kernel size.
text data bss dec hex filename
18140829 10224724 2957312 31322865 1ddf2f1 ./vmlinux before
18140970 10225412 2957312 31323694 1ddf62e ./vmlinux after (+829)
Static text symbols:
Before: 40321
After: 40302 (-19)
Cc: Christopher Li <sparse@chrisli.org>
Cc: linux-sparse@vger.kernel.org
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Nadav Amit <namit@vmware.com>
---
arch/x86/kernel/macros.S | 2 ++
include/linux/compiler.h | 56 ++++++++++++++++++++++++++++++----------
2 files changed, 45 insertions(+), 13 deletions(-)
diff --git a/arch/x86/kernel/macros.S b/arch/x86/kernel/macros.S
index cfc1c7d1a6eb..cee28c3246dc 100644
--- a/arch/x86/kernel/macros.S
+++ b/arch/x86/kernel/macros.S
@@ -5,3 +5,5 @@
* commonly used. The macros are precompiled into assmebly file which is later
* assembled together with each compiled file.
*/
+
+#include <linux/compiler.h>
diff --git a/include/linux/compiler.h b/include/linux/compiler.h
index 42506e4d1f53..2688f0d826e9 100644
--- a/include/linux/compiler.h
+++ b/include/linux/compiler.h
@@ -99,22 +99,13 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
* unique, to convince GCC not to merge duplicate inline asm statements.
*/
#define annotate_reachable() ({ \
- asm volatile("%c0:\n\t" \
- ".pushsection .discard.reachable\n\t" \
- ".long %c0b - .\n\t" \
- ".popsection\n\t" : : "i" (__COUNTER__)); \
+ asm volatile("ANNOTATE_REACHABLE counter=%c0" \
+ : : "i" (__COUNTER__)); \
})
#define annotate_unreachable() ({ \
- asm volatile("%c0:\n\t" \
- ".pushsection .discard.unreachable\n\t" \
- ".long %c0b - .\n\t" \
- ".popsection\n\t" : : "i" (__COUNTER__)); \
+ asm volatile("ANNOTATE_UNREACHABLE counter=%c0" \
+ : : "i" (__COUNTER__)); \
})
-#define ASM_UNREACHABLE \
- "999:\n\t" \
- ".pushsection .discard.unreachable\n\t" \
- ".long 999b - .\n\t" \
- ".popsection\n\t"
#else
#define annotate_reachable()
#define annotate_unreachable()
@@ -280,6 +271,45 @@ unsigned long read_word_at_a_time(const void *addr)
#endif /* __KERNEL__ */
+#else /* __ASSEMBLY__ */
+
+#ifdef __KERNEL__
+#ifndef LINKER_SCRIPT
+
+#ifdef CONFIG_STACK_VALIDATION
+.macro ANNOTATE_UNREACHABLE counter:req
+\counter:
+ .pushsection .discard.unreachable
+ .long \counter\()b -.
+ .popsection
+.endm
+
+.macro ANNOTATE_REACHABLE counter:req
+\counter:
+ .pushsection .discard.reachable
+ .long \counter\()b -.
+ .popsection
+.endm
+
+.macro ASM_UNREACHABLE
+999:
+ .pushsection .discard.unreachable
+ .long 999b - .
+ .popsection
+.endm
+#else /* CONFIG_STACK_VALIDATION */
+.macro ANNOTATE_UNREACHABLE counter:req
+.endm
+
+.macro ANNOTATE_REACHABLE counter:req
+.endm
+
+.macro ASM_UNREACHABLE
+.endm
+#endif /* CONFIG_STACK_VALIDATION */
+
+#endif /* LINKER_SCRIPT */
+#endif /* __KERNEL__ */
#endif /* __ASSEMBLY__ */
#ifndef __optimize
--
2.17.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH v6 0/9] x86: macrofying inline asm for better compilation
2018-06-22 17:22 [PATCH v6 0/9] x86: macrofying inline asm for better compilation Nadav Amit
2018-06-22 17:22 ` [PATCH v6 2/9] x86: objtool: use asm macro for better compiler decisions Nadav Amit
@ 2018-07-11 1:59 ` Nadav Amit
2018-07-15 21:54 ` [kbuild ack?] " Ingo Molnar
1 sibling, 1 reply; 5+ messages in thread
From: Nadav Amit @ 2018-07-11 1:59 UTC (permalink / raw)
To: Linux Kernel Mailing List, X86 ML, Ingo Molnar, Thomas Gleixner
Cc: Masahiro Yamada, Sam Ravnborg, Alok Kataria, Christopher Li,
Greg Kroah-Hartman, H. Peter Anvin, Jan Beulich, Josh Poimboeuf,
Juergen Gross, Kate Stewart, Kees Cook,
linux-sparse@vger.kernel.org, Peter Zijlstra, Philippe Ombredanne,
virtualization@lists.linux-foundation.org, Linus Torvalds
at 1:22 PM, Nadav Amit <namit@vmware.com> wrote:
> This patch-set deals with an interesting yet stupid problem: kernel code
> that does not get inlined despite its simplicity. There are several
> causes for this behavior: "cold" attribute on __init, different function
> optimization levels; conditional constant computations based on
> __builtin_constant_p(); and finally large inline assembly blocks.
>
> This patch-set deals with the inline assembly problem. I separated these
> patches from the others (that were sent in the RFC) for easier
> inclusion. I also separated the removal of unnecessary new-lines which
> would be sent separately.
>
> The problem with inline assembly is that inline assembly is often used
> by the kernel for things that are other than code - for example,
> assembly directives and data. GCC however is oblivious to the content of
> the blocks and assumes their cost in space and time is proportional to
> the number of the perceived assembly "instruction", according to the
> number of newlines and semicolons. Alternatives, paravirt and other
> mechanisms are affected, causing code not to be inlined, and degrading
> compilation quality in general.
>
> The solution that this patch-set carries for this problem is to create
> an assembly macro, and then call it from the inline assembly block. As
> a result, the compiler sees a single "instruction" and assigns the more
> appropriate cost to the code.
>
> To avoid uglification of the code, as many noted, the macros are first
> precompiled into an assembly file, which is later assembled together
> with the C files. This also enables to avoid duplicate implementation
> that was set before for the asm and C code. This can be seen in the
> exception table changes.
>
> Overall this patch-set slightly increases the kernel size (my build was
> done using my Ubuntu 18.04 config + localyesconfig for the record):
>
> text data bss dec hex filename
> 18140829 10224724 2957312 31322865 1ddf2f1 ./vmlinux before
> 18163608 10227348 2957312 31348268 1de562c ./vmlinux after (+0.1%)
>
> The number of static functions in the image is reduced by 379, but
> actually inlining is even better, which does not always shows in these
> numbers: a function may be inlined causing the calling function not to
> be inlined.
>
> I ran some limited number of benchmarks, and in general the performance
> impact is not very notable. You can still see >10 cycles shaved off some
> syscalls that manipulate page-tables (e.g., mprotect()), in which
> paravirt caused many functions not to be inlined. In addition this
> patch-set can prevent issues such as [1], and improves code readability
> and maintainability.
>
> [1] https://patchwork.kernel.org/patch/10450037/
>
> v5->v6: * Removing more code from jump-labels (PeterZ)
> * Fix build issue on i386 (0-day, PeterZ)
>
> v4->v5: * Makefile fixes (Masahiro, Sam)
>
> v3->v4: * Changed naming of macros in 2 patches (PeterZ)
> * Minor cleanup of the paravirt patch
>
> v2->v3: * Several build issues resolved (0-day)
> * Wrong comments fix (Josh)
> * Change asm vs C order in refcount (Kees)
>
> v1->v2: * Compiling the macros into a separate .s file, improving
> readability (Linus)
> * Improving assembly formatting, applying most of the comments
> according to my judgment (Jan)
> * Adding exception-table, cpufeature and jump-labels
> * Removing new-line cleanup; to be submitted separately
>
> Cc: Masahiro Yamada <yamada.masahiro@socionext.com>
> Cc: Sam Ravnborg <sam@ravnborg.org>
> Cc: Alok Kataria <akataria@vmware.com>
> Cc: Christopher Li <sparse@chrisli.org>
> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Cc: "H. Peter Anvin" <hpa@zytor.com>
> Cc: Ingo Molnar <mingo@redhat.com>
> Cc: Jan Beulich <JBeulich@suse.com>
> Cc: Josh Poimboeuf <jpoimboe@redhat.com>
> Cc: Juergen Gross <jgross@suse.com>
> Cc: Kate Stewart <kstewart@linuxfoundation.org>
> Cc: Kees Cook <keescook@chromium.org>
> Cc: linux-sparse@vger.kernel.org
> Cc: Peter Zijlstra <peterz@infradead.org>
> Cc: Philippe Ombredanne <pombredanne@nexb.com>
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Cc: virtualization@lists.linux-foundation.org
> Cc: Linus Torvalds <torvalds@linux-foundation.org>
> Cc: x86@kernel.org
>
>
> Nadav Amit (9):
> Makefile: Prepare for using macros for inline asm
> x86: objtool: use asm macro for better compiler decisions
> x86: refcount: prevent gcc distortions
> x86: alternatives: macrofy locks for better inlining
> x86: bug: prevent gcc distortions
> x86: prevent inline distortion by paravirt ops
> x86: extable: use macros instead of inline assembly
> x86: cpufeature: use macros instead of inline assembly
> x86: jump-labels: use macros instead of inline assembly
>
> Makefile | 9 ++-
> arch/x86/Makefile | 11 ++-
> arch/x86/entry/calling.h | 2 +-
> arch/x86/include/asm/alternative-asm.h | 20 ++++--
> arch/x86/include/asm/alternative.h | 11 +--
> arch/x86/include/asm/asm.h | 61 +++++++---------
> arch/x86/include/asm/bug.h | 98 +++++++++++++++-----------
> arch/x86/include/asm/cpufeature.h | 82 ++++++++++++---------
> arch/x86/include/asm/jump_label.h | 77 ++++++++------------
> arch/x86/include/asm/paravirt_types.h | 56 +++++++--------
> arch/x86/include/asm/refcount.h | 74 +++++++++++--------
> arch/x86/kernel/macros.S | 16 +++++
> include/asm-generic/bug.h | 8 +--
> include/linux/compiler.h | 56 +++++++++++----
> scripts/Kbuild.include | 4 +-
> scripts/mod/Makefile | 2 +
> 16 files changed, 331 insertions(+), 256 deletions(-)
> create mode 100644 arch/x86/kernel/macros.S
>
> --
> 2.17.1
Ping?
^ permalink raw reply [flat|nested] 5+ messages in thread
* [kbuild ack?] Re: [PATCH v6 0/9] x86: macrofying inline asm for better compilation
2018-07-11 1:59 ` [PATCH v6 0/9] x86: macrofying inline asm for better compilation Nadav Amit
@ 2018-07-15 21:54 ` Ingo Molnar
2018-07-18 1:47 ` Masahiro Yamada
0 siblings, 1 reply; 5+ messages in thread
From: Ingo Molnar @ 2018-07-15 21:54 UTC (permalink / raw)
To: Nadav Amit
Cc: Juergen Gross, Kate Stewart, Kees Cook, Josh Poimboeuf,
Peter Zijlstra, Greg Kroah-Hartman, Christopher Li, X86 ML,
Linux Kernel Mailing List, Philippe Ombredanne,
virtualization@lists.linux-foundation.org, Masahiro Yamada,
linux-sparse@vger.kernel.org, Ingo Molnar, Jan Beulich,
H. Peter Anvin, Linus Torvalds, Thomas Gleixner, Sam Ravnborg,
Alok Kataria
* Nadav Amit <namit@vmware.com> wrote:
> > I ran some limited number of benchmarks, and in general the performance
> > impact is not very notable. You can still see >10 cycles shaved off some
> > syscalls that manipulate page-tables (e.g., mprotect()), in which
> > paravirt caused many functions not to be inlined. In addition this
> > patch-set can prevent issues such as [1], and improves code readability
> > and maintainability.
Ok, that's good enough as a benefit, I suppose.
> > Nadav Amit (9):
> > Makefile: Prepare for using macros for inline asm
This non-trivial kbuild patch needs an Acked-by from Masahiro.
Thanks,
Ingo
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [kbuild ack?] Re: [PATCH v6 0/9] x86: macrofying inline asm for better compilation
2018-07-15 21:54 ` [kbuild ack?] " Ingo Molnar
@ 2018-07-18 1:47 ` Masahiro Yamada
0 siblings, 0 replies; 5+ messages in thread
From: Masahiro Yamada @ 2018-07-18 1:47 UTC (permalink / raw)
To: Ingo Molnar
Cc: Nadav Amit, Linux Kernel Mailing List, X86 ML, Ingo Molnar,
Thomas Gleixner, Sam Ravnborg, Alok Kataria, Christopher Li,
Greg Kroah-Hartman, H. Peter Anvin, Jan Beulich, Josh Poimboeuf,
Juergen Gross, Kate Stewart, Kees Cook,
linux-sparse@vger.kernel.org, Peter Zijlstra, Philippe Ombredanne,
"virtualization@lists.linux-foundation.org" <virtualizat>
2018-07-16 6:54 GMT+09:00 Ingo Molnar <mingo@kernel.org>:
>
> * Nadav Amit <namit@vmware.com> wrote:
>
>> > I ran some limited number of benchmarks, and in general the performance
>> > impact is not very notable. You can still see >10 cycles shaved off some
>> > syscalls that manipulate page-tables (e.g., mprotect()), in which
>> > paravirt caused many functions not to be inlined. In addition this
>> > patch-set can prevent issues such as [1], and improves code readability
>> > and maintainability.
>
> Ok, that's good enough as a benefit, I suppose.
>
>> > Nadav Amit (9):
>> > Makefile: Prepare for using macros for inline asm
>
> This non-trivial kbuild patch needs an Acked-by from Masahiro.
Sorry for delay.
I've issued Acked-by to 1/9 now.
Thanks.
> Thanks,
>
> Ingo
--
Best Regards
Masahiro Yamada
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2018-07-18 1:47 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-06-22 17:22 [PATCH v6 0/9] x86: macrofying inline asm for better compilation Nadav Amit
2018-06-22 17:22 ` [PATCH v6 2/9] x86: objtool: use asm macro for better compiler decisions Nadav Amit
2018-07-11 1:59 ` [PATCH v6 0/9] x86: macrofying inline asm for better compilation Nadav Amit
2018-07-15 21:54 ` [kbuild ack?] " Ingo Molnar
2018-07-18 1:47 ` Masahiro Yamada
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).