[PATCH] x86/apic: Switch to x2apic driver early if x2apic is enabled

From: Grzegorz Jaszczyk

Date: Thu Sep 10 2026 - 05:15:58 EST


During early boot, the generic x86 kernel defaults to the MMIO-based
APIC driver (apic_physflat). However, if the kernel is booted (e.g. via
kexec) when x2apic is already enabled in hardware, the MMIO interface to
the APIC is disabled.

Normally, ACPI MADT probing would install an x2APIC driver early.
However, if ACPI is disabled (e.g., CONFIG_ACPI is not set, as in
crashdump kernels), x86_64_probe_apic() does not run until late_time_init()
via apic_intr_mode_init().

This creates a window between local_irq_enable() and late_time_init()
where interrupts are enabled, but the APIC driver pointer still points
to apic_physflat. Because check_x2apic() detected hardware x2APIC mode
and set x2apic_mode = 1, init_apic_mappings() skipped mapping the APIC
fixmap. If a pending interrupt (e.g., left in IRR across kexec on
secondary CPUs) fires during this window, native_apic_mem_eoi() is
invoked, which attempts to write to the unmapped APIC EOI register,
triggering an immediate kernel page fault (#PF).

To prevent this, check if x2apic is enabled in hardware during early APIC
call setup in apic_setup_apic_calls() and switch the default APIC driver
from apic_physflat to apic_x2apic_phys immediately. This ensures that any
early EOI writes use safe MSR-based accesses. The driver can still be
upgraded later during the normal APIC probe phase.

Signed-off-by: Grzegorz Jaszczyk <jaszczyk@xxxxxxxxxxxx>
---
arch/x86/kernel/apic/init.c | 2 ++
arch/x86/kernel/apic/local.h | 5 +++++
arch/x86/kernel/apic/x2apic_phys.c | 10 ++++++++++
3 files changed, 17 insertions(+)

diff --git a/arch/x86/kernel/apic/init.c b/arch/x86/kernel/apic/init.c
index 821e2e536f19..c48323f725e7 100644
--- a/arch/x86/kernel/apic/init.c
+++ b/arch/x86/kernel/apic/init.c
@@ -82,6 +82,8 @@ static __init void update_static_calls(void)

void __init apic_setup_apic_calls(void)
{
+ x2apic_phys_early_init();
+
/* Ensure that the default APIC has native_eoi populated */
apic->native_eoi = apic->eoi;
update_static_calls();
diff --git a/arch/x86/kernel/apic/local.h b/arch/x86/kernel/apic/local.h
index 090dd71837aa..0c33eaa3b03a 100644
--- a/arch/x86/kernel/apic/local.h
+++ b/arch/x86/kernel/apic/local.h
@@ -20,6 +20,11 @@ void x2apic_send_IPI_all(int vector);
void x2apic_send_IPI_allbutself(int vector);
void x2apic_send_IPI_self(int vector);
extern u32 x2apic_max_apicid;
+#ifdef CONFIG_X86_X2APIC
+void x2apic_phys_early_init(void);
+#else
+static inline void x2apic_phys_early_init(void) { }
+#endif

/* IPI */

diff --git a/arch/x86/kernel/apic/x2apic_phys.c b/arch/x86/kernel/apic/x2apic_phys.c
index 090647cc5a78..c41f47aed7ab 100644
--- a/arch/x86/kernel/apic/x2apic_phys.c
+++ b/arch/x86/kernel/apic/x2apic_phys.c
@@ -155,3 +155,13 @@ static struct apic apic_x2apic_phys __ro_after_init = {
};

apic_driver(apic_x2apic_phys);
+
+void __init x2apic_phys_early_init(void)
+{
+ if (x2apic_enabled()) {
+ apic = &apic_x2apic_phys;
+ if (apic->x2apic_set_max_apicid)
+ apic->max_apic_id = x2apic_max_apicid;
+ pr_info("Switched default APIC to: %s\n", apic->name);
+ }
+}
--
2.55.0.1003.g10538fe699-goog