Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1314400 > unrolled thread

[PATCH 5/5] x86/alternatives: Discard dynamic check after init

Started byBorislav Petkov <bp@alien8.de>
First post2016-01-21 19:40 +0100
Last post2016-01-21 20:10 +0100
Articles 2 — 2 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  [PATCH 5/5] x86/alternatives: Discard dynamic check after init Borislav Petkov <bp@alien8.de> - 2016-01-21 19:40 +0100
    Re: [PATCH 5/5] x86/alternatives: Discard dynamic check after init "H. Peter Anvin" <hpa@zytor.com> - 2016-01-21 20:10 +0100

#1314400 — [PATCH 5/5] x86/alternatives: Discard dynamic check after init

FromBorislav Petkov <bp@alien8.de>
Date2016-01-21 19:40 +0100
Subject[PATCH 5/5] x86/alternatives: Discard dynamic check after init
Message-ID<qTsjf-I1-5@gated-at.bofh.it>
From: Brian Gerst <brgerst@gmail.com>

Move the code to do the dynamic check to the altinstr_aux section so
that it is discarded after alternatives have run and a static branch has
been chosen.

Boris: change it to do TESTB, as hpa suggests.

Signed-off-by: Brian Gerst <brgerst@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Cc: Dave Young <dyoung@redhat.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Kristen Carlson Accardi <kristen@linux.intel.com>
Cc: Laura Abbott <labbott@fedoraproject.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: "Peter Zijlstra (Intel)" <peterz@infradead.org>
Cc: Prarit Bhargava <prarit@redhat.com>
Cc: Ross Zwisler <ross.zwisler@linux.intel.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: x86-ml <x86@kernel.org>
Link: http://lkml.kernel.org/r/1452972124-7380-1-git-send-email-brgerst@gmail.com
Signed-off-by: Borislav Petkov <bp@suse.de>
---
 arch/x86/include/asm/cpufeature.h | 17 +++++++++++------
 arch/x86/kernel/cpu/common.c      |  6 ------
 2 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/arch/x86/include/asm/cpufeature.h b/arch/x86/include/asm/cpufeature.h
index e4759223f82c..77c51f4c15b7 100644
--- a/arch/x86/include/asm/cpufeature.h
+++ b/arch/x86/include/asm/cpufeature.h
@@ -131,7 +131,6 @@ extern const char * const x86_bug_flags[NBUGINTS*32];
 
 #if __GNUC__ >= 4 && defined(CONFIG_X86_FAST_FEATURE_TESTS)
 extern void warn_pre_alternatives(void);
-extern bool __static_cpu_has(u16 bit);
 
 /*
  * Static testing of CPU features.  Used the same as boot_cpu_has().
@@ -141,7 +140,7 @@ extern bool __static_cpu_has(u16 bit);
 static __always_inline __pure bool _static_cpu_has(u16 bit)
 {
 #ifdef CC_HAVE_ASM_GOTO
-		asm_volatile_goto("1: jmp %l[t_dynamic]\n"
+		asm_volatile_goto("1: jmp 6f\n"
 			 "2:\n"
 			 ".skip -(((5f-4f) - (2b-1b)) > 0) * "
 			         "((5f-4f) - (2b-1b)),0x90\n"
@@ -166,13 +165,19 @@ static __always_inline __pure bool _static_cpu_has(u16 bit)
 			 " .byte 0\n"			/* repl len */
 			 " .byte 0\n"			/* pad len */
 			 ".previous\n"
-			 : : "i" (bit), "i" (X86_FEATURE_ALWAYS)
-			 : : t_dynamic, t_no);
+			 ".section .altinstr_aux,\"ax\"\n"
+			 "6: testb %[bitnum],%[cap_word]\n"
+			 "   jnz %l[t_yes]\n"
+			 "   jmp %l[t_no]\n"
+			 ".previous\n"
+			 : : "i" (bit), "i" (X86_FEATURE_ALWAYS),
+			     [bitnum] "i" (1 << (bit & 7)),
+			     [cap_word] "m" (((const char *)boot_cpu_data.x86_capability)[bit >> 3])
+			 : : t_yes, t_no);
+	t_yes:
 		return true;
 	t_no:
 		return false;
