mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Thomas Gleixner <tglx@linutronix.de>
To: LKML <linux-kernel@vger.kernel.org>
Cc: x86@kernel.org, Andrew Cooper <andrew.cooper3@citrix.com>,
	Tom Lendacky <thomas.lendacky@amd.com>,
	Paolo Bonzini <pbonzini@redhat.com>, Wei Liu <wei.liu@kernel.org>,
	Arjan van de Ven <arjan@linux.intel.com>,
	Juergen Gross <jgross@suse.com>,
	Michael Kelley <mikelley@microsoft.com>,
	Peter Keresztes Schmidt <peter@keresztesschmidt.de>,
	"Peter Zijlstra (Intel)" <peterz@infradead.org>
Subject: [patch V3 09/60] x86/apic: Get rid of boot_cpu_physical_apicid madness
Date: Tue,  1 Aug 2023 12:46:52 +0200 (CEST)	[thread overview]
Message-ID: <20230801103815.649527789@linutronix.de> (raw)
In-Reply-To: <20230801103042.936020332@linutronix.de>

boot_cpu_physical_apicid is written in random places and in the last
consequence filled with the APIC ID read from the local APIC. That causes
it to have inconsistent state when the MPTABLE is broken. As a consequence
tons of moronic checks are sprinkled all over the place.

Consolidate the code and read it exactly once when either X2APIC mode is
detected early or when the APIC mapping is established.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
 arch/x86/include/asm/apic.h |    2 
 arch/x86/kernel/apic/apic.c |  102 +++++++++++++-------------------------------
 arch/x86/kernel/mpparse.c   |    4 -
 3 files changed, 34 insertions(+), 74 deletions(-)

--- a/arch/x86/include/asm/apic.h
+++ b/arch/x86/include/asm/apic.h
@@ -247,7 +247,7 @@ static inline int x2apic_enabled(void)
 #else /* !CONFIG_X86_X2APIC */
 static inline void x2apic_setup(void) { }
 static inline int x2apic_enabled(void) { return 0; }
-
+static inline u32 native_apic_msr_read(u32 reg) { BUG(); }
 #define x2apic_mode		(0)
 #define	x2apic_supported()	(0)
 #endif /* !CONFIG_X86_X2APIC */
--- a/arch/x86/kernel/apic/apic.c
+++ b/arch/x86/kernel/apic/apic.c
@@ -1318,8 +1318,7 @@ static int __init __apic_intr_mode_selec
 	if (!boot_cpu_has(X86_FEATURE_APIC) &&
 		APIC_INTEGRATED(boot_cpu_apic_version)) {
 		apic_is_disabled = true;
-		pr_err(FW_BUG "Local APIC %d not detected, force emulation\n",
-				       boot_cpu_physical_apicid);
+		pr_err(FW_BUG "Local APIC not detected, force emulation\n");
 		return APIC_PIC;
 	}
 #endif
@@ -1340,12 +1339,6 @@ static int __init __apic_intr_mode_selec
 		pr_info("APIC: SMP mode deactivated\n");
 		return APIC_SYMMETRIC_IO_NO_ROUTING;
 	}
-
-	if (read_apic_id() != boot_cpu_physical_apicid) {
-		panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
-		     read_apic_id(), boot_cpu_physical_apicid);
-		/* Or can we switch back to PIC here? */
-	}
 #endif
 
 	return APIC_SYMMETRIC_IO;
@@ -1741,6 +1734,23 @@ void apic_ap_setup(void)
 	end_local_APIC_setup();
 }
 
