Skip to contentSkip to conversation

cpu: Add missing check to cpuhp_smt_enable()

PATCH In review Rafael J. Wysocki 5 participants 17 messages Aug 29, 2025 – Sep 22, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpu: Add missing check to cpuhp_smt_enable()

From: Rafael J. Wysocki <[email protected]>

Christian has reported that commit a430c11f4015 ("intel_idle: Rescan "dead" SMT siblings during initialization") broke the use case in which both nosmt and maxcpus were added to the kernel command line because it caused CPUs that were not SMT siblings to be brought online during the intel_idle driver initialization in violation of the maxcpus limit.

The underlying reason for this is a missing topology_is_primary_thread() check in cpuhp_smt_enable() which causes that function to put online more CPUs than just the SMT siblings that it is supposed to handle.

Add the missing check to address the issue.

Fixes a430c11f4015 ("intel_idle: Rescan "dead" SMT siblings during initialization")
Fixes f694481b1d31 ("ACPI: processor: Rescan "dead" SMT siblings during initialization")
Cc 6.16+ <[email protected]> # 6.16+
---
 kernel/cpu.c |    6 ++++++
 1 file changed, 6 insertions(+)
kernel/cpu.c +6 -0
@@ -2710,6 +2710,12 @@
 	cpu_maps_update_begin();
 	cpu_smt_control = CPU_SMT_ENABLED;
 	for_each_present_cpu(cpu) {
+		/*
+		 * Avoid accidentally onlining primary thread CPUs that have
+		 * been taken offline.
+		 */
+		if (topology_is_primary_thread(cpu))
+			continue;
 		/* Skip online CPUs and CPUs on offline nodes */
 		if (cpu_online(cpu) || !node_online(cpu_to_node(cpu)))
 			continue;
#1 lore raw
Quoted 16 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Christian has reported that commit a430c11f4015 ("intel_idle: Rescan "dead" SMT siblings during initialization") broke the use case in which both nosmt and maxcpus were added to the kernel command line because it caused CPUs that were not SMT siblings to be brought online during the intel_idle driver initialization in violation of the maxcpus limit.

The underlying reason for this is a missing topology_is_primary_thread() check in cpuhp_smt_enable() which causes that function to put online more CPUs than just the SMT siblings that it is supposed to handle.

Add the missing check to address the issue.

Quoted 7 lines
Fixes a430c11f4015 ("intel_idle: Rescan "dead" SMT siblings during initialization")
Fixes f694481b1d31 ("ACPI: processor: Rescan "dead" SMT siblings during initialization")
Cc 6.16+ <[email protected]> # 6.16+

I'm wondering if there are any objections or concerns.

Since this is a regression in 6.16, it would be good to get it fixed in 6.17-rc.

Quoted 23 lines
---
 kernel/cpu.c |    6 ++++++
 1 file changed, 6 insertions(+)
kernel/cpu.c +6 -0
@@ -2710,6 +2710,12 @@
        cpu_maps_update_begin();
        cpu_smt_control = CPU_SMT_ENABLED;
        for_each_present_cpu(cpu) {
+               /*
+                * Avoid accidentally onlining primary thread CPUs that have
+                * been taken offline.
+                */
+               if (topology_is_primary_thread(cpu))
+                       continue;
                /* Skip online CPUs and CPUs on offline nodes */
                if (cpu_online(cpu) || !node_online(cpu_to_node(cpu)))
                        continue;
#2 lore raw
Quoted 4 lines · Rafael J. Wysocki
Quoted 3 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]> Christian has reported that commit a430c11f4015 ("intel_idle: Rescan "dead" SMT siblings during initialization") broke the use case in

Does "dead" here mean present but offline?

Quoted 4 lines
Quoted 4 lines

which both nosmt and maxcpus were added to the kernel command line because it caused CPUs that were not SMT siblings to be brought online during the intel_idle driver initialization in violation of the maxcpus limit.

How does intel_idle fit in here? I don't immediately see it calling cpuhp_smt_enable().

Quoted 5 lines
Quoted 5 lines

The underlying reason for this is a missing topology_is_primary_thread() check in cpuhp_smt_enable() which causes that function to put online more CPUs than just the SMT siblings that it is supposed to handle.

Add the missing check to address the issue.

We should probably add a bit more checking in cpuhp_smt_enable() to make sure that it's being called under expected conditions like a:

	WARN_ON_ONCE(cpu_smt_control != CPU_SMT_DISABLED);

and probably also some comments about how it is expected to work.

cpuhp_smt_enable() doesn't _really_ enable SMT. It specifically takes it from DISABLED=>ENABLED. Thinking about it in that context, enabling _just_ the secondary (disabled) threads makes a lot of sense.

But that can come later, after the bug fix.

Quoted 3 lines

kernel/cpu.c

Quoted 3 lines
kernel/cpu.c +0 -0
@@ -2710,6 +2710,12 @@

No 'diff -p', eh?

Quoted 4 lines
Quoted 4 lines
kernel/cpu.c +1 -0
        cpu_maps_update_begin();
        cpu_smt_control = CPU_SMT_ENABLED;
        for_each_present_cpu(cpu) {
+               /*
Quoted 8 lines

kernel/cpu.c line 2721

Quoted 8 lines
kernel/cpu.c +5 -0
+                * Avoid accidentally onlining primary thread CPUs that have
+                * been taken offline.
+                */
+               if (topology_is_primary_thread(cpu))
+                       continue;
                /* Skip online CPUs and CPUs on offline nodes */
                if (cpu_online(cpu) || !node_online(cpu_to_node(cpu)))
                        continue;

Is there a more generic problem with this not respecting 'maxcpus'? If maxcpus had forced a primary thread offline, this would still online the secondary thread, even with the fix. Taking _that_ online might still bring you over the maxcpus limit.

#3 lore raw
Quoted 8 lines · Dave Hansen
Quoted 4 lines · Rafael J. Wysocki
Quoted 3 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]> Christian has reported that commit a430c11f4015 ("intel_idle: Rescan "dead" SMT siblings during initialization") broke the use case in

Does "dead" here mean present but offline?

Yes.

Quoted 7 lines
Quoted 4 lines
Quoted 4 lines

which both nosmt and maxcpus were added to the kernel command line because it caused CPUs that were not SMT siblings to be brought online during the intel_idle driver initialization in violation of the maxcpus limit.

How does intel_idle fit in here? I don't immediately see it calling cpuhp_smt_enable().

It calls arch_cpu_rescan_dead_smt_siblings() which calls cpuhp_smt_enable().

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

The underlying reason for this is a missing topology_is_primary_thread() check in cpuhp_smt_enable() which causes that function to put online more CPUs than just the SMT siblings that it is supposed to handle.

Quoted 8 lines
Quoted 1 line
Quoted 1 line

Add the missing check to address the issue.

We should probably add a bit more checking in cpuhp_smt_enable() to make sure that it's being called under expected conditions like a:

        WARN_ON_ONCE(cpu_smt_control != CPU_SMT_DISABLED);

and probably also some comments about how it is expected to work.

Well, see the callers. Two out of three take care of this already and the third one doesn't care.

Quoted 11 lines

cpuhp_smt_enable() doesn't _really_ enable SMT. It specifically takes it from DISABLED=>ENABLED. Thinking about it in that context, enabling _just_ the secondary (disabled) threads makes a lot of sense.

But that can come later, after the bug fix.

Quoted 3 lines

kernel/cpu.c

Quoted 3 lines
kernel/cpu.c +0 -0
@@ -2710,6 +2710,12 @@

No 'diff -p', eh?

Ah, sorry about this.

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
kernel/cpu.c +1 -0
        cpu_maps_update_begin();
        cpu_smt_control = CPU_SMT_ENABLED;
        for_each_present_cpu(cpu) {
+               /*
Quoted 8 lines

kernel/cpu.c line 2721

Quoted 8 lines
kernel/cpu.c +5 -0
+                * Avoid accidentally onlining primary thread CPUs that have
+                * been taken offline.
+                */
+               if (topology_is_primary_thread(cpu))
+                       continue;
                /* Skip online CPUs and CPUs on offline nodes */
                if (cpu_online(cpu) || !node_online(cpu_to_node(cpu)))
                        continue;
Quoted 4 lines

Is there a more generic problem with this not respecting 'maxcpus'? If maxcpus had forced a primary thread offline, this would still online the secondary thread, even with the fix. Taking _that_ online might still bring you over the maxcpus limit.

Yes, there is AFAICS, but it has been there for some time and it doesn't affect the rescan during boot.

For the rescan, it is actually useful to not respect maxcpus because it allows all of the SMT siblings to be kicked, but for run-time enabling of SMT it may be sort of a problem.

This change simply addresses the regression, which is that cores without SMT become online quite surprisingly after intel_idle (and ACPI idle too for that matter) initialization, due to arch_cpu_rescan_dead_smt_siblings().

#6 lore raw
In reply to Rafael J. Wysocki

kernel/cpu.c line 2718

Quoted 12 lines · Rafael J. Wysocki
kernel/cpu.c +6 -0
@@ -2710,6 +2710,12 @@
 	cpu_maps_update_begin();
 	cpu_smt_control = CPU_SMT_ENABLED;
 	for_each_present_cpu(cpu) {
+		/*
+		 * Avoid accidentally onlining primary thread CPUs that have
+		 * been taken offline.
+		 */
+		if (topology_is_primary_thread(cpu))
+			continue;

Hmm. That's kinda solving the problem, but I think the proper solution would be to implement topology_is_core_online() for x86.

The above is preventing the primary thread to be onlined, but then onlines the corresponding hyperthread, which does not really make sense.

Something like the completely untested below.

Thanks,

tglx
---

arch/x86/include/asm/topology.h +10 -0
index 6c79ee7c0957..21041898157a 100644
@@ -231,6 +231,16 @@ static inline bool topology_is_primary_thread(unsigned int cpu)
 }
 #define topology_is_primary_thread topology_is_primary_thread
 
+int topology_get_primary_thread(unsigned int cpu);
+
+static inline bool topology_is_core_online(unsigned int cpu)
+{
+	int pcpu = topology_get_primary_thread(cpu);
+
+	return pcpu >= 0 ? cpu_online(pcpu) : false;
+}
+#define topology_is_core_online topology_is_core_online
+
 #else /* CONFIG_SMP */
 static inline int topology_phys_to_logical_pkg(unsigned int pkg) { return 0; }
 static inline int topology_max_smt_threads(void) { return 1; }
arch/x86/kernel/cpu/topology.c +13 -0
index e35ccdc84910..6073a16628f9 100644
@@ -372,6 +372,19 @@ unsigned int topology_unit_count(u32 apicid, enum x86_topology_domains which_uni
 	return topo_unit_count(lvlid, at_level, apic_maps[which_units].map);
 }
 
+#ifdef CONFIG_SMP
+int topology_get_primary_thread(unsigned int cpu)
+{
+	u32 apic_id = cpuid_to_apicid[cpu];
+
+	/*
+	 * Get the core domain level APIC id, which is the primary thread
+	 * and return the CPU number assigned to it.
+	 */
+	return topo_lookup_cpuid(topo_apicid(apic_id, TOPO_CORE_DOMAIN));
+}
+#endif
+
 #ifdef CONFIG_ACPI_HOTPLUG_CPU
 /**
  * topology_hotplug_apic - Handle a physical hotplugged APIC after boot
#7 lore raw
Quoted 18 lines · Thomas Gleixner

kernel/cpu.c line 2718

Quoted 12 lines · Rafael J. Wysocki
kernel/cpu.c +6 -0
@@ -2710,6 +2710,12 @@
      cpu_maps_update_begin();
      cpu_smt_control = CPU_SMT_ENABLED;
      for_each_present_cpu(cpu) {
+             /*
+              * Avoid accidentally onlining primary thread CPUs that have
+              * been taken offline.
+              */
+             if (topology_is_primary_thread(cpu))
+                     continue;

Hmm. That's kinda solving the problem, but I think the proper solution would be to implement topology_is_core_online() for x86.

Quoted 2 lines

The above is preventing the primary thread to be onlined, but then onlines the corresponding hyperthread, which does not really make sense.

Well, manual online can be used for onlining the secondary thread of a core where the primary thread is offline, so this is technically possible already.

Quoted 1 line

Something like the completely untested below.

So given the above, shouldn't topology_is_core_online() check if any thread in the given core is online?

Quoted 48 lines

---

arch/x86/include/asm/topology.h +10 -0
index 6c79ee7c0957..21041898157a 100644
@@ -231,6 +231,16 @@ static inline bool topology_is_primary_thread(unsigned int cpu)
 }
 #define topology_is_primary_thread topology_is_primary_thread
 
+int topology_get_primary_thread(unsigned int cpu);
+
+static inline bool topology_is_core_online(unsigned int cpu)
+{
+       int pcpu = topology_get_primary_thread(cpu);
+
+       return pcpu >= 0 ? cpu_online(pcpu) : false;
+}
+#define topology_is_core_online topology_is_core_online
+
 #else /* CONFIG_SMP */
 static inline int topology_phys_to_logical_pkg(unsigned int pkg) { return 0; }
 static inline int topology_max_smt_threads(void) { return 1; }
arch/x86/kernel/cpu/topology.c +13 -0
index e35ccdc84910..6073a16628f9 100644
@@ -372,6 +372,19 @@ unsigned int topology_unit_count(u32 apicid, enum x86_topology_domains which_uni
        return topo_unit_count(lvlid, at_level, apic_maps[which_units].map);
 }
 
+#ifdef CONFIG_SMP
+int topology_get_primary_thread(unsigned int cpu)
+{
+       u32 apic_id = cpuid_to_apicid[cpu];
+
+       /*
+        * Get the core domain level APIC id, which is the primary thread
+        * and return the CPU number assigned to it.
+        */
+       return topo_lookup_cpuid(topo_apicid(apic_id, TOPO_CORE_DOMAIN));
+}
+#endif
+
 #ifdef CONFIG_ACPI_HOTPLUG_CPU
 /**
  * topology_hotplug_apic - Handle a physical hotplugged APIC after boot
#8 lore raw
Quoted 23 lines · Rafael J. Wysocki
Quoted 18 lines · Thomas Gleixner

kernel/cpu.c line 2718

Quoted 12 lines · Rafael J. Wysocki
kernel/cpu.c +6 -0
@@ -2710,6 +2710,12 @@
      cpu_maps_update_begin();
      cpu_smt_control = CPU_SMT_ENABLED;
      for_each_present_cpu(cpu) {
+             /*
+              * Avoid accidentally onlining primary thread CPUs that have
+              * been taken offline.
+              */
+             if (topology_is_primary_thread(cpu))
+                     continue;

Hmm. That's kinda solving the problem, but I think the proper solution would be to implement topology_is_core_online() for x86.

Quoted 2 lines

The above is preventing the primary thread to be onlined, but then onlines the corresponding hyperthread, which does not really make sense.

Quoted 8 lines

Well, manual online can be used for onlining the secondary thread of a core where the primary thread is offline, so this is technically possible already.

Quoted 1 line

Something like the completely untested below.

So given the above, shouldn't topology_is_core_online() check if any thread in the given core is online?

Besides, this would cause the siblings of offline SMT threads to be skipped while enabling SMT via sysfs (using /sys/devices/system/cpu/smt/control), but I'm not sure if this is the expectation in the field today. The current behavior is to online all secondary SMT threads (and more, but that part is quite arguably broken).

That's why I decided to use a simpler patch which doesn't go that far.

#9 lore raw
Quoted 10 lines · Rafael J. Wysocki
Quoted 8 lines · Rafael J. Wysocki

Well, manual online can be used for onlining the secondary thread of a core where the primary thread is offline, so this is technically possible already.

Quoted 1 line

Something like the completely untested below.

So given the above, shouldn't topology_is_core_online() check if any thread in the given core is online?

Quoted 6 lines

Besides, this would cause the siblings of offline SMT threads to be skipped while enabling SMT via sysfs (using /sys/devices/system/cpu/smt/control), but I'm not sure if this is the expectation in the field today. The current behavior is to online all secondary SMT threads (and more, but that part is quite arguably broken).

It is broken, because the initial logic is to bring up primary threads unconditionally and then refuse to bring up sibling threads.

With "maxcpus=xxx" this obviously limits the amount of primary threads, so there is arguably no point to online any of the related secondary threads of them.

The initial implementation was naively making that assumption, but the core check which was added due to PPC made this actually correct.

It just did not snap with me back then, but it's actually the correct thing to do, no?

Thanks,

tglx

#10 lore raw
Quoted 26 lines · Thomas Gleixner
Quoted 10 lines · Rafael J. Wysocki
Quoted 8 lines · Rafael J. Wysocki

Well, manual online can be used for onlining the secondary thread of a core where the primary thread is offline, so this is technically possible already.

Quoted 1 line

Something like the completely untested below.

So given the above, shouldn't topology_is_core_online() check if any thread in the given core is online?

Quoted 6 lines

Besides, this would cause the siblings of offline SMT threads to be skipped while enabling SMT via sysfs (using /sys/devices/system/cpu/smt/control), but I'm not sure if this is the expectation in the field today. The current behavior is to online all secondary SMT threads (and more, but that part is quite arguably broken).

It is broken, because the initial logic is to bring up primary threads unconditionally and then refuse to bring up sibling threads.

With "maxcpus=xxx" this obviously limits the amount of primary threads, so there is arguably no point to online any of the related secondary threads of them.

Quoted 5 lines

The initial implementation was naively making that assumption, but the core check which was added due to PPC made this actually correct.

It just did not snap with me back then, but it's actually the correct thing to do, no?

It would at least be consistent with the existing PPC behavior. :-)

