Skip to contentSkip to conversation

Make cpumask_of_node() robust against NUMA_NO_NODE

PATCH Applied John Garry 4 participants 15 messages Jan 7 – Jan 31

Subsystems (6)
lore
Share & tools
lore

John Garry

author
#0 lore raw

v2 cover Make cpumask_of_node() robust against NUMA_NO_NODE

This series aims to remedy an issue that not all per-arch versions of cpumask_of_node() are robust against NUMA_NO_NODE.

In my view, cpumask_of_node() should be able to handle NUMA_NO_NODE. This is because NUMA_NO_NODE is a valid index from the following flow, where the device NUMA node is not set (from default):

device_initialize(dev)

	set_dev_node(dev, NUMA_NO_NODE);

mask = cpumask_of_node(dev_to_node(dev));

The CONFIG_DEBUG_PER_CPU_MAPS=n x86 version cpumask_of_node() would produce an array out-of-index issue (when passed NUMA_NO_NODE), which I think is attempted to be worked around here:
https://lore.kernel.org/linux-scsi/[email protected]/T/#mdedb68052e419b4bfca9ce45bb33b58988018945

I also see a CVE which also looks related:
https://nvd.nist.gov/vuln/detail/cve-2024-39277

Each per-arch version could be picked up separately, as can the asm-generic change.

Differences to v1:
- Put mips and loongarch definition on a single line (Huacai)

John Garry (4):
include/asm-generic/topology.h: Remove unused definition of
cpumask_of_node()
LoongArch: Make cpumask_of_node() robust against NUMA_NO_NODE
MIPS: Loongson: Make cpumask_of_node() robust against NUMA_NO_NODE x86/cpu/topology: Make cpumask_of_node() robust against NUMA_NO_NODE

 arch/loongarch/include/asm/topology.h            | 2 +-
 arch/mips/include/asm/mach-loongson64/topology.h | 2 +-
 arch/x86/include/asm/topology.h                  | 2 ++
 arch/x86/mm/numa.c                               | 2 ++
 include/asm-generic/topology.h                   | 8 ++------
 5 files changed, 8 insertions(+), 8 deletions(-)
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH v2 1/4 include/asm-generic/topology.h: Remove unused definition of cpumask_of_node()

The definition of cpumask_of_node() in question is guarded by conflicting CONFIG_NUMA and !CONFIG_NUMA checks, so remove it.

---
 include/asm-generic/topology.h | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
include/asm-generic/topology.h +2 -6
index 4dbe715be65b4..9865ba48c5b16 100644
@@ -45,11 +45,7 @@
 #endif
 
 #ifndef cpumask_of_node
-  #ifdef CONFIG_NUMA
-    #define cpumask_of_node(node)	((node) == 0 ? cpu_online_mask : cpu_none_mask)
-  #else
-    #define cpumask_of_node(node)	((void)(node), cpu_online_mask)
-  #endif
+#define cpumask_of_node(node)	((void)(node), cpu_online_mask)
 #endif
 #ifndef pcibus_to_node
 #define pcibus_to_node(bus)	((void)(bus), -1)
@@ -61,7 +57,7 @@
 				 cpumask_of_node(pcibus_to_node(bus)))
 #endif
 
-#endif	/* CONFIG_NUMA */
+#endif	/* !CONFIG_NUMA */
 
 #if !defined(CONFIG_NUMA) || !defined(CONFIG_HAVE_MEMORYLESS_NODES)
 
Signature · 1 line
2.43.5
author
#2 lore raw

Re: [PATCH v2 1/4] include/asm-generic/topology.h: Remove unused definition of cpumask_of_node()

Quoted 2 lines · John Garry

The definition of cpumask_of_node() in question is guarded by conflicting CONFIG_NUMA and !CONFIG_NUMA checks, so remove it.

Arnd, can you consider picking this up please?

Thanks

Quoted 32 lines
---
  include/asm-generic/topology.h | 8 ++------
  1 file changed, 2 insertions(+), 6 deletions(-)
include/asm-generic/topology.h +2 -6
index 4dbe715be65b4..9865ba48c5b16 100644
@@ -45,11 +45,7 @@
  #endif
 
  #ifndef cpumask_of_node
-  #ifdef CONFIG_NUMA
-    #define cpumask_of_node(node)	((node) == 0 ? cpu_online_mask : cpu_none_mask)
-  #else
-    #define cpumask_of_node(node)	((void)(node), cpu_online_mask)
-  #endif
+#define cpumask_of_node(node)	((void)(node), cpu_online_mask)
  #endif
  #ifndef pcibus_to_node
  #define pcibus_to_node(bus)	((void)(bus), -1)