+static __init void apic_read_boot_cpu_id(bool x2apic)
+{
+	/*
+	 * This can be invoked from check_x2apic() before the APIC has been
+	 * selected. But that code knows for sure that the BIOS enabled
+	 * X2APIC.
+	 */
+	if (x2apic) {
+		boot_cpu_physical_apicid = native_apic_msr_read(APIC_ID);
+		boot_cpu_apic_version = GET_APIC_VERSION(native_apic_msr_read(APIC_LVR));
+	} else {
+		boot_cpu_physical_apicid = read_apic_id();
+		boot_cpu_apic_version = GET_APIC_VERSION(apic_read(APIC_LVR));
+	}
+}
+
+
 #ifdef CONFIG_X86_X2APIC
 int x2apic_mode;
 EXPORT_SYMBOL_GPL(x2apic_mode);
@@ -1921,6 +1931,7 @@ void __init check_x2apic(void)
 			x2apic_state = X2APIC_ON_LOCKED;
 		else
 			x2apic_state = X2APIC_ON;
+		apic_read_boot_cpu_id(true);
 	} else if (!boot_cpu_has(X86_FEATURE_X2APIC)) {
 		x2apic_state = X2APIC_DISABLED;
 	}
@@ -2109,15 +2120,11 @@ static int __init detect_init_APIC(void)
  */
 void __init init_apic_mappings(void)
 {
-	unsigned int new_apicid;
-
 	if (apic_validate_deadline_timer())
 		pr_info("TSC deadline timer available\n");
 
-	if (x2apic_mode) {
-		boot_cpu_physical_apicid = read_apic_id();
+	if (x2apic_mode)
 		return;
-	}
 
 	/* If no local APIC can be found return early */
 	if (!smp_found_config && detect_init_APIC()) {
@@ -2134,39 +2141,19 @@ void __init init_apic_mappings(void)
 		if (!acpi_lapic && !smp_found_config)
 			register_lapic_address(apic_phys);
 	}
-
-	/*
-	 * Fetch the APIC ID of the BSP in case we have a
-	 * default configuration (or the MP table is broken).
-	 */
-	new_apicid = read_apic_id();
-	if (boot_cpu_physical_apicid != new_apicid) {
-		boot_cpu_physical_apicid = new_apicid;
-		/*
-		 * yeah -- we lie about apic_version
-		 * in case if apic was disabled via boot option
-		 * but it's not a problem for SMP compiled kernel
-		 * since apic_intr_mode_select is prepared for such
-		 * a case and disable smp mode
-		 */
-		boot_cpu_apic_version = GET_APIC_VERSION(apic_read(APIC_LVR));
-	}
 }
 
 void __init register_lapic_address(unsigned long address)
 {
 	mp_lapic_addr = address;
 
-	if (!x2apic_mode) {
-		set_fixmap_nocache(FIX_APIC_BASE, address);
-		apic_mmio_base = APIC_BASE;
-		apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
-			    APIC_BASE, address);
-	}
-	if (boot_cpu_physical_apicid == -1U) {
-		boot_cpu_physical_apicid  = read_apic_id();
-		boot_cpu_apic_version = GET_APIC_VERSION(apic_read(APIC_LVR));
-	}
+	if (x2apic_mode)
+		return;
+
+	set_fixmap_nocache(FIX_APIC_BASE, address);
+	apic_mmio_base = APIC_BASE;
+	apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n", APIC_BASE, address);
+	apic_read_boot_cpu_id(false);
 }
 
 /*
@@ -2446,31 +2433,15 @@ int generic_processor_info(int apicid, i
 				phys_cpu_present_map);
 
 	/*
-	 * boot_cpu_physical_apicid is designed to have the apicid
-	 * returned by read_apic_id(), i.e, the apicid of the
-	 * currently booting-up processor. However, on some platforms,
-	 * it is temporarily modified by the apicid reported as BSP
-	 * through MP table. Concretely:
-	 *
-	 * - arch/x86/kernel/mpparse.c: MP_processor_info()
-	 * - arch/x86/mm/amdtopology.c: amd_numa_init()
-	 *
-	 * This function is executed with the modified
-	 * boot_cpu_physical_apicid. So, disabled_cpu_apicid kernel
-	 * parameter doesn't work to disable APs on kdump 2nd kernel.
-	 *
-	 * Since fixing handling of boot_cpu_physical_apicid requires
-	 * another discussion and tests on each platform, we leave it
-	 * for now and here we use read_apic_id() directly in this
-	 * function, generic_processor_info().
+	 * boot_cpu_physical_apicid is guaranteed to contain the boot CPU
+	 * APIC ID read from the local APIC when this function is invoked.
 	 */