#11 lore raw
Quoted 27 lines · Rafael J. Wysocki
Quoted 26 lines · Thomas Gleixner
Quoted 10 lines · Rafael J. Wysocki
Quoted 8 lines · Rafael J. Wysocki

Well, manual online can be used for onlining the secondary thread of a core where the primary thread is offline, so this is technically possible already.

Quoted 1 line

Something like the completely untested below.

So given the above, shouldn't topology_is_core_online() check if any thread in the given core is online?

Quoted 6 lines

Besides, this would cause the siblings of offline SMT threads to be skipped while enabling SMT via sysfs (using /sys/devices/system/cpu/smt/control), but I'm not sure if this is the expectation in the field today. The current behavior is to online all secondary SMT threads (and more, but that part is quite arguably broken).

It is broken, because the initial logic is to bring up primary threads unconditionally and then refuse to bring up sibling threads.

With "maxcpus=xxx" this obviously limits the amount of primary threads, so there is arguably no point to online any of the related secondary threads of them.

Quoted 7 lines
Quoted 5 lines

The initial implementation was naively making that assumption, but the core check which was added due to PPC made this actually correct.

It just did not snap with me back then, but it's actually the correct thing to do, no?

It would at least be consistent with the existing PPC behavior. :-)

Correct.

#12 lore raw
Quoted 32 lines · Thomas Gleixner
Quoted 30 lines · Rafael J. Wysocki
Quoted 29 lines · Thomas Gleixner
Quoted 10 lines · Rafael J. Wysocki
Quoted 8 lines · Rafael J. Wysocki

