Re: [RFC][PATCH 2/6] x86/topo: Add TOPO_NUMA_DOMAIN

From: K Prateek Nayak

Date: Fri Feb 27 2026 - 08:21:33 EST


Hello Peter,

On 2/26/2026 4:19 PM, Peter Zijlstra wrote:
> @@ -88,6 +89,14 @@ static inline u32 topo_apicid(u32 apicid
> {
> if (dom == TOPO_SMT_DOMAIN)
> return apicid;
> +#ifdef CONFIG_NUMA
> + if (dom == TOPO_NUMA_DOMAIN) {
> + int nid = __apicid_to_phys_node[apicid];
> + if (nid == NUMA_NO_NODE)
> + nid = 0;
> + return nid;
> + }
> +#endif

I'm not digging this override - simply because topo_apicid() was not
meant to handle these kinds of cases where we cannot derive a topology
ID by simply shifting and masking the APICID.

Looking at the series, all we need is an equivalent of:

domain_weight(TOPO_NUMA_DOMAIN)

so can we do something like the following on top of the changes in this
series:

(!CONFIG_NUMA has only been build tested)

diff --git a/arch/x86/include/asm/numa.h b/arch/x86/include/asm/numa.h
index 5d6da2ad84e5..05461e2cd931 100644
--- a/arch/x86/include/asm/numa.h
+++ b/arch/x86/include/asm/numa.h
@@ -53,6 +53,8 @@ extern void __init init_cpu_to_node(void);
extern void numa_add_cpu(unsigned int cpu);
extern void numa_remove_cpu(unsigned int cpu);
extern void init_gi_nodes(void);
+extern void __init topo_register_apic_phys_node(int apicid);
+extern int num_phys_nodes(void);
#else /* CONFIG_NUMA */
static inline void numa_set_node(int cpu, int node) { }
static inline void numa_clear_node(int cpu) { }
@@ -60,6 +62,11 @@ static inline void init_cpu_to_node(void) { }
static inline void numa_add_cpu(unsigned int cpu) { }
static inline void numa_remove_cpu(unsigned int cpu) { }
static inline void init_gi_nodes(void) { }
+static inline void __init topo_register_apic_phys_node(int apicid) { }
+static inline int num_phys_nodes(void)
+{
+ return 1;
+}
#endif /* CONFIG_NUMA */

#ifdef CONFIG_DEBUG_PER_CPU_MAPS
diff --git a/arch/x86/include/asm/topology.h b/arch/x86/include/asm/topology.h
index 7fe9ea4ee1e7..9b3f92c5f0e0 100644
--- a/arch/x86/include/asm/topology.h
+++ b/arch/x86/include/asm/topology.h
@@ -111,9 +111,6 @@ enum x86_topology_domains {
TOPO_DIE_DOMAIN,
TOPO_DIEGRP_DOMAIN,
TOPO_PKG_DOMAIN,
-#ifdef CONFIG_NUMA
- TOPO_NUMA_DOMAIN,
-#endif
TOPO_MAX_DOMAIN,
};

diff --git a/arch/x86/kernel/cpu/topology.c b/arch/x86/kernel/cpu/topology.c
index 399388213bc0..1d3bed3ae40e 100644
--- a/arch/x86/kernel/cpu/topology.c
+++ b/arch/x86/kernel/cpu/topology.c
@@ -89,14 +89,6 @@ static inline u32 topo_apicid(u32 apicid, enum x86_topology_domains dom)
{
if (dom == TOPO_SMT_DOMAIN)
return apicid;
-#ifdef CONFIG_NUMA
- if (dom == TOPO_NUMA_DOMAIN) {
- int nid = __apicid_to_phys_node[apicid];
- if (nid == NUMA_NO_NODE)
- nid = 0;
- return nid;
- }
-#endif
return apicid & (UINT_MAX << x86_topo_system.dom_shifts[dom - 1]);
}

@@ -254,6 +246,8 @@ static __init void topo_register_apic(u32 apic_id, u32 acpi_id, bool present)
*/
for (dom = TOPO_SMT_DOMAIN; dom < TOPO_MAX_DOMAIN; dom++)
set_bit(topo_apicid(apic_id, dom), apic_maps[dom].map);
+
+ topo_register_apic_phys_node(apic_id);
}

/**
@@ -501,7 +495,7 @@ void __init topology_init_possible_cpus(void)
set_nr_cpu_ids(allowed);

cnta = domain_weight(TOPO_PKG_DOMAIN);
- cntb = domain_weight(TOPO_NUMA_DOMAIN);
+ cntb = num_phys_nodes();

__num_nodes_per_package = DIV_ROUND_UP(cntb, cnta);

diff --git a/arch/x86/mm/numa.c b/arch/x86/mm/numa.c
index 4556a1561aa0..b60076745a32 100644
--- a/arch/x86/mm/numa.c
+++ b/arch/x86/mm/numa.c
@@ -52,6 +52,8 @@ s16 __apicid_to_phys_node[MAX_LOCAL_APIC] = {
[0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
};

+static nodemask_t apic_phys_node_map __ro_after_init;
+
int numa_cpu_node(int cpu)
{
u32 apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
@@ -61,6 +63,24 @@ int numa_cpu_node(int cpu)
return NUMA_NO_NODE;
}

+static int topo_apicid_to_node(int apicid)
+{
+ int nid = __apicid_to_phys_node[apicid];
+ if (nid == NUMA_NO_NODE)
+ nid = 0;
+ return nid;
+}
+
+void __init topo_register_apic_phys_node(int apicid)
+{
+ set_bit(topo_apicid_to_node(apicid), apic_phys_node_map.bits);
+}
+
+int __init num_phys_nodes(void)
+{
+ return bitmap_weight(apic_phys_node_map.bits, MAX_NUMNODES);
+}
+
cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
EXPORT_SYMBOL(node_to_cpumask_map);

---

Slightly larger diffstat but all the NUMA bits are together.

Thoughts?

> return apicid & (UINT_MAX << x86_topo_system.dom_shifts[dom - 1]);
> }
>

--
Thanks and Regards,
Prateek