-	t_dynamic:
-		return __static_cpu_has(bit);
 #else
 		return boot_cpu_has(bit);
 #endif /* CC_HAVE_ASM_GOTO */
diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index a57ec0df9cc3..897c65bd3faa 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -1483,12 +1483,6 @@ void warn_pre_alternatives(void)
 EXPORT_SYMBOL_GPL(warn_pre_alternatives);
 #endif
 
-inline bool __static_cpu_has(u16 bit)
-{
-	return boot_cpu_has(bit);
-}
-EXPORT_SYMBOL_GPL(__static_cpu_has);
-
 static void bsp_resume(void)
 {
 	if (this_cpu->c_bsp_resume)
-- 
2.3.5

[toc] | [next] | [standalone]


#1314430

From"H. Peter Anvin" <hpa@zytor.com>
Date2016-01-21 20:10 +0100
Message-ID<qTsMj-19q-33@gated-at.bofh.it>
In reply to#1314400
On 01/21/16 10:34, Borislav Petkov wrote:
> From: Brian Gerst <brgerst@gmail.com>
> 
> Move the code to do the dynamic check to the altinstr_aux section so
> that it is discarded after alternatives have run and a static branch has
> been chosen.
> 
> Boris: change it to do TESTB, as hpa suggests.

[Also document that this changes it from C code to assembly, which makes
it *substantially* smaller while avoiding a completely unnecessary call
to an out of line function.]

>   * Static testing of CPU features.  Used the same as boot_cpu_has().
> @@ -141,7 +140,7 @@ extern bool __static_cpu_has(u16 bit);
>  static __always_inline __pure bool _static_cpu_has(u16 bit)
>  {
>  #ifdef CC_HAVE_ASM_GOTO
> -		asm_volatile_goto("1: jmp %l[t_dynamic]\n"
> +		asm_volatile_goto("1: jmp 6f\n"
>  			 "2:\n"
>  			 ".skip -(((5f-4f) - (2b-1b)) > 0) * "
>  			         "((5f-4f) - (2b-1b)),0x90\n"
> @@ -166,13 +165,19 @@ static __always_inline __pure bool _static_cpu_has(u16 bit)
>  			 " .byte 0\n"			/* repl len */
>  			 " .byte 0\n"			/* pad len */
>  			 ".previous\n"
> -			 : : "i" (bit), "i" (X86_FEATURE_ALWAYS)
> -			 : : t_dynamic, t_no);
> +			 ".section .altinstr_aux,\"ax\"\n"
> +			 "6: testb %[bitnum],%[cap_word]\n"
> +			 "   jnz %l[t_yes]\n"
> +			 "   jmp %l[t_no]\n"
> +			 ".previous\n"
> +			 : : "i" (bit), "i" (X86_FEATURE_ALWAYS),
> +			     [bitnum] "i" (1 << (bit & 7)),
> +			     [cap_word] "m" (((const char *)boot_cpu_data.x86_capability)[bit >> 3])

Change to [cap_byte] as we normally refer to this array by 32-bit words.

> +			 : : t_yes, t_no);
> +	t_yes:
>  		return true;
>  	t_no:
>  		return false;
> -	t_dynamic:
> -		return __static_cpu_has(bit);
>  #else
>  		return boot_cpu_has(bit);
>  #endif /* CC_HAVE_ASM_GOTO */
> diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
> index a57ec0df9cc3..897c65bd3faa 100644
> --- a/arch/x86/kernel/cpu/common.c
> +++ b/arch/x86/kernel/cpu/common.c
> @@ -1483,12 +1483,6 @@ void warn_pre_alternatives(void)
>  EXPORT_SYMBOL_GPL(warn_pre_alternatives);
>  #endif
>  
> -inline bool __static_cpu_has(u16 bit)
> -{
> -	return boot_cpu_has(bit);
> -}
> -EXPORT_SYMBOL_GPL(__static_cpu_has);
> -
>  static void bsp_resume(void)
>  {
>  	if (this_cpu->c_bsp_resume)
> 

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web