Re: [PATCH] x86/apic: Switch to x2apic driver early if x2apic is enabled
From: Dave Hansen
Date: Thu Sep 10 2026 - 11:35:01 EST
On 9/10/26 02:04, Grzegorz Jaszczyk wrote:
> 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.
There's also some new hardware that locks the hardware in x2apic mode. I
wonder if it has a similar problem.
> 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().
Is this "default_acpi_madt_oem_check()" that uses apic_install_driver()?
> 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).
Long-term, it seems like reducing the use of x2apic_mode would be really
nice.
> 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.
>
>
> 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();
This seems a bit ad-hoc.
> 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);
> + }
> +}
This seems to be a subset of what apic_install_driver() and
apic_x2apic_phys->probe() do, down do the pr_info().
Even if those are overkill for this, it would be nice to use them for
consistency if they function.
It also seems like some of the more obscure apic drivers depend on
x2apic_mode. They also seem like they might be selected by the MADT
search. Could this end up overwriting those?
Maybe this is too much work for the task at hand, but this does seem to
show that there's some information missing from 'struct apic'. Say
apic_physflat had an ->initialized() that was false until its MMIO was
unmapped and apic_x2apic_phys->initialized() pointed over to:
x2apic_enabled().
apic_setup_apic_calls()
{
// Search for an APIC driver that is initialized:
for_each(drv) {
if (!drv->initialized())
continue;
apic_install_driver(drv);
}
}
Basically, I'm wondering if it's worth continuing to special-case x2apic
handling or whether it should just be a part of the apic driver
infrastructure.