Skip to contentSkip to conversation

virtio: Fix affinity for >32 VCPUs

PATCH Superseded Ben Serebrin 6 participants 11 messages Feb 3, 2017 – Feb 6, 2017

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio: Fix affinity for >32 VCPUs

From: Benjamin Serebrin <[email protected]>

If the number of virtio queue pairs is not equal to the number of VCPUs, the virtio guest driver doesn't assign any CPU affinity for the queue interrupts or the xps aggregation interrupt.

Google Compute Engine currently provides 1 queue pair for every VCPU, but limits that at a maximum of 32 queue pairs.

This code assigns interrupt affinity even when there are more than 32 VCPUs.

Tested:

(on a 64-VCPU VM with debian 8, jessie-backports 4.9.2)

Without the fix we see all queues affinitized to all CPUs:

cd /proc/irq
for i in seq 24 92 ; do sudo grep ".*" $i/smp_affinity_list; done 0-63
[...]
0-63

and we see all TX queues' xps_cpus affinitzed to no cores:

for i in seq 0 31 ; do sudo grep ".*" tx-$i/xps_cpus; done 00000000,00000000
[...]
00000000,00000000

With the fix, we see each queue assigned to the a single core, and xps affinity set to 1 unique cpu per TX queue.

64 VCPU:

cd /proc/irq
for i in seq 24 92 ; do sudo grep ".*" $i/smp_affinity_list; done

0-63
0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15
15
16
16
17
17
18
18
19
19
20
20
21
21
22
22
23
23
24
24
25
25
26
26
27
27
28
28
29
29
30
30
31
31
0-63
0-63
0-63
0-63

cd /sys/class/net/eth0/queues
for i in seq 0 31 ; do sudo grep ".*" tx-$i/xps_cpus; done

00000001,00000001
00000002,00000002
00000004,00000004
00000008,00000008
00000010,00000010
00000020,00000020
00000040,00000040
00000080,00000080
00000100,00000100
00000200,00000200
00000400,00000400
00000800,00000800
00001000,00001000
00002000,00002000
00004000,00004000
00008000,00008000
00010000,00010000
00020000,00020000
00040000,00040000
00080000,00080000
00100000,00100000
00200000,00200000
00400000,00400000
00800000,00800000
01000000,01000000
02000000,02000000
04000000,04000000
08000000,08000000
10000000,10000000
20000000,20000000
40000000,40000000
80000000,80000000

48 VCPU:

cd /proc/irq
for i in seq 24 92 ; do sudo grep ".*" $i/smp_affinity_list; done 0-47
0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15
15
16
16
17
17
18
18
19
19
20
20
21
21
22
22
23
23
24
24
25
25
26
26
27
27
28
28
29
29
30
30
31
31
0-47
0-47
0-47
0-47

cd /sys/class/net/eth0/queues
for i in seq 0 31 ; do sudo grep ".*" tx-$i/xps_cpus; done

0001,00000001
0002,00000002
0004,00000004
0008,00000008
0010,00000010
0020,00000020
0040,00000040
0080,00000080
0100,00000100
0200,00000200
0400,00000400
0800,00000800
1000,00001000
2000,00002000
4000,00004000
8000,00008000
0000,00010000
0000,00020000
0000,00040000
0000,00080000
0000,00100000
0000,00200000
0000,00400000
0000,00800000
0000,01000000
0000,02000000
0000,04000000
0000,08000000
0000,10000000
0000,20000000
0000,40000000
0000,80000000

Effort: kvm

---
 drivers/net/virtio_net.c | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)
drivers/net/virtio_net.c +27 -3
index 765c2d6358da..0dc3a102bfc4 100644
@@ -1502,20 +1502,44 @@ static void virtnet_set_affinity(struct virtnet_info *vi)
 	 * queue pairs, we let the queue pairs to be private to one cpu by
 	 * setting the affinity hint to eliminate the contention.
 	 */
