Skip to contentSkip to conversation

net: mana: add irq_spread()

PATCH In review Yury Norov 3 participants 11 messages Dec 17, 2023 – Dec 19, 2023

Subsystems (7)
lore
Share & tools
lore

Yury Norov

author maintainer
#0 lore raw

cover net: mana: add irq_spread()

Add irq_spread() function that makes the driver working 15% faster than with cpumask_local_spread()

Yury Norov (3):
cpumask: add cpumask_weight_andnot()
cpumask: define cleanup function for cpumasks
net: mana: add a function to spread IRQs per CPUs

 .../net/ethernet/microsoft/mana/gdma_main.c   | 28 +++++++++++++++++++
 include/linux/bitmap.h                        | 12 ++++++++
 include/linux/cpumask.h                       | 17 +++++++++++
 lib/bitmap.c                                  |  7 +++++
 4 files changed, 64 insertions(+)
Signature · 1 line
2.40.1
author maintainer
#1 lore raw

PATCH 1/3 cpumask: add cpumask_weight_andnot()

Similarly to cpumask_weight_and(), cpumask_weight_andnot() is a handy helper that may help to avoid creating an intermediate mask just to calculate number of bits that set in a 1st given mask, and clear in 2nd one.

---
 include/linux/bitmap.h  | 12 ++++++++++++
 include/linux/cpumask.h | 13 +++++++++++++
 lib/bitmap.c            |  7 +++++++
 3 files changed, 32 insertions(+)
include/linux/bitmap.h +12 -0
index 99451431e4d6..5814e9ee40ba 100644
@@ -54,6 +54,7 @@ struct device;
  *  bitmap_full(src, nbits)                     Are all bits set in *src?
  *  bitmap_weight(src, nbits)                   Hamming Weight: number set bits
  *  bitmap_weight_and(src1, src2, nbits)        Hamming Weight of and'ed bitmap
+ *  bitmap_weight_andnot(src1, src2, nbits)     Hamming Weight of andnot'ed bitmap
  *  bitmap_set(dst, pos, nbits)                 Set specified bit area
  *  bitmap_clear(dst, pos, nbits)               Clear specified bit area
  *  bitmap_find_next_zero_area(buf, len, pos, n, mask)  Find bit free area
