All of lore.kernel.org
 help / color / mirror / Atom feed
* [patch 0/4] x86/topology: Cure fallout
@ 2024-03-22 18:56 Thomas Gleixner
  2024-03-22 18:56 ` [patch 1/4] x86/cpu: Ensure that CPU info updates are propagated on UP Thomas Gleixner
                   ` (3 more replies)
  0 siblings, 4 replies; 13+ messages in thread
From: Thomas Gleixner @ 2024-03-22 18:56 UTC (permalink / raw)
  To: LKML; +Cc: x86, Guenter Roeck, Linus Torvalds

Guenter reported a crash on UP which was caused by the per CPU rework and
this unearthed a few other issues in the new topology evaluation code:

  https://lore.kernel.org/all/e20d88d0-5fb9-4307-be67-88b04ae9a188@roeck-us.net/

The following series addresses it. It is based on Linus tree and also
available from git:

   git://git.kernel.org/pub/scm/linux/kernel/git/tglx/devel.git x86/core

Thanks,

	tglx
---
 arch/x86/kernel/cpu/common.c          |    9 +++++++++
 arch/x86/kernel/cpu/topology.c        |   11 +++++++++++
 arch/x86/kernel/cpu/topology_common.c |   12 +++++++-----
 arch/x86/kernel/mpparse.c             |   10 +++++-----
 arch/x86/kernel/setup.c               |   10 ----------
 arch/x86/kernel/smpboot.c             |   32 +++++---------------------------
 6 files changed, 37 insertions(+), 47 deletions(-)

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [patch 1/4] x86/cpu: Ensure that CPU info updates are propagated on UP
  2024-03-22 18:56 [patch 0/4] x86/topology: Cure fallout Thomas Gleixner
@ 2024-03-22 18:56 ` Thomas Gleixner
  2024-03-22 21:41   ` Guenter Roeck
  2024-03-23 13:24   ` [tip: x86/urgent] " tip-bot2 for Thomas Gleixner
  2024-03-22 18:56 ` [patch 2/4] x86/topology: Dont evaluate logical IDs during early boot Thomas Gleixner
                   ` (2 subsequent siblings)
  3 siblings, 2 replies; 13+ messages in thread
From: Thomas Gleixner @ 2024-03-22 18:56 UTC (permalink / raw)
  To: LKML; +Cc: x86, Guenter Roeck, Linus Torvalds

The boot sequence evaluates CPUID information twice:

  1) During early boot

  2) When finalizing the early setup right before
     mitigations are selected and alternatives are patched.

In both cases the evaluation is stored in boot_cpu_data, but on UP the
copying of boot_cpu_data to the per CPU info of the boot CPU happens
between #1 and #2. So any update which happens in #2 is never propagated to
the per CPU info instance.

Consolidate the whole logic and copy boot_cpu_data right before applying
alternatives as that's the point where boot_cpu_data is in it's final state
and not supposed to change anymore.

This also removes the voodoo mb() from smp_prepare_cpus_common() which had
absolutely no purpose.

Fixes: 71eb4893cfaf ("x86/percpu: Cure per CPU madness on UP")
Reported-by: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 arch/x86/kernel/cpu/common.c |    9 +++++++++
 arch/x86/kernel/setup.c      |   10 ----------
 arch/x86/kernel/smpboot.c    |   32 +++++---------------------------
 3 files changed, 14 insertions(+), 37 deletions(-)

--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -2307,6 +2307,8 @@ void arch_smt_update(void)
 
 void __init arch_cpu_finalize_init(void)
 {
+	struct cpuinfo_x86 *c = this_cpu_ptr(&cpu_info);
+
 	identify_boot_cpu();
 
 	select_idle_routine();
@@ -2345,6 +2347,13 @@ void __init arch_cpu_finalize_init(void)
 	fpu__init_system();
 	fpu__init_cpu();
 
+	/*
+	 * Ensure that access to the per CPU representation has the initial
+	 * boot CPU configuration.
+	 */
+	*c = boot_cpu_data;
+	c->initialized = true;
+
 	alternative_instructions();
 
 	if (IS_ENABLED(CONFIG_X86_64)) {
--- a/arch/x86/kernel/setup.c
+++ b/arch/x86/kernel/setup.c
@@ -1206,16 +1206,6 @@ void __init i386_reserve_resources(void)
 
 #endif /* CONFIG_X86_32 */
 
-#ifndef CONFIG_SMP
-void __init smp_prepare_boot_cpu(void)
-{
-	struct cpuinfo_x86 *c = &cpu_data(0);
-
-	*c = boot_cpu_data;
-	c->initialized = true;
-}
-#endif
-
 static struct notifier_block kernel_offset_notifier = {
 	.notifier_call = dump_kernel_offset
 };
--- a/arch/x86/kernel/smpboot.c
+++ b/arch/x86/kernel/smpboot.c
@@ -313,14 +313,6 @@ static void notrace start_secondary(void
 	cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
 }
 
-static void __init smp_store_boot_cpu_info(void)
-{
-	struct cpuinfo_x86 *c = &cpu_data(0);
-
-	*c = boot_cpu_data;
-	c->initialized = true;
-}
-
 /*
  * The bootstrap kernel entry code has set these up. Save them for
  * a given CPU
@@ -1039,29 +1031,15 @@ static __init void disable_smp(void)
 	cpumask_set_cpu(0, topology_die_cpumask(0));
 }
 
-static void __init smp_cpu_index_default(void)
-{
-	int i;
-	struct cpuinfo_x86 *c;
-
-	for_each_possible_cpu(i) {
-		c = &cpu_data(i);
-		/* mark all to hotplug */
-		c->cpu_index = nr_cpu_ids;
-	}
-}
-
 void __init smp_prepare_cpus_common(void)
 {
 	unsigned int i;
 
-	smp_cpu_index_default();
-
-	/*
-	 * Setup boot CPU information
-	 */
-	smp_store_boot_cpu_info(); /* Final full version of the data */
-	mb();
+	/* Mark all except the boot CPU as hotpluggable */
+	for_each_possible_cpu(i) {
+		if (i)
+			per_cpu(cpu_info.cpu_index, i) = nr_cpu_ids;
+	}
 
 	for_each_possible_cpu(i) {
 		zalloc_cpumask_var(&per_cpu(cpu_sibling_map, i), GFP_KERNEL);


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [patch 2/4] x86/topology: Dont evaluate logical IDs during early boot
  2024-03-22 18:56 [patch 0/4] x86/topology: Cure fallout Thomas Gleixner
  2024-03-22 18:56 ` [patch 1/4] x86/cpu: Ensure that CPU info updates are propagated on UP Thomas Gleixner