-	if (vi->curr_queue_pairs == 1 ||
-	    vi->max_queue_pairs != num_online_cpus()) {
+	if (vi->curr_queue_pairs == 1) {
 		virtnet_clean_affinity(vi, -1);
 		return;
 	}
 
+	/* If there are more cpus than queues, then assign the queues'
+	 * interrupts to the first cpus until we run out.
+	 */
 	i = 0;
 	for_each_online_cpu(cpu) {
+		if (i == vi->max_queue_pairs)
+			break;
 		virtqueue_set_affinity(vi->rq[i].vq, cpu);
 		virtqueue_set_affinity(vi->sq[i].vq, cpu);
-		netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
 		i++;
 	}
 
+	/* Stripe the XPS affinities across the online CPUs.
+	 * Hyperthread pairs are typically assigned such that Linux's
+	 * CPU X and X + (numcpus / 2) are hyperthread twins, so we cause
+	 * hyperthread twins to share TX queues, in the case where there are
+	 * more cpus than queues.
+	 */
+	for (i = 0; i < vi->max_queue_pairs; i++) {
+		struct cpumask mask;
+		int skip = i;
+
+		cpumask_clear(&mask);
+		for_each_online_cpu(cpu) {
+			while (skip--)
+				cpu = cpumask_next(cpu, cpu_online_mask);
+			if (cpu < num_possible_cpus())
+				cpumask_set_cpu(cpu, &mask);
+			skip = vi->max_queue_pairs - 1;
+		}
+		netif_set_xps_queue(vi->dev, &mask, i);
+	}
+
 	vi->affinity_hint_set = true;
 }
 
#1 lore raw
Quoted 256 lines · Ben Serebrin

From: Benjamin Serebrin <[email protected]>

If the number of virtio queue pairs is not equal to the number of VCPUs, the virtio guest driver doesn't assign any CPU affinity for the queue interrupts or the xps aggregation interrupt.

Google Compute Engine currently provides 1 queue pair for every VCPU, but limits that at a maximum of 32 queue pairs.

This code assigns interrupt affinity even when there are more than 32 VCPUs.

Tested:

(on a 64-VCPU VM with debian 8, jessie-backports 4.9.2)

Without the fix we see all queues affinitized to all CPUs:

cd /proc/irq
for i in seq 24 92 ; do sudo grep ".*" $i/smp_affinity_list; done 0-63
[...]
0-63

and we see all TX queues' xps_cpus affinitzed to no cores:

for i in seq 0 31 ; do sudo grep ".*" tx-$i/xps_cpus; done 00000000,00000000
[...]
00000000,00000000

With the fix, we see each queue assigned to the a single core, and xps affinity set to 1 unique cpu per TX queue.

64 VCPU:

cd /proc/irq
for i in seq 24 92 ; do sudo grep ".*" $i/smp_affinity_list; done

0-63
0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15
15
16
16
17
17
18
18
19
19
20
20
21
21
22
22
23
23
24
24
25
25
26
26
27
27
28
28
29
29
30
30
31
31
0-63
0-63
0-63
0-63

cd /sys/class/net/eth0/queues
for i in seq 0 31 ; do sudo grep ".*" tx-$i/xps_cpus; done

00000001,00000001
00000002,00000002
00000004,00000004
00000008,00000008
00000010,00000010
00000020,00000020
00000040,00000040
00000080,00000080
00000100,00000100
00000200,00000200
00000400,00000400
00000800,00000800
00001000,00001000
00002000,00002000
00004000,00004000
00008000,00008000
00010000,00010000
00020000,00020000
00040000,00040000
00080000,00080000
00100000,00100000
00200000,00200000
00400000,00400000
00800000,00800000
01000000,01000000
02000000,02000000
04000000,04000000
08000000,08000000
10000000,10000000
20000000,20000000
40000000,40000000
80000000,80000000

48 VCPU:

cd /proc/irq
for i in seq 24 92 ; do sudo grep ".*" $i/smp_affinity_list; done 0-47
0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15
15
16
16
17
17
18
18
19
19
20
20
21
21
22
22
23
23
24
24
25
25
26
26
27
27
28
28
29
29
30
30
31
31
0-47
0-47
0-47
0-47

cd /sys/class/net/eth0/queues
for i in seq 0 31 ; do sudo grep ".*" tx-$i/xps_cpus; done

0001,00000001
0002,00000002
0004,00000004
0008,00000008
0010,00000010
0020,00000020
0040,00000040
0080,00000080
0100,00000100
0200,00000200
0400,00000400
0800,00000800
1000,00001000
2000,00002000
4000,00004000
8000,00008000
0000,00010000
0000,00020000
0000,00040000
0000,00080000
0000,00100000
0000,00200000
0000,00400000
0000,00800000
0000,01000000
0000,02000000
0000,04000000
0000,08000000
0000,10000000
0000,20000000
0000,40000000
0000,80000000