@@ -169,6 +170,8 @@ bool __bitmap_subset(const unsigned long *bitmap1,
 unsigned int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
 unsigned int __bitmap_weight_and(const unsigned long *bitmap1,
 				 const unsigned long *bitmap2, unsigned int nbits);
+unsigned int __bitmap_weight_andnot(const unsigned long *bitmap1,
+				    const unsigned long *bitmap2, unsigned int nbits);
 void __bitmap_set(unsigned long *map, unsigned int start, int len);
 void __bitmap_clear(unsigned long *map, unsigned int start, int len);
 
@@ -425,6 +428,15 @@ unsigned long bitmap_weight_and(const unsigned long *src1,
 	return __bitmap_weight_and(src1, src2, nbits);
 }
 
+static __always_inline
+unsigned long bitmap_weight_andnot(const unsigned long *src1,
+				   const unsigned long *src2, unsigned int nbits)
+{
+	if (small_const_nbits(nbits))
+		return hweight_long(*src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits));
+	return __bitmap_weight_andnot(src1, src2, nbits);
+}
+
 static __always_inline void bitmap_set(unsigned long *map, unsigned int start,
 		unsigned int nbits)
 {
include/linux/cpumask.h +13 -0
index cfb545841a2c..228c23eb36d2 100644
@@ -719,6 +719,19 @@ static inline unsigned int cpumask_weight_and(const struct cpumask *srcp1,
 	return bitmap_weight_and(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits);
 }
 
+/**
+ * cpumask_weight_andnot - Count of bits in (*srcp1 & ~*srcp2)
+ * @srcp1: the cpumask to count bits (< nr_cpu_ids) in.
+ * @srcp2: the cpumask to count bits (< nr_cpu_ids) in.
+ *
+ * Return: count of bits set in both *srcp1 and *srcp2
+ */
+static inline unsigned int cpumask_weight_andnot(const struct cpumask *srcp1,
+						const struct cpumask *srcp2)
+{
+	return bitmap_weight_andnot(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits);
+}
+
 /**
  * cpumask_shift_right - *dstp = *srcp >> n
  * @dstp: the cpumask result
lib/bitmap.c +7 -0
index 09522af227f1..b97692854966 100644
@@ -348,6 +348,13 @@ unsigned int __bitmap_weight_and(const unsigned long *bitmap1,
 }
 EXPORT_SYMBOL(__bitmap_weight_and);
 
+unsigned int __bitmap_weight_andnot(const unsigned long *bitmap1,
+				const unsigned long *bitmap2, unsigned int bits)
+{
+	return BITMAP_WEIGHT(bitmap1[idx] & ~bitmap2[idx], bits);
+}
+EXPORT_SYMBOL(__bitmap_weight_andnot);
+
 void __bitmap_set(unsigned long *map, unsigned int start, int len)
 {
 	unsigned long *p = map + BIT_WORD(start);
Signature · 1 line
2.40.1
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] cpumask: add cpumask_weight_andnot()

Quoted 6 lines · Yury Norov

Similarly to cpumask_weight_and(), cpumask_weight_andnot() is a handy helper that may help to avoid creating an intermediate mask just to calculate number of bits that set in a 1st given mask, and clear in 2nd one.

This seems reasonable to me.

Quoted 85 lines
---
 include/linux/bitmap.h  | 12 ++++++++++++
 include/linux/cpumask.h | 13 +++++++++++++
 lib/bitmap.c            |  7 +++++++
 3 files changed, 32 insertions(+)
include/linux/bitmap.h +12 -0
index 99451431e4d6..5814e9ee40ba 100644
@@ -54,6 +54,7 @@ struct device;
  *  bitmap_full(src, nbits)                     Are all bits set in *src?
  *  bitmap_weight(src, nbits)                   Hamming Weight: number set bits
  *  bitmap_weight_and(src1, src2, nbits)        Hamming Weight of and'ed bitmap
+ *  bitmap_weight_andnot(src1, src2, nbits)     Hamming Weight of andnot'ed bitmap
  *  bitmap_set(dst, pos, nbits)                 Set specified bit area
  *  bitmap_clear(dst, pos, nbits)               Clear specified bit area
  *  bitmap_find_next_zero_area(buf, len, pos, n, mask)  Find bit free area
@@ -169,6 +170,8 @@ bool __bitmap_subset(const unsigned long *bitmap1,
 unsigned int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
 unsigned int __bitmap_weight_and(const unsigned long *bitmap1,
 				 const unsigned long *bitmap2, unsigned int nbits);
+unsigned int __bitmap_weight_andnot(const unsigned long *bitmap1,
+				    const unsigned long *bitmap2, unsigned int nbits);
 void __bitmap_set(unsigned long *map, unsigned int start, int len);
 void __bitmap_clear(unsigned long *map, unsigned int start, int len);
 
@@ -425,6 +428,15 @@ unsigned long bitmap_weight_and(const unsigned long *src1,
 	return __bitmap_weight_and(src1, src2, nbits);
 }
 
+static __always_inline
+unsigned long bitmap_weight_andnot(const unsigned long *src1,
+				   const unsigned long *src2, unsigned int nbits)
+{
+	if (small_const_nbits(nbits))
+		return hweight_long(*src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits));
+	return __bitmap_weight_andnot(src1, src2, nbits);
+}
+
 static __always_inline void bitmap_set(unsigned long *map, unsigned int start,
 		unsigned int nbits)
 {
include/linux/cpumask.h +13 -0
index cfb545841a2c..228c23eb36d2 100644
@@ -719,6 +719,19 @@ static inline unsigned int cpumask_weight_and(const struct cpumask *srcp1,
 	return bitmap_weight_and(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits);
 }
 
+/**
+ * cpumask_weight_andnot - Count of bits in (*srcp1 & ~*srcp2)
+ * @srcp1: the cpumask to count bits (< nr_cpu_ids) in.
+ * @srcp2: the cpumask to count bits (< nr_cpu_ids) in.
+ *
+ * Return: count of bits set in both *srcp1 and *srcp2
+ */
+static inline unsigned int cpumask_weight_andnot(const struct cpumask *srcp1,
+						const struct cpumask *srcp2)
+{
+	return bitmap_weight_andnot(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits);
+}
+
 /**
  * cpumask_shift_right - *dstp = *srcp >> n
  * @dstp: the cpumask result
lib/bitmap.c +7 -0
index 09522af227f1..b97692854966 100644
@@ -348,6 +348,13 @@ unsigned int __bitmap_weight_and(const unsigned long *bitmap1,
 }
 EXPORT_SYMBOL(__bitmap_weight_and);
 
+unsigned int __bitmap_weight_andnot(const unsigned long *bitmap1,
+				const unsigned long *bitmap2, unsigned int bits)
+{
+	return BITMAP_WEIGHT(bitmap1[idx] & ~bitmap2[idx], bits);
+}
+EXPORT_SYMBOL(__bitmap_weight_andnot);
+
 void __bitmap_set(unsigned long *map, unsigned int start, int len)
 {
 	unsigned long *p = map + BIT_WORD(start);
author maintainer
#3 lore raw

PATCH 2/3 cpumask: define cleanup function for cpumasks

In reply to Yury Norov

Now we can simplify code that allocates cpumasks for local needs.

---
 include/linux/cpumask.h | 3 +++
 1 file changed, 3 insertions(+)
include/linux/cpumask.h +3 -0
index 228c23eb36d2..1c29947db848 100644
@@ -7,6 +7,7 @@
  * set of CPUs in a system, one bit position per CPU number.  In general,
  * only nr_cpu_ids (<= NR_CPUS) bits are valid.
  */
+#include <linux/cleanup.h>
 #include <linux/kernel.h>
 #include <linux/threads.h>
 #include <linux/bitmap.h>
@@ -990,6 +991,8 @@ static inline bool cpumask_available(cpumask_var_t mask)
 }
 #endif /* CONFIG_CPUMASK_OFFSTACK */
 