Well, manual online can be used for onlining the secondary thread of a core where the primary thread is offline, so this is technically possible already.

Quoted 1 line

Something like the completely untested below.

So given the above, shouldn't topology_is_core_online() check if any thread in the given core is online?

Quoted 6 lines

Besides, this would cause the siblings of offline SMT threads to be skipped while enabling SMT via sysfs (using /sys/devices/system/cpu/smt/control), but I'm not sure if this is the expectation in the field today. The current behavior is to online all secondary SMT threads (and more, but that part is quite arguably broken).

It is broken, because the initial logic is to bring up primary threads unconditionally and then refuse to bring up sibling threads.

With "maxcpus=xxx" this obviously limits the amount of primary threads, so there is arguably no point to online any of the related secondary threads of them.

The initial implementation was naively making that assumption, but the core check which was added due to PPC made this actually correct.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

It just did not snap with me back then, but it's actually the correct thing to do, no?

It would at least be consistent with the existing PPC behavior. :-)

Correct.

So are you going to send a patch or do you want me to do something?

From a user standpoint, this issue is a regression in 6.16, so it would be good to address it before final 6.17.

#13 lore raw

PATCH 1/1 x86/topology: Implement topology_is_core_online() to address SMT regression

Christian reported that commit a430c11f4015 ("intel_idle: Rescan "dead" SMT siblings during initialization") broke the use case in which both 'nosmt' and 'maxcpus' are on the kernel command line because it onlines primary threads, which were offline due to the maxcpus limit.