Quoted 4 lines

I wonder why not just do it in userspace though. It would be nice to mention this in the commit log. Are we sure this distribution is best for all workloads? While irqbalancer is hardly a perfect oracle it does manage to balance the load somewhat, and IIUC kernel affinities would break that.
Thoughts?

Quoted 57 lines

Effort: kvm

---
 drivers/net/virtio_net.c | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)
drivers/net/virtio_net.c +27 -3
index 765c2d6358da..0dc3a102bfc4 100644
@@ -1502,20 +1502,44 @@ static void virtnet_set_affinity(struct virtnet_info *vi)
 	 * queue pairs, we let the queue pairs to be private to one cpu by
 	 * setting the affinity hint to eliminate the contention.
 	 */
-	if (vi->curr_queue_pairs == 1 ||
-	    vi->max_queue_pairs != num_online_cpus()) {
+	if (vi->curr_queue_pairs == 1) {
 		virtnet_clean_affinity(vi, -1);
 		return;
 	}
 
+	/* If there are more cpus than queues, then assign the queues'
+	 * interrupts to the first cpus until we run out.
+	 */
 	i = 0;
 	for_each_online_cpu(cpu) {
+		if (i == vi->max_queue_pairs)
+			break;
 		virtqueue_set_affinity(vi->rq[i].vq, cpu);
 		virtqueue_set_affinity(vi->sq[i].vq, cpu);
-		netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
 		i++;
 	}
 
+	/* Stripe the XPS affinities across the online CPUs.
+	 * Hyperthread pairs are typically assigned such that Linux's
+	 * CPU X and X + (numcpus / 2) are hyperthread twins, so we cause
+	 * hyperthread twins to share TX queues, in the case where there are
+	 * more cpus than queues.
+	 */
+	for (i = 0; i < vi->max_queue_pairs; i++) {
+		struct cpumask mask;
+		int skip = i;
+
+		cpumask_clear(&mask);
+		for_each_online_cpu(cpu) {
+			while (skip--)
+				cpu = cpumask_next(cpu, cpu_online_mask);
+			if (cpu < num_possible_cpus())
+				cpumask_set_cpu(cpu, &mask);
+			skip = vi->max_queue_pairs - 1;
+		}
+		netif_set_xps_queue(vi->dev, &mask, i);
+	}
+
 	vi->affinity_hint_set = true;
 }
 
#2 lore raw

Thanks, Michael, I'll put this text in the commit log:

XPS settings aren't write-able from userspace, so the only way I know to fix XPS is in the driver.

The interrupt affinity settings could be set by irqbalancer, yes, but it's not always enabled
(by intention) in all guests. I just confirmed that on an unpatched 64VCPU VM with no irqbalancer,
all virtio-net interrupts end up on core 0, and on a patched 64VCPU, interrupts end up on the core whose
number matches the queue number. This patch is just extending the existing behavior in the interrupt affinity
assignment loop, and I think it stands on its own.

It actually sparked some internal discussion about further intelligence in XPS and interrupt affinity, and we'll do some experiments and come back with further patches.

Quoted 328 lines · Michael S. Tsirkin
Quoted 256 lines · Ben Serebrin

From: Benjamin Serebrin <[email protected]>

If the number of virtio queue pairs is not equal to the number of VCPUs, the virtio guest driver doesn't assign any CPU affinity for the queue interrupts or the xps aggregation interrupt.

Google Compute Engine currently provides 1 queue pair for every VCPU, but limits that at a maximum of 32 queue pairs.

This code assigns interrupt affinity even when there are more than 32 VCPUs.

Tested:

(on a 64-VCPU VM with debian 8, jessie-backports 4.9.2)

Without the fix we see all queues affinitized to all CPUs:

cd /proc/irq
for i in seq 24 92 ; do sudo grep ".*" $i/smp_affinity_list; done 0-63
[...]
0-63

and we see all TX queues' xps_cpus affinitzed to no cores:

for i in seq 0 31 ; do sudo grep ".*" tx-$i/xps_cpus; done 00000000,00000000
[...]
00000000,00000000

With the fix, we see each queue assigned to the a single core, and xps affinity set to 1 unique cpu per TX queue.

64 VCPU:

cd /proc/irq
for i in seq 24 92 ; do sudo grep ".*" $i/smp_affinity_list; done