@@ -61,7 +57,7 @@
  				 cpumask_of_node(pcibus_to_node(bus)))
  #endif
 
-#endif	/* CONFIG_NUMA */
+#endif	/* !CONFIG_NUMA */
 
  #if !defined(CONFIG_NUMA) || !defined(CONFIG_HAVE_MEMORYLESS_NODES)
 
maintainer
#3 lore raw

Re: [PATCH v2 1/4] include/asm-generic/topology.h: Remove unused definition of cpumask_of_node()

Quoted 5 lines · John Garry
Quoted 2 lines · John Garry

The definition of cpumask_of_node() in question is guarded by conflicting CONFIG_NUMA and !CONFIG_NUMA checks, so remove it.

Arnd, can you consider picking this up please?

Merged now, sorry missing this earlier.

Arnd

author
#4 lore raw

PATCH v2 2/4 LoongArch: Make cpumask_of_node() robust against NUMA_NO_NODE

In reply to John Garry

The arch definition of cpumask_of_node() cannot handle NUMA_NO_NODE - which is a valid index - so add a check for this.

---
 arch/loongarch/include/asm/topology.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/loongarch/include/asm/topology.h +1 -1
index f06e7ff25bb7c..6b79d6183085a 100644
@@ -12,7 +12,7 @@
 
 extern cpumask_t cpus_on_node[];
 
-#define cpumask_of_node(node)  (&cpus_on_node[node])
+#define cpumask_of_node(node)  ((node) == NUMA_NO_NODE ? cpu_all_mask : &cpus_on_node[node])
 
 struct pci_bus;
 extern int pcibus_to_node(struct pci_bus *);
Signature · 1 line
2.43.5
maintainer Reviewed-by on v2 2/4
#5 lore raw

Re: [PATCH v2 2/4] LoongArch: Make cpumask_of_node() robust against NUMA_NO_NODE

Quoted 25 lines · John Garry

The arch definition of cpumask_of_node() cannot handle NUMA_NO_NODE - which is a valid index - so add a check for this.

---
 arch/loongarch/include/asm/topology.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/loongarch/include/asm/topology.h +1 -2
index f06e7ff25bb7c..6b79d6183085a 100644
@@ -12,7 +12,7 @@
 
 extern cpumask_t cpus_on_node[];
 
-#define cpumask_of_node(node)  (&cpus_on_node[node])
+#define cpumask_of_node(node)  ((node) == NUMA_NO_NODE ? cpu_all_mask : &cpus_on_node[node])
 
 struct pci_bus;
 extern int pcibus_to_node(struct pci_bus *);
--

2.43.5

author
#6 lore raw

Re: [PATCH v2 2/4] LoongArch: Make cpumask_of_node() robust against NUMA_NO_NODE

Quoted 1 line · Huacai Chen

Thanks

So can a loongarch maintainer pick this up please?

Quoted 27 lines · John Garry
Quoted 25 lines

The arch definition of cpumask_of_node() cannot handle NUMA_NO_NODE - which is a valid index - so add a check for this.

---
  arch/loongarch/include/asm/topology.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/loongarch/include/asm/topology.h +1 -2
index f06e7ff25bb7c..6b79d6183085a 100644
@@ -12,7 +12,7 @@
 
  extern cpumask_t cpus_on_node[];
 
-#define cpumask_of_node(node)  (&cpus_on_node[node])
+#define cpumask_of_node(node)  ((node) == NUMA_NO_NODE ? cpu_all_mask : &cpus_on_node[node])
 
  struct pci_bus;
  extern int pcibus_to_node(struct pci_bus *);
--

2.43.5

maintainer
#7 lore raw

Re: [PATCH v2 2/4] LoongArch: Make cpumask_of_node() robust against NUMA_NO_NODE

Quoted 7 lines · John Garry
Quoted 1 line · Huacai Chen

Thanks

So can a loongarch maintainer pick this up please?

OK, applied.

Huacai

Quoted 29 lines
Quoted 28 lines · John Garry
Quoted 25 lines

The arch definition of cpumask_of_node() cannot handle NUMA_NO_NODE - which is a valid index - so add a check for this.

---
  arch/loongarch/include/asm/topology.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/loongarch/include/asm/topology.h +1 -2