+DEFINE_FREE(free_cpumask_var, struct cpumask *, if (_T) free_cpumask_var(_T));
+
 /* It's common to want to use cpu_all_mask in struct member initializers,
  * so it has to refer to an address rather than a pointer. */
 extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
Signature · 1 line
2.40.1
author maintainer
#4 lore raw

PATCH 3/3 net: mana: add a function to spread IRQs per CPUs

In reply to Yury Norov

Souradeep investigated that the driver performs faster if IRQs are spread on CPUs with the following heuristics:

1. No more than one IRQ per CPU, if possible;
2. NUMA locality is the second priority;
3. Sibling dislocality is the last priority.

Let's consider this topology:

Node 0 1
Core 0 1 2 3
CPU 0 1 2 3 4 5 6 7

The most performant IRQ distribution based on the above topology and heuristics may look like this:

IRQ     Nodes   Cores   CPUs
0       1       0       0-1
1       1       1       2-3
2       1       0       0-1
3       1       1       2-3
4       2       2       4-5
5       2       3       6-7
6       2       2       4-5
7       2       3       6-7

The irq_setup() routine introduced in this patch leverages the for_each_numa_hop_mask() iterator and assigns IRQs to sibling groups as described above.

According to [1], for NUMA-aware but sibling-ignorant IRQ distribution based on cpumask_local_spread() performance test results look like this:

./ntttcp -r -m 16
NTTTCP for Linux 1.4.0
---------------------------------------------------------
08:05:20 INFO: 17 threads created
08:05:28 INFO: Network activity progressing...
08:06:28 INFO: Test run completed.
08:06:28 INFO: Test cycle finished.
08:06:28 INFO: ##### Totals: #####

08:06:28 INFO: test duration    :60.00 seconds
08:06:28 INFO: total bytes      :630292053310
08:06:28 INFO:   throughput     :84.04Gbps
08:06:28 INFO:   retrans segs   :4
08:06:28 INFO: cpu cores        :192
08:06:28 INFO:   cpu speed      :3799.725MHz
08:06:28 INFO:   user           :0.05%
08:06:28 INFO:   system         :1.60%
08:06:28 INFO:   idle           :96.41%
08:06:28 INFO:   iowait         :0.00%
08:06:28 INFO:   softirq        :1.94%
08:06:28 INFO:   cycles/byte    :2.50
08:06:28 INFO: cpu busy (all)   :534.41%

For NUMA- and sibling-aware IRQ distribution, the same test works 15% faster:

./ntttcp -r -m 16
NTTTCP for Linux 1.4.0
---------------------------------------------------------
08:08:51 INFO: 17 threads created
08:08:56 INFO: Network activity progressing...
08:09:56 INFO: Test run completed.
08:09:56 INFO: Test cycle finished.
08:09:56 INFO: ##### Totals: #####

08:09:56 INFO: test duration    :60.00 seconds
08:09:56 INFO: total bytes      :741966608384
08:09:56 INFO:   throughput     :98.93Gbps
08:09:56 INFO:   retrans segs   :6
08:09:56 INFO: cpu cores        :192
08:09:56 INFO:   cpu speed      :3799.791MHz
08:09:56 INFO:   user           :0.06%
08:09:56 INFO:   system         :1.81%
08:09:56 INFO:   idle           :96.18%
08:09:56 INFO:   iowait         :0.00%
08:09:56 INFO:   softirq        :1.95%
08:09:56 INFO:   cycles/byte    :2.25
08:09:56 INFO: cpu busy (all)   :569.22%

[1] https://lore.kernel.org/all/20231211063726.GA4977@linuxonhyperv3.guj3yctzbm1etfxqx2vob5hsef.xx.internal.cloudapp.net/

---
 .../net/ethernet/microsoft/mana/gdma_main.c   | 28 +++++++++++++++++++
 1 file changed, 28 insertions(+)
drivers/net/ethernet/microsoft/mana/gdma_main.c +28 -0
index 6367de0c2c2e..11e64e42e3b2 100644
@@ -1243,6 +1243,34 @@ void mana_gd_free_res_map(struct gdma_resource *r)
 	r->size = 0;
 }
 
