Skip to contentSkip to conversation

mm: vmalloc: Check if a hash-index is in cpu_possible_mask

PATCH Superseded Uladzislau Rezki (Sony) 2 participants 2 messages Jun 26, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Uladzislau Rezki (Sony)

author maintainer
#0 lore raw

PATCH 1/1 mm: vmalloc: Check if a hash-index is in cpu_possible_mask

The problem is that there are systems where cpu_possible_mask has gaps between set CPUs, for example SPARC. In this scenario addr_to_vb_xa() hash function can return an index which accesses to not-possible and not setup CPU area using per_cpu() macro.

A per-cpu vmap_block_queue is also used as hash table assuming that incorrectly assumes the cpu_possible_mask has not gaps.

Fix it by adjusting an index to a next possible CPU.

Fixes 062eacf57ad9 ("mm: vmalloc: remove a global vmap_blocks xarray")
---
 mm/vmalloc.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
mm/vmalloc.c +9 -1
index b4c42da9f3901..6b783baf12a14 100644
@@ -2544,7 +2544,15 @@ static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
 static struct xarray *
 addr_to_vb_xa(unsigned long addr)
 {
-	int index = (addr / VMAP_BLOCK_SIZE) % num_possible_cpus();
+	int index = (addr / VMAP_BLOCK_SIZE) % nr_cpu_ids;
+
+	/*
+	 * Please note, nr_cpu_ids points on a highest set
+	 * possible bit, i.e. we never invoke cpumask_next()
+	 * if an index points on it which is nr_cpu_ids - 1.
+	 */
+	if (!cpu_possible(index))
+		index = cpumask_next(index, cpu_possible_mask);
 
 	return &per_cpu(vmap_block_queue, index).vmap_blocks;
 }
Signature · 1 line
2.39.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Uladzislau Rezki (Sony)

The problem is that there are systems where cpu_possible_mask has gaps between set CPUs, for example SPARC. In this scenario addr_to_vb_xa() hash function can return an index which accesses to not-possible and not setup CPU area using per_cpu() macro.

A per-cpu vmap_block_queue is also used as hash table assuming

                                                        ~~~~~
Quoted 1 line

that incorrectly assumes the cpu_possible_mask has not gaps.

~~~~~~
Typo of duplicated word?

Other than above nit, this looks good to me. Thanks.

Quoted 35 lines

Fix it by adjusting an index to a next possible CPU.

Fixes 062eacf57ad9 ("mm: vmalloc: remove a global vmap_blocks xarray")
---
 mm/vmalloc.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
mm/vmalloc.c +9 -1
index b4c42da9f3901..6b783baf12a14 100644
@@ -2544,7 +2544,15 @@ static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
 static struct xarray *
 addr_to_vb_xa(unsigned long addr)
 {
-	int index = (addr / VMAP_BLOCK_SIZE) % num_possible_cpus();
+	int index = (addr / VMAP_BLOCK_SIZE) % nr_cpu_ids;
+
+	/*
+	 * Please note, nr_cpu_ids points on a highest set
+	 * possible bit, i.e. we never invoke cpumask_next()
+	 * if an index points on it which is nr_cpu_ids - 1.
+	 */
+	if (!cpu_possible(index))
+		index = cpumask_next(index, cpu_possible_mask);
 
 	return &per_cpu(vmap_block_queue, index).vmap_blocks;
 }
Signature · 1 line
2.39.2