index f06e7ff25bb7c..6b79d6183085a 100644
@@ -12,7 +12,7 @@
 
  extern cpumask_t cpus_on_node[];
 
-#define cpumask_of_node(node)  (&cpus_on_node[node])
+#define cpumask_of_node(node)  ((node) == NUMA_NO_NODE ? cpu_all_mask : &cpus_on_node[node])
 
  struct pci_bus;
  extern int pcibus_to_node(struct pci_bus *);
--

2.43.5

author
#8 lore raw

PATCH v2 3/4 MIPS: Loongson: Make cpumask_of_node() robust against NUMA_NO_NODE

In reply to John Garry

The arch definition of cpumask_of_node() cannot handle NUMA_NO_NODE - which is a valid index - so add a check for this.

---
 arch/mips/include/asm/mach-loongson64/topology.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/mips/include/asm/mach-loongson64/topology.h +1 -1
index 3414a1fd17835..89bb4deab98a6 100644
@@ -7,7 +7,7 @@
 #define cpu_to_node(cpu)	(cpu_logical_map(cpu) >> 2)
 
 extern cpumask_t __node_cpumask[];
-#define cpumask_of_node(node)	(&__node_cpumask[node])
+#define cpumask_of_node(node)	  ((node) == NUMA_NO_NODE ? cpu_all_mask : &__node_cpumask[node])
 
 struct pci_bus;
 extern int pcibus_to_node(struct pci_bus *);
Signature · 1 line
2.43.5
maintainer Reviewed-by on v2 3/4
#9 lore raw

Re: [PATCH v2 3/4] MIPS: Loongson: Make cpumask_of_node() robust against NUMA_NO_NODE

Quoted 25 lines · John Garry

The arch definition of cpumask_of_node() cannot handle NUMA_NO_NODE - which is a valid index - so add a check for this.

---
 arch/mips/include/asm/mach-loongson64/topology.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/mips/include/asm/mach-loongson64/topology.h +1 -2
index 3414a1fd17835..89bb4deab98a6 100644
@@ -7,7 +7,7 @@
 #define cpu_to_node(cpu)       (cpu_logical_map(cpu) >> 2)
 
 extern cpumask_t __node_cpumask[];
-#define cpumask_of_node(node)  (&__node_cpumask[node])
+#define cpumask_of_node(node)    ((node) == NUMA_NO_NODE ? cpu_all_mask : &__node_cpumask[node])
 
 struct pci_bus;
 extern int pcibus_to_node(struct pci_bus *);
--

2.43.5

author
#10 lore raw

Re: [PATCH v2 3/4] MIPS: Loongson: Make cpumask_of_node() robust against NUMA_NO_NODE

Quoted 1 line · Huacai Chen

Thanks

So can a loongarch or mips maintainer pick this up please?

Quoted 27 lines · John Garry
Quoted 25 lines

The arch definition of cpumask_of_node() cannot handle NUMA_NO_NODE - which is a valid index - so add a check for this.

---
  arch/mips/include/asm/mach-loongson64/topology.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/mips/include/asm/mach-loongson64/topology.h +1 -2
index 3414a1fd17835..89bb4deab98a6 100644
@@ -7,7 +7,7 @@
  #define cpu_to_node(cpu)       (cpu_logical_map(cpu) >> 2)
 
  extern cpumask_t __node_cpumask[];
-#define cpumask_of_node(node)  (&__node_cpumask[node])
+#define cpumask_of_node(node)    ((node) == NUMA_NO_NODE ? cpu_all_mask : &__node_cpumask[node])
 
  struct pci_bus;
  extern int pcibus_to_node(struct pci_bus *);
--

2.43.5

#11 lore raw

Re: [PATCH v2 3/4] MIPS: Loongson: Make cpumask_of_node() robust against NUMA_NO_NODE

Quoted 6 lines · John Garry
Quoted 1 line · Huacai Chen

Thanks

So can a loongarch or mips maintainer pick this up please?

I'll take MIPS patch.

Thomas.

Signature · 2 lines
Crap can work. Given enough thrust pigs will fly, but it's not necessarily a
good idea.                                                [ RFC1925, 2.3 ]
maintainer Applied
#12 lore raw

Re: [PATCH v2 3/4] MIPS: Loongson: Make cpumask_of_node() robust against NUMA_NO_NODE

In reply to John Garry
Quoted 23 lines · John Garry