The initially proposed fix to skip primary threads in the loop is inconsistent. While it prevents the primary thread to be onlined, it then onlines the corresponding hyperthread(s), which does not really make sense.

The CPU iterator in cpuhp_smt_enable() contains a check which excludes all threads of a core, when the primary thread is offline. The default implementation is a NOOP and therefore not effective on x86.

Implement topology_is_core_online() on x86 to address this issue. This makes the behaviour consistent between x86 and PowerPC.

Fixes a430c11f4015 ("intel_idle: Rescan "dead" SMT siblings during initialization")
Fixes f694481b1d31 ("ACPI: processor: Rescan "dead" SMT siblings during initialization")
---
 arch/x86/include/asm/topology.h |   10 ++++++++++
 arch/x86/kernel/cpu/topology.c  |   13 +++++++++++++
 2 files changed, 23 insertions(+)
arch/x86/include/asm/topology.h +10 -0
@@ -231,6 +231,16 @@ static inline bool topology_is_primary_t
 }
 #define topology_is_primary_thread topology_is_primary_thread
 
+int topology_get_primary_thread(unsigned int cpu);
+
+static inline bool topology_is_core_online(unsigned int cpu)
+{
+	int pcpu = topology_get_primary_thread(cpu);
+
+	return pcpu >= 0 ? cpu_online(pcpu) : false;
+}
+#define topology_is_core_online topology_is_core_online
+
 #else /* CONFIG_SMP */
 static inline int topology_phys_to_logical_pkg(unsigned int pkg) { return 0; }
 static inline int topology_max_smt_threads(void) { return 1; }