@ 2024-03-22 18:56 ` Thomas Gleixner
  2024-03-22 21:41   ` Guenter Roeck
  2024-03-23 13:24   ` [tip: x86/urgent] x86/topology: Don't " tip-bot2 for Thomas Gleixner
  2024-03-22 18:56 ` [patch 3/4] x86/topology: Handle the !APIC case gracefully Thomas Gleixner
  2024-03-22 18:56 ` [patch 4/4] x86/mpparse: Register APIC address only once Thomas Gleixner
  3 siblings, 2 replies; 13+ messages in thread
From: Thomas Gleixner @ 2024-03-22 18:56 UTC (permalink / raw)
  To: LKML; +Cc: x86, Guenter Roeck, Linus Torvalds

The local APICs have not yet been enumerated so the logical ID evaluation
from the topology bitmaps does not work and would return an error code.

Skip the evaluation during the early boot CPUID evaluation and only apply
it on the final run.

Fixes: 380414be78bf ("x86/cpu/topology: Use topology logical mapping mechanism")
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 arch/x86/kernel/cpu/topology_common.c |   12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

--- a/arch/x86/kernel/cpu/topology_common.c
+++ b/arch/x86/kernel/cpu/topology_common.c
@@ -140,7 +140,7 @@ static void parse_topology(struct topo_s
 	}
 }
 
-static void topo_set_ids(struct topo_scan *tscan)
+static void topo_set_ids(struct topo_scan *tscan, bool early)
 {
 	struct cpuinfo_x86 *c = tscan->c;
 	u32 apicid = c->topo.apicid;
@@ -148,8 +148,10 @@ static void topo_set_ids(struct topo_sca
 	c->topo.pkg_id = topo_shift_apicid(apicid, TOPO_PKG_DOMAIN);
 	c->topo.die_id = topo_shift_apicid(apicid, TOPO_DIE_DOMAIN);
 
-	c->topo.logical_pkg_id = topology_get_logical_id(apicid, TOPO_PKG_DOMAIN);
-	c->topo.logical_die_id = topology_get_logical_id(apicid, TOPO_DIE_DOMAIN);
+	if (!early) {
+		c->topo.logical_pkg_id = topology_get_logical_id(apicid, TOPO_PKG_DOMAIN);
+		c->topo.logical_die_id = topology_get_logical_id(apicid, TOPO_DIE_DOMAIN);
+	}
 
 	/* Package relative core ID */
 	c->topo.core_id = (apicid & topo_domain_mask(TOPO_PKG_DOMAIN)) >>
@@ -187,7 +189,7 @@ void cpu_parse_topology(struct cpuinfo_x
 		       tscan.dom_shifts[dom], x86_topo_system.dom_shifts[dom]);
 	}
 
-	topo_set_ids(&tscan);
+	topo_set_ids(&tscan, false);
 }
 
 void __init cpu_init_topology(struct cpuinfo_x86 *c)
@@ -208,7 +210,7 @@ void __init cpu_init_topology(struct cpu
 		x86_topo_system.dom_size[dom] = 1U << sft;
 	}
 
-	topo_set_ids(&tscan);
+	topo_set_ids(&tscan, true);
 
 	/*
 	 * AMD systems have Nodes per package which cannot be mapped to


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [patch 3/4] x86/topology: Handle the !APIC case gracefully
  2024-03-22 18:56 [patch 0/4] x86/topology: Cure fallout Thomas Gleixner
  2024-03-22 18:56 ` [patch 1/4] x86/cpu: Ensure that CPU info updates are propagated on UP Thomas Gleixner
  2024-03-22 18:56 ` [patch 2/4] x86/topology: Dont evaluate logical IDs during early boot Thomas Gleixner
@ 2024-03-22 18:56 ` Thomas Gleixner
  2024-03-22 21:41   ` Guenter Roeck
  2024-03-23 13:24   ` [tip: x86/urgent] " tip-bot2 for Thomas Gleixner
  2024-03-22 18:56 ` [patch 4/4] x86/mpparse: Register APIC address only once Thomas Gleixner
  3 siblings, 2 replies; 13+ messages in thread
From: Thomas Gleixner @ 2024-03-22 18:56 UTC (permalink / raw)
  To: LKML; +Cc: x86, Guenter Roeck, Linus Torvalds

If there is no local APIC enumerated and registered then the topology
bitmaps are empty. Therefore topology_init_possible_cpus() will die with a
division by zero exception.

Prevent this by registering a fake APIC id to populate the topology
bitmap. This also allows to use all topology query interfaces
unconditionally. It does not affect the actual APIC code because either the
local APIC address was not registered or no local APIC could be detected.

Fixes: f1f758a80516 ("x86/topology: Add a mechanism to track topology via APIC IDs")
Reported-by: Guenter Roeck <linux@roeck-us.net>
Reported-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 arch/x86/kernel/cpu/topology.c |   11 +++++++++++
 1 file changed, 11 insertions(+)

--- a/arch/x86/kernel/cpu/topology.c
+++ b/arch/x86/kernel/cpu/topology.c
@@ -415,6 +415,17 @@ void __init topology_init_possible_cpus(
 	unsigned int total = assigned + disabled;
 	u32 apicid, firstid;
 
+	/*
+	 * If there was no APIC registered, then fake one so that the
+	 * topology bitmap is populated. That ensures that the code below
+	 * is valid and the various query interfaces can be used
+	 * unconditionally. This does not affect the actual APIC code in
+	 * any way because either the local APIC address has not been
+	 * registered or the local APIC was disabled on the command line.
+	 */
+	if (topo_info.boot_cpu_apic_id == BAD_APICID)
+		topology_register_boot_apic(0);
+
 	if (!restrict_to_up()) {
 		if (WARN_ON_ONCE(assigned > nr_cpu_ids)) {
 			disabled += assigned - nr_cpu_ids;


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [patch 4/4] x86/mpparse: Register APIC address only once
  2024-03-22 18:56 [patch 0/4] x86/topology: Cure fallout Thomas Gleixner
                   ` (2 preceding siblings ...)
  2024-03-22 18:56 ` [patch 3/4] x86/topology: Handle the !APIC case gracefully Thomas Gleixner
@ 2024-03-22 18:56 ` Thomas Gleixner
  2024-03-22 21:41   ` Guenter Roeck
  2024-03-23 13:24   ` [tip: x86/urgent] " tip-bot2 for Thomas Gleixner
  3 siblings, 2 replies; 13+ messages in thread