The arch definition of cpumask_of_node() cannot handle NUMA_NO_NODE - which is a valid index - so add a check for this.

---
 arch/mips/include/asm/mach-loongson64/topology.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/mips/include/asm/mach-loongson64/topology.h +1 -1
index 3414a1fd17835..89bb4deab98a6 100644
@@ -7,7 +7,7 @@
 #define cpu_to_node(cpu)	(cpu_logical_map(cpu) >> 2)
 
 extern cpumask_t __node_cpumask[];
-#define cpumask_of_node(node)	(&__node_cpumask[node])
+#define cpumask_of_node(node)	  ((node) == NUMA_NO_NODE ? cpu_all_mask : &__node_cpumask[node])
 
 struct pci_bus;
 extern int pcibus_to_node(struct pci_bus *);
Signature · 1 line
2.43.5

applied to mips-next
Thomas.

Signature · 2 lines
Crap can work. Given enough thrust pigs will fly, but it's not necessarily a
good idea.                                                [ RFC1925, 2.3 ]
author
#13 lore raw

PATCH v2 4/4 x86/cpu/topology: Make cpumask_of_node() robust against NUMA_NO_NODE

In reply to John Garry

The arch definitions of cpumask_of_node() cannot handle NUMA_NO_NODE - which is a valid index - so add checks for this.

---
 arch/x86/include/asm/topology.h | 2 ++
 arch/x86/mm/numa.c              | 2 ++
 2 files changed, 4 insertions(+)
arch/x86/include/asm/topology.h +2 -0
index 1fadf0cf520c5..b51f2e771a582 100644
@@ -69,6 +69,8 @@ extern const struct cpumask *cpumask_of_node(int node);
 /* Returns a pointer to the cpumask of CPUs on Node 'node'. */
 static inline const struct cpumask *cpumask_of_node(int node)
 {
+	if (node == NUMA_NO_NODE)
+		return cpu_all_mask;
 	return node_to_cpumask_map[node];
 }
 #endif
arch/x86/mm/numa.c +2 -0
index 7a97327140df8..0be94f4682232 100644
@@ -408,6 +408,8 @@ void numa_remove_cpu(unsigned int cpu)
  */
 const struct cpumask *cpumask_of_node(int node)
 {
+	if (node == NUMA_NO_NODE)
+		return cpu_all_mask;
 	if ((unsigned)node >= nr_node_ids) {
 		printk(KERN_WARNING
 			"cpumask_of_node(%d): (unsigned)node >= nr_node_ids(%u)\n",
Signature · 1 line
2.43.5
author
#14 lore raw
In reply to John Garry

On 07/01/2026 09:40, John Garry wrote:

A friendly reminder on this one.

Thanks

Quoted 40 lines

This series aims to remedy an issue that not all per-arch versions of cpumask_of_node() are robust against NUMA_NO_NODE.

In my view, cpumask_of_node() should be able to handle NUMA_NO_NODE. This is because NUMA_NO_NODE is a valid index from the following flow, where the device NUMA node is not set (from default):

device_initialize(dev)

	set_dev_node(dev, NUMA_NO_NODE);

mask = cpumask_of_node(dev_to_node(dev));

The CONFIG_DEBUG_PER_CPU_MAPS=n x86 version cpumask_of_node() would produce an array out-of-index issue (when passed NUMA_NO_NODE), which I think is attempted to be worked around here:
https://lore.kernel.org/linux-scsi/[email protected]/T/#mdedb68052e419b4bfca9ce45bb33b58988018945

I also see a CVE which also looks related:
https://nvd.nist.gov/vuln/detail/cve-2024-39277

Each per-arch version could be picked up separately, as can the asm-generic change.

Differences to v1:
- Put mips and loongarch definition on a single line (Huacai)

John Garry (4):
include/asm-generic/topology.h: Remove unused definition of
cpumask_of_node()
LoongArch: Make cpumask_of_node() robust against NUMA_NO_NODE
MIPS: Loongson: Make cpumask_of_node() robust against NUMA_NO_NODE x86/cpu/topology: Make cpumask_of_node() robust against NUMA_NO_NODE

  arch/loongarch/include/asm/topology.h            | 2 +-
  arch/mips/include/asm/mach-loongson64/topology.h | 2 +-
  arch/x86/include/asm/topology.h                  | 2 ++
  arch/x86/mm/numa.c                               | 2 ++
  include/asm-generic/topology.h                   | 8 ++------
  5 files changed, 8 insertions(+), 8 deletions(-)