arch/x86/kernel/cpu/topology.c +13 -0
@@ -372,6 +372,19 @@ unsigned int topology_unit_count(u32 api
 	return topo_unit_count(lvlid, at_level, apic_maps[which_units].map);
 }
 
+#ifdef CONFIG_SMP
+int topology_get_primary_thread(unsigned int cpu)
+{
+	u32 apic_id = cpuid_to_apicid[cpu];
+
+	/*
+	 * Get the core domain level APIC id, which is the primary thread
+	 * and return the CPU number assigned to it.
+	 */
+	return topo_lookup_cpuid(topo_apicid(apic_id, TOPO_CORE_DOMAIN));
+}
+#endif
+
 #ifdef CONFIG_ACPI_HOTPLUG_CPU
 /**
  * topology_hotplug_apic - Handle a physical hotplugged APIC after boot
Tested-by on v1 1/1
#14 lore raw

Re: [PATCH] x86/topology: Implement topology_is_core_online() to address SMT regression

Quoted 13 lines · Thomas Gleixner

Christian reported that commit a430c11f4015 ("intel_idle: Rescan "dead" SMT siblings during initialization") broke the use case in which both 'nosmt' and 'maxcpus' are on the kernel command line because it onlines primary threads, which were offline due to the maxcpus limit.

The initially proposed fix to skip primary threads in the loop is inconsistent. While it prevents the primary thread to be onlined, it then onlines the corresponding hyperthread(s), which does not really make sense.

The CPU iterator in cpuhp_smt_enable() contains a check which excludes all threads of a core, when the primary thread is offline. The default implementation is a NOOP and therefore not effective on x86.

Quoted 7 lines

Implement topology_is_core_online() on x86 to address this issue. This makes the behaviour consistent between x86 and PowerPC.

Fixes a430c11f4015 ("intel_idle: Rescan "dead" SMT siblings during initialization")
Fixes f694481b1d31 ("ACPI: processor: Rescan "dead" SMT siblings during initialization")
author Reviewed-by on v1 1/1
#15 lore raw

Re: [PATCH] x86/topology: Implement topology_is_core_online() to address SMT regression

In reply to Thomas Gleixner
Quoted 17 lines · Thomas Gleixner

Christian reported that commit a430c11f4015 ("intel_idle: Rescan "dead" SMT siblings during initialization") broke the use case in which both 'nosmt' and 'maxcpus' are on the kernel command line because it onlines primary threads, which were offline due to the maxcpus limit.

The initially proposed fix to skip primary threads in the loop is inconsistent. While it prevents the primary thread to be onlined, it then onlines the corresponding hyperthread(s), which does not really make sense.

The CPU iterator in cpuhp_smt_enable() contains a check which excludes all threads of a core, when the primary thread is offline. The default implementation is a NOOP and therefore not effective on x86.

Implement topology_is_core_online() on x86 to address this issue. This makes the behaviour consistent between x86 and PowerPC.

Quoted 7 lines
Fixes a430c11f4015 ("intel_idle: Rescan "dead" SMT siblings during initialization")
Fixes f694481b1d31 ("ACPI: processor: Rescan "dead" SMT siblings during initialization")
Quoted 46 lines
---
 arch/x86/include/asm/topology.h |   10 ++++++++++
 arch/x86/kernel/cpu/topology.c  |   13 +++++++++++++
 2 files changed, 23 insertions(+)
arch/x86/include/asm/topology.h +10 -0
@@ -231,6 +231,16 @@ static inline bool topology_is_primary_t
 }
 #define topology_is_primary_thread topology_is_primary_thread
 
+int topology_get_primary_thread(unsigned int cpu);
+
+static inline bool topology_is_core_online(unsigned int cpu)
+{
+       int pcpu = topology_get_primary_thread(cpu);
+
+       return pcpu >= 0 ? cpu_online(pcpu) : false;
+}
+#define topology_is_core_online topology_is_core_online
+
 #else /* CONFIG_SMP */
 static inline int topology_phys_to_logical_pkg(unsigned int pkg) { return 0; }
 static inline int topology_max_smt_threads(void) { return 1; }
