All of lore.kernel.org
 help / color / mirror / Atom feed
From: Nadav Amit <namit@vmware.com>
To: <linux-kernel@vger.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>,
	<x86@kernel.org>
Subject: [RFC 8/8] bitops: prevent compiler inline decision distortion
Date: Tue, 15 May 2018 07:11:15 -0700	[thread overview]
Message-ID: <20180515141124.84254-9-namit@vmware.com> (raw)
In-Reply-To: <20180515141124.84254-1-namit@vmware.com>

There are several places in the kernel in which there is a condition
that is based on whether the input is known to be constant in
compilation time. If it is, there are complex computations, which only
take place during compilation time.

Although this scheme works correctly, when GCC computes the expected
cost of this code in time and size, it disregards the fact that the
computations of the "constant" case will not take place during runtime
for the non-constant case. The cost of these functions is considered to
be much higher.  As a result, inline and branching decisions of the
compiler are distorted. Specifically, functions are less likely to be
inlined due to their preserved big size and execution time.

One of this cases is test_bit() which performs some computations for
constant inputs.

The solution is to use __builtin_choose_expr() to detect whether the
input is constant instead of a C condition. GCC evaluates the builtin
earlier, which allows it to improve code-generation decisions.

This patch allows function such as bitmap_pos_to_ord() to be inlined.
Its overall effect on size:

   text	   data	    bss	    dec	    hex	filename
18149165 10064176 2936832 31150173 1db505d ./vmlinux before
18149210 10064048 2936832 31150090 1db500a ./vmlinux after (-83)

Static text symbols:
Before:	39643
After:	39632	(-11)

Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: x86@kernel.org

Signed-off-by: Nadav Amit <namit@vmware.com>
---
 arch/x86/include/asm/bitops.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/arch/x86/include/asm/bitops.h b/arch/x86/include/asm/bitops.h
index 9f645ba57dbb..f1cb1c9125a9 100644
--- a/arch/x86/include/asm/bitops.h
+++ b/arch/x86/include/asm/bitops.h
@@ -349,10 +349,10 @@ static __always_inline bool variable_test_bit(long nr, volatile const unsigned l
 static bool test_bit(int nr, const volatile unsigned long *addr);
 #endif
 
-#define test_bit(nr, addr)			\
-	(__builtin_constant_p((nr))		\
-	 ? constant_test_bit((nr), (addr))	\
-	 : variable_test_bit((nr), (addr)))
+#define test_bit(nr, addr)					\
+	__builtin_choose_expr(__builtin_constant_p((nr)),	\
+		constant_test_bit((nr), (addr)),		\
+		variable_test_bit((nr), (addr)))
 
 /**
  * __ffs - find first set bit in word
-- 
2.17.0

  parent reply	other threads:[~2018-05-15 21:29 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-15 14:11 [RFC 0/8] Improving compiler inlining decisions Nadav Amit
2018-05-15 14:11 ` Nadav Amit
2018-05-15 14:11 ` [RFC 1/8] x86: objtool: use asm macro for better compiler decisions Nadav Amit
2018-05-15 14:11   ` Nadav Amit
2018-05-15 21:37   ` Josh Triplett
2018-05-15 21:53     ` Nadav Amit
2018-05-15 21:55       ` Josh Triplett
2018-05-15 14:11 ` [RFC 2/8] x86: bug: prevent gcc distortions Nadav Amit
2018-05-15 14:11 ` [RFC 3/8] x86: alternative: macrofy locks for better inlining Nadav Amit
2018-05-15 14:11 ` [RFC 4/8] x86: prevent inline distortion by paravirt ops Nadav Amit
2018-05-15 14:11 ` [RFC 5/8] x86: refcount: prevent gcc distortions Nadav Amit
2018-05-16 13:59   ` Kees Cook
2018-05-16 16:37     ` Nadav Amit
2018-05-15 14:11 ` [RFC 6/8] x86: removing unneeded new-lines Nadav Amit
2018-05-15 14:11 ` [RFC 7/8] ilog2: preventing compiler distortion due to big condition Nadav Amit
2018-05-15 14:11 ` Nadav Amit [this message]
2018-05-15 14:11 ` [RFC 0/8] Improving compiler inlining decisions Nadav Amit
2018-05-15 14:11   ` Nadav Amit
2018-05-15 14:11 ` [RFC 1/8] x86: objtool: use asm macro for better compiler decisions Nadav Amit
2018-05-15 14:11   ` Nadav Amit
2018-05-15 14:11 ` [RFC 2/8] x86: bug: prevent gcc distortions Nadav Amit
2018-05-15 14:11 ` [RFC 3/8] x86: alternative: macrofy locks for better inlining Nadav Amit
2018-05-15 14:11 ` [RFC 4/8] x86: prevent inline distortion by paravirt ops Nadav Amit
2018-05-15 14:11 ` [RFC 5/8] x86: refcount: prevent gcc distortions Nadav Amit
2018-05-16  7:14   ` Jan Beulich
2018-05-16 16:44     ` Nadav Amit
2018-05-17  7:18       ` Jan Beulich
2018-05-15 14:11 ` [RFC 6/8] x86: removing unneeded new-lines Nadav Amit
2018-05-15 14:11 ` [RFC 7/8] ilog2: preventing compiler distortion due to big condition Nadav Amit
2018-05-15 14:11 ` [RFC 8/8] bitops: prevent compiler inline decision distortion Nadav Amit
2018-05-16 14:09   ` Kees Cook
2018-05-15 22:14 ` [RFC 0/8] Improving compiler inlining decisions Nadav Amit
2018-05-16  3:48 ` Josh Poimboeuf
2018-05-16  3:48   ` Josh Poimboeuf
2018-05-16  4:30   ` 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=20180515141124.84254-9-namit@vmware.com \
    --to=namit@vmware.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=nadav.amit@gmail.com \
    --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 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.