0-63
0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15
15
16
16
17
17
18
18
19
19
20
20
21
21
22
22
23
23
24
24
25
25
26
26
27
27
28
28
29
29
30
30
31
31
0-63
0-63
0-63
0-63

cd /sys/class/net/eth0/queues
for i in seq 0 31 ; do sudo grep ".*" tx-$i/xps_cpus; done

00000001,00000001
00000002,00000002
00000004,00000004
00000008,00000008
00000010,00000010
00000020,00000020
00000040,00000040
00000080,00000080
00000100,00000100
00000200,00000200
00000400,00000400
00000800,00000800
00001000,00001000
00002000,00002000
00004000,00004000
00008000,00008000
00010000,00010000
00020000,00020000
00040000,00040000
00080000,00080000
00100000,00100000
00200000,00200000
00400000,00400000
00800000,00800000
01000000,01000000
02000000,02000000
04000000,04000000
08000000,08000000
10000000,10000000
20000000,20000000
40000000,40000000
80000000,80000000

48 VCPU:

cd /proc/irq
for i in seq 24 92 ; do sudo grep ".*" $i/smp_affinity_list; done 0-47
0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15
15
16
16
17
17
18
18
19
19
20
20
21
21
22
22
23
23
24
24
25
25
26
26
27
27
28
28
29
29
30
30
31
31
0-47
0-47
0-47
0-47

cd /sys/class/net/eth0/queues
for i in seq 0 31 ; do sudo grep ".*" tx-$i/xps_cpus; done

0001,00000001
0002,00000002
0004,00000004
0008,00000008
0010,00000010
0020,00000020
0040,00000040
0080,00000080
0100,00000100
0200,00000200
0400,00000400
0800,00000800
1000,00001000
2000,00002000
4000,00004000
8000,00008000
0000,00010000
0000,00020000
0000,00040000
0000,00080000
0000,00100000
0000,00200000
0000,00400000
0000,00800000
0000,01000000
0000,02000000
0000,04000000
0000,08000000
0000,10000000
0000,20000000
0000,40000000
0000,80000000

Quoted 4 lines

I wonder why not just do it in userspace though. It would be nice to mention this in the commit log. Are we sure this distribution is best for all workloads? While irqbalancer is hardly a perfect oracle it does manage to balance the load somewhat, and IIUC kernel affinities would break that.
Thoughts?

Quoted 57 lines

Effort: kvm

---
 drivers/net/virtio_net.c | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)
drivers/net/virtio_net.c +27 -3
index 765c2d6358da..0dc3a102bfc4 100644
@@ -1502,20 +1502,44 @@ static void virtnet_set_affinity(struct virtnet_info *vi)
       * queue pairs, we let the queue pairs to be private to one cpu by
       * setting the affinity hint to eliminate the contention.
       */
-     if (vi->curr_queue_pairs == 1 ||
-         vi->max_queue_pairs != num_online_cpus()) {
+     if (vi->curr_queue_pairs == 1) {
              virtnet_clean_affinity(vi, -1);
              return;
      }
 
+     /* If there are more cpus than queues, then assign the queues'
+      * interrupts to the first cpus until we run out.
+      */
      i = 0;
      for_each_online_cpu(cpu) {
+             if (i == vi->max_queue_pairs)
+                     break;
              virtqueue_set_affinity(vi->rq[i].vq, cpu);
              virtqueue_set_affinity(vi->sq[i].vq, cpu);
-             netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
              i++;
      }
 
+     /* Stripe the XPS affinities across the online CPUs.
+      * Hyperthread pairs are typically assigned such that Linux's
+      * CPU X and X + (numcpus / 2) are hyperthread twins, so we cause
+      * hyperthread twins to share TX queues, in the case where there are
+      * more cpus than queues.
+      */
+     for (i = 0; i < vi->max_queue_pairs; i++) {
+             struct cpumask mask;
+             int skip = i;
+
+             cpumask_clear(&mask);
+             for_each_online_cpu(cpu) {
+                     while (skip--)
+                             cpu = cpumask_next(cpu, cpu_online_mask);
+                     if (cpu < num_possible_cpus())
+                             cpumask_set_cpu(cpu, &mask);
+                     skip = vi->max_queue_pairs - 1;
+             }
+             netif_set_xps_queue(vi->dev, &mask, i);
+     }
+
      vi->affinity_hint_set = true;
 }
 
#3 lore raw
Quoted 2 lines

XPS settings aren't write-able from userspace, so the only way I know to fix XPS is in the driver.

