Skip to contentSkip to conversation

RDMA: hfi1: cpumasks usage fixes

PATCH Applied Yury Norov 3 participants 13 messages Jun 4, 2025 – Jun 25, 2025

Subsystems (3)
lore
Share & tools
lore

Yury Norov

author maintainer
#0 lore raw

cover RDMA: hfi1: cpumasks usage fixes

The driver uses cpumasks API in a non-optimal way; partially because of absence of proper functions. Fix this and nearby logic.

Yury Norov [NVIDIA] (7):
cpumask: add cpumask_clear_cpus()
RDMA: hfi1: fix possible divide-by-zero in find_hw_thread_mask()
RDMA: hfi1: simplify find_hw_thread_mask()
RDMA: hfi1: simplify init_real_cpu_mask()
RDMA: hfi1: use rounddown in find_hw_thread_mask()
RDMA: hfi1: simplify hfi1_get_proc_affinity()
RDMI: hfi1: drop cpumask_empty() call in hfi1/affinity.c

 drivers/infiniband/hw/hfi1/affinity.c | 96 +++++++++++----------------
 include/linux/cpumask.h               | 12 ++++
 2 files changed, 49 insertions(+), 59 deletions(-)
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH 1/7 cpumask: add cpumask_clear_cpus()

From: "Yury Norov [NVIDIA]" <[email protected]>

When user wants to clear a range in cpumask, the only option the API provides now is a for-loop, like:

	for_each_cpu_from(cpu, mask) {
		if (cpu >= ncpus)
			break;
		__cpumask_clear_cpu(cpu, mask);
	}

In the bitmap API we have bitmap_clear() for that, which is significantly faster than a for-loop. Propagate it to cpumasks.

---
 include/linux/cpumask.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
include/linux/cpumask.h +12 -0
index 7ae80a7ca81e..ede95bbe8b80 100644
@@ -609,6 +609,18 @@ void __cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
 	__set_bit(cpumask_check(cpu), cpumask_bits(dstp));
 }
 
+/**
+ * cpumask_clear_cpus - clear cpus in a cpumask
+ * @dstp:  the cpumask pointer
+ * @cpu:   cpu number (< nr_cpu_ids)
+ * @ncpus: number of cpus to clear (< nr_cpu_ids)
+ */
+static __always_inline void cpumask_clear_cpus(struct cpumask *dstp,
+						unsigned int cpu, unsigned int ncpus)
+{
+	cpumask_check(cpu + ncpus - 1);
+	bitmap_clear(cpumask_bits(dstp), cpumask_check(cpu), ncpus);
+}
 
 /**
  * cpumask_clear_cpu - clear a cpu in a cpumask
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH 2/7 RDMA: hfi1: fix possible divide-by-zero in find_hw_thread_mask()

In reply to Yury Norov

From: "Yury Norov [NVIDIA]" <[email protected]>

The function divides number of online CPUs by num_core_siblings, and later checks the divider by zero. This implies a possibility to get and divide-by-zero runtime error. Fix it by moving the check prior to division. This also helps to save one indentation level.

---
 drivers/infiniband/hw/hfi1/affinity.c | 44 +++++++++++++++------------
 1 file changed, 24 insertions(+), 20 deletions(-)
drivers/infiniband/hw/hfi1/affinity.c +24 -20
index 7ead8746b79b..f2c530ab85a5 100644
@@ -964,31 +964,35 @@ static void find_hw_thread_mask(uint hw_thread_no, cpumask_var_t hw_thread_mask,
 				struct hfi1_affinity_node_list *affinity)
 {
 	int possible, curr_cpu, i;
-	uint num_cores_per_socket = node_affinity.num_online_cpus /
+	uint num_cores_per_socket;
+
+	cpumask_copy(hw_thread_mask, &affinity->proc.mask);
+
+	if (affinity->num_core_siblings == 0)
+		return;
+
+	num_cores_per_socket = node_affinity.num_online_cpus /
 					affinity->num_core_siblings /
 						node_affinity.num_online_nodes;
 
-	cpumask_copy(hw_thread_mask, &affinity->proc.mask);
-	if (affinity->num_core_siblings > 0) {
-		/* Removing other siblings not needed for now */
-		possible = cpumask_weight(hw_thread_mask);
-		curr_cpu = cpumask_first(hw_thread_mask);
-		for (i = 0;
-		     i < num_cores_per_socket * node_affinity.num_online_nodes;
-		     i++)
-			curr_cpu = cpumask_next(curr_cpu, hw_thread_mask);
-
-		for (; i < possible; i++) {
-			cpumask_clear_cpu(curr_cpu, hw_thread_mask);
-			curr_cpu = cpumask_next(curr_cpu, hw_thread_mask);
-		}
+	/* Removing other siblings not needed for now */
+	possible = cpumask_weight(hw_thread_mask);
+	curr_cpu = cpumask_first(hw_thread_mask);
+	for (i = 0;
+	     i < num_cores_per_socket * node_affinity.num_online_nodes;
+	     i++)
+		curr_cpu = cpumask_next(curr_cpu, hw_thread_mask);
 
