x86/mce: protect nr_cpus from rebooting by broadcast mce
Subsystems (3)
v2 cover x86/mce: protect nr_cpus from rebooting by broadcast mce
v1 -> v2: fix compile warning and error on x86_32
This series include two related groups:
[1-3/4]: protect nr_cpus from rebooting by broadcast mce [4/4]: improve "kexec -l" robustness against broadcast mce
When I tried to fix [1], Thomas raised concern about the nr_cpus' vulnerability to unexpected rebooting by broadcast mce. After analysis, I think only the following first case suffers from the rebooting by broadcast mce. [1-3/4] aims to fix that issue.
*** Back ground ***
On x86 it's required to have all logical CPUs set CR4.MCE=1. Otherwise, a broadcast MCE observing CR4.MCE=0b on any core will shutdown the machine.
The option 'nosmt' has already complied with the above rule by Thomas's patch. For detail, refer to 506a66f3748 (Revert "x86/apic: Ignore secondary threads if nosmt=force")
But for nr_cpus option, the exposure to broadcast MCE is a little complicated, and can be categorized into three cases.
-1. boot up by BIOS. Since no one set CR4.MCE=1, nr_cpus risks rebooting by broadcast MCE.
-2. boot up by "kexec -p nr_cpus=". Since the 1st kernel has all cpus' CR4.MCE=1 set before kexec -p, nr_cpus is free of rebooting by broadcast MCE. Furthermore, the crashed kernel's wreckage, including page table and text, is not touched by capture kernel. Hence if MCE event happens on capped cpu, do_machine_check->__mc_check_crashing_cpu() runs smoothly and returns immediately, the capped cpu is still pinned on "halt".
-3. boot up by "kexec -l nr_cpus=". As "kexec -p", it is free of rebooting by broadcast MCE. But the 1st kernel's wreckage is discarded and changed. when capped cpus execute do_machine_check(), they may crack the new kernel. But this is not related with broadcast MCE, and need an extra fix.
*** Solution ***
"nr_cpus" can not follow the same way as "nosmt". Because nr_cpus limits the allocation of percpu area and some other kthread memory, which is critical to cpu hotplug framework. Instead, developing a dedicated SIPI callback make_capped_cpu_stable() for capped cpu, which does not lean on percpu area to work.
[1]: https://lkml.org/lkml/2019/7/5/3
To: Gleixner <[email protected]>
To: Andy Lutomirski <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: "H. Peter Anvin" <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: Peter Zijlstra <[email protected]>
To: [email protected]
Cc: Masami Hiramatsu <[email protected]>
Cc: Qian Cai <[email protected]>
Cc: Vlastimil Babka <[email protected]>
Cc: Daniel Drake <[email protected]>
Cc: Jacob Pan <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: Eric Biederman <[email protected]>
Cc: [email protected]
Cc: Dave Young <[email protected]>
Cc: Baoquan He <[email protected]>
Cc: [email protected]
---
Pingfan Liu (4):
x86/apic: correct the ENO in generic_processor_info() x86/apic: record capped cpu in generic_processor_info() x86/smp: send capped cpus to a stable state when smp_init() x86/smp: disallow MCE handler on rebooting AP
arch/x86/include/asm/apic.h | 1 + arch/x86/include/asm/smp.h | 3 ++ arch/x86/kernel/apic/apic.c | 23 ++++++++---- arch/x86/kernel/cpu/common.c | 7 ++++ arch/x86/kernel/smp.c | 8 +++++ arch/x86/kernel/smpboot.c | 83 ++++++++++++++++++++++++++++++++++++++++++++ kernel/smp.c | 6 ++++ 7 files changed, 124 insertions(+), 7 deletions(-)
Signature · 1 line
2.7.5
PATCH v2 1/4 x86/apic: correct the ENO in generic_processor_info()
When capped by nr_cpu_ids, the current code return -EINVAL or -ENODEV. It is better to return -EINVAL for both cases.
To: Thomas Gleixner <[email protected]>
To: Andy Lutomirski <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: "H. Peter Anvin" <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: Peter Zijlstra <[email protected]>
To: [email protected]
Cc: Masami Hiramatsu <[email protected]>
Cc: Qian Cai <[email protected]>
Cc: Vlastimil Babka <[email protected]>
Cc: Daniel Drake <[email protected]>
Cc: Jacob Pan <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: Eric Biederman <[email protected]>
Cc: [email protected]
Cc: Dave Young <[email protected]>
Cc: Baoquan He <[email protected]>
Cc: [email protected]
--- arch/x86/kernel/apic/apic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/apic/apic.c +1 -1
@@ -2346,7 +2346,7 @@ int generic_processor_info(int apicid, int version) " Processor %d/0x%x ignored.\n", max, thiscpu, apicid); disabled_cpus++; -return -ENODEV ; +return -EINVAL ; }if (num_processors >= nr_cpu_ids) {
Signature · 7 lines
2.7.5 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
PATCH v2 2/4 x86/apic: record capped cpu in generic_processor_info()
In reply to Pingfan LiuNo matter the cpu is capped by nr_cpus option, recording the mapping between all cpus' id and apic id
Later this mapping will be used by BSP to sent SIPI to bring capped cpu to stable state
To: Thomas Gleixner <[email protected]>
To: Andy Lutomirski <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: "H. Peter Anvin" <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: Peter Zijlstra <[email protected]>
To: [email protected]
Cc: Masami Hiramatsu <[email protected]>
Cc: Qian Cai <[email protected]>
Cc: Vlastimil Babka <[email protected]>
Cc: Daniel Drake <[email protected]>
Cc: Jacob Pan <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: Eric Biederman <[email protected]>
Cc: [email protected]
Cc: Dave Young <[email protected]>
Cc: Baoquan He <[email protected]>
Cc: [email protected]
--- arch/x86/include/asm/smp.h | 2 ++ arch/x86/kernel/apic/apic.c | 21 +++++++++++++++------ arch/x86/kernel/cpu/common.c | 4 ++++ 3 files changed, 21 insertions(+), 6 deletions(-)
arch/x86/include/asm/smp.h +2 -0
@@ -196,5 +196,7 @@ extern void nmi_selftest(void);#define nmi_selftest () do { } while (0)#endif +extern struct cpumask *cpu_capped_mask; +#endif /* __ASSEMBLY__ */ #endif /* _ASM_X86_SMP_H */
arch/x86/kernel/apic/apic.c +15 -6
@@ -2296,9 +2296,10 @@ static int allocate_logical_cpuid(int apicid)int generic_processor_info (int apicid,int version) { -int cpu, max = nr_cpu_ids; +int thiscpu, cpu, max = nr_cpu_ids;bool boot_cpu_detected =physid_isset (boot_cpu_physical_apicid, phys_cpu_present_map); +bool capped = false;/* * boot_cpu_physical_apicid is designed to have the apicid
@@ -2322,7 +2323,7 @@ int generic_processor_info(int apicid, int version)if (disabled_cpu_apicid !=BAD_APICID && disabled_cpu_apicid !=read_apic_id () && disabled_cpu_apicid == apicid) { -int thiscpu = num_processors + disabled_cpus; + thiscpu = num_processors + disabled_cpus; pr_warning("APIC: Disabling requested cpu." " Processor %d/0x%x ignored.\n",
@@ -2338,7 +2339,7 @@ int generic_processor_info(int apicid, int version)*/ if (!boot_cpu_detected && num_processors >= nr_cpu_ids -1 && apicid != boot_cpu_physical_apicid) { -int thiscpu = max + disabled_cpus -1 ; + thiscpu = max + disabled_cpus -1 ;pr_warning ( "APIC: NR_CPUS/possible_cpus limit of %i almost"
@@ -2346,20 +2347,28 @@ int generic_processor_info(int apicid, int version) " Processor %d/0x%x ignored.\n", max, thiscpu, apicid); disabled_cpus++; -return -EINVAL ; + capped = true; }if (num_processors >= nr_cpu_ids) { -int thiscpu = max + disabled_cpus; + thiscpu = max + disabled_cpus;pr_warning ("APIC: NR_CPUS/possible_cpus limit of %i " "reached. Processor %d/0x%x ignored.\n" , max, thiscpu, apicid); disabled_cpus++; -return -EINVAL ; + capped = true; } +if (capped) { +/* record the mapping between capped cpu and apicid */ +if (thiscpu <NR_CPUS && cpu_capped_mask !=NULL ) { + cpuid_to_apicid[thiscpu] = apicid; +cpumask_set_cpu (thiscpu, cpu_capped_mask); + } +return -EINVAL ; + }if (apicid == boot_cpu_physical_apicid) {/* * x86_bios_cpu_apicid is required to have processors listed
arch/x86/kernel/cpu/common.c +4 -0
@@ -66,6 +66,9 @@ u32 elf_hwcap2 __read_mostly;cpumask_var_t cpu_initialized_mask;cpumask_var_t cpu_callout_mask;cpumask_var_t cpu_callin_mask; +/* size of NR_CPUS is required. */ +static struct cpumask __cpu_capped_mask__initdata ; +struct cpumask *cpu_capped_mask;/* representing cpus for which sibling maps can be computed */ cpumask_var_t cpu_sibling_setup_mask;
@@ -84,6 +87,7 @@ void __init setup_cpu_local_masks(void)alloc_bootmem_cpumask_var (&cpu_callin_mask);alloc_bootmem_cpumask_var (&cpu_callout_mask);alloc_bootmem_cpumask_var (&cpu_sibling_setup_mask); + cpu_capped_mask = &__cpu_capped_mask; }static void default_init (struct cpuinfo_x86 *c)
Signature · 7 lines
2.7.5 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
PATCH v2 3/4 x86/smp: send capped cpus to a stable state when smp_init()
In reply to Pingfan Liu*** Back ground ***
On x86 it's required to have all logical CPUs set CR4.MCE=1. Otherwise, a broadcast MCE observing CR4.MCE=0b on any core will shutdown the machine.
The option 'nosmt' has already complied with the above rule by Thomas's patch. For detail, refer to 506a66f3748 (Revert "x86/apic: Ignore secondary threads if nosmt=force")
But for nr_cpus option, the exposure to broadcast MCE is a little complicated, and can be categorized into three cases.
-1. boot up by BIOS. Since no one set CR4.MCE=1, nr_cpus risks rebooting by broadcast MCE.
-2. boot up by "kexec -p nr_cpus=". Since the 1st kernel has all cpus' CR4.MCE=1 set before kexec -p, nr_cpus is free of rebooting by broadcast MCE. Furthermore, the crashed kernel's wreckage, including page table and text, is not touched by capture kernel. Hence if MCE event happens on capped cpu, do_machine_check->__mc_check_crashing_cpu() runs smoothly and returns immediately, the capped cpu is still pinned on "halt".
-3. boot up by "kexec -l nr_cpus=". As "kexec -p", it is free of rebooting by broadcast MCE. But the 1st kernel's wreckage is discarded and changed. when capped cpus execute do_machine_check(), they may crack the new kernel. But this is not related with broadcast MCE, and need an extra fix.
In a word, only case 1 suffers from unexpected rebooting due to broadcast MCE.
*** Solution ***
When fixing case 1, "nr_cpus" can not follow the same way as "nosmt". Because nr_cpus limits the allocation of percpu area and some other kthread memory, which is critical to cpu hotplug framework.
Instead, developing a dedicated SIPI callback make_capped_cpu_stable() for capped cpu, which does not lean on percpu area to work.
BTW this patch has side effect to suppress case 3. It brings capped cpu to make_capped_cpu_stable() in the 2nd kernel's context, instead of leaving them with the 1st kernel's context.
To: Thomas Gleixner <[email protected]>
To: Andy Lutomirski <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: "H. Peter Anvin" <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: Peter Zijlstra <[email protected]>
To: [email protected]
Cc: Masami Hiramatsu <[email protected]>
Cc: Qian Cai <[email protected]>
Cc: Vlastimil Babka <[email protected]>
Cc: Daniel Drake <[email protected]>
Cc: Jacob Pan <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: Eric Biederman <[email protected]>
Cc: [email protected]
Cc: Dave Young <[email protected]>
Cc: Baoquan He <[email protected]>
Cc: [email protected]
--- arch/x86/include/asm/apic.h | 1 + arch/x86/include/asm/smp.h | 1 + arch/x86/kernel/apic/apic.c | 2 +- arch/x86/kernel/cpu/common.c | 3 ++ arch/x86/kernel/smpboot.c | 85 ++++++++++++++++++++++++++++++++++++++++++++ kernel/smp.c | 6 ++++ 6 files changed, 97 insertions(+), 1 deletion(-)
arch/x86/include/asm/apic.h +1 -0
@@ -49,6 +49,7 @@ static inline void generic_apic_probe(void)#ifdef CONFIG_X86_LOCAL_APIC +extern int cpuid_to_apicid[];extern int apic_verbosity;extern int local_apic_timer_c2_ok;
arch/x86/include/asm/smp.h +1 -0
@@ -197,6 +197,7 @@ extern void nmi_selftest(void);#endif extern struct cpumask *cpu_capped_mask; +extern struct cpumask *cpu_capped_done_mask;#endif /* __ASSEMBLY__ */ #endif /* _ASM_X86_SMP_H */
arch/x86/kernel/apic/apic.c +1 -1
@@ -2244,7 +2244,7 @@ static int nr_logical_cpuids = 1;/* * Used to store mapping between logical CPU IDs and APIC IDs. */ -static int cpuid_to_apicid[] = { +int cpuid_to_apicid[] = { [0 ...NR_CPUS -1 ] =-1 , };
arch/x86/kernel/cpu/common.c +3 -0
@@ -69,6 +69,8 @@ cpumask_var_t cpu_callin_mask;/* size of NR_CPUS is required. */ static struct cpumask __cpu_capped_mask__initdata ;struct cpumask *cpu_capped_mask; +static struct cpumask __cpu_capped_done_mask__initdata ; +struct cpumask *cpu_capped_done_mask;/* representing cpus for which sibling maps can be computed */ cpumask_var_t cpu_sibling_setup_mask;
@@ -88,6 +90,7 @@ void __init setup_cpu_local_masks(void)alloc_bootmem_cpumask_var (&cpu_callout_mask);alloc_bootmem_cpumask_var (&cpu_sibling_setup_mask); cpu_capped_mask = &__cpu_capped_mask; + cpu_capped_done_mask = &__cpu_capped_done_mask; }static void default_init (struct cpuinfo_x86 *c)
arch/x86/kernel/smpboot.c +85 -0
91 lines not on this page · raw message
kernel/smp.c +6 -0
@@ -574,6 +574,10 @@ void __init setup_nr_cpu_ids(void) nr_cpu_ids =find_last_bit (cpumask_bits (cpu_possible_mask),NR_CPUS ) +1 ; } +void __weak __init bring_capped_cpu_stable (void ) +{ +} +/* Called by boot processor to activate the rest. */ void __init smp_init (void ) {
@@ -593,6 +597,8 @@ void __init smp_init(void)cpu_up (cpu); } +/* force cpus capped by nr_cpus option into stable state */ +bring_capped_cpu_stable (); num_nodes =num_online_nodes (); num_cpus =num_online_cpus (); pr_info("Brought up %d node%s, %d CPU%s\n",
Signature · 7 lines
2.7.5 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
PATCH v2 4/4 x86/smp: disallow MCE handler on rebooting AP
In reply to Pingfan Liu"kexec -l" sends the rest cpu to halt state with local apic disabled. But they can still respond to MCE. Meanwhile the execution of MCE handler relies on the 1st kernel's page table and text, which may be cracked during the 2nd kernel bootup. Hence Before sending SIPI to AP in 2nd kernel, an MCE event makes AP take the risk of running in weird context.
Heavily suppress it by disallowing MCE handler on rebooting AP.
Note: after this patch, "kexec -l" still has a little window vulnerable to weird context, despite AP uses tlb cache and icache. Consider the
scenario: The 1st kernel code native_halt() in stop_this_cpu() is modified during the 2nd kernel bootup. Then AP is waken up by MCE after the modification, and will continue in a weired context. This needs extra effort.
To: Thomas Gleixner <[email protected]>
To: Andy Lutomirski <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: "H. Peter Anvin" <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: Peter Zijlstra <[email protected]>
To: [email protected]
Cc: Masami Hiramatsu <[email protected]>
Cc: Qian Cai <[email protected]>
Cc: Vlastimil Babka <[email protected]>
Cc: Daniel Drake <[email protected]>
Cc: Jacob Pan <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: Eric Biederman <[email protected]>
Cc: [email protected]
Cc: Dave Young <[email protected]>
Cc: Baoquan He <[email protected]>
Cc: [email protected]
--- arch/x86/kernel/smp.c | 8 ++++++++ 1 file changed, 8 insertions(+)
arch/x86/kernel/smp.c +8 -0
@@ -157,11 +157,15 @@ void native_send_call_func_ipi(const struct cpumask *mask)static int smp_stop_nmi_callback (unsigned int val,struct pt_regs *regs) { +struct desc_ptr null_ptr = {0 }; +/* We are registered on stopping cpu too, avoid spurious NMI */ if (raw_smp_processor_id () ==atomic_read (&stopping_cpu))return NMI_HANDLED ;cpu_emergency_vmxoff (); +/* prevent from dispatching MCE handler */ +load_idt (&null_ptr);stop_this_cpu (NULL );return NMI_HANDLED ;
@@ -173,8 +177,12 @@ static int smp_stop_nmi_callback(unsigned int val, struct pt_regs *regs) asmlinkage __visible void smp_reboot_interrupt(void) { +struct desc_ptr null_ptr = {0 }; +ipi_entering_ack_irq ();cpu_emergency_vmxoff (); +/* prevent from dispatching MCE handler */ +load_idt (&null_ptr);stop_this_cpu (NULL );irq_exit (); }
Signature · 7 lines
2.7.5 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
Borislav Petkov
maintainerQuoted 17 lines · Pingfan Liu
v1 -> v2: fix compile warning and error on
x86_32This series include two related groups:
[1-3/4]: protectnr_cpusfrom rebooting by broadcast mce [4/4]: improve "kexec -l" robustness against broadcast mceWhen I tried to fix [1], Thomas raised concern about the
nr_cpus' vulnerability to unexpected rebooting by broadcast mce. After analysis, I think only the following first case suffers from the rebooting by broadcast mce. [1-3/4] aims to fix that issue.*** Back ground ***
On x86 it's required to have all logical CPUs set CR4.MCE=1. Otherwise, a broadcast MCE observing CR4.MCE=0b on any core will shutdown the machine.
Quoted 6 lines
The option 'nosmt' has already complied with the above rule by Thomas's patch. For detail, refer to
506a66f3748(Revert "x86/apic: Ignore secondary threads if nosmt=force")But for
nr_cpusoption, the exposure to broadcast MCE is a little complicated, and can be categorized into three cases.
One thing is not clear to me: are you "fixing" a hypothetical case here or have you *actually* experienced an MCE happening while kdumping with nr_cpus < num_online_cpus()?
Btw, pls do not use lkml.org to refer to previous mails but
http://lkml.kernel.org/r/<Message-ID>
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Quoted 22 lines · Borislav Petkov
Quoted 21 lines · Pingfan Liu
v1 -> v2: fix compile warning and error on
x86_32This series include two related groups:
[1-3/4]: protectnr_cpusfrom rebooting by broadcast mce [4/4]: improve "kexec -l" robustness against broadcast mceWhen I tried to fix [1], Thomas raised concern about the
nr_cpus' vulnerability to unexpected rebooting by broadcast mce. After analysis, I think only the following first case suffers from the rebooting by broadcast mce. [1-3/4] aims to fix that issue.*** Back ground ***
On x86 it's required to have all logical CPUs set CR4.MCE=1. Otherwise, a broadcast MCE observing CR4.MCE=0b on any core will shutdown the machine.
The option 'nosmt' has already complied with the above rule by Thomas's patch. For detail, refer to
506a66f3748(Revert "x86/apic: Ignore secondary threads if nosmt=force")
Quoted 6 lines
Quoted 2 lines
But for
nr_cpusoption, the exposure to broadcast MCE is a little complicated, and can be categorized into three cases.One thing is not clear to me: are you "fixing" a hypothetical case here or have you *actually* experienced an MCE happening while kdumping with
nr_cpus<num_online_cpus()?
No, I do not hit this issue by myself.
But from the following two commits:
commit 5bc329503e8191c91c4c40836f062ef771d8ba83
Author: Xunlei Pang <[email protected]> Date: Mon Mar 13 10:50:19 2017 +0100
x86/mce: Handle broadcasted MCE gracefully with kexec
And
commit 506a66f374891ff08e064a058c446b336c5ac760
Author: Thomas Gleixner <[email protected]> Date: Fri Jun 29 16:05:47 2018 +0200
Revert "x86/apic: Ignore secondary threads if nosmt=force"
This issue is in practice.
BTW, clarify one thing kdumping (kexec -p) will not suffer from mce issue as described in case 2. Only "kexec -l" will.
Quoted 4 lines
Btw, pls do not use lkml.org to refer to previous mails but
http://lkml.kernel.org/r/<Message-ID>
OK, I will.
Thanks,
Pingfan