It is writable by privileged users.

Quoted 1 line
Quoted 1 line

I wonder why not just do it in userspace though.

Configuring interrupts and xps from userspace at boot is more robust, as device driver defaults can change. But especially for customers who are unaware of these settings, choosing sane defaults won't hurt.

Quoted 2 lines
Quoted 2 lines

It would be nice to mention this in the commit log. Are we sure this distribution is best for all workloads?

Overall, this minimizes contention between cores, so it should be broadly preferable.

#4 lore raw
Quoted 3 lines · Willem de Bruijn

Configuring interrupts and xps from userspace at boot is more robust, as device driver defaults can change. But especially for customers who are unaware of these settings, choosing sane defaults won't hurt.

The devil is in finding the sane defaults. For example, the issues we've seen with VMs sending traffic getting reordered when the driver took it upon itself to enable xps.

rick jones

#5 lore raw
Quoted 8 lines · Rick Jones
Quoted 4 lines · Willem de Bruijn

Configuring interrupts and xps from userspace at boot is more robust, as device driver defaults can change. But especially for customers who are unaware of these settings, choosing sane defaults won't hurt.

The devil is in finding the sane defaults.

Agreed, of course.

Quoted 3 lines

For example, the issues we've
seen with VMs sending traffic getting reordered when the driver took it upon itself to enable xps.

That particular reordering issue [1] was with xps on the host, where vm thread migration causes flows to exit on different tx queues because ooo_okay is not supported for flows from tap devices.

Within a guest, on the other hand, xps settings should not cause reordering of packets from the same flow, as ooo_okay is active for tcp flows.

[1] https://patchwork.ozlabs.org/patch/662913/

#6 lore raw
In reply to Benjamin Serebrin
Quoted 4 lines · Benjamin Serebrin

Thanks, Michael, I'll put this text in the commit log:

XPS settings aren't write-able from userspace, so the only way I know to fix XPS is in the driver.

??

root@np-cp1-c0-m1-mgmt:/home/stack# cat /sys/devices/pci0000:00/0000:00:02.0/0000:04:00.0/net/hed0/queues/tx-0/xps_cpus
00000000,00000001
root@np-cp1-c0-m1-mgmt:/home/stack# echo 0 > /sys/devices/pci0000:00/0000:00:02.0/0000:04:00.0/net/hed0/queues/tx-0/xps_cpus
root@np-cp1-c0-m1-mgmt:/home/stack# cat /sys/devices/pci0000:00/0000:00:02.0/0000:04:00.0/net/hed0/queues/tx-0/xps_cpus
00000000,00000000
#7 lore raw
In reply to Ben Serebrin
Quoted 6 lines · Ben Serebrin

From: Benjamin Serebrin <[email protected]>

If the number of virtio queue pairs is not equal to the number of VCPUs, the virtio guest driver doesn't assign any CPU affinity for the queue interrupts or the xps aggregation interrupt.

So this in fact is not a affinity fixing for #cpus > 32 but adding affinity for #cpus != #queue pairs.

Quoted 11 lines

Google Compute Engine currently provides 1 queue pair for every VCPU, but limits that at a maximum of 32 queue pairs.

This code assigns interrupt affinity even when there are more than 32 VCPUs.

Tested:

(on a 64-VCPU VM with debian 8, jessie-backports 4.9.2)

Without the fix we see all queues affinitized to all CPUs:

[...]

Quoted 9 lines
drivers/net/virtio_net.c +5 -0
+	/* If there are more cpus than queues, then assign the queues'
+	 * interrupts to the first cpus until we run out.
+	 */
  	i = 0;
  	for_each_online_cpu(cpu) {
+		if (i == vi->max_queue_pairs)
+			break;
  		virtqueue_set_affinity(vi->rq[i].vq, cpu);

drivers/net/virtio_net.c static void virtnet_set_affinity(struct virtnet_info *vi) · line 1526

Quoted 10 lines
drivers/net/virtio_net.c +5 -1
  		virtqueue_set_affinity(vi->sq[i].vq, cpu);
-		netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
  		i++;
  	}
 