-		/* Identifying correct HW threads within physical cores */
-		cpumask_shift_left(hw_thread_mask, hw_thread_mask,
-				   num_cores_per_socket *
-				   node_affinity.num_online_nodes *
-				   hw_thread_no);
+	for (; i < possible; i++) {
+		cpumask_clear_cpu(curr_cpu, hw_thread_mask);
+		curr_cpu = cpumask_next(curr_cpu, hw_thread_mask);
 	}
+
+	/* Identifying correct HW threads within physical cores */
+	cpumask_shift_left(hw_thread_mask, hw_thread_mask,
+			   num_cores_per_socket *
+			   node_affinity.num_online_nodes *
+			   hw_thread_no);
 }
 
 int hfi1_get_proc_affinity(int node)
Signature · 1 line
2.43.0
author maintainer
#3 lore raw

PATCH 3/7 RDMA: hfi1: simplify find_hw_thread_mask()

In reply to Yury Norov

From: "Yury Norov [NVIDIA]" <[email protected]>

The function opencodes cpumask_nth() and cpumask_clear_cpus(). The dedicated helpers are easier to use and usually much faster than opencoded for-loops.

---
 drivers/infiniband/hw/hfi1/affinity.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)
drivers/infiniband/hw/hfi1/affinity.c +4 -12
index f2c530ab85a5..9ea80b777061 100644
@@ -963,7 +963,7 @@ void hfi1_put_irq_affinity(struct hfi1_devdata *dd,
 static void find_hw_thread_mask(uint hw_thread_no, cpumask_var_t hw_thread_mask,
 				struct hfi1_affinity_node_list *affinity)
 {
-	int possible, curr_cpu, i;
+	int curr_cpu;
 	uint num_cores_per_socket;
 
 	cpumask_copy(hw_thread_mask, &affinity->proc.mask);
@@ -976,17 +976,9 @@ static void find_hw_thread_mask(uint hw_thread_no, cpumask_var_t hw_thread_mask,
 						node_affinity.num_online_nodes;
 
 	/* Removing other siblings not needed for now */
-	possible = cpumask_weight(hw_thread_mask);
-	curr_cpu = cpumask_first(hw_thread_mask);
-	for (i = 0;
-	     i < num_cores_per_socket * node_affinity.num_online_nodes;
-	     i++)
-		curr_cpu = cpumask_next(curr_cpu, hw_thread_mask);
-
-	for (; i < possible; i++) {
-		cpumask_clear_cpu(curr_cpu, hw_thread_mask);
-		curr_cpu = cpumask_next(curr_cpu, hw_thread_mask);
-	}
+	curr_cpu = cpumask_cpumask_nth(num_cores_per_socket *
+			node_affinity.num_online_nodes, hw_thread_mask) + 1;
+	cpumask_clear_cpus(hw_thread_mask, curr_cpu, nr_cpu_ids - curr_cpu);
 
 	/* Identifying correct HW threads within physical cores */
 	cpumask_shift_left(hw_thread_mask, hw_thread_mask,
Signature · 1 line
2.43.0
author maintainer
#4 lore raw

PATCH 4/7 RDMA: hfi1: simplify init_real_cpu_mask()

In reply to Yury Norov

From: "Yury Norov [NVIDIA]" <[email protected]>

The function opencodes cpumask_nth() and cpumask_clear_cpus(). The dedicated helpers are easier to use and usually much faster than opencoded for-loops.

While there, drop useless clear of real_cpu_mask.

---
 drivers/infiniband/hw/hfi1/affinity.c | 19 +++++--------------
 1 file changed, 5 insertions(+), 14 deletions(-)
drivers/infiniband/hw/hfi1/affinity.c +5 -14
index 9ea80b777061..b2884226827a 100644
@@ -92,9 +92,7 @@ static void cpu_mask_set_put(struct cpu_mask_set *set, int cpu)
 /* Initialize non-HT cpu cores mask */
 void init_real_cpu_mask(void)
 {
-	int possible, curr_cpu, i, ht;
-
-	cpumask_clear(&node_affinity.real_cpu_mask);
+	int possible, curr_cpu, ht;
 
 	/* Start with cpu online mask as the real cpu mask */
 	cpumask_copy(&node_affinity.real_cpu_mask, cpu_online_mask);
@@ -110,17 +108,10 @@ void init_real_cpu_mask(void)
 	 * "real" cores.  Assumes that HT cores are not enumerated in
 	 * succession (except in the single core case).
 	 */
-	curr_cpu = cpumask_first(&node_affinity.real_cpu_mask);
-	for (i = 0; i < possible / ht; i++)
-		curr_cpu = cpumask_next(curr_cpu, &node_affinity.real_cpu_mask);
-	/*
-	 * Step 2.  Remove the remaining HT siblings.  Use cpumask_next() to
-	 * skip any gaps.
-	 */
-	for (; i < possible; i++) {
-		cpumask_clear_cpu(curr_cpu, &node_affinity.real_cpu_mask);
-		curr_cpu = cpumask_next(curr_cpu, &node_affinity.real_cpu_mask);
-	}
+	curr_cpu = cpumask_nth(possible / ht, &node_affinity.real_cpu_mask) + 1;
+
+	/* Step 2.  Remove the remaining HT siblings. */
+	cpumask_clear_cpus(&node_affinity.real_cpu_mask, curr_cpu, nr_cpu_ids - curr_cpu);
 }
 
 int node_affinity_init(void)
Signature · 1 line
2.43.0
author maintainer
#5 lore raw

PATCH 5/7 RDMA: hfi1: use rounddown in find_hw_thread_mask()

In reply to Yury Norov

From: "Yury Norov [NVIDIA]" <[email protected]>

num_cores_per_socket is calculated by dividing by node_affinity.num_online_nodes, but all users of this variable multiply it by node_affinity.num_online_nodes again. This effectively is the same as rounding it down by node_affinity.num_online_nodes.

---
 drivers/infiniband/hw/hfi1/affinity.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
drivers/infiniband/hw/hfi1/affinity.c +5 -10
index b2884226827a..7fa894c23fea 100644
@@ -955,27 +955,22 @@ static void find_hw_thread_mask(uint hw_thread_no, cpumask_var_t hw_thread_mask,
 				struct hfi1_affinity_node_list *affinity)
 {
 	int curr_cpu;
-	uint num_cores_per_socket;
+	uint num_cores;
 
 	cpumask_copy(hw_thread_mask, &affinity->proc.mask);
 
 	if (affinity->num_core_siblings == 0)
 		return;
 
-	num_cores_per_socket = node_affinity.num_online_cpus /
-					affinity->num_core_siblings /
-						node_affinity.num_online_nodes;
+	num_cores = rounddown(node_affinity.num_online_cpus / affinity->num_core_siblings,
+				node_affinity.num_online_nodes);
 
 	/* Removing other siblings not needed for now */
-	curr_cpu = cpumask_cpumask_nth(num_cores_per_socket *
-			node_affinity.num_online_nodes, hw_thread_mask) + 1;
+	curr_cpu = cpumask_nth(num_cores * node_affinity.num_online_nodes, hw_thread_mask) + 1;
 	cpumask_clear_cpus(hw_thread_mask, curr_cpu, nr_cpu_ids - curr_cpu);
 
 	/* Identifying correct HW threads within physical cores */
-	cpumask_shift_left(hw_thread_mask, hw_thread_mask,
-			   num_cores_per_socket *
-			   node_affinity.num_online_nodes *
-			   hw_thread_no);
+	cpumask_shift_left(hw_thread_mask, hw_thread_mask, num_cores * hw_thread_no);
 }
 
 int hfi1_get_proc_affinity(int node)
Signature · 1 line
2.43.0
author maintainer
#6 lore raw

PATCH 6/7 RDMA: hfi1: simplify hfi1_get_proc_affinity()

In reply to Yury Norov

From: "Yury Norov [NVIDIA]" <[email protected]>

The function protects the for loop with affinity->num_core_siblings > 0 condition, which is redundant because the loop will break immediately in that case.

Drop it and save one indentation level.

---
 drivers/infiniband/hw/hfi1/affinity.c | 28 +++++++++++++--------------
 1 file changed, 13 insertions(+), 15 deletions(-)
drivers/infiniband/hw/hfi1/affinity.c +13 -15
index 7fa894c23fea..8974aa1e63d1 100644
@@ -1069,22 +1069,20 @@ int hfi1_get_proc_affinity(int node)
 	 * If HT cores are enabled, identify which HW threads within the
 	 * physical cores should be used.
 	 */
-	if (affinity->num_core_siblings > 0) {
-		for (i = 0; i < affinity->num_core_siblings; i++) {
-			find_hw_thread_mask(i, hw_thread_mask, affinity);
+	for (i = 0; i < affinity->num_core_siblings; i++) {
+		find_hw_thread_mask(i, hw_thread_mask, affinity);
 
-			/*
-			 * If there's at least one available core for this HW
-			 * thread number, stop looking for a core.
-			 *
-			 * diff will always be not empty at least once in this
-			 * loop as the used mask gets reset when
-			 * (set->mask == set->used) before this loop.
-			 */
-			cpumask_andnot(diff, hw_thread_mask, &set->used);
-			if (!cpumask_empty(diff))
-				break;
-		}
+		/*
+		 * If there's at least one available core for this HW
+		 * thread number, stop looking for a core.
+		 *
+		 * diff will always be not empty at least once in this
+		 * loop as the used mask gets reset when
+		 * (set->mask == set->used) before this loop.
+		 */
+		cpumask_andnot(diff, hw_thread_mask, &set->used);
+		if (!cpumask_empty(diff))
+			break;
 	}
 	hfi1_cdbg(PROC, "Same available HW thread on all physical CPUs: %*pbl",
 		  cpumask_pr_args(hw_thread_mask));
Signature · 1 line
2.43.0
author maintainer
#7 lore raw

PATCH 7/7 RDMI: hfi1: drop cpumask_empty() call in hfi1/affinity.c

In reply to Yury Norov

From: "Yury Norov [NVIDIA]" <[email protected]>

In few places, the driver tests a cpumask for emptiness immediately before calling functions that report emptiness themself.

---
 drivers/infiniband/hw/hfi1/affinity.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/infiniband/hw/hfi1/affinity.c +7 -9
index 8974aa1e63d1..ee7fedc67b86 100644
@@ -337,9 +337,10 @@ static int _dev_comp_vect_cpu_get(struct hfi1_devdata *dd,
 		       &entry->def_intr.used);
 
 	/* If there are non-interrupt CPUs available, use them first */
-	if (!cpumask_empty(non_intr_cpus))
-		cpu = cpumask_first(non_intr_cpus);
-	else /* Otherwise, use interrupt CPUs */
+	cpu = cpumask_first(non_intr_cpus);
+
+	/* Otherwise, use interrupt CPUs */
+	if (cpu >= nr_cpu_ids)
 		cpu = cpumask_first(available_cpus);
 
 	if (cpu >= nr_cpu_ids) { /* empty */
@@ -1080,8 +1081,7 @@ int hfi1_get_proc_affinity(int node)
 		 * loop as the used mask gets reset when
 		 * (set->mask == set->used) before this loop.
 		 */
-		cpumask_andnot(diff, hw_thread_mask, &set->used);
-		if (!cpumask_empty(diff))
+		if (cpumask_andnot(diff, hw_thread_mask, &set->used))
 			break;
 	}
 	hfi1_cdbg(PROC, "Same available HW thread on all physical CPUs: %*pbl",
@@ -1113,8 +1113,7 @@ int hfi1_get_proc_affinity(int node)
 	 *    used for process assignments using the same method as
 	 *    the preferred NUMA node.
 	 */
-	cpumask_andnot(diff, available_mask, intrs_mask);
-	if (!cpumask_empty(diff))
+	if (cpumask_andnot(diff, available_mask, intrs_mask))
 		cpumask_copy(available_mask, diff);
 
 	/* If we don't have CPUs on the preferred node, use other NUMA nodes */
@@ -1130,8 +1129,7 @@ int hfi1_get_proc_affinity(int node)
 		 * At first, we don't want to place processes on the same
 		 * CPUs as interrupt handlers.
 		 */
-		cpumask_andnot(diff, available_mask, intrs_mask);
-		if (!cpumask_empty(diff))
+		if (cpumask_andnot(diff, available_mask, intrs_mask))
 			cpumask_copy(available_mask, diff);
 	}
 	hfi1_cdbg(PROC, "Possible CPUs for process: %*pbl",
Signature · 1 line
2.43.0
#8 lore raw
In reply to Yury Norov
Quoted 12 lines · Yury Norov

The driver uses cpumasks API in a non-optimal way; partially because of absence of proper functions. Fix this and nearby logic.

Yury Norov [NVIDIA] (7):
cpumask: add cpumask_clear_cpus()
RDMA: hfi1: fix possible divide-by-zero in find_hw_thread_mask()
RDMA: hfi1: simplify find_hw_thread_mask()
RDMA: hfi1: simplify init_real_cpu_mask()
RDMA: hfi1: use rounddown in find_hw_thread_mask()
RDMA: hfi1: simplify hfi1_get_proc_affinity()
RDMI: hfi1: drop cpumask_empty() call in hfi1/affinity.c

Quoted 3 lines
 drivers/infiniband/hw/hfi1/affinity.c | 96 +++++++++++----------------
 include/linux/cpumask.h               | 12 ++++
 2 files changed, 49 insertions(+), 59 deletions(-)

Dennis?

Quoted 4 lines
Signature · 2 lines
-- 
2.43.0
author maintainer
#9 lore raw
Quoted 13 lines · Leon Romanovsky
Quoted 12 lines · Yury Norov

The driver uses cpumasks API in a non-optimal way; partially because of absence of proper functions. Fix this and nearby logic.

Yury Norov [NVIDIA] (7):
cpumask: add cpumask_clear_cpus()
RDMA: hfi1: fix possible divide-by-zero in find_hw_thread_mask()
RDMA: hfi1: simplify find_hw_thread_mask()
RDMA: hfi1: simplify init_real_cpu_mask()
RDMA: hfi1: use rounddown in find_hw_thread_mask()
RDMA: hfi1: simplify hfi1_get_proc_affinity()
RDMI: hfi1: drop cpumask_empty() call in hfi1/affinity.c

Quoted 5 lines
Quoted 3 lines
 drivers/infiniband/hw/hfi1/affinity.c | 96 +++++++++++----------------
 include/linux/cpumask.h               | 12 ++++
 2 files changed, 49 insertions(+), 59 deletions(-)

Dennis?

So?.. Any feedback?

#10 lore raw
Quoted 14 lines · Yury Norov
Quoted 13 lines · Leon Romanovsky
Quoted 12 lines · Yury Norov

The driver uses cpumasks API in a non-optimal way; partially because of absence of proper functions. Fix this and nearby logic.

Yury Norov [NVIDIA] (7):
cpumask: add cpumask_clear_cpus()
RDMA: hfi1: fix possible divide-by-zero in find_hw_thread_mask()
RDMA: hfi1: simplify find_hw_thread_mask()
RDMA: hfi1: simplify init_real_cpu_mask()
RDMA: hfi1: use rounddown in find_hw_thread_mask()
RDMA: hfi1: simplify hfi1_get_proc_affinity()
RDMI: hfi1: drop cpumask_empty() call in hfi1/affinity.c

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
 drivers/infiniband/hw/hfi1/affinity.c | 96 +++++++++++----------------
 include/linux/cpumask.h               | 12 ++++
 2 files changed, 49 insertions(+), 59 deletions(-)

Dennis?

So?.. Any feedback?

I'm ambivalent about this patch series. It looks OK but I don't think it's really fixing anything.

#11 lore raw
Quoted 19 lines · Dennis Dalessandro
Quoted 18 lines · Yury Norov
Quoted 17 lines · Leon Romanovsky
Quoted 15 lines · Yury Norov

The driver uses cpumasks API in a non-optimal way; partially because of absence of proper functions. Fix this and nearby logic.

Yury Norov [NVIDIA] (7):
cpumask: add cpumask_clear_cpus()
RDMA: hfi1: fix possible divide-by-zero in find_hw_thread_mask()
RDMA: hfi1: simplify find_hw_thread_mask()
RDMA: hfi1: simplify init_real_cpu_mask()
RDMA: hfi1: use rounddown in find_hw_thread_mask()
RDMA: hfi1: simplify hfi1_get_proc_affinity()
RDMI: hfi1: drop cpumask_empty() call in hfi1/affinity.c

 drivers/infiniband/hw/hfi1/affinity.c | 96 +++++++++++----------------
 include/linux/cpumask.h               | 12 ++++
 2 files changed, 49 insertions(+), 59 deletions(-)
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Dennis?

So?.. Any feedback?

I'm ambivalent about this patch series. It looks OK but I don't think it's really fixing anything.

Yeas, I applied because more code was deleted than added.

#12 lore raw
In reply to Yury Norov
Quoted 12 lines · Yury Norov

The driver uses cpumasks API in a non-optimal way; partially because of absence of proper functions. Fix this and nearby logic.

Yury Norov [NVIDIA] (7):
cpumask: add cpumask_clear_cpus()
RDMA: hfi1: fix possible divide-by-zero in find_hw_thread_mask()
RDMA: hfi1: simplify find_hw_thread_mask()
RDMA: hfi1: simplify init_real_cpu_mask()
RDMA: hfi1: use rounddown in find_hw_thread_mask()
RDMA: hfi1: simplify hfi1_get_proc_affinity()
RDMI: hfi1: drop cpumask_empty() call in hfi1/affinity.c

Quoted 1 line

[...]

Applied, thanks!

[1/7] cpumask: add cpumask_clear_cpus()

      https://git.kernel.org/rdma/rdma/c/c15d5e70db9627
[2/7] RDMA: hfi1: fix possible divide-by-zero in find_hw_thread_mask()
      https://git.kernel.org/rdma/rdma/c/37b3cba54b404a
[3/7] RDMA: hfi1: simplify find_hw_thread_mask()
      https://git.kernel.org/rdma/rdma/c/f2c2afbba77c11
[4/7] RDMA: hfi1: simplify init_real_cpu_mask()
      https://git.kernel.org/rdma/rdma/c/9c965445a636b7
[5/7] RDMA: hfi1: use rounddown in find_hw_thread_mask()
      https://git.kernel.org/rdma/rdma/c/9370795029d41a
[6/7] RDMA: hfi1: simplify hfi1_get_proc_affinity()
      https://git.kernel.org/rdma/rdma/c/7c2c2f3a205b2b
[7/7] RDMI: hfi1: drop cpumask_empty() call in hfi1/affinity.c
      https://git.kernel.org/rdma/rdma/c/185e34e8f249cb

Best regards,

Signature · 1 line
Leon Romanovsky <[email protected]>