-	if (disabled_cpu_apicid != BAD_APICID &&
-	    disabled_cpu_apicid != read_apic_id() &&
+	if (disabled_cpu_apicid != boot_cpu_physical_apicid &&
 	    disabled_cpu_apicid == apicid) {
 		int thiscpu = num_processors + disabled_cpus;
 
-		pr_warn("APIC: Disabling requested cpu."
-			" Processor %d/0x%x ignored.\n", thiscpu, apicid);
+		pr_warn("APIC: Disabling requested cpu. Processor %d/0x%x ignored.\n",
+			thiscpu, apicid);
 
 		disabled_cpus++;
 		return -ENODEV;
@@ -2626,15 +2597,6 @@ static void __init apic_bsp_up_setup(voi
 {
 #ifdef CONFIG_X86_64
 	apic_write(APIC_ID, apic->set_apic_id(boot_cpu_physical_apicid));
-#else
-	/*
-	 * Hack: In case of kdump, after a crash, kernel might be booting
-	 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
-	 * might be zero if read from MP tables. Get it from LAPIC.
-	 */
-# ifdef CONFIG_CRASH_DUMP
-	boot_cpu_physical_apicid = read_apic_id();
-# endif
 #endif
 	physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
 }
--- a/arch/x86/kernel/mpparse.c
+++ b/arch/x86/kernel/mpparse.c
@@ -58,10 +58,8 @@ static void __init MP_processor_info(str
 
 	apicid = m->apicid;
 
-	if (m->cpuflag & CPU_BOOTPROCESSOR) {
+	if (m->cpuflag & CPU_BOOTPROCESSOR)
 		bootup_cpu = " (Bootup-CPU)";
-		boot_cpu_physical_apicid = m->apicid;
-	}
 
 	pr_info("Processor #%d%s\n", m->apicid, bootup_cpu);
 	generic_processor_info(apicid, m->apicver);


  parent reply	other threads:[~2023-08-01 10:47 UTC|newest]

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-01 10:46 [patch V3 00/60] x86/apic: Decrapification and static calls Thomas Gleixner
2023-08-01 10:46 ` [patch V3 01/60] x86/cpu: Make identify_boot_cpu() static Thomas Gleixner
2023-08-01 10:46 ` [patch V3 02/60] x86/cpu: Remove unused physid_*() nonsense Thomas Gleixner
2023-08-01 10:46 ` [patch V3 03/60] x86/apic: Rename disable_apic Thomas Gleixner
2023-08-01 10:46 ` [patch V3 04/60] x86/apic/ioapic: Rename skip_ioapic_setup Thomas Gleixner
2023-08-01 10:46 ` [patch V3 05/60] x86/apic: Remove pointless x86_bios_cpu_apicid Thomas Gleixner
2023-08-01 10:46 ` [patch V3 06/60] x86/apic: Get rid of hard_smp_processor_id() Thomas Gleixner
2023-08-01 10:46 ` [patch V3 07/60] x86/apic: Remove unused max_physical_apicid Thomas Gleixner
2023-08-01 10:46 ` [patch V3 08/60] x86/apic: Nuke unused apic::inquire_remote_apic() Thomas Gleixner
2023-08-01 10:46 ` Thomas Gleixner [this message]
2023-08-01 10:46 ` [patch V3 10/60] x86/apic: Register boot CPU APIC early Thomas Gleixner
2023-08-01 10:46 ` [patch V3 11/60] x86/apic: Remove the pointless APIC version check Thomas Gleixner
2023-08-01 10:46 ` [patch V3 12/60] x86/of: Fix the APIC address registration Thomas Gleixner
2023-08-03 22:42   ` Dave Hansen
2023-08-03 23:35     ` Thomas Gleixner
2023-08-01 10:46 ` [patch V3 13/60] x86/apic: Make some APIC init functions bool Thomas Gleixner
2023-08-01 10:46 ` [patch V3 14/60] x86/apic: Split register_apic_address() Thomas Gleixner
2023-08-01 10:46 ` [patch V3 15/60] x86/apic: Sanitize APIC address setup Thomas Gleixner
2023-08-01 10:47 ` [patch V3 16/60] x86/xen/pv: Pretend that it found SMP configuration Thomas Gleixner
2023-08-01 10:47 ` [patch V3 17/60] x86/apic: Sanitize num_processors handling Thomas Gleixner
2023-08-01 10:47 ` [patch V3 18/60] x86/apic: Nuke another processor check Thomas Gleixner
2023-08-01 10:47 ` [patch V3 19/60] x86/apic: Remove check_phys_apicid_present() Thomas Gleixner
2023-08-01 10:47 ` [patch V3 20/60] x86/apic: Get rid of apic_phys Thomas Gleixner
2023-08-01 10:47 ` [patch V3 21/60] x86/apic/32: Sanitize logical APIC ID handling Thomas Gleixner
2023-08-01 10:47 ` [patch V3 22/60] x86/apic/32: Remove x86_cpu_to_logical_apicid Thomas Gleixner
2023-08-01 10:47 ` [patch V3 23/60] x86/apic/ipi: Code cleanup Thomas Gleixner
2023-08-01 10:47 ` [patch V3 24/60] x86/apic: Mop up early_per_cpu() abuse Thomas Gleixner
2023-08-01 10:47 ` [patch V3 25/60] x86/apic/32: Remove pointless default_acpi_madt_oem_check() Thomas Gleixner
2023-08-01 10:47 ` [patch V3 26/60] x86/apic/32: Decrapify the def_bigsmp mechanism Thomas Gleixner
2023-08-07 20:11   ` Dave Hansen
2023-08-07 21:18     ` Thomas Gleixner
2023-08-01 10:47 ` [patch V3 27/60] x86/apic/32: Remove bigsmp_cpu_present_to_apicid() Thomas Gleixner
2023-08-01 10:47 ` [patch V3 28/60] x86/apic: Nuke empty init_apic_ldr() callbacks Thomas Gleixner
2023-08-01 10:47 ` [patch V3 29/60] x86/apic: Nuke apic::apicid_to_cpu_present() Thomas Gleixner
2023-08-01 10:47 ` [patch V3 30/60] x86/ioapic/32: Decrapify phys_id_present_map operation Thomas Gleixner
2023-08-01 10:47 ` [patch V3 31/60] x86/apic: Mop up *setup_apic_routing() Thomas Gleixner
2023-08-01 10:47 ` [patch V3 32/60] x86/apic: Mop up apic::apic_id_registered() Thomas Gleixner
2023-08-01 10:47 ` [patch V3 33/60] x86/apic/ipi: Tidy up the code and fixup comments Thomas Gleixner
2023-08-01 10:47 ` [patch V3 34/60] x86/apic: Consolidate wait_icr_idle() implementations Thomas Gleixner
2023-08-01 10:47 ` [patch V3 35/60] x86/apic: Allow apic::wait_icr_idle() to be NULL Thomas Gleixner
2023-08-01 10:47 ` [patch V3 36/60] x86/apic: Allow apic::safe_wait_icr_idle() " Thomas Gleixner
2023-08-01 10:47 ` [patch V3 37/60] x86/apic: Move safe wait_icr_idle() next to apic_mem_wait_icr_idle() Thomas Gleixner
2023-08-01 10:47 ` [patch V3 38/60] x86/apic/uv: Get rid of wrapper callbacks Thomas Gleixner
2023-08-01 10:47 ` [patch V3 39/60] x86/apic/x2apic: Share all common IPI functions Thomas Gleixner
2023-08-01 10:47 ` [patch V3 40/60] x86/apic/64: Uncopypaste probing Thomas Gleixner
2023-08-01 10:47 ` [patch V3 41/60] x86/apic: Wrap APIC ID validation into an inline Thomas Gleixner
2023-08-01 10:47 ` [patch V3 42/60] x86/apic: Add max_apic_id member Thomas Gleixner
2023-08-01 10:47 ` [patch V3 43/60] x86/apic: Simplify X2APIC ID validation Thomas Gleixner
2023-08-01 10:47 ` [patch V3 44/60] x86/apic: Prepare x2APIC for using apic::max_apic_id Thomas Gleixner
2023-08-01 10:47 ` [patch V3 45/60] x86/apic: Sanitize APID ID range validation Thomas Gleixner
2023-08-08 13:34   ` Qiuxu Zhuo
2023-08-08 18:55     ` Thomas Gleixner
2023-08-09 12:01   ` Zhang, Rui
2023-08-01 10:47 ` [patch V3 46/60] x86/apic: Remove pointless NULL initializations Thomas Gleixner
2023-08-01 10:47 ` [patch V3 47/60] x86/apic/noop: Tidy up the code Thomas Gleixner
2023-08-01 10:47 ` [patch V3 48/60] x86/apic: Remove pointless arguments from [native_]eoi_write() Thomas Gleixner
2023-08-01 10:47 ` [patch V3 49/60] x86/apic: Nuke ack_APIC_irq() Thomas Gleixner
2023-08-01 10:47 ` [patch V3 50/60] x86/apic: Wrap apic->native_eoi() into a helper Thomas Gleixner
2023-08-01 10:47 ` [patch V3 51/60] x86/apic: Provide common init infrastructure Thomas Gleixner
2023-08-09  1:41   ` Qiuxu Zhuo
2023-08-01 10:47 ` [patch V3 52/60] x86/xen/apic: Use standard apic driver mechanism for Xen PV Thomas Gleixner
2023-08-01 10:47 ` [patch V3 53/60] x86/apic: Provide apic_update_callback() Thomas Gleixner
2023-08-01 10:47 ` [patch V3 54/60] x86/apic: Replace acpi_wake_cpu_handler_update() and apic_set_eoi_cb() Thomas Gleixner
2023-08-01 10:47 ` [patch V3 55/60] x86/apic: Convert other overrides to apic_update_callback() Thomas Gleixner
2023-08-01 10:47 ` [patch V3 56/60] x86/xen/apic: Mark apic __ro_after_init Thomas Gleixner
2023-08-01 10:47 ` [patch V3 57/60] x86/apic: Mark all hotpath APIC callback wrappers __always_inline Thomas Gleixner
2023-08-01 10:47 ` [patch V3 58/60] x86/apic: Wrap IPI calls into helper functions Thomas Gleixner
2023-08-01 10:47 ` [patch V3 59/60] x86/apic: Provide static call infrastructure for APIC callbacks Thomas Gleixner
2023-08-01 10:47 ` [patch V3 60/60] x86/apic: Turn on static calls Thomas Gleixner
2023-08-02 11:55 ` [patch V3 00/60] x86/apic: Decrapification and " Juergen Gross
2023-08-03  0:05 ` Sohil Mehta
2023-08-03  3:50 ` Michael Kelley (LINUX)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230801103815.649527789@linutronix.de \
    --to=tglx@linutronix.de \
    --cc=andrew.cooper3@citrix.com \
    --cc=arjan@linux.intel.com \
    --cc=jgross@suse.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mikelley@microsoft.com \
    --cc=pbonzini@redhat.com \
    --cc=peter@keresztesschmidt.de \
    --cc=peterz@infradead.org \
    --cc=thomas.lendacky@amd.com \
    --cc=wei.liu@kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®