+	/* Stripe the XPS affinities across the online CPUs.
+	 * Hyperthread pairs are typically assigned such that Linux's
+	 * CPU X and X + (numcpus / 2) are hyperthread twins, so we cause
+	 * hyperthread twins to share TX queues, in the case where there are
+	 * more cpus than queues.

Since we use combined queue pairs, why not use the same policy for RX?

Thanks

Quoted 20 lines
drivers/net/virtio_net.c +16 -0
+	 */
+	for (i = 0; i < vi->max_queue_pairs; i++) {
+		struct cpumask mask;
+		int skip = i;
+
+		cpumask_clear(&mask);
+		for_each_online_cpu(cpu) {
+			while (skip--)
+				cpu = cpumask_next(cpu, cpu_online_mask);
+			if (cpu < num_possible_cpus())
+				cpumask_set_cpu(cpu, &mask);
+			skip = vi->max_queue_pairs - 1;
+		}
+		netif_set_xps_queue(vi->dev, &mask, i);
+	}
+
  	vi->affinity_hint_set = true;
  }
#8 lore raw
Quoted 6 lines · Jason Wang
Quoted 3 lines · Ben Serebrin

From: Benjamin Serebrin <[email protected]>

Quoted 8 lines
Quoted 4 lines

If the number of virtio queue pairs is not equal to the number of VCPUs, the virtio guest driver doesn't assign any CPU affinity for the queue interrupts or the xps aggregation interrupt.

So this in fact is not a affinity fixing for #cpus > 32 but adding affinity for #cpus != #queue pairs.

Fair enough. I'll adjust the title line in the subsequent version.

Quoted 36 lines
Quoted 12 lines

Google Compute Engine currently provides 1 queue pair for every VCPU, but limits that at a maximum of 32 queue pairs.

This code assigns interrupt affinity even when there are more than 32 VCPUs.

Tested:

(on a 64-VCPU VM with debian 8, jessie-backports 4.9.2)

Without the fix we see all queues affinitized to all CPUs:

[...]

Quoted 16 lines

+ /* If there are more cpus than queues, then assign the queues'

drivers/net/virtio_net.c +7 -1
+        * interrupts to the first cpus until we run out.
+        */
        i = 0;
        for_each_online_cpu(cpu) {
+               if (i == vi->max_queue_pairs)
+                       break;
                virtqueue_set_affinity(vi->rq[i].vq, cpu);
                virtqueue_set_affinity(vi->sq[i].vq, cpu);
-               netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
                i++;
        }
  +     /* Stripe the XPS affinities across the online CPUs.
+        * Hyperthread pairs are typically assigned such that Linux's
+        * CPU X and X + (numcpus / 2) are hyperthread twins, so we cause
+        * hyperthread twins to share TX queues, in the case where there
Quoted 2 lines

are
+ * more cpus than queues.

Quoted 1 line

Since we use combined queue pairs, why not use the same policy for RX?

XPS is for transmit only.

Quoted 24 lines

Thanks

Quoted 19 lines
drivers/net/virtio_net.c +16 -0
+        */
+       for (i = 0; i < vi->max_queue_pairs; i++) {
+               struct cpumask mask;
+               int skip = i;
+
+               cpumask_clear(&mask);
+               for_each_online_cpu(cpu) {
+                       while (skip--)
+                               cpu = cpumask_next(cpu, cpu_online_mask);
+                       if (cpu < num_possible_cpus())
+                               cpumask_set_cpu(cpu, &mask);
+                       skip = vi->max_queue_pairs - 1;
+               }
+               netif_set_xps_queue(vi->dev, &mask, i);
+       }
+
        vi->affinity_hint_set = true;
  }
#9 lore raw
Quoted 44 lines · Benjamin Serebrin
Quoted 9 lines · Jason Wang
Quoted 6 lines · Ben Serebrin

From: Benjamin Serebrin<[email protected]>

If the number of virtio queue pairs is not equal to the number of VCPUs, the virtio guest driver doesn't assign any CPU affinity for the queue interrupts or the xps aggregation interrupt.

So this in fact is not a affinity fixing for #cpus > 32 but adding affinity for #cpus != #queue pairs.

Fair enough. I'll adjust the title line in the subsequent version.

Quoted 31 lines
Quoted 11 lines

Google Compute Engine currently provides 1 queue pair for every VCPU, but limits that at a maximum of 32 queue pairs.

This code assigns interrupt affinity even when there are more than 32 VCPUs.

Tested:

(on a 64-VCPU VM with debian 8, jessie-backports 4.9.2)

Without the fix we see all queues affinitized to all CPUs:

[...]

Quoted 18 lines