arch/x86/kernel/cpu/topology.c +13 -0
@@ -372,6 +372,19 @@ unsigned int topology_unit_count(u32 api
        return topo_unit_count(lvlid, at_level, apic_maps[which_units].map);
 }
 
+#ifdef CONFIG_SMP
+int topology_get_primary_thread(unsigned int cpu)
+{
+       u32 apic_id = cpuid_to_apicid[cpu];
+
+       /*
+        * Get the core domain level APIC id, which is the primary thread
+        * and return the CPU number assigned to it.
+        */
+       return topo_lookup_cpuid(topo_apicid(apic_id, TOPO_CORE_DOMAIN));
+}
+#endif
+
 #ifdef CONFIG_ACPI_HOTPLUG_CPU
 /**
  * topology_hotplug_apic - Handle a physical hotplugged APIC after boot
#16 lore raw

[tip: x86/urgent] x86/topology: Implement topology_is_core_online() to address SMT regression

In reply to Thomas Gleixner
The following commit has been merged into the x86/urgent branch of tip:

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

Commit-ID:     2066f00e5b2dc061fb6d8c88fadaebc97f11feaa
Gitweb:        https://git.kernel.org/tip/2066f00e5b2dc061fb6d8c88fadaebc97f11feaa
Author:        Thomas Gleixner <[email protected]>
AuthorDate:    Sun, 21 Sep 2025 10:56:40 +02:00
Committer:     Borislav Petkov (AMD) <[email protected]>
CommitterDate: Mon, 22 Sep 2025 21:25:36 +02:00

x86/topology: Implement topology_is_core_online() to address SMT regression

Christian reported that commit a430c11f4015 ("intel_idle: Rescan "dead" SMT siblings during initialization") broke the use case in which both 'nosmt' and 'maxcpus' are on the kernel command line because it onlines primary threads, which were offline due to the maxcpus limit.

The initially proposed fix to skip primary threads in the loop is inconsistent. While it prevents the primary thread to be onlined, it then onlines the corresponding hyperthread(s), which does not really make sense.

The CPU iterator in cpuhp_smt_enable() contains a check which excludes all threads of a core, when the primary thread is offline. The default implementation is a NOOP and therefore not effective on x86.

Implement topology_is_core_online() on x86 to address this issue. This makes the behaviour consistent between x86 and PowerPC.

Fixes a430c11f4015 ("intel_idle: Rescan "dead" SMT siblings during initialization")
Fixes f694481b1d31 ("ACPI: processor: Rescan "dead" SMT siblings during initialization")
---
 arch/x86/include/asm/topology.h | 10 ++++++++++
 arch/x86/kernel/cpu/topology.c  | 13 +++++++++++++
 2 files changed, 23 insertions(+)
arch/x86/include/asm/topology.h +10 -0
index 6c79ee7..2104189 100644
@@ -231,6 +231,16 @@ static inline bool topology_is_primary_thread(unsigned int cpu)
 }
 #define topology_is_primary_thread topology_is_primary_thread
 
+int topology_get_primary_thread(unsigned int cpu);
+
+static inline bool topology_is_core_online(unsigned int cpu)
+{
+	int pcpu = topology_get_primary_thread(cpu);
+
+	return pcpu >= 0 ? cpu_online(pcpu) : false;
+}
+#define topology_is_core_online topology_is_core_online
+
 #else /* CONFIG_SMP */
 static inline int topology_phys_to_logical_pkg(unsigned int pkg) { return 0; }
 static inline int topology_max_smt_threads(void) { return 1; }
arch/x86/kernel/cpu/topology.c +13 -0
index e35ccdc..6073a16 100644
@@ -372,6 +372,19 @@ unsigned int topology_unit_count(u32 apicid, enum x86_topology_domains which_uni
 	return topo_unit_count(lvlid, at_level, apic_maps[which_units].map);
 }
 
+#ifdef CONFIG_SMP
+int topology_get_primary_thread(unsigned int cpu)
+{
+	u32 apic_id = cpuid_to_apicid[cpu];
+
+	/*
+	 * Get the core domain level APIC id, which is the primary thread
+	 * and return the CPU number assigned to it.
+	 */
+	return topo_lookup_cpuid(topo_apicid(apic_id, TOPO_CORE_DOMAIN));
+}
+#endif
+
 #ifdef CONFIG_ACPI_HOTPLUG_CPU
 /**
  * topology_hotplug_apic - Handle a physical hotplugged APIC after boot