From: Thomas Gleixner @ 2024-03-22 18:56 UTC (permalink / raw)
  To: LKML; +Cc: x86, Guenter Roeck, Linus Torvalds

The APIC address is registered twice. First during the early detection and
afterwards when actually scanning the table for APIC IDs. The APIC and
topology core warn about the second attempt.

Restrict it to the early detection call.

Fixes: 81287ad65da5 ("x86/apic: Sanitize APIC address setup")
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 arch/x86/kernel/mpparse.c |   10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

--- a/arch/x86/kernel/mpparse.c
+++ b/arch/x86/kernel/mpparse.c
@@ -197,12 +197,12 @@ static int __init smp_read_mpc(struct mp
 	if (!smp_check_mpc(mpc, oem, str))
 		return 0;
 
-	/* Initialize the lapic mapping */
-	if (!acpi_lapic)
-		register_lapic_address(mpc->lapic);
-
-	if (early)
+	if (early) {
+		/* Initialize the lapic mapping */
+		if (!acpi_lapic)
+			register_lapic_address(mpc->lapic);
 		return 1;
+	}
 
 	/* Now process the configuration blocks. */
 	while (count < mpc->length) {


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [patch 1/4] x86/cpu: Ensure that CPU info updates are propagated on UP
  2024-03-22 18:56 ` [patch 1/4] x86/cpu: Ensure that CPU info updates are propagated on UP Thomas Gleixner
@ 2024-03-22 21:41   ` Guenter Roeck
  2024-03-23 13:24   ` [tip: x86/urgent] " tip-bot2 for Thomas Gleixner
  1 sibling, 0 replies; 13+ messages in thread
From: Guenter Roeck @ 2024-03-22 21:41 UTC (permalink / raw)
  To: Thomas Gleixner; +Cc: LKML, x86, Linus Torvalds

On Fri, Mar 22, 2024 at 07:56:35PM +0100, Thomas Gleixner wrote:
> The boot sequence evaluates CPUID information twice:
> 
>   1) During early boot
> 
>   2) When finalizing the early setup right before
>      mitigations are selected and alternatives are patched.
> 
> In both cases the evaluation is stored in boot_cpu_data, but on UP the
> copying of boot_cpu_data to the per CPU info of the boot CPU happens
> between #1 and #2. So any update which happens in #2 is never propagated to
> the per CPU info instance.
> 
> Consolidate the whole logic and copy boot_cpu_data right before applying
> alternatives as that's the point where boot_cpu_data is in it's final state
> and not supposed to change anymore.
> 
> This also removes the voodoo mb() from smp_prepare_cpus_common() which had
> absolutely no purpose.
> 
> Fixes: 71eb4893cfaf ("x86/percpu: Cure per CPU madness on UP")
> Reported-by: Guenter Roeck <linux@roeck-us.net>
> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>

Tested-by: Guenter Roeck <linux@roeck-us.net>

Guenter

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [patch 2/4] x86/topology: Dont evaluate logical IDs during early boot
  2024-03-22 18:56 ` [patch 2/4] x86/topology: Dont evaluate logical IDs during early boot Thomas Gleixner
@ 2024-03-22 21:41   ` Guenter Roeck
  2024-03-23 13:24   ` [tip: x86/urgent] x86/topology: Don't " tip-bot2 for Thomas Gleixner
  1 sibling, 0 replies; 13+ messages in thread
From: Guenter Roeck @ 2024-03-22 21:41 UTC (permalink / raw)
  To: Thomas Gleixner; +Cc: LKML, x86, Linus Torvalds

On Fri, Mar 22, 2024 at 07:56:36PM +0100, Thomas Gleixner wrote:
> The local APICs have not yet been enumerated so the logical ID evaluation
> from the topology bitmaps does not work and would return an error code.
> 
> Skip the evaluation during the early boot CPUID evaluation and only apply
> it on the final run.
> 
> Fixes: 380414be78bf ("x86/cpu/topology: Use topology logical mapping mechanism")
> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>

Tested-by: Guenter Roeck <linux@roeck-us.net>

Guenter

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [patch 3/4] x86/topology: Handle the !APIC case gracefully
  2024-03-22 18:56 ` [patch 3/4] x86/topology: Handle the !APIC case gracefully Thomas Gleixner
@ 2024-03-22 21:41   ` Guenter Roeck
  2024-03-23 13:24   ` [tip: x86/urgent] " tip-bot2 for Thomas Gleixner
  1 sibling, 0 replies; 13+ messages in thread
From: Guenter Roeck @ 2024-03-22 21:41 UTC (permalink / raw)
  To: Thomas Gleixner; +Cc: LKML, x86, Linus Torvalds

On Fri, Mar 22, 2024 at 07:56:38PM +0100, Thomas Gleixner wrote:
> If there is no local APIC enumerated and registered then the topology
> bitmaps are empty. Therefore topology_init_possible_cpus() will die with a
> division by zero exception.
> 
> Prevent this by registering a fake APIC id to populate the topology
> bitmap. This also allows to use all topology query interfaces
> unconditionally. It does not affect the actual APIC code because either the
> local APIC address was not registered or no local APIC could be detected.
> 
> Fixes: f1f758a80516 ("x86/topology: Add a mechanism to track topology via APIC IDs")
> Reported-by: Guenter Roeck <linux@roeck-us.net>
> Reported-by: Linus Torvalds <torvalds@linux-foundation.org>
> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>

Tested-by: Guenter Roeck <linux@roeck-us.net>

Guenter

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [patch 4/4] x86/mpparse: Register APIC address only once
  2024-03-22 18:56 ` [patch 4/4] x86/mpparse: Register APIC address only once Thomas Gleixner
@ 2024-03-22 21:41   ` Guenter Roeck
  2024-03-23 13:24   ` [tip: x86/urgent] " tip-bot2 for Thomas Gleixner
  1 sibling, 0 replies; 13+ messages in thread
From: Guenter Roeck @ 2024-03-22 21:41 UTC (permalink / raw)
  To: Thomas Gleixner; +Cc: LKML, x86, Linus Torvalds

On Fri, Mar 22, 2024 at 07:56:39PM +0100, Thomas Gleixner wrote:
> The APIC address is registered twice. First during the early detection and
> afterwards when actually scanning the table for APIC IDs. The APIC and
> topology core warn about the second attempt.
> 
> Restrict it to the early detection call.
> 
> Fixes: 81287ad65da5 ("x86/apic: Sanitize APIC address setup")
> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>

Tested-by: Guenter Roeck <linux@roeck-us.net>

Guenter

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [tip: x86/urgent] x86/mpparse: Register APIC address only once
  2024-03-22 18:56 ` [patch 4/4] x86/mpparse: Register APIC address only once Thomas Gleixner
  2024-03-22 21:41   ` Guenter Roeck
@ 2024-03-23 13:24   ` tip-bot2 for Thomas Gleixner
  1 sibling, 0 replies; 13+ messages in thread
From: tip-bot2 for Thomas Gleixner @ 2024-03-23 13:24 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Thomas Gleixner, Borislav Petkov (AMD), Guenter Roeck, x86, linux-kernel

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     f2208aa12c27bfada3c15c550c03ca81d42dcac2
Gitweb:        https://git.kernel.org/tip/f2208aa12c27bfada3c15c550c03ca81d42dcac2
Author:        Thomas Gleixner <tglx@linutronix.de>
AuthorDate:    Fri, 22 Mar 2024 19:56:39 +01:00
Committer:     Borislav Petkov (AMD) <bp@alien8.de>
CommitterDate: Sat, 23 Mar 2024 12:41:48 +01:00

x86/mpparse: Register APIC address only once

The APIC address is registered twice. First during the early detection and
afterwards when actually scanning the table for APIC IDs. The APIC and
topology core warn about the second attempt.

Restrict it to the early detection call.

Fixes: 81287ad65da5 ("x86/apic: Sanitize APIC address setup")
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Tested-by: Guenter Roeck <linux@roeck-us.net>
Link: https://lore.kernel.org/r/20240322185305.297774848@linutronix.de
---
 arch/x86/kernel/mpparse.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kernel/mpparse.c b/arch/x86/kernel/mpparse.c
index 1ccd30c..e89171b 100644
--- a/arch/x86/kernel/mpparse.c
+++ b/arch/x86/kernel/mpparse.c
@@ -197,12 +197,12 @@ static int __init smp_read_mpc(struct mpc_table *mpc, unsigned early)
 	if (!smp_check_mpc(mpc, oem, str))
 		return 0;
 
-	/* Initialize the lapic mapping */
-	if (!acpi_lapic)
-		register_lapic_address(mpc->lapic);
-
-	if (early)
+	if (early) {
+		/* Initialize the lapic mapping */
+		if (!acpi_lapic)
+			register_lapic_address(mpc->lapic);
 		return 1;
+	}
 
 	/* Now process the configuration blocks. */
 	while (count < mpc->length) {

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [tip: x86/urgent] x86/topology: Handle the !APIC case gracefully
  2024-03-22 18:56 ` [patch 3/4] x86/topology: Handle the !APIC case gracefully Thomas Gleixner
  2024-03-22 21:41   ` Guenter Roeck
@ 2024-03-23 13:24   ` tip-bot2 for Thomas Gleixner
  1 sibling, 0 replies; 13+ messages in thread
From: tip-bot2 for Thomas Gleixner @ 2024-03-23 13:24 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Guenter Roeck, Linus Torvalds, Thomas Gleixner,
	Borislav Petkov (AMD),
	x86, linux-kernel

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     5e25eb25dae9fa0700bbe42aff0e2f105fcd096a
Gitweb:        https://git.kernel.org/tip/5e25eb25dae9fa0700bbe42aff0e2f105fcd096a
Author:        Thomas Gleixner <tglx@linutronix.de>
AuthorDate:    Fri, 22 Mar 2024 19:56:38 +01:00
Committer:     Borislav Petkov (AMD) <bp@alien8.de>
CommitterDate: Sat, 23 Mar 2024 12:35:56 +01:00

x86/topology: Handle the !APIC case gracefully

If there is no local APIC enumerated and registered then the topology
bitmaps are empty. Therefore, topology_init_possible_cpus() will die with
a division by zero exception.

Prevent this by registering a fake APIC id to populate the topology
bitmap. This also allows to use all topology query interfaces
unconditionally. It does not affect the actual APIC code because either
the local APIC address was not registered or no local APIC could be
detected.

Fixes: f1f758a80516 ("x86/topology: Add a mechanism to track topology via APIC IDs")
Reported-by: Guenter Roeck <linux@roeck-us.net>
Reported-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Tested-by: Guenter Roeck <linux@roeck-us.net>
Link: https://lore.kernel.org/r/20240322185305.242709302@linutronix.de
---
 arch/x86/kernel/cpu/topology.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/arch/x86/kernel/cpu/topology.c b/arch/x86/kernel/cpu/topology.c
index 3259b1d..aaca8d2 100644
--- a/arch/x86/kernel/cpu/topology.c
+++ b/arch/x86/kernel/cpu/topology.c
@@ -415,6 +415,17 @@ void __init topology_init_possible_cpus(void)
 	unsigned int total = assigned + disabled;
 	u32 apicid, firstid;
 
+	/*
+	 * If there was no APIC registered, then fake one so that the
+	 * topology bitmap is populated. That ensures that the code below
+	 * is valid and the various query interfaces can be used
+	 * unconditionally. This does not affect the actual APIC code in
+	 * any way because either the local APIC address has not been
+	 * registered or the local APIC was disabled on the command line.
+	 */
+	if (topo_info.boot_cpu_apic_id == BAD_APICID)
+		topology_register_boot_apic(0);
+
 	if (!restrict_to_up()) {
 		if (WARN_ON_ONCE(assigned > nr_cpu_ids)) {
 			disabled += assigned - nr_cpu_ids;

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [tip: x86/urgent] x86/topology: Don't evaluate logical IDs during early boot
  2024-03-22 18:56 ` [patch 2/4] x86/topology: Dont evaluate logical IDs during early boot Thomas Gleixner
  2024-03-22 21:41   ` Guenter Roeck
@ 2024-03-23 13:24   ` tip-bot2 for Thomas Gleixner
  1 sibling, 0 replies; 13+ messages in thread
From: tip-bot2 for Thomas Gleixner @ 2024-03-23 13:24 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Thomas Gleixner, Borislav Petkov (AMD), Guenter Roeck, x86, linux-kernel

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     7af541cee1e0eb48c6eb439bc6309175339fa96f
Gitweb:        https://git.kernel.org/tip/7af541cee1e0eb48c6eb439bc6309175339fa96f
Author:        Thomas Gleixner <tglx@linutronix.de>
AuthorDate:    Fri, 22 Mar 2024 19:56:36 +01:00
Committer:     Borislav Petkov (AMD) <bp@alien8.de>
CommitterDate: Sat, 23 Mar 2024 12:28:06 +01:00

x86/topology: Don't evaluate logical IDs during early boot

The local APICs have not yet been enumerated so the logical ID evaluation
from the topology bitmaps does not work and would return an error code.

Skip the evaluation during the early boot CPUID evaluation and only apply
it on the final run.

Fixes: 380414be78bf ("x86/cpu/topology: Use topology logical mapping mechanism")
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Tested-by: Guenter Roeck <linux@roeck-us.net>
Link: https://lore.kernel.org/r/20240322185305.186943142@linutronix.de
---
 arch/x86/kernel/cpu/topology_common.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kernel/cpu/topology_common.c b/arch/x86/kernel/cpu/topology_common.c
index a50ae8d..9a6069e 100644
--- a/arch/x86/kernel/cpu/topology_common.c
+++ b/arch/x86/kernel/cpu/topology_common.c
@@ -140,7 +140,7 @@ static void parse_topology(struct topo_scan *tscan, bool early)
 	}
 }
 
-static void topo_set_ids(struct topo_scan *tscan)
+static void topo_set_ids(struct topo_scan *tscan, bool early)
 {
 	struct cpuinfo_x86 *c = tscan->c;
 	u32 apicid = c->topo.apicid;
@@ -148,8 +148,10 @@ static void topo_set_ids(struct topo_scan *tscan)
 	c->topo.pkg_id = topo_shift_apicid(apicid, TOPO_PKG_DOMAIN);
 	c->topo.die_id = topo_shift_apicid(apicid, TOPO_DIE_DOMAIN);
 
-	c->topo.logical_pkg_id = topology_get_logical_id(apicid, TOPO_PKG_DOMAIN);
-	c->topo.logical_die_id = topology_get_logical_id(apicid, TOPO_DIE_DOMAIN);
+	if (!early) {
+		c->topo.logical_pkg_id = topology_get_logical_id(apicid, TOPO_PKG_DOMAIN);
+		c->topo.logical_die_id = topology_get_logical_id(apicid, TOPO_DIE_DOMAIN);
+	}
 
 	/* Package relative core ID */
 	c->topo.core_id = (apicid & topo_domain_mask(TOPO_PKG_DOMAIN)) >>
@@ -187,7 +189,7 @@ void cpu_parse_topology(struct cpuinfo_x86 *c)
 		       tscan.dom_shifts[dom], x86_topo_system.dom_shifts[dom]);
 	}
 
-	topo_set_ids(&tscan);
+	topo_set_ids(&tscan, false);
 }
 
 void __init cpu_init_topology(struct cpuinfo_x86 *c)
@@ -208,7 +210,7 @@ void __init cpu_init_topology(struct cpuinfo_x86 *c)
 		x86_topo_system.dom_size[dom] = 1U << sft;
 	}
 
-	topo_set_ids(&tscan);
+	topo_set_ids(&tscan, true);
 
 	/*
 	 * AMD systems have Nodes per package which cannot be mapped to

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [tip: x86/urgent] x86/cpu: Ensure that CPU info updates are propagated on UP
  2024-03-22 18:56 ` [patch 1/4] x86/cpu: Ensure that CPU info updates are propagated on UP Thomas Gleixner
  2024-03-22 21:41   ` Guenter Roeck
@ 2024-03-23 13:24   ` tip-bot2 for Thomas Gleixner
  1 sibling, 0 replies; 13+ messages in thread
From: tip-bot2 for Thomas Gleixner @ 2024-03-23 13:24 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Guenter Roeck, Thomas Gleixner, Borislav Petkov (AMD), x86, linux-kernel

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     c90399fbd74a0713d5972a6d931e4a9918621e88
Gitweb:        https://git.kernel.org/tip/c90399fbd74a0713d5972a6d931e4a9918621e88
Author:        Thomas Gleixner <tglx@linutronix.de>
AuthorDate:    Fri, 22 Mar 2024 19:56:35 +01:00
Committer:     Borislav Petkov (AMD) <bp@alien8.de>
CommitterDate: Sat, 23 Mar 2024 12:22:04 +01:00

x86/cpu: Ensure that CPU info updates are propagated on UP

The boot sequence evaluates CPUID information twice:

  1) During early boot

  2) When finalizing the early setup right before
     mitigations are selected and alternatives are patched.

In both cases the evaluation is stored in boot_cpu_data, but on UP the
copying of boot_cpu_data to the per CPU info of the boot CPU happens
between #1 and #2. So any update which happens in #2 is never propagated to
the per CPU info instance.

Consolidate the whole logic and copy boot_cpu_data right before applying
alternatives as that's the point where boot_cpu_data is in it's final
state and not supposed to change anymore.

This also removes the voodoo mb() from smp_prepare_cpus_common() which
had absolutely no purpose.

Fixes: 71eb4893cfaf ("x86/percpu: Cure per CPU madness on UP")
Reported-by: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Tested-by: Guenter Roeck <linux@roeck-us.net>
Link: https://lore.kernel.org/r/20240322185305.127642785@linutronix.de
---
 arch/x86/kernel/cpu/common.c |  9 +++++++++
 arch/x86/kernel/setup.c      | 10 ----------
 arch/x86/kernel/smpboot.c    | 32 +++++---------------------------
 3 files changed, 14 insertions(+), 37 deletions(-)

diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index ba8cf5e..5c1e6d6 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -2307,6 +2307,8 @@ void arch_smt_update(void)
 
 void __init arch_cpu_finalize_init(void)
 {
+	struct cpuinfo_x86 *c = this_cpu_ptr(&cpu_info);
+
 	identify_boot_cpu();
 
 	select_idle_routine();
@@ -2345,6 +2347,13 @@ void __init arch_cpu_finalize_init(void)
 	fpu__init_system();
 	fpu__init_cpu();
 
+	/*
+	 * Ensure that access to the per CPU representation has the initial
+	 * boot CPU configuration.
+	 */
+	*c = boot_cpu_data;
+	c->initialized = true;
+
 	alternative_instructions();
 
 	if (IS_ENABLED(CONFIG_X86_64)) {
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
index 3e1e96e..ef20650 100644
--- a/arch/x86/kernel/setup.c
+++ b/arch/x86/kernel/setup.c
@@ -1206,16 +1206,6 @@ void __init i386_reserve_resources(void)
 
 #endif /* CONFIG_X86_32 */
 
-#ifndef CONFIG_SMP
-void __init smp_prepare_boot_cpu(void)
-{
-	struct cpuinfo_x86 *c = &cpu_data(0);
-
-	*c = boot_cpu_data;
-	c->initialized = true;
-}
-#endif
-
 static struct notifier_block kernel_offset_notifier = {
 	.notifier_call = dump_kernel_offset
 };
diff --git a/arch/x86/kernel/smpboot.c b/arch/x86/kernel/smpboot.c
index fe355c8..76bb650 100644
--- a/arch/x86/kernel/smpboot.c
+++ b/arch/x86/kernel/smpboot.c
@@ -313,14 +313,6 @@ static void notrace start_secondary(void *unused)
 	cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
 }
 
-static void __init smp_store_boot_cpu_info(void)
-{
-	struct cpuinfo_x86 *c = &cpu_data(0);
-
-	*c = boot_cpu_data;
-	c->initialized = true;
-}
-
 /*
  * The bootstrap kernel entry code has set these up. Save them for
  * a given CPU
@@ -1039,29 +1031,15 @@ static __init void disable_smp(void)
 	cpumask_set_cpu(0, topology_die_cpumask(0));
 }
 
-static void __init smp_cpu_index_default(void)
-{
-	int i;
-	struct cpuinfo_x86 *c;
-
-	for_each_possible_cpu(i) {
-		c = &cpu_data(i);
-		/* mark all to hotplug */
-		c->cpu_index = nr_cpu_ids;
-	}
-}
-
 void __init smp_prepare_cpus_common(void)
 {
 	unsigned int i;
 
-	smp_cpu_index_default();
-
-	/*
-	 * Setup boot CPU information
-	 */
-	smp_store_boot_cpu_info(); /* Final full version of the data */
-	mb();
+	/* Mark all except the boot CPU as hotpluggable */
+	for_each_possible_cpu(i) {
+		if (i)
+			per_cpu(cpu_info.cpu_index, i) = nr_cpu_ids;
+	}
 
 	for_each_possible_cpu(i) {
 		zalloc_cpumask_var(&per_cpu(cpu_sibling_map, i), GFP_KERNEL);

^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2024-03-23 13:24 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-22 18:56 [patch 0/4] x86/topology: Cure fallout Thomas Gleixner
2024-03-22 18:56 ` [patch 1/4] x86/cpu: Ensure that CPU info updates are propagated on UP Thomas Gleixner
2024-03-22 21:41   ` Guenter Roeck
2024-03-23 13:24   ` [tip: x86/urgent] " tip-bot2 for Thomas Gleixner
2024-03-22 18:56 ` [patch 2/4] x86/topology: Dont evaluate logical IDs during early boot Thomas Gleixner
2024-03-22 21:41   ` Guenter Roeck
2024-03-23 13:24   ` [tip: x86/urgent] x86/topology: Don't " tip-bot2 for Thomas Gleixner
2024-03-22 18:56 ` [patch 3/4] x86/topology: Handle the !APIC case gracefully Thomas Gleixner
2024-03-22 21:41   ` Guenter Roeck
2024-03-23 13:24   ` [tip: x86/urgent] " tip-bot2 for Thomas Gleixner
2024-03-22 18:56 ` [patch 4/4] x86/mpparse: Register APIC address only once Thomas Gleixner
2024-03-22 21:41   ` Guenter Roeck
2024-03-23 13:24   ` [tip: x86/urgent] " tip-bot2 for Thomas Gleixner

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.