+static __maybe_unused int irq_setup(unsigned int *irqs, unsigned int len, int node)
+{
+	const struct cpumask *next, *prev = cpu_none_mask;
+	cpumask_var_t cpus __free(free_cpumask_var);
+	int cpu, weight;
+
+	if (!alloc_cpumask_var(&cpus, GFP_KERNEL))
+		return -ENOMEM;
+
+	rcu_read_lock();
+	for_each_numa_hop_mask(next, node) {
+		weight = cpumask_weight_andnot(next, prev);
+		while (weight-- > 0) {
+			cpumask_andnot(cpus, next, prev);
+			for_each_cpu(cpu, cpus) {
+				if (len-- == 0)
+					goto done;
+				irq_set_affinity_and_hint(*irqs++, topology_sibling_cpumask(cpu));
+				cpumask_andnot(cpus, cpus, topology_sibling_cpumask(cpu));
+			}
+		}
+		prev = next;
+	}
+done:
+	rcu_read_unlock();
+	return 0;
+}
+
 static int mana_gd_setup_irqs(struct pci_dev *pdev)
 {
 	unsigned int max_queues_per_port = num_online_cpus();
Signature · 1 line
2.40.1
#5 lore raw

Re: [PATCH 3/3] net: mana: add a function to spread IRQs per CPUs

Quoted 18 lines · Yury Norov
drivers/net/ethernet/microsoft/mana/gdma_main.c +18 -0
+static __maybe_unused int irq_setup(unsigned int *irqs, unsigned int len, int node)
+{
+	const struct cpumask *next, *prev = cpu_none_mask;
+	cpumask_var_t cpus __free(free_cpumask_var);
+	int cpu, weight;
+
+	if (!alloc_cpumask_var(&cpus, GFP_KERNEL))
+		return -ENOMEM;
+
+	rcu_read_lock();
+	for_each_numa_hop_mask(next, node) {
+		weight = cpumask_weight_andnot(next, prev);
+		while (weight-- > 0) {
+			cpumask_andnot(cpus, next, prev);
+			for_each_cpu(cpu, cpus) {
+				if (len-- == 0)
+					goto done;
+				irq_set_affinity_and_hint(*irqs++, topology_sibling_cpumask(cpu));

drivers/net/ethernet/microsoft/mana/gdma_main.c void mana_gd_free_res_map(struct gdma_resource *r) · line 1273

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/gdma_main.c +10 -0
+				cpumask_andnot(cpus, cpus, topology_sibling_cpumask(cpu));
+			}
+		}
+		prev = next;
+	}
+done:
+	rcu_read_unlock();
+	return 0;
+}
+

You're adding a function here but its not called and even marked as __maybe_unused?

Quoted 3 lines

static int mana_gd_setup_irqs(struct pci_dev *pdev) {
unsigned int max_queues_per_port = num_online_cpus();

author maintainer
#6 lore raw

Re: [PATCH 3/3] net: mana: add a function to spread IRQs per CPUs

Quoted 26 lines · Jacob Keller
Quoted 23 lines · Yury Norov
drivers/net/ethernet/microsoft/mana/gdma_main.c +23 -0
+static __maybe_unused int irq_setup(unsigned int *irqs, unsigned int len, int node)
+{
+	const struct cpumask *next, *prev = cpu_none_mask;
+	cpumask_var_t cpus __free(free_cpumask_var);
+	int cpu, weight;
+
+	if (!alloc_cpumask_var(&cpus, GFP_KERNEL))
+		return -ENOMEM;
+
+	rcu_read_lock();
+	for_each_numa_hop_mask(next, node) {
+		weight = cpumask_weight_andnot(next, prev);
+		while (weight-- > 0) {
+			cpumask_andnot(cpus, next, prev);
+			for_each_cpu(cpu, cpus) {
+				if (len-- == 0)
+					goto done;
+				irq_set_affinity_and_hint(*irqs++, topology_sibling_cpumask(cpu));
+				cpumask_andnot(cpus, cpus, topology_sibling_cpumask(cpu));
+			}
+		}
+		prev = next;
+	}
Quoted 8 lines

drivers/net/ethernet/microsoft/mana/gdma_main.c void mana_gd_free_res_map(struct gdma_resource *r) · line 1273

Quoted 5 lines
drivers/net/ethernet/microsoft/mana/gdma_main.c +5 -0
+done:
+	rcu_read_unlock();
+	return 0;
+}
+

You're adding a function here but its not called and even marked as __maybe_unused?

I expect that Souradeep would build his driver improvement on top of this function. cpumask API is somewhat tricky to use it properly here, so this is an attempt help him, instead of moving back and forth on review.

Sorry, I had to be more explicit.

Thanks,
Yury

#7 lore raw

RE: [EXTERNAL] [PATCH 3/3] net: mana: add a function to spread IRQs per CPUs

In reply to Yury Norov
Quoted 110 lines

-----Original Message-----
From: Yury Norov <[email protected]>
Sent: Monday, December 18, 2023 3:02 AM
To: Souradeep Chakrabarti <[email protected]>; KY Srinivasan <[email protected]>; Haiyang Zhang <[email protected]>; [email protected]; Dexuan Cui <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Long Li <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Cc: Souradeep Chakrabarti <[email protected]>; Paul Rosswurm <[email protected]>
Subject: [EXTERNAL] [PATCH 3/3] net: mana: add a function to spread IRQs per CPUs

[Some people who received this message don't often get email from [email protected]. Learn why this is important at
https://aka.ms/LearnAboutSenderIdentification ]

Souradeep investigated that the driver performs faster if IRQs are spread on CPUs with the following heuristics:

1. No more than one IRQ per CPU, if possible; 2. NUMA locality is the second priority; 3. Sibling dislocality is the last priority.

Let's consider this topology:

Node 0 1
Core 0 1 2 3
CPU 0 1 2 3 4 5 6 7

The most performant IRQ distribution based on the above topology and heuristics may look like this:

IRQ     Nodes   Cores   CPUs
0       1       0       0-1
1       1       1       2-3
2       1       0       0-1
3       1       1       2-3
4       2       2       4-5
5       2       3       6-7
6       2       2       4-5
7       2       3       6-7

The irq_setup() routine introduced in this patch leverages the for_each_numa_hop_mask() iterator and assigns IRQs to sibling groups as described above.

According to [1], for NUMA-aware but sibling-ignorant IRQ distribution based on cpumask_local_spread() performance test results look like this:

./ntttcp -r -m 16
NTTTCP for Linux 1.4.0
---------------------------------------------------------
08:05:20 INFO: 17 threads created
08:05:28 INFO: Network activity progressing...
08:06:28 INFO: Test run completed.
08:06:28 INFO: Test cycle finished.
08:06:28 INFO: ##### Totals: #####

08:06:28 INFO: test duration    :60.00 seconds
08:06:28 INFO: total bytes      :630292053310
08:06:28 INFO:   throughput     :84.04Gbps
08:06:28 INFO:   retrans segs   :4
08:06:28 INFO: cpu cores        :192
08:06:28 INFO:   cpu speed      :3799.725MHz
08:06:28 INFO:   user           :0.05%
08:06:28 INFO:   system         :1.60%
08:06:28 INFO:   idle           :96.41%
08:06:28 INFO:   iowait         :0.00%
08:06:28 INFO:   softirq        :1.94%
08:06:28 INFO:   cycles/byte    :2.50
08:06:28 INFO: cpu busy (all)   :534.41%

For NUMA- and sibling-aware IRQ distribution, the same test works 15% faster:

./ntttcp -r -m 16
NTTTCP for Linux 1.4.0
---------------------------------------------------------
08:08:51 INFO: 17 threads created
08:08:56 INFO: Network activity progressing...
08:09:56 INFO: Test run completed.
08:09:56 INFO: Test cycle finished.
08:09:56 INFO: ##### Totals: #####

08:09:56 INFO: test duration    :60.00 seconds
08:09:56 INFO: total bytes      :741966608384
08:09:56 INFO:   throughput     :98.93Gbps
08:09:56 INFO:   retrans segs   :6
08:09:56 INFO: cpu cores        :192
08:09:56 INFO:   cpu speed      :3799.791MHz
08:09:56 INFO:   user           :0.06%
08:09:56 INFO:   system         :1.81%
08:09:56 INFO:   idle           :96.18%
08:09:56 INFO:   iowait         :0.00%
08:09:56 INFO:   softirq        :1.95%
08:09:56 INFO:   cycles/byte    :2.25
08:09:56 INFO: cpu busy (all)   :569.22%

[1]
https://lore.kernel/
.org%2Fall%2F20231211063726.GA4977%40linuxonhyperv3.guj3yctzbm1etfxqx2v ob5hsef.xx.internal.cloudapp.net%2F&data=05%7C02%7Cschakrabarti%40micros oft.com%7Ca385a5a5d661458219c208dbff47a7ab%7C72f988bf86f141af91ab2d7 cd011db47%7C1%7C0%7C638384455520036393%7CUnknown%7CTWFpbGZsb3d 8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D% 7C3000%7C%7C%7C&sdata=kzoalzSu6frB0GIaUM5VWsz04%2FsB%2FBdXwXKb26 IhqkE%3D&reserved=0

Quoted 2 lines

Please also add Signed-off-by: Souradeep Chakrabarti <[email protected]>

Quoted 47 lines

---
.../net/ethernet/microsoft/mana/gdma_main.c | 28 +++++++++++++++++++

1 file changed, 28 insertions(+)
drivers/net/ethernet/microsoft/mana/gdma_main.c +28 -0
index 6367de0c2c2e..11e64e42e3b2 100644
@@ -1243,6 +1243,34 @@ void mana_gd_free_res_map(struct gdma_resource
 *r)
       r->size = 0;
 }
 
+static __maybe_unused int irq_setup(unsigned int *irqs, unsigned int
+len, int node) {
+       const struct cpumask *next, *prev = cpu_none_mask;
+       cpumask_var_t cpus __free(free_cpumask_var);
+       int cpu, weight;
+
+       if (!alloc_cpumask_var(&cpus, GFP_KERNEL))
+               return -ENOMEM;
+
+       rcu_read_lock();
+       for_each_numa_hop_mask(next, node) {
+               weight = cpumask_weight_andnot(next, prev);
+               while (weight-- > 0) {
+                       cpumask_andnot(cpus, next, prev);
+                       for_each_cpu(cpu, cpus) {
+                               if (len-- == 0)
+                                       goto done;
+                               irq_set_affinity_and_hint(*irqs++,
 topology_sibling_cpumask(cpu));
+                               cpumask_andnot(cpus, cpus, topology_sibling_cpumask(cpu));
+                       }
+               }
+               prev = next;
+       }
+done:
+       rcu_read_unlock();
+       return 0;
+}
+
 static int mana_gd_setup_irqs(struct pci_dev *pdev)  {
       unsigned int max_queues_per_port = num_online_cpus();

--
2.40.1

#8 lore raw

RE: [EXTERNAL] [PATCH 3/3] net: mana: add a function to spread IRQs per CPUs

In reply to Yury Norov
Quoted 129 lines

-----Original Message-----
From: Yury Norov <[email protected]>
Sent: Monday, December 18, 2023 3:02 AM
To: Souradeep Chakrabarti <[email protected]>; KY Srinivasan <[email protected]>; Haiyang Zhang <[email protected]>; [email protected]; Dexuan Cui <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Long Li <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Cc: Souradeep Chakrabarti <[email protected]>; Paul Rosswurm <[email protected]>
Subject: [EXTERNAL] [PATCH 3/3] net: mana: add a function to spread IRQs per CPUs

[Some people who received this message don't often get email from [email protected]. Learn why this is important at
https://aka.ms/LearnAboutSenderIdentification ]

Souradeep investigated that the driver performs faster if IRQs are spread on CPUs with the following heuristics:

1. No more than one IRQ per CPU, if possible; 2. NUMA locality is the second priority; 3. Sibling dislocality is the last priority.

Let's consider this topology:

Node 0 1
Core 0 1 2 3
CPU 0 1 2 3 4 5 6 7

The most performant IRQ distribution based on the above topology and heuristics may look like this:

IRQ     Nodes   Cores   CPUs
0       1       0       0-1
1       1       1       2-3
2       1       0       0-1
3       1       1       2-3
4       2       2       4-5
5       2       3       6-7
6       2       2       4-5
7       2       3       6-7

The irq_setup() routine introduced in this patch leverages the for_each_numa_hop_mask() iterator and assigns IRQs to sibling groups as described above.

According to [1], for NUMA-aware but sibling-ignorant IRQ distribution based on cpumask_local_spread() performance test results look like this:

./ntttcp -r -m 16
NTTTCP for Linux 1.4.0
---------------------------------------------------------
08:05:20 INFO: 17 threads created
08:05:28 INFO: Network activity progressing...
08:06:28 INFO: Test run completed.
08:06:28 INFO: Test cycle finished.
08:06:28 INFO: ##### Totals: #####

08:06:28 INFO: test duration    :60.00 seconds
08:06:28 INFO: total bytes      :630292053310
08:06:28 INFO:   throughput     :84.04Gbps
08:06:28 INFO:   retrans segs   :4
08:06:28 INFO: cpu cores        :192
08:06:28 INFO:   cpu speed      :3799.725MHz
08:06:28 INFO:   user           :0.05%
08:06:28 INFO:   system         :1.60%
08:06:28 INFO:   idle           :96.41%
08:06:28 INFO:   iowait         :0.00%
08:06:28 INFO:   softirq        :1.94%
08:06:28 INFO:   cycles/byte    :2.50
08:06:28 INFO: cpu busy (all)   :534.41%

For NUMA- and sibling-aware IRQ distribution, the same test works 15% faster:

./ntttcp -r -m 16
NTTTCP for Linux 1.4.0
---------------------------------------------------------
08:08:51 INFO: 17 threads created
08:08:56 INFO: Network activity progressing...
08:09:56 INFO: Test run completed.
08:09:56 INFO: Test cycle finished.
08:09:56 INFO: ##### Totals: #####

08:09:56 INFO: test duration    :60.00 seconds
08:09:56 INFO: total bytes      :741966608384
08:09:56 INFO:   throughput     :98.93Gbps
08:09:56 INFO:   retrans segs   :6
08:09:56 INFO: cpu cores        :192
08:09:56 INFO:   cpu speed      :3799.791MHz
08:09:56 INFO:   user           :0.06%
08:09:56 INFO:   system         :1.81%
08:09:56 INFO:   idle           :96.18%
08:09:56 INFO:   iowait         :0.00%
08:09:56 INFO:   softirq        :1.95%
08:09:56 INFO:   cycles/byte    :2.25
08:09:56 INFO: cpu busy (all)   :569.22%

[1]
https://lore.kernel/
.org%2Fall%2F20231211063726.GA4977%40linuxonhyperv3.guj3yctzbm1etfxqx2v ob5hsef.xx.internal.cloudapp.net%2F&data=05%7C02%7Cschakrabarti%40micros oft.com%7Ca385a5a5d661458219c208dbff47a7ab%7C72f988bf86f141af91ab2d7 cd011db47%7C1%7C0%7C638384455520036393%7CUnknown%7CTWFpbGZsb3d 8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D% 7C3000%7C%7C%7C&sdata=kzoalzSu6frB0GIaUM5VWsz04%2FsB%2FBdXwXKb26 IhqkE%3D&reserved=0

---
.../net/ethernet/microsoft/mana/gdma_main.c | 28 +++++++++++++++++++

1 file changed, 28 insertions(+)
drivers/net/ethernet/microsoft/mana/gdma_main.c +3 -0
index 6367de0c2c2e..11e64e42e3b2 100644
@@ -1243,6 +1243,34 @@ void mana_gd_free_res_map(struct gdma_resource
 *r)
       r->size = 0;
 }
 
+static __maybe_unused int irq_setup(unsigned int *irqs, unsigned int
+len, int node) {
+       const struct cpumask *next, *prev = cpu_none_mask;

drivers/net/ethernet/microsoft/mana/gdma_main.c void mana_gd_free_res_map(struct gdma_resource · line 1259

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/gdma_main.c +10 -0
+       cpumask_var_t cpus __free(free_cpumask_var);
+       int cpu, weight;
+
+       if (!alloc_cpumask_var(&cpus, GFP_KERNEL))
+               return -ENOMEM;
+
+       rcu_read_lock();
+       for_each_numa_hop_mask(next, node) {
+               weight = cpumask_weight_andnot(next, prev);
+               while (weight-- > 0) {

Make it while (weight > 0) {

drivers/net/ethernet/microsoft/mana/gdma_main.c void mana_gd_free_res_map(struct gdma_resource *r) · line 1264

Quoted 7 lines
drivers/net/ethernet/microsoft/mana/gdma_main.c +6 -0
+                       cpumask_andnot(cpus, next, prev);
+                       for_each_cpu(cpu, cpus) {
+                               if (len-- == 0)
+                                       goto done;
+                               irq_set_affinity_and_hint(*irqs++,
 topology_sibling_cpumask(cpu));
+                               cpumask_andnot(cpus, cpus, topology_sibling_cpumask(cpu));

Here do --weight, else this code will traverse the same node N^2 times, where each node has N cpus .

Quoted 13 lines
drivers/net/ethernet/microsoft/mana/gdma_main.c +9 -0
+                       }
+               }
+               prev = next;
+       }
+done:
+       rcu_read_unlock();
+       return 0;
+}
+
 static int mana_gd_setup_irqs(struct pci_dev *pdev)  {
       unsigned int max_queues_per_port = num_online_cpus();

--
2.40.1

author maintainer
#9 lore raw

Re: [EXTERNAL] [PATCH 3/3] net: mana: add a function to spread IRQs per CPUs

Quoted 147 lines · Souradeep Chakrabarti
Quoted 129 lines

-----Original Message-----
From: Yury Norov <[email protected]>
Sent: Monday, December 18, 2023 3:02 AM
To: Souradeep Chakrabarti <[email protected]>; KY Srinivasan <[email protected]>; Haiyang Zhang <[email protected]>; [email protected]; Dexuan Cui <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Long Li <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Cc: Souradeep Chakrabarti <[email protected]>; Paul Rosswurm <[email protected]>
Subject: [EXTERNAL] [PATCH 3/3] net: mana: add a function to spread IRQs per CPUs

[Some people who received this message don't often get email from [email protected]. Learn why this is important at
https://aka.ms/LearnAboutSenderIdentification ]

Souradeep investigated that the driver performs faster if IRQs are spread on CPUs with the following heuristics:

1. No more than one IRQ per CPU, if possible; 2. NUMA locality is the second priority; 3. Sibling dislocality is the last priority.

Let's consider this topology:

Node 0 1
Core 0 1 2 3
CPU 0 1 2 3 4 5 6 7

The most performant IRQ distribution based on the above topology and heuristics may look like this:

IRQ     Nodes   Cores   CPUs
0       1       0       0-1
1       1       1       2-3
2       1       0       0-1
3       1       1       2-3
4       2       2       4-5
5       2       3       6-7
6       2       2       4-5
7       2       3       6-7

The irq_setup() routine introduced in this patch leverages the for_each_numa_hop_mask() iterator and assigns IRQs to sibling groups as described above.

According to [1], for NUMA-aware but sibling-ignorant IRQ distribution based on cpumask_local_spread() performance test results look like this:

./ntttcp -r -m 16
NTTTCP for Linux 1.4.0
---------------------------------------------------------
08:05:20 INFO: 17 threads created
08:05:28 INFO: Network activity progressing...
08:06:28 INFO: Test run completed.
08:06:28 INFO: Test cycle finished.
08:06:28 INFO: ##### Totals: #####

08:06:28 INFO: test duration    :60.00 seconds
08:06:28 INFO: total bytes      :630292053310
08:06:28 INFO:   throughput     :84.04Gbps
08:06:28 INFO:   retrans segs   :4
08:06:28 INFO: cpu cores        :192
08:06:28 INFO:   cpu speed      :3799.725MHz
08:06:28 INFO:   user           :0.05%
08:06:28 INFO:   system         :1.60%
08:06:28 INFO:   idle           :96.41%
08:06:28 INFO:   iowait         :0.00%
08:06:28 INFO:   softirq        :1.94%
08:06:28 INFO:   cycles/byte    :2.50
08:06:28 INFO: cpu busy (all)   :534.41%

For NUMA- and sibling-aware IRQ distribution, the same test works 15% faster:

./ntttcp -r -m 16
NTTTCP for Linux 1.4.0
---------------------------------------------------------
08:08:51 INFO: 17 threads created
08:08:56 INFO: Network activity progressing...
08:09:56 INFO: Test run completed.
08:09:56 INFO: Test cycle finished.
08:09:56 INFO: ##### Totals: #####

08:09:56 INFO: test duration    :60.00 seconds
08:09:56 INFO: total bytes      :741966608384
08:09:56 INFO:   throughput     :98.93Gbps
08:09:56 INFO:   retrans segs   :6
08:09:56 INFO: cpu cores        :192
08:09:56 INFO:   cpu speed      :3799.791MHz
08:09:56 INFO:   user           :0.06%
08:09:56 INFO:   system         :1.81%
08:09:56 INFO:   idle           :96.18%
08:09:56 INFO:   iowait         :0.00%
08:09:56 INFO:   softirq        :1.95%
08:09:56 INFO:   cycles/byte    :2.25
08:09:56 INFO: cpu busy (all)   :569.22%

[1]
https://lore.kernel/
.org%2Fall%2F20231211063726.GA4977%40linuxonhyperv3.guj3yctzbm1etfxqx2v ob5hsef.xx.internal.cloudapp.net%2F&data=05%7C02%7Cschakrabarti%40micros oft.com%7Ca385a5a5d661458219c208dbff47a7ab%7C72f988bf86f141af91ab2d7 cd011db47%7C1%7C0%7C638384455520036393%7CUnknown%7CTWFpbGZsb3d 8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D% 7C3000%7C%7C%7C&sdata=kzoalzSu6frB0GIaUM5VWsz04%2FsB%2FBdXwXKb26 IhqkE%3D&reserved=0

---
.../net/ethernet/microsoft/mana/gdma_main.c | 28 +++++++++++++++++++

1 file changed, 28 insertions(+)
drivers/net/ethernet/microsoft/mana/gdma_main.c +3 -0
index 6367de0c2c2e..11e64e42e3b2 100644
@@ -1243,6 +1243,34 @@ void mana_gd_free_res_map(struct gdma_resource
 *r)
       r->size = 0;
 }
 
+static __maybe_unused int irq_setup(unsigned int *irqs, unsigned int
+len, int node) {
+       const struct cpumask *next, *prev = cpu_none_mask;

drivers/net/ethernet/microsoft/mana/gdma_main.c void mana_gd_free_res_map(struct gdma_resource · line 1259

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/gdma_main.c +10 -0
+       cpumask_var_t cpus __free(free_cpumask_var);
+       int cpu, weight;
+
+       if (!alloc_cpumask_var(&cpus, GFP_KERNEL))
+               return -ENOMEM;
+
+       rcu_read_lock();
+       for_each_numa_hop_mask(next, node) {
+               weight = cpumask_weight_andnot(next, prev);
+               while (weight-- > 0) {

Make it while (weight > 0) {

drivers/net/ethernet/microsoft/mana/gdma_main.c void mana_gd_free_res_map(struct gdma_resource *r) · line 1264

Quoted 7 lines
drivers/net/ethernet/microsoft/mana/gdma_main.c +6 -0
+                       cpumask_andnot(cpus, next, prev);
+                       for_each_cpu(cpu, cpus) {
+                               if (len-- == 0)
+                                       goto done;
+                               irq_set_affinity_and_hint(*irqs++,
 topology_sibling_cpumask(cpu));
+                               cpumask_andnot(cpus, cpus, topology_sibling_cpumask(cpu));
Quoted 2 lines

Here do --weight, else this code will traverse the same node N^2 times, where each node has N cpus .

Sure.

When building your series on top of this, can you please fix it inplace?

Thanks,
Yury

Quoted 13 lines
Quoted 13 lines
drivers/net/ethernet/microsoft/mana/gdma_main.c +9 -0
+                       }
+               }
+               prev = next;
+       }
+done:
+       rcu_read_unlock();
+       return 0;
+}
+
 static int mana_gd_setup_irqs(struct pci_dev *pdev)  {
       unsigned int max_queues_per_port = num_online_cpus();

--
2.40.1

#10 lore raw
In reply to Yury Norov
Quoted 8 lines · Yury Norov

Add irq_spread() function that makes the driver working 15% faster than with cpumask_local_spread()

Yury Norov (3):
cpumask: add cpumask_weight_andnot()
cpumask: define cleanup function for cpumasks
net: mana: add a function to spread IRQs per CPUs

Quoted 6 lines
 .../net/ethernet/microsoft/mana/gdma_main.c   | 28 +++++++++++++++++++
 include/linux/bitmap.h                        | 12 ++++++++
 include/linux/cpumask.h                       | 17 +++++++++++
 lib/bitmap.c                                  |  7 +++++
 4 files changed, 64 insertions(+)

Process would be to tag this for which tree it aims for. Since this looks like an improvement and includes cleanup and changes to add a new helper function that makes me think it would be net-next.

Thanks,
Jake