+ /* If there are more cpus than queues, then assign the queues'

drivers/net/virtio_net.c +7 -1
+        * interrupts to the first cpus until we run out.
+        */
         i = 0;
         for_each_online_cpu(cpu) {
+               if (i == vi->max_queue_pairs)
+                       break;
                 virtqueue_set_affinity(vi->rq[i].vq, cpu);
                 virtqueue_set_affinity(vi->sq[i].vq, cpu);
-               netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
                 i++;
         }
   +     /* Stripe the XPS affinities across the online CPUs.
+        * Hyperthread pairs are typically assigned such that Linux's
+        * CPU X and X + (numcpus / 2) are hyperthread twins, so we cause
+        * hyperthread twins to share TX queues, in the case where there

are
+ * more cpus than queues.

Quoted 4 lines
Quoted 1 line

Since we use combined queue pairs, why not use the same policy for RX?

XPS is for transmit only.

Yes, but I mean, e.g consider you let hyperthread twins to share TX queues (XPS), why not share TX and RX queue interrupts (affinity)?

Thanks

#10 lore raw
In reply to Ben Serebrin

On 02/03/2017 07:19 AM, Ben Serebrin wrote:
[...]

Quoted 22 lines
drivers/net/virtio_net.c +6 -2
@@ -1502,20 +1502,44 @@ static void virtnet_set_affinity(struct virtnet_info *vi)
 	 * queue pairs, we let the queue pairs to be private to one cpu by
 	 * setting the affinity hint to eliminate the contention.
 	 */
-	if (vi->curr_queue_pairs == 1 ||
-	    vi->max_queue_pairs != num_online_cpus()) {
+	if (vi->curr_queue_pairs == 1) {
 		virtnet_clean_affinity(vi, -1);
 		return;
 	}
 
+	/* If there are more cpus than queues, then assign the queues'
+	 * interrupts to the first cpus until we run out.
+	 */
 	i = 0;
 	for_each_online_cpu(cpu) {
+		if (i == vi->max_queue_pairs)
+			break;
 		virtqueue_set_affinity(vi->rq[i].vq, cpu);
 		virtqueue_set_affinity(vi->sq[i].vq, cpu);

drivers/net/virtio_net.c static void virtnet_set_affinity(struct virtnet_info *vi) · line 1527

Quoted 10 lines
drivers/net/virtio_net.c +6 -1
-		netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
 		i++;
 	}
 
+	/* Stripe the XPS affinities across the online CPUs.
+	 * Hyperthread pairs are typically assigned such that Linux's
+	 * CPU X and X + (numcpus / 2) are hyperthread twins, so we cause
+	 * hyperthread twins to share TX queues, in the case where there are
+	 * more cpus than queues.
+	 */

This is not always true. E.g. on s390 the SMT threads are usually paired even/odd.

e.g.

[cborntra@s38lp08 linux]$ lscpu -e
CPU NODE BOOK SOCKET CORE L1d:L1i:L2d:L2i ONLINE CONFIGURED POLARIZATION ADDRESS

0   0    0    0      0    0:0:0:0         yes    yes        horizontal   0
1   0    0    0      0    1:1:1:1         yes    yes        horizontal   1
2   0    0    0      1    2:2:2:2         yes    yes        horizontal   2
3   0    0    0      1    3:3:3:3         yes    yes        horizontal   3
4   0    0    0      2    4:4:4:4         yes    yes        horizontal   4
5   0    0    0      2    5:5:5:5         yes    yes        horizontal   5
6   0    0    0      3    6:6:6:6         yes    yes        horizontal   6

This does not matter yet for s390 (as virtio is usally doen via the ccw bus) but maybe we should consider an future patch to provide some arch-specific striping hints.

Or would it make sense to change the s390 layout for SMT twins because there is more code that expects all threads 0 at the front and all threads 1 at the end?

Quoted 19 lines
drivers/net/virtio_net.c +15 -0
+	for (i = 0; i < vi->max_queue_pairs; i++) {
+		struct cpumask mask;
+		int skip = i;
+
+		cpumask_clear(&mask);
+		for_each_online_cpu(cpu) {
+			while (skip--)
+				cpu = cpumask_next(cpu, cpu_online_mask);
+			if (cpu < num_possible_cpus())
+				cpumask_set_cpu(cpu, &mask);
+			skip = vi->max_queue_pairs - 1;
+		}
+		netif_set_xps_queue(vi->dev, &mask, i);
+	}
+
 	vi->affinity_hint_set = true;
 }