2018-01-18 15:29:13

by Thomas Gleixner

[permalink] [raw]
Subject: [PATCH] x86/mce: Make machine check speculation protected

The machine check idtentry uses an indirect branch directly from the low
level code. This evades the speculation protection.

Replace it by a direct call into C code and issue the indirect call there
so the compiler can apply the proper speculation protection.

Signed-off-by: Thomas Gleixner <[email protected]>
---
arch/x86/entry/entry_64.S | 2 +-
arch/x86/include/asm/traps.h | 1 +
arch/x86/kernel/cpu/mcheck/mce.c | 5 +++++
3 files changed, 7 insertions(+), 1 deletion(-)

--- a/arch/x86/entry/entry_64.S
+++ b/arch/x86/entry/entry_64.S
@@ -1264,7 +1264,7 @@ idtentry async_page_fault do_async_page_
#endif

#ifdef CONFIG_X86_MCE
-idtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip)
+idtentry machine_check do_mce has_error_code=0 paranoid=1
#endif

/*
--- a/arch/x86/include/asm/traps.h
+++ b/arch/x86/include/asm/traps.h
@@ -88,6 +88,7 @@ dotraplinkage void do_simd_coprocessor_e
#ifdef CONFIG_X86_32
dotraplinkage void do_iret_error(struct pt_regs *, long);
#endif
+dotraplinkage void do_mce(struct pt_regs *, long);

static inline int get_si_code(unsigned long condition)
{
--- a/arch/x86/kernel/cpu/mcheck/mce.c
+++ b/arch/x86/kernel/cpu/mcheck/mce.c
@@ -1785,6 +1785,11 @@ static void unexpected_machine_check(str
void (*machine_check_vector)(struct pt_regs *, long error_code) =
unexpected_machine_check;

+dotraplinkage void do_mce(struct pt_regs *regs, long error_code)
+{
+ machine_check_vector(regs, error_code);
+}
+
/*
* Called for each booted CPU to set up machine checks.
* Must be called with preempt off:


2018-01-18 16:28:59

by David Woodhouse

[permalink] [raw]
Subject: Re: [PATCH] x86/mce: Make machine check speculation protected

On Thu, 2018-01-18 at 16:28 +0100, Thomas Gleixner wrote:
> The machine check idtentry uses an indirect branch directly from the low
> level code. This evades the speculation protection.
>
> Replace it by a direct call into C code and issue the indirect call there
> so the compiler can apply the proper speculation protection.
>
> Signed-off-by: Thomas Gleixner <[email protected]>

Reviewed-by: David Woodhouse <[email protected]>


Attachments:
smime.p7s (5.09 kB)

2018-01-18 16:29:52

by Borislav Petkov

[permalink] [raw]
Subject: Re: [PATCH] x86/mce: Make machine check speculation protected

On Thu, Jan 18, 2018 at 04:28:26PM +0100, Thomas Gleixner wrote:
> The machine check idtentry uses an indirect branch directly from the low
> level code. This evades the speculation protection.
>
> Replace it by a direct call into C code and issue the indirect call there
> so the compiler can apply the proper speculation protection.
>
> Signed-off-by: Thomas Gleixner <[email protected]>
> ---
> arch/x86/entry/entry_64.S | 2 +-
> arch/x86/include/asm/traps.h | 1 +
> arch/x86/kernel/cpu/mcheck/mce.c | 5 +++++
> 3 files changed, 7 insertions(+), 1 deletion(-)
>
> --- a/arch/x86/entry/entry_64.S
> +++ b/arch/x86/entry/entry_64.S
> @@ -1264,7 +1264,7 @@ idtentry async_page_fault do_async_page_
> #endif
>
> #ifdef CONFIG_X86_MCE
> -idtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip)
> +idtentry machine_check do_mce has_error_code=0 paranoid=1
> #endif
>
> /*
> --- a/arch/x86/include/asm/traps.h
> +++ b/arch/x86/include/asm/traps.h
> @@ -88,6 +88,7 @@ dotraplinkage void do_simd_coprocessor_e
> #ifdef CONFIG_X86_32
> dotraplinkage void do_iret_error(struct pt_regs *, long);
> #endif
> +dotraplinkage void do_mce(struct pt_regs *, long);
>
> static inline int get_si_code(unsigned long condition)
> {
> --- a/arch/x86/kernel/cpu/mcheck/mce.c
> +++ b/arch/x86/kernel/cpu/mcheck/mce.c
> @@ -1785,6 +1785,11 @@ static void unexpected_machine_check(str
> void (*machine_check_vector)(struct pt_regs *, long error_code) =
> unexpected_machine_check;
>
> +dotraplinkage void do_mce(struct pt_regs *regs, long error_code)
> +{
> + machine_check_vector(regs, error_code);
> +}
> +
> /*
> * Called for each booted CPU to set up machine checks.
> * Must be called with preempt off:

Reviewed-by: Borislav Petkov <[email protected]>

--
Regards/Gruss,
Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.

2018-01-18 16:30:39

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH] x86/mce: Make machine check speculation protected

On Thu, Jan 18, 2018 at 04:28:26PM +0100, Thomas Gleixner wrote:
> The machine check idtentry uses an indirect branch directly from the low
> level code. This evades the speculation protection.
>
> Replace it by a direct call into C code and issue the indirect call there
> so the compiler can apply the proper speculation protection.
>
> Signed-off-by: Thomas Gleixner <[email protected]>
> ---
> arch/x86/entry/entry_64.S | 2 +-
> arch/x86/include/asm/traps.h | 1 +
> arch/x86/kernel/cpu/mcheck/mce.c | 5 +++++
> 3 files changed, 7 insertions(+), 1 deletion(-)
>
> --- a/arch/x86/entry/entry_64.S
> +++ b/arch/x86/entry/entry_64.S
> @@ -1264,7 +1264,7 @@ idtentry async_page_fault do_async_page_
> #endif
>
> #ifdef CONFIG_X86_MCE
> -idtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip)
> +idtentry machine_check do_mce has_error_code=0 paranoid=1

Nice, Thanks!

2018-01-19 08:50:01

by Woodhouse, David

[permalink] [raw]
Subject: Re: [PATCH] x86/mce: Make machine check speculation protected

On Thu, 2018-01-18 at 16:28 +0100, Thomas Gleixner wrote:
> The machine check idtentry uses an indirect branch directly from the low
> level code. This evades the speculation protection.
>
> Replace it by a direct call into C code and issue the indirect call there
> so the compiler can apply the proper speculation protection.
>
> Signed-off-by: Thomas Gleixner <[email protected]>

Cc: stable for at least 4.9.

> ---
>  arch/x86/entry/entry_64.S        |    2 +-
>  arch/x86/include/asm/traps.h     |    1 +
>  arch/x86/kernel/cpu/mcheck/mce.c |    5 +++++
>  3 files changed, 7 insertions(+), 1 deletion(-)
>
> --- a/arch/x86/entry/entry_64.S
> +++ b/arch/x86/entry/entry_64.S
> @@ -1264,7 +1264,7 @@ idtentry async_page_fault do_async_page_
>  #endif
>  
>  #ifdef CONFIG_X86_MCE
> -idtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip)
> +idtentry machine_check do_mce has_error_code=0 paranoid=1
>  #endif
>  
>  /*
> --- a/arch/x86/include/asm/traps.h
> +++ b/arch/x86/include/asm/traps.h
> @@ -88,6 +88,7 @@ dotraplinkage void do_simd_coprocessor_e
>  #ifdef CONFIG_X86_32
>  dotraplinkage void do_iret_error(struct pt_regs *, long);
>  #endif
> +dotraplinkage void do_mce(struct pt_regs *, long);
>  
>  static inline int get_si_code(unsigned long condition)
>  {
> --- a/arch/x86/kernel/cpu/mcheck/mce.c
> +++ b/arch/x86/kernel/cpu/mcheck/mce.c
> @@ -1785,6 +1785,11 @@ static void unexpected_machine_check(str
>  void (*machine_check_vector)(struct pt_regs *, long error_code) =
>   unexpected_machine_check;
>  
> +dotraplinkage void do_mce(struct pt_regs *regs, long error_code)
> +{
> + machine_check_vector(regs, error_code);
> +}
> +
>  /*
>   * Called for each booted CPU to set up machine checks.
>   * Must be called with preempt off:
>


Attachments:
smime.p7s (5.09 kB)
Subject: [tip:x86/pti] x86/mce: Make machine check speculation protected

Commit-ID: 6f41c34d69eb005e7848716bbcafc979b35037d5
Gitweb: https://git.kernel.org/tip/6f41c34d69eb005e7848716bbcafc979b35037d5
Author: Thomas Gleixner <[email protected]>
AuthorDate: Thu, 18 Jan 2018 16:28:26 +0100
Committer: Thomas Gleixner <[email protected]>
CommitDate: Fri, 19 Jan 2018 16:31:28 +0100

x86/mce: Make machine check speculation protected

The machine check idtentry uses an indirect branch directly from the low
level code. This evades the speculation protection.

Replace it by a direct call into C code and issue the indirect call there
so the compiler can apply the proper speculation protection.

Signed-off-by: Thomas Gleixner <[email protected]>
Reviewed-by:Borislav Petkov <[email protected]>
Reviewed-by: David Woodhouse <[email protected]>
Niced-by: Peter Zijlstra <[email protected]>
Cc: [email protected]
Link: https://lkml.kernel.org/r/alpine.DEB.2.20.1801181626290.1847@nanos

---
arch/x86/entry/entry_64.S | 2 +-
arch/x86/include/asm/traps.h | 1 +
arch/x86/kernel/cpu/mcheck/mce.c | 5 +++++
3 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
index d54a0ed..63f4320 100644
--- a/arch/x86/entry/entry_64.S
+++ b/arch/x86/entry/entry_64.S
@@ -1258,7 +1258,7 @@ idtentry async_page_fault do_async_page_fault has_error_code=1
#endif

#ifdef CONFIG_X86_MCE
-idtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip)
+idtentry machine_check do_mce has_error_code=0 paranoid=1
#endif

/*
diff --git a/arch/x86/include/asm/traps.h b/arch/x86/include/asm/traps.h
index 31051f3..3de6933 100644
--- a/arch/x86/include/asm/traps.h
+++ b/arch/x86/include/asm/traps.h
@@ -88,6 +88,7 @@ dotraplinkage void do_simd_coprocessor_error(struct pt_regs *, long);
#ifdef CONFIG_X86_32
dotraplinkage void do_iret_error(struct pt_regs *, long);
#endif
+dotraplinkage void do_mce(struct pt_regs *, long);

static inline int get_si_code(unsigned long condition)
{
diff --git a/arch/x86/kernel/cpu/mcheck/mce.c b/arch/x86/kernel/cpu/mcheck/mce.c
index 3b413065..a9e898b 100644
--- a/arch/x86/kernel/cpu/mcheck/mce.c
+++ b/arch/x86/kernel/cpu/mcheck/mce.c
@@ -1788,6 +1788,11 @@ static void unexpected_machine_check(struct pt_regs *regs, long error_code)
void (*machine_check_vector)(struct pt_regs *, long error_code) =
unexpected_machine_check;

+dotraplinkage void do_mce(struct pt_regs *regs, long error_code)
+{
+ machine_check_vector(regs, error_code);
+}
+
/*
* Called for each booted CPU to set up machine checks.
* Must be called with preempt off: