Skip to contentSkip to conversation

x86/kexec: Check for a good per-cpu area before accessing IDTs

PATCH In review Andrew Cooper 3 participants 3 messages Jun 22 – Jun 23

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/kexec: Check for a good per-cpu area before accessing IDTs

Prior to commit 9c20d3c5915d ("x86/IDT: Make idt_tables[] be per_cpu(idt)"), the global idt_tables[] was always safe to use for CPUs in any state.

However, not-yet-onlined CPUs (e.g. MADT with more entries than exist in practice) or offlined CPUs (e.g. xen-hptool) have their per-cpu pointer poisoned to detect incorrect uses. machine_kexec() trips over the posion when clobbering #MC entry paths.

This fixes a fatal #GP (non-canonical memory reference) when trying to enter the crash kernel.

Fixes 9c20d3c5915d ("x86/IDT: Make idt_tables[] be per_cpu(idt)")

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>
CC: Oleksii Kurochko <[email protected]>
CC: Lin Liu <[email protected]>

The fix here is a bit ugly. nmi_shootdown_cpus() uses the cpu_online_map but this is wrong too; it misses parked CPUs, which do want to be captured.

For 4.22. This is the minimal fix to stop systems crashing, but more work is needed to make this path fully robust.

---
 xen/arch/x86/machine_kexec.c | 8 +++++++-
 xen/common/percpu.c          | 1 -
 xen/include/xen/percpu.h     | 1 +
 3 files changed, 8 insertions(+), 2 deletions(-)
xen/arch/x86/machine_kexec.c +7 -1
index f921eec5aae6..0f5437cb65cc 100644
@@ -18,6 +18,7 @@
 #include <xen/domain_page.h>
 #include <xen/elfstructs.h>
 #include <xen/kexec.h>
+#include <xen/percpu.h>
 #include <xen/types.h>
 
 #include <asm/fixmap.h>
@@ -171,7 +172,12 @@ void machine_kexec(struct kexec_image *image)
      */
     for ( i = 0; i < nr_cpu_ids; i++ )
     {
-        idt_entry_t *idt = per_cpu(idt, i);
+        idt_entry_t *idt;
+
+        if ( __per_cpu_offset[i] == INVALID_PERCPU_AREA )
+            continue;
+
+        idt = per_cpu(idt, i);
 
         if ( !idt )
             continue;
xen/common/percpu.c +0 -1
index cdd70acbeaf3..f180f37253ed 100644
@@ -13,7 +13,6 @@
 
 #define PERCPU_ORDER get_order_from_bytes(__per_cpu_data_end - __per_cpu_start)
 
-extern char __per_cpu_start[];
 extern const char __per_cpu_data_end[];
 
 unsigned long __read_mostly __per_cpu_offset[NR_CPUS];
xen/include/xen/percpu.h +1 -0
index fcf2095bd543..30609f49f0b3 100644
@@ -43,6 +43,7 @@
 #endif
 
 extern unsigned long __per_cpu_offset[];
+extern char __per_cpu_start[];
 
 #define per_cpu(var, cpu)  \
     (*RELOC_HIDE(&per_cpu__##var, __per_cpu_offset[cpu]))

base-commit: 6a21252a742ec021a814e124b88d273da37065db

Signature · 1 line
2.39.5
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Andrew Cooper

Prior to commit 9c20d3c5915d ("x86/IDT: Make idt_tables[] be per_cpu(idt)"), the global idt_tables[] was always safe to use for CPUs in any state.

However, not-yet-onlined CPUs (e.g. MADT with more entries than exist in practice) or offlined CPUs (e.g. xen-hptool) have their per-cpu pointer poisoned to detect incorrect uses. machine_kexec() trips over the posion when clobbering #MC entry paths.

Quoted 6 lines

This fixes a fatal #GP (non-canonical memory reference) when trying to enter the crash kernel.

Fixes 9c20d3c5915d ("x86/IDT: Make idt_tables[] be per_cpu(idt)")

albeit preferably, because of ...

Quoted 2 lines

The fix here is a bit ugly. nmi_shootdown_cpus() uses the cpu_online_map but this is wrong too; it misses parked CPUs, which do want to be captured.

... this, ...

Quoted 4 lines
xen/arch/x86/machine_kexec.c +0 -0
@@ -171,7 +172,12 @@ void machine_kexec(struct kexec_image *image)
      */
     for ( i = 0; i < nr_cpu_ids; i++ )
     {

xen/arch/x86/machine_kexec.c void machine_kexec(struct kexec_image *image) · line 183

Quoted 10 lines
xen/arch/x86/machine_kexec.c +6 -1
-        idt_entry_t *idt = per_cpu(idt, i);
+        idt_entry_t *idt;
+
+        if ( __per_cpu_offset[i] == INVALID_PERCPU_AREA )
+            continue;
+
+        idt = per_cpu(idt, i);
 
         if ( !idt )
             continue;

... with the comment ahead of the for() also adjusted to at least briefly mention why cpu_online() cannot be used, and why hence this (seemingly) awkward check is needed instead.

Jan

#2 lore raw
In reply to Andrew Cooper
Quoted 21 lines · Andrew Cooper

Prior to commit 9c20d3c5915d ("x86/IDT: Make idt_tables[] be per_cpu(idt)"), the global idt_tables[] was always safe to use for CPUs in any state.

However, not-yet-onlined CPUs (e.g. MADT with more entries than exist in practice) or offlined CPUs (e.g. xen-hptool) have their per-cpu pointer poisoned to detect incorrect uses. machine_kexec() trips over the posion when clobbering #MC entry paths.

This fixes a fatal #GP (non-canonical memory reference) when trying to enter the crash kernel.

Fixes 9c20d3c5915d ("x86/IDT: Make idt_tables[] be per_cpu(idt)")

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>
CC: Oleksii Kurochko <[email protected]>
CC: Lin Liu <[email protected]>

Quoted 6 lines

The fix here is a bit ugly. nmi_shootdown_cpus() uses the cpu_online_map but this is wrong too; it misses parked CPUs, which do want to be captured.

For 4.22. This is the minimal fix to stop systems crashing, but more work is needed to make this path fully robust.
---

Thanks.

~ Oleksii