RDMA: hfi1: cpumasks usage fixes
Subsystems (3)
Yury Norov
maintainercover 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
Yury Norov
maintainerPATCH 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
@@ -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
Yury Norov
maintainerPATCH 2/7 RDMA: hfi1: fix possible divide-by-zero in find_hw_thread_mask()
In reply to Yury NorovFrom: "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
@@ -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
Yury Norov
maintainerPATCH 3/7 RDMA: hfi1: simplify find_hw_thread_mask()
In reply to Yury NorovFrom: "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
@@ -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
Yury Norov
maintainerPATCH 4/7 RDMA: hfi1: simplify init_real_cpu_mask()
In reply to Yury NorovFrom: "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
@@ -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
Yury Norov
maintainerPATCH 5/7 RDMA: hfi1: use rounddown in find_hw_thread_mask()
In reply to Yury NorovFrom: "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
@@ -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
Yury Norov
maintainerPATCH 6/7 RDMA: hfi1: simplify hfi1_get_proc_affinity()
In reply to Yury NorovFrom: "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
@@ -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
Yury Norov
maintainerPATCH 7/7 RDMI: hfi1: drop cpumask_empty() call in hfi1/affinity.c
In reply to Yury NorovFrom: "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
@@ -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
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: addcpumask_clear_cpus()
RDMA: hfi1: fix possible divide-by-zero infind_hw_thread_mask()
RDMA: hfi1: simplifyfind_hw_thread_mask()
RDMA: hfi1: simplifyinit_real_cpu_mask()
RDMA: hfi1: use rounddown infind_hw_thread_mask()
RDMA: hfi1: simplifyhfi1_get_proc_affinity()
RDMI: hfi1: dropcpumask_empty()call inhfi1/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
Yury Norov
maintainerQuoted 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: addcpumask_clear_cpus()
RDMA: hfi1: fix possible divide-by-zero infind_hw_thread_mask()
RDMA: hfi1: simplifyfind_hw_thread_mask()
RDMA: hfi1: simplifyinit_real_cpu_mask()
RDMA: hfi1: use rounddown infind_hw_thread_mask()
RDMA: hfi1: simplifyhfi1_get_proc_affinity()
RDMI: hfi1: dropcpumask_empty()call inhfi1/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?
Dennis Dalessandro
maintainerQuoted 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: addcpumask_clear_cpus()
RDMA: hfi1: fix possible divide-by-zero infind_hw_thread_mask()
RDMA: hfi1: simplifyfind_hw_thread_mask()
RDMA: hfi1: simplifyinit_real_cpu_mask()
RDMA: hfi1: use rounddown infind_hw_thread_mask()
RDMA: hfi1: simplifyhfi1_get_proc_affinity()
RDMI: hfi1: dropcpumask_empty()call inhfi1/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.
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: addcpumask_clear_cpus()
RDMA: hfi1: fix possible divide-by-zero infind_hw_thread_mask()
RDMA: hfi1: simplifyfind_hw_thread_mask()
RDMA: hfi1: simplifyinit_real_cpu_mask()
RDMA: hfi1: use rounddown infind_hw_thread_mask()
RDMA: hfi1: simplifyhfi1_get_proc_affinity()
RDMI: hfi1: dropcpumask_empty()call inhfi1/affinity.cdrivers/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.
Leon Romanovsky
AppliedQuoted 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: addcpumask_clear_cpus()
RDMA: hfi1: fix possible divide-by-zero infind_hw_thread_mask()
RDMA: hfi1: simplifyfind_hw_thread_mask()
RDMA: hfi1: simplifyinit_real_cpu_mask()
RDMA: hfi1: use rounddown infind_hw_thread_mask()
RDMA: hfi1: simplifyhfi1_get_proc_affinity()
RDMI: hfi1: dropcpumask_empty()